sched: clean up kernel/sched_rt.c
[linux-2.6/linux-loongson.git] / kernel / sched_rt.c
blob1a2d8f0aa659d5fd0dd8c0d44d18d438189d0c36
1 /*
2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
3 * policies)
4 */
6 #ifdef CONFIG_SMP
8 /*
9 * The "RT overload" flag: it gets set if a CPU has more than
10 * one runnable RT task.
12 static cpumask_t rt_overload_mask;
13 static atomic_t rto_count;
15 static inline int rt_overloaded(void)
17 return atomic_read(&rto_count);
20 static inline cpumask_t *rt_overload(void)
22 return &rt_overload_mask;
25 static inline void rt_set_overload(struct rq *rq)
27 rq->rt.overloaded = 1;
28 cpu_set(rq->cpu, rt_overload_mask);
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.
36 wmb();
37 atomic_inc(&rto_count);
40 static inline void rt_clear_overload(struct rq *rq)
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;
48 static void update_rt_migration(struct rq *rq)
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);
55 #endif /* CONFIG_SMP */
58 * Update the current task's runtime statistics. Skip current tasks that
59 * are not in our scheduling class.
61 static void update_curr_rt(struct rq *rq)
63 struct task_struct *curr = rq->curr;
64 u64 delta_exec;
66 if (!task_has_rt_policy(curr))
67 return;
69 delta_exec = rq->clock - curr->se.exec_start;
70 if (unlikely((s64)delta_exec < 0))
71 delta_exec = 0;
73 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
75 curr->se.sum_exec_runtime += delta_exec;
76 curr->se.exec_start = rq->clock;
77 cpuacct_charge(curr, delta_exec);
80 static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq)
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++;
90 update_rt_migration(rq);
91 #endif /* CONFIG_SMP */
94 static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
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;
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--;
115 update_rt_migration(rq);
116 #endif /* CONFIG_SMP */
119 static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
121 struct rt_prio_array *array = &rq->rt.active;
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);
127 inc_rt_tasks(p, rq);
131 * Adding/removing a task to/from a priority array:
133 static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
135 struct rt_prio_array *array = &rq->rt.active;
137 update_curr_rt(rq);
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);
144 dec_rt_tasks(p, rq);
148 * Put task to the end of the run list without the overhead of dequeue
149 * followed by enqueue.
151 static void requeue_task_rt(struct rq *rq, struct task_struct *p)
153 struct rt_prio_array *array = &rq->rt.active;
155 list_move_tail(&p->run_list, array->queue + p->prio);
158 static void
159 yield_task_rt(struct rq *rq)
161 requeue_task_rt(rq, rq->curr);
164 #ifdef CONFIG_SMP
165 static int find_lowest_rq(struct task_struct *task);
167 static int select_task_rq_rt(struct task_struct *p, int sync)
169 struct rq *rq = task_rq(p);
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.
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.
188 if (unlikely(rt_task(rq->curr)) &&
189 (p->nr_cpus_allowed > 1)) {
190 int cpu = find_lowest_rq(p);
192 return (cpu == -1) ? task_cpu(p) : cpu;
196 * Otherwise, just let it ride on the affined RQ and the
197 * post-schedule router will push the preempted task away
199 return task_cpu(p);
201 #endif /* CONFIG_SMP */
204 * Preempt the current task with a newly woken task if needed:
206 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
208 if (p->prio < rq->curr->prio)
209 resched_task(rq->curr);
212 static struct task_struct *pick_next_task_rt(struct rq *rq)
214 struct rt_prio_array *array = &rq->rt.active;
215 struct task_struct *next;
216 struct list_head *queue;
217 int idx;
219 idx = sched_find_first_bit(array->bitmap);
220 if (idx >= MAX_RT_PRIO)
221 return NULL;
223 queue = array->queue + idx;
224 next = list_entry(queue->next, struct task_struct, run_list);
226 next->se.exec_start = rq->clock;
228 return next;
231 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
233 update_curr_rt(rq);
234 p->se.exec_start = 0;
237 #ifdef CONFIG_SMP
238 /* Only try algorithms three times */
239 #define RT_MAX_TRIES 3
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);
244 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
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;
253 /* Return the second highest RT task, NULL otherwise */
254 static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
256 struct rt_prio_array *array = &rq->rt.active;
257 struct task_struct *next;
258 struct list_head *queue;
259 int idx;
261 assert_spin_locked(&rq->lock);
263 if (likely(rq->rt.rt_nr_running < 2))
264 return NULL;
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;
272 queue = array->queue + idx;
273 BUG_ON(list_empty(queue));
275 next = list_entry(queue->next, struct task_struct, run_list);
276 if (unlikely(pick_rt_task(rq, next, cpu)))
277 goto out;
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;
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;
293 queue = array->queue + idx;
294 BUG_ON(list_empty(queue));
296 list_for_each_entry(next, queue, run_list) {
297 if (pick_rt_task(rq, next, cpu))
298 goto out;
301 goto retry;
303 out:
304 return next;
307 static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
309 static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
311 int lowest_prio = -1;
312 int lowest_cpu = -1;
313 int count = 0;
314 int cpu;
316 cpus_and(*lowest_mask, cpu_online_map, task->cpus_allowed);
319 * Scan each rq for the lowest prio.
321 for_each_cpu_mask(cpu, *lowest_mask) {
322 struct rq *rq = cpu_rq(cpu);
324 /* We look for lowest RT prio or non-rt CPU */
325 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
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.
334 if (lowest_cpu != -1) {
335 cpus_clear(*lowest_mask);
336 cpu_set(rq->cpu, *lowest_mask);
338 return 1;
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;
350 count++;
351 } else
352 cpu_clear(cpu, *lowest_mask);
356 * Clear out all the set bits that represent
357 * runqueues that were of higher prio than
358 * the lowest_prio.
360 if (lowest_cpu > 0) {
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.
366 for_each_cpu_mask(cpu, *lowest_mask) {
367 if (cpu >= lowest_cpu)
368 break;
369 cpu_clear(cpu, *lowest_mask);
373 return count;
376 static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
378 int first;
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;
384 first = first_cpu(*mask);
385 if (first != NR_CPUS)
386 return first;
388 return -1;
391 static int find_lowest_rq(struct task_struct *task)
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);
399 if (!count)
400 return -1; /* No targets found */
403 * There is no sense in performing an optimal search if only one
404 * target is found.
406 if (count == 1)
407 return first_cpu(*lowest_mask);
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.
414 * We prioritize the last cpu that the task executed on since
415 * it is most likely cache-hot in that location.
417 if (cpu_isset(cpu, *lowest_mask))
418 return cpu;
421 * Otherwise, we consult the sched_domains span maps to figure
422 * out which cpu is logically closest to our hot cache data.
424 if (this_cpu == cpu)
425 this_cpu = -1; /* Skip this_cpu opt if the same */
427 for_each_domain(cpu, sd) {
428 if (sd->flags & SD_WAKE_AFFINE) {
429 cpumask_t domain_mask;
430 int best_cpu;
432 cpus_and(domain_mask, sd->span, *lowest_mask);
434 best_cpu = pick_optimal_cpu(this_cpu,
435 &domain_mask);
436 if (best_cpu != -1)
437 return best_cpu;
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.
446 return pick_optimal_cpu(this_cpu, lowest_mask);
449 /* Will lock the rq it finds */
450 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
452 struct rq *lowest_rq = NULL;
453 int tries;
454 int cpu;
456 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
457 cpu = find_lowest_rq(task);
459 if ((cpu == -1) || (cpu == rq->cpu))
460 break;
462 lowest_rq = cpu_rq(cpu);
464 /* if the prio of this runqueue changed, try again */
465 if (double_lock_balance(rq, lowest_rq)) {
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.
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)) {
478 spin_unlock(&lowest_rq->lock);
479 lowest_rq = NULL;
480 break;
484 /* If this rq is still suitable use it. */
485 if (lowest_rq->rt.highest_prio > task->prio)
486 break;
488 /* try again */
489 spin_unlock(&lowest_rq->lock);
490 lowest_rq = NULL;
493 return lowest_rq;
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.
501 static int push_rt_task(struct rq *rq)
503 struct task_struct *next_task;
504 struct rq *lowest_rq;
505 int ret = 0;
506 int paranoid = RT_MAX_TRIES;
508 assert_spin_locked(&rq->lock);
510 if (!rq->rt.overloaded)
511 return 0;
513 next_task = pick_next_highest_task_rt(rq, -1);
514 if (!next_task)
515 return 0;
517 retry:
518 if (unlikely(next_task == rq->curr)) {
519 WARN_ON(1);
520 return 0;
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.
528 if (unlikely(next_task->prio < rq->curr->prio)) {
529 resched_task(rq->curr);
530 return 0;
533 /* We might release rq lock */
534 get_task_struct(next_task);
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;
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.
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;
551 goto out;
554 assert_spin_locked(&lowest_rq->lock);
556 deactivate_task(rq, next_task, 0);
557 set_task_cpu(next_task, lowest_rq->cpu);
558 activate_task(lowest_rq, next_task, 0);
560 resched_task(lowest_rq->curr);
562 spin_unlock(&lowest_rq->lock);
564 ret = 1;
565 out:
566 put_task_struct(next_task);
568 return ret;
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!
581 static void push_rt_tasks(struct rq *rq)
583 /* push_rt_task will return true if it moved an RT */
584 while (push_rt_task(rq))
588 static int pull_rt_task(struct rq *this_rq)
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;
598 assert_spin_locked(&this_rq->lock);
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.
606 if (likely(!rt_overloaded()))
607 return 0;
609 next = pick_next_task_rt(this_rq);
611 rto_cpumask = rt_overload();
613 for_each_cpu_mask(cpu, *rto_cpumask) {
614 if (this_cpu == cpu)
615 continue;
617 src_rq = cpu_rq(cpu);
618 if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
620 * It is possible that overlapping cpusets
621 * will miss clearing a non overloaded runqueue.
622 * Clear it now.
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;
631 if (likely(src_rq->rt.rt_nr_running <= 1))
633 * Small chance that this_rq->curr changed
634 * but it's really harmless here.
636 rt_clear_overload(this_rq);
637 else
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.
643 goto try_pulling;
644 spin_unlock(&src_rq->lock);
645 continue;
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:
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;
663 * Are there still pullable RT tasks?
665 if (src_rq->rt.rt_nr_running <= 1) {
666 spin_unlock(&src_rq->lock);
667 continue;
670 try_pulling:
671 p = pick_next_highest_task_rt(src_rq, this_cpu);
674 * Do we have an RT task that preempts
675 * the to-be-scheduled task?
677 if (p && (!next || (p->prio < next->prio))) {
678 WARN_ON(p == src_rq->curr);
679 WARN_ON(!p->se.on_rq);
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.
691 if (p->prio < src_rq->curr->prio ||
692 (next && next->prio < src_rq->curr->prio))
693 goto bail;
695 ret = 1;
697 deactivate_task(src_rq, p, 0);
698 set_task_cpu(p, this_cpu);
699 activate_task(this_rq, p, 0);
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)
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.
712 next = p;
715 bail:
716 spin_unlock(&src_rq->lock);
719 return ret;
722 static void schedule_balance_rt(struct rq *rq,
723 struct task_struct *prev)
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);
731 static void schedule_tail_balance_rt(struct rq *rq)
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.
740 if (unlikely(rq->rt.overloaded)) {
741 spin_lock_irq(&rq->lock);
742 push_rt_tasks(rq);
743 spin_unlock_irq(&rq->lock);
748 static void wakeup_balance_rt(struct rq *rq, struct task_struct *p)
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);
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)
763 /* don't touch RT tasks */
764 return 0;
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)
771 /* don't touch RT tasks */
772 return 0;
775 static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
777 int weight = cpus_weight(*new_mask);
779 BUG_ON(!rt_task(p));
782 * Update the migration status of the RQ if we have an RT task
783 * which is running AND changing its weight value.
785 if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
786 struct rq *rq = task_rq(p);
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--;
795 update_rt_migration(rq);
798 p->cpus_allowed = *new_mask;
799 p->nr_cpus_allowed = weight;
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 */
808 static void task_tick_rt(struct rq *rq, struct task_struct *p)
810 update_curr_rt(rq);
813 * RR tasks need a special form of timeslice management.
814 * FIFO tasks have no timeslices.
816 if (p->policy != SCHED_RR)
817 return;
819 if (--p->time_slice)
820 return;
822 p->time_slice = DEF_TIMESLICE;
825 * Requeue to the end of queue if we are not the only element
826 * on the queue:
828 if (p->run_list.prev != p->run_list.next) {
829 requeue_task_rt(rq, p);
830 set_tsk_need_resched(p);
834 static void set_curr_task_rt(struct rq *rq)
836 struct task_struct *p = rq->curr;
838 p->se.exec_start = rq->clock;
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 */
850 .check_preempt_curr = check_preempt_curr_rt,
852 .pick_next_task = pick_next_task_rt,
853 .put_prev_task = put_prev_task_rt,
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
861 .set_curr_task = set_curr_task_rt,
862 .task_tick = task_tick_rt,