blob: 0bee537554f6dd79ef30868aa9115a0a84eab5e4 [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);
Luca Abeni387e3132017-05-18 22:13:30 +0200178 if (!dl_task(p) || p->state == TASK_DEAD) {
179 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
180
181 raw_spin_lock(&dl_b->lock);
182 __dl_clear(dl_b, p->dl.dl_bw);
Luca Abeni209a0cb2017-05-18 22:13:29 +0200183 __dl_clear_params(p);
Luca Abeni387e3132017-05-18 22:13:30 +0200184 raw_spin_unlock(&dl_b->lock);
185 }
Luca Abeni209a0cb2017-05-18 22:13:29 +0200186
187 return;
188 }
189
190 dl_se->dl_non_contending = 1;
191 get_task_struct(p);
192 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL);
193}
194
195static void task_contending(struct sched_dl_entity *dl_se)
196{
197 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
198
199 /*
200 * If this is a non-deadline task that has been boosted,
201 * do nothing
202 */
203 if (dl_se->dl_runtime == 0)
204 return;
205
206 if (dl_se->dl_non_contending) {
207 dl_se->dl_non_contending = 0;
208 /*
209 * If the timer handler is currently running and the
210 * timer cannot be cancelled, inactive_task_timer()
211 * will see that dl_not_contending is not set, and
212 * will not touch the rq's active utilization,
213 * so we are still safe.
214 */
215 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1)
216 put_task_struct(dl_task_of(dl_se));
217 } else {
218 /*
219 * Since "dl_non_contending" is not set, the
220 * task's utilization has already been removed from
221 * active utilization (either when the task blocked,
222 * when the "inactive timer" fired).
223 * So, add it back.
224 */
225 add_running_bw(dl_se->dl_bw, dl_rq);
226 }
227}
228
Dario Faggioliaab03e02013-11-28 11:14:43 +0100229static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
230{
231 struct sched_dl_entity *dl_se = &p->dl;
232
233 return dl_rq->rb_leftmost == &dl_se->rb_node;
234}
235
Dario Faggioli332ac172013-11-07 14:43:45 +0100236void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
237{
238 raw_spin_lock_init(&dl_b->dl_runtime_lock);
239 dl_b->dl_period = period;
240 dl_b->dl_runtime = runtime;
241}
242
Dario Faggioli332ac172013-11-07 14:43:45 +0100243void init_dl_bw(struct dl_bw *dl_b)
244{
245 raw_spin_lock_init(&dl_b->lock);
246 raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
Peter Zijlstra17248132013-12-17 12:44:49 +0100247 if (global_rt_runtime() == RUNTIME_INF)
Dario Faggioli332ac172013-11-07 14:43:45 +0100248 dl_b->bw = -1;
249 else
Peter Zijlstra17248132013-12-17 12:44:49 +0100250 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +0100251 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
252 dl_b->total_bw = 0;
253}
254
Abel Vesa07c54f72015-03-03 13:50:27 +0200255void init_dl_rq(struct dl_rq *dl_rq)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100256{
257 dl_rq->rb_root = RB_ROOT;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100258
259#ifdef CONFIG_SMP
260 /* zero means no -deadline tasks */
261 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
262
263 dl_rq->dl_nr_migratory = 0;
264 dl_rq->overloaded = 0;
265 dl_rq->pushable_dl_tasks_root = RB_ROOT;
Dario Faggioli332ac172013-11-07 14:43:45 +0100266#else
267 init_dl_bw(&dl_rq->dl_bw);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100268#endif
Luca Abenie36d8672017-05-18 22:13:28 +0200269
270 dl_rq->running_bw = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100271}
272
Juri Lelli1baca4c2013-11-07 14:43:38 +0100273#ifdef CONFIG_SMP
274
275static inline int dl_overloaded(struct rq *rq)
276{
277 return atomic_read(&rq->rd->dlo_count);
278}
279
280static inline void dl_set_overload(struct rq *rq)
281{
282 if (!rq->online)
283 return;
284
285 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
286 /*
287 * Must be visible before the overload count is
288 * set (as in sched_rt.c).
289 *
290 * Matched by the barrier in pull_dl_task().
291 */
292 smp_wmb();
293 atomic_inc(&rq->rd->dlo_count);
294}
295
296static inline void dl_clear_overload(struct rq *rq)
297{
298 if (!rq->online)
299 return;
300
301 atomic_dec(&rq->rd->dlo_count);
302 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
303}
304
305static void update_dl_migration(struct dl_rq *dl_rq)
306{
Kirill Tkhai995b9ea2014-02-18 02:24:13 +0400307 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100308 if (!dl_rq->overloaded) {
309 dl_set_overload(rq_of_dl_rq(dl_rq));
310 dl_rq->overloaded = 1;
311 }
312 } else if (dl_rq->overloaded) {
313 dl_clear_overload(rq_of_dl_rq(dl_rq));
314 dl_rq->overloaded = 0;
315 }
316}
317
318static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
319{
320 struct task_struct *p = dl_task_of(dl_se);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100321
Ingo Molnar4b53a342017-02-05 15:41:03 +0100322 if (p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100323 dl_rq->dl_nr_migratory++;
324
325 update_dl_migration(dl_rq);
326}
327
328static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
329{
330 struct task_struct *p = dl_task_of(dl_se);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100331
Ingo Molnar4b53a342017-02-05 15:41:03 +0100332 if (p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100333 dl_rq->dl_nr_migratory--;
334
335 update_dl_migration(dl_rq);
336}
337
338/*
339 * The list of pushable -deadline task is not a plist, like in
340 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
341 */
342static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
343{
344 struct dl_rq *dl_rq = &rq->dl;
345 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
346 struct rb_node *parent = NULL;
347 struct task_struct *entry;
348 int leftmost = 1;
349
350 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
351
352 while (*link) {
353 parent = *link;
354 entry = rb_entry(parent, struct task_struct,
355 pushable_dl_tasks);
356 if (dl_entity_preempt(&p->dl, &entry->dl))
357 link = &parent->rb_left;
358 else {
359 link = &parent->rb_right;
360 leftmost = 0;
361 }
362 }
363
Wanpeng Li7d92de32015-12-03 17:42:10 +0800364 if (leftmost) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100365 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
Wanpeng Li7d92de32015-12-03 17:42:10 +0800366 dl_rq->earliest_dl.next = p->dl.deadline;
367 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100368
369 rb_link_node(&p->pushable_dl_tasks, parent, link);
370 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
371}
372
373static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
374{
375 struct dl_rq *dl_rq = &rq->dl;
376
377 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
378 return;
379
380 if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
381 struct rb_node *next_node;
382
383 next_node = rb_next(&p->pushable_dl_tasks);
384 dl_rq->pushable_dl_tasks_leftmost = next_node;
Wanpeng Li7d92de32015-12-03 17:42:10 +0800385 if (next_node) {
386 dl_rq->earliest_dl.next = rb_entry(next_node,
387 struct task_struct, pushable_dl_tasks)->dl.deadline;
388 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100389 }
390
391 rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
392 RB_CLEAR_NODE(&p->pushable_dl_tasks);
393}
394
395static inline int has_pushable_dl_tasks(struct rq *rq)
396{
397 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
398}
399
400static int push_dl_task(struct rq *rq);
401
Peter Zijlstradc877342014-02-12 15:47:29 +0100402static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
403{
404 return dl_task(prev);
405}
406
Peter Zijlstra9916e212015-06-11 14:46:43 +0200407static DEFINE_PER_CPU(struct callback_head, dl_push_head);
408static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200409
410static void push_dl_tasks(struct rq *);
Peter Zijlstra9916e212015-06-11 14:46:43 +0200411static void pull_dl_task(struct rq *);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200412
413static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100414{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200415 if (!has_pushable_dl_tasks(rq))
416 return;
417
Peter Zijlstra9916e212015-06-11 14:46:43 +0200418 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
419}
420
421static inline void queue_pull_task(struct rq *rq)
422{
423 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
Peter Zijlstradc877342014-02-12 15:47:29 +0100424}
425
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800426static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
427
Peter Zijlstraa649f232015-06-11 14:46:49 +0200428static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800429{
430 struct rq *later_rq = NULL;
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800431
432 later_rq = find_lock_later_rq(p, rq);
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800433 if (!later_rq) {
434 int cpu;
435
436 /*
437 * If we cannot preempt any rq, fall back to pick any
438 * online cpu.
439 */
Ingo Molnar0c98d342017-02-05 15:38:10 +0100440 cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800441 if (cpu >= nr_cpu_ids) {
442 /*
443 * Fail to find any suitable cpu.
444 * The task will never come back!
445 */
446 BUG_ON(dl_bandwidth_enabled());
447
448 /*
449 * If admission control is disabled we
450 * try a little harder to let the task
451 * run.
452 */
453 cpu = cpumask_any(cpu_active_mask);
454 }
455 later_rq = cpu_rq(cpu);
456 double_lock_balance(rq, later_rq);
457 }
458
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800459 set_task_cpu(p, later_rq->cpu);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200460 double_unlock_balance(later_rq, rq);
461
462 return later_rq;
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800463}
464
Juri Lelli1baca4c2013-11-07 14:43:38 +0100465#else
466
467static inline
468void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
469{
470}
471
472static inline
473void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
474{
475}
476
477static inline
478void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
479{
480}
481
482static inline
483void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
484{
485}
486
Peter Zijlstradc877342014-02-12 15:47:29 +0100487static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
488{
489 return false;
490}
491
Peter Zijlstra0ea60c22015-06-11 14:46:42 +0200492static inline void pull_dl_task(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100493{
Peter Zijlstradc877342014-02-12 15:47:29 +0100494}
495
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200496static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100497{
498}
Peter Zijlstra9916e212015-06-11 14:46:43 +0200499
500static inline void queue_pull_task(struct rq *rq)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100501{
502}
503#endif /* CONFIG_SMP */
504
Dario Faggioliaab03e02013-11-28 11:14:43 +0100505static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
506static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
507static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
508 int flags);
509
510/*
511 * We are being explicitly informed that a new instance is starting,
512 * and this means that:
513 * - the absolute deadline of the entity has to be placed at
514 * current time + relative deadline;
515 * - the runtime of the entity has to be set to the maximum value.
516 *
517 * The capability of specifying such event is useful whenever a -deadline
518 * entity wants to (try to!) synchronize its behaviour with the scheduler's
519 * one, and to (try to!) reconcile itself with its own scheduling
520 * parameters.
521 */
Juri Lelli98b0a852016-08-05 16:07:55 +0100522static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100523{
524 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
525 struct rq *rq = rq_of_dl_rq(dl_rq);
526
Juri Lelli98b0a852016-08-05 16:07:55 +0100527 WARN_ON(dl_se->dl_boosted);
Luca Abeni72f9f3f2016-03-07 12:27:04 +0100528 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
529
530 /*
531 * We are racing with the deadline timer. So, do nothing because
532 * the deadline timer handler will take care of properly recharging
533 * the runtime and postponing the deadline
534 */
535 if (dl_se->dl_throttled)
536 return;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100537
538 /*
539 * We use the regular wall clock time to set deadlines in the
540 * future; in fact, we must consider execution overheads (time
541 * spent on hardirq context, etc.).
542 */
Juri Lelli98b0a852016-08-05 16:07:55 +0100543 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
544 dl_se->runtime = dl_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100545}
546
547/*
548 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
549 * possibility of a entity lasting more than what it declared, and thus
550 * exhausting its runtime.
551 *
552 * Here we are interested in making runtime overrun possible, but we do
553 * not want a entity which is misbehaving to affect the scheduling of all
554 * other entities.
555 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
556 * is used, in order to confine each entity within its own bandwidth.
557 *
558 * This function deals exactly with that, and ensures that when the runtime
559 * of a entity is replenished, its deadline is also postponed. That ensures
560 * the overrunning entity can't interfere with other entity in the system and
561 * can't make them miss their deadlines. Reasons why this kind of overruns
562 * could happen are, typically, a entity voluntarily trying to overcome its
xiaofeng.yan1b09d292014-07-07 05:59:04 +0000563 * runtime, or it just underestimated it during sched_setattr().
Dario Faggioliaab03e02013-11-28 11:14:43 +0100564 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100565static void replenish_dl_entity(struct sched_dl_entity *dl_se,
566 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100567{
568 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
569 struct rq *rq = rq_of_dl_rq(dl_rq);
570
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100571 BUG_ON(pi_se->dl_runtime <= 0);
572
573 /*
574 * This could be the case for a !-dl task that is boosted.
575 * Just go with full inherited parameters.
576 */
577 if (dl_se->dl_deadline == 0) {
578 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
579 dl_se->runtime = pi_se->dl_runtime;
580 }
581
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100582 if (dl_se->dl_yielded && dl_se->runtime > 0)
583 dl_se->runtime = 0;
584
Dario Faggioliaab03e02013-11-28 11:14:43 +0100585 /*
586 * We keep moving the deadline away until we get some
587 * available runtime for the entity. This ensures correct
588 * handling of situations where the runtime overrun is
589 * arbitrary large.
590 */
591 while (dl_se->runtime <= 0) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100592 dl_se->deadline += pi_se->dl_period;
593 dl_se->runtime += pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100594 }
595
596 /*
597 * At this point, the deadline really should be "in
598 * the future" with respect to rq->clock. If it's
599 * not, we are, for some reason, lagging too much!
600 * Anyway, after having warn userspace abut that,
601 * we still try to keep the things running by
602 * resetting the deadline and the budget of the
603 * entity.
604 */
605 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
Steven Rostedtc219b7d2016-02-10 12:04:22 -0500606 printk_deferred_once("sched: DL replenish lagged too much\n");
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100607 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
608 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100609 }
Peter Zijlstra1019a352014-11-26 08:44:03 +0800610
611 if (dl_se->dl_yielded)
612 dl_se->dl_yielded = 0;
613 if (dl_se->dl_throttled)
614 dl_se->dl_throttled = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100615}
616
617/*
618 * Here we check if --at time t-- an entity (which is probably being
619 * [re]activated or, in general, enqueued) can use its remaining runtime
620 * and its current deadline _without_ exceeding the bandwidth it is
621 * assigned (function returns true if it can't). We are in fact applying
622 * one of the CBS rules: when a task wakes up, if the residual runtime
623 * over residual deadline fits within the allocated bandwidth, then we
624 * can keep the current (absolute) deadline and residual budget without
625 * disrupting the schedulability of the system. Otherwise, we should
626 * refill the runtime and set the deadline a period in the future,
627 * because keeping the current (absolute) deadline of the task would
Dario Faggioli712e5e32014-01-27 12:20:15 +0100628 * result in breaking guarantees promised to other tasks (refer to
629 * Documentation/scheduler/sched-deadline.txt for more informations).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100630 *
631 * This function returns true if:
632 *
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100633 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
Dario Faggioliaab03e02013-11-28 11:14:43 +0100634 *
635 * IOW we can't recycle current parameters.
Harald Gustafsson755378a2013-11-07 14:43:40 +0100636 *
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100637 * Notice that the bandwidth check is done against the deadline. For
Harald Gustafsson755378a2013-11-07 14:43:40 +0100638 * task with deadline equal to period this is the same of using
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100639 * dl_period instead of dl_deadline in the equation above.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100640 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100641static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
642 struct sched_dl_entity *pi_se, u64 t)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100643{
644 u64 left, right;
645
646 /*
647 * left and right are the two sides of the equation above,
648 * after a bit of shuffling to use multiplications instead
649 * of divisions.
650 *
651 * Note that none of the time values involved in the two
652 * multiplications are absolute: dl_deadline and dl_runtime
653 * are the relative deadline and the maximum runtime of each
654 * instance, runtime is the runtime left for the last instance
655 * and (deadline - t), since t is rq->clock, is the time left
656 * to the (absolute) deadline. Even if overflowing the u64 type
657 * is very unlikely to occur in both cases, here we scale down
658 * as we want to avoid that risk at all. Scaling down by 10
659 * means that we reduce granularity to 1us. We are fine with it,
660 * since this is only a true/false check and, anyway, thinking
661 * of anything below microseconds resolution is actually fiction
662 * (but still we want to give the user that illusion >;).
663 */
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100664 left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
Dario Faggioli332ac172013-11-07 14:43:45 +0100665 right = ((dl_se->deadline - t) >> DL_SCALE) *
666 (pi_se->dl_runtime >> DL_SCALE);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100667
668 return dl_time_before(right, left);
669}
670
671/*
672 * When a -deadline entity is queued back on the runqueue, its runtime and
673 * deadline might need updating.
674 *
675 * The policy here is that we update the deadline of the entity only if:
676 * - the current deadline is in the past,
677 * - using the remaining runtime with the current deadline would make
678 * the entity exceed its bandwidth.
679 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100680static void update_dl_entity(struct sched_dl_entity *dl_se,
681 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100682{
683 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
684 struct rq *rq = rq_of_dl_rq(dl_rq);
685
Dario Faggioliaab03e02013-11-28 11:14:43 +0100686 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100687 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
688 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
689 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100690 }
691}
692
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100693static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
694{
695 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
696}
697
Dario Faggioliaab03e02013-11-28 11:14:43 +0100698/*
699 * If the entity depleted all its runtime, and if we want it to sleep
700 * while waiting for some new execution time to become available, we
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100701 * set the bandwidth replenishment timer to the replenishment instant
Dario Faggioliaab03e02013-11-28 11:14:43 +0100702 * and try to activate it.
703 *
704 * Notice that it is important for the caller to know if the timer
705 * actually started or not (i.e., the replenishment instant is in
706 * the future or in the past).
707 */
Peter Zijlstraa649f232015-06-11 14:46:49 +0200708static int start_dl_timer(struct task_struct *p)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100709{
Peter Zijlstraa649f232015-06-11 14:46:49 +0200710 struct sched_dl_entity *dl_se = &p->dl;
711 struct hrtimer *timer = &dl_se->dl_timer;
712 struct rq *rq = task_rq(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100713 ktime_t now, act;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100714 s64 delta;
715
Peter Zijlstraa649f232015-06-11 14:46:49 +0200716 lockdep_assert_held(&rq->lock);
717
Dario Faggioliaab03e02013-11-28 11:14:43 +0100718 /*
719 * We want the timer to fire at the deadline, but considering
720 * that it is actually coming from rq->clock and not from
721 * hrtimer's time base reading.
722 */
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100723 act = ns_to_ktime(dl_next_period(dl_se));
Peter Zijlstraa649f232015-06-11 14:46:49 +0200724 now = hrtimer_cb_get_time(timer);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100725 delta = ktime_to_ns(now) - rq_clock(rq);
726 act = ktime_add_ns(act, delta);
727
728 /*
729 * If the expiry time already passed, e.g., because the value
730 * chosen as the deadline is too small, don't even try to
731 * start the timer in the past!
732 */
733 if (ktime_us_delta(act, now) < 0)
734 return 0;
735
Peter Zijlstraa649f232015-06-11 14:46:49 +0200736 /*
737 * !enqueued will guarantee another callback; even if one is already in
738 * progress. This ensures a balanced {get,put}_task_struct().
739 *
740 * The race against __run_timer() clearing the enqueued state is
741 * harmless because we're holding task_rq()->lock, therefore the timer
742 * expiring after we've done the check will wait on its task_rq_lock()
743 * and observe our state.
744 */
745 if (!hrtimer_is_queued(timer)) {
746 get_task_struct(p);
747 hrtimer_start(timer, act, HRTIMER_MODE_ABS);
748 }
Dario Faggioliaab03e02013-11-28 11:14:43 +0100749
Thomas Gleixnercc9684d2015-04-14 21:09:06 +0000750 return 1;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100751}
752
753/*
754 * This is the bandwidth enforcement timer callback. If here, we know
755 * a task is not on its dl_rq, since the fact that the timer was running
756 * means the task is throttled and needs a runtime replenishment.
757 *
758 * However, what we actually do depends on the fact the task is active,
759 * (it is on its rq) or has been removed from there by a call to
760 * dequeue_task_dl(). In the former case we must issue the runtime
761 * replenishment and add the task back to the dl_rq; in the latter, we just
762 * do nothing but clearing dl_throttled, so that runtime and deadline
763 * updating (and the queueing back to dl_rq) will be done by the
764 * next call to enqueue_task_dl().
765 */
766static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
767{
768 struct sched_dl_entity *dl_se = container_of(timer,
769 struct sched_dl_entity,
770 dl_timer);
771 struct task_struct *p = dl_task_of(dl_se);
Peter Zijlstraeb580752015-07-31 21:28:18 +0200772 struct rq_flags rf;
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400773 struct rq *rq;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100774
Peter Zijlstraeb580752015-07-31 21:28:18 +0200775 rq = task_rq_lock(p, &rf);
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400776
Dario Faggioliaab03e02013-11-28 11:14:43 +0100777 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200778 * The task might have changed its scheduling policy to something
Daniel Bristot de Oliveira9846d502016-11-08 11:15:23 +0100779 * different than SCHED_DEADLINE (through switched_from_dl()).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100780 */
Luca Abeni209a0cb2017-05-18 22:13:29 +0200781 if (!dl_task(p))
Peter Zijlstraa649f232015-06-11 14:46:49 +0200782 goto unlock;
Peter Zijlstraa649f232015-06-11 14:46:49 +0200783
784 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200785 * The task might have been boosted by someone else and might be in the
786 * boosting/deboosting path, its not throttled.
787 */
788 if (dl_se->dl_boosted)
789 goto unlock;
790
791 /*
792 * Spurious timer due to start_dl_timer() race; or we already received
793 * a replenishment from rt_mutex_setprio().
794 */
795 if (!dl_se->dl_throttled)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100796 goto unlock;
797
798 sched_clock_tick();
799 update_rq_clock(rq);
Kirill Tkhaia79ec892015-02-16 15:38:34 +0300800
801 /*
802 * If the throttle happened during sched-out; like:
803 *
804 * schedule()
805 * deactivate_task()
806 * dequeue_task_dl()
807 * update_curr_dl()
808 * start_dl_timer()
809 * __dequeue_task_dl()
810 * prev->on_rq = 0;
811 *
812 * We can be both throttled and !queued. Replenish the counter
813 * but do not enqueue -- wait for our wakeup to do that.
814 */
815 if (!task_on_rq_queued(p)) {
816 replenish_dl_entity(dl_se, dl_se);
817 goto unlock;
818 }
819
Wanpeng Li61c7aca2016-08-31 18:27:44 +0800820#ifdef CONFIG_SMP
821 if (unlikely(!rq->online)) {
822 /*
823 * If the runqueue is no longer available, migrate the
824 * task elsewhere. This necessarily changes rq.
825 */
826 lockdep_unpin_lock(&rq->lock, rf.cookie);
827 rq = dl_task_offline_migration(rq, p);
828 rf.cookie = lockdep_pin_lock(&rq->lock);
Wanpeng Lidcc3b5f2017-03-06 21:51:28 -0800829 update_rq_clock(rq);
Wanpeng Li61c7aca2016-08-31 18:27:44 +0800830
831 /*
832 * Now that the task has been migrated to the new RQ and we
833 * have that locked, proceed as normal and enqueue the task
834 * there.
835 */
836 }
837#endif
838
Peter Zijlstra1019a352014-11-26 08:44:03 +0800839 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
840 if (dl_task(rq->curr))
841 check_preempt_curr_dl(rq, p, 0);
842 else
843 resched_curr(rq);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200844
Juri Lelli1baca4c2013-11-07 14:43:38 +0100845#ifdef CONFIG_SMP
Peter Zijlstra1019a352014-11-26 08:44:03 +0800846 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200847 * Queueing this task back might have overloaded rq, check if we need
848 * to kick someone away.
Peter Zijlstra1019a352014-11-26 08:44:03 +0800849 */
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +0200850 if (has_pushable_dl_tasks(rq)) {
851 /*
852 * Nothing relies on rq->lock after this, so its safe to drop
853 * rq->lock.
854 */
Matt Flemingd8ac8972016-09-21 14:38:10 +0100855 rq_unpin_lock(rq, &rf);
Peter Zijlstra1019a352014-11-26 08:44:03 +0800856 push_dl_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +0100857 rq_repin_lock(rq, &rf);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +0200858 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100859#endif
Peter Zijlstraa649f232015-06-11 14:46:49 +0200860
Dario Faggioliaab03e02013-11-28 11:14:43 +0100861unlock:
Peter Zijlstraeb580752015-07-31 21:28:18 +0200862 task_rq_unlock(rq, p, &rf);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100863
Peter Zijlstraa649f232015-06-11 14:46:49 +0200864 /*
865 * This can free the task_struct, including this hrtimer, do not touch
866 * anything related to that after this.
867 */
868 put_task_struct(p);
869
Dario Faggioliaab03e02013-11-28 11:14:43 +0100870 return HRTIMER_NORESTART;
871}
872
873void init_dl_task_timer(struct sched_dl_entity *dl_se)
874{
875 struct hrtimer *timer = &dl_se->dl_timer;
876
Dario Faggioliaab03e02013-11-28 11:14:43 +0100877 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
878 timer->function = dl_task_timer;
879}
880
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +0100881/*
882 * During the activation, CBS checks if it can reuse the current task's
883 * runtime and period. If the deadline of the task is in the past, CBS
884 * cannot use the runtime, and so it replenishes the task. This rule
885 * works fine for implicit deadline tasks (deadline == period), and the
886 * CBS was designed for implicit deadline tasks. However, a task with
887 * constrained deadline (deadine < period) might be awakened after the
888 * deadline, but before the next period. In this case, replenishing the
889 * task would allow it to run for runtime / deadline. As in this case
890 * deadline < period, CBS enables a task to run for more than the
891 * runtime / period. In a very loaded system, this can cause a domino
892 * effect, making other tasks miss their deadlines.
893 *
894 * To avoid this problem, in the activation of a constrained deadline
895 * task after the deadline but before the next period, throttle the
896 * task and set the replenishing timer to the begin of the next period,
897 * unless it is boosted.
898 */
899static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
900{
901 struct task_struct *p = dl_task_of(dl_se);
902 struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
903
904 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
905 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
906 if (unlikely(dl_se->dl_boosted || !start_dl_timer(p)))
907 return;
908 dl_se->dl_throttled = 1;
909 }
910}
911
Dario Faggioliaab03e02013-11-28 11:14:43 +0100912static
Zhiqiang Zhang6fab5412015-06-15 11:15:20 +0800913int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100914{
Luca Abeni269ad802014-12-17 11:50:32 +0100915 return (dl_se->runtime <= 0);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100916}
917
Juri Lellifaa59932014-02-21 11:37:15 +0100918extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
919
Dario Faggioliaab03e02013-11-28 11:14:43 +0100920/*
Luca Abenic52f14d2017-05-18 22:13:31 +0200921 * This function implements the GRUB accounting rule:
922 * according to the GRUB reclaiming algorithm, the runtime is
923 * not decreased as "dq = -dt", but as "dq = -Uact dt", where
924 * Uact is the (per-runqueue) active utilization.
925 * Since rq->dl.running_bw contains Uact * 2^BW_SHIFT, the result
926 * has to be shifted right by BW_SHIFT.
927 */
928u64 grub_reclaim(u64 delta, struct rq *rq)
929{
930 delta *= rq->dl.running_bw;
931 delta >>= BW_SHIFT;
932
933 return delta;
934}
935
936/*
Dario Faggioliaab03e02013-11-28 11:14:43 +0100937 * Update the current task's runtime statistics (provided it is still
938 * a -deadline task and has not been removed from the dl_rq).
939 */
940static void update_curr_dl(struct rq *rq)
941{
942 struct task_struct *curr = rq->curr;
943 struct sched_dl_entity *dl_se = &curr->dl;
944 u64 delta_exec;
945
946 if (!dl_task(curr) || !on_dl_rq(dl_se))
947 return;
948
949 /*
950 * Consumed budget is computed considering the time as
951 * observed by schedulable tasks (excluding time spent
952 * in hardirq context, etc.). Deadlines are instead
953 * computed using hard walltime. This seems to be the more
954 * natural solution, but the full ramifications of this
955 * approach need further study.
956 */
957 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100958 if (unlikely((s64)delta_exec <= 0)) {
959 if (unlikely(dl_se->dl_yielded))
960 goto throttle;
Kirill Tkhai734ff2a2014-03-04 19:25:46 +0400961 return;
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100962 }
Dario Faggioliaab03e02013-11-28 11:14:43 +0100963
Rafael J. Wysocki58919e82016-08-16 22:14:55 +0200964 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +0200965 cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_DL);
Wanpeng Li594dd292016-04-22 17:07:24 +0800966
Dario Faggioliaab03e02013-11-28 11:14:43 +0100967 schedstat_set(curr->se.statistics.exec_max,
968 max(curr->se.statistics.exec_max, delta_exec));
969
970 curr->se.sum_exec_runtime += delta_exec;
971 account_group_exec_runtime(curr, delta_exec);
972
973 curr->se.exec_start = rq_clock_task(rq);
974 cpuacct_charge(curr, delta_exec);
975
Dario Faggioli239be4a2013-11-07 14:43:39 +0100976 sched_rt_avg_update(rq, delta_exec);
977
Luca Abenic52f14d2017-05-18 22:13:31 +0200978 delta_exec = grub_reclaim(delta_exec, rq);
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100979 dl_se->runtime -= delta_exec;
980
981throttle:
982 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
Peter Zijlstra1019a352014-11-26 08:44:03 +0800983 dl_se->dl_throttled = 1;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100984 __dequeue_task_dl(rq, curr, 0);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200985 if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
Dario Faggioliaab03e02013-11-28 11:14:43 +0100986 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
987
988 if (!is_leftmost(curr, &rq->dl))
Kirill Tkhai88751252014-06-29 00:03:57 +0400989 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100990 }
Peter Zijlstra17248132013-12-17 12:44:49 +0100991
992 /*
993 * Because -- for now -- we share the rt bandwidth, we need to
994 * account our runtime there too, otherwise actual rt tasks
995 * would be able to exceed the shared quota.
996 *
997 * Account to the root rt group for now.
998 *
999 * The solution we're working towards is having the RT groups scheduled
1000 * using deadline servers -- however there's a few nasties to figure
1001 * out before that can happen.
1002 */
1003 if (rt_bandwidth_enabled()) {
1004 struct rt_rq *rt_rq = &rq->rt;
1005
1006 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra17248132013-12-17 12:44:49 +01001007 /*
1008 * We'll let actual RT tasks worry about the overflow here, we
Juri Lellifaa59932014-02-21 11:37:15 +01001009 * have our own CBS to keep us inline; only account when RT
1010 * bandwidth is relevant.
Peter Zijlstra17248132013-12-17 12:44:49 +01001011 */
Juri Lellifaa59932014-02-21 11:37:15 +01001012 if (sched_rt_bandwidth_account(rt_rq))
1013 rt_rq->rt_time += delta_exec;
Peter Zijlstra17248132013-12-17 12:44:49 +01001014 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1015 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001016}
1017
Luca Abeni209a0cb2017-05-18 22:13:29 +02001018static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
1019{
1020 struct sched_dl_entity *dl_se = container_of(timer,
1021 struct sched_dl_entity,
1022 inactive_timer);
1023 struct task_struct *p = dl_task_of(dl_se);
1024 struct rq_flags rf;
1025 struct rq *rq;
1026
1027 rq = task_rq_lock(p, &rf);
1028
1029 if (!dl_task(p) || p->state == TASK_DEAD) {
Luca Abeni387e3132017-05-18 22:13:30 +02001030 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1031
Luca Abeni209a0cb2017-05-18 22:13:29 +02001032 if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
1033 sub_running_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl));
1034 dl_se->dl_non_contending = 0;
1035 }
Luca Abeni387e3132017-05-18 22:13:30 +02001036
1037 raw_spin_lock(&dl_b->lock);
1038 __dl_clear(dl_b, p->dl.dl_bw);
1039 raw_spin_unlock(&dl_b->lock);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001040 __dl_clear_params(p);
1041
1042 goto unlock;
1043 }
1044 if (dl_se->dl_non_contending == 0)
1045 goto unlock;
1046
1047 sched_clock_tick();
1048 update_rq_clock(rq);
1049
1050 sub_running_bw(dl_se->dl_bw, &rq->dl);
1051 dl_se->dl_non_contending = 0;
1052unlock:
1053 task_rq_unlock(rq, p, &rf);
1054 put_task_struct(p);
1055
1056 return HRTIMER_NORESTART;
1057}
1058
1059void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1060{
1061 struct hrtimer *timer = &dl_se->inactive_timer;
1062
1063 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1064 timer->function = inactive_task_timer;
1065}
1066
Juri Lelli1baca4c2013-11-07 14:43:38 +01001067#ifdef CONFIG_SMP
1068
Juri Lelli1baca4c2013-11-07 14:43:38 +01001069static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1070{
1071 struct rq *rq = rq_of_dl_rq(dl_rq);
1072
1073 if (dl_rq->earliest_dl.curr == 0 ||
1074 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001075 dl_rq->earliest_dl.curr = deadline;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001076 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001077 }
1078}
1079
1080static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1081{
1082 struct rq *rq = rq_of_dl_rq(dl_rq);
1083
1084 /*
1085 * Since we may have removed our earliest (and/or next earliest)
1086 * task we must recompute them.
1087 */
1088 if (!dl_rq->dl_nr_running) {
1089 dl_rq->earliest_dl.curr = 0;
1090 dl_rq->earliest_dl.next = 0;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001091 cpudl_clear(&rq->rd->cpudl, rq->cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001092 } else {
1093 struct rb_node *leftmost = dl_rq->rb_leftmost;
1094 struct sched_dl_entity *entry;
1095
1096 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
1097 dl_rq->earliest_dl.curr = entry->deadline;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001098 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001099 }
1100}
1101
1102#else
1103
1104static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1105static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1106
1107#endif /* CONFIG_SMP */
1108
1109static inline
1110void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1111{
1112 int prio = dl_task_of(dl_se)->prio;
1113 u64 deadline = dl_se->deadline;
1114
1115 WARN_ON(!dl_prio(prio));
1116 dl_rq->dl_nr_running++;
Kirill Tkhai72465442014-05-09 03:00:14 +04001117 add_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001118
1119 inc_dl_deadline(dl_rq, deadline);
1120 inc_dl_migration(dl_se, dl_rq);
1121}
1122
1123static inline
1124void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1125{
1126 int prio = dl_task_of(dl_se)->prio;
1127
1128 WARN_ON(!dl_prio(prio));
1129 WARN_ON(!dl_rq->dl_nr_running);
1130 dl_rq->dl_nr_running--;
Kirill Tkhai72465442014-05-09 03:00:14 +04001131 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001132
1133 dec_dl_deadline(dl_rq, dl_se->deadline);
1134 dec_dl_migration(dl_se, dl_rq);
1135}
1136
Dario Faggioliaab03e02013-11-28 11:14:43 +01001137static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1138{
1139 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1140 struct rb_node **link = &dl_rq->rb_root.rb_node;
1141 struct rb_node *parent = NULL;
1142 struct sched_dl_entity *entry;
1143 int leftmost = 1;
1144
1145 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
1146
1147 while (*link) {
1148 parent = *link;
1149 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
1150 if (dl_time_before(dl_se->deadline, entry->deadline))
1151 link = &parent->rb_left;
1152 else {
1153 link = &parent->rb_right;
1154 leftmost = 0;
1155 }
1156 }
1157
1158 if (leftmost)
1159 dl_rq->rb_leftmost = &dl_se->rb_node;
1160
1161 rb_link_node(&dl_se->rb_node, parent, link);
1162 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
1163
Juri Lelli1baca4c2013-11-07 14:43:38 +01001164 inc_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001165}
1166
1167static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1168{
1169 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1170
1171 if (RB_EMPTY_NODE(&dl_se->rb_node))
1172 return;
1173
1174 if (dl_rq->rb_leftmost == &dl_se->rb_node) {
1175 struct rb_node *next_node;
1176
1177 next_node = rb_next(&dl_se->rb_node);
1178 dl_rq->rb_leftmost = next_node;
1179 }
1180
1181 rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
1182 RB_CLEAR_NODE(&dl_se->rb_node);
1183
Juri Lelli1baca4c2013-11-07 14:43:38 +01001184 dec_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001185}
1186
1187static void
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001188enqueue_dl_entity(struct sched_dl_entity *dl_se,
1189 struct sched_dl_entity *pi_se, int flags)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001190{
1191 BUG_ON(on_dl_rq(dl_se));
1192
1193 /*
1194 * If this is a wakeup or a new instance, the scheduling
1195 * parameters of the task might need updating. Otherwise,
1196 * we want a replenishment of its runtime.
1197 */
Luca Abenie36d8672017-05-18 22:13:28 +02001198 if (flags & ENQUEUE_WAKEUP) {
Luca Abeni209a0cb2017-05-18 22:13:29 +02001199 task_contending(dl_se);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001200 update_dl_entity(dl_se, pi_se);
Luca Abenie36d8672017-05-18 22:13:28 +02001201 } else if (flags & ENQUEUE_REPLENISH) {
Luca Abeni6a503c32014-12-17 11:50:31 +01001202 replenish_dl_entity(dl_se, pi_se);
Luca Abenie36d8672017-05-18 22:13:28 +02001203 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001204
1205 __enqueue_dl_entity(dl_se);
1206}
1207
1208static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
1209{
1210 __dequeue_dl_entity(dl_se);
1211}
1212
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001213static inline bool dl_is_constrained(struct sched_dl_entity *dl_se)
1214{
1215 return dl_se->dl_deadline < dl_se->dl_period;
1216}
1217
Dario Faggioliaab03e02013-11-28 11:14:43 +01001218static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1219{
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001220 struct task_struct *pi_task = rt_mutex_get_top_task(p);
1221 struct sched_dl_entity *pi_se = &p->dl;
1222
1223 /*
1224 * Use the scheduling parameters of the top pi-waiter
Andrea Parriff277d42015-08-05 15:56:19 +02001225 * task if we have one and its (absolute) deadline is
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001226 * smaller than our one... OTW we keep our runtime and
1227 * deadline.
1228 */
Juri Lelli64be6f12014-10-24 10:16:37 +01001229 if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001230 pi_se = &pi_task->dl;
Juri Lelli64be6f12014-10-24 10:16:37 +01001231 } else if (!dl_prio(p->normal_prio)) {
1232 /*
1233 * Special case in which we have a !SCHED_DEADLINE task
1234 * that is going to be deboosted, but exceedes its
1235 * runtime while doing so. No point in replenishing
1236 * it, as it's going to return back to its original
1237 * scheduling class after this.
1238 */
1239 BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
1240 return;
1241 }
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001242
Dario Faggioliaab03e02013-11-28 11:14:43 +01001243 /*
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001244 * Check if a constrained deadline task was activated
1245 * after the deadline but before the next period.
1246 * If that is the case, the task will be throttled and
1247 * the replenishment timer will be set to the next period.
1248 */
1249 if (!p->dl.dl_throttled && dl_is_constrained(&p->dl))
1250 dl_check_constrained_dl(&p->dl);
1251
Luca Abenie36d8672017-05-18 22:13:28 +02001252 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE)
1253 add_running_bw(p->dl.dl_bw, &rq->dl);
1254
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001255 /*
Luca Abenie36d8672017-05-18 22:13:28 +02001256 * If p is throttled, we do not enqueue it. In fact, if it exhausted
Dario Faggioliaab03e02013-11-28 11:14:43 +01001257 * its budget it needs a replenishment and, since it now is on
1258 * its rq, the bandwidth timer callback (which clearly has not
1259 * run yet) will take care of this.
Luca Abenie36d8672017-05-18 22:13:28 +02001260 * However, the active utilization does not depend on the fact
1261 * that the task is on the runqueue or not (but depends on the
1262 * task's state - in GRUB parlance, "inactive" vs "active contending").
1263 * In other words, even if a task is throttled its utilization must
1264 * be counted in the active utilization; hence, we need to call
1265 * add_running_bw().
Dario Faggioliaab03e02013-11-28 11:14:43 +01001266 */
Luca Abenie36d8672017-05-18 22:13:28 +02001267 if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
Luca Abeni209a0cb2017-05-18 22:13:29 +02001268 if (flags & ENQUEUE_WAKEUP)
1269 task_contending(&p->dl);
1270
Dario Faggioliaab03e02013-11-28 11:14:43 +01001271 return;
Luca Abenie36d8672017-05-18 22:13:28 +02001272 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001273
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001274 enqueue_dl_entity(&p->dl, pi_se, flags);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001275
Ingo Molnar4b53a342017-02-05 15:41:03 +01001276 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001277 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001278}
1279
1280static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1281{
1282 dequeue_dl_entity(&p->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001283 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001284}
1285
1286static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1287{
1288 update_curr_dl(rq);
1289 __dequeue_task_dl(rq, p, flags);
Luca Abenie36d8672017-05-18 22:13:28 +02001290
1291 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE)
1292 sub_running_bw(p->dl.dl_bw, &rq->dl);
1293
1294 /*
Luca Abeni209a0cb2017-05-18 22:13:29 +02001295 * This check allows to start the inactive timer (or to immediately
1296 * decrease the active utilization, if needed) in two cases:
Luca Abenie36d8672017-05-18 22:13:28 +02001297 * when the task blocks and when it is terminating
1298 * (p->state == TASK_DEAD). We can handle the two cases in the same
1299 * way, because from GRUB's point of view the same thing is happening
1300 * (the task moves from "active contending" to "active non contending"
1301 * or "inactive")
1302 */
1303 if (flags & DEQUEUE_SLEEP)
Luca Abeni209a0cb2017-05-18 22:13:29 +02001304 task_non_contending(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001305}
1306
1307/*
1308 * Yield task semantic for -deadline tasks is:
1309 *
1310 * get off from the CPU until our next instance, with
1311 * a new runtime. This is of little use now, since we
1312 * don't have a bandwidth reclaiming mechanism. Anyway,
1313 * bandwidth reclaiming is planned for the future, and
1314 * yield_task_dl will indicate that some spare budget
1315 * is available for other task instances to use it.
1316 */
1317static void yield_task_dl(struct rq *rq)
1318{
Dario Faggioliaab03e02013-11-28 11:14:43 +01001319 /*
1320 * We make the task go to sleep until its current deadline by
1321 * forcing its runtime to zero. This way, update_curr_dl() stops
1322 * it and the bandwidth timer will wake it up and will give it
Juri Lelli5bfd1262014-04-15 13:49:04 +02001323 * new scheduling parameters (thanks to dl_yielded=1).
Dario Faggioliaab03e02013-11-28 11:14:43 +01001324 */
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001325 rq->curr->dl.dl_yielded = 1;
1326
Kirill Tkhai6f1607f2015-02-04 12:09:32 +03001327 update_rq_clock(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001328 update_curr_dl(rq);
Wanpeng Li44fb0852015-03-10 12:20:00 +08001329 /*
1330 * Tell update_rq_clock() that we've just updated,
1331 * so we don't do microscopic update in schedule()
1332 * and double the fastpath cost.
1333 */
1334 rq_clock_skip_update(rq, true);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001335}
1336
Juri Lelli1baca4c2013-11-07 14:43:38 +01001337#ifdef CONFIG_SMP
1338
1339static int find_later_rq(struct task_struct *task);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001340
1341static int
1342select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1343{
1344 struct task_struct *curr;
1345 struct rq *rq;
1346
Wanpeng Li1d7e9742014-10-14 10:22:39 +08001347 if (sd_flag != SD_BALANCE_WAKE)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001348 goto out;
1349
1350 rq = cpu_rq(cpu);
1351
1352 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07001353 curr = READ_ONCE(rq->curr); /* unlocked access */
Juri Lelli1baca4c2013-11-07 14:43:38 +01001354
1355 /*
1356 * If we are dealing with a -deadline task, we must
1357 * decide where to wake it up.
1358 * If it has a later deadline and the current task
1359 * on this rq can't move (provided the waking task
1360 * can!) we prefer to send it somewhere else. On the
1361 * other hand, if it has a shorter deadline, we
1362 * try to make it stay here, it might be important.
1363 */
1364 if (unlikely(dl_task(curr)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001365 (curr->nr_cpus_allowed < 2 ||
Juri Lelli1baca4c2013-11-07 14:43:38 +01001366 !dl_entity_preempt(&p->dl, &curr->dl)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001367 (p->nr_cpus_allowed > 1)) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001368 int target = find_later_rq(p);
1369
Wanpeng Li9d514262015-05-13 14:01:03 +08001370 if (target != -1 &&
Luca Abeni5aa50502015-10-16 10:06:21 +02001371 (dl_time_before(p->dl.deadline,
1372 cpu_rq(target)->dl.earliest_dl.curr) ||
1373 (cpu_rq(target)->dl.dl_nr_running == 0)))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001374 cpu = target;
1375 }
1376 rcu_read_unlock();
1377
1378out:
1379 return cpu;
1380}
1381
Luca Abeni209a0cb2017-05-18 22:13:29 +02001382static void migrate_task_rq_dl(struct task_struct *p)
1383{
1384 struct rq *rq;
1385
1386 if (!(p->state == TASK_WAKING) || !(p->dl.dl_non_contending))
1387 return;
1388
1389 rq = task_rq(p);
1390 /*
1391 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1392 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1393 * rq->lock is not... So, lock it
1394 */
1395 raw_spin_lock(&rq->lock);
1396 sub_running_bw(p->dl.dl_bw, &rq->dl);
1397 p->dl.dl_non_contending = 0;
1398 /*
1399 * If the timer handler is currently running and the
1400 * timer cannot be cancelled, inactive_task_timer()
1401 * will see that dl_not_contending is not set, and
1402 * will not touch the rq's active utilization,
1403 * so we are still safe.
1404 */
1405 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1406 put_task_struct(p);
1407
1408 raw_spin_unlock(&rq->lock);
1409}
1410
Juri Lelli1baca4c2013-11-07 14:43:38 +01001411static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1412{
1413 /*
1414 * Current can't be migrated, useless to reschedule,
1415 * let's hope p can move out.
1416 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001417 if (rq->curr->nr_cpus_allowed == 1 ||
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001418 cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001419 return;
1420
1421 /*
1422 * p is migratable, so let's not schedule it and
1423 * see if it is pushed or pulled somewhere else.
1424 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001425 if (p->nr_cpus_allowed != 1 &&
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001426 cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001427 return;
1428
Kirill Tkhai88751252014-06-29 00:03:57 +04001429 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001430}
1431
1432#endif /* CONFIG_SMP */
1433
Dario Faggioliaab03e02013-11-28 11:14:43 +01001434/*
1435 * Only called when both the current and waking task are -deadline
1436 * tasks.
1437 */
1438static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1439 int flags)
1440{
Juri Lelli1baca4c2013-11-07 14:43:38 +01001441 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001442 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001443 return;
1444 }
1445
1446#ifdef CONFIG_SMP
1447 /*
1448 * In the unlikely case current and p have the same deadline
1449 * let us try to decide what's the best thing to do...
1450 */
Dario Faggioli332ac172013-11-07 14:43:45 +01001451 if ((p->dl.deadline == rq->curr->dl.deadline) &&
1452 !test_tsk_need_resched(rq->curr))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001453 check_preempt_equal_dl(rq, p);
1454#endif /* CONFIG_SMP */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001455}
1456
1457#ifdef CONFIG_SCHED_HRTICK
1458static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1459{
xiaofeng.yan177ef2a2014-08-26 03:15:41 +00001460 hrtick_start(rq, p->dl.runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001461}
Wanpeng Li36ce9882014-11-11 09:52:26 +08001462#else /* !CONFIG_SCHED_HRTICK */
1463static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1464{
1465}
Dario Faggioliaab03e02013-11-28 11:14:43 +01001466#endif
1467
1468static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1469 struct dl_rq *dl_rq)
1470{
1471 struct rb_node *left = dl_rq->rb_leftmost;
1472
1473 if (!left)
1474 return NULL;
1475
1476 return rb_entry(left, struct sched_dl_entity, rb_node);
1477}
1478
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001479struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01001480pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001481{
1482 struct sched_dl_entity *dl_se;
1483 struct task_struct *p;
1484 struct dl_rq *dl_rq;
1485
1486 dl_rq = &rq->dl;
1487
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001488 if (need_pull_dl_task(rq, prev)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001489 /*
1490 * This is OK, because current is on_cpu, which avoids it being
1491 * picked for load-balance and preemption/IRQs are still
1492 * disabled avoiding further scheduler activity on it and we're
1493 * being very careful to re-start the picking loop.
1494 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01001495 rq_unpin_lock(rq, rf);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001496 pull_dl_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01001497 rq_repin_lock(rq, rf);
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001498 /*
T.Zhou176cedc2016-11-23 08:48:32 +08001499 * pull_dl_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001500 * means a stop task can slip in, in which case we need to
1501 * re-start task selection.
1502 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001503 if (rq->stop && task_on_rq_queued(rq->stop))
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001504 return RETRY_TASK;
1505 }
1506
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001507 /*
1508 * When prev is DL, we may throttle it in put_prev_task().
1509 * So, we update time before we check for dl_nr_running.
1510 */
1511 if (prev->sched_class == &dl_sched_class)
1512 update_curr_dl(rq);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001513
Dario Faggioliaab03e02013-11-28 11:14:43 +01001514 if (unlikely(!dl_rq->dl_nr_running))
1515 return NULL;
1516
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001517 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001518
Dario Faggioliaab03e02013-11-28 11:14:43 +01001519 dl_se = pick_next_dl_entity(rq, dl_rq);
1520 BUG_ON(!dl_se);
1521
1522 p = dl_task_of(dl_se);
1523 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001524
1525 /* Running task will never be pushed. */
Juri Lelli71362652014-01-14 12:03:51 +01001526 dequeue_pushable_dl_task(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001527
Dario Faggioliaab03e02013-11-28 11:14:43 +01001528 if (hrtick_enabled(rq))
1529 start_hrtick_dl(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001530
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02001531 queue_push_tasks(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001532
Dario Faggioliaab03e02013-11-28 11:14:43 +01001533 return p;
1534}
1535
1536static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1537{
1538 update_curr_dl(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001539
Ingo Molnar4b53a342017-02-05 15:41:03 +01001540 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001541 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001542}
1543
1544static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1545{
1546 update_curr_dl(rq);
1547
Wanpeng Lia7bebf42014-11-26 08:44:01 +08001548 /*
1549 * Even when we have runtime, update_curr_dl() might have resulted in us
1550 * not being the leftmost task anymore. In that case NEED_RESCHED will
1551 * be set and schedule() will start a new hrtick for the next task.
1552 */
1553 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
1554 is_leftmost(p, &rq->dl))
Dario Faggioliaab03e02013-11-28 11:14:43 +01001555 start_hrtick_dl(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001556}
1557
1558static void task_fork_dl(struct task_struct *p)
1559{
1560 /*
1561 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1562 * sched_fork()
1563 */
1564}
1565
Dario Faggioliaab03e02013-11-28 11:14:43 +01001566static void set_curr_task_dl(struct rq *rq)
1567{
1568 struct task_struct *p = rq->curr;
1569
1570 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001571
1572 /* You can't push away the running task */
1573 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001574}
1575
Juri Lelli1baca4c2013-11-07 14:43:38 +01001576#ifdef CONFIG_SMP
1577
1578/* Only try algorithms three times */
1579#define DL_MAX_TRIES 3
1580
1581static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1582{
1583 if (!task_running(rq, p) &&
Ingo Molnar0c98d342017-02-05 15:38:10 +01001584 cpumask_test_cpu(cpu, &p->cpus_allowed))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001585 return 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001586 return 0;
1587}
1588
Wanpeng Li8b5e7702015-05-13 14:01:01 +08001589/*
1590 * Return the earliest pushable rq's task, which is suitable to be executed
1591 * on the CPU, NULL otherwise:
1592 */
1593static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
1594{
1595 struct rb_node *next_node = rq->dl.pushable_dl_tasks_leftmost;
1596 struct task_struct *p = NULL;
1597
1598 if (!has_pushable_dl_tasks(rq))
1599 return NULL;
1600
1601next_node:
1602 if (next_node) {
1603 p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1604
1605 if (pick_dl_task(rq, p, cpu))
1606 return p;
1607
1608 next_node = rb_next(next_node);
1609 goto next_node;
1610 }
1611
1612 return NULL;
1613}
1614
Juri Lelli1baca4c2013-11-07 14:43:38 +01001615static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1616
1617static int find_later_rq(struct task_struct *task)
1618{
1619 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001620 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001621 int this_cpu = smp_processor_id();
1622 int best_cpu, cpu = task_cpu(task);
1623
1624 /* Make sure the mask is initialized first */
1625 if (unlikely(!later_mask))
1626 return -1;
1627
Ingo Molnar4b53a342017-02-05 15:41:03 +01001628 if (task->nr_cpus_allowed == 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001629 return -1;
1630
Juri Lelli91ec6772014-09-19 10:22:41 +01001631 /*
1632 * We have to consider system topology and task affinity
1633 * first, then we can look for a suitable cpu.
1634 */
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001635 best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
1636 task, later_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001637 if (best_cpu == -1)
1638 return -1;
1639
1640 /*
1641 * If we are here, some target has been found,
1642 * the most suitable of which is cached in best_cpu.
1643 * This is, among the runqueues where the current tasks
1644 * have later deadlines than the task's one, the rq
1645 * with the latest possible one.
1646 *
1647 * Now we check how well this matches with task's
1648 * affinity and system topology.
1649 *
1650 * The last cpu where the task run is our first
1651 * guess, since it is most likely cache-hot there.
1652 */
1653 if (cpumask_test_cpu(cpu, later_mask))
1654 return cpu;
1655 /*
1656 * Check if this_cpu is to be skipped (i.e., it is
1657 * not in the mask) or not.
1658 */
1659 if (!cpumask_test_cpu(this_cpu, later_mask))
1660 this_cpu = -1;
1661
1662 rcu_read_lock();
1663 for_each_domain(cpu, sd) {
1664 if (sd->flags & SD_WAKE_AFFINE) {
1665
1666 /*
1667 * If possible, preempting this_cpu is
1668 * cheaper than migrating.
1669 */
1670 if (this_cpu != -1 &&
1671 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1672 rcu_read_unlock();
1673 return this_cpu;
1674 }
1675
1676 /*
1677 * Last chance: if best_cpu is valid and is
1678 * in the mask, that becomes our choice.
1679 */
1680 if (best_cpu < nr_cpu_ids &&
1681 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
1682 rcu_read_unlock();
1683 return best_cpu;
1684 }
1685 }
1686 }
1687 rcu_read_unlock();
1688
1689 /*
1690 * At this point, all our guesses failed, we just return
1691 * 'something', and let the caller sort the things out.
1692 */
1693 if (this_cpu != -1)
1694 return this_cpu;
1695
1696 cpu = cpumask_any(later_mask);
1697 if (cpu < nr_cpu_ids)
1698 return cpu;
1699
1700 return -1;
1701}
1702
1703/* Locks the rq it finds */
1704static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1705{
1706 struct rq *later_rq = NULL;
1707 int tries;
1708 int cpu;
1709
1710 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1711 cpu = find_later_rq(task);
1712
1713 if ((cpu == -1) || (cpu == rq->cpu))
1714 break;
1715
1716 later_rq = cpu_rq(cpu);
1717
Luca Abeni5aa50502015-10-16 10:06:21 +02001718 if (later_rq->dl.dl_nr_running &&
1719 !dl_time_before(task->dl.deadline,
Wanpeng Li9d514262015-05-13 14:01:03 +08001720 later_rq->dl.earliest_dl.curr)) {
1721 /*
1722 * Target rq has tasks of equal or earlier deadline,
1723 * retrying does not release any lock and is unlikely
1724 * to yield a different result.
1725 */
1726 later_rq = NULL;
1727 break;
1728 }
1729
Juri Lelli1baca4c2013-11-07 14:43:38 +01001730 /* Retry if something changed. */
1731 if (double_lock_balance(rq, later_rq)) {
1732 if (unlikely(task_rq(task) != rq ||
Ingo Molnar0c98d342017-02-05 15:38:10 +01001733 !cpumask_test_cpu(later_rq->cpu, &task->cpus_allowed) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001734 task_running(rq, task) ||
Xunlei Pang13b5ab02016-05-09 12:11:31 +08001735 !dl_task(task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001736 !task_on_rq_queued(task))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001737 double_unlock_balance(rq, later_rq);
1738 later_rq = NULL;
1739 break;
1740 }
1741 }
1742
1743 /*
1744 * If the rq we found has no -deadline task, or
1745 * its earliest one has a later deadline than our
1746 * task, the rq is a good one.
1747 */
1748 if (!later_rq->dl.dl_nr_running ||
1749 dl_time_before(task->dl.deadline,
1750 later_rq->dl.earliest_dl.curr))
1751 break;
1752
1753 /* Otherwise we try again. */
1754 double_unlock_balance(rq, later_rq);
1755 later_rq = NULL;
1756 }
1757
1758 return later_rq;
1759}
1760
1761static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1762{
1763 struct task_struct *p;
1764
1765 if (!has_pushable_dl_tasks(rq))
1766 return NULL;
1767
1768 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
1769 struct task_struct, pushable_dl_tasks);
1770
1771 BUG_ON(rq->cpu != task_cpu(p));
1772 BUG_ON(task_current(rq, p));
Ingo Molnar4b53a342017-02-05 15:41:03 +01001773 BUG_ON(p->nr_cpus_allowed <= 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001774
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001775 BUG_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001776 BUG_ON(!dl_task(p));
1777
1778 return p;
1779}
1780
1781/*
1782 * See if the non running -deadline tasks on this rq
1783 * can be sent to some other CPU where they can preempt
1784 * and start executing.
1785 */
1786static int push_dl_task(struct rq *rq)
1787{
1788 struct task_struct *next_task;
1789 struct rq *later_rq;
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08001790 int ret = 0;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001791
1792 if (!rq->dl.overloaded)
1793 return 0;
1794
1795 next_task = pick_next_pushable_dl_task(rq);
1796 if (!next_task)
1797 return 0;
1798
1799retry:
1800 if (unlikely(next_task == rq->curr)) {
1801 WARN_ON(1);
1802 return 0;
1803 }
1804
1805 /*
1806 * If next_task preempts rq->curr, and rq->curr
1807 * can move away, it makes sense to just reschedule
1808 * without going further in pushing next_task.
1809 */
1810 if (dl_task(rq->curr) &&
1811 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001812 rq->curr->nr_cpus_allowed > 1) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001813 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001814 return 0;
1815 }
1816
1817 /* We might release rq lock */
1818 get_task_struct(next_task);
1819
1820 /* Will lock the rq it'll find */
1821 later_rq = find_lock_later_rq(next_task, rq);
1822 if (!later_rq) {
1823 struct task_struct *task;
1824
1825 /*
1826 * We must check all this again, since
1827 * find_lock_later_rq releases rq->lock and it is
1828 * then possible that next_task has migrated.
1829 */
1830 task = pick_next_pushable_dl_task(rq);
Byungchul Parka776b962017-05-12 10:05:59 +09001831 if (task == next_task) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001832 /*
1833 * The task is still there. We don't try
1834 * again, some other cpu will pull it when ready.
1835 */
Juri Lelli1baca4c2013-11-07 14:43:38 +01001836 goto out;
1837 }
1838
1839 if (!task)
1840 /* No more tasks */
1841 goto out;
1842
1843 put_task_struct(next_task);
1844 next_task = task;
1845 goto retry;
1846 }
1847
1848 deactivate_task(rq, next_task, 0);
Luca Abenie36d8672017-05-18 22:13:28 +02001849 sub_running_bw(next_task->dl.dl_bw, &rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001850 set_task_cpu(next_task, later_rq->cpu);
Luca Abenie36d8672017-05-18 22:13:28 +02001851 add_running_bw(next_task->dl.dl_bw, &later_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001852 activate_task(later_rq, next_task, 0);
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08001853 ret = 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001854
Kirill Tkhai88751252014-06-29 00:03:57 +04001855 resched_curr(later_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001856
1857 double_unlock_balance(rq, later_rq);
1858
1859out:
1860 put_task_struct(next_task);
1861
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08001862 return ret;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001863}
1864
1865static void push_dl_tasks(struct rq *rq)
1866{
Andrea Parri4ffa08e2015-08-05 15:56:18 +02001867 /* push_dl_task() will return true if it moved a -deadline task */
Juri Lelli1baca4c2013-11-07 14:43:38 +01001868 while (push_dl_task(rq))
1869 ;
1870}
1871
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001872static void pull_dl_task(struct rq *this_rq)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001873{
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001874 int this_cpu = this_rq->cpu, cpu;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001875 struct task_struct *p;
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001876 bool resched = false;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001877 struct rq *src_rq;
1878 u64 dmin = LONG_MAX;
1879
1880 if (likely(!dl_overloaded(this_rq)))
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001881 return;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001882
1883 /*
1884 * Match the barrier from dl_set_overloaded; this guarantees that if we
1885 * see overloaded we must also see the dlo_mask bit.
1886 */
1887 smp_rmb();
1888
1889 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
1890 if (this_cpu == cpu)
1891 continue;
1892
1893 src_rq = cpu_rq(cpu);
1894
1895 /*
1896 * It looks racy, abd it is! However, as in sched_rt.c,
1897 * we are fine with this.
1898 */
1899 if (this_rq->dl.dl_nr_running &&
1900 dl_time_before(this_rq->dl.earliest_dl.curr,
1901 src_rq->dl.earliest_dl.next))
1902 continue;
1903
1904 /* Might drop this_rq->lock */
1905 double_lock_balance(this_rq, src_rq);
1906
1907 /*
1908 * If there are no more pullable tasks on the
1909 * rq, we're done with it.
1910 */
1911 if (src_rq->dl.dl_nr_running <= 1)
1912 goto skip;
1913
Wanpeng Li8b5e7702015-05-13 14:01:01 +08001914 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001915
1916 /*
1917 * We found a task to be pulled if:
1918 * - it preempts our current (if there's one),
1919 * - it will preempt the last one we pulled (if any).
1920 */
1921 if (p && dl_time_before(p->dl.deadline, dmin) &&
1922 (!this_rq->dl.dl_nr_running ||
1923 dl_time_before(p->dl.deadline,
1924 this_rq->dl.earliest_dl.curr))) {
1925 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001926 WARN_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001927
1928 /*
1929 * Then we pull iff p has actually an earlier
1930 * deadline than the current task of its runqueue.
1931 */
1932 if (dl_time_before(p->dl.deadline,
1933 src_rq->curr->dl.deadline))
1934 goto skip;
1935
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001936 resched = true;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001937
1938 deactivate_task(src_rq, p, 0);
Luca Abenie36d8672017-05-18 22:13:28 +02001939 sub_running_bw(p->dl.dl_bw, &src_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001940 set_task_cpu(p, this_cpu);
Luca Abenie36d8672017-05-18 22:13:28 +02001941 add_running_bw(p->dl.dl_bw, &this_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001942 activate_task(this_rq, p, 0);
1943 dmin = p->dl.deadline;
1944
1945 /* Is there any other task even earlier? */
1946 }
1947skip:
1948 double_unlock_balance(this_rq, src_rq);
1949 }
1950
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02001951 if (resched)
1952 resched_curr(this_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001953}
1954
1955/*
1956 * Since the task is not running and a reschedule is not going to happen
1957 * anytime soon on its runqueue, we try pushing it away now.
1958 */
1959static void task_woken_dl(struct rq *rq, struct task_struct *p)
1960{
1961 if (!task_running(rq, p) &&
1962 !test_tsk_need_resched(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001963 p->nr_cpus_allowed > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01001964 dl_task(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001965 (rq->curr->nr_cpus_allowed < 2 ||
Wanpeng Li6b0a5632014-10-31 06:39:34 +08001966 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001967 push_dl_tasks(rq);
1968 }
1969}
1970
1971static void set_cpus_allowed_dl(struct task_struct *p,
1972 const struct cpumask *new_mask)
1973{
Juri Lelli7f514122014-09-19 10:22:40 +01001974 struct root_domain *src_rd;
Peter Zijlstra6c370672015-05-15 17:43:36 +02001975 struct rq *rq;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001976
1977 BUG_ON(!dl_task(p));
1978
Juri Lelli7f514122014-09-19 10:22:40 +01001979 rq = task_rq(p);
1980 src_rd = rq->rd;
1981 /*
1982 * Migrating a SCHED_DEADLINE task between exclusive
1983 * cpusets (different root_domains) entails a bandwidth
1984 * update. We already made space for us in the destination
1985 * domain (see cpuset_can_attach()).
1986 */
1987 if (!cpumask_intersects(src_rd->span, new_mask)) {
1988 struct dl_bw *src_dl_b;
1989
1990 src_dl_b = dl_bw_of(cpu_of(rq));
1991 /*
1992 * We now free resources of the root_domain we are migrating
1993 * off. In the worst case, sched_setattr() may temporary fail
1994 * until we complete the update.
1995 */
1996 raw_spin_lock(&src_dl_b->lock);
1997 __dl_clear(src_dl_b, p->dl.dl_bw);
1998 raw_spin_unlock(&src_dl_b->lock);
1999 }
2000
Peter Zijlstra6c370672015-05-15 17:43:36 +02002001 set_cpus_allowed_common(p, new_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002002}
2003
2004/* Assumes rq->lock is held */
2005static void rq_online_dl(struct rq *rq)
2006{
2007 if (rq->dl.overloaded)
2008 dl_set_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002009
Xunlei Pang16b26942015-01-19 04:49:36 +00002010 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002011 if (rq->dl.dl_nr_running > 0)
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02002012 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002013}
2014
2015/* Assumes rq->lock is held */
2016static void rq_offline_dl(struct rq *rq)
2017{
2018 if (rq->dl.overloaded)
2019 dl_clear_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002020
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02002021 cpudl_clear(&rq->rd->cpudl, rq->cpu);
Xunlei Pang16b26942015-01-19 04:49:36 +00002022 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002023}
2024
Wanpeng Lia6c0e742015-05-13 14:01:02 +08002025void __init init_sched_dl_class(void)
Juri Lelli1baca4c2013-11-07 14:43:38 +01002026{
2027 unsigned int i;
2028
2029 for_each_possible_cpu(i)
2030 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2031 GFP_KERNEL, cpu_to_node(i));
2032}
2033
2034#endif /* CONFIG_SMP */
2035
Dario Faggioliaab03e02013-11-28 11:14:43 +01002036static void switched_from_dl(struct rq *rq, struct task_struct *p)
2037{
Peter Zijlstraa649f232015-06-11 14:46:49 +02002038 /*
Luca Abeni209a0cb2017-05-18 22:13:29 +02002039 * task_non_contending() can start the "inactive timer" (if the 0-lag
2040 * time is in the future). If the task switches back to dl before
2041 * the "inactive timer" fires, it can continue to consume its current
2042 * runtime using its current deadline. If it stays outside of
2043 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2044 * will reset the task parameters.
Peter Zijlstraa649f232015-06-11 14:46:49 +02002045 */
Luca Abeni209a0cb2017-05-18 22:13:29 +02002046 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2047 task_non_contending(p);
2048
2049 /*
2050 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2051 * at the 0-lag time, because the task could have been migrated
2052 * while SCHED_OTHER in the meanwhile.
2053 */
2054 if (p->dl.dl_non_contending)
2055 p->dl.dl_non_contending = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002056
Juri Lelli1baca4c2013-11-07 14:43:38 +01002057 /*
2058 * Since this might be the only -deadline task on the rq,
2059 * this is the right place to try to pull some other one
2060 * from an overloaded cpu, if any.
2061 */
Wanpeng Licd660912014-10-31 06:39:35 +08002062 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2063 return;
2064
Peter Zijlstra9916e212015-06-11 14:46:43 +02002065 queue_pull_task(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002066}
2067
Juri Lelli1baca4c2013-11-07 14:43:38 +01002068/*
2069 * When switching to -deadline, we may overload the rq, then
2070 * we try to push someone off, if possible.
2071 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002072static void switched_to_dl(struct rq *rq, struct task_struct *p)
2073{
Luca Abeni209a0cb2017-05-18 22:13:29 +02002074 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2075 put_task_struct(p);
Luca Abeni72f9f3f2016-03-07 12:27:04 +01002076
Juri Lelli98b0a852016-08-05 16:07:55 +01002077 /* If p is not queued we will update its parameters at next wakeup. */
2078 if (!task_on_rq_queued(p))
2079 return;
2080
2081 /*
2082 * If p is boosted we already updated its params in
2083 * rt_mutex_setprio()->enqueue_task(..., ENQUEUE_REPLENISH),
2084 * p's deadline being now already after rq_clock(rq).
2085 */
2086 if (dl_time_before(p->dl.deadline, rq_clock(rq)))
2087 setup_new_dl_entity(&p->dl);
2088
2089 if (rq->curr != p) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01002090#ifdef CONFIG_SMP
Ingo Molnar4b53a342017-02-05 15:41:03 +01002091 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
Peter Zijlstra9916e212015-06-11 14:46:43 +02002092 queue_push_tasks(rq);
Sebastian Andrzej Siewior619bd4a2017-01-24 15:40:06 +01002093#endif
Peter Zijlstra9916e212015-06-11 14:46:43 +02002094 if (dl_task(rq->curr))
2095 check_preempt_curr_dl(rq, p, 0);
2096 else
2097 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002098 }
2099}
2100
Juri Lelli1baca4c2013-11-07 14:43:38 +01002101/*
2102 * If the scheduling parameters of a -deadline task changed,
2103 * a push or pull operation might be needed.
2104 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002105static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2106 int oldprio)
2107{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002108 if (task_on_rq_queued(p) || rq->curr == p) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002109#ifdef CONFIG_SMP
Juri Lelli1baca4c2013-11-07 14:43:38 +01002110 /*
2111 * This might be too much, but unfortunately
2112 * we don't have the old deadline value, and
2113 * we can't argue if the task is increasing
2114 * or lowering its prio, so...
2115 */
2116 if (!rq->dl.overloaded)
Peter Zijlstra9916e212015-06-11 14:46:43 +02002117 queue_pull_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002118
2119 /*
2120 * If we now have a earlier deadline task than p,
2121 * then reschedule, provided p is still on this
2122 * runqueue.
2123 */
Peter Zijlstra9916e212015-06-11 14:46:43 +02002124 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
Kirill Tkhai88751252014-06-29 00:03:57 +04002125 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002126#else
2127 /*
2128 * Again, we don't know if p has a earlier
2129 * or later deadline, so let's blindly set a
2130 * (maybe not needed) rescheduling point.
2131 */
Kirill Tkhai88751252014-06-29 00:03:57 +04002132 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002133#endif /* CONFIG_SMP */
Peter Zijlstra801ccdb2016-02-25 15:01:49 +01002134 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01002135}
Dario Faggioliaab03e02013-11-28 11:14:43 +01002136
2137const struct sched_class dl_sched_class = {
2138 .next = &rt_sched_class,
2139 .enqueue_task = enqueue_task_dl,
2140 .dequeue_task = dequeue_task_dl,
2141 .yield_task = yield_task_dl,
2142
2143 .check_preempt_curr = check_preempt_curr_dl,
2144
2145 .pick_next_task = pick_next_task_dl,
2146 .put_prev_task = put_prev_task_dl,
2147
2148#ifdef CONFIG_SMP
2149 .select_task_rq = select_task_rq_dl,
Luca Abeni209a0cb2017-05-18 22:13:29 +02002150 .migrate_task_rq = migrate_task_rq_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01002151 .set_cpus_allowed = set_cpus_allowed_dl,
2152 .rq_online = rq_online_dl,
2153 .rq_offline = rq_offline_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01002154 .task_woken = task_woken_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002155#endif
2156
2157 .set_curr_task = set_curr_task_dl,
2158 .task_tick = task_tick_dl,
2159 .task_fork = task_fork_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002160
2161 .prio_changed = prio_changed_dl,
2162 .switched_from = switched_from_dl,
2163 .switched_to = switched_to_dl,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002164
2165 .update_curr = update_curr_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002166};
Wanpeng Liacb32132014-10-31 06:39:33 +08002167
2168#ifdef CONFIG_SCHED_DEBUG
2169extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2170
2171void print_dl_stats(struct seq_file *m, int cpu)
2172{
2173 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
2174}
2175#endif /* CONFIG_SCHED_DEBUG */