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