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