sched/deadline: Add SCHED_DEADLINE structures & implementation
[cascardo/linux.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  *                    Michael Trimarchi <michael@amarulasolutions.com>,
14  *                    Fabio Checconi <fchecconi@gmail.com>
15  */
16 #include "sched.h"
17
18 static inline int dl_time_before(u64 a, u64 b)
19 {
20         return (s64)(a - b) < 0;
21 }
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_rq(struct dl_rq *dl_rq, struct rq *rq)
54 {
55         dl_rq->rb_root = RB_ROOT;
56 }
57
58 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
59 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
60 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
61                                   int flags);
62
63 /*
64  * We are being explicitly informed that a new instance is starting,
65  * and this means that:
66  *  - the absolute deadline of the entity has to be placed at
67  *    current time + relative deadline;
68  *  - the runtime of the entity has to be set to the maximum value.
69  *
70  * The capability of specifying such event is useful whenever a -deadline
71  * entity wants to (try to!) synchronize its behaviour with the scheduler's
72  * one, and to (try to!) reconcile itself with its own scheduling
73  * parameters.
74  */
75 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
76 {
77         struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
78         struct rq *rq = rq_of_dl_rq(dl_rq);
79
80         WARN_ON(!dl_se->dl_new || dl_se->dl_throttled);
81
82         /*
83          * We use the regular wall clock time to set deadlines in the
84          * future; in fact, we must consider execution overheads (time
85          * spent on hardirq context, etc.).
86          */
87         dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
88         dl_se->runtime = dl_se->dl_runtime;
89         dl_se->dl_new = 0;
90 }
91
92 /*
93  * Pure Earliest Deadline First (EDF) scheduling does not deal with the
94  * possibility of a entity lasting more than what it declared, and thus
95  * exhausting its runtime.
96  *
97  * Here we are interested in making runtime overrun possible, but we do
98  * not want a entity which is misbehaving to affect the scheduling of all
99  * other entities.
100  * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
101  * is used, in order to confine each entity within its own bandwidth.
102  *
103  * This function deals exactly with that, and ensures that when the runtime
104  * of a entity is replenished, its deadline is also postponed. That ensures
105  * the overrunning entity can't interfere with other entity in the system and
106  * can't make them miss their deadlines. Reasons why this kind of overruns
107  * could happen are, typically, a entity voluntarily trying to overcome its
108  * runtime, or it just underestimated it during sched_setscheduler_ex().
109  */
110 static void replenish_dl_entity(struct sched_dl_entity *dl_se)
111 {
112         struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
113         struct rq *rq = rq_of_dl_rq(dl_rq);
114
115         /*
116          * We keep moving the deadline away until we get some
117          * available runtime for the entity. This ensures correct
118          * handling of situations where the runtime overrun is
119          * arbitrary large.
120          */
121         while (dl_se->runtime <= 0) {
122                 dl_se->deadline += dl_se->dl_deadline;
123                 dl_se->runtime += dl_se->dl_runtime;
124         }
125
126         /*
127          * At this point, the deadline really should be "in
128          * the future" with respect to rq->clock. If it's
129          * not, we are, for some reason, lagging too much!
130          * Anyway, after having warn userspace abut that,
131          * we still try to keep the things running by
132          * resetting the deadline and the budget of the
133          * entity.
134          */
135         if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
136                 static bool lag_once = false;
137
138                 if (!lag_once) {
139                         lag_once = true;
140                         printk_sched("sched: DL replenish lagged to much\n");
141                 }
142                 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
143                 dl_se->runtime = dl_se->dl_runtime;
144         }
145 }
146
147 /*
148  * Here we check if --at time t-- an entity (which is probably being
149  * [re]activated or, in general, enqueued) can use its remaining runtime
150  * and its current deadline _without_ exceeding the bandwidth it is
151  * assigned (function returns true if it can't). We are in fact applying
152  * one of the CBS rules: when a task wakes up, if the residual runtime
153  * over residual deadline fits within the allocated bandwidth, then we
154  * can keep the current (absolute) deadline and residual budget without
155  * disrupting the schedulability of the system. Otherwise, we should
156  * refill the runtime and set the deadline a period in the future,
157  * because keeping the current (absolute) deadline of the task would
158  * result in breaking guarantees promised to other tasks.
159  *
160  * This function returns true if:
161  *
162  *   runtime / (deadline - t) > dl_runtime / dl_deadline ,
163  *
164  * IOW we can't recycle current parameters.
165  */
166 static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
167 {
168         u64 left, right;
169
170         /*
171          * left and right are the two sides of the equation above,
172          * after a bit of shuffling to use multiplications instead
173          * of divisions.
174          *
175          * Note that none of the time values involved in the two
176          * multiplications are absolute: dl_deadline and dl_runtime
177          * are the relative deadline and the maximum runtime of each
178          * instance, runtime is the runtime left for the last instance
179          * and (deadline - t), since t is rq->clock, is the time left
180          * to the (absolute) deadline. Even if overflowing the u64 type
181          * is very unlikely to occur in both cases, here we scale down
182          * as we want to avoid that risk at all. Scaling down by 10
183          * means that we reduce granularity to 1us. We are fine with it,
184          * since this is only a true/false check and, anyway, thinking
185          * of anything below microseconds resolution is actually fiction
186          * (but still we want to give the user that illusion >;).
187          */
188         left = (dl_se->dl_deadline >> 10) * (dl_se->runtime >> 10);
189         right = ((dl_se->deadline - t) >> 10) * (dl_se->dl_runtime >> 10);
190
191         return dl_time_before(right, left);
192 }
193
194 /*
195  * When a -deadline entity is queued back on the runqueue, its runtime and
196  * deadline might need updating.
197  *
198  * The policy here is that we update the deadline of the entity only if:
199  *  - the current deadline is in the past,
200  *  - using the remaining runtime with the current deadline would make
201  *    the entity exceed its bandwidth.
202  */
203 static void update_dl_entity(struct sched_dl_entity *dl_se)
204 {
205         struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
206         struct rq *rq = rq_of_dl_rq(dl_rq);
207
208         /*
209          * The arrival of a new instance needs special treatment, i.e.,
210          * the actual scheduling parameters have to be "renewed".
211          */
212         if (dl_se->dl_new) {
213                 setup_new_dl_entity(dl_se);
214                 return;
215         }
216
217         if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
218             dl_entity_overflow(dl_se, rq_clock(rq))) {
219                 dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
220                 dl_se->runtime = dl_se->dl_runtime;
221         }
222 }
223
224 /*
225  * If the entity depleted all its runtime, and if we want it to sleep
226  * while waiting for some new execution time to become available, we
227  * set the bandwidth enforcement timer to the replenishment instant
228  * and try to activate it.
229  *
230  * Notice that it is important for the caller to know if the timer
231  * actually started or not (i.e., the replenishment instant is in
232  * the future or in the past).
233  */
234 static int start_dl_timer(struct sched_dl_entity *dl_se)
235 {
236         struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
237         struct rq *rq = rq_of_dl_rq(dl_rq);
238         ktime_t now, act;
239         ktime_t soft, hard;
240         unsigned long range;
241         s64 delta;
242
243         /*
244          * We want the timer to fire at the deadline, but considering
245          * that it is actually coming from rq->clock and not from
246          * hrtimer's time base reading.
247          */
248         act = ns_to_ktime(dl_se->deadline);
249         now = hrtimer_cb_get_time(&dl_se->dl_timer);
250         delta = ktime_to_ns(now) - rq_clock(rq);
251         act = ktime_add_ns(act, delta);
252
253         /*
254          * If the expiry time already passed, e.g., because the value
255          * chosen as the deadline is too small, don't even try to
256          * start the timer in the past!
257          */
258         if (ktime_us_delta(act, now) < 0)
259                 return 0;
260
261         hrtimer_set_expires(&dl_se->dl_timer, act);
262
263         soft = hrtimer_get_softexpires(&dl_se->dl_timer);
264         hard = hrtimer_get_expires(&dl_se->dl_timer);
265         range = ktime_to_ns(ktime_sub(hard, soft));
266         __hrtimer_start_range_ns(&dl_se->dl_timer, soft,
267                                  range, HRTIMER_MODE_ABS, 0);
268
269         return hrtimer_active(&dl_se->dl_timer);
270 }
271
272 /*
273  * This is the bandwidth enforcement timer callback. If here, we know
274  * a task is not on its dl_rq, since the fact that the timer was running
275  * means the task is throttled and needs a runtime replenishment.
276  *
277  * However, what we actually do depends on the fact the task is active,
278  * (it is on its rq) or has been removed from there by a call to
279  * dequeue_task_dl(). In the former case we must issue the runtime
280  * replenishment and add the task back to the dl_rq; in the latter, we just
281  * do nothing but clearing dl_throttled, so that runtime and deadline
282  * updating (and the queueing back to dl_rq) will be done by the
283  * next call to enqueue_task_dl().
284  */
285 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
286 {
287         struct sched_dl_entity *dl_se = container_of(timer,
288                                                      struct sched_dl_entity,
289                                                      dl_timer);
290         struct task_struct *p = dl_task_of(dl_se);
291         struct rq *rq = task_rq(p);
292         raw_spin_lock(&rq->lock);
293
294         /*
295          * We need to take care of a possible races here. In fact, the
296          * task might have changed its scheduling policy to something
297          * different from SCHED_DEADLINE or changed its reservation
298          * parameters (through sched_setscheduler()).
299          */
300         if (!dl_task(p) || dl_se->dl_new)
301                 goto unlock;
302
303         sched_clock_tick();
304         update_rq_clock(rq);
305         dl_se->dl_throttled = 0;
306         if (p->on_rq) {
307                 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
308                 if (task_has_dl_policy(rq->curr))
309                         check_preempt_curr_dl(rq, p, 0);
310                 else
311                         resched_task(rq->curr);
312         }
313 unlock:
314         raw_spin_unlock(&rq->lock);
315
316         return HRTIMER_NORESTART;
317 }
318
319 void init_dl_task_timer(struct sched_dl_entity *dl_se)
320 {
321         struct hrtimer *timer = &dl_se->dl_timer;
322
323         if (hrtimer_active(timer)) {
324                 hrtimer_try_to_cancel(timer);
325                 return;
326         }
327
328         hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
329         timer->function = dl_task_timer;
330 }
331
332 static
333 int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
334 {
335         int dmiss = dl_time_before(dl_se->deadline, rq_clock(rq));
336         int rorun = dl_se->runtime <= 0;
337
338         if (!rorun && !dmiss)
339                 return 0;
340
341         /*
342          * If we are beyond our current deadline and we are still
343          * executing, then we have already used some of the runtime of
344          * the next instance. Thus, if we do not account that, we are
345          * stealing bandwidth from the system at each deadline miss!
346          */
347         if (dmiss) {
348                 dl_se->runtime = rorun ? dl_se->runtime : 0;
349                 dl_se->runtime -= rq_clock(rq) - dl_se->deadline;
350         }
351
352         return 1;
353 }
354
355 /*
356  * Update the current task's runtime statistics (provided it is still
357  * a -deadline task and has not been removed from the dl_rq).
358  */
359 static void update_curr_dl(struct rq *rq)
360 {
361         struct task_struct *curr = rq->curr;
362         struct sched_dl_entity *dl_se = &curr->dl;
363         u64 delta_exec;
364
365         if (!dl_task(curr) || !on_dl_rq(dl_se))
366                 return;
367
368         /*
369          * Consumed budget is computed considering the time as
370          * observed by schedulable tasks (excluding time spent
371          * in hardirq context, etc.). Deadlines are instead
372          * computed using hard walltime. This seems to be the more
373          * natural solution, but the full ramifications of this
374          * approach need further study.
375          */
376         delta_exec = rq_clock_task(rq) - curr->se.exec_start;
377         if (unlikely((s64)delta_exec < 0))
378                 delta_exec = 0;
379
380         schedstat_set(curr->se.statistics.exec_max,
381                       max(curr->se.statistics.exec_max, delta_exec));
382
383         curr->se.sum_exec_runtime += delta_exec;
384         account_group_exec_runtime(curr, delta_exec);
385
386         curr->se.exec_start = rq_clock_task(rq);
387         cpuacct_charge(curr, delta_exec);
388
389         dl_se->runtime -= delta_exec;
390         if (dl_runtime_exceeded(rq, dl_se)) {
391                 __dequeue_task_dl(rq, curr, 0);
392                 if (likely(start_dl_timer(dl_se)))
393                         dl_se->dl_throttled = 1;
394                 else
395                         enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
396
397                 if (!is_leftmost(curr, &rq->dl))
398                         resched_task(curr);
399         }
400 }
401
402 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
403 {
404         struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
405         struct rb_node **link = &dl_rq->rb_root.rb_node;
406         struct rb_node *parent = NULL;
407         struct sched_dl_entity *entry;
408         int leftmost = 1;
409
410         BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
411
412         while (*link) {
413                 parent = *link;
414                 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
415                 if (dl_time_before(dl_se->deadline, entry->deadline))
416                         link = &parent->rb_left;
417                 else {
418                         link = &parent->rb_right;
419                         leftmost = 0;
420                 }
421         }
422
423         if (leftmost)
424                 dl_rq->rb_leftmost = &dl_se->rb_node;
425
426         rb_link_node(&dl_se->rb_node, parent, link);
427         rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
428
429         dl_rq->dl_nr_running++;
430 }
431
432 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
433 {
434         struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
435
436         if (RB_EMPTY_NODE(&dl_se->rb_node))
437                 return;
438
439         if (dl_rq->rb_leftmost == &dl_se->rb_node) {
440                 struct rb_node *next_node;
441
442                 next_node = rb_next(&dl_se->rb_node);
443                 dl_rq->rb_leftmost = next_node;
444         }
445
446         rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
447         RB_CLEAR_NODE(&dl_se->rb_node);
448
449         dl_rq->dl_nr_running--;
450 }
451
452 static void
453 enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
454 {
455         BUG_ON(on_dl_rq(dl_se));
456
457         /*
458          * If this is a wakeup or a new instance, the scheduling
459          * parameters of the task might need updating. Otherwise,
460          * we want a replenishment of its runtime.
461          */
462         if (!dl_se->dl_new && flags & ENQUEUE_REPLENISH)
463                 replenish_dl_entity(dl_se);
464         else
465                 update_dl_entity(dl_se);
466
467         __enqueue_dl_entity(dl_se);
468 }
469
470 static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
471 {
472         __dequeue_dl_entity(dl_se);
473 }
474
475 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
476 {
477         /*
478          * If p is throttled, we do nothing. In fact, if it exhausted
479          * its budget it needs a replenishment and, since it now is on
480          * its rq, the bandwidth timer callback (which clearly has not
481          * run yet) will take care of this.
482          */
483         if (p->dl.dl_throttled)
484                 return;
485
486         enqueue_dl_entity(&p->dl, flags);
487         inc_nr_running(rq);
488 }
489
490 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
491 {
492         dequeue_dl_entity(&p->dl);
493 }
494
495 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
496 {
497         update_curr_dl(rq);
498         __dequeue_task_dl(rq, p, flags);
499
500         dec_nr_running(rq);
501 }
502
503 /*
504  * Yield task semantic for -deadline tasks is:
505  *
506  *   get off from the CPU until our next instance, with
507  *   a new runtime. This is of little use now, since we
508  *   don't have a bandwidth reclaiming mechanism. Anyway,
509  *   bandwidth reclaiming is planned for the future, and
510  *   yield_task_dl will indicate that some spare budget
511  *   is available for other task instances to use it.
512  */
513 static void yield_task_dl(struct rq *rq)
514 {
515         struct task_struct *p = rq->curr;
516
517         /*
518          * We make the task go to sleep until its current deadline by
519          * forcing its runtime to zero. This way, update_curr_dl() stops
520          * it and the bandwidth timer will wake it up and will give it
521          * new scheduling parameters (thanks to dl_new=1).
522          */
523         if (p->dl.runtime > 0) {
524                 rq->curr->dl.dl_new = 1;
525                 p->dl.runtime = 0;
526         }
527         update_curr_dl(rq);
528 }
529
530 /*
531  * Only called when both the current and waking task are -deadline
532  * tasks.
533  */
534 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
535                                   int flags)
536 {
537         if (dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
538                 resched_task(rq->curr);
539 }
540
541 #ifdef CONFIG_SCHED_HRTICK
542 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
543 {
544         s64 delta = p->dl.dl_runtime - p->dl.runtime;
545
546         if (delta > 10000)
547                 hrtick_start(rq, p->dl.runtime);
548 }
549 #endif
550
551 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
552                                                    struct dl_rq *dl_rq)
553 {
554         struct rb_node *left = dl_rq->rb_leftmost;
555
556         if (!left)
557                 return NULL;
558
559         return rb_entry(left, struct sched_dl_entity, rb_node);
560 }
561
562 struct task_struct *pick_next_task_dl(struct rq *rq)
563 {
564         struct sched_dl_entity *dl_se;
565         struct task_struct *p;
566         struct dl_rq *dl_rq;
567
568         dl_rq = &rq->dl;
569
570         if (unlikely(!dl_rq->dl_nr_running))
571                 return NULL;
572
573         dl_se = pick_next_dl_entity(rq, dl_rq);
574         BUG_ON(!dl_se);
575
576         p = dl_task_of(dl_se);
577         p->se.exec_start = rq_clock_task(rq);
578 #ifdef CONFIG_SCHED_HRTICK
579         if (hrtick_enabled(rq))
580                 start_hrtick_dl(rq, p);
581 #endif
582         return p;
583 }
584
585 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
586 {
587         update_curr_dl(rq);
588 }
589
590 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
591 {
592         update_curr_dl(rq);
593
594 #ifdef CONFIG_SCHED_HRTICK
595         if (hrtick_enabled(rq) && queued && p->dl.runtime > 0)
596                 start_hrtick_dl(rq, p);
597 #endif
598 }
599
600 static void task_fork_dl(struct task_struct *p)
601 {
602         /*
603          * SCHED_DEADLINE tasks cannot fork and this is achieved through
604          * sched_fork()
605          */
606 }
607
608 static void task_dead_dl(struct task_struct *p)
609 {
610         struct hrtimer *timer = &p->dl.dl_timer;
611
612         if (hrtimer_active(timer))
613                 hrtimer_try_to_cancel(timer);
614 }
615
616 static void set_curr_task_dl(struct rq *rq)
617 {
618         struct task_struct *p = rq->curr;
619
620         p->se.exec_start = rq_clock_task(rq);
621 }
622
623 static void switched_from_dl(struct rq *rq, struct task_struct *p)
624 {
625         if (hrtimer_active(&p->dl.dl_timer))
626                 hrtimer_try_to_cancel(&p->dl.dl_timer);
627 }
628
629 static void switched_to_dl(struct rq *rq, struct task_struct *p)
630 {
631         /*
632          * If p is throttled, don't consider the possibility
633          * of preempting rq->curr, the check will be done right
634          * after its runtime will get replenished.
635          */
636         if (unlikely(p->dl.dl_throttled))
637                 return;
638
639         if (p->on_rq || rq->curr != p) {
640                 if (task_has_dl_policy(rq->curr))
641                         check_preempt_curr_dl(rq, p, 0);
642                 else
643                         resched_task(rq->curr);
644         }
645 }
646
647 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
648                             int oldprio)
649 {
650         switched_to_dl(rq, p);
651 }
652
653 #ifdef CONFIG_SMP
654 static int
655 select_task_rq_dl(struct task_struct *p, int prev_cpu, int sd_flag, int flags)
656 {
657         return task_cpu(p);
658 }
659 #endif
660
661 const struct sched_class dl_sched_class = {
662         .next                   = &rt_sched_class,
663         .enqueue_task           = enqueue_task_dl,
664         .dequeue_task           = dequeue_task_dl,
665         .yield_task             = yield_task_dl,
666
667         .check_preempt_curr     = check_preempt_curr_dl,
668
669         .pick_next_task         = pick_next_task_dl,
670         .put_prev_task          = put_prev_task_dl,
671
672 #ifdef CONFIG_SMP
673         .select_task_rq         = select_task_rq_dl,
674 #endif
675
676         .set_curr_task          = set_curr_task_dl,
677         .task_tick              = task_tick_dl,
678         .task_fork              = task_fork_dl,
679         .task_dead              = task_dead_dl,
680
681         .prio_changed           = prio_changed_dl,
682         .switched_from          = switched_from_dl,
683         .switched_to            = switched_to_dl,
684 };