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