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