Commit | Line | Data |
---|---|---|
aab03e05 DF |
1 | /* |
2 | * Deadline Scheduling Class (SCHED_DEADLINE) | |
3 | * | |
4 | * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS). | |
5 | * | |
6 | * Tasks that periodically executes their instances for less than their | |
7 | * runtime won't miss any of their deadlines. | |
8 | * Tasks that are not periodic or sporadic or that tries to execute more | |
9 | * than their reserved bandwidth will be slowed down (and may potentially | |
10 | * miss some of their deadlines), and won't affect any other task. | |
11 | * | |
12 | * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>, | |
1baca4ce | 13 | * Juri Lelli <juri.lelli@gmail.com>, |
aab03e05 DF |
14 | * Michael Trimarchi <michael@amarulasolutions.com>, |
15 | * Fabio Checconi <fchecconi@gmail.com> | |
16 | */ | |
17 | #include "sched.h" | |
18 | ||
6bfd6d72 JL |
19 | #include <linux/slab.h> |
20 | ||
332ac17e DF |
21 | struct dl_bandwidth def_dl_bandwidth; |
22 | ||
aab03e05 DF |
23 | static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se) |
24 | { | |
25 | return container_of(dl_se, struct task_struct, dl); | |
26 | } | |
27 | ||
28 | static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq) | |
29 | { | |
30 | return container_of(dl_rq, struct rq, dl); | |
31 | } | |
32 | ||
33 | static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se) | |
34 | { | |
35 | struct task_struct *p = dl_task_of(dl_se); | |
36 | struct rq *rq = task_rq(p); | |
37 | ||
38 | return &rq->dl; | |
39 | } | |
40 | ||
41 | static inline int on_dl_rq(struct sched_dl_entity *dl_se) | |
42 | { | |
43 | return !RB_EMPTY_NODE(&dl_se->rb_node); | |
44 | } | |
45 | ||
46 | static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) | |
47 | { | |
48 | struct sched_dl_entity *dl_se = &p->dl; | |
49 | ||
50 | return dl_rq->rb_leftmost == &dl_se->rb_node; | |
51 | } | |
52 | ||
332ac17e DF |
53 | void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime) |
54 | { | |
55 | raw_spin_lock_init(&dl_b->dl_runtime_lock); | |
56 | dl_b->dl_period = period; | |
57 | dl_b->dl_runtime = runtime; | |
58 | } | |
59 | ||
60 | extern unsigned long to_ratio(u64 period, u64 runtime); | |
61 | ||
62 | void init_dl_bw(struct dl_bw *dl_b) | |
63 | { | |
64 | raw_spin_lock_init(&dl_b->lock); | |
65 | raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock); | |
1724813d | 66 | if (global_rt_runtime() == RUNTIME_INF) |
332ac17e DF |
67 | dl_b->bw = -1; |
68 | else | |
1724813d | 69 | dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime()); |
332ac17e DF |
70 | raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock); |
71 | dl_b->total_bw = 0; | |
72 | } | |
73 | ||
aab03e05 DF |
74 | void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq) |
75 | { | |
76 | dl_rq->rb_root = RB_ROOT; | |
1baca4ce JL |
77 | |
78 | #ifdef CONFIG_SMP | |
79 | /* zero means no -deadline tasks */ | |
80 | dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0; | |
81 | ||
82 | dl_rq->dl_nr_migratory = 0; | |
83 | dl_rq->overloaded = 0; | |
84 | dl_rq->pushable_dl_tasks_root = RB_ROOT; | |
332ac17e DF |
85 | #else |
86 | init_dl_bw(&dl_rq->dl_bw); | |
1baca4ce JL |
87 | #endif |
88 | } | |
89 | ||
90 | #ifdef CONFIG_SMP | |
91 | ||
92 | static inline int dl_overloaded(struct rq *rq) | |
93 | { | |
94 | return atomic_read(&rq->rd->dlo_count); | |
95 | } | |
96 | ||
97 | static inline void dl_set_overload(struct rq *rq) | |
98 | { | |
99 | if (!rq->online) | |
100 | return; | |
101 | ||
102 | cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask); | |
103 | /* | |
104 | * Must be visible before the overload count is | |
105 | * set (as in sched_rt.c). | |
106 | * | |
107 | * Matched by the barrier in pull_dl_task(). | |
108 | */ | |
109 | smp_wmb(); | |
110 | atomic_inc(&rq->rd->dlo_count); | |
111 | } | |
112 | ||
113 | static inline void dl_clear_overload(struct rq *rq) | |
114 | { | |
115 | if (!rq->online) | |
116 | return; | |
117 | ||
118 | atomic_dec(&rq->rd->dlo_count); | |
119 | cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask); | |
120 | } | |
121 | ||
122 | static void update_dl_migration(struct dl_rq *dl_rq) | |
123 | { | |
995b9ea4 | 124 | if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) { |
1baca4ce JL |
125 | if (!dl_rq->overloaded) { |
126 | dl_set_overload(rq_of_dl_rq(dl_rq)); | |
127 | dl_rq->overloaded = 1; | |
128 | } | |
129 | } else if (dl_rq->overloaded) { | |
130 | dl_clear_overload(rq_of_dl_rq(dl_rq)); | |
131 | dl_rq->overloaded = 0; | |
132 | } | |
133 | } | |
134 | ||
135 | static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
136 | { | |
137 | struct task_struct *p = dl_task_of(dl_se); | |
1baca4ce | 138 | |
1baca4ce JL |
139 | if (p->nr_cpus_allowed > 1) |
140 | dl_rq->dl_nr_migratory++; | |
141 | ||
142 | update_dl_migration(dl_rq); | |
143 | } | |
144 | ||
145 | static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
146 | { | |
147 | struct task_struct *p = dl_task_of(dl_se); | |
1baca4ce | 148 | |
1baca4ce JL |
149 | if (p->nr_cpus_allowed > 1) |
150 | dl_rq->dl_nr_migratory--; | |
151 | ||
152 | update_dl_migration(dl_rq); | |
153 | } | |
154 | ||
155 | /* | |
156 | * The list of pushable -deadline task is not a plist, like in | |
157 | * sched_rt.c, it is an rb-tree with tasks ordered by deadline. | |
158 | */ | |
159 | static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) | |
160 | { | |
161 | struct dl_rq *dl_rq = &rq->dl; | |
162 | struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node; | |
163 | struct rb_node *parent = NULL; | |
164 | struct task_struct *entry; | |
165 | int leftmost = 1; | |
166 | ||
167 | BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks)); | |
168 | ||
169 | while (*link) { | |
170 | parent = *link; | |
171 | entry = rb_entry(parent, struct task_struct, | |
172 | pushable_dl_tasks); | |
173 | if (dl_entity_preempt(&p->dl, &entry->dl)) | |
174 | link = &parent->rb_left; | |
175 | else { | |
176 | link = &parent->rb_right; | |
177 | leftmost = 0; | |
178 | } | |
179 | } | |
180 | ||
181 | if (leftmost) | |
182 | dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks; | |
183 | ||
184 | rb_link_node(&p->pushable_dl_tasks, parent, link); | |
185 | rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root); | |
aab03e05 DF |
186 | } |
187 | ||
1baca4ce JL |
188 | static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) |
189 | { | |
190 | struct dl_rq *dl_rq = &rq->dl; | |
191 | ||
192 | if (RB_EMPTY_NODE(&p->pushable_dl_tasks)) | |
193 | return; | |
194 | ||
195 | if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) { | |
196 | struct rb_node *next_node; | |
197 | ||
198 | next_node = rb_next(&p->pushable_dl_tasks); | |
199 | dl_rq->pushable_dl_tasks_leftmost = next_node; | |
200 | } | |
201 | ||
202 | rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root); | |
203 | RB_CLEAR_NODE(&p->pushable_dl_tasks); | |
204 | } | |
205 | ||
206 | static inline int has_pushable_dl_tasks(struct rq *rq) | |
207 | { | |
208 | return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root); | |
209 | } | |
210 | ||
211 | static int push_dl_task(struct rq *rq); | |
212 | ||
213 | #else | |
214 | ||
215 | static inline | |
216 | void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) | |
217 | { | |
218 | } | |
219 | ||
220 | static inline | |
221 | void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) | |
222 | { | |
223 | } | |
224 | ||
225 | static inline | |
226 | void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
227 | { | |
228 | } | |
229 | ||
230 | static inline | |
231 | void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
232 | { | |
233 | } | |
234 | ||
235 | #endif /* CONFIG_SMP */ | |
236 | ||
aab03e05 DF |
237 | static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags); |
238 | static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags); | |
239 | static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, | |
240 | int flags); | |
241 | ||
242 | /* | |
243 | * We are being explicitly informed that a new instance is starting, | |
244 | * and this means that: | |
245 | * - the absolute deadline of the entity has to be placed at | |
246 | * current time + relative deadline; | |
247 | * - the runtime of the entity has to be set to the maximum value. | |
248 | * | |
249 | * The capability of specifying such event is useful whenever a -deadline | |
250 | * entity wants to (try to!) synchronize its behaviour with the scheduler's | |
251 | * one, and to (try to!) reconcile itself with its own scheduling | |
252 | * parameters. | |
253 | */ | |
2d3d891d DF |
254 | static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se, |
255 | struct sched_dl_entity *pi_se) | |
aab03e05 DF |
256 | { |
257 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
258 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
259 | ||
260 | WARN_ON(!dl_se->dl_new || dl_se->dl_throttled); | |
261 | ||
262 | /* | |
263 | * We use the regular wall clock time to set deadlines in the | |
264 | * future; in fact, we must consider execution overheads (time | |
265 | * spent on hardirq context, etc.). | |
266 | */ | |
2d3d891d DF |
267 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; |
268 | dl_se->runtime = pi_se->dl_runtime; | |
aab03e05 DF |
269 | dl_se->dl_new = 0; |
270 | } | |
271 | ||
272 | /* | |
273 | * Pure Earliest Deadline First (EDF) scheduling does not deal with the | |
274 | * possibility of a entity lasting more than what it declared, and thus | |
275 | * exhausting its runtime. | |
276 | * | |
277 | * Here we are interested in making runtime overrun possible, but we do | |
278 | * not want a entity which is misbehaving to affect the scheduling of all | |
279 | * other entities. | |
280 | * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS) | |
281 | * is used, in order to confine each entity within its own bandwidth. | |
282 | * | |
283 | * This function deals exactly with that, and ensures that when the runtime | |
284 | * of a entity is replenished, its deadline is also postponed. That ensures | |
285 | * the overrunning entity can't interfere with other entity in the system and | |
286 | * can't make them miss their deadlines. Reasons why this kind of overruns | |
287 | * could happen are, typically, a entity voluntarily trying to overcome its | |
288 | * runtime, or it just underestimated it during sched_setscheduler_ex(). | |
289 | */ | |
2d3d891d DF |
290 | static void replenish_dl_entity(struct sched_dl_entity *dl_se, |
291 | struct sched_dl_entity *pi_se) | |
aab03e05 DF |
292 | { |
293 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
294 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
295 | ||
2d3d891d DF |
296 | BUG_ON(pi_se->dl_runtime <= 0); |
297 | ||
298 | /* | |
299 | * This could be the case for a !-dl task that is boosted. | |
300 | * Just go with full inherited parameters. | |
301 | */ | |
302 | if (dl_se->dl_deadline == 0) { | |
303 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; | |
304 | dl_se->runtime = pi_se->dl_runtime; | |
305 | } | |
306 | ||
aab03e05 DF |
307 | /* |
308 | * We keep moving the deadline away until we get some | |
309 | * available runtime for the entity. This ensures correct | |
310 | * handling of situations where the runtime overrun is | |
311 | * arbitrary large. | |
312 | */ | |
313 | while (dl_se->runtime <= 0) { | |
2d3d891d DF |
314 | dl_se->deadline += pi_se->dl_period; |
315 | dl_se->runtime += pi_se->dl_runtime; | |
aab03e05 DF |
316 | } |
317 | ||
318 | /* | |
319 | * At this point, the deadline really should be "in | |
320 | * the future" with respect to rq->clock. If it's | |
321 | * not, we are, for some reason, lagging too much! | |
322 | * Anyway, after having warn userspace abut that, | |
323 | * we still try to keep the things running by | |
324 | * resetting the deadline and the budget of the | |
325 | * entity. | |
326 | */ | |
327 | if (dl_time_before(dl_se->deadline, rq_clock(rq))) { | |
328 | static bool lag_once = false; | |
329 | ||
330 | if (!lag_once) { | |
331 | lag_once = true; | |
332 | printk_sched("sched: DL replenish lagged to much\n"); | |
333 | } | |
2d3d891d DF |
334 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; |
335 | dl_se->runtime = pi_se->dl_runtime; | |
aab03e05 DF |
336 | } |
337 | } | |
338 | ||
339 | /* | |
340 | * Here we check if --at time t-- an entity (which is probably being | |
341 | * [re]activated or, in general, enqueued) can use its remaining runtime | |
342 | * and its current deadline _without_ exceeding the bandwidth it is | |
343 | * assigned (function returns true if it can't). We are in fact applying | |
344 | * one of the CBS rules: when a task wakes up, if the residual runtime | |
345 | * over residual deadline fits within the allocated bandwidth, then we | |
346 | * can keep the current (absolute) deadline and residual budget without | |
347 | * disrupting the schedulability of the system. Otherwise, we should | |
348 | * refill the runtime and set the deadline a period in the future, | |
349 | * because keeping the current (absolute) deadline of the task would | |
712e5e34 DF |
350 | * result in breaking guarantees promised to other tasks (refer to |
351 | * Documentation/scheduler/sched-deadline.txt for more informations). | |
aab03e05 DF |
352 | * |
353 | * This function returns true if: | |
354 | * | |
755378a4 | 355 | * runtime / (deadline - t) > dl_runtime / dl_period , |
aab03e05 DF |
356 | * |
357 | * IOW we can't recycle current parameters. | |
755378a4 HG |
358 | * |
359 | * Notice that the bandwidth check is done against the period. For | |
360 | * task with deadline equal to period this is the same of using | |
361 | * dl_deadline instead of dl_period in the equation above. | |
aab03e05 | 362 | */ |
2d3d891d DF |
363 | static bool dl_entity_overflow(struct sched_dl_entity *dl_se, |
364 | struct sched_dl_entity *pi_se, u64 t) | |
aab03e05 DF |
365 | { |
366 | u64 left, right; | |
367 | ||
368 | /* | |
369 | * left and right are the two sides of the equation above, | |
370 | * after a bit of shuffling to use multiplications instead | |
371 | * of divisions. | |
372 | * | |
373 | * Note that none of the time values involved in the two | |
374 | * multiplications are absolute: dl_deadline and dl_runtime | |
375 | * are the relative deadline and the maximum runtime of each | |
376 | * instance, runtime is the runtime left for the last instance | |
377 | * and (deadline - t), since t is rq->clock, is the time left | |
378 | * to the (absolute) deadline. Even if overflowing the u64 type | |
379 | * is very unlikely to occur in both cases, here we scale down | |
380 | * as we want to avoid that risk at all. Scaling down by 10 | |
381 | * means that we reduce granularity to 1us. We are fine with it, | |
382 | * since this is only a true/false check and, anyway, thinking | |
383 | * of anything below microseconds resolution is actually fiction | |
384 | * (but still we want to give the user that illusion >;). | |
385 | */ | |
332ac17e DF |
386 | left = (pi_se->dl_period >> DL_SCALE) * (dl_se->runtime >> DL_SCALE); |
387 | right = ((dl_se->deadline - t) >> DL_SCALE) * | |
388 | (pi_se->dl_runtime >> DL_SCALE); | |
aab03e05 DF |
389 | |
390 | return dl_time_before(right, left); | |
391 | } | |
392 | ||
393 | /* | |
394 | * When a -deadline entity is queued back on the runqueue, its runtime and | |
395 | * deadline might need updating. | |
396 | * | |
397 | * The policy here is that we update the deadline of the entity only if: | |
398 | * - the current deadline is in the past, | |
399 | * - using the remaining runtime with the current deadline would make | |
400 | * the entity exceed its bandwidth. | |
401 | */ | |
2d3d891d DF |
402 | static void update_dl_entity(struct sched_dl_entity *dl_se, |
403 | struct sched_dl_entity *pi_se) | |
aab03e05 DF |
404 | { |
405 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
406 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
407 | ||
408 | /* | |
409 | * The arrival of a new instance needs special treatment, i.e., | |
410 | * the actual scheduling parameters have to be "renewed". | |
411 | */ | |
412 | if (dl_se->dl_new) { | |
2d3d891d | 413 | setup_new_dl_entity(dl_se, pi_se); |
aab03e05 DF |
414 | return; |
415 | } | |
416 | ||
417 | if (dl_time_before(dl_se->deadline, rq_clock(rq)) || | |
2d3d891d DF |
418 | dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) { |
419 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; | |
420 | dl_se->runtime = pi_se->dl_runtime; | |
aab03e05 DF |
421 | } |
422 | } | |
423 | ||
424 | /* | |
425 | * If the entity depleted all its runtime, and if we want it to sleep | |
426 | * while waiting for some new execution time to become available, we | |
427 | * set the bandwidth enforcement timer to the replenishment instant | |
428 | * and try to activate it. | |
429 | * | |
430 | * Notice that it is important for the caller to know if the timer | |
431 | * actually started or not (i.e., the replenishment instant is in | |
432 | * the future or in the past). | |
433 | */ | |
2d3d891d | 434 | static int start_dl_timer(struct sched_dl_entity *dl_se, bool boosted) |
aab03e05 DF |
435 | { |
436 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
437 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
438 | ktime_t now, act; | |
439 | ktime_t soft, hard; | |
440 | unsigned long range; | |
441 | s64 delta; | |
442 | ||
2d3d891d DF |
443 | if (boosted) |
444 | return 0; | |
aab03e05 DF |
445 | /* |
446 | * We want the timer to fire at the deadline, but considering | |
447 | * that it is actually coming from rq->clock and not from | |
448 | * hrtimer's time base reading. | |
449 | */ | |
450 | act = ns_to_ktime(dl_se->deadline); | |
451 | now = hrtimer_cb_get_time(&dl_se->dl_timer); | |
452 | delta = ktime_to_ns(now) - rq_clock(rq); | |
453 | act = ktime_add_ns(act, delta); | |
454 | ||
455 | /* | |
456 | * If the expiry time already passed, e.g., because the value | |
457 | * chosen as the deadline is too small, don't even try to | |
458 | * start the timer in the past! | |
459 | */ | |
460 | if (ktime_us_delta(act, now) < 0) | |
461 | return 0; | |
462 | ||
463 | hrtimer_set_expires(&dl_se->dl_timer, act); | |
464 | ||
465 | soft = hrtimer_get_softexpires(&dl_se->dl_timer); | |
466 | hard = hrtimer_get_expires(&dl_se->dl_timer); | |
467 | range = ktime_to_ns(ktime_sub(hard, soft)); | |
468 | __hrtimer_start_range_ns(&dl_se->dl_timer, soft, | |
469 | range, HRTIMER_MODE_ABS, 0); | |
470 | ||
471 | return hrtimer_active(&dl_se->dl_timer); | |
472 | } | |
473 | ||
474 | /* | |
475 | * This is the bandwidth enforcement timer callback. If here, we know | |
476 | * a task is not on its dl_rq, since the fact that the timer was running | |
477 | * means the task is throttled and needs a runtime replenishment. | |
478 | * | |
479 | * However, what we actually do depends on the fact the task is active, | |
480 | * (it is on its rq) or has been removed from there by a call to | |
481 | * dequeue_task_dl(). In the former case we must issue the runtime | |
482 | * replenishment and add the task back to the dl_rq; in the latter, we just | |
483 | * do nothing but clearing dl_throttled, so that runtime and deadline | |
484 | * updating (and the queueing back to dl_rq) will be done by the | |
485 | * next call to enqueue_task_dl(). | |
486 | */ | |
487 | static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) | |
488 | { | |
489 | struct sched_dl_entity *dl_se = container_of(timer, | |
490 | struct sched_dl_entity, | |
491 | dl_timer); | |
492 | struct task_struct *p = dl_task_of(dl_se); | |
493 | struct rq *rq = task_rq(p); | |
494 | raw_spin_lock(&rq->lock); | |
495 | ||
496 | /* | |
497 | * We need to take care of a possible races here. In fact, the | |
498 | * task might have changed its scheduling policy to something | |
499 | * different from SCHED_DEADLINE or changed its reservation | |
500 | * parameters (through sched_setscheduler()). | |
501 | */ | |
502 | if (!dl_task(p) || dl_se->dl_new) | |
503 | goto unlock; | |
504 | ||
505 | sched_clock_tick(); | |
506 | update_rq_clock(rq); | |
507 | dl_se->dl_throttled = 0; | |
508 | if (p->on_rq) { | |
509 | enqueue_task_dl(rq, p, ENQUEUE_REPLENISH); | |
510 | if (task_has_dl_policy(rq->curr)) | |
511 | check_preempt_curr_dl(rq, p, 0); | |
512 | else | |
513 | resched_task(rq->curr); | |
1baca4ce JL |
514 | #ifdef CONFIG_SMP |
515 | /* | |
516 | * Queueing this task back might have overloaded rq, | |
517 | * check if we need to kick someone away. | |
518 | */ | |
519 | if (has_pushable_dl_tasks(rq)) | |
520 | push_dl_task(rq); | |
521 | #endif | |
aab03e05 DF |
522 | } |
523 | unlock: | |
524 | raw_spin_unlock(&rq->lock); | |
525 | ||
526 | return HRTIMER_NORESTART; | |
527 | } | |
528 | ||
529 | void init_dl_task_timer(struct sched_dl_entity *dl_se) | |
530 | { | |
531 | struct hrtimer *timer = &dl_se->dl_timer; | |
532 | ||
533 | if (hrtimer_active(timer)) { | |
534 | hrtimer_try_to_cancel(timer); | |
535 | return; | |
536 | } | |
537 | ||
538 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
539 | timer->function = dl_task_timer; | |
540 | } | |
541 | ||
542 | static | |
543 | int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se) | |
544 | { | |
545 | int dmiss = dl_time_before(dl_se->deadline, rq_clock(rq)); | |
546 | int rorun = dl_se->runtime <= 0; | |
547 | ||
548 | if (!rorun && !dmiss) | |
549 | return 0; | |
550 | ||
551 | /* | |
552 | * If we are beyond our current deadline and we are still | |
553 | * executing, then we have already used some of the runtime of | |
554 | * the next instance. Thus, if we do not account that, we are | |
555 | * stealing bandwidth from the system at each deadline miss! | |
556 | */ | |
557 | if (dmiss) { | |
558 | dl_se->runtime = rorun ? dl_se->runtime : 0; | |
559 | dl_se->runtime -= rq_clock(rq) - dl_se->deadline; | |
560 | } | |
561 | ||
562 | return 1; | |
563 | } | |
564 | ||
565 | /* | |
566 | * Update the current task's runtime statistics (provided it is still | |
567 | * a -deadline task and has not been removed from the dl_rq). | |
568 | */ | |
569 | static void update_curr_dl(struct rq *rq) | |
570 | { | |
571 | struct task_struct *curr = rq->curr; | |
572 | struct sched_dl_entity *dl_se = &curr->dl; | |
573 | u64 delta_exec; | |
574 | ||
575 | if (!dl_task(curr) || !on_dl_rq(dl_se)) | |
576 | return; | |
577 | ||
578 | /* | |
579 | * Consumed budget is computed considering the time as | |
580 | * observed by schedulable tasks (excluding time spent | |
581 | * in hardirq context, etc.). Deadlines are instead | |
582 | * computed using hard walltime. This seems to be the more | |
583 | * natural solution, but the full ramifications of this | |
584 | * approach need further study. | |
585 | */ | |
586 | delta_exec = rq_clock_task(rq) - curr->se.exec_start; | |
587 | if (unlikely((s64)delta_exec < 0)) | |
588 | delta_exec = 0; | |
589 | ||
590 | schedstat_set(curr->se.statistics.exec_max, | |
591 | max(curr->se.statistics.exec_max, delta_exec)); | |
592 | ||
593 | curr->se.sum_exec_runtime += delta_exec; | |
594 | account_group_exec_runtime(curr, delta_exec); | |
595 | ||
596 | curr->se.exec_start = rq_clock_task(rq); | |
597 | cpuacct_charge(curr, delta_exec); | |
598 | ||
239be4a9 DF |
599 | sched_rt_avg_update(rq, delta_exec); |
600 | ||
aab03e05 DF |
601 | dl_se->runtime -= delta_exec; |
602 | if (dl_runtime_exceeded(rq, dl_se)) { | |
603 | __dequeue_task_dl(rq, curr, 0); | |
2d3d891d | 604 | if (likely(start_dl_timer(dl_se, curr->dl.dl_boosted))) |
aab03e05 DF |
605 | dl_se->dl_throttled = 1; |
606 | else | |
607 | enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH); | |
608 | ||
609 | if (!is_leftmost(curr, &rq->dl)) | |
610 | resched_task(curr); | |
611 | } | |
1724813d PZ |
612 | |
613 | /* | |
614 | * Because -- for now -- we share the rt bandwidth, we need to | |
615 | * account our runtime there too, otherwise actual rt tasks | |
616 | * would be able to exceed the shared quota. | |
617 | * | |
618 | * Account to the root rt group for now. | |
619 | * | |
620 | * The solution we're working towards is having the RT groups scheduled | |
621 | * using deadline servers -- however there's a few nasties to figure | |
622 | * out before that can happen. | |
623 | */ | |
624 | if (rt_bandwidth_enabled()) { | |
625 | struct rt_rq *rt_rq = &rq->rt; | |
626 | ||
627 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
628 | rt_rq->rt_time += delta_exec; | |
629 | /* | |
630 | * We'll let actual RT tasks worry about the overflow here, we | |
631 | * have our own CBS to keep us inline -- see above. | |
632 | */ | |
633 | raw_spin_unlock(&rt_rq->rt_runtime_lock); | |
634 | } | |
aab03e05 DF |
635 | } |
636 | ||
1baca4ce JL |
637 | #ifdef CONFIG_SMP |
638 | ||
639 | static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu); | |
640 | ||
641 | static inline u64 next_deadline(struct rq *rq) | |
642 | { | |
643 | struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu); | |
644 | ||
645 | if (next && dl_prio(next->prio)) | |
646 | return next->dl.deadline; | |
647 | else | |
648 | return 0; | |
649 | } | |
650 | ||
651 | static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) | |
652 | { | |
653 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
654 | ||
655 | if (dl_rq->earliest_dl.curr == 0 || | |
656 | dl_time_before(deadline, dl_rq->earliest_dl.curr)) { | |
657 | /* | |
658 | * If the dl_rq had no -deadline tasks, or if the new task | |
659 | * has shorter deadline than the current one on dl_rq, we | |
660 | * know that the previous earliest becomes our next earliest, | |
661 | * as the new task becomes the earliest itself. | |
662 | */ | |
663 | dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr; | |
664 | dl_rq->earliest_dl.curr = deadline; | |
6bfd6d72 | 665 | cpudl_set(&rq->rd->cpudl, rq->cpu, deadline, 1); |
1baca4ce JL |
666 | } else if (dl_rq->earliest_dl.next == 0 || |
667 | dl_time_before(deadline, dl_rq->earliest_dl.next)) { | |
668 | /* | |
669 | * On the other hand, if the new -deadline task has a | |
670 | * a later deadline than the earliest one on dl_rq, but | |
671 | * it is earlier than the next (if any), we must | |
672 | * recompute the next-earliest. | |
673 | */ | |
674 | dl_rq->earliest_dl.next = next_deadline(rq); | |
675 | } | |
676 | } | |
677 | ||
678 | static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) | |
679 | { | |
680 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
681 | ||
682 | /* | |
683 | * Since we may have removed our earliest (and/or next earliest) | |
684 | * task we must recompute them. | |
685 | */ | |
686 | if (!dl_rq->dl_nr_running) { | |
687 | dl_rq->earliest_dl.curr = 0; | |
688 | dl_rq->earliest_dl.next = 0; | |
6bfd6d72 | 689 | cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0); |
1baca4ce JL |
690 | } else { |
691 | struct rb_node *leftmost = dl_rq->rb_leftmost; | |
692 | struct sched_dl_entity *entry; | |
693 | ||
694 | entry = rb_entry(leftmost, struct sched_dl_entity, rb_node); | |
695 | dl_rq->earliest_dl.curr = entry->deadline; | |
696 | dl_rq->earliest_dl.next = next_deadline(rq); | |
6bfd6d72 | 697 | cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline, 1); |
1baca4ce JL |
698 | } |
699 | } | |
700 | ||
701 | #else | |
702 | ||
703 | static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} | |
704 | static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} | |
705 | ||
706 | #endif /* CONFIG_SMP */ | |
707 | ||
708 | static inline | |
709 | void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
710 | { | |
711 | int prio = dl_task_of(dl_se)->prio; | |
712 | u64 deadline = dl_se->deadline; | |
713 | ||
714 | WARN_ON(!dl_prio(prio)); | |
715 | dl_rq->dl_nr_running++; | |
3d5f35bd | 716 | inc_nr_running(rq_of_dl_rq(dl_rq)); |
1baca4ce JL |
717 | |
718 | inc_dl_deadline(dl_rq, deadline); | |
719 | inc_dl_migration(dl_se, dl_rq); | |
720 | } | |
721 | ||
722 | static inline | |
723 | void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
724 | { | |
725 | int prio = dl_task_of(dl_se)->prio; | |
726 | ||
727 | WARN_ON(!dl_prio(prio)); | |
728 | WARN_ON(!dl_rq->dl_nr_running); | |
729 | dl_rq->dl_nr_running--; | |
3d5f35bd | 730 | dec_nr_running(rq_of_dl_rq(dl_rq)); |
1baca4ce JL |
731 | |
732 | dec_dl_deadline(dl_rq, dl_se->deadline); | |
733 | dec_dl_migration(dl_se, dl_rq); | |
734 | } | |
735 | ||
aab03e05 DF |
736 | static void __enqueue_dl_entity(struct sched_dl_entity *dl_se) |
737 | { | |
738 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
739 | struct rb_node **link = &dl_rq->rb_root.rb_node; | |
740 | struct rb_node *parent = NULL; | |
741 | struct sched_dl_entity *entry; | |
742 | int leftmost = 1; | |
743 | ||
744 | BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node)); | |
745 | ||
746 | while (*link) { | |
747 | parent = *link; | |
748 | entry = rb_entry(parent, struct sched_dl_entity, rb_node); | |
749 | if (dl_time_before(dl_se->deadline, entry->deadline)) | |
750 | link = &parent->rb_left; | |
751 | else { | |
752 | link = &parent->rb_right; | |
753 | leftmost = 0; | |
754 | } | |
755 | } | |
756 | ||
757 | if (leftmost) | |
758 | dl_rq->rb_leftmost = &dl_se->rb_node; | |
759 | ||
760 | rb_link_node(&dl_se->rb_node, parent, link); | |
761 | rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root); | |
762 | ||
1baca4ce | 763 | inc_dl_tasks(dl_se, dl_rq); |
aab03e05 DF |
764 | } |
765 | ||
766 | static void __dequeue_dl_entity(struct sched_dl_entity *dl_se) | |
767 | { | |
768 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
769 | ||
770 | if (RB_EMPTY_NODE(&dl_se->rb_node)) | |
771 | return; | |
772 | ||
773 | if (dl_rq->rb_leftmost == &dl_se->rb_node) { | |
774 | struct rb_node *next_node; | |
775 | ||
776 | next_node = rb_next(&dl_se->rb_node); | |
777 | dl_rq->rb_leftmost = next_node; | |
778 | } | |
779 | ||
780 | rb_erase(&dl_se->rb_node, &dl_rq->rb_root); | |
781 | RB_CLEAR_NODE(&dl_se->rb_node); | |
782 | ||
1baca4ce | 783 | dec_dl_tasks(dl_se, dl_rq); |
aab03e05 DF |
784 | } |
785 | ||
786 | static void | |
2d3d891d DF |
787 | enqueue_dl_entity(struct sched_dl_entity *dl_se, |
788 | struct sched_dl_entity *pi_se, int flags) | |
aab03e05 DF |
789 | { |
790 | BUG_ON(on_dl_rq(dl_se)); | |
791 | ||
792 | /* | |
793 | * If this is a wakeup or a new instance, the scheduling | |
794 | * parameters of the task might need updating. Otherwise, | |
795 | * we want a replenishment of its runtime. | |
796 | */ | |
797 | if (!dl_se->dl_new && flags & ENQUEUE_REPLENISH) | |
2d3d891d | 798 | replenish_dl_entity(dl_se, pi_se); |
aab03e05 | 799 | else |
2d3d891d | 800 | update_dl_entity(dl_se, pi_se); |
aab03e05 DF |
801 | |
802 | __enqueue_dl_entity(dl_se); | |
803 | } | |
804 | ||
805 | static void dequeue_dl_entity(struct sched_dl_entity *dl_se) | |
806 | { | |
807 | __dequeue_dl_entity(dl_se); | |
808 | } | |
809 | ||
810 | static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags) | |
811 | { | |
2d3d891d DF |
812 | struct task_struct *pi_task = rt_mutex_get_top_task(p); |
813 | struct sched_dl_entity *pi_se = &p->dl; | |
814 | ||
815 | /* | |
816 | * Use the scheduling parameters of the top pi-waiter | |
817 | * task if we have one and its (relative) deadline is | |
818 | * smaller than our one... OTW we keep our runtime and | |
819 | * deadline. | |
820 | */ | |
821 | if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) | |
822 | pi_se = &pi_task->dl; | |
823 | ||
aab03e05 DF |
824 | /* |
825 | * If p is throttled, we do nothing. In fact, if it exhausted | |
826 | * its budget it needs a replenishment and, since it now is on | |
827 | * its rq, the bandwidth timer callback (which clearly has not | |
828 | * run yet) will take care of this. | |
829 | */ | |
830 | if (p->dl.dl_throttled) | |
831 | return; | |
832 | ||
2d3d891d | 833 | enqueue_dl_entity(&p->dl, pi_se, flags); |
1baca4ce JL |
834 | |
835 | if (!task_current(rq, p) && p->nr_cpus_allowed > 1) | |
836 | enqueue_pushable_dl_task(rq, p); | |
aab03e05 DF |
837 | } |
838 | ||
839 | static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) | |
840 | { | |
841 | dequeue_dl_entity(&p->dl); | |
1baca4ce | 842 | dequeue_pushable_dl_task(rq, p); |
aab03e05 DF |
843 | } |
844 | ||
845 | static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) | |
846 | { | |
847 | update_curr_dl(rq); | |
848 | __dequeue_task_dl(rq, p, flags); | |
aab03e05 DF |
849 | } |
850 | ||
851 | /* | |
852 | * Yield task semantic for -deadline tasks is: | |
853 | * | |
854 | * get off from the CPU until our next instance, with | |
855 | * a new runtime. This is of little use now, since we | |
856 | * don't have a bandwidth reclaiming mechanism. Anyway, | |
857 | * bandwidth reclaiming is planned for the future, and | |
858 | * yield_task_dl will indicate that some spare budget | |
859 | * is available for other task instances to use it. | |
860 | */ | |
861 | static void yield_task_dl(struct rq *rq) | |
862 | { | |
863 | struct task_struct *p = rq->curr; | |
864 | ||
865 | /* | |
866 | * We make the task go to sleep until its current deadline by | |
867 | * forcing its runtime to zero. This way, update_curr_dl() stops | |
868 | * it and the bandwidth timer will wake it up and will give it | |
869 | * new scheduling parameters (thanks to dl_new=1). | |
870 | */ | |
871 | if (p->dl.runtime > 0) { | |
872 | rq->curr->dl.dl_new = 1; | |
873 | p->dl.runtime = 0; | |
874 | } | |
875 | update_curr_dl(rq); | |
876 | } | |
877 | ||
1baca4ce JL |
878 | #ifdef CONFIG_SMP |
879 | ||
880 | static int find_later_rq(struct task_struct *task); | |
1baca4ce JL |
881 | |
882 | static int | |
883 | select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags) | |
884 | { | |
885 | struct task_struct *curr; | |
886 | struct rq *rq; | |
887 | ||
888 | if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK) | |
889 | goto out; | |
890 | ||
891 | rq = cpu_rq(cpu); | |
892 | ||
893 | rcu_read_lock(); | |
894 | curr = ACCESS_ONCE(rq->curr); /* unlocked access */ | |
895 | ||
896 | /* | |
897 | * If we are dealing with a -deadline task, we must | |
898 | * decide where to wake it up. | |
899 | * If it has a later deadline and the current task | |
900 | * on this rq can't move (provided the waking task | |
901 | * can!) we prefer to send it somewhere else. On the | |
902 | * other hand, if it has a shorter deadline, we | |
903 | * try to make it stay here, it might be important. | |
904 | */ | |
905 | if (unlikely(dl_task(curr)) && | |
906 | (curr->nr_cpus_allowed < 2 || | |
907 | !dl_entity_preempt(&p->dl, &curr->dl)) && | |
908 | (p->nr_cpus_allowed > 1)) { | |
909 | int target = find_later_rq(p); | |
910 | ||
911 | if (target != -1) | |
912 | cpu = target; | |
913 | } | |
914 | rcu_read_unlock(); | |
915 | ||
916 | out: | |
917 | return cpu; | |
918 | } | |
919 | ||
920 | static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p) | |
921 | { | |
922 | /* | |
923 | * Current can't be migrated, useless to reschedule, | |
924 | * let's hope p can move out. | |
925 | */ | |
926 | if (rq->curr->nr_cpus_allowed == 1 || | |
6bfd6d72 | 927 | cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1) |
1baca4ce JL |
928 | return; |
929 | ||
930 | /* | |
931 | * p is migratable, so let's not schedule it and | |
932 | * see if it is pushed or pulled somewhere else. | |
933 | */ | |
934 | if (p->nr_cpus_allowed != 1 && | |
6bfd6d72 | 935 | cpudl_find(&rq->rd->cpudl, p, NULL) != -1) |
1baca4ce JL |
936 | return; |
937 | ||
938 | resched_task(rq->curr); | |
939 | } | |
940 | ||
941 | #endif /* CONFIG_SMP */ | |
942 | ||
aab03e05 DF |
943 | /* |
944 | * Only called when both the current and waking task are -deadline | |
945 | * tasks. | |
946 | */ | |
947 | static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, | |
948 | int flags) | |
949 | { | |
1baca4ce | 950 | if (dl_entity_preempt(&p->dl, &rq->curr->dl)) { |
aab03e05 | 951 | resched_task(rq->curr); |
1baca4ce JL |
952 | return; |
953 | } | |
954 | ||
955 | #ifdef CONFIG_SMP | |
956 | /* | |
957 | * In the unlikely case current and p have the same deadline | |
958 | * let us try to decide what's the best thing to do... | |
959 | */ | |
332ac17e DF |
960 | if ((p->dl.deadline == rq->curr->dl.deadline) && |
961 | !test_tsk_need_resched(rq->curr)) | |
1baca4ce JL |
962 | check_preempt_equal_dl(rq, p); |
963 | #endif /* CONFIG_SMP */ | |
aab03e05 DF |
964 | } |
965 | ||
966 | #ifdef CONFIG_SCHED_HRTICK | |
967 | static void start_hrtick_dl(struct rq *rq, struct task_struct *p) | |
968 | { | |
969 | s64 delta = p->dl.dl_runtime - p->dl.runtime; | |
970 | ||
971 | if (delta > 10000) | |
972 | hrtick_start(rq, p->dl.runtime); | |
973 | } | |
974 | #endif | |
975 | ||
976 | static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq, | |
977 | struct dl_rq *dl_rq) | |
978 | { | |
979 | struct rb_node *left = dl_rq->rb_leftmost; | |
980 | ||
981 | if (!left) | |
982 | return NULL; | |
983 | ||
984 | return rb_entry(left, struct sched_dl_entity, rb_node); | |
985 | } | |
986 | ||
987 | struct task_struct *pick_next_task_dl(struct rq *rq) | |
988 | { | |
989 | struct sched_dl_entity *dl_se; | |
990 | struct task_struct *p; | |
991 | struct dl_rq *dl_rq; | |
992 | ||
993 | dl_rq = &rq->dl; | |
994 | ||
995 | if (unlikely(!dl_rq->dl_nr_running)) | |
996 | return NULL; | |
997 | ||
998 | dl_se = pick_next_dl_entity(rq, dl_rq); | |
999 | BUG_ON(!dl_se); | |
1000 | ||
1001 | p = dl_task_of(dl_se); | |
1002 | p->se.exec_start = rq_clock_task(rq); | |
1baca4ce JL |
1003 | |
1004 | /* Running task will never be pushed. */ | |
71362650 | 1005 | dequeue_pushable_dl_task(rq, p); |
1baca4ce | 1006 | |
aab03e05 DF |
1007 | #ifdef CONFIG_SCHED_HRTICK |
1008 | if (hrtick_enabled(rq)) | |
1009 | start_hrtick_dl(rq, p); | |
1010 | #endif | |
1baca4ce JL |
1011 | |
1012 | #ifdef CONFIG_SMP | |
1013 | rq->post_schedule = has_pushable_dl_tasks(rq); | |
1014 | #endif /* CONFIG_SMP */ | |
1015 | ||
aab03e05 DF |
1016 | return p; |
1017 | } | |
1018 | ||
1019 | static void put_prev_task_dl(struct rq *rq, struct task_struct *p) | |
1020 | { | |
1021 | update_curr_dl(rq); | |
1baca4ce JL |
1022 | |
1023 | if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1) | |
1024 | enqueue_pushable_dl_task(rq, p); | |
aab03e05 DF |
1025 | } |
1026 | ||
1027 | static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued) | |
1028 | { | |
1029 | update_curr_dl(rq); | |
1030 | ||
1031 | #ifdef CONFIG_SCHED_HRTICK | |
1032 | if (hrtick_enabled(rq) && queued && p->dl.runtime > 0) | |
1033 | start_hrtick_dl(rq, p); | |
1034 | #endif | |
1035 | } | |
1036 | ||
1037 | static void task_fork_dl(struct task_struct *p) | |
1038 | { | |
1039 | /* | |
1040 | * SCHED_DEADLINE tasks cannot fork and this is achieved through | |
1041 | * sched_fork() | |
1042 | */ | |
1043 | } | |
1044 | ||
1045 | static void task_dead_dl(struct task_struct *p) | |
1046 | { | |
1047 | struct hrtimer *timer = &p->dl.dl_timer; | |
332ac17e DF |
1048 | struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); |
1049 | ||
1050 | /* | |
1051 | * Since we are TASK_DEAD we won't slip out of the domain! | |
1052 | */ | |
1053 | raw_spin_lock_irq(&dl_b->lock); | |
1054 | dl_b->total_bw -= p->dl.dl_bw; | |
1055 | raw_spin_unlock_irq(&dl_b->lock); | |
aab03e05 | 1056 | |
2d3d891d | 1057 | hrtimer_cancel(timer); |
aab03e05 DF |
1058 | } |
1059 | ||
1060 | static void set_curr_task_dl(struct rq *rq) | |
1061 | { | |
1062 | struct task_struct *p = rq->curr; | |
1063 | ||
1064 | p->se.exec_start = rq_clock_task(rq); | |
1baca4ce JL |
1065 | |
1066 | /* You can't push away the running task */ | |
1067 | dequeue_pushable_dl_task(rq, p); | |
1068 | } | |
1069 | ||
1070 | #ifdef CONFIG_SMP | |
1071 | ||
1072 | /* Only try algorithms three times */ | |
1073 | #define DL_MAX_TRIES 3 | |
1074 | ||
1075 | static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu) | |
1076 | { | |
1077 | if (!task_running(rq, p) && | |
1078 | (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) && | |
1079 | (p->nr_cpus_allowed > 1)) | |
1080 | return 1; | |
1081 | ||
1082 | return 0; | |
1083 | } | |
1084 | ||
1085 | /* Returns the second earliest -deadline task, NULL otherwise */ | |
1086 | static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu) | |
1087 | { | |
1088 | struct rb_node *next_node = rq->dl.rb_leftmost; | |
1089 | struct sched_dl_entity *dl_se; | |
1090 | struct task_struct *p = NULL; | |
1091 | ||
1092 | next_node: | |
1093 | next_node = rb_next(next_node); | |
1094 | if (next_node) { | |
1095 | dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node); | |
1096 | p = dl_task_of(dl_se); | |
1097 | ||
1098 | if (pick_dl_task(rq, p, cpu)) | |
1099 | return p; | |
1100 | ||
1101 | goto next_node; | |
1102 | } | |
1103 | ||
1104 | return NULL; | |
1105 | } | |
1106 | ||
1baca4ce JL |
1107 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl); |
1108 | ||
1109 | static int find_later_rq(struct task_struct *task) | |
1110 | { | |
1111 | struct sched_domain *sd; | |
1112 | struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl); | |
1113 | int this_cpu = smp_processor_id(); | |
1114 | int best_cpu, cpu = task_cpu(task); | |
1115 | ||
1116 | /* Make sure the mask is initialized first */ | |
1117 | if (unlikely(!later_mask)) | |
1118 | return -1; | |
1119 | ||
1120 | if (task->nr_cpus_allowed == 1) | |
1121 | return -1; | |
1122 | ||
6bfd6d72 JL |
1123 | best_cpu = cpudl_find(&task_rq(task)->rd->cpudl, |
1124 | task, later_mask); | |
1baca4ce JL |
1125 | if (best_cpu == -1) |
1126 | return -1; | |
1127 | ||
1128 | /* | |
1129 | * If we are here, some target has been found, | |
1130 | * the most suitable of which is cached in best_cpu. | |
1131 | * This is, among the runqueues where the current tasks | |
1132 | * have later deadlines than the task's one, the rq | |
1133 | * with the latest possible one. | |
1134 | * | |
1135 | * Now we check how well this matches with task's | |
1136 | * affinity and system topology. | |
1137 | * | |
1138 | * The last cpu where the task run is our first | |
1139 | * guess, since it is most likely cache-hot there. | |
1140 | */ | |
1141 | if (cpumask_test_cpu(cpu, later_mask)) | |
1142 | return cpu; | |
1143 | /* | |
1144 | * Check if this_cpu is to be skipped (i.e., it is | |
1145 | * not in the mask) or not. | |
1146 | */ | |
1147 | if (!cpumask_test_cpu(this_cpu, later_mask)) | |
1148 | this_cpu = -1; | |
1149 | ||
1150 | rcu_read_lock(); | |
1151 | for_each_domain(cpu, sd) { | |
1152 | if (sd->flags & SD_WAKE_AFFINE) { | |
1153 | ||
1154 | /* | |
1155 | * If possible, preempting this_cpu is | |
1156 | * cheaper than migrating. | |
1157 | */ | |
1158 | if (this_cpu != -1 && | |
1159 | cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { | |
1160 | rcu_read_unlock(); | |
1161 | return this_cpu; | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * Last chance: if best_cpu is valid and is | |
1166 | * in the mask, that becomes our choice. | |
1167 | */ | |
1168 | if (best_cpu < nr_cpu_ids && | |
1169 | cpumask_test_cpu(best_cpu, sched_domain_span(sd))) { | |
1170 | rcu_read_unlock(); | |
1171 | return best_cpu; | |
1172 | } | |
1173 | } | |
1174 | } | |
1175 | rcu_read_unlock(); | |
1176 | ||
1177 | /* | |
1178 | * At this point, all our guesses failed, we just return | |
1179 | * 'something', and let the caller sort the things out. | |
1180 | */ | |
1181 | if (this_cpu != -1) | |
1182 | return this_cpu; | |
1183 | ||
1184 | cpu = cpumask_any(later_mask); | |
1185 | if (cpu < nr_cpu_ids) | |
1186 | return cpu; | |
1187 | ||
1188 | return -1; | |
1189 | } | |
1190 | ||
1191 | /* Locks the rq it finds */ | |
1192 | static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq) | |
1193 | { | |
1194 | struct rq *later_rq = NULL; | |
1195 | int tries; | |
1196 | int cpu; | |
1197 | ||
1198 | for (tries = 0; tries < DL_MAX_TRIES; tries++) { | |
1199 | cpu = find_later_rq(task); | |
1200 | ||
1201 | if ((cpu == -1) || (cpu == rq->cpu)) | |
1202 | break; | |
1203 | ||
1204 | later_rq = cpu_rq(cpu); | |
1205 | ||
1206 | /* Retry if something changed. */ | |
1207 | if (double_lock_balance(rq, later_rq)) { | |
1208 | if (unlikely(task_rq(task) != rq || | |
1209 | !cpumask_test_cpu(later_rq->cpu, | |
1210 | &task->cpus_allowed) || | |
1211 | task_running(rq, task) || !task->on_rq)) { | |
1212 | double_unlock_balance(rq, later_rq); | |
1213 | later_rq = NULL; | |
1214 | break; | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | /* | |
1219 | * If the rq we found has no -deadline task, or | |
1220 | * its earliest one has a later deadline than our | |
1221 | * task, the rq is a good one. | |
1222 | */ | |
1223 | if (!later_rq->dl.dl_nr_running || | |
1224 | dl_time_before(task->dl.deadline, | |
1225 | later_rq->dl.earliest_dl.curr)) | |
1226 | break; | |
1227 | ||
1228 | /* Otherwise we try again. */ | |
1229 | double_unlock_balance(rq, later_rq); | |
1230 | later_rq = NULL; | |
1231 | } | |
1232 | ||
1233 | return later_rq; | |
1234 | } | |
1235 | ||
1236 | static struct task_struct *pick_next_pushable_dl_task(struct rq *rq) | |
1237 | { | |
1238 | struct task_struct *p; | |
1239 | ||
1240 | if (!has_pushable_dl_tasks(rq)) | |
1241 | return NULL; | |
1242 | ||
1243 | p = rb_entry(rq->dl.pushable_dl_tasks_leftmost, | |
1244 | struct task_struct, pushable_dl_tasks); | |
1245 | ||
1246 | BUG_ON(rq->cpu != task_cpu(p)); | |
1247 | BUG_ON(task_current(rq, p)); | |
1248 | BUG_ON(p->nr_cpus_allowed <= 1); | |
1249 | ||
332ac17e | 1250 | BUG_ON(!p->on_rq); |
1baca4ce JL |
1251 | BUG_ON(!dl_task(p)); |
1252 | ||
1253 | return p; | |
1254 | } | |
1255 | ||
1256 | /* | |
1257 | * See if the non running -deadline tasks on this rq | |
1258 | * can be sent to some other CPU where they can preempt | |
1259 | * and start executing. | |
1260 | */ | |
1261 | static int push_dl_task(struct rq *rq) | |
1262 | { | |
1263 | struct task_struct *next_task; | |
1264 | struct rq *later_rq; | |
1265 | ||
1266 | if (!rq->dl.overloaded) | |
1267 | return 0; | |
1268 | ||
1269 | next_task = pick_next_pushable_dl_task(rq); | |
1270 | if (!next_task) | |
1271 | return 0; | |
1272 | ||
1273 | retry: | |
1274 | if (unlikely(next_task == rq->curr)) { | |
1275 | WARN_ON(1); | |
1276 | return 0; | |
1277 | } | |
1278 | ||
1279 | /* | |
1280 | * If next_task preempts rq->curr, and rq->curr | |
1281 | * can move away, it makes sense to just reschedule | |
1282 | * without going further in pushing next_task. | |
1283 | */ | |
1284 | if (dl_task(rq->curr) && | |
1285 | dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) && | |
1286 | rq->curr->nr_cpus_allowed > 1) { | |
1287 | resched_task(rq->curr); | |
1288 | return 0; | |
1289 | } | |
1290 | ||
1291 | /* We might release rq lock */ | |
1292 | get_task_struct(next_task); | |
1293 | ||
1294 | /* Will lock the rq it'll find */ | |
1295 | later_rq = find_lock_later_rq(next_task, rq); | |
1296 | if (!later_rq) { | |
1297 | struct task_struct *task; | |
1298 | ||
1299 | /* | |
1300 | * We must check all this again, since | |
1301 | * find_lock_later_rq releases rq->lock and it is | |
1302 | * then possible that next_task has migrated. | |
1303 | */ | |
1304 | task = pick_next_pushable_dl_task(rq); | |
1305 | if (task_cpu(next_task) == rq->cpu && task == next_task) { | |
1306 | /* | |
1307 | * The task is still there. We don't try | |
1308 | * again, some other cpu will pull it when ready. | |
1309 | */ | |
1310 | dequeue_pushable_dl_task(rq, next_task); | |
1311 | goto out; | |
1312 | } | |
1313 | ||
1314 | if (!task) | |
1315 | /* No more tasks */ | |
1316 | goto out; | |
1317 | ||
1318 | put_task_struct(next_task); | |
1319 | next_task = task; | |
1320 | goto retry; | |
1321 | } | |
1322 | ||
1323 | deactivate_task(rq, next_task, 0); | |
1324 | set_task_cpu(next_task, later_rq->cpu); | |
1325 | activate_task(later_rq, next_task, 0); | |
1326 | ||
1327 | resched_task(later_rq->curr); | |
1328 | ||
1329 | double_unlock_balance(rq, later_rq); | |
1330 | ||
1331 | out: | |
1332 | put_task_struct(next_task); | |
1333 | ||
1334 | return 1; | |
1335 | } | |
1336 | ||
1337 | static void push_dl_tasks(struct rq *rq) | |
1338 | { | |
1339 | /* Terminates as it moves a -deadline task */ | |
1340 | while (push_dl_task(rq)) | |
1341 | ; | |
aab03e05 DF |
1342 | } |
1343 | ||
1baca4ce JL |
1344 | static int pull_dl_task(struct rq *this_rq) |
1345 | { | |
1346 | int this_cpu = this_rq->cpu, ret = 0, cpu; | |
1347 | struct task_struct *p; | |
1348 | struct rq *src_rq; | |
1349 | u64 dmin = LONG_MAX; | |
1350 | ||
1351 | if (likely(!dl_overloaded(this_rq))) | |
1352 | return 0; | |
1353 | ||
1354 | /* | |
1355 | * Match the barrier from dl_set_overloaded; this guarantees that if we | |
1356 | * see overloaded we must also see the dlo_mask bit. | |
1357 | */ | |
1358 | smp_rmb(); | |
1359 | ||
1360 | for_each_cpu(cpu, this_rq->rd->dlo_mask) { | |
1361 | if (this_cpu == cpu) | |
1362 | continue; | |
1363 | ||
1364 | src_rq = cpu_rq(cpu); | |
1365 | ||
1366 | /* | |
1367 | * It looks racy, abd it is! However, as in sched_rt.c, | |
1368 | * we are fine with this. | |
1369 | */ | |
1370 | if (this_rq->dl.dl_nr_running && | |
1371 | dl_time_before(this_rq->dl.earliest_dl.curr, | |
1372 | src_rq->dl.earliest_dl.next)) | |
1373 | continue; | |
1374 | ||
1375 | /* Might drop this_rq->lock */ | |
1376 | double_lock_balance(this_rq, src_rq); | |
1377 | ||
1378 | /* | |
1379 | * If there are no more pullable tasks on the | |
1380 | * rq, we're done with it. | |
1381 | */ | |
1382 | if (src_rq->dl.dl_nr_running <= 1) | |
1383 | goto skip; | |
1384 | ||
1385 | p = pick_next_earliest_dl_task(src_rq, this_cpu); | |
1386 | ||
1387 | /* | |
1388 | * We found a task to be pulled if: | |
1389 | * - it preempts our current (if there's one), | |
1390 | * - it will preempt the last one we pulled (if any). | |
1391 | */ | |
1392 | if (p && dl_time_before(p->dl.deadline, dmin) && | |
1393 | (!this_rq->dl.dl_nr_running || | |
1394 | dl_time_before(p->dl.deadline, | |
1395 | this_rq->dl.earliest_dl.curr))) { | |
1396 | WARN_ON(p == src_rq->curr); | |
332ac17e | 1397 | WARN_ON(!p->on_rq); |
1baca4ce JL |
1398 | |
1399 | /* | |
1400 | * Then we pull iff p has actually an earlier | |
1401 | * deadline than the current task of its runqueue. | |
1402 | */ | |
1403 | if (dl_time_before(p->dl.deadline, | |
1404 | src_rq->curr->dl.deadline)) | |
1405 | goto skip; | |
1406 | ||
1407 | ret = 1; | |
1408 | ||
1409 | deactivate_task(src_rq, p, 0); | |
1410 | set_task_cpu(p, this_cpu); | |
1411 | activate_task(this_rq, p, 0); | |
1412 | dmin = p->dl.deadline; | |
1413 | ||
1414 | /* Is there any other task even earlier? */ | |
1415 | } | |
1416 | skip: | |
1417 | double_unlock_balance(this_rq, src_rq); | |
1418 | } | |
1419 | ||
1420 | return ret; | |
1421 | } | |
1422 | ||
1423 | static void pre_schedule_dl(struct rq *rq, struct task_struct *prev) | |
1424 | { | |
1425 | /* Try to pull other tasks here */ | |
1426 | if (dl_task(prev)) | |
1427 | pull_dl_task(rq); | |
1428 | } | |
1429 | ||
1430 | static void post_schedule_dl(struct rq *rq) | |
1431 | { | |
1432 | push_dl_tasks(rq); | |
1433 | } | |
1434 | ||
1435 | /* | |
1436 | * Since the task is not running and a reschedule is not going to happen | |
1437 | * anytime soon on its runqueue, we try pushing it away now. | |
1438 | */ | |
1439 | static void task_woken_dl(struct rq *rq, struct task_struct *p) | |
1440 | { | |
1441 | if (!task_running(rq, p) && | |
1442 | !test_tsk_need_resched(rq->curr) && | |
1443 | has_pushable_dl_tasks(rq) && | |
1444 | p->nr_cpus_allowed > 1 && | |
1445 | dl_task(rq->curr) && | |
1446 | (rq->curr->nr_cpus_allowed < 2 || | |
1447 | dl_entity_preempt(&rq->curr->dl, &p->dl))) { | |
1448 | push_dl_tasks(rq); | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | static void set_cpus_allowed_dl(struct task_struct *p, | |
1453 | const struct cpumask *new_mask) | |
1454 | { | |
1455 | struct rq *rq; | |
1456 | int weight; | |
1457 | ||
1458 | BUG_ON(!dl_task(p)); | |
1459 | ||
1460 | /* | |
1461 | * Update only if the task is actually running (i.e., | |
1462 | * it is on the rq AND it is not throttled). | |
1463 | */ | |
1464 | if (!on_dl_rq(&p->dl)) | |
1465 | return; | |
1466 | ||
1467 | weight = cpumask_weight(new_mask); | |
1468 | ||
1469 | /* | |
1470 | * Only update if the process changes its state from whether it | |
1471 | * can migrate or not. | |
1472 | */ | |
1473 | if ((p->nr_cpus_allowed > 1) == (weight > 1)) | |
1474 | return; | |
1475 | ||
1476 | rq = task_rq(p); | |
1477 | ||
1478 | /* | |
1479 | * The process used to be able to migrate OR it can now migrate | |
1480 | */ | |
1481 | if (weight <= 1) { | |
1482 | if (!task_current(rq, p)) | |
1483 | dequeue_pushable_dl_task(rq, p); | |
1484 | BUG_ON(!rq->dl.dl_nr_migratory); | |
1485 | rq->dl.dl_nr_migratory--; | |
1486 | } else { | |
1487 | if (!task_current(rq, p)) | |
1488 | enqueue_pushable_dl_task(rq, p); | |
1489 | rq->dl.dl_nr_migratory++; | |
1490 | } | |
1491 | ||
1492 | update_dl_migration(&rq->dl); | |
1493 | } | |
1494 | ||
1495 | /* Assumes rq->lock is held */ | |
1496 | static void rq_online_dl(struct rq *rq) | |
1497 | { | |
1498 | if (rq->dl.overloaded) | |
1499 | dl_set_overload(rq); | |
6bfd6d72 JL |
1500 | |
1501 | if (rq->dl.dl_nr_running > 0) | |
1502 | cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr, 1); | |
1baca4ce JL |
1503 | } |
1504 | ||
1505 | /* Assumes rq->lock is held */ | |
1506 | static void rq_offline_dl(struct rq *rq) | |
1507 | { | |
1508 | if (rq->dl.overloaded) | |
1509 | dl_clear_overload(rq); | |
6bfd6d72 JL |
1510 | |
1511 | cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0); | |
1baca4ce JL |
1512 | } |
1513 | ||
1514 | void init_sched_dl_class(void) | |
1515 | { | |
1516 | unsigned int i; | |
1517 | ||
1518 | for_each_possible_cpu(i) | |
1519 | zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i), | |
1520 | GFP_KERNEL, cpu_to_node(i)); | |
1521 | } | |
1522 | ||
1523 | #endif /* CONFIG_SMP */ | |
1524 | ||
aab03e05 DF |
1525 | static void switched_from_dl(struct rq *rq, struct task_struct *p) |
1526 | { | |
1baca4ce | 1527 | if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy)) |
aab03e05 | 1528 | hrtimer_try_to_cancel(&p->dl.dl_timer); |
1baca4ce JL |
1529 | |
1530 | #ifdef CONFIG_SMP | |
1531 | /* | |
1532 | * Since this might be the only -deadline task on the rq, | |
1533 | * this is the right place to try to pull some other one | |
1534 | * from an overloaded cpu, if any. | |
1535 | */ | |
1536 | if (!rq->dl.dl_nr_running) | |
1537 | pull_dl_task(rq); | |
1538 | #endif | |
aab03e05 DF |
1539 | } |
1540 | ||
1baca4ce JL |
1541 | /* |
1542 | * When switching to -deadline, we may overload the rq, then | |
1543 | * we try to push someone off, if possible. | |
1544 | */ | |
aab03e05 DF |
1545 | static void switched_to_dl(struct rq *rq, struct task_struct *p) |
1546 | { | |
1baca4ce JL |
1547 | int check_resched = 1; |
1548 | ||
aab03e05 DF |
1549 | /* |
1550 | * If p is throttled, don't consider the possibility | |
1551 | * of preempting rq->curr, the check will be done right | |
1552 | * after its runtime will get replenished. | |
1553 | */ | |
1554 | if (unlikely(p->dl.dl_throttled)) | |
1555 | return; | |
1556 | ||
1557 | if (p->on_rq || rq->curr != p) { | |
1baca4ce JL |
1558 | #ifdef CONFIG_SMP |
1559 | if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p)) | |
1560 | /* Only reschedule if pushing failed */ | |
1561 | check_resched = 0; | |
1562 | #endif /* CONFIG_SMP */ | |
1563 | if (check_resched && task_has_dl_policy(rq->curr)) | |
aab03e05 | 1564 | check_preempt_curr_dl(rq, p, 0); |
aab03e05 DF |
1565 | } |
1566 | } | |
1567 | ||
1baca4ce JL |
1568 | /* |
1569 | * If the scheduling parameters of a -deadline task changed, | |
1570 | * a push or pull operation might be needed. | |
1571 | */ | |
aab03e05 DF |
1572 | static void prio_changed_dl(struct rq *rq, struct task_struct *p, |
1573 | int oldprio) | |
1574 | { | |
1baca4ce | 1575 | if (p->on_rq || rq->curr == p) { |
aab03e05 | 1576 | #ifdef CONFIG_SMP |
1baca4ce JL |
1577 | /* |
1578 | * This might be too much, but unfortunately | |
1579 | * we don't have the old deadline value, and | |
1580 | * we can't argue if the task is increasing | |
1581 | * or lowering its prio, so... | |
1582 | */ | |
1583 | if (!rq->dl.overloaded) | |
1584 | pull_dl_task(rq); | |
1585 | ||
1586 | /* | |
1587 | * If we now have a earlier deadline task than p, | |
1588 | * then reschedule, provided p is still on this | |
1589 | * runqueue. | |
1590 | */ | |
1591 | if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) && | |
1592 | rq->curr == p) | |
1593 | resched_task(p); | |
1594 | #else | |
1595 | /* | |
1596 | * Again, we don't know if p has a earlier | |
1597 | * or later deadline, so let's blindly set a | |
1598 | * (maybe not needed) rescheduling point. | |
1599 | */ | |
1600 | resched_task(p); | |
1601 | #endif /* CONFIG_SMP */ | |
1602 | } else | |
1603 | switched_to_dl(rq, p); | |
aab03e05 | 1604 | } |
aab03e05 DF |
1605 | |
1606 | const struct sched_class dl_sched_class = { | |
1607 | .next = &rt_sched_class, | |
1608 | .enqueue_task = enqueue_task_dl, | |
1609 | .dequeue_task = dequeue_task_dl, | |
1610 | .yield_task = yield_task_dl, | |
1611 | ||
1612 | .check_preempt_curr = check_preempt_curr_dl, | |
1613 | ||
1614 | .pick_next_task = pick_next_task_dl, | |
1615 | .put_prev_task = put_prev_task_dl, | |
1616 | ||
1617 | #ifdef CONFIG_SMP | |
1618 | .select_task_rq = select_task_rq_dl, | |
1baca4ce JL |
1619 | .set_cpus_allowed = set_cpus_allowed_dl, |
1620 | .rq_online = rq_online_dl, | |
1621 | .rq_offline = rq_offline_dl, | |
1622 | .pre_schedule = pre_schedule_dl, | |
1623 | .post_schedule = post_schedule_dl, | |
1624 | .task_woken = task_woken_dl, | |
aab03e05 DF |
1625 | #endif |
1626 | ||
1627 | .set_curr_task = set_curr_task_dl, | |
1628 | .task_tick = task_tick_dl, | |
1629 | .task_fork = task_fork_dl, | |
1630 | .task_dead = task_dead_dl, | |
1631 | ||
1632 | .prio_changed = prio_changed_dl, | |
1633 | .switched_from = switched_from_dl, | |
1634 | .switched_to = switched_to_dl, | |
1635 | }; |