sched: clean up kernel/sched_rt.c
[safe/jmp/linux-2.6] / 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 /*
9  * The "RT overload" flag: it gets set if a CPU has more than
10  * one runnable RT task.
11  */
12 static cpumask_t rt_overload_mask;
13 static atomic_t rto_count;
14
15 static inline int rt_overloaded(void)
16 {
17         return atomic_read(&rto_count);
18 }
19
20 static inline cpumask_t *rt_overload(void)
21 {
22         return &rt_overload_mask;
23 }
24
25 static inline void rt_set_overload(struct rq *rq)
26 {
27         rq->rt.overloaded = 1;
28         cpu_set(rq->cpu, rt_overload_mask);
29         /*
30          * Make sure the mask is visible before we set
31          * the overload count. That is checked to determine
32          * if we should look at the mask. It would be a shame
33          * if we looked at the mask, but the mask was not
34          * updated yet.
35          */
36         wmb();
37         atomic_inc(&rto_count);
38 }
39
40 static inline void rt_clear_overload(struct rq *rq)
41 {
42         /* the order here really doesn't matter */
43         atomic_dec(&rto_count);
44         cpu_clear(rq->cpu, rt_overload_mask);
45         rq->rt.overloaded = 0;
46 }
47
48 static void update_rt_migration(struct rq *rq)
49 {
50         if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1))
51                 rt_set_overload(rq);
52         else
53                 rt_clear_overload(rq);
54 }
55 #endif /* CONFIG_SMP */
56
57 /*
58  * Update the current task's runtime statistics. Skip current tasks that
59  * are not in our scheduling class.
60  */
61 static void update_curr_rt(struct rq *rq)
62 {
63         struct task_struct *curr = rq->curr;
64         u64 delta_exec;
65
66         if (!task_has_rt_policy(curr))
67                 return;
68
69         delta_exec = rq->clock - curr->se.exec_start;
70         if (unlikely((s64)delta_exec < 0))
71                 delta_exec = 0;
72
73         schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
74
75         curr->se.sum_exec_runtime += delta_exec;
76         curr->se.exec_start = rq->clock;
77         cpuacct_charge(curr, delta_exec);
78 }
79
80 static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq)
81 {
82         WARN_ON(!rt_task(p));
83         rq->rt.rt_nr_running++;
84 #ifdef CONFIG_SMP
85         if (p->prio < rq->rt.highest_prio)
86                 rq->rt.highest_prio = p->prio;
87         if (p->nr_cpus_allowed > 1)
88                 rq->rt.rt_nr_migratory++;
89
90         update_rt_migration(rq);
91 #endif /* CONFIG_SMP */
92 }
93
94 static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
95 {
96         WARN_ON(!rt_task(p));
97         WARN_ON(!rq->rt.rt_nr_running);
98         rq->rt.rt_nr_running--;
99 #ifdef CONFIG_SMP
100         if (rq->rt.rt_nr_running) {
101                 struct rt_prio_array *array;
102
103                 WARN_ON(p->prio < rq->rt.highest_prio);
104                 if (p->prio == rq->rt.highest_prio) {
105                         /* recalculate */
106                         array = &rq->rt.active;
107                         rq->rt.highest_prio =
108                                 sched_find_first_bit(array->bitmap);
109                 } /* otherwise leave rq->highest prio alone */
110         } else
111                 rq->rt.highest_prio = MAX_RT_PRIO;
112         if (p->nr_cpus_allowed > 1)
113                 rq->rt.rt_nr_migratory--;
114
115         update_rt_migration(rq);
116 #endif /* CONFIG_SMP */
117 }
118
119 static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
120 {
121         struct rt_prio_array *array = &rq->rt.active;
122
123         list_add_tail(&p->run_list, array->queue + p->prio);
124         __set_bit(p->prio, array->bitmap);
125         inc_cpu_load(rq, p->se.load.weight);
126
127         inc_rt_tasks(p, rq);
128 }
129
130 /*
131  * Adding/removing a task to/from a priority array:
132  */
133 static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
134 {
135         struct rt_prio_array *array = &rq->rt.active;
136
137         update_curr_rt(rq);
138
139         list_del(&p->run_list);
140         if (list_empty(array->queue + p->prio))
141                 __clear_bit(p->prio, array->bitmap);
142         dec_cpu_load(rq, p->se.load.weight);
143
144         dec_rt_tasks(p, rq);
145 }
146
147 /*
148  * Put task to the end of the run list without the overhead of dequeue
149  * followed by enqueue.
150  */
151 static void requeue_task_rt(struct rq *rq, struct task_struct *p)
152 {
153         struct rt_prio_array *array = &rq->rt.active;
154
155         list_move_tail(&p->run_list, array->queue + p->prio);
156 }
157
158 static void
159 yield_task_rt(struct rq *rq)
160 {
161         requeue_task_rt(rq, rq->curr);
162 }
163
164 #ifdef CONFIG_SMP
165 static int find_lowest_rq(struct task_struct *task);
166
167 static int select_task_rq_rt(struct task_struct *p, int sync)
168 {
169         struct rq *rq = task_rq(p);
170
171         /*
172          * If the current task is an RT task, then
173          * try to see if we can wake this RT task up on another
174          * runqueue. Otherwise simply start this RT task
175          * on its current runqueue.
176          *
177          * We want to avoid overloading runqueues. Even if
178          * the RT task is of higher priority than the current RT task.
179          * RT tasks behave differently than other tasks. If
180          * one gets preempted, we try to push it off to another queue.
181          * So trying to keep a preempting RT task on the same
182          * cache hot CPU will force the running RT task to
183          * a cold CPU. So we waste all the cache for the lower
184          * RT task in hopes of saving some of a RT task
185          * that is just being woken and probably will have
186          * cold cache anyway.
187          */
188         if (unlikely(rt_task(rq->curr)) &&
189             (p->nr_cpus_allowed > 1)) {
190                 int cpu = find_lowest_rq(p);
191
192                 return (cpu == -1) ? task_cpu(p) : cpu;
193         }
194
195         /*
196          * Otherwise, just let it ride on the affined RQ and the
197          * post-schedule router will push the preempted task away
198          */
199         return task_cpu(p);
200 }
201 #endif /* CONFIG_SMP */
202
203 /*
204  * Preempt the current task with a newly woken task if needed:
205  */
206 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
207 {
208         if (p->prio < rq->curr->prio)
209                 resched_task(rq->curr);
210 }
211
212 static struct task_struct *pick_next_task_rt(struct rq *rq)
213 {
214         struct rt_prio_array *array = &rq->rt.active;
215         struct task_struct *next;
216         struct list_head *queue;
217         int idx;
218
219         idx = sched_find_first_bit(array->bitmap);
220         if (idx >= MAX_RT_PRIO)
221                 return NULL;
222
223         queue = array->queue + idx;
224         next = list_entry(queue->next, struct task_struct, run_list);
225
226         next->se.exec_start = rq->clock;
227
228         return next;
229 }
230
231 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
232 {
233         update_curr_rt(rq);
234         p->se.exec_start = 0;
235 }
236
237 #ifdef CONFIG_SMP
238 /* Only try algorithms three times */
239 #define RT_MAX_TRIES 3
240
241 static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
242 static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
243
244 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
245 {
246         if (!task_running(rq, p) &&
247             (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
248             (p->nr_cpus_allowed > 1))
249                 return 1;
250         return 0;
251 }
252
253 /* Return the second highest RT task, NULL otherwise */
254 static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
255 {
256         struct rt_prio_array *array = &rq->rt.active;
257         struct task_struct *next;
258         struct list_head *queue;
259         int idx;
260
261         assert_spin_locked(&rq->lock);
262
263         if (likely(rq->rt.rt_nr_running < 2))
264                 return NULL;
265
266         idx = sched_find_first_bit(array->bitmap);
267         if (unlikely(idx >= MAX_RT_PRIO)) {
268                 WARN_ON(1); /* rt_nr_running is bad */
269                 return NULL;
270         }
271
272         queue = array->queue + idx;
273         BUG_ON(list_empty(queue));
274
275         next = list_entry(queue->next, struct task_struct, run_list);
276         if (unlikely(pick_rt_task(rq, next, cpu)))
277                 goto out;
278
279         if (queue->next->next != queue) {
280                 /* same prio task */
281                 next = list_entry(queue->next->next, struct task_struct,
282                                   run_list);
283                 if (pick_rt_task(rq, next, cpu))
284                         goto out;
285         }
286
287  retry:
288         /* slower, but more flexible */
289         idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
290         if (unlikely(idx >= MAX_RT_PRIO))
291                 return NULL;
292
293         queue = array->queue + idx;
294         BUG_ON(list_empty(queue));
295
296         list_for_each_entry(next, queue, run_list) {
297                 if (pick_rt_task(rq, next, cpu))
298                         goto out;
299         }
300
301         goto retry;
302
303  out:
304         return next;
305 }
306
307 static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
308
309 static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
310 {
311         int       lowest_prio = -1;
312         int       lowest_cpu  = -1;
313         int       count       = 0;
314         int       cpu;
315
316         cpus_and(*lowest_mask, cpu_online_map, task->cpus_allowed);
317
318         /*
319          * Scan each rq for the lowest prio.
320          */
321         for_each_cpu_mask(cpu, *lowest_mask) {
322                 struct rq *rq = cpu_rq(cpu);
323
324                 /* We look for lowest RT prio or non-rt CPU */
325                 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
326                         /*
327                          * if we already found a low RT queue
328                          * and now we found this non-rt queue
329                          * clear the mask and set our bit.
330                          * Otherwise just return the queue as is
331                          * and the count==1 will cause the algorithm
332                          * to use the first bit found.
333                          */
334                         if (lowest_cpu != -1) {
335                                 cpus_clear(*lowest_mask);
336                                 cpu_set(rq->cpu, *lowest_mask);
337                         }
338                         return 1;
339                 }
340
341                 /* no locking for now */
342                 if ((rq->rt.highest_prio > task->prio)
343                     && (rq->rt.highest_prio >= lowest_prio)) {
344                         if (rq->rt.highest_prio > lowest_prio) {
345                                 /* new low - clear old data */
346                                 lowest_prio = rq->rt.highest_prio;
347                                 lowest_cpu = cpu;
348                                 count = 0;
349                         }
350                         count++;
351                 } else
352                         cpu_clear(cpu, *lowest_mask);
353         }
354
355         /*
356          * Clear out all the set bits that represent
357          * runqueues that were of higher prio than
358          * the lowest_prio.
359          */
360         if (lowest_cpu > 0) {
361                 /*
362                  * Perhaps we could add another cpumask op to
363                  * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
364                  * Then that could be optimized to use memset and such.
365                  */
366                 for_each_cpu_mask(cpu, *lowest_mask) {
367                         if (cpu >= lowest_cpu)
368                                 break;
369                         cpu_clear(cpu, *lowest_mask);
370                 }
371         }
372
373         return count;
374 }
375
376 static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
377 {
378         int first;
379
380         /* "this_cpu" is cheaper to preempt than a remote processor */
381         if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
382                 return this_cpu;
383
384         first = first_cpu(*mask);
385         if (first != NR_CPUS)
386                 return first;
387
388         return -1;
389 }
390
391 static int find_lowest_rq(struct task_struct *task)
392 {
393         struct sched_domain *sd;
394         cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
395         int this_cpu = smp_processor_id();
396         int cpu      = task_cpu(task);
397         int count    = find_lowest_cpus(task, lowest_mask);
398
399         if (!count)
400                 return -1; /* No targets found */
401
402         /*
403          * There is no sense in performing an optimal search if only one
404          * target is found.
405          */
406         if (count == 1)
407                 return first_cpu(*lowest_mask);
408
409         /*
410          * At this point we have built a mask of cpus representing the
411          * lowest priority tasks in the system.  Now we want to elect
412          * the best one based on our affinity and topology.
413          *
414          * We prioritize the last cpu that the task executed on since
415          * it is most likely cache-hot in that location.
416          */
417         if (cpu_isset(cpu, *lowest_mask))
418                 return cpu;
419
420         /*
421          * Otherwise, we consult the sched_domains span maps to figure
422          * out which cpu is logically closest to our hot cache data.
423          */
424         if (this_cpu == cpu)
425                 this_cpu = -1; /* Skip this_cpu opt if the same */
426
427         for_each_domain(cpu, sd) {
428                 if (sd->flags & SD_WAKE_AFFINE) {
429                         cpumask_t domain_mask;
430                         int       best_cpu;
431
432                         cpus_and(domain_mask, sd->span, *lowest_mask);
433
434                         best_cpu = pick_optimal_cpu(this_cpu,
435                                                     &domain_mask);
436                         if (best_cpu != -1)
437                                 return best_cpu;
438                 }
439         }
440
441         /*
442          * And finally, if there were no matches within the domains
443          * just give the caller *something* to work with from the compatible
444          * locations.
445          */
446         return pick_optimal_cpu(this_cpu, lowest_mask);
447 }
448
449 /* Will lock the rq it finds */
450 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
451 {
452         struct rq *lowest_rq = NULL;
453         int tries;
454         int cpu;
455
456         for (tries = 0; tries < RT_MAX_TRIES; tries++) {
457                 cpu = find_lowest_rq(task);
458
459                 if ((cpu == -1) || (cpu == rq->cpu))
460                         break;
461
462                 lowest_rq = cpu_rq(cpu);
463
464                 /* if the prio of this runqueue changed, try again */
465                 if (double_lock_balance(rq, lowest_rq)) {
466                         /*
467                          * We had to unlock the run queue. In
468                          * the mean time, task could have
469                          * migrated already or had its affinity changed.
470                          * Also make sure that it wasn't scheduled on its rq.
471                          */
472                         if (unlikely(task_rq(task) != rq ||
473                                      !cpu_isset(lowest_rq->cpu,
474                                                 task->cpus_allowed) ||
475                                      task_running(rq, task) ||
476                                      !task->se.on_rq)) {
477
478                                 spin_unlock(&lowest_rq->lock);
479                                 lowest_rq = NULL;
480                                 break;
481                         }
482                 }
483
484                 /* If this rq is still suitable use it. */
485                 if (lowest_rq->rt.highest_prio > task->prio)
486                         break;
487
488                 /* try again */
489                 spin_unlock(&lowest_rq->lock);
490                 lowest_rq = NULL;
491         }
492
493         return lowest_rq;
494 }
495
496 /*
497  * If the current CPU has more than one RT task, see if the non
498  * running task can migrate over to a CPU that is running a task
499  * of lesser priority.
500  */
501 static int push_rt_task(struct rq *rq)
502 {
503         struct task_struct *next_task;
504         struct rq *lowest_rq;
505         int ret = 0;
506         int paranoid = RT_MAX_TRIES;
507
508         assert_spin_locked(&rq->lock);
509
510         if (!rq->rt.overloaded)
511                 return 0;
512
513         next_task = pick_next_highest_task_rt(rq, -1);
514         if (!next_task)
515                 return 0;
516
517  retry:
518         if (unlikely(next_task == rq->curr)) {
519                 WARN_ON(1);
520                 return 0;
521         }
522
523         /*
524          * It's possible that the next_task slipped in of
525          * higher priority than current. If that's the case
526          * just reschedule current.
527          */
528         if (unlikely(next_task->prio < rq->curr->prio)) {
529                 resched_task(rq->curr);
530                 return 0;
531         }
532
533         /* We might release rq lock */
534         get_task_struct(next_task);
535
536         /* find_lock_lowest_rq locks the rq if found */
537         lowest_rq = find_lock_lowest_rq(next_task, rq);
538         if (!lowest_rq) {
539                 struct task_struct *task;
540                 /*
541                  * find lock_lowest_rq releases rq->lock
542                  * so it is possible that next_task has changed.
543                  * If it has, then try again.
544                  */
545                 task = pick_next_highest_task_rt(rq, -1);
546                 if (unlikely(task != next_task) && task && paranoid--) {
547                         put_task_struct(next_task);
548                         next_task = task;
549                         goto retry;
550                 }
551                 goto out;
552         }
553
554         assert_spin_locked(&lowest_rq->lock);
555
556         deactivate_task(rq, next_task, 0);
557         set_task_cpu(next_task, lowest_rq->cpu);
558         activate_task(lowest_rq, next_task, 0);
559
560         resched_task(lowest_rq->curr);
561
562         spin_unlock(&lowest_rq->lock);
563
564         ret = 1;
565 out:
566         put_task_struct(next_task);
567
568         return ret;
569 }
570
571 /*
572  * TODO: Currently we just use the second highest prio task on
573  *       the queue, and stop when it can't migrate (or there's
574  *       no more RT tasks).  There may be a case where a lower
575  *       priority RT task has a different affinity than the
576  *       higher RT task. In this case the lower RT task could
577  *       possibly be able to migrate where as the higher priority
578  *       RT task could not.  We currently ignore this issue.
579  *       Enhancements are welcome!
580  */
581 static void push_rt_tasks(struct rq *rq)
582 {
583         /* push_rt_task will return true if it moved an RT */
584         while (push_rt_task(rq))
585                 ;
586 }
587
588 static int pull_rt_task(struct rq *this_rq)
589 {
590         struct task_struct *next;
591         struct task_struct *p;
592         struct rq *src_rq;
593         cpumask_t *rto_cpumask;
594         int this_cpu = this_rq->cpu;
595         int cpu;
596         int ret = 0;
597
598         assert_spin_locked(&this_rq->lock);
599
600         /*
601          * If cpusets are used, and we have overlapping
602          * run queue cpusets, then this algorithm may not catch all.
603          * This is just the price you pay on trying to keep
604          * dirtying caches down on large SMP machines.
605          */
606         if (likely(!rt_overloaded()))
607                 return 0;
608
609         next = pick_next_task_rt(this_rq);
610
611         rto_cpumask = rt_overload();
612
613         for_each_cpu_mask(cpu, *rto_cpumask) {
614                 if (this_cpu == cpu)
615                         continue;
616
617                 src_rq = cpu_rq(cpu);
618                 if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
619                         /*
620                          * It is possible that overlapping cpusets
621                          * will miss clearing a non overloaded runqueue.
622                          * Clear it now.
623                          */
624                         if (double_lock_balance(this_rq, src_rq)) {
625                                 /* unlocked our runqueue lock */
626                                 struct task_struct *old_next = next;
627                                 next = pick_next_task_rt(this_rq);
628                                 if (next != old_next)
629                                         ret = 1;
630                         }
631                         if (likely(src_rq->rt.rt_nr_running <= 1))
632                                 /*
633                                  * Small chance that this_rq->curr changed
634                                  * but it's really harmless here.
635                                  */
636                                 rt_clear_overload(this_rq);
637                         else
638                                 /*
639                                  * Heh, the src_rq is now overloaded, since
640                                  * we already have the src_rq lock, go straight
641                                  * to pulling tasks from it.
642                                  */
643                                 goto try_pulling;
644                         spin_unlock(&src_rq->lock);
645                         continue;
646                 }
647
648                 /*
649                  * We can potentially drop this_rq's lock in
650                  * double_lock_balance, and another CPU could
651                  * steal our next task - hence we must cause
652                  * the caller to recalculate the next task
653                  * in that case:
654                  */
655                 if (double_lock_balance(this_rq, src_rq)) {
656                         struct task_struct *old_next = next;
657                         next = pick_next_task_rt(this_rq);
658                         if (next != old_next)
659                                 ret = 1;
660                 }
661
662                 /*
663                  * Are there still pullable RT tasks?
664                  */
665                 if (src_rq->rt.rt_nr_running <= 1) {
666                         spin_unlock(&src_rq->lock);
667                         continue;
668                 }
669
670  try_pulling:
671                 p = pick_next_highest_task_rt(src_rq, this_cpu);
672
673                 /*
674                  * Do we have an RT task that preempts
675                  * the to-be-scheduled task?
676                  */
677                 if (p && (!next || (p->prio < next->prio))) {
678                         WARN_ON(p == src_rq->curr);
679                         WARN_ON(!p->se.on_rq);
680
681                         /*
682                          * There's a chance that p is higher in priority
683                          * than what's currently running on its cpu.
684                          * This is just that p is wakeing up and hasn't
685                          * had a chance to schedule. We only pull
686                          * p if it is lower in priority than the
687                          * current task on the run queue or
688                          * this_rq next task is lower in prio than
689                          * the current task on that rq.
690                          */
691                         if (p->prio < src_rq->curr->prio ||
692                             (next && next->prio < src_rq->curr->prio))
693                                 goto bail;
694
695                         ret = 1;
696
697                         deactivate_task(src_rq, p, 0);
698                         set_task_cpu(p, this_cpu);
699                         activate_task(this_rq, p, 0);
700                         /*
701                          * We continue with the search, just in
702                          * case there's an even higher prio task
703                          * in another runqueue. (low likelyhood
704                          * but possible)
705                          */
706
707                         /*
708                          * Update next so that we won't pick a task
709                          * on another cpu with a priority lower (or equal)
710                          * than the one we just picked.
711                          */
712                         next = p;
713
714                 }
715  bail:
716                 spin_unlock(&src_rq->lock);
717         }
718
719         return ret;
720 }
721
722 static void schedule_balance_rt(struct rq *rq,
723                                 struct task_struct *prev)
724 {
725         /* Try to pull RT tasks here if we lower this rq's prio */
726         if (unlikely(rt_task(prev)) &&
727             rq->rt.highest_prio > prev->prio)
728                 pull_rt_task(rq);
729 }
730
731 static void schedule_tail_balance_rt(struct rq *rq)
732 {
733         /*
734          * If we have more than one rt_task queued, then
735          * see if we can push the other rt_tasks off to other CPUS.
736          * Note we may release the rq lock, and since
737          * the lock was owned by prev, we need to release it
738          * first via finish_lock_switch and then reaquire it here.
739          */
740         if (unlikely(rq->rt.overloaded)) {
741                 spin_lock_irq(&rq->lock);
742                 push_rt_tasks(rq);
743                 spin_unlock_irq(&rq->lock);
744         }
745 }
746
747
748 static void wakeup_balance_rt(struct rq *rq, struct task_struct *p)
749 {
750         if (unlikely(rt_task(p)) &&
751             !task_running(rq, p) &&
752             (p->prio >= rq->rt.highest_prio) &&
753             rq->rt.overloaded)
754                 push_rt_tasks(rq);
755 }
756
757 static unsigned long
758 load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
759                 unsigned long max_load_move,
760                 struct sched_domain *sd, enum cpu_idle_type idle,
761                 int *all_pinned, int *this_best_prio)
762 {
763         /* don't touch RT tasks */
764         return 0;
765 }
766
767 static int
768 move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
769                  struct sched_domain *sd, enum cpu_idle_type idle)
770 {
771         /* don't touch RT tasks */
772         return 0;
773 }
774
775 static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
776 {
777         int weight = cpus_weight(*new_mask);
778
779         BUG_ON(!rt_task(p));
780
781         /*
782          * Update the migration status of the RQ if we have an RT task
783          * which is running AND changing its weight value.
784          */
785         if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
786                 struct rq *rq = task_rq(p);
787
788                 if ((p->nr_cpus_allowed <= 1) && (weight > 1)) {
789                         rq->rt.rt_nr_migratory++;
790                 } else if ((p->nr_cpus_allowed > 1) && (weight <= 1)) {
791                         BUG_ON(!rq->rt.rt_nr_migratory);
792                         rq->rt.rt_nr_migratory--;
793                 }
794
795                 update_rt_migration(rq);
796         }
797
798         p->cpus_allowed    = *new_mask;
799         p->nr_cpus_allowed = weight;
800 }
801
802 #else /* CONFIG_SMP */
803 # define schedule_tail_balance_rt(rq)   do { } while (0)
804 # define schedule_balance_rt(rq, prev)  do { } while (0)
805 # define wakeup_balance_rt(rq, p)       do { } while (0)
806 #endif /* CONFIG_SMP */
807
808 static void task_tick_rt(struct rq *rq, struct task_struct *p)
809 {
810         update_curr_rt(rq);
811
812         /*
813          * RR tasks need a special form of timeslice management.
814          * FIFO tasks have no timeslices.
815          */
816         if (p->policy != SCHED_RR)
817                 return;
818
819         if (--p->time_slice)
820                 return;
821
822         p->time_slice = DEF_TIMESLICE;
823
824         /*
825          * Requeue to the end of queue if we are not the only element
826          * on the queue:
827          */
828         if (p->run_list.prev != p->run_list.next) {
829                 requeue_task_rt(rq, p);
830                 set_tsk_need_resched(p);
831         }
832 }
833
834 static void set_curr_task_rt(struct rq *rq)
835 {
836         struct task_struct *p = rq->curr;
837
838         p->se.exec_start = rq->clock;
839 }
840
841 const struct sched_class rt_sched_class = {
842         .next                   = &fair_sched_class,
843         .enqueue_task           = enqueue_task_rt,
844         .dequeue_task           = dequeue_task_rt,
845         .yield_task             = yield_task_rt,
846 #ifdef CONFIG_SMP
847         .select_task_rq         = select_task_rq_rt,
848 #endif /* CONFIG_SMP */
849
850         .check_preempt_curr     = check_preempt_curr_rt,
851
852         .pick_next_task         = pick_next_task_rt,
853         .put_prev_task          = put_prev_task_rt,
854
855 #ifdef CONFIG_SMP
856         .load_balance           = load_balance_rt,
857         .move_one_task          = move_one_task_rt,
858         .set_cpus_allowed       = set_cpus_allowed_rt,
859 #endif
860
861         .set_curr_task          = set_curr_task_rt,
862         .task_tick              = task_tick_rt,
863 };