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>
15 int sched_rr_timeslice
= RR_TIMESLICE
;
16 int sysctl_sched_rr_timeslice
= (MSEC_PER_SEC
/ HZ
) * RR_TIMESLICE
;
19 void update_rt_load_avg(u64 now
, struct sched_rt_entity
*rt_se
);
21 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
);
23 struct rt_bandwidth def_rt_bandwidth
;
25 static enum hrtimer_restart
sched_rt_period_timer(struct hrtimer
*timer
)
27 struct rt_bandwidth
*rt_b
=
28 container_of(timer
, struct rt_bandwidth
, rt_period_timer
);
32 raw_spin_lock(&rt_b
->rt_runtime_lock
);
34 overrun
= hrtimer_forward_now(timer
, rt_b
->rt_period
);
38 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
39 idle
= do_sched_rt_period_timer(rt_b
, overrun
);
40 raw_spin_lock(&rt_b
->rt_runtime_lock
);
43 rt_b
->rt_period_active
= 0;
44 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
46 return idle
? HRTIMER_NORESTART
: HRTIMER_RESTART
;
49 void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
)
51 rt_b
->rt_period
= ns_to_ktime(period
);
52 rt_b
->rt_runtime
= runtime
;
54 raw_spin_lock_init(&rt_b
->rt_runtime_lock
);
56 hrtimer_init(&rt_b
->rt_period_timer
,
57 CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
58 rt_b
->rt_period_timer
.function
= sched_rt_period_timer
;
61 static inline void do_start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
63 raw_spin_lock(&rt_b
->rt_runtime_lock
);
64 if (!rt_b
->rt_period_active
) {
65 rt_b
->rt_period_active
= 1;
67 * SCHED_DEADLINE updates the bandwidth, as a run away
68 * RT task with a DL task could hog a CPU. But DL does
69 * not reset the period. If a deadline task was running
70 * without an RT task running, it can cause RT tasks to
71 * throttle when they start up. Kick the timer right away
72 * to update the period.
74 hrtimer_forward_now(&rt_b
->rt_period_timer
, ns_to_ktime(0));
75 hrtimer_start_expires(&rt_b
->rt_period_timer
, HRTIMER_MODE_ABS_PINNED
);
77 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
80 static void start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
82 if (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
)
85 do_start_rt_bandwidth(rt_b
);
88 void init_rt_rq(struct rt_rq
*rt_rq
)
90 struct rt_prio_array
*array
;
93 array
= &rt_rq
->active
;
94 for (i
= 0; i
< MAX_RT_PRIO
; i
++) {
95 INIT_LIST_HEAD(array
->queue
+ i
);
96 __clear_bit(i
, array
->bitmap
);
98 /* delimiter for bitsearch: */
99 __set_bit(MAX_RT_PRIO
, array
->bitmap
);
101 #if defined CONFIG_SMP
102 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
103 rt_rq
->highest_prio
.next
= MAX_RT_PRIO
;
104 rt_rq
->rt_nr_migratory
= 0;
105 rt_rq
->overloaded
= 0;
106 plist_head_init(&rt_rq
->pushable_tasks
);
107 atomic_long_set(&rt_rq
->removed_util_avg
, 0);
108 atomic_long_set(&rt_rq
->removed_load_avg
, 0);
109 #endif /* CONFIG_SMP */
110 /* We start is dequeued state, because no RT tasks are queued */
111 rt_rq
->rt_queued
= 0;
114 rt_rq
->rt_throttled
= 0;
115 rt_rq
->rt_runtime
= 0;
116 raw_spin_lock_init(&rt_rq
->rt_runtime_lock
);
119 #ifdef CONFIG_RT_GROUP_SCHED
120 static void destroy_rt_bandwidth(struct rt_bandwidth
*rt_b
)
122 hrtimer_cancel(&rt_b
->rt_period_timer
);
125 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
127 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
129 #ifdef CONFIG_SCHED_DEBUG
130 WARN_ON_ONCE(!rt_entity_is_task(rt_se
));
132 return container_of(rt_se
, struct task_struct
, rt
);
135 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
140 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
145 static inline struct rq
*rq_of_rt_se(struct sched_rt_entity
*rt_se
)
147 struct rt_rq
*rt_rq
= rt_se
->rt_rq
;
152 void free_rt_sched_group(struct task_group
*tg
)
157 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
159 for_each_possible_cpu(i
) {
170 void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
171 struct sched_rt_entity
*rt_se
, int cpu
,
172 struct sched_rt_entity
*parent
)
174 struct rq
*rq
= cpu_rq(cpu
);
176 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
177 rt_rq
->rt_nr_boosted
= 0;
181 tg
->rt_rq
[cpu
] = rt_rq
;
182 tg
->rt_se
[cpu
] = rt_se
;
188 rt_se
->rt_rq
= &rq
->rt
;
190 rt_se
->rt_rq
= parent
->my_q
;
193 rt_se
->parent
= parent
;
194 INIT_LIST_HEAD(&rt_se
->run_list
);
197 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
200 struct sched_rt_entity
*rt_se
;
203 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
206 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
210 init_rt_bandwidth(&tg
->rt_bandwidth
,
211 ktime_to_ns(def_rt_bandwidth
.rt_period
), 0);
213 for_each_possible_cpu(i
) {
214 rt_rq
= kzalloc_node(sizeof(struct rt_rq
),
215 GFP_KERNEL
, cpu_to_node(i
));
219 rt_se
= kzalloc_node(sizeof(struct sched_rt_entity
),
220 GFP_KERNEL
, cpu_to_node(i
));
225 rt_rq
->rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
226 init_tg_rt_entry(tg
, rt_rq
, rt_se
, i
, parent
->rt_se
[i
]);
227 init_rt_entity_runnable_average(rt_se
);
238 #else /* CONFIG_RT_GROUP_SCHED */
240 #define rt_entity_is_task(rt_se) (1)
242 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
244 return container_of(rt_se
, struct task_struct
, rt
);
247 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
249 return container_of(rt_rq
, struct rq
, rt
);
252 static inline struct rq
*rq_of_rt_se(struct sched_rt_entity
*rt_se
)
254 struct task_struct
*p
= rt_task_of(rt_se
);
259 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
261 struct rq
*rq
= rq_of_rt_se(rt_se
);
266 void free_rt_sched_group(struct task_group
*tg
) { }
268 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
272 #endif /* CONFIG_RT_GROUP_SCHED */
276 #include "sched-pelt.h"
278 extern u64
decay_load(u64 val
, u64 n
);
280 static u32
__accumulate_pelt_segments_rt(u64 periods
, u32 d1
, u32 d3
)
284 c1
= decay_load((u64
)d1
, periods
);
286 c2
= LOAD_AVG_MAX
- decay_load(LOAD_AVG_MAX
, periods
) - 1024;
291 #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
293 static __always_inline u32
294 accumulate_sum_rt(u64 delta
, int cpu
, struct sched_avg
*sa
,
295 unsigned long weight
, int running
)
297 unsigned long scale_freq
, scale_cpu
;
298 u32 contrib
= (u32
)delta
;
301 scale_freq
= arch_scale_freq_capacity(NULL
, cpu
);
302 scale_cpu
= arch_scale_cpu_capacity(NULL
, cpu
);
304 delta
+= sa
->period_contrib
;
305 periods
= delta
/ 1024;
308 sa
->load_sum
= decay_load(sa
->load_sum
, periods
);
309 sa
->util_sum
= decay_load((u64
)(sa
->util_sum
), periods
);
312 contrib
= __accumulate_pelt_segments_rt(periods
,
313 1024 - sa
->period_contrib
, delta
);
315 sa
->period_contrib
= delta
;
317 contrib
= cap_scale(contrib
, scale_freq
);
319 sa
->load_sum
+= weight
* contrib
;
322 sa
->util_sum
+= contrib
* scale_cpu
;
328 * We can represent the historical contribution to runnable average as the
329 * coefficients of a geometric series, exactly like fair task load.
330 * refer the ___update_load_avg @ fair sched class
332 static __always_inline
int
333 __update_load_avg(u64 now
, int cpu
, struct sched_avg
*sa
,
334 unsigned long weight
, int running
, struct rt_rq
*rt_rq
)
338 delta
= now
- sa
->last_update_time
;
340 if ((s64
)delta
< 0) {
341 sa
->last_update_time
= now
;
349 sa
->last_update_time
+= delta
<< 10;
354 if (!accumulate_sum_rt(delta
, cpu
, sa
, weight
, running
))
357 sa
->load_avg
= div_u64(sa
->load_sum
, LOAD_AVG_MAX
- 1024 + sa
->period_contrib
);
358 sa
->util_avg
= sa
->util_sum
/ (LOAD_AVG_MAX
- 1024 + sa
->period_contrib
);
363 static void pull_rt_task(struct rq
*this_rq
);
365 static inline bool need_pull_rt_task(struct rq
*rq
, struct task_struct
*prev
)
367 /* Try to pull RT tasks here if we lower this rq's prio */
368 return rq
->rt
.highest_prio
.curr
> prev
->prio
;
371 static inline int rt_overloaded(struct rq
*rq
)
373 return atomic_read(&rq
->rd
->rto_count
);
376 static inline void rt_set_overload(struct rq
*rq
)
381 cpumask_set_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
383 * Make sure the mask is visible before we set
384 * the overload count. That is checked to determine
385 * if we should look at the mask. It would be a shame
386 * if we looked at the mask, but the mask was not
389 * Matched by the barrier in pull_rt_task().
392 atomic_inc(&rq
->rd
->rto_count
);
395 static inline void rt_clear_overload(struct rq
*rq
)
400 /* the order here really doesn't matter */
401 atomic_dec(&rq
->rd
->rto_count
);
402 cpumask_clear_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
405 static void update_rt_migration(struct rt_rq
*rt_rq
)
407 if (rt_rq
->rt_nr_migratory
&& rt_rq
->rt_nr_total
> 1) {
408 if (!rt_rq
->overloaded
) {
409 rt_set_overload(rq_of_rt_rq(rt_rq
));
410 rt_rq
->overloaded
= 1;
412 } else if (rt_rq
->overloaded
) {
413 rt_clear_overload(rq_of_rt_rq(rt_rq
));
414 rt_rq
->overloaded
= 0;
418 static void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
420 struct task_struct
*p
;
422 if (!rt_entity_is_task(rt_se
))
425 p
= rt_task_of(rt_se
);
426 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
428 rt_rq
->rt_nr_total
++;
429 if (p
->nr_cpus_allowed
> 1)
430 rt_rq
->rt_nr_migratory
++;
432 update_rt_migration(rt_rq
);
435 static void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
437 struct task_struct
*p
;
439 if (!rt_entity_is_task(rt_se
))
442 p
= rt_task_of(rt_se
);
443 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
445 rt_rq
->rt_nr_total
--;
446 if (p
->nr_cpus_allowed
> 1)
447 rt_rq
->rt_nr_migratory
--;
449 update_rt_migration(rt_rq
);
452 static inline int has_pushable_tasks(struct rq
*rq
)
454 return !plist_head_empty(&rq
->rt
.pushable_tasks
);
457 static DEFINE_PER_CPU(struct callback_head
, rt_push_head
);
458 static DEFINE_PER_CPU(struct callback_head
, rt_pull_head
);
460 static void push_rt_tasks(struct rq
*);
461 static void pull_rt_task(struct rq
*);
463 static inline void queue_push_tasks(struct rq
*rq
)
465 if (!has_pushable_tasks(rq
))
468 queue_balance_callback(rq
, &per_cpu(rt_push_head
, rq
->cpu
), push_rt_tasks
);
471 static inline void queue_pull_task(struct rq
*rq
)
473 queue_balance_callback(rq
, &per_cpu(rt_pull_head
, rq
->cpu
), pull_rt_task
);
476 static void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
478 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
479 plist_node_init(&p
->pushable_tasks
, p
->prio
);
480 plist_add(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
482 /* Update the highest prio pushable task */
483 if (p
->prio
< rq
->rt
.highest_prio
.next
)
484 rq
->rt
.highest_prio
.next
= p
->prio
;
487 static void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
489 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
491 /* Update the new highest prio pushable task */
492 if (has_pushable_tasks(rq
)) {
493 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
494 struct task_struct
, pushable_tasks
);
495 rq
->rt
.highest_prio
.next
= p
->prio
;
497 rq
->rt
.highest_prio
.next
= MAX_RT_PRIO
;
502 static inline void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
506 static inline void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
511 void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
516 void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
520 static inline bool need_pull_rt_task(struct rq
*rq
, struct task_struct
*prev
)
525 static inline void pull_rt_task(struct rq
*this_rq
)
529 static inline void queue_push_tasks(struct rq
*rq
)
532 #endif /* CONFIG_SMP */
534 static void enqueue_top_rt_rq(struct rt_rq
*rt_rq
);
535 static void dequeue_top_rt_rq(struct rt_rq
*rt_rq
);
537 static inline int on_rt_rq(struct sched_rt_entity
*rt_se
)
542 #ifdef CONFIG_RT_GROUP_SCHED
544 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
549 return rt_rq
->rt_runtime
;
552 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
554 return ktime_to_ns(rt_rq
->tg
->rt_bandwidth
.rt_period
);
557 typedef struct task_group
*rt_rq_iter_t
;
559 static inline struct task_group
*next_task_group(struct task_group
*tg
)
562 tg
= list_entry_rcu(tg
->list
.next
,
563 typeof(struct task_group
), list
);
564 } while (&tg
->list
!= &task_groups
&& task_group_is_autogroup(tg
));
566 if (&tg
->list
== &task_groups
)
572 #define for_each_rt_rq(rt_rq, iter, rq) \
573 for (iter = container_of(&task_groups, typeof(*iter), list); \
574 (iter = next_task_group(iter)) && \
575 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
577 #define for_each_sched_rt_entity(rt_se) \
578 for (; rt_se; rt_se = rt_se->parent)
580 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
585 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
);
586 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
);
588 static void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
590 struct task_struct
*curr
= rq_of_rt_rq(rt_rq
)->curr
;
591 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
592 struct sched_rt_entity
*rt_se
;
594 int cpu
= cpu_of(rq
);
596 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
598 if (rt_rq
->rt_nr_running
) {
600 enqueue_top_rt_rq(rt_rq
);
601 else if (!on_rt_rq(rt_se
))
602 enqueue_rt_entity(rt_se
, 0);
604 if (rt_rq
->highest_prio
.curr
< curr
->prio
)
609 static void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
611 struct sched_rt_entity
*rt_se
;
612 int cpu
= cpu_of(rq_of_rt_rq(rt_rq
));
614 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
617 dequeue_top_rt_rq(rt_rq
);
618 else if (on_rt_rq(rt_se
))
619 dequeue_rt_entity(rt_se
, 0);
622 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
624 return rt_rq
->rt_throttled
&& !rt_rq
->rt_nr_boosted
;
627 static int rt_se_boosted(struct sched_rt_entity
*rt_se
)
629 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
630 struct task_struct
*p
;
633 return !!rt_rq
->rt_nr_boosted
;
635 p
= rt_task_of(rt_se
);
636 return p
->prio
!= p
->normal_prio
;
640 static inline const struct cpumask
*sched_rt_period_mask(void)
642 return this_rq()->rd
->span
;
645 static inline const struct cpumask
*sched_rt_period_mask(void)
647 return cpu_online_mask
;
652 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
654 return container_of(rt_b
, struct task_group
, rt_bandwidth
)->rt_rq
[cpu
];
657 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
659 return &rt_rq
->tg
->rt_bandwidth
;
662 #else /* !CONFIG_RT_GROUP_SCHED */
664 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
666 return rt_rq
->rt_runtime
;
669 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
671 return ktime_to_ns(def_rt_bandwidth
.rt_period
);
674 typedef struct rt_rq
*rt_rq_iter_t
;
676 #define for_each_rt_rq(rt_rq, iter, rq) \
677 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
679 #define for_each_sched_rt_entity(rt_se) \
680 for (; rt_se; rt_se = NULL)
682 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
687 static inline void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
689 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
691 if (!rt_rq
->rt_nr_running
)
694 enqueue_top_rt_rq(rt_rq
);
698 static inline void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
700 dequeue_top_rt_rq(rt_rq
);
703 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
705 return rt_rq
->rt_throttled
;
708 static inline const struct cpumask
*sched_rt_period_mask(void)
710 return cpu_online_mask
;
714 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
716 return &cpu_rq(cpu
)->rt
;
719 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
721 return &def_rt_bandwidth
;
724 #endif /* CONFIG_RT_GROUP_SCHED */
726 bool sched_rt_bandwidth_account(struct rt_rq
*rt_rq
)
728 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
730 return (hrtimer_active(&rt_b
->rt_period_timer
) ||
731 rt_rq
->rt_time
< rt_b
->rt_runtime
);
736 * We ran out of runtime, see if we can borrow some from our neighbours.
738 static void do_balance_runtime(struct rt_rq
*rt_rq
)
740 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
741 struct root_domain
*rd
= rq_of_rt_rq(rt_rq
)->rd
;
745 weight
= cpumask_weight(rd
->span
);
747 raw_spin_lock(&rt_b
->rt_runtime_lock
);
748 rt_period
= ktime_to_ns(rt_b
->rt_period
);
749 for_each_cpu(i
, rd
->span
) {
750 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
756 raw_spin_lock(&iter
->rt_runtime_lock
);
758 * Either all rqs have inf runtime and there's nothing to steal
759 * or __disable_runtime() below sets a specific rq to inf to
760 * indicate its been disabled and disalow stealing.
762 if (iter
->rt_runtime
== RUNTIME_INF
)
766 * From runqueues with spare time, take 1/n part of their
767 * spare time, but no more than our period.
769 diff
= iter
->rt_runtime
- iter
->rt_time
;
771 diff
= div_u64((u64
)diff
, weight
);
772 if (rt_rq
->rt_runtime
+ diff
> rt_period
)
773 diff
= rt_period
- rt_rq
->rt_runtime
;
774 iter
->rt_runtime
-= diff
;
775 rt_rq
->rt_runtime
+= diff
;
776 if (rt_rq
->rt_runtime
== rt_period
) {
777 raw_spin_unlock(&iter
->rt_runtime_lock
);
782 raw_spin_unlock(&iter
->rt_runtime_lock
);
784 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
788 * Ensure this RQ takes back all the runtime it lend to its neighbours.
790 static void __disable_runtime(struct rq
*rq
)
792 struct root_domain
*rd
= rq
->rd
;
796 if (unlikely(!scheduler_running
))
799 for_each_rt_rq(rt_rq
, iter
, rq
) {
800 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
804 raw_spin_lock(&rt_b
->rt_runtime_lock
);
805 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
807 * Either we're all inf and nobody needs to borrow, or we're
808 * already disabled and thus have nothing to do, or we have
809 * exactly the right amount of runtime to take out.
811 if (rt_rq
->rt_runtime
== RUNTIME_INF
||
812 rt_rq
->rt_runtime
== rt_b
->rt_runtime
)
814 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
817 * Calculate the difference between what we started out with
818 * and what we current have, that's the amount of runtime
819 * we lend and now have to reclaim.
821 want
= rt_b
->rt_runtime
- rt_rq
->rt_runtime
;
824 * Greedy reclaim, take back as much as we can.
826 for_each_cpu(i
, rd
->span
) {
827 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
831 * Can't reclaim from ourselves or disabled runqueues.
833 if (iter
== rt_rq
|| iter
->rt_runtime
== RUNTIME_INF
)
836 raw_spin_lock(&iter
->rt_runtime_lock
);
838 diff
= min_t(s64
, iter
->rt_runtime
, want
);
839 iter
->rt_runtime
-= diff
;
842 iter
->rt_runtime
-= want
;
845 raw_spin_unlock(&iter
->rt_runtime_lock
);
851 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
853 * We cannot be left wanting - that would mean some runtime
854 * leaked out of the system.
859 * Disable all the borrow logic by pretending we have inf
860 * runtime - in which case borrowing doesn't make sense.
862 rt_rq
->rt_runtime
= RUNTIME_INF
;
863 rt_rq
->rt_throttled
= 0;
864 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
865 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
867 /* Make rt_rq available for pick_next_task() */
868 sched_rt_rq_enqueue(rt_rq
);
872 static void __enable_runtime(struct rq
*rq
)
877 if (unlikely(!scheduler_running
))
881 * Reset each runqueue's bandwidth settings
883 for_each_rt_rq(rt_rq
, iter
, rq
) {
884 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
886 raw_spin_lock(&rt_b
->rt_runtime_lock
);
887 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
888 rt_rq
->rt_runtime
= rt_b
->rt_runtime
;
890 rt_rq
->rt_throttled
= 0;
891 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
892 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
896 static void balance_runtime(struct rt_rq
*rt_rq
)
898 if (!sched_feat(RT_RUNTIME_SHARE
))
901 if (rt_rq
->rt_time
> rt_rq
->rt_runtime
) {
902 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
903 do_balance_runtime(rt_rq
);
904 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
907 #else /* !CONFIG_SMP */
908 static inline void balance_runtime(struct rt_rq
*rt_rq
) {}
909 #endif /* CONFIG_SMP */
911 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
)
913 int i
, idle
= 1, throttled
= 0;
914 const struct cpumask
*span
;
916 span
= sched_rt_period_mask();
917 #ifdef CONFIG_RT_GROUP_SCHED
919 * FIXME: isolated CPUs should really leave the root task group,
920 * whether they are isolcpus or were isolated via cpusets, lest
921 * the timer run on a CPU which does not service all runqueues,
922 * potentially leaving other CPUs indefinitely throttled. If
923 * isolation is really required, the user will turn the throttle
924 * off to kill the perturbations it causes anyway. Meanwhile,
925 * this maintains functionality for boot and/or troubleshooting.
927 if (rt_b
== &root_task_group
.rt_bandwidth
)
928 span
= cpu_online_mask
;
930 for_each_cpu(i
, span
) {
932 struct rt_rq
*rt_rq
= sched_rt_period_rt_rq(rt_b
, i
);
933 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
937 * When span == cpu_online_mask, taking each rq->lock
938 * can be time-consuming. Try to avoid it when possible.
940 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
941 if (!sched_feat(RT_RUNTIME_SHARE
) && rt_rq
->rt_runtime
!= RUNTIME_INF
)
942 rt_rq
->rt_runtime
= rt_b
->rt_runtime
;
943 skip
= !rt_rq
->rt_time
&& !rt_rq
->rt_nr_running
;
944 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
948 raw_spin_lock(&rq
->lock
);
951 if (rt_rq
->rt_time
) {
954 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
955 if (rt_rq
->rt_throttled
)
956 balance_runtime(rt_rq
);
957 runtime
= rt_rq
->rt_runtime
;
958 rt_rq
->rt_time
-= min(rt_rq
->rt_time
, overrun
*runtime
);
959 if (rt_rq
->rt_throttled
&& rt_rq
->rt_time
< runtime
) {
960 rt_rq
->rt_throttled
= 0;
964 * When we're idle and a woken (rt) task is
965 * throttled check_preempt_curr() will set
966 * skip_update and the time between the wakeup
967 * and this unthrottle will get accounted as
970 if (rt_rq
->rt_nr_running
&& rq
->curr
== rq
->idle
)
971 rq_clock_skip_update(rq
, false);
973 if (rt_rq
->rt_time
|| rt_rq
->rt_nr_running
)
975 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
976 } else if (rt_rq
->rt_nr_running
) {
978 if (!rt_rq_throttled(rt_rq
))
981 if (rt_rq
->rt_throttled
)
985 sched_rt_rq_enqueue(rt_rq
);
986 raw_spin_unlock(&rq
->lock
);
989 if (!throttled
&& (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
))
995 static inline int rt_se_prio(struct sched_rt_entity
*rt_se
)
997 #ifdef CONFIG_RT_GROUP_SCHED
998 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1001 return rt_rq
->highest_prio
.curr
;
1004 return rt_task_of(rt_se
)->prio
;
1007 static int sched_rt_runtime_exceeded(struct rt_rq
*rt_rq
)
1009 u64 runtime
= sched_rt_runtime(rt_rq
);
1011 if (rt_rq
->rt_throttled
)
1012 return rt_rq_throttled(rt_rq
);
1014 if (runtime
>= sched_rt_period(rt_rq
))
1017 balance_runtime(rt_rq
);
1018 runtime
= sched_rt_runtime(rt_rq
);
1019 if (runtime
== RUNTIME_INF
)
1022 if (rt_rq
->rt_time
> runtime
) {
1023 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
1026 * Don't actually throttle groups that have no runtime assigned
1027 * but accrue some time due to boosting.
1029 if (likely(rt_b
->rt_runtime
)) {
1030 rt_rq
->rt_throttled
= 1;
1031 printk_deferred_once("sched: RT throttling activated\n");
1034 * In case we did anyway, make it go away,
1035 * replenishment is a joke, since it will replenish us
1036 * with exactly 0 ns.
1041 if (rt_rq_throttled(rt_rq
)) {
1042 sched_rt_rq_dequeue(rt_rq
);
1051 * Update the current task's runtime statistics. Skip current tasks that
1052 * are not in our scheduling class.
1054 static void update_curr_rt(struct rq
*rq
)
1056 struct task_struct
*curr
= rq
->curr
;
1057 struct sched_rt_entity
*rt_se
= &curr
->rt
;
1060 if (curr
->sched_class
!= &rt_sched_class
)
1063 delta_exec
= rq_clock_task(rq
) - curr
->se
.exec_start
;
1064 if (unlikely((s64
)delta_exec
<= 0))
1067 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
1068 cpufreq_update_util(rq
, SCHED_CPUFREQ_RT
);
1070 schedstat_set(curr
->se
.statistics
.exec_max
,
1071 max(curr
->se
.statistics
.exec_max
, delta_exec
));
1073 curr
->se
.sum_exec_runtime
+= delta_exec
;
1074 account_group_exec_runtime(curr
, delta_exec
);
1076 curr
->se
.exec_start
= rq_clock_task(rq
);
1077 cpuacct_charge(curr
, delta_exec
);
1079 sched_rt_avg_update(rq
, delta_exec
);
1081 if (!rt_bandwidth_enabled())
1084 for_each_sched_rt_entity(rt_se
) {
1085 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1088 if (sched_rt_runtime(rt_rq
) != RUNTIME_INF
) {
1089 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
1090 rt_rq
->rt_time
+= delta_exec
;
1091 exceeded
= sched_rt_runtime_exceeded(rt_rq
);
1094 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
1096 do_start_rt_bandwidth(sched_rt_bandwidth(rt_rq
));
1102 dequeue_top_rt_rq(struct rt_rq
*rt_rq
)
1104 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1106 BUG_ON(&rq
->rt
!= rt_rq
);
1108 if (!rt_rq
->rt_queued
)
1111 BUG_ON(!rq
->nr_running
);
1113 sub_nr_running(rq
, rt_rq
->rt_nr_running
);
1114 rt_rq
->rt_queued
= 0;
1118 enqueue_top_rt_rq(struct rt_rq
*rt_rq
)
1120 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1122 BUG_ON(&rq
->rt
!= rt_rq
);
1124 if (rt_rq
->rt_queued
)
1126 if (rt_rq_throttled(rt_rq
) || !rt_rq
->rt_nr_running
)
1129 add_nr_running(rq
, rt_rq
->rt_nr_running
);
1130 rt_rq
->rt_queued
= 1;
1133 #if defined CONFIG_SMP
1136 inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1138 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1140 #ifdef CONFIG_RT_GROUP_SCHED
1142 * Change rq's cpupri only if rt_rq is the top queue.
1144 if (&rq
->rt
!= rt_rq
)
1147 if (rq
->online
&& prio
< prev_prio
)
1148 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, prio
);
1152 dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1154 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1156 #ifdef CONFIG_RT_GROUP_SCHED
1158 * Change rq's cpupri only if rt_rq is the top queue.
1160 if (&rq
->rt
!= rt_rq
)
1163 if (rq
->online
&& rt_rq
->highest_prio
.curr
!= prev_prio
)
1164 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rt_rq
->highest_prio
.curr
);
1167 #else /* CONFIG_SMP */
1170 void inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1172 void dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1174 #endif /* CONFIG_SMP */
1176 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1178 inc_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1180 int prev_prio
= rt_rq
->highest_prio
.curr
;
1182 if (prio
< prev_prio
)
1183 rt_rq
->highest_prio
.curr
= prio
;
1185 inc_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1189 dec_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1191 int prev_prio
= rt_rq
->highest_prio
.curr
;
1193 if (rt_rq
->rt_nr_running
) {
1195 WARN_ON(prio
< prev_prio
);
1198 * This may have been our highest task, and therefore
1199 * we may have some recomputation to do
1201 if (prio
== prev_prio
) {
1202 struct rt_prio_array
*array
= &rt_rq
->active
;
1204 rt_rq
->highest_prio
.curr
=
1205 sched_find_first_bit(array
->bitmap
);
1209 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
1211 dec_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1216 static inline void inc_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1217 static inline void dec_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1219 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1221 #ifdef CONFIG_RT_GROUP_SCHED
1224 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1226 if (rt_se_boosted(rt_se
))
1227 rt_rq
->rt_nr_boosted
++;
1230 start_rt_bandwidth(&rt_rq
->tg
->rt_bandwidth
);
1234 dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1236 if (rt_se_boosted(rt_se
))
1237 rt_rq
->rt_nr_boosted
--;
1239 WARN_ON(!rt_rq
->rt_nr_running
&& rt_rq
->rt_nr_boosted
);
1242 #else /* CONFIG_RT_GROUP_SCHED */
1245 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1247 start_rt_bandwidth(&def_rt_bandwidth
);
1251 void dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
) {}
1253 #endif /* CONFIG_RT_GROUP_SCHED */
1256 unsigned int rt_se_nr_running(struct sched_rt_entity
*rt_se
)
1258 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1261 return group_rq
->rt_nr_running
;
1267 unsigned int rt_se_rr_nr_running(struct sched_rt_entity
*rt_se
)
1269 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1270 struct task_struct
*tsk
;
1273 return group_rq
->rr_nr_running
;
1275 tsk
= rt_task_of(rt_se
);
1277 return (tsk
->policy
== SCHED_RR
) ? 1 : 0;
1281 void inc_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1283 int prio
= rt_se_prio(rt_se
);
1285 WARN_ON(!rt_prio(prio
));
1286 rt_rq
->rt_nr_running
+= rt_se_nr_running(rt_se
);
1287 rt_rq
->rr_nr_running
+= rt_se_rr_nr_running(rt_se
);
1289 inc_rt_prio(rt_rq
, prio
);
1290 inc_rt_migration(rt_se
, rt_rq
);
1291 inc_rt_group(rt_se
, rt_rq
);
1295 void dec_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1297 WARN_ON(!rt_prio(rt_se_prio(rt_se
)));
1298 WARN_ON(!rt_rq
->rt_nr_running
);
1299 rt_rq
->rt_nr_running
-= rt_se_nr_running(rt_se
);
1300 rt_rq
->rr_nr_running
-= rt_se_rr_nr_running(rt_se
);
1302 dec_rt_prio(rt_rq
, rt_se_prio(rt_se
));
1303 dec_rt_migration(rt_se
, rt_rq
);
1304 dec_rt_group(rt_se
, rt_rq
);
1309 attach_rt_entity_load_avg(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
)
1311 rt_se
->avg
.last_update_time
= rt_rq
->avg
.last_update_time
;
1312 rt_rq
->avg
.util_avg
+= rt_se
->avg
.util_avg
;
1313 rt_rq
->avg
.util_sum
+= rt_se
->avg
.util_sum
;
1314 rt_rq
->avg
.load_avg
+= rt_se
->avg
.load_avg
;
1315 rt_rq
->avg
.load_sum
+= rt_se
->avg
.load_sum
;
1316 #ifdef CONFIG_RT_GROUP_SCHED
1317 rt_rq
->propagate_avg
= 1;
1319 rt_rq_util_change(rt_rq
);
1323 detach_rt_entity_load_avg(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
)
1325 sub_positive(&rt_rq
->avg
.util_avg
, rt_se
->avg
.util_avg
);
1326 sub_positive(&rt_rq
->avg
.util_sum
, rt_se
->avg
.util_sum
);
1327 sub_positive(&rt_rq
->avg
.load_avg
, rt_se
->avg
.load_avg
);
1328 sub_positive(&rt_rq
->avg
.load_sum
, rt_se
->avg
.load_sum
);
1329 #ifdef CONFIG_RT_GROUP_SCHED
1330 rt_rq
->propagate_avg
= 1;
1332 rt_rq_util_change(rt_rq
);
1336 attach_rt_entity_load_avg(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
) {}
1338 detach_rt_entity_load_avg(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
) {}
1342 * Change rt_se->run_list location unless SAVE && !MOVE
1344 * assumes ENQUEUE/DEQUEUE flags match
1346 static inline bool move_entity(unsigned int flags
)
1348 if ((flags
& (DEQUEUE_SAVE
| DEQUEUE_MOVE
)) == DEQUEUE_SAVE
)
1354 static void __delist_rt_entity(struct sched_rt_entity
*rt_se
, struct rt_prio_array
*array
)
1356 list_del_init(&rt_se
->run_list
);
1358 if (list_empty(array
->queue
+ rt_se_prio(rt_se
)))
1359 __clear_bit(rt_se_prio(rt_se
), array
->bitmap
);
1364 static void __enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1366 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1367 struct rt_prio_array
*array
= &rt_rq
->active
;
1368 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1369 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1372 * Don't enqueue the group if its throttled, or when empty.
1373 * The latter is a consequence of the former when a child group
1374 * get throttled and the current group doesn't have any other
1377 if (group_rq
&& (rt_rq_throttled(group_rq
) || !group_rq
->rt_nr_running
)) {
1379 __delist_rt_entity(rt_se
, array
);
1383 if (move_entity(flags
)) {
1384 WARN_ON_ONCE(rt_se
->on_list
);
1385 if (flags
& ENQUEUE_HEAD
)
1386 list_add(&rt_se
->run_list
, queue
);
1388 list_add_tail(&rt_se
->run_list
, queue
);
1390 __set_bit(rt_se_prio(rt_se
), array
->bitmap
);
1395 update_rt_load_avg(rq_clock_task(rq_of_rt_rq(rt_rq
)), rt_se
);
1397 if (rt_entity_is_task(rt_se
) && !rt_se
->avg
.last_update_time
)
1398 attach_rt_entity_load_avg(rt_rq
, rt_se
);
1400 inc_rt_tasks(rt_se
, rt_rq
);
1403 static void __dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1405 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1406 struct rt_prio_array
*array
= &rt_rq
->active
;
1408 if (move_entity(flags
)) {
1409 WARN_ON_ONCE(!rt_se
->on_list
);
1410 __delist_rt_entity(rt_se
, array
);
1414 update_rt_load_avg(rq_clock_task(rq_of_rt_rq(rt_rq
)), rt_se
);
1416 dec_rt_tasks(rt_se
, rt_rq
);
1420 * Because the prio of an upper entry depends on the lower
1421 * entries, we must remove entries top - down.
1423 static void dequeue_rt_stack(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1425 struct sched_rt_entity
*back
= NULL
;
1427 for_each_sched_rt_entity(rt_se
) {
1432 dequeue_top_rt_rq(rt_rq_of_se(back
));
1434 for (rt_se
= back
; rt_se
; rt_se
= rt_se
->back
) {
1435 if (on_rt_rq(rt_se
))
1436 __dequeue_rt_entity(rt_se
, flags
);
1440 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1442 struct rq
*rq
= rq_of_rt_se(rt_se
);
1444 dequeue_rt_stack(rt_se
, flags
);
1445 for_each_sched_rt_entity(rt_se
)
1446 __enqueue_rt_entity(rt_se
, flags
);
1447 enqueue_top_rt_rq(&rq
->rt
);
1450 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1452 struct rq
*rq
= rq_of_rt_se(rt_se
);
1454 dequeue_rt_stack(rt_se
, flags
);
1456 for_each_sched_rt_entity(rt_se
) {
1457 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1459 if (rt_rq
&& rt_rq
->rt_nr_running
)
1460 __enqueue_rt_entity(rt_se
, flags
);
1462 enqueue_top_rt_rq(&rq
->rt
);
1466 * Adding/removing a task to/from a priority array:
1469 enqueue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1471 struct sched_rt_entity
*rt_se
= &p
->rt
;
1473 schedtune_enqueue_task(p
, cpu_of(rq
));
1475 if (flags
& ENQUEUE_WAKEUP
)
1478 enqueue_rt_entity(rt_se
, flags
);
1479 walt_inc_cumulative_runnable_avg(rq
, p
);
1481 if (!task_current(rq
, p
) && p
->nr_cpus_allowed
> 1)
1482 enqueue_pushable_task(rq
, p
);
1485 static void dequeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1487 struct sched_rt_entity
*rt_se
= &p
->rt
;
1489 schedtune_dequeue_task(p
, cpu_of(rq
));
1492 dequeue_rt_entity(rt_se
, flags
);
1493 walt_dec_cumulative_runnable_avg(rq
, p
);
1495 dequeue_pushable_task(rq
, p
);
1499 * Put task to the head or the end of the run list without the overhead of
1500 * dequeue followed by enqueue.
1503 requeue_rt_entity(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
, int head
)
1505 if (on_rt_rq(rt_se
)) {
1506 struct rt_prio_array
*array
= &rt_rq
->active
;
1507 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1510 list_move(&rt_se
->run_list
, queue
);
1512 list_move_tail(&rt_se
->run_list
, queue
);
1516 static void requeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int head
)
1518 struct sched_rt_entity
*rt_se
= &p
->rt
;
1519 struct rt_rq
*rt_rq
;
1521 for_each_sched_rt_entity(rt_se
) {
1522 rt_rq
= rt_rq_of_se(rt_se
);
1523 requeue_rt_entity(rt_rq
, rt_se
, head
);
1527 static void yield_task_rt(struct rq
*rq
)
1529 requeue_task_rt(rq
, rq
->curr
, 0);
1535 * attach/detach/migrate_task_rt_rq() for load tracking
1538 static int find_lowest_rq(struct task_struct
*task
);
1541 select_task_rq_rt(struct task_struct
*p
, int cpu
, int sd_flag
, int flags
,
1542 int sibling_count_hint
)
1544 struct task_struct
*curr
;
1547 /* For anything but wake ups, just return the task_cpu */
1548 if (sd_flag
!= SD_BALANCE_WAKE
&& sd_flag
!= SD_BALANCE_FORK
)
1554 curr
= READ_ONCE(rq
->curr
); /* unlocked access */
1557 * If the current task on @p's runqueue is an RT task, then
1558 * try to see if we can wake this RT task up on another
1559 * runqueue. Otherwise simply start this RT task
1560 * on its current runqueue.
1562 * We want to avoid overloading runqueues. If the woken
1563 * task is a higher priority, then it will stay on this CPU
1564 * and the lower prio task should be moved to another CPU.
1565 * Even though this will probably make the lower prio task
1566 * lose its cache, we do not want to bounce a higher task
1567 * around just because it gave up its CPU, perhaps for a
1570 * For equal prio tasks, we just let the scheduler sort it out.
1572 * Otherwise, just let it ride on the affined RQ and the
1573 * post-schedule router will push the preempted task away
1575 * This test is optimistic, if we get it wrong the load-balancer
1576 * will have to sort it out.
1578 if (curr
&& unlikely(rt_task(curr
)) &&
1579 (curr
->nr_cpus_allowed
< 2 ||
1580 curr
->prio
<= p
->prio
)) {
1581 int target
= find_lowest_rq(p
);
1584 * Don't bother moving it if the destination CPU is
1585 * not running a lower priority task.
1588 p
->prio
< cpu_rq(target
)->rt
.highest_prio
.curr
)
1597 #ifdef CONFIG_RT_GROUP_SCHED
1599 * Called within set_task_rq() right before setting a task's cpu. The
1600 * caller only guarantees p->pi_lock is held; no other assumptions,
1601 * including the state of rq->lock, should be made.
1603 void set_task_rq_rt(struct sched_rt_entity
*rt_se
,
1604 struct rt_rq
*prev
, struct rt_rq
*next
)
1606 u64 p_last_update_time
;
1607 u64 n_last_update_time
;
1609 if (!sched_feat(ATTACH_AGE_LOAD
))
1612 * We are supposed to update the task to "current" time, then its up to
1613 * date and ready to go to new CPU/rt_rq. But we have difficulty in
1614 * getting what current time is, so simply throw away the out-of-date
1615 * time. This will result in the wakee task is less decayed, but giving
1616 * the wakee more load sounds not bad.
1618 if (!(rt_se
->avg
.last_update_time
&& prev
))
1620 #ifndef CONFIG_64BIT
1622 u64 p_last_update_time_copy
;
1623 u64 n_last_update_time_copy
;
1626 p_last_update_time_copy
= prev
->load_last_update_time_copy
;
1627 n_last_update_time_copy
= next
->load_last_update_time_copy
;
1631 p_last_update_time
= prev
->avg
.last_update_time
;
1632 n_last_update_time
= next
->avg
.last_update_time
;
1634 } while (p_last_update_time
!= p_last_update_time_copy
||
1635 n_last_update_time
!= n_last_update_time_copy
);
1638 p_last_update_time
= prev
->avg
.last_update_time
;
1639 n_last_update_time
= next
->avg
.last_update_time
;
1641 __update_load_avg(p_last_update_time
, cpu_of(rq_of_rt_rq(prev
)),
1642 &rt_se
->avg
, 0, 0, NULL
);
1644 rt_se
->avg
.last_update_time
= n_last_update_time
;
1646 #endif /* CONFIG_RT_GROUP_SCHED */
1648 #ifndef CONFIG_64BIT
1649 static inline u64
rt_rq_last_update_time(struct rt_rq
*rt_rq
)
1651 u64 last_update_time_copy
;
1652 u64 last_update_time
;
1655 last_update_time_copy
= rt_rq
->load_last_update_time_copy
;
1657 last_update_time
= rt_rq
->avg
.last_update_time
;
1658 } while (last_update_time
!= last_update_time_copy
);
1660 return last_update_time
;
1663 static inline u64
rt_rq_last_update_time(struct rt_rq
*rt_rq
)
1665 return rt_rq
->avg
.last_update_time
;
1670 * Synchronize entity load avg of dequeued entity without locking
1673 void sync_rt_entity_load_avg(struct sched_rt_entity
*rt_se
)
1675 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1676 u64 last_update_time
;
1678 last_update_time
= rt_rq_last_update_time(rt_rq
);
1679 update_rt_load_avg(last_update_time
, rt_se
);
1683 * Task first catches up with rt_rq, and then subtract
1684 * itself from the rt_rq (task must be off the queue now).
1686 static void remove_rt_entity_load_avg(struct sched_rt_entity
*rt_se
)
1688 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1691 * tasks cannot exit without having gone through wake_up_new_task() ->
1692 * post_init_entity_util_avg() which will have added things to the
1693 * rt_rq, so we can remove unconditionally.
1695 * Similarly for groups, they will have passed through
1696 * post_init_entity_util_avg() before unregister_sched_fair_group()
1700 sync_rt_entity_load_avg(rt_se
);
1701 atomic_long_add(rt_se
->avg
.load_avg
, &rt_rq
->removed_load_avg
);
1702 atomic_long_add(rt_se
->avg
.util_avg
, &rt_rq
->removed_util_avg
);
1705 static void attach_task_rt_rq(struct task_struct
*p
)
1707 struct sched_rt_entity
*rt_se
= &p
->rt
;
1708 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1709 u64 now
= rq_clock_task(rq_of_rt_rq(rt_rq
));
1711 update_rt_load_avg(now
, rt_se
);
1712 attach_rt_entity_load_avg(rt_rq
, rt_se
);
1715 static void detach_task_rt_rq(struct task_struct
*p
)
1717 struct sched_rt_entity
*rt_se
= &p
->rt
;
1718 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1719 u64 now
= rq_clock_task(rq_of_rt_rq(rt_rq
));
1721 update_rt_load_avg(now
, rt_se
);
1722 detach_rt_entity_load_avg(rt_rq
, rt_se
);
1725 static void migrate_task_rq_rt(struct task_struct
*p
)
1728 * We are supposed to update the task to "current" time, then its up to date
1729 * and ready to go to new CPU/cfs_rq. But we have difficulty in getting
1730 * what current time is, so simply throw away the out-of-date time. This
1731 * will result in the wakee task is less decayed, but giving the wakee more
1732 * load sounds not bad.
1734 remove_rt_entity_load_avg(&p
->rt
);
1736 /* Tell new CPU we are migrated */
1737 p
->rt
.avg
.last_update_time
= 0;
1739 /* We have migrated, no longer consider this task hot */
1740 p
->se
.exec_start
= 0;
1743 static void task_dead_rt(struct task_struct
*p
)
1745 remove_rt_entity_load_avg(&p
->rt
);
1748 #ifdef CONFIG_RT_GROUP_SCHED
1749 static void task_set_group_rt(struct task_struct
*p
)
1751 set_task_rq(p
, task_cpu(p
));
1754 static void task_move_group_rt(struct task_struct
*p
)
1756 detach_task_rt_rq(p
);
1757 set_task_rq(p
, task_cpu(p
));
1760 /* Tell se's cfs_rq has been changed -- migrated */
1761 p
->se
.avg
.last_update_time
= 0;
1763 attach_task_rt_rq(p
);
1766 static void task_change_group_rt(struct task_struct
*p
, int type
)
1769 case TASK_SET_GROUP
:
1770 task_set_group_rt(p
);
1773 case TASK_MOVE_GROUP
:
1774 task_move_group_rt(p
);
1780 static void check_preempt_equal_prio(struct rq
*rq
, struct task_struct
*p
)
1783 * Current can't be migrated, useless to reschedule,
1784 * let's hope p can move out.
1786 if (rq
->curr
->nr_cpus_allowed
== 1 ||
1787 !cpupri_find(&rq
->rd
->cpupri
, rq
->curr
, NULL
))
1791 * p is migratable, so let's not schedule it and
1792 * see if it is pushed or pulled somewhere else.
1794 if (p
->nr_cpus_allowed
!= 1
1795 && cpupri_find(&rq
->rd
->cpupri
, p
, NULL
))
1799 * There appears to be other cpus that can accept
1800 * current and none to run 'p', so lets reschedule
1801 * to try and push current away:
1803 requeue_task_rt(rq
, p
, 1);
1807 /* Give new sched_entity start runnable values to heavy its load in infant time */
1808 void init_rt_entity_runnable_average(struct sched_rt_entity
*rt_se
)
1810 struct sched_avg
*sa
= &rt_se
->avg
;
1812 sa
->last_update_time
= 0;
1814 sa
->period_contrib
= 1023;
1817 * Tasks are intialized with zero load.
1818 * Load is not actually used by RT, but can be inherited into fair task.
1823 * At this point, util_avg won't be used in select_task_rq_rt anyway
1827 /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */
1830 void init_rt_entity_runnable_average(struct sched_rt_entity
*rt_se
) { }
1831 #endif /* CONFIG_SMP */
1834 * Preempt the current task with a newly woken task if needed:
1836 static void check_preempt_curr_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1838 if (p
->prio
< rq
->curr
->prio
) {
1847 * - the newly woken task is of equal priority to the current task
1848 * - the newly woken task is non-migratable while current is migratable
1849 * - current will be preempted on the next reschedule
1851 * we should check to see if current can readily move to a different
1852 * cpu. If so, we will reschedule to allow the push logic to try
1853 * to move current somewhere else, making room for our non-migratable
1856 if (p
->prio
== rq
->curr
->prio
&& !test_tsk_need_resched(rq
->curr
))
1857 check_preempt_equal_prio(rq
, p
);
1861 static struct sched_rt_entity
*pick_next_rt_entity(struct rq
*rq
,
1862 struct rt_rq
*rt_rq
)
1864 struct rt_prio_array
*array
= &rt_rq
->active
;
1865 struct sched_rt_entity
*next
= NULL
;
1866 struct list_head
*queue
;
1869 idx
= sched_find_first_bit(array
->bitmap
);
1870 BUG_ON(idx
>= MAX_RT_PRIO
);
1872 queue
= array
->queue
+ idx
;
1873 next
= list_entry(queue
->next
, struct sched_rt_entity
, run_list
);
1878 static struct task_struct
*_pick_next_task_rt(struct rq
*rq
)
1880 struct sched_rt_entity
*rt_se
;
1881 struct task_struct
*p
;
1882 struct rt_rq
*rt_rq
= &rq
->rt
;
1883 u64 now
= rq_clock_task(rq
);
1886 rt_se
= pick_next_rt_entity(rq
, rt_rq
);
1888 update_rt_load_avg(now
, rt_se
);
1889 rt_rq
->curr
= rt_se
;
1890 rt_rq
= group_rt_rq(rt_se
);
1893 p
= rt_task_of(rt_se
);
1894 p
->se
.exec_start
= now
;
1899 extern int update_rt_rq_load_avg(u64 now
, int cpu
, struct rt_rq
*rt_rq
, int running
);
1901 static struct task_struct
*
1902 pick_next_task_rt(struct rq
*rq
, struct task_struct
*prev
, struct rq_flags
*rf
)
1904 struct task_struct
*p
;
1905 struct rt_rq
*rt_rq
= &rq
->rt
;
1907 if (need_pull_rt_task(rq
, prev
)) {
1909 * This is OK, because current is on_cpu, which avoids it being
1910 * picked for load-balance and preemption/IRQs are still
1911 * disabled avoiding further scheduler activity on it and we're
1912 * being very careful to re-start the picking loop.
1914 rq_unpin_lock(rq
, rf
);
1916 rq_repin_lock(rq
, rf
);
1918 * pull_rt_task() can drop (and re-acquire) rq->lock; this
1919 * means a dl or stop task can slip in, in which case we need
1920 * to re-start task selection.
1922 if (unlikely((rq
->stop
&& task_on_rq_queued(rq
->stop
)) ||
1923 rq
->dl
.dl_nr_running
))
1928 * We may dequeue prev's rt_rq in put_prev_task().
1929 * So, we update time before rt_nr_running check.
1931 if (prev
->sched_class
== &rt_sched_class
)
1934 if (!rt_rq
->rt_queued
)
1937 put_prev_task(rq
, prev
);
1939 p
= _pick_next_task_rt(rq
);
1941 /* The running task is never eligible for pushing */
1942 dequeue_pushable_task(rq
, p
);
1944 queue_push_tasks(rq
);
1947 update_rt_rq_load_avg(rq_clock_task(rq
), cpu_of(rq
), rt_rq
,
1948 rq
->curr
->sched_class
== &rt_sched_class
);
1953 static void put_prev_task_rt(struct rq
*rq
, struct task_struct
*p
)
1955 struct sched_rt_entity
*rt_se
= &p
->rt
;
1956 u64 now
= rq_clock_task(rq
);
1960 update_rt_rq_load_avg(rq_clock_task(rq
), cpu_of(rq
), &rq
->rt
, 1);
1963 * The previous task needs to be made eligible for pushing
1964 * if it is still active
1966 if (on_rt_rq(&p
->rt
) && p
->nr_cpus_allowed
> 1)
1967 enqueue_pushable_task(rq
, p
);
1969 for_each_sched_rt_entity(rt_se
) {
1970 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1972 update_rt_load_avg(now
, rt_se
);
1980 void rt_rq_util_change(struct rt_rq
*rt_rq
)
1982 if (&this_rq()->rt
== rt_rq
)
1983 cpufreq_update_util(rt_rq
->rq
, SCHED_CPUFREQ_RT
);
1986 #ifdef CONFIG_RT_GROUP_SCHED
1987 /* Take into account change of utilization of a child task group */
1989 update_tg_rt_util(struct rt_rq
*cfs_rq
, struct sched_rt_entity
*rt_se
)
1991 struct rt_rq
*grt_rq
= rt_se
->my_q
;
1992 long delta
= grt_rq
->avg
.util_avg
- rt_se
->avg
.util_avg
;
1994 /* Nothing to update */
1998 /* Set new sched_rt_entity's utilization */
1999 rt_se
->avg
.util_avg
= grt_rq
->avg
.util_avg
;
2000 rt_se
->avg
.util_sum
= rt_se
->avg
.util_avg
* LOAD_AVG_MAX
;
2002 /* Update parent rt_rq utilization */
2003 add_positive(&cfs_rq
->avg
.util_avg
, delta
);
2004 cfs_rq
->avg
.util_sum
= cfs_rq
->avg
.util_avg
* LOAD_AVG_MAX
;
2008 /* Take into account change of load of a child task group */
2010 update_tg_rt_load(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
)
2012 struct rt_rq
*grt_rq
= rt_se
->my_q
;
2013 long delta
= grt_rq
->avg
.load_avg
- rt_se
->avg
.load_avg
;
2016 * TODO: Need to consider the TG group update
2020 /* Nothing to update */
2024 /* Set new sched_rt_entity's load */
2025 rt_se
->avg
.load_avg
= grt_rq
->avg
.load_avg
;
2026 rt_se
->avg
.load_sum
= rt_se
->avg
.load_avg
* LOAD_AVG_MAX
;
2028 /* Update parent cfs_rq load */
2029 add_positive(&rt_rq
->avg
.load_avg
, delta
);
2030 rt_rq
->avg
.load_sum
= rt_rq
->avg
.load_avg
* LOAD_AVG_MAX
;
2033 * TODO: If the sched_entity is already enqueued, should we have to update the
2034 * runnable load avg.
2038 static inline int test_and_clear_tg_rt_propagate(struct sched_rt_entity
*rt_se
)
2040 struct rt_rq
*rt_rq
= rt_se
->my_q
;
2042 if (!rt_rq
->propagate_avg
)
2045 rt_rq
->propagate_avg
= 0;
2049 /* Update task and its cfs_rq load average */
2050 static inline int propagate_entity_rt_load_avg(struct sched_rt_entity
*rt_se
)
2052 struct rt_rq
*rt_rq
;
2054 if (rt_entity_is_task(rt_se
))
2057 if (!test_and_clear_tg_rt_propagate(rt_se
))
2060 rt_rq
= rt_rq_of_se(rt_se
);
2062 rt_rq
->propagate_avg
= 1;
2064 update_tg_rt_util(rt_rq
, rt_se
);
2065 update_tg_rt_load(rt_rq
, rt_se
);
2070 static inline int propagate_entity_rt_load_avg(struct sched_rt_entity
*rt_se
) { };
2073 void update_rt_load_avg(u64 now
, struct sched_rt_entity
*rt_se
)
2075 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
2076 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
2077 int cpu
= cpu_of(rq
);
2079 * Track task load average for carrying it to new CPU after migrated.
2081 if (rt_se
->avg
.last_update_time
)
2082 __update_load_avg(now
, cpu
, &rt_se
->avg
, scale_load_down(NICE_0_LOAD
),
2083 rt_rq
->curr
== rt_se
, NULL
);
2085 update_rt_rq_load_avg(now
, cpu
, rt_rq
, true);
2086 propagate_entity_rt_load_avg(rt_se
);
2089 /* Only try algorithms three times */
2090 #define RT_MAX_TRIES 3
2092 static int pick_rt_task(struct rq
*rq
, struct task_struct
*p
, int cpu
)
2094 if (!task_running(rq
, p
) &&
2095 cpumask_test_cpu(cpu
, &p
->cpus_allowed
))
2101 * Return the highest pushable rq's task, which is suitable to be executed
2102 * on the cpu, NULL otherwise
2104 static struct task_struct
*pick_highest_pushable_task(struct rq
*rq
, int cpu
)
2106 struct plist_head
*head
= &rq
->rt
.pushable_tasks
;
2107 struct task_struct
*p
;
2109 if (!has_pushable_tasks(rq
))
2112 plist_for_each_entry(p
, head
, pushable_tasks
) {
2113 if (pick_rt_task(rq
, p
, cpu
))
2120 static DEFINE_PER_CPU(cpumask_var_t
, local_cpu_mask
);
2122 static int find_lowest_rq(struct task_struct
*task
)
2124 struct sched_domain
*sd
;
2125 struct cpumask
*lowest_mask
= this_cpu_cpumask_var_ptr(local_cpu_mask
);
2126 int this_cpu
= smp_processor_id();
2127 int cpu
= task_cpu(task
);
2129 /* Make sure the mask is initialized first */
2130 if (unlikely(!lowest_mask
))
2133 if (task
->nr_cpus_allowed
== 1)
2134 return -1; /* No other targets possible */
2136 if (!cpupri_find(&task_rq(task
)->rd
->cpupri
, task
, lowest_mask
))
2137 return -1; /* No targets found */
2140 * At this point we have built a mask of cpus representing the
2141 * lowest priority tasks in the system. Now we want to elect
2142 * the best one based on our affinity and topology.
2144 * We prioritize the last cpu that the task executed on since
2145 * it is most likely cache-hot in that location.
2147 if (cpumask_test_cpu(cpu
, lowest_mask
))
2151 * Otherwise, we consult the sched_domains span maps to figure
2152 * out which cpu is logically closest to our hot cache data.
2154 if (!cpumask_test_cpu(this_cpu
, lowest_mask
))
2155 this_cpu
= -1; /* Skip this_cpu opt if not among lowest */
2158 for_each_domain(cpu
, sd
) {
2159 if (sd
->flags
& SD_WAKE_AFFINE
) {
2163 * "this_cpu" is cheaper to preempt than a
2166 if (this_cpu
!= -1 &&
2167 cpumask_test_cpu(this_cpu
, sched_domain_span(sd
))) {
2172 best_cpu
= cpumask_first_and(lowest_mask
,
2173 sched_domain_span(sd
));
2174 if (best_cpu
< nr_cpu_ids
) {
2183 * And finally, if there were no matches within the domains
2184 * just give the caller *something* to work with from the compatible
2190 cpu
= cpumask_any(lowest_mask
);
2191 if (cpu
< nr_cpu_ids
)
2196 /* Will lock the rq it finds */
2197 static struct rq
*find_lock_lowest_rq(struct task_struct
*task
, struct rq
*rq
)
2199 struct rq
*lowest_rq
= NULL
;
2203 for (tries
= 0; tries
< RT_MAX_TRIES
; tries
++) {
2204 cpu
= find_lowest_rq(task
);
2206 if ((cpu
== -1) || (cpu
== rq
->cpu
))
2209 lowest_rq
= cpu_rq(cpu
);
2211 if (lowest_rq
->rt
.highest_prio
.curr
<= task
->prio
) {
2213 * Target rq has tasks of equal or higher priority,
2214 * retrying does not release any lock and is unlikely
2215 * to yield a different result.
2221 /* if the prio of this runqueue changed, try again */
2222 if (double_lock_balance(rq
, lowest_rq
)) {
2224 * We had to unlock the run queue. In
2225 * the mean time, task could have
2226 * migrated already or had its affinity changed.
2227 * Also make sure that it wasn't scheduled on its rq.
2229 if (unlikely(task_rq(task
) != rq
||
2230 !cpumask_test_cpu(lowest_rq
->cpu
, &task
->cpus_allowed
) ||
2231 task_running(rq
, task
) ||
2233 !task_on_rq_queued(task
))) {
2235 double_unlock_balance(rq
, lowest_rq
);
2241 /* If this rq is still suitable use it. */
2242 if (lowest_rq
->rt
.highest_prio
.curr
> task
->prio
)
2246 double_unlock_balance(rq
, lowest_rq
);
2253 static struct task_struct
*pick_next_pushable_task(struct rq
*rq
)
2255 struct task_struct
*p
;
2257 if (!has_pushable_tasks(rq
))
2260 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
2261 struct task_struct
, pushable_tasks
);
2263 BUG_ON(rq
->cpu
!= task_cpu(p
));
2264 BUG_ON(task_current(rq
, p
));
2265 BUG_ON(p
->nr_cpus_allowed
<= 1);
2267 BUG_ON(!task_on_rq_queued(p
));
2268 BUG_ON(!rt_task(p
));
2274 * If the current CPU has more than one RT task, see if the non
2275 * running task can migrate over to a CPU that is running a task
2276 * of lesser priority.
2278 static int push_rt_task(struct rq
*rq
)
2280 struct task_struct
*next_task
;
2281 struct rq
*lowest_rq
;
2284 if (!rq
->rt
.overloaded
)
2287 next_task
= pick_next_pushable_task(rq
);
2292 if (unlikely(next_task
== rq
->curr
)) {
2298 * It's possible that the next_task slipped in of
2299 * higher priority than current. If that's the case
2300 * just reschedule current.
2302 if (unlikely(next_task
->prio
< rq
->curr
->prio
)) {
2307 /* We might release rq lock */
2308 get_task_struct(next_task
);
2310 /* find_lock_lowest_rq locks the rq if found */
2311 lowest_rq
= find_lock_lowest_rq(next_task
, rq
);
2313 struct task_struct
*task
;
2315 * find_lock_lowest_rq releases rq->lock
2316 * so it is possible that next_task has migrated.
2318 * We need to make sure that the task is still on the same
2319 * run-queue and is also still the next task eligible for
2322 task
= pick_next_pushable_task(rq
);
2323 if (task
== next_task
) {
2325 * The task hasn't migrated, and is still the next
2326 * eligible task, but we failed to find a run-queue
2327 * to push it to. Do not retry in this case, since
2328 * other cpus will pull from us when ready.
2334 /* No more tasks, just exit */
2338 * Something has shifted, try again.
2340 put_task_struct(next_task
);
2345 deactivate_task(rq
, next_task
, 0);
2346 next_task
->on_rq
= TASK_ON_RQ_MIGRATING
;
2347 set_task_cpu(next_task
, lowest_rq
->cpu
);
2348 next_task
->on_rq
= TASK_ON_RQ_QUEUED
;
2349 activate_task(lowest_rq
, next_task
, 0);
2352 resched_curr(lowest_rq
);
2354 double_unlock_balance(rq
, lowest_rq
);
2357 put_task_struct(next_task
);
2362 static void push_rt_tasks(struct rq
*rq
)
2364 /* push_rt_task will return true if it moved an RT */
2365 while (push_rt_task(rq
))
2369 #ifdef HAVE_RT_PUSH_IPI
2372 * When a high priority task schedules out from a CPU and a lower priority
2373 * task is scheduled in, a check is made to see if there's any RT tasks
2374 * on other CPUs that are waiting to run because a higher priority RT task
2375 * is currently running on its CPU. In this case, the CPU with multiple RT
2376 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
2377 * up that may be able to run one of its non-running queued RT tasks.
2379 * All CPUs with overloaded RT tasks need to be notified as there is currently
2380 * no way to know which of these CPUs have the highest priority task waiting
2381 * to run. Instead of trying to take a spinlock on each of these CPUs,
2382 * which has shown to cause large latency when done on machines with many
2383 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
2384 * RT tasks waiting to run.
2386 * Just sending an IPI to each of the CPUs is also an issue, as on large
2387 * count CPU machines, this can cause an IPI storm on a CPU, especially
2388 * if its the only CPU with multiple RT tasks queued, and a large number
2389 * of CPUs scheduling a lower priority task at the same time.
2391 * Each root domain has its own irq work function that can iterate over
2392 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
2393 * tassk must be checked if there's one or many CPUs that are lowering
2394 * their priority, there's a single irq work iterator that will try to
2395 * push off RT tasks that are waiting to run.
2397 * When a CPU schedules a lower priority task, it will kick off the
2398 * irq work iterator that will jump to each CPU with overloaded RT tasks.
2399 * As it only takes the first CPU that schedules a lower priority task
2400 * to start the process, the rto_start variable is incremented and if
2401 * the atomic result is one, then that CPU will try to take the rto_lock.
2402 * This prevents high contention on the lock as the process handles all
2403 * CPUs scheduling lower priority tasks.
2405 * All CPUs that are scheduling a lower priority task will increment the
2406 * rt_loop_next variable. This will make sure that the irq work iterator
2407 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
2408 * priority task, even if the iterator is in the middle of a scan. Incrementing
2409 * the rt_loop_next will cause the iterator to perform another scan.
2412 static int rto_next_cpu(struct root_domain
*rd
)
2418 * When starting the IPI RT pushing, the rto_cpu is set to -1,
2419 * rt_next_cpu() will simply return the first CPU found in
2422 * If rto_next_cpu() is called with rto_cpu is a valid cpu, it
2423 * will return the next CPU found in the rto_mask.
2425 * If there are no more CPUs left in the rto_mask, then a check is made
2426 * against rto_loop and rto_loop_next. rto_loop is only updated with
2427 * the rto_lock held, but any CPU may increment the rto_loop_next
2428 * without any locking.
2432 /* When rto_cpu is -1 this acts like cpumask_first() */
2433 cpu
= cpumask_next(rd
->rto_cpu
, rd
->rto_mask
);
2437 if (cpu
< nr_cpu_ids
)
2443 * ACQUIRE ensures we see the @rto_mask changes
2444 * made prior to the @next value observed.
2446 * Matches WMB in rt_set_overload().
2448 next
= atomic_read_acquire(&rd
->rto_loop_next
);
2450 if (rd
->rto_loop
== next
)
2453 rd
->rto_loop
= next
;
2459 static inline bool rto_start_trylock(atomic_t
*v
)
2461 return !atomic_cmpxchg_acquire(v
, 0, 1);
2464 static inline void rto_start_unlock(atomic_t
*v
)
2466 atomic_set_release(v
, 0);
2469 static void tell_cpu_to_push(struct rq
*rq
)
2473 /* Keep the loop going if the IPI is currently active */
2474 atomic_inc(&rq
->rd
->rto_loop_next
);
2476 /* Only one CPU can initiate a loop at a time */
2477 if (!rto_start_trylock(&rq
->rd
->rto_loop_start
))
2480 raw_spin_lock(&rq
->rd
->rto_lock
);
2483 * The rto_cpu is updated under the lock, if it has a valid cpu
2484 * then the IPI is still running and will continue due to the
2485 * update to loop_next, and nothing needs to be done here.
2486 * Otherwise it is finishing up and an ipi needs to be sent.
2488 if (rq
->rd
->rto_cpu
< 0)
2489 cpu
= rto_next_cpu(rq
->rd
);
2491 raw_spin_unlock(&rq
->rd
->rto_lock
);
2493 rto_start_unlock(&rq
->rd
->rto_loop_start
);
2496 /* Make sure the rd does not get freed while pushing */
2497 sched_get_rd(rq
->rd
);
2498 irq_work_queue_on(&rq
->rd
->rto_push_work
, cpu
);
2502 /* Called from hardirq context */
2503 void rto_push_irq_work_func(struct irq_work
*work
)
2505 struct root_domain
*rd
=
2506 container_of(work
, struct root_domain
, rto_push_work
);
2513 * We do not need to grab the lock to check for has_pushable_tasks.
2514 * When it gets updated, a check is made if a push is possible.
2516 if (has_pushable_tasks(rq
)) {
2517 raw_spin_lock(&rq
->lock
);
2519 raw_spin_unlock(&rq
->lock
);
2522 raw_spin_lock(&rd
->rto_lock
);
2524 /* Pass the IPI to the next rt overloaded queue */
2525 cpu
= rto_next_cpu(rd
);
2527 raw_spin_unlock(&rd
->rto_lock
);
2534 /* Try the next RT overloaded CPU */
2535 irq_work_queue_on(&rd
->rto_push_work
, cpu
);
2537 #endif /* HAVE_RT_PUSH_IPI */
2539 static void pull_rt_task(struct rq
*this_rq
)
2541 int this_cpu
= this_rq
->cpu
, cpu
;
2542 bool resched
= false;
2543 struct task_struct
*p
;
2545 int rt_overload_count
= rt_overloaded(this_rq
);
2547 if (likely(!rt_overload_count
))
2551 * Match the barrier from rt_set_overloaded; this guarantees that if we
2552 * see overloaded we must also see the rto_mask bit.
2556 /* If we are the only overloaded CPU do nothing */
2557 if (rt_overload_count
== 1 &&
2558 cpumask_test_cpu(this_rq
->cpu
, this_rq
->rd
->rto_mask
))
2561 #ifdef HAVE_RT_PUSH_IPI
2562 if (sched_feat(RT_PUSH_IPI
)) {
2563 tell_cpu_to_push(this_rq
);
2568 for_each_cpu(cpu
, this_rq
->rd
->rto_mask
) {
2569 if (this_cpu
== cpu
)
2572 src_rq
= cpu_rq(cpu
);
2575 * Don't bother taking the src_rq->lock if the next highest
2576 * task is known to be lower-priority than our current task.
2577 * This may look racy, but if this value is about to go
2578 * logically higher, the src_rq will push this task away.
2579 * And if its going logically lower, we do not care
2581 if (src_rq
->rt
.highest_prio
.next
>=
2582 this_rq
->rt
.highest_prio
.curr
)
2586 * We can potentially drop this_rq's lock in
2587 * double_lock_balance, and another CPU could
2590 double_lock_balance(this_rq
, src_rq
);
2593 * We can pull only a task, which is pushable
2594 * on its rq, and no others.
2596 p
= pick_highest_pushable_task(src_rq
, this_cpu
);
2599 * Do we have an RT task that preempts
2600 * the to-be-scheduled task?
2602 if (p
&& (p
->prio
< this_rq
->rt
.highest_prio
.curr
)) {
2603 WARN_ON(p
== src_rq
->curr
);
2604 WARN_ON(!task_on_rq_queued(p
));
2607 * There's a chance that p is higher in priority
2608 * than what's currently running on its cpu.
2609 * This is just that p is wakeing up and hasn't
2610 * had a chance to schedule. We only pull
2611 * p if it is lower in priority than the
2612 * current task on the run queue
2614 if (p
->prio
< src_rq
->curr
->prio
)
2619 deactivate_task(src_rq
, p
, 0);
2620 p
->on_rq
= TASK_ON_RQ_MIGRATING
;
2621 set_task_cpu(p
, this_cpu
);
2622 p
->on_rq
= TASK_ON_RQ_QUEUED
;
2623 activate_task(this_rq
, p
, 0);
2625 * We continue with the search, just in
2626 * case there's an even higher prio task
2627 * in another runqueue. (low likelihood
2632 double_unlock_balance(this_rq
, src_rq
);
2636 resched_curr(this_rq
);
2640 * If we are not running and we are not going to reschedule soon, we should
2641 * try to push tasks away now
2643 static void task_woken_rt(struct rq
*rq
, struct task_struct
*p
)
2645 if (!task_running(rq
, p
) &&
2646 !test_tsk_need_resched(rq
->curr
) &&
2647 p
->nr_cpus_allowed
> 1 &&
2648 (dl_task(rq
->curr
) || rt_task(rq
->curr
)) &&
2649 (rq
->curr
->nr_cpus_allowed
< 2 ||
2650 rq
->curr
->prio
<= p
->prio
))
2654 /* Assumes rq->lock is held */
2655 static void rq_online_rt(struct rq
*rq
)
2657 if (rq
->rt
.overloaded
)
2658 rt_set_overload(rq
);
2660 __enable_runtime(rq
);
2662 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rq
->rt
.highest_prio
.curr
);
2665 /* Assumes rq->lock is held */
2666 static void rq_offline_rt(struct rq
*rq
)
2668 if (rq
->rt
.overloaded
)
2669 rt_clear_overload(rq
);
2671 __disable_runtime(rq
);
2673 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, CPUPRI_INVALID
);
2677 * When switch from the rt queue, we bring ourselves to a position
2678 * that we might want to pull RT tasks from other runqueues.
2680 static void switched_from_rt(struct rq
*rq
, struct task_struct
*p
)
2682 detach_task_rt_rq(p
);
2684 * If there are other RT tasks then we will reschedule
2685 * and the scheduling of the other RT tasks will handle
2686 * the balancing. But if we are the last RT task
2687 * we may need to handle the pulling of RT tasks
2690 if (!task_on_rq_queued(p
) || rq
->rt
.rt_nr_running
)
2693 queue_pull_task(rq
);
2696 void __init
init_sched_rt_class(void)
2700 for_each_possible_cpu(i
) {
2701 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask
, i
),
2702 GFP_KERNEL
, cpu_to_node(i
));
2706 void update_rt_load_avg(u64 now
, struct sched_rt_entity
*rt_se
)
2709 #endif /* CONFIG_SMP */
2712 copy_sched_avg(struct sched_avg
*from
, struct sched_avg
*to
, unsigned int ratio
);
2715 * When switching a task to RT, we may overload the runqueue
2716 * with RT tasks. In this case we try to push them off to
2719 static void switched_to_rt(struct rq
*rq
, struct task_struct
*p
)
2721 /* Copy fair sched avg into rt sched avg */
2722 copy_sched_avg(&p
->se
.avg
, &p
->rt
.avg
, 100);
2724 * If we are already running, then there's nothing
2725 * that needs to be done. But if we are not running
2726 * we may need to preempt the current running task.
2727 * If that current running task is also an RT task
2728 * then see if we can move to another run queue.
2730 if (task_on_rq_queued(p
) && rq
->curr
!= p
) {
2732 if (p
->nr_cpus_allowed
> 1 && rq
->rt
.overloaded
)
2733 queue_push_tasks(rq
);
2734 #endif /* CONFIG_SMP */
2735 if (p
->prio
< rq
->curr
->prio
&& cpu_online(cpu_of(rq
)))
2741 * Priority of the task has changed. This may cause
2742 * us to initiate a push or pull.
2745 prio_changed_rt(struct rq
*rq
, struct task_struct
*p
, int oldprio
)
2747 if (!task_on_rq_queued(p
))
2750 if (rq
->curr
== p
) {
2753 * If our priority decreases while running, we
2754 * may need to pull tasks to this runqueue.
2756 if (oldprio
< p
->prio
)
2757 queue_pull_task(rq
);
2760 * If there's a higher priority task waiting to run
2763 if (p
->prio
> rq
->rt
.highest_prio
.curr
)
2766 /* For UP simply resched on drop of prio */
2767 if (oldprio
< p
->prio
)
2769 #endif /* CONFIG_SMP */
2772 * This task is not running, but if it is
2773 * greater than the current running task
2776 if (p
->prio
< rq
->curr
->prio
)
2781 #ifdef CONFIG_POSIX_TIMERS
2782 static void watchdog(struct rq
*rq
, struct task_struct
*p
)
2784 unsigned long soft
, hard
;
2786 /* max may change after cur was read, this will be fixed next tick */
2787 soft
= task_rlimit(p
, RLIMIT_RTTIME
);
2788 hard
= task_rlimit_max(p
, RLIMIT_RTTIME
);
2790 if (soft
!= RLIM_INFINITY
) {
2793 if (p
->rt
.watchdog_stamp
!= jiffies
) {
2795 p
->rt
.watchdog_stamp
= jiffies
;
2798 next
= DIV_ROUND_UP(min(soft
, hard
), USEC_PER_SEC
/HZ
);
2799 if (p
->rt
.timeout
> next
)
2800 p
->cputime_expires
.sched_exp
= p
->se
.sum_exec_runtime
;
2804 static inline void watchdog(struct rq
*rq
, struct task_struct
*p
) { }
2807 static void task_tick_rt(struct rq
*rq
, struct task_struct
*p
, int queued
)
2809 struct sched_rt_entity
*rt_se
= &p
->rt
;
2810 u64 now
= rq_clock_task(rq
);
2813 update_rt_rq_load_avg(now
, cpu_of(rq
), &rq
->rt
, 1);
2815 for_each_sched_rt_entity(rt_se
)
2816 update_rt_load_avg(now
, rt_se
);
2821 * RR tasks need a special form of timeslice management.
2822 * FIFO tasks have no timeslices.
2824 if (p
->policy
!= SCHED_RR
)
2827 if (--p
->rt
.time_slice
)
2830 p
->rt
.time_slice
= sched_rr_timeslice
;
2833 * Requeue to the end of queue if we (and all of our ancestors) are not
2834 * the only element on the queue
2836 for_each_sched_rt_entity(rt_se
) {
2837 if (rt_se
->run_list
.prev
!= rt_se
->run_list
.next
) {
2838 requeue_task_rt(rq
, p
, 0);
2845 static void set_curr_task_rt(struct rq
*rq
)
2847 struct task_struct
*p
= rq
->curr
;
2848 struct sched_rt_entity
*rt_se
= &p
->rt
;
2850 p
->se
.exec_start
= rq_clock_task(rq
);
2852 for_each_sched_rt_entity(rt_se
) {
2853 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
2854 rt_rq
->curr
= rt_se
;
2857 /* The running task is never eligible for pushing */
2858 dequeue_pushable_task(rq
, p
);
2861 static unsigned int get_rr_interval_rt(struct rq
*rq
, struct task_struct
*task
)
2864 * Time slice is 0 for SCHED_FIFO tasks
2866 if (task
->policy
== SCHED_RR
)
2867 return sched_rr_timeslice
;
2872 const struct sched_class rt_sched_class
= {
2873 .next
= &fair_sched_class
,
2874 .enqueue_task
= enqueue_task_rt
,
2875 .dequeue_task
= dequeue_task_rt
,
2876 .yield_task
= yield_task_rt
,
2878 .check_preempt_curr
= check_preempt_curr_rt
,
2880 .pick_next_task
= pick_next_task_rt
,
2881 .put_prev_task
= put_prev_task_rt
,
2884 .select_task_rq
= select_task_rq_rt
,
2886 .migrate_task_rq
= migrate_task_rq_rt
,
2887 .task_dead
= task_dead_rt
,
2888 .set_cpus_allowed
= set_cpus_allowed_common
,
2889 .rq_online
= rq_online_rt
,
2890 .rq_offline
= rq_offline_rt
,
2891 .task_woken
= task_woken_rt
,
2892 .switched_from
= switched_from_rt
,
2895 .set_curr_task
= set_curr_task_rt
,
2896 .task_tick
= task_tick_rt
,
2898 .get_rr_interval
= get_rr_interval_rt
,
2900 .prio_changed
= prio_changed_rt
,
2901 .switched_to
= switched_to_rt
,
2903 .update_curr
= update_curr_rt
,
2904 #ifdef CONFIG_RT_GROUP_SCHED
2905 .task_change_group
= task_change_group_rt
,
2909 #ifdef CONFIG_RT_GROUP_SCHED
2911 * Ensure that the real time constraints are schedulable.
2913 static DEFINE_MUTEX(rt_constraints_mutex
);
2915 /* Must be called with tasklist_lock held */
2916 static inline int tg_has_rt_tasks(struct task_group
*tg
)
2918 struct task_struct
*g
, *p
;
2921 * Autogroups do not have RT tasks; see autogroup_create().
2923 if (task_group_is_autogroup(tg
))
2926 for_each_process_thread(g
, p
) {
2927 if (rt_task(p
) && task_group(p
) == tg
)
2934 struct rt_schedulable_data
{
2935 struct task_group
*tg
;
2940 static int tg_rt_schedulable(struct task_group
*tg
, void *data
)
2942 struct rt_schedulable_data
*d
= data
;
2943 struct task_group
*child
;
2944 unsigned long total
, sum
= 0;
2945 u64 period
, runtime
;
2947 period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
2948 runtime
= tg
->rt_bandwidth
.rt_runtime
;
2951 period
= d
->rt_period
;
2952 runtime
= d
->rt_runtime
;
2956 * Cannot have more runtime than the period.
2958 if (runtime
> period
&& runtime
!= RUNTIME_INF
)
2962 * Ensure we don't starve existing RT tasks.
2964 if (rt_bandwidth_enabled() && !runtime
&& tg_has_rt_tasks(tg
))
2967 total
= to_ratio(period
, runtime
);
2970 * Nobody can have more than the global setting allows.
2972 if (total
> to_ratio(global_rt_period(), global_rt_runtime()))
2976 * The sum of our children's runtime should not exceed our own.
2978 list_for_each_entry_rcu(child
, &tg
->children
, siblings
) {
2979 period
= ktime_to_ns(child
->rt_bandwidth
.rt_period
);
2980 runtime
= child
->rt_bandwidth
.rt_runtime
;
2982 if (child
== d
->tg
) {
2983 period
= d
->rt_period
;
2984 runtime
= d
->rt_runtime
;
2987 sum
+= to_ratio(period
, runtime
);
2996 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
3000 struct rt_schedulable_data data
= {
3002 .rt_period
= period
,
3003 .rt_runtime
= runtime
,
3007 ret
= walk_tg_tree(tg_rt_schedulable
, tg_nop
, &data
);
3013 static int tg_set_rt_bandwidth(struct task_group
*tg
,
3014 u64 rt_period
, u64 rt_runtime
)
3019 * Disallowing the root group RT runtime is BAD, it would disallow the
3020 * kernel creating (and or operating) RT threads.
3022 if (tg
== &root_task_group
&& rt_runtime
== 0)
3025 /* No period doesn't make any sense. */
3029 mutex_lock(&rt_constraints_mutex
);
3030 read_lock(&tasklist_lock
);
3031 err
= __rt_schedulable(tg
, rt_period
, rt_runtime
);
3035 raw_spin_lock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
3036 tg
->rt_bandwidth
.rt_period
= ns_to_ktime(rt_period
);
3037 tg
->rt_bandwidth
.rt_runtime
= rt_runtime
;
3039 for_each_possible_cpu(i
) {
3040 struct rt_rq
*rt_rq
= tg
->rt_rq
[i
];
3042 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
3043 rt_rq
->rt_runtime
= rt_runtime
;
3044 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
3046 raw_spin_unlock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
3048 read_unlock(&tasklist_lock
);
3049 mutex_unlock(&rt_constraints_mutex
);
3054 int sched_group_set_rt_runtime(struct task_group
*tg
, long rt_runtime_us
)
3056 u64 rt_runtime
, rt_period
;
3058 rt_period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
3059 rt_runtime
= (u64
)rt_runtime_us
* NSEC_PER_USEC
;
3060 if (rt_runtime_us
< 0)
3061 rt_runtime
= RUNTIME_INF
;
3062 else if ((u64
)rt_runtime_us
> U64_MAX
/ NSEC_PER_USEC
)
3065 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
3068 long sched_group_rt_runtime(struct task_group
*tg
)
3072 if (tg
->rt_bandwidth
.rt_runtime
== RUNTIME_INF
)
3075 rt_runtime_us
= tg
->rt_bandwidth
.rt_runtime
;
3076 do_div(rt_runtime_us
, NSEC_PER_USEC
);
3077 return rt_runtime_us
;
3080 int sched_group_set_rt_period(struct task_group
*tg
, u64 rt_period_us
)
3082 u64 rt_runtime
, rt_period
;
3084 if (rt_period_us
> U64_MAX
/ NSEC_PER_USEC
)
3087 rt_period
= rt_period_us
* NSEC_PER_USEC
;
3088 rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
3090 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
3093 long sched_group_rt_period(struct task_group
*tg
)
3097 rt_period_us
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
3098 do_div(rt_period_us
, NSEC_PER_USEC
);
3099 return rt_period_us
;
3102 static int sched_rt_global_constraints(void)
3106 mutex_lock(&rt_constraints_mutex
);
3107 read_lock(&tasklist_lock
);
3108 ret
= __rt_schedulable(NULL
, 0, 0);
3109 read_unlock(&tasklist_lock
);
3110 mutex_unlock(&rt_constraints_mutex
);
3115 int sched_rt_can_attach(struct task_group
*tg
, struct task_struct
*tsk
)
3117 /* Don't accept realtime tasks when there is no way for them to run */
3118 if (rt_task(tsk
) && tg
->rt_bandwidth
.rt_runtime
== 0)
3124 #else /* !CONFIG_RT_GROUP_SCHED */
3125 static int sched_rt_global_constraints(void)
3127 unsigned long flags
;
3130 raw_spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
3131 for_each_possible_cpu(i
) {
3132 struct rt_rq
*rt_rq
= &cpu_rq(i
)->rt
;
3134 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
3135 rt_rq
->rt_runtime
= global_rt_runtime();
3136 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
3138 raw_spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
3142 #endif /* CONFIG_RT_GROUP_SCHED */
3144 static int sched_rt_global_validate(void)
3146 if (sysctl_sched_rt_period
<= 0)
3149 if ((sysctl_sched_rt_runtime
!= RUNTIME_INF
) &&
3150 (sysctl_sched_rt_runtime
> sysctl_sched_rt_period
))
3156 static void sched_rt_do_global(void)
3158 unsigned long flags
;
3160 raw_spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
3161 def_rt_bandwidth
.rt_runtime
= global_rt_runtime();
3162 def_rt_bandwidth
.rt_period
= ns_to_ktime(global_rt_period());
3163 raw_spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
3166 int sched_rt_handler(struct ctl_table
*table
, int write
,
3167 void __user
*buffer
, size_t *lenp
,
3170 int old_period
, old_runtime
;
3171 static DEFINE_MUTEX(mutex
);
3175 old_period
= sysctl_sched_rt_period
;
3176 old_runtime
= sysctl_sched_rt_runtime
;
3178 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3180 if (!ret
&& write
) {
3181 ret
= sched_rt_global_validate();
3185 ret
= sched_dl_global_validate();
3189 ret
= sched_rt_global_constraints();
3193 sched_rt_do_global();
3194 sched_dl_do_global();
3198 sysctl_sched_rt_period
= old_period
;
3199 sysctl_sched_rt_runtime
= old_runtime
;
3201 mutex_unlock(&mutex
);
3206 int sched_rr_handler(struct ctl_table
*table
, int write
,
3207 void __user
*buffer
, size_t *lenp
,
3211 static DEFINE_MUTEX(mutex
);
3214 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3216 * Make sure that internally we keep jiffies.
3217 * Also, writing zero resets the timeslice to default:
3219 if (!ret
&& write
) {
3220 sched_rr_timeslice
=
3221 sysctl_sched_rr_timeslice
<= 0 ? RR_TIMESLICE
:
3222 msecs_to_jiffies(sysctl_sched_rr_timeslice
);
3224 mutex_unlock(&mutex
);
3228 #ifdef CONFIG_SCHED_DEBUG
3229 void print_rt_stats(struct seq_file
*m
, int cpu
)
3232 struct rt_rq
*rt_rq
;
3235 for_each_rt_rq(rt_rq
, iter
, cpu_rq(cpu
))
3236 print_rt_rq(m
, cpu
, rt_rq
);
3239 #endif /* CONFIG_SCHED_DEBUG */