blob: 362ab1f886b079df1975225a74837ac843239239 [file] [log] [blame]
Dario Faggioliaab03e02013-11-28 11:14:43 +01001/*
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>,
Juri Lelli1baca4c2013-11-07 14:43:38 +010013 * Juri Lelli <juri.lelli@gmail.com>,
Dario Faggioliaab03e02013-11-28 11:14:43 +010014 * Michael Trimarchi <michael@amarulasolutions.com>,
15 * Fabio Checconi <fchecconi@gmail.com>
16 */
17#include "sched.h"
18
Juri Lelli6bfd6d72013-11-07 14:43:47 +010019#include <linux/slab.h>
20
Dario Faggioli332ac172013-11-07 14:43:45 +010021struct dl_bandwidth def_dl_bandwidth;
22
Dario Faggioliaab03e02013-11-28 11:14:43 +010023static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
24{
25 return container_of(dl_se, struct task_struct, dl);
26}
27
28static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
29{
30 return container_of(dl_rq, struct rq, dl);
31}
32
33static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
34{
35 struct task_struct *p = dl_task_of(dl_se);
36 struct rq *rq = task_rq(p);
37
38 return &rq->dl;
39}
40
41static inline int on_dl_rq(struct sched_dl_entity *dl_se)
42{
43 return !RB_EMPTY_NODE(&dl_se->rb_node);
44}
45
46static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
47{
48 struct sched_dl_entity *dl_se = &p->dl;
49
50 return dl_rq->rb_leftmost == &dl_se->rb_node;
51}
52
Dario Faggioli332ac172013-11-07 14:43:45 +010053void 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
Dario Faggioli332ac172013-11-07 14:43:45 +010060void 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);
Peter Zijlstra17248132013-12-17 12:44:49 +010064 if (global_rt_runtime() == RUNTIME_INF)
Dario Faggioli332ac172013-11-07 14:43:45 +010065 dl_b->bw = -1;
66 else
Peter Zijlstra17248132013-12-17 12:44:49 +010067 dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +010068 raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
69 dl_b->total_bw = 0;
70}
71
Dario Faggioliaab03e02013-11-28 11:14:43 +010072void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq)
73{
74 dl_rq->rb_root = RB_ROOT;
Juri Lelli1baca4c2013-11-07 14:43:38 +010075
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;
Dario Faggioli332ac172013-11-07 14:43:45 +010083#else
84 init_dl_bw(&dl_rq->dl_bw);
Juri Lelli1baca4c2013-11-07 14:43:38 +010085#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +010086}
87
Juri Lelli1baca4c2013-11-07 14:43:38 +010088#ifdef CONFIG_SMP
89
90static inline int dl_overloaded(struct rq *rq)
91{
92 return atomic_read(&rq->rd->dlo_count);
93}
94
95static inline void dl_set_overload(struct rq *rq)
96{
97 if (!rq->online)
98 return;
99
100 cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
101 /*
102 * Must be visible before the overload count is
103 * set (as in sched_rt.c).
104 *
105 * Matched by the barrier in pull_dl_task().
106 */
107 smp_wmb();
108 atomic_inc(&rq->rd->dlo_count);
109}
110
111static inline void dl_clear_overload(struct rq *rq)
112{
113 if (!rq->online)
114 return;
115
116 atomic_dec(&rq->rd->dlo_count);
117 cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
118}
119
120static void update_dl_migration(struct dl_rq *dl_rq)
121{
Kirill Tkhai995b9ea2014-02-18 02:24:13 +0400122 if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
Juri Lelli1baca4c2013-11-07 14:43:38 +0100123 if (!dl_rq->overloaded) {
124 dl_set_overload(rq_of_dl_rq(dl_rq));
125 dl_rq->overloaded = 1;
126 }
127 } else if (dl_rq->overloaded) {
128 dl_clear_overload(rq_of_dl_rq(dl_rq));
129 dl_rq->overloaded = 0;
130 }
131}
132
133static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
134{
135 struct task_struct *p = dl_task_of(dl_se);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100136
Juri Lelli1baca4c2013-11-07 14:43:38 +0100137 if (p->nr_cpus_allowed > 1)
138 dl_rq->dl_nr_migratory++;
139
140 update_dl_migration(dl_rq);
141}
142
143static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
144{
145 struct task_struct *p = dl_task_of(dl_se);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100146
Juri Lelli1baca4c2013-11-07 14:43:38 +0100147 if (p->nr_cpus_allowed > 1)
148 dl_rq->dl_nr_migratory--;
149
150 update_dl_migration(dl_rq);
151}
152
153/*
154 * The list of pushable -deadline task is not a plist, like in
155 * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
156 */
157static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
158{
159 struct dl_rq *dl_rq = &rq->dl;
160 struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
161 struct rb_node *parent = NULL;
162 struct task_struct *entry;
163 int leftmost = 1;
164
165 BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
166
167 while (*link) {
168 parent = *link;
169 entry = rb_entry(parent, struct task_struct,
170 pushable_dl_tasks);
171 if (dl_entity_preempt(&p->dl, &entry->dl))
172 link = &parent->rb_left;
173 else {
174 link = &parent->rb_right;
175 leftmost = 0;
176 }
177 }
178
179 if (leftmost)
180 dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
181
182 rb_link_node(&p->pushable_dl_tasks, parent, link);
183 rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
184}
185
186static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
187{
188 struct dl_rq *dl_rq = &rq->dl;
189
190 if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
191 return;
192
193 if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
194 struct rb_node *next_node;
195
196 next_node = rb_next(&p->pushable_dl_tasks);
197 dl_rq->pushable_dl_tasks_leftmost = next_node;
198 }
199
200 rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
201 RB_CLEAR_NODE(&p->pushable_dl_tasks);
202}
203
204static inline int has_pushable_dl_tasks(struct rq *rq)
205{
206 return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
207}
208
209static int push_dl_task(struct rq *rq);
210
Peter Zijlstradc877342014-02-12 15:47:29 +0100211static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
212{
213 return dl_task(prev);
214}
215
216static inline void set_post_schedule(struct rq *rq)
217{
218 rq->post_schedule = has_pushable_dl_tasks(rq);
219}
220
Juri Lelli1baca4c2013-11-07 14:43:38 +0100221#else
222
223static inline
224void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
225{
226}
227
228static inline
229void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
230{
231}
232
233static inline
234void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
235{
236}
237
238static inline
239void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
240{
241}
242
Peter Zijlstradc877342014-02-12 15:47:29 +0100243static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
244{
245 return false;
246}
247
248static inline int pull_dl_task(struct rq *rq)
249{
250 return 0;
251}
252
253static inline void set_post_schedule(struct rq *rq)
254{
255}
Juri Lelli1baca4c2013-11-07 14:43:38 +0100256#endif /* CONFIG_SMP */
257
Dario Faggioliaab03e02013-11-28 11:14:43 +0100258static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
259static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
260static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
261 int flags);
262
263/*
264 * We are being explicitly informed that a new instance is starting,
265 * and this means that:
266 * - the absolute deadline of the entity has to be placed at
267 * current time + relative deadline;
268 * - the runtime of the entity has to be set to the maximum value.
269 *
270 * The capability of specifying such event is useful whenever a -deadline
271 * entity wants to (try to!) synchronize its behaviour with the scheduler's
272 * one, and to (try to!) reconcile itself with its own scheduling
273 * parameters.
274 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100275static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se,
276 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100277{
278 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
279 struct rq *rq = rq_of_dl_rq(dl_rq);
280
281 WARN_ON(!dl_se->dl_new || dl_se->dl_throttled);
282
283 /*
284 * We use the regular wall clock time to set deadlines in the
285 * future; in fact, we must consider execution overheads (time
286 * spent on hardirq context, etc.).
287 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100288 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
289 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100290 dl_se->dl_new = 0;
291}
292
293/*
294 * Pure Earliest Deadline First (EDF) scheduling does not deal with the
295 * possibility of a entity lasting more than what it declared, and thus
296 * exhausting its runtime.
297 *
298 * Here we are interested in making runtime overrun possible, but we do
299 * not want a entity which is misbehaving to affect the scheduling of all
300 * other entities.
301 * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
302 * is used, in order to confine each entity within its own bandwidth.
303 *
304 * This function deals exactly with that, and ensures that when the runtime
305 * of a entity is replenished, its deadline is also postponed. That ensures
306 * the overrunning entity can't interfere with other entity in the system and
307 * can't make them miss their deadlines. Reasons why this kind of overruns
308 * could happen are, typically, a entity voluntarily trying to overcome its
xiaofeng.yan1b09d292014-07-07 05:59:04 +0000309 * runtime, or it just underestimated it during sched_setattr().
Dario Faggioliaab03e02013-11-28 11:14:43 +0100310 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100311static void replenish_dl_entity(struct sched_dl_entity *dl_se,
312 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100313{
314 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
315 struct rq *rq = rq_of_dl_rq(dl_rq);
316
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100317 BUG_ON(pi_se->dl_runtime <= 0);
318
319 /*
320 * This could be the case for a !-dl task that is boosted.
321 * Just go with full inherited parameters.
322 */
323 if (dl_se->dl_deadline == 0) {
324 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
325 dl_se->runtime = pi_se->dl_runtime;
326 }
327
Dario Faggioliaab03e02013-11-28 11:14:43 +0100328 /*
329 * We keep moving the deadline away until we get some
330 * available runtime for the entity. This ensures correct
331 * handling of situations where the runtime overrun is
332 * arbitrary large.
333 */
334 while (dl_se->runtime <= 0) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100335 dl_se->deadline += pi_se->dl_period;
336 dl_se->runtime += pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100337 }
338
339 /*
340 * At this point, the deadline really should be "in
341 * the future" with respect to rq->clock. If it's
342 * not, we are, for some reason, lagging too much!
343 * Anyway, after having warn userspace abut that,
344 * we still try to keep the things running by
345 * resetting the deadline and the budget of the
346 * entity.
347 */
348 if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
John Stultzc2248152014-06-04 16:11:41 -0700349 printk_deferred_once("sched: DL replenish lagged to much\n");
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100350 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
351 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100352 }
353}
354
355/*
356 * Here we check if --at time t-- an entity (which is probably being
357 * [re]activated or, in general, enqueued) can use its remaining runtime
358 * and its current deadline _without_ exceeding the bandwidth it is
359 * assigned (function returns true if it can't). We are in fact applying
360 * one of the CBS rules: when a task wakes up, if the residual runtime
361 * over residual deadline fits within the allocated bandwidth, then we
362 * can keep the current (absolute) deadline and residual budget without
363 * disrupting the schedulability of the system. Otherwise, we should
364 * refill the runtime and set the deadline a period in the future,
365 * because keeping the current (absolute) deadline of the task would
Dario Faggioli712e5e32014-01-27 12:20:15 +0100366 * result in breaking guarantees promised to other tasks (refer to
367 * Documentation/scheduler/sched-deadline.txt for more informations).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100368 *
369 * This function returns true if:
370 *
Harald Gustafsson755378a2013-11-07 14:43:40 +0100371 * runtime / (deadline - t) > dl_runtime / dl_period ,
Dario Faggioliaab03e02013-11-28 11:14:43 +0100372 *
373 * IOW we can't recycle current parameters.
Harald Gustafsson755378a2013-11-07 14:43:40 +0100374 *
375 * Notice that the bandwidth check is done against the period. For
376 * task with deadline equal to period this is the same of using
377 * dl_deadline instead of dl_period in the equation above.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100378 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100379static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
380 struct sched_dl_entity *pi_se, u64 t)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100381{
382 u64 left, right;
383
384 /*
385 * left and right are the two sides of the equation above,
386 * after a bit of shuffling to use multiplications instead
387 * of divisions.
388 *
389 * Note that none of the time values involved in the two
390 * multiplications are absolute: dl_deadline and dl_runtime
391 * are the relative deadline and the maximum runtime of each
392 * instance, runtime is the runtime left for the last instance
393 * and (deadline - t), since t is rq->clock, is the time left
394 * to the (absolute) deadline. Even if overflowing the u64 type
395 * is very unlikely to occur in both cases, here we scale down
396 * as we want to avoid that risk at all. Scaling down by 10
397 * means that we reduce granularity to 1us. We are fine with it,
398 * since this is only a true/false check and, anyway, thinking
399 * of anything below microseconds resolution is actually fiction
400 * (but still we want to give the user that illusion >;).
401 */
Dario Faggioli332ac172013-11-07 14:43:45 +0100402 left = (pi_se->dl_period >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
403 right = ((dl_se->deadline - t) >> DL_SCALE) *
404 (pi_se->dl_runtime >> DL_SCALE);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100405
406 return dl_time_before(right, left);
407}
408
409/*
410 * When a -deadline entity is queued back on the runqueue, its runtime and
411 * deadline might need updating.
412 *
413 * The policy here is that we update the deadline of the entity only if:
414 * - the current deadline is in the past,
415 * - using the remaining runtime with the current deadline would make
416 * the entity exceed its bandwidth.
417 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100418static void update_dl_entity(struct sched_dl_entity *dl_se,
419 struct sched_dl_entity *pi_se)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100420{
421 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
422 struct rq *rq = rq_of_dl_rq(dl_rq);
423
424 /*
425 * The arrival of a new instance needs special treatment, i.e.,
426 * the actual scheduling parameters have to be "renewed".
427 */
428 if (dl_se->dl_new) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100429 setup_new_dl_entity(dl_se, pi_se);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100430 return;
431 }
432
433 if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100434 dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
435 dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
436 dl_se->runtime = pi_se->dl_runtime;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100437 }
438}
439
440/*
441 * If the entity depleted all its runtime, and if we want it to sleep
442 * while waiting for some new execution time to become available, we
443 * set the bandwidth enforcement timer to the replenishment instant
444 * and try to activate it.
445 *
446 * Notice that it is important for the caller to know if the timer
447 * actually started or not (i.e., the replenishment instant is in
448 * the future or in the past).
449 */
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100450static int start_dl_timer(struct sched_dl_entity *dl_se, bool boosted)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100451{
452 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
453 struct rq *rq = rq_of_dl_rq(dl_rq);
454 ktime_t now, act;
455 ktime_t soft, hard;
456 unsigned long range;
457 s64 delta;
458
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100459 if (boosted)
460 return 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100461 /*
462 * We want the timer to fire at the deadline, but considering
463 * that it is actually coming from rq->clock and not from
464 * hrtimer's time base reading.
465 */
466 act = ns_to_ktime(dl_se->deadline);
467 now = hrtimer_cb_get_time(&dl_se->dl_timer);
468 delta = ktime_to_ns(now) - rq_clock(rq);
469 act = ktime_add_ns(act, delta);
470
471 /*
472 * If the expiry time already passed, e.g., because the value
473 * chosen as the deadline is too small, don't even try to
474 * start the timer in the past!
475 */
476 if (ktime_us_delta(act, now) < 0)
477 return 0;
478
479 hrtimer_set_expires(&dl_se->dl_timer, act);
480
481 soft = hrtimer_get_softexpires(&dl_se->dl_timer);
482 hard = hrtimer_get_expires(&dl_se->dl_timer);
483 range = ktime_to_ns(ktime_sub(hard, soft));
484 __hrtimer_start_range_ns(&dl_se->dl_timer, soft,
485 range, HRTIMER_MODE_ABS, 0);
486
487 return hrtimer_active(&dl_se->dl_timer);
488}
489
490/*
491 * This is the bandwidth enforcement timer callback. If here, we know
492 * a task is not on its dl_rq, since the fact that the timer was running
493 * means the task is throttled and needs a runtime replenishment.
494 *
495 * However, what we actually do depends on the fact the task is active,
496 * (it is on its rq) or has been removed from there by a call to
497 * dequeue_task_dl(). In the former case we must issue the runtime
498 * replenishment and add the task back to the dl_rq; in the latter, we just
499 * do nothing but clearing dl_throttled, so that runtime and deadline
500 * updating (and the queueing back to dl_rq) will be done by the
501 * next call to enqueue_task_dl().
502 */
503static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
504{
505 struct sched_dl_entity *dl_se = container_of(timer,
506 struct sched_dl_entity,
507 dl_timer);
508 struct task_struct *p = dl_task_of(dl_se);
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400509 struct rq *rq;
510again:
511 rq = task_rq(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100512 raw_spin_lock(&rq->lock);
513
Kirill Tkhai0f397f22014-05-20 13:33:42 +0400514 if (rq != task_rq(p)) {
515 /* Task was moved, retrying. */
516 raw_spin_unlock(&rq->lock);
517 goto again;
518 }
519
Dario Faggioliaab03e02013-11-28 11:14:43 +0100520 /*
Juri Lelliaee38ea2014-10-24 10:16:38 +0100521 * We need to take care of several possible races here:
522 *
523 * - the task might have changed its scheduling policy
524 * to something different than SCHED_DEADLINE
525 * - the task might have changed its reservation parameters
526 * (through sched_setattr())
527 * - the task might have been boosted by someone else and
528 * might be in the boosting/deboosting path
529 *
530 * In all this cases we bail out, as the task is already
531 * in the runqueue or is going to be enqueued back anyway.
Dario Faggioliaab03e02013-11-28 11:14:43 +0100532 */
Juri Lelliaee38ea2014-10-24 10:16:38 +0100533 if (!dl_task(p) || dl_se->dl_new ||
534 dl_se->dl_boosted || !dl_se->dl_throttled)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100535 goto unlock;
536
537 sched_clock_tick();
538 update_rq_clock(rq);
539 dl_se->dl_throttled = 0;
Juri Lelli5bfd1262014-04-15 13:49:04 +0200540 dl_se->dl_yielded = 0;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +0400541 if (task_on_rq_queued(p)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +0100542 enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
Kirill Tkhaif3a7e1a2014-10-21 20:35:56 +0400543 if (dl_task(rq->curr))
Dario Faggioliaab03e02013-11-28 11:14:43 +0100544 check_preempt_curr_dl(rq, p, 0);
545 else
Kirill Tkhai88751252014-06-29 00:03:57 +0400546 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100547#ifdef CONFIG_SMP
548 /*
549 * Queueing this task back might have overloaded rq,
550 * check if we need to kick someone away.
551 */
552 if (has_pushable_dl_tasks(rq))
553 push_dl_task(rq);
554#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +0100555 }
556unlock:
557 raw_spin_unlock(&rq->lock);
558
559 return HRTIMER_NORESTART;
560}
561
562void init_dl_task_timer(struct sched_dl_entity *dl_se)
563{
564 struct hrtimer *timer = &dl_se->dl_timer;
565
Dario Faggioliaab03e02013-11-28 11:14:43 +0100566 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
567 timer->function = dl_task_timer;
568}
569
570static
571int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
572{
573 int dmiss = dl_time_before(dl_se->deadline, rq_clock(rq));
574 int rorun = dl_se->runtime <= 0;
575
576 if (!rorun && !dmiss)
577 return 0;
578
579 /*
580 * If we are beyond our current deadline and we are still
581 * executing, then we have already used some of the runtime of
582 * the next instance. Thus, if we do not account that, we are
583 * stealing bandwidth from the system at each deadline miss!
584 */
585 if (dmiss) {
586 dl_se->runtime = rorun ? dl_se->runtime : 0;
587 dl_se->runtime -= rq_clock(rq) - dl_se->deadline;
588 }
589
590 return 1;
591}
592
Juri Lellifaa59932014-02-21 11:37:15 +0100593extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
594
Dario Faggioliaab03e02013-11-28 11:14:43 +0100595/*
596 * Update the current task's runtime statistics (provided it is still
597 * a -deadline task and has not been removed from the dl_rq).
598 */
599static void update_curr_dl(struct rq *rq)
600{
601 struct task_struct *curr = rq->curr;
602 struct sched_dl_entity *dl_se = &curr->dl;
603 u64 delta_exec;
604
605 if (!dl_task(curr) || !on_dl_rq(dl_se))
606 return;
607
608 /*
609 * Consumed budget is computed considering the time as
610 * observed by schedulable tasks (excluding time spent
611 * in hardirq context, etc.). Deadlines are instead
612 * computed using hard walltime. This seems to be the more
613 * natural solution, but the full ramifications of this
614 * approach need further study.
615 */
616 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
Kirill Tkhai734ff2a2014-03-04 19:25:46 +0400617 if (unlikely((s64)delta_exec <= 0))
618 return;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100619
620 schedstat_set(curr->se.statistics.exec_max,
621 max(curr->se.statistics.exec_max, delta_exec));
622
623 curr->se.sum_exec_runtime += delta_exec;
624 account_group_exec_runtime(curr, delta_exec);
625
626 curr->se.exec_start = rq_clock_task(rq);
627 cpuacct_charge(curr, delta_exec);
628
Dario Faggioli239be4a2013-11-07 14:43:39 +0100629 sched_rt_avg_update(rq, delta_exec);
630
Wanpeng Li80496882014-10-31 06:39:32 +0800631 dl_se->runtime -= dl_se->dl_yielded ? 0 : delta_exec;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100632 if (dl_runtime_exceeded(rq, dl_se)) {
633 __dequeue_task_dl(rq, curr, 0);
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100634 if (likely(start_dl_timer(dl_se, curr->dl.dl_boosted)))
Dario Faggioliaab03e02013-11-28 11:14:43 +0100635 dl_se->dl_throttled = 1;
636 else
637 enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
638
639 if (!is_leftmost(curr, &rq->dl))
Kirill Tkhai88751252014-06-29 00:03:57 +0400640 resched_curr(rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100641 }
Peter Zijlstra17248132013-12-17 12:44:49 +0100642
643 /*
644 * Because -- for now -- we share the rt bandwidth, we need to
645 * account our runtime there too, otherwise actual rt tasks
646 * would be able to exceed the shared quota.
647 *
648 * Account to the root rt group for now.
649 *
650 * The solution we're working towards is having the RT groups scheduled
651 * using deadline servers -- however there's a few nasties to figure
652 * out before that can happen.
653 */
654 if (rt_bandwidth_enabled()) {
655 struct rt_rq *rt_rq = &rq->rt;
656
657 raw_spin_lock(&rt_rq->rt_runtime_lock);
Peter Zijlstra17248132013-12-17 12:44:49 +0100658 /*
659 * We'll let actual RT tasks worry about the overflow here, we
Juri Lellifaa59932014-02-21 11:37:15 +0100660 * have our own CBS to keep us inline; only account when RT
661 * bandwidth is relevant.
Peter Zijlstra17248132013-12-17 12:44:49 +0100662 */
Juri Lellifaa59932014-02-21 11:37:15 +0100663 if (sched_rt_bandwidth_account(rt_rq))
664 rt_rq->rt_time += delta_exec;
Peter Zijlstra17248132013-12-17 12:44:49 +0100665 raw_spin_unlock(&rt_rq->rt_runtime_lock);
666 }
Dario Faggioliaab03e02013-11-28 11:14:43 +0100667}
668
Juri Lelli1baca4c2013-11-07 14:43:38 +0100669#ifdef CONFIG_SMP
670
671static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
672
673static inline u64 next_deadline(struct rq *rq)
674{
675 struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
676
677 if (next && dl_prio(next->prio))
678 return next->dl.deadline;
679 else
680 return 0;
681}
682
683static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
684{
685 struct rq *rq = rq_of_dl_rq(dl_rq);
686
687 if (dl_rq->earliest_dl.curr == 0 ||
688 dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
689 /*
690 * If the dl_rq had no -deadline tasks, or if the new task
691 * has shorter deadline than the current one on dl_rq, we
692 * know that the previous earliest becomes our next earliest,
693 * as the new task becomes the earliest itself.
694 */
695 dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
696 dl_rq->earliest_dl.curr = deadline;
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100697 cpudl_set(&rq->rd->cpudl, rq->cpu, deadline, 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100698 } else if (dl_rq->earliest_dl.next == 0 ||
699 dl_time_before(deadline, dl_rq->earliest_dl.next)) {
700 /*
701 * On the other hand, if the new -deadline task has a
702 * a later deadline than the earliest one on dl_rq, but
703 * it is earlier than the next (if any), we must
704 * recompute the next-earliest.
705 */
706 dl_rq->earliest_dl.next = next_deadline(rq);
707 }
708}
709
710static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
711{
712 struct rq *rq = rq_of_dl_rq(dl_rq);
713
714 /*
715 * Since we may have removed our earliest (and/or next earliest)
716 * task we must recompute them.
717 */
718 if (!dl_rq->dl_nr_running) {
719 dl_rq->earliest_dl.curr = 0;
720 dl_rq->earliest_dl.next = 0;
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100721 cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100722 } else {
723 struct rb_node *leftmost = dl_rq->rb_leftmost;
724 struct sched_dl_entity *entry;
725
726 entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
727 dl_rq->earliest_dl.curr = entry->deadline;
728 dl_rq->earliest_dl.next = next_deadline(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100729 cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline, 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100730 }
731}
732
733#else
734
735static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
736static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
737
738#endif /* CONFIG_SMP */
739
740static inline
741void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
742{
743 int prio = dl_task_of(dl_se)->prio;
744 u64 deadline = dl_se->deadline;
745
746 WARN_ON(!dl_prio(prio));
747 dl_rq->dl_nr_running++;
Kirill Tkhai72465442014-05-09 03:00:14 +0400748 add_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100749
750 inc_dl_deadline(dl_rq, deadline);
751 inc_dl_migration(dl_se, dl_rq);
752}
753
754static inline
755void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
756{
757 int prio = dl_task_of(dl_se)->prio;
758
759 WARN_ON(!dl_prio(prio));
760 WARN_ON(!dl_rq->dl_nr_running);
761 dl_rq->dl_nr_running--;
Kirill Tkhai72465442014-05-09 03:00:14 +0400762 sub_nr_running(rq_of_dl_rq(dl_rq), 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100763
764 dec_dl_deadline(dl_rq, dl_se->deadline);
765 dec_dl_migration(dl_se, dl_rq);
766}
767
Dario Faggioliaab03e02013-11-28 11:14:43 +0100768static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
769{
770 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
771 struct rb_node **link = &dl_rq->rb_root.rb_node;
772 struct rb_node *parent = NULL;
773 struct sched_dl_entity *entry;
774 int leftmost = 1;
775
776 BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
777
778 while (*link) {
779 parent = *link;
780 entry = rb_entry(parent, struct sched_dl_entity, rb_node);
781 if (dl_time_before(dl_se->deadline, entry->deadline))
782 link = &parent->rb_left;
783 else {
784 link = &parent->rb_right;
785 leftmost = 0;
786 }
787 }
788
789 if (leftmost)
790 dl_rq->rb_leftmost = &dl_se->rb_node;
791
792 rb_link_node(&dl_se->rb_node, parent, link);
793 rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
794
Juri Lelli1baca4c2013-11-07 14:43:38 +0100795 inc_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100796}
797
798static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
799{
800 struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
801
802 if (RB_EMPTY_NODE(&dl_se->rb_node))
803 return;
804
805 if (dl_rq->rb_leftmost == &dl_se->rb_node) {
806 struct rb_node *next_node;
807
808 next_node = rb_next(&dl_se->rb_node);
809 dl_rq->rb_leftmost = next_node;
810 }
811
812 rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
813 RB_CLEAR_NODE(&dl_se->rb_node);
814
Juri Lelli1baca4c2013-11-07 14:43:38 +0100815 dec_dl_tasks(dl_se, dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100816}
817
818static void
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100819enqueue_dl_entity(struct sched_dl_entity *dl_se,
820 struct sched_dl_entity *pi_se, int flags)
Dario Faggioliaab03e02013-11-28 11:14:43 +0100821{
822 BUG_ON(on_dl_rq(dl_se));
823
824 /*
825 * If this is a wakeup or a new instance, the scheduling
826 * parameters of the task might need updating. Otherwise,
827 * we want a replenishment of its runtime.
828 */
829 if (!dl_se->dl_new && flags & ENQUEUE_REPLENISH)
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100830 replenish_dl_entity(dl_se, pi_se);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100831 else
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100832 update_dl_entity(dl_se, pi_se);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100833
834 __enqueue_dl_entity(dl_se);
835}
836
837static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
838{
839 __dequeue_dl_entity(dl_se);
840}
841
842static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
843{
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100844 struct task_struct *pi_task = rt_mutex_get_top_task(p);
845 struct sched_dl_entity *pi_se = &p->dl;
846
847 /*
848 * Use the scheduling parameters of the top pi-waiter
849 * task if we have one and its (relative) deadline is
850 * smaller than our one... OTW we keep our runtime and
851 * deadline.
852 */
Juri Lelli64be6f12014-10-24 10:16:37 +0100853 if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100854 pi_se = &pi_task->dl;
Juri Lelli64be6f12014-10-24 10:16:37 +0100855 } else if (!dl_prio(p->normal_prio)) {
856 /*
857 * Special case in which we have a !SCHED_DEADLINE task
858 * that is going to be deboosted, but exceedes its
859 * runtime while doing so. No point in replenishing
860 * it, as it's going to return back to its original
861 * scheduling class after this.
862 */
863 BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
864 return;
865 }
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100866
Dario Faggioliaab03e02013-11-28 11:14:43 +0100867 /*
868 * If p is throttled, we do nothing. In fact, if it exhausted
869 * its budget it needs a replenishment and, since it now is on
870 * its rq, the bandwidth timer callback (which clearly has not
871 * run yet) will take care of this.
872 */
873 if (p->dl.dl_throttled)
874 return;
875
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100876 enqueue_dl_entity(&p->dl, pi_se, flags);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100877
878 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
879 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100880}
881
882static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
883{
884 dequeue_dl_entity(&p->dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100885 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100886}
887
888static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
889{
890 update_curr_dl(rq);
891 __dequeue_task_dl(rq, p, flags);
Dario Faggioliaab03e02013-11-28 11:14:43 +0100892}
893
894/*
895 * Yield task semantic for -deadline tasks is:
896 *
897 * get off from the CPU until our next instance, with
898 * a new runtime. This is of little use now, since we
899 * don't have a bandwidth reclaiming mechanism. Anyway,
900 * bandwidth reclaiming is planned for the future, and
901 * yield_task_dl will indicate that some spare budget
902 * is available for other task instances to use it.
903 */
904static void yield_task_dl(struct rq *rq)
905{
906 struct task_struct *p = rq->curr;
907
908 /*
909 * We make the task go to sleep until its current deadline by
910 * forcing its runtime to zero. This way, update_curr_dl() stops
911 * it and the bandwidth timer will wake it up and will give it
Juri Lelli5bfd1262014-04-15 13:49:04 +0200912 * new scheduling parameters (thanks to dl_yielded=1).
Dario Faggioliaab03e02013-11-28 11:14:43 +0100913 */
914 if (p->dl.runtime > 0) {
Juri Lelli5bfd1262014-04-15 13:49:04 +0200915 rq->curr->dl.dl_yielded = 1;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100916 p->dl.runtime = 0;
917 }
918 update_curr_dl(rq);
919}
920
Juri Lelli1baca4c2013-11-07 14:43:38 +0100921#ifdef CONFIG_SMP
922
923static int find_later_rq(struct task_struct *task);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100924
925static int
926select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
927{
928 struct task_struct *curr;
929 struct rq *rq;
930
Wanpeng Lif4e9d94a2014-10-14 10:22:40 +0800931 if (p->nr_cpus_allowed == 1)
932 goto out;
933
Wanpeng Li1d7e9742014-10-14 10:22:39 +0800934 if (sd_flag != SD_BALANCE_WAKE)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100935 goto out;
936
937 rq = cpu_rq(cpu);
938
939 rcu_read_lock();
940 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
941
942 /*
943 * If we are dealing with a -deadline task, we must
944 * decide where to wake it up.
945 * If it has a later deadline and the current task
946 * on this rq can't move (provided the waking task
947 * can!) we prefer to send it somewhere else. On the
948 * other hand, if it has a shorter deadline, we
949 * try to make it stay here, it might be important.
950 */
951 if (unlikely(dl_task(curr)) &&
952 (curr->nr_cpus_allowed < 2 ||
953 !dl_entity_preempt(&p->dl, &curr->dl)) &&
954 (p->nr_cpus_allowed > 1)) {
955 int target = find_later_rq(p);
956
957 if (target != -1)
958 cpu = target;
959 }
960 rcu_read_unlock();
961
962out:
963 return cpu;
964}
965
966static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
967{
968 /*
969 * Current can't be migrated, useless to reschedule,
970 * let's hope p can move out.
971 */
972 if (rq->curr->nr_cpus_allowed == 1 ||
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100973 cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100974 return;
975
976 /*
977 * p is migratable, so let's not schedule it and
978 * see if it is pushed or pulled somewhere else.
979 */
980 if (p->nr_cpus_allowed != 1 &&
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100981 cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
Juri Lelli1baca4c2013-11-07 14:43:38 +0100982 return;
983
Kirill Tkhai88751252014-06-29 00:03:57 +0400984 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +0100985}
986
Peter Zijlstra38033c32014-01-23 20:32:21 +0100987static int pull_dl_task(struct rq *this_rq);
988
Juri Lelli1baca4c2013-11-07 14:43:38 +0100989#endif /* CONFIG_SMP */
990
Dario Faggioliaab03e02013-11-28 11:14:43 +0100991/*
992 * Only called when both the current and waking task are -deadline
993 * tasks.
994 */
995static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
996 int flags)
997{
Juri Lelli1baca4c2013-11-07 14:43:38 +0100998 if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400999 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001000 return;
1001 }
1002
1003#ifdef CONFIG_SMP
1004 /*
1005 * In the unlikely case current and p have the same deadline
1006 * let us try to decide what's the best thing to do...
1007 */
Dario Faggioli332ac172013-11-07 14:43:45 +01001008 if ((p->dl.deadline == rq->curr->dl.deadline) &&
1009 !test_tsk_need_resched(rq->curr))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001010 check_preempt_equal_dl(rq, p);
1011#endif /* CONFIG_SMP */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001012}
1013
1014#ifdef CONFIG_SCHED_HRTICK
1015static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1016{
xiaofeng.yan177ef2a2014-08-26 03:15:41 +00001017 hrtick_start(rq, p->dl.runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001018}
1019#endif
1020
1021static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1022 struct dl_rq *dl_rq)
1023{
1024 struct rb_node *left = dl_rq->rb_leftmost;
1025
1026 if (!left)
1027 return NULL;
1028
1029 return rb_entry(left, struct sched_dl_entity, rb_node);
1030}
1031
Peter Zijlstra606dba22012-02-11 06:05:00 +01001032struct task_struct *pick_next_task_dl(struct rq *rq, struct task_struct *prev)
Dario Faggioliaab03e02013-11-28 11:14:43 +01001033{
1034 struct sched_dl_entity *dl_se;
1035 struct task_struct *p;
1036 struct dl_rq *dl_rq;
1037
1038 dl_rq = &rq->dl;
1039
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001040 if (need_pull_dl_task(rq, prev)) {
Peter Zijlstra38033c32014-01-23 20:32:21 +01001041 pull_dl_task(rq);
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001042 /*
1043 * pull_rt_task() can drop (and re-acquire) rq->lock; this
1044 * means a stop task can slip in, in which case we need to
1045 * re-start task selection.
1046 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001047 if (rq->stop && task_on_rq_queued(rq->stop))
Kirill Tkhaia1d9a322014-04-10 17:38:36 +04001048 return RETRY_TASK;
1049 }
1050
Kirill Tkhai734ff2a2014-03-04 19:25:46 +04001051 /*
1052 * When prev is DL, we may throttle it in put_prev_task().
1053 * So, we update time before we check for dl_nr_running.
1054 */
1055 if (prev->sched_class == &dl_sched_class)
1056 update_curr_dl(rq);
Peter Zijlstra38033c32014-01-23 20:32:21 +01001057
Dario Faggioliaab03e02013-11-28 11:14:43 +01001058 if (unlikely(!dl_rq->dl_nr_running))
1059 return NULL;
1060
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001061 put_prev_task(rq, prev);
Peter Zijlstra606dba22012-02-11 06:05:00 +01001062
Dario Faggioliaab03e02013-11-28 11:14:43 +01001063 dl_se = pick_next_dl_entity(rq, dl_rq);
1064 BUG_ON(!dl_se);
1065
1066 p = dl_task_of(dl_se);
1067 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001068
1069 /* Running task will never be pushed. */
Juri Lelli71362652014-01-14 12:03:51 +01001070 dequeue_pushable_dl_task(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001071
Dario Faggioliaab03e02013-11-28 11:14:43 +01001072#ifdef CONFIG_SCHED_HRTICK
1073 if (hrtick_enabled(rq))
1074 start_hrtick_dl(rq, p);
1075#endif
Juri Lelli1baca4c2013-11-07 14:43:38 +01001076
Peter Zijlstradc877342014-02-12 15:47:29 +01001077 set_post_schedule(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001078
Dario Faggioliaab03e02013-11-28 11:14:43 +01001079 return p;
1080}
1081
1082static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1083{
1084 update_curr_dl(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001085
1086 if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
1087 enqueue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001088}
1089
1090static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1091{
1092 update_curr_dl(rq);
1093
1094#ifdef CONFIG_SCHED_HRTICK
1095 if (hrtick_enabled(rq) && queued && p->dl.runtime > 0)
1096 start_hrtick_dl(rq, p);
1097#endif
1098}
1099
1100static void task_fork_dl(struct task_struct *p)
1101{
1102 /*
1103 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1104 * sched_fork()
1105 */
1106}
1107
1108static void task_dead_dl(struct task_struct *p)
1109{
1110 struct hrtimer *timer = &p->dl.dl_timer;
Dario Faggioli332ac172013-11-07 14:43:45 +01001111 struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1112
1113 /*
1114 * Since we are TASK_DEAD we won't slip out of the domain!
1115 */
1116 raw_spin_lock_irq(&dl_b->lock);
1117 dl_b->total_bw -= p->dl.dl_bw;
1118 raw_spin_unlock_irq(&dl_b->lock);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001119
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001120 hrtimer_cancel(timer);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001121}
1122
1123static void set_curr_task_dl(struct rq *rq)
1124{
1125 struct task_struct *p = rq->curr;
1126
1127 p->se.exec_start = rq_clock_task(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001128
1129 /* You can't push away the running task */
1130 dequeue_pushable_dl_task(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001131}
1132
Juri Lelli1baca4c2013-11-07 14:43:38 +01001133#ifdef CONFIG_SMP
1134
1135/* Only try algorithms three times */
1136#define DL_MAX_TRIES 3
1137
1138static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1139{
1140 if (!task_running(rq, p) &&
Kirill Tkhai1ba93d42014-09-12 17:42:20 +04001141 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001142 return 1;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001143 return 0;
1144}
1145
1146/* Returns the second earliest -deadline task, NULL otherwise */
1147static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
1148{
1149 struct rb_node *next_node = rq->dl.rb_leftmost;
1150 struct sched_dl_entity *dl_se;
1151 struct task_struct *p = NULL;
1152
1153next_node:
1154 next_node = rb_next(next_node);
1155 if (next_node) {
1156 dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
1157 p = dl_task_of(dl_se);
1158
1159 if (pick_dl_task(rq, p, cpu))
1160 return p;
1161
1162 goto next_node;
1163 }
1164
1165 return NULL;
1166}
1167
Juri Lelli1baca4c2013-11-07 14:43:38 +01001168static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1169
1170static int find_later_rq(struct task_struct *task)
1171{
1172 struct sched_domain *sd;
Christoph Lameter4ba29682014-08-26 19:12:21 -05001173 struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001174 int this_cpu = smp_processor_id();
1175 int best_cpu, cpu = task_cpu(task);
1176
1177 /* Make sure the mask is initialized first */
1178 if (unlikely(!later_mask))
1179 return -1;
1180
1181 if (task->nr_cpus_allowed == 1)
1182 return -1;
1183
Juri Lelli91ec6772014-09-19 10:22:41 +01001184 /*
1185 * We have to consider system topology and task affinity
1186 * first, then we can look for a suitable cpu.
1187 */
1188 cpumask_copy(later_mask, task_rq(task)->rd->span);
1189 cpumask_and(later_mask, later_mask, cpu_active_mask);
1190 cpumask_and(later_mask, later_mask, &task->cpus_allowed);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001191 best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
1192 task, later_mask);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001193 if (best_cpu == -1)
1194 return -1;
1195
1196 /*
1197 * If we are here, some target has been found,
1198 * the most suitable of which is cached in best_cpu.
1199 * This is, among the runqueues where the current tasks
1200 * have later deadlines than the task's one, the rq
1201 * with the latest possible one.
1202 *
1203 * Now we check how well this matches with task's
1204 * affinity and system topology.
1205 *
1206 * The last cpu where the task run is our first
1207 * guess, since it is most likely cache-hot there.
1208 */
1209 if (cpumask_test_cpu(cpu, later_mask))
1210 return cpu;
1211 /*
1212 * Check if this_cpu is to be skipped (i.e., it is
1213 * not in the mask) or not.
1214 */
1215 if (!cpumask_test_cpu(this_cpu, later_mask))
1216 this_cpu = -1;
1217
1218 rcu_read_lock();
1219 for_each_domain(cpu, sd) {
1220 if (sd->flags & SD_WAKE_AFFINE) {
1221
1222 /*
1223 * If possible, preempting this_cpu is
1224 * cheaper than migrating.
1225 */
1226 if (this_cpu != -1 &&
1227 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1228 rcu_read_unlock();
1229 return this_cpu;
1230 }
1231
1232 /*
1233 * Last chance: if best_cpu is valid and is
1234 * in the mask, that becomes our choice.
1235 */
1236 if (best_cpu < nr_cpu_ids &&
1237 cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
1238 rcu_read_unlock();
1239 return best_cpu;
1240 }
1241 }
1242 }
1243 rcu_read_unlock();
1244
1245 /*
1246 * At this point, all our guesses failed, we just return
1247 * 'something', and let the caller sort the things out.
1248 */
1249 if (this_cpu != -1)
1250 return this_cpu;
1251
1252 cpu = cpumask_any(later_mask);
1253 if (cpu < nr_cpu_ids)
1254 return cpu;
1255
1256 return -1;
1257}
1258
1259/* Locks the rq it finds */
1260static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
1261{
1262 struct rq *later_rq = NULL;
1263 int tries;
1264 int cpu;
1265
1266 for (tries = 0; tries < DL_MAX_TRIES; tries++) {
1267 cpu = find_later_rq(task);
1268
1269 if ((cpu == -1) || (cpu == rq->cpu))
1270 break;
1271
1272 later_rq = cpu_rq(cpu);
1273
1274 /* Retry if something changed. */
1275 if (double_lock_balance(rq, later_rq)) {
1276 if (unlikely(task_rq(task) != rq ||
1277 !cpumask_test_cpu(later_rq->cpu,
1278 &task->cpus_allowed) ||
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001279 task_running(rq, task) ||
1280 !task_on_rq_queued(task))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001281 double_unlock_balance(rq, later_rq);
1282 later_rq = NULL;
1283 break;
1284 }
1285 }
1286
1287 /*
1288 * If the rq we found has no -deadline task, or
1289 * its earliest one has a later deadline than our
1290 * task, the rq is a good one.
1291 */
1292 if (!later_rq->dl.dl_nr_running ||
1293 dl_time_before(task->dl.deadline,
1294 later_rq->dl.earliest_dl.curr))
1295 break;
1296
1297 /* Otherwise we try again. */
1298 double_unlock_balance(rq, later_rq);
1299 later_rq = NULL;
1300 }
1301
1302 return later_rq;
1303}
1304
1305static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
1306{
1307 struct task_struct *p;
1308
1309 if (!has_pushable_dl_tasks(rq))
1310 return NULL;
1311
1312 p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
1313 struct task_struct, pushable_dl_tasks);
1314
1315 BUG_ON(rq->cpu != task_cpu(p));
1316 BUG_ON(task_current(rq, p));
1317 BUG_ON(p->nr_cpus_allowed <= 1);
1318
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001319 BUG_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001320 BUG_ON(!dl_task(p));
1321
1322 return p;
1323}
1324
1325/*
1326 * See if the non running -deadline tasks on this rq
1327 * can be sent to some other CPU where they can preempt
1328 * and start executing.
1329 */
1330static int push_dl_task(struct rq *rq)
1331{
1332 struct task_struct *next_task;
1333 struct rq *later_rq;
1334
1335 if (!rq->dl.overloaded)
1336 return 0;
1337
1338 next_task = pick_next_pushable_dl_task(rq);
1339 if (!next_task)
1340 return 0;
1341
1342retry:
1343 if (unlikely(next_task == rq->curr)) {
1344 WARN_ON(1);
1345 return 0;
1346 }
1347
1348 /*
1349 * If next_task preempts rq->curr, and rq->curr
1350 * can move away, it makes sense to just reschedule
1351 * without going further in pushing next_task.
1352 */
1353 if (dl_task(rq->curr) &&
1354 dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
1355 rq->curr->nr_cpus_allowed > 1) {
Kirill Tkhai88751252014-06-29 00:03:57 +04001356 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001357 return 0;
1358 }
1359
1360 /* We might release rq lock */
1361 get_task_struct(next_task);
1362
1363 /* Will lock the rq it'll find */
1364 later_rq = find_lock_later_rq(next_task, rq);
1365 if (!later_rq) {
1366 struct task_struct *task;
1367
1368 /*
1369 * We must check all this again, since
1370 * find_lock_later_rq releases rq->lock and it is
1371 * then possible that next_task has migrated.
1372 */
1373 task = pick_next_pushable_dl_task(rq);
1374 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1375 /*
1376 * The task is still there. We don't try
1377 * again, some other cpu will pull it when ready.
1378 */
1379 dequeue_pushable_dl_task(rq, next_task);
1380 goto out;
1381 }
1382
1383 if (!task)
1384 /* No more tasks */
1385 goto out;
1386
1387 put_task_struct(next_task);
1388 next_task = task;
1389 goto retry;
1390 }
1391
1392 deactivate_task(rq, next_task, 0);
1393 set_task_cpu(next_task, later_rq->cpu);
1394 activate_task(later_rq, next_task, 0);
1395
Kirill Tkhai88751252014-06-29 00:03:57 +04001396 resched_curr(later_rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001397
1398 double_unlock_balance(rq, later_rq);
1399
1400out:
1401 put_task_struct(next_task);
1402
1403 return 1;
1404}
1405
1406static void push_dl_tasks(struct rq *rq)
1407{
1408 /* Terminates as it moves a -deadline task */
1409 while (push_dl_task(rq))
1410 ;
1411}
1412
1413static int pull_dl_task(struct rq *this_rq)
1414{
1415 int this_cpu = this_rq->cpu, ret = 0, cpu;
1416 struct task_struct *p;
1417 struct rq *src_rq;
1418 u64 dmin = LONG_MAX;
1419
1420 if (likely(!dl_overloaded(this_rq)))
1421 return 0;
1422
1423 /*
1424 * Match the barrier from dl_set_overloaded; this guarantees that if we
1425 * see overloaded we must also see the dlo_mask bit.
1426 */
1427 smp_rmb();
1428
1429 for_each_cpu(cpu, this_rq->rd->dlo_mask) {
1430 if (this_cpu == cpu)
1431 continue;
1432
1433 src_rq = cpu_rq(cpu);
1434
1435 /*
1436 * It looks racy, abd it is! However, as in sched_rt.c,
1437 * we are fine with this.
1438 */
1439 if (this_rq->dl.dl_nr_running &&
1440 dl_time_before(this_rq->dl.earliest_dl.curr,
1441 src_rq->dl.earliest_dl.next))
1442 continue;
1443
1444 /* Might drop this_rq->lock */
1445 double_lock_balance(this_rq, src_rq);
1446
1447 /*
1448 * If there are no more pullable tasks on the
1449 * rq, we're done with it.
1450 */
1451 if (src_rq->dl.dl_nr_running <= 1)
1452 goto skip;
1453
1454 p = pick_next_earliest_dl_task(src_rq, this_cpu);
1455
1456 /*
1457 * We found a task to be pulled if:
1458 * - it preempts our current (if there's one),
1459 * - it will preempt the last one we pulled (if any).
1460 */
1461 if (p && dl_time_before(p->dl.deadline, dmin) &&
1462 (!this_rq->dl.dl_nr_running ||
1463 dl_time_before(p->dl.deadline,
1464 this_rq->dl.earliest_dl.curr))) {
1465 WARN_ON(p == src_rq->curr);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001466 WARN_ON(!task_on_rq_queued(p));
Juri Lelli1baca4c2013-11-07 14:43:38 +01001467
1468 /*
1469 * Then we pull iff p has actually an earlier
1470 * deadline than the current task of its runqueue.
1471 */
1472 if (dl_time_before(p->dl.deadline,
1473 src_rq->curr->dl.deadline))
1474 goto skip;
1475
1476 ret = 1;
1477
1478 deactivate_task(src_rq, p, 0);
1479 set_task_cpu(p, this_cpu);
1480 activate_task(this_rq, p, 0);
1481 dmin = p->dl.deadline;
1482
1483 /* Is there any other task even earlier? */
1484 }
1485skip:
1486 double_unlock_balance(this_rq, src_rq);
1487 }
1488
1489 return ret;
1490}
1491
Juri Lelli1baca4c2013-11-07 14:43:38 +01001492static void post_schedule_dl(struct rq *rq)
1493{
1494 push_dl_tasks(rq);
1495}
1496
1497/*
1498 * Since the task is not running and a reschedule is not going to happen
1499 * anytime soon on its runqueue, we try pushing it away now.
1500 */
1501static void task_woken_dl(struct rq *rq, struct task_struct *p)
1502{
1503 if (!task_running(rq, p) &&
1504 !test_tsk_need_resched(rq->curr) &&
1505 has_pushable_dl_tasks(rq) &&
1506 p->nr_cpus_allowed > 1 &&
1507 dl_task(rq->curr) &&
1508 (rq->curr->nr_cpus_allowed < 2 ||
Wanpeng Li6b0a5632014-10-31 06:39:34 +08001509 !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001510 push_dl_tasks(rq);
1511 }
1512}
1513
1514static void set_cpus_allowed_dl(struct task_struct *p,
1515 const struct cpumask *new_mask)
1516{
1517 struct rq *rq;
Juri Lelli7f514122014-09-19 10:22:40 +01001518 struct root_domain *src_rd;
Juri Lelli1baca4c2013-11-07 14:43:38 +01001519 int weight;
1520
1521 BUG_ON(!dl_task(p));
1522
Juri Lelli7f514122014-09-19 10:22:40 +01001523 rq = task_rq(p);
1524 src_rd = rq->rd;
1525 /*
1526 * Migrating a SCHED_DEADLINE task between exclusive
1527 * cpusets (different root_domains) entails a bandwidth
1528 * update. We already made space for us in the destination
1529 * domain (see cpuset_can_attach()).
1530 */
1531 if (!cpumask_intersects(src_rd->span, new_mask)) {
1532 struct dl_bw *src_dl_b;
1533
1534 src_dl_b = dl_bw_of(cpu_of(rq));
1535 /*
1536 * We now free resources of the root_domain we are migrating
1537 * off. In the worst case, sched_setattr() may temporary fail
1538 * until we complete the update.
1539 */
1540 raw_spin_lock(&src_dl_b->lock);
1541 __dl_clear(src_dl_b, p->dl.dl_bw);
1542 raw_spin_unlock(&src_dl_b->lock);
1543 }
1544
Juri Lelli1baca4c2013-11-07 14:43:38 +01001545 /*
1546 * Update only if the task is actually running (i.e.,
1547 * it is on the rq AND it is not throttled).
1548 */
1549 if (!on_dl_rq(&p->dl))
1550 return;
1551
1552 weight = cpumask_weight(new_mask);
1553
1554 /*
1555 * Only update if the process changes its state from whether it
1556 * can migrate or not.
1557 */
1558 if ((p->nr_cpus_allowed > 1) == (weight > 1))
1559 return;
1560
Juri Lelli1baca4c2013-11-07 14:43:38 +01001561 /*
1562 * The process used to be able to migrate OR it can now migrate
1563 */
1564 if (weight <= 1) {
1565 if (!task_current(rq, p))
1566 dequeue_pushable_dl_task(rq, p);
1567 BUG_ON(!rq->dl.dl_nr_migratory);
1568 rq->dl.dl_nr_migratory--;
1569 } else {
1570 if (!task_current(rq, p))
1571 enqueue_pushable_dl_task(rq, p);
1572 rq->dl.dl_nr_migratory++;
1573 }
1574
1575 update_dl_migration(&rq->dl);
1576}
1577
1578/* Assumes rq->lock is held */
1579static void rq_online_dl(struct rq *rq)
1580{
1581 if (rq->dl.overloaded)
1582 dl_set_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001583
1584 if (rq->dl.dl_nr_running > 0)
1585 cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr, 1);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001586}
1587
1588/* Assumes rq->lock is held */
1589static void rq_offline_dl(struct rq *rq)
1590{
1591 if (rq->dl.overloaded)
1592 dl_clear_overload(rq);
Juri Lelli6bfd6d72013-11-07 14:43:47 +01001593
1594 cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001595}
1596
1597void init_sched_dl_class(void)
1598{
1599 unsigned int i;
1600
1601 for_each_possible_cpu(i)
1602 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
1603 GFP_KERNEL, cpu_to_node(i));
1604}
1605
1606#endif /* CONFIG_SMP */
1607
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001608/*
1609 * Ensure p's dl_timer is cancelled. May drop rq->lock for a while.
1610 */
1611static void cancel_dl_timer(struct rq *rq, struct task_struct *p)
1612{
1613 struct hrtimer *dl_timer = &p->dl.dl_timer;
1614
1615 /* Nobody will change task's class if pi_lock is held */
1616 lockdep_assert_held(&p->pi_lock);
1617
1618 if (hrtimer_active(dl_timer)) {
1619 int ret = hrtimer_try_to_cancel(dl_timer);
1620
1621 if (unlikely(ret == -1)) {
1622 /*
1623 * Note, p may migrate OR new deadline tasks
1624 * may appear in rq when we are unlocking it.
1625 * A caller of us must be fine with that.
1626 */
1627 raw_spin_unlock(&rq->lock);
1628 hrtimer_cancel(dl_timer);
1629 raw_spin_lock(&rq->lock);
1630 }
1631 }
1632}
1633
Dario Faggioliaab03e02013-11-28 11:14:43 +01001634static void switched_from_dl(struct rq *rq, struct task_struct *p)
1635{
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001636 cancel_dl_timer(rq, p);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001637
Juri Lellia5e7be32014-09-19 10:22:39 +01001638 __dl_clear_params(p);
1639
Juri Lelli1baca4c2013-11-07 14:43:38 +01001640#ifdef CONFIG_SMP
1641 /*
1642 * Since this might be the only -deadline task on the rq,
1643 * this is the right place to try to pull some other one
1644 * from an overloaded cpu, if any.
1645 */
Wanpeng Licd660912014-10-31 06:39:35 +08001646 if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
1647 return;
1648
1649 if (pull_dl_task(rq))
1650 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001651#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +01001652}
1653
Juri Lelli1baca4c2013-11-07 14:43:38 +01001654/*
1655 * When switching to -deadline, we may overload the rq, then
1656 * we try to push someone off, if possible.
1657 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001658static void switched_to_dl(struct rq *rq, struct task_struct *p)
1659{
Juri Lelli1baca4c2013-11-07 14:43:38 +01001660 int check_resched = 1;
1661
Dario Faggioliaab03e02013-11-28 11:14:43 +01001662 /*
1663 * If p is throttled, don't consider the possibility
1664 * of preempting rq->curr, the check will be done right
1665 * after its runtime will get replenished.
1666 */
1667 if (unlikely(p->dl.dl_throttled))
1668 return;
1669
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001670 if (task_on_rq_queued(p) && rq->curr != p) {
Juri Lelli1baca4c2013-11-07 14:43:38 +01001671#ifdef CONFIG_SMP
Wanpeng Lid9aade7a2014-10-22 08:36:43 +08001672 if (p->nr_cpus_allowed > 1 && rq->dl.overloaded &&
1673 push_dl_task(rq) && rq != task_rq(p))
Juri Lelli1baca4c2013-11-07 14:43:38 +01001674 /* Only reschedule if pushing failed */
1675 check_resched = 0;
1676#endif /* CONFIG_SMP */
Kirill Tkhaif3a7e1a2014-10-21 20:35:56 +04001677 if (check_resched) {
1678 if (dl_task(rq->curr))
1679 check_preempt_curr_dl(rq, p, 0);
1680 else
1681 resched_curr(rq);
1682 }
Dario Faggioliaab03e02013-11-28 11:14:43 +01001683 }
1684}
1685
Juri Lelli1baca4c2013-11-07 14:43:38 +01001686/*
1687 * If the scheduling parameters of a -deadline task changed,
1688 * a push or pull operation might be needed.
1689 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01001690static void prio_changed_dl(struct rq *rq, struct task_struct *p,
1691 int oldprio)
1692{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001693 if (task_on_rq_queued(p) || rq->curr == p) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01001694#ifdef CONFIG_SMP
Juri Lelli1baca4c2013-11-07 14:43:38 +01001695 /*
1696 * This might be too much, but unfortunately
1697 * we don't have the old deadline value, and
1698 * we can't argue if the task is increasing
1699 * or lowering its prio, so...
1700 */
1701 if (!rq->dl.overloaded)
1702 pull_dl_task(rq);
1703
1704 /*
1705 * If we now have a earlier deadline task than p,
1706 * then reschedule, provided p is still on this
1707 * runqueue.
1708 */
1709 if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) &&
1710 rq->curr == p)
Kirill Tkhai88751252014-06-29 00:03:57 +04001711 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001712#else
1713 /*
1714 * Again, we don't know if p has a earlier
1715 * or later deadline, so let's blindly set a
1716 * (maybe not needed) rescheduling point.
1717 */
Kirill Tkhai88751252014-06-29 00:03:57 +04001718 resched_curr(rq);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001719#endif /* CONFIG_SMP */
1720 } else
1721 switched_to_dl(rq, p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001722}
Dario Faggioliaab03e02013-11-28 11:14:43 +01001723
1724const struct sched_class dl_sched_class = {
1725 .next = &rt_sched_class,
1726 .enqueue_task = enqueue_task_dl,
1727 .dequeue_task = dequeue_task_dl,
1728 .yield_task = yield_task_dl,
1729
1730 .check_preempt_curr = check_preempt_curr_dl,
1731
1732 .pick_next_task = pick_next_task_dl,
1733 .put_prev_task = put_prev_task_dl,
1734
1735#ifdef CONFIG_SMP
1736 .select_task_rq = select_task_rq_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01001737 .set_cpus_allowed = set_cpus_allowed_dl,
1738 .rq_online = rq_online_dl,
1739 .rq_offline = rq_offline_dl,
Juri Lelli1baca4c2013-11-07 14:43:38 +01001740 .post_schedule = post_schedule_dl,
1741 .task_woken = task_woken_dl,
Dario Faggioliaab03e02013-11-28 11:14:43 +01001742#endif
1743
1744 .set_curr_task = set_curr_task_dl,
1745 .task_tick = task_tick_dl,
1746 .task_fork = task_fork_dl,
1747 .task_dead = task_dead_dl,
1748
1749 .prio_changed = prio_changed_dl,
1750 .switched_from = switched_from_dl,
1751 .switched_to = switched_to_dl,
1752};
Wanpeng Liacb32132014-10-31 06:39:33 +08001753
1754#ifdef CONFIG_SCHED_DEBUG
1755extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
1756
1757void print_dl_stats(struct seq_file *m, int cpu)
1758{
1759 print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
1760}
1761#endif /* CONFIG_SCHED_DEBUG */