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;
11 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
13 struct rt_bandwidth def_rt_bandwidth;
16 * period over which we measure -rt task CPU usage in us.
19 int sysctl_sched_rt_period = 1000000;
22 * part of the period that we allow rt tasks to run in us.
25 int sysctl_sched_rt_runtime = 950000;
28 static int sysctl_sched_rr_timeslice = (MSEC_PER_SEC * RR_TIMESLICE) / HZ;
29 static int sched_rt_handler(const struct ctl_table *table, int write, void *buffer,
30 size_t *lenp, loff_t *ppos);
31 static int sched_rr_handler(const struct ctl_table *table, int write, void *buffer,
32 size_t *lenp, loff_t *ppos);
33 static struct ctl_table sched_rt_sysctls[] = {
35 .procname = "sched_rt_period_us",
36 .data = &sysctl_sched_rt_period,
37 .maxlen = sizeof(int),
39 .proc_handler = sched_rt_handler,
41 .extra2 = SYSCTL_INT_MAX,
44 .procname = "sched_rt_runtime_us",
45 .data = &sysctl_sched_rt_runtime,
46 .maxlen = sizeof(int),
48 .proc_handler = sched_rt_handler,
49 .extra1 = SYSCTL_NEG_ONE,
50 .extra2 = (void *)&sysctl_sched_rt_period,
53 .procname = "sched_rr_timeslice_ms",
54 .data = &sysctl_sched_rr_timeslice,
55 .maxlen = sizeof(int),
57 .proc_handler = sched_rr_handler,
61 static int __init sched_rt_sysctl_init(void)
63 register_sysctl_init("kernel", sched_rt_sysctls);
66 late_initcall(sched_rt_sysctl_init);
69 static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
71 struct rt_bandwidth *rt_b =
72 container_of(timer, struct rt_bandwidth, rt_period_timer);
76 raw_spin_lock(&rt_b->rt_runtime_lock);
78 overrun = hrtimer_forward_now(timer, rt_b->rt_period);
82 raw_spin_unlock(&rt_b->rt_runtime_lock);
83 idle = do_sched_rt_period_timer(rt_b, overrun);
84 raw_spin_lock(&rt_b->rt_runtime_lock);
87 rt_b->rt_period_active = 0;
88 raw_spin_unlock(&rt_b->rt_runtime_lock);
90 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
93 void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
95 rt_b->rt_period = ns_to_ktime(period);
96 rt_b->rt_runtime = runtime;
98 raw_spin_lock_init(&rt_b->rt_runtime_lock);
100 hrtimer_init(&rt_b->rt_period_timer, CLOCK_MONOTONIC,
101 HRTIMER_MODE_REL_HARD);
102 rt_b->rt_period_timer.function = sched_rt_period_timer;
105 static inline void do_start_rt_bandwidth(struct rt_bandwidth *rt_b)
107 raw_spin_lock(&rt_b->rt_runtime_lock);
108 if (!rt_b->rt_period_active) {
109 rt_b->rt_period_active = 1;
111 * SCHED_DEADLINE updates the bandwidth, as a run away
112 * RT task with a DL task could hog a CPU. But DL does
113 * not reset the period. If a deadline task was running
114 * without an RT task running, it can cause RT tasks to
115 * throttle when they start up. Kick the timer right away
116 * to update the period.
118 hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
119 hrtimer_start_expires(&rt_b->rt_period_timer,
120 HRTIMER_MODE_ABS_PINNED_HARD);
122 raw_spin_unlock(&rt_b->rt_runtime_lock);
125 static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
127 if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
130 do_start_rt_bandwidth(rt_b);
133 void init_rt_rq(struct rt_rq *rt_rq)
135 struct rt_prio_array *array;
138 array = &rt_rq->active;
139 for (i = 0; i < MAX_RT_PRIO; i++) {
140 INIT_LIST_HEAD(array->queue + i);
141 __clear_bit(i, array->bitmap);
143 /* delimiter for bit-search: */
144 __set_bit(MAX_RT_PRIO, array->bitmap);
146 #if defined CONFIG_SMP
147 rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
148 rt_rq->highest_prio.next = MAX_RT_PRIO-1;
149 rt_rq->overloaded = 0;
150 plist_head_init(&rt_rq->pushable_tasks);
151 #endif /* CONFIG_SMP */
152 /* We start is dequeued state, because no RT tasks are queued */
153 rt_rq->rt_queued = 0;
156 rt_rq->rt_throttled = 0;
157 rt_rq->rt_runtime = 0;
158 raw_spin_lock_init(&rt_rq->rt_runtime_lock);
161 #ifdef CONFIG_RT_GROUP_SCHED
162 static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
164 hrtimer_cancel(&rt_b->rt_period_timer);
167 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
169 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
171 #ifdef CONFIG_SCHED_DEBUG
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)
182 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
187 static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
189 struct rt_rq *rt_rq = rt_se->rt_rq;
194 void unregister_rt_sched_group(struct task_group *tg)
197 destroy_rt_bandwidth(&tg->rt_bandwidth);
201 void free_rt_sched_group(struct task_group *tg)
205 for_each_possible_cpu(i) {
216 void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
217 struct sched_rt_entity *rt_se, int cpu,
218 struct sched_rt_entity *parent)
220 struct rq *rq = cpu_rq(cpu);
222 rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
223 rt_rq->rt_nr_boosted = 0;
227 tg->rt_rq[cpu] = rt_rq;
228 tg->rt_se[cpu] = rt_se;
234 rt_se->rt_rq = &rq->rt;
236 rt_se->rt_rq = parent->my_q;
239 rt_se->parent = parent;
240 INIT_LIST_HEAD(&rt_se->run_list);
243 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
246 struct sched_rt_entity *rt_se;
249 tg->rt_rq = kcalloc(nr_cpu_ids, sizeof(rt_rq), GFP_KERNEL);
252 tg->rt_se = kcalloc(nr_cpu_ids, sizeof(rt_se), GFP_KERNEL);
256 init_rt_bandwidth(&tg->rt_bandwidth,
257 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
259 for_each_possible_cpu(i) {
260 rt_rq = kzalloc_node(sizeof(struct rt_rq),
261 GFP_KERNEL, cpu_to_node(i));
265 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
266 GFP_KERNEL, cpu_to_node(i));
271 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
272 init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
283 #else /* CONFIG_RT_GROUP_SCHED */
285 #define rt_entity_is_task(rt_se) (1)
287 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
289 return container_of(rt_se, struct task_struct, rt);
292 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
294 return container_of(rt_rq, struct rq, rt);
297 static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
299 struct task_struct *p = rt_task_of(rt_se);
304 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
306 struct rq *rq = rq_of_rt_se(rt_se);
311 void unregister_rt_sched_group(struct task_group *tg) { }
313 void free_rt_sched_group(struct task_group *tg) { }
315 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
319 #endif /* CONFIG_RT_GROUP_SCHED */
323 static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
325 /* Try to pull RT tasks here if we lower this rq's prio */
326 return rq->online && rq->rt.highest_prio.curr > prev->prio;
329 static inline int rt_overloaded(struct rq *rq)
331 return atomic_read(&rq->rd->rto_count);
334 static inline void rt_set_overload(struct rq *rq)
339 cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
341 * Make sure the mask is visible before we set
342 * the overload count. That is checked to determine
343 * if we should look at the mask. It would be a shame
344 * if we looked at the mask, but the mask was not
347 * Matched by the barrier in pull_rt_task().
350 atomic_inc(&rq->rd->rto_count);
353 static inline void rt_clear_overload(struct rq *rq)
358 /* the order here really doesn't matter */
359 atomic_dec(&rq->rd->rto_count);
360 cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
363 static inline int has_pushable_tasks(struct rq *rq)
365 return !plist_head_empty(&rq->rt.pushable_tasks);
368 static DEFINE_PER_CPU(struct balance_callback, rt_push_head);
369 static DEFINE_PER_CPU(struct balance_callback, rt_pull_head);
371 static void push_rt_tasks(struct rq *);
372 static void pull_rt_task(struct rq *);
374 static inline void rt_queue_push_tasks(struct rq *rq)
376 if (!has_pushable_tasks(rq))
379 queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
382 static inline void rt_queue_pull_task(struct rq *rq)
384 queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
387 static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
389 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
390 plist_node_init(&p->pushable_tasks, p->prio);
391 plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
393 /* Update the highest prio pushable task */
394 if (p->prio < rq->rt.highest_prio.next)
395 rq->rt.highest_prio.next = p->prio;
397 if (!rq->rt.overloaded) {
399 rq->rt.overloaded = 1;
403 static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
405 plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
407 /* Update the new highest prio pushable task */
408 if (has_pushable_tasks(rq)) {
409 p = plist_first_entry(&rq->rt.pushable_tasks,
410 struct task_struct, pushable_tasks);
411 rq->rt.highest_prio.next = p->prio;
413 rq->rt.highest_prio.next = MAX_RT_PRIO-1;
415 if (rq->rt.overloaded) {
416 rt_clear_overload(rq);
417 rq->rt.overloaded = 0;
424 static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
428 static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
432 static inline void rt_queue_push_tasks(struct rq *rq)
435 #endif /* CONFIG_SMP */
437 static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
438 static void dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count);
440 static inline int on_rt_rq(struct sched_rt_entity *rt_se)
445 #ifdef CONFIG_UCLAMP_TASK
447 * Verify the fitness of task @p to run on @cpu taking into account the uclamp
450 * This check is only important for heterogeneous systems where uclamp_min value
451 * is higher than the capacity of a @cpu. For non-heterogeneous system this
452 * function will always return true.
454 * The function will return true if the capacity of the @cpu is >= the
455 * uclamp_min and false otherwise.
457 * Note that uclamp_min will be clamped to uclamp_max if uclamp_min
460 static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
462 unsigned int min_cap;
463 unsigned int max_cap;
464 unsigned int cpu_cap;
466 /* Only heterogeneous systems can benefit from this check */
467 if (!sched_asym_cpucap_active())
470 min_cap = uclamp_eff_value(p, UCLAMP_MIN);
471 max_cap = uclamp_eff_value(p, UCLAMP_MAX);
473 cpu_cap = arch_scale_cpu_capacity(cpu);
475 return cpu_cap >= min(min_cap, max_cap);
478 static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
484 #ifdef CONFIG_RT_GROUP_SCHED
486 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
491 return rt_rq->rt_runtime;
494 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
496 return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
499 typedef struct task_group *rt_rq_iter_t;
501 static inline struct task_group *next_task_group(struct task_group *tg)
504 tg = list_entry_rcu(tg->list.next,
505 typeof(struct task_group), list);
506 } while (&tg->list != &task_groups && task_group_is_autogroup(tg));
508 if (&tg->list == &task_groups)
514 #define for_each_rt_rq(rt_rq, iter, rq) \
515 for (iter = container_of(&task_groups, typeof(*iter), list); \
516 (iter = next_task_group(iter)) && \
517 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
519 #define for_each_sched_rt_entity(rt_se) \
520 for (; rt_se; rt_se = rt_se->parent)
522 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
527 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
528 static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
530 static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
532 struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
533 struct rq *rq = rq_of_rt_rq(rt_rq);
534 struct sched_rt_entity *rt_se;
536 int cpu = cpu_of(rq);
538 rt_se = rt_rq->tg->rt_se[cpu];
540 if (rt_rq->rt_nr_running) {
542 enqueue_top_rt_rq(rt_rq);
543 else if (!on_rt_rq(rt_se))
544 enqueue_rt_entity(rt_se, 0);
546 if (rt_rq->highest_prio.curr < curr->prio)
551 static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
553 struct sched_rt_entity *rt_se;
554 int cpu = cpu_of(rq_of_rt_rq(rt_rq));
556 rt_se = rt_rq->tg->rt_se[cpu];
559 dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
560 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
561 cpufreq_update_util(rq_of_rt_rq(rt_rq), 0);
563 else if (on_rt_rq(rt_se))
564 dequeue_rt_entity(rt_se, 0);
567 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
569 return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
572 static int rt_se_boosted(struct sched_rt_entity *rt_se)
574 struct rt_rq *rt_rq = group_rt_rq(rt_se);
575 struct task_struct *p;
578 return !!rt_rq->rt_nr_boosted;
580 p = rt_task_of(rt_se);
581 return p->prio != p->normal_prio;
585 static inline const struct cpumask *sched_rt_period_mask(void)
587 return this_rq()->rd->span;
590 static inline const struct cpumask *sched_rt_period_mask(void)
592 return cpu_online_mask;
597 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
599 return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
602 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
604 return &rt_rq->tg->rt_bandwidth;
607 #else /* !CONFIG_RT_GROUP_SCHED */
609 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
611 return rt_rq->rt_runtime;
614 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
616 return ktime_to_ns(def_rt_bandwidth.rt_period);
619 typedef struct rt_rq *rt_rq_iter_t;
621 #define for_each_rt_rq(rt_rq, iter, rq) \
622 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
624 #define for_each_sched_rt_entity(rt_se) \
625 for (; rt_se; rt_se = NULL)
627 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
632 static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
634 struct rq *rq = rq_of_rt_rq(rt_rq);
636 if (!rt_rq->rt_nr_running)
639 enqueue_top_rt_rq(rt_rq);
643 static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
645 dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
648 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
650 return rt_rq->rt_throttled;
653 static inline const struct cpumask *sched_rt_period_mask(void)
655 return cpu_online_mask;
659 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
661 return &cpu_rq(cpu)->rt;
664 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
666 return &def_rt_bandwidth;
669 #endif /* CONFIG_RT_GROUP_SCHED */
671 bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
673 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
675 return (hrtimer_active(&rt_b->rt_period_timer) ||
676 rt_rq->rt_time < rt_b->rt_runtime);
681 * We ran out of runtime, see if we can borrow some from our neighbours.
683 static void do_balance_runtime(struct rt_rq *rt_rq)
685 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
686 struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
690 weight = cpumask_weight(rd->span);
692 raw_spin_lock(&rt_b->rt_runtime_lock);
693 rt_period = ktime_to_ns(rt_b->rt_period);
694 for_each_cpu(i, rd->span) {
695 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
701 raw_spin_lock(&iter->rt_runtime_lock);
703 * Either all rqs have inf runtime and there's nothing to steal
704 * or __disable_runtime() below sets a specific rq to inf to
705 * indicate its been disabled and disallow stealing.
707 if (iter->rt_runtime == RUNTIME_INF)
711 * From runqueues with spare time, take 1/n part of their
712 * spare time, but no more than our period.
714 diff = iter->rt_runtime - iter->rt_time;
716 diff = div_u64((u64)diff, weight);
717 if (rt_rq->rt_runtime + diff > rt_period)
718 diff = rt_period - rt_rq->rt_runtime;
719 iter->rt_runtime -= diff;
720 rt_rq->rt_runtime += diff;
721 if (rt_rq->rt_runtime == rt_period) {
722 raw_spin_unlock(&iter->rt_runtime_lock);
727 raw_spin_unlock(&iter->rt_runtime_lock);
729 raw_spin_unlock(&rt_b->rt_runtime_lock);
733 * Ensure this RQ takes back all the runtime it lend to its neighbours.
735 static void __disable_runtime(struct rq *rq)
737 struct root_domain *rd = rq->rd;
741 if (unlikely(!scheduler_running))
744 for_each_rt_rq(rt_rq, iter, rq) {
745 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
749 raw_spin_lock(&rt_b->rt_runtime_lock);
750 raw_spin_lock(&rt_rq->rt_runtime_lock);
752 * Either we're all inf and nobody needs to borrow, or we're
753 * already disabled and thus have nothing to do, or we have
754 * exactly the right amount of runtime to take out.
756 if (rt_rq->rt_runtime == RUNTIME_INF ||
757 rt_rq->rt_runtime == rt_b->rt_runtime)
759 raw_spin_unlock(&rt_rq->rt_runtime_lock);
762 * Calculate the difference between what we started out with
763 * and what we current have, that's the amount of runtime
764 * we lend and now have to reclaim.
766 want = rt_b->rt_runtime - rt_rq->rt_runtime;
769 * Greedy reclaim, take back as much as we can.
771 for_each_cpu(i, rd->span) {
772 struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
776 * Can't reclaim from ourselves or disabled runqueues.
778 if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
781 raw_spin_lock(&iter->rt_runtime_lock);
783 diff = min_t(s64, iter->rt_runtime, want);
784 iter->rt_runtime -= diff;
787 iter->rt_runtime -= want;
790 raw_spin_unlock(&iter->rt_runtime_lock);
796 raw_spin_lock(&rt_rq->rt_runtime_lock);
798 * We cannot be left wanting - that would mean some runtime
799 * leaked out of the system.
804 * Disable all the borrow logic by pretending we have inf
805 * runtime - in which case borrowing doesn't make sense.
807 rt_rq->rt_runtime = RUNTIME_INF;
808 rt_rq->rt_throttled = 0;
809 raw_spin_unlock(&rt_rq->rt_runtime_lock);
810 raw_spin_unlock(&rt_b->rt_runtime_lock);
812 /* Make rt_rq available for pick_next_task() */
813 sched_rt_rq_enqueue(rt_rq);
817 static void __enable_runtime(struct rq *rq)
822 if (unlikely(!scheduler_running))
826 * Reset each runqueue's bandwidth settings
828 for_each_rt_rq(rt_rq, iter, rq) {
829 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
831 raw_spin_lock(&rt_b->rt_runtime_lock);
832 raw_spin_lock(&rt_rq->rt_runtime_lock);
833 rt_rq->rt_runtime = rt_b->rt_runtime;
835 rt_rq->rt_throttled = 0;
836 raw_spin_unlock(&rt_rq->rt_runtime_lock);
837 raw_spin_unlock(&rt_b->rt_runtime_lock);
841 static void balance_runtime(struct rt_rq *rt_rq)
843 if (!sched_feat(RT_RUNTIME_SHARE))
846 if (rt_rq->rt_time > rt_rq->rt_runtime) {
847 raw_spin_unlock(&rt_rq->rt_runtime_lock);
848 do_balance_runtime(rt_rq);
849 raw_spin_lock(&rt_rq->rt_runtime_lock);
852 #else /* !CONFIG_SMP */
853 static inline void balance_runtime(struct rt_rq *rt_rq) {}
854 #endif /* CONFIG_SMP */
856 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
858 int i, idle = 1, throttled = 0;
859 const struct cpumask *span;
861 span = sched_rt_period_mask();
862 #ifdef CONFIG_RT_GROUP_SCHED
864 * FIXME: isolated CPUs should really leave the root task group,
865 * whether they are isolcpus or were isolated via cpusets, lest
866 * the timer run on a CPU which does not service all runqueues,
867 * potentially leaving other CPUs indefinitely throttled. If
868 * isolation is really required, the user will turn the throttle
869 * off to kill the perturbations it causes anyway. Meanwhile,
870 * this maintains functionality for boot and/or troubleshooting.
872 if (rt_b == &root_task_group.rt_bandwidth)
873 span = cpu_online_mask;
875 for_each_cpu(i, span) {
877 struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
878 struct rq *rq = rq_of_rt_rq(rt_rq);
883 * When span == cpu_online_mask, taking each rq->lock
884 * can be time-consuming. Try to avoid it when possible.
886 raw_spin_lock(&rt_rq->rt_runtime_lock);
887 if (!sched_feat(RT_RUNTIME_SHARE) && rt_rq->rt_runtime != RUNTIME_INF)
888 rt_rq->rt_runtime = rt_b->rt_runtime;
889 skip = !rt_rq->rt_time && !rt_rq->rt_nr_running;
890 raw_spin_unlock(&rt_rq->rt_runtime_lock);
897 if (rt_rq->rt_time) {
900 raw_spin_lock(&rt_rq->rt_runtime_lock);
901 if (rt_rq->rt_throttled)
902 balance_runtime(rt_rq);
903 runtime = rt_rq->rt_runtime;
904 rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
905 if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
906 rt_rq->rt_throttled = 0;
910 * When we're idle and a woken (rt) task is
911 * throttled wakeup_preempt() will set
912 * skip_update and the time between the wakeup
913 * and this unthrottle will get accounted as
916 if (rt_rq->rt_nr_running && rq->curr == rq->idle)
917 rq_clock_cancel_skipupdate(rq);
919 if (rt_rq->rt_time || rt_rq->rt_nr_running)
921 raw_spin_unlock(&rt_rq->rt_runtime_lock);
922 } else if (rt_rq->rt_nr_running) {
924 if (!rt_rq_throttled(rt_rq))
927 if (rt_rq->rt_throttled)
931 sched_rt_rq_enqueue(rt_rq);
935 if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
941 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
943 #ifdef CONFIG_RT_GROUP_SCHED
944 struct rt_rq *rt_rq = group_rt_rq(rt_se);
947 return rt_rq->highest_prio.curr;
950 return rt_task_of(rt_se)->prio;
953 static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
955 u64 runtime = sched_rt_runtime(rt_rq);
957 if (rt_rq->rt_throttled)
958 return rt_rq_throttled(rt_rq);
960 if (runtime >= sched_rt_period(rt_rq))
963 balance_runtime(rt_rq);
964 runtime = sched_rt_runtime(rt_rq);
965 if (runtime == RUNTIME_INF)
968 if (rt_rq->rt_time > runtime) {
969 struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
972 * Don't actually throttle groups that have no runtime assigned
973 * but accrue some time due to boosting.
975 if (likely(rt_b->rt_runtime)) {
976 rt_rq->rt_throttled = 1;
977 printk_deferred_once("sched: RT throttling activated\n");
980 * In case we did anyway, make it go away,
981 * replenishment is a joke, since it will replenish us
987 if (rt_rq_throttled(rt_rq)) {
988 sched_rt_rq_dequeue(rt_rq);
997 * Update the current task's runtime statistics. Skip current tasks that
998 * are not in our scheduling class.
1000 static void update_curr_rt(struct rq *rq)
1002 struct task_struct *curr = rq->curr;
1003 struct sched_rt_entity *rt_se = &curr->rt;
1006 if (curr->sched_class != &rt_sched_class)
1009 delta_exec = update_curr_common(rq);
1010 if (unlikely(delta_exec <= 0))
1013 if (!rt_bandwidth_enabled())
1016 for_each_sched_rt_entity(rt_se) {
1017 struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1020 if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
1021 raw_spin_lock(&rt_rq->rt_runtime_lock);
1022 rt_rq->rt_time += delta_exec;
1023 exceeded = sched_rt_runtime_exceeded(rt_rq);
1026 raw_spin_unlock(&rt_rq->rt_runtime_lock);
1028 do_start_rt_bandwidth(sched_rt_bandwidth(rt_rq));
1034 dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count)
1036 struct rq *rq = rq_of_rt_rq(rt_rq);
1038 BUG_ON(&rq->rt != rt_rq);
1040 if (!rt_rq->rt_queued)
1043 BUG_ON(!rq->nr_running);
1045 sub_nr_running(rq, count);
1046 rt_rq->rt_queued = 0;
1051 enqueue_top_rt_rq(struct rt_rq *rt_rq)
1053 struct rq *rq = rq_of_rt_rq(rt_rq);
1055 BUG_ON(&rq->rt != rt_rq);
1057 if (rt_rq->rt_queued)
1060 if (rt_rq_throttled(rt_rq))
1063 if (rt_rq->rt_nr_running) {
1064 add_nr_running(rq, rt_rq->rt_nr_running);
1065 rt_rq->rt_queued = 1;
1068 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
1069 cpufreq_update_util(rq, 0);
1072 #if defined CONFIG_SMP
1075 inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1077 struct rq *rq = rq_of_rt_rq(rt_rq);
1079 #ifdef CONFIG_RT_GROUP_SCHED
1081 * Change rq's cpupri only if rt_rq is the top queue.
1083 if (&rq->rt != rt_rq)
1086 if (rq->online && prio < prev_prio)
1087 cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
1091 dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1093 struct rq *rq = rq_of_rt_rq(rt_rq);
1095 #ifdef CONFIG_RT_GROUP_SCHED
1097 * Change rq's cpupri only if rt_rq is the top queue.
1099 if (&rq->rt != rt_rq)
1102 if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1103 cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1106 #else /* CONFIG_SMP */
1109 void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1111 void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1113 #endif /* CONFIG_SMP */
1115 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1117 inc_rt_prio(struct rt_rq *rt_rq, int prio)
1119 int prev_prio = rt_rq->highest_prio.curr;
1121 if (prio < prev_prio)
1122 rt_rq->highest_prio.curr = prio;
1124 inc_rt_prio_smp(rt_rq, prio, prev_prio);
1128 dec_rt_prio(struct rt_rq *rt_rq, int prio)
1130 int prev_prio = rt_rq->highest_prio.curr;
1132 if (rt_rq->rt_nr_running) {
1134 WARN_ON(prio < prev_prio);
1137 * This may have been our highest task, and therefore
1138 * we may have some re-computation to do
1140 if (prio == prev_prio) {
1141 struct rt_prio_array *array = &rt_rq->active;
1143 rt_rq->highest_prio.curr =
1144 sched_find_first_bit(array->bitmap);
1148 rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
1151 dec_rt_prio_smp(rt_rq, prio, prev_prio);
1156 static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1157 static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1159 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1161 #ifdef CONFIG_RT_GROUP_SCHED
1164 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1166 if (rt_se_boosted(rt_se))
1167 rt_rq->rt_nr_boosted++;
1170 start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
1174 dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1176 if (rt_se_boosted(rt_se))
1177 rt_rq->rt_nr_boosted--;
1179 WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
1182 #else /* CONFIG_RT_GROUP_SCHED */
1185 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1187 start_rt_bandwidth(&def_rt_bandwidth);
1191 void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1193 #endif /* CONFIG_RT_GROUP_SCHED */
1196 unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1198 struct rt_rq *group_rq = group_rt_rq(rt_se);
1201 return group_rq->rt_nr_running;
1207 unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
1209 struct rt_rq *group_rq = group_rt_rq(rt_se);
1210 struct task_struct *tsk;
1213 return group_rq->rr_nr_running;
1215 tsk = rt_task_of(rt_se);
1217 return (tsk->policy == SCHED_RR) ? 1 : 0;
1221 void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1223 int prio = rt_se_prio(rt_se);
1225 WARN_ON(!rt_prio(prio));
1226 rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
1227 rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
1229 inc_rt_prio(rt_rq, prio);
1230 inc_rt_group(rt_se, rt_rq);
1234 void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1236 WARN_ON(!rt_prio(rt_se_prio(rt_se)));
1237 WARN_ON(!rt_rq->rt_nr_running);
1238 rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
1239 rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
1241 dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1242 dec_rt_group(rt_se, rt_rq);
1246 * Change rt_se->run_list location unless SAVE && !MOVE
1248 * assumes ENQUEUE/DEQUEUE flags match
1250 static inline bool move_entity(unsigned int flags)
1252 if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
1258 static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
1260 list_del_init(&rt_se->run_list);
1262 if (list_empty(array->queue + rt_se_prio(rt_se)))
1263 __clear_bit(rt_se_prio(rt_se), array->bitmap);
1268 static inline struct sched_statistics *
1269 __schedstats_from_rt_se(struct sched_rt_entity *rt_se)
1271 #ifdef CONFIG_RT_GROUP_SCHED
1272 /* schedstats is not supported for rt group. */
1273 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 void 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);
1506 * Put task to the head or the end of the run list without the overhead of
1507 * dequeue followed by enqueue.
1510 requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
1512 if (on_rt_rq(rt_se)) {
1513 struct rt_prio_array *array = &rt_rq->active;
1514 struct list_head *queue = array->queue + rt_se_prio(rt_se);
1517 list_move(&rt_se->run_list, queue);
1519 list_move_tail(&rt_se->run_list, queue);
1523 static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
1525 struct sched_rt_entity *rt_se = &p->rt;
1526 struct rt_rq *rt_rq;
1528 for_each_sched_rt_entity(rt_se) {
1529 rt_rq = rt_rq_of_se(rt_se);
1530 requeue_rt_entity(rt_rq, rt_se, head);
1534 static void yield_task_rt(struct rq *rq)
1536 requeue_task_rt(rq, rq->curr, 0);
1540 static int find_lowest_rq(struct task_struct *task);
1543 select_task_rq_rt(struct task_struct *p, int cpu, int flags)
1545 struct task_struct *curr;
1549 /* For anything but wake ups, just return the task_cpu */
1550 if (!(flags & (WF_TTWU | WF_FORK)))
1556 curr = READ_ONCE(rq->curr); /* unlocked access */
1559 * If the current task on @p's runqueue is an RT task, then
1560 * try to see if we can wake this RT task up on another
1561 * runqueue. Otherwise simply start this RT task
1562 * on its current runqueue.
1564 * We want to avoid overloading runqueues. If the woken
1565 * task is a higher priority, then it will stay on this CPU
1566 * and the lower prio task should be moved to another CPU.
1567 * Even though this will probably make the lower prio task
1568 * lose its cache, we do not want to bounce a higher task
1569 * around just because it gave up its CPU, perhaps for a
1572 * For equal prio tasks, we just let the scheduler sort it out.
1574 * Otherwise, just let it ride on the affine RQ and the
1575 * post-schedule router will push the preempted task away
1577 * This test is optimistic, if we get it wrong the load-balancer
1578 * will have to sort it out.
1580 * We take into account the capacity of the CPU to ensure it fits the
1581 * requirement of the task - which is only important on heterogeneous
1582 * systems like big.LITTLE.
1585 unlikely(rt_task(curr)) &&
1586 (curr->nr_cpus_allowed < 2 || curr->prio <= p->prio);
1588 if (test || !rt_task_fits_capacity(p, cpu)) {
1589 int target = find_lowest_rq(p);
1592 * Bail out if we were forcing a migration to find a better
1593 * fitting CPU but our search failed.
1595 if (!test && target != -1 && !rt_task_fits_capacity(p, target))
1599 * Don't bother moving it if the destination CPU is
1600 * not running a lower priority task.
1603 p->prio < cpu_rq(target)->rt.highest_prio.curr)
1614 static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1617 * Current can't be migrated, useless to reschedule,
1618 * let's hope p can move out.
1620 if (rq->curr->nr_cpus_allowed == 1 ||
1621 !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1625 * p is migratable, so let's not schedule it and
1626 * see if it is pushed or pulled somewhere else.
1628 if (p->nr_cpus_allowed != 1 &&
1629 cpupri_find(&rq->rd->cpupri, p, NULL))
1633 * There appear to be other CPUs that can accept
1634 * the current task but none can run 'p', so lets reschedule
1635 * to try and push the current task away:
1637 requeue_task_rt(rq, p, 1);
1641 static int balance_rt(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1643 if (!on_rt_rq(&p->rt) && need_pull_rt_task(rq, p)) {
1645 * This is OK, because current is on_cpu, which avoids it being
1646 * picked for load-balance and preemption/IRQs are still
1647 * disabled avoiding further scheduler activity on it and we've
1648 * not yet started the picking loop.
1650 rq_unpin_lock(rq, rf);
1652 rq_repin_lock(rq, rf);
1655 return sched_stop_runnable(rq) || sched_dl_runnable(rq) || sched_rt_runnable(rq);
1657 #endif /* CONFIG_SMP */
1660 * Preempt the current task with a newly woken task if needed:
1662 static void wakeup_preempt_rt(struct rq *rq, struct task_struct *p, int flags)
1664 if (p->prio < rq->curr->prio) {
1673 * - the newly woken task is of equal priority to the current task
1674 * - the newly woken task is non-migratable while current is migratable
1675 * - current will be preempted on the next reschedule
1677 * we should check to see if current can readily move to a different
1678 * cpu. If so, we will reschedule to allow the push logic to try
1679 * to move current somewhere else, making room for our non-migratable
1682 if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
1683 check_preempt_equal_prio(rq, p);
1687 static inline void set_next_task_rt(struct rq *rq, struct task_struct *p, bool first)
1689 struct sched_rt_entity *rt_se = &p->rt;
1690 struct rt_rq *rt_rq = &rq->rt;
1692 p->se.exec_start = rq_clock_task(rq);
1693 if (on_rt_rq(&p->rt))
1694 update_stats_wait_end_rt(rt_rq, rt_se);
1696 /* The running task is never eligible for pushing */
1697 dequeue_pushable_task(rq, p);
1703 * If prev task was rt, put_prev_task() has already updated the
1704 * utilization. We only care of the case where we start to schedule a
1707 if (rq->curr->sched_class != &rt_sched_class)
1708 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
1710 rt_queue_push_tasks(rq);
1713 static struct sched_rt_entity *pick_next_rt_entity(struct rt_rq *rt_rq)
1715 struct rt_prio_array *array = &rt_rq->active;
1716 struct sched_rt_entity *next = NULL;
1717 struct list_head *queue;
1720 idx = sched_find_first_bit(array->bitmap);
1721 BUG_ON(idx >= MAX_RT_PRIO);
1723 queue = array->queue + idx;
1724 if (SCHED_WARN_ON(list_empty(queue)))
1726 next = list_entry(queue->next, struct sched_rt_entity, run_list);
1731 static struct task_struct *_pick_next_task_rt(struct rq *rq)
1733 struct sched_rt_entity *rt_se;
1734 struct rt_rq *rt_rq = &rq->rt;
1737 rt_se = pick_next_rt_entity(rt_rq);
1738 if (unlikely(!rt_se))
1740 rt_rq = group_rt_rq(rt_se);
1743 return rt_task_of(rt_se);
1746 static struct task_struct *pick_task_rt(struct rq *rq)
1748 struct task_struct *p;
1750 if (!sched_rt_runnable(rq))
1753 p = _pick_next_task_rt(rq);
1758 static struct task_struct *pick_next_task_rt(struct rq *rq)
1760 struct task_struct *p = pick_task_rt(rq);
1763 set_next_task_rt(rq, p, true);
1768 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
1770 struct sched_rt_entity *rt_se = &p->rt;
1771 struct rt_rq *rt_rq = &rq->rt;
1773 if (on_rt_rq(&p->rt))
1774 update_stats_wait_start_rt(rt_rq, rt_se);
1778 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
1781 * The previous task needs to be made eligible for pushing
1782 * if it is still active
1784 if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
1785 enqueue_pushable_task(rq, p);
1790 /* Only try algorithms three times */
1791 #define RT_MAX_TRIES 3
1793 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1795 if (!task_on_cpu(rq, p) &&
1796 cpumask_test_cpu(cpu, &p->cpus_mask))
1803 * Return the highest pushable rq's task, which is suitable to be executed
1804 * on the CPU, NULL otherwise
1806 static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
1808 struct plist_head *head = &rq->rt.pushable_tasks;
1809 struct task_struct *p;
1811 if (!has_pushable_tasks(rq))
1814 plist_for_each_entry(p, head, pushable_tasks) {
1815 if (pick_rt_task(rq, p, cpu))
1822 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
1824 static int find_lowest_rq(struct task_struct *task)
1826 struct sched_domain *sd;
1827 struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
1828 int this_cpu = smp_processor_id();
1829 int cpu = task_cpu(task);
1832 /* Make sure the mask is initialized first */
1833 if (unlikely(!lowest_mask))
1836 if (task->nr_cpus_allowed == 1)
1837 return -1; /* No other targets possible */
1840 * If we're on asym system ensure we consider the different capacities
1841 * of the CPUs when searching for the lowest_mask.
1843 if (sched_asym_cpucap_active()) {
1845 ret = cpupri_find_fitness(&task_rq(task)->rd->cpupri,
1847 rt_task_fits_capacity);
1850 ret = cpupri_find(&task_rq(task)->rd->cpupri,
1855 return -1; /* No targets found */
1858 * At this point we have built a mask of CPUs representing the
1859 * lowest priority tasks in the system. Now we want to elect
1860 * the best one based on our affinity and topology.
1862 * We prioritize the last CPU that the task executed on since
1863 * it is most likely cache-hot in that location.
1865 if (cpumask_test_cpu(cpu, lowest_mask))
1869 * Otherwise, we consult the sched_domains span maps to figure
1870 * out which CPU is logically closest to our hot cache data.
1872 if (!cpumask_test_cpu(this_cpu, lowest_mask))
1873 this_cpu = -1; /* Skip this_cpu opt if not among lowest */
1876 for_each_domain(cpu, sd) {
1877 if (sd->flags & SD_WAKE_AFFINE) {
1881 * "this_cpu" is cheaper to preempt than a
1884 if (this_cpu != -1 &&
1885 cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1890 best_cpu = cpumask_any_and_distribute(lowest_mask,
1891 sched_domain_span(sd));
1892 if (best_cpu < nr_cpu_ids) {
1901 * And finally, if there were no matches within the domains
1902 * just give the caller *something* to work with from the compatible
1908 cpu = cpumask_any_distribute(lowest_mask);
1909 if (cpu < nr_cpu_ids)
1915 /* Will lock the rq it finds */
1916 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
1918 struct rq *lowest_rq = NULL;
1922 for (tries = 0; tries < RT_MAX_TRIES; tries++) {
1923 cpu = find_lowest_rq(task);
1925 if ((cpu == -1) || (cpu == rq->cpu))
1928 lowest_rq = cpu_rq(cpu);
1930 if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1932 * Target rq has tasks of equal or higher priority,
1933 * retrying does not release any lock and is unlikely
1934 * to yield a different result.
1940 /* if the prio of this runqueue changed, try again */
1941 if (double_lock_balance(rq, lowest_rq)) {
1943 * We had to unlock the run queue. In
1944 * the mean time, task could have
1945 * migrated already or had its affinity changed.
1946 * Also make sure that it wasn't scheduled on its rq.
1947 * It is possible the task was scheduled, set
1948 * "migrate_disabled" and then got preempted, so we must
1949 * check the task migration disable flag here too.
1951 if (unlikely(task_rq(task) != rq ||
1952 !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_mask) ||
1953 task_on_cpu(rq, task) ||
1955 is_migration_disabled(task) ||
1956 !task_on_rq_queued(task))) {
1958 double_unlock_balance(rq, lowest_rq);
1964 /* If this rq is still suitable use it. */
1965 if (lowest_rq->rt.highest_prio.curr > task->prio)
1969 double_unlock_balance(rq, lowest_rq);
1976 static struct task_struct *pick_next_pushable_task(struct rq *rq)
1978 struct task_struct *p;
1980 if (!has_pushable_tasks(rq))
1983 p = plist_first_entry(&rq->rt.pushable_tasks,
1984 struct task_struct, pushable_tasks);
1986 BUG_ON(rq->cpu != task_cpu(p));
1987 BUG_ON(task_current(rq, p));
1988 BUG_ON(p->nr_cpus_allowed <= 1);
1990 BUG_ON(!task_on_rq_queued(p));
1991 BUG_ON(!rt_task(p));
1997 * If the current CPU has more than one RT task, see if the non
1998 * running task can migrate over to a CPU that is running a task
1999 * of lesser priority.
2001 static int push_rt_task(struct rq *rq, bool pull)
2003 struct task_struct *next_task;
2004 struct rq *lowest_rq;
2007 if (!rq->rt.overloaded)
2010 next_task = pick_next_pushable_task(rq);
2016 * It's possible that the next_task slipped in of
2017 * higher priority than current. If that's the case
2018 * just reschedule current.
2020 if (unlikely(next_task->prio < rq->curr->prio)) {
2025 if (is_migration_disabled(next_task)) {
2026 struct task_struct *push_task = NULL;
2029 if (!pull || rq->push_busy)
2033 * Invoking find_lowest_rq() on anything but an RT task doesn't
2034 * make sense. Per the above priority check, curr has to
2035 * be of higher priority than next_task, so no need to
2036 * reschedule when bailing out.
2038 * Note that the stoppers are masqueraded as SCHED_FIFO
2039 * (cf. sched_set_stop_task()), so we can't rely on rt_task().
2041 if (rq->curr->sched_class != &rt_sched_class)
2044 cpu = find_lowest_rq(rq->curr);
2045 if (cpu == -1 || cpu == rq->cpu)
2049 * Given we found a CPU with lower priority than @next_task,
2050 * therefore it should be running. However we cannot migrate it
2051 * to this other CPU, instead attempt to push the current
2052 * running task on this CPU away.
2054 push_task = get_push_task(rq);
2057 raw_spin_rq_unlock(rq);
2058 stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
2059 push_task, &rq->push_work);
2061 raw_spin_rq_lock(rq);
2067 if (WARN_ON(next_task == rq->curr))
2070 /* We might release rq lock */
2071 get_task_struct(next_task);
2073 /* find_lock_lowest_rq locks the rq if found */
2074 lowest_rq = find_lock_lowest_rq(next_task, rq);
2076 struct task_struct *task;
2078 * find_lock_lowest_rq releases rq->lock
2079 * so it is possible that next_task has migrated.
2081 * We need to make sure that the task is still on the same
2082 * run-queue and is also still the next task eligible for
2085 task = pick_next_pushable_task(rq);
2086 if (task == next_task) {
2088 * The task hasn't migrated, and is still the next
2089 * eligible task, but we failed to find a run-queue
2090 * to push it to. Do not retry in this case, since
2091 * other CPUs will pull from us when ready.
2097 /* No more tasks, just exit */
2101 * Something has shifted, try again.
2103 put_task_struct(next_task);
2108 deactivate_task(rq, next_task, 0);
2109 set_task_cpu(next_task, lowest_rq->cpu);
2110 activate_task(lowest_rq, next_task, 0);
2111 resched_curr(lowest_rq);
2114 double_unlock_balance(rq, lowest_rq);
2116 put_task_struct(next_task);
2121 static void push_rt_tasks(struct rq *rq)
2123 /* push_rt_task will return true if it moved an RT */
2124 while (push_rt_task(rq, false))
2128 #ifdef HAVE_RT_PUSH_IPI
2131 * When a high priority task schedules out from a CPU and a lower priority
2132 * task is scheduled in, a check is made to see if there's any RT tasks
2133 * on other CPUs that are waiting to run because a higher priority RT task
2134 * is currently running on its CPU. In this case, the CPU with multiple RT
2135 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
2136 * up that may be able to run one of its non-running queued RT tasks.
2138 * All CPUs with overloaded RT tasks need to be notified as there is currently
2139 * no way to know which of these CPUs have the highest priority task waiting
2140 * to run. Instead of trying to take a spinlock on each of these CPUs,
2141 * which has shown to cause large latency when done on machines with many
2142 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
2143 * RT tasks waiting to run.
2145 * Just sending an IPI to each of the CPUs is also an issue, as on large
2146 * count CPU machines, this can cause an IPI storm on a CPU, especially
2147 * if its the only CPU with multiple RT tasks queued, and a large number
2148 * of CPUs scheduling a lower priority task at the same time.
2150 * Each root domain has its own IRQ work function that can iterate over
2151 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
2152 * task must be checked if there's one or many CPUs that are lowering
2153 * their priority, there's a single IRQ work iterator that will try to
2154 * push off RT tasks that are waiting to run.
2156 * When a CPU schedules a lower priority task, it will kick off the
2157 * IRQ work iterator that will jump to each CPU with overloaded RT tasks.
2158 * As it only takes the first CPU that schedules a lower priority task
2159 * to start the process, the rto_start variable is incremented and if
2160 * the atomic result is one, then that CPU will try to take the rto_lock.
2161 * This prevents high contention on the lock as the process handles all
2162 * CPUs scheduling lower priority tasks.
2164 * All CPUs that are scheduling a lower priority task will increment the
2165 * rt_loop_next variable. This will make sure that the IRQ work iterator
2166 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
2167 * priority task, even if the iterator is in the middle of a scan. Incrementing
2168 * the rt_loop_next will cause the iterator to perform another scan.
2171 static int rto_next_cpu(struct root_domain *rd)
2177 * When starting the IPI RT pushing, the rto_cpu is set to -1,
2178 * rt_next_cpu() will simply return the first CPU found in
2181 * If rto_next_cpu() is called with rto_cpu is a valid CPU, it
2182 * will return the next CPU found in the rto_mask.
2184 * If there are no more CPUs left in the rto_mask, then a check is made
2185 * against rto_loop and rto_loop_next. rto_loop is only updated with
2186 * the rto_lock held, but any CPU may increment the rto_loop_next
2187 * without any locking.
2191 /* When rto_cpu is -1 this acts like cpumask_first() */
2192 cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
2196 if (cpu < nr_cpu_ids)
2202 * ACQUIRE ensures we see the @rto_mask changes
2203 * made prior to the @next value observed.
2205 * Matches WMB in rt_set_overload().
2207 next = atomic_read_acquire(&rd->rto_loop_next);
2209 if (rd->rto_loop == next)
2212 rd->rto_loop = next;
2218 static inline bool rto_start_trylock(atomic_t *v)
2220 return !atomic_cmpxchg_acquire(v, 0, 1);
2223 static inline void rto_start_unlock(atomic_t *v)
2225 atomic_set_release(v, 0);
2228 static void tell_cpu_to_push(struct rq *rq)
2232 /* Keep the loop going if the IPI is currently active */
2233 atomic_inc(&rq->rd->rto_loop_next);
2235 /* Only one CPU can initiate a loop at a time */
2236 if (!rto_start_trylock(&rq->rd->rto_loop_start))
2239 raw_spin_lock(&rq->rd->rto_lock);
2242 * The rto_cpu is updated under the lock, if it has a valid CPU
2243 * then the IPI is still running and will continue due to the
2244 * update to loop_next, and nothing needs to be done here.
2245 * Otherwise it is finishing up and an IPI needs to be sent.
2247 if (rq->rd->rto_cpu < 0)
2248 cpu = rto_next_cpu(rq->rd);
2250 raw_spin_unlock(&rq->rd->rto_lock);
2252 rto_start_unlock(&rq->rd->rto_loop_start);
2255 /* Make sure the rd does not get freed while pushing */
2256 sched_get_rd(rq->rd);
2257 irq_work_queue_on(&rq->rd->rto_push_work, cpu);
2261 /* Called from hardirq context */
2262 void rto_push_irq_work_func(struct irq_work *work)
2264 struct root_domain *rd =
2265 container_of(work, struct root_domain, rto_push_work);
2272 * We do not need to grab the lock to check for has_pushable_tasks.
2273 * When it gets updated, a check is made if a push is possible.
2275 if (has_pushable_tasks(rq)) {
2276 raw_spin_rq_lock(rq);
2277 while (push_rt_task(rq, true))
2279 raw_spin_rq_unlock(rq);
2282 raw_spin_lock(&rd->rto_lock);
2284 /* Pass the IPI to the next rt overloaded queue */
2285 cpu = rto_next_cpu(rd);
2287 raw_spin_unlock(&rd->rto_lock);
2294 /* Try the next RT overloaded CPU */
2295 irq_work_queue_on(&rd->rto_push_work, cpu);
2297 #endif /* HAVE_RT_PUSH_IPI */
2299 static void pull_rt_task(struct rq *this_rq)
2301 int this_cpu = this_rq->cpu, cpu;
2302 bool resched = false;
2303 struct task_struct *p, *push_task;
2305 int rt_overload_count = rt_overloaded(this_rq);
2307 if (likely(!rt_overload_count))
2311 * Match the barrier from rt_set_overloaded; this guarantees that if we
2312 * see overloaded we must also see the rto_mask bit.
2316 /* If we are the only overloaded CPU do nothing */
2317 if (rt_overload_count == 1 &&
2318 cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
2321 #ifdef HAVE_RT_PUSH_IPI
2322 if (sched_feat(RT_PUSH_IPI)) {
2323 tell_cpu_to_push(this_rq);
2328 for_each_cpu(cpu, this_rq->rd->rto_mask) {
2329 if (this_cpu == cpu)
2332 src_rq = cpu_rq(cpu);
2335 * Don't bother taking the src_rq->lock if the next highest
2336 * task is known to be lower-priority than our current task.
2337 * This may look racy, but if this value is about to go
2338 * logically higher, the src_rq will push this task away.
2339 * And if its going logically lower, we do not care
2341 if (src_rq->rt.highest_prio.next >=
2342 this_rq->rt.highest_prio.curr)
2346 * We can potentially drop this_rq's lock in
2347 * double_lock_balance, and another CPU could
2351 double_lock_balance(this_rq, src_rq);
2354 * We can pull only a task, which is pushable
2355 * on its rq, and no others.
2357 p = pick_highest_pushable_task(src_rq, this_cpu);
2360 * Do we have an RT task that preempts
2361 * the to-be-scheduled task?
2363 if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2364 WARN_ON(p == src_rq->curr);
2365 WARN_ON(!task_on_rq_queued(p));
2368 * There's a chance that p is higher in priority
2369 * than what's currently running on its CPU.
2370 * This is just that p is waking up and hasn't
2371 * had a chance to schedule. We only pull
2372 * p if it is lower in priority than the
2373 * current task on the run queue
2375 if (p->prio < src_rq->curr->prio)
2378 if (is_migration_disabled(p)) {
2379 push_task = get_push_task(src_rq);
2381 deactivate_task(src_rq, p, 0);
2382 set_task_cpu(p, this_cpu);
2383 activate_task(this_rq, p, 0);
2387 * We continue with the search, just in
2388 * case there's an even higher prio task
2389 * in another runqueue. (low likelihood
2394 double_unlock_balance(this_rq, src_rq);
2398 raw_spin_rq_unlock(this_rq);
2399 stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2400 push_task, &src_rq->push_work);
2402 raw_spin_rq_lock(this_rq);
2407 resched_curr(this_rq);
2411 * If we are not running and we are not going to reschedule soon, we should
2412 * try to push tasks away now
2414 static void task_woken_rt(struct rq *rq, struct task_struct *p)
2416 bool need_to_push = !task_on_cpu(rq, p) &&
2417 !test_tsk_need_resched(rq->curr) &&
2418 p->nr_cpus_allowed > 1 &&
2419 (dl_task(rq->curr) || rt_task(rq->curr)) &&
2420 (rq->curr->nr_cpus_allowed < 2 ||
2421 rq->curr->prio <= p->prio);
2427 /* Assumes rq->lock is held */
2428 static void rq_online_rt(struct rq *rq)
2430 if (rq->rt.overloaded)
2431 rt_set_overload(rq);
2433 __enable_runtime(rq);
2435 cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
2438 /* Assumes rq->lock is held */
2439 static void rq_offline_rt(struct rq *rq)
2441 if (rq->rt.overloaded)
2442 rt_clear_overload(rq);
2444 __disable_runtime(rq);
2446 cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
2450 * When switch from the rt queue, we bring ourselves to a position
2451 * that we might want to pull RT tasks from other runqueues.
2453 static void switched_from_rt(struct rq *rq, struct task_struct *p)
2456 * If there are other RT tasks then we will reschedule
2457 * and the scheduling of the other RT tasks will handle
2458 * the balancing. But if we are the last RT task
2459 * we may need to handle the pulling of RT tasks
2462 if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
2465 rt_queue_pull_task(rq);
2468 void __init init_sched_rt_class(void)
2472 for_each_possible_cpu(i) {
2473 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
2474 GFP_KERNEL, cpu_to_node(i));
2477 #endif /* CONFIG_SMP */
2480 * When switching a task to RT, we may overload the runqueue
2481 * with RT tasks. In this case we try to push them off to
2484 static void switched_to_rt(struct rq *rq, struct task_struct *p)
2487 * If we are running, update the avg_rt tracking, as the running time
2488 * will now on be accounted into the latter.
2490 if (task_current(rq, p)) {
2491 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2496 * If we are not running we may need to preempt the current
2497 * running task. If that current running task is also an RT task
2498 * then see if we can move to another run queue.
2500 if (task_on_rq_queued(p)) {
2502 if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
2503 rt_queue_push_tasks(rq);
2504 #endif /* CONFIG_SMP */
2505 if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
2511 * Priority of the task has changed. This may cause
2512 * us to initiate a push or pull.
2515 prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
2517 if (!task_on_rq_queued(p))
2520 if (task_current(rq, p)) {
2523 * If our priority decreases while running, we
2524 * may need to pull tasks to this runqueue.
2526 if (oldprio < p->prio)
2527 rt_queue_pull_task(rq);
2530 * If there's a higher priority task waiting to run
2533 if (p->prio > rq->rt.highest_prio.curr)
2536 /* For UP simply resched on drop of prio */
2537 if (oldprio < p->prio)
2539 #endif /* CONFIG_SMP */
2542 * This task is not running, but if it is
2543 * greater than the current running task
2546 if (p->prio < rq->curr->prio)
2551 #ifdef CONFIG_POSIX_TIMERS
2552 static void watchdog(struct rq *rq, struct task_struct *p)
2554 unsigned long soft, hard;
2556 /* max may change after cur was read, this will be fixed next tick */
2557 soft = task_rlimit(p, RLIMIT_RTTIME);
2558 hard = task_rlimit_max(p, RLIMIT_RTTIME);
2560 if (soft != RLIM_INFINITY) {
2563 if (p->rt.watchdog_stamp != jiffies) {
2565 p->rt.watchdog_stamp = jiffies;
2568 next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
2569 if (p->rt.timeout > next) {
2570 posix_cputimers_rt_watchdog(&p->posix_cputimers,
2571 p->se.sum_exec_runtime);
2576 static inline void watchdog(struct rq *rq, struct task_struct *p) { }
2580 * scheduler tick hitting a task of our scheduling class.
2582 * NOTE: This function can be called remotely by the tick offload that
2583 * goes along full dynticks. Therefore no local assumption can be made
2584 * and everything must be accessed through the @rq and @curr passed in
2587 static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
2589 struct sched_rt_entity *rt_se = &p->rt;
2592 update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2597 * RR tasks need a special form of time-slice management.
2598 * FIFO tasks have no timeslices.
2600 if (p->policy != SCHED_RR)
2603 if (--p->rt.time_slice)
2606 p->rt.time_slice = sched_rr_timeslice;
2609 * Requeue to the end of queue if we (and all of our ancestors) are not
2610 * the only element on the queue
2612 for_each_sched_rt_entity(rt_se) {
2613 if (rt_se->run_list.prev != rt_se->run_list.next) {
2614 requeue_task_rt(rq, p, 0);
2621 static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
2624 * Time slice is 0 for SCHED_FIFO tasks
2626 if (task->policy == SCHED_RR)
2627 return sched_rr_timeslice;
2632 #ifdef CONFIG_SCHED_CORE
2633 static int task_is_throttled_rt(struct task_struct *p, int cpu)
2635 struct rt_rq *rt_rq;
2637 #ifdef CONFIG_RT_GROUP_SCHED
2638 rt_rq = task_group(p)->rt_rq[cpu];
2640 rt_rq = &cpu_rq(cpu)->rt;
2643 return rt_rq_throttled(rt_rq);
2647 DEFINE_SCHED_CLASS(rt) = {
2649 .enqueue_task = enqueue_task_rt,
2650 .dequeue_task = dequeue_task_rt,
2651 .yield_task = yield_task_rt,
2653 .wakeup_preempt = wakeup_preempt_rt,
2655 .pick_next_task = pick_next_task_rt,
2656 .put_prev_task = put_prev_task_rt,
2657 .set_next_task = set_next_task_rt,
2660 .balance = balance_rt,
2661 .pick_task = pick_task_rt,
2662 .select_task_rq = select_task_rq_rt,
2663 .set_cpus_allowed = set_cpus_allowed_common,
2664 .rq_online = rq_online_rt,
2665 .rq_offline = rq_offline_rt,
2666 .task_woken = task_woken_rt,
2667 .switched_from = switched_from_rt,
2668 .find_lock_rq = find_lock_lowest_rq,
2671 .task_tick = task_tick_rt,
2673 .get_rr_interval = get_rr_interval_rt,
2675 .prio_changed = prio_changed_rt,
2676 .switched_to = switched_to_rt,
2678 .update_curr = update_curr_rt,
2680 #ifdef CONFIG_SCHED_CORE
2681 .task_is_throttled = task_is_throttled_rt,
2684 #ifdef CONFIG_UCLAMP_TASK
2685 .uclamp_enabled = 1,
2689 #ifdef CONFIG_RT_GROUP_SCHED
2691 * Ensure that the real time constraints are schedulable.
2693 static DEFINE_MUTEX(rt_constraints_mutex);
2695 static inline int tg_has_rt_tasks(struct task_group *tg)
2697 struct task_struct *task;
2698 struct css_task_iter it;
2702 * Autogroups do not have RT tasks; see autogroup_create().
2704 if (task_group_is_autogroup(tg))
2707 css_task_iter_start(&tg->css, 0, &it);
2708 while (!ret && (task = css_task_iter_next(&it)))
2709 ret |= rt_task(task);
2710 css_task_iter_end(&it);
2715 struct rt_schedulable_data {
2716 struct task_group *tg;
2721 static int tg_rt_schedulable(struct task_group *tg, void *data)
2723 struct rt_schedulable_data *d = data;
2724 struct task_group *child;
2725 unsigned long total, sum = 0;
2726 u64 period, runtime;
2728 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2729 runtime = tg->rt_bandwidth.rt_runtime;
2732 period = d->rt_period;
2733 runtime = d->rt_runtime;
2737 * Cannot have more runtime than the period.
2739 if (runtime > period && runtime != RUNTIME_INF)
2743 * Ensure we don't starve existing RT tasks if runtime turns zero.
2745 if (rt_bandwidth_enabled() && !runtime &&
2746 tg->rt_bandwidth.rt_runtime && tg_has_rt_tasks(tg))
2749 total = to_ratio(period, runtime);
2752 * Nobody can have more than the global setting allows.
2754 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
2758 * The sum of our children's runtime should not exceed our own.
2760 list_for_each_entry_rcu(child, &tg->children, siblings) {
2761 period = ktime_to_ns(child->rt_bandwidth.rt_period);
2762 runtime = child->rt_bandwidth.rt_runtime;
2764 if (child == d->tg) {
2765 period = d->rt_period;
2766 runtime = d->rt_runtime;
2769 sum += to_ratio(period, runtime);
2778 static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
2782 struct rt_schedulable_data data = {
2784 .rt_period = period,
2785 .rt_runtime = runtime,
2789 ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
2795 static int tg_set_rt_bandwidth(struct task_group *tg,
2796 u64 rt_period, u64 rt_runtime)
2801 * Disallowing the root group RT runtime is BAD, it would disallow the
2802 * kernel creating (and or operating) RT threads.
2804 if (tg == &root_task_group && rt_runtime == 0)
2807 /* No period doesn't make any sense. */
2812 * Bound quota to defend quota against overflow during bandwidth shift.
2814 if (rt_runtime != RUNTIME_INF && rt_runtime > max_rt_runtime)
2817 mutex_lock(&rt_constraints_mutex);
2818 err = __rt_schedulable(tg, rt_period, rt_runtime);
2822 raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2823 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
2824 tg->rt_bandwidth.rt_runtime = rt_runtime;
2826 for_each_possible_cpu(i) {
2827 struct rt_rq *rt_rq = tg->rt_rq[i];
2829 raw_spin_lock(&rt_rq->rt_runtime_lock);
2830 rt_rq->rt_runtime = rt_runtime;
2831 raw_spin_unlock(&rt_rq->rt_runtime_lock);
2833 raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2835 mutex_unlock(&rt_constraints_mutex);
2840 int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
2842 u64 rt_runtime, rt_period;
2844 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2845 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
2846 if (rt_runtime_us < 0)
2847 rt_runtime = RUNTIME_INF;
2848 else if ((u64)rt_runtime_us > U64_MAX / NSEC_PER_USEC)
2851 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2854 long sched_group_rt_runtime(struct task_group *tg)
2858 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
2861 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
2862 do_div(rt_runtime_us, NSEC_PER_USEC);
2863 return rt_runtime_us;
2866 int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
2868 u64 rt_runtime, rt_period;
2870 if (rt_period_us > U64_MAX / NSEC_PER_USEC)
2873 rt_period = rt_period_us * NSEC_PER_USEC;
2874 rt_runtime = tg->rt_bandwidth.rt_runtime;
2876 return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2879 long sched_group_rt_period(struct task_group *tg)
2883 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
2884 do_div(rt_period_us, NSEC_PER_USEC);
2885 return rt_period_us;
2888 #ifdef CONFIG_SYSCTL
2889 static int sched_rt_global_constraints(void)
2893 mutex_lock(&rt_constraints_mutex);
2894 ret = __rt_schedulable(NULL, 0, 0);
2895 mutex_unlock(&rt_constraints_mutex);
2899 #endif /* CONFIG_SYSCTL */
2901 int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
2903 /* Don't accept real-time tasks when there is no way for them to run */
2904 if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
2910 #else /* !CONFIG_RT_GROUP_SCHED */
2912 #ifdef CONFIG_SYSCTL
2913 static int sched_rt_global_constraints(void)
2915 unsigned long flags;
2918 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
2919 for_each_possible_cpu(i) {
2920 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
2922 raw_spin_lock(&rt_rq->rt_runtime_lock);
2923 rt_rq->rt_runtime = global_rt_runtime();
2924 raw_spin_unlock(&rt_rq->rt_runtime_lock);
2926 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
2930 #endif /* CONFIG_SYSCTL */
2931 #endif /* CONFIG_RT_GROUP_SCHED */
2933 #ifdef CONFIG_SYSCTL
2934 static int sched_rt_global_validate(void)
2936 if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
2937 ((sysctl_sched_rt_runtime > sysctl_sched_rt_period) ||
2938 ((u64)sysctl_sched_rt_runtime *
2939 NSEC_PER_USEC > max_rt_runtime)))
2945 static void sched_rt_do_global(void)
2947 unsigned long flags;
2949 raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
2950 def_rt_bandwidth.rt_runtime = global_rt_runtime();
2951 def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
2952 raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
2955 static int sched_rt_handler(const struct ctl_table *table, int write, void *buffer,
2956 size_t *lenp, loff_t *ppos)
2958 int old_period, old_runtime;
2959 static DEFINE_MUTEX(mutex);
2963 old_period = sysctl_sched_rt_period;
2964 old_runtime = sysctl_sched_rt_runtime;
2966 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2968 if (!ret && write) {
2969 ret = sched_rt_global_validate();
2973 ret = sched_dl_global_validate();
2977 ret = sched_rt_global_constraints();
2981 sched_rt_do_global();
2982 sched_dl_do_global();
2986 sysctl_sched_rt_period = old_period;
2987 sysctl_sched_rt_runtime = old_runtime;
2989 mutex_unlock(&mutex);
2994 static int sched_rr_handler(const struct ctl_table *table, int write, void *buffer,
2995 size_t *lenp, loff_t *ppos)
2998 static DEFINE_MUTEX(mutex);
3001 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3003 * Make sure that internally we keep jiffies.
3004 * Also, writing zero resets the time-slice to default:
3006 if (!ret && write) {
3007 sched_rr_timeslice =
3008 sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
3009 msecs_to_jiffies(sysctl_sched_rr_timeslice);
3011 if (sysctl_sched_rr_timeslice <= 0)
3012 sysctl_sched_rr_timeslice = jiffies_to_msecs(RR_TIMESLICE);
3014 mutex_unlock(&mutex);
3018 #endif /* CONFIG_SYSCTL */
3020 #ifdef CONFIG_SCHED_DEBUG
3021 void print_rt_stats(struct seq_file *m, int cpu)
3024 struct rt_rq *rt_rq;
3027 for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
3028 print_rt_rq(m, cpu, rt_rq);
3031 #endif /* CONFIG_SCHED_DEBUG */