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