1 // SPDX-License-Identifier: GPL-2.0
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>
19 #include <linux/cpuset.h>
22 * Default limits for DL period; on the top end we guard against small util
23 * tasks still getting ridiculously long effective runtimes, on the bottom end we
24 * guard against timer DoS.
26 static unsigned int sysctl_sched_dl_period_max = 1 << 22; /* ~4 seconds */
27 static unsigned int sysctl_sched_dl_period_min = 100; /* 100 us */
29 static struct ctl_table sched_dl_sysctls[] = {
31 .procname = "sched_deadline_period_max_us",
32 .data = &sysctl_sched_dl_period_max,
33 .maxlen = sizeof(unsigned int),
35 .proc_handler = proc_douintvec_minmax,
36 .extra1 = (void *)&sysctl_sched_dl_period_min,
39 .procname = "sched_deadline_period_min_us",
40 .data = &sysctl_sched_dl_period_min,
41 .maxlen = sizeof(unsigned int),
43 .proc_handler = proc_douintvec_minmax,
44 .extra2 = (void *)&sysctl_sched_dl_period_max,
48 static int __init sched_dl_sysctl_init(void)
50 register_sysctl_init("kernel", sched_dl_sysctls);
53 late_initcall(sched_dl_sysctl_init);
56 static bool dl_server(struct sched_dl_entity *dl_se)
58 return dl_se->dl_server;
61 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
63 BUG_ON(dl_server(dl_se));
64 return container_of(dl_se, struct task_struct, dl);
67 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
69 return container_of(dl_rq, struct rq, dl);
72 static inline struct rq *rq_of_dl_se(struct sched_dl_entity *dl_se)
74 struct rq *rq = dl_se->rq;
76 if (!dl_server(dl_se))
77 rq = task_rq(dl_task_of(dl_se));
82 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
84 return &rq_of_dl_se(dl_se)->dl;
87 static inline int on_dl_rq(struct sched_dl_entity *dl_se)
89 return !RB_EMPTY_NODE(&dl_se->rb_node);
92 #ifdef CONFIG_RT_MUTEXES
93 static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
98 static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
100 return pi_of(dl_se) != dl_se;
103 static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
108 static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
115 static inline struct dl_bw *dl_bw_of(int i)
117 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
118 "sched RCU must be held");
119 return &cpu_rq(i)->rd->dl_bw;
122 static inline int dl_bw_cpus(int i)
124 struct root_domain *rd = cpu_rq(i)->rd;
127 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
128 "sched RCU must be held");
130 if (cpumask_subset(rd->span, cpu_active_mask))
131 return cpumask_weight(rd->span);
135 for_each_cpu_and(i, rd->span, cpu_active_mask)
141 static inline unsigned long __dl_bw_capacity(const struct cpumask *mask)
143 unsigned long cap = 0;
146 for_each_cpu_and(i, mask, cpu_active_mask)
147 cap += arch_scale_cpu_capacity(i);
153 * XXX Fix: If 'rq->rd == def_root_domain' perform AC against capacity
154 * of the CPU the task is running on rather rd's \Sum CPU capacity.
156 static inline unsigned long dl_bw_capacity(int i)
158 if (!sched_asym_cpucap_active() &&
159 arch_scale_cpu_capacity(i) == SCHED_CAPACITY_SCALE) {
160 return dl_bw_cpus(i) << SCHED_CAPACITY_SHIFT;
162 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
163 "sched RCU must be held");
165 return __dl_bw_capacity(cpu_rq(i)->rd->span);
169 static inline bool dl_bw_visited(int cpu, u64 gen)
171 struct root_domain *rd = cpu_rq(cpu)->rd;
173 if (rd->visit_gen == gen)
181 void __dl_update(struct dl_bw *dl_b, s64 bw)
183 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
186 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
187 "sched RCU must be held");
188 for_each_cpu_and(i, rd->span, cpu_active_mask) {
189 struct rq *rq = cpu_rq(i);
191 rq->dl.extra_bw += bw;
195 static inline struct dl_bw *dl_bw_of(int i)
197 return &cpu_rq(i)->dl.dl_bw;
200 static inline int dl_bw_cpus(int i)
205 static inline unsigned long dl_bw_capacity(int i)
207 return SCHED_CAPACITY_SCALE;
210 static inline bool dl_bw_visited(int cpu, u64 gen)
216 void __dl_update(struct dl_bw *dl_b, s64 bw)
218 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
225 void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
227 dl_b->total_bw -= tsk_bw;
228 __dl_update(dl_b, (s32)tsk_bw / cpus);
232 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
234 dl_b->total_bw += tsk_bw;
235 __dl_update(dl_b, -((s32)tsk_bw / cpus));
239 __dl_overflow(struct dl_bw *dl_b, unsigned long cap, u64 old_bw, u64 new_bw)
241 return dl_b->bw != -1 &&
242 cap_scale(dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw;
246 void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
248 u64 old = dl_rq->running_bw;
250 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
251 dl_rq->running_bw += dl_bw;
252 SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
253 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
254 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
255 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
259 void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
261 u64 old = dl_rq->running_bw;
263 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
264 dl_rq->running_bw -= dl_bw;
265 SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
266 if (dl_rq->running_bw > old)
267 dl_rq->running_bw = 0;
268 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
269 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
273 void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
275 u64 old = dl_rq->this_bw;
277 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
278 dl_rq->this_bw += dl_bw;
279 SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */
283 void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
285 u64 old = dl_rq->this_bw;
287 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
288 dl_rq->this_bw -= dl_bw;
289 SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */
290 if (dl_rq->this_bw > old)
292 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
296 void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
298 if (!dl_entity_is_special(dl_se))
299 __add_rq_bw(dl_se->dl_bw, dl_rq);
303 void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
305 if (!dl_entity_is_special(dl_se))
306 __sub_rq_bw(dl_se->dl_bw, dl_rq);
310 void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
312 if (!dl_entity_is_special(dl_se))
313 __add_running_bw(dl_se->dl_bw, dl_rq);
317 void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
319 if (!dl_entity_is_special(dl_se))
320 __sub_running_bw(dl_se->dl_bw, dl_rq);
323 static void dl_change_utilization(struct task_struct *p, u64 new_bw)
327 WARN_ON_ONCE(p->dl.flags & SCHED_FLAG_SUGOV);
329 if (task_on_rq_queued(p))
333 if (p->dl.dl_non_contending) {
334 sub_running_bw(&p->dl, &rq->dl);
335 p->dl.dl_non_contending = 0;
337 * If the timer handler is currently running and the
338 * timer cannot be canceled, inactive_task_timer()
339 * will see that dl_not_contending is not set, and
340 * will not touch the rq's active utilization,
341 * so we are still safe.
343 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
346 __sub_rq_bw(p->dl.dl_bw, &rq->dl);
347 __add_rq_bw(new_bw, &rq->dl);
350 static void __dl_clear_params(struct sched_dl_entity *dl_se);
353 * The utilization of a task cannot be immediately removed from
354 * the rq active utilization (running_bw) when the task blocks.
355 * Instead, we have to wait for the so called "0-lag time".
357 * If a task blocks before the "0-lag time", a timer (the inactive
358 * timer) is armed, and running_bw is decreased when the timer
361 * If the task wakes up again before the inactive timer fires,
362 * the timer is canceled, whereas if the task wakes up after the
363 * inactive timer fired (and running_bw has been decreased) the
364 * task's utilization has to be added to running_bw again.
365 * A flag in the deadline scheduling entity (dl_non_contending)
366 * is used to avoid race conditions between the inactive timer handler
369 * The following diagram shows how running_bw is updated. A task is
370 * "ACTIVE" when its utilization contributes to running_bw; an
371 * "ACTIVE contending" task is in the TASK_RUNNING state, while an
372 * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
373 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
374 * time already passed, which does not contribute to running_bw anymore.
375 * +------------------+
377 * +------------------>+ contending |
378 * | add_running_bw | |
379 * | +----+------+------+
382 * +--------+-------+ | |
383 * | | t >= 0-lag | | wakeup
384 * | INACTIVE |<---------------+ |
385 * | | sub_running_bw | |
386 * +--------+-------+ | |
391 * | +----+------+------+
392 * | sub_running_bw | ACTIVE |
393 * +-------------------+ |
394 * inactive timer | non contending |
395 * fired +------------------+
397 * The task_non_contending() function is invoked when a task
398 * blocks, and checks if the 0-lag time already passed or
399 * not (in the first case, it directly updates running_bw;
400 * in the second case, it arms the inactive timer).
402 * The task_contending() function is invoked when a task wakes
403 * up, and checks if the task is still in the "ACTIVE non contending"
404 * state or not (in the second case, it updates running_bw).
406 static void task_non_contending(struct sched_dl_entity *dl_se)
408 struct hrtimer *timer = &dl_se->inactive_timer;
409 struct rq *rq = rq_of_dl_se(dl_se);
410 struct dl_rq *dl_rq = &rq->dl;
414 * If this is a non-deadline task that has been boosted,
417 if (dl_se->dl_runtime == 0)
420 if (dl_entity_is_special(dl_se))
423 WARN_ON(dl_se->dl_non_contending);
425 zerolag_time = dl_se->deadline -
426 div64_long((dl_se->runtime * dl_se->dl_period),
430 * Using relative times instead of the absolute "0-lag time"
431 * allows to simplify the code
433 zerolag_time -= rq_clock(rq);
436 * If the "0-lag time" already passed, decrease the active
437 * utilization now, instead of starting a timer
439 if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
440 if (dl_server(dl_se)) {
441 sub_running_bw(dl_se, dl_rq);
443 struct task_struct *p = dl_task_of(dl_se);
446 sub_running_bw(dl_se, dl_rq);
448 if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) {
449 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
451 if (READ_ONCE(p->__state) == TASK_DEAD)
452 sub_rq_bw(dl_se, &rq->dl);
453 raw_spin_lock(&dl_b->lock);
454 __dl_sub(dl_b, dl_se->dl_bw, dl_bw_cpus(task_cpu(p)));
455 raw_spin_unlock(&dl_b->lock);
456 __dl_clear_params(dl_se);
463 dl_se->dl_non_contending = 1;
464 if (!dl_server(dl_se))
465 get_task_struct(dl_task_of(dl_se));
467 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD);
470 static void task_contending(struct sched_dl_entity *dl_se, int flags)
472 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
475 * If this is a non-deadline task that has been boosted,
478 if (dl_se->dl_runtime == 0)
481 if (flags & ENQUEUE_MIGRATED)
482 add_rq_bw(dl_se, dl_rq);
484 if (dl_se->dl_non_contending) {
485 dl_se->dl_non_contending = 0;
487 * If the timer handler is currently running and the
488 * timer cannot be canceled, inactive_task_timer()
489 * will see that dl_not_contending is not set, and
490 * will not touch the rq's active utilization,
491 * so we are still safe.
493 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) {
494 if (!dl_server(dl_se))
495 put_task_struct(dl_task_of(dl_se));
499 * Since "dl_non_contending" is not set, the
500 * task's utilization has already been removed from
501 * active utilization (either when the task blocked,
502 * when the "inactive timer" fired).
505 add_running_bw(dl_se, dl_rq);
509 static inline int is_leftmost(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
511 return rb_first_cached(&dl_rq->root) == &dl_se->rb_node;
514 static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
516 void init_dl_bw(struct dl_bw *dl_b)
518 raw_spin_lock_init(&dl_b->lock);
519 if (global_rt_runtime() == RUNTIME_INF)
522 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
526 void init_dl_rq(struct dl_rq *dl_rq)
528 dl_rq->root = RB_ROOT_CACHED;
531 /* zero means no -deadline tasks */
532 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
534 dl_rq->overloaded = 0;
535 dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
537 init_dl_bw(&dl_rq->dl_bw);
540 dl_rq->running_bw = 0;
542 init_dl_rq_bw_ratio(dl_rq);
547 static inline int dl_overloaded(struct rq *rq)
549 return atomic_read(&rq->rd->dlo_count);
552 static inline void dl_set_overload(struct rq *rq)
557 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
559 * Must be visible before the overload count is
560 * set (as in sched_rt.c).
562 * Matched by the barrier in pull_dl_task().
565 atomic_inc(&rq->rd->dlo_count);
568 static inline void dl_clear_overload(struct rq *rq)
573 atomic_dec(&rq->rd->dlo_count);
574 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
577 #define __node_2_pdl(node) \
578 rb_entry((node), struct task_struct, pushable_dl_tasks)
580 static inline bool __pushable_less(struct rb_node *a, const struct rb_node *b)
582 return dl_entity_preempt(&__node_2_pdl(a)->dl, &__node_2_pdl(b)->dl);
585 static inline int has_pushable_dl_tasks(struct rq *rq)
587 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
591 * The list of pushable -deadline task is not a plist, like in
592 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
594 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
596 struct rb_node *leftmost;
598 WARN_ON_ONCE(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
600 leftmost = rb_add_cached(&p->pushable_dl_tasks,
601 &rq->dl.pushable_dl_tasks_root,
604 rq->dl.earliest_dl.next = p->dl.deadline;
606 if (!rq->dl.overloaded) {
608 rq->dl.overloaded = 1;
612 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
614 struct dl_rq *dl_rq = &rq->dl;
615 struct rb_root_cached *root = &dl_rq->pushable_dl_tasks_root;
616 struct rb_node *leftmost;
618 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
621 leftmost = rb_erase_cached(&p->pushable_dl_tasks, root);
623 dl_rq->earliest_dl.next = __node_2_pdl(leftmost)->dl.deadline;
625 RB_CLEAR_NODE(&p->pushable_dl_tasks);
627 if (!has_pushable_dl_tasks(rq) && rq->dl.overloaded) {
628 dl_clear_overload(rq);
629 rq->dl.overloaded = 0;
633 static int push_dl_task(struct rq *rq);
635 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
637 return rq->online && dl_task(prev);
640 static DEFINE_PER_CPU(struct balance_callback, dl_push_head);
641 static DEFINE_PER_CPU(struct balance_callback, dl_pull_head);
643 static void push_dl_tasks(struct rq *);
644 static void pull_dl_task(struct rq *);
646 static inline void deadline_queue_push_tasks(struct rq *rq)
648 if (!has_pushable_dl_tasks(rq))
651 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
654 static inline void deadline_queue_pull_task(struct rq *rq)
656 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
659 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
661 static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
663 struct rq *later_rq = NULL;
666 later_rq = find_lock_later_rq(p, rq);
671 * If we cannot preempt any rq, fall back to pick any
674 cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
675 if (cpu >= nr_cpu_ids) {
677 * Failed to find any suitable CPU.
678 * The task will never come back!
680 WARN_ON_ONCE(dl_bandwidth_enabled());
683 * If admission control is disabled we
684 * try a little harder to let the task
687 cpu = cpumask_any(cpu_active_mask);
689 later_rq = cpu_rq(cpu);
690 double_lock_balance(rq, later_rq);
693 if (p->dl.dl_non_contending || p->dl.dl_throttled) {
695 * Inactive timer is armed (or callback is running, but
696 * waiting for us to release rq locks). In any case, when it
697 * will fire (or continue), it will see running_bw of this
698 * task migrated to later_rq (and correctly handle it).
700 sub_running_bw(&p->dl, &rq->dl);
701 sub_rq_bw(&p->dl, &rq->dl);
703 add_rq_bw(&p->dl, &later_rq->dl);
704 add_running_bw(&p->dl, &later_rq->dl);
706 sub_rq_bw(&p->dl, &rq->dl);
707 add_rq_bw(&p->dl, &later_rq->dl);
711 * And we finally need to fixup root_domain(s) bandwidth accounting,
712 * since p is still hanging out in the old (now moved to default) root
715 dl_b = &rq->rd->dl_bw;
716 raw_spin_lock(&dl_b->lock);
717 __dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
718 raw_spin_unlock(&dl_b->lock);
720 dl_b = &later_rq->rd->dl_bw;
721 raw_spin_lock(&dl_b->lock);
722 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span));
723 raw_spin_unlock(&dl_b->lock);
725 set_task_cpu(p, later_rq->cpu);
726 double_unlock_balance(later_rq, rq);
734 void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
739 void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
744 void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
749 void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
753 static inline void deadline_queue_push_tasks(struct rq *rq)
757 static inline void deadline_queue_pull_task(struct rq *rq)
760 #endif /* CONFIG_SMP */
763 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags);
764 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
765 static void dequeue_dl_entity(struct sched_dl_entity *dl_se, int flags);
766 static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p, int flags);
768 static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se,
771 /* for non-boosted task, pi_of(dl_se) == dl_se */
772 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
773 dl_se->runtime = pi_of(dl_se)->dl_runtime;
777 * We are being explicitly informed that a new instance is starting,
778 * and this means that:
779 * - the absolute deadline of the entity has to be placed at
780 * current time + relative deadline;
781 * - the runtime of the entity has to be set to the maximum value.
783 * The capability of specifying such event is useful whenever a -deadline
784 * entity wants to (try to!) synchronize its behaviour with the scheduler's
785 * one, and to (try to!) reconcile itself with its own scheduling
788 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
790 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
791 struct rq *rq = rq_of_dl_rq(dl_rq);
793 WARN_ON(is_dl_boosted(dl_se));
794 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
797 * We are racing with the deadline timer. So, do nothing because
798 * the deadline timer handler will take care of properly recharging
799 * the runtime and postponing the deadline
801 if (dl_se->dl_throttled)
805 * We use the regular wall clock time to set deadlines in the
806 * future; in fact, we must consider execution overheads (time
807 * spent on hardirq context, etc.).
809 replenish_dl_new_period(dl_se, rq);
813 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
814 * possibility of a entity lasting more than what it declared, and thus
815 * exhausting its runtime.
817 * Here we are interested in making runtime overrun possible, but we do
818 * not want a entity which is misbehaving to affect the scheduling of all
820 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
821 * is used, in order to confine each entity within its own bandwidth.
823 * This function deals exactly with that, and ensures that when the runtime
824 * of a entity is replenished, its deadline is also postponed. That ensures
825 * the overrunning entity can't interfere with other entity in the system and
826 * can't make them miss their deadlines. Reasons why this kind of overruns
827 * could happen are, typically, a entity voluntarily trying to overcome its
828 * runtime, or it just underestimated it during sched_setattr().
830 static void replenish_dl_entity(struct sched_dl_entity *dl_se)
832 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
833 struct rq *rq = rq_of_dl_rq(dl_rq);
835 WARN_ON_ONCE(pi_of(dl_se)->dl_runtime <= 0);
838 * This could be the case for a !-dl task that is boosted.
839 * Just go with full inherited parameters.
841 if (dl_se->dl_deadline == 0)
842 replenish_dl_new_period(dl_se, rq);
844 if (dl_se->dl_yielded && dl_se->runtime > 0)
848 * We keep moving the deadline away until we get some
849 * available runtime for the entity. This ensures correct
850 * handling of situations where the runtime overrun is
853 while (dl_se->runtime <= 0) {
854 dl_se->deadline += pi_of(dl_se)->dl_period;
855 dl_se->runtime += pi_of(dl_se)->dl_runtime;
859 * At this point, the deadline really should be "in
860 * the future" with respect to rq->clock. If it's
861 * not, we are, for some reason, lagging too much!
862 * Anyway, after having warn userspace abut that,
863 * we still try to keep the things running by
864 * resetting the deadline and the budget of the
867 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
868 printk_deferred_once("sched: DL replenish lagged too much\n");
869 replenish_dl_new_period(dl_se, rq);
872 if (dl_se->dl_yielded)
873 dl_se->dl_yielded = 0;
874 if (dl_se->dl_throttled)
875 dl_se->dl_throttled = 0;
879 * Here we check if --at time t-- an entity (which is probably being
880 * [re]activated or, in general, enqueued) can use its remaining runtime
881 * and its current deadline _without_ exceeding the bandwidth it is
882 * assigned (function returns true if it can't). We are in fact applying
883 * one of the CBS rules: when a task wakes up, if the residual runtime
884 * over residual deadline fits within the allocated bandwidth, then we
885 * can keep the current (absolute) deadline and residual budget without
886 * disrupting the schedulability of the system. Otherwise, we should
887 * refill the runtime and set the deadline a period in the future,
888 * because keeping the current (absolute) deadline of the task would
889 * result in breaking guarantees promised to other tasks (refer to
890 * Documentation/scheduler/sched-deadline.rst for more information).
892 * This function returns true if:
894 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
896 * IOW we can't recycle current parameters.
898 * Notice that the bandwidth check is done against the deadline. For
899 * task with deadline equal to period this is the same of using
900 * dl_period instead of dl_deadline in the equation above.
902 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
907 * left and right are the two sides of the equation above,
908 * after a bit of shuffling to use multiplications instead
911 * Note that none of the time values involved in the two
912 * multiplications are absolute: dl_deadline and dl_runtime
913 * are the relative deadline and the maximum runtime of each
914 * instance, runtime is the runtime left for the last instance
915 * and (deadline - t), since t is rq->clock, is the time left
916 * to the (absolute) deadline. Even if overflowing the u64 type
917 * is very unlikely to occur in both cases, here we scale down
918 * as we want to avoid that risk at all. Scaling down by 10
919 * means that we reduce granularity to 1us. We are fine with it,
920 * since this is only a true/false check and, anyway, thinking
921 * of anything below microseconds resolution is actually fiction
922 * (but still we want to give the user that illusion >;).
924 left = (pi_of(dl_se)->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
925 right = ((dl_se->deadline - t) >> DL_SCALE) *
926 (pi_of(dl_se)->dl_runtime >> DL_SCALE);
928 return dl_time_before(right, left);
932 * Revised wakeup rule [1]: For self-suspending tasks, rather then
933 * re-initializing task's runtime and deadline, the revised wakeup
934 * rule adjusts the task's runtime to avoid the task to overrun its
937 * Reasoning: a task may overrun the density if:
938 * runtime / (deadline - t) > dl_runtime / dl_deadline
940 * Therefore, runtime can be adjusted to:
941 * runtime = (dl_runtime / dl_deadline) * (deadline - t)
943 * In such way that runtime will be equal to the maximum density
944 * the task can use without breaking any rule.
946 * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
947 * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
950 update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
952 u64 laxity = dl_se->deadline - rq_clock(rq);
955 * If the task has deadline < period, and the deadline is in the past,
956 * it should already be throttled before this check.
958 * See update_dl_entity() comments for further details.
960 WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
962 dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
966 * Regarding the deadline, a task with implicit deadline has a relative
967 * deadline == relative period. A task with constrained deadline has a
968 * relative deadline <= relative period.
970 * We support constrained deadline tasks. However, there are some restrictions
971 * applied only for tasks which do not have an implicit deadline. See
972 * update_dl_entity() to know more about such restrictions.
974 * The dl_is_implicit() returns true if the task has an implicit deadline.
976 static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
978 return dl_se->dl_deadline == dl_se->dl_period;
982 * When a deadline entity is placed in the runqueue, its runtime and deadline
983 * might need to be updated. This is done by a CBS wake up rule. There are two
984 * different rules: 1) the original CBS; and 2) the Revisited CBS.
986 * When the task is starting a new period, the Original CBS is used. In this
987 * case, the runtime is replenished and a new absolute deadline is set.
989 * When a task is queued before the begin of the next period, using the
990 * remaining runtime and deadline could make the entity to overflow, see
991 * dl_entity_overflow() to find more about runtime overflow. When such case
992 * is detected, the runtime and deadline need to be updated.
994 * If the task has an implicit deadline, i.e., deadline == period, the Original
995 * CBS is applied. the runtime is replenished and a new absolute deadline is
996 * set, as in the previous cases.
998 * However, the Original CBS does not work properly for tasks with
999 * deadline < period, which are said to have a constrained deadline. By
1000 * applying the Original CBS, a constrained deadline task would be able to run
1001 * runtime/deadline in a period. With deadline < period, the task would
1002 * overrun the runtime/period allowed bandwidth, breaking the admission test.
1004 * In order to prevent this misbehave, the Revisited CBS is used for
1005 * constrained deadline tasks when a runtime overflow is detected. In the
1006 * Revisited CBS, rather than replenishing & setting a new absolute deadline,
1007 * the remaining runtime of the task is reduced to avoid runtime overflow.
1008 * Please refer to the comments update_dl_revised_wakeup() function to find
1009 * more about the Revised CBS rule.
1011 static void update_dl_entity(struct sched_dl_entity *dl_se)
1013 struct rq *rq = rq_of_dl_se(dl_se);
1015 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
1016 dl_entity_overflow(dl_se, rq_clock(rq))) {
1018 if (unlikely(!dl_is_implicit(dl_se) &&
1019 !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1020 !is_dl_boosted(dl_se))) {
1021 update_dl_revised_wakeup(dl_se, rq);
1025 replenish_dl_new_period(dl_se, rq);
1029 static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
1031 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
1035 * If the entity depleted all its runtime, and if we want it to sleep
1036 * while waiting for some new execution time to become available, we
1037 * set the bandwidth replenishment timer to the replenishment instant
1038 * and try to activate it.
1040 * Notice that it is important for the caller to know if the timer
1041 * actually started or not (i.e., the replenishment instant is in
1042 * the future or in the past).
1044 static int start_dl_timer(struct sched_dl_entity *dl_se)
1046 struct hrtimer *timer = &dl_se->dl_timer;
1047 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1048 struct rq *rq = rq_of_dl_rq(dl_rq);
1052 lockdep_assert_rq_held(rq);
1055 * We want the timer to fire at the deadline, but considering
1056 * that it is actually coming from rq->clock and not from
1057 * hrtimer's time base reading.
1059 act = ns_to_ktime(dl_next_period(dl_se));
1060 now = hrtimer_cb_get_time(timer);
1061 delta = ktime_to_ns(now) - rq_clock(rq);
1062 act = ktime_add_ns(act, delta);
1065 * If the expiry time already passed, e.g., because the value
1066 * chosen as the deadline is too small, don't even try to
1067 * start the timer in the past!
1069 if (ktime_us_delta(act, now) < 0)
1073 * !enqueued will guarantee another callback; even if one is already in
1074 * progress. This ensures a balanced {get,put}_task_struct().
1076 * The race against __run_timer() clearing the enqueued state is
1077 * harmless because we're holding task_rq()->lock, therefore the timer
1078 * expiring after we've done the check will wait on its task_rq_lock()
1079 * and observe our state.
1081 if (!hrtimer_is_queued(timer)) {
1082 if (!dl_server(dl_se))
1083 get_task_struct(dl_task_of(dl_se));
1084 hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD);
1090 static void __push_dl_task(struct rq *rq, struct rq_flags *rf)
1094 * Queueing this task back might have overloaded rq, check if we need
1095 * to kick someone away.
1097 if (has_pushable_dl_tasks(rq)) {
1099 * Nothing relies on rq->lock after this, so its safe to drop
1102 rq_unpin_lock(rq, rf);
1104 rq_repin_lock(rq, rf);
1110 * This is the bandwidth enforcement timer callback. If here, we know
1111 * a task is not on its dl_rq, since the fact that the timer was running
1112 * means the task is throttled and needs a runtime replenishment.
1114 * However, what we actually do depends on the fact the task is active,
1115 * (it is on its rq) or has been removed from there by a call to
1116 * dequeue_task_dl(). In the former case we must issue the runtime
1117 * replenishment and add the task back to the dl_rq; in the latter, we just
1118 * do nothing but clearing dl_throttled, so that runtime and deadline
1119 * updating (and the queueing back to dl_rq) will be done by the
1120 * next call to enqueue_task_dl().
1122 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
1124 struct sched_dl_entity *dl_se = container_of(timer,
1125 struct sched_dl_entity,
1127 struct task_struct *p;
1131 if (dl_server(dl_se)) {
1132 struct rq *rq = rq_of_dl_se(dl_se);
1136 if (dl_se->dl_throttled) {
1138 update_rq_clock(rq);
1140 if (dl_se->server_has_tasks(dl_se)) {
1141 enqueue_dl_entity(dl_se, ENQUEUE_REPLENISH);
1143 __push_dl_task(rq, &rf);
1145 replenish_dl_entity(dl_se);
1151 return HRTIMER_NORESTART;
1154 p = dl_task_of(dl_se);
1155 rq = task_rq_lock(p, &rf);
1158 * The task might have changed its scheduling policy to something
1159 * different than SCHED_DEADLINE (through switched_from_dl()).
1165 * The task might have been boosted by someone else and might be in the
1166 * boosting/deboosting path, its not throttled.
1168 if (is_dl_boosted(dl_se))
1172 * Spurious timer due to start_dl_timer() race; or we already received
1173 * a replenishment from rt_mutex_setprio().
1175 if (!dl_se->dl_throttled)
1179 update_rq_clock(rq);
1182 * If the throttle happened during sched-out; like:
1189 * __dequeue_task_dl()
1192 * We can be both throttled and !queued. Replenish the counter
1193 * but do not enqueue -- wait for our wakeup to do that.
1195 if (!task_on_rq_queued(p)) {
1196 replenish_dl_entity(dl_se);
1201 if (unlikely(!rq->online)) {
1203 * If the runqueue is no longer available, migrate the
1204 * task elsewhere. This necessarily changes rq.
1206 lockdep_unpin_lock(__rq_lockp(rq), rf.cookie);
1207 rq = dl_task_offline_migration(rq, p);
1208 rf.cookie = lockdep_pin_lock(__rq_lockp(rq));
1209 update_rq_clock(rq);
1212 * Now that the task has been migrated to the new RQ and we
1213 * have that locked, proceed as normal and enqueue the task
1219 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
1220 if (dl_task(rq->curr))
1221 wakeup_preempt_dl(rq, p, 0);
1225 __push_dl_task(rq, &rf);
1228 task_rq_unlock(rq, p, &rf);
1231 * This can free the task_struct, including this hrtimer, do not touch
1232 * anything related to that after this.
1236 return HRTIMER_NORESTART;
1239 static void init_dl_task_timer(struct sched_dl_entity *dl_se)
1241 struct hrtimer *timer = &dl_se->dl_timer;
1243 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1244 timer->function = dl_task_timer;
1248 * During the activation, CBS checks if it can reuse the current task's
1249 * runtime and period. If the deadline of the task is in the past, CBS
1250 * cannot use the runtime, and so it replenishes the task. This rule
1251 * works fine for implicit deadline tasks (deadline == period), and the
1252 * CBS was designed for implicit deadline tasks. However, a task with
1253 * constrained deadline (deadline < period) might be awakened after the
1254 * deadline, but before the next period. In this case, replenishing the
1255 * task would allow it to run for runtime / deadline. As in this case
1256 * deadline < period, CBS enables a task to run for more than the
1257 * runtime / period. In a very loaded system, this can cause a domino
1258 * effect, making other tasks miss their deadlines.
1260 * To avoid this problem, in the activation of a constrained deadline
1261 * task after the deadline but before the next period, throttle the
1262 * task and set the replenishing timer to the begin of the next period,
1263 * unless it is boosted.
1265 static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
1267 struct rq *rq = rq_of_dl_se(dl_se);
1269 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1270 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
1271 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se)))
1273 dl_se->dl_throttled = 1;
1274 if (dl_se->runtime > 0)
1280 int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
1282 return (dl_se->runtime <= 0);
1286 * This function implements the GRUB accounting rule. According to the
1287 * GRUB reclaiming algorithm, the runtime is not decreased as "dq = -dt",
1288 * but as "dq = -(max{u, (Umax - Uinact - Uextra)} / Umax) dt",
1289 * where u is the utilization of the task, Umax is the maximum reclaimable
1290 * utilization, Uinact is the (per-runqueue) inactive utilization, computed
1291 * as the difference between the "total runqueue utilization" and the
1292 * "runqueue active utilization", and Uextra is the (per runqueue) extra
1293 * reclaimable utilization.
1294 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations multiplied
1295 * by 2^BW_SHIFT, the result has to be shifted right by BW_SHIFT.
1296 * Since rq->dl.bw_ratio contains 1 / Umax multiplied by 2^RATIO_SHIFT, dl_bw
1297 * is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
1298 * Since delta is a 64 bit variable, to have an overflow its value should be
1299 * larger than 2^(64 - 20 - 8), which is more than 64 seconds. So, overflow is
1300 * not an issue here.
1302 static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
1305 u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
1308 * Instead of computing max{u, (u_max - u_inact - u_extra)}, we
1309 * compare u_inact + u_extra with u_max - u, because u_inact + u_extra
1310 * can be larger than u_max. So, u_max - u_inact - u_extra would be
1311 * negative leading to wrong results.
1313 if (u_inact + rq->dl.extra_bw > rq->dl.max_bw - dl_se->dl_bw)
1314 u_act = dl_se->dl_bw;
1316 u_act = rq->dl.max_bw - u_inact - rq->dl.extra_bw;
1318 u_act = (u_act * rq->dl.bw_ratio) >> RATIO_SHIFT;
1319 return (delta * u_act) >> BW_SHIFT;
1323 update_stats_dequeue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
1325 static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec)
1327 s64 scaled_delta_exec;
1329 if (unlikely(delta_exec <= 0)) {
1330 if (unlikely(dl_se->dl_yielded))
1335 if (dl_entity_is_special(dl_se))
1339 * For tasks that participate in GRUB, we implement GRUB-PA: the
1340 * spare reclaimed bandwidth is used to clock down frequency.
1342 * For the others, we still need to scale reservation parameters
1343 * according to current frequency and CPU maximum capacity.
1345 if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) {
1346 scaled_delta_exec = grub_reclaim(delta_exec, rq, dl_se);
1348 int cpu = cpu_of(rq);
1349 unsigned long scale_freq = arch_scale_freq_capacity(cpu);
1350 unsigned long scale_cpu = arch_scale_cpu_capacity(cpu);
1352 scaled_delta_exec = cap_scale(delta_exec, scale_freq);
1353 scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu);
1356 dl_se->runtime -= scaled_delta_exec;
1359 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
1360 dl_se->dl_throttled = 1;
1362 /* If requested, inform the user about runtime overruns. */
1363 if (dl_runtime_exceeded(dl_se) &&
1364 (dl_se->flags & SCHED_FLAG_DL_OVERRUN))
1365 dl_se->dl_overrun = 1;
1367 dequeue_dl_entity(dl_se, 0);
1368 if (!dl_server(dl_se)) {
1369 update_stats_dequeue_dl(&rq->dl, dl_se, 0);
1370 dequeue_pushable_dl_task(rq, dl_task_of(dl_se));
1373 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se))) {
1374 if (dl_server(dl_se))
1375 enqueue_dl_entity(dl_se, ENQUEUE_REPLENISH);
1377 enqueue_task_dl(rq, dl_task_of(dl_se), ENQUEUE_REPLENISH);
1380 if (!is_leftmost(dl_se, &rq->dl))
1385 * Because -- for now -- we share the rt bandwidth, we need to
1386 * account our runtime there too, otherwise actual rt tasks
1387 * would be able to exceed the shared quota.
1389 * Account to the root rt group for now.
1391 * The solution we're working towards is having the RT groups scheduled
1392 * using deadline servers -- however there's a few nasties to figure
1393 * out before that can happen.
1395 if (rt_bandwidth_enabled()) {
1396 struct rt_rq *rt_rq = &rq->rt;
1398 raw_spin_lock(&rt_rq->rt_runtime_lock);
1400 * We'll let actual RT tasks worry about the overflow here, we
1401 * have our own CBS to keep us inline; only account when RT
1402 * bandwidth is relevant.
1404 if (sched_rt_bandwidth_account(rt_rq))
1405 rt_rq->rt_time += delta_exec;
1406 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1410 void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec)
1412 update_curr_dl_se(dl_se->rq, dl_se, delta_exec);
1415 void dl_server_start(struct sched_dl_entity *dl_se)
1417 if (!dl_server(dl_se)) {
1418 dl_se->dl_server = 1;
1419 setup_new_dl_entity(dl_se);
1421 enqueue_dl_entity(dl_se, ENQUEUE_WAKEUP);
1424 void dl_server_stop(struct sched_dl_entity *dl_se)
1426 dequeue_dl_entity(dl_se, DEQUEUE_SLEEP);
1429 void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
1430 dl_server_has_tasks_f has_tasks,
1431 dl_server_pick_f pick)
1434 dl_se->server_has_tasks = has_tasks;
1435 dl_se->server_pick = pick;
1439 * Update the current task's runtime statistics (provided it is still
1440 * a -deadline task and has not been removed from the dl_rq).
1442 static void update_curr_dl(struct rq *rq)
1444 struct task_struct *curr = rq->curr;
1445 struct sched_dl_entity *dl_se = &curr->dl;
1448 if (!dl_task(curr) || !on_dl_rq(dl_se))
1452 * Consumed budget is computed considering the time as
1453 * observed by schedulable tasks (excluding time spent
1454 * in hardirq context, etc.). Deadlines are instead
1455 * computed using hard walltime. This seems to be the more
1456 * natural solution, but the full ramifications of this
1457 * approach need further study.
1459 delta_exec = update_curr_common(rq);
1460 update_curr_dl_se(rq, dl_se, delta_exec);
1463 static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
1465 struct sched_dl_entity *dl_se = container_of(timer,
1466 struct sched_dl_entity,
1468 struct task_struct *p = NULL;
1472 if (!dl_server(dl_se)) {
1473 p = dl_task_of(dl_se);
1474 rq = task_rq_lock(p, &rf);
1481 update_rq_clock(rq);
1483 if (dl_server(dl_se))
1486 if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) {
1487 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1489 if (READ_ONCE(p->__state) == TASK_DEAD && dl_se->dl_non_contending) {
1490 sub_running_bw(&p->dl, dl_rq_of_se(&p->dl));
1491 sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl));
1492 dl_se->dl_non_contending = 0;
1495 raw_spin_lock(&dl_b->lock);
1496 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
1497 raw_spin_unlock(&dl_b->lock);
1498 __dl_clear_params(dl_se);
1504 if (dl_se->dl_non_contending == 0)
1507 sub_running_bw(dl_se, &rq->dl);
1508 dl_se->dl_non_contending = 0;
1511 if (!dl_server(dl_se)) {
1512 task_rq_unlock(rq, p, &rf);
1518 return HRTIMER_NORESTART;
1521 static void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1523 struct hrtimer *timer = &dl_se->inactive_timer;
1525 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1526 timer->function = inactive_task_timer;
1529 #define __node_2_dle(node) \
1530 rb_entry((node), struct sched_dl_entity, rb_node)
1534 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1536 struct rq *rq = rq_of_dl_rq(dl_rq);
1538 if (dl_rq->earliest_dl.curr == 0 ||
1539 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
1540 if (dl_rq->earliest_dl.curr == 0)
1541 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_HIGHER);
1542 dl_rq->earliest_dl.curr = deadline;
1543 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
1547 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1549 struct rq *rq = rq_of_dl_rq(dl_rq);
1552 * Since we may have removed our earliest (and/or next earliest)
1553 * task we must recompute them.
1555 if (!dl_rq->dl_nr_running) {
1556 dl_rq->earliest_dl.curr = 0;
1557 dl_rq->earliest_dl.next = 0;
1558 cpudl_clear(&rq->rd->cpudl, rq->cpu);
1559 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
1561 struct rb_node *leftmost = rb_first_cached(&dl_rq->root);
1562 struct sched_dl_entity *entry = __node_2_dle(leftmost);
1564 dl_rq->earliest_dl.curr = entry->deadline;
1565 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
1571 static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1572 static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1574 #endif /* CONFIG_SMP */
1577 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1579 u64 deadline = dl_se->deadline;
1581 dl_rq->dl_nr_running++;
1582 add_nr_running(rq_of_dl_rq(dl_rq), 1);
1584 inc_dl_deadline(dl_rq, deadline);
1588 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1590 WARN_ON(!dl_rq->dl_nr_running);
1591 dl_rq->dl_nr_running--;
1592 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
1594 dec_dl_deadline(dl_rq, dl_se->deadline);
1597 static inline bool __dl_less(struct rb_node *a, const struct rb_node *b)
1599 return dl_time_before(__node_2_dle(a)->deadline, __node_2_dle(b)->deadline);
1602 static inline struct sched_statistics *
1603 __schedstats_from_dl_se(struct sched_dl_entity *dl_se)
1605 return &dl_task_of(dl_se)->stats;
1609 update_stats_wait_start_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
1611 struct sched_statistics *stats;
1613 if (!schedstat_enabled())
1616 stats = __schedstats_from_dl_se(dl_se);
1617 __update_stats_wait_start(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
1621 update_stats_wait_end_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
1623 struct sched_statistics *stats;
1625 if (!schedstat_enabled())
1628 stats = __schedstats_from_dl_se(dl_se);
1629 __update_stats_wait_end(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
1633 update_stats_enqueue_sleeper_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
1635 struct sched_statistics *stats;
1637 if (!schedstat_enabled())
1640 stats = __schedstats_from_dl_se(dl_se);
1641 __update_stats_enqueue_sleeper(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
1645 update_stats_enqueue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
1648 if (!schedstat_enabled())
1651 if (flags & ENQUEUE_WAKEUP)
1652 update_stats_enqueue_sleeper_dl(dl_rq, dl_se);
1656 update_stats_dequeue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
1659 struct task_struct *p = dl_task_of(dl_se);
1661 if (!schedstat_enabled())
1664 if ((flags & DEQUEUE_SLEEP)) {
1667 state = READ_ONCE(p->__state);
1668 if (state & TASK_INTERRUPTIBLE)
1669 __schedstat_set(p->stats.sleep_start,
1670 rq_clock(rq_of_dl_rq(dl_rq)));
1672 if (state & TASK_UNINTERRUPTIBLE)
1673 __schedstat_set(p->stats.block_start,
1674 rq_clock(rq_of_dl_rq(dl_rq)));
1678 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1680 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1682 WARN_ON_ONCE(!RB_EMPTY_NODE(&dl_se->rb_node));
1684 rb_add_cached(&dl_se->rb_node, &dl_rq->root, __dl_less);
1686 inc_dl_tasks(dl_se, dl_rq);
1689 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1691 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1693 if (RB_EMPTY_NODE(&dl_se->rb_node))
1696 rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
1698 RB_CLEAR_NODE(&dl_se->rb_node);
1700 dec_dl_tasks(dl_se, dl_rq);
1704 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
1706 WARN_ON_ONCE(on_dl_rq(dl_se));
1708 update_stats_enqueue_dl(dl_rq_of_se(dl_se), dl_se, flags);
1711 * Check if a constrained deadline task was activated
1712 * after the deadline but before the next period.
1713 * If that is the case, the task will be throttled and
1714 * the replenishment timer will be set to the next period.
1716 if (!dl_se->dl_throttled && !dl_is_implicit(dl_se))
1717 dl_check_constrained_dl(dl_se);
1719 if (flags & (ENQUEUE_RESTORE|ENQUEUE_MIGRATING)) {
1720 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1722 add_rq_bw(dl_se, dl_rq);
1723 add_running_bw(dl_se, dl_rq);
1727 * If p is throttled, we do not enqueue it. In fact, if it exhausted
1728 * its budget it needs a replenishment and, since it now is on
1729 * its rq, the bandwidth timer callback (which clearly has not
1730 * run yet) will take care of this.
1731 * However, the active utilization does not depend on the fact
1732 * that the task is on the runqueue or not (but depends on the
1733 * task's state - in GRUB parlance, "inactive" vs "active contending").
1734 * In other words, even if a task is throttled its utilization must
1735 * be counted in the active utilization; hence, we need to call
1738 if (dl_se->dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
1739 if (flags & ENQUEUE_WAKEUP)
1740 task_contending(dl_se, flags);
1746 * If this is a wakeup or a new instance, the scheduling
1747 * parameters of the task might need updating. Otherwise,
1748 * we want a replenishment of its runtime.
1750 if (flags & ENQUEUE_WAKEUP) {
1751 task_contending(dl_se, flags);
1752 update_dl_entity(dl_se);
1753 } else if (flags & ENQUEUE_REPLENISH) {
1754 replenish_dl_entity(dl_se);
1755 } else if ((flags & ENQUEUE_RESTORE) &&
1756 dl_time_before(dl_se->deadline, rq_clock(rq_of_dl_se(dl_se)))) {
1757 setup_new_dl_entity(dl_se);
1760 __enqueue_dl_entity(dl_se);
1763 static void dequeue_dl_entity(struct sched_dl_entity *dl_se, int flags)
1765 __dequeue_dl_entity(dl_se);
1767 if (flags & (DEQUEUE_SAVE|DEQUEUE_MIGRATING)) {
1768 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1770 sub_running_bw(dl_se, dl_rq);
1771 sub_rq_bw(dl_se, dl_rq);
1775 * This check allows to start the inactive timer (or to immediately
1776 * decrease the active utilization, if needed) in two cases:
1777 * when the task blocks and when it is terminating
1778 * (p->state == TASK_DEAD). We can handle the two cases in the same
1779 * way, because from GRUB's point of view the same thing is happening
1780 * (the task moves from "active contending" to "active non contending"
1783 if (flags & DEQUEUE_SLEEP)
1784 task_non_contending(dl_se);
1787 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1789 if (is_dl_boosted(&p->dl)) {
1791 * Because of delays in the detection of the overrun of a
1792 * thread's runtime, it might be the case that a thread
1793 * goes to sleep in a rt mutex with negative runtime. As
1794 * a consequence, the thread will be throttled.
1796 * While waiting for the mutex, this thread can also be
1797 * boosted via PI, resulting in a thread that is throttled
1798 * and boosted at the same time.
1800 * In this case, the boost overrides the throttle.
1802 if (p->dl.dl_throttled) {
1804 * The replenish timer needs to be canceled. No
1805 * problem if it fires concurrently: boosted threads
1806 * are ignored in dl_task_timer().
1808 hrtimer_try_to_cancel(&p->dl.dl_timer);
1809 p->dl.dl_throttled = 0;
1811 } else if (!dl_prio(p->normal_prio)) {
1813 * Special case in which we have a !SCHED_DEADLINE task that is going
1814 * to be deboosted, but exceeds its runtime while doing so. No point in
1815 * replenishing it, as it's going to return back to its original
1816 * scheduling class after this. If it has been throttled, we need to
1817 * clear the flag, otherwise the task may wake up as throttled after
1818 * being boosted again with no means to replenish the runtime and clear
1821 p->dl.dl_throttled = 0;
1822 if (!(flags & ENQUEUE_REPLENISH))
1823 printk_deferred_once("sched: DL de-boosted task PID %d: REPLENISH flag missing\n",
1829 check_schedstat_required();
1830 update_stats_wait_start_dl(dl_rq_of_se(&p->dl), &p->dl);
1832 if (p->on_rq == TASK_ON_RQ_MIGRATING)
1833 flags |= ENQUEUE_MIGRATING;
1835 enqueue_dl_entity(&p->dl, flags);
1837 if (dl_server(&p->dl))
1840 if (!task_current(rq, p) && !p->dl.dl_throttled && p->nr_cpus_allowed > 1)
1841 enqueue_pushable_dl_task(rq, p);
1844 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1848 if (p->on_rq == TASK_ON_RQ_MIGRATING)
1849 flags |= DEQUEUE_MIGRATING;
1851 dequeue_dl_entity(&p->dl, flags);
1852 if (!p->dl.dl_throttled && !dl_server(&p->dl))
1853 dequeue_pushable_dl_task(rq, p);
1857 * Yield task semantic for -deadline tasks is:
1859 * get off from the CPU until our next instance, with
1860 * a new runtime. This is of little use now, since we
1861 * don't have a bandwidth reclaiming mechanism. Anyway,
1862 * bandwidth reclaiming is planned for the future, and
1863 * yield_task_dl will indicate that some spare budget
1864 * is available for other task instances to use it.
1866 static void yield_task_dl(struct rq *rq)
1869 * We make the task go to sleep until its current deadline by
1870 * forcing its runtime to zero. This way, update_curr_dl() stops
1871 * it and the bandwidth timer will wake it up and will give it
1872 * new scheduling parameters (thanks to dl_yielded=1).
1874 rq->curr->dl.dl_yielded = 1;
1876 update_rq_clock(rq);
1879 * Tell update_rq_clock() that we've just updated,
1880 * so we don't do microscopic update in schedule()
1881 * and double the fastpath cost.
1883 rq_clock_skip_update(rq);
1888 static inline bool dl_task_is_earliest_deadline(struct task_struct *p,
1891 return (!rq->dl.dl_nr_running ||
1892 dl_time_before(p->dl.deadline,
1893 rq->dl.earliest_dl.curr));
1896 static int find_later_rq(struct task_struct *task);
1899 select_task_rq_dl(struct task_struct *p, int cpu, int flags)
1901 struct task_struct *curr;
1905 if (!(flags & WF_TTWU))
1911 curr = READ_ONCE(rq->curr); /* unlocked access */
1914 * If we are dealing with a -deadline task, we must
1915 * decide where to wake it up.
1916 * If it has a later deadline and the current task
1917 * on this rq can't move (provided the waking task
1918 * can!) we prefer to send it somewhere else. On the
1919 * other hand, if it has a shorter deadline, we
1920 * try to make it stay here, it might be important.
1922 select_rq = unlikely(dl_task(curr)) &&
1923 (curr->nr_cpus_allowed < 2 ||
1924 !dl_entity_preempt(&p->dl, &curr->dl)) &&
1925 p->nr_cpus_allowed > 1;
1928 * Take the capacity of the CPU into account to
1929 * ensure it fits the requirement of the task.
1931 if (sched_asym_cpucap_active())
1932 select_rq |= !dl_task_fits_capacity(p, cpu);
1935 int target = find_later_rq(p);
1938 dl_task_is_earliest_deadline(p, cpu_rq(target)))
1947 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
1952 if (READ_ONCE(p->__state) != TASK_WAKING)
1957 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1958 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1959 * rq->lock is not... So, lock it
1962 if (p->dl.dl_non_contending) {
1963 update_rq_clock(rq);
1964 sub_running_bw(&p->dl, &rq->dl);
1965 p->dl.dl_non_contending = 0;
1967 * If the timer handler is currently running and the
1968 * timer cannot be canceled, inactive_task_timer()
1969 * will see that dl_not_contending is not set, and
1970 * will not touch the rq's active utilization,
1971 * so we are still safe.
1973 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1976 sub_rq_bw(&p->dl, &rq->dl);
1980 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1983 * Current can't be migrated, useless to reschedule,
1984 * let's hope p can move out.
1986 if (rq->curr->nr_cpus_allowed == 1 ||
1987 !cpudl_find(&rq->rd->cpudl, rq->curr, NULL))
1991 * p is migratable, so let's not schedule it and
1992 * see if it is pushed or pulled somewhere else.
1994 if (p->nr_cpus_allowed != 1 &&
1995 cpudl_find(&rq->rd->cpudl, p, NULL))
2001 static int balance_dl(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
2003 if (!on_dl_rq(&p->dl) && need_pull_dl_task(rq, p)) {
2005 * This is OK, because current is on_cpu, which avoids it being
2006 * picked for load-balance and preemption/IRQs are still
2007 * disabled avoiding further scheduler activity on it and we've
2008 * not yet started the picking loop.
2010 rq_unpin_lock(rq, rf);
2012 rq_repin_lock(rq, rf);
2015 return sched_stop_runnable(rq) || sched_dl_runnable(rq);
2017 #endif /* CONFIG_SMP */
2020 * Only called when both the current and waking task are -deadline
2023 static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p,
2026 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
2033 * In the unlikely case current and p have the same deadline
2034 * let us try to decide what's the best thing to do...
2036 if ((p->dl.deadline == rq->curr->dl.deadline) &&
2037 !test_tsk_need_resched(rq->curr))
2038 check_preempt_equal_dl(rq, p);
2039 #endif /* CONFIG_SMP */
2042 #ifdef CONFIG_SCHED_HRTICK
2043 static void start_hrtick_dl(struct rq *rq, struct sched_dl_entity *dl_se)
2045 hrtick_start(rq, dl_se->runtime);
2047 #else /* !CONFIG_SCHED_HRTICK */
2048 static void start_hrtick_dl(struct rq *rq, struct sched_dl_entity *dl_se)
2053 static void set_next_task_dl(struct rq *rq, struct task_struct *p, bool first)
2055 struct sched_dl_entity *dl_se = &p->dl;
2056 struct dl_rq *dl_rq = &rq->dl;
2058 p->se.exec_start = rq_clock_task(rq);
2059 if (on_dl_rq(&p->dl))
2060 update_stats_wait_end_dl(dl_rq, dl_se);
2062 /* You can't push away the running task */
2063 dequeue_pushable_dl_task(rq, p);
2068 if (rq->curr->sched_class != &dl_sched_class)
2069 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2071 deadline_queue_push_tasks(rq);
2074 static struct sched_dl_entity *pick_next_dl_entity(struct dl_rq *dl_rq)
2076 struct rb_node *left = rb_first_cached(&dl_rq->root);
2081 return __node_2_dle(left);
2084 static struct task_struct *pick_task_dl(struct rq *rq)
2086 struct sched_dl_entity *dl_se;
2087 struct dl_rq *dl_rq = &rq->dl;
2088 struct task_struct *p;
2091 if (!sched_dl_runnable(rq))
2094 dl_se = pick_next_dl_entity(dl_rq);
2095 WARN_ON_ONCE(!dl_se);
2097 if (dl_server(dl_se)) {
2098 p = dl_se->server_pick(dl_se);
2101 dl_se->dl_yielded = 1;
2102 update_curr_dl_se(rq, dl_se, 0);
2105 p->dl_server = dl_se;
2107 p = dl_task_of(dl_se);
2113 static struct task_struct *pick_next_task_dl(struct rq *rq)
2115 struct task_struct *p;
2117 p = pick_task_dl(rq);
2122 set_next_task_dl(rq, p, true);
2124 if (hrtick_enabled(rq))
2125 start_hrtick_dl(rq, &p->dl);
2130 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
2132 struct sched_dl_entity *dl_se = &p->dl;
2133 struct dl_rq *dl_rq = &rq->dl;
2135 if (on_dl_rq(&p->dl))
2136 update_stats_wait_start_dl(dl_rq, dl_se);
2140 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2141 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
2142 enqueue_pushable_dl_task(rq, p);
2146 * scheduler tick hitting a task of our scheduling class.
2148 * NOTE: This function can be called remotely by the tick offload that
2149 * goes along full dynticks. Therefore no local assumption can be made
2150 * and everything must be accessed through the @rq and @curr passed in
2153 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
2157 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2159 * Even when we have runtime, update_curr_dl() might have resulted in us
2160 * not being the leftmost task anymore. In that case NEED_RESCHED will
2161 * be set and schedule() will start a new hrtick for the next task.
2163 if (hrtick_enabled_dl(rq) && queued && p->dl.runtime > 0 &&
2164 is_leftmost(&p->dl, &rq->dl))
2165 start_hrtick_dl(rq, &p->dl);
2168 static void task_fork_dl(struct task_struct *p)
2171 * SCHED_DEADLINE tasks cannot fork and this is achieved through
2178 /* Only try algorithms three times */
2179 #define DL_MAX_TRIES 3
2181 static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
2183 if (!task_on_cpu(rq, p) &&
2184 cpumask_test_cpu(cpu, &p->cpus_mask))
2190 * Return the earliest pushable rq's task, which is suitable to be executed
2191 * on the CPU, NULL otherwise:
2193 static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
2195 struct task_struct *p = NULL;
2196 struct rb_node *next_node;
2198 if (!has_pushable_dl_tasks(rq))
2201 next_node = rb_first_cached(&rq->dl.pushable_dl_tasks_root);
2205 p = __node_2_pdl(next_node);
2207 if (pick_dl_task(rq, p, cpu))
2210 next_node = rb_next(next_node);
2217 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
2219 static int find_later_rq(struct task_struct *task)
2221 struct sched_domain *sd;
2222 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
2223 int this_cpu = smp_processor_id();
2224 int cpu = task_cpu(task);
2226 /* Make sure the mask is initialized first */
2227 if (unlikely(!later_mask))
2230 if (task->nr_cpus_allowed == 1)
2234 * We have to consider system topology and task affinity
2235 * first, then we can look for a suitable CPU.
2237 if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
2241 * If we are here, some targets have been found, including
2242 * the most suitable which is, among the runqueues where the
2243 * current tasks have later deadlines than the task's one, the
2244 * rq with the latest possible one.
2246 * Now we check how well this matches with task's
2247 * affinity and system topology.
2249 * The last CPU where the task run is our first
2250 * guess, since it is most likely cache-hot there.
2252 if (cpumask_test_cpu(cpu, later_mask))
2255 * Check if this_cpu is to be skipped (i.e., it is
2256 * not in the mask) or not.
2258 if (!cpumask_test_cpu(this_cpu, later_mask))
2262 for_each_domain(cpu, sd) {
2263 if (sd->flags & SD_WAKE_AFFINE) {
2267 * If possible, preempting this_cpu is
2268 * cheaper than migrating.
2270 if (this_cpu != -1 &&
2271 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
2276 best_cpu = cpumask_any_and_distribute(later_mask,
2277 sched_domain_span(sd));
2279 * Last chance: if a CPU being in both later_mask
2280 * and current sd span is valid, that becomes our
2281 * choice. Of course, the latest possible CPU is
2282 * already under consideration through later_mask.
2284 if (best_cpu < nr_cpu_ids) {
2293 * At this point, all our guesses failed, we just return
2294 * 'something', and let the caller sort the things out.
2299 cpu = cpumask_any_distribute(later_mask);
2300 if (cpu < nr_cpu_ids)
2306 /* Locks the rq it finds */
2307 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
2309 struct rq *later_rq = NULL;
2313 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
2314 cpu = find_later_rq(task);
2316 if ((cpu == -1) || (cpu == rq->cpu))
2319 later_rq = cpu_rq(cpu);
2321 if (!dl_task_is_earliest_deadline(task, later_rq)) {
2323 * Target rq has tasks of equal or earlier deadline,
2324 * retrying does not release any lock and is unlikely
2325 * to yield a different result.
2331 /* Retry if something changed. */
2332 if (double_lock_balance(rq, later_rq)) {
2333 if (unlikely(task_rq(task) != rq ||
2334 !cpumask_test_cpu(later_rq->cpu, &task->cpus_mask) ||
2335 task_on_cpu(rq, task) ||
2337 is_migration_disabled(task) ||
2338 !task_on_rq_queued(task))) {
2339 double_unlock_balance(rq, later_rq);
2346 * If the rq we found has no -deadline task, or
2347 * its earliest one has a later deadline than our
2348 * task, the rq is a good one.
2350 if (dl_task_is_earliest_deadline(task, later_rq))
2353 /* Otherwise we try again. */
2354 double_unlock_balance(rq, later_rq);
2361 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
2363 struct task_struct *p;
2365 if (!has_pushable_dl_tasks(rq))
2368 p = __node_2_pdl(rb_first_cached(&rq->dl.pushable_dl_tasks_root));
2370 WARN_ON_ONCE(rq->cpu != task_cpu(p));
2371 WARN_ON_ONCE(task_current(rq, p));
2372 WARN_ON_ONCE(p->nr_cpus_allowed <= 1);
2374 WARN_ON_ONCE(!task_on_rq_queued(p));
2375 WARN_ON_ONCE(!dl_task(p));
2381 * See if the non running -deadline tasks on this rq
2382 * can be sent to some other CPU where they can preempt
2383 * and start executing.
2385 static int push_dl_task(struct rq *rq)
2387 struct task_struct *next_task;
2388 struct rq *later_rq;
2391 next_task = pick_next_pushable_dl_task(rq);
2397 * If next_task preempts rq->curr, and rq->curr
2398 * can move away, it makes sense to just reschedule
2399 * without going further in pushing next_task.
2401 if (dl_task(rq->curr) &&
2402 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
2403 rq->curr->nr_cpus_allowed > 1) {
2408 if (is_migration_disabled(next_task))
2411 if (WARN_ON(next_task == rq->curr))
2414 /* We might release rq lock */
2415 get_task_struct(next_task);
2417 /* Will lock the rq it'll find */
2418 later_rq = find_lock_later_rq(next_task, rq);
2420 struct task_struct *task;
2423 * We must check all this again, since
2424 * find_lock_later_rq releases rq->lock and it is
2425 * then possible that next_task has migrated.
2427 task = pick_next_pushable_dl_task(rq);
2428 if (task == next_task) {
2430 * The task is still there. We don't try
2431 * again, some other CPU will pull it when ready.
2440 put_task_struct(next_task);
2445 deactivate_task(rq, next_task, 0);
2446 set_task_cpu(next_task, later_rq->cpu);
2447 activate_task(later_rq, next_task, 0);
2450 resched_curr(later_rq);
2452 double_unlock_balance(rq, later_rq);
2455 put_task_struct(next_task);
2460 static void push_dl_tasks(struct rq *rq)
2462 /* push_dl_task() will return true if it moved a -deadline task */
2463 while (push_dl_task(rq))
2467 static void pull_dl_task(struct rq *this_rq)
2469 int this_cpu = this_rq->cpu, cpu;
2470 struct task_struct *p, *push_task;
2471 bool resched = false;
2473 u64 dmin = LONG_MAX;
2475 if (likely(!dl_overloaded(this_rq)))
2479 * Match the barrier from dl_set_overloaded; this guarantees that if we
2480 * see overloaded we must also see the dlo_mask bit.
2484 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2485 if (this_cpu == cpu)
2488 src_rq = cpu_rq(cpu);
2491 * It looks racy, abd it is! However, as in sched_rt.c,
2492 * we are fine with this.
2494 if (this_rq->dl.dl_nr_running &&
2495 dl_time_before(this_rq->dl.earliest_dl.curr,
2496 src_rq->dl.earliest_dl.next))
2499 /* Might drop this_rq->lock */
2501 double_lock_balance(this_rq, src_rq);
2504 * If there are no more pullable tasks on the
2505 * rq, we're done with it.
2507 if (src_rq->dl.dl_nr_running <= 1)
2510 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
2513 * We found a task to be pulled if:
2514 * - it preempts our current (if there's one),
2515 * - it will preempt the last one we pulled (if any).
2517 if (p && dl_time_before(p->dl.deadline, dmin) &&
2518 dl_task_is_earliest_deadline(p, this_rq)) {
2519 WARN_ON(p == src_rq->curr);
2520 WARN_ON(!task_on_rq_queued(p));
2523 * Then we pull iff p has actually an earlier
2524 * deadline than the current task of its runqueue.
2526 if (dl_time_before(p->dl.deadline,
2527 src_rq->curr->dl.deadline))
2530 if (is_migration_disabled(p)) {
2531 push_task = get_push_task(src_rq);
2533 deactivate_task(src_rq, p, 0);
2534 set_task_cpu(p, this_cpu);
2535 activate_task(this_rq, p, 0);
2536 dmin = p->dl.deadline;
2540 /* Is there any other task even earlier? */
2543 double_unlock_balance(this_rq, src_rq);
2547 raw_spin_rq_unlock(this_rq);
2548 stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2549 push_task, &src_rq->push_work);
2551 raw_spin_rq_lock(this_rq);
2556 resched_curr(this_rq);
2560 * Since the task is not running and a reschedule is not going to happen
2561 * anytime soon on its runqueue, we try pushing it away now.
2563 static void task_woken_dl(struct rq *rq, struct task_struct *p)
2565 if (!task_on_cpu(rq, p) &&
2566 !test_tsk_need_resched(rq->curr) &&
2567 p->nr_cpus_allowed > 1 &&
2568 dl_task(rq->curr) &&
2569 (rq->curr->nr_cpus_allowed < 2 ||
2570 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
2575 static void set_cpus_allowed_dl(struct task_struct *p,
2576 struct affinity_context *ctx)
2578 struct root_domain *src_rd;
2581 WARN_ON_ONCE(!dl_task(p));
2586 * Migrating a SCHED_DEADLINE task between exclusive
2587 * cpusets (different root_domains) entails a bandwidth
2588 * update. We already made space for us in the destination
2589 * domain (see cpuset_can_attach()).
2591 if (!cpumask_intersects(src_rd->span, ctx->new_mask)) {
2592 struct dl_bw *src_dl_b;
2594 src_dl_b = dl_bw_of(cpu_of(rq));
2596 * We now free resources of the root_domain we are migrating
2597 * off. In the worst case, sched_setattr() may temporary fail
2598 * until we complete the update.
2600 raw_spin_lock(&src_dl_b->lock);
2601 __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
2602 raw_spin_unlock(&src_dl_b->lock);
2605 set_cpus_allowed_common(p, ctx);
2608 /* Assumes rq->lock is held */
2609 static void rq_online_dl(struct rq *rq)
2611 if (rq->dl.overloaded)
2612 dl_set_overload(rq);
2614 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
2615 if (rq->dl.dl_nr_running > 0)
2616 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
2619 /* Assumes rq->lock is held */
2620 static void rq_offline_dl(struct rq *rq)
2622 if (rq->dl.overloaded)
2623 dl_clear_overload(rq);
2625 cpudl_clear(&rq->rd->cpudl, rq->cpu);
2626 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
2629 void __init init_sched_dl_class(void)
2633 for_each_possible_cpu(i)
2634 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2635 GFP_KERNEL, cpu_to_node(i));
2638 void dl_add_task_root_domain(struct task_struct *p)
2644 raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
2646 raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags);
2650 rq = __task_rq_lock(p, &rf);
2652 dl_b = &rq->rd->dl_bw;
2653 raw_spin_lock(&dl_b->lock);
2655 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
2657 raw_spin_unlock(&dl_b->lock);
2659 task_rq_unlock(rq, p, &rf);
2662 void dl_clear_root_domain(struct root_domain *rd)
2664 unsigned long flags;
2666 raw_spin_lock_irqsave(&rd->dl_bw.lock, flags);
2667 rd->dl_bw.total_bw = 0;
2668 raw_spin_unlock_irqrestore(&rd->dl_bw.lock, flags);
2671 #endif /* CONFIG_SMP */
2673 static void switched_from_dl(struct rq *rq, struct task_struct *p)
2676 * task_non_contending() can start the "inactive timer" (if the 0-lag
2677 * time is in the future). If the task switches back to dl before
2678 * the "inactive timer" fires, it can continue to consume its current
2679 * runtime using its current deadline. If it stays outside of
2680 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2681 * will reset the task parameters.
2683 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2684 task_non_contending(&p->dl);
2687 * In case a task is setscheduled out from SCHED_DEADLINE we need to
2688 * keep track of that on its cpuset (for correct bandwidth tracking).
2692 if (!task_on_rq_queued(p)) {
2694 * Inactive timer is armed. However, p is leaving DEADLINE and
2695 * might migrate away from this rq while continuing to run on
2696 * some other class. We need to remove its contribution from
2697 * this rq running_bw now, or sub_rq_bw (below) will complain.
2699 if (p->dl.dl_non_contending)
2700 sub_running_bw(&p->dl, &rq->dl);
2701 sub_rq_bw(&p->dl, &rq->dl);
2705 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2706 * at the 0-lag time, because the task could have been migrated
2707 * while SCHED_OTHER in the meanwhile.
2709 if (p->dl.dl_non_contending)
2710 p->dl.dl_non_contending = 0;
2713 * Since this might be the only -deadline task on the rq,
2714 * this is the right place to try to pull some other one
2715 * from an overloaded CPU, if any.
2717 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2720 deadline_queue_pull_task(rq);
2724 * When switching to -deadline, we may overload the rq, then
2725 * we try to push someone off, if possible.
2727 static void switched_to_dl(struct rq *rq, struct task_struct *p)
2729 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2733 * In case a task is setscheduled to SCHED_DEADLINE we need to keep
2734 * track of that on its cpuset (for correct bandwidth tracking).
2738 /* If p is not queued we will update its parameters at next wakeup. */
2739 if (!task_on_rq_queued(p)) {
2740 add_rq_bw(&p->dl, &rq->dl);
2745 if (rq->curr != p) {
2747 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
2748 deadline_queue_push_tasks(rq);
2750 if (dl_task(rq->curr))
2751 wakeup_preempt_dl(rq, p, 0);
2755 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2760 * If the scheduling parameters of a -deadline task changed,
2761 * a push or pull operation might be needed.
2763 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2766 if (!task_on_rq_queued(p))
2771 * This might be too much, but unfortunately
2772 * we don't have the old deadline value, and
2773 * we can't argue if the task is increasing
2774 * or lowering its prio, so...
2776 if (!rq->dl.overloaded)
2777 deadline_queue_pull_task(rq);
2779 if (task_current(rq, p)) {
2781 * If we now have a earlier deadline task than p,
2782 * then reschedule, provided p is still on this
2785 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
2789 * Current may not be deadline in case p was throttled but we
2790 * have just replenished it (e.g. rt_mutex_setprio()).
2792 * Otherwise, if p was given an earlier deadline, reschedule.
2794 if (!dl_task(rq->curr) ||
2795 dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
2800 * We don't know if p has a earlier or later deadline, so let's blindly
2801 * set a (maybe not needed) rescheduling point.
2807 #ifdef CONFIG_SCHED_CORE
2808 static int task_is_throttled_dl(struct task_struct *p, int cpu)
2810 return p->dl.dl_throttled;
2814 DEFINE_SCHED_CLASS(dl) = {
2816 .enqueue_task = enqueue_task_dl,
2817 .dequeue_task = dequeue_task_dl,
2818 .yield_task = yield_task_dl,
2820 .wakeup_preempt = wakeup_preempt_dl,
2822 .pick_next_task = pick_next_task_dl,
2823 .put_prev_task = put_prev_task_dl,
2824 .set_next_task = set_next_task_dl,
2827 .balance = balance_dl,
2828 .pick_task = pick_task_dl,
2829 .select_task_rq = select_task_rq_dl,
2830 .migrate_task_rq = migrate_task_rq_dl,
2831 .set_cpus_allowed = set_cpus_allowed_dl,
2832 .rq_online = rq_online_dl,
2833 .rq_offline = rq_offline_dl,
2834 .task_woken = task_woken_dl,
2835 .find_lock_rq = find_lock_later_rq,
2838 .task_tick = task_tick_dl,
2839 .task_fork = task_fork_dl,
2841 .prio_changed = prio_changed_dl,
2842 .switched_from = switched_from_dl,
2843 .switched_to = switched_to_dl,
2845 .update_curr = update_curr_dl,
2846 #ifdef CONFIG_SCHED_CORE
2847 .task_is_throttled = task_is_throttled_dl,
2851 /* Used for dl_bw check and update, used under sched_rt_handler()::mutex */
2852 static u64 dl_generation;
2854 int sched_dl_global_validate(void)
2856 u64 runtime = global_rt_runtime();
2857 u64 period = global_rt_period();
2858 u64 new_bw = to_ratio(period, runtime);
2859 u64 gen = ++dl_generation;
2861 int cpu, cpus, ret = 0;
2862 unsigned long flags;
2865 * Here we want to check the bandwidth not being set to some
2866 * value smaller than the currently allocated bandwidth in
2867 * any of the root_domains.
2869 for_each_possible_cpu(cpu) {
2870 rcu_read_lock_sched();
2872 if (dl_bw_visited(cpu, gen))
2875 dl_b = dl_bw_of(cpu);
2876 cpus = dl_bw_cpus(cpu);
2878 raw_spin_lock_irqsave(&dl_b->lock, flags);
2879 if (new_bw * cpus < dl_b->total_bw)
2881 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2884 rcu_read_unlock_sched();
2893 static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
2895 if (global_rt_runtime() == RUNTIME_INF) {
2896 dl_rq->bw_ratio = 1 << RATIO_SHIFT;
2897 dl_rq->max_bw = dl_rq->extra_bw = 1 << BW_SHIFT;
2899 dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
2900 global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
2901 dl_rq->max_bw = dl_rq->extra_bw =
2902 to_ratio(global_rt_period(), global_rt_runtime());
2906 void sched_dl_do_global(void)
2909 u64 gen = ++dl_generation;
2912 unsigned long flags;
2914 if (global_rt_runtime() != RUNTIME_INF)
2915 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
2917 for_each_possible_cpu(cpu) {
2918 rcu_read_lock_sched();
2920 if (dl_bw_visited(cpu, gen)) {
2921 rcu_read_unlock_sched();
2925 dl_b = dl_bw_of(cpu);
2927 raw_spin_lock_irqsave(&dl_b->lock, flags);
2929 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2931 rcu_read_unlock_sched();
2932 init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
2937 * We must be sure that accepting a new task (or allowing changing the
2938 * parameters of an existing one) is consistent with the bandwidth
2939 * constraints. If yes, this function also accordingly updates the currently
2940 * allocated bandwidth to reflect the new situation.
2942 * This function is called while holding p's rq->lock.
2944 int sched_dl_overflow(struct task_struct *p, int policy,
2945 const struct sched_attr *attr)
2947 u64 period = attr->sched_period ?: attr->sched_deadline;
2948 u64 runtime = attr->sched_runtime;
2949 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
2950 int cpus, err = -1, cpu = task_cpu(p);
2951 struct dl_bw *dl_b = dl_bw_of(cpu);
2954 if (attr->sched_flags & SCHED_FLAG_SUGOV)
2957 /* !deadline task may carry old deadline bandwidth */
2958 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
2962 * Either if a task, enters, leave, or stays -deadline but changes
2963 * its parameters, we may need to update accordingly the total
2964 * allocated bandwidth of the container.
2966 raw_spin_lock(&dl_b->lock);
2967 cpus = dl_bw_cpus(cpu);
2968 cap = dl_bw_capacity(cpu);
2970 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2971 !__dl_overflow(dl_b, cap, 0, new_bw)) {
2972 if (hrtimer_active(&p->dl.inactive_timer))
2973 __dl_sub(dl_b, p->dl.dl_bw, cpus);
2974 __dl_add(dl_b, new_bw, cpus);
2976 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2977 !__dl_overflow(dl_b, cap, p->dl.dl_bw, new_bw)) {
2979 * XXX this is slightly incorrect: when the task
2980 * utilization decreases, we should delay the total
2981 * utilization change until the task's 0-lag point.
2982 * But this would require to set the task's "inactive
2983 * timer" when the task is not inactive.
2985 __dl_sub(dl_b, p->dl.dl_bw, cpus);
2986 __dl_add(dl_b, new_bw, cpus);
2987 dl_change_utilization(p, new_bw);
2989 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2991 * Do not decrease the total deadline utilization here,
2992 * switched_from_dl() will take care to do it at the correct
2997 raw_spin_unlock(&dl_b->lock);
3003 * This function initializes the sched_dl_entity of a newly becoming
3004 * SCHED_DEADLINE task.
3006 * Only the static values are considered here, the actual runtime and the
3007 * absolute deadline will be properly calculated when the task is enqueued
3008 * for the first time with its new policy.
3010 void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3012 struct sched_dl_entity *dl_se = &p->dl;
3014 dl_se->dl_runtime = attr->sched_runtime;
3015 dl_se->dl_deadline = attr->sched_deadline;
3016 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
3017 dl_se->flags = attr->sched_flags & SCHED_DL_FLAGS;
3018 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
3019 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
3022 void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
3024 struct sched_dl_entity *dl_se = &p->dl;
3026 attr->sched_priority = p->rt_priority;
3027 attr->sched_runtime = dl_se->dl_runtime;
3028 attr->sched_deadline = dl_se->dl_deadline;
3029 attr->sched_period = dl_se->dl_period;
3030 attr->sched_flags &= ~SCHED_DL_FLAGS;
3031 attr->sched_flags |= dl_se->flags;
3035 * This function validates the new parameters of a -deadline task.
3036 * We ask for the deadline not being zero, and greater or equal
3037 * than the runtime, as well as the period of being zero or
3038 * greater than deadline. Furthermore, we have to be sure that
3039 * user parameters are above the internal resolution of 1us (we
3040 * check sched_runtime only since it is always the smaller one) and
3041 * below 2^63 ns (we have to check both sched_deadline and
3042 * sched_period, as the latter can be zero).
3044 bool __checkparam_dl(const struct sched_attr *attr)
3046 u64 period, max, min;
3048 /* special dl tasks don't actually use any parameter */
3049 if (attr->sched_flags & SCHED_FLAG_SUGOV)
3053 if (attr->sched_deadline == 0)
3057 * Since we truncate DL_SCALE bits, make sure we're at least
3060 if (attr->sched_runtime < (1ULL << DL_SCALE))
3064 * Since we use the MSB for wrap-around and sign issues, make
3065 * sure it's not set (mind that period can be equal to zero).
3067 if (attr->sched_deadline & (1ULL << 63) ||
3068 attr->sched_period & (1ULL << 63))
3071 period = attr->sched_period;
3073 period = attr->sched_deadline;
3075 /* runtime <= deadline <= period (if period != 0) */
3076 if (period < attr->sched_deadline ||
3077 attr->sched_deadline < attr->sched_runtime)
3080 max = (u64)READ_ONCE(sysctl_sched_dl_period_max) * NSEC_PER_USEC;
3081 min = (u64)READ_ONCE(sysctl_sched_dl_period_min) * NSEC_PER_USEC;
3083 if (period < min || period > max)
3090 * This function clears the sched_dl_entity static params.
3092 static void __dl_clear_params(struct sched_dl_entity *dl_se)
3094 dl_se->dl_runtime = 0;
3095 dl_se->dl_deadline = 0;
3096 dl_se->dl_period = 0;
3099 dl_se->dl_density = 0;
3101 dl_se->dl_throttled = 0;
3102 dl_se->dl_yielded = 0;
3103 dl_se->dl_non_contending = 0;
3104 dl_se->dl_overrun = 0;
3105 dl_se->dl_server = 0;
3107 #ifdef CONFIG_RT_MUTEXES
3108 dl_se->pi_se = dl_se;
3112 void init_dl_entity(struct sched_dl_entity *dl_se)
3114 RB_CLEAR_NODE(&dl_se->rb_node);
3115 init_dl_task_timer(dl_se);
3116 init_dl_inactive_task_timer(dl_se);
3117 __dl_clear_params(dl_se);
3120 bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
3122 struct sched_dl_entity *dl_se = &p->dl;
3124 if (dl_se->dl_runtime != attr->sched_runtime ||
3125 dl_se->dl_deadline != attr->sched_deadline ||
3126 dl_se->dl_period != attr->sched_period ||
3127 dl_se->flags != (attr->sched_flags & SCHED_DL_FLAGS))
3134 int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
3135 const struct cpumask *trial)
3137 unsigned long flags, cap;
3138 struct dl_bw *cur_dl_b;
3141 rcu_read_lock_sched();
3142 cur_dl_b = dl_bw_of(cpumask_any(cur));
3143 cap = __dl_bw_capacity(trial);
3144 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
3145 if (__dl_overflow(cur_dl_b, cap, 0, 0))
3147 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
3148 rcu_read_unlock_sched();
3153 enum dl_bw_request {
3154 dl_bw_req_check_overflow = 0,
3159 static int dl_bw_manage(enum dl_bw_request req, int cpu, u64 dl_bw)
3161 unsigned long flags;
3165 rcu_read_lock_sched();
3166 dl_b = dl_bw_of(cpu);
3167 raw_spin_lock_irqsave(&dl_b->lock, flags);
3169 if (req == dl_bw_req_free) {
3170 __dl_sub(dl_b, dl_bw, dl_bw_cpus(cpu));
3172 unsigned long cap = dl_bw_capacity(cpu);
3174 overflow = __dl_overflow(dl_b, cap, 0, dl_bw);
3176 if (req == dl_bw_req_alloc && !overflow) {
3178 * We reserve space in the destination
3179 * root_domain, as we can't fail after this point.
3180 * We will free resources in the source root_domain
3181 * later on (see set_cpus_allowed_dl()).
3183 __dl_add(dl_b, dl_bw, dl_bw_cpus(cpu));
3187 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
3188 rcu_read_unlock_sched();
3190 return overflow ? -EBUSY : 0;
3193 int dl_bw_check_overflow(int cpu)
3195 return dl_bw_manage(dl_bw_req_check_overflow, cpu, 0);
3198 int dl_bw_alloc(int cpu, u64 dl_bw)
3200 return dl_bw_manage(dl_bw_req_alloc, cpu, dl_bw);
3203 void dl_bw_free(int cpu, u64 dl_bw)
3205 dl_bw_manage(dl_bw_req_free, cpu, dl_bw);
3209 #ifdef CONFIG_SCHED_DEBUG
3210 void print_dl_stats(struct seq_file *m, int cpu)
3212 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
3214 #endif /* CONFIG_SCHED_DEBUG */