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