2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
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
);
24 raw_spin_lock(&rt_b
->rt_runtime_lock
);
26 overrun
= hrtimer_forward_now(timer
, rt_b
->rt_period
);
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 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
36 return idle
? HRTIMER_NORESTART
: HRTIMER_RESTART
;
39 void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
)
41 rt_b
->rt_period
= ns_to_ktime(period
);
42 rt_b
->rt_runtime
= runtime
;
44 raw_spin_lock_init(&rt_b
->rt_runtime_lock
);
46 hrtimer_init(&rt_b
->rt_period_timer
,
47 CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
48 rt_b
->rt_period_timer
.function
= sched_rt_period_timer
;
51 static void start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
53 if (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
)
56 raw_spin_lock(&rt_b
->rt_runtime_lock
);
57 start_bandwidth_timer(&rt_b
->rt_period_timer
, rt_b
->rt_period
);
58 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
62 static void push_irq_work_func(struct irq_work
*work
);
65 void init_rt_rq(struct rt_rq
*rt_rq
)
67 struct rt_prio_array
*array
;
70 array
= &rt_rq
->active
;
71 for (i
= 0; i
< MAX_RT_PRIO
; i
++) {
72 INIT_LIST_HEAD(array
->queue
+ i
);
73 __clear_bit(i
, array
->bitmap
);
75 /* delimiter for bitsearch: */
76 __set_bit(MAX_RT_PRIO
, array
->bitmap
);
78 #if defined CONFIG_SMP
79 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
80 rt_rq
->highest_prio
.next
= MAX_RT_PRIO
;
81 rt_rq
->rt_nr_migratory
= 0;
82 rt_rq
->overloaded
= 0;
83 plist_head_init(&rt_rq
->pushable_tasks
);
85 #ifdef HAVE_RT_PUSH_IPI
86 rt_rq
->push_flags
= 0;
87 rt_rq
->push_cpu
= nr_cpu_ids
;
88 raw_spin_lock_init(&rt_rq
->push_lock
);
89 init_irq_work(&rt_rq
->push_work
, push_irq_work_func
);
91 #endif /* CONFIG_SMP */
92 /* We start is dequeued state, because no RT tasks are queued */
96 rt_rq
->rt_throttled
= 0;
97 rt_rq
->rt_runtime
= 0;
98 raw_spin_lock_init(&rt_rq
->rt_runtime_lock
);
101 #ifdef CONFIG_RT_GROUP_SCHED
102 static void destroy_rt_bandwidth(struct rt_bandwidth
*rt_b
)
104 hrtimer_cancel(&rt_b
->rt_period_timer
);
107 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
109 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
111 #ifdef CONFIG_SCHED_DEBUG
112 WARN_ON_ONCE(!rt_entity_is_task(rt_se
));
114 return container_of(rt_se
, struct task_struct
, rt
);
117 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
122 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
127 static inline struct rq
*rq_of_rt_se(struct sched_rt_entity
*rt_se
)
129 struct rt_rq
*rt_rq
= rt_se
->rt_rq
;
134 void free_rt_sched_group(struct task_group
*tg
)
139 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
141 for_each_possible_cpu(i
) {
152 void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
153 struct sched_rt_entity
*rt_se
, int cpu
,
154 struct sched_rt_entity
*parent
)
156 struct rq
*rq
= cpu_rq(cpu
);
158 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
159 rt_rq
->rt_nr_boosted
= 0;
163 tg
->rt_rq
[cpu
] = rt_rq
;
164 tg
->rt_se
[cpu
] = rt_se
;
170 rt_se
->rt_rq
= &rq
->rt
;
172 rt_se
->rt_rq
= parent
->my_q
;
175 rt_se
->parent
= parent
;
176 INIT_LIST_HEAD(&rt_se
->run_list
);
179 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
182 struct sched_rt_entity
*rt_se
;
185 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
188 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
192 init_rt_bandwidth(&tg
->rt_bandwidth
,
193 ktime_to_ns(def_rt_bandwidth
.rt_period
), 0);
195 for_each_possible_cpu(i
) {
196 rt_rq
= kzalloc_node(sizeof(struct rt_rq
),
197 GFP_KERNEL
, cpu_to_node(i
));
201 rt_se
= kzalloc_node(sizeof(struct sched_rt_entity
),
202 GFP_KERNEL
, cpu_to_node(i
));
207 rt_rq
->rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
208 init_tg_rt_entry(tg
, rt_rq
, rt_se
, i
, parent
->rt_se
[i
]);
219 #else /* CONFIG_RT_GROUP_SCHED */
221 #define rt_entity_is_task(rt_se) (1)
223 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
225 return container_of(rt_se
, struct task_struct
, rt
);
228 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
230 return container_of(rt_rq
, struct rq
, rt
);
233 static inline struct rq
*rq_of_rt_se(struct sched_rt_entity
*rt_se
)
235 struct task_struct
*p
= rt_task_of(rt_se
);
240 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
242 struct rq
*rq
= rq_of_rt_se(rt_se
);
247 void free_rt_sched_group(struct task_group
*tg
) { }
249 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
253 #endif /* CONFIG_RT_GROUP_SCHED */
257 static int pull_rt_task(struct rq
*this_rq
);
259 static inline bool need_pull_rt_task(struct rq
*rq
, struct task_struct
*prev
)
261 /* Try to pull RT tasks here if we lower this rq's prio */
262 return rq
->rt
.highest_prio
.curr
> prev
->prio
;
265 static inline int rt_overloaded(struct rq
*rq
)
267 return atomic_read(&rq
->rd
->rto_count
);
270 static inline void rt_set_overload(struct rq
*rq
)
275 cpumask_set_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
277 * Make sure the mask is visible before we set
278 * the overload count. That is checked to determine
279 * if we should look at the mask. It would be a shame
280 * if we looked at the mask, but the mask was not
283 * Matched by the barrier in pull_rt_task().
286 atomic_inc(&rq
->rd
->rto_count
);
289 static inline void rt_clear_overload(struct rq
*rq
)
294 /* the order here really doesn't matter */
295 atomic_dec(&rq
->rd
->rto_count
);
296 cpumask_clear_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
299 static void update_rt_migration(struct rt_rq
*rt_rq
)
301 if (rt_rq
->rt_nr_migratory
&& rt_rq
->rt_nr_total
> 1) {
302 if (!rt_rq
->overloaded
) {
303 rt_set_overload(rq_of_rt_rq(rt_rq
));
304 rt_rq
->overloaded
= 1;
306 } else if (rt_rq
->overloaded
) {
307 rt_clear_overload(rq_of_rt_rq(rt_rq
));
308 rt_rq
->overloaded
= 0;
312 static void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
314 struct task_struct
*p
;
316 if (!rt_entity_is_task(rt_se
))
319 p
= rt_task_of(rt_se
);
320 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
322 rt_rq
->rt_nr_total
++;
323 if (p
->nr_cpus_allowed
> 1)
324 rt_rq
->rt_nr_migratory
++;
326 update_rt_migration(rt_rq
);
329 static void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
331 struct task_struct
*p
;
333 if (!rt_entity_is_task(rt_se
))
336 p
= rt_task_of(rt_se
);
337 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
339 rt_rq
->rt_nr_total
--;
340 if (p
->nr_cpus_allowed
> 1)
341 rt_rq
->rt_nr_migratory
--;
343 update_rt_migration(rt_rq
);
346 static inline int has_pushable_tasks(struct rq
*rq
)
348 return !plist_head_empty(&rq
->rt
.pushable_tasks
);
351 static inline void set_post_schedule(struct rq
*rq
)
354 * We detect this state here so that we can avoid taking the RQ
355 * lock again later if there is no need to push
357 rq
->post_schedule
= has_pushable_tasks(rq
);
360 static void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
362 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
363 plist_node_init(&p
->pushable_tasks
, p
->prio
);
364 plist_add(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
366 /* Update the highest prio pushable task */
367 if (p
->prio
< rq
->rt
.highest_prio
.next
)
368 rq
->rt
.highest_prio
.next
= p
->prio
;
371 static void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
373 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
375 /* Update the new highest prio pushable task */
376 if (has_pushable_tasks(rq
)) {
377 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
378 struct task_struct
, pushable_tasks
);
379 rq
->rt
.highest_prio
.next
= p
->prio
;
381 rq
->rt
.highest_prio
.next
= MAX_RT_PRIO
;
386 static inline void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
390 static inline void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
395 void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
400 void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
404 static inline bool need_pull_rt_task(struct rq
*rq
, struct task_struct
*prev
)
409 static inline int pull_rt_task(struct rq
*this_rq
)
414 static inline void set_post_schedule(struct rq
*rq
)
417 #endif /* CONFIG_SMP */
419 static void enqueue_top_rt_rq(struct rt_rq
*rt_rq
);
420 static void dequeue_top_rt_rq(struct rt_rq
*rt_rq
);
422 static inline int on_rt_rq(struct sched_rt_entity
*rt_se
)
424 return !list_empty(&rt_se
->run_list
);
427 #ifdef CONFIG_RT_GROUP_SCHED
429 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
434 return rt_rq
->rt_runtime
;
437 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
439 return ktime_to_ns(rt_rq
->tg
->rt_bandwidth
.rt_period
);
442 typedef struct task_group
*rt_rq_iter_t
;
444 static inline struct task_group
*next_task_group(struct task_group
*tg
)
447 tg
= list_entry_rcu(tg
->list
.next
,
448 typeof(struct task_group
), list
);
449 } while (&tg
->list
!= &task_groups
&& task_group_is_autogroup(tg
));
451 if (&tg
->list
== &task_groups
)
457 #define for_each_rt_rq(rt_rq, iter, rq) \
458 for (iter = container_of(&task_groups, typeof(*iter), list); \
459 (iter = next_task_group(iter)) && \
460 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
462 #define for_each_sched_rt_entity(rt_se) \
463 for (; rt_se; rt_se = rt_se->parent)
465 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
470 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, bool head
);
471 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
);
473 static void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
475 struct task_struct
*curr
= rq_of_rt_rq(rt_rq
)->curr
;
476 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
477 struct sched_rt_entity
*rt_se
;
479 int cpu
= cpu_of(rq
);
481 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
483 if (rt_rq
->rt_nr_running
) {
485 enqueue_top_rt_rq(rt_rq
);
486 else if (!on_rt_rq(rt_se
))
487 enqueue_rt_entity(rt_se
, false);
489 if (rt_rq
->highest_prio
.curr
< curr
->prio
)
494 static void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
496 struct sched_rt_entity
*rt_se
;
497 int cpu
= cpu_of(rq_of_rt_rq(rt_rq
));
499 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
502 dequeue_top_rt_rq(rt_rq
);
503 else if (on_rt_rq(rt_se
))
504 dequeue_rt_entity(rt_se
);
507 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
509 return rt_rq
->rt_throttled
&& !rt_rq
->rt_nr_boosted
;
512 static int rt_se_boosted(struct sched_rt_entity
*rt_se
)
514 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
515 struct task_struct
*p
;
518 return !!rt_rq
->rt_nr_boosted
;
520 p
= rt_task_of(rt_se
);
521 return p
->prio
!= p
->normal_prio
;
525 static inline const struct cpumask
*sched_rt_period_mask(void)
527 return this_rq()->rd
->span
;
530 static inline const struct cpumask
*sched_rt_period_mask(void)
532 return cpu_online_mask
;
537 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
539 return container_of(rt_b
, struct task_group
, rt_bandwidth
)->rt_rq
[cpu
];
542 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
544 return &rt_rq
->tg
->rt_bandwidth
;
547 #else /* !CONFIG_RT_GROUP_SCHED */
549 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
551 return rt_rq
->rt_runtime
;
554 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
556 return ktime_to_ns(def_rt_bandwidth
.rt_period
);
559 typedef struct rt_rq
*rt_rq_iter_t
;
561 #define for_each_rt_rq(rt_rq, iter, rq) \
562 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
564 #define for_each_sched_rt_entity(rt_se) \
565 for (; rt_se; rt_se = NULL)
567 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
572 static inline void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
574 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
576 if (!rt_rq
->rt_nr_running
)
579 enqueue_top_rt_rq(rt_rq
);
583 static inline void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
585 dequeue_top_rt_rq(rt_rq
);
588 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
590 return rt_rq
->rt_throttled
;
593 static inline const struct cpumask
*sched_rt_period_mask(void)
595 return cpu_online_mask
;
599 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
601 return &cpu_rq(cpu
)->rt
;
604 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
606 return &def_rt_bandwidth
;
609 #endif /* CONFIG_RT_GROUP_SCHED */
611 bool sched_rt_bandwidth_account(struct rt_rq
*rt_rq
)
613 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
615 return (hrtimer_active(&rt_b
->rt_period_timer
) ||
616 rt_rq
->rt_time
< rt_b
->rt_runtime
);
621 * We ran out of runtime, see if we can borrow some from our neighbours.
623 static int do_balance_runtime(struct rt_rq
*rt_rq
)
625 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
626 struct root_domain
*rd
= rq_of_rt_rq(rt_rq
)->rd
;
627 int i
, weight
, more
= 0;
630 weight
= cpumask_weight(rd
->span
);
632 raw_spin_lock(&rt_b
->rt_runtime_lock
);
633 rt_period
= ktime_to_ns(rt_b
->rt_period
);
634 for_each_cpu(i
, rd
->span
) {
635 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
641 raw_spin_lock(&iter
->rt_runtime_lock
);
643 * Either all rqs have inf runtime and there's nothing to steal
644 * or __disable_runtime() below sets a specific rq to inf to
645 * indicate its been disabled and disalow stealing.
647 if (iter
->rt_runtime
== RUNTIME_INF
)
651 * From runqueues with spare time, take 1/n part of their
652 * spare time, but no more than our period.
654 diff
= iter
->rt_runtime
- iter
->rt_time
;
656 diff
= div_u64((u64
)diff
, weight
);
657 if (rt_rq
->rt_runtime
+ diff
> rt_period
)
658 diff
= rt_period
- rt_rq
->rt_runtime
;
659 iter
->rt_runtime
-= diff
;
660 rt_rq
->rt_runtime
+= diff
;
662 if (rt_rq
->rt_runtime
== rt_period
) {
663 raw_spin_unlock(&iter
->rt_runtime_lock
);
668 raw_spin_unlock(&iter
->rt_runtime_lock
);
670 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
676 * Ensure this RQ takes back all the runtime it lend to its neighbours.
678 static void __disable_runtime(struct rq
*rq
)
680 struct root_domain
*rd
= rq
->rd
;
684 if (unlikely(!scheduler_running
))
687 for_each_rt_rq(rt_rq
, iter
, rq
) {
688 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
692 raw_spin_lock(&rt_b
->rt_runtime_lock
);
693 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
695 * Either we're all inf and nobody needs to borrow, or we're
696 * already disabled and thus have nothing to do, or we have
697 * exactly the right amount of runtime to take out.
699 if (rt_rq
->rt_runtime
== RUNTIME_INF
||
700 rt_rq
->rt_runtime
== rt_b
->rt_runtime
)
702 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
705 * Calculate the difference between what we started out with
706 * and what we current have, that's the amount of runtime
707 * we lend and now have to reclaim.
709 want
= rt_b
->rt_runtime
- rt_rq
->rt_runtime
;
712 * Greedy reclaim, take back as much as we can.
714 for_each_cpu(i
, rd
->span
) {
715 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
719 * Can't reclaim from ourselves or disabled runqueues.
721 if (iter
== rt_rq
|| iter
->rt_runtime
== RUNTIME_INF
)
724 raw_spin_lock(&iter
->rt_runtime_lock
);
726 diff
= min_t(s64
, iter
->rt_runtime
, want
);
727 iter
->rt_runtime
-= diff
;
730 iter
->rt_runtime
-= want
;
733 raw_spin_unlock(&iter
->rt_runtime_lock
);
739 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
741 * We cannot be left wanting - that would mean some runtime
742 * leaked out of the system.
747 * Disable all the borrow logic by pretending we have inf
748 * runtime - in which case borrowing doesn't make sense.
750 rt_rq
->rt_runtime
= RUNTIME_INF
;
751 rt_rq
->rt_throttled
= 0;
752 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
753 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
755 /* Make rt_rq available for pick_next_task() */
756 sched_rt_rq_enqueue(rt_rq
);
760 static void __enable_runtime(struct rq
*rq
)
765 if (unlikely(!scheduler_running
))
769 * Reset each runqueue's bandwidth settings
771 for_each_rt_rq(rt_rq
, iter
, rq
) {
772 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
774 raw_spin_lock(&rt_b
->rt_runtime_lock
);
775 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
776 rt_rq
->rt_runtime
= rt_b
->rt_runtime
;
778 rt_rq
->rt_throttled
= 0;
779 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
780 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
784 static int balance_runtime(struct rt_rq
*rt_rq
)
788 if (!sched_feat(RT_RUNTIME_SHARE
))
791 if (rt_rq
->rt_time
> rt_rq
->rt_runtime
) {
792 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
793 more
= do_balance_runtime(rt_rq
);
794 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
799 #else /* !CONFIG_SMP */
800 static inline int balance_runtime(struct rt_rq
*rt_rq
)
804 #endif /* CONFIG_SMP */
806 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
)
808 int i
, idle
= 1, throttled
= 0;
809 const struct cpumask
*span
;
811 span
= sched_rt_period_mask();
812 #ifdef CONFIG_RT_GROUP_SCHED
814 * FIXME: isolated CPUs should really leave the root task group,
815 * whether they are isolcpus or were isolated via cpusets, lest
816 * the timer run on a CPU which does not service all runqueues,
817 * potentially leaving other CPUs indefinitely throttled. If
818 * isolation is really required, the user will turn the throttle
819 * off to kill the perturbations it causes anyway. Meanwhile,
820 * this maintains functionality for boot and/or troubleshooting.
822 if (rt_b
== &root_task_group
.rt_bandwidth
)
823 span
= cpu_online_mask
;
825 for_each_cpu(i
, span
) {
827 struct rt_rq
*rt_rq
= sched_rt_period_rt_rq(rt_b
, i
);
828 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
830 raw_spin_lock(&rq
->lock
);
831 if (rt_rq
->rt_time
) {
834 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
835 if (rt_rq
->rt_throttled
)
836 balance_runtime(rt_rq
);
837 runtime
= rt_rq
->rt_runtime
;
838 rt_rq
->rt_time
-= min(rt_rq
->rt_time
, overrun
*runtime
);
839 if (rt_rq
->rt_throttled
&& rt_rq
->rt_time
< runtime
) {
840 rt_rq
->rt_throttled
= 0;
844 * When we're idle and a woken (rt) task is
845 * throttled check_preempt_curr() will set
846 * skip_update and the time between the wakeup
847 * and this unthrottle will get accounted as
850 if (rt_rq
->rt_nr_running
&& rq
->curr
== rq
->idle
)
851 rq_clock_skip_update(rq
, false);
853 if (rt_rq
->rt_time
|| rt_rq
->rt_nr_running
)
855 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
856 } else if (rt_rq
->rt_nr_running
) {
858 if (!rt_rq_throttled(rt_rq
))
861 if (rt_rq
->rt_throttled
)
865 sched_rt_rq_enqueue(rt_rq
);
866 raw_spin_unlock(&rq
->lock
);
869 if (!throttled
&& (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
))
875 static inline int rt_se_prio(struct sched_rt_entity
*rt_se
)
877 #ifdef CONFIG_RT_GROUP_SCHED
878 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
881 return rt_rq
->highest_prio
.curr
;
884 return rt_task_of(rt_se
)->prio
;
887 static int sched_rt_runtime_exceeded(struct rt_rq
*rt_rq
)
889 u64 runtime
= sched_rt_runtime(rt_rq
);
891 if (rt_rq
->rt_throttled
)
892 return rt_rq_throttled(rt_rq
);
894 if (runtime
>= sched_rt_period(rt_rq
))
897 balance_runtime(rt_rq
);
898 runtime
= sched_rt_runtime(rt_rq
);
899 if (runtime
== RUNTIME_INF
)
902 if (rt_rq
->rt_time
> runtime
) {
903 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
906 * Don't actually throttle groups that have no runtime assigned
907 * but accrue some time due to boosting.
909 if (likely(rt_b
->rt_runtime
)) {
910 rt_rq
->rt_throttled
= 1;
911 printk_deferred_once("sched: RT throttling activated\n");
914 * In case we did anyway, make it go away,
915 * replenishment is a joke, since it will replenish us
921 if (rt_rq_throttled(rt_rq
)) {
922 sched_rt_rq_dequeue(rt_rq
);
931 * Update the current task's runtime statistics. Skip current tasks that
932 * are not in our scheduling class.
934 static void update_curr_rt(struct rq
*rq
)
936 struct task_struct
*curr
= rq
->curr
;
937 struct sched_rt_entity
*rt_se
= &curr
->rt
;
940 if (curr
->sched_class
!= &rt_sched_class
)
943 delta_exec
= rq_clock_task(rq
) - curr
->se
.exec_start
;
944 if (unlikely((s64
)delta_exec
<= 0))
947 schedstat_set(curr
->se
.statistics
.exec_max
,
948 max(curr
->se
.statistics
.exec_max
, delta_exec
));
950 curr
->se
.sum_exec_runtime
+= delta_exec
;
951 account_group_exec_runtime(curr
, delta_exec
);
953 curr
->se
.exec_start
= rq_clock_task(rq
);
954 cpuacct_charge(curr
, delta_exec
);
956 sched_rt_avg_update(rq
, delta_exec
);
958 if (!rt_bandwidth_enabled())
961 for_each_sched_rt_entity(rt_se
) {
962 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
964 if (sched_rt_runtime(rt_rq
) != RUNTIME_INF
) {
965 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
966 rt_rq
->rt_time
+= delta_exec
;
967 if (sched_rt_runtime_exceeded(rt_rq
))
969 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
975 dequeue_top_rt_rq(struct rt_rq
*rt_rq
)
977 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
979 BUG_ON(&rq
->rt
!= rt_rq
);
981 if (!rt_rq
->rt_queued
)
984 BUG_ON(!rq
->nr_running
);
986 sub_nr_running(rq
, rt_rq
->rt_nr_running
);
987 rt_rq
->rt_queued
= 0;
991 enqueue_top_rt_rq(struct rt_rq
*rt_rq
)
993 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
995 BUG_ON(&rq
->rt
!= rt_rq
);
997 if (rt_rq
->rt_queued
)
999 if (rt_rq_throttled(rt_rq
) || !rt_rq
->rt_nr_running
)
1002 add_nr_running(rq
, rt_rq
->rt_nr_running
);
1003 rt_rq
->rt_queued
= 1;
1006 #if defined CONFIG_SMP
1009 inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1011 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1013 #ifdef CONFIG_RT_GROUP_SCHED
1015 * Change rq's cpupri only if rt_rq is the top queue.
1017 if (&rq
->rt
!= rt_rq
)
1020 if (rq
->online
&& prio
< prev_prio
)
1021 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, prio
);
1025 dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1027 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1029 #ifdef CONFIG_RT_GROUP_SCHED
1031 * Change rq's cpupri only if rt_rq is the top queue.
1033 if (&rq
->rt
!= rt_rq
)
1036 if (rq
->online
&& rt_rq
->highest_prio
.curr
!= prev_prio
)
1037 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rt_rq
->highest_prio
.curr
);
1040 #else /* CONFIG_SMP */
1043 void inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1045 void dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1047 #endif /* CONFIG_SMP */
1049 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1051 inc_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1053 int prev_prio
= rt_rq
->highest_prio
.curr
;
1055 if (prio
< prev_prio
)
1056 rt_rq
->highest_prio
.curr
= prio
;
1058 inc_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1062 dec_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1064 int prev_prio
= rt_rq
->highest_prio
.curr
;
1066 if (rt_rq
->rt_nr_running
) {
1068 WARN_ON(prio
< prev_prio
);
1071 * This may have been our highest task, and therefore
1072 * we may have some recomputation to do
1074 if (prio
== prev_prio
) {
1075 struct rt_prio_array
*array
= &rt_rq
->active
;
1077 rt_rq
->highest_prio
.curr
=
1078 sched_find_first_bit(array
->bitmap
);
1082 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
1084 dec_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1089 static inline void inc_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1090 static inline void dec_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1092 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1094 #ifdef CONFIG_RT_GROUP_SCHED
1097 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1099 if (rt_se_boosted(rt_se
))
1100 rt_rq
->rt_nr_boosted
++;
1103 start_rt_bandwidth(&rt_rq
->tg
->rt_bandwidth
);
1107 dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1109 if (rt_se_boosted(rt_se
))
1110 rt_rq
->rt_nr_boosted
--;
1112 WARN_ON(!rt_rq
->rt_nr_running
&& rt_rq
->rt_nr_boosted
);
1115 #else /* CONFIG_RT_GROUP_SCHED */
1118 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1120 start_rt_bandwidth(&def_rt_bandwidth
);
1124 void dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
) {}
1126 #endif /* CONFIG_RT_GROUP_SCHED */
1129 unsigned int rt_se_nr_running(struct sched_rt_entity
*rt_se
)
1131 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1134 return group_rq
->rt_nr_running
;
1140 void inc_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1142 int prio
= rt_se_prio(rt_se
);
1144 WARN_ON(!rt_prio(prio
));
1145 rt_rq
->rt_nr_running
+= rt_se_nr_running(rt_se
);
1147 inc_rt_prio(rt_rq
, prio
);
1148 inc_rt_migration(rt_se
, rt_rq
);
1149 inc_rt_group(rt_se
, rt_rq
);
1153 void dec_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1155 WARN_ON(!rt_prio(rt_se_prio(rt_se
)));
1156 WARN_ON(!rt_rq
->rt_nr_running
);
1157 rt_rq
->rt_nr_running
-= rt_se_nr_running(rt_se
);
1159 dec_rt_prio(rt_rq
, rt_se_prio(rt_se
));
1160 dec_rt_migration(rt_se
, rt_rq
);
1161 dec_rt_group(rt_se
, rt_rq
);
1164 static void __enqueue_rt_entity(struct sched_rt_entity
*rt_se
, bool head
)
1166 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1167 struct rt_prio_array
*array
= &rt_rq
->active
;
1168 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1169 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1172 * Don't enqueue the group if its throttled, or when empty.
1173 * The latter is a consequence of the former when a child group
1174 * get throttled and the current group doesn't have any other
1177 if (group_rq
&& (rt_rq_throttled(group_rq
) || !group_rq
->rt_nr_running
))
1181 list_add(&rt_se
->run_list
, queue
);
1183 list_add_tail(&rt_se
->run_list
, queue
);
1184 __set_bit(rt_se_prio(rt_se
), array
->bitmap
);
1186 inc_rt_tasks(rt_se
, rt_rq
);
1189 static void __dequeue_rt_entity(struct sched_rt_entity
*rt_se
)
1191 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1192 struct rt_prio_array
*array
= &rt_rq
->active
;
1194 list_del_init(&rt_se
->run_list
);
1195 if (list_empty(array
->queue
+ rt_se_prio(rt_se
)))
1196 __clear_bit(rt_se_prio(rt_se
), array
->bitmap
);
1198 dec_rt_tasks(rt_se
, rt_rq
);
1202 * Because the prio of an upper entry depends on the lower
1203 * entries, we must remove entries top - down.
1205 static void dequeue_rt_stack(struct sched_rt_entity
*rt_se
)
1207 struct sched_rt_entity
*back
= NULL
;
1209 for_each_sched_rt_entity(rt_se
) {
1214 dequeue_top_rt_rq(rt_rq_of_se(back
));
1216 for (rt_se
= back
; rt_se
; rt_se
= rt_se
->back
) {
1217 if (on_rt_rq(rt_se
))
1218 __dequeue_rt_entity(rt_se
);
1222 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, bool head
)
1224 struct rq
*rq
= rq_of_rt_se(rt_se
);
1226 dequeue_rt_stack(rt_se
);
1227 for_each_sched_rt_entity(rt_se
)
1228 __enqueue_rt_entity(rt_se
, head
);
1229 enqueue_top_rt_rq(&rq
->rt
);
1232 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
)
1234 struct rq
*rq
= rq_of_rt_se(rt_se
);
1236 dequeue_rt_stack(rt_se
);
1238 for_each_sched_rt_entity(rt_se
) {
1239 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1241 if (rt_rq
&& rt_rq
->rt_nr_running
)
1242 __enqueue_rt_entity(rt_se
, false);
1244 enqueue_top_rt_rq(&rq
->rt
);
1248 * Adding/removing a task to/from a priority array:
1251 enqueue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1253 struct sched_rt_entity
*rt_se
= &p
->rt
;
1255 if (flags
& ENQUEUE_WAKEUP
)
1258 enqueue_rt_entity(rt_se
, flags
& ENQUEUE_HEAD
);
1260 if (!task_current(rq
, p
) && p
->nr_cpus_allowed
> 1)
1261 enqueue_pushable_task(rq
, p
);
1264 static void dequeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1266 struct sched_rt_entity
*rt_se
= &p
->rt
;
1269 dequeue_rt_entity(rt_se
);
1271 dequeue_pushable_task(rq
, p
);
1275 * Put task to the head or the end of the run list without the overhead of
1276 * dequeue followed by enqueue.
1279 requeue_rt_entity(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
, int head
)
1281 if (on_rt_rq(rt_se
)) {
1282 struct rt_prio_array
*array
= &rt_rq
->active
;
1283 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1286 list_move(&rt_se
->run_list
, queue
);
1288 list_move_tail(&rt_se
->run_list
, queue
);
1292 static void requeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int head
)
1294 struct sched_rt_entity
*rt_se
= &p
->rt
;
1295 struct rt_rq
*rt_rq
;
1297 for_each_sched_rt_entity(rt_se
) {
1298 rt_rq
= rt_rq_of_se(rt_se
);
1299 requeue_rt_entity(rt_rq
, rt_se
, head
);
1303 static void yield_task_rt(struct rq
*rq
)
1305 requeue_task_rt(rq
, rq
->curr
, 0);
1309 static int find_lowest_rq(struct task_struct
*task
);
1312 select_task_rq_rt(struct task_struct
*p
, int cpu
, int sd_flag
, int flags
)
1314 struct task_struct
*curr
;
1317 /* For anything but wake ups, just return the task_cpu */
1318 if (sd_flag
!= SD_BALANCE_WAKE
&& sd_flag
!= SD_BALANCE_FORK
)
1324 curr
= ACCESS_ONCE(rq
->curr
); /* unlocked access */
1327 * If the current task on @p's runqueue is an RT task, then
1328 * try to see if we can wake this RT task up on another
1329 * runqueue. Otherwise simply start this RT task
1330 * on its current runqueue.
1332 * We want to avoid overloading runqueues. If the woken
1333 * task is a higher priority, then it will stay on this CPU
1334 * and the lower prio task should be moved to another CPU.
1335 * Even though this will probably make the lower prio task
1336 * lose its cache, we do not want to bounce a higher task
1337 * around just because it gave up its CPU, perhaps for a
1340 * For equal prio tasks, we just let the scheduler sort it out.
1342 * Otherwise, just let it ride on the affined RQ and the
1343 * post-schedule router will push the preempted task away
1345 * This test is optimistic, if we get it wrong the load-balancer
1346 * will have to sort it out.
1348 if (curr
&& unlikely(rt_task(curr
)) &&
1349 (curr
->nr_cpus_allowed
< 2 ||
1350 curr
->prio
<= p
->prio
)) {
1351 int target
= find_lowest_rq(p
);
1354 * Don't bother moving it if the destination CPU is
1355 * not running a lower priority task.
1358 p
->prio
< cpu_rq(target
)->rt
.highest_prio
.curr
)
1367 static void check_preempt_equal_prio(struct rq
*rq
, struct task_struct
*p
)
1370 * Current can't be migrated, useless to reschedule,
1371 * let's hope p can move out.
1373 if (rq
->curr
->nr_cpus_allowed
== 1 ||
1374 !cpupri_find(&rq
->rd
->cpupri
, rq
->curr
, NULL
))
1378 * p is migratable, so let's not schedule it and
1379 * see if it is pushed or pulled somewhere else.
1381 if (p
->nr_cpus_allowed
!= 1
1382 && cpupri_find(&rq
->rd
->cpupri
, p
, NULL
))
1386 * There appears to be other cpus that can accept
1387 * current and none to run 'p', so lets reschedule
1388 * to try and push current away:
1390 requeue_task_rt(rq
, p
, 1);
1394 #endif /* CONFIG_SMP */
1397 * Preempt the current task with a newly woken task if needed:
1399 static void check_preempt_curr_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1401 if (p
->prio
< rq
->curr
->prio
) {
1410 * - the newly woken task is of equal priority to the current task
1411 * - the newly woken task is non-migratable while current is migratable
1412 * - current will be preempted on the next reschedule
1414 * we should check to see if current can readily move to a different
1415 * cpu. If so, we will reschedule to allow the push logic to try
1416 * to move current somewhere else, making room for our non-migratable
1419 if (p
->prio
== rq
->curr
->prio
&& !test_tsk_need_resched(rq
->curr
))
1420 check_preempt_equal_prio(rq
, p
);
1424 static struct sched_rt_entity
*pick_next_rt_entity(struct rq
*rq
,
1425 struct rt_rq
*rt_rq
)
1427 struct rt_prio_array
*array
= &rt_rq
->active
;
1428 struct sched_rt_entity
*next
= NULL
;
1429 struct list_head
*queue
;
1432 idx
= sched_find_first_bit(array
->bitmap
);
1433 BUG_ON(idx
>= MAX_RT_PRIO
);
1435 queue
= array
->queue
+ idx
;
1436 next
= list_entry(queue
->next
, struct sched_rt_entity
, run_list
);
1441 static struct task_struct
*_pick_next_task_rt(struct rq
*rq
)
1443 struct sched_rt_entity
*rt_se
;
1444 struct task_struct
*p
;
1445 struct rt_rq
*rt_rq
= &rq
->rt
;
1448 rt_se
= pick_next_rt_entity(rq
, rt_rq
);
1450 rt_rq
= group_rt_rq(rt_se
);
1453 p
= rt_task_of(rt_se
);
1454 p
->se
.exec_start
= rq_clock_task(rq
);
1459 static struct task_struct
*
1460 pick_next_task_rt(struct rq
*rq
, struct task_struct
*prev
)
1462 struct task_struct
*p
;
1463 struct rt_rq
*rt_rq
= &rq
->rt
;
1465 if (need_pull_rt_task(rq
, prev
)) {
1468 * pull_rt_task() can drop (and re-acquire) rq->lock; this
1469 * means a dl or stop task can slip in, in which case we need
1470 * to re-start task selection.
1472 if (unlikely((rq
->stop
&& task_on_rq_queued(rq
->stop
)) ||
1473 rq
->dl
.dl_nr_running
))
1478 * We may dequeue prev's rt_rq in put_prev_task().
1479 * So, we update time before rt_nr_running check.
1481 if (prev
->sched_class
== &rt_sched_class
)
1484 if (!rt_rq
->rt_queued
)
1487 put_prev_task(rq
, prev
);
1489 p
= _pick_next_task_rt(rq
);
1491 /* The running task is never eligible for pushing */
1492 dequeue_pushable_task(rq
, p
);
1494 set_post_schedule(rq
);
1499 static void put_prev_task_rt(struct rq
*rq
, struct task_struct
*p
)
1504 * The previous task needs to be made eligible for pushing
1505 * if it is still active
1507 if (on_rt_rq(&p
->rt
) && p
->nr_cpus_allowed
> 1)
1508 enqueue_pushable_task(rq
, p
);
1513 /* Only try algorithms three times */
1514 #define RT_MAX_TRIES 3
1516 static int pick_rt_task(struct rq
*rq
, struct task_struct
*p
, int cpu
)
1518 if (!task_running(rq
, p
) &&
1519 cpumask_test_cpu(cpu
, tsk_cpus_allowed(p
)))
1525 * Return the highest pushable rq's task, which is suitable to be executed
1526 * on the cpu, NULL otherwise
1528 static struct task_struct
*pick_highest_pushable_task(struct rq
*rq
, int cpu
)
1530 struct plist_head
*head
= &rq
->rt
.pushable_tasks
;
1531 struct task_struct
*p
;
1533 if (!has_pushable_tasks(rq
))
1536 plist_for_each_entry(p
, head
, pushable_tasks
) {
1537 if (pick_rt_task(rq
, p
, cpu
))
1544 static DEFINE_PER_CPU(cpumask_var_t
, local_cpu_mask
);
1546 static int find_lowest_rq(struct task_struct
*task
)
1548 struct sched_domain
*sd
;
1549 struct cpumask
*lowest_mask
= this_cpu_cpumask_var_ptr(local_cpu_mask
);
1550 int this_cpu
= smp_processor_id();
1551 int cpu
= task_cpu(task
);
1553 /* Make sure the mask is initialized first */
1554 if (unlikely(!lowest_mask
))
1557 if (task
->nr_cpus_allowed
== 1)
1558 return -1; /* No other targets possible */
1560 if (!cpupri_find(&task_rq(task
)->rd
->cpupri
, task
, lowest_mask
))
1561 return -1; /* No targets found */
1564 * At this point we have built a mask of cpus representing the
1565 * lowest priority tasks in the system. Now we want to elect
1566 * the best one based on our affinity and topology.
1568 * We prioritize the last cpu that the task executed on since
1569 * it is most likely cache-hot in that location.
1571 if (cpumask_test_cpu(cpu
, lowest_mask
))
1575 * Otherwise, we consult the sched_domains span maps to figure
1576 * out which cpu is logically closest to our hot cache data.
1578 if (!cpumask_test_cpu(this_cpu
, lowest_mask
))
1579 this_cpu
= -1; /* Skip this_cpu opt if not among lowest */
1582 for_each_domain(cpu
, sd
) {
1583 if (sd
->flags
& SD_WAKE_AFFINE
) {
1587 * "this_cpu" is cheaper to preempt than a
1590 if (this_cpu
!= -1 &&
1591 cpumask_test_cpu(this_cpu
, sched_domain_span(sd
))) {
1596 best_cpu
= cpumask_first_and(lowest_mask
,
1597 sched_domain_span(sd
));
1598 if (best_cpu
< nr_cpu_ids
) {
1607 * And finally, if there were no matches within the domains
1608 * just give the caller *something* to work with from the compatible
1614 cpu
= cpumask_any(lowest_mask
);
1615 if (cpu
< nr_cpu_ids
)
1620 /* Will lock the rq it finds */
1621 static struct rq
*find_lock_lowest_rq(struct task_struct
*task
, struct rq
*rq
)
1623 struct rq
*lowest_rq
= NULL
;
1627 for (tries
= 0; tries
< RT_MAX_TRIES
; tries
++) {
1628 cpu
= find_lowest_rq(task
);
1630 if ((cpu
== -1) || (cpu
== rq
->cpu
))
1633 lowest_rq
= cpu_rq(cpu
);
1635 if (lowest_rq
->rt
.highest_prio
.curr
<= task
->prio
) {
1637 * Target rq has tasks of equal or higher priority,
1638 * retrying does not release any lock and is unlikely
1639 * to yield a different result.
1645 /* if the prio of this runqueue changed, try again */
1646 if (double_lock_balance(rq
, lowest_rq
)) {
1648 * We had to unlock the run queue. In
1649 * the mean time, task could have
1650 * migrated already or had its affinity changed.
1651 * Also make sure that it wasn't scheduled on its rq.
1653 if (unlikely(task_rq(task
) != rq
||
1654 !cpumask_test_cpu(lowest_rq
->cpu
,
1655 tsk_cpus_allowed(task
)) ||
1656 task_running(rq
, task
) ||
1657 !task_on_rq_queued(task
))) {
1659 double_unlock_balance(rq
, lowest_rq
);
1665 /* If this rq is still suitable use it. */
1666 if (lowest_rq
->rt
.highest_prio
.curr
> task
->prio
)
1670 double_unlock_balance(rq
, lowest_rq
);
1677 static struct task_struct
*pick_next_pushable_task(struct rq
*rq
)
1679 struct task_struct
*p
;
1681 if (!has_pushable_tasks(rq
))
1684 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
1685 struct task_struct
, pushable_tasks
);
1687 BUG_ON(rq
->cpu
!= task_cpu(p
));
1688 BUG_ON(task_current(rq
, p
));
1689 BUG_ON(p
->nr_cpus_allowed
<= 1);
1691 BUG_ON(!task_on_rq_queued(p
));
1692 BUG_ON(!rt_task(p
));
1698 * If the current CPU has more than one RT task, see if the non
1699 * running task can migrate over to a CPU that is running a task
1700 * of lesser priority.
1702 static int push_rt_task(struct rq
*rq
)
1704 struct task_struct
*next_task
;
1705 struct rq
*lowest_rq
;
1708 if (!rq
->rt
.overloaded
)
1711 next_task
= pick_next_pushable_task(rq
);
1716 if (unlikely(next_task
== rq
->curr
)) {
1722 * It's possible that the next_task slipped in of
1723 * higher priority than current. If that's the case
1724 * just reschedule current.
1726 if (unlikely(next_task
->prio
< rq
->curr
->prio
)) {
1731 /* We might release rq lock */
1732 get_task_struct(next_task
);
1734 /* find_lock_lowest_rq locks the rq if found */
1735 lowest_rq
= find_lock_lowest_rq(next_task
, rq
);
1737 struct task_struct
*task
;
1739 * find_lock_lowest_rq releases rq->lock
1740 * so it is possible that next_task has migrated.
1742 * We need to make sure that the task is still on the same
1743 * run-queue and is also still the next task eligible for
1746 task
= pick_next_pushable_task(rq
);
1747 if (task_cpu(next_task
) == rq
->cpu
&& task
== next_task
) {
1749 * The task hasn't migrated, and is still the next
1750 * eligible task, but we failed to find a run-queue
1751 * to push it to. Do not retry in this case, since
1752 * other cpus will pull from us when ready.
1758 /* No more tasks, just exit */
1762 * Something has shifted, try again.
1764 put_task_struct(next_task
);
1769 deactivate_task(rq
, next_task
, 0);
1770 set_task_cpu(next_task
, lowest_rq
->cpu
);
1771 activate_task(lowest_rq
, next_task
, 0);
1774 resched_curr(lowest_rq
);
1776 double_unlock_balance(rq
, lowest_rq
);
1779 put_task_struct(next_task
);
1784 static void push_rt_tasks(struct rq
*rq
)
1786 /* push_rt_task will return true if it moved an RT */
1787 while (push_rt_task(rq
))
1791 #ifdef HAVE_RT_PUSH_IPI
1793 * The search for the next cpu always starts at rq->cpu and ends
1794 * when we reach rq->cpu again. It will never return rq->cpu.
1795 * This returns the next cpu to check, or nr_cpu_ids if the loop
1798 * rq->rt.push_cpu holds the last cpu returned by this function,
1799 * or if this is the first instance, it must hold rq->cpu.
1801 static int rto_next_cpu(struct rq
*rq
)
1803 int prev_cpu
= rq
->rt
.push_cpu
;
1806 cpu
= cpumask_next(prev_cpu
, rq
->rd
->rto_mask
);
1809 * If the previous cpu is less than the rq's CPU, then it already
1810 * passed the end of the mask, and has started from the beginning.
1811 * We end if the next CPU is greater or equal to rq's CPU.
1813 if (prev_cpu
< rq
->cpu
) {
1817 } else if (cpu
>= nr_cpu_ids
) {
1819 * We passed the end of the mask, start at the beginning.
1820 * If the result is greater or equal to the rq's CPU, then
1821 * the loop is finished.
1823 cpu
= cpumask_first(rq
->rd
->rto_mask
);
1827 rq
->rt
.push_cpu
= cpu
;
1829 /* Return cpu to let the caller know if the loop is finished or not */
1833 static int find_next_push_cpu(struct rq
*rq
)
1839 cpu
= rto_next_cpu(rq
);
1840 if (cpu
>= nr_cpu_ids
)
1842 next_rq
= cpu_rq(cpu
);
1844 /* Make sure the next rq can push to this rq */
1845 if (next_rq
->rt
.highest_prio
.next
< rq
->rt
.highest_prio
.curr
)
1852 #define RT_PUSH_IPI_EXECUTING 1
1853 #define RT_PUSH_IPI_RESTART 2
1855 static void tell_cpu_to_push(struct rq
*rq
)
1859 if (rq
->rt
.push_flags
& RT_PUSH_IPI_EXECUTING
) {
1860 raw_spin_lock(&rq
->rt
.push_lock
);
1861 /* Make sure it's still executing */
1862 if (rq
->rt
.push_flags
& RT_PUSH_IPI_EXECUTING
) {
1864 * Tell the IPI to restart the loop as things have
1865 * changed since it started.
1867 rq
->rt
.push_flags
|= RT_PUSH_IPI_RESTART
;
1868 raw_spin_unlock(&rq
->rt
.push_lock
);
1871 raw_spin_unlock(&rq
->rt
.push_lock
);
1874 /* When here, there's no IPI going around */
1876 rq
->rt
.push_cpu
= rq
->cpu
;
1877 cpu
= find_next_push_cpu(rq
);
1878 if (cpu
>= nr_cpu_ids
)
1881 rq
->rt
.push_flags
= RT_PUSH_IPI_EXECUTING
;
1883 irq_work_queue_on(&rq
->rt
.push_work
, cpu
);
1886 /* Called from hardirq context */
1887 static void try_to_push_tasks(void *arg
)
1889 struct rt_rq
*rt_rq
= arg
;
1890 struct rq
*rq
, *src_rq
;
1894 this_cpu
= rt_rq
->push_cpu
;
1896 /* Paranoid check */
1897 BUG_ON(this_cpu
!= smp_processor_id());
1899 rq
= cpu_rq(this_cpu
);
1900 src_rq
= rq_of_rt_rq(rt_rq
);
1903 if (has_pushable_tasks(rq
)) {
1904 raw_spin_lock(&rq
->lock
);
1906 raw_spin_unlock(&rq
->lock
);
1909 /* Pass the IPI to the next rt overloaded queue */
1910 raw_spin_lock(&rt_rq
->push_lock
);
1912 * If the source queue changed since the IPI went out,
1913 * we need to restart the search from that CPU again.
1915 if (rt_rq
->push_flags
& RT_PUSH_IPI_RESTART
) {
1916 rt_rq
->push_flags
&= ~RT_PUSH_IPI_RESTART
;
1917 rt_rq
->push_cpu
= src_rq
->cpu
;
1920 cpu
= find_next_push_cpu(src_rq
);
1922 if (cpu
>= nr_cpu_ids
)
1923 rt_rq
->push_flags
&= ~RT_PUSH_IPI_EXECUTING
;
1924 raw_spin_unlock(&rt_rq
->push_lock
);
1926 if (cpu
>= nr_cpu_ids
)
1930 * It is possible that a restart caused this CPU to be
1931 * chosen again. Don't bother with an IPI, just see if we
1932 * have more to push.
1934 if (unlikely(cpu
== rq
->cpu
))
1937 /* Try the next RT overloaded CPU */
1938 irq_work_queue_on(&rt_rq
->push_work
, cpu
);
1941 static void push_irq_work_func(struct irq_work
*work
)
1943 struct rt_rq
*rt_rq
= container_of(work
, struct rt_rq
, push_work
);
1945 try_to_push_tasks(rt_rq
);
1947 #endif /* HAVE_RT_PUSH_IPI */
1949 static int pull_rt_task(struct rq
*this_rq
)
1951 int this_cpu
= this_rq
->cpu
, ret
= 0, cpu
;
1952 struct task_struct
*p
;
1955 if (likely(!rt_overloaded(this_rq
)))
1959 * Match the barrier from rt_set_overloaded; this guarantees that if we
1960 * see overloaded we must also see the rto_mask bit.
1964 #ifdef HAVE_RT_PUSH_IPI
1965 if (sched_feat(RT_PUSH_IPI
)) {
1966 tell_cpu_to_push(this_rq
);
1971 for_each_cpu(cpu
, this_rq
->rd
->rto_mask
) {
1972 if (this_cpu
== cpu
)
1975 src_rq
= cpu_rq(cpu
);
1978 * Don't bother taking the src_rq->lock if the next highest
1979 * task is known to be lower-priority than our current task.
1980 * This may look racy, but if this value is about to go
1981 * logically higher, the src_rq will push this task away.
1982 * And if its going logically lower, we do not care
1984 if (src_rq
->rt
.highest_prio
.next
>=
1985 this_rq
->rt
.highest_prio
.curr
)
1989 * We can potentially drop this_rq's lock in
1990 * double_lock_balance, and another CPU could
1993 double_lock_balance(this_rq
, src_rq
);
1996 * We can pull only a task, which is pushable
1997 * on its rq, and no others.
1999 p
= pick_highest_pushable_task(src_rq
, this_cpu
);
2002 * Do we have an RT task that preempts
2003 * the to-be-scheduled task?
2005 if (p
&& (p
->prio
< this_rq
->rt
.highest_prio
.curr
)) {
2006 WARN_ON(p
== src_rq
->curr
);
2007 WARN_ON(!task_on_rq_queued(p
));
2010 * There's a chance that p is higher in priority
2011 * than what's currently running on its cpu.
2012 * This is just that p is wakeing up and hasn't
2013 * had a chance to schedule. We only pull
2014 * p if it is lower in priority than the
2015 * current task on the run queue
2017 if (p
->prio
< src_rq
->curr
->prio
)
2022 deactivate_task(src_rq
, p
, 0);
2023 set_task_cpu(p
, this_cpu
);
2024 activate_task(this_rq
, p
, 0);
2026 * We continue with the search, just in
2027 * case there's an even higher prio task
2028 * in another runqueue. (low likelihood
2033 double_unlock_balance(this_rq
, src_rq
);
2039 static void post_schedule_rt(struct rq
*rq
)
2045 * If we are not running and we are not going to reschedule soon, we should
2046 * try to push tasks away now
2048 static void task_woken_rt(struct rq
*rq
, struct task_struct
*p
)
2050 if (!task_running(rq
, p
) &&
2051 !test_tsk_need_resched(rq
->curr
) &&
2052 has_pushable_tasks(rq
) &&
2053 p
->nr_cpus_allowed
> 1 &&
2054 (dl_task(rq
->curr
) || rt_task(rq
->curr
)) &&
2055 (rq
->curr
->nr_cpus_allowed
< 2 ||
2056 rq
->curr
->prio
<= p
->prio
))
2060 static void set_cpus_allowed_rt(struct task_struct
*p
,
2061 const struct cpumask
*new_mask
)
2066 BUG_ON(!rt_task(p
));
2068 if (!task_on_rq_queued(p
))
2071 weight
= cpumask_weight(new_mask
);
2074 * Only update if the process changes its state from whether it
2075 * can migrate or not.
2077 if ((p
->nr_cpus_allowed
> 1) == (weight
> 1))
2083 * The process used to be able to migrate OR it can now migrate
2086 if (!task_current(rq
, p
))
2087 dequeue_pushable_task(rq
, p
);
2088 BUG_ON(!rq
->rt
.rt_nr_migratory
);
2089 rq
->rt
.rt_nr_migratory
--;
2091 if (!task_current(rq
, p
))
2092 enqueue_pushable_task(rq
, p
);
2093 rq
->rt
.rt_nr_migratory
++;
2096 update_rt_migration(&rq
->rt
);
2099 /* Assumes rq->lock is held */
2100 static void rq_online_rt(struct rq
*rq
)
2102 if (rq
->rt
.overloaded
)
2103 rt_set_overload(rq
);
2105 __enable_runtime(rq
);
2107 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rq
->rt
.highest_prio
.curr
);
2110 /* Assumes rq->lock is held */
2111 static void rq_offline_rt(struct rq
*rq
)
2113 if (rq
->rt
.overloaded
)
2114 rt_clear_overload(rq
);
2116 __disable_runtime(rq
);
2118 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, CPUPRI_INVALID
);
2122 * When switch from the rt queue, we bring ourselves to a position
2123 * that we might want to pull RT tasks from other runqueues.
2125 static void switched_from_rt(struct rq
*rq
, struct task_struct
*p
)
2128 * If there are other RT tasks then we will reschedule
2129 * and the scheduling of the other RT tasks will handle
2130 * the balancing. But if we are the last RT task
2131 * we may need to handle the pulling of RT tasks
2134 if (!task_on_rq_queued(p
) || rq
->rt
.rt_nr_running
)
2137 if (pull_rt_task(rq
))
2141 void __init
init_sched_rt_class(void)
2145 for_each_possible_cpu(i
) {
2146 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask
, i
),
2147 GFP_KERNEL
, cpu_to_node(i
));
2150 #endif /* CONFIG_SMP */
2153 * When switching a task to RT, we may overload the runqueue
2154 * with RT tasks. In this case we try to push them off to
2157 static void switched_to_rt(struct rq
*rq
, struct task_struct
*p
)
2159 int check_resched
= 1;
2162 * If we are already running, then there's nothing
2163 * that needs to be done. But if we are not running
2164 * we may need to preempt the current running task.
2165 * If that current running task is also an RT task
2166 * then see if we can move to another run queue.
2168 if (task_on_rq_queued(p
) && rq
->curr
!= p
) {
2170 if (p
->nr_cpus_allowed
> 1 && rq
->rt
.overloaded
&&
2171 /* Don't resched if we changed runqueues */
2172 push_rt_task(rq
) && rq
!= task_rq(p
))
2174 #endif /* CONFIG_SMP */
2175 if (check_resched
&& p
->prio
< rq
->curr
->prio
)
2181 * Priority of the task has changed. This may cause
2182 * us to initiate a push or pull.
2185 prio_changed_rt(struct rq
*rq
, struct task_struct
*p
, int oldprio
)
2187 if (!task_on_rq_queued(p
))
2190 if (rq
->curr
== p
) {
2193 * If our priority decreases while running, we
2194 * may need to pull tasks to this runqueue.
2196 if (oldprio
< p
->prio
)
2199 * If there's a higher priority task waiting to run
2200 * then reschedule. Note, the above pull_rt_task
2201 * can release the rq lock and p could migrate.
2202 * Only reschedule if p is still on the same runqueue.
2204 if (p
->prio
> rq
->rt
.highest_prio
.curr
&& rq
->curr
== p
)
2207 /* For UP simply resched on drop of prio */
2208 if (oldprio
< p
->prio
)
2210 #endif /* CONFIG_SMP */
2213 * This task is not running, but if it is
2214 * greater than the current running task
2217 if (p
->prio
< rq
->curr
->prio
)
2222 static void watchdog(struct rq
*rq
, struct task_struct
*p
)
2224 unsigned long soft
, hard
;
2226 /* max may change after cur was read, this will be fixed next tick */
2227 soft
= task_rlimit(p
, RLIMIT_RTTIME
);
2228 hard
= task_rlimit_max(p
, RLIMIT_RTTIME
);
2230 if (soft
!= RLIM_INFINITY
) {
2233 if (p
->rt
.watchdog_stamp
!= jiffies
) {
2235 p
->rt
.watchdog_stamp
= jiffies
;
2238 next
= DIV_ROUND_UP(min(soft
, hard
), USEC_PER_SEC
/HZ
);
2239 if (p
->rt
.timeout
> next
)
2240 p
->cputime_expires
.sched_exp
= p
->se
.sum_exec_runtime
;
2244 static void task_tick_rt(struct rq
*rq
, struct task_struct
*p
, int queued
)
2246 struct sched_rt_entity
*rt_se
= &p
->rt
;
2253 * RR tasks need a special form of timeslice management.
2254 * FIFO tasks have no timeslices.
2256 if (p
->policy
!= SCHED_RR
)
2259 if (--p
->rt
.time_slice
)
2262 p
->rt
.time_slice
= sched_rr_timeslice
;
2265 * Requeue to the end of queue if we (and all of our ancestors) are not
2266 * the only element on the queue
2268 for_each_sched_rt_entity(rt_se
) {
2269 if (rt_se
->run_list
.prev
!= rt_se
->run_list
.next
) {
2270 requeue_task_rt(rq
, p
, 0);
2277 static void set_curr_task_rt(struct rq
*rq
)
2279 struct task_struct
*p
= rq
->curr
;
2281 p
->se
.exec_start
= rq_clock_task(rq
);
2283 /* The running task is never eligible for pushing */
2284 dequeue_pushable_task(rq
, p
);
2287 static unsigned int get_rr_interval_rt(struct rq
*rq
, struct task_struct
*task
)
2290 * Time slice is 0 for SCHED_FIFO tasks
2292 if (task
->policy
== SCHED_RR
)
2293 return sched_rr_timeslice
;
2298 const struct sched_class rt_sched_class
= {
2299 .next
= &fair_sched_class
,
2300 .enqueue_task
= enqueue_task_rt
,
2301 .dequeue_task
= dequeue_task_rt
,
2302 .yield_task
= yield_task_rt
,
2304 .check_preempt_curr
= check_preempt_curr_rt
,
2306 .pick_next_task
= pick_next_task_rt
,
2307 .put_prev_task
= put_prev_task_rt
,
2310 .select_task_rq
= select_task_rq_rt
,
2312 .set_cpus_allowed
= set_cpus_allowed_rt
,
2313 .rq_online
= rq_online_rt
,
2314 .rq_offline
= rq_offline_rt
,
2315 .post_schedule
= post_schedule_rt
,
2316 .task_woken
= task_woken_rt
,
2317 .switched_from
= switched_from_rt
,
2320 .set_curr_task
= set_curr_task_rt
,
2321 .task_tick
= task_tick_rt
,
2323 .get_rr_interval
= get_rr_interval_rt
,
2325 .prio_changed
= prio_changed_rt
,
2326 .switched_to
= switched_to_rt
,
2328 .update_curr
= update_curr_rt
,
2331 #ifdef CONFIG_SCHED_DEBUG
2332 extern void print_rt_rq(struct seq_file
*m
, int cpu
, struct rt_rq
*rt_rq
);
2334 void print_rt_stats(struct seq_file
*m
, int cpu
)
2337 struct rt_rq
*rt_rq
;
2340 for_each_rt_rq(rt_rq
, iter
, cpu_rq(cpu
))
2341 print_rt_rq(m
, cpu
, rt_rq
);
2344 #endif /* CONFIG_SCHED_DEBUG */