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