sched: de CPP-ify the scheduler code
[linux-2.6-block.git] / kernel / sched_rt.c
CommitLineData
bb44e5d1
IM
1/*
2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
3 * policies)
4 */
5
4fd29176 6#ifdef CONFIG_SMP
84de4274 7
637f5085 8static inline int rt_overloaded(struct rq *rq)
4fd29176 9{
637f5085 10 return atomic_read(&rq->rd->rto_count);
4fd29176 11}
84de4274 12
4fd29176
SR
13static inline void rt_set_overload(struct rq *rq)
14{
1f11eb6a
GH
15 if (!rq->online)
16 return;
17
c6c4927b 18 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
4fd29176
SR
19 /*
20 * Make sure the mask is visible before we set
21 * the overload count. That is checked to determine
22 * if we should look at the mask. It would be a shame
23 * if we looked at the mask, but the mask was not
24 * updated yet.
25 */
26 wmb();
637f5085 27 atomic_inc(&rq->rd->rto_count);
4fd29176 28}
84de4274 29
4fd29176
SR
30static inline void rt_clear_overload(struct rq *rq)
31{
1f11eb6a
GH
32 if (!rq->online)
33 return;
34
4fd29176 35 /* the order here really doesn't matter */
637f5085 36 atomic_dec(&rq->rd->rto_count);
c6c4927b 37 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
4fd29176 38}
73fe6aae
GH
39
40static void update_rt_migration(struct rq *rq)
41{
637f5085 42 if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
cdc8eb98
GH
43 if (!rq->rt.overloaded) {
44 rt_set_overload(rq);
45 rq->rt.overloaded = 1;
46 }
47 } else if (rq->rt.overloaded) {
73fe6aae 48 rt_clear_overload(rq);
637f5085
GH
49 rq->rt.overloaded = 0;
50 }
73fe6aae 51}
917b627d
GH
52
53static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
54{
55 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
56 plist_node_init(&p->pushable_tasks, p->prio);
57 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
58}
59
60static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
61{
62 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
63}
64
65#else
66
b07430ac
GH
67static inline
68void enqueue_pushable_task(struct rq *rq, struct task_struct *p) {}
69static inline
70void dequeue_pushable_task(struct rq *rq, struct task_struct *p) {}
917b627d 71
4fd29176
SR
72#endif /* CONFIG_SMP */
73
6f505b16 74static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
fa85ae24 75{
6f505b16
PZ
76 return container_of(rt_se, struct task_struct, rt);
77}
78
79static inline int on_rt_rq(struct sched_rt_entity *rt_se)
80{
81 return !list_empty(&rt_se->run_list);
82}
83
052f1dc7 84#ifdef CONFIG_RT_GROUP_SCHED
6f505b16 85
9f0c1e56 86static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
6f505b16
PZ
87{
88 if (!rt_rq->tg)
9f0c1e56 89 return RUNTIME_INF;
6f505b16 90
ac086bc2
PZ
91 return rt_rq->rt_runtime;
92}
93
94static inline u64 sched_rt_period(struct rt_rq *rt_rq)
95{
96 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
6f505b16
PZ
97}
98
99#define for_each_leaf_rt_rq(rt_rq, rq) \
80f40ee4 100 list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
6f505b16
PZ
101
102static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
103{
104 return rt_rq->rq;
105}
106
107static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
108{
109 return rt_se->rt_rq;
110}
111
112#define for_each_sched_rt_entity(rt_se) \
113 for (; rt_se; rt_se = rt_se->parent)
114
115static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
116{
117 return rt_se->my_q;
118}
119
120static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
121static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
122
9f0c1e56 123static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
6f505b16 124{
f6121f4f 125 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
6f505b16
PZ
126 struct sched_rt_entity *rt_se = rt_rq->rt_se;
127
f6121f4f
DF
128 if (rt_rq->rt_nr_running) {
129 if (rt_se && !on_rt_rq(rt_se))
130 enqueue_rt_entity(rt_se);
e864c499 131 if (rt_rq->highest_prio.curr < curr->prio)
1020387f 132 resched_task(curr);
6f505b16
PZ
133 }
134}
135
9f0c1e56 136static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
6f505b16
PZ
137{
138 struct sched_rt_entity *rt_se = rt_rq->rt_se;
139
140 if (rt_se && on_rt_rq(rt_se))
141 dequeue_rt_entity(rt_se);
142}
143
23b0fdfc
PZ
144static inline int rt_rq_throttled(struct rt_rq *rt_rq)
145{
146 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
147}
148
149static int rt_se_boosted(struct sched_rt_entity *rt_se)
150{
151 struct rt_rq *rt_rq = group_rt_rq(rt_se);
152 struct task_struct *p;
153
154 if (rt_rq)
155 return !!rt_rq->rt_nr_boosted;
156
157 p = rt_task_of(rt_se);
158 return p->prio != p->normal_prio;
159}
160
d0b27fa7 161#ifdef CONFIG_SMP
c6c4927b 162static inline const struct cpumask *sched_rt_period_mask(void)
d0b27fa7
PZ
163{
164 return cpu_rq(smp_processor_id())->rd->span;
165}
6f505b16 166#else
c6c4927b 167static inline const struct cpumask *sched_rt_period_mask(void)
d0b27fa7 168{
c6c4927b 169 return cpu_online_mask;
d0b27fa7
PZ
170}
171#endif
6f505b16 172
d0b27fa7
PZ
173static inline
174struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
6f505b16 175{
d0b27fa7
PZ
176 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
177}
9f0c1e56 178
ac086bc2
PZ
179static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
180{
181 return &rt_rq->tg->rt_bandwidth;
182}
183
55e12e5e 184#else /* !CONFIG_RT_GROUP_SCHED */
d0b27fa7
PZ
185
186static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
187{
ac086bc2
PZ
188 return rt_rq->rt_runtime;
189}
190
191static inline u64 sched_rt_period(struct rt_rq *rt_rq)
192{
193 return ktime_to_ns(def_rt_bandwidth.rt_period);
6f505b16
PZ
194}
195
196#define for_each_leaf_rt_rq(rt_rq, rq) \
197 for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
198
199static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
200{
201 return container_of(rt_rq, struct rq, rt);
202}
203
204static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
205{
206 struct task_struct *p = rt_task_of(rt_se);
207 struct rq *rq = task_rq(p);
208
209 return &rq->rt;
210}
211
212#define for_each_sched_rt_entity(rt_se) \
213 for (; rt_se; rt_se = NULL)
214
215static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
216{
217 return NULL;
218}
219
9f0c1e56 220static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
6f505b16 221{
f3ade837
JB
222 if (rt_rq->rt_nr_running)
223 resched_task(rq_of_rt_rq(rt_rq)->curr);
6f505b16
PZ
224}
225
9f0c1e56 226static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
6f505b16
PZ
227{
228}
229
23b0fdfc
PZ
230static inline int rt_rq_throttled(struct rt_rq *rt_rq)
231{
232 return rt_rq->rt_throttled;
233}
d0b27fa7 234
c6c4927b 235static inline const struct cpumask *sched_rt_period_mask(void)
d0b27fa7 236{
c6c4927b 237 return cpu_online_mask;
d0b27fa7
PZ
238}
239
240static inline
241struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
242{
243 return &cpu_rq(cpu)->rt;
244}
245
ac086bc2
PZ
246static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
247{
248 return &def_rt_bandwidth;
249}
250
55e12e5e 251#endif /* CONFIG_RT_GROUP_SCHED */
d0b27fa7 252
ac086bc2 253#ifdef CONFIG_SMP
78333cdd
PZ
254/*
255 * We ran out of runtime, see if we can borrow some from our neighbours.
256 */
b79f3833 257static int do_balance_runtime(struct rt_rq *rt_rq)
ac086bc2
PZ
258{
259 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
260 struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
261 int i, weight, more = 0;
262 u64 rt_period;
263
c6c4927b 264 weight = cpumask_weight(rd->span);
ac086bc2
PZ
265
266 spin_lock(&rt_b->rt_runtime_lock);
267 rt_period = ktime_to_ns(rt_b->rt_period);
c6c4927b 268 for_each_cpu(i, rd->span) {
ac086bc2
PZ
269 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
270 s64 diff;
271
272 if (iter == rt_rq)
273 continue;
274
275 spin_lock(&iter->rt_runtime_lock);
78333cdd
PZ
276 /*
277 * Either all rqs have inf runtime and there's nothing to steal
278 * or __disable_runtime() below sets a specific rq to inf to
279 * indicate its been disabled and disalow stealing.
280 */
7def2be1
PZ
281 if (iter->rt_runtime == RUNTIME_INF)
282 goto next;
283
78333cdd
PZ
284 /*
285 * From runqueues with spare time, take 1/n part of their
286 * spare time, but no more than our period.
287 */
ac086bc2
PZ
288 diff = iter->rt_runtime - iter->rt_time;
289 if (diff > 0) {
58838cf3 290 diff = div_u64((u64)diff, weight);
ac086bc2
PZ
291 if (rt_rq->rt_runtime + diff > rt_period)
292 diff = rt_period - rt_rq->rt_runtime;
293 iter->rt_runtime -= diff;
294 rt_rq->rt_runtime += diff;
295 more = 1;
296 if (rt_rq->rt_runtime == rt_period) {
297 spin_unlock(&iter->rt_runtime_lock);
298 break;
299 }
300 }
7def2be1 301next:
ac086bc2
PZ
302 spin_unlock(&iter->rt_runtime_lock);
303 }
304 spin_unlock(&rt_b->rt_runtime_lock);
305
306 return more;
307}
7def2be1 308
78333cdd
PZ
309/*
310 * Ensure this RQ takes back all the runtime it lend to its neighbours.
311 */
7def2be1
PZ
312static void __disable_runtime(struct rq *rq)
313{
314 struct root_domain *rd = rq->rd;
315 struct rt_rq *rt_rq;
316
317 if (unlikely(!scheduler_running))
318 return;
319
320 for_each_leaf_rt_rq(rt_rq, rq) {
321 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
322 s64 want;
323 int i;
324
325 spin_lock(&rt_b->rt_runtime_lock);
326 spin_lock(&rt_rq->rt_runtime_lock);
78333cdd
PZ
327 /*
328 * Either we're all inf and nobody needs to borrow, or we're
329 * already disabled and thus have nothing to do, or we have
330 * exactly the right amount of runtime to take out.
331 */
7def2be1
PZ
332 if (rt_rq->rt_runtime == RUNTIME_INF ||
333 rt_rq->rt_runtime == rt_b->rt_runtime)
334 goto balanced;
335 spin_unlock(&rt_rq->rt_runtime_lock);
336
78333cdd
PZ
337 /*
338 * Calculate the difference between what we started out with
339 * and what we current have, that's the amount of runtime
340 * we lend and now have to reclaim.
341 */
7def2be1
PZ
342 want = rt_b->rt_runtime - rt_rq->rt_runtime;
343
78333cdd
PZ
344 /*
345 * Greedy reclaim, take back as much as we can.
346 */
c6c4927b 347 for_each_cpu(i, rd->span) {
7def2be1
PZ
348 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
349 s64 diff;
350
78333cdd
PZ
351 /*
352 * Can't reclaim from ourselves or disabled runqueues.
353 */
f1679d08 354 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
7def2be1
PZ
355 continue;
356
357 spin_lock(&iter->rt_runtime_lock);
358 if (want > 0) {
359 diff = min_t(s64, iter->rt_runtime, want);
360 iter->rt_runtime -= diff;
361 want -= diff;
362 } else {
363 iter->rt_runtime -= want;
364 want -= want;
365 }
366 spin_unlock(&iter->rt_runtime_lock);
367
368 if (!want)
369 break;
370 }
371
372 spin_lock(&rt_rq->rt_runtime_lock);
78333cdd
PZ
373 /*
374 * We cannot be left wanting - that would mean some runtime
375 * leaked out of the system.
376 */
7def2be1
PZ
377 BUG_ON(want);
378balanced:
78333cdd
PZ
379 /*
380 * Disable all the borrow logic by pretending we have inf
381 * runtime - in which case borrowing doesn't make sense.
382 */
7def2be1
PZ
383 rt_rq->rt_runtime = RUNTIME_INF;
384 spin_unlock(&rt_rq->rt_runtime_lock);
385 spin_unlock(&rt_b->rt_runtime_lock);
386 }
387}
388
389static void disable_runtime(struct rq *rq)
390{
391 unsigned long flags;
392
393 spin_lock_irqsave(&rq->lock, flags);
394 __disable_runtime(rq);
395 spin_unlock_irqrestore(&rq->lock, flags);
396}
397
398static void __enable_runtime(struct rq *rq)
399{
7def2be1
PZ
400 struct rt_rq *rt_rq;
401
402 if (unlikely(!scheduler_running))
403 return;
404
78333cdd
PZ
405 /*
406 * Reset each runqueue's bandwidth settings
407 */
7def2be1
PZ
408 for_each_leaf_rt_rq(rt_rq, rq) {
409 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
410
411 spin_lock(&rt_b->rt_runtime_lock);
412 spin_lock(&rt_rq->rt_runtime_lock);
413 rt_rq->rt_runtime = rt_b->rt_runtime;
414 rt_rq->rt_time = 0;
baf25731 415 rt_rq->rt_throttled = 0;
7def2be1
PZ
416 spin_unlock(&rt_rq->rt_runtime_lock);
417 spin_unlock(&rt_b->rt_runtime_lock);
418 }
419}
420
421static void enable_runtime(struct rq *rq)
422{
423 unsigned long flags;
424
425 spin_lock_irqsave(&rq->lock, flags);
426 __enable_runtime(rq);
427 spin_unlock_irqrestore(&rq->lock, flags);
428}
429
eff6549b
PZ
430static int balance_runtime(struct rt_rq *rt_rq)
431{
432 int more = 0;
433
434 if (rt_rq->rt_time > rt_rq->rt_runtime) {
435 spin_unlock(&rt_rq->rt_runtime_lock);
436 more = do_balance_runtime(rt_rq);
437 spin_lock(&rt_rq->rt_runtime_lock);
438 }
439
440 return more;
441}
55e12e5e 442#else /* !CONFIG_SMP */
eff6549b
PZ
443static inline int balance_runtime(struct rt_rq *rt_rq)
444{
445 return 0;
446}
55e12e5e 447#endif /* CONFIG_SMP */
ac086bc2 448
eff6549b
PZ
449static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
450{
451 int i, idle = 1;
c6c4927b 452 const struct cpumask *span;
eff6549b 453
0b148fa0 454 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
eff6549b
PZ
455 return 1;
456
457 span = sched_rt_period_mask();
c6c4927b 458 for_each_cpu(i, span) {
eff6549b
PZ
459 int enqueue = 0;
460 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
461 struct rq *rq = rq_of_rt_rq(rt_rq);
462
463 spin_lock(&rq->lock);
464 if (rt_rq->rt_time) {
465 u64 runtime;
466
467 spin_lock(&rt_rq->rt_runtime_lock);
468 if (rt_rq->rt_throttled)
469 balance_runtime(rt_rq);
470 runtime = rt_rq->rt_runtime;
471 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
472 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
473 rt_rq->rt_throttled = 0;
474 enqueue = 1;
475 }
476 if (rt_rq->rt_time || rt_rq->rt_nr_running)
477 idle = 0;
478 spin_unlock(&rt_rq->rt_runtime_lock);
6c3df255
PZ
479 } else if (rt_rq->rt_nr_running)
480 idle = 0;
eff6549b
PZ
481
482 if (enqueue)
483 sched_rt_rq_enqueue(rt_rq);
484 spin_unlock(&rq->lock);
485 }
486
487 return idle;
488}
ac086bc2 489
6f505b16
PZ
490static inline int rt_se_prio(struct sched_rt_entity *rt_se)
491{
052f1dc7 492#ifdef CONFIG_RT_GROUP_SCHED
6f505b16
PZ
493 struct rt_rq *rt_rq = group_rt_rq(rt_se);
494
495 if (rt_rq)
e864c499 496 return rt_rq->highest_prio.curr;
6f505b16
PZ
497#endif
498
499 return rt_task_of(rt_se)->prio;
500}
501
9f0c1e56 502static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
6f505b16 503{
9f0c1e56 504 u64 runtime = sched_rt_runtime(rt_rq);
fa85ae24 505
fa85ae24 506 if (rt_rq->rt_throttled)
23b0fdfc 507 return rt_rq_throttled(rt_rq);
fa85ae24 508
ac086bc2
PZ
509 if (sched_rt_runtime(rt_rq) >= sched_rt_period(rt_rq))
510 return 0;
511
b79f3833
PZ
512 balance_runtime(rt_rq);
513 runtime = sched_rt_runtime(rt_rq);
514 if (runtime == RUNTIME_INF)
515 return 0;
ac086bc2 516
9f0c1e56 517 if (rt_rq->rt_time > runtime) {
6f505b16 518 rt_rq->rt_throttled = 1;
23b0fdfc 519 if (rt_rq_throttled(rt_rq)) {
9f0c1e56 520 sched_rt_rq_dequeue(rt_rq);
23b0fdfc
PZ
521 return 1;
522 }
fa85ae24
PZ
523 }
524
525 return 0;
526}
527
bb44e5d1
IM
528/*
529 * Update the current task's runtime statistics. Skip current tasks that
530 * are not in our scheduling class.
531 */
a9957449 532static void update_curr_rt(struct rq *rq)
bb44e5d1
IM
533{
534 struct task_struct *curr = rq->curr;
6f505b16
PZ
535 struct sched_rt_entity *rt_se = &curr->rt;
536 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
bb44e5d1
IM
537 u64 delta_exec;
538
539 if (!task_has_rt_policy(curr))
540 return;
541
d281918d 542 delta_exec = rq->clock - curr->se.exec_start;
bb44e5d1
IM
543 if (unlikely((s64)delta_exec < 0))
544 delta_exec = 0;
6cfb0d5d
IM
545
546 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
bb44e5d1
IM
547
548 curr->se.sum_exec_runtime += delta_exec;
f06febc9
FM
549 account_group_exec_runtime(curr, delta_exec);
550
d281918d 551 curr->se.exec_start = rq->clock;
d842de87 552 cpuacct_charge(curr, delta_exec);
fa85ae24 553
0b148fa0
PZ
554 if (!rt_bandwidth_enabled())
555 return;
556
354d60c2
DG
557 for_each_sched_rt_entity(rt_se) {
558 rt_rq = rt_rq_of_se(rt_se);
559
cc2991cf 560 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
e113a745 561 spin_lock(&rt_rq->rt_runtime_lock);
cc2991cf
PZ
562 rt_rq->rt_time += delta_exec;
563 if (sched_rt_runtime_exceeded(rt_rq))
564 resched_task(curr);
e113a745 565 spin_unlock(&rt_rq->rt_runtime_lock);
cc2991cf 566 }
354d60c2 567 }
bb44e5d1
IM
568}
569
e864c499
GH
570#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
571
572static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu);
573
574static inline int next_prio(struct rq *rq)
575{
576 struct task_struct *next = pick_next_highest_task_rt(rq, rq->cpu);
577
578 if (next && rt_prio(next->prio))
579 return next->prio;
580 else
581 return MAX_RT_PRIO;
582}
583#endif
584
6f505b16
PZ
585static inline
586void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
63489e45 587{
4d984277 588 int prio = rt_se_prio(rt_se);
577b4a58 589#ifdef CONFIG_SMP
4d984277 590 struct rq *rq = rq_of_rt_rq(rt_rq);
577b4a58 591#endif
1f11eb6a 592
4d984277
GH
593 WARN_ON(!rt_prio(prio));
594 rt_rq->rt_nr_running++;
595#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
e864c499 596 if (prio < rt_rq->highest_prio.curr) {
4d984277 597
e864c499
GH
598 /*
599 * If the new task is higher in priority than anything on the
600 * run-queue, we have a new high that must be published to
601 * the world. We also know that the previous high becomes
602 * our next-highest.
603 */
604 rt_rq->highest_prio.next = rt_rq->highest_prio.curr;
605 rt_rq->highest_prio.curr = prio;
1100ac91 606#ifdef CONFIG_SMP
1f11eb6a 607 if (rq->online)
4d984277 608 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
1100ac91 609#endif
e864c499
GH
610 } else if (prio == rt_rq->highest_prio.curr)
611 /*
612 * If the next task is equal in priority to the highest on
613 * the run-queue, then we implicitly know that the next highest
614 * task cannot be any lower than current
615 */
616 rt_rq->highest_prio.next = prio;
617 else if (prio < rt_rq->highest_prio.next)
618 /*
619 * Otherwise, we need to recompute next-highest
620 */
621 rt_rq->highest_prio.next = next_prio(rq);
6f505b16 622#endif
764a9d6f 623#ifdef CONFIG_SMP
4d984277 624 if (rt_se->nr_cpus_allowed > 1)
73fe6aae
GH
625 rq->rt.rt_nr_migratory++;
626
4d984277 627 update_rt_migration(rq);
6f505b16 628#endif
052f1dc7 629#ifdef CONFIG_RT_GROUP_SCHED
23b0fdfc
PZ
630 if (rt_se_boosted(rt_se))
631 rt_rq->rt_nr_boosted++;
d0b27fa7
PZ
632
633 if (rt_rq->tg)
634 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
635#else
636 start_rt_bandwidth(&def_rt_bandwidth);
23b0fdfc 637#endif
63489e45
SR
638}
639
6f505b16
PZ
640static inline
641void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
63489e45 642{
6e0534f2 643#ifdef CONFIG_SMP
4d984277 644 struct rq *rq = rq_of_rt_rq(rt_rq);
e864c499 645 int highest_prio = rt_rq->highest_prio.curr;
6e0534f2
GH
646#endif
647
6f505b16
PZ
648 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
649 WARN_ON(!rt_rq->rt_nr_running);
650 rt_rq->rt_nr_running--;
052f1dc7 651#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
6f505b16 652 if (rt_rq->rt_nr_running) {
e864c499 653 int prio = rt_se_prio(rt_se);
764a9d6f 654
e864c499 655 WARN_ON(prio < rt_rq->highest_prio.curr);
764a9d6f 656
e864c499
GH
657 /*
658 * This may have been our highest or next-highest priority
659 * task and therefore we may have some recomputation to do
660 */
661 if (prio == rt_rq->highest_prio.curr) {
662 struct rt_prio_array *array = &rt_rq->active;
663
664 rt_rq->highest_prio.curr =
764a9d6f 665 sched_find_first_bit(array->bitmap);
e864c499
GH
666 }
667
668 if (prio <= rt_rq->highest_prio.next)
669 rt_rq->highest_prio.next = next_prio(rq);
764a9d6f 670 } else
e864c499 671 rt_rq->highest_prio.curr = MAX_RT_PRIO;
6f505b16
PZ
672#endif
673#ifdef CONFIG_SMP
4d984277 674 if (rt_se->nr_cpus_allowed > 1)
73fe6aae
GH
675 rq->rt.rt_nr_migratory--;
676
e864c499
GH
677 if (rq->online && rt_rq->highest_prio.curr != highest_prio)
678 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1f11eb6a 679
4d984277 680 update_rt_migration(rq);
764a9d6f 681#endif /* CONFIG_SMP */
052f1dc7 682#ifdef CONFIG_RT_GROUP_SCHED
23b0fdfc
PZ
683 if (rt_se_boosted(rt_se))
684 rt_rq->rt_nr_boosted--;
685
686 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
687#endif
63489e45
SR
688}
689
ad2a3f13 690static void __enqueue_rt_entity(struct sched_rt_entity *rt_se)
bb44e5d1 691{
6f505b16
PZ
692 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
693 struct rt_prio_array *array = &rt_rq->active;
694 struct rt_rq *group_rq = group_rt_rq(rt_se);
20b6331b 695 struct list_head *queue = array->queue + rt_se_prio(rt_se);
bb44e5d1 696
ad2a3f13
PZ
697 /*
698 * Don't enqueue the group if its throttled, or when empty.
699 * The latter is a consequence of the former when a child group
700 * get throttled and the current group doesn't have any other
701 * active members.
702 */
703 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
6f505b16 704 return;
63489e45 705
7ebefa8c 706 list_add_tail(&rt_se->run_list, queue);
6f505b16 707 __set_bit(rt_se_prio(rt_se), array->bitmap);
78f2c7db 708
6f505b16
PZ
709 inc_rt_tasks(rt_se, rt_rq);
710}
711
ad2a3f13 712static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
6f505b16
PZ
713{
714 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
715 struct rt_prio_array *array = &rt_rq->active;
716
717 list_del_init(&rt_se->run_list);
718 if (list_empty(array->queue + rt_se_prio(rt_se)))
719 __clear_bit(rt_se_prio(rt_se), array->bitmap);
720
721 dec_rt_tasks(rt_se, rt_rq);
722}
723
724/*
725 * Because the prio of an upper entry depends on the lower
726 * entries, we must remove entries top - down.
6f505b16 727 */
ad2a3f13 728static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
6f505b16 729{
ad2a3f13 730 struct sched_rt_entity *back = NULL;
6f505b16 731
58d6c2d7
PZ
732 for_each_sched_rt_entity(rt_se) {
733 rt_se->back = back;
734 back = rt_se;
735 }
736
737 for (rt_se = back; rt_se; rt_se = rt_se->back) {
738 if (on_rt_rq(rt_se))
ad2a3f13
PZ
739 __dequeue_rt_entity(rt_se);
740 }
741}
742
743static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
744{
745 dequeue_rt_stack(rt_se);
746 for_each_sched_rt_entity(rt_se)
747 __enqueue_rt_entity(rt_se);
748}
749
750static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
751{
752 dequeue_rt_stack(rt_se);
753
754 for_each_sched_rt_entity(rt_se) {
755 struct rt_rq *rt_rq = group_rt_rq(rt_se);
756
757 if (rt_rq && rt_rq->rt_nr_running)
758 __enqueue_rt_entity(rt_se);
58d6c2d7 759 }
bb44e5d1
IM
760}
761
762/*
763 * Adding/removing a task to/from a priority array:
764 */
6f505b16
PZ
765static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
766{
767 struct sched_rt_entity *rt_se = &p->rt;
768
769 if (wakeup)
770 rt_se->timeout = 0;
771
ad2a3f13 772 enqueue_rt_entity(rt_se);
c09595f6 773
917b627d
GH
774 if (!task_current(rq, p) && p->rt.nr_cpus_allowed > 1)
775 enqueue_pushable_task(rq, p);
776
c09595f6 777 inc_cpu_load(rq, p->se.load.weight);
6f505b16
PZ
778}
779
f02231e5 780static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
bb44e5d1 781{
6f505b16 782 struct sched_rt_entity *rt_se = &p->rt;
bb44e5d1 783
f1e14ef6 784 update_curr_rt(rq);
ad2a3f13 785 dequeue_rt_entity(rt_se);
c09595f6 786
917b627d
GH
787 dequeue_pushable_task(rq, p);
788
c09595f6 789 dec_cpu_load(rq, p->se.load.weight);
bb44e5d1
IM
790}
791
792/*
793 * Put task to the end of the run list without the overhead of dequeue
794 * followed by enqueue.
795 */
7ebefa8c
DA
796static void
797requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
6f505b16 798{
1cdad715 799 if (on_rt_rq(rt_se)) {
7ebefa8c
DA
800 struct rt_prio_array *array = &rt_rq->active;
801 struct list_head *queue = array->queue + rt_se_prio(rt_se);
802
803 if (head)
804 list_move(&rt_se->run_list, queue);
805 else
806 list_move_tail(&rt_se->run_list, queue);
1cdad715 807 }
6f505b16
PZ
808}
809
7ebefa8c 810static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
bb44e5d1 811{
6f505b16
PZ
812 struct sched_rt_entity *rt_se = &p->rt;
813 struct rt_rq *rt_rq;
bb44e5d1 814
6f505b16
PZ
815 for_each_sched_rt_entity(rt_se) {
816 rt_rq = rt_rq_of_se(rt_se);
7ebefa8c 817 requeue_rt_entity(rt_rq, rt_se, head);
6f505b16 818 }
bb44e5d1
IM
819}
820
6f505b16 821static void yield_task_rt(struct rq *rq)
bb44e5d1 822{
7ebefa8c 823 requeue_task_rt(rq, rq->curr, 0);
bb44e5d1
IM
824}
825
e7693a36 826#ifdef CONFIG_SMP
318e0893
GH
827static int find_lowest_rq(struct task_struct *task);
828
e7693a36
GH
829static int select_task_rq_rt(struct task_struct *p, int sync)
830{
318e0893
GH
831 struct rq *rq = task_rq(p);
832
833 /*
e1f47d89
SR
834 * If the current task is an RT task, then
835 * try to see if we can wake this RT task up on another
836 * runqueue. Otherwise simply start this RT task
837 * on its current runqueue.
838 *
839 * We want to avoid overloading runqueues. Even if
840 * the RT task is of higher priority than the current RT task.
841 * RT tasks behave differently than other tasks. If
842 * one gets preempted, we try to push it off to another queue.
843 * So trying to keep a preempting RT task on the same
844 * cache hot CPU will force the running RT task to
845 * a cold CPU. So we waste all the cache for the lower
846 * RT task in hopes of saving some of a RT task
847 * that is just being woken and probably will have
848 * cold cache anyway.
318e0893 849 */
17b3279b 850 if (unlikely(rt_task(rq->curr)) &&
6f505b16 851 (p->rt.nr_cpus_allowed > 1)) {
318e0893
GH
852 int cpu = find_lowest_rq(p);
853
854 return (cpu == -1) ? task_cpu(p) : cpu;
855 }
856
857 /*
858 * Otherwise, just let it ride on the affined RQ and the
859 * post-schedule router will push the preempted task away
860 */
e7693a36
GH
861 return task_cpu(p);
862}
7ebefa8c
DA
863
864static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
865{
24600ce8 866 cpumask_var_t mask;
7ebefa8c
DA
867
868 if (rq->curr->rt.nr_cpus_allowed == 1)
869 return;
870
24600ce8 871 if (!alloc_cpumask_var(&mask, GFP_ATOMIC))
7ebefa8c
DA
872 return;
873
24600ce8
RR
874 if (p->rt.nr_cpus_allowed != 1
875 && cpupri_find(&rq->rd->cpupri, p, mask))
876 goto free;
877
878 if (!cpupri_find(&rq->rd->cpupri, rq->curr, mask))
879 goto free;
7ebefa8c
DA
880
881 /*
882 * There appears to be other cpus that can accept
883 * current and none to run 'p', so lets reschedule
884 * to try and push current away:
885 */
886 requeue_task_rt(rq, p, 1);
887 resched_task(rq->curr);
24600ce8
RR
888free:
889 free_cpumask_var(mask);
7ebefa8c
DA
890}
891
e7693a36
GH
892#endif /* CONFIG_SMP */
893
bb44e5d1
IM
894/*
895 * Preempt the current task with a newly woken task if needed:
896 */
15afe09b 897static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int sync)
bb44e5d1 898{
45c01e82 899 if (p->prio < rq->curr->prio) {
bb44e5d1 900 resched_task(rq->curr);
45c01e82
GH
901 return;
902 }
903
904#ifdef CONFIG_SMP
905 /*
906 * If:
907 *
908 * - the newly woken task is of equal priority to the current task
909 * - the newly woken task is non-migratable while current is migratable
910 * - current will be preempted on the next reschedule
911 *
912 * we should check to see if current can readily move to a different
913 * cpu. If so, we will reschedule to allow the push logic to try
914 * to move current somewhere else, making room for our non-migratable
915 * task.
916 */
7ebefa8c
DA
917 if (p->prio == rq->curr->prio && !need_resched())
918 check_preempt_equal_prio(rq, p);
45c01e82 919#endif
bb44e5d1
IM
920}
921
6f505b16
PZ
922static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
923 struct rt_rq *rt_rq)
bb44e5d1 924{
6f505b16
PZ
925 struct rt_prio_array *array = &rt_rq->active;
926 struct sched_rt_entity *next = NULL;
bb44e5d1
IM
927 struct list_head *queue;
928 int idx;
929
930 idx = sched_find_first_bit(array->bitmap);
6f505b16 931 BUG_ON(idx >= MAX_RT_PRIO);
bb44e5d1
IM
932
933 queue = array->queue + idx;
6f505b16 934 next = list_entry(queue->next, struct sched_rt_entity, run_list);
326587b8 935
6f505b16
PZ
936 return next;
937}
bb44e5d1 938
917b627d 939static struct task_struct *_pick_next_task_rt(struct rq *rq)
6f505b16
PZ
940{
941 struct sched_rt_entity *rt_se;
942 struct task_struct *p;
943 struct rt_rq *rt_rq;
bb44e5d1 944
6f505b16
PZ
945 rt_rq = &rq->rt;
946
947 if (unlikely(!rt_rq->rt_nr_running))
948 return NULL;
949
23b0fdfc 950 if (rt_rq_throttled(rt_rq))
6f505b16
PZ
951 return NULL;
952
953 do {
954 rt_se = pick_next_rt_entity(rq, rt_rq);
326587b8 955 BUG_ON(!rt_se);
6f505b16
PZ
956 rt_rq = group_rt_rq(rt_se);
957 } while (rt_rq);
958
959 p = rt_task_of(rt_se);
960 p->se.exec_start = rq->clock;
917b627d
GH
961
962 return p;
963}
964
965static struct task_struct *pick_next_task_rt(struct rq *rq)
966{
967 struct task_struct *p = _pick_next_task_rt(rq);
968
969 /* The running task is never eligible for pushing */
970 if (p)
971 dequeue_pushable_task(rq, p);
972
6f505b16 973 return p;
bb44e5d1
IM
974}
975
31ee529c 976static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
bb44e5d1 977{
f1e14ef6 978 update_curr_rt(rq);
bb44e5d1 979 p->se.exec_start = 0;
917b627d
GH
980
981 /*
982 * The previous task needs to be made eligible for pushing
983 * if it is still active
984 */
985 if (p->se.on_rq && p->rt.nr_cpus_allowed > 1)
986 enqueue_pushable_task(rq, p);
bb44e5d1
IM
987}
988
681f3e68 989#ifdef CONFIG_SMP
6f505b16 990
e8fa1362
SR
991/* Only try algorithms three times */
992#define RT_MAX_TRIES 3
993
e8fa1362
SR
994static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
995
f65eda4f
SR
996static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
997{
998 if (!task_running(rq, p) &&
96f874e2 999 (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
6f505b16 1000 (p->rt.nr_cpus_allowed > 1))
f65eda4f
SR
1001 return 1;
1002 return 0;
1003}
1004
e8fa1362 1005/* Return the second highest RT task, NULL otherwise */
79064fbf 1006static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
e8fa1362 1007{
6f505b16
PZ
1008 struct task_struct *next = NULL;
1009 struct sched_rt_entity *rt_se;
1010 struct rt_prio_array *array;
1011 struct rt_rq *rt_rq;
e8fa1362
SR
1012 int idx;
1013
6f505b16
PZ
1014 for_each_leaf_rt_rq(rt_rq, rq) {
1015 array = &rt_rq->active;
1016 idx = sched_find_first_bit(array->bitmap);
1017 next_idx:
1018 if (idx >= MAX_RT_PRIO)
1019 continue;
1020 if (next && next->prio < idx)
1021 continue;
1022 list_for_each_entry(rt_se, array->queue + idx, run_list) {
1023 struct task_struct *p = rt_task_of(rt_se);
1024 if (pick_rt_task(rq, p, cpu)) {
1025 next = p;
1026 break;
1027 }
1028 }
1029 if (!next) {
1030 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
1031 goto next_idx;
1032 }
f65eda4f
SR
1033 }
1034
e8fa1362
SR
1035 return next;
1036}
1037
0e3900e6 1038static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
e8fa1362 1039
6e1254d2
GH
1040static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
1041{
1042 int first;
1043
1044 /* "this_cpu" is cheaper to preempt than a remote processor */
1045 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
1046 return this_cpu;
1047
1048 first = first_cpu(*mask);
1049 if (first != NR_CPUS)
1050 return first;
1051
1052 return -1;
1053}
1054
1055static int find_lowest_rq(struct task_struct *task)
1056{
1057 struct sched_domain *sd;
96f874e2 1058 struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
6e1254d2
GH
1059 int this_cpu = smp_processor_id();
1060 int cpu = task_cpu(task);
06f90dbd 1061
6e0534f2
GH
1062 if (task->rt.nr_cpus_allowed == 1)
1063 return -1; /* No other targets possible */
6e1254d2 1064
6e0534f2
GH
1065 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
1066 return -1; /* No targets found */
6e1254d2 1067
e761b772
MK
1068 /*
1069 * Only consider CPUs that are usable for migration.
1070 * I guess we might want to change cpupri_find() to ignore those
1071 * in the first place.
1072 */
96f874e2 1073 cpumask_and(lowest_mask, lowest_mask, cpu_active_mask);
e761b772 1074
6e1254d2
GH
1075 /*
1076 * At this point we have built a mask of cpus representing the
1077 * lowest priority tasks in the system. Now we want to elect
1078 * the best one based on our affinity and topology.
1079 *
1080 * We prioritize the last cpu that the task executed on since
1081 * it is most likely cache-hot in that location.
1082 */
96f874e2 1083 if (cpumask_test_cpu(cpu, lowest_mask))
6e1254d2
GH
1084 return cpu;
1085
1086 /*
1087 * Otherwise, we consult the sched_domains span maps to figure
1088 * out which cpu is logically closest to our hot cache data.
1089 */
1090 if (this_cpu == cpu)
1091 this_cpu = -1; /* Skip this_cpu opt if the same */
1092
1093 for_each_domain(cpu, sd) {
1094 if (sd->flags & SD_WAKE_AFFINE) {
1095 cpumask_t domain_mask;
1096 int best_cpu;
1097
758b2cdc
RR
1098 cpumask_and(&domain_mask, sched_domain_span(sd),
1099 lowest_mask);
6e1254d2
GH
1100
1101 best_cpu = pick_optimal_cpu(this_cpu,
1102 &domain_mask);
1103 if (best_cpu != -1)
1104 return best_cpu;
1105 }
1106 }
1107
1108 /*
1109 * And finally, if there were no matches within the domains
1110 * just give the caller *something* to work with from the compatible
1111 * locations.
1112 */
1113 return pick_optimal_cpu(this_cpu, lowest_mask);
07b4032c
GH
1114}
1115
1116/* Will lock the rq it finds */
4df64c0b 1117static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
07b4032c
GH
1118{
1119 struct rq *lowest_rq = NULL;
07b4032c 1120 int tries;
4df64c0b 1121 int cpu;
e8fa1362 1122
07b4032c
GH
1123 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
1124 cpu = find_lowest_rq(task);
1125
2de0b463 1126 if ((cpu == -1) || (cpu == rq->cpu))
e8fa1362
SR
1127 break;
1128
07b4032c
GH
1129 lowest_rq = cpu_rq(cpu);
1130
e8fa1362 1131 /* if the prio of this runqueue changed, try again */
07b4032c 1132 if (double_lock_balance(rq, lowest_rq)) {
e8fa1362
SR
1133 /*
1134 * We had to unlock the run queue. In
1135 * the mean time, task could have
1136 * migrated already or had its affinity changed.
1137 * Also make sure that it wasn't scheduled on its rq.
1138 */
07b4032c 1139 if (unlikely(task_rq(task) != rq ||
96f874e2
RR
1140 !cpumask_test_cpu(lowest_rq->cpu,
1141 &task->cpus_allowed) ||
07b4032c 1142 task_running(rq, task) ||
e8fa1362 1143 !task->se.on_rq)) {
4df64c0b 1144
e8fa1362
SR
1145 spin_unlock(&lowest_rq->lock);
1146 lowest_rq = NULL;
1147 break;
1148 }
1149 }
1150
1151 /* If this rq is still suitable use it. */
e864c499 1152 if (lowest_rq->rt.highest_prio.curr > task->prio)
e8fa1362
SR
1153 break;
1154
1155 /* try again */
1b12bbc7 1156 double_unlock_balance(rq, lowest_rq);
e8fa1362
SR
1157 lowest_rq = NULL;
1158 }
1159
1160 return lowest_rq;
1161}
1162
917b627d
GH
1163static inline int has_pushable_tasks(struct rq *rq)
1164{
1165 return !plist_head_empty(&rq->rt.pushable_tasks);
1166}
1167
1168static struct task_struct *pick_next_pushable_task(struct rq *rq)
1169{
1170 struct task_struct *p;
1171
1172 if (!has_pushable_tasks(rq))
1173 return NULL;
1174
1175 p = plist_first_entry(&rq->rt.pushable_tasks,
1176 struct task_struct, pushable_tasks);
1177
1178 BUG_ON(rq->cpu != task_cpu(p));
1179 BUG_ON(task_current(rq, p));
1180 BUG_ON(p->rt.nr_cpus_allowed <= 1);
1181
1182 BUG_ON(!p->se.on_rq);
1183 BUG_ON(!rt_task(p));
1184
1185 return p;
1186}
1187
e8fa1362
SR
1188/*
1189 * If the current CPU has more than one RT task, see if the non
1190 * running task can migrate over to a CPU that is running a task
1191 * of lesser priority.
1192 */
697f0a48 1193static int push_rt_task(struct rq *rq)
e8fa1362
SR
1194{
1195 struct task_struct *next_task;
1196 struct rq *lowest_rq;
e8fa1362 1197
a22d7fc1
GH
1198 if (!rq->rt.overloaded)
1199 return 0;
1200
917b627d 1201 next_task = pick_next_pushable_task(rq);
e8fa1362
SR
1202 if (!next_task)
1203 return 0;
1204
1205 retry:
697f0a48 1206 if (unlikely(next_task == rq->curr)) {
f65eda4f 1207 WARN_ON(1);
e8fa1362 1208 return 0;
f65eda4f 1209 }
e8fa1362
SR
1210
1211 /*
1212 * It's possible that the next_task slipped in of
1213 * higher priority than current. If that's the case
1214 * just reschedule current.
1215 */
697f0a48
GH
1216 if (unlikely(next_task->prio < rq->curr->prio)) {
1217 resched_task(rq->curr);
e8fa1362
SR
1218 return 0;
1219 }
1220
697f0a48 1221 /* We might release rq lock */
e8fa1362
SR
1222 get_task_struct(next_task);
1223
1224 /* find_lock_lowest_rq locks the rq if found */
697f0a48 1225 lowest_rq = find_lock_lowest_rq(next_task, rq);
e8fa1362
SR
1226 if (!lowest_rq) {
1227 struct task_struct *task;
1228 /*
697f0a48 1229 * find lock_lowest_rq releases rq->lock
1563513d
GH
1230 * so it is possible that next_task has migrated.
1231 *
1232 * We need to make sure that the task is still on the same
1233 * run-queue and is also still the next task eligible for
1234 * pushing.
e8fa1362 1235 */
917b627d 1236 task = pick_next_pushable_task(rq);
1563513d
GH
1237 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1238 /*
1239 * If we get here, the task hasnt moved at all, but
1240 * it has failed to push. We will not try again,
1241 * since the other cpus will pull from us when they
1242 * are ready.
1243 */
1244 dequeue_pushable_task(rq, next_task);
1245 goto out;
e8fa1362 1246 }
917b627d 1247
1563513d
GH
1248 if (!task)
1249 /* No more tasks, just exit */
1250 goto out;
1251
917b627d 1252 /*
1563513d 1253 * Something has shifted, try again.
917b627d 1254 */
1563513d
GH
1255 put_task_struct(next_task);
1256 next_task = task;
1257 goto retry;
e8fa1362
SR
1258 }
1259
697f0a48 1260 deactivate_task(rq, next_task, 0);
e8fa1362
SR
1261 set_task_cpu(next_task, lowest_rq->cpu);
1262 activate_task(lowest_rq, next_task, 0);
1263
1264 resched_task(lowest_rq->curr);
1265
1b12bbc7 1266 double_unlock_balance(rq, lowest_rq);
e8fa1362 1267
e8fa1362
SR
1268out:
1269 put_task_struct(next_task);
1270
917b627d 1271 return 1;
e8fa1362
SR
1272}
1273
e8fa1362
SR
1274static void push_rt_tasks(struct rq *rq)
1275{
1276 /* push_rt_task will return true if it moved an RT */
1277 while (push_rt_task(rq))
1278 ;
1279}
1280
f65eda4f
SR
1281static int pull_rt_task(struct rq *this_rq)
1282{
80bf3171 1283 int this_cpu = this_rq->cpu, ret = 0, cpu;
a8728944 1284 struct task_struct *p;
f65eda4f 1285 struct rq *src_rq;
f65eda4f 1286
637f5085 1287 if (likely(!rt_overloaded(this_rq)))
f65eda4f
SR
1288 return 0;
1289
c6c4927b 1290 for_each_cpu(cpu, this_rq->rd->rto_mask) {
f65eda4f
SR
1291 if (this_cpu == cpu)
1292 continue;
1293
1294 src_rq = cpu_rq(cpu);
74ab8e4f
GH
1295
1296 /*
1297 * Don't bother taking the src_rq->lock if the next highest
1298 * task is known to be lower-priority than our current task.
1299 * This may look racy, but if this value is about to go
1300 * logically higher, the src_rq will push this task away.
1301 * And if its going logically lower, we do not care
1302 */
1303 if (src_rq->rt.highest_prio.next >=
1304 this_rq->rt.highest_prio.curr)
1305 continue;
1306
f65eda4f
SR
1307 /*
1308 * We can potentially drop this_rq's lock in
1309 * double_lock_balance, and another CPU could
a8728944 1310 * alter this_rq
f65eda4f 1311 */
a8728944 1312 double_lock_balance(this_rq, src_rq);
f65eda4f
SR
1313
1314 /*
1315 * Are there still pullable RT tasks?
1316 */
614ee1f6
MG
1317 if (src_rq->rt.rt_nr_running <= 1)
1318 goto skip;
f65eda4f 1319
f65eda4f
SR
1320 p = pick_next_highest_task_rt(src_rq, this_cpu);
1321
1322 /*
1323 * Do we have an RT task that preempts
1324 * the to-be-scheduled task?
1325 */
a8728944 1326 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
f65eda4f
SR
1327 WARN_ON(p == src_rq->curr);
1328 WARN_ON(!p->se.on_rq);
1329
1330 /*
1331 * There's a chance that p is higher in priority
1332 * than what's currently running on its cpu.
1333 * This is just that p is wakeing up and hasn't
1334 * had a chance to schedule. We only pull
1335 * p if it is lower in priority than the
a8728944 1336 * current task on the run queue
f65eda4f 1337 */
a8728944 1338 if (p->prio < src_rq->curr->prio)
614ee1f6 1339 goto skip;
f65eda4f
SR
1340
1341 ret = 1;
1342
1343 deactivate_task(src_rq, p, 0);
1344 set_task_cpu(p, this_cpu);
1345 activate_task(this_rq, p, 0);
1346 /*
1347 * We continue with the search, just in
1348 * case there's an even higher prio task
1349 * in another runqueue. (low likelyhood
1350 * but possible)
f65eda4f 1351 */
f65eda4f 1352 }
614ee1f6 1353 skip:
1b12bbc7 1354 double_unlock_balance(this_rq, src_rq);
f65eda4f
SR
1355 }
1356
1357 return ret;
1358}
1359
9a897c5a 1360static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
f65eda4f
SR
1361{
1362 /* Try to pull RT tasks here if we lower this rq's prio */
e864c499 1363 if (unlikely(rt_task(prev)) && rq->rt.highest_prio.curr > prev->prio)
f65eda4f
SR
1364 pull_rt_task(rq);
1365}
1366
967fc046
GH
1367/*
1368 * assumes rq->lock is held
1369 */
1370static int needs_post_schedule_rt(struct rq *rq)
1371{
917b627d 1372 return has_pushable_tasks(rq);
967fc046
GH
1373}
1374
9a897c5a 1375static void post_schedule_rt(struct rq *rq)
e8fa1362
SR
1376{
1377 /*
967fc046
GH
1378 * This is only called if needs_post_schedule_rt() indicates that
1379 * we need to push tasks away
e8fa1362 1380 */
967fc046
GH
1381 spin_lock_irq(&rq->lock);
1382 push_rt_tasks(rq);
1383 spin_unlock_irq(&rq->lock);
e8fa1362
SR
1384}
1385
8ae121ac
GH
1386/*
1387 * If we are not running and we are not going to reschedule soon, we should
1388 * try to push tasks away now
1389 */
9a897c5a 1390static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
4642dafd 1391{
9a897c5a 1392 if (!task_running(rq, p) &&
8ae121ac 1393 !test_tsk_need_resched(rq->curr) &&
917b627d 1394 has_pushable_tasks(rq) &&
777c2f38 1395 p->rt.nr_cpus_allowed > 1)
4642dafd
SR
1396 push_rt_tasks(rq);
1397}
1398
43010659 1399static unsigned long
bb44e5d1 1400load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
e1d1484f
PW
1401 unsigned long max_load_move,
1402 struct sched_domain *sd, enum cpu_idle_type idle,
1403 int *all_pinned, int *this_best_prio)
bb44e5d1 1404{
c7a1e46a
SR
1405 /* don't touch RT tasks */
1406 return 0;
e1d1484f
PW
1407}
1408
1409static int
1410move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
1411 struct sched_domain *sd, enum cpu_idle_type idle)
1412{
c7a1e46a
SR
1413 /* don't touch RT tasks */
1414 return 0;
bb44e5d1 1415}
deeeccd4 1416
cd8ba7cd 1417static void set_cpus_allowed_rt(struct task_struct *p,
96f874e2 1418 const struct cpumask *new_mask)
73fe6aae 1419{
96f874e2 1420 int weight = cpumask_weight(new_mask);
73fe6aae
GH
1421
1422 BUG_ON(!rt_task(p));
1423
1424 /*
1425 * Update the migration status of the RQ if we have an RT task
1426 * which is running AND changing its weight value.
1427 */
6f505b16 1428 if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
73fe6aae
GH
1429 struct rq *rq = task_rq(p);
1430
917b627d
GH
1431 if (!task_current(rq, p)) {
1432 /*
1433 * Make sure we dequeue this task from the pushable list
1434 * before going further. It will either remain off of
1435 * the list because we are no longer pushable, or it
1436 * will be requeued.
1437 */
1438 if (p->rt.nr_cpus_allowed > 1)
1439 dequeue_pushable_task(rq, p);
1440
1441 /*
1442 * Requeue if our weight is changing and still > 1
1443 */
1444 if (weight > 1)
1445 enqueue_pushable_task(rq, p);
1446
1447 }
1448
6f505b16 1449 if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
73fe6aae 1450 rq->rt.rt_nr_migratory++;
6f505b16 1451 } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
73fe6aae
GH
1452 BUG_ON(!rq->rt.rt_nr_migratory);
1453 rq->rt.rt_nr_migratory--;
1454 }
1455
1456 update_rt_migration(rq);
1457 }
1458
96f874e2 1459 cpumask_copy(&p->cpus_allowed, new_mask);
6f505b16 1460 p->rt.nr_cpus_allowed = weight;
73fe6aae 1461}
deeeccd4 1462
bdd7c81b 1463/* Assumes rq->lock is held */
1f11eb6a 1464static void rq_online_rt(struct rq *rq)
bdd7c81b
IM
1465{
1466 if (rq->rt.overloaded)
1467 rt_set_overload(rq);
6e0534f2 1468
7def2be1
PZ
1469 __enable_runtime(rq);
1470
e864c499 1471 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
bdd7c81b
IM
1472}
1473
1474/* Assumes rq->lock is held */
1f11eb6a 1475static void rq_offline_rt(struct rq *rq)
bdd7c81b
IM
1476{
1477 if (rq->rt.overloaded)
1478 rt_clear_overload(rq);
6e0534f2 1479
7def2be1
PZ
1480 __disable_runtime(rq);
1481
6e0534f2 1482 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
bdd7c81b 1483}
cb469845
SR
1484
1485/*
1486 * When switch from the rt queue, we bring ourselves to a position
1487 * that we might want to pull RT tasks from other runqueues.
1488 */
1489static void switched_from_rt(struct rq *rq, struct task_struct *p,
1490 int running)
1491{
1492 /*
1493 * If there are other RT tasks then we will reschedule
1494 * and the scheduling of the other RT tasks will handle
1495 * the balancing. But if we are the last RT task
1496 * we may need to handle the pulling of RT tasks
1497 * now.
1498 */
1499 if (!rq->rt.rt_nr_running)
1500 pull_rt_task(rq);
1501}
3d8cbdf8
RR
1502
1503static inline void init_sched_rt_class(void)
1504{
1505 unsigned int i;
1506
1507 for_each_possible_cpu(i)
6ca09dfc
MT
1508 alloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
1509 GFP_KERNEL, cpu_to_node(i));
3d8cbdf8 1510}
cb469845
SR
1511#endif /* CONFIG_SMP */
1512
1513/*
1514 * When switching a task to RT, we may overload the runqueue
1515 * with RT tasks. In this case we try to push them off to
1516 * other runqueues.
1517 */
1518static void switched_to_rt(struct rq *rq, struct task_struct *p,
1519 int running)
1520{
1521 int check_resched = 1;
1522
1523 /*
1524 * If we are already running, then there's nothing
1525 * that needs to be done. But if we are not running
1526 * we may need to preempt the current running task.
1527 * If that current running task is also an RT task
1528 * then see if we can move to another run queue.
1529 */
1530 if (!running) {
1531#ifdef CONFIG_SMP
1532 if (rq->rt.overloaded && push_rt_task(rq) &&
1533 /* Don't resched if we changed runqueues */
1534 rq != task_rq(p))
1535 check_resched = 0;
1536#endif /* CONFIG_SMP */
1537 if (check_resched && p->prio < rq->curr->prio)
1538 resched_task(rq->curr);
1539 }
1540}
1541
1542/*
1543 * Priority of the task has changed. This may cause
1544 * us to initiate a push or pull.
1545 */
1546static void prio_changed_rt(struct rq *rq, struct task_struct *p,
1547 int oldprio, int running)
1548{
1549 if (running) {
1550#ifdef CONFIG_SMP
1551 /*
1552 * If our priority decreases while running, we
1553 * may need to pull tasks to this runqueue.
1554 */
1555 if (oldprio < p->prio)
1556 pull_rt_task(rq);
1557 /*
1558 * If there's a higher priority task waiting to run
6fa46fa5
SR
1559 * then reschedule. Note, the above pull_rt_task
1560 * can release the rq lock and p could migrate.
1561 * Only reschedule if p is still on the same runqueue.
cb469845 1562 */
e864c499 1563 if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
cb469845
SR
1564 resched_task(p);
1565#else
1566 /* For UP simply resched on drop of prio */
1567 if (oldprio < p->prio)
1568 resched_task(p);
e8fa1362 1569#endif /* CONFIG_SMP */
cb469845
SR
1570 } else {
1571 /*
1572 * This task is not running, but if it is
1573 * greater than the current running task
1574 * then reschedule.
1575 */
1576 if (p->prio < rq->curr->prio)
1577 resched_task(rq->curr);
1578 }
1579}
1580
78f2c7db
PZ
1581static void watchdog(struct rq *rq, struct task_struct *p)
1582{
1583 unsigned long soft, hard;
1584
1585 if (!p->signal)
1586 return;
1587
1588 soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
1589 hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
1590
1591 if (soft != RLIM_INFINITY) {
1592 unsigned long next;
1593
1594 p->rt.timeout++;
1595 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
5a52dd50 1596 if (p->rt.timeout > next)
f06febc9 1597 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
78f2c7db
PZ
1598 }
1599}
bb44e5d1 1600
8f4d37ec 1601static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
bb44e5d1 1602{
67e2be02
PZ
1603 update_curr_rt(rq);
1604
78f2c7db
PZ
1605 watchdog(rq, p);
1606
bb44e5d1
IM
1607 /*
1608 * RR tasks need a special form of timeslice management.
1609 * FIFO tasks have no timeslices.
1610 */
1611 if (p->policy != SCHED_RR)
1612 return;
1613
fa717060 1614 if (--p->rt.time_slice)
bb44e5d1
IM
1615 return;
1616
fa717060 1617 p->rt.time_slice = DEF_TIMESLICE;
bb44e5d1 1618
98fbc798
DA
1619 /*
1620 * Requeue to the end of queue if we are not the only element
1621 * on the queue:
1622 */
fa717060 1623 if (p->rt.run_list.prev != p->rt.run_list.next) {
7ebefa8c 1624 requeue_task_rt(rq, p, 0);
98fbc798
DA
1625 set_tsk_need_resched(p);
1626 }
bb44e5d1
IM
1627}
1628
83b699ed
SV
1629static void set_curr_task_rt(struct rq *rq)
1630{
1631 struct task_struct *p = rq->curr;
1632
1633 p->se.exec_start = rq->clock;
917b627d
GH
1634
1635 /* The running task is never eligible for pushing */
1636 dequeue_pushable_task(rq, p);
83b699ed
SV
1637}
1638
2abdad0a 1639static const struct sched_class rt_sched_class = {
5522d5d5 1640 .next = &fair_sched_class,
bb44e5d1
IM
1641 .enqueue_task = enqueue_task_rt,
1642 .dequeue_task = dequeue_task_rt,
1643 .yield_task = yield_task_rt,
1644
1645 .check_preempt_curr = check_preempt_curr_rt,
1646
1647 .pick_next_task = pick_next_task_rt,
1648 .put_prev_task = put_prev_task_rt,
1649
681f3e68 1650#ifdef CONFIG_SMP
4ce72a2c
LZ
1651 .select_task_rq = select_task_rq_rt,
1652
bb44e5d1 1653 .load_balance = load_balance_rt,
e1d1484f 1654 .move_one_task = move_one_task_rt,
73fe6aae 1655 .set_cpus_allowed = set_cpus_allowed_rt,
1f11eb6a
GH
1656 .rq_online = rq_online_rt,
1657 .rq_offline = rq_offline_rt,
9a897c5a 1658 .pre_schedule = pre_schedule_rt,
967fc046 1659 .needs_post_schedule = needs_post_schedule_rt,
9a897c5a
SR
1660 .post_schedule = post_schedule_rt,
1661 .task_wake_up = task_wake_up_rt,
cb469845 1662 .switched_from = switched_from_rt,
681f3e68 1663#endif
bb44e5d1 1664
83b699ed 1665 .set_curr_task = set_curr_task_rt,
bb44e5d1 1666 .task_tick = task_tick_rt,
cb469845
SR
1667
1668 .prio_changed = prio_changed_rt,
1669 .switched_to = switched_to_rt,
bb44e5d1 1670};
ada18de2
PZ
1671
1672#ifdef CONFIG_SCHED_DEBUG
1673extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
1674
1675static void print_rt_stats(struct seq_file *m, int cpu)
1676{
1677 struct rt_rq *rt_rq;
1678
1679 rcu_read_lock();
1680 for_each_leaf_rt_rq(rt_rq, cpu_rq(cpu))
1681 print_rt_rq(m, cpu, rt_rq);
1682 rcu_read_unlock();
1683}
55e12e5e 1684#endif /* CONFIG_SCHED_DEBUG */
0e3900e6 1685