blob: a3ae00c348a8b2f98beaefdca446964ec1bdef30 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Deadline Scheduling Class (SCHED_DEADLINE)
4 *
5 * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
6 *
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.
12 *
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>
17 */
18#include "sched.h"
19#include "pelt.h"
20
21struct dl_bandwidth def_dl_bandwidth;
22
23static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
24{
25 return container_of(dl_se, struct task_struct, dl);
26}
27
28static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
29{
30 return container_of(dl_rq, struct rq, dl);
31}
32
33static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
34{
35 struct task_struct *p = dl_task_of(dl_se);
36 struct rq *rq = task_rq(p);
37
38 return &rq->dl;
39}
40
41static inline int on_dl_rq(struct sched_dl_entity *dl_se)
42{
43 return !RB_EMPTY_NODE(&dl_se->rb_node);
44}
45
Olivier Deprez157378f2022-04-04 15:47:50 +020046#ifdef CONFIG_RT_MUTEXES
47static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
48{
49 return dl_se->pi_se;
50}
51
52static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
53{
54 return pi_of(dl_se) != dl_se;
55}
56#else
57static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
58{
59 return dl_se;
60}
61
62static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
63{
64 return false;
65}
66#endif
67
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068#ifdef CONFIG_SMP
69static inline struct dl_bw *dl_bw_of(int i)
70{
71 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
72 "sched RCU must be held");
73 return &cpu_rq(i)->rd->dl_bw;
74}
75
76static inline int dl_bw_cpus(int i)
77{
78 struct root_domain *rd = cpu_rq(i)->rd;
Olivier Deprez157378f2022-04-04 15:47:50 +020079 int cpus;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080
81 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
82 "sched RCU must be held");
Olivier Deprez157378f2022-04-04 15:47:50 +020083
84 if (cpumask_subset(rd->span, cpu_active_mask))
85 return cpumask_weight(rd->span);
86
87 cpus = 0;
88
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089 for_each_cpu_and(i, rd->span, cpu_active_mask)
90 cpus++;
91
92 return cpus;
93}
Olivier Deprez157378f2022-04-04 15:47:50 +020094
95static inline unsigned long __dl_bw_capacity(int i)
96{
97 struct root_domain *rd = cpu_rq(i)->rd;
98 unsigned long cap = 0;
99
100 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
101 "sched RCU must be held");
102
103 for_each_cpu_and(i, rd->span, cpu_active_mask)
104 cap += capacity_orig_of(i);
105
106 return cap;
107}
108
109/*
110 * XXX Fix: If 'rq->rd == def_root_domain' perform AC against capacity
111 * of the CPU the task is running on rather rd's \Sum CPU capacity.
112 */
113static inline unsigned long dl_bw_capacity(int i)
114{
115 if (!static_branch_unlikely(&sched_asym_cpucapacity) &&
116 capacity_orig_of(i) == SCHED_CAPACITY_SCALE) {
117 return dl_bw_cpus(i) << SCHED_CAPACITY_SHIFT;
118 } else {
119 return __dl_bw_capacity(i);
120 }
121}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000122#else
123static inline struct dl_bw *dl_bw_of(int i)
124{
125 return &cpu_rq(i)->dl.dl_bw;
126}
127
128static inline int dl_bw_cpus(int i)
129{
130 return 1;
131}
Olivier Deprez157378f2022-04-04 15:47:50 +0200132
133static inline unsigned long dl_bw_capacity(int i)
134{
135 return SCHED_CAPACITY_SCALE;
136}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137#endif
138
139static inline
140void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
141{
142 u64 old = dl_rq->running_bw;
143
144 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
145 dl_rq->running_bw += dl_bw;
146 SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
147 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
148 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
149 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
150}
151
152static inline
153void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
154{
155 u64 old = dl_rq->running_bw;
156
157 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
158 dl_rq->running_bw -= dl_bw;
159 SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
160 if (dl_rq->running_bw > old)
161 dl_rq->running_bw = 0;
162 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
163 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
164}
165
166static inline
167void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
168{
169 u64 old = dl_rq->this_bw;
170
171 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
172 dl_rq->this_bw += dl_bw;
173 SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */
174}
175
176static inline
177void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
178{
179 u64 old = dl_rq->this_bw;
180
181 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
182 dl_rq->this_bw -= dl_bw;
183 SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */
184 if (dl_rq->this_bw > old)
185 dl_rq->this_bw = 0;
186 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
187}
188
189static inline
190void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
191{
192 if (!dl_entity_is_special(dl_se))
193 __add_rq_bw(dl_se->dl_bw, dl_rq);
194}
195
196static inline
197void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
198{
199 if (!dl_entity_is_special(dl_se))
200 __sub_rq_bw(dl_se->dl_bw, dl_rq);
201}
202
203static inline
204void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
205{
206 if (!dl_entity_is_special(dl_se))
207 __add_running_bw(dl_se->dl_bw, dl_rq);
208}
209
210static inline
211void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
212{
213 if (!dl_entity_is_special(dl_se))
214 __sub_running_bw(dl_se->dl_bw, dl_rq);
215}
216
Olivier Deprez157378f2022-04-04 15:47:50 +0200217static void dl_change_utilization(struct task_struct *p, u64 new_bw)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218{
219 struct rq *rq;
220
221 BUG_ON(p->dl.flags & SCHED_FLAG_SUGOV);
222
223 if (task_on_rq_queued(p))
224 return;
225
226 rq = task_rq(p);
227 if (p->dl.dl_non_contending) {
228 sub_running_bw(&p->dl, &rq->dl);
229 p->dl.dl_non_contending = 0;
230 /*
231 * If the timer handler is currently running and the
232 * timer cannot be cancelled, inactive_task_timer()
233 * will see that dl_not_contending is not set, and
234 * will not touch the rq's active utilization,
235 * so we are still safe.
236 */
237 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
238 put_task_struct(p);
239 }
240 __sub_rq_bw(p->dl.dl_bw, &rq->dl);
241 __add_rq_bw(new_bw, &rq->dl);
242}
243
244/*
245 * The utilization of a task cannot be immediately removed from
246 * the rq active utilization (running_bw) when the task blocks.
247 * Instead, we have to wait for the so called "0-lag time".
248 *
249 * If a task blocks before the "0-lag time", a timer (the inactive
250 * timer) is armed, and running_bw is decreased when the timer
251 * fires.
252 *
253 * If the task wakes up again before the inactive timer fires,
254 * the timer is cancelled, whereas if the task wakes up after the
255 * inactive timer fired (and running_bw has been decreased) the
256 * task's utilization has to be added to running_bw again.
257 * A flag in the deadline scheduling entity (dl_non_contending)
258 * is used to avoid race conditions between the inactive timer handler
259 * and task wakeups.
260 *
261 * The following diagram shows how running_bw is updated. A task is
262 * "ACTIVE" when its utilization contributes to running_bw; an
263 * "ACTIVE contending" task is in the TASK_RUNNING state, while an
264 * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
265 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
266 * time already passed, which does not contribute to running_bw anymore.
267 * +------------------+
268 * wakeup | ACTIVE |
269 * +------------------>+ contending |
270 * | add_running_bw | |
271 * | +----+------+------+
272 * | | ^
273 * | dequeue | |
274 * +--------+-------+ | |
275 * | | t >= 0-lag | | wakeup
276 * | INACTIVE |<---------------+ |
277 * | | sub_running_bw | |
278 * +--------+-------+ | |
279 * ^ | |
280 * | t < 0-lag | |
281 * | | |
282 * | V |
283 * | +----+------+------+
284 * | sub_running_bw | ACTIVE |
285 * +-------------------+ |
286 * inactive timer | non contending |
287 * fired +------------------+
288 *
289 * The task_non_contending() function is invoked when a task
290 * blocks, and checks if the 0-lag time already passed or
291 * not (in the first case, it directly updates running_bw;
292 * in the second case, it arms the inactive timer).
293 *
294 * The task_contending() function is invoked when a task wakes
295 * up, and checks if the task is still in the "ACTIVE non contending"
296 * state or not (in the second case, it updates running_bw).
297 */
298static void task_non_contending(struct task_struct *p)
299{
300 struct sched_dl_entity *dl_se = &p->dl;
301 struct hrtimer *timer = &dl_se->inactive_timer;
302 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
303 struct rq *rq = rq_of_dl_rq(dl_rq);
304 s64 zerolag_time;
305
306 /*
307 * If this is a non-deadline task that has been boosted,
308 * do nothing
309 */
310 if (dl_se->dl_runtime == 0)
311 return;
312
313 if (dl_entity_is_special(dl_se))
314 return;
315
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316 WARN_ON(dl_se->dl_non_contending);
317
318 zerolag_time = dl_se->deadline -
319 div64_long((dl_se->runtime * dl_se->dl_period),
320 dl_se->dl_runtime);
321
322 /*
323 * Using relative times instead of the absolute "0-lag time"
324 * allows to simplify the code
325 */
326 zerolag_time -= rq_clock(rq);
327
328 /*
329 * If the "0-lag time" already passed, decrease the active
330 * utilization now, instead of starting a timer
331 */
David Brazdil0f672f62019-12-10 10:32:29 +0000332 if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000333 if (dl_task(p))
334 sub_running_bw(dl_se, dl_rq);
335 if (!dl_task(p) || p->state == TASK_DEAD) {
336 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
337
338 if (p->state == TASK_DEAD)
339 sub_rq_bw(&p->dl, &rq->dl);
340 raw_spin_lock(&dl_b->lock);
341 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
342 __dl_clear_params(p);
343 raw_spin_unlock(&dl_b->lock);
344 }
345
346 return;
347 }
348
349 dl_se->dl_non_contending = 1;
350 get_task_struct(p);
David Brazdil0f672f62019-12-10 10:32:29 +0000351 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000352}
353
354static void task_contending(struct sched_dl_entity *dl_se, int flags)
355{
356 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
357
358 /*
359 * If this is a non-deadline task that has been boosted,
360 * do nothing
361 */
362 if (dl_se->dl_runtime == 0)
363 return;
364
365 if (flags & ENQUEUE_MIGRATED)
366 add_rq_bw(dl_se, dl_rq);
367
368 if (dl_se->dl_non_contending) {
369 dl_se->dl_non_contending = 0;
370 /*
371 * If the timer handler is currently running and the
372 * timer cannot be cancelled, inactive_task_timer()
373 * will see that dl_not_contending is not set, and
374 * will not touch the rq's active utilization,
375 * so we are still safe.
376 */
377 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1)
378 put_task_struct(dl_task_of(dl_se));
379 } else {
380 /*
381 * Since "dl_non_contending" is not set, the
382 * task's utilization has already been removed from
383 * active utilization (either when the task blocked,
384 * when the "inactive timer" fired).
385 * So, add it back.
386 */
387 add_running_bw(dl_se, dl_rq);
388 }
389}
390
391static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
392{
393 struct sched_dl_entity *dl_se = &p->dl;
394
395 return dl_rq->root.rb_leftmost == &dl_se->rb_node;
396}
397
Olivier Deprez157378f2022-04-04 15:47:50 +0200398static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
399
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
401{
402 raw_spin_lock_init(&dl_b->dl_runtime_lock);
403 dl_b->dl_period = period;
404 dl_b->dl_runtime = runtime;
405}
406
407void init_dl_bw(struct dl_bw *dl_b)
408{
409 raw_spin_lock_init(&dl_b->lock);
410 raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
411 if (global_rt_runtime() == RUNTIME_INF)
412 dl_b->bw = -1;
413 else
414 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
415 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
416 dl_b->total_bw = 0;
417}
418
419void init_dl_rq(struct dl_rq *dl_rq)
420{
421 dl_rq->root = RB_ROOT_CACHED;
422
423#ifdef CONFIG_SMP
424 /* zero means no -deadline tasks */
425 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
426
427 dl_rq->dl_nr_migratory = 0;
428 dl_rq->overloaded = 0;
429 dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
430#else
431 init_dl_bw(&dl_rq->dl_bw);
432#endif
433
434 dl_rq->running_bw = 0;
435 dl_rq->this_bw = 0;
436 init_dl_rq_bw_ratio(dl_rq);
437}
438
439#ifdef CONFIG_SMP
440
441static inline int dl_overloaded(struct rq *rq)
442{
443 return atomic_read(&rq->rd->dlo_count);
444}
445
446static inline void dl_set_overload(struct rq *rq)
447{
448 if (!rq->online)
449 return;
450
451 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
452 /*
453 * Must be visible before the overload count is
454 * set (as in sched_rt.c).
455 *
456 * Matched by the barrier in pull_dl_task().
457 */
458 smp_wmb();
459 atomic_inc(&rq->rd->dlo_count);
460}
461
462static inline void dl_clear_overload(struct rq *rq)
463{
464 if (!rq->online)
465 return;
466
467 atomic_dec(&rq->rd->dlo_count);
468 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
469}
470
471static void update_dl_migration(struct dl_rq *dl_rq)
472{
473 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
474 if (!dl_rq->overloaded) {
475 dl_set_overload(rq_of_dl_rq(dl_rq));
476 dl_rq->overloaded = 1;
477 }
478 } else if (dl_rq->overloaded) {
479 dl_clear_overload(rq_of_dl_rq(dl_rq));
480 dl_rq->overloaded = 0;
481 }
482}
483
484static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
485{
486 struct task_struct *p = dl_task_of(dl_se);
487
488 if (p->nr_cpus_allowed > 1)
489 dl_rq->dl_nr_migratory++;
490
491 update_dl_migration(dl_rq);
492}
493
494static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
495{
496 struct task_struct *p = dl_task_of(dl_se);
497
498 if (p->nr_cpus_allowed > 1)
499 dl_rq->dl_nr_migratory--;
500
501 update_dl_migration(dl_rq);
502}
503
504/*
505 * The list of pushable -deadline task is not a plist, like in
506 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
507 */
508static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
509{
510 struct dl_rq *dl_rq = &rq->dl;
511 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_root.rb_node;
512 struct rb_node *parent = NULL;
513 struct task_struct *entry;
514 bool leftmost = true;
515
516 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
517
518 while (*link) {
519 parent = *link;
520 entry = rb_entry(parent, struct task_struct,
521 pushable_dl_tasks);
522 if (dl_entity_preempt(&p->dl, &entry->dl))
523 link = &parent->rb_left;
524 else {
525 link = &parent->rb_right;
526 leftmost = false;
527 }
528 }
529
530 if (leftmost)
531 dl_rq->earliest_dl.next = p->dl.deadline;
532
533 rb_link_node(&p->pushable_dl_tasks, parent, link);
534 rb_insert_color_cached(&p->pushable_dl_tasks,
535 &dl_rq->pushable_dl_tasks_root, leftmost);
536}
537
538static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
539{
540 struct dl_rq *dl_rq = &rq->dl;
541
542 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
543 return;
544
545 if (dl_rq->pushable_dl_tasks_root.rb_leftmost == &p->pushable_dl_tasks) {
546 struct rb_node *next_node;
547
548 next_node = rb_next(&p->pushable_dl_tasks);
549 if (next_node) {
550 dl_rq->earliest_dl.next = rb_entry(next_node,
551 struct task_struct, pushable_dl_tasks)->dl.deadline;
552 }
553 }
554
555 rb_erase_cached(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
556 RB_CLEAR_NODE(&p->pushable_dl_tasks);
557}
558
559static inline int has_pushable_dl_tasks(struct rq *rq)
560{
561 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
562}
563
564static int push_dl_task(struct rq *rq);
565
566static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
567{
568 return dl_task(prev);
569}
570
571static DEFINE_PER_CPU(struct callback_head, dl_push_head);
572static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
573
574static void push_dl_tasks(struct rq *);
575static void pull_dl_task(struct rq *);
576
577static inline void deadline_queue_push_tasks(struct rq *rq)
578{
579 if (!has_pushable_dl_tasks(rq))
580 return;
581
582 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
583}
584
585static inline void deadline_queue_pull_task(struct rq *rq)
586{
587 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
588}
589
590static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
591
592static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
593{
594 struct rq *later_rq = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +0000595 struct dl_bw *dl_b;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000596
597 later_rq = find_lock_later_rq(p, rq);
598 if (!later_rq) {
599 int cpu;
600
601 /*
602 * If we cannot preempt any rq, fall back to pick any
603 * online CPU:
604 */
David Brazdil0f672f62019-12-10 10:32:29 +0000605 cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000606 if (cpu >= nr_cpu_ids) {
607 /*
608 * Failed to find any suitable CPU.
609 * The task will never come back!
610 */
611 BUG_ON(dl_bandwidth_enabled());
612
613 /*
614 * If admission control is disabled we
615 * try a little harder to let the task
616 * run.
617 */
618 cpu = cpumask_any(cpu_active_mask);
619 }
620 later_rq = cpu_rq(cpu);
621 double_lock_balance(rq, later_rq);
622 }
623
David Brazdil0f672f62019-12-10 10:32:29 +0000624 if (p->dl.dl_non_contending || p->dl.dl_throttled) {
625 /*
626 * Inactive timer is armed (or callback is running, but
627 * waiting for us to release rq locks). In any case, when it
628 * will fire (or continue), it will see running_bw of this
629 * task migrated to later_rq (and correctly handle it).
630 */
631 sub_running_bw(&p->dl, &rq->dl);
632 sub_rq_bw(&p->dl, &rq->dl);
633
634 add_rq_bw(&p->dl, &later_rq->dl);
635 add_running_bw(&p->dl, &later_rq->dl);
636 } else {
637 sub_rq_bw(&p->dl, &rq->dl);
638 add_rq_bw(&p->dl, &later_rq->dl);
639 }
640
641 /*
642 * And we finally need to fixup root_domain(s) bandwidth accounting,
643 * since p is still hanging out in the old (now moved to default) root
644 * domain.
645 */
646 dl_b = &rq->rd->dl_bw;
647 raw_spin_lock(&dl_b->lock);
648 __dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
649 raw_spin_unlock(&dl_b->lock);
650
651 dl_b = &later_rq->rd->dl_bw;
652 raw_spin_lock(&dl_b->lock);
653 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span));
654 raw_spin_unlock(&dl_b->lock);
655
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000656 set_task_cpu(p, later_rq->cpu);
657 double_unlock_balance(later_rq, rq);
658
659 return later_rq;
660}
661
662#else
663
664static inline
665void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
666{
667}
668
669static inline
670void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
671{
672}
673
674static inline
675void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
676{
677}
678
679static inline
680void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
681{
682}
683
684static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
685{
686 return false;
687}
688
689static inline void pull_dl_task(struct rq *rq)
690{
691}
692
693static inline void deadline_queue_push_tasks(struct rq *rq)
694{
695}
696
697static inline void deadline_queue_pull_task(struct rq *rq)
698{
699}
700#endif /* CONFIG_SMP */
701
702static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
703static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
704static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, int flags);
705
706/*
707 * We are being explicitly informed that a new instance is starting,
708 * and this means that:
709 * - the absolute deadline of the entity has to be placed at
710 * current time + relative deadline;
711 * - the runtime of the entity has to be set to the maximum value.
712 *
713 * The capability of specifying such event is useful whenever a -deadline
714 * entity wants to (try to!) synchronize its behaviour with the scheduler's
715 * one, and to (try to!) reconcile itself with its own scheduling
716 * parameters.
717 */
718static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
719{
720 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
721 struct rq *rq = rq_of_dl_rq(dl_rq);
722
Olivier Deprez157378f2022-04-04 15:47:50 +0200723 WARN_ON(is_dl_boosted(dl_se));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
725
726 /*
727 * We are racing with the deadline timer. So, do nothing because
728 * the deadline timer handler will take care of properly recharging
729 * the runtime and postponing the deadline
730 */
731 if (dl_se->dl_throttled)
732 return;
733
734 /*
735 * We use the regular wall clock time to set deadlines in the
736 * future; in fact, we must consider execution overheads (time
737 * spent on hardirq context, etc.).
738 */
739 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
740 dl_se->runtime = dl_se->dl_runtime;
741}
742
743/*
744 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
745 * possibility of a entity lasting more than what it declared, and thus
746 * exhausting its runtime.
747 *
748 * Here we are interested in making runtime overrun possible, but we do
749 * not want a entity which is misbehaving to affect the scheduling of all
750 * other entities.
751 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
752 * is used, in order to confine each entity within its own bandwidth.
753 *
754 * This function deals exactly with that, and ensures that when the runtime
755 * of a entity is replenished, its deadline is also postponed. That ensures
756 * the overrunning entity can't interfere with other entity in the system and
757 * can't make them miss their deadlines. Reasons why this kind of overruns
758 * could happen are, typically, a entity voluntarily trying to overcome its
759 * runtime, or it just underestimated it during sched_setattr().
760 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200761static void replenish_dl_entity(struct sched_dl_entity *dl_se)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000762{
763 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
764 struct rq *rq = rq_of_dl_rq(dl_rq);
765
Olivier Deprez157378f2022-04-04 15:47:50 +0200766 BUG_ON(pi_of(dl_se)->dl_runtime <= 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000767
768 /*
769 * This could be the case for a !-dl task that is boosted.
770 * Just go with full inherited parameters.
771 */
772 if (dl_se->dl_deadline == 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200773 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
774 dl_se->runtime = pi_of(dl_se)->dl_runtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000775 }
776
777 if (dl_se->dl_yielded && dl_se->runtime > 0)
778 dl_se->runtime = 0;
779
780 /*
781 * We keep moving the deadline away until we get some
782 * available runtime for the entity. This ensures correct
783 * handling of situations where the runtime overrun is
784 * arbitrary large.
785 */
786 while (dl_se->runtime <= 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200787 dl_se->deadline += pi_of(dl_se)->dl_period;
788 dl_se->runtime += pi_of(dl_se)->dl_runtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789 }
790
791 /*
792 * At this point, the deadline really should be "in
793 * the future" with respect to rq->clock. If it's
794 * not, we are, for some reason, lagging too much!
795 * Anyway, after having warn userspace abut that,
796 * we still try to keep the things running by
797 * resetting the deadline and the budget of the
798 * entity.
799 */
800 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
801 printk_deferred_once("sched: DL replenish lagged too much\n");
Olivier Deprez157378f2022-04-04 15:47:50 +0200802 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
803 dl_se->runtime = pi_of(dl_se)->dl_runtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000804 }
805
806 if (dl_se->dl_yielded)
807 dl_se->dl_yielded = 0;
808 if (dl_se->dl_throttled)
809 dl_se->dl_throttled = 0;
810}
811
812/*
813 * Here we check if --at time t-- an entity (which is probably being
814 * [re]activated or, in general, enqueued) can use its remaining runtime
815 * and its current deadline _without_ exceeding the bandwidth it is
816 * assigned (function returns true if it can't). We are in fact applying
817 * one of the CBS rules: when a task wakes up, if the residual runtime
818 * over residual deadline fits within the allocated bandwidth, then we
819 * can keep the current (absolute) deadline and residual budget without
820 * disrupting the schedulability of the system. Otherwise, we should
821 * refill the runtime and set the deadline a period in the future,
822 * because keeping the current (absolute) deadline of the task would
823 * result in breaking guarantees promised to other tasks (refer to
David Brazdil0f672f62019-12-10 10:32:29 +0000824 * Documentation/scheduler/sched-deadline.rst for more information).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000825 *
826 * This function returns true if:
827 *
828 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
829 *
830 * IOW we can't recycle current parameters.
831 *
832 * Notice that the bandwidth check is done against the deadline. For
833 * task with deadline equal to period this is the same of using
834 * dl_period instead of dl_deadline in the equation above.
835 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200836static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000837{
838 u64 left, right;
839
840 /*
841 * left and right are the two sides of the equation above,
842 * after a bit of shuffling to use multiplications instead
843 * of divisions.
844 *
845 * Note that none of the time values involved in the two
846 * multiplications are absolute: dl_deadline and dl_runtime
847 * are the relative deadline and the maximum runtime of each
848 * instance, runtime is the runtime left for the last instance
849 * and (deadline - t), since t is rq->clock, is the time left
850 * to the (absolute) deadline. Even if overflowing the u64 type
851 * is very unlikely to occur in both cases, here we scale down
852 * as we want to avoid that risk at all. Scaling down by 10
853 * means that we reduce granularity to 1us. We are fine with it,
854 * since this is only a true/false check and, anyway, thinking
855 * of anything below microseconds resolution is actually fiction
856 * (but still we want to give the user that illusion >;).
857 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200858 left = (pi_of(dl_se)->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000859 right = ((dl_se->deadline - t) >> DL_SCALE) *
Olivier Deprez157378f2022-04-04 15:47:50 +0200860 (pi_of(dl_se)->dl_runtime >> DL_SCALE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000861
862 return dl_time_before(right, left);
863}
864
865/*
866 * Revised wakeup rule [1]: For self-suspending tasks, rather then
867 * re-initializing task's runtime and deadline, the revised wakeup
868 * rule adjusts the task's runtime to avoid the task to overrun its
869 * density.
870 *
871 * Reasoning: a task may overrun the density if:
872 * runtime / (deadline - t) > dl_runtime / dl_deadline
873 *
874 * Therefore, runtime can be adjusted to:
875 * runtime = (dl_runtime / dl_deadline) * (deadline - t)
876 *
877 * In such way that runtime will be equal to the maximum density
878 * the task can use without breaking any rule.
879 *
880 * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
881 * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
882 */
883static void
884update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
885{
886 u64 laxity = dl_se->deadline - rq_clock(rq);
887
888 /*
889 * If the task has deadline < period, and the deadline is in the past,
890 * it should already be throttled before this check.
891 *
892 * See update_dl_entity() comments for further details.
893 */
894 WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
895
896 dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
897}
898
899/*
900 * Regarding the deadline, a task with implicit deadline has a relative
901 * deadline == relative period. A task with constrained deadline has a
902 * relative deadline <= relative period.
903 *
904 * We support constrained deadline tasks. However, there are some restrictions
905 * applied only for tasks which do not have an implicit deadline. See
906 * update_dl_entity() to know more about such restrictions.
907 *
908 * The dl_is_implicit() returns true if the task has an implicit deadline.
909 */
910static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
911{
912 return dl_se->dl_deadline == dl_se->dl_period;
913}
914
915/*
916 * When a deadline entity is placed in the runqueue, its runtime and deadline
917 * might need to be updated. This is done by a CBS wake up rule. There are two
918 * different rules: 1) the original CBS; and 2) the Revisited CBS.
919 *
920 * When the task is starting a new period, the Original CBS is used. In this
921 * case, the runtime is replenished and a new absolute deadline is set.
922 *
923 * When a task is queued before the begin of the next period, using the
924 * remaining runtime and deadline could make the entity to overflow, see
925 * dl_entity_overflow() to find more about runtime overflow. When such case
926 * is detected, the runtime and deadline need to be updated.
927 *
928 * If the task has an implicit deadline, i.e., deadline == period, the Original
929 * CBS is applied. the runtime is replenished and a new absolute deadline is
930 * set, as in the previous cases.
931 *
932 * However, the Original CBS does not work properly for tasks with
933 * deadline < period, which are said to have a constrained deadline. By
934 * applying the Original CBS, a constrained deadline task would be able to run
935 * runtime/deadline in a period. With deadline < period, the task would
936 * overrun the runtime/period allowed bandwidth, breaking the admission test.
937 *
938 * In order to prevent this misbehave, the Revisited CBS is used for
939 * constrained deadline tasks when a runtime overflow is detected. In the
940 * Revisited CBS, rather than replenishing & setting a new absolute deadline,
941 * the remaining runtime of the task is reduced to avoid runtime overflow.
942 * Please refer to the comments update_dl_revised_wakeup() function to find
943 * more about the Revised CBS rule.
944 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200945static void update_dl_entity(struct sched_dl_entity *dl_se)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000946{
947 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
948 struct rq *rq = rq_of_dl_rq(dl_rq);
949
950 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
Olivier Deprez157378f2022-04-04 15:47:50 +0200951 dl_entity_overflow(dl_se, rq_clock(rq))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000952
953 if (unlikely(!dl_is_implicit(dl_se) &&
954 !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
Olivier Deprez157378f2022-04-04 15:47:50 +0200955 !is_dl_boosted(dl_se))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000956 update_dl_revised_wakeup(dl_se, rq);
957 return;
958 }
959
Olivier Deprez157378f2022-04-04 15:47:50 +0200960 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
961 dl_se->runtime = pi_of(dl_se)->dl_runtime;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000962 }
963}
964
965static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
966{
967 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
968}
969
970/*
971 * If the entity depleted all its runtime, and if we want it to sleep
972 * while waiting for some new execution time to become available, we
973 * set the bandwidth replenishment timer to the replenishment instant
974 * and try to activate it.
975 *
976 * Notice that it is important for the caller to know if the timer
977 * actually started or not (i.e., the replenishment instant is in
978 * the future or in the past).
979 */
980static int start_dl_timer(struct task_struct *p)
981{
982 struct sched_dl_entity *dl_se = &p->dl;
983 struct hrtimer *timer = &dl_se->dl_timer;
984 struct rq *rq = task_rq(p);
985 ktime_t now, act;
986 s64 delta;
987
988 lockdep_assert_held(&rq->lock);
989
990 /*
991 * We want the timer to fire at the deadline, but considering
992 * that it is actually coming from rq->clock and not from
993 * hrtimer's time base reading.
994 */
995 act = ns_to_ktime(dl_next_period(dl_se));
996 now = hrtimer_cb_get_time(timer);
997 delta = ktime_to_ns(now) - rq_clock(rq);
998 act = ktime_add_ns(act, delta);
999
1000 /*
1001 * If the expiry time already passed, e.g., because the value
1002 * chosen as the deadline is too small, don't even try to
1003 * start the timer in the past!
1004 */
1005 if (ktime_us_delta(act, now) < 0)
1006 return 0;
1007
1008 /*
1009 * !enqueued will guarantee another callback; even if one is already in
1010 * progress. This ensures a balanced {get,put}_task_struct().
1011 *
1012 * The race against __run_timer() clearing the enqueued state is
1013 * harmless because we're holding task_rq()->lock, therefore the timer
1014 * expiring after we've done the check will wait on its task_rq_lock()
1015 * and observe our state.
1016 */
1017 if (!hrtimer_is_queued(timer)) {
1018 get_task_struct(p);
David Brazdil0f672f62019-12-10 10:32:29 +00001019 hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001020 }
1021
1022 return 1;
1023}
1024
1025/*
1026 * This is the bandwidth enforcement timer callback. If here, we know
1027 * a task is not on its dl_rq, since the fact that the timer was running
1028 * means the task is throttled and needs a runtime replenishment.
1029 *
1030 * However, what we actually do depends on the fact the task is active,
1031 * (it is on its rq) or has been removed from there by a call to
1032 * dequeue_task_dl(). In the former case we must issue the runtime
1033 * replenishment and add the task back to the dl_rq; in the latter, we just
1034 * do nothing but clearing dl_throttled, so that runtime and deadline
1035 * updating (and the queueing back to dl_rq) will be done by the
1036 * next call to enqueue_task_dl().
1037 */
1038static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
1039{
1040 struct sched_dl_entity *dl_se = container_of(timer,
1041 struct sched_dl_entity,
1042 dl_timer);
1043 struct task_struct *p = dl_task_of(dl_se);
1044 struct rq_flags rf;
1045 struct rq *rq;
1046
1047 rq = task_rq_lock(p, &rf);
1048
1049 /*
1050 * The task might have changed its scheduling policy to something
1051 * different than SCHED_DEADLINE (through switched_from_dl()).
1052 */
1053 if (!dl_task(p))
1054 goto unlock;
1055
1056 /*
1057 * The task might have been boosted by someone else and might be in the
1058 * boosting/deboosting path, its not throttled.
1059 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001060 if (is_dl_boosted(dl_se))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001061 goto unlock;
1062
1063 /*
1064 * Spurious timer due to start_dl_timer() race; or we already received
1065 * a replenishment from rt_mutex_setprio().
1066 */
1067 if (!dl_se->dl_throttled)
1068 goto unlock;
1069
1070 sched_clock_tick();
1071 update_rq_clock(rq);
1072
1073 /*
1074 * If the throttle happened during sched-out; like:
1075 *
1076 * schedule()
1077 * deactivate_task()
1078 * dequeue_task_dl()
1079 * update_curr_dl()
1080 * start_dl_timer()
1081 * __dequeue_task_dl()
1082 * prev->on_rq = 0;
1083 *
1084 * We can be both throttled and !queued. Replenish the counter
1085 * but do not enqueue -- wait for our wakeup to do that.
1086 */
1087 if (!task_on_rq_queued(p)) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001088 replenish_dl_entity(dl_se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001089 goto unlock;
1090 }
1091
1092#ifdef CONFIG_SMP
1093 if (unlikely(!rq->online)) {
1094 /*
1095 * If the runqueue is no longer available, migrate the
1096 * task elsewhere. This necessarily changes rq.
1097 */
1098 lockdep_unpin_lock(&rq->lock, rf.cookie);
1099 rq = dl_task_offline_migration(rq, p);
1100 rf.cookie = lockdep_pin_lock(&rq->lock);
1101 update_rq_clock(rq);
1102
1103 /*
1104 * Now that the task has been migrated to the new RQ and we
1105 * have that locked, proceed as normal and enqueue the task
1106 * there.
1107 */
1108 }
1109#endif
1110
1111 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
1112 if (dl_task(rq->curr))
1113 check_preempt_curr_dl(rq, p, 0);
1114 else
1115 resched_curr(rq);
1116
1117#ifdef CONFIG_SMP
1118 /*
1119 * Queueing this task back might have overloaded rq, check if we need
1120 * to kick someone away.
1121 */
1122 if (has_pushable_dl_tasks(rq)) {
1123 /*
1124 * Nothing relies on rq->lock after this, so its safe to drop
1125 * rq->lock.
1126 */
1127 rq_unpin_lock(rq, &rf);
1128 push_dl_task(rq);
1129 rq_repin_lock(rq, &rf);
1130 }
1131#endif
1132
1133unlock:
1134 task_rq_unlock(rq, p, &rf);
1135
1136 /*
1137 * This can free the task_struct, including this hrtimer, do not touch
1138 * anything related to that after this.
1139 */
1140 put_task_struct(p);
1141
1142 return HRTIMER_NORESTART;
1143}
1144
1145void init_dl_task_timer(struct sched_dl_entity *dl_se)
1146{
1147 struct hrtimer *timer = &dl_se->dl_timer;
1148
David Brazdil0f672f62019-12-10 10:32:29 +00001149 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001150 timer->function = dl_task_timer;
1151}
1152
1153/*
1154 * During the activation, CBS checks if it can reuse the current task's
1155 * runtime and period. If the deadline of the task is in the past, CBS
1156 * cannot use the runtime, and so it replenishes the task. This rule
1157 * works fine for implicit deadline tasks (deadline == period), and the
1158 * CBS was designed for implicit deadline tasks. However, a task with
Olivier Deprez157378f2022-04-04 15:47:50 +02001159 * constrained deadline (deadline < period) might be awakened after the
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001160 * deadline, but before the next period. In this case, replenishing the
1161 * task would allow it to run for runtime / deadline. As in this case
1162 * deadline < period, CBS enables a task to run for more than the
1163 * runtime / period. In a very loaded system, this can cause a domino
1164 * effect, making other tasks miss their deadlines.
1165 *
1166 * To avoid this problem, in the activation of a constrained deadline
1167 * task after the deadline but before the next period, throttle the
1168 * task and set the replenishing timer to the begin of the next period,
1169 * unless it is boosted.
1170 */
1171static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
1172{
1173 struct task_struct *p = dl_task_of(dl_se);
1174 struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
1175
1176 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1177 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001178 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(p)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001179 return;
1180 dl_se->dl_throttled = 1;
1181 if (dl_se->runtime > 0)
1182 dl_se->runtime = 0;
1183 }
1184}
1185
1186static
1187int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
1188{
1189 return (dl_se->runtime <= 0);
1190}
1191
1192extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
1193
1194/*
1195 * This function implements the GRUB accounting rule:
1196 * according to the GRUB reclaiming algorithm, the runtime is
1197 * not decreased as "dq = -dt", but as
1198 * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt",
1199 * where u is the utilization of the task, Umax is the maximum reclaimable
1200 * utilization, Uinact is the (per-runqueue) inactive utilization, computed
1201 * as the difference between the "total runqueue utilization" and the
1202 * runqueue active utilization, and Uextra is the (per runqueue) extra
1203 * reclaimable utilization.
1204 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations
1205 * multiplied by 2^BW_SHIFT, the result has to be shifted right by
1206 * BW_SHIFT.
1207 * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT,
1208 * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
1209 * Since delta is a 64 bit variable, to have an overflow its value
1210 * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds.
1211 * So, overflow is not an issue here.
1212 */
1213static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
1214{
1215 u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
1216 u64 u_act;
1217 u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT;
1218
1219 /*
1220 * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)},
1221 * we compare u_inact + rq->dl.extra_bw with
1222 * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because
1223 * u_inact + rq->dl.extra_bw can be larger than
1224 * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative
1225 * leading to wrong results)
1226 */
1227 if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min)
1228 u_act = u_act_min;
1229 else
1230 u_act = BW_UNIT - u_inact - rq->dl.extra_bw;
1231
1232 return (delta * u_act) >> BW_SHIFT;
1233}
1234
1235/*
1236 * Update the current task's runtime statistics (provided it is still
1237 * a -deadline task and has not been removed from the dl_rq).
1238 */
1239static void update_curr_dl(struct rq *rq)
1240{
1241 struct task_struct *curr = rq->curr;
1242 struct sched_dl_entity *dl_se = &curr->dl;
1243 u64 delta_exec, scaled_delta_exec;
1244 int cpu = cpu_of(rq);
1245 u64 now;
1246
1247 if (!dl_task(curr) || !on_dl_rq(dl_se))
1248 return;
1249
1250 /*
1251 * Consumed budget is computed considering the time as
1252 * observed by schedulable tasks (excluding time spent
1253 * in hardirq context, etc.). Deadlines are instead
1254 * computed using hard walltime. This seems to be the more
1255 * natural solution, but the full ramifications of this
1256 * approach need further study.
1257 */
1258 now = rq_clock_task(rq);
1259 delta_exec = now - curr->se.exec_start;
1260 if (unlikely((s64)delta_exec <= 0)) {
1261 if (unlikely(dl_se->dl_yielded))
1262 goto throttle;
1263 return;
1264 }
1265
1266 schedstat_set(curr->se.statistics.exec_max,
1267 max(curr->se.statistics.exec_max, delta_exec));
1268
1269 curr->se.sum_exec_runtime += delta_exec;
1270 account_group_exec_runtime(curr, delta_exec);
1271
1272 curr->se.exec_start = now;
1273 cgroup_account_cputime(curr, delta_exec);
1274
1275 if (dl_entity_is_special(dl_se))
1276 return;
1277
1278 /*
1279 * For tasks that participate in GRUB, we implement GRUB-PA: the
1280 * spare reclaimed bandwidth is used to clock down frequency.
1281 *
1282 * For the others, we still need to scale reservation parameters
1283 * according to current frequency and CPU maximum capacity.
1284 */
1285 if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) {
1286 scaled_delta_exec = grub_reclaim(delta_exec,
1287 rq,
1288 &curr->dl);
1289 } else {
1290 unsigned long scale_freq = arch_scale_freq_capacity(cpu);
David Brazdil0f672f62019-12-10 10:32:29 +00001291 unsigned long scale_cpu = arch_scale_cpu_capacity(cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001292
1293 scaled_delta_exec = cap_scale(delta_exec, scale_freq);
1294 scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu);
1295 }
1296
1297 dl_se->runtime -= scaled_delta_exec;
1298
1299throttle:
1300 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
1301 dl_se->dl_throttled = 1;
1302
1303 /* If requested, inform the user about runtime overruns. */
1304 if (dl_runtime_exceeded(dl_se) &&
1305 (dl_se->flags & SCHED_FLAG_DL_OVERRUN))
1306 dl_se->dl_overrun = 1;
1307
1308 __dequeue_task_dl(rq, curr, 0);
Olivier Deprez157378f2022-04-04 15:47:50 +02001309 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(curr)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001310 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
1311
1312 if (!is_leftmost(curr, &rq->dl))
1313 resched_curr(rq);
1314 }
1315
1316 /*
1317 * Because -- for now -- we share the rt bandwidth, we need to
1318 * account our runtime there too, otherwise actual rt tasks
1319 * would be able to exceed the shared quota.
1320 *
1321 * Account to the root rt group for now.
1322 *
1323 * The solution we're working towards is having the RT groups scheduled
1324 * using deadline servers -- however there's a few nasties to figure
1325 * out before that can happen.
1326 */
1327 if (rt_bandwidth_enabled()) {
1328 struct rt_rq *rt_rq = &rq->rt;
1329
1330 raw_spin_lock(&rt_rq->rt_runtime_lock);
1331 /*
1332 * We'll let actual RT tasks worry about the overflow here, we
1333 * have our own CBS to keep us inline; only account when RT
1334 * bandwidth is relevant.
1335 */
1336 if (sched_rt_bandwidth_account(rt_rq))
1337 rt_rq->rt_time += delta_exec;
1338 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1339 }
1340}
1341
1342static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
1343{
1344 struct sched_dl_entity *dl_se = container_of(timer,
1345 struct sched_dl_entity,
1346 inactive_timer);
1347 struct task_struct *p = dl_task_of(dl_se);
1348 struct rq_flags rf;
1349 struct rq *rq;
1350
1351 rq = task_rq_lock(p, &rf);
1352
1353 sched_clock_tick();
1354 update_rq_clock(rq);
1355
1356 if (!dl_task(p) || p->state == TASK_DEAD) {
1357 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1358
1359 if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
1360 sub_running_bw(&p->dl, dl_rq_of_se(&p->dl));
1361 sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl));
1362 dl_se->dl_non_contending = 0;
1363 }
1364
1365 raw_spin_lock(&dl_b->lock);
1366 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
1367 raw_spin_unlock(&dl_b->lock);
1368 __dl_clear_params(p);
1369
1370 goto unlock;
1371 }
1372 if (dl_se->dl_non_contending == 0)
1373 goto unlock;
1374
1375 sub_running_bw(dl_se, &rq->dl);
1376 dl_se->dl_non_contending = 0;
1377unlock:
1378 task_rq_unlock(rq, p, &rf);
1379 put_task_struct(p);
1380
1381 return HRTIMER_NORESTART;
1382}
1383
1384void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1385{
1386 struct hrtimer *timer = &dl_se->inactive_timer;
1387
David Brazdil0f672f62019-12-10 10:32:29 +00001388 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001389 timer->function = inactive_task_timer;
1390}
1391
1392#ifdef CONFIG_SMP
1393
1394static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1395{
1396 struct rq *rq = rq_of_dl_rq(dl_rq);
1397
1398 if (dl_rq->earliest_dl.curr == 0 ||
1399 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
1400 dl_rq->earliest_dl.curr = deadline;
1401 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
1402 }
1403}
1404
1405static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1406{
1407 struct rq *rq = rq_of_dl_rq(dl_rq);
1408
1409 /*
1410 * Since we may have removed our earliest (and/or next earliest)
1411 * task we must recompute them.
1412 */
1413 if (!dl_rq->dl_nr_running) {
1414 dl_rq->earliest_dl.curr = 0;
1415 dl_rq->earliest_dl.next = 0;
1416 cpudl_clear(&rq->rd->cpudl, rq->cpu);
1417 } else {
1418 struct rb_node *leftmost = dl_rq->root.rb_leftmost;
1419 struct sched_dl_entity *entry;
1420
1421 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
1422 dl_rq->earliest_dl.curr = entry->deadline;
1423 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
1424 }
1425}
1426
1427#else
1428
1429static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1430static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1431
1432#endif /* CONFIG_SMP */
1433
1434static inline
1435void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1436{
1437 int prio = dl_task_of(dl_se)->prio;
1438 u64 deadline = dl_se->deadline;
1439
1440 WARN_ON(!dl_prio(prio));
1441 dl_rq->dl_nr_running++;
1442 add_nr_running(rq_of_dl_rq(dl_rq), 1);
1443
1444 inc_dl_deadline(dl_rq, deadline);
1445 inc_dl_migration(dl_se, dl_rq);
1446}
1447
1448static inline
1449void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1450{
1451 int prio = dl_task_of(dl_se)->prio;
1452
1453 WARN_ON(!dl_prio(prio));
1454 WARN_ON(!dl_rq->dl_nr_running);
1455 dl_rq->dl_nr_running--;
1456 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
1457
1458 dec_dl_deadline(dl_rq, dl_se->deadline);
1459 dec_dl_migration(dl_se, dl_rq);
1460}
1461
1462static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1463{
1464 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1465 struct rb_node **link = &dl_rq->root.rb_root.rb_node;
1466 struct rb_node *parent = NULL;
1467 struct sched_dl_entity *entry;
1468 int leftmost = 1;
1469
1470 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
1471
1472 while (*link) {
1473 parent = *link;
1474 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
1475 if (dl_time_before(dl_se->deadline, entry->deadline))
1476 link = &parent->rb_left;
1477 else {
1478 link = &parent->rb_right;
1479 leftmost = 0;
1480 }
1481 }
1482
1483 rb_link_node(&dl_se->rb_node, parent, link);
1484 rb_insert_color_cached(&dl_se->rb_node, &dl_rq->root, leftmost);
1485
1486 inc_dl_tasks(dl_se, dl_rq);
1487}
1488
1489static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1490{
1491 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1492
1493 if (RB_EMPTY_NODE(&dl_se->rb_node))
1494 return;
1495
1496 rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
1497 RB_CLEAR_NODE(&dl_se->rb_node);
1498
1499 dec_dl_tasks(dl_se, dl_rq);
1500}
1501
1502static void
Olivier Deprez157378f2022-04-04 15:47:50 +02001503enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001504{
1505 BUG_ON(on_dl_rq(dl_se));
1506
1507 /*
1508 * If this is a wakeup or a new instance, the scheduling
1509 * parameters of the task might need updating. Otherwise,
1510 * we want a replenishment of its runtime.
1511 */
1512 if (flags & ENQUEUE_WAKEUP) {
1513 task_contending(dl_se, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02001514 update_dl_entity(dl_se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001515 } else if (flags & ENQUEUE_REPLENISH) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001516 replenish_dl_entity(dl_se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001517 } else if ((flags & ENQUEUE_RESTORE) &&
1518 dl_time_before(dl_se->deadline,
1519 rq_clock(rq_of_dl_rq(dl_rq_of_se(dl_se))))) {
1520 setup_new_dl_entity(dl_se);
1521 }
1522
1523 __enqueue_dl_entity(dl_se);
1524}
1525
1526static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
1527{
1528 __dequeue_dl_entity(dl_se);
1529}
1530
1531static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1532{
Olivier Deprez157378f2022-04-04 15:47:50 +02001533 if (is_dl_boosted(&p->dl)) {
1534 /*
1535 * Because of delays in the detection of the overrun of a
1536 * thread's runtime, it might be the case that a thread
1537 * goes to sleep in a rt mutex with negative runtime. As
1538 * a consequence, the thread will be throttled.
1539 *
1540 * While waiting for the mutex, this thread can also be
1541 * boosted via PI, resulting in a thread that is throttled
1542 * and boosted at the same time.
1543 *
1544 * In this case, the boost overrides the throttle.
1545 */
1546 if (p->dl.dl_throttled) {
1547 /*
1548 * The replenish timer needs to be canceled. No
1549 * problem if it fires concurrently: boosted threads
1550 * are ignored in dl_task_timer().
1551 */
1552 hrtimer_try_to_cancel(&p->dl.dl_timer);
1553 p->dl.dl_throttled = 0;
1554 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001555 } else if (!dl_prio(p->normal_prio)) {
1556 /*
Olivier Deprez157378f2022-04-04 15:47:50 +02001557 * Special case in which we have a !SCHED_DEADLINE task that is going
1558 * to be deboosted, but exceeds its runtime while doing so. No point in
1559 * replenishing it, as it's going to return back to its original
1560 * scheduling class after this. If it has been throttled, we need to
1561 * clear the flag, otherwise the task may wake up as throttled after
1562 * being boosted again with no means to replenish the runtime and clear
1563 * the throttle.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001564 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001565 p->dl.dl_throttled = 0;
1566 BUG_ON(!is_dl_boosted(&p->dl) || flags != ENQUEUE_REPLENISH);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001567 return;
1568 }
1569
1570 /*
1571 * Check if a constrained deadline task was activated
1572 * after the deadline but before the next period.
1573 * If that is the case, the task will be throttled and
1574 * the replenishment timer will be set to the next period.
1575 */
1576 if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl))
1577 dl_check_constrained_dl(&p->dl);
1578
1579 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) {
1580 add_rq_bw(&p->dl, &rq->dl);
1581 add_running_bw(&p->dl, &rq->dl);
1582 }
1583
1584 /*
1585 * If p is throttled, we do not enqueue it. In fact, if it exhausted
1586 * its budget it needs a replenishment and, since it now is on
1587 * its rq, the bandwidth timer callback (which clearly has not
1588 * run yet) will take care of this.
1589 * However, the active utilization does not depend on the fact
1590 * that the task is on the runqueue or not (but depends on the
1591 * task's state - in GRUB parlance, "inactive" vs "active contending").
1592 * In other words, even if a task is throttled its utilization must
1593 * be counted in the active utilization; hence, we need to call
1594 * add_running_bw().
1595 */
1596 if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
1597 if (flags & ENQUEUE_WAKEUP)
1598 task_contending(&p->dl, flags);
1599
1600 return;
1601 }
1602
Olivier Deprez157378f2022-04-04 15:47:50 +02001603 enqueue_dl_entity(&p->dl, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001604
1605 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1606 enqueue_pushable_dl_task(rq, p);
1607}
1608
1609static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1610{
1611 dequeue_dl_entity(&p->dl);
1612 dequeue_pushable_dl_task(rq, p);
1613}
1614
1615static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1616{
1617 update_curr_dl(rq);
1618 __dequeue_task_dl(rq, p, flags);
1619
1620 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) {
1621 sub_running_bw(&p->dl, &rq->dl);
1622 sub_rq_bw(&p->dl, &rq->dl);
1623 }
1624
1625 /*
1626 * This check allows to start the inactive timer (or to immediately
1627 * decrease the active utilization, if needed) in two cases:
1628 * when the task blocks and when it is terminating
1629 * (p->state == TASK_DEAD). We can handle the two cases in the same
1630 * way, because from GRUB's point of view the same thing is happening
1631 * (the task moves from "active contending" to "active non contending"
1632 * or "inactive")
1633 */
1634 if (flags & DEQUEUE_SLEEP)
1635 task_non_contending(p);
1636}
1637
1638/*
1639 * Yield task semantic for -deadline tasks is:
1640 *
1641 * get off from the CPU until our next instance, with
1642 * a new runtime. This is of little use now, since we
1643 * don't have a bandwidth reclaiming mechanism. Anyway,
1644 * bandwidth reclaiming is planned for the future, and
1645 * yield_task_dl will indicate that some spare budget
1646 * is available for other task instances to use it.
1647 */
1648static void yield_task_dl(struct rq *rq)
1649{
1650 /*
1651 * We make the task go to sleep until its current deadline by
1652 * forcing its runtime to zero. This way, update_curr_dl() stops
1653 * it and the bandwidth timer will wake it up and will give it
1654 * new scheduling parameters (thanks to dl_yielded=1).
1655 */
1656 rq->curr->dl.dl_yielded = 1;
1657
1658 update_rq_clock(rq);
1659 update_curr_dl(rq);
1660 /*
1661 * Tell update_rq_clock() that we've just updated,
1662 * so we don't do microscopic update in schedule()
1663 * and double the fastpath cost.
1664 */
1665 rq_clock_skip_update(rq);
1666}
1667
1668#ifdef CONFIG_SMP
1669
1670static int find_later_rq(struct task_struct *task);
1671
1672static int
1673select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1674{
1675 struct task_struct *curr;
Olivier Deprez157378f2022-04-04 15:47:50 +02001676 bool select_rq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001677 struct rq *rq;
1678
1679 if (sd_flag != SD_BALANCE_WAKE)
1680 goto out;
1681
1682 rq = cpu_rq(cpu);
1683
1684 rcu_read_lock();
1685 curr = READ_ONCE(rq->curr); /* unlocked access */
1686
1687 /*
1688 * If we are dealing with a -deadline task, we must
1689 * decide where to wake it up.
1690 * If it has a later deadline and the current task
1691 * on this rq can't move (provided the waking task
1692 * can!) we prefer to send it somewhere else. On the
1693 * other hand, if it has a shorter deadline, we
1694 * try to make it stay here, it might be important.
1695 */
Olivier Deprez157378f2022-04-04 15:47:50 +02001696 select_rq = unlikely(dl_task(curr)) &&
1697 (curr->nr_cpus_allowed < 2 ||
1698 !dl_entity_preempt(&p->dl, &curr->dl)) &&
1699 p->nr_cpus_allowed > 1;
1700
1701 /*
1702 * Take the capacity of the CPU into account to
1703 * ensure it fits the requirement of the task.
1704 */
1705 if (static_branch_unlikely(&sched_asym_cpucapacity))
1706 select_rq |= !dl_task_fits_capacity(p, cpu);
1707
1708 if (select_rq) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001709 int target = find_later_rq(p);
1710
1711 if (target != -1 &&
1712 (dl_time_before(p->dl.deadline,
1713 cpu_rq(target)->dl.earliest_dl.curr) ||
1714 (cpu_rq(target)->dl.dl_nr_running == 0)))
1715 cpu = target;
1716 }
1717 rcu_read_unlock();
1718
1719out:
1720 return cpu;
1721}
1722
1723static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
1724{
1725 struct rq *rq;
1726
1727 if (p->state != TASK_WAKING)
1728 return;
1729
1730 rq = task_rq(p);
1731 /*
1732 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1733 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1734 * rq->lock is not... So, lock it
1735 */
1736 raw_spin_lock(&rq->lock);
1737 if (p->dl.dl_non_contending) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001738 update_rq_clock(rq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001739 sub_running_bw(&p->dl, &rq->dl);
1740 p->dl.dl_non_contending = 0;
1741 /*
1742 * If the timer handler is currently running and the
1743 * timer cannot be cancelled, inactive_task_timer()
1744 * will see that dl_not_contending is not set, and
1745 * will not touch the rq's active utilization,
1746 * so we are still safe.
1747 */
1748 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1749 put_task_struct(p);
1750 }
1751 sub_rq_bw(&p->dl, &rq->dl);
1752 raw_spin_unlock(&rq->lock);
1753}
1754
1755static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1756{
1757 /*
1758 * Current can't be migrated, useless to reschedule,
1759 * let's hope p can move out.
1760 */
1761 if (rq->curr->nr_cpus_allowed == 1 ||
1762 !cpudl_find(&rq->rd->cpudl, rq->curr, NULL))
1763 return;
1764
1765 /*
1766 * p is migratable, so let's not schedule it and
1767 * see if it is pushed or pulled somewhere else.
1768 */
1769 if (p->nr_cpus_allowed != 1 &&
1770 cpudl_find(&rq->rd->cpudl, p, NULL))
1771 return;
1772
1773 resched_curr(rq);
1774}
1775
David Brazdil0f672f62019-12-10 10:32:29 +00001776static int balance_dl(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1777{
1778 if (!on_dl_rq(&p->dl) && need_pull_dl_task(rq, p)) {
1779 /*
1780 * This is OK, because current is on_cpu, which avoids it being
1781 * picked for load-balance and preemption/IRQs are still
1782 * disabled avoiding further scheduler activity on it and we've
1783 * not yet started the picking loop.
1784 */
1785 rq_unpin_lock(rq, rf);
1786 pull_dl_task(rq);
1787 rq_repin_lock(rq, rf);
1788 }
1789
1790 return sched_stop_runnable(rq) || sched_dl_runnable(rq);
1791}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001792#endif /* CONFIG_SMP */
1793
1794/*
1795 * Only called when both the current and waking task are -deadline
1796 * tasks.
1797 */
1798static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1799 int flags)
1800{
1801 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
1802 resched_curr(rq);
1803 return;
1804 }
1805
1806#ifdef CONFIG_SMP
1807 /*
1808 * In the unlikely case current and p have the same deadline
1809 * let us try to decide what's the best thing to do...
1810 */
1811 if ((p->dl.deadline == rq->curr->dl.deadline) &&
1812 !test_tsk_need_resched(rq->curr))
1813 check_preempt_equal_dl(rq, p);
1814#endif /* CONFIG_SMP */
1815}
1816
1817#ifdef CONFIG_SCHED_HRTICK
1818static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1819{
1820 hrtick_start(rq, p->dl.runtime);
1821}
1822#else /* !CONFIG_SCHED_HRTICK */
1823static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1824{
1825}
1826#endif
1827
Olivier Deprez0e641232021-09-23 10:07:05 +02001828static void set_next_task_dl(struct rq *rq, struct task_struct *p, bool first)
David Brazdil0f672f62019-12-10 10:32:29 +00001829{
1830 p->se.exec_start = rq_clock_task(rq);
1831
1832 /* You can't push away the running task */
1833 dequeue_pushable_dl_task(rq, p);
1834
Olivier Deprez0e641232021-09-23 10:07:05 +02001835 if (!first)
1836 return;
1837
David Brazdil0f672f62019-12-10 10:32:29 +00001838 if (hrtick_enabled(rq))
1839 start_hrtick_dl(rq, p);
1840
1841 if (rq->curr->sched_class != &dl_sched_class)
1842 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
1843
1844 deadline_queue_push_tasks(rq);
1845}
1846
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001847static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1848 struct dl_rq *dl_rq)
1849{
1850 struct rb_node *left = rb_first_cached(&dl_rq->root);
1851
1852 if (!left)
1853 return NULL;
1854
1855 return rb_entry(left, struct sched_dl_entity, rb_node);
1856}
1857
Olivier Deprez157378f2022-04-04 15:47:50 +02001858static struct task_struct *pick_next_task_dl(struct rq *rq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001859{
1860 struct sched_dl_entity *dl_se;
David Brazdil0f672f62019-12-10 10:32:29 +00001861 struct dl_rq *dl_rq = &rq->dl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001862 struct task_struct *p;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001863
David Brazdil0f672f62019-12-10 10:32:29 +00001864 if (!sched_dl_runnable(rq))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001865 return NULL;
1866
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001867 dl_se = pick_next_dl_entity(rq, dl_rq);
1868 BUG_ON(!dl_se);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001869 p = dl_task_of(dl_se);
Olivier Deprez0e641232021-09-23 10:07:05 +02001870 set_next_task_dl(rq, p, true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001871 return p;
1872}
1873
1874static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1875{
1876 update_curr_dl(rq);
1877
David Brazdil0f672f62019-12-10 10:32:29 +00001878 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001879 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
1880 enqueue_pushable_dl_task(rq, p);
1881}
1882
1883/*
1884 * scheduler tick hitting a task of our scheduling class.
1885 *
1886 * NOTE: This function can be called remotely by the tick offload that
1887 * goes along full dynticks. Therefore no local assumption can be made
1888 * and everything must be accessed through the @rq and @curr passed in
1889 * parameters.
1890 */
1891static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1892{
1893 update_curr_dl(rq);
1894
David Brazdil0f672f62019-12-10 10:32:29 +00001895 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001896 /*
1897 * Even when we have runtime, update_curr_dl() might have resulted in us
1898 * not being the leftmost task anymore. In that case NEED_RESCHED will
1899 * be set and schedule() will start a new hrtick for the next task.
1900 */
1901 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
1902 is_leftmost(p, &rq->dl))
1903 start_hrtick_dl(rq, p);
1904}
1905
1906static void task_fork_dl(struct task_struct *p)
1907{
1908 /*
1909 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1910 * sched_fork()
1911 */
1912}
1913
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001914#ifdef CONFIG_SMP
1915
1916/* Only try algorithms three times */
1917#define DL_MAX_TRIES 3
1918
1919static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1920{
1921 if (!task_running(rq, p) &&
David Brazdil0f672f62019-12-10 10:32:29 +00001922 cpumask_test_cpu(cpu, p->cpus_ptr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001923 return 1;
1924 return 0;
1925}
1926
1927/*
1928 * Return the earliest pushable rq's task, which is suitable to be executed
1929 * on the CPU, NULL otherwise:
1930 */
1931static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
1932{
1933 struct rb_node *next_node = rq->dl.pushable_dl_tasks_root.rb_leftmost;
1934 struct task_struct *p = NULL;
1935
1936 if (!has_pushable_dl_tasks(rq))
1937 return NULL;
1938
1939next_node:
1940 if (next_node) {
1941 p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1942
1943 if (pick_dl_task(rq, p, cpu))
1944 return p;
1945
1946 next_node = rb_next(next_node);
1947 goto next_node;
1948 }
1949
1950 return NULL;
1951}
1952
1953static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1954
1955static int find_later_rq(struct task_struct *task)
1956{
1957 struct sched_domain *sd;
1958 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
1959 int this_cpu = smp_processor_id();
1960 int cpu = task_cpu(task);
1961
1962 /* Make sure the mask is initialized first */
1963 if (unlikely(!later_mask))
1964 return -1;
1965
1966 if (task->nr_cpus_allowed == 1)
1967 return -1;
1968
1969 /*
1970 * We have to consider system topology and task affinity
1971 * first, then we can look for a suitable CPU.
1972 */
1973 if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
1974 return -1;
1975
1976 /*
1977 * If we are here, some targets have been found, including
1978 * the most suitable which is, among the runqueues where the
1979 * current tasks have later deadlines than the task's one, the
1980 * rq with the latest possible one.
1981 *
1982 * Now we check how well this matches with task's
1983 * affinity and system topology.
1984 *
1985 * The last CPU where the task run is our first
1986 * guess, since it is most likely cache-hot there.
1987 */
1988 if (cpumask_test_cpu(cpu, later_mask))
1989 return cpu;
1990 /*
1991 * Check if this_cpu is to be skipped (i.e., it is
1992 * not in the mask) or not.
1993 */
1994 if (!cpumask_test_cpu(this_cpu, later_mask))
1995 this_cpu = -1;
1996
1997 rcu_read_lock();
1998 for_each_domain(cpu, sd) {
1999 if (sd->flags & SD_WAKE_AFFINE) {
2000 int best_cpu;
2001
2002 /*
2003 * If possible, preempting this_cpu is
2004 * cheaper than migrating.
2005 */
2006 if (this_cpu != -1 &&
2007 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
2008 rcu_read_unlock();
2009 return this_cpu;
2010 }
2011
2012 best_cpu = cpumask_first_and(later_mask,
2013 sched_domain_span(sd));
2014 /*
2015 * Last chance: if a CPU being in both later_mask
2016 * and current sd span is valid, that becomes our
2017 * choice. Of course, the latest possible CPU is
2018 * already under consideration through later_mask.
2019 */
2020 if (best_cpu < nr_cpu_ids) {
2021 rcu_read_unlock();
2022 return best_cpu;
2023 }
2024 }
2025 }
2026 rcu_read_unlock();
2027
2028 /*
2029 * At this point, all our guesses failed, we just return
2030 * 'something', and let the caller sort the things out.
2031 */
2032 if (this_cpu != -1)
2033 return this_cpu;
2034
2035 cpu = cpumask_any(later_mask);
2036 if (cpu < nr_cpu_ids)
2037 return cpu;
2038
2039 return -1;
2040}
2041
2042/* Locks the rq it finds */
2043static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
2044{
2045 struct rq *later_rq = NULL;
2046 int tries;
2047 int cpu;
2048
2049 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
2050 cpu = find_later_rq(task);
2051
2052 if ((cpu == -1) || (cpu == rq->cpu))
2053 break;
2054
2055 later_rq = cpu_rq(cpu);
2056
2057 if (later_rq->dl.dl_nr_running &&
2058 !dl_time_before(task->dl.deadline,
2059 later_rq->dl.earliest_dl.curr)) {
2060 /*
2061 * Target rq has tasks of equal or earlier deadline,
2062 * retrying does not release any lock and is unlikely
2063 * to yield a different result.
2064 */
2065 later_rq = NULL;
2066 break;
2067 }
2068
2069 /* Retry if something changed. */
2070 if (double_lock_balance(rq, later_rq)) {
2071 if (unlikely(task_rq(task) != rq ||
David Brazdil0f672f62019-12-10 10:32:29 +00002072 !cpumask_test_cpu(later_rq->cpu, task->cpus_ptr) ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002073 task_running(rq, task) ||
2074 !dl_task(task) ||
2075 !task_on_rq_queued(task))) {
2076 double_unlock_balance(rq, later_rq);
2077 later_rq = NULL;
2078 break;
2079 }
2080 }
2081
2082 /*
2083 * If the rq we found has no -deadline task, or
2084 * its earliest one has a later deadline than our
2085 * task, the rq is a good one.
2086 */
2087 if (!later_rq->dl.dl_nr_running ||
2088 dl_time_before(task->dl.deadline,
2089 later_rq->dl.earliest_dl.curr))
2090 break;
2091
2092 /* Otherwise we try again. */
2093 double_unlock_balance(rq, later_rq);
2094 later_rq = NULL;
2095 }
2096
2097 return later_rq;
2098}
2099
2100static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
2101{
2102 struct task_struct *p;
2103
2104 if (!has_pushable_dl_tasks(rq))
2105 return NULL;
2106
2107 p = rb_entry(rq->dl.pushable_dl_tasks_root.rb_leftmost,
2108 struct task_struct, pushable_dl_tasks);
2109
2110 BUG_ON(rq->cpu != task_cpu(p));
2111 BUG_ON(task_current(rq, p));
2112 BUG_ON(p->nr_cpus_allowed <= 1);
2113
2114 BUG_ON(!task_on_rq_queued(p));
2115 BUG_ON(!dl_task(p));
2116
2117 return p;
2118}
2119
2120/*
2121 * See if the non running -deadline tasks on this rq
2122 * can be sent to some other CPU where they can preempt
2123 * and start executing.
2124 */
2125static int push_dl_task(struct rq *rq)
2126{
2127 struct task_struct *next_task;
2128 struct rq *later_rq;
2129 int ret = 0;
2130
2131 if (!rq->dl.overloaded)
2132 return 0;
2133
2134 next_task = pick_next_pushable_dl_task(rq);
2135 if (!next_task)
2136 return 0;
2137
2138retry:
David Brazdil0f672f62019-12-10 10:32:29 +00002139 if (WARN_ON(next_task == rq->curr))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002140 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002141
2142 /*
2143 * If next_task preempts rq->curr, and rq->curr
2144 * can move away, it makes sense to just reschedule
2145 * without going further in pushing next_task.
2146 */
2147 if (dl_task(rq->curr) &&
2148 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
2149 rq->curr->nr_cpus_allowed > 1) {
2150 resched_curr(rq);
2151 return 0;
2152 }
2153
2154 /* We might release rq lock */
2155 get_task_struct(next_task);
2156
2157 /* Will lock the rq it'll find */
2158 later_rq = find_lock_later_rq(next_task, rq);
2159 if (!later_rq) {
2160 struct task_struct *task;
2161
2162 /*
2163 * We must check all this again, since
2164 * find_lock_later_rq releases rq->lock and it is
2165 * then possible that next_task has migrated.
2166 */
2167 task = pick_next_pushable_dl_task(rq);
2168 if (task == next_task) {
2169 /*
2170 * The task is still there. We don't try
2171 * again, some other CPU will pull it when ready.
2172 */
2173 goto out;
2174 }
2175
2176 if (!task)
2177 /* No more tasks */
2178 goto out;
2179
2180 put_task_struct(next_task);
2181 next_task = task;
2182 goto retry;
2183 }
2184
2185 deactivate_task(rq, next_task, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002186 set_task_cpu(next_task, later_rq->cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002187
2188 /*
2189 * Update the later_rq clock here, because the clock is used
2190 * by the cpufreq_update_util() inside __add_running_bw().
2191 */
2192 update_rq_clock(later_rq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002193 activate_task(later_rq, next_task, ENQUEUE_NOCLOCK);
2194 ret = 1;
2195
2196 resched_curr(later_rq);
2197
2198 double_unlock_balance(rq, later_rq);
2199
2200out:
2201 put_task_struct(next_task);
2202
2203 return ret;
2204}
2205
2206static void push_dl_tasks(struct rq *rq)
2207{
2208 /* push_dl_task() will return true if it moved a -deadline task */
2209 while (push_dl_task(rq))
2210 ;
2211}
2212
2213static void pull_dl_task(struct rq *this_rq)
2214{
2215 int this_cpu = this_rq->cpu, cpu;
2216 struct task_struct *p;
2217 bool resched = false;
2218 struct rq *src_rq;
2219 u64 dmin = LONG_MAX;
2220
2221 if (likely(!dl_overloaded(this_rq)))
2222 return;
2223
2224 /*
2225 * Match the barrier from dl_set_overloaded; this guarantees that if we
2226 * see overloaded we must also see the dlo_mask bit.
2227 */
2228 smp_rmb();
2229
2230 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2231 if (this_cpu == cpu)
2232 continue;
2233
2234 src_rq = cpu_rq(cpu);
2235
2236 /*
2237 * It looks racy, abd it is! However, as in sched_rt.c,
2238 * we are fine with this.
2239 */
2240 if (this_rq->dl.dl_nr_running &&
2241 dl_time_before(this_rq->dl.earliest_dl.curr,
2242 src_rq->dl.earliest_dl.next))
2243 continue;
2244
2245 /* Might drop this_rq->lock */
2246 double_lock_balance(this_rq, src_rq);
2247
2248 /*
2249 * If there are no more pullable tasks on the
2250 * rq, we're done with it.
2251 */
2252 if (src_rq->dl.dl_nr_running <= 1)
2253 goto skip;
2254
2255 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
2256
2257 /*
2258 * We found a task to be pulled if:
2259 * - it preempts our current (if there's one),
2260 * - it will preempt the last one we pulled (if any).
2261 */
2262 if (p && dl_time_before(p->dl.deadline, dmin) &&
2263 (!this_rq->dl.dl_nr_running ||
2264 dl_time_before(p->dl.deadline,
2265 this_rq->dl.earliest_dl.curr))) {
2266 WARN_ON(p == src_rq->curr);
2267 WARN_ON(!task_on_rq_queued(p));
2268
2269 /*
2270 * Then we pull iff p has actually an earlier
2271 * deadline than the current task of its runqueue.
2272 */
2273 if (dl_time_before(p->dl.deadline,
2274 src_rq->curr->dl.deadline))
2275 goto skip;
2276
2277 resched = true;
2278
2279 deactivate_task(src_rq, p, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002280 set_task_cpu(p, this_cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002281 activate_task(this_rq, p, 0);
2282 dmin = p->dl.deadline;
2283
2284 /* Is there any other task even earlier? */
2285 }
2286skip:
2287 double_unlock_balance(this_rq, src_rq);
2288 }
2289
2290 if (resched)
2291 resched_curr(this_rq);
2292}
2293
2294/*
2295 * Since the task is not running and a reschedule is not going to happen
2296 * anytime soon on its runqueue, we try pushing it away now.
2297 */
2298static void task_woken_dl(struct rq *rq, struct task_struct *p)
2299{
2300 if (!task_running(rq, p) &&
2301 !test_tsk_need_resched(rq->curr) &&
2302 p->nr_cpus_allowed > 1 &&
2303 dl_task(rq->curr) &&
2304 (rq->curr->nr_cpus_allowed < 2 ||
2305 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
2306 push_dl_tasks(rq);
2307 }
2308}
2309
2310static void set_cpus_allowed_dl(struct task_struct *p,
2311 const struct cpumask *new_mask)
2312{
2313 struct root_domain *src_rd;
2314 struct rq *rq;
2315
2316 BUG_ON(!dl_task(p));
2317
2318 rq = task_rq(p);
2319 src_rd = rq->rd;
2320 /*
2321 * Migrating a SCHED_DEADLINE task between exclusive
2322 * cpusets (different root_domains) entails a bandwidth
2323 * update. We already made space for us in the destination
2324 * domain (see cpuset_can_attach()).
2325 */
2326 if (!cpumask_intersects(src_rd->span, new_mask)) {
2327 struct dl_bw *src_dl_b;
2328
2329 src_dl_b = dl_bw_of(cpu_of(rq));
2330 /*
2331 * We now free resources of the root_domain we are migrating
2332 * off. In the worst case, sched_setattr() may temporary fail
2333 * until we complete the update.
2334 */
2335 raw_spin_lock(&src_dl_b->lock);
2336 __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
2337 raw_spin_unlock(&src_dl_b->lock);
2338 }
2339
2340 set_cpus_allowed_common(p, new_mask);
2341}
2342
2343/* Assumes rq->lock is held */
2344static void rq_online_dl(struct rq *rq)
2345{
2346 if (rq->dl.overloaded)
2347 dl_set_overload(rq);
2348
2349 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
2350 if (rq->dl.dl_nr_running > 0)
2351 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
2352}
2353
2354/* Assumes rq->lock is held */
2355static void rq_offline_dl(struct rq *rq)
2356{
2357 if (rq->dl.overloaded)
2358 dl_clear_overload(rq);
2359
2360 cpudl_clear(&rq->rd->cpudl, rq->cpu);
2361 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
2362}
2363
2364void __init init_sched_dl_class(void)
2365{
2366 unsigned int i;
2367
2368 for_each_possible_cpu(i)
2369 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2370 GFP_KERNEL, cpu_to_node(i));
2371}
2372
David Brazdil0f672f62019-12-10 10:32:29 +00002373void dl_add_task_root_domain(struct task_struct *p)
2374{
2375 struct rq_flags rf;
2376 struct rq *rq;
2377 struct dl_bw *dl_b;
2378
2379 rq = task_rq_lock(p, &rf);
2380 if (!dl_task(p))
2381 goto unlock;
2382
2383 dl_b = &rq->rd->dl_bw;
2384 raw_spin_lock(&dl_b->lock);
2385
2386 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
2387
2388 raw_spin_unlock(&dl_b->lock);
2389
2390unlock:
2391 task_rq_unlock(rq, p, &rf);
2392}
2393
2394void dl_clear_root_domain(struct root_domain *rd)
2395{
2396 unsigned long flags;
2397
2398 raw_spin_lock_irqsave(&rd->dl_bw.lock, flags);
2399 rd->dl_bw.total_bw = 0;
2400 raw_spin_unlock_irqrestore(&rd->dl_bw.lock, flags);
2401}
2402
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002403#endif /* CONFIG_SMP */
2404
2405static void switched_from_dl(struct rq *rq, struct task_struct *p)
2406{
2407 /*
2408 * task_non_contending() can start the "inactive timer" (if the 0-lag
2409 * time is in the future). If the task switches back to dl before
2410 * the "inactive timer" fires, it can continue to consume its current
2411 * runtime using its current deadline. If it stays outside of
2412 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2413 * will reset the task parameters.
2414 */
2415 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2416 task_non_contending(p);
2417
2418 if (!task_on_rq_queued(p)) {
2419 /*
2420 * Inactive timer is armed. However, p is leaving DEADLINE and
2421 * might migrate away from this rq while continuing to run on
2422 * some other class. We need to remove its contribution from
2423 * this rq running_bw now, or sub_rq_bw (below) will complain.
2424 */
2425 if (p->dl.dl_non_contending)
2426 sub_running_bw(&p->dl, &rq->dl);
2427 sub_rq_bw(&p->dl, &rq->dl);
2428 }
2429
2430 /*
2431 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2432 * at the 0-lag time, because the task could have been migrated
2433 * while SCHED_OTHER in the meanwhile.
2434 */
2435 if (p->dl.dl_non_contending)
2436 p->dl.dl_non_contending = 0;
2437
2438 /*
2439 * Since this might be the only -deadline task on the rq,
2440 * this is the right place to try to pull some other one
2441 * from an overloaded CPU, if any.
2442 */
2443 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2444 return;
2445
2446 deadline_queue_pull_task(rq);
2447}
2448
2449/*
2450 * When switching to -deadline, we may overload the rq, then
2451 * we try to push someone off, if possible.
2452 */
2453static void switched_to_dl(struct rq *rq, struct task_struct *p)
2454{
2455 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2456 put_task_struct(p);
2457
2458 /* If p is not queued we will update its parameters at next wakeup. */
2459 if (!task_on_rq_queued(p)) {
2460 add_rq_bw(&p->dl, &rq->dl);
2461
2462 return;
2463 }
2464
2465 if (rq->curr != p) {
2466#ifdef CONFIG_SMP
2467 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
2468 deadline_queue_push_tasks(rq);
2469#endif
2470 if (dl_task(rq->curr))
2471 check_preempt_curr_dl(rq, p, 0);
2472 else
2473 resched_curr(rq);
Olivier Deprez0e641232021-09-23 10:07:05 +02002474 } else {
2475 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002476 }
2477}
2478
2479/*
2480 * If the scheduling parameters of a -deadline task changed,
2481 * a push or pull operation might be needed.
2482 */
2483static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2484 int oldprio)
2485{
2486 if (task_on_rq_queued(p) || rq->curr == p) {
2487#ifdef CONFIG_SMP
2488 /*
2489 * This might be too much, but unfortunately
2490 * we don't have the old deadline value, and
2491 * we can't argue if the task is increasing
2492 * or lowering its prio, so...
2493 */
2494 if (!rq->dl.overloaded)
2495 deadline_queue_pull_task(rq);
2496
2497 /*
2498 * If we now have a earlier deadline task than p,
2499 * then reschedule, provided p is still on this
2500 * runqueue.
2501 */
2502 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
2503 resched_curr(rq);
2504#else
2505 /*
2506 * Again, we don't know if p has a earlier
2507 * or later deadline, so let's blindly set a
2508 * (maybe not needed) rescheduling point.
2509 */
2510 resched_curr(rq);
2511#endif /* CONFIG_SMP */
2512 }
2513}
2514
Olivier Deprez157378f2022-04-04 15:47:50 +02002515const struct sched_class dl_sched_class
2516 __section("__dl_sched_class") = {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002517 .enqueue_task = enqueue_task_dl,
2518 .dequeue_task = dequeue_task_dl,
2519 .yield_task = yield_task_dl,
2520
2521 .check_preempt_curr = check_preempt_curr_dl,
2522
2523 .pick_next_task = pick_next_task_dl,
2524 .put_prev_task = put_prev_task_dl,
David Brazdil0f672f62019-12-10 10:32:29 +00002525 .set_next_task = set_next_task_dl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002526
2527#ifdef CONFIG_SMP
David Brazdil0f672f62019-12-10 10:32:29 +00002528 .balance = balance_dl,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002529 .select_task_rq = select_task_rq_dl,
2530 .migrate_task_rq = migrate_task_rq_dl,
2531 .set_cpus_allowed = set_cpus_allowed_dl,
2532 .rq_online = rq_online_dl,
2533 .rq_offline = rq_offline_dl,
2534 .task_woken = task_woken_dl,
2535#endif
2536
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002537 .task_tick = task_tick_dl,
2538 .task_fork = task_fork_dl,
2539
2540 .prio_changed = prio_changed_dl,
2541 .switched_from = switched_from_dl,
2542 .switched_to = switched_to_dl,
2543
2544 .update_curr = update_curr_dl,
2545};
2546
2547int sched_dl_global_validate(void)
2548{
2549 u64 runtime = global_rt_runtime();
2550 u64 period = global_rt_period();
2551 u64 new_bw = to_ratio(period, runtime);
2552 struct dl_bw *dl_b;
Olivier Deprez0e641232021-09-23 10:07:05 +02002553 int cpu, cpus, ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002554 unsigned long flags;
2555
2556 /*
2557 * Here we want to check the bandwidth not being set to some
2558 * value smaller than the currently allocated bandwidth in
2559 * any of the root_domains.
2560 *
2561 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
2562 * cycling on root_domains... Discussion on different/better
2563 * solutions is welcome!
2564 */
2565 for_each_possible_cpu(cpu) {
2566 rcu_read_lock_sched();
2567 dl_b = dl_bw_of(cpu);
Olivier Deprez0e641232021-09-23 10:07:05 +02002568 cpus = dl_bw_cpus(cpu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002569
2570 raw_spin_lock_irqsave(&dl_b->lock, flags);
Olivier Deprez0e641232021-09-23 10:07:05 +02002571 if (new_bw * cpus < dl_b->total_bw)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002572 ret = -EBUSY;
2573 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2574
2575 rcu_read_unlock_sched();
2576
2577 if (ret)
2578 break;
2579 }
2580
2581 return ret;
2582}
2583
Olivier Deprez157378f2022-04-04 15:47:50 +02002584static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002585{
2586 if (global_rt_runtime() == RUNTIME_INF) {
2587 dl_rq->bw_ratio = 1 << RATIO_SHIFT;
2588 dl_rq->extra_bw = 1 << BW_SHIFT;
2589 } else {
2590 dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
2591 global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
2592 dl_rq->extra_bw = to_ratio(global_rt_period(),
2593 global_rt_runtime());
2594 }
2595}
2596
2597void sched_dl_do_global(void)
2598{
2599 u64 new_bw = -1;
2600 struct dl_bw *dl_b;
2601 int cpu;
2602 unsigned long flags;
2603
2604 def_dl_bandwidth.dl_period = global_rt_period();
2605 def_dl_bandwidth.dl_runtime = global_rt_runtime();
2606
2607 if (global_rt_runtime() != RUNTIME_INF)
2608 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
2609
2610 /*
2611 * FIXME: As above...
2612 */
2613 for_each_possible_cpu(cpu) {
2614 rcu_read_lock_sched();
2615 dl_b = dl_bw_of(cpu);
2616
2617 raw_spin_lock_irqsave(&dl_b->lock, flags);
2618 dl_b->bw = new_bw;
2619 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2620
2621 rcu_read_unlock_sched();
2622 init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
2623 }
2624}
2625
2626/*
2627 * We must be sure that accepting a new task (or allowing changing the
2628 * parameters of an existing one) is consistent with the bandwidth
2629 * constraints. If yes, this function also accordingly updates the currently
2630 * allocated bandwidth to reflect the new situation.
2631 *
2632 * This function is called while holding p's rq->lock.
2633 */
2634int sched_dl_overflow(struct task_struct *p, int policy,
2635 const struct sched_attr *attr)
2636{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002637 u64 period = attr->sched_period ?: attr->sched_deadline;
2638 u64 runtime = attr->sched_runtime;
2639 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002640 int cpus, err = -1, cpu = task_cpu(p);
2641 struct dl_bw *dl_b = dl_bw_of(cpu);
2642 unsigned long cap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002643
2644 if (attr->sched_flags & SCHED_FLAG_SUGOV)
2645 return 0;
2646
2647 /* !deadline task may carry old deadline bandwidth */
2648 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
2649 return 0;
2650
2651 /*
2652 * Either if a task, enters, leave, or stays -deadline but changes
2653 * its parameters, we may need to update accordingly the total
2654 * allocated bandwidth of the container.
2655 */
2656 raw_spin_lock(&dl_b->lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02002657 cpus = dl_bw_cpus(cpu);
2658 cap = dl_bw_capacity(cpu);
2659
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002660 if (dl_policy(policy) && !task_has_dl_policy(p) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02002661 !__dl_overflow(dl_b, cap, 0, new_bw)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002662 if (hrtimer_active(&p->dl.inactive_timer))
2663 __dl_sub(dl_b, p->dl.dl_bw, cpus);
2664 __dl_add(dl_b, new_bw, cpus);
2665 err = 0;
2666 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02002667 !__dl_overflow(dl_b, cap, p->dl.dl_bw, new_bw)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002668 /*
2669 * XXX this is slightly incorrect: when the task
2670 * utilization decreases, we should delay the total
2671 * utilization change until the task's 0-lag point.
2672 * But this would require to set the task's "inactive
2673 * timer" when the task is not inactive.
2674 */
2675 __dl_sub(dl_b, p->dl.dl_bw, cpus);
2676 __dl_add(dl_b, new_bw, cpus);
2677 dl_change_utilization(p, new_bw);
2678 err = 0;
2679 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2680 /*
2681 * Do not decrease the total deadline utilization here,
2682 * switched_from_dl() will take care to do it at the correct
2683 * (0-lag) time.
2684 */
2685 err = 0;
2686 }
2687 raw_spin_unlock(&dl_b->lock);
2688
2689 return err;
2690}
2691
2692/*
2693 * This function initializes the sched_dl_entity of a newly becoming
2694 * SCHED_DEADLINE task.
2695 *
2696 * Only the static values are considered here, the actual runtime and the
2697 * absolute deadline will be properly calculated when the task is enqueued
2698 * for the first time with its new policy.
2699 */
2700void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
2701{
2702 struct sched_dl_entity *dl_se = &p->dl;
2703
2704 dl_se->dl_runtime = attr->sched_runtime;
2705 dl_se->dl_deadline = attr->sched_deadline;
2706 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
Olivier Deprez0e641232021-09-23 10:07:05 +02002707 dl_se->flags = attr->sched_flags & SCHED_DL_FLAGS;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002708 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
2709 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
2710}
2711
2712void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
2713{
2714 struct sched_dl_entity *dl_se = &p->dl;
2715
2716 attr->sched_priority = p->rt_priority;
2717 attr->sched_runtime = dl_se->dl_runtime;
2718 attr->sched_deadline = dl_se->dl_deadline;
2719 attr->sched_period = dl_se->dl_period;
Olivier Deprez0e641232021-09-23 10:07:05 +02002720 attr->sched_flags &= ~SCHED_DL_FLAGS;
2721 attr->sched_flags |= dl_se->flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002722}
2723
2724/*
Olivier Deprez157378f2022-04-04 15:47:50 +02002725 * Default limits for DL period; on the top end we guard against small util
2726 * tasks still getting rediculous long effective runtimes, on the bottom end we
2727 * guard against timer DoS.
2728 */
2729unsigned int sysctl_sched_dl_period_max = 1 << 22; /* ~4 seconds */
2730unsigned int sysctl_sched_dl_period_min = 100; /* 100 us */
2731
2732/*
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002733 * This function validates the new parameters of a -deadline task.
2734 * We ask for the deadline not being zero, and greater or equal
2735 * than the runtime, as well as the period of being zero or
2736 * greater than deadline. Furthermore, we have to be sure that
2737 * user parameters are above the internal resolution of 1us (we
2738 * check sched_runtime only since it is always the smaller one) and
2739 * below 2^63 ns (we have to check both sched_deadline and
2740 * sched_period, as the latter can be zero).
2741 */
2742bool __checkparam_dl(const struct sched_attr *attr)
2743{
Olivier Deprez157378f2022-04-04 15:47:50 +02002744 u64 period, max, min;
2745
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002746 /* special dl tasks don't actually use any parameter */
2747 if (attr->sched_flags & SCHED_FLAG_SUGOV)
2748 return true;
2749
2750 /* deadline != 0 */
2751 if (attr->sched_deadline == 0)
2752 return false;
2753
2754 /*
2755 * Since we truncate DL_SCALE bits, make sure we're at least
2756 * that big.
2757 */
2758 if (attr->sched_runtime < (1ULL << DL_SCALE))
2759 return false;
2760
2761 /*
2762 * Since we use the MSB for wrap-around and sign issues, make
2763 * sure it's not set (mind that period can be equal to zero).
2764 */
2765 if (attr->sched_deadline & (1ULL << 63) ||
2766 attr->sched_period & (1ULL << 63))
2767 return false;
2768
Olivier Deprez157378f2022-04-04 15:47:50 +02002769 period = attr->sched_period;
2770 if (!period)
2771 period = attr->sched_deadline;
2772
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002773 /* runtime <= deadline <= period (if period != 0) */
Olivier Deprez157378f2022-04-04 15:47:50 +02002774 if (period < attr->sched_deadline ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002775 attr->sched_deadline < attr->sched_runtime)
2776 return false;
2777
Olivier Deprez157378f2022-04-04 15:47:50 +02002778 max = (u64)READ_ONCE(sysctl_sched_dl_period_max) * NSEC_PER_USEC;
2779 min = (u64)READ_ONCE(sysctl_sched_dl_period_min) * NSEC_PER_USEC;
2780
2781 if (period < min || period > max)
2782 return false;
2783
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002784 return true;
2785}
2786
2787/*
2788 * This function clears the sched_dl_entity static params.
2789 */
2790void __dl_clear_params(struct task_struct *p)
2791{
2792 struct sched_dl_entity *dl_se = &p->dl;
2793
2794 dl_se->dl_runtime = 0;
2795 dl_se->dl_deadline = 0;
2796 dl_se->dl_period = 0;
2797 dl_se->flags = 0;
2798 dl_se->dl_bw = 0;
2799 dl_se->dl_density = 0;
2800
2801 dl_se->dl_throttled = 0;
2802 dl_se->dl_yielded = 0;
2803 dl_se->dl_non_contending = 0;
2804 dl_se->dl_overrun = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02002805
2806#ifdef CONFIG_RT_MUTEXES
2807 dl_se->pi_se = dl_se;
2808#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002809}
2810
2811bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
2812{
2813 struct sched_dl_entity *dl_se = &p->dl;
2814
2815 if (dl_se->dl_runtime != attr->sched_runtime ||
2816 dl_se->dl_deadline != attr->sched_deadline ||
2817 dl_se->dl_period != attr->sched_period ||
Olivier Deprez0e641232021-09-23 10:07:05 +02002818 dl_se->flags != (attr->sched_flags & SCHED_DL_FLAGS))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002819 return true;
2820
2821 return false;
2822}
2823
2824#ifdef CONFIG_SMP
2825int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed)
2826{
Olivier Deprez157378f2022-04-04 15:47:50 +02002827 unsigned long flags, cap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002828 unsigned int dest_cpu;
2829 struct dl_bw *dl_b;
2830 bool overflow;
Olivier Deprez157378f2022-04-04 15:47:50 +02002831 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002832
2833 dest_cpu = cpumask_any_and(cpu_active_mask, cs_cpus_allowed);
2834
2835 rcu_read_lock_sched();
2836 dl_b = dl_bw_of(dest_cpu);
2837 raw_spin_lock_irqsave(&dl_b->lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02002838 cap = dl_bw_capacity(dest_cpu);
2839 overflow = __dl_overflow(dl_b, cap, 0, p->dl.dl_bw);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002840 if (overflow) {
2841 ret = -EBUSY;
2842 } else {
2843 /*
2844 * We reserve space for this task in the destination
2845 * root_domain, as we can't fail after this point.
2846 * We will free resources in the source root_domain
2847 * later on (see set_cpus_allowed_dl()).
2848 */
Olivier Deprez157378f2022-04-04 15:47:50 +02002849 int cpus = dl_bw_cpus(dest_cpu);
2850
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002851 __dl_add(dl_b, p->dl.dl_bw, cpus);
2852 ret = 0;
2853 }
2854 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2855 rcu_read_unlock_sched();
2856
2857 return ret;
2858}
2859
2860int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
2861 const struct cpumask *trial)
2862{
2863 int ret = 1, trial_cpus;
2864 struct dl_bw *cur_dl_b;
2865 unsigned long flags;
2866
2867 rcu_read_lock_sched();
2868 cur_dl_b = dl_bw_of(cpumask_any(cur));
2869 trial_cpus = cpumask_weight(trial);
2870
2871 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
2872 if (cur_dl_b->bw != -1 &&
2873 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
2874 ret = 0;
2875 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
2876 rcu_read_unlock_sched();
2877
2878 return ret;
2879}
2880
2881bool dl_cpu_busy(unsigned int cpu)
2882{
Olivier Deprez157378f2022-04-04 15:47:50 +02002883 unsigned long flags, cap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002884 struct dl_bw *dl_b;
2885 bool overflow;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002886
2887 rcu_read_lock_sched();
2888 dl_b = dl_bw_of(cpu);
2889 raw_spin_lock_irqsave(&dl_b->lock, flags);
Olivier Deprez157378f2022-04-04 15:47:50 +02002890 cap = dl_bw_capacity(cpu);
2891 overflow = __dl_overflow(dl_b, cap, 0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002892 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2893 rcu_read_unlock_sched();
2894
2895 return overflow;
2896}
2897#endif
2898
2899#ifdef CONFIG_SCHED_DEBUG
2900void print_dl_stats(struct seq_file *m, int cpu)
2901{
2902 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
2903}
2904#endif /* CONFIG_SCHED_DEBUG */