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