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