blob: 6480a929417c74a3d669f0b9a946ee8115e05caf [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>
20
Dario Faggioli332ac172013-11-07 14:43:45 +010021struct dl_bandwidth def_dl_bandwidth;
22
Dario Faggioliaab03e02013-11-28 11:14:43 +010023static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
24{
25 return container_of(dl_se, struct task_struct, dl);
26}
27
28static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
29{
30 return container_of(dl_rq, struct rq, dl);
31}
32
33static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
34{
35 struct task_struct *p = dl_task_of(dl_se);
36 struct rq *rq = task_rq(p);
37
38 return &rq->dl;
39}
40
41static inline int on_dl_rq(struct sched_dl_entity *dl_se)
42{
43 return !RB_EMPTY_NODE(&dl_se->rb_node);
44}
45
Luca Abenie36d8672017-05-18 22:13:28 +020046static inline
47void add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
48{
49 u64 old = dl_rq->running_bw;
50
51 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
52 dl_rq->running_bw += dl_bw;
53 SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
54}
55
56static inline
57void sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
58{
59 u64 old = dl_rq->running_bw;
60
61 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
62 dl_rq->running_bw -= dl_bw;
63 SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
64 if (dl_rq->running_bw > old)
65 dl_rq->running_bw = 0;
66}
67
Luca Abeni209a0cb2017-05-18 22:13:29 +020068void dl_change_utilization(struct task_struct *p, u64 new_bw)
69{
70 if (task_on_rq_queued(p))
71 return;
72
73 if (!p->dl.dl_non_contending)
74 return;
75
76 sub_running_bw(p->dl.dl_bw, &task_rq(p)->dl);
77 p->dl.dl_non_contending = 0;
78 /*
79 * If the timer handler is currently running and the
80 * timer cannot be cancelled, inactive_task_timer()
81 * will see that dl_not_contending is not set, and
82 * will not touch the rq's active utilization,
83 * so we are still safe.
84 */
85 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
86 put_task_struct(p);
87}
88
89/*
90 * The utilization of a task cannot be immediately removed from
91 * the rq active utilization (running_bw) when the task blocks.
92 * Instead, we have to wait for the so called "0-lag time".
93 *
94 * If a task blocks before the "0-lag time", a timer (the inactive
95 * timer) is armed, and running_bw is decreased when the timer
96 * fires.
97 *
98 * If the task wakes up again before the inactive timer fires,
99 * the timer is cancelled, whereas if the task wakes up after the
100 * inactive timer fired (and running_bw has been decreased) the
101 * task's utilization has to be added to running_bw again.
102 * A flag in the deadline scheduling entity (dl_non_contending)
103 * is used to avoid race conditions between the inactive timer handler
104 * and task wakeups.
105 *
106 * The following diagram shows how running_bw is updated. A task is
107 * "ACTIVE" when its utilization contributes to running_bw; an
108 * "ACTIVE contending" task is in the TASK_RUNNING state, while an
109 * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
110 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
111 * time already passed, which does not contribute to running_bw anymore.
112 * +------------------+
113 * wakeup | ACTIVE |
114 * +------------------>+ contending |
115 * | add_running_bw | |
116 * | +----+------+------+
117 * | | ^
118 * | dequeue | |
119 * +--------+-------+ | |
120 * | | t >= 0-lag | | wakeup
121 * | INACTIVE |<---------------+ |
122 * | | sub_running_bw | |
123 * +--------+-------+ | |
124 * ^ | |
125 * | t < 0-lag | |
126 * | | |
127 * | V |
128 * | +----+------+------+
129 * | sub_running_bw | ACTIVE |
130 * +-------------------+ |
131 * inactive timer | non contending |
132 * fired +------------------+
133 *
134 * The task_non_contending() function is invoked when a task
135 * blocks, and checks if the 0-lag time already passed or
136 * not (in the first case, it directly updates running_bw;
137 * in the second case, it arms the inactive timer).
138 *
139 * The task_contending() function is invoked when a task wakes
140 * up, and checks if the task is still in the "ACTIVE non contending"
141 * state or not (in the second case, it updates running_bw).
142 */
143static void task_non_contending(struct task_struct *p)
144{
145 struct sched_dl_entity *dl_se = &p->dl;
146 struct hrtimer *timer = &dl_se->inactive_timer;
147 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
148 struct rq *rq = rq_of_dl_rq(dl_rq);
149 s64 zerolag_time;
150
151 /*
152 * If this is a non-deadline task that has been boosted,
153 * do nothing
154 */
155 if (dl_se->dl_runtime == 0)
156 return;
157
158 WARN_ON(hrtimer_active(&dl_se->inactive_timer));
159 WARN_ON(dl_se->dl_non_contending);
160
161 zerolag_time = dl_se->deadline -
162 div64_long((dl_se->runtime * dl_se->dl_period),
163 dl_se->dl_runtime);
164
165 /*
166 * Using relative times instead of the absolute "0-lag time"
167 * allows to simplify the code
168 */
169 zerolag_time -= rq_clock(rq);
170
171 /*
172 * If the "0-lag time" already passed, decrease the active
173 * utilization now, instead of starting a timer
174 */
175 if (zerolag_time < 0) {
176 if (dl_task(p))
177 sub_running_bw(dl_se->dl_bw, dl_rq);
178 if (!dl_task(p) || p->state == TASK_DEAD)
179 __dl_clear_params(p);
180
181 return;
182 }
183
184 dl_se->dl_non_contending = 1;
185 get_task_struct(p);
186 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL);
187}
188
189static void task_contending(struct sched_dl_entity *dl_se)
190{
191 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
192
193 /*
194 * If this is a non-deadline task that has been boosted,
195 * do nothing
196 */
197 if (dl_se->dl_runtime == 0)
198 return;
199
200 if (dl_se->dl_non_contending) {
201 dl_se->dl_non_contending = 0;
202 /*
203 * If the timer handler is currently running and the
204 * timer cannot be cancelled, inactive_task_timer()
205 * will see that dl_not_contending is not set, and
206 * will not touch the rq's active utilization,
207 * so we are still safe.
208 */
209 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1)
210 put_task_struct(dl_task_of(dl_se));
211 } else {
212 /*
213 * Since "dl_non_contending" is not set, the
214 * task's utilization has already been removed from
215 * active utilization (either when the task blocked,
216 * when the "inactive timer" fired).
217 * So, add it back.
218 */
219 add_running_bw(dl_se->dl_bw, dl_rq);
220 }
221}
222
Dario Faggioliaab03e02013-11-28 11:14:43 +0100223static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
224{
225 struct sched_dl_entity *dl_se = &p->dl;
226
227 return dl_rq->rb_leftmost == &dl_se->rb_node;
228}
229
Dario Faggioli332ac172013-11-07 14:43:45 +0100230void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
231{
232 raw_spin_lock_init(&dl_b->dl_runtime_lock);
233 dl_b->dl_period = period;
234 dl_b->dl_runtime = runtime;
235}
236
Dario Faggioli332ac172013-11-07 14:43:45 +0100237void init_dl_bw(struct dl_bw *dl_b)
238{
239 raw_spin_lock_init(&dl_b->lock);
240 raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
Peter Zijlstra17248132013-12-17 12:44:49 +0100241 if (global_rt_runtime() == RUNTIME_INF)
Dario Faggioli332ac172013-11-07 14:43:45 +0100242 dl_b->bw = -1;
243 else
Peter Zijlstra17248132013-12-17 12:44:49 +0100244 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +0100245 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
246 dl_b->total_bw = 0;
247}
248
Abel Vesa07c54f72015-03-03 13:50:27 +0200249void init_dl_rq(struct dl_rq *dl_rq)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100250{
251 dl_rq->rb_root = RB_ROOT;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100252
253#ifdef CONFIG_SMP
254 /* zero means no -deadline tasks */
255 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
256
257 dl_rq->dl_nr_migratory = 0;
258 dl_rq->overloaded = 0;
259 dl_rq->pushable_dl_tasks_root = RB_ROOT;
Dario Faggioli332ac172013-11-07 14:43:45 +0100260#else
261 init_dl_bw(&dl_rq->dl_bw);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100262#endif
Luca Abenie36d8672017-05-18 22:13:28 +0200263
264 dl_rq->running_bw = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100265}
266
Juri Lelli1baca4c2013-11-07 14:43:38 +0100267#ifdef CONFIG_SMP
268
269static inline int dl_overloaded(struct rq *rq)
270{
271 return atomic_read(&rq->rd->dlo_count);
272}
273
274static inline void dl_set_overload(struct rq *rq)
275{
276 if (!rq->online)
277 return;
278
279 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
280 /*
281 * Must be visible before the overload count is
282 * set (as in sched_rt.c).
283 *
284 * Matched by the barrier in pull_dl_task().
285 */
286 smp_wmb();
287 atomic_inc(&rq->rd->dlo_count);
288}
289
290static inline void dl_clear_overload(struct rq *rq)
291{
292 if (!rq->online)
293 return;
294
295 atomic_dec(&rq->rd->dlo_count);
296 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
297}
298
299static void update_dl_migration(struct dl_rq *dl_rq)
300{
Kirill Tkhai995b9ea2014-02-18 02:24:13 +0400301 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100302 if (!dl_rq->overloaded) {
303 dl_set_overload(rq_of_dl_rq(dl_rq));
304 dl_rq->overloaded = 1;
305 }
306 } else if (dl_rq->overloaded) {
307 dl_clear_overload(rq_of_dl_rq(dl_rq));
308 dl_rq->overloaded = 0;
309 }
310}
311
312static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
313{
314 struct task_struct *p = dl_task_of(dl_se);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100315
Ingo Molnar4b53a342017-02-05 15:41:03 +0100316 if (p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100317 dl_rq->dl_nr_migratory++;
318
319 update_dl_migration(dl_rq);
320}
321
322static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
323{
324 struct task_struct *p = dl_task_of(dl_se);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100325
Ingo Molnar4b53a342017-02-05 15:41:03 +0100326 if (p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100327 dl_rq->dl_nr_migratory--;
328
329 update_dl_migration(dl_rq);
330}
331
332/*
333 * The list of pushable -deadline task is not a plist, like in
334 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
335 */
336static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
337{
338 struct dl_rq *dl_rq = &rq->dl;
339 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
340 struct rb_node *parent = NULL;
341 struct task_struct *entry;
342 int leftmost = 1;
343
344 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
345
346 while (*link) {
347 parent = *link;
348 entry = rb_entry(parent, struct task_struct,
349 pushable_dl_tasks);
350 if (dl_entity_preempt(&p->dl, &entry->dl))
351 link = &parent->rb_left;
352 else {
353 link = &parent->rb_right;
354 leftmost = 0;
355 }
356 }
357
Wanpeng Li7d92de32015-12-03 17:42:10 +0800358 if (leftmost) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100359 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
Wanpeng Li7d92de32015-12-03 17:42:10 +0800360 dl_rq->earliest_dl.next = p->dl.deadline;
361 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100362
363 rb_link_node(&p->pushable_dl_tasks, parent, link);
364 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
365}
366
367static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
368{
369 struct dl_rq *dl_rq = &rq->dl;
370
371 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
372 return;
373
374 if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
375 struct rb_node *next_node;
376
377 next_node = rb_next(&p->pushable_dl_tasks);
378 dl_rq->pushable_dl_tasks_leftmost = next_node;
Wanpeng Li7d92de32015-12-03 17:42:10 +0800379 if (next_node) {
380 dl_rq->earliest_dl.next = rb_entry(next_node,
381 struct task_struct, pushable_dl_tasks)->dl.deadline;
382 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100383 }
384
385 rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
386 RB_CLEAR_NODE(&p->pushable_dl_tasks);
387}
388
389static inline int has_pushable_dl_tasks(struct rq *rq)
390{
391 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
392}
393
394static int push_dl_task(struct rq *rq);
395
Peter Zijlstradc877342014-02-12 15:47:29 +0100396static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
397{
398 return dl_task(prev);
399}
400
Peter Zijlstra9916e212015-06-11 14:46:43 +0200401static DEFINE_PER_CPU(struct callback_head, dl_push_head);
402static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200403
404static void push_dl_tasks(struct rq *);
Peter Zijlstra9916e212015-06-11 14:46:43 +0200405static void pull_dl_task(struct rq *);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200406
407static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100408{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200409 if (!has_pushable_dl_tasks(rq))
410 return;
411
Peter Zijlstra9916e212015-06-11 14:46:43 +0200412 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
413}
414
415static inline void queue_pull_task(struct rq *rq)
416{
417 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
Peter Zijlstradc877342014-02-12 15:47:29 +0100418}
419
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800420static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
421
Peter Zijlstraa649f232015-06-11 14:46:49 +0200422static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800423{
424 struct rq *later_rq = NULL;
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800425
426 later_rq = find_lock_later_rq(p, rq);
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800427 if (!later_rq) {
428 int cpu;
429
430 /*
431 * If we cannot preempt any rq, fall back to pick any
432 * online cpu.
433 */
Ingo Molnar0c98d342017-02-05 15:38:10 +0100434 cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800435 if (cpu >= nr_cpu_ids) {
436 /*
437 * Fail to find any suitable cpu.
438 * The task will never come back!
439 */
440 BUG_ON(dl_bandwidth_enabled());
441
442 /*
443 * If admission control is disabled we
444 * try a little harder to let the task
445 * run.
446 */
447 cpu = cpumask_any(cpu_active_mask);
448 }
449 later_rq = cpu_rq(cpu);
450 double_lock_balance(rq, later_rq);
451 }
452
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800453 set_task_cpu(p, later_rq->cpu);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200454 double_unlock_balance(later_rq, rq);
455
456 return later_rq;
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800457}
458
Juri Lelli1baca4c2013-11-07 14:43:38 +0100459#else
460
461static inline
462void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
463{
464}
465
466static inline
467void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
468{
469}
470
471static inline
472void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
473{
474}
475
476static inline
477void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
478{
479}
480
Peter Zijlstradc877342014-02-12 15:47:29 +0100481static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
482{
483 return false;
484}
485
Peter Zijlstra0ea60c22015-06-11 14:46:42 +0200486static inline void pull_dl_task(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100487{
Peter Zijlstradc877342014-02-12 15:47:29 +0100488}
489
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200490static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100491{
492}
Peter Zijlstra9916e212015-06-11 14:46:43 +0200493
494static inline void queue_pull_task(struct rq *rq)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100495{
496}
497#endif /* CONFIG_SMP */
498
Dario Faggioliaab03e02013-11-28 11:14:43 +0100499static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
500static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
501static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
502 int flags);
503
504/*
505 * We are being explicitly informed that a new instance is starting,
506 * and this means that:
507 * - the absolute deadline of the entity has to be placed at
508 * current time + relative deadline;
509 * - the runtime of the entity has to be set to the maximum value.
510 *
511 * The capability of specifying such event is useful whenever a -deadline
512 * entity wants to (try to!) synchronize its behaviour with the scheduler's
513 * one, and to (try to!) reconcile itself with its own scheduling
514 * parameters.
515 */
Juri Lelli98b0a852016-08-05 16:07:55 +0100516static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100517{
518 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
519 struct rq *rq = rq_of_dl_rq(dl_rq);
520
Juri Lelli98b0a852016-08-05 16:07:55 +0100521 WARN_ON(dl_se->dl_boosted);
Luca Abeni72f9f3f2016-03-07 12:27:04 +0100522 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
523
524 /*
525 * We are racing with the deadline timer. So, do nothing because
526 * the deadline timer handler will take care of properly recharging
527 * the runtime and postponing the deadline
528 */
529 if (dl_se->dl_throttled)
530 return;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100531
532 /*
533 * We use the regular wall clock time to set deadlines in the
534 * future; in fact, we must consider execution overheads (time
535 * spent on hardirq context, etc.).
536 */
Juri Lelli98b0a852016-08-05 16:07:55 +0100537 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
538 dl_se->runtime = dl_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100539}
540
541/*
542 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
543 * possibility of a entity lasting more than what it declared, and thus
544 * exhausting its runtime.
545 *
546 * Here we are interested in making runtime overrun possible, but we do
547 * not want a entity which is misbehaving to affect the scheduling of all
548 * other entities.
549 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
550 * is used, in order to confine each entity within its own bandwidth.
551 *
552 * This function deals exactly with that, and ensures that when the runtime
553 * of a entity is replenished, its deadline is also postponed. That ensures
554 * the overrunning entity can't interfere with other entity in the system and
555 * can't make them miss their deadlines. Reasons why this kind of overruns
556 * could happen are, typically, a entity voluntarily trying to overcome its
xiaofeng.yan1b09d292014-07-07 05:59:04 +0000557 * runtime, or it just underestimated it during sched_setattr().
Dario Faggioliaab03e02013-11-28 11:14:43 +0100558 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100559static void replenish_dl_entity(struct sched_dl_entity *dl_se,
560 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100561{
562 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
563 struct rq *rq = rq_of_dl_rq(dl_rq);
564
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100565 BUG_ON(pi_se->dl_runtime <= 0);
566
567 /*
568 * This could be the case for a !-dl task that is boosted.
569 * Just go with full inherited parameters.
570 */
571 if (dl_se->dl_deadline == 0) {
572 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
573 dl_se->runtime = pi_se->dl_runtime;
574 }
575
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100576 if (dl_se->dl_yielded && dl_se->runtime > 0)
577 dl_se->runtime = 0;
578
Dario Faggioliaab03e02013-11-28 11:14:43 +0100579 /*
580 * We keep moving the deadline away until we get some
581 * available runtime for the entity. This ensures correct
582 * handling of situations where the runtime overrun is
583 * arbitrary large.
584 */
585 while (dl_se->runtime <= 0) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100586 dl_se->deadline += pi_se->dl_period;
587 dl_se->runtime += pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100588 }
589
590 /*
591 * At this point, the deadline really should be "in
592 * the future" with respect to rq->clock. If it's
593 * not, we are, for some reason, lagging too much!
594 * Anyway, after having warn userspace abut that,
595 * we still try to keep the things running by
596 * resetting the deadline and the budget of the
597 * entity.
598 */
599 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
Steven Rostedtc219b7d2016-02-10 12:04:22 -0500600 printk_deferred_once("sched: DL replenish lagged too much\n");
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100601 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
602 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100603 }
Peter Zijlstra1019a352014-11-26 08:44:03 +0800604
605 if (dl_se->dl_yielded)
606 dl_se->dl_yielded = 0;
607 if (dl_se->dl_throttled)
608 dl_se->dl_throttled = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100609}
610
611/*
612 * Here we check if --at time t-- an entity (which is probably being
613 * [re]activated or, in general, enqueued) can use its remaining runtime
614 * and its current deadline _without_ exceeding the bandwidth it is
615 * assigned (function returns true if it can't). We are in fact applying
616 * one of the CBS rules: when a task wakes up, if the residual runtime
617 * over residual deadline fits within the allocated bandwidth, then we
618 * can keep the current (absolute) deadline and residual budget without
619 * disrupting the schedulability of the system. Otherwise, we should
620 * refill the runtime and set the deadline a period in the future,
621 * because keeping the current (absolute) deadline of the task would
Dario Faggioli712e5e32014-01-27 12:20:15 +0100622 * result in breaking guarantees promised to other tasks (refer to
623 * Documentation/scheduler/sched-deadline.txt for more informations).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100624 *
625 * This function returns true if:
626 *
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100627 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
Dario Faggioliaab03e02013-11-28 11:14:43 +0100628 *
629 * IOW we can't recycle current parameters.
Harald Gustafsson755378a2013-11-07 14:43:40 +0100630 *
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100631 * Notice that the bandwidth check is done against the deadline. For
Harald Gustafsson755378a2013-11-07 14:43:40 +0100632 * task with deadline equal to period this is the same of using
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100633 * dl_period instead of dl_deadline in the equation above.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100634 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100635static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
636 struct sched_dl_entity *pi_se, u64 t)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100637{
638 u64 left, right;
639
640 /*
641 * left and right are the two sides of the equation above,
642 * after a bit of shuffling to use multiplications instead
643 * of divisions.
644 *
645 * Note that none of the time values involved in the two
646 * multiplications are absolute: dl_deadline and dl_runtime
647 * are the relative deadline and the maximum runtime of each
648 * instance, runtime is the runtime left for the last instance
649 * and (deadline - t), since t is rq->clock, is the time left
650 * to the (absolute) deadline. Even if overflowing the u64 type
651 * is very unlikely to occur in both cases, here we scale down
652 * as we want to avoid that risk at all. Scaling down by 10
653 * means that we reduce granularity to 1us. We are fine with it,
654 * since this is only a true/false check and, anyway, thinking
655 * of anything below microseconds resolution is actually fiction
656 * (but still we want to give the user that illusion >;).
657 */
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100658 left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
Dario Faggioli332ac172013-11-07 14:43:45 +0100659 right = ((dl_se->deadline - t) >> DL_SCALE) *
660 (pi_se->dl_runtime >> DL_SCALE);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100661
662 return dl_time_before(right, left);
663}
664
665/*
666 * When a -deadline entity is queued back on the runqueue, its runtime and
667 * deadline might need updating.
668 *
669 * The policy here is that we update the deadline of the entity only if:
670 * - the current deadline is in the past,
671 * - using the remaining runtime with the current deadline would make
672 * the entity exceed its bandwidth.
673 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100674static void update_dl_entity(struct sched_dl_entity *dl_se,
675 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100676{
677 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
678 struct rq *rq = rq_of_dl_rq(dl_rq);
679
Dario Faggioliaab03e02013-11-28 11:14:43 +0100680 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100681 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
682 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
683 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100684 }
685}
686
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100687static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
688{
689 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
690}
691
Dario Faggioliaab03e02013-11-28 11:14:43 +0100692/*
693 * If the entity depleted all its runtime, and if we want it to sleep
694 * while waiting for some new execution time to become available, we
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100695 * set the bandwidth replenishment timer to the replenishment instant
Dario Faggioliaab03e02013-11-28 11:14:43 +0100696 * and try to activate it.
697 *
698 * Notice that it is important for the caller to know if the timer
699 * actually started or not (i.e., the replenishment instant is in
700 * the future or in the past).
701 */
Peter Zijlstraa649f232015-06-11 14:46:49 +0200702static int start_dl_timer(struct task_struct *p)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100703{
Peter Zijlstraa649f232015-06-11 14:46:49 +0200704 struct sched_dl_entity *dl_se = &p->dl;
705 struct hrtimer *timer = &dl_se->dl_timer;
706 struct rq *rq = task_rq(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100707 ktime_t now, act;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100708 s64 delta;
709
Peter Zijlstraa649f232015-06-11 14:46:49 +0200710 lockdep_assert_held(&rq->lock);
711
Dario Faggioliaab03e02013-11-28 11:14:43 +0100712 /*
713 * We want the timer to fire at the deadline, but considering
714 * that it is actually coming from rq->clock and not from
715 * hrtimer's time base reading.
716 */
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100717 act = ns_to_ktime(dl_next_period(dl_se));
Peter Zijlstraa649f232015-06-11 14:46:49 +0200718 now = hrtimer_cb_get_time(timer);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100719 delta = ktime_to_ns(now) - rq_clock(rq);
720 act = ktime_add_ns(act, delta);
721
722 /*
723 * If the expiry time already passed, e.g., because the value
724 * chosen as the deadline is too small, don't even try to
725 * start the timer in the past!
726 */
727 if (ktime_us_delta(act, now) < 0)
728 return 0;
729
Peter Zijlstraa649f232015-06-11 14:46:49 +0200730 /*
731 * !enqueued will guarantee another callback; even if one is already in
732 * progress. This ensures a balanced {get,put}_task_struct().
733 *
734 * The race against __run_timer() clearing the enqueued state is
735 * harmless because we're holding task_rq()->lock, therefore the timer
736 * expiring after we've done the check will wait on its task_rq_lock()
737 * and observe our state.
738 */
739 if (!hrtimer_is_queued(timer)) {
740 get_task_struct(p);
741 hrtimer_start(timer, act, HRTIMER_MODE_ABS);
742 }
Dario Faggioliaab03e02013-11-28 11:14:43 +0100743
Thomas Gleixnercc9684d2015-04-14 21:09:06 +0000744 return 1;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100745}
746
747/*
748 * This is the bandwidth enforcement timer callback. If here, we know
749 * a task is not on its dl_rq, since the fact that the timer was running
750 * means the task is throttled and needs a runtime replenishment.
751 *
752 * However, what we actually do depends on the fact the task is active,
753 * (it is on its rq) or has been removed from there by a call to
754 * dequeue_task_dl(). In the former case we must issue the runtime
755 * replenishment and add the task back to the dl_rq; in the latter, we just
756 * do nothing but clearing dl_throttled, so that runtime and deadline
757 * updating (and the queueing back to dl_rq) will be done by the
758 * next call to enqueue_task_dl().
759 */
760static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
761{
762 struct sched_dl_entity *dl_se = container_of(timer,
763 struct sched_dl_entity,
764 dl_timer);
765 struct task_struct *p = dl_task_of(dl_se);
Peter Zijlstraeb580752015-07-31 21:28:18 +0200766 struct rq_flags rf;
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400767 struct rq *rq;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100768
Peter Zijlstraeb580752015-07-31 21:28:18 +0200769 rq = task_rq_lock(p, &rf);
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400770
Dario Faggioliaab03e02013-11-28 11:14:43 +0100771 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200772 * The task might have changed its scheduling policy to something
Daniel Bristot de Oliveira9846d502016-11-08 11:15:23 +0100773 * different than SCHED_DEADLINE (through switched_from_dl()).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100774 */
Luca Abeni209a0cb2017-05-18 22:13:29 +0200775 if (!dl_task(p))
Peter Zijlstraa649f232015-06-11 14:46:49 +0200776 goto unlock;
Peter Zijlstraa649f232015-06-11 14:46:49 +0200777
778 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200779 * The task might have been boosted by someone else and might be in the
780 * boosting/deboosting path, its not throttled.
781 */
782 if (dl_se->dl_boosted)
783 goto unlock;
784
785 /*
786 * Spurious timer due to start_dl_timer() race; or we already received
787 * a replenishment from rt_mutex_setprio().
788 */
789 if (!dl_se->dl_throttled)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100790 goto unlock;
791
792 sched_clock_tick();
793 update_rq_clock(rq);
Kirill Tkhaia79ec892015-02-16 15:38:34 +0300794
795 /*
796 * If the throttle happened during sched-out; like:
797 *
798 * schedule()
799 * deactivate_task()
800 * dequeue_task_dl()
801 * update_curr_dl()
802 * start_dl_timer()
803 * __dequeue_task_dl()
804 * prev->on_rq = 0;
805 *
806 * We can be both throttled and !queued. Replenish the counter
807 * but do not enqueue -- wait for our wakeup to do that.
808 */
809 if (!task_on_rq_queued(p)) {
810 replenish_dl_entity(dl_se, dl_se);
811 goto unlock;
812 }
813
Wanpeng Li61c7aca2016-08-31 18:27:44 +0800814#ifdef CONFIG_SMP
815 if (unlikely(!rq->online)) {
816 /*
817 * If the runqueue is no longer available, migrate the
818 * task elsewhere. This necessarily changes rq.
819 */
820 lockdep_unpin_lock(&rq->lock, rf.cookie);
821 rq = dl_task_offline_migration(rq, p);
822 rf.cookie = lockdep_pin_lock(&rq->lock);
Wanpeng Lidcc3b5f2017-03-06 21:51:28 -0800823 update_rq_clock(rq);
Wanpeng Li61c7aca2016-08-31 18:27:44 +0800824
825 /*
826 * Now that the task has been migrated to the new RQ and we
827 * have that locked, proceed as normal and enqueue the task
828 * there.
829 */
830 }
831#endif
832
Peter Zijlstra1019a352014-11-26 08:44:03 +0800833 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
834 if (dl_task(rq->curr))
835 check_preempt_curr_dl(rq, p, 0);
836 else
837 resched_curr(rq);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200838
Juri Lelli1baca4c2013-11-07 14:43:38 +0100839#ifdef CONFIG_SMP
Peter Zijlstra1019a352014-11-26 08:44:03 +0800840 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200841 * Queueing this task back might have overloaded rq, check if we need
842 * to kick someone away.
Peter Zijlstra1019a352014-11-26 08:44:03 +0800843 */
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +0200844 if (has_pushable_dl_tasks(rq)) {
845 /*
846 * Nothing relies on rq->lock after this, so its safe to drop
847 * rq->lock.
848 */
Matt Flemingd8ac8972016-09-21 14:38:10 +0100849 rq_unpin_lock(rq, &rf);
Peter Zijlstra1019a352014-11-26 08:44:03 +0800850 push_dl_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +0100851 rq_repin_lock(rq, &rf);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +0200852 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100853#endif
Peter Zijlstraa649f232015-06-11 14:46:49 +0200854
Dario Faggioliaab03e02013-11-28 11:14:43 +0100855unlock:
Peter Zijlstraeb580752015-07-31 21:28:18 +0200856 task_rq_unlock(rq, p, &rf);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100857
Peter Zijlstraa649f232015-06-11 14:46:49 +0200858 /*
859 * This can free the task_struct, including this hrtimer, do not touch
860 * anything related to that after this.
861 */
862 put_task_struct(p);
863
Dario Faggioliaab03e02013-11-28 11:14:43 +0100864 return HRTIMER_NORESTART;
865}
866
867void init_dl_task_timer(struct sched_dl_entity *dl_se)
868{
869 struct hrtimer *timer = &dl_se->dl_timer;
870
Dario Faggioliaab03e02013-11-28 11:14:43 +0100871 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
872 timer->function = dl_task_timer;
873}
874
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +0100875/*
876 * During the activation, CBS checks if it can reuse the current task's
877 * runtime and period. If the deadline of the task is in the past, CBS
878 * cannot use the runtime, and so it replenishes the task. This rule
879 * works fine for implicit deadline tasks (deadline == period), and the
880 * CBS was designed for implicit deadline tasks. However, a task with
881 * constrained deadline (deadine < period) might be awakened after the
882 * deadline, but before the next period. In this case, replenishing the
883 * task would allow it to run for runtime / deadline. As in this case
884 * deadline < period, CBS enables a task to run for more than the
885 * runtime / period. In a very loaded system, this can cause a domino
886 * effect, making other tasks miss their deadlines.
887 *
888 * To avoid this problem, in the activation of a constrained deadline
889 * task after the deadline but before the next period, throttle the
890 * task and set the replenishing timer to the begin of the next period,
891 * unless it is boosted.
892 */
893static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
894{
895 struct task_struct *p = dl_task_of(dl_se);
896 struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
897
898 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
899 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
900 if (unlikely(dl_se->dl_boosted || !start_dl_timer(p)))
901 return;
902 dl_se->dl_throttled = 1;
903 }
904}
905
Dario Faggioliaab03e02013-11-28 11:14:43 +0100906static
Zhiqiang Zhang6fab5412015-06-15 11:15:20 +0800907int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100908{
Luca Abeni269ad802014-12-17 11:50:32 +0100909 return (dl_se->runtime <= 0);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100910}
911
Juri Lellifaa59932014-02-21 11:37:15 +0100912extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
913
Dario Faggioliaab03e02013-11-28 11:14:43 +0100914/*
915 * Update the current task's runtime statistics (provided it is still
916 * a -deadline task and has not been removed from the dl_rq).
917 */
918static void update_curr_dl(struct rq *rq)
919{
920 struct task_struct *curr = rq->curr;
921 struct sched_dl_entity *dl_se = &curr->dl;
922 u64 delta_exec;
923
924 if (!dl_task(curr) || !on_dl_rq(dl_se))
925 return;
926
927 /*
928 * Consumed budget is computed considering the time as
929 * observed by schedulable tasks (excluding time spent
930 * in hardirq context, etc.). Deadlines are instead
931 * computed using hard walltime. This seems to be the more
932 * natural solution, but the full ramifications of this
933 * approach need further study.
934 */
935 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100936 if (unlikely((s64)delta_exec <= 0)) {
937 if (unlikely(dl_se->dl_yielded))
938 goto throttle;
Kirill Tkhai734ff2a2014-03-04 19:25:46 +0400939 return;
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100940 }
Dario Faggioliaab03e02013-11-28 11:14:43 +0100941
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200942 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +0200943 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_DL);
Wanpeng Li594dd292016-04-22 17:07:24 +0800944
Dario Faggioliaab03e02013-11-28 11:14:43 +0100945 schedstat_set(curr->se.statistics.exec_max,
946 max(curr->se.statistics.exec_max, delta_exec));
947
948 curr->se.sum_exec_runtime += delta_exec;
949 account_group_exec_runtime(curr, delta_exec);
950
951 curr->se.exec_start = rq_clock_task(rq);
952 cpuacct_charge(curr, delta_exec);
953
Dario Faggioli239be4a2013-11-07 14:43:39 +0100954 sched_rt_avg_update(rq, delta_exec);
955
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100956 dl_se->runtime -= delta_exec;
957
958throttle:
959 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
Peter Zijlstra1019a352014-11-26 08:44:03 +0800960 dl_se->dl_throttled = 1;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100961 __dequeue_task_dl(rq, curr, 0);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200962 if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
Dario Faggioliaab03e02013-11-28 11:14:43 +0100963 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
964
965 if (!is_leftmost(curr, &rq->dl))
Kirill Tkhai88751252014-06-29 00:03:57 +0400966 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100967 }
Peter Zijlstra17248132013-12-17 12:44:49 +0100968
969 /*
970 * Because -- for now -- we share the rt bandwidth, we need to
971 * account our runtime there too, otherwise actual rt tasks
972 * would be able to exceed the shared quota.
973 *
974 * Account to the root rt group for now.
975 *
976 * The solution we're working towards is having the RT groups scheduled
977 * using deadline servers -- however there's a few nasties to figure
978 * out before that can happen.
979 */
980 if (rt_bandwidth_enabled()) {
981 struct rt_rq *rt_rq = &rq->rt;
982
983 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra17248132013-12-17 12:44:49 +0100984 /*
985 * We'll let actual RT tasks worry about the overflow here, we
Juri Lellifaa59932014-02-21 11:37:15 +0100986 * have our own CBS to keep us inline; only account when RT
987 * bandwidth is relevant.
Peter Zijlstra17248132013-12-17 12:44:49 +0100988 */
Juri Lellifaa59932014-02-21 11:37:15 +0100989 if (sched_rt_bandwidth_account(rt_rq))
990 rt_rq->rt_time += delta_exec;
Peter Zijlstra17248132013-12-17 12:44:49 +0100991 raw_spin_unlock(&rt_rq->rt_runtime_lock);
992 }
Dario Faggioliaab03e02013-11-28 11:14:43 +0100993}
994
Luca Abeni209a0cb2017-05-18 22:13:29 +0200995static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
996{
997 struct sched_dl_entity *dl_se = container_of(timer,
998 struct sched_dl_entity,
999 inactive_timer);
1000 struct task_struct *p = dl_task_of(dl_se);
1001 struct rq_flags rf;
1002 struct rq *rq;
1003
1004 rq = task_rq_lock(p, &rf);
1005
1006 if (!dl_task(p) || p->state == TASK_DEAD) {
1007 if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
1008 sub_running_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl));
1009 dl_se->dl_non_contending = 0;
1010 }
1011 __dl_clear_params(p);
1012
1013 goto unlock;
1014 }
1015 if (dl_se->dl_non_contending == 0)
1016 goto unlock;
1017
1018 sched_clock_tick();
1019 update_rq_clock(rq);
1020
1021 sub_running_bw(dl_se->dl_bw, &rq->dl);
1022 dl_se->dl_non_contending = 0;
1023unlock:
1024 task_rq_unlock(rq, p, &rf);
1025 put_task_struct(p);
1026
1027 return HRTIMER_NORESTART;
1028}
1029
1030void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1031{
1032 struct hrtimer *timer = &dl_se->inactive_timer;
1033
1034 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1035 timer->function = inactive_task_timer;
1036}
1037
Juri Lelli1baca4c2013-11-07 14:43:38 +01001038#ifdef CONFIG_SMP
1039
Juri Lelli1baca4c2013-11-07 14:43:38 +01001040static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1041{
1042 struct rq *rq = rq_of_dl_rq(dl_rq);
1043
1044 if (dl_rq->earliest_dl.curr == 0 ||
1045 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001046 dl_rq->earliest_dl.curr = deadline;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001047 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001048 }
1049}
1050
1051static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1052{
1053 struct rq *rq = rq_of_dl_rq(dl_rq);
1054
1055 /*
1056 * Since we may have removed our earliest (and/or next earliest)
1057 * task we must recompute them.
1058 */
1059 if (!dl_rq->dl_nr_running) {
1060 dl_rq->earliest_dl.curr = 0;
1061 dl_rq->earliest_dl.next = 0;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001062 cpudl_clear(&rq->rd->cpudl, rq->cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001063 } else {
1064 struct rb_node *leftmost = dl_rq->rb_leftmost;
1065 struct sched_dl_entity *entry;
1066
1067 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
1068 dl_rq->earliest_dl.curr = entry->deadline;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001069 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001070 }
1071}
1072
1073#else
1074
1075static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1076static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1077
1078#endif /* CONFIG_SMP */
1079
1080static inline
1081void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1082{
1083 int prio = dl_task_of(dl_se)->prio;
1084 u64 deadline = dl_se->deadline;
1085
1086 WARN_ON(!dl_prio(prio));
1087 dl_rq->dl_nr_running++;
Kirill Tkhai72465442014-05-09 03:00:14 +04001088 add_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001089
1090 inc_dl_deadline(dl_rq, deadline);
1091 inc_dl_migration(dl_se, dl_rq);
1092}
1093
1094static inline
1095void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1096{
1097 int prio = dl_task_of(dl_se)->prio;
1098
1099 WARN_ON(!dl_prio(prio));
1100 WARN_ON(!dl_rq->dl_nr_running);
1101 dl_rq->dl_nr_running--;
Kirill Tkhai72465442014-05-09 03:00:14 +04001102 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001103
1104 dec_dl_deadline(dl_rq, dl_se->deadline);
1105 dec_dl_migration(dl_se, dl_rq);
1106}
1107
Dario Faggioliaab03e02013-11-28 11:14:43 +01001108static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1109{
1110 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1111 struct rb_node **link = &dl_rq->rb_root.rb_node;
1112 struct rb_node *parent = NULL;
1113 struct sched_dl_entity *entry;
1114 int leftmost = 1;
1115
1116 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
1117
1118 while (*link) {
1119 parent = *link;
1120 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
1121 if (dl_time_before(dl_se->deadline, entry->deadline))
1122 link = &parent->rb_left;
1123 else {
1124 link = &parent->rb_right;
1125 leftmost = 0;
1126 }
1127 }
1128
1129 if (leftmost)
1130 dl_rq->rb_leftmost = &dl_se->rb_node;
1131
1132 rb_link_node(&dl_se->rb_node, parent, link);
1133 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
1134
Juri Lelli1baca4c2013-11-07 14:43:38 +01001135 inc_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001136}
1137
1138static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1139{
1140 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1141
1142 if (RB_EMPTY_NODE(&dl_se->rb_node))
1143 return;
1144
1145 if (dl_rq->rb_leftmost == &dl_se->rb_node) {
1146 struct rb_node *next_node;
1147
1148 next_node = rb_next(&dl_se->rb_node);
1149 dl_rq->rb_leftmost = next_node;
1150 }
1151
1152 rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
1153 RB_CLEAR_NODE(&dl_se->rb_node);
1154
Juri Lelli1baca4c2013-11-07 14:43:38 +01001155 dec_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001156}
1157
1158static void
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001159enqueue_dl_entity(struct sched_dl_entity *dl_se,
1160 struct sched_dl_entity *pi_se, int flags)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001161{
1162 BUG_ON(on_dl_rq(dl_se));
1163
1164 /*
1165 * If this is a wakeup or a new instance, the scheduling
1166 * parameters of the task might need updating. Otherwise,
1167 * we want a replenishment of its runtime.
1168 */
Luca Abenie36d8672017-05-18 22:13:28 +02001169 if (flags & ENQUEUE_WAKEUP) {
Luca Abeni209a0cb2017-05-18 22:13:29 +02001170 task_contending(dl_se);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001171 update_dl_entity(dl_se, pi_se);
Luca Abenie36d8672017-05-18 22:13:28 +02001172 } else if (flags & ENQUEUE_REPLENISH) {
Luca Abeni6a503c32014-12-17 11:50:31 +01001173 replenish_dl_entity(dl_se, pi_se);
Luca Abenie36d8672017-05-18 22:13:28 +02001174 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001175
1176 __enqueue_dl_entity(dl_se);
1177}
1178
1179static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
1180{
1181 __dequeue_dl_entity(dl_se);
1182}
1183
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001184static inline bool dl_is_constrained(struct sched_dl_entity *dl_se)
1185{
1186 return dl_se->dl_deadline < dl_se->dl_period;
1187}
1188
Dario Faggioliaab03e02013-11-28 11:14:43 +01001189static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1190{
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001191 struct task_struct *pi_task = rt_mutex_get_top_task(p);
1192 struct sched_dl_entity *pi_se = &p->dl;
1193
1194 /*
1195 * Use the scheduling parameters of the top pi-waiter
Andrea Parriff277d42015-08-05 15:56:19 +02001196 * task if we have one and its (absolute) deadline is
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001197 * smaller than our one... OTW we keep our runtime and
1198 * deadline.
1199 */
Juri Lelli64be6f12014-10-24 10:16:37 +01001200 if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001201 pi_se = &pi_task->dl;
Juri Lelli64be6f12014-10-24 10:16:37 +01001202 } else if (!dl_prio(p->normal_prio)) {
1203 /*
1204 * Special case in which we have a !SCHED_DEADLINE task
1205 * that is going to be deboosted, but exceedes its
1206 * runtime while doing so. No point in replenishing
1207 * it, as it's going to return back to its original
1208 * scheduling class after this.
1209 */
1210 BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
1211 return;
1212 }
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001213
Dario Faggioliaab03e02013-11-28 11:14:43 +01001214 /*
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001215 * Check if a constrained deadline task was activated
1216 * after the deadline but before the next period.
1217 * If that is the case, the task will be throttled and
1218 * the replenishment timer will be set to the next period.
1219 */
1220 if (!p->dl.dl_throttled && dl_is_constrained(&p->dl))
1221 dl_check_constrained_dl(&p->dl);
1222
Luca Abenie36d8672017-05-18 22:13:28 +02001223 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE)
1224 add_running_bw(p->dl.dl_bw, &rq->dl);
1225
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001226 /*
Luca Abenie36d8672017-05-18 22:13:28 +02001227 * If p is throttled, we do not enqueue it. In fact, if it exhausted
Dario Faggioliaab03e02013-11-28 11:14:43 +01001228 * its budget it needs a replenishment and, since it now is on
1229 * its rq, the bandwidth timer callback (which clearly has not
1230 * run yet) will take care of this.
Luca Abenie36d8672017-05-18 22:13:28 +02001231 * However, the active utilization does not depend on the fact
1232 * that the task is on the runqueue or not (but depends on the
1233 * task's state - in GRUB parlance, "inactive" vs "active contending").
1234 * In other words, even if a task is throttled its utilization must
1235 * be counted in the active utilization; hence, we need to call
1236 * add_running_bw().
Dario Faggioliaab03e02013-11-28 11:14:43 +01001237 */
Luca Abenie36d8672017-05-18 22:13:28 +02001238 if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
Luca Abeni209a0cb2017-05-18 22:13:29 +02001239 if (flags & ENQUEUE_WAKEUP)
1240 task_contending(&p->dl);
1241
Dario Faggioliaab03e02013-11-28 11:14:43 +01001242 return;
Luca Abenie36d8672017-05-18 22:13:28 +02001243 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001244
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001245 enqueue_dl_entity(&p->dl, pi_se, flags);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001246
Ingo Molnar4b53a342017-02-05 15:41:03 +01001247 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001248 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001249}
1250
1251static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1252{
1253 dequeue_dl_entity(&p->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001254 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001255}
1256
1257static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1258{
1259 update_curr_dl(rq);
1260 __dequeue_task_dl(rq, p, flags);
Luca Abenie36d8672017-05-18 22:13:28 +02001261
1262 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE)
1263 sub_running_bw(p->dl.dl_bw, &rq->dl);
1264
1265 /*
Luca Abeni209a0cb2017-05-18 22:13:29 +02001266 * This check allows to start the inactive timer (or to immediately
1267 * decrease the active utilization, if needed) in two cases:
Luca Abenie36d8672017-05-18 22:13:28 +02001268 * when the task blocks and when it is terminating
1269 * (p->state == TASK_DEAD). We can handle the two cases in the same
1270 * way, because from GRUB's point of view the same thing is happening
1271 * (the task moves from "active contending" to "active non contending"
1272 * or "inactive")
1273 */
1274 if (flags & DEQUEUE_SLEEP)
Luca Abeni209a0cb2017-05-18 22:13:29 +02001275 task_non_contending(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001276}
1277
1278/*
1279 * Yield task semantic for -deadline tasks is:
1280 *
1281 * get off from the CPU until our next instance, with
1282 * a new runtime. This is of little use now, since we
1283 * don't have a bandwidth reclaiming mechanism. Anyway,
1284 * bandwidth reclaiming is planned for the future, and
1285 * yield_task_dl will indicate that some spare budget
1286 * is available for other task instances to use it.
1287 */
1288static void yield_task_dl(struct rq *rq)
1289{
Dario Faggioliaab03e02013-11-28 11:14:43 +01001290 /*
1291 * We make the task go to sleep until its current deadline by
1292 * forcing its runtime to zero. This way, update_curr_dl() stops
1293 * it and the bandwidth timer will wake it up and will give it
Juri Lelli5bfd1262014-04-15 13:49:04 +02001294 * new scheduling parameters (thanks to dl_yielded=1).
Dario Faggioliaab03e02013-11-28 11:14:43 +01001295 */
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001296 rq->curr->dl.dl_yielded = 1;
1297
Kirill Tkhai6f1607f2015-02-04 12:09:32 +03001298 update_rq_clock(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001299 update_curr_dl(rq);
Wanpeng Li44fb0852015-03-10 12:20:00 +08001300 /*
1301 * Tell update_rq_clock() that we've just updated,
1302 * so we don't do microscopic update in schedule()
1303 * and double the fastpath cost.
1304 */
1305 rq_clock_skip_update(rq, true);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001306}
1307
Juri Lelli1baca4c2013-11-07 14:43:38 +01001308#ifdef CONFIG_SMP
1309
1310static int find_later_rq(struct task_struct *task);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001311
1312static int
1313select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1314{
1315 struct task_struct *curr;
1316 struct rq *rq;
1317
Wanpeng Li1d7e9742014-10-14 10:22:39 +08001318 if (sd_flag != SD_BALANCE_WAKE)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001319 goto out;
1320
1321 rq = cpu_rq(cpu);
1322
1323 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07001324 curr = READ_ONCE(rq->curr); /* unlocked access */
Juri Lelli1baca4c2013-11-07 14:43:38 +01001325
1326 /*
1327 * If we are dealing with a -deadline task, we must
1328 * decide where to wake it up.
1329 * If it has a later deadline and the current task
1330 * on this rq can't move (provided the waking task
1331 * can!) we prefer to send it somewhere else. On the
1332 * other hand, if it has a shorter deadline, we
1333 * try to make it stay here, it might be important.
1334 */
1335 if (unlikely(dl_task(curr)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001336 (curr->nr_cpus_allowed < 2 ||
Juri Lelli1baca4c2013-11-07 14:43:38 +01001337 !dl_entity_preempt(&p->dl, &curr->dl)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001338 (p->nr_cpus_allowed > 1)) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001339 int target = find_later_rq(p);
1340
Wanpeng Li9d514262015-05-13 14:01:03 +08001341 if (target != -1 &&
Luca Abeni5aa50502015-10-16 10:06:21 +02001342 (dl_time_before(p->dl.deadline,
1343 cpu_rq(target)->dl.earliest_dl.curr) ||
1344 (cpu_rq(target)->dl.dl_nr_running == 0)))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001345 cpu = target;
1346 }
1347 rcu_read_unlock();
1348
1349out:
1350 return cpu;
1351}
1352
Luca Abeni209a0cb2017-05-18 22:13:29 +02001353static void migrate_task_rq_dl(struct task_struct *p)
1354{
1355 struct rq *rq;
1356
1357 if (!(p->state == TASK_WAKING) || !(p->dl.dl_non_contending))
1358 return;
1359
1360 rq = task_rq(p);
1361 /*
1362 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1363 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1364 * rq->lock is not... So, lock it
1365 */
1366 raw_spin_lock(&rq->lock);
1367 sub_running_bw(p->dl.dl_bw, &rq->dl);
1368 p->dl.dl_non_contending = 0;
1369 /*
1370 * If the timer handler is currently running and the
1371 * timer cannot be cancelled, inactive_task_timer()
1372 * will see that dl_not_contending is not set, and
1373 * will not touch the rq's active utilization,
1374 * so we are still safe.
1375 */
1376 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1377 put_task_struct(p);
1378
1379 raw_spin_unlock(&rq->lock);
1380}
1381
Juri Lelli1baca4c2013-11-07 14:43:38 +01001382static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1383{
1384 /*
1385 * Current can't be migrated, useless to reschedule,
1386 * let's hope p can move out.
1387 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001388 if (rq->curr->nr_cpus_allowed == 1 ||
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001389 cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001390 return;
1391
1392 /*
1393 * p is migratable, so let's not schedule it and
1394 * see if it is pushed or pulled somewhere else.
1395 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001396 if (p->nr_cpus_allowed != 1 &&
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001397 cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001398 return;
1399
Kirill Tkhai88751252014-06-29 00:03:57 +04001400 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001401}
1402
1403#endif /* CONFIG_SMP */
1404
Dario Faggioliaab03e02013-11-28 11:14:43 +01001405/*
1406 * Only called when both the current and waking task are -deadline
1407 * tasks.
1408 */
1409static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1410 int flags)
1411{
Juri Lelli1baca4c2013-11-07 14:43:38 +01001412 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001413 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001414 return;
1415 }
1416
1417#ifdef CONFIG_SMP
1418 /*
1419 * In the unlikely case current and p have the same deadline
1420 * let us try to decide what's the best thing to do...
1421 */
Dario Faggioli332ac172013-11-07 14:43:45 +01001422 if ((p->dl.deadline == rq->curr->dl.deadline) &&
1423 !test_tsk_need_resched(rq->curr))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001424 check_preempt_equal_dl(rq, p);
1425#endif /* CONFIG_SMP */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001426}
1427
1428#ifdef CONFIG_SCHED_HRTICK
1429static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1430{
xiaofeng.yan177ef2a2014-08-26 03:15:41 +00001431 hrtick_start(rq, p->dl.runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001432}
Wanpeng Li36ce9882014-11-11 09:52:26 +08001433#else /* !CONFIG_SCHED_HRTICK */
1434static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1435{
1436}
Dario Faggioliaab03e02013-11-28 11:14:43 +01001437#endif
1438
1439static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1440 struct dl_rq *dl_rq)
1441{
1442 struct rb_node *left = dl_rq->rb_leftmost;
1443
1444 if (!left)
1445 return NULL;
1446
1447 return rb_entry(left, struct sched_dl_entity, rb_node);
1448}
1449
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001450struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01001451pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001452{
1453 struct sched_dl_entity *dl_se;
1454 struct task_struct *p;
1455 struct dl_rq *dl_rq;
1456
1457 dl_rq = &rq->dl;
1458
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001459 if (need_pull_dl_task(rq, prev)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001460 /*
1461 * This is OK, because current is on_cpu, which avoids it being
1462 * picked for load-balance and preemption/IRQs are still
1463 * disabled avoiding further scheduler activity on it and we're
1464 * being very careful to re-start the picking loop.
1465 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01001466 rq_unpin_lock(rq, rf);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001467 pull_dl_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01001468 rq_repin_lock(rq, rf);
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001469 /*
T.Zhou176cedc2016-11-23 08:48:32 +08001470 * pull_dl_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001471 * means a stop task can slip in, in which case we need to
1472 * re-start task selection.
1473 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001474 if (rq->stop && task_on_rq_queued(rq->stop))
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001475 return RETRY_TASK;
1476 }
1477
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001478 /*
1479 * When prev is DL, we may throttle it in put_prev_task().
1480 * So, we update time before we check for dl_nr_running.
1481 */
1482 if (prev->sched_class == &dl_sched_class)
1483 update_curr_dl(rq);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001484
Dario Faggioliaab03e02013-11-28 11:14:43 +01001485 if (unlikely(!dl_rq->dl_nr_running))
1486 return NULL;
1487
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001488 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001489
Dario Faggioliaab03e02013-11-28 11:14:43 +01001490 dl_se = pick_next_dl_entity(rq, dl_rq);
1491 BUG_ON(!dl_se);
1492
1493 p = dl_task_of(dl_se);
1494 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001495
1496 /* Running task will never be pushed. */
Juri Lelli71362652014-01-14 12:03:51 +01001497 dequeue_pushable_dl_task(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001498
Dario Faggioliaab03e02013-11-28 11:14:43 +01001499 if (hrtick_enabled(rq))
1500 start_hrtick_dl(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001501
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02001502 queue_push_tasks(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001503
Dario Faggioliaab03e02013-11-28 11:14:43 +01001504 return p;
1505}
1506
1507static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1508{
1509 update_curr_dl(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001510
Ingo Molnar4b53a342017-02-05 15:41:03 +01001511 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001512 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001513}
1514
1515static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1516{
1517 update_curr_dl(rq);
1518
Wanpeng Lia7bebf42014-11-26 08:44:01 +08001519 /*
1520 * Even when we have runtime, update_curr_dl() might have resulted in us
1521 * not being the leftmost task anymore. In that case NEED_RESCHED will
1522 * be set and schedule() will start a new hrtick for the next task.
1523 */
1524 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
1525 is_leftmost(p, &rq->dl))
Dario Faggioliaab03e02013-11-28 11:14:43 +01001526 start_hrtick_dl(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001527}
1528
1529static void task_fork_dl(struct task_struct *p)
1530{
1531 /*
1532 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1533 * sched_fork()
1534 */
1535}
1536
1537static void task_dead_dl(struct task_struct *p)
1538{
Dario Faggioli332ac172013-11-07 14:43:45 +01001539 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1540
1541 /*
1542 * Since we are TASK_DEAD we won't slip out of the domain!
1543 */
1544 raw_spin_lock_irq(&dl_b->lock);
Peter Zijlstra40767b02015-01-28 15:08:03 +01001545 /* XXX we should retain the bw until 0-lag */
Dario Faggioli332ac172013-11-07 14:43:45 +01001546 dl_b->total_bw -= p->dl.dl_bw;
1547 raw_spin_unlock_irq(&dl_b->lock);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001548}
1549
1550static void set_curr_task_dl(struct rq *rq)
1551{
1552 struct task_struct *p = rq->curr;
1553
1554 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001555
1556 /* You can't push away the running task */
1557 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001558}
1559
Juri Lelli1baca4c2013-11-07 14:43:38 +01001560#ifdef CONFIG_SMP
1561
1562/* Only try algorithms three times */
1563#define DL_MAX_TRIES 3
1564
1565static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1566{
1567 if (!task_running(rq, p) &&
Ingo Molnar0c98d342017-02-05 15:38:10 +01001568 cpumask_test_cpu(cpu, &p->cpus_allowed))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001569 return 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001570 return 0;
1571}
1572
Wanpeng Li8b5e7702015-05-13 14:01:01 +08001573/*
1574 * Return the earliest pushable rq's task, which is suitable to be executed
1575 * on the CPU, NULL otherwise:
1576 */
1577static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
1578{
1579 struct rb_node *next_node = rq->dl.pushable_dl_tasks_leftmost;
1580 struct task_struct *p = NULL;
1581
1582 if (!has_pushable_dl_tasks(rq))
1583 return NULL;
1584
1585next_node:
1586 if (next_node) {
1587 p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1588
1589 if (pick_dl_task(rq, p, cpu))
1590 return p;
1591
1592 next_node = rb_next(next_node);
1593 goto next_node;
1594 }
1595
1596 return NULL;
1597}
1598
Juri Lelli1baca4c2013-11-07 14:43:38 +01001599static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1600
1601static int find_later_rq(struct task_struct *task)
1602{
1603 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001604 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001605 int this_cpu = smp_processor_id();
1606 int best_cpu, cpu = task_cpu(task);
1607
1608 /* Make sure the mask is initialized first */
1609 if (unlikely(!later_mask))
1610 return -1;
1611
Ingo Molnar4b53a342017-02-05 15:41:03 +01001612 if (task->nr_cpus_allowed == 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001613 return -1;
1614
Juri Lelli91ec6772014-09-19 10:22:41 +01001615 /*
1616 * We have to consider system topology and task affinity
1617 * first, then we can look for a suitable cpu.
1618 */
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001619 best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
1620 task, later_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001621 if (best_cpu == -1)
1622 return -1;
1623
1624 /*
1625 * If we are here, some target has been found,
1626 * the most suitable of which is cached in best_cpu.
1627 * This is, among the runqueues where the current tasks
1628 * have later deadlines than the task's one, the rq
1629 * with the latest possible one.
1630 *
1631 * Now we check how well this matches with task's
1632 * affinity and system topology.
1633 *
1634 * The last cpu where the task run is our first
1635 * guess, since it is most likely cache-hot there.
1636 */
1637 if (cpumask_test_cpu(cpu, later_mask))
1638 return cpu;
1639 /*
1640 * Check if this_cpu is to be skipped (i.e., it is
1641 * not in the mask) or not.
1642 */
1643 if (!cpumask_test_cpu(this_cpu, later_mask))
1644 this_cpu = -1;
1645
1646 rcu_read_lock();
1647 for_each_domain(cpu, sd) {
1648 if (sd->flags & SD_WAKE_AFFINE) {
1649
1650 /*
1651 * If possible, preempting this_cpu is
1652 * cheaper than migrating.
1653 */
1654 if (this_cpu != -1 &&
1655 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1656 rcu_read_unlock();
1657 return this_cpu;
1658 }
1659
1660 /*
1661 * Last chance: if best_cpu is valid and is
1662 * in the mask, that becomes our choice.
1663 */
1664 if (best_cpu < nr_cpu_ids &&
1665 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
1666 rcu_read_unlock();
1667 return best_cpu;
1668 }
1669 }
1670 }
1671 rcu_read_unlock();
1672
1673 /*
1674 * At this point, all our guesses failed, we just return
1675 * 'something', and let the caller sort the things out.
1676 */
1677 if (this_cpu != -1)
1678 return this_cpu;
1679
1680 cpu = cpumask_any(later_mask);
1681 if (cpu < nr_cpu_ids)
1682 return cpu;
1683
1684 return -1;
1685}
1686
1687/* Locks the rq it finds */
1688static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1689{
1690 struct rq *later_rq = NULL;
1691 int tries;
1692 int cpu;
1693
1694 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1695 cpu = find_later_rq(task);
1696
1697 if ((cpu == -1) || (cpu == rq->cpu))
1698 break;
1699
1700 later_rq = cpu_rq(cpu);
1701
Luca Abeni5aa50502015-10-16 10:06:21 +02001702 if (later_rq->dl.dl_nr_running &&
1703 !dl_time_before(task->dl.deadline,
Wanpeng Li9d514262015-05-13 14:01:03 +08001704 later_rq->dl.earliest_dl.curr)) {
1705 /*
1706 * Target rq has tasks of equal or earlier deadline,
1707 * retrying does not release any lock and is unlikely
1708 * to yield a different result.
1709 */
1710 later_rq = NULL;
1711 break;
1712 }
1713
Juri Lelli1baca4c2013-11-07 14:43:38 +01001714 /* Retry if something changed. */
1715 if (double_lock_balance(rq, later_rq)) {
1716 if (unlikely(task_rq(task) != rq ||
Ingo Molnar0c98d342017-02-05 15:38:10 +01001717 !cpumask_test_cpu(later_rq->cpu, &task->cpus_allowed) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001718 task_running(rq, task) ||
Xunlei Pang13b5ab02016-05-09 12:11:31 +08001719 !dl_task(task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001720 !task_on_rq_queued(task))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001721 double_unlock_balance(rq, later_rq);
1722 later_rq = NULL;
1723 break;
1724 }
1725 }
1726
1727 /*
1728 * If the rq we found has no -deadline task, or
1729 * its earliest one has a later deadline than our
1730 * task, the rq is a good one.
1731 */
1732 if (!later_rq->dl.dl_nr_running ||
1733 dl_time_before(task->dl.deadline,
1734 later_rq->dl.earliest_dl.curr))
1735 break;
1736
1737 /* Otherwise we try again. */
1738 double_unlock_balance(rq, later_rq);
1739 later_rq = NULL;
1740 }
1741
1742 return later_rq;
1743}
1744
1745static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1746{
1747 struct task_struct *p;
1748
1749 if (!has_pushable_dl_tasks(rq))
1750 return NULL;
1751
1752 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
1753 struct task_struct, pushable_dl_tasks);
1754
1755 BUG_ON(rq->cpu != task_cpu(p));
1756 BUG_ON(task_current(rq, p));
Ingo Molnar4b53a342017-02-05 15:41:03 +01001757 BUG_ON(p->nr_cpus_allowed <= 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001758
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001759 BUG_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001760 BUG_ON(!dl_task(p));
1761
1762 return p;
1763}
1764
1765/*
1766 * See if the non running -deadline tasks on this rq
1767 * can be sent to some other CPU where they can preempt
1768 * and start executing.
1769 */
1770static int push_dl_task(struct rq *rq)
1771{
1772 struct task_struct *next_task;
1773 struct rq *later_rq;
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08001774 int ret = 0;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001775
1776 if (!rq->dl.overloaded)
1777 return 0;
1778
1779 next_task = pick_next_pushable_dl_task(rq);
1780 if (!next_task)
1781 return 0;
1782
1783retry:
1784 if (unlikely(next_task == rq->curr)) {
1785 WARN_ON(1);
1786 return 0;
1787 }
1788
1789 /*
1790 * If next_task preempts rq->curr, and rq->curr
1791 * can move away, it makes sense to just reschedule
1792 * without going further in pushing next_task.
1793 */
1794 if (dl_task(rq->curr) &&
1795 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001796 rq->curr->nr_cpus_allowed > 1) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001797 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001798 return 0;
1799 }
1800
1801 /* We might release rq lock */
1802 get_task_struct(next_task);
1803
1804 /* Will lock the rq it'll find */
1805 later_rq = find_lock_later_rq(next_task, rq);
1806 if (!later_rq) {
1807 struct task_struct *task;
1808
1809 /*
1810 * We must check all this again, since
1811 * find_lock_later_rq releases rq->lock and it is
1812 * then possible that next_task has migrated.
1813 */
1814 task = pick_next_pushable_dl_task(rq);
Byungchul Parka776b962017-05-12 10:05:59 +09001815 if (task == next_task) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001816 /*
1817 * The task is still there. We don't try
1818 * again, some other cpu will pull it when ready.
1819 */
Juri Lelli1baca4c2013-11-07 14:43:38 +01001820 goto out;
1821 }
1822
1823 if (!task)
1824 /* No more tasks */
1825 goto out;
1826
1827 put_task_struct(next_task);
1828 next_task = task;
1829 goto retry;
1830 }
1831
1832 deactivate_task(rq, next_task, 0);
Luca Abenie36d8672017-05-18 22:13:28 +02001833 sub_running_bw(next_task->dl.dl_bw, &rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001834 set_task_cpu(next_task, later_rq->cpu);
Luca Abenie36d8672017-05-18 22:13:28 +02001835 add_running_bw(next_task->dl.dl_bw, &later_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001836 activate_task(later_rq, next_task, 0);
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08001837 ret = 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001838
Kirill Tkhai88751252014-06-29 00:03:57 +04001839 resched_curr(later_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001840
1841 double_unlock_balance(rq, later_rq);
1842
1843out:
1844 put_task_struct(next_task);
1845
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08001846 return ret;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001847}
1848
1849static void push_dl_tasks(struct rq *rq)
1850{
Andrea Parri4ffa08e2015-08-05 15:56:18 +02001851 /* push_dl_task() will return true if it moved a -deadline task */
Juri Lelli1baca4c2013-11-07 14:43:38 +01001852 while (push_dl_task(rq))
1853 ;
1854}
1855
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001856static void pull_dl_task(struct rq *this_rq)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001857{
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001858 int this_cpu = this_rq->cpu, cpu;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001859 struct task_struct *p;
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001860 bool resched = false;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001861 struct rq *src_rq;
1862 u64 dmin = LONG_MAX;
1863
1864 if (likely(!dl_overloaded(this_rq)))
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001865 return;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001866
1867 /*
1868 * Match the barrier from dl_set_overloaded; this guarantees that if we
1869 * see overloaded we must also see the dlo_mask bit.
1870 */
1871 smp_rmb();
1872
1873 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
1874 if (this_cpu == cpu)
1875 continue;
1876
1877 src_rq = cpu_rq(cpu);
1878
1879 /*
1880 * It looks racy, abd it is! However, as in sched_rt.c,
1881 * we are fine with this.
1882 */
1883 if (this_rq->dl.dl_nr_running &&
1884 dl_time_before(this_rq->dl.earliest_dl.curr,
1885 src_rq->dl.earliest_dl.next))
1886 continue;
1887
1888 /* Might drop this_rq->lock */
1889 double_lock_balance(this_rq, src_rq);
1890
1891 /*
1892 * If there are no more pullable tasks on the
1893 * rq, we're done with it.
1894 */
1895 if (src_rq->dl.dl_nr_running <= 1)
1896 goto skip;
1897
Wanpeng Li8b5e7702015-05-13 14:01:01 +08001898 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001899
1900 /*
1901 * We found a task to be pulled if:
1902 * - it preempts our current (if there's one),
1903 * - it will preempt the last one we pulled (if any).
1904 */
1905 if (p && dl_time_before(p->dl.deadline, dmin) &&
1906 (!this_rq->dl.dl_nr_running ||
1907 dl_time_before(p->dl.deadline,
1908 this_rq->dl.earliest_dl.curr))) {
1909 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001910 WARN_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001911
1912 /*
1913 * Then we pull iff p has actually an earlier
1914 * deadline than the current task of its runqueue.
1915 */
1916 if (dl_time_before(p->dl.deadline,
1917 src_rq->curr->dl.deadline))
1918 goto skip;
1919
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001920 resched = true;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001921
1922 deactivate_task(src_rq, p, 0);
Luca Abenie36d8672017-05-18 22:13:28 +02001923 sub_running_bw(p->dl.dl_bw, &src_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001924 set_task_cpu(p, this_cpu);
Luca Abenie36d8672017-05-18 22:13:28 +02001925 add_running_bw(p->dl.dl_bw, &this_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001926 activate_task(this_rq, p, 0);
1927 dmin = p->dl.deadline;
1928
1929 /* Is there any other task even earlier? */
1930 }
1931skip:
1932 double_unlock_balance(this_rq, src_rq);
1933 }
1934
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001935 if (resched)
1936 resched_curr(this_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001937}
1938
1939/*
1940 * Since the task is not running and a reschedule is not going to happen
1941 * anytime soon on its runqueue, we try pushing it away now.
1942 */
1943static void task_woken_dl(struct rq *rq, struct task_struct *p)
1944{
1945 if (!task_running(rq, p) &&
1946 !test_tsk_need_resched(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001947 p->nr_cpus_allowed > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01001948 dl_task(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001949 (rq->curr->nr_cpus_allowed < 2 ||
Wanpeng Li6b0a5632014-10-31 06:39:34 +08001950 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001951 push_dl_tasks(rq);
1952 }
1953}
1954
1955static void set_cpus_allowed_dl(struct task_struct *p,
1956 const struct cpumask *new_mask)
1957{
Juri Lelli7f514122014-09-19 10:22:40 +01001958 struct root_domain *src_rd;
Peter Zijlstra6c370672015-05-15 17:43:36 +02001959 struct rq *rq;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001960
1961 BUG_ON(!dl_task(p));
1962
Juri Lelli7f514122014-09-19 10:22:40 +01001963 rq = task_rq(p);
1964 src_rd = rq->rd;
1965 /*
1966 * Migrating a SCHED_DEADLINE task between exclusive
1967 * cpusets (different root_domains) entails a bandwidth
1968 * update. We already made space for us in the destination
1969 * domain (see cpuset_can_attach()).
1970 */
1971 if (!cpumask_intersects(src_rd->span, new_mask)) {
1972 struct dl_bw *src_dl_b;
1973
1974 src_dl_b = dl_bw_of(cpu_of(rq));
1975 /*
1976 * We now free resources of the root_domain we are migrating
1977 * off. In the worst case, sched_setattr() may temporary fail
1978 * until we complete the update.
1979 */
1980 raw_spin_lock(&src_dl_b->lock);
1981 __dl_clear(src_dl_b, p->dl.dl_bw);
1982 raw_spin_unlock(&src_dl_b->lock);
1983 }
1984
Peter Zijlstra6c370672015-05-15 17:43:36 +02001985 set_cpus_allowed_common(p, new_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001986}
1987
1988/* Assumes rq->lock is held */
1989static void rq_online_dl(struct rq *rq)
1990{
1991 if (rq->dl.overloaded)
1992 dl_set_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001993
Xunlei Pang16b26942015-01-19 04:49:36 +00001994 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001995 if (rq->dl.dl_nr_running > 0)
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001996 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001997}
1998
1999/* Assumes rq->lock is held */
2000static void rq_offline_dl(struct rq *rq)
2001{
2002 if (rq->dl.overloaded)
2003 dl_clear_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002004
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02002005 cpudl_clear(&rq->rd->cpudl, rq->cpu);
Xunlei Pang16b26942015-01-19 04:49:36 +00002006 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002007}
2008
Wanpeng Lia6c0e742015-05-13 14:01:02 +08002009void __init init_sched_dl_class(void)
Juri Lelli1baca4c2013-11-07 14:43:38 +01002010{
2011 unsigned int i;
2012
2013 for_each_possible_cpu(i)
2014 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2015 GFP_KERNEL, cpu_to_node(i));
2016}
2017
2018#endif /* CONFIG_SMP */
2019
Dario Faggioliaab03e02013-11-28 11:14:43 +01002020static void switched_from_dl(struct rq *rq, struct task_struct *p)
2021{
Peter Zijlstraa649f232015-06-11 14:46:49 +02002022 /*
Luca Abeni209a0cb2017-05-18 22:13:29 +02002023 * task_non_contending() can start the "inactive timer" (if the 0-lag
2024 * time is in the future). If the task switches back to dl before
2025 * the "inactive timer" fires, it can continue to consume its current
2026 * runtime using its current deadline. If it stays outside of
2027 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2028 * will reset the task parameters.
Peter Zijlstraa649f232015-06-11 14:46:49 +02002029 */
Luca Abeni209a0cb2017-05-18 22:13:29 +02002030 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2031 task_non_contending(p);
2032
2033 /*
2034 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2035 * at the 0-lag time, because the task could have been migrated
2036 * while SCHED_OTHER in the meanwhile.
2037 */
2038 if (p->dl.dl_non_contending)
2039 p->dl.dl_non_contending = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002040
Juri Lelli1baca4c2013-11-07 14:43:38 +01002041 /*
2042 * Since this might be the only -deadline task on the rq,
2043 * this is the right place to try to pull some other one
2044 * from an overloaded cpu, if any.
2045 */
Wanpeng Licd660912014-10-31 06:39:35 +08002046 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2047 return;
2048
Peter Zijlstra9916e212015-06-11 14:46:43 +02002049 queue_pull_task(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002050}
2051
Juri Lelli1baca4c2013-11-07 14:43:38 +01002052/*
2053 * When switching to -deadline, we may overload the rq, then
2054 * we try to push someone off, if possible.
2055 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002056static void switched_to_dl(struct rq *rq, struct task_struct *p)
2057{
Luca Abeni209a0cb2017-05-18 22:13:29 +02002058 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2059 put_task_struct(p);
Luca Abeni72f9f3f2016-03-07 12:27:04 +01002060
Juri Lelli98b0a852016-08-05 16:07:55 +01002061 /* If p is not queued we will update its parameters at next wakeup. */
2062 if (!task_on_rq_queued(p))
2063 return;
2064
2065 /*
2066 * If p is boosted we already updated its params in
2067 * rt_mutex_setprio()->enqueue_task(..., ENQUEUE_REPLENISH),
2068 * p's deadline being now already after rq_clock(rq).
2069 */
2070 if (dl_time_before(p->dl.deadline, rq_clock(rq)))
2071 setup_new_dl_entity(&p->dl);
2072
2073 if (rq->curr != p) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01002074#ifdef CONFIG_SMP
Ingo Molnar4b53a342017-02-05 15:41:03 +01002075 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
Peter Zijlstra9916e212015-06-11 14:46:43 +02002076 queue_push_tasks(rq);
Sebastian Andrzej Siewior619bd4a2017-01-24 15:40:06 +01002077#endif
Peter Zijlstra9916e212015-06-11 14:46:43 +02002078 if (dl_task(rq->curr))
2079 check_preempt_curr_dl(rq, p, 0);
2080 else
2081 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002082 }
2083}
2084
Juri Lelli1baca4c2013-11-07 14:43:38 +01002085/*
2086 * If the scheduling parameters of a -deadline task changed,
2087 * a push or pull operation might be needed.
2088 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002089static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2090 int oldprio)
2091{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002092 if (task_on_rq_queued(p) || rq->curr == p) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002093#ifdef CONFIG_SMP
Juri Lelli1baca4c2013-11-07 14:43:38 +01002094 /*
2095 * This might be too much, but unfortunately
2096 * we don't have the old deadline value, and
2097 * we can't argue if the task is increasing
2098 * or lowering its prio, so...
2099 */
2100 if (!rq->dl.overloaded)
Peter Zijlstra9916e212015-06-11 14:46:43 +02002101 queue_pull_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002102
2103 /*
2104 * If we now have a earlier deadline task than p,
2105 * then reschedule, provided p is still on this
2106 * runqueue.
2107 */
Peter Zijlstra9916e212015-06-11 14:46:43 +02002108 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
Kirill Tkhai88751252014-06-29 00:03:57 +04002109 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002110#else
2111 /*
2112 * Again, we don't know if p has a earlier
2113 * or later deadline, so let's blindly set a
2114 * (maybe not needed) rescheduling point.
2115 */
Kirill Tkhai88751252014-06-29 00:03:57 +04002116 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002117#endif /* CONFIG_SMP */
Peter Zijlstra801ccdb2016-02-25 15:01:49 +01002118 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01002119}
Dario Faggioliaab03e02013-11-28 11:14:43 +01002120
2121const struct sched_class dl_sched_class = {
2122 .next = &rt_sched_class,
2123 .enqueue_task = enqueue_task_dl,
2124 .dequeue_task = dequeue_task_dl,
2125 .yield_task = yield_task_dl,
2126
2127 .check_preempt_curr = check_preempt_curr_dl,
2128
2129 .pick_next_task = pick_next_task_dl,
2130 .put_prev_task = put_prev_task_dl,
2131
2132#ifdef CONFIG_SMP
2133 .select_task_rq = select_task_rq_dl,
Luca Abeni209a0cb2017-05-18 22:13:29 +02002134 .migrate_task_rq = migrate_task_rq_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01002135 .set_cpus_allowed = set_cpus_allowed_dl,
2136 .rq_online = rq_online_dl,
2137 .rq_offline = rq_offline_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01002138 .task_woken = task_woken_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002139#endif
2140
2141 .set_curr_task = set_curr_task_dl,
2142 .task_tick = task_tick_dl,
2143 .task_fork = task_fork_dl,
2144 .task_dead = task_dead_dl,
2145
2146 .prio_changed = prio_changed_dl,
2147 .switched_from = switched_from_dl,
2148 .switched_to = switched_to_dl,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002149
2150 .update_curr = update_curr_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002151};
Wanpeng Liacb32132014-10-31 06:39:33 +08002152
2153#ifdef CONFIG_SCHED_DEBUG
2154extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2155
2156void print_dl_stats(struct seq_file *m, int cpu)
2157{
2158 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
2159}
2160#endif /* CONFIG_SCHED_DEBUG */