1 // SPDX-License-Identifier: GPL-2.0
3 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
7 int sched_rr_timeslice = RR_TIMESLICE;
8 /* More than 4 hours if BW_SHIFT equals 20. */
9 static const u64 max_rt_runtime = MAX_BW;
12 * period over which we measure -rt task CPU usage in us.
15 int sysctl_sched_rt_period = 1000000;
18 * part of the period that we allow rt tasks to run in us.
21 int sysctl_sched_rt_runtime = 950000;
24 static int sysctl_sched_rr_timeslice = (MSEC_PER_SEC * RR_TIMESLICE) / HZ;
25 static int sched_rt_handler(const struct ctl_table *table, int write, void *buffer,
26 size_t *lenp, loff_t *ppos);
27 static int sched_rr_handler(const struct ctl_table *table, int write, void *buffer,
28 size_t *lenp, loff_t *ppos);
29 static const struct ctl_table sched_rt_sysctls[] = {
31 .procname = "sched_rt_period_us",
32 .data = &sysctl_sched_rt_period,
33 .maxlen = sizeof(int),
35 .proc_handler = sched_rt_handler,
37 .extra2 = SYSCTL_INT_MAX,
40 .procname = "sched_rt_runtime_us",
41 .data = &sysctl_sched_rt_runtime,
42 .maxlen = sizeof(int),
44 .proc_handler = sched_rt_handler,
45 .extra1 = SYSCTL_NEG_ONE,
46 .extra2 = (void *)&sysctl_sched_rt_period,
49 .procname = "sched_rr_timeslice_ms",
50 .data = &sysctl_sched_rr_timeslice,
51 .maxlen = sizeof(int),
53 .proc_handler = sched_rr_handler,
57 static int __init sched_rt_sysctl_init(void)
59 register_sysctl_init("kernel", sched_rt_sysctls);
62 late_initcall(sched_rt_sysctl_init);
65 void init_rt_rq(struct rt_rq *rt_rq)
67 struct rt_prio_array *array;
70 array = &rt_rq->active;
71 for (i = 0; i < MAX_RT_PRIO; i++) {
72 INIT_LIST_HEAD(array->queue + i);
73 __clear_bit(i, array->bitmap);
75 /* delimiter for bitsearch: */
76 __set_bit(MAX_RT_PRIO, array->bitmap);
78 #if defined CONFIG_SMP
79 rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
80 rt_rq->highest_prio.next = MAX_RT_PRIO-1;
81 rt_rq->overloaded = 0;
82 plist_head_init(&rt_rq->pushable_tasks);
83 #endif /* CONFIG_SMP */
84 /* We start is dequeued state, because no RT tasks are queued */
87 #ifdef CONFIG_RT_GROUP_SCHED
89 rt_rq->rt_throttled = 0;
90 rt_rq->rt_runtime = 0;
91 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
92 rt_rq->tg = &root_task_group;
96 #ifdef CONFIG_RT_GROUP_SCHED
98 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
100 static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
102 struct rt_bandwidth *rt_b =
103 container_of(timer, struct rt_bandwidth, rt_period_timer);
107 raw_spin_lock(&rt_b->rt_runtime_lock);
109 overrun = hrtimer_forward_now(timer, rt_b->rt_period);
113 raw_spin_unlock(&rt_b->rt_runtime_lock);
114 idle = do_sched_rt_period_timer(rt_b, overrun);
115 raw_spin_lock(&rt_b->rt_runtime_lock);
118 rt_b->rt_period_active = 0;
119 raw_spin_unlock(&rt_b->rt_runtime_lock);
121 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
124 void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
126 rt_b->rt_period = ns_to_ktime(period);
127 rt_b->rt_runtime = runtime;
129 raw_spin_lock_init(&rt_b->rt_runtime_lock);
131 hrtimer_setup(&rt_b->rt_period_timer, sched_rt_period_timer, CLOCK_MONOTONIC,
132 HRTIMER_MODE_REL_HARD);
135 static inline void do_start_rt_bandwidth(struct rt_bandwidth *rt_b)
137 raw_spin_lock(&rt_b->rt_runtime_lock);
138 if (!rt_b->rt_period_active) {
139 rt_b->rt_period_active = 1;
141 * SCHED_DEADLINE updates the bandwidth, as a run away
142 * RT task with a DL task could hog a CPU. But DL does
143 * not reset the period. If a deadline task was running
144 * without an RT task running, it can cause RT tasks to
145 * throttle when they start up. Kick the timer right away
146 * to update the period.
148 hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
149 hrtimer_start_expires(&rt_b->rt_period_timer,
150 HRTIMER_MODE_ABS_PINNED_HARD);
152 raw_spin_unlock(&rt_b->rt_runtime_lock);
155 static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
157 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
160 do_start_rt_bandwidth(rt_b);
163 static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
165 hrtimer_cancel(&rt_b->rt_period_timer);
168 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
170 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
172 WARN_ON_ONCE(!rt_entity_is_task(rt_se));
174 return container_of(rt_se, struct task_struct, rt);
177 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
179 /* Cannot fold with non-CONFIG_RT_GROUP_SCHED version, layout */
180 WARN_ON(!rt_group_sched_enabled() && rt_rq->tg != &root_task_group);
184 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
186 WARN_ON(!rt_group_sched_enabled() && rt_se->rt_rq->tg != &root_task_group);
190 static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
192 struct rt_rq *rt_rq = rt_se->rt_rq;
194 WARN_ON(!rt_group_sched_enabled() && rt_rq->tg != &root_task_group);
198 void unregister_rt_sched_group(struct task_group *tg)
200 if (!rt_group_sched_enabled())
204 destroy_rt_bandwidth(&tg->rt_bandwidth);
207 void free_rt_sched_group(struct task_group *tg)
211 if (!rt_group_sched_enabled())
214 for_each_possible_cpu(i) {
225 void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
226 struct sched_rt_entity *rt_se, int cpu,
227 struct sched_rt_entity *parent)
229 struct rq *rq = cpu_rq(cpu);
231 rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
232 rt_rq->rt_nr_boosted = 0;
236 tg->rt_rq[cpu] = rt_rq;
237 tg->rt_se[cpu] = rt_se;
243 rt_se->rt_rq = &rq->rt;
245 rt_se->rt_rq = parent->my_q;
248 rt_se->parent = parent;
249 INIT_LIST_HEAD(&rt_se->run_list);
252 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
255 struct sched_rt_entity *rt_se;
258 if (!rt_group_sched_enabled())
261 tg->rt_rq = kcalloc(nr_cpu_ids, sizeof(rt_rq), GFP_KERNEL);
264 tg->rt_se = kcalloc(nr_cpu_ids, sizeof(rt_se), GFP_KERNEL);
268 init_rt_bandwidth(&tg->rt_bandwidth, ktime_to_ns(global_rt_period()), 0);
270 for_each_possible_cpu(i) {
271 rt_rq = kzalloc_node(sizeof(struct rt_rq),
272 GFP_KERNEL, cpu_to_node(i));
276 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
277 GFP_KERNEL, cpu_to_node(i));
282 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
283 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
294 #else /* CONFIG_RT_GROUP_SCHED */
296 #define rt_entity_is_task(rt_se) (1)
298 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
300 return container_of(rt_se, struct task_struct, rt);
303 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
305 return container_of(rt_rq, struct rq, rt);
308 static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
310 struct task_struct *p = rt_task_of(rt_se);
315 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
317 struct rq *rq = rq_of_rt_se(rt_se);
322 void unregister_rt_sched_group(struct task_group *tg) { }
324 void free_rt_sched_group(struct task_group *tg) { }
326 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
330 #endif /* CONFIG_RT_GROUP_SCHED */
334 static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
336 /* Try to pull RT tasks here if we lower this rq's prio */
337 return rq->online && rq->rt.highest_prio.curr > prev->prio;
340 static inline int rt_overloaded(struct rq *rq)
342 return atomic_read(&rq->rd->rto_count);
345 static inline void rt_set_overload(struct rq *rq)
350 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
352 * Make sure the mask is visible before we set
353 * the overload count. That is checked to determine
354 * if we should look at the mask. It would be a shame
355 * if we looked at the mask, but the mask was not
358 * Matched by the barrier in pull_rt_task().
361 atomic_inc(&rq->rd->rto_count);
364 static inline void rt_clear_overload(struct rq *rq)
369 /* the order here really doesn't matter */
370 atomic_dec(&rq->rd->rto_count);
371 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
374 static inline int has_pushable_tasks(struct rq *rq)
376 return !plist_head_empty(&rq->rt.pushable_tasks);
379 static DEFINE_PER_CPU(struct balance_callback, rt_push_head);
380 static DEFINE_PER_CPU(struct balance_callback, rt_pull_head);
382 static void push_rt_tasks(struct rq *);
383 static void pull_rt_task(struct rq *);
385 static inline void rt_queue_push_tasks(struct rq *rq)
387 if (!has_pushable_tasks(rq))
390 queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
393 static inline void rt_queue_pull_task(struct rq *rq)
395 queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
398 static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
400 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
401 plist_node_init(&p->pushable_tasks, p->prio);
402 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
404 /* Update the highest prio pushable task */
405 if (p->prio < rq->rt.highest_prio.next)
406 rq->rt.highest_prio.next = p->prio;
408 if (!rq->rt.overloaded) {
410 rq->rt.overloaded = 1;
414 static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
416 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
418 /* Update the new highest prio pushable task */
419 if (has_pushable_tasks(rq)) {
420 p = plist_first_entry(&rq->rt.pushable_tasks,
421 struct task_struct, pushable_tasks);
422 rq->rt.highest_prio.next = p->prio;
424 rq->rt.highest_prio.next = MAX_RT_PRIO-1;
426 if (rq->rt.overloaded) {
427 rt_clear_overload(rq);
428 rq->rt.overloaded = 0;
435 static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
439 static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
443 static inline void rt_queue_push_tasks(struct rq *rq)
446 #endif /* CONFIG_SMP */
448 static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
449 static void dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count);
451 static inline int on_rt_rq(struct sched_rt_entity *rt_se)
456 #ifdef CONFIG_UCLAMP_TASK
458 * Verify the fitness of task @p to run on @cpu taking into account the uclamp
461 * This check is only important for heterogeneous systems where uclamp_min value
462 * is higher than the capacity of a @cpu. For non-heterogeneous system this
463 * function will always return true.
465 * The function will return true if the capacity of the @cpu is >= the
466 * uclamp_min and false otherwise.
468 * Note that uclamp_min will be clamped to uclamp_max if uclamp_min
471 static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
473 unsigned int min_cap;
474 unsigned int max_cap;
475 unsigned int cpu_cap;
477 /* Only heterogeneous systems can benefit from this check */
478 if (!sched_asym_cpucap_active())
481 min_cap = uclamp_eff_value(p, UCLAMP_MIN);
482 max_cap = uclamp_eff_value(p, UCLAMP_MAX);
484 cpu_cap = arch_scale_cpu_capacity(cpu);
486 return cpu_cap >= min(min_cap, max_cap);
489 static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
495 #ifdef CONFIG_RT_GROUP_SCHED
497 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
499 return rt_rq->rt_runtime;
502 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
504 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
507 typedef struct task_group *rt_rq_iter_t;
509 static inline struct task_group *next_task_group(struct task_group *tg)
511 if (!rt_group_sched_enabled()) {
512 WARN_ON(tg != &root_task_group);
517 tg = list_entry_rcu(tg->list.next,
518 typeof(struct task_group), list);
519 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
521 if (&tg->list == &task_groups)
527 #define for_each_rt_rq(rt_rq, iter, rq) \
528 for (iter = &root_task_group; \
529 iter && (rt_rq = iter->rt_rq[cpu_of(rq)]); \
530 iter = next_task_group(iter))
532 #define for_each_sched_rt_entity(rt_se) \
533 for (; rt_se; rt_se = rt_se->parent)
535 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
540 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
541 static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
543 static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
545 struct task_struct *donor = rq_of_rt_rq(rt_rq)->donor;
546 struct rq *rq = rq_of_rt_rq(rt_rq);
547 struct sched_rt_entity *rt_se;
549 int cpu = cpu_of(rq);
551 rt_se = rt_rq->tg->rt_se[cpu];
553 if (rt_rq->rt_nr_running) {
555 enqueue_top_rt_rq(rt_rq);
556 else if (!on_rt_rq(rt_se))
557 enqueue_rt_entity(rt_se, 0);
559 if (rt_rq->highest_prio.curr < donor->prio)
564 static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
566 struct sched_rt_entity *rt_se;
567 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
569 rt_se = rt_rq->tg->rt_se[cpu];
572 dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
573 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
574 cpufreq_update_util(rq_of_rt_rq(rt_rq), 0);
576 else if (on_rt_rq(rt_se))
577 dequeue_rt_entity(rt_se, 0);
580 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
582 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
585 static int rt_se_boosted(struct sched_rt_entity *rt_se)
587 struct rt_rq *rt_rq = group_rt_rq(rt_se);
588 struct task_struct *p;
591 return !!rt_rq->rt_nr_boosted;
593 p = rt_task_of(rt_se);
594 return p->prio != p->normal_prio;
598 static inline const struct cpumask *sched_rt_period_mask(void)
600 return this_rq()->rd->span;
603 static inline const struct cpumask *sched_rt_period_mask(void)
605 return cpu_online_mask;
610 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
612 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
615 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
617 return &rt_rq->tg->rt_bandwidth;
620 bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
622 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
624 return (hrtimer_active(&rt_b->rt_period_timer) ||
625 rt_rq->rt_time < rt_b->rt_runtime);
630 * We ran out of runtime, see if we can borrow some from our neighbours.
632 static void do_balance_runtime(struct rt_rq *rt_rq)
634 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
635 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
639 weight = cpumask_weight(rd->span);
641 raw_spin_lock(&rt_b->rt_runtime_lock);
642 rt_period = ktime_to_ns(rt_b->rt_period);
643 for_each_cpu(i, rd->span) {
644 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
650 raw_spin_lock(&iter->rt_runtime_lock);
652 * Either all rqs have inf runtime and there's nothing to steal
653 * or __disable_runtime() below sets a specific rq to inf to
654 * indicate its been disabled and disallow stealing.
656 if (iter->rt_runtime == RUNTIME_INF)
660 * From runqueues with spare time, take 1/n part of their
661 * spare time, but no more than our period.
663 diff = iter->rt_runtime - iter->rt_time;
665 diff = div_u64((u64)diff, weight);
666 if (rt_rq->rt_runtime + diff > rt_period)
667 diff = rt_period - rt_rq->rt_runtime;
668 iter->rt_runtime -= diff;
669 rt_rq->rt_runtime += diff;
670 if (rt_rq->rt_runtime == rt_period) {
671 raw_spin_unlock(&iter->rt_runtime_lock);
676 raw_spin_unlock(&iter->rt_runtime_lock);
678 raw_spin_unlock(&rt_b->rt_runtime_lock);
682 * Ensure this RQ takes back all the runtime it lend to its neighbours.
684 static void __disable_runtime(struct rq *rq)
686 struct root_domain *rd = rq->rd;
690 if (unlikely(!scheduler_running))
693 for_each_rt_rq(rt_rq, iter, rq) {
694 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
698 raw_spin_lock(&rt_b->rt_runtime_lock);
699 raw_spin_lock(&rt_rq->rt_runtime_lock);
701 * Either we're all inf and nobody needs to borrow, or we're
702 * already disabled and thus have nothing to do, or we have
703 * exactly the right amount of runtime to take out.
705 if (rt_rq->rt_runtime == RUNTIME_INF ||
706 rt_rq->rt_runtime == rt_b->rt_runtime)
708 raw_spin_unlock(&rt_rq->rt_runtime_lock);
711 * Calculate the difference between what we started out with
712 * and what we current have, that's the amount of runtime
713 * we lend and now have to reclaim.
715 want = rt_b->rt_runtime - rt_rq->rt_runtime;
718 * Greedy reclaim, take back as much as we can.
720 for_each_cpu(i, rd->span) {
721 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
725 * Can't reclaim from ourselves or disabled runqueues.
727 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
730 raw_spin_lock(&iter->rt_runtime_lock);
732 diff = min_t(s64, iter->rt_runtime, want);
733 iter->rt_runtime -= diff;
736 iter->rt_runtime -= want;
739 raw_spin_unlock(&iter->rt_runtime_lock);
745 raw_spin_lock(&rt_rq->rt_runtime_lock);
747 * We cannot be left wanting - that would mean some runtime
748 * leaked out of the system.
753 * Disable all the borrow logic by pretending we have inf
754 * runtime - in which case borrowing doesn't make sense.
756 rt_rq->rt_runtime = RUNTIME_INF;
757 rt_rq->rt_throttled = 0;
758 raw_spin_unlock(&rt_rq->rt_runtime_lock);
759 raw_spin_unlock(&rt_b->rt_runtime_lock);
761 /* Make rt_rq available for pick_next_task() */
762 sched_rt_rq_enqueue(rt_rq);
766 static void __enable_runtime(struct rq *rq)
771 if (unlikely(!scheduler_running))
775 * Reset each runqueue's bandwidth settings
777 for_each_rt_rq(rt_rq, iter, rq) {
778 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
780 raw_spin_lock(&rt_b->rt_runtime_lock);
781 raw_spin_lock(&rt_rq->rt_runtime_lock);
782 rt_rq->rt_runtime = rt_b->rt_runtime;
784 rt_rq->rt_throttled = 0;
785 raw_spin_unlock(&rt_rq->rt_runtime_lock);
786 raw_spin_unlock(&rt_b->rt_runtime_lock);
790 static void balance_runtime(struct rt_rq *rt_rq)
792 if (!sched_feat(RT_RUNTIME_SHARE))
795 if (rt_rq->rt_time > rt_rq->rt_runtime) {
796 raw_spin_unlock(&rt_rq->rt_runtime_lock);
797 do_balance_runtime(rt_rq);
798 raw_spin_lock(&rt_rq->rt_runtime_lock);
801 #else /* !CONFIG_SMP */
802 static inline void balance_runtime(struct rt_rq *rt_rq) {}
803 #endif /* CONFIG_SMP */
805 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
807 int i, idle = 1, throttled = 0;
808 const struct cpumask *span;
810 span = sched_rt_period_mask();
813 * FIXME: isolated CPUs should really leave the root task group,
814 * whether they are isolcpus or were isolated via cpusets, lest
815 * the timer run on a CPU which does not service all runqueues,
816 * potentially leaving other CPUs indefinitely throttled. If
817 * isolation is really required, the user will turn the throttle
818 * off to kill the perturbations it causes anyway. Meanwhile,
819 * this maintains functionality for boot and/or troubleshooting.
821 if (rt_b == &root_task_group.rt_bandwidth)
822 span = cpu_online_mask;
824 for_each_cpu(i, span) {
826 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
827 struct rq *rq = rq_of_rt_rq(rt_rq);
832 * When span == cpu_online_mask, taking each rq->lock
833 * can be time-consuming. Try to avoid it when possible.
835 raw_spin_lock(&rt_rq->rt_runtime_lock);
836 if (!sched_feat(RT_RUNTIME_SHARE) && rt_rq->rt_runtime != RUNTIME_INF)
837 rt_rq->rt_runtime = rt_b->rt_runtime;
838 skip = !rt_rq->rt_time && !rt_rq->rt_nr_running;
839 raw_spin_unlock(&rt_rq->rt_runtime_lock);
846 if (rt_rq->rt_time) {
849 raw_spin_lock(&rt_rq->rt_runtime_lock);
850 if (rt_rq->rt_throttled)
851 balance_runtime(rt_rq);
852 runtime = rt_rq->rt_runtime;
853 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
854 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
855 rt_rq->rt_throttled = 0;
859 * When we're idle and a woken (rt) task is
860 * throttled wakeup_preempt() will set
861 * skip_update and the time between the wakeup
862 * and this unthrottle will get accounted as
865 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
866 rq_clock_cancel_skipupdate(rq);
868 if (rt_rq->rt_time || rt_rq->rt_nr_running)
870 raw_spin_unlock(&rt_rq->rt_runtime_lock);
871 } else if (rt_rq->rt_nr_running) {
873 if (!rt_rq_throttled(rt_rq))
876 if (rt_rq->rt_throttled)
880 sched_rt_rq_enqueue(rt_rq);
884 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
890 static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
892 u64 runtime = sched_rt_runtime(rt_rq);
894 if (rt_rq->rt_throttled)
895 return rt_rq_throttled(rt_rq);
897 if (runtime >= sched_rt_period(rt_rq))
900 balance_runtime(rt_rq);
901 runtime = sched_rt_runtime(rt_rq);
902 if (runtime == RUNTIME_INF)
905 if (rt_rq->rt_time > runtime) {
906 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
909 * Don't actually throttle groups that have no runtime assigned
910 * but accrue some time due to boosting.
912 if (likely(rt_b->rt_runtime)) {
913 rt_rq->rt_throttled = 1;
914 printk_deferred_once("sched: RT throttling activated\n");
917 * In case we did anyway, make it go away,
918 * replenishment is a joke, since it will replenish us
924 if (rt_rq_throttled(rt_rq)) {
925 sched_rt_rq_dequeue(rt_rq);
933 #else /* !CONFIG_RT_GROUP_SCHED */
935 typedef struct rt_rq *rt_rq_iter_t;
937 #define for_each_rt_rq(rt_rq, iter, rq) \
938 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
940 #define for_each_sched_rt_entity(rt_se) \
941 for (; rt_se; rt_se = NULL)
943 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
948 static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
950 struct rq *rq = rq_of_rt_rq(rt_rq);
952 if (!rt_rq->rt_nr_running)
955 enqueue_top_rt_rq(rt_rq);
959 static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
961 dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
964 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
969 static inline const struct cpumask *sched_rt_period_mask(void)
971 return cpu_online_mask;
975 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
977 return &cpu_rq(cpu)->rt;
981 static void __enable_runtime(struct rq *rq) { }
982 static void __disable_runtime(struct rq *rq) { }
985 #endif /* CONFIG_RT_GROUP_SCHED */
987 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
989 #ifdef CONFIG_RT_GROUP_SCHED
990 struct rt_rq *rt_rq = group_rt_rq(rt_se);
993 return rt_rq->highest_prio.curr;
996 return rt_task_of(rt_se)->prio;
1000 * Update the current task's runtime statistics. Skip current tasks that
1001 * are not in our scheduling class.
1003 static void update_curr_rt(struct rq *rq)
1005 struct task_struct *donor = rq->donor;
1008 if (donor->sched_class != &rt_sched_class)
1011 delta_exec = update_curr_common(rq);
1012 if (unlikely(delta_exec <= 0))
1015 #ifdef CONFIG_RT_GROUP_SCHED
1016 struct sched_rt_entity *rt_se = &donor->rt;
1018 if (!rt_bandwidth_enabled())
1021 for_each_sched_rt_entity(rt_se) {
1022 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1025 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
1026 raw_spin_lock(&rt_rq->rt_runtime_lock);
1027 rt_rq->rt_time += delta_exec;
1028 exceeded = sched_rt_runtime_exceeded(rt_rq);
1031 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1033 do_start_rt_bandwidth(sched_rt_bandwidth(rt_rq));
1040 dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count)
1042 struct rq *rq = rq_of_rt_rq(rt_rq);
1044 BUG_ON(&rq->rt != rt_rq);
1046 if (!rt_rq->rt_queued)
1049 BUG_ON(!rq->nr_running);
1051 sub_nr_running(rq, count);
1052 rt_rq->rt_queued = 0;
1057 enqueue_top_rt_rq(struct rt_rq *rt_rq)
1059 struct rq *rq = rq_of_rt_rq(rt_rq);
1061 BUG_ON(&rq->rt != rt_rq);
1063 if (rt_rq->rt_queued)
1066 if (rt_rq_throttled(rt_rq))
1069 if (rt_rq->rt_nr_running) {
1070 add_nr_running(rq, rt_rq->rt_nr_running);
1071 rt_rq->rt_queued = 1;
1074 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
1075 cpufreq_update_util(rq, 0);
1078 #if defined CONFIG_SMP
1081 inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1083 struct rq *rq = rq_of_rt_rq(rt_rq);
1086 * Change rq's cpupri only if rt_rq is the top queue.
1088 if (IS_ENABLED(CONFIG_RT_GROUP_SCHED) && &rq->rt != rt_rq)
1091 if (rq->online && prio < prev_prio)
1092 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
1096 dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1098 struct rq *rq = rq_of_rt_rq(rt_rq);
1101 * Change rq's cpupri only if rt_rq is the top queue.
1103 if (IS_ENABLED(CONFIG_RT_GROUP_SCHED) && &rq->rt != rt_rq)
1106 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1107 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1110 #else /* CONFIG_SMP */
1113 void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1115 void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1117 #endif /* CONFIG_SMP */
1119 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1121 inc_rt_prio(struct rt_rq *rt_rq, int prio)
1123 int prev_prio = rt_rq->highest_prio.curr;
1125 if (prio < prev_prio)
1126 rt_rq->highest_prio.curr = prio;
1128 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1132 dec_rt_prio(struct rt_rq *rt_rq, int prio)
1134 int prev_prio = rt_rq->highest_prio.curr;
1136 if (rt_rq->rt_nr_running) {
1138 WARN_ON(prio < prev_prio);
1141 * This may have been our highest task, and therefore
1142 * we may have some re-computation to do
1144 if (prio == prev_prio) {
1145 struct rt_prio_array *array = &rt_rq->active;
1147 rt_rq->highest_prio.curr =
1148 sched_find_first_bit(array->bitmap);
1152 rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
1155 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1160 static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1161 static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1163 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1165 #ifdef CONFIG_RT_GROUP_SCHED
1168 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1170 if (rt_se_boosted(rt_se))
1171 rt_rq->rt_nr_boosted++;
1173 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
1177 dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1179 if (rt_se_boosted(rt_se))
1180 rt_rq->rt_nr_boosted--;
1182 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
1185 #else /* CONFIG_RT_GROUP_SCHED */
1188 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1193 void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1195 #endif /* CONFIG_RT_GROUP_SCHED */
1198 unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1200 struct rt_rq *group_rq = group_rt_rq(rt_se);
1203 return group_rq->rt_nr_running;
1209 unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
1211 struct rt_rq *group_rq = group_rt_rq(rt_se);
1212 struct task_struct *tsk;
1215 return group_rq->rr_nr_running;
1217 tsk = rt_task_of(rt_se);
1219 return (tsk->policy == SCHED_RR) ? 1 : 0;
1223 void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1225 int prio = rt_se_prio(rt_se);
1227 WARN_ON(!rt_prio(prio));
1228 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
1229 rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
1231 inc_rt_prio(rt_rq, prio);
1232 inc_rt_group(rt_se, rt_rq);
1236 void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1238 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
1239 WARN_ON(!rt_rq->rt_nr_running);
1240 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
1241 rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
1243 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1244 dec_rt_group(rt_se, rt_rq);
1248 * Change rt_se->run_list location unless SAVE && !MOVE
1250 * assumes ENQUEUE/DEQUEUE flags match
1252 static inline bool move_entity(unsigned int flags)
1254 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
1260 static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
1262 list_del_init(&rt_se->run_list);
1264 if (list_empty(array->queue + rt_se_prio(rt_se)))
1265 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1270 static inline struct sched_statistics *
1271 __schedstats_from_rt_se(struct sched_rt_entity *rt_se)
1273 /* schedstats is not supported for rt group. */
1274 if (!rt_entity_is_task(rt_se))
1277 return &rt_task_of(rt_se)->stats;
1281 update_stats_wait_start_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
1283 struct sched_statistics *stats;
1284 struct task_struct *p = NULL;
1286 if (!schedstat_enabled())
1289 if (rt_entity_is_task(rt_se))
1290 p = rt_task_of(rt_se);
1292 stats = __schedstats_from_rt_se(rt_se);
1296 __update_stats_wait_start(rq_of_rt_rq(rt_rq), p, stats);
1300 update_stats_enqueue_sleeper_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
1302 struct sched_statistics *stats;
1303 struct task_struct *p = NULL;
1305 if (!schedstat_enabled())
1308 if (rt_entity_is_task(rt_se))
1309 p = rt_task_of(rt_se);
1311 stats = __schedstats_from_rt_se(rt_se);
1315 __update_stats_enqueue_sleeper(rq_of_rt_rq(rt_rq), p, stats);
1319 update_stats_enqueue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
1322 if (!schedstat_enabled())
1325 if (flags & ENQUEUE_WAKEUP)
1326 update_stats_enqueue_sleeper_rt(rt_rq, rt_se);
1330 update_stats_wait_end_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
1332 struct sched_statistics *stats;
1333 struct task_struct *p = NULL;
1335 if (!schedstat_enabled())
1338 if (rt_entity_is_task(rt_se))
1339 p = rt_task_of(rt_se);
1341 stats = __schedstats_from_rt_se(rt_se);
1345 __update_stats_wait_end(rq_of_rt_rq(rt_rq), p, stats);
1349 update_stats_dequeue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
1352 struct task_struct *p = NULL;
1354 if (!schedstat_enabled())
1357 if (rt_entity_is_task(rt_se))
1358 p = rt_task_of(rt_se);
1360 if ((flags & DEQUEUE_SLEEP) && p) {
1363 state = READ_ONCE(p->__state);
1364 if (state & TASK_INTERRUPTIBLE)
1365 __schedstat_set(p->stats.sleep_start,
1366 rq_clock(rq_of_rt_rq(rt_rq)));
1368 if (state & TASK_UNINTERRUPTIBLE)
1369 __schedstat_set(p->stats.block_start,
1370 rq_clock(rq_of_rt_rq(rt_rq)));
1374 static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1376 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1377 struct rt_prio_array *array = &rt_rq->active;
1378 struct rt_rq *group_rq = group_rt_rq(rt_se);
1379 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1382 * Don't enqueue the group if its throttled, or when empty.
1383 * The latter is a consequence of the former when a child group
1384 * get throttled and the current group doesn't have any other
1387 if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
1389 __delist_rt_entity(rt_se, array);
1393 if (move_entity(flags)) {
1394 WARN_ON_ONCE(rt_se->on_list);
1395 if (flags & ENQUEUE_HEAD)
1396 list_add(&rt_se->run_list, queue);
1398 list_add_tail(&rt_se->run_list, queue);
1400 __set_bit(rt_se_prio(rt_se), array->bitmap);
1405 inc_rt_tasks(rt_se, rt_rq);
1408 static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1410 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1411 struct rt_prio_array *array = &rt_rq->active;
1413 if (move_entity(flags)) {
1414 WARN_ON_ONCE(!rt_se->on_list);
1415 __delist_rt_entity(rt_se, array);
1419 dec_rt_tasks(rt_se, rt_rq);
1423 * Because the prio of an upper entry depends on the lower
1424 * entries, we must remove entries top - down.
1426 static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
1428 struct sched_rt_entity *back = NULL;
1429 unsigned int rt_nr_running;
1431 for_each_sched_rt_entity(rt_se) {
1436 rt_nr_running = rt_rq_of_se(back)->rt_nr_running;
1438 for (rt_se = back; rt_se; rt_se = rt_se->back) {
1439 if (on_rt_rq(rt_se))
1440 __dequeue_rt_entity(rt_se, flags);
1443 dequeue_top_rt_rq(rt_rq_of_se(back), rt_nr_running);
1446 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1448 struct rq *rq = rq_of_rt_se(rt_se);
1450 update_stats_enqueue_rt(rt_rq_of_se(rt_se), rt_se, flags);
1452 dequeue_rt_stack(rt_se, flags);
1453 for_each_sched_rt_entity(rt_se)
1454 __enqueue_rt_entity(rt_se, flags);
1455 enqueue_top_rt_rq(&rq->rt);
1458 static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1460 struct rq *rq = rq_of_rt_se(rt_se);
1462 update_stats_dequeue_rt(rt_rq_of_se(rt_se), rt_se, flags);
1464 dequeue_rt_stack(rt_se, flags);
1466 for_each_sched_rt_entity(rt_se) {
1467 struct rt_rq *rt_rq = group_rt_rq(rt_se);
1469 if (rt_rq && rt_rq->rt_nr_running)
1470 __enqueue_rt_entity(rt_se, flags);
1472 enqueue_top_rt_rq(&rq->rt);
1476 * Adding/removing a task to/from a priority array:
1479 enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1481 struct sched_rt_entity *rt_se = &p->rt;
1483 if (flags & ENQUEUE_WAKEUP)
1486 check_schedstat_required();
1487 update_stats_wait_start_rt(rt_rq_of_se(rt_se), rt_se);
1489 enqueue_rt_entity(rt_se, flags);
1491 if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1492 enqueue_pushable_task(rq, p);
1495 static bool dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1497 struct sched_rt_entity *rt_se = &p->rt;
1500 dequeue_rt_entity(rt_se, flags);
1502 dequeue_pushable_task(rq, p);
1508 * Put task to the head or the end of the run list without the overhead of
1509 * dequeue followed by enqueue.
1512 requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
1514 if (on_rt_rq(rt_se)) {
1515 struct rt_prio_array *array = &rt_rq->active;
1516 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1519 list_move(&rt_se->run_list, queue);
1521 list_move_tail(&rt_se->run_list, queue);
1525 static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
1527 struct sched_rt_entity *rt_se = &p->rt;
1528 struct rt_rq *rt_rq;
1530 for_each_sched_rt_entity(rt_se) {
1531 rt_rq = rt_rq_of_se(rt_se);
1532 requeue_rt_entity(rt_rq, rt_se, head);
1536 static void yield_task_rt(struct rq *rq)
1538 requeue_task_rt(rq, rq->curr, 0);
1542 static int find_lowest_rq(struct task_struct *task);
1545 select_task_rq_rt(struct task_struct *p, int cpu, int flags)
1547 struct task_struct *curr, *donor;
1551 /* For anything but wake ups, just return the task_cpu */
1552 if (!(flags & (WF_TTWU | WF_FORK)))
1558 curr = READ_ONCE(rq->curr); /* unlocked access */
1559 donor = READ_ONCE(rq->donor);
1562 * If the current task on @p's runqueue is an RT task, then
1563 * try to see if we can wake this RT task up on another
1564 * runqueue. Otherwise simply start this RT task
1565 * on its current runqueue.
1567 * We want to avoid overloading runqueues. If the woken
1568 * task is a higher priority, then it will stay on this CPU
1569 * and the lower prio task should be moved to another CPU.
1570 * Even though this will probably make the lower prio task
1571 * lose its cache, we do not want to bounce a higher task
1572 * around just because it gave up its CPU, perhaps for a
1575 * For equal prio tasks, we just let the scheduler sort it out.
1577 * Otherwise, just let it ride on the affine RQ and the
1578 * post-schedule router will push the preempted task away
1580 * This test is optimistic, if we get it wrong the load-balancer
1581 * will have to sort it out.
1583 * We take into account the capacity of the CPU to ensure it fits the
1584 * requirement of the task - which is only important on heterogeneous
1585 * systems like big.LITTLE.
1588 unlikely(rt_task(donor)) &&
1589 (curr->nr_cpus_allowed < 2 || donor->prio <= p->prio);
1591 if (test || !rt_task_fits_capacity(p, cpu)) {
1592 int target = find_lowest_rq(p);
1595 * Bail out if we were forcing a migration to find a better
1596 * fitting CPU but our search failed.
1598 if (!test && target != -1 && !rt_task_fits_capacity(p, target))
1602 * Don't bother moving it if the destination CPU is
1603 * not running a lower priority task.
1606 p->prio < cpu_rq(target)->rt.highest_prio.curr)
1617 static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1619 if (rq->curr->nr_cpus_allowed == 1 ||
1620 !cpupri_find(&rq->rd->cpupri, rq->donor, NULL))
1624 * p is migratable, so let's not schedule it and
1625 * see if it is pushed or pulled somewhere else.
1627 if (p->nr_cpus_allowed != 1 &&
1628 cpupri_find(&rq->rd->cpupri, p, NULL))
1632 * There appear to be other CPUs that can accept
1633 * the current task but none can run 'p', so lets reschedule
1634 * to try and push the current task away:
1636 requeue_task_rt(rq, p, 1);
1640 static int balance_rt(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1642 if (!on_rt_rq(&p->rt) && need_pull_rt_task(rq, p)) {
1644 * This is OK, because current is on_cpu, which avoids it being
1645 * picked for load-balance and preemption/IRQs are still
1646 * disabled avoiding further scheduler activity on it and we've
1647 * not yet started the picking loop.
1649 rq_unpin_lock(rq, rf);
1651 rq_repin_lock(rq, rf);
1654 return sched_stop_runnable(rq) || sched_dl_runnable(rq) || sched_rt_runnable(rq);
1656 #endif /* CONFIG_SMP */
1659 * Preempt the current task with a newly woken task if needed:
1661 static void wakeup_preempt_rt(struct rq *rq, struct task_struct *p, int flags)
1663 struct task_struct *donor = rq->donor;
1665 if (p->prio < donor->prio) {
1674 * - the newly woken task is of equal priority to the current task
1675 * - the newly woken task is non-migratable while current is migratable
1676 * - current will be preempted on the next reschedule
1678 * we should check to see if current can readily move to a different
1679 * cpu. If so, we will reschedule to allow the push logic to try
1680 * to move current somewhere else, making room for our non-migratable
1683 if (p->prio == donor->prio && !test_tsk_need_resched(rq->curr))
1684 check_preempt_equal_prio(rq, p);
1688 static inline void set_next_task_rt(struct rq *rq, struct task_struct *p, bool first)
1690 struct sched_rt_entity *rt_se = &p->rt;
1691 struct rt_rq *rt_rq = &rq->rt;
1693 p->se.exec_start = rq_clock_task(rq);
1694 if (on_rt_rq(&p->rt))
1695 update_stats_wait_end_rt(rt_rq, rt_se);
1697 /* The running task is never eligible for pushing */
1698 dequeue_pushable_task(rq, p);
1704 * If prev task was rt, put_prev_task() has already updated the
1705 * utilization. We only care of the case where we start to schedule a
1708 if (rq->donor->sched_class != &rt_sched_class)
1709 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
1711 rt_queue_push_tasks(rq);
1714 static struct sched_rt_entity *pick_next_rt_entity(struct rt_rq *rt_rq)
1716 struct rt_prio_array *array = &rt_rq->active;
1717 struct sched_rt_entity *next = NULL;
1718 struct list_head *queue;
1721 idx = sched_find_first_bit(array->bitmap);
1722 BUG_ON(idx >= MAX_RT_PRIO);
1724 queue = array->queue + idx;
1725 if (WARN_ON_ONCE(list_empty(queue)))
1727 next = list_entry(queue->next, struct sched_rt_entity, run_list);
1732 static struct task_struct *_pick_next_task_rt(struct rq *rq)
1734 struct sched_rt_entity *rt_se;
1735 struct rt_rq *rt_rq = &rq->rt;
1738 rt_se = pick_next_rt_entity(rt_rq);
1739 if (unlikely(!rt_se))
1741 rt_rq = group_rt_rq(rt_se);
1744 return rt_task_of(rt_se);
1747 static struct task_struct *pick_task_rt(struct rq *rq)
1749 struct task_struct *p;
1751 if (!sched_rt_runnable(rq))
1754 p = _pick_next_task_rt(rq);
1759 static void put_prev_task_rt(struct rq *rq, struct task_struct *p, struct task_struct *next)
1761 struct sched_rt_entity *rt_se = &p->rt;
1762 struct rt_rq *rt_rq = &rq->rt;
1764 if (on_rt_rq(&p->rt))
1765 update_stats_wait_start_rt(rt_rq, rt_se);
1769 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
1772 * The previous task needs to be made eligible for pushing
1773 * if it is still active
1775 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
1776 enqueue_pushable_task(rq, p);
1781 /* Only try algorithms three times */
1782 #define RT_MAX_TRIES 3
1785 * Return the highest pushable rq's task, which is suitable to be executed
1786 * on the CPU, NULL otherwise
1788 static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
1790 struct plist_head *head = &rq->rt.pushable_tasks;
1791 struct task_struct *p;
1793 if (!has_pushable_tasks(rq))
1796 plist_for_each_entry(p, head, pushable_tasks) {
1797 if (task_is_pushable(rq, p, cpu))
1804 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
1806 static int find_lowest_rq(struct task_struct *task)
1808 struct sched_domain *sd;
1809 struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
1810 int this_cpu = smp_processor_id();
1811 int cpu = task_cpu(task);
1814 /* Make sure the mask is initialized first */
1815 if (unlikely(!lowest_mask))
1818 if (task->nr_cpus_allowed == 1)
1819 return -1; /* No other targets possible */
1822 * If we're on asym system ensure we consider the different capacities
1823 * of the CPUs when searching for the lowest_mask.
1825 if (sched_asym_cpucap_active()) {
1827 ret = cpupri_find_fitness(&task_rq(task)->rd->cpupri,
1829 rt_task_fits_capacity);
1832 ret = cpupri_find(&task_rq(task)->rd->cpupri,
1837 return -1; /* No targets found */
1840 * At this point we have built a mask of CPUs representing the
1841 * lowest priority tasks in the system. Now we want to elect
1842 * the best one based on our affinity and topology.
1844 * We prioritize the last CPU that the task executed on since
1845 * it is most likely cache-hot in that location.
1847 if (cpumask_test_cpu(cpu, lowest_mask))
1851 * Otherwise, we consult the sched_domains span maps to figure
1852 * out which CPU is logically closest to our hot cache data.
1854 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1855 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
1858 for_each_domain(cpu, sd) {
1859 if (sd->flags & SD_WAKE_AFFINE) {
1863 * "this_cpu" is cheaper to preempt than a
1866 if (this_cpu != -1 &&
1867 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1872 best_cpu = cpumask_any_and_distribute(lowest_mask,
1873 sched_domain_span(sd));
1874 if (best_cpu < nr_cpu_ids) {
1883 * And finally, if there were no matches within the domains
1884 * just give the caller *something* to work with from the compatible
1890 cpu = cpumask_any_distribute(lowest_mask);
1891 if (cpu < nr_cpu_ids)
1897 static struct task_struct *pick_next_pushable_task(struct rq *rq)
1899 struct task_struct *p;
1901 if (!has_pushable_tasks(rq))
1904 p = plist_first_entry(&rq->rt.pushable_tasks,
1905 struct task_struct, pushable_tasks);
1907 BUG_ON(rq->cpu != task_cpu(p));
1908 BUG_ON(task_current(rq, p));
1909 BUG_ON(task_current_donor(rq, p));
1910 BUG_ON(p->nr_cpus_allowed <= 1);
1912 BUG_ON(!task_on_rq_queued(p));
1913 BUG_ON(!rt_task(p));
1918 /* Will lock the rq it finds */
1919 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
1921 struct rq *lowest_rq = NULL;
1925 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
1926 cpu = find_lowest_rq(task);
1928 if ((cpu == -1) || (cpu == rq->cpu))
1931 lowest_rq = cpu_rq(cpu);
1933 if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1935 * Target rq has tasks of equal or higher priority,
1936 * retrying does not release any lock and is unlikely
1937 * to yield a different result.
1943 /* if the prio of this runqueue changed, try again */
1944 if (double_lock_balance(rq, lowest_rq)) {
1946 * We had to unlock the run queue. In
1947 * the mean time, task could have
1948 * migrated already or had its affinity changed,
1949 * therefore check if the task is still at the
1950 * head of the pushable tasks list.
1951 * It is possible the task was scheduled, set
1952 * "migrate_disabled" and then got preempted, so we must
1953 * check the task migration disable flag here too.
1955 if (unlikely(is_migration_disabled(task) ||
1956 !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_mask) ||
1957 task != pick_next_pushable_task(rq))) {
1959 double_unlock_balance(rq, lowest_rq);
1965 /* If this rq is still suitable use it. */
1966 if (lowest_rq->rt.highest_prio.curr > task->prio)
1970 double_unlock_balance(rq, lowest_rq);
1978 * If the current CPU has more than one RT task, see if the non
1979 * running task can migrate over to a CPU that is running a task
1980 * of lesser priority.
1982 static int push_rt_task(struct rq *rq, bool pull)
1984 struct task_struct *next_task;
1985 struct rq *lowest_rq;
1988 if (!rq->rt.overloaded)
1991 next_task = pick_next_pushable_task(rq);
1997 * It's possible that the next_task slipped in of
1998 * higher priority than current. If that's the case
1999 * just reschedule current.
2001 if (unlikely(next_task->prio < rq->donor->prio)) {
2006 if (is_migration_disabled(next_task)) {
2007 struct task_struct *push_task = NULL;
2010 if (!pull || rq->push_busy)
2014 * Invoking find_lowest_rq() on anything but an RT task doesn't
2015 * make sense. Per the above priority check, curr has to
2016 * be of higher priority than next_task, so no need to
2017 * reschedule when bailing out.
2019 * Note that the stoppers are masqueraded as SCHED_FIFO
2020 * (cf. sched_set_stop_task()), so we can't rely on rt_task().
2022 if (rq->donor->sched_class != &rt_sched_class)
2025 cpu = find_lowest_rq(rq->curr);
2026 if (cpu == -1 || cpu == rq->cpu)
2030 * Given we found a CPU with lower priority than @next_task,
2031 * therefore it should be running. However we cannot migrate it
2032 * to this other CPU, instead attempt to push the current
2033 * running task on this CPU away.
2035 push_task = get_push_task(rq);
2038 raw_spin_rq_unlock(rq);
2039 stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
2040 push_task, &rq->push_work);
2042 raw_spin_rq_lock(rq);
2048 if (WARN_ON(next_task == rq->curr))
2051 /* We might release rq lock */
2052 get_task_struct(next_task);
2054 /* find_lock_lowest_rq locks the rq if found */
2055 lowest_rq = find_lock_lowest_rq(next_task, rq);
2057 struct task_struct *task;
2059 * find_lock_lowest_rq releases rq->lock
2060 * so it is possible that next_task has migrated.
2062 * We need to make sure that the task is still on the same
2063 * run-queue and is also still the next task eligible for
2066 task = pick_next_pushable_task(rq);
2067 if (task == next_task) {
2069 * The task hasn't migrated, and is still the next
2070 * eligible task, but we failed to find a run-queue
2071 * to push it to. Do not retry in this case, since
2072 * other CPUs will pull from us when ready.
2078 /* No more tasks, just exit */
2082 * Something has shifted, try again.
2084 put_task_struct(next_task);
2089 move_queued_task_locked(rq, lowest_rq, next_task);
2090 resched_curr(lowest_rq);
2093 double_unlock_balance(rq, lowest_rq);
2095 put_task_struct(next_task);
2100 static void push_rt_tasks(struct rq *rq)
2102 /* push_rt_task will return true if it moved an RT */
2103 while (push_rt_task(rq, false))
2107 #ifdef HAVE_RT_PUSH_IPI
2110 * When a high priority task schedules out from a CPU and a lower priority
2111 * task is scheduled in, a check is made to see if there's any RT tasks
2112 * on other CPUs that are waiting to run because a higher priority RT task
2113 * is currently running on its CPU. In this case, the CPU with multiple RT
2114 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
2115 * up that may be able to run one of its non-running queued RT tasks.
2117 * All CPUs with overloaded RT tasks need to be notified as there is currently
2118 * no way to know which of these CPUs have the highest priority task waiting
2119 * to run. Instead of trying to take a spinlock on each of these CPUs,
2120 * which has shown to cause large latency when done on machines with many
2121 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
2122 * RT tasks waiting to run.
2124 * Just sending an IPI to each of the CPUs is also an issue, as on large
2125 * count CPU machines, this can cause an IPI storm on a CPU, especially
2126 * if its the only CPU with multiple RT tasks queued, and a large number
2127 * of CPUs scheduling a lower priority task at the same time.
2129 * Each root domain has its own IRQ work function that can iterate over
2130 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
2131 * task must be checked if there's one or many CPUs that are lowering
2132 * their priority, there's a single IRQ work iterator that will try to
2133 * push off RT tasks that are waiting to run.
2135 * When a CPU schedules a lower priority task, it will kick off the
2136 * IRQ work iterator that will jump to each CPU with overloaded RT tasks.
2137 * As it only takes the first CPU that schedules a lower priority task
2138 * to start the process, the rto_start variable is incremented and if
2139 * the atomic result is one, then that CPU will try to take the rto_lock.
2140 * This prevents high contention on the lock as the process handles all
2141 * CPUs scheduling lower priority tasks.
2143 * All CPUs that are scheduling a lower priority task will increment the
2144 * rt_loop_next variable. This will make sure that the IRQ work iterator
2145 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
2146 * priority task, even if the iterator is in the middle of a scan. Incrementing
2147 * the rt_loop_next will cause the iterator to perform another scan.
2150 static int rto_next_cpu(struct root_domain *rd)
2156 * When starting the IPI RT pushing, the rto_cpu is set to -1,
2157 * rt_next_cpu() will simply return the first CPU found in
2160 * If rto_next_cpu() is called with rto_cpu is a valid CPU, it
2161 * will return the next CPU found in the rto_mask.
2163 * If there are no more CPUs left in the rto_mask, then a check is made
2164 * against rto_loop and rto_loop_next. rto_loop is only updated with
2165 * the rto_lock held, but any CPU may increment the rto_loop_next
2166 * without any locking.
2170 /* When rto_cpu is -1 this acts like cpumask_first() */
2171 cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
2175 if (cpu < nr_cpu_ids)
2181 * ACQUIRE ensures we see the @rto_mask changes
2182 * made prior to the @next value observed.
2184 * Matches WMB in rt_set_overload().
2186 next = atomic_read_acquire(&rd->rto_loop_next);
2188 if (rd->rto_loop == next)
2191 rd->rto_loop = next;
2197 static inline bool rto_start_trylock(atomic_t *v)
2199 return !atomic_cmpxchg_acquire(v, 0, 1);
2202 static inline void rto_start_unlock(atomic_t *v)
2204 atomic_set_release(v, 0);
2207 static void tell_cpu_to_push(struct rq *rq)
2211 /* Keep the loop going if the IPI is currently active */
2212 atomic_inc(&rq->rd->rto_loop_next);
2214 /* Only one CPU can initiate a loop at a time */
2215 if (!rto_start_trylock(&rq->rd->rto_loop_start))
2218 raw_spin_lock(&rq->rd->rto_lock);
2221 * The rto_cpu is updated under the lock, if it has a valid CPU
2222 * then the IPI is still running and will continue due to the
2223 * update to loop_next, and nothing needs to be done here.
2224 * Otherwise it is finishing up and an IPI needs to be sent.
2226 if (rq->rd->rto_cpu < 0)
2227 cpu = rto_next_cpu(rq->rd);
2229 raw_spin_unlock(&rq->rd->rto_lock);
2231 rto_start_unlock(&rq->rd->rto_loop_start);
2234 /* Make sure the rd does not get freed while pushing */
2235 sched_get_rd(rq->rd);
2236 irq_work_queue_on(&rq->rd->rto_push_work, cpu);
2240 /* Called from hardirq context */
2241 void rto_push_irq_work_func(struct irq_work *work)
2243 struct root_domain *rd =
2244 container_of(work, struct root_domain, rto_push_work);
2251 * We do not need to grab the lock to check for has_pushable_tasks.
2252 * When it gets updated, a check is made if a push is possible.
2254 if (has_pushable_tasks(rq)) {
2255 raw_spin_rq_lock(rq);
2256 while (push_rt_task(rq, true))
2258 raw_spin_rq_unlock(rq);
2261 raw_spin_lock(&rd->rto_lock);
2263 /* Pass the IPI to the next rt overloaded queue */
2264 cpu = rto_next_cpu(rd);
2266 raw_spin_unlock(&rd->rto_lock);
2273 /* Try the next RT overloaded CPU */
2274 irq_work_queue_on(&rd->rto_push_work, cpu);
2276 #endif /* HAVE_RT_PUSH_IPI */
2278 static void pull_rt_task(struct rq *this_rq)
2280 int this_cpu = this_rq->cpu, cpu;
2281 bool resched = false;
2282 struct task_struct *p, *push_task;
2284 int rt_overload_count = rt_overloaded(this_rq);
2286 if (likely(!rt_overload_count))
2290 * Match the barrier from rt_set_overloaded; this guarantees that if we
2291 * see overloaded we must also see the rto_mask bit.
2295 /* If we are the only overloaded CPU do nothing */
2296 if (rt_overload_count == 1 &&
2297 cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
2300 #ifdef HAVE_RT_PUSH_IPI
2301 if (sched_feat(RT_PUSH_IPI)) {
2302 tell_cpu_to_push(this_rq);
2307 for_each_cpu(cpu, this_rq->rd->rto_mask) {
2308 if (this_cpu == cpu)
2311 src_rq = cpu_rq(cpu);
2314 * Don't bother taking the src_rq->lock if the next highest
2315 * task is known to be lower-priority than our current task.
2316 * This may look racy, but if this value is about to go
2317 * logically higher, the src_rq will push this task away.
2318 * And if its going logically lower, we do not care
2320 if (src_rq->rt.highest_prio.next >=
2321 this_rq->rt.highest_prio.curr)
2325 * We can potentially drop this_rq's lock in
2326 * double_lock_balance, and another CPU could
2330 double_lock_balance(this_rq, src_rq);
2333 * We can pull only a task, which is pushable
2334 * on its rq, and no others.
2336 p = pick_highest_pushable_task(src_rq, this_cpu);
2339 * Do we have an RT task that preempts
2340 * the to-be-scheduled task?
2342 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2343 WARN_ON(p == src_rq->curr);
2344 WARN_ON(!task_on_rq_queued(p));
2347 * There's a chance that p is higher in priority
2348 * than what's currently running on its CPU.
2349 * This is just that p is waking up and hasn't
2350 * had a chance to schedule. We only pull
2351 * p if it is lower in priority than the
2352 * current task on the run queue
2354 if (p->prio < src_rq->donor->prio)
2357 if (is_migration_disabled(p)) {
2358 push_task = get_push_task(src_rq);
2360 move_queued_task_locked(src_rq, this_rq, p);
2364 * We continue with the search, just in
2365 * case there's an even higher prio task
2366 * in another runqueue. (low likelihood
2371 double_unlock_balance(this_rq, src_rq);
2375 raw_spin_rq_unlock(this_rq);
2376 stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2377 push_task, &src_rq->push_work);
2379 raw_spin_rq_lock(this_rq);
2384 resched_curr(this_rq);
2388 * If we are not running and we are not going to reschedule soon, we should
2389 * try to push tasks away now
2391 static void task_woken_rt(struct rq *rq, struct task_struct *p)
2393 bool need_to_push = !task_on_cpu(rq, p) &&
2394 !test_tsk_need_resched(rq->curr) &&
2395 p->nr_cpus_allowed > 1 &&
2396 (dl_task(rq->donor) || rt_task(rq->donor)) &&
2397 (rq->curr->nr_cpus_allowed < 2 ||
2398 rq->donor->prio <= p->prio);
2404 /* Assumes rq->lock is held */
2405 static void rq_online_rt(struct rq *rq)
2407 if (rq->rt.overloaded)
2408 rt_set_overload(rq);
2410 __enable_runtime(rq);
2412 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
2415 /* Assumes rq->lock is held */
2416 static void rq_offline_rt(struct rq *rq)
2418 if (rq->rt.overloaded)
2419 rt_clear_overload(rq);
2421 __disable_runtime(rq);
2423 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
2427 * When switch from the rt queue, we bring ourselves to a position
2428 * that we might want to pull RT tasks from other runqueues.
2430 static void switched_from_rt(struct rq *rq, struct task_struct *p)
2433 * If there are other RT tasks then we will reschedule
2434 * and the scheduling of the other RT tasks will handle
2435 * the balancing. But if we are the last RT task
2436 * we may need to handle the pulling of RT tasks
2439 if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
2442 rt_queue_pull_task(rq);
2445 void __init init_sched_rt_class(void)
2449 for_each_possible_cpu(i) {
2450 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
2451 GFP_KERNEL, cpu_to_node(i));
2454 #endif /* CONFIG_SMP */
2457 * When switching a task to RT, we may overload the runqueue
2458 * with RT tasks. In this case we try to push them off to
2461 static void switched_to_rt(struct rq *rq, struct task_struct *p)
2464 * If we are running, update the avg_rt tracking, as the running time
2465 * will now on be accounted into the latter.
2467 if (task_current(rq, p)) {
2468 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2473 * If we are not running we may need to preempt the current
2474 * running task. If that current running task is also an RT task
2475 * then see if we can move to another run queue.
2477 if (task_on_rq_queued(p)) {
2479 if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
2480 rt_queue_push_tasks(rq);
2481 #endif /* CONFIG_SMP */
2482 if (p->prio < rq->donor->prio && cpu_online(cpu_of(rq)))
2488 * Priority of the task has changed. This may cause
2489 * us to initiate a push or pull.
2492 prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
2494 if (!task_on_rq_queued(p))
2497 if (task_current_donor(rq, p)) {
2500 * If our priority decreases while running, we
2501 * may need to pull tasks to this runqueue.
2503 if (oldprio < p->prio)
2504 rt_queue_pull_task(rq);
2507 * If there's a higher priority task waiting to run
2510 if (p->prio > rq->rt.highest_prio.curr)
2513 /* For UP simply resched on drop of prio */
2514 if (oldprio < p->prio)
2516 #endif /* CONFIG_SMP */
2519 * This task is not running, but if it is
2520 * greater than the current running task
2523 if (p->prio < rq->donor->prio)
2528 #ifdef CONFIG_POSIX_TIMERS
2529 static void watchdog(struct rq *rq, struct task_struct *p)
2531 unsigned long soft, hard;
2533 /* max may change after cur was read, this will be fixed next tick */
2534 soft = task_rlimit(p, RLIMIT_RTTIME);
2535 hard = task_rlimit_max(p, RLIMIT_RTTIME);
2537 if (soft != RLIM_INFINITY) {
2540 if (p->rt.watchdog_stamp != jiffies) {
2542 p->rt.watchdog_stamp = jiffies;
2545 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
2546 if (p->rt.timeout > next) {
2547 posix_cputimers_rt_watchdog(&p->posix_cputimers,
2548 p->se.sum_exec_runtime);
2553 static inline void watchdog(struct rq *rq, struct task_struct *p) { }
2557 * scheduler tick hitting a task of our scheduling class.
2559 * NOTE: This function can be called remotely by the tick offload that
2560 * goes along full dynticks. Therefore no local assumption can be made
2561 * and everything must be accessed through the @rq and @curr passed in
2564 static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
2566 struct sched_rt_entity *rt_se = &p->rt;
2569 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2574 * RR tasks need a special form of time-slice management.
2575 * FIFO tasks have no timeslices.
2577 if (p->policy != SCHED_RR)
2580 if (--p->rt.time_slice)
2583 p->rt.time_slice = sched_rr_timeslice;
2586 * Requeue to the end of queue if we (and all of our ancestors) are not
2587 * the only element on the queue
2589 for_each_sched_rt_entity(rt_se) {
2590 if (rt_se->run_list.prev != rt_se->run_list.next) {
2591 requeue_task_rt(rq, p, 0);
2598 static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
2601 * Time slice is 0 for SCHED_FIFO tasks
2603 if (task->policy == SCHED_RR)
2604 return sched_rr_timeslice;
2609 #ifdef CONFIG_SCHED_CORE
2610 static int task_is_throttled_rt(struct task_struct *p, int cpu)
2612 struct rt_rq *rt_rq;
2614 #ifdef CONFIG_RT_GROUP_SCHED // XXX maybe add task_rt_rq(), see also sched_rt_period_rt_rq
2615 rt_rq = task_group(p)->rt_rq[cpu];
2616 WARN_ON(!rt_group_sched_enabled() && rt_rq->tg != &root_task_group);
2618 rt_rq = &cpu_rq(cpu)->rt;
2621 return rt_rq_throttled(rt_rq);
2625 DEFINE_SCHED_CLASS(rt) = {
2627 .enqueue_task = enqueue_task_rt,
2628 .dequeue_task = dequeue_task_rt,
2629 .yield_task = yield_task_rt,
2631 .wakeup_preempt = wakeup_preempt_rt,
2633 .pick_task = pick_task_rt,
2634 .put_prev_task = put_prev_task_rt,
2635 .set_next_task = set_next_task_rt,
2638 .balance = balance_rt,
2639 .select_task_rq = select_task_rq_rt,
2640 .set_cpus_allowed = set_cpus_allowed_common,
2641 .rq_online = rq_online_rt,
2642 .rq_offline = rq_offline_rt,
2643 .task_woken = task_woken_rt,
2644 .switched_from = switched_from_rt,
2645 .find_lock_rq = find_lock_lowest_rq,
2648 .task_tick = task_tick_rt,
2650 .get_rr_interval = get_rr_interval_rt,
2652 .prio_changed = prio_changed_rt,
2653 .switched_to = switched_to_rt,
2655 .update_curr = update_curr_rt,
2657 #ifdef CONFIG_SCHED_CORE
2658 .task_is_throttled = task_is_throttled_rt,
2661 #ifdef CONFIG_UCLAMP_TASK
2662 .uclamp_enabled = 1,
2666 #ifdef CONFIG_RT_GROUP_SCHED
2668 * Ensure that the real time constraints are schedulable.
2670 static DEFINE_MUTEX(rt_constraints_mutex);
2672 static inline int tg_has_rt_tasks(struct task_group *tg)
2674 struct task_struct *task;
2675 struct css_task_iter it;
2679 * Autogroups do not have RT tasks; see autogroup_create().
2681 if (task_group_is_autogroup(tg))
2684 css_task_iter_start(&tg->css, 0, &it);
2685 while (!ret && (task = css_task_iter_next(&it)))
2686 ret |= rt_task(task);
2687 css_task_iter_end(&it);
2692 struct rt_schedulable_data {
2693 struct task_group *tg;
2698 static int tg_rt_schedulable(struct task_group *tg, void *data)
2700 struct rt_schedulable_data *d = data;
2701 struct task_group *child;
2702 unsigned long total, sum = 0;
2703 u64 period, runtime;
2705 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2706 runtime = tg->rt_bandwidth.rt_runtime;
2709 period = d->rt_period;
2710 runtime = d->rt_runtime;
2714 * Cannot have more runtime than the period.
2716 if (runtime > period && runtime != RUNTIME_INF)
2720 * Ensure we don't starve existing RT tasks if runtime turns zero.
2722 if (rt_bandwidth_enabled() && !runtime &&
2723 tg->rt_bandwidth.rt_runtime && tg_has_rt_tasks(tg))
2726 if (WARN_ON(!rt_group_sched_enabled() && tg != &root_task_group))
2729 total = to_ratio(period, runtime);
2732 * Nobody can have more than the global setting allows.
2734 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
2738 * The sum of our children's runtime should not exceed our own.
2740 list_for_each_entry_rcu(child, &tg->children, siblings) {
2741 period = ktime_to_ns(child->rt_bandwidth.rt_period);
2742 runtime = child->rt_bandwidth.rt_runtime;
2744 if (child == d->tg) {
2745 period = d->rt_period;
2746 runtime = d->rt_runtime;
2749 sum += to_ratio(period, runtime);
2758 static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
2762 struct rt_schedulable_data data = {
2764 .rt_period = period,
2765 .rt_runtime = runtime,
2769 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
2775 static int tg_set_rt_bandwidth(struct task_group *tg,
2776 u64 rt_period, u64 rt_runtime)
2781 * Disallowing the root group RT runtime is BAD, it would disallow the
2782 * kernel creating (and or operating) RT threads.
2784 if (tg == &root_task_group && rt_runtime == 0)
2787 /* No period doesn't make any sense. */
2792 * Bound quota to defend quota against overflow during bandwidth shift.
2794 if (rt_runtime != RUNTIME_INF && rt_runtime > max_rt_runtime)
2797 mutex_lock(&rt_constraints_mutex);
2798 err = __rt_schedulable(tg, rt_period, rt_runtime);
2802 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2803 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
2804 tg->rt_bandwidth.rt_runtime = rt_runtime;
2806 for_each_possible_cpu(i) {
2807 struct rt_rq *rt_rq = tg->rt_rq[i];
2809 raw_spin_lock(&rt_rq->rt_runtime_lock);
2810 rt_rq->rt_runtime = rt_runtime;
2811 raw_spin_unlock(&rt_rq->rt_runtime_lock);
2813 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2815 mutex_unlock(&rt_constraints_mutex);
2820 int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
2822 u64 rt_runtime, rt_period;
2824 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2825 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
2826 if (rt_runtime_us < 0)
2827 rt_runtime = RUNTIME_INF;
2828 else if ((u64)rt_runtime_us > U64_MAX / NSEC_PER_USEC)
2831 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2834 long sched_group_rt_runtime(struct task_group *tg)
2838 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
2841 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
2842 do_div(rt_runtime_us, NSEC_PER_USEC);
2843 return rt_runtime_us;
2846 int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
2848 u64 rt_runtime, rt_period;
2850 if (rt_period_us > U64_MAX / NSEC_PER_USEC)
2853 rt_period = rt_period_us * NSEC_PER_USEC;
2854 rt_runtime = tg->rt_bandwidth.rt_runtime;
2856 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2859 long sched_group_rt_period(struct task_group *tg)
2863 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
2864 do_div(rt_period_us, NSEC_PER_USEC);
2865 return rt_period_us;
2868 #ifdef CONFIG_SYSCTL
2869 static int sched_rt_global_constraints(void)
2873 mutex_lock(&rt_constraints_mutex);
2874 ret = __rt_schedulable(NULL, 0, 0);
2875 mutex_unlock(&rt_constraints_mutex);
2879 #endif /* CONFIG_SYSCTL */
2881 int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
2883 /* Don't accept real-time tasks when there is no way for them to run */
2884 if (rt_group_sched_enabled() && rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
2890 #else /* !CONFIG_RT_GROUP_SCHED */
2892 #ifdef CONFIG_SYSCTL
2893 static int sched_rt_global_constraints(void)
2897 #endif /* CONFIG_SYSCTL */
2898 #endif /* CONFIG_RT_GROUP_SCHED */
2900 #ifdef CONFIG_SYSCTL
2901 static int sched_rt_global_validate(void)
2903 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
2904 ((sysctl_sched_rt_runtime > sysctl_sched_rt_period) ||
2905 ((u64)sysctl_sched_rt_runtime *
2906 NSEC_PER_USEC > max_rt_runtime)))
2912 static void sched_rt_do_global(void)
2916 static int sched_rt_handler(const struct ctl_table *table, int write, void *buffer,
2917 size_t *lenp, loff_t *ppos)
2919 int old_period, old_runtime;
2920 static DEFINE_MUTEX(mutex);
2924 sched_domains_mutex_lock();
2925 old_period = sysctl_sched_rt_period;
2926 old_runtime = sysctl_sched_rt_runtime;
2928 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2930 if (!ret && write) {
2931 ret = sched_rt_global_validate();
2935 ret = sched_dl_global_validate();
2939 ret = sched_rt_global_constraints();
2943 sched_rt_do_global();
2944 sched_dl_do_global();
2948 sysctl_sched_rt_period = old_period;
2949 sysctl_sched_rt_runtime = old_runtime;
2951 sched_domains_mutex_unlock();
2952 mutex_unlock(&mutex);
2957 static int sched_rr_handler(const struct ctl_table *table, int write, void *buffer,
2958 size_t *lenp, loff_t *ppos)
2961 static DEFINE_MUTEX(mutex);
2964 ret = proc_dointvec(table, write, buffer, lenp, ppos);
2966 * Make sure that internally we keep jiffies.
2967 * Also, writing zero resets the time-slice to default:
2969 if (!ret && write) {
2970 sched_rr_timeslice =
2971 sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
2972 msecs_to_jiffies(sysctl_sched_rr_timeslice);
2974 if (sysctl_sched_rr_timeslice <= 0)
2975 sysctl_sched_rr_timeslice = jiffies_to_msecs(RR_TIMESLICE);
2977 mutex_unlock(&mutex);
2981 #endif /* CONFIG_SYSCTL */
2983 void print_rt_stats(struct seq_file *m, int cpu)
2986 struct rt_rq *rt_rq;
2989 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
2990 print_rt_rq(m, cpu, rt_rq);