dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / kernel / sched / rt.c
blob801b4ec407023b8fe5e477b4854d1be54149186c
1 /*
2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
3 * policies)
4 */
6 #include "sched.h"
8 #include <linux/slab.h>
9 #include <linux/irq_work.h>
11 int sched_rr_timeslice = RR_TIMESLICE;
13 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
15 struct rt_bandwidth def_rt_bandwidth;
17 static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
19 struct rt_bandwidth *rt_b =
20 container_of(timer, struct rt_bandwidth, rt_period_timer);
21 int idle = 0;
22 int overrun;
24 raw_spin_lock(&rt_b->rt_runtime_lock);
25 for (;;) {
26 overrun = hrtimer_forward_now(timer, rt_b->rt_period);
27 if (!overrun)
28 break;
30 raw_spin_unlock(&rt_b->rt_runtime_lock);
31 idle = do_sched_rt_period_timer(rt_b, overrun);
32 raw_spin_lock(&rt_b->rt_runtime_lock);
34 if (idle)
35 rt_b->rt_period_active = 0;
36 raw_spin_unlock(&rt_b->rt_runtime_lock);
38 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
41 void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
43 rt_b->rt_period = ns_to_ktime(period);
44 rt_b->rt_runtime = runtime;
46 raw_spin_lock_init(&rt_b->rt_runtime_lock);
48 hrtimer_init(&rt_b->rt_period_timer,
49 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
50 rt_b->rt_period_timer.function = sched_rt_period_timer;
53 static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
55 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
56 return;
58 raw_spin_lock(&rt_b->rt_runtime_lock);
59 if (!rt_b->rt_period_active) {
60 rt_b->rt_period_active = 1;
61 hrtimer_forward_now(&rt_b->rt_period_timer, rt_b->rt_period);
62 hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED);
64 raw_spin_unlock(&rt_b->rt_runtime_lock);
67 void init_rt_rq(struct rt_rq *rt_rq)
69 struct rt_prio_array *array;
70 int i;
72 array = &rt_rq->active;
73 for (i = 0; i < MAX_RT_PRIO; i++) {
74 INIT_LIST_HEAD(array->queue + i);
75 __clear_bit(i, array->bitmap);
77 /* delimiter for bitsearch: */
78 __set_bit(MAX_RT_PRIO, array->bitmap);
80 #if defined CONFIG_SMP
81 rt_rq->highest_prio.curr = MAX_RT_PRIO;
82 rt_rq->highest_prio.next = MAX_RT_PRIO;
83 rt_rq->rt_nr_migratory = 0;
84 rt_rq->overloaded = 0;
85 plist_head_init(&rt_rq->pushable_tasks);
86 #endif /* CONFIG_SMP */
87 /* We start is dequeued state, because no RT tasks are queued */
88 rt_rq->rt_queued = 0;
90 rt_rq->rt_time = 0;
91 rt_rq->rt_throttled = 0;
92 rt_rq->rt_runtime = 0;
93 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
96 #ifdef CONFIG_RT_GROUP_SCHED
97 static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
99 hrtimer_cancel(&rt_b->rt_period_timer);
102 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
104 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
106 #ifdef CONFIG_SCHED_DEBUG
107 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
108 #endif
109 return container_of(rt_se, struct task_struct, rt);
112 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
114 return rt_rq->rq;
117 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
119 return rt_se->rt_rq;
122 static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
124 struct rt_rq *rt_rq = rt_se->rt_rq;
126 return rt_rq->rq;
129 void free_rt_sched_group(struct task_group *tg)
131 int i;
133 if (tg->rt_se)
134 destroy_rt_bandwidth(&tg->rt_bandwidth);
136 for_each_possible_cpu(i) {
137 if (tg->rt_rq)
138 kfree(tg->rt_rq[i]);
139 if (tg->rt_se)
140 kfree(tg->rt_se[i]);
143 kfree(tg->rt_rq);
144 kfree(tg->rt_se);
147 void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
148 struct sched_rt_entity *rt_se, int cpu,
149 struct sched_rt_entity *parent)
151 struct rq *rq = cpu_rq(cpu);
153 rt_rq->highest_prio.curr = MAX_RT_PRIO;
154 rt_rq->rt_nr_boosted = 0;
155 rt_rq->rq = rq;
156 rt_rq->tg = tg;
158 tg->rt_rq[cpu] = rt_rq;
159 tg->rt_se[cpu] = rt_se;
161 if (!rt_se)
162 return;
164 if (!parent)
165 rt_se->rt_rq = &rq->rt;
166 else
167 rt_se->rt_rq = parent->my_q;
169 rt_se->my_q = rt_rq;
170 rt_se->parent = parent;
171 INIT_LIST_HEAD(&rt_se->run_list);
174 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
176 struct rt_rq *rt_rq;
177 struct sched_rt_entity *rt_se;
178 int i;
180 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
181 if (!tg->rt_rq)
182 goto err;
183 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
184 if (!tg->rt_se)
185 goto err;
187 init_rt_bandwidth(&tg->rt_bandwidth,
188 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
190 for_each_possible_cpu(i) {
191 rt_rq = kzalloc_node(sizeof(struct rt_rq),
192 GFP_KERNEL, cpu_to_node(i));
193 if (!rt_rq)
194 goto err;
196 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
197 GFP_KERNEL, cpu_to_node(i));
198 if (!rt_se)
199 goto err_free_rq;
201 init_rt_rq(rt_rq);
202 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
203 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
206 return 1;
208 err_free_rq:
209 kfree(rt_rq);
210 err:
211 return 0;
214 #else /* CONFIG_RT_GROUP_SCHED */
216 #define rt_entity_is_task(rt_se) (1)
218 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
220 return container_of(rt_se, struct task_struct, rt);
223 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
225 return container_of(rt_rq, struct rq, rt);
228 static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
230 struct task_struct *p = rt_task_of(rt_se);
232 return task_rq(p);
235 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
237 struct rq *rq = rq_of_rt_se(rt_se);
239 return &rq->rt;
242 void free_rt_sched_group(struct task_group *tg) { }
244 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
246 return 1;
248 #endif /* CONFIG_RT_GROUP_SCHED */
250 #ifdef CONFIG_SMP
252 static void pull_rt_task(struct rq *this_rq);
254 static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
256 /* Try to pull RT tasks here if we lower this rq's prio */
257 return rq->rt.highest_prio.curr > prev->prio;
260 static inline int rt_overloaded(struct rq *rq)
262 return atomic_read(&rq->rd->rto_count);
265 static inline void rt_set_overload(struct rq *rq)
267 if (!rq->online)
268 return;
270 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
272 * Make sure the mask is visible before we set
273 * the overload count. That is checked to determine
274 * if we should look at the mask. It would be a shame
275 * if we looked at the mask, but the mask was not
276 * updated yet.
278 * Matched by the barrier in pull_rt_task().
280 smp_wmb();
281 atomic_inc(&rq->rd->rto_count);
284 static inline void rt_clear_overload(struct rq *rq)
286 if (!rq->online)
287 return;
289 /* the order here really doesn't matter */
290 atomic_dec(&rq->rd->rto_count);
291 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
294 static void update_rt_migration(struct rt_rq *rt_rq)
296 if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
297 if (!rt_rq->overloaded) {
298 rt_set_overload(rq_of_rt_rq(rt_rq));
299 rt_rq->overloaded = 1;
301 } else if (rt_rq->overloaded) {
302 rt_clear_overload(rq_of_rt_rq(rt_rq));
303 rt_rq->overloaded = 0;
307 static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
309 struct task_struct *p;
311 if (!rt_entity_is_task(rt_se))
312 return;
314 p = rt_task_of(rt_se);
315 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
317 rt_rq->rt_nr_total++;
318 if (p->nr_cpus_allowed > 1)
319 rt_rq->rt_nr_migratory++;
321 update_rt_migration(rt_rq);
324 static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
326 struct task_struct *p;
328 if (!rt_entity_is_task(rt_se))
329 return;
331 p = rt_task_of(rt_se);
332 rt_rq = &rq_of_rt_rq(rt_rq)->rt;
334 rt_rq->rt_nr_total--;
335 if (p->nr_cpus_allowed > 1)
336 rt_rq->rt_nr_migratory--;
338 update_rt_migration(rt_rq);
341 static inline int has_pushable_tasks(struct rq *rq)
343 return !plist_head_empty(&rq->rt.pushable_tasks);
346 static DEFINE_PER_CPU(struct callback_head, rt_push_head);
347 static DEFINE_PER_CPU(struct callback_head, rt_pull_head);
349 static void push_rt_tasks(struct rq *);
350 static void pull_rt_task(struct rq *);
352 static inline void queue_push_tasks(struct rq *rq)
354 if (!has_pushable_tasks(rq))
355 return;
357 queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
360 static inline void queue_pull_task(struct rq *rq)
362 queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
365 static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
367 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
368 plist_node_init(&p->pushable_tasks, p->prio);
369 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
371 /* Update the highest prio pushable task */
372 if (p->prio < rq->rt.highest_prio.next)
373 rq->rt.highest_prio.next = p->prio;
376 static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
378 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
380 /* Update the new highest prio pushable task */
381 if (has_pushable_tasks(rq)) {
382 p = plist_first_entry(&rq->rt.pushable_tasks,
383 struct task_struct, pushable_tasks);
384 rq->rt.highest_prio.next = p->prio;
385 } else
386 rq->rt.highest_prio.next = MAX_RT_PRIO;
389 #else
391 static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
395 static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
399 static inline
400 void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
404 static inline
405 void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
409 static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
411 return false;
414 static inline void pull_rt_task(struct rq *this_rq)
418 static inline void queue_push_tasks(struct rq *rq)
421 #endif /* CONFIG_SMP */
423 static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
424 static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
426 static inline int on_rt_rq(struct sched_rt_entity *rt_se)
428 return !list_empty(&rt_se->run_list);
431 #ifdef CONFIG_RT_GROUP_SCHED
433 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
435 if (!rt_rq->tg)
436 return RUNTIME_INF;
438 return rt_rq->rt_runtime;
441 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
443 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
446 typedef struct task_group *rt_rq_iter_t;
448 static inline struct task_group *next_task_group(struct task_group *tg)
450 do {
451 tg = list_entry_rcu(tg->list.next,
452 typeof(struct task_group), list);
453 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
455 if (&tg->list == &task_groups)
456 tg = NULL;
458 return tg;
461 #define for_each_rt_rq(rt_rq, iter, rq) \
462 for (iter = container_of(&task_groups, typeof(*iter), list); \
463 (iter = next_task_group(iter)) && \
464 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
466 #define for_each_sched_rt_entity(rt_se) \
467 for (; rt_se; rt_se = rt_se->parent)
469 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
471 return rt_se->my_q;
474 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head);
475 static void dequeue_rt_entity(struct sched_rt_entity *rt_se);
477 static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
479 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
480 struct rq *rq = rq_of_rt_rq(rt_rq);
481 struct sched_rt_entity *rt_se;
483 int cpu = cpu_of(rq);
485 rt_se = rt_rq->tg->rt_se[cpu];
487 if (rt_rq->rt_nr_running) {
488 if (!rt_se)
489 enqueue_top_rt_rq(rt_rq);
490 else if (!on_rt_rq(rt_se))
491 enqueue_rt_entity(rt_se, false);
493 if (rt_rq->highest_prio.curr < curr->prio)
494 resched_curr(rq);
498 static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
500 struct sched_rt_entity *rt_se;
501 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
503 rt_se = rt_rq->tg->rt_se[cpu];
505 if (!rt_se)
506 dequeue_top_rt_rq(rt_rq);
507 else if (on_rt_rq(rt_se))
508 dequeue_rt_entity(rt_se);
511 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
513 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
516 static int rt_se_boosted(struct sched_rt_entity *rt_se)
518 struct rt_rq *rt_rq = group_rt_rq(rt_se);
519 struct task_struct *p;
521 if (rt_rq)
522 return !!rt_rq->rt_nr_boosted;
524 p = rt_task_of(rt_se);
525 return p->prio != p->normal_prio;
528 #ifdef CONFIG_SMP
529 static inline const struct cpumask *sched_rt_period_mask(void)
531 return this_rq()->rd->span;
533 #else
534 static inline const struct cpumask *sched_rt_period_mask(void)
536 return cpu_online_mask;
538 #endif
540 static inline
541 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
543 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
546 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
548 return &rt_rq->tg->rt_bandwidth;
551 #else /* !CONFIG_RT_GROUP_SCHED */
553 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
555 return rt_rq->rt_runtime;
558 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
560 return ktime_to_ns(def_rt_bandwidth.rt_period);
563 typedef struct rt_rq *rt_rq_iter_t;
565 #define for_each_rt_rq(rt_rq, iter, rq) \
566 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
568 #define for_each_sched_rt_entity(rt_se) \
569 for (; rt_se; rt_se = NULL)
571 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
573 return NULL;
576 static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
578 struct rq *rq = rq_of_rt_rq(rt_rq);
580 if (!rt_rq->rt_nr_running)
581 return;
583 enqueue_top_rt_rq(rt_rq);
584 resched_curr(rq);
587 static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
589 dequeue_top_rt_rq(rt_rq);
592 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
594 return rt_rq->rt_throttled;
597 static inline const struct cpumask *sched_rt_period_mask(void)
599 return cpu_online_mask;
602 static inline
603 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
605 return &cpu_rq(cpu)->rt;
608 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
610 return &def_rt_bandwidth;
613 #endif /* CONFIG_RT_GROUP_SCHED */
615 bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
617 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
619 return (hrtimer_active(&rt_b->rt_period_timer) ||
620 rt_rq->rt_time < rt_b->rt_runtime);
623 #ifdef CONFIG_SMP
625 * We ran out of runtime, see if we can borrow some from our neighbours.
627 static void do_balance_runtime(struct rt_rq *rt_rq)
629 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
630 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
631 int i, weight;
632 u64 rt_period;
634 weight = cpumask_weight(rd->span);
636 raw_spin_lock(&rt_b->rt_runtime_lock);
637 rt_period = ktime_to_ns(rt_b->rt_period);
638 for_each_cpu(i, rd->span) {
639 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
640 s64 diff;
642 if (iter == rt_rq)
643 continue;
645 raw_spin_lock(&iter->rt_runtime_lock);
647 * Either all rqs have inf runtime and there's nothing to steal
648 * or __disable_runtime() below sets a specific rq to inf to
649 * indicate its been disabled and disalow stealing.
651 if (iter->rt_runtime == RUNTIME_INF)
652 goto next;
655 * From runqueues with spare time, take 1/n part of their
656 * spare time, but no more than our period.
658 diff = iter->rt_runtime - iter->rt_time;
659 if (diff > 0) {
660 diff = div_u64((u64)diff, weight);
661 if (rt_rq->rt_runtime + diff > rt_period)
662 diff = rt_period - rt_rq->rt_runtime;
663 iter->rt_runtime -= diff;
664 rt_rq->rt_runtime += diff;
665 if (rt_rq->rt_runtime == rt_period) {
666 raw_spin_unlock(&iter->rt_runtime_lock);
667 break;
670 next:
671 raw_spin_unlock(&iter->rt_runtime_lock);
673 raw_spin_unlock(&rt_b->rt_runtime_lock);
677 * Ensure this RQ takes back all the runtime it lend to its neighbours.
679 static void __disable_runtime(struct rq *rq)
681 struct root_domain *rd = rq->rd;
682 rt_rq_iter_t iter;
683 struct rt_rq *rt_rq;
685 if (unlikely(!scheduler_running))
686 return;
688 for_each_rt_rq(rt_rq, iter, rq) {
689 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
690 s64 want;
691 int i;
693 raw_spin_lock(&rt_b->rt_runtime_lock);
694 raw_spin_lock(&rt_rq->rt_runtime_lock);
696 * Either we're all inf and nobody needs to borrow, or we're
697 * already disabled and thus have nothing to do, or we have
698 * exactly the right amount of runtime to take out.
700 if (rt_rq->rt_runtime == RUNTIME_INF ||
701 rt_rq->rt_runtime == rt_b->rt_runtime)
702 goto balanced;
703 raw_spin_unlock(&rt_rq->rt_runtime_lock);
706 * Calculate the difference between what we started out with
707 * and what we current have, that's the amount of runtime
708 * we lend and now have to reclaim.
710 want = rt_b->rt_runtime - rt_rq->rt_runtime;
713 * Greedy reclaim, take back as much as we can.
715 for_each_cpu(i, rd->span) {
716 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
717 s64 diff;
720 * Can't reclaim from ourselves or disabled runqueues.
722 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
723 continue;
725 raw_spin_lock(&iter->rt_runtime_lock);
726 if (want > 0) {
727 diff = min_t(s64, iter->rt_runtime, want);
728 iter->rt_runtime -= diff;
729 want -= diff;
730 } else {
731 iter->rt_runtime -= want;
732 want -= want;
734 raw_spin_unlock(&iter->rt_runtime_lock);
736 if (!want)
737 break;
740 raw_spin_lock(&rt_rq->rt_runtime_lock);
742 * We cannot be left wanting - that would mean some runtime
743 * leaked out of the system.
745 BUG_ON(want);
746 balanced:
748 * Disable all the borrow logic by pretending we have inf
749 * runtime - in which case borrowing doesn't make sense.
751 rt_rq->rt_runtime = RUNTIME_INF;
752 rt_rq->rt_throttled = 0;
753 raw_spin_unlock(&rt_rq->rt_runtime_lock);
754 raw_spin_unlock(&rt_b->rt_runtime_lock);
756 /* Make rt_rq available for pick_next_task() */
757 sched_rt_rq_enqueue(rt_rq);
761 static void __enable_runtime(struct rq *rq)
763 rt_rq_iter_t iter;
764 struct rt_rq *rt_rq;
766 if (unlikely(!scheduler_running))
767 return;
770 * Reset each runqueue's bandwidth settings
772 for_each_rt_rq(rt_rq, iter, rq) {
773 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
775 raw_spin_lock(&rt_b->rt_runtime_lock);
776 raw_spin_lock(&rt_rq->rt_runtime_lock);
777 rt_rq->rt_runtime = rt_b->rt_runtime;
778 rt_rq->rt_time = 0;
779 rt_rq->rt_throttled = 0;
780 raw_spin_unlock(&rt_rq->rt_runtime_lock);
781 raw_spin_unlock(&rt_b->rt_runtime_lock);
785 static void balance_runtime(struct rt_rq *rt_rq)
787 if (!sched_feat(RT_RUNTIME_SHARE))
788 return;
790 if (rt_rq->rt_time > rt_rq->rt_runtime) {
791 raw_spin_unlock(&rt_rq->rt_runtime_lock);
792 do_balance_runtime(rt_rq);
793 raw_spin_lock(&rt_rq->rt_runtime_lock);
796 #else /* !CONFIG_SMP */
797 static inline void balance_runtime(struct rt_rq *rt_rq) {}
798 #endif /* CONFIG_SMP */
800 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
802 int i, idle = 1, throttled = 0;
803 const struct cpumask *span;
805 span = sched_rt_period_mask();
806 #ifdef CONFIG_RT_GROUP_SCHED
808 * FIXME: isolated CPUs should really leave the root task group,
809 * whether they are isolcpus or were isolated via cpusets, lest
810 * the timer run on a CPU which does not service all runqueues,
811 * potentially leaving other CPUs indefinitely throttled. If
812 * isolation is really required, the user will turn the throttle
813 * off to kill the perturbations it causes anyway. Meanwhile,
814 * this maintains functionality for boot and/or troubleshooting.
816 if (rt_b == &root_task_group.rt_bandwidth)
817 span = cpu_online_mask;
818 #endif
819 for_each_cpu(i, span) {
820 int enqueue = 0;
821 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
822 struct rq *rq = rq_of_rt_rq(rt_rq);
824 raw_spin_lock(&rq->lock);
825 update_rq_clock(rq);
827 if (rt_rq->rt_time) {
828 u64 runtime;
830 raw_spin_lock(&rt_rq->rt_runtime_lock);
831 if (rt_rq->rt_throttled)
832 balance_runtime(rt_rq);
833 runtime = rt_rq->rt_runtime;
834 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
835 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
836 rt_rq->rt_throttled = 0;
837 enqueue = 1;
840 * When we're idle and a woken (rt) task is
841 * throttled check_preempt_curr() will set
842 * skip_update and the time between the wakeup
843 * and this unthrottle will get accounted as
844 * 'runtime'.
846 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
847 rq_clock_skip_update(rq, false);
849 if (rt_rq->rt_time || rt_rq->rt_nr_running)
850 idle = 0;
851 raw_spin_unlock(&rt_rq->rt_runtime_lock);
852 } else if (rt_rq->rt_nr_running) {
853 idle = 0;
854 if (!rt_rq_throttled(rt_rq))
855 enqueue = 1;
857 if (rt_rq->rt_throttled)
858 throttled = 1;
860 if (enqueue)
861 sched_rt_rq_enqueue(rt_rq);
862 raw_spin_unlock(&rq->lock);
865 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
866 return 1;
868 return idle;
871 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
873 #ifdef CONFIG_RT_GROUP_SCHED
874 struct rt_rq *rt_rq = group_rt_rq(rt_se);
876 if (rt_rq)
877 return rt_rq->highest_prio.curr;
878 #endif
880 return rt_task_of(rt_se)->prio;
883 static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
885 u64 runtime = sched_rt_runtime(rt_rq);
887 if (rt_rq->rt_throttled)
888 return rt_rq_throttled(rt_rq);
890 if (runtime >= sched_rt_period(rt_rq))
891 return 0;
893 balance_runtime(rt_rq);
894 runtime = sched_rt_runtime(rt_rq);
895 if (runtime == RUNTIME_INF)
896 return 0;
898 if (rt_rq->rt_time > runtime) {
899 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
902 * Don't actually throttle groups that have no runtime assigned
903 * but accrue some time due to boosting.
905 if (likely(rt_b->rt_runtime)) {
906 rt_rq->rt_throttled = 1;
907 printk_deferred_once("sched: RT throttling activated\n");
908 } else {
910 * In case we did anyway, make it go away,
911 * replenishment is a joke, since it will replenish us
912 * with exactly 0 ns.
914 rt_rq->rt_time = 0;
917 if (rt_rq_throttled(rt_rq)) {
918 sched_rt_rq_dequeue(rt_rq);
919 return 1;
923 return 0;
927 * Update the current task's runtime statistics. Skip current tasks that
928 * are not in our scheduling class.
930 static void update_curr_rt(struct rq *rq)
932 struct task_struct *curr = rq->curr;
933 struct sched_rt_entity *rt_se = &curr->rt;
934 u64 delta_exec;
936 if (curr->sched_class != &rt_sched_class)
937 return;
939 delta_exec = rq_clock_task(rq) - curr->se.exec_start;
940 if (unlikely((s64)delta_exec <= 0))
941 return;
943 schedstat_set(curr->se.statistics.exec_max,
944 max(curr->se.statistics.exec_max, delta_exec));
946 curr->se.sum_exec_runtime += delta_exec;
947 account_group_exec_runtime(curr, delta_exec);
949 curr->se.exec_start = rq_clock_task(rq);
950 cpuacct_charge(curr, delta_exec);
952 sched_rt_avg_update(rq, delta_exec);
954 if (!rt_bandwidth_enabled())
955 return;
957 for_each_sched_rt_entity(rt_se) {
958 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
960 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
961 raw_spin_lock(&rt_rq->rt_runtime_lock);
962 rt_rq->rt_time += delta_exec;
963 if (sched_rt_runtime_exceeded(rt_rq))
964 resched_curr(rq);
965 raw_spin_unlock(&rt_rq->rt_runtime_lock);
970 static void
971 dequeue_top_rt_rq(struct rt_rq *rt_rq)
973 struct rq *rq = rq_of_rt_rq(rt_rq);
975 BUG_ON(&rq->rt != rt_rq);
977 if (!rt_rq->rt_queued)
978 return;
980 BUG_ON(!rq->nr_running);
982 sub_nr_running(rq, rt_rq->rt_nr_running);
983 rt_rq->rt_queued = 0;
986 static void
987 enqueue_top_rt_rq(struct rt_rq *rt_rq)
989 struct rq *rq = rq_of_rt_rq(rt_rq);
991 BUG_ON(&rq->rt != rt_rq);
993 if (rt_rq->rt_queued)
994 return;
995 if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
996 return;
998 add_nr_running(rq, rt_rq->rt_nr_running);
999 rt_rq->rt_queued = 1;
1002 #if defined CONFIG_SMP
1004 static void
1005 inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1007 struct rq *rq = rq_of_rt_rq(rt_rq);
1009 #ifdef CONFIG_RT_GROUP_SCHED
1011 * Change rq's cpupri only if rt_rq is the top queue.
1013 if (&rq->rt != rt_rq)
1014 return;
1015 #endif
1016 if (rq->online && prio < prev_prio)
1017 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
1020 static void
1021 dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1023 struct rq *rq = rq_of_rt_rq(rt_rq);
1025 #ifdef CONFIG_RT_GROUP_SCHED
1027 * Change rq's cpupri only if rt_rq is the top queue.
1029 if (&rq->rt != rt_rq)
1030 return;
1031 #endif
1032 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1033 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1036 #else /* CONFIG_SMP */
1038 static inline
1039 void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1040 static inline
1041 void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1043 #endif /* CONFIG_SMP */
1045 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1046 static void
1047 inc_rt_prio(struct rt_rq *rt_rq, int prio)
1049 int prev_prio = rt_rq->highest_prio.curr;
1051 if (prio < prev_prio)
1052 rt_rq->highest_prio.curr = prio;
1054 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1057 static void
1058 dec_rt_prio(struct rt_rq *rt_rq, int prio)
1060 int prev_prio = rt_rq->highest_prio.curr;
1062 if (rt_rq->rt_nr_running) {
1064 WARN_ON(prio < prev_prio);
1067 * This may have been our highest task, and therefore
1068 * we may have some recomputation to do
1070 if (prio == prev_prio) {
1071 struct rt_prio_array *array = &rt_rq->active;
1073 rt_rq->highest_prio.curr =
1074 sched_find_first_bit(array->bitmap);
1077 } else
1078 rt_rq->highest_prio.curr = MAX_RT_PRIO;
1080 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1083 #else
1085 static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1086 static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1088 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1090 #ifdef CONFIG_RT_GROUP_SCHED
1092 static void
1093 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1095 if (rt_se_boosted(rt_se))
1096 rt_rq->rt_nr_boosted++;
1098 if (rt_rq->tg)
1099 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
1102 static void
1103 dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1105 if (rt_se_boosted(rt_se))
1106 rt_rq->rt_nr_boosted--;
1108 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
1111 #else /* CONFIG_RT_GROUP_SCHED */
1113 static void
1114 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1116 start_rt_bandwidth(&def_rt_bandwidth);
1119 static inline
1120 void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1122 #endif /* CONFIG_RT_GROUP_SCHED */
1124 static inline
1125 unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1127 struct rt_rq *group_rq = group_rt_rq(rt_se);
1129 if (group_rq)
1130 return group_rq->rt_nr_running;
1131 else
1132 return 1;
1135 static inline
1136 void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1138 int prio = rt_se_prio(rt_se);
1140 WARN_ON(!rt_prio(prio));
1141 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
1143 inc_rt_prio(rt_rq, prio);
1144 inc_rt_migration(rt_se, rt_rq);
1145 inc_rt_group(rt_se, rt_rq);
1148 static inline
1149 void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1151 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
1152 WARN_ON(!rt_rq->rt_nr_running);
1153 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
1155 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1156 dec_rt_migration(rt_se, rt_rq);
1157 dec_rt_group(rt_se, rt_rq);
1160 static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
1162 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1163 struct rt_prio_array *array = &rt_rq->active;
1164 struct rt_rq *group_rq = group_rt_rq(rt_se);
1165 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1168 * Don't enqueue the group if its throttled, or when empty.
1169 * The latter is a consequence of the former when a child group
1170 * get throttled and the current group doesn't have any other
1171 * active members.
1173 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running))
1174 return;
1176 if (head)
1177 list_add(&rt_se->run_list, queue);
1178 else
1179 list_add_tail(&rt_se->run_list, queue);
1180 __set_bit(rt_se_prio(rt_se), array->bitmap);
1182 inc_rt_tasks(rt_se, rt_rq);
1185 static void __dequeue_rt_entity(struct sched_rt_entity *rt_se)
1187 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1188 struct rt_prio_array *array = &rt_rq->active;
1190 list_del_init(&rt_se->run_list);
1191 if (list_empty(array->queue + rt_se_prio(rt_se)))
1192 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1194 dec_rt_tasks(rt_se, rt_rq);
1198 * Because the prio of an upper entry depends on the lower
1199 * entries, we must remove entries top - down.
1201 static void dequeue_rt_stack(struct sched_rt_entity *rt_se)
1203 struct sched_rt_entity *back = NULL;
1205 for_each_sched_rt_entity(rt_se) {
1206 rt_se->back = back;
1207 back = rt_se;
1210 dequeue_top_rt_rq(rt_rq_of_se(back));
1212 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1213 if (on_rt_rq(rt_se))
1214 __dequeue_rt_entity(rt_se);
1218 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head)
1220 struct rq *rq = rq_of_rt_se(rt_se);
1222 dequeue_rt_stack(rt_se);
1223 for_each_sched_rt_entity(rt_se)
1224 __enqueue_rt_entity(rt_se, head);
1225 enqueue_top_rt_rq(&rq->rt);
1228 static void dequeue_rt_entity(struct sched_rt_entity *rt_se)
1230 struct rq *rq = rq_of_rt_se(rt_se);
1232 dequeue_rt_stack(rt_se);
1234 for_each_sched_rt_entity(rt_se) {
1235 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1237 if (rt_rq && rt_rq->rt_nr_running)
1238 __enqueue_rt_entity(rt_se, false);
1240 enqueue_top_rt_rq(&rq->rt);
1244 * Adding/removing a task to/from a priority array:
1246 static void
1247 enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1249 struct sched_rt_entity *rt_se = &p->rt;
1251 if (flags & ENQUEUE_WAKEUP)
1252 rt_se->timeout = 0;
1254 enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD);
1256 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1257 enqueue_pushable_task(rq, p);
1260 static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1262 struct sched_rt_entity *rt_se = &p->rt;
1264 update_curr_rt(rq);
1265 dequeue_rt_entity(rt_se);
1267 dequeue_pushable_task(rq, p);
1271 * Put task to the head or the end of the run list without the overhead of
1272 * dequeue followed by enqueue.
1274 static void
1275 requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
1277 if (on_rt_rq(rt_se)) {
1278 struct rt_prio_array *array = &rt_rq->active;
1279 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1281 if (head)
1282 list_move(&rt_se->run_list, queue);
1283 else
1284 list_move_tail(&rt_se->run_list, queue);
1288 static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
1290 struct sched_rt_entity *rt_se = &p->rt;
1291 struct rt_rq *rt_rq;
1293 for_each_sched_rt_entity(rt_se) {
1294 rt_rq = rt_rq_of_se(rt_se);
1295 requeue_rt_entity(rt_rq, rt_se, head);
1299 static void yield_task_rt(struct rq *rq)
1301 requeue_task_rt(rq, rq->curr, 0);
1304 #ifdef CONFIG_SMP
1305 static int find_lowest_rq(struct task_struct *task);
1307 static int
1308 select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
1310 struct task_struct *curr;
1311 struct rq *rq;
1313 /* For anything but wake ups, just return the task_cpu */
1314 if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1315 goto out;
1317 rq = cpu_rq(cpu);
1319 rcu_read_lock();
1320 curr = READ_ONCE(rq->curr); /* unlocked access */
1323 * If the current task on @p's runqueue is an RT task, then
1324 * try to see if we can wake this RT task up on another
1325 * runqueue. Otherwise simply start this RT task
1326 * on its current runqueue.
1328 * We want to avoid overloading runqueues. If the woken
1329 * task is a higher priority, then it will stay on this CPU
1330 * and the lower prio task should be moved to another CPU.
1331 * Even though this will probably make the lower prio task
1332 * lose its cache, we do not want to bounce a higher task
1333 * around just because it gave up its CPU, perhaps for a
1334 * lock?
1336 * For equal prio tasks, we just let the scheduler sort it out.
1338 * Otherwise, just let it ride on the affined RQ and the
1339 * post-schedule router will push the preempted task away
1341 * This test is optimistic, if we get it wrong the load-balancer
1342 * will have to sort it out.
1344 if (curr && unlikely(rt_task(curr)) &&
1345 (curr->nr_cpus_allowed < 2 ||
1346 curr->prio <= p->prio)) {
1347 int target = find_lowest_rq(p);
1350 * Don't bother moving it if the destination CPU is
1351 * not running a lower priority task.
1353 if (target != -1 &&
1354 p->prio < cpu_rq(target)->rt.highest_prio.curr)
1355 cpu = target;
1357 rcu_read_unlock();
1359 out:
1360 return cpu;
1363 static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1366 * Current can't be migrated, useless to reschedule,
1367 * let's hope p can move out.
1369 if (rq->curr->nr_cpus_allowed == 1 ||
1370 !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1371 return;
1374 * p is migratable, so let's not schedule it and
1375 * see if it is pushed or pulled somewhere else.
1377 if (p->nr_cpus_allowed != 1
1378 && cpupri_find(&rq->rd->cpupri, p, NULL))
1379 return;
1382 * There appears to be other cpus that can accept
1383 * current and none to run 'p', so lets reschedule
1384 * to try and push current away:
1386 requeue_task_rt(rq, p, 1);
1387 resched_curr(rq);
1390 #endif /* CONFIG_SMP */
1393 * Preempt the current task with a newly woken task if needed:
1395 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
1397 if (p->prio < rq->curr->prio) {
1398 resched_curr(rq);
1399 return;
1402 #ifdef CONFIG_SMP
1404 * If:
1406 * - the newly woken task is of equal priority to the current task
1407 * - the newly woken task is non-migratable while current is migratable
1408 * - current will be preempted on the next reschedule
1410 * we should check to see if current can readily move to a different
1411 * cpu. If so, we will reschedule to allow the push logic to try
1412 * to move current somewhere else, making room for our non-migratable
1413 * task.
1415 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
1416 check_preempt_equal_prio(rq, p);
1417 #endif
1420 static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1421 struct rt_rq *rt_rq)
1423 struct rt_prio_array *array = &rt_rq->active;
1424 struct sched_rt_entity *next = NULL;
1425 struct list_head *queue;
1426 int idx;
1428 idx = sched_find_first_bit(array->bitmap);
1429 BUG_ON(idx >= MAX_RT_PRIO);
1431 queue = array->queue + idx;
1432 next = list_entry(queue->next, struct sched_rt_entity, run_list);
1434 return next;
1437 static struct task_struct *_pick_next_task_rt(struct rq *rq)
1439 struct sched_rt_entity *rt_se;
1440 struct task_struct *p;
1441 struct rt_rq *rt_rq = &rq->rt;
1443 do {
1444 rt_se = pick_next_rt_entity(rq, rt_rq);
1445 BUG_ON(!rt_se);
1446 rt_rq = group_rt_rq(rt_se);
1447 } while (rt_rq);
1449 p = rt_task_of(rt_se);
1450 p->se.exec_start = rq_clock_task(rq);
1452 return p;
1455 static struct task_struct *
1456 pick_next_task_rt(struct rq *rq, struct task_struct *prev)
1458 struct task_struct *p;
1459 struct rt_rq *rt_rq = &rq->rt;
1461 if (need_pull_rt_task(rq, prev)) {
1463 * This is OK, because current is on_cpu, which avoids it being
1464 * picked for load-balance and preemption/IRQs are still
1465 * disabled avoiding further scheduler activity on it and we're
1466 * being very careful to re-start the picking loop.
1468 lockdep_unpin_lock(&rq->lock);
1469 pull_rt_task(rq);
1470 lockdep_pin_lock(&rq->lock);
1472 * pull_rt_task() can drop (and re-acquire) rq->lock; this
1473 * means a dl or stop task can slip in, in which case we need
1474 * to re-start task selection.
1476 if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
1477 rq->dl.dl_nr_running))
1478 return RETRY_TASK;
1482 * We may dequeue prev's rt_rq in put_prev_task().
1483 * So, we update time before rt_nr_running check.
1485 if (prev->sched_class == &rt_sched_class)
1486 update_curr_rt(rq);
1488 if (!rt_rq->rt_queued)
1489 return NULL;
1491 put_prev_task(rq, prev);
1493 p = _pick_next_task_rt(rq);
1495 /* The running task is never eligible for pushing */
1496 dequeue_pushable_task(rq, p);
1498 queue_push_tasks(rq);
1500 return p;
1503 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
1505 update_curr_rt(rq);
1508 * The previous task needs to be made eligible for pushing
1509 * if it is still active
1511 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
1512 enqueue_pushable_task(rq, p);
1515 #ifdef CONFIG_SMP
1517 /* Only try algorithms three times */
1518 #define RT_MAX_TRIES 3
1520 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1522 if (!task_running(rq, p) &&
1523 cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
1524 return 1;
1525 return 0;
1529 * Return the highest pushable rq's task, which is suitable to be executed
1530 * on the cpu, NULL otherwise
1532 static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
1534 struct plist_head *head = &rq->rt.pushable_tasks;
1535 struct task_struct *p;
1537 if (!has_pushable_tasks(rq))
1538 return NULL;
1540 plist_for_each_entry(p, head, pushable_tasks) {
1541 if (pick_rt_task(rq, p, cpu))
1542 return p;
1545 return NULL;
1548 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
1550 static int find_lowest_rq(struct task_struct *task)
1552 struct sched_domain *sd;
1553 struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
1554 int this_cpu = smp_processor_id();
1555 int cpu = task_cpu(task);
1557 /* Make sure the mask is initialized first */
1558 if (unlikely(!lowest_mask))
1559 return -1;
1561 if (task->nr_cpus_allowed == 1)
1562 return -1; /* No other targets possible */
1564 if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
1565 return -1; /* No targets found */
1568 * At this point we have built a mask of cpus representing the
1569 * lowest priority tasks in the system. Now we want to elect
1570 * the best one based on our affinity and topology.
1572 * We prioritize the last cpu that the task executed on since
1573 * it is most likely cache-hot in that location.
1575 if (cpumask_test_cpu(cpu, lowest_mask))
1576 return cpu;
1579 * Otherwise, we consult the sched_domains span maps to figure
1580 * out which cpu is logically closest to our hot cache data.
1582 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1583 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
1585 rcu_read_lock();
1586 for_each_domain(cpu, sd) {
1587 if (sd->flags & SD_WAKE_AFFINE) {
1588 int best_cpu;
1591 * "this_cpu" is cheaper to preempt than a
1592 * remote processor.
1594 if (this_cpu != -1 &&
1595 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1596 rcu_read_unlock();
1597 return this_cpu;
1600 best_cpu = cpumask_first_and(lowest_mask,
1601 sched_domain_span(sd));
1602 if (best_cpu < nr_cpu_ids) {
1603 rcu_read_unlock();
1604 return best_cpu;
1608 rcu_read_unlock();
1611 * And finally, if there were no matches within the domains
1612 * just give the caller *something* to work with from the compatible
1613 * locations.
1615 if (this_cpu != -1)
1616 return this_cpu;
1618 cpu = cpumask_any(lowest_mask);
1619 if (cpu < nr_cpu_ids)
1620 return cpu;
1621 return -1;
1624 /* Will lock the rq it finds */
1625 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
1627 struct rq *lowest_rq = NULL;
1628 int tries;
1629 int cpu;
1631 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
1632 cpu = find_lowest_rq(task);
1634 if ((cpu == -1) || (cpu == rq->cpu))
1635 break;
1637 lowest_rq = cpu_rq(cpu);
1639 if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1641 * Target rq has tasks of equal or higher priority,
1642 * retrying does not release any lock and is unlikely
1643 * to yield a different result.
1645 lowest_rq = NULL;
1646 break;
1649 /* if the prio of this runqueue changed, try again */
1650 if (double_lock_balance(rq, lowest_rq)) {
1652 * We had to unlock the run queue. In
1653 * the mean time, task could have
1654 * migrated already or had its affinity changed.
1655 * Also make sure that it wasn't scheduled on its rq.
1657 if (unlikely(task_rq(task) != rq ||
1658 !cpumask_test_cpu(lowest_rq->cpu,
1659 tsk_cpus_allowed(task)) ||
1660 task_running(rq, task) ||
1661 !task_on_rq_queued(task))) {
1663 double_unlock_balance(rq, lowest_rq);
1664 lowest_rq = NULL;
1665 break;
1669 /* If this rq is still suitable use it. */
1670 if (lowest_rq->rt.highest_prio.curr > task->prio)
1671 break;
1673 /* try again */
1674 double_unlock_balance(rq, lowest_rq);
1675 lowest_rq = NULL;
1678 return lowest_rq;
1681 static struct task_struct *pick_next_pushable_task(struct rq *rq)
1683 struct task_struct *p;
1685 if (!has_pushable_tasks(rq))
1686 return NULL;
1688 p = plist_first_entry(&rq->rt.pushable_tasks,
1689 struct task_struct, pushable_tasks);
1691 BUG_ON(rq->cpu != task_cpu(p));
1692 BUG_ON(task_current(rq, p));
1693 BUG_ON(p->nr_cpus_allowed <= 1);
1695 BUG_ON(!task_on_rq_queued(p));
1696 BUG_ON(!rt_task(p));
1698 return p;
1702 * If the current CPU has more than one RT task, see if the non
1703 * running task can migrate over to a CPU that is running a task
1704 * of lesser priority.
1706 static int push_rt_task(struct rq *rq)
1708 struct task_struct *next_task;
1709 struct rq *lowest_rq;
1710 int ret = 0;
1712 if (!rq->rt.overloaded)
1713 return 0;
1715 next_task = pick_next_pushable_task(rq);
1716 if (!next_task)
1717 return 0;
1719 retry:
1720 if (unlikely(next_task == rq->curr)) {
1721 WARN_ON(1);
1722 return 0;
1726 * It's possible that the next_task slipped in of
1727 * higher priority than current. If that's the case
1728 * just reschedule current.
1730 if (unlikely(next_task->prio < rq->curr->prio)) {
1731 resched_curr(rq);
1732 return 0;
1735 /* We might release rq lock */
1736 get_task_struct(next_task);
1738 /* find_lock_lowest_rq locks the rq if found */
1739 lowest_rq = find_lock_lowest_rq(next_task, rq);
1740 if (!lowest_rq) {
1741 struct task_struct *task;
1743 * find_lock_lowest_rq releases rq->lock
1744 * so it is possible that next_task has migrated.
1746 * We need to make sure that the task is still on the same
1747 * run-queue and is also still the next task eligible for
1748 * pushing.
1750 task = pick_next_pushable_task(rq);
1751 if (task_cpu(next_task) == rq->cpu && task == next_task) {
1753 * The task hasn't migrated, and is still the next
1754 * eligible task, but we failed to find a run-queue
1755 * to push it to. Do not retry in this case, since
1756 * other cpus will pull from us when ready.
1758 goto out;
1761 if (!task)
1762 /* No more tasks, just exit */
1763 goto out;
1766 * Something has shifted, try again.
1768 put_task_struct(next_task);
1769 next_task = task;
1770 goto retry;
1773 deactivate_task(rq, next_task, 0);
1774 set_task_cpu(next_task, lowest_rq->cpu);
1775 activate_task(lowest_rq, next_task, 0);
1776 ret = 1;
1778 resched_curr(lowest_rq);
1780 double_unlock_balance(rq, lowest_rq);
1782 out:
1783 put_task_struct(next_task);
1785 return ret;
1788 static void push_rt_tasks(struct rq *rq)
1790 /* push_rt_task will return true if it moved an RT */
1791 while (push_rt_task(rq))
1795 #ifdef HAVE_RT_PUSH_IPI
1798 * When a high priority task schedules out from a CPU and a lower priority
1799 * task is scheduled in, a check is made to see if there's any RT tasks
1800 * on other CPUs that are waiting to run because a higher priority RT task
1801 * is currently running on its CPU. In this case, the CPU with multiple RT
1802 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
1803 * up that may be able to run one of its non-running queued RT tasks.
1805 * All CPUs with overloaded RT tasks need to be notified as there is currently
1806 * no way to know which of these CPUs have the highest priority task waiting
1807 * to run. Instead of trying to take a spinlock on each of these CPUs,
1808 * which has shown to cause large latency when done on machines with many
1809 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
1810 * RT tasks waiting to run.
1812 * Just sending an IPI to each of the CPUs is also an issue, as on large
1813 * count CPU machines, this can cause an IPI storm on a CPU, especially
1814 * if its the only CPU with multiple RT tasks queued, and a large number
1815 * of CPUs scheduling a lower priority task at the same time.
1817 * Each root domain has its own irq work function that can iterate over
1818 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
1819 * tassk must be checked if there's one or many CPUs that are lowering
1820 * their priority, there's a single irq work iterator that will try to
1821 * push off RT tasks that are waiting to run.
1823 * When a CPU schedules a lower priority task, it will kick off the
1824 * irq work iterator that will jump to each CPU with overloaded RT tasks.
1825 * As it only takes the first CPU that schedules a lower priority task
1826 * to start the process, the rto_start variable is incremented and if
1827 * the atomic result is one, then that CPU will try to take the rto_lock.
1828 * This prevents high contention on the lock as the process handles all
1829 * CPUs scheduling lower priority tasks.
1831 * All CPUs that are scheduling a lower priority task will increment the
1832 * rt_loop_next variable. This will make sure that the irq work iterator
1833 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
1834 * priority task, even if the iterator is in the middle of a scan. Incrementing
1835 * the rt_loop_next will cause the iterator to perform another scan.
1838 static int rto_next_cpu(struct root_domain *rd)
1840 int next;
1841 int cpu;
1844 * When starting the IPI RT pushing, the rto_cpu is set to -1,
1845 * rt_next_cpu() will simply return the first CPU found in
1846 * the rto_mask.
1848 * If rto_next_cpu() is called with rto_cpu is a valid cpu, it
1849 * will return the next CPU found in the rto_mask.
1851 * If there are no more CPUs left in the rto_mask, then a check is made
1852 * against rto_loop and rto_loop_next. rto_loop is only updated with
1853 * the rto_lock held, but any CPU may increment the rto_loop_next
1854 * without any locking.
1856 for (;;) {
1858 /* When rto_cpu is -1 this acts like cpumask_first() */
1859 cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
1861 rd->rto_cpu = cpu;
1863 if (cpu < nr_cpu_ids)
1864 return cpu;
1866 rd->rto_cpu = -1;
1869 * ACQUIRE ensures we see the @rto_mask changes
1870 * made prior to the @next value observed.
1872 * Matches WMB in rt_set_overload().
1874 next = atomic_read_acquire(&rd->rto_loop_next);
1876 if (rd->rto_loop == next)
1877 break;
1879 rd->rto_loop = next;
1882 return -1;
1885 static inline bool rto_start_trylock(atomic_t *v)
1887 return !atomic_cmpxchg_acquire(v, 0, 1);
1890 static inline void rto_start_unlock(atomic_t *v)
1892 atomic_set_release(v, 0);
1895 static void tell_cpu_to_push(struct rq *rq)
1897 int cpu = -1;
1899 /* Keep the loop going if the IPI is currently active */
1900 atomic_inc(&rq->rd->rto_loop_next);
1902 /* Only one CPU can initiate a loop at a time */
1903 if (!rto_start_trylock(&rq->rd->rto_loop_start))
1904 return;
1906 raw_spin_lock(&rq->rd->rto_lock);
1909 * The rto_cpu is updated under the lock, if it has a valid cpu
1910 * then the IPI is still running and will continue due to the
1911 * update to loop_next, and nothing needs to be done here.
1912 * Otherwise it is finishing up and an ipi needs to be sent.
1914 if (rq->rd->rto_cpu < 0)
1915 cpu = rto_next_cpu(rq->rd);
1917 raw_spin_unlock(&rq->rd->rto_lock);
1919 rto_start_unlock(&rq->rd->rto_loop_start);
1921 if (cpu >= 0) {
1922 /* Make sure the rd does not get freed while pushing */
1923 sched_get_rd(rq->rd);
1924 irq_work_queue_on(&rq->rd->rto_push_work, cpu);
1928 /* Called from hardirq context */
1929 void rto_push_irq_work_func(struct irq_work *work)
1931 struct root_domain *rd =
1932 container_of(work, struct root_domain, rto_push_work);
1933 struct rq *rq;
1934 int cpu;
1936 rq = this_rq();
1939 * We do not need to grab the lock to check for has_pushable_tasks.
1940 * When it gets updated, a check is made if a push is possible.
1942 if (has_pushable_tasks(rq)) {
1943 raw_spin_lock(&rq->lock);
1944 push_rt_tasks(rq);
1945 raw_spin_unlock(&rq->lock);
1948 raw_spin_lock(&rd->rto_lock);
1950 /* Pass the IPI to the next rt overloaded queue */
1951 cpu = rto_next_cpu(rd);
1953 raw_spin_unlock(&rd->rto_lock);
1955 if (cpu < 0) {
1956 sched_put_rd(rd);
1957 return;
1960 /* Try the next RT overloaded CPU */
1961 irq_work_queue_on(&rd->rto_push_work, cpu);
1963 #endif /* HAVE_RT_PUSH_IPI */
1965 static void pull_rt_task(struct rq *this_rq)
1967 int this_cpu = this_rq->cpu, cpu;
1968 bool resched = false;
1969 struct task_struct *p;
1970 struct rq *src_rq;
1971 int rt_overload_count = rt_overloaded(this_rq);
1973 if (likely(!rt_overload_count))
1974 return;
1977 * Match the barrier from rt_set_overloaded; this guarantees that if we
1978 * see overloaded we must also see the rto_mask bit.
1980 smp_rmb();
1982 /* If we are the only overloaded CPU do nothing */
1983 if (rt_overload_count == 1 &&
1984 cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
1985 return;
1987 #ifdef HAVE_RT_PUSH_IPI
1988 if (sched_feat(RT_PUSH_IPI)) {
1989 tell_cpu_to_push(this_rq);
1990 return;
1992 #endif
1994 for_each_cpu(cpu, this_rq->rd->rto_mask) {
1995 if (this_cpu == cpu)
1996 continue;
1998 src_rq = cpu_rq(cpu);
2001 * Don't bother taking the src_rq->lock if the next highest
2002 * task is known to be lower-priority than our current task.
2003 * This may look racy, but if this value is about to go
2004 * logically higher, the src_rq will push this task away.
2005 * And if its going logically lower, we do not care
2007 if (src_rq->rt.highest_prio.next >=
2008 this_rq->rt.highest_prio.curr)
2009 continue;
2012 * We can potentially drop this_rq's lock in
2013 * double_lock_balance, and another CPU could
2014 * alter this_rq
2016 double_lock_balance(this_rq, src_rq);
2019 * We can pull only a task, which is pushable
2020 * on its rq, and no others.
2022 p = pick_highest_pushable_task(src_rq, this_cpu);
2025 * Do we have an RT task that preempts
2026 * the to-be-scheduled task?
2028 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2029 WARN_ON(p == src_rq->curr);
2030 WARN_ON(!task_on_rq_queued(p));
2033 * There's a chance that p is higher in priority
2034 * than what's currently running on its cpu.
2035 * This is just that p is wakeing up and hasn't
2036 * had a chance to schedule. We only pull
2037 * p if it is lower in priority than the
2038 * current task on the run queue
2040 if (p->prio < src_rq->curr->prio)
2041 goto skip;
2043 resched = true;
2045 deactivate_task(src_rq, p, 0);
2046 set_task_cpu(p, this_cpu);
2047 activate_task(this_rq, p, 0);
2049 * We continue with the search, just in
2050 * case there's an even higher prio task
2051 * in another runqueue. (low likelihood
2052 * but possible)
2055 skip:
2056 double_unlock_balance(this_rq, src_rq);
2059 if (resched)
2060 resched_curr(this_rq);
2064 * If we are not running and we are not going to reschedule soon, we should
2065 * try to push tasks away now
2067 static void task_woken_rt(struct rq *rq, struct task_struct *p)
2069 if (!task_running(rq, p) &&
2070 !test_tsk_need_resched(rq->curr) &&
2071 p->nr_cpus_allowed > 1 &&
2072 (dl_task(rq->curr) || rt_task(rq->curr)) &&
2073 (rq->curr->nr_cpus_allowed < 2 ||
2074 rq->curr->prio <= p->prio))
2075 push_rt_tasks(rq);
2078 /* Assumes rq->lock is held */
2079 static void rq_online_rt(struct rq *rq)
2081 if (rq->rt.overloaded)
2082 rt_set_overload(rq);
2084 __enable_runtime(rq);
2086 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
2089 /* Assumes rq->lock is held */
2090 static void rq_offline_rt(struct rq *rq)
2092 if (rq->rt.overloaded)
2093 rt_clear_overload(rq);
2095 __disable_runtime(rq);
2097 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
2101 * When switch from the rt queue, we bring ourselves to a position
2102 * that we might want to pull RT tasks from other runqueues.
2104 static void switched_from_rt(struct rq *rq, struct task_struct *p)
2107 * If there are other RT tasks then we will reschedule
2108 * and the scheduling of the other RT tasks will handle
2109 * the balancing. But if we are the last RT task
2110 * we may need to handle the pulling of RT tasks
2111 * now.
2113 if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
2114 return;
2116 queue_pull_task(rq);
2119 void __init init_sched_rt_class(void)
2121 unsigned int i;
2123 for_each_possible_cpu(i) {
2124 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
2125 GFP_KERNEL, cpu_to_node(i));
2128 #endif /* CONFIG_SMP */
2131 * When switching a task to RT, we may overload the runqueue
2132 * with RT tasks. In this case we try to push them off to
2133 * other runqueues.
2135 static void switched_to_rt(struct rq *rq, struct task_struct *p)
2138 * If we are already running, then there's nothing
2139 * that needs to be done. But if we are not running
2140 * we may need to preempt the current running task.
2141 * If that current running task is also an RT task
2142 * then see if we can move to another run queue.
2144 if (task_on_rq_queued(p) && rq->curr != p) {
2145 #ifdef CONFIG_SMP
2146 if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
2147 queue_push_tasks(rq);
2148 #endif /* CONFIG_SMP */
2149 if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
2150 resched_curr(rq);
2155 * Priority of the task has changed. This may cause
2156 * us to initiate a push or pull.
2158 static void
2159 prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
2161 if (!task_on_rq_queued(p))
2162 return;
2164 if (rq->curr == p) {
2165 #ifdef CONFIG_SMP
2167 * If our priority decreases while running, we
2168 * may need to pull tasks to this runqueue.
2170 if (oldprio < p->prio)
2171 queue_pull_task(rq);
2174 * If there's a higher priority task waiting to run
2175 * then reschedule.
2177 if (p->prio > rq->rt.highest_prio.curr)
2178 resched_curr(rq);
2179 #else
2180 /* For UP simply resched on drop of prio */
2181 if (oldprio < p->prio)
2182 resched_curr(rq);
2183 #endif /* CONFIG_SMP */
2184 } else {
2186 * This task is not running, but if it is
2187 * greater than the current running task
2188 * then reschedule.
2190 if (p->prio < rq->curr->prio)
2191 resched_curr(rq);
2195 static void watchdog(struct rq *rq, struct task_struct *p)
2197 unsigned long soft, hard;
2199 /* max may change after cur was read, this will be fixed next tick */
2200 soft = task_rlimit(p, RLIMIT_RTTIME);
2201 hard = task_rlimit_max(p, RLIMIT_RTTIME);
2203 if (soft != RLIM_INFINITY) {
2204 unsigned long next;
2206 if (p->rt.watchdog_stamp != jiffies) {
2207 p->rt.timeout++;
2208 p->rt.watchdog_stamp = jiffies;
2211 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
2212 if (p->rt.timeout > next)
2213 p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
2217 static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
2219 struct sched_rt_entity *rt_se = &p->rt;
2221 update_curr_rt(rq);
2223 watchdog(rq, p);
2226 * RR tasks need a special form of timeslice management.
2227 * FIFO tasks have no timeslices.
2229 if (p->policy != SCHED_RR)
2230 return;
2232 if (--p->rt.time_slice)
2233 return;
2235 p->rt.time_slice = sched_rr_timeslice;
2238 * Requeue to the end of queue if we (and all of our ancestors) are not
2239 * the only element on the queue
2241 for_each_sched_rt_entity(rt_se) {
2242 if (rt_se->run_list.prev != rt_se->run_list.next) {
2243 requeue_task_rt(rq, p, 0);
2244 resched_curr(rq);
2245 return;
2250 static void set_curr_task_rt(struct rq *rq)
2252 struct task_struct *p = rq->curr;
2254 p->se.exec_start = rq_clock_task(rq);
2256 /* The running task is never eligible for pushing */
2257 dequeue_pushable_task(rq, p);
2260 static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
2263 * Time slice is 0 for SCHED_FIFO tasks
2265 if (task->policy == SCHED_RR)
2266 return sched_rr_timeslice;
2267 else
2268 return 0;
2271 const struct sched_class rt_sched_class = {
2272 .next = &fair_sched_class,
2273 .enqueue_task = enqueue_task_rt,
2274 .dequeue_task = dequeue_task_rt,
2275 .yield_task = yield_task_rt,
2277 .check_preempt_curr = check_preempt_curr_rt,
2279 .pick_next_task = pick_next_task_rt,
2280 .put_prev_task = put_prev_task_rt,
2282 #ifdef CONFIG_SMP
2283 .select_task_rq = select_task_rq_rt,
2285 .set_cpus_allowed = set_cpus_allowed_common,
2286 .rq_online = rq_online_rt,
2287 .rq_offline = rq_offline_rt,
2288 .task_woken = task_woken_rt,
2289 .switched_from = switched_from_rt,
2290 #endif
2292 .set_curr_task = set_curr_task_rt,
2293 .task_tick = task_tick_rt,
2295 .get_rr_interval = get_rr_interval_rt,
2297 .prio_changed = prio_changed_rt,
2298 .switched_to = switched_to_rt,
2300 .update_curr = update_curr_rt,
2303 #ifdef CONFIG_SCHED_DEBUG
2304 extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2306 void print_rt_stats(struct seq_file *m, int cpu)
2308 rt_rq_iter_t iter;
2309 struct rt_rq *rt_rq;
2311 rcu_read_lock();
2312 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
2313 print_rt_rq(m, cpu, rt_rq);
2314 rcu_read_unlock();
2316 #endif /* CONFIG_SCHED_DEBUG */