]>
Commit | Line | Data |
---|---|---|
aab03e05 DF |
1 | /* |
2 | * Deadline Scheduling Class (SCHED_DEADLINE) | |
3 | * | |
4 | * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS). | |
5 | * | |
6 | * Tasks that periodically executes their instances for less than their | |
7 | * runtime won't miss any of their deadlines. | |
8 | * Tasks that are not periodic or sporadic or that tries to execute more | |
9 | * than their reserved bandwidth will be slowed down (and may potentially | |
10 | * miss some of their deadlines), and won't affect any other task. | |
11 | * | |
12 | * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>, | |
1baca4ce | 13 | * Juri Lelli <juri.lelli@gmail.com>, |
aab03e05 DF |
14 | * Michael Trimarchi <michael@amarulasolutions.com>, |
15 | * Fabio Checconi <fchecconi@gmail.com> | |
16 | */ | |
17 | #include "sched.h" | |
18 | ||
6bfd6d72 JL |
19 | #include <linux/slab.h> |
20 | ||
332ac17e DF |
21 | struct dl_bandwidth def_dl_bandwidth; |
22 | ||
aab03e05 DF |
23 | static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se) |
24 | { | |
25 | return container_of(dl_se, struct task_struct, dl); | |
26 | } | |
27 | ||
28 | static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq) | |
29 | { | |
30 | return container_of(dl_rq, struct rq, dl); | |
31 | } | |
32 | ||
33 | static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se) | |
34 | { | |
35 | struct task_struct *p = dl_task_of(dl_se); | |
36 | struct rq *rq = task_rq(p); | |
37 | ||
38 | return &rq->dl; | |
39 | } | |
40 | ||
41 | static inline int on_dl_rq(struct sched_dl_entity *dl_se) | |
42 | { | |
43 | return !RB_EMPTY_NODE(&dl_se->rb_node); | |
44 | } | |
45 | ||
e36d8677 LA |
46 | static inline |
47 | void add_running_bw(u64 dl_bw, struct dl_rq *dl_rq) | |
48 | { | |
49 | u64 old = dl_rq->running_bw; | |
50 | ||
51 | lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock); | |
52 | dl_rq->running_bw += dl_bw; | |
53 | SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */ | |
8fd27231 | 54 | SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw); |
e36d8677 LA |
55 | } |
56 | ||
57 | static inline | |
58 | void sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq) | |
59 | { | |
60 | u64 old = dl_rq->running_bw; | |
61 | ||
62 | lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock); | |
63 | dl_rq->running_bw -= dl_bw; | |
64 | SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */ | |
65 | if (dl_rq->running_bw > old) | |
66 | dl_rq->running_bw = 0; | |
67 | } | |
68 | ||
8fd27231 LA |
69 | static inline |
70 | void add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq) | |
71 | { | |
72 | u64 old = dl_rq->this_bw; | |
73 | ||
74 | lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock); | |
75 | dl_rq->this_bw += dl_bw; | |
76 | SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */ | |
77 | } | |
78 | ||
79 | static inline | |
80 | void sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq) | |
81 | { | |
82 | u64 old = dl_rq->this_bw; | |
83 | ||
84 | lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock); | |
85 | dl_rq->this_bw -= dl_bw; | |
86 | SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */ | |
87 | if (dl_rq->this_bw > old) | |
88 | dl_rq->this_bw = 0; | |
89 | SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw); | |
90 | } | |
91 | ||
209a0cbd LA |
92 | void dl_change_utilization(struct task_struct *p, u64 new_bw) |
93 | { | |
8fd27231 | 94 | struct rq *rq; |
209a0cbd | 95 | |
8fd27231 | 96 | if (task_on_rq_queued(p)) |
209a0cbd LA |
97 | return; |
98 | ||
8fd27231 LA |
99 | rq = task_rq(p); |
100 | if (p->dl.dl_non_contending) { | |
101 | sub_running_bw(p->dl.dl_bw, &rq->dl); | |
102 | p->dl.dl_non_contending = 0; | |
103 | /* | |
104 | * If the timer handler is currently running and the | |
105 | * timer cannot be cancelled, inactive_task_timer() | |
106 | * will see that dl_not_contending is not set, and | |
107 | * will not touch the rq's active utilization, | |
108 | * so we are still safe. | |
109 | */ | |
110 | if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) | |
111 | put_task_struct(p); | |
112 | } | |
113 | sub_rq_bw(p->dl.dl_bw, &rq->dl); | |
114 | add_rq_bw(new_bw, &rq->dl); | |
209a0cbd LA |
115 | } |
116 | ||
117 | /* | |
118 | * The utilization of a task cannot be immediately removed from | |
119 | * the rq active utilization (running_bw) when the task blocks. | |
120 | * Instead, we have to wait for the so called "0-lag time". | |
121 | * | |
122 | * If a task blocks before the "0-lag time", a timer (the inactive | |
123 | * timer) is armed, and running_bw is decreased when the timer | |
124 | * fires. | |
125 | * | |
126 | * If the task wakes up again before the inactive timer fires, | |
127 | * the timer is cancelled, whereas if the task wakes up after the | |
128 | * inactive timer fired (and running_bw has been decreased) the | |
129 | * task's utilization has to be added to running_bw again. | |
130 | * A flag in the deadline scheduling entity (dl_non_contending) | |
131 | * is used to avoid race conditions between the inactive timer handler | |
132 | * and task wakeups. | |
133 | * | |
134 | * The following diagram shows how running_bw is updated. A task is | |
135 | * "ACTIVE" when its utilization contributes to running_bw; an | |
136 | * "ACTIVE contending" task is in the TASK_RUNNING state, while an | |
137 | * "ACTIVE non contending" task is a blocked task for which the "0-lag time" | |
138 | * has not passed yet. An "INACTIVE" task is a task for which the "0-lag" | |
139 | * time already passed, which does not contribute to running_bw anymore. | |
140 | * +------------------+ | |
141 | * wakeup | ACTIVE | | |
142 | * +------------------>+ contending | | |
143 | * | add_running_bw | | | |
144 | * | +----+------+------+ | |
145 | * | | ^ | |
146 | * | dequeue | | | |
147 | * +--------+-------+ | | | |
148 | * | | t >= 0-lag | | wakeup | |
149 | * | INACTIVE |<---------------+ | | |
150 | * | | sub_running_bw | | | |
151 | * +--------+-------+ | | | |
152 | * ^ | | | |
153 | * | t < 0-lag | | | |
154 | * | | | | |
155 | * | V | | |
156 | * | +----+------+------+ | |
157 | * | sub_running_bw | ACTIVE | | |
158 | * +-------------------+ | | |
159 | * inactive timer | non contending | | |
160 | * fired +------------------+ | |
161 | * | |
162 | * The task_non_contending() function is invoked when a task | |
163 | * blocks, and checks if the 0-lag time already passed or | |
164 | * not (in the first case, it directly updates running_bw; | |
165 | * in the second case, it arms the inactive timer). | |
166 | * | |
167 | * The task_contending() function is invoked when a task wakes | |
168 | * up, and checks if the task is still in the "ACTIVE non contending" | |
169 | * state or not (in the second case, it updates running_bw). | |
170 | */ | |
171 | static void task_non_contending(struct task_struct *p) | |
172 | { | |
173 | struct sched_dl_entity *dl_se = &p->dl; | |
174 | struct hrtimer *timer = &dl_se->inactive_timer; | |
175 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
176 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
177 | s64 zerolag_time; | |
178 | ||
179 | /* | |
180 | * If this is a non-deadline task that has been boosted, | |
181 | * do nothing | |
182 | */ | |
183 | if (dl_se->dl_runtime == 0) | |
184 | return; | |
185 | ||
186 | WARN_ON(hrtimer_active(&dl_se->inactive_timer)); | |
187 | WARN_ON(dl_se->dl_non_contending); | |
188 | ||
189 | zerolag_time = dl_se->deadline - | |
190 | div64_long((dl_se->runtime * dl_se->dl_period), | |
191 | dl_se->dl_runtime); | |
192 | ||
193 | /* | |
194 | * Using relative times instead of the absolute "0-lag time" | |
195 | * allows to simplify the code | |
196 | */ | |
197 | zerolag_time -= rq_clock(rq); | |
198 | ||
199 | /* | |
200 | * If the "0-lag time" already passed, decrease the active | |
201 | * utilization now, instead of starting a timer | |
202 | */ | |
203 | if (zerolag_time < 0) { | |
204 | if (dl_task(p)) | |
205 | sub_running_bw(dl_se->dl_bw, dl_rq); | |
387e3130 LA |
206 | if (!dl_task(p) || p->state == TASK_DEAD) { |
207 | struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); | |
208 | ||
8fd27231 LA |
209 | if (p->state == TASK_DEAD) |
210 | sub_rq_bw(p->dl.dl_bw, &rq->dl); | |
387e3130 | 211 | raw_spin_lock(&dl_b->lock); |
daec5798 | 212 | __dl_clear(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); |
209a0cbd | 213 | __dl_clear_params(p); |
387e3130 LA |
214 | raw_spin_unlock(&dl_b->lock); |
215 | } | |
209a0cbd LA |
216 | |
217 | return; | |
218 | } | |
219 | ||
220 | dl_se->dl_non_contending = 1; | |
221 | get_task_struct(p); | |
222 | hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL); | |
223 | } | |
224 | ||
8fd27231 | 225 | static void task_contending(struct sched_dl_entity *dl_se, int flags) |
209a0cbd LA |
226 | { |
227 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
228 | ||
229 | /* | |
230 | * If this is a non-deadline task that has been boosted, | |
231 | * do nothing | |
232 | */ | |
233 | if (dl_se->dl_runtime == 0) | |
234 | return; | |
235 | ||
8fd27231 LA |
236 | if (flags & ENQUEUE_MIGRATED) |
237 | add_rq_bw(dl_se->dl_bw, dl_rq); | |
238 | ||
209a0cbd LA |
239 | if (dl_se->dl_non_contending) { |
240 | dl_se->dl_non_contending = 0; | |
241 | /* | |
242 | * If the timer handler is currently running and the | |
243 | * timer cannot be cancelled, inactive_task_timer() | |
244 | * will see that dl_not_contending is not set, and | |
245 | * will not touch the rq's active utilization, | |
246 | * so we are still safe. | |
247 | */ | |
248 | if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) | |
249 | put_task_struct(dl_task_of(dl_se)); | |
250 | } else { | |
251 | /* | |
252 | * Since "dl_non_contending" is not set, the | |
253 | * task's utilization has already been removed from | |
254 | * active utilization (either when the task blocked, | |
255 | * when the "inactive timer" fired). | |
256 | * So, add it back. | |
257 | */ | |
258 | add_running_bw(dl_se->dl_bw, dl_rq); | |
259 | } | |
260 | } | |
261 | ||
aab03e05 DF |
262 | static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq) |
263 | { | |
264 | struct sched_dl_entity *dl_se = &p->dl; | |
265 | ||
266 | return dl_rq->rb_leftmost == &dl_se->rb_node; | |
267 | } | |
268 | ||
332ac17e DF |
269 | void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime) |
270 | { | |
271 | raw_spin_lock_init(&dl_b->dl_runtime_lock); | |
272 | dl_b->dl_period = period; | |
273 | dl_b->dl_runtime = runtime; | |
274 | } | |
275 | ||
332ac17e DF |
276 | void init_dl_bw(struct dl_bw *dl_b) |
277 | { | |
278 | raw_spin_lock_init(&dl_b->lock); | |
279 | raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock); | |
1724813d | 280 | if (global_rt_runtime() == RUNTIME_INF) |
332ac17e DF |
281 | dl_b->bw = -1; |
282 | else | |
1724813d | 283 | dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime()); |
332ac17e DF |
284 | raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock); |
285 | dl_b->total_bw = 0; | |
286 | } | |
287 | ||
07c54f7a | 288 | void init_dl_rq(struct dl_rq *dl_rq) |
aab03e05 DF |
289 | { |
290 | dl_rq->rb_root = RB_ROOT; | |
1baca4ce JL |
291 | |
292 | #ifdef CONFIG_SMP | |
293 | /* zero means no -deadline tasks */ | |
294 | dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0; | |
295 | ||
296 | dl_rq->dl_nr_migratory = 0; | |
297 | dl_rq->overloaded = 0; | |
298 | dl_rq->pushable_dl_tasks_root = RB_ROOT; | |
332ac17e DF |
299 | #else |
300 | init_dl_bw(&dl_rq->dl_bw); | |
1baca4ce | 301 | #endif |
e36d8677 LA |
302 | |
303 | dl_rq->running_bw = 0; | |
8fd27231 | 304 | dl_rq->this_bw = 0; |
4da3abce | 305 | init_dl_rq_bw_ratio(dl_rq); |
1baca4ce JL |
306 | } |
307 | ||
308 | #ifdef CONFIG_SMP | |
309 | ||
310 | static inline int dl_overloaded(struct rq *rq) | |
311 | { | |
312 | return atomic_read(&rq->rd->dlo_count); | |
313 | } | |
314 | ||
315 | static inline void dl_set_overload(struct rq *rq) | |
316 | { | |
317 | if (!rq->online) | |
318 | return; | |
319 | ||
320 | cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask); | |
321 | /* | |
322 | * Must be visible before the overload count is | |
323 | * set (as in sched_rt.c). | |
324 | * | |
325 | * Matched by the barrier in pull_dl_task(). | |
326 | */ | |
327 | smp_wmb(); | |
328 | atomic_inc(&rq->rd->dlo_count); | |
329 | } | |
330 | ||
331 | static inline void dl_clear_overload(struct rq *rq) | |
332 | { | |
333 | if (!rq->online) | |
334 | return; | |
335 | ||
336 | atomic_dec(&rq->rd->dlo_count); | |
337 | cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask); | |
338 | } | |
339 | ||
340 | static void update_dl_migration(struct dl_rq *dl_rq) | |
341 | { | |
995b9ea4 | 342 | if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) { |
1baca4ce JL |
343 | if (!dl_rq->overloaded) { |
344 | dl_set_overload(rq_of_dl_rq(dl_rq)); | |
345 | dl_rq->overloaded = 1; | |
346 | } | |
347 | } else if (dl_rq->overloaded) { | |
348 | dl_clear_overload(rq_of_dl_rq(dl_rq)); | |
349 | dl_rq->overloaded = 0; | |
350 | } | |
351 | } | |
352 | ||
353 | static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
354 | { | |
355 | struct task_struct *p = dl_task_of(dl_se); | |
1baca4ce | 356 | |
4b53a341 | 357 | if (p->nr_cpus_allowed > 1) |
1baca4ce JL |
358 | dl_rq->dl_nr_migratory++; |
359 | ||
360 | update_dl_migration(dl_rq); | |
361 | } | |
362 | ||
363 | static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
364 | { | |
365 | struct task_struct *p = dl_task_of(dl_se); | |
1baca4ce | 366 | |
4b53a341 | 367 | if (p->nr_cpus_allowed > 1) |
1baca4ce JL |
368 | dl_rq->dl_nr_migratory--; |
369 | ||
370 | update_dl_migration(dl_rq); | |
371 | } | |
372 | ||
373 | /* | |
374 | * The list of pushable -deadline task is not a plist, like in | |
375 | * sched_rt.c, it is an rb-tree with tasks ordered by deadline. | |
376 | */ | |
377 | static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) | |
378 | { | |
379 | struct dl_rq *dl_rq = &rq->dl; | |
380 | struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node; | |
381 | struct rb_node *parent = NULL; | |
382 | struct task_struct *entry; | |
383 | int leftmost = 1; | |
384 | ||
385 | BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks)); | |
386 | ||
387 | while (*link) { | |
388 | parent = *link; | |
389 | entry = rb_entry(parent, struct task_struct, | |
390 | pushable_dl_tasks); | |
391 | if (dl_entity_preempt(&p->dl, &entry->dl)) | |
392 | link = &parent->rb_left; | |
393 | else { | |
394 | link = &parent->rb_right; | |
395 | leftmost = 0; | |
396 | } | |
397 | } | |
398 | ||
7d92de3a | 399 | if (leftmost) { |
1baca4ce | 400 | dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks; |
7d92de3a WL |
401 | dl_rq->earliest_dl.next = p->dl.deadline; |
402 | } | |
1baca4ce JL |
403 | |
404 | rb_link_node(&p->pushable_dl_tasks, parent, link); | |
405 | rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root); | |
aab03e05 DF |
406 | } |
407 | ||
1baca4ce JL |
408 | static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) |
409 | { | |
410 | struct dl_rq *dl_rq = &rq->dl; | |
411 | ||
412 | if (RB_EMPTY_NODE(&p->pushable_dl_tasks)) | |
413 | return; | |
414 | ||
415 | if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) { | |
416 | struct rb_node *next_node; | |
417 | ||
418 | next_node = rb_next(&p->pushable_dl_tasks); | |
419 | dl_rq->pushable_dl_tasks_leftmost = next_node; | |
7d92de3a WL |
420 | if (next_node) { |
421 | dl_rq->earliest_dl.next = rb_entry(next_node, | |
422 | struct task_struct, pushable_dl_tasks)->dl.deadline; | |
423 | } | |
1baca4ce JL |
424 | } |
425 | ||
426 | rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root); | |
427 | RB_CLEAR_NODE(&p->pushable_dl_tasks); | |
428 | } | |
429 | ||
430 | static inline int has_pushable_dl_tasks(struct rq *rq) | |
431 | { | |
432 | return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root); | |
433 | } | |
434 | ||
435 | static int push_dl_task(struct rq *rq); | |
436 | ||
dc877341 PZ |
437 | static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev) |
438 | { | |
439 | return dl_task(prev); | |
440 | } | |
441 | ||
9916e214 PZ |
442 | static DEFINE_PER_CPU(struct callback_head, dl_push_head); |
443 | static DEFINE_PER_CPU(struct callback_head, dl_pull_head); | |
e3fca9e7 PZ |
444 | |
445 | static void push_dl_tasks(struct rq *); | |
9916e214 | 446 | static void pull_dl_task(struct rq *); |
e3fca9e7 PZ |
447 | |
448 | static inline void queue_push_tasks(struct rq *rq) | |
dc877341 | 449 | { |
e3fca9e7 PZ |
450 | if (!has_pushable_dl_tasks(rq)) |
451 | return; | |
452 | ||
9916e214 PZ |
453 | queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks); |
454 | } | |
455 | ||
456 | static inline void queue_pull_task(struct rq *rq) | |
457 | { | |
458 | queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task); | |
dc877341 PZ |
459 | } |
460 | ||
fa9c9d10 WL |
461 | static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq); |
462 | ||
a649f237 | 463 | static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p) |
fa9c9d10 WL |
464 | { |
465 | struct rq *later_rq = NULL; | |
fa9c9d10 WL |
466 | |
467 | later_rq = find_lock_later_rq(p, rq); | |
fa9c9d10 WL |
468 | if (!later_rq) { |
469 | int cpu; | |
470 | ||
471 | /* | |
472 | * If we cannot preempt any rq, fall back to pick any | |
473 | * online cpu. | |
474 | */ | |
0c98d344 | 475 | cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed); |
fa9c9d10 WL |
476 | if (cpu >= nr_cpu_ids) { |
477 | /* | |
478 | * Fail to find any suitable cpu. | |
479 | * The task will never come back! | |
480 | */ | |
481 | BUG_ON(dl_bandwidth_enabled()); | |
482 | ||
483 | /* | |
484 | * If admission control is disabled we | |
485 | * try a little harder to let the task | |
486 | * run. | |
487 | */ | |
488 | cpu = cpumask_any(cpu_active_mask); | |
489 | } | |
490 | later_rq = cpu_rq(cpu); | |
491 | double_lock_balance(rq, later_rq); | |
492 | } | |
493 | ||
fa9c9d10 | 494 | set_task_cpu(p, later_rq->cpu); |
a649f237 PZ |
495 | double_unlock_balance(later_rq, rq); |
496 | ||
497 | return later_rq; | |
fa9c9d10 WL |
498 | } |
499 | ||
1baca4ce JL |
500 | #else |
501 | ||
502 | static inline | |
503 | void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p) | |
504 | { | |
505 | } | |
506 | ||
507 | static inline | |
508 | void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p) | |
509 | { | |
510 | } | |
511 | ||
512 | static inline | |
513 | void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
514 | { | |
515 | } | |
516 | ||
517 | static inline | |
518 | void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
519 | { | |
520 | } | |
521 | ||
dc877341 PZ |
522 | static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev) |
523 | { | |
524 | return false; | |
525 | } | |
526 | ||
0ea60c20 | 527 | static inline void pull_dl_task(struct rq *rq) |
dc877341 | 528 | { |
dc877341 PZ |
529 | } |
530 | ||
e3fca9e7 | 531 | static inline void queue_push_tasks(struct rq *rq) |
dc877341 | 532 | { |
dc877341 PZ |
533 | } |
534 | ||
9916e214 | 535 | static inline void queue_pull_task(struct rq *rq) |
dc877341 PZ |
536 | { |
537 | } | |
1baca4ce JL |
538 | #endif /* CONFIG_SMP */ |
539 | ||
aab03e05 DF |
540 | static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags); |
541 | static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags); | |
542 | static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, | |
543 | int flags); | |
544 | ||
545 | /* | |
546 | * We are being explicitly informed that a new instance is starting, | |
547 | * and this means that: | |
548 | * - the absolute deadline of the entity has to be placed at | |
549 | * current time + relative deadline; | |
550 | * - the runtime of the entity has to be set to the maximum value. | |
551 | * | |
552 | * The capability of specifying such event is useful whenever a -deadline | |
553 | * entity wants to (try to!) synchronize its behaviour with the scheduler's | |
554 | * one, and to (try to!) reconcile itself with its own scheduling | |
555 | * parameters. | |
556 | */ | |
98b0a857 | 557 | static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se) |
aab03e05 DF |
558 | { |
559 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
560 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
561 | ||
98b0a857 | 562 | WARN_ON(dl_se->dl_boosted); |
72f9f3fd LA |
563 | WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline)); |
564 | ||
565 | /* | |
566 | * We are racing with the deadline timer. So, do nothing because | |
567 | * the deadline timer handler will take care of properly recharging | |
568 | * the runtime and postponing the deadline | |
569 | */ | |
570 | if (dl_se->dl_throttled) | |
571 | return; | |
aab03e05 DF |
572 | |
573 | /* | |
574 | * We use the regular wall clock time to set deadlines in the | |
575 | * future; in fact, we must consider execution overheads (time | |
576 | * spent on hardirq context, etc.). | |
577 | */ | |
98b0a857 JL |
578 | dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline; |
579 | dl_se->runtime = dl_se->dl_runtime; | |
aab03e05 DF |
580 | } |
581 | ||
582 | /* | |
583 | * Pure Earliest Deadline First (EDF) scheduling does not deal with the | |
584 | * possibility of a entity lasting more than what it declared, and thus | |
585 | * exhausting its runtime. | |
586 | * | |
587 | * Here we are interested in making runtime overrun possible, but we do | |
588 | * not want a entity which is misbehaving to affect the scheduling of all | |
589 | * other entities. | |
590 | * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS) | |
591 | * is used, in order to confine each entity within its own bandwidth. | |
592 | * | |
593 | * This function deals exactly with that, and ensures that when the runtime | |
594 | * of a entity is replenished, its deadline is also postponed. That ensures | |
595 | * the overrunning entity can't interfere with other entity in the system and | |
596 | * can't make them miss their deadlines. Reasons why this kind of overruns | |
597 | * could happen are, typically, a entity voluntarily trying to overcome its | |
1b09d29b | 598 | * runtime, or it just underestimated it during sched_setattr(). |
aab03e05 | 599 | */ |
2d3d891d DF |
600 | static void replenish_dl_entity(struct sched_dl_entity *dl_se, |
601 | struct sched_dl_entity *pi_se) | |
aab03e05 DF |
602 | { |
603 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
604 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
605 | ||
2d3d891d DF |
606 | BUG_ON(pi_se->dl_runtime <= 0); |
607 | ||
608 | /* | |
609 | * This could be the case for a !-dl task that is boosted. | |
610 | * Just go with full inherited parameters. | |
611 | */ | |
612 | if (dl_se->dl_deadline == 0) { | |
613 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; | |
614 | dl_se->runtime = pi_se->dl_runtime; | |
615 | } | |
616 | ||
48be3a67 PZ |
617 | if (dl_se->dl_yielded && dl_se->runtime > 0) |
618 | dl_se->runtime = 0; | |
619 | ||
aab03e05 DF |
620 | /* |
621 | * We keep moving the deadline away until we get some | |
622 | * available runtime for the entity. This ensures correct | |
623 | * handling of situations where the runtime overrun is | |
624 | * arbitrary large. | |
625 | */ | |
626 | while (dl_se->runtime <= 0) { | |
2d3d891d DF |
627 | dl_se->deadline += pi_se->dl_period; |
628 | dl_se->runtime += pi_se->dl_runtime; | |
aab03e05 DF |
629 | } |
630 | ||
631 | /* | |
632 | * At this point, the deadline really should be "in | |
633 | * the future" with respect to rq->clock. If it's | |
634 | * not, we are, for some reason, lagging too much! | |
635 | * Anyway, after having warn userspace abut that, | |
636 | * we still try to keep the things running by | |
637 | * resetting the deadline and the budget of the | |
638 | * entity. | |
639 | */ | |
640 | if (dl_time_before(dl_se->deadline, rq_clock(rq))) { | |
c219b7dd | 641 | printk_deferred_once("sched: DL replenish lagged too much\n"); |
2d3d891d DF |
642 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; |
643 | dl_se->runtime = pi_se->dl_runtime; | |
aab03e05 | 644 | } |
1019a359 PZ |
645 | |
646 | if (dl_se->dl_yielded) | |
647 | dl_se->dl_yielded = 0; | |
648 | if (dl_se->dl_throttled) | |
649 | dl_se->dl_throttled = 0; | |
aab03e05 DF |
650 | } |
651 | ||
652 | /* | |
653 | * Here we check if --at time t-- an entity (which is probably being | |
654 | * [re]activated or, in general, enqueued) can use its remaining runtime | |
655 | * and its current deadline _without_ exceeding the bandwidth it is | |
656 | * assigned (function returns true if it can't). We are in fact applying | |
657 | * one of the CBS rules: when a task wakes up, if the residual runtime | |
658 | * over residual deadline fits within the allocated bandwidth, then we | |
659 | * can keep the current (absolute) deadline and residual budget without | |
660 | * disrupting the schedulability of the system. Otherwise, we should | |
661 | * refill the runtime and set the deadline a period in the future, | |
662 | * because keeping the current (absolute) deadline of the task would | |
712e5e34 DF |
663 | * result in breaking guarantees promised to other tasks (refer to |
664 | * Documentation/scheduler/sched-deadline.txt for more informations). | |
aab03e05 DF |
665 | * |
666 | * This function returns true if: | |
667 | * | |
2317d5f1 | 668 | * runtime / (deadline - t) > dl_runtime / dl_deadline , |
aab03e05 DF |
669 | * |
670 | * IOW we can't recycle current parameters. | |
755378a4 | 671 | * |
2317d5f1 | 672 | * Notice that the bandwidth check is done against the deadline. For |
755378a4 | 673 | * task with deadline equal to period this is the same of using |
2317d5f1 | 674 | * dl_period instead of dl_deadline in the equation above. |
aab03e05 | 675 | */ |
2d3d891d DF |
676 | static bool dl_entity_overflow(struct sched_dl_entity *dl_se, |
677 | struct sched_dl_entity *pi_se, u64 t) | |
aab03e05 DF |
678 | { |
679 | u64 left, right; | |
680 | ||
681 | /* | |
682 | * left and right are the two sides of the equation above, | |
683 | * after a bit of shuffling to use multiplications instead | |
684 | * of divisions. | |
685 | * | |
686 | * Note that none of the time values involved in the two | |
687 | * multiplications are absolute: dl_deadline and dl_runtime | |
688 | * are the relative deadline and the maximum runtime of each | |
689 | * instance, runtime is the runtime left for the last instance | |
690 | * and (deadline - t), since t is rq->clock, is the time left | |
691 | * to the (absolute) deadline. Even if overflowing the u64 type | |
692 | * is very unlikely to occur in both cases, here we scale down | |
693 | * as we want to avoid that risk at all. Scaling down by 10 | |
694 | * means that we reduce granularity to 1us. We are fine with it, | |
695 | * since this is only a true/false check and, anyway, thinking | |
696 | * of anything below microseconds resolution is actually fiction | |
697 | * (but still we want to give the user that illusion >;). | |
698 | */ | |
2317d5f1 | 699 | left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE); |
332ac17e DF |
700 | right = ((dl_se->deadline - t) >> DL_SCALE) * |
701 | (pi_se->dl_runtime >> DL_SCALE); | |
aab03e05 DF |
702 | |
703 | return dl_time_before(right, left); | |
704 | } | |
705 | ||
706 | /* | |
3effcb42 DBO |
707 | * Revised wakeup rule [1]: For self-suspending tasks, rather then |
708 | * re-initializing task's runtime and deadline, the revised wakeup | |
709 | * rule adjusts the task's runtime to avoid the task to overrun its | |
710 | * density. | |
aab03e05 | 711 | * |
3effcb42 DBO |
712 | * Reasoning: a task may overrun the density if: |
713 | * runtime / (deadline - t) > dl_runtime / dl_deadline | |
714 | * | |
715 | * Therefore, runtime can be adjusted to: | |
716 | * runtime = (dl_runtime / dl_deadline) * (deadline - t) | |
717 | * | |
718 | * In such way that runtime will be equal to the maximum density | |
719 | * the task can use without breaking any rule. | |
720 | * | |
721 | * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant | |
722 | * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24. | |
723 | */ | |
724 | static void | |
725 | update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq) | |
726 | { | |
727 | u64 laxity = dl_se->deadline - rq_clock(rq); | |
728 | ||
729 | /* | |
730 | * If the task has deadline < period, and the deadline is in the past, | |
731 | * it should already be throttled before this check. | |
732 | * | |
733 | * See update_dl_entity() comments for further details. | |
734 | */ | |
735 | WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq))); | |
736 | ||
737 | dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT; | |
738 | } | |
739 | ||
740 | /* | |
741 | * Regarding the deadline, a task with implicit deadline has a relative | |
742 | * deadline == relative period. A task with constrained deadline has a | |
743 | * relative deadline <= relative period. | |
744 | * | |
745 | * We support constrained deadline tasks. However, there are some restrictions | |
746 | * applied only for tasks which do not have an implicit deadline. See | |
747 | * update_dl_entity() to know more about such restrictions. | |
748 | * | |
749 | * The dl_is_implicit() returns true if the task has an implicit deadline. | |
750 | */ | |
751 | static inline bool dl_is_implicit(struct sched_dl_entity *dl_se) | |
752 | { | |
753 | return dl_se->dl_deadline == dl_se->dl_period; | |
754 | } | |
755 | ||
756 | /* | |
757 | * When a deadline entity is placed in the runqueue, its runtime and deadline | |
758 | * might need to be updated. This is done by a CBS wake up rule. There are two | |
759 | * different rules: 1) the original CBS; and 2) the Revisited CBS. | |
760 | * | |
761 | * When the task is starting a new period, the Original CBS is used. In this | |
762 | * case, the runtime is replenished and a new absolute deadline is set. | |
763 | * | |
764 | * When a task is queued before the begin of the next period, using the | |
765 | * remaining runtime and deadline could make the entity to overflow, see | |
766 | * dl_entity_overflow() to find more about runtime overflow. When such case | |
767 | * is detected, the runtime and deadline need to be updated. | |
768 | * | |
769 | * If the task has an implicit deadline, i.e., deadline == period, the Original | |
770 | * CBS is applied. the runtime is replenished and a new absolute deadline is | |
771 | * set, as in the previous cases. | |
772 | * | |
773 | * However, the Original CBS does not work properly for tasks with | |
774 | * deadline < period, which are said to have a constrained deadline. By | |
775 | * applying the Original CBS, a constrained deadline task would be able to run | |
776 | * runtime/deadline in a period. With deadline < period, the task would | |
777 | * overrun the runtime/period allowed bandwidth, breaking the admission test. | |
778 | * | |
779 | * In order to prevent this misbehave, the Revisited CBS is used for | |
780 | * constrained deadline tasks when a runtime overflow is detected. In the | |
781 | * Revisited CBS, rather than replenishing & setting a new absolute deadline, | |
782 | * the remaining runtime of the task is reduced to avoid runtime overflow. | |
783 | * Please refer to the comments update_dl_revised_wakeup() function to find | |
784 | * more about the Revised CBS rule. | |
aab03e05 | 785 | */ |
2d3d891d DF |
786 | static void update_dl_entity(struct sched_dl_entity *dl_se, |
787 | struct sched_dl_entity *pi_se) | |
aab03e05 DF |
788 | { |
789 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
790 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
791 | ||
aab03e05 | 792 | if (dl_time_before(dl_se->deadline, rq_clock(rq)) || |
2d3d891d | 793 | dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) { |
3effcb42 DBO |
794 | |
795 | if (unlikely(!dl_is_implicit(dl_se) && | |
796 | !dl_time_before(dl_se->deadline, rq_clock(rq)) && | |
797 | !dl_se->dl_boosted)){ | |
798 | update_dl_revised_wakeup(dl_se, rq); | |
799 | return; | |
800 | } | |
801 | ||
2d3d891d DF |
802 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; |
803 | dl_se->runtime = pi_se->dl_runtime; | |
aab03e05 DF |
804 | } |
805 | } | |
806 | ||
5ac69d37 DBO |
807 | static inline u64 dl_next_period(struct sched_dl_entity *dl_se) |
808 | { | |
809 | return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period; | |
810 | } | |
811 | ||
aab03e05 DF |
812 | /* |
813 | * If the entity depleted all its runtime, and if we want it to sleep | |
814 | * while waiting for some new execution time to become available, we | |
5ac69d37 | 815 | * set the bandwidth replenishment timer to the replenishment instant |
aab03e05 DF |
816 | * and try to activate it. |
817 | * | |
818 | * Notice that it is important for the caller to know if the timer | |
819 | * actually started or not (i.e., the replenishment instant is in | |
820 | * the future or in the past). | |
821 | */ | |
a649f237 | 822 | static int start_dl_timer(struct task_struct *p) |
aab03e05 | 823 | { |
a649f237 PZ |
824 | struct sched_dl_entity *dl_se = &p->dl; |
825 | struct hrtimer *timer = &dl_se->dl_timer; | |
826 | struct rq *rq = task_rq(p); | |
aab03e05 | 827 | ktime_t now, act; |
aab03e05 DF |
828 | s64 delta; |
829 | ||
a649f237 PZ |
830 | lockdep_assert_held(&rq->lock); |
831 | ||
aab03e05 DF |
832 | /* |
833 | * We want the timer to fire at the deadline, but considering | |
834 | * that it is actually coming from rq->clock and not from | |
835 | * hrtimer's time base reading. | |
836 | */ | |
5ac69d37 | 837 | act = ns_to_ktime(dl_next_period(dl_se)); |
a649f237 | 838 | now = hrtimer_cb_get_time(timer); |
aab03e05 DF |
839 | delta = ktime_to_ns(now) - rq_clock(rq); |
840 | act = ktime_add_ns(act, delta); | |
841 | ||
842 | /* | |
843 | * If the expiry time already passed, e.g., because the value | |
844 | * chosen as the deadline is too small, don't even try to | |
845 | * start the timer in the past! | |
846 | */ | |
847 | if (ktime_us_delta(act, now) < 0) | |
848 | return 0; | |
849 | ||
a649f237 PZ |
850 | /* |
851 | * !enqueued will guarantee another callback; even if one is already in | |
852 | * progress. This ensures a balanced {get,put}_task_struct(). | |
853 | * | |
854 | * The race against __run_timer() clearing the enqueued state is | |
855 | * harmless because we're holding task_rq()->lock, therefore the timer | |
856 | * expiring after we've done the check will wait on its task_rq_lock() | |
857 | * and observe our state. | |
858 | */ | |
859 | if (!hrtimer_is_queued(timer)) { | |
860 | get_task_struct(p); | |
861 | hrtimer_start(timer, act, HRTIMER_MODE_ABS); | |
862 | } | |
aab03e05 | 863 | |
cc9684d3 | 864 | return 1; |
aab03e05 DF |
865 | } |
866 | ||
867 | /* | |
868 | * This is the bandwidth enforcement timer callback. If here, we know | |
869 | * a task is not on its dl_rq, since the fact that the timer was running | |
870 | * means the task is throttled and needs a runtime replenishment. | |
871 | * | |
872 | * However, what we actually do depends on the fact the task is active, | |
873 | * (it is on its rq) or has been removed from there by a call to | |
874 | * dequeue_task_dl(). In the former case we must issue the runtime | |
875 | * replenishment and add the task back to the dl_rq; in the latter, we just | |
876 | * do nothing but clearing dl_throttled, so that runtime and deadline | |
877 | * updating (and the queueing back to dl_rq) will be done by the | |
878 | * next call to enqueue_task_dl(). | |
879 | */ | |
880 | static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) | |
881 | { | |
882 | struct sched_dl_entity *dl_se = container_of(timer, | |
883 | struct sched_dl_entity, | |
884 | dl_timer); | |
885 | struct task_struct *p = dl_task_of(dl_se); | |
eb580751 | 886 | struct rq_flags rf; |
0f397f2c | 887 | struct rq *rq; |
3960c8c0 | 888 | |
eb580751 | 889 | rq = task_rq_lock(p, &rf); |
0f397f2c | 890 | |
aab03e05 | 891 | /* |
a649f237 | 892 | * The task might have changed its scheduling policy to something |
9846d50d | 893 | * different than SCHED_DEADLINE (through switched_from_dl()). |
a649f237 | 894 | */ |
209a0cbd | 895 | if (!dl_task(p)) |
a649f237 | 896 | goto unlock; |
a649f237 | 897 | |
a649f237 PZ |
898 | /* |
899 | * The task might have been boosted by someone else and might be in the | |
900 | * boosting/deboosting path, its not throttled. | |
901 | */ | |
902 | if (dl_se->dl_boosted) | |
903 | goto unlock; | |
a79ec89f | 904 | |
fa9c9d10 | 905 | /* |
a649f237 PZ |
906 | * Spurious timer due to start_dl_timer() race; or we already received |
907 | * a replenishment from rt_mutex_setprio(). | |
fa9c9d10 | 908 | */ |
a649f237 | 909 | if (!dl_se->dl_throttled) |
fa9c9d10 | 910 | goto unlock; |
a649f237 PZ |
911 | |
912 | sched_clock_tick(); | |
913 | update_rq_clock(rq); | |
fa9c9d10 | 914 | |
a79ec89f KT |
915 | /* |
916 | * If the throttle happened during sched-out; like: | |
917 | * | |
918 | * schedule() | |
919 | * deactivate_task() | |
920 | * dequeue_task_dl() | |
921 | * update_curr_dl() | |
922 | * start_dl_timer() | |
923 | * __dequeue_task_dl() | |
924 | * prev->on_rq = 0; | |
925 | * | |
926 | * We can be both throttled and !queued. Replenish the counter | |
927 | * but do not enqueue -- wait for our wakeup to do that. | |
928 | */ | |
929 | if (!task_on_rq_queued(p)) { | |
930 | replenish_dl_entity(dl_se, dl_se); | |
931 | goto unlock; | |
932 | } | |
933 | ||
1baca4ce | 934 | #ifdef CONFIG_SMP |
c0c8c9fa | 935 | if (unlikely(!rq->online)) { |
61c7aca6 WL |
936 | /* |
937 | * If the runqueue is no longer available, migrate the | |
938 | * task elsewhere. This necessarily changes rq. | |
939 | */ | |
c0c8c9fa | 940 | lockdep_unpin_lock(&rq->lock, rf.cookie); |
a649f237 | 941 | rq = dl_task_offline_migration(rq, p); |
c0c8c9fa | 942 | rf.cookie = lockdep_pin_lock(&rq->lock); |
dcc3b5ff | 943 | update_rq_clock(rq); |
61c7aca6 WL |
944 | |
945 | /* | |
946 | * Now that the task has been migrated to the new RQ and we | |
947 | * have that locked, proceed as normal and enqueue the task | |
948 | * there. | |
949 | */ | |
c0c8c9fa | 950 | } |
61c7aca6 | 951 | #endif |
a649f237 | 952 | |
61c7aca6 WL |
953 | enqueue_task_dl(rq, p, ENQUEUE_REPLENISH); |
954 | if (dl_task(rq->curr)) | |
955 | check_preempt_curr_dl(rq, p, 0); | |
956 | else | |
957 | resched_curr(rq); | |
a649f237 | 958 | |
61c7aca6 | 959 | #ifdef CONFIG_SMP |
a649f237 PZ |
960 | /* |
961 | * Queueing this task back might have overloaded rq, check if we need | |
962 | * to kick someone away. | |
1019a359 | 963 | */ |
0aaafaab PZ |
964 | if (has_pushable_dl_tasks(rq)) { |
965 | /* | |
966 | * Nothing relies on rq->lock after this, so its safe to drop | |
967 | * rq->lock. | |
968 | */ | |
d8ac8971 | 969 | rq_unpin_lock(rq, &rf); |
1019a359 | 970 | push_dl_task(rq); |
d8ac8971 | 971 | rq_repin_lock(rq, &rf); |
0aaafaab | 972 | } |
1baca4ce | 973 | #endif |
a649f237 | 974 | |
aab03e05 | 975 | unlock: |
eb580751 | 976 | task_rq_unlock(rq, p, &rf); |
aab03e05 | 977 | |
a649f237 PZ |
978 | /* |
979 | * This can free the task_struct, including this hrtimer, do not touch | |
980 | * anything related to that after this. | |
981 | */ | |
982 | put_task_struct(p); | |
983 | ||
aab03e05 DF |
984 | return HRTIMER_NORESTART; |
985 | } | |
986 | ||
987 | void init_dl_task_timer(struct sched_dl_entity *dl_se) | |
988 | { | |
989 | struct hrtimer *timer = &dl_se->dl_timer; | |
990 | ||
aab03e05 DF |
991 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
992 | timer->function = dl_task_timer; | |
993 | } | |
994 | ||
df8eac8c DBO |
995 | /* |
996 | * During the activation, CBS checks if it can reuse the current task's | |
997 | * runtime and period. If the deadline of the task is in the past, CBS | |
998 | * cannot use the runtime, and so it replenishes the task. This rule | |
999 | * works fine for implicit deadline tasks (deadline == period), and the | |
1000 | * CBS was designed for implicit deadline tasks. However, a task with | |
1001 | * constrained deadline (deadine < period) might be awakened after the | |
1002 | * deadline, but before the next period. In this case, replenishing the | |
1003 | * task would allow it to run for runtime / deadline. As in this case | |
1004 | * deadline < period, CBS enables a task to run for more than the | |
1005 | * runtime / period. In a very loaded system, this can cause a domino | |
1006 | * effect, making other tasks miss their deadlines. | |
1007 | * | |
1008 | * To avoid this problem, in the activation of a constrained deadline | |
1009 | * task after the deadline but before the next period, throttle the | |
1010 | * task and set the replenishing timer to the begin of the next period, | |
1011 | * unless it is boosted. | |
1012 | */ | |
1013 | static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se) | |
1014 | { | |
1015 | struct task_struct *p = dl_task_of(dl_se); | |
1016 | struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se)); | |
1017 | ||
1018 | if (dl_time_before(dl_se->deadline, rq_clock(rq)) && | |
1019 | dl_time_before(rq_clock(rq), dl_next_period(dl_se))) { | |
1020 | if (unlikely(dl_se->dl_boosted || !start_dl_timer(p))) | |
1021 | return; | |
1022 | dl_se->dl_throttled = 1; | |
ae83b56a XP |
1023 | if (dl_se->runtime > 0) |
1024 | dl_se->runtime = 0; | |
df8eac8c DBO |
1025 | } |
1026 | } | |
1027 | ||
aab03e05 | 1028 | static |
6fab5410 | 1029 | int dl_runtime_exceeded(struct sched_dl_entity *dl_se) |
aab03e05 | 1030 | { |
269ad801 | 1031 | return (dl_se->runtime <= 0); |
aab03e05 DF |
1032 | } |
1033 | ||
faa59937 JL |
1034 | extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq); |
1035 | ||
c52f14d3 LA |
1036 | /* |
1037 | * This function implements the GRUB accounting rule: | |
1038 | * according to the GRUB reclaiming algorithm, the runtime is | |
daec5798 LA |
1039 | * not decreased as "dq = -dt", but as |
1040 | * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt", | |
1041 | * where u is the utilization of the task, Umax is the maximum reclaimable | |
1042 | * utilization, Uinact is the (per-runqueue) inactive utilization, computed | |
1043 | * as the difference between the "total runqueue utilization" and the | |
1044 | * runqueue active utilization, and Uextra is the (per runqueue) extra | |
1045 | * reclaimable utilization. | |
9f0d1a50 | 1046 | * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations |
daec5798 LA |
1047 | * multiplied by 2^BW_SHIFT, the result has to be shifted right by |
1048 | * BW_SHIFT. | |
1049 | * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT, | |
1050 | * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT. | |
1051 | * Since delta is a 64 bit variable, to have an overflow its value | |
1052 | * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds. | |
1053 | * So, overflow is not an issue here. | |
c52f14d3 | 1054 | */ |
9f0d1a50 | 1055 | u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se) |
c52f14d3 | 1056 | { |
9f0d1a50 LA |
1057 | u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */ |
1058 | u64 u_act; | |
daec5798 | 1059 | u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT; |
c52f14d3 | 1060 | |
9f0d1a50 | 1061 | /* |
daec5798 LA |
1062 | * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)}, |
1063 | * we compare u_inact + rq->dl.extra_bw with | |
1064 | * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because | |
1065 | * u_inact + rq->dl.extra_bw can be larger than | |
1066 | * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative | |
1067 | * leading to wrong results) | |
9f0d1a50 | 1068 | */ |
daec5798 LA |
1069 | if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min) |
1070 | u_act = u_act_min; | |
9f0d1a50 | 1071 | else |
daec5798 | 1072 | u_act = BW_UNIT - u_inact - rq->dl.extra_bw; |
9f0d1a50 LA |
1073 | |
1074 | return (delta * u_act) >> BW_SHIFT; | |
c52f14d3 LA |
1075 | } |
1076 | ||
aab03e05 DF |
1077 | /* |
1078 | * Update the current task's runtime statistics (provided it is still | |
1079 | * a -deadline task and has not been removed from the dl_rq). | |
1080 | */ | |
1081 | static void update_curr_dl(struct rq *rq) | |
1082 | { | |
1083 | struct task_struct *curr = rq->curr; | |
1084 | struct sched_dl_entity *dl_se = &curr->dl; | |
1085 | u64 delta_exec; | |
1086 | ||
1087 | if (!dl_task(curr) || !on_dl_rq(dl_se)) | |
1088 | return; | |
1089 | ||
1090 | /* | |
1091 | * Consumed budget is computed considering the time as | |
1092 | * observed by schedulable tasks (excluding time spent | |
1093 | * in hardirq context, etc.). Deadlines are instead | |
1094 | * computed using hard walltime. This seems to be the more | |
1095 | * natural solution, but the full ramifications of this | |
1096 | * approach need further study. | |
1097 | */ | |
1098 | delta_exec = rq_clock_task(rq) - curr->se.exec_start; | |
48be3a67 PZ |
1099 | if (unlikely((s64)delta_exec <= 0)) { |
1100 | if (unlikely(dl_se->dl_yielded)) | |
1101 | goto throttle; | |
734ff2a7 | 1102 | return; |
48be3a67 | 1103 | } |
aab03e05 | 1104 | |
58919e83 | 1105 | /* kick cpufreq (see the comment in kernel/sched/sched.h). */ |
12bde33d | 1106 | cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_DL); |
594dd290 | 1107 | |
aab03e05 DF |
1108 | schedstat_set(curr->se.statistics.exec_max, |
1109 | max(curr->se.statistics.exec_max, delta_exec)); | |
1110 | ||
1111 | curr->se.sum_exec_runtime += delta_exec; | |
1112 | account_group_exec_runtime(curr, delta_exec); | |
1113 | ||
1114 | curr->se.exec_start = rq_clock_task(rq); | |
1115 | cpuacct_charge(curr, delta_exec); | |
1116 | ||
239be4a9 DF |
1117 | sched_rt_avg_update(rq, delta_exec); |
1118 | ||
2d4283e9 | 1119 | if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) |
9f0d1a50 | 1120 | delta_exec = grub_reclaim(delta_exec, rq, &curr->dl); |
48be3a67 PZ |
1121 | dl_se->runtime -= delta_exec; |
1122 | ||
1123 | throttle: | |
1124 | if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) { | |
1019a359 | 1125 | dl_se->dl_throttled = 1; |
aab03e05 | 1126 | __dequeue_task_dl(rq, curr, 0); |
a649f237 | 1127 | if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr))) |
aab03e05 DF |
1128 | enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH); |
1129 | ||
1130 | if (!is_leftmost(curr, &rq->dl)) | |
8875125e | 1131 | resched_curr(rq); |
aab03e05 | 1132 | } |
1724813d PZ |
1133 | |
1134 | /* | |
1135 | * Because -- for now -- we share the rt bandwidth, we need to | |
1136 | * account our runtime there too, otherwise actual rt tasks | |
1137 | * would be able to exceed the shared quota. | |
1138 | * | |
1139 | * Account to the root rt group for now. | |
1140 | * | |
1141 | * The solution we're working towards is having the RT groups scheduled | |
1142 | * using deadline servers -- however there's a few nasties to figure | |
1143 | * out before that can happen. | |
1144 | */ | |
1145 | if (rt_bandwidth_enabled()) { | |
1146 | struct rt_rq *rt_rq = &rq->rt; | |
1147 | ||
1148 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
1724813d PZ |
1149 | /* |
1150 | * We'll let actual RT tasks worry about the overflow here, we | |
faa59937 JL |
1151 | * have our own CBS to keep us inline; only account when RT |
1152 | * bandwidth is relevant. | |
1724813d | 1153 | */ |
faa59937 JL |
1154 | if (sched_rt_bandwidth_account(rt_rq)) |
1155 | rt_rq->rt_time += delta_exec; | |
1724813d PZ |
1156 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
1157 | } | |
aab03e05 DF |
1158 | } |
1159 | ||
209a0cbd LA |
1160 | static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) |
1161 | { | |
1162 | struct sched_dl_entity *dl_se = container_of(timer, | |
1163 | struct sched_dl_entity, | |
1164 | inactive_timer); | |
1165 | struct task_struct *p = dl_task_of(dl_se); | |
1166 | struct rq_flags rf; | |
1167 | struct rq *rq; | |
1168 | ||
1169 | rq = task_rq_lock(p, &rf); | |
1170 | ||
1171 | if (!dl_task(p) || p->state == TASK_DEAD) { | |
387e3130 LA |
1172 | struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); |
1173 | ||
209a0cbd LA |
1174 | if (p->state == TASK_DEAD && dl_se->dl_non_contending) { |
1175 | sub_running_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl)); | |
8fd27231 | 1176 | sub_rq_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl)); |
209a0cbd LA |
1177 | dl_se->dl_non_contending = 0; |
1178 | } | |
387e3130 LA |
1179 | |
1180 | raw_spin_lock(&dl_b->lock); | |
daec5798 | 1181 | __dl_clear(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); |
387e3130 | 1182 | raw_spin_unlock(&dl_b->lock); |
209a0cbd LA |
1183 | __dl_clear_params(p); |
1184 | ||
1185 | goto unlock; | |
1186 | } | |
1187 | if (dl_se->dl_non_contending == 0) | |
1188 | goto unlock; | |
1189 | ||
1190 | sched_clock_tick(); | |
1191 | update_rq_clock(rq); | |
1192 | ||
1193 | sub_running_bw(dl_se->dl_bw, &rq->dl); | |
1194 | dl_se->dl_non_contending = 0; | |
1195 | unlock: | |
1196 | task_rq_unlock(rq, p, &rf); | |
1197 | put_task_struct(p); | |
1198 | ||
1199 | return HRTIMER_NORESTART; | |
1200 | } | |
1201 | ||
1202 | void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se) | |
1203 | { | |
1204 | struct hrtimer *timer = &dl_se->inactive_timer; | |
1205 | ||
1206 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
1207 | timer->function = inactive_task_timer; | |
1208 | } | |
1209 | ||
1baca4ce JL |
1210 | #ifdef CONFIG_SMP |
1211 | ||
1baca4ce JL |
1212 | static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) |
1213 | { | |
1214 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
1215 | ||
1216 | if (dl_rq->earliest_dl.curr == 0 || | |
1217 | dl_time_before(deadline, dl_rq->earliest_dl.curr)) { | |
1baca4ce | 1218 | dl_rq->earliest_dl.curr = deadline; |
d8206bb3 | 1219 | cpudl_set(&rq->rd->cpudl, rq->cpu, deadline); |
1baca4ce JL |
1220 | } |
1221 | } | |
1222 | ||
1223 | static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) | |
1224 | { | |
1225 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
1226 | ||
1227 | /* | |
1228 | * Since we may have removed our earliest (and/or next earliest) | |
1229 | * task we must recompute them. | |
1230 | */ | |
1231 | if (!dl_rq->dl_nr_running) { | |
1232 | dl_rq->earliest_dl.curr = 0; | |
1233 | dl_rq->earliest_dl.next = 0; | |
d8206bb3 | 1234 | cpudl_clear(&rq->rd->cpudl, rq->cpu); |
1baca4ce JL |
1235 | } else { |
1236 | struct rb_node *leftmost = dl_rq->rb_leftmost; | |
1237 | struct sched_dl_entity *entry; | |
1238 | ||
1239 | entry = rb_entry(leftmost, struct sched_dl_entity, rb_node); | |
1240 | dl_rq->earliest_dl.curr = entry->deadline; | |
d8206bb3 | 1241 | cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline); |
1baca4ce JL |
1242 | } |
1243 | } | |
1244 | ||
1245 | #else | |
1246 | ||
1247 | static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} | |
1248 | static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} | |
1249 | ||
1250 | #endif /* CONFIG_SMP */ | |
1251 | ||
1252 | static inline | |
1253 | void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
1254 | { | |
1255 | int prio = dl_task_of(dl_se)->prio; | |
1256 | u64 deadline = dl_se->deadline; | |
1257 | ||
1258 | WARN_ON(!dl_prio(prio)); | |
1259 | dl_rq->dl_nr_running++; | |
72465447 | 1260 | add_nr_running(rq_of_dl_rq(dl_rq), 1); |
1baca4ce JL |
1261 | |
1262 | inc_dl_deadline(dl_rq, deadline); | |
1263 | inc_dl_migration(dl_se, dl_rq); | |
1264 | } | |
1265 | ||
1266 | static inline | |
1267 | void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
1268 | { | |
1269 | int prio = dl_task_of(dl_se)->prio; | |
1270 | ||
1271 | WARN_ON(!dl_prio(prio)); | |
1272 | WARN_ON(!dl_rq->dl_nr_running); | |
1273 | dl_rq->dl_nr_running--; | |
72465447 | 1274 | sub_nr_running(rq_of_dl_rq(dl_rq), 1); |
1baca4ce JL |
1275 | |
1276 | dec_dl_deadline(dl_rq, dl_se->deadline); | |
1277 | dec_dl_migration(dl_se, dl_rq); | |
1278 | } | |
1279 | ||
aab03e05 DF |
1280 | static void __enqueue_dl_entity(struct sched_dl_entity *dl_se) |
1281 | { | |
1282 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
1283 | struct rb_node **link = &dl_rq->rb_root.rb_node; | |
1284 | struct rb_node *parent = NULL; | |
1285 | struct sched_dl_entity *entry; | |
1286 | int leftmost = 1; | |
1287 | ||
1288 | BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node)); | |
1289 | ||
1290 | while (*link) { | |
1291 | parent = *link; | |
1292 | entry = rb_entry(parent, struct sched_dl_entity, rb_node); | |
1293 | if (dl_time_before(dl_se->deadline, entry->deadline)) | |
1294 | link = &parent->rb_left; | |
1295 | else { | |
1296 | link = &parent->rb_right; | |
1297 | leftmost = 0; | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | if (leftmost) | |
1302 | dl_rq->rb_leftmost = &dl_se->rb_node; | |
1303 | ||
1304 | rb_link_node(&dl_se->rb_node, parent, link); | |
1305 | rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root); | |
1306 | ||
1baca4ce | 1307 | inc_dl_tasks(dl_se, dl_rq); |
aab03e05 DF |
1308 | } |
1309 | ||
1310 | static void __dequeue_dl_entity(struct sched_dl_entity *dl_se) | |
1311 | { | |
1312 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
1313 | ||
1314 | if (RB_EMPTY_NODE(&dl_se->rb_node)) | |
1315 | return; | |
1316 | ||
1317 | if (dl_rq->rb_leftmost == &dl_se->rb_node) { | |
1318 | struct rb_node *next_node; | |
1319 | ||
1320 | next_node = rb_next(&dl_se->rb_node); | |
1321 | dl_rq->rb_leftmost = next_node; | |
1322 | } | |
1323 | ||
1324 | rb_erase(&dl_se->rb_node, &dl_rq->rb_root); | |
1325 | RB_CLEAR_NODE(&dl_se->rb_node); | |
1326 | ||
1baca4ce | 1327 | dec_dl_tasks(dl_se, dl_rq); |
aab03e05 DF |
1328 | } |
1329 | ||
1330 | static void | |
2d3d891d DF |
1331 | enqueue_dl_entity(struct sched_dl_entity *dl_se, |
1332 | struct sched_dl_entity *pi_se, int flags) | |
aab03e05 DF |
1333 | { |
1334 | BUG_ON(on_dl_rq(dl_se)); | |
1335 | ||
1336 | /* | |
1337 | * If this is a wakeup or a new instance, the scheduling | |
1338 | * parameters of the task might need updating. Otherwise, | |
1339 | * we want a replenishment of its runtime. | |
1340 | */ | |
e36d8677 | 1341 | if (flags & ENQUEUE_WAKEUP) { |
8fd27231 | 1342 | task_contending(dl_se, flags); |
2d3d891d | 1343 | update_dl_entity(dl_se, pi_se); |
e36d8677 | 1344 | } else if (flags & ENQUEUE_REPLENISH) { |
6a503c3b | 1345 | replenish_dl_entity(dl_se, pi_se); |
e36d8677 | 1346 | } |
aab03e05 DF |
1347 | |
1348 | __enqueue_dl_entity(dl_se); | |
1349 | } | |
1350 | ||
1351 | static void dequeue_dl_entity(struct sched_dl_entity *dl_se) | |
1352 | { | |
1353 | __dequeue_dl_entity(dl_se); | |
1354 | } | |
1355 | ||
1356 | static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags) | |
1357 | { | |
2d3d891d DF |
1358 | struct task_struct *pi_task = rt_mutex_get_top_task(p); |
1359 | struct sched_dl_entity *pi_se = &p->dl; | |
1360 | ||
1361 | /* | |
1362 | * Use the scheduling parameters of the top pi-waiter | |
ff277d42 | 1363 | * task if we have one and its (absolute) deadline is |
2d3d891d DF |
1364 | * smaller than our one... OTW we keep our runtime and |
1365 | * deadline. | |
1366 | */ | |
64be6f1f | 1367 | if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) { |
2d3d891d | 1368 | pi_se = &pi_task->dl; |
64be6f1f JL |
1369 | } else if (!dl_prio(p->normal_prio)) { |
1370 | /* | |
1371 | * Special case in which we have a !SCHED_DEADLINE task | |
1372 | * that is going to be deboosted, but exceedes its | |
1373 | * runtime while doing so. No point in replenishing | |
1374 | * it, as it's going to return back to its original | |
1375 | * scheduling class after this. | |
1376 | */ | |
1377 | BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH); | |
1378 | return; | |
1379 | } | |
2d3d891d | 1380 | |
df8eac8c DBO |
1381 | /* |
1382 | * Check if a constrained deadline task was activated | |
1383 | * after the deadline but before the next period. | |
1384 | * If that is the case, the task will be throttled and | |
1385 | * the replenishment timer will be set to the next period. | |
1386 | */ | |
3effcb42 | 1387 | if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl)) |
df8eac8c DBO |
1388 | dl_check_constrained_dl(&p->dl); |
1389 | ||
8fd27231 LA |
1390 | if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) { |
1391 | add_rq_bw(p->dl.dl_bw, &rq->dl); | |
e36d8677 | 1392 | add_running_bw(p->dl.dl_bw, &rq->dl); |
8fd27231 | 1393 | } |
e36d8677 | 1394 | |
aab03e05 | 1395 | /* |
e36d8677 | 1396 | * If p is throttled, we do not enqueue it. In fact, if it exhausted |
aab03e05 DF |
1397 | * its budget it needs a replenishment and, since it now is on |
1398 | * its rq, the bandwidth timer callback (which clearly has not | |
1399 | * run yet) will take care of this. | |
e36d8677 LA |
1400 | * However, the active utilization does not depend on the fact |
1401 | * that the task is on the runqueue or not (but depends on the | |
1402 | * task's state - in GRUB parlance, "inactive" vs "active contending"). | |
1403 | * In other words, even if a task is throttled its utilization must | |
1404 | * be counted in the active utilization; hence, we need to call | |
1405 | * add_running_bw(). | |
aab03e05 | 1406 | */ |
e36d8677 | 1407 | if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) { |
209a0cbd | 1408 | if (flags & ENQUEUE_WAKEUP) |
8fd27231 | 1409 | task_contending(&p->dl, flags); |
209a0cbd | 1410 | |
aab03e05 | 1411 | return; |
e36d8677 | 1412 | } |
aab03e05 | 1413 | |
2d3d891d | 1414 | enqueue_dl_entity(&p->dl, pi_se, flags); |
1baca4ce | 1415 | |
4b53a341 | 1416 | if (!task_current(rq, p) && p->nr_cpus_allowed > 1) |
1baca4ce | 1417 | enqueue_pushable_dl_task(rq, p); |
aab03e05 DF |
1418 | } |
1419 | ||
1420 | static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) | |
1421 | { | |
1422 | dequeue_dl_entity(&p->dl); | |
1baca4ce | 1423 | dequeue_pushable_dl_task(rq, p); |
aab03e05 DF |
1424 | } |
1425 | ||
1426 | static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) | |
1427 | { | |
1428 | update_curr_dl(rq); | |
1429 | __dequeue_task_dl(rq, p, flags); | |
e36d8677 | 1430 | |
8fd27231 | 1431 | if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) { |
e36d8677 | 1432 | sub_running_bw(p->dl.dl_bw, &rq->dl); |
8fd27231 LA |
1433 | sub_rq_bw(p->dl.dl_bw, &rq->dl); |
1434 | } | |
e36d8677 LA |
1435 | |
1436 | /* | |
209a0cbd LA |
1437 | * This check allows to start the inactive timer (or to immediately |
1438 | * decrease the active utilization, if needed) in two cases: | |
e36d8677 LA |
1439 | * when the task blocks and when it is terminating |
1440 | * (p->state == TASK_DEAD). We can handle the two cases in the same | |
1441 | * way, because from GRUB's point of view the same thing is happening | |
1442 | * (the task moves from "active contending" to "active non contending" | |
1443 | * or "inactive") | |
1444 | */ | |
1445 | if (flags & DEQUEUE_SLEEP) | |
209a0cbd | 1446 | task_non_contending(p); |
aab03e05 DF |
1447 | } |
1448 | ||
1449 | /* | |
1450 | * Yield task semantic for -deadline tasks is: | |
1451 | * | |
1452 | * get off from the CPU until our next instance, with | |
1453 | * a new runtime. This is of little use now, since we | |
1454 | * don't have a bandwidth reclaiming mechanism. Anyway, | |
1455 | * bandwidth reclaiming is planned for the future, and | |
1456 | * yield_task_dl will indicate that some spare budget | |
1457 | * is available for other task instances to use it. | |
1458 | */ | |
1459 | static void yield_task_dl(struct rq *rq) | |
1460 | { | |
aab03e05 DF |
1461 | /* |
1462 | * We make the task go to sleep until its current deadline by | |
1463 | * forcing its runtime to zero. This way, update_curr_dl() stops | |
1464 | * it and the bandwidth timer will wake it up and will give it | |
5bfd126e | 1465 | * new scheduling parameters (thanks to dl_yielded=1). |
aab03e05 | 1466 | */ |
48be3a67 PZ |
1467 | rq->curr->dl.dl_yielded = 1; |
1468 | ||
6f1607f1 | 1469 | update_rq_clock(rq); |
aab03e05 | 1470 | update_curr_dl(rq); |
44fb085b WL |
1471 | /* |
1472 | * Tell update_rq_clock() that we've just updated, | |
1473 | * so we don't do microscopic update in schedule() | |
1474 | * and double the fastpath cost. | |
1475 | */ | |
1476 | rq_clock_skip_update(rq, true); | |
aab03e05 DF |
1477 | } |
1478 | ||
1baca4ce JL |
1479 | #ifdef CONFIG_SMP |
1480 | ||
1481 | static int find_later_rq(struct task_struct *task); | |
1baca4ce JL |
1482 | |
1483 | static int | |
1484 | select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags) | |
1485 | { | |
1486 | struct task_struct *curr; | |
1487 | struct rq *rq; | |
1488 | ||
1d7e974c | 1489 | if (sd_flag != SD_BALANCE_WAKE) |
1baca4ce JL |
1490 | goto out; |
1491 | ||
1492 | rq = cpu_rq(cpu); | |
1493 | ||
1494 | rcu_read_lock(); | |
316c1608 | 1495 | curr = READ_ONCE(rq->curr); /* unlocked access */ |
1baca4ce JL |
1496 | |
1497 | /* | |
1498 | * If we are dealing with a -deadline task, we must | |
1499 | * decide where to wake it up. | |
1500 | * If it has a later deadline and the current task | |
1501 | * on this rq can't move (provided the waking task | |
1502 | * can!) we prefer to send it somewhere else. On the | |
1503 | * other hand, if it has a shorter deadline, we | |
1504 | * try to make it stay here, it might be important. | |
1505 | */ | |
1506 | if (unlikely(dl_task(curr)) && | |
4b53a341 | 1507 | (curr->nr_cpus_allowed < 2 || |
1baca4ce | 1508 | !dl_entity_preempt(&p->dl, &curr->dl)) && |
4b53a341 | 1509 | (p->nr_cpus_allowed > 1)) { |
1baca4ce JL |
1510 | int target = find_later_rq(p); |
1511 | ||
9d514262 | 1512 | if (target != -1 && |
5aa50507 LA |
1513 | (dl_time_before(p->dl.deadline, |
1514 | cpu_rq(target)->dl.earliest_dl.curr) || | |
1515 | (cpu_rq(target)->dl.dl_nr_running == 0))) | |
1baca4ce JL |
1516 | cpu = target; |
1517 | } | |
1518 | rcu_read_unlock(); | |
1519 | ||
1520 | out: | |
1521 | return cpu; | |
1522 | } | |
1523 | ||
209a0cbd LA |
1524 | static void migrate_task_rq_dl(struct task_struct *p) |
1525 | { | |
1526 | struct rq *rq; | |
1527 | ||
8fd27231 | 1528 | if (p->state != TASK_WAKING) |
209a0cbd LA |
1529 | return; |
1530 | ||
1531 | rq = task_rq(p); | |
1532 | /* | |
1533 | * Since p->state == TASK_WAKING, set_task_cpu() has been called | |
1534 | * from try_to_wake_up(). Hence, p->pi_lock is locked, but | |
1535 | * rq->lock is not... So, lock it | |
1536 | */ | |
1537 | raw_spin_lock(&rq->lock); | |
8fd27231 LA |
1538 | if (p->dl.dl_non_contending) { |
1539 | sub_running_bw(p->dl.dl_bw, &rq->dl); | |
1540 | p->dl.dl_non_contending = 0; | |
1541 | /* | |
1542 | * If the timer handler is currently running and the | |
1543 | * timer cannot be cancelled, inactive_task_timer() | |
1544 | * will see that dl_not_contending is not set, and | |
1545 | * will not touch the rq's active utilization, | |
1546 | * so we are still safe. | |
1547 | */ | |
1548 | if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) | |
1549 | put_task_struct(p); | |
1550 | } | |
1551 | sub_rq_bw(p->dl.dl_bw, &rq->dl); | |
209a0cbd LA |
1552 | raw_spin_unlock(&rq->lock); |
1553 | } | |
1554 | ||
1baca4ce JL |
1555 | static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p) |
1556 | { | |
1557 | /* | |
1558 | * Current can't be migrated, useless to reschedule, | |
1559 | * let's hope p can move out. | |
1560 | */ | |
4b53a341 | 1561 | if (rq->curr->nr_cpus_allowed == 1 || |
6bfd6d72 | 1562 | cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1) |
1baca4ce JL |
1563 | return; |
1564 | ||
1565 | /* | |
1566 | * p is migratable, so let's not schedule it and | |
1567 | * see if it is pushed or pulled somewhere else. | |
1568 | */ | |
4b53a341 | 1569 | if (p->nr_cpus_allowed != 1 && |
6bfd6d72 | 1570 | cpudl_find(&rq->rd->cpudl, p, NULL) != -1) |
1baca4ce JL |
1571 | return; |
1572 | ||
8875125e | 1573 | resched_curr(rq); |
1baca4ce JL |
1574 | } |
1575 | ||
1576 | #endif /* CONFIG_SMP */ | |
1577 | ||
aab03e05 DF |
1578 | /* |
1579 | * Only called when both the current and waking task are -deadline | |
1580 | * tasks. | |
1581 | */ | |
1582 | static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, | |
1583 | int flags) | |
1584 | { | |
1baca4ce | 1585 | if (dl_entity_preempt(&p->dl, &rq->curr->dl)) { |
8875125e | 1586 | resched_curr(rq); |
1baca4ce JL |
1587 | return; |
1588 | } | |
1589 | ||
1590 | #ifdef CONFIG_SMP | |
1591 | /* | |
1592 | * In the unlikely case current and p have the same deadline | |
1593 | * let us try to decide what's the best thing to do... | |
1594 | */ | |
332ac17e DF |
1595 | if ((p->dl.deadline == rq->curr->dl.deadline) && |
1596 | !test_tsk_need_resched(rq->curr)) | |
1baca4ce JL |
1597 | check_preempt_equal_dl(rq, p); |
1598 | #endif /* CONFIG_SMP */ | |
aab03e05 DF |
1599 | } |
1600 | ||
1601 | #ifdef CONFIG_SCHED_HRTICK | |
1602 | static void start_hrtick_dl(struct rq *rq, struct task_struct *p) | |
1603 | { | |
177ef2a6 | 1604 | hrtick_start(rq, p->dl.runtime); |
aab03e05 | 1605 | } |
36ce9881 WL |
1606 | #else /* !CONFIG_SCHED_HRTICK */ |
1607 | static void start_hrtick_dl(struct rq *rq, struct task_struct *p) | |
1608 | { | |
1609 | } | |
aab03e05 DF |
1610 | #endif |
1611 | ||
1612 | static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq, | |
1613 | struct dl_rq *dl_rq) | |
1614 | { | |
1615 | struct rb_node *left = dl_rq->rb_leftmost; | |
1616 | ||
1617 | if (!left) | |
1618 | return NULL; | |
1619 | ||
1620 | return rb_entry(left, struct sched_dl_entity, rb_node); | |
1621 | } | |
1622 | ||
e7904a28 | 1623 | struct task_struct * |
d8ac8971 | 1624 | pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
aab03e05 DF |
1625 | { |
1626 | struct sched_dl_entity *dl_se; | |
1627 | struct task_struct *p; | |
1628 | struct dl_rq *dl_rq; | |
1629 | ||
1630 | dl_rq = &rq->dl; | |
1631 | ||
a1d9a323 | 1632 | if (need_pull_dl_task(rq, prev)) { |
cbce1a68 PZ |
1633 | /* |
1634 | * This is OK, because current is on_cpu, which avoids it being | |
1635 | * picked for load-balance and preemption/IRQs are still | |
1636 | * disabled avoiding further scheduler activity on it and we're | |
1637 | * being very careful to re-start the picking loop. | |
1638 | */ | |
d8ac8971 | 1639 | rq_unpin_lock(rq, rf); |
38033c37 | 1640 | pull_dl_task(rq); |
d8ac8971 | 1641 | rq_repin_lock(rq, rf); |
a1d9a323 | 1642 | /* |
176cedc4 | 1643 | * pull_dl_task() can drop (and re-acquire) rq->lock; this |
a1d9a323 KT |
1644 | * means a stop task can slip in, in which case we need to |
1645 | * re-start task selection. | |
1646 | */ | |
da0c1e65 | 1647 | if (rq->stop && task_on_rq_queued(rq->stop)) |
a1d9a323 KT |
1648 | return RETRY_TASK; |
1649 | } | |
1650 | ||
734ff2a7 KT |
1651 | /* |
1652 | * When prev is DL, we may throttle it in put_prev_task(). | |
1653 | * So, we update time before we check for dl_nr_running. | |
1654 | */ | |
1655 | if (prev->sched_class == &dl_sched_class) | |
1656 | update_curr_dl(rq); | |
38033c37 | 1657 | |
aab03e05 DF |
1658 | if (unlikely(!dl_rq->dl_nr_running)) |
1659 | return NULL; | |
1660 | ||
3f1d2a31 | 1661 | put_prev_task(rq, prev); |
606dba2e | 1662 | |
aab03e05 DF |
1663 | dl_se = pick_next_dl_entity(rq, dl_rq); |
1664 | BUG_ON(!dl_se); | |
1665 | ||
1666 | p = dl_task_of(dl_se); | |
1667 | p->se.exec_start = rq_clock_task(rq); | |
1baca4ce JL |
1668 | |
1669 | /* Running task will never be pushed. */ | |
71362650 | 1670 | dequeue_pushable_dl_task(rq, p); |
1baca4ce | 1671 | |
aab03e05 DF |
1672 | if (hrtick_enabled(rq)) |
1673 | start_hrtick_dl(rq, p); | |
1baca4ce | 1674 | |
e3fca9e7 | 1675 | queue_push_tasks(rq); |
1baca4ce | 1676 | |
aab03e05 DF |
1677 | return p; |
1678 | } | |
1679 | ||
1680 | static void put_prev_task_dl(struct rq *rq, struct task_struct *p) | |
1681 | { | |
1682 | update_curr_dl(rq); | |
1baca4ce | 1683 | |
4b53a341 | 1684 | if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1) |
1baca4ce | 1685 | enqueue_pushable_dl_task(rq, p); |
aab03e05 DF |
1686 | } |
1687 | ||
1688 | static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued) | |
1689 | { | |
1690 | update_curr_dl(rq); | |
1691 | ||
a7bebf48 WL |
1692 | /* |
1693 | * Even when we have runtime, update_curr_dl() might have resulted in us | |
1694 | * not being the leftmost task anymore. In that case NEED_RESCHED will | |
1695 | * be set and schedule() will start a new hrtick for the next task. | |
1696 | */ | |
1697 | if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 && | |
1698 | is_leftmost(p, &rq->dl)) | |
aab03e05 | 1699 | start_hrtick_dl(rq, p); |
aab03e05 DF |
1700 | } |
1701 | ||
1702 | static void task_fork_dl(struct task_struct *p) | |
1703 | { | |
1704 | /* | |
1705 | * SCHED_DEADLINE tasks cannot fork and this is achieved through | |
1706 | * sched_fork() | |
1707 | */ | |
1708 | } | |
1709 | ||
aab03e05 DF |
1710 | static void set_curr_task_dl(struct rq *rq) |
1711 | { | |
1712 | struct task_struct *p = rq->curr; | |
1713 | ||
1714 | p->se.exec_start = rq_clock_task(rq); | |
1baca4ce JL |
1715 | |
1716 | /* You can't push away the running task */ | |
1717 | dequeue_pushable_dl_task(rq, p); | |
1718 | } | |
1719 | ||
1720 | #ifdef CONFIG_SMP | |
1721 | ||
1722 | /* Only try algorithms three times */ | |
1723 | #define DL_MAX_TRIES 3 | |
1724 | ||
1725 | static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu) | |
1726 | { | |
1727 | if (!task_running(rq, p) && | |
0c98d344 | 1728 | cpumask_test_cpu(cpu, &p->cpus_allowed)) |
1baca4ce | 1729 | return 1; |
1baca4ce JL |
1730 | return 0; |
1731 | } | |
1732 | ||
8b5e770e WL |
1733 | /* |
1734 | * Return the earliest pushable rq's task, which is suitable to be executed | |
1735 | * on the CPU, NULL otherwise: | |
1736 | */ | |
1737 | static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu) | |
1738 | { | |
1739 | struct rb_node *next_node = rq->dl.pushable_dl_tasks_leftmost; | |
1740 | struct task_struct *p = NULL; | |
1741 | ||
1742 | if (!has_pushable_dl_tasks(rq)) | |
1743 | return NULL; | |
1744 | ||
1745 | next_node: | |
1746 | if (next_node) { | |
1747 | p = rb_entry(next_node, struct task_struct, pushable_dl_tasks); | |
1748 | ||
1749 | if (pick_dl_task(rq, p, cpu)) | |
1750 | return p; | |
1751 | ||
1752 | next_node = rb_next(next_node); | |
1753 | goto next_node; | |
1754 | } | |
1755 | ||
1756 | return NULL; | |
1757 | } | |
1758 | ||
1baca4ce JL |
1759 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl); |
1760 | ||
1761 | static int find_later_rq(struct task_struct *task) | |
1762 | { | |
1763 | struct sched_domain *sd; | |
4ba29684 | 1764 | struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl); |
1baca4ce JL |
1765 | int this_cpu = smp_processor_id(); |
1766 | int best_cpu, cpu = task_cpu(task); | |
1767 | ||
1768 | /* Make sure the mask is initialized first */ | |
1769 | if (unlikely(!later_mask)) | |
1770 | return -1; | |
1771 | ||
4b53a341 | 1772 | if (task->nr_cpus_allowed == 1) |
1baca4ce JL |
1773 | return -1; |
1774 | ||
91ec6778 JL |
1775 | /* |
1776 | * We have to consider system topology and task affinity | |
1777 | * first, then we can look for a suitable cpu. | |
1778 | */ | |
6bfd6d72 JL |
1779 | best_cpu = cpudl_find(&task_rq(task)->rd->cpudl, |
1780 | task, later_mask); | |
1baca4ce JL |
1781 | if (best_cpu == -1) |
1782 | return -1; | |
1783 | ||
1784 | /* | |
1785 | * If we are here, some target has been found, | |
1786 | * the most suitable of which is cached in best_cpu. | |
1787 | * This is, among the runqueues where the current tasks | |
1788 | * have later deadlines than the task's one, the rq | |
1789 | * with the latest possible one. | |
1790 | * | |
1791 | * Now we check how well this matches with task's | |
1792 | * affinity and system topology. | |
1793 | * | |
1794 | * The last cpu where the task run is our first | |
1795 | * guess, since it is most likely cache-hot there. | |
1796 | */ | |
1797 | if (cpumask_test_cpu(cpu, later_mask)) | |
1798 | return cpu; | |
1799 | /* | |
1800 | * Check if this_cpu is to be skipped (i.e., it is | |
1801 | * not in the mask) or not. | |
1802 | */ | |
1803 | if (!cpumask_test_cpu(this_cpu, later_mask)) | |
1804 | this_cpu = -1; | |
1805 | ||
1806 | rcu_read_lock(); | |
1807 | for_each_domain(cpu, sd) { | |
1808 | if (sd->flags & SD_WAKE_AFFINE) { | |
1809 | ||
1810 | /* | |
1811 | * If possible, preempting this_cpu is | |
1812 | * cheaper than migrating. | |
1813 | */ | |
1814 | if (this_cpu != -1 && | |
1815 | cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { | |
1816 | rcu_read_unlock(); | |
1817 | return this_cpu; | |
1818 | } | |
1819 | ||
1820 | /* | |
1821 | * Last chance: if best_cpu is valid and is | |
1822 | * in the mask, that becomes our choice. | |
1823 | */ | |
1824 | if (best_cpu < nr_cpu_ids && | |
1825 | cpumask_test_cpu(best_cpu, sched_domain_span(sd))) { | |
1826 | rcu_read_unlock(); | |
1827 | return best_cpu; | |
1828 | } | |
1829 | } | |
1830 | } | |
1831 | rcu_read_unlock(); | |
1832 | ||
1833 | /* | |
1834 | * At this point, all our guesses failed, we just return | |
1835 | * 'something', and let the caller sort the things out. | |
1836 | */ | |
1837 | if (this_cpu != -1) | |
1838 | return this_cpu; | |
1839 | ||
1840 | cpu = cpumask_any(later_mask); | |
1841 | if (cpu < nr_cpu_ids) | |
1842 | return cpu; | |
1843 | ||
1844 | return -1; | |
1845 | } | |
1846 | ||
1847 | /* Locks the rq it finds */ | |
1848 | static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq) | |
1849 | { | |
1850 | struct rq *later_rq = NULL; | |
1851 | int tries; | |
1852 | int cpu; | |
1853 | ||
1854 | for (tries = 0; tries < DL_MAX_TRIES; tries++) { | |
1855 | cpu = find_later_rq(task); | |
1856 | ||
1857 | if ((cpu == -1) || (cpu == rq->cpu)) | |
1858 | break; | |
1859 | ||
1860 | later_rq = cpu_rq(cpu); | |
1861 | ||
5aa50507 LA |
1862 | if (later_rq->dl.dl_nr_running && |
1863 | !dl_time_before(task->dl.deadline, | |
9d514262 WL |
1864 | later_rq->dl.earliest_dl.curr)) { |
1865 | /* | |
1866 | * Target rq has tasks of equal or earlier deadline, | |
1867 | * retrying does not release any lock and is unlikely | |
1868 | * to yield a different result. | |
1869 | */ | |
1870 | later_rq = NULL; | |
1871 | break; | |
1872 | } | |
1873 | ||
1baca4ce JL |
1874 | /* Retry if something changed. */ |
1875 | if (double_lock_balance(rq, later_rq)) { | |
1876 | if (unlikely(task_rq(task) != rq || | |
0c98d344 | 1877 | !cpumask_test_cpu(later_rq->cpu, &task->cpus_allowed) || |
da0c1e65 | 1878 | task_running(rq, task) || |
13b5ab02 | 1879 | !dl_task(task) || |
da0c1e65 | 1880 | !task_on_rq_queued(task))) { |
1baca4ce JL |
1881 | double_unlock_balance(rq, later_rq); |
1882 | later_rq = NULL; | |
1883 | break; | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | /* | |
1888 | * If the rq we found has no -deadline task, or | |
1889 | * its earliest one has a later deadline than our | |
1890 | * task, the rq is a good one. | |
1891 | */ | |
1892 | if (!later_rq->dl.dl_nr_running || | |
1893 | dl_time_before(task->dl.deadline, | |
1894 | later_rq->dl.earliest_dl.curr)) | |
1895 | break; | |
1896 | ||
1897 | /* Otherwise we try again. */ | |
1898 | double_unlock_balance(rq, later_rq); | |
1899 | later_rq = NULL; | |
1900 | } | |
1901 | ||
1902 | return later_rq; | |
1903 | } | |
1904 | ||
1905 | static struct task_struct *pick_next_pushable_dl_task(struct rq *rq) | |
1906 | { | |
1907 | struct task_struct *p; | |
1908 | ||
1909 | if (!has_pushable_dl_tasks(rq)) | |
1910 | return NULL; | |
1911 | ||
1912 | p = rb_entry(rq->dl.pushable_dl_tasks_leftmost, | |
1913 | struct task_struct, pushable_dl_tasks); | |
1914 | ||
1915 | BUG_ON(rq->cpu != task_cpu(p)); | |
1916 | BUG_ON(task_current(rq, p)); | |
4b53a341 | 1917 | BUG_ON(p->nr_cpus_allowed <= 1); |
1baca4ce | 1918 | |
da0c1e65 | 1919 | BUG_ON(!task_on_rq_queued(p)); |
1baca4ce JL |
1920 | BUG_ON(!dl_task(p)); |
1921 | ||
1922 | return p; | |
1923 | } | |
1924 | ||
1925 | /* | |
1926 | * See if the non running -deadline tasks on this rq | |
1927 | * can be sent to some other CPU where they can preempt | |
1928 | * and start executing. | |
1929 | */ | |
1930 | static int push_dl_task(struct rq *rq) | |
1931 | { | |
1932 | struct task_struct *next_task; | |
1933 | struct rq *later_rq; | |
c51b8ab5 | 1934 | int ret = 0; |
1baca4ce JL |
1935 | |
1936 | if (!rq->dl.overloaded) | |
1937 | return 0; | |
1938 | ||
1939 | next_task = pick_next_pushable_dl_task(rq); | |
1940 | if (!next_task) | |
1941 | return 0; | |
1942 | ||
1943 | retry: | |
1944 | if (unlikely(next_task == rq->curr)) { | |
1945 | WARN_ON(1); | |
1946 | return 0; | |
1947 | } | |
1948 | ||
1949 | /* | |
1950 | * If next_task preempts rq->curr, and rq->curr | |
1951 | * can move away, it makes sense to just reschedule | |
1952 | * without going further in pushing next_task. | |
1953 | */ | |
1954 | if (dl_task(rq->curr) && | |
1955 | dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) && | |
4b53a341 | 1956 | rq->curr->nr_cpus_allowed > 1) { |
8875125e | 1957 | resched_curr(rq); |
1baca4ce JL |
1958 | return 0; |
1959 | } | |
1960 | ||
1961 | /* We might release rq lock */ | |
1962 | get_task_struct(next_task); | |
1963 | ||
1964 | /* Will lock the rq it'll find */ | |
1965 | later_rq = find_lock_later_rq(next_task, rq); | |
1966 | if (!later_rq) { | |
1967 | struct task_struct *task; | |
1968 | ||
1969 | /* | |
1970 | * We must check all this again, since | |
1971 | * find_lock_later_rq releases rq->lock and it is | |
1972 | * then possible that next_task has migrated. | |
1973 | */ | |
1974 | task = pick_next_pushable_dl_task(rq); | |
a776b968 | 1975 | if (task == next_task) { |
1baca4ce JL |
1976 | /* |
1977 | * The task is still there. We don't try | |
1978 | * again, some other cpu will pull it when ready. | |
1979 | */ | |
1baca4ce JL |
1980 | goto out; |
1981 | } | |
1982 | ||
1983 | if (!task) | |
1984 | /* No more tasks */ | |
1985 | goto out; | |
1986 | ||
1987 | put_task_struct(next_task); | |
1988 | next_task = task; | |
1989 | goto retry; | |
1990 | } | |
1991 | ||
1992 | deactivate_task(rq, next_task, 0); | |
e36d8677 | 1993 | sub_running_bw(next_task->dl.dl_bw, &rq->dl); |
8fd27231 | 1994 | sub_rq_bw(next_task->dl.dl_bw, &rq->dl); |
1baca4ce | 1995 | set_task_cpu(next_task, later_rq->cpu); |
8fd27231 | 1996 | add_rq_bw(next_task->dl.dl_bw, &later_rq->dl); |
e36d8677 | 1997 | add_running_bw(next_task->dl.dl_bw, &later_rq->dl); |
1baca4ce | 1998 | activate_task(later_rq, next_task, 0); |
c51b8ab5 | 1999 | ret = 1; |
1baca4ce | 2000 | |
8875125e | 2001 | resched_curr(later_rq); |
1baca4ce JL |
2002 | |
2003 | double_unlock_balance(rq, later_rq); | |
2004 | ||
2005 | out: | |
2006 | put_task_struct(next_task); | |
2007 | ||
c51b8ab5 | 2008 | return ret; |
1baca4ce JL |
2009 | } |
2010 | ||
2011 | static void push_dl_tasks(struct rq *rq) | |
2012 | { | |
4ffa08ed | 2013 | /* push_dl_task() will return true if it moved a -deadline task */ |
1baca4ce JL |
2014 | while (push_dl_task(rq)) |
2015 | ; | |
aab03e05 DF |
2016 | } |
2017 | ||
0ea60c20 | 2018 | static void pull_dl_task(struct rq *this_rq) |
1baca4ce | 2019 | { |
0ea60c20 | 2020 | int this_cpu = this_rq->cpu, cpu; |
1baca4ce | 2021 | struct task_struct *p; |
0ea60c20 | 2022 | bool resched = false; |
1baca4ce JL |
2023 | struct rq *src_rq; |
2024 | u64 dmin = LONG_MAX; | |
2025 | ||
2026 | if (likely(!dl_overloaded(this_rq))) | |
0ea60c20 | 2027 | return; |
1baca4ce JL |
2028 | |
2029 | /* | |
2030 | * Match the barrier from dl_set_overloaded; this guarantees that if we | |
2031 | * see overloaded we must also see the dlo_mask bit. | |
2032 | */ | |
2033 | smp_rmb(); | |
2034 | ||
2035 | for_each_cpu(cpu, this_rq->rd->dlo_mask) { | |
2036 | if (this_cpu == cpu) | |
2037 | continue; | |
2038 | ||
2039 | src_rq = cpu_rq(cpu); | |
2040 | ||
2041 | /* | |
2042 | * It looks racy, abd it is! However, as in sched_rt.c, | |
2043 | * we are fine with this. | |
2044 | */ | |
2045 | if (this_rq->dl.dl_nr_running && | |
2046 | dl_time_before(this_rq->dl.earliest_dl.curr, | |
2047 | src_rq->dl.earliest_dl.next)) | |
2048 | continue; | |
2049 | ||
2050 | /* Might drop this_rq->lock */ | |
2051 | double_lock_balance(this_rq, src_rq); | |
2052 | ||
2053 | /* | |
2054 | * If there are no more pullable tasks on the | |
2055 | * rq, we're done with it. | |
2056 | */ | |
2057 | if (src_rq->dl.dl_nr_running <= 1) | |
2058 | goto skip; | |
2059 | ||
8b5e770e | 2060 | p = pick_earliest_pushable_dl_task(src_rq, this_cpu); |
1baca4ce JL |
2061 | |
2062 | /* | |
2063 | * We found a task to be pulled if: | |
2064 | * - it preempts our current (if there's one), | |
2065 | * - it will preempt the last one we pulled (if any). | |
2066 | */ | |
2067 | if (p && dl_time_before(p->dl.deadline, dmin) && | |
2068 | (!this_rq->dl.dl_nr_running || | |
2069 | dl_time_before(p->dl.deadline, | |
2070 | this_rq->dl.earliest_dl.curr))) { | |
2071 | WARN_ON(p == src_rq->curr); | |
da0c1e65 | 2072 | WARN_ON(!task_on_rq_queued(p)); |
1baca4ce JL |
2073 | |
2074 | /* | |
2075 | * Then we pull iff p has actually an earlier | |
2076 | * deadline than the current task of its runqueue. | |
2077 | */ | |
2078 | if (dl_time_before(p->dl.deadline, | |
2079 | src_rq->curr->dl.deadline)) | |
2080 | goto skip; | |
2081 | ||
0ea60c20 | 2082 | resched = true; |
1baca4ce JL |
2083 | |
2084 | deactivate_task(src_rq, p, 0); | |
e36d8677 | 2085 | sub_running_bw(p->dl.dl_bw, &src_rq->dl); |
8fd27231 | 2086 | sub_rq_bw(p->dl.dl_bw, &src_rq->dl); |
1baca4ce | 2087 | set_task_cpu(p, this_cpu); |
8fd27231 | 2088 | add_rq_bw(p->dl.dl_bw, &this_rq->dl); |
e36d8677 | 2089 | add_running_bw(p->dl.dl_bw, &this_rq->dl); |
1baca4ce JL |
2090 | activate_task(this_rq, p, 0); |
2091 | dmin = p->dl.deadline; | |
2092 | ||
2093 | /* Is there any other task even earlier? */ | |
2094 | } | |
2095 | skip: | |
2096 | double_unlock_balance(this_rq, src_rq); | |
2097 | } | |
2098 | ||
0ea60c20 PZ |
2099 | if (resched) |
2100 | resched_curr(this_rq); | |
1baca4ce JL |
2101 | } |
2102 | ||
2103 | /* | |
2104 | * Since the task is not running and a reschedule is not going to happen | |
2105 | * anytime soon on its runqueue, we try pushing it away now. | |
2106 | */ | |
2107 | static void task_woken_dl(struct rq *rq, struct task_struct *p) | |
2108 | { | |
2109 | if (!task_running(rq, p) && | |
2110 | !test_tsk_need_resched(rq->curr) && | |
4b53a341 | 2111 | p->nr_cpus_allowed > 1 && |
1baca4ce | 2112 | dl_task(rq->curr) && |
4b53a341 | 2113 | (rq->curr->nr_cpus_allowed < 2 || |
6b0a563f | 2114 | !dl_entity_preempt(&p->dl, &rq->curr->dl))) { |
1baca4ce JL |
2115 | push_dl_tasks(rq); |
2116 | } | |
2117 | } | |
2118 | ||
2119 | static void set_cpus_allowed_dl(struct task_struct *p, | |
2120 | const struct cpumask *new_mask) | |
2121 | { | |
7f51412a | 2122 | struct root_domain *src_rd; |
6c37067e | 2123 | struct rq *rq; |
1baca4ce JL |
2124 | |
2125 | BUG_ON(!dl_task(p)); | |
2126 | ||
7f51412a JL |
2127 | rq = task_rq(p); |
2128 | src_rd = rq->rd; | |
2129 | /* | |
2130 | * Migrating a SCHED_DEADLINE task between exclusive | |
2131 | * cpusets (different root_domains) entails a bandwidth | |
2132 | * update. We already made space for us in the destination | |
2133 | * domain (see cpuset_can_attach()). | |
2134 | */ | |
2135 | if (!cpumask_intersects(src_rd->span, new_mask)) { | |
2136 | struct dl_bw *src_dl_b; | |
2137 | ||
2138 | src_dl_b = dl_bw_of(cpu_of(rq)); | |
2139 | /* | |
2140 | * We now free resources of the root_domain we are migrating | |
2141 | * off. In the worst case, sched_setattr() may temporary fail | |
2142 | * until we complete the update. | |
2143 | */ | |
2144 | raw_spin_lock(&src_dl_b->lock); | |
daec5798 | 2145 | __dl_clear(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); |
7f51412a JL |
2146 | raw_spin_unlock(&src_dl_b->lock); |
2147 | } | |
2148 | ||
6c37067e | 2149 | set_cpus_allowed_common(p, new_mask); |
1baca4ce JL |
2150 | } |
2151 | ||
2152 | /* Assumes rq->lock is held */ | |
2153 | static void rq_online_dl(struct rq *rq) | |
2154 | { | |
2155 | if (rq->dl.overloaded) | |
2156 | dl_set_overload(rq); | |
6bfd6d72 | 2157 | |
16b26943 | 2158 | cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu); |
6bfd6d72 | 2159 | if (rq->dl.dl_nr_running > 0) |
d8206bb3 | 2160 | cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr); |
1baca4ce JL |
2161 | } |
2162 | ||
2163 | /* Assumes rq->lock is held */ | |
2164 | static void rq_offline_dl(struct rq *rq) | |
2165 | { | |
2166 | if (rq->dl.overloaded) | |
2167 | dl_clear_overload(rq); | |
6bfd6d72 | 2168 | |
d8206bb3 | 2169 | cpudl_clear(&rq->rd->cpudl, rq->cpu); |
16b26943 | 2170 | cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu); |
1baca4ce JL |
2171 | } |
2172 | ||
a6c0e746 | 2173 | void __init init_sched_dl_class(void) |
1baca4ce JL |
2174 | { |
2175 | unsigned int i; | |
2176 | ||
2177 | for_each_possible_cpu(i) | |
2178 | zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i), | |
2179 | GFP_KERNEL, cpu_to_node(i)); | |
2180 | } | |
2181 | ||
2182 | #endif /* CONFIG_SMP */ | |
2183 | ||
aab03e05 DF |
2184 | static void switched_from_dl(struct rq *rq, struct task_struct *p) |
2185 | { | |
a649f237 | 2186 | /* |
209a0cbd LA |
2187 | * task_non_contending() can start the "inactive timer" (if the 0-lag |
2188 | * time is in the future). If the task switches back to dl before | |
2189 | * the "inactive timer" fires, it can continue to consume its current | |
2190 | * runtime using its current deadline. If it stays outside of | |
2191 | * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer() | |
2192 | * will reset the task parameters. | |
a649f237 | 2193 | */ |
209a0cbd LA |
2194 | if (task_on_rq_queued(p) && p->dl.dl_runtime) |
2195 | task_non_contending(p); | |
2196 | ||
8fd27231 LA |
2197 | if (!task_on_rq_queued(p)) |
2198 | sub_rq_bw(p->dl.dl_bw, &rq->dl); | |
2199 | ||
209a0cbd LA |
2200 | /* |
2201 | * We cannot use inactive_task_timer() to invoke sub_running_bw() | |
2202 | * at the 0-lag time, because the task could have been migrated | |
2203 | * while SCHED_OTHER in the meanwhile. | |
2204 | */ | |
2205 | if (p->dl.dl_non_contending) | |
2206 | p->dl.dl_non_contending = 0; | |
a5e7be3b | 2207 | |
1baca4ce JL |
2208 | /* |
2209 | * Since this might be the only -deadline task on the rq, | |
2210 | * this is the right place to try to pull some other one | |
2211 | * from an overloaded cpu, if any. | |
2212 | */ | |
cd660911 WL |
2213 | if (!task_on_rq_queued(p) || rq->dl.dl_nr_running) |
2214 | return; | |
2215 | ||
9916e214 | 2216 | queue_pull_task(rq); |
aab03e05 DF |
2217 | } |
2218 | ||
1baca4ce JL |
2219 | /* |
2220 | * When switching to -deadline, we may overload the rq, then | |
2221 | * we try to push someone off, if possible. | |
2222 | */ | |
aab03e05 DF |
2223 | static void switched_to_dl(struct rq *rq, struct task_struct *p) |
2224 | { | |
209a0cbd LA |
2225 | if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) |
2226 | put_task_struct(p); | |
98b0a857 JL |
2227 | |
2228 | /* If p is not queued we will update its parameters at next wakeup. */ | |
8fd27231 LA |
2229 | if (!task_on_rq_queued(p)) { |
2230 | add_rq_bw(p->dl.dl_bw, &rq->dl); | |
98b0a857 | 2231 | |
8fd27231 LA |
2232 | return; |
2233 | } | |
98b0a857 JL |
2234 | /* |
2235 | * If p is boosted we already updated its params in | |
2236 | * rt_mutex_setprio()->enqueue_task(..., ENQUEUE_REPLENISH), | |
2237 | * p's deadline being now already after rq_clock(rq). | |
2238 | */ | |
72f9f3fd | 2239 | if (dl_time_before(p->dl.deadline, rq_clock(rq))) |
98b0a857 | 2240 | setup_new_dl_entity(&p->dl); |
72f9f3fd | 2241 | |
98b0a857 | 2242 | if (rq->curr != p) { |
1baca4ce | 2243 | #ifdef CONFIG_SMP |
4b53a341 | 2244 | if (p->nr_cpus_allowed > 1 && rq->dl.overloaded) |
9916e214 | 2245 | queue_push_tasks(rq); |
619bd4a7 | 2246 | #endif |
9916e214 PZ |
2247 | if (dl_task(rq->curr)) |
2248 | check_preempt_curr_dl(rq, p, 0); | |
2249 | else | |
2250 | resched_curr(rq); | |
aab03e05 DF |
2251 | } |
2252 | } | |
2253 | ||
1baca4ce JL |
2254 | /* |
2255 | * If the scheduling parameters of a -deadline task changed, | |
2256 | * a push or pull operation might be needed. | |
2257 | */ | |
aab03e05 DF |
2258 | static void prio_changed_dl(struct rq *rq, struct task_struct *p, |
2259 | int oldprio) | |
2260 | { | |
da0c1e65 | 2261 | if (task_on_rq_queued(p) || rq->curr == p) { |
aab03e05 | 2262 | #ifdef CONFIG_SMP |
1baca4ce JL |
2263 | /* |
2264 | * This might be too much, but unfortunately | |
2265 | * we don't have the old deadline value, and | |
2266 | * we can't argue if the task is increasing | |
2267 | * or lowering its prio, so... | |
2268 | */ | |
2269 | if (!rq->dl.overloaded) | |
9916e214 | 2270 | queue_pull_task(rq); |
1baca4ce JL |
2271 | |
2272 | /* | |
2273 | * If we now have a earlier deadline task than p, | |
2274 | * then reschedule, provided p is still on this | |
2275 | * runqueue. | |
2276 | */ | |
9916e214 | 2277 | if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline)) |
8875125e | 2278 | resched_curr(rq); |
1baca4ce JL |
2279 | #else |
2280 | /* | |
2281 | * Again, we don't know if p has a earlier | |
2282 | * or later deadline, so let's blindly set a | |
2283 | * (maybe not needed) rescheduling point. | |
2284 | */ | |
8875125e | 2285 | resched_curr(rq); |
1baca4ce | 2286 | #endif /* CONFIG_SMP */ |
801ccdbf | 2287 | } |
aab03e05 | 2288 | } |
aab03e05 DF |
2289 | |
2290 | const struct sched_class dl_sched_class = { | |
2291 | .next = &rt_sched_class, | |
2292 | .enqueue_task = enqueue_task_dl, | |
2293 | .dequeue_task = dequeue_task_dl, | |
2294 | .yield_task = yield_task_dl, | |
2295 | ||
2296 | .check_preempt_curr = check_preempt_curr_dl, | |
2297 | ||
2298 | .pick_next_task = pick_next_task_dl, | |
2299 | .put_prev_task = put_prev_task_dl, | |
2300 | ||
2301 | #ifdef CONFIG_SMP | |
2302 | .select_task_rq = select_task_rq_dl, | |
209a0cbd | 2303 | .migrate_task_rq = migrate_task_rq_dl, |
1baca4ce JL |
2304 | .set_cpus_allowed = set_cpus_allowed_dl, |
2305 | .rq_online = rq_online_dl, | |
2306 | .rq_offline = rq_offline_dl, | |
1baca4ce | 2307 | .task_woken = task_woken_dl, |
aab03e05 DF |
2308 | #endif |
2309 | ||
2310 | .set_curr_task = set_curr_task_dl, | |
2311 | .task_tick = task_tick_dl, | |
2312 | .task_fork = task_fork_dl, | |
aab03e05 DF |
2313 | |
2314 | .prio_changed = prio_changed_dl, | |
2315 | .switched_from = switched_from_dl, | |
2316 | .switched_to = switched_to_dl, | |
6e998916 SG |
2317 | |
2318 | .update_curr = update_curr_dl, | |
aab03e05 | 2319 | }; |
acb32132 WL |
2320 | |
2321 | #ifdef CONFIG_SCHED_DEBUG | |
2322 | extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq); | |
2323 | ||
2324 | void print_dl_stats(struct seq_file *m, int cpu) | |
2325 | { | |
2326 | print_dl_rq(m, cpu, &cpu_rq(cpu)->dl); | |
2327 | } | |
2328 | #endif /* CONFIG_SCHED_DEBUG */ |