blob: f584837b32e7debb6c96bec8cff53c3c663cf51f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dario Faggioliaab03e02013-11-28 11:14:43 +01002/*
3 * Deadline Scheduling Class (SCHED_DEADLINE)
4 *
5 * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
6 *
7 * Tasks that periodically executes their instances for less than their
8 * runtime won't miss any of their deadlines.
9 * Tasks that are not periodic or sporadic or that tries to execute more
10 * than their reserved bandwidth will be slowed down (and may potentially
11 * miss some of their deadlines), and won't affect any other task.
12 *
13 * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>,
Juri Lelli1baca4c2013-11-07 14:43:38 +010014 * Juri Lelli <juri.lelli@gmail.com>,
Dario Faggioliaab03e02013-11-28 11:14:43 +010015 * Michael Trimarchi <michael@amarulasolutions.com>,
16 * Fabio Checconi <fchecconi@gmail.com>
17 */
18#include "sched.h"
19
Juri Lelli6bfd6d72013-11-07 14:43:47 +010020#include <linux/slab.h>
Nicolas Pitre06a76fe2017-06-21 14:22:01 -040021#include <uapi/linux/sched/types.h>
Juri Lelli6bfd6d72013-11-07 14:43:47 +010022
Dario Faggioli332ac172013-11-07 14:43:45 +010023struct dl_bandwidth def_dl_bandwidth;
24
Dario Faggioliaab03e02013-11-28 11:14:43 +010025static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
26{
27 return container_of(dl_se, struct task_struct, dl);
28}
29
30static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
31{
32 return container_of(dl_rq, struct rq, dl);
33}
34
35static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
36{
37 struct task_struct *p = dl_task_of(dl_se);
38 struct rq *rq = task_rq(p);
39
40 return &rq->dl;
41}
42
43static inline int on_dl_rq(struct sched_dl_entity *dl_se)
44{
45 return !RB_EMPTY_NODE(&dl_se->rb_node);
46}
47
Nicolas Pitre06a76fe2017-06-21 14:22:01 -040048#ifdef CONFIG_SMP
49static inline struct dl_bw *dl_bw_of(int i)
50{
51 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
52 "sched RCU must be held");
53 return &cpu_rq(i)->rd->dl_bw;
54}
55
56static inline int dl_bw_cpus(int i)
57{
58 struct root_domain *rd = cpu_rq(i)->rd;
59 int cpus = 0;
60
61 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
62 "sched RCU must be held");
63 for_each_cpu_and(i, rd->span, cpu_active_mask)
64 cpus++;
65
66 return cpus;
67}
68#else
69static inline struct dl_bw *dl_bw_of(int i)
70{
71 return &cpu_rq(i)->dl.dl_bw;
72}
73
74static inline int dl_bw_cpus(int i)
75{
76 return 1;
77}
78#endif
79
Luca Abenie36d8672017-05-18 22:13:28 +020080static inline
81void add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
82{
83 u64 old = dl_rq->running_bw;
84
85 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
86 dl_rq->running_bw += dl_bw;
87 SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
Luca Abeni8fd27232017-05-18 22:13:34 +020088 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
Juri Lellie0367b12017-12-04 11:23:19 +010089 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
90 cpufreq_update_util(rq_of_dl_rq(dl_rq), SCHED_CPUFREQ_DL);
Luca Abenie36d8672017-05-18 22:13:28 +020091}
92
93static inline
94void sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
95{
96 u64 old = dl_rq->running_bw;
97
98 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
99 dl_rq->running_bw -= dl_bw;
100 SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
101 if (dl_rq->running_bw > old)
102 dl_rq->running_bw = 0;
Juri Lellie0367b12017-12-04 11:23:19 +0100103 /* kick cpufreq (see the comment in kernel/sched/sched.h). */
104 cpufreq_update_util(rq_of_dl_rq(dl_rq), SCHED_CPUFREQ_DL);
Luca Abenie36d8672017-05-18 22:13:28 +0200105}
106
Luca Abeni8fd27232017-05-18 22:13:34 +0200107static inline
108void add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
109{
110 u64 old = dl_rq->this_bw;
111
112 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
113 dl_rq->this_bw += dl_bw;
114 SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */
115}
116
117static inline
118void sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
119{
120 u64 old = dl_rq->this_bw;
121
122 lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
123 dl_rq->this_bw -= dl_bw;
124 SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */
125 if (dl_rq->this_bw > old)
126 dl_rq->this_bw = 0;
127 SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
128}
129
Luca Abeni209a0cb2017-05-18 22:13:29 +0200130void dl_change_utilization(struct task_struct *p, u64 new_bw)
131{
Luca Abeni8fd27232017-05-18 22:13:34 +0200132 struct rq *rq;
133
Luca Abeni209a0cb2017-05-18 22:13:29 +0200134 if (task_on_rq_queued(p))
135 return;
136
Luca Abeni8fd27232017-05-18 22:13:34 +0200137 rq = task_rq(p);
138 if (p->dl.dl_non_contending) {
139 sub_running_bw(p->dl.dl_bw, &rq->dl);
140 p->dl.dl_non_contending = 0;
141 /*
142 * If the timer handler is currently running and the
143 * timer cannot be cancelled, inactive_task_timer()
144 * will see that dl_not_contending is not set, and
145 * will not touch the rq's active utilization,
146 * so we are still safe.
147 */
148 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
149 put_task_struct(p);
150 }
151 sub_rq_bw(p->dl.dl_bw, &rq->dl);
152 add_rq_bw(new_bw, &rq->dl);
Luca Abeni209a0cb2017-05-18 22:13:29 +0200153}
154
155/*
156 * The utilization of a task cannot be immediately removed from
157 * the rq active utilization (running_bw) when the task blocks.
158 * Instead, we have to wait for the so called "0-lag time".
159 *
160 * If a task blocks before the "0-lag time", a timer (the inactive
161 * timer) is armed, and running_bw is decreased when the timer
162 * fires.
163 *
164 * If the task wakes up again before the inactive timer fires,
165 * the timer is cancelled, whereas if the task wakes up after the
166 * inactive timer fired (and running_bw has been decreased) the
167 * task's utilization has to be added to running_bw again.
168 * A flag in the deadline scheduling entity (dl_non_contending)
169 * is used to avoid race conditions between the inactive timer handler
170 * and task wakeups.
171 *
172 * The following diagram shows how running_bw is updated. A task is
173 * "ACTIVE" when its utilization contributes to running_bw; an
174 * "ACTIVE contending" task is in the TASK_RUNNING state, while an
175 * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
176 * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
177 * time already passed, which does not contribute to running_bw anymore.
178 * +------------------+
179 * wakeup | ACTIVE |
180 * +------------------>+ contending |
181 * | add_running_bw | |
182 * | +----+------+------+
183 * | | ^
184 * | dequeue | |
185 * +--------+-------+ | |
186 * | | t >= 0-lag | | wakeup
187 * | INACTIVE |<---------------+ |
188 * | | sub_running_bw | |
189 * +--------+-------+ | |
190 * ^ | |
191 * | t < 0-lag | |
192 * | | |
193 * | V |
194 * | +----+------+------+
195 * | sub_running_bw | ACTIVE |
196 * +-------------------+ |
197 * inactive timer | non contending |
198 * fired +------------------+
199 *
200 * The task_non_contending() function is invoked when a task
201 * blocks, and checks if the 0-lag time already passed or
202 * not (in the first case, it directly updates running_bw;
203 * in the second case, it arms the inactive timer).
204 *
205 * The task_contending() function is invoked when a task wakes
206 * up, and checks if the task is still in the "ACTIVE non contending"
207 * state or not (in the second case, it updates running_bw).
208 */
209static void task_non_contending(struct task_struct *p)
210{
211 struct sched_dl_entity *dl_se = &p->dl;
212 struct hrtimer *timer = &dl_se->inactive_timer;
213 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
214 struct rq *rq = rq_of_dl_rq(dl_rq);
215 s64 zerolag_time;
216
217 /*
218 * If this is a non-deadline task that has been boosted,
219 * do nothing
220 */
221 if (dl_se->dl_runtime == 0)
222 return;
223
224 WARN_ON(hrtimer_active(&dl_se->inactive_timer));
225 WARN_ON(dl_se->dl_non_contending);
226
227 zerolag_time = dl_se->deadline -
228 div64_long((dl_se->runtime * dl_se->dl_period),
229 dl_se->dl_runtime);
230
231 /*
232 * Using relative times instead of the absolute "0-lag time"
233 * allows to simplify the code
234 */
235 zerolag_time -= rq_clock(rq);
236
237 /*
238 * If the "0-lag time" already passed, decrease the active
239 * utilization now, instead of starting a timer
240 */
241 if (zerolag_time < 0) {
242 if (dl_task(p))
243 sub_running_bw(dl_se->dl_bw, dl_rq);
Luca Abeni387e3132017-05-18 22:13:30 +0200244 if (!dl_task(p) || p->state == TASK_DEAD) {
245 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
246
Luca Abeni8fd27232017-05-18 22:13:34 +0200247 if (p->state == TASK_DEAD)
248 sub_rq_bw(p->dl.dl_bw, &rq->dl);
Luca Abeni387e3132017-05-18 22:13:30 +0200249 raw_spin_lock(&dl_b->lock);
Peter Zijlstra8c0944ce2017-09-07 12:09:30 +0200250 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
Luca Abeni209a0cb2017-05-18 22:13:29 +0200251 __dl_clear_params(p);
Luca Abeni387e3132017-05-18 22:13:30 +0200252 raw_spin_unlock(&dl_b->lock);
253 }
Luca Abeni209a0cb2017-05-18 22:13:29 +0200254
255 return;
256 }
257
258 dl_se->dl_non_contending = 1;
259 get_task_struct(p);
260 hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL);
261}
262
Luca Abeni8fd27232017-05-18 22:13:34 +0200263static void task_contending(struct sched_dl_entity *dl_se, int flags)
Luca Abeni209a0cb2017-05-18 22:13:29 +0200264{
265 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
266
267 /*
268 * If this is a non-deadline task that has been boosted,
269 * do nothing
270 */
271 if (dl_se->dl_runtime == 0)
272 return;
273
Luca Abeni8fd27232017-05-18 22:13:34 +0200274 if (flags & ENQUEUE_MIGRATED)
275 add_rq_bw(dl_se->dl_bw, dl_rq);
276
Luca Abeni209a0cb2017-05-18 22:13:29 +0200277 if (dl_se->dl_non_contending) {
278 dl_se->dl_non_contending = 0;
279 /*
280 * If the timer handler is currently running and the
281 * timer cannot be cancelled, inactive_task_timer()
282 * will see that dl_not_contending is not set, and
283 * will not touch the rq's active utilization,
284 * so we are still safe.
285 */
286 if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1)
287 put_task_struct(dl_task_of(dl_se));
288 } else {
289 /*
290 * Since "dl_non_contending" is not set, the
291 * task's utilization has already been removed from
292 * active utilization (either when the task blocked,
293 * when the "inactive timer" fired).
294 * So, add it back.
295 */
296 add_running_bw(dl_se->dl_bw, dl_rq);
297 }
298}
299
Dario Faggioliaab03e02013-11-28 11:14:43 +0100300static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
301{
302 struct sched_dl_entity *dl_se = &p->dl;
303
Davidlohr Bueso21615732017-09-08 16:14:58 -0700304 return dl_rq->root.rb_leftmost == &dl_se->rb_node;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100305}
306
Dario Faggioli332ac172013-11-07 14:43:45 +0100307void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
308{
309 raw_spin_lock_init(&dl_b->dl_runtime_lock);
310 dl_b->dl_period = period;
311 dl_b->dl_runtime = runtime;
312}
313
Dario Faggioli332ac172013-11-07 14:43:45 +0100314void init_dl_bw(struct dl_bw *dl_b)
315{
316 raw_spin_lock_init(&dl_b->lock);
317 raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
Peter Zijlstra17248132013-12-17 12:44:49 +0100318 if (global_rt_runtime() == RUNTIME_INF)
Dario Faggioli332ac172013-11-07 14:43:45 +0100319 dl_b->bw = -1;
320 else
Peter Zijlstra17248132013-12-17 12:44:49 +0100321 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +0100322 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
323 dl_b->total_bw = 0;
324}
325
Abel Vesa07c54f72015-03-03 13:50:27 +0200326void init_dl_rq(struct dl_rq *dl_rq)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100327{
Davidlohr Bueso21615732017-09-08 16:14:58 -0700328 dl_rq->root = RB_ROOT_CACHED;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100329
330#ifdef CONFIG_SMP
331 /* zero means no -deadline tasks */
332 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
333
334 dl_rq->dl_nr_migratory = 0;
335 dl_rq->overloaded = 0;
Davidlohr Bueso21615732017-09-08 16:14:58 -0700336 dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
Dario Faggioli332ac172013-11-07 14:43:45 +0100337#else
338 init_dl_bw(&dl_rq->dl_bw);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100339#endif
Luca Abenie36d8672017-05-18 22:13:28 +0200340
341 dl_rq->running_bw = 0;
Luca Abeni8fd27232017-05-18 22:13:34 +0200342 dl_rq->this_bw = 0;
Luca Abeni4da3abc2017-05-18 22:13:32 +0200343 init_dl_rq_bw_ratio(dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100344}
345
Juri Lelli1baca4c2013-11-07 14:43:38 +0100346#ifdef CONFIG_SMP
347
348static inline int dl_overloaded(struct rq *rq)
349{
350 return atomic_read(&rq->rd->dlo_count);
351}
352
353static inline void dl_set_overload(struct rq *rq)
354{
355 if (!rq->online)
356 return;
357
358 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
359 /*
360 * Must be visible before the overload count is
361 * set (as in sched_rt.c).
362 *
363 * Matched by the barrier in pull_dl_task().
364 */
365 smp_wmb();
366 atomic_inc(&rq->rd->dlo_count);
367}
368
369static inline void dl_clear_overload(struct rq *rq)
370{
371 if (!rq->online)
372 return;
373
374 atomic_dec(&rq->rd->dlo_count);
375 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
376}
377
378static void update_dl_migration(struct dl_rq *dl_rq)
379{
Kirill Tkhai995b9ea2014-02-18 02:24:13 +0400380 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100381 if (!dl_rq->overloaded) {
382 dl_set_overload(rq_of_dl_rq(dl_rq));
383 dl_rq->overloaded = 1;
384 }
385 } else if (dl_rq->overloaded) {
386 dl_clear_overload(rq_of_dl_rq(dl_rq));
387 dl_rq->overloaded = 0;
388 }
389}
390
391static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
392{
393 struct task_struct *p = dl_task_of(dl_se);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100394
Ingo Molnar4b53a342017-02-05 15:41:03 +0100395 if (p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100396 dl_rq->dl_nr_migratory++;
397
398 update_dl_migration(dl_rq);
399}
400
401static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
402{
403 struct task_struct *p = dl_task_of(dl_se);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100404
Ingo Molnar4b53a342017-02-05 15:41:03 +0100405 if (p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100406 dl_rq->dl_nr_migratory--;
407
408 update_dl_migration(dl_rq);
409}
410
411/*
412 * The list of pushable -deadline task is not a plist, like in
413 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
414 */
415static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
416{
417 struct dl_rq *dl_rq = &rq->dl;
Davidlohr Bueso21615732017-09-08 16:14:58 -0700418 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_root.rb_node;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100419 struct rb_node *parent = NULL;
420 struct task_struct *entry;
Davidlohr Bueso21615732017-09-08 16:14:58 -0700421 bool leftmost = true;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100422
423 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
424
425 while (*link) {
426 parent = *link;
427 entry = rb_entry(parent, struct task_struct,
428 pushable_dl_tasks);
429 if (dl_entity_preempt(&p->dl, &entry->dl))
430 link = &parent->rb_left;
431 else {
432 link = &parent->rb_right;
Davidlohr Bueso21615732017-09-08 16:14:58 -0700433 leftmost = false;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100434 }
435 }
436
Davidlohr Bueso21615732017-09-08 16:14:58 -0700437 if (leftmost)
Wanpeng Li7d92de32015-12-03 17:42:10 +0800438 dl_rq->earliest_dl.next = p->dl.deadline;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100439
440 rb_link_node(&p->pushable_dl_tasks, parent, link);
Davidlohr Bueso21615732017-09-08 16:14:58 -0700441 rb_insert_color_cached(&p->pushable_dl_tasks,
442 &dl_rq->pushable_dl_tasks_root, leftmost);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100443}
444
445static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
446{
447 struct dl_rq *dl_rq = &rq->dl;
448
449 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
450 return;
451
Davidlohr Bueso21615732017-09-08 16:14:58 -0700452 if (dl_rq->pushable_dl_tasks_root.rb_leftmost == &p->pushable_dl_tasks) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100453 struct rb_node *next_node;
454
455 next_node = rb_next(&p->pushable_dl_tasks);
Wanpeng Li7d92de32015-12-03 17:42:10 +0800456 if (next_node) {
457 dl_rq->earliest_dl.next = rb_entry(next_node,
458 struct task_struct, pushable_dl_tasks)->dl.deadline;
459 }
Juri Lelli1baca4c2013-11-07 14:43:38 +0100460 }
461
Davidlohr Bueso21615732017-09-08 16:14:58 -0700462 rb_erase_cached(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100463 RB_CLEAR_NODE(&p->pushable_dl_tasks);
464}
465
466static inline int has_pushable_dl_tasks(struct rq *rq)
467{
Davidlohr Bueso21615732017-09-08 16:14:58 -0700468 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100469}
470
471static int push_dl_task(struct rq *rq);
472
Peter Zijlstradc877342014-02-12 15:47:29 +0100473static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
474{
475 return dl_task(prev);
476}
477
Peter Zijlstra9916e212015-06-11 14:46:43 +0200478static DEFINE_PER_CPU(struct callback_head, dl_push_head);
479static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200480
481static void push_dl_tasks(struct rq *);
Peter Zijlstra9916e212015-06-11 14:46:43 +0200482static void pull_dl_task(struct rq *);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200483
484static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100485{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200486 if (!has_pushable_dl_tasks(rq))
487 return;
488
Peter Zijlstra9916e212015-06-11 14:46:43 +0200489 queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
490}
491
492static inline void queue_pull_task(struct rq *rq)
493{
494 queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
Peter Zijlstradc877342014-02-12 15:47:29 +0100495}
496
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800497static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
498
Peter Zijlstraa649f232015-06-11 14:46:49 +0200499static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800500{
501 struct rq *later_rq = NULL;
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800502
503 later_rq = find_lock_later_rq(p, rq);
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800504 if (!later_rq) {
505 int cpu;
506
507 /*
508 * If we cannot preempt any rq, fall back to pick any
509 * online cpu.
510 */
Ingo Molnar0c98d342017-02-05 15:38:10 +0100511 cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800512 if (cpu >= nr_cpu_ids) {
513 /*
514 * Fail to find any suitable cpu.
515 * The task will never come back!
516 */
517 BUG_ON(dl_bandwidth_enabled());
518
519 /*
520 * If admission control is disabled we
521 * try a little harder to let the task
522 * run.
523 */
524 cpu = cpumask_any(cpu_active_mask);
525 }
526 later_rq = cpu_rq(cpu);
527 double_lock_balance(rq, later_rq);
528 }
529
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800530 set_task_cpu(p, later_rq->cpu);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200531 double_unlock_balance(later_rq, rq);
532
533 return later_rq;
Wanpeng Lifa9c9d12015-03-27 07:08:35 +0800534}
535
Juri Lelli1baca4c2013-11-07 14:43:38 +0100536#else
537
538static inline
539void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
540{
541}
542
543static inline
544void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
545{
546}
547
548static inline
549void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
550{
551}
552
553static inline
554void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
555{
556}
557
Peter Zijlstradc877342014-02-12 15:47:29 +0100558static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
559{
560 return false;
561}
562
Peter Zijlstra0ea60c22015-06-11 14:46:42 +0200563static inline void pull_dl_task(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100564{
Peter Zijlstradc877342014-02-12 15:47:29 +0100565}
566
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200567static inline void queue_push_tasks(struct rq *rq)
Peter Zijlstradc877342014-02-12 15:47:29 +0100568{
569}
Peter Zijlstra9916e212015-06-11 14:46:43 +0200570
571static inline void queue_pull_task(struct rq *rq)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100572{
573}
574#endif /* CONFIG_SMP */
575
Dario Faggioliaab03e02013-11-28 11:14:43 +0100576static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
577static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
578static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
579 int flags);
580
581/*
582 * We are being explicitly informed that a new instance is starting,
583 * and this means that:
584 * - the absolute deadline of the entity has to be placed at
585 * current time + relative deadline;
586 * - the runtime of the entity has to be set to the maximum value.
587 *
588 * The capability of specifying such event is useful whenever a -deadline
589 * entity wants to (try to!) synchronize its behaviour with the scheduler's
590 * one, and to (try to!) reconcile itself with its own scheduling
591 * parameters.
592 */
Juri Lelli98b0a852016-08-05 16:07:55 +0100593static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100594{
595 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
596 struct rq *rq = rq_of_dl_rq(dl_rq);
597
Juri Lelli98b0a852016-08-05 16:07:55 +0100598 WARN_ON(dl_se->dl_boosted);
Luca Abeni72f9f3f2016-03-07 12:27:04 +0100599 WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
600
601 /*
602 * We are racing with the deadline timer. So, do nothing because
603 * the deadline timer handler will take care of properly recharging
604 * the runtime and postponing the deadline
605 */
606 if (dl_se->dl_throttled)
607 return;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100608
609 /*
610 * We use the regular wall clock time to set deadlines in the
611 * future; in fact, we must consider execution overheads (time
612 * spent on hardirq context, etc.).
613 */
Juri Lelli98b0a852016-08-05 16:07:55 +0100614 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
615 dl_se->runtime = dl_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100616}
617
618/*
619 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
620 * possibility of a entity lasting more than what it declared, and thus
621 * exhausting its runtime.
622 *
623 * Here we are interested in making runtime overrun possible, but we do
624 * not want a entity which is misbehaving to affect the scheduling of all
625 * other entities.
626 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
627 * is used, in order to confine each entity within its own bandwidth.
628 *
629 * This function deals exactly with that, and ensures that when the runtime
630 * of a entity is replenished, its deadline is also postponed. That ensures
631 * the overrunning entity can't interfere with other entity in the system and
632 * can't make them miss their deadlines. Reasons why this kind of overruns
633 * could happen are, typically, a entity voluntarily trying to overcome its
xiaofeng.yan1b09d292014-07-07 05:59:04 +0000634 * runtime, or it just underestimated it during sched_setattr().
Dario Faggioliaab03e02013-11-28 11:14:43 +0100635 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100636static void replenish_dl_entity(struct sched_dl_entity *dl_se,
637 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100638{
639 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
640 struct rq *rq = rq_of_dl_rq(dl_rq);
641
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100642 BUG_ON(pi_se->dl_runtime <= 0);
643
644 /*
645 * This could be the case for a !-dl task that is boosted.
646 * Just go with full inherited parameters.
647 */
648 if (dl_se->dl_deadline == 0) {
649 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
650 dl_se->runtime = pi_se->dl_runtime;
651 }
652
Peter Zijlstra48be3a62016-02-23 13:28:22 +0100653 if (dl_se->dl_yielded && dl_se->runtime > 0)
654 dl_se->runtime = 0;
655
Dario Faggioliaab03e02013-11-28 11:14:43 +0100656 /*
657 * We keep moving the deadline away until we get some
658 * available runtime for the entity. This ensures correct
659 * handling of situations where the runtime overrun is
660 * arbitrary large.
661 */
662 while (dl_se->runtime <= 0) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100663 dl_se->deadline += pi_se->dl_period;
664 dl_se->runtime += pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100665 }
666
667 /*
668 * At this point, the deadline really should be "in
669 * the future" with respect to rq->clock. If it's
670 * not, we are, for some reason, lagging too much!
671 * Anyway, after having warn userspace abut that,
672 * we still try to keep the things running by
673 * resetting the deadline and the budget of the
674 * entity.
675 */
676 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
Steven Rostedtc219b7d2016-02-10 12:04:22 -0500677 printk_deferred_once("sched: DL replenish lagged too much\n");
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100678 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
679 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100680 }
Peter Zijlstra1019a352014-11-26 08:44:03 +0800681
682 if (dl_se->dl_yielded)
683 dl_se->dl_yielded = 0;
684 if (dl_se->dl_throttled)
685 dl_se->dl_throttled = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100686}
687
688/*
689 * Here we check if --at time t-- an entity (which is probably being
690 * [re]activated or, in general, enqueued) can use its remaining runtime
691 * and its current deadline _without_ exceeding the bandwidth it is
692 * assigned (function returns true if it can't). We are in fact applying
693 * one of the CBS rules: when a task wakes up, if the residual runtime
694 * over residual deadline fits within the allocated bandwidth, then we
695 * can keep the current (absolute) deadline and residual budget without
696 * disrupting the schedulability of the system. Otherwise, we should
697 * refill the runtime and set the deadline a period in the future,
698 * because keeping the current (absolute) deadline of the task would
Dario Faggioli712e5e32014-01-27 12:20:15 +0100699 * result in breaking guarantees promised to other tasks (refer to
700 * Documentation/scheduler/sched-deadline.txt for more informations).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100701 *
702 * This function returns true if:
703 *
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100704 * runtime / (deadline - t) > dl_runtime / dl_deadline ,
Dario Faggioliaab03e02013-11-28 11:14:43 +0100705 *
706 * IOW we can't recycle current parameters.
Harald Gustafsson755378a2013-11-07 14:43:40 +0100707 *
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100708 * Notice that the bandwidth check is done against the deadline. For
Harald Gustafsson755378a2013-11-07 14:43:40 +0100709 * task with deadline equal to period this is the same of using
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100710 * dl_period instead of dl_deadline in the equation above.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100711 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100712static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
713 struct sched_dl_entity *pi_se, u64 t)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100714{
715 u64 left, right;
716
717 /*
718 * left and right are the two sides of the equation above,
719 * after a bit of shuffling to use multiplications instead
720 * of divisions.
721 *
722 * Note that none of the time values involved in the two
723 * multiplications are absolute: dl_deadline and dl_runtime
724 * are the relative deadline and the maximum runtime of each
725 * instance, runtime is the runtime left for the last instance
726 * and (deadline - t), since t is rq->clock, is the time left
727 * to the (absolute) deadline. Even if overflowing the u64 type
728 * is very unlikely to occur in both cases, here we scale down
729 * as we want to avoid that risk at all. Scaling down by 10
730 * means that we reduce granularity to 1us. We are fine with it,
731 * since this is only a true/false check and, anyway, thinking
732 * of anything below microseconds resolution is actually fiction
733 * (but still we want to give the user that illusion >;).
734 */
Steven Rostedt (VMware)2317d5f2017-03-02 15:10:59 +0100735 left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
Dario Faggioli332ac172013-11-07 14:43:45 +0100736 right = ((dl_se->deadline - t) >> DL_SCALE) *
737 (pi_se->dl_runtime >> DL_SCALE);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100738
739 return dl_time_before(right, left);
740}
741
742/*
Daniel Bristot de Oliveira3effcb42017-05-29 16:24:03 +0200743 * Revised wakeup rule [1]: For self-suspending tasks, rather then
744 * re-initializing task's runtime and deadline, the revised wakeup
745 * rule adjusts the task's runtime to avoid the task to overrun its
746 * density.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100747 *
Daniel Bristot de Oliveira3effcb42017-05-29 16:24:03 +0200748 * Reasoning: a task may overrun the density if:
749 * runtime / (deadline - t) > dl_runtime / dl_deadline
750 *
751 * Therefore, runtime can be adjusted to:
752 * runtime = (dl_runtime / dl_deadline) * (deadline - t)
753 *
754 * In such way that runtime will be equal to the maximum density
755 * the task can use without breaking any rule.
756 *
757 * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
758 * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
759 */
760static void
761update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
762{
763 u64 laxity = dl_se->deadline - rq_clock(rq);
764
765 /*
766 * If the task has deadline < period, and the deadline is in the past,
767 * it should already be throttled before this check.
768 *
769 * See update_dl_entity() comments for further details.
770 */
771 WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
772
773 dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
774}
775
776/*
777 * Regarding the deadline, a task with implicit deadline has a relative
778 * deadline == relative period. A task with constrained deadline has a
779 * relative deadline <= relative period.
780 *
781 * We support constrained deadline tasks. However, there are some restrictions
782 * applied only for tasks which do not have an implicit deadline. See
783 * update_dl_entity() to know more about such restrictions.
784 *
785 * The dl_is_implicit() returns true if the task has an implicit deadline.
786 */
787static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
788{
789 return dl_se->dl_deadline == dl_se->dl_period;
790}
791
792/*
793 * When a deadline entity is placed in the runqueue, its runtime and deadline
794 * might need to be updated. This is done by a CBS wake up rule. There are two
795 * different rules: 1) the original CBS; and 2) the Revisited CBS.
796 *
797 * When the task is starting a new period, the Original CBS is used. In this
798 * case, the runtime is replenished and a new absolute deadline is set.
799 *
800 * When a task is queued before the begin of the next period, using the
801 * remaining runtime and deadline could make the entity to overflow, see
802 * dl_entity_overflow() to find more about runtime overflow. When such case
803 * is detected, the runtime and deadline need to be updated.
804 *
805 * If the task has an implicit deadline, i.e., deadline == period, the Original
806 * CBS is applied. the runtime is replenished and a new absolute deadline is
807 * set, as in the previous cases.
808 *
809 * However, the Original CBS does not work properly for tasks with
810 * deadline < period, which are said to have a constrained deadline. By
811 * applying the Original CBS, a constrained deadline task would be able to run
812 * runtime/deadline in a period. With deadline < period, the task would
813 * overrun the runtime/period allowed bandwidth, breaking the admission test.
814 *
815 * In order to prevent this misbehave, the Revisited CBS is used for
816 * constrained deadline tasks when a runtime overflow is detected. In the
817 * Revisited CBS, rather than replenishing & setting a new absolute deadline,
818 * the remaining runtime of the task is reduced to avoid runtime overflow.
819 * Please refer to the comments update_dl_revised_wakeup() function to find
820 * more about the Revised CBS rule.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100821 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100822static void update_dl_entity(struct sched_dl_entity *dl_se,
823 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100824{
825 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
826 struct rq *rq = rq_of_dl_rq(dl_rq);
827
Dario Faggioliaab03e02013-11-28 11:14:43 +0100828 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100829 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
Daniel Bristot de Oliveira3effcb42017-05-29 16:24:03 +0200830
831 if (unlikely(!dl_is_implicit(dl_se) &&
832 !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
833 !dl_se->dl_boosted)){
834 update_dl_revised_wakeup(dl_se, rq);
835 return;
836 }
837
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100838 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
839 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100840 }
841}
842
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100843static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
844{
845 return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
846}
847
Dario Faggioliaab03e02013-11-28 11:14:43 +0100848/*
849 * If the entity depleted all its runtime, and if we want it to sleep
850 * while waiting for some new execution time to become available, we
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100851 * set the bandwidth replenishment timer to the replenishment instant
Dario Faggioliaab03e02013-11-28 11:14:43 +0100852 * and try to activate it.
853 *
854 * Notice that it is important for the caller to know if the timer
855 * actually started or not (i.e., the replenishment instant is in
856 * the future or in the past).
857 */
Peter Zijlstraa649f232015-06-11 14:46:49 +0200858static int start_dl_timer(struct task_struct *p)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100859{
Peter Zijlstraa649f232015-06-11 14:46:49 +0200860 struct sched_dl_entity *dl_se = &p->dl;
861 struct hrtimer *timer = &dl_se->dl_timer;
862 struct rq *rq = task_rq(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100863 ktime_t now, act;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100864 s64 delta;
865
Peter Zijlstraa649f232015-06-11 14:46:49 +0200866 lockdep_assert_held(&rq->lock);
867
Dario Faggioliaab03e02013-11-28 11:14:43 +0100868 /*
869 * We want the timer to fire at the deadline, but considering
870 * that it is actually coming from rq->clock and not from
871 * hrtimer's time base reading.
872 */
Daniel Bristot de Oliveira5ac69d32017-03-02 15:10:57 +0100873 act = ns_to_ktime(dl_next_period(dl_se));
Peter Zijlstraa649f232015-06-11 14:46:49 +0200874 now = hrtimer_cb_get_time(timer);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100875 delta = ktime_to_ns(now) - rq_clock(rq);
876 act = ktime_add_ns(act, delta);
877
878 /*
879 * If the expiry time already passed, e.g., because the value
880 * chosen as the deadline is too small, don't even try to
881 * start the timer in the past!
882 */
883 if (ktime_us_delta(act, now) < 0)
884 return 0;
885
Peter Zijlstraa649f232015-06-11 14:46:49 +0200886 /*
887 * !enqueued will guarantee another callback; even if one is already in
888 * progress. This ensures a balanced {get,put}_task_struct().
889 *
890 * The race against __run_timer() clearing the enqueued state is
891 * harmless because we're holding task_rq()->lock, therefore the timer
892 * expiring after we've done the check will wait on its task_rq_lock()
893 * and observe our state.
894 */
895 if (!hrtimer_is_queued(timer)) {
896 get_task_struct(p);
897 hrtimer_start(timer, act, HRTIMER_MODE_ABS);
898 }
Dario Faggioliaab03e02013-11-28 11:14:43 +0100899
Thomas Gleixnercc9684d2015-04-14 21:09:06 +0000900 return 1;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100901}
902
903/*
904 * This is the bandwidth enforcement timer callback. If here, we know
905 * a task is not on its dl_rq, since the fact that the timer was running
906 * means the task is throttled and needs a runtime replenishment.
907 *
908 * However, what we actually do depends on the fact the task is active,
909 * (it is on its rq) or has been removed from there by a call to
910 * dequeue_task_dl(). In the former case we must issue the runtime
911 * replenishment and add the task back to the dl_rq; in the latter, we just
912 * do nothing but clearing dl_throttled, so that runtime and deadline
913 * updating (and the queueing back to dl_rq) will be done by the
914 * next call to enqueue_task_dl().
915 */
916static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
917{
918 struct sched_dl_entity *dl_se = container_of(timer,
919 struct sched_dl_entity,
920 dl_timer);
921 struct task_struct *p = dl_task_of(dl_se);
Peter Zijlstraeb580752015-07-31 21:28:18 +0200922 struct rq_flags rf;
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400923 struct rq *rq;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100924
Peter Zijlstraeb580752015-07-31 21:28:18 +0200925 rq = task_rq_lock(p, &rf);
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400926
Dario Faggioliaab03e02013-11-28 11:14:43 +0100927 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200928 * The task might have changed its scheduling policy to something
Daniel Bristot de Oliveira9846d502016-11-08 11:15:23 +0100929 * different than SCHED_DEADLINE (through switched_from_dl()).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100930 */
Luca Abeni209a0cb2017-05-18 22:13:29 +0200931 if (!dl_task(p))
Peter Zijlstraa649f232015-06-11 14:46:49 +0200932 goto unlock;
Peter Zijlstraa649f232015-06-11 14:46:49 +0200933
934 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200935 * The task might have been boosted by someone else and might be in the
936 * boosting/deboosting path, its not throttled.
937 */
938 if (dl_se->dl_boosted)
939 goto unlock;
940
941 /*
942 * Spurious timer due to start_dl_timer() race; or we already received
943 * a replenishment from rt_mutex_setprio().
944 */
945 if (!dl_se->dl_throttled)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100946 goto unlock;
947
948 sched_clock_tick();
949 update_rq_clock(rq);
Kirill Tkhaia79ec892015-02-16 15:38:34 +0300950
951 /*
952 * If the throttle happened during sched-out; like:
953 *
954 * schedule()
955 * deactivate_task()
956 * dequeue_task_dl()
957 * update_curr_dl()
958 * start_dl_timer()
959 * __dequeue_task_dl()
960 * prev->on_rq = 0;
961 *
962 * We can be both throttled and !queued. Replenish the counter
963 * but do not enqueue -- wait for our wakeup to do that.
964 */
965 if (!task_on_rq_queued(p)) {
966 replenish_dl_entity(dl_se, dl_se);
967 goto unlock;
968 }
969
Wanpeng Li61c7aca2016-08-31 18:27:44 +0800970#ifdef CONFIG_SMP
971 if (unlikely(!rq->online)) {
972 /*
973 * If the runqueue is no longer available, migrate the
974 * task elsewhere. This necessarily changes rq.
975 */
976 lockdep_unpin_lock(&rq->lock, rf.cookie);
977 rq = dl_task_offline_migration(rq, p);
978 rf.cookie = lockdep_pin_lock(&rq->lock);
Wanpeng Lidcc3b5f2017-03-06 21:51:28 -0800979 update_rq_clock(rq);
Wanpeng Li61c7aca2016-08-31 18:27:44 +0800980
981 /*
982 * Now that the task has been migrated to the new RQ and we
983 * have that locked, proceed as normal and enqueue the task
984 * there.
985 */
986 }
987#endif
988
Peter Zijlstra1019a352014-11-26 08:44:03 +0800989 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
990 if (dl_task(rq->curr))
991 check_preempt_curr_dl(rq, p, 0);
992 else
993 resched_curr(rq);
Peter Zijlstraa649f232015-06-11 14:46:49 +0200994
Juri Lelli1baca4c2013-11-07 14:43:38 +0100995#ifdef CONFIG_SMP
Peter Zijlstra1019a352014-11-26 08:44:03 +0800996 /*
Peter Zijlstraa649f232015-06-11 14:46:49 +0200997 * Queueing this task back might have overloaded rq, check if we need
998 * to kick someone away.
Peter Zijlstra1019a352014-11-26 08:44:03 +0800999 */
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02001000 if (has_pushable_dl_tasks(rq)) {
1001 /*
1002 * Nothing relies on rq->lock after this, so its safe to drop
1003 * rq->lock.
1004 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01001005 rq_unpin_lock(rq, &rf);
Peter Zijlstra1019a352014-11-26 08:44:03 +08001006 push_dl_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01001007 rq_repin_lock(rq, &rf);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02001008 }
Juri Lelli1baca4c2013-11-07 14:43:38 +01001009#endif
Peter Zijlstraa649f232015-06-11 14:46:49 +02001010
Dario Faggioliaab03e02013-11-28 11:14:43 +01001011unlock:
Peter Zijlstraeb580752015-07-31 21:28:18 +02001012 task_rq_unlock(rq, p, &rf);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001013
Peter Zijlstraa649f232015-06-11 14:46:49 +02001014 /*
1015 * This can free the task_struct, including this hrtimer, do not touch
1016 * anything related to that after this.
1017 */
1018 put_task_struct(p);
1019
Dario Faggioliaab03e02013-11-28 11:14:43 +01001020 return HRTIMER_NORESTART;
1021}
1022
1023void init_dl_task_timer(struct sched_dl_entity *dl_se)
1024{
1025 struct hrtimer *timer = &dl_se->dl_timer;
1026
Dario Faggioliaab03e02013-11-28 11:14:43 +01001027 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1028 timer->function = dl_task_timer;
1029}
1030
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001031/*
1032 * During the activation, CBS checks if it can reuse the current task's
1033 * runtime and period. If the deadline of the task is in the past, CBS
1034 * cannot use the runtime, and so it replenishes the task. This rule
1035 * works fine for implicit deadline tasks (deadline == period), and the
1036 * CBS was designed for implicit deadline tasks. However, a task with
1037 * constrained deadline (deadine < period) might be awakened after the
1038 * deadline, but before the next period. In this case, replenishing the
1039 * task would allow it to run for runtime / deadline. As in this case
1040 * deadline < period, CBS enables a task to run for more than the
1041 * runtime / period. In a very loaded system, this can cause a domino
1042 * effect, making other tasks miss their deadlines.
1043 *
1044 * To avoid this problem, in the activation of a constrained deadline
1045 * task after the deadline but before the next period, throttle the
1046 * task and set the replenishing timer to the begin of the next period,
1047 * unless it is boosted.
1048 */
1049static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
1050{
1051 struct task_struct *p = dl_task_of(dl_se);
1052 struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
1053
1054 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1055 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
1056 if (unlikely(dl_se->dl_boosted || !start_dl_timer(p)))
1057 return;
1058 dl_se->dl_throttled = 1;
Xunlei Pangae83b562017-05-10 21:03:37 +08001059 if (dl_se->runtime > 0)
1060 dl_se->runtime = 0;
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001061 }
1062}
1063
Dario Faggioliaab03e02013-11-28 11:14:43 +01001064static
Zhiqiang Zhang6fab5412015-06-15 11:15:20 +08001065int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001066{
Luca Abeni269ad802014-12-17 11:50:32 +01001067 return (dl_se->runtime <= 0);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001068}
1069
Juri Lellifaa59932014-02-21 11:37:15 +01001070extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
1071
Dario Faggioliaab03e02013-11-28 11:14:43 +01001072/*
Luca Abenic52f14d2017-05-18 22:13:31 +02001073 * This function implements the GRUB accounting rule:
1074 * according to the GRUB reclaiming algorithm, the runtime is
Luca Abenidaec5792017-05-18 22:13:36 +02001075 * not decreased as "dq = -dt", but as
1076 * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt",
1077 * where u is the utilization of the task, Umax is the maximum reclaimable
1078 * utilization, Uinact is the (per-runqueue) inactive utilization, computed
1079 * as the difference between the "total runqueue utilization" and the
1080 * runqueue active utilization, and Uextra is the (per runqueue) extra
1081 * reclaimable utilization.
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001082 * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations
Luca Abenidaec5792017-05-18 22:13:36 +02001083 * multiplied by 2^BW_SHIFT, the result has to be shifted right by
1084 * BW_SHIFT.
1085 * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT,
1086 * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
1087 * Since delta is a 64 bit variable, to have an overflow its value
1088 * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds.
1089 * So, overflow is not an issue here.
Luca Abenic52f14d2017-05-18 22:13:31 +02001090 */
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001091u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
Luca Abenic52f14d2017-05-18 22:13:31 +02001092{
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001093 u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
1094 u64 u_act;
Luca Abenidaec5792017-05-18 22:13:36 +02001095 u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT;
Luca Abenic52f14d2017-05-18 22:13:31 +02001096
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001097 /*
Luca Abenidaec5792017-05-18 22:13:36 +02001098 * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)},
1099 * we compare u_inact + rq->dl.extra_bw with
1100 * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because
1101 * u_inact + rq->dl.extra_bw can be larger than
1102 * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative
1103 * leading to wrong results)
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001104 */
Luca Abenidaec5792017-05-18 22:13:36 +02001105 if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min)
1106 u_act = u_act_min;
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001107 else
Luca Abenidaec5792017-05-18 22:13:36 +02001108 u_act = BW_UNIT - u_inact - rq->dl.extra_bw;
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001109
1110 return (delta * u_act) >> BW_SHIFT;
Luca Abenic52f14d2017-05-18 22:13:31 +02001111}
1112
1113/*
Dario Faggioliaab03e02013-11-28 11:14:43 +01001114 * Update the current task's runtime statistics (provided it is still
1115 * a -deadline task and has not been removed from the dl_rq).
1116 */
1117static void update_curr_dl(struct rq *rq)
1118{
1119 struct task_struct *curr = rq->curr;
1120 struct sched_dl_entity *dl_se = &curr->dl;
1121 u64 delta_exec;
1122
1123 if (!dl_task(curr) || !on_dl_rq(dl_se))
1124 return;
1125
1126 /*
1127 * Consumed budget is computed considering the time as
1128 * observed by schedulable tasks (excluding time spent
1129 * in hardirq context, etc.). Deadlines are instead
1130 * computed using hard walltime. This seems to be the more
1131 * natural solution, but the full ramifications of this
1132 * approach need further study.
1133 */
1134 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001135 if (unlikely((s64)delta_exec <= 0)) {
1136 if (unlikely(dl_se->dl_yielded))
1137 goto throttle;
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001138 return;
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001139 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001140
1141 schedstat_set(curr->se.statistics.exec_max,
1142 max(curr->se.statistics.exec_max, delta_exec));
1143
1144 curr->se.sum_exec_runtime += delta_exec;
1145 account_group_exec_runtime(curr, delta_exec);
1146
1147 curr->se.exec_start = rq_clock_task(rq);
Tejun Heod2cc5ed2017-09-25 08:12:04 -07001148 cgroup_account_cputime(curr, delta_exec);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001149
Dario Faggioli239be4a2013-11-07 14:43:39 +01001150 sched_rt_avg_update(rq, delta_exec);
1151
Luca Abeni2d4283e2017-05-18 22:13:33 +02001152 if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM))
Luca Abeni9f0d1a52017-05-18 22:13:35 +02001153 delta_exec = grub_reclaim(delta_exec, rq, &curr->dl);
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001154 dl_se->runtime -= delta_exec;
1155
1156throttle:
1157 if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
Peter Zijlstra1019a352014-11-26 08:44:03 +08001158 dl_se->dl_throttled = 1;
Juri Lelli34be3932017-12-12 12:10:24 +01001159
1160 /* If requested, inform the user about runtime overruns. */
1161 if (dl_runtime_exceeded(dl_se) &&
1162 (dl_se->flags & SCHED_FLAG_DL_OVERRUN))
1163 dl_se->dl_overrun = 1;
1164
Dario Faggioliaab03e02013-11-28 11:14:43 +01001165 __dequeue_task_dl(rq, curr, 0);
Peter Zijlstraa649f232015-06-11 14:46:49 +02001166 if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
Dario Faggioliaab03e02013-11-28 11:14:43 +01001167 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
1168
1169 if (!is_leftmost(curr, &rq->dl))
Kirill Tkhai88751252014-06-29 00:03:57 +04001170 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001171 }
Peter Zijlstra17248132013-12-17 12:44:49 +01001172
1173 /*
1174 * Because -- for now -- we share the rt bandwidth, we need to
1175 * account our runtime there too, otherwise actual rt tasks
1176 * would be able to exceed the shared quota.
1177 *
1178 * Account to the root rt group for now.
1179 *
1180 * The solution we're working towards is having the RT groups scheduled
1181 * using deadline servers -- however there's a few nasties to figure
1182 * out before that can happen.
1183 */
1184 if (rt_bandwidth_enabled()) {
1185 struct rt_rq *rt_rq = &rq->rt;
1186
1187 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra17248132013-12-17 12:44:49 +01001188 /*
1189 * We'll let actual RT tasks worry about the overflow here, we
Juri Lellifaa59932014-02-21 11:37:15 +01001190 * have our own CBS to keep us inline; only account when RT
1191 * bandwidth is relevant.
Peter Zijlstra17248132013-12-17 12:44:49 +01001192 */
Juri Lellifaa59932014-02-21 11:37:15 +01001193 if (sched_rt_bandwidth_account(rt_rq))
1194 rt_rq->rt_time += delta_exec;
Peter Zijlstra17248132013-12-17 12:44:49 +01001195 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1196 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001197}
1198
Luca Abeni209a0cb2017-05-18 22:13:29 +02001199static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
1200{
1201 struct sched_dl_entity *dl_se = container_of(timer,
1202 struct sched_dl_entity,
1203 inactive_timer);
1204 struct task_struct *p = dl_task_of(dl_se);
1205 struct rq_flags rf;
1206 struct rq *rq;
1207
1208 rq = task_rq_lock(p, &rf);
1209
1210 if (!dl_task(p) || p->state == TASK_DEAD) {
Luca Abeni387e3132017-05-18 22:13:30 +02001211 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1212
Luca Abeni209a0cb2017-05-18 22:13:29 +02001213 if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
1214 sub_running_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl));
Luca Abeni8fd27232017-05-18 22:13:34 +02001215 sub_rq_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl));
Luca Abeni209a0cb2017-05-18 22:13:29 +02001216 dl_se->dl_non_contending = 0;
1217 }
Luca Abeni387e3132017-05-18 22:13:30 +02001218
1219 raw_spin_lock(&dl_b->lock);
Peter Zijlstra8c0944ce2017-09-07 12:09:30 +02001220 __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
Luca Abeni387e3132017-05-18 22:13:30 +02001221 raw_spin_unlock(&dl_b->lock);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001222 __dl_clear_params(p);
1223
1224 goto unlock;
1225 }
1226 if (dl_se->dl_non_contending == 0)
1227 goto unlock;
1228
1229 sched_clock_tick();
1230 update_rq_clock(rq);
1231
1232 sub_running_bw(dl_se->dl_bw, &rq->dl);
1233 dl_se->dl_non_contending = 0;
1234unlock:
1235 task_rq_unlock(rq, p, &rf);
1236 put_task_struct(p);
1237
1238 return HRTIMER_NORESTART;
1239}
1240
1241void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1242{
1243 struct hrtimer *timer = &dl_se->inactive_timer;
1244
1245 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1246 timer->function = inactive_task_timer;
1247}
1248
Juri Lelli1baca4c2013-11-07 14:43:38 +01001249#ifdef CONFIG_SMP
1250
Juri Lelli1baca4c2013-11-07 14:43:38 +01001251static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1252{
1253 struct rq *rq = rq_of_dl_rq(dl_rq);
1254
1255 if (dl_rq->earliest_dl.curr == 0 ||
1256 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001257 dl_rq->earliest_dl.curr = deadline;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001258 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001259 }
1260}
1261
1262static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1263{
1264 struct rq *rq = rq_of_dl_rq(dl_rq);
1265
1266 /*
1267 * Since we may have removed our earliest (and/or next earliest)
1268 * task we must recompute them.
1269 */
1270 if (!dl_rq->dl_nr_running) {
1271 dl_rq->earliest_dl.curr = 0;
1272 dl_rq->earliest_dl.next = 0;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001273 cpudl_clear(&rq->rd->cpudl, rq->cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001274 } else {
Davidlohr Bueso21615732017-09-08 16:14:58 -07001275 struct rb_node *leftmost = dl_rq->root.rb_leftmost;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001276 struct sched_dl_entity *entry;
1277
1278 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
1279 dl_rq->earliest_dl.curr = entry->deadline;
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02001280 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001281 }
1282}
1283
1284#else
1285
1286static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1287static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1288
1289#endif /* CONFIG_SMP */
1290
1291static inline
1292void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1293{
1294 int prio = dl_task_of(dl_se)->prio;
1295 u64 deadline = dl_se->deadline;
1296
1297 WARN_ON(!dl_prio(prio));
1298 dl_rq->dl_nr_running++;
Kirill Tkhai72465442014-05-09 03:00:14 +04001299 add_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001300
1301 inc_dl_deadline(dl_rq, deadline);
1302 inc_dl_migration(dl_se, dl_rq);
1303}
1304
1305static inline
1306void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1307{
1308 int prio = dl_task_of(dl_se)->prio;
1309
1310 WARN_ON(!dl_prio(prio));
1311 WARN_ON(!dl_rq->dl_nr_running);
1312 dl_rq->dl_nr_running--;
Kirill Tkhai72465442014-05-09 03:00:14 +04001313 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001314
1315 dec_dl_deadline(dl_rq, dl_se->deadline);
1316 dec_dl_migration(dl_se, dl_rq);
1317}
1318
Dario Faggioliaab03e02013-11-28 11:14:43 +01001319static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1320{
1321 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
Davidlohr Bueso21615732017-09-08 16:14:58 -07001322 struct rb_node **link = &dl_rq->root.rb_root.rb_node;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001323 struct rb_node *parent = NULL;
1324 struct sched_dl_entity *entry;
1325 int leftmost = 1;
1326
1327 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
1328
1329 while (*link) {
1330 parent = *link;
1331 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
1332 if (dl_time_before(dl_se->deadline, entry->deadline))
1333 link = &parent->rb_left;
1334 else {
1335 link = &parent->rb_right;
1336 leftmost = 0;
1337 }
1338 }
1339
Dario Faggioliaab03e02013-11-28 11:14:43 +01001340 rb_link_node(&dl_se->rb_node, parent, link);
Davidlohr Bueso21615732017-09-08 16:14:58 -07001341 rb_insert_color_cached(&dl_se->rb_node, &dl_rq->root, leftmost);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001342
Juri Lelli1baca4c2013-11-07 14:43:38 +01001343 inc_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001344}
1345
1346static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1347{
1348 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1349
1350 if (RB_EMPTY_NODE(&dl_se->rb_node))
1351 return;
1352
Davidlohr Bueso21615732017-09-08 16:14:58 -07001353 rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001354 RB_CLEAR_NODE(&dl_se->rb_node);
1355
Juri Lelli1baca4c2013-11-07 14:43:38 +01001356 dec_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001357}
1358
1359static void
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001360enqueue_dl_entity(struct sched_dl_entity *dl_se,
1361 struct sched_dl_entity *pi_se, int flags)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001362{
1363 BUG_ON(on_dl_rq(dl_se));
1364
1365 /*
1366 * If this is a wakeup or a new instance, the scheduling
1367 * parameters of the task might need updating. Otherwise,
1368 * we want a replenishment of its runtime.
1369 */
Luca Abenie36d8672017-05-18 22:13:28 +02001370 if (flags & ENQUEUE_WAKEUP) {
Luca Abeni8fd27232017-05-18 22:13:34 +02001371 task_contending(dl_se, flags);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001372 update_dl_entity(dl_se, pi_se);
Luca Abenie36d8672017-05-18 22:13:28 +02001373 } else if (flags & ENQUEUE_REPLENISH) {
Luca Abeni6a503c32014-12-17 11:50:31 +01001374 replenish_dl_entity(dl_se, pi_se);
Luca Abeni295d6d52017-09-07 12:09:29 +02001375 } else if ((flags & ENQUEUE_RESTORE) &&
1376 dl_time_before(dl_se->deadline,
1377 rq_clock(rq_of_dl_rq(dl_rq_of_se(dl_se))))) {
1378 setup_new_dl_entity(dl_se);
Luca Abenie36d8672017-05-18 22:13:28 +02001379 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001380
1381 __enqueue_dl_entity(dl_se);
1382}
1383
1384static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
1385{
1386 __dequeue_dl_entity(dl_se);
1387}
1388
1389static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1390{
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001391 struct task_struct *pi_task = rt_mutex_get_top_task(p);
1392 struct sched_dl_entity *pi_se = &p->dl;
1393
1394 /*
Joel Fernandes193be412017-07-12 19:24:29 -07001395 * Use the scheduling parameters of the top pi-waiter task if:
1396 * - we have a top pi-waiter which is a SCHED_DEADLINE task AND
1397 * - our dl_boosted is set (i.e. the pi-waiter's (absolute) deadline is
1398 * smaller than our deadline OR we are a !SCHED_DEADLINE task getting
1399 * boosted due to a SCHED_DEADLINE pi-waiter).
1400 * Otherwise we keep our runtime and deadline.
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001401 */
Joel Fernandes193be412017-07-12 19:24:29 -07001402 if (pi_task && dl_prio(pi_task->normal_prio) && p->dl.dl_boosted) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001403 pi_se = &pi_task->dl;
Juri Lelli64be6f12014-10-24 10:16:37 +01001404 } else if (!dl_prio(p->normal_prio)) {
1405 /*
1406 * Special case in which we have a !SCHED_DEADLINE task
Joel Fernandes193be412017-07-12 19:24:29 -07001407 * that is going to be deboosted, but exceeds its
Juri Lelli64be6f12014-10-24 10:16:37 +01001408 * runtime while doing so. No point in replenishing
1409 * it, as it's going to return back to its original
1410 * scheduling class after this.
1411 */
1412 BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
1413 return;
1414 }
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001415
Dario Faggioliaab03e02013-11-28 11:14:43 +01001416 /*
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001417 * Check if a constrained deadline task was activated
1418 * after the deadline but before the next period.
1419 * If that is the case, the task will be throttled and
1420 * the replenishment timer will be set to the next period.
1421 */
Daniel Bristot de Oliveira3effcb42017-05-29 16:24:03 +02001422 if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl))
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001423 dl_check_constrained_dl(&p->dl);
1424
Luca Abeni8fd27232017-05-18 22:13:34 +02001425 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) {
1426 add_rq_bw(p->dl.dl_bw, &rq->dl);
Luca Abenie36d8672017-05-18 22:13:28 +02001427 add_running_bw(p->dl.dl_bw, &rq->dl);
Luca Abeni8fd27232017-05-18 22:13:34 +02001428 }
Luca Abenie36d8672017-05-18 22:13:28 +02001429
Daniel Bristot de Oliveiradf8eac82017-03-02 15:10:58 +01001430 /*
Luca Abenie36d8672017-05-18 22:13:28 +02001431 * If p is throttled, we do not enqueue it. In fact, if it exhausted
Dario Faggioliaab03e02013-11-28 11:14:43 +01001432 * its budget it needs a replenishment and, since it now is on
1433 * its rq, the bandwidth timer callback (which clearly has not
1434 * run yet) will take care of this.
Luca Abenie36d8672017-05-18 22:13:28 +02001435 * However, the active utilization does not depend on the fact
1436 * that the task is on the runqueue or not (but depends on the
1437 * task's state - in GRUB parlance, "inactive" vs "active contending").
1438 * In other words, even if a task is throttled its utilization must
1439 * be counted in the active utilization; hence, we need to call
1440 * add_running_bw().
Dario Faggioliaab03e02013-11-28 11:14:43 +01001441 */
Luca Abenie36d8672017-05-18 22:13:28 +02001442 if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
Luca Abeni209a0cb2017-05-18 22:13:29 +02001443 if (flags & ENQUEUE_WAKEUP)
Luca Abeni8fd27232017-05-18 22:13:34 +02001444 task_contending(&p->dl, flags);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001445
Dario Faggioliaab03e02013-11-28 11:14:43 +01001446 return;
Luca Abenie36d8672017-05-18 22:13:28 +02001447 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001448
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001449 enqueue_dl_entity(&p->dl, pi_se, flags);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001450
Ingo Molnar4b53a342017-02-05 15:41:03 +01001451 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001452 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001453}
1454
1455static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1456{
1457 dequeue_dl_entity(&p->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001458 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001459}
1460
1461static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1462{
1463 update_curr_dl(rq);
1464 __dequeue_task_dl(rq, p, flags);
Luca Abenie36d8672017-05-18 22:13:28 +02001465
Luca Abeni8fd27232017-05-18 22:13:34 +02001466 if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) {
Luca Abenie36d8672017-05-18 22:13:28 +02001467 sub_running_bw(p->dl.dl_bw, &rq->dl);
Luca Abeni8fd27232017-05-18 22:13:34 +02001468 sub_rq_bw(p->dl.dl_bw, &rq->dl);
1469 }
Luca Abenie36d8672017-05-18 22:13:28 +02001470
1471 /*
Luca Abeni209a0cb2017-05-18 22:13:29 +02001472 * This check allows to start the inactive timer (or to immediately
1473 * decrease the active utilization, if needed) in two cases:
Luca Abenie36d8672017-05-18 22:13:28 +02001474 * when the task blocks and when it is terminating
1475 * (p->state == TASK_DEAD). We can handle the two cases in the same
1476 * way, because from GRUB's point of view the same thing is happening
1477 * (the task moves from "active contending" to "active non contending"
1478 * or "inactive")
1479 */
1480 if (flags & DEQUEUE_SLEEP)
Luca Abeni209a0cb2017-05-18 22:13:29 +02001481 task_non_contending(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001482}
1483
1484/*
1485 * Yield task semantic for -deadline tasks is:
1486 *
1487 * get off from the CPU until our next instance, with
1488 * a new runtime. This is of little use now, since we
1489 * don't have a bandwidth reclaiming mechanism. Anyway,
1490 * bandwidth reclaiming is planned for the future, and
1491 * yield_task_dl will indicate that some spare budget
1492 * is available for other task instances to use it.
1493 */
1494static void yield_task_dl(struct rq *rq)
1495{
Dario Faggioliaab03e02013-11-28 11:14:43 +01001496 /*
1497 * We make the task go to sleep until its current deadline by
1498 * forcing its runtime to zero. This way, update_curr_dl() stops
1499 * it and the bandwidth timer will wake it up and will give it
Juri Lelli5bfd1262014-04-15 13:49:04 +02001500 * new scheduling parameters (thanks to dl_yielded=1).
Dario Faggioliaab03e02013-11-28 11:14:43 +01001501 */
Peter Zijlstra48be3a62016-02-23 13:28:22 +01001502 rq->curr->dl.dl_yielded = 1;
1503
Kirill Tkhai6f1607f2015-02-04 12:09:32 +03001504 update_rq_clock(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001505 update_curr_dl(rq);
Wanpeng Li44fb0852015-03-10 12:20:00 +08001506 /*
1507 * Tell update_rq_clock() that we've just updated,
1508 * so we don't do microscopic update in schedule()
1509 * and double the fastpath cost.
1510 */
1511 rq_clock_skip_update(rq, true);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001512}
1513
Juri Lelli1baca4c2013-11-07 14:43:38 +01001514#ifdef CONFIG_SMP
1515
1516static int find_later_rq(struct task_struct *task);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001517
1518static int
1519select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1520{
1521 struct task_struct *curr;
1522 struct rq *rq;
1523
Wanpeng Li1d7e9742014-10-14 10:22:39 +08001524 if (sd_flag != SD_BALANCE_WAKE)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001525 goto out;
1526
1527 rq = cpu_rq(cpu);
1528
1529 rcu_read_lock();
Jason Low316c1608d2015-04-28 13:00:20 -07001530 curr = READ_ONCE(rq->curr); /* unlocked access */
Juri Lelli1baca4c2013-11-07 14:43:38 +01001531
1532 /*
1533 * If we are dealing with a -deadline task, we must
1534 * decide where to wake it up.
1535 * If it has a later deadline and the current task
1536 * on this rq can't move (provided the waking task
1537 * can!) we prefer to send it somewhere else. On the
1538 * other hand, if it has a shorter deadline, we
1539 * try to make it stay here, it might be important.
1540 */
1541 if (unlikely(dl_task(curr)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001542 (curr->nr_cpus_allowed < 2 ||
Juri Lelli1baca4c2013-11-07 14:43:38 +01001543 !dl_entity_preempt(&p->dl, &curr->dl)) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001544 (p->nr_cpus_allowed > 1)) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001545 int target = find_later_rq(p);
1546
Wanpeng Li9d514262015-05-13 14:01:03 +08001547 if (target != -1 &&
Luca Abeni5aa50502015-10-16 10:06:21 +02001548 (dl_time_before(p->dl.deadline,
1549 cpu_rq(target)->dl.earliest_dl.curr) ||
1550 (cpu_rq(target)->dl.dl_nr_running == 0)))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001551 cpu = target;
1552 }
1553 rcu_read_unlock();
1554
1555out:
1556 return cpu;
1557}
1558
Luca Abeni209a0cb2017-05-18 22:13:29 +02001559static void migrate_task_rq_dl(struct task_struct *p)
1560{
1561 struct rq *rq;
1562
Luca Abeni8fd27232017-05-18 22:13:34 +02001563 if (p->state != TASK_WAKING)
Luca Abeni209a0cb2017-05-18 22:13:29 +02001564 return;
1565
1566 rq = task_rq(p);
1567 /*
1568 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1569 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1570 * rq->lock is not... So, lock it
1571 */
1572 raw_spin_lock(&rq->lock);
Luca Abeni8fd27232017-05-18 22:13:34 +02001573 if (p->dl.dl_non_contending) {
1574 sub_running_bw(p->dl.dl_bw, &rq->dl);
1575 p->dl.dl_non_contending = 0;
1576 /*
1577 * If the timer handler is currently running and the
1578 * timer cannot be cancelled, inactive_task_timer()
1579 * will see that dl_not_contending is not set, and
1580 * will not touch the rq's active utilization,
1581 * so we are still safe.
1582 */
1583 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1584 put_task_struct(p);
1585 }
1586 sub_rq_bw(p->dl.dl_bw, &rq->dl);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001587 raw_spin_unlock(&rq->lock);
1588}
1589
Juri Lelli1baca4c2013-11-07 14:43:38 +01001590static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1591{
1592 /*
1593 * Current can't be migrated, useless to reschedule,
1594 * let's hope p can move out.
1595 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001596 if (rq->curr->nr_cpus_allowed == 1 ||
Byungchul Park3261ed02017-05-23 11:00:57 +09001597 !cpudl_find(&rq->rd->cpudl, rq->curr, NULL))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001598 return;
1599
1600 /*
1601 * p is migratable, so let's not schedule it and
1602 * see if it is pushed or pulled somewhere else.
1603 */
Ingo Molnar4b53a342017-02-05 15:41:03 +01001604 if (p->nr_cpus_allowed != 1 &&
Byungchul Park3261ed02017-05-23 11:00:57 +09001605 cpudl_find(&rq->rd->cpudl, p, NULL))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001606 return;
1607
Kirill Tkhai88751252014-06-29 00:03:57 +04001608 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001609}
1610
1611#endif /* CONFIG_SMP */
1612
Dario Faggioliaab03e02013-11-28 11:14:43 +01001613/*
1614 * Only called when both the current and waking task are -deadline
1615 * tasks.
1616 */
1617static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1618 int flags)
1619{
Juri Lelli1baca4c2013-11-07 14:43:38 +01001620 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001621 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001622 return;
1623 }
1624
1625#ifdef CONFIG_SMP
1626 /*
1627 * In the unlikely case current and p have the same deadline
1628 * let us try to decide what's the best thing to do...
1629 */
Dario Faggioli332ac172013-11-07 14:43:45 +01001630 if ((p->dl.deadline == rq->curr->dl.deadline) &&
1631 !test_tsk_need_resched(rq->curr))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001632 check_preempt_equal_dl(rq, p);
1633#endif /* CONFIG_SMP */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001634}
1635
1636#ifdef CONFIG_SCHED_HRTICK
1637static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1638{
xiaofeng.yan177ef2a2014-08-26 03:15:41 +00001639 hrtick_start(rq, p->dl.runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001640}
Wanpeng Li36ce9882014-11-11 09:52:26 +08001641#else /* !CONFIG_SCHED_HRTICK */
1642static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1643{
1644}
Dario Faggioliaab03e02013-11-28 11:14:43 +01001645#endif
1646
1647static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1648 struct dl_rq *dl_rq)
1649{
Davidlohr Bueso21615732017-09-08 16:14:58 -07001650 struct rb_node *left = rb_first_cached(&dl_rq->root);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001651
1652 if (!left)
1653 return NULL;
1654
1655 return rb_entry(left, struct sched_dl_entity, rb_node);
1656}
1657
Viresh Kumar181a80d12017-04-27 13:58:59 +05301658static struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01001659pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001660{
1661 struct sched_dl_entity *dl_se;
1662 struct task_struct *p;
1663 struct dl_rq *dl_rq;
1664
1665 dl_rq = &rq->dl;
1666
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001667 if (need_pull_dl_task(rq, prev)) {
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001668 /*
1669 * This is OK, because current is on_cpu, which avoids it being
1670 * picked for load-balance and preemption/IRQs are still
1671 * disabled avoiding further scheduler activity on it and we're
1672 * being very careful to re-start the picking loop.
1673 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01001674 rq_unpin_lock(rq, rf);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001675 pull_dl_task(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01001676 rq_repin_lock(rq, rf);
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001677 /*
T.Zhou176cedc2016-11-23 08:48:32 +08001678 * pull_dl_task() can drop (and re-acquire) rq->lock; this
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001679 * means a stop task can slip in, in which case we need to
1680 * re-start task selection.
1681 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001682 if (rq->stop && task_on_rq_queued(rq->stop))
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001683 return RETRY_TASK;
1684 }
1685
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001686 /*
1687 * When prev is DL, we may throttle it in put_prev_task().
1688 * So, we update time before we check for dl_nr_running.
1689 */
1690 if (prev->sched_class == &dl_sched_class)
1691 update_curr_dl(rq);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001692
Dario Faggioliaab03e02013-11-28 11:14:43 +01001693 if (unlikely(!dl_rq->dl_nr_running))
1694 return NULL;
1695
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001696 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001697
Dario Faggioliaab03e02013-11-28 11:14:43 +01001698 dl_se = pick_next_dl_entity(rq, dl_rq);
1699 BUG_ON(!dl_se);
1700
1701 p = dl_task_of(dl_se);
1702 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001703
1704 /* Running task will never be pushed. */
Juri Lelli71362652014-01-14 12:03:51 +01001705 dequeue_pushable_dl_task(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001706
Dario Faggioliaab03e02013-11-28 11:14:43 +01001707 if (hrtick_enabled(rq))
1708 start_hrtick_dl(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001709
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02001710 queue_push_tasks(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001711
Dario Faggioliaab03e02013-11-28 11:14:43 +01001712 return p;
1713}
1714
1715static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1716{
1717 update_curr_dl(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001718
Ingo Molnar4b53a342017-02-05 15:41:03 +01001719 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001720 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001721}
1722
1723static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1724{
1725 update_curr_dl(rq);
1726
Wanpeng Lia7bebf42014-11-26 08:44:01 +08001727 /*
1728 * Even when we have runtime, update_curr_dl() might have resulted in us
1729 * not being the leftmost task anymore. In that case NEED_RESCHED will
1730 * be set and schedule() will start a new hrtick for the next task.
1731 */
1732 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
1733 is_leftmost(p, &rq->dl))
Dario Faggioliaab03e02013-11-28 11:14:43 +01001734 start_hrtick_dl(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001735}
1736
1737static void task_fork_dl(struct task_struct *p)
1738{
1739 /*
1740 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1741 * sched_fork()
1742 */
1743}
1744
Dario Faggioliaab03e02013-11-28 11:14:43 +01001745static void set_curr_task_dl(struct rq *rq)
1746{
1747 struct task_struct *p = rq->curr;
1748
1749 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001750
1751 /* You can't push away the running task */
1752 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001753}
1754
Juri Lelli1baca4c2013-11-07 14:43:38 +01001755#ifdef CONFIG_SMP
1756
1757/* Only try algorithms three times */
1758#define DL_MAX_TRIES 3
1759
1760static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1761{
1762 if (!task_running(rq, p) &&
Ingo Molnar0c98d342017-02-05 15:38:10 +01001763 cpumask_test_cpu(cpu, &p->cpus_allowed))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001764 return 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001765 return 0;
1766}
1767
Wanpeng Li8b5e7702015-05-13 14:01:01 +08001768/*
1769 * Return the earliest pushable rq's task, which is suitable to be executed
1770 * on the CPU, NULL otherwise:
1771 */
1772static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
1773{
Davidlohr Bueso21615732017-09-08 16:14:58 -07001774 struct rb_node *next_node = rq->dl.pushable_dl_tasks_root.rb_leftmost;
Wanpeng Li8b5e7702015-05-13 14:01:01 +08001775 struct task_struct *p = NULL;
1776
1777 if (!has_pushable_dl_tasks(rq))
1778 return NULL;
1779
1780next_node:
1781 if (next_node) {
1782 p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1783
1784 if (pick_dl_task(rq, p, cpu))
1785 return p;
1786
1787 next_node = rb_next(next_node);
1788 goto next_node;
1789 }
1790
1791 return NULL;
1792}
1793
Juri Lelli1baca4c2013-11-07 14:43:38 +01001794static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1795
1796static int find_later_rq(struct task_struct *task)
1797{
1798 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001799 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001800 int this_cpu = smp_processor_id();
Byungchul Parkb18c3ca2017-05-23 11:00:56 +09001801 int cpu = task_cpu(task);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001802
1803 /* Make sure the mask is initialized first */
1804 if (unlikely(!later_mask))
1805 return -1;
1806
Ingo Molnar4b53a342017-02-05 15:41:03 +01001807 if (task->nr_cpus_allowed == 1)
Juri Lelli1baca4c2013-11-07 14:43:38 +01001808 return -1;
1809
Juri Lelli91ec6772014-09-19 10:22:41 +01001810 /*
1811 * We have to consider system topology and task affinity
1812 * first, then we can look for a suitable cpu.
1813 */
Byungchul Park3261ed02017-05-23 11:00:57 +09001814 if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001815 return -1;
1816
1817 /*
Byungchul Parkb18c3ca2017-05-23 11:00:56 +09001818 * If we are here, some targets have been found, including
1819 * the most suitable which is, among the runqueues where the
1820 * current tasks have later deadlines than the task's one, the
1821 * rq with the latest possible one.
Juri Lelli1baca4c2013-11-07 14:43:38 +01001822 *
1823 * Now we check how well this matches with task's
1824 * affinity and system topology.
1825 *
1826 * The last cpu where the task run is our first
1827 * guess, since it is most likely cache-hot there.
1828 */
1829 if (cpumask_test_cpu(cpu, later_mask))
1830 return cpu;
1831 /*
1832 * Check if this_cpu is to be skipped (i.e., it is
1833 * not in the mask) or not.
1834 */
1835 if (!cpumask_test_cpu(this_cpu, later_mask))
1836 this_cpu = -1;
1837
1838 rcu_read_lock();
1839 for_each_domain(cpu, sd) {
1840 if (sd->flags & SD_WAKE_AFFINE) {
Byungchul Parkb18c3ca2017-05-23 11:00:56 +09001841 int best_cpu;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001842
1843 /*
1844 * If possible, preempting this_cpu is
1845 * cheaper than migrating.
1846 */
1847 if (this_cpu != -1 &&
1848 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1849 rcu_read_unlock();
1850 return this_cpu;
1851 }
1852
Byungchul Parkb18c3ca2017-05-23 11:00:56 +09001853 best_cpu = cpumask_first_and(later_mask,
1854 sched_domain_span(sd));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001855 /*
Byungchul Parkb18c3ca2017-05-23 11:00:56 +09001856 * Last chance: if a cpu being in both later_mask
1857 * and current sd span is valid, that becomes our
1858 * choice. Of course, the latest possible cpu is
1859 * already under consideration through later_mask.
Juri Lelli1baca4c2013-11-07 14:43:38 +01001860 */
Byungchul Parkb18c3ca2017-05-23 11:00:56 +09001861 if (best_cpu < nr_cpu_ids) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001862 rcu_read_unlock();
1863 return best_cpu;
1864 }
1865 }
1866 }
1867 rcu_read_unlock();
1868
1869 /*
1870 * At this point, all our guesses failed, we just return
1871 * 'something', and let the caller sort the things out.
1872 */
1873 if (this_cpu != -1)
1874 return this_cpu;
1875
1876 cpu = cpumask_any(later_mask);
1877 if (cpu < nr_cpu_ids)
1878 return cpu;
1879
1880 return -1;
1881}
1882
1883/* Locks the rq it finds */
1884static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1885{
1886 struct rq *later_rq = NULL;
1887 int tries;
1888 int cpu;
1889
1890 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1891 cpu = find_later_rq(task);
1892
1893 if ((cpu == -1) || (cpu == rq->cpu))
1894 break;
1895
1896 later_rq = cpu_rq(cpu);
1897
Luca Abeni5aa50502015-10-16 10:06:21 +02001898 if (later_rq->dl.dl_nr_running &&
1899 !dl_time_before(task->dl.deadline,
Wanpeng Li9d514262015-05-13 14:01:03 +08001900 later_rq->dl.earliest_dl.curr)) {
1901 /*
1902 * Target rq has tasks of equal or earlier deadline,
1903 * retrying does not release any lock and is unlikely
1904 * to yield a different result.
1905 */
1906 later_rq = NULL;
1907 break;
1908 }
1909
Juri Lelli1baca4c2013-11-07 14:43:38 +01001910 /* Retry if something changed. */
1911 if (double_lock_balance(rq, later_rq)) {
1912 if (unlikely(task_rq(task) != rq ||
Ingo Molnar0c98d342017-02-05 15:38:10 +01001913 !cpumask_test_cpu(later_rq->cpu, &task->cpus_allowed) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001914 task_running(rq, task) ||
Xunlei Pang13b5ab02016-05-09 12:11:31 +08001915 !dl_task(task) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001916 !task_on_rq_queued(task))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001917 double_unlock_balance(rq, later_rq);
1918 later_rq = NULL;
1919 break;
1920 }
1921 }
1922
1923 /*
1924 * If the rq we found has no -deadline task, or
1925 * its earliest one has a later deadline than our
1926 * task, the rq is a good one.
1927 */
1928 if (!later_rq->dl.dl_nr_running ||
1929 dl_time_before(task->dl.deadline,
1930 later_rq->dl.earliest_dl.curr))
1931 break;
1932
1933 /* Otherwise we try again. */
1934 double_unlock_balance(rq, later_rq);
1935 later_rq = NULL;
1936 }
1937
1938 return later_rq;
1939}
1940
1941static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1942{
1943 struct task_struct *p;
1944
1945 if (!has_pushable_dl_tasks(rq))
1946 return NULL;
1947
Davidlohr Bueso21615732017-09-08 16:14:58 -07001948 p = rb_entry(rq->dl.pushable_dl_tasks_root.rb_leftmost,
Juri Lelli1baca4c2013-11-07 14:43:38 +01001949 struct task_struct, pushable_dl_tasks);
1950
1951 BUG_ON(rq->cpu != task_cpu(p));
1952 BUG_ON(task_current(rq, p));
Ingo Molnar4b53a342017-02-05 15:41:03 +01001953 BUG_ON(p->nr_cpus_allowed <= 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001954
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001955 BUG_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001956 BUG_ON(!dl_task(p));
1957
1958 return p;
1959}
1960
1961/*
1962 * See if the non running -deadline tasks on this rq
1963 * can be sent to some other CPU where they can preempt
1964 * and start executing.
1965 */
1966static int push_dl_task(struct rq *rq)
1967{
1968 struct task_struct *next_task;
1969 struct rq *later_rq;
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08001970 int ret = 0;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001971
1972 if (!rq->dl.overloaded)
1973 return 0;
1974
1975 next_task = pick_next_pushable_dl_task(rq);
1976 if (!next_task)
1977 return 0;
1978
1979retry:
1980 if (unlikely(next_task == rq->curr)) {
1981 WARN_ON(1);
1982 return 0;
1983 }
1984
1985 /*
1986 * If next_task preempts rq->curr, and rq->curr
1987 * can move away, it makes sense to just reschedule
1988 * without going further in pushing next_task.
1989 */
1990 if (dl_task(rq->curr) &&
1991 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01001992 rq->curr->nr_cpus_allowed > 1) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001993 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001994 return 0;
1995 }
1996
1997 /* We might release rq lock */
1998 get_task_struct(next_task);
1999
2000 /* Will lock the rq it'll find */
2001 later_rq = find_lock_later_rq(next_task, rq);
2002 if (!later_rq) {
2003 struct task_struct *task;
2004
2005 /*
2006 * We must check all this again, since
2007 * find_lock_later_rq releases rq->lock and it is
2008 * then possible that next_task has migrated.
2009 */
2010 task = pick_next_pushable_dl_task(rq);
Byungchul Parka776b962017-05-12 10:05:59 +09002011 if (task == next_task) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01002012 /*
2013 * The task is still there. We don't try
2014 * again, some other cpu will pull it when ready.
2015 */
Juri Lelli1baca4c2013-11-07 14:43:38 +01002016 goto out;
2017 }
2018
2019 if (!task)
2020 /* No more tasks */
2021 goto out;
2022
2023 put_task_struct(next_task);
2024 next_task = task;
2025 goto retry;
2026 }
2027
2028 deactivate_task(rq, next_task, 0);
Luca Abenie36d8672017-05-18 22:13:28 +02002029 sub_running_bw(next_task->dl.dl_bw, &rq->dl);
Luca Abeni8fd27232017-05-18 22:13:34 +02002030 sub_rq_bw(next_task->dl.dl_bw, &rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002031 set_task_cpu(next_task, later_rq->cpu);
Luca Abeni8fd27232017-05-18 22:13:34 +02002032 add_rq_bw(next_task->dl.dl_bw, &later_rq->dl);
Luca Abenie36d8672017-05-18 22:13:28 +02002033 add_running_bw(next_task->dl.dl_bw, &later_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002034 activate_task(later_rq, next_task, 0);
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08002035 ret = 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002036
Kirill Tkhai88751252014-06-29 00:03:57 +04002037 resched_curr(later_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002038
2039 double_unlock_balance(rq, later_rq);
2040
2041out:
2042 put_task_struct(next_task);
2043
Wanpeng Lic51b8ab2014-11-06 15:22:44 +08002044 return ret;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002045}
2046
2047static void push_dl_tasks(struct rq *rq)
2048{
Andrea Parri4ffa08e2015-08-05 15:56:18 +02002049 /* push_dl_task() will return true if it moved a -deadline task */
Juri Lelli1baca4c2013-11-07 14:43:38 +01002050 while (push_dl_task(rq))
2051 ;
2052}
2053
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002054static void pull_dl_task(struct rq *this_rq)
Juri Lelli1baca4c2013-11-07 14:43:38 +01002055{
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002056 int this_cpu = this_rq->cpu, cpu;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002057 struct task_struct *p;
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002058 bool resched = false;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002059 struct rq *src_rq;
2060 u64 dmin = LONG_MAX;
2061
2062 if (likely(!dl_overloaded(this_rq)))
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002063 return;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002064
2065 /*
2066 * Match the barrier from dl_set_overloaded; this guarantees that if we
2067 * see overloaded we must also see the dlo_mask bit.
2068 */
2069 smp_rmb();
2070
2071 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2072 if (this_cpu == cpu)
2073 continue;
2074
2075 src_rq = cpu_rq(cpu);
2076
2077 /*
2078 * It looks racy, abd it is! However, as in sched_rt.c,
2079 * we are fine with this.
2080 */
2081 if (this_rq->dl.dl_nr_running &&
2082 dl_time_before(this_rq->dl.earliest_dl.curr,
2083 src_rq->dl.earliest_dl.next))
2084 continue;
2085
2086 /* Might drop this_rq->lock */
2087 double_lock_balance(this_rq, src_rq);
2088
2089 /*
2090 * If there are no more pullable tasks on the
2091 * rq, we're done with it.
2092 */
2093 if (src_rq->dl.dl_nr_running <= 1)
2094 goto skip;
2095
Wanpeng Li8b5e7702015-05-13 14:01:01 +08002096 p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002097
2098 /*
2099 * We found a task to be pulled if:
2100 * - it preempts our current (if there's one),
2101 * - it will preempt the last one we pulled (if any).
2102 */
2103 if (p && dl_time_before(p->dl.deadline, dmin) &&
2104 (!this_rq->dl.dl_nr_running ||
2105 dl_time_before(p->dl.deadline,
2106 this_rq->dl.earliest_dl.curr))) {
2107 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002108 WARN_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01002109
2110 /*
2111 * Then we pull iff p has actually an earlier
2112 * deadline than the current task of its runqueue.
2113 */
2114 if (dl_time_before(p->dl.deadline,
2115 src_rq->curr->dl.deadline))
2116 goto skip;
2117
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002118 resched = true;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002119
2120 deactivate_task(src_rq, p, 0);
Luca Abenie36d8672017-05-18 22:13:28 +02002121 sub_running_bw(p->dl.dl_bw, &src_rq->dl);
Luca Abeni8fd27232017-05-18 22:13:34 +02002122 sub_rq_bw(p->dl.dl_bw, &src_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002123 set_task_cpu(p, this_cpu);
Luca Abeni8fd27232017-05-18 22:13:34 +02002124 add_rq_bw(p->dl.dl_bw, &this_rq->dl);
Luca Abenie36d8672017-05-18 22:13:28 +02002125 add_running_bw(p->dl.dl_bw, &this_rq->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002126 activate_task(this_rq, p, 0);
2127 dmin = p->dl.deadline;
2128
2129 /* Is there any other task even earlier? */
2130 }
2131skip:
2132 double_unlock_balance(this_rq, src_rq);
2133 }
2134
Peter Zijlstra0ea60c22015-06-11 14:46:42 +02002135 if (resched)
2136 resched_curr(this_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002137}
2138
2139/*
2140 * Since the task is not running and a reschedule is not going to happen
2141 * anytime soon on its runqueue, we try pushing it away now.
2142 */
2143static void task_woken_dl(struct rq *rq, struct task_struct *p)
2144{
2145 if (!task_running(rq, p) &&
2146 !test_tsk_need_resched(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01002147 p->nr_cpus_allowed > 1 &&
Juri Lelli1baca4c2013-11-07 14:43:38 +01002148 dl_task(rq->curr) &&
Ingo Molnar4b53a342017-02-05 15:41:03 +01002149 (rq->curr->nr_cpus_allowed < 2 ||
Wanpeng Li6b0a5632014-10-31 06:39:34 +08002150 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01002151 push_dl_tasks(rq);
2152 }
2153}
2154
2155static void set_cpus_allowed_dl(struct task_struct *p,
2156 const struct cpumask *new_mask)
2157{
Juri Lelli7f514122014-09-19 10:22:40 +01002158 struct root_domain *src_rd;
Peter Zijlstra6c370672015-05-15 17:43:36 +02002159 struct rq *rq;
Juri Lelli1baca4c2013-11-07 14:43:38 +01002160
2161 BUG_ON(!dl_task(p));
2162
Juri Lelli7f514122014-09-19 10:22:40 +01002163 rq = task_rq(p);
2164 src_rd = rq->rd;
2165 /*
2166 * Migrating a SCHED_DEADLINE task between exclusive
2167 * cpusets (different root_domains) entails a bandwidth
2168 * update. We already made space for us in the destination
2169 * domain (see cpuset_can_attach()).
2170 */
2171 if (!cpumask_intersects(src_rd->span, new_mask)) {
2172 struct dl_bw *src_dl_b;
2173
2174 src_dl_b = dl_bw_of(cpu_of(rq));
2175 /*
2176 * We now free resources of the root_domain we are migrating
2177 * off. In the worst case, sched_setattr() may temporary fail
2178 * until we complete the update.
2179 */
2180 raw_spin_lock(&src_dl_b->lock);
Peter Zijlstra8c0944ce2017-09-07 12:09:30 +02002181 __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
Juri Lelli7f514122014-09-19 10:22:40 +01002182 raw_spin_unlock(&src_dl_b->lock);
2183 }
2184
Peter Zijlstra6c370672015-05-15 17:43:36 +02002185 set_cpus_allowed_common(p, new_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002186}
2187
2188/* Assumes rq->lock is held */
2189static void rq_online_dl(struct rq *rq)
2190{
2191 if (rq->dl.overloaded)
2192 dl_set_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002193
Xunlei Pang16b26942015-01-19 04:49:36 +00002194 cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002195 if (rq->dl.dl_nr_running > 0)
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02002196 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002197}
2198
2199/* Assumes rq->lock is held */
2200static void rq_offline_dl(struct rq *rq)
2201{
2202 if (rq->dl.overloaded)
2203 dl_clear_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01002204
Tommaso Cucinottad8206bb2016-08-14 16:27:08 +02002205 cpudl_clear(&rq->rd->cpudl, rq->cpu);
Xunlei Pang16b26942015-01-19 04:49:36 +00002206 cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002207}
2208
Wanpeng Lia6c0e742015-05-13 14:01:02 +08002209void __init init_sched_dl_class(void)
Juri Lelli1baca4c2013-11-07 14:43:38 +01002210{
2211 unsigned int i;
2212
2213 for_each_possible_cpu(i)
2214 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2215 GFP_KERNEL, cpu_to_node(i));
2216}
2217
2218#endif /* CONFIG_SMP */
2219
Dario Faggioliaab03e02013-11-28 11:14:43 +01002220static void switched_from_dl(struct rq *rq, struct task_struct *p)
2221{
Peter Zijlstraa649f232015-06-11 14:46:49 +02002222 /*
Luca Abeni209a0cb2017-05-18 22:13:29 +02002223 * task_non_contending() can start the "inactive timer" (if the 0-lag
2224 * time is in the future). If the task switches back to dl before
2225 * the "inactive timer" fires, it can continue to consume its current
2226 * runtime using its current deadline. If it stays outside of
2227 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2228 * will reset the task parameters.
Peter Zijlstraa649f232015-06-11 14:46:49 +02002229 */
Luca Abeni209a0cb2017-05-18 22:13:29 +02002230 if (task_on_rq_queued(p) && p->dl.dl_runtime)
2231 task_non_contending(p);
2232
Luca Abeni8fd27232017-05-18 22:13:34 +02002233 if (!task_on_rq_queued(p))
2234 sub_rq_bw(p->dl.dl_bw, &rq->dl);
2235
Luca Abeni209a0cb2017-05-18 22:13:29 +02002236 /*
2237 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2238 * at the 0-lag time, because the task could have been migrated
2239 * while SCHED_OTHER in the meanwhile.
2240 */
2241 if (p->dl.dl_non_contending)
2242 p->dl.dl_non_contending = 0;
Juri Lellia5e7be32014-09-19 10:22:39 +01002243
Juri Lelli1baca4c2013-11-07 14:43:38 +01002244 /*
2245 * Since this might be the only -deadline task on the rq,
2246 * this is the right place to try to pull some other one
2247 * from an overloaded cpu, if any.
2248 */
Wanpeng Licd660912014-10-31 06:39:35 +08002249 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2250 return;
2251
Peter Zijlstra9916e212015-06-11 14:46:43 +02002252 queue_pull_task(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002253}
2254
Juri Lelli1baca4c2013-11-07 14:43:38 +01002255/*
2256 * When switching to -deadline, we may overload the rq, then
2257 * we try to push someone off, if possible.
2258 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002259static void switched_to_dl(struct rq *rq, struct task_struct *p)
2260{
Luca Abeni209a0cb2017-05-18 22:13:29 +02002261 if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2262 put_task_struct(p);
Luca Abeni72f9f3f2016-03-07 12:27:04 +01002263
Juri Lelli98b0a852016-08-05 16:07:55 +01002264 /* If p is not queued we will update its parameters at next wakeup. */
Luca Abeni8fd27232017-05-18 22:13:34 +02002265 if (!task_on_rq_queued(p)) {
2266 add_rq_bw(p->dl.dl_bw, &rq->dl);
Juri Lelli98b0a852016-08-05 16:07:55 +01002267
Luca Abeni8fd27232017-05-18 22:13:34 +02002268 return;
2269 }
Juri Lelli98b0a852016-08-05 16:07:55 +01002270
2271 if (rq->curr != p) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01002272#ifdef CONFIG_SMP
Ingo Molnar4b53a342017-02-05 15:41:03 +01002273 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
Peter Zijlstra9916e212015-06-11 14:46:43 +02002274 queue_push_tasks(rq);
Sebastian Andrzej Siewior619bd4a2017-01-24 15:40:06 +01002275#endif
Peter Zijlstra9916e212015-06-11 14:46:43 +02002276 if (dl_task(rq->curr))
2277 check_preempt_curr_dl(rq, p, 0);
2278 else
2279 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002280 }
2281}
2282
Juri Lelli1baca4c2013-11-07 14:43:38 +01002283/*
2284 * If the scheduling parameters of a -deadline task changed,
2285 * a push or pull operation might be needed.
2286 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002287static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2288 int oldprio)
2289{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002290 if (task_on_rq_queued(p) || rq->curr == p) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01002291#ifdef CONFIG_SMP
Juri Lelli1baca4c2013-11-07 14:43:38 +01002292 /*
2293 * This might be too much, but unfortunately
2294 * we don't have the old deadline value, and
2295 * we can't argue if the task is increasing
2296 * or lowering its prio, so...
2297 */
2298 if (!rq->dl.overloaded)
Peter Zijlstra9916e212015-06-11 14:46:43 +02002299 queue_pull_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002300
2301 /*
2302 * If we now have a earlier deadline task than p,
2303 * then reschedule, provided p is still on this
2304 * runqueue.
2305 */
Peter Zijlstra9916e212015-06-11 14:46:43 +02002306 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
Kirill Tkhai88751252014-06-29 00:03:57 +04002307 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002308#else
2309 /*
2310 * Again, we don't know if p has a earlier
2311 * or later deadline, so let's blindly set a
2312 * (maybe not needed) rescheduling point.
2313 */
Kirill Tkhai88751252014-06-29 00:03:57 +04002314 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01002315#endif /* CONFIG_SMP */
Peter Zijlstra801ccdb2016-02-25 15:01:49 +01002316 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01002317}
Dario Faggioliaab03e02013-11-28 11:14:43 +01002318
2319const struct sched_class dl_sched_class = {
2320 .next = &rt_sched_class,
2321 .enqueue_task = enqueue_task_dl,
2322 .dequeue_task = dequeue_task_dl,
2323 .yield_task = yield_task_dl,
2324
2325 .check_preempt_curr = check_preempt_curr_dl,
2326
2327 .pick_next_task = pick_next_task_dl,
2328 .put_prev_task = put_prev_task_dl,
2329
2330#ifdef CONFIG_SMP
2331 .select_task_rq = select_task_rq_dl,
Luca Abeni209a0cb2017-05-18 22:13:29 +02002332 .migrate_task_rq = migrate_task_rq_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01002333 .set_cpus_allowed = set_cpus_allowed_dl,
2334 .rq_online = rq_online_dl,
2335 .rq_offline = rq_offline_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01002336 .task_woken = task_woken_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002337#endif
2338
2339 .set_curr_task = set_curr_task_dl,
2340 .task_tick = task_tick_dl,
2341 .task_fork = task_fork_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002342
2343 .prio_changed = prio_changed_dl,
2344 .switched_from = switched_from_dl,
2345 .switched_to = switched_to_dl,
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01002346
2347 .update_curr = update_curr_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01002348};
Wanpeng Liacb32132014-10-31 06:39:33 +08002349
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04002350int sched_dl_global_validate(void)
2351{
2352 u64 runtime = global_rt_runtime();
2353 u64 period = global_rt_period();
2354 u64 new_bw = to_ratio(period, runtime);
2355 struct dl_bw *dl_b;
2356 int cpu, ret = 0;
2357 unsigned long flags;
2358
2359 /*
2360 * Here we want to check the bandwidth not being set to some
2361 * value smaller than the currently allocated bandwidth in
2362 * any of the root_domains.
2363 *
2364 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
2365 * cycling on root_domains... Discussion on different/better
2366 * solutions is welcome!
2367 */
2368 for_each_possible_cpu(cpu) {
2369 rcu_read_lock_sched();
2370 dl_b = dl_bw_of(cpu);
2371
2372 raw_spin_lock_irqsave(&dl_b->lock, flags);
2373 if (new_bw < dl_b->total_bw)
2374 ret = -EBUSY;
2375 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2376
2377 rcu_read_unlock_sched();
2378
2379 if (ret)
2380 break;
2381 }
2382
2383 return ret;
2384}
2385
2386void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
2387{
2388 if (global_rt_runtime() == RUNTIME_INF) {
2389 dl_rq->bw_ratio = 1 << RATIO_SHIFT;
2390 dl_rq->extra_bw = 1 << BW_SHIFT;
2391 } else {
2392 dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
2393 global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
2394 dl_rq->extra_bw = to_ratio(global_rt_period(),
2395 global_rt_runtime());
2396 }
2397}
2398
2399void sched_dl_do_global(void)
2400{
2401 u64 new_bw = -1;
2402 struct dl_bw *dl_b;
2403 int cpu;
2404 unsigned long flags;
2405
2406 def_dl_bandwidth.dl_period = global_rt_period();
2407 def_dl_bandwidth.dl_runtime = global_rt_runtime();
2408
2409 if (global_rt_runtime() != RUNTIME_INF)
2410 new_bw = to_ratio(global_rt_period(), global_rt_runtime());
2411
2412 /*
2413 * FIXME: As above...
2414 */
2415 for_each_possible_cpu(cpu) {
2416 rcu_read_lock_sched();
2417 dl_b = dl_bw_of(cpu);
2418
2419 raw_spin_lock_irqsave(&dl_b->lock, flags);
2420 dl_b->bw = new_bw;
2421 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2422
2423 rcu_read_unlock_sched();
2424 init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
2425 }
2426}
2427
2428/*
2429 * We must be sure that accepting a new task (or allowing changing the
2430 * parameters of an existing one) is consistent with the bandwidth
2431 * constraints. If yes, this function also accordingly updates the currently
2432 * allocated bandwidth to reflect the new situation.
2433 *
2434 * This function is called while holding p's rq->lock.
2435 */
2436int sched_dl_overflow(struct task_struct *p, int policy,
2437 const struct sched_attr *attr)
2438{
2439 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
2440 u64 period = attr->sched_period ?: attr->sched_deadline;
2441 u64 runtime = attr->sched_runtime;
2442 u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
2443 int cpus, err = -1;
2444
2445 /* !deadline task may carry old deadline bandwidth */
2446 if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
2447 return 0;
2448
2449 /*
2450 * Either if a task, enters, leave, or stays -deadline but changes
2451 * its parameters, we may need to update accordingly the total
2452 * allocated bandwidth of the container.
2453 */
2454 raw_spin_lock(&dl_b->lock);
2455 cpus = dl_bw_cpus(task_cpu(p));
2456 if (dl_policy(policy) && !task_has_dl_policy(p) &&
2457 !__dl_overflow(dl_b, cpus, 0, new_bw)) {
2458 if (hrtimer_active(&p->dl.inactive_timer))
Peter Zijlstra8c0944ce2017-09-07 12:09:30 +02002459 __dl_sub(dl_b, p->dl.dl_bw, cpus);
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04002460 __dl_add(dl_b, new_bw, cpus);
2461 err = 0;
2462 } else if (dl_policy(policy) && task_has_dl_policy(p) &&
2463 !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
2464 /*
2465 * XXX this is slightly incorrect: when the task
2466 * utilization decreases, we should delay the total
2467 * utilization change until the task's 0-lag point.
2468 * But this would require to set the task's "inactive
2469 * timer" when the task is not inactive.
2470 */
Peter Zijlstra8c0944ce2017-09-07 12:09:30 +02002471 __dl_sub(dl_b, p->dl.dl_bw, cpus);
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04002472 __dl_add(dl_b, new_bw, cpus);
2473 dl_change_utilization(p, new_bw);
2474 err = 0;
2475 } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2476 /*
2477 * Do not decrease the total deadline utilization here,
2478 * switched_from_dl() will take care to do it at the correct
2479 * (0-lag) time.
2480 */
2481 err = 0;
2482 }
2483 raw_spin_unlock(&dl_b->lock);
2484
2485 return err;
2486}
2487
2488/*
2489 * This function initializes the sched_dl_entity of a newly becoming
2490 * SCHED_DEADLINE task.
2491 *
2492 * Only the static values are considered here, the actual runtime and the
2493 * absolute deadline will be properly calculated when the task is enqueued
2494 * for the first time with its new policy.
2495 */
2496void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
2497{
2498 struct sched_dl_entity *dl_se = &p->dl;
2499
2500 dl_se->dl_runtime = attr->sched_runtime;
2501 dl_se->dl_deadline = attr->sched_deadline;
2502 dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
2503 dl_se->flags = attr->sched_flags;
2504 dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
2505 dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
2506}
2507
2508void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
2509{
2510 struct sched_dl_entity *dl_se = &p->dl;
2511
2512 attr->sched_priority = p->rt_priority;
2513 attr->sched_runtime = dl_se->dl_runtime;
2514 attr->sched_deadline = dl_se->dl_deadline;
2515 attr->sched_period = dl_se->dl_period;
2516 attr->sched_flags = dl_se->flags;
2517}
2518
2519/*
2520 * This function validates the new parameters of a -deadline task.
2521 * We ask for the deadline not being zero, and greater or equal
2522 * than the runtime, as well as the period of being zero or
2523 * greater than deadline. Furthermore, we have to be sure that
2524 * user parameters are above the internal resolution of 1us (we
2525 * check sched_runtime only since it is always the smaller one) and
2526 * below 2^63 ns (we have to check both sched_deadline and
2527 * sched_period, as the latter can be zero).
2528 */
2529bool __checkparam_dl(const struct sched_attr *attr)
2530{
2531 /* deadline != 0 */
2532 if (attr->sched_deadline == 0)
2533 return false;
2534
2535 /*
2536 * Since we truncate DL_SCALE bits, make sure we're at least
2537 * that big.
2538 */
2539 if (attr->sched_runtime < (1ULL << DL_SCALE))
2540 return false;
2541
2542 /*
2543 * Since we use the MSB for wrap-around and sign issues, make
2544 * sure it's not set (mind that period can be equal to zero).
2545 */
2546 if (attr->sched_deadline & (1ULL << 63) ||
2547 attr->sched_period & (1ULL << 63))
2548 return false;
2549
2550 /* runtime <= deadline <= period (if period != 0) */
2551 if ((attr->sched_period != 0 &&
2552 attr->sched_period < attr->sched_deadline) ||
2553 attr->sched_deadline < attr->sched_runtime)
2554 return false;
2555
2556 return true;
2557}
2558
2559/*
2560 * This function clears the sched_dl_entity static params.
2561 */
2562void __dl_clear_params(struct task_struct *p)
2563{
2564 struct sched_dl_entity *dl_se = &p->dl;
2565
2566 dl_se->dl_runtime = 0;
2567 dl_se->dl_deadline = 0;
2568 dl_se->dl_period = 0;
2569 dl_se->flags = 0;
2570 dl_se->dl_bw = 0;
2571 dl_se->dl_density = 0;
2572
2573 dl_se->dl_throttled = 0;
2574 dl_se->dl_yielded = 0;
2575 dl_se->dl_non_contending = 0;
Juri Lelli34be3932017-12-12 12:10:24 +01002576 dl_se->dl_overrun = 0;
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04002577}
2578
2579bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
2580{
2581 struct sched_dl_entity *dl_se = &p->dl;
2582
2583 if (dl_se->dl_runtime != attr->sched_runtime ||
2584 dl_se->dl_deadline != attr->sched_deadline ||
2585 dl_se->dl_period != attr->sched_period ||
2586 dl_se->flags != attr->sched_flags)
2587 return true;
2588
2589 return false;
2590}
2591
2592#ifdef CONFIG_SMP
2593int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed)
2594{
2595 unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
2596 cs_cpus_allowed);
2597 struct dl_bw *dl_b;
2598 bool overflow;
2599 int cpus, ret;
2600 unsigned long flags;
2601
2602 rcu_read_lock_sched();
2603 dl_b = dl_bw_of(dest_cpu);
2604 raw_spin_lock_irqsave(&dl_b->lock, flags);
2605 cpus = dl_bw_cpus(dest_cpu);
2606 overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
2607 if (overflow)
2608 ret = -EBUSY;
2609 else {
2610 /*
2611 * We reserve space for this task in the destination
2612 * root_domain, as we can't fail after this point.
2613 * We will free resources in the source root_domain
2614 * later on (see set_cpus_allowed_dl()).
2615 */
2616 __dl_add(dl_b, p->dl.dl_bw, cpus);
2617 ret = 0;
2618 }
2619 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2620 rcu_read_unlock_sched();
2621 return ret;
2622}
2623
2624int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
2625 const struct cpumask *trial)
2626{
2627 int ret = 1, trial_cpus;
2628 struct dl_bw *cur_dl_b;
2629 unsigned long flags;
2630
2631 rcu_read_lock_sched();
2632 cur_dl_b = dl_bw_of(cpumask_any(cur));
2633 trial_cpus = cpumask_weight(trial);
2634
2635 raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
2636 if (cur_dl_b->bw != -1 &&
2637 cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
2638 ret = 0;
2639 raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
2640 rcu_read_unlock_sched();
2641 return ret;
2642}
2643
2644bool dl_cpu_busy(unsigned int cpu)
2645{
2646 unsigned long flags;
2647 struct dl_bw *dl_b;
2648 bool overflow;
2649 int cpus;
2650
2651 rcu_read_lock_sched();
2652 dl_b = dl_bw_of(cpu);
2653 raw_spin_lock_irqsave(&dl_b->lock, flags);
2654 cpus = dl_bw_cpus(cpu);
2655 overflow = __dl_overflow(dl_b, cpus, 0, 0);
2656 raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2657 rcu_read_unlock_sched();
2658 return overflow;
2659}
2660#endif
2661
Wanpeng Liacb32132014-10-31 06:39:33 +08002662#ifdef CONFIG_SCHED_DEBUG
2663extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
2664
2665void print_dl_stats(struct seq_file *m, int cpu)
2666{
2667 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
2668}
2669#endif /* CONFIG_SCHED_DEBUG */