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