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 * Michael Trimarchi <michael@amarulasolutions.com>,
14 * Fabio Checconi <fchecconi@gmail.com>
18 static inline int dl_time_before(u64 a, u64 b)
20 return (s64)(a - b) < 0;
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_rq(struct dl_rq *dl_rq, struct rq *rq)
55 dl_rq->rb_root = RB_ROOT;
58 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
59 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
60 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
64 * We are being explicitly informed that a new instance is starting,
65 * and this means that:
66 * - the absolute deadline of the entity has to be placed at
67 * current time + relative deadline;
68 * - the runtime of the entity has to be set to the maximum value.
70 * The capability of specifying such event is useful whenever a -deadline
71 * entity wants to (try to!) synchronize its behaviour with the scheduler's
72 * one, and to (try to!) reconcile itself with its own scheduling
75 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
77 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
78 struct rq *rq = rq_of_dl_rq(dl_rq);
80 WARN_ON(!dl_se->dl_new || dl_se->dl_throttled);
83 * We use the regular wall clock time to set deadlines in the
84 * future; in fact, we must consider execution overheads (time
85 * spent on hardirq context, etc.).
87 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
88 dl_se->runtime = dl_se->dl_runtime;
93 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
94 * possibility of a entity lasting more than what it declared, and thus
95 * exhausting its runtime.
97 * Here we are interested in making runtime overrun possible, but we do
98 * not want a entity which is misbehaving to affect the scheduling of all
100 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
101 * is used, in order to confine each entity within its own bandwidth.
103 * This function deals exactly with that, and ensures that when the runtime
104 * of a entity is replenished, its deadline is also postponed. That ensures
105 * the overrunning entity can't interfere with other entity in the system and
106 * can't make them miss their deadlines. Reasons why this kind of overruns
107 * could happen are, typically, a entity voluntarily trying to overcome its
108 * runtime, or it just underestimated it during sched_setscheduler_ex().
110 static void replenish_dl_entity(struct sched_dl_entity *dl_se)
112 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
113 struct rq *rq = rq_of_dl_rq(dl_rq);
116 * We keep moving the deadline away until we get some
117 * available runtime for the entity. This ensures correct
118 * handling of situations where the runtime overrun is
121 while (dl_se->runtime <= 0) {
122 dl_se->deadline += dl_se->dl_deadline;
123 dl_se->runtime += dl_se->dl_runtime;
127 * At this point, the deadline really should be "in
128 * the future" with respect to rq->clock. If it's
129 * not, we are, for some reason, lagging too much!
130 * Anyway, after having warn userspace abut that,
131 * we still try to keep the things running by
132 * resetting the deadline and the budget of the
135 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
136 static bool lag_once = false;
140 printk_sched("sched: DL replenish lagged to much\n");
142 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
143 dl_se->runtime = dl_se->dl_runtime;
148 * Here we check if --at time t-- an entity (which is probably being
149 * [re]activated or, in general, enqueued) can use its remaining runtime
150 * and its current deadline _without_ exceeding the bandwidth it is
151 * assigned (function returns true if it can't). We are in fact applying
152 * one of the CBS rules: when a task wakes up, if the residual runtime
153 * over residual deadline fits within the allocated bandwidth, then we
154 * can keep the current (absolute) deadline and residual budget without
155 * disrupting the schedulability of the system. Otherwise, we should
156 * refill the runtime and set the deadline a period in the future,
157 * because keeping the current (absolute) deadline of the task would
158 * result in breaking guarantees promised to other tasks.
160 * This function returns true if:
162 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
164 * IOW we can't recycle current parameters.
166 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
171 * left and right are the two sides of the equation above,
172 * after a bit of shuffling to use multiplications instead
175 * Note that none of the time values involved in the two
176 * multiplications are absolute: dl_deadline and dl_runtime
177 * are the relative deadline and the maximum runtime of each
178 * instance, runtime is the runtime left for the last instance
179 * and (deadline - t), since t is rq->clock, is the time left
180 * to the (absolute) deadline. Even if overflowing the u64 type
181 * is very unlikely to occur in both cases, here we scale down
182 * as we want to avoid that risk at all. Scaling down by 10
183 * means that we reduce granularity to 1us. We are fine with it,
184 * since this is only a true/false check and, anyway, thinking
185 * of anything below microseconds resolution is actually fiction
186 * (but still we want to give the user that illusion >;).
188 left = (dl_se->dl_deadline >> 10) * (dl_se->runtime >> 10);
189 right = ((dl_se->deadline - t) >> 10) * (dl_se->dl_runtime >> 10);
191 return dl_time_before(right, left);
195 * When a -deadline entity is queued back on the runqueue, its runtime and
196 * deadline might need updating.
198 * The policy here is that we update the deadline of the entity only if:
199 * - the current deadline is in the past,
200 * - using the remaining runtime with the current deadline would make
201 * the entity exceed its bandwidth.
203 static void update_dl_entity(struct sched_dl_entity *dl_se)
205 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
206 struct rq *rq = rq_of_dl_rq(dl_rq);
209 * The arrival of a new instance needs special treatment, i.e.,
210 * the actual scheduling parameters have to be "renewed".
213 setup_new_dl_entity(dl_se);
217 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
218 dl_entity_overflow(dl_se, rq_clock(rq))) {
219 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
220 dl_se->runtime = dl_se->dl_runtime;
225 * If the entity depleted all its runtime, and if we want it to sleep
226 * while waiting for some new execution time to become available, we
227 * set the bandwidth enforcement timer to the replenishment instant
228 * and try to activate it.
230 * Notice that it is important for the caller to know if the timer
231 * actually started or not (i.e., the replenishment instant is in
232 * the future or in the past).
234 static int start_dl_timer(struct sched_dl_entity *dl_se)
236 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
237 struct rq *rq = rq_of_dl_rq(dl_rq);
244 * We want the timer to fire at the deadline, but considering
245 * that it is actually coming from rq->clock and not from
246 * hrtimer's time base reading.
248 act = ns_to_ktime(dl_se->deadline);
249 now = hrtimer_cb_get_time(&dl_se->dl_timer);
250 delta = ktime_to_ns(now) - rq_clock(rq);
251 act = ktime_add_ns(act, delta);
254 * If the expiry time already passed, e.g., because the value
255 * chosen as the deadline is too small, don't even try to
256 * start the timer in the past!
258 if (ktime_us_delta(act, now) < 0)
261 hrtimer_set_expires(&dl_se->dl_timer, act);
263 soft = hrtimer_get_softexpires(&dl_se->dl_timer);
264 hard = hrtimer_get_expires(&dl_se->dl_timer);
265 range = ktime_to_ns(ktime_sub(hard, soft));
266 __hrtimer_start_range_ns(&dl_se->dl_timer, soft,
267 range, HRTIMER_MODE_ABS, 0);
269 return hrtimer_active(&dl_se->dl_timer);
273 * This is the bandwidth enforcement timer callback. If here, we know
274 * a task is not on its dl_rq, since the fact that the timer was running
275 * means the task is throttled and needs a runtime replenishment.
277 * However, what we actually do depends on the fact the task is active,
278 * (it is on its rq) or has been removed from there by a call to
279 * dequeue_task_dl(). In the former case we must issue the runtime
280 * replenishment and add the task back to the dl_rq; in the latter, we just
281 * do nothing but clearing dl_throttled, so that runtime and deadline
282 * updating (and the queueing back to dl_rq) will be done by the
283 * next call to enqueue_task_dl().
285 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
287 struct sched_dl_entity *dl_se = container_of(timer,
288 struct sched_dl_entity,
290 struct task_struct *p = dl_task_of(dl_se);
291 struct rq *rq = task_rq(p);
292 raw_spin_lock(&rq->lock);
295 * We need to take care of a possible races here. In fact, the
296 * task might have changed its scheduling policy to something
297 * different from SCHED_DEADLINE or changed its reservation
298 * parameters (through sched_setscheduler()).
300 if (!dl_task(p) || dl_se->dl_new)
305 dl_se->dl_throttled = 0;
307 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
308 if (task_has_dl_policy(rq->curr))
309 check_preempt_curr_dl(rq, p, 0);
311 resched_task(rq->curr);
314 raw_spin_unlock(&rq->lock);
316 return HRTIMER_NORESTART;
319 void init_dl_task_timer(struct sched_dl_entity *dl_se)
321 struct hrtimer *timer = &dl_se->dl_timer;
323 if (hrtimer_active(timer)) {
324 hrtimer_try_to_cancel(timer);
328 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
329 timer->function = dl_task_timer;
333 int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
335 int dmiss = dl_time_before(dl_se->deadline, rq_clock(rq));
336 int rorun = dl_se->runtime <= 0;
338 if (!rorun && !dmiss)
342 * If we are beyond our current deadline and we are still
343 * executing, then we have already used some of the runtime of
344 * the next instance. Thus, if we do not account that, we are
345 * stealing bandwidth from the system at each deadline miss!
348 dl_se->runtime = rorun ? dl_se->runtime : 0;
349 dl_se->runtime -= rq_clock(rq) - dl_se->deadline;
356 * Update the current task's runtime statistics (provided it is still
357 * a -deadline task and has not been removed from the dl_rq).
359 static void update_curr_dl(struct rq *rq)
361 struct task_struct *curr = rq->curr;
362 struct sched_dl_entity *dl_se = &curr->dl;
365 if (!dl_task(curr) || !on_dl_rq(dl_se))
369 * Consumed budget is computed considering the time as
370 * observed by schedulable tasks (excluding time spent
371 * in hardirq context, etc.). Deadlines are instead
372 * computed using hard walltime. This seems to be the more
373 * natural solution, but the full ramifications of this
374 * approach need further study.
376 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
377 if (unlikely((s64)delta_exec < 0))
380 schedstat_set(curr->se.statistics.exec_max,
381 max(curr->se.statistics.exec_max, delta_exec));
383 curr->se.sum_exec_runtime += delta_exec;
384 account_group_exec_runtime(curr, delta_exec);
386 curr->se.exec_start = rq_clock_task(rq);
387 cpuacct_charge(curr, delta_exec);
389 dl_se->runtime -= delta_exec;
390 if (dl_runtime_exceeded(rq, dl_se)) {
391 __dequeue_task_dl(rq, curr, 0);
392 if (likely(start_dl_timer(dl_se)))
393 dl_se->dl_throttled = 1;
395 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
397 if (!is_leftmost(curr, &rq->dl))
402 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
404 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
405 struct rb_node **link = &dl_rq->rb_root.rb_node;
406 struct rb_node *parent = NULL;
407 struct sched_dl_entity *entry;
410 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
414 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
415 if (dl_time_before(dl_se->deadline, entry->deadline))
416 link = &parent->rb_left;
418 link = &parent->rb_right;
424 dl_rq->rb_leftmost = &dl_se->rb_node;
426 rb_link_node(&dl_se->rb_node, parent, link);
427 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
429 dl_rq->dl_nr_running++;
432 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
434 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
436 if (RB_EMPTY_NODE(&dl_se->rb_node))
439 if (dl_rq->rb_leftmost == &dl_se->rb_node) {
440 struct rb_node *next_node;
442 next_node = rb_next(&dl_se->rb_node);
443 dl_rq->rb_leftmost = next_node;
446 rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
447 RB_CLEAR_NODE(&dl_se->rb_node);
449 dl_rq->dl_nr_running--;
453 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
455 BUG_ON(on_dl_rq(dl_se));
458 * If this is a wakeup or a new instance, the scheduling
459 * parameters of the task might need updating. Otherwise,
460 * we want a replenishment of its runtime.
462 if (!dl_se->dl_new && flags & ENQUEUE_REPLENISH)
463 replenish_dl_entity(dl_se);
465 update_dl_entity(dl_se);
467 __enqueue_dl_entity(dl_se);
470 static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
472 __dequeue_dl_entity(dl_se);
475 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
478 * If p is throttled, we do nothing. In fact, if it exhausted
479 * its budget it needs a replenishment and, since it now is on
480 * its rq, the bandwidth timer callback (which clearly has not
481 * run yet) will take care of this.
483 if (p->dl.dl_throttled)
486 enqueue_dl_entity(&p->dl, flags);
490 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
492 dequeue_dl_entity(&p->dl);
495 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
498 __dequeue_task_dl(rq, p, flags);
504 * Yield task semantic for -deadline tasks is:
506 * get off from the CPU until our next instance, with
507 * a new runtime. This is of little use now, since we
508 * don't have a bandwidth reclaiming mechanism. Anyway,
509 * bandwidth reclaiming is planned for the future, and
510 * yield_task_dl will indicate that some spare budget
511 * is available for other task instances to use it.
513 static void yield_task_dl(struct rq *rq)
515 struct task_struct *p = rq->curr;
518 * We make the task go to sleep until its current deadline by
519 * forcing its runtime to zero. This way, update_curr_dl() stops
520 * it and the bandwidth timer will wake it up and will give it
521 * new scheduling parameters (thanks to dl_new=1).
523 if (p->dl.runtime > 0) {
524 rq->curr->dl.dl_new = 1;
531 * Only called when both the current and waking task are -deadline
534 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
537 if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
538 resched_task(rq->curr);
541 #ifdef CONFIG_SCHED_HRTICK
542 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
544 s64 delta = p->dl.dl_runtime - p->dl.runtime;
547 hrtick_start(rq, p->dl.runtime);
551 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
554 struct rb_node *left = dl_rq->rb_leftmost;
559 return rb_entry(left, struct sched_dl_entity, rb_node);
562 struct task_struct *pick_next_task_dl(struct rq *rq)
564 struct sched_dl_entity *dl_se;
565 struct task_struct *p;
570 if (unlikely(!dl_rq->dl_nr_running))
573 dl_se = pick_next_dl_entity(rq, dl_rq);
576 p = dl_task_of(dl_se);
577 p->se.exec_start = rq_clock_task(rq);
578 #ifdef CONFIG_SCHED_HRTICK
579 if (hrtick_enabled(rq))
580 start_hrtick_dl(rq, p);
585 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
590 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
594 #ifdef CONFIG_SCHED_HRTICK
595 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0)
596 start_hrtick_dl(rq, p);
600 static void task_fork_dl(struct task_struct *p)
603 * SCHED_DEADLINE tasks cannot fork and this is achieved through
608 static void task_dead_dl(struct task_struct *p)
610 struct hrtimer *timer = &p->dl.dl_timer;
612 if (hrtimer_active(timer))
613 hrtimer_try_to_cancel(timer);
616 static void set_curr_task_dl(struct rq *rq)
618 struct task_struct *p = rq->curr;
620 p->se.exec_start = rq_clock_task(rq);
623 static void switched_from_dl(struct rq *rq, struct task_struct *p)
625 if (hrtimer_active(&p->dl.dl_timer))
626 hrtimer_try_to_cancel(&p->dl.dl_timer);
629 static void switched_to_dl(struct rq *rq, struct task_struct *p)
632 * If p is throttled, don't consider the possibility
633 * of preempting rq->curr, the check will be done right
634 * after its runtime will get replenished.
636 if (unlikely(p->dl.dl_throttled))
639 if (p->on_rq || rq->curr != p) {
640 if (task_has_dl_policy(rq->curr))
641 check_preempt_curr_dl(rq, p, 0);
643 resched_task(rq->curr);
647 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
650 switched_to_dl(rq, p);
655 select_task_rq_dl(struct task_struct *p, int prev_cpu, int sd_flag, int flags)
661 const struct sched_class dl_sched_class = {
662 .next = &rt_sched_class,
663 .enqueue_task = enqueue_task_dl,
664 .dequeue_task = dequeue_task_dl,
665 .yield_task = yield_task_dl,
667 .check_preempt_curr = check_preempt_curr_dl,
669 .pick_next_task = pick_next_task_dl,
670 .put_prev_task = put_prev_task_dl,
673 .select_task_rq = select_task_rq_dl,
676 .set_curr_task = set_curr_task_dl,
677 .task_tick = task_tick_dl,
678 .task_fork = task_fork_dl,
679 .task_dead = task_dead_dl,
681 .prio_changed = prio_changed_dl,
682 .switched_from = switched_from_dl,
683 .switched_to = switched_to_dl,