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
);
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
)
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
;
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 */
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
));
109 return container_of(rt_se
, struct task_struct
, rt
);
112 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
117 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
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
;
129 void free_rt_sched_group(struct task_group
*tg
)
134 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
136 for_each_possible_cpu(i
) {
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;
158 tg
->rt_rq
[cpu
] = rt_rq
;
159 tg
->rt_se
[cpu
] = rt_se
;
165 rt_se
->rt_rq
= &rq
->rt
;
167 rt_se
->rt_rq
= parent
->my_q
;
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
)
177 struct sched_rt_entity
*rt_se
;
180 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
183 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
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
));
196 rt_se
= kzalloc_node(sizeof(struct sched_rt_entity
),
197 GFP_KERNEL
, cpu_to_node(i
));
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
]);
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
);
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
);
242 void free_rt_sched_group(struct task_group
*tg
) { }
244 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
248 #endif /* CONFIG_RT_GROUP_SCHED */
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
)
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
278 * Matched by the barrier in pull_rt_task().
281 atomic_inc(&rq
->rd
->rto_count
);
284 static inline void rt_clear_overload(struct rq
*rq
)
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
))
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
))
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
))
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
;
386 rq
->rt
.highest_prio
.next
= MAX_RT_PRIO
;
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
)
400 void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
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
)
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
)
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
)
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
)
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
)
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
) {
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
)
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
];
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
;
522 return !!rt_rq
->rt_nr_boosted
;
524 p
= rt_task_of(rt_se
);
525 return p
->prio
!= p
->normal_prio
;
529 static inline const struct cpumask
*sched_rt_period_mask(void)
531 return this_rq()->rd
->span
;
534 static inline const struct cpumask
*sched_rt_period_mask(void)
536 return cpu_online_mask
;
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
)
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
)
583 enqueue_top_rt_rq(rt_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
;
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
);
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
;
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
);
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
)
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
;
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
);
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
;
685 if (unlikely(!scheduler_running
))
688 for_each_rt_rq(rt_rq
, iter
, rq
) {
689 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
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
)
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
);
720 * Can't reclaim from ourselves or disabled runqueues.
722 if (iter
== rt_rq
|| iter
->rt_runtime
== RUNTIME_INF
)
725 raw_spin_lock(&iter
->rt_runtime_lock
);
727 diff
= min_t(s64
, iter
->rt_runtime
, want
);
728 iter
->rt_runtime
-= diff
;
731 iter
->rt_runtime
-= want
;
734 raw_spin_unlock(&iter
->rt_runtime_lock
);
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.
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
)
766 if (unlikely(!scheduler_running
))
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
;
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
))
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
;
819 for_each_cpu(i
, span
) {
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
);
827 if (rt_rq
->rt_time
) {
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;
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
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
)
851 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
852 } else if (rt_rq
->rt_nr_running
) {
854 if (!rt_rq_throttled(rt_rq
))
857 if (rt_rq
->rt_throttled
)
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
))
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
);
877 return rt_rq
->highest_prio
.curr
;
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
))
893 balance_runtime(rt_rq
);
894 runtime
= sched_rt_runtime(rt_rq
);
895 if (runtime
== RUNTIME_INF
)
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");
910 * In case we did anyway, make it go away,
911 * replenishment is a joke, since it will replenish us
917 if (rt_rq_throttled(rt_rq
)) {
918 sched_rt_rq_dequeue(rt_rq
);
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
;
936 if (curr
->sched_class
!= &rt_sched_class
)
939 delta_exec
= rq_clock_task(rq
) - curr
->se
.exec_start
;
940 if (unlikely((s64
)delta_exec
<= 0))
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())
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
))
965 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
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
)
980 BUG_ON(!rq
->nr_running
);
982 sub_nr_running(rq
, rt_rq
->rt_nr_running
);
983 rt_rq
->rt_queued
= 0;
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
)
995 if (rt_rq_throttled(rt_rq
) || !rt_rq
->rt_nr_running
)
998 add_nr_running(rq
, rt_rq
->rt_nr_running
);
999 rt_rq
->rt_queued
= 1;
1002 #if defined CONFIG_SMP
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
)
1016 if (rq
->online
&& prio
< prev_prio
)
1017 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, prio
);
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
)
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 */
1039 void inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
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
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
);
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
);
1078 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
1080 dec_rt_prio_smp(rt_rq
, prio
, prev_prio
);
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
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
++;
1099 start_rt_bandwidth(&rt_rq
->tg
->rt_bandwidth
);
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 */
1114 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1116 start_rt_bandwidth(&def_rt_bandwidth
);
1120 void dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
) {}
1122 #endif /* CONFIG_RT_GROUP_SCHED */
1125 unsigned int rt_se_nr_running(struct sched_rt_entity
*rt_se
)
1127 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1130 return group_rq
->rt_nr_running
;
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
);
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
1173 if (group_rq
&& (rt_rq_throttled(group_rq
) || !group_rq
->rt_nr_running
))
1177 list_add(&rt_se
->run_list
, queue
);
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
) {
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:
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
)
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
;
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.
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
);
1282 list_move(&rt_se
->run_list
, queue
);
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);
1305 static int find_lowest_rq(struct task_struct
*task
);
1308 select_task_rq_rt(struct task_struct
*p
, int cpu
, int sd_flag
, int flags
)
1310 struct task_struct
*curr
;
1313 /* For anything but wake ups, just return the task_cpu */
1314 if (sd_flag
!= SD_BALANCE_WAKE
&& sd_flag
!= SD_BALANCE_FORK
)
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
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.
1354 p
->prio
< cpu_rq(target
)->rt
.highest_prio
.curr
)
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
))
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
))
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);
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
) {
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
1415 if (p
->prio
== rq
->curr
->prio
&& !test_tsk_need_resched(rq
->curr
))
1416 check_preempt_equal_prio(rq
, p
);
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
;
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
);
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
;
1444 rt_se
= pick_next_rt_entity(rq
, rt_rq
);
1446 rt_rq
= group_rt_rq(rt_se
);
1449 p
= rt_task_of(rt_se
);
1450 p
->se
.exec_start
= rq_clock_task(rq
);
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
);
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
))
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
)
1488 if (!rt_rq
->rt_queued
)
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
);
1503 static void put_prev_task_rt(struct rq
*rq
, struct task_struct
*p
)
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
);
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
)))
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
))
1540 plist_for_each_entry(p
, head
, pushable_tasks
) {
1541 if (pick_rt_task(rq
, p
, cpu
))
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
))
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
))
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 */
1586 for_each_domain(cpu
, sd
) {
1587 if (sd
->flags
& SD_WAKE_AFFINE
) {
1591 * "this_cpu" is cheaper to preempt than a
1594 if (this_cpu
!= -1 &&
1595 cpumask_test_cpu(this_cpu
, sched_domain_span(sd
))) {
1600 best_cpu
= cpumask_first_and(lowest_mask
,
1601 sched_domain_span(sd
));
1602 if (best_cpu
< nr_cpu_ids
) {
1611 * And finally, if there were no matches within the domains
1612 * just give the caller *something* to work with from the compatible
1618 cpu
= cpumask_any(lowest_mask
);
1619 if (cpu
< nr_cpu_ids
)
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
;
1631 for (tries
= 0; tries
< RT_MAX_TRIES
; tries
++) {
1632 cpu
= find_lowest_rq(task
);
1634 if ((cpu
== -1) || (cpu
== rq
->cpu
))
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.
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
);
1669 /* If this rq is still suitable use it. */
1670 if (lowest_rq
->rt
.highest_prio
.curr
> task
->prio
)
1674 double_unlock_balance(rq
, 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
))
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
));
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
;
1712 if (!rq
->rt
.overloaded
)
1715 next_task
= pick_next_pushable_task(rq
);
1720 if (unlikely(next_task
== rq
->curr
)) {
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
)) {
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
);
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
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.
1762 /* No more tasks, just exit */
1766 * Something has shifted, try again.
1768 put_task_struct(next_task
);
1773 deactivate_task(rq
, next_task
, 0);
1774 set_task_cpu(next_task
, lowest_rq
->cpu
);
1775 activate_task(lowest_rq
, next_task
, 0);
1778 resched_curr(lowest_rq
);
1780 double_unlock_balance(rq
, lowest_rq
);
1783 put_task_struct(next_task
);
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
)
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
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.
1858 /* When rto_cpu is -1 this acts like cpumask_first() */
1859 cpu
= cpumask_next(rd
->rto_cpu
, rd
->rto_mask
);
1863 if (cpu
< nr_cpu_ids
)
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
)
1879 rd
->rto_loop
= next
;
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
)
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
))
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
);
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
);
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
);
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
);
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
;
1971 int rt_overload_count
= rt_overloaded(this_rq
);
1973 if (likely(!rt_overload_count
))
1977 * Match the barrier from rt_set_overloaded; this guarantees that if we
1978 * see overloaded we must also see the rto_mask bit.
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
))
1987 #ifdef HAVE_RT_PUSH_IPI
1988 if (sched_feat(RT_PUSH_IPI
)) {
1989 tell_cpu_to_push(this_rq
);
1994 for_each_cpu(cpu
, this_rq
->rd
->rto_mask
) {
1995 if (this_cpu
== cpu
)
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
)
2012 * We can potentially drop this_rq's lock in
2013 * double_lock_balance, and another CPU could
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
)
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
2056 double_unlock_balance(this_rq
, src_rq
);
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
))
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
2113 if (!task_on_rq_queued(p
) || rq
->rt
.rt_nr_running
)
2116 queue_pull_task(rq
);
2119 void __init
init_sched_rt_class(void)
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
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
) {
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
)))
2155 * Priority of the task has changed. This may cause
2156 * us to initiate a push or pull.
2159 prio_changed_rt(struct rq
*rq
, struct task_struct
*p
, int oldprio
)
2161 if (!task_on_rq_queued(p
))
2164 if (rq
->curr
== p
) {
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
2177 if (p
->prio
> rq
->rt
.highest_prio
.curr
)
2180 /* For UP simply resched on drop of prio */
2181 if (oldprio
< p
->prio
)
2183 #endif /* CONFIG_SMP */
2186 * This task is not running, but if it is
2187 * greater than the current running task
2190 if (p
->prio
< rq
->curr
->prio
)
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
) {
2206 if (p
->rt
.watchdog_stamp
!= jiffies
) {
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
;
2226 * RR tasks need a special form of timeslice management.
2227 * FIFO tasks have no timeslices.
2229 if (p
->policy
!= SCHED_RR
)
2232 if (--p
->rt
.time_slice
)
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);
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
;
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
,
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
,
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
)
2309 struct rt_rq
*rt_rq
;
2312 for_each_rt_rq(rt_rq
, iter
, cpu_rq(cpu
))
2313 print_rt_rq(m
, cpu
, rt_rq
);
2316 #endif /* CONFIG_SCHED_DEBUG */