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>
21 struct dl_bandwidth def_dl_bandwidth;
23 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
25 return container_of(dl_se, struct task_struct, dl);
28 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
30 return container_of(dl_rq, struct rq, dl);
33 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
35 struct task_struct *p = dl_task_of(dl_se);
36 struct rq *rq = task_rq(p);
41 static inline int on_dl_rq(struct sched_dl_entity *dl_se)
43 return !RB_EMPTY_NODE(&dl_se->rb_node);
46 #ifdef CONFIG_RT_MUTEXES
47 static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
52 static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
54 return pi_of(dl_se) != dl_se;
57 static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
62 static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
69 static inline struct dl_bw *dl_bw_of(int i)
71 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
72 "sched RCU must be held");
73 return &cpu_rq(i)->rd->dl_bw;
76 static inline int dl_bw_cpus(int i)
78 struct root_domain *rd = cpu_rq(i)->rd;
81 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
82 "sched RCU must be held");
84 if (cpumask_subset(rd->span, cpu_active_mask))
85 return cpumask_weight(rd->span);
89 for_each_cpu_and(i, rd->span, cpu_active_mask)
95 static inline unsigned long __dl_bw_capacity(int i)
97 struct root_domain *rd = cpu_rq(i)->rd;
98 unsigned long cap = 0;
100 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
101 "sched RCU must be held");
103 for_each_cpu_and(i, rd->span, cpu_active_mask)
104 cap += capacity_orig_of(i);
110 * XXX Fix: If 'rq->rd == def_root_domain' perform AC against capacity
111 * of the CPU the task is running on rather rd's \Sum CPU capacity.
113 static inline unsigned long dl_bw_capacity(int i)
115 if (!static_branch_unlikely(&sched_asym_cpucapacity) &&
116 capacity_orig_of(i) == SCHED_CAPACITY_SCALE) {
117 return dl_bw_cpus(i) << SCHED_CAPACITY_SHIFT;
119 return __dl_bw_capacity(i);
123 static inline struct dl_bw *dl_bw_of(int i)
125 return &cpu_rq(i)->dl.dl_bw;
128 static inline int dl_bw_cpus(int i)
133 static inline unsigned long dl_bw_capacity(int i)
135 return SCHED_CAPACITY_SCALE;
140 void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
142 u64 old = dl_rq->running_bw;
144 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
145 dl_rq->running_bw += dl_bw;
146 SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
147 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
148 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
149 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
153 void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
155 u64 old = dl_rq->running_bw;
157 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
158 dl_rq->running_bw -= dl_bw;
159 SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
160 if (dl_rq->running_bw > old)
161 dl_rq->running_bw = 0;
162 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
163 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
167 void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
169 u64 old = dl_rq->this_bw;
171 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
172 dl_rq->this_bw += dl_bw;
173 SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */
177 void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
179 u64 old = dl_rq->this_bw;
181 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
182 dl_rq->this_bw -= dl_bw;
183 SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */
184 if (dl_rq->this_bw > old)
186 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
190 void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
192 if (!dl_entity_is_special(dl_se))
193 __add_rq_bw(dl_se->dl_bw, dl_rq);
197 void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
199 if (!dl_entity_is_special(dl_se))
200 __sub_rq_bw(dl_se->dl_bw, dl_rq);
204 void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
206 if (!dl_entity_is_special(dl_se))
207 __add_running_bw(dl_se->dl_bw, dl_rq);
211 void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
213 if (!dl_entity_is_special(dl_se))
214 __sub_running_bw(dl_se->dl_bw, dl_rq);
217 static void dl_change_utilization(struct task_struct *p, u64 new_bw)
221 BUG_ON(p->dl.flags & SCHED_FLAG_SUGOV);
223 if (task_on_rq_queued(p))
227 if (p->dl.dl_non_contending) {
228 sub_running_bw(&p->dl, &rq->dl);
229 p->dl.dl_non_contending = 0;
231 * If the timer handler is currently running and the
232 * timer cannot be cancelled, inactive_task_timer()
233 * will see that dl_not_contending is not set, and
234 * will not touch the rq's active utilization,
235 * so we are still safe.
237 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
240 __sub_rq_bw(p->dl.dl_bw, &rq->dl);
241 __add_rq_bw(new_bw, &rq->dl);
245 * The utilization of a task cannot be immediately removed from
246 * the rq active utilization (running_bw) when the task blocks.
247 * Instead, we have to wait for the so called "0-lag time".
249 * If a task blocks before the "0-lag time", a timer (the inactive
250 * timer) is armed, and running_bw is decreased when the timer
253 * If the task wakes up again before the inactive timer fires,
254 * the timer is cancelled, whereas if the task wakes up after the
255 * inactive timer fired (and running_bw has been decreased) the
256 * task's utilization has to be added to running_bw again.
257 * A flag in the deadline scheduling entity (dl_non_contending)
258 * is used to avoid race conditions between the inactive timer handler
261 * The following diagram shows how running_bw is updated. A task is
262 * "ACTIVE" when its utilization contributes to running_bw; an
263 * "ACTIVE contending" task is in the TASK_RUNNING state, while an
264 * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
265 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
266 * time already passed, which does not contribute to running_bw anymore.
267 * +------------------+
269 * +------------------>+ contending |
270 * | add_running_bw | |
271 * | +----+------+------+
274 * +--------+-------+ | |
275 * | | t >= 0-lag | | wakeup
276 * | INACTIVE |<---------------+ |
277 * | | sub_running_bw | |
278 * +--------+-------+ | |
283 * | +----+------+------+
284 * | sub_running_bw | ACTIVE |
285 * +-------------------+ |
286 * inactive timer | non contending |
287 * fired +------------------+
289 * The task_non_contending() function is invoked when a task
290 * blocks, and checks if the 0-lag time already passed or
291 * not (in the first case, it directly updates running_bw;
292 * in the second case, it arms the inactive timer).
294 * The task_contending() function is invoked when a task wakes
295 * up, and checks if the task is still in the "ACTIVE non contending"
296 * state or not (in the second case, it updates running_bw).
298 static void task_non_contending(struct task_struct *p)
300 struct sched_dl_entity *dl_se = &p->dl;
301 struct hrtimer *timer = &dl_se->inactive_timer;
302 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
303 struct rq *rq = rq_of_dl_rq(dl_rq);
307 * If this is a non-deadline task that has been boosted,
310 if (dl_se->dl_runtime == 0)
313 if (dl_entity_is_special(dl_se))
316 WARN_ON(dl_se->dl_non_contending);
318 zerolag_time = dl_se->deadline -
319 div64_long((dl_se->runtime * dl_se->dl_period),
323 * Using relative times instead of the absolute "0-lag time"
324 * allows to simplify the code
326 zerolag_time -= rq_clock(rq);
329 * If the "0-lag time" already passed, decrease the active
330 * utilization now, instead of starting a timer
332 if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
334 sub_running_bw(dl_se, dl_rq);
335 if (!dl_task(p) || p->state == TASK_DEAD) {
336 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
338 if (p->state == TASK_DEAD)
339 sub_rq_bw(&p->dl, &rq->dl);
340 raw_spin_lock(&dl_b->lock);
341 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
342 __dl_clear_params(p);
343 raw_spin_unlock(&dl_b->lock);
349 dl_se->dl_non_contending = 1;
351 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD);
354 static void task_contending(struct sched_dl_entity *dl_se, int flags)
356 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
359 * If this is a non-deadline task that has been boosted,
362 if (dl_se->dl_runtime == 0)
365 if (flags & ENQUEUE_MIGRATED)
366 add_rq_bw(dl_se, dl_rq);
368 if (dl_se->dl_non_contending) {
369 dl_se->dl_non_contending = 0;
371 * If the timer handler is currently running and the
372 * timer cannot be cancelled, inactive_task_timer()
373 * will see that dl_not_contending is not set, and
374 * will not touch the rq's active utilization,
375 * so we are still safe.
377 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1)
378 put_task_struct(dl_task_of(dl_se));
381 * Since "dl_non_contending" is not set, the
382 * task's utilization has already been removed from
383 * active utilization (either when the task blocked,
384 * when the "inactive timer" fired).
387 add_running_bw(dl_se, dl_rq);
391 static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
393 struct sched_dl_entity *dl_se = &p->dl;
395 return dl_rq->root.rb_leftmost == &dl_se->rb_node;
398 static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
400 void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
402 raw_spin_lock_init(&dl_b->dl_runtime_lock);
403 dl_b->dl_period = period;
404 dl_b->dl_runtime = runtime;
407 void init_dl_bw(struct dl_bw *dl_b)
409 raw_spin_lock_init(&dl_b->lock);
410 raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
411 if (global_rt_runtime() == RUNTIME_INF)
414 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
415 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
419 void init_dl_rq(struct dl_rq *dl_rq)
421 dl_rq->root = RB_ROOT_CACHED;
424 /* zero means no -deadline tasks */
425 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
427 dl_rq->dl_nr_migratory = 0;
428 dl_rq->overloaded = 0;
429 dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
431 init_dl_bw(&dl_rq->dl_bw);
434 dl_rq->running_bw = 0;
436 init_dl_rq_bw_ratio(dl_rq);
441 static inline int dl_overloaded(struct rq *rq)
443 return atomic_read(&rq->rd->dlo_count);
446 static inline void dl_set_overload(struct rq *rq)
451 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
453 * Must be visible before the overload count is
454 * set (as in sched_rt.c).
456 * Matched by the barrier in pull_dl_task().
459 atomic_inc(&rq->rd->dlo_count);
462 static inline void dl_clear_overload(struct rq *rq)
467 atomic_dec(&rq->rd->dlo_count);
468 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
471 static void update_dl_migration(struct dl_rq *dl_rq)
473 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
474 if (!dl_rq->overloaded) {
475 dl_set_overload(rq_of_dl_rq(dl_rq));
476 dl_rq->overloaded = 1;
478 } else if (dl_rq->overloaded) {
479 dl_clear_overload(rq_of_dl_rq(dl_rq));
480 dl_rq->overloaded = 0;
484 static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
486 struct task_struct *p = dl_task_of(dl_se);
488 if (p->nr_cpus_allowed > 1)
489 dl_rq->dl_nr_migratory++;
491 update_dl_migration(dl_rq);
494 static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
496 struct task_struct *p = dl_task_of(dl_se);
498 if (p->nr_cpus_allowed > 1)
499 dl_rq->dl_nr_migratory--;
501 update_dl_migration(dl_rq);
505 * The list of pushable -deadline task is not a plist, like in
506 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
508 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
510 struct dl_rq *dl_rq = &rq->dl;
511 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_root.rb_node;
512 struct rb_node *parent = NULL;
513 struct task_struct *entry;
514 bool leftmost = true;
516 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
520 entry = rb_entry(parent, struct task_struct,
522 if (dl_entity_preempt(&p->dl, &entry->dl))
523 link = &parent->rb_left;
525 link = &parent->rb_right;
531 dl_rq->earliest_dl.next = p->dl.deadline;
533 rb_link_node(&p->pushable_dl_tasks, parent, link);
534 rb_insert_color_cached(&p->pushable_dl_tasks,
535 &dl_rq->pushable_dl_tasks_root, leftmost);
538 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
540 struct dl_rq *dl_rq = &rq->dl;
542 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
545 if (dl_rq->pushable_dl_tasks_root.rb_leftmost == &p->pushable_dl_tasks) {
546 struct rb_node *next_node;
548 next_node = rb_next(&p->pushable_dl_tasks);
550 dl_rq->earliest_dl.next = rb_entry(next_node,
551 struct task_struct, pushable_dl_tasks)->dl.deadline;
555 rb_erase_cached(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
556 RB_CLEAR_NODE(&p->pushable_dl_tasks);
559 static inline int has_pushable_dl_tasks(struct rq *rq)
561 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
564 static int push_dl_task(struct rq *rq);
566 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
568 return rq->online && dl_task(prev);
571 static DEFINE_PER_CPU(struct callback_head, dl_push_head);
572 static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
574 static void push_dl_tasks(struct rq *);
575 static void pull_dl_task(struct rq *);
577 static inline void deadline_queue_push_tasks(struct rq *rq)
579 if (!has_pushable_dl_tasks(rq))
582 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
585 static inline void deadline_queue_pull_task(struct rq *rq)
587 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
590 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
592 static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
594 struct rq *later_rq = NULL;
597 later_rq = find_lock_later_rq(p, rq);
602 * If we cannot preempt any rq, fall back to pick any
605 cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
606 if (cpu >= nr_cpu_ids) {
608 * Failed to find any suitable CPU.
609 * The task will never come back!
611 BUG_ON(dl_bandwidth_enabled());
614 * If admission control is disabled we
615 * try a little harder to let the task
618 cpu = cpumask_any(cpu_active_mask);
620 later_rq = cpu_rq(cpu);
621 double_lock_balance(rq, later_rq);
624 if (p->dl.dl_non_contending || p->dl.dl_throttled) {
626 * Inactive timer is armed (or callback is running, but
627 * waiting for us to release rq locks). In any case, when it
628 * will fire (or continue), it will see running_bw of this
629 * task migrated to later_rq (and correctly handle it).
631 sub_running_bw(&p->dl, &rq->dl);
632 sub_rq_bw(&p->dl, &rq->dl);
634 add_rq_bw(&p->dl, &later_rq->dl);
635 add_running_bw(&p->dl, &later_rq->dl);
637 sub_rq_bw(&p->dl, &rq->dl);
638 add_rq_bw(&p->dl, &later_rq->dl);
642 * And we finally need to fixup root_domain(s) bandwidth accounting,
643 * since p is still hanging out in the old (now moved to default) root
646 dl_b = &rq->rd->dl_bw;
647 raw_spin_lock(&dl_b->lock);
648 __dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
649 raw_spin_unlock(&dl_b->lock);
651 dl_b = &later_rq->rd->dl_bw;
652 raw_spin_lock(&dl_b->lock);
653 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span));
654 raw_spin_unlock(&dl_b->lock);
656 set_task_cpu(p, later_rq->cpu);
657 double_unlock_balance(later_rq, rq);
665 void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
670 void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
675 void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
680 void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
684 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
689 static inline void pull_dl_task(struct rq *rq)
693 static inline void deadline_queue_push_tasks(struct rq *rq)
697 static inline void deadline_queue_pull_task(struct rq *rq)
700 #endif /* CONFIG_SMP */
702 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
703 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
704 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, int flags);
707 * We are being explicitly informed that a new instance is starting,
708 * and this means that:
709 * - the absolute deadline of the entity has to be placed at
710 * current time + relative deadline;
711 * - the runtime of the entity has to be set to the maximum value.
713 * The capability of specifying such event is useful whenever a -deadline
714 * entity wants to (try to!) synchronize its behaviour with the scheduler's
715 * one, and to (try to!) reconcile itself with its own scheduling
718 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
720 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
721 struct rq *rq = rq_of_dl_rq(dl_rq);
723 WARN_ON(is_dl_boosted(dl_se));
724 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
727 * We are racing with the deadline timer. So, do nothing because
728 * the deadline timer handler will take care of properly recharging
729 * the runtime and postponing the deadline
731 if (dl_se->dl_throttled)
735 * We use the regular wall clock time to set deadlines in the
736 * future; in fact, we must consider execution overheads (time
737 * spent on hardirq context, etc.).
739 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
740 dl_se->runtime = dl_se->dl_runtime;
744 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
745 * possibility of a entity lasting more than what it declared, and thus
746 * exhausting its runtime.
748 * Here we are interested in making runtime overrun possible, but we do
749 * not want a entity which is misbehaving to affect the scheduling of all
751 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
752 * is used, in order to confine each entity within its own bandwidth.
754 * This function deals exactly with that, and ensures that when the runtime
755 * of a entity is replenished, its deadline is also postponed. That ensures
756 * the overrunning entity can't interfere with other entity in the system and
757 * can't make them miss their deadlines. Reasons why this kind of overruns
758 * could happen are, typically, a entity voluntarily trying to overcome its
759 * runtime, or it just underestimated it during sched_setattr().
761 static void replenish_dl_entity(struct sched_dl_entity *dl_se)
763 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
764 struct rq *rq = rq_of_dl_rq(dl_rq);
766 BUG_ON(pi_of(dl_se)->dl_runtime <= 0);
769 * This could be the case for a !-dl task that is boosted.
770 * Just go with full inherited parameters.
772 if (dl_se->dl_deadline == 0) {
773 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
774 dl_se->runtime = pi_of(dl_se)->dl_runtime;
777 if (dl_se->dl_yielded && dl_se->runtime > 0)
781 * We keep moving the deadline away until we get some
782 * available runtime for the entity. This ensures correct
783 * handling of situations where the runtime overrun is
786 while (dl_se->runtime <= 0) {
787 dl_se->deadline += pi_of(dl_se)->dl_period;
788 dl_se->runtime += pi_of(dl_se)->dl_runtime;
792 * At this point, the deadline really should be "in
793 * the future" with respect to rq->clock. If it's
794 * not, we are, for some reason, lagging too much!
795 * Anyway, after having warn userspace abut that,
796 * we still try to keep the things running by
797 * resetting the deadline and the budget of the
800 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
801 printk_deferred_once("sched: DL replenish lagged too much\n");
802 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
803 dl_se->runtime = pi_of(dl_se)->dl_runtime;
806 if (dl_se->dl_yielded)
807 dl_se->dl_yielded = 0;
808 if (dl_se->dl_throttled)
809 dl_se->dl_throttled = 0;
813 * Here we check if --at time t-- an entity (which is probably being
814 * [re]activated or, in general, enqueued) can use its remaining runtime
815 * and its current deadline _without_ exceeding the bandwidth it is
816 * assigned (function returns true if it can't). We are in fact applying
817 * one of the CBS rules: when a task wakes up, if the residual runtime
818 * over residual deadline fits within the allocated bandwidth, then we
819 * can keep the current (absolute) deadline and residual budget without
820 * disrupting the schedulability of the system. Otherwise, we should
821 * refill the runtime and set the deadline a period in the future,
822 * because keeping the current (absolute) deadline of the task would
823 * result in breaking guarantees promised to other tasks (refer to
824 * Documentation/scheduler/sched-deadline.rst for more information).
826 * This function returns true if:
828 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
830 * IOW we can't recycle current parameters.
832 * Notice that the bandwidth check is done against the deadline. For
833 * task with deadline equal to period this is the same of using
834 * dl_period instead of dl_deadline in the equation above.
836 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
841 * left and right are the two sides of the equation above,
842 * after a bit of shuffling to use multiplications instead
845 * Note that none of the time values involved in the two
846 * multiplications are absolute: dl_deadline and dl_runtime
847 * are the relative deadline and the maximum runtime of each
848 * instance, runtime is the runtime left for the last instance
849 * and (deadline - t), since t is rq->clock, is the time left
850 * to the (absolute) deadline. Even if overflowing the u64 type
851 * is very unlikely to occur in both cases, here we scale down
852 * as we want to avoid that risk at all. Scaling down by 10
853 * means that we reduce granularity to 1us. We are fine with it,
854 * since this is only a true/false check and, anyway, thinking
855 * of anything below microseconds resolution is actually fiction
856 * (but still we want to give the user that illusion >;).
858 left = (pi_of(dl_se)->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
859 right = ((dl_se->deadline - t) >> DL_SCALE) *
860 (pi_of(dl_se)->dl_runtime >> DL_SCALE);
862 return dl_time_before(right, left);
866 * Revised wakeup rule [1]: For self-suspending tasks, rather then
867 * re-initializing task's runtime and deadline, the revised wakeup
868 * rule adjusts the task's runtime to avoid the task to overrun its
871 * Reasoning: a task may overrun the density if:
872 * runtime / (deadline - t) > dl_runtime / dl_deadline
874 * Therefore, runtime can be adjusted to:
875 * runtime = (dl_runtime / dl_deadline) * (deadline - t)
877 * In such way that runtime will be equal to the maximum density
878 * the task can use without breaking any rule.
880 * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
881 * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
884 update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
886 u64 laxity = dl_se->deadline - rq_clock(rq);
889 * If the task has deadline < period, and the deadline is in the past,
890 * it should already be throttled before this check.
892 * See update_dl_entity() comments for further details.
894 WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
896 dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
900 * Regarding the deadline, a task with implicit deadline has a relative
901 * deadline == relative period. A task with constrained deadline has a
902 * relative deadline <= relative period.
904 * We support constrained deadline tasks. However, there are some restrictions
905 * applied only for tasks which do not have an implicit deadline. See
906 * update_dl_entity() to know more about such restrictions.
908 * The dl_is_implicit() returns true if the task has an implicit deadline.
910 static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
912 return dl_se->dl_deadline == dl_se->dl_period;
916 * When a deadline entity is placed in the runqueue, its runtime and deadline
917 * might need to be updated. This is done by a CBS wake up rule. There are two
918 * different rules: 1) the original CBS; and 2) the Revisited CBS.
920 * When the task is starting a new period, the Original CBS is used. In this
921 * case, the runtime is replenished and a new absolute deadline is set.
923 * When a task is queued before the begin of the next period, using the
924 * remaining runtime and deadline could make the entity to overflow, see
925 * dl_entity_overflow() to find more about runtime overflow. When such case
926 * is detected, the runtime and deadline need to be updated.
928 * If the task has an implicit deadline, i.e., deadline == period, the Original
929 * CBS is applied. the runtime is replenished and a new absolute deadline is
930 * set, as in the previous cases.
932 * However, the Original CBS does not work properly for tasks with
933 * deadline < period, which are said to have a constrained deadline. By
934 * applying the Original CBS, a constrained deadline task would be able to run
935 * runtime/deadline in a period. With deadline < period, the task would
936 * overrun the runtime/period allowed bandwidth, breaking the admission test.
938 * In order to prevent this misbehave, the Revisited CBS is used for
939 * constrained deadline tasks when a runtime overflow is detected. In the
940 * Revisited CBS, rather than replenishing & setting a new absolute deadline,
941 * the remaining runtime of the task is reduced to avoid runtime overflow.
942 * Please refer to the comments update_dl_revised_wakeup() function to find
943 * more about the Revised CBS rule.
945 static void update_dl_entity(struct sched_dl_entity *dl_se)
947 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
948 struct rq *rq = rq_of_dl_rq(dl_rq);
950 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
951 dl_entity_overflow(dl_se, rq_clock(rq))) {
953 if (unlikely(!dl_is_implicit(dl_se) &&
954 !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
955 !is_dl_boosted(dl_se))) {
956 update_dl_revised_wakeup(dl_se, rq);
960 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
961 dl_se->runtime = pi_of(dl_se)->dl_runtime;
965 static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
967 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
971 * If the entity depleted all its runtime, and if we want it to sleep
972 * while waiting for some new execution time to become available, we
973 * set the bandwidth replenishment timer to the replenishment instant
974 * and try to activate it.
976 * Notice that it is important for the caller to know if the timer
977 * actually started or not (i.e., the replenishment instant is in
978 * the future or in the past).
980 static int start_dl_timer(struct task_struct *p)
982 struct sched_dl_entity *dl_se = &p->dl;
983 struct hrtimer *timer = &dl_se->dl_timer;
984 struct rq *rq = task_rq(p);
988 lockdep_assert_held(&rq->lock);
991 * We want the timer to fire at the deadline, but considering
992 * that it is actually coming from rq->clock and not from
993 * hrtimer's time base reading.
995 act = ns_to_ktime(dl_next_period(dl_se));
996 now = hrtimer_cb_get_time(timer);
997 delta = ktime_to_ns(now) - rq_clock(rq);
998 act = ktime_add_ns(act, delta);
1001 * If the expiry time already passed, e.g., because the value
1002 * chosen as the deadline is too small, don't even try to
1003 * start the timer in the past!
1005 if (ktime_us_delta(act, now) < 0)
1009 * !enqueued will guarantee another callback; even if one is already in
1010 * progress. This ensures a balanced {get,put}_task_struct().
1012 * The race against __run_timer() clearing the enqueued state is
1013 * harmless because we're holding task_rq()->lock, therefore the timer
1014 * expiring after we've done the check will wait on its task_rq_lock()
1015 * and observe our state.
1017 if (!hrtimer_is_queued(timer)) {
1019 hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD);
1026 * This is the bandwidth enforcement timer callback. If here, we know
1027 * a task is not on its dl_rq, since the fact that the timer was running
1028 * means the task is throttled and needs a runtime replenishment.
1030 * However, what we actually do depends on the fact the task is active,
1031 * (it is on its rq) or has been removed from there by a call to
1032 * dequeue_task_dl(). In the former case we must issue the runtime
1033 * replenishment and add the task back to the dl_rq; in the latter, we just
1034 * do nothing but clearing dl_throttled, so that runtime and deadline
1035 * updating (and the queueing back to dl_rq) will be done by the
1036 * next call to enqueue_task_dl().
1038 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
1040 struct sched_dl_entity *dl_se = container_of(timer,
1041 struct sched_dl_entity,
1043 struct task_struct *p = dl_task_of(dl_se);
1047 rq = task_rq_lock(p, &rf);
1050 * The task might have changed its scheduling policy to something
1051 * different than SCHED_DEADLINE (through switched_from_dl()).
1057 * The task might have been boosted by someone else and might be in the
1058 * boosting/deboosting path, its not throttled.
1060 if (is_dl_boosted(dl_se))
1064 * Spurious timer due to start_dl_timer() race; or we already received
1065 * a replenishment from rt_mutex_setprio().
1067 if (!dl_se->dl_throttled)
1071 update_rq_clock(rq);
1074 * If the throttle happened during sched-out; like:
1081 * __dequeue_task_dl()
1084 * We can be both throttled and !queued. Replenish the counter
1085 * but do not enqueue -- wait for our wakeup to do that.
1087 if (!task_on_rq_queued(p)) {
1088 replenish_dl_entity(dl_se);
1093 if (unlikely(!rq->online)) {
1095 * If the runqueue is no longer available, migrate the
1096 * task elsewhere. This necessarily changes rq.
1098 lockdep_unpin_lock(&rq->lock, rf.cookie);
1099 rq = dl_task_offline_migration(rq, p);
1100 rf.cookie = lockdep_pin_lock(&rq->lock);
1101 update_rq_clock(rq);
1104 * Now that the task has been migrated to the new RQ and we
1105 * have that locked, proceed as normal and enqueue the task
1111 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
1112 if (dl_task(rq->curr))
1113 check_preempt_curr_dl(rq, p, 0);
1119 * Queueing this task back might have overloaded rq, check if we need
1120 * to kick someone away.
1122 if (has_pushable_dl_tasks(rq)) {
1124 * Nothing relies on rq->lock after this, so its safe to drop
1127 rq_unpin_lock(rq, &rf);
1129 rq_repin_lock(rq, &rf);
1134 task_rq_unlock(rq, p, &rf);
1137 * This can free the task_struct, including this hrtimer, do not touch
1138 * anything related to that after this.
1142 return HRTIMER_NORESTART;
1145 void init_dl_task_timer(struct sched_dl_entity *dl_se)
1147 struct hrtimer *timer = &dl_se->dl_timer;
1149 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1150 timer->function = dl_task_timer;
1154 * During the activation, CBS checks if it can reuse the current task's
1155 * runtime and period. If the deadline of the task is in the past, CBS
1156 * cannot use the runtime, and so it replenishes the task. This rule
1157 * works fine for implicit deadline tasks (deadline == period), and the
1158 * CBS was designed for implicit deadline tasks. However, a task with
1159 * constrained deadline (deadline < period) might be awakened after the
1160 * deadline, but before the next period. In this case, replenishing the
1161 * task would allow it to run for runtime / deadline. As in this case
1162 * deadline < period, CBS enables a task to run for more than the
1163 * runtime / period. In a very loaded system, this can cause a domino
1164 * effect, making other tasks miss their deadlines.
1166 * To avoid this problem, in the activation of a constrained deadline
1167 * task after the deadline but before the next period, throttle the
1168 * task and set the replenishing timer to the begin of the next period,
1169 * unless it is boosted.
1171 static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
1173 struct task_struct *p = dl_task_of(dl_se);
1174 struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
1176 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1177 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
1178 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(p)))
1180 dl_se->dl_throttled = 1;
1181 if (dl_se->runtime > 0)
1187 int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
1189 return (dl_se->runtime <= 0);
1192 extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
1195 * This function implements the GRUB accounting rule:
1196 * according to the GRUB reclaiming algorithm, the runtime is
1197 * not decreased as "dq = -dt", but as
1198 * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt",
1199 * where u is the utilization of the task, Umax is the maximum reclaimable
1200 * utilization, Uinact is the (per-runqueue) inactive utilization, computed
1201 * as the difference between the "total runqueue utilization" and the
1202 * runqueue active utilization, and Uextra is the (per runqueue) extra
1203 * reclaimable utilization.
1204 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations
1205 * multiplied by 2^BW_SHIFT, the result has to be shifted right by
1207 * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT,
1208 * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
1209 * Since delta is a 64 bit variable, to have an overflow its value
1210 * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds.
1211 * So, overflow is not an issue here.
1213 static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
1215 u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
1217 u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT;
1220 * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)},
1221 * we compare u_inact + rq->dl.extra_bw with
1222 * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because
1223 * u_inact + rq->dl.extra_bw can be larger than
1224 * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative
1225 * leading to wrong results)
1227 if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min)
1230 u_act = BW_UNIT - u_inact - rq->dl.extra_bw;
1232 return (delta * u_act) >> BW_SHIFT;
1236 * Update the current task's runtime statistics (provided it is still
1237 * a -deadline task and has not been removed from the dl_rq).
1239 static void update_curr_dl(struct rq *rq)
1241 struct task_struct *curr = rq->curr;
1242 struct sched_dl_entity *dl_se = &curr->dl;
1243 u64 delta_exec, scaled_delta_exec;
1244 int cpu = cpu_of(rq);
1247 if (!dl_task(curr) || !on_dl_rq(dl_se))
1251 * Consumed budget is computed considering the time as
1252 * observed by schedulable tasks (excluding time spent
1253 * in hardirq context, etc.). Deadlines are instead
1254 * computed using hard walltime. This seems to be the more
1255 * natural solution, but the full ramifications of this
1256 * approach need further study.
1258 now = rq_clock_task(rq);
1259 delta_exec = now - curr->se.exec_start;
1260 if (unlikely((s64)delta_exec <= 0)) {
1261 if (unlikely(dl_se->dl_yielded))
1266 schedstat_set(curr->se.statistics.exec_max,
1267 max(curr->se.statistics.exec_max, delta_exec));
1269 curr->se.sum_exec_runtime += delta_exec;
1270 account_group_exec_runtime(curr, delta_exec);
1272 curr->se.exec_start = now;
1273 cgroup_account_cputime(curr, delta_exec);
1275 if (dl_entity_is_special(dl_se))
1279 * For tasks that participate in GRUB, we implement GRUB-PA: the
1280 * spare reclaimed bandwidth is used to clock down frequency.
1282 * For the others, we still need to scale reservation parameters
1283 * according to current frequency and CPU maximum capacity.
1285 if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) {
1286 scaled_delta_exec = grub_reclaim(delta_exec,
1290 unsigned long scale_freq = arch_scale_freq_capacity(cpu);
1291 unsigned long scale_cpu = arch_scale_cpu_capacity(cpu);
1293 scaled_delta_exec = cap_scale(delta_exec, scale_freq);
1294 scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu);
1297 dl_se->runtime -= scaled_delta_exec;
1300 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
1301 dl_se->dl_throttled = 1;
1303 /* If requested, inform the user about runtime overruns. */
1304 if (dl_runtime_exceeded(dl_se) &&
1305 (dl_se->flags & SCHED_FLAG_DL_OVERRUN))
1306 dl_se->dl_overrun = 1;
1308 __dequeue_task_dl(rq, curr, 0);
1309 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(curr)))
1310 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
1312 if (!is_leftmost(curr, &rq->dl))
1317 * Because -- for now -- we share the rt bandwidth, we need to
1318 * account our runtime there too, otherwise actual rt tasks
1319 * would be able to exceed the shared quota.
1321 * Account to the root rt group for now.
1323 * The solution we're working towards is having the RT groups scheduled
1324 * using deadline servers -- however there's a few nasties to figure
1325 * out before that can happen.
1327 if (rt_bandwidth_enabled()) {
1328 struct rt_rq *rt_rq = &rq->rt;
1330 raw_spin_lock(&rt_rq->rt_runtime_lock);
1332 * We'll let actual RT tasks worry about the overflow here, we
1333 * have our own CBS to keep us inline; only account when RT
1334 * bandwidth is relevant.
1336 if (sched_rt_bandwidth_account(rt_rq))
1337 rt_rq->rt_time += delta_exec;
1338 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1342 static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
1344 struct sched_dl_entity *dl_se = container_of(timer,
1345 struct sched_dl_entity,
1347 struct task_struct *p = dl_task_of(dl_se);
1351 rq = task_rq_lock(p, &rf);
1354 update_rq_clock(rq);
1356 if (!dl_task(p) || p->state == TASK_DEAD) {
1357 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1359 if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
1360 sub_running_bw(&p->dl, dl_rq_of_se(&p->dl));
1361 sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl));
1362 dl_se->dl_non_contending = 0;
1365 raw_spin_lock(&dl_b->lock);
1366 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
1367 raw_spin_unlock(&dl_b->lock);
1368 __dl_clear_params(p);
1372 if (dl_se->dl_non_contending == 0)
1375 sub_running_bw(dl_se, &rq->dl);
1376 dl_se->dl_non_contending = 0;
1378 task_rq_unlock(rq, p, &rf);
1381 return HRTIMER_NORESTART;
1384 void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1386 struct hrtimer *timer = &dl_se->inactive_timer;
1388 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1389 timer->function = inactive_task_timer;
1394 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1396 struct rq *rq = rq_of_dl_rq(dl_rq);
1398 if (dl_rq->earliest_dl.curr == 0 ||
1399 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
1400 dl_rq->earliest_dl.curr = deadline;
1401 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
1405 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1407 struct rq *rq = rq_of_dl_rq(dl_rq);
1410 * Since we may have removed our earliest (and/or next earliest)
1411 * task we must recompute them.
1413 if (!dl_rq->dl_nr_running) {
1414 dl_rq->earliest_dl.curr = 0;
1415 dl_rq->earliest_dl.next = 0;
1416 cpudl_clear(&rq->rd->cpudl, rq->cpu);
1418 struct rb_node *leftmost = dl_rq->root.rb_leftmost;
1419 struct sched_dl_entity *entry;
1421 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
1422 dl_rq->earliest_dl.curr = entry->deadline;
1423 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
1429 static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1430 static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1432 #endif /* CONFIG_SMP */
1435 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1437 int prio = dl_task_of(dl_se)->prio;
1438 u64 deadline = dl_se->deadline;
1440 WARN_ON(!dl_prio(prio));
1441 dl_rq->dl_nr_running++;
1442 add_nr_running(rq_of_dl_rq(dl_rq), 1);
1444 inc_dl_deadline(dl_rq, deadline);
1445 inc_dl_migration(dl_se, dl_rq);
1449 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1451 int prio = dl_task_of(dl_se)->prio;
1453 WARN_ON(!dl_prio(prio));
1454 WARN_ON(!dl_rq->dl_nr_running);
1455 dl_rq->dl_nr_running--;
1456 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
1458 dec_dl_deadline(dl_rq, dl_se->deadline);
1459 dec_dl_migration(dl_se, dl_rq);
1462 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1464 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1465 struct rb_node **link = &dl_rq->root.rb_root.rb_node;
1466 struct rb_node *parent = NULL;
1467 struct sched_dl_entity *entry;
1470 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
1474 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
1475 if (dl_time_before(dl_se->deadline, entry->deadline))
1476 link = &parent->rb_left;
1478 link = &parent->rb_right;
1483 rb_link_node(&dl_se->rb_node, parent, link);
1484 rb_insert_color_cached(&dl_se->rb_node, &dl_rq->root, leftmost);
1486 inc_dl_tasks(dl_se, dl_rq);
1489 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1491 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1493 if (RB_EMPTY_NODE(&dl_se->rb_node))
1496 rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
1497 RB_CLEAR_NODE(&dl_se->rb_node);
1499 dec_dl_tasks(dl_se, dl_rq);
1503 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
1505 BUG_ON(on_dl_rq(dl_se));
1508 * If this is a wakeup or a new instance, the scheduling
1509 * parameters of the task might need updating. Otherwise,
1510 * we want a replenishment of its runtime.
1512 if (flags & ENQUEUE_WAKEUP) {
1513 task_contending(dl_se, flags);
1514 update_dl_entity(dl_se);
1515 } else if (flags & ENQUEUE_REPLENISH) {
1516 replenish_dl_entity(dl_se);
1517 } else if ((flags & ENQUEUE_RESTORE) &&
1518 dl_time_before(dl_se->deadline,
1519 rq_clock(rq_of_dl_rq(dl_rq_of_se(dl_se))))) {
1520 setup_new_dl_entity(dl_se);
1523 __enqueue_dl_entity(dl_se);
1526 static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
1528 __dequeue_dl_entity(dl_se);
1531 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1533 if (is_dl_boosted(&p->dl)) {
1535 * Because of delays in the detection of the overrun of a
1536 * thread's runtime, it might be the case that a thread
1537 * goes to sleep in a rt mutex with negative runtime. As
1538 * a consequence, the thread will be throttled.
1540 * While waiting for the mutex, this thread can also be
1541 * boosted via PI, resulting in a thread that is throttled
1542 * and boosted at the same time.
1544 * In this case, the boost overrides the throttle.
1546 if (p->dl.dl_throttled) {
1548 * The replenish timer needs to be canceled. No
1549 * problem if it fires concurrently: boosted threads
1550 * are ignored in dl_task_timer().
1552 hrtimer_try_to_cancel(&p->dl.dl_timer);
1553 p->dl.dl_throttled = 0;
1555 } else if (!dl_prio(p->normal_prio)) {
1557 * Special case in which we have a !SCHED_DEADLINE task that is going
1558 * to be deboosted, but exceeds its runtime while doing so. No point in
1559 * replenishing it, as it's going to return back to its original
1560 * scheduling class after this. If it has been throttled, we need to
1561 * clear the flag, otherwise the task may wake up as throttled after
1562 * being boosted again with no means to replenish the runtime and clear
1565 p->dl.dl_throttled = 0;
1566 BUG_ON(!is_dl_boosted(&p->dl) || flags != ENQUEUE_REPLENISH);
1571 * Check if a constrained deadline task was activated
1572 * after the deadline but before the next period.
1573 * If that is the case, the task will be throttled and
1574 * the replenishment timer will be set to the next period.
1576 if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl))
1577 dl_check_constrained_dl(&p->dl);
1579 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) {
1580 add_rq_bw(&p->dl, &rq->dl);
1581 add_running_bw(&p->dl, &rq->dl);
1585 * If p is throttled, we do not enqueue it. In fact, if it exhausted
1586 * its budget it needs a replenishment and, since it now is on
1587 * its rq, the bandwidth timer callback (which clearly has not
1588 * run yet) will take care of this.
1589 * However, the active utilization does not depend on the fact
1590 * that the task is on the runqueue or not (but depends on the
1591 * task's state - in GRUB parlance, "inactive" vs "active contending").
1592 * In other words, even if a task is throttled its utilization must
1593 * be counted in the active utilization; hence, we need to call
1596 if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
1597 if (flags & ENQUEUE_WAKEUP)
1598 task_contending(&p->dl, flags);
1603 enqueue_dl_entity(&p->dl, flags);
1605 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1606 enqueue_pushable_dl_task(rq, p);
1609 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1611 dequeue_dl_entity(&p->dl);
1612 dequeue_pushable_dl_task(rq, p);
1615 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1618 __dequeue_task_dl(rq, p, flags);
1620 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) {
1621 sub_running_bw(&p->dl, &rq->dl);
1622 sub_rq_bw(&p->dl, &rq->dl);
1626 * This check allows to start the inactive timer (or to immediately
1627 * decrease the active utilization, if needed) in two cases:
1628 * when the task blocks and when it is terminating
1629 * (p->state == TASK_DEAD). We can handle the two cases in the same
1630 * way, because from GRUB's point of view the same thing is happening
1631 * (the task moves from "active contending" to "active non contending"
1634 if (flags & DEQUEUE_SLEEP)
1635 task_non_contending(p);
1639 * Yield task semantic for -deadline tasks is:
1641 * get off from the CPU until our next instance, with
1642 * a new runtime. This is of little use now, since we
1643 * don't have a bandwidth reclaiming mechanism. Anyway,
1644 * bandwidth reclaiming is planned for the future, and
1645 * yield_task_dl will indicate that some spare budget
1646 * is available for other task instances to use it.
1648 static void yield_task_dl(struct rq *rq)
1651 * We make the task go to sleep until its current deadline by
1652 * forcing its runtime to zero. This way, update_curr_dl() stops
1653 * it and the bandwidth timer will wake it up and will give it
1654 * new scheduling parameters (thanks to dl_yielded=1).
1656 rq->curr->dl.dl_yielded = 1;
1658 update_rq_clock(rq);
1661 * Tell update_rq_clock() that we've just updated,
1662 * so we don't do microscopic update in schedule()
1663 * and double the fastpath cost.
1665 rq_clock_skip_update(rq);
1670 static int find_later_rq(struct task_struct *task);
1673 select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1675 struct task_struct *curr;
1679 if (sd_flag != SD_BALANCE_WAKE)
1685 curr = READ_ONCE(rq->curr); /* unlocked access */
1688 * If we are dealing with a -deadline task, we must
1689 * decide where to wake it up.
1690 * If it has a later deadline and the current task
1691 * on this rq can't move (provided the waking task
1692 * can!) we prefer to send it somewhere else. On the
1693 * other hand, if it has a shorter deadline, we
1694 * try to make it stay here, it might be important.
1696 select_rq = unlikely(dl_task(curr)) &&
1697 (curr->nr_cpus_allowed < 2 ||
1698 !dl_entity_preempt(&p->dl, &curr->dl)) &&
1699 p->nr_cpus_allowed > 1;
1702 * Take the capacity of the CPU into account to
1703 * ensure it fits the requirement of the task.
1705 if (static_branch_unlikely(&sched_asym_cpucapacity))
1706 select_rq |= !dl_task_fits_capacity(p, cpu);
1709 int target = find_later_rq(p);
1712 (dl_time_before(p->dl.deadline,
1713 cpu_rq(target)->dl.earliest_dl.curr) ||
1714 (cpu_rq(target)->dl.dl_nr_running == 0)))
1723 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
1727 if (p->state != TASK_WAKING)
1732 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1733 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1734 * rq->lock is not... So, lock it
1736 raw_spin_lock(&rq->lock);
1737 if (p->dl.dl_non_contending) {
1738 update_rq_clock(rq);
1739 sub_running_bw(&p->dl, &rq->dl);
1740 p->dl.dl_non_contending = 0;
1742 * If the timer handler is currently running and the
1743 * timer cannot be cancelled, inactive_task_timer()
1744 * will see that dl_not_contending is not set, and
1745 * will not touch the rq's active utilization,
1746 * so we are still safe.
1748 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1751 sub_rq_bw(&p->dl, &rq->dl);
1752 raw_spin_unlock(&rq->lock);
1755 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1758 * Current can't be migrated, useless to reschedule,
1759 * let's hope p can move out.
1761 if (rq->curr->nr_cpus_allowed == 1 ||
1762 !cpudl_find(&rq->rd->cpudl, rq->curr, NULL))
1766 * p is migratable, so let's not schedule it and
1767 * see if it is pushed or pulled somewhere else.
1769 if (p->nr_cpus_allowed != 1 &&
1770 cpudl_find(&rq->rd->cpudl, p, NULL))
1776 static int balance_dl(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1778 if (!on_dl_rq(&p->dl) && need_pull_dl_task(rq, p)) {
1780 * This is OK, because current is on_cpu, which avoids it being
1781 * picked for load-balance and preemption/IRQs are still
1782 * disabled avoiding further scheduler activity on it and we've
1783 * not yet started the picking loop.
1785 rq_unpin_lock(rq, rf);
1787 rq_repin_lock(rq, rf);
1790 return sched_stop_runnable(rq) || sched_dl_runnable(rq);
1792 #endif /* CONFIG_SMP */
1795 * Only called when both the current and waking task are -deadline
1798 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1801 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
1808 * In the unlikely case current and p have the same deadline
1809 * let us try to decide what's the best thing to do...
1811 if ((p->dl.deadline == rq->curr->dl.deadline) &&
1812 !test_tsk_need_resched(rq->curr))
1813 check_preempt_equal_dl(rq, p);
1814 #endif /* CONFIG_SMP */
1817 #ifdef CONFIG_SCHED_HRTICK
1818 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1820 hrtick_start(rq, p->dl.runtime);
1822 #else /* !CONFIG_SCHED_HRTICK */
1823 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1828 static void set_next_task_dl(struct rq *rq, struct task_struct *p, bool first)
1830 p->se.exec_start = rq_clock_task(rq);
1832 /* You can't push away the running task */
1833 dequeue_pushable_dl_task(rq, p);
1838 if (hrtick_enabled(rq))
1839 start_hrtick_dl(rq, p);
1841 if (rq->curr->sched_class != &dl_sched_class)
1842 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
1844 deadline_queue_push_tasks(rq);
1847 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1848 struct dl_rq *dl_rq)
1850 struct rb_node *left = rb_first_cached(&dl_rq->root);
1855 return rb_entry(left, struct sched_dl_entity, rb_node);
1858 static struct task_struct *pick_next_task_dl(struct rq *rq)
1860 struct sched_dl_entity *dl_se;
1861 struct dl_rq *dl_rq = &rq->dl;
1862 struct task_struct *p;
1864 if (!sched_dl_runnable(rq))
1867 dl_se = pick_next_dl_entity(rq, dl_rq);
1869 p = dl_task_of(dl_se);
1870 set_next_task_dl(rq, p, true);
1874 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1878 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
1879 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
1880 enqueue_pushable_dl_task(rq, p);
1884 * scheduler tick hitting a task of our scheduling class.
1886 * NOTE: This function can be called remotely by the tick offload that
1887 * goes along full dynticks. Therefore no local assumption can be made
1888 * and everything must be accessed through the @rq and @curr passed in
1891 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1895 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
1897 * Even when we have runtime, update_curr_dl() might have resulted in us
1898 * not being the leftmost task anymore. In that case NEED_RESCHED will
1899 * be set and schedule() will start a new hrtick for the next task.
1901 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
1902 is_leftmost(p, &rq->dl))
1903 start_hrtick_dl(rq, p);
1906 static void task_fork_dl(struct task_struct *p)
1909 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1916 /* Only try algorithms three times */
1917 #define DL_MAX_TRIES 3
1919 static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1921 if (!task_running(rq, p) &&
1922 cpumask_test_cpu(cpu, &p->cpus_mask))
1928 * Return the earliest pushable rq's task, which is suitable to be executed
1929 * on the CPU, NULL otherwise:
1931 static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
1933 struct rb_node *next_node = rq->dl.pushable_dl_tasks_root.rb_leftmost;
1934 struct task_struct *p = NULL;
1936 if (!has_pushable_dl_tasks(rq))
1941 p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1943 if (pick_dl_task(rq, p, cpu))
1946 next_node = rb_next(next_node);
1953 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1955 static int find_later_rq(struct task_struct *task)
1957 struct sched_domain *sd;
1958 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
1959 int this_cpu = smp_processor_id();
1960 int cpu = task_cpu(task);
1962 /* Make sure the mask is initialized first */
1963 if (unlikely(!later_mask))
1966 if (task->nr_cpus_allowed == 1)
1970 * We have to consider system topology and task affinity
1971 * first, then we can look for a suitable CPU.
1973 if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
1977 * If we are here, some targets have been found, including
1978 * the most suitable which is, among the runqueues where the
1979 * current tasks have later deadlines than the task's one, the
1980 * rq with the latest possible one.
1982 * Now we check how well this matches with task's
1983 * affinity and system topology.
1985 * The last CPU where the task run is our first
1986 * guess, since it is most likely cache-hot there.
1988 if (cpumask_test_cpu(cpu, later_mask))
1991 * Check if this_cpu is to be skipped (i.e., it is
1992 * not in the mask) or not.
1994 if (!cpumask_test_cpu(this_cpu, later_mask))
1998 for_each_domain(cpu, sd) {
1999 if (sd->flags & SD_WAKE_AFFINE) {
2003 * If possible, preempting this_cpu is
2004 * cheaper than migrating.
2006 if (this_cpu != -1 &&
2007 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
2012 best_cpu = cpumask_any_and_distribute(later_mask,
2013 sched_domain_span(sd));
2015 * Last chance: if a CPU being in both later_mask
2016 * and current sd span is valid, that becomes our
2017 * choice. Of course, the latest possible CPU is
2018 * already under consideration through later_mask.
2020 if (best_cpu < nr_cpu_ids) {
2029 * At this point, all our guesses failed, we just return
2030 * 'something', and let the caller sort the things out.
2035 cpu = cpumask_any_distribute(later_mask);
2036 if (cpu < nr_cpu_ids)
2042 /* Locks the rq it finds */
2043 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
2045 struct rq *later_rq = NULL;
2049 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
2050 cpu = find_later_rq(task);
2052 if ((cpu == -1) || (cpu == rq->cpu))
2055 later_rq = cpu_rq(cpu);
2057 if (later_rq->dl.dl_nr_running &&
2058 !dl_time_before(task->dl.deadline,
2059 later_rq->dl.earliest_dl.curr)) {
2061 * Target rq has tasks of equal or earlier deadline,
2062 * retrying does not release any lock and is unlikely
2063 * to yield a different result.
2069 /* Retry if something changed. */
2070 if (double_lock_balance(rq, later_rq)) {
2071 if (unlikely(task_rq(task) != rq ||
2072 !cpumask_test_cpu(later_rq->cpu, &task->cpus_mask) ||
2073 task_running(rq, task) ||
2075 !task_on_rq_queued(task))) {
2076 double_unlock_balance(rq, later_rq);
2083 * If the rq we found has no -deadline task, or
2084 * its earliest one has a later deadline than our
2085 * task, the rq is a good one.
2087 if (!later_rq->dl.dl_nr_running ||
2088 dl_time_before(task->dl.deadline,
2089 later_rq->dl.earliest_dl.curr))
2092 /* Otherwise we try again. */
2093 double_unlock_balance(rq, later_rq);
2100 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
2102 struct task_struct *p;
2104 if (!has_pushable_dl_tasks(rq))
2107 p = rb_entry(rq->dl.pushable_dl_tasks_root.rb_leftmost,
2108 struct task_struct, pushable_dl_tasks);
2110 BUG_ON(rq->cpu != task_cpu(p));
2111 BUG_ON(task_current(rq, p));
2112 BUG_ON(p->nr_cpus_allowed <= 1);
2114 BUG_ON(!task_on_rq_queued(p));
2115 BUG_ON(!dl_task(p));
2121 * See if the non running -deadline tasks on this rq
2122 * can be sent to some other CPU where they can preempt
2123 * and start executing.
2125 static int push_dl_task(struct rq *rq)
2127 struct task_struct *next_task;
2128 struct rq *later_rq;
2131 if (!rq->dl.overloaded)
2134 next_task = pick_next_pushable_dl_task(rq);
2139 if (is_migration_disabled(next_task))
2142 if (WARN_ON(next_task == rq->curr))
2146 * If next_task preempts rq->curr, and rq->curr
2147 * can move away, it makes sense to just reschedule
2148 * without going further in pushing next_task.
2150 if (dl_task(rq->curr) &&
2151 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
2152 rq->curr->nr_cpus_allowed > 1) {
2157 /* We might release rq lock */
2158 get_task_struct(next_task);
2160 /* Will lock the rq it'll find */
2161 later_rq = find_lock_later_rq(next_task, rq);
2163 struct task_struct *task;
2166 * We must check all this again, since
2167 * find_lock_later_rq releases rq->lock and it is
2168 * then possible that next_task has migrated.
2170 task = pick_next_pushable_dl_task(rq);
2171 if (task == next_task) {
2173 * The task is still there. We don't try
2174 * again, some other CPU will pull it when ready.
2183 put_task_struct(next_task);
2188 deactivate_task(rq, next_task, 0);
2189 set_task_cpu(next_task, later_rq->cpu);
2192 * Update the later_rq clock here, because the clock is used
2193 * by the cpufreq_update_util() inside __add_running_bw().
2195 update_rq_clock(later_rq);
2196 activate_task(later_rq, next_task, ENQUEUE_NOCLOCK);
2199 resched_curr(later_rq);
2201 double_unlock_balance(rq, later_rq);
2204 put_task_struct(next_task);
2209 static void push_dl_tasks(struct rq *rq)
2211 /* push_dl_task() will return true if it moved a -deadline task */
2212 while (push_dl_task(rq))
2216 static void pull_dl_task(struct rq *this_rq)
2218 int this_cpu = this_rq->cpu, cpu;
2219 struct task_struct *p, *push_task;
2220 bool resched = false;
2222 u64 dmin = LONG_MAX;
2224 if (likely(!dl_overloaded(this_rq)))
2228 * Match the barrier from dl_set_overloaded; this guarantees that if we
2229 * see overloaded we must also see the dlo_mask bit.
2233 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2234 if (this_cpu == cpu)
2237 src_rq = cpu_rq(cpu);
2240 * It looks racy, abd it is! However, as in sched_rt.c,
2241 * we are fine with this.
2243 if (this_rq->dl.dl_nr_running &&
2244 dl_time_before(this_rq->dl.earliest_dl.curr,
2245 src_rq->dl.earliest_dl.next))
2248 /* Might drop this_rq->lock */
2250 double_lock_balance(this_rq, src_rq);
2253 * If there are no more pullable tasks on the
2254 * rq, we're done with it.
2256 if (src_rq->dl.dl_nr_running <= 1)
2259 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
2262 * We found a task to be pulled if:
2263 * - it preempts our current (if there's one),
2264 * - it will preempt the last one we pulled (if any).
2266 if (p && dl_time_before(p->dl.deadline, dmin) &&
2267 (!this_rq->dl.dl_nr_running ||
2268 dl_time_before(p->dl.deadline,
2269 this_rq->dl.earliest_dl.curr))) {
2270 WARN_ON(p == src_rq->curr);
2271 WARN_ON(!task_on_rq_queued(p));
2274 * Then we pull iff p has actually an earlier
2275 * deadline than the current task of its runqueue.
2277 if (dl_time_before(p->dl.deadline,
2278 src_rq->curr->dl.deadline))
2281 if (is_migration_disabled(p)) {
2282 trace_sched_migrate_pull_tp(p);
2283 push_task = get_push_task(src_rq);
2285 deactivate_task(src_rq, p, 0);
2286 set_task_cpu(p, this_cpu);
2287 activate_task(this_rq, p, 0);
2288 dmin = p->dl.deadline;
2292 /* Is there any other task even earlier? */
2295 double_unlock_balance(this_rq, src_rq);
2298 raw_spin_unlock(&this_rq->lock);
2299 stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2300 push_task, &src_rq->push_work);
2301 raw_spin_lock(&this_rq->lock);
2306 resched_curr(this_rq);
2310 * Since the task is not running and a reschedule is not going to happen
2311 * anytime soon on its runqueue, we try pushing it away now.
2313 static void task_woken_dl(struct rq *rq, struct task_struct *p)
2315 if (!task_running(rq, p) &&
2316 !test_tsk_need_resched(rq->curr) &&
2317 p->nr_cpus_allowed > 1 &&
2318 dl_task(rq->curr) &&
2319 (rq->curr->nr_cpus_allowed < 2 ||
2320 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
2325 static void set_cpus_allowed_dl(struct task_struct *p,
2326 const struct cpumask *new_mask,
2329 struct root_domain *src_rd;
2332 BUG_ON(!dl_task(p));
2337 * Migrating a SCHED_DEADLINE task between exclusive
2338 * cpusets (different root_domains) entails a bandwidth
2339 * update. We already made space for us in the destination
2340 * domain (see cpuset_can_attach()).
2342 if (!cpumask_intersects(src_rd->span, new_mask)) {
2343 struct dl_bw *src_dl_b;
2345 src_dl_b = dl_bw_of(cpu_of(rq));
2347 * We now free resources of the root_domain we are migrating
2348 * off. In the worst case, sched_setattr() may temporary fail
2349 * until we complete the update.
2351 raw_spin_lock(&src_dl_b->lock);
2352 __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
2353 raw_spin_unlock(&src_dl_b->lock);
2356 set_cpus_allowed_common(p, new_mask, flags);
2359 /* Assumes rq->lock is held */
2360 static void rq_online_dl(struct rq *rq)
2362 if (rq->dl.overloaded)
2363 dl_set_overload(rq);
2365 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
2366 if (rq->dl.dl_nr_running > 0)
2367 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
2370 /* Assumes rq->lock is held */
2371 static void rq_offline_dl(struct rq *rq)
2373 if (rq->dl.overloaded)
2374 dl_clear_overload(rq);
2376 cpudl_clear(&rq->rd->cpudl, rq->cpu);
2377 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
2380 void __init init_sched_dl_class(void)
2384 for_each_possible_cpu(i)
2385 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2386 GFP_KERNEL, cpu_to_node(i));
2389 void dl_add_task_root_domain(struct task_struct *p)
2395 rq = task_rq_lock(p, &rf);
2399 dl_b = &rq->rd->dl_bw;
2400 raw_spin_lock(&dl_b->lock);
2402 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
2404 raw_spin_unlock(&dl_b->lock);
2407 task_rq_unlock(rq, p, &rf);
2410 void dl_clear_root_domain(struct root_domain *rd)
2412 unsigned long flags;
2414 raw_spin_lock_irqsave(&rd->dl_bw.lock, flags);
2415 rd->dl_bw.total_bw = 0;
2416 raw_spin_unlock_irqrestore(&rd->dl_bw.lock, flags);
2419 #endif /* CONFIG_SMP */
2421 static void switched_from_dl(struct rq *rq, struct task_struct *p)
2424 * task_non_contending() can start the "inactive timer" (if the 0-lag
2425 * time is in the future). If the task switches back to dl before
2426 * the "inactive timer" fires, it can continue to consume its current
2427 * runtime using its current deadline. If it stays outside of
2428 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2429 * will reset the task parameters.
2431 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2432 task_non_contending(p);
2434 if (!task_on_rq_queued(p)) {
2436 * Inactive timer is armed. However, p is leaving DEADLINE and
2437 * might migrate away from this rq while continuing to run on
2438 * some other class. We need to remove its contribution from
2439 * this rq running_bw now, or sub_rq_bw (below) will complain.
2441 if (p->dl.dl_non_contending)
2442 sub_running_bw(&p->dl, &rq->dl);
2443 sub_rq_bw(&p->dl, &rq->dl);
2447 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2448 * at the 0-lag time, because the task could have been migrated
2449 * while SCHED_OTHER in the meanwhile.
2451 if (p->dl.dl_non_contending)
2452 p->dl.dl_non_contending = 0;
2455 * Since this might be the only -deadline task on the rq,
2456 * this is the right place to try to pull some other one
2457 * from an overloaded CPU, if any.
2459 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2462 deadline_queue_pull_task(rq);
2466 * When switching to -deadline, we may overload the rq, then
2467 * we try to push someone off, if possible.
2469 static void switched_to_dl(struct rq *rq, struct task_struct *p)
2471 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2474 /* If p is not queued we will update its parameters at next wakeup. */
2475 if (!task_on_rq_queued(p)) {
2476 add_rq_bw(&p->dl, &rq->dl);
2481 if (rq->curr != p) {
2483 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
2484 deadline_queue_push_tasks(rq);
2486 if (dl_task(rq->curr))
2487 check_preempt_curr_dl(rq, p, 0);
2491 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2496 * If the scheduling parameters of a -deadline task changed,
2497 * a push or pull operation might be needed.
2499 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2502 if (task_on_rq_queued(p) || rq->curr == p) {
2505 * This might be too much, but unfortunately
2506 * we don't have the old deadline value, and
2507 * we can't argue if the task is increasing
2508 * or lowering its prio, so...
2510 if (!rq->dl.overloaded)
2511 deadline_queue_pull_task(rq);
2514 * If we now have a earlier deadline task than p,
2515 * then reschedule, provided p is still on this
2518 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
2522 * Again, we don't know if p has a earlier
2523 * or later deadline, so let's blindly set a
2524 * (maybe not needed) rescheduling point.
2527 #endif /* CONFIG_SMP */
2531 const struct sched_class dl_sched_class
2532 __section("__dl_sched_class") = {
2533 .enqueue_task = enqueue_task_dl,
2534 .dequeue_task = dequeue_task_dl,
2535 .yield_task = yield_task_dl,
2537 .check_preempt_curr = check_preempt_curr_dl,
2539 .pick_next_task = pick_next_task_dl,
2540 .put_prev_task = put_prev_task_dl,
2541 .set_next_task = set_next_task_dl,
2544 .balance = balance_dl,
2545 .select_task_rq = select_task_rq_dl,
2546 .migrate_task_rq = migrate_task_rq_dl,
2547 .set_cpus_allowed = set_cpus_allowed_dl,
2548 .rq_online = rq_online_dl,
2549 .rq_offline = rq_offline_dl,
2550 .task_woken = task_woken_dl,
2551 .find_lock_rq = find_lock_later_rq,
2554 .task_tick = task_tick_dl,
2555 .task_fork = task_fork_dl,
2557 .prio_changed = prio_changed_dl,
2558 .switched_from = switched_from_dl,
2559 .switched_to = switched_to_dl,
2561 .update_curr = update_curr_dl,
2564 int sched_dl_global_validate(void)
2566 u64 runtime = global_rt_runtime();
2567 u64 period = global_rt_period();
2568 u64 new_bw = to_ratio(period, runtime);
2570 int cpu, cpus, ret = 0;
2571 unsigned long flags;
2574 * Here we want to check the bandwidth not being set to some
2575 * value smaller than the currently allocated bandwidth in
2576 * any of the root_domains.
2578 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
2579 * cycling on root_domains... Discussion on different/better
2580 * solutions is welcome!
2582 for_each_possible_cpu(cpu) {
2583 rcu_read_lock_sched();
2584 dl_b = dl_bw_of(cpu);
2585 cpus = dl_bw_cpus(cpu);
2587 raw_spin_lock_irqsave(&dl_b->lock, flags);
2588 if (new_bw * cpus < dl_b->total_bw)
2590 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2592 rcu_read_unlock_sched();
2601 static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
2603 if (global_rt_runtime() == RUNTIME_INF) {
2604 dl_rq->bw_ratio = 1 << RATIO_SHIFT;
2605 dl_rq->extra_bw = 1 << BW_SHIFT;
2607 dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
2608 global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
2609 dl_rq->extra_bw = to_ratio(global_rt_period(),
2610 global_rt_runtime());
2614 void sched_dl_do_global(void)
2619 unsigned long flags;
2621 def_dl_bandwidth.dl_period = global_rt_period();
2622 def_dl_bandwidth.dl_runtime = global_rt_runtime();
2624 if (global_rt_runtime() != RUNTIME_INF)
2625 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
2628 * FIXME: As above...
2630 for_each_possible_cpu(cpu) {
2631 rcu_read_lock_sched();
2632 dl_b = dl_bw_of(cpu);
2634 raw_spin_lock_irqsave(&dl_b->lock, flags);
2636 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2638 rcu_read_unlock_sched();
2639 init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
2644 * We must be sure that accepting a new task (or allowing changing the
2645 * parameters of an existing one) is consistent with the bandwidth
2646 * constraints. If yes, this function also accordingly updates the currently
2647 * allocated bandwidth to reflect the new situation.
2649 * This function is called while holding p's rq->lock.
2651 int sched_dl_overflow(struct task_struct *p, int policy,
2652 const struct sched_attr *attr)
2654 u64 period = attr->sched_period ?: attr->sched_deadline;
2655 u64 runtime = attr->sched_runtime;
2656 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
2657 int cpus, err = -1, cpu = task_cpu(p);
2658 struct dl_bw *dl_b = dl_bw_of(cpu);
2661 if (attr->sched_flags & SCHED_FLAG_SUGOV)
2664 /* !deadline task may carry old deadline bandwidth */
2665 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
2669 * Either if a task, enters, leave, or stays -deadline but changes
2670 * its parameters, we may need to update accordingly the total
2671 * allocated bandwidth of the container.
2673 raw_spin_lock(&dl_b->lock);
2674 cpus = dl_bw_cpus(cpu);
2675 cap = dl_bw_capacity(cpu);
2677 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2678 !__dl_overflow(dl_b, cap, 0, new_bw)) {
2679 if (hrtimer_active(&p->dl.inactive_timer))
2680 __dl_sub(dl_b, p->dl.dl_bw, cpus);
2681 __dl_add(dl_b, new_bw, cpus);
2683 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2684 !__dl_overflow(dl_b, cap, p->dl.dl_bw, new_bw)) {
2686 * XXX this is slightly incorrect: when the task
2687 * utilization decreases, we should delay the total
2688 * utilization change until the task's 0-lag point.
2689 * But this would require to set the task's "inactive
2690 * timer" when the task is not inactive.
2692 __dl_sub(dl_b, p->dl.dl_bw, cpus);
2693 __dl_add(dl_b, new_bw, cpus);
2694 dl_change_utilization(p, new_bw);
2696 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2698 * Do not decrease the total deadline utilization here,
2699 * switched_from_dl() will take care to do it at the correct
2704 raw_spin_unlock(&dl_b->lock);
2710 * This function initializes the sched_dl_entity of a newly becoming
2711 * SCHED_DEADLINE task.
2713 * Only the static values are considered here, the actual runtime and the
2714 * absolute deadline will be properly calculated when the task is enqueued
2715 * for the first time with its new policy.
2717 void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
2719 struct sched_dl_entity *dl_se = &p->dl;
2721 dl_se->dl_runtime = attr->sched_runtime;
2722 dl_se->dl_deadline = attr->sched_deadline;
2723 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
2724 dl_se->flags = attr->sched_flags & SCHED_DL_FLAGS;
2725 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
2726 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
2729 void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
2731 struct sched_dl_entity *dl_se = &p->dl;
2733 attr->sched_priority = p->rt_priority;
2734 attr->sched_runtime = dl_se->dl_runtime;
2735 attr->sched_deadline = dl_se->dl_deadline;
2736 attr->sched_period = dl_se->dl_period;
2737 attr->sched_flags &= ~SCHED_DL_FLAGS;
2738 attr->sched_flags |= dl_se->flags;
2742 * Default limits for DL period; on the top end we guard against small util
2743 * tasks still getting rediculous long effective runtimes, on the bottom end we
2744 * guard against timer DoS.
2746 unsigned int sysctl_sched_dl_period_max = 1 << 22; /* ~4 seconds */
2747 unsigned int sysctl_sched_dl_period_min = 100; /* 100 us */
2750 * This function validates the new parameters of a -deadline task.
2751 * We ask for the deadline not being zero, and greater or equal
2752 * than the runtime, as well as the period of being zero or
2753 * greater than deadline. Furthermore, we have to be sure that
2754 * user parameters are above the internal resolution of 1us (we
2755 * check sched_runtime only since it is always the smaller one) and
2756 * below 2^63 ns (we have to check both sched_deadline and
2757 * sched_period, as the latter can be zero).
2759 bool __checkparam_dl(const struct sched_attr *attr)
2761 u64 period, max, min;
2763 /* special dl tasks don't actually use any parameter */
2764 if (attr->sched_flags & SCHED_FLAG_SUGOV)
2768 if (attr->sched_deadline == 0)
2772 * Since we truncate DL_SCALE bits, make sure we're at least
2775 if (attr->sched_runtime < (1ULL << DL_SCALE))
2779 * Since we use the MSB for wrap-around and sign issues, make
2780 * sure it's not set (mind that period can be equal to zero).
2782 if (attr->sched_deadline & (1ULL << 63) ||
2783 attr->sched_period & (1ULL << 63))
2786 period = attr->sched_period;
2788 period = attr->sched_deadline;
2790 /* runtime <= deadline <= period (if period != 0) */
2791 if (period < attr->sched_deadline ||
2792 attr->sched_deadline < attr->sched_runtime)
2795 max = (u64)READ_ONCE(sysctl_sched_dl_period_max) * NSEC_PER_USEC;
2796 min = (u64)READ_ONCE(sysctl_sched_dl_period_min) * NSEC_PER_USEC;
2798 if (period < min || period > max)
2805 * This function clears the sched_dl_entity static params.
2807 void __dl_clear_params(struct task_struct *p)
2809 struct sched_dl_entity *dl_se = &p->dl;
2811 dl_se->dl_runtime = 0;
2812 dl_se->dl_deadline = 0;
2813 dl_se->dl_period = 0;
2816 dl_se->dl_density = 0;
2818 dl_se->dl_throttled = 0;
2819 dl_se->dl_yielded = 0;
2820 dl_se->dl_non_contending = 0;
2821 dl_se->dl_overrun = 0;
2823 #ifdef CONFIG_RT_MUTEXES
2824 dl_se->pi_se = dl_se;
2828 bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
2830 struct sched_dl_entity *dl_se = &p->dl;
2832 if (dl_se->dl_runtime != attr->sched_runtime ||
2833 dl_se->dl_deadline != attr->sched_deadline ||
2834 dl_se->dl_period != attr->sched_period ||
2835 dl_se->flags != (attr->sched_flags & SCHED_DL_FLAGS))
2842 int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed)
2844 unsigned long flags, cap;
2845 unsigned int dest_cpu;
2850 dest_cpu = cpumask_any_and(cpu_active_mask, cs_cpus_allowed);
2852 rcu_read_lock_sched();
2853 dl_b = dl_bw_of(dest_cpu);
2854 raw_spin_lock_irqsave(&dl_b->lock, flags);
2855 cap = dl_bw_capacity(dest_cpu);
2856 overflow = __dl_overflow(dl_b, cap, 0, p->dl.dl_bw);
2861 * We reserve space for this task in the destination
2862 * root_domain, as we can't fail after this point.
2863 * We will free resources in the source root_domain
2864 * later on (see set_cpus_allowed_dl()).
2866 int cpus = dl_bw_cpus(dest_cpu);
2868 __dl_add(dl_b, p->dl.dl_bw, cpus);
2871 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2872 rcu_read_unlock_sched();
2877 int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
2878 const struct cpumask *trial)
2880 int ret = 1, trial_cpus;
2881 struct dl_bw *cur_dl_b;
2882 unsigned long flags;
2884 rcu_read_lock_sched();
2885 cur_dl_b = dl_bw_of(cpumask_any(cur));
2886 trial_cpus = cpumask_weight(trial);
2888 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
2889 if (cur_dl_b->bw != -1 &&
2890 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
2892 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
2893 rcu_read_unlock_sched();
2898 bool dl_cpu_busy(unsigned int cpu)
2900 unsigned long flags, cap;
2904 rcu_read_lock_sched();
2905 dl_b = dl_bw_of(cpu);
2906 raw_spin_lock_irqsave(&dl_b->lock, flags);
2907 cap = dl_bw_capacity(cpu);
2908 overflow = __dl_overflow(dl_b, cap, 0, 0);
2909 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2910 rcu_read_unlock_sched();
2916 #ifdef CONFIG_SCHED_DEBUG
2917 void print_dl_stats(struct seq_file *m, int cpu)
2919 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
2921 #endif /* CONFIG_SCHED_DEBUG */