sched/rt: Keep period timer ticking when rt throttling is active
[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
029632fb
PZ
6#include "sched.h"
7
8#include <linux/slab.h>
9
10static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
11
12struct rt_bandwidth def_rt_bandwidth;
13
14static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
15{
16 struct rt_bandwidth *rt_b =
17 container_of(timer, struct rt_bandwidth, rt_period_timer);
18 ktime_t now;
19 int overrun;
20 int idle = 0;
21
22 for (;;) {
23 now = hrtimer_cb_get_time(timer);
24 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
25
26 if (!overrun)
27 break;
28
29 idle = do_sched_rt_period_timer(rt_b, overrun);
30 }
31
32 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
33}
34
35void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
36{
37 rt_b->rt_period = ns_to_ktime(period);
38 rt_b->rt_runtime = runtime;
39
40 raw_spin_lock_init(&rt_b->rt_runtime_lock);
41
42 hrtimer_init(&rt_b->rt_period_timer,
43 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
44 rt_b->rt_period_timer.function = sched_rt_period_timer;
45}
46
47static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
48{
49 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
50 return;
51
52 if (hrtimer_active(&rt_b->rt_period_timer))
53 return;
54
55 raw_spin_lock(&rt_b->rt_runtime_lock);
56 start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period);
57 raw_spin_unlock(&rt_b->rt_runtime_lock);
58}
59
60void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
61{
62 struct rt_prio_array *array;
63 int i;
64
65 array = &rt_rq->active;
66 for (i = 0; i < MAX_RT_PRIO; i++) {
67 INIT_LIST_HEAD(array->queue + i);
68 __clear_bit(i, array->bitmap);
69 }
70 /* delimiter for bitsearch: */
71 __set_bit(MAX_RT_PRIO, array->bitmap);
72
73#if defined CONFIG_SMP
74 rt_rq->highest_prio.curr = MAX_RT_PRIO;
75 rt_rq->highest_prio.next = MAX_RT_PRIO;
76 rt_rq->rt_nr_migratory = 0;
77 rt_rq->overloaded = 0;
78 plist_head_init(&rt_rq->pushable_tasks);
79#endif
80
81 rt_rq->rt_time = 0;
82 rt_rq->rt_throttled = 0;
83 rt_rq->rt_runtime = 0;
84 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
85}
86
8f48894f 87#ifdef CONFIG_RT_GROUP_SCHED
029632fb
PZ
88static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
89{
90 hrtimer_cancel(&rt_b->rt_period_timer);
91}
8f48894f
PZ
92
93#define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
94
398a153b
GH
95static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
96{
8f48894f
PZ
97#ifdef CONFIG_SCHED_DEBUG
98 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
99#endif
398a153b
GH
100 return container_of(rt_se, struct task_struct, rt);
101}
102
398a153b
GH
103static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
104{
105 return rt_rq->rq;
106}
107
108static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
109{
110 return rt_se->rt_rq;
111}
112
029632fb
PZ
113void free_rt_sched_group(struct task_group *tg)
114{
115 int i;
116
117 if (tg->rt_se)
118 destroy_rt_bandwidth(&tg->rt_bandwidth);
119
120 for_each_possible_cpu(i) {
121 if (tg->rt_rq)
122 kfree(tg->rt_rq[i]);
123 if (tg->rt_se)
124 kfree(tg->rt_se[i]);
125 }
126
127 kfree(tg->rt_rq);
128 kfree(tg->rt_se);
129}
130
131void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
132 struct sched_rt_entity *rt_se, int cpu,
133 struct sched_rt_entity *parent)
134{
135 struct rq *rq = cpu_rq(cpu);
136
137 rt_rq->highest_prio.curr = MAX_RT_PRIO;
138 rt_rq->rt_nr_boosted = 0;
139 rt_rq->rq = rq;
140 rt_rq->tg = tg;
141
142 tg->rt_rq[cpu] = rt_rq;
143 tg->rt_se[cpu] = rt_se;
144
145 if (!rt_se)
146 return;
147
148 if (!parent)
149 rt_se->rt_rq = &rq->rt;
150 else
151 rt_se->rt_rq = parent->my_q;
152
153 rt_se->my_q = rt_rq;
154 rt_se->parent = parent;
155 INIT_LIST_HEAD(&rt_se->run_list);
156}
157
158int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
159{
160 struct rt_rq *rt_rq;
161 struct sched_rt_entity *rt_se;
162 int i;
163
164 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
165 if (!tg->rt_rq)
166 goto err;
167 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
168 if (!tg->rt_se)
169 goto err;
170
171 init_rt_bandwidth(&tg->rt_bandwidth,
172 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
173
174 for_each_possible_cpu(i) {
175 rt_rq = kzalloc_node(sizeof(struct rt_rq),
176 GFP_KERNEL, cpu_to_node(i));
177 if (!rt_rq)
178 goto err;
179
180 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
181 GFP_KERNEL, cpu_to_node(i));
182 if (!rt_se)
183 goto err_free_rq;
184
185 init_rt_rq(rt_rq, cpu_rq(i));
186 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
187 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
188 }
189
190 return 1;
191
192err_free_rq:
193 kfree(rt_rq);
194err:
195 return 0;
196}
197
398a153b
GH
198#else /* CONFIG_RT_GROUP_SCHED */
199
a1ba4d8b
PZ
200#define rt_entity_is_task(rt_se) (1)
201
8f48894f
PZ
202static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
203{
204 return container_of(rt_se, struct task_struct, rt);
205}
206
398a153b
GH
207static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
208{
209 return container_of(rt_rq, struct rq, rt);
210}
211
212static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
213{
214 struct task_struct *p = rt_task_of(rt_se);
215 struct rq *rq = task_rq(p);
216
217 return &rq->rt;
218}
219
029632fb
PZ
220void free_rt_sched_group(struct task_group *tg) { }
221
222int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
223{
224 return 1;
225}
398a153b
GH
226#endif /* CONFIG_RT_GROUP_SCHED */
227
4fd29176 228#ifdef CONFIG_SMP
84de4274 229
637f5085 230static inline int rt_overloaded(struct rq *rq)
4fd29176 231{
637f5085 232 return atomic_read(&rq->rd->rto_count);
4fd29176 233}
84de4274 234
4fd29176
SR
235static inline void rt_set_overload(struct rq *rq)
236{
1f11eb6a
GH
237 if (!rq->online)
238 return;
239
c6c4927b 240 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
4fd29176
SR
241 /*
242 * Make sure the mask is visible before we set
243 * the overload count. That is checked to determine
244 * if we should look at the mask. It would be a shame
245 * if we looked at the mask, but the mask was not
246 * updated yet.
247 */
248 wmb();
637f5085 249 atomic_inc(&rq->rd->rto_count);
4fd29176 250}
84de4274 251
4fd29176
SR
252static inline void rt_clear_overload(struct rq *rq)
253{
1f11eb6a
GH
254 if (!rq->online)
255 return;
256
4fd29176 257 /* the order here really doesn't matter */
637f5085 258 atomic_dec(&rq->rd->rto_count);
c6c4927b 259 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
4fd29176 260}
73fe6aae 261
398a153b 262static void update_rt_migration(struct rt_rq *rt_rq)
73fe6aae 263{
a1ba4d8b 264 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
398a153b
GH
265 if (!rt_rq->overloaded) {
266 rt_set_overload(rq_of_rt_rq(rt_rq));
267 rt_rq->overloaded = 1;
cdc8eb98 268 }
398a153b
GH
269 } else if (rt_rq->overloaded) {
270 rt_clear_overload(rq_of_rt_rq(rt_rq));
271 rt_rq->overloaded = 0;
637f5085 272 }
73fe6aae 273}
4fd29176 274
398a153b
GH
275static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
276{
a1ba4d8b
PZ
277 if (!rt_entity_is_task(rt_se))
278 return;
279
280 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
281
282 rt_rq->rt_nr_total++;
398a153b
GH
283 if (rt_se->nr_cpus_allowed > 1)
284 rt_rq->rt_nr_migratory++;
285
286 update_rt_migration(rt_rq);
287}
288
289static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
290{
a1ba4d8b
PZ
291 if (!rt_entity_is_task(rt_se))
292 return;
293
294 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
295
296 rt_rq->rt_nr_total--;
398a153b
GH
297 if (rt_se->nr_cpus_allowed > 1)
298 rt_rq->rt_nr_migratory--;
299
300 update_rt_migration(rt_rq);
301}
302
5181f4a4
SR
303static inline int has_pushable_tasks(struct rq *rq)
304{
305 return !plist_head_empty(&rq->rt.pushable_tasks);
306}
307
917b627d
GH
308static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
309{
310 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
311 plist_node_init(&p->pushable_tasks, p->prio);
312 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
5181f4a4
SR
313
314 /* Update the highest prio pushable task */
315 if (p->prio < rq->rt.highest_prio.next)
316 rq->rt.highest_prio.next = p->prio;
917b627d
GH
317}
318
319static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
320{
321 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
917b627d 322
5181f4a4
SR
323 /* Update the new highest prio pushable task */
324 if (has_pushable_tasks(rq)) {
325 p = plist_first_entry(&rq->rt.pushable_tasks,
326 struct task_struct, pushable_tasks);
327 rq->rt.highest_prio.next = p->prio;
328 } else
329 rq->rt.highest_prio.next = MAX_RT_PRIO;
bcf08df3
IM
330}
331
917b627d
GH
332#else
333
ceacc2c1 334static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
fa85ae24 335{
6f505b16
PZ
336}
337
ceacc2c1
PZ
338static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
339{
340}
341
b07430ac 342static inline
ceacc2c1
PZ
343void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
344{
345}
346
398a153b 347static inline
ceacc2c1
PZ
348void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
349{
350}
917b627d 351
4fd29176
SR
352#endif /* CONFIG_SMP */
353
6f505b16
PZ
354static inline int on_rt_rq(struct sched_rt_entity *rt_se)
355{
356 return !list_empty(&rt_se->run_list);
357}
358
052f1dc7 359#ifdef CONFIG_RT_GROUP_SCHED
6f505b16 360
9f0c1e56 361static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
6f505b16
PZ
362{
363 if (!rt_rq->tg)
9f0c1e56 364 return RUNTIME_INF;
6f505b16 365
ac086bc2
PZ
366 return rt_rq->rt_runtime;
367}
368
369static inline u64 sched_rt_period(struct rt_rq *rt_rq)
370{
371 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
6f505b16
PZ
372}
373
ec514c48
CX
374typedef struct task_group *rt_rq_iter_t;
375
1c09ab0d
YZ
376static inline struct task_group *next_task_group(struct task_group *tg)
377{
378 do {
379 tg = list_entry_rcu(tg->list.next,
380 typeof(struct task_group), list);
381 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
382
383 if (&tg->list == &task_groups)
384 tg = NULL;
385
386 return tg;
387}
388
389#define for_each_rt_rq(rt_rq, iter, rq) \
390 for (iter = container_of(&task_groups, typeof(*iter), list); \
391 (iter = next_task_group(iter)) && \
392 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
ec514c48 393
3d4b47b4
PZ
394static inline void list_add_leaf_rt_rq(struct rt_rq *rt_rq)
395{
396 list_add_rcu(&rt_rq->leaf_rt_rq_list,
397 &rq_of_rt_rq(rt_rq)->leaf_rt_rq_list);
398}
399
400static inline void list_del_leaf_rt_rq(struct rt_rq *rt_rq)
401{
402 list_del_rcu(&rt_rq->leaf_rt_rq_list);
403}
404
6f505b16 405#define for_each_leaf_rt_rq(rt_rq, rq) \
80f40ee4 406 list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
6f505b16 407
6f505b16
PZ
408#define for_each_sched_rt_entity(rt_se) \
409 for (; rt_se; rt_se = rt_se->parent)
410
411static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
412{
413 return rt_se->my_q;
414}
415
37dad3fc 416static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head);
6f505b16
PZ
417static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
418
9f0c1e56 419static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
6f505b16 420{
f6121f4f 421 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
74b7eb58
YZ
422 struct sched_rt_entity *rt_se;
423
0c3b9168
BS
424 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
425
426 rt_se = rt_rq->tg->rt_se[cpu];
6f505b16 427
f6121f4f
DF
428 if (rt_rq->rt_nr_running) {
429 if (rt_se && !on_rt_rq(rt_se))
37dad3fc 430 enqueue_rt_entity(rt_se, false);
e864c499 431 if (rt_rq->highest_prio.curr < curr->prio)
1020387f 432 resched_task(curr);
6f505b16
PZ
433 }
434}
435
9f0c1e56 436static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
6f505b16 437{
74b7eb58 438 struct sched_rt_entity *rt_se;
0c3b9168 439 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
74b7eb58 440
0c3b9168 441 rt_se = rt_rq->tg->rt_se[cpu];
6f505b16
PZ
442
443 if (rt_se && on_rt_rq(rt_se))
444 dequeue_rt_entity(rt_se);
445}
446
23b0fdfc
PZ
447static inline int rt_rq_throttled(struct rt_rq *rt_rq)
448{
449 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
450}
451
452static int rt_se_boosted(struct sched_rt_entity *rt_se)
453{
454 struct rt_rq *rt_rq = group_rt_rq(rt_se);
455 struct task_struct *p;
456
457 if (rt_rq)
458 return !!rt_rq->rt_nr_boosted;
459
460 p = rt_task_of(rt_se);
461 return p->prio != p->normal_prio;
462}
463
d0b27fa7 464#ifdef CONFIG_SMP
c6c4927b 465static inline const struct cpumask *sched_rt_period_mask(void)
d0b27fa7
PZ
466{
467 return cpu_rq(smp_processor_id())->rd->span;
468}
6f505b16 469#else
c6c4927b 470static inline const struct cpumask *sched_rt_period_mask(void)
d0b27fa7 471{
c6c4927b 472 return cpu_online_mask;
d0b27fa7
PZ
473}
474#endif
6f505b16 475
d0b27fa7
PZ
476static inline
477struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
6f505b16 478{
d0b27fa7
PZ
479 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
480}
9f0c1e56 481
ac086bc2
PZ
482static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
483{
484 return &rt_rq->tg->rt_bandwidth;
485}
486
55e12e5e 487#else /* !CONFIG_RT_GROUP_SCHED */
d0b27fa7
PZ
488
489static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
490{
ac086bc2
PZ
491 return rt_rq->rt_runtime;
492}
493
494static inline u64 sched_rt_period(struct rt_rq *rt_rq)
495{
496 return ktime_to_ns(def_rt_bandwidth.rt_period);
6f505b16
PZ
497}
498
ec514c48
CX
499typedef struct rt_rq *rt_rq_iter_t;
500
501#define for_each_rt_rq(rt_rq, iter, rq) \
502 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
503
3d4b47b4
PZ
504static inline void list_add_leaf_rt_rq(struct rt_rq *rt_rq)
505{
506}
507
508static inline void list_del_leaf_rt_rq(struct rt_rq *rt_rq)
509{
510}
511
6f505b16
PZ
512#define for_each_leaf_rt_rq(rt_rq, rq) \
513 for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
514
6f505b16
PZ
515#define for_each_sched_rt_entity(rt_se) \
516 for (; rt_se; rt_se = NULL)
517
518static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
519{
520 return NULL;
521}
522
9f0c1e56 523static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
6f505b16 524{
f3ade837
JB
525 if (rt_rq->rt_nr_running)
526 resched_task(rq_of_rt_rq(rt_rq)->curr);
6f505b16
PZ
527}
528
9f0c1e56 529static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
6f505b16
PZ
530{
531}
532
23b0fdfc
PZ
533static inline int rt_rq_throttled(struct rt_rq *rt_rq)
534{
535 return rt_rq->rt_throttled;
536}
d0b27fa7 537
c6c4927b 538static inline const struct cpumask *sched_rt_period_mask(void)
d0b27fa7 539{
c6c4927b 540 return cpu_online_mask;
d0b27fa7
PZ
541}
542
543static inline
544struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
545{
546 return &cpu_rq(cpu)->rt;
547}
548
ac086bc2
PZ
549static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
550{
551 return &def_rt_bandwidth;
552}
553
55e12e5e 554#endif /* CONFIG_RT_GROUP_SCHED */
d0b27fa7 555
ac086bc2 556#ifdef CONFIG_SMP
78333cdd
PZ
557/*
558 * We ran out of runtime, see if we can borrow some from our neighbours.
559 */
b79f3833 560static int do_balance_runtime(struct rt_rq *rt_rq)
ac086bc2
PZ
561{
562 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
563 struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
564 int i, weight, more = 0;
565 u64 rt_period;
566
c6c4927b 567 weight = cpumask_weight(rd->span);
ac086bc2 568
0986b11b 569 raw_spin_lock(&rt_b->rt_runtime_lock);
ac086bc2 570 rt_period = ktime_to_ns(rt_b->rt_period);
c6c4927b 571 for_each_cpu(i, rd->span) {
ac086bc2
PZ
572 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
573 s64 diff;
574
575 if (iter == rt_rq)
576 continue;
577
0986b11b 578 raw_spin_lock(&iter->rt_runtime_lock);
78333cdd
PZ
579 /*
580 * Either all rqs have inf runtime and there's nothing to steal
581 * or __disable_runtime() below sets a specific rq to inf to
582 * indicate its been disabled and disalow stealing.
583 */
7def2be1
PZ
584 if (iter->rt_runtime == RUNTIME_INF)
585 goto next;
586
78333cdd
PZ
587 /*
588 * From runqueues with spare time, take 1/n part of their
589 * spare time, but no more than our period.
590 */
ac086bc2
PZ
591 diff = iter->rt_runtime - iter->rt_time;
592 if (diff > 0) {
58838cf3 593 diff = div_u64((u64)diff, weight);
ac086bc2
PZ
594 if (rt_rq->rt_runtime + diff > rt_period)
595 diff = rt_period - rt_rq->rt_runtime;
596 iter->rt_runtime -= diff;
597 rt_rq->rt_runtime += diff;
598 more = 1;
599 if (rt_rq->rt_runtime == rt_period) {
0986b11b 600 raw_spin_unlock(&iter->rt_runtime_lock);
ac086bc2
PZ
601 break;
602 }
603 }
7def2be1 604next:
0986b11b 605 raw_spin_unlock(&iter->rt_runtime_lock);
ac086bc2 606 }
0986b11b 607 raw_spin_unlock(&rt_b->rt_runtime_lock);
ac086bc2
PZ
608
609 return more;
610}
7def2be1 611
78333cdd
PZ
612/*
613 * Ensure this RQ takes back all the runtime it lend to its neighbours.
614 */
7def2be1
PZ
615static void __disable_runtime(struct rq *rq)
616{
617 struct root_domain *rd = rq->rd;
ec514c48 618 rt_rq_iter_t iter;
7def2be1
PZ
619 struct rt_rq *rt_rq;
620
621 if (unlikely(!scheduler_running))
622 return;
623
ec514c48 624 for_each_rt_rq(rt_rq, iter, rq) {
7def2be1
PZ
625 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
626 s64 want;
627 int i;
628
0986b11b
TG
629 raw_spin_lock(&rt_b->rt_runtime_lock);
630 raw_spin_lock(&rt_rq->rt_runtime_lock);
78333cdd
PZ
631 /*
632 * Either we're all inf and nobody needs to borrow, or we're
633 * already disabled and thus have nothing to do, or we have
634 * exactly the right amount of runtime to take out.
635 */
7def2be1
PZ
636 if (rt_rq->rt_runtime == RUNTIME_INF ||
637 rt_rq->rt_runtime == rt_b->rt_runtime)
638 goto balanced;
0986b11b 639 raw_spin_unlock(&rt_rq->rt_runtime_lock);
7def2be1 640
78333cdd
PZ
641 /*
642 * Calculate the difference between what we started out with
643 * and what we current have, that's the amount of runtime
644 * we lend and now have to reclaim.
645 */
7def2be1
PZ
646 want = rt_b->rt_runtime - rt_rq->rt_runtime;
647
78333cdd
PZ
648 /*
649 * Greedy reclaim, take back as much as we can.
650 */
c6c4927b 651 for_each_cpu(i, rd->span) {
7def2be1
PZ
652 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
653 s64 diff;
654
78333cdd
PZ
655 /*
656 * Can't reclaim from ourselves or disabled runqueues.
657 */
f1679d08 658 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
7def2be1
PZ
659 continue;
660
0986b11b 661 raw_spin_lock(&iter->rt_runtime_lock);
7def2be1
PZ
662 if (want > 0) {
663 diff = min_t(s64, iter->rt_runtime, want);
664 iter->rt_runtime -= diff;
665 want -= diff;
666 } else {
667 iter->rt_runtime -= want;
668 want -= want;
669 }
0986b11b 670 raw_spin_unlock(&iter->rt_runtime_lock);
7def2be1
PZ
671
672 if (!want)
673 break;
674 }
675
0986b11b 676 raw_spin_lock(&rt_rq->rt_runtime_lock);
78333cdd
PZ
677 /*
678 * We cannot be left wanting - that would mean some runtime
679 * leaked out of the system.
680 */
7def2be1
PZ
681 BUG_ON(want);
682balanced:
78333cdd
PZ
683 /*
684 * Disable all the borrow logic by pretending we have inf
685 * runtime - in which case borrowing doesn't make sense.
686 */
7def2be1 687 rt_rq->rt_runtime = RUNTIME_INF;
0986b11b
TG
688 raw_spin_unlock(&rt_rq->rt_runtime_lock);
689 raw_spin_unlock(&rt_b->rt_runtime_lock);
7def2be1
PZ
690 }
691}
692
693static void disable_runtime(struct rq *rq)
694{
695 unsigned long flags;
696
05fa785c 697 raw_spin_lock_irqsave(&rq->lock, flags);
7def2be1 698 __disable_runtime(rq);
05fa785c 699 raw_spin_unlock_irqrestore(&rq->lock, flags);
7def2be1
PZ
700}
701
702static void __enable_runtime(struct rq *rq)
703{
ec514c48 704 rt_rq_iter_t iter;
7def2be1
PZ
705 struct rt_rq *rt_rq;
706
707 if (unlikely(!scheduler_running))
708 return;
709
78333cdd
PZ
710 /*
711 * Reset each runqueue's bandwidth settings
712 */
ec514c48 713 for_each_rt_rq(rt_rq, iter, rq) {
7def2be1
PZ
714 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
715
0986b11b
TG
716 raw_spin_lock(&rt_b->rt_runtime_lock);
717 raw_spin_lock(&rt_rq->rt_runtime_lock);
7def2be1
PZ
718 rt_rq->rt_runtime = rt_b->rt_runtime;
719 rt_rq->rt_time = 0;
baf25731 720 rt_rq->rt_throttled = 0;
0986b11b
TG
721 raw_spin_unlock(&rt_rq->rt_runtime_lock);
722 raw_spin_unlock(&rt_b->rt_runtime_lock);
7def2be1
PZ
723 }
724}
725
726static void enable_runtime(struct rq *rq)
727{
728 unsigned long flags;
729
05fa785c 730 raw_spin_lock_irqsave(&rq->lock, flags);
7def2be1 731 __enable_runtime(rq);
05fa785c 732 raw_spin_unlock_irqrestore(&rq->lock, flags);
7def2be1
PZ
733}
734
029632fb
PZ
735int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu)
736{
737 int cpu = (int)(long)hcpu;
738
739 switch (action) {
740 case CPU_DOWN_PREPARE:
741 case CPU_DOWN_PREPARE_FROZEN:
742 disable_runtime(cpu_rq(cpu));
743 return NOTIFY_OK;
744
745 case CPU_DOWN_FAILED:
746 case CPU_DOWN_FAILED_FROZEN:
747 case CPU_ONLINE:
748 case CPU_ONLINE_FROZEN:
749 enable_runtime(cpu_rq(cpu));
750 return NOTIFY_OK;
751
752 default:
753 return NOTIFY_DONE;
754 }
755}
756
eff6549b
PZ
757static int balance_runtime(struct rt_rq *rt_rq)
758{
759 int more = 0;
760
4a6184ce
PZ
761 if (!sched_feat(RT_RUNTIME_SHARE))
762 return more;
763
eff6549b 764 if (rt_rq->rt_time > rt_rq->rt_runtime) {
0986b11b 765 raw_spin_unlock(&rt_rq->rt_runtime_lock);
eff6549b 766 more = do_balance_runtime(rt_rq);
0986b11b 767 raw_spin_lock(&rt_rq->rt_runtime_lock);
eff6549b
PZ
768 }
769
770 return more;
771}
55e12e5e 772#else /* !CONFIG_SMP */
eff6549b
PZ
773static inline int balance_runtime(struct rt_rq *rt_rq)
774{
775 return 0;
776}
55e12e5e 777#endif /* CONFIG_SMP */
ac086bc2 778
eff6549b
PZ
779static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
780{
42c62a58 781 int i, idle = 1, throttled = 0;
c6c4927b 782 const struct cpumask *span;
eff6549b 783
eff6549b 784 span = sched_rt_period_mask();
c6c4927b 785 for_each_cpu(i, span) {
eff6549b
PZ
786 int enqueue = 0;
787 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
788 struct rq *rq = rq_of_rt_rq(rt_rq);
789
05fa785c 790 raw_spin_lock(&rq->lock);
eff6549b
PZ
791 if (rt_rq->rt_time) {
792 u64 runtime;
793
0986b11b 794 raw_spin_lock(&rt_rq->rt_runtime_lock);
eff6549b
PZ
795 if (rt_rq->rt_throttled)
796 balance_runtime(rt_rq);
797 runtime = rt_rq->rt_runtime;
798 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
799 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
800 rt_rq->rt_throttled = 0;
801 enqueue = 1;
61eadef6
MG
802
803 /*
804 * Force a clock update if the CPU was idle,
805 * lest wakeup -> unthrottle time accumulate.
806 */
807 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
808 rq->skip_clock_update = -1;
eff6549b
PZ
809 }
810 if (rt_rq->rt_time || rt_rq->rt_nr_running)
811 idle = 0;
0986b11b 812 raw_spin_unlock(&rt_rq->rt_runtime_lock);
0c3b9168 813 } else if (rt_rq->rt_nr_running) {
6c3df255 814 idle = 0;
0c3b9168
BS
815 if (!rt_rq_throttled(rt_rq))
816 enqueue = 1;
817 }
42c62a58
PZ
818 if (rt_rq->rt_throttled)
819 throttled = 1;
eff6549b
PZ
820
821 if (enqueue)
822 sched_rt_rq_enqueue(rt_rq);
05fa785c 823 raw_spin_unlock(&rq->lock);
eff6549b
PZ
824 }
825
42c62a58
PZ
826 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
827 return 1;
828
eff6549b
PZ
829 return idle;
830}
ac086bc2 831
6f505b16
PZ
832static inline int rt_se_prio(struct sched_rt_entity *rt_se)
833{
052f1dc7 834#ifdef CONFIG_RT_GROUP_SCHED
6f505b16
PZ
835 struct rt_rq *rt_rq = group_rt_rq(rt_se);
836
837 if (rt_rq)
e864c499 838 return rt_rq->highest_prio.curr;
6f505b16
PZ
839#endif
840
841 return rt_task_of(rt_se)->prio;
842}
843
9f0c1e56 844static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
6f505b16 845{
9f0c1e56 846 u64 runtime = sched_rt_runtime(rt_rq);
fa85ae24 847
fa85ae24 848 if (rt_rq->rt_throttled)
23b0fdfc 849 return rt_rq_throttled(rt_rq);
fa85ae24 850
5b680fd6 851 if (runtime >= sched_rt_period(rt_rq))
ac086bc2
PZ
852 return 0;
853
b79f3833
PZ
854 balance_runtime(rt_rq);
855 runtime = sched_rt_runtime(rt_rq);
856 if (runtime == RUNTIME_INF)
857 return 0;
ac086bc2 858
9f0c1e56 859 if (rt_rq->rt_time > runtime) {
6f505b16 860 rt_rq->rt_throttled = 1;
1c83437e 861 printk_once(KERN_WARNING "sched: RT throttling activated\n");
23b0fdfc 862 if (rt_rq_throttled(rt_rq)) {
9f0c1e56 863 sched_rt_rq_dequeue(rt_rq);
23b0fdfc
PZ
864 return 1;
865 }
fa85ae24
PZ
866 }
867
868 return 0;
869}
870
bb44e5d1
IM
871/*
872 * Update the current task's runtime statistics. Skip current tasks that
873 * are not in our scheduling class.
874 */
a9957449 875static void update_curr_rt(struct rq *rq)
bb44e5d1
IM
876{
877 struct task_struct *curr = rq->curr;
6f505b16
PZ
878 struct sched_rt_entity *rt_se = &curr->rt;
879 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
bb44e5d1
IM
880 u64 delta_exec;
881
06c3bc65 882 if (curr->sched_class != &rt_sched_class)
bb44e5d1
IM
883 return;
884
305e6835 885 delta_exec = rq->clock_task - curr->se.exec_start;
bb44e5d1
IM
886 if (unlikely((s64)delta_exec < 0))
887 delta_exec = 0;
6cfb0d5d 888
42c62a58
PZ
889 schedstat_set(curr->se.statistics.exec_max,
890 max(curr->se.statistics.exec_max, delta_exec));
bb44e5d1
IM
891
892 curr->se.sum_exec_runtime += delta_exec;
f06febc9
FM
893 account_group_exec_runtime(curr, delta_exec);
894
305e6835 895 curr->se.exec_start = rq->clock_task;
d842de87 896 cpuacct_charge(curr, delta_exec);
fa85ae24 897
e9e9250b
PZ
898 sched_rt_avg_update(rq, delta_exec);
899
0b148fa0
PZ
900 if (!rt_bandwidth_enabled())
901 return;
902
354d60c2
DG
903 for_each_sched_rt_entity(rt_se) {
904 rt_rq = rt_rq_of_se(rt_se);
905
cc2991cf 906 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
0986b11b 907 raw_spin_lock(&rt_rq->rt_runtime_lock);
cc2991cf
PZ
908 rt_rq->rt_time += delta_exec;
909 if (sched_rt_runtime_exceeded(rt_rq))
910 resched_task(curr);
0986b11b 911 raw_spin_unlock(&rt_rq->rt_runtime_lock);
cc2991cf 912 }
354d60c2 913 }
bb44e5d1
IM
914}
915
398a153b 916#if defined CONFIG_SMP
e864c499 917
398a153b
GH
918static void
919inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
63489e45 920{
4d984277 921 struct rq *rq = rq_of_rt_rq(rt_rq);
1f11eb6a 922
5181f4a4
SR
923 if (rq->online && prio < prev_prio)
924 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
398a153b 925}
73fe6aae 926
398a153b
GH
927static void
928dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
929{
930 struct rq *rq = rq_of_rt_rq(rt_rq);
d0b27fa7 931
398a153b
GH
932 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
933 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
63489e45
SR
934}
935
398a153b
GH
936#else /* CONFIG_SMP */
937
6f505b16 938static inline
398a153b
GH
939void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
940static inline
941void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
942
943#endif /* CONFIG_SMP */
6e0534f2 944
052f1dc7 945#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
398a153b
GH
946static void
947inc_rt_prio(struct rt_rq *rt_rq, int prio)
948{
949 int prev_prio = rt_rq->highest_prio.curr;
950
951 if (prio < prev_prio)
952 rt_rq->highest_prio.curr = prio;
953
954 inc_rt_prio_smp(rt_rq, prio, prev_prio);
955}
956
957static void
958dec_rt_prio(struct rt_rq *rt_rq, int prio)
959{
960 int prev_prio = rt_rq->highest_prio.curr;
961
6f505b16 962 if (rt_rq->rt_nr_running) {
764a9d6f 963
398a153b 964 WARN_ON(prio < prev_prio);
764a9d6f 965
e864c499 966 /*
398a153b
GH
967 * This may have been our highest task, and therefore
968 * we may have some recomputation to do
e864c499 969 */
398a153b 970 if (prio == prev_prio) {
e864c499
GH
971 struct rt_prio_array *array = &rt_rq->active;
972
973 rt_rq->highest_prio.curr =
764a9d6f 974 sched_find_first_bit(array->bitmap);
e864c499
GH
975 }
976
764a9d6f 977 } else
e864c499 978 rt_rq->highest_prio.curr = MAX_RT_PRIO;
73fe6aae 979
398a153b
GH
980 dec_rt_prio_smp(rt_rq, prio, prev_prio);
981}
1f11eb6a 982
398a153b
GH
983#else
984
985static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
986static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
987
988#endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
6e0534f2 989
052f1dc7 990#ifdef CONFIG_RT_GROUP_SCHED
398a153b
GH
991
992static void
993inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
994{
995 if (rt_se_boosted(rt_se))
996 rt_rq->rt_nr_boosted++;
997
998 if (rt_rq->tg)
999 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
1000}
1001
1002static void
1003dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1004{
23b0fdfc
PZ
1005 if (rt_se_boosted(rt_se))
1006 rt_rq->rt_nr_boosted--;
1007
1008 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
398a153b
GH
1009}
1010
1011#else /* CONFIG_RT_GROUP_SCHED */
1012
1013static void
1014inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1015{
1016 start_rt_bandwidth(&def_rt_bandwidth);
1017}
1018
1019static inline
1020void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1021
1022#endif /* CONFIG_RT_GROUP_SCHED */
1023
1024static inline
1025void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1026{
1027 int prio = rt_se_prio(rt_se);
1028
1029 WARN_ON(!rt_prio(prio));
1030 rt_rq->rt_nr_running++;
1031
1032 inc_rt_prio(rt_rq, prio);
1033 inc_rt_migration(rt_se, rt_rq);
1034 inc_rt_group(rt_se, rt_rq);
1035}
1036
1037static inline
1038void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1039{
1040 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
1041 WARN_ON(!rt_rq->rt_nr_running);
1042 rt_rq->rt_nr_running--;
1043
1044 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1045 dec_rt_migration(rt_se, rt_rq);
1046 dec_rt_group(rt_se, rt_rq);
63489e45
SR
1047}
1048
37dad3fc 1049static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
bb44e5d1 1050{
6f505b16
PZ
1051 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1052 struct rt_prio_array *array = &rt_rq->active;
1053 struct rt_rq *group_rq = group_rt_rq(rt_se);
20b6331b 1054 struct list_head *queue = array->queue + rt_se_prio(rt_se);
bb44e5d1 1055
ad2a3f13
PZ
1056 /*
1057 * Don't enqueue the group if its throttled, or when empty.
1058 * The latter is a consequence of the former when a child group
1059 * get throttled and the current group doesn't have any other
1060 * active members.
1061 */
1062 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
6f505b16 1063 return;
63489e45 1064
3d4b47b4
PZ
1065 if (!rt_rq->rt_nr_running)
1066 list_add_leaf_rt_rq(rt_rq);
1067
37dad3fc
TG
1068 if (head)
1069 list_add(&rt_se->run_list, queue);
1070 else
1071 list_add_tail(&rt_se->run_list, queue);
6f505b16 1072 __set_bit(rt_se_prio(rt_se), array->bitmap);
78f2c7db 1073
6f505b16
PZ
1074 inc_rt_tasks(rt_se, rt_rq);
1075}
1076
ad2a3f13 1077static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
6f505b16
PZ
1078{
1079 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1080 struct rt_prio_array *array = &rt_rq->active;
1081
1082 list_del_init(&rt_se->run_list);
1083 if (list_empty(array->queue + rt_se_prio(rt_se)))
1084 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1085
1086 dec_rt_tasks(rt_se, rt_rq);
3d4b47b4
PZ
1087 if (!rt_rq->rt_nr_running)
1088 list_del_leaf_rt_rq(rt_rq);
6f505b16
PZ
1089}
1090
1091/*
1092 * Because the prio of an upper entry depends on the lower
1093 * entries, we must remove entries top - down.
6f505b16 1094 */
ad2a3f13 1095static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
6f505b16 1096{
ad2a3f13 1097 struct sched_rt_entity *back = NULL;
6f505b16 1098
58d6c2d7
PZ
1099 for_each_sched_rt_entity(rt_se) {
1100 rt_se->back = back;
1101 back = rt_se;
1102 }
1103
1104 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1105 if (on_rt_rq(rt_se))
ad2a3f13
PZ
1106 __dequeue_rt_entity(rt_se);
1107 }
1108}
1109
37dad3fc 1110static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
ad2a3f13
PZ
1111{
1112 dequeue_rt_stack(rt_se);
1113 for_each_sched_rt_entity(rt_se)
37dad3fc 1114 __enqueue_rt_entity(rt_se, head);
ad2a3f13
PZ
1115}
1116
1117static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
1118{
1119 dequeue_rt_stack(rt_se);
1120
1121 for_each_sched_rt_entity(rt_se) {
1122 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1123
1124 if (rt_rq && rt_rq->rt_nr_running)
37dad3fc 1125 __enqueue_rt_entity(rt_se, false);
58d6c2d7 1126 }
bb44e5d1
IM
1127}
1128
1129/*
1130 * Adding/removing a task to/from a priority array:
1131 */
ea87bb78 1132static void
371fd7e7 1133enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
6f505b16
PZ
1134{
1135 struct sched_rt_entity *rt_se = &p->rt;
1136
371fd7e7 1137 if (flags & ENQUEUE_WAKEUP)
6f505b16
PZ
1138 rt_se->timeout = 0;
1139
371fd7e7 1140 enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
c09595f6 1141
917b627d
GH
1142 if (!task_current(rq, p) && p->rt.nr_cpus_allowed > 1)
1143 enqueue_pushable_task(rq, p);
953bfcd1
PT
1144
1145 inc_nr_running(rq);
6f505b16
PZ
1146}
1147
371fd7e7 1148static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
bb44e5d1 1149{
6f505b16 1150 struct sched_rt_entity *rt_se = &p->rt;
bb44e5d1 1151
f1e14ef6 1152 update_curr_rt(rq);
ad2a3f13 1153 dequeue_rt_entity(rt_se);
c09595f6 1154
917b627d 1155 dequeue_pushable_task(rq, p);
953bfcd1
PT
1156
1157 dec_nr_running(rq);
bb44e5d1
IM
1158}
1159
1160/*
60686317
RW
1161 * Put task to the head or the end of the run list without the overhead of
1162 * dequeue followed by enqueue.
bb44e5d1 1163 */
7ebefa8c
DA
1164static void
1165requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
6f505b16 1166{
1cdad715 1167 if (on_rt_rq(rt_se)) {
7ebefa8c
DA
1168 struct rt_prio_array *array = &rt_rq->active;
1169 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1170
1171 if (head)
1172 list_move(&rt_se->run_list, queue);
1173 else
1174 list_move_tail(&rt_se->run_list, queue);
1cdad715 1175 }
6f505b16
PZ
1176}
1177
7ebefa8c 1178static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
bb44e5d1 1179{
6f505b16
PZ
1180 struct sched_rt_entity *rt_se = &p->rt;
1181 struct rt_rq *rt_rq;
bb44e5d1 1182
6f505b16
PZ
1183 for_each_sched_rt_entity(rt_se) {
1184 rt_rq = rt_rq_of_se(rt_se);
7ebefa8c 1185 requeue_rt_entity(rt_rq, rt_se, head);
6f505b16 1186 }
bb44e5d1
IM
1187}
1188
6f505b16 1189static void yield_task_rt(struct rq *rq)
bb44e5d1 1190{
7ebefa8c 1191 requeue_task_rt(rq, rq->curr, 0);
bb44e5d1
IM
1192}
1193
e7693a36 1194#ifdef CONFIG_SMP
318e0893
GH
1195static int find_lowest_rq(struct task_struct *task);
1196
0017d735 1197static int
7608dec2 1198select_task_rq_rt(struct task_struct *p, int sd_flag, int flags)
e7693a36 1199{
7608dec2
PZ
1200 struct task_struct *curr;
1201 struct rq *rq;
1202 int cpu;
1203
7608dec2 1204 cpu = task_cpu(p);
c37495fd 1205
76854c7e
MG
1206 if (p->rt.nr_cpus_allowed == 1)
1207 goto out;
1208
c37495fd
SR
1209 /* For anything but wake ups, just return the task_cpu */
1210 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1211 goto out;
1212
7608dec2
PZ
1213 rq = cpu_rq(cpu);
1214
1215 rcu_read_lock();
1216 curr = ACCESS_ONCE(rq->curr); /* unlocked access */
1217
318e0893 1218 /*
7608dec2 1219 * If the current task on @p's runqueue is an RT task, then
e1f47d89
SR
1220 * try to see if we can wake this RT task up on another
1221 * runqueue. Otherwise simply start this RT task
1222 * on its current runqueue.
1223 *
43fa5460
SR
1224 * We want to avoid overloading runqueues. If the woken
1225 * task is a higher priority, then it will stay on this CPU
1226 * and the lower prio task should be moved to another CPU.
1227 * Even though this will probably make the lower prio task
1228 * lose its cache, we do not want to bounce a higher task
1229 * around just because it gave up its CPU, perhaps for a
1230 * lock?
1231 *
1232 * For equal prio tasks, we just let the scheduler sort it out.
7608dec2
PZ
1233 *
1234 * Otherwise, just let it ride on the affined RQ and the
1235 * post-schedule router will push the preempted task away
1236 *
1237 * This test is optimistic, if we get it wrong the load-balancer
1238 * will have to sort it out.
318e0893 1239 */
7608dec2
PZ
1240 if (curr && unlikely(rt_task(curr)) &&
1241 (curr->rt.nr_cpus_allowed < 2 ||
3be209a8 1242 curr->prio <= p->prio) &&
6f505b16 1243 (p->rt.nr_cpus_allowed > 1)) {
7608dec2 1244 int target = find_lowest_rq(p);
318e0893 1245
7608dec2
PZ
1246 if (target != -1)
1247 cpu = target;
318e0893 1248 }
7608dec2 1249 rcu_read_unlock();
318e0893 1250
c37495fd 1251out:
7608dec2 1252 return cpu;
e7693a36 1253}
7ebefa8c
DA
1254
1255static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1256{
7ebefa8c
DA
1257 if (rq->curr->rt.nr_cpus_allowed == 1)
1258 return;
1259
24600ce8 1260 if (p->rt.nr_cpus_allowed != 1
13b8bd0a
RR
1261 && cpupri_find(&rq->rd->cpupri, p, NULL))
1262 return;
24600ce8 1263
13b8bd0a
RR
1264 if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1265 return;
7ebefa8c
DA
1266
1267 /*
1268 * There appears to be other cpus that can accept
1269 * current and none to run 'p', so lets reschedule
1270 * to try and push current away:
1271 */
1272 requeue_task_rt(rq, p, 1);
1273 resched_task(rq->curr);
1274}
1275
e7693a36
GH
1276#endif /* CONFIG_SMP */
1277
bb44e5d1
IM
1278/*
1279 * Preempt the current task with a newly woken task if needed:
1280 */
7d478721 1281static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
bb44e5d1 1282{
45c01e82 1283 if (p->prio < rq->curr->prio) {
bb44e5d1 1284 resched_task(rq->curr);
45c01e82
GH
1285 return;
1286 }
1287
1288#ifdef CONFIG_SMP
1289 /*
1290 * If:
1291 *
1292 * - the newly woken task is of equal priority to the current task
1293 * - the newly woken task is non-migratable while current is migratable
1294 * - current will be preempted on the next reschedule
1295 *
1296 * we should check to see if current can readily move to a different
1297 * cpu. If so, we will reschedule to allow the push logic to try
1298 * to move current somewhere else, making room for our non-migratable
1299 * task.
1300 */
8dd0de8b 1301 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
7ebefa8c 1302 check_preempt_equal_prio(rq, p);
45c01e82 1303#endif
bb44e5d1
IM
1304}
1305
6f505b16
PZ
1306static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1307 struct rt_rq *rt_rq)
bb44e5d1 1308{
6f505b16
PZ
1309 struct rt_prio_array *array = &rt_rq->active;
1310 struct sched_rt_entity *next = NULL;
bb44e5d1
IM
1311 struct list_head *queue;
1312 int idx;
1313
1314 idx = sched_find_first_bit(array->bitmap);
6f505b16 1315 BUG_ON(idx >= MAX_RT_PRIO);
bb44e5d1
IM
1316
1317 queue = array->queue + idx;
6f505b16 1318 next = list_entry(queue->next, struct sched_rt_entity, run_list);
326587b8 1319
6f505b16
PZ
1320 return next;
1321}
bb44e5d1 1322
917b627d 1323static struct task_struct *_pick_next_task_rt(struct rq *rq)
6f505b16
PZ
1324{
1325 struct sched_rt_entity *rt_se;
1326 struct task_struct *p;
1327 struct rt_rq *rt_rq;
bb44e5d1 1328
6f505b16
PZ
1329 rt_rq = &rq->rt;
1330
8e54a2c0 1331 if (!rt_rq->rt_nr_running)
6f505b16
PZ
1332 return NULL;
1333
23b0fdfc 1334 if (rt_rq_throttled(rt_rq))
6f505b16
PZ
1335 return NULL;
1336
1337 do {
1338 rt_se = pick_next_rt_entity(rq, rt_rq);
326587b8 1339 BUG_ON(!rt_se);
6f505b16
PZ
1340 rt_rq = group_rt_rq(rt_se);
1341 } while (rt_rq);
1342
1343 p = rt_task_of(rt_se);
305e6835 1344 p->se.exec_start = rq->clock_task;
917b627d
GH
1345
1346 return p;
1347}
1348
1349static struct task_struct *pick_next_task_rt(struct rq *rq)
1350{
1351 struct task_struct *p = _pick_next_task_rt(rq);
1352
1353 /* The running task is never eligible for pushing */
1354 if (p)
1355 dequeue_pushable_task(rq, p);
1356
bcf08df3 1357#ifdef CONFIG_SMP
3f029d3c
GH
1358 /*
1359 * We detect this state here so that we can avoid taking the RQ
1360 * lock again later if there is no need to push
1361 */
1362 rq->post_schedule = has_pushable_tasks(rq);
bcf08df3 1363#endif
3f029d3c 1364
6f505b16 1365 return p;
bb44e5d1
IM
1366}
1367
31ee529c 1368static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
bb44e5d1 1369{
f1e14ef6 1370 update_curr_rt(rq);
917b627d
GH
1371
1372 /*
1373 * The previous task needs to be made eligible for pushing
1374 * if it is still active
1375 */
fd2f4419 1376 if (on_rt_rq(&p->rt) && p->rt.nr_cpus_allowed > 1)
917b627d 1377 enqueue_pushable_task(rq, p);
bb44e5d1
IM
1378}
1379
681f3e68 1380#ifdef CONFIG_SMP
6f505b16 1381
e8fa1362
SR
1382/* Only try algorithms three times */
1383#define RT_MAX_TRIES 3
1384
f65eda4f
SR
1385static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1386{
1387 if (!task_running(rq, p) &&
fa17b507 1388 (cpu < 0 || cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) &&
6f505b16 1389 (p->rt.nr_cpus_allowed > 1))
f65eda4f
SR
1390 return 1;
1391 return 0;
1392}
1393
e8fa1362 1394/* Return the second highest RT task, NULL otherwise */
79064fbf 1395static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
e8fa1362 1396{
6f505b16
PZ
1397 struct task_struct *next = NULL;
1398 struct sched_rt_entity *rt_se;
1399 struct rt_prio_array *array;
1400 struct rt_rq *rt_rq;
e8fa1362
SR
1401 int idx;
1402
6f505b16
PZ
1403 for_each_leaf_rt_rq(rt_rq, rq) {
1404 array = &rt_rq->active;
1405 idx = sched_find_first_bit(array->bitmap);
49246274 1406next_idx:
6f505b16
PZ
1407 if (idx >= MAX_RT_PRIO)
1408 continue;
1409 if (next && next->prio < idx)
1410 continue;
1411 list_for_each_entry(rt_se, array->queue + idx, run_list) {
3d07467b
PZ
1412 struct task_struct *p;
1413
1414 if (!rt_entity_is_task(rt_se))
1415 continue;
1416
1417 p = rt_task_of(rt_se);
6f505b16
PZ
1418 if (pick_rt_task(rq, p, cpu)) {
1419 next = p;
1420 break;
1421 }
1422 }
1423 if (!next) {
1424 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
1425 goto next_idx;
1426 }
f65eda4f
SR
1427 }
1428
e8fa1362
SR
1429 return next;
1430}
1431
0e3900e6 1432static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
e8fa1362 1433
6e1254d2
GH
1434static int find_lowest_rq(struct task_struct *task)
1435{
1436 struct sched_domain *sd;
96f874e2 1437 struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask);
6e1254d2
GH
1438 int this_cpu = smp_processor_id();
1439 int cpu = task_cpu(task);
06f90dbd 1440
0da938c4
SR
1441 /* Make sure the mask is initialized first */
1442 if (unlikely(!lowest_mask))
1443 return -1;
1444
6e0534f2
GH
1445 if (task->rt.nr_cpus_allowed == 1)
1446 return -1; /* No other targets possible */
6e1254d2 1447
6e0534f2
GH
1448 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
1449 return -1; /* No targets found */
6e1254d2
GH
1450
1451 /*
1452 * At this point we have built a mask of cpus representing the
1453 * lowest priority tasks in the system. Now we want to elect
1454 * the best one based on our affinity and topology.
1455 *
1456 * We prioritize the last cpu that the task executed on since
1457 * it is most likely cache-hot in that location.
1458 */
96f874e2 1459 if (cpumask_test_cpu(cpu, lowest_mask))
6e1254d2
GH
1460 return cpu;
1461
1462 /*
1463 * Otherwise, we consult the sched_domains span maps to figure
1464 * out which cpu is logically closest to our hot cache data.
1465 */
e2c88063
RR
1466 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1467 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
6e1254d2 1468
cd4ae6ad 1469 rcu_read_lock();
e2c88063
RR
1470 for_each_domain(cpu, sd) {
1471 if (sd->flags & SD_WAKE_AFFINE) {
1472 int best_cpu;
6e1254d2 1473
e2c88063
RR
1474 /*
1475 * "this_cpu" is cheaper to preempt than a
1476 * remote processor.
1477 */
1478 if (this_cpu != -1 &&
cd4ae6ad
XF
1479 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1480 rcu_read_unlock();
e2c88063 1481 return this_cpu;
cd4ae6ad 1482 }
e2c88063
RR
1483
1484 best_cpu = cpumask_first_and(lowest_mask,
1485 sched_domain_span(sd));
cd4ae6ad
XF
1486 if (best_cpu < nr_cpu_ids) {
1487 rcu_read_unlock();
e2c88063 1488 return best_cpu;
cd4ae6ad 1489 }
6e1254d2
GH
1490 }
1491 }
cd4ae6ad 1492 rcu_read_unlock();
6e1254d2
GH
1493
1494 /*
1495 * And finally, if there were no matches within the domains
1496 * just give the caller *something* to work with from the compatible
1497 * locations.
1498 */
e2c88063
RR
1499 if (this_cpu != -1)
1500 return this_cpu;
1501
1502 cpu = cpumask_any(lowest_mask);
1503 if (cpu < nr_cpu_ids)
1504 return cpu;
1505 return -1;
07b4032c
GH
1506}
1507
1508/* Will lock the rq it finds */
4df64c0b 1509static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
07b4032c
GH
1510{
1511 struct rq *lowest_rq = NULL;
07b4032c 1512 int tries;
4df64c0b 1513 int cpu;
e8fa1362 1514
07b4032c
GH
1515 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
1516 cpu = find_lowest_rq(task);
1517
2de0b463 1518 if ((cpu == -1) || (cpu == rq->cpu))
e8fa1362
SR
1519 break;
1520
07b4032c
GH
1521 lowest_rq = cpu_rq(cpu);
1522
e8fa1362 1523 /* if the prio of this runqueue changed, try again */
07b4032c 1524 if (double_lock_balance(rq, lowest_rq)) {
e8fa1362
SR
1525 /*
1526 * We had to unlock the run queue. In
1527 * the mean time, task could have
1528 * migrated already or had its affinity changed.
1529 * Also make sure that it wasn't scheduled on its rq.
1530 */
07b4032c 1531 if (unlikely(task_rq(task) != rq ||
96f874e2 1532 !cpumask_test_cpu(lowest_rq->cpu,
fa17b507 1533 tsk_cpus_allowed(task)) ||
07b4032c 1534 task_running(rq, task) ||
fd2f4419 1535 !task->on_rq)) {
4df64c0b 1536
05fa785c 1537 raw_spin_unlock(&lowest_rq->lock);
e8fa1362
SR
1538 lowest_rq = NULL;
1539 break;
1540 }
1541 }
1542
1543 /* If this rq is still suitable use it. */
e864c499 1544 if (lowest_rq->rt.highest_prio.curr > task->prio)
e8fa1362
SR
1545 break;
1546
1547 /* try again */
1b12bbc7 1548 double_unlock_balance(rq, lowest_rq);
e8fa1362
SR
1549 lowest_rq = NULL;
1550 }
1551
1552 return lowest_rq;
1553}
1554
917b627d
GH
1555static struct task_struct *pick_next_pushable_task(struct rq *rq)
1556{
1557 struct task_struct *p;
1558
1559 if (!has_pushable_tasks(rq))
1560 return NULL;
1561
1562 p = plist_first_entry(&rq->rt.pushable_tasks,
1563 struct task_struct, pushable_tasks);
1564
1565 BUG_ON(rq->cpu != task_cpu(p));
1566 BUG_ON(task_current(rq, p));
1567 BUG_ON(p->rt.nr_cpus_allowed <= 1);
1568
fd2f4419 1569 BUG_ON(!p->on_rq);
917b627d
GH
1570 BUG_ON(!rt_task(p));
1571
1572 return p;
1573}
1574
e8fa1362
SR
1575/*
1576 * If the current CPU has more than one RT task, see if the non
1577 * running task can migrate over to a CPU that is running a task
1578 * of lesser priority.
1579 */
697f0a48 1580static int push_rt_task(struct rq *rq)
e8fa1362
SR
1581{
1582 struct task_struct *next_task;
1583 struct rq *lowest_rq;
311e800e 1584 int ret = 0;
e8fa1362 1585
a22d7fc1
GH
1586 if (!rq->rt.overloaded)
1587 return 0;
1588
917b627d 1589 next_task = pick_next_pushable_task(rq);
e8fa1362
SR
1590 if (!next_task)
1591 return 0;
1592
cb297a3e
CM
1593#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1594 if (unlikely(task_running(rq, next_task)))
1595 return 0;
1596#endif
1597
49246274 1598retry:
697f0a48 1599 if (unlikely(next_task == rq->curr)) {
f65eda4f 1600 WARN_ON(1);
e8fa1362 1601 return 0;
f65eda4f 1602 }
e8fa1362
SR
1603
1604 /*
1605 * It's possible that the next_task slipped in of
1606 * higher priority than current. If that's the case
1607 * just reschedule current.
1608 */
697f0a48
GH
1609 if (unlikely(next_task->prio < rq->curr->prio)) {
1610 resched_task(rq->curr);
e8fa1362
SR
1611 return 0;
1612 }
1613
697f0a48 1614 /* We might release rq lock */
e8fa1362
SR
1615 get_task_struct(next_task);
1616
1617 /* find_lock_lowest_rq locks the rq if found */
697f0a48 1618 lowest_rq = find_lock_lowest_rq(next_task, rq);
e8fa1362
SR
1619 if (!lowest_rq) {
1620 struct task_struct *task;
1621 /*
311e800e 1622 * find_lock_lowest_rq releases rq->lock
1563513d
GH
1623 * so it is possible that next_task has migrated.
1624 *
1625 * We need to make sure that the task is still on the same
1626 * run-queue and is also still the next task eligible for
1627 * pushing.
e8fa1362 1628 */
917b627d 1629 task = pick_next_pushable_task(rq);
1563513d
GH
1630 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1631 /*
311e800e
HD
1632 * The task hasn't migrated, and is still the next
1633 * eligible task, but we failed to find a run-queue
1634 * to push it to. Do not retry in this case, since
1635 * other cpus will pull from us when ready.
1563513d 1636 */
1563513d 1637 goto out;
e8fa1362 1638 }
917b627d 1639
1563513d
GH
1640 if (!task)
1641 /* No more tasks, just exit */
1642 goto out;
1643
917b627d 1644 /*
1563513d 1645 * Something has shifted, try again.
917b627d 1646 */
1563513d
GH
1647 put_task_struct(next_task);
1648 next_task = task;
1649 goto retry;
e8fa1362
SR
1650 }
1651
697f0a48 1652 deactivate_task(rq, next_task, 0);
e8fa1362
SR
1653 set_task_cpu(next_task, lowest_rq->cpu);
1654 activate_task(lowest_rq, next_task, 0);
311e800e 1655 ret = 1;
e8fa1362
SR
1656
1657 resched_task(lowest_rq->curr);
1658
1b12bbc7 1659 double_unlock_balance(rq, lowest_rq);
e8fa1362 1660
e8fa1362
SR
1661out:
1662 put_task_struct(next_task);
1663
311e800e 1664 return ret;
e8fa1362
SR
1665}
1666
e8fa1362
SR
1667static void push_rt_tasks(struct rq *rq)
1668{
1669 /* push_rt_task will return true if it moved an RT */
1670 while (push_rt_task(rq))
1671 ;
1672}
1673
f65eda4f
SR
1674static int pull_rt_task(struct rq *this_rq)
1675{
80bf3171 1676 int this_cpu = this_rq->cpu, ret = 0, cpu;
a8728944 1677 struct task_struct *p;
f65eda4f 1678 struct rq *src_rq;
f65eda4f 1679
637f5085 1680 if (likely(!rt_overloaded(this_rq)))
f65eda4f
SR
1681 return 0;
1682
c6c4927b 1683 for_each_cpu(cpu, this_rq->rd->rto_mask) {
f65eda4f
SR
1684 if (this_cpu == cpu)
1685 continue;
1686
1687 src_rq = cpu_rq(cpu);
74ab8e4f
GH
1688
1689 /*
1690 * Don't bother taking the src_rq->lock if the next highest
1691 * task is known to be lower-priority than our current task.
1692 * This may look racy, but if this value is about to go
1693 * logically higher, the src_rq will push this task away.
1694 * And if its going logically lower, we do not care
1695 */
1696 if (src_rq->rt.highest_prio.next >=
1697 this_rq->rt.highest_prio.curr)
1698 continue;
1699
f65eda4f
SR
1700 /*
1701 * We can potentially drop this_rq's lock in
1702 * double_lock_balance, and another CPU could
a8728944 1703 * alter this_rq
f65eda4f 1704 */
a8728944 1705 double_lock_balance(this_rq, src_rq);
f65eda4f
SR
1706
1707 /*
1708 * Are there still pullable RT tasks?
1709 */
614ee1f6
MG
1710 if (src_rq->rt.rt_nr_running <= 1)
1711 goto skip;
f65eda4f 1712
f65eda4f
SR
1713 p = pick_next_highest_task_rt(src_rq, this_cpu);
1714
1715 /*
1716 * Do we have an RT task that preempts
1717 * the to-be-scheduled task?
1718 */
a8728944 1719 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
f65eda4f 1720 WARN_ON(p == src_rq->curr);
fd2f4419 1721 WARN_ON(!p->on_rq);
f65eda4f
SR
1722
1723 /*
1724 * There's a chance that p is higher in priority
1725 * than what's currently running on its cpu.
1726 * This is just that p is wakeing up and hasn't
1727 * had a chance to schedule. We only pull
1728 * p if it is lower in priority than the
a8728944 1729 * current task on the run queue
f65eda4f 1730 */
a8728944 1731 if (p->prio < src_rq->curr->prio)
614ee1f6 1732 goto skip;
f65eda4f
SR
1733
1734 ret = 1;
1735
1736 deactivate_task(src_rq, p, 0);
1737 set_task_cpu(p, this_cpu);
1738 activate_task(this_rq, p, 0);
1739 /*
1740 * We continue with the search, just in
1741 * case there's an even higher prio task
25985edc 1742 * in another runqueue. (low likelihood
f65eda4f 1743 * but possible)
f65eda4f 1744 */
f65eda4f 1745 }
49246274 1746skip:
1b12bbc7 1747 double_unlock_balance(this_rq, src_rq);
f65eda4f
SR
1748 }
1749
1750 return ret;
1751}
1752
9a897c5a 1753static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
f65eda4f
SR
1754{
1755 /* Try to pull RT tasks here if we lower this rq's prio */
33c3d6c6 1756 if (rq->rt.highest_prio.curr > prev->prio)
f65eda4f
SR
1757 pull_rt_task(rq);
1758}
1759
9a897c5a 1760static void post_schedule_rt(struct rq *rq)
e8fa1362 1761{
967fc046 1762 push_rt_tasks(rq);
e8fa1362
SR
1763}
1764
8ae121ac
GH
1765/*
1766 * If we are not running and we are not going to reschedule soon, we should
1767 * try to push tasks away now
1768 */
efbbd05a 1769static void task_woken_rt(struct rq *rq, struct task_struct *p)
4642dafd 1770{
9a897c5a 1771 if (!task_running(rq, p) &&
8ae121ac 1772 !test_tsk_need_resched(rq->curr) &&
917b627d 1773 has_pushable_tasks(rq) &&
b3bc211c 1774 p->rt.nr_cpus_allowed > 1 &&
43fa5460 1775 rt_task(rq->curr) &&
b3bc211c 1776 (rq->curr->rt.nr_cpus_allowed < 2 ||
3be209a8 1777 rq->curr->prio <= p->prio))
4642dafd
SR
1778 push_rt_tasks(rq);
1779}
1780
cd8ba7cd 1781static void set_cpus_allowed_rt(struct task_struct *p,
96f874e2 1782 const struct cpumask *new_mask)
73fe6aae 1783{
96f874e2 1784 int weight = cpumask_weight(new_mask);
73fe6aae
GH
1785
1786 BUG_ON(!rt_task(p));
1787
1788 /*
1789 * Update the migration status of the RQ if we have an RT task
1790 * which is running AND changing its weight value.
1791 */
fd2f4419 1792 if (p->on_rq && (weight != p->rt.nr_cpus_allowed)) {
73fe6aae
GH
1793 struct rq *rq = task_rq(p);
1794
917b627d
GH
1795 if (!task_current(rq, p)) {
1796 /*
1797 * Make sure we dequeue this task from the pushable list
1798 * before going further. It will either remain off of
1799 * the list because we are no longer pushable, or it
1800 * will be requeued.
1801 */
1802 if (p->rt.nr_cpus_allowed > 1)
1803 dequeue_pushable_task(rq, p);
1804
1805 /*
1806 * Requeue if our weight is changing and still > 1
1807 */
1808 if (weight > 1)
1809 enqueue_pushable_task(rq, p);
1810
1811 }
1812
6f505b16 1813 if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
73fe6aae 1814 rq->rt.rt_nr_migratory++;
6f505b16 1815 } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
73fe6aae
GH
1816 BUG_ON(!rq->rt.rt_nr_migratory);
1817 rq->rt.rt_nr_migratory--;
1818 }
1819
398a153b 1820 update_rt_migration(&rq->rt);
73fe6aae 1821 }
73fe6aae 1822}
deeeccd4 1823
bdd7c81b 1824/* Assumes rq->lock is held */
1f11eb6a 1825static void rq_online_rt(struct rq *rq)
bdd7c81b
IM
1826{
1827 if (rq->rt.overloaded)
1828 rt_set_overload(rq);
6e0534f2 1829
7def2be1
PZ
1830 __enable_runtime(rq);
1831
e864c499 1832 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
bdd7c81b
IM
1833}
1834
1835/* Assumes rq->lock is held */
1f11eb6a 1836static void rq_offline_rt(struct rq *rq)
bdd7c81b
IM
1837{
1838 if (rq->rt.overloaded)
1839 rt_clear_overload(rq);
6e0534f2 1840
7def2be1
PZ
1841 __disable_runtime(rq);
1842
6e0534f2 1843 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
bdd7c81b 1844}
cb469845
SR
1845
1846/*
1847 * When switch from the rt queue, we bring ourselves to a position
1848 * that we might want to pull RT tasks from other runqueues.
1849 */
da7a735e 1850static void switched_from_rt(struct rq *rq, struct task_struct *p)
cb469845
SR
1851{
1852 /*
1853 * If there are other RT tasks then we will reschedule
1854 * and the scheduling of the other RT tasks will handle
1855 * the balancing. But if we are the last RT task
1856 * we may need to handle the pulling of RT tasks
1857 * now.
1858 */
fd2f4419 1859 if (p->on_rq && !rq->rt.rt_nr_running)
cb469845
SR
1860 pull_rt_task(rq);
1861}
3d8cbdf8 1862
029632fb 1863void init_sched_rt_class(void)
3d8cbdf8
RR
1864{
1865 unsigned int i;
1866
029632fb 1867 for_each_possible_cpu(i) {
eaa95840 1868 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
6ca09dfc 1869 GFP_KERNEL, cpu_to_node(i));
029632fb 1870 }
3d8cbdf8 1871}
cb469845
SR
1872#endif /* CONFIG_SMP */
1873
1874/*
1875 * When switching a task to RT, we may overload the runqueue
1876 * with RT tasks. In this case we try to push them off to
1877 * other runqueues.
1878 */
da7a735e 1879static void switched_to_rt(struct rq *rq, struct task_struct *p)
cb469845
SR
1880{
1881 int check_resched = 1;
1882
1883 /*
1884 * If we are already running, then there's nothing
1885 * that needs to be done. But if we are not running
1886 * we may need to preempt the current running task.
1887 * If that current running task is also an RT task
1888 * then see if we can move to another run queue.
1889 */
fd2f4419 1890 if (p->on_rq && rq->curr != p) {
cb469845
SR
1891#ifdef CONFIG_SMP
1892 if (rq->rt.overloaded && push_rt_task(rq) &&
1893 /* Don't resched if we changed runqueues */
1894 rq != task_rq(p))
1895 check_resched = 0;
1896#endif /* CONFIG_SMP */
1897 if (check_resched && p->prio < rq->curr->prio)
1898 resched_task(rq->curr);
1899 }
1900}
1901
1902/*
1903 * Priority of the task has changed. This may cause
1904 * us to initiate a push or pull.
1905 */
da7a735e
PZ
1906static void
1907prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
cb469845 1908{
fd2f4419 1909 if (!p->on_rq)
da7a735e
PZ
1910 return;
1911
1912 if (rq->curr == p) {
cb469845
SR
1913#ifdef CONFIG_SMP
1914 /*
1915 * If our priority decreases while running, we
1916 * may need to pull tasks to this runqueue.
1917 */
1918 if (oldprio < p->prio)
1919 pull_rt_task(rq);
1920 /*
1921 * If there's a higher priority task waiting to run
6fa46fa5
SR
1922 * then reschedule. Note, the above pull_rt_task
1923 * can release the rq lock and p could migrate.
1924 * Only reschedule if p is still on the same runqueue.
cb469845 1925 */
e864c499 1926 if (p->prio > rq->rt.highest_prio.curr && rq->curr == p)
cb469845
SR
1927 resched_task(p);
1928#else
1929 /* For UP simply resched on drop of prio */
1930 if (oldprio < p->prio)
1931 resched_task(p);
e8fa1362 1932#endif /* CONFIG_SMP */
cb469845
SR
1933 } else {
1934 /*
1935 * This task is not running, but if it is
1936 * greater than the current running task
1937 * then reschedule.
1938 */
1939 if (p->prio < rq->curr->prio)
1940 resched_task(rq->curr);
1941 }
1942}
1943
78f2c7db
PZ
1944static void watchdog(struct rq *rq, struct task_struct *p)
1945{
1946 unsigned long soft, hard;
1947
78d7d407
JS
1948 /* max may change after cur was read, this will be fixed next tick */
1949 soft = task_rlimit(p, RLIMIT_RTTIME);
1950 hard = task_rlimit_max(p, RLIMIT_RTTIME);
78f2c7db
PZ
1951
1952 if (soft != RLIM_INFINITY) {
1953 unsigned long next;
1954
1955 p->rt.timeout++;
1956 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
5a52dd50 1957 if (p->rt.timeout > next)
f06febc9 1958 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
78f2c7db
PZ
1959 }
1960}
bb44e5d1 1961
8f4d37ec 1962static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
bb44e5d1 1963{
67e2be02
PZ
1964 update_curr_rt(rq);
1965
78f2c7db
PZ
1966 watchdog(rq, p);
1967
bb44e5d1
IM
1968 /*
1969 * RR tasks need a special form of timeslice management.
1970 * FIFO tasks have no timeslices.
1971 */
1972 if (p->policy != SCHED_RR)
1973 return;
1974
fa717060 1975 if (--p->rt.time_slice)
bb44e5d1
IM
1976 return;
1977
de5bdff7 1978 p->rt.time_slice = RR_TIMESLICE;
bb44e5d1 1979
98fbc798
DA
1980 /*
1981 * Requeue to the end of queue if we are not the only element
1982 * on the queue:
1983 */
fa717060 1984 if (p->rt.run_list.prev != p->rt.run_list.next) {
7ebefa8c 1985 requeue_task_rt(rq, p, 0);
98fbc798
DA
1986 set_tsk_need_resched(p);
1987 }
bb44e5d1
IM
1988}
1989
83b699ed
SV
1990static void set_curr_task_rt(struct rq *rq)
1991{
1992 struct task_struct *p = rq->curr;
1993
305e6835 1994 p->se.exec_start = rq->clock_task;
917b627d
GH
1995
1996 /* The running task is never eligible for pushing */
1997 dequeue_pushable_task(rq, p);
83b699ed
SV
1998}
1999
6d686f45 2000static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
0d721cea
PW
2001{
2002 /*
2003 * Time slice is 0 for SCHED_FIFO tasks
2004 */
2005 if (task->policy == SCHED_RR)
de5bdff7 2006 return RR_TIMESLICE;
0d721cea
PW
2007 else
2008 return 0;
2009}
2010
029632fb 2011const struct sched_class rt_sched_class = {
5522d5d5 2012 .next = &fair_sched_class,
bb44e5d1
IM
2013 .enqueue_task = enqueue_task_rt,
2014 .dequeue_task = dequeue_task_rt,
2015 .yield_task = yield_task_rt,
2016
2017 .check_preempt_curr = check_preempt_curr_rt,
2018
2019 .pick_next_task = pick_next_task_rt,
2020 .put_prev_task = put_prev_task_rt,
2021
681f3e68 2022#ifdef CONFIG_SMP
4ce72a2c
LZ
2023 .select_task_rq = select_task_rq_rt,
2024
73fe6aae 2025 .set_cpus_allowed = set_cpus_allowed_rt,
1f11eb6a
GH
2026 .rq_online = rq_online_rt,
2027 .rq_offline = rq_offline_rt,
9a897c5a
SR
2028 .pre_schedule = pre_schedule_rt,
2029 .post_schedule = post_schedule_rt,
efbbd05a 2030 .task_woken = task_woken_rt,
cb469845 2031 .switched_from = switched_from_rt,
681f3e68 2032#endif
bb44e5d1 2033
83b699ed 2034 .set_curr_task = set_curr_task_rt,
bb44e5d1 2035 .task_tick = task_tick_rt,
cb469845 2036
0d721cea
PW
2037 .get_rr_interval = get_rr_interval_rt,
2038
cb469845
SR
2039 .prio_changed = prio_changed_rt,
2040 .switched_to = switched_to_rt,
bb44e5d1 2041};
ada18de2
PZ
2042
2043#ifdef CONFIG_SCHED_DEBUG
2044extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2045
029632fb 2046void print_rt_stats(struct seq_file *m, int cpu)
ada18de2 2047{
ec514c48 2048 rt_rq_iter_t iter;
ada18de2
PZ
2049 struct rt_rq *rt_rq;
2050
2051 rcu_read_lock();
ec514c48 2052 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
ada18de2
PZ
2053 print_rt_rq(m, cpu, rt_rq);
2054 rcu_read_unlock();
2055}
55e12e5e 2056#endif /* CONFIG_SCHED_DEBUG */