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
);
47 static inline struct dl_bw
*dl_bw_of(int i
)
49 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
50 "sched RCU must be held");
51 return &cpu_rq(i
)->rd
->dl_bw
;
54 static inline int dl_bw_cpus(int i
)
56 struct root_domain
*rd
= cpu_rq(i
)->rd
;
59 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
60 "sched RCU must be held");
61 for_each_cpu_and(i
, rd
->span
, cpu_active_mask
)
67 static inline struct dl_bw
*dl_bw_of(int i
)
69 return &cpu_rq(i
)->dl
.dl_bw
;
72 static inline int dl_bw_cpus(int i
)
79 void __add_running_bw(u64 dl_bw
, struct dl_rq
*dl_rq
)
81 u64 old
= dl_rq
->running_bw
;
83 lockdep_assert_held(&(rq_of_dl_rq(dl_rq
))->lock
);
84 dl_rq
->running_bw
+= dl_bw
;
85 SCHED_WARN_ON(dl_rq
->running_bw
< old
); /* overflow */
86 SCHED_WARN_ON(dl_rq
->running_bw
> dl_rq
->this_bw
);
87 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
88 cpufreq_update_util(rq_of_dl_rq(dl_rq
), 0);
92 void __sub_running_bw(u64 dl_bw
, struct dl_rq
*dl_rq
)
94 u64 old
= dl_rq
->running_bw
;
96 lockdep_assert_held(&(rq_of_dl_rq(dl_rq
))->lock
);
97 dl_rq
->running_bw
-= dl_bw
;
98 SCHED_WARN_ON(dl_rq
->running_bw
> old
); /* underflow */
99 if (dl_rq
->running_bw
> old
)
100 dl_rq
->running_bw
= 0;
101 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
102 cpufreq_update_util(rq_of_dl_rq(dl_rq
), 0);
106 void __add_rq_bw(u64 dl_bw
, struct dl_rq
*dl_rq
)
108 u64 old
= dl_rq
->this_bw
;
110 lockdep_assert_held(&(rq_of_dl_rq(dl_rq
))->lock
);
111 dl_rq
->this_bw
+= dl_bw
;
112 SCHED_WARN_ON(dl_rq
->this_bw
< old
); /* overflow */
116 void __sub_rq_bw(u64 dl_bw
, struct dl_rq
*dl_rq
)
118 u64 old
= dl_rq
->this_bw
;
120 lockdep_assert_held(&(rq_of_dl_rq(dl_rq
))->lock
);
121 dl_rq
->this_bw
-= dl_bw
;
122 SCHED_WARN_ON(dl_rq
->this_bw
> old
); /* underflow */
123 if (dl_rq
->this_bw
> old
)
125 SCHED_WARN_ON(dl_rq
->running_bw
> dl_rq
->this_bw
);
129 void add_rq_bw(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
131 if (!dl_entity_is_special(dl_se
))
132 __add_rq_bw(dl_se
->dl_bw
, dl_rq
);
136 void sub_rq_bw(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
138 if (!dl_entity_is_special(dl_se
))
139 __sub_rq_bw(dl_se
->dl_bw
, dl_rq
);
143 void add_running_bw(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
145 if (!dl_entity_is_special(dl_se
))
146 __add_running_bw(dl_se
->dl_bw
, dl_rq
);
150 void sub_running_bw(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
152 if (!dl_entity_is_special(dl_se
))
153 __sub_running_bw(dl_se
->dl_bw
, dl_rq
);
156 static void dl_change_utilization(struct task_struct
*p
, u64 new_bw
)
160 BUG_ON(p
->dl
.flags
& SCHED_FLAG_SUGOV
);
162 if (task_on_rq_queued(p
))
166 if (p
->dl
.dl_non_contending
) {
167 sub_running_bw(&p
->dl
, &rq
->dl
);
168 p
->dl
.dl_non_contending
= 0;
170 * If the timer handler is currently running and the
171 * timer cannot be cancelled, inactive_task_timer()
172 * will see that dl_not_contending is not set, and
173 * will not touch the rq's active utilization,
174 * so we are still safe.
176 if (hrtimer_try_to_cancel(&p
->dl
.inactive_timer
) == 1)
179 __sub_rq_bw(p
->dl
.dl_bw
, &rq
->dl
);
180 __add_rq_bw(new_bw
, &rq
->dl
);
184 * The utilization of a task cannot be immediately removed from
185 * the rq active utilization (running_bw) when the task blocks.
186 * Instead, we have to wait for the so called "0-lag time".
188 * If a task blocks before the "0-lag time", a timer (the inactive
189 * timer) is armed, and running_bw is decreased when the timer
192 * If the task wakes up again before the inactive timer fires,
193 * the timer is cancelled, whereas if the task wakes up after the
194 * inactive timer fired (and running_bw has been decreased) the
195 * task's utilization has to be added to running_bw again.
196 * A flag in the deadline scheduling entity (dl_non_contending)
197 * is used to avoid race conditions between the inactive timer handler
200 * The following diagram shows how running_bw is updated. A task is
201 * "ACTIVE" when its utilization contributes to running_bw; an
202 * "ACTIVE contending" task is in the TASK_RUNNING state, while an
203 * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
204 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
205 * time already passed, which does not contribute to running_bw anymore.
206 * +------------------+
208 * +------------------>+ contending |
209 * | add_running_bw | |
210 * | +----+------+------+
213 * +--------+-------+ | |
214 * | | t >= 0-lag | | wakeup
215 * | INACTIVE |<---------------+ |
216 * | | sub_running_bw | |
217 * +--------+-------+ | |
222 * | +----+------+------+
223 * | sub_running_bw | ACTIVE |
224 * +-------------------+ |
225 * inactive timer | non contending |
226 * fired +------------------+
228 * The task_non_contending() function is invoked when a task
229 * blocks, and checks if the 0-lag time already passed or
230 * not (in the first case, it directly updates running_bw;
231 * in the second case, it arms the inactive timer).
233 * The task_contending() function is invoked when a task wakes
234 * up, and checks if the task is still in the "ACTIVE non contending"
235 * state or not (in the second case, it updates running_bw).
237 static void task_non_contending(struct task_struct
*p
)
239 struct sched_dl_entity
*dl_se
= &p
->dl
;
240 struct hrtimer
*timer
= &dl_se
->inactive_timer
;
241 struct dl_rq
*dl_rq
= dl_rq_of_se(dl_se
);
242 struct rq
*rq
= rq_of_dl_rq(dl_rq
);
246 * If this is a non-deadline task that has been boosted,
249 if (dl_se
->dl_runtime
== 0)
252 if (dl_entity_is_special(dl_se
))
255 WARN_ON(dl_se
->dl_non_contending
);
257 zerolag_time
= dl_se
->deadline
-
258 div64_long((dl_se
->runtime
* dl_se
->dl_period
),
262 * Using relative times instead of the absolute "0-lag time"
263 * allows to simplify the code
265 zerolag_time
-= rq_clock(rq
);
268 * If the "0-lag time" already passed, decrease the active
269 * utilization now, instead of starting a timer
271 if ((zerolag_time
< 0) || hrtimer_active(&dl_se
->inactive_timer
)) {
273 sub_running_bw(dl_se
, dl_rq
);
274 if (!dl_task(p
) || p
->state
== TASK_DEAD
) {
275 struct dl_bw
*dl_b
= dl_bw_of(task_cpu(p
));
277 if (p
->state
== TASK_DEAD
)
278 sub_rq_bw(&p
->dl
, &rq
->dl
);
279 raw_spin_lock(&dl_b
->lock
);
280 __dl_sub(dl_b
, p
->dl
.dl_bw
, dl_bw_cpus(task_cpu(p
)));
281 __dl_clear_params(p
);
282 raw_spin_unlock(&dl_b
->lock
);
288 dl_se
->dl_non_contending
= 1;
290 hrtimer_start(timer
, ns_to_ktime(zerolag_time
), HRTIMER_MODE_REL_HARD
);
293 static void task_contending(struct sched_dl_entity
*dl_se
, int flags
)
295 struct dl_rq
*dl_rq
= dl_rq_of_se(dl_se
);
298 * If this is a non-deadline task that has been boosted,
301 if (dl_se
->dl_runtime
== 0)
304 if (flags
& ENQUEUE_MIGRATED
)
305 add_rq_bw(dl_se
, dl_rq
);
307 if (dl_se
->dl_non_contending
) {
308 dl_se
->dl_non_contending
= 0;
310 * If the timer handler is currently running and the
311 * timer cannot be cancelled, inactive_task_timer()
312 * will see that dl_not_contending is not set, and
313 * will not touch the rq's active utilization,
314 * so we are still safe.
316 if (hrtimer_try_to_cancel(&dl_se
->inactive_timer
) == 1)
317 put_task_struct(dl_task_of(dl_se
));
320 * Since "dl_non_contending" is not set, the
321 * task's utilization has already been removed from
322 * active utilization (either when the task blocked,
323 * when the "inactive timer" fired).
326 add_running_bw(dl_se
, dl_rq
);
330 static inline int is_leftmost(struct task_struct
*p
, struct dl_rq
*dl_rq
)
332 struct sched_dl_entity
*dl_se
= &p
->dl
;
334 return dl_rq
->root
.rb_leftmost
== &dl_se
->rb_node
;
337 static void init_dl_rq_bw_ratio(struct dl_rq
*dl_rq
);
339 void init_dl_bandwidth(struct dl_bandwidth
*dl_b
, u64 period
, u64 runtime
)
341 raw_spin_lock_init(&dl_b
->dl_runtime_lock
);
342 dl_b
->dl_period
= period
;
343 dl_b
->dl_runtime
= runtime
;
346 void init_dl_bw(struct dl_bw
*dl_b
)
348 raw_spin_lock_init(&dl_b
->lock
);
349 raw_spin_lock(&def_dl_bandwidth
.dl_runtime_lock
);
350 if (global_rt_runtime() == RUNTIME_INF
)
353 dl_b
->bw
= to_ratio(global_rt_period(), global_rt_runtime());
354 raw_spin_unlock(&def_dl_bandwidth
.dl_runtime_lock
);
358 void init_dl_rq(struct dl_rq
*dl_rq
)
360 dl_rq
->root
= RB_ROOT_CACHED
;
363 /* zero means no -deadline tasks */
364 dl_rq
->earliest_dl
.curr
= dl_rq
->earliest_dl
.next
= 0;
366 dl_rq
->dl_nr_migratory
= 0;
367 dl_rq
->overloaded
= 0;
368 dl_rq
->pushable_dl_tasks_root
= RB_ROOT_CACHED
;
370 init_dl_bw(&dl_rq
->dl_bw
);
373 dl_rq
->running_bw
= 0;
375 init_dl_rq_bw_ratio(dl_rq
);
380 static inline int dl_overloaded(struct rq
*rq
)
382 return atomic_read(&rq
->rd
->dlo_count
);
385 static inline void dl_set_overload(struct rq
*rq
)
390 cpumask_set_cpu(rq
->cpu
, rq
->rd
->dlo_mask
);
392 * Must be visible before the overload count is
393 * set (as in sched_rt.c).
395 * Matched by the barrier in pull_dl_task().
398 atomic_inc(&rq
->rd
->dlo_count
);
401 static inline void dl_clear_overload(struct rq
*rq
)
406 atomic_dec(&rq
->rd
->dlo_count
);
407 cpumask_clear_cpu(rq
->cpu
, rq
->rd
->dlo_mask
);
410 static void update_dl_migration(struct dl_rq
*dl_rq
)
412 if (dl_rq
->dl_nr_migratory
&& dl_rq
->dl_nr_running
> 1) {
413 if (!dl_rq
->overloaded
) {
414 dl_set_overload(rq_of_dl_rq(dl_rq
));
415 dl_rq
->overloaded
= 1;
417 } else if (dl_rq
->overloaded
) {
418 dl_clear_overload(rq_of_dl_rq(dl_rq
));
419 dl_rq
->overloaded
= 0;
423 static void inc_dl_migration(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
425 struct task_struct
*p
= dl_task_of(dl_se
);
427 if (p
->nr_cpus_allowed
> 1)
428 dl_rq
->dl_nr_migratory
++;
430 update_dl_migration(dl_rq
);
433 static void dec_dl_migration(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
435 struct task_struct
*p
= dl_task_of(dl_se
);
437 if (p
->nr_cpus_allowed
> 1)
438 dl_rq
->dl_nr_migratory
--;
440 update_dl_migration(dl_rq
);
444 * The list of pushable -deadline task is not a plist, like in
445 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
447 static void enqueue_pushable_dl_task(struct rq
*rq
, struct task_struct
*p
)
449 struct dl_rq
*dl_rq
= &rq
->dl
;
450 struct rb_node
**link
= &dl_rq
->pushable_dl_tasks_root
.rb_root
.rb_node
;
451 struct rb_node
*parent
= NULL
;
452 struct task_struct
*entry
;
453 bool leftmost
= true;
455 BUG_ON(!RB_EMPTY_NODE(&p
->pushable_dl_tasks
));
459 entry
= rb_entry(parent
, struct task_struct
,
461 if (dl_entity_preempt(&p
->dl
, &entry
->dl
))
462 link
= &parent
->rb_left
;
464 link
= &parent
->rb_right
;
470 dl_rq
->earliest_dl
.next
= p
->dl
.deadline
;
472 rb_link_node(&p
->pushable_dl_tasks
, parent
, link
);
473 rb_insert_color_cached(&p
->pushable_dl_tasks
,
474 &dl_rq
->pushable_dl_tasks_root
, leftmost
);
477 static void dequeue_pushable_dl_task(struct rq
*rq
, struct task_struct
*p
)
479 struct dl_rq
*dl_rq
= &rq
->dl
;
481 if (RB_EMPTY_NODE(&p
->pushable_dl_tasks
))
484 if (dl_rq
->pushable_dl_tasks_root
.rb_leftmost
== &p
->pushable_dl_tasks
) {
485 struct rb_node
*next_node
;
487 next_node
= rb_next(&p
->pushable_dl_tasks
);
489 dl_rq
->earliest_dl
.next
= rb_entry(next_node
,
490 struct task_struct
, pushable_dl_tasks
)->dl
.deadline
;
494 rb_erase_cached(&p
->pushable_dl_tasks
, &dl_rq
->pushable_dl_tasks_root
);
495 RB_CLEAR_NODE(&p
->pushable_dl_tasks
);
498 static inline int has_pushable_dl_tasks(struct rq
*rq
)
500 return !RB_EMPTY_ROOT(&rq
->dl
.pushable_dl_tasks_root
.rb_root
);
503 static int push_dl_task(struct rq
*rq
);
505 static inline bool need_pull_dl_task(struct rq
*rq
, struct task_struct
*prev
)
507 return dl_task(prev
);
510 static DEFINE_PER_CPU(struct callback_head
, dl_push_head
);
511 static DEFINE_PER_CPU(struct callback_head
, dl_pull_head
);
513 static void push_dl_tasks(struct rq
*);
514 static void pull_dl_task(struct rq
*);
516 static inline void deadline_queue_push_tasks(struct rq
*rq
)
518 if (!has_pushable_dl_tasks(rq
))
521 queue_balance_callback(rq
, &per_cpu(dl_push_head
, rq
->cpu
), push_dl_tasks
);
524 static inline void deadline_queue_pull_task(struct rq
*rq
)
526 queue_balance_callback(rq
, &per_cpu(dl_pull_head
, rq
->cpu
), pull_dl_task
);
529 static struct rq
*find_lock_later_rq(struct task_struct
*task
, struct rq
*rq
);
531 static struct rq
*dl_task_offline_migration(struct rq
*rq
, struct task_struct
*p
)
533 struct rq
*later_rq
= NULL
;
536 later_rq
= find_lock_later_rq(p
, rq
);
541 * If we cannot preempt any rq, fall back to pick any
544 cpu
= cpumask_any_and(cpu_active_mask
, p
->cpus_ptr
);
545 if (cpu
>= nr_cpu_ids
) {
547 * Failed to find any suitable CPU.
548 * The task will never come back!
550 BUG_ON(dl_bandwidth_enabled());
553 * If admission control is disabled we
554 * try a little harder to let the task
557 cpu
= cpumask_any(cpu_active_mask
);
559 later_rq
= cpu_rq(cpu
);
560 double_lock_balance(rq
, later_rq
);
563 if (p
->dl
.dl_non_contending
|| p
->dl
.dl_throttled
) {
565 * Inactive timer is armed (or callback is running, but
566 * waiting for us to release rq locks). In any case, when it
567 * will fire (or continue), it will see running_bw of this
568 * task migrated to later_rq (and correctly handle it).
570 sub_running_bw(&p
->dl
, &rq
->dl
);
571 sub_rq_bw(&p
->dl
, &rq
->dl
);
573 add_rq_bw(&p
->dl
, &later_rq
->dl
);
574 add_running_bw(&p
->dl
, &later_rq
->dl
);
576 sub_rq_bw(&p
->dl
, &rq
->dl
);
577 add_rq_bw(&p
->dl
, &later_rq
->dl
);
581 * And we finally need to fixup root_domain(s) bandwidth accounting,
582 * since p is still hanging out in the old (now moved to default) root
585 dl_b
= &rq
->rd
->dl_bw
;
586 raw_spin_lock(&dl_b
->lock
);
587 __dl_sub(dl_b
, p
->dl
.dl_bw
, cpumask_weight(rq
->rd
->span
));
588 raw_spin_unlock(&dl_b
->lock
);
590 dl_b
= &later_rq
->rd
->dl_bw
;
591 raw_spin_lock(&dl_b
->lock
);
592 __dl_add(dl_b
, p
->dl
.dl_bw
, cpumask_weight(later_rq
->rd
->span
));
593 raw_spin_unlock(&dl_b
->lock
);
595 set_task_cpu(p
, later_rq
->cpu
);
596 double_unlock_balance(later_rq
, rq
);
604 void enqueue_pushable_dl_task(struct rq
*rq
, struct task_struct
*p
)
609 void dequeue_pushable_dl_task(struct rq
*rq
, struct task_struct
*p
)
614 void inc_dl_migration(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
619 void dec_dl_migration(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
623 static inline bool need_pull_dl_task(struct rq
*rq
, struct task_struct
*prev
)
628 static inline void pull_dl_task(struct rq
*rq
)
632 static inline void deadline_queue_push_tasks(struct rq
*rq
)
636 static inline void deadline_queue_pull_task(struct rq
*rq
)
639 #endif /* CONFIG_SMP */
641 static void enqueue_task_dl(struct rq
*rq
, struct task_struct
*p
, int flags
);
642 static void __dequeue_task_dl(struct rq
*rq
, struct task_struct
*p
, int flags
);
643 static void check_preempt_curr_dl(struct rq
*rq
, struct task_struct
*p
, int flags
);
646 * We are being explicitly informed that a new instance is starting,
647 * and this means that:
648 * - the absolute deadline of the entity has to be placed at
649 * current time + relative deadline;
650 * - the runtime of the entity has to be set to the maximum value.
652 * The capability of specifying such event is useful whenever a -deadline
653 * entity wants to (try to!) synchronize its behaviour with the scheduler's
654 * one, and to (try to!) reconcile itself with its own scheduling
657 static inline void setup_new_dl_entity(struct sched_dl_entity
*dl_se
)
659 struct dl_rq
*dl_rq
= dl_rq_of_se(dl_se
);
660 struct rq
*rq
= rq_of_dl_rq(dl_rq
);
662 WARN_ON(dl_se
->dl_boosted
);
663 WARN_ON(dl_time_before(rq_clock(rq
), dl_se
->deadline
));
666 * We are racing with the deadline timer. So, do nothing because
667 * the deadline timer handler will take care of properly recharging
668 * the runtime and postponing the deadline
670 if (dl_se
->dl_throttled
)
674 * We use the regular wall clock time to set deadlines in the
675 * future; in fact, we must consider execution overheads (time
676 * spent on hardirq context, etc.).
678 dl_se
->deadline
= rq_clock(rq
) + dl_se
->dl_deadline
;
679 dl_se
->runtime
= dl_se
->dl_runtime
;
683 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
684 * possibility of a entity lasting more than what it declared, and thus
685 * exhausting its runtime.
687 * Here we are interested in making runtime overrun possible, but we do
688 * not want a entity which is misbehaving to affect the scheduling of all
690 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
691 * is used, in order to confine each entity within its own bandwidth.
693 * This function deals exactly with that, and ensures that when the runtime
694 * of a entity is replenished, its deadline is also postponed. That ensures
695 * the overrunning entity can't interfere with other entity in the system and
696 * can't make them miss their deadlines. Reasons why this kind of overruns
697 * could happen are, typically, a entity voluntarily trying to overcome its
698 * runtime, or it just underestimated it during sched_setattr().
700 static void replenish_dl_entity(struct sched_dl_entity
*dl_se
,
701 struct sched_dl_entity
*pi_se
)
703 struct dl_rq
*dl_rq
= dl_rq_of_se(dl_se
);
704 struct rq
*rq
= rq_of_dl_rq(dl_rq
);
706 BUG_ON(pi_se
->dl_runtime
<= 0);
709 * This could be the case for a !-dl task that is boosted.
710 * Just go with full inherited parameters.
712 if (dl_se
->dl_deadline
== 0) {
713 dl_se
->deadline
= rq_clock(rq
) + pi_se
->dl_deadline
;
714 dl_se
->runtime
= pi_se
->dl_runtime
;
717 if (dl_se
->dl_yielded
&& dl_se
->runtime
> 0)
721 * We keep moving the deadline away until we get some
722 * available runtime for the entity. This ensures correct
723 * handling of situations where the runtime overrun is
726 while (dl_se
->runtime
<= 0) {
727 dl_se
->deadline
+= pi_se
->dl_period
;
728 dl_se
->runtime
+= pi_se
->dl_runtime
;
732 * At this point, the deadline really should be "in
733 * the future" with respect to rq->clock. If it's
734 * not, we are, for some reason, lagging too much!
735 * Anyway, after having warn userspace abut that,
736 * we still try to keep the things running by
737 * resetting the deadline and the budget of the
740 if (dl_time_before(dl_se
->deadline
, rq_clock(rq
))) {
741 printk_deferred_once("sched: DL replenish lagged too much\n");
742 dl_se
->deadline
= rq_clock(rq
) + pi_se
->dl_deadline
;
743 dl_se
->runtime
= pi_se
->dl_runtime
;
746 if (dl_se
->dl_yielded
)
747 dl_se
->dl_yielded
= 0;
748 if (dl_se
->dl_throttled
)
749 dl_se
->dl_throttled
= 0;
753 * Here we check if --at time t-- an entity (which is probably being
754 * [re]activated or, in general, enqueued) can use its remaining runtime
755 * and its current deadline _without_ exceeding the bandwidth it is
756 * assigned (function returns true if it can't). We are in fact applying
757 * one of the CBS rules: when a task wakes up, if the residual runtime
758 * over residual deadline fits within the allocated bandwidth, then we
759 * can keep the current (absolute) deadline and residual budget without
760 * disrupting the schedulability of the system. Otherwise, we should
761 * refill the runtime and set the deadline a period in the future,
762 * because keeping the current (absolute) deadline of the task would
763 * result in breaking guarantees promised to other tasks (refer to
764 * Documentation/scheduler/sched-deadline.rst for more information).
766 * This function returns true if:
768 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
770 * IOW we can't recycle current parameters.
772 * Notice that the bandwidth check is done against the deadline. For
773 * task with deadline equal to period this is the same of using
774 * dl_period instead of dl_deadline in the equation above.
776 static bool dl_entity_overflow(struct sched_dl_entity
*dl_se
,
777 struct sched_dl_entity
*pi_se
, u64 t
)
782 * left and right are the two sides of the equation above,
783 * after a bit of shuffling to use multiplications instead
786 * Note that none of the time values involved in the two
787 * multiplications are absolute: dl_deadline and dl_runtime
788 * are the relative deadline and the maximum runtime of each
789 * instance, runtime is the runtime left for the last instance
790 * and (deadline - t), since t is rq->clock, is the time left
791 * to the (absolute) deadline. Even if overflowing the u64 type
792 * is very unlikely to occur in both cases, here we scale down
793 * as we want to avoid that risk at all. Scaling down by 10
794 * means that we reduce granularity to 1us. We are fine with it,
795 * since this is only a true/false check and, anyway, thinking
796 * of anything below microseconds resolution is actually fiction
797 * (but still we want to give the user that illusion >;).
799 left
= (pi_se
->dl_deadline
>> DL_SCALE
) * (dl_se
->runtime
>> DL_SCALE
);
800 right
= ((dl_se
->deadline
- t
) >> DL_SCALE
) *
801 (pi_se
->dl_runtime
>> DL_SCALE
);
803 return dl_time_before(right
, left
);
807 * Revised wakeup rule [1]: For self-suspending tasks, rather then
808 * re-initializing task's runtime and deadline, the revised wakeup
809 * rule adjusts the task's runtime to avoid the task to overrun its
812 * Reasoning: a task may overrun the density if:
813 * runtime / (deadline - t) > dl_runtime / dl_deadline
815 * Therefore, runtime can be adjusted to:
816 * runtime = (dl_runtime / dl_deadline) * (deadline - t)
818 * In such way that runtime will be equal to the maximum density
819 * the task can use without breaking any rule.
821 * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
822 * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
825 update_dl_revised_wakeup(struct sched_dl_entity
*dl_se
, struct rq
*rq
)
827 u64 laxity
= dl_se
->deadline
- rq_clock(rq
);
830 * If the task has deadline < period, and the deadline is in the past,
831 * it should already be throttled before this check.
833 * See update_dl_entity() comments for further details.
835 WARN_ON(dl_time_before(dl_se
->deadline
, rq_clock(rq
)));
837 dl_se
->runtime
= (dl_se
->dl_density
* laxity
) >> BW_SHIFT
;
841 * Regarding the deadline, a task with implicit deadline has a relative
842 * deadline == relative period. A task with constrained deadline has a
843 * relative deadline <= relative period.
845 * We support constrained deadline tasks. However, there are some restrictions
846 * applied only for tasks which do not have an implicit deadline. See
847 * update_dl_entity() to know more about such restrictions.
849 * The dl_is_implicit() returns true if the task has an implicit deadline.
851 static inline bool dl_is_implicit(struct sched_dl_entity
*dl_se
)
853 return dl_se
->dl_deadline
== dl_se
->dl_period
;
857 * When a deadline entity is placed in the runqueue, its runtime and deadline
858 * might need to be updated. This is done by a CBS wake up rule. There are two
859 * different rules: 1) the original CBS; and 2) the Revisited CBS.
861 * When the task is starting a new period, the Original CBS is used. In this
862 * case, the runtime is replenished and a new absolute deadline is set.
864 * When a task is queued before the begin of the next period, using the
865 * remaining runtime and deadline could make the entity to overflow, see
866 * dl_entity_overflow() to find more about runtime overflow. When such case
867 * is detected, the runtime and deadline need to be updated.
869 * If the task has an implicit deadline, i.e., deadline == period, the Original
870 * CBS is applied. the runtime is replenished and a new absolute deadline is
871 * set, as in the previous cases.
873 * However, the Original CBS does not work properly for tasks with
874 * deadline < period, which are said to have a constrained deadline. By
875 * applying the Original CBS, a constrained deadline task would be able to run
876 * runtime/deadline in a period. With deadline < period, the task would
877 * overrun the runtime/period allowed bandwidth, breaking the admission test.
879 * In order to prevent this misbehave, the Revisited CBS is used for
880 * constrained deadline tasks when a runtime overflow is detected. In the
881 * Revisited CBS, rather than replenishing & setting a new absolute deadline,
882 * the remaining runtime of the task is reduced to avoid runtime overflow.
883 * Please refer to the comments update_dl_revised_wakeup() function to find
884 * more about the Revised CBS rule.
886 static void update_dl_entity(struct sched_dl_entity
*dl_se
,
887 struct sched_dl_entity
*pi_se
)
889 struct dl_rq
*dl_rq
= dl_rq_of_se(dl_se
);
890 struct rq
*rq
= rq_of_dl_rq(dl_rq
);
892 if (dl_time_before(dl_se
->deadline
, rq_clock(rq
)) ||
893 dl_entity_overflow(dl_se
, pi_se
, rq_clock(rq
))) {
895 if (unlikely(!dl_is_implicit(dl_se
) &&
896 !dl_time_before(dl_se
->deadline
, rq_clock(rq
)) &&
897 !dl_se
->dl_boosted
)){
898 update_dl_revised_wakeup(dl_se
, rq
);
902 dl_se
->deadline
= rq_clock(rq
) + pi_se
->dl_deadline
;
903 dl_se
->runtime
= pi_se
->dl_runtime
;
907 static inline u64
dl_next_period(struct sched_dl_entity
*dl_se
)
909 return dl_se
->deadline
- dl_se
->dl_deadline
+ dl_se
->dl_period
;
913 * If the entity depleted all its runtime, and if we want it to sleep
914 * while waiting for some new execution time to become available, we
915 * set the bandwidth replenishment timer to the replenishment instant
916 * and try to activate it.
918 * Notice that it is important for the caller to know if the timer
919 * actually started or not (i.e., the replenishment instant is in
920 * the future or in the past).
922 static int start_dl_timer(struct task_struct
*p
)
924 struct sched_dl_entity
*dl_se
= &p
->dl
;
925 struct hrtimer
*timer
= &dl_se
->dl_timer
;
926 struct rq
*rq
= task_rq(p
);
930 lockdep_assert_held(&rq
->lock
);
933 * We want the timer to fire at the deadline, but considering
934 * that it is actually coming from rq->clock and not from
935 * hrtimer's time base reading.
937 act
= ns_to_ktime(dl_next_period(dl_se
));
938 now
= hrtimer_cb_get_time(timer
);
939 delta
= ktime_to_ns(now
) - rq_clock(rq
);
940 act
= ktime_add_ns(act
, delta
);
943 * If the expiry time already passed, e.g., because the value
944 * chosen as the deadline is too small, don't even try to
945 * start the timer in the past!
947 if (ktime_us_delta(act
, now
) < 0)
951 * !enqueued will guarantee another callback; even if one is already in
952 * progress. This ensures a balanced {get,put}_task_struct().
954 * The race against __run_timer() clearing the enqueued state is
955 * harmless because we're holding task_rq()->lock, therefore the timer
956 * expiring after we've done the check will wait on its task_rq_lock()
957 * and observe our state.
959 if (!hrtimer_is_queued(timer
)) {
961 hrtimer_start(timer
, act
, HRTIMER_MODE_ABS_HARD
);
968 * This is the bandwidth enforcement timer callback. If here, we know
969 * a task is not on its dl_rq, since the fact that the timer was running
970 * means the task is throttled and needs a runtime replenishment.
972 * However, what we actually do depends on the fact the task is active,
973 * (it is on its rq) or has been removed from there by a call to
974 * dequeue_task_dl(). In the former case we must issue the runtime
975 * replenishment and add the task back to the dl_rq; in the latter, we just
976 * do nothing but clearing dl_throttled, so that runtime and deadline
977 * updating (and the queueing back to dl_rq) will be done by the
978 * next call to enqueue_task_dl().
980 static enum hrtimer_restart
dl_task_timer(struct hrtimer
*timer
)
982 struct sched_dl_entity
*dl_se
= container_of(timer
,
983 struct sched_dl_entity
,
985 struct task_struct
*p
= dl_task_of(dl_se
);
989 rq
= task_rq_lock(p
, &rf
);
992 * The task might have changed its scheduling policy to something
993 * different than SCHED_DEADLINE (through switched_from_dl()).
999 * The task might have been boosted by someone else and might be in the
1000 * boosting/deboosting path, its not throttled.
1002 if (dl_se
->dl_boosted
)
1006 * Spurious timer due to start_dl_timer() race; or we already received
1007 * a replenishment from rt_mutex_setprio().
1009 if (!dl_se
->dl_throttled
)
1013 update_rq_clock(rq
);
1016 * If the throttle happened during sched-out; like:
1023 * __dequeue_task_dl()
1026 * We can be both throttled and !queued. Replenish the counter
1027 * but do not enqueue -- wait for our wakeup to do that.
1029 if (!task_on_rq_queued(p
)) {
1030 replenish_dl_entity(dl_se
, dl_se
);
1035 if (unlikely(!rq
->online
)) {
1037 * If the runqueue is no longer available, migrate the
1038 * task elsewhere. This necessarily changes rq.
1040 lockdep_unpin_lock(&rq
->lock
, rf
.cookie
);
1041 rq
= dl_task_offline_migration(rq
, p
);
1042 rf
.cookie
= lockdep_pin_lock(&rq
->lock
);
1043 update_rq_clock(rq
);
1046 * Now that the task has been migrated to the new RQ and we
1047 * have that locked, proceed as normal and enqueue the task
1053 enqueue_task_dl(rq
, p
, ENQUEUE_REPLENISH
);
1054 if (dl_task(rq
->curr
))
1055 check_preempt_curr_dl(rq
, p
, 0);
1061 * Queueing this task back might have overloaded rq, check if we need
1062 * to kick someone away.
1064 if (has_pushable_dl_tasks(rq
)) {
1066 * Nothing relies on rq->lock after this, so its safe to drop
1069 rq_unpin_lock(rq
, &rf
);
1071 rq_repin_lock(rq
, &rf
);
1076 task_rq_unlock(rq
, p
, &rf
);
1079 * This can free the task_struct, including this hrtimer, do not touch
1080 * anything related to that after this.
1084 return HRTIMER_NORESTART
;
1087 void init_dl_task_timer(struct sched_dl_entity
*dl_se
)
1089 struct hrtimer
*timer
= &dl_se
->dl_timer
;
1091 hrtimer_init(timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL_HARD
);
1092 timer
->function
= dl_task_timer
;
1096 * During the activation, CBS checks if it can reuse the current task's
1097 * runtime and period. If the deadline of the task is in the past, CBS
1098 * cannot use the runtime, and so it replenishes the task. This rule
1099 * works fine for implicit deadline tasks (deadline == period), and the
1100 * CBS was designed for implicit deadline tasks. However, a task with
1101 * constrained deadline (deadine < period) might be awakened after the
1102 * deadline, but before the next period. In this case, replenishing the
1103 * task would allow it to run for runtime / deadline. As in this case
1104 * deadline < period, CBS enables a task to run for more than the
1105 * runtime / period. In a very loaded system, this can cause a domino
1106 * effect, making other tasks miss their deadlines.
1108 * To avoid this problem, in the activation of a constrained deadline
1109 * task after the deadline but before the next period, throttle the
1110 * task and set the replenishing timer to the begin of the next period,
1111 * unless it is boosted.
1113 static inline void dl_check_constrained_dl(struct sched_dl_entity
*dl_se
)
1115 struct task_struct
*p
= dl_task_of(dl_se
);
1116 struct rq
*rq
= rq_of_dl_rq(dl_rq_of_se(dl_se
));
1118 if (dl_time_before(dl_se
->deadline
, rq_clock(rq
)) &&
1119 dl_time_before(rq_clock(rq
), dl_next_period(dl_se
))) {
1120 if (unlikely(dl_se
->dl_boosted
|| !start_dl_timer(p
)))
1122 dl_se
->dl_throttled
= 1;
1123 if (dl_se
->runtime
> 0)
1129 int dl_runtime_exceeded(struct sched_dl_entity
*dl_se
)
1131 return (dl_se
->runtime
<= 0);
1134 extern bool sched_rt_bandwidth_account(struct rt_rq
*rt_rq
);
1137 * This function implements the GRUB accounting rule:
1138 * according to the GRUB reclaiming algorithm, the runtime is
1139 * not decreased as "dq = -dt", but as
1140 * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt",
1141 * where u is the utilization of the task, Umax is the maximum reclaimable
1142 * utilization, Uinact is the (per-runqueue) inactive utilization, computed
1143 * as the difference between the "total runqueue utilization" and the
1144 * runqueue active utilization, and Uextra is the (per runqueue) extra
1145 * reclaimable utilization.
1146 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations
1147 * multiplied by 2^BW_SHIFT, the result has to be shifted right by
1149 * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT,
1150 * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
1151 * Since delta is a 64 bit variable, to have an overflow its value
1152 * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds.
1153 * So, overflow is not an issue here.
1155 static u64
grub_reclaim(u64 delta
, struct rq
*rq
, struct sched_dl_entity
*dl_se
)
1157 u64 u_inact
= rq
->dl
.this_bw
- rq
->dl
.running_bw
; /* Utot - Uact */
1159 u64 u_act_min
= (dl_se
->dl_bw
* rq
->dl
.bw_ratio
) >> RATIO_SHIFT
;
1162 * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)},
1163 * we compare u_inact + rq->dl.extra_bw with
1164 * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because
1165 * u_inact + rq->dl.extra_bw can be larger than
1166 * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative
1167 * leading to wrong results)
1169 if (u_inact
+ rq
->dl
.extra_bw
> BW_UNIT
- u_act_min
)
1172 u_act
= BW_UNIT
- u_inact
- rq
->dl
.extra_bw
;
1174 return (delta
* u_act
) >> BW_SHIFT
;
1178 * Update the current task's runtime statistics (provided it is still
1179 * a -deadline task and has not been removed from the dl_rq).
1181 static void update_curr_dl(struct rq
*rq
)
1183 struct task_struct
*curr
= rq
->curr
;
1184 struct sched_dl_entity
*dl_se
= &curr
->dl
;
1185 u64 delta_exec
, scaled_delta_exec
;
1186 int cpu
= cpu_of(rq
);
1189 if (!dl_task(curr
) || !on_dl_rq(dl_se
))
1193 * Consumed budget is computed considering the time as
1194 * observed by schedulable tasks (excluding time spent
1195 * in hardirq context, etc.). Deadlines are instead
1196 * computed using hard walltime. This seems to be the more
1197 * natural solution, but the full ramifications of this
1198 * approach need further study.
1200 now
= rq_clock_task(rq
);
1201 delta_exec
= now
- curr
->se
.exec_start
;
1202 if (unlikely((s64
)delta_exec
<= 0)) {
1203 if (unlikely(dl_se
->dl_yielded
))
1208 schedstat_set(curr
->se
.statistics
.exec_max
,
1209 max(curr
->se
.statistics
.exec_max
, delta_exec
));
1211 curr
->se
.sum_exec_runtime
+= delta_exec
;
1212 account_group_exec_runtime(curr
, delta_exec
);
1214 curr
->se
.exec_start
= now
;
1215 cgroup_account_cputime(curr
, delta_exec
);
1217 if (dl_entity_is_special(dl_se
))
1221 * For tasks that participate in GRUB, we implement GRUB-PA: the
1222 * spare reclaimed bandwidth is used to clock down frequency.
1224 * For the others, we still need to scale reservation parameters
1225 * according to current frequency and CPU maximum capacity.
1227 if (unlikely(dl_se
->flags
& SCHED_FLAG_RECLAIM
)) {
1228 scaled_delta_exec
= grub_reclaim(delta_exec
,
1232 unsigned long scale_freq
= arch_scale_freq_capacity(cpu
);
1233 unsigned long scale_cpu
= arch_scale_cpu_capacity(cpu
);
1235 scaled_delta_exec
= cap_scale(delta_exec
, scale_freq
);
1236 scaled_delta_exec
= cap_scale(scaled_delta_exec
, scale_cpu
);
1239 dl_se
->runtime
-= scaled_delta_exec
;
1242 if (dl_runtime_exceeded(dl_se
) || dl_se
->dl_yielded
) {
1243 dl_se
->dl_throttled
= 1;
1245 /* If requested, inform the user about runtime overruns. */
1246 if (dl_runtime_exceeded(dl_se
) &&
1247 (dl_se
->flags
& SCHED_FLAG_DL_OVERRUN
))
1248 dl_se
->dl_overrun
= 1;
1250 __dequeue_task_dl(rq
, curr
, 0);
1251 if (unlikely(dl_se
->dl_boosted
|| !start_dl_timer(curr
)))
1252 enqueue_task_dl(rq
, curr
, ENQUEUE_REPLENISH
);
1254 if (!is_leftmost(curr
, &rq
->dl
))
1259 * Because -- for now -- we share the rt bandwidth, we need to
1260 * account our runtime there too, otherwise actual rt tasks
1261 * would be able to exceed the shared quota.
1263 * Account to the root rt group for now.
1265 * The solution we're working towards is having the RT groups scheduled
1266 * using deadline servers -- however there's a few nasties to figure
1267 * out before that can happen.
1269 if (rt_bandwidth_enabled()) {
1270 struct rt_rq
*rt_rq
= &rq
->rt
;
1272 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
1274 * We'll let actual RT tasks worry about the overflow here, we
1275 * have our own CBS to keep us inline; only account when RT
1276 * bandwidth is relevant.
1278 if (sched_rt_bandwidth_account(rt_rq
))
1279 rt_rq
->rt_time
+= delta_exec
;
1280 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
1284 static enum hrtimer_restart
inactive_task_timer(struct hrtimer
*timer
)
1286 struct sched_dl_entity
*dl_se
= container_of(timer
,
1287 struct sched_dl_entity
,
1289 struct task_struct
*p
= dl_task_of(dl_se
);
1293 rq
= task_rq_lock(p
, &rf
);
1296 update_rq_clock(rq
);
1298 if (!dl_task(p
) || p
->state
== TASK_DEAD
) {
1299 struct dl_bw
*dl_b
= dl_bw_of(task_cpu(p
));
1301 if (p
->state
== TASK_DEAD
&& dl_se
->dl_non_contending
) {
1302 sub_running_bw(&p
->dl
, dl_rq_of_se(&p
->dl
));
1303 sub_rq_bw(&p
->dl
, dl_rq_of_se(&p
->dl
));
1304 dl_se
->dl_non_contending
= 0;
1307 raw_spin_lock(&dl_b
->lock
);
1308 __dl_sub(dl_b
, p
->dl
.dl_bw
, dl_bw_cpus(task_cpu(p
)));
1309 raw_spin_unlock(&dl_b
->lock
);
1310 __dl_clear_params(p
);
1314 if (dl_se
->dl_non_contending
== 0)
1317 sub_running_bw(dl_se
, &rq
->dl
);
1318 dl_se
->dl_non_contending
= 0;
1320 task_rq_unlock(rq
, p
, &rf
);
1323 return HRTIMER_NORESTART
;
1326 void init_dl_inactive_task_timer(struct sched_dl_entity
*dl_se
)
1328 struct hrtimer
*timer
= &dl_se
->inactive_timer
;
1330 hrtimer_init(timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL_HARD
);
1331 timer
->function
= inactive_task_timer
;
1336 static void inc_dl_deadline(struct dl_rq
*dl_rq
, u64 deadline
)
1338 struct rq
*rq
= rq_of_dl_rq(dl_rq
);
1340 if (dl_rq
->earliest_dl
.curr
== 0 ||
1341 dl_time_before(deadline
, dl_rq
->earliest_dl
.curr
)) {
1342 dl_rq
->earliest_dl
.curr
= deadline
;
1343 cpudl_set(&rq
->rd
->cpudl
, rq
->cpu
, deadline
);
1347 static void dec_dl_deadline(struct dl_rq
*dl_rq
, u64 deadline
)
1349 struct rq
*rq
= rq_of_dl_rq(dl_rq
);
1352 * Since we may have removed our earliest (and/or next earliest)
1353 * task we must recompute them.
1355 if (!dl_rq
->dl_nr_running
) {
1356 dl_rq
->earliest_dl
.curr
= 0;
1357 dl_rq
->earliest_dl
.next
= 0;
1358 cpudl_clear(&rq
->rd
->cpudl
, rq
->cpu
);
1360 struct rb_node
*leftmost
= dl_rq
->root
.rb_leftmost
;
1361 struct sched_dl_entity
*entry
;
1363 entry
= rb_entry(leftmost
, struct sched_dl_entity
, rb_node
);
1364 dl_rq
->earliest_dl
.curr
= entry
->deadline
;
1365 cpudl_set(&rq
->rd
->cpudl
, rq
->cpu
, entry
->deadline
);
1371 static inline void inc_dl_deadline(struct dl_rq
*dl_rq
, u64 deadline
) {}
1372 static inline void dec_dl_deadline(struct dl_rq
*dl_rq
, u64 deadline
) {}
1374 #endif /* CONFIG_SMP */
1377 void inc_dl_tasks(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
1379 int prio
= dl_task_of(dl_se
)->prio
;
1380 u64 deadline
= dl_se
->deadline
;
1382 WARN_ON(!dl_prio(prio
));
1383 dl_rq
->dl_nr_running
++;
1384 add_nr_running(rq_of_dl_rq(dl_rq
), 1);
1386 inc_dl_deadline(dl_rq
, deadline
);
1387 inc_dl_migration(dl_se
, dl_rq
);
1391 void dec_dl_tasks(struct sched_dl_entity
*dl_se
, struct dl_rq
*dl_rq
)
1393 int prio
= dl_task_of(dl_se
)->prio
;
1395 WARN_ON(!dl_prio(prio
));
1396 WARN_ON(!dl_rq
->dl_nr_running
);
1397 dl_rq
->dl_nr_running
--;
1398 sub_nr_running(rq_of_dl_rq(dl_rq
), 1);
1400 dec_dl_deadline(dl_rq
, dl_se
->deadline
);
1401 dec_dl_migration(dl_se
, dl_rq
);
1404 static void __enqueue_dl_entity(struct sched_dl_entity
*dl_se
)
1406 struct dl_rq
*dl_rq
= dl_rq_of_se(dl_se
);
1407 struct rb_node
**link
= &dl_rq
->root
.rb_root
.rb_node
;
1408 struct rb_node
*parent
= NULL
;
1409 struct sched_dl_entity
*entry
;
1412 BUG_ON(!RB_EMPTY_NODE(&dl_se
->rb_node
));
1416 entry
= rb_entry(parent
, struct sched_dl_entity
, rb_node
);
1417 if (dl_time_before(dl_se
->deadline
, entry
->deadline
))
1418 link
= &parent
->rb_left
;
1420 link
= &parent
->rb_right
;
1425 rb_link_node(&dl_se
->rb_node
, parent
, link
);
1426 rb_insert_color_cached(&dl_se
->rb_node
, &dl_rq
->root
, leftmost
);
1428 inc_dl_tasks(dl_se
, dl_rq
);
1431 static void __dequeue_dl_entity(struct sched_dl_entity
*dl_se
)
1433 struct dl_rq
*dl_rq
= dl_rq_of_se(dl_se
);
1435 if (RB_EMPTY_NODE(&dl_se
->rb_node
))
1438 rb_erase_cached(&dl_se
->rb_node
, &dl_rq
->root
);
1439 RB_CLEAR_NODE(&dl_se
->rb_node
);
1441 dec_dl_tasks(dl_se
, dl_rq
);
1445 enqueue_dl_entity(struct sched_dl_entity
*dl_se
,
1446 struct sched_dl_entity
*pi_se
, int flags
)
1448 BUG_ON(on_dl_rq(dl_se
));
1451 * If this is a wakeup or a new instance, the scheduling
1452 * parameters of the task might need updating. Otherwise,
1453 * we want a replenishment of its runtime.
1455 if (flags
& ENQUEUE_WAKEUP
) {
1456 task_contending(dl_se
, flags
);
1457 update_dl_entity(dl_se
, pi_se
);
1458 } else if (flags
& ENQUEUE_REPLENISH
) {
1459 replenish_dl_entity(dl_se
, pi_se
);
1460 } else if ((flags
& ENQUEUE_RESTORE
) &&
1461 dl_time_before(dl_se
->deadline
,
1462 rq_clock(rq_of_dl_rq(dl_rq_of_se(dl_se
))))) {
1463 setup_new_dl_entity(dl_se
);
1466 __enqueue_dl_entity(dl_se
);
1469 static void dequeue_dl_entity(struct sched_dl_entity
*dl_se
)
1471 __dequeue_dl_entity(dl_se
);
1474 static void enqueue_task_dl(struct rq
*rq
, struct task_struct
*p
, int flags
)
1476 struct task_struct
*pi_task
= rt_mutex_get_top_task(p
);
1477 struct sched_dl_entity
*pi_se
= &p
->dl
;
1480 * Use the scheduling parameters of the top pi-waiter task if:
1481 * - we have a top pi-waiter which is a SCHED_DEADLINE task AND
1482 * - our dl_boosted is set (i.e. the pi-waiter's (absolute) deadline is
1483 * smaller than our deadline OR we are a !SCHED_DEADLINE task getting
1484 * boosted due to a SCHED_DEADLINE pi-waiter).
1485 * Otherwise we keep our runtime and deadline.
1487 if (pi_task
&& dl_prio(pi_task
->normal_prio
) && p
->dl
.dl_boosted
) {
1488 pi_se
= &pi_task
->dl
;
1489 } else if (!dl_prio(p
->normal_prio
)) {
1491 * Special case in which we have a !SCHED_DEADLINE task
1492 * that is going to be deboosted, but exceeds its
1493 * runtime while doing so. No point in replenishing
1494 * it, as it's going to return back to its original
1495 * scheduling class after this.
1497 BUG_ON(!p
->dl
.dl_boosted
|| flags
!= ENQUEUE_REPLENISH
);
1502 * Check if a constrained deadline task was activated
1503 * after the deadline but before the next period.
1504 * If that is the case, the task will be throttled and
1505 * the replenishment timer will be set to the next period.
1507 if (!p
->dl
.dl_throttled
&& !dl_is_implicit(&p
->dl
))
1508 dl_check_constrained_dl(&p
->dl
);
1510 if (p
->on_rq
== TASK_ON_RQ_MIGRATING
|| flags
& ENQUEUE_RESTORE
) {
1511 add_rq_bw(&p
->dl
, &rq
->dl
);
1512 add_running_bw(&p
->dl
, &rq
->dl
);
1516 * If p is throttled, we do not enqueue it. In fact, if it exhausted
1517 * its budget it needs a replenishment and, since it now is on
1518 * its rq, the bandwidth timer callback (which clearly has not
1519 * run yet) will take care of this.
1520 * However, the active utilization does not depend on the fact
1521 * that the task is on the runqueue or not (but depends on the
1522 * task's state - in GRUB parlance, "inactive" vs "active contending").
1523 * In other words, even if a task is throttled its utilization must
1524 * be counted in the active utilization; hence, we need to call
1527 if (p
->dl
.dl_throttled
&& !(flags
& ENQUEUE_REPLENISH
)) {
1528 if (flags
& ENQUEUE_WAKEUP
)
1529 task_contending(&p
->dl
, flags
);
1534 enqueue_dl_entity(&p
->dl
, pi_se
, flags
);
1536 if (!task_current(rq
, p
) && p
->nr_cpus_allowed
> 1)
1537 enqueue_pushable_dl_task(rq
, p
);
1540 static void __dequeue_task_dl(struct rq
*rq
, struct task_struct
*p
, int flags
)
1542 dequeue_dl_entity(&p
->dl
);
1543 dequeue_pushable_dl_task(rq
, p
);
1546 static void dequeue_task_dl(struct rq
*rq
, struct task_struct
*p
, int flags
)
1549 __dequeue_task_dl(rq
, p
, flags
);
1551 if (p
->on_rq
== TASK_ON_RQ_MIGRATING
|| flags
& DEQUEUE_SAVE
) {
1552 sub_running_bw(&p
->dl
, &rq
->dl
);
1553 sub_rq_bw(&p
->dl
, &rq
->dl
);
1557 * This check allows to start the inactive timer (or to immediately
1558 * decrease the active utilization, if needed) in two cases:
1559 * when the task blocks and when it is terminating
1560 * (p->state == TASK_DEAD). We can handle the two cases in the same
1561 * way, because from GRUB's point of view the same thing is happening
1562 * (the task moves from "active contending" to "active non contending"
1565 if (flags
& DEQUEUE_SLEEP
)
1566 task_non_contending(p
);
1570 * Yield task semantic for -deadline tasks is:
1572 * get off from the CPU until our next instance, with
1573 * a new runtime. This is of little use now, since we
1574 * don't have a bandwidth reclaiming mechanism. Anyway,
1575 * bandwidth reclaiming is planned for the future, and
1576 * yield_task_dl will indicate that some spare budget
1577 * is available for other task instances to use it.
1579 static void yield_task_dl(struct rq
*rq
)
1582 * We make the task go to sleep until its current deadline by
1583 * forcing its runtime to zero. This way, update_curr_dl() stops
1584 * it and the bandwidth timer will wake it up and will give it
1585 * new scheduling parameters (thanks to dl_yielded=1).
1587 rq
->curr
->dl
.dl_yielded
= 1;
1589 update_rq_clock(rq
);
1592 * Tell update_rq_clock() that we've just updated,
1593 * so we don't do microscopic update in schedule()
1594 * and double the fastpath cost.
1596 rq_clock_skip_update(rq
);
1601 static int find_later_rq(struct task_struct
*task
);
1604 select_task_rq_dl(struct task_struct
*p
, int cpu
, int sd_flag
, int flags
)
1606 struct task_struct
*curr
;
1609 if (sd_flag
!= SD_BALANCE_WAKE
)
1615 curr
= READ_ONCE(rq
->curr
); /* unlocked access */
1618 * If we are dealing with a -deadline task, we must
1619 * decide where to wake it up.
1620 * If it has a later deadline and the current task
1621 * on this rq can't move (provided the waking task
1622 * can!) we prefer to send it somewhere else. On the
1623 * other hand, if it has a shorter deadline, we
1624 * try to make it stay here, it might be important.
1626 if (unlikely(dl_task(curr
)) &&
1627 (curr
->nr_cpus_allowed
< 2 ||
1628 !dl_entity_preempt(&p
->dl
, &curr
->dl
)) &&
1629 (p
->nr_cpus_allowed
> 1)) {
1630 int target
= find_later_rq(p
);
1633 (dl_time_before(p
->dl
.deadline
,
1634 cpu_rq(target
)->dl
.earliest_dl
.curr
) ||
1635 (cpu_rq(target
)->dl
.dl_nr_running
== 0)))
1644 static void migrate_task_rq_dl(struct task_struct
*p
, int new_cpu __maybe_unused
)
1648 if (p
->state
!= TASK_WAKING
)
1653 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1654 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1655 * rq->lock is not... So, lock it
1657 raw_spin_lock(&rq
->lock
);
1658 if (p
->dl
.dl_non_contending
) {
1659 sub_running_bw(&p
->dl
, &rq
->dl
);
1660 p
->dl
.dl_non_contending
= 0;
1662 * If the timer handler is currently running and the
1663 * timer cannot be cancelled, inactive_task_timer()
1664 * will see that dl_not_contending is not set, and
1665 * will not touch the rq's active utilization,
1666 * so we are still safe.
1668 if (hrtimer_try_to_cancel(&p
->dl
.inactive_timer
) == 1)
1671 sub_rq_bw(&p
->dl
, &rq
->dl
);
1672 raw_spin_unlock(&rq
->lock
);
1675 static void check_preempt_equal_dl(struct rq
*rq
, struct task_struct
*p
)
1678 * Current can't be migrated, useless to reschedule,
1679 * let's hope p can move out.
1681 if (rq
->curr
->nr_cpus_allowed
== 1 ||
1682 !cpudl_find(&rq
->rd
->cpudl
, rq
->curr
, NULL
))
1686 * p is migratable, so let's not schedule it and
1687 * see if it is pushed or pulled somewhere else.
1689 if (p
->nr_cpus_allowed
!= 1 &&
1690 cpudl_find(&rq
->rd
->cpudl
, p
, NULL
))
1696 static int balance_dl(struct rq
*rq
, struct task_struct
*p
, struct rq_flags
*rf
)
1698 if (!on_dl_rq(&p
->dl
) && need_pull_dl_task(rq
, p
)) {
1700 * This is OK, because current is on_cpu, which avoids it being
1701 * picked for load-balance and preemption/IRQs are still
1702 * disabled avoiding further scheduler activity on it and we've
1703 * not yet started the picking loop.
1705 rq_unpin_lock(rq
, rf
);
1707 rq_repin_lock(rq
, rf
);
1710 return sched_stop_runnable(rq
) || sched_dl_runnable(rq
);
1712 #endif /* CONFIG_SMP */
1715 * Only called when both the current and waking task are -deadline
1718 static void check_preempt_curr_dl(struct rq
*rq
, struct task_struct
*p
,
1721 if (dl_entity_preempt(&p
->dl
, &rq
->curr
->dl
)) {
1728 * In the unlikely case current and p have the same deadline
1729 * let us try to decide what's the best thing to do...
1731 if ((p
->dl
.deadline
== rq
->curr
->dl
.deadline
) &&
1732 !test_tsk_need_resched(rq
->curr
))
1733 check_preempt_equal_dl(rq
, p
);
1734 #endif /* CONFIG_SMP */
1737 #ifdef CONFIG_SCHED_HRTICK
1738 static void start_hrtick_dl(struct rq
*rq
, struct task_struct
*p
)
1740 hrtick_start(rq
, p
->dl
.runtime
);
1742 #else /* !CONFIG_SCHED_HRTICK */
1743 static void start_hrtick_dl(struct rq
*rq
, struct task_struct
*p
)
1748 static void set_next_task_dl(struct rq
*rq
, struct task_struct
*p
, bool first
)
1750 p
->se
.exec_start
= rq_clock_task(rq
);
1752 /* You can't push away the running task */
1753 dequeue_pushable_dl_task(rq
, p
);
1758 if (hrtick_enabled(rq
))
1759 start_hrtick_dl(rq
, p
);
1761 if (rq
->curr
->sched_class
!= &dl_sched_class
)
1762 update_dl_rq_load_avg(rq_clock_pelt(rq
), rq
, 0);
1764 deadline_queue_push_tasks(rq
);
1767 static struct sched_dl_entity
*pick_next_dl_entity(struct rq
*rq
,
1768 struct dl_rq
*dl_rq
)
1770 struct rb_node
*left
= rb_first_cached(&dl_rq
->root
);
1775 return rb_entry(left
, struct sched_dl_entity
, rb_node
);
1778 static struct task_struct
*pick_next_task_dl(struct rq
*rq
)
1780 struct sched_dl_entity
*dl_se
;
1781 struct dl_rq
*dl_rq
= &rq
->dl
;
1782 struct task_struct
*p
;
1784 if (!sched_dl_runnable(rq
))
1787 dl_se
= pick_next_dl_entity(rq
, dl_rq
);
1789 p
= dl_task_of(dl_se
);
1790 set_next_task_dl(rq
, p
, true);
1794 static void put_prev_task_dl(struct rq
*rq
, struct task_struct
*p
)
1798 update_dl_rq_load_avg(rq_clock_pelt(rq
), rq
, 1);
1799 if (on_dl_rq(&p
->dl
) && p
->nr_cpus_allowed
> 1)
1800 enqueue_pushable_dl_task(rq
, p
);
1804 * scheduler tick hitting a task of our scheduling class.
1806 * NOTE: This function can be called remotely by the tick offload that
1807 * goes along full dynticks. Therefore no local assumption can be made
1808 * and everything must be accessed through the @rq and @curr passed in
1811 static void task_tick_dl(struct rq
*rq
, struct task_struct
*p
, int queued
)
1815 update_dl_rq_load_avg(rq_clock_pelt(rq
), rq
, 1);
1817 * Even when we have runtime, update_curr_dl() might have resulted in us
1818 * not being the leftmost task anymore. In that case NEED_RESCHED will
1819 * be set and schedule() will start a new hrtick for the next task.
1821 if (hrtick_enabled(rq
) && queued
&& p
->dl
.runtime
> 0 &&
1822 is_leftmost(p
, &rq
->dl
))
1823 start_hrtick_dl(rq
, p
);
1826 static void task_fork_dl(struct task_struct
*p
)
1829 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1836 /* Only try algorithms three times */
1837 #define DL_MAX_TRIES 3
1839 static int pick_dl_task(struct rq
*rq
, struct task_struct
*p
, int cpu
)
1841 if (!task_running(rq
, p
) &&
1842 cpumask_test_cpu(cpu
, p
->cpus_ptr
))
1848 * Return the earliest pushable rq's task, which is suitable to be executed
1849 * on the CPU, NULL otherwise:
1851 static struct task_struct
*pick_earliest_pushable_dl_task(struct rq
*rq
, int cpu
)
1853 struct rb_node
*next_node
= rq
->dl
.pushable_dl_tasks_root
.rb_leftmost
;
1854 struct task_struct
*p
= NULL
;
1856 if (!has_pushable_dl_tasks(rq
))
1861 p
= rb_entry(next_node
, struct task_struct
, pushable_dl_tasks
);
1863 if (pick_dl_task(rq
, p
, cpu
))
1866 next_node
= rb_next(next_node
);
1873 static DEFINE_PER_CPU(cpumask_var_t
, local_cpu_mask_dl
);
1875 static int find_later_rq(struct task_struct
*task
)
1877 struct sched_domain
*sd
;
1878 struct cpumask
*later_mask
= this_cpu_cpumask_var_ptr(local_cpu_mask_dl
);
1879 int this_cpu
= smp_processor_id();
1880 int cpu
= task_cpu(task
);
1882 /* Make sure the mask is initialized first */
1883 if (unlikely(!later_mask
))
1886 if (task
->nr_cpus_allowed
== 1)
1890 * We have to consider system topology and task affinity
1891 * first, then we can look for a suitable CPU.
1893 if (!cpudl_find(&task_rq(task
)->rd
->cpudl
, task
, later_mask
))
1897 * If we are here, some targets have been found, including
1898 * the most suitable which is, among the runqueues where the
1899 * current tasks have later deadlines than the task's one, the
1900 * rq with the latest possible one.
1902 * Now we check how well this matches with task's
1903 * affinity and system topology.
1905 * The last CPU where the task run is our first
1906 * guess, since it is most likely cache-hot there.
1908 if (cpumask_test_cpu(cpu
, later_mask
))
1911 * Check if this_cpu is to be skipped (i.e., it is
1912 * not in the mask) or not.
1914 if (!cpumask_test_cpu(this_cpu
, later_mask
))
1918 for_each_domain(cpu
, sd
) {
1919 if (sd
->flags
& SD_WAKE_AFFINE
) {
1923 * If possible, preempting this_cpu is
1924 * cheaper than migrating.
1926 if (this_cpu
!= -1 &&
1927 cpumask_test_cpu(this_cpu
, sched_domain_span(sd
))) {
1932 best_cpu
= cpumask_first_and(later_mask
,
1933 sched_domain_span(sd
));
1935 * Last chance: if a CPU being in both later_mask
1936 * and current sd span is valid, that becomes our
1937 * choice. Of course, the latest possible CPU is
1938 * already under consideration through later_mask.
1940 if (best_cpu
< nr_cpu_ids
) {
1949 * At this point, all our guesses failed, we just return
1950 * 'something', and let the caller sort the things out.
1955 cpu
= cpumask_any(later_mask
);
1956 if (cpu
< nr_cpu_ids
)
1962 /* Locks the rq it finds */
1963 static struct rq
*find_lock_later_rq(struct task_struct
*task
, struct rq
*rq
)
1965 struct rq
*later_rq
= NULL
;
1969 for (tries
= 0; tries
< DL_MAX_TRIES
; tries
++) {
1970 cpu
= find_later_rq(task
);
1972 if ((cpu
== -1) || (cpu
== rq
->cpu
))
1975 later_rq
= cpu_rq(cpu
);
1977 if (later_rq
->dl
.dl_nr_running
&&
1978 !dl_time_before(task
->dl
.deadline
,
1979 later_rq
->dl
.earliest_dl
.curr
)) {
1981 * Target rq has tasks of equal or earlier deadline,
1982 * retrying does not release any lock and is unlikely
1983 * to yield a different result.
1989 /* Retry if something changed. */
1990 if (double_lock_balance(rq
, later_rq
)) {
1991 if (unlikely(task_rq(task
) != rq
||
1992 !cpumask_test_cpu(later_rq
->cpu
, task
->cpus_ptr
) ||
1993 task_running(rq
, task
) ||
1995 !task_on_rq_queued(task
))) {
1996 double_unlock_balance(rq
, later_rq
);
2003 * If the rq we found has no -deadline task, or
2004 * its earliest one has a later deadline than our
2005 * task, the rq is a good one.
2007 if (!later_rq
->dl
.dl_nr_running
||
2008 dl_time_before(task
->dl
.deadline
,
2009 later_rq
->dl
.earliest_dl
.curr
))
2012 /* Otherwise we try again. */
2013 double_unlock_balance(rq
, later_rq
);
2020 static struct task_struct
*pick_next_pushable_dl_task(struct rq
*rq
)
2022 struct task_struct
*p
;
2024 if (!has_pushable_dl_tasks(rq
))
2027 p
= rb_entry(rq
->dl
.pushable_dl_tasks_root
.rb_leftmost
,
2028 struct task_struct
, pushable_dl_tasks
);
2030 BUG_ON(rq
->cpu
!= task_cpu(p
));
2031 BUG_ON(task_current(rq
, p
));
2032 BUG_ON(p
->nr_cpus_allowed
<= 1);
2034 BUG_ON(!task_on_rq_queued(p
));
2035 BUG_ON(!dl_task(p
));
2041 * See if the non running -deadline tasks on this rq
2042 * can be sent to some other CPU where they can preempt
2043 * and start executing.
2045 static int push_dl_task(struct rq
*rq
)
2047 struct task_struct
*next_task
;
2048 struct rq
*later_rq
;
2051 if (!rq
->dl
.overloaded
)
2054 next_task
= pick_next_pushable_dl_task(rq
);
2059 if (WARN_ON(next_task
== rq
->curr
))
2063 * If next_task preempts rq->curr, and rq->curr
2064 * can move away, it makes sense to just reschedule
2065 * without going further in pushing next_task.
2067 if (dl_task(rq
->curr
) &&
2068 dl_time_before(next_task
->dl
.deadline
, rq
->curr
->dl
.deadline
) &&
2069 rq
->curr
->nr_cpus_allowed
> 1) {
2074 /* We might release rq lock */
2075 get_task_struct(next_task
);
2077 /* Will lock the rq it'll find */
2078 later_rq
= find_lock_later_rq(next_task
, rq
);
2080 struct task_struct
*task
;
2083 * We must check all this again, since
2084 * find_lock_later_rq releases rq->lock and it is
2085 * then possible that next_task has migrated.
2087 task
= pick_next_pushable_dl_task(rq
);
2088 if (task
== next_task
) {
2090 * The task is still there. We don't try
2091 * again, some other CPU will pull it when ready.
2100 put_task_struct(next_task
);
2105 deactivate_task(rq
, next_task
, 0);
2106 set_task_cpu(next_task
, later_rq
->cpu
);
2109 * Update the later_rq clock here, because the clock is used
2110 * by the cpufreq_update_util() inside __add_running_bw().
2112 update_rq_clock(later_rq
);
2113 activate_task(later_rq
, next_task
, ENQUEUE_NOCLOCK
);
2116 resched_curr(later_rq
);
2118 double_unlock_balance(rq
, later_rq
);
2121 put_task_struct(next_task
);
2126 static void push_dl_tasks(struct rq
*rq
)
2128 /* push_dl_task() will return true if it moved a -deadline task */
2129 while (push_dl_task(rq
))
2133 static void pull_dl_task(struct rq
*this_rq
)
2135 int this_cpu
= this_rq
->cpu
, cpu
;
2136 struct task_struct
*p
;
2137 bool resched
= false;
2139 u64 dmin
= LONG_MAX
;
2141 if (likely(!dl_overloaded(this_rq
)))
2145 * Match the barrier from dl_set_overloaded; this guarantees that if we
2146 * see overloaded we must also see the dlo_mask bit.
2150 for_each_cpu(cpu
, this_rq
->rd
->dlo_mask
) {
2151 if (this_cpu
== cpu
)
2154 src_rq
= cpu_rq(cpu
);
2157 * It looks racy, abd it is! However, as in sched_rt.c,
2158 * we are fine with this.
2160 if (this_rq
->dl
.dl_nr_running
&&
2161 dl_time_before(this_rq
->dl
.earliest_dl
.curr
,
2162 src_rq
->dl
.earliest_dl
.next
))
2165 /* Might drop this_rq->lock */
2166 double_lock_balance(this_rq
, src_rq
);
2169 * If there are no more pullable tasks on the
2170 * rq, we're done with it.
2172 if (src_rq
->dl
.dl_nr_running
<= 1)
2175 p
= pick_earliest_pushable_dl_task(src_rq
, this_cpu
);
2178 * We found a task to be pulled if:
2179 * - it preempts our current (if there's one),
2180 * - it will preempt the last one we pulled (if any).
2182 if (p
&& dl_time_before(p
->dl
.deadline
, dmin
) &&
2183 (!this_rq
->dl
.dl_nr_running
||
2184 dl_time_before(p
->dl
.deadline
,
2185 this_rq
->dl
.earliest_dl
.curr
))) {
2186 WARN_ON(p
== src_rq
->curr
);
2187 WARN_ON(!task_on_rq_queued(p
));
2190 * Then we pull iff p has actually an earlier
2191 * deadline than the current task of its runqueue.
2193 if (dl_time_before(p
->dl
.deadline
,
2194 src_rq
->curr
->dl
.deadline
))
2199 deactivate_task(src_rq
, p
, 0);
2200 set_task_cpu(p
, this_cpu
);
2201 activate_task(this_rq
, p
, 0);
2202 dmin
= p
->dl
.deadline
;
2204 /* Is there any other task even earlier? */
2207 double_unlock_balance(this_rq
, src_rq
);
2211 resched_curr(this_rq
);
2215 * Since the task is not running and a reschedule is not going to happen
2216 * anytime soon on its runqueue, we try pushing it away now.
2218 static void task_woken_dl(struct rq
*rq
, struct task_struct
*p
)
2220 if (!task_running(rq
, p
) &&
2221 !test_tsk_need_resched(rq
->curr
) &&
2222 p
->nr_cpus_allowed
> 1 &&
2223 dl_task(rq
->curr
) &&
2224 (rq
->curr
->nr_cpus_allowed
< 2 ||
2225 !dl_entity_preempt(&p
->dl
, &rq
->curr
->dl
))) {
2230 static void set_cpus_allowed_dl(struct task_struct
*p
,
2231 const struct cpumask
*new_mask
)
2233 struct root_domain
*src_rd
;
2236 BUG_ON(!dl_task(p
));
2241 * Migrating a SCHED_DEADLINE task between exclusive
2242 * cpusets (different root_domains) entails a bandwidth
2243 * update. We already made space for us in the destination
2244 * domain (see cpuset_can_attach()).
2246 if (!cpumask_intersects(src_rd
->span
, new_mask
)) {
2247 struct dl_bw
*src_dl_b
;
2249 src_dl_b
= dl_bw_of(cpu_of(rq
));
2251 * We now free resources of the root_domain we are migrating
2252 * off. In the worst case, sched_setattr() may temporary fail
2253 * until we complete the update.
2255 raw_spin_lock(&src_dl_b
->lock
);
2256 __dl_sub(src_dl_b
, p
->dl
.dl_bw
, dl_bw_cpus(task_cpu(p
)));
2257 raw_spin_unlock(&src_dl_b
->lock
);
2260 set_cpus_allowed_common(p
, new_mask
);
2263 /* Assumes rq->lock is held */
2264 static void rq_online_dl(struct rq
*rq
)
2266 if (rq
->dl
.overloaded
)
2267 dl_set_overload(rq
);
2269 cpudl_set_freecpu(&rq
->rd
->cpudl
, rq
->cpu
);
2270 if (rq
->dl
.dl_nr_running
> 0)
2271 cpudl_set(&rq
->rd
->cpudl
, rq
->cpu
, rq
->dl
.earliest_dl
.curr
);
2274 /* Assumes rq->lock is held */
2275 static void rq_offline_dl(struct rq
*rq
)
2277 if (rq
->dl
.overloaded
)
2278 dl_clear_overload(rq
);
2280 cpudl_clear(&rq
->rd
->cpudl
, rq
->cpu
);
2281 cpudl_clear_freecpu(&rq
->rd
->cpudl
, rq
->cpu
);
2284 void __init
init_sched_dl_class(void)
2288 for_each_possible_cpu(i
)
2289 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl
, i
),
2290 GFP_KERNEL
, cpu_to_node(i
));
2293 void dl_add_task_root_domain(struct task_struct
*p
)
2299 rq
= task_rq_lock(p
, &rf
);
2303 dl_b
= &rq
->rd
->dl_bw
;
2304 raw_spin_lock(&dl_b
->lock
);
2306 __dl_add(dl_b
, p
->dl
.dl_bw
, cpumask_weight(rq
->rd
->span
));
2308 raw_spin_unlock(&dl_b
->lock
);
2311 task_rq_unlock(rq
, p
, &rf
);
2314 void dl_clear_root_domain(struct root_domain
*rd
)
2316 unsigned long flags
;
2318 raw_spin_lock_irqsave(&rd
->dl_bw
.lock
, flags
);
2319 rd
->dl_bw
.total_bw
= 0;
2320 raw_spin_unlock_irqrestore(&rd
->dl_bw
.lock
, flags
);
2323 #endif /* CONFIG_SMP */
2325 static void switched_from_dl(struct rq
*rq
, struct task_struct
*p
)
2328 * task_non_contending() can start the "inactive timer" (if the 0-lag
2329 * time is in the future). If the task switches back to dl before
2330 * the "inactive timer" fires, it can continue to consume its current
2331 * runtime using its current deadline. If it stays outside of
2332 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2333 * will reset the task parameters.
2335 if (task_on_rq_queued(p
) && p
->dl
.dl_runtime
)
2336 task_non_contending(p
);
2338 if (!task_on_rq_queued(p
)) {
2340 * Inactive timer is armed. However, p is leaving DEADLINE and
2341 * might migrate away from this rq while continuing to run on
2342 * some other class. We need to remove its contribution from
2343 * this rq running_bw now, or sub_rq_bw (below) will complain.
2345 if (p
->dl
.dl_non_contending
)
2346 sub_running_bw(&p
->dl
, &rq
->dl
);
2347 sub_rq_bw(&p
->dl
, &rq
->dl
);
2351 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2352 * at the 0-lag time, because the task could have been migrated
2353 * while SCHED_OTHER in the meanwhile.
2355 if (p
->dl
.dl_non_contending
)
2356 p
->dl
.dl_non_contending
= 0;
2359 * Since this might be the only -deadline task on the rq,
2360 * this is the right place to try to pull some other one
2361 * from an overloaded CPU, if any.
2363 if (!task_on_rq_queued(p
) || rq
->dl
.dl_nr_running
)
2366 deadline_queue_pull_task(rq
);
2370 * When switching to -deadline, we may overload the rq, then
2371 * we try to push someone off, if possible.
2373 static void switched_to_dl(struct rq
*rq
, struct task_struct
*p
)
2375 if (hrtimer_try_to_cancel(&p
->dl
.inactive_timer
) == 1)
2378 /* If p is not queued we will update its parameters at next wakeup. */
2379 if (!task_on_rq_queued(p
)) {
2380 add_rq_bw(&p
->dl
, &rq
->dl
);
2385 if (rq
->curr
!= p
) {
2387 if (p
->nr_cpus_allowed
> 1 && rq
->dl
.overloaded
)
2388 deadline_queue_push_tasks(rq
);
2390 if (dl_task(rq
->curr
))
2391 check_preempt_curr_dl(rq
, p
, 0);
2398 * If the scheduling parameters of a -deadline task changed,
2399 * a push or pull operation might be needed.
2401 static void prio_changed_dl(struct rq
*rq
, struct task_struct
*p
,
2404 if (task_on_rq_queued(p
) || rq
->curr
== p
) {
2407 * This might be too much, but unfortunately
2408 * we don't have the old deadline value, and
2409 * we can't argue if the task is increasing
2410 * or lowering its prio, so...
2412 if (!rq
->dl
.overloaded
)
2413 deadline_queue_pull_task(rq
);
2416 * If we now have a earlier deadline task than p,
2417 * then reschedule, provided p is still on this
2420 if (dl_time_before(rq
->dl
.earliest_dl
.curr
, p
->dl
.deadline
))
2424 * Again, we don't know if p has a earlier
2425 * or later deadline, so let's blindly set a
2426 * (maybe not needed) rescheduling point.
2429 #endif /* CONFIG_SMP */
2433 const struct sched_class dl_sched_class
= {
2434 .next
= &rt_sched_class
,
2435 .enqueue_task
= enqueue_task_dl
,
2436 .dequeue_task
= dequeue_task_dl
,
2437 .yield_task
= yield_task_dl
,
2439 .check_preempt_curr
= check_preempt_curr_dl
,
2441 .pick_next_task
= pick_next_task_dl
,
2442 .put_prev_task
= put_prev_task_dl
,
2443 .set_next_task
= set_next_task_dl
,
2446 .balance
= balance_dl
,
2447 .select_task_rq
= select_task_rq_dl
,
2448 .migrate_task_rq
= migrate_task_rq_dl
,
2449 .set_cpus_allowed
= set_cpus_allowed_dl
,
2450 .rq_online
= rq_online_dl
,
2451 .rq_offline
= rq_offline_dl
,
2452 .task_woken
= task_woken_dl
,
2455 .task_tick
= task_tick_dl
,
2456 .task_fork
= task_fork_dl
,
2458 .prio_changed
= prio_changed_dl
,
2459 .switched_from
= switched_from_dl
,
2460 .switched_to
= switched_to_dl
,
2462 .update_curr
= update_curr_dl
,
2465 int sched_dl_global_validate(void)
2467 u64 runtime
= global_rt_runtime();
2468 u64 period
= global_rt_period();
2469 u64 new_bw
= to_ratio(period
, runtime
);
2472 unsigned long flags
;
2475 * Here we want to check the bandwidth not being set to some
2476 * value smaller than the currently allocated bandwidth in
2477 * any of the root_domains.
2479 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
2480 * cycling on root_domains... Discussion on different/better
2481 * solutions is welcome!
2483 for_each_possible_cpu(cpu
) {
2484 rcu_read_lock_sched();
2485 dl_b
= dl_bw_of(cpu
);
2487 raw_spin_lock_irqsave(&dl_b
->lock
, flags
);
2488 if (new_bw
< dl_b
->total_bw
)
2490 raw_spin_unlock_irqrestore(&dl_b
->lock
, flags
);
2492 rcu_read_unlock_sched();
2501 static void init_dl_rq_bw_ratio(struct dl_rq
*dl_rq
)
2503 if (global_rt_runtime() == RUNTIME_INF
) {
2504 dl_rq
->bw_ratio
= 1 << RATIO_SHIFT
;
2505 dl_rq
->extra_bw
= 1 << BW_SHIFT
;
2507 dl_rq
->bw_ratio
= to_ratio(global_rt_runtime(),
2508 global_rt_period()) >> (BW_SHIFT
- RATIO_SHIFT
);
2509 dl_rq
->extra_bw
= to_ratio(global_rt_period(),
2510 global_rt_runtime());
2514 void sched_dl_do_global(void)
2519 unsigned long flags
;
2521 def_dl_bandwidth
.dl_period
= global_rt_period();
2522 def_dl_bandwidth
.dl_runtime
= global_rt_runtime();
2524 if (global_rt_runtime() != RUNTIME_INF
)
2525 new_bw
= to_ratio(global_rt_period(), global_rt_runtime());
2528 * FIXME: As above...
2530 for_each_possible_cpu(cpu
) {
2531 rcu_read_lock_sched();
2532 dl_b
= dl_bw_of(cpu
);
2534 raw_spin_lock_irqsave(&dl_b
->lock
, flags
);
2536 raw_spin_unlock_irqrestore(&dl_b
->lock
, flags
);
2538 rcu_read_unlock_sched();
2539 init_dl_rq_bw_ratio(&cpu_rq(cpu
)->dl
);
2544 * We must be sure that accepting a new task (or allowing changing the
2545 * parameters of an existing one) is consistent with the bandwidth
2546 * constraints. If yes, this function also accordingly updates the currently
2547 * allocated bandwidth to reflect the new situation.
2549 * This function is called while holding p's rq->lock.
2551 int sched_dl_overflow(struct task_struct
*p
, int policy
,
2552 const struct sched_attr
*attr
)
2554 struct dl_bw
*dl_b
= dl_bw_of(task_cpu(p
));
2555 u64 period
= attr
->sched_period
?: attr
->sched_deadline
;
2556 u64 runtime
= attr
->sched_runtime
;
2557 u64 new_bw
= dl_policy(policy
) ? to_ratio(period
, runtime
) : 0;
2560 if (attr
->sched_flags
& SCHED_FLAG_SUGOV
)
2563 /* !deadline task may carry old deadline bandwidth */
2564 if (new_bw
== p
->dl
.dl_bw
&& task_has_dl_policy(p
))
2568 * Either if a task, enters, leave, or stays -deadline but changes
2569 * its parameters, we may need to update accordingly the total
2570 * allocated bandwidth of the container.
2572 raw_spin_lock(&dl_b
->lock
);
2573 cpus
= dl_bw_cpus(task_cpu(p
));
2574 if (dl_policy(policy
) && !task_has_dl_policy(p
) &&
2575 !__dl_overflow(dl_b
, cpus
, 0, new_bw
)) {
2576 if (hrtimer_active(&p
->dl
.inactive_timer
))
2577 __dl_sub(dl_b
, p
->dl
.dl_bw
, cpus
);
2578 __dl_add(dl_b
, new_bw
, cpus
);
2580 } else if (dl_policy(policy
) && task_has_dl_policy(p
) &&
2581 !__dl_overflow(dl_b
, cpus
, p
->dl
.dl_bw
, new_bw
)) {
2583 * XXX this is slightly incorrect: when the task
2584 * utilization decreases, we should delay the total
2585 * utilization change until the task's 0-lag point.
2586 * But this would require to set the task's "inactive
2587 * timer" when the task is not inactive.
2589 __dl_sub(dl_b
, p
->dl
.dl_bw
, cpus
);
2590 __dl_add(dl_b
, new_bw
, cpus
);
2591 dl_change_utilization(p
, new_bw
);
2593 } else if (!dl_policy(policy
) && task_has_dl_policy(p
)) {
2595 * Do not decrease the total deadline utilization here,
2596 * switched_from_dl() will take care to do it at the correct
2601 raw_spin_unlock(&dl_b
->lock
);
2607 * This function initializes the sched_dl_entity of a newly becoming
2608 * SCHED_DEADLINE task.
2610 * Only the static values are considered here, the actual runtime and the
2611 * absolute deadline will be properly calculated when the task is enqueued
2612 * for the first time with its new policy.
2614 void __setparam_dl(struct task_struct
*p
, const struct sched_attr
*attr
)
2616 struct sched_dl_entity
*dl_se
= &p
->dl
;
2618 dl_se
->dl_runtime
= attr
->sched_runtime
;
2619 dl_se
->dl_deadline
= attr
->sched_deadline
;
2620 dl_se
->dl_period
= attr
->sched_period
?: dl_se
->dl_deadline
;
2621 dl_se
->flags
= attr
->sched_flags
;
2622 dl_se
->dl_bw
= to_ratio(dl_se
->dl_period
, dl_se
->dl_runtime
);
2623 dl_se
->dl_density
= to_ratio(dl_se
->dl_deadline
, dl_se
->dl_runtime
);
2626 void __getparam_dl(struct task_struct
*p
, struct sched_attr
*attr
)
2628 struct sched_dl_entity
*dl_se
= &p
->dl
;
2630 attr
->sched_priority
= p
->rt_priority
;
2631 attr
->sched_runtime
= dl_se
->dl_runtime
;
2632 attr
->sched_deadline
= dl_se
->dl_deadline
;
2633 attr
->sched_period
= dl_se
->dl_period
;
2634 attr
->sched_flags
= dl_se
->flags
;
2638 * This function validates the new parameters of a -deadline task.
2639 * We ask for the deadline not being zero, and greater or equal
2640 * than the runtime, as well as the period of being zero or
2641 * greater than deadline. Furthermore, we have to be sure that
2642 * user parameters are above the internal resolution of 1us (we
2643 * check sched_runtime only since it is always the smaller one) and
2644 * below 2^63 ns (we have to check both sched_deadline and
2645 * sched_period, as the latter can be zero).
2647 bool __checkparam_dl(const struct sched_attr
*attr
)
2649 /* special dl tasks don't actually use any parameter */
2650 if (attr
->sched_flags
& SCHED_FLAG_SUGOV
)
2654 if (attr
->sched_deadline
== 0)
2658 * Since we truncate DL_SCALE bits, make sure we're at least
2661 if (attr
->sched_runtime
< (1ULL << DL_SCALE
))
2665 * Since we use the MSB for wrap-around and sign issues, make
2666 * sure it's not set (mind that period can be equal to zero).
2668 if (attr
->sched_deadline
& (1ULL << 63) ||
2669 attr
->sched_period
& (1ULL << 63))
2672 /* runtime <= deadline <= period (if period != 0) */
2673 if ((attr
->sched_period
!= 0 &&
2674 attr
->sched_period
< attr
->sched_deadline
) ||
2675 attr
->sched_deadline
< attr
->sched_runtime
)
2682 * This function clears the sched_dl_entity static params.
2684 void __dl_clear_params(struct task_struct
*p
)
2686 struct sched_dl_entity
*dl_se
= &p
->dl
;
2688 dl_se
->dl_runtime
= 0;
2689 dl_se
->dl_deadline
= 0;
2690 dl_se
->dl_period
= 0;
2693 dl_se
->dl_density
= 0;
2695 dl_se
->dl_boosted
= 0;
2696 dl_se
->dl_throttled
= 0;
2697 dl_se
->dl_yielded
= 0;
2698 dl_se
->dl_non_contending
= 0;
2699 dl_se
->dl_overrun
= 0;
2702 bool dl_param_changed(struct task_struct
*p
, const struct sched_attr
*attr
)
2704 struct sched_dl_entity
*dl_se
= &p
->dl
;
2706 if (dl_se
->dl_runtime
!= attr
->sched_runtime
||
2707 dl_se
->dl_deadline
!= attr
->sched_deadline
||
2708 dl_se
->dl_period
!= attr
->sched_period
||
2709 dl_se
->flags
!= attr
->sched_flags
)
2716 int dl_task_can_attach(struct task_struct
*p
, const struct cpumask
*cs_cpus_allowed
)
2718 unsigned int dest_cpu
;
2722 unsigned long flags
;
2724 dest_cpu
= cpumask_any_and(cpu_active_mask
, cs_cpus_allowed
);
2726 rcu_read_lock_sched();
2727 dl_b
= dl_bw_of(dest_cpu
);
2728 raw_spin_lock_irqsave(&dl_b
->lock
, flags
);
2729 cpus
= dl_bw_cpus(dest_cpu
);
2730 overflow
= __dl_overflow(dl_b
, cpus
, 0, p
->dl
.dl_bw
);
2735 * We reserve space for this task in the destination
2736 * root_domain, as we can't fail after this point.
2737 * We will free resources in the source root_domain
2738 * later on (see set_cpus_allowed_dl()).
2740 __dl_add(dl_b
, p
->dl
.dl_bw
, cpus
);
2743 raw_spin_unlock_irqrestore(&dl_b
->lock
, flags
);
2744 rcu_read_unlock_sched();
2749 int dl_cpuset_cpumask_can_shrink(const struct cpumask
*cur
,
2750 const struct cpumask
*trial
)
2752 int ret
= 1, trial_cpus
;
2753 struct dl_bw
*cur_dl_b
;
2754 unsigned long flags
;
2756 rcu_read_lock_sched();
2757 cur_dl_b
= dl_bw_of(cpumask_any(cur
));
2758 trial_cpus
= cpumask_weight(trial
);
2760 raw_spin_lock_irqsave(&cur_dl_b
->lock
, flags
);
2761 if (cur_dl_b
->bw
!= -1 &&
2762 cur_dl_b
->bw
* trial_cpus
< cur_dl_b
->total_bw
)
2764 raw_spin_unlock_irqrestore(&cur_dl_b
->lock
, flags
);
2765 rcu_read_unlock_sched();
2770 bool dl_cpu_busy(unsigned int cpu
)
2772 unsigned long flags
;
2777 rcu_read_lock_sched();
2778 dl_b
= dl_bw_of(cpu
);
2779 raw_spin_lock_irqsave(&dl_b
->lock
, flags
);
2780 cpus
= dl_bw_cpus(cpu
);
2781 overflow
= __dl_overflow(dl_b
, cpus
, 0, 0);
2782 raw_spin_unlock_irqrestore(&dl_b
->lock
, flags
);
2783 rcu_read_unlock_sched();
2789 #ifdef CONFIG_SCHED_DEBUG
2790 void print_dl_stats(struct seq_file
*m
, int cpu
)
2792 print_dl_rq(m
, cpu
, &cpu_rq(cpu
)->dl
);
2794 #endif /* CONFIG_SCHED_DEBUG */