Linux 4.15.6
[linux/fpc-iii.git] / kernel / sched / deadline.c
blob2473736c7616dd3810b5295a8a5fe4bc4c16b92c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Deadline Scheduling Class (SCHED_DEADLINE)
5 * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
7 * Tasks that periodically executes their instances for less than their
8 * runtime won't miss any of their deadlines.
9 * Tasks that are not periodic or sporadic or that tries to execute more
10 * than their reserved bandwidth will be slowed down (and may potentially
11 * miss some of their deadlines), and won't affect any other task.
13 * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>,
14 * Juri Lelli <juri.lelli@gmail.com>,
15 * Michael Trimarchi <michael@amarulasolutions.com>,
16 * Fabio Checconi <fchecconi@gmail.com>
18 #include "sched.h"
20 #include <linux/slab.h>
21 #include <uapi/linux/sched/types.h>
23 struct dl_bandwidth def_dl_bandwidth;
25 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
27 return container_of(dl_se, struct task_struct, dl);
30 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
32 return container_of(dl_rq, struct rq, dl);
35 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
37 struct task_struct *p = dl_task_of(dl_se);
38 struct rq *rq = task_rq(p);
40 return &rq->dl;
43 static inline int on_dl_rq(struct sched_dl_entity *dl_se)
45 return !RB_EMPTY_NODE(&dl_se->rb_node);
48 #ifdef CONFIG_SMP
49 static inline struct dl_bw *dl_bw_of(int i)
51 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
52 "sched RCU must be held");
53 return &cpu_rq(i)->rd->dl_bw;
56 static inline int dl_bw_cpus(int i)
58 struct root_domain *rd = cpu_rq(i)->rd;
59 int cpus = 0;
61 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
62 "sched RCU must be held");
63 for_each_cpu_and(i, rd->span, cpu_active_mask)
64 cpus++;
66 return cpus;
68 #else
69 static inline struct dl_bw *dl_bw_of(int i)
71 return &cpu_rq(i)->dl.dl_bw;
74 static inline int dl_bw_cpus(int i)
76 return 1;
78 #endif
80 static inline
81 void add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
83 u64 old = dl_rq->running_bw;
85 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
86 dl_rq->running_bw += dl_bw;
87 SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
88 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
91 static inline
92 void sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
94 u64 old = dl_rq->running_bw;
96 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
97 dl_rq->running_bw -= dl_bw;
98 SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
99 if (dl_rq->running_bw > old)
100 dl_rq->running_bw = 0;
103 static inline
104 void add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
106 u64 old = dl_rq->this_bw;
108 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
109 dl_rq->this_bw += dl_bw;
110 SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */
113 static inline
114 void sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
116 u64 old = dl_rq->this_bw;
118 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
119 dl_rq->this_bw -= dl_bw;
120 SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */
121 if (dl_rq->this_bw > old)
122 dl_rq->this_bw = 0;
123 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
126 void dl_change_utilization(struct task_struct *p, u64 new_bw)
128 struct rq *rq;
130 if (task_on_rq_queued(p))
131 return;
133 rq = task_rq(p);
134 if (p->dl.dl_non_contending) {
135 sub_running_bw(p->dl.dl_bw, &rq->dl);
136 p->dl.dl_non_contending = 0;
138 * If the timer handler is currently running and the
139 * timer cannot be cancelled, inactive_task_timer()
140 * will see that dl_not_contending is not set, and
141 * will not touch the rq's active utilization,
142 * so we are still safe.
144 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
145 put_task_struct(p);
147 sub_rq_bw(p->dl.dl_bw, &rq->dl);
148 add_rq_bw(new_bw, &rq->dl);
152 * The utilization of a task cannot be immediately removed from
153 * the rq active utilization (running_bw) when the task blocks.
154 * Instead, we have to wait for the so called "0-lag time".
156 * If a task blocks before the "0-lag time", a timer (the inactive
157 * timer) is armed, and running_bw is decreased when the timer
158 * fires.
160 * If the task wakes up again before the inactive timer fires,
161 * the timer is cancelled, whereas if the task wakes up after the
162 * inactive timer fired (and running_bw has been decreased) the
163 * task's utilization has to be added to running_bw again.
164 * A flag in the deadline scheduling entity (dl_non_contending)
165 * is used to avoid race conditions between the inactive timer handler
166 * and task wakeups.
168 * The following diagram shows how running_bw is updated. A task is
169 * "ACTIVE" when its utilization contributes to running_bw; an
170 * "ACTIVE contending" task is in the TASK_RUNNING state, while an
171 * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
172 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
173 * time already passed, which does not contribute to running_bw anymore.
174 * +------------------+
175 * wakeup | ACTIVE |
176 * +------------------>+ contending |
177 * | add_running_bw | |
178 * | +----+------+------+
179 * | | ^
180 * | dequeue | |
181 * +--------+-------+ | |
182 * | | t >= 0-lag | | wakeup
183 * | INACTIVE |<---------------+ |
184 * | | sub_running_bw | |
185 * +--------+-------+ | |
186 * ^ | |
187 * | t < 0-lag | |
188 * | | |
189 * | V |
190 * | +----+------+------+
191 * | sub_running_bw | ACTIVE |
192 * +-------------------+ |
193 * inactive timer | non contending |
194 * fired +------------------+
196 * The task_non_contending() function is invoked when a task
197 * blocks, and checks if the 0-lag time already passed or
198 * not (in the first case, it directly updates running_bw;
199 * in the second case, it arms the inactive timer).
201 * The task_contending() function is invoked when a task wakes
202 * up, and checks if the task is still in the "ACTIVE non contending"
203 * state or not (in the second case, it updates running_bw).
205 static void task_non_contending(struct task_struct *p)
207 struct sched_dl_entity *dl_se = &p->dl;
208 struct hrtimer *timer = &dl_se->inactive_timer;
209 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
210 struct rq *rq = rq_of_dl_rq(dl_rq);
211 s64 zerolag_time;
214 * If this is a non-deadline task that has been boosted,
215 * do nothing
217 if (dl_se->dl_runtime == 0)
218 return;
220 WARN_ON(hrtimer_active(&dl_se->inactive_timer));
221 WARN_ON(dl_se->dl_non_contending);
223 zerolag_time = dl_se->deadline -
224 div64_long((dl_se->runtime * dl_se->dl_period),
225 dl_se->dl_runtime);
228 * Using relative times instead of the absolute "0-lag time"
229 * allows to simplify the code
231 zerolag_time -= rq_clock(rq);
234 * If the "0-lag time" already passed, decrease the active
235 * utilization now, instead of starting a timer
237 if (zerolag_time < 0) {
238 if (dl_task(p))
239 sub_running_bw(dl_se->dl_bw, dl_rq);
240 if (!dl_task(p) || p->state == TASK_DEAD) {
241 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
243 if (p->state == TASK_DEAD)
244 sub_rq_bw(p->dl.dl_bw, &rq->dl);
245 raw_spin_lock(&dl_b->lock);
246 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
247 __dl_clear_params(p);
248 raw_spin_unlock(&dl_b->lock);
251 return;
254 dl_se->dl_non_contending = 1;
255 get_task_struct(p);
256 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL);
259 static void task_contending(struct sched_dl_entity *dl_se, int flags)
261 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
264 * If this is a non-deadline task that has been boosted,
265 * do nothing
267 if (dl_se->dl_runtime == 0)
268 return;
270 if (flags & ENQUEUE_MIGRATED)
271 add_rq_bw(dl_se->dl_bw, dl_rq);
273 if (dl_se->dl_non_contending) {
274 dl_se->dl_non_contending = 0;
276 * If the timer handler is currently running and the
277 * timer cannot be cancelled, inactive_task_timer()
278 * will see that dl_not_contending is not set, and
279 * will not touch the rq's active utilization,
280 * so we are still safe.
282 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1)
283 put_task_struct(dl_task_of(dl_se));
284 } else {
286 * Since "dl_non_contending" is not set, the
287 * task's utilization has already been removed from
288 * active utilization (either when the task blocked,
289 * when the "inactive timer" fired).
290 * So, add it back.
292 add_running_bw(dl_se->dl_bw, dl_rq);
296 static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
298 struct sched_dl_entity *dl_se = &p->dl;
300 return dl_rq->root.rb_leftmost == &dl_se->rb_node;
303 void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
305 raw_spin_lock_init(&dl_b->dl_runtime_lock);
306 dl_b->dl_period = period;
307 dl_b->dl_runtime = runtime;
310 void init_dl_bw(struct dl_bw *dl_b)
312 raw_spin_lock_init(&dl_b->lock);
313 raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
314 if (global_rt_runtime() == RUNTIME_INF)
315 dl_b->bw = -1;
316 else
317 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
318 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
319 dl_b->total_bw = 0;
322 void init_dl_rq(struct dl_rq *dl_rq)
324 dl_rq->root = RB_ROOT_CACHED;
326 #ifdef CONFIG_SMP
327 /* zero means no -deadline tasks */
328 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
330 dl_rq->dl_nr_migratory = 0;
331 dl_rq->overloaded = 0;
332 dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
333 #else
334 init_dl_bw(&dl_rq->dl_bw);
335 #endif
337 dl_rq->running_bw = 0;
338 dl_rq->this_bw = 0;
339 init_dl_rq_bw_ratio(dl_rq);
342 #ifdef CONFIG_SMP
344 static inline int dl_overloaded(struct rq *rq)
346 return atomic_read(&rq->rd->dlo_count);
349 static inline void dl_set_overload(struct rq *rq)
351 if (!rq->online)
352 return;
354 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
356 * Must be visible before the overload count is
357 * set (as in sched_rt.c).
359 * Matched by the barrier in pull_dl_task().
361 smp_wmb();
362 atomic_inc(&rq->rd->dlo_count);
365 static inline void dl_clear_overload(struct rq *rq)
367 if (!rq->online)
368 return;
370 atomic_dec(&rq->rd->dlo_count);
371 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
374 static void update_dl_migration(struct dl_rq *dl_rq)
376 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
377 if (!dl_rq->overloaded) {
378 dl_set_overload(rq_of_dl_rq(dl_rq));
379 dl_rq->overloaded = 1;
381 } else if (dl_rq->overloaded) {
382 dl_clear_overload(rq_of_dl_rq(dl_rq));
383 dl_rq->overloaded = 0;
387 static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
389 struct task_struct *p = dl_task_of(dl_se);
391 if (p->nr_cpus_allowed > 1)
392 dl_rq->dl_nr_migratory++;
394 update_dl_migration(dl_rq);
397 static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
399 struct task_struct *p = dl_task_of(dl_se);
401 if (p->nr_cpus_allowed > 1)
402 dl_rq->dl_nr_migratory--;
404 update_dl_migration(dl_rq);
408 * The list of pushable -deadline task is not a plist, like in
409 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
411 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
413 struct dl_rq *dl_rq = &rq->dl;
414 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_root.rb_node;
415 struct rb_node *parent = NULL;
416 struct task_struct *entry;
417 bool leftmost = true;
419 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
421 while (*link) {
422 parent = *link;
423 entry = rb_entry(parent, struct task_struct,
424 pushable_dl_tasks);
425 if (dl_entity_preempt(&p->dl, &entry->dl))
426 link = &parent->rb_left;
427 else {
428 link = &parent->rb_right;
429 leftmost = false;
433 if (leftmost)
434 dl_rq->earliest_dl.next = p->dl.deadline;
436 rb_link_node(&p->pushable_dl_tasks, parent, link);
437 rb_insert_color_cached(&p->pushable_dl_tasks,
438 &dl_rq->pushable_dl_tasks_root, leftmost);
441 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
443 struct dl_rq *dl_rq = &rq->dl;
445 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
446 return;
448 if (dl_rq->pushable_dl_tasks_root.rb_leftmost == &p->pushable_dl_tasks) {
449 struct rb_node *next_node;
451 next_node = rb_next(&p->pushable_dl_tasks);
452 if (next_node) {
453 dl_rq->earliest_dl.next = rb_entry(next_node,
454 struct task_struct, pushable_dl_tasks)->dl.deadline;
458 rb_erase_cached(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
459 RB_CLEAR_NODE(&p->pushable_dl_tasks);
462 static inline int has_pushable_dl_tasks(struct rq *rq)
464 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
467 static int push_dl_task(struct rq *rq);
469 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
471 return dl_task(prev);
474 static DEFINE_PER_CPU(struct callback_head, dl_push_head);
475 static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
477 static void push_dl_tasks(struct rq *);
478 static void pull_dl_task(struct rq *);
480 static inline void queue_push_tasks(struct rq *rq)
482 if (!has_pushable_dl_tasks(rq))
483 return;
485 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
488 static inline void queue_pull_task(struct rq *rq)
490 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
493 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
495 static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
497 struct rq *later_rq = NULL;
499 later_rq = find_lock_later_rq(p, rq);
500 if (!later_rq) {
501 int cpu;
504 * If we cannot preempt any rq, fall back to pick any
505 * online cpu.
507 cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
508 if (cpu >= nr_cpu_ids) {
510 * Fail to find any suitable cpu.
511 * The task will never come back!
513 BUG_ON(dl_bandwidth_enabled());
516 * If admission control is disabled we
517 * try a little harder to let the task
518 * run.
520 cpu = cpumask_any(cpu_active_mask);
522 later_rq = cpu_rq(cpu);
523 double_lock_balance(rq, later_rq);
526 set_task_cpu(p, later_rq->cpu);
527 double_unlock_balance(later_rq, rq);
529 return later_rq;
532 #else
534 static inline
535 void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
539 static inline
540 void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
544 static inline
545 void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
549 static inline
550 void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
554 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
556 return false;
559 static inline void pull_dl_task(struct rq *rq)
563 static inline void queue_push_tasks(struct rq *rq)
567 static inline void queue_pull_task(struct rq *rq)
570 #endif /* CONFIG_SMP */
572 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
573 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
574 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
575 int flags);
578 * We are being explicitly informed that a new instance is starting,
579 * and this means that:
580 * - the absolute deadline of the entity has to be placed at
581 * current time + relative deadline;
582 * - the runtime of the entity has to be set to the maximum value.
584 * The capability of specifying such event is useful whenever a -deadline
585 * entity wants to (try to!) synchronize its behaviour with the scheduler's
586 * one, and to (try to!) reconcile itself with its own scheduling
587 * parameters.
589 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
591 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
592 struct rq *rq = rq_of_dl_rq(dl_rq);
594 WARN_ON(dl_se->dl_boosted);
595 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
598 * We are racing with the deadline timer. So, do nothing because
599 * the deadline timer handler will take care of properly recharging
600 * the runtime and postponing the deadline
602 if (dl_se->dl_throttled)
603 return;
606 * We use the regular wall clock time to set deadlines in the
607 * future; in fact, we must consider execution overheads (time
608 * spent on hardirq context, etc.).
610 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
611 dl_se->runtime = dl_se->dl_runtime;
615 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
616 * possibility of a entity lasting more than what it declared, and thus
617 * exhausting its runtime.
619 * Here we are interested in making runtime overrun possible, but we do
620 * not want a entity which is misbehaving to affect the scheduling of all
621 * other entities.
622 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
623 * is used, in order to confine each entity within its own bandwidth.
625 * This function deals exactly with that, and ensures that when the runtime
626 * of a entity is replenished, its deadline is also postponed. That ensures
627 * the overrunning entity can't interfere with other entity in the system and
628 * can't make them miss their deadlines. Reasons why this kind of overruns
629 * could happen are, typically, a entity voluntarily trying to overcome its
630 * runtime, or it just underestimated it during sched_setattr().
632 static void replenish_dl_entity(struct sched_dl_entity *dl_se,
633 struct sched_dl_entity *pi_se)
635 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
636 struct rq *rq = rq_of_dl_rq(dl_rq);
638 BUG_ON(pi_se->dl_runtime <= 0);
641 * This could be the case for a !-dl task that is boosted.
642 * Just go with full inherited parameters.
644 if (dl_se->dl_deadline == 0) {
645 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
646 dl_se->runtime = pi_se->dl_runtime;
649 if (dl_se->dl_yielded && dl_se->runtime > 0)
650 dl_se->runtime = 0;
653 * We keep moving the deadline away until we get some
654 * available runtime for the entity. This ensures correct
655 * handling of situations where the runtime overrun is
656 * arbitrary large.
658 while (dl_se->runtime <= 0) {
659 dl_se->deadline += pi_se->dl_period;
660 dl_se->runtime += pi_se->dl_runtime;
664 * At this point, the deadline really should be "in
665 * the future" with respect to rq->clock. If it's
666 * not, we are, for some reason, lagging too much!
667 * Anyway, after having warn userspace abut that,
668 * we still try to keep the things running by
669 * resetting the deadline and the budget of the
670 * entity.
672 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
673 printk_deferred_once("sched: DL replenish lagged too much\n");
674 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
675 dl_se->runtime = pi_se->dl_runtime;
678 if (dl_se->dl_yielded)
679 dl_se->dl_yielded = 0;
680 if (dl_se->dl_throttled)
681 dl_se->dl_throttled = 0;
685 * Here we check if --at time t-- an entity (which is probably being
686 * [re]activated or, in general, enqueued) can use its remaining runtime
687 * and its current deadline _without_ exceeding the bandwidth it is
688 * assigned (function returns true if it can't). We are in fact applying
689 * one of the CBS rules: when a task wakes up, if the residual runtime
690 * over residual deadline fits within the allocated bandwidth, then we
691 * can keep the current (absolute) deadline and residual budget without
692 * disrupting the schedulability of the system. Otherwise, we should
693 * refill the runtime and set the deadline a period in the future,
694 * because keeping the current (absolute) deadline of the task would
695 * result in breaking guarantees promised to other tasks (refer to
696 * Documentation/scheduler/sched-deadline.txt for more informations).
698 * This function returns true if:
700 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
702 * IOW we can't recycle current parameters.
704 * Notice that the bandwidth check is done against the deadline. For
705 * task with deadline equal to period this is the same of using
706 * dl_period instead of dl_deadline in the equation above.
708 static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
709 struct sched_dl_entity *pi_se, u64 t)
711 u64 left, right;
714 * left and right are the two sides of the equation above,
715 * after a bit of shuffling to use multiplications instead
716 * of divisions.
718 * Note that none of the time values involved in the two
719 * multiplications are absolute: dl_deadline and dl_runtime
720 * are the relative deadline and the maximum runtime of each
721 * instance, runtime is the runtime left for the last instance
722 * and (deadline - t), since t is rq->clock, is the time left
723 * to the (absolute) deadline. Even if overflowing the u64 type
724 * is very unlikely to occur in both cases, here we scale down
725 * as we want to avoid that risk at all. Scaling down by 10
726 * means that we reduce granularity to 1us. We are fine with it,
727 * since this is only a true/false check and, anyway, thinking
728 * of anything below microseconds resolution is actually fiction
729 * (but still we want to give the user that illusion >;).
731 left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
732 right = ((dl_se->deadline - t) >> DL_SCALE) *
733 (pi_se->dl_runtime >> DL_SCALE);
735 return dl_time_before(right, left);
739 * Revised wakeup rule [1]: For self-suspending tasks, rather then
740 * re-initializing task's runtime and deadline, the revised wakeup
741 * rule adjusts the task's runtime to avoid the task to overrun its
742 * density.
744 * Reasoning: a task may overrun the density if:
745 * runtime / (deadline - t) > dl_runtime / dl_deadline
747 * Therefore, runtime can be adjusted to:
748 * runtime = (dl_runtime / dl_deadline) * (deadline - t)
750 * In such way that runtime will be equal to the maximum density
751 * the task can use without breaking any rule.
753 * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
754 * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
756 static void
757 update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
759 u64 laxity = dl_se->deadline - rq_clock(rq);
762 * If the task has deadline < period, and the deadline is in the past,
763 * it should already be throttled before this check.
765 * See update_dl_entity() comments for further details.
767 WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
769 dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
773 * Regarding the deadline, a task with implicit deadline has a relative
774 * deadline == relative period. A task with constrained deadline has a
775 * relative deadline <= relative period.
777 * We support constrained deadline tasks. However, there are some restrictions
778 * applied only for tasks which do not have an implicit deadline. See
779 * update_dl_entity() to know more about such restrictions.
781 * The dl_is_implicit() returns true if the task has an implicit deadline.
783 static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
785 return dl_se->dl_deadline == dl_se->dl_period;
789 * When a deadline entity is placed in the runqueue, its runtime and deadline
790 * might need to be updated. This is done by a CBS wake up rule. There are two
791 * different rules: 1) the original CBS; and 2) the Revisited CBS.
793 * When the task is starting a new period, the Original CBS is used. In this
794 * case, the runtime is replenished and a new absolute deadline is set.
796 * When a task is queued before the begin of the next period, using the
797 * remaining runtime and deadline could make the entity to overflow, see
798 * dl_entity_overflow() to find more about runtime overflow. When such case
799 * is detected, the runtime and deadline need to be updated.
801 * If the task has an implicit deadline, i.e., deadline == period, the Original
802 * CBS is applied. the runtime is replenished and a new absolute deadline is
803 * set, as in the previous cases.
805 * However, the Original CBS does not work properly for tasks with
806 * deadline < period, which are said to have a constrained deadline. By
807 * applying the Original CBS, a constrained deadline task would be able to run
808 * runtime/deadline in a period. With deadline < period, the task would
809 * overrun the runtime/period allowed bandwidth, breaking the admission test.
811 * In order to prevent this misbehave, the Revisited CBS is used for
812 * constrained deadline tasks when a runtime overflow is detected. In the
813 * Revisited CBS, rather than replenishing & setting a new absolute deadline,
814 * the remaining runtime of the task is reduced to avoid runtime overflow.
815 * Please refer to the comments update_dl_revised_wakeup() function to find
816 * more about the Revised CBS rule.
818 static void update_dl_entity(struct sched_dl_entity *dl_se,
819 struct sched_dl_entity *pi_se)
821 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
822 struct rq *rq = rq_of_dl_rq(dl_rq);
824 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
825 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
827 if (unlikely(!dl_is_implicit(dl_se) &&
828 !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
829 !dl_se->dl_boosted)){
830 update_dl_revised_wakeup(dl_se, rq);
831 return;
834 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
835 dl_se->runtime = pi_se->dl_runtime;
839 static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
841 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
845 * If the entity depleted all its runtime, and if we want it to sleep
846 * while waiting for some new execution time to become available, we
847 * set the bandwidth replenishment timer to the replenishment instant
848 * and try to activate it.
850 * Notice that it is important for the caller to know if the timer
851 * actually started or not (i.e., the replenishment instant is in
852 * the future or in the past).
854 static int start_dl_timer(struct task_struct *p)
856 struct sched_dl_entity *dl_se = &p->dl;
857 struct hrtimer *timer = &dl_se->dl_timer;
858 struct rq *rq = task_rq(p);
859 ktime_t now, act;
860 s64 delta;
862 lockdep_assert_held(&rq->lock);
865 * We want the timer to fire at the deadline, but considering
866 * that it is actually coming from rq->clock and not from
867 * hrtimer's time base reading.
869 act = ns_to_ktime(dl_next_period(dl_se));
870 now = hrtimer_cb_get_time(timer);
871 delta = ktime_to_ns(now) - rq_clock(rq);
872 act = ktime_add_ns(act, delta);
875 * If the expiry time already passed, e.g., because the value
876 * chosen as the deadline is too small, don't even try to
877 * start the timer in the past!
879 if (ktime_us_delta(act, now) < 0)
880 return 0;
883 * !enqueued will guarantee another callback; even if one is already in
884 * progress. This ensures a balanced {get,put}_task_struct().
886 * The race against __run_timer() clearing the enqueued state is
887 * harmless because we're holding task_rq()->lock, therefore the timer
888 * expiring after we've done the check will wait on its task_rq_lock()
889 * and observe our state.
891 if (!hrtimer_is_queued(timer)) {
892 get_task_struct(p);
893 hrtimer_start(timer, act, HRTIMER_MODE_ABS);
896 return 1;
900 * This is the bandwidth enforcement timer callback. If here, we know
901 * a task is not on its dl_rq, since the fact that the timer was running
902 * means the task is throttled and needs a runtime replenishment.
904 * However, what we actually do depends on the fact the task is active,
905 * (it is on its rq) or has been removed from there by a call to
906 * dequeue_task_dl(). In the former case we must issue the runtime
907 * replenishment and add the task back to the dl_rq; in the latter, we just
908 * do nothing but clearing dl_throttled, so that runtime and deadline
909 * updating (and the queueing back to dl_rq) will be done by the
910 * next call to enqueue_task_dl().
912 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
914 struct sched_dl_entity *dl_se = container_of(timer,
915 struct sched_dl_entity,
916 dl_timer);
917 struct task_struct *p = dl_task_of(dl_se);
918 struct rq_flags rf;
919 struct rq *rq;
921 rq = task_rq_lock(p, &rf);
924 * The task might have changed its scheduling policy to something
925 * different than SCHED_DEADLINE (through switched_from_dl()).
927 if (!dl_task(p))
928 goto unlock;
931 * The task might have been boosted by someone else and might be in the
932 * boosting/deboosting path, its not throttled.
934 if (dl_se->dl_boosted)
935 goto unlock;
938 * Spurious timer due to start_dl_timer() race; or we already received
939 * a replenishment from rt_mutex_setprio().
941 if (!dl_se->dl_throttled)
942 goto unlock;
944 sched_clock_tick();
945 update_rq_clock(rq);
948 * If the throttle happened during sched-out; like:
950 * schedule()
951 * deactivate_task()
952 * dequeue_task_dl()
953 * update_curr_dl()
954 * start_dl_timer()
955 * __dequeue_task_dl()
956 * prev->on_rq = 0;
958 * We can be both throttled and !queued. Replenish the counter
959 * but do not enqueue -- wait for our wakeup to do that.
961 if (!task_on_rq_queued(p)) {
962 replenish_dl_entity(dl_se, dl_se);
963 goto unlock;
966 #ifdef CONFIG_SMP
967 if (unlikely(!rq->online)) {
969 * If the runqueue is no longer available, migrate the
970 * task elsewhere. This necessarily changes rq.
972 lockdep_unpin_lock(&rq->lock, rf.cookie);
973 rq = dl_task_offline_migration(rq, p);
974 rf.cookie = lockdep_pin_lock(&rq->lock);
975 update_rq_clock(rq);
978 * Now that the task has been migrated to the new RQ and we
979 * have that locked, proceed as normal and enqueue the task
980 * there.
983 #endif
985 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
986 if (dl_task(rq->curr))
987 check_preempt_curr_dl(rq, p, 0);
988 else
989 resched_curr(rq);
991 #ifdef CONFIG_SMP
993 * Queueing this task back might have overloaded rq, check if we need
994 * to kick someone away.
996 if (has_pushable_dl_tasks(rq)) {
998 * Nothing relies on rq->lock after this, so its safe to drop
999 * rq->lock.
1001 rq_unpin_lock(rq, &rf);
1002 push_dl_task(rq);
1003 rq_repin_lock(rq, &rf);
1005 #endif
1007 unlock:
1008 task_rq_unlock(rq, p, &rf);
1011 * This can free the task_struct, including this hrtimer, do not touch
1012 * anything related to that after this.
1014 put_task_struct(p);
1016 return HRTIMER_NORESTART;
1019 void init_dl_task_timer(struct sched_dl_entity *dl_se)
1021 struct hrtimer *timer = &dl_se->dl_timer;
1023 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1024 timer->function = dl_task_timer;
1028 * During the activation, CBS checks if it can reuse the current task's
1029 * runtime and period. If the deadline of the task is in the past, CBS
1030 * cannot use the runtime, and so it replenishes the task. This rule
1031 * works fine for implicit deadline tasks (deadline == period), and the
1032 * CBS was designed for implicit deadline tasks. However, a task with
1033 * constrained deadline (deadine < period) might be awakened after the
1034 * deadline, but before the next period. In this case, replenishing the
1035 * task would allow it to run for runtime / deadline. As in this case
1036 * deadline < period, CBS enables a task to run for more than the
1037 * runtime / period. In a very loaded system, this can cause a domino
1038 * effect, making other tasks miss their deadlines.
1040 * To avoid this problem, in the activation of a constrained deadline
1041 * task after the deadline but before the next period, throttle the
1042 * task and set the replenishing timer to the begin of the next period,
1043 * unless it is boosted.
1045 static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
1047 struct task_struct *p = dl_task_of(dl_se);
1048 struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
1050 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1051 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
1052 if (unlikely(dl_se->dl_boosted || !start_dl_timer(p)))
1053 return;
1054 dl_se->dl_throttled = 1;
1055 if (dl_se->runtime > 0)
1056 dl_se->runtime = 0;
1060 static
1061 int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
1063 return (dl_se->runtime <= 0);
1066 extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
1069 * This function implements the GRUB accounting rule:
1070 * according to the GRUB reclaiming algorithm, the runtime is
1071 * not decreased as "dq = -dt", but as
1072 * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt",
1073 * where u is the utilization of the task, Umax is the maximum reclaimable
1074 * utilization, Uinact is the (per-runqueue) inactive utilization, computed
1075 * as the difference between the "total runqueue utilization" and the
1076 * runqueue active utilization, and Uextra is the (per runqueue) extra
1077 * reclaimable utilization.
1078 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations
1079 * multiplied by 2^BW_SHIFT, the result has to be shifted right by
1080 * BW_SHIFT.
1081 * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT,
1082 * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
1083 * Since delta is a 64 bit variable, to have an overflow its value
1084 * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds.
1085 * So, overflow is not an issue here.
1087 u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
1089 u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
1090 u64 u_act;
1091 u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT;
1094 * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)},
1095 * we compare u_inact + rq->dl.extra_bw with
1096 * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because
1097 * u_inact + rq->dl.extra_bw can be larger than
1098 * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative
1099 * leading to wrong results)
1101 if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min)
1102 u_act = u_act_min;
1103 else
1104 u_act = BW_UNIT - u_inact - rq->dl.extra_bw;
1106 return (delta * u_act) >> BW_SHIFT;
1110 * Update the current task's runtime statistics (provided it is still
1111 * a -deadline task and has not been removed from the dl_rq).
1113 static void update_curr_dl(struct rq *rq)
1115 struct task_struct *curr = rq->curr;
1116 struct sched_dl_entity *dl_se = &curr->dl;
1117 u64 delta_exec;
1119 if (!dl_task(curr) || !on_dl_rq(dl_se))
1120 return;
1123 * Consumed budget is computed considering the time as
1124 * observed by schedulable tasks (excluding time spent
1125 * in hardirq context, etc.). Deadlines are instead
1126 * computed using hard walltime. This seems to be the more
1127 * natural solution, but the full ramifications of this
1128 * approach need further study.
1130 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
1131 if (unlikely((s64)delta_exec <= 0)) {
1132 if (unlikely(dl_se->dl_yielded))
1133 goto throttle;
1134 return;
1137 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
1138 cpufreq_update_util(rq, SCHED_CPUFREQ_DL);
1140 schedstat_set(curr->se.statistics.exec_max,
1141 max(curr->se.statistics.exec_max, delta_exec));
1143 curr->se.sum_exec_runtime += delta_exec;
1144 account_group_exec_runtime(curr, delta_exec);
1146 curr->se.exec_start = rq_clock_task(rq);
1147 cgroup_account_cputime(curr, delta_exec);
1149 sched_rt_avg_update(rq, delta_exec);
1151 if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM))
1152 delta_exec = grub_reclaim(delta_exec, rq, &curr->dl);
1153 dl_se->runtime -= delta_exec;
1155 throttle:
1156 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
1157 dl_se->dl_throttled = 1;
1158 __dequeue_task_dl(rq, curr, 0);
1159 if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
1160 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
1162 if (!is_leftmost(curr, &rq->dl))
1163 resched_curr(rq);
1167 * Because -- for now -- we share the rt bandwidth, we need to
1168 * account our runtime there too, otherwise actual rt tasks
1169 * would be able to exceed the shared quota.
1171 * Account to the root rt group for now.
1173 * The solution we're working towards is having the RT groups scheduled
1174 * using deadline servers -- however there's a few nasties to figure
1175 * out before that can happen.
1177 if (rt_bandwidth_enabled()) {
1178 struct rt_rq *rt_rq = &rq->rt;
1180 raw_spin_lock(&rt_rq->rt_runtime_lock);
1182 * We'll let actual RT tasks worry about the overflow here, we
1183 * have our own CBS to keep us inline; only account when RT
1184 * bandwidth is relevant.
1186 if (sched_rt_bandwidth_account(rt_rq))
1187 rt_rq->rt_time += delta_exec;
1188 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1192 static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
1194 struct sched_dl_entity *dl_se = container_of(timer,
1195 struct sched_dl_entity,
1196 inactive_timer);
1197 struct task_struct *p = dl_task_of(dl_se);
1198 struct rq_flags rf;
1199 struct rq *rq;
1201 rq = task_rq_lock(p, &rf);
1203 if (!dl_task(p) || p->state == TASK_DEAD) {
1204 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1206 if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
1207 sub_running_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl));
1208 sub_rq_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl));
1209 dl_se->dl_non_contending = 0;
1212 raw_spin_lock(&dl_b->lock);
1213 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
1214 raw_spin_unlock(&dl_b->lock);
1215 __dl_clear_params(p);
1217 goto unlock;
1219 if (dl_se->dl_non_contending == 0)
1220 goto unlock;
1222 sched_clock_tick();
1223 update_rq_clock(rq);
1225 sub_running_bw(dl_se->dl_bw, &rq->dl);
1226 dl_se->dl_non_contending = 0;
1227 unlock:
1228 task_rq_unlock(rq, p, &rf);
1229 put_task_struct(p);
1231 return HRTIMER_NORESTART;
1234 void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1236 struct hrtimer *timer = &dl_se->inactive_timer;
1238 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1239 timer->function = inactive_task_timer;
1242 #ifdef CONFIG_SMP
1244 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1246 struct rq *rq = rq_of_dl_rq(dl_rq);
1248 if (dl_rq->earliest_dl.curr == 0 ||
1249 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
1250 dl_rq->earliest_dl.curr = deadline;
1251 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
1255 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1257 struct rq *rq = rq_of_dl_rq(dl_rq);
1260 * Since we may have removed our earliest (and/or next earliest)
1261 * task we must recompute them.
1263 if (!dl_rq->dl_nr_running) {
1264 dl_rq->earliest_dl.curr = 0;
1265 dl_rq->earliest_dl.next = 0;
1266 cpudl_clear(&rq->rd->cpudl, rq->cpu);
1267 } else {
1268 struct rb_node *leftmost = dl_rq->root.rb_leftmost;
1269 struct sched_dl_entity *entry;
1271 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
1272 dl_rq->earliest_dl.curr = entry->deadline;
1273 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
1277 #else
1279 static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1280 static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1282 #endif /* CONFIG_SMP */
1284 static inline
1285 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1287 int prio = dl_task_of(dl_se)->prio;
1288 u64 deadline = dl_se->deadline;
1290 WARN_ON(!dl_prio(prio));
1291 dl_rq->dl_nr_running++;
1292 add_nr_running(rq_of_dl_rq(dl_rq), 1);
1294 inc_dl_deadline(dl_rq, deadline);
1295 inc_dl_migration(dl_se, dl_rq);
1298 static inline
1299 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1301 int prio = dl_task_of(dl_se)->prio;
1303 WARN_ON(!dl_prio(prio));
1304 WARN_ON(!dl_rq->dl_nr_running);
1305 dl_rq->dl_nr_running--;
1306 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
1308 dec_dl_deadline(dl_rq, dl_se->deadline);
1309 dec_dl_migration(dl_se, dl_rq);
1312 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1314 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1315 struct rb_node **link = &dl_rq->root.rb_root.rb_node;
1316 struct rb_node *parent = NULL;
1317 struct sched_dl_entity *entry;
1318 int leftmost = 1;
1320 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
1322 while (*link) {
1323 parent = *link;
1324 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
1325 if (dl_time_before(dl_se->deadline, entry->deadline))
1326 link = &parent->rb_left;
1327 else {
1328 link = &parent->rb_right;
1329 leftmost = 0;
1333 rb_link_node(&dl_se->rb_node, parent, link);
1334 rb_insert_color_cached(&dl_se->rb_node, &dl_rq->root, leftmost);
1336 inc_dl_tasks(dl_se, dl_rq);
1339 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1341 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1343 if (RB_EMPTY_NODE(&dl_se->rb_node))
1344 return;
1346 rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
1347 RB_CLEAR_NODE(&dl_se->rb_node);
1349 dec_dl_tasks(dl_se, dl_rq);
1352 static void
1353 enqueue_dl_entity(struct sched_dl_entity *dl_se,
1354 struct sched_dl_entity *pi_se, int flags)
1356 BUG_ON(on_dl_rq(dl_se));
1359 * If this is a wakeup or a new instance, the scheduling
1360 * parameters of the task might need updating. Otherwise,
1361 * we want a replenishment of its runtime.
1363 if (flags & ENQUEUE_WAKEUP) {
1364 task_contending(dl_se, flags);
1365 update_dl_entity(dl_se, pi_se);
1366 } else if (flags & ENQUEUE_REPLENISH) {
1367 replenish_dl_entity(dl_se, pi_se);
1368 } else if ((flags & ENQUEUE_RESTORE) &&
1369 dl_time_before(dl_se->deadline,
1370 rq_clock(rq_of_dl_rq(dl_rq_of_se(dl_se))))) {
1371 setup_new_dl_entity(dl_se);
1374 __enqueue_dl_entity(dl_se);
1377 static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
1379 __dequeue_dl_entity(dl_se);
1382 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1384 struct task_struct *pi_task = rt_mutex_get_top_task(p);
1385 struct sched_dl_entity *pi_se = &p->dl;
1388 * Use the scheduling parameters of the top pi-waiter task if:
1389 * - we have a top pi-waiter which is a SCHED_DEADLINE task AND
1390 * - our dl_boosted is set (i.e. the pi-waiter's (absolute) deadline is
1391 * smaller than our deadline OR we are a !SCHED_DEADLINE task getting
1392 * boosted due to a SCHED_DEADLINE pi-waiter).
1393 * Otherwise we keep our runtime and deadline.
1395 if (pi_task && dl_prio(pi_task->normal_prio) && p->dl.dl_boosted) {
1396 pi_se = &pi_task->dl;
1397 } else if (!dl_prio(p->normal_prio)) {
1399 * Special case in which we have a !SCHED_DEADLINE task
1400 * that is going to be deboosted, but exceeds its
1401 * runtime while doing so. No point in replenishing
1402 * it, as it's going to return back to its original
1403 * scheduling class after this.
1405 BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
1406 return;
1410 * Check if a constrained deadline task was activated
1411 * after the deadline but before the next period.
1412 * If that is the case, the task will be throttled and
1413 * the replenishment timer will be set to the next period.
1415 if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl))
1416 dl_check_constrained_dl(&p->dl);
1418 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) {
1419 add_rq_bw(p->dl.dl_bw, &rq->dl);
1420 add_running_bw(p->dl.dl_bw, &rq->dl);
1424 * If p is throttled, we do not enqueue it. In fact, if it exhausted
1425 * its budget it needs a replenishment and, since it now is on
1426 * its rq, the bandwidth timer callback (which clearly has not
1427 * run yet) will take care of this.
1428 * However, the active utilization does not depend on the fact
1429 * that the task is on the runqueue or not (but depends on the
1430 * task's state - in GRUB parlance, "inactive" vs "active contending").
1431 * In other words, even if a task is throttled its utilization must
1432 * be counted in the active utilization; hence, we need to call
1433 * add_running_bw().
1435 if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
1436 if (flags & ENQUEUE_WAKEUP)
1437 task_contending(&p->dl, flags);
1439 return;
1442 enqueue_dl_entity(&p->dl, pi_se, flags);
1444 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1445 enqueue_pushable_dl_task(rq, p);
1448 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1450 dequeue_dl_entity(&p->dl);
1451 dequeue_pushable_dl_task(rq, p);
1454 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1456 update_curr_dl(rq);
1457 __dequeue_task_dl(rq, p, flags);
1459 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) {
1460 sub_running_bw(p->dl.dl_bw, &rq->dl);
1461 sub_rq_bw(p->dl.dl_bw, &rq->dl);
1465 * This check allows to start the inactive timer (or to immediately
1466 * decrease the active utilization, if needed) in two cases:
1467 * when the task blocks and when it is terminating
1468 * (p->state == TASK_DEAD). We can handle the two cases in the same
1469 * way, because from GRUB's point of view the same thing is happening
1470 * (the task moves from "active contending" to "active non contending"
1471 * or "inactive")
1473 if (flags & DEQUEUE_SLEEP)
1474 task_non_contending(p);
1478 * Yield task semantic for -deadline tasks is:
1480 * get off from the CPU until our next instance, with
1481 * a new runtime. This is of little use now, since we
1482 * don't have a bandwidth reclaiming mechanism. Anyway,
1483 * bandwidth reclaiming is planned for the future, and
1484 * yield_task_dl will indicate that some spare budget
1485 * is available for other task instances to use it.
1487 static void yield_task_dl(struct rq *rq)
1490 * We make the task go to sleep until its current deadline by
1491 * forcing its runtime to zero. This way, update_curr_dl() stops
1492 * it and the bandwidth timer will wake it up and will give it
1493 * new scheduling parameters (thanks to dl_yielded=1).
1495 rq->curr->dl.dl_yielded = 1;
1497 update_rq_clock(rq);
1498 update_curr_dl(rq);
1500 * Tell update_rq_clock() that we've just updated,
1501 * so we don't do microscopic update in schedule()
1502 * and double the fastpath cost.
1504 rq_clock_skip_update(rq, true);
1507 #ifdef CONFIG_SMP
1509 static int find_later_rq(struct task_struct *task);
1511 static int
1512 select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1514 struct task_struct *curr;
1515 struct rq *rq;
1517 if (sd_flag != SD_BALANCE_WAKE)
1518 goto out;
1520 rq = cpu_rq(cpu);
1522 rcu_read_lock();
1523 curr = READ_ONCE(rq->curr); /* unlocked access */
1526 * If we are dealing with a -deadline task, we must
1527 * decide where to wake it up.
1528 * If it has a later deadline and the current task
1529 * on this rq can't move (provided the waking task
1530 * can!) we prefer to send it somewhere else. On the
1531 * other hand, if it has a shorter deadline, we
1532 * try to make it stay here, it might be important.
1534 if (unlikely(dl_task(curr)) &&
1535 (curr->nr_cpus_allowed < 2 ||
1536 !dl_entity_preempt(&p->dl, &curr->dl)) &&
1537 (p->nr_cpus_allowed > 1)) {
1538 int target = find_later_rq(p);
1540 if (target != -1 &&
1541 (dl_time_before(p->dl.deadline,
1542 cpu_rq(target)->dl.earliest_dl.curr) ||
1543 (cpu_rq(target)->dl.dl_nr_running == 0)))
1544 cpu = target;
1546 rcu_read_unlock();
1548 out:
1549 return cpu;
1552 static void migrate_task_rq_dl(struct task_struct *p)
1554 struct rq *rq;
1556 if (p->state != TASK_WAKING)
1557 return;
1559 rq = task_rq(p);
1561 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1562 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1563 * rq->lock is not... So, lock it
1565 raw_spin_lock(&rq->lock);
1566 if (p->dl.dl_non_contending) {
1567 sub_running_bw(p->dl.dl_bw, &rq->dl);
1568 p->dl.dl_non_contending = 0;
1570 * If the timer handler is currently running and the
1571 * timer cannot be cancelled, inactive_task_timer()
1572 * will see that dl_not_contending is not set, and
1573 * will not touch the rq's active utilization,
1574 * so we are still safe.
1576 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1577 put_task_struct(p);
1579 sub_rq_bw(p->dl.dl_bw, &rq->dl);
1580 raw_spin_unlock(&rq->lock);
1583 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1586 * Current can't be migrated, useless to reschedule,
1587 * let's hope p can move out.
1589 if (rq->curr->nr_cpus_allowed == 1 ||
1590 !cpudl_find(&rq->rd->cpudl, rq->curr, NULL))
1591 return;
1594 * p is migratable, so let's not schedule it and
1595 * see if it is pushed or pulled somewhere else.
1597 if (p->nr_cpus_allowed != 1 &&
1598 cpudl_find(&rq->rd->cpudl, p, NULL))
1599 return;
1601 resched_curr(rq);
1604 #endif /* CONFIG_SMP */
1607 * Only called when both the current and waking task are -deadline
1608 * tasks.
1610 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1611 int flags)
1613 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
1614 resched_curr(rq);
1615 return;
1618 #ifdef CONFIG_SMP
1620 * In the unlikely case current and p have the same deadline
1621 * let us try to decide what's the best thing to do...
1623 if ((p->dl.deadline == rq->curr->dl.deadline) &&
1624 !test_tsk_need_resched(rq->curr))
1625 check_preempt_equal_dl(rq, p);
1626 #endif /* CONFIG_SMP */
1629 #ifdef CONFIG_SCHED_HRTICK
1630 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1632 hrtick_start(rq, p->dl.runtime);
1634 #else /* !CONFIG_SCHED_HRTICK */
1635 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1638 #endif
1640 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1641 struct dl_rq *dl_rq)
1643 struct rb_node *left = rb_first_cached(&dl_rq->root);
1645 if (!left)
1646 return NULL;
1648 return rb_entry(left, struct sched_dl_entity, rb_node);
1651 static struct task_struct *
1652 pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
1654 struct sched_dl_entity *dl_se;
1655 struct task_struct *p;
1656 struct dl_rq *dl_rq;
1658 dl_rq = &rq->dl;
1660 if (need_pull_dl_task(rq, prev)) {
1662 * This is OK, because current is on_cpu, which avoids it being
1663 * picked for load-balance and preemption/IRQs are still
1664 * disabled avoiding further scheduler activity on it and we're
1665 * being very careful to re-start the picking loop.
1667 rq_unpin_lock(rq, rf);
1668 pull_dl_task(rq);
1669 rq_repin_lock(rq, rf);
1671 * pull_dl_task() can drop (and re-acquire) rq->lock; this
1672 * means a stop task can slip in, in which case we need to
1673 * re-start task selection.
1675 if (rq->stop && task_on_rq_queued(rq->stop))
1676 return RETRY_TASK;
1680 * When prev is DL, we may throttle it in put_prev_task().
1681 * So, we update time before we check for dl_nr_running.
1683 if (prev->sched_class == &dl_sched_class)
1684 update_curr_dl(rq);
1686 if (unlikely(!dl_rq->dl_nr_running))
1687 return NULL;
1689 put_prev_task(rq, prev);
1691 dl_se = pick_next_dl_entity(rq, dl_rq);
1692 BUG_ON(!dl_se);
1694 p = dl_task_of(dl_se);
1695 p->se.exec_start = rq_clock_task(rq);
1697 /* Running task will never be pushed. */
1698 dequeue_pushable_dl_task(rq, p);
1700 if (hrtick_enabled(rq))
1701 start_hrtick_dl(rq, p);
1703 queue_push_tasks(rq);
1705 return p;
1708 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1710 update_curr_dl(rq);
1712 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
1713 enqueue_pushable_dl_task(rq, p);
1716 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1718 update_curr_dl(rq);
1721 * Even when we have runtime, update_curr_dl() might have resulted in us
1722 * not being the leftmost task anymore. In that case NEED_RESCHED will
1723 * be set and schedule() will start a new hrtick for the next task.
1725 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
1726 is_leftmost(p, &rq->dl))
1727 start_hrtick_dl(rq, p);
1730 static void task_fork_dl(struct task_struct *p)
1733 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1734 * sched_fork()
1738 static void set_curr_task_dl(struct rq *rq)
1740 struct task_struct *p = rq->curr;
1742 p->se.exec_start = rq_clock_task(rq);
1744 /* You can't push away the running task */
1745 dequeue_pushable_dl_task(rq, p);
1748 #ifdef CONFIG_SMP
1750 /* Only try algorithms three times */
1751 #define DL_MAX_TRIES 3
1753 static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1755 if (!task_running(rq, p) &&
1756 cpumask_test_cpu(cpu, &p->cpus_allowed))
1757 return 1;
1758 return 0;
1762 * Return the earliest pushable rq's task, which is suitable to be executed
1763 * on the CPU, NULL otherwise:
1765 static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
1767 struct rb_node *next_node = rq->dl.pushable_dl_tasks_root.rb_leftmost;
1768 struct task_struct *p = NULL;
1770 if (!has_pushable_dl_tasks(rq))
1771 return NULL;
1773 next_node:
1774 if (next_node) {
1775 p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1777 if (pick_dl_task(rq, p, cpu))
1778 return p;
1780 next_node = rb_next(next_node);
1781 goto next_node;
1784 return NULL;
1787 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1789 static int find_later_rq(struct task_struct *task)
1791 struct sched_domain *sd;
1792 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
1793 int this_cpu = smp_processor_id();
1794 int cpu = task_cpu(task);
1796 /* Make sure the mask is initialized first */
1797 if (unlikely(!later_mask))
1798 return -1;
1800 if (task->nr_cpus_allowed == 1)
1801 return -1;
1804 * We have to consider system topology and task affinity
1805 * first, then we can look for a suitable cpu.
1807 if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
1808 return -1;
1811 * If we are here, some targets have been found, including
1812 * the most suitable which is, among the runqueues where the
1813 * current tasks have later deadlines than the task's one, the
1814 * rq with the latest possible one.
1816 * Now we check how well this matches with task's
1817 * affinity and system topology.
1819 * The last cpu where the task run is our first
1820 * guess, since it is most likely cache-hot there.
1822 if (cpumask_test_cpu(cpu, later_mask))
1823 return cpu;
1825 * Check if this_cpu is to be skipped (i.e., it is
1826 * not in the mask) or not.
1828 if (!cpumask_test_cpu(this_cpu, later_mask))
1829 this_cpu = -1;
1831 rcu_read_lock();
1832 for_each_domain(cpu, sd) {
1833 if (sd->flags & SD_WAKE_AFFINE) {
1834 int best_cpu;
1837 * If possible, preempting this_cpu is
1838 * cheaper than migrating.
1840 if (this_cpu != -1 &&
1841 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1842 rcu_read_unlock();
1843 return this_cpu;
1846 best_cpu = cpumask_first_and(later_mask,
1847 sched_domain_span(sd));
1849 * Last chance: if a cpu being in both later_mask
1850 * and current sd span is valid, that becomes our
1851 * choice. Of course, the latest possible cpu is
1852 * already under consideration through later_mask.
1854 if (best_cpu < nr_cpu_ids) {
1855 rcu_read_unlock();
1856 return best_cpu;
1860 rcu_read_unlock();
1863 * At this point, all our guesses failed, we just return
1864 * 'something', and let the caller sort the things out.
1866 if (this_cpu != -1)
1867 return this_cpu;
1869 cpu = cpumask_any(later_mask);
1870 if (cpu < nr_cpu_ids)
1871 return cpu;
1873 return -1;
1876 /* Locks the rq it finds */
1877 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1879 struct rq *later_rq = NULL;
1880 int tries;
1881 int cpu;
1883 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1884 cpu = find_later_rq(task);
1886 if ((cpu == -1) || (cpu == rq->cpu))
1887 break;
1889 later_rq = cpu_rq(cpu);
1891 if (later_rq->dl.dl_nr_running &&
1892 !dl_time_before(task->dl.deadline,
1893 later_rq->dl.earliest_dl.curr)) {
1895 * Target rq has tasks of equal or earlier deadline,
1896 * retrying does not release any lock and is unlikely
1897 * to yield a different result.
1899 later_rq = NULL;
1900 break;
1903 /* Retry if something changed. */
1904 if (double_lock_balance(rq, later_rq)) {
1905 if (unlikely(task_rq(task) != rq ||
1906 !cpumask_test_cpu(later_rq->cpu, &task->cpus_allowed) ||
1907 task_running(rq, task) ||
1908 !dl_task(task) ||
1909 !task_on_rq_queued(task))) {
1910 double_unlock_balance(rq, later_rq);
1911 later_rq = NULL;
1912 break;
1917 * If the rq we found has no -deadline task, or
1918 * its earliest one has a later deadline than our
1919 * task, the rq is a good one.
1921 if (!later_rq->dl.dl_nr_running ||
1922 dl_time_before(task->dl.deadline,
1923 later_rq->dl.earliest_dl.curr))
1924 break;
1926 /* Otherwise we try again. */
1927 double_unlock_balance(rq, later_rq);
1928 later_rq = NULL;
1931 return later_rq;
1934 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1936 struct task_struct *p;
1938 if (!has_pushable_dl_tasks(rq))
1939 return NULL;
1941 p = rb_entry(rq->dl.pushable_dl_tasks_root.rb_leftmost,
1942 struct task_struct, pushable_dl_tasks);
1944 BUG_ON(rq->cpu != task_cpu(p));
1945 BUG_ON(task_current(rq, p));
1946 BUG_ON(p->nr_cpus_allowed <= 1);
1948 BUG_ON(!task_on_rq_queued(p));
1949 BUG_ON(!dl_task(p));
1951 return p;
1955 * See if the non running -deadline tasks on this rq
1956 * can be sent to some other CPU where they can preempt
1957 * and start executing.
1959 static int push_dl_task(struct rq *rq)
1961 struct task_struct *next_task;
1962 struct rq *later_rq;
1963 int ret = 0;
1965 if (!rq->dl.overloaded)
1966 return 0;
1968 next_task = pick_next_pushable_dl_task(rq);
1969 if (!next_task)
1970 return 0;
1972 retry:
1973 if (unlikely(next_task == rq->curr)) {
1974 WARN_ON(1);
1975 return 0;
1979 * If next_task preempts rq->curr, and rq->curr
1980 * can move away, it makes sense to just reschedule
1981 * without going further in pushing next_task.
1983 if (dl_task(rq->curr) &&
1984 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
1985 rq->curr->nr_cpus_allowed > 1) {
1986 resched_curr(rq);
1987 return 0;
1990 /* We might release rq lock */
1991 get_task_struct(next_task);
1993 /* Will lock the rq it'll find */
1994 later_rq = find_lock_later_rq(next_task, rq);
1995 if (!later_rq) {
1996 struct task_struct *task;
1999 * We must check all this again, since
2000 * find_lock_later_rq releases rq->lock and it is
2001 * then possible that next_task has migrated.
2003 task = pick_next_pushable_dl_task(rq);
2004 if (task == next_task) {
2006 * The task is still there. We don't try
2007 * again, some other cpu will pull it when ready.
2009 goto out;
2012 if (!task)
2013 /* No more tasks */
2014 goto out;
2016 put_task_struct(next_task);
2017 next_task = task;
2018 goto retry;
2021 deactivate_task(rq, next_task, 0);
2022 sub_running_bw(next_task->dl.dl_bw, &rq->dl);
2023 sub_rq_bw(next_task->dl.dl_bw, &rq->dl);
2024 set_task_cpu(next_task, later_rq->cpu);
2025 add_rq_bw(next_task->dl.dl_bw, &later_rq->dl);
2026 add_running_bw(next_task->dl.dl_bw, &later_rq->dl);
2027 activate_task(later_rq, next_task, 0);
2028 ret = 1;
2030 resched_curr(later_rq);
2032 double_unlock_balance(rq, later_rq);
2034 out:
2035 put_task_struct(next_task);
2037 return ret;
2040 static void push_dl_tasks(struct rq *rq)
2042 /* push_dl_task() will return true if it moved a -deadline task */
2043 while (push_dl_task(rq))
2047 static void pull_dl_task(struct rq *this_rq)
2049 int this_cpu = this_rq->cpu, cpu;
2050 struct task_struct *p;
2051 bool resched = false;
2052 struct rq *src_rq;
2053 u64 dmin = LONG_MAX;
2055 if (likely(!dl_overloaded(this_rq)))
2056 return;
2059 * Match the barrier from dl_set_overloaded; this guarantees that if we
2060 * see overloaded we must also see the dlo_mask bit.
2062 smp_rmb();
2064 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2065 if (this_cpu == cpu)
2066 continue;
2068 src_rq = cpu_rq(cpu);
2071 * It looks racy, abd it is! However, as in sched_rt.c,
2072 * we are fine with this.
2074 if (this_rq->dl.dl_nr_running &&
2075 dl_time_before(this_rq->dl.earliest_dl.curr,
2076 src_rq->dl.earliest_dl.next))
2077 continue;
2079 /* Might drop this_rq->lock */
2080 double_lock_balance(this_rq, src_rq);
2083 * If there are no more pullable tasks on the
2084 * rq, we're done with it.
2086 if (src_rq->dl.dl_nr_running <= 1)
2087 goto skip;
2089 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
2092 * We found a task to be pulled if:
2093 * - it preempts our current (if there's one),
2094 * - it will preempt the last one we pulled (if any).
2096 if (p && dl_time_before(p->dl.deadline, dmin) &&
2097 (!this_rq->dl.dl_nr_running ||
2098 dl_time_before(p->dl.deadline,
2099 this_rq->dl.earliest_dl.curr))) {
2100 WARN_ON(p == src_rq->curr);
2101 WARN_ON(!task_on_rq_queued(p));
2104 * Then we pull iff p has actually an earlier
2105 * deadline than the current task of its runqueue.
2107 if (dl_time_before(p->dl.deadline,
2108 src_rq->curr->dl.deadline))
2109 goto skip;
2111 resched = true;
2113 deactivate_task(src_rq, p, 0);
2114 sub_running_bw(p->dl.dl_bw, &src_rq->dl);
2115 sub_rq_bw(p->dl.dl_bw, &src_rq->dl);
2116 set_task_cpu(p, this_cpu);
2117 add_rq_bw(p->dl.dl_bw, &this_rq->dl);
2118 add_running_bw(p->dl.dl_bw, &this_rq->dl);
2119 activate_task(this_rq, p, 0);
2120 dmin = p->dl.deadline;
2122 /* Is there any other task even earlier? */
2124 skip:
2125 double_unlock_balance(this_rq, src_rq);
2128 if (resched)
2129 resched_curr(this_rq);
2133 * Since the task is not running and a reschedule is not going to happen
2134 * anytime soon on its runqueue, we try pushing it away now.
2136 static void task_woken_dl(struct rq *rq, struct task_struct *p)
2138 if (!task_running(rq, p) &&
2139 !test_tsk_need_resched(rq->curr) &&
2140 p->nr_cpus_allowed > 1 &&
2141 dl_task(rq->curr) &&
2142 (rq->curr->nr_cpus_allowed < 2 ||
2143 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
2144 push_dl_tasks(rq);
2148 static void set_cpus_allowed_dl(struct task_struct *p,
2149 const struct cpumask *new_mask)
2151 struct root_domain *src_rd;
2152 struct rq *rq;
2154 BUG_ON(!dl_task(p));
2156 rq = task_rq(p);
2157 src_rd = rq->rd;
2159 * Migrating a SCHED_DEADLINE task between exclusive
2160 * cpusets (different root_domains) entails a bandwidth
2161 * update. We already made space for us in the destination
2162 * domain (see cpuset_can_attach()).
2164 if (!cpumask_intersects(src_rd->span, new_mask)) {
2165 struct dl_bw *src_dl_b;
2167 src_dl_b = dl_bw_of(cpu_of(rq));
2169 * We now free resources of the root_domain we are migrating
2170 * off. In the worst case, sched_setattr() may temporary fail
2171 * until we complete the update.
2173 raw_spin_lock(&src_dl_b->lock);
2174 __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
2175 raw_spin_unlock(&src_dl_b->lock);
2178 set_cpus_allowed_common(p, new_mask);
2181 /* Assumes rq->lock is held */
2182 static void rq_online_dl(struct rq *rq)
2184 if (rq->dl.overloaded)
2185 dl_set_overload(rq);
2187 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
2188 if (rq->dl.dl_nr_running > 0)
2189 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
2192 /* Assumes rq->lock is held */
2193 static void rq_offline_dl(struct rq *rq)
2195 if (rq->dl.overloaded)
2196 dl_clear_overload(rq);
2198 cpudl_clear(&rq->rd->cpudl, rq->cpu);
2199 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
2202 void __init init_sched_dl_class(void)
2204 unsigned int i;
2206 for_each_possible_cpu(i)
2207 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2208 GFP_KERNEL, cpu_to_node(i));
2211 #endif /* CONFIG_SMP */
2213 static void switched_from_dl(struct rq *rq, struct task_struct *p)
2216 * task_non_contending() can start the "inactive timer" (if the 0-lag
2217 * time is in the future). If the task switches back to dl before
2218 * the "inactive timer" fires, it can continue to consume its current
2219 * runtime using its current deadline. If it stays outside of
2220 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2221 * will reset the task parameters.
2223 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2224 task_non_contending(p);
2226 if (!task_on_rq_queued(p))
2227 sub_rq_bw(p->dl.dl_bw, &rq->dl);
2230 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2231 * at the 0-lag time, because the task could have been migrated
2232 * while SCHED_OTHER in the meanwhile.
2234 if (p->dl.dl_non_contending)
2235 p->dl.dl_non_contending = 0;
2238 * Since this might be the only -deadline task on the rq,
2239 * this is the right place to try to pull some other one
2240 * from an overloaded cpu, if any.
2242 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2243 return;
2245 queue_pull_task(rq);
2249 * When switching to -deadline, we may overload the rq, then
2250 * we try to push someone off, if possible.
2252 static void switched_to_dl(struct rq *rq, struct task_struct *p)
2254 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2255 put_task_struct(p);
2257 /* If p is not queued we will update its parameters at next wakeup. */
2258 if (!task_on_rq_queued(p)) {
2259 add_rq_bw(p->dl.dl_bw, &rq->dl);
2261 return;
2264 if (rq->curr != p) {
2265 #ifdef CONFIG_SMP
2266 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
2267 queue_push_tasks(rq);
2268 #endif
2269 if (dl_task(rq->curr))
2270 check_preempt_curr_dl(rq, p, 0);
2271 else
2272 resched_curr(rq);
2277 * If the scheduling parameters of a -deadline task changed,
2278 * a push or pull operation might be needed.
2280 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2281 int oldprio)
2283 if (task_on_rq_queued(p) || rq->curr == p) {
2284 #ifdef CONFIG_SMP
2286 * This might be too much, but unfortunately
2287 * we don't have the old deadline value, and
2288 * we can't argue if the task is increasing
2289 * or lowering its prio, so...
2291 if (!rq->dl.overloaded)
2292 queue_pull_task(rq);
2295 * If we now have a earlier deadline task than p,
2296 * then reschedule, provided p is still on this
2297 * runqueue.
2299 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
2300 resched_curr(rq);
2301 #else
2303 * Again, we don't know if p has a earlier
2304 * or later deadline, so let's blindly set a
2305 * (maybe not needed) rescheduling point.
2307 resched_curr(rq);
2308 #endif /* CONFIG_SMP */
2312 const struct sched_class dl_sched_class = {
2313 .next = &rt_sched_class,
2314 .enqueue_task = enqueue_task_dl,
2315 .dequeue_task = dequeue_task_dl,
2316 .yield_task = yield_task_dl,
2318 .check_preempt_curr = check_preempt_curr_dl,
2320 .pick_next_task = pick_next_task_dl,
2321 .put_prev_task = put_prev_task_dl,
2323 #ifdef CONFIG_SMP
2324 .select_task_rq = select_task_rq_dl,
2325 .migrate_task_rq = migrate_task_rq_dl,
2326 .set_cpus_allowed = set_cpus_allowed_dl,
2327 .rq_online = rq_online_dl,
2328 .rq_offline = rq_offline_dl,
2329 .task_woken = task_woken_dl,
2330 #endif
2332 .set_curr_task = set_curr_task_dl,
2333 .task_tick = task_tick_dl,
2334 .task_fork = task_fork_dl,
2336 .prio_changed = prio_changed_dl,
2337 .switched_from = switched_from_dl,
2338 .switched_to = switched_to_dl,
2340 .update_curr = update_curr_dl,
2343 int sched_dl_global_validate(void)
2345 u64 runtime = global_rt_runtime();
2346 u64 period = global_rt_period();
2347 u64 new_bw = to_ratio(period, runtime);
2348 struct dl_bw *dl_b;
2349 int cpu, ret = 0;
2350 unsigned long flags;
2353 * Here we want to check the bandwidth not being set to some
2354 * value smaller than the currently allocated bandwidth in
2355 * any of the root_domains.
2357 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
2358 * cycling on root_domains... Discussion on different/better
2359 * solutions is welcome!
2361 for_each_possible_cpu(cpu) {
2362 rcu_read_lock_sched();
2363 dl_b = dl_bw_of(cpu);
2365 raw_spin_lock_irqsave(&dl_b->lock, flags);
2366 if (new_bw < dl_b->total_bw)
2367 ret = -EBUSY;
2368 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2370 rcu_read_unlock_sched();
2372 if (ret)
2373 break;
2376 return ret;
2379 void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
2381 if (global_rt_runtime() == RUNTIME_INF) {
2382 dl_rq->bw_ratio = 1 << RATIO_SHIFT;
2383 dl_rq->extra_bw = 1 << BW_SHIFT;
2384 } else {
2385 dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
2386 global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
2387 dl_rq->extra_bw = to_ratio(global_rt_period(),
2388 global_rt_runtime());
2392 void sched_dl_do_global(void)
2394 u64 new_bw = -1;
2395 struct dl_bw *dl_b;
2396 int cpu;
2397 unsigned long flags;
2399 def_dl_bandwidth.dl_period = global_rt_period();
2400 def_dl_bandwidth.dl_runtime = global_rt_runtime();
2402 if (global_rt_runtime() != RUNTIME_INF)
2403 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
2406 * FIXME: As above...
2408 for_each_possible_cpu(cpu) {
2409 rcu_read_lock_sched();
2410 dl_b = dl_bw_of(cpu);
2412 raw_spin_lock_irqsave(&dl_b->lock, flags);
2413 dl_b->bw = new_bw;
2414 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2416 rcu_read_unlock_sched();
2417 init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
2422 * We must be sure that accepting a new task (or allowing changing the
2423 * parameters of an existing one) is consistent with the bandwidth
2424 * constraints. If yes, this function also accordingly updates the currently
2425 * allocated bandwidth to reflect the new situation.
2427 * This function is called while holding p's rq->lock.
2429 int sched_dl_overflow(struct task_struct *p, int policy,
2430 const struct sched_attr *attr)
2432 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
2433 u64 period = attr->sched_period ?: attr->sched_deadline;
2434 u64 runtime = attr->sched_runtime;
2435 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
2436 int cpus, err = -1;
2438 /* !deadline task may carry old deadline bandwidth */
2439 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
2440 return 0;
2443 * Either if a task, enters, leave, or stays -deadline but changes
2444 * its parameters, we may need to update accordingly the total
2445 * allocated bandwidth of the container.
2447 raw_spin_lock(&dl_b->lock);
2448 cpus = dl_bw_cpus(task_cpu(p));
2449 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2450 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2451 if (hrtimer_active(&p->dl.inactive_timer))
2452 __dl_sub(dl_b, p->dl.dl_bw, cpus);
2453 __dl_add(dl_b, new_bw, cpus);
2454 err = 0;
2455 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2456 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2458 * XXX this is slightly incorrect: when the task
2459 * utilization decreases, we should delay the total
2460 * utilization change until the task's 0-lag point.
2461 * But this would require to set the task's "inactive
2462 * timer" when the task is not inactive.
2464 __dl_sub(dl_b, p->dl.dl_bw, cpus);
2465 __dl_add(dl_b, new_bw, cpus);
2466 dl_change_utilization(p, new_bw);
2467 err = 0;
2468 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2470 * Do not decrease the total deadline utilization here,
2471 * switched_from_dl() will take care to do it at the correct
2472 * (0-lag) time.
2474 err = 0;
2476 raw_spin_unlock(&dl_b->lock);
2478 return err;
2482 * This function initializes the sched_dl_entity of a newly becoming
2483 * SCHED_DEADLINE task.
2485 * Only the static values are considered here, the actual runtime and the
2486 * absolute deadline will be properly calculated when the task is enqueued
2487 * for the first time with its new policy.
2489 void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
2491 struct sched_dl_entity *dl_se = &p->dl;
2493 dl_se->dl_runtime = attr->sched_runtime;
2494 dl_se->dl_deadline = attr->sched_deadline;
2495 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
2496 dl_se->flags = attr->sched_flags;
2497 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
2498 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
2501 void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
2503 struct sched_dl_entity *dl_se = &p->dl;
2505 attr->sched_priority = p->rt_priority;
2506 attr->sched_runtime = dl_se->dl_runtime;
2507 attr->sched_deadline = dl_se->dl_deadline;
2508 attr->sched_period = dl_se->dl_period;
2509 attr->sched_flags = dl_se->flags;
2513 * This function validates the new parameters of a -deadline task.
2514 * We ask for the deadline not being zero, and greater or equal
2515 * than the runtime, as well as the period of being zero or
2516 * greater than deadline. Furthermore, we have to be sure that
2517 * user parameters are above the internal resolution of 1us (we
2518 * check sched_runtime only since it is always the smaller one) and
2519 * below 2^63 ns (we have to check both sched_deadline and
2520 * sched_period, as the latter can be zero).
2522 bool __checkparam_dl(const struct sched_attr *attr)
2524 /* deadline != 0 */
2525 if (attr->sched_deadline == 0)
2526 return false;
2529 * Since we truncate DL_SCALE bits, make sure we're at least
2530 * that big.
2532 if (attr->sched_runtime < (1ULL << DL_SCALE))
2533 return false;
2536 * Since we use the MSB for wrap-around and sign issues, make
2537 * sure it's not set (mind that period can be equal to zero).
2539 if (attr->sched_deadline & (1ULL << 63) ||
2540 attr->sched_period & (1ULL << 63))
2541 return false;
2543 /* runtime <= deadline <= period (if period != 0) */
2544 if ((attr->sched_period != 0 &&
2545 attr->sched_period < attr->sched_deadline) ||
2546 attr->sched_deadline < attr->sched_runtime)
2547 return false;
2549 return true;
2553 * This function clears the sched_dl_entity static params.
2555 void __dl_clear_params(struct task_struct *p)
2557 struct sched_dl_entity *dl_se = &p->dl;
2559 dl_se->dl_runtime = 0;
2560 dl_se->dl_deadline = 0;
2561 dl_se->dl_period = 0;
2562 dl_se->flags = 0;
2563 dl_se->dl_bw = 0;
2564 dl_se->dl_density = 0;
2566 dl_se->dl_throttled = 0;
2567 dl_se->dl_yielded = 0;
2568 dl_se->dl_non_contending = 0;
2571 bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
2573 struct sched_dl_entity *dl_se = &p->dl;
2575 if (dl_se->dl_runtime != attr->sched_runtime ||
2576 dl_se->dl_deadline != attr->sched_deadline ||
2577 dl_se->dl_period != attr->sched_period ||
2578 dl_se->flags != attr->sched_flags)
2579 return true;
2581 return false;
2584 #ifdef CONFIG_SMP
2585 int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed)
2587 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
2588 cs_cpus_allowed);
2589 struct dl_bw *dl_b;
2590 bool overflow;
2591 int cpus, ret;
2592 unsigned long flags;
2594 rcu_read_lock_sched();
2595 dl_b = dl_bw_of(dest_cpu);
2596 raw_spin_lock_irqsave(&dl_b->lock, flags);
2597 cpus = dl_bw_cpus(dest_cpu);
2598 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
2599 if (overflow)
2600 ret = -EBUSY;
2601 else {
2603 * We reserve space for this task in the destination
2604 * root_domain, as we can't fail after this point.
2605 * We will free resources in the source root_domain
2606 * later on (see set_cpus_allowed_dl()).
2608 __dl_add(dl_b, p->dl.dl_bw, cpus);
2609 ret = 0;
2611 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2612 rcu_read_unlock_sched();
2613 return ret;
2616 int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
2617 const struct cpumask *trial)
2619 int ret = 1, trial_cpus;
2620 struct dl_bw *cur_dl_b;
2621 unsigned long flags;
2623 rcu_read_lock_sched();
2624 cur_dl_b = dl_bw_of(cpumask_any(cur));
2625 trial_cpus = cpumask_weight(trial);
2627 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
2628 if (cur_dl_b->bw != -1 &&
2629 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
2630 ret = 0;
2631 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
2632 rcu_read_unlock_sched();
2633 return ret;
2636 bool dl_cpu_busy(unsigned int cpu)
2638 unsigned long flags;
2639 struct dl_bw *dl_b;
2640 bool overflow;
2641 int cpus;
2643 rcu_read_lock_sched();
2644 dl_b = dl_bw_of(cpu);
2645 raw_spin_lock_irqsave(&dl_b->lock, flags);
2646 cpus = dl_bw_cpus(cpu);
2647 overflow = __dl_overflow(dl_b, cpus, 0, 0);
2648 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2649 rcu_read_unlock_sched();
2650 return overflow;
2652 #endif
2654 #ifdef CONFIG_SCHED_DEBUG
2655 extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2657 void print_dl_stats(struct seq_file *m, int cpu)
2659 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
2661 #endif /* CONFIG_SCHED_DEBUG */