serial: exar: Fix GPIO configuration for Sealevel cards based on XR17V35X
[linux/fpc-iii.git] / kernel / sched / deadline.c
blobf63f337c7147a5b6128b419e357d2ed7377c90f5
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Deadline Scheduling Class (SCHED_DEADLINE)
5 * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
7 * Tasks that periodically executes their instances for less than their
8 * runtime won't miss any of their deadlines.
9 * Tasks that are not periodic or sporadic or that tries to execute more
10 * than their reserved bandwidth will be slowed down (and may potentially
11 * miss some of their deadlines), and won't affect any other task.
13 * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>,
14 * Juri Lelli <juri.lelli@gmail.com>,
15 * Michael Trimarchi <michael@amarulasolutions.com>,
16 * Fabio Checconi <fchecconi@gmail.com>
18 #include "sched.h"
19 #include "pelt.h"
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);
38 return &rq->dl;
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_SMP
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;
57 int cpus = 0;
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)
62 cpus++;
64 return cpus;
66 #else
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)
74 return 1;
76 #endif
78 static inline
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);
91 static inline
92 void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
94 u64 old = dl_rq->running_bw;
96 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
97 dl_rq->running_bw -= dl_bw;
98 SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
99 if (dl_rq->running_bw > old)
100 dl_rq->running_bw = 0;
101 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
102 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
105 static inline
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 */
115 static inline
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)
124 dl_rq->this_bw = 0;
125 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
128 static inline
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);
135 static inline
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);
142 static inline
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);
149 static inline
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)
158 struct rq *rq;
160 BUG_ON(p->dl.flags & SCHED_FLAG_SUGOV);
162 if (task_on_rq_queued(p))
163 return;
165 rq = task_rq(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)
177 put_task_struct(p);
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
190 * fires.
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
198 * and task wakeups.
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 * +------------------+
207 * wakeup | ACTIVE |
208 * +------------------>+ contending |
209 * | add_running_bw | |
210 * | +----+------+------+
211 * | | ^
212 * | dequeue | |
213 * +--------+-------+ | |
214 * | | t >= 0-lag | | wakeup
215 * | INACTIVE |<---------------+ |
216 * | | sub_running_bw | |
217 * +--------+-------+ | |
218 * ^ | |
219 * | t < 0-lag | |
220 * | | |
221 * | V |
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);
243 s64 zerolag_time;
246 * If this is a non-deadline task that has been boosted,
247 * do nothing
249 if (dl_se->dl_runtime == 0)
250 return;
252 if (dl_entity_is_special(dl_se))
253 return;
255 WARN_ON(dl_se->dl_non_contending);
257 zerolag_time = dl_se->deadline -
258 div64_long((dl_se->runtime * dl_se->dl_period),
259 dl_se->dl_runtime);
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)) {
272 if (dl_task(p))
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);
285 return;
288 dl_se->dl_non_contending = 1;
289 get_task_struct(p);
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,
299 * do nothing
301 if (dl_se->dl_runtime == 0)
302 return;
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));
318 } else {
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).
324 * So, add it back.
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)
351 dl_b->bw = -1;
352 else
353 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
354 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
355 dl_b->total_bw = 0;
358 void init_dl_rq(struct dl_rq *dl_rq)
360 dl_rq->root = RB_ROOT_CACHED;
362 #ifdef CONFIG_SMP
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;
369 #else
370 init_dl_bw(&dl_rq->dl_bw);
371 #endif
373 dl_rq->running_bw = 0;
374 dl_rq->this_bw = 0;
375 init_dl_rq_bw_ratio(dl_rq);
378 #ifdef CONFIG_SMP
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)
387 if (!rq->online)
388 return;
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().
397 smp_wmb();
398 atomic_inc(&rq->rd->dlo_count);
401 static inline void dl_clear_overload(struct rq *rq)
403 if (!rq->online)
404 return;
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));
457 while (*link) {
458 parent = *link;
459 entry = rb_entry(parent, struct task_struct,
460 pushable_dl_tasks);
461 if (dl_entity_preempt(&p->dl, &entry->dl))
462 link = &parent->rb_left;
463 else {
464 link = &parent->rb_right;
465 leftmost = false;
469 if (leftmost)
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))
482 return;
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);
488 if (next_node) {
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))
519 return;
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;
534 struct dl_bw *dl_b;
536 later_rq = find_lock_later_rq(p, rq);
537 if (!later_rq) {
538 int cpu;
541 * If we cannot preempt any rq, fall back to pick any
542 * online CPU:
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
555 * run.
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);
575 } else {
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
583 * domain.
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);
598 return later_rq;
601 #else
603 static inline
604 void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
608 static inline
609 void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
613 static inline
614 void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
618 static inline
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)
625 return false;
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
655 * parameters.
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)
671 return;
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
689 * other entities.
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)
718 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
724 * arbitrary large.
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
738 * entity.
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)
779 u64 left, right;
782 * left and right are the two sides of the equation above,
783 * after a bit of shuffling to use multiplications instead
784 * of divisions.
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
810 * density.
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.
824 static void
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);
899 return;
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);
927 ktime_t now, act;
928 s64 delta;
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)
948 return 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)) {
960 get_task_struct(p);
961 hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD);
964 return 1;
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,
984 dl_timer);
985 struct task_struct *p = dl_task_of(dl_se);
986 struct rq_flags rf;
987 struct rq *rq;
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()).
995 if (!dl_task(p))
996 goto unlock;
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)
1003 goto unlock;
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)
1010 goto unlock;
1012 sched_clock_tick();
1013 update_rq_clock(rq);
1016 * If the throttle happened during sched-out; like:
1018 * schedule()
1019 * deactivate_task()
1020 * dequeue_task_dl()
1021 * update_curr_dl()
1022 * start_dl_timer()
1023 * __dequeue_task_dl()
1024 * prev->on_rq = 0;
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);
1031 goto unlock;
1034 #ifdef CONFIG_SMP
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
1048 * there.
1051 #endif
1053 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
1054 if (dl_task(rq->curr))
1055 check_preempt_curr_dl(rq, p, 0);
1056 else
1057 resched_curr(rq);
1059 #ifdef CONFIG_SMP
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
1067 * rq->lock.
1069 rq_unpin_lock(rq, &rf);
1070 push_dl_task(rq);
1071 rq_repin_lock(rq, &rf);
1073 #endif
1075 unlock:
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.
1082 put_task_struct(p);
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)))
1121 return;
1122 dl_se->dl_throttled = 1;
1123 if (dl_se->runtime > 0)
1124 dl_se->runtime = 0;
1128 static
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
1148 * BW_SHIFT.
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 */
1158 u64 u_act;
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)
1170 u_act = u_act_min;
1171 else
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);
1187 u64 now;
1189 if (!dl_task(curr) || !on_dl_rq(dl_se))
1190 return;
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))
1204 goto throttle;
1205 return;
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))
1218 return;
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,
1230 &curr->dl);
1231 } else {
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;
1241 throttle:
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))
1255 resched_curr(rq);
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,
1288 inactive_timer);
1289 struct task_struct *p = dl_task_of(dl_se);
1290 struct rq_flags rf;
1291 struct rq *rq;
1293 rq = task_rq_lock(p, &rf);
1295 sched_clock_tick();
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);
1312 goto unlock;
1314 if (dl_se->dl_non_contending == 0)
1315 goto unlock;
1317 sub_running_bw(dl_se, &rq->dl);
1318 dl_se->dl_non_contending = 0;
1319 unlock:
1320 task_rq_unlock(rq, p, &rf);
1321 put_task_struct(p);
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;
1334 #ifdef CONFIG_SMP
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);
1359 } else {
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);
1369 #else
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 */
1376 static inline
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);
1390 static inline
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;
1410 int leftmost = 1;
1412 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
1414 while (*link) {
1415 parent = *link;
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;
1419 else {
1420 link = &parent->rb_right;
1421 leftmost = 0;
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))
1436 return;
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);
1444 static void
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);
1498 return;
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
1525 * add_running_bw().
1527 if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
1528 if (flags & ENQUEUE_WAKEUP)
1529 task_contending(&p->dl, flags);
1531 return;
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)
1548 update_curr_dl(rq);
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"
1563 * or "inactive")
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);
1590 update_curr_dl(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);
1599 #ifdef CONFIG_SMP
1601 static int find_later_rq(struct task_struct *task);
1603 static int
1604 select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1606 struct task_struct *curr;
1607 struct rq *rq;
1609 if (sd_flag != SD_BALANCE_WAKE)
1610 goto out;
1612 rq = cpu_rq(cpu);
1614 rcu_read_lock();
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);
1632 if (target != -1 &&
1633 (dl_time_before(p->dl.deadline,
1634 cpu_rq(target)->dl.earliest_dl.curr) ||
1635 (cpu_rq(target)->dl.dl_nr_running == 0)))
1636 cpu = target;
1638 rcu_read_unlock();
1640 out:
1641 return cpu;
1644 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
1646 struct rq *rq;
1648 if (p->state != TASK_WAKING)
1649 return;
1651 rq = task_rq(p);
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)
1669 put_task_struct(p);
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))
1683 return;
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))
1691 return;
1693 resched_curr(rq);
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);
1706 pull_dl_task(rq);
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
1716 * tasks.
1718 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1719 int flags)
1721 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
1722 resched_curr(rq);
1723 return;
1726 #ifdef CONFIG_SMP
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)
1746 #endif
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);
1755 if (!first)
1756 return;
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);
1772 if (!left)
1773 return NULL;
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))
1785 return NULL;
1787 dl_se = pick_next_dl_entity(rq, dl_rq);
1788 BUG_ON(!dl_se);
1789 p = dl_task_of(dl_se);
1790 set_next_task_dl(rq, p, true);
1791 return p;
1794 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1796 update_curr_dl(rq);
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
1809 * parameters.
1811 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1813 update_curr_dl(rq);
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
1830 * sched_fork()
1834 #ifdef CONFIG_SMP
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))
1843 return 1;
1844 return 0;
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))
1857 return NULL;
1859 next_node:
1860 if (next_node) {
1861 p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1863 if (pick_dl_task(rq, p, cpu))
1864 return p;
1866 next_node = rb_next(next_node);
1867 goto next_node;
1870 return NULL;
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))
1884 return -1;
1886 if (task->nr_cpus_allowed == 1)
1887 return -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))
1894 return -1;
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))
1909 return cpu;
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))
1915 this_cpu = -1;
1917 rcu_read_lock();
1918 for_each_domain(cpu, sd) {
1919 if (sd->flags & SD_WAKE_AFFINE) {
1920 int best_cpu;
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))) {
1928 rcu_read_unlock();
1929 return this_cpu;
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) {
1941 rcu_read_unlock();
1942 return best_cpu;
1946 rcu_read_unlock();
1949 * At this point, all our guesses failed, we just return
1950 * 'something', and let the caller sort the things out.
1952 if (this_cpu != -1)
1953 return this_cpu;
1955 cpu = cpumask_any(later_mask);
1956 if (cpu < nr_cpu_ids)
1957 return cpu;
1959 return -1;
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;
1966 int tries;
1967 int cpu;
1969 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1970 cpu = find_later_rq(task);
1972 if ((cpu == -1) || (cpu == rq->cpu))
1973 break;
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.
1985 later_rq = NULL;
1986 break;
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) ||
1994 !dl_task(task) ||
1995 !task_on_rq_queued(task))) {
1996 double_unlock_balance(rq, later_rq);
1997 later_rq = NULL;
1998 break;
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))
2010 break;
2012 /* Otherwise we try again. */
2013 double_unlock_balance(rq, later_rq);
2014 later_rq = NULL;
2017 return 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))
2025 return NULL;
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));
2037 return 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;
2049 int ret = 0;
2051 if (!rq->dl.overloaded)
2052 return 0;
2054 next_task = pick_next_pushable_dl_task(rq);
2055 if (!next_task)
2056 return 0;
2058 retry:
2059 if (WARN_ON(next_task == rq->curr))
2060 return 0;
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) {
2070 resched_curr(rq);
2071 return 0;
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);
2079 if (!later_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.
2093 goto out;
2096 if (!task)
2097 /* No more tasks */
2098 goto out;
2100 put_task_struct(next_task);
2101 next_task = task;
2102 goto retry;
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);
2114 ret = 1;
2116 resched_curr(later_rq);
2118 double_unlock_balance(rq, later_rq);
2120 out:
2121 put_task_struct(next_task);
2123 return ret;
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;
2138 struct rq *src_rq;
2139 u64 dmin = LONG_MAX;
2141 if (likely(!dl_overloaded(this_rq)))
2142 return;
2145 * Match the barrier from dl_set_overloaded; this guarantees that if we
2146 * see overloaded we must also see the dlo_mask bit.
2148 smp_rmb();
2150 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2151 if (this_cpu == cpu)
2152 continue;
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))
2163 continue;
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)
2173 goto skip;
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))
2195 goto skip;
2197 resched = true;
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? */
2206 skip:
2207 double_unlock_balance(this_rq, src_rq);
2210 if (resched)
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))) {
2226 push_dl_tasks(rq);
2230 static void set_cpus_allowed_dl(struct task_struct *p,
2231 const struct cpumask *new_mask)
2233 struct root_domain *src_rd;
2234 struct rq *rq;
2236 BUG_ON(!dl_task(p));
2238 rq = task_rq(p);
2239 src_rd = rq->rd;
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)
2286 unsigned int i;
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)
2295 struct rq_flags rf;
2296 struct rq *rq;
2297 struct dl_bw *dl_b;
2299 rq = task_rq_lock(p, &rf);
2300 if (!dl_task(p))
2301 goto unlock;
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);
2310 unlock:
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)
2364 return;
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)
2376 put_task_struct(p);
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);
2382 return;
2385 if (rq->curr != p) {
2386 #ifdef CONFIG_SMP
2387 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
2388 deadline_queue_push_tasks(rq);
2389 #endif
2390 if (dl_task(rq->curr))
2391 check_preempt_curr_dl(rq, p, 0);
2392 else
2393 resched_curr(rq);
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,
2402 int oldprio)
2404 if (task_on_rq_queued(p) || rq->curr == p) {
2405 #ifdef CONFIG_SMP
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
2418 * runqueue.
2420 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
2421 resched_curr(rq);
2422 #else
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.
2428 resched_curr(rq);
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,
2445 #ifdef CONFIG_SMP
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,
2453 #endif
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);
2470 struct dl_bw *dl_b;
2471 int cpu, ret = 0;
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)
2489 ret = -EBUSY;
2490 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2492 rcu_read_unlock_sched();
2494 if (ret)
2495 break;
2498 return ret;
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;
2506 } else {
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)
2516 u64 new_bw = -1;
2517 struct dl_bw *dl_b;
2518 int cpu;
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);
2535 dl_b->bw = new_bw;
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;
2558 int cpus, err = -1;
2560 if (attr->sched_flags & SCHED_FLAG_SUGOV)
2561 return 0;
2563 /* !deadline task may carry old deadline bandwidth */
2564 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
2565 return 0;
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);
2579 err = 0;
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);
2592 err = 0;
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
2597 * (0-lag) time.
2599 err = 0;
2601 raw_spin_unlock(&dl_b->lock);
2603 return err;
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)
2651 return true;
2653 /* deadline != 0 */
2654 if (attr->sched_deadline == 0)
2655 return false;
2658 * Since we truncate DL_SCALE bits, make sure we're at least
2659 * that big.
2661 if (attr->sched_runtime < (1ULL << DL_SCALE))
2662 return false;
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))
2670 return false;
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)
2676 return false;
2678 return true;
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;
2691 dl_se->flags = 0;
2692 dl_se->dl_bw = 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)
2710 return true;
2712 return false;
2715 #ifdef CONFIG_SMP
2716 int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed)
2718 unsigned int dest_cpu;
2719 struct dl_bw *dl_b;
2720 bool overflow;
2721 int cpus, ret;
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);
2731 if (overflow) {
2732 ret = -EBUSY;
2733 } else {
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);
2741 ret = 0;
2743 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2744 rcu_read_unlock_sched();
2746 return ret;
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)
2763 ret = 0;
2764 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
2765 rcu_read_unlock_sched();
2767 return ret;
2770 bool dl_cpu_busy(unsigned int cpu)
2772 unsigned long flags;
2773 struct dl_bw *dl_b;
2774 bool overflow;
2775 int cpus;
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();
2785 return overflow;
2787 #endif
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 */