2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
8 static inline int rt_overloaded(struct rq *rq)
10 return atomic_read(&rq->rd->rto_count);
13 static inline void rt_set_overload(struct rq *rq)
15 cpu_set(rq->cpu, rq->rd->rto_mask);
17 * Make sure the mask is visible before we set
18 * the overload count. That is checked to determine
19 * if we should look at the mask. It would be a shame
20 * if we looked at the mask, but the mask was not
24 atomic_inc(&rq->rd->rto_count);
27 static inline void rt_clear_overload(struct rq *rq)
29 /* the order here really doesn't matter */
30 atomic_dec(&rq->rd->rto_count);
31 cpu_clear(rq->cpu, rq->rd->rto_mask);
34 static void update_rt_migration(struct rq *rq)
36 if (rq->rt.rt_nr_migratory && (rq->rt.rt_nr_running > 1)) {
38 rq->rt.overloaded = 1;
40 rt_clear_overload(rq);
41 rq->rt.overloaded = 0;
44 #endif /* CONFIG_SMP */
47 * Update the current task's runtime statistics. Skip current tasks that
48 * are not in our scheduling class.
50 static void update_curr_rt(struct rq *rq)
52 struct task_struct *curr = rq->curr;
55 if (!task_has_rt_policy(curr))
58 delta_exec = rq->clock - curr->se.exec_start;
59 if (unlikely((s64)delta_exec < 0))
62 schedstat_set(curr->se.exec_max, max(curr->se.exec_max, delta_exec));
64 curr->se.sum_exec_runtime += delta_exec;
65 curr->se.exec_start = rq->clock;
66 cpuacct_charge(curr, delta_exec);
69 static inline void inc_rt_tasks(struct task_struct *p, struct rq *rq)
72 rq->rt.rt_nr_running++;
74 if (p->prio < rq->rt.highest_prio)
75 rq->rt.highest_prio = p->prio;
76 if (p->nr_cpus_allowed > 1)
77 rq->rt.rt_nr_migratory++;
79 update_rt_migration(rq);
80 #endif /* CONFIG_SMP */
83 static inline void dec_rt_tasks(struct task_struct *p, struct rq *rq)
86 WARN_ON(!rq->rt.rt_nr_running);
87 rq->rt.rt_nr_running--;
89 if (rq->rt.rt_nr_running) {
90 struct rt_prio_array *array;
92 WARN_ON(p->prio < rq->rt.highest_prio);
93 if (p->prio == rq->rt.highest_prio) {
95 array = &rq->rt.active;
97 sched_find_first_bit(array->bitmap);
98 } /* otherwise leave rq->highest prio alone */
100 rq->rt.highest_prio = MAX_RT_PRIO;
101 if (p->nr_cpus_allowed > 1)
102 rq->rt.rt_nr_migratory--;
104 update_rt_migration(rq);
105 #endif /* CONFIG_SMP */
108 static void enqueue_task_rt(struct rq *rq, struct task_struct *p, int wakeup)
110 struct rt_prio_array *array = &rq->rt.active;
112 list_add_tail(&p->run_list, array->queue + p->prio);
113 __set_bit(p->prio, array->bitmap);
114 inc_cpu_load(rq, p->se.load.weight);
120 * Adding/removing a task to/from a priority array:
122 static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int sleep)
124 struct rt_prio_array *array = &rq->rt.active;
128 list_del(&p->run_list);
129 if (list_empty(array->queue + p->prio))
130 __clear_bit(p->prio, array->bitmap);
131 dec_cpu_load(rq, p->se.load.weight);
137 * Put task to the end of the run list without the overhead of dequeue
138 * followed by enqueue.
140 static void requeue_task_rt(struct rq *rq, struct task_struct *p)
142 struct rt_prio_array *array = &rq->rt.active;
144 list_move_tail(&p->run_list, array->queue + p->prio);
148 yield_task_rt(struct rq *rq)
150 requeue_task_rt(rq, rq->curr);
154 static int find_lowest_rq(struct task_struct *task);
156 static int select_task_rq_rt(struct task_struct *p, int sync)
158 struct rq *rq = task_rq(p);
161 * If the current task is an RT task, then
162 * try to see if we can wake this RT task up on another
163 * runqueue. Otherwise simply start this RT task
164 * on its current runqueue.
166 * We want to avoid overloading runqueues. Even if
167 * the RT task is of higher priority than the current RT task.
168 * RT tasks behave differently than other tasks. If
169 * one gets preempted, we try to push it off to another queue.
170 * So trying to keep a preempting RT task on the same
171 * cache hot CPU will force the running RT task to
172 * a cold CPU. So we waste all the cache for the lower
173 * RT task in hopes of saving some of a RT task
174 * that is just being woken and probably will have
177 if (unlikely(rt_task(rq->curr)) &&
178 (p->nr_cpus_allowed > 1)) {
179 int cpu = find_lowest_rq(p);
181 return (cpu == -1) ? task_cpu(p) : cpu;
185 * Otherwise, just let it ride on the affined RQ and the
186 * post-schedule router will push the preempted task away
190 #endif /* CONFIG_SMP */
193 * Preempt the current task with a newly woken task if needed:
195 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p)
197 if (p->prio < rq->curr->prio)
198 resched_task(rq->curr);
201 static struct task_struct *pick_next_task_rt(struct rq *rq)
203 struct rt_prio_array *array = &rq->rt.active;
204 struct task_struct *next;
205 struct list_head *queue;
208 idx = sched_find_first_bit(array->bitmap);
209 if (idx >= MAX_RT_PRIO)
212 queue = array->queue + idx;
213 next = list_entry(queue->next, struct task_struct, run_list);
215 next->se.exec_start = rq->clock;
220 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
223 p->se.exec_start = 0;
227 /* Only try algorithms three times */
228 #define RT_MAX_TRIES 3
230 static int double_lock_balance(struct rq *this_rq, struct rq *busiest);
231 static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep);
233 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
235 if (!task_running(rq, p) &&
236 (cpu < 0 || cpu_isset(cpu, p->cpus_allowed)) &&
237 (p->nr_cpus_allowed > 1))
242 /* Return the second highest RT task, NULL otherwise */
243 static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu)
245 struct rt_prio_array *array = &rq->rt.active;
246 struct task_struct *next;
247 struct list_head *queue;
250 if (likely(rq->rt.rt_nr_running < 2))
253 idx = sched_find_first_bit(array->bitmap);
254 if (unlikely(idx >= MAX_RT_PRIO)) {
255 WARN_ON(1); /* rt_nr_running is bad */
259 queue = array->queue + idx;
260 BUG_ON(list_empty(queue));
262 next = list_entry(queue->next, struct task_struct, run_list);
263 if (unlikely(pick_rt_task(rq, next, cpu)))
266 if (queue->next->next != queue) {
268 next = list_entry(queue->next->next, struct task_struct,
270 if (pick_rt_task(rq, next, cpu))
275 /* slower, but more flexible */
276 idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1);
277 if (unlikely(idx >= MAX_RT_PRIO))
280 queue = array->queue + idx;
281 BUG_ON(list_empty(queue));
283 list_for_each_entry(next, queue, run_list) {
284 if (pick_rt_task(rq, next, cpu))
294 static DEFINE_PER_CPU(cpumask_t, local_cpu_mask);
296 static int find_lowest_cpus(struct task_struct *task, cpumask_t *lowest_mask)
298 int lowest_prio = -1;
303 cpus_and(*lowest_mask, task_rq(task)->rd->online, task->cpus_allowed);
306 * Scan each rq for the lowest prio.
308 for_each_cpu_mask(cpu, *lowest_mask) {
309 struct rq *rq = cpu_rq(cpu);
311 /* We look for lowest RT prio or non-rt CPU */
312 if (rq->rt.highest_prio >= MAX_RT_PRIO) {
314 * if we already found a low RT queue
315 * and now we found this non-rt queue
316 * clear the mask and set our bit.
317 * Otherwise just return the queue as is
318 * and the count==1 will cause the algorithm
319 * to use the first bit found.
321 if (lowest_cpu != -1) {
322 cpus_clear(*lowest_mask);
323 cpu_set(rq->cpu, *lowest_mask);
328 /* no locking for now */
329 if ((rq->rt.highest_prio > task->prio)
330 && (rq->rt.highest_prio >= lowest_prio)) {
331 if (rq->rt.highest_prio > lowest_prio) {
332 /* new low - clear old data */
333 lowest_prio = rq->rt.highest_prio;
339 cpu_clear(cpu, *lowest_mask);
343 * Clear out all the set bits that represent
344 * runqueues that were of higher prio than
347 if (lowest_cpu > 0) {
349 * Perhaps we could add another cpumask op to
350 * zero out bits. Like cpu_zero_bits(cpumask, nrbits);
351 * Then that could be optimized to use memset and such.
353 for_each_cpu_mask(cpu, *lowest_mask) {
354 if (cpu >= lowest_cpu)
356 cpu_clear(cpu, *lowest_mask);
363 static inline int pick_optimal_cpu(int this_cpu, cpumask_t *mask)
367 /* "this_cpu" is cheaper to preempt than a remote processor */
368 if ((this_cpu != -1) && cpu_isset(this_cpu, *mask))
371 first = first_cpu(*mask);
372 if (first != NR_CPUS)
378 static int find_lowest_rq(struct task_struct *task)
380 struct sched_domain *sd;
381 cpumask_t *lowest_mask = &__get_cpu_var(local_cpu_mask);
382 int this_cpu = smp_processor_id();
383 int cpu = task_cpu(task);
384 int count = find_lowest_cpus(task, lowest_mask);
387 return -1; /* No targets found */
390 * There is no sense in performing an optimal search if only one
394 return first_cpu(*lowest_mask);
397 * At this point we have built a mask of cpus representing the
398 * lowest priority tasks in the system. Now we want to elect
399 * the best one based on our affinity and topology.
401 * We prioritize the last cpu that the task executed on since
402 * it is most likely cache-hot in that location.
404 if (cpu_isset(cpu, *lowest_mask))
408 * Otherwise, we consult the sched_domains span maps to figure
409 * out which cpu is logically closest to our hot cache data.
412 this_cpu = -1; /* Skip this_cpu opt if the same */
414 for_each_domain(cpu, sd) {
415 if (sd->flags & SD_WAKE_AFFINE) {
416 cpumask_t domain_mask;
419 cpus_and(domain_mask, sd->span, *lowest_mask);
421 best_cpu = pick_optimal_cpu(this_cpu,
429 * And finally, if there were no matches within the domains
430 * just give the caller *something* to work with from the compatible
433 return pick_optimal_cpu(this_cpu, lowest_mask);
436 /* Will lock the rq it finds */
437 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
439 struct rq *lowest_rq = NULL;
443 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
444 cpu = find_lowest_rq(task);
446 if ((cpu == -1) || (cpu == rq->cpu))
449 lowest_rq = cpu_rq(cpu);
451 /* if the prio of this runqueue changed, try again */
452 if (double_lock_balance(rq, lowest_rq)) {
454 * We had to unlock the run queue. In
455 * the mean time, task could have
456 * migrated already or had its affinity changed.
457 * Also make sure that it wasn't scheduled on its rq.
459 if (unlikely(task_rq(task) != rq ||
460 !cpu_isset(lowest_rq->cpu,
461 task->cpus_allowed) ||
462 task_running(rq, task) ||
465 spin_unlock(&lowest_rq->lock);
471 /* If this rq is still suitable use it. */
472 if (lowest_rq->rt.highest_prio > task->prio)
476 spin_unlock(&lowest_rq->lock);
484 * If the current CPU has more than one RT task, see if the non
485 * running task can migrate over to a CPU that is running a task
486 * of lesser priority.
488 static int push_rt_task(struct rq *rq)
490 struct task_struct *next_task;
491 struct rq *lowest_rq;
493 int paranoid = RT_MAX_TRIES;
495 if (!rq->rt.overloaded)
498 next_task = pick_next_highest_task_rt(rq, -1);
503 if (unlikely(next_task == rq->curr)) {
509 * It's possible that the next_task slipped in of
510 * higher priority than current. If that's the case
511 * just reschedule current.
513 if (unlikely(next_task->prio < rq->curr->prio)) {
514 resched_task(rq->curr);
518 /* We might release rq lock */
519 get_task_struct(next_task);
521 /* find_lock_lowest_rq locks the rq if found */
522 lowest_rq = find_lock_lowest_rq(next_task, rq);
524 struct task_struct *task;
526 * find lock_lowest_rq releases rq->lock
527 * so it is possible that next_task has changed.
528 * If it has, then try again.
530 task = pick_next_highest_task_rt(rq, -1);
531 if (unlikely(task != next_task) && task && paranoid--) {
532 put_task_struct(next_task);
539 deactivate_task(rq, next_task, 0);
540 set_task_cpu(next_task, lowest_rq->cpu);
541 activate_task(lowest_rq, next_task, 0);
543 resched_task(lowest_rq->curr);
545 spin_unlock(&lowest_rq->lock);
549 put_task_struct(next_task);
555 * TODO: Currently we just use the second highest prio task on
556 * the queue, and stop when it can't migrate (or there's
557 * no more RT tasks). There may be a case where a lower
558 * priority RT task has a different affinity than the
559 * higher RT task. In this case the lower RT task could
560 * possibly be able to migrate where as the higher priority
561 * RT task could not. We currently ignore this issue.
562 * Enhancements are welcome!
564 static void push_rt_tasks(struct rq *rq)
566 /* push_rt_task will return true if it moved an RT */
567 while (push_rt_task(rq))
571 static int pull_rt_task(struct rq *this_rq)
573 int this_cpu = this_rq->cpu, ret = 0, cpu;
574 struct task_struct *p, *next;
577 if (likely(!rt_overloaded(this_rq)))
580 next = pick_next_task_rt(this_rq);
582 for_each_cpu_mask(cpu, this_rq->rd->rto_mask) {
586 src_rq = cpu_rq(cpu);
587 if (unlikely(src_rq->rt.rt_nr_running <= 1)) {
589 * It is possible that overlapping cpusets
590 * will miss clearing a non overloaded runqueue.
593 if (double_lock_balance(this_rq, src_rq)) {
594 /* unlocked our runqueue lock */
595 struct task_struct *old_next = next;
597 next = pick_next_task_rt(this_rq);
598 if (next != old_next)
601 if (likely(src_rq->rt.rt_nr_running <= 1)) {
603 * Small chance that this_rq->curr changed
604 * but it's really harmless here.
606 rt_clear_overload(this_rq);
609 * Heh, the src_rq is now overloaded, since
610 * we already have the src_rq lock, go straight
611 * to pulling tasks from it.
615 spin_unlock(&src_rq->lock);
620 * We can potentially drop this_rq's lock in
621 * double_lock_balance, and another CPU could
622 * steal our next task - hence we must cause
623 * the caller to recalculate the next task
626 if (double_lock_balance(this_rq, src_rq)) {
627 struct task_struct *old_next = next;
629 next = pick_next_task_rt(this_rq);
630 if (next != old_next)
635 * Are there still pullable RT tasks?
637 if (src_rq->rt.rt_nr_running <= 1) {
638 spin_unlock(&src_rq->lock);
643 p = pick_next_highest_task_rt(src_rq, this_cpu);
646 * Do we have an RT task that preempts
647 * the to-be-scheduled task?
649 if (p && (!next || (p->prio < next->prio))) {
650 WARN_ON(p == src_rq->curr);
651 WARN_ON(!p->se.on_rq);
654 * There's a chance that p is higher in priority
655 * than what's currently running on its cpu.
656 * This is just that p is wakeing up and hasn't
657 * had a chance to schedule. We only pull
658 * p if it is lower in priority than the
659 * current task on the run queue or
660 * this_rq next task is lower in prio than
661 * the current task on that rq.
663 if (p->prio < src_rq->curr->prio ||
664 (next && next->prio < src_rq->curr->prio))
669 deactivate_task(src_rq, p, 0);
670 set_task_cpu(p, this_cpu);
671 activate_task(this_rq, p, 0);
673 * We continue with the search, just in
674 * case there's an even higher prio task
675 * in another runqueue. (low likelyhood
678 * Update next so that we won't pick a task
679 * on another cpu with a priority lower (or equal)
680 * than the one we just picked.
686 spin_unlock(&src_rq->lock);
692 static void schedule_balance_rt(struct rq *rq, struct task_struct *prev)
694 /* Try to pull RT tasks here if we lower this rq's prio */
695 if (unlikely(rt_task(prev)) && rq->rt.highest_prio > prev->prio)
699 static void schedule_tail_balance_rt(struct rq *rq)
702 * If we have more than one rt_task queued, then
703 * see if we can push the other rt_tasks off to other CPUS.
704 * Note we may release the rq lock, and since
705 * the lock was owned by prev, we need to release it
706 * first via finish_lock_switch and then reaquire it here.
708 if (unlikely(rq->rt.overloaded)) {
709 spin_lock_irq(&rq->lock);
711 spin_unlock_irq(&rq->lock);
716 static void wakeup_balance_rt(struct rq *rq, struct task_struct *p)
718 if (unlikely(rt_task(p)) &&
719 !task_running(rq, p) &&
720 (p->prio >= rq->rt.highest_prio) &&
726 load_balance_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
727 unsigned long max_load_move,
728 struct sched_domain *sd, enum cpu_idle_type idle,
729 int *all_pinned, int *this_best_prio)
731 /* don't touch RT tasks */
736 move_one_task_rt(struct rq *this_rq, int this_cpu, struct rq *busiest,
737 struct sched_domain *sd, enum cpu_idle_type idle)
739 /* don't touch RT tasks */
743 static void set_cpus_allowed_rt(struct task_struct *p, cpumask_t *new_mask)
745 int weight = cpus_weight(*new_mask);
750 * Update the migration status of the RQ if we have an RT task
751 * which is running AND changing its weight value.
753 if (p->se.on_rq && (weight != p->nr_cpus_allowed)) {
754 struct rq *rq = task_rq(p);
756 if ((p->nr_cpus_allowed <= 1) && (weight > 1)) {
757 rq->rt.rt_nr_migratory++;
758 } else if ((p->nr_cpus_allowed > 1) && (weight <= 1)) {
759 BUG_ON(!rq->rt.rt_nr_migratory);
760 rq->rt.rt_nr_migratory--;
763 update_rt_migration(rq);
766 p->cpus_allowed = *new_mask;
767 p->nr_cpus_allowed = weight;
770 /* Assumes rq->lock is held */
771 static void join_domain_rt(struct rq *rq)
773 if (rq->rt.overloaded)
777 /* Assumes rq->lock is held */
778 static void leave_domain_rt(struct rq *rq)
780 if (rq->rt.overloaded)
781 rt_clear_overload(rq);
784 #else /* CONFIG_SMP */
785 # define schedule_tail_balance_rt(rq) do { } while (0)
786 # define schedule_balance_rt(rq, prev) do { } while (0)
787 # define wakeup_balance_rt(rq, p) do { } while (0)
788 #endif /* CONFIG_SMP */
790 static void task_tick_rt(struct rq *rq, struct task_struct *p)
795 * RR tasks need a special form of timeslice management.
796 * FIFO tasks have no timeslices.
798 if (p->policy != SCHED_RR)
804 p->time_slice = DEF_TIMESLICE;
807 * Requeue to the end of queue if we are not the only element
810 if (p->run_list.prev != p->run_list.next) {
811 requeue_task_rt(rq, p);
812 set_tsk_need_resched(p);
816 static void set_curr_task_rt(struct rq *rq)
818 struct task_struct *p = rq->curr;
820 p->se.exec_start = rq->clock;
823 const struct sched_class rt_sched_class = {
824 .next = &fair_sched_class,
825 .enqueue_task = enqueue_task_rt,
826 .dequeue_task = dequeue_task_rt,
827 .yield_task = yield_task_rt,
829 .select_task_rq = select_task_rq_rt,
830 #endif /* CONFIG_SMP */
832 .check_preempt_curr = check_preempt_curr_rt,
834 .pick_next_task = pick_next_task_rt,
835 .put_prev_task = put_prev_task_rt,
838 .load_balance = load_balance_rt,
839 .move_one_task = move_one_task_rt,
840 .set_cpus_allowed = set_cpus_allowed_rt,
841 .join_domain = join_domain_rt,
842 .leave_domain = leave_domain_rt,
845 .set_curr_task = set_curr_task_rt,
846 .task_tick = task_tick_rt,