1 // 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
19 #include <linux/cpuset.h>
20 #include <linux/sched/clock.h>
21 #include <uapi/linux/sched/types.h>
22 #include "sched.h"
23 #include "pelt.h"
24
25 /*
26 * Default limits for DL period; on the top end we guard against small util
27 * tasks still getting ridiculously long effective runtimes, on the bottom end we
28 * guard against timer DoS.
29 */
30 static unsigned int sysctl_sched_dl_period_max = 1 << 22; /* ~4 seconds */
31 static unsigned int sysctl_sched_dl_period_min = 100; /* 100 us */
32 #ifdef CONFIG_SYSCTL
33 static const struct ctl_table sched_dl_sysctls[] = {
34 {
35 .procname = "sched_deadline_period_max_us",
36 .data = &sysctl_sched_dl_period_max,
37 .maxlen = sizeof(unsigned int),
38 .mode = 0644,
39 .proc_handler = proc_douintvec_minmax,
40 .extra1 = (void *)&sysctl_sched_dl_period_min,
41 },
42 {
43 .procname = "sched_deadline_period_min_us",
44 .data = &sysctl_sched_dl_period_min,
45 .maxlen = sizeof(unsigned int),
46 .mode = 0644,
47 .proc_handler = proc_douintvec_minmax,
48 .extra2 = (void *)&sysctl_sched_dl_period_max,
49 },
50 };
51
sched_dl_sysctl_init(void)52 static int __init sched_dl_sysctl_init(void)
53 {
54 register_sysctl_init("kernel", sched_dl_sysctls);
55 return 0;
56 }
57 late_initcall(sched_dl_sysctl_init);
58 #endif /* CONFIG_SYSCTL */
59
dl_server(struct sched_dl_entity * dl_se)60 static bool dl_server(struct sched_dl_entity *dl_se)
61 {
62 return dl_se->dl_server;
63 }
64
dl_task_of(struct sched_dl_entity * dl_se)65 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
66 {
67 BUG_ON(dl_server(dl_se));
68 return container_of(dl_se, struct task_struct, dl);
69 }
70
rq_of_dl_rq(struct dl_rq * dl_rq)71 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
72 {
73 return container_of(dl_rq, struct rq, dl);
74 }
75
rq_of_dl_se(struct sched_dl_entity * dl_se)76 static inline struct rq *rq_of_dl_se(struct sched_dl_entity *dl_se)
77 {
78 struct rq *rq = dl_se->rq;
79
80 if (!dl_server(dl_se))
81 rq = task_rq(dl_task_of(dl_se));
82
83 return rq;
84 }
85
dl_rq_of_se(struct sched_dl_entity * dl_se)86 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
87 {
88 return &rq_of_dl_se(dl_se)->dl;
89 }
90
on_dl_rq(struct sched_dl_entity * dl_se)91 static inline int on_dl_rq(struct sched_dl_entity *dl_se)
92 {
93 return !RB_EMPTY_NODE(&dl_se->rb_node);
94 }
95
96 #ifdef CONFIG_RT_MUTEXES
pi_of(struct sched_dl_entity * dl_se)97 static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
98 {
99 return dl_se->pi_se;
100 }
101
is_dl_boosted(struct sched_dl_entity * dl_se)102 static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
103 {
104 return pi_of(dl_se) != dl_se;
105 }
106 #else /* !CONFIG_RT_MUTEXES: */
pi_of(struct sched_dl_entity * dl_se)107 static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
108 {
109 return dl_se;
110 }
111
is_dl_boosted(struct sched_dl_entity * dl_se)112 static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
113 {
114 return false;
115 }
116 #endif /* !CONFIG_RT_MUTEXES */
117
dl_bw_of(int i)118 static inline struct dl_bw *dl_bw_of(int i)
119 {
120 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
121 "sched RCU must be held");
122 return &cpu_rq(i)->rd->dl_bw;
123 }
124
dl_bw_cpus(int i)125 static inline int dl_bw_cpus(int i)
126 {
127 struct root_domain *rd = cpu_rq(i)->rd;
128 int cpus;
129
130 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
131 "sched RCU must be held");
132
133 if (cpumask_subset(rd->span, cpu_active_mask))
134 return cpumask_weight(rd->span);
135
136 cpus = 0;
137
138 for_each_cpu_and(i, rd->span, cpu_active_mask)
139 cpus++;
140
141 return cpus;
142 }
143
__dl_bw_capacity(const struct cpumask * mask)144 static inline unsigned long __dl_bw_capacity(const struct cpumask *mask)
145 {
146 unsigned long cap = 0;
147 int i;
148
149 for_each_cpu_and(i, mask, cpu_active_mask)
150 cap += arch_scale_cpu_capacity(i);
151
152 return cap;
153 }
154
155 /*
156 * XXX Fix: If 'rq->rd == def_root_domain' perform AC against capacity
157 * of the CPU the task is running on rather rd's \Sum CPU capacity.
158 */
dl_bw_capacity(int i)159 static inline unsigned long dl_bw_capacity(int i)
160 {
161 if (!sched_asym_cpucap_active() &&
162 arch_scale_cpu_capacity(i) == SCHED_CAPACITY_SCALE) {
163 return dl_bw_cpus(i) << SCHED_CAPACITY_SHIFT;
164 } else {
165 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
166 "sched RCU must be held");
167
168 return __dl_bw_capacity(cpu_rq(i)->rd->span);
169 }
170 }
171
dl_bw_visited(int cpu,u64 cookie)172 bool dl_bw_visited(int cpu, u64 cookie)
173 {
174 struct root_domain *rd = cpu_rq(cpu)->rd;
175
176 if (rd->visit_cookie == cookie)
177 return true;
178
179 rd->visit_cookie = cookie;
180 return false;
181 }
182
183 static inline
__dl_update(struct dl_bw * dl_b,s64 bw)184 void __dl_update(struct dl_bw *dl_b, s64 bw)
185 {
186 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
187 int i;
188
189 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
190 "sched RCU must be held");
191 for_each_cpu_and(i, rd->span, cpu_active_mask) {
192 struct rq *rq = cpu_rq(i);
193
194 rq->dl.extra_bw += bw;
195 }
196 }
197
198 static inline
__dl_sub(struct dl_bw * dl_b,u64 tsk_bw,int cpus)199 void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
200 {
201 dl_b->total_bw -= tsk_bw;
202 __dl_update(dl_b, (s32)tsk_bw / cpus);
203 }
204
205 static inline
__dl_add(struct dl_bw * dl_b,u64 tsk_bw,int cpus)206 void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
207 {
208 dl_b->total_bw += tsk_bw;
209 __dl_update(dl_b, -((s32)tsk_bw / cpus));
210 }
211
212 static inline bool
__dl_overflow(struct dl_bw * dl_b,unsigned long cap,u64 old_bw,u64 new_bw)213 __dl_overflow(struct dl_bw *dl_b, unsigned long cap, u64 old_bw, u64 new_bw)
214 {
215 return dl_b->bw != -1 &&
216 cap_scale(dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw;
217 }
218
219 static inline
__add_running_bw(u64 dl_bw,struct dl_rq * dl_rq)220 void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
221 {
222 u64 old = dl_rq->running_bw;
223
224 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
225 dl_rq->running_bw += dl_bw;
226 WARN_ON_ONCE(dl_rq->running_bw < old); /* overflow */
227 WARN_ON_ONCE(dl_rq->running_bw > dl_rq->this_bw);
228 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
229 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
230 }
231
232 static inline
__sub_running_bw(u64 dl_bw,struct dl_rq * dl_rq)233 void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
234 {
235 u64 old = dl_rq->running_bw;
236
237 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
238 dl_rq->running_bw -= dl_bw;
239 WARN_ON_ONCE(dl_rq->running_bw > old); /* underflow */
240 if (dl_rq->running_bw > old)
241 dl_rq->running_bw = 0;
242 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
243 cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
244 }
245
246 static inline
__add_rq_bw(u64 dl_bw,struct dl_rq * dl_rq)247 void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
248 {
249 u64 old = dl_rq->this_bw;
250
251 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
252 dl_rq->this_bw += dl_bw;
253 WARN_ON_ONCE(dl_rq->this_bw < old); /* overflow */
254 }
255
256 static inline
__sub_rq_bw(u64 dl_bw,struct dl_rq * dl_rq)257 void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
258 {
259 u64 old = dl_rq->this_bw;
260
261 lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
262 dl_rq->this_bw -= dl_bw;
263 WARN_ON_ONCE(dl_rq->this_bw > old); /* underflow */
264 if (dl_rq->this_bw > old)
265 dl_rq->this_bw = 0;
266 WARN_ON_ONCE(dl_rq->running_bw > dl_rq->this_bw);
267 }
268
269 static inline
add_rq_bw(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)270 void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
271 {
272 if (!dl_entity_is_special(dl_se))
273 __add_rq_bw(dl_se->dl_bw, dl_rq);
274 }
275
276 static inline
sub_rq_bw(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)277 void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
278 {
279 if (!dl_entity_is_special(dl_se))
280 __sub_rq_bw(dl_se->dl_bw, dl_rq);
281 }
282
283 static inline
add_running_bw(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)284 void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
285 {
286 if (!dl_entity_is_special(dl_se))
287 __add_running_bw(dl_se->dl_bw, dl_rq);
288 }
289
290 static inline
sub_running_bw(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)291 void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
292 {
293 if (!dl_entity_is_special(dl_se))
294 __sub_running_bw(dl_se->dl_bw, dl_rq);
295 }
296
dl_rq_change_utilization(struct rq * rq,struct sched_dl_entity * dl_se,u64 new_bw)297 static void dl_rq_change_utilization(struct rq *rq, struct sched_dl_entity *dl_se, u64 new_bw)
298 {
299 if (dl_se->dl_non_contending) {
300 sub_running_bw(dl_se, &rq->dl);
301 dl_se->dl_non_contending = 0;
302
303 /*
304 * If the timer handler is currently running and the
305 * timer cannot be canceled, inactive_task_timer()
306 * will see that dl_not_contending is not set, and
307 * will not touch the rq's active utilization,
308 * so we are still safe.
309 */
310 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) {
311 if (!dl_server(dl_se))
312 put_task_struct(dl_task_of(dl_se));
313 }
314 }
315 __sub_rq_bw(dl_se->dl_bw, &rq->dl);
316 __add_rq_bw(new_bw, &rq->dl);
317 }
318
319 static __always_inline
cancel_dl_timer(struct sched_dl_entity * dl_se,struct hrtimer * timer)320 void cancel_dl_timer(struct sched_dl_entity *dl_se, struct hrtimer *timer)
321 {
322 /*
323 * If the timer callback was running (hrtimer_try_to_cancel == -1),
324 * it will eventually call put_task_struct().
325 */
326 if (hrtimer_try_to_cancel(timer) == 1 && !dl_server(dl_se))
327 put_task_struct(dl_task_of(dl_se));
328 }
329
330 static __always_inline
cancel_replenish_timer(struct sched_dl_entity * dl_se)331 void cancel_replenish_timer(struct sched_dl_entity *dl_se)
332 {
333 cancel_dl_timer(dl_se, &dl_se->dl_timer);
334 }
335
336 static __always_inline
cancel_inactive_timer(struct sched_dl_entity * dl_se)337 void cancel_inactive_timer(struct sched_dl_entity *dl_se)
338 {
339 cancel_dl_timer(dl_se, &dl_se->inactive_timer);
340 }
341
dl_change_utilization(struct task_struct * p,u64 new_bw)342 static void dl_change_utilization(struct task_struct *p, u64 new_bw)
343 {
344 WARN_ON_ONCE(p->dl.flags & SCHED_FLAG_SUGOV);
345
346 if (task_on_rq_queued(p))
347 return;
348
349 dl_rq_change_utilization(task_rq(p), &p->dl, new_bw);
350 }
351
352 static void __dl_clear_params(struct sched_dl_entity *dl_se);
353
354 /*
355 * The utilization of a task cannot be immediately removed from
356 * the rq active utilization (running_bw) when the task blocks.
357 * Instead, we have to wait for the so called "0-lag time".
358 *
359 * If a task blocks before the "0-lag time", a timer (the inactive
360 * timer) is armed, and running_bw is decreased when the timer
361 * fires.
362 *
363 * If the task wakes up again before the inactive timer fires,
364 * the timer is canceled, whereas if the task wakes up after the
365 * inactive timer fired (and running_bw has been decreased) the
366 * task's utilization has to be added to running_bw again.
367 * A flag in the deadline scheduling entity (dl_non_contending)
368 * is used to avoid race conditions between the inactive timer handler
369 * and task wakeups.
370 *
371 * The following diagram shows how running_bw is updated. A task is
372 * "ACTIVE" when its utilization contributes to running_bw; an
373 * "ACTIVE contending" task is in the TASK_RUNNING state, while an
374 * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
375 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
376 * time already passed, which does not contribute to running_bw anymore.
377 * +------------------+
378 * wakeup | ACTIVE |
379 * +------------------>+ contending |
380 * | add_running_bw | |
381 * | +----+------+------+
382 * | | ^
383 * | dequeue | |
384 * +--------+-------+ | |
385 * | | t >= 0-lag | | wakeup
386 * | INACTIVE |<---------------+ |
387 * | | sub_running_bw | |
388 * +--------+-------+ | |
389 * ^ | |
390 * | t < 0-lag | |
391 * | | |
392 * | V |
393 * | +----+------+------+
394 * | sub_running_bw | ACTIVE |
395 * +-------------------+ |
396 * inactive timer | non contending |
397 * fired +------------------+
398 *
399 * The task_non_contending() function is invoked when a task
400 * blocks, and checks if the 0-lag time already passed or
401 * not (in the first case, it directly updates running_bw;
402 * in the second case, it arms the inactive timer).
403 *
404 * The task_contending() function is invoked when a task wakes
405 * up, and checks if the task is still in the "ACTIVE non contending"
406 * state or not (in the second case, it updates running_bw).
407 */
task_non_contending(struct sched_dl_entity * dl_se)408 static void task_non_contending(struct sched_dl_entity *dl_se)
409 {
410 struct hrtimer *timer = &dl_se->inactive_timer;
411 struct rq *rq = rq_of_dl_se(dl_se);
412 struct dl_rq *dl_rq = &rq->dl;
413 s64 zerolag_time;
414
415 /*
416 * If this is a non-deadline task that has been boosted,
417 * do nothing
418 */
419 if (dl_se->dl_runtime == 0)
420 return;
421
422 if (dl_entity_is_special(dl_se))
423 return;
424
425 WARN_ON(dl_se->dl_non_contending);
426
427 zerolag_time = dl_se->deadline -
428 div64_long((dl_se->runtime * dl_se->dl_period),
429 dl_se->dl_runtime);
430
431 /*
432 * Using relative times instead of the absolute "0-lag time"
433 * allows to simplify the code
434 */
435 zerolag_time -= rq_clock(rq);
436
437 /*
438 * If the "0-lag time" already passed, decrease the active
439 * utilization now, instead of starting a timer
440 */
441 if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
442 if (dl_server(dl_se)) {
443 sub_running_bw(dl_se, dl_rq);
444 } else {
445 struct task_struct *p = dl_task_of(dl_se);
446
447 if (dl_task(p))
448 sub_running_bw(dl_se, dl_rq);
449
450 if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) {
451 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
452
453 if (READ_ONCE(p->__state) == TASK_DEAD)
454 sub_rq_bw(dl_se, &rq->dl);
455 raw_spin_lock(&dl_b->lock);
456 __dl_sub(dl_b, dl_se->dl_bw, dl_bw_cpus(task_cpu(p)));
457 raw_spin_unlock(&dl_b->lock);
458 __dl_clear_params(dl_se);
459 }
460 }
461
462 return;
463 }
464
465 dl_se->dl_non_contending = 1;
466 if (!dl_server(dl_se))
467 get_task_struct(dl_task_of(dl_se));
468
469 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD);
470 }
471
task_contending(struct sched_dl_entity * dl_se,int flags)472 static void task_contending(struct sched_dl_entity *dl_se, int flags)
473 {
474 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
475
476 /*
477 * If this is a non-deadline task that has been boosted,
478 * do nothing
479 */
480 if (dl_se->dl_runtime == 0)
481 return;
482
483 if (flags & ENQUEUE_MIGRATED)
484 add_rq_bw(dl_se, dl_rq);
485
486 if (dl_se->dl_non_contending) {
487 dl_se->dl_non_contending = 0;
488 /*
489 * If the timer handler is currently running and the
490 * timer cannot be canceled, inactive_task_timer()
491 * will see that dl_not_contending is not set, and
492 * will not touch the rq's active utilization,
493 * so we are still safe.
494 */
495 cancel_inactive_timer(dl_se);
496 } else {
497 /*
498 * Since "dl_non_contending" is not set, the
499 * task's utilization has already been removed from
500 * active utilization (either when the task blocked,
501 * when the "inactive timer" fired).
502 * So, add it back.
503 */
504 add_running_bw(dl_se, dl_rq);
505 }
506 }
507
is_leftmost(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)508 static inline int is_leftmost(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
509 {
510 return rb_first_cached(&dl_rq->root) == &dl_se->rb_node;
511 }
512
513 static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
514
init_dl_bw(struct dl_bw * dl_b)515 void init_dl_bw(struct dl_bw *dl_b)
516 {
517 raw_spin_lock_init(&dl_b->lock);
518 if (global_rt_runtime() == RUNTIME_INF)
519 dl_b->bw = -1;
520 else
521 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
522 dl_b->total_bw = 0;
523 }
524
init_dl_rq(struct dl_rq * dl_rq)525 void init_dl_rq(struct dl_rq *dl_rq)
526 {
527 dl_rq->root = RB_ROOT_CACHED;
528
529 /* zero means no -deadline tasks */
530 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
531
532 dl_rq->overloaded = 0;
533 dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
534
535 dl_rq->running_bw = 0;
536 dl_rq->this_bw = 0;
537 init_dl_rq_bw_ratio(dl_rq);
538 }
539
dl_overloaded(struct rq * rq)540 static inline int dl_overloaded(struct rq *rq)
541 {
542 return atomic_read(&rq->rd->dlo_count);
543 }
544
dl_set_overload(struct rq * rq)545 static inline void dl_set_overload(struct rq *rq)
546 {
547 if (!rq->online)
548 return;
549
550 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
551 /*
552 * Must be visible before the overload count is
553 * set (as in sched_rt.c).
554 *
555 * Matched by the barrier in pull_dl_task().
556 */
557 smp_wmb();
558 atomic_inc(&rq->rd->dlo_count);
559 }
560
dl_clear_overload(struct rq * rq)561 static inline void dl_clear_overload(struct rq *rq)
562 {
563 if (!rq->online)
564 return;
565
566 atomic_dec(&rq->rd->dlo_count);
567 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
568 }
569
570 #define __node_2_pdl(node) \
571 rb_entry((node), struct task_struct, pushable_dl_tasks)
572
__pushable_less(struct rb_node * a,const struct rb_node * b)573 static inline bool __pushable_less(struct rb_node *a, const struct rb_node *b)
574 {
575 return dl_entity_preempt(&__node_2_pdl(a)->dl, &__node_2_pdl(b)->dl);
576 }
577
has_pushable_dl_tasks(struct rq * rq)578 static inline int has_pushable_dl_tasks(struct rq *rq)
579 {
580 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
581 }
582
583 /*
584 * The list of pushable -deadline task is not a plist, like in
585 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
586 */
enqueue_pushable_dl_task(struct rq * rq,struct task_struct * p)587 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
588 {
589 struct rb_node *leftmost;
590
591 WARN_ON_ONCE(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
592
593 leftmost = rb_add_cached(&p->pushable_dl_tasks,
594 &rq->dl.pushable_dl_tasks_root,
595 __pushable_less);
596 if (leftmost)
597 rq->dl.earliest_dl.next = p->dl.deadline;
598
599 if (!rq->dl.overloaded) {
600 dl_set_overload(rq);
601 rq->dl.overloaded = 1;
602 }
603 }
604
dequeue_pushable_dl_task(struct rq * rq,struct task_struct * p)605 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
606 {
607 struct dl_rq *dl_rq = &rq->dl;
608 struct rb_root_cached *root = &dl_rq->pushable_dl_tasks_root;
609 struct rb_node *leftmost;
610
611 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
612 return;
613
614 leftmost = rb_erase_cached(&p->pushable_dl_tasks, root);
615 if (leftmost)
616 dl_rq->earliest_dl.next = __node_2_pdl(leftmost)->dl.deadline;
617
618 RB_CLEAR_NODE(&p->pushable_dl_tasks);
619
620 if (!has_pushable_dl_tasks(rq) && rq->dl.overloaded) {
621 dl_clear_overload(rq);
622 rq->dl.overloaded = 0;
623 }
624 }
625
626 static int push_dl_task(struct rq *rq);
627
need_pull_dl_task(struct rq * rq,struct task_struct * prev)628 static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
629 {
630 return rq->online && dl_task(prev);
631 }
632
633 static DEFINE_PER_CPU(struct balance_callback, dl_push_head);
634 static DEFINE_PER_CPU(struct balance_callback, dl_pull_head);
635
636 static void push_dl_tasks(struct rq *);
637 static void pull_dl_task(struct rq *);
638
deadline_queue_push_tasks(struct rq * rq)639 static inline void deadline_queue_push_tasks(struct rq *rq)
640 {
641 if (!has_pushable_dl_tasks(rq))
642 return;
643
644 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
645 }
646
deadline_queue_pull_task(struct rq * rq)647 static inline void deadline_queue_pull_task(struct rq *rq)
648 {
649 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
650 }
651
652 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
653
dl_task_offline_migration(struct rq * rq,struct task_struct * p)654 static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
655 {
656 struct rq *later_rq = NULL;
657 struct dl_bw *dl_b;
658
659 later_rq = find_lock_later_rq(p, rq);
660 if (!later_rq) {
661 int cpu;
662
663 /*
664 * If we cannot preempt any rq, fall back to pick any
665 * online CPU:
666 */
667 cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
668 if (cpu >= nr_cpu_ids) {
669 /*
670 * Failed to find any suitable CPU.
671 * The task will never come back!
672 */
673 WARN_ON_ONCE(dl_bandwidth_enabled());
674
675 /*
676 * If admission control is disabled we
677 * try a little harder to let the task
678 * run.
679 */
680 cpu = cpumask_any(cpu_active_mask);
681 }
682 later_rq = cpu_rq(cpu);
683 double_lock_balance(rq, later_rq);
684 }
685
686 if (p->dl.dl_non_contending || p->dl.dl_throttled) {
687 /*
688 * Inactive timer is armed (or callback is running, but
689 * waiting for us to release rq locks). In any case, when it
690 * will fire (or continue), it will see running_bw of this
691 * task migrated to later_rq (and correctly handle it).
692 */
693 sub_running_bw(&p->dl, &rq->dl);
694 sub_rq_bw(&p->dl, &rq->dl);
695
696 add_rq_bw(&p->dl, &later_rq->dl);
697 add_running_bw(&p->dl, &later_rq->dl);
698 } else {
699 sub_rq_bw(&p->dl, &rq->dl);
700 add_rq_bw(&p->dl, &later_rq->dl);
701 }
702
703 /*
704 * And we finally need to fix up root_domain(s) bandwidth accounting,
705 * since p is still hanging out in the old (now moved to default) root
706 * domain.
707 */
708 dl_b = &rq->rd->dl_bw;
709 raw_spin_lock(&dl_b->lock);
710 __dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
711 raw_spin_unlock(&dl_b->lock);
712
713 dl_b = &later_rq->rd->dl_bw;
714 raw_spin_lock(&dl_b->lock);
715 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span));
716 raw_spin_unlock(&dl_b->lock);
717
718 set_task_cpu(p, later_rq->cpu);
719 double_unlock_balance(later_rq, rq);
720
721 return later_rq;
722 }
723
724 static void
725 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags);
726 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
727 static void dequeue_dl_entity(struct sched_dl_entity *dl_se, int flags);
728 static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p, int flags);
729
replenish_dl_new_period(struct sched_dl_entity * dl_se,struct rq * rq)730 static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se,
731 struct rq *rq)
732 {
733 /* for non-boosted task, pi_of(dl_se) == dl_se */
734 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
735 dl_se->runtime = pi_of(dl_se)->dl_runtime;
736
737 /*
738 * If it is a deferred reservation, and the server
739 * is not handling an starvation case, defer it.
740 */
741 if (dl_se->dl_defer && !dl_se->dl_defer_running) {
742 dl_se->dl_throttled = 1;
743 dl_se->dl_defer_armed = 1;
744 }
745 }
746
747 /*
748 * We are being explicitly informed that a new instance is starting,
749 * and this means that:
750 * - the absolute deadline of the entity has to be placed at
751 * current time + relative deadline;
752 * - the runtime of the entity has to be set to the maximum value.
753 *
754 * The capability of specifying such event is useful whenever a -deadline
755 * entity wants to (try to!) synchronize its behaviour with the scheduler's
756 * one, and to (try to!) reconcile itself with its own scheduling
757 * parameters.
758 */
setup_new_dl_entity(struct sched_dl_entity * dl_se)759 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
760 {
761 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
762 struct rq *rq = rq_of_dl_rq(dl_rq);
763
764 update_rq_clock(rq);
765
766 WARN_ON(is_dl_boosted(dl_se));
767 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
768
769 /*
770 * We are racing with the deadline timer. So, do nothing because
771 * the deadline timer handler will take care of properly recharging
772 * the runtime and postponing the deadline
773 */
774 if (dl_se->dl_throttled)
775 return;
776
777 /*
778 * We use the regular wall clock time to set deadlines in the
779 * future; in fact, we must consider execution overheads (time
780 * spent on hardirq context, etc.).
781 */
782 replenish_dl_new_period(dl_se, rq);
783 }
784
785 static int start_dl_timer(struct sched_dl_entity *dl_se);
786 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t);
787
788 /*
789 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
790 * possibility of a entity lasting more than what it declared, and thus
791 * exhausting its runtime.
792 *
793 * Here we are interested in making runtime overrun possible, but we do
794 * not want a entity which is misbehaving to affect the scheduling of all
795 * other entities.
796 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
797 * is used, in order to confine each entity within its own bandwidth.
798 *
799 * This function deals exactly with that, and ensures that when the runtime
800 * of a entity is replenished, its deadline is also postponed. That ensures
801 * the overrunning entity can't interfere with other entity in the system and
802 * can't make them miss their deadlines. Reasons why this kind of overruns
803 * could happen are, typically, a entity voluntarily trying to overcome its
804 * runtime, or it just underestimated it during sched_setattr().
805 */
replenish_dl_entity(struct sched_dl_entity * dl_se)806 static void replenish_dl_entity(struct sched_dl_entity *dl_se)
807 {
808 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
809 struct rq *rq = rq_of_dl_rq(dl_rq);
810
811 WARN_ON_ONCE(pi_of(dl_se)->dl_runtime <= 0);
812
813 /*
814 * This could be the case for a !-dl task that is boosted.
815 * Just go with full inherited parameters.
816 *
817 * Or, it could be the case of a deferred reservation that
818 * was not able to consume its runtime in background and
819 * reached this point with current u > U.
820 *
821 * In both cases, set a new period.
822 */
823 if (dl_se->dl_deadline == 0 ||
824 (dl_se->dl_defer_armed && dl_entity_overflow(dl_se, rq_clock(rq)))) {
825 dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
826 dl_se->runtime = pi_of(dl_se)->dl_runtime;
827 }
828
829 if (dl_se->dl_yielded && dl_se->runtime > 0)
830 dl_se->runtime = 0;
831
832 /*
833 * We keep moving the deadline away until we get some
834 * available runtime for the entity. This ensures correct
835 * handling of situations where the runtime overrun is
836 * arbitrary large.
837 */
838 while (dl_se->runtime <= 0) {
839 dl_se->deadline += pi_of(dl_se)->dl_period;
840 dl_se->runtime += pi_of(dl_se)->dl_runtime;
841 }
842
843 /*
844 * At this point, the deadline really should be "in
845 * the future" with respect to rq->clock. If it's
846 * not, we are, for some reason, lagging too much!
847 * Anyway, after having warn userspace abut that,
848 * we still try to keep the things running by
849 * resetting the deadline and the budget of the
850 * entity.
851 */
852 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
853 printk_deferred_once("sched: DL replenish lagged too much\n");
854 replenish_dl_new_period(dl_se, rq);
855 }
856
857 if (dl_se->dl_yielded)
858 dl_se->dl_yielded = 0;
859 if (dl_se->dl_throttled)
860 dl_se->dl_throttled = 0;
861
862 /*
863 * If this is the replenishment of a deferred reservation,
864 * clear the flag and return.
865 */
866 if (dl_se->dl_defer_armed) {
867 dl_se->dl_defer_armed = 0;
868 return;
869 }
870
871 /*
872 * A this point, if the deferred server is not armed, and the deadline
873 * is in the future, if it is not running already, throttle the server
874 * and arm the defer timer.
875 */
876 if (dl_se->dl_defer && !dl_se->dl_defer_running &&
877 dl_time_before(rq_clock(dl_se->rq), dl_se->deadline - dl_se->runtime)) {
878 if (!is_dl_boosted(dl_se)) {
879
880 /*
881 * Set dl_se->dl_defer_armed and dl_throttled variables to
882 * inform the start_dl_timer() that this is a deferred
883 * activation.
884 */
885 dl_se->dl_defer_armed = 1;
886 dl_se->dl_throttled = 1;
887 if (!start_dl_timer(dl_se)) {
888 /*
889 * If for whatever reason (delays), a previous timer was
890 * queued but not serviced, cancel it and clean the
891 * deferrable server variables intended for start_dl_timer().
892 */
893 hrtimer_try_to_cancel(&dl_se->dl_timer);
894 dl_se->dl_defer_armed = 0;
895 dl_se->dl_throttled = 0;
896 }
897 }
898 }
899 }
900
901 /*
902 * Here we check if --at time t-- an entity (which is probably being
903 * [re]activated or, in general, enqueued) can use its remaining runtime
904 * and its current deadline _without_ exceeding the bandwidth it is
905 * assigned (function returns true if it can't). We are in fact applying
906 * one of the CBS rules: when a task wakes up, if the residual runtime
907 * over residual deadline fits within the allocated bandwidth, then we
908 * can keep the current (absolute) deadline and residual budget without
909 * disrupting the schedulability of the system. Otherwise, we should
910 * refill the runtime and set the deadline a period in the future,
911 * because keeping the current (absolute) deadline of the task would
912 * result in breaking guarantees promised to other tasks (refer to
913 * Documentation/scheduler/sched-deadline.rst for more information).
914 *
915 * This function returns true if:
916 *
917 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
918 *
919 * IOW we can't recycle current parameters.
920 *
921 * Notice that the bandwidth check is done against the deadline. For
922 * task with deadline equal to period this is the same of using
923 * dl_period instead of dl_deadline in the equation above.
924 */
dl_entity_overflow(struct sched_dl_entity * dl_se,u64 t)925 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
926 {
927 u64 left, right;
928
929 /*
930 * left and right are the two sides of the equation above,
931 * after a bit of shuffling to use multiplications instead
932 * of divisions.
933 *
934 * Note that none of the time values involved in the two
935 * multiplications are absolute: dl_deadline and dl_runtime
936 * are the relative deadline and the maximum runtime of each
937 * instance, runtime is the runtime left for the last instance
938 * and (deadline - t), since t is rq->clock, is the time left
939 * to the (absolute) deadline. Even if overflowing the u64 type
940 * is very unlikely to occur in both cases, here we scale down
941 * as we want to avoid that risk at all. Scaling down by 10
942 * means that we reduce granularity to 1us. We are fine with it,
943 * since this is only a true/false check and, anyway, thinking
944 * of anything below microseconds resolution is actually fiction
945 * (but still we want to give the user that illusion >;).
946 */
947 left = (pi_of(dl_se)->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
948 right = ((dl_se->deadline - t) >> DL_SCALE) *
949 (pi_of(dl_se)->dl_runtime >> DL_SCALE);
950
951 return dl_time_before(right, left);
952 }
953
954 /*
955 * Revised wakeup rule [1]: For self-suspending tasks, rather then
956 * re-initializing task's runtime and deadline, the revised wakeup
957 * rule adjusts the task's runtime to avoid the task to overrun its
958 * density.
959 *
960 * Reasoning: a task may overrun the density if:
961 * runtime / (deadline - t) > dl_runtime / dl_deadline
962 *
963 * Therefore, runtime can be adjusted to:
964 * runtime = (dl_runtime / dl_deadline) * (deadline - t)
965 *
966 * In such way that runtime will be equal to the maximum density
967 * the task can use without breaking any rule.
968 *
969 * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
970 * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
971 */
972 static void
update_dl_revised_wakeup(struct sched_dl_entity * dl_se,struct rq * rq)973 update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
974 {
975 u64 laxity = dl_se->deadline - rq_clock(rq);
976
977 /*
978 * If the task has deadline < period, and the deadline is in the past,
979 * it should already be throttled before this check.
980 *
981 * See update_dl_entity() comments for further details.
982 */
983 WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
984
985 dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
986 }
987
988 /*
989 * Regarding the deadline, a task with implicit deadline has a relative
990 * deadline == relative period. A task with constrained deadline has a
991 * relative deadline <= relative period.
992 *
993 * We support constrained deadline tasks. However, there are some restrictions
994 * applied only for tasks which do not have an implicit deadline. See
995 * update_dl_entity() to know more about such restrictions.
996 *
997 * The dl_is_implicit() returns true if the task has an implicit deadline.
998 */
dl_is_implicit(struct sched_dl_entity * dl_se)999 static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
1000 {
1001 return dl_se->dl_deadline == dl_se->dl_period;
1002 }
1003
1004 /*
1005 * When a deadline entity is placed in the runqueue, its runtime and deadline
1006 * might need to be updated. This is done by a CBS wake up rule. There are two
1007 * different rules: 1) the original CBS; and 2) the Revisited CBS.
1008 *
1009 * When the task is starting a new period, the Original CBS is used. In this
1010 * case, the runtime is replenished and a new absolute deadline is set.
1011 *
1012 * When a task is queued before the begin of the next period, using the
1013 * remaining runtime and deadline could make the entity to overflow, see
1014 * dl_entity_overflow() to find more about runtime overflow. When such case
1015 * is detected, the runtime and deadline need to be updated.
1016 *
1017 * If the task has an implicit deadline, i.e., deadline == period, the Original
1018 * CBS is applied. The runtime is replenished and a new absolute deadline is
1019 * set, as in the previous cases.
1020 *
1021 * However, the Original CBS does not work properly for tasks with
1022 * deadline < period, which are said to have a constrained deadline. By
1023 * applying the Original CBS, a constrained deadline task would be able to run
1024 * runtime/deadline in a period. With deadline < period, the task would
1025 * overrun the runtime/period allowed bandwidth, breaking the admission test.
1026 *
1027 * In order to prevent this misbehave, the Revisited CBS is used for
1028 * constrained deadline tasks when a runtime overflow is detected. In the
1029 * Revisited CBS, rather than replenishing & setting a new absolute deadline,
1030 * the remaining runtime of the task is reduced to avoid runtime overflow.
1031 * Please refer to the comments update_dl_revised_wakeup() function to find
1032 * more about the Revised CBS rule.
1033 */
update_dl_entity(struct sched_dl_entity * dl_se)1034 static void update_dl_entity(struct sched_dl_entity *dl_se)
1035 {
1036 struct rq *rq = rq_of_dl_se(dl_se);
1037
1038 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
1039 dl_entity_overflow(dl_se, rq_clock(rq))) {
1040
1041 if (unlikely(!dl_is_implicit(dl_se) &&
1042 !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1043 !is_dl_boosted(dl_se))) {
1044 update_dl_revised_wakeup(dl_se, rq);
1045 return;
1046 }
1047
1048 replenish_dl_new_period(dl_se, rq);
1049 } else if (dl_server(dl_se) && dl_se->dl_defer) {
1050 /*
1051 * The server can still use its previous deadline, so check if
1052 * it left the dl_defer_running state.
1053 */
1054 if (!dl_se->dl_defer_running) {
1055 dl_se->dl_defer_armed = 1;
1056 dl_se->dl_throttled = 1;
1057 }
1058 }
1059 }
1060
dl_next_period(struct sched_dl_entity * dl_se)1061 static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
1062 {
1063 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
1064 }
1065
1066 /*
1067 * If the entity depleted all its runtime, and if we want it to sleep
1068 * while waiting for some new execution time to become available, we
1069 * set the bandwidth replenishment timer to the replenishment instant
1070 * and try to activate it.
1071 *
1072 * Notice that it is important for the caller to know if the timer
1073 * actually started or not (i.e., the replenishment instant is in
1074 * the future or in the past).
1075 */
start_dl_timer(struct sched_dl_entity * dl_se)1076 static int start_dl_timer(struct sched_dl_entity *dl_se)
1077 {
1078 struct hrtimer *timer = &dl_se->dl_timer;
1079 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1080 struct rq *rq = rq_of_dl_rq(dl_rq);
1081 ktime_t now, act;
1082 s64 delta;
1083
1084 lockdep_assert_rq_held(rq);
1085
1086 /*
1087 * We want the timer to fire at the deadline, but considering
1088 * that it is actually coming from rq->clock and not from
1089 * hrtimer's time base reading.
1090 *
1091 * The deferred reservation will have its timer set to
1092 * (deadline - runtime). At that point, the CBS rule will decide
1093 * if the current deadline can be used, or if a replenishment is
1094 * required to avoid add too much pressure on the system
1095 * (current u > U).
1096 */
1097 if (dl_se->dl_defer_armed) {
1098 WARN_ON_ONCE(!dl_se->dl_throttled);
1099 act = ns_to_ktime(dl_se->deadline - dl_se->runtime);
1100 } else {
1101 /* act = deadline - rel-deadline + period */
1102 act = ns_to_ktime(dl_next_period(dl_se));
1103 }
1104
1105 now = hrtimer_cb_get_time(timer);
1106 delta = ktime_to_ns(now) - rq_clock(rq);
1107 act = ktime_add_ns(act, delta);
1108
1109 /*
1110 * If the expiry time already passed, e.g., because the value
1111 * chosen as the deadline is too small, don't even try to
1112 * start the timer in the past!
1113 */
1114 if (ktime_us_delta(act, now) < 0)
1115 return 0;
1116
1117 /*
1118 * !enqueued will guarantee another callback; even if one is already in
1119 * progress. This ensures a balanced {get,put}_task_struct().
1120 *
1121 * The race against __run_timer() clearing the enqueued state is
1122 * harmless because we're holding task_rq()->lock, therefore the timer
1123 * expiring after we've done the check will wait on its task_rq_lock()
1124 * and observe our state.
1125 */
1126 if (!hrtimer_is_queued(timer)) {
1127 if (!dl_server(dl_se))
1128 get_task_struct(dl_task_of(dl_se));
1129 hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD);
1130 }
1131
1132 return 1;
1133 }
1134
__push_dl_task(struct rq * rq,struct rq_flags * rf)1135 static void __push_dl_task(struct rq *rq, struct rq_flags *rf)
1136 {
1137 /*
1138 * Queueing this task back might have overloaded rq, check if we need
1139 * to kick someone away.
1140 */
1141 if (has_pushable_dl_tasks(rq)) {
1142 /*
1143 * Nothing relies on rq->lock after this, so its safe to drop
1144 * rq->lock.
1145 */
1146 rq_unpin_lock(rq, rf);
1147 push_dl_task(rq);
1148 rq_repin_lock(rq, rf);
1149 }
1150 }
1151
1152 /* a defer timer will not be reset if the runtime consumed was < dl_server_min_res */
1153 static const u64 dl_server_min_res = 1 * NSEC_PER_MSEC;
1154
dl_server_timer(struct hrtimer * timer,struct sched_dl_entity * dl_se)1155 static enum hrtimer_restart dl_server_timer(struct hrtimer *timer, struct sched_dl_entity *dl_se)
1156 {
1157 struct rq *rq = rq_of_dl_se(dl_se);
1158 u64 fw;
1159
1160 scoped_guard (rq_lock, rq) {
1161 struct rq_flags *rf = &scope.rf;
1162
1163 if (!dl_se->dl_throttled || !dl_se->dl_runtime)
1164 return HRTIMER_NORESTART;
1165
1166 sched_clock_tick();
1167 update_rq_clock(rq);
1168
1169 if (!dl_se->dl_runtime)
1170 return HRTIMER_NORESTART;
1171
1172 if (dl_se->dl_defer_armed) {
1173 /*
1174 * First check if the server could consume runtime in background.
1175 * If so, it is possible to push the defer timer for this amount
1176 * of time. The dl_server_min_res serves as a limit to avoid
1177 * forwarding the timer for a too small amount of time.
1178 */
1179 if (dl_time_before(rq_clock(dl_se->rq),
1180 (dl_se->deadline - dl_se->runtime - dl_server_min_res))) {
1181
1182 /* reset the defer timer */
1183 fw = dl_se->deadline - rq_clock(dl_se->rq) - dl_se->runtime;
1184
1185 hrtimer_forward_now(timer, ns_to_ktime(fw));
1186 return HRTIMER_RESTART;
1187 }
1188
1189 dl_se->dl_defer_running = 1;
1190 }
1191
1192 enqueue_dl_entity(dl_se, ENQUEUE_REPLENISH);
1193
1194 if (!dl_task(dl_se->rq->curr) || dl_entity_preempt(dl_se, &dl_se->rq->curr->dl))
1195 resched_curr(rq);
1196
1197 __push_dl_task(rq, rf);
1198 }
1199
1200 return HRTIMER_NORESTART;
1201 }
1202
1203 /*
1204 * This is the bandwidth enforcement timer callback. If here, we know
1205 * a task is not on its dl_rq, since the fact that the timer was running
1206 * means the task is throttled and needs a runtime replenishment.
1207 *
1208 * However, what we actually do depends on the fact the task is active,
1209 * (it is on its rq) or has been removed from there by a call to
1210 * dequeue_task_dl(). In the former case we must issue the runtime
1211 * replenishment and add the task back to the dl_rq; in the latter, we just
1212 * do nothing but clearing dl_throttled, so that runtime and deadline
1213 * updating (and the queueing back to dl_rq) will be done by the
1214 * next call to enqueue_task_dl().
1215 */
dl_task_timer(struct hrtimer * timer)1216 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
1217 {
1218 struct sched_dl_entity *dl_se = container_of(timer,
1219 struct sched_dl_entity,
1220 dl_timer);
1221 struct task_struct *p;
1222 struct rq_flags rf;
1223 struct rq *rq;
1224
1225 if (dl_server(dl_se))
1226 return dl_server_timer(timer, dl_se);
1227
1228 p = dl_task_of(dl_se);
1229 rq = task_rq_lock(p, &rf);
1230
1231 /*
1232 * The task might have changed its scheduling policy to something
1233 * different than SCHED_DEADLINE (through switched_from_dl()).
1234 */
1235 if (!dl_task(p))
1236 goto unlock;
1237
1238 /*
1239 * The task might have been boosted by someone else and might be in the
1240 * boosting/deboosting path, its not throttled.
1241 */
1242 if (is_dl_boosted(dl_se))
1243 goto unlock;
1244
1245 /*
1246 * Spurious timer due to start_dl_timer() race; or we already received
1247 * a replenishment from rt_mutex_setprio().
1248 */
1249 if (!dl_se->dl_throttled)
1250 goto unlock;
1251
1252 sched_clock_tick();
1253 update_rq_clock(rq);
1254
1255 /*
1256 * If the throttle happened during sched-out; like:
1257 *
1258 * schedule()
1259 * deactivate_task()
1260 * dequeue_task_dl()
1261 * update_curr_dl()
1262 * start_dl_timer()
1263 * __dequeue_task_dl()
1264 * prev->on_rq = 0;
1265 *
1266 * We can be both throttled and !queued. Replenish the counter
1267 * but do not enqueue -- wait for our wakeup to do that.
1268 */
1269 if (!task_on_rq_queued(p)) {
1270 replenish_dl_entity(dl_se);
1271 goto unlock;
1272 }
1273
1274 if (unlikely(!rq->online)) {
1275 /*
1276 * If the runqueue is no longer available, migrate the
1277 * task elsewhere. This necessarily changes rq.
1278 */
1279 lockdep_unpin_lock(__rq_lockp(rq), rf.cookie);
1280 rq = dl_task_offline_migration(rq, p);
1281 rf.cookie = lockdep_pin_lock(__rq_lockp(rq));
1282 update_rq_clock(rq);
1283
1284 /*
1285 * Now that the task has been migrated to the new RQ and we
1286 * have that locked, proceed as normal and enqueue the task
1287 * there.
1288 */
1289 }
1290
1291 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
1292 if (dl_task(rq->donor))
1293 wakeup_preempt_dl(rq, p, 0);
1294 else
1295 resched_curr(rq);
1296
1297 __push_dl_task(rq, &rf);
1298
1299 unlock:
1300 task_rq_unlock(rq, p, &rf);
1301
1302 /*
1303 * This can free the task_struct, including this hrtimer, do not touch
1304 * anything related to that after this.
1305 */
1306 put_task_struct(p);
1307
1308 return HRTIMER_NORESTART;
1309 }
1310
init_dl_task_timer(struct sched_dl_entity * dl_se)1311 static void init_dl_task_timer(struct sched_dl_entity *dl_se)
1312 {
1313 struct hrtimer *timer = &dl_se->dl_timer;
1314
1315 hrtimer_setup(timer, dl_task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1316 }
1317
1318 /*
1319 * During the activation, CBS checks if it can reuse the current task's
1320 * runtime and period. If the deadline of the task is in the past, CBS
1321 * cannot use the runtime, and so it replenishes the task. This rule
1322 * works fine for implicit deadline tasks (deadline == period), and the
1323 * CBS was designed for implicit deadline tasks. However, a task with
1324 * constrained deadline (deadline < period) might be awakened after the
1325 * deadline, but before the next period. In this case, replenishing the
1326 * task would allow it to run for runtime / deadline. As in this case
1327 * deadline < period, CBS enables a task to run for more than the
1328 * runtime / period. In a very loaded system, this can cause a domino
1329 * effect, making other tasks miss their deadlines.
1330 *
1331 * To avoid this problem, in the activation of a constrained deadline
1332 * task after the deadline but before the next period, throttle the
1333 * task and set the replenishing timer to the begin of the next period,
1334 * unless it is boosted.
1335 */
dl_check_constrained_dl(struct sched_dl_entity * dl_se)1336 static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
1337 {
1338 struct rq *rq = rq_of_dl_se(dl_se);
1339
1340 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1341 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
1342 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se)))
1343 return;
1344 dl_se->dl_throttled = 1;
1345 if (dl_se->runtime > 0)
1346 dl_se->runtime = 0;
1347 }
1348 }
1349
1350 static
dl_runtime_exceeded(struct sched_dl_entity * dl_se)1351 int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
1352 {
1353 return (dl_se->runtime <= 0);
1354 }
1355
1356 /*
1357 * This function implements the GRUB accounting rule. According to the
1358 * GRUB reclaiming algorithm, the runtime is not decreased as "dq = -dt",
1359 * but as "dq = -(max{u, (Umax - Uinact - Uextra)} / Umax) dt",
1360 * where u is the utilization of the task, Umax is the maximum reclaimable
1361 * utilization, Uinact is the (per-runqueue) inactive utilization, computed
1362 * as the difference between the "total runqueue utilization" and the
1363 * "runqueue active utilization", and Uextra is the (per runqueue) extra
1364 * reclaimable utilization.
1365 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations multiplied
1366 * by 2^BW_SHIFT, the result has to be shifted right by BW_SHIFT.
1367 * Since rq->dl.bw_ratio contains 1 / Umax multiplied by 2^RATIO_SHIFT, dl_bw
1368 * is multiplied by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
1369 * Since delta is a 64 bit variable, to have an overflow its value should be
1370 * larger than 2^(64 - 20 - 8), which is more than 64 seconds. So, overflow is
1371 * not an issue here.
1372 */
grub_reclaim(u64 delta,struct rq * rq,struct sched_dl_entity * dl_se)1373 static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
1374 {
1375 u64 u_act;
1376 u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
1377
1378 /*
1379 * Instead of computing max{u, (u_max - u_inact - u_extra)}, we
1380 * compare u_inact + u_extra with u_max - u, because u_inact + u_extra
1381 * can be larger than u_max. So, u_max - u_inact - u_extra would be
1382 * negative leading to wrong results.
1383 */
1384 if (u_inact + rq->dl.extra_bw > rq->dl.max_bw - dl_se->dl_bw)
1385 u_act = dl_se->dl_bw;
1386 else
1387 u_act = rq->dl.max_bw - u_inact - rq->dl.extra_bw;
1388
1389 u_act = (u_act * rq->dl.bw_ratio) >> RATIO_SHIFT;
1390 return (delta * u_act) >> BW_SHIFT;
1391 }
1392
dl_scaled_delta_exec(struct rq * rq,struct sched_dl_entity * dl_se,s64 delta_exec)1393 s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec)
1394 {
1395 s64 scaled_delta_exec;
1396
1397 /*
1398 * For tasks that participate in GRUB, we implement GRUB-PA: the
1399 * spare reclaimed bandwidth is used to clock down frequency.
1400 *
1401 * For the others, we still need to scale reservation parameters
1402 * according to current frequency and CPU maximum capacity.
1403 */
1404 if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) {
1405 scaled_delta_exec = grub_reclaim(delta_exec, rq, dl_se);
1406 } else {
1407 int cpu = cpu_of(rq);
1408 unsigned long scale_freq = arch_scale_freq_capacity(cpu);
1409 unsigned long scale_cpu = arch_scale_cpu_capacity(cpu);
1410
1411 scaled_delta_exec = cap_scale(delta_exec, scale_freq);
1412 scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu);
1413 }
1414
1415 return scaled_delta_exec;
1416 }
1417
1418 static inline void
1419 update_stats_dequeue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
1420 int flags);
update_curr_dl_se(struct rq * rq,struct sched_dl_entity * dl_se,s64 delta_exec)1421 static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec)
1422 {
1423 s64 scaled_delta_exec;
1424
1425 if (unlikely(delta_exec <= 0)) {
1426 if (unlikely(dl_se->dl_yielded))
1427 goto throttle;
1428 return;
1429 }
1430
1431 if (dl_server(dl_se) && dl_se->dl_throttled && !dl_se->dl_defer)
1432 return;
1433
1434 if (dl_entity_is_special(dl_se))
1435 return;
1436
1437 scaled_delta_exec = delta_exec;
1438 if (!dl_server(dl_se))
1439 scaled_delta_exec = dl_scaled_delta_exec(rq, dl_se, delta_exec);
1440
1441 dl_se->runtime -= scaled_delta_exec;
1442
1443 /*
1444 * The fair server can consume its runtime while throttled (not queued/
1445 * running as regular CFS).
1446 *
1447 * If the server consumes its entire runtime in this state. The server
1448 * is not required for the current period. Thus, reset the server by
1449 * starting a new period, pushing the activation.
1450 */
1451 if (dl_se->dl_defer && dl_se->dl_throttled && dl_runtime_exceeded(dl_se)) {
1452 /*
1453 * If the server was previously activated - the starving condition
1454 * took place, it this point it went away because the fair scheduler
1455 * was able to get runtime in background. So return to the initial
1456 * state.
1457 */
1458 dl_se->dl_defer_running = 0;
1459
1460 hrtimer_try_to_cancel(&dl_se->dl_timer);
1461
1462 replenish_dl_new_period(dl_se, dl_se->rq);
1463
1464 /*
1465 * Not being able to start the timer seems problematic. If it could not
1466 * be started for whatever reason, we need to "unthrottle" the DL server
1467 * and queue right away. Otherwise nothing might queue it. That's similar
1468 * to what enqueue_dl_entity() does on start_dl_timer==0. For now, just warn.
1469 */
1470 WARN_ON_ONCE(!start_dl_timer(dl_se));
1471
1472 return;
1473 }
1474
1475 throttle:
1476 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
1477 dl_se->dl_throttled = 1;
1478
1479 /* If requested, inform the user about runtime overruns. */
1480 if (dl_runtime_exceeded(dl_se) &&
1481 (dl_se->flags & SCHED_FLAG_DL_OVERRUN))
1482 dl_se->dl_overrun = 1;
1483
1484 dequeue_dl_entity(dl_se, 0);
1485 if (!dl_server(dl_se)) {
1486 update_stats_dequeue_dl(&rq->dl, dl_se, 0);
1487 dequeue_pushable_dl_task(rq, dl_task_of(dl_se));
1488 }
1489
1490 if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se))) {
1491 if (dl_server(dl_se)) {
1492 replenish_dl_new_period(dl_se, rq);
1493 start_dl_timer(dl_se);
1494 } else {
1495 enqueue_task_dl(rq, dl_task_of(dl_se), ENQUEUE_REPLENISH);
1496 }
1497 }
1498
1499 if (!is_leftmost(dl_se, &rq->dl))
1500 resched_curr(rq);
1501 }
1502
1503 /*
1504 * The fair server (sole dl_server) does not account for real-time
1505 * workload because it is running fair work.
1506 */
1507 if (dl_se == &rq->fair_server)
1508 return;
1509
1510 #ifdef CONFIG_RT_GROUP_SCHED
1511 /*
1512 * Because -- for now -- we share the rt bandwidth, we need to
1513 * account our runtime there too, otherwise actual rt tasks
1514 * would be able to exceed the shared quota.
1515 *
1516 * Account to the root rt group for now.
1517 *
1518 * The solution we're working towards is having the RT groups scheduled
1519 * using deadline servers -- however there's a few nasties to figure
1520 * out before that can happen.
1521 */
1522 if (rt_bandwidth_enabled()) {
1523 struct rt_rq *rt_rq = &rq->rt;
1524
1525 raw_spin_lock(&rt_rq->rt_runtime_lock);
1526 /*
1527 * We'll let actual RT tasks worry about the overflow here, we
1528 * have our own CBS to keep us inline; only account when RT
1529 * bandwidth is relevant.
1530 */
1531 if (sched_rt_bandwidth_account(rt_rq))
1532 rt_rq->rt_time += delta_exec;
1533 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1534 }
1535 #endif /* CONFIG_RT_GROUP_SCHED */
1536 }
1537
1538 /*
1539 * In the non-defer mode, the idle time is not accounted, as the
1540 * server provides a guarantee.
1541 *
1542 * If the dl_server is in defer mode, the idle time is also considered
1543 * as time available for the fair server, avoiding a penalty for the
1544 * rt scheduler that did not consumed that time.
1545 */
dl_server_update_idle_time(struct rq * rq,struct task_struct * p)1546 void dl_server_update_idle_time(struct rq *rq, struct task_struct *p)
1547 {
1548 s64 delta_exec;
1549
1550 if (!rq->fair_server.dl_defer)
1551 return;
1552
1553 /* no need to discount more */
1554 if (rq->fair_server.runtime < 0)
1555 return;
1556
1557 delta_exec = rq_clock_task(rq) - p->se.exec_start;
1558 if (delta_exec < 0)
1559 return;
1560
1561 rq->fair_server.runtime -= delta_exec;
1562
1563 if (rq->fair_server.runtime < 0) {
1564 rq->fair_server.dl_defer_running = 0;
1565 rq->fair_server.runtime = 0;
1566 }
1567
1568 p->se.exec_start = rq_clock_task(rq);
1569 }
1570
dl_server_update(struct sched_dl_entity * dl_se,s64 delta_exec)1571 void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec)
1572 {
1573 /* 0 runtime = fair server disabled */
1574 if (dl_se->dl_runtime)
1575 update_curr_dl_se(dl_se->rq, dl_se, delta_exec);
1576 }
1577
dl_server_start(struct sched_dl_entity * dl_se)1578 void dl_server_start(struct sched_dl_entity *dl_se)
1579 {
1580 struct rq *rq = dl_se->rq;
1581
1582 if (!dl_server(dl_se) || dl_se->dl_server_active)
1583 return;
1584
1585 dl_se->dl_server_active = 1;
1586 enqueue_dl_entity(dl_se, ENQUEUE_WAKEUP);
1587 if (!dl_task(dl_se->rq->curr) || dl_entity_preempt(dl_se, &rq->curr->dl))
1588 resched_curr(dl_se->rq);
1589 }
1590
dl_server_stop(struct sched_dl_entity * dl_se)1591 void dl_server_stop(struct sched_dl_entity *dl_se)
1592 {
1593 if (!dl_server(dl_se) || !dl_server_active(dl_se))
1594 return;
1595
1596 dequeue_dl_entity(dl_se, DEQUEUE_SLEEP);
1597 hrtimer_try_to_cancel(&dl_se->dl_timer);
1598 dl_se->dl_defer_armed = 0;
1599 dl_se->dl_throttled = 0;
1600 dl_se->dl_server_active = 0;
1601 }
1602
dl_server_init(struct sched_dl_entity * dl_se,struct rq * rq,dl_server_pick_f pick_task)1603 void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
1604 dl_server_pick_f pick_task)
1605 {
1606 dl_se->rq = rq;
1607 dl_se->server_pick_task = pick_task;
1608 }
1609
sched_init_dl_servers(void)1610 void sched_init_dl_servers(void)
1611 {
1612 int cpu;
1613 struct rq *rq;
1614 struct sched_dl_entity *dl_se;
1615
1616 for_each_online_cpu(cpu) {
1617 u64 runtime = 50 * NSEC_PER_MSEC;
1618 u64 period = 1000 * NSEC_PER_MSEC;
1619
1620 rq = cpu_rq(cpu);
1621
1622 guard(rq_lock_irq)(rq);
1623
1624 dl_se = &rq->fair_server;
1625
1626 WARN_ON(dl_server(dl_se));
1627
1628 dl_server_apply_params(dl_se, runtime, period, 1);
1629
1630 dl_se->dl_server = 1;
1631 dl_se->dl_defer = 1;
1632 setup_new_dl_entity(dl_se);
1633 }
1634 }
1635
__dl_server_attach_root(struct sched_dl_entity * dl_se,struct rq * rq)1636 void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq)
1637 {
1638 u64 new_bw = dl_se->dl_bw;
1639 int cpu = cpu_of(rq);
1640 struct dl_bw *dl_b;
1641
1642 dl_b = dl_bw_of(cpu_of(rq));
1643 guard(raw_spinlock)(&dl_b->lock);
1644
1645 if (!dl_bw_cpus(cpu))
1646 return;
1647
1648 __dl_add(dl_b, new_bw, dl_bw_cpus(cpu));
1649 }
1650
dl_server_apply_params(struct sched_dl_entity * dl_se,u64 runtime,u64 period,bool init)1651 int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 period, bool init)
1652 {
1653 u64 old_bw = init ? 0 : to_ratio(dl_se->dl_period, dl_se->dl_runtime);
1654 u64 new_bw = to_ratio(period, runtime);
1655 struct rq *rq = dl_se->rq;
1656 int cpu = cpu_of(rq);
1657 struct dl_bw *dl_b;
1658 unsigned long cap;
1659 int retval = 0;
1660 int cpus;
1661
1662 dl_b = dl_bw_of(cpu);
1663 guard(raw_spinlock)(&dl_b->lock);
1664
1665 cpus = dl_bw_cpus(cpu);
1666 cap = dl_bw_capacity(cpu);
1667
1668 if (__dl_overflow(dl_b, cap, old_bw, new_bw))
1669 return -EBUSY;
1670
1671 if (init) {
1672 __add_rq_bw(new_bw, &rq->dl);
1673 __dl_add(dl_b, new_bw, cpus);
1674 } else {
1675 __dl_sub(dl_b, dl_se->dl_bw, cpus);
1676 __dl_add(dl_b, new_bw, cpus);
1677
1678 dl_rq_change_utilization(rq, dl_se, new_bw);
1679 }
1680
1681 dl_se->dl_runtime = runtime;
1682 dl_se->dl_deadline = period;
1683 dl_se->dl_period = period;
1684
1685 dl_se->runtime = 0;
1686 dl_se->deadline = 0;
1687
1688 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
1689 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
1690
1691 return retval;
1692 }
1693
1694 /*
1695 * Update the current task's runtime statistics (provided it is still
1696 * a -deadline task and has not been removed from the dl_rq).
1697 */
update_curr_dl(struct rq * rq)1698 static void update_curr_dl(struct rq *rq)
1699 {
1700 struct task_struct *donor = rq->donor;
1701 struct sched_dl_entity *dl_se = &donor->dl;
1702 s64 delta_exec;
1703
1704 if (!dl_task(donor) || !on_dl_rq(dl_se))
1705 return;
1706
1707 /*
1708 * Consumed budget is computed considering the time as
1709 * observed by schedulable tasks (excluding time spent
1710 * in hardirq context, etc.). Deadlines are instead
1711 * computed using hard walltime. This seems to be the more
1712 * natural solution, but the full ramifications of this
1713 * approach need further study.
1714 */
1715 delta_exec = update_curr_common(rq);
1716 update_curr_dl_se(rq, dl_se, delta_exec);
1717 }
1718
inactive_task_timer(struct hrtimer * timer)1719 static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
1720 {
1721 struct sched_dl_entity *dl_se = container_of(timer,
1722 struct sched_dl_entity,
1723 inactive_timer);
1724 struct task_struct *p = NULL;
1725 struct rq_flags rf;
1726 struct rq *rq;
1727
1728 if (!dl_server(dl_se)) {
1729 p = dl_task_of(dl_se);
1730 rq = task_rq_lock(p, &rf);
1731 } else {
1732 rq = dl_se->rq;
1733 rq_lock(rq, &rf);
1734 }
1735
1736 sched_clock_tick();
1737 update_rq_clock(rq);
1738
1739 if (dl_server(dl_se))
1740 goto no_task;
1741
1742 if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) {
1743 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1744
1745 if (READ_ONCE(p->__state) == TASK_DEAD && dl_se->dl_non_contending) {
1746 sub_running_bw(&p->dl, dl_rq_of_se(&p->dl));
1747 sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl));
1748 dl_se->dl_non_contending = 0;
1749 }
1750
1751 raw_spin_lock(&dl_b->lock);
1752 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
1753 raw_spin_unlock(&dl_b->lock);
1754 __dl_clear_params(dl_se);
1755
1756 goto unlock;
1757 }
1758
1759 no_task:
1760 if (dl_se->dl_non_contending == 0)
1761 goto unlock;
1762
1763 sub_running_bw(dl_se, &rq->dl);
1764 dl_se->dl_non_contending = 0;
1765 unlock:
1766
1767 if (!dl_server(dl_se)) {
1768 task_rq_unlock(rq, p, &rf);
1769 put_task_struct(p);
1770 } else {
1771 rq_unlock(rq, &rf);
1772 }
1773
1774 return HRTIMER_NORESTART;
1775 }
1776
init_dl_inactive_task_timer(struct sched_dl_entity * dl_se)1777 static void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1778 {
1779 struct hrtimer *timer = &dl_se->inactive_timer;
1780
1781 hrtimer_setup(timer, inactive_task_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1782 }
1783
1784 #define __node_2_dle(node) \
1785 rb_entry((node), struct sched_dl_entity, rb_node)
1786
inc_dl_deadline(struct dl_rq * dl_rq,u64 deadline)1787 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1788 {
1789 struct rq *rq = rq_of_dl_rq(dl_rq);
1790
1791 if (dl_rq->earliest_dl.curr == 0 ||
1792 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
1793 if (dl_rq->earliest_dl.curr == 0)
1794 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_HIGHER);
1795 dl_rq->earliest_dl.curr = deadline;
1796 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
1797 }
1798 }
1799
dec_dl_deadline(struct dl_rq * dl_rq,u64 deadline)1800 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1801 {
1802 struct rq *rq = rq_of_dl_rq(dl_rq);
1803
1804 /*
1805 * Since we may have removed our earliest (and/or next earliest)
1806 * task we must recompute them.
1807 */
1808 if (!dl_rq->dl_nr_running) {
1809 dl_rq->earliest_dl.curr = 0;
1810 dl_rq->earliest_dl.next = 0;
1811 cpudl_clear(&rq->rd->cpudl, rq->cpu);
1812 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
1813 } else {
1814 struct rb_node *leftmost = rb_first_cached(&dl_rq->root);
1815 struct sched_dl_entity *entry = __node_2_dle(leftmost);
1816
1817 dl_rq->earliest_dl.curr = entry->deadline;
1818 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
1819 }
1820 }
1821
1822 static inline
inc_dl_tasks(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)1823 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1824 {
1825 u64 deadline = dl_se->deadline;
1826
1827 dl_rq->dl_nr_running++;
1828
1829 if (!dl_server(dl_se))
1830 add_nr_running(rq_of_dl_rq(dl_rq), 1);
1831
1832 inc_dl_deadline(dl_rq, deadline);
1833 }
1834
1835 static inline
dec_dl_tasks(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)1836 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1837 {
1838 WARN_ON(!dl_rq->dl_nr_running);
1839 dl_rq->dl_nr_running--;
1840
1841 if (!dl_server(dl_se))
1842 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
1843
1844 dec_dl_deadline(dl_rq, dl_se->deadline);
1845 }
1846
__dl_less(struct rb_node * a,const struct rb_node * b)1847 static inline bool __dl_less(struct rb_node *a, const struct rb_node *b)
1848 {
1849 return dl_time_before(__node_2_dle(a)->deadline, __node_2_dle(b)->deadline);
1850 }
1851
1852 static __always_inline struct sched_statistics *
__schedstats_from_dl_se(struct sched_dl_entity * dl_se)1853 __schedstats_from_dl_se(struct sched_dl_entity *dl_se)
1854 {
1855 if (!schedstat_enabled())
1856 return NULL;
1857
1858 if (dl_server(dl_se))
1859 return NULL;
1860
1861 return &dl_task_of(dl_se)->stats;
1862 }
1863
1864 static inline void
update_stats_wait_start_dl(struct dl_rq * dl_rq,struct sched_dl_entity * dl_se)1865 update_stats_wait_start_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
1866 {
1867 struct sched_statistics *stats = __schedstats_from_dl_se(dl_se);
1868 if (stats)
1869 __update_stats_wait_start(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
1870 }
1871
1872 static inline void
update_stats_wait_end_dl(struct dl_rq * dl_rq,struct sched_dl_entity * dl_se)1873 update_stats_wait_end_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
1874 {
1875 struct sched_statistics *stats = __schedstats_from_dl_se(dl_se);
1876 if (stats)
1877 __update_stats_wait_end(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
1878 }
1879
1880 static inline void
update_stats_enqueue_sleeper_dl(struct dl_rq * dl_rq,struct sched_dl_entity * dl_se)1881 update_stats_enqueue_sleeper_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
1882 {
1883 struct sched_statistics *stats = __schedstats_from_dl_se(dl_se);
1884 if (stats)
1885 __update_stats_enqueue_sleeper(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
1886 }
1887
1888 static inline void
update_stats_enqueue_dl(struct dl_rq * dl_rq,struct sched_dl_entity * dl_se,int flags)1889 update_stats_enqueue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
1890 int flags)
1891 {
1892 if (!schedstat_enabled())
1893 return;
1894
1895 if (flags & ENQUEUE_WAKEUP)
1896 update_stats_enqueue_sleeper_dl(dl_rq, dl_se);
1897 }
1898
1899 static inline void
update_stats_dequeue_dl(struct dl_rq * dl_rq,struct sched_dl_entity * dl_se,int flags)1900 update_stats_dequeue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
1901 int flags)
1902 {
1903 struct task_struct *p = dl_task_of(dl_se);
1904
1905 if (!schedstat_enabled())
1906 return;
1907
1908 if ((flags & DEQUEUE_SLEEP)) {
1909 unsigned int state;
1910
1911 state = READ_ONCE(p->__state);
1912 if (state & TASK_INTERRUPTIBLE)
1913 __schedstat_set(p->stats.sleep_start,
1914 rq_clock(rq_of_dl_rq(dl_rq)));
1915
1916 if (state & TASK_UNINTERRUPTIBLE)
1917 __schedstat_set(p->stats.block_start,
1918 rq_clock(rq_of_dl_rq(dl_rq)));
1919 }
1920 }
1921
__enqueue_dl_entity(struct sched_dl_entity * dl_se)1922 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1923 {
1924 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1925
1926 WARN_ON_ONCE(!RB_EMPTY_NODE(&dl_se->rb_node));
1927
1928 rb_add_cached(&dl_se->rb_node, &dl_rq->root, __dl_less);
1929
1930 inc_dl_tasks(dl_se, dl_rq);
1931 }
1932
__dequeue_dl_entity(struct sched_dl_entity * dl_se)1933 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1934 {
1935 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1936
1937 if (RB_EMPTY_NODE(&dl_se->rb_node))
1938 return;
1939
1940 rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
1941
1942 RB_CLEAR_NODE(&dl_se->rb_node);
1943
1944 dec_dl_tasks(dl_se, dl_rq);
1945 }
1946
1947 static void
enqueue_dl_entity(struct sched_dl_entity * dl_se,int flags)1948 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
1949 {
1950 WARN_ON_ONCE(on_dl_rq(dl_se));
1951
1952 update_stats_enqueue_dl(dl_rq_of_se(dl_se), dl_se, flags);
1953
1954 /*
1955 * Check if a constrained deadline task was activated
1956 * after the deadline but before the next period.
1957 * If that is the case, the task will be throttled and
1958 * the replenishment timer will be set to the next period.
1959 */
1960 if (!dl_se->dl_throttled && !dl_is_implicit(dl_se))
1961 dl_check_constrained_dl(dl_se);
1962
1963 if (flags & (ENQUEUE_RESTORE|ENQUEUE_MIGRATING)) {
1964 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1965
1966 add_rq_bw(dl_se, dl_rq);
1967 add_running_bw(dl_se, dl_rq);
1968 }
1969
1970 /*
1971 * If p is throttled, we do not enqueue it. In fact, if it exhausted
1972 * its budget it needs a replenishment and, since it now is on
1973 * its rq, the bandwidth timer callback (which clearly has not
1974 * run yet) will take care of this.
1975 * However, the active utilization does not depend on the fact
1976 * that the task is on the runqueue or not (but depends on the
1977 * task's state - in GRUB parlance, "inactive" vs "active contending").
1978 * In other words, even if a task is throttled its utilization must
1979 * be counted in the active utilization; hence, we need to call
1980 * add_running_bw().
1981 */
1982 if (!dl_se->dl_defer && dl_se->dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
1983 if (flags & ENQUEUE_WAKEUP)
1984 task_contending(dl_se, flags);
1985
1986 return;
1987 }
1988
1989 /*
1990 * If this is a wakeup or a new instance, the scheduling
1991 * parameters of the task might need updating. Otherwise,
1992 * we want a replenishment of its runtime.
1993 */
1994 if (flags & ENQUEUE_WAKEUP) {
1995 task_contending(dl_se, flags);
1996 update_dl_entity(dl_se);
1997 } else if (flags & ENQUEUE_REPLENISH) {
1998 replenish_dl_entity(dl_se);
1999 } else if ((flags & ENQUEUE_RESTORE) &&
2000 !is_dl_boosted(dl_se) &&
2001 dl_time_before(dl_se->deadline, rq_clock(rq_of_dl_se(dl_se)))) {
2002 setup_new_dl_entity(dl_se);
2003 }
2004
2005 /*
2006 * If the reservation is still throttled, e.g., it got replenished but is a
2007 * deferred task and still got to wait, don't enqueue.
2008 */
2009 if (dl_se->dl_throttled && start_dl_timer(dl_se))
2010 return;
2011
2012 /*
2013 * We're about to enqueue, make sure we're not ->dl_throttled!
2014 * In case the timer was not started, say because the defer time
2015 * has passed, mark as not throttled and mark unarmed.
2016 * Also cancel earlier timers, since letting those run is pointless.
2017 */
2018 if (dl_se->dl_throttled) {
2019 hrtimer_try_to_cancel(&dl_se->dl_timer);
2020 dl_se->dl_defer_armed = 0;
2021 dl_se->dl_throttled = 0;
2022 }
2023
2024 __enqueue_dl_entity(dl_se);
2025 }
2026
dequeue_dl_entity(struct sched_dl_entity * dl_se,int flags)2027 static void dequeue_dl_entity(struct sched_dl_entity *dl_se, int flags)
2028 {
2029 __dequeue_dl_entity(dl_se);
2030
2031 if (flags & (DEQUEUE_SAVE|DEQUEUE_MIGRATING)) {
2032 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
2033
2034 sub_running_bw(dl_se, dl_rq);
2035 sub_rq_bw(dl_se, dl_rq);
2036 }
2037
2038 /*
2039 * This check allows to start the inactive timer (or to immediately
2040 * decrease the active utilization, if needed) in two cases:
2041 * when the task blocks and when it is terminating
2042 * (p->state == TASK_DEAD). We can handle the two cases in the same
2043 * way, because from GRUB's point of view the same thing is happening
2044 * (the task moves from "active contending" to "active non contending"
2045 * or "inactive")
2046 */
2047 if (flags & DEQUEUE_SLEEP)
2048 task_non_contending(dl_se);
2049 }
2050
enqueue_task_dl(struct rq * rq,struct task_struct * p,int flags)2051 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
2052 {
2053 if (is_dl_boosted(&p->dl)) {
2054 /*
2055 * Because of delays in the detection of the overrun of a
2056 * thread's runtime, it might be the case that a thread
2057 * goes to sleep in a rt mutex with negative runtime. As
2058 * a consequence, the thread will be throttled.
2059 *
2060 * While waiting for the mutex, this thread can also be
2061 * boosted via PI, resulting in a thread that is throttled
2062 * and boosted at the same time.
2063 *
2064 * In this case, the boost overrides the throttle.
2065 */
2066 if (p->dl.dl_throttled) {
2067 /*
2068 * The replenish timer needs to be canceled. No
2069 * problem if it fires concurrently: boosted threads
2070 * are ignored in dl_task_timer().
2071 */
2072 cancel_replenish_timer(&p->dl);
2073 p->dl.dl_throttled = 0;
2074 }
2075 } else if (!dl_prio(p->normal_prio)) {
2076 /*
2077 * Special case in which we have a !SCHED_DEADLINE task that is going
2078 * to be deboosted, but exceeds its runtime while doing so. No point in
2079 * replenishing it, as it's going to return back to its original
2080 * scheduling class after this. If it has been throttled, we need to
2081 * clear the flag, otherwise the task may wake up as throttled after
2082 * being boosted again with no means to replenish the runtime and clear
2083 * the throttle.
2084 */
2085 p->dl.dl_throttled = 0;
2086 if (!(flags & ENQUEUE_REPLENISH))
2087 printk_deferred_once("sched: DL de-boosted task PID %d: REPLENISH flag missing\n",
2088 task_pid_nr(p));
2089
2090 return;
2091 }
2092
2093 check_schedstat_required();
2094 update_stats_wait_start_dl(dl_rq_of_se(&p->dl), &p->dl);
2095
2096 if (p->on_rq == TASK_ON_RQ_MIGRATING)
2097 flags |= ENQUEUE_MIGRATING;
2098
2099 enqueue_dl_entity(&p->dl, flags);
2100
2101 if (dl_server(&p->dl))
2102 return;
2103
2104 if (task_is_blocked(p))
2105 return;
2106
2107 if (!task_current(rq, p) && !p->dl.dl_throttled && p->nr_cpus_allowed > 1)
2108 enqueue_pushable_dl_task(rq, p);
2109 }
2110
dequeue_task_dl(struct rq * rq,struct task_struct * p,int flags)2111 static bool dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
2112 {
2113 update_curr_dl(rq);
2114
2115 if (p->on_rq == TASK_ON_RQ_MIGRATING)
2116 flags |= DEQUEUE_MIGRATING;
2117
2118 dequeue_dl_entity(&p->dl, flags);
2119 if (!p->dl.dl_throttled && !dl_server(&p->dl))
2120 dequeue_pushable_dl_task(rq, p);
2121
2122 return true;
2123 }
2124
2125 /*
2126 * Yield task semantic for -deadline tasks is:
2127 *
2128 * get off from the CPU until our next instance, with
2129 * a new runtime. This is of little use now, since we
2130 * don't have a bandwidth reclaiming mechanism. Anyway,
2131 * bandwidth reclaiming is planned for the future, and
2132 * yield_task_dl will indicate that some spare budget
2133 * is available for other task instances to use it.
2134 */
yield_task_dl(struct rq * rq)2135 static void yield_task_dl(struct rq *rq)
2136 {
2137 /*
2138 * We make the task go to sleep until its current deadline by
2139 * forcing its runtime to zero. This way, update_curr_dl() stops
2140 * it and the bandwidth timer will wake it up and will give it
2141 * new scheduling parameters (thanks to dl_yielded=1).
2142 */
2143 rq->curr->dl.dl_yielded = 1;
2144
2145 update_rq_clock(rq);
2146 update_curr_dl(rq);
2147 /*
2148 * Tell update_rq_clock() that we've just updated,
2149 * so we don't do microscopic update in schedule()
2150 * and double the fastpath cost.
2151 */
2152 rq_clock_skip_update(rq);
2153 }
2154
dl_task_is_earliest_deadline(struct task_struct * p,struct rq * rq)2155 static inline bool dl_task_is_earliest_deadline(struct task_struct *p,
2156 struct rq *rq)
2157 {
2158 return (!rq->dl.dl_nr_running ||
2159 dl_time_before(p->dl.deadline,
2160 rq->dl.earliest_dl.curr));
2161 }
2162
2163 static int find_later_rq(struct task_struct *task);
2164
2165 static int
select_task_rq_dl(struct task_struct * p,int cpu,int flags)2166 select_task_rq_dl(struct task_struct *p, int cpu, int flags)
2167 {
2168 struct task_struct *curr, *donor;
2169 bool select_rq;
2170 struct rq *rq;
2171
2172 if (!(flags & WF_TTWU))
2173 goto out;
2174
2175 rq = cpu_rq(cpu);
2176
2177 rcu_read_lock();
2178 curr = READ_ONCE(rq->curr); /* unlocked access */
2179 donor = READ_ONCE(rq->donor);
2180
2181 /*
2182 * If we are dealing with a -deadline task, we must
2183 * decide where to wake it up.
2184 * If it has a later deadline and the current task
2185 * on this rq can't move (provided the waking task
2186 * can!) we prefer to send it somewhere else. On the
2187 * other hand, if it has a shorter deadline, we
2188 * try to make it stay here, it might be important.
2189 */
2190 select_rq = unlikely(dl_task(donor)) &&
2191 (curr->nr_cpus_allowed < 2 ||
2192 !dl_entity_preempt(&p->dl, &donor->dl)) &&
2193 p->nr_cpus_allowed > 1;
2194
2195 /*
2196 * Take the capacity of the CPU into account to
2197 * ensure it fits the requirement of the task.
2198 */
2199 if (sched_asym_cpucap_active())
2200 select_rq |= !dl_task_fits_capacity(p, cpu);
2201
2202 if (select_rq) {
2203 int target = find_later_rq(p);
2204
2205 if (target != -1 &&
2206 dl_task_is_earliest_deadline(p, cpu_rq(target)))
2207 cpu = target;
2208 }
2209 rcu_read_unlock();
2210
2211 out:
2212 return cpu;
2213 }
2214
migrate_task_rq_dl(struct task_struct * p,int new_cpu __maybe_unused)2215 static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
2216 {
2217 struct rq_flags rf;
2218 struct rq *rq;
2219
2220 if (READ_ONCE(p->__state) != TASK_WAKING)
2221 return;
2222
2223 rq = task_rq(p);
2224 /*
2225 * Since p->state == TASK_WAKING, set_task_cpu() has been called
2226 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
2227 * rq->lock is not... So, lock it
2228 */
2229 rq_lock(rq, &rf);
2230 if (p->dl.dl_non_contending) {
2231 update_rq_clock(rq);
2232 sub_running_bw(&p->dl, &rq->dl);
2233 p->dl.dl_non_contending = 0;
2234 /*
2235 * If the timer handler is currently running and the
2236 * timer cannot be canceled, inactive_task_timer()
2237 * will see that dl_not_contending is not set, and
2238 * will not touch the rq's active utilization,
2239 * so we are still safe.
2240 */
2241 cancel_inactive_timer(&p->dl);
2242 }
2243 sub_rq_bw(&p->dl, &rq->dl);
2244 rq_unlock(rq, &rf);
2245 }
2246
check_preempt_equal_dl(struct rq * rq,struct task_struct * p)2247 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
2248 {
2249 /*
2250 * Current can't be migrated, useless to reschedule,
2251 * let's hope p can move out.
2252 */
2253 if (rq->curr->nr_cpus_allowed == 1 ||
2254 !cpudl_find(&rq->rd->cpudl, rq->donor, NULL))
2255 return;
2256
2257 /*
2258 * p is migratable, so let's not schedule it and
2259 * see if it is pushed or pulled somewhere else.
2260 */
2261 if (p->nr_cpus_allowed != 1 &&
2262 cpudl_find(&rq->rd->cpudl, p, NULL))
2263 return;
2264
2265 resched_curr(rq);
2266 }
2267
balance_dl(struct rq * rq,struct task_struct * p,struct rq_flags * rf)2268 static int balance_dl(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
2269 {
2270 if (!on_dl_rq(&p->dl) && need_pull_dl_task(rq, p)) {
2271 /*
2272 * This is OK, because current is on_cpu, which avoids it being
2273 * picked for load-balance and preemption/IRQs are still
2274 * disabled avoiding further scheduler activity on it and we've
2275 * not yet started the picking loop.
2276 */
2277 rq_unpin_lock(rq, rf);
2278 pull_dl_task(rq);
2279 rq_repin_lock(rq, rf);
2280 }
2281
2282 return sched_stop_runnable(rq) || sched_dl_runnable(rq);
2283 }
2284
2285 /*
2286 * Only called when both the current and waking task are -deadline
2287 * tasks.
2288 */
wakeup_preempt_dl(struct rq * rq,struct task_struct * p,int flags)2289 static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p,
2290 int flags)
2291 {
2292 if (dl_entity_preempt(&p->dl, &rq->donor->dl)) {
2293 resched_curr(rq);
2294 return;
2295 }
2296
2297 /*
2298 * In the unlikely case current and p have the same deadline
2299 * let us try to decide what's the best thing to do...
2300 */
2301 if ((p->dl.deadline == rq->donor->dl.deadline) &&
2302 !test_tsk_need_resched(rq->curr))
2303 check_preempt_equal_dl(rq, p);
2304 }
2305
2306 #ifdef CONFIG_SCHED_HRTICK
start_hrtick_dl(struct rq * rq,struct sched_dl_entity * dl_se)2307 static void start_hrtick_dl(struct rq *rq, struct sched_dl_entity *dl_se)
2308 {
2309 hrtick_start(rq, dl_se->runtime);
2310 }
2311 #else /* !CONFIG_SCHED_HRTICK: */
start_hrtick_dl(struct rq * rq,struct sched_dl_entity * dl_se)2312 static void start_hrtick_dl(struct rq *rq, struct sched_dl_entity *dl_se)
2313 {
2314 }
2315 #endif /* !CONFIG_SCHED_HRTICK */
2316
set_next_task_dl(struct rq * rq,struct task_struct * p,bool first)2317 static void set_next_task_dl(struct rq *rq, struct task_struct *p, bool first)
2318 {
2319 struct sched_dl_entity *dl_se = &p->dl;
2320 struct dl_rq *dl_rq = &rq->dl;
2321
2322 p->se.exec_start = rq_clock_task(rq);
2323 if (on_dl_rq(&p->dl))
2324 update_stats_wait_end_dl(dl_rq, dl_se);
2325
2326 /* You can't push away the running task */
2327 dequeue_pushable_dl_task(rq, p);
2328
2329 if (!first)
2330 return;
2331
2332 if (rq->donor->sched_class != &dl_sched_class)
2333 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2334
2335 deadline_queue_push_tasks(rq);
2336
2337 if (hrtick_enabled_dl(rq))
2338 start_hrtick_dl(rq, &p->dl);
2339 }
2340
pick_next_dl_entity(struct dl_rq * dl_rq)2341 static struct sched_dl_entity *pick_next_dl_entity(struct dl_rq *dl_rq)
2342 {
2343 struct rb_node *left = rb_first_cached(&dl_rq->root);
2344
2345 if (!left)
2346 return NULL;
2347
2348 return __node_2_dle(left);
2349 }
2350
2351 /*
2352 * __pick_next_task_dl - Helper to pick the next -deadline task to run.
2353 * @rq: The runqueue to pick the next task from.
2354 */
__pick_task_dl(struct rq * rq)2355 static struct task_struct *__pick_task_dl(struct rq *rq)
2356 {
2357 struct sched_dl_entity *dl_se;
2358 struct dl_rq *dl_rq = &rq->dl;
2359 struct task_struct *p;
2360
2361 again:
2362 if (!sched_dl_runnable(rq))
2363 return NULL;
2364
2365 dl_se = pick_next_dl_entity(dl_rq);
2366 WARN_ON_ONCE(!dl_se);
2367
2368 if (dl_server(dl_se)) {
2369 p = dl_se->server_pick_task(dl_se);
2370 if (!p) {
2371 dl_server_stop(dl_se);
2372 goto again;
2373 }
2374 rq->dl_server = dl_se;
2375 } else {
2376 p = dl_task_of(dl_se);
2377 }
2378
2379 return p;
2380 }
2381
pick_task_dl(struct rq * rq)2382 static struct task_struct *pick_task_dl(struct rq *rq)
2383 {
2384 return __pick_task_dl(rq);
2385 }
2386
put_prev_task_dl(struct rq * rq,struct task_struct * p,struct task_struct * next)2387 static void put_prev_task_dl(struct rq *rq, struct task_struct *p, struct task_struct *next)
2388 {
2389 struct sched_dl_entity *dl_se = &p->dl;
2390 struct dl_rq *dl_rq = &rq->dl;
2391
2392 if (on_dl_rq(&p->dl))
2393 update_stats_wait_start_dl(dl_rq, dl_se);
2394
2395 update_curr_dl(rq);
2396
2397 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2398
2399 if (task_is_blocked(p))
2400 return;
2401
2402 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
2403 enqueue_pushable_dl_task(rq, p);
2404 }
2405
2406 /*
2407 * scheduler tick hitting a task of our scheduling class.
2408 *
2409 * NOTE: This function can be called remotely by the tick offload that
2410 * goes along full dynticks. Therefore no local assumption can be made
2411 * and everything must be accessed through the @rq and @curr passed in
2412 * parameters.
2413 */
task_tick_dl(struct rq * rq,struct task_struct * p,int queued)2414 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
2415 {
2416 update_curr_dl(rq);
2417
2418 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2419 /*
2420 * Even when we have runtime, update_curr_dl() might have resulted in us
2421 * not being the leftmost task anymore. In that case NEED_RESCHED will
2422 * be set and schedule() will start a new hrtick for the next task.
2423 */
2424 if (hrtick_enabled_dl(rq) && queued && p->dl.runtime > 0 &&
2425 is_leftmost(&p->dl, &rq->dl))
2426 start_hrtick_dl(rq, &p->dl);
2427 }
2428
task_fork_dl(struct task_struct * p)2429 static void task_fork_dl(struct task_struct *p)
2430 {
2431 /*
2432 * SCHED_DEADLINE tasks cannot fork and this is achieved through
2433 * sched_fork()
2434 */
2435 }
2436
2437 /* Only try algorithms three times */
2438 #define DL_MAX_TRIES 3
2439
2440 /*
2441 * Return the earliest pushable rq's task, which is suitable to be executed
2442 * on the CPU, NULL otherwise:
2443 */
pick_earliest_pushable_dl_task(struct rq * rq,int cpu)2444 static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
2445 {
2446 struct task_struct *p = NULL;
2447 struct rb_node *next_node;
2448
2449 if (!has_pushable_dl_tasks(rq))
2450 return NULL;
2451
2452 next_node = rb_first_cached(&rq->dl.pushable_dl_tasks_root);
2453 while (next_node) {
2454 p = __node_2_pdl(next_node);
2455
2456 if (task_is_pushable(rq, p, cpu))
2457 return p;
2458
2459 next_node = rb_next(next_node);
2460 }
2461
2462 return NULL;
2463 }
2464
2465 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
2466
find_later_rq(struct task_struct * task)2467 static int find_later_rq(struct task_struct *task)
2468 {
2469 struct sched_domain *sd;
2470 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
2471 int this_cpu = smp_processor_id();
2472 int cpu = task_cpu(task);
2473
2474 /* Make sure the mask is initialized first */
2475 if (unlikely(!later_mask))
2476 return -1;
2477
2478 if (task->nr_cpus_allowed == 1)
2479 return -1;
2480
2481 /*
2482 * We have to consider system topology and task affinity
2483 * first, then we can look for a suitable CPU.
2484 */
2485 if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
2486 return -1;
2487
2488 /*
2489 * If we are here, some targets have been found, including
2490 * the most suitable which is, among the runqueues where the
2491 * current tasks have later deadlines than the task's one, the
2492 * rq with the latest possible one.
2493 *
2494 * Now we check how well this matches with task's
2495 * affinity and system topology.
2496 *
2497 * The last CPU where the task run is our first
2498 * guess, since it is most likely cache-hot there.
2499 */
2500 if (cpumask_test_cpu(cpu, later_mask))
2501 return cpu;
2502 /*
2503 * Check if this_cpu is to be skipped (i.e., it is
2504 * not in the mask) or not.
2505 */
2506 if (!cpumask_test_cpu(this_cpu, later_mask))
2507 this_cpu = -1;
2508
2509 rcu_read_lock();
2510 for_each_domain(cpu, sd) {
2511 if (sd->flags & SD_WAKE_AFFINE) {
2512 int best_cpu;
2513
2514 /*
2515 * If possible, preempting this_cpu is
2516 * cheaper than migrating.
2517 */
2518 if (this_cpu != -1 &&
2519 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
2520 rcu_read_unlock();
2521 return this_cpu;
2522 }
2523
2524 best_cpu = cpumask_any_and_distribute(later_mask,
2525 sched_domain_span(sd));
2526 /*
2527 * Last chance: if a CPU being in both later_mask
2528 * and current sd span is valid, that becomes our
2529 * choice. Of course, the latest possible CPU is
2530 * already under consideration through later_mask.
2531 */
2532 if (best_cpu < nr_cpu_ids) {
2533 rcu_read_unlock();
2534 return best_cpu;
2535 }
2536 }
2537 }
2538 rcu_read_unlock();
2539
2540 /*
2541 * At this point, all our guesses failed, we just return
2542 * 'something', and let the caller sort the things out.
2543 */
2544 if (this_cpu != -1)
2545 return this_cpu;
2546
2547 cpu = cpumask_any_distribute(later_mask);
2548 if (cpu < nr_cpu_ids)
2549 return cpu;
2550
2551 return -1;
2552 }
2553
pick_next_pushable_dl_task(struct rq * rq)2554 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
2555 {
2556 struct task_struct *p;
2557
2558 if (!has_pushable_dl_tasks(rq))
2559 return NULL;
2560
2561 p = __node_2_pdl(rb_first_cached(&rq->dl.pushable_dl_tasks_root));
2562
2563 WARN_ON_ONCE(rq->cpu != task_cpu(p));
2564 WARN_ON_ONCE(task_current(rq, p));
2565 WARN_ON_ONCE(p->nr_cpus_allowed <= 1);
2566
2567 WARN_ON_ONCE(!task_on_rq_queued(p));
2568 WARN_ON_ONCE(!dl_task(p));
2569
2570 return p;
2571 }
2572
2573 /* Locks the rq it finds */
find_lock_later_rq(struct task_struct * task,struct rq * rq)2574 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
2575 {
2576 struct rq *later_rq = NULL;
2577 int tries;
2578 int cpu;
2579
2580 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
2581 cpu = find_later_rq(task);
2582
2583 if ((cpu == -1) || (cpu == rq->cpu))
2584 break;
2585
2586 later_rq = cpu_rq(cpu);
2587
2588 if (!dl_task_is_earliest_deadline(task, later_rq)) {
2589 /*
2590 * Target rq has tasks of equal or earlier deadline,
2591 * retrying does not release any lock and is unlikely
2592 * to yield a different result.
2593 */
2594 later_rq = NULL;
2595 break;
2596 }
2597
2598 /* Retry if something changed. */
2599 if (double_lock_balance(rq, later_rq)) {
2600 /*
2601 * double_lock_balance had to release rq->lock, in the
2602 * meantime, task may no longer be fit to be migrated.
2603 * Check the following to ensure that the task is
2604 * still suitable for migration:
2605 * 1. It is possible the task was scheduled,
2606 * migrate_disabled was set and then got preempted,
2607 * so we must check the task migration disable
2608 * flag.
2609 * 2. The CPU picked is in the task's affinity.
2610 * 3. For throttled task (dl_task_offline_migration),
2611 * check the following:
2612 * - the task is not on the rq anymore (it was
2613 * migrated)
2614 * - the task is not on CPU anymore
2615 * - the task is still a dl task
2616 * - the task is not queued on the rq anymore
2617 * 4. For the non-throttled task (push_dl_task), the
2618 * check to ensure that this task is still at the
2619 * head of the pushable tasks list is enough.
2620 */
2621 if (unlikely(is_migration_disabled(task) ||
2622 !cpumask_test_cpu(later_rq->cpu, &task->cpus_mask) ||
2623 (task->dl.dl_throttled &&
2624 (task_rq(task) != rq ||
2625 task_on_cpu(rq, task) ||
2626 !dl_task(task) ||
2627 !task_on_rq_queued(task))) ||
2628 (!task->dl.dl_throttled &&
2629 task != pick_next_pushable_dl_task(rq)))) {
2630
2631 double_unlock_balance(rq, later_rq);
2632 later_rq = NULL;
2633 break;
2634 }
2635 }
2636
2637 /*
2638 * If the rq we found has no -deadline task, or
2639 * its earliest one has a later deadline than our
2640 * task, the rq is a good one.
2641 */
2642 if (dl_task_is_earliest_deadline(task, later_rq))
2643 break;
2644
2645 /* Otherwise we try again. */
2646 double_unlock_balance(rq, later_rq);
2647 later_rq = NULL;
2648 }
2649
2650 return later_rq;
2651 }
2652
2653 /*
2654 * See if the non running -deadline tasks on this rq
2655 * can be sent to some other CPU where they can preempt
2656 * and start executing.
2657 */
push_dl_task(struct rq * rq)2658 static int push_dl_task(struct rq *rq)
2659 {
2660 struct task_struct *next_task;
2661 struct rq *later_rq;
2662 int ret = 0;
2663
2664 next_task = pick_next_pushable_dl_task(rq);
2665 if (!next_task)
2666 return 0;
2667
2668 retry:
2669 /*
2670 * If next_task preempts rq->curr, and rq->curr
2671 * can move away, it makes sense to just reschedule
2672 * without going further in pushing next_task.
2673 */
2674 if (dl_task(rq->donor) &&
2675 dl_time_before(next_task->dl.deadline, rq->donor->dl.deadline) &&
2676 rq->curr->nr_cpus_allowed > 1) {
2677 resched_curr(rq);
2678 return 0;
2679 }
2680
2681 if (is_migration_disabled(next_task))
2682 return 0;
2683
2684 if (WARN_ON(next_task == rq->curr))
2685 return 0;
2686
2687 /* We might release rq lock */
2688 get_task_struct(next_task);
2689
2690 /* Will lock the rq it'll find */
2691 later_rq = find_lock_later_rq(next_task, rq);
2692 if (!later_rq) {
2693 struct task_struct *task;
2694
2695 /*
2696 * We must check all this again, since
2697 * find_lock_later_rq releases rq->lock and it is
2698 * then possible that next_task has migrated.
2699 */
2700 task = pick_next_pushable_dl_task(rq);
2701 if (task == next_task) {
2702 /*
2703 * The task is still there. We don't try
2704 * again, some other CPU will pull it when ready.
2705 */
2706 goto out;
2707 }
2708
2709 if (!task)
2710 /* No more tasks */
2711 goto out;
2712
2713 put_task_struct(next_task);
2714 next_task = task;
2715 goto retry;
2716 }
2717
2718 move_queued_task_locked(rq, later_rq, next_task);
2719 ret = 1;
2720
2721 resched_curr(later_rq);
2722
2723 double_unlock_balance(rq, later_rq);
2724
2725 out:
2726 put_task_struct(next_task);
2727
2728 return ret;
2729 }
2730
push_dl_tasks(struct rq * rq)2731 static void push_dl_tasks(struct rq *rq)
2732 {
2733 /* push_dl_task() will return true if it moved a -deadline task */
2734 while (push_dl_task(rq))
2735 ;
2736 }
2737
pull_dl_task(struct rq * this_rq)2738 static void pull_dl_task(struct rq *this_rq)
2739 {
2740 int this_cpu = this_rq->cpu, cpu;
2741 struct task_struct *p, *push_task;
2742 bool resched = false;
2743 struct rq *src_rq;
2744 u64 dmin = LONG_MAX;
2745
2746 if (likely(!dl_overloaded(this_rq)))
2747 return;
2748
2749 /*
2750 * Match the barrier from dl_set_overloaded; this guarantees that if we
2751 * see overloaded we must also see the dlo_mask bit.
2752 */
2753 smp_rmb();
2754
2755 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2756 if (this_cpu == cpu)
2757 continue;
2758
2759 src_rq = cpu_rq(cpu);
2760
2761 /*
2762 * It looks racy, and it is! However, as in sched_rt.c,
2763 * we are fine with this.
2764 */
2765 if (this_rq->dl.dl_nr_running &&
2766 dl_time_before(this_rq->dl.earliest_dl.curr,
2767 src_rq->dl.earliest_dl.next))
2768 continue;
2769
2770 /* Might drop this_rq->lock */
2771 push_task = NULL;
2772 double_lock_balance(this_rq, src_rq);
2773
2774 /*
2775 * If there are no more pullable tasks on the
2776 * rq, we're done with it.
2777 */
2778 if (src_rq->dl.dl_nr_running <= 1)
2779 goto skip;
2780
2781 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
2782
2783 /*
2784 * We found a task to be pulled if:
2785 * - it preempts our current (if there's one),
2786 * - it will preempt the last one we pulled (if any).
2787 */
2788 if (p && dl_time_before(p->dl.deadline, dmin) &&
2789 dl_task_is_earliest_deadline(p, this_rq)) {
2790 WARN_ON(p == src_rq->curr);
2791 WARN_ON(!task_on_rq_queued(p));
2792
2793 /*
2794 * Then we pull iff p has actually an earlier
2795 * deadline than the current task of its runqueue.
2796 */
2797 if (dl_time_before(p->dl.deadline,
2798 src_rq->donor->dl.deadline))
2799 goto skip;
2800
2801 if (is_migration_disabled(p)) {
2802 push_task = get_push_task(src_rq);
2803 } else {
2804 move_queued_task_locked(src_rq, this_rq, p);
2805 dmin = p->dl.deadline;
2806 resched = true;
2807 }
2808
2809 /* Is there any other task even earlier? */
2810 }
2811 skip:
2812 double_unlock_balance(this_rq, src_rq);
2813
2814 if (push_task) {
2815 preempt_disable();
2816 raw_spin_rq_unlock(this_rq);
2817 stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2818 push_task, &src_rq->push_work);
2819 preempt_enable();
2820 raw_spin_rq_lock(this_rq);
2821 }
2822 }
2823
2824 if (resched)
2825 resched_curr(this_rq);
2826 }
2827
2828 /*
2829 * Since the task is not running and a reschedule is not going to happen
2830 * anytime soon on its runqueue, we try pushing it away now.
2831 */
task_woken_dl(struct rq * rq,struct task_struct * p)2832 static void task_woken_dl(struct rq *rq, struct task_struct *p)
2833 {
2834 if (!task_on_cpu(rq, p) &&
2835 !test_tsk_need_resched(rq->curr) &&
2836 p->nr_cpus_allowed > 1 &&
2837 dl_task(rq->donor) &&
2838 (rq->curr->nr_cpus_allowed < 2 ||
2839 !dl_entity_preempt(&p->dl, &rq->donor->dl))) {
2840 push_dl_tasks(rq);
2841 }
2842 }
2843
set_cpus_allowed_dl(struct task_struct * p,struct affinity_context * ctx)2844 static void set_cpus_allowed_dl(struct task_struct *p,
2845 struct affinity_context *ctx)
2846 {
2847 struct root_domain *src_rd;
2848 struct rq *rq;
2849
2850 WARN_ON_ONCE(!dl_task(p));
2851
2852 rq = task_rq(p);
2853 src_rd = rq->rd;
2854 /*
2855 * Migrating a SCHED_DEADLINE task between exclusive
2856 * cpusets (different root_domains) entails a bandwidth
2857 * update. We already made space for us in the destination
2858 * domain (see cpuset_can_attach()).
2859 */
2860 if (!cpumask_intersects(src_rd->span, ctx->new_mask)) {
2861 struct dl_bw *src_dl_b;
2862
2863 src_dl_b = dl_bw_of(cpu_of(rq));
2864 /*
2865 * We now free resources of the root_domain we are migrating
2866 * off. In the worst case, sched_setattr() may temporary fail
2867 * until we complete the update.
2868 */
2869 raw_spin_lock(&src_dl_b->lock);
2870 __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
2871 raw_spin_unlock(&src_dl_b->lock);
2872 }
2873
2874 set_cpus_allowed_common(p, ctx);
2875 }
2876
2877 /* Assumes rq->lock is held */
rq_online_dl(struct rq * rq)2878 static void rq_online_dl(struct rq *rq)
2879 {
2880 if (rq->dl.overloaded)
2881 dl_set_overload(rq);
2882
2883 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
2884 if (rq->dl.dl_nr_running > 0)
2885 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
2886 }
2887
2888 /* Assumes rq->lock is held */
rq_offline_dl(struct rq * rq)2889 static void rq_offline_dl(struct rq *rq)
2890 {
2891 if (rq->dl.overloaded)
2892 dl_clear_overload(rq);
2893
2894 cpudl_clear(&rq->rd->cpudl, rq->cpu);
2895 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
2896 }
2897
init_sched_dl_class(void)2898 void __init init_sched_dl_class(void)
2899 {
2900 unsigned int i;
2901
2902 for_each_possible_cpu(i)
2903 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2904 GFP_KERNEL, cpu_to_node(i));
2905 }
2906
dl_add_task_root_domain(struct task_struct * p)2907 void dl_add_task_root_domain(struct task_struct *p)
2908 {
2909 struct rq_flags rf;
2910 struct rq *rq;
2911 struct dl_bw *dl_b;
2912
2913 raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
2914 if (!dl_task(p) || dl_entity_is_special(&p->dl)) {
2915 raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags);
2916 return;
2917 }
2918
2919 rq = __task_rq_lock(p, &rf);
2920
2921 dl_b = &rq->rd->dl_bw;
2922 raw_spin_lock(&dl_b->lock);
2923
2924 __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
2925
2926 raw_spin_unlock(&dl_b->lock);
2927
2928 task_rq_unlock(rq, p, &rf);
2929 }
2930
dl_clear_root_domain(struct root_domain * rd)2931 void dl_clear_root_domain(struct root_domain *rd)
2932 {
2933 int i;
2934
2935 guard(raw_spinlock_irqsave)(&rd->dl_bw.lock);
2936
2937 /*
2938 * Reset total_bw to zero and extra_bw to max_bw so that next
2939 * loop will add dl-servers contributions back properly,
2940 */
2941 rd->dl_bw.total_bw = 0;
2942 for_each_cpu(i, rd->span)
2943 cpu_rq(i)->dl.extra_bw = cpu_rq(i)->dl.max_bw;
2944
2945 /*
2946 * dl_servers are not tasks. Since dl_add_task_root_domain ignores
2947 * them, we need to account for them here explicitly.
2948 */
2949 for_each_cpu(i, rd->span) {
2950 struct sched_dl_entity *dl_se = &cpu_rq(i)->fair_server;
2951
2952 if (dl_server(dl_se) && cpu_active(i))
2953 __dl_add(&rd->dl_bw, dl_se->dl_bw, dl_bw_cpus(i));
2954 }
2955 }
2956
dl_clear_root_domain_cpu(int cpu)2957 void dl_clear_root_domain_cpu(int cpu)
2958 {
2959 dl_clear_root_domain(cpu_rq(cpu)->rd);
2960 }
2961
switched_from_dl(struct rq * rq,struct task_struct * p)2962 static void switched_from_dl(struct rq *rq, struct task_struct *p)
2963 {
2964 /*
2965 * task_non_contending() can start the "inactive timer" (if the 0-lag
2966 * time is in the future). If the task switches back to dl before
2967 * the "inactive timer" fires, it can continue to consume its current
2968 * runtime using its current deadline. If it stays outside of
2969 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2970 * will reset the task parameters.
2971 */
2972 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2973 task_non_contending(&p->dl);
2974
2975 /*
2976 * In case a task is setscheduled out from SCHED_DEADLINE we need to
2977 * keep track of that on its cpuset (for correct bandwidth tracking).
2978 */
2979 dec_dl_tasks_cs(p);
2980
2981 if (!task_on_rq_queued(p)) {
2982 /*
2983 * Inactive timer is armed. However, p is leaving DEADLINE and
2984 * might migrate away from this rq while continuing to run on
2985 * some other class. We need to remove its contribution from
2986 * this rq running_bw now, or sub_rq_bw (below) will complain.
2987 */
2988 if (p->dl.dl_non_contending)
2989 sub_running_bw(&p->dl, &rq->dl);
2990 sub_rq_bw(&p->dl, &rq->dl);
2991 }
2992
2993 /*
2994 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2995 * at the 0-lag time, because the task could have been migrated
2996 * while SCHED_OTHER in the meanwhile.
2997 */
2998 if (p->dl.dl_non_contending)
2999 p->dl.dl_non_contending = 0;
3000
3001 /*
3002 * Since this might be the only -deadline task on the rq,
3003 * this is the right place to try to pull some other one
3004 * from an overloaded CPU, if any.
3005 */
3006 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
3007 return;
3008
3009 deadline_queue_pull_task(rq);
3010 }
3011
3012 /*
3013 * When switching to -deadline, we may overload the rq, then
3014 * we try to push someone off, if possible.
3015 */
switched_to_dl(struct rq * rq,struct task_struct * p)3016 static void switched_to_dl(struct rq *rq, struct task_struct *p)
3017 {
3018 cancel_inactive_timer(&p->dl);
3019
3020 /*
3021 * In case a task is setscheduled to SCHED_DEADLINE we need to keep
3022 * track of that on its cpuset (for correct bandwidth tracking).
3023 */
3024 inc_dl_tasks_cs(p);
3025
3026 /* If p is not queued we will update its parameters at next wakeup. */
3027 if (!task_on_rq_queued(p)) {
3028 add_rq_bw(&p->dl, &rq->dl);
3029
3030 return;
3031 }
3032
3033 if (rq->donor != p) {
3034 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
3035 deadline_queue_push_tasks(rq);
3036 if (dl_task(rq->donor))
3037 wakeup_preempt_dl(rq, p, 0);
3038 else
3039 resched_curr(rq);
3040 } else {
3041 update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
3042 }
3043 }
3044
3045 /*
3046 * If the scheduling parameters of a -deadline task changed,
3047 * a push or pull operation might be needed.
3048 */
prio_changed_dl(struct rq * rq,struct task_struct * p,int oldprio)3049 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
3050 int oldprio)
3051 {
3052 if (!task_on_rq_queued(p))
3053 return;
3054
3055 /*
3056 * This might be too much, but unfortunately
3057 * we don't have the old deadline value, and
3058 * we can't argue if the task is increasing
3059 * or lowering its prio, so...
3060 */
3061 if (!rq->dl.overloaded)
3062 deadline_queue_pull_task(rq);
3063
3064 if (task_current_donor(rq, p)) {
3065 /*
3066 * If we now have a earlier deadline task than p,
3067 * then reschedule, provided p is still on this
3068 * runqueue.
3069 */
3070 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
3071 resched_curr(rq);
3072 } else {
3073 /*
3074 * Current may not be deadline in case p was throttled but we
3075 * have just replenished it (e.g. rt_mutex_setprio()).
3076 *
3077 * Otherwise, if p was given an earlier deadline, reschedule.
3078 */
3079 if (!dl_task(rq->curr) ||
3080 dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
3081 resched_curr(rq);
3082 }
3083 }
3084
3085 #ifdef CONFIG_SCHED_CORE
task_is_throttled_dl(struct task_struct * p,int cpu)3086 static int task_is_throttled_dl(struct task_struct *p, int cpu)
3087 {
3088 return p->dl.dl_throttled;
3089 }
3090 #endif
3091
3092 DEFINE_SCHED_CLASS(dl) = {
3093
3094 .enqueue_task = enqueue_task_dl,
3095 .dequeue_task = dequeue_task_dl,
3096 .yield_task = yield_task_dl,
3097
3098 .wakeup_preempt = wakeup_preempt_dl,
3099
3100 .pick_task = pick_task_dl,
3101 .put_prev_task = put_prev_task_dl,
3102 .set_next_task = set_next_task_dl,
3103
3104 .balance = balance_dl,
3105 .select_task_rq = select_task_rq_dl,
3106 .migrate_task_rq = migrate_task_rq_dl,
3107 .set_cpus_allowed = set_cpus_allowed_dl,
3108 .rq_online = rq_online_dl,
3109 .rq_offline = rq_offline_dl,
3110 .task_woken = task_woken_dl,
3111 .find_lock_rq = find_lock_later_rq,
3112
3113 .task_tick = task_tick_dl,
3114 .task_fork = task_fork_dl,
3115
3116 .prio_changed = prio_changed_dl,
3117 .switched_from = switched_from_dl,
3118 .switched_to = switched_to_dl,
3119
3120 .update_curr = update_curr_dl,
3121 #ifdef CONFIG_SCHED_CORE
3122 .task_is_throttled = task_is_throttled_dl,
3123 #endif
3124 };
3125
3126 /*
3127 * Used for dl_bw check and update, used under sched_rt_handler()::mutex and
3128 * sched_domains_mutex.
3129 */
3130 u64 dl_cookie;
3131
sched_dl_global_validate(void)3132 int sched_dl_global_validate(void)
3133 {
3134 u64 runtime = global_rt_runtime();
3135 u64 period = global_rt_period();
3136 u64 new_bw = to_ratio(period, runtime);
3137 u64 cookie = ++dl_cookie;
3138 struct dl_bw *dl_b;
3139 int cpu, cpus, ret = 0;
3140 unsigned long flags;
3141
3142 /*
3143 * Here we want to check the bandwidth not being set to some
3144 * value smaller than the currently allocated bandwidth in
3145 * any of the root_domains.
3146 */
3147 for_each_online_cpu(cpu) {
3148 rcu_read_lock_sched();
3149
3150 if (dl_bw_visited(cpu, cookie))
3151 goto next;
3152
3153 dl_b = dl_bw_of(cpu);
3154 cpus = dl_bw_cpus(cpu);
3155
3156 raw_spin_lock_irqsave(&dl_b->lock, flags);
3157 if (new_bw * cpus < dl_b->total_bw)
3158 ret = -EBUSY;
3159 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
3160
3161 next:
3162 rcu_read_unlock_sched();
3163
3164 if (ret)
3165 break;
3166 }
3167
3168 return ret;
3169 }
3170
init_dl_rq_bw_ratio(struct dl_rq * dl_rq)3171 static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
3172 {
3173 if (global_rt_runtime() == RUNTIME_INF) {
3174 dl_rq->bw_ratio = 1 << RATIO_SHIFT;
3175 dl_rq->max_bw = dl_rq->extra_bw = 1 << BW_SHIFT;
3176 } else {
3177 dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
3178 global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
3179 dl_rq->max_bw = dl_rq->extra_bw =
3180 to_ratio(global_rt_period(), global_rt_runtime());
3181 }
3182 }
3183
sched_dl_do_global(void)3184 void sched_dl_do_global(void)
3185 {
3186 u64 new_bw = -1;
3187 u64 cookie = ++dl_cookie;
3188 struct dl_bw *dl_b;
3189 int cpu;
3190 unsigned long flags;
3191
3192 if (global_rt_runtime() != RUNTIME_INF)
3193 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
3194
3195 for_each_possible_cpu(cpu)
3196 init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
3197
3198 for_each_possible_cpu(cpu) {
3199 rcu_read_lock_sched();
3200
3201 if (dl_bw_visited(cpu, cookie)) {
3202 rcu_read_unlock_sched();
3203 continue;
3204 }
3205
3206 dl_b = dl_bw_of(cpu);
3207
3208 raw_spin_lock_irqsave(&dl_b->lock, flags);
3209 dl_b->bw = new_bw;
3210 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
3211
3212 rcu_read_unlock_sched();
3213 }
3214 }
3215
3216 /*
3217 * We must be sure that accepting a new task (or allowing changing the
3218 * parameters of an existing one) is consistent with the bandwidth
3219 * constraints. If yes, this function also accordingly updates the currently
3220 * allocated bandwidth to reflect the new situation.
3221 *
3222 * This function is called while holding p's rq->lock.
3223 */
sched_dl_overflow(struct task_struct * p,int policy,const struct sched_attr * attr)3224 int sched_dl_overflow(struct task_struct *p, int policy,
3225 const struct sched_attr *attr)
3226 {
3227 u64 period = attr->sched_period ?: attr->sched_deadline;
3228 u64 runtime = attr->sched_runtime;
3229 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
3230 int cpus, err = -1, cpu = task_cpu(p);
3231 struct dl_bw *dl_b = dl_bw_of(cpu);
3232 unsigned long cap;
3233
3234 if (attr->sched_flags & SCHED_FLAG_SUGOV)
3235 return 0;
3236
3237 /* !deadline task may carry old deadline bandwidth */
3238 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
3239 return 0;
3240
3241 /*
3242 * Either if a task, enters, leave, or stays -deadline but changes
3243 * its parameters, we may need to update accordingly the total
3244 * allocated bandwidth of the container.
3245 */
3246 raw_spin_lock(&dl_b->lock);
3247 cpus = dl_bw_cpus(cpu);
3248 cap = dl_bw_capacity(cpu);
3249
3250 if (dl_policy(policy) && !task_has_dl_policy(p) &&
3251 !__dl_overflow(dl_b, cap, 0, new_bw)) {
3252 if (hrtimer_active(&p->dl.inactive_timer))
3253 __dl_sub(dl_b, p->dl.dl_bw, cpus);
3254 __dl_add(dl_b, new_bw, cpus);
3255 err = 0;
3256 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
3257 !__dl_overflow(dl_b, cap, p->dl.dl_bw, new_bw)) {
3258 /*
3259 * XXX this is slightly incorrect: when the task
3260 * utilization decreases, we should delay the total
3261 * utilization change until the task's 0-lag point.
3262 * But this would require to set the task's "inactive
3263 * timer" when the task is not inactive.
3264 */
3265 __dl_sub(dl_b, p->dl.dl_bw, cpus);
3266 __dl_add(dl_b, new_bw, cpus);
3267 dl_change_utilization(p, new_bw);
3268 err = 0;
3269 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
3270 /*
3271 * Do not decrease the total deadline utilization here,
3272 * switched_from_dl() will take care to do it at the correct
3273 * (0-lag) time.
3274 */
3275 err = 0;
3276 }
3277 raw_spin_unlock(&dl_b->lock);
3278
3279 return err;
3280 }
3281
3282 /*
3283 * This function initializes the sched_dl_entity of a newly becoming
3284 * SCHED_DEADLINE task.
3285 *
3286 * Only the static values are considered here, the actual runtime and the
3287 * absolute deadline will be properly calculated when the task is enqueued
3288 * for the first time with its new policy.
3289 */
__setparam_dl(struct task_struct * p,const struct sched_attr * attr)3290 void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
3291 {
3292 struct sched_dl_entity *dl_se = &p->dl;
3293
3294 dl_se->dl_runtime = attr->sched_runtime;
3295 dl_se->dl_deadline = attr->sched_deadline;
3296 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
3297 dl_se->flags = attr->sched_flags & SCHED_DL_FLAGS;
3298 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
3299 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
3300 }
3301
__getparam_dl(struct task_struct * p,struct sched_attr * attr)3302 void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
3303 {
3304 struct sched_dl_entity *dl_se = &p->dl;
3305
3306 attr->sched_priority = p->rt_priority;
3307 attr->sched_runtime = dl_se->dl_runtime;
3308 attr->sched_deadline = dl_se->dl_deadline;
3309 attr->sched_period = dl_se->dl_period;
3310 attr->sched_flags &= ~SCHED_DL_FLAGS;
3311 attr->sched_flags |= dl_se->flags;
3312 }
3313
3314 /*
3315 * This function validates the new parameters of a -deadline task.
3316 * We ask for the deadline not being zero, and greater or equal
3317 * than the runtime, as well as the period of being zero or
3318 * greater than deadline. Furthermore, we have to be sure that
3319 * user parameters are above the internal resolution of 1us (we
3320 * check sched_runtime only since it is always the smaller one) and
3321 * below 2^63 ns (we have to check both sched_deadline and
3322 * sched_period, as the latter can be zero).
3323 */
__checkparam_dl(const struct sched_attr * attr)3324 bool __checkparam_dl(const struct sched_attr *attr)
3325 {
3326 u64 period, max, min;
3327
3328 /* special dl tasks don't actually use any parameter */
3329 if (attr->sched_flags & SCHED_FLAG_SUGOV)
3330 return true;
3331
3332 /* deadline != 0 */
3333 if (attr->sched_deadline == 0)
3334 return false;
3335
3336 /*
3337 * Since we truncate DL_SCALE bits, make sure we're at least
3338 * that big.
3339 */
3340 if (attr->sched_runtime < (1ULL << DL_SCALE))
3341 return false;
3342
3343 /*
3344 * Since we use the MSB for wrap-around and sign issues, make
3345 * sure it's not set (mind that period can be equal to zero).
3346 */
3347 if (attr->sched_deadline & (1ULL << 63) ||
3348 attr->sched_period & (1ULL << 63))
3349 return false;
3350
3351 period = attr->sched_period;
3352 if (!period)
3353 period = attr->sched_deadline;
3354
3355 /* runtime <= deadline <= period (if period != 0) */
3356 if (period < attr->sched_deadline ||
3357 attr->sched_deadline < attr->sched_runtime)
3358 return false;
3359
3360 max = (u64)READ_ONCE(sysctl_sched_dl_period_max) * NSEC_PER_USEC;
3361 min = (u64)READ_ONCE(sysctl_sched_dl_period_min) * NSEC_PER_USEC;
3362
3363 if (period < min || period > max)
3364 return false;
3365
3366 return true;
3367 }
3368
3369 /*
3370 * This function clears the sched_dl_entity static params.
3371 */
__dl_clear_params(struct sched_dl_entity * dl_se)3372 static void __dl_clear_params(struct sched_dl_entity *dl_se)
3373 {
3374 dl_se->dl_runtime = 0;
3375 dl_se->dl_deadline = 0;
3376 dl_se->dl_period = 0;
3377 dl_se->flags = 0;
3378 dl_se->dl_bw = 0;
3379 dl_se->dl_density = 0;
3380
3381 dl_se->dl_throttled = 0;
3382 dl_se->dl_yielded = 0;
3383 dl_se->dl_non_contending = 0;
3384 dl_se->dl_overrun = 0;
3385 dl_se->dl_server = 0;
3386
3387 #ifdef CONFIG_RT_MUTEXES
3388 dl_se->pi_se = dl_se;
3389 #endif
3390 }
3391
init_dl_entity(struct sched_dl_entity * dl_se)3392 void init_dl_entity(struct sched_dl_entity *dl_se)
3393 {
3394 RB_CLEAR_NODE(&dl_se->rb_node);
3395 init_dl_task_timer(dl_se);
3396 init_dl_inactive_task_timer(dl_se);
3397 __dl_clear_params(dl_se);
3398 }
3399
dl_param_changed(struct task_struct * p,const struct sched_attr * attr)3400 bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
3401 {
3402 struct sched_dl_entity *dl_se = &p->dl;
3403
3404 if (dl_se->dl_runtime != attr->sched_runtime ||
3405 dl_se->dl_deadline != attr->sched_deadline ||
3406 dl_se->dl_period != attr->sched_period ||
3407 dl_se->flags != (attr->sched_flags & SCHED_DL_FLAGS))
3408 return true;
3409
3410 return false;
3411 }
3412
dl_cpuset_cpumask_can_shrink(const struct cpumask * cur,const struct cpumask * trial)3413 int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
3414 const struct cpumask *trial)
3415 {
3416 unsigned long flags, cap;
3417 struct dl_bw *cur_dl_b;
3418 int ret = 1;
3419
3420 rcu_read_lock_sched();
3421 cur_dl_b = dl_bw_of(cpumask_any(cur));
3422 cap = __dl_bw_capacity(trial);
3423 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
3424 if (__dl_overflow(cur_dl_b, cap, 0, 0))
3425 ret = 0;
3426 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
3427 rcu_read_unlock_sched();
3428
3429 return ret;
3430 }
3431
3432 enum dl_bw_request {
3433 dl_bw_req_deactivate = 0,
3434 dl_bw_req_alloc,
3435 dl_bw_req_free
3436 };
3437
dl_bw_manage(enum dl_bw_request req,int cpu,u64 dl_bw)3438 static int dl_bw_manage(enum dl_bw_request req, int cpu, u64 dl_bw)
3439 {
3440 unsigned long flags, cap;
3441 struct dl_bw *dl_b;
3442 bool overflow = 0;
3443 u64 fair_server_bw = 0;
3444
3445 rcu_read_lock_sched();
3446 dl_b = dl_bw_of(cpu);
3447 raw_spin_lock_irqsave(&dl_b->lock, flags);
3448
3449 cap = dl_bw_capacity(cpu);
3450 switch (req) {
3451 case dl_bw_req_free:
3452 __dl_sub(dl_b, dl_bw, dl_bw_cpus(cpu));
3453 break;
3454 case dl_bw_req_alloc:
3455 overflow = __dl_overflow(dl_b, cap, 0, dl_bw);
3456
3457 if (!overflow) {
3458 /*
3459 * We reserve space in the destination
3460 * root_domain, as we can't fail after this point.
3461 * We will free resources in the source root_domain
3462 * later on (see set_cpus_allowed_dl()).
3463 */
3464 __dl_add(dl_b, dl_bw, dl_bw_cpus(cpu));
3465 }
3466 break;
3467 case dl_bw_req_deactivate:
3468 /*
3469 * cpu is not off yet, but we need to do the math by
3470 * considering it off already (i.e., what would happen if we
3471 * turn cpu off?).
3472 */
3473 cap -= arch_scale_cpu_capacity(cpu);
3474
3475 /*
3476 * cpu is going offline and NORMAL tasks will be moved away
3477 * from it. We can thus discount dl_server bandwidth
3478 * contribution as it won't need to be servicing tasks after
3479 * the cpu is off.
3480 */
3481 if (cpu_rq(cpu)->fair_server.dl_server)
3482 fair_server_bw = cpu_rq(cpu)->fair_server.dl_bw;
3483
3484 /*
3485 * Not much to check if no DEADLINE bandwidth is present.
3486 * dl_servers we can discount, as tasks will be moved out the
3487 * offlined CPUs anyway.
3488 */
3489 if (dl_b->total_bw - fair_server_bw > 0) {
3490 /*
3491 * Leaving at least one CPU for DEADLINE tasks seems a
3492 * wise thing to do. As said above, cpu is not offline
3493 * yet, so account for that.
3494 */
3495 if (dl_bw_cpus(cpu) - 1)
3496 overflow = __dl_overflow(dl_b, cap, fair_server_bw, 0);
3497 else
3498 overflow = 1;
3499 }
3500
3501 break;
3502 }
3503
3504 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
3505 rcu_read_unlock_sched();
3506
3507 return overflow ? -EBUSY : 0;
3508 }
3509
dl_bw_deactivate(int cpu)3510 int dl_bw_deactivate(int cpu)
3511 {
3512 return dl_bw_manage(dl_bw_req_deactivate, cpu, 0);
3513 }
3514
dl_bw_alloc(int cpu,u64 dl_bw)3515 int dl_bw_alloc(int cpu, u64 dl_bw)
3516 {
3517 return dl_bw_manage(dl_bw_req_alloc, cpu, dl_bw);
3518 }
3519
dl_bw_free(int cpu,u64 dl_bw)3520 void dl_bw_free(int cpu, u64 dl_bw)
3521 {
3522 dl_bw_manage(dl_bw_req_free, cpu, dl_bw);
3523 }
3524
print_dl_stats(struct seq_file * m,int cpu)3525 void print_dl_stats(struct seq_file *m, int cpu)
3526 {
3527 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
3528 }
3529