Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / kernel / sched / deadline.c
blob0dd5e0971a0778a3e09ee8a91f5851dfa1dc25a9
1 /*
2 * Deadline Scheduling Class (SCHED_DEADLINE)
4 * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
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.
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>
17 #include "sched.h"
19 #include <linux/slab.h>
21 struct dl_bandwidth def_dl_bandwidth;
23 static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
25 return container_of(dl_se, struct task_struct, dl);
28 static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
30 return container_of(dl_rq, struct rq, dl);
33 static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
35 struct task_struct *p = dl_task_of(dl_se);
36 struct rq *rq = task_rq(p);
38 return &rq->dl;
41 static inline int on_dl_rq(struct sched_dl_entity *dl_se)
43 return !RB_EMPTY_NODE(&dl_se->rb_node);
46 static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
48 struct sched_dl_entity *dl_se = &p->dl;
50 return dl_rq->rb_leftmost == &dl_se->rb_node;
53 void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
55 raw_spin_lock_init(&dl_b->dl_runtime_lock);
56 dl_b->dl_period = period;
57 dl_b->dl_runtime = runtime;
60 extern unsigned long to_ratio(u64 period, u64 runtime);
62 void init_dl_bw(struct dl_bw *dl_b)
64 raw_spin_lock_init(&dl_b->lock);
65 raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
66 if (global_rt_runtime() == RUNTIME_INF)
67 dl_b->bw = -1;
68 else
69 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
70 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
71 dl_b->total_bw = 0;
74 void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq)
76 dl_rq->rb_root = RB_ROOT;
78 #ifdef CONFIG_SMP
79 /* zero means no -deadline tasks */
80 dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
82 dl_rq->dl_nr_migratory = 0;
83 dl_rq->overloaded = 0;
84 dl_rq->pushable_dl_tasks_root = RB_ROOT;
85 #else
86 init_dl_bw(&dl_rq->dl_bw);
87 #endif
90 #ifdef CONFIG_SMP
92 static inline int dl_overloaded(struct rq *rq)
94 return atomic_read(&rq->rd->dlo_count);
97 static inline void dl_set_overload(struct rq *rq)
99 if (!rq->online)
100 return;
102 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
104 * Must be visible before the overload count is
105 * set (as in sched_rt.c).
107 * Matched by the barrier in pull_dl_task().
109 smp_wmb();
110 atomic_inc(&rq->rd->dlo_count);
113 static inline void dl_clear_overload(struct rq *rq)
115 if (!rq->online)
116 return;
118 atomic_dec(&rq->rd->dlo_count);
119 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
122 static void update_dl_migration(struct dl_rq *dl_rq)
124 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_total > 1) {
125 if (!dl_rq->overloaded) {
126 dl_set_overload(rq_of_dl_rq(dl_rq));
127 dl_rq->overloaded = 1;
129 } else if (dl_rq->overloaded) {
130 dl_clear_overload(rq_of_dl_rq(dl_rq));
131 dl_rq->overloaded = 0;
135 static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
137 struct task_struct *p = dl_task_of(dl_se);
138 dl_rq = &rq_of_dl_rq(dl_rq)->dl;
140 dl_rq->dl_nr_total++;
141 if (p->nr_cpus_allowed > 1)
142 dl_rq->dl_nr_migratory++;
144 update_dl_migration(dl_rq);
147 static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
149 struct task_struct *p = dl_task_of(dl_se);
150 dl_rq = &rq_of_dl_rq(dl_rq)->dl;
152 dl_rq->dl_nr_total--;
153 if (p->nr_cpus_allowed > 1)
154 dl_rq->dl_nr_migratory--;
156 update_dl_migration(dl_rq);
160 * The list of pushable -deadline task is not a plist, like in
161 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
163 static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
165 struct dl_rq *dl_rq = &rq->dl;
166 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
167 struct rb_node *parent = NULL;
168 struct task_struct *entry;
169 int leftmost = 1;
171 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
173 while (*link) {
174 parent = *link;
175 entry = rb_entry(parent, struct task_struct,
176 pushable_dl_tasks);
177 if (dl_entity_preempt(&p->dl, &entry->dl))
178 link = &parent->rb_left;
179 else {
180 link = &parent->rb_right;
181 leftmost = 0;
185 if (leftmost)
186 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
188 rb_link_node(&p->pushable_dl_tasks, parent, link);
189 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
192 static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
194 struct dl_rq *dl_rq = &rq->dl;
196 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
197 return;
199 if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
200 struct rb_node *next_node;
202 next_node = rb_next(&p->pushable_dl_tasks);
203 dl_rq->pushable_dl_tasks_leftmost = next_node;
206 rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
207 RB_CLEAR_NODE(&p->pushable_dl_tasks);
210 static inline int has_pushable_dl_tasks(struct rq *rq)
212 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
215 static int push_dl_task(struct rq *rq);
217 #else
219 static inline
220 void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
224 static inline
225 void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
229 static inline
230 void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
234 static inline
235 void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
239 #endif /* CONFIG_SMP */
241 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
242 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
243 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
244 int flags);
247 * We are being explicitly informed that a new instance is starting,
248 * and this means that:
249 * - the absolute deadline of the entity has to be placed at
250 * current time + relative deadline;
251 * - the runtime of the entity has to be set to the maximum value.
253 * The capability of specifying such event is useful whenever a -deadline
254 * entity wants to (try to!) synchronize its behaviour with the scheduler's
255 * one, and to (try to!) reconcile itself with its own scheduling
256 * parameters.
258 static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se,
259 struct sched_dl_entity *pi_se)
261 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
262 struct rq *rq = rq_of_dl_rq(dl_rq);
264 WARN_ON(!dl_se->dl_new || dl_se->dl_throttled);
267 * We use the regular wall clock time to set deadlines in the
268 * future; in fact, we must consider execution overheads (time
269 * spent on hardirq context, etc.).
271 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
272 dl_se->runtime = pi_se->dl_runtime;
273 dl_se->dl_new = 0;
277 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
278 * possibility of a entity lasting more than what it declared, and thus
279 * exhausting its runtime.
281 * Here we are interested in making runtime overrun possible, but we do
282 * not want a entity which is misbehaving to affect the scheduling of all
283 * other entities.
284 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
285 * is used, in order to confine each entity within its own bandwidth.
287 * This function deals exactly with that, and ensures that when the runtime
288 * of a entity is replenished, its deadline is also postponed. That ensures
289 * the overrunning entity can't interfere with other entity in the system and
290 * can't make them miss their deadlines. Reasons why this kind of overruns
291 * could happen are, typically, a entity voluntarily trying to overcome its
292 * runtime, or it just underestimated it during sched_setscheduler_ex().
294 static void replenish_dl_entity(struct sched_dl_entity *dl_se,
295 struct sched_dl_entity *pi_se)
297 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
298 struct rq *rq = rq_of_dl_rq(dl_rq);
300 BUG_ON(pi_se->dl_runtime <= 0);
303 * This could be the case for a !-dl task that is boosted.
304 * Just go with full inherited parameters.
306 if (dl_se->dl_deadline == 0) {
307 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
308 dl_se->runtime = pi_se->dl_runtime;
312 * We keep moving the deadline away until we get some
313 * available runtime for the entity. This ensures correct
314 * handling of situations where the runtime overrun is
315 * arbitrary large.
317 while (dl_se->runtime <= 0) {
318 dl_se->deadline += pi_se->dl_period;
319 dl_se->runtime += pi_se->dl_runtime;
323 * At this point, the deadline really should be "in
324 * the future" with respect to rq->clock. If it's
325 * not, we are, for some reason, lagging too much!
326 * Anyway, after having warn userspace abut that,
327 * we still try to keep the things running by
328 * resetting the deadline and the budget of the
329 * entity.
331 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
332 static bool lag_once = false;
334 if (!lag_once) {
335 lag_once = true;
336 printk_sched("sched: DL replenish lagged to much\n");
338 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
339 dl_se->runtime = pi_se->dl_runtime;
344 * Here we check if --at time t-- an entity (which is probably being
345 * [re]activated or, in general, enqueued) can use its remaining runtime
346 * and its current deadline _without_ exceeding the bandwidth it is
347 * assigned (function returns true if it can't). We are in fact applying
348 * one of the CBS rules: when a task wakes up, if the residual runtime
349 * over residual deadline fits within the allocated bandwidth, then we
350 * can keep the current (absolute) deadline and residual budget without
351 * disrupting the schedulability of the system. Otherwise, we should
352 * refill the runtime and set the deadline a period in the future,
353 * because keeping the current (absolute) deadline of the task would
354 * result in breaking guarantees promised to other tasks (refer to
355 * Documentation/scheduler/sched-deadline.txt for more informations).
357 * This function returns true if:
359 * runtime / (deadline - t) > dl_runtime / dl_period ,
361 * IOW we can't recycle current parameters.
363 * Notice that the bandwidth check is done against the period. For
364 * task with deadline equal to period this is the same of using
365 * dl_deadline instead of dl_period in the equation above.
367 static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
368 struct sched_dl_entity *pi_se, u64 t)
370 u64 left, right;
373 * left and right are the two sides of the equation above,
374 * after a bit of shuffling to use multiplications instead
375 * of divisions.
377 * Note that none of the time values involved in the two
378 * multiplications are absolute: dl_deadline and dl_runtime
379 * are the relative deadline and the maximum runtime of each
380 * instance, runtime is the runtime left for the last instance
381 * and (deadline - t), since t is rq->clock, is the time left
382 * to the (absolute) deadline. Even if overflowing the u64 type
383 * is very unlikely to occur in both cases, here we scale down
384 * as we want to avoid that risk at all. Scaling down by 10
385 * means that we reduce granularity to 1us. We are fine with it,
386 * since this is only a true/false check and, anyway, thinking
387 * of anything below microseconds resolution is actually fiction
388 * (but still we want to give the user that illusion >;).
390 left = (pi_se->dl_period >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
391 right = ((dl_se->deadline - t) >> DL_SCALE) *
392 (pi_se->dl_runtime >> DL_SCALE);
394 return dl_time_before(right, left);
398 * When a -deadline entity is queued back on the runqueue, its runtime and
399 * deadline might need updating.
401 * The policy here is that we update the deadline of the entity only if:
402 * - the current deadline is in the past,
403 * - using the remaining runtime with the current deadline would make
404 * the entity exceed its bandwidth.
406 static void update_dl_entity(struct sched_dl_entity *dl_se,
407 struct sched_dl_entity *pi_se)
409 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
410 struct rq *rq = rq_of_dl_rq(dl_rq);
413 * The arrival of a new instance needs special treatment, i.e.,
414 * the actual scheduling parameters have to be "renewed".
416 if (dl_se->dl_new) {
417 setup_new_dl_entity(dl_se, pi_se);
418 return;
421 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
422 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
423 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
424 dl_se->runtime = pi_se->dl_runtime;
429 * If the entity depleted all its runtime, and if we want it to sleep
430 * while waiting for some new execution time to become available, we
431 * set the bandwidth enforcement timer to the replenishment instant
432 * and try to activate it.
434 * Notice that it is important for the caller to know if the timer
435 * actually started or not (i.e., the replenishment instant is in
436 * the future or in the past).
438 static int start_dl_timer(struct sched_dl_entity *dl_se, bool boosted)
440 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
441 struct rq *rq = rq_of_dl_rq(dl_rq);
442 ktime_t now, act;
443 ktime_t soft, hard;
444 unsigned long range;
445 s64 delta;
447 if (boosted)
448 return 0;
450 * We want the timer to fire at the deadline, but considering
451 * that it is actually coming from rq->clock and not from
452 * hrtimer's time base reading.
454 act = ns_to_ktime(dl_se->deadline);
455 now = hrtimer_cb_get_time(&dl_se->dl_timer);
456 delta = ktime_to_ns(now) - rq_clock(rq);
457 act = ktime_add_ns(act, delta);
460 * If the expiry time already passed, e.g., because the value
461 * chosen as the deadline is too small, don't even try to
462 * start the timer in the past!
464 if (ktime_us_delta(act, now) < 0)
465 return 0;
467 hrtimer_set_expires(&dl_se->dl_timer, act);
469 soft = hrtimer_get_softexpires(&dl_se->dl_timer);
470 hard = hrtimer_get_expires(&dl_se->dl_timer);
471 range = ktime_to_ns(ktime_sub(hard, soft));
472 __hrtimer_start_range_ns(&dl_se->dl_timer, soft,
473 range, HRTIMER_MODE_ABS, 0);
475 return hrtimer_active(&dl_se->dl_timer);
479 * This is the bandwidth enforcement timer callback. If here, we know
480 * a task is not on its dl_rq, since the fact that the timer was running
481 * means the task is throttled and needs a runtime replenishment.
483 * However, what we actually do depends on the fact the task is active,
484 * (it is on its rq) or has been removed from there by a call to
485 * dequeue_task_dl(). In the former case we must issue the runtime
486 * replenishment and add the task back to the dl_rq; in the latter, we just
487 * do nothing but clearing dl_throttled, so that runtime and deadline
488 * updating (and the queueing back to dl_rq) will be done by the
489 * next call to enqueue_task_dl().
491 static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
493 struct sched_dl_entity *dl_se = container_of(timer,
494 struct sched_dl_entity,
495 dl_timer);
496 struct task_struct *p = dl_task_of(dl_se);
497 struct rq *rq = task_rq(p);
498 raw_spin_lock(&rq->lock);
501 * We need to take care of a possible races here. In fact, the
502 * task might have changed its scheduling policy to something
503 * different from SCHED_DEADLINE or changed its reservation
504 * parameters (through sched_setscheduler()).
506 if (!dl_task(p) || dl_se->dl_new)
507 goto unlock;
509 sched_clock_tick();
510 update_rq_clock(rq);
511 dl_se->dl_throttled = 0;
512 if (p->on_rq) {
513 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
514 if (task_has_dl_policy(rq->curr))
515 check_preempt_curr_dl(rq, p, 0);
516 else
517 resched_task(rq->curr);
518 #ifdef CONFIG_SMP
520 * Queueing this task back might have overloaded rq,
521 * check if we need to kick someone away.
523 if (has_pushable_dl_tasks(rq))
524 push_dl_task(rq);
525 #endif
527 unlock:
528 raw_spin_unlock(&rq->lock);
530 return HRTIMER_NORESTART;
533 void init_dl_task_timer(struct sched_dl_entity *dl_se)
535 struct hrtimer *timer = &dl_se->dl_timer;
537 if (hrtimer_active(timer)) {
538 hrtimer_try_to_cancel(timer);
539 return;
542 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
543 timer->function = dl_task_timer;
546 static
547 int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
549 int dmiss = dl_time_before(dl_se->deadline, rq_clock(rq));
550 int rorun = dl_se->runtime <= 0;
552 if (!rorun && !dmiss)
553 return 0;
556 * If we are beyond our current deadline and we are still
557 * executing, then we have already used some of the runtime of
558 * the next instance. Thus, if we do not account that, we are
559 * stealing bandwidth from the system at each deadline miss!
561 if (dmiss) {
562 dl_se->runtime = rorun ? dl_se->runtime : 0;
563 dl_se->runtime -= rq_clock(rq) - dl_se->deadline;
566 return 1;
570 * Update the current task's runtime statistics (provided it is still
571 * a -deadline task and has not been removed from the dl_rq).
573 static void update_curr_dl(struct rq *rq)
575 struct task_struct *curr = rq->curr;
576 struct sched_dl_entity *dl_se = &curr->dl;
577 u64 delta_exec;
579 if (!dl_task(curr) || !on_dl_rq(dl_se))
580 return;
583 * Consumed budget is computed considering the time as
584 * observed by schedulable tasks (excluding time spent
585 * in hardirq context, etc.). Deadlines are instead
586 * computed using hard walltime. This seems to be the more
587 * natural solution, but the full ramifications of this
588 * approach need further study.
590 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
591 if (unlikely((s64)delta_exec < 0))
592 delta_exec = 0;
594 schedstat_set(curr->se.statistics.exec_max,
595 max(curr->se.statistics.exec_max, delta_exec));
597 curr->se.sum_exec_runtime += delta_exec;
598 account_group_exec_runtime(curr, delta_exec);
600 curr->se.exec_start = rq_clock_task(rq);
601 cpuacct_charge(curr, delta_exec);
603 sched_rt_avg_update(rq, delta_exec);
605 dl_se->runtime -= delta_exec;
606 if (dl_runtime_exceeded(rq, dl_se)) {
607 __dequeue_task_dl(rq, curr, 0);
608 if (likely(start_dl_timer(dl_se, curr->dl.dl_boosted)))
609 dl_se->dl_throttled = 1;
610 else
611 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
613 if (!is_leftmost(curr, &rq->dl))
614 resched_task(curr);
618 * Because -- for now -- we share the rt bandwidth, we need to
619 * account our runtime there too, otherwise actual rt tasks
620 * would be able to exceed the shared quota.
622 * Account to the root rt group for now.
624 * The solution we're working towards is having the RT groups scheduled
625 * using deadline servers -- however there's a few nasties to figure
626 * out before that can happen.
628 if (rt_bandwidth_enabled()) {
629 struct rt_rq *rt_rq = &rq->rt;
631 raw_spin_lock(&rt_rq->rt_runtime_lock);
632 rt_rq->rt_time += delta_exec;
634 * We'll let actual RT tasks worry about the overflow here, we
635 * have our own CBS to keep us inline -- see above.
637 raw_spin_unlock(&rt_rq->rt_runtime_lock);
641 #ifdef CONFIG_SMP
643 static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
645 static inline u64 next_deadline(struct rq *rq)
647 struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
649 if (next && dl_prio(next->prio))
650 return next->dl.deadline;
651 else
652 return 0;
655 static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
657 struct rq *rq = rq_of_dl_rq(dl_rq);
659 if (dl_rq->earliest_dl.curr == 0 ||
660 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
662 * If the dl_rq had no -deadline tasks, or if the new task
663 * has shorter deadline than the current one on dl_rq, we
664 * know that the previous earliest becomes our next earliest,
665 * as the new task becomes the earliest itself.
667 dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
668 dl_rq->earliest_dl.curr = deadline;
669 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline, 1);
670 } else if (dl_rq->earliest_dl.next == 0 ||
671 dl_time_before(deadline, dl_rq->earliest_dl.next)) {
673 * On the other hand, if the new -deadline task has a
674 * a later deadline than the earliest one on dl_rq, but
675 * it is earlier than the next (if any), we must
676 * recompute the next-earliest.
678 dl_rq->earliest_dl.next = next_deadline(rq);
682 static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
684 struct rq *rq = rq_of_dl_rq(dl_rq);
687 * Since we may have removed our earliest (and/or next earliest)
688 * task we must recompute them.
690 if (!dl_rq->dl_nr_running) {
691 dl_rq->earliest_dl.curr = 0;
692 dl_rq->earliest_dl.next = 0;
693 cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
694 } else {
695 struct rb_node *leftmost = dl_rq->rb_leftmost;
696 struct sched_dl_entity *entry;
698 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
699 dl_rq->earliest_dl.curr = entry->deadline;
700 dl_rq->earliest_dl.next = next_deadline(rq);
701 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline, 1);
705 #else
707 static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
708 static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
710 #endif /* CONFIG_SMP */
712 static inline
713 void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
715 int prio = dl_task_of(dl_se)->prio;
716 u64 deadline = dl_se->deadline;
718 WARN_ON(!dl_prio(prio));
719 dl_rq->dl_nr_running++;
721 inc_dl_deadline(dl_rq, deadline);
722 inc_dl_migration(dl_se, dl_rq);
725 static inline
726 void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
728 int prio = dl_task_of(dl_se)->prio;
730 WARN_ON(!dl_prio(prio));
731 WARN_ON(!dl_rq->dl_nr_running);
732 dl_rq->dl_nr_running--;
734 dec_dl_deadline(dl_rq, dl_se->deadline);
735 dec_dl_migration(dl_se, dl_rq);
738 static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
740 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
741 struct rb_node **link = &dl_rq->rb_root.rb_node;
742 struct rb_node *parent = NULL;
743 struct sched_dl_entity *entry;
744 int leftmost = 1;
746 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
748 while (*link) {
749 parent = *link;
750 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
751 if (dl_time_before(dl_se->deadline, entry->deadline))
752 link = &parent->rb_left;
753 else {
754 link = &parent->rb_right;
755 leftmost = 0;
759 if (leftmost)
760 dl_rq->rb_leftmost = &dl_se->rb_node;
762 rb_link_node(&dl_se->rb_node, parent, link);
763 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
765 inc_dl_tasks(dl_se, dl_rq);
768 static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
770 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
772 if (RB_EMPTY_NODE(&dl_se->rb_node))
773 return;
775 if (dl_rq->rb_leftmost == &dl_se->rb_node) {
776 struct rb_node *next_node;
778 next_node = rb_next(&dl_se->rb_node);
779 dl_rq->rb_leftmost = next_node;
782 rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
783 RB_CLEAR_NODE(&dl_se->rb_node);
785 dec_dl_tasks(dl_se, dl_rq);
788 static void
789 enqueue_dl_entity(struct sched_dl_entity *dl_se,
790 struct sched_dl_entity *pi_se, int flags)
792 BUG_ON(on_dl_rq(dl_se));
795 * If this is a wakeup or a new instance, the scheduling
796 * parameters of the task might need updating. Otherwise,
797 * we want a replenishment of its runtime.
799 if (!dl_se->dl_new && flags & ENQUEUE_REPLENISH)
800 replenish_dl_entity(dl_se, pi_se);
801 else
802 update_dl_entity(dl_se, pi_se);
804 __enqueue_dl_entity(dl_se);
807 static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
809 __dequeue_dl_entity(dl_se);
812 static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
814 struct task_struct *pi_task = rt_mutex_get_top_task(p);
815 struct sched_dl_entity *pi_se = &p->dl;
818 * Use the scheduling parameters of the top pi-waiter
819 * task if we have one and its (relative) deadline is
820 * smaller than our one... OTW we keep our runtime and
821 * deadline.
823 if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio))
824 pi_se = &pi_task->dl;
827 * If p is throttled, we do nothing. In fact, if it exhausted
828 * its budget it needs a replenishment and, since it now is on
829 * its rq, the bandwidth timer callback (which clearly has not
830 * run yet) will take care of this.
832 if (p->dl.dl_throttled)
833 return;
835 enqueue_dl_entity(&p->dl, pi_se, flags);
837 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
838 enqueue_pushable_dl_task(rq, p);
840 inc_nr_running(rq);
843 static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
845 dequeue_dl_entity(&p->dl);
846 dequeue_pushable_dl_task(rq, p);
849 static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
851 update_curr_dl(rq);
852 __dequeue_task_dl(rq, p, flags);
854 dec_nr_running(rq);
858 * Yield task semantic for -deadline tasks is:
860 * get off from the CPU until our next instance, with
861 * a new runtime. This is of little use now, since we
862 * don't have a bandwidth reclaiming mechanism. Anyway,
863 * bandwidth reclaiming is planned for the future, and
864 * yield_task_dl will indicate that some spare budget
865 * is available for other task instances to use it.
867 static void yield_task_dl(struct rq *rq)
869 struct task_struct *p = rq->curr;
872 * We make the task go to sleep until its current deadline by
873 * forcing its runtime to zero. This way, update_curr_dl() stops
874 * it and the bandwidth timer will wake it up and will give it
875 * new scheduling parameters (thanks to dl_new=1).
877 if (p->dl.runtime > 0) {
878 rq->curr->dl.dl_new = 1;
879 p->dl.runtime = 0;
881 update_curr_dl(rq);
884 #ifdef CONFIG_SMP
886 static int find_later_rq(struct task_struct *task);
888 static int
889 select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
891 struct task_struct *curr;
892 struct rq *rq;
894 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
895 goto out;
897 rq = cpu_rq(cpu);
899 rcu_read_lock();
900 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
903 * If we are dealing with a -deadline task, we must
904 * decide where to wake it up.
905 * If it has a later deadline and the current task
906 * on this rq can't move (provided the waking task
907 * can!) we prefer to send it somewhere else. On the
908 * other hand, if it has a shorter deadline, we
909 * try to make it stay here, it might be important.
911 if (unlikely(dl_task(curr)) &&
912 (curr->nr_cpus_allowed < 2 ||
913 !dl_entity_preempt(&p->dl, &curr->dl)) &&
914 (p->nr_cpus_allowed > 1)) {
915 int target = find_later_rq(p);
917 if (target != -1)
918 cpu = target;
920 rcu_read_unlock();
922 out:
923 return cpu;
926 static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
929 * Current can't be migrated, useless to reschedule,
930 * let's hope p can move out.
932 if (rq->curr->nr_cpus_allowed == 1 ||
933 cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
934 return;
937 * p is migratable, so let's not schedule it and
938 * see if it is pushed or pulled somewhere else.
940 if (p->nr_cpus_allowed != 1 &&
941 cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
942 return;
944 resched_task(rq->curr);
947 #endif /* CONFIG_SMP */
950 * Only called when both the current and waking task are -deadline
951 * tasks.
953 static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
954 int flags)
956 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
957 resched_task(rq->curr);
958 return;
961 #ifdef CONFIG_SMP
963 * In the unlikely case current and p have the same deadline
964 * let us try to decide what's the best thing to do...
966 if ((p->dl.deadline == rq->curr->dl.deadline) &&
967 !test_tsk_need_resched(rq->curr))
968 check_preempt_equal_dl(rq, p);
969 #endif /* CONFIG_SMP */
972 #ifdef CONFIG_SCHED_HRTICK
973 static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
975 s64 delta = p->dl.dl_runtime - p->dl.runtime;
977 if (delta > 10000)
978 hrtick_start(rq, p->dl.runtime);
980 #endif
982 static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
983 struct dl_rq *dl_rq)
985 struct rb_node *left = dl_rq->rb_leftmost;
987 if (!left)
988 return NULL;
990 return rb_entry(left, struct sched_dl_entity, rb_node);
993 struct task_struct *pick_next_task_dl(struct rq *rq)
995 struct sched_dl_entity *dl_se;
996 struct task_struct *p;
997 struct dl_rq *dl_rq;
999 dl_rq = &rq->dl;
1001 if (unlikely(!dl_rq->dl_nr_running))
1002 return NULL;
1004 dl_se = pick_next_dl_entity(rq, dl_rq);
1005 BUG_ON(!dl_se);
1007 p = dl_task_of(dl_se);
1008 p->se.exec_start = rq_clock_task(rq);
1010 /* Running task will never be pushed. */
1011 dequeue_pushable_dl_task(rq, p);
1013 #ifdef CONFIG_SCHED_HRTICK
1014 if (hrtick_enabled(rq))
1015 start_hrtick_dl(rq, p);
1016 #endif
1018 #ifdef CONFIG_SMP
1019 rq->post_schedule = has_pushable_dl_tasks(rq);
1020 #endif /* CONFIG_SMP */
1022 return p;
1025 static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1027 update_curr_dl(rq);
1029 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
1030 enqueue_pushable_dl_task(rq, p);
1033 static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1035 update_curr_dl(rq);
1037 #ifdef CONFIG_SCHED_HRTICK
1038 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0)
1039 start_hrtick_dl(rq, p);
1040 #endif
1043 static void task_fork_dl(struct task_struct *p)
1046 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1047 * sched_fork()
1051 static void task_dead_dl(struct task_struct *p)
1053 struct hrtimer *timer = &p->dl.dl_timer;
1054 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1057 * Since we are TASK_DEAD we won't slip out of the domain!
1059 raw_spin_lock_irq(&dl_b->lock);
1060 dl_b->total_bw -= p->dl.dl_bw;
1061 raw_spin_unlock_irq(&dl_b->lock);
1063 hrtimer_cancel(timer);
1066 static void set_curr_task_dl(struct rq *rq)
1068 struct task_struct *p = rq->curr;
1070 p->se.exec_start = rq_clock_task(rq);
1072 /* You can't push away the running task */
1073 dequeue_pushable_dl_task(rq, p);
1076 #ifdef CONFIG_SMP
1078 /* Only try algorithms three times */
1079 #define DL_MAX_TRIES 3
1081 static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1083 if (!task_running(rq, p) &&
1084 (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
1085 (p->nr_cpus_allowed > 1))
1086 return 1;
1088 return 0;
1091 /* Returns the second earliest -deadline task, NULL otherwise */
1092 static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
1094 struct rb_node *next_node = rq->dl.rb_leftmost;
1095 struct sched_dl_entity *dl_se;
1096 struct task_struct *p = NULL;
1098 next_node:
1099 next_node = rb_next(next_node);
1100 if (next_node) {
1101 dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
1102 p = dl_task_of(dl_se);
1104 if (pick_dl_task(rq, p, cpu))
1105 return p;
1107 goto next_node;
1110 return NULL;
1113 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1115 static int find_later_rq(struct task_struct *task)
1117 struct sched_domain *sd;
1118 struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl);
1119 int this_cpu = smp_processor_id();
1120 int best_cpu, cpu = task_cpu(task);
1122 /* Make sure the mask is initialized first */
1123 if (unlikely(!later_mask))
1124 return -1;
1126 if (task->nr_cpus_allowed == 1)
1127 return -1;
1129 best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
1130 task, later_mask);
1131 if (best_cpu == -1)
1132 return -1;
1135 * If we are here, some target has been found,
1136 * the most suitable of which is cached in best_cpu.
1137 * This is, among the runqueues where the current tasks
1138 * have later deadlines than the task's one, the rq
1139 * with the latest possible one.
1141 * Now we check how well this matches with task's
1142 * affinity and system topology.
1144 * The last cpu where the task run is our first
1145 * guess, since it is most likely cache-hot there.
1147 if (cpumask_test_cpu(cpu, later_mask))
1148 return cpu;
1150 * Check if this_cpu is to be skipped (i.e., it is
1151 * not in the mask) or not.
1153 if (!cpumask_test_cpu(this_cpu, later_mask))
1154 this_cpu = -1;
1156 rcu_read_lock();
1157 for_each_domain(cpu, sd) {
1158 if (sd->flags & SD_WAKE_AFFINE) {
1161 * If possible, preempting this_cpu is
1162 * cheaper than migrating.
1164 if (this_cpu != -1 &&
1165 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1166 rcu_read_unlock();
1167 return this_cpu;
1171 * Last chance: if best_cpu is valid and is
1172 * in the mask, that becomes our choice.
1174 if (best_cpu < nr_cpu_ids &&
1175 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
1176 rcu_read_unlock();
1177 return best_cpu;
1181 rcu_read_unlock();
1184 * At this point, all our guesses failed, we just return
1185 * 'something', and let the caller sort the things out.
1187 if (this_cpu != -1)
1188 return this_cpu;
1190 cpu = cpumask_any(later_mask);
1191 if (cpu < nr_cpu_ids)
1192 return cpu;
1194 return -1;
1197 /* Locks the rq it finds */
1198 static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1200 struct rq *later_rq = NULL;
1201 int tries;
1202 int cpu;
1204 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1205 cpu = find_later_rq(task);
1207 if ((cpu == -1) || (cpu == rq->cpu))
1208 break;
1210 later_rq = cpu_rq(cpu);
1212 /* Retry if something changed. */
1213 if (double_lock_balance(rq, later_rq)) {
1214 if (unlikely(task_rq(task) != rq ||
1215 !cpumask_test_cpu(later_rq->cpu,
1216 &task->cpus_allowed) ||
1217 task_running(rq, task) || !task->on_rq)) {
1218 double_unlock_balance(rq, later_rq);
1219 later_rq = NULL;
1220 break;
1225 * If the rq we found has no -deadline task, or
1226 * its earliest one has a later deadline than our
1227 * task, the rq is a good one.
1229 if (!later_rq->dl.dl_nr_running ||
1230 dl_time_before(task->dl.deadline,
1231 later_rq->dl.earliest_dl.curr))
1232 break;
1234 /* Otherwise we try again. */
1235 double_unlock_balance(rq, later_rq);
1236 later_rq = NULL;
1239 return later_rq;
1242 static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1244 struct task_struct *p;
1246 if (!has_pushable_dl_tasks(rq))
1247 return NULL;
1249 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
1250 struct task_struct, pushable_dl_tasks);
1252 BUG_ON(rq->cpu != task_cpu(p));
1253 BUG_ON(task_current(rq, p));
1254 BUG_ON(p->nr_cpus_allowed <= 1);
1256 BUG_ON(!p->on_rq);
1257 BUG_ON(!dl_task(p));
1259 return p;
1263 * See if the non running -deadline tasks on this rq
1264 * can be sent to some other CPU where they can preempt
1265 * and start executing.
1267 static int push_dl_task(struct rq *rq)
1269 struct task_struct *next_task;
1270 struct rq *later_rq;
1272 if (!rq->dl.overloaded)
1273 return 0;
1275 next_task = pick_next_pushable_dl_task(rq);
1276 if (!next_task)
1277 return 0;
1279 retry:
1280 if (unlikely(next_task == rq->curr)) {
1281 WARN_ON(1);
1282 return 0;
1286 * If next_task preempts rq->curr, and rq->curr
1287 * can move away, it makes sense to just reschedule
1288 * without going further in pushing next_task.
1290 if (dl_task(rq->curr) &&
1291 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
1292 rq->curr->nr_cpus_allowed > 1) {
1293 resched_task(rq->curr);
1294 return 0;
1297 /* We might release rq lock */
1298 get_task_struct(next_task);
1300 /* Will lock the rq it'll find */
1301 later_rq = find_lock_later_rq(next_task, rq);
1302 if (!later_rq) {
1303 struct task_struct *task;
1306 * We must check all this again, since
1307 * find_lock_later_rq releases rq->lock and it is
1308 * then possible that next_task has migrated.
1310 task = pick_next_pushable_dl_task(rq);
1311 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1313 * The task is still there. We don't try
1314 * again, some other cpu will pull it when ready.
1316 dequeue_pushable_dl_task(rq, next_task);
1317 goto out;
1320 if (!task)
1321 /* No more tasks */
1322 goto out;
1324 put_task_struct(next_task);
1325 next_task = task;
1326 goto retry;
1329 deactivate_task(rq, next_task, 0);
1330 set_task_cpu(next_task, later_rq->cpu);
1331 activate_task(later_rq, next_task, 0);
1333 resched_task(later_rq->curr);
1335 double_unlock_balance(rq, later_rq);
1337 out:
1338 put_task_struct(next_task);
1340 return 1;
1343 static void push_dl_tasks(struct rq *rq)
1345 /* Terminates as it moves a -deadline task */
1346 while (push_dl_task(rq))
1350 static int pull_dl_task(struct rq *this_rq)
1352 int this_cpu = this_rq->cpu, ret = 0, cpu;
1353 struct task_struct *p;
1354 struct rq *src_rq;
1355 u64 dmin = LONG_MAX;
1357 if (likely(!dl_overloaded(this_rq)))
1358 return 0;
1361 * Match the barrier from dl_set_overloaded; this guarantees that if we
1362 * see overloaded we must also see the dlo_mask bit.
1364 smp_rmb();
1366 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
1367 if (this_cpu == cpu)
1368 continue;
1370 src_rq = cpu_rq(cpu);
1373 * It looks racy, abd it is! However, as in sched_rt.c,
1374 * we are fine with this.
1376 if (this_rq->dl.dl_nr_running &&
1377 dl_time_before(this_rq->dl.earliest_dl.curr,
1378 src_rq->dl.earliest_dl.next))
1379 continue;
1381 /* Might drop this_rq->lock */
1382 double_lock_balance(this_rq, src_rq);
1385 * If there are no more pullable tasks on the
1386 * rq, we're done with it.
1388 if (src_rq->dl.dl_nr_running <= 1)
1389 goto skip;
1391 p = pick_next_earliest_dl_task(src_rq, this_cpu);
1394 * We found a task to be pulled if:
1395 * - it preempts our current (if there's one),
1396 * - it will preempt the last one we pulled (if any).
1398 if (p && dl_time_before(p->dl.deadline, dmin) &&
1399 (!this_rq->dl.dl_nr_running ||
1400 dl_time_before(p->dl.deadline,
1401 this_rq->dl.earliest_dl.curr))) {
1402 WARN_ON(p == src_rq->curr);
1403 WARN_ON(!p->on_rq);
1406 * Then we pull iff p has actually an earlier
1407 * deadline than the current task of its runqueue.
1409 if (dl_time_before(p->dl.deadline,
1410 src_rq->curr->dl.deadline))
1411 goto skip;
1413 ret = 1;
1415 deactivate_task(src_rq, p, 0);
1416 set_task_cpu(p, this_cpu);
1417 activate_task(this_rq, p, 0);
1418 dmin = p->dl.deadline;
1420 /* Is there any other task even earlier? */
1422 skip:
1423 double_unlock_balance(this_rq, src_rq);
1426 return ret;
1429 static void pre_schedule_dl(struct rq *rq, struct task_struct *prev)
1431 /* Try to pull other tasks here */
1432 if (dl_task(prev))
1433 pull_dl_task(rq);
1436 static void post_schedule_dl(struct rq *rq)
1438 push_dl_tasks(rq);
1442 * Since the task is not running and a reschedule is not going to happen
1443 * anytime soon on its runqueue, we try pushing it away now.
1445 static void task_woken_dl(struct rq *rq, struct task_struct *p)
1447 if (!task_running(rq, p) &&
1448 !test_tsk_need_resched(rq->curr) &&
1449 has_pushable_dl_tasks(rq) &&
1450 p->nr_cpus_allowed > 1 &&
1451 dl_task(rq->curr) &&
1452 (rq->curr->nr_cpus_allowed < 2 ||
1453 dl_entity_preempt(&rq->curr->dl, &p->dl))) {
1454 push_dl_tasks(rq);
1458 static void set_cpus_allowed_dl(struct task_struct *p,
1459 const struct cpumask *new_mask)
1461 struct rq *rq;
1462 int weight;
1464 BUG_ON(!dl_task(p));
1467 * Update only if the task is actually running (i.e.,
1468 * it is on the rq AND it is not throttled).
1470 if (!on_dl_rq(&p->dl))
1471 return;
1473 weight = cpumask_weight(new_mask);
1476 * Only update if the process changes its state from whether it
1477 * can migrate or not.
1479 if ((p->nr_cpus_allowed > 1) == (weight > 1))
1480 return;
1482 rq = task_rq(p);
1485 * The process used to be able to migrate OR it can now migrate
1487 if (weight <= 1) {
1488 if (!task_current(rq, p))
1489 dequeue_pushable_dl_task(rq, p);
1490 BUG_ON(!rq->dl.dl_nr_migratory);
1491 rq->dl.dl_nr_migratory--;
1492 } else {
1493 if (!task_current(rq, p))
1494 enqueue_pushable_dl_task(rq, p);
1495 rq->dl.dl_nr_migratory++;
1498 update_dl_migration(&rq->dl);
1501 /* Assumes rq->lock is held */
1502 static void rq_online_dl(struct rq *rq)
1504 if (rq->dl.overloaded)
1505 dl_set_overload(rq);
1507 if (rq->dl.dl_nr_running > 0)
1508 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr, 1);
1511 /* Assumes rq->lock is held */
1512 static void rq_offline_dl(struct rq *rq)
1514 if (rq->dl.overloaded)
1515 dl_clear_overload(rq);
1517 cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
1520 void init_sched_dl_class(void)
1522 unsigned int i;
1524 for_each_possible_cpu(i)
1525 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
1526 GFP_KERNEL, cpu_to_node(i));
1529 #endif /* CONFIG_SMP */
1531 static void switched_from_dl(struct rq *rq, struct task_struct *p)
1533 if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy))
1534 hrtimer_try_to_cancel(&p->dl.dl_timer);
1536 #ifdef CONFIG_SMP
1538 * Since this might be the only -deadline task on the rq,
1539 * this is the right place to try to pull some other one
1540 * from an overloaded cpu, if any.
1542 if (!rq->dl.dl_nr_running)
1543 pull_dl_task(rq);
1544 #endif
1548 * When switching to -deadline, we may overload the rq, then
1549 * we try to push someone off, if possible.
1551 static void switched_to_dl(struct rq *rq, struct task_struct *p)
1553 int check_resched = 1;
1556 * If p is throttled, don't consider the possibility
1557 * of preempting rq->curr, the check will be done right
1558 * after its runtime will get replenished.
1560 if (unlikely(p->dl.dl_throttled))
1561 return;
1563 if (p->on_rq || rq->curr != p) {
1564 #ifdef CONFIG_SMP
1565 if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p))
1566 /* Only reschedule if pushing failed */
1567 check_resched = 0;
1568 #endif /* CONFIG_SMP */
1569 if (check_resched && task_has_dl_policy(rq->curr))
1570 check_preempt_curr_dl(rq, p, 0);
1575 * If the scheduling parameters of a -deadline task changed,
1576 * a push or pull operation might be needed.
1578 static void prio_changed_dl(struct rq *rq, struct task_struct *p,
1579 int oldprio)
1581 if (p->on_rq || rq->curr == p) {
1582 #ifdef CONFIG_SMP
1584 * This might be too much, but unfortunately
1585 * we don't have the old deadline value, and
1586 * we can't argue if the task is increasing
1587 * or lowering its prio, so...
1589 if (!rq->dl.overloaded)
1590 pull_dl_task(rq);
1593 * If we now have a earlier deadline task than p,
1594 * then reschedule, provided p is still on this
1595 * runqueue.
1597 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) &&
1598 rq->curr == p)
1599 resched_task(p);
1600 #else
1602 * Again, we don't know if p has a earlier
1603 * or later deadline, so let's blindly set a
1604 * (maybe not needed) rescheduling point.
1606 resched_task(p);
1607 #endif /* CONFIG_SMP */
1608 } else
1609 switched_to_dl(rq, p);
1612 const struct sched_class dl_sched_class = {
1613 .next = &rt_sched_class,
1614 .enqueue_task = enqueue_task_dl,
1615 .dequeue_task = dequeue_task_dl,
1616 .yield_task = yield_task_dl,
1618 .check_preempt_curr = check_preempt_curr_dl,
1620 .pick_next_task = pick_next_task_dl,
1621 .put_prev_task = put_prev_task_dl,
1623 #ifdef CONFIG_SMP
1624 .select_task_rq = select_task_rq_dl,
1625 .set_cpus_allowed = set_cpus_allowed_dl,
1626 .rq_online = rq_online_dl,
1627 .rq_offline = rq_offline_dl,
1628 .pre_schedule = pre_schedule_dl,
1629 .post_schedule = post_schedule_dl,
1630 .task_woken = task_woken_dl,
1631 #endif
1633 .set_curr_task = set_curr_task_dl,
1634 .task_tick = task_tick_dl,
1635 .task_fork = task_fork_dl,
1636 .task_dead = task_dead_dl,
1638 .prio_changed = prio_changed_dl,
1639 .switched_from = switched_from_dl,
1640 .switched_to = switched_to_dl,