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 #include <linux/slab.h>
21 struct dl_bandwidth def_dl_bandwidth;
23 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
25 return container_of(dl_se, struct task_struct, dl);
28 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
30 return container_of(dl_rq, struct rq, dl);
33 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
35 struct task_struct *p = dl_task_of(dl_se);
36 struct rq *rq = task_rq(p);
41 static inline int on_dl_rq(struct sched_dl_entity *dl_se)
43 return !RB_EMPTY_NODE(&dl_se->rb_node);
46 static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
48 struct sched_dl_entity *dl_se = &p->dl;
50 return dl_rq->rb_leftmost == &dl_se->rb_node;
53 void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
55 raw_spin_lock_init(&dl_b->dl_runtime_lock);
56 dl_b->dl_period = period;
57 dl_b->dl_runtime = 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_rt_runtime() == RUNTIME_INF)
67 dl_b->bw = to_ratio(global_rt_period(), global_rt_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_running > 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);
137 if (p->nr_cpus_allowed > 1)
138 dl_rq->dl_nr_migratory++;
140 update_dl_migration(dl_rq);
143 static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
145 struct task_struct *p = dl_task_of(dl_se);
147 if (p->nr_cpus_allowed > 1)
148 dl_rq->dl_nr_migratory--;
150 update_dl_migration(dl_rq);
154 * The list of pushable -deadline task is not a plist, like in
155 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
157 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
159 struct dl_rq *dl_rq = &rq->dl;
160 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
161 struct rb_node *parent = NULL;
162 struct task_struct *entry;
165 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
169 entry = rb_entry(parent, struct task_struct,
171 if (dl_entity_preempt(&p->dl, &entry->dl))
172 link = &parent->rb_left;
174 link = &parent->rb_right;
180 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
182 rb_link_node(&p->pushable_dl_tasks, parent, link);
183 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
186 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
188 struct dl_rq *dl_rq = &rq->dl;
190 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
193 if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
194 struct rb_node *next_node;
196 next_node = rb_next(&p->pushable_dl_tasks);
197 dl_rq->pushable_dl_tasks_leftmost = next_node;
200 rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
201 RB_CLEAR_NODE(&p->pushable_dl_tasks);
204 static inline int has_pushable_dl_tasks(struct rq *rq)
206 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
209 static int push_dl_task(struct rq *rq);
211 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
213 return dl_task(prev);
216 static inline void set_post_schedule(struct rq *rq)
218 rq->post_schedule = has_pushable_dl_tasks(rq);
224 void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
229 void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
234 void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
239 void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
243 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
248 static inline int pull_dl_task(struct rq *rq)
253 static inline void set_post_schedule(struct rq *rq)
256 #endif /* CONFIG_SMP */
258 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
259 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
260 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
264 * We are being explicitly informed that a new instance is starting,
265 * and this means that:
266 * - the absolute deadline of the entity has to be placed at
267 * current time + relative deadline;
268 * - the runtime of the entity has to be set to the maximum value.
270 * The capability of specifying such event is useful whenever a -deadline
271 * entity wants to (try to!) synchronize its behaviour with the scheduler's
272 * one, and to (try to!) reconcile itself with its own scheduling
275 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se,
276 struct sched_dl_entity *pi_se)
278 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
279 struct rq *rq = rq_of_dl_rq(dl_rq);
281 WARN_ON(!dl_se->dl_new || dl_se->dl_throttled);
284 * We use the regular wall clock time to set deadlines in the
285 * future; in fact, we must consider execution overheads (time
286 * spent on hardirq context, etc.).
288 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
289 dl_se->runtime = pi_se->dl_runtime;
294 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
295 * possibility of a entity lasting more than what it declared, and thus
296 * exhausting its runtime.
298 * Here we are interested in making runtime overrun possible, but we do
299 * not want a entity which is misbehaving to affect the scheduling of all
301 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
302 * is used, in order to confine each entity within its own bandwidth.
304 * This function deals exactly with that, and ensures that when the runtime
305 * of a entity is replenished, its deadline is also postponed. That ensures
306 * the overrunning entity can't interfere with other entity in the system and
307 * can't make them miss their deadlines. Reasons why this kind of overruns
308 * could happen are, typically, a entity voluntarily trying to overcome its
309 * runtime, or it just underestimated it during sched_setattr().
311 static void replenish_dl_entity(struct sched_dl_entity *dl_se,
312 struct sched_dl_entity *pi_se)
314 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
315 struct rq *rq = rq_of_dl_rq(dl_rq);
317 BUG_ON(pi_se->dl_runtime <= 0);
320 * This could be the case for a !-dl task that is boosted.
321 * Just go with full inherited parameters.
323 if (dl_se->dl_deadline == 0) {
324 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
325 dl_se->runtime = pi_se->dl_runtime;
329 * We keep moving the deadline away until we get some
330 * available runtime for the entity. This ensures correct
331 * handling of situations where the runtime overrun is
334 while (dl_se->runtime <= 0) {
335 dl_se->deadline += pi_se->dl_period;
336 dl_se->runtime += pi_se->dl_runtime;
340 * At this point, the deadline really should be "in
341 * the future" with respect to rq->clock. If it's
342 * not, we are, for some reason, lagging too much!
343 * Anyway, after having warn userspace abut that,
344 * we still try to keep the things running by
345 * resetting the deadline and the budget of the
348 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
349 printk_deferred_once("sched: DL replenish lagged to much\n");
350 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
351 dl_se->runtime = pi_se->dl_runtime;
356 * Here we check if --at time t-- an entity (which is probably being
357 * [re]activated or, in general, enqueued) can use its remaining runtime
358 * and its current deadline _without_ exceeding the bandwidth it is
359 * assigned (function returns true if it can't). We are in fact applying
360 * one of the CBS rules: when a task wakes up, if the residual runtime
361 * over residual deadline fits within the allocated bandwidth, then we
362 * can keep the current (absolute) deadline and residual budget without
363 * disrupting the schedulability of the system. Otherwise, we should
364 * refill the runtime and set the deadline a period in the future,
365 * because keeping the current (absolute) deadline of the task would
366 * result in breaking guarantees promised to other tasks (refer to
367 * Documentation/scheduler/sched-deadline.txt for more informations).
369 * This function returns true if:
371 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
373 * IOW we can't recycle current parameters.
375 * Notice that the bandwidth check is done against the deadline. For
376 * task with deadline equal to period this is the same of using
377 * dl_period instead of dl_deadline in the equation above.
379 static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
380 struct sched_dl_entity *pi_se, u64 t)
385 * left and right are the two sides of the equation above,
386 * after a bit of shuffling to use multiplications instead
389 * Note that none of the time values involved in the two
390 * multiplications are absolute: dl_deadline and dl_runtime
391 * are the relative deadline and the maximum runtime of each
392 * instance, runtime is the runtime left for the last instance
393 * and (deadline - t), since t is rq->clock, is the time left
394 * to the (absolute) deadline. Even if overflowing the u64 type
395 * is very unlikely to occur in both cases, here we scale down
396 * as we want to avoid that risk at all. Scaling down by 10
397 * means that we reduce granularity to 1us. We are fine with it,
398 * since this is only a true/false check and, anyway, thinking
399 * of anything below microseconds resolution is actually fiction
400 * (but still we want to give the user that illusion >;).
402 left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
403 right = ((dl_se->deadline - t) >> DL_SCALE) *
404 (pi_se->dl_runtime >> DL_SCALE);
406 return dl_time_before(right, left);
410 * When a -deadline entity is queued back on the runqueue, its runtime and
411 * deadline might need updating.
413 * The policy here is that we update the deadline of the entity only if:
414 * - the current deadline is in the past,
415 * - using the remaining runtime with the current deadline would make
416 * the entity exceed its bandwidth.
418 static void update_dl_entity(struct sched_dl_entity *dl_se,
419 struct sched_dl_entity *pi_se)
421 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
422 struct rq *rq = rq_of_dl_rq(dl_rq);
425 * The arrival of a new instance needs special treatment, i.e.,
426 * the actual scheduling parameters have to be "renewed".
429 setup_new_dl_entity(dl_se, pi_se);
433 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
434 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
435 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
436 dl_se->runtime = pi_se->dl_runtime;
441 * If the entity depleted all its runtime, and if we want it to sleep
442 * while waiting for some new execution time to become available, we
443 * set the bandwidth enforcement timer to the replenishment instant
444 * and try to activate it.
446 * Notice that it is important for the caller to know if the timer
447 * actually started or not (i.e., the replenishment instant is in
448 * the future or in the past).
450 static int start_dl_timer(struct sched_dl_entity *dl_se, bool boosted)
452 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
453 struct rq *rq = rq_of_dl_rq(dl_rq);
462 * We want the timer to fire at the deadline, but considering
463 * that it is actually coming from rq->clock and not from
464 * hrtimer's time base reading.
466 act = ns_to_ktime(dl_se->deadline);
467 now = hrtimer_cb_get_time(&dl_se->dl_timer);
468 delta = ktime_to_ns(now) - rq_clock(rq);
469 act = ktime_add_ns(act, delta);
472 * If the expiry time already passed, e.g., because the value
473 * chosen as the deadline is too small, don't even try to
474 * start the timer in the past!
476 if (ktime_us_delta(act, now) < 0)
479 hrtimer_set_expires(&dl_se->dl_timer, act);
481 soft = hrtimer_get_softexpires(&dl_se->dl_timer);
482 hard = hrtimer_get_expires(&dl_se->dl_timer);
483 range = ktime_to_ns(ktime_sub(hard, soft));
484 __hrtimer_start_range_ns(&dl_se->dl_timer, soft,
485 range, HRTIMER_MODE_ABS, 0);
487 return hrtimer_active(&dl_se->dl_timer);
491 * This is the bandwidth enforcement timer callback. If here, we know
492 * a task is not on its dl_rq, since the fact that the timer was running
493 * means the task is throttled and needs a runtime replenishment.
495 * However, what we actually do depends on the fact the task is active,
496 * (it is on its rq) or has been removed from there by a call to
497 * dequeue_task_dl(). In the former case we must issue the runtime
498 * replenishment and add the task back to the dl_rq; in the latter, we just
499 * do nothing but clearing dl_throttled, so that runtime and deadline
500 * updating (and the queueing back to dl_rq) will be done by the
501 * next call to enqueue_task_dl().
503 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
505 struct sched_dl_entity *dl_se = container_of(timer,
506 struct sched_dl_entity,
508 struct task_struct *p = dl_task_of(dl_se);
512 raw_spin_lock(&rq->lock);
514 if (rq != task_rq(p)) {
515 /* Task was moved, retrying. */
516 raw_spin_unlock(&rq->lock);
521 * We need to take care of several possible races here:
523 * - the task might have changed its scheduling policy
524 * to something different than SCHED_DEADLINE
525 * - the task might have changed its reservation parameters
526 * (through sched_setattr())
527 * - the task might have been boosted by someone else and
528 * might be in the boosting/deboosting path
530 * In all this cases we bail out, as the task is already
531 * in the runqueue or is going to be enqueued back anyway.
533 if (!dl_task(p) || dl_se->dl_new ||
534 dl_se->dl_boosted || !dl_se->dl_throttled)
539 dl_se->dl_throttled = 0;
540 dl_se->dl_yielded = 0;
541 if (task_on_rq_queued(p)) {
542 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
543 if (dl_task(rq->curr))
544 check_preempt_curr_dl(rq, p, 0);
549 * Queueing this task back might have overloaded rq,
550 * check if we need to kick someone away.
552 if (has_pushable_dl_tasks(rq))
557 raw_spin_unlock(&rq->lock);
559 return HRTIMER_NORESTART;
562 void init_dl_task_timer(struct sched_dl_entity *dl_se)
564 struct hrtimer *timer = &dl_se->dl_timer;
566 if (hrtimer_active(timer)) {
567 hrtimer_try_to_cancel(timer);
571 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
572 timer->function = dl_task_timer;
576 int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
578 return (dl_se->runtime <= 0);
581 extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
584 * Update the current task's runtime statistics (provided it is still
585 * a -deadline task and has not been removed from the dl_rq).
587 static void update_curr_dl(struct rq *rq)
589 struct task_struct *curr = rq->curr;
590 struct sched_dl_entity *dl_se = &curr->dl;
593 if (!dl_task(curr) || !on_dl_rq(dl_se))
597 * Consumed budget is computed considering the time as
598 * observed by schedulable tasks (excluding time spent
599 * in hardirq context, etc.). Deadlines are instead
600 * computed using hard walltime. This seems to be the more
601 * natural solution, but the full ramifications of this
602 * approach need further study.
604 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
605 if (unlikely((s64)delta_exec <= 0))
608 schedstat_set(curr->se.statistics.exec_max,
609 max(curr->se.statistics.exec_max, delta_exec));
611 curr->se.sum_exec_runtime += delta_exec;
612 account_group_exec_runtime(curr, delta_exec);
614 curr->se.exec_start = rq_clock_task(rq);
615 cpuacct_charge(curr, delta_exec);
617 sched_rt_avg_update(rq, delta_exec);
619 dl_se->runtime -= delta_exec;
620 if (dl_runtime_exceeded(rq, dl_se)) {
621 __dequeue_task_dl(rq, curr, 0);
622 if (likely(start_dl_timer(dl_se, curr->dl.dl_boosted)))
623 dl_se->dl_throttled = 1;
625 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
627 if (!is_leftmost(curr, &rq->dl))
632 * Because -- for now -- we share the rt bandwidth, we need to
633 * account our runtime there too, otherwise actual rt tasks
634 * would be able to exceed the shared quota.
636 * Account to the root rt group for now.
638 * The solution we're working towards is having the RT groups scheduled
639 * using deadline servers -- however there's a few nasties to figure
640 * out before that can happen.
642 if (rt_bandwidth_enabled()) {
643 struct rt_rq *rt_rq = &rq->rt;
645 raw_spin_lock(&rt_rq->rt_runtime_lock);
647 * We'll let actual RT tasks worry about the overflow here, we
648 * have our own CBS to keep us inline; only account when RT
649 * bandwidth is relevant.
651 if (sched_rt_bandwidth_account(rt_rq))
652 rt_rq->rt_time += delta_exec;
653 raw_spin_unlock(&rt_rq->rt_runtime_lock);
659 static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
661 static inline u64 next_deadline(struct rq *rq)
663 struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
665 if (next && dl_prio(next->prio))
666 return next->dl.deadline;
671 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
673 struct rq *rq = rq_of_dl_rq(dl_rq);
675 if (dl_rq->earliest_dl.curr == 0 ||
676 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
678 * If the dl_rq had no -deadline tasks, or if the new task
679 * has shorter deadline than the current one on dl_rq, we
680 * know that the previous earliest becomes our next earliest,
681 * as the new task becomes the earliest itself.
683 dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
684 dl_rq->earliest_dl.curr = deadline;
685 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline, 1);
686 } else if (dl_rq->earliest_dl.next == 0 ||
687 dl_time_before(deadline, dl_rq->earliest_dl.next)) {
689 * On the other hand, if the new -deadline task has a
690 * a later deadline than the earliest one on dl_rq, but
691 * it is earlier than the next (if any), we must
692 * recompute the next-earliest.
694 dl_rq->earliest_dl.next = next_deadline(rq);
698 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
700 struct rq *rq = rq_of_dl_rq(dl_rq);
703 * Since we may have removed our earliest (and/or next earliest)
704 * task we must recompute them.
706 if (!dl_rq->dl_nr_running) {
707 dl_rq->earliest_dl.curr = 0;
708 dl_rq->earliest_dl.next = 0;
709 cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
711 struct rb_node *leftmost = dl_rq->rb_leftmost;
712 struct sched_dl_entity *entry;
714 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
715 dl_rq->earliest_dl.curr = entry->deadline;
716 dl_rq->earliest_dl.next = next_deadline(rq);
717 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline, 1);
723 static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
724 static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
726 #endif /* CONFIG_SMP */
729 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
731 int prio = dl_task_of(dl_se)->prio;
732 u64 deadline = dl_se->deadline;
734 WARN_ON(!dl_prio(prio));
735 dl_rq->dl_nr_running++;
736 add_nr_running(rq_of_dl_rq(dl_rq), 1);
738 inc_dl_deadline(dl_rq, deadline);
739 inc_dl_migration(dl_se, dl_rq);
743 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
745 int prio = dl_task_of(dl_se)->prio;
747 WARN_ON(!dl_prio(prio));
748 WARN_ON(!dl_rq->dl_nr_running);
749 dl_rq->dl_nr_running--;
750 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
752 dec_dl_deadline(dl_rq, dl_se->deadline);
753 dec_dl_migration(dl_se, dl_rq);
756 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
758 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
759 struct rb_node **link = &dl_rq->rb_root.rb_node;
760 struct rb_node *parent = NULL;
761 struct sched_dl_entity *entry;
764 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
768 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
769 if (dl_time_before(dl_se->deadline, entry->deadline))
770 link = &parent->rb_left;
772 link = &parent->rb_right;
778 dl_rq->rb_leftmost = &dl_se->rb_node;
780 rb_link_node(&dl_se->rb_node, parent, link);
781 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
783 inc_dl_tasks(dl_se, dl_rq);
786 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
788 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
790 if (RB_EMPTY_NODE(&dl_se->rb_node))
793 if (dl_rq->rb_leftmost == &dl_se->rb_node) {
794 struct rb_node *next_node;
796 next_node = rb_next(&dl_se->rb_node);
797 dl_rq->rb_leftmost = next_node;
800 rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
801 RB_CLEAR_NODE(&dl_se->rb_node);
803 dec_dl_tasks(dl_se, dl_rq);
807 enqueue_dl_entity(struct sched_dl_entity *dl_se,
808 struct sched_dl_entity *pi_se, int flags)
810 BUG_ON(on_dl_rq(dl_se));
813 * If this is a wakeup or a new instance, the scheduling
814 * parameters of the task might need updating. Otherwise,
815 * we want a replenishment of its runtime.
817 if (dl_se->dl_new || flags & ENQUEUE_WAKEUP)
818 update_dl_entity(dl_se, pi_se);
819 else if (flags & ENQUEUE_REPLENISH)
820 replenish_dl_entity(dl_se, pi_se);
822 __enqueue_dl_entity(dl_se);
825 static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
827 __dequeue_dl_entity(dl_se);
830 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
832 struct task_struct *pi_task = rt_mutex_get_top_task(p);
833 struct sched_dl_entity *pi_se = &p->dl;
836 * Use the scheduling parameters of the top pi-waiter
837 * task if we have one and its (relative) deadline is
838 * smaller than our one... OTW we keep our runtime and
841 if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) {
842 pi_se = &pi_task->dl;
843 } else if (!dl_prio(p->normal_prio)) {
845 * Special case in which we have a !SCHED_DEADLINE task
846 * that is going to be deboosted, but exceedes its
847 * runtime while doing so. No point in replenishing
848 * it, as it's going to return back to its original
849 * scheduling class after this.
851 BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
856 * If p is throttled, we do nothing. In fact, if it exhausted
857 * its budget it needs a replenishment and, since it now is on
858 * its rq, the bandwidth timer callback (which clearly has not
859 * run yet) will take care of this.
861 if (p->dl.dl_throttled)
864 enqueue_dl_entity(&p->dl, pi_se, flags);
866 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
867 enqueue_pushable_dl_task(rq, p);
870 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
872 dequeue_dl_entity(&p->dl);
873 dequeue_pushable_dl_task(rq, p);
876 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
879 __dequeue_task_dl(rq, p, flags);
883 * Yield task semantic for -deadline tasks is:
885 * get off from the CPU until our next instance, with
886 * a new runtime. This is of little use now, since we
887 * don't have a bandwidth reclaiming mechanism. Anyway,
888 * bandwidth reclaiming is planned for the future, and
889 * yield_task_dl will indicate that some spare budget
890 * is available for other task instances to use it.
892 static void yield_task_dl(struct rq *rq)
894 struct task_struct *p = rq->curr;
897 * We make the task go to sleep until its current deadline by
898 * forcing its runtime to zero. This way, update_curr_dl() stops
899 * it and the bandwidth timer will wake it up and will give it
900 * new scheduling parameters (thanks to dl_yielded=1).
902 if (p->dl.runtime > 0) {
903 rq->curr->dl.dl_yielded = 1;
911 static int find_later_rq(struct task_struct *task);
914 select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
916 struct task_struct *curr;
919 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
925 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
928 * If we are dealing with a -deadline task, we must
929 * decide where to wake it up.
930 * If it has a later deadline and the current task
931 * on this rq can't move (provided the waking task
932 * can!) we prefer to send it somewhere else. On the
933 * other hand, if it has a shorter deadline, we
934 * try to make it stay here, it might be important.
936 if (unlikely(dl_task(curr)) &&
937 (curr->nr_cpus_allowed < 2 ||
938 !dl_entity_preempt(&p->dl, &curr->dl)) &&
939 (p->nr_cpus_allowed > 1)) {
940 int target = find_later_rq(p);
951 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
954 * Current can't be migrated, useless to reschedule,
955 * let's hope p can move out.
957 if (rq->curr->nr_cpus_allowed == 1 ||
958 cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
962 * p is migratable, so let's not schedule it and
963 * see if it is pushed or pulled somewhere else.
965 if (p->nr_cpus_allowed != 1 &&
966 cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
972 static int pull_dl_task(struct rq *this_rq);
974 #endif /* CONFIG_SMP */
977 * Only called when both the current and waking task are -deadline
980 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
983 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
990 * In the unlikely case current and p have the same deadline
991 * let us try to decide what's the best thing to do...
993 if ((p->dl.deadline == rq->curr->dl.deadline) &&
994 !test_tsk_need_resched(rq->curr))
995 check_preempt_equal_dl(rq, p);
996 #endif /* CONFIG_SMP */
999 #ifdef CONFIG_SCHED_HRTICK
1000 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1002 hrtick_start(rq, p->dl.runtime);
1006 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1007 struct dl_rq *dl_rq)
1009 struct rb_node *left = dl_rq->rb_leftmost;
1014 return rb_entry(left, struct sched_dl_entity, rb_node);
1017 struct task_struct *pick_next_task_dl(struct rq *rq, struct task_struct *prev)
1019 struct sched_dl_entity *dl_se;
1020 struct task_struct *p;
1021 struct dl_rq *dl_rq;
1025 if (need_pull_dl_task(rq, prev)) {
1028 * pull_rt_task() can drop (and re-acquire) rq->lock; this
1029 * means a stop task can slip in, in which case we need to
1030 * re-start task selection.
1032 if (rq->stop && task_on_rq_queued(rq->stop))
1037 * When prev is DL, we may throttle it in put_prev_task().
1038 * So, we update time before we check for dl_nr_running.
1040 if (prev->sched_class == &dl_sched_class)
1043 if (unlikely(!dl_rq->dl_nr_running))
1046 put_prev_task(rq, prev);
1048 dl_se = pick_next_dl_entity(rq, dl_rq);
1051 p = dl_task_of(dl_se);
1052 p->se.exec_start = rq_clock_task(rq);
1054 /* Running task will never be pushed. */
1055 dequeue_pushable_dl_task(rq, p);
1057 #ifdef CONFIG_SCHED_HRTICK
1058 if (hrtick_enabled(rq))
1059 start_hrtick_dl(rq, p);
1062 set_post_schedule(rq);
1067 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1071 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
1072 enqueue_pushable_dl_task(rq, p);
1075 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1079 #ifdef CONFIG_SCHED_HRTICK
1080 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0)
1081 start_hrtick_dl(rq, p);
1085 static void task_fork_dl(struct task_struct *p)
1088 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1093 static void task_dead_dl(struct task_struct *p)
1095 struct hrtimer *timer = &p->dl.dl_timer;
1096 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1099 * Since we are TASK_DEAD we won't slip out of the domain!
1101 raw_spin_lock_irq(&dl_b->lock);
1102 dl_b->total_bw -= p->dl.dl_bw;
1103 raw_spin_unlock_irq(&dl_b->lock);
1105 hrtimer_cancel(timer);
1108 static void set_curr_task_dl(struct rq *rq)
1110 struct task_struct *p = rq->curr;
1112 p->se.exec_start = rq_clock_task(rq);
1114 /* You can't push away the running task */
1115 dequeue_pushable_dl_task(rq, p);
1120 /* Only try algorithms three times */
1121 #define DL_MAX_TRIES 3
1123 static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1125 if (!task_running(rq, p) &&
1126 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
1131 /* Returns the second earliest -deadline task, NULL otherwise */
1132 static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
1134 struct rb_node *next_node = rq->dl.rb_leftmost;
1135 struct sched_dl_entity *dl_se;
1136 struct task_struct *p = NULL;
1139 next_node = rb_next(next_node);
1141 dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
1142 p = dl_task_of(dl_se);
1144 if (pick_dl_task(rq, p, cpu))
1153 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1155 static int find_later_rq(struct task_struct *task)
1157 struct sched_domain *sd;
1158 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
1159 int this_cpu = smp_processor_id();
1160 int best_cpu, cpu = task_cpu(task);
1162 /* Make sure the mask is initialized first */
1163 if (unlikely(!later_mask))
1166 if (task->nr_cpus_allowed == 1)
1170 * We have to consider system topology and task affinity
1171 * first, then we can look for a suitable cpu.
1173 cpumask_copy(later_mask, task_rq(task)->rd->span);
1174 cpumask_and(later_mask, later_mask, cpu_active_mask);
1175 cpumask_and(later_mask, later_mask, &task->cpus_allowed);
1176 best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
1182 * If we are here, some target has been found,
1183 * the most suitable of which is cached in best_cpu.
1184 * This is, among the runqueues where the current tasks
1185 * have later deadlines than the task's one, the rq
1186 * with the latest possible one.
1188 * Now we check how well this matches with task's
1189 * affinity and system topology.
1191 * The last cpu where the task run is our first
1192 * guess, since it is most likely cache-hot there.
1194 if (cpumask_test_cpu(cpu, later_mask))
1197 * Check if this_cpu is to be skipped (i.e., it is
1198 * not in the mask) or not.
1200 if (!cpumask_test_cpu(this_cpu, later_mask))
1204 for_each_domain(cpu, sd) {
1205 if (sd->flags & SD_WAKE_AFFINE) {
1208 * If possible, preempting this_cpu is
1209 * cheaper than migrating.
1211 if (this_cpu != -1 &&
1212 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1218 * Last chance: if best_cpu is valid and is
1219 * in the mask, that becomes our choice.
1221 if (best_cpu < nr_cpu_ids &&
1222 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
1231 * At this point, all our guesses failed, we just return
1232 * 'something', and let the caller sort the things out.
1237 cpu = cpumask_any(later_mask);
1238 if (cpu < nr_cpu_ids)
1244 /* Locks the rq it finds */
1245 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1247 struct rq *later_rq = NULL;
1251 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1252 cpu = find_later_rq(task);
1254 if ((cpu == -1) || (cpu == rq->cpu))
1257 later_rq = cpu_rq(cpu);
1259 /* Retry if something changed. */
1260 if (double_lock_balance(rq, later_rq)) {
1261 if (unlikely(task_rq(task) != rq ||
1262 !cpumask_test_cpu(later_rq->cpu,
1263 &task->cpus_allowed) ||
1264 task_running(rq, task) ||
1265 !task_on_rq_queued(task))) {
1266 double_unlock_balance(rq, later_rq);
1273 * If the rq we found has no -deadline task, or
1274 * its earliest one has a later deadline than our
1275 * task, the rq is a good one.
1277 if (!later_rq->dl.dl_nr_running ||
1278 dl_time_before(task->dl.deadline,
1279 later_rq->dl.earliest_dl.curr))
1282 /* Otherwise we try again. */
1283 double_unlock_balance(rq, later_rq);
1290 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1292 struct task_struct *p;
1294 if (!has_pushable_dl_tasks(rq))
1297 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
1298 struct task_struct, pushable_dl_tasks);
1300 BUG_ON(rq->cpu != task_cpu(p));
1301 BUG_ON(task_current(rq, p));
1302 BUG_ON(p->nr_cpus_allowed <= 1);
1304 BUG_ON(!task_on_rq_queued(p));
1305 BUG_ON(!dl_task(p));
1311 * See if the non running -deadline tasks on this rq
1312 * can be sent to some other CPU where they can preempt
1313 * and start executing.
1315 static int push_dl_task(struct rq *rq)
1317 struct task_struct *next_task;
1318 struct rq *later_rq;
1320 if (!rq->dl.overloaded)
1323 next_task = pick_next_pushable_dl_task(rq);
1328 if (unlikely(next_task == rq->curr)) {
1334 * If next_task preempts rq->curr, and rq->curr
1335 * can move away, it makes sense to just reschedule
1336 * without going further in pushing next_task.
1338 if (dl_task(rq->curr) &&
1339 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
1340 rq->curr->nr_cpus_allowed > 1) {
1345 /* We might release rq lock */
1346 get_task_struct(next_task);
1348 /* Will lock the rq it'll find */
1349 later_rq = find_lock_later_rq(next_task, rq);
1351 struct task_struct *task;
1354 * We must check all this again, since
1355 * find_lock_later_rq releases rq->lock and it is
1356 * then possible that next_task has migrated.
1358 task = pick_next_pushable_dl_task(rq);
1359 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1361 * The task is still there. We don't try
1362 * again, some other cpu will pull it when ready.
1364 dequeue_pushable_dl_task(rq, next_task);
1372 put_task_struct(next_task);
1377 deactivate_task(rq, next_task, 0);
1378 set_task_cpu(next_task, later_rq->cpu);
1379 activate_task(later_rq, next_task, 0);
1381 resched_curr(later_rq);
1383 double_unlock_balance(rq, later_rq);
1386 put_task_struct(next_task);
1391 static void push_dl_tasks(struct rq *rq)
1393 /* Terminates as it moves a -deadline task */
1394 while (push_dl_task(rq))
1398 static int pull_dl_task(struct rq *this_rq)
1400 int this_cpu = this_rq->cpu, ret = 0, cpu;
1401 struct task_struct *p;
1403 u64 dmin = LONG_MAX;
1405 if (likely(!dl_overloaded(this_rq)))
1409 * Match the barrier from dl_set_overloaded; this guarantees that if we
1410 * see overloaded we must also see the dlo_mask bit.
1414 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
1415 if (this_cpu == cpu)
1418 src_rq = cpu_rq(cpu);
1421 * It looks racy, abd it is! However, as in sched_rt.c,
1422 * we are fine with this.
1424 if (this_rq->dl.dl_nr_running &&
1425 dl_time_before(this_rq->dl.earliest_dl.curr,
1426 src_rq->dl.earliest_dl.next))
1429 /* Might drop this_rq->lock */
1430 double_lock_balance(this_rq, src_rq);
1433 * If there are no more pullable tasks on the
1434 * rq, we're done with it.
1436 if (src_rq->dl.dl_nr_running <= 1)
1439 p = pick_next_earliest_dl_task(src_rq, this_cpu);
1442 * We found a task to be pulled if:
1443 * - it preempts our current (if there's one),
1444 * - it will preempt the last one we pulled (if any).
1446 if (p && dl_time_before(p->dl.deadline, dmin) &&
1447 (!this_rq->dl.dl_nr_running ||
1448 dl_time_before(p->dl.deadline,
1449 this_rq->dl.earliest_dl.curr))) {
1450 WARN_ON(p == src_rq->curr);
1451 WARN_ON(!task_on_rq_queued(p));
1454 * Then we pull iff p has actually an earlier
1455 * deadline than the current task of its runqueue.
1457 if (dl_time_before(p->dl.deadline,
1458 src_rq->curr->dl.deadline))
1463 deactivate_task(src_rq, p, 0);
1464 set_task_cpu(p, this_cpu);
1465 activate_task(this_rq, p, 0);
1466 dmin = p->dl.deadline;
1468 /* Is there any other task even earlier? */
1471 double_unlock_balance(this_rq, src_rq);
1477 static void post_schedule_dl(struct rq *rq)
1483 * Since the task is not running and a reschedule is not going to happen
1484 * anytime soon on its runqueue, we try pushing it away now.
1486 static void task_woken_dl(struct rq *rq, struct task_struct *p)
1488 if (!task_running(rq, p) &&
1489 !test_tsk_need_resched(rq->curr) &&
1490 has_pushable_dl_tasks(rq) &&
1491 p->nr_cpus_allowed > 1 &&
1492 dl_task(rq->curr) &&
1493 (rq->curr->nr_cpus_allowed < 2 ||
1494 dl_entity_preempt(&rq->curr->dl, &p->dl))) {
1499 static void set_cpus_allowed_dl(struct task_struct *p,
1500 const struct cpumask *new_mask)
1505 BUG_ON(!dl_task(p));
1508 * Update only if the task is actually running (i.e.,
1509 * it is on the rq AND it is not throttled).
1511 if (!on_dl_rq(&p->dl))
1514 weight = cpumask_weight(new_mask);
1517 * Only update if the process changes its state from whether it
1518 * can migrate or not.
1520 if ((p->nr_cpus_allowed > 1) == (weight > 1))
1526 * The process used to be able to migrate OR it can now migrate
1529 if (!task_current(rq, p))
1530 dequeue_pushable_dl_task(rq, p);
1531 BUG_ON(!rq->dl.dl_nr_migratory);
1532 rq->dl.dl_nr_migratory--;
1534 if (!task_current(rq, p))
1535 enqueue_pushable_dl_task(rq, p);
1536 rq->dl.dl_nr_migratory++;
1539 update_dl_migration(&rq->dl);
1542 /* Assumes rq->lock is held */
1543 static void rq_online_dl(struct rq *rq)
1545 if (rq->dl.overloaded)
1546 dl_set_overload(rq);
1548 if (rq->dl.dl_nr_running > 0)
1549 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr, 1);
1552 /* Assumes rq->lock is held */
1553 static void rq_offline_dl(struct rq *rq)
1555 if (rq->dl.overloaded)
1556 dl_clear_overload(rq);
1558 cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
1561 void init_sched_dl_class(void)
1565 for_each_possible_cpu(i)
1566 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
1567 GFP_KERNEL, cpu_to_node(i));
1570 #endif /* CONFIG_SMP */
1572 static void switched_from_dl(struct rq *rq, struct task_struct *p)
1574 if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy))
1575 hrtimer_try_to_cancel(&p->dl.dl_timer);
1577 __dl_clear_params(p);
1581 * Since this might be the only -deadline task on the rq,
1582 * this is the right place to try to pull some other one
1583 * from an overloaded cpu, if any.
1585 if (!rq->dl.dl_nr_running)
1591 * When switching to -deadline, we may overload the rq, then
1592 * we try to push someone off, if possible.
1594 static void switched_to_dl(struct rq *rq, struct task_struct *p)
1596 int check_resched = 1;
1599 * If p is throttled, don't consider the possibility
1600 * of preempting rq->curr, the check will be done right
1601 * after its runtime will get replenished.
1603 if (unlikely(p->dl.dl_throttled))
1606 if (task_on_rq_queued(p) && rq->curr != p) {
1608 if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p))
1609 /* Only reschedule if pushing failed */
1611 #endif /* CONFIG_SMP */
1612 if (check_resched) {
1613 if (dl_task(rq->curr))
1614 check_preempt_curr_dl(rq, p, 0);
1622 * If the scheduling parameters of a -deadline task changed,
1623 * a push or pull operation might be needed.
1625 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
1628 if (task_on_rq_queued(p) || rq->curr == p) {
1631 * This might be too much, but unfortunately
1632 * we don't have the old deadline value, and
1633 * we can't argue if the task is increasing
1634 * or lowering its prio, so...
1636 if (!rq->dl.overloaded)
1640 * If we now have a earlier deadline task than p,
1641 * then reschedule, provided p is still on this
1644 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) &&
1649 * Again, we don't know if p has a earlier
1650 * or later deadline, so let's blindly set a
1651 * (maybe not needed) rescheduling point.
1654 #endif /* CONFIG_SMP */
1656 switched_to_dl(rq, p);
1659 const struct sched_class dl_sched_class = {
1660 .next = &rt_sched_class,
1661 .enqueue_task = enqueue_task_dl,
1662 .dequeue_task = dequeue_task_dl,
1663 .yield_task = yield_task_dl,
1665 .check_preempt_curr = check_preempt_curr_dl,
1667 .pick_next_task = pick_next_task_dl,
1668 .put_prev_task = put_prev_task_dl,
1671 .select_task_rq = select_task_rq_dl,
1672 .set_cpus_allowed = set_cpus_allowed_dl,
1673 .rq_online = rq_online_dl,
1674 .rq_offline = rq_offline_dl,
1675 .post_schedule = post_schedule_dl,
1676 .task_woken = task_woken_dl,
1679 .set_curr_task = set_curr_task_dl,
1680 .task_tick = task_tick_dl,
1681 .task_fork = task_fork_dl,
1682 .task_dead = task_dead_dl,
1684 .prio_changed = prio_changed_dl,
1685 .switched_from = switched_from_dl,
1686 .switched_to = switched_to_dl,
1688 .update_curr = update_curr_dl,