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