Merge branch 'sched' into sched-devel
[linux-2.6.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_SMP
7
8 static inline int rt_overloaded(struct rq *rq)
9 {
10         return atomic_read(&rq->rd->rto_count);
11 }
12
13 static inline void rt_set_overload(struct rq *rq)
14 {
15         if (!rq->online)
16                 return;
17
18         cpu_set(rq->cpu, rq->rd->rto_mask);
19         /*
20          * Make sure the mask is visible before we set
21          * the overload count. That is checked to determine
22          * if we should look at the mask. It would be a shame
23          * if we looked at the mask, but the mask was not
24          * updated yet.
25          */
26         wmb();
27         atomic_inc(&rq->rd->rto_count);
28 }
29
30 static inline void rt_clear_overload(struct rq *rq)
31 {
32         if (!rq->online)
33                 return;
34
35         /* the order here really doesn't matter */
36         atomic_dec(&rq->rd->rto_count);
37         cpu_clear(rq->cpu, rq->rd->rto_mask);
38 }
39
40 static void update_rt_migration(struct rq *rq)
41 {
42         if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
43                 if (!rq->rt.overloaded) {
44                         rt_set_overload(rq);
45                         rq->rt.overloaded = 1;
46                 }
47         } else if (rq->rt.overloaded) {
48                 rt_clear_overload(rq);
49                 rq->rt.overloaded = 0;
50         }
51 }
52 #endif /* CONFIG_SMP */
53
54 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
55 {
56         return container_of(rt_se, struct task_struct, rt);
57 }
58
59 static inline int on_rt_rq(struct sched_rt_entity *rt_se)
60 {
61         return !list_empty(&rt_se->run_list);
62 }
63
64 #ifdef CONFIG_RT_GROUP_SCHED
65
66 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
67 {
68         if (!rt_rq->tg)
69                 return RUNTIME_INF;
70
71         return rt_rq->rt_runtime;
72 }
73
74 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
75 {
76         return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
77 }
78
79 #define for_each_leaf_rt_rq(rt_rq, rq) \
80         list_for_each_entry(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
81
82 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
83 {
84         return rt_rq->rq;
85 }
86
87 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
88 {
89         return rt_se->rt_rq;
90 }
91
92 #define for_each_sched_rt_entity(rt_se) \
93         for (; rt_se; rt_se = rt_se->parent)
94
95 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
96 {
97         return rt_se->my_q;
98 }
99
100 static void enqueue_rt_entity(struct sched_rt_entity *rt_se);
101 static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
102
103 static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
104 {
105         struct sched_rt_entity *rt_se = rt_rq->rt_se;
106
107         if (rt_se && !on_rt_rq(rt_se) && rt_rq->rt_nr_running) {
108                 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
109
110                 enqueue_rt_entity(rt_se);
111                 if (rt_rq->highest_prio < curr->prio)
112                         resched_task(curr);
113         }
114 }
115
116 static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
117 {
118         struct sched_rt_entity *rt_se = rt_rq->rt_se;
119
120         if (rt_se && on_rt_rq(rt_se))
121                 dequeue_rt_entity(rt_se);
122 }
123
124 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
125 {
126         return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
127 }
128
129 static int rt_se_boosted(struct sched_rt_entity *rt_se)
130 {
131         struct rt_rq *rt_rq = group_rt_rq(rt_se);
132         struct task_struct *p;
133
134         if (rt_rq)
135                 return !!rt_rq->rt_nr_boosted;
136
137         p = rt_task_of(rt_se);
138         return p->prio != p->normal_prio;
139 }
140
141 #ifdef CONFIG_SMP
142 static inline cpumask_t sched_rt_period_mask(void)
143 {
144         return cpu_rq(smp_processor_id())->rd->span;
145 }
146 #else
147 static inline cpumask_t sched_rt_period_mask(void)
148 {
149         return cpu_online_map;
150 }
151 #endif
152
153 static inline
154 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
155 {
156         return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
157 }
158
159 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
160 {
161         return &rt_rq->tg->rt_bandwidth;
162 }
163
164 #else
165
166 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
167 {
168         return rt_rq->rt_runtime;
169 }
170
171 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
172 {
173         return ktime_to_ns(def_rt_bandwidth.rt_period);
174 }
175
176 #define for_each_leaf_rt_rq(rt_rq, rq) \
177         for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
178
179 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
180 {
181         return container_of(rt_rq, struct rq, rt);
182 }
183
184 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
185 {
186         struct task_struct *p = rt_task_of(rt_se);
187         struct rq *rq = task_rq(p);
188
189         return &rq->rt;
190 }
191
192 #define for_each_sched_rt_entity(rt_se) \
193         for (; rt_se; rt_se = NULL)
194
195 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
196 {
197         return NULL;
198 }
199
200 static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
201 {
202 }
203
204 static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
205 {
206 }
207
208 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
209 {
210         return rt_rq->rt_throttled;
211 }
212
213 static inline cpumask_t sched_rt_period_mask(void)
214 {
215         return cpu_online_map;
216 }
217
218 static inline
219 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
220 {
221         return &cpu_rq(cpu)->rt;
222 }
223
224 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
225 {
226         return &def_rt_bandwidth;
227 }
228
229 #endif
230
231 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
232 {
233         int i, idle = 1;
234         cpumask_t span;
235
236         if (rt_b->rt_runtime == RUNTIME_INF)
237                 return 1;
238
239         span = sched_rt_period_mask();
240         for_each_cpu_mask(i, span) {
241                 int enqueue = 0;
242                 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
243                 struct rq *rq = rq_of_rt_rq(rt_rq);
244
245                 spin_lock(&rq->lock);
246                 if (rt_rq->rt_time) {
247                         u64 runtime;
248
249                         spin_lock(&rt_rq->rt_runtime_lock);
250                         runtime = rt_rq->rt_runtime;
251                         rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
252                         if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
253                                 rt_rq->rt_throttled = 0;
254                                 enqueue = 1;
255                         }
256                         if (rt_rq->rt_time || rt_rq->rt_nr_running)
257                                 idle = 0;
258                         spin_unlock(&rt_rq->rt_runtime_lock);
259                 }
260
261                 if (enqueue)
262                         sched_rt_rq_enqueue(rt_rq);
263                 spin_unlock(&rq->lock);
264         }
265
266         return idle;
267 }
268
269 #ifdef CONFIG_SMP
270 static int balance_runtime(struct rt_rq *rt_rq)
271 {
272         struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
273         struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
274         int i, weight, more = 0;
275         u64 rt_period;
276
277         weight = cpus_weight(rd->span);
278
279         spin_lock(&rt_b->rt_runtime_lock);
280         rt_period = ktime_to_ns(rt_b->rt_period);
281         for_each_cpu_mask(i, rd->span) {
282                 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
283                 s64 diff;
284
285                 if (iter == rt_rq)
286                         continue;
287
288                 spin_lock(&iter->rt_runtime_lock);
289                 if (iter->rt_runtime == RUNTIME_INF)
290                         goto next;
291
292                 diff = iter->rt_runtime - iter->rt_time;
293                 if (diff > 0) {
294                         do_div(diff, weight);
295                         if (rt_rq->rt_runtime + diff > rt_period)
296                                 diff = rt_period - rt_rq->rt_runtime;
297                         iter->rt_runtime -= diff;
298                         rt_rq->rt_runtime += diff;
299                         more = 1;
300                         if (rt_rq->rt_runtime == rt_period) {
301                                 spin_unlock(&iter->rt_runtime_lock);
302                                 break;
303                         }
304                 }
305 next:
306                 spin_unlock(&iter->rt_runtime_lock);
307         }
308         spin_unlock(&rt_b->rt_runtime_lock);
309
310         return more;
311 }
312
313 static void __disable_runtime(struct rq *rq)
314 {
315         struct root_domain *rd = rq->rd;
316         struct rt_rq *rt_rq;
317
318         if (unlikely(!scheduler_running))
319                 return;
320
321         for_each_leaf_rt_rq(rt_rq, rq) {
322                 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
323                 s64 want;
324                 int i;
325
326                 spin_lock(&rt_b->rt_runtime_lock);
327                 spin_lock(&rt_rq->rt_runtime_lock);
328                 if (rt_rq->rt_runtime == RUNTIME_INF ||
329                                 rt_rq->rt_runtime == rt_b->rt_runtime)
330                         goto balanced;
331                 spin_unlock(&rt_rq->rt_runtime_lock);
332
333                 want = rt_b->rt_runtime - rt_rq->rt_runtime;
334
335                 for_each_cpu_mask(i, rd->span) {
336                         struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
337                         s64 diff;
338
339                         if (iter == rt_rq)
340                                 continue;
341
342                         spin_lock(&iter->rt_runtime_lock);
343                         if (want > 0) {
344                                 diff = min_t(s64, iter->rt_runtime, want);
345                                 iter->rt_runtime -= diff;
346                                 want -= diff;
347                         } else {
348                                 iter->rt_runtime -= want;
349                                 want -= want;
350                         }
351                         spin_unlock(&iter->rt_runtime_lock);
352
353                         if (!want)
354                                 break;
355                 }
356
357                 spin_lock(&rt_rq->rt_runtime_lock);
358                 BUG_ON(want);
359 balanced:
360                 rt_rq->rt_runtime = RUNTIME_INF;
361                 spin_unlock(&rt_rq->rt_runtime_lock);
362                 spin_unlock(&rt_b->rt_runtime_lock);
363         }
364 }
365
366 static void disable_runtime(struct rq *rq)
367 {
368         unsigned long flags;
369
370         spin_lock_irqsave(&rq->lock, flags);
371         __disable_runtime(rq);
372         spin_unlock_irqrestore(&rq->lock, flags);
373 }
374
375 static void __enable_runtime(struct rq *rq)
376 {
377         struct root_domain *rd = rq->rd;
378         struct rt_rq *rt_rq;
379
380         if (unlikely(!scheduler_running))
381                 return;
382
383         for_each_leaf_rt_rq(rt_rq, rq) {
384                 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
385
386                 spin_lock(&rt_b->rt_runtime_lock);
387                 spin_lock(&rt_rq->rt_runtime_lock);
388                 rt_rq->rt_runtime = rt_b->rt_runtime;
389                 rt_rq->rt_time = 0;
390                 spin_unlock(&rt_rq->rt_runtime_lock);
391                 spin_unlock(&rt_b->rt_runtime_lock);
392         }
393 }
394
395 static void enable_runtime(struct rq *rq)
396 {
397         unsigned long flags;
398
399         spin_lock_irqsave(&rq->lock, flags);
400         __enable_runtime(rq);
401         spin_unlock_irqrestore(&rq->lock, flags);
402 }
403
404 #endif
405
406 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
407 {
408 #ifdef CONFIG_RT_GROUP_SCHED
409         struct rt_rq *rt_rq = group_rt_rq(rt_se);
410
411         if (rt_rq)
412                 return rt_rq->highest_prio;
413 #endif
414
415         return rt_task_of(rt_se)->prio;
416 }
417
418 static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
419 {
420         u64 runtime = sched_rt_runtime(rt_rq);
421
422         if (runtime == RUNTIME_INF)
423                 return 0;
424
425         if (rt_rq->rt_throttled)
426                 return rt_rq_throttled(rt_rq);
427
428         if (sched_rt_runtime(rt_rq) >= sched_rt_period(rt_rq))
429                 return 0;
430
431 #ifdef CONFIG_SMP
432         if (rt_rq->rt_time > runtime) {
433                 spin_unlock(&rt_rq->rt_runtime_lock);
434                 balance_runtime(rt_rq);
435                 spin_lock(&rt_rq->rt_runtime_lock);
436
437                 runtime = sched_rt_runtime(rt_rq);
438                 if (runtime == RUNTIME_INF)
439                         return 0;
440         }
441 #endif
442
443         if (rt_rq->rt_time > runtime) {
444                 rt_rq->rt_throttled = 1;
445                 if (rt_rq_throttled(rt_rq)) {
446                         sched_rt_rq_dequeue(rt_rq);
447                         return 1;
448                 }
449         }
450
451         return 0;
452 }
453
454 /*
455  * Update the current task's runtime statistics. Skip current tasks that
456  * are not in our scheduling class.
457  */
458 static void update_curr_rt(struct rq *rq)
459 {
460         struct task_struct *curr = rq->curr;
461         struct sched_rt_entity *rt_se = &curr->rt;
462         struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
463         u64 delta_exec;
464
465         if (!task_has_rt_policy(curr))
466                 return;
467
468         delta_exec = rq->clock - curr->se.exec_start;
469         if (unlikely((s64)delta_exec < 0))
470                 delta_exec = 0;
471
472         schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
473
474         curr->se.sum_exec_runtime += delta_exec;
475         curr->se.exec_start = rq->clock;
476         cpuacct_charge(curr, delta_exec);
477
478         for_each_sched_rt_entity(rt_se) {
479                 rt_rq = rt_rq_of_se(rt_se);
480
481                 spin_lock(&rt_rq->rt_runtime_lock);
482                 rt_rq->rt_time += delta_exec;
483                 if (sched_rt_runtime_exceeded(rt_rq))
484                         resched_task(curr);
485                 spin_unlock(&rt_rq->rt_runtime_lock);
486         }
487 }
488
489 static inline
490 void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
491 {
492         WARN_ON(!rt_prio(rt_se_prio(rt_se)));
493         rt_rq->rt_nr_running++;
494 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
495         if (rt_se_prio(rt_se) < rt_rq->highest_prio) {
496                 struct rq *rq = rq_of_rt_rq(rt_rq);
497
498                 rt_rq->highest_prio = rt_se_prio(rt_se);
499 #ifdef CONFIG_SMP
500                 if (rq->online)
501                         cpupri_set(&rq->rd->cpupri, rq->cpu,
502                                    rt_se_prio(rt_se));
503 #endif
504         }
505 #endif
506 #ifdef CONFIG_SMP
507         if (rt_se->nr_cpus_allowed > 1) {
508                 struct rq *rq = rq_of_rt_rq(rt_rq);
509
510                 rq->rt.rt_nr_migratory++;
511         }
512
513         update_rt_migration(rq_of_rt_rq(rt_rq));
514 #endif
515 #ifdef CONFIG_RT_GROUP_SCHED
516         if (rt_se_boosted(rt_se))
517                 rt_rq->rt_nr_boosted++;
518
519         if (rt_rq->tg)
520                 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
521 #else
522         start_rt_bandwidth(&def_rt_bandwidth);
523 #endif
524 }
525
526 static inline
527 void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
528 {
529 #ifdef CONFIG_SMP
530         int highest_prio = rt_rq->highest_prio;
531 #endif
532
533         WARN_ON(!rt_prio(rt_se_prio(rt_se)));
534         WARN_ON(!rt_rq->rt_nr_running);
535         rt_rq->rt_nr_running--;
536 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
537         if (rt_rq->rt_nr_running) {
538                 struct rt_prio_array *array;
539
540                 WARN_ON(rt_se_prio(rt_se) < rt_rq->highest_prio);
541                 if (rt_se_prio(rt_se) == rt_rq->highest_prio) {
542                         /* recalculate */
543                         array = &rt_rq->active;
544                         rt_rq->highest_prio =
545                                 sched_find_first_bit(array->bitmap);
546                 } /* otherwise leave rq->highest prio alone */
547         } else
548                 rt_rq->highest_prio = MAX_RT_PRIO;
549 #endif
550 #ifdef CONFIG_SMP
551         if (rt_se->nr_cpus_allowed > 1) {
552                 struct rq *rq = rq_of_rt_rq(rt_rq);
553                 rq->rt.rt_nr_migratory--;
554         }
555
556         if (rt_rq->highest_prio != highest_prio) {
557                 struct rq *rq = rq_of_rt_rq(rt_rq);
558
559                 if (rq->online)
560                         cpupri_set(&rq->rd->cpupri, rq->cpu,
561                                    rt_rq->highest_prio);
562         }
563
564         update_rt_migration(rq_of_rt_rq(rt_rq));
565 #endif /* CONFIG_SMP */
566 #ifdef CONFIG_RT_GROUP_SCHED
567         if (rt_se_boosted(rt_se))
568                 rt_rq->rt_nr_boosted--;
569
570         WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
571 #endif
572 }
573
574 static void __enqueue_rt_entity(struct sched_rt_entity *rt_se)
575 {
576         struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
577         struct rt_prio_array *array = &rt_rq->active;
578         struct rt_rq *group_rq = group_rt_rq(rt_se);
579         struct list_head *queue = array->queue + rt_se_prio(rt_se);
580
581         /*
582          * Don't enqueue the group if its throttled, or when empty.
583          * The latter is a consequence of the former when a child group
584          * get throttled and the current group doesn't have any other
585          * active members.
586          */
587         if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
588                 return;
589
590         if (rt_se->nr_cpus_allowed == 1)
591                 list_add(&rt_se->run_list, queue);
592         else
593                 list_add_tail(&rt_se->run_list, queue);
594
595         __set_bit(rt_se_prio(rt_se), array->bitmap);
596
597         inc_rt_tasks(rt_se, rt_rq);
598 }
599
600 static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
601 {
602         struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
603         struct rt_prio_array *array = &rt_rq->active;
604
605         list_del_init(&rt_se->run_list);
606         if (list_empty(array->queue + rt_se_prio(rt_se)))
607                 __clear_bit(rt_se_prio(rt_se), array->bitmap);
608
609         dec_rt_tasks(rt_se, rt_rq);
610 }
611
612 /*
613  * Because the prio of an upper entry depends on the lower
614  * entries, we must remove entries top - down.
615  */
616 static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
617 {
618         struct sched_rt_entity *back = NULL;
619
620         for_each_sched_rt_entity(rt_se) {
621                 rt_se->back = back;
622                 back = rt_se;
623         }
624
625         for (rt_se = back; rt_se; rt_se = rt_se->back) {
626                 if (on_rt_rq(rt_se))
627                         __dequeue_rt_entity(rt_se);
628         }
629 }
630
631 static void enqueue_rt_entity(struct sched_rt_entity *rt_se)
632 {
633         dequeue_rt_stack(rt_se);
634         for_each_sched_rt_entity(rt_se)
635                 __enqueue_rt_entity(rt_se);
636 }
637
638 static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
639 {
640         dequeue_rt_stack(rt_se);
641
642         for_each_sched_rt_entity(rt_se) {
643                 struct rt_rq *rt_rq = group_rt_rq(rt_se);
644
645                 if (rt_rq && rt_rq->rt_nr_running)
646                         __enqueue_rt_entity(rt_se);
647         }
648 }
649
650 /*
651  * Adding/removing a task to/from a priority array:
652  */
653 static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
654 {
655         struct sched_rt_entity *rt_se = &p->rt;
656
657         if (wakeup)
658                 rt_se->timeout = 0;
659
660         enqueue_rt_entity(rt_se);
661 }
662
663 static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
664 {
665         struct sched_rt_entity *rt_se = &p->rt;
666
667         update_curr_rt(rq);
668         dequeue_rt_entity(rt_se);
669 }
670
671 /*
672  * Put task to the end of the run list without the overhead of dequeue
673  * followed by enqueue.
674  */
675 static
676 void requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
677 {
678         struct rt_prio_array *array = &rt_rq->active;
679         struct list_head *queue = array->queue + rt_se_prio(rt_se);
680
681         if (on_rt_rq(rt_se)) {
682                 list_del_init(&rt_se->run_list);
683                 list_add_tail(&rt_se->run_list,
684                               array->queue + rt_se_prio(rt_se));
685         }
686 }
687
688 static void requeue_task_rt(struct rq *rq, struct task_struct *p)
689 {
690         struct sched_rt_entity *rt_se = &p->rt;
691         struct rt_rq *rt_rq;
692
693         for_each_sched_rt_entity(rt_se) {
694                 rt_rq = rt_rq_of_se(rt_se);
695                 requeue_rt_entity(rt_rq, rt_se);
696         }
697 }
698
699 static void yield_task_rt(struct rq *rq)
700 {
701         requeue_task_rt(rq, rq->curr);
702 }
703
704 #ifdef CONFIG_SMP
705 static int find_lowest_rq(struct task_struct *task);
706
707 static int select_task_rq_rt(struct task_struct *p, int sync)
708 {
709         struct rq *rq = task_rq(p);
710
711         /*
712          * If the current task is an RT task, then
713          * try to see if we can wake this RT task up on another
714          * runqueue. Otherwise simply start this RT task
715          * on its current runqueue.
716          *
717          * We want to avoid overloading runqueues. Even if
718          * the RT task is of higher priority than the current RT task.
719          * RT tasks behave differently than other tasks. If
720          * one gets preempted, we try to push it off to another queue.
721          * So trying to keep a preempting RT task on the same
722          * cache hot CPU will force the running RT task to
723          * a cold CPU. So we waste all the cache for the lower
724          * RT task in hopes of saving some of a RT task
725          * that is just being woken and probably will have
726          * cold cache anyway.
727          */
728         if (unlikely(rt_task(rq->curr)) &&
729             (p->rt.nr_cpus_allowed > 1)) {
730                 int cpu = find_lowest_rq(p);
731
732                 return (cpu == -1) ? task_cpu(p) : cpu;
733         }
734
735         /*
736          * Otherwise, just let it ride on the affined RQ and the
737          * post-schedule router will push the preempted task away
738          */
739         return task_cpu(p);
740 }
741 #endif /* CONFIG_SMP */
742
743 /*
744  * Preempt the current task with a newly woken task if needed:
745  */
746 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
747 {
748         if (p->prio < rq->curr->prio) {
749                 resched_task(rq->curr);
750                 return;
751         }
752
753 #ifdef CONFIG_SMP
754         /*
755          * If:
756          *
757          * - the newly woken task is of equal priority to the current task
758          * - the newly woken task is non-migratable while current is migratable
759          * - current will be preempted on the next reschedule
760          *
761          * we should check to see if current can readily move to a different
762          * cpu.  If so, we will reschedule to allow the push logic to try
763          * to move current somewhere else, making room for our non-migratable
764          * task.
765          */
766         if((p->prio == rq->curr->prio)
767            && p->rt.nr_cpus_allowed == 1
768            && rq->curr->rt.nr_cpus_allowed != 1) {
769                 cpumask_t mask;
770
771                 if (cpupri_find(&rq->rd->cpupri, rq->curr, &mask))
772                         /*
773                          * There appears to be other cpus that can accept
774                          * current, so lets reschedule to try and push it away
775                          */
776                         resched_task(rq->curr);
777         }
778 #endif
779 }
780
781 static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
782                                                    struct rt_rq *rt_rq)
783 {
784         struct rt_prio_array *array = &rt_rq->active;
785         struct sched_rt_entity *next = NULL;
786         struct list_head *queue;
787         int idx;
788
789         idx = sched_find_first_bit(array->bitmap);
790         BUG_ON(idx >= MAX_RT_PRIO);
791
792         queue = array->queue + idx;
793         next = list_entry(queue->next, struct sched_rt_entity, run_list);
794
795         return next;
796 }
797
798 static struct task_struct *pick_next_task_rt(struct rq *rq)
799 {
800         struct sched_rt_entity *rt_se;
801         struct task_struct *p;
802         struct rt_rq *rt_rq;
803
804         rt_rq = &rq->rt;
805
806         if (unlikely(!rt_rq->rt_nr_running))
807                 return NULL;
808
809         if (rt_rq_throttled(rt_rq))
810                 return NULL;
811
812         do {
813                 rt_se = pick_next_rt_entity(rq, rt_rq);
814                 BUG_ON(!rt_se);
815                 rt_rq = group_rt_rq(rt_se);
816         } while (rt_rq);
817
818         p = rt_task_of(rt_se);
819         p->se.exec_start = rq->clock;
820         return p;
821 }
822
823 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
824 {
825         update_curr_rt(rq);
826         p->se.exec_start = 0;
827 }
828
829 #ifdef CONFIG_SMP
830
831 /* Only try algorithms three times */
832 #define RT_MAX_TRIES 3
833
834 static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
835 static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
836
837 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
838 {
839         if (!task_running(rq, p) &&
840             (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
841             (p->rt.nr_cpus_allowed > 1))
842                 return 1;
843         return 0;
844 }
845
846 /* Return the second highest RT task, NULL otherwise */
847 static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
848 {
849         struct task_struct *next = NULL;
850         struct sched_rt_entity *rt_se;
851         struct rt_prio_array *array;
852         struct rt_rq *rt_rq;
853         int idx;
854
855         for_each_leaf_rt_rq(rt_rq, rq) {
856                 array = &rt_rq->active;
857                 idx = sched_find_first_bit(array->bitmap);
858  next_idx:
859                 if (idx >= MAX_RT_PRIO)
860                         continue;
861                 if (next && next->prio < idx)
862                         continue;
863                 list_for_each_entry(rt_se, array->queue + idx, run_list) {
864                         struct task_struct *p = rt_task_of(rt_se);
865                         if (pick_rt_task(rq, p, cpu)) {
866                                 next = p;
867                                 break;
868                         }
869                 }
870                 if (!next) {
871                         idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
872                         goto next_idx;
873                 }
874         }
875
876         return next;
877 }
878
879 static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
880
881 static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
882 {
883         int first;
884
885         /* "this_cpu" is cheaper to preempt than a remote processor */
886         if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
887                 return this_cpu;
888
889         first = first_cpu(*mask);
890         if (first != NR_CPUS)
891                 return first;
892
893         return -1;
894 }
895
896 static int find_lowest_rq(struct task_struct *task)
897 {
898         struct sched_domain *sd;
899         cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
900         int this_cpu = smp_processor_id();
901         int cpu      = task_cpu(task);
902
903         if (task->rt.nr_cpus_allowed == 1)
904                 return -1; /* No other targets possible */
905
906         if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
907                 return -1; /* No targets found */
908
909         /*
910          * At this point we have built a mask of cpus representing the
911          * lowest priority tasks in the system.  Now we want to elect
912          * the best one based on our affinity and topology.
913          *
914          * We prioritize the last cpu that the task executed on since
915          * it is most likely cache-hot in that location.
916          */
917         if (cpu_isset(cpu, *lowest_mask))
918                 return cpu;
919
920         /*
921          * Otherwise, we consult the sched_domains span maps to figure
922          * out which cpu is logically closest to our hot cache data.
923          */
924         if (this_cpu == cpu)
925                 this_cpu = -1; /* Skip this_cpu opt if the same */
926
927         for_each_domain(cpu, sd) {
928                 if (sd->flags & SD_WAKE_AFFINE) {
929                         cpumask_t domain_mask;
930                         int       best_cpu;
931
932                         cpus_and(domain_mask, sd->span, *lowest_mask);
933
934                         best_cpu = pick_optimal_cpu(this_cpu,
935                                                     &domain_mask);
936                         if (best_cpu != -1)
937                                 return best_cpu;
938                 }
939         }
940
941         /*
942          * And finally, if there were no matches within the domains
943          * just give the caller *something* to work with from the compatible
944          * locations.
945          */
946         return pick_optimal_cpu(this_cpu, lowest_mask);
947 }
948
949 /* Will lock the rq it finds */
950 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
951 {
952         struct rq *lowest_rq = NULL;
953         int tries;
954         int cpu;
955
956         for (tries = 0; tries < RT_MAX_TRIES; tries++) {
957                 cpu = find_lowest_rq(task);
958
959                 if ((cpu == -1) || (cpu == rq->cpu))
960                         break;
961
962                 lowest_rq = cpu_rq(cpu);
963
964                 /* if the prio of this runqueue changed, try again */
965                 if (double_lock_balance(rq, lowest_rq)) {
966                         /*
967                          * We had to unlock the run queue. In
968                          * the mean time, task could have
969                          * migrated already or had its affinity changed.
970                          * Also make sure that it wasn't scheduled on its rq.
971                          */
972                         if (unlikely(task_rq(task) != rq ||
973                                      !cpu_isset(lowest_rq->cpu,
974                                                 task->cpus_allowed) ||
975                                      task_running(rq, task) ||
976                                      !task->se.on_rq)) {
977
978                                 spin_unlock(&lowest_rq->lock);
979                                 lowest_rq = NULL;
980                                 break;
981                         }
982                 }
983
984                 /* If this rq is still suitable use it. */
985                 if (lowest_rq->rt.highest_prio > task->prio)
986                         break;
987
988                 /* try again */
989                 spin_unlock(&lowest_rq->lock);
990                 lowest_rq = NULL;
991         }
992
993         return lowest_rq;
994 }
995
996 /*
997  * If the current CPU has more than one RT task, see if the non
998  * running task can migrate over to a CPU that is running a task
999  * of lesser priority.
1000  */
1001 static int push_rt_task(struct rq *rq)
1002 {
1003         struct task_struct *next_task;
1004         struct rq *lowest_rq;
1005         int ret = 0;
1006         int paranoid = RT_MAX_TRIES;
1007
1008         if (!rq->rt.overloaded)
1009                 return 0;
1010
1011         next_task = pick_next_highest_task_rt(rq, -1);
1012         if (!next_task)
1013                 return 0;
1014
1015  retry:
1016         if (unlikely(next_task == rq->curr)) {
1017                 WARN_ON(1);
1018                 return 0;
1019         }
1020
1021         /*
1022          * It's possible that the next_task slipped in of
1023          * higher priority than current. If that's the case
1024          * just reschedule current.
1025          */
1026         if (unlikely(next_task->prio < rq->curr->prio)) {
1027                 resched_task(rq->curr);
1028                 return 0;
1029         }
1030
1031         /* We might release rq lock */
1032         get_task_struct(next_task);
1033
1034         /* find_lock_lowest_rq locks the rq if found */
1035         lowest_rq = find_lock_lowest_rq(next_task, rq);
1036         if (!lowest_rq) {
1037                 struct task_struct *task;
1038                 /*
1039                  * find lock_lowest_rq releases rq->lock
1040                  * so it is possible that next_task has changed.
1041                  * If it has, then try again.
1042                  */
1043                 task = pick_next_highest_task_rt(rq, -1);
1044                 if (unlikely(task != next_task) && task && paranoid--) {
1045                         put_task_struct(next_task);
1046                         next_task = task;
1047                         goto retry;
1048                 }
1049                 goto out;
1050         }
1051
1052         deactivate_task(rq, next_task, 0);
1053         set_task_cpu(next_task, lowest_rq->cpu);
1054         activate_task(lowest_rq, next_task, 0);
1055
1056         resched_task(lowest_rq->curr);
1057
1058         spin_unlock(&lowest_rq->lock);
1059
1060         ret = 1;
1061 out:
1062         put_task_struct(next_task);
1063
1064         return ret;
1065 }
1066
1067 /*
1068  * TODO: Currently we just use the second highest prio task on
1069  *       the queue, and stop when it can't migrate (or there's
1070  *       no more RT tasks).  There may be a case where a lower
1071  *       priority RT task has a different affinity than the
1072  *       higher RT task. In this case the lower RT task could
1073  *       possibly be able to migrate where as the higher priority
1074  *       RT task could not.  We currently ignore this issue.
1075  *       Enhancements are welcome!
1076  */
1077 static void push_rt_tasks(struct rq *rq)
1078 {
1079         /* push_rt_task will return true if it moved an RT */
1080         while (push_rt_task(rq))
1081                 ;
1082 }
1083
1084 static int pull_rt_task(struct rq *this_rq)
1085 {
1086         int this_cpu = this_rq->cpu, ret = 0, cpu;
1087         struct task_struct *p, *next;
1088         struct rq *src_rq;
1089
1090         if (likely(!rt_overloaded(this_rq)))
1091                 return 0;
1092
1093         next = pick_next_task_rt(this_rq);
1094
1095         for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
1096                 if (this_cpu == cpu)
1097                         continue;
1098
1099                 src_rq = cpu_rq(cpu);
1100                 /*
1101                  * We can potentially drop this_rq's lock in
1102                  * double_lock_balance, and another CPU could
1103                  * steal our next task - hence we must cause
1104                  * the caller to recalculate the next task
1105                  * in that case:
1106                  */
1107                 if (double_lock_balance(this_rq, src_rq)) {
1108                         struct task_struct *old_next = next;
1109
1110                         next = pick_next_task_rt(this_rq);
1111                         if (next != old_next)
1112                                 ret = 1;
1113                 }
1114
1115                 /*
1116                  * Are there still pullable RT tasks?
1117                  */
1118                 if (src_rq->rt.rt_nr_running <= 1)
1119                         goto skip;
1120
1121                 p = pick_next_highest_task_rt(src_rq, this_cpu);
1122
1123                 /*
1124                  * Do we have an RT task that preempts
1125                  * the to-be-scheduled task?
1126                  */
1127                 if (p && (!next || (p->prio < next->prio))) {
1128                         WARN_ON(p == src_rq->curr);
1129                         WARN_ON(!p->se.on_rq);
1130
1131                         /*
1132                          * There's a chance that p is higher in priority
1133                          * than what's currently running on its cpu.
1134                          * This is just that p is wakeing up and hasn't
1135                          * had a chance to schedule. We only pull
1136                          * p if it is lower in priority than the
1137                          * current task on the run queue or
1138                          * this_rq next task is lower in prio than
1139                          * the current task on that rq.
1140                          */
1141                         if (p->prio < src_rq->curr->prio ||
1142                             (next && next->prio < src_rq->curr->prio))
1143                                 goto skip;
1144
1145                         ret = 1;
1146
1147                         deactivate_task(src_rq, p, 0);
1148                         set_task_cpu(p, this_cpu);
1149                         activate_task(this_rq, p, 0);
1150                         /*
1151                          * We continue with the search, just in
1152                          * case there's an even higher prio task
1153                          * in another runqueue. (low likelyhood
1154                          * but possible)
1155                          *
1156                          * Update next so that we won't pick a task
1157                          * on another cpu with a priority lower (or equal)
1158                          * than the one we just picked.
1159                          */
1160                         next = p;
1161
1162                 }
1163  skip:
1164                 spin_unlock(&src_rq->lock);
1165         }
1166
1167         return ret;
1168 }
1169
1170 static void pre_schedule_rt(struct rq *rq, struct task_struct *prev)
1171 {
1172         /* Try to pull RT tasks here if we lower this rq's prio */
1173         if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
1174                 pull_rt_task(rq);
1175 }
1176
1177 static void post_schedule_rt(struct rq *rq)
1178 {
1179         /*
1180          * If we have more than one rt_task queued, then
1181          * see if we can push the other rt_tasks off to other CPUS.
1182          * Note we may release the rq lock, and since
1183          * the lock was owned by prev, we need to release it
1184          * first via finish_lock_switch and then reaquire it here.
1185          */
1186         if (unlikely(rq->rt.overloaded)) {
1187                 spin_lock_irq(&rq->lock);
1188                 push_rt_tasks(rq);
1189                 spin_unlock_irq(&rq->lock);
1190         }
1191 }
1192
1193 /*
1194  * If we are not running and we are not going to reschedule soon, we should
1195  * try to push tasks away now
1196  */
1197 static void task_wake_up_rt(struct rq *rq, struct task_struct *p)
1198 {
1199         if (!task_running(rq, p) &&
1200             !test_tsk_need_resched(rq->curr) &&
1201             rq->rt.overloaded)
1202                 push_rt_tasks(rq);
1203 }
1204
1205 static unsigned long
1206 load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
1207                 unsigned long max_load_move,
1208                 struct sched_domain *sd, enum cpu_idle_type idle,
1209                 int *all_pinned, int *this_best_prio)
1210 {
1211         /* don't touch RT tasks */
1212         return 0;
1213 }
1214
1215 static int
1216 move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
1217                  struct sched_domain *sd, enum cpu_idle_type idle)
1218 {
1219         /* don't touch RT tasks */
1220         return 0;
1221 }
1222
1223 static void set_cpus_allowed_rt(struct task_struct *p,
1224                                 const cpumask_t *new_mask)
1225 {
1226         int weight = cpus_weight(*new_mask);
1227
1228         BUG_ON(!rt_task(p));
1229
1230         /*
1231          * Update the migration status of the RQ if we have an RT task
1232          * which is running AND changing its weight value.
1233          */
1234         if (p->se.on_rq && (weight != p->rt.nr_cpus_allowed)) {
1235                 struct rq *rq = task_rq(p);
1236
1237                 if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) {
1238                         rq->rt.rt_nr_migratory++;
1239                 } else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) {
1240                         BUG_ON(!rq->rt.rt_nr_migratory);
1241                         rq->rt.rt_nr_migratory--;
1242                 }
1243
1244                 update_rt_migration(rq);
1245         }
1246
1247         p->cpus_allowed    = *new_mask;
1248         p->rt.nr_cpus_allowed = weight;
1249 }
1250
1251 /* Assumes rq->lock is held */
1252 static void rq_online_rt(struct rq *rq)
1253 {
1254         if (rq->rt.overloaded)
1255                 rt_set_overload(rq);
1256
1257         __enable_runtime(rq);
1258
1259         cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio);
1260 }
1261
1262 /* Assumes rq->lock is held */
1263 static void rq_offline_rt(struct rq *rq)
1264 {
1265         if (rq->rt.overloaded)
1266                 rt_clear_overload(rq);
1267
1268         __disable_runtime(rq);
1269
1270         cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
1271 }
1272
1273 /*
1274  * When switch from the rt queue, we bring ourselves to a position
1275  * that we might want to pull RT tasks from other runqueues.
1276  */
1277 static void switched_from_rt(struct rq *rq, struct task_struct *p,
1278                            int running)
1279 {
1280         /*
1281          * If there are other RT tasks then we will reschedule
1282          * and the scheduling of the other RT tasks will handle
1283          * the balancing. But if we are the last RT task
1284          * we may need to handle the pulling of RT tasks
1285          * now.
1286          */
1287         if (!rq->rt.rt_nr_running)
1288                 pull_rt_task(rq);
1289 }
1290 #endif /* CONFIG_SMP */
1291
1292 /*
1293  * When switching a task to RT, we may overload the runqueue
1294  * with RT tasks. In this case we try to push them off to
1295  * other runqueues.
1296  */
1297 static void switched_to_rt(struct rq *rq, struct task_struct *p,
1298                            int running)
1299 {
1300         int check_resched = 1;
1301
1302         /*
1303          * If we are already running, then there's nothing
1304          * that needs to be done. But if we are not running
1305          * we may need to preempt the current running task.
1306          * If that current running task is also an RT task
1307          * then see if we can move to another run queue.
1308          */
1309         if (!running) {
1310 #ifdef CONFIG_SMP
1311                 if (rq->rt.overloaded && push_rt_task(rq) &&
1312                     /* Don't resched if we changed runqueues */
1313                     rq != task_rq(p))
1314                         check_resched = 0;
1315 #endif /* CONFIG_SMP */
1316                 if (check_resched && p->prio < rq->curr->prio)
1317                         resched_task(rq->curr);
1318         }
1319 }
1320
1321 /*
1322  * Priority of the task has changed. This may cause
1323  * us to initiate a push or pull.
1324  */
1325 static void prio_changed_rt(struct rq *rq, struct task_struct *p,
1326                             int oldprio, int running)
1327 {
1328         if (running) {
1329 #ifdef CONFIG_SMP
1330                 /*
1331                  * If our priority decreases while running, we
1332                  * may need to pull tasks to this runqueue.
1333                  */
1334                 if (oldprio < p->prio)
1335                         pull_rt_task(rq);
1336                 /*
1337                  * If there's a higher priority task waiting to run
1338                  * then reschedule. Note, the above pull_rt_task
1339                  * can release the rq lock and p could migrate.
1340                  * Only reschedule if p is still on the same runqueue.
1341                  */
1342                 if (p->prio > rq->rt.highest_prio && rq->curr == p)
1343                         resched_task(p);
1344 #else
1345                 /* For UP simply resched on drop of prio */
1346                 if (oldprio < p->prio)
1347                         resched_task(p);
1348 #endif /* CONFIG_SMP */
1349         } else {
1350                 /*
1351                  * This task is not running, but if it is
1352                  * greater than the current running task
1353                  * then reschedule.
1354                  */
1355                 if (p->prio < rq->curr->prio)
1356                         resched_task(rq->curr);
1357         }
1358 }
1359
1360 static void watchdog(struct rq *rq, struct task_struct *p)
1361 {
1362         unsigned long soft, hard;
1363
1364         if (!p->signal)
1365                 return;
1366
1367         soft = p->signal->rlim[RLIMIT_RTTIME].rlim_cur;
1368         hard = p->signal->rlim[RLIMIT_RTTIME].rlim_max;
1369
1370         if (soft != RLIM_INFINITY) {
1371                 unsigned long next;
1372
1373                 p->rt.timeout++;
1374                 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
1375                 if (p->rt.timeout > next)
1376                         p->it_sched_expires = p->se.sum_exec_runtime;
1377         }
1378 }
1379
1380 static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
1381 {
1382         update_curr_rt(rq);
1383
1384         watchdog(rq, p);
1385
1386         /*
1387          * RR tasks need a special form of timeslice management.
1388          * FIFO tasks have no timeslices.
1389          */
1390         if (p->policy != SCHED_RR)
1391                 return;
1392
1393         if (--p->rt.time_slice)
1394                 return;
1395
1396         p->rt.time_slice = DEF_TIMESLICE;
1397
1398         /*
1399          * Requeue to the end of queue if we are not the only element
1400          * on the queue:
1401          */
1402         if (p->rt.run_list.prev != p->rt.run_list.next) {
1403                 requeue_task_rt(rq, p);
1404                 set_tsk_need_resched(p);
1405         }
1406 }
1407
1408 static void set_curr_task_rt(struct rq *rq)
1409 {
1410         struct task_struct *p = rq->curr;
1411
1412         p->se.exec_start = rq->clock;
1413 }
1414
1415 static const struct sched_class rt_sched_class = {
1416         .next                   = &fair_sched_class,
1417         .enqueue_task           = enqueue_task_rt,
1418         .dequeue_task           = dequeue_task_rt,
1419         .yield_task             = yield_task_rt,
1420 #ifdef CONFIG_SMP
1421         .select_task_rq         = select_task_rq_rt,
1422 #endif /* CONFIG_SMP */
1423
1424         .check_preempt_curr     = check_preempt_curr_rt,
1425
1426         .pick_next_task         = pick_next_task_rt,
1427         .put_prev_task          = put_prev_task_rt,
1428
1429 #ifdef CONFIG_SMP
1430         .load_balance           = load_balance_rt,
1431         .move_one_task          = move_one_task_rt,
1432         .set_cpus_allowed       = set_cpus_allowed_rt,
1433         .rq_online              = rq_online_rt,
1434         .rq_offline             = rq_offline_rt,
1435         .pre_schedule           = pre_schedule_rt,
1436         .post_schedule          = post_schedule_rt,
1437         .task_wake_up           = task_wake_up_rt,
1438         .switched_from          = switched_from_rt,
1439 #endif
1440
1441         .set_curr_task          = set_curr_task_rt,
1442         .task_tick              = task_tick_rt,
1443
1444         .prio_changed           = prio_changed_rt,
1445         .switched_to            = switched_to_rt,
1446 };