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