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 | /* | |
707 | * When a -deadline entity is queued back on the runqueue, its runtime and | |
708 | * deadline might need updating. | |
709 | * | |
710 | * The policy here is that we update the deadline of the entity only if: | |
711 | * - the current deadline is in the past, | |
712 | * - using the remaining runtime with the current deadline would make | |
713 | * the entity exceed its bandwidth. | |
714 | */ | |
2d3d891d DF |
715 | static void update_dl_entity(struct sched_dl_entity *dl_se, |
716 | struct sched_dl_entity *pi_se) | |
aab03e05 DF |
717 | { |
718 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
719 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
720 | ||
aab03e05 | 721 | if (dl_time_before(dl_se->deadline, rq_clock(rq)) || |
2d3d891d DF |
722 | dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) { |
723 | dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline; | |
724 | dl_se->runtime = pi_se->dl_runtime; | |
aab03e05 DF |
725 | } |
726 | } | |
727 | ||
5ac69d37 DBO |
728 | static inline u64 dl_next_period(struct sched_dl_entity *dl_se) |
729 | { | |
730 | return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period; | |
731 | } | |
732 | ||
aab03e05 DF |
733 | /* |
734 | * If the entity depleted all its runtime, and if we want it to sleep | |
735 | * while waiting for some new execution time to become available, we | |
5ac69d37 | 736 | * set the bandwidth replenishment timer to the replenishment instant |
aab03e05 DF |
737 | * and try to activate it. |
738 | * | |
739 | * Notice that it is important for the caller to know if the timer | |
740 | * actually started or not (i.e., the replenishment instant is in | |
741 | * the future or in the past). | |
742 | */ | |
a649f237 | 743 | static int start_dl_timer(struct task_struct *p) |
aab03e05 | 744 | { |
a649f237 PZ |
745 | struct sched_dl_entity *dl_se = &p->dl; |
746 | struct hrtimer *timer = &dl_se->dl_timer; | |
747 | struct rq *rq = task_rq(p); | |
aab03e05 | 748 | ktime_t now, act; |
aab03e05 DF |
749 | s64 delta; |
750 | ||
a649f237 PZ |
751 | lockdep_assert_held(&rq->lock); |
752 | ||
aab03e05 DF |
753 | /* |
754 | * We want the timer to fire at the deadline, but considering | |
755 | * that it is actually coming from rq->clock and not from | |
756 | * hrtimer's time base reading. | |
757 | */ | |
5ac69d37 | 758 | act = ns_to_ktime(dl_next_period(dl_se)); |
a649f237 | 759 | now = hrtimer_cb_get_time(timer); |
aab03e05 DF |
760 | delta = ktime_to_ns(now) - rq_clock(rq); |
761 | act = ktime_add_ns(act, delta); | |
762 | ||
763 | /* | |
764 | * If the expiry time already passed, e.g., because the value | |
765 | * chosen as the deadline is too small, don't even try to | |
766 | * start the timer in the past! | |
767 | */ | |
768 | if (ktime_us_delta(act, now) < 0) | |
769 | return 0; | |
770 | ||
a649f237 PZ |
771 | /* |
772 | * !enqueued will guarantee another callback; even if one is already in | |
773 | * progress. This ensures a balanced {get,put}_task_struct(). | |
774 | * | |
775 | * The race against __run_timer() clearing the enqueued state is | |
776 | * harmless because we're holding task_rq()->lock, therefore the timer | |
777 | * expiring after we've done the check will wait on its task_rq_lock() | |
778 | * and observe our state. | |
779 | */ | |
780 | if (!hrtimer_is_queued(timer)) { | |
781 | get_task_struct(p); | |
782 | hrtimer_start(timer, act, HRTIMER_MODE_ABS); | |
783 | } | |
aab03e05 | 784 | |
cc9684d3 | 785 | return 1; |
aab03e05 DF |
786 | } |
787 | ||
788 | /* | |
789 | * This is the bandwidth enforcement timer callback. If here, we know | |
790 | * a task is not on its dl_rq, since the fact that the timer was running | |
791 | * means the task is throttled and needs a runtime replenishment. | |
792 | * | |
793 | * However, what we actually do depends on the fact the task is active, | |
794 | * (it is on its rq) or has been removed from there by a call to | |
795 | * dequeue_task_dl(). In the former case we must issue the runtime | |
796 | * replenishment and add the task back to the dl_rq; in the latter, we just | |
797 | * do nothing but clearing dl_throttled, so that runtime and deadline | |
798 | * updating (and the queueing back to dl_rq) will be done by the | |
799 | * next call to enqueue_task_dl(). | |
800 | */ | |
801 | static enum hrtimer_restart dl_task_timer(struct hrtimer *timer) | |
802 | { | |
803 | struct sched_dl_entity *dl_se = container_of(timer, | |
804 | struct sched_dl_entity, | |
805 | dl_timer); | |
806 | struct task_struct *p = dl_task_of(dl_se); | |
eb580751 | 807 | struct rq_flags rf; |
0f397f2c | 808 | struct rq *rq; |
3960c8c0 | 809 | |
eb580751 | 810 | rq = task_rq_lock(p, &rf); |
0f397f2c | 811 | |
aab03e05 | 812 | /* |
a649f237 | 813 | * The task might have changed its scheduling policy to something |
9846d50d | 814 | * different than SCHED_DEADLINE (through switched_from_dl()). |
a649f237 | 815 | */ |
209a0cbd | 816 | if (!dl_task(p)) |
a649f237 | 817 | goto unlock; |
a649f237 | 818 | |
a649f237 PZ |
819 | /* |
820 | * The task might have been boosted by someone else and might be in the | |
821 | * boosting/deboosting path, its not throttled. | |
822 | */ | |
823 | if (dl_se->dl_boosted) | |
824 | goto unlock; | |
a79ec89f | 825 | |
fa9c9d10 | 826 | /* |
a649f237 PZ |
827 | * Spurious timer due to start_dl_timer() race; or we already received |
828 | * a replenishment from rt_mutex_setprio(). | |
fa9c9d10 | 829 | */ |
a649f237 | 830 | if (!dl_se->dl_throttled) |
fa9c9d10 | 831 | goto unlock; |
a649f237 PZ |
832 | |
833 | sched_clock_tick(); | |
834 | update_rq_clock(rq); | |
fa9c9d10 | 835 | |
a79ec89f KT |
836 | /* |
837 | * If the throttle happened during sched-out; like: | |
838 | * | |
839 | * schedule() | |
840 | * deactivate_task() | |
841 | * dequeue_task_dl() | |
842 | * update_curr_dl() | |
843 | * start_dl_timer() | |
844 | * __dequeue_task_dl() | |
845 | * prev->on_rq = 0; | |
846 | * | |
847 | * We can be both throttled and !queued. Replenish the counter | |
848 | * but do not enqueue -- wait for our wakeup to do that. | |
849 | */ | |
850 | if (!task_on_rq_queued(p)) { | |
851 | replenish_dl_entity(dl_se, dl_se); | |
852 | goto unlock; | |
853 | } | |
854 | ||
1baca4ce | 855 | #ifdef CONFIG_SMP |
c0c8c9fa | 856 | if (unlikely(!rq->online)) { |
61c7aca6 WL |
857 | /* |
858 | * If the runqueue is no longer available, migrate the | |
859 | * task elsewhere. This necessarily changes rq. | |
860 | */ | |
c0c8c9fa | 861 | lockdep_unpin_lock(&rq->lock, rf.cookie); |
a649f237 | 862 | rq = dl_task_offline_migration(rq, p); |
c0c8c9fa | 863 | rf.cookie = lockdep_pin_lock(&rq->lock); |
dcc3b5ff | 864 | update_rq_clock(rq); |
61c7aca6 WL |
865 | |
866 | /* | |
867 | * Now that the task has been migrated to the new RQ and we | |
868 | * have that locked, proceed as normal and enqueue the task | |
869 | * there. | |
870 | */ | |
c0c8c9fa | 871 | } |
61c7aca6 | 872 | #endif |
a649f237 | 873 | |
61c7aca6 WL |
874 | enqueue_task_dl(rq, p, ENQUEUE_REPLENISH); |
875 | if (dl_task(rq->curr)) | |
876 | check_preempt_curr_dl(rq, p, 0); | |
877 | else | |
878 | resched_curr(rq); | |
a649f237 | 879 | |
61c7aca6 | 880 | #ifdef CONFIG_SMP |
a649f237 PZ |
881 | /* |
882 | * Queueing this task back might have overloaded rq, check if we need | |
883 | * to kick someone away. | |
1019a359 | 884 | */ |
0aaafaab PZ |
885 | if (has_pushable_dl_tasks(rq)) { |
886 | /* | |
887 | * Nothing relies on rq->lock after this, so its safe to drop | |
888 | * rq->lock. | |
889 | */ | |
d8ac8971 | 890 | rq_unpin_lock(rq, &rf); |
1019a359 | 891 | push_dl_task(rq); |
d8ac8971 | 892 | rq_repin_lock(rq, &rf); |
0aaafaab | 893 | } |
1baca4ce | 894 | #endif |
a649f237 | 895 | |
aab03e05 | 896 | unlock: |
eb580751 | 897 | task_rq_unlock(rq, p, &rf); |
aab03e05 | 898 | |
a649f237 PZ |
899 | /* |
900 | * This can free the task_struct, including this hrtimer, do not touch | |
901 | * anything related to that after this. | |
902 | */ | |
903 | put_task_struct(p); | |
904 | ||
aab03e05 DF |
905 | return HRTIMER_NORESTART; |
906 | } | |
907 | ||
908 | void init_dl_task_timer(struct sched_dl_entity *dl_se) | |
909 | { | |
910 | struct hrtimer *timer = &dl_se->dl_timer; | |
911 | ||
aab03e05 DF |
912 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
913 | timer->function = dl_task_timer; | |
914 | } | |
915 | ||
df8eac8c DBO |
916 | /* |
917 | * During the activation, CBS checks if it can reuse the current task's | |
918 | * runtime and period. If the deadline of the task is in the past, CBS | |
919 | * cannot use the runtime, and so it replenishes the task. This rule | |
920 | * works fine for implicit deadline tasks (deadline == period), and the | |
921 | * CBS was designed for implicit deadline tasks. However, a task with | |
922 | * constrained deadline (deadine < period) might be awakened after the | |
923 | * deadline, but before the next period. In this case, replenishing the | |
924 | * task would allow it to run for runtime / deadline. As in this case | |
925 | * deadline < period, CBS enables a task to run for more than the | |
926 | * runtime / period. In a very loaded system, this can cause a domino | |
927 | * effect, making other tasks miss their deadlines. | |
928 | * | |
929 | * To avoid this problem, in the activation of a constrained deadline | |
930 | * task after the deadline but before the next period, throttle the | |
931 | * task and set the replenishing timer to the begin of the next period, | |
932 | * unless it is boosted. | |
933 | */ | |
934 | static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se) | |
935 | { | |
936 | struct task_struct *p = dl_task_of(dl_se); | |
937 | struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se)); | |
938 | ||
939 | if (dl_time_before(dl_se->deadline, rq_clock(rq)) && | |
940 | dl_time_before(rq_clock(rq), dl_next_period(dl_se))) { | |
941 | if (unlikely(dl_se->dl_boosted || !start_dl_timer(p))) | |
942 | return; | |
943 | dl_se->dl_throttled = 1; | |
944 | } | |
945 | } | |
946 | ||
aab03e05 | 947 | static |
6fab5410 | 948 | int dl_runtime_exceeded(struct sched_dl_entity *dl_se) |
aab03e05 | 949 | { |
269ad801 | 950 | return (dl_se->runtime <= 0); |
aab03e05 DF |
951 | } |
952 | ||
faa59937 JL |
953 | extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq); |
954 | ||
c52f14d3 LA |
955 | /* |
956 | * This function implements the GRUB accounting rule: | |
957 | * according to the GRUB reclaiming algorithm, the runtime is | |
daec5798 LA |
958 | * not decreased as "dq = -dt", but as |
959 | * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt", | |
960 | * where u is the utilization of the task, Umax is the maximum reclaimable | |
961 | * utilization, Uinact is the (per-runqueue) inactive utilization, computed | |
962 | * as the difference between the "total runqueue utilization" and the | |
963 | * runqueue active utilization, and Uextra is the (per runqueue) extra | |
964 | * reclaimable utilization. | |
9f0d1a50 | 965 | * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations |
daec5798 LA |
966 | * multiplied by 2^BW_SHIFT, the result has to be shifted right by |
967 | * BW_SHIFT. | |
968 | * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT, | |
969 | * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT. | |
970 | * Since delta is a 64 bit variable, to have an overflow its value | |
971 | * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds. | |
972 | * So, overflow is not an issue here. | |
c52f14d3 | 973 | */ |
9f0d1a50 | 974 | u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se) |
c52f14d3 | 975 | { |
9f0d1a50 LA |
976 | u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */ |
977 | u64 u_act; | |
daec5798 | 978 | u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT; |
c52f14d3 | 979 | |
9f0d1a50 | 980 | /* |
daec5798 LA |
981 | * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)}, |
982 | * we compare u_inact + rq->dl.extra_bw with | |
983 | * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because | |
984 | * u_inact + rq->dl.extra_bw can be larger than | |
985 | * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative | |
986 | * leading to wrong results) | |
9f0d1a50 | 987 | */ |
daec5798 LA |
988 | if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min) |
989 | u_act = u_act_min; | |
9f0d1a50 | 990 | else |
daec5798 | 991 | u_act = BW_UNIT - u_inact - rq->dl.extra_bw; |
9f0d1a50 LA |
992 | |
993 | return (delta * u_act) >> BW_SHIFT; | |
c52f14d3 LA |
994 | } |
995 | ||
aab03e05 DF |
996 | /* |
997 | * Update the current task's runtime statistics (provided it is still | |
998 | * a -deadline task and has not been removed from the dl_rq). | |
999 | */ | |
1000 | static void update_curr_dl(struct rq *rq) | |
1001 | { | |
1002 | struct task_struct *curr = rq->curr; | |
1003 | struct sched_dl_entity *dl_se = &curr->dl; | |
1004 | u64 delta_exec; | |
1005 | ||
1006 | if (!dl_task(curr) || !on_dl_rq(dl_se)) | |
1007 | return; | |
1008 | ||
1009 | /* | |
1010 | * Consumed budget is computed considering the time as | |
1011 | * observed by schedulable tasks (excluding time spent | |
1012 | * in hardirq context, etc.). Deadlines are instead | |
1013 | * computed using hard walltime. This seems to be the more | |
1014 | * natural solution, but the full ramifications of this | |
1015 | * approach need further study. | |
1016 | */ | |
1017 | delta_exec = rq_clock_task(rq) - curr->se.exec_start; | |
48be3a67 PZ |
1018 | if (unlikely((s64)delta_exec <= 0)) { |
1019 | if (unlikely(dl_se->dl_yielded)) | |
1020 | goto throttle; | |
734ff2a7 | 1021 | return; |
48be3a67 | 1022 | } |
aab03e05 | 1023 | |
58919e83 | 1024 | /* kick cpufreq (see the comment in kernel/sched/sched.h). */ |
12bde33d | 1025 | cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_DL); |
594dd290 | 1026 | |
aab03e05 DF |
1027 | schedstat_set(curr->se.statistics.exec_max, |
1028 | max(curr->se.statistics.exec_max, delta_exec)); | |
1029 | ||
1030 | curr->se.sum_exec_runtime += delta_exec; | |
1031 | account_group_exec_runtime(curr, delta_exec); | |
1032 | ||
1033 | curr->se.exec_start = rq_clock_task(rq); | |
1034 | cpuacct_charge(curr, delta_exec); | |
1035 | ||
239be4a9 DF |
1036 | sched_rt_avg_update(rq, delta_exec); |
1037 | ||
2d4283e9 | 1038 | if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) |
9f0d1a50 | 1039 | delta_exec = grub_reclaim(delta_exec, rq, &curr->dl); |
48be3a67 PZ |
1040 | dl_se->runtime -= delta_exec; |
1041 | ||
1042 | throttle: | |
1043 | if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) { | |
1019a359 | 1044 | dl_se->dl_throttled = 1; |
aab03e05 | 1045 | __dequeue_task_dl(rq, curr, 0); |
a649f237 | 1046 | if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr))) |
aab03e05 DF |
1047 | enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH); |
1048 | ||
1049 | if (!is_leftmost(curr, &rq->dl)) | |
8875125e | 1050 | resched_curr(rq); |
aab03e05 | 1051 | } |
1724813d PZ |
1052 | |
1053 | /* | |
1054 | * Because -- for now -- we share the rt bandwidth, we need to | |
1055 | * account our runtime there too, otherwise actual rt tasks | |
1056 | * would be able to exceed the shared quota. | |
1057 | * | |
1058 | * Account to the root rt group for now. | |
1059 | * | |
1060 | * The solution we're working towards is having the RT groups scheduled | |
1061 | * using deadline servers -- however there's a few nasties to figure | |
1062 | * out before that can happen. | |
1063 | */ | |
1064 | if (rt_bandwidth_enabled()) { | |
1065 | struct rt_rq *rt_rq = &rq->rt; | |
1066 | ||
1067 | raw_spin_lock(&rt_rq->rt_runtime_lock); | |
1724813d PZ |
1068 | /* |
1069 | * We'll let actual RT tasks worry about the overflow here, we | |
faa59937 JL |
1070 | * have our own CBS to keep us inline; only account when RT |
1071 | * bandwidth is relevant. | |
1724813d | 1072 | */ |
faa59937 JL |
1073 | if (sched_rt_bandwidth_account(rt_rq)) |
1074 | rt_rq->rt_time += delta_exec; | |
1724813d PZ |
1075 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
1076 | } | |
aab03e05 DF |
1077 | } |
1078 | ||
209a0cbd LA |
1079 | static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer) |
1080 | { | |
1081 | struct sched_dl_entity *dl_se = container_of(timer, | |
1082 | struct sched_dl_entity, | |
1083 | inactive_timer); | |
1084 | struct task_struct *p = dl_task_of(dl_se); | |
1085 | struct rq_flags rf; | |
1086 | struct rq *rq; | |
1087 | ||
1088 | rq = task_rq_lock(p, &rf); | |
1089 | ||
1090 | if (!dl_task(p) || p->state == TASK_DEAD) { | |
387e3130 LA |
1091 | struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); |
1092 | ||
209a0cbd LA |
1093 | if (p->state == TASK_DEAD && dl_se->dl_non_contending) { |
1094 | sub_running_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl)); | |
8fd27231 | 1095 | sub_rq_bw(p->dl.dl_bw, dl_rq_of_se(&p->dl)); |
209a0cbd LA |
1096 | dl_se->dl_non_contending = 0; |
1097 | } | |
387e3130 LA |
1098 | |
1099 | raw_spin_lock(&dl_b->lock); | |
daec5798 | 1100 | __dl_clear(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); |
387e3130 | 1101 | raw_spin_unlock(&dl_b->lock); |
209a0cbd LA |
1102 | __dl_clear_params(p); |
1103 | ||
1104 | goto unlock; | |
1105 | } | |
1106 | if (dl_se->dl_non_contending == 0) | |
1107 | goto unlock; | |
1108 | ||
1109 | sched_clock_tick(); | |
1110 | update_rq_clock(rq); | |
1111 | ||
1112 | sub_running_bw(dl_se->dl_bw, &rq->dl); | |
1113 | dl_se->dl_non_contending = 0; | |
1114 | unlock: | |
1115 | task_rq_unlock(rq, p, &rf); | |
1116 | put_task_struct(p); | |
1117 | ||
1118 | return HRTIMER_NORESTART; | |
1119 | } | |
1120 | ||
1121 | void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se) | |
1122 | { | |
1123 | struct hrtimer *timer = &dl_se->inactive_timer; | |
1124 | ||
1125 | hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
1126 | timer->function = inactive_task_timer; | |
1127 | } | |
1128 | ||
1baca4ce JL |
1129 | #ifdef CONFIG_SMP |
1130 | ||
1baca4ce JL |
1131 | static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) |
1132 | { | |
1133 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
1134 | ||
1135 | if (dl_rq->earliest_dl.curr == 0 || | |
1136 | dl_time_before(deadline, dl_rq->earliest_dl.curr)) { | |
1baca4ce | 1137 | dl_rq->earliest_dl.curr = deadline; |
d8206bb3 | 1138 | cpudl_set(&rq->rd->cpudl, rq->cpu, deadline); |
1baca4ce JL |
1139 | } |
1140 | } | |
1141 | ||
1142 | static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) | |
1143 | { | |
1144 | struct rq *rq = rq_of_dl_rq(dl_rq); | |
1145 | ||
1146 | /* | |
1147 | * Since we may have removed our earliest (and/or next earliest) | |
1148 | * task we must recompute them. | |
1149 | */ | |
1150 | if (!dl_rq->dl_nr_running) { | |
1151 | dl_rq->earliest_dl.curr = 0; | |
1152 | dl_rq->earliest_dl.next = 0; | |
d8206bb3 | 1153 | cpudl_clear(&rq->rd->cpudl, rq->cpu); |
1baca4ce JL |
1154 | } else { |
1155 | struct rb_node *leftmost = dl_rq->rb_leftmost; | |
1156 | struct sched_dl_entity *entry; | |
1157 | ||
1158 | entry = rb_entry(leftmost, struct sched_dl_entity, rb_node); | |
1159 | dl_rq->earliest_dl.curr = entry->deadline; | |
d8206bb3 | 1160 | cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline); |
1baca4ce JL |
1161 | } |
1162 | } | |
1163 | ||
1164 | #else | |
1165 | ||
1166 | static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} | |
1167 | static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {} | |
1168 | ||
1169 | #endif /* CONFIG_SMP */ | |
1170 | ||
1171 | static inline | |
1172 | void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
1173 | { | |
1174 | int prio = dl_task_of(dl_se)->prio; | |
1175 | u64 deadline = dl_se->deadline; | |
1176 | ||
1177 | WARN_ON(!dl_prio(prio)); | |
1178 | dl_rq->dl_nr_running++; | |
72465447 | 1179 | add_nr_running(rq_of_dl_rq(dl_rq), 1); |
1baca4ce JL |
1180 | |
1181 | inc_dl_deadline(dl_rq, deadline); | |
1182 | inc_dl_migration(dl_se, dl_rq); | |
1183 | } | |
1184 | ||
1185 | static inline | |
1186 | void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq) | |
1187 | { | |
1188 | int prio = dl_task_of(dl_se)->prio; | |
1189 | ||
1190 | WARN_ON(!dl_prio(prio)); | |
1191 | WARN_ON(!dl_rq->dl_nr_running); | |
1192 | dl_rq->dl_nr_running--; | |
72465447 | 1193 | sub_nr_running(rq_of_dl_rq(dl_rq), 1); |
1baca4ce JL |
1194 | |
1195 | dec_dl_deadline(dl_rq, dl_se->deadline); | |
1196 | dec_dl_migration(dl_se, dl_rq); | |
1197 | } | |
1198 | ||
aab03e05 DF |
1199 | static void __enqueue_dl_entity(struct sched_dl_entity *dl_se) |
1200 | { | |
1201 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
1202 | struct rb_node **link = &dl_rq->rb_root.rb_node; | |
1203 | struct rb_node *parent = NULL; | |
1204 | struct sched_dl_entity *entry; | |
1205 | int leftmost = 1; | |
1206 | ||
1207 | BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node)); | |
1208 | ||
1209 | while (*link) { | |
1210 | parent = *link; | |
1211 | entry = rb_entry(parent, struct sched_dl_entity, rb_node); | |
1212 | if (dl_time_before(dl_se->deadline, entry->deadline)) | |
1213 | link = &parent->rb_left; | |
1214 | else { | |
1215 | link = &parent->rb_right; | |
1216 | leftmost = 0; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | if (leftmost) | |
1221 | dl_rq->rb_leftmost = &dl_se->rb_node; | |
1222 | ||
1223 | rb_link_node(&dl_se->rb_node, parent, link); | |
1224 | rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root); | |
1225 | ||
1baca4ce | 1226 | inc_dl_tasks(dl_se, dl_rq); |
aab03e05 DF |
1227 | } |
1228 | ||
1229 | static void __dequeue_dl_entity(struct sched_dl_entity *dl_se) | |
1230 | { | |
1231 | struct dl_rq *dl_rq = dl_rq_of_se(dl_se); | |
1232 | ||
1233 | if (RB_EMPTY_NODE(&dl_se->rb_node)) | |
1234 | return; | |
1235 | ||
1236 | if (dl_rq->rb_leftmost == &dl_se->rb_node) { | |
1237 | struct rb_node *next_node; | |
1238 | ||
1239 | next_node = rb_next(&dl_se->rb_node); | |
1240 | dl_rq->rb_leftmost = next_node; | |
1241 | } | |
1242 | ||
1243 | rb_erase(&dl_se->rb_node, &dl_rq->rb_root); | |
1244 | RB_CLEAR_NODE(&dl_se->rb_node); | |
1245 | ||
1baca4ce | 1246 | dec_dl_tasks(dl_se, dl_rq); |
aab03e05 DF |
1247 | } |
1248 | ||
1249 | static void | |
2d3d891d DF |
1250 | enqueue_dl_entity(struct sched_dl_entity *dl_se, |
1251 | struct sched_dl_entity *pi_se, int flags) | |
aab03e05 DF |
1252 | { |
1253 | BUG_ON(on_dl_rq(dl_se)); | |
1254 | ||
1255 | /* | |
1256 | * If this is a wakeup or a new instance, the scheduling | |
1257 | * parameters of the task might need updating. Otherwise, | |
1258 | * we want a replenishment of its runtime. | |
1259 | */ | |
e36d8677 | 1260 | if (flags & ENQUEUE_WAKEUP) { |
8fd27231 | 1261 | task_contending(dl_se, flags); |
2d3d891d | 1262 | update_dl_entity(dl_se, pi_se); |
e36d8677 | 1263 | } else if (flags & ENQUEUE_REPLENISH) { |
6a503c3b | 1264 | replenish_dl_entity(dl_se, pi_se); |
e36d8677 | 1265 | } |
aab03e05 DF |
1266 | |
1267 | __enqueue_dl_entity(dl_se); | |
1268 | } | |
1269 | ||
1270 | static void dequeue_dl_entity(struct sched_dl_entity *dl_se) | |
1271 | { | |
1272 | __dequeue_dl_entity(dl_se); | |
1273 | } | |
1274 | ||
df8eac8c DBO |
1275 | static inline bool dl_is_constrained(struct sched_dl_entity *dl_se) |
1276 | { | |
1277 | return dl_se->dl_deadline < dl_se->dl_period; | |
1278 | } | |
1279 | ||
aab03e05 DF |
1280 | static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags) |
1281 | { | |
2d3d891d DF |
1282 | struct task_struct *pi_task = rt_mutex_get_top_task(p); |
1283 | struct sched_dl_entity *pi_se = &p->dl; | |
1284 | ||
1285 | /* | |
1286 | * Use the scheduling parameters of the top pi-waiter | |
ff277d42 | 1287 | * task if we have one and its (absolute) deadline is |
2d3d891d DF |
1288 | * smaller than our one... OTW we keep our runtime and |
1289 | * deadline. | |
1290 | */ | |
64be6f1f | 1291 | if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) { |
2d3d891d | 1292 | pi_se = &pi_task->dl; |
64be6f1f JL |
1293 | } else if (!dl_prio(p->normal_prio)) { |
1294 | /* | |
1295 | * Special case in which we have a !SCHED_DEADLINE task | |
1296 | * that is going to be deboosted, but exceedes its | |
1297 | * runtime while doing so. No point in replenishing | |
1298 | * it, as it's going to return back to its original | |
1299 | * scheduling class after this. | |
1300 | */ | |
1301 | BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH); | |
1302 | return; | |
1303 | } | |
2d3d891d | 1304 | |
df8eac8c DBO |
1305 | /* |
1306 | * Check if a constrained deadline task was activated | |
1307 | * after the deadline but before the next period. | |
1308 | * If that is the case, the task will be throttled and | |
1309 | * the replenishment timer will be set to the next period. | |
1310 | */ | |
1311 | if (!p->dl.dl_throttled && dl_is_constrained(&p->dl)) | |
1312 | dl_check_constrained_dl(&p->dl); | |
1313 | ||
8fd27231 LA |
1314 | if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) { |
1315 | add_rq_bw(p->dl.dl_bw, &rq->dl); | |
e36d8677 | 1316 | add_running_bw(p->dl.dl_bw, &rq->dl); |
8fd27231 | 1317 | } |
e36d8677 | 1318 | |
aab03e05 | 1319 | /* |
e36d8677 | 1320 | * If p is throttled, we do not enqueue it. In fact, if it exhausted |
aab03e05 DF |
1321 | * its budget it needs a replenishment and, since it now is on |
1322 | * its rq, the bandwidth timer callback (which clearly has not | |
1323 | * run yet) will take care of this. | |
e36d8677 LA |
1324 | * However, the active utilization does not depend on the fact |
1325 | * that the task is on the runqueue or not (but depends on the | |
1326 | * task's state - in GRUB parlance, "inactive" vs "active contending"). | |
1327 | * In other words, even if a task is throttled its utilization must | |
1328 | * be counted in the active utilization; hence, we need to call | |
1329 | * add_running_bw(). | |
aab03e05 | 1330 | */ |
e36d8677 | 1331 | if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) { |
209a0cbd | 1332 | if (flags & ENQUEUE_WAKEUP) |
8fd27231 | 1333 | task_contending(&p->dl, flags); |
209a0cbd | 1334 | |
aab03e05 | 1335 | return; |
e36d8677 | 1336 | } |
aab03e05 | 1337 | |
2d3d891d | 1338 | enqueue_dl_entity(&p->dl, pi_se, flags); |
1baca4ce | 1339 | |
4b53a341 | 1340 | if (!task_current(rq, p) && p->nr_cpus_allowed > 1) |
1baca4ce | 1341 | enqueue_pushable_dl_task(rq, p); |
aab03e05 DF |
1342 | } |
1343 | ||
1344 | static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) | |
1345 | { | |
1346 | dequeue_dl_entity(&p->dl); | |
1baca4ce | 1347 | dequeue_pushable_dl_task(rq, p); |
aab03e05 DF |
1348 | } |
1349 | ||
1350 | static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags) | |
1351 | { | |
1352 | update_curr_dl(rq); | |
1353 | __dequeue_task_dl(rq, p, flags); | |
e36d8677 | 1354 | |
8fd27231 | 1355 | if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) { |
e36d8677 | 1356 | sub_running_bw(p->dl.dl_bw, &rq->dl); |
8fd27231 LA |
1357 | sub_rq_bw(p->dl.dl_bw, &rq->dl); |
1358 | } | |
e36d8677 LA |
1359 | |
1360 | /* | |
209a0cbd LA |
1361 | * This check allows to start the inactive timer (or to immediately |
1362 | * decrease the active utilization, if needed) in two cases: | |
e36d8677 LA |
1363 | * when the task blocks and when it is terminating |
1364 | * (p->state == TASK_DEAD). We can handle the two cases in the same | |
1365 | * way, because from GRUB's point of view the same thing is happening | |
1366 | * (the task moves from "active contending" to "active non contending" | |
1367 | * or "inactive") | |
1368 | */ | |
1369 | if (flags & DEQUEUE_SLEEP) | |
209a0cbd | 1370 | task_non_contending(p); |
aab03e05 DF |
1371 | } |
1372 | ||
1373 | /* | |
1374 | * Yield task semantic for -deadline tasks is: | |
1375 | * | |
1376 | * get off from the CPU until our next instance, with | |
1377 | * a new runtime. This is of little use now, since we | |
1378 | * don't have a bandwidth reclaiming mechanism. Anyway, | |
1379 | * bandwidth reclaiming is planned for the future, and | |
1380 | * yield_task_dl will indicate that some spare budget | |
1381 | * is available for other task instances to use it. | |
1382 | */ | |
1383 | static void yield_task_dl(struct rq *rq) | |
1384 | { | |
aab03e05 DF |
1385 | /* |
1386 | * We make the task go to sleep until its current deadline by | |
1387 | * forcing its runtime to zero. This way, update_curr_dl() stops | |
1388 | * it and the bandwidth timer will wake it up and will give it | |
5bfd126e | 1389 | * new scheduling parameters (thanks to dl_yielded=1). |
aab03e05 | 1390 | */ |
48be3a67 PZ |
1391 | rq->curr->dl.dl_yielded = 1; |
1392 | ||
6f1607f1 | 1393 | update_rq_clock(rq); |
aab03e05 | 1394 | update_curr_dl(rq); |
44fb085b WL |
1395 | /* |
1396 | * Tell update_rq_clock() that we've just updated, | |
1397 | * so we don't do microscopic update in schedule() | |
1398 | * and double the fastpath cost. | |
1399 | */ | |
1400 | rq_clock_skip_update(rq, true); | |
aab03e05 DF |
1401 | } |
1402 | ||
1baca4ce JL |
1403 | #ifdef CONFIG_SMP |
1404 | ||
1405 | static int find_later_rq(struct task_struct *task); | |
1baca4ce JL |
1406 | |
1407 | static int | |
1408 | select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags) | |
1409 | { | |
1410 | struct task_struct *curr; | |
1411 | struct rq *rq; | |
1412 | ||
1d7e974c | 1413 | if (sd_flag != SD_BALANCE_WAKE) |
1baca4ce JL |
1414 | goto out; |
1415 | ||
1416 | rq = cpu_rq(cpu); | |
1417 | ||
1418 | rcu_read_lock(); | |
316c1608 | 1419 | curr = READ_ONCE(rq->curr); /* unlocked access */ |
1baca4ce JL |
1420 | |
1421 | /* | |
1422 | * If we are dealing with a -deadline task, we must | |
1423 | * decide where to wake it up. | |
1424 | * If it has a later deadline and the current task | |
1425 | * on this rq can't move (provided the waking task | |
1426 | * can!) we prefer to send it somewhere else. On the | |
1427 | * other hand, if it has a shorter deadline, we | |
1428 | * try to make it stay here, it might be important. | |
1429 | */ | |
1430 | if (unlikely(dl_task(curr)) && | |
4b53a341 | 1431 | (curr->nr_cpus_allowed < 2 || |
1baca4ce | 1432 | !dl_entity_preempt(&p->dl, &curr->dl)) && |
4b53a341 | 1433 | (p->nr_cpus_allowed > 1)) { |
1baca4ce JL |
1434 | int target = find_later_rq(p); |
1435 | ||
9d514262 | 1436 | if (target != -1 && |
5aa50507 LA |
1437 | (dl_time_before(p->dl.deadline, |
1438 | cpu_rq(target)->dl.earliest_dl.curr) || | |
1439 | (cpu_rq(target)->dl.dl_nr_running == 0))) | |
1baca4ce JL |
1440 | cpu = target; |
1441 | } | |
1442 | rcu_read_unlock(); | |
1443 | ||
1444 | out: | |
1445 | return cpu; | |
1446 | } | |
1447 | ||
209a0cbd LA |
1448 | static void migrate_task_rq_dl(struct task_struct *p) |
1449 | { | |
1450 | struct rq *rq; | |
1451 | ||
8fd27231 | 1452 | if (p->state != TASK_WAKING) |
209a0cbd LA |
1453 | return; |
1454 | ||
1455 | rq = task_rq(p); | |
1456 | /* | |
1457 | * Since p->state == TASK_WAKING, set_task_cpu() has been called | |
1458 | * from try_to_wake_up(). Hence, p->pi_lock is locked, but | |
1459 | * rq->lock is not... So, lock it | |
1460 | */ | |
1461 | raw_spin_lock(&rq->lock); | |
8fd27231 LA |
1462 | if (p->dl.dl_non_contending) { |
1463 | sub_running_bw(p->dl.dl_bw, &rq->dl); | |
1464 | p->dl.dl_non_contending = 0; | |
1465 | /* | |
1466 | * If the timer handler is currently running and the | |
1467 | * timer cannot be cancelled, inactive_task_timer() | |
1468 | * will see that dl_not_contending is not set, and | |
1469 | * will not touch the rq's active utilization, | |
1470 | * so we are still safe. | |
1471 | */ | |
1472 | if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) | |
1473 | put_task_struct(p); | |
1474 | } | |
1475 | sub_rq_bw(p->dl.dl_bw, &rq->dl); | |
209a0cbd LA |
1476 | raw_spin_unlock(&rq->lock); |
1477 | } | |
1478 | ||
1baca4ce JL |
1479 | static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p) |
1480 | { | |
1481 | /* | |
1482 | * Current can't be migrated, useless to reschedule, | |
1483 | * let's hope p can move out. | |
1484 | */ | |
4b53a341 | 1485 | if (rq->curr->nr_cpus_allowed == 1 || |
6bfd6d72 | 1486 | cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1) |
1baca4ce JL |
1487 | return; |
1488 | ||
1489 | /* | |
1490 | * p is migratable, so let's not schedule it and | |
1491 | * see if it is pushed or pulled somewhere else. | |
1492 | */ | |
4b53a341 | 1493 | if (p->nr_cpus_allowed != 1 && |
6bfd6d72 | 1494 | cpudl_find(&rq->rd->cpudl, p, NULL) != -1) |
1baca4ce JL |
1495 | return; |
1496 | ||
8875125e | 1497 | resched_curr(rq); |
1baca4ce JL |
1498 | } |
1499 | ||
1500 | #endif /* CONFIG_SMP */ | |
1501 | ||
aab03e05 DF |
1502 | /* |
1503 | * Only called when both the current and waking task are -deadline | |
1504 | * tasks. | |
1505 | */ | |
1506 | static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, | |
1507 | int flags) | |
1508 | { | |
1baca4ce | 1509 | if (dl_entity_preempt(&p->dl, &rq->curr->dl)) { |
8875125e | 1510 | resched_curr(rq); |
1baca4ce JL |
1511 | return; |
1512 | } | |
1513 | ||
1514 | #ifdef CONFIG_SMP | |
1515 | /* | |
1516 | * In the unlikely case current and p have the same deadline | |
1517 | * let us try to decide what's the best thing to do... | |
1518 | */ | |
332ac17e DF |
1519 | if ((p->dl.deadline == rq->curr->dl.deadline) && |
1520 | !test_tsk_need_resched(rq->curr)) | |
1baca4ce JL |
1521 | check_preempt_equal_dl(rq, p); |
1522 | #endif /* CONFIG_SMP */ | |
aab03e05 DF |
1523 | } |
1524 | ||
1525 | #ifdef CONFIG_SCHED_HRTICK | |
1526 | static void start_hrtick_dl(struct rq *rq, struct task_struct *p) | |
1527 | { | |
177ef2a6 | 1528 | hrtick_start(rq, p->dl.runtime); |
aab03e05 | 1529 | } |
36ce9881 WL |
1530 | #else /* !CONFIG_SCHED_HRTICK */ |
1531 | static void start_hrtick_dl(struct rq *rq, struct task_struct *p) | |
1532 | { | |
1533 | } | |
aab03e05 DF |
1534 | #endif |
1535 | ||
1536 | static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq, | |
1537 | struct dl_rq *dl_rq) | |
1538 | { | |
1539 | struct rb_node *left = dl_rq->rb_leftmost; | |
1540 | ||
1541 | if (!left) | |
1542 | return NULL; | |
1543 | ||
1544 | return rb_entry(left, struct sched_dl_entity, rb_node); | |
1545 | } | |
1546 | ||
e7904a28 | 1547 | struct task_struct * |
d8ac8971 | 1548 | pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
aab03e05 DF |
1549 | { |
1550 | struct sched_dl_entity *dl_se; | |
1551 | struct task_struct *p; | |
1552 | struct dl_rq *dl_rq; | |
1553 | ||
1554 | dl_rq = &rq->dl; | |
1555 | ||
a1d9a323 | 1556 | if (need_pull_dl_task(rq, prev)) { |
cbce1a68 PZ |
1557 | /* |
1558 | * This is OK, because current is on_cpu, which avoids it being | |
1559 | * picked for load-balance and preemption/IRQs are still | |
1560 | * disabled avoiding further scheduler activity on it and we're | |
1561 | * being very careful to re-start the picking loop. | |
1562 | */ | |
d8ac8971 | 1563 | rq_unpin_lock(rq, rf); |
38033c37 | 1564 | pull_dl_task(rq); |
d8ac8971 | 1565 | rq_repin_lock(rq, rf); |
a1d9a323 | 1566 | /* |
176cedc4 | 1567 | * pull_dl_task() can drop (and re-acquire) rq->lock; this |
a1d9a323 KT |
1568 | * means a stop task can slip in, in which case we need to |
1569 | * re-start task selection. | |
1570 | */ | |
da0c1e65 | 1571 | if (rq->stop && task_on_rq_queued(rq->stop)) |
a1d9a323 KT |
1572 | return RETRY_TASK; |
1573 | } | |
1574 | ||
734ff2a7 KT |
1575 | /* |
1576 | * When prev is DL, we may throttle it in put_prev_task(). | |
1577 | * So, we update time before we check for dl_nr_running. | |
1578 | */ | |
1579 | if (prev->sched_class == &dl_sched_class) | |
1580 | update_curr_dl(rq); | |
38033c37 | 1581 | |
aab03e05 DF |
1582 | if (unlikely(!dl_rq->dl_nr_running)) |
1583 | return NULL; | |
1584 | ||
3f1d2a31 | 1585 | put_prev_task(rq, prev); |
606dba2e | 1586 | |
aab03e05 DF |
1587 | dl_se = pick_next_dl_entity(rq, dl_rq); |
1588 | BUG_ON(!dl_se); | |
1589 | ||
1590 | p = dl_task_of(dl_se); | |
1591 | p->se.exec_start = rq_clock_task(rq); | |
1baca4ce JL |
1592 | |
1593 | /* Running task will never be pushed. */ | |
71362650 | 1594 | dequeue_pushable_dl_task(rq, p); |
1baca4ce | 1595 | |
aab03e05 DF |
1596 | if (hrtick_enabled(rq)) |
1597 | start_hrtick_dl(rq, p); | |
1baca4ce | 1598 | |
e3fca9e7 | 1599 | queue_push_tasks(rq); |
1baca4ce | 1600 | |
aab03e05 DF |
1601 | return p; |
1602 | } | |
1603 | ||
1604 | static void put_prev_task_dl(struct rq *rq, struct task_struct *p) | |
1605 | { | |
1606 | update_curr_dl(rq); | |
1baca4ce | 1607 | |
4b53a341 | 1608 | if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1) |
1baca4ce | 1609 | enqueue_pushable_dl_task(rq, p); |
aab03e05 DF |
1610 | } |
1611 | ||
1612 | static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued) | |
1613 | { | |
1614 | update_curr_dl(rq); | |
1615 | ||
a7bebf48 WL |
1616 | /* |
1617 | * Even when we have runtime, update_curr_dl() might have resulted in us | |
1618 | * not being the leftmost task anymore. In that case NEED_RESCHED will | |
1619 | * be set and schedule() will start a new hrtick for the next task. | |
1620 | */ | |
1621 | if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 && | |
1622 | is_leftmost(p, &rq->dl)) | |
aab03e05 | 1623 | start_hrtick_dl(rq, p); |
aab03e05 DF |
1624 | } |
1625 | ||
1626 | static void task_fork_dl(struct task_struct *p) | |
1627 | { | |
1628 | /* | |
1629 | * SCHED_DEADLINE tasks cannot fork and this is achieved through | |
1630 | * sched_fork() | |
1631 | */ | |
1632 | } | |
1633 | ||
aab03e05 DF |
1634 | static void set_curr_task_dl(struct rq *rq) |
1635 | { | |
1636 | struct task_struct *p = rq->curr; | |
1637 | ||
1638 | p->se.exec_start = rq_clock_task(rq); | |
1baca4ce JL |
1639 | |
1640 | /* You can't push away the running task */ | |
1641 | dequeue_pushable_dl_task(rq, p); | |
1642 | } | |
1643 | ||
1644 | #ifdef CONFIG_SMP | |
1645 | ||
1646 | /* Only try algorithms three times */ | |
1647 | #define DL_MAX_TRIES 3 | |
1648 | ||
1649 | static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu) | |
1650 | { | |
1651 | if (!task_running(rq, p) && | |
0c98d344 | 1652 | cpumask_test_cpu(cpu, &p->cpus_allowed)) |
1baca4ce | 1653 | return 1; |
1baca4ce JL |
1654 | return 0; |
1655 | } | |
1656 | ||
8b5e770e WL |
1657 | /* |
1658 | * Return the earliest pushable rq's task, which is suitable to be executed | |
1659 | * on the CPU, NULL otherwise: | |
1660 | */ | |
1661 | static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu) | |
1662 | { | |
1663 | struct rb_node *next_node = rq->dl.pushable_dl_tasks_leftmost; | |
1664 | struct task_struct *p = NULL; | |
1665 | ||
1666 | if (!has_pushable_dl_tasks(rq)) | |
1667 | return NULL; | |
1668 | ||
1669 | next_node: | |
1670 | if (next_node) { | |
1671 | p = rb_entry(next_node, struct task_struct, pushable_dl_tasks); | |
1672 | ||
1673 | if (pick_dl_task(rq, p, cpu)) | |
1674 | return p; | |
1675 | ||
1676 | next_node = rb_next(next_node); | |
1677 | goto next_node; | |
1678 | } | |
1679 | ||
1680 | return NULL; | |
1681 | } | |
1682 | ||
1baca4ce JL |
1683 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl); |
1684 | ||
1685 | static int find_later_rq(struct task_struct *task) | |
1686 | { | |
1687 | struct sched_domain *sd; | |
4ba29684 | 1688 | struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl); |
1baca4ce JL |
1689 | int this_cpu = smp_processor_id(); |
1690 | int best_cpu, cpu = task_cpu(task); | |
1691 | ||
1692 | /* Make sure the mask is initialized first */ | |
1693 | if (unlikely(!later_mask)) | |
1694 | return -1; | |
1695 | ||
4b53a341 | 1696 | if (task->nr_cpus_allowed == 1) |
1baca4ce JL |
1697 | return -1; |
1698 | ||
91ec6778 JL |
1699 | /* |
1700 | * We have to consider system topology and task affinity | |
1701 | * first, then we can look for a suitable cpu. | |
1702 | */ | |
6bfd6d72 JL |
1703 | best_cpu = cpudl_find(&task_rq(task)->rd->cpudl, |
1704 | task, later_mask); | |
1baca4ce JL |
1705 | if (best_cpu == -1) |
1706 | return -1; | |
1707 | ||
1708 | /* | |
1709 | * If we are here, some target has been found, | |
1710 | * the most suitable of which is cached in best_cpu. | |
1711 | * This is, among the runqueues where the current tasks | |
1712 | * have later deadlines than the task's one, the rq | |
1713 | * with the latest possible one. | |
1714 | * | |
1715 | * Now we check how well this matches with task's | |
1716 | * affinity and system topology. | |
1717 | * | |
1718 | * The last cpu where the task run is our first | |
1719 | * guess, since it is most likely cache-hot there. | |
1720 | */ | |
1721 | if (cpumask_test_cpu(cpu, later_mask)) | |
1722 | return cpu; | |
1723 | /* | |
1724 | * Check if this_cpu is to be skipped (i.e., it is | |
1725 | * not in the mask) or not. | |
1726 | */ | |
1727 | if (!cpumask_test_cpu(this_cpu, later_mask)) | |
1728 | this_cpu = -1; | |
1729 | ||
1730 | rcu_read_lock(); | |
1731 | for_each_domain(cpu, sd) { | |
1732 | if (sd->flags & SD_WAKE_AFFINE) { | |
1733 | ||
1734 | /* | |
1735 | * If possible, preempting this_cpu is | |
1736 | * cheaper than migrating. | |
1737 | */ | |
1738 | if (this_cpu != -1 && | |
1739 | cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { | |
1740 | rcu_read_unlock(); | |
1741 | return this_cpu; | |
1742 | } | |
1743 | ||
1744 | /* | |
1745 | * Last chance: if best_cpu is valid and is | |
1746 | * in the mask, that becomes our choice. | |
1747 | */ | |
1748 | if (best_cpu < nr_cpu_ids && | |
1749 | cpumask_test_cpu(best_cpu, sched_domain_span(sd))) { | |
1750 | rcu_read_unlock(); | |
1751 | return best_cpu; | |
1752 | } | |
1753 | } | |
1754 | } | |
1755 | rcu_read_unlock(); | |
1756 | ||
1757 | /* | |
1758 | * At this point, all our guesses failed, we just return | |
1759 | * 'something', and let the caller sort the things out. | |
1760 | */ | |
1761 | if (this_cpu != -1) | |
1762 | return this_cpu; | |
1763 | ||
1764 | cpu = cpumask_any(later_mask); | |
1765 | if (cpu < nr_cpu_ids) | |
1766 | return cpu; | |
1767 | ||
1768 | return -1; | |
1769 | } | |
1770 | ||
1771 | /* Locks the rq it finds */ | |
1772 | static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq) | |
1773 | { | |
1774 | struct rq *later_rq = NULL; | |
1775 | int tries; | |
1776 | int cpu; | |
1777 | ||
1778 | for (tries = 0; tries < DL_MAX_TRIES; tries++) { | |
1779 | cpu = find_later_rq(task); | |
1780 | ||
1781 | if ((cpu == -1) || (cpu == rq->cpu)) | |
1782 | break; | |
1783 | ||
1784 | later_rq = cpu_rq(cpu); | |
1785 | ||
5aa50507 LA |
1786 | if (later_rq->dl.dl_nr_running && |
1787 | !dl_time_before(task->dl.deadline, | |
9d514262 WL |
1788 | later_rq->dl.earliest_dl.curr)) { |
1789 | /* | |
1790 | * Target rq has tasks of equal or earlier deadline, | |
1791 | * retrying does not release any lock and is unlikely | |
1792 | * to yield a different result. | |
1793 | */ | |
1794 | later_rq = NULL; | |
1795 | break; | |
1796 | } | |
1797 | ||
1baca4ce JL |
1798 | /* Retry if something changed. */ |
1799 | if (double_lock_balance(rq, later_rq)) { | |
1800 | if (unlikely(task_rq(task) != rq || | |
0c98d344 | 1801 | !cpumask_test_cpu(later_rq->cpu, &task->cpus_allowed) || |
da0c1e65 | 1802 | task_running(rq, task) || |
13b5ab02 | 1803 | !dl_task(task) || |
da0c1e65 | 1804 | !task_on_rq_queued(task))) { |
1baca4ce JL |
1805 | double_unlock_balance(rq, later_rq); |
1806 | later_rq = NULL; | |
1807 | break; | |
1808 | } | |
1809 | } | |
1810 | ||
1811 | /* | |
1812 | * If the rq we found has no -deadline task, or | |
1813 | * its earliest one has a later deadline than our | |
1814 | * task, the rq is a good one. | |
1815 | */ | |
1816 | if (!later_rq->dl.dl_nr_running || | |
1817 | dl_time_before(task->dl.deadline, | |
1818 | later_rq->dl.earliest_dl.curr)) | |
1819 | break; | |
1820 | ||
1821 | /* Otherwise we try again. */ | |
1822 | double_unlock_balance(rq, later_rq); | |
1823 | later_rq = NULL; | |
1824 | } | |
1825 | ||
1826 | return later_rq; | |
1827 | } | |
1828 | ||
1829 | static struct task_struct *pick_next_pushable_dl_task(struct rq *rq) | |
1830 | { | |
1831 | struct task_struct *p; | |
1832 | ||
1833 | if (!has_pushable_dl_tasks(rq)) | |
1834 | return NULL; | |
1835 | ||
1836 | p = rb_entry(rq->dl.pushable_dl_tasks_leftmost, | |
1837 | struct task_struct, pushable_dl_tasks); | |
1838 | ||
1839 | BUG_ON(rq->cpu != task_cpu(p)); | |
1840 | BUG_ON(task_current(rq, p)); | |
4b53a341 | 1841 | BUG_ON(p->nr_cpus_allowed <= 1); |
1baca4ce | 1842 | |
da0c1e65 | 1843 | BUG_ON(!task_on_rq_queued(p)); |
1baca4ce JL |
1844 | BUG_ON(!dl_task(p)); |
1845 | ||
1846 | return p; | |
1847 | } | |
1848 | ||
1849 | /* | |
1850 | * See if the non running -deadline tasks on this rq | |
1851 | * can be sent to some other CPU where they can preempt | |
1852 | * and start executing. | |
1853 | */ | |
1854 | static int push_dl_task(struct rq *rq) | |
1855 | { | |
1856 | struct task_struct *next_task; | |
1857 | struct rq *later_rq; | |
c51b8ab5 | 1858 | int ret = 0; |
1baca4ce JL |
1859 | |
1860 | if (!rq->dl.overloaded) | |
1861 | return 0; | |
1862 | ||
1863 | next_task = pick_next_pushable_dl_task(rq); | |
1864 | if (!next_task) | |
1865 | return 0; | |
1866 | ||
1867 | retry: | |
1868 | if (unlikely(next_task == rq->curr)) { | |
1869 | WARN_ON(1); | |
1870 | return 0; | |
1871 | } | |
1872 | ||
1873 | /* | |
1874 | * If next_task preempts rq->curr, and rq->curr | |
1875 | * can move away, it makes sense to just reschedule | |
1876 | * without going further in pushing next_task. | |
1877 | */ | |
1878 | if (dl_task(rq->curr) && | |
1879 | dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) && | |
4b53a341 | 1880 | rq->curr->nr_cpus_allowed > 1) { |
8875125e | 1881 | resched_curr(rq); |
1baca4ce JL |
1882 | return 0; |
1883 | } | |
1884 | ||
1885 | /* We might release rq lock */ | |
1886 | get_task_struct(next_task); | |
1887 | ||
1888 | /* Will lock the rq it'll find */ | |
1889 | later_rq = find_lock_later_rq(next_task, rq); | |
1890 | if (!later_rq) { | |
1891 | struct task_struct *task; | |
1892 | ||
1893 | /* | |
1894 | * We must check all this again, since | |
1895 | * find_lock_later_rq releases rq->lock and it is | |
1896 | * then possible that next_task has migrated. | |
1897 | */ | |
1898 | task = pick_next_pushable_dl_task(rq); | |
a776b968 | 1899 | if (task == next_task) { |
1baca4ce JL |
1900 | /* |
1901 | * The task is still there. We don't try | |
1902 | * again, some other cpu will pull it when ready. | |
1903 | */ | |
1baca4ce JL |
1904 | goto out; |
1905 | } | |
1906 | ||
1907 | if (!task) | |
1908 | /* No more tasks */ | |
1909 | goto out; | |
1910 | ||
1911 | put_task_struct(next_task); | |
1912 | next_task = task; | |
1913 | goto retry; | |
1914 | } | |
1915 | ||
1916 | deactivate_task(rq, next_task, 0); | |
e36d8677 | 1917 | sub_running_bw(next_task->dl.dl_bw, &rq->dl); |
8fd27231 | 1918 | sub_rq_bw(next_task->dl.dl_bw, &rq->dl); |
1baca4ce | 1919 | set_task_cpu(next_task, later_rq->cpu); |
8fd27231 | 1920 | add_rq_bw(next_task->dl.dl_bw, &later_rq->dl); |
e36d8677 | 1921 | add_running_bw(next_task->dl.dl_bw, &later_rq->dl); |
1baca4ce | 1922 | activate_task(later_rq, next_task, 0); |
c51b8ab5 | 1923 | ret = 1; |
1baca4ce | 1924 | |
8875125e | 1925 | resched_curr(later_rq); |
1baca4ce JL |
1926 | |
1927 | double_unlock_balance(rq, later_rq); | |
1928 | ||
1929 | out: | |
1930 | put_task_struct(next_task); | |
1931 | ||
c51b8ab5 | 1932 | return ret; |
1baca4ce JL |
1933 | } |
1934 | ||
1935 | static void push_dl_tasks(struct rq *rq) | |
1936 | { | |
4ffa08ed | 1937 | /* push_dl_task() will return true if it moved a -deadline task */ |
1baca4ce JL |
1938 | while (push_dl_task(rq)) |
1939 | ; | |
aab03e05 DF |
1940 | } |
1941 | ||
0ea60c20 | 1942 | static void pull_dl_task(struct rq *this_rq) |
1baca4ce | 1943 | { |
0ea60c20 | 1944 | int this_cpu = this_rq->cpu, cpu; |
1baca4ce | 1945 | struct task_struct *p; |
0ea60c20 | 1946 | bool resched = false; |
1baca4ce JL |
1947 | struct rq *src_rq; |
1948 | u64 dmin = LONG_MAX; | |
1949 | ||
1950 | if (likely(!dl_overloaded(this_rq))) | |
0ea60c20 | 1951 | return; |
1baca4ce JL |
1952 | |
1953 | /* | |
1954 | * Match the barrier from dl_set_overloaded; this guarantees that if we | |
1955 | * see overloaded we must also see the dlo_mask bit. | |
1956 | */ | |
1957 | smp_rmb(); | |
1958 | ||
1959 | for_each_cpu(cpu, this_rq->rd->dlo_mask) { | |
1960 | if (this_cpu == cpu) | |
1961 | continue; | |
1962 | ||
1963 | src_rq = cpu_rq(cpu); | |
1964 | ||
1965 | /* | |
1966 | * It looks racy, abd it is! However, as in sched_rt.c, | |
1967 | * we are fine with this. | |
1968 | */ | |
1969 | if (this_rq->dl.dl_nr_running && | |
1970 | dl_time_before(this_rq->dl.earliest_dl.curr, | |
1971 | src_rq->dl.earliest_dl.next)) | |
1972 | continue; | |
1973 | ||
1974 | /* Might drop this_rq->lock */ | |
1975 | double_lock_balance(this_rq, src_rq); | |
1976 | ||
1977 | /* | |
1978 | * If there are no more pullable tasks on the | |
1979 | * rq, we're done with it. | |
1980 | */ | |
1981 | if (src_rq->dl.dl_nr_running <= 1) | |
1982 | goto skip; | |
1983 | ||
8b5e770e | 1984 | p = pick_earliest_pushable_dl_task(src_rq, this_cpu); |
1baca4ce JL |
1985 | |
1986 | /* | |
1987 | * We found a task to be pulled if: | |
1988 | * - it preempts our current (if there's one), | |
1989 | * - it will preempt the last one we pulled (if any). | |
1990 | */ | |
1991 | if (p && dl_time_before(p->dl.deadline, dmin) && | |
1992 | (!this_rq->dl.dl_nr_running || | |
1993 | dl_time_before(p->dl.deadline, | |
1994 | this_rq->dl.earliest_dl.curr))) { | |
1995 | WARN_ON(p == src_rq->curr); | |
da0c1e65 | 1996 | WARN_ON(!task_on_rq_queued(p)); |
1baca4ce JL |
1997 | |
1998 | /* | |
1999 | * Then we pull iff p has actually an earlier | |
2000 | * deadline than the current task of its runqueue. | |
2001 | */ | |
2002 | if (dl_time_before(p->dl.deadline, | |
2003 | src_rq->curr->dl.deadline)) | |
2004 | goto skip; | |
2005 | ||
0ea60c20 | 2006 | resched = true; |
1baca4ce JL |
2007 | |
2008 | deactivate_task(src_rq, p, 0); | |
e36d8677 | 2009 | sub_running_bw(p->dl.dl_bw, &src_rq->dl); |
8fd27231 | 2010 | sub_rq_bw(p->dl.dl_bw, &src_rq->dl); |
1baca4ce | 2011 | set_task_cpu(p, this_cpu); |
8fd27231 | 2012 | add_rq_bw(p->dl.dl_bw, &this_rq->dl); |
e36d8677 | 2013 | add_running_bw(p->dl.dl_bw, &this_rq->dl); |
1baca4ce JL |
2014 | activate_task(this_rq, p, 0); |
2015 | dmin = p->dl.deadline; | |
2016 | ||
2017 | /* Is there any other task even earlier? */ | |
2018 | } | |
2019 | skip: | |
2020 | double_unlock_balance(this_rq, src_rq); | |
2021 | } | |
2022 | ||
0ea60c20 PZ |
2023 | if (resched) |
2024 | resched_curr(this_rq); | |
1baca4ce JL |
2025 | } |
2026 | ||
2027 | /* | |
2028 | * Since the task is not running and a reschedule is not going to happen | |
2029 | * anytime soon on its runqueue, we try pushing it away now. | |
2030 | */ | |
2031 | static void task_woken_dl(struct rq *rq, struct task_struct *p) | |
2032 | { | |
2033 | if (!task_running(rq, p) && | |
2034 | !test_tsk_need_resched(rq->curr) && | |
4b53a341 | 2035 | p->nr_cpus_allowed > 1 && |
1baca4ce | 2036 | dl_task(rq->curr) && |
4b53a341 | 2037 | (rq->curr->nr_cpus_allowed < 2 || |
6b0a563f | 2038 | !dl_entity_preempt(&p->dl, &rq->curr->dl))) { |
1baca4ce JL |
2039 | push_dl_tasks(rq); |
2040 | } | |
2041 | } | |
2042 | ||
2043 | static void set_cpus_allowed_dl(struct task_struct *p, | |
2044 | const struct cpumask *new_mask) | |
2045 | { | |
7f51412a | 2046 | struct root_domain *src_rd; |
6c37067e | 2047 | struct rq *rq; |
1baca4ce JL |
2048 | |
2049 | BUG_ON(!dl_task(p)); | |
2050 | ||
7f51412a JL |
2051 | rq = task_rq(p); |
2052 | src_rd = rq->rd; | |
2053 | /* | |
2054 | * Migrating a SCHED_DEADLINE task between exclusive | |
2055 | * cpusets (different root_domains) entails a bandwidth | |
2056 | * update. We already made space for us in the destination | |
2057 | * domain (see cpuset_can_attach()). | |
2058 | */ | |
2059 | if (!cpumask_intersects(src_rd->span, new_mask)) { | |
2060 | struct dl_bw *src_dl_b; | |
2061 | ||
2062 | src_dl_b = dl_bw_of(cpu_of(rq)); | |
2063 | /* | |
2064 | * We now free resources of the root_domain we are migrating | |
2065 | * off. In the worst case, sched_setattr() may temporary fail | |
2066 | * until we complete the update. | |
2067 | */ | |
2068 | raw_spin_lock(&src_dl_b->lock); | |
daec5798 | 2069 | __dl_clear(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p))); |
7f51412a JL |
2070 | raw_spin_unlock(&src_dl_b->lock); |
2071 | } | |
2072 | ||
6c37067e | 2073 | set_cpus_allowed_common(p, new_mask); |
1baca4ce JL |
2074 | } |
2075 | ||
2076 | /* Assumes rq->lock is held */ | |
2077 | static void rq_online_dl(struct rq *rq) | |
2078 | { | |
2079 | if (rq->dl.overloaded) | |
2080 | dl_set_overload(rq); | |
6bfd6d72 | 2081 | |
16b26943 | 2082 | cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu); |
6bfd6d72 | 2083 | if (rq->dl.dl_nr_running > 0) |
d8206bb3 | 2084 | cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr); |
1baca4ce JL |
2085 | } |
2086 | ||
2087 | /* Assumes rq->lock is held */ | |
2088 | static void rq_offline_dl(struct rq *rq) | |
2089 | { | |
2090 | if (rq->dl.overloaded) | |
2091 | dl_clear_overload(rq); | |
6bfd6d72 | 2092 | |
d8206bb3 | 2093 | cpudl_clear(&rq->rd->cpudl, rq->cpu); |
16b26943 | 2094 | cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu); |
1baca4ce JL |
2095 | } |
2096 | ||
a6c0e746 | 2097 | void __init init_sched_dl_class(void) |
1baca4ce JL |
2098 | { |
2099 | unsigned int i; | |
2100 | ||
2101 | for_each_possible_cpu(i) | |
2102 | zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i), | |
2103 | GFP_KERNEL, cpu_to_node(i)); | |
2104 | } | |
2105 | ||
2106 | #endif /* CONFIG_SMP */ | |
2107 | ||
aab03e05 DF |
2108 | static void switched_from_dl(struct rq *rq, struct task_struct *p) |
2109 | { | |
a649f237 | 2110 | /* |
209a0cbd LA |
2111 | * task_non_contending() can start the "inactive timer" (if the 0-lag |
2112 | * time is in the future). If the task switches back to dl before | |
2113 | * the "inactive timer" fires, it can continue to consume its current | |
2114 | * runtime using its current deadline. If it stays outside of | |
2115 | * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer() | |
2116 | * will reset the task parameters. | |
a649f237 | 2117 | */ |
209a0cbd LA |
2118 | if (task_on_rq_queued(p) && p->dl.dl_runtime) |
2119 | task_non_contending(p); | |
2120 | ||
8fd27231 LA |
2121 | if (!task_on_rq_queued(p)) |
2122 | sub_rq_bw(p->dl.dl_bw, &rq->dl); | |
2123 | ||
209a0cbd LA |
2124 | /* |
2125 | * We cannot use inactive_task_timer() to invoke sub_running_bw() | |
2126 | * at the 0-lag time, because the task could have been migrated | |
2127 | * while SCHED_OTHER in the meanwhile. | |
2128 | */ | |
2129 | if (p->dl.dl_non_contending) | |
2130 | p->dl.dl_non_contending = 0; | |
a5e7be3b | 2131 | |
1baca4ce JL |
2132 | /* |
2133 | * Since this might be the only -deadline task on the rq, | |
2134 | * this is the right place to try to pull some other one | |
2135 | * from an overloaded cpu, if any. | |
2136 | */ | |
cd660911 WL |
2137 | if (!task_on_rq_queued(p) || rq->dl.dl_nr_running) |
2138 | return; | |
2139 | ||
9916e214 | 2140 | queue_pull_task(rq); |
aab03e05 DF |
2141 | } |
2142 | ||
1baca4ce JL |
2143 | /* |
2144 | * When switching to -deadline, we may overload the rq, then | |
2145 | * we try to push someone off, if possible. | |
2146 | */ | |
aab03e05 DF |
2147 | static void switched_to_dl(struct rq *rq, struct task_struct *p) |
2148 | { | |
209a0cbd LA |
2149 | if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1) |
2150 | put_task_struct(p); | |
98b0a857 JL |
2151 | |
2152 | /* If p is not queued we will update its parameters at next wakeup. */ | |
8fd27231 LA |
2153 | if (!task_on_rq_queued(p)) { |
2154 | add_rq_bw(p->dl.dl_bw, &rq->dl); | |
98b0a857 | 2155 | |
8fd27231 LA |
2156 | return; |
2157 | } | |
98b0a857 JL |
2158 | /* |
2159 | * If p is boosted we already updated its params in | |
2160 | * rt_mutex_setprio()->enqueue_task(..., ENQUEUE_REPLENISH), | |
2161 | * p's deadline being now already after rq_clock(rq). | |
2162 | */ | |
72f9f3fd | 2163 | if (dl_time_before(p->dl.deadline, rq_clock(rq))) |
98b0a857 | 2164 | setup_new_dl_entity(&p->dl); |
72f9f3fd | 2165 | |
98b0a857 | 2166 | if (rq->curr != p) { |
1baca4ce | 2167 | #ifdef CONFIG_SMP |
4b53a341 | 2168 | if (p->nr_cpus_allowed > 1 && rq->dl.overloaded) |
9916e214 | 2169 | queue_push_tasks(rq); |
619bd4a7 | 2170 | #endif |
9916e214 PZ |
2171 | if (dl_task(rq->curr)) |
2172 | check_preempt_curr_dl(rq, p, 0); | |
2173 | else | |
2174 | resched_curr(rq); | |
aab03e05 DF |
2175 | } |
2176 | } | |
2177 | ||
1baca4ce JL |
2178 | /* |
2179 | * If the scheduling parameters of a -deadline task changed, | |
2180 | * a push or pull operation might be needed. | |
2181 | */ | |
aab03e05 DF |
2182 | static void prio_changed_dl(struct rq *rq, struct task_struct *p, |
2183 | int oldprio) | |
2184 | { | |
da0c1e65 | 2185 | if (task_on_rq_queued(p) || rq->curr == p) { |
aab03e05 | 2186 | #ifdef CONFIG_SMP |
1baca4ce JL |
2187 | /* |
2188 | * This might be too much, but unfortunately | |
2189 | * we don't have the old deadline value, and | |
2190 | * we can't argue if the task is increasing | |
2191 | * or lowering its prio, so... | |
2192 | */ | |
2193 | if (!rq->dl.overloaded) | |
9916e214 | 2194 | queue_pull_task(rq); |
1baca4ce JL |
2195 | |
2196 | /* | |
2197 | * If we now have a earlier deadline task than p, | |
2198 | * then reschedule, provided p is still on this | |
2199 | * runqueue. | |
2200 | */ | |
9916e214 | 2201 | if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline)) |
8875125e | 2202 | resched_curr(rq); |
1baca4ce JL |
2203 | #else |
2204 | /* | |
2205 | * Again, we don't know if p has a earlier | |
2206 | * or later deadline, so let's blindly set a | |
2207 | * (maybe not needed) rescheduling point. | |
2208 | */ | |
8875125e | 2209 | resched_curr(rq); |
1baca4ce | 2210 | #endif /* CONFIG_SMP */ |
801ccdbf | 2211 | } |
aab03e05 | 2212 | } |
aab03e05 DF |
2213 | |
2214 | const struct sched_class dl_sched_class = { | |
2215 | .next = &rt_sched_class, | |
2216 | .enqueue_task = enqueue_task_dl, | |
2217 | .dequeue_task = dequeue_task_dl, | |
2218 | .yield_task = yield_task_dl, | |
2219 | ||
2220 | .check_preempt_curr = check_preempt_curr_dl, | |
2221 | ||
2222 | .pick_next_task = pick_next_task_dl, | |
2223 | .put_prev_task = put_prev_task_dl, | |
2224 | ||
2225 | #ifdef CONFIG_SMP | |
2226 | .select_task_rq = select_task_rq_dl, | |
209a0cbd | 2227 | .migrate_task_rq = migrate_task_rq_dl, |
1baca4ce JL |
2228 | .set_cpus_allowed = set_cpus_allowed_dl, |
2229 | .rq_online = rq_online_dl, | |
2230 | .rq_offline = rq_offline_dl, | |
1baca4ce | 2231 | .task_woken = task_woken_dl, |
aab03e05 DF |
2232 | #endif |
2233 | ||
2234 | .set_curr_task = set_curr_task_dl, | |
2235 | .task_tick = task_tick_dl, | |
2236 | .task_fork = task_fork_dl, | |
aab03e05 DF |
2237 | |
2238 | .prio_changed = prio_changed_dl, | |
2239 | .switched_from = switched_from_dl, | |
2240 | .switched_to = switched_to_dl, | |
6e998916 SG |
2241 | |
2242 | .update_curr = update_curr_dl, | |
aab03e05 | 2243 | }; |
acb32132 WL |
2244 | |
2245 | #ifdef CONFIG_SCHED_DEBUG | |
2246 | extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq); | |
2247 | ||
2248 | void print_dl_stats(struct seq_file *m, int cpu) | |
2249 | { | |
2250 | print_dl_rq(m, cpu, &cpu_rq(cpu)->dl); | |
2251 | } | |
2252 | #endif /* CONFIG_SCHED_DEBUG */ |