1 // SPDX-License-Identifier: GPL-2.0
3 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
9 #include <linux/slab.h>
10 #include <linux/irq_work.h>
14 int sched_rr_timeslice
= RR_TIMESLICE
;
15 int sysctl_sched_rr_timeslice
= (MSEC_PER_SEC
/ HZ
) * RR_TIMESLICE
;
17 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
);
19 struct rt_bandwidth def_rt_bandwidth
;
21 static enum hrtimer_restart
sched_rt_period_timer(struct hrtimer
*timer
)
23 struct rt_bandwidth
*rt_b
=
24 container_of(timer
, struct rt_bandwidth
, rt_period_timer
);
28 raw_spin_lock(&rt_b
->rt_runtime_lock
);
30 overrun
= hrtimer_forward_now(timer
, rt_b
->rt_period
);
34 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
35 idle
= do_sched_rt_period_timer(rt_b
, overrun
);
36 raw_spin_lock(&rt_b
->rt_runtime_lock
);
39 rt_b
->rt_period_active
= 0;
40 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
42 return idle
? HRTIMER_NORESTART
: HRTIMER_RESTART
;
45 void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
)
47 rt_b
->rt_period
= ns_to_ktime(period
);
48 rt_b
->rt_runtime
= runtime
;
50 raw_spin_lock_init(&rt_b
->rt_runtime_lock
);
52 hrtimer_init(&rt_b
->rt_period_timer
,
53 CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
54 rt_b
->rt_period_timer
.function
= sched_rt_period_timer
;
57 static void start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
59 if (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
)
62 raw_spin_lock(&rt_b
->rt_runtime_lock
);
63 if (!rt_b
->rt_period_active
) {
64 rt_b
->rt_period_active
= 1;
66 * SCHED_DEADLINE updates the bandwidth, as a run away
67 * RT task with a DL task could hog a CPU. But DL does
68 * not reset the period. If a deadline task was running
69 * without an RT task running, it can cause RT tasks to
70 * throttle when they start up. Kick the timer right away
71 * to update the period.
73 hrtimer_forward_now(&rt_b
->rt_period_timer
, ns_to_ktime(0));
74 hrtimer_start_expires(&rt_b
->rt_period_timer
, HRTIMER_MODE_ABS_PINNED
);
76 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
79 void init_rt_rq(struct rt_rq
*rt_rq
)
81 struct rt_prio_array
*array
;
84 array
= &rt_rq
->active
;
85 for (i
= 0; i
< MAX_RT_PRIO
; i
++) {
86 INIT_LIST_HEAD(array
->queue
+ i
);
87 __clear_bit(i
, array
->bitmap
);
89 /* delimiter for bitsearch: */
90 __set_bit(MAX_RT_PRIO
, array
->bitmap
);
92 #if defined CONFIG_SMP
93 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
94 rt_rq
->highest_prio
.next
= MAX_RT_PRIO
;
95 rt_rq
->rt_nr_migratory
= 0;
96 rt_rq
->overloaded
= 0;
97 plist_head_init(&rt_rq
->pushable_tasks
);
98 #endif /* CONFIG_SMP */
99 /* We start is dequeued state, because no RT tasks are queued */
100 rt_rq
->rt_queued
= 0;
103 rt_rq
->rt_throttled
= 0;
104 rt_rq
->rt_runtime
= 0;
105 raw_spin_lock_init(&rt_rq
->rt_runtime_lock
);
108 #ifdef CONFIG_RT_GROUP_SCHED
109 static void destroy_rt_bandwidth(struct rt_bandwidth
*rt_b
)
111 hrtimer_cancel(&rt_b
->rt_period_timer
);
114 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
116 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
118 #ifdef CONFIG_SCHED_DEBUG
119 WARN_ON_ONCE(!rt_entity_is_task(rt_se
));
121 return container_of(rt_se
, struct task_struct
, rt
);
124 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
129 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
134 static inline struct rq
*rq_of_rt_se(struct sched_rt_entity
*rt_se
)
136 struct rt_rq
*rt_rq
= rt_se
->rt_rq
;
141 void free_rt_sched_group(struct task_group
*tg
)
146 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
148 for_each_possible_cpu(i
) {
159 void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
160 struct sched_rt_entity
*rt_se
, int cpu
,
161 struct sched_rt_entity
*parent
)
163 struct rq
*rq
= cpu_rq(cpu
);
165 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
166 rt_rq
->rt_nr_boosted
= 0;
170 tg
->rt_rq
[cpu
] = rt_rq
;
171 tg
->rt_se
[cpu
] = rt_se
;
177 rt_se
->rt_rq
= &rq
->rt
;
179 rt_se
->rt_rq
= parent
->my_q
;
182 rt_se
->parent
= parent
;
183 INIT_LIST_HEAD(&rt_se
->run_list
);
186 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
189 struct sched_rt_entity
*rt_se
;
192 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
195 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
199 init_rt_bandwidth(&tg
->rt_bandwidth
,
200 ktime_to_ns(def_rt_bandwidth
.rt_period
), 0);
202 for_each_possible_cpu(i
) {
203 rt_rq
= kzalloc_node(sizeof(struct rt_rq
),
204 GFP_KERNEL
, cpu_to_node(i
));
208 rt_se
= kzalloc_node(sizeof(struct sched_rt_entity
),
209 GFP_KERNEL
, cpu_to_node(i
));
214 rt_rq
->rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
215 init_tg_rt_entry(tg
, rt_rq
, rt_se
, i
, parent
->rt_se
[i
]);
226 #else /* CONFIG_RT_GROUP_SCHED */
228 #define rt_entity_is_task(rt_se) (1)
230 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
232 return container_of(rt_se
, struct task_struct
, rt
);
235 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
237 return container_of(rt_rq
, struct rq
, rt
);
240 static inline struct rq
*rq_of_rt_se(struct sched_rt_entity
*rt_se
)
242 struct task_struct
*p
= rt_task_of(rt_se
);
247 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
249 struct rq
*rq
= rq_of_rt_se(rt_se
);
254 void free_rt_sched_group(struct task_group
*tg
) { }
256 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
260 #endif /* CONFIG_RT_GROUP_SCHED */
264 static void pull_rt_task(struct rq
*this_rq
);
266 static inline bool need_pull_rt_task(struct rq
*rq
, struct task_struct
*prev
)
268 /* Try to pull RT tasks here if we lower this rq's prio */
269 return rq
->rt
.highest_prio
.curr
> prev
->prio
;
272 static inline int rt_overloaded(struct rq
*rq
)
274 return atomic_read(&rq
->rd
->rto_count
);
277 static inline void rt_set_overload(struct rq
*rq
)
282 cpumask_set_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
284 * Make sure the mask is visible before we set
285 * the overload count. That is checked to determine
286 * if we should look at the mask. It would be a shame
287 * if we looked at the mask, but the mask was not
290 * Matched by the barrier in pull_rt_task().
293 atomic_inc(&rq
->rd
->rto_count
);
296 static inline void rt_clear_overload(struct rq
*rq
)
301 /* the order here really doesn't matter */
302 atomic_dec(&rq
->rd
->rto_count
);
303 cpumask_clear_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
306 static void update_rt_migration(struct rt_rq
*rt_rq
)
308 if (rt_rq
->rt_nr_migratory
&& rt_rq
->rt_nr_total
> 1) {
309 if (!rt_rq
->overloaded
) {
310 rt_set_overload(rq_of_rt_rq(rt_rq
));
311 rt_rq
->overloaded
= 1;
313 } else if (rt_rq
->overloaded
) {
314 rt_clear_overload(rq_of_rt_rq(rt_rq
));
315 rt_rq
->overloaded
= 0;
319 static void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
321 struct task_struct
*p
;
323 if (!rt_entity_is_task(rt_se
))
326 p
= rt_task_of(rt_se
);
327 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
329 rt_rq
->rt_nr_total
++;
330 if (p
->nr_cpus_allowed
> 1)
331 rt_rq
->rt_nr_migratory
++;
333 update_rt_migration(rt_rq
);
336 static void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
338 struct task_struct
*p
;
340 if (!rt_entity_is_task(rt_se
))
343 p
= rt_task_of(rt_se
);
344 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
346 rt_rq
->rt_nr_total
--;
347 if (p
->nr_cpus_allowed
> 1)
348 rt_rq
->rt_nr_migratory
--;
350 update_rt_migration(rt_rq
);
353 static inline int has_pushable_tasks(struct rq
*rq
)
355 return !plist_head_empty(&rq
->rt
.pushable_tasks
);
358 static DEFINE_PER_CPU(struct callback_head
, rt_push_head
);
359 static DEFINE_PER_CPU(struct callback_head
, rt_pull_head
);
361 static void push_rt_tasks(struct rq
*);
362 static void pull_rt_task(struct rq
*);
364 static inline void queue_push_tasks(struct rq
*rq
)
366 if (!has_pushable_tasks(rq
))
369 queue_balance_callback(rq
, &per_cpu(rt_push_head
, rq
->cpu
), push_rt_tasks
);
372 static inline void queue_pull_task(struct rq
*rq
)
374 queue_balance_callback(rq
, &per_cpu(rt_pull_head
, rq
->cpu
), pull_rt_task
);
377 static void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
379 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
380 plist_node_init(&p
->pushable_tasks
, p
->prio
);
381 plist_add(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
383 /* Update the highest prio pushable task */
384 if (p
->prio
< rq
->rt
.highest_prio
.next
)
385 rq
->rt
.highest_prio
.next
= p
->prio
;
388 static void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
390 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
392 /* Update the new highest prio pushable task */
393 if (has_pushable_tasks(rq
)) {
394 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
395 struct task_struct
, pushable_tasks
);
396 rq
->rt
.highest_prio
.next
= p
->prio
;
398 rq
->rt
.highest_prio
.next
= MAX_RT_PRIO
;
403 static inline void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
407 static inline void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
412 void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
417 void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
421 static inline bool need_pull_rt_task(struct rq
*rq
, struct task_struct
*prev
)
426 static inline void pull_rt_task(struct rq
*this_rq
)
430 static inline void queue_push_tasks(struct rq
*rq
)
433 #endif /* CONFIG_SMP */
435 static void enqueue_top_rt_rq(struct rt_rq
*rt_rq
);
436 static void dequeue_top_rt_rq(struct rt_rq
*rt_rq
);
438 static inline int on_rt_rq(struct sched_rt_entity
*rt_se
)
443 #ifdef CONFIG_RT_GROUP_SCHED
445 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
450 return rt_rq
->rt_runtime
;
453 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
455 return ktime_to_ns(rt_rq
->tg
->rt_bandwidth
.rt_period
);
458 typedef struct task_group
*rt_rq_iter_t
;
460 static inline struct task_group
*next_task_group(struct task_group
*tg
)
463 tg
= list_entry_rcu(tg
->list
.next
,
464 typeof(struct task_group
), list
);
465 } while (&tg
->list
!= &task_groups
&& task_group_is_autogroup(tg
));
467 if (&tg
->list
== &task_groups
)
473 #define for_each_rt_rq(rt_rq, iter, rq) \
474 for (iter = container_of(&task_groups, typeof(*iter), list); \
475 (iter = next_task_group(iter)) && \
476 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
478 #define for_each_sched_rt_entity(rt_se) \
479 for (; rt_se; rt_se = rt_se->parent)
481 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
486 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
);
487 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
);
489 static void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
491 struct task_struct
*curr
= rq_of_rt_rq(rt_rq
)->curr
;
492 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
493 struct sched_rt_entity
*rt_se
;
495 int cpu
= cpu_of(rq
);
497 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
499 if (rt_rq
->rt_nr_running
) {
501 enqueue_top_rt_rq(rt_rq
);
502 else if (!on_rt_rq(rt_se
))
503 enqueue_rt_entity(rt_se
, 0);
505 if (rt_rq
->highest_prio
.curr
< curr
->prio
)
510 static void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
512 struct sched_rt_entity
*rt_se
;
513 int cpu
= cpu_of(rq_of_rt_rq(rt_rq
));
515 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
518 dequeue_top_rt_rq(rt_rq
);
519 else if (on_rt_rq(rt_se
))
520 dequeue_rt_entity(rt_se
, 0);
523 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
525 return rt_rq
->rt_throttled
&& !rt_rq
->rt_nr_boosted
;
528 static int rt_se_boosted(struct sched_rt_entity
*rt_se
)
530 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
531 struct task_struct
*p
;
534 return !!rt_rq
->rt_nr_boosted
;
536 p
= rt_task_of(rt_se
);
537 return p
->prio
!= p
->normal_prio
;
541 static inline const struct cpumask
*sched_rt_period_mask(void)
543 return this_rq()->rd
->span
;
546 static inline const struct cpumask
*sched_rt_period_mask(void)
548 return cpu_online_mask
;
553 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
555 return container_of(rt_b
, struct task_group
, rt_bandwidth
)->rt_rq
[cpu
];
558 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
560 return &rt_rq
->tg
->rt_bandwidth
;
563 #else /* !CONFIG_RT_GROUP_SCHED */
565 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
567 return rt_rq
->rt_runtime
;
570 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
572 return ktime_to_ns(def_rt_bandwidth
.rt_period
);
575 typedef struct rt_rq
*rt_rq_iter_t
;
577 #define for_each_rt_rq(rt_rq, iter, rq) \
578 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
580 #define for_each_sched_rt_entity(rt_se) \
581 for (; rt_se; rt_se = NULL)
583 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
588 static inline void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
590 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
592 if (!rt_rq
->rt_nr_running
)
595 enqueue_top_rt_rq(rt_rq
);
599 static inline void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
601 dequeue_top_rt_rq(rt_rq
);
604 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
606 return rt_rq
->rt_throttled
;
609 static inline const struct cpumask
*sched_rt_period_mask(void)
611 return cpu_online_mask
;
615 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
617 return &cpu_rq(cpu
)->rt
;
620 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
622 return &def_rt_bandwidth
;
625 #endif /* CONFIG_RT_GROUP_SCHED */
627 bool sched_rt_bandwidth_account(struct rt_rq
*rt_rq
)
629 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
631 return (hrtimer_active(&rt_b
->rt_period_timer
) ||
632 rt_rq
->rt_time
< rt_b
->rt_runtime
);
637 * We ran out of runtime, see if we can borrow some from our neighbours.
639 static void do_balance_runtime(struct rt_rq
*rt_rq
)
641 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
642 struct root_domain
*rd
= rq_of_rt_rq(rt_rq
)->rd
;
646 weight
= cpumask_weight(rd
->span
);
648 raw_spin_lock(&rt_b
->rt_runtime_lock
);
649 rt_period
= ktime_to_ns(rt_b
->rt_period
);
650 for_each_cpu(i
, rd
->span
) {
651 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
657 raw_spin_lock(&iter
->rt_runtime_lock
);
659 * Either all rqs have inf runtime and there's nothing to steal
660 * or __disable_runtime() below sets a specific rq to inf to
661 * indicate its been disabled and disalow stealing.
663 if (iter
->rt_runtime
== RUNTIME_INF
)
667 * From runqueues with spare time, take 1/n part of their
668 * spare time, but no more than our period.
670 diff
= iter
->rt_runtime
- iter
->rt_time
;
672 diff
= div_u64((u64
)diff
, weight
);
673 if (rt_rq
->rt_runtime
+ diff
> rt_period
)
674 diff
= rt_period
- rt_rq
->rt_runtime
;
675 iter
->rt_runtime
-= diff
;
676 rt_rq
->rt_runtime
+= diff
;
677 if (rt_rq
->rt_runtime
== rt_period
) {
678 raw_spin_unlock(&iter
->rt_runtime_lock
);
683 raw_spin_unlock(&iter
->rt_runtime_lock
);
685 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
689 * Ensure this RQ takes back all the runtime it lend to its neighbours.
691 static void __disable_runtime(struct rq
*rq
)
693 struct root_domain
*rd
= rq
->rd
;
697 if (unlikely(!scheduler_running
))
700 for_each_rt_rq(rt_rq
, iter
, rq
) {
701 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
705 raw_spin_lock(&rt_b
->rt_runtime_lock
);
706 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
708 * Either we're all inf and nobody needs to borrow, or we're
709 * already disabled and thus have nothing to do, or we have
710 * exactly the right amount of runtime to take out.
712 if (rt_rq
->rt_runtime
== RUNTIME_INF
||
713 rt_rq
->rt_runtime
== rt_b
->rt_runtime
)
715 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
718 * Calculate the difference between what we started out with
719 * and what we current have, that's the amount of runtime
720 * we lend and now have to reclaim.
722 want
= rt_b
->rt_runtime
- rt_rq
->rt_runtime
;
725 * Greedy reclaim, take back as much as we can.
727 for_each_cpu(i
, rd
->span
) {
728 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
732 * Can't reclaim from ourselves or disabled runqueues.
734 if (iter
== rt_rq
|| iter
->rt_runtime
== RUNTIME_INF
)
737 raw_spin_lock(&iter
->rt_runtime_lock
);
739 diff
= min_t(s64
, iter
->rt_runtime
, want
);
740 iter
->rt_runtime
-= diff
;
743 iter
->rt_runtime
-= want
;
746 raw_spin_unlock(&iter
->rt_runtime_lock
);
752 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
754 * We cannot be left wanting - that would mean some runtime
755 * leaked out of the system.
760 * Disable all the borrow logic by pretending we have inf
761 * runtime - in which case borrowing doesn't make sense.
763 rt_rq
->rt_runtime
= RUNTIME_INF
;
764 rt_rq
->rt_throttled
= 0;
765 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
766 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
768 /* Make rt_rq available for pick_next_task() */
769 sched_rt_rq_enqueue(rt_rq
);
773 static void __enable_runtime(struct rq
*rq
)
778 if (unlikely(!scheduler_running
))
782 * Reset each runqueue's bandwidth settings
784 for_each_rt_rq(rt_rq
, iter
, rq
) {
785 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
787 raw_spin_lock(&rt_b
->rt_runtime_lock
);
788 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
789 rt_rq
->rt_runtime
= rt_b
->rt_runtime
;
791 rt_rq
->rt_throttled
= 0;
792 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
793 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
797 static void balance_runtime(struct rt_rq
*rt_rq
)
799 if (!sched_feat(RT_RUNTIME_SHARE
))
802 if (rt_rq
->rt_time
> rt_rq
->rt_runtime
) {
803 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
804 do_balance_runtime(rt_rq
);
805 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
808 #else /* !CONFIG_SMP */
809 static inline void balance_runtime(struct rt_rq
*rt_rq
) {}
810 #endif /* CONFIG_SMP */
812 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
)
814 int i
, idle
= 1, throttled
= 0;
815 const struct cpumask
*span
;
817 span
= sched_rt_period_mask();
818 #ifdef CONFIG_RT_GROUP_SCHED
820 * FIXME: isolated CPUs should really leave the root task group,
821 * whether they are isolcpus or were isolated via cpusets, lest
822 * the timer run on a CPU which does not service all runqueues,
823 * potentially leaving other CPUs indefinitely throttled. If
824 * isolation is really required, the user will turn the throttle
825 * off to kill the perturbations it causes anyway. Meanwhile,
826 * this maintains functionality for boot and/or troubleshooting.
828 if (rt_b
== &root_task_group
.rt_bandwidth
)
829 span
= cpu_online_mask
;
831 for_each_cpu(i
, span
) {
833 struct rt_rq
*rt_rq
= sched_rt_period_rt_rq(rt_b
, i
);
834 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
838 * When span == cpu_online_mask, taking each rq->lock
839 * can be time-consuming. Try to avoid it when possible.
841 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
842 skip
= !rt_rq
->rt_time
&& !rt_rq
->rt_nr_running
;
843 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
847 raw_spin_lock(&rq
->lock
);
848 if (rt_rq
->rt_time
) {
851 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
852 if (rt_rq
->rt_throttled
)
853 balance_runtime(rt_rq
);
854 runtime
= rt_rq
->rt_runtime
;
855 rt_rq
->rt_time
-= min(rt_rq
->rt_time
, overrun
*runtime
);
856 if (rt_rq
->rt_throttled
&& rt_rq
->rt_time
< runtime
) {
857 rt_rq
->rt_throttled
= 0;
861 * When we're idle and a woken (rt) task is
862 * throttled check_preempt_curr() will set
863 * skip_update and the time between the wakeup
864 * and this unthrottle will get accounted as
867 if (rt_rq
->rt_nr_running
&& rq
->curr
== rq
->idle
)
868 rq_clock_skip_update(rq
, false);
870 if (rt_rq
->rt_time
|| rt_rq
->rt_nr_running
)
872 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
873 } else if (rt_rq
->rt_nr_running
) {
875 if (!rt_rq_throttled(rt_rq
))
878 if (rt_rq
->rt_throttled
)
882 sched_rt_rq_enqueue(rt_rq
);
883 raw_spin_unlock(&rq
->lock
);
886 if (!throttled
&& (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
))
892 static inline int rt_se_prio(struct sched_rt_entity
*rt_se
)
894 #ifdef CONFIG_RT_GROUP_SCHED
895 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
898 return rt_rq
->highest_prio
.curr
;
901 return rt_task_of(rt_se
)->prio
;
904 static int sched_rt_runtime_exceeded(struct rt_rq
*rt_rq
)
906 u64 runtime
= sched_rt_runtime(rt_rq
);
908 if (rt_rq
->rt_throttled
)
909 return rt_rq_throttled(rt_rq
);
911 if (runtime
>= sched_rt_period(rt_rq
))
914 balance_runtime(rt_rq
);
915 runtime
= sched_rt_runtime(rt_rq
);
916 if (runtime
== RUNTIME_INF
)
919 if (rt_rq
->rt_time
> runtime
) {
920 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
923 * Don't actually throttle groups that have no runtime assigned
924 * but accrue some time due to boosting.
926 if (likely(rt_b
->rt_runtime
)) {
927 rt_rq
->rt_throttled
= 1;
928 printk_deferred_once("sched: RT throttling activated\n");
931 * In case we did anyway, make it go away,
932 * replenishment is a joke, since it will replenish us
938 if (rt_rq_throttled(rt_rq
)) {
939 sched_rt_rq_dequeue(rt_rq
);
948 * Update the current task's runtime statistics. Skip current tasks that
949 * are not in our scheduling class.
951 static void update_curr_rt(struct rq
*rq
)
953 struct task_struct
*curr
= rq
->curr
;
954 struct sched_rt_entity
*rt_se
= &curr
->rt
;
957 if (curr
->sched_class
!= &rt_sched_class
)
960 delta_exec
= rq_clock_task(rq
) - curr
->se
.exec_start
;
961 if (unlikely((s64
)delta_exec
<= 0))
964 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
965 cpufreq_update_util(rq
, SCHED_CPUFREQ_RT
);
967 schedstat_set(curr
->se
.statistics
.exec_max
,
968 max(curr
->se
.statistics
.exec_max
, delta_exec
));
970 curr
->se
.sum_exec_runtime
+= delta_exec
;
971 account_group_exec_runtime(curr
, delta_exec
);
973 curr
->se
.exec_start
= rq_clock_task(rq
);
974 cpuacct_charge(curr
, delta_exec
);
976 sched_rt_avg_update(rq
, delta_exec
);
978 if (!rt_bandwidth_enabled())
981 for_each_sched_rt_entity(rt_se
) {
982 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
984 if (sched_rt_runtime(rt_rq
) != RUNTIME_INF
) {
985 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
986 rt_rq
->rt_time
+= delta_exec
;
987 if (sched_rt_runtime_exceeded(rt_rq
))
989 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
995 dequeue_top_rt_rq(struct rt_rq
*rt_rq
)
997 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
999 BUG_ON(&rq
->rt
!= rt_rq
);
1001 if (!rt_rq
->rt_queued
)
1004 BUG_ON(!rq
->nr_running
);
1006 sub_nr_running(rq
, rt_rq
->rt_nr_running
);
1007 rt_rq
->rt_queued
= 0;
1011 enqueue_top_rt_rq(struct rt_rq
*rt_rq
)
1013 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1015 BUG_ON(&rq
->rt
!= rt_rq
);
1017 if (rt_rq
->rt_queued
)
1019 if (rt_rq_throttled(rt_rq
) || !rt_rq
->rt_nr_running
)
1022 add_nr_running(rq
, rt_rq
->rt_nr_running
);
1023 rt_rq
->rt_queued
= 1;
1026 #if defined CONFIG_SMP
1029 inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1031 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1033 #ifdef CONFIG_RT_GROUP_SCHED
1035 * Change rq's cpupri only if rt_rq is the top queue.
1037 if (&rq
->rt
!= rt_rq
)
1040 if (rq
->online
&& prio
< prev_prio
)
1041 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, prio
);
1045 dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1047 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1049 #ifdef CONFIG_RT_GROUP_SCHED
1051 * Change rq's cpupri only if rt_rq is the top queue.
1053 if (&rq
->rt
!= rt_rq
)
1056 if (rq
->online
&& rt_rq
->highest_prio
.curr
!= prev_prio
)
1057 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rt_rq
->highest_prio
.curr
);
1060 #else /* CONFIG_SMP */
1063 void inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1065 void dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1067 #endif /* CONFIG_SMP */
1069 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1071 inc_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1073 int prev_prio
= rt_rq
->highest_prio
.curr
;
1075 if (prio
< prev_prio
)
1076 rt_rq
->highest_prio
.curr
= prio
;
1078 inc_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1082 dec_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1084 int prev_prio
= rt_rq
->highest_prio
.curr
;
1086 if (rt_rq
->rt_nr_running
) {
1088 WARN_ON(prio
< prev_prio
);
1091 * This may have been our highest task, and therefore
1092 * we may have some recomputation to do
1094 if (prio
== prev_prio
) {
1095 struct rt_prio_array
*array
= &rt_rq
->active
;
1097 rt_rq
->highest_prio
.curr
=
1098 sched_find_first_bit(array
->bitmap
);
1102 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
1104 dec_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1109 static inline void inc_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1110 static inline void dec_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1112 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1114 #ifdef CONFIG_RT_GROUP_SCHED
1117 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1119 if (rt_se_boosted(rt_se
))
1120 rt_rq
->rt_nr_boosted
++;
1123 start_rt_bandwidth(&rt_rq
->tg
->rt_bandwidth
);
1127 dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1129 if (rt_se_boosted(rt_se
))
1130 rt_rq
->rt_nr_boosted
--;
1132 WARN_ON(!rt_rq
->rt_nr_running
&& rt_rq
->rt_nr_boosted
);
1135 #else /* CONFIG_RT_GROUP_SCHED */
1138 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1140 start_rt_bandwidth(&def_rt_bandwidth
);
1144 void dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
) {}
1146 #endif /* CONFIG_RT_GROUP_SCHED */
1149 unsigned int rt_se_nr_running(struct sched_rt_entity
*rt_se
)
1151 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1154 return group_rq
->rt_nr_running
;
1160 unsigned int rt_se_rr_nr_running(struct sched_rt_entity
*rt_se
)
1162 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1163 struct task_struct
*tsk
;
1166 return group_rq
->rr_nr_running
;
1168 tsk
= rt_task_of(rt_se
);
1170 return (tsk
->policy
== SCHED_RR
) ? 1 : 0;
1174 void inc_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1176 int prio
= rt_se_prio(rt_se
);
1178 WARN_ON(!rt_prio(prio
));
1179 rt_rq
->rt_nr_running
+= rt_se_nr_running(rt_se
);
1180 rt_rq
->rr_nr_running
+= rt_se_rr_nr_running(rt_se
);
1182 inc_rt_prio(rt_rq
, prio
);
1183 inc_rt_migration(rt_se
, rt_rq
);
1184 inc_rt_group(rt_se
, rt_rq
);
1188 void dec_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1190 WARN_ON(!rt_prio(rt_se_prio(rt_se
)));
1191 WARN_ON(!rt_rq
->rt_nr_running
);
1192 rt_rq
->rt_nr_running
-= rt_se_nr_running(rt_se
);
1193 rt_rq
->rr_nr_running
-= rt_se_rr_nr_running(rt_se
);
1195 dec_rt_prio(rt_rq
, rt_se_prio(rt_se
));
1196 dec_rt_migration(rt_se
, rt_rq
);
1197 dec_rt_group(rt_se
, rt_rq
);
1201 * Change rt_se->run_list location unless SAVE && !MOVE
1203 * assumes ENQUEUE/DEQUEUE flags match
1205 static inline bool move_entity(unsigned int flags
)
1207 if ((flags
& (DEQUEUE_SAVE
| DEQUEUE_MOVE
)) == DEQUEUE_SAVE
)
1213 static void __delist_rt_entity(struct sched_rt_entity
*rt_se
, struct rt_prio_array
*array
)
1215 list_del_init(&rt_se
->run_list
);
1217 if (list_empty(array
->queue
+ rt_se_prio(rt_se
)))
1218 __clear_bit(rt_se_prio(rt_se
), array
->bitmap
);
1223 static void __enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1225 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1226 struct rt_prio_array
*array
= &rt_rq
->active
;
1227 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1228 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1231 * Don't enqueue the group if its throttled, or when empty.
1232 * The latter is a consequence of the former when a child group
1233 * get throttled and the current group doesn't have any other
1236 if (group_rq
&& (rt_rq_throttled(group_rq
) || !group_rq
->rt_nr_running
)) {
1238 __delist_rt_entity(rt_se
, array
);
1242 if (move_entity(flags
)) {
1243 WARN_ON_ONCE(rt_se
->on_list
);
1244 if (flags
& ENQUEUE_HEAD
)
1245 list_add(&rt_se
->run_list
, queue
);
1247 list_add_tail(&rt_se
->run_list
, queue
);
1249 __set_bit(rt_se_prio(rt_se
), array
->bitmap
);
1254 inc_rt_tasks(rt_se
, rt_rq
);
1257 static void __dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1259 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1260 struct rt_prio_array
*array
= &rt_rq
->active
;
1262 if (move_entity(flags
)) {
1263 WARN_ON_ONCE(!rt_se
->on_list
);
1264 __delist_rt_entity(rt_se
, array
);
1268 dec_rt_tasks(rt_se
, rt_rq
);
1272 * Because the prio of an upper entry depends on the lower
1273 * entries, we must remove entries top - down.
1275 static void dequeue_rt_stack(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1277 struct sched_rt_entity
*back
= NULL
;
1279 for_each_sched_rt_entity(rt_se
) {
1284 dequeue_top_rt_rq(rt_rq_of_se(back
));
1286 for (rt_se
= back
; rt_se
; rt_se
= rt_se
->back
) {
1287 if (on_rt_rq(rt_se
))
1288 __dequeue_rt_entity(rt_se
, flags
);
1292 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1294 struct rq
*rq
= rq_of_rt_se(rt_se
);
1296 dequeue_rt_stack(rt_se
, flags
);
1297 for_each_sched_rt_entity(rt_se
)
1298 __enqueue_rt_entity(rt_se
, flags
);
1299 enqueue_top_rt_rq(&rq
->rt
);
1302 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1304 struct rq
*rq
= rq_of_rt_se(rt_se
);
1306 dequeue_rt_stack(rt_se
, flags
);
1308 for_each_sched_rt_entity(rt_se
) {
1309 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1311 if (rt_rq
&& rt_rq
->rt_nr_running
)
1312 __enqueue_rt_entity(rt_se
, flags
);
1314 enqueue_top_rt_rq(&rq
->rt
);
1318 * Adding/removing a task to/from a priority array:
1321 enqueue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1323 struct sched_rt_entity
*rt_se
= &p
->rt
;
1325 if (flags
& ENQUEUE_WAKEUP
)
1328 enqueue_rt_entity(rt_se
, flags
);
1329 walt_inc_cumulative_runnable_avg(rq
, p
);
1331 if (!task_current(rq
, p
) && p
->nr_cpus_allowed
> 1)
1332 enqueue_pushable_task(rq
, p
);
1335 static void dequeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1337 struct sched_rt_entity
*rt_se
= &p
->rt
;
1340 dequeue_rt_entity(rt_se
, flags
);
1341 walt_dec_cumulative_runnable_avg(rq
, p
);
1343 dequeue_pushable_task(rq
, p
);
1347 * Put task to the head or the end of the run list without the overhead of
1348 * dequeue followed by enqueue.
1351 requeue_rt_entity(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
, int head
)
1353 if (on_rt_rq(rt_se
)) {
1354 struct rt_prio_array
*array
= &rt_rq
->active
;
1355 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1358 list_move(&rt_se
->run_list
, queue
);
1360 list_move_tail(&rt_se
->run_list
, queue
);
1364 static void requeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int head
)
1366 struct sched_rt_entity
*rt_se
= &p
->rt
;
1367 struct rt_rq
*rt_rq
;
1369 for_each_sched_rt_entity(rt_se
) {
1370 rt_rq
= rt_rq_of_se(rt_se
);
1371 requeue_rt_entity(rt_rq
, rt_se
, head
);
1375 static void yield_task_rt(struct rq
*rq
)
1377 requeue_task_rt(rq
, rq
->curr
, 0);
1381 static int find_lowest_rq(struct task_struct
*task
);
1384 select_task_rq_rt(struct task_struct
*p
, int cpu
, int sd_flag
, int flags
,
1385 int sibling_count_hint
)
1387 struct task_struct
*curr
;
1390 /* For anything but wake ups, just return the task_cpu */
1391 if (sd_flag
!= SD_BALANCE_WAKE
&& sd_flag
!= SD_BALANCE_FORK
)
1397 curr
= READ_ONCE(rq
->curr
); /* unlocked access */
1400 * If the current task on @p's runqueue is an RT task, then
1401 * try to see if we can wake this RT task up on another
1402 * runqueue. Otherwise simply start this RT task
1403 * on its current runqueue.
1405 * We want to avoid overloading runqueues. If the woken
1406 * task is a higher priority, then it will stay on this CPU
1407 * and the lower prio task should be moved to another CPU.
1408 * Even though this will probably make the lower prio task
1409 * lose its cache, we do not want to bounce a higher task
1410 * around just because it gave up its CPU, perhaps for a
1413 * For equal prio tasks, we just let the scheduler sort it out.
1415 * Otherwise, just let it ride on the affined RQ and the
1416 * post-schedule router will push the preempted task away
1418 * This test is optimistic, if we get it wrong the load-balancer
1419 * will have to sort it out.
1421 if (curr
&& unlikely(rt_task(curr
)) &&
1422 (curr
->nr_cpus_allowed
< 2 ||
1423 curr
->prio
<= p
->prio
)) {
1424 int target
= find_lowest_rq(p
);
1427 * Don't bother moving it if the destination CPU is
1428 * not running a lower priority task.
1431 p
->prio
< cpu_rq(target
)->rt
.highest_prio
.curr
)
1440 static void check_preempt_equal_prio(struct rq
*rq
, struct task_struct
*p
)
1443 * Current can't be migrated, useless to reschedule,
1444 * let's hope p can move out.
1446 if (rq
->curr
->nr_cpus_allowed
== 1 ||
1447 !cpupri_find(&rq
->rd
->cpupri
, rq
->curr
, NULL
))
1451 * p is migratable, so let's not schedule it and
1452 * see if it is pushed or pulled somewhere else.
1454 if (p
->nr_cpus_allowed
!= 1
1455 && cpupri_find(&rq
->rd
->cpupri
, p
, NULL
))
1459 * There appears to be other cpus that can accept
1460 * current and none to run 'p', so lets reschedule
1461 * to try and push current away:
1463 requeue_task_rt(rq
, p
, 1);
1467 #endif /* CONFIG_SMP */
1470 * Preempt the current task with a newly woken task if needed:
1472 static void check_preempt_curr_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1474 if (p
->prio
< rq
->curr
->prio
) {
1483 * - the newly woken task is of equal priority to the current task
1484 * - the newly woken task is non-migratable while current is migratable
1485 * - current will be preempted on the next reschedule
1487 * we should check to see if current can readily move to a different
1488 * cpu. If so, we will reschedule to allow the push logic to try
1489 * to move current somewhere else, making room for our non-migratable
1492 if (p
->prio
== rq
->curr
->prio
&& !test_tsk_need_resched(rq
->curr
))
1493 check_preempt_equal_prio(rq
, p
);
1497 static struct sched_rt_entity
*pick_next_rt_entity(struct rq
*rq
,
1498 struct rt_rq
*rt_rq
)
1500 struct rt_prio_array
*array
= &rt_rq
->active
;
1501 struct sched_rt_entity
*next
= NULL
;
1502 struct list_head
*queue
;
1505 idx
= sched_find_first_bit(array
->bitmap
);
1506 BUG_ON(idx
>= MAX_RT_PRIO
);
1508 queue
= array
->queue
+ idx
;
1509 next
= list_entry(queue
->next
, struct sched_rt_entity
, run_list
);
1514 static struct task_struct
*_pick_next_task_rt(struct rq
*rq
)
1516 struct sched_rt_entity
*rt_se
;
1517 struct task_struct
*p
;
1518 struct rt_rq
*rt_rq
= &rq
->rt
;
1521 rt_se
= pick_next_rt_entity(rq
, rt_rq
);
1523 rt_rq
= group_rt_rq(rt_se
);
1526 p
= rt_task_of(rt_se
);
1527 p
->se
.exec_start
= rq_clock_task(rq
);
1532 extern int update_rt_rq_load_avg(u64 now
, int cpu
, struct rt_rq
*rt_rq
, int running
);
1534 static struct task_struct
*
1535 pick_next_task_rt(struct rq
*rq
, struct task_struct
*prev
, struct rq_flags
*rf
)
1537 struct task_struct
*p
;
1538 struct rt_rq
*rt_rq
= &rq
->rt
;
1540 if (need_pull_rt_task(rq
, prev
)) {
1542 * This is OK, because current is on_cpu, which avoids it being
1543 * picked for load-balance and preemption/IRQs are still
1544 * disabled avoiding further scheduler activity on it and we're
1545 * being very careful to re-start the picking loop.
1547 rq_unpin_lock(rq
, rf
);
1549 rq_repin_lock(rq
, rf
);
1551 * pull_rt_task() can drop (and re-acquire) rq->lock; this
1552 * means a dl or stop task can slip in, in which case we need
1553 * to re-start task selection.
1555 if (unlikely((rq
->stop
&& task_on_rq_queued(rq
->stop
)) ||
1556 rq
->dl
.dl_nr_running
))
1561 * We may dequeue prev's rt_rq in put_prev_task().
1562 * So, we update time before rt_nr_running check.
1564 if (prev
->sched_class
== &rt_sched_class
)
1567 if (!rt_rq
->rt_queued
)
1570 put_prev_task(rq
, prev
);
1572 p
= _pick_next_task_rt(rq
);
1574 /* The running task is never eligible for pushing */
1575 dequeue_pushable_task(rq
, p
);
1577 queue_push_tasks(rq
);
1580 update_rt_rq_load_avg(rq_clock_task(rq
), cpu_of(rq
), rt_rq
,
1581 rq
->curr
->sched_class
== &rt_sched_class
);
1586 static void put_prev_task_rt(struct rq
*rq
, struct task_struct
*p
)
1590 update_rt_rq_load_avg(rq_clock_task(rq
), cpu_of(rq
), &rq
->rt
, 1);
1593 * The previous task needs to be made eligible for pushing
1594 * if it is still active
1596 if (on_rt_rq(&p
->rt
) && p
->nr_cpus_allowed
> 1)
1597 enqueue_pushable_task(rq
, p
);
1602 /* Only try algorithms three times */
1603 #define RT_MAX_TRIES 3
1605 static int pick_rt_task(struct rq
*rq
, struct task_struct
*p
, int cpu
)
1607 if (!task_running(rq
, p
) &&
1608 cpumask_test_cpu(cpu
, &p
->cpus_allowed
))
1614 * Return the highest pushable rq's task, which is suitable to be executed
1615 * on the cpu, NULL otherwise
1617 static struct task_struct
*pick_highest_pushable_task(struct rq
*rq
, int cpu
)
1619 struct plist_head
*head
= &rq
->rt
.pushable_tasks
;
1620 struct task_struct
*p
;
1622 if (!has_pushable_tasks(rq
))
1625 plist_for_each_entry(p
, head
, pushable_tasks
) {
1626 if (pick_rt_task(rq
, p
, cpu
))
1633 static DEFINE_PER_CPU(cpumask_var_t
, local_cpu_mask
);
1635 static int find_lowest_rq(struct task_struct
*task
)
1637 struct sched_domain
*sd
;
1638 struct cpumask
*lowest_mask
= this_cpu_cpumask_var_ptr(local_cpu_mask
);
1639 int this_cpu
= smp_processor_id();
1640 int cpu
= task_cpu(task
);
1642 /* Make sure the mask is initialized first */
1643 if (unlikely(!lowest_mask
))
1646 if (task
->nr_cpus_allowed
== 1)
1647 return -1; /* No other targets possible */
1649 if (!cpupri_find(&task_rq(task
)->rd
->cpupri
, task
, lowest_mask
))
1650 return -1; /* No targets found */
1653 * At this point we have built a mask of cpus representing the
1654 * lowest priority tasks in the system. Now we want to elect
1655 * the best one based on our affinity and topology.
1657 * We prioritize the last cpu that the task executed on since
1658 * it is most likely cache-hot in that location.
1660 if (cpumask_test_cpu(cpu
, lowest_mask
))
1664 * Otherwise, we consult the sched_domains span maps to figure
1665 * out which cpu is logically closest to our hot cache data.
1667 if (!cpumask_test_cpu(this_cpu
, lowest_mask
))
1668 this_cpu
= -1; /* Skip this_cpu opt if not among lowest */
1671 for_each_domain(cpu
, sd
) {
1672 if (sd
->flags
& SD_WAKE_AFFINE
) {
1676 * "this_cpu" is cheaper to preempt than a
1679 if (this_cpu
!= -1 &&
1680 cpumask_test_cpu(this_cpu
, sched_domain_span(sd
))) {
1685 best_cpu
= cpumask_first_and(lowest_mask
,
1686 sched_domain_span(sd
));
1687 if (best_cpu
< nr_cpu_ids
) {
1696 * And finally, if there were no matches within the domains
1697 * just give the caller *something* to work with from the compatible
1703 cpu
= cpumask_any(lowest_mask
);
1704 if (cpu
< nr_cpu_ids
)
1709 /* Will lock the rq it finds */
1710 static struct rq
*find_lock_lowest_rq(struct task_struct
*task
, struct rq
*rq
)
1712 struct rq
*lowest_rq
= NULL
;
1716 for (tries
= 0; tries
< RT_MAX_TRIES
; tries
++) {
1717 cpu
= find_lowest_rq(task
);
1719 if ((cpu
== -1) || (cpu
== rq
->cpu
))
1722 lowest_rq
= cpu_rq(cpu
);
1724 if (lowest_rq
->rt
.highest_prio
.curr
<= task
->prio
) {
1726 * Target rq has tasks of equal or higher priority,
1727 * retrying does not release any lock and is unlikely
1728 * to yield a different result.
1734 /* if the prio of this runqueue changed, try again */
1735 if (double_lock_balance(rq
, lowest_rq
)) {
1737 * We had to unlock the run queue. In
1738 * the mean time, task could have
1739 * migrated already or had its affinity changed.
1740 * Also make sure that it wasn't scheduled on its rq.
1742 if (unlikely(task_rq(task
) != rq
||
1743 !cpumask_test_cpu(lowest_rq
->cpu
, &task
->cpus_allowed
) ||
1744 task_running(rq
, task
) ||
1746 !task_on_rq_queued(task
))) {
1748 double_unlock_balance(rq
, lowest_rq
);
1754 /* If this rq is still suitable use it. */
1755 if (lowest_rq
->rt
.highest_prio
.curr
> task
->prio
)
1759 double_unlock_balance(rq
, lowest_rq
);
1766 static struct task_struct
*pick_next_pushable_task(struct rq
*rq
)
1768 struct task_struct
*p
;
1770 if (!has_pushable_tasks(rq
))
1773 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
1774 struct task_struct
, pushable_tasks
);
1776 BUG_ON(rq
->cpu
!= task_cpu(p
));
1777 BUG_ON(task_current(rq
, p
));
1778 BUG_ON(p
->nr_cpus_allowed
<= 1);
1780 BUG_ON(!task_on_rq_queued(p
));
1781 BUG_ON(!rt_task(p
));
1787 * If the current CPU has more than one RT task, see if the non
1788 * running task can migrate over to a CPU that is running a task
1789 * of lesser priority.
1791 static int push_rt_task(struct rq
*rq
)
1793 struct task_struct
*next_task
;
1794 struct rq
*lowest_rq
;
1797 if (!rq
->rt
.overloaded
)
1800 next_task
= pick_next_pushable_task(rq
);
1805 if (unlikely(next_task
== rq
->curr
)) {
1811 * It's possible that the next_task slipped in of
1812 * higher priority than current. If that's the case
1813 * just reschedule current.
1815 if (unlikely(next_task
->prio
< rq
->curr
->prio
)) {
1820 /* We might release rq lock */
1821 get_task_struct(next_task
);
1823 /* find_lock_lowest_rq locks the rq if found */
1824 lowest_rq
= find_lock_lowest_rq(next_task
, rq
);
1826 struct task_struct
*task
;
1828 * find_lock_lowest_rq releases rq->lock
1829 * so it is possible that next_task has migrated.
1831 * We need to make sure that the task is still on the same
1832 * run-queue and is also still the next task eligible for
1835 task
= pick_next_pushable_task(rq
);
1836 if (task
== next_task
) {
1838 * The task hasn't migrated, and is still the next
1839 * eligible task, but we failed to find a run-queue
1840 * to push it to. Do not retry in this case, since
1841 * other cpus will pull from us when ready.
1847 /* No more tasks, just exit */
1851 * Something has shifted, try again.
1853 put_task_struct(next_task
);
1858 deactivate_task(rq
, next_task
, 0);
1859 next_task
->on_rq
= TASK_ON_RQ_MIGRATING
;
1860 set_task_cpu(next_task
, lowest_rq
->cpu
);
1861 next_task
->on_rq
= TASK_ON_RQ_QUEUED
;
1862 activate_task(lowest_rq
, next_task
, 0);
1865 resched_curr(lowest_rq
);
1867 double_unlock_balance(rq
, lowest_rq
);
1870 put_task_struct(next_task
);
1875 static void push_rt_tasks(struct rq
*rq
)
1877 /* push_rt_task will return true if it moved an RT */
1878 while (push_rt_task(rq
))
1882 #ifdef HAVE_RT_PUSH_IPI
1885 * When a high priority task schedules out from a CPU and a lower priority
1886 * task is scheduled in, a check is made to see if there's any RT tasks
1887 * on other CPUs that are waiting to run because a higher priority RT task
1888 * is currently running on its CPU. In this case, the CPU with multiple RT
1889 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
1890 * up that may be able to run one of its non-running queued RT tasks.
1892 * All CPUs with overloaded RT tasks need to be notified as there is currently
1893 * no way to know which of these CPUs have the highest priority task waiting
1894 * to run. Instead of trying to take a spinlock on each of these CPUs,
1895 * which has shown to cause large latency when done on machines with many
1896 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
1897 * RT tasks waiting to run.
1899 * Just sending an IPI to each of the CPUs is also an issue, as on large
1900 * count CPU machines, this can cause an IPI storm on a CPU, especially
1901 * if its the only CPU with multiple RT tasks queued, and a large number
1902 * of CPUs scheduling a lower priority task at the same time.
1904 * Each root domain has its own irq work function that can iterate over
1905 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
1906 * tassk must be checked if there's one or many CPUs that are lowering
1907 * their priority, there's a single irq work iterator that will try to
1908 * push off RT tasks that are waiting to run.
1910 * When a CPU schedules a lower priority task, it will kick off the
1911 * irq work iterator that will jump to each CPU with overloaded RT tasks.
1912 * As it only takes the first CPU that schedules a lower priority task
1913 * to start the process, the rto_start variable is incremented and if
1914 * the atomic result is one, then that CPU will try to take the rto_lock.
1915 * This prevents high contention on the lock as the process handles all
1916 * CPUs scheduling lower priority tasks.
1918 * All CPUs that are scheduling a lower priority task will increment the
1919 * rt_loop_next variable. This will make sure that the irq work iterator
1920 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
1921 * priority task, even if the iterator is in the middle of a scan. Incrementing
1922 * the rt_loop_next will cause the iterator to perform another scan.
1925 static int rto_next_cpu(struct root_domain
*rd
)
1931 * When starting the IPI RT pushing, the rto_cpu is set to -1,
1932 * rt_next_cpu() will simply return the first CPU found in
1935 * If rto_next_cpu() is called with rto_cpu is a valid cpu, it
1936 * will return the next CPU found in the rto_mask.
1938 * If there are no more CPUs left in the rto_mask, then a check is made
1939 * against rto_loop and rto_loop_next. rto_loop is only updated with
1940 * the rto_lock held, but any CPU may increment the rto_loop_next
1941 * without any locking.
1945 /* When rto_cpu is -1 this acts like cpumask_first() */
1946 cpu
= cpumask_next(rd
->rto_cpu
, rd
->rto_mask
);
1950 if (cpu
< nr_cpu_ids
)
1956 * ACQUIRE ensures we see the @rto_mask changes
1957 * made prior to the @next value observed.
1959 * Matches WMB in rt_set_overload().
1961 next
= atomic_read_acquire(&rd
->rto_loop_next
);
1963 if (rd
->rto_loop
== next
)
1966 rd
->rto_loop
= next
;
1972 static inline bool rto_start_trylock(atomic_t
*v
)
1974 return !atomic_cmpxchg_acquire(v
, 0, 1);
1977 static inline void rto_start_unlock(atomic_t
*v
)
1979 atomic_set_release(v
, 0);
1982 static void tell_cpu_to_push(struct rq
*rq
)
1986 /* Keep the loop going if the IPI is currently active */
1987 atomic_inc(&rq
->rd
->rto_loop_next
);
1989 /* Only one CPU can initiate a loop at a time */
1990 if (!rto_start_trylock(&rq
->rd
->rto_loop_start
))
1993 raw_spin_lock(&rq
->rd
->rto_lock
);
1996 * The rto_cpu is updated under the lock, if it has a valid cpu
1997 * then the IPI is still running and will continue due to the
1998 * update to loop_next, and nothing needs to be done here.
1999 * Otherwise it is finishing up and an ipi needs to be sent.
2001 if (rq
->rd
->rto_cpu
< 0)
2002 cpu
= rto_next_cpu(rq
->rd
);
2004 raw_spin_unlock(&rq
->rd
->rto_lock
);
2006 rto_start_unlock(&rq
->rd
->rto_loop_start
);
2009 /* Make sure the rd does not get freed while pushing */
2010 sched_get_rd(rq
->rd
);
2011 irq_work_queue_on(&rq
->rd
->rto_push_work
, cpu
);
2015 /* Called from hardirq context */
2016 void rto_push_irq_work_func(struct irq_work
*work
)
2018 struct root_domain
*rd
=
2019 container_of(work
, struct root_domain
, rto_push_work
);
2026 * We do not need to grab the lock to check for has_pushable_tasks.
2027 * When it gets updated, a check is made if a push is possible.
2029 if (has_pushable_tasks(rq
)) {
2030 raw_spin_lock(&rq
->lock
);
2032 raw_spin_unlock(&rq
->lock
);
2035 raw_spin_lock(&rd
->rto_lock
);
2037 /* Pass the IPI to the next rt overloaded queue */
2038 cpu
= rto_next_cpu(rd
);
2040 raw_spin_unlock(&rd
->rto_lock
);
2047 /* Try the next RT overloaded CPU */
2048 irq_work_queue_on(&rd
->rto_push_work
, cpu
);
2050 #endif /* HAVE_RT_PUSH_IPI */
2052 static void pull_rt_task(struct rq
*this_rq
)
2054 int this_cpu
= this_rq
->cpu
, cpu
;
2055 bool resched
= false;
2056 struct task_struct
*p
;
2058 int rt_overload_count
= rt_overloaded(this_rq
);
2060 if (likely(!rt_overload_count
))
2064 * Match the barrier from rt_set_overloaded; this guarantees that if we
2065 * see overloaded we must also see the rto_mask bit.
2069 /* If we are the only overloaded CPU do nothing */
2070 if (rt_overload_count
== 1 &&
2071 cpumask_test_cpu(this_rq
->cpu
, this_rq
->rd
->rto_mask
))
2074 #ifdef HAVE_RT_PUSH_IPI
2075 if (sched_feat(RT_PUSH_IPI
)) {
2076 tell_cpu_to_push(this_rq
);
2081 for_each_cpu(cpu
, this_rq
->rd
->rto_mask
) {
2082 if (this_cpu
== cpu
)
2085 src_rq
= cpu_rq(cpu
);
2088 * Don't bother taking the src_rq->lock if the next highest
2089 * task is known to be lower-priority than our current task.
2090 * This may look racy, but if this value is about to go
2091 * logically higher, the src_rq will push this task away.
2092 * And if its going logically lower, we do not care
2094 if (src_rq
->rt
.highest_prio
.next
>=
2095 this_rq
->rt
.highest_prio
.curr
)
2099 * We can potentially drop this_rq's lock in
2100 * double_lock_balance, and another CPU could
2103 double_lock_balance(this_rq
, src_rq
);
2106 * We can pull only a task, which is pushable
2107 * on its rq, and no others.
2109 p
= pick_highest_pushable_task(src_rq
, this_cpu
);
2112 * Do we have an RT task that preempts
2113 * the to-be-scheduled task?
2115 if (p
&& (p
->prio
< this_rq
->rt
.highest_prio
.curr
)) {
2116 WARN_ON(p
== src_rq
->curr
);
2117 WARN_ON(!task_on_rq_queued(p
));
2120 * There's a chance that p is higher in priority
2121 * than what's currently running on its cpu.
2122 * This is just that p is wakeing up and hasn't
2123 * had a chance to schedule. We only pull
2124 * p if it is lower in priority than the
2125 * current task on the run queue
2127 if (p
->prio
< src_rq
->curr
->prio
)
2132 deactivate_task(src_rq
, p
, 0);
2133 p
->on_rq
= TASK_ON_RQ_MIGRATING
;
2134 set_task_cpu(p
, this_cpu
);
2135 p
->on_rq
= TASK_ON_RQ_QUEUED
;
2136 activate_task(this_rq
, p
, 0);
2138 * We continue with the search, just in
2139 * case there's an even higher prio task
2140 * in another runqueue. (low likelihood
2145 double_unlock_balance(this_rq
, src_rq
);
2149 resched_curr(this_rq
);
2153 * If we are not running and we are not going to reschedule soon, we should
2154 * try to push tasks away now
2156 static void task_woken_rt(struct rq
*rq
, struct task_struct
*p
)
2158 if (!task_running(rq
, p
) &&
2159 !test_tsk_need_resched(rq
->curr
) &&
2160 p
->nr_cpus_allowed
> 1 &&
2161 (dl_task(rq
->curr
) || rt_task(rq
->curr
)) &&
2162 (rq
->curr
->nr_cpus_allowed
< 2 ||
2163 rq
->curr
->prio
<= p
->prio
))
2167 /* Assumes rq->lock is held */
2168 static void rq_online_rt(struct rq
*rq
)
2170 if (rq
->rt
.overloaded
)
2171 rt_set_overload(rq
);
2173 __enable_runtime(rq
);
2175 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rq
->rt
.highest_prio
.curr
);
2178 /* Assumes rq->lock is held */
2179 static void rq_offline_rt(struct rq
*rq
)
2181 if (rq
->rt
.overloaded
)
2182 rt_clear_overload(rq
);
2184 __disable_runtime(rq
);
2186 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, CPUPRI_INVALID
);
2190 * When switch from the rt queue, we bring ourselves to a position
2191 * that we might want to pull RT tasks from other runqueues.
2193 static void switched_from_rt(struct rq
*rq
, struct task_struct
*p
)
2196 * If there are other RT tasks then we will reschedule
2197 * and the scheduling of the other RT tasks will handle
2198 * the balancing. But if we are the last RT task
2199 * we may need to handle the pulling of RT tasks
2202 if (!task_on_rq_queued(p
) || rq
->rt
.rt_nr_running
)
2205 queue_pull_task(rq
);
2208 void __init
init_sched_rt_class(void)
2212 for_each_possible_cpu(i
) {
2213 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask
, i
),
2214 GFP_KERNEL
, cpu_to_node(i
));
2217 #endif /* CONFIG_SMP */
2220 * When switching a task to RT, we may overload the runqueue
2221 * with RT tasks. In this case we try to push them off to
2224 static void switched_to_rt(struct rq
*rq
, struct task_struct
*p
)
2227 * If we are already running, then there's nothing
2228 * that needs to be done. But if we are not running
2229 * we may need to preempt the current running task.
2230 * If that current running task is also an RT task
2231 * then see if we can move to another run queue.
2233 if (task_on_rq_queued(p
) && rq
->curr
!= p
) {
2235 if (p
->nr_cpus_allowed
> 1 && rq
->rt
.overloaded
)
2236 queue_push_tasks(rq
);
2237 #endif /* CONFIG_SMP */
2238 if (p
->prio
< rq
->curr
->prio
)
2244 * Priority of the task has changed. This may cause
2245 * us to initiate a push or pull.
2248 prio_changed_rt(struct rq
*rq
, struct task_struct
*p
, int oldprio
)
2250 if (!task_on_rq_queued(p
))
2253 if (rq
->curr
== p
) {
2256 * If our priority decreases while running, we
2257 * may need to pull tasks to this runqueue.
2259 if (oldprio
< p
->prio
)
2260 queue_pull_task(rq
);
2263 * If there's a higher priority task waiting to run
2266 if (p
->prio
> rq
->rt
.highest_prio
.curr
)
2269 /* For UP simply resched on drop of prio */
2270 if (oldprio
< p
->prio
)
2272 #endif /* CONFIG_SMP */
2275 * This task is not running, but if it is
2276 * greater than the current running task
2279 if (p
->prio
< rq
->curr
->prio
)
2284 #ifdef CONFIG_POSIX_TIMERS
2285 static void watchdog(struct rq
*rq
, struct task_struct
*p
)
2287 unsigned long soft
, hard
;
2289 /* max may change after cur was read, this will be fixed next tick */
2290 soft
= task_rlimit(p
, RLIMIT_RTTIME
);
2291 hard
= task_rlimit_max(p
, RLIMIT_RTTIME
);
2293 if (soft
!= RLIM_INFINITY
) {
2296 if (p
->rt
.watchdog_stamp
!= jiffies
) {
2298 p
->rt
.watchdog_stamp
= jiffies
;
2301 next
= DIV_ROUND_UP(min(soft
, hard
), USEC_PER_SEC
/HZ
);
2302 if (p
->rt
.timeout
> next
)
2303 p
->cputime_expires
.sched_exp
= p
->se
.sum_exec_runtime
;
2307 static inline void watchdog(struct rq
*rq
, struct task_struct
*p
) { }
2310 static void task_tick_rt(struct rq
*rq
, struct task_struct
*p
, int queued
)
2312 struct sched_rt_entity
*rt_se
= &p
->rt
;
2315 update_rt_rq_load_avg(rq_clock_task(rq
), cpu_of(rq
), &rq
->rt
, 1);
2320 * RR tasks need a special form of timeslice management.
2321 * FIFO tasks have no timeslices.
2323 if (p
->policy
!= SCHED_RR
)
2326 if (--p
->rt
.time_slice
)
2329 p
->rt
.time_slice
= sched_rr_timeslice
;
2332 * Requeue to the end of queue if we (and all of our ancestors) are not
2333 * the only element on the queue
2335 for_each_sched_rt_entity(rt_se
) {
2336 if (rt_se
->run_list
.prev
!= rt_se
->run_list
.next
) {
2337 requeue_task_rt(rq
, p
, 0);
2344 static void set_curr_task_rt(struct rq
*rq
)
2346 struct task_struct
*p
= rq
->curr
;
2348 p
->se
.exec_start
= rq_clock_task(rq
);
2350 /* The running task is never eligible for pushing */
2351 dequeue_pushable_task(rq
, p
);
2354 static unsigned int get_rr_interval_rt(struct rq
*rq
, struct task_struct
*task
)
2357 * Time slice is 0 for SCHED_FIFO tasks
2359 if (task
->policy
== SCHED_RR
)
2360 return sched_rr_timeslice
;
2365 const struct sched_class rt_sched_class
= {
2366 .next
= &fair_sched_class
,
2367 .enqueue_task
= enqueue_task_rt
,
2368 .dequeue_task
= dequeue_task_rt
,
2369 .yield_task
= yield_task_rt
,
2371 .check_preempt_curr
= check_preempt_curr_rt
,
2373 .pick_next_task
= pick_next_task_rt
,
2374 .put_prev_task
= put_prev_task_rt
,
2377 .select_task_rq
= select_task_rq_rt
,
2379 .set_cpus_allowed
= set_cpus_allowed_common
,
2380 .rq_online
= rq_online_rt
,
2381 .rq_offline
= rq_offline_rt
,
2382 .task_woken
= task_woken_rt
,
2383 .switched_from
= switched_from_rt
,
2386 .set_curr_task
= set_curr_task_rt
,
2387 .task_tick
= task_tick_rt
,
2389 .get_rr_interval
= get_rr_interval_rt
,
2391 .prio_changed
= prio_changed_rt
,
2392 .switched_to
= switched_to_rt
,
2394 .update_curr
= update_curr_rt
,
2397 #ifdef CONFIG_RT_GROUP_SCHED
2399 * Ensure that the real time constraints are schedulable.
2401 static DEFINE_MUTEX(rt_constraints_mutex
);
2403 /* Must be called with tasklist_lock held */
2404 static inline int tg_has_rt_tasks(struct task_group
*tg
)
2406 struct task_struct
*g
, *p
;
2409 * Autogroups do not have RT tasks; see autogroup_create().
2411 if (task_group_is_autogroup(tg
))
2414 for_each_process_thread(g
, p
) {
2415 if (rt_task(p
) && task_group(p
) == tg
)
2422 struct rt_schedulable_data
{
2423 struct task_group
*tg
;
2428 static int tg_rt_schedulable(struct task_group
*tg
, void *data
)
2430 struct rt_schedulable_data
*d
= data
;
2431 struct task_group
*child
;
2432 unsigned long total
, sum
= 0;
2433 u64 period
, runtime
;
2435 period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
2436 runtime
= tg
->rt_bandwidth
.rt_runtime
;
2439 period
= d
->rt_period
;
2440 runtime
= d
->rt_runtime
;
2444 * Cannot have more runtime than the period.
2446 if (runtime
> period
&& runtime
!= RUNTIME_INF
)
2450 * Ensure we don't starve existing RT tasks.
2452 if (rt_bandwidth_enabled() && !runtime
&& tg_has_rt_tasks(tg
))
2455 total
= to_ratio(period
, runtime
);
2458 * Nobody can have more than the global setting allows.
2460 if (total
> to_ratio(global_rt_period(), global_rt_runtime()))
2464 * The sum of our children's runtime should not exceed our own.
2466 list_for_each_entry_rcu(child
, &tg
->children
, siblings
) {
2467 period
= ktime_to_ns(child
->rt_bandwidth
.rt_period
);
2468 runtime
= child
->rt_bandwidth
.rt_runtime
;
2470 if (child
== d
->tg
) {
2471 period
= d
->rt_period
;
2472 runtime
= d
->rt_runtime
;
2475 sum
+= to_ratio(period
, runtime
);
2484 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
2488 struct rt_schedulable_data data
= {
2490 .rt_period
= period
,
2491 .rt_runtime
= runtime
,
2495 ret
= walk_tg_tree(tg_rt_schedulable
, tg_nop
, &data
);
2501 static int tg_set_rt_bandwidth(struct task_group
*tg
,
2502 u64 rt_period
, u64 rt_runtime
)
2507 * Disallowing the root group RT runtime is BAD, it would disallow the
2508 * kernel creating (and or operating) RT threads.
2510 if (tg
== &root_task_group
&& rt_runtime
== 0)
2513 /* No period doesn't make any sense. */
2517 mutex_lock(&rt_constraints_mutex
);
2518 read_lock(&tasklist_lock
);
2519 err
= __rt_schedulable(tg
, rt_period
, rt_runtime
);
2523 raw_spin_lock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
2524 tg
->rt_bandwidth
.rt_period
= ns_to_ktime(rt_period
);
2525 tg
->rt_bandwidth
.rt_runtime
= rt_runtime
;
2527 for_each_possible_cpu(i
) {
2528 struct rt_rq
*rt_rq
= tg
->rt_rq
[i
];
2530 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
2531 rt_rq
->rt_runtime
= rt_runtime
;
2532 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
2534 raw_spin_unlock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
2536 read_unlock(&tasklist_lock
);
2537 mutex_unlock(&rt_constraints_mutex
);
2542 int sched_group_set_rt_runtime(struct task_group
*tg
, long rt_runtime_us
)
2544 u64 rt_runtime
, rt_period
;
2546 rt_period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
2547 rt_runtime
= (u64
)rt_runtime_us
* NSEC_PER_USEC
;
2548 if (rt_runtime_us
< 0)
2549 rt_runtime
= RUNTIME_INF
;
2551 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
2554 long sched_group_rt_runtime(struct task_group
*tg
)
2558 if (tg
->rt_bandwidth
.rt_runtime
== RUNTIME_INF
)
2561 rt_runtime_us
= tg
->rt_bandwidth
.rt_runtime
;
2562 do_div(rt_runtime_us
, NSEC_PER_USEC
);
2563 return rt_runtime_us
;
2566 int sched_group_set_rt_period(struct task_group
*tg
, u64 rt_period_us
)
2568 u64 rt_runtime
, rt_period
;
2570 rt_period
= rt_period_us
* NSEC_PER_USEC
;
2571 rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
2573 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
2576 long sched_group_rt_period(struct task_group
*tg
)
2580 rt_period_us
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
2581 do_div(rt_period_us
, NSEC_PER_USEC
);
2582 return rt_period_us
;
2585 static int sched_rt_global_constraints(void)
2589 mutex_lock(&rt_constraints_mutex
);
2590 read_lock(&tasklist_lock
);
2591 ret
= __rt_schedulable(NULL
, 0, 0);
2592 read_unlock(&tasklist_lock
);
2593 mutex_unlock(&rt_constraints_mutex
);
2598 int sched_rt_can_attach(struct task_group
*tg
, struct task_struct
*tsk
)
2600 /* Don't accept realtime tasks when there is no way for them to run */
2601 if (rt_task(tsk
) && tg
->rt_bandwidth
.rt_runtime
== 0)
2607 #else /* !CONFIG_RT_GROUP_SCHED */
2608 static int sched_rt_global_constraints(void)
2610 unsigned long flags
;
2613 raw_spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
2614 for_each_possible_cpu(i
) {
2615 struct rt_rq
*rt_rq
= &cpu_rq(i
)->rt
;
2617 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
2618 rt_rq
->rt_runtime
= global_rt_runtime();
2619 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
2621 raw_spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
2625 #endif /* CONFIG_RT_GROUP_SCHED */
2627 static int sched_rt_global_validate(void)
2629 if (sysctl_sched_rt_period
<= 0)
2632 if ((sysctl_sched_rt_runtime
!= RUNTIME_INF
) &&
2633 (sysctl_sched_rt_runtime
> sysctl_sched_rt_period
))
2639 static void sched_rt_do_global(void)
2641 def_rt_bandwidth
.rt_runtime
= global_rt_runtime();
2642 def_rt_bandwidth
.rt_period
= ns_to_ktime(global_rt_period());
2645 int sched_rt_handler(struct ctl_table
*table
, int write
,
2646 void __user
*buffer
, size_t *lenp
,
2649 int old_period
, old_runtime
;
2650 static DEFINE_MUTEX(mutex
);
2654 old_period
= sysctl_sched_rt_period
;
2655 old_runtime
= sysctl_sched_rt_runtime
;
2657 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
2659 if (!ret
&& write
) {
2660 ret
= sched_rt_global_validate();
2664 ret
= sched_dl_global_validate();
2668 ret
= sched_rt_global_constraints();
2672 sched_rt_do_global();
2673 sched_dl_do_global();
2677 sysctl_sched_rt_period
= old_period
;
2678 sysctl_sched_rt_runtime
= old_runtime
;
2680 mutex_unlock(&mutex
);
2685 int sched_rr_handler(struct ctl_table
*table
, int write
,
2686 void __user
*buffer
, size_t *lenp
,
2690 static DEFINE_MUTEX(mutex
);
2693 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
2695 * Make sure that internally we keep jiffies.
2696 * Also, writing zero resets the timeslice to default:
2698 if (!ret
&& write
) {
2699 sched_rr_timeslice
=
2700 sysctl_sched_rr_timeslice
<= 0 ? RR_TIMESLICE
:
2701 msecs_to_jiffies(sysctl_sched_rr_timeslice
);
2703 mutex_unlock(&mutex
);
2707 #ifdef CONFIG_SCHED_DEBUG
2708 extern void print_rt_rq(struct seq_file
*m
, int cpu
, struct rt_rq
*rt_rq
);
2710 void print_rt_stats(struct seq_file
*m
, int cpu
)
2713 struct rt_rq
*rt_rq
;
2716 for_each_rt_rq(rt_rq
, iter
, cpu_rq(cpu
))
2717 print_rt_rq(m
, cpu
, rt_rq
);
2720 #endif /* CONFIG_SCHED_DEBUG */