2 * Deadline Scheduling Class (SCHED_DEADLINE)
4 * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
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.
12 * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>,
13 * Juri Lelli <juri.lelli@gmail.com>,
14 * Michael Trimarchi <michael@amarulasolutions.com>,
15 * Fabio Checconi <fchecconi@gmail.com>
19 struct dl_bandwidth def_dl_bandwidth;
21 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
23 return container_of(dl_se, struct task_struct, dl);
26 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
28 return container_of(dl_rq, struct rq, dl);
31 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
33 struct task_struct *p = dl_task_of(dl_se);
34 struct rq *rq = task_rq(p);
39 static inline int on_dl_rq(struct sched_dl_entity *dl_se)
41 return !RB_EMPTY_NODE(&dl_se->rb_node);
44 static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
46 struct sched_dl_entity *dl_se = &p->dl;
48 return dl_rq->rb_leftmost == &dl_se->rb_node;
51 void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
53 raw_spin_lock_init(&dl_b->dl_runtime_lock);
54 dl_b->dl_period = period;
55 dl_b->dl_runtime = runtime;
58 extern unsigned long to_ratio(u64 period, u64 runtime);
60 void init_dl_bw(struct dl_bw *dl_b)
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)
67 dl_b->bw = to_ratio(global_dl_period(), global_dl_runtime());
68 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
72 void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq)
74 dl_rq->rb_root = RB_ROOT;
77 /* zero means no -deadline tasks */
78 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
80 dl_rq->dl_nr_migratory = 0;
81 dl_rq->overloaded = 0;
82 dl_rq->pushable_dl_tasks_root = RB_ROOT;
84 init_dl_bw(&dl_rq->dl_bw);
90 static inline int dl_overloaded(struct rq *rq)
92 return atomic_read(&rq->rd->dlo_count);
95 static inline void dl_set_overload(struct rq *rq)
100 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
102 * Must be visible before the overload count is
103 * set (as in sched_rt.c).
105 * Matched by the barrier in pull_dl_task().
108 atomic_inc(&rq->rd->dlo_count);
111 static inline void dl_clear_overload(struct rq *rq)
116 atomic_dec(&rq->rd->dlo_count);
117 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
120 static void update_dl_migration(struct dl_rq *dl_rq)
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;
127 } else if (dl_rq->overloaded) {
128 dl_clear_overload(rq_of_dl_rq(dl_rq));
129 dl_rq->overloaded = 0;
133 static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
135 struct task_struct *p = dl_task_of(dl_se);
136 dl_rq = &rq_of_dl_rq(dl_rq)->dl;
138 dl_rq->dl_nr_total++;
139 if (p->nr_cpus_allowed > 1)
140 dl_rq->dl_nr_migratory++;
142 update_dl_migration(dl_rq);
145 static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
147 struct task_struct *p = dl_task_of(dl_se);
148 dl_rq = &rq_of_dl_rq(dl_rq)->dl;
150 dl_rq->dl_nr_total--;
151 if (p->nr_cpus_allowed > 1)
152 dl_rq->dl_nr_migratory--;
154 update_dl_migration(dl_rq);
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.
161 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
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;
169 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
173 entry = rb_entry(parent, struct task_struct,
175 if (dl_entity_preempt(&p->dl, &entry->dl))
176 link = &parent->rb_left;
178 link = &parent->rb_right;
184 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
186 rb_link_node(&p->pushable_dl_tasks, parent, link);
187 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
190 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
192 struct dl_rq *dl_rq = &rq->dl;
194 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
197 if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
198 struct rb_node *next_node;
200 next_node = rb_next(&p->pushable_dl_tasks);
201 dl_rq->pushable_dl_tasks_leftmost = next_node;
204 rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
205 RB_CLEAR_NODE(&p->pushable_dl_tasks);
208 static inline int has_pushable_dl_tasks(struct rq *rq)
210 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
213 static int push_dl_task(struct rq *rq);
218 void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
223 void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
228 void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
233 void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
237 #endif /* CONFIG_SMP */
239 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
240 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
241 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
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.
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
256 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se,
257 struct sched_dl_entity *pi_se)
259 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
260 struct rq *rq = rq_of_dl_rq(dl_rq);
262 WARN_ON(!dl_se->dl_new || dl_se->dl_throttled);
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.).
269 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
270 dl_se->runtime = pi_se->dl_runtime;
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.
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
282 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
283 * is used, in order to confine each entity within its own bandwidth.
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().
292 static void replenish_dl_entity(struct sched_dl_entity *dl_se,
293 struct sched_dl_entity *pi_se)
295 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
296 struct rq *rq = rq_of_dl_rq(dl_rq);
298 BUG_ON(pi_se->dl_runtime <= 0);
301 * This could be the case for a !-dl task that is boosted.
302 * Just go with full inherited parameters.
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;
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
315 while (dl_se->runtime <= 0) {
316 dl_se->deadline += pi_se->dl_period;
317 dl_se->runtime += pi_se->dl_runtime;
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
329 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
330 static bool lag_once = false;
334 printk_sched("sched: DL replenish lagged to much\n");
336 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
337 dl_se->runtime = pi_se->dl_runtime;
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.
354 * This function returns true if:
356 * runtime / (deadline - t) > dl_runtime / dl_period ,
358 * IOW we can't recycle current parameters.
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.
364 static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
365 struct sched_dl_entity *pi_se, u64 t)
370 * left and right are the two sides of the equation above,
371 * after a bit of shuffling to use multiplications instead
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 >;).
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);
391 return dl_time_before(right, left);
395 * When a -deadline entity is queued back on the runqueue, its runtime and
396 * deadline might need updating.
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.
403 static void update_dl_entity(struct sched_dl_entity *dl_se,
404 struct sched_dl_entity *pi_se)
406 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
407 struct rq *rq = rq_of_dl_rq(dl_rq);
410 * The arrival of a new instance needs special treatment, i.e.,
411 * the actual scheduling parameters have to be "renewed".
414 setup_new_dl_entity(dl_se, pi_se);
418 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
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;
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.
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).
435 static int start_dl_timer(struct sched_dl_entity *dl_se, bool boosted)
437 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
438 struct rq *rq = rq_of_dl_rq(dl_rq);
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.
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);
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!
461 if (ktime_us_delta(act, now) < 0)
464 hrtimer_set_expires(&dl_se->dl_timer, act);
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);
472 return hrtimer_active(&dl_se->dl_timer);
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.
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().
488 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
490 struct sched_dl_entity *dl_se = container_of(timer,
491 struct sched_dl_entity,
493 struct task_struct *p = dl_task_of(dl_se);
494 struct rq *rq = task_rq(p);
495 raw_spin_lock(&rq->lock);
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()).
503 if (!dl_task(p) || dl_se->dl_new)
508 dl_se->dl_throttled = 0;
510 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
511 if (task_has_dl_policy(rq->curr))
512 check_preempt_curr_dl(rq, p, 0);
514 resched_task(rq->curr);
517 * Queueing this task back might have overloaded rq,
518 * check if we need to kick someone away.
520 if (has_pushable_dl_tasks(rq))
525 raw_spin_unlock(&rq->lock);
527 return HRTIMER_NORESTART;
530 void init_dl_task_timer(struct sched_dl_entity *dl_se)
532 struct hrtimer *timer = &dl_se->dl_timer;
534 if (hrtimer_active(timer)) {
535 hrtimer_try_to_cancel(timer);
539 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
540 timer->function = dl_task_timer;
544 int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
546 int dmiss = dl_time_before(dl_se->deadline, rq_clock(rq));
547 int rorun = dl_se->runtime <= 0;
549 if (!rorun && !dmiss)
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!
559 dl_se->runtime = rorun ? dl_se->runtime : 0;
560 dl_se->runtime -= rq_clock(rq) - dl_se->deadline;
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).
570 static void update_curr_dl(struct rq *rq)
572 struct task_struct *curr = rq->curr;
573 struct sched_dl_entity *dl_se = &curr->dl;
576 if (!dl_task(curr) || !on_dl_rq(dl_se))
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.
587 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
588 if (unlikely((s64)delta_exec < 0))
591 schedstat_set(curr->se.statistics.exec_max,
592 max(curr->se.statistics.exec_max, delta_exec));
594 curr->se.sum_exec_runtime += delta_exec;
595 account_group_exec_runtime(curr, delta_exec);
597 curr->se.exec_start = rq_clock_task(rq);
598 cpuacct_charge(curr, delta_exec);
600 sched_rt_avg_update(rq, delta_exec);
602 dl_se->runtime -= delta_exec;
603 if (dl_runtime_exceeded(rq, dl_se)) {
604 __dequeue_task_dl(rq, curr, 0);
605 if (likely(start_dl_timer(dl_se, curr->dl.dl_boosted)))
606 dl_se->dl_throttled = 1;
608 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
610 if (!is_leftmost(curr, &rq->dl))
617 static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
619 static inline u64 next_deadline(struct rq *rq)
621 struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
623 if (next && dl_prio(next->prio))
624 return next->dl.deadline;
629 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
631 struct rq *rq = rq_of_dl_rq(dl_rq);
633 if (dl_rq->earliest_dl.curr == 0 ||
634 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
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.
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)) {
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.
651 dl_rq->earliest_dl.next = next_deadline(rq);
655 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
657 struct rq *rq = rq_of_dl_rq(dl_rq);
660 * Since we may have removed our earliest (and/or next earliest)
661 * task we must recompute them.
663 if (!dl_rq->dl_nr_running) {
664 dl_rq->earliest_dl.curr = 0;
665 dl_rq->earliest_dl.next = 0;
667 struct rb_node *leftmost = dl_rq->rb_leftmost;
668 struct sched_dl_entity *entry;
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);
678 static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
679 static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
681 #endif /* CONFIG_SMP */
684 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
686 int prio = dl_task_of(dl_se)->prio;
687 u64 deadline = dl_se->deadline;
689 WARN_ON(!dl_prio(prio));
690 dl_rq->dl_nr_running++;
692 inc_dl_deadline(dl_rq, deadline);
693 inc_dl_migration(dl_se, dl_rq);
697 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
699 int prio = dl_task_of(dl_se)->prio;
701 WARN_ON(!dl_prio(prio));
702 WARN_ON(!dl_rq->dl_nr_running);
703 dl_rq->dl_nr_running--;
705 dec_dl_deadline(dl_rq, dl_se->deadline);
706 dec_dl_migration(dl_se, dl_rq);
709 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
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;
717 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
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;
725 link = &parent->rb_right;
731 dl_rq->rb_leftmost = &dl_se->rb_node;
733 rb_link_node(&dl_se->rb_node, parent, link);
734 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
736 inc_dl_tasks(dl_se, dl_rq);
739 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
741 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
743 if (RB_EMPTY_NODE(&dl_se->rb_node))
746 if (dl_rq->rb_leftmost == &dl_se->rb_node) {
747 struct rb_node *next_node;
749 next_node = rb_next(&dl_se->rb_node);
750 dl_rq->rb_leftmost = next_node;
753 rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
754 RB_CLEAR_NODE(&dl_se->rb_node);
756 dec_dl_tasks(dl_se, dl_rq);
760 enqueue_dl_entity(struct sched_dl_entity *dl_se,
761 struct sched_dl_entity *pi_se, int flags)
763 BUG_ON(on_dl_rq(dl_se));
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.
770 if (!dl_se->dl_new && flags & ENQUEUE_REPLENISH)
771 replenish_dl_entity(dl_se, pi_se);
773 update_dl_entity(dl_se, pi_se);
775 __enqueue_dl_entity(dl_se);
778 static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
780 __dequeue_dl_entity(dl_se);
783 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
785 struct task_struct *pi_task = rt_mutex_get_top_task(p);
786 struct sched_dl_entity *pi_se = &p->dl;
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
794 if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio))
795 pi_se = &pi_task->dl;
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.
803 if (p->dl.dl_throttled)
806 enqueue_dl_entity(&p->dl, pi_se, flags);
808 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
809 enqueue_pushable_dl_task(rq, p);
814 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
816 dequeue_dl_entity(&p->dl);
817 dequeue_pushable_dl_task(rq, p);
820 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
823 __dequeue_task_dl(rq, p, flags);
829 * Yield task semantic for -deadline tasks is:
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.
838 static void yield_task_dl(struct rq *rq)
840 struct task_struct *p = rq->curr;
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).
848 if (p->dl.runtime > 0) {
849 rq->curr->dl.dl_new = 1;
857 static int find_later_rq(struct task_struct *task);
858 static int latest_cpu_find(struct cpumask *span,
859 struct task_struct *task,
860 struct cpumask *later_mask);
863 select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
865 struct task_struct *curr;
868 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
874 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
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.
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);
900 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
903 * Current can't be migrated, useless to reschedule,
904 * let's hope p can move out.
906 if (rq->curr->nr_cpus_allowed == 1 ||
907 latest_cpu_find(rq->rd->span, rq->curr, NULL) == -1)
911 * p is migratable, so let's not schedule it and
912 * see if it is pushed or pulled somewhere else.
914 if (p->nr_cpus_allowed != 1 &&
915 latest_cpu_find(rq->rd->span, p, NULL) != -1)
918 resched_task(rq->curr);
921 #endif /* CONFIG_SMP */
924 * Only called when both the current and waking task are -deadline
927 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
930 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
931 resched_task(rq->curr);
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...
940 if ((p->dl.deadline == rq->curr->dl.deadline) &&
941 !test_tsk_need_resched(rq->curr))
942 check_preempt_equal_dl(rq, p);
943 #endif /* CONFIG_SMP */
946 #ifdef CONFIG_SCHED_HRTICK
947 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
949 s64 delta = p->dl.dl_runtime - p->dl.runtime;
952 hrtick_start(rq, p->dl.runtime);
956 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
959 struct rb_node *left = dl_rq->rb_leftmost;
964 return rb_entry(left, struct sched_dl_entity, rb_node);
967 struct task_struct *pick_next_task_dl(struct rq *rq)
969 struct sched_dl_entity *dl_se;
970 struct task_struct *p;
975 if (unlikely(!dl_rq->dl_nr_running))
978 dl_se = pick_next_dl_entity(rq, dl_rq);
981 p = dl_task_of(dl_se);
982 p->se.exec_start = rq_clock_task(rq);
984 /* Running task will never be pushed. */
986 dequeue_pushable_dl_task(rq, p);
988 #ifdef CONFIG_SCHED_HRTICK
989 if (hrtick_enabled(rq))
990 start_hrtick_dl(rq, p);
994 rq->post_schedule = has_pushable_dl_tasks(rq);
995 #endif /* CONFIG_SMP */
1000 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1004 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
1005 enqueue_pushable_dl_task(rq, p);
1008 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1012 #ifdef CONFIG_SCHED_HRTICK
1013 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0)
1014 start_hrtick_dl(rq, p);
1018 static void task_fork_dl(struct task_struct *p)
1021 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1026 static void task_dead_dl(struct task_struct *p)
1028 struct hrtimer *timer = &p->dl.dl_timer;
1029 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1032 * Since we are TASK_DEAD we won't slip out of the domain!
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);
1038 hrtimer_cancel(timer);
1041 static void set_curr_task_dl(struct rq *rq)
1043 struct task_struct *p = rq->curr;
1045 p->se.exec_start = rq_clock_task(rq);
1047 /* You can't push away the running task */
1048 dequeue_pushable_dl_task(rq, p);
1053 /* Only try algorithms three times */
1054 #define DL_MAX_TRIES 3
1056 static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1058 if (!task_running(rq, p) &&
1059 (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
1060 (p->nr_cpus_allowed > 1))
1066 /* Returns the second earliest -deadline task, NULL otherwise */
1067 static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
1069 struct rb_node *next_node = rq->dl.rb_leftmost;
1070 struct sched_dl_entity *dl_se;
1071 struct task_struct *p = NULL;
1074 next_node = rb_next(next_node);
1076 dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
1077 p = dl_task_of(dl_se);
1079 if (pick_dl_task(rq, p, cpu))
1088 static int latest_cpu_find(struct cpumask *span,
1089 struct task_struct *task,
1090 struct cpumask *later_mask)
1092 const struct sched_dl_entity *dl_se = &task->dl;
1093 int cpu, found = -1, best = 0;
1096 for_each_cpu(cpu, span) {
1097 struct rq *rq = cpu_rq(cpu);
1098 struct dl_rq *dl_rq = &rq->dl;
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))) {
1104 cpumask_set_cpu(cpu, later_mask);
1105 if (!best && !dl_rq->dl_nr_running) {
1109 dl_time_before(max_dl,
1110 dl_rq->earliest_dl.curr)) {
1111 max_dl = dl_rq->earliest_dl.curr;
1114 } else if (later_mask)
1115 cpumask_clear_cpu(cpu, later_mask);
1121 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1123 static int find_later_rq(struct task_struct *task)
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);
1130 /* Make sure the mask is initialized first */
1131 if (unlikely(!later_mask))
1134 if (task->nr_cpus_allowed == 1)
1137 best_cpu = latest_cpu_find(task_rq(task)->rd->span, task, later_mask);
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.
1148 * Now we check how well this matches with task's
1149 * affinity and system topology.
1151 * The last cpu where the task run is our first
1152 * guess, since it is most likely cache-hot there.
1154 if (cpumask_test_cpu(cpu, later_mask))
1157 * Check if this_cpu is to be skipped (i.e., it is
1158 * not in the mask) or not.
1160 if (!cpumask_test_cpu(this_cpu, later_mask))
1164 for_each_domain(cpu, sd) {
1165 if (sd->flags & SD_WAKE_AFFINE) {
1168 * If possible, preempting this_cpu is
1169 * cheaper than migrating.
1171 if (this_cpu != -1 &&
1172 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1178 * Last chance: if best_cpu is valid and is
1179 * in the mask, that becomes our choice.
1181 if (best_cpu < nr_cpu_ids &&
1182 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
1191 * At this point, all our guesses failed, we just return
1192 * 'something', and let the caller sort the things out.
1197 cpu = cpumask_any(later_mask);
1198 if (cpu < nr_cpu_ids)
1204 /* Locks the rq it finds */
1205 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1207 struct rq *later_rq = NULL;
1211 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1212 cpu = find_later_rq(task);
1214 if ((cpu == -1) || (cpu == rq->cpu))
1217 later_rq = cpu_rq(cpu);
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);
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.
1236 if (!later_rq->dl.dl_nr_running ||
1237 dl_time_before(task->dl.deadline,
1238 later_rq->dl.earliest_dl.curr))
1241 /* Otherwise we try again. */
1242 double_unlock_balance(rq, later_rq);
1249 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1251 struct task_struct *p;
1253 if (!has_pushable_dl_tasks(rq))
1256 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
1257 struct task_struct, pushable_dl_tasks);
1259 BUG_ON(rq->cpu != task_cpu(p));
1260 BUG_ON(task_current(rq, p));
1261 BUG_ON(p->nr_cpus_allowed <= 1);
1264 BUG_ON(!dl_task(p));
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.
1274 static int push_dl_task(struct rq *rq)
1276 struct task_struct *next_task;
1277 struct rq *later_rq;
1279 if (!rq->dl.overloaded)
1282 next_task = pick_next_pushable_dl_task(rq);
1287 if (unlikely(next_task == rq->curr)) {
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.
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);
1304 /* We might release rq lock */
1305 get_task_struct(next_task);
1307 /* Will lock the rq it'll find */
1308 later_rq = find_lock_later_rq(next_task, rq);
1310 struct task_struct *task;
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.
1317 task = pick_next_pushable_dl_task(rq);
1318 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1320 * The task is still there. We don't try
1321 * again, some other cpu will pull it when ready.
1323 dequeue_pushable_dl_task(rq, next_task);
1331 put_task_struct(next_task);
1336 deactivate_task(rq, next_task, 0);
1337 set_task_cpu(next_task, later_rq->cpu);
1338 activate_task(later_rq, next_task, 0);
1340 resched_task(later_rq->curr);
1342 double_unlock_balance(rq, later_rq);
1345 put_task_struct(next_task);
1350 static void push_dl_tasks(struct rq *rq)
1352 /* Terminates as it moves a -deadline task */
1353 while (push_dl_task(rq))
1357 static int pull_dl_task(struct rq *this_rq)
1359 int this_cpu = this_rq->cpu, ret = 0, cpu;
1360 struct task_struct *p;
1362 u64 dmin = LONG_MAX;
1364 if (likely(!dl_overloaded(this_rq)))
1368 * Match the barrier from dl_set_overloaded; this guarantees that if we
1369 * see overloaded we must also see the dlo_mask bit.
1373 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
1374 if (this_cpu == cpu)
1377 src_rq = cpu_rq(cpu);
1380 * It looks racy, abd it is! However, as in sched_rt.c,
1381 * we are fine with this.
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))
1388 /* Might drop this_rq->lock */
1389 double_lock_balance(this_rq, src_rq);
1392 * If there are no more pullable tasks on the
1393 * rq, we're done with it.
1395 if (src_rq->dl.dl_nr_running <= 1)
1398 p = pick_next_earliest_dl_task(src_rq, this_cpu);
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).
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);
1413 * Then we pull iff p has actually an earlier
1414 * deadline than the current task of its runqueue.
1416 if (dl_time_before(p->dl.deadline,
1417 src_rq->curr->dl.deadline))
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;
1427 /* Is there any other task even earlier? */
1430 double_unlock_balance(this_rq, src_rq);
1436 static void pre_schedule_dl(struct rq *rq, struct task_struct *prev)
1438 /* Try to pull other tasks here */
1443 static void post_schedule_dl(struct rq *rq)
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.
1452 static void task_woken_dl(struct rq *rq, struct task_struct *p)
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))) {
1465 static void set_cpus_allowed_dl(struct task_struct *p,
1466 const struct cpumask *new_mask)
1471 BUG_ON(!dl_task(p));
1474 * Update only if the task is actually running (i.e.,
1475 * it is on the rq AND it is not throttled).
1477 if (!on_dl_rq(&p->dl))
1480 weight = cpumask_weight(new_mask);
1483 * Only update if the process changes its state from whether it
1484 * can migrate or not.
1486 if ((p->nr_cpus_allowed > 1) == (weight > 1))
1492 * The process used to be able to migrate OR it can now migrate
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--;
1500 if (!task_current(rq, p))
1501 enqueue_pushable_dl_task(rq, p);
1502 rq->dl.dl_nr_migratory++;
1505 update_dl_migration(&rq->dl);
1508 /* Assumes rq->lock is held */
1509 static void rq_online_dl(struct rq *rq)
1511 if (rq->dl.overloaded)
1512 dl_set_overload(rq);
1515 /* Assumes rq->lock is held */
1516 static void rq_offline_dl(struct rq *rq)
1518 if (rq->dl.overloaded)
1519 dl_clear_overload(rq);
1522 void init_sched_dl_class(void)
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));
1531 #endif /* CONFIG_SMP */
1533 static void switched_from_dl(struct rq *rq, struct task_struct *p)
1535 if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy))
1536 hrtimer_try_to_cancel(&p->dl.dl_timer);
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.
1544 if (!rq->dl.dl_nr_running)
1550 * When switching to -deadline, we may overload the rq, then
1551 * we try to push someone off, if possible.
1553 static void switched_to_dl(struct rq *rq, struct task_struct *p)
1555 int check_resched = 1;
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.
1562 if (unlikely(p->dl.dl_throttled))
1565 if (p->on_rq || rq->curr != p) {
1567 if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p))
1568 /* Only reschedule if pushing failed */
1570 #endif /* CONFIG_SMP */
1571 if (check_resched && task_has_dl_policy(rq->curr))
1572 check_preempt_curr_dl(rq, p, 0);
1577 * If the scheduling parameters of a -deadline task changed,
1578 * a push or pull operation might be needed.
1580 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
1583 if (p->on_rq || rq->curr == p) {
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...
1591 if (!rq->dl.overloaded)
1595 * If we now have a earlier deadline task than p,
1596 * then reschedule, provided p is still on this
1599 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) &&
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.
1609 #endif /* CONFIG_SMP */
1611 switched_to_dl(rq, p);
1614 const 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,
1620 .check_preempt_curr = check_preempt_curr_dl,
1622 .pick_next_task = pick_next_task_dl,
1623 .put_prev_task = put_prev_task_dl,
1626 .select_task_rq = select_task_rq_dl,
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,
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,
1640 .prio_changed = prio_changed_dl,
1641 .switched_from = switched_from_dl,
1642 .switched_to = switched_to_dl,