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