xref: /openbmc/linux/kernel/sched/rt.c (revision a7c81556ec4d341dfdbf2cc478ead89d73e474a7)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2391e43daSPeter Zijlstra /*
3391e43daSPeter Zijlstra  * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
4391e43daSPeter Zijlstra  * policies)
5391e43daSPeter Zijlstra  */
6391e43daSPeter Zijlstra #include "sched.h"
7391e43daSPeter Zijlstra 
8371bf427SVincent Guittot #include "pelt.h"
9371bf427SVincent Guittot 
10ce0dbbbbSClark Williams int sched_rr_timeslice = RR_TIMESLICE;
11975e155eSShile Zhang int sysctl_sched_rr_timeslice = (MSEC_PER_SEC / HZ) * RR_TIMESLICE;
12d505b8afSHuaixin Chang /* More than 4 hours if BW_SHIFT equals 20. */
13d505b8afSHuaixin Chang static const u64 max_rt_runtime = MAX_BW;
14ce0dbbbbSClark Williams 
15391e43daSPeter Zijlstra static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
16391e43daSPeter Zijlstra 
17391e43daSPeter Zijlstra struct rt_bandwidth def_rt_bandwidth;
18391e43daSPeter Zijlstra 
19391e43daSPeter Zijlstra static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
20391e43daSPeter Zijlstra {
21391e43daSPeter Zijlstra 	struct rt_bandwidth *rt_b =
22391e43daSPeter Zijlstra 		container_of(timer, struct rt_bandwidth, rt_period_timer);
23391e43daSPeter Zijlstra 	int idle = 0;
2477a4d1a1SPeter Zijlstra 	int overrun;
25391e43daSPeter Zijlstra 
2677a4d1a1SPeter Zijlstra 	raw_spin_lock(&rt_b->rt_runtime_lock);
27391e43daSPeter Zijlstra 	for (;;) {
2877a4d1a1SPeter Zijlstra 		overrun = hrtimer_forward_now(timer, rt_b->rt_period);
29391e43daSPeter Zijlstra 		if (!overrun)
30391e43daSPeter Zijlstra 			break;
31391e43daSPeter Zijlstra 
3277a4d1a1SPeter Zijlstra 		raw_spin_unlock(&rt_b->rt_runtime_lock);
33391e43daSPeter Zijlstra 		idle = do_sched_rt_period_timer(rt_b, overrun);
3477a4d1a1SPeter Zijlstra 		raw_spin_lock(&rt_b->rt_runtime_lock);
35391e43daSPeter Zijlstra 	}
364cfafd30SPeter Zijlstra 	if (idle)
374cfafd30SPeter Zijlstra 		rt_b->rt_period_active = 0;
3877a4d1a1SPeter Zijlstra 	raw_spin_unlock(&rt_b->rt_runtime_lock);
39391e43daSPeter Zijlstra 
40391e43daSPeter Zijlstra 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
41391e43daSPeter Zijlstra }
42391e43daSPeter Zijlstra 
43391e43daSPeter Zijlstra void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
44391e43daSPeter Zijlstra {
45391e43daSPeter Zijlstra 	rt_b->rt_period = ns_to_ktime(period);
46391e43daSPeter Zijlstra 	rt_b->rt_runtime = runtime;
47391e43daSPeter Zijlstra 
48391e43daSPeter Zijlstra 	raw_spin_lock_init(&rt_b->rt_runtime_lock);
49391e43daSPeter Zijlstra 
50d5096aa6SSebastian Andrzej Siewior 	hrtimer_init(&rt_b->rt_period_timer, CLOCK_MONOTONIC,
51d5096aa6SSebastian Andrzej Siewior 		     HRTIMER_MODE_REL_HARD);
52391e43daSPeter Zijlstra 	rt_b->rt_period_timer.function = sched_rt_period_timer;
53391e43daSPeter Zijlstra }
54391e43daSPeter Zijlstra 
55391e43daSPeter Zijlstra static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
56391e43daSPeter Zijlstra {
57391e43daSPeter Zijlstra 	if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
58391e43daSPeter Zijlstra 		return;
59391e43daSPeter Zijlstra 
60391e43daSPeter Zijlstra 	raw_spin_lock(&rt_b->rt_runtime_lock);
614cfafd30SPeter Zijlstra 	if (!rt_b->rt_period_active) {
624cfafd30SPeter Zijlstra 		rt_b->rt_period_active = 1;
63c3a990dcSSteven Rostedt 		/*
64c3a990dcSSteven Rostedt 		 * SCHED_DEADLINE updates the bandwidth, as a run away
65c3a990dcSSteven Rostedt 		 * RT task with a DL task could hog a CPU. But DL does
66c3a990dcSSteven Rostedt 		 * not reset the period. If a deadline task was running
67c3a990dcSSteven Rostedt 		 * without an RT task running, it can cause RT tasks to
68c3a990dcSSteven Rostedt 		 * throttle when they start up. Kick the timer right away
69c3a990dcSSteven Rostedt 		 * to update the period.
70c3a990dcSSteven Rostedt 		 */
71c3a990dcSSteven Rostedt 		hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
72d5096aa6SSebastian Andrzej Siewior 		hrtimer_start_expires(&rt_b->rt_period_timer,
73d5096aa6SSebastian Andrzej Siewior 				      HRTIMER_MODE_ABS_PINNED_HARD);
744cfafd30SPeter Zijlstra 	}
75391e43daSPeter Zijlstra 	raw_spin_unlock(&rt_b->rt_runtime_lock);
76391e43daSPeter Zijlstra }
77391e43daSPeter Zijlstra 
7807c54f7aSAbel Vesa void init_rt_rq(struct rt_rq *rt_rq)
79391e43daSPeter Zijlstra {
80391e43daSPeter Zijlstra 	struct rt_prio_array *array;
81391e43daSPeter Zijlstra 	int i;
82391e43daSPeter Zijlstra 
83391e43daSPeter Zijlstra 	array = &rt_rq->active;
84391e43daSPeter Zijlstra 	for (i = 0; i < MAX_RT_PRIO; i++) {
85391e43daSPeter Zijlstra 		INIT_LIST_HEAD(array->queue + i);
86391e43daSPeter Zijlstra 		__clear_bit(i, array->bitmap);
87391e43daSPeter Zijlstra 	}
88391e43daSPeter Zijlstra 	/* delimiter for bitsearch: */
89391e43daSPeter Zijlstra 	__set_bit(MAX_RT_PRIO, array->bitmap);
90391e43daSPeter Zijlstra 
91391e43daSPeter Zijlstra #if defined CONFIG_SMP
92391e43daSPeter Zijlstra 	rt_rq->highest_prio.curr = MAX_RT_PRIO;
93391e43daSPeter Zijlstra 	rt_rq->highest_prio.next = MAX_RT_PRIO;
94391e43daSPeter Zijlstra 	rt_rq->rt_nr_migratory = 0;
95391e43daSPeter Zijlstra 	rt_rq->overloaded = 0;
96391e43daSPeter Zijlstra 	plist_head_init(&rt_rq->pushable_tasks);
97b6366f04SSteven Rostedt #endif /* CONFIG_SMP */
98f4ebcbc0SKirill Tkhai 	/* We start is dequeued state, because no RT tasks are queued */
99f4ebcbc0SKirill Tkhai 	rt_rq->rt_queued = 0;
100391e43daSPeter Zijlstra 
101391e43daSPeter Zijlstra 	rt_rq->rt_time = 0;
102391e43daSPeter Zijlstra 	rt_rq->rt_throttled = 0;
103391e43daSPeter Zijlstra 	rt_rq->rt_runtime = 0;
104391e43daSPeter Zijlstra 	raw_spin_lock_init(&rt_rq->rt_runtime_lock);
105391e43daSPeter Zijlstra }
106391e43daSPeter Zijlstra 
107391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
108391e43daSPeter Zijlstra static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
109391e43daSPeter Zijlstra {
110391e43daSPeter Zijlstra 	hrtimer_cancel(&rt_b->rt_period_timer);
111391e43daSPeter Zijlstra }
112391e43daSPeter Zijlstra 
113391e43daSPeter Zijlstra #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
114391e43daSPeter Zijlstra 
115391e43daSPeter Zijlstra static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
116391e43daSPeter Zijlstra {
117391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG
118391e43daSPeter Zijlstra 	WARN_ON_ONCE(!rt_entity_is_task(rt_se));
119391e43daSPeter Zijlstra #endif
120391e43daSPeter Zijlstra 	return container_of(rt_se, struct task_struct, rt);
121391e43daSPeter Zijlstra }
122391e43daSPeter Zijlstra 
123391e43daSPeter Zijlstra static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
124391e43daSPeter Zijlstra {
125391e43daSPeter Zijlstra 	return rt_rq->rq;
126391e43daSPeter Zijlstra }
127391e43daSPeter Zijlstra 
128391e43daSPeter Zijlstra static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
129391e43daSPeter Zijlstra {
130391e43daSPeter Zijlstra 	return rt_se->rt_rq;
131391e43daSPeter Zijlstra }
132391e43daSPeter Zijlstra 
133653d07a6SKirill Tkhai static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
134653d07a6SKirill Tkhai {
135653d07a6SKirill Tkhai 	struct rt_rq *rt_rq = rt_se->rt_rq;
136653d07a6SKirill Tkhai 
137653d07a6SKirill Tkhai 	return rt_rq->rq;
138653d07a6SKirill Tkhai }
139653d07a6SKirill Tkhai 
140391e43daSPeter Zijlstra void free_rt_sched_group(struct task_group *tg)
141391e43daSPeter Zijlstra {
142391e43daSPeter Zijlstra 	int i;
143391e43daSPeter Zijlstra 
144391e43daSPeter Zijlstra 	if (tg->rt_se)
145391e43daSPeter Zijlstra 		destroy_rt_bandwidth(&tg->rt_bandwidth);
146391e43daSPeter Zijlstra 
147391e43daSPeter Zijlstra 	for_each_possible_cpu(i) {
148391e43daSPeter Zijlstra 		if (tg->rt_rq)
149391e43daSPeter Zijlstra 			kfree(tg->rt_rq[i]);
150391e43daSPeter Zijlstra 		if (tg->rt_se)
151391e43daSPeter Zijlstra 			kfree(tg->rt_se[i]);
152391e43daSPeter Zijlstra 	}
153391e43daSPeter Zijlstra 
154391e43daSPeter Zijlstra 	kfree(tg->rt_rq);
155391e43daSPeter Zijlstra 	kfree(tg->rt_se);
156391e43daSPeter Zijlstra }
157391e43daSPeter Zijlstra 
158391e43daSPeter Zijlstra void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
159391e43daSPeter Zijlstra 		struct sched_rt_entity *rt_se, int cpu,
160391e43daSPeter Zijlstra 		struct sched_rt_entity *parent)
161391e43daSPeter Zijlstra {
162391e43daSPeter Zijlstra 	struct rq *rq = cpu_rq(cpu);
163391e43daSPeter Zijlstra 
164391e43daSPeter Zijlstra 	rt_rq->highest_prio.curr = MAX_RT_PRIO;
165391e43daSPeter Zijlstra 	rt_rq->rt_nr_boosted = 0;
166391e43daSPeter Zijlstra 	rt_rq->rq = rq;
167391e43daSPeter Zijlstra 	rt_rq->tg = tg;
168391e43daSPeter Zijlstra 
169391e43daSPeter Zijlstra 	tg->rt_rq[cpu] = rt_rq;
170391e43daSPeter Zijlstra 	tg->rt_se[cpu] = rt_se;
171391e43daSPeter Zijlstra 
172391e43daSPeter Zijlstra 	if (!rt_se)
173391e43daSPeter Zijlstra 		return;
174391e43daSPeter Zijlstra 
175391e43daSPeter Zijlstra 	if (!parent)
176391e43daSPeter Zijlstra 		rt_se->rt_rq = &rq->rt;
177391e43daSPeter Zijlstra 	else
178391e43daSPeter Zijlstra 		rt_se->rt_rq = parent->my_q;
179391e43daSPeter Zijlstra 
180391e43daSPeter Zijlstra 	rt_se->my_q = rt_rq;
181391e43daSPeter Zijlstra 	rt_se->parent = parent;
182391e43daSPeter Zijlstra 	INIT_LIST_HEAD(&rt_se->run_list);
183391e43daSPeter Zijlstra }
184391e43daSPeter Zijlstra 
185391e43daSPeter Zijlstra int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
186391e43daSPeter Zijlstra {
187391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
188391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se;
189391e43daSPeter Zijlstra 	int i;
190391e43daSPeter Zijlstra 
1916396bb22SKees Cook 	tg->rt_rq = kcalloc(nr_cpu_ids, sizeof(rt_rq), GFP_KERNEL);
192391e43daSPeter Zijlstra 	if (!tg->rt_rq)
193391e43daSPeter Zijlstra 		goto err;
1946396bb22SKees Cook 	tg->rt_se = kcalloc(nr_cpu_ids, sizeof(rt_se), GFP_KERNEL);
195391e43daSPeter Zijlstra 	if (!tg->rt_se)
196391e43daSPeter Zijlstra 		goto err;
197391e43daSPeter Zijlstra 
198391e43daSPeter Zijlstra 	init_rt_bandwidth(&tg->rt_bandwidth,
199391e43daSPeter Zijlstra 			ktime_to_ns(def_rt_bandwidth.rt_period), 0);
200391e43daSPeter Zijlstra 
201391e43daSPeter Zijlstra 	for_each_possible_cpu(i) {
202391e43daSPeter Zijlstra 		rt_rq = kzalloc_node(sizeof(struct rt_rq),
203391e43daSPeter Zijlstra 				     GFP_KERNEL, cpu_to_node(i));
204391e43daSPeter Zijlstra 		if (!rt_rq)
205391e43daSPeter Zijlstra 			goto err;
206391e43daSPeter Zijlstra 
207391e43daSPeter Zijlstra 		rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
208391e43daSPeter Zijlstra 				     GFP_KERNEL, cpu_to_node(i));
209391e43daSPeter Zijlstra 		if (!rt_se)
210391e43daSPeter Zijlstra 			goto err_free_rq;
211391e43daSPeter Zijlstra 
21207c54f7aSAbel Vesa 		init_rt_rq(rt_rq);
213391e43daSPeter Zijlstra 		rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
214391e43daSPeter Zijlstra 		init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
215391e43daSPeter Zijlstra 	}
216391e43daSPeter Zijlstra 
217391e43daSPeter Zijlstra 	return 1;
218391e43daSPeter Zijlstra 
219391e43daSPeter Zijlstra err_free_rq:
220391e43daSPeter Zijlstra 	kfree(rt_rq);
221391e43daSPeter Zijlstra err:
222391e43daSPeter Zijlstra 	return 0;
223391e43daSPeter Zijlstra }
224391e43daSPeter Zijlstra 
225391e43daSPeter Zijlstra #else /* CONFIG_RT_GROUP_SCHED */
226391e43daSPeter Zijlstra 
227391e43daSPeter Zijlstra #define rt_entity_is_task(rt_se) (1)
228391e43daSPeter Zijlstra 
229391e43daSPeter Zijlstra static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
230391e43daSPeter Zijlstra {
231391e43daSPeter Zijlstra 	return container_of(rt_se, struct task_struct, rt);
232391e43daSPeter Zijlstra }
233391e43daSPeter Zijlstra 
234391e43daSPeter Zijlstra static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
235391e43daSPeter Zijlstra {
236391e43daSPeter Zijlstra 	return container_of(rt_rq, struct rq, rt);
237391e43daSPeter Zijlstra }
238391e43daSPeter Zijlstra 
239653d07a6SKirill Tkhai static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
240391e43daSPeter Zijlstra {
241391e43daSPeter Zijlstra 	struct task_struct *p = rt_task_of(rt_se);
242653d07a6SKirill Tkhai 
243653d07a6SKirill Tkhai 	return task_rq(p);
244653d07a6SKirill Tkhai }
245653d07a6SKirill Tkhai 
246653d07a6SKirill Tkhai static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
247653d07a6SKirill Tkhai {
248653d07a6SKirill Tkhai 	struct rq *rq = rq_of_rt_se(rt_se);
249391e43daSPeter Zijlstra 
250391e43daSPeter Zijlstra 	return &rq->rt;
251391e43daSPeter Zijlstra }
252391e43daSPeter Zijlstra 
253391e43daSPeter Zijlstra void free_rt_sched_group(struct task_group *tg) { }
254391e43daSPeter Zijlstra 
255391e43daSPeter Zijlstra int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
256391e43daSPeter Zijlstra {
257391e43daSPeter Zijlstra 	return 1;
258391e43daSPeter Zijlstra }
259391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */
260391e43daSPeter Zijlstra 
261391e43daSPeter Zijlstra #ifdef CONFIG_SMP
262391e43daSPeter Zijlstra 
2638046d680SPeter Zijlstra static void pull_rt_task(struct rq *this_rq);
26438033c37SPeter Zijlstra 
265dc877341SPeter Zijlstra static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
266dc877341SPeter Zijlstra {
267dc877341SPeter Zijlstra 	/* Try to pull RT tasks here if we lower this rq's prio */
268120455c5SPeter Zijlstra 	return rq->online && rq->rt.highest_prio.curr > prev->prio;
269dc877341SPeter Zijlstra }
270dc877341SPeter Zijlstra 
271391e43daSPeter Zijlstra static inline int rt_overloaded(struct rq *rq)
272391e43daSPeter Zijlstra {
273391e43daSPeter Zijlstra 	return atomic_read(&rq->rd->rto_count);
274391e43daSPeter Zijlstra }
275391e43daSPeter Zijlstra 
276391e43daSPeter Zijlstra static inline void rt_set_overload(struct rq *rq)
277391e43daSPeter Zijlstra {
278391e43daSPeter Zijlstra 	if (!rq->online)
279391e43daSPeter Zijlstra 		return;
280391e43daSPeter Zijlstra 
281391e43daSPeter Zijlstra 	cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
282391e43daSPeter Zijlstra 	/*
283391e43daSPeter Zijlstra 	 * Make sure the mask is visible before we set
284391e43daSPeter Zijlstra 	 * the overload count. That is checked to determine
285391e43daSPeter Zijlstra 	 * if we should look at the mask. It would be a shame
286391e43daSPeter Zijlstra 	 * if we looked at the mask, but the mask was not
287391e43daSPeter Zijlstra 	 * updated yet.
2887c3f2ab7SPeter Zijlstra 	 *
2897c3f2ab7SPeter Zijlstra 	 * Matched by the barrier in pull_rt_task().
290391e43daSPeter Zijlstra 	 */
2917c3f2ab7SPeter Zijlstra 	smp_wmb();
292391e43daSPeter Zijlstra 	atomic_inc(&rq->rd->rto_count);
293391e43daSPeter Zijlstra }
294391e43daSPeter Zijlstra 
295391e43daSPeter Zijlstra static inline void rt_clear_overload(struct rq *rq)
296391e43daSPeter Zijlstra {
297391e43daSPeter Zijlstra 	if (!rq->online)
298391e43daSPeter Zijlstra 		return;
299391e43daSPeter Zijlstra 
300391e43daSPeter Zijlstra 	/* the order here really doesn't matter */
301391e43daSPeter Zijlstra 	atomic_dec(&rq->rd->rto_count);
302391e43daSPeter Zijlstra 	cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
303391e43daSPeter Zijlstra }
304391e43daSPeter Zijlstra 
305391e43daSPeter Zijlstra static void update_rt_migration(struct rt_rq *rt_rq)
306391e43daSPeter Zijlstra {
307391e43daSPeter Zijlstra 	if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
308391e43daSPeter Zijlstra 		if (!rt_rq->overloaded) {
309391e43daSPeter Zijlstra 			rt_set_overload(rq_of_rt_rq(rt_rq));
310391e43daSPeter Zijlstra 			rt_rq->overloaded = 1;
311391e43daSPeter Zijlstra 		}
312391e43daSPeter Zijlstra 	} else if (rt_rq->overloaded) {
313391e43daSPeter Zijlstra 		rt_clear_overload(rq_of_rt_rq(rt_rq));
314391e43daSPeter Zijlstra 		rt_rq->overloaded = 0;
315391e43daSPeter Zijlstra 	}
316391e43daSPeter Zijlstra }
317391e43daSPeter Zijlstra 
318391e43daSPeter Zijlstra static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
319391e43daSPeter Zijlstra {
32029baa747SPeter Zijlstra 	struct task_struct *p;
32129baa747SPeter Zijlstra 
322391e43daSPeter Zijlstra 	if (!rt_entity_is_task(rt_se))
323391e43daSPeter Zijlstra 		return;
324391e43daSPeter Zijlstra 
32529baa747SPeter Zijlstra 	p = rt_task_of(rt_se);
326391e43daSPeter Zijlstra 	rt_rq = &rq_of_rt_rq(rt_rq)->rt;
327391e43daSPeter Zijlstra 
328391e43daSPeter Zijlstra 	rt_rq->rt_nr_total++;
3294b53a341SIngo Molnar 	if (p->nr_cpus_allowed > 1)
330391e43daSPeter Zijlstra 		rt_rq->rt_nr_migratory++;
331391e43daSPeter Zijlstra 
332391e43daSPeter Zijlstra 	update_rt_migration(rt_rq);
333391e43daSPeter Zijlstra }
334391e43daSPeter Zijlstra 
335391e43daSPeter Zijlstra static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
336391e43daSPeter Zijlstra {
33729baa747SPeter Zijlstra 	struct task_struct *p;
33829baa747SPeter Zijlstra 
339391e43daSPeter Zijlstra 	if (!rt_entity_is_task(rt_se))
340391e43daSPeter Zijlstra 		return;
341391e43daSPeter Zijlstra 
34229baa747SPeter Zijlstra 	p = rt_task_of(rt_se);
343391e43daSPeter Zijlstra 	rt_rq = &rq_of_rt_rq(rt_rq)->rt;
344391e43daSPeter Zijlstra 
345391e43daSPeter Zijlstra 	rt_rq->rt_nr_total--;
3464b53a341SIngo Molnar 	if (p->nr_cpus_allowed > 1)
347391e43daSPeter Zijlstra 		rt_rq->rt_nr_migratory--;
348391e43daSPeter Zijlstra 
349391e43daSPeter Zijlstra 	update_rt_migration(rt_rq);
350391e43daSPeter Zijlstra }
351391e43daSPeter Zijlstra 
352391e43daSPeter Zijlstra static inline int has_pushable_tasks(struct rq *rq)
353391e43daSPeter Zijlstra {
354391e43daSPeter Zijlstra 	return !plist_head_empty(&rq->rt.pushable_tasks);
355391e43daSPeter Zijlstra }
356391e43daSPeter Zijlstra 
357fd7a4bedSPeter Zijlstra static DEFINE_PER_CPU(struct callback_head, rt_push_head);
358fd7a4bedSPeter Zijlstra static DEFINE_PER_CPU(struct callback_head, rt_pull_head);
359e3fca9e7SPeter Zijlstra 
360e3fca9e7SPeter Zijlstra static void push_rt_tasks(struct rq *);
361fd7a4bedSPeter Zijlstra static void pull_rt_task(struct rq *);
362e3fca9e7SPeter Zijlstra 
36302d8ec94SIngo Molnar static inline void rt_queue_push_tasks(struct rq *rq)
364dc877341SPeter Zijlstra {
365e3fca9e7SPeter Zijlstra 	if (!has_pushable_tasks(rq))
366e3fca9e7SPeter Zijlstra 		return;
367e3fca9e7SPeter Zijlstra 
368fd7a4bedSPeter Zijlstra 	queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
369fd7a4bedSPeter Zijlstra }
370fd7a4bedSPeter Zijlstra 
37102d8ec94SIngo Molnar static inline void rt_queue_pull_task(struct rq *rq)
372fd7a4bedSPeter Zijlstra {
373fd7a4bedSPeter Zijlstra 	queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
374dc877341SPeter Zijlstra }
375dc877341SPeter Zijlstra 
376391e43daSPeter Zijlstra static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
377391e43daSPeter Zijlstra {
378391e43daSPeter Zijlstra 	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
379391e43daSPeter Zijlstra 	plist_node_init(&p->pushable_tasks, p->prio);
380391e43daSPeter Zijlstra 	plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
381391e43daSPeter Zijlstra 
382391e43daSPeter Zijlstra 	/* Update the highest prio pushable task */
383391e43daSPeter Zijlstra 	if (p->prio < rq->rt.highest_prio.next)
384391e43daSPeter Zijlstra 		rq->rt.highest_prio.next = p->prio;
385391e43daSPeter Zijlstra }
386391e43daSPeter Zijlstra 
387391e43daSPeter Zijlstra static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
388391e43daSPeter Zijlstra {
389391e43daSPeter Zijlstra 	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
390391e43daSPeter Zijlstra 
391391e43daSPeter Zijlstra 	/* Update the new highest prio pushable task */
392391e43daSPeter Zijlstra 	if (has_pushable_tasks(rq)) {
393391e43daSPeter Zijlstra 		p = plist_first_entry(&rq->rt.pushable_tasks,
394391e43daSPeter Zijlstra 				      struct task_struct, pushable_tasks);
395391e43daSPeter Zijlstra 		rq->rt.highest_prio.next = p->prio;
396391e43daSPeter Zijlstra 	} else
397391e43daSPeter Zijlstra 		rq->rt.highest_prio.next = MAX_RT_PRIO;
398391e43daSPeter Zijlstra }
399391e43daSPeter Zijlstra 
400391e43daSPeter Zijlstra #else
401391e43daSPeter Zijlstra 
402391e43daSPeter Zijlstra static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
403391e43daSPeter Zijlstra {
404391e43daSPeter Zijlstra }
405391e43daSPeter Zijlstra 
406391e43daSPeter Zijlstra static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
407391e43daSPeter Zijlstra {
408391e43daSPeter Zijlstra }
409391e43daSPeter Zijlstra 
410391e43daSPeter Zijlstra static inline
411391e43daSPeter Zijlstra void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
412391e43daSPeter Zijlstra {
413391e43daSPeter Zijlstra }
414391e43daSPeter Zijlstra 
415391e43daSPeter Zijlstra static inline
416391e43daSPeter Zijlstra void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
417391e43daSPeter Zijlstra {
418391e43daSPeter Zijlstra }
419391e43daSPeter Zijlstra 
420dc877341SPeter Zijlstra static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
421dc877341SPeter Zijlstra {
422dc877341SPeter Zijlstra 	return false;
423dc877341SPeter Zijlstra }
424dc877341SPeter Zijlstra 
4258046d680SPeter Zijlstra static inline void pull_rt_task(struct rq *this_rq)
426dc877341SPeter Zijlstra {
427dc877341SPeter Zijlstra }
428dc877341SPeter Zijlstra 
42902d8ec94SIngo Molnar static inline void rt_queue_push_tasks(struct rq *rq)
430dc877341SPeter Zijlstra {
431dc877341SPeter Zijlstra }
432391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
433391e43daSPeter Zijlstra 
434f4ebcbc0SKirill Tkhai static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
435f4ebcbc0SKirill Tkhai static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
436f4ebcbc0SKirill Tkhai 
437391e43daSPeter Zijlstra static inline int on_rt_rq(struct sched_rt_entity *rt_se)
438391e43daSPeter Zijlstra {
439ff77e468SPeter Zijlstra 	return rt_se->on_rq;
440391e43daSPeter Zijlstra }
441391e43daSPeter Zijlstra 
442804d402fSQais Yousef #ifdef CONFIG_UCLAMP_TASK
443804d402fSQais Yousef /*
444804d402fSQais Yousef  * Verify the fitness of task @p to run on @cpu taking into account the uclamp
445804d402fSQais Yousef  * settings.
446804d402fSQais Yousef  *
447804d402fSQais Yousef  * This check is only important for heterogeneous systems where uclamp_min value
448804d402fSQais Yousef  * is higher than the capacity of a @cpu. For non-heterogeneous system this
449804d402fSQais Yousef  * function will always return true.
450804d402fSQais Yousef  *
451804d402fSQais Yousef  * The function will return true if the capacity of the @cpu is >= the
452804d402fSQais Yousef  * uclamp_min and false otherwise.
453804d402fSQais Yousef  *
454804d402fSQais Yousef  * Note that uclamp_min will be clamped to uclamp_max if uclamp_min
455804d402fSQais Yousef  * > uclamp_max.
456804d402fSQais Yousef  */
457804d402fSQais Yousef static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
458804d402fSQais Yousef {
459804d402fSQais Yousef 	unsigned int min_cap;
460804d402fSQais Yousef 	unsigned int max_cap;
461804d402fSQais Yousef 	unsigned int cpu_cap;
462804d402fSQais Yousef 
463804d402fSQais Yousef 	/* Only heterogeneous systems can benefit from this check */
464804d402fSQais Yousef 	if (!static_branch_unlikely(&sched_asym_cpucapacity))
465804d402fSQais Yousef 		return true;
466804d402fSQais Yousef 
467804d402fSQais Yousef 	min_cap = uclamp_eff_value(p, UCLAMP_MIN);
468804d402fSQais Yousef 	max_cap = uclamp_eff_value(p, UCLAMP_MAX);
469804d402fSQais Yousef 
470804d402fSQais Yousef 	cpu_cap = capacity_orig_of(cpu);
471804d402fSQais Yousef 
472804d402fSQais Yousef 	return cpu_cap >= min(min_cap, max_cap);
473804d402fSQais Yousef }
474804d402fSQais Yousef #else
475804d402fSQais Yousef static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
476804d402fSQais Yousef {
477804d402fSQais Yousef 	return true;
478804d402fSQais Yousef }
479804d402fSQais Yousef #endif
480804d402fSQais Yousef 
481391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
482391e43daSPeter Zijlstra 
483391e43daSPeter Zijlstra static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
484391e43daSPeter Zijlstra {
485391e43daSPeter Zijlstra 	if (!rt_rq->tg)
486391e43daSPeter Zijlstra 		return RUNTIME_INF;
487391e43daSPeter Zijlstra 
488391e43daSPeter Zijlstra 	return rt_rq->rt_runtime;
489391e43daSPeter Zijlstra }
490391e43daSPeter Zijlstra 
491391e43daSPeter Zijlstra static inline u64 sched_rt_period(struct rt_rq *rt_rq)
492391e43daSPeter Zijlstra {
493391e43daSPeter Zijlstra 	return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
494391e43daSPeter Zijlstra }
495391e43daSPeter Zijlstra 
496391e43daSPeter Zijlstra typedef struct task_group *rt_rq_iter_t;
497391e43daSPeter Zijlstra 
498391e43daSPeter Zijlstra static inline struct task_group *next_task_group(struct task_group *tg)
499391e43daSPeter Zijlstra {
500391e43daSPeter Zijlstra 	do {
501391e43daSPeter Zijlstra 		tg = list_entry_rcu(tg->list.next,
502391e43daSPeter Zijlstra 			typeof(struct task_group), list);
503391e43daSPeter Zijlstra 	} while (&tg->list != &task_groups && task_group_is_autogroup(tg));
504391e43daSPeter Zijlstra 
505391e43daSPeter Zijlstra 	if (&tg->list == &task_groups)
506391e43daSPeter Zijlstra 		tg = NULL;
507391e43daSPeter Zijlstra 
508391e43daSPeter Zijlstra 	return tg;
509391e43daSPeter Zijlstra }
510391e43daSPeter Zijlstra 
511391e43daSPeter Zijlstra #define for_each_rt_rq(rt_rq, iter, rq)					\
512391e43daSPeter Zijlstra 	for (iter = container_of(&task_groups, typeof(*iter), list);	\
513391e43daSPeter Zijlstra 		(iter = next_task_group(iter)) &&			\
514391e43daSPeter Zijlstra 		(rt_rq = iter->rt_rq[cpu_of(rq)]);)
515391e43daSPeter Zijlstra 
516391e43daSPeter Zijlstra #define for_each_sched_rt_entity(rt_se) \
517391e43daSPeter Zijlstra 	for (; rt_se; rt_se = rt_se->parent)
518391e43daSPeter Zijlstra 
519391e43daSPeter Zijlstra static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
520391e43daSPeter Zijlstra {
521391e43daSPeter Zijlstra 	return rt_se->my_q;
522391e43daSPeter Zijlstra }
523391e43daSPeter Zijlstra 
524ff77e468SPeter Zijlstra static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
525ff77e468SPeter Zijlstra static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
526391e43daSPeter Zijlstra 
527391e43daSPeter Zijlstra static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
528391e43daSPeter Zijlstra {
529391e43daSPeter Zijlstra 	struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
5308875125eSKirill Tkhai 	struct rq *rq = rq_of_rt_rq(rt_rq);
531391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se;
532391e43daSPeter Zijlstra 
5338875125eSKirill Tkhai 	int cpu = cpu_of(rq);
534391e43daSPeter Zijlstra 
535391e43daSPeter Zijlstra 	rt_se = rt_rq->tg->rt_se[cpu];
536391e43daSPeter Zijlstra 
537391e43daSPeter Zijlstra 	if (rt_rq->rt_nr_running) {
538f4ebcbc0SKirill Tkhai 		if (!rt_se)
539f4ebcbc0SKirill Tkhai 			enqueue_top_rt_rq(rt_rq);
540f4ebcbc0SKirill Tkhai 		else if (!on_rt_rq(rt_se))
541ff77e468SPeter Zijlstra 			enqueue_rt_entity(rt_se, 0);
542f4ebcbc0SKirill Tkhai 
543391e43daSPeter Zijlstra 		if (rt_rq->highest_prio.curr < curr->prio)
5448875125eSKirill Tkhai 			resched_curr(rq);
545391e43daSPeter Zijlstra 	}
546391e43daSPeter Zijlstra }
547391e43daSPeter Zijlstra 
548391e43daSPeter Zijlstra static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
549391e43daSPeter Zijlstra {
550391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se;
551391e43daSPeter Zijlstra 	int cpu = cpu_of(rq_of_rt_rq(rt_rq));
552391e43daSPeter Zijlstra 
553391e43daSPeter Zijlstra 	rt_se = rt_rq->tg->rt_se[cpu];
554391e43daSPeter Zijlstra 
555296b2ffeSVincent Guittot 	if (!rt_se) {
556f4ebcbc0SKirill Tkhai 		dequeue_top_rt_rq(rt_rq);
557296b2ffeSVincent Guittot 		/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
558296b2ffeSVincent Guittot 		cpufreq_update_util(rq_of_rt_rq(rt_rq), 0);
559296b2ffeSVincent Guittot 	}
560f4ebcbc0SKirill Tkhai 	else if (on_rt_rq(rt_se))
561ff77e468SPeter Zijlstra 		dequeue_rt_entity(rt_se, 0);
562391e43daSPeter Zijlstra }
563391e43daSPeter Zijlstra 
56446383648SKirill Tkhai static inline int rt_rq_throttled(struct rt_rq *rt_rq)
56546383648SKirill Tkhai {
56646383648SKirill Tkhai 	return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
56746383648SKirill Tkhai }
56846383648SKirill Tkhai 
569391e43daSPeter Zijlstra static int rt_se_boosted(struct sched_rt_entity *rt_se)
570391e43daSPeter Zijlstra {
571391e43daSPeter Zijlstra 	struct rt_rq *rt_rq = group_rt_rq(rt_se);
572391e43daSPeter Zijlstra 	struct task_struct *p;
573391e43daSPeter Zijlstra 
574391e43daSPeter Zijlstra 	if (rt_rq)
575391e43daSPeter Zijlstra 		return !!rt_rq->rt_nr_boosted;
576391e43daSPeter Zijlstra 
577391e43daSPeter Zijlstra 	p = rt_task_of(rt_se);
578391e43daSPeter Zijlstra 	return p->prio != p->normal_prio;
579391e43daSPeter Zijlstra }
580391e43daSPeter Zijlstra 
581391e43daSPeter Zijlstra #ifdef CONFIG_SMP
582391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void)
583391e43daSPeter Zijlstra {
584424c93feSNathan Zimmer 	return this_rq()->rd->span;
585391e43daSPeter Zijlstra }
586391e43daSPeter Zijlstra #else
587391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void)
588391e43daSPeter Zijlstra {
589391e43daSPeter Zijlstra 	return cpu_online_mask;
590391e43daSPeter Zijlstra }
591391e43daSPeter Zijlstra #endif
592391e43daSPeter Zijlstra 
593391e43daSPeter Zijlstra static inline
594391e43daSPeter Zijlstra struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
595391e43daSPeter Zijlstra {
596391e43daSPeter Zijlstra 	return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
597391e43daSPeter Zijlstra }
598391e43daSPeter Zijlstra 
599391e43daSPeter Zijlstra static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
600391e43daSPeter Zijlstra {
601391e43daSPeter Zijlstra 	return &rt_rq->tg->rt_bandwidth;
602391e43daSPeter Zijlstra }
603391e43daSPeter Zijlstra 
604391e43daSPeter Zijlstra #else /* !CONFIG_RT_GROUP_SCHED */
605391e43daSPeter Zijlstra 
606391e43daSPeter Zijlstra static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
607391e43daSPeter Zijlstra {
608391e43daSPeter Zijlstra 	return rt_rq->rt_runtime;
609391e43daSPeter Zijlstra }
610391e43daSPeter Zijlstra 
611391e43daSPeter Zijlstra static inline u64 sched_rt_period(struct rt_rq *rt_rq)
612391e43daSPeter Zijlstra {
613391e43daSPeter Zijlstra 	return ktime_to_ns(def_rt_bandwidth.rt_period);
614391e43daSPeter Zijlstra }
615391e43daSPeter Zijlstra 
616391e43daSPeter Zijlstra typedef struct rt_rq *rt_rq_iter_t;
617391e43daSPeter Zijlstra 
618391e43daSPeter Zijlstra #define for_each_rt_rq(rt_rq, iter, rq) \
619391e43daSPeter Zijlstra 	for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
620391e43daSPeter Zijlstra 
621391e43daSPeter Zijlstra #define for_each_sched_rt_entity(rt_se) \
622391e43daSPeter Zijlstra 	for (; rt_se; rt_se = NULL)
623391e43daSPeter Zijlstra 
624391e43daSPeter Zijlstra static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
625391e43daSPeter Zijlstra {
626391e43daSPeter Zijlstra 	return NULL;
627391e43daSPeter Zijlstra }
628391e43daSPeter Zijlstra 
629391e43daSPeter Zijlstra static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
630391e43daSPeter Zijlstra {
631f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_rq(rt_rq);
632f4ebcbc0SKirill Tkhai 
633f4ebcbc0SKirill Tkhai 	if (!rt_rq->rt_nr_running)
634f4ebcbc0SKirill Tkhai 		return;
635f4ebcbc0SKirill Tkhai 
636f4ebcbc0SKirill Tkhai 	enqueue_top_rt_rq(rt_rq);
6378875125eSKirill Tkhai 	resched_curr(rq);
638391e43daSPeter Zijlstra }
639391e43daSPeter Zijlstra 
640391e43daSPeter Zijlstra static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
641391e43daSPeter Zijlstra {
642f4ebcbc0SKirill Tkhai 	dequeue_top_rt_rq(rt_rq);
643391e43daSPeter Zijlstra }
644391e43daSPeter Zijlstra 
64546383648SKirill Tkhai static inline int rt_rq_throttled(struct rt_rq *rt_rq)
64646383648SKirill Tkhai {
64746383648SKirill Tkhai 	return rt_rq->rt_throttled;
64846383648SKirill Tkhai }
64946383648SKirill Tkhai 
650391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void)
651391e43daSPeter Zijlstra {
652391e43daSPeter Zijlstra 	return cpu_online_mask;
653391e43daSPeter Zijlstra }
654391e43daSPeter Zijlstra 
655391e43daSPeter Zijlstra static inline
656391e43daSPeter Zijlstra struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
657391e43daSPeter Zijlstra {
658391e43daSPeter Zijlstra 	return &cpu_rq(cpu)->rt;
659391e43daSPeter Zijlstra }
660391e43daSPeter Zijlstra 
661391e43daSPeter Zijlstra static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
662391e43daSPeter Zijlstra {
663391e43daSPeter Zijlstra 	return &def_rt_bandwidth;
664391e43daSPeter Zijlstra }
665391e43daSPeter Zijlstra 
666391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */
667391e43daSPeter Zijlstra 
668faa59937SJuri Lelli bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
669faa59937SJuri Lelli {
670faa59937SJuri Lelli 	struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
671faa59937SJuri Lelli 
672faa59937SJuri Lelli 	return (hrtimer_active(&rt_b->rt_period_timer) ||
673faa59937SJuri Lelli 		rt_rq->rt_time < rt_b->rt_runtime);
674faa59937SJuri Lelli }
675faa59937SJuri Lelli 
676391e43daSPeter Zijlstra #ifdef CONFIG_SMP
677391e43daSPeter Zijlstra /*
678391e43daSPeter Zijlstra  * We ran out of runtime, see if we can borrow some from our neighbours.
679391e43daSPeter Zijlstra  */
680269b26a5SJuri Lelli static void do_balance_runtime(struct rt_rq *rt_rq)
681391e43daSPeter Zijlstra {
682391e43daSPeter Zijlstra 	struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
683aa7f6730SShawn Bohrer 	struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
684269b26a5SJuri Lelli 	int i, weight;
685391e43daSPeter Zijlstra 	u64 rt_period;
686391e43daSPeter Zijlstra 
687391e43daSPeter Zijlstra 	weight = cpumask_weight(rd->span);
688391e43daSPeter Zijlstra 
689391e43daSPeter Zijlstra 	raw_spin_lock(&rt_b->rt_runtime_lock);
690391e43daSPeter Zijlstra 	rt_period = ktime_to_ns(rt_b->rt_period);
691391e43daSPeter Zijlstra 	for_each_cpu(i, rd->span) {
692391e43daSPeter Zijlstra 		struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
693391e43daSPeter Zijlstra 		s64 diff;
694391e43daSPeter Zijlstra 
695391e43daSPeter Zijlstra 		if (iter == rt_rq)
696391e43daSPeter Zijlstra 			continue;
697391e43daSPeter Zijlstra 
698391e43daSPeter Zijlstra 		raw_spin_lock(&iter->rt_runtime_lock);
699391e43daSPeter Zijlstra 		/*
700391e43daSPeter Zijlstra 		 * Either all rqs have inf runtime and there's nothing to steal
701391e43daSPeter Zijlstra 		 * or __disable_runtime() below sets a specific rq to inf to
702391e43daSPeter Zijlstra 		 * indicate its been disabled and disalow stealing.
703391e43daSPeter Zijlstra 		 */
704391e43daSPeter Zijlstra 		if (iter->rt_runtime == RUNTIME_INF)
705391e43daSPeter Zijlstra 			goto next;
706391e43daSPeter Zijlstra 
707391e43daSPeter Zijlstra 		/*
708391e43daSPeter Zijlstra 		 * From runqueues with spare time, take 1/n part of their
709391e43daSPeter Zijlstra 		 * spare time, but no more than our period.
710391e43daSPeter Zijlstra 		 */
711391e43daSPeter Zijlstra 		diff = iter->rt_runtime - iter->rt_time;
712391e43daSPeter Zijlstra 		if (diff > 0) {
713391e43daSPeter Zijlstra 			diff = div_u64((u64)diff, weight);
714391e43daSPeter Zijlstra 			if (rt_rq->rt_runtime + diff > rt_period)
715391e43daSPeter Zijlstra 				diff = rt_period - rt_rq->rt_runtime;
716391e43daSPeter Zijlstra 			iter->rt_runtime -= diff;
717391e43daSPeter Zijlstra 			rt_rq->rt_runtime += diff;
718391e43daSPeter Zijlstra 			if (rt_rq->rt_runtime == rt_period) {
719391e43daSPeter Zijlstra 				raw_spin_unlock(&iter->rt_runtime_lock);
720391e43daSPeter Zijlstra 				break;
721391e43daSPeter Zijlstra 			}
722391e43daSPeter Zijlstra 		}
723391e43daSPeter Zijlstra next:
724391e43daSPeter Zijlstra 		raw_spin_unlock(&iter->rt_runtime_lock);
725391e43daSPeter Zijlstra 	}
726391e43daSPeter Zijlstra 	raw_spin_unlock(&rt_b->rt_runtime_lock);
727391e43daSPeter Zijlstra }
728391e43daSPeter Zijlstra 
729391e43daSPeter Zijlstra /*
730391e43daSPeter Zijlstra  * Ensure this RQ takes back all the runtime it lend to its neighbours.
731391e43daSPeter Zijlstra  */
732391e43daSPeter Zijlstra static void __disable_runtime(struct rq *rq)
733391e43daSPeter Zijlstra {
734391e43daSPeter Zijlstra 	struct root_domain *rd = rq->rd;
735391e43daSPeter Zijlstra 	rt_rq_iter_t iter;
736391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
737391e43daSPeter Zijlstra 
738391e43daSPeter Zijlstra 	if (unlikely(!scheduler_running))
739391e43daSPeter Zijlstra 		return;
740391e43daSPeter Zijlstra 
741391e43daSPeter Zijlstra 	for_each_rt_rq(rt_rq, iter, rq) {
742391e43daSPeter Zijlstra 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
743391e43daSPeter Zijlstra 		s64 want;
744391e43daSPeter Zijlstra 		int i;
745391e43daSPeter Zijlstra 
746391e43daSPeter Zijlstra 		raw_spin_lock(&rt_b->rt_runtime_lock);
747391e43daSPeter Zijlstra 		raw_spin_lock(&rt_rq->rt_runtime_lock);
748391e43daSPeter Zijlstra 		/*
749391e43daSPeter Zijlstra 		 * Either we're all inf and nobody needs to borrow, or we're
750391e43daSPeter Zijlstra 		 * already disabled and thus have nothing to do, or we have
751391e43daSPeter Zijlstra 		 * exactly the right amount of runtime to take out.
752391e43daSPeter Zijlstra 		 */
753391e43daSPeter Zijlstra 		if (rt_rq->rt_runtime == RUNTIME_INF ||
754391e43daSPeter Zijlstra 				rt_rq->rt_runtime == rt_b->rt_runtime)
755391e43daSPeter Zijlstra 			goto balanced;
756391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
757391e43daSPeter Zijlstra 
758391e43daSPeter Zijlstra 		/*
759391e43daSPeter Zijlstra 		 * Calculate the difference between what we started out with
760391e43daSPeter Zijlstra 		 * and what we current have, that's the amount of runtime
761391e43daSPeter Zijlstra 		 * we lend and now have to reclaim.
762391e43daSPeter Zijlstra 		 */
763391e43daSPeter Zijlstra 		want = rt_b->rt_runtime - rt_rq->rt_runtime;
764391e43daSPeter Zijlstra 
765391e43daSPeter Zijlstra 		/*
766391e43daSPeter Zijlstra 		 * Greedy reclaim, take back as much as we can.
767391e43daSPeter Zijlstra 		 */
768391e43daSPeter Zijlstra 		for_each_cpu(i, rd->span) {
769391e43daSPeter Zijlstra 			struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
770391e43daSPeter Zijlstra 			s64 diff;
771391e43daSPeter Zijlstra 
772391e43daSPeter Zijlstra 			/*
773391e43daSPeter Zijlstra 			 * Can't reclaim from ourselves or disabled runqueues.
774391e43daSPeter Zijlstra 			 */
775391e43daSPeter Zijlstra 			if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
776391e43daSPeter Zijlstra 				continue;
777391e43daSPeter Zijlstra 
778391e43daSPeter Zijlstra 			raw_spin_lock(&iter->rt_runtime_lock);
779391e43daSPeter Zijlstra 			if (want > 0) {
780391e43daSPeter Zijlstra 				diff = min_t(s64, iter->rt_runtime, want);
781391e43daSPeter Zijlstra 				iter->rt_runtime -= diff;
782391e43daSPeter Zijlstra 				want -= diff;
783391e43daSPeter Zijlstra 			} else {
784391e43daSPeter Zijlstra 				iter->rt_runtime -= want;
785391e43daSPeter Zijlstra 				want -= want;
786391e43daSPeter Zijlstra 			}
787391e43daSPeter Zijlstra 			raw_spin_unlock(&iter->rt_runtime_lock);
788391e43daSPeter Zijlstra 
789391e43daSPeter Zijlstra 			if (!want)
790391e43daSPeter Zijlstra 				break;
791391e43daSPeter Zijlstra 		}
792391e43daSPeter Zijlstra 
793391e43daSPeter Zijlstra 		raw_spin_lock(&rt_rq->rt_runtime_lock);
794391e43daSPeter Zijlstra 		/*
795391e43daSPeter Zijlstra 		 * We cannot be left wanting - that would mean some runtime
796391e43daSPeter Zijlstra 		 * leaked out of the system.
797391e43daSPeter Zijlstra 		 */
798391e43daSPeter Zijlstra 		BUG_ON(want);
799391e43daSPeter Zijlstra balanced:
800391e43daSPeter Zijlstra 		/*
801391e43daSPeter Zijlstra 		 * Disable all the borrow logic by pretending we have inf
802391e43daSPeter Zijlstra 		 * runtime - in which case borrowing doesn't make sense.
803391e43daSPeter Zijlstra 		 */
804391e43daSPeter Zijlstra 		rt_rq->rt_runtime = RUNTIME_INF;
805a4c96ae3SPeter Boonstoppel 		rt_rq->rt_throttled = 0;
806391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
807391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_b->rt_runtime_lock);
80899b62567SKirill Tkhai 
80999b62567SKirill Tkhai 		/* Make rt_rq available for pick_next_task() */
81099b62567SKirill Tkhai 		sched_rt_rq_enqueue(rt_rq);
811391e43daSPeter Zijlstra 	}
812391e43daSPeter Zijlstra }
813391e43daSPeter Zijlstra 
814391e43daSPeter Zijlstra static void __enable_runtime(struct rq *rq)
815391e43daSPeter Zijlstra {
816391e43daSPeter Zijlstra 	rt_rq_iter_t iter;
817391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
818391e43daSPeter Zijlstra 
819391e43daSPeter Zijlstra 	if (unlikely(!scheduler_running))
820391e43daSPeter Zijlstra 		return;
821391e43daSPeter Zijlstra 
822391e43daSPeter Zijlstra 	/*
823391e43daSPeter Zijlstra 	 * Reset each runqueue's bandwidth settings
824391e43daSPeter Zijlstra 	 */
825391e43daSPeter Zijlstra 	for_each_rt_rq(rt_rq, iter, rq) {
826391e43daSPeter Zijlstra 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
827391e43daSPeter Zijlstra 
828391e43daSPeter Zijlstra 		raw_spin_lock(&rt_b->rt_runtime_lock);
829391e43daSPeter Zijlstra 		raw_spin_lock(&rt_rq->rt_runtime_lock);
830391e43daSPeter Zijlstra 		rt_rq->rt_runtime = rt_b->rt_runtime;
831391e43daSPeter Zijlstra 		rt_rq->rt_time = 0;
832391e43daSPeter Zijlstra 		rt_rq->rt_throttled = 0;
833391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
834391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_b->rt_runtime_lock);
835391e43daSPeter Zijlstra 	}
836391e43daSPeter Zijlstra }
837391e43daSPeter Zijlstra 
838269b26a5SJuri Lelli static void balance_runtime(struct rt_rq *rt_rq)
839391e43daSPeter Zijlstra {
840391e43daSPeter Zijlstra 	if (!sched_feat(RT_RUNTIME_SHARE))
841269b26a5SJuri Lelli 		return;
842391e43daSPeter Zijlstra 
843391e43daSPeter Zijlstra 	if (rt_rq->rt_time > rt_rq->rt_runtime) {
844391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
845269b26a5SJuri Lelli 		do_balance_runtime(rt_rq);
846391e43daSPeter Zijlstra 		raw_spin_lock(&rt_rq->rt_runtime_lock);
847391e43daSPeter Zijlstra 	}
848391e43daSPeter Zijlstra }
849391e43daSPeter Zijlstra #else /* !CONFIG_SMP */
850269b26a5SJuri Lelli static inline void balance_runtime(struct rt_rq *rt_rq) {}
851391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
852391e43daSPeter Zijlstra 
853391e43daSPeter Zijlstra static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
854391e43daSPeter Zijlstra {
85542c62a58SPeter Zijlstra 	int i, idle = 1, throttled = 0;
856391e43daSPeter Zijlstra 	const struct cpumask *span;
857391e43daSPeter Zijlstra 
858391e43daSPeter Zijlstra 	span = sched_rt_period_mask();
859e221d028SMike Galbraith #ifdef CONFIG_RT_GROUP_SCHED
860e221d028SMike Galbraith 	/*
861e221d028SMike Galbraith 	 * FIXME: isolated CPUs should really leave the root task group,
862e221d028SMike Galbraith 	 * whether they are isolcpus or were isolated via cpusets, lest
863e221d028SMike Galbraith 	 * the timer run on a CPU which does not service all runqueues,
864e221d028SMike Galbraith 	 * potentially leaving other CPUs indefinitely throttled.  If
865e221d028SMike Galbraith 	 * isolation is really required, the user will turn the throttle
866e221d028SMike Galbraith 	 * off to kill the perturbations it causes anyway.  Meanwhile,
867e221d028SMike Galbraith 	 * this maintains functionality for boot and/or troubleshooting.
868e221d028SMike Galbraith 	 */
869e221d028SMike Galbraith 	if (rt_b == &root_task_group.rt_bandwidth)
870e221d028SMike Galbraith 		span = cpu_online_mask;
871e221d028SMike Galbraith #endif
872391e43daSPeter Zijlstra 	for_each_cpu(i, span) {
873391e43daSPeter Zijlstra 		int enqueue = 0;
874391e43daSPeter Zijlstra 		struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
875391e43daSPeter Zijlstra 		struct rq *rq = rq_of_rt_rq(rt_rq);
876c249f255SDave Kleikamp 		int skip;
877c249f255SDave Kleikamp 
878c249f255SDave Kleikamp 		/*
879c249f255SDave Kleikamp 		 * When span == cpu_online_mask, taking each rq->lock
880c249f255SDave Kleikamp 		 * can be time-consuming. Try to avoid it when possible.
881c249f255SDave Kleikamp 		 */
882c249f255SDave Kleikamp 		raw_spin_lock(&rt_rq->rt_runtime_lock);
883f3d133eeSHailong Liu 		if (!sched_feat(RT_RUNTIME_SHARE) && rt_rq->rt_runtime != RUNTIME_INF)
884f3d133eeSHailong Liu 			rt_rq->rt_runtime = rt_b->rt_runtime;
885c249f255SDave Kleikamp 		skip = !rt_rq->rt_time && !rt_rq->rt_nr_running;
886c249f255SDave Kleikamp 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
887c249f255SDave Kleikamp 		if (skip)
888c249f255SDave Kleikamp 			continue;
889391e43daSPeter Zijlstra 
890391e43daSPeter Zijlstra 		raw_spin_lock(&rq->lock);
891d29a2064SDavidlohr Bueso 		update_rq_clock(rq);
892d29a2064SDavidlohr Bueso 
893391e43daSPeter Zijlstra 		if (rt_rq->rt_time) {
894391e43daSPeter Zijlstra 			u64 runtime;
895391e43daSPeter Zijlstra 
896391e43daSPeter Zijlstra 			raw_spin_lock(&rt_rq->rt_runtime_lock);
897391e43daSPeter Zijlstra 			if (rt_rq->rt_throttled)
898391e43daSPeter Zijlstra 				balance_runtime(rt_rq);
899391e43daSPeter Zijlstra 			runtime = rt_rq->rt_runtime;
900391e43daSPeter Zijlstra 			rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
901391e43daSPeter Zijlstra 			if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
902391e43daSPeter Zijlstra 				rt_rq->rt_throttled = 0;
903391e43daSPeter Zijlstra 				enqueue = 1;
904391e43daSPeter Zijlstra 
905391e43daSPeter Zijlstra 				/*
9069edfbfedSPeter Zijlstra 				 * When we're idle and a woken (rt) task is
9079edfbfedSPeter Zijlstra 				 * throttled check_preempt_curr() will set
9089edfbfedSPeter Zijlstra 				 * skip_update and the time between the wakeup
9099edfbfedSPeter Zijlstra 				 * and this unthrottle will get accounted as
9109edfbfedSPeter Zijlstra 				 * 'runtime'.
911391e43daSPeter Zijlstra 				 */
912391e43daSPeter Zijlstra 				if (rt_rq->rt_nr_running && rq->curr == rq->idle)
913adcc8da8SDavidlohr Bueso 					rq_clock_cancel_skipupdate(rq);
914391e43daSPeter Zijlstra 			}
915391e43daSPeter Zijlstra 			if (rt_rq->rt_time || rt_rq->rt_nr_running)
916391e43daSPeter Zijlstra 				idle = 0;
917391e43daSPeter Zijlstra 			raw_spin_unlock(&rt_rq->rt_runtime_lock);
918391e43daSPeter Zijlstra 		} else if (rt_rq->rt_nr_running) {
919391e43daSPeter Zijlstra 			idle = 0;
920391e43daSPeter Zijlstra 			if (!rt_rq_throttled(rt_rq))
921391e43daSPeter Zijlstra 				enqueue = 1;
922391e43daSPeter Zijlstra 		}
92342c62a58SPeter Zijlstra 		if (rt_rq->rt_throttled)
92442c62a58SPeter Zijlstra 			throttled = 1;
925391e43daSPeter Zijlstra 
926391e43daSPeter Zijlstra 		if (enqueue)
927391e43daSPeter Zijlstra 			sched_rt_rq_enqueue(rt_rq);
928391e43daSPeter Zijlstra 		raw_spin_unlock(&rq->lock);
929391e43daSPeter Zijlstra 	}
930391e43daSPeter Zijlstra 
93142c62a58SPeter Zijlstra 	if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
93242c62a58SPeter Zijlstra 		return 1;
93342c62a58SPeter Zijlstra 
934391e43daSPeter Zijlstra 	return idle;
935391e43daSPeter Zijlstra }
936391e43daSPeter Zijlstra 
937391e43daSPeter Zijlstra static inline int rt_se_prio(struct sched_rt_entity *rt_se)
938391e43daSPeter Zijlstra {
939391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
940391e43daSPeter Zijlstra 	struct rt_rq *rt_rq = group_rt_rq(rt_se);
941391e43daSPeter Zijlstra 
942391e43daSPeter Zijlstra 	if (rt_rq)
943391e43daSPeter Zijlstra 		return rt_rq->highest_prio.curr;
944391e43daSPeter Zijlstra #endif
945391e43daSPeter Zijlstra 
946391e43daSPeter Zijlstra 	return rt_task_of(rt_se)->prio;
947391e43daSPeter Zijlstra }
948391e43daSPeter Zijlstra 
949391e43daSPeter Zijlstra static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
950391e43daSPeter Zijlstra {
951391e43daSPeter Zijlstra 	u64 runtime = sched_rt_runtime(rt_rq);
952391e43daSPeter Zijlstra 
953391e43daSPeter Zijlstra 	if (rt_rq->rt_throttled)
954391e43daSPeter Zijlstra 		return rt_rq_throttled(rt_rq);
955391e43daSPeter Zijlstra 
9565b680fd6SShan Hai 	if (runtime >= sched_rt_period(rt_rq))
957391e43daSPeter Zijlstra 		return 0;
958391e43daSPeter Zijlstra 
959391e43daSPeter Zijlstra 	balance_runtime(rt_rq);
960391e43daSPeter Zijlstra 	runtime = sched_rt_runtime(rt_rq);
961391e43daSPeter Zijlstra 	if (runtime == RUNTIME_INF)
962391e43daSPeter Zijlstra 		return 0;
963391e43daSPeter Zijlstra 
964391e43daSPeter Zijlstra 	if (rt_rq->rt_time > runtime) {
9657abc63b1SPeter Zijlstra 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
9667abc63b1SPeter Zijlstra 
9677abc63b1SPeter Zijlstra 		/*
9687abc63b1SPeter Zijlstra 		 * Don't actually throttle groups that have no runtime assigned
9697abc63b1SPeter Zijlstra 		 * but accrue some time due to boosting.
9707abc63b1SPeter Zijlstra 		 */
9717abc63b1SPeter Zijlstra 		if (likely(rt_b->rt_runtime)) {
972391e43daSPeter Zijlstra 			rt_rq->rt_throttled = 1;
973c224815dSJohn Stultz 			printk_deferred_once("sched: RT throttling activated\n");
9747abc63b1SPeter Zijlstra 		} else {
9757abc63b1SPeter Zijlstra 			/*
9767abc63b1SPeter Zijlstra 			 * In case we did anyway, make it go away,
9777abc63b1SPeter Zijlstra 			 * replenishment is a joke, since it will replenish us
9787abc63b1SPeter Zijlstra 			 * with exactly 0 ns.
9797abc63b1SPeter Zijlstra 			 */
9807abc63b1SPeter Zijlstra 			rt_rq->rt_time = 0;
9817abc63b1SPeter Zijlstra 		}
9827abc63b1SPeter Zijlstra 
983391e43daSPeter Zijlstra 		if (rt_rq_throttled(rt_rq)) {
984391e43daSPeter Zijlstra 			sched_rt_rq_dequeue(rt_rq);
985391e43daSPeter Zijlstra 			return 1;
986391e43daSPeter Zijlstra 		}
987391e43daSPeter Zijlstra 	}
988391e43daSPeter Zijlstra 
989391e43daSPeter Zijlstra 	return 0;
990391e43daSPeter Zijlstra }
991391e43daSPeter Zijlstra 
992391e43daSPeter Zijlstra /*
993391e43daSPeter Zijlstra  * Update the current task's runtime statistics. Skip current tasks that
994391e43daSPeter Zijlstra  * are not in our scheduling class.
995391e43daSPeter Zijlstra  */
996391e43daSPeter Zijlstra static void update_curr_rt(struct rq *rq)
997391e43daSPeter Zijlstra {
998391e43daSPeter Zijlstra 	struct task_struct *curr = rq->curr;
999391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se = &curr->rt;
1000391e43daSPeter Zijlstra 	u64 delta_exec;
1001a7711602SWen Yang 	u64 now;
1002391e43daSPeter Zijlstra 
1003391e43daSPeter Zijlstra 	if (curr->sched_class != &rt_sched_class)
1004391e43daSPeter Zijlstra 		return;
1005391e43daSPeter Zijlstra 
1006a7711602SWen Yang 	now = rq_clock_task(rq);
1007e7ad2031SWen Yang 	delta_exec = now - curr->se.exec_start;
1008fc79e240SKirill Tkhai 	if (unlikely((s64)delta_exec <= 0))
1009fc79e240SKirill Tkhai 		return;
1010391e43daSPeter Zijlstra 
101142c62a58SPeter Zijlstra 	schedstat_set(curr->se.statistics.exec_max,
101242c62a58SPeter Zijlstra 		      max(curr->se.statistics.exec_max, delta_exec));
1013391e43daSPeter Zijlstra 
1014391e43daSPeter Zijlstra 	curr->se.sum_exec_runtime += delta_exec;
1015391e43daSPeter Zijlstra 	account_group_exec_runtime(curr, delta_exec);
1016391e43daSPeter Zijlstra 
1017e7ad2031SWen Yang 	curr->se.exec_start = now;
1018d2cc5ed6STejun Heo 	cgroup_account_cputime(curr, delta_exec);
1019391e43daSPeter Zijlstra 
1020391e43daSPeter Zijlstra 	if (!rt_bandwidth_enabled())
1021391e43daSPeter Zijlstra 		return;
1022391e43daSPeter Zijlstra 
1023391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se) {
10240b07939cSGiedrius Rekasius 		struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1025391e43daSPeter Zijlstra 
1026391e43daSPeter Zijlstra 		if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
1027391e43daSPeter Zijlstra 			raw_spin_lock(&rt_rq->rt_runtime_lock);
1028391e43daSPeter Zijlstra 			rt_rq->rt_time += delta_exec;
1029391e43daSPeter Zijlstra 			if (sched_rt_runtime_exceeded(rt_rq))
10308875125eSKirill Tkhai 				resched_curr(rq);
1031391e43daSPeter Zijlstra 			raw_spin_unlock(&rt_rq->rt_runtime_lock);
1032391e43daSPeter Zijlstra 		}
1033391e43daSPeter Zijlstra 	}
1034391e43daSPeter Zijlstra }
1035391e43daSPeter Zijlstra 
1036f4ebcbc0SKirill Tkhai static void
1037f4ebcbc0SKirill Tkhai dequeue_top_rt_rq(struct rt_rq *rt_rq)
1038f4ebcbc0SKirill Tkhai {
1039f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_rq(rt_rq);
1040f4ebcbc0SKirill Tkhai 
1041f4ebcbc0SKirill Tkhai 	BUG_ON(&rq->rt != rt_rq);
1042f4ebcbc0SKirill Tkhai 
1043f4ebcbc0SKirill Tkhai 	if (!rt_rq->rt_queued)
1044f4ebcbc0SKirill Tkhai 		return;
1045f4ebcbc0SKirill Tkhai 
1046f4ebcbc0SKirill Tkhai 	BUG_ON(!rq->nr_running);
1047f4ebcbc0SKirill Tkhai 
104872465447SKirill Tkhai 	sub_nr_running(rq, rt_rq->rt_nr_running);
1049f4ebcbc0SKirill Tkhai 	rt_rq->rt_queued = 0;
10508f111bc3SPeter Zijlstra 
1051f4ebcbc0SKirill Tkhai }
1052f4ebcbc0SKirill Tkhai 
1053f4ebcbc0SKirill Tkhai static void
1054f4ebcbc0SKirill Tkhai enqueue_top_rt_rq(struct rt_rq *rt_rq)
1055f4ebcbc0SKirill Tkhai {
1056f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_rq(rt_rq);
1057f4ebcbc0SKirill Tkhai 
1058f4ebcbc0SKirill Tkhai 	BUG_ON(&rq->rt != rt_rq);
1059f4ebcbc0SKirill Tkhai 
1060f4ebcbc0SKirill Tkhai 	if (rt_rq->rt_queued)
1061f4ebcbc0SKirill Tkhai 		return;
1062296b2ffeSVincent Guittot 
1063296b2ffeSVincent Guittot 	if (rt_rq_throttled(rt_rq))
1064f4ebcbc0SKirill Tkhai 		return;
1065f4ebcbc0SKirill Tkhai 
1066296b2ffeSVincent Guittot 	if (rt_rq->rt_nr_running) {
106772465447SKirill Tkhai 		add_nr_running(rq, rt_rq->rt_nr_running);
1068f4ebcbc0SKirill Tkhai 		rt_rq->rt_queued = 1;
1069296b2ffeSVincent Guittot 	}
10708f111bc3SPeter Zijlstra 
10718f111bc3SPeter Zijlstra 	/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
10728f111bc3SPeter Zijlstra 	cpufreq_update_util(rq, 0);
1073f4ebcbc0SKirill Tkhai }
1074f4ebcbc0SKirill Tkhai 
1075391e43daSPeter Zijlstra #if defined CONFIG_SMP
1076391e43daSPeter Zijlstra 
1077391e43daSPeter Zijlstra static void
1078391e43daSPeter Zijlstra inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1079391e43daSPeter Zijlstra {
1080391e43daSPeter Zijlstra 	struct rq *rq = rq_of_rt_rq(rt_rq);
1081391e43daSPeter Zijlstra 
1082757dfcaaSKirill Tkhai #ifdef CONFIG_RT_GROUP_SCHED
1083757dfcaaSKirill Tkhai 	/*
1084757dfcaaSKirill Tkhai 	 * Change rq's cpupri only if rt_rq is the top queue.
1085757dfcaaSKirill Tkhai 	 */
1086757dfcaaSKirill Tkhai 	if (&rq->rt != rt_rq)
1087757dfcaaSKirill Tkhai 		return;
1088757dfcaaSKirill Tkhai #endif
1089391e43daSPeter Zijlstra 	if (rq->online && prio < prev_prio)
1090391e43daSPeter Zijlstra 		cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
1091391e43daSPeter Zijlstra }
1092391e43daSPeter Zijlstra 
1093391e43daSPeter Zijlstra static void
1094391e43daSPeter Zijlstra dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1095391e43daSPeter Zijlstra {
1096391e43daSPeter Zijlstra 	struct rq *rq = rq_of_rt_rq(rt_rq);
1097391e43daSPeter Zijlstra 
1098757dfcaaSKirill Tkhai #ifdef CONFIG_RT_GROUP_SCHED
1099757dfcaaSKirill Tkhai 	/*
1100757dfcaaSKirill Tkhai 	 * Change rq's cpupri only if rt_rq is the top queue.
1101757dfcaaSKirill Tkhai 	 */
1102757dfcaaSKirill Tkhai 	if (&rq->rt != rt_rq)
1103757dfcaaSKirill Tkhai 		return;
1104757dfcaaSKirill Tkhai #endif
1105391e43daSPeter Zijlstra 	if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1106391e43daSPeter Zijlstra 		cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1107391e43daSPeter Zijlstra }
1108391e43daSPeter Zijlstra 
1109391e43daSPeter Zijlstra #else /* CONFIG_SMP */
1110391e43daSPeter Zijlstra 
1111391e43daSPeter Zijlstra static inline
1112391e43daSPeter Zijlstra void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1113391e43daSPeter Zijlstra static inline
1114391e43daSPeter Zijlstra void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1115391e43daSPeter Zijlstra 
1116391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
1117391e43daSPeter Zijlstra 
1118391e43daSPeter Zijlstra #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1119391e43daSPeter Zijlstra static void
1120391e43daSPeter Zijlstra inc_rt_prio(struct rt_rq *rt_rq, int prio)
1121391e43daSPeter Zijlstra {
1122391e43daSPeter Zijlstra 	int prev_prio = rt_rq->highest_prio.curr;
1123391e43daSPeter Zijlstra 
1124391e43daSPeter Zijlstra 	if (prio < prev_prio)
1125391e43daSPeter Zijlstra 		rt_rq->highest_prio.curr = prio;
1126391e43daSPeter Zijlstra 
1127391e43daSPeter Zijlstra 	inc_rt_prio_smp(rt_rq, prio, prev_prio);
1128391e43daSPeter Zijlstra }
1129391e43daSPeter Zijlstra 
1130391e43daSPeter Zijlstra static void
1131391e43daSPeter Zijlstra dec_rt_prio(struct rt_rq *rt_rq, int prio)
1132391e43daSPeter Zijlstra {
1133391e43daSPeter Zijlstra 	int prev_prio = rt_rq->highest_prio.curr;
1134391e43daSPeter Zijlstra 
1135391e43daSPeter Zijlstra 	if (rt_rq->rt_nr_running) {
1136391e43daSPeter Zijlstra 
1137391e43daSPeter Zijlstra 		WARN_ON(prio < prev_prio);
1138391e43daSPeter Zijlstra 
1139391e43daSPeter Zijlstra 		/*
1140391e43daSPeter Zijlstra 		 * This may have been our highest task, and therefore
1141391e43daSPeter Zijlstra 		 * we may have some recomputation to do
1142391e43daSPeter Zijlstra 		 */
1143391e43daSPeter Zijlstra 		if (prio == prev_prio) {
1144391e43daSPeter Zijlstra 			struct rt_prio_array *array = &rt_rq->active;
1145391e43daSPeter Zijlstra 
1146391e43daSPeter Zijlstra 			rt_rq->highest_prio.curr =
1147391e43daSPeter Zijlstra 				sched_find_first_bit(array->bitmap);
1148391e43daSPeter Zijlstra 		}
1149391e43daSPeter Zijlstra 
1150391e43daSPeter Zijlstra 	} else
1151391e43daSPeter Zijlstra 		rt_rq->highest_prio.curr = MAX_RT_PRIO;
1152391e43daSPeter Zijlstra 
1153391e43daSPeter Zijlstra 	dec_rt_prio_smp(rt_rq, prio, prev_prio);
1154391e43daSPeter Zijlstra }
1155391e43daSPeter Zijlstra 
1156391e43daSPeter Zijlstra #else
1157391e43daSPeter Zijlstra 
1158391e43daSPeter Zijlstra static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1159391e43daSPeter Zijlstra static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1160391e43daSPeter Zijlstra 
1161391e43daSPeter Zijlstra #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1162391e43daSPeter Zijlstra 
1163391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
1164391e43daSPeter Zijlstra 
1165391e43daSPeter Zijlstra static void
1166391e43daSPeter Zijlstra inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1167391e43daSPeter Zijlstra {
1168391e43daSPeter Zijlstra 	if (rt_se_boosted(rt_se))
1169391e43daSPeter Zijlstra 		rt_rq->rt_nr_boosted++;
1170391e43daSPeter Zijlstra 
1171391e43daSPeter Zijlstra 	if (rt_rq->tg)
1172391e43daSPeter Zijlstra 		start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
1173391e43daSPeter Zijlstra }
1174391e43daSPeter Zijlstra 
1175391e43daSPeter Zijlstra static void
1176391e43daSPeter Zijlstra dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1177391e43daSPeter Zijlstra {
1178391e43daSPeter Zijlstra 	if (rt_se_boosted(rt_se))
1179391e43daSPeter Zijlstra 		rt_rq->rt_nr_boosted--;
1180391e43daSPeter Zijlstra 
1181391e43daSPeter Zijlstra 	WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
1182391e43daSPeter Zijlstra }
1183391e43daSPeter Zijlstra 
1184391e43daSPeter Zijlstra #else /* CONFIG_RT_GROUP_SCHED */
1185391e43daSPeter Zijlstra 
1186391e43daSPeter Zijlstra static void
1187391e43daSPeter Zijlstra inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1188391e43daSPeter Zijlstra {
1189391e43daSPeter Zijlstra 	start_rt_bandwidth(&def_rt_bandwidth);
1190391e43daSPeter Zijlstra }
1191391e43daSPeter Zijlstra 
1192391e43daSPeter Zijlstra static inline
1193391e43daSPeter Zijlstra void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1194391e43daSPeter Zijlstra 
1195391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */
1196391e43daSPeter Zijlstra 
1197391e43daSPeter Zijlstra static inline
119822abdef3SKirill Tkhai unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
119922abdef3SKirill Tkhai {
120022abdef3SKirill Tkhai 	struct rt_rq *group_rq = group_rt_rq(rt_se);
120122abdef3SKirill Tkhai 
120222abdef3SKirill Tkhai 	if (group_rq)
120322abdef3SKirill Tkhai 		return group_rq->rt_nr_running;
120422abdef3SKirill Tkhai 	else
120522abdef3SKirill Tkhai 		return 1;
120622abdef3SKirill Tkhai }
120722abdef3SKirill Tkhai 
120822abdef3SKirill Tkhai static inline
120901d36d0aSFrederic Weisbecker unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
121001d36d0aSFrederic Weisbecker {
121101d36d0aSFrederic Weisbecker 	struct rt_rq *group_rq = group_rt_rq(rt_se);
121201d36d0aSFrederic Weisbecker 	struct task_struct *tsk;
121301d36d0aSFrederic Weisbecker 
121401d36d0aSFrederic Weisbecker 	if (group_rq)
121501d36d0aSFrederic Weisbecker 		return group_rq->rr_nr_running;
121601d36d0aSFrederic Weisbecker 
121701d36d0aSFrederic Weisbecker 	tsk = rt_task_of(rt_se);
121801d36d0aSFrederic Weisbecker 
121901d36d0aSFrederic Weisbecker 	return (tsk->policy == SCHED_RR) ? 1 : 0;
122001d36d0aSFrederic Weisbecker }
122101d36d0aSFrederic Weisbecker 
122201d36d0aSFrederic Weisbecker static inline
1223391e43daSPeter Zijlstra void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1224391e43daSPeter Zijlstra {
1225391e43daSPeter Zijlstra 	int prio = rt_se_prio(rt_se);
1226391e43daSPeter Zijlstra 
1227391e43daSPeter Zijlstra 	WARN_ON(!rt_prio(prio));
122822abdef3SKirill Tkhai 	rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
122901d36d0aSFrederic Weisbecker 	rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
1230391e43daSPeter Zijlstra 
1231391e43daSPeter Zijlstra 	inc_rt_prio(rt_rq, prio);
1232391e43daSPeter Zijlstra 	inc_rt_migration(rt_se, rt_rq);
1233391e43daSPeter Zijlstra 	inc_rt_group(rt_se, rt_rq);
1234391e43daSPeter Zijlstra }
1235391e43daSPeter Zijlstra 
1236391e43daSPeter Zijlstra static inline
1237391e43daSPeter Zijlstra void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1238391e43daSPeter Zijlstra {
1239391e43daSPeter Zijlstra 	WARN_ON(!rt_prio(rt_se_prio(rt_se)));
1240391e43daSPeter Zijlstra 	WARN_ON(!rt_rq->rt_nr_running);
124122abdef3SKirill Tkhai 	rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
124201d36d0aSFrederic Weisbecker 	rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
1243391e43daSPeter Zijlstra 
1244391e43daSPeter Zijlstra 	dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1245391e43daSPeter Zijlstra 	dec_rt_migration(rt_se, rt_rq);
1246391e43daSPeter Zijlstra 	dec_rt_group(rt_se, rt_rq);
1247391e43daSPeter Zijlstra }
1248391e43daSPeter Zijlstra 
1249ff77e468SPeter Zijlstra /*
1250ff77e468SPeter Zijlstra  * Change rt_se->run_list location unless SAVE && !MOVE
1251ff77e468SPeter Zijlstra  *
1252ff77e468SPeter Zijlstra  * assumes ENQUEUE/DEQUEUE flags match
1253ff77e468SPeter Zijlstra  */
1254ff77e468SPeter Zijlstra static inline bool move_entity(unsigned int flags)
1255ff77e468SPeter Zijlstra {
1256ff77e468SPeter Zijlstra 	if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
1257ff77e468SPeter Zijlstra 		return false;
1258ff77e468SPeter Zijlstra 
1259ff77e468SPeter Zijlstra 	return true;
1260ff77e468SPeter Zijlstra }
1261ff77e468SPeter Zijlstra 
1262ff77e468SPeter Zijlstra static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
1263ff77e468SPeter Zijlstra {
1264ff77e468SPeter Zijlstra 	list_del_init(&rt_se->run_list);
1265ff77e468SPeter Zijlstra 
1266ff77e468SPeter Zijlstra 	if (list_empty(array->queue + rt_se_prio(rt_se)))
1267ff77e468SPeter Zijlstra 		__clear_bit(rt_se_prio(rt_se), array->bitmap);
1268ff77e468SPeter Zijlstra 
1269ff77e468SPeter Zijlstra 	rt_se->on_list = 0;
1270ff77e468SPeter Zijlstra }
1271ff77e468SPeter Zijlstra 
1272ff77e468SPeter Zijlstra static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1273391e43daSPeter Zijlstra {
1274391e43daSPeter Zijlstra 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1275391e43daSPeter Zijlstra 	struct rt_prio_array *array = &rt_rq->active;
1276391e43daSPeter Zijlstra 	struct rt_rq *group_rq = group_rt_rq(rt_se);
1277391e43daSPeter Zijlstra 	struct list_head *queue = array->queue + rt_se_prio(rt_se);
1278391e43daSPeter Zijlstra 
1279391e43daSPeter Zijlstra 	/*
1280391e43daSPeter Zijlstra 	 * Don't enqueue the group if its throttled, or when empty.
1281391e43daSPeter Zijlstra 	 * The latter is a consequence of the former when a child group
1282391e43daSPeter Zijlstra 	 * get throttled and the current group doesn't have any other
1283391e43daSPeter Zijlstra 	 * active members.
1284391e43daSPeter Zijlstra 	 */
1285ff77e468SPeter Zijlstra 	if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
1286ff77e468SPeter Zijlstra 		if (rt_se->on_list)
1287ff77e468SPeter Zijlstra 			__delist_rt_entity(rt_se, array);
1288391e43daSPeter Zijlstra 		return;
1289ff77e468SPeter Zijlstra 	}
1290391e43daSPeter Zijlstra 
1291ff77e468SPeter Zijlstra 	if (move_entity(flags)) {
1292ff77e468SPeter Zijlstra 		WARN_ON_ONCE(rt_se->on_list);
1293ff77e468SPeter Zijlstra 		if (flags & ENQUEUE_HEAD)
1294391e43daSPeter Zijlstra 			list_add(&rt_se->run_list, queue);
1295391e43daSPeter Zijlstra 		else
1296391e43daSPeter Zijlstra 			list_add_tail(&rt_se->run_list, queue);
1297ff77e468SPeter Zijlstra 
1298391e43daSPeter Zijlstra 		__set_bit(rt_se_prio(rt_se), array->bitmap);
1299ff77e468SPeter Zijlstra 		rt_se->on_list = 1;
1300ff77e468SPeter Zijlstra 	}
1301ff77e468SPeter Zijlstra 	rt_se->on_rq = 1;
1302391e43daSPeter Zijlstra 
1303391e43daSPeter Zijlstra 	inc_rt_tasks(rt_se, rt_rq);
1304391e43daSPeter Zijlstra }
1305391e43daSPeter Zijlstra 
1306ff77e468SPeter Zijlstra static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1307391e43daSPeter Zijlstra {
1308391e43daSPeter Zijlstra 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1309391e43daSPeter Zijlstra 	struct rt_prio_array *array = &rt_rq->active;
1310391e43daSPeter Zijlstra 
1311ff77e468SPeter Zijlstra 	if (move_entity(flags)) {
1312ff77e468SPeter Zijlstra 		WARN_ON_ONCE(!rt_se->on_list);
1313ff77e468SPeter Zijlstra 		__delist_rt_entity(rt_se, array);
1314ff77e468SPeter Zijlstra 	}
1315ff77e468SPeter Zijlstra 	rt_se->on_rq = 0;
1316391e43daSPeter Zijlstra 
1317391e43daSPeter Zijlstra 	dec_rt_tasks(rt_se, rt_rq);
1318391e43daSPeter Zijlstra }
1319391e43daSPeter Zijlstra 
1320391e43daSPeter Zijlstra /*
1321391e43daSPeter Zijlstra  * Because the prio of an upper entry depends on the lower
1322391e43daSPeter Zijlstra  * entries, we must remove entries top - down.
1323391e43daSPeter Zijlstra  */
1324ff77e468SPeter Zijlstra static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
1325391e43daSPeter Zijlstra {
1326391e43daSPeter Zijlstra 	struct sched_rt_entity *back = NULL;
1327391e43daSPeter Zijlstra 
1328391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se) {
1329391e43daSPeter Zijlstra 		rt_se->back = back;
1330391e43daSPeter Zijlstra 		back = rt_se;
1331391e43daSPeter Zijlstra 	}
1332391e43daSPeter Zijlstra 
1333f4ebcbc0SKirill Tkhai 	dequeue_top_rt_rq(rt_rq_of_se(back));
1334f4ebcbc0SKirill Tkhai 
1335391e43daSPeter Zijlstra 	for (rt_se = back; rt_se; rt_se = rt_se->back) {
1336391e43daSPeter Zijlstra 		if (on_rt_rq(rt_se))
1337ff77e468SPeter Zijlstra 			__dequeue_rt_entity(rt_se, flags);
1338391e43daSPeter Zijlstra 	}
1339391e43daSPeter Zijlstra }
1340391e43daSPeter Zijlstra 
1341ff77e468SPeter Zijlstra static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1342391e43daSPeter Zijlstra {
1343f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_se(rt_se);
1344f4ebcbc0SKirill Tkhai 
1345ff77e468SPeter Zijlstra 	dequeue_rt_stack(rt_se, flags);
1346391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se)
1347ff77e468SPeter Zijlstra 		__enqueue_rt_entity(rt_se, flags);
1348f4ebcbc0SKirill Tkhai 	enqueue_top_rt_rq(&rq->rt);
1349391e43daSPeter Zijlstra }
1350391e43daSPeter Zijlstra 
1351ff77e468SPeter Zijlstra static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1352391e43daSPeter Zijlstra {
1353f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_se(rt_se);
1354f4ebcbc0SKirill Tkhai 
1355ff77e468SPeter Zijlstra 	dequeue_rt_stack(rt_se, flags);
1356391e43daSPeter Zijlstra 
1357391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se) {
1358391e43daSPeter Zijlstra 		struct rt_rq *rt_rq = group_rt_rq(rt_se);
1359391e43daSPeter Zijlstra 
1360391e43daSPeter Zijlstra 		if (rt_rq && rt_rq->rt_nr_running)
1361ff77e468SPeter Zijlstra 			__enqueue_rt_entity(rt_se, flags);
1362391e43daSPeter Zijlstra 	}
1363f4ebcbc0SKirill Tkhai 	enqueue_top_rt_rq(&rq->rt);
1364391e43daSPeter Zijlstra }
1365391e43daSPeter Zijlstra 
1366391e43daSPeter Zijlstra /*
1367391e43daSPeter Zijlstra  * Adding/removing a task to/from a priority array:
1368391e43daSPeter Zijlstra  */
1369391e43daSPeter Zijlstra static void
1370391e43daSPeter Zijlstra enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1371391e43daSPeter Zijlstra {
1372391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se = &p->rt;
1373391e43daSPeter Zijlstra 
1374391e43daSPeter Zijlstra 	if (flags & ENQUEUE_WAKEUP)
1375391e43daSPeter Zijlstra 		rt_se->timeout = 0;
1376391e43daSPeter Zijlstra 
1377ff77e468SPeter Zijlstra 	enqueue_rt_entity(rt_se, flags);
1378391e43daSPeter Zijlstra 
13794b53a341SIngo Molnar 	if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1380391e43daSPeter Zijlstra 		enqueue_pushable_task(rq, p);
1381391e43daSPeter Zijlstra }
1382391e43daSPeter Zijlstra 
1383391e43daSPeter Zijlstra static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1384391e43daSPeter Zijlstra {
1385391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se = &p->rt;
1386391e43daSPeter Zijlstra 
1387391e43daSPeter Zijlstra 	update_curr_rt(rq);
1388ff77e468SPeter Zijlstra 	dequeue_rt_entity(rt_se, flags);
1389391e43daSPeter Zijlstra 
1390391e43daSPeter Zijlstra 	dequeue_pushable_task(rq, p);
1391391e43daSPeter Zijlstra }
1392391e43daSPeter Zijlstra 
1393391e43daSPeter Zijlstra /*
1394391e43daSPeter Zijlstra  * Put task to the head or the end of the run list without the overhead of
1395391e43daSPeter Zijlstra  * dequeue followed by enqueue.
1396391e43daSPeter Zijlstra  */
1397391e43daSPeter Zijlstra static void
1398391e43daSPeter Zijlstra requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
1399391e43daSPeter Zijlstra {
1400391e43daSPeter Zijlstra 	if (on_rt_rq(rt_se)) {
1401391e43daSPeter Zijlstra 		struct rt_prio_array *array = &rt_rq->active;
1402391e43daSPeter Zijlstra 		struct list_head *queue = array->queue + rt_se_prio(rt_se);
1403391e43daSPeter Zijlstra 
1404391e43daSPeter Zijlstra 		if (head)
1405391e43daSPeter Zijlstra 			list_move(&rt_se->run_list, queue);
1406391e43daSPeter Zijlstra 		else
1407391e43daSPeter Zijlstra 			list_move_tail(&rt_se->run_list, queue);
1408391e43daSPeter Zijlstra 	}
1409391e43daSPeter Zijlstra }
1410391e43daSPeter Zijlstra 
1411391e43daSPeter Zijlstra static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
1412391e43daSPeter Zijlstra {
1413391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se = &p->rt;
1414391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
1415391e43daSPeter Zijlstra 
1416391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se) {
1417391e43daSPeter Zijlstra 		rt_rq = rt_rq_of_se(rt_se);
1418391e43daSPeter Zijlstra 		requeue_rt_entity(rt_rq, rt_se, head);
1419391e43daSPeter Zijlstra 	}
1420391e43daSPeter Zijlstra }
1421391e43daSPeter Zijlstra 
1422391e43daSPeter Zijlstra static void yield_task_rt(struct rq *rq)
1423391e43daSPeter Zijlstra {
1424391e43daSPeter Zijlstra 	requeue_task_rt(rq, rq->curr, 0);
1425391e43daSPeter Zijlstra }
1426391e43daSPeter Zijlstra 
1427391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1428391e43daSPeter Zijlstra static int find_lowest_rq(struct task_struct *task);
1429391e43daSPeter Zijlstra 
1430391e43daSPeter Zijlstra static int
1431ac66f547SPeter Zijlstra select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
1432391e43daSPeter Zijlstra {
1433391e43daSPeter Zijlstra 	struct task_struct *curr;
1434391e43daSPeter Zijlstra 	struct rq *rq;
1435804d402fSQais Yousef 	bool test;
1436391e43daSPeter Zijlstra 
1437391e43daSPeter Zijlstra 	/* For anything but wake ups, just return the task_cpu */
1438391e43daSPeter Zijlstra 	if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1439391e43daSPeter Zijlstra 		goto out;
1440391e43daSPeter Zijlstra 
1441391e43daSPeter Zijlstra 	rq = cpu_rq(cpu);
1442391e43daSPeter Zijlstra 
1443391e43daSPeter Zijlstra 	rcu_read_lock();
1444316c1608SJason Low 	curr = READ_ONCE(rq->curr); /* unlocked access */
1445391e43daSPeter Zijlstra 
1446391e43daSPeter Zijlstra 	/*
1447391e43daSPeter Zijlstra 	 * If the current task on @p's runqueue is an RT task, then
1448391e43daSPeter Zijlstra 	 * try to see if we can wake this RT task up on another
1449391e43daSPeter Zijlstra 	 * runqueue. Otherwise simply start this RT task
1450391e43daSPeter Zijlstra 	 * on its current runqueue.
1451391e43daSPeter Zijlstra 	 *
1452391e43daSPeter Zijlstra 	 * We want to avoid overloading runqueues. If the woken
1453391e43daSPeter Zijlstra 	 * task is a higher priority, then it will stay on this CPU
1454391e43daSPeter Zijlstra 	 * and the lower prio task should be moved to another CPU.
1455391e43daSPeter Zijlstra 	 * Even though this will probably make the lower prio task
1456391e43daSPeter Zijlstra 	 * lose its cache, we do not want to bounce a higher task
1457391e43daSPeter Zijlstra 	 * around just because it gave up its CPU, perhaps for a
1458391e43daSPeter Zijlstra 	 * lock?
1459391e43daSPeter Zijlstra 	 *
1460391e43daSPeter Zijlstra 	 * For equal prio tasks, we just let the scheduler sort it out.
1461391e43daSPeter Zijlstra 	 *
1462391e43daSPeter Zijlstra 	 * Otherwise, just let it ride on the affined RQ and the
1463391e43daSPeter Zijlstra 	 * post-schedule router will push the preempted task away
1464391e43daSPeter Zijlstra 	 *
1465391e43daSPeter Zijlstra 	 * This test is optimistic, if we get it wrong the load-balancer
1466391e43daSPeter Zijlstra 	 * will have to sort it out.
1467804d402fSQais Yousef 	 *
1468804d402fSQais Yousef 	 * We take into account the capacity of the CPU to ensure it fits the
1469804d402fSQais Yousef 	 * requirement of the task - which is only important on heterogeneous
1470804d402fSQais Yousef 	 * systems like big.LITTLE.
1471391e43daSPeter Zijlstra 	 */
1472804d402fSQais Yousef 	test = curr &&
1473804d402fSQais Yousef 	       unlikely(rt_task(curr)) &&
1474804d402fSQais Yousef 	       (curr->nr_cpus_allowed < 2 || curr->prio <= p->prio);
1475804d402fSQais Yousef 
1476804d402fSQais Yousef 	if (test || !rt_task_fits_capacity(p, cpu)) {
1477391e43daSPeter Zijlstra 		int target = find_lowest_rq(p);
1478391e43daSPeter Zijlstra 
147980e3d87bSTim Chen 		/*
1480b28bc1e0SQais Yousef 		 * Bail out if we were forcing a migration to find a better
1481b28bc1e0SQais Yousef 		 * fitting CPU but our search failed.
1482b28bc1e0SQais Yousef 		 */
1483b28bc1e0SQais Yousef 		if (!test && target != -1 && !rt_task_fits_capacity(p, target))
1484b28bc1e0SQais Yousef 			goto out_unlock;
1485b28bc1e0SQais Yousef 
1486b28bc1e0SQais Yousef 		/*
148780e3d87bSTim Chen 		 * Don't bother moving it if the destination CPU is
148880e3d87bSTim Chen 		 * not running a lower priority task.
148980e3d87bSTim Chen 		 */
149080e3d87bSTim Chen 		if (target != -1 &&
149180e3d87bSTim Chen 		    p->prio < cpu_rq(target)->rt.highest_prio.curr)
1492391e43daSPeter Zijlstra 			cpu = target;
1493391e43daSPeter Zijlstra 	}
1494b28bc1e0SQais Yousef 
1495b28bc1e0SQais Yousef out_unlock:
1496391e43daSPeter Zijlstra 	rcu_read_unlock();
1497391e43daSPeter Zijlstra 
1498391e43daSPeter Zijlstra out:
1499391e43daSPeter Zijlstra 	return cpu;
1500391e43daSPeter Zijlstra }
1501391e43daSPeter Zijlstra 
1502391e43daSPeter Zijlstra static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1503391e43daSPeter Zijlstra {
1504308a623aSWanpeng Li 	/*
1505308a623aSWanpeng Li 	 * Current can't be migrated, useless to reschedule,
1506308a623aSWanpeng Li 	 * let's hope p can move out.
1507308a623aSWanpeng Li 	 */
15084b53a341SIngo Molnar 	if (rq->curr->nr_cpus_allowed == 1 ||
1509a1bd02e1SQais Yousef 	    !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1510391e43daSPeter Zijlstra 		return;
1511391e43daSPeter Zijlstra 
1512308a623aSWanpeng Li 	/*
1513308a623aSWanpeng Li 	 * p is migratable, so let's not schedule it and
1514308a623aSWanpeng Li 	 * see if it is pushed or pulled somewhere else.
1515308a623aSWanpeng Li 	 */
1516804d402fSQais Yousef 	if (p->nr_cpus_allowed != 1 &&
1517a1bd02e1SQais Yousef 	    cpupri_find(&rq->rd->cpupri, p, NULL))
1518391e43daSPeter Zijlstra 		return;
1519391e43daSPeter Zijlstra 
1520391e43daSPeter Zijlstra 	/*
152197fb7a0aSIngo Molnar 	 * There appear to be other CPUs that can accept
152297fb7a0aSIngo Molnar 	 * the current task but none can run 'p', so lets reschedule
152397fb7a0aSIngo Molnar 	 * to try and push the current task away:
1524391e43daSPeter Zijlstra 	 */
1525391e43daSPeter Zijlstra 	requeue_task_rt(rq, p, 1);
15268875125eSKirill Tkhai 	resched_curr(rq);
1527391e43daSPeter Zijlstra }
1528391e43daSPeter Zijlstra 
15296e2df058SPeter Zijlstra static int balance_rt(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
15306e2df058SPeter Zijlstra {
15316e2df058SPeter Zijlstra 	if (!on_rt_rq(&p->rt) && need_pull_rt_task(rq, p)) {
15326e2df058SPeter Zijlstra 		/*
15336e2df058SPeter Zijlstra 		 * This is OK, because current is on_cpu, which avoids it being
15346e2df058SPeter Zijlstra 		 * picked for load-balance and preemption/IRQs are still
15356e2df058SPeter Zijlstra 		 * disabled avoiding further scheduler activity on it and we've
15366e2df058SPeter Zijlstra 		 * not yet started the picking loop.
15376e2df058SPeter Zijlstra 		 */
15386e2df058SPeter Zijlstra 		rq_unpin_lock(rq, rf);
15396e2df058SPeter Zijlstra 		pull_rt_task(rq);
15406e2df058SPeter Zijlstra 		rq_repin_lock(rq, rf);
15416e2df058SPeter Zijlstra 	}
15426e2df058SPeter Zijlstra 
15436e2df058SPeter Zijlstra 	return sched_stop_runnable(rq) || sched_dl_runnable(rq) || sched_rt_runnable(rq);
15446e2df058SPeter Zijlstra }
1545391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
1546391e43daSPeter Zijlstra 
1547391e43daSPeter Zijlstra /*
1548391e43daSPeter Zijlstra  * Preempt the current task with a newly woken task if needed:
1549391e43daSPeter Zijlstra  */
1550391e43daSPeter Zijlstra static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
1551391e43daSPeter Zijlstra {
1552391e43daSPeter Zijlstra 	if (p->prio < rq->curr->prio) {
15538875125eSKirill Tkhai 		resched_curr(rq);
1554391e43daSPeter Zijlstra 		return;
1555391e43daSPeter Zijlstra 	}
1556391e43daSPeter Zijlstra 
1557391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1558391e43daSPeter Zijlstra 	/*
1559391e43daSPeter Zijlstra 	 * If:
1560391e43daSPeter Zijlstra 	 *
1561391e43daSPeter Zijlstra 	 * - the newly woken task is of equal priority to the current task
1562391e43daSPeter Zijlstra 	 * - the newly woken task is non-migratable while current is migratable
1563391e43daSPeter Zijlstra 	 * - current will be preempted on the next reschedule
1564391e43daSPeter Zijlstra 	 *
1565391e43daSPeter Zijlstra 	 * we should check to see if current can readily move to a different
1566391e43daSPeter Zijlstra 	 * cpu.  If so, we will reschedule to allow the push logic to try
1567391e43daSPeter Zijlstra 	 * to move current somewhere else, making room for our non-migratable
1568391e43daSPeter Zijlstra 	 * task.
1569391e43daSPeter Zijlstra 	 */
1570391e43daSPeter Zijlstra 	if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
1571391e43daSPeter Zijlstra 		check_preempt_equal_prio(rq, p);
1572391e43daSPeter Zijlstra #endif
1573391e43daSPeter Zijlstra }
1574391e43daSPeter Zijlstra 
1575a0e813f2SPeter Zijlstra static inline void set_next_task_rt(struct rq *rq, struct task_struct *p, bool first)
1576ff1cdc94SMuchun Song {
1577ff1cdc94SMuchun Song 	p->se.exec_start = rq_clock_task(rq);
1578ff1cdc94SMuchun Song 
1579ff1cdc94SMuchun Song 	/* The running task is never eligible for pushing */
1580ff1cdc94SMuchun Song 	dequeue_pushable_task(rq, p);
1581f95d4eaeSPeter Zijlstra 
1582a0e813f2SPeter Zijlstra 	if (!first)
1583a0e813f2SPeter Zijlstra 		return;
1584a0e813f2SPeter Zijlstra 
1585f95d4eaeSPeter Zijlstra 	/*
1586f95d4eaeSPeter Zijlstra 	 * If prev task was rt, put_prev_task() has already updated the
1587f95d4eaeSPeter Zijlstra 	 * utilization. We only care of the case where we start to schedule a
1588f95d4eaeSPeter Zijlstra 	 * rt task
1589f95d4eaeSPeter Zijlstra 	 */
1590f95d4eaeSPeter Zijlstra 	if (rq->curr->sched_class != &rt_sched_class)
1591f95d4eaeSPeter Zijlstra 		update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
1592f95d4eaeSPeter Zijlstra 
1593f95d4eaeSPeter Zijlstra 	rt_queue_push_tasks(rq);
1594ff1cdc94SMuchun Song }
1595ff1cdc94SMuchun Song 
1596391e43daSPeter Zijlstra static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1597391e43daSPeter Zijlstra 						   struct rt_rq *rt_rq)
1598391e43daSPeter Zijlstra {
1599391e43daSPeter Zijlstra 	struct rt_prio_array *array = &rt_rq->active;
1600391e43daSPeter Zijlstra 	struct sched_rt_entity *next = NULL;
1601391e43daSPeter Zijlstra 	struct list_head *queue;
1602391e43daSPeter Zijlstra 	int idx;
1603391e43daSPeter Zijlstra 
1604391e43daSPeter Zijlstra 	idx = sched_find_first_bit(array->bitmap);
1605391e43daSPeter Zijlstra 	BUG_ON(idx >= MAX_RT_PRIO);
1606391e43daSPeter Zijlstra 
1607391e43daSPeter Zijlstra 	queue = array->queue + idx;
1608391e43daSPeter Zijlstra 	next = list_entry(queue->next, struct sched_rt_entity, run_list);
1609391e43daSPeter Zijlstra 
1610391e43daSPeter Zijlstra 	return next;
1611391e43daSPeter Zijlstra }
1612391e43daSPeter Zijlstra 
1613391e43daSPeter Zijlstra static struct task_struct *_pick_next_task_rt(struct rq *rq)
1614391e43daSPeter Zijlstra {
1615391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se;
1616606dba2eSPeter Zijlstra 	struct rt_rq *rt_rq  = &rq->rt;
1617391e43daSPeter Zijlstra 
1618391e43daSPeter Zijlstra 	do {
1619391e43daSPeter Zijlstra 		rt_se = pick_next_rt_entity(rq, rt_rq);
1620391e43daSPeter Zijlstra 		BUG_ON(!rt_se);
1621391e43daSPeter Zijlstra 		rt_rq = group_rt_rq(rt_se);
1622391e43daSPeter Zijlstra 	} while (rt_rq);
1623391e43daSPeter Zijlstra 
1624ff1cdc94SMuchun Song 	return rt_task_of(rt_se);
1625391e43daSPeter Zijlstra }
1626391e43daSPeter Zijlstra 
162798c2f700SPeter Zijlstra static struct task_struct *pick_next_task_rt(struct rq *rq)
1628391e43daSPeter Zijlstra {
1629606dba2eSPeter Zijlstra 	struct task_struct *p;
1630606dba2eSPeter Zijlstra 
16316e2df058SPeter Zijlstra 	if (!sched_rt_runnable(rq))
1632606dba2eSPeter Zijlstra 		return NULL;
1633606dba2eSPeter Zijlstra 
1634606dba2eSPeter Zijlstra 	p = _pick_next_task_rt(rq);
1635a0e813f2SPeter Zijlstra 	set_next_task_rt(rq, p, true);
1636391e43daSPeter Zijlstra 	return p;
1637391e43daSPeter Zijlstra }
1638391e43daSPeter Zijlstra 
16396e2df058SPeter Zijlstra static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
1640391e43daSPeter Zijlstra {
1641391e43daSPeter Zijlstra 	update_curr_rt(rq);
1642391e43daSPeter Zijlstra 
164323127296SVincent Guittot 	update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
1644371bf427SVincent Guittot 
1645391e43daSPeter Zijlstra 	/*
1646391e43daSPeter Zijlstra 	 * The previous task needs to be made eligible for pushing
1647391e43daSPeter Zijlstra 	 * if it is still active
1648391e43daSPeter Zijlstra 	 */
16494b53a341SIngo Molnar 	if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
1650391e43daSPeter Zijlstra 		enqueue_pushable_task(rq, p);
1651391e43daSPeter Zijlstra }
1652391e43daSPeter Zijlstra 
1653391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1654391e43daSPeter Zijlstra 
1655391e43daSPeter Zijlstra /* Only try algorithms three times */
1656391e43daSPeter Zijlstra #define RT_MAX_TRIES 3
1657391e43daSPeter Zijlstra 
1658391e43daSPeter Zijlstra static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1659391e43daSPeter Zijlstra {
1660391e43daSPeter Zijlstra 	if (!task_running(rq, p) &&
166195158a89SPeter Zijlstra 	    cpumask_test_cpu(cpu, &p->cpus_mask))
1662391e43daSPeter Zijlstra 		return 1;
166397fb7a0aSIngo Molnar 
1664391e43daSPeter Zijlstra 	return 0;
1665391e43daSPeter Zijlstra }
1666391e43daSPeter Zijlstra 
1667e23ee747SKirill Tkhai /*
1668e23ee747SKirill Tkhai  * Return the highest pushable rq's task, which is suitable to be executed
166997fb7a0aSIngo Molnar  * on the CPU, NULL otherwise
1670e23ee747SKirill Tkhai  */
1671e23ee747SKirill Tkhai static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
1672391e43daSPeter Zijlstra {
1673e23ee747SKirill Tkhai 	struct plist_head *head = &rq->rt.pushable_tasks;
1674391e43daSPeter Zijlstra 	struct task_struct *p;
1675391e43daSPeter Zijlstra 
1676e23ee747SKirill Tkhai 	if (!has_pushable_tasks(rq))
1677e23ee747SKirill Tkhai 		return NULL;
1678391e43daSPeter Zijlstra 
1679e23ee747SKirill Tkhai 	plist_for_each_entry(p, head, pushable_tasks) {
1680e23ee747SKirill Tkhai 		if (pick_rt_task(rq, p, cpu))
1681e23ee747SKirill Tkhai 			return p;
1682391e43daSPeter Zijlstra 	}
1683391e43daSPeter Zijlstra 
1684e23ee747SKirill Tkhai 	return NULL;
1685391e43daSPeter Zijlstra }
1686391e43daSPeter Zijlstra 
1687391e43daSPeter Zijlstra static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
1688391e43daSPeter Zijlstra 
1689391e43daSPeter Zijlstra static int find_lowest_rq(struct task_struct *task)
1690391e43daSPeter Zijlstra {
1691391e43daSPeter Zijlstra 	struct sched_domain *sd;
16924ba29684SChristoph Lameter 	struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
1693391e43daSPeter Zijlstra 	int this_cpu = smp_processor_id();
1694391e43daSPeter Zijlstra 	int cpu      = task_cpu(task);
1695a1bd02e1SQais Yousef 	int ret;
1696391e43daSPeter Zijlstra 
1697391e43daSPeter Zijlstra 	/* Make sure the mask is initialized first */
1698391e43daSPeter Zijlstra 	if (unlikely(!lowest_mask))
1699391e43daSPeter Zijlstra 		return -1;
1700391e43daSPeter Zijlstra 
17014b53a341SIngo Molnar 	if (task->nr_cpus_allowed == 1)
1702391e43daSPeter Zijlstra 		return -1; /* No other targets possible */
1703391e43daSPeter Zijlstra 
1704a1bd02e1SQais Yousef 	/*
1705a1bd02e1SQais Yousef 	 * If we're on asym system ensure we consider the different capacities
1706a1bd02e1SQais Yousef 	 * of the CPUs when searching for the lowest_mask.
1707a1bd02e1SQais Yousef 	 */
1708a1bd02e1SQais Yousef 	if (static_branch_unlikely(&sched_asym_cpucapacity)) {
1709a1bd02e1SQais Yousef 
1710a1bd02e1SQais Yousef 		ret = cpupri_find_fitness(&task_rq(task)->rd->cpupri,
1711a1bd02e1SQais Yousef 					  task, lowest_mask,
1712a1bd02e1SQais Yousef 					  rt_task_fits_capacity);
1713a1bd02e1SQais Yousef 	} else {
1714a1bd02e1SQais Yousef 
1715a1bd02e1SQais Yousef 		ret = cpupri_find(&task_rq(task)->rd->cpupri,
1716a1bd02e1SQais Yousef 				  task, lowest_mask);
1717a1bd02e1SQais Yousef 	}
1718a1bd02e1SQais Yousef 
1719a1bd02e1SQais Yousef 	if (!ret)
1720391e43daSPeter Zijlstra 		return -1; /* No targets found */
1721391e43daSPeter Zijlstra 
1722391e43daSPeter Zijlstra 	/*
172397fb7a0aSIngo Molnar 	 * At this point we have built a mask of CPUs representing the
1724391e43daSPeter Zijlstra 	 * lowest priority tasks in the system.  Now we want to elect
1725391e43daSPeter Zijlstra 	 * the best one based on our affinity and topology.
1726391e43daSPeter Zijlstra 	 *
172797fb7a0aSIngo Molnar 	 * We prioritize the last CPU that the task executed on since
1728391e43daSPeter Zijlstra 	 * it is most likely cache-hot in that location.
1729391e43daSPeter Zijlstra 	 */
1730391e43daSPeter Zijlstra 	if (cpumask_test_cpu(cpu, lowest_mask))
1731391e43daSPeter Zijlstra 		return cpu;
1732391e43daSPeter Zijlstra 
1733391e43daSPeter Zijlstra 	/*
1734391e43daSPeter Zijlstra 	 * Otherwise, we consult the sched_domains span maps to figure
173597fb7a0aSIngo Molnar 	 * out which CPU is logically closest to our hot cache data.
1736391e43daSPeter Zijlstra 	 */
1737391e43daSPeter Zijlstra 	if (!cpumask_test_cpu(this_cpu, lowest_mask))
1738391e43daSPeter Zijlstra 		this_cpu = -1; /* Skip this_cpu opt if not among lowest */
1739391e43daSPeter Zijlstra 
1740391e43daSPeter Zijlstra 	rcu_read_lock();
1741391e43daSPeter Zijlstra 	for_each_domain(cpu, sd) {
1742391e43daSPeter Zijlstra 		if (sd->flags & SD_WAKE_AFFINE) {
1743391e43daSPeter Zijlstra 			int best_cpu;
1744391e43daSPeter Zijlstra 
1745391e43daSPeter Zijlstra 			/*
1746391e43daSPeter Zijlstra 			 * "this_cpu" is cheaper to preempt than a
1747391e43daSPeter Zijlstra 			 * remote processor.
1748391e43daSPeter Zijlstra 			 */
1749391e43daSPeter Zijlstra 			if (this_cpu != -1 &&
1750391e43daSPeter Zijlstra 			    cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1751391e43daSPeter Zijlstra 				rcu_read_unlock();
1752391e43daSPeter Zijlstra 				return this_cpu;
1753391e43daSPeter Zijlstra 			}
1754391e43daSPeter Zijlstra 
175514e292f8SPeter Zijlstra 			best_cpu = cpumask_any_and_distribute(lowest_mask,
1756391e43daSPeter Zijlstra 							      sched_domain_span(sd));
1757391e43daSPeter Zijlstra 			if (best_cpu < nr_cpu_ids) {
1758391e43daSPeter Zijlstra 				rcu_read_unlock();
1759391e43daSPeter Zijlstra 				return best_cpu;
1760391e43daSPeter Zijlstra 			}
1761391e43daSPeter Zijlstra 		}
1762391e43daSPeter Zijlstra 	}
1763391e43daSPeter Zijlstra 	rcu_read_unlock();
1764391e43daSPeter Zijlstra 
1765391e43daSPeter Zijlstra 	/*
1766391e43daSPeter Zijlstra 	 * And finally, if there were no matches within the domains
1767391e43daSPeter Zijlstra 	 * just give the caller *something* to work with from the compatible
1768391e43daSPeter Zijlstra 	 * locations.
1769391e43daSPeter Zijlstra 	 */
1770391e43daSPeter Zijlstra 	if (this_cpu != -1)
1771391e43daSPeter Zijlstra 		return this_cpu;
1772391e43daSPeter Zijlstra 
177314e292f8SPeter Zijlstra 	cpu = cpumask_any_distribute(lowest_mask);
1774391e43daSPeter Zijlstra 	if (cpu < nr_cpu_ids)
1775391e43daSPeter Zijlstra 		return cpu;
177697fb7a0aSIngo Molnar 
1777391e43daSPeter Zijlstra 	return -1;
1778391e43daSPeter Zijlstra }
1779391e43daSPeter Zijlstra 
1780391e43daSPeter Zijlstra /* Will lock the rq it finds */
1781391e43daSPeter Zijlstra static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
1782391e43daSPeter Zijlstra {
1783391e43daSPeter Zijlstra 	struct rq *lowest_rq = NULL;
1784391e43daSPeter Zijlstra 	int tries;
1785391e43daSPeter Zijlstra 	int cpu;
1786391e43daSPeter Zijlstra 
1787391e43daSPeter Zijlstra 	for (tries = 0; tries < RT_MAX_TRIES; tries++) {
1788391e43daSPeter Zijlstra 		cpu = find_lowest_rq(task);
1789391e43daSPeter Zijlstra 
1790391e43daSPeter Zijlstra 		if ((cpu == -1) || (cpu == rq->cpu))
1791391e43daSPeter Zijlstra 			break;
1792391e43daSPeter Zijlstra 
1793391e43daSPeter Zijlstra 		lowest_rq = cpu_rq(cpu);
1794391e43daSPeter Zijlstra 
179580e3d87bSTim Chen 		if (lowest_rq->rt.highest_prio.curr <= task->prio) {
179680e3d87bSTim Chen 			/*
179780e3d87bSTim Chen 			 * Target rq has tasks of equal or higher priority,
179880e3d87bSTim Chen 			 * retrying does not release any lock and is unlikely
179980e3d87bSTim Chen 			 * to yield a different result.
180080e3d87bSTim Chen 			 */
180180e3d87bSTim Chen 			lowest_rq = NULL;
180280e3d87bSTim Chen 			break;
180380e3d87bSTim Chen 		}
180480e3d87bSTim Chen 
1805391e43daSPeter Zijlstra 		/* if the prio of this runqueue changed, try again */
1806391e43daSPeter Zijlstra 		if (double_lock_balance(rq, lowest_rq)) {
1807391e43daSPeter Zijlstra 			/*
1808391e43daSPeter Zijlstra 			 * We had to unlock the run queue. In
1809391e43daSPeter Zijlstra 			 * the mean time, task could have
1810391e43daSPeter Zijlstra 			 * migrated already or had its affinity changed.
1811391e43daSPeter Zijlstra 			 * Also make sure that it wasn't scheduled on its rq.
1812391e43daSPeter Zijlstra 			 */
1813391e43daSPeter Zijlstra 			if (unlikely(task_rq(task) != rq ||
181495158a89SPeter Zijlstra 				     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_mask) ||
1815391e43daSPeter Zijlstra 				     task_running(rq, task) ||
181613b5ab02SXunlei Pang 				     !rt_task(task) ||
1817da0c1e65SKirill Tkhai 				     !task_on_rq_queued(task))) {
1818391e43daSPeter Zijlstra 
18197f1b4393SPeter Zijlstra 				double_unlock_balance(rq, lowest_rq);
1820391e43daSPeter Zijlstra 				lowest_rq = NULL;
1821391e43daSPeter Zijlstra 				break;
1822391e43daSPeter Zijlstra 			}
1823391e43daSPeter Zijlstra 		}
1824391e43daSPeter Zijlstra 
1825391e43daSPeter Zijlstra 		/* If this rq is still suitable use it. */
1826391e43daSPeter Zijlstra 		if (lowest_rq->rt.highest_prio.curr > task->prio)
1827391e43daSPeter Zijlstra 			break;
1828391e43daSPeter Zijlstra 
1829391e43daSPeter Zijlstra 		/* try again */
1830391e43daSPeter Zijlstra 		double_unlock_balance(rq, lowest_rq);
1831391e43daSPeter Zijlstra 		lowest_rq = NULL;
1832391e43daSPeter Zijlstra 	}
1833391e43daSPeter Zijlstra 
1834391e43daSPeter Zijlstra 	return lowest_rq;
1835391e43daSPeter Zijlstra }
1836391e43daSPeter Zijlstra 
1837391e43daSPeter Zijlstra static struct task_struct *pick_next_pushable_task(struct rq *rq)
1838391e43daSPeter Zijlstra {
1839391e43daSPeter Zijlstra 	struct task_struct *p;
1840391e43daSPeter Zijlstra 
1841391e43daSPeter Zijlstra 	if (!has_pushable_tasks(rq))
1842391e43daSPeter Zijlstra 		return NULL;
1843391e43daSPeter Zijlstra 
1844391e43daSPeter Zijlstra 	p = plist_first_entry(&rq->rt.pushable_tasks,
1845391e43daSPeter Zijlstra 			      struct task_struct, pushable_tasks);
1846391e43daSPeter Zijlstra 
1847391e43daSPeter Zijlstra 	BUG_ON(rq->cpu != task_cpu(p));
1848391e43daSPeter Zijlstra 	BUG_ON(task_current(rq, p));
18494b53a341SIngo Molnar 	BUG_ON(p->nr_cpus_allowed <= 1);
1850391e43daSPeter Zijlstra 
1851da0c1e65SKirill Tkhai 	BUG_ON(!task_on_rq_queued(p));
1852391e43daSPeter Zijlstra 	BUG_ON(!rt_task(p));
1853391e43daSPeter Zijlstra 
1854391e43daSPeter Zijlstra 	return p;
1855391e43daSPeter Zijlstra }
1856391e43daSPeter Zijlstra 
1857391e43daSPeter Zijlstra /*
1858391e43daSPeter Zijlstra  * If the current CPU has more than one RT task, see if the non
1859391e43daSPeter Zijlstra  * running task can migrate over to a CPU that is running a task
1860391e43daSPeter Zijlstra  * of lesser priority.
1861391e43daSPeter Zijlstra  */
1862*a7c81556SPeter Zijlstra static int push_rt_task(struct rq *rq, bool pull)
1863391e43daSPeter Zijlstra {
1864391e43daSPeter Zijlstra 	struct task_struct *next_task;
1865391e43daSPeter Zijlstra 	struct rq *lowest_rq;
1866391e43daSPeter Zijlstra 	int ret = 0;
1867391e43daSPeter Zijlstra 
1868391e43daSPeter Zijlstra 	if (!rq->rt.overloaded)
1869391e43daSPeter Zijlstra 		return 0;
1870391e43daSPeter Zijlstra 
1871391e43daSPeter Zijlstra 	next_task = pick_next_pushable_task(rq);
1872391e43daSPeter Zijlstra 	if (!next_task)
1873391e43daSPeter Zijlstra 		return 0;
1874391e43daSPeter Zijlstra 
1875391e43daSPeter Zijlstra retry:
1876*a7c81556SPeter Zijlstra 	if (is_migration_disabled(next_task)) {
1877*a7c81556SPeter Zijlstra 		struct task_struct *push_task = NULL;
1878*a7c81556SPeter Zijlstra 		int cpu;
1879*a7c81556SPeter Zijlstra 
1880*a7c81556SPeter Zijlstra 		if (!pull || rq->push_busy)
1881*a7c81556SPeter Zijlstra 			return 0;
1882*a7c81556SPeter Zijlstra 
1883*a7c81556SPeter Zijlstra 		cpu = find_lowest_rq(rq->curr);
1884*a7c81556SPeter Zijlstra 		if (cpu == -1 || cpu == rq->cpu)
1885*a7c81556SPeter Zijlstra 			return 0;
1886*a7c81556SPeter Zijlstra 
1887*a7c81556SPeter Zijlstra 		/*
1888*a7c81556SPeter Zijlstra 		 * Given we found a CPU with lower priority than @next_task,
1889*a7c81556SPeter Zijlstra 		 * therefore it should be running. However we cannot migrate it
1890*a7c81556SPeter Zijlstra 		 * to this other CPU, instead attempt to push the current
1891*a7c81556SPeter Zijlstra 		 * running task on this CPU away.
1892*a7c81556SPeter Zijlstra 		 */
1893*a7c81556SPeter Zijlstra 		push_task = get_push_task(rq);
1894*a7c81556SPeter Zijlstra 		if (push_task) {
1895*a7c81556SPeter Zijlstra 			raw_spin_unlock(&rq->lock);
1896*a7c81556SPeter Zijlstra 			stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
1897*a7c81556SPeter Zijlstra 					    push_task, &rq->push_work);
1898*a7c81556SPeter Zijlstra 			raw_spin_lock(&rq->lock);
1899*a7c81556SPeter Zijlstra 		}
1900*a7c81556SPeter Zijlstra 
1901*a7c81556SPeter Zijlstra 		return 0;
1902*a7c81556SPeter Zijlstra 	}
1903*a7c81556SPeter Zijlstra 
19049ebc6053SYangtao Li 	if (WARN_ON(next_task == rq->curr))
1905391e43daSPeter Zijlstra 		return 0;
1906391e43daSPeter Zijlstra 
1907391e43daSPeter Zijlstra 	/*
1908391e43daSPeter Zijlstra 	 * It's possible that the next_task slipped in of
1909391e43daSPeter Zijlstra 	 * higher priority than current. If that's the case
1910391e43daSPeter Zijlstra 	 * just reschedule current.
1911391e43daSPeter Zijlstra 	 */
1912391e43daSPeter Zijlstra 	if (unlikely(next_task->prio < rq->curr->prio)) {
19138875125eSKirill Tkhai 		resched_curr(rq);
1914391e43daSPeter Zijlstra 		return 0;
1915391e43daSPeter Zijlstra 	}
1916391e43daSPeter Zijlstra 
1917391e43daSPeter Zijlstra 	/* We might release rq lock */
1918391e43daSPeter Zijlstra 	get_task_struct(next_task);
1919391e43daSPeter Zijlstra 
1920391e43daSPeter Zijlstra 	/* find_lock_lowest_rq locks the rq if found */
1921391e43daSPeter Zijlstra 	lowest_rq = find_lock_lowest_rq(next_task, rq);
1922391e43daSPeter Zijlstra 	if (!lowest_rq) {
1923391e43daSPeter Zijlstra 		struct task_struct *task;
1924391e43daSPeter Zijlstra 		/*
1925391e43daSPeter Zijlstra 		 * find_lock_lowest_rq releases rq->lock
1926391e43daSPeter Zijlstra 		 * so it is possible that next_task has migrated.
1927391e43daSPeter Zijlstra 		 *
1928391e43daSPeter Zijlstra 		 * We need to make sure that the task is still on the same
1929391e43daSPeter Zijlstra 		 * run-queue and is also still the next task eligible for
1930391e43daSPeter Zijlstra 		 * pushing.
1931391e43daSPeter Zijlstra 		 */
1932391e43daSPeter Zijlstra 		task = pick_next_pushable_task(rq);
1933de16b91eSByungchul Park 		if (task == next_task) {
1934391e43daSPeter Zijlstra 			/*
1935391e43daSPeter Zijlstra 			 * The task hasn't migrated, and is still the next
1936391e43daSPeter Zijlstra 			 * eligible task, but we failed to find a run-queue
1937391e43daSPeter Zijlstra 			 * to push it to.  Do not retry in this case, since
193897fb7a0aSIngo Molnar 			 * other CPUs will pull from us when ready.
1939391e43daSPeter Zijlstra 			 */
1940391e43daSPeter Zijlstra 			goto out;
1941391e43daSPeter Zijlstra 		}
1942391e43daSPeter Zijlstra 
1943391e43daSPeter Zijlstra 		if (!task)
1944391e43daSPeter Zijlstra 			/* No more tasks, just exit */
1945391e43daSPeter Zijlstra 			goto out;
1946391e43daSPeter Zijlstra 
1947391e43daSPeter Zijlstra 		/*
1948391e43daSPeter Zijlstra 		 * Something has shifted, try again.
1949391e43daSPeter Zijlstra 		 */
1950391e43daSPeter Zijlstra 		put_task_struct(next_task);
1951391e43daSPeter Zijlstra 		next_task = task;
1952391e43daSPeter Zijlstra 		goto retry;
1953391e43daSPeter Zijlstra 	}
1954391e43daSPeter Zijlstra 
1955391e43daSPeter Zijlstra 	deactivate_task(rq, next_task, 0);
1956391e43daSPeter Zijlstra 	set_task_cpu(next_task, lowest_rq->cpu);
1957391e43daSPeter Zijlstra 	activate_task(lowest_rq, next_task, 0);
1958*a7c81556SPeter Zijlstra 	resched_curr(lowest_rq);
1959391e43daSPeter Zijlstra 	ret = 1;
1960391e43daSPeter Zijlstra 
1961391e43daSPeter Zijlstra 	double_unlock_balance(rq, lowest_rq);
1962391e43daSPeter Zijlstra out:
1963391e43daSPeter Zijlstra 	put_task_struct(next_task);
1964391e43daSPeter Zijlstra 
1965391e43daSPeter Zijlstra 	return ret;
1966391e43daSPeter Zijlstra }
1967391e43daSPeter Zijlstra 
1968391e43daSPeter Zijlstra static void push_rt_tasks(struct rq *rq)
1969391e43daSPeter Zijlstra {
1970391e43daSPeter Zijlstra 	/* push_rt_task will return true if it moved an RT */
1971*a7c81556SPeter Zijlstra 	while (push_rt_task(rq, false))
1972391e43daSPeter Zijlstra 		;
1973391e43daSPeter Zijlstra }
1974391e43daSPeter Zijlstra 
1975b6366f04SSteven Rostedt #ifdef HAVE_RT_PUSH_IPI
1976b6366f04SSteven Rostedt 
19773e777f99SSteven Rostedt (VMware) /*
19783e777f99SSteven Rostedt (VMware)  * When a high priority task schedules out from a CPU and a lower priority
19793e777f99SSteven Rostedt (VMware)  * task is scheduled in, a check is made to see if there's any RT tasks
19803e777f99SSteven Rostedt (VMware)  * on other CPUs that are waiting to run because a higher priority RT task
19813e777f99SSteven Rostedt (VMware)  * is currently running on its CPU. In this case, the CPU with multiple RT
19823e777f99SSteven Rostedt (VMware)  * tasks queued on it (overloaded) needs to be notified that a CPU has opened
19833e777f99SSteven Rostedt (VMware)  * up that may be able to run one of its non-running queued RT tasks.
19843e777f99SSteven Rostedt (VMware)  *
19854bdced5cSSteven Rostedt (Red Hat)  * All CPUs with overloaded RT tasks need to be notified as there is currently
19864bdced5cSSteven Rostedt (Red Hat)  * no way to know which of these CPUs have the highest priority task waiting
19874bdced5cSSteven Rostedt (Red Hat)  * to run. Instead of trying to take a spinlock on each of these CPUs,
19884bdced5cSSteven Rostedt (Red Hat)  * which has shown to cause large latency when done on machines with many
19894bdced5cSSteven Rostedt (Red Hat)  * CPUs, sending an IPI to the CPUs to have them push off the overloaded
19904bdced5cSSteven Rostedt (Red Hat)  * RT tasks waiting to run.
19913e777f99SSteven Rostedt (VMware)  *
19924bdced5cSSteven Rostedt (Red Hat)  * Just sending an IPI to each of the CPUs is also an issue, as on large
19934bdced5cSSteven Rostedt (Red Hat)  * count CPU machines, this can cause an IPI storm on a CPU, especially
19944bdced5cSSteven Rostedt (Red Hat)  * if its the only CPU with multiple RT tasks queued, and a large number
19954bdced5cSSteven Rostedt (Red Hat)  * of CPUs scheduling a lower priority task at the same time.
19963e777f99SSteven Rostedt (VMware)  *
19974bdced5cSSteven Rostedt (Red Hat)  * Each root domain has its own irq work function that can iterate over
19984bdced5cSSteven Rostedt (Red Hat)  * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
19994bdced5cSSteven Rostedt (Red Hat)  * tassk must be checked if there's one or many CPUs that are lowering
20004bdced5cSSteven Rostedt (Red Hat)  * their priority, there's a single irq work iterator that will try to
20014bdced5cSSteven Rostedt (Red Hat)  * push off RT tasks that are waiting to run.
20023e777f99SSteven Rostedt (VMware)  *
20034bdced5cSSteven Rostedt (Red Hat)  * When a CPU schedules a lower priority task, it will kick off the
20044bdced5cSSteven Rostedt (Red Hat)  * irq work iterator that will jump to each CPU with overloaded RT tasks.
20054bdced5cSSteven Rostedt (Red Hat)  * As it only takes the first CPU that schedules a lower priority task
20064bdced5cSSteven Rostedt (Red Hat)  * to start the process, the rto_start variable is incremented and if
20074bdced5cSSteven Rostedt (Red Hat)  * the atomic result is one, then that CPU will try to take the rto_lock.
20084bdced5cSSteven Rostedt (Red Hat)  * This prevents high contention on the lock as the process handles all
20094bdced5cSSteven Rostedt (Red Hat)  * CPUs scheduling lower priority tasks.
20103e777f99SSteven Rostedt (VMware)  *
20114bdced5cSSteven Rostedt (Red Hat)  * All CPUs that are scheduling a lower priority task will increment the
20124bdced5cSSteven Rostedt (Red Hat)  * rt_loop_next variable. This will make sure that the irq work iterator
20134bdced5cSSteven Rostedt (Red Hat)  * checks all RT overloaded CPUs whenever a CPU schedules a new lower
20144bdced5cSSteven Rostedt (Red Hat)  * priority task, even if the iterator is in the middle of a scan. Incrementing
20154bdced5cSSteven Rostedt (Red Hat)  * the rt_loop_next will cause the iterator to perform another scan.
20163e777f99SSteven Rostedt (VMware)  *
20173e777f99SSteven Rostedt (VMware)  */
2018ad0f1d9dSSteven Rostedt (VMware) static int rto_next_cpu(struct root_domain *rd)
2019b6366f04SSteven Rostedt {
20204bdced5cSSteven Rostedt (Red Hat) 	int next;
2021b6366f04SSteven Rostedt 	int cpu;
2022b6366f04SSteven Rostedt 
2023b6366f04SSteven Rostedt 	/*
20244bdced5cSSteven Rostedt (Red Hat) 	 * When starting the IPI RT pushing, the rto_cpu is set to -1,
20254bdced5cSSteven Rostedt (Red Hat) 	 * rt_next_cpu() will simply return the first CPU found in
20264bdced5cSSteven Rostedt (Red Hat) 	 * the rto_mask.
20274bdced5cSSteven Rostedt (Red Hat) 	 *
202897fb7a0aSIngo Molnar 	 * If rto_next_cpu() is called with rto_cpu is a valid CPU, it
20294bdced5cSSteven Rostedt (Red Hat) 	 * will return the next CPU found in the rto_mask.
20304bdced5cSSteven Rostedt (Red Hat) 	 *
20314bdced5cSSteven Rostedt (Red Hat) 	 * If there are no more CPUs left in the rto_mask, then a check is made
20324bdced5cSSteven Rostedt (Red Hat) 	 * against rto_loop and rto_loop_next. rto_loop is only updated with
20334bdced5cSSteven Rostedt (Red Hat) 	 * the rto_lock held, but any CPU may increment the rto_loop_next
20344bdced5cSSteven Rostedt (Red Hat) 	 * without any locking.
2035b6366f04SSteven Rostedt 	 */
20364bdced5cSSteven Rostedt (Red Hat) 	for (;;) {
20374bdced5cSSteven Rostedt (Red Hat) 
20384bdced5cSSteven Rostedt (Red Hat) 		/* When rto_cpu is -1 this acts like cpumask_first() */
20394bdced5cSSteven Rostedt (Red Hat) 		cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
20404bdced5cSSteven Rostedt (Red Hat) 
20414bdced5cSSteven Rostedt (Red Hat) 		rd->rto_cpu = cpu;
20424bdced5cSSteven Rostedt (Red Hat) 
20434bdced5cSSteven Rostedt (Red Hat) 		if (cpu < nr_cpu_ids)
20444bdced5cSSteven Rostedt (Red Hat) 			return cpu;
20454bdced5cSSteven Rostedt (Red Hat) 
20464bdced5cSSteven Rostedt (Red Hat) 		rd->rto_cpu = -1;
20474bdced5cSSteven Rostedt (Red Hat) 
20484bdced5cSSteven Rostedt (Red Hat) 		/*
20494bdced5cSSteven Rostedt (Red Hat) 		 * ACQUIRE ensures we see the @rto_mask changes
20504bdced5cSSteven Rostedt (Red Hat) 		 * made prior to the @next value observed.
20514bdced5cSSteven Rostedt (Red Hat) 		 *
20524bdced5cSSteven Rostedt (Red Hat) 		 * Matches WMB in rt_set_overload().
20534bdced5cSSteven Rostedt (Red Hat) 		 */
20544bdced5cSSteven Rostedt (Red Hat) 		next = atomic_read_acquire(&rd->rto_loop_next);
20554bdced5cSSteven Rostedt (Red Hat) 
20564bdced5cSSteven Rostedt (Red Hat) 		if (rd->rto_loop == next)
20574bdced5cSSteven Rostedt (Red Hat) 			break;
20584bdced5cSSteven Rostedt (Red Hat) 
20594bdced5cSSteven Rostedt (Red Hat) 		rd->rto_loop = next;
2060b6366f04SSteven Rostedt 	}
2061b6366f04SSteven Rostedt 
20624bdced5cSSteven Rostedt (Red Hat) 	return -1;
20634bdced5cSSteven Rostedt (Red Hat) }
2064b6366f04SSteven Rostedt 
20654bdced5cSSteven Rostedt (Red Hat) static inline bool rto_start_trylock(atomic_t *v)
20664bdced5cSSteven Rostedt (Red Hat) {
20674bdced5cSSteven Rostedt (Red Hat) 	return !atomic_cmpxchg_acquire(v, 0, 1);
20684bdced5cSSteven Rostedt (Red Hat) }
20694bdced5cSSteven Rostedt (Red Hat) 
20704bdced5cSSteven Rostedt (Red Hat) static inline void rto_start_unlock(atomic_t *v)
20714bdced5cSSteven Rostedt (Red Hat) {
20724bdced5cSSteven Rostedt (Red Hat) 	atomic_set_release(v, 0);
20734bdced5cSSteven Rostedt (Red Hat) }
20744bdced5cSSteven Rostedt (Red Hat) 
20754bdced5cSSteven Rostedt (Red Hat) static void tell_cpu_to_push(struct rq *rq)
20764bdced5cSSteven Rostedt (Red Hat) {
20774bdced5cSSteven Rostedt (Red Hat) 	int cpu = -1;
20784bdced5cSSteven Rostedt (Red Hat) 
20794bdced5cSSteven Rostedt (Red Hat) 	/* Keep the loop going if the IPI is currently active */
20804bdced5cSSteven Rostedt (Red Hat) 	atomic_inc(&rq->rd->rto_loop_next);
20814bdced5cSSteven Rostedt (Red Hat) 
20824bdced5cSSteven Rostedt (Red Hat) 	/* Only one CPU can initiate a loop at a time */
20834bdced5cSSteven Rostedt (Red Hat) 	if (!rto_start_trylock(&rq->rd->rto_loop_start))
2084b6366f04SSteven Rostedt 		return;
2085b6366f04SSteven Rostedt 
20864bdced5cSSteven Rostedt (Red Hat) 	raw_spin_lock(&rq->rd->rto_lock);
2087b6366f04SSteven Rostedt 
20884bdced5cSSteven Rostedt (Red Hat) 	/*
208997fb7a0aSIngo Molnar 	 * The rto_cpu is updated under the lock, if it has a valid CPU
20904bdced5cSSteven Rostedt (Red Hat) 	 * then the IPI is still running and will continue due to the
20914bdced5cSSteven Rostedt (Red Hat) 	 * update to loop_next, and nothing needs to be done here.
20924bdced5cSSteven Rostedt (Red Hat) 	 * Otherwise it is finishing up and an ipi needs to be sent.
20934bdced5cSSteven Rostedt (Red Hat) 	 */
20944bdced5cSSteven Rostedt (Red Hat) 	if (rq->rd->rto_cpu < 0)
2095ad0f1d9dSSteven Rostedt (VMware) 		cpu = rto_next_cpu(rq->rd);
20964bdced5cSSteven Rostedt (Red Hat) 
20974bdced5cSSteven Rostedt (Red Hat) 	raw_spin_unlock(&rq->rd->rto_lock);
20984bdced5cSSteven Rostedt (Red Hat) 
20994bdced5cSSteven Rostedt (Red Hat) 	rto_start_unlock(&rq->rd->rto_loop_start);
21004bdced5cSSteven Rostedt (Red Hat) 
2101364f5665SSteven Rostedt (VMware) 	if (cpu >= 0) {
2102364f5665SSteven Rostedt (VMware) 		/* Make sure the rd does not get freed while pushing */
2103364f5665SSteven Rostedt (VMware) 		sched_get_rd(rq->rd);
21044bdced5cSSteven Rostedt (Red Hat) 		irq_work_queue_on(&rq->rd->rto_push_work, cpu);
2105b6366f04SSteven Rostedt 	}
2106364f5665SSteven Rostedt (VMware) }
2107b6366f04SSteven Rostedt 
2108b6366f04SSteven Rostedt /* Called from hardirq context */
21094bdced5cSSteven Rostedt (Red Hat) void rto_push_irq_work_func(struct irq_work *work)
2110b6366f04SSteven Rostedt {
2111ad0f1d9dSSteven Rostedt (VMware) 	struct root_domain *rd =
2112ad0f1d9dSSteven Rostedt (VMware) 		container_of(work, struct root_domain, rto_push_work);
21134bdced5cSSteven Rostedt (Red Hat) 	struct rq *rq;
2114b6366f04SSteven Rostedt 	int cpu;
2115b6366f04SSteven Rostedt 
21164bdced5cSSteven Rostedt (Red Hat) 	rq = this_rq();
2117b6366f04SSteven Rostedt 
21184bdced5cSSteven Rostedt (Red Hat) 	/*
21194bdced5cSSteven Rostedt (Red Hat) 	 * We do not need to grab the lock to check for has_pushable_tasks.
21204bdced5cSSteven Rostedt (Red Hat) 	 * When it gets updated, a check is made if a push is possible.
21214bdced5cSSteven Rostedt (Red Hat) 	 */
2122b6366f04SSteven Rostedt 	if (has_pushable_tasks(rq)) {
2123b6366f04SSteven Rostedt 		raw_spin_lock(&rq->lock);
2124*a7c81556SPeter Zijlstra 		while (push_rt_task(rq, true))
2125*a7c81556SPeter Zijlstra 			;
2126b6366f04SSteven Rostedt 		raw_spin_unlock(&rq->lock);
2127b6366f04SSteven Rostedt 	}
2128b6366f04SSteven Rostedt 
2129ad0f1d9dSSteven Rostedt (VMware) 	raw_spin_lock(&rd->rto_lock);
21304bdced5cSSteven Rostedt (Red Hat) 
2131b6366f04SSteven Rostedt 	/* Pass the IPI to the next rt overloaded queue */
2132ad0f1d9dSSteven Rostedt (VMware) 	cpu = rto_next_cpu(rd);
2133b6366f04SSteven Rostedt 
2134ad0f1d9dSSteven Rostedt (VMware) 	raw_spin_unlock(&rd->rto_lock);
2135b6366f04SSteven Rostedt 
2136364f5665SSteven Rostedt (VMware) 	if (cpu < 0) {
2137364f5665SSteven Rostedt (VMware) 		sched_put_rd(rd);
2138b6366f04SSteven Rostedt 		return;
2139364f5665SSteven Rostedt (VMware) 	}
2140b6366f04SSteven Rostedt 
2141b6366f04SSteven Rostedt 	/* Try the next RT overloaded CPU */
2142ad0f1d9dSSteven Rostedt (VMware) 	irq_work_queue_on(&rd->rto_push_work, cpu);
2143b6366f04SSteven Rostedt }
2144b6366f04SSteven Rostedt #endif /* HAVE_RT_PUSH_IPI */
2145b6366f04SSteven Rostedt 
21468046d680SPeter Zijlstra static void pull_rt_task(struct rq *this_rq)
2147391e43daSPeter Zijlstra {
21488046d680SPeter Zijlstra 	int this_cpu = this_rq->cpu, cpu;
21498046d680SPeter Zijlstra 	bool resched = false;
2150*a7c81556SPeter Zijlstra 	struct task_struct *p, *push_task;
2151391e43daSPeter Zijlstra 	struct rq *src_rq;
2152f73c52a5SSteven Rostedt 	int rt_overload_count = rt_overloaded(this_rq);
2153391e43daSPeter Zijlstra 
2154f73c52a5SSteven Rostedt 	if (likely(!rt_overload_count))
21558046d680SPeter Zijlstra 		return;
2156391e43daSPeter Zijlstra 
21577c3f2ab7SPeter Zijlstra 	/*
21587c3f2ab7SPeter Zijlstra 	 * Match the barrier from rt_set_overloaded; this guarantees that if we
21597c3f2ab7SPeter Zijlstra 	 * see overloaded we must also see the rto_mask bit.
21607c3f2ab7SPeter Zijlstra 	 */
21617c3f2ab7SPeter Zijlstra 	smp_rmb();
21627c3f2ab7SPeter Zijlstra 
2163f73c52a5SSteven Rostedt 	/* If we are the only overloaded CPU do nothing */
2164f73c52a5SSteven Rostedt 	if (rt_overload_count == 1 &&
2165f73c52a5SSteven Rostedt 	    cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
2166f73c52a5SSteven Rostedt 		return;
2167f73c52a5SSteven Rostedt 
2168b6366f04SSteven Rostedt #ifdef HAVE_RT_PUSH_IPI
2169b6366f04SSteven Rostedt 	if (sched_feat(RT_PUSH_IPI)) {
2170b6366f04SSteven Rostedt 		tell_cpu_to_push(this_rq);
21718046d680SPeter Zijlstra 		return;
2172b6366f04SSteven Rostedt 	}
2173b6366f04SSteven Rostedt #endif
2174b6366f04SSteven Rostedt 
2175391e43daSPeter Zijlstra 	for_each_cpu(cpu, this_rq->rd->rto_mask) {
2176391e43daSPeter Zijlstra 		if (this_cpu == cpu)
2177391e43daSPeter Zijlstra 			continue;
2178391e43daSPeter Zijlstra 
2179391e43daSPeter Zijlstra 		src_rq = cpu_rq(cpu);
2180391e43daSPeter Zijlstra 
2181391e43daSPeter Zijlstra 		/*
2182391e43daSPeter Zijlstra 		 * Don't bother taking the src_rq->lock if the next highest
2183391e43daSPeter Zijlstra 		 * task is known to be lower-priority than our current task.
2184391e43daSPeter Zijlstra 		 * This may look racy, but if this value is about to go
2185391e43daSPeter Zijlstra 		 * logically higher, the src_rq will push this task away.
2186391e43daSPeter Zijlstra 		 * And if its going logically lower, we do not care
2187391e43daSPeter Zijlstra 		 */
2188391e43daSPeter Zijlstra 		if (src_rq->rt.highest_prio.next >=
2189391e43daSPeter Zijlstra 		    this_rq->rt.highest_prio.curr)
2190391e43daSPeter Zijlstra 			continue;
2191391e43daSPeter Zijlstra 
2192391e43daSPeter Zijlstra 		/*
2193391e43daSPeter Zijlstra 		 * We can potentially drop this_rq's lock in
2194391e43daSPeter Zijlstra 		 * double_lock_balance, and another CPU could
2195391e43daSPeter Zijlstra 		 * alter this_rq
2196391e43daSPeter Zijlstra 		 */
2197*a7c81556SPeter Zijlstra 		push_task = NULL;
2198391e43daSPeter Zijlstra 		double_lock_balance(this_rq, src_rq);
2199391e43daSPeter Zijlstra 
2200391e43daSPeter Zijlstra 		/*
2201e23ee747SKirill Tkhai 		 * We can pull only a task, which is pushable
2202e23ee747SKirill Tkhai 		 * on its rq, and no others.
2203391e43daSPeter Zijlstra 		 */
2204e23ee747SKirill Tkhai 		p = pick_highest_pushable_task(src_rq, this_cpu);
2205391e43daSPeter Zijlstra 
2206391e43daSPeter Zijlstra 		/*
2207391e43daSPeter Zijlstra 		 * Do we have an RT task that preempts
2208391e43daSPeter Zijlstra 		 * the to-be-scheduled task?
2209391e43daSPeter Zijlstra 		 */
2210391e43daSPeter Zijlstra 		if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2211391e43daSPeter Zijlstra 			WARN_ON(p == src_rq->curr);
2212da0c1e65SKirill Tkhai 			WARN_ON(!task_on_rq_queued(p));
2213391e43daSPeter Zijlstra 
2214391e43daSPeter Zijlstra 			/*
2215391e43daSPeter Zijlstra 			 * There's a chance that p is higher in priority
221697fb7a0aSIngo Molnar 			 * than what's currently running on its CPU.
2217391e43daSPeter Zijlstra 			 * This is just that p is wakeing up and hasn't
2218391e43daSPeter Zijlstra 			 * had a chance to schedule. We only pull
2219391e43daSPeter Zijlstra 			 * p if it is lower in priority than the
2220391e43daSPeter Zijlstra 			 * current task on the run queue
2221391e43daSPeter Zijlstra 			 */
2222391e43daSPeter Zijlstra 			if (p->prio < src_rq->curr->prio)
2223391e43daSPeter Zijlstra 				goto skip;
2224391e43daSPeter Zijlstra 
2225*a7c81556SPeter Zijlstra 			if (is_migration_disabled(p)) {
2226*a7c81556SPeter Zijlstra 				push_task = get_push_task(src_rq);
2227*a7c81556SPeter Zijlstra 			} else {
2228391e43daSPeter Zijlstra 				deactivate_task(src_rq, p, 0);
2229391e43daSPeter Zijlstra 				set_task_cpu(p, this_cpu);
2230391e43daSPeter Zijlstra 				activate_task(this_rq, p, 0);
2231*a7c81556SPeter Zijlstra 				resched = true;
2232*a7c81556SPeter Zijlstra 			}
2233391e43daSPeter Zijlstra 			/*
2234391e43daSPeter Zijlstra 			 * We continue with the search, just in
2235391e43daSPeter Zijlstra 			 * case there's an even higher prio task
2236391e43daSPeter Zijlstra 			 * in another runqueue. (low likelihood
2237391e43daSPeter Zijlstra 			 * but possible)
2238391e43daSPeter Zijlstra 			 */
2239391e43daSPeter Zijlstra 		}
2240391e43daSPeter Zijlstra skip:
2241391e43daSPeter Zijlstra 		double_unlock_balance(this_rq, src_rq);
2242*a7c81556SPeter Zijlstra 
2243*a7c81556SPeter Zijlstra 		if (push_task) {
2244*a7c81556SPeter Zijlstra 			raw_spin_unlock(&this_rq->lock);
2245*a7c81556SPeter Zijlstra 			stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2246*a7c81556SPeter Zijlstra 					    push_task, &src_rq->push_work);
2247*a7c81556SPeter Zijlstra 			raw_spin_lock(&this_rq->lock);
2248*a7c81556SPeter Zijlstra 		}
2249391e43daSPeter Zijlstra 	}
2250391e43daSPeter Zijlstra 
22518046d680SPeter Zijlstra 	if (resched)
22528046d680SPeter Zijlstra 		resched_curr(this_rq);
2253391e43daSPeter Zijlstra }
2254391e43daSPeter Zijlstra 
2255391e43daSPeter Zijlstra /*
2256391e43daSPeter Zijlstra  * If we are not running and we are not going to reschedule soon, we should
2257391e43daSPeter Zijlstra  * try to push tasks away now
2258391e43daSPeter Zijlstra  */
2259391e43daSPeter Zijlstra static void task_woken_rt(struct rq *rq, struct task_struct *p)
2260391e43daSPeter Zijlstra {
2261804d402fSQais Yousef 	bool need_to_push = !task_running(rq, p) &&
2262391e43daSPeter Zijlstra 			    !test_tsk_need_resched(rq->curr) &&
22634b53a341SIngo Molnar 			    p->nr_cpus_allowed > 1 &&
22641baca4ceSJuri Lelli 			    (dl_task(rq->curr) || rt_task(rq->curr)) &&
22654b53a341SIngo Molnar 			    (rq->curr->nr_cpus_allowed < 2 ||
2266804d402fSQais Yousef 			     rq->curr->prio <= p->prio);
2267804d402fSQais Yousef 
2268d94a9df4SQais Yousef 	if (need_to_push)
2269391e43daSPeter Zijlstra 		push_rt_tasks(rq);
2270391e43daSPeter Zijlstra }
2271391e43daSPeter Zijlstra 
2272391e43daSPeter Zijlstra /* Assumes rq->lock is held */
2273391e43daSPeter Zijlstra static void rq_online_rt(struct rq *rq)
2274391e43daSPeter Zijlstra {
2275391e43daSPeter Zijlstra 	if (rq->rt.overloaded)
2276391e43daSPeter Zijlstra 		rt_set_overload(rq);
2277391e43daSPeter Zijlstra 
2278391e43daSPeter Zijlstra 	__enable_runtime(rq);
2279391e43daSPeter Zijlstra 
2280391e43daSPeter Zijlstra 	cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
2281391e43daSPeter Zijlstra }
2282391e43daSPeter Zijlstra 
2283391e43daSPeter Zijlstra /* Assumes rq->lock is held */
2284391e43daSPeter Zijlstra static void rq_offline_rt(struct rq *rq)
2285391e43daSPeter Zijlstra {
2286391e43daSPeter Zijlstra 	if (rq->rt.overloaded)
2287391e43daSPeter Zijlstra 		rt_clear_overload(rq);
2288391e43daSPeter Zijlstra 
2289391e43daSPeter Zijlstra 	__disable_runtime(rq);
2290391e43daSPeter Zijlstra 
2291391e43daSPeter Zijlstra 	cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
2292391e43daSPeter Zijlstra }
2293391e43daSPeter Zijlstra 
2294391e43daSPeter Zijlstra /*
2295391e43daSPeter Zijlstra  * When switch from the rt queue, we bring ourselves to a position
2296391e43daSPeter Zijlstra  * that we might want to pull RT tasks from other runqueues.
2297391e43daSPeter Zijlstra  */
2298391e43daSPeter Zijlstra static void switched_from_rt(struct rq *rq, struct task_struct *p)
2299391e43daSPeter Zijlstra {
2300391e43daSPeter Zijlstra 	/*
2301391e43daSPeter Zijlstra 	 * If there are other RT tasks then we will reschedule
2302391e43daSPeter Zijlstra 	 * and the scheduling of the other RT tasks will handle
2303391e43daSPeter Zijlstra 	 * the balancing. But if we are the last RT task
2304391e43daSPeter Zijlstra 	 * we may need to handle the pulling of RT tasks
2305391e43daSPeter Zijlstra 	 * now.
2306391e43daSPeter Zijlstra 	 */
2307da0c1e65SKirill Tkhai 	if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
23081158ddb5SKirill Tkhai 		return;
23091158ddb5SKirill Tkhai 
231002d8ec94SIngo Molnar 	rt_queue_pull_task(rq);
2311391e43daSPeter Zijlstra }
2312391e43daSPeter Zijlstra 
231311c785b7SLi Zefan void __init init_sched_rt_class(void)
2314391e43daSPeter Zijlstra {
2315391e43daSPeter Zijlstra 	unsigned int i;
2316391e43daSPeter Zijlstra 
2317391e43daSPeter Zijlstra 	for_each_possible_cpu(i) {
2318391e43daSPeter Zijlstra 		zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
2319391e43daSPeter Zijlstra 					GFP_KERNEL, cpu_to_node(i));
2320391e43daSPeter Zijlstra 	}
2321391e43daSPeter Zijlstra }
2322391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
2323391e43daSPeter Zijlstra 
2324391e43daSPeter Zijlstra /*
2325391e43daSPeter Zijlstra  * When switching a task to RT, we may overload the runqueue
2326391e43daSPeter Zijlstra  * with RT tasks. In this case we try to push them off to
2327391e43daSPeter Zijlstra  * other runqueues.
2328391e43daSPeter Zijlstra  */
2329391e43daSPeter Zijlstra static void switched_to_rt(struct rq *rq, struct task_struct *p)
2330391e43daSPeter Zijlstra {
2331391e43daSPeter Zijlstra 	/*
2332391e43daSPeter Zijlstra 	 * If we are already running, then there's nothing
2333391e43daSPeter Zijlstra 	 * that needs to be done. But if we are not running
2334391e43daSPeter Zijlstra 	 * we may need to preempt the current running task.
2335391e43daSPeter Zijlstra 	 * If that current running task is also an RT task
2336391e43daSPeter Zijlstra 	 * then see if we can move to another run queue.
2337391e43daSPeter Zijlstra 	 */
2338da0c1e65SKirill Tkhai 	if (task_on_rq_queued(p) && rq->curr != p) {
2339391e43daSPeter Zijlstra #ifdef CONFIG_SMP
2340d94a9df4SQais Yousef 		if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
234102d8ec94SIngo Molnar 			rt_queue_push_tasks(rq);
2342619bd4a7SSebastian Andrzej Siewior #endif /* CONFIG_SMP */
23432fe25826SPaul E. McKenney 		if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
23448875125eSKirill Tkhai 			resched_curr(rq);
2345391e43daSPeter Zijlstra 	}
2346391e43daSPeter Zijlstra }
2347391e43daSPeter Zijlstra 
2348391e43daSPeter Zijlstra /*
2349391e43daSPeter Zijlstra  * Priority of the task has changed. This may cause
2350391e43daSPeter Zijlstra  * us to initiate a push or pull.
2351391e43daSPeter Zijlstra  */
2352391e43daSPeter Zijlstra static void
2353391e43daSPeter Zijlstra prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
2354391e43daSPeter Zijlstra {
2355da0c1e65SKirill Tkhai 	if (!task_on_rq_queued(p))
2356391e43daSPeter Zijlstra 		return;
2357391e43daSPeter Zijlstra 
2358391e43daSPeter Zijlstra 	if (rq->curr == p) {
2359391e43daSPeter Zijlstra #ifdef CONFIG_SMP
2360391e43daSPeter Zijlstra 		/*
2361391e43daSPeter Zijlstra 		 * If our priority decreases while running, we
2362391e43daSPeter Zijlstra 		 * may need to pull tasks to this runqueue.
2363391e43daSPeter Zijlstra 		 */
2364391e43daSPeter Zijlstra 		if (oldprio < p->prio)
236502d8ec94SIngo Molnar 			rt_queue_pull_task(rq);
2366fd7a4bedSPeter Zijlstra 
2367391e43daSPeter Zijlstra 		/*
2368391e43daSPeter Zijlstra 		 * If there's a higher priority task waiting to run
2369fd7a4bedSPeter Zijlstra 		 * then reschedule.
2370391e43daSPeter Zijlstra 		 */
2371fd7a4bedSPeter Zijlstra 		if (p->prio > rq->rt.highest_prio.curr)
23728875125eSKirill Tkhai 			resched_curr(rq);
2373391e43daSPeter Zijlstra #else
2374391e43daSPeter Zijlstra 		/* For UP simply resched on drop of prio */
2375391e43daSPeter Zijlstra 		if (oldprio < p->prio)
23768875125eSKirill Tkhai 			resched_curr(rq);
2377391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
2378391e43daSPeter Zijlstra 	} else {
2379391e43daSPeter Zijlstra 		/*
2380391e43daSPeter Zijlstra 		 * This task is not running, but if it is
2381391e43daSPeter Zijlstra 		 * greater than the current running task
2382391e43daSPeter Zijlstra 		 * then reschedule.
2383391e43daSPeter Zijlstra 		 */
2384391e43daSPeter Zijlstra 		if (p->prio < rq->curr->prio)
23858875125eSKirill Tkhai 			resched_curr(rq);
2386391e43daSPeter Zijlstra 	}
2387391e43daSPeter Zijlstra }
2388391e43daSPeter Zijlstra 
2389b18b6a9cSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS
2390391e43daSPeter Zijlstra static void watchdog(struct rq *rq, struct task_struct *p)
2391391e43daSPeter Zijlstra {
2392391e43daSPeter Zijlstra 	unsigned long soft, hard;
2393391e43daSPeter Zijlstra 
2394391e43daSPeter Zijlstra 	/* max may change after cur was read, this will be fixed next tick */
2395391e43daSPeter Zijlstra 	soft = task_rlimit(p, RLIMIT_RTTIME);
2396391e43daSPeter Zijlstra 	hard = task_rlimit_max(p, RLIMIT_RTTIME);
2397391e43daSPeter Zijlstra 
2398391e43daSPeter Zijlstra 	if (soft != RLIM_INFINITY) {
2399391e43daSPeter Zijlstra 		unsigned long next;
2400391e43daSPeter Zijlstra 
240157d2aa00SYing Xue 		if (p->rt.watchdog_stamp != jiffies) {
2402391e43daSPeter Zijlstra 			p->rt.timeout++;
240357d2aa00SYing Xue 			p->rt.watchdog_stamp = jiffies;
240457d2aa00SYing Xue 		}
240557d2aa00SYing Xue 
2406391e43daSPeter Zijlstra 		next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
24073a245c0fSThomas Gleixner 		if (p->rt.timeout > next) {
24083a245c0fSThomas Gleixner 			posix_cputimers_rt_watchdog(&p->posix_cputimers,
24093a245c0fSThomas Gleixner 						    p->se.sum_exec_runtime);
24103a245c0fSThomas Gleixner 		}
2411391e43daSPeter Zijlstra 	}
2412391e43daSPeter Zijlstra }
2413b18b6a9cSNicolas Pitre #else
2414b18b6a9cSNicolas Pitre static inline void watchdog(struct rq *rq, struct task_struct *p) { }
2415b18b6a9cSNicolas Pitre #endif
2416391e43daSPeter Zijlstra 
2417d84b3131SFrederic Weisbecker /*
2418d84b3131SFrederic Weisbecker  * scheduler tick hitting a task of our scheduling class.
2419d84b3131SFrederic Weisbecker  *
2420d84b3131SFrederic Weisbecker  * NOTE: This function can be called remotely by the tick offload that
2421d84b3131SFrederic Weisbecker  * goes along full dynticks. Therefore no local assumption can be made
2422d84b3131SFrederic Weisbecker  * and everything must be accessed through the @rq and @curr passed in
2423d84b3131SFrederic Weisbecker  * parameters.
2424d84b3131SFrederic Weisbecker  */
2425391e43daSPeter Zijlstra static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
2426391e43daSPeter Zijlstra {
2427454c7999SColin Cross 	struct sched_rt_entity *rt_se = &p->rt;
2428454c7999SColin Cross 
2429391e43daSPeter Zijlstra 	update_curr_rt(rq);
243023127296SVincent Guittot 	update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2431391e43daSPeter Zijlstra 
2432391e43daSPeter Zijlstra 	watchdog(rq, p);
2433391e43daSPeter Zijlstra 
2434391e43daSPeter Zijlstra 	/*
2435391e43daSPeter Zijlstra 	 * RR tasks need a special form of timeslice management.
2436391e43daSPeter Zijlstra 	 * FIFO tasks have no timeslices.
2437391e43daSPeter Zijlstra 	 */
2438391e43daSPeter Zijlstra 	if (p->policy != SCHED_RR)
2439391e43daSPeter Zijlstra 		return;
2440391e43daSPeter Zijlstra 
2441391e43daSPeter Zijlstra 	if (--p->rt.time_slice)
2442391e43daSPeter Zijlstra 		return;
2443391e43daSPeter Zijlstra 
2444ce0dbbbbSClark Williams 	p->rt.time_slice = sched_rr_timeslice;
2445391e43daSPeter Zijlstra 
2446391e43daSPeter Zijlstra 	/*
2447e9aa39bbSLi Bin 	 * Requeue to the end of queue if we (and all of our ancestors) are not
2448e9aa39bbSLi Bin 	 * the only element on the queue
2449391e43daSPeter Zijlstra 	 */
2450454c7999SColin Cross 	for_each_sched_rt_entity(rt_se) {
2451454c7999SColin Cross 		if (rt_se->run_list.prev != rt_se->run_list.next) {
2452391e43daSPeter Zijlstra 			requeue_task_rt(rq, p, 0);
24538aa6f0ebSKirill Tkhai 			resched_curr(rq);
2454454c7999SColin Cross 			return;
2455454c7999SColin Cross 		}
2456391e43daSPeter Zijlstra 	}
2457391e43daSPeter Zijlstra }
2458391e43daSPeter Zijlstra 
2459391e43daSPeter Zijlstra static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
2460391e43daSPeter Zijlstra {
2461391e43daSPeter Zijlstra 	/*
2462391e43daSPeter Zijlstra 	 * Time slice is 0 for SCHED_FIFO tasks
2463391e43daSPeter Zijlstra 	 */
2464391e43daSPeter Zijlstra 	if (task->policy == SCHED_RR)
2465ce0dbbbbSClark Williams 		return sched_rr_timeslice;
2466391e43daSPeter Zijlstra 	else
2467391e43daSPeter Zijlstra 		return 0;
2468391e43daSPeter Zijlstra }
2469391e43daSPeter Zijlstra 
2470590d6979SSteven Rostedt (VMware) const struct sched_class rt_sched_class
247133def849SJoe Perches 	__section("__rt_sched_class") = {
2472391e43daSPeter Zijlstra 	.enqueue_task		= enqueue_task_rt,
2473391e43daSPeter Zijlstra 	.dequeue_task		= dequeue_task_rt,
2474391e43daSPeter Zijlstra 	.yield_task		= yield_task_rt,
2475391e43daSPeter Zijlstra 
2476391e43daSPeter Zijlstra 	.check_preempt_curr	= check_preempt_curr_rt,
2477391e43daSPeter Zijlstra 
2478391e43daSPeter Zijlstra 	.pick_next_task		= pick_next_task_rt,
2479391e43daSPeter Zijlstra 	.put_prev_task		= put_prev_task_rt,
248003b7fad1SPeter Zijlstra 	.set_next_task          = set_next_task_rt,
2481391e43daSPeter Zijlstra 
2482391e43daSPeter Zijlstra #ifdef CONFIG_SMP
24836e2df058SPeter Zijlstra 	.balance		= balance_rt,
2484391e43daSPeter Zijlstra 	.select_task_rq		= select_task_rq_rt,
24856c37067eSPeter Zijlstra 	.set_cpus_allowed       = set_cpus_allowed_common,
2486391e43daSPeter Zijlstra 	.rq_online              = rq_online_rt,
2487391e43daSPeter Zijlstra 	.rq_offline             = rq_offline_rt,
2488391e43daSPeter Zijlstra 	.task_woken		= task_woken_rt,
2489391e43daSPeter Zijlstra 	.switched_from		= switched_from_rt,
2490*a7c81556SPeter Zijlstra 	.find_lock_rq		= find_lock_lowest_rq,
2491391e43daSPeter Zijlstra #endif
2492391e43daSPeter Zijlstra 
2493391e43daSPeter Zijlstra 	.task_tick		= task_tick_rt,
2494391e43daSPeter Zijlstra 
2495391e43daSPeter Zijlstra 	.get_rr_interval	= get_rr_interval_rt,
2496391e43daSPeter Zijlstra 
2497391e43daSPeter Zijlstra 	.prio_changed		= prio_changed_rt,
2498391e43daSPeter Zijlstra 	.switched_to		= switched_to_rt,
24996e998916SStanislaw Gruszka 
25006e998916SStanislaw Gruszka 	.update_curr		= update_curr_rt,
2501982d9cdcSPatrick Bellasi 
2502982d9cdcSPatrick Bellasi #ifdef CONFIG_UCLAMP_TASK
2503982d9cdcSPatrick Bellasi 	.uclamp_enabled		= 1,
2504982d9cdcSPatrick Bellasi #endif
2505391e43daSPeter Zijlstra };
2506391e43daSPeter Zijlstra 
25078887cd99SNicolas Pitre #ifdef CONFIG_RT_GROUP_SCHED
25088887cd99SNicolas Pitre /*
25098887cd99SNicolas Pitre  * Ensure that the real time constraints are schedulable.
25108887cd99SNicolas Pitre  */
25118887cd99SNicolas Pitre static DEFINE_MUTEX(rt_constraints_mutex);
25128887cd99SNicolas Pitre 
25138887cd99SNicolas Pitre static inline int tg_has_rt_tasks(struct task_group *tg)
25148887cd99SNicolas Pitre {
2515b4fb015eSKonstantin Khlebnikov 	struct task_struct *task;
2516b4fb015eSKonstantin Khlebnikov 	struct css_task_iter it;
2517b4fb015eSKonstantin Khlebnikov 	int ret = 0;
25188887cd99SNicolas Pitre 
25198887cd99SNicolas Pitre 	/*
25208887cd99SNicolas Pitre 	 * Autogroups do not have RT tasks; see autogroup_create().
25218887cd99SNicolas Pitre 	 */
25228887cd99SNicolas Pitre 	if (task_group_is_autogroup(tg))
25238887cd99SNicolas Pitre 		return 0;
25248887cd99SNicolas Pitre 
2525b4fb015eSKonstantin Khlebnikov 	css_task_iter_start(&tg->css, 0, &it);
2526b4fb015eSKonstantin Khlebnikov 	while (!ret && (task = css_task_iter_next(&it)))
2527b4fb015eSKonstantin Khlebnikov 		ret |= rt_task(task);
2528b4fb015eSKonstantin Khlebnikov 	css_task_iter_end(&it);
25298887cd99SNicolas Pitre 
2530b4fb015eSKonstantin Khlebnikov 	return ret;
25318887cd99SNicolas Pitre }
25328887cd99SNicolas Pitre 
25338887cd99SNicolas Pitre struct rt_schedulable_data {
25348887cd99SNicolas Pitre 	struct task_group *tg;
25358887cd99SNicolas Pitre 	u64 rt_period;
25368887cd99SNicolas Pitre 	u64 rt_runtime;
25378887cd99SNicolas Pitre };
25388887cd99SNicolas Pitre 
25398887cd99SNicolas Pitre static int tg_rt_schedulable(struct task_group *tg, void *data)
25408887cd99SNicolas Pitre {
25418887cd99SNicolas Pitre 	struct rt_schedulable_data *d = data;
25428887cd99SNicolas Pitre 	struct task_group *child;
25438887cd99SNicolas Pitre 	unsigned long total, sum = 0;
25448887cd99SNicolas Pitre 	u64 period, runtime;
25458887cd99SNicolas Pitre 
25468887cd99SNicolas Pitre 	period = ktime_to_ns(tg->rt_bandwidth.rt_period);
25478887cd99SNicolas Pitre 	runtime = tg->rt_bandwidth.rt_runtime;
25488887cd99SNicolas Pitre 
25498887cd99SNicolas Pitre 	if (tg == d->tg) {
25508887cd99SNicolas Pitre 		period = d->rt_period;
25518887cd99SNicolas Pitre 		runtime = d->rt_runtime;
25528887cd99SNicolas Pitre 	}
25538887cd99SNicolas Pitre 
25548887cd99SNicolas Pitre 	/*
25558887cd99SNicolas Pitre 	 * Cannot have more runtime than the period.
25568887cd99SNicolas Pitre 	 */
25578887cd99SNicolas Pitre 	if (runtime > period && runtime != RUNTIME_INF)
25588887cd99SNicolas Pitre 		return -EINVAL;
25598887cd99SNicolas Pitre 
25608887cd99SNicolas Pitre 	/*
2561b4fb015eSKonstantin Khlebnikov 	 * Ensure we don't starve existing RT tasks if runtime turns zero.
25628887cd99SNicolas Pitre 	 */
2563b4fb015eSKonstantin Khlebnikov 	if (rt_bandwidth_enabled() && !runtime &&
2564b4fb015eSKonstantin Khlebnikov 	    tg->rt_bandwidth.rt_runtime && tg_has_rt_tasks(tg))
25658887cd99SNicolas Pitre 		return -EBUSY;
25668887cd99SNicolas Pitre 
25678887cd99SNicolas Pitre 	total = to_ratio(period, runtime);
25688887cd99SNicolas Pitre 
25698887cd99SNicolas Pitre 	/*
25708887cd99SNicolas Pitre 	 * Nobody can have more than the global setting allows.
25718887cd99SNicolas Pitre 	 */
25728887cd99SNicolas Pitre 	if (total > to_ratio(global_rt_period(), global_rt_runtime()))
25738887cd99SNicolas Pitre 		return -EINVAL;
25748887cd99SNicolas Pitre 
25758887cd99SNicolas Pitre 	/*
25768887cd99SNicolas Pitre 	 * The sum of our children's runtime should not exceed our own.
25778887cd99SNicolas Pitre 	 */
25788887cd99SNicolas Pitre 	list_for_each_entry_rcu(child, &tg->children, siblings) {
25798887cd99SNicolas Pitre 		period = ktime_to_ns(child->rt_bandwidth.rt_period);
25808887cd99SNicolas Pitre 		runtime = child->rt_bandwidth.rt_runtime;
25818887cd99SNicolas Pitre 
25828887cd99SNicolas Pitre 		if (child == d->tg) {
25838887cd99SNicolas Pitre 			period = d->rt_period;
25848887cd99SNicolas Pitre 			runtime = d->rt_runtime;
25858887cd99SNicolas Pitre 		}
25868887cd99SNicolas Pitre 
25878887cd99SNicolas Pitre 		sum += to_ratio(period, runtime);
25888887cd99SNicolas Pitre 	}
25898887cd99SNicolas Pitre 
25908887cd99SNicolas Pitre 	if (sum > total)
25918887cd99SNicolas Pitre 		return -EINVAL;
25928887cd99SNicolas Pitre 
25938887cd99SNicolas Pitre 	return 0;
25948887cd99SNicolas Pitre }
25958887cd99SNicolas Pitre 
25968887cd99SNicolas Pitre static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
25978887cd99SNicolas Pitre {
25988887cd99SNicolas Pitre 	int ret;
25998887cd99SNicolas Pitre 
26008887cd99SNicolas Pitre 	struct rt_schedulable_data data = {
26018887cd99SNicolas Pitre 		.tg = tg,
26028887cd99SNicolas Pitre 		.rt_period = period,
26038887cd99SNicolas Pitre 		.rt_runtime = runtime,
26048887cd99SNicolas Pitre 	};
26058887cd99SNicolas Pitre 
26068887cd99SNicolas Pitre 	rcu_read_lock();
26078887cd99SNicolas Pitre 	ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
26088887cd99SNicolas Pitre 	rcu_read_unlock();
26098887cd99SNicolas Pitre 
26108887cd99SNicolas Pitre 	return ret;
26118887cd99SNicolas Pitre }
26128887cd99SNicolas Pitre 
26138887cd99SNicolas Pitre static int tg_set_rt_bandwidth(struct task_group *tg,
26148887cd99SNicolas Pitre 		u64 rt_period, u64 rt_runtime)
26158887cd99SNicolas Pitre {
26168887cd99SNicolas Pitre 	int i, err = 0;
26178887cd99SNicolas Pitre 
26188887cd99SNicolas Pitre 	/*
26198887cd99SNicolas Pitre 	 * Disallowing the root group RT runtime is BAD, it would disallow the
26208887cd99SNicolas Pitre 	 * kernel creating (and or operating) RT threads.
26218887cd99SNicolas Pitre 	 */
26228887cd99SNicolas Pitre 	if (tg == &root_task_group && rt_runtime == 0)
26238887cd99SNicolas Pitre 		return -EINVAL;
26248887cd99SNicolas Pitre 
26258887cd99SNicolas Pitre 	/* No period doesn't make any sense. */
26268887cd99SNicolas Pitre 	if (rt_period == 0)
26278887cd99SNicolas Pitre 		return -EINVAL;
26288887cd99SNicolas Pitre 
2629d505b8afSHuaixin Chang 	/*
2630d505b8afSHuaixin Chang 	 * Bound quota to defend quota against overflow during bandwidth shift.
2631d505b8afSHuaixin Chang 	 */
2632d505b8afSHuaixin Chang 	if (rt_runtime != RUNTIME_INF && rt_runtime > max_rt_runtime)
2633d505b8afSHuaixin Chang 		return -EINVAL;
2634d505b8afSHuaixin Chang 
26358887cd99SNicolas Pitre 	mutex_lock(&rt_constraints_mutex);
26368887cd99SNicolas Pitre 	err = __rt_schedulable(tg, rt_period, rt_runtime);
26378887cd99SNicolas Pitre 	if (err)
26388887cd99SNicolas Pitre 		goto unlock;
26398887cd99SNicolas Pitre 
26408887cd99SNicolas Pitre 	raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
26418887cd99SNicolas Pitre 	tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
26428887cd99SNicolas Pitre 	tg->rt_bandwidth.rt_runtime = rt_runtime;
26438887cd99SNicolas Pitre 
26448887cd99SNicolas Pitre 	for_each_possible_cpu(i) {
26458887cd99SNicolas Pitre 		struct rt_rq *rt_rq = tg->rt_rq[i];
26468887cd99SNicolas Pitre 
26478887cd99SNicolas Pitre 		raw_spin_lock(&rt_rq->rt_runtime_lock);
26488887cd99SNicolas Pitre 		rt_rq->rt_runtime = rt_runtime;
26498887cd99SNicolas Pitre 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
26508887cd99SNicolas Pitre 	}
26518887cd99SNicolas Pitre 	raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
26528887cd99SNicolas Pitre unlock:
26538887cd99SNicolas Pitre 	mutex_unlock(&rt_constraints_mutex);
26548887cd99SNicolas Pitre 
26558887cd99SNicolas Pitre 	return err;
26568887cd99SNicolas Pitre }
26578887cd99SNicolas Pitre 
26588887cd99SNicolas Pitre int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
26598887cd99SNicolas Pitre {
26608887cd99SNicolas Pitre 	u64 rt_runtime, rt_period;
26618887cd99SNicolas Pitre 
26628887cd99SNicolas Pitre 	rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
26638887cd99SNicolas Pitre 	rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
26648887cd99SNicolas Pitre 	if (rt_runtime_us < 0)
26658887cd99SNicolas Pitre 		rt_runtime = RUNTIME_INF;
26661a010e29SKonstantin Khlebnikov 	else if ((u64)rt_runtime_us > U64_MAX / NSEC_PER_USEC)
26671a010e29SKonstantin Khlebnikov 		return -EINVAL;
26688887cd99SNicolas Pitre 
26698887cd99SNicolas Pitre 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
26708887cd99SNicolas Pitre }
26718887cd99SNicolas Pitre 
26728887cd99SNicolas Pitre long sched_group_rt_runtime(struct task_group *tg)
26738887cd99SNicolas Pitre {
26748887cd99SNicolas Pitre 	u64 rt_runtime_us;
26758887cd99SNicolas Pitre 
26768887cd99SNicolas Pitre 	if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
26778887cd99SNicolas Pitre 		return -1;
26788887cd99SNicolas Pitre 
26798887cd99SNicolas Pitre 	rt_runtime_us = tg->rt_bandwidth.rt_runtime;
26808887cd99SNicolas Pitre 	do_div(rt_runtime_us, NSEC_PER_USEC);
26818887cd99SNicolas Pitre 	return rt_runtime_us;
26828887cd99SNicolas Pitre }
26838887cd99SNicolas Pitre 
26848887cd99SNicolas Pitre int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
26858887cd99SNicolas Pitre {
26868887cd99SNicolas Pitre 	u64 rt_runtime, rt_period;
26878887cd99SNicolas Pitre 
26881a010e29SKonstantin Khlebnikov 	if (rt_period_us > U64_MAX / NSEC_PER_USEC)
26891a010e29SKonstantin Khlebnikov 		return -EINVAL;
26901a010e29SKonstantin Khlebnikov 
26918887cd99SNicolas Pitre 	rt_period = rt_period_us * NSEC_PER_USEC;
26928887cd99SNicolas Pitre 	rt_runtime = tg->rt_bandwidth.rt_runtime;
26938887cd99SNicolas Pitre 
26948887cd99SNicolas Pitre 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
26958887cd99SNicolas Pitre }
26968887cd99SNicolas Pitre 
26978887cd99SNicolas Pitre long sched_group_rt_period(struct task_group *tg)
26988887cd99SNicolas Pitre {
26998887cd99SNicolas Pitre 	u64 rt_period_us;
27008887cd99SNicolas Pitre 
27018887cd99SNicolas Pitre 	rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
27028887cd99SNicolas Pitre 	do_div(rt_period_us, NSEC_PER_USEC);
27038887cd99SNicolas Pitre 	return rt_period_us;
27048887cd99SNicolas Pitre }
27058887cd99SNicolas Pitre 
27068887cd99SNicolas Pitre static int sched_rt_global_constraints(void)
27078887cd99SNicolas Pitre {
27088887cd99SNicolas Pitre 	int ret = 0;
27098887cd99SNicolas Pitre 
27108887cd99SNicolas Pitre 	mutex_lock(&rt_constraints_mutex);
27118887cd99SNicolas Pitre 	ret = __rt_schedulable(NULL, 0, 0);
27128887cd99SNicolas Pitre 	mutex_unlock(&rt_constraints_mutex);
27138887cd99SNicolas Pitre 
27148887cd99SNicolas Pitre 	return ret;
27158887cd99SNicolas Pitre }
27168887cd99SNicolas Pitre 
27178887cd99SNicolas Pitre int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
27188887cd99SNicolas Pitre {
27198887cd99SNicolas Pitre 	/* Don't accept realtime tasks when there is no way for them to run */
27208887cd99SNicolas Pitre 	if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
27218887cd99SNicolas Pitre 		return 0;
27228887cd99SNicolas Pitre 
27238887cd99SNicolas Pitre 	return 1;
27248887cd99SNicolas Pitre }
27258887cd99SNicolas Pitre 
27268887cd99SNicolas Pitre #else /* !CONFIG_RT_GROUP_SCHED */
27278887cd99SNicolas Pitre static int sched_rt_global_constraints(void)
27288887cd99SNicolas Pitre {
27298887cd99SNicolas Pitre 	unsigned long flags;
27308887cd99SNicolas Pitre 	int i;
27318887cd99SNicolas Pitre 
27328887cd99SNicolas Pitre 	raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
27338887cd99SNicolas Pitre 	for_each_possible_cpu(i) {
27348887cd99SNicolas Pitre 		struct rt_rq *rt_rq = &cpu_rq(i)->rt;
27358887cd99SNicolas Pitre 
27368887cd99SNicolas Pitre 		raw_spin_lock(&rt_rq->rt_runtime_lock);
27378887cd99SNicolas Pitre 		rt_rq->rt_runtime = global_rt_runtime();
27388887cd99SNicolas Pitre 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
27398887cd99SNicolas Pitre 	}
27408887cd99SNicolas Pitre 	raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
27418887cd99SNicolas Pitre 
27428887cd99SNicolas Pitre 	return 0;
27438887cd99SNicolas Pitre }
27448887cd99SNicolas Pitre #endif /* CONFIG_RT_GROUP_SCHED */
27458887cd99SNicolas Pitre 
27468887cd99SNicolas Pitre static int sched_rt_global_validate(void)
27478887cd99SNicolas Pitre {
27488887cd99SNicolas Pitre 	if (sysctl_sched_rt_period <= 0)
27498887cd99SNicolas Pitre 		return -EINVAL;
27508887cd99SNicolas Pitre 
27518887cd99SNicolas Pitre 	if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
2752d505b8afSHuaixin Chang 		((sysctl_sched_rt_runtime > sysctl_sched_rt_period) ||
2753d505b8afSHuaixin Chang 		 ((u64)sysctl_sched_rt_runtime *
2754d505b8afSHuaixin Chang 			NSEC_PER_USEC > max_rt_runtime)))
27558887cd99SNicolas Pitre 		return -EINVAL;
27568887cd99SNicolas Pitre 
27578887cd99SNicolas Pitre 	return 0;
27588887cd99SNicolas Pitre }
27598887cd99SNicolas Pitre 
27608887cd99SNicolas Pitre static void sched_rt_do_global(void)
27618887cd99SNicolas Pitre {
27628887cd99SNicolas Pitre 	def_rt_bandwidth.rt_runtime = global_rt_runtime();
27638887cd99SNicolas Pitre 	def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
27648887cd99SNicolas Pitre }
27658887cd99SNicolas Pitre 
276632927393SChristoph Hellwig int sched_rt_handler(struct ctl_table *table, int write, void *buffer,
276732927393SChristoph Hellwig 		size_t *lenp, loff_t *ppos)
27688887cd99SNicolas Pitre {
27698887cd99SNicolas Pitre 	int old_period, old_runtime;
27708887cd99SNicolas Pitre 	static DEFINE_MUTEX(mutex);
27718887cd99SNicolas Pitre 	int ret;
27728887cd99SNicolas Pitre 
27738887cd99SNicolas Pitre 	mutex_lock(&mutex);
27748887cd99SNicolas Pitre 	old_period = sysctl_sched_rt_period;
27758887cd99SNicolas Pitre 	old_runtime = sysctl_sched_rt_runtime;
27768887cd99SNicolas Pitre 
27778887cd99SNicolas Pitre 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
27788887cd99SNicolas Pitre 
27798887cd99SNicolas Pitre 	if (!ret && write) {
27808887cd99SNicolas Pitre 		ret = sched_rt_global_validate();
27818887cd99SNicolas Pitre 		if (ret)
27828887cd99SNicolas Pitre 			goto undo;
27838887cd99SNicolas Pitre 
27848887cd99SNicolas Pitre 		ret = sched_dl_global_validate();
27858887cd99SNicolas Pitre 		if (ret)
27868887cd99SNicolas Pitre 			goto undo;
27878887cd99SNicolas Pitre 
27888887cd99SNicolas Pitre 		ret = sched_rt_global_constraints();
27898887cd99SNicolas Pitre 		if (ret)
27908887cd99SNicolas Pitre 			goto undo;
27918887cd99SNicolas Pitre 
27928887cd99SNicolas Pitre 		sched_rt_do_global();
27938887cd99SNicolas Pitre 		sched_dl_do_global();
27948887cd99SNicolas Pitre 	}
27958887cd99SNicolas Pitre 	if (0) {
27968887cd99SNicolas Pitre undo:
27978887cd99SNicolas Pitre 		sysctl_sched_rt_period = old_period;
27988887cd99SNicolas Pitre 		sysctl_sched_rt_runtime = old_runtime;
27998887cd99SNicolas Pitre 	}
28008887cd99SNicolas Pitre 	mutex_unlock(&mutex);
28018887cd99SNicolas Pitre 
28028887cd99SNicolas Pitre 	return ret;
28038887cd99SNicolas Pitre }
28048887cd99SNicolas Pitre 
280532927393SChristoph Hellwig int sched_rr_handler(struct ctl_table *table, int write, void *buffer,
280632927393SChristoph Hellwig 		size_t *lenp, loff_t *ppos)
28078887cd99SNicolas Pitre {
28088887cd99SNicolas Pitre 	int ret;
28098887cd99SNicolas Pitre 	static DEFINE_MUTEX(mutex);
28108887cd99SNicolas Pitre 
28118887cd99SNicolas Pitre 	mutex_lock(&mutex);
28128887cd99SNicolas Pitre 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
28138887cd99SNicolas Pitre 	/*
28148887cd99SNicolas Pitre 	 * Make sure that internally we keep jiffies.
28158887cd99SNicolas Pitre 	 * Also, writing zero resets the timeslice to default:
28168887cd99SNicolas Pitre 	 */
28178887cd99SNicolas Pitre 	if (!ret && write) {
28188887cd99SNicolas Pitre 		sched_rr_timeslice =
28198887cd99SNicolas Pitre 			sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
28208887cd99SNicolas Pitre 			msecs_to_jiffies(sysctl_sched_rr_timeslice);
28218887cd99SNicolas Pitre 	}
28228887cd99SNicolas Pitre 	mutex_unlock(&mutex);
282397fb7a0aSIngo Molnar 
28248887cd99SNicolas Pitre 	return ret;
28258887cd99SNicolas Pitre }
28268887cd99SNicolas Pitre 
2827391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG
2828391e43daSPeter Zijlstra void print_rt_stats(struct seq_file *m, int cpu)
2829391e43daSPeter Zijlstra {
2830391e43daSPeter Zijlstra 	rt_rq_iter_t iter;
2831391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
2832391e43daSPeter Zijlstra 
2833391e43daSPeter Zijlstra 	rcu_read_lock();
2834391e43daSPeter Zijlstra 	for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
2835391e43daSPeter Zijlstra 		print_rt_rq(m, cpu, rt_rq);
2836391e43daSPeter Zijlstra 	rcu_read_unlock();
2837391e43daSPeter Zijlstra }
2838391e43daSPeter Zijlstra #endif /* CONFIG_SCHED_DEBUG */
2839