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