blob: a84299f44b5d8c2990e10f9a74d1818fad3a7040 [file] [log] [blame]
Dario Faggioliaab03e02013-11-28 11:14:43 +01001/*
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>,
Juri Lelli1baca4c2013-11-07 14:43:38 +010013 * Juri Lelli <juri.lelli@gmail.com>,
Dario Faggioliaab03e02013-11-28 11:14:43 +010014 * Michael Trimarchi <michael@amarulasolutions.com>,
15 * Fabio Checconi <fchecconi@gmail.com>
16 */
17#include "sched.h"
18
Juri Lelli6bfd6d72013-11-07 14:43:47 +010019#include <linux/slab.h>
Nicolas Pitre06a76fe2017-06-21 14:22:01 -040020#include <uapi/linux/sched/types.h>
Juri Lelli6bfd6d72013-11-07 14:43:47 +010021
Dario Faggioli332ac172013-11-07 14:43:45 +010022struct dl_bandwidth def_dl_bandwidth;
23
Dario Faggioliaab03e02013-11-28 11:14:43 +010024static 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
Nicolas Pitre06a76fe2017-06-21 14:22:01 -040047#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
Luca Abenie36d8672017-05-18 22:13:28 +020079static 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 */
Luca Abeni8fd27232017-05-18 22:13:34 +020087 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
Luca Abenie36d8672017-05-18 22:13:28 +020088}
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
Luca Abeni8fd27232017-05-18 22:13:34 +0200102static 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
Luca Abeni209a0cb2017-05-18 22:13:29 +0200125void dl_change_utilization(struct task_struct *p, u64 new_bw)
126{
Luca Abeni8fd27232017-05-18 22:13:34 +0200127 struct rq *rq;
128
Luca Abeni209a0cb2017-05-18 22:13:29 +0200129 if (task_on_rq_queued(p))
130 return;
131
Luca Abeni8fd27232017-05-18 22:13:34 +0200132 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);
Luca Abeni209a0cb2017-05-18 22:13:29 +0200148}
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);
Luca Abeni387e3132017-05-18 22:13:30 +0200239 if (!dl_task(p) || p->state == TASK_DEAD) {
240 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
241
Luca Abeni8fd27232017-05-18 22:13:34 +0200242 if (p->state == TASK_DEAD)
243 sub_rq_bw(p->dl.dl_bw, &rq->dl);
Luca Abeni387e3132017-05-18 22:13:30 +0200244 raw_spin_lock(&dl_b->lock);
Luca Abenidaec5792017-05-18 22:13:36 +0200245 __dl_clear(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
Luca Abeni209a0cb2017-05-18 22:13:29 +0200246 __dl_clear_params(p);
Luca Abeni387e3132017-05-18 22:13:30 +0200247 raw_spin_unlock(&dl_b->lock);
248 }
Luca Abeni209a0cb2017-05-18 22:13:29 +0200249
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
Luca Abeni8fd27232017-05-18 22:13:34 +0200258static void task_contending(struct sched_dl_entity *dl_se, int flags)
Luca Abeni209a0cb2017-05-18 22:13:29 +0200259{
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
Luca Abeni8fd27232017-05-18 22:13:34 +0200269 if (flags & ENQUEUE_MIGRATED)
270 add_rq_bw(dl_se->dl_bw, dl_rq);
271
Luca Abeni209a0cb2017-05-18 22:13:29 +0200272 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
Dario Faggioliaab03e02013-11-28 11:14:43 +0100295static 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
Dario Faggioli332ac172013-11-07 14:43:45 +0100302void 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
Dario Faggioli332ac172013-11-07 14:43:45 +0100309void 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);
Peter Zijlstra17248132013-12-17 12:44:49 +0100313 if (global_rt_runtime() == RUNTIME_INF)
Dario Faggioli332ac172013-11-07 14:43:45 +0100314 dl_b->bw = -1;
315 else
Peter Zijlstra17248132013-12-17 12:44:49 +0100316 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +0100317 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
318 dl_b->total_bw = 0;
319}
320
Abel Vesa07c54f72015-03-03 13:50:27 +0200321void init_dl_rq(struct dl_rq *dl_rq)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100322{
323 dl_rq->rb_root = RB_ROOT;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100324
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;
Dario Faggioli332ac172013-11-07 14:43:45 +0100332#else
333 init_dl_bw(&dl_rq->dl_bw);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100334#endif
Luca Abenie36d8672017-05-18 22:13:28 +0200335
336 dl_rq->running_bw = 0;
Luca Abeni8fd27232017-05-18 22:13:34 +0200337 dl_rq->this_bw = 0;
Luca Abeni4da3abc2017-05-18 22:13:32 +0200338 init_dl_rq_bw_ratio(dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100339}
340
Juri Lelli1baca4c2013-11-07 14:43:38 +0100341#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{
Kirill Tkhai995b9ea2014-02-18 02:24:13 +0400375 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100376 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);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100389
Ingo Molnar4b53a342017-02-05 15:41:03 +0100390 if (p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100391 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);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100399
Ingo Molnar4b53a342017-02-05 15:41:03 +0100400 if (p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100401 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
Wanpeng Li7d92de32015-12-03 17:42:10 +0800432 if (leftmost) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100433 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
Wanpeng Li7d92de32015-12-03 17:42:10 +0800434 dl_rq->earliest_dl.next = p->dl.deadline;
435 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100436
437 rb_link_node(&p->pushable_dl_tasks, parent, link);
438 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
439}
440
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;
Wanpeng Li7d92de32015-12-03 17:42:10 +0800453 if (next_node) {
454 dl_rq->earliest_dl.next = rb_entry(next_node,
455 struct task_struct, pushable_dl_tasks)->dl.deadline;
456 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100457 }
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
Peter Zijlstradc877342014-02-12 15:47:29 +0100470static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
471{
472 return dl_task(prev);
473}
474
Peter Zijlstra9916e212015-06-11 14:46:43 +0200475static DEFINE_PER_CPU(struct callback_head, dl_push_head);
476static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200477
478static void push_dl_tasks(struct rq *);
Peter Zijlstra9916e212015-06-11 14:46:43 +0200479static void pull_dl_task(struct rq *);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200480
481static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100482{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200483 if (!has_pushable_dl_tasks(rq))
484 return;
485
Peter Zijlstra9916e212015-06-11 14:46:43 +0200486 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);
Peter Zijlstradc877342014-02-12 15:47:29 +0100492}
493
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800494static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
495
Peter Zijlstraa649f232015-06-11 14:46:49 +0200496static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800497{
498 struct rq *later_rq = NULL;
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800499
500 later_rq = find_lock_later_rq(p, rq);
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800501 if (!later_rq) {
502 int cpu;
503
504 /*
505 * If we cannot preempt any rq, fall back to pick any
506 * online cpu.
507 */
Ingo Molnar0c98d342017-02-05 15:38:10 +0100508 cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800509 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
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800527 set_task_cpu(p, later_rq->cpu);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200528 double_unlock_balance(later_rq, rq);
529
530 return later_rq;
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800531}
532
Juri Lelli1baca4c2013-11-07 14:43:38 +0100533#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
Peter Zijlstradc877342014-02-12 15:47:29 +0100555static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
556{
557 return false;
558}
559
Peter Zijlstra0ea60c22015-06-11 14:46:42 +0200560static inline void pull_dl_task(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100561{
Peter Zijlstradc877342014-02-12 15:47:29 +0100562}
563
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200564static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100565{
566}
Peter Zijlstra9916e212015-06-11 14:46:43 +0200567
568static inline void queue_pull_task(struct rq *rq)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100569{
570}
571#endif /* CONFIG_SMP */
572
Dario Faggioliaab03e02013-11-28 11:14:43 +0100573static 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 */
Juri Lelli98b0a852016-08-05 16:07:55 +0100590static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100591{
592 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
593 struct rq *rq = rq_of_dl_rq(dl_rq);
594
Juri Lelli98b0a852016-08-05 16:07:55 +0100595 WARN_ON(dl_se->dl_boosted);
Luca Abeni72f9f3f2016-03-07 12:27:04 +0100596 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;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100605
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 */
Juri Lelli98b0a852016-08-05 16:07:55 +0100611 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
612 dl_se->runtime = dl_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100613}
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
xiaofeng.yan1b09d292014-07-07 05:59:04 +0000631 * runtime, or it just underestimated it during sched_setattr().
Dario Faggioliaab03e02013-11-28 11:14:43 +0100632 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100633static void replenish_dl_entity(struct sched_dl_entity *dl_se,
634 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100635{
636 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
637 struct rq *rq = rq_of_dl_rq(dl_rq);
638
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100639 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
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100650 if (dl_se->dl_yielded && dl_se->runtime > 0)
651 dl_se->runtime = 0;
652
Dario Faggioliaab03e02013-11-28 11:14:43 +0100653 /*
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) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100660 dl_se->deadline += pi_se->dl_period;
661 dl_se->runtime += pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100662 }
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))) {
Steven Rostedtc219b7d2016-02-10 12:04:22 -0500674 printk_deferred_once("sched: DL replenish lagged too much\n");
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100675 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
676 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100677 }
Peter Zijlstra1019a352014-11-26 08:44:03 +0800678
679 if (dl_se->dl_yielded)
680 dl_se->dl_yielded = 0;
681 if (dl_se->dl_throttled)
682 dl_se->dl_throttled = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100683}
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
Dario Faggioli712e5e32014-01-27 12:20:15 +0100696 * result in breaking guarantees promised to other tasks (refer to
697 * Documentation/scheduler/sched-deadline.txt for more informations).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100698 *
699 * This function returns true if:
700 *
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100701 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
Dario Faggioliaab03e02013-11-28 11:14:43 +0100702 *
703 * IOW we can't recycle current parameters.
Harald Gustafsson755378a2013-11-07 14:43:40 +0100704 *
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100705 * Notice that the bandwidth check is done against the deadline. For
Harald Gustafsson755378a2013-11-07 14:43:40 +0100706 * task with deadline equal to period this is the same of using
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100707 * dl_period instead of dl_deadline in the equation above.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100708 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100709static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
710 struct sched_dl_entity *pi_se, u64 t)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100711{
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 */
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100732 left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
Dario Faggioli332ac172013-11-07 14:43:45 +0100733 right = ((dl_se->deadline - t) >> DL_SCALE) *
734 (pi_se->dl_runtime >> DL_SCALE);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100735
736 return dl_time_before(right, left);
737}
738
739/*
Daniel Bristot de Oliveira3effcb42017-05-29 16:24:03 +0200740 * 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.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100744 *
Daniel Bristot de Oliveira3effcb42017-05-29 16:24:03 +0200745 * 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.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100818 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100819static void update_dl_entity(struct sched_dl_entity *dl_se,
820 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100821{
822 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
823 struct rq *rq = rq_of_dl_rq(dl_rq);
824
Dario Faggioliaab03e02013-11-28 11:14:43 +0100825 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100826 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
Daniel Bristot de Oliveira3effcb42017-05-29 16:24:03 +0200827
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
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100835 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
836 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100837 }
838}
839
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100840static 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
Dario Faggioliaab03e02013-11-28 11:14:43 +0100845/*
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
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100848 * set the bandwidth replenishment timer to the replenishment instant
Dario Faggioliaab03e02013-11-28 11:14:43 +0100849 * 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 */
Peter Zijlstraa649f232015-06-11 14:46:49 +0200855static int start_dl_timer(struct task_struct *p)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100856{
Peter Zijlstraa649f232015-06-11 14:46:49 +0200857 struct sched_dl_entity *dl_se = &p->dl;
858 struct hrtimer *timer = &dl_se->dl_timer;
859 struct rq *rq = task_rq(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100860 ktime_t now, act;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100861 s64 delta;
862
Peter Zijlstraa649f232015-06-11 14:46:49 +0200863 lockdep_assert_held(&rq->lock);
864
Dario Faggioliaab03e02013-11-28 11:14:43 +0100865 /*
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 */
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100870 act = ns_to_ktime(dl_next_period(dl_se));
Peter Zijlstraa649f232015-06-11 14:46:49 +0200871 now = hrtimer_cb_get_time(timer);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100872 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
Peter Zijlstraa649f232015-06-11 14:46:49 +0200883 /*
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 }
Dario Faggioliaab03e02013-11-28 11:14:43 +0100896
Thomas Gleixnercc9684d2015-04-14 21:09:06 +0000897 return 1;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100898}
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);
Peter Zijlstraeb580752015-07-31 21:28:18 +0200919 struct rq_flags rf;
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400920 struct rq *rq;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100921
Peter Zijlstraeb580752015-07-31 21:28:18 +0200922 rq = task_rq_lock(p, &rf);
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400923
Dario Faggioliaab03e02013-11-28 11:14:43 +0100924 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200925 * The task might have changed its scheduling policy to something
Daniel Bristot de Oliveira9846d502016-11-08 11:15:23 +0100926 * different than SCHED_DEADLINE (through switched_from_dl()).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100927 */
Luca Abeni209a0cb2017-05-18 22:13:29 +0200928 if (!dl_task(p))
Peter Zijlstraa649f232015-06-11 14:46:49 +0200929 goto unlock;
Peter Zijlstraa649f232015-06-11 14:46:49 +0200930
931 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200932 * 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;
937
938 /*
939 * Spurious timer due to start_dl_timer() race; or we already received
940 * a replenishment from rt_mutex_setprio().
941 */
942 if (!dl_se->dl_throttled)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100943 goto unlock;
944
945 sched_clock_tick();
946 update_rq_clock(rq);
Kirill Tkhaia79ec892015-02-16 15:38:34 +0300947
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
Wanpeng Li61c7aca2016-08-31 18:27:44 +0800967#ifdef CONFIG_SMP
968 if (unlikely(!rq->online)) {
969 /*
970 * If the runqueue is no longer available, migrate the
971 * task elsewhere. This necessarily changes rq.
972 */
973 lockdep_unpin_lock(&rq->lock, rf.cookie);
974 rq = dl_task_offline_migration(rq, p);
975 rf.cookie = lockdep_pin_lock(&rq->lock);
Wanpeng Lidcc3b5f2017-03-06 21:51:28 -0800976 update_rq_clock(rq);
Wanpeng Li61c7aca2016-08-31 18:27:44 +0800977
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 */
983 }
984#endif
985
Peter Zijlstra1019a352014-11-26 08:44:03 +0800986 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);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200991
Juri Lelli1baca4c2013-11-07 14:43:38 +0100992#ifdef CONFIG_SMP
Peter Zijlstra1019a352014-11-26 08:44:03 +0800993 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200994 * Queueing this task back might have overloaded rq, check if we need
995 * to kick someone away.
Peter Zijlstra1019a352014-11-26 08:44:03 +0800996 */
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +0200997 if (has_pushable_dl_tasks(rq)) {
998 /*
999 * Nothing relies on rq->lock after this, so its safe to drop
1000 * rq->lock.
1001 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01001002 rq_unpin_lock(rq, &rf);
Peter Zijlstra1019a352014-11-26 08:44:03 +08001003 push_dl_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01001004 rq_repin_lock(rq, &rf);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02001005 }
Juri Lelli1baca4c2013-11-07 14:43:38 +01001006#endif
Peter Zijlstraa649f232015-06-11 14:46:49 +02001007
Dario Faggioliaab03e02013-11-28 11:14:43 +01001008unlock:
Peter Zijlstraeb580752015-07-31 21:28:18 +02001009 task_rq_unlock(rq, p, &rf);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001010
Peter Zijlstraa649f232015-06-11 14:46:49 +02001011 /*
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
Dario Faggioliaab03e02013-11-28 11:14:43 +01001017 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
Dario Faggioliaab03e02013-11-28 11:14:43 +01001024 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1025 timer->function = dl_task_timer;
1026}
1027
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001028/*
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;
Xunlei Pangae83b562017-05-10 21:03:37 +08001056 if (dl_se->runtime > 0)
1057 dl_se->runtime = 0;
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001058 }
1059}
1060
Dario Faggioliaab03e02013-11-28 11:14:43 +01001061static
Zhiqiang Zhang6fab5412015-06-15 11:15:20 +08001062int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001063{
Luca Abeni269ad802014-12-17 11:50:32 +01001064 return (dl_se->runtime <= 0);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001065}
1066
Juri Lellifaa59932014-02-21 11:37:15 +01001067extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
1068
Dario Faggioliaab03e02013-11-28 11:14:43 +01001069/*
Luca Abenic52f14d2017-05-18 22:13:31 +02001070 * This function implements the GRUB accounting rule:
1071 * according to the GRUB reclaiming algorithm, the runtime is
Luca Abenidaec5792017-05-18 22:13:36 +02001072 * 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.
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001079 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations
Luca Abenidaec5792017-05-18 22:13:36 +02001080 * 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.
Luca Abenic52f14d2017-05-18 22:13:31 +02001087 */
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001088u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
Luca Abenic52f14d2017-05-18 22:13:31 +02001089{
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001090 u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
1091 u64 u_act;
Luca Abenidaec5792017-05-18 22:13:36 +02001092 u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT;
Luca Abenic52f14d2017-05-18 22:13:31 +02001093
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001094 /*
Luca Abenidaec5792017-05-18 22:13:36 +02001095 * 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)
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001101 */
Luca Abenidaec5792017-05-18 22:13:36 +02001102 if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min)
1103 u_act = u_act_min;
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001104 else
Luca Abenidaec5792017-05-18 22:13:36 +02001105 u_act = BW_UNIT - u_inact - rq->dl.extra_bw;
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001106
1107 return (delta * u_act) >> BW_SHIFT;
Luca Abenic52f14d2017-05-18 22:13:31 +02001108}
1109
1110/*
Dario Faggioliaab03e02013-11-28 11:14:43 +01001111 * 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;
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001132 if (unlikely((s64)delta_exec <= 0)) {
1133 if (unlikely(dl_se->dl_yielded))
1134 goto throttle;
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001135 return;
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001136 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001137
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02001138 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02001139 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_DL);
Wanpeng Li594dd292016-04-22 17:07:24 +08001140
Dario Faggioliaab03e02013-11-28 11:14:43 +01001141 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
Dario Faggioli239be4a2013-11-07 14:43:39 +01001150 sched_rt_avg_update(rq, delta_exec);
1151
Luca Abeni2d4283e2017-05-18 22:13:33 +02001152 if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM))
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001153 delta_exec = grub_reclaim(delta_exec, rq, &curr->dl);
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001154 dl_se->runtime -= delta_exec;
1155
1156throttle:
1157 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
Peter Zijlstra1019a352014-11-26 08:44:03 +08001158 dl_se->dl_throttled = 1;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001159 __dequeue_task_dl(rq, curr, 0);
Peter Zijlstraa649f232015-06-11 14:46:49 +02001160 if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
Dario Faggioliaab03e02013-11-28 11:14:43 +01001161 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
1162
1163 if (!is_leftmost(curr, &rq->dl))
Kirill Tkhai88751252014-06-29 00:03:57 +04001164 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001165 }
Peter Zijlstra17248132013-12-17 12:44:49 +01001166
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);
Peter Zijlstra17248132013-12-17 12:44:49 +01001182 /*
1183 * We'll let actual RT tasks worry about the overflow here, we
Juri Lellifaa59932014-02-21 11:37:15 +01001184 * have our own CBS to keep us inline; only account when RT
1185 * bandwidth is relevant.
Peter Zijlstra17248132013-12-17 12:44:49 +01001186 */
Juri Lellifaa59932014-02-21 11:37:15 +01001187 if (sched_rt_bandwidth_account(rt_rq))
1188 rt_rq->rt_time += delta_exec;
Peter Zijlstra17248132013-12-17 12:44:49 +01001189 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1190 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001191}
1192
Luca Abeni209a0cb2017-05-18 22:13:29 +02001193static 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) {
Luca Abeni387e3132017-05-18 22:13:30 +02001205 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1206
Luca Abeni209a0cb2017-05-18 22:13:29 +02001207 if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
1208 sub_running_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl));
Luca Abeni8fd27232017-05-18 22:13:34 +02001209 sub_rq_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl));
Luca Abeni209a0cb2017-05-18 22:13:29 +02001210 dl_se->dl_non_contending = 0;
1211 }
Luca Abeni387e3132017-05-18 22:13:30 +02001212
1213 raw_spin_lock(&dl_b->lock);
Luca Abenidaec5792017-05-18 22:13:36 +02001214 __dl_clear(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
Luca Abeni387e3132017-05-18 22:13:30 +02001215 raw_spin_unlock(&dl_b->lock);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001216 __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
Juri Lelli1baca4c2013-11-07 14:43:38 +01001243#ifdef CONFIG_SMP
1244
Juri Lelli1baca4c2013-11-07 14:43:38 +01001245static 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)) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001251 dl_rq->earliest_dl.curr = deadline;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001252 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001253 }
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;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001267 cpudl_clear(&rq->rd->cpudl, rq->cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001268 } 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;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001274 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001275 }
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++;
Kirill Tkhai72465442014-05-09 03:00:14 +04001293 add_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001294
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--;
Kirill Tkhai72465442014-05-09 03:00:14 +04001307 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001308
1309 dec_dl_deadline(dl_rq, dl_se->deadline);
1310 dec_dl_migration(dl_se, dl_rq);
1311}
1312
Dario Faggioliaab03e02013-11-28 11:14:43 +01001313static 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
Juri Lelli1baca4c2013-11-07 14:43:38 +01001340 inc_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001341}
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
Juri Lelli1baca4c2013-11-07 14:43:38 +01001360 dec_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001361}
1362
1363static void
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001364enqueue_dl_entity(struct sched_dl_entity *dl_se,
1365 struct sched_dl_entity *pi_se, int flags)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001366{
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 */
Luca Abenie36d8672017-05-18 22:13:28 +02001374 if (flags & ENQUEUE_WAKEUP) {
Luca Abeni8fd27232017-05-18 22:13:34 +02001375 task_contending(dl_se, flags);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001376 update_dl_entity(dl_se, pi_se);
Luca Abenie36d8672017-05-18 22:13:28 +02001377 } else if (flags & ENQUEUE_REPLENISH) {
Luca Abeni6a503c32014-12-17 11:50:31 +01001378 replenish_dl_entity(dl_se, pi_se);
Luca Abenie36d8672017-05-18 22:13:28 +02001379 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001380
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{
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001391 struct task_struct *pi_task = rt_mutex_get_top_task(p);
1392 struct sched_dl_entity *pi_se = &p->dl;
1393
1394 /*
1395 * Use the scheduling parameters of the top pi-waiter
Andrea Parriff277d42015-08-05 15:56:19 +02001396 * task if we have one and its (absolute) deadline is
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001397 * smaller than our one... OTW we keep our runtime and
1398 * deadline.
1399 */
Juri Lelli64be6f12014-10-24 10:16:37 +01001400 if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001401 pi_se = &pi_task->dl;
Juri Lelli64be6f12014-10-24 10:16:37 +01001402 } else if (!dl_prio(p->normal_prio)) {
1403 /*
1404 * Special case in which we have a !SCHED_DEADLINE task
1405 * that is going to be deboosted, but exceedes its
1406 * runtime while doing so. No point in replenishing
1407 * it, as it's going to return back to its original
1408 * scheduling class after this.
1409 */
1410 BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
1411 return;
1412 }
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001413
Dario Faggioliaab03e02013-11-28 11:14:43 +01001414 /*
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001415 * Check if a constrained deadline task was activated
1416 * after the deadline but before the next period.
1417 * If that is the case, the task will be throttled and
1418 * the replenishment timer will be set to the next period.
1419 */
Daniel Bristot de Oliveira3effcb42017-05-29 16:24:03 +02001420 if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl))
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001421 dl_check_constrained_dl(&p->dl);
1422
Luca Abeni8fd27232017-05-18 22:13:34 +02001423 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) {
1424 add_rq_bw(p->dl.dl_bw, &rq->dl);
Luca Abenie36d8672017-05-18 22:13:28 +02001425 add_running_bw(p->dl.dl_bw, &rq->dl);
Luca Abeni8fd27232017-05-18 22:13:34 +02001426 }
Luca Abenie36d8672017-05-18 22:13:28 +02001427
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001428 /*
Luca Abenie36d8672017-05-18 22:13:28 +02001429 * If p is throttled, we do not enqueue it. In fact, if it exhausted
Dario Faggioliaab03e02013-11-28 11:14:43 +01001430 * its budget it needs a replenishment and, since it now is on
1431 * its rq, the bandwidth timer callback (which clearly has not
1432 * run yet) will take care of this.
Luca Abenie36d8672017-05-18 22:13:28 +02001433 * However, the active utilization does not depend on the fact
1434 * that the task is on the runqueue or not (but depends on the
1435 * task's state - in GRUB parlance, "inactive" vs "active contending").
1436 * In other words, even if a task is throttled its utilization must
1437 * be counted in the active utilization; hence, we need to call
1438 * add_running_bw().
Dario Faggioliaab03e02013-11-28 11:14:43 +01001439 */
Luca Abenie36d8672017-05-18 22:13:28 +02001440 if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
Luca Abeni209a0cb2017-05-18 22:13:29 +02001441 if (flags & ENQUEUE_WAKEUP)
Luca Abeni8fd27232017-05-18 22:13:34 +02001442 task_contending(&p->dl, flags);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001443
Dario Faggioliaab03e02013-11-28 11:14:43 +01001444 return;
Luca Abenie36d8672017-05-18 22:13:28 +02001445 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001446
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001447 enqueue_dl_entity(&p->dl, pi_se, flags);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001448
Ingo Molnar4b53a342017-02-05 15:41:03 +01001449 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001450 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001451}
1452
1453static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1454{
1455 dequeue_dl_entity(&p->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001456 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001457}
1458
1459static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1460{
1461 update_curr_dl(rq);
1462 __dequeue_task_dl(rq, p, flags);
Luca Abenie36d8672017-05-18 22:13:28 +02001463
Luca Abeni8fd27232017-05-18 22:13:34 +02001464 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) {
Luca Abenie36d8672017-05-18 22:13:28 +02001465 sub_running_bw(p->dl.dl_bw, &rq->dl);
Luca Abeni8fd27232017-05-18 22:13:34 +02001466 sub_rq_bw(p->dl.dl_bw, &rq->dl);
1467 }
Luca Abenie36d8672017-05-18 22:13:28 +02001468
1469 /*
Luca Abeni209a0cb2017-05-18 22:13:29 +02001470 * This check allows to start the inactive timer (or to immediately
1471 * decrease the active utilization, if needed) in two cases:
Luca Abenie36d8672017-05-18 22:13:28 +02001472 * when the task blocks and when it is terminating
1473 * (p->state == TASK_DEAD). We can handle the two cases in the same
1474 * way, because from GRUB's point of view the same thing is happening
1475 * (the task moves from "active contending" to "active non contending"
1476 * or "inactive")
1477 */
1478 if (flags & DEQUEUE_SLEEP)
Luca Abeni209a0cb2017-05-18 22:13:29 +02001479 task_non_contending(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001480}
1481
1482/*
1483 * Yield task semantic for -deadline tasks is:
1484 *
1485 * get off from the CPU until our next instance, with
1486 * a new runtime. This is of little use now, since we
1487 * don't have a bandwidth reclaiming mechanism. Anyway,
1488 * bandwidth reclaiming is planned for the future, and
1489 * yield_task_dl will indicate that some spare budget
1490 * is available for other task instances to use it.
1491 */
1492static void yield_task_dl(struct rq *rq)
1493{
Dario Faggioliaab03e02013-11-28 11:14:43 +01001494 /*
1495 * We make the task go to sleep until its current deadline by
1496 * forcing its runtime to zero. This way, update_curr_dl() stops
1497 * it and the bandwidth timer will wake it up and will give it
Juri Lelli5bfd1262014-04-15 13:49:04 +02001498 * new scheduling parameters (thanks to dl_yielded=1).
Dario Faggioliaab03e02013-11-28 11:14:43 +01001499 */
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001500 rq->curr->dl.dl_yielded = 1;
1501
Kirill Tkhai6f1607f2015-02-04 12:09:32 +03001502 update_rq_clock(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001503 update_curr_dl(rq);
Wanpeng Li44fb0852015-03-10 12:20:00 +08001504 /*
1505 * Tell update_rq_clock() that we've just updated,
1506 * so we don't do microscopic update in schedule()
1507 * and double the fastpath cost.
1508 */
1509 rq_clock_skip_update(rq, true);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001510}
1511
Juri Lelli1baca4c2013-11-07 14:43:38 +01001512#ifdef CONFIG_SMP
1513
1514static int find_later_rq(struct task_struct *task);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001515
1516static int
1517select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1518{
1519 struct task_struct *curr;
1520 struct rq *rq;
1521
Wanpeng Li1d7e9742014-10-14 10:22:39 +08001522 if (sd_flag != SD_BALANCE_WAKE)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001523 goto out;
1524
1525 rq = cpu_rq(cpu);
1526
1527 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07001528 curr = READ_ONCE(rq->curr); /* unlocked access */
Juri Lelli1baca4c2013-11-07 14:43:38 +01001529
1530 /*
1531 * If we are dealing with a -deadline task, we must
1532 * decide where to wake it up.
1533 * If it has a later deadline and the current task
1534 * on this rq can't move (provided the waking task
1535 * can!) we prefer to send it somewhere else. On the
1536 * other hand, if it has a shorter deadline, we
1537 * try to make it stay here, it might be important.
1538 */
1539 if (unlikely(dl_task(curr)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001540 (curr->nr_cpus_allowed < 2 ||
Juri Lelli1baca4c2013-11-07 14:43:38 +01001541 !dl_entity_preempt(&p->dl, &curr->dl)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001542 (p->nr_cpus_allowed > 1)) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001543 int target = find_later_rq(p);
1544
Wanpeng Li9d514262015-05-13 14:01:03 +08001545 if (target != -1 &&
Luca Abeni5aa50502015-10-16 10:06:21 +02001546 (dl_time_before(p->dl.deadline,
1547 cpu_rq(target)->dl.earliest_dl.curr) ||
1548 (cpu_rq(target)->dl.dl_nr_running == 0)))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001549 cpu = target;
1550 }
1551 rcu_read_unlock();
1552
1553out:
1554 return cpu;
1555}
1556
Luca Abeni209a0cb2017-05-18 22:13:29 +02001557static void migrate_task_rq_dl(struct task_struct *p)
1558{
1559 struct rq *rq;
1560
Luca Abeni8fd27232017-05-18 22:13:34 +02001561 if (p->state != TASK_WAKING)
Luca Abeni209a0cb2017-05-18 22:13:29 +02001562 return;
1563
1564 rq = task_rq(p);
1565 /*
1566 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1567 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1568 * rq->lock is not... So, lock it
1569 */
1570 raw_spin_lock(&rq->lock);
Luca Abeni8fd27232017-05-18 22:13:34 +02001571 if (p->dl.dl_non_contending) {
1572 sub_running_bw(p->dl.dl_bw, &rq->dl);
1573 p->dl.dl_non_contending = 0;
1574 /*
1575 * If the timer handler is currently running and the
1576 * timer cannot be cancelled, inactive_task_timer()
1577 * will see that dl_not_contending is not set, and
1578 * will not touch the rq's active utilization,
1579 * so we are still safe.
1580 */
1581 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1582 put_task_struct(p);
1583 }
1584 sub_rq_bw(p->dl.dl_bw, &rq->dl);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001585 raw_spin_unlock(&rq->lock);
1586}
1587
Juri Lelli1baca4c2013-11-07 14:43:38 +01001588static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1589{
1590 /*
1591 * Current can't be migrated, useless to reschedule,
1592 * let's hope p can move out.
1593 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001594 if (rq->curr->nr_cpus_allowed == 1 ||
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001595 cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001596 return;
1597
1598 /*
1599 * p is migratable, so let's not schedule it and
1600 * see if it is pushed or pulled somewhere else.
1601 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001602 if (p->nr_cpus_allowed != 1 &&
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001603 cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001604 return;
1605
Kirill Tkhai88751252014-06-29 00:03:57 +04001606 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001607}
1608
1609#endif /* CONFIG_SMP */
1610
Dario Faggioliaab03e02013-11-28 11:14:43 +01001611/*
1612 * Only called when both the current and waking task are -deadline
1613 * tasks.
1614 */
1615static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1616 int flags)
1617{
Juri Lelli1baca4c2013-11-07 14:43:38 +01001618 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001619 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001620 return;
1621 }
1622
1623#ifdef CONFIG_SMP
1624 /*
1625 * In the unlikely case current and p have the same deadline
1626 * let us try to decide what's the best thing to do...
1627 */
Dario Faggioli332ac172013-11-07 14:43:45 +01001628 if ((p->dl.deadline == rq->curr->dl.deadline) &&
1629 !test_tsk_need_resched(rq->curr))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001630 check_preempt_equal_dl(rq, p);
1631#endif /* CONFIG_SMP */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001632}
1633
1634#ifdef CONFIG_SCHED_HRTICK
1635static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1636{
xiaofeng.yan177ef2a2014-08-26 03:15:41 +00001637 hrtick_start(rq, p->dl.runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001638}
Wanpeng Li36ce9882014-11-11 09:52:26 +08001639#else /* !CONFIG_SCHED_HRTICK */
1640static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1641{
1642}
Dario Faggioliaab03e02013-11-28 11:14:43 +01001643#endif
1644
1645static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1646 struct dl_rq *dl_rq)
1647{
1648 struct rb_node *left = dl_rq->rb_leftmost;
1649
1650 if (!left)
1651 return NULL;
1652
1653 return rb_entry(left, struct sched_dl_entity, rb_node);
1654}
1655
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001656struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01001657pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001658{
1659 struct sched_dl_entity *dl_se;
1660 struct task_struct *p;
1661 struct dl_rq *dl_rq;
1662
1663 dl_rq = &rq->dl;
1664
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001665 if (need_pull_dl_task(rq, prev)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001666 /*
1667 * This is OK, because current is on_cpu, which avoids it being
1668 * picked for load-balance and preemption/IRQs are still
1669 * disabled avoiding further scheduler activity on it and we're
1670 * being very careful to re-start the picking loop.
1671 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01001672 rq_unpin_lock(rq, rf);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001673 pull_dl_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01001674 rq_repin_lock(rq, rf);
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001675 /*
T.Zhou176cedc2016-11-23 08:48:32 +08001676 * pull_dl_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001677 * means a stop task can slip in, in which case we need to
1678 * re-start task selection.
1679 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001680 if (rq->stop && task_on_rq_queued(rq->stop))
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001681 return RETRY_TASK;
1682 }
1683
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001684 /*
1685 * When prev is DL, we may throttle it in put_prev_task().
1686 * So, we update time before we check for dl_nr_running.
1687 */
1688 if (prev->sched_class == &dl_sched_class)
1689 update_curr_dl(rq);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001690
Dario Faggioliaab03e02013-11-28 11:14:43 +01001691 if (unlikely(!dl_rq->dl_nr_running))
1692 return NULL;
1693
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001694 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001695
Dario Faggioliaab03e02013-11-28 11:14:43 +01001696 dl_se = pick_next_dl_entity(rq, dl_rq);
1697 BUG_ON(!dl_se);
1698
1699 p = dl_task_of(dl_se);
1700 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001701
1702 /* Running task will never be pushed. */
Juri Lelli71362652014-01-14 12:03:51 +01001703 dequeue_pushable_dl_task(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001704
Dario Faggioliaab03e02013-11-28 11:14:43 +01001705 if (hrtick_enabled(rq))
1706 start_hrtick_dl(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001707
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02001708 queue_push_tasks(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001709
Dario Faggioliaab03e02013-11-28 11:14:43 +01001710 return p;
1711}
1712
1713static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1714{
1715 update_curr_dl(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001716
Ingo Molnar4b53a342017-02-05 15:41:03 +01001717 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001718 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001719}
1720
1721static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1722{
1723 update_curr_dl(rq);
1724
Wanpeng Lia7bebf42014-11-26 08:44:01 +08001725 /*
1726 * Even when we have runtime, update_curr_dl() might have resulted in us
1727 * not being the leftmost task anymore. In that case NEED_RESCHED will
1728 * be set and schedule() will start a new hrtick for the next task.
1729 */
1730 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
1731 is_leftmost(p, &rq->dl))
Dario Faggioliaab03e02013-11-28 11:14:43 +01001732 start_hrtick_dl(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001733}
1734
1735static void task_fork_dl(struct task_struct *p)
1736{
1737 /*
1738 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1739 * sched_fork()
1740 */
1741}
1742
Dario Faggioliaab03e02013-11-28 11:14:43 +01001743static void set_curr_task_dl(struct rq *rq)
1744{
1745 struct task_struct *p = rq->curr;
1746
1747 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001748
1749 /* You can't push away the running task */
1750 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001751}
1752
Juri Lelli1baca4c2013-11-07 14:43:38 +01001753#ifdef CONFIG_SMP
1754
1755/* Only try algorithms three times */
1756#define DL_MAX_TRIES 3
1757
1758static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1759{
1760 if (!task_running(rq, p) &&
Ingo Molnar0c98d342017-02-05 15:38:10 +01001761 cpumask_test_cpu(cpu, &p->cpus_allowed))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001762 return 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001763 return 0;
1764}
1765
Wanpeng Li8b5e7702015-05-13 14:01:01 +08001766/*
1767 * Return the earliest pushable rq's task, which is suitable to be executed
1768 * on the CPU, NULL otherwise:
1769 */
1770static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
1771{
1772 struct rb_node *next_node = rq->dl.pushable_dl_tasks_leftmost;
1773 struct task_struct *p = NULL;
1774
1775 if (!has_pushable_dl_tasks(rq))
1776 return NULL;
1777
1778next_node:
1779 if (next_node) {
1780 p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1781
1782 if (pick_dl_task(rq, p, cpu))
1783 return p;
1784
1785 next_node = rb_next(next_node);
1786 goto next_node;
1787 }
1788
1789 return NULL;
1790}
1791
Juri Lelli1baca4c2013-11-07 14:43:38 +01001792static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1793
1794static int find_later_rq(struct task_struct *task)
1795{
1796 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001797 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001798 int this_cpu = smp_processor_id();
1799 int best_cpu, cpu = task_cpu(task);
1800
1801 /* Make sure the mask is initialized first */
1802 if (unlikely(!later_mask))
1803 return -1;
1804
Ingo Molnar4b53a342017-02-05 15:41:03 +01001805 if (task->nr_cpus_allowed == 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001806 return -1;
1807
Juri Lelli91ec6772014-09-19 10:22:41 +01001808 /*
1809 * We have to consider system topology and task affinity
1810 * first, then we can look for a suitable cpu.
1811 */
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001812 best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
1813 task, later_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001814 if (best_cpu == -1)
1815 return -1;
1816
1817 /*
1818 * If we are here, some target has been found,
1819 * the most suitable of which is cached in best_cpu.
1820 * This is, among the runqueues where the current tasks
1821 * have later deadlines than the task's one, the rq
1822 * with the latest possible one.
1823 *
1824 * Now we check how well this matches with task's
1825 * affinity and system topology.
1826 *
1827 * The last cpu where the task run is our first
1828 * guess, since it is most likely cache-hot there.
1829 */
1830 if (cpumask_test_cpu(cpu, later_mask))
1831 return cpu;
1832 /*
1833 * Check if this_cpu is to be skipped (i.e., it is
1834 * not in the mask) or not.
1835 */
1836 if (!cpumask_test_cpu(this_cpu, later_mask))
1837 this_cpu = -1;
1838
1839 rcu_read_lock();
1840 for_each_domain(cpu, sd) {
1841 if (sd->flags & SD_WAKE_AFFINE) {
1842
1843 /*
1844 * If possible, preempting this_cpu is
1845 * cheaper than migrating.
1846 */
1847 if (this_cpu != -1 &&
1848 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1849 rcu_read_unlock();
1850 return this_cpu;
1851 }
1852
1853 /*
1854 * Last chance: if best_cpu is valid and is
1855 * in the mask, that becomes our choice.
1856 */
1857 if (best_cpu < nr_cpu_ids &&
1858 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
1859 rcu_read_unlock();
1860 return best_cpu;
1861 }
1862 }
1863 }
1864 rcu_read_unlock();
1865
1866 /*
1867 * At this point, all our guesses failed, we just return
1868 * 'something', and let the caller sort the things out.
1869 */
1870 if (this_cpu != -1)
1871 return this_cpu;
1872
1873 cpu = cpumask_any(later_mask);
1874 if (cpu < nr_cpu_ids)
1875 return cpu;
1876
1877 return -1;
1878}
1879
1880/* Locks the rq it finds */
1881static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1882{
1883 struct rq *later_rq = NULL;
1884 int tries;
1885 int cpu;
1886
1887 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1888 cpu = find_later_rq(task);
1889
1890 if ((cpu == -1) || (cpu == rq->cpu))
1891 break;
1892
1893 later_rq = cpu_rq(cpu);
1894
Luca Abeni5aa50502015-10-16 10:06:21 +02001895 if (later_rq->dl.dl_nr_running &&
1896 !dl_time_before(task->dl.deadline,
Wanpeng Li9d514262015-05-13 14:01:03 +08001897 later_rq->dl.earliest_dl.curr)) {
1898 /*
1899 * Target rq has tasks of equal or earlier deadline,
1900 * retrying does not release any lock and is unlikely
1901 * to yield a different result.
1902 */
1903 later_rq = NULL;
1904 break;
1905 }
1906
Juri Lelli1baca4c2013-11-07 14:43:38 +01001907 /* Retry if something changed. */
1908 if (double_lock_balance(rq, later_rq)) {
1909 if (unlikely(task_rq(task) != rq ||
Ingo Molnar0c98d342017-02-05 15:38:10 +01001910 !cpumask_test_cpu(later_rq->cpu, &task->cpus_allowed) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001911 task_running(rq, task) ||
Xunlei Pang13b5ab02016-05-09 12:11:31 +08001912 !dl_task(task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001913 !task_on_rq_queued(task))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001914 double_unlock_balance(rq, later_rq);
1915 later_rq = NULL;
1916 break;
1917 }
1918 }
1919
1920 /*
1921 * If the rq we found has no -deadline task, or
1922 * its earliest one has a later deadline than our
1923 * task, the rq is a good one.
1924 */
1925 if (!later_rq->dl.dl_nr_running ||
1926 dl_time_before(task->dl.deadline,
1927 later_rq->dl.earliest_dl.curr))
1928 break;
1929
1930 /* Otherwise we try again. */
1931 double_unlock_balance(rq, later_rq);
1932 later_rq = NULL;
1933 }
1934
1935 return later_rq;
1936}
1937
1938static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1939{
1940 struct task_struct *p;
1941
1942 if (!has_pushable_dl_tasks(rq))
1943 return NULL;
1944
1945 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
1946 struct task_struct, pushable_dl_tasks);
1947
1948 BUG_ON(rq->cpu != task_cpu(p));
1949 BUG_ON(task_current(rq, p));
Ingo Molnar4b53a342017-02-05 15:41:03 +01001950 BUG_ON(p->nr_cpus_allowed <= 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001951
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001952 BUG_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001953 BUG_ON(!dl_task(p));
1954
1955 return p;
1956}
1957
1958/*
1959 * See if the non running -deadline tasks on this rq
1960 * can be sent to some other CPU where they can preempt
1961 * and start executing.
1962 */
1963static int push_dl_task(struct rq *rq)
1964{
1965 struct task_struct *next_task;
1966 struct rq *later_rq;
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08001967 int ret = 0;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001968
1969 if (!rq->dl.overloaded)
1970 return 0;
1971
1972 next_task = pick_next_pushable_dl_task(rq);
1973 if (!next_task)
1974 return 0;
1975
1976retry:
1977 if (unlikely(next_task == rq->curr)) {
1978 WARN_ON(1);
1979 return 0;
1980 }
1981
1982 /*
1983 * If next_task preempts rq->curr, and rq->curr
1984 * can move away, it makes sense to just reschedule
1985 * without going further in pushing next_task.
1986 */
1987 if (dl_task(rq->curr) &&
1988 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001989 rq->curr->nr_cpus_allowed > 1) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001990 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001991 return 0;
1992 }
1993
1994 /* We might release rq lock */
1995 get_task_struct(next_task);
1996
1997 /* Will lock the rq it'll find */
1998 later_rq = find_lock_later_rq(next_task, rq);
1999 if (!later_rq) {
2000 struct task_struct *task;
2001
2002 /*
2003 * We must check all this again, since
2004 * find_lock_later_rq releases rq->lock and it is
2005 * then possible that next_task has migrated.
2006 */
2007 task = pick_next_pushable_dl_task(rq);
Byungchul Parka776b962017-05-12 10:05:59 +09002008 if (task == next_task) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01002009 /*
2010 * The task is still there. We don't try
2011 * again, some other cpu will pull it when ready.
2012 */
Juri Lelli1baca4c2013-11-07 14:43:38 +01002013 goto out;
2014 }
2015
2016 if (!task)
2017 /* No more tasks */
2018 goto out;
2019
2020 put_task_struct(next_task);
2021 next_task = task;
2022 goto retry;
2023 }
2024
2025 deactivate_task(rq, next_task, 0);
Luca Abenie36d8672017-05-18 22:13:28 +02002026 sub_running_bw(next_task->dl.dl_bw, &rq->dl);
Luca Abeni8fd27232017-05-18 22:13:34 +02002027 sub_rq_bw(next_task->dl.dl_bw, &rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002028 set_task_cpu(next_task, later_rq->cpu);
Luca Abeni8fd27232017-05-18 22:13:34 +02002029 add_rq_bw(next_task->dl.dl_bw, &later_rq->dl);
Luca Abenie36d8672017-05-18 22:13:28 +02002030 add_running_bw(next_task->dl.dl_bw, &later_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002031 activate_task(later_rq, next_task, 0);
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08002032 ret = 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002033
Kirill Tkhai88751252014-06-29 00:03:57 +04002034 resched_curr(later_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002035
2036 double_unlock_balance(rq, later_rq);
2037
2038out:
2039 put_task_struct(next_task);
2040
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08002041 return ret;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002042}
2043
2044static void push_dl_tasks(struct rq *rq)
2045{
Andrea Parri4ffa08e2015-08-05 15:56:18 +02002046 /* push_dl_task() will return true if it moved a -deadline task */
Juri Lelli1baca4c2013-11-07 14:43:38 +01002047 while (push_dl_task(rq))
2048 ;
2049}
2050
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002051static void pull_dl_task(struct rq *this_rq)
Juri Lelli1baca4c2013-11-07 14:43:38 +01002052{
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002053 int this_cpu = this_rq->cpu, cpu;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002054 struct task_struct *p;
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002055 bool resched = false;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002056 struct rq *src_rq;
2057 u64 dmin = LONG_MAX;
2058
2059 if (likely(!dl_overloaded(this_rq)))
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002060 return;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002061
2062 /*
2063 * Match the barrier from dl_set_overloaded; this guarantees that if we
2064 * see overloaded we must also see the dlo_mask bit.
2065 */
2066 smp_rmb();
2067
2068 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2069 if (this_cpu == cpu)
2070 continue;
2071
2072 src_rq = cpu_rq(cpu);
2073
2074 /*
2075 * It looks racy, abd it is! However, as in sched_rt.c,
2076 * we are fine with this.
2077 */
2078 if (this_rq->dl.dl_nr_running &&
2079 dl_time_before(this_rq->dl.earliest_dl.curr,
2080 src_rq->dl.earliest_dl.next))
2081 continue;
2082
2083 /* Might drop this_rq->lock */
2084 double_lock_balance(this_rq, src_rq);
2085
2086 /*
2087 * If there are no more pullable tasks on the
2088 * rq, we're done with it.
2089 */
2090 if (src_rq->dl.dl_nr_running <= 1)
2091 goto skip;
2092
Wanpeng Li8b5e7702015-05-13 14:01:01 +08002093 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002094
2095 /*
2096 * We found a task to be pulled if:
2097 * - it preempts our current (if there's one),
2098 * - it will preempt the last one we pulled (if any).
2099 */
2100 if (p && dl_time_before(p->dl.deadline, dmin) &&
2101 (!this_rq->dl.dl_nr_running ||
2102 dl_time_before(p->dl.deadline,
2103 this_rq->dl.earliest_dl.curr))) {
2104 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002105 WARN_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01002106
2107 /*
2108 * Then we pull iff p has actually an earlier
2109 * deadline than the current task of its runqueue.
2110 */
2111 if (dl_time_before(p->dl.deadline,
2112 src_rq->curr->dl.deadline))
2113 goto skip;
2114
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002115 resched = true;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002116
2117 deactivate_task(src_rq, p, 0);
Luca Abenie36d8672017-05-18 22:13:28 +02002118 sub_running_bw(p->dl.dl_bw, &src_rq->dl);
Luca Abeni8fd27232017-05-18 22:13:34 +02002119 sub_rq_bw(p->dl.dl_bw, &src_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002120 set_task_cpu(p, this_cpu);
Luca Abeni8fd27232017-05-18 22:13:34 +02002121 add_rq_bw(p->dl.dl_bw, &this_rq->dl);
Luca Abenie36d8672017-05-18 22:13:28 +02002122 add_running_bw(p->dl.dl_bw, &this_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002123 activate_task(this_rq, p, 0);
2124 dmin = p->dl.deadline;
2125
2126 /* Is there any other task even earlier? */
2127 }
2128skip:
2129 double_unlock_balance(this_rq, src_rq);
2130 }
2131
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002132 if (resched)
2133 resched_curr(this_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002134}
2135
2136/*
2137 * Since the task is not running and a reschedule is not going to happen
2138 * anytime soon on its runqueue, we try pushing it away now.
2139 */
2140static void task_woken_dl(struct rq *rq, struct task_struct *p)
2141{
2142 if (!task_running(rq, p) &&
2143 !test_tsk_need_resched(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01002144 p->nr_cpus_allowed > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01002145 dl_task(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01002146 (rq->curr->nr_cpus_allowed < 2 ||
Wanpeng Li6b0a5632014-10-31 06:39:34 +08002147 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01002148 push_dl_tasks(rq);
2149 }
2150}
2151
2152static void set_cpus_allowed_dl(struct task_struct *p,
2153 const struct cpumask *new_mask)
2154{
Juri Lelli7f514122014-09-19 10:22:40 +01002155 struct root_domain *src_rd;
Peter Zijlstra6c370672015-05-15 17:43:36 +02002156 struct rq *rq;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002157
2158 BUG_ON(!dl_task(p));
2159
Juri Lelli7f514122014-09-19 10:22:40 +01002160 rq = task_rq(p);
2161 src_rd = rq->rd;
2162 /*
2163 * Migrating a SCHED_DEADLINE task between exclusive
2164 * cpusets (different root_domains) entails a bandwidth
2165 * update. We already made space for us in the destination
2166 * domain (see cpuset_can_attach()).
2167 */
2168 if (!cpumask_intersects(src_rd->span, new_mask)) {
2169 struct dl_bw *src_dl_b;
2170
2171 src_dl_b = dl_bw_of(cpu_of(rq));
2172 /*
2173 * We now free resources of the root_domain we are migrating
2174 * off. In the worst case, sched_setattr() may temporary fail
2175 * until we complete the update.
2176 */
2177 raw_spin_lock(&src_dl_b->lock);
Luca Abenidaec5792017-05-18 22:13:36 +02002178 __dl_clear(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
Juri Lelli7f514122014-09-19 10:22:40 +01002179 raw_spin_unlock(&src_dl_b->lock);
2180 }
2181
Peter Zijlstra6c370672015-05-15 17:43:36 +02002182 set_cpus_allowed_common(p, new_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002183}
2184
2185/* Assumes rq->lock is held */
2186static void rq_online_dl(struct rq *rq)
2187{
2188 if (rq->dl.overloaded)
2189 dl_set_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002190
Xunlei Pang16b26942015-01-19 04:49:36 +00002191 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002192 if (rq->dl.dl_nr_running > 0)
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02002193 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002194}
2195
2196/* Assumes rq->lock is held */
2197static void rq_offline_dl(struct rq *rq)
2198{
2199 if (rq->dl.overloaded)
2200 dl_clear_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002201
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02002202 cpudl_clear(&rq->rd->cpudl, rq->cpu);
Xunlei Pang16b26942015-01-19 04:49:36 +00002203 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002204}
2205
Wanpeng Lia6c0e742015-05-13 14:01:02 +08002206void __init init_sched_dl_class(void)
Juri Lelli1baca4c2013-11-07 14:43:38 +01002207{
2208 unsigned int i;
2209
2210 for_each_possible_cpu(i)
2211 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2212 GFP_KERNEL, cpu_to_node(i));
2213}
2214
2215#endif /* CONFIG_SMP */
2216
Dario Faggioliaab03e02013-11-28 11:14:43 +01002217static void switched_from_dl(struct rq *rq, struct task_struct *p)
2218{
Peter Zijlstraa649f232015-06-11 14:46:49 +02002219 /*
Luca Abeni209a0cb2017-05-18 22:13:29 +02002220 * task_non_contending() can start the "inactive timer" (if the 0-lag
2221 * time is in the future). If the task switches back to dl before
2222 * the "inactive timer" fires, it can continue to consume its current
2223 * runtime using its current deadline. If it stays outside of
2224 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2225 * will reset the task parameters.
Peter Zijlstraa649f232015-06-11 14:46:49 +02002226 */
Luca Abeni209a0cb2017-05-18 22:13:29 +02002227 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2228 task_non_contending(p);
2229
Luca Abeni8fd27232017-05-18 22:13:34 +02002230 if (!task_on_rq_queued(p))
2231 sub_rq_bw(p->dl.dl_bw, &rq->dl);
2232
Luca Abeni209a0cb2017-05-18 22:13:29 +02002233 /*
2234 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2235 * at the 0-lag time, because the task could have been migrated
2236 * while SCHED_OTHER in the meanwhile.
2237 */
2238 if (p->dl.dl_non_contending)
2239 p->dl.dl_non_contending = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002240
Juri Lelli1baca4c2013-11-07 14:43:38 +01002241 /*
2242 * Since this might be the only -deadline task on the rq,
2243 * this is the right place to try to pull some other one
2244 * from an overloaded cpu, if any.
2245 */
Wanpeng Licd660912014-10-31 06:39:35 +08002246 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2247 return;
2248
Peter Zijlstra9916e212015-06-11 14:46:43 +02002249 queue_pull_task(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002250}
2251
Juri Lelli1baca4c2013-11-07 14:43:38 +01002252/*
2253 * When switching to -deadline, we may overload the rq, then
2254 * we try to push someone off, if possible.
2255 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002256static void switched_to_dl(struct rq *rq, struct task_struct *p)
2257{
Luca Abeni209a0cb2017-05-18 22:13:29 +02002258 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2259 put_task_struct(p);
Luca Abeni72f9f3f2016-03-07 12:27:04 +01002260
Juri Lelli98b0a852016-08-05 16:07:55 +01002261 /* If p is not queued we will update its parameters at next wakeup. */
Luca Abeni8fd27232017-05-18 22:13:34 +02002262 if (!task_on_rq_queued(p)) {
2263 add_rq_bw(p->dl.dl_bw, &rq->dl);
Juri Lelli98b0a852016-08-05 16:07:55 +01002264
Luca Abeni8fd27232017-05-18 22:13:34 +02002265 return;
2266 }
Juri Lelli98b0a852016-08-05 16:07:55 +01002267 /*
2268 * If p is boosted we already updated its params in
2269 * rt_mutex_setprio()->enqueue_task(..., ENQUEUE_REPLENISH),
2270 * p's deadline being now already after rq_clock(rq).
2271 */
2272 if (dl_time_before(p->dl.deadline, rq_clock(rq)))
2273 setup_new_dl_entity(&p->dl);
2274
2275 if (rq->curr != p) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01002276#ifdef CONFIG_SMP
Ingo Molnar4b53a342017-02-05 15:41:03 +01002277 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
Peter Zijlstra9916e212015-06-11 14:46:43 +02002278 queue_push_tasks(rq);
Sebastian Andrzej Siewior619bd4a2017-01-24 15:40:06 +01002279#endif
Peter Zijlstra9916e212015-06-11 14:46:43 +02002280 if (dl_task(rq->curr))
2281 check_preempt_curr_dl(rq, p, 0);
2282 else
2283 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002284 }
2285}
2286
Juri Lelli1baca4c2013-11-07 14:43:38 +01002287/*
2288 * If the scheduling parameters of a -deadline task changed,
2289 * a push or pull operation might be needed.
2290 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002291static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2292 int oldprio)
2293{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002294 if (task_on_rq_queued(p) || rq->curr == p) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002295#ifdef CONFIG_SMP
Juri Lelli1baca4c2013-11-07 14:43:38 +01002296 /*
2297 * This might be too much, but unfortunately
2298 * we don't have the old deadline value, and
2299 * we can't argue if the task is increasing
2300 * or lowering its prio, so...
2301 */
2302 if (!rq->dl.overloaded)
Peter Zijlstra9916e212015-06-11 14:46:43 +02002303 queue_pull_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002304
2305 /*
2306 * If we now have a earlier deadline task than p,
2307 * then reschedule, provided p is still on this
2308 * runqueue.
2309 */
Peter Zijlstra9916e212015-06-11 14:46:43 +02002310 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
Kirill Tkhai88751252014-06-29 00:03:57 +04002311 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002312#else
2313 /*
2314 * Again, we don't know if p has a earlier
2315 * or later deadline, so let's blindly set a
2316 * (maybe not needed) rescheduling point.
2317 */
Kirill Tkhai88751252014-06-29 00:03:57 +04002318 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002319#endif /* CONFIG_SMP */
Peter Zijlstra801ccdb2016-02-25 15:01:49 +01002320 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01002321}
Dario Faggioliaab03e02013-11-28 11:14:43 +01002322
2323const struct sched_class dl_sched_class = {
2324 .next = &rt_sched_class,
2325 .enqueue_task = enqueue_task_dl,
2326 .dequeue_task = dequeue_task_dl,
2327 .yield_task = yield_task_dl,
2328
2329 .check_preempt_curr = check_preempt_curr_dl,
2330
2331 .pick_next_task = pick_next_task_dl,
2332 .put_prev_task = put_prev_task_dl,
2333
2334#ifdef CONFIG_SMP
2335 .select_task_rq = select_task_rq_dl,
Luca Abeni209a0cb2017-05-18 22:13:29 +02002336 .migrate_task_rq = migrate_task_rq_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01002337 .set_cpus_allowed = set_cpus_allowed_dl,
2338 .rq_online = rq_online_dl,
2339 .rq_offline = rq_offline_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01002340 .task_woken = task_woken_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002341#endif
2342
2343 .set_curr_task = set_curr_task_dl,
2344 .task_tick = task_tick_dl,
2345 .task_fork = task_fork_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002346
2347 .prio_changed = prio_changed_dl,
2348 .switched_from = switched_from_dl,
2349 .switched_to = switched_to_dl,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002350
2351 .update_curr = update_curr_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002352};
Wanpeng Liacb32132014-10-31 06:39:33 +08002353
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04002354int sched_dl_global_validate(void)
2355{
2356 u64 runtime = global_rt_runtime();
2357 u64 period = global_rt_period();
2358 u64 new_bw = to_ratio(period, runtime);
2359 struct dl_bw *dl_b;
2360 int cpu, ret = 0;
2361 unsigned long flags;
2362
2363 /*
2364 * Here we want to check the bandwidth not being set to some
2365 * value smaller than the currently allocated bandwidth in
2366 * any of the root_domains.
2367 *
2368 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
2369 * cycling on root_domains... Discussion on different/better
2370 * solutions is welcome!
2371 */
2372 for_each_possible_cpu(cpu) {
2373 rcu_read_lock_sched();
2374 dl_b = dl_bw_of(cpu);
2375
2376 raw_spin_lock_irqsave(&dl_b->lock, flags);
2377 if (new_bw < dl_b->total_bw)
2378 ret = -EBUSY;
2379 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2380
2381 rcu_read_unlock_sched();
2382
2383 if (ret)
2384 break;
2385 }
2386
2387 return ret;
2388}
2389
2390void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
2391{
2392 if (global_rt_runtime() == RUNTIME_INF) {
2393 dl_rq->bw_ratio = 1 << RATIO_SHIFT;
2394 dl_rq->extra_bw = 1 << BW_SHIFT;
2395 } else {
2396 dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
2397 global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
2398 dl_rq->extra_bw = to_ratio(global_rt_period(),
2399 global_rt_runtime());
2400 }
2401}
2402
2403void sched_dl_do_global(void)
2404{
2405 u64 new_bw = -1;
2406 struct dl_bw *dl_b;
2407 int cpu;
2408 unsigned long flags;
2409
2410 def_dl_bandwidth.dl_period = global_rt_period();
2411 def_dl_bandwidth.dl_runtime = global_rt_runtime();
2412
2413 if (global_rt_runtime() != RUNTIME_INF)
2414 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
2415
2416 /*
2417 * FIXME: As above...
2418 */
2419 for_each_possible_cpu(cpu) {
2420 rcu_read_lock_sched();
2421 dl_b = dl_bw_of(cpu);
2422
2423 raw_spin_lock_irqsave(&dl_b->lock, flags);
2424 dl_b->bw = new_bw;
2425 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2426
2427 rcu_read_unlock_sched();
2428 init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
2429 }
2430}
2431
2432/*
2433 * We must be sure that accepting a new task (or allowing changing the
2434 * parameters of an existing one) is consistent with the bandwidth
2435 * constraints. If yes, this function also accordingly updates the currently
2436 * allocated bandwidth to reflect the new situation.
2437 *
2438 * This function is called while holding p's rq->lock.
2439 */
2440int sched_dl_overflow(struct task_struct *p, int policy,
2441 const struct sched_attr *attr)
2442{
2443 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
2444 u64 period = attr->sched_period ?: attr->sched_deadline;
2445 u64 runtime = attr->sched_runtime;
2446 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
2447 int cpus, err = -1;
2448
2449 /* !deadline task may carry old deadline bandwidth */
2450 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
2451 return 0;
2452
2453 /*
2454 * Either if a task, enters, leave, or stays -deadline but changes
2455 * its parameters, we may need to update accordingly the total
2456 * allocated bandwidth of the container.
2457 */
2458 raw_spin_lock(&dl_b->lock);
2459 cpus = dl_bw_cpus(task_cpu(p));
2460 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2461 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2462 if (hrtimer_active(&p->dl.inactive_timer))
2463 __dl_clear(dl_b, p->dl.dl_bw, cpus);
2464 __dl_add(dl_b, new_bw, cpus);
2465 err = 0;
2466 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2467 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2468 /*
2469 * XXX this is slightly incorrect: when the task
2470 * utilization decreases, we should delay the total
2471 * utilization change until the task's 0-lag point.
2472 * But this would require to set the task's "inactive
2473 * timer" when the task is not inactive.
2474 */
2475 __dl_clear(dl_b, p->dl.dl_bw, cpus);
2476 __dl_add(dl_b, new_bw, cpus);
2477 dl_change_utilization(p, new_bw);
2478 err = 0;
2479 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2480 /*
2481 * Do not decrease the total deadline utilization here,
2482 * switched_from_dl() will take care to do it at the correct
2483 * (0-lag) time.
2484 */
2485 err = 0;
2486 }
2487 raw_spin_unlock(&dl_b->lock);
2488
2489 return err;
2490}
2491
2492/*
2493 * This function initializes the sched_dl_entity of a newly becoming
2494 * SCHED_DEADLINE task.
2495 *
2496 * Only the static values are considered here, the actual runtime and the
2497 * absolute deadline will be properly calculated when the task is enqueued
2498 * for the first time with its new policy.
2499 */
2500void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
2501{
2502 struct sched_dl_entity *dl_se = &p->dl;
2503
2504 dl_se->dl_runtime = attr->sched_runtime;
2505 dl_se->dl_deadline = attr->sched_deadline;
2506 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
2507 dl_se->flags = attr->sched_flags;
2508 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
2509 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
2510}
2511
2512void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
2513{
2514 struct sched_dl_entity *dl_se = &p->dl;
2515
2516 attr->sched_priority = p->rt_priority;
2517 attr->sched_runtime = dl_se->dl_runtime;
2518 attr->sched_deadline = dl_se->dl_deadline;
2519 attr->sched_period = dl_se->dl_period;
2520 attr->sched_flags = dl_se->flags;
2521}
2522
2523/*
2524 * This function validates the new parameters of a -deadline task.
2525 * We ask for the deadline not being zero, and greater or equal
2526 * than the runtime, as well as the period of being zero or
2527 * greater than deadline. Furthermore, we have to be sure that
2528 * user parameters are above the internal resolution of 1us (we
2529 * check sched_runtime only since it is always the smaller one) and
2530 * below 2^63 ns (we have to check both sched_deadline and
2531 * sched_period, as the latter can be zero).
2532 */
2533bool __checkparam_dl(const struct sched_attr *attr)
2534{
2535 /* deadline != 0 */
2536 if (attr->sched_deadline == 0)
2537 return false;
2538
2539 /*
2540 * Since we truncate DL_SCALE bits, make sure we're at least
2541 * that big.
2542 */
2543 if (attr->sched_runtime < (1ULL << DL_SCALE))
2544 return false;
2545
2546 /*
2547 * Since we use the MSB for wrap-around and sign issues, make
2548 * sure it's not set (mind that period can be equal to zero).
2549 */
2550 if (attr->sched_deadline & (1ULL << 63) ||
2551 attr->sched_period & (1ULL << 63))
2552 return false;
2553
2554 /* runtime <= deadline <= period (if period != 0) */
2555 if ((attr->sched_period != 0 &&
2556 attr->sched_period < attr->sched_deadline) ||
2557 attr->sched_deadline < attr->sched_runtime)
2558 return false;
2559
2560 return true;
2561}
2562
2563/*
2564 * This function clears the sched_dl_entity static params.
2565 */
2566void __dl_clear_params(struct task_struct *p)
2567{
2568 struct sched_dl_entity *dl_se = &p->dl;
2569
2570 dl_se->dl_runtime = 0;
2571 dl_se->dl_deadline = 0;
2572 dl_se->dl_period = 0;
2573 dl_se->flags = 0;
2574 dl_se->dl_bw = 0;
2575 dl_se->dl_density = 0;
2576
2577 dl_se->dl_throttled = 0;
2578 dl_se->dl_yielded = 0;
2579 dl_se->dl_non_contending = 0;
2580}
2581
2582bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
2583{
2584 struct sched_dl_entity *dl_se = &p->dl;
2585
2586 if (dl_se->dl_runtime != attr->sched_runtime ||
2587 dl_se->dl_deadline != attr->sched_deadline ||
2588 dl_se->dl_period != attr->sched_period ||
2589 dl_se->flags != attr->sched_flags)
2590 return true;
2591
2592 return false;
2593}
2594
2595#ifdef CONFIG_SMP
2596int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed)
2597{
2598 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
2599 cs_cpus_allowed);
2600 struct dl_bw *dl_b;
2601 bool overflow;
2602 int cpus, ret;
2603 unsigned long flags;
2604
2605 rcu_read_lock_sched();
2606 dl_b = dl_bw_of(dest_cpu);
2607 raw_spin_lock_irqsave(&dl_b->lock, flags);
2608 cpus = dl_bw_cpus(dest_cpu);
2609 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
2610 if (overflow)
2611 ret = -EBUSY;
2612 else {
2613 /*
2614 * We reserve space for this task in the destination
2615 * root_domain, as we can't fail after this point.
2616 * We will free resources in the source root_domain
2617 * later on (see set_cpus_allowed_dl()).
2618 */
2619 __dl_add(dl_b, p->dl.dl_bw, cpus);
2620 ret = 0;
2621 }
2622 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2623 rcu_read_unlock_sched();
2624 return ret;
2625}
2626
2627int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
2628 const struct cpumask *trial)
2629{
2630 int ret = 1, trial_cpus;
2631 struct dl_bw *cur_dl_b;
2632 unsigned long flags;
2633
2634 rcu_read_lock_sched();
2635 cur_dl_b = dl_bw_of(cpumask_any(cur));
2636 trial_cpus = cpumask_weight(trial);
2637
2638 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
2639 if (cur_dl_b->bw != -1 &&
2640 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
2641 ret = 0;
2642 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
2643 rcu_read_unlock_sched();
2644 return ret;
2645}
2646
2647bool dl_cpu_busy(unsigned int cpu)
2648{
2649 unsigned long flags;
2650 struct dl_bw *dl_b;
2651 bool overflow;
2652 int cpus;
2653
2654 rcu_read_lock_sched();
2655 dl_b = dl_bw_of(cpu);
2656 raw_spin_lock_irqsave(&dl_b->lock, flags);
2657 cpus = dl_bw_cpus(cpu);
2658 overflow = __dl_overflow(dl_b, cpus, 0, 0);
2659 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2660 rcu_read_unlock_sched();
2661 return overflow;
2662}
2663#endif
2664
Wanpeng Liacb32132014-10-31 06:39:33 +08002665#ifdef CONFIG_SCHED_DEBUG
2666extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2667
2668void print_dl_stats(struct seq_file *m, int cpu)
2669{
2670 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
2671}
2672#endif /* CONFIG_SCHED_DEBUG */