xref: /openbmc/linux/kernel/sched/rt.c (revision 4bdced5c9a2922521e325896a7bbbf0132c94e56)
1391e43daSPeter Zijlstra /*
2391e43daSPeter Zijlstra  * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
3391e43daSPeter Zijlstra  * policies)
4391e43daSPeter Zijlstra  */
5391e43daSPeter Zijlstra 
6391e43daSPeter Zijlstra #include "sched.h"
7391e43daSPeter Zijlstra 
8391e43daSPeter Zijlstra #include <linux/slab.h>
9b6366f04SSteven Rostedt #include <linux/irq_work.h>
10391e43daSPeter Zijlstra 
11ce0dbbbbSClark Williams int sched_rr_timeslice = RR_TIMESLICE;
12975e155eSShile Zhang int sysctl_sched_rr_timeslice = (MSEC_PER_SEC / HZ) * RR_TIMESLICE;
13ce0dbbbbSClark Williams 
14391e43daSPeter Zijlstra static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
15391e43daSPeter Zijlstra 
16391e43daSPeter Zijlstra struct rt_bandwidth def_rt_bandwidth;
17391e43daSPeter Zijlstra 
18391e43daSPeter Zijlstra static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
19391e43daSPeter Zijlstra {
20391e43daSPeter Zijlstra 	struct rt_bandwidth *rt_b =
21391e43daSPeter Zijlstra 		container_of(timer, struct rt_bandwidth, rt_period_timer);
22391e43daSPeter Zijlstra 	int idle = 0;
2377a4d1a1SPeter Zijlstra 	int overrun;
24391e43daSPeter Zijlstra 
2577a4d1a1SPeter Zijlstra 	raw_spin_lock(&rt_b->rt_runtime_lock);
26391e43daSPeter Zijlstra 	for (;;) {
2777a4d1a1SPeter Zijlstra 		overrun = hrtimer_forward_now(timer, rt_b->rt_period);
28391e43daSPeter Zijlstra 		if (!overrun)
29391e43daSPeter Zijlstra 			break;
30391e43daSPeter Zijlstra 
3177a4d1a1SPeter Zijlstra 		raw_spin_unlock(&rt_b->rt_runtime_lock);
32391e43daSPeter Zijlstra 		idle = do_sched_rt_period_timer(rt_b, overrun);
3377a4d1a1SPeter Zijlstra 		raw_spin_lock(&rt_b->rt_runtime_lock);
34391e43daSPeter Zijlstra 	}
354cfafd30SPeter Zijlstra 	if (idle)
364cfafd30SPeter Zijlstra 		rt_b->rt_period_active = 0;
3777a4d1a1SPeter Zijlstra 	raw_spin_unlock(&rt_b->rt_runtime_lock);
38391e43daSPeter Zijlstra 
39391e43daSPeter Zijlstra 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
40391e43daSPeter Zijlstra }
41391e43daSPeter Zijlstra 
42391e43daSPeter Zijlstra void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
43391e43daSPeter Zijlstra {
44391e43daSPeter Zijlstra 	rt_b->rt_period = ns_to_ktime(period);
45391e43daSPeter Zijlstra 	rt_b->rt_runtime = runtime;
46391e43daSPeter Zijlstra 
47391e43daSPeter Zijlstra 	raw_spin_lock_init(&rt_b->rt_runtime_lock);
48391e43daSPeter Zijlstra 
49391e43daSPeter Zijlstra 	hrtimer_init(&rt_b->rt_period_timer,
50391e43daSPeter Zijlstra 			CLOCK_MONOTONIC, HRTIMER_MODE_REL);
51391e43daSPeter Zijlstra 	rt_b->rt_period_timer.function = sched_rt_period_timer;
52391e43daSPeter Zijlstra }
53391e43daSPeter Zijlstra 
54391e43daSPeter Zijlstra static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
55391e43daSPeter Zijlstra {
56391e43daSPeter Zijlstra 	if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
57391e43daSPeter Zijlstra 		return;
58391e43daSPeter Zijlstra 
59391e43daSPeter Zijlstra 	raw_spin_lock(&rt_b->rt_runtime_lock);
604cfafd30SPeter Zijlstra 	if (!rt_b->rt_period_active) {
614cfafd30SPeter Zijlstra 		rt_b->rt_period_active = 1;
62c3a990dcSSteven Rostedt 		/*
63c3a990dcSSteven Rostedt 		 * SCHED_DEADLINE updates the bandwidth, as a run away
64c3a990dcSSteven Rostedt 		 * RT task with a DL task could hog a CPU. But DL does
65c3a990dcSSteven Rostedt 		 * not reset the period. If a deadline task was running
66c3a990dcSSteven Rostedt 		 * without an RT task running, it can cause RT tasks to
67c3a990dcSSteven Rostedt 		 * throttle when they start up. Kick the timer right away
68c3a990dcSSteven Rostedt 		 * to update the period.
69c3a990dcSSteven Rostedt 		 */
70c3a990dcSSteven Rostedt 		hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
714cfafd30SPeter Zijlstra 		hrtimer_start_expires(&rt_b->rt_period_timer, HRTIMER_MODE_ABS_PINNED);
724cfafd30SPeter Zijlstra 	}
73391e43daSPeter Zijlstra 	raw_spin_unlock(&rt_b->rt_runtime_lock);
74391e43daSPeter Zijlstra }
75391e43daSPeter Zijlstra 
7607c54f7aSAbel Vesa void init_rt_rq(struct rt_rq *rt_rq)
77391e43daSPeter Zijlstra {
78391e43daSPeter Zijlstra 	struct rt_prio_array *array;
79391e43daSPeter Zijlstra 	int i;
80391e43daSPeter Zijlstra 
81391e43daSPeter Zijlstra 	array = &rt_rq->active;
82391e43daSPeter Zijlstra 	for (i = 0; i < MAX_RT_PRIO; i++) {
83391e43daSPeter Zijlstra 		INIT_LIST_HEAD(array->queue + i);
84391e43daSPeter Zijlstra 		__clear_bit(i, array->bitmap);
85391e43daSPeter Zijlstra 	}
86391e43daSPeter Zijlstra 	/* delimiter for bitsearch: */
87391e43daSPeter Zijlstra 	__set_bit(MAX_RT_PRIO, array->bitmap);
88391e43daSPeter Zijlstra 
89391e43daSPeter Zijlstra #if defined CONFIG_SMP
90391e43daSPeter Zijlstra 	rt_rq->highest_prio.curr = MAX_RT_PRIO;
91391e43daSPeter Zijlstra 	rt_rq->highest_prio.next = MAX_RT_PRIO;
92391e43daSPeter Zijlstra 	rt_rq->rt_nr_migratory = 0;
93391e43daSPeter Zijlstra 	rt_rq->overloaded = 0;
94391e43daSPeter Zijlstra 	plist_head_init(&rt_rq->pushable_tasks);
95b6366f04SSteven Rostedt #endif /* CONFIG_SMP */
96f4ebcbc0SKirill Tkhai 	/* We start is dequeued state, because no RT tasks are queued */
97f4ebcbc0SKirill Tkhai 	rt_rq->rt_queued = 0;
98391e43daSPeter Zijlstra 
99391e43daSPeter Zijlstra 	rt_rq->rt_time = 0;
100391e43daSPeter Zijlstra 	rt_rq->rt_throttled = 0;
101391e43daSPeter Zijlstra 	rt_rq->rt_runtime = 0;
102391e43daSPeter Zijlstra 	raw_spin_lock_init(&rt_rq->rt_runtime_lock);
103391e43daSPeter Zijlstra }
104391e43daSPeter Zijlstra 
105391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
106391e43daSPeter Zijlstra static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
107391e43daSPeter Zijlstra {
108391e43daSPeter Zijlstra 	hrtimer_cancel(&rt_b->rt_period_timer);
109391e43daSPeter Zijlstra }
110391e43daSPeter Zijlstra 
111391e43daSPeter Zijlstra #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
112391e43daSPeter Zijlstra 
113391e43daSPeter Zijlstra static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
114391e43daSPeter Zijlstra {
115391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG
116391e43daSPeter Zijlstra 	WARN_ON_ONCE(!rt_entity_is_task(rt_se));
117391e43daSPeter Zijlstra #endif
118391e43daSPeter Zijlstra 	return container_of(rt_se, struct task_struct, rt);
119391e43daSPeter Zijlstra }
120391e43daSPeter Zijlstra 
121391e43daSPeter Zijlstra static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
122391e43daSPeter Zijlstra {
123391e43daSPeter Zijlstra 	return rt_rq->rq;
124391e43daSPeter Zijlstra }
125391e43daSPeter Zijlstra 
126391e43daSPeter Zijlstra static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
127391e43daSPeter Zijlstra {
128391e43daSPeter Zijlstra 	return rt_se->rt_rq;
129391e43daSPeter Zijlstra }
130391e43daSPeter Zijlstra 
131653d07a6SKirill Tkhai static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
132653d07a6SKirill Tkhai {
133653d07a6SKirill Tkhai 	struct rt_rq *rt_rq = rt_se->rt_rq;
134653d07a6SKirill Tkhai 
135653d07a6SKirill Tkhai 	return rt_rq->rq;
136653d07a6SKirill Tkhai }
137653d07a6SKirill Tkhai 
138391e43daSPeter Zijlstra void free_rt_sched_group(struct task_group *tg)
139391e43daSPeter Zijlstra {
140391e43daSPeter Zijlstra 	int i;
141391e43daSPeter Zijlstra 
142391e43daSPeter Zijlstra 	if (tg->rt_se)
143391e43daSPeter Zijlstra 		destroy_rt_bandwidth(&tg->rt_bandwidth);
144391e43daSPeter Zijlstra 
145391e43daSPeter Zijlstra 	for_each_possible_cpu(i) {
146391e43daSPeter Zijlstra 		if (tg->rt_rq)
147391e43daSPeter Zijlstra 			kfree(tg->rt_rq[i]);
148391e43daSPeter Zijlstra 		if (tg->rt_se)
149391e43daSPeter Zijlstra 			kfree(tg->rt_se[i]);
150391e43daSPeter Zijlstra 	}
151391e43daSPeter Zijlstra 
152391e43daSPeter Zijlstra 	kfree(tg->rt_rq);
153391e43daSPeter Zijlstra 	kfree(tg->rt_se);
154391e43daSPeter Zijlstra }
155391e43daSPeter Zijlstra 
156391e43daSPeter Zijlstra void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
157391e43daSPeter Zijlstra 		struct sched_rt_entity *rt_se, int cpu,
158391e43daSPeter Zijlstra 		struct sched_rt_entity *parent)
159391e43daSPeter Zijlstra {
160391e43daSPeter Zijlstra 	struct rq *rq = cpu_rq(cpu);
161391e43daSPeter Zijlstra 
162391e43daSPeter Zijlstra 	rt_rq->highest_prio.curr = MAX_RT_PRIO;
163391e43daSPeter Zijlstra 	rt_rq->rt_nr_boosted = 0;
164391e43daSPeter Zijlstra 	rt_rq->rq = rq;
165391e43daSPeter Zijlstra 	rt_rq->tg = tg;
166391e43daSPeter Zijlstra 
167391e43daSPeter Zijlstra 	tg->rt_rq[cpu] = rt_rq;
168391e43daSPeter Zijlstra 	tg->rt_se[cpu] = rt_se;
169391e43daSPeter Zijlstra 
170391e43daSPeter Zijlstra 	if (!rt_se)
171391e43daSPeter Zijlstra 		return;
172391e43daSPeter Zijlstra 
173391e43daSPeter Zijlstra 	if (!parent)
174391e43daSPeter Zijlstra 		rt_se->rt_rq = &rq->rt;
175391e43daSPeter Zijlstra 	else
176391e43daSPeter Zijlstra 		rt_se->rt_rq = parent->my_q;
177391e43daSPeter Zijlstra 
178391e43daSPeter Zijlstra 	rt_se->my_q = rt_rq;
179391e43daSPeter Zijlstra 	rt_se->parent = parent;
180391e43daSPeter Zijlstra 	INIT_LIST_HEAD(&rt_se->run_list);
181391e43daSPeter Zijlstra }
182391e43daSPeter Zijlstra 
183391e43daSPeter Zijlstra int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
184391e43daSPeter Zijlstra {
185391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
186391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se;
187391e43daSPeter Zijlstra 	int i;
188391e43daSPeter Zijlstra 
189391e43daSPeter Zijlstra 	tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
190391e43daSPeter Zijlstra 	if (!tg->rt_rq)
191391e43daSPeter Zijlstra 		goto err;
192391e43daSPeter Zijlstra 	tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
193391e43daSPeter Zijlstra 	if (!tg->rt_se)
194391e43daSPeter Zijlstra 		goto err;
195391e43daSPeter Zijlstra 
196391e43daSPeter Zijlstra 	init_rt_bandwidth(&tg->rt_bandwidth,
197391e43daSPeter Zijlstra 			ktime_to_ns(def_rt_bandwidth.rt_period), 0);
198391e43daSPeter Zijlstra 
199391e43daSPeter Zijlstra 	for_each_possible_cpu(i) {
200391e43daSPeter Zijlstra 		rt_rq = kzalloc_node(sizeof(struct rt_rq),
201391e43daSPeter Zijlstra 				     GFP_KERNEL, cpu_to_node(i));
202391e43daSPeter Zijlstra 		if (!rt_rq)
203391e43daSPeter Zijlstra 			goto err;
204391e43daSPeter Zijlstra 
205391e43daSPeter Zijlstra 		rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
206391e43daSPeter Zijlstra 				     GFP_KERNEL, cpu_to_node(i));
207391e43daSPeter Zijlstra 		if (!rt_se)
208391e43daSPeter Zijlstra 			goto err_free_rq;
209391e43daSPeter Zijlstra 
21007c54f7aSAbel Vesa 		init_rt_rq(rt_rq);
211391e43daSPeter Zijlstra 		rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
212391e43daSPeter Zijlstra 		init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
213391e43daSPeter Zijlstra 	}
214391e43daSPeter Zijlstra 
215391e43daSPeter Zijlstra 	return 1;
216391e43daSPeter Zijlstra 
217391e43daSPeter Zijlstra err_free_rq:
218391e43daSPeter Zijlstra 	kfree(rt_rq);
219391e43daSPeter Zijlstra err:
220391e43daSPeter Zijlstra 	return 0;
221391e43daSPeter Zijlstra }
222391e43daSPeter Zijlstra 
223391e43daSPeter Zijlstra #else /* CONFIG_RT_GROUP_SCHED */
224391e43daSPeter Zijlstra 
225391e43daSPeter Zijlstra #define rt_entity_is_task(rt_se) (1)
226391e43daSPeter Zijlstra 
227391e43daSPeter Zijlstra static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
228391e43daSPeter Zijlstra {
229391e43daSPeter Zijlstra 	return container_of(rt_se, struct task_struct, rt);
230391e43daSPeter Zijlstra }
231391e43daSPeter Zijlstra 
232391e43daSPeter Zijlstra static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
233391e43daSPeter Zijlstra {
234391e43daSPeter Zijlstra 	return container_of(rt_rq, struct rq, rt);
235391e43daSPeter Zijlstra }
236391e43daSPeter Zijlstra 
237653d07a6SKirill Tkhai static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
238391e43daSPeter Zijlstra {
239391e43daSPeter Zijlstra 	struct task_struct *p = rt_task_of(rt_se);
240653d07a6SKirill Tkhai 
241653d07a6SKirill Tkhai 	return task_rq(p);
242653d07a6SKirill Tkhai }
243653d07a6SKirill Tkhai 
244653d07a6SKirill Tkhai static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
245653d07a6SKirill Tkhai {
246653d07a6SKirill Tkhai 	struct rq *rq = rq_of_rt_se(rt_se);
247391e43daSPeter Zijlstra 
248391e43daSPeter Zijlstra 	return &rq->rt;
249391e43daSPeter Zijlstra }
250391e43daSPeter Zijlstra 
251391e43daSPeter Zijlstra void free_rt_sched_group(struct task_group *tg) { }
252391e43daSPeter Zijlstra 
253391e43daSPeter Zijlstra int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
254391e43daSPeter Zijlstra {
255391e43daSPeter Zijlstra 	return 1;
256391e43daSPeter Zijlstra }
257391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */
258391e43daSPeter Zijlstra 
259391e43daSPeter Zijlstra #ifdef CONFIG_SMP
260391e43daSPeter Zijlstra 
2618046d680SPeter Zijlstra static void pull_rt_task(struct rq *this_rq);
26238033c37SPeter Zijlstra 
263dc877341SPeter Zijlstra static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
264dc877341SPeter Zijlstra {
265dc877341SPeter Zijlstra 	/* Try to pull RT tasks here if we lower this rq's prio */
266dc877341SPeter Zijlstra 	return rq->rt.highest_prio.curr > prev->prio;
267dc877341SPeter Zijlstra }
268dc877341SPeter Zijlstra 
269391e43daSPeter Zijlstra static inline int rt_overloaded(struct rq *rq)
270391e43daSPeter Zijlstra {
271391e43daSPeter Zijlstra 	return atomic_read(&rq->rd->rto_count);
272391e43daSPeter Zijlstra }
273391e43daSPeter Zijlstra 
274391e43daSPeter Zijlstra static inline void rt_set_overload(struct rq *rq)
275391e43daSPeter Zijlstra {
276391e43daSPeter Zijlstra 	if (!rq->online)
277391e43daSPeter Zijlstra 		return;
278391e43daSPeter Zijlstra 
279391e43daSPeter Zijlstra 	cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
280391e43daSPeter Zijlstra 	/*
281391e43daSPeter Zijlstra 	 * Make sure the mask is visible before we set
282391e43daSPeter Zijlstra 	 * the overload count. That is checked to determine
283391e43daSPeter Zijlstra 	 * if we should look at the mask. It would be a shame
284391e43daSPeter Zijlstra 	 * if we looked at the mask, but the mask was not
285391e43daSPeter Zijlstra 	 * updated yet.
2867c3f2ab7SPeter Zijlstra 	 *
2877c3f2ab7SPeter Zijlstra 	 * Matched by the barrier in pull_rt_task().
288391e43daSPeter Zijlstra 	 */
2897c3f2ab7SPeter Zijlstra 	smp_wmb();
290391e43daSPeter Zijlstra 	atomic_inc(&rq->rd->rto_count);
291391e43daSPeter Zijlstra }
292391e43daSPeter Zijlstra 
293391e43daSPeter Zijlstra static inline void rt_clear_overload(struct rq *rq)
294391e43daSPeter Zijlstra {
295391e43daSPeter Zijlstra 	if (!rq->online)
296391e43daSPeter Zijlstra 		return;
297391e43daSPeter Zijlstra 
298391e43daSPeter Zijlstra 	/* the order here really doesn't matter */
299391e43daSPeter Zijlstra 	atomic_dec(&rq->rd->rto_count);
300391e43daSPeter Zijlstra 	cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
301391e43daSPeter Zijlstra }
302391e43daSPeter Zijlstra 
303391e43daSPeter Zijlstra static void update_rt_migration(struct rt_rq *rt_rq)
304391e43daSPeter Zijlstra {
305391e43daSPeter Zijlstra 	if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
306391e43daSPeter Zijlstra 		if (!rt_rq->overloaded) {
307391e43daSPeter Zijlstra 			rt_set_overload(rq_of_rt_rq(rt_rq));
308391e43daSPeter Zijlstra 			rt_rq->overloaded = 1;
309391e43daSPeter Zijlstra 		}
310391e43daSPeter Zijlstra 	} else if (rt_rq->overloaded) {
311391e43daSPeter Zijlstra 		rt_clear_overload(rq_of_rt_rq(rt_rq));
312391e43daSPeter Zijlstra 		rt_rq->overloaded = 0;
313391e43daSPeter Zijlstra 	}
314391e43daSPeter Zijlstra }
315391e43daSPeter Zijlstra 
316391e43daSPeter Zijlstra static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
317391e43daSPeter Zijlstra {
31829baa747SPeter Zijlstra 	struct task_struct *p;
31929baa747SPeter Zijlstra 
320391e43daSPeter Zijlstra 	if (!rt_entity_is_task(rt_se))
321391e43daSPeter Zijlstra 		return;
322391e43daSPeter Zijlstra 
32329baa747SPeter Zijlstra 	p = rt_task_of(rt_se);
324391e43daSPeter Zijlstra 	rt_rq = &rq_of_rt_rq(rt_rq)->rt;
325391e43daSPeter Zijlstra 
326391e43daSPeter Zijlstra 	rt_rq->rt_nr_total++;
3274b53a341SIngo Molnar 	if (p->nr_cpus_allowed > 1)
328391e43daSPeter Zijlstra 		rt_rq->rt_nr_migratory++;
329391e43daSPeter Zijlstra 
330391e43daSPeter Zijlstra 	update_rt_migration(rt_rq);
331391e43daSPeter Zijlstra }
332391e43daSPeter Zijlstra 
333391e43daSPeter Zijlstra static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
334391e43daSPeter Zijlstra {
33529baa747SPeter Zijlstra 	struct task_struct *p;
33629baa747SPeter Zijlstra 
337391e43daSPeter Zijlstra 	if (!rt_entity_is_task(rt_se))
338391e43daSPeter Zijlstra 		return;
339391e43daSPeter Zijlstra 
34029baa747SPeter Zijlstra 	p = rt_task_of(rt_se);
341391e43daSPeter Zijlstra 	rt_rq = &rq_of_rt_rq(rt_rq)->rt;
342391e43daSPeter Zijlstra 
343391e43daSPeter Zijlstra 	rt_rq->rt_nr_total--;
3444b53a341SIngo Molnar 	if (p->nr_cpus_allowed > 1)
345391e43daSPeter Zijlstra 		rt_rq->rt_nr_migratory--;
346391e43daSPeter Zijlstra 
347391e43daSPeter Zijlstra 	update_rt_migration(rt_rq);
348391e43daSPeter Zijlstra }
349391e43daSPeter Zijlstra 
350391e43daSPeter Zijlstra static inline int has_pushable_tasks(struct rq *rq)
351391e43daSPeter Zijlstra {
352391e43daSPeter Zijlstra 	return !plist_head_empty(&rq->rt.pushable_tasks);
353391e43daSPeter Zijlstra }
354391e43daSPeter Zijlstra 
355fd7a4bedSPeter Zijlstra static DEFINE_PER_CPU(struct callback_head, rt_push_head);
356fd7a4bedSPeter Zijlstra static DEFINE_PER_CPU(struct callback_head, rt_pull_head);
357e3fca9e7SPeter Zijlstra 
358e3fca9e7SPeter Zijlstra static void push_rt_tasks(struct rq *);
359fd7a4bedSPeter Zijlstra static void pull_rt_task(struct rq *);
360e3fca9e7SPeter Zijlstra 
361e3fca9e7SPeter Zijlstra static inline void queue_push_tasks(struct rq *rq)
362dc877341SPeter Zijlstra {
363e3fca9e7SPeter Zijlstra 	if (!has_pushable_tasks(rq))
364e3fca9e7SPeter Zijlstra 		return;
365e3fca9e7SPeter Zijlstra 
366fd7a4bedSPeter Zijlstra 	queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
367fd7a4bedSPeter Zijlstra }
368fd7a4bedSPeter Zijlstra 
369fd7a4bedSPeter Zijlstra static inline void queue_pull_task(struct rq *rq)
370fd7a4bedSPeter Zijlstra {
371fd7a4bedSPeter Zijlstra 	queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
372dc877341SPeter Zijlstra }
373dc877341SPeter Zijlstra 
374391e43daSPeter Zijlstra static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
375391e43daSPeter Zijlstra {
376391e43daSPeter Zijlstra 	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
377391e43daSPeter Zijlstra 	plist_node_init(&p->pushable_tasks, p->prio);
378391e43daSPeter Zijlstra 	plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
379391e43daSPeter Zijlstra 
380391e43daSPeter Zijlstra 	/* Update the highest prio pushable task */
381391e43daSPeter Zijlstra 	if (p->prio < rq->rt.highest_prio.next)
382391e43daSPeter Zijlstra 		rq->rt.highest_prio.next = p->prio;
383391e43daSPeter Zijlstra }
384391e43daSPeter Zijlstra 
385391e43daSPeter Zijlstra static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
386391e43daSPeter Zijlstra {
387391e43daSPeter Zijlstra 	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
388391e43daSPeter Zijlstra 
389391e43daSPeter Zijlstra 	/* Update the new highest prio pushable task */
390391e43daSPeter Zijlstra 	if (has_pushable_tasks(rq)) {
391391e43daSPeter Zijlstra 		p = plist_first_entry(&rq->rt.pushable_tasks,
392391e43daSPeter Zijlstra 				      struct task_struct, pushable_tasks);
393391e43daSPeter Zijlstra 		rq->rt.highest_prio.next = p->prio;
394391e43daSPeter Zijlstra 	} else
395391e43daSPeter Zijlstra 		rq->rt.highest_prio.next = MAX_RT_PRIO;
396391e43daSPeter Zijlstra }
397391e43daSPeter Zijlstra 
398391e43daSPeter Zijlstra #else
399391e43daSPeter Zijlstra 
400391e43daSPeter Zijlstra static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
401391e43daSPeter Zijlstra {
402391e43daSPeter Zijlstra }
403391e43daSPeter Zijlstra 
404391e43daSPeter Zijlstra static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
405391e43daSPeter Zijlstra {
406391e43daSPeter Zijlstra }
407391e43daSPeter Zijlstra 
408391e43daSPeter Zijlstra static inline
409391e43daSPeter Zijlstra void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
410391e43daSPeter Zijlstra {
411391e43daSPeter Zijlstra }
412391e43daSPeter Zijlstra 
413391e43daSPeter Zijlstra static inline
414391e43daSPeter Zijlstra void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
415391e43daSPeter Zijlstra {
416391e43daSPeter Zijlstra }
417391e43daSPeter Zijlstra 
418dc877341SPeter Zijlstra static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
419dc877341SPeter Zijlstra {
420dc877341SPeter Zijlstra 	return false;
421dc877341SPeter Zijlstra }
422dc877341SPeter Zijlstra 
4238046d680SPeter Zijlstra static inline void pull_rt_task(struct rq *this_rq)
424dc877341SPeter Zijlstra {
425dc877341SPeter Zijlstra }
426dc877341SPeter Zijlstra 
427e3fca9e7SPeter Zijlstra static inline void queue_push_tasks(struct rq *rq)
428dc877341SPeter Zijlstra {
429dc877341SPeter Zijlstra }
430391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
431391e43daSPeter Zijlstra 
432f4ebcbc0SKirill Tkhai static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
433f4ebcbc0SKirill Tkhai static void dequeue_top_rt_rq(struct rt_rq *rt_rq);
434f4ebcbc0SKirill Tkhai 
435391e43daSPeter Zijlstra static inline int on_rt_rq(struct sched_rt_entity *rt_se)
436391e43daSPeter Zijlstra {
437ff77e468SPeter Zijlstra 	return rt_se->on_rq;
438391e43daSPeter Zijlstra }
439391e43daSPeter Zijlstra 
440391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
441391e43daSPeter Zijlstra 
442391e43daSPeter Zijlstra static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
443391e43daSPeter Zijlstra {
444391e43daSPeter Zijlstra 	if (!rt_rq->tg)
445391e43daSPeter Zijlstra 		return RUNTIME_INF;
446391e43daSPeter Zijlstra 
447391e43daSPeter Zijlstra 	return rt_rq->rt_runtime;
448391e43daSPeter Zijlstra }
449391e43daSPeter Zijlstra 
450391e43daSPeter Zijlstra static inline u64 sched_rt_period(struct rt_rq *rt_rq)
451391e43daSPeter Zijlstra {
452391e43daSPeter Zijlstra 	return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
453391e43daSPeter Zijlstra }
454391e43daSPeter Zijlstra 
455391e43daSPeter Zijlstra typedef struct task_group *rt_rq_iter_t;
456391e43daSPeter Zijlstra 
457391e43daSPeter Zijlstra static inline struct task_group *next_task_group(struct task_group *tg)
458391e43daSPeter Zijlstra {
459391e43daSPeter Zijlstra 	do {
460391e43daSPeter Zijlstra 		tg = list_entry_rcu(tg->list.next,
461391e43daSPeter Zijlstra 			typeof(struct task_group), list);
462391e43daSPeter Zijlstra 	} while (&tg->list != &task_groups && task_group_is_autogroup(tg));
463391e43daSPeter Zijlstra 
464391e43daSPeter Zijlstra 	if (&tg->list == &task_groups)
465391e43daSPeter Zijlstra 		tg = NULL;
466391e43daSPeter Zijlstra 
467391e43daSPeter Zijlstra 	return tg;
468391e43daSPeter Zijlstra }
469391e43daSPeter Zijlstra 
470391e43daSPeter Zijlstra #define for_each_rt_rq(rt_rq, iter, rq)					\
471391e43daSPeter Zijlstra 	for (iter = container_of(&task_groups, typeof(*iter), list);	\
472391e43daSPeter Zijlstra 		(iter = next_task_group(iter)) &&			\
473391e43daSPeter Zijlstra 		(rt_rq = iter->rt_rq[cpu_of(rq)]);)
474391e43daSPeter Zijlstra 
475391e43daSPeter Zijlstra #define for_each_sched_rt_entity(rt_se) \
476391e43daSPeter Zijlstra 	for (; rt_se; rt_se = rt_se->parent)
477391e43daSPeter Zijlstra 
478391e43daSPeter Zijlstra static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
479391e43daSPeter Zijlstra {
480391e43daSPeter Zijlstra 	return rt_se->my_q;
481391e43daSPeter Zijlstra }
482391e43daSPeter Zijlstra 
483ff77e468SPeter Zijlstra static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
484ff77e468SPeter Zijlstra static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
485391e43daSPeter Zijlstra 
486391e43daSPeter Zijlstra static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
487391e43daSPeter Zijlstra {
488391e43daSPeter Zijlstra 	struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
4898875125eSKirill Tkhai 	struct rq *rq = rq_of_rt_rq(rt_rq);
490391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se;
491391e43daSPeter Zijlstra 
4928875125eSKirill Tkhai 	int cpu = cpu_of(rq);
493391e43daSPeter Zijlstra 
494391e43daSPeter Zijlstra 	rt_se = rt_rq->tg->rt_se[cpu];
495391e43daSPeter Zijlstra 
496391e43daSPeter Zijlstra 	if (rt_rq->rt_nr_running) {
497f4ebcbc0SKirill Tkhai 		if (!rt_se)
498f4ebcbc0SKirill Tkhai 			enqueue_top_rt_rq(rt_rq);
499f4ebcbc0SKirill Tkhai 		else if (!on_rt_rq(rt_se))
500ff77e468SPeter Zijlstra 			enqueue_rt_entity(rt_se, 0);
501f4ebcbc0SKirill Tkhai 
502391e43daSPeter Zijlstra 		if (rt_rq->highest_prio.curr < curr->prio)
5038875125eSKirill Tkhai 			resched_curr(rq);
504391e43daSPeter Zijlstra 	}
505391e43daSPeter Zijlstra }
506391e43daSPeter Zijlstra 
507391e43daSPeter Zijlstra static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
508391e43daSPeter Zijlstra {
509391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se;
510391e43daSPeter Zijlstra 	int cpu = cpu_of(rq_of_rt_rq(rt_rq));
511391e43daSPeter Zijlstra 
512391e43daSPeter Zijlstra 	rt_se = rt_rq->tg->rt_se[cpu];
513391e43daSPeter Zijlstra 
514f4ebcbc0SKirill Tkhai 	if (!rt_se)
515f4ebcbc0SKirill Tkhai 		dequeue_top_rt_rq(rt_rq);
516f4ebcbc0SKirill Tkhai 	else if (on_rt_rq(rt_se))
517ff77e468SPeter Zijlstra 		dequeue_rt_entity(rt_se, 0);
518391e43daSPeter Zijlstra }
519391e43daSPeter Zijlstra 
52046383648SKirill Tkhai static inline int rt_rq_throttled(struct rt_rq *rt_rq)
52146383648SKirill Tkhai {
52246383648SKirill Tkhai 	return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
52346383648SKirill Tkhai }
52446383648SKirill Tkhai 
525391e43daSPeter Zijlstra static int rt_se_boosted(struct sched_rt_entity *rt_se)
526391e43daSPeter Zijlstra {
527391e43daSPeter Zijlstra 	struct rt_rq *rt_rq = group_rt_rq(rt_se);
528391e43daSPeter Zijlstra 	struct task_struct *p;
529391e43daSPeter Zijlstra 
530391e43daSPeter Zijlstra 	if (rt_rq)
531391e43daSPeter Zijlstra 		return !!rt_rq->rt_nr_boosted;
532391e43daSPeter Zijlstra 
533391e43daSPeter Zijlstra 	p = rt_task_of(rt_se);
534391e43daSPeter Zijlstra 	return p->prio != p->normal_prio;
535391e43daSPeter Zijlstra }
536391e43daSPeter Zijlstra 
537391e43daSPeter Zijlstra #ifdef CONFIG_SMP
538391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void)
539391e43daSPeter Zijlstra {
540424c93feSNathan Zimmer 	return this_rq()->rd->span;
541391e43daSPeter Zijlstra }
542391e43daSPeter Zijlstra #else
543391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void)
544391e43daSPeter Zijlstra {
545391e43daSPeter Zijlstra 	return cpu_online_mask;
546391e43daSPeter Zijlstra }
547391e43daSPeter Zijlstra #endif
548391e43daSPeter Zijlstra 
549391e43daSPeter Zijlstra static inline
550391e43daSPeter Zijlstra struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
551391e43daSPeter Zijlstra {
552391e43daSPeter Zijlstra 	return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
553391e43daSPeter Zijlstra }
554391e43daSPeter Zijlstra 
555391e43daSPeter Zijlstra static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
556391e43daSPeter Zijlstra {
557391e43daSPeter Zijlstra 	return &rt_rq->tg->rt_bandwidth;
558391e43daSPeter Zijlstra }
559391e43daSPeter Zijlstra 
560391e43daSPeter Zijlstra #else /* !CONFIG_RT_GROUP_SCHED */
561391e43daSPeter Zijlstra 
562391e43daSPeter Zijlstra static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
563391e43daSPeter Zijlstra {
564391e43daSPeter Zijlstra 	return rt_rq->rt_runtime;
565391e43daSPeter Zijlstra }
566391e43daSPeter Zijlstra 
567391e43daSPeter Zijlstra static inline u64 sched_rt_period(struct rt_rq *rt_rq)
568391e43daSPeter Zijlstra {
569391e43daSPeter Zijlstra 	return ktime_to_ns(def_rt_bandwidth.rt_period);
570391e43daSPeter Zijlstra }
571391e43daSPeter Zijlstra 
572391e43daSPeter Zijlstra typedef struct rt_rq *rt_rq_iter_t;
573391e43daSPeter Zijlstra 
574391e43daSPeter Zijlstra #define for_each_rt_rq(rt_rq, iter, rq) \
575391e43daSPeter Zijlstra 	for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
576391e43daSPeter Zijlstra 
577391e43daSPeter Zijlstra #define for_each_sched_rt_entity(rt_se) \
578391e43daSPeter Zijlstra 	for (; rt_se; rt_se = NULL)
579391e43daSPeter Zijlstra 
580391e43daSPeter Zijlstra static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
581391e43daSPeter Zijlstra {
582391e43daSPeter Zijlstra 	return NULL;
583391e43daSPeter Zijlstra }
584391e43daSPeter Zijlstra 
585391e43daSPeter Zijlstra static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
586391e43daSPeter Zijlstra {
587f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_rq(rt_rq);
588f4ebcbc0SKirill Tkhai 
589f4ebcbc0SKirill Tkhai 	if (!rt_rq->rt_nr_running)
590f4ebcbc0SKirill Tkhai 		return;
591f4ebcbc0SKirill Tkhai 
592f4ebcbc0SKirill Tkhai 	enqueue_top_rt_rq(rt_rq);
5938875125eSKirill Tkhai 	resched_curr(rq);
594391e43daSPeter Zijlstra }
595391e43daSPeter Zijlstra 
596391e43daSPeter Zijlstra static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
597391e43daSPeter Zijlstra {
598f4ebcbc0SKirill Tkhai 	dequeue_top_rt_rq(rt_rq);
599391e43daSPeter Zijlstra }
600391e43daSPeter Zijlstra 
60146383648SKirill Tkhai static inline int rt_rq_throttled(struct rt_rq *rt_rq)
60246383648SKirill Tkhai {
60346383648SKirill Tkhai 	return rt_rq->rt_throttled;
60446383648SKirill Tkhai }
60546383648SKirill Tkhai 
606391e43daSPeter Zijlstra static inline const struct cpumask *sched_rt_period_mask(void)
607391e43daSPeter Zijlstra {
608391e43daSPeter Zijlstra 	return cpu_online_mask;
609391e43daSPeter Zijlstra }
610391e43daSPeter Zijlstra 
611391e43daSPeter Zijlstra static inline
612391e43daSPeter Zijlstra struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
613391e43daSPeter Zijlstra {
614391e43daSPeter Zijlstra 	return &cpu_rq(cpu)->rt;
615391e43daSPeter Zijlstra }
616391e43daSPeter Zijlstra 
617391e43daSPeter Zijlstra static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
618391e43daSPeter Zijlstra {
619391e43daSPeter Zijlstra 	return &def_rt_bandwidth;
620391e43daSPeter Zijlstra }
621391e43daSPeter Zijlstra 
622391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */
623391e43daSPeter Zijlstra 
624faa59937SJuri Lelli bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
625faa59937SJuri Lelli {
626faa59937SJuri Lelli 	struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
627faa59937SJuri Lelli 
628faa59937SJuri Lelli 	return (hrtimer_active(&rt_b->rt_period_timer) ||
629faa59937SJuri Lelli 		rt_rq->rt_time < rt_b->rt_runtime);
630faa59937SJuri Lelli }
631faa59937SJuri Lelli 
632391e43daSPeter Zijlstra #ifdef CONFIG_SMP
633391e43daSPeter Zijlstra /*
634391e43daSPeter Zijlstra  * We ran out of runtime, see if we can borrow some from our neighbours.
635391e43daSPeter Zijlstra  */
636269b26a5SJuri Lelli static void do_balance_runtime(struct rt_rq *rt_rq)
637391e43daSPeter Zijlstra {
638391e43daSPeter Zijlstra 	struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
639aa7f6730SShawn Bohrer 	struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
640269b26a5SJuri Lelli 	int i, weight;
641391e43daSPeter Zijlstra 	u64 rt_period;
642391e43daSPeter Zijlstra 
643391e43daSPeter Zijlstra 	weight = cpumask_weight(rd->span);
644391e43daSPeter Zijlstra 
645391e43daSPeter Zijlstra 	raw_spin_lock(&rt_b->rt_runtime_lock);
646391e43daSPeter Zijlstra 	rt_period = ktime_to_ns(rt_b->rt_period);
647391e43daSPeter Zijlstra 	for_each_cpu(i, rd->span) {
648391e43daSPeter Zijlstra 		struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
649391e43daSPeter Zijlstra 		s64 diff;
650391e43daSPeter Zijlstra 
651391e43daSPeter Zijlstra 		if (iter == rt_rq)
652391e43daSPeter Zijlstra 			continue;
653391e43daSPeter Zijlstra 
654391e43daSPeter Zijlstra 		raw_spin_lock(&iter->rt_runtime_lock);
655391e43daSPeter Zijlstra 		/*
656391e43daSPeter Zijlstra 		 * Either all rqs have inf runtime and there's nothing to steal
657391e43daSPeter Zijlstra 		 * or __disable_runtime() below sets a specific rq to inf to
658391e43daSPeter Zijlstra 		 * indicate its been disabled and disalow stealing.
659391e43daSPeter Zijlstra 		 */
660391e43daSPeter Zijlstra 		if (iter->rt_runtime == RUNTIME_INF)
661391e43daSPeter Zijlstra 			goto next;
662391e43daSPeter Zijlstra 
663391e43daSPeter Zijlstra 		/*
664391e43daSPeter Zijlstra 		 * From runqueues with spare time, take 1/n part of their
665391e43daSPeter Zijlstra 		 * spare time, but no more than our period.
666391e43daSPeter Zijlstra 		 */
667391e43daSPeter Zijlstra 		diff = iter->rt_runtime - iter->rt_time;
668391e43daSPeter Zijlstra 		if (diff > 0) {
669391e43daSPeter Zijlstra 			diff = div_u64((u64)diff, weight);
670391e43daSPeter Zijlstra 			if (rt_rq->rt_runtime + diff > rt_period)
671391e43daSPeter Zijlstra 				diff = rt_period - rt_rq->rt_runtime;
672391e43daSPeter Zijlstra 			iter->rt_runtime -= diff;
673391e43daSPeter Zijlstra 			rt_rq->rt_runtime += diff;
674391e43daSPeter Zijlstra 			if (rt_rq->rt_runtime == rt_period) {
675391e43daSPeter Zijlstra 				raw_spin_unlock(&iter->rt_runtime_lock);
676391e43daSPeter Zijlstra 				break;
677391e43daSPeter Zijlstra 			}
678391e43daSPeter Zijlstra 		}
679391e43daSPeter Zijlstra next:
680391e43daSPeter Zijlstra 		raw_spin_unlock(&iter->rt_runtime_lock);
681391e43daSPeter Zijlstra 	}
682391e43daSPeter Zijlstra 	raw_spin_unlock(&rt_b->rt_runtime_lock);
683391e43daSPeter Zijlstra }
684391e43daSPeter Zijlstra 
685391e43daSPeter Zijlstra /*
686391e43daSPeter Zijlstra  * Ensure this RQ takes back all the runtime it lend to its neighbours.
687391e43daSPeter Zijlstra  */
688391e43daSPeter Zijlstra static void __disable_runtime(struct rq *rq)
689391e43daSPeter Zijlstra {
690391e43daSPeter Zijlstra 	struct root_domain *rd = rq->rd;
691391e43daSPeter Zijlstra 	rt_rq_iter_t iter;
692391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
693391e43daSPeter Zijlstra 
694391e43daSPeter Zijlstra 	if (unlikely(!scheduler_running))
695391e43daSPeter Zijlstra 		return;
696391e43daSPeter Zijlstra 
697391e43daSPeter Zijlstra 	for_each_rt_rq(rt_rq, iter, rq) {
698391e43daSPeter Zijlstra 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
699391e43daSPeter Zijlstra 		s64 want;
700391e43daSPeter Zijlstra 		int i;
701391e43daSPeter Zijlstra 
702391e43daSPeter Zijlstra 		raw_spin_lock(&rt_b->rt_runtime_lock);
703391e43daSPeter Zijlstra 		raw_spin_lock(&rt_rq->rt_runtime_lock);
704391e43daSPeter Zijlstra 		/*
705391e43daSPeter Zijlstra 		 * Either we're all inf and nobody needs to borrow, or we're
706391e43daSPeter Zijlstra 		 * already disabled and thus have nothing to do, or we have
707391e43daSPeter Zijlstra 		 * exactly the right amount of runtime to take out.
708391e43daSPeter Zijlstra 		 */
709391e43daSPeter Zijlstra 		if (rt_rq->rt_runtime == RUNTIME_INF ||
710391e43daSPeter Zijlstra 				rt_rq->rt_runtime == rt_b->rt_runtime)
711391e43daSPeter Zijlstra 			goto balanced;
712391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
713391e43daSPeter Zijlstra 
714391e43daSPeter Zijlstra 		/*
715391e43daSPeter Zijlstra 		 * Calculate the difference between what we started out with
716391e43daSPeter Zijlstra 		 * and what we current have, that's the amount of runtime
717391e43daSPeter Zijlstra 		 * we lend and now have to reclaim.
718391e43daSPeter Zijlstra 		 */
719391e43daSPeter Zijlstra 		want = rt_b->rt_runtime - rt_rq->rt_runtime;
720391e43daSPeter Zijlstra 
721391e43daSPeter Zijlstra 		/*
722391e43daSPeter Zijlstra 		 * Greedy reclaim, take back as much as we can.
723391e43daSPeter Zijlstra 		 */
724391e43daSPeter Zijlstra 		for_each_cpu(i, rd->span) {
725391e43daSPeter Zijlstra 			struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
726391e43daSPeter Zijlstra 			s64 diff;
727391e43daSPeter Zijlstra 
728391e43daSPeter Zijlstra 			/*
729391e43daSPeter Zijlstra 			 * Can't reclaim from ourselves or disabled runqueues.
730391e43daSPeter Zijlstra 			 */
731391e43daSPeter Zijlstra 			if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
732391e43daSPeter Zijlstra 				continue;
733391e43daSPeter Zijlstra 
734391e43daSPeter Zijlstra 			raw_spin_lock(&iter->rt_runtime_lock);
735391e43daSPeter Zijlstra 			if (want > 0) {
736391e43daSPeter Zijlstra 				diff = min_t(s64, iter->rt_runtime, want);
737391e43daSPeter Zijlstra 				iter->rt_runtime -= diff;
738391e43daSPeter Zijlstra 				want -= diff;
739391e43daSPeter Zijlstra 			} else {
740391e43daSPeter Zijlstra 				iter->rt_runtime -= want;
741391e43daSPeter Zijlstra 				want -= want;
742391e43daSPeter Zijlstra 			}
743391e43daSPeter Zijlstra 			raw_spin_unlock(&iter->rt_runtime_lock);
744391e43daSPeter Zijlstra 
745391e43daSPeter Zijlstra 			if (!want)
746391e43daSPeter Zijlstra 				break;
747391e43daSPeter Zijlstra 		}
748391e43daSPeter Zijlstra 
749391e43daSPeter Zijlstra 		raw_spin_lock(&rt_rq->rt_runtime_lock);
750391e43daSPeter Zijlstra 		/*
751391e43daSPeter Zijlstra 		 * We cannot be left wanting - that would mean some runtime
752391e43daSPeter Zijlstra 		 * leaked out of the system.
753391e43daSPeter Zijlstra 		 */
754391e43daSPeter Zijlstra 		BUG_ON(want);
755391e43daSPeter Zijlstra balanced:
756391e43daSPeter Zijlstra 		/*
757391e43daSPeter Zijlstra 		 * Disable all the borrow logic by pretending we have inf
758391e43daSPeter Zijlstra 		 * runtime - in which case borrowing doesn't make sense.
759391e43daSPeter Zijlstra 		 */
760391e43daSPeter Zijlstra 		rt_rq->rt_runtime = RUNTIME_INF;
761a4c96ae3SPeter Boonstoppel 		rt_rq->rt_throttled = 0;
762391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
763391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_b->rt_runtime_lock);
76499b62567SKirill Tkhai 
76599b62567SKirill Tkhai 		/* Make rt_rq available for pick_next_task() */
76699b62567SKirill Tkhai 		sched_rt_rq_enqueue(rt_rq);
767391e43daSPeter Zijlstra 	}
768391e43daSPeter Zijlstra }
769391e43daSPeter Zijlstra 
770391e43daSPeter Zijlstra static void __enable_runtime(struct rq *rq)
771391e43daSPeter Zijlstra {
772391e43daSPeter Zijlstra 	rt_rq_iter_t iter;
773391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
774391e43daSPeter Zijlstra 
775391e43daSPeter Zijlstra 	if (unlikely(!scheduler_running))
776391e43daSPeter Zijlstra 		return;
777391e43daSPeter Zijlstra 
778391e43daSPeter Zijlstra 	/*
779391e43daSPeter Zijlstra 	 * Reset each runqueue's bandwidth settings
780391e43daSPeter Zijlstra 	 */
781391e43daSPeter Zijlstra 	for_each_rt_rq(rt_rq, iter, rq) {
782391e43daSPeter Zijlstra 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
783391e43daSPeter Zijlstra 
784391e43daSPeter Zijlstra 		raw_spin_lock(&rt_b->rt_runtime_lock);
785391e43daSPeter Zijlstra 		raw_spin_lock(&rt_rq->rt_runtime_lock);
786391e43daSPeter Zijlstra 		rt_rq->rt_runtime = rt_b->rt_runtime;
787391e43daSPeter Zijlstra 		rt_rq->rt_time = 0;
788391e43daSPeter Zijlstra 		rt_rq->rt_throttled = 0;
789391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
790391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_b->rt_runtime_lock);
791391e43daSPeter Zijlstra 	}
792391e43daSPeter Zijlstra }
793391e43daSPeter Zijlstra 
794269b26a5SJuri Lelli static void balance_runtime(struct rt_rq *rt_rq)
795391e43daSPeter Zijlstra {
796391e43daSPeter Zijlstra 	if (!sched_feat(RT_RUNTIME_SHARE))
797269b26a5SJuri Lelli 		return;
798391e43daSPeter Zijlstra 
799391e43daSPeter Zijlstra 	if (rt_rq->rt_time > rt_rq->rt_runtime) {
800391e43daSPeter Zijlstra 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
801269b26a5SJuri Lelli 		do_balance_runtime(rt_rq);
802391e43daSPeter Zijlstra 		raw_spin_lock(&rt_rq->rt_runtime_lock);
803391e43daSPeter Zijlstra 	}
804391e43daSPeter Zijlstra }
805391e43daSPeter Zijlstra #else /* !CONFIG_SMP */
806269b26a5SJuri Lelli static inline void balance_runtime(struct rt_rq *rt_rq) {}
807391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
808391e43daSPeter Zijlstra 
809391e43daSPeter Zijlstra static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
810391e43daSPeter Zijlstra {
81142c62a58SPeter Zijlstra 	int i, idle = 1, throttled = 0;
812391e43daSPeter Zijlstra 	const struct cpumask *span;
813391e43daSPeter Zijlstra 
814391e43daSPeter Zijlstra 	span = sched_rt_period_mask();
815e221d028SMike Galbraith #ifdef CONFIG_RT_GROUP_SCHED
816e221d028SMike Galbraith 	/*
817e221d028SMike Galbraith 	 * FIXME: isolated CPUs should really leave the root task group,
818e221d028SMike Galbraith 	 * whether they are isolcpus or were isolated via cpusets, lest
819e221d028SMike Galbraith 	 * the timer run on a CPU which does not service all runqueues,
820e221d028SMike Galbraith 	 * potentially leaving other CPUs indefinitely throttled.  If
821e221d028SMike Galbraith 	 * isolation is really required, the user will turn the throttle
822e221d028SMike Galbraith 	 * off to kill the perturbations it causes anyway.  Meanwhile,
823e221d028SMike Galbraith 	 * this maintains functionality for boot and/or troubleshooting.
824e221d028SMike Galbraith 	 */
825e221d028SMike Galbraith 	if (rt_b == &root_task_group.rt_bandwidth)
826e221d028SMike Galbraith 		span = cpu_online_mask;
827e221d028SMike Galbraith #endif
828391e43daSPeter Zijlstra 	for_each_cpu(i, span) {
829391e43daSPeter Zijlstra 		int enqueue = 0;
830391e43daSPeter Zijlstra 		struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
831391e43daSPeter Zijlstra 		struct rq *rq = rq_of_rt_rq(rt_rq);
832c249f255SDave Kleikamp 		int skip;
833c249f255SDave Kleikamp 
834c249f255SDave Kleikamp 		/*
835c249f255SDave Kleikamp 		 * When span == cpu_online_mask, taking each rq->lock
836c249f255SDave Kleikamp 		 * can be time-consuming. Try to avoid it when possible.
837c249f255SDave Kleikamp 		 */
838c249f255SDave Kleikamp 		raw_spin_lock(&rt_rq->rt_runtime_lock);
839c249f255SDave Kleikamp 		skip = !rt_rq->rt_time && !rt_rq->rt_nr_running;
840c249f255SDave Kleikamp 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
841c249f255SDave Kleikamp 		if (skip)
842c249f255SDave Kleikamp 			continue;
843391e43daSPeter Zijlstra 
844391e43daSPeter Zijlstra 		raw_spin_lock(&rq->lock);
845391e43daSPeter Zijlstra 		if (rt_rq->rt_time) {
846391e43daSPeter Zijlstra 			u64 runtime;
847391e43daSPeter Zijlstra 
848391e43daSPeter Zijlstra 			raw_spin_lock(&rt_rq->rt_runtime_lock);
849391e43daSPeter Zijlstra 			if (rt_rq->rt_throttled)
850391e43daSPeter Zijlstra 				balance_runtime(rt_rq);
851391e43daSPeter Zijlstra 			runtime = rt_rq->rt_runtime;
852391e43daSPeter Zijlstra 			rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
853391e43daSPeter Zijlstra 			if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
854391e43daSPeter Zijlstra 				rt_rq->rt_throttled = 0;
855391e43daSPeter Zijlstra 				enqueue = 1;
856391e43daSPeter Zijlstra 
857391e43daSPeter Zijlstra 				/*
8589edfbfedSPeter Zijlstra 				 * When we're idle and a woken (rt) task is
8599edfbfedSPeter Zijlstra 				 * throttled check_preempt_curr() will set
8609edfbfedSPeter Zijlstra 				 * skip_update and the time between the wakeup
8619edfbfedSPeter Zijlstra 				 * and this unthrottle will get accounted as
8629edfbfedSPeter Zijlstra 				 * 'runtime'.
863391e43daSPeter Zijlstra 				 */
864391e43daSPeter Zijlstra 				if (rt_rq->rt_nr_running && rq->curr == rq->idle)
8659edfbfedSPeter Zijlstra 					rq_clock_skip_update(rq, false);
866391e43daSPeter Zijlstra 			}
867391e43daSPeter Zijlstra 			if (rt_rq->rt_time || rt_rq->rt_nr_running)
868391e43daSPeter Zijlstra 				idle = 0;
869391e43daSPeter Zijlstra 			raw_spin_unlock(&rt_rq->rt_runtime_lock);
870391e43daSPeter Zijlstra 		} else if (rt_rq->rt_nr_running) {
871391e43daSPeter Zijlstra 			idle = 0;
872391e43daSPeter Zijlstra 			if (!rt_rq_throttled(rt_rq))
873391e43daSPeter Zijlstra 				enqueue = 1;
874391e43daSPeter Zijlstra 		}
87542c62a58SPeter Zijlstra 		if (rt_rq->rt_throttled)
87642c62a58SPeter Zijlstra 			throttled = 1;
877391e43daSPeter Zijlstra 
878391e43daSPeter Zijlstra 		if (enqueue)
879391e43daSPeter Zijlstra 			sched_rt_rq_enqueue(rt_rq);
880391e43daSPeter Zijlstra 		raw_spin_unlock(&rq->lock);
881391e43daSPeter Zijlstra 	}
882391e43daSPeter Zijlstra 
88342c62a58SPeter Zijlstra 	if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
88442c62a58SPeter Zijlstra 		return 1;
88542c62a58SPeter Zijlstra 
886391e43daSPeter Zijlstra 	return idle;
887391e43daSPeter Zijlstra }
888391e43daSPeter Zijlstra 
889391e43daSPeter Zijlstra static inline int rt_se_prio(struct sched_rt_entity *rt_se)
890391e43daSPeter Zijlstra {
891391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
892391e43daSPeter Zijlstra 	struct rt_rq *rt_rq = group_rt_rq(rt_se);
893391e43daSPeter Zijlstra 
894391e43daSPeter Zijlstra 	if (rt_rq)
895391e43daSPeter Zijlstra 		return rt_rq->highest_prio.curr;
896391e43daSPeter Zijlstra #endif
897391e43daSPeter Zijlstra 
898391e43daSPeter Zijlstra 	return rt_task_of(rt_se)->prio;
899391e43daSPeter Zijlstra }
900391e43daSPeter Zijlstra 
901391e43daSPeter Zijlstra static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
902391e43daSPeter Zijlstra {
903391e43daSPeter Zijlstra 	u64 runtime = sched_rt_runtime(rt_rq);
904391e43daSPeter Zijlstra 
905391e43daSPeter Zijlstra 	if (rt_rq->rt_throttled)
906391e43daSPeter Zijlstra 		return rt_rq_throttled(rt_rq);
907391e43daSPeter Zijlstra 
9085b680fd6SShan Hai 	if (runtime >= sched_rt_period(rt_rq))
909391e43daSPeter Zijlstra 		return 0;
910391e43daSPeter Zijlstra 
911391e43daSPeter Zijlstra 	balance_runtime(rt_rq);
912391e43daSPeter Zijlstra 	runtime = sched_rt_runtime(rt_rq);
913391e43daSPeter Zijlstra 	if (runtime == RUNTIME_INF)
914391e43daSPeter Zijlstra 		return 0;
915391e43daSPeter Zijlstra 
916391e43daSPeter Zijlstra 	if (rt_rq->rt_time > runtime) {
9177abc63b1SPeter Zijlstra 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
9187abc63b1SPeter Zijlstra 
9197abc63b1SPeter Zijlstra 		/*
9207abc63b1SPeter Zijlstra 		 * Don't actually throttle groups that have no runtime assigned
9217abc63b1SPeter Zijlstra 		 * but accrue some time due to boosting.
9227abc63b1SPeter Zijlstra 		 */
9237abc63b1SPeter Zijlstra 		if (likely(rt_b->rt_runtime)) {
924391e43daSPeter Zijlstra 			rt_rq->rt_throttled = 1;
925c224815dSJohn Stultz 			printk_deferred_once("sched: RT throttling activated\n");
9267abc63b1SPeter Zijlstra 		} else {
9277abc63b1SPeter Zijlstra 			/*
9287abc63b1SPeter Zijlstra 			 * In case we did anyway, make it go away,
9297abc63b1SPeter Zijlstra 			 * replenishment is a joke, since it will replenish us
9307abc63b1SPeter Zijlstra 			 * with exactly 0 ns.
9317abc63b1SPeter Zijlstra 			 */
9327abc63b1SPeter Zijlstra 			rt_rq->rt_time = 0;
9337abc63b1SPeter Zijlstra 		}
9347abc63b1SPeter Zijlstra 
935391e43daSPeter Zijlstra 		if (rt_rq_throttled(rt_rq)) {
936391e43daSPeter Zijlstra 			sched_rt_rq_dequeue(rt_rq);
937391e43daSPeter Zijlstra 			return 1;
938391e43daSPeter Zijlstra 		}
939391e43daSPeter Zijlstra 	}
940391e43daSPeter Zijlstra 
941391e43daSPeter Zijlstra 	return 0;
942391e43daSPeter Zijlstra }
943391e43daSPeter Zijlstra 
944391e43daSPeter Zijlstra /*
945391e43daSPeter Zijlstra  * Update the current task's runtime statistics. Skip current tasks that
946391e43daSPeter Zijlstra  * are not in our scheduling class.
947391e43daSPeter Zijlstra  */
948391e43daSPeter Zijlstra static void update_curr_rt(struct rq *rq)
949391e43daSPeter Zijlstra {
950391e43daSPeter Zijlstra 	struct task_struct *curr = rq->curr;
951391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se = &curr->rt;
952391e43daSPeter Zijlstra 	u64 delta_exec;
953391e43daSPeter Zijlstra 
954391e43daSPeter Zijlstra 	if (curr->sched_class != &rt_sched_class)
955391e43daSPeter Zijlstra 		return;
956391e43daSPeter Zijlstra 
95778becc27SFrederic Weisbecker 	delta_exec = rq_clock_task(rq) - curr->se.exec_start;
958fc79e240SKirill Tkhai 	if (unlikely((s64)delta_exec <= 0))
959fc79e240SKirill Tkhai 		return;
960391e43daSPeter Zijlstra 
96158919e83SRafael J. Wysocki 	/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
962674e7541SViresh Kumar 	cpufreq_update_util(rq, SCHED_CPUFREQ_RT);
963594dd290SWanpeng Li 
96442c62a58SPeter Zijlstra 	schedstat_set(curr->se.statistics.exec_max,
96542c62a58SPeter Zijlstra 		      max(curr->se.statistics.exec_max, delta_exec));
966391e43daSPeter Zijlstra 
967391e43daSPeter Zijlstra 	curr->se.sum_exec_runtime += delta_exec;
968391e43daSPeter Zijlstra 	account_group_exec_runtime(curr, delta_exec);
969391e43daSPeter Zijlstra 
97078becc27SFrederic Weisbecker 	curr->se.exec_start = rq_clock_task(rq);
971391e43daSPeter Zijlstra 	cpuacct_charge(curr, delta_exec);
972391e43daSPeter Zijlstra 
973391e43daSPeter Zijlstra 	sched_rt_avg_update(rq, delta_exec);
974391e43daSPeter Zijlstra 
975391e43daSPeter Zijlstra 	if (!rt_bandwidth_enabled())
976391e43daSPeter Zijlstra 		return;
977391e43daSPeter Zijlstra 
978391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se) {
9790b07939cSGiedrius Rekasius 		struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
980391e43daSPeter Zijlstra 
981391e43daSPeter Zijlstra 		if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
982391e43daSPeter Zijlstra 			raw_spin_lock(&rt_rq->rt_runtime_lock);
983391e43daSPeter Zijlstra 			rt_rq->rt_time += delta_exec;
984391e43daSPeter Zijlstra 			if (sched_rt_runtime_exceeded(rt_rq))
9858875125eSKirill Tkhai 				resched_curr(rq);
986391e43daSPeter Zijlstra 			raw_spin_unlock(&rt_rq->rt_runtime_lock);
987391e43daSPeter Zijlstra 		}
988391e43daSPeter Zijlstra 	}
989391e43daSPeter Zijlstra }
990391e43daSPeter Zijlstra 
991f4ebcbc0SKirill Tkhai static void
992f4ebcbc0SKirill Tkhai dequeue_top_rt_rq(struct rt_rq *rt_rq)
993f4ebcbc0SKirill Tkhai {
994f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_rq(rt_rq);
995f4ebcbc0SKirill Tkhai 
996f4ebcbc0SKirill Tkhai 	BUG_ON(&rq->rt != rt_rq);
997f4ebcbc0SKirill Tkhai 
998f4ebcbc0SKirill Tkhai 	if (!rt_rq->rt_queued)
999f4ebcbc0SKirill Tkhai 		return;
1000f4ebcbc0SKirill Tkhai 
1001f4ebcbc0SKirill Tkhai 	BUG_ON(!rq->nr_running);
1002f4ebcbc0SKirill Tkhai 
100372465447SKirill Tkhai 	sub_nr_running(rq, rt_rq->rt_nr_running);
1004f4ebcbc0SKirill Tkhai 	rt_rq->rt_queued = 0;
1005f4ebcbc0SKirill Tkhai }
1006f4ebcbc0SKirill Tkhai 
1007f4ebcbc0SKirill Tkhai static void
1008f4ebcbc0SKirill Tkhai enqueue_top_rt_rq(struct rt_rq *rt_rq)
1009f4ebcbc0SKirill Tkhai {
1010f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_rq(rt_rq);
1011f4ebcbc0SKirill Tkhai 
1012f4ebcbc0SKirill Tkhai 	BUG_ON(&rq->rt != rt_rq);
1013f4ebcbc0SKirill Tkhai 
1014f4ebcbc0SKirill Tkhai 	if (rt_rq->rt_queued)
1015f4ebcbc0SKirill Tkhai 		return;
1016f4ebcbc0SKirill Tkhai 	if (rt_rq_throttled(rt_rq) || !rt_rq->rt_nr_running)
1017f4ebcbc0SKirill Tkhai 		return;
1018f4ebcbc0SKirill Tkhai 
101972465447SKirill Tkhai 	add_nr_running(rq, rt_rq->rt_nr_running);
1020f4ebcbc0SKirill Tkhai 	rt_rq->rt_queued = 1;
1021f4ebcbc0SKirill Tkhai }
1022f4ebcbc0SKirill Tkhai 
1023391e43daSPeter Zijlstra #if defined CONFIG_SMP
1024391e43daSPeter Zijlstra 
1025391e43daSPeter Zijlstra static void
1026391e43daSPeter Zijlstra inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1027391e43daSPeter Zijlstra {
1028391e43daSPeter Zijlstra 	struct rq *rq = rq_of_rt_rq(rt_rq);
1029391e43daSPeter Zijlstra 
1030757dfcaaSKirill Tkhai #ifdef CONFIG_RT_GROUP_SCHED
1031757dfcaaSKirill Tkhai 	/*
1032757dfcaaSKirill Tkhai 	 * Change rq's cpupri only if rt_rq is the top queue.
1033757dfcaaSKirill Tkhai 	 */
1034757dfcaaSKirill Tkhai 	if (&rq->rt != rt_rq)
1035757dfcaaSKirill Tkhai 		return;
1036757dfcaaSKirill Tkhai #endif
1037391e43daSPeter Zijlstra 	if (rq->online && prio < prev_prio)
1038391e43daSPeter Zijlstra 		cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
1039391e43daSPeter Zijlstra }
1040391e43daSPeter Zijlstra 
1041391e43daSPeter Zijlstra static void
1042391e43daSPeter Zijlstra dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1043391e43daSPeter Zijlstra {
1044391e43daSPeter Zijlstra 	struct rq *rq = rq_of_rt_rq(rt_rq);
1045391e43daSPeter Zijlstra 
1046757dfcaaSKirill Tkhai #ifdef CONFIG_RT_GROUP_SCHED
1047757dfcaaSKirill Tkhai 	/*
1048757dfcaaSKirill Tkhai 	 * Change rq's cpupri only if rt_rq is the top queue.
1049757dfcaaSKirill Tkhai 	 */
1050757dfcaaSKirill Tkhai 	if (&rq->rt != rt_rq)
1051757dfcaaSKirill Tkhai 		return;
1052757dfcaaSKirill Tkhai #endif
1053391e43daSPeter Zijlstra 	if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1054391e43daSPeter Zijlstra 		cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1055391e43daSPeter Zijlstra }
1056391e43daSPeter Zijlstra 
1057391e43daSPeter Zijlstra #else /* CONFIG_SMP */
1058391e43daSPeter Zijlstra 
1059391e43daSPeter Zijlstra static inline
1060391e43daSPeter Zijlstra void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1061391e43daSPeter Zijlstra static inline
1062391e43daSPeter Zijlstra void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1063391e43daSPeter Zijlstra 
1064391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
1065391e43daSPeter Zijlstra 
1066391e43daSPeter Zijlstra #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1067391e43daSPeter Zijlstra static void
1068391e43daSPeter Zijlstra inc_rt_prio(struct rt_rq *rt_rq, int prio)
1069391e43daSPeter Zijlstra {
1070391e43daSPeter Zijlstra 	int prev_prio = rt_rq->highest_prio.curr;
1071391e43daSPeter Zijlstra 
1072391e43daSPeter Zijlstra 	if (prio < prev_prio)
1073391e43daSPeter Zijlstra 		rt_rq->highest_prio.curr = prio;
1074391e43daSPeter Zijlstra 
1075391e43daSPeter Zijlstra 	inc_rt_prio_smp(rt_rq, prio, prev_prio);
1076391e43daSPeter Zijlstra }
1077391e43daSPeter Zijlstra 
1078391e43daSPeter Zijlstra static void
1079391e43daSPeter Zijlstra dec_rt_prio(struct rt_rq *rt_rq, int prio)
1080391e43daSPeter Zijlstra {
1081391e43daSPeter Zijlstra 	int prev_prio = rt_rq->highest_prio.curr;
1082391e43daSPeter Zijlstra 
1083391e43daSPeter Zijlstra 	if (rt_rq->rt_nr_running) {
1084391e43daSPeter Zijlstra 
1085391e43daSPeter Zijlstra 		WARN_ON(prio < prev_prio);
1086391e43daSPeter Zijlstra 
1087391e43daSPeter Zijlstra 		/*
1088391e43daSPeter Zijlstra 		 * This may have been our highest task, and therefore
1089391e43daSPeter Zijlstra 		 * we may have some recomputation to do
1090391e43daSPeter Zijlstra 		 */
1091391e43daSPeter Zijlstra 		if (prio == prev_prio) {
1092391e43daSPeter Zijlstra 			struct rt_prio_array *array = &rt_rq->active;
1093391e43daSPeter Zijlstra 
1094391e43daSPeter Zijlstra 			rt_rq->highest_prio.curr =
1095391e43daSPeter Zijlstra 				sched_find_first_bit(array->bitmap);
1096391e43daSPeter Zijlstra 		}
1097391e43daSPeter Zijlstra 
1098391e43daSPeter Zijlstra 	} else
1099391e43daSPeter Zijlstra 		rt_rq->highest_prio.curr = MAX_RT_PRIO;
1100391e43daSPeter Zijlstra 
1101391e43daSPeter Zijlstra 	dec_rt_prio_smp(rt_rq, prio, prev_prio);
1102391e43daSPeter Zijlstra }
1103391e43daSPeter Zijlstra 
1104391e43daSPeter Zijlstra #else
1105391e43daSPeter Zijlstra 
1106391e43daSPeter Zijlstra static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
1107391e43daSPeter Zijlstra static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1108391e43daSPeter Zijlstra 
1109391e43daSPeter Zijlstra #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1110391e43daSPeter Zijlstra 
1111391e43daSPeter Zijlstra #ifdef CONFIG_RT_GROUP_SCHED
1112391e43daSPeter Zijlstra 
1113391e43daSPeter Zijlstra static void
1114391e43daSPeter Zijlstra inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1115391e43daSPeter Zijlstra {
1116391e43daSPeter Zijlstra 	if (rt_se_boosted(rt_se))
1117391e43daSPeter Zijlstra 		rt_rq->rt_nr_boosted++;
1118391e43daSPeter Zijlstra 
1119391e43daSPeter Zijlstra 	if (rt_rq->tg)
1120391e43daSPeter Zijlstra 		start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
1121391e43daSPeter Zijlstra }
1122391e43daSPeter Zijlstra 
1123391e43daSPeter Zijlstra static void
1124391e43daSPeter Zijlstra dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1125391e43daSPeter Zijlstra {
1126391e43daSPeter Zijlstra 	if (rt_se_boosted(rt_se))
1127391e43daSPeter Zijlstra 		rt_rq->rt_nr_boosted--;
1128391e43daSPeter Zijlstra 
1129391e43daSPeter Zijlstra 	WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
1130391e43daSPeter Zijlstra }
1131391e43daSPeter Zijlstra 
1132391e43daSPeter Zijlstra #else /* CONFIG_RT_GROUP_SCHED */
1133391e43daSPeter Zijlstra 
1134391e43daSPeter Zijlstra static void
1135391e43daSPeter Zijlstra inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1136391e43daSPeter Zijlstra {
1137391e43daSPeter Zijlstra 	start_rt_bandwidth(&def_rt_bandwidth);
1138391e43daSPeter Zijlstra }
1139391e43daSPeter Zijlstra 
1140391e43daSPeter Zijlstra static inline
1141391e43daSPeter Zijlstra void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1142391e43daSPeter Zijlstra 
1143391e43daSPeter Zijlstra #endif /* CONFIG_RT_GROUP_SCHED */
1144391e43daSPeter Zijlstra 
1145391e43daSPeter Zijlstra static inline
114622abdef3SKirill Tkhai unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
114722abdef3SKirill Tkhai {
114822abdef3SKirill Tkhai 	struct rt_rq *group_rq = group_rt_rq(rt_se);
114922abdef3SKirill Tkhai 
115022abdef3SKirill Tkhai 	if (group_rq)
115122abdef3SKirill Tkhai 		return group_rq->rt_nr_running;
115222abdef3SKirill Tkhai 	else
115322abdef3SKirill Tkhai 		return 1;
115422abdef3SKirill Tkhai }
115522abdef3SKirill Tkhai 
115622abdef3SKirill Tkhai static inline
115701d36d0aSFrederic Weisbecker unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
115801d36d0aSFrederic Weisbecker {
115901d36d0aSFrederic Weisbecker 	struct rt_rq *group_rq = group_rt_rq(rt_se);
116001d36d0aSFrederic Weisbecker 	struct task_struct *tsk;
116101d36d0aSFrederic Weisbecker 
116201d36d0aSFrederic Weisbecker 	if (group_rq)
116301d36d0aSFrederic Weisbecker 		return group_rq->rr_nr_running;
116401d36d0aSFrederic Weisbecker 
116501d36d0aSFrederic Weisbecker 	tsk = rt_task_of(rt_se);
116601d36d0aSFrederic Weisbecker 
116701d36d0aSFrederic Weisbecker 	return (tsk->policy == SCHED_RR) ? 1 : 0;
116801d36d0aSFrederic Weisbecker }
116901d36d0aSFrederic Weisbecker 
117001d36d0aSFrederic Weisbecker static inline
1171391e43daSPeter Zijlstra void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1172391e43daSPeter Zijlstra {
1173391e43daSPeter Zijlstra 	int prio = rt_se_prio(rt_se);
1174391e43daSPeter Zijlstra 
1175391e43daSPeter Zijlstra 	WARN_ON(!rt_prio(prio));
117622abdef3SKirill Tkhai 	rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
117701d36d0aSFrederic Weisbecker 	rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
1178391e43daSPeter Zijlstra 
1179391e43daSPeter Zijlstra 	inc_rt_prio(rt_rq, prio);
1180391e43daSPeter Zijlstra 	inc_rt_migration(rt_se, rt_rq);
1181391e43daSPeter Zijlstra 	inc_rt_group(rt_se, rt_rq);
1182391e43daSPeter Zijlstra }
1183391e43daSPeter Zijlstra 
1184391e43daSPeter Zijlstra static inline
1185391e43daSPeter Zijlstra void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1186391e43daSPeter Zijlstra {
1187391e43daSPeter Zijlstra 	WARN_ON(!rt_prio(rt_se_prio(rt_se)));
1188391e43daSPeter Zijlstra 	WARN_ON(!rt_rq->rt_nr_running);
118922abdef3SKirill Tkhai 	rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
119001d36d0aSFrederic Weisbecker 	rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
1191391e43daSPeter Zijlstra 
1192391e43daSPeter Zijlstra 	dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1193391e43daSPeter Zijlstra 	dec_rt_migration(rt_se, rt_rq);
1194391e43daSPeter Zijlstra 	dec_rt_group(rt_se, rt_rq);
1195391e43daSPeter Zijlstra }
1196391e43daSPeter Zijlstra 
1197ff77e468SPeter Zijlstra /*
1198ff77e468SPeter Zijlstra  * Change rt_se->run_list location unless SAVE && !MOVE
1199ff77e468SPeter Zijlstra  *
1200ff77e468SPeter Zijlstra  * assumes ENQUEUE/DEQUEUE flags match
1201ff77e468SPeter Zijlstra  */
1202ff77e468SPeter Zijlstra static inline bool move_entity(unsigned int flags)
1203ff77e468SPeter Zijlstra {
1204ff77e468SPeter Zijlstra 	if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
1205ff77e468SPeter Zijlstra 		return false;
1206ff77e468SPeter Zijlstra 
1207ff77e468SPeter Zijlstra 	return true;
1208ff77e468SPeter Zijlstra }
1209ff77e468SPeter Zijlstra 
1210ff77e468SPeter Zijlstra static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
1211ff77e468SPeter Zijlstra {
1212ff77e468SPeter Zijlstra 	list_del_init(&rt_se->run_list);
1213ff77e468SPeter Zijlstra 
1214ff77e468SPeter Zijlstra 	if (list_empty(array->queue + rt_se_prio(rt_se)))
1215ff77e468SPeter Zijlstra 		__clear_bit(rt_se_prio(rt_se), array->bitmap);
1216ff77e468SPeter Zijlstra 
1217ff77e468SPeter Zijlstra 	rt_se->on_list = 0;
1218ff77e468SPeter Zijlstra }
1219ff77e468SPeter Zijlstra 
1220ff77e468SPeter Zijlstra static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1221391e43daSPeter Zijlstra {
1222391e43daSPeter Zijlstra 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1223391e43daSPeter Zijlstra 	struct rt_prio_array *array = &rt_rq->active;
1224391e43daSPeter Zijlstra 	struct rt_rq *group_rq = group_rt_rq(rt_se);
1225391e43daSPeter Zijlstra 	struct list_head *queue = array->queue + rt_se_prio(rt_se);
1226391e43daSPeter Zijlstra 
1227391e43daSPeter Zijlstra 	/*
1228391e43daSPeter Zijlstra 	 * Don't enqueue the group if its throttled, or when empty.
1229391e43daSPeter Zijlstra 	 * The latter is a consequence of the former when a child group
1230391e43daSPeter Zijlstra 	 * get throttled and the current group doesn't have any other
1231391e43daSPeter Zijlstra 	 * active members.
1232391e43daSPeter Zijlstra 	 */
1233ff77e468SPeter Zijlstra 	if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
1234ff77e468SPeter Zijlstra 		if (rt_se->on_list)
1235ff77e468SPeter Zijlstra 			__delist_rt_entity(rt_se, array);
1236391e43daSPeter Zijlstra 		return;
1237ff77e468SPeter Zijlstra 	}
1238391e43daSPeter Zijlstra 
1239ff77e468SPeter Zijlstra 	if (move_entity(flags)) {
1240ff77e468SPeter Zijlstra 		WARN_ON_ONCE(rt_se->on_list);
1241ff77e468SPeter Zijlstra 		if (flags & ENQUEUE_HEAD)
1242391e43daSPeter Zijlstra 			list_add(&rt_se->run_list, queue);
1243391e43daSPeter Zijlstra 		else
1244391e43daSPeter Zijlstra 			list_add_tail(&rt_se->run_list, queue);
1245ff77e468SPeter Zijlstra 
1246391e43daSPeter Zijlstra 		__set_bit(rt_se_prio(rt_se), array->bitmap);
1247ff77e468SPeter Zijlstra 		rt_se->on_list = 1;
1248ff77e468SPeter Zijlstra 	}
1249ff77e468SPeter Zijlstra 	rt_se->on_rq = 1;
1250391e43daSPeter Zijlstra 
1251391e43daSPeter Zijlstra 	inc_rt_tasks(rt_se, rt_rq);
1252391e43daSPeter Zijlstra }
1253391e43daSPeter Zijlstra 
1254ff77e468SPeter Zijlstra static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1255391e43daSPeter Zijlstra {
1256391e43daSPeter Zijlstra 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1257391e43daSPeter Zijlstra 	struct rt_prio_array *array = &rt_rq->active;
1258391e43daSPeter Zijlstra 
1259ff77e468SPeter Zijlstra 	if (move_entity(flags)) {
1260ff77e468SPeter Zijlstra 		WARN_ON_ONCE(!rt_se->on_list);
1261ff77e468SPeter Zijlstra 		__delist_rt_entity(rt_se, array);
1262ff77e468SPeter Zijlstra 	}
1263ff77e468SPeter Zijlstra 	rt_se->on_rq = 0;
1264391e43daSPeter Zijlstra 
1265391e43daSPeter Zijlstra 	dec_rt_tasks(rt_se, rt_rq);
1266391e43daSPeter Zijlstra }
1267391e43daSPeter Zijlstra 
1268391e43daSPeter Zijlstra /*
1269391e43daSPeter Zijlstra  * Because the prio of an upper entry depends on the lower
1270391e43daSPeter Zijlstra  * entries, we must remove entries top - down.
1271391e43daSPeter Zijlstra  */
1272ff77e468SPeter Zijlstra static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
1273391e43daSPeter Zijlstra {
1274391e43daSPeter Zijlstra 	struct sched_rt_entity *back = NULL;
1275391e43daSPeter Zijlstra 
1276391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se) {
1277391e43daSPeter Zijlstra 		rt_se->back = back;
1278391e43daSPeter Zijlstra 		back = rt_se;
1279391e43daSPeter Zijlstra 	}
1280391e43daSPeter Zijlstra 
1281f4ebcbc0SKirill Tkhai 	dequeue_top_rt_rq(rt_rq_of_se(back));
1282f4ebcbc0SKirill Tkhai 
1283391e43daSPeter Zijlstra 	for (rt_se = back; rt_se; rt_se = rt_se->back) {
1284391e43daSPeter Zijlstra 		if (on_rt_rq(rt_se))
1285ff77e468SPeter Zijlstra 			__dequeue_rt_entity(rt_se, flags);
1286391e43daSPeter Zijlstra 	}
1287391e43daSPeter Zijlstra }
1288391e43daSPeter Zijlstra 
1289ff77e468SPeter Zijlstra static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1290391e43daSPeter Zijlstra {
1291f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_se(rt_se);
1292f4ebcbc0SKirill Tkhai 
1293ff77e468SPeter Zijlstra 	dequeue_rt_stack(rt_se, flags);
1294391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se)
1295ff77e468SPeter Zijlstra 		__enqueue_rt_entity(rt_se, flags);
1296f4ebcbc0SKirill Tkhai 	enqueue_top_rt_rq(&rq->rt);
1297391e43daSPeter Zijlstra }
1298391e43daSPeter Zijlstra 
1299ff77e468SPeter Zijlstra static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1300391e43daSPeter Zijlstra {
1301f4ebcbc0SKirill Tkhai 	struct rq *rq = rq_of_rt_se(rt_se);
1302f4ebcbc0SKirill Tkhai 
1303ff77e468SPeter Zijlstra 	dequeue_rt_stack(rt_se, flags);
1304391e43daSPeter Zijlstra 
1305391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se) {
1306391e43daSPeter Zijlstra 		struct rt_rq *rt_rq = group_rt_rq(rt_se);
1307391e43daSPeter Zijlstra 
1308391e43daSPeter Zijlstra 		if (rt_rq && rt_rq->rt_nr_running)
1309ff77e468SPeter Zijlstra 			__enqueue_rt_entity(rt_se, flags);
1310391e43daSPeter Zijlstra 	}
1311f4ebcbc0SKirill Tkhai 	enqueue_top_rt_rq(&rq->rt);
1312391e43daSPeter Zijlstra }
1313391e43daSPeter Zijlstra 
1314391e43daSPeter Zijlstra /*
1315391e43daSPeter Zijlstra  * Adding/removing a task to/from a priority array:
1316391e43daSPeter Zijlstra  */
1317391e43daSPeter Zijlstra static void
1318391e43daSPeter Zijlstra enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1319391e43daSPeter Zijlstra {
1320391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se = &p->rt;
1321391e43daSPeter Zijlstra 
1322391e43daSPeter Zijlstra 	if (flags & ENQUEUE_WAKEUP)
1323391e43daSPeter Zijlstra 		rt_se->timeout = 0;
1324391e43daSPeter Zijlstra 
1325ff77e468SPeter Zijlstra 	enqueue_rt_entity(rt_se, flags);
1326391e43daSPeter Zijlstra 
13274b53a341SIngo Molnar 	if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1328391e43daSPeter Zijlstra 		enqueue_pushable_task(rq, p);
1329391e43daSPeter Zijlstra }
1330391e43daSPeter Zijlstra 
1331391e43daSPeter Zijlstra static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1332391e43daSPeter Zijlstra {
1333391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se = &p->rt;
1334391e43daSPeter Zijlstra 
1335391e43daSPeter Zijlstra 	update_curr_rt(rq);
1336ff77e468SPeter Zijlstra 	dequeue_rt_entity(rt_se, flags);
1337391e43daSPeter Zijlstra 
1338391e43daSPeter Zijlstra 	dequeue_pushable_task(rq, p);
1339391e43daSPeter Zijlstra }
1340391e43daSPeter Zijlstra 
1341391e43daSPeter Zijlstra /*
1342391e43daSPeter Zijlstra  * Put task to the head or the end of the run list without the overhead of
1343391e43daSPeter Zijlstra  * dequeue followed by enqueue.
1344391e43daSPeter Zijlstra  */
1345391e43daSPeter Zijlstra static void
1346391e43daSPeter Zijlstra requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
1347391e43daSPeter Zijlstra {
1348391e43daSPeter Zijlstra 	if (on_rt_rq(rt_se)) {
1349391e43daSPeter Zijlstra 		struct rt_prio_array *array = &rt_rq->active;
1350391e43daSPeter Zijlstra 		struct list_head *queue = array->queue + rt_se_prio(rt_se);
1351391e43daSPeter Zijlstra 
1352391e43daSPeter Zijlstra 		if (head)
1353391e43daSPeter Zijlstra 			list_move(&rt_se->run_list, queue);
1354391e43daSPeter Zijlstra 		else
1355391e43daSPeter Zijlstra 			list_move_tail(&rt_se->run_list, queue);
1356391e43daSPeter Zijlstra 	}
1357391e43daSPeter Zijlstra }
1358391e43daSPeter Zijlstra 
1359391e43daSPeter Zijlstra static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
1360391e43daSPeter Zijlstra {
1361391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se = &p->rt;
1362391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
1363391e43daSPeter Zijlstra 
1364391e43daSPeter Zijlstra 	for_each_sched_rt_entity(rt_se) {
1365391e43daSPeter Zijlstra 		rt_rq = rt_rq_of_se(rt_se);
1366391e43daSPeter Zijlstra 		requeue_rt_entity(rt_rq, rt_se, head);
1367391e43daSPeter Zijlstra 	}
1368391e43daSPeter Zijlstra }
1369391e43daSPeter Zijlstra 
1370391e43daSPeter Zijlstra static void yield_task_rt(struct rq *rq)
1371391e43daSPeter Zijlstra {
1372391e43daSPeter Zijlstra 	requeue_task_rt(rq, rq->curr, 0);
1373391e43daSPeter Zijlstra }
1374391e43daSPeter Zijlstra 
1375391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1376391e43daSPeter Zijlstra static int find_lowest_rq(struct task_struct *task);
1377391e43daSPeter Zijlstra 
1378391e43daSPeter Zijlstra static int
1379ac66f547SPeter Zijlstra select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
1380391e43daSPeter Zijlstra {
1381391e43daSPeter Zijlstra 	struct task_struct *curr;
1382391e43daSPeter Zijlstra 	struct rq *rq;
1383391e43daSPeter Zijlstra 
1384391e43daSPeter Zijlstra 	/* For anything but wake ups, just return the task_cpu */
1385391e43daSPeter Zijlstra 	if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
1386391e43daSPeter Zijlstra 		goto out;
1387391e43daSPeter Zijlstra 
1388391e43daSPeter Zijlstra 	rq = cpu_rq(cpu);
1389391e43daSPeter Zijlstra 
1390391e43daSPeter Zijlstra 	rcu_read_lock();
1391316c1608SJason Low 	curr = READ_ONCE(rq->curr); /* unlocked access */
1392391e43daSPeter Zijlstra 
1393391e43daSPeter Zijlstra 	/*
1394391e43daSPeter Zijlstra 	 * If the current task on @p's runqueue is an RT task, then
1395391e43daSPeter Zijlstra 	 * try to see if we can wake this RT task up on another
1396391e43daSPeter Zijlstra 	 * runqueue. Otherwise simply start this RT task
1397391e43daSPeter Zijlstra 	 * on its current runqueue.
1398391e43daSPeter Zijlstra 	 *
1399391e43daSPeter Zijlstra 	 * We want to avoid overloading runqueues. If the woken
1400391e43daSPeter Zijlstra 	 * task is a higher priority, then it will stay on this CPU
1401391e43daSPeter Zijlstra 	 * and the lower prio task should be moved to another CPU.
1402391e43daSPeter Zijlstra 	 * Even though this will probably make the lower prio task
1403391e43daSPeter Zijlstra 	 * lose its cache, we do not want to bounce a higher task
1404391e43daSPeter Zijlstra 	 * around just because it gave up its CPU, perhaps for a
1405391e43daSPeter Zijlstra 	 * lock?
1406391e43daSPeter Zijlstra 	 *
1407391e43daSPeter Zijlstra 	 * For equal prio tasks, we just let the scheduler sort it out.
1408391e43daSPeter Zijlstra 	 *
1409391e43daSPeter Zijlstra 	 * Otherwise, just let it ride on the affined RQ and the
1410391e43daSPeter Zijlstra 	 * post-schedule router will push the preempted task away
1411391e43daSPeter Zijlstra 	 *
1412391e43daSPeter Zijlstra 	 * This test is optimistic, if we get it wrong the load-balancer
1413391e43daSPeter Zijlstra 	 * will have to sort it out.
1414391e43daSPeter Zijlstra 	 */
1415391e43daSPeter Zijlstra 	if (curr && unlikely(rt_task(curr)) &&
14164b53a341SIngo Molnar 	    (curr->nr_cpus_allowed < 2 ||
14176bfa687cSShawn Bohrer 	     curr->prio <= p->prio)) {
1418391e43daSPeter Zijlstra 		int target = find_lowest_rq(p);
1419391e43daSPeter Zijlstra 
142080e3d87bSTim Chen 		/*
142180e3d87bSTim Chen 		 * Don't bother moving it if the destination CPU is
142280e3d87bSTim Chen 		 * not running a lower priority task.
142380e3d87bSTim Chen 		 */
142480e3d87bSTim Chen 		if (target != -1 &&
142580e3d87bSTim Chen 		    p->prio < cpu_rq(target)->rt.highest_prio.curr)
1426391e43daSPeter Zijlstra 			cpu = target;
1427391e43daSPeter Zijlstra 	}
1428391e43daSPeter Zijlstra 	rcu_read_unlock();
1429391e43daSPeter Zijlstra 
1430391e43daSPeter Zijlstra out:
1431391e43daSPeter Zijlstra 	return cpu;
1432391e43daSPeter Zijlstra }
1433391e43daSPeter Zijlstra 
1434391e43daSPeter Zijlstra static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1435391e43daSPeter Zijlstra {
1436308a623aSWanpeng Li 	/*
1437308a623aSWanpeng Li 	 * Current can't be migrated, useless to reschedule,
1438308a623aSWanpeng Li 	 * let's hope p can move out.
1439308a623aSWanpeng Li 	 */
14404b53a341SIngo Molnar 	if (rq->curr->nr_cpus_allowed == 1 ||
1441308a623aSWanpeng Li 	    !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1442391e43daSPeter Zijlstra 		return;
1443391e43daSPeter Zijlstra 
1444308a623aSWanpeng Li 	/*
1445308a623aSWanpeng Li 	 * p is migratable, so let's not schedule it and
1446308a623aSWanpeng Li 	 * see if it is pushed or pulled somewhere else.
1447308a623aSWanpeng Li 	 */
14484b53a341SIngo Molnar 	if (p->nr_cpus_allowed != 1
1449391e43daSPeter Zijlstra 	    && cpupri_find(&rq->rd->cpupri, p, NULL))
1450391e43daSPeter Zijlstra 		return;
1451391e43daSPeter Zijlstra 
1452391e43daSPeter Zijlstra 	/*
1453391e43daSPeter Zijlstra 	 * There appears to be other cpus that can accept
1454391e43daSPeter Zijlstra 	 * current and none to run 'p', so lets reschedule
1455391e43daSPeter Zijlstra 	 * to try and push current away:
1456391e43daSPeter Zijlstra 	 */
1457391e43daSPeter Zijlstra 	requeue_task_rt(rq, p, 1);
14588875125eSKirill Tkhai 	resched_curr(rq);
1459391e43daSPeter Zijlstra }
1460391e43daSPeter Zijlstra 
1461391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
1462391e43daSPeter Zijlstra 
1463391e43daSPeter Zijlstra /*
1464391e43daSPeter Zijlstra  * Preempt the current task with a newly woken task if needed:
1465391e43daSPeter Zijlstra  */
1466391e43daSPeter Zijlstra static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
1467391e43daSPeter Zijlstra {
1468391e43daSPeter Zijlstra 	if (p->prio < rq->curr->prio) {
14698875125eSKirill Tkhai 		resched_curr(rq);
1470391e43daSPeter Zijlstra 		return;
1471391e43daSPeter Zijlstra 	}
1472391e43daSPeter Zijlstra 
1473391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1474391e43daSPeter Zijlstra 	/*
1475391e43daSPeter Zijlstra 	 * If:
1476391e43daSPeter Zijlstra 	 *
1477391e43daSPeter Zijlstra 	 * - the newly woken task is of equal priority to the current task
1478391e43daSPeter Zijlstra 	 * - the newly woken task is non-migratable while current is migratable
1479391e43daSPeter Zijlstra 	 * - current will be preempted on the next reschedule
1480391e43daSPeter Zijlstra 	 *
1481391e43daSPeter Zijlstra 	 * we should check to see if current can readily move to a different
1482391e43daSPeter Zijlstra 	 * cpu.  If so, we will reschedule to allow the push logic to try
1483391e43daSPeter Zijlstra 	 * to move current somewhere else, making room for our non-migratable
1484391e43daSPeter Zijlstra 	 * task.
1485391e43daSPeter Zijlstra 	 */
1486391e43daSPeter Zijlstra 	if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
1487391e43daSPeter Zijlstra 		check_preempt_equal_prio(rq, p);
1488391e43daSPeter Zijlstra #endif
1489391e43daSPeter Zijlstra }
1490391e43daSPeter Zijlstra 
1491391e43daSPeter Zijlstra static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq,
1492391e43daSPeter Zijlstra 						   struct rt_rq *rt_rq)
1493391e43daSPeter Zijlstra {
1494391e43daSPeter Zijlstra 	struct rt_prio_array *array = &rt_rq->active;
1495391e43daSPeter Zijlstra 	struct sched_rt_entity *next = NULL;
1496391e43daSPeter Zijlstra 	struct list_head *queue;
1497391e43daSPeter Zijlstra 	int idx;
1498391e43daSPeter Zijlstra 
1499391e43daSPeter Zijlstra 	idx = sched_find_first_bit(array->bitmap);
1500391e43daSPeter Zijlstra 	BUG_ON(idx >= MAX_RT_PRIO);
1501391e43daSPeter Zijlstra 
1502391e43daSPeter Zijlstra 	queue = array->queue + idx;
1503391e43daSPeter Zijlstra 	next = list_entry(queue->next, struct sched_rt_entity, run_list);
1504391e43daSPeter Zijlstra 
1505391e43daSPeter Zijlstra 	return next;
1506391e43daSPeter Zijlstra }
1507391e43daSPeter Zijlstra 
1508391e43daSPeter Zijlstra static struct task_struct *_pick_next_task_rt(struct rq *rq)
1509391e43daSPeter Zijlstra {
1510391e43daSPeter Zijlstra 	struct sched_rt_entity *rt_se;
1511391e43daSPeter Zijlstra 	struct task_struct *p;
1512606dba2eSPeter Zijlstra 	struct rt_rq *rt_rq  = &rq->rt;
1513391e43daSPeter Zijlstra 
1514391e43daSPeter Zijlstra 	do {
1515391e43daSPeter Zijlstra 		rt_se = pick_next_rt_entity(rq, rt_rq);
1516391e43daSPeter Zijlstra 		BUG_ON(!rt_se);
1517391e43daSPeter Zijlstra 		rt_rq = group_rt_rq(rt_se);
1518391e43daSPeter Zijlstra 	} while (rt_rq);
1519391e43daSPeter Zijlstra 
1520391e43daSPeter Zijlstra 	p = rt_task_of(rt_se);
152178becc27SFrederic Weisbecker 	p->se.exec_start = rq_clock_task(rq);
1522391e43daSPeter Zijlstra 
1523391e43daSPeter Zijlstra 	return p;
1524391e43daSPeter Zijlstra }
1525391e43daSPeter Zijlstra 
1526606dba2eSPeter Zijlstra static struct task_struct *
1527d8ac8971SMatt Fleming pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
1528391e43daSPeter Zijlstra {
1529606dba2eSPeter Zijlstra 	struct task_struct *p;
1530606dba2eSPeter Zijlstra 	struct rt_rq *rt_rq = &rq->rt;
1531606dba2eSPeter Zijlstra 
153237e117c0SPeter Zijlstra 	if (need_pull_rt_task(rq, prev)) {
1533cbce1a68SPeter Zijlstra 		/*
1534cbce1a68SPeter Zijlstra 		 * This is OK, because current is on_cpu, which avoids it being
1535cbce1a68SPeter Zijlstra 		 * picked for load-balance and preemption/IRQs are still
1536cbce1a68SPeter Zijlstra 		 * disabled avoiding further scheduler activity on it and we're
1537cbce1a68SPeter Zijlstra 		 * being very careful to re-start the picking loop.
1538cbce1a68SPeter Zijlstra 		 */
1539d8ac8971SMatt Fleming 		rq_unpin_lock(rq, rf);
154038033c37SPeter Zijlstra 		pull_rt_task(rq);
1541d8ac8971SMatt Fleming 		rq_repin_lock(rq, rf);
154237e117c0SPeter Zijlstra 		/*
154337e117c0SPeter Zijlstra 		 * pull_rt_task() can drop (and re-acquire) rq->lock; this
1544a1d9a323SKirill Tkhai 		 * means a dl or stop task can slip in, in which case we need
1545a1d9a323SKirill Tkhai 		 * to re-start task selection.
154637e117c0SPeter Zijlstra 		 */
1547da0c1e65SKirill Tkhai 		if (unlikely((rq->stop && task_on_rq_queued(rq->stop)) ||
1548a1d9a323SKirill Tkhai 			     rq->dl.dl_nr_running))
154937e117c0SPeter Zijlstra 			return RETRY_TASK;
155037e117c0SPeter Zijlstra 	}
155138033c37SPeter Zijlstra 
1552734ff2a7SKirill Tkhai 	/*
1553734ff2a7SKirill Tkhai 	 * We may dequeue prev's rt_rq in put_prev_task().
1554734ff2a7SKirill Tkhai 	 * So, we update time before rt_nr_running check.
1555734ff2a7SKirill Tkhai 	 */
1556734ff2a7SKirill Tkhai 	if (prev->sched_class == &rt_sched_class)
1557734ff2a7SKirill Tkhai 		update_curr_rt(rq);
1558734ff2a7SKirill Tkhai 
1559f4ebcbc0SKirill Tkhai 	if (!rt_rq->rt_queued)
1560606dba2eSPeter Zijlstra 		return NULL;
1561606dba2eSPeter Zijlstra 
15623f1d2a31SPeter Zijlstra 	put_prev_task(rq, prev);
1563606dba2eSPeter Zijlstra 
1564606dba2eSPeter Zijlstra 	p = _pick_next_task_rt(rq);
1565391e43daSPeter Zijlstra 
1566391e43daSPeter Zijlstra 	/* The running task is never eligible for pushing */
1567391e43daSPeter Zijlstra 	dequeue_pushable_task(rq, p);
1568391e43daSPeter Zijlstra 
1569e3fca9e7SPeter Zijlstra 	queue_push_tasks(rq);
1570391e43daSPeter Zijlstra 
1571391e43daSPeter Zijlstra 	return p;
1572391e43daSPeter Zijlstra }
1573391e43daSPeter Zijlstra 
1574391e43daSPeter Zijlstra static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
1575391e43daSPeter Zijlstra {
1576391e43daSPeter Zijlstra 	update_curr_rt(rq);
1577391e43daSPeter Zijlstra 
1578391e43daSPeter Zijlstra 	/*
1579391e43daSPeter Zijlstra 	 * The previous task needs to be made eligible for pushing
1580391e43daSPeter Zijlstra 	 * if it is still active
1581391e43daSPeter Zijlstra 	 */
15824b53a341SIngo Molnar 	if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
1583391e43daSPeter Zijlstra 		enqueue_pushable_task(rq, p);
1584391e43daSPeter Zijlstra }
1585391e43daSPeter Zijlstra 
1586391e43daSPeter Zijlstra #ifdef CONFIG_SMP
1587391e43daSPeter Zijlstra 
1588391e43daSPeter Zijlstra /* Only try algorithms three times */
1589391e43daSPeter Zijlstra #define RT_MAX_TRIES 3
1590391e43daSPeter Zijlstra 
1591391e43daSPeter Zijlstra static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1592391e43daSPeter Zijlstra {
1593391e43daSPeter Zijlstra 	if (!task_running(rq, p) &&
15940c98d344SIngo Molnar 	    cpumask_test_cpu(cpu, &p->cpus_allowed))
1595391e43daSPeter Zijlstra 		return 1;
1596391e43daSPeter Zijlstra 	return 0;
1597391e43daSPeter Zijlstra }
1598391e43daSPeter Zijlstra 
1599e23ee747SKirill Tkhai /*
1600e23ee747SKirill Tkhai  * Return the highest pushable rq's task, which is suitable to be executed
1601e23ee747SKirill Tkhai  * on the cpu, NULL otherwise
1602e23ee747SKirill Tkhai  */
1603e23ee747SKirill Tkhai static struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
1604391e43daSPeter Zijlstra {
1605e23ee747SKirill Tkhai 	struct plist_head *head = &rq->rt.pushable_tasks;
1606391e43daSPeter Zijlstra 	struct task_struct *p;
1607391e43daSPeter Zijlstra 
1608e23ee747SKirill Tkhai 	if (!has_pushable_tasks(rq))
1609e23ee747SKirill Tkhai 		return NULL;
1610391e43daSPeter Zijlstra 
1611e23ee747SKirill Tkhai 	plist_for_each_entry(p, head, pushable_tasks) {
1612e23ee747SKirill Tkhai 		if (pick_rt_task(rq, p, cpu))
1613e23ee747SKirill Tkhai 			return p;
1614391e43daSPeter Zijlstra 	}
1615391e43daSPeter Zijlstra 
1616e23ee747SKirill Tkhai 	return NULL;
1617391e43daSPeter Zijlstra }
1618391e43daSPeter Zijlstra 
1619391e43daSPeter Zijlstra static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
1620391e43daSPeter Zijlstra 
1621391e43daSPeter Zijlstra static int find_lowest_rq(struct task_struct *task)
1622391e43daSPeter Zijlstra {
1623391e43daSPeter Zijlstra 	struct sched_domain *sd;
16244ba29684SChristoph Lameter 	struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
1625391e43daSPeter Zijlstra 	int this_cpu = smp_processor_id();
1626391e43daSPeter Zijlstra 	int cpu      = task_cpu(task);
1627391e43daSPeter Zijlstra 
1628391e43daSPeter Zijlstra 	/* Make sure the mask is initialized first */
1629391e43daSPeter Zijlstra 	if (unlikely(!lowest_mask))
1630391e43daSPeter Zijlstra 		return -1;
1631391e43daSPeter Zijlstra 
16324b53a341SIngo Molnar 	if (task->nr_cpus_allowed == 1)
1633391e43daSPeter Zijlstra 		return -1; /* No other targets possible */
1634391e43daSPeter Zijlstra 
1635391e43daSPeter Zijlstra 	if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
1636391e43daSPeter Zijlstra 		return -1; /* No targets found */
1637391e43daSPeter Zijlstra 
1638391e43daSPeter Zijlstra 	/*
1639391e43daSPeter Zijlstra 	 * At this point we have built a mask of cpus representing the
1640391e43daSPeter Zijlstra 	 * lowest priority tasks in the system.  Now we want to elect
1641391e43daSPeter Zijlstra 	 * the best one based on our affinity and topology.
1642391e43daSPeter Zijlstra 	 *
1643391e43daSPeter Zijlstra 	 * We prioritize the last cpu that the task executed on since
1644391e43daSPeter Zijlstra 	 * it is most likely cache-hot in that location.
1645391e43daSPeter Zijlstra 	 */
1646391e43daSPeter Zijlstra 	if (cpumask_test_cpu(cpu, lowest_mask))
1647391e43daSPeter Zijlstra 		return cpu;
1648391e43daSPeter Zijlstra 
1649391e43daSPeter Zijlstra 	/*
1650391e43daSPeter Zijlstra 	 * Otherwise, we consult the sched_domains span maps to figure
1651391e43daSPeter Zijlstra 	 * out which cpu is logically closest to our hot cache data.
1652391e43daSPeter Zijlstra 	 */
1653391e43daSPeter Zijlstra 	if (!cpumask_test_cpu(this_cpu, lowest_mask))
1654391e43daSPeter Zijlstra 		this_cpu = -1; /* Skip this_cpu opt if not among lowest */
1655391e43daSPeter Zijlstra 
1656391e43daSPeter Zijlstra 	rcu_read_lock();
1657391e43daSPeter Zijlstra 	for_each_domain(cpu, sd) {
1658391e43daSPeter Zijlstra 		if (sd->flags & SD_WAKE_AFFINE) {
1659391e43daSPeter Zijlstra 			int best_cpu;
1660391e43daSPeter Zijlstra 
1661391e43daSPeter Zijlstra 			/*
1662391e43daSPeter Zijlstra 			 * "this_cpu" is cheaper to preempt than a
1663391e43daSPeter Zijlstra 			 * remote processor.
1664391e43daSPeter Zijlstra 			 */
1665391e43daSPeter Zijlstra 			if (this_cpu != -1 &&
1666391e43daSPeter Zijlstra 			    cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1667391e43daSPeter Zijlstra 				rcu_read_unlock();
1668391e43daSPeter Zijlstra 				return this_cpu;
1669391e43daSPeter Zijlstra 			}
1670391e43daSPeter Zijlstra 
1671391e43daSPeter Zijlstra 			best_cpu = cpumask_first_and(lowest_mask,
1672391e43daSPeter Zijlstra 						     sched_domain_span(sd));
1673391e43daSPeter Zijlstra 			if (best_cpu < nr_cpu_ids) {
1674391e43daSPeter Zijlstra 				rcu_read_unlock();
1675391e43daSPeter Zijlstra 				return best_cpu;
1676391e43daSPeter Zijlstra 			}
1677391e43daSPeter Zijlstra 		}
1678391e43daSPeter Zijlstra 	}
1679391e43daSPeter Zijlstra 	rcu_read_unlock();
1680391e43daSPeter Zijlstra 
1681391e43daSPeter Zijlstra 	/*
1682391e43daSPeter Zijlstra 	 * And finally, if there were no matches within the domains
1683391e43daSPeter Zijlstra 	 * just give the caller *something* to work with from the compatible
1684391e43daSPeter Zijlstra 	 * locations.
1685391e43daSPeter Zijlstra 	 */
1686391e43daSPeter Zijlstra 	if (this_cpu != -1)
1687391e43daSPeter Zijlstra 		return this_cpu;
1688391e43daSPeter Zijlstra 
1689391e43daSPeter Zijlstra 	cpu = cpumask_any(lowest_mask);
1690391e43daSPeter Zijlstra 	if (cpu < nr_cpu_ids)
1691391e43daSPeter Zijlstra 		return cpu;
1692391e43daSPeter Zijlstra 	return -1;
1693391e43daSPeter Zijlstra }
1694391e43daSPeter Zijlstra 
1695391e43daSPeter Zijlstra /* Will lock the rq it finds */
1696391e43daSPeter Zijlstra static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
1697391e43daSPeter Zijlstra {
1698391e43daSPeter Zijlstra 	struct rq *lowest_rq = NULL;
1699391e43daSPeter Zijlstra 	int tries;
1700391e43daSPeter Zijlstra 	int cpu;
1701391e43daSPeter Zijlstra 
1702391e43daSPeter Zijlstra 	for (tries = 0; tries < RT_MAX_TRIES; tries++) {
1703391e43daSPeter Zijlstra 		cpu = find_lowest_rq(task);
1704391e43daSPeter Zijlstra 
1705391e43daSPeter Zijlstra 		if ((cpu == -1) || (cpu == rq->cpu))
1706391e43daSPeter Zijlstra 			break;
1707391e43daSPeter Zijlstra 
1708391e43daSPeter Zijlstra 		lowest_rq = cpu_rq(cpu);
1709391e43daSPeter Zijlstra 
171080e3d87bSTim Chen 		if (lowest_rq->rt.highest_prio.curr <= task->prio) {
171180e3d87bSTim Chen 			/*
171280e3d87bSTim Chen 			 * Target rq has tasks of equal or higher priority,
171380e3d87bSTim Chen 			 * retrying does not release any lock and is unlikely
171480e3d87bSTim Chen 			 * to yield a different result.
171580e3d87bSTim Chen 			 */
171680e3d87bSTim Chen 			lowest_rq = NULL;
171780e3d87bSTim Chen 			break;
171880e3d87bSTim Chen 		}
171980e3d87bSTim Chen 
1720391e43daSPeter Zijlstra 		/* if the prio of this runqueue changed, try again */
1721391e43daSPeter Zijlstra 		if (double_lock_balance(rq, lowest_rq)) {
1722391e43daSPeter Zijlstra 			/*
1723391e43daSPeter Zijlstra 			 * We had to unlock the run queue. In
1724391e43daSPeter Zijlstra 			 * the mean time, task could have
1725391e43daSPeter Zijlstra 			 * migrated already or had its affinity changed.
1726391e43daSPeter Zijlstra 			 * Also make sure that it wasn't scheduled on its rq.
1727391e43daSPeter Zijlstra 			 */
1728391e43daSPeter Zijlstra 			if (unlikely(task_rq(task) != rq ||
17290c98d344SIngo Molnar 				     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_allowed) ||
1730391e43daSPeter Zijlstra 				     task_running(rq, task) ||
173113b5ab02SXunlei Pang 				     !rt_task(task) ||
1732da0c1e65SKirill Tkhai 				     !task_on_rq_queued(task))) {
1733391e43daSPeter Zijlstra 
17347f1b4393SPeter Zijlstra 				double_unlock_balance(rq, lowest_rq);
1735391e43daSPeter Zijlstra 				lowest_rq = NULL;
1736391e43daSPeter Zijlstra 				break;
1737391e43daSPeter Zijlstra 			}
1738391e43daSPeter Zijlstra 		}
1739391e43daSPeter Zijlstra 
1740391e43daSPeter Zijlstra 		/* If this rq is still suitable use it. */
1741391e43daSPeter Zijlstra 		if (lowest_rq->rt.highest_prio.curr > task->prio)
1742391e43daSPeter Zijlstra 			break;
1743391e43daSPeter Zijlstra 
1744391e43daSPeter Zijlstra 		/* try again */
1745391e43daSPeter Zijlstra 		double_unlock_balance(rq, lowest_rq);
1746391e43daSPeter Zijlstra 		lowest_rq = NULL;
1747391e43daSPeter Zijlstra 	}
1748391e43daSPeter Zijlstra 
1749391e43daSPeter Zijlstra 	return lowest_rq;
1750391e43daSPeter Zijlstra }
1751391e43daSPeter Zijlstra 
1752391e43daSPeter Zijlstra static struct task_struct *pick_next_pushable_task(struct rq *rq)
1753391e43daSPeter Zijlstra {
1754391e43daSPeter Zijlstra 	struct task_struct *p;
1755391e43daSPeter Zijlstra 
1756391e43daSPeter Zijlstra 	if (!has_pushable_tasks(rq))
1757391e43daSPeter Zijlstra 		return NULL;
1758391e43daSPeter Zijlstra 
1759391e43daSPeter Zijlstra 	p = plist_first_entry(&rq->rt.pushable_tasks,
1760391e43daSPeter Zijlstra 			      struct task_struct, pushable_tasks);
1761391e43daSPeter Zijlstra 
1762391e43daSPeter Zijlstra 	BUG_ON(rq->cpu != task_cpu(p));
1763391e43daSPeter Zijlstra 	BUG_ON(task_current(rq, p));
17644b53a341SIngo Molnar 	BUG_ON(p->nr_cpus_allowed <= 1);
1765391e43daSPeter Zijlstra 
1766da0c1e65SKirill Tkhai 	BUG_ON(!task_on_rq_queued(p));
1767391e43daSPeter Zijlstra 	BUG_ON(!rt_task(p));
1768391e43daSPeter Zijlstra 
1769391e43daSPeter Zijlstra 	return p;
1770391e43daSPeter Zijlstra }
1771391e43daSPeter Zijlstra 
1772391e43daSPeter Zijlstra /*
1773391e43daSPeter Zijlstra  * If the current CPU has more than one RT task, see if the non
1774391e43daSPeter Zijlstra  * running task can migrate over to a CPU that is running a task
1775391e43daSPeter Zijlstra  * of lesser priority.
1776391e43daSPeter Zijlstra  */
1777391e43daSPeter Zijlstra static int push_rt_task(struct rq *rq)
1778391e43daSPeter Zijlstra {
1779391e43daSPeter Zijlstra 	struct task_struct *next_task;
1780391e43daSPeter Zijlstra 	struct rq *lowest_rq;
1781391e43daSPeter Zijlstra 	int ret = 0;
1782391e43daSPeter Zijlstra 
1783391e43daSPeter Zijlstra 	if (!rq->rt.overloaded)
1784391e43daSPeter Zijlstra 		return 0;
1785391e43daSPeter Zijlstra 
1786391e43daSPeter Zijlstra 	next_task = pick_next_pushable_task(rq);
1787391e43daSPeter Zijlstra 	if (!next_task)
1788391e43daSPeter Zijlstra 		return 0;
1789391e43daSPeter Zijlstra 
1790391e43daSPeter Zijlstra retry:
1791391e43daSPeter Zijlstra 	if (unlikely(next_task == rq->curr)) {
1792391e43daSPeter Zijlstra 		WARN_ON(1);
1793391e43daSPeter Zijlstra 		return 0;
1794391e43daSPeter Zijlstra 	}
1795391e43daSPeter Zijlstra 
1796391e43daSPeter Zijlstra 	/*
1797391e43daSPeter Zijlstra 	 * It's possible that the next_task slipped in of
1798391e43daSPeter Zijlstra 	 * higher priority than current. If that's the case
1799391e43daSPeter Zijlstra 	 * just reschedule current.
1800391e43daSPeter Zijlstra 	 */
1801391e43daSPeter Zijlstra 	if (unlikely(next_task->prio < rq->curr->prio)) {
18028875125eSKirill Tkhai 		resched_curr(rq);
1803391e43daSPeter Zijlstra 		return 0;
1804391e43daSPeter Zijlstra 	}
1805391e43daSPeter Zijlstra 
1806391e43daSPeter Zijlstra 	/* We might release rq lock */
1807391e43daSPeter Zijlstra 	get_task_struct(next_task);
1808391e43daSPeter Zijlstra 
1809391e43daSPeter Zijlstra 	/* find_lock_lowest_rq locks the rq if found */
1810391e43daSPeter Zijlstra 	lowest_rq = find_lock_lowest_rq(next_task, rq);
1811391e43daSPeter Zijlstra 	if (!lowest_rq) {
1812391e43daSPeter Zijlstra 		struct task_struct *task;
1813391e43daSPeter Zijlstra 		/*
1814391e43daSPeter Zijlstra 		 * find_lock_lowest_rq releases rq->lock
1815391e43daSPeter Zijlstra 		 * so it is possible that next_task has migrated.
1816391e43daSPeter Zijlstra 		 *
1817391e43daSPeter Zijlstra 		 * We need to make sure that the task is still on the same
1818391e43daSPeter Zijlstra 		 * run-queue and is also still the next task eligible for
1819391e43daSPeter Zijlstra 		 * pushing.
1820391e43daSPeter Zijlstra 		 */
1821391e43daSPeter Zijlstra 		task = pick_next_pushable_task(rq);
1822de16b91eSByungchul Park 		if (task == next_task) {
1823391e43daSPeter Zijlstra 			/*
1824391e43daSPeter Zijlstra 			 * The task hasn't migrated, and is still the next
1825391e43daSPeter Zijlstra 			 * eligible task, but we failed to find a run-queue
1826391e43daSPeter Zijlstra 			 * to push it to.  Do not retry in this case, since
1827391e43daSPeter Zijlstra 			 * other cpus will pull from us when ready.
1828391e43daSPeter Zijlstra 			 */
1829391e43daSPeter Zijlstra 			goto out;
1830391e43daSPeter Zijlstra 		}
1831391e43daSPeter Zijlstra 
1832391e43daSPeter Zijlstra 		if (!task)
1833391e43daSPeter Zijlstra 			/* No more tasks, just exit */
1834391e43daSPeter Zijlstra 			goto out;
1835391e43daSPeter Zijlstra 
1836391e43daSPeter Zijlstra 		/*
1837391e43daSPeter Zijlstra 		 * Something has shifted, try again.
1838391e43daSPeter Zijlstra 		 */
1839391e43daSPeter Zijlstra 		put_task_struct(next_task);
1840391e43daSPeter Zijlstra 		next_task = task;
1841391e43daSPeter Zijlstra 		goto retry;
1842391e43daSPeter Zijlstra 	}
1843391e43daSPeter Zijlstra 
1844391e43daSPeter Zijlstra 	deactivate_task(rq, next_task, 0);
1845391e43daSPeter Zijlstra 	set_task_cpu(next_task, lowest_rq->cpu);
1846391e43daSPeter Zijlstra 	activate_task(lowest_rq, next_task, 0);
1847391e43daSPeter Zijlstra 	ret = 1;
1848391e43daSPeter Zijlstra 
18498875125eSKirill Tkhai 	resched_curr(lowest_rq);
1850391e43daSPeter Zijlstra 
1851391e43daSPeter Zijlstra 	double_unlock_balance(rq, lowest_rq);
1852391e43daSPeter Zijlstra 
1853391e43daSPeter Zijlstra out:
1854391e43daSPeter Zijlstra 	put_task_struct(next_task);
1855391e43daSPeter Zijlstra 
1856391e43daSPeter Zijlstra 	return ret;
1857391e43daSPeter Zijlstra }
1858391e43daSPeter Zijlstra 
1859391e43daSPeter Zijlstra static void push_rt_tasks(struct rq *rq)
1860391e43daSPeter Zijlstra {
1861391e43daSPeter Zijlstra 	/* push_rt_task will return true if it moved an RT */
1862391e43daSPeter Zijlstra 	while (push_rt_task(rq))
1863391e43daSPeter Zijlstra 		;
1864391e43daSPeter Zijlstra }
1865391e43daSPeter Zijlstra 
1866b6366f04SSteven Rostedt #ifdef HAVE_RT_PUSH_IPI
1867b6366f04SSteven Rostedt 
18683e777f99SSteven Rostedt (VMware) /*
18693e777f99SSteven Rostedt (VMware)  * When a high priority task schedules out from a CPU and a lower priority
18703e777f99SSteven Rostedt (VMware)  * task is scheduled in, a check is made to see if there's any RT tasks
18713e777f99SSteven Rostedt (VMware)  * on other CPUs that are waiting to run because a higher priority RT task
18723e777f99SSteven Rostedt (VMware)  * is currently running on its CPU. In this case, the CPU with multiple RT
18733e777f99SSteven Rostedt (VMware)  * tasks queued on it (overloaded) needs to be notified that a CPU has opened
18743e777f99SSteven Rostedt (VMware)  * up that may be able to run one of its non-running queued RT tasks.
18753e777f99SSteven Rostedt (VMware)  *
1876*4bdced5cSSteven Rostedt (Red Hat)  * All CPUs with overloaded RT tasks need to be notified as there is currently
1877*4bdced5cSSteven Rostedt (Red Hat)  * no way to know which of these CPUs have the highest priority task waiting
1878*4bdced5cSSteven Rostedt (Red Hat)  * to run. Instead of trying to take a spinlock on each of these CPUs,
1879*4bdced5cSSteven Rostedt (Red Hat)  * which has shown to cause large latency when done on machines with many
1880*4bdced5cSSteven Rostedt (Red Hat)  * CPUs, sending an IPI to the CPUs to have them push off the overloaded
1881*4bdced5cSSteven Rostedt (Red Hat)  * RT tasks waiting to run.
18823e777f99SSteven Rostedt (VMware)  *
1883*4bdced5cSSteven Rostedt (Red Hat)  * Just sending an IPI to each of the CPUs is also an issue, as on large
1884*4bdced5cSSteven Rostedt (Red Hat)  * count CPU machines, this can cause an IPI storm on a CPU, especially
1885*4bdced5cSSteven Rostedt (Red Hat)  * if its the only CPU with multiple RT tasks queued, and a large number
1886*4bdced5cSSteven Rostedt (Red Hat)  * of CPUs scheduling a lower priority task at the same time.
18873e777f99SSteven Rostedt (VMware)  *
1888*4bdced5cSSteven Rostedt (Red Hat)  * Each root domain has its own irq work function that can iterate over
1889*4bdced5cSSteven Rostedt (Red Hat)  * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
1890*4bdced5cSSteven Rostedt (Red Hat)  * tassk must be checked if there's one or many CPUs that are lowering
1891*4bdced5cSSteven Rostedt (Red Hat)  * their priority, there's a single irq work iterator that will try to
1892*4bdced5cSSteven Rostedt (Red Hat)  * push off RT tasks that are waiting to run.
18933e777f99SSteven Rostedt (VMware)  *
1894*4bdced5cSSteven Rostedt (Red Hat)  * When a CPU schedules a lower priority task, it will kick off the
1895*4bdced5cSSteven Rostedt (Red Hat)  * irq work iterator that will jump to each CPU with overloaded RT tasks.
1896*4bdced5cSSteven Rostedt (Red Hat)  * As it only takes the first CPU that schedules a lower priority task
1897*4bdced5cSSteven Rostedt (Red Hat)  * to start the process, the rto_start variable is incremented and if
1898*4bdced5cSSteven Rostedt (Red Hat)  * the atomic result is one, then that CPU will try to take the rto_lock.
1899*4bdced5cSSteven Rostedt (Red Hat)  * This prevents high contention on the lock as the process handles all
1900*4bdced5cSSteven Rostedt (Red Hat)  * CPUs scheduling lower priority tasks.
19013e777f99SSteven Rostedt (VMware)  *
1902*4bdced5cSSteven Rostedt (Red Hat)  * All CPUs that are scheduling a lower priority task will increment the
1903*4bdced5cSSteven Rostedt (Red Hat)  * rt_loop_next variable. This will make sure that the irq work iterator
1904*4bdced5cSSteven Rostedt (Red Hat)  * checks all RT overloaded CPUs whenever a CPU schedules a new lower
1905*4bdced5cSSteven Rostedt (Red Hat)  * priority task, even if the iterator is in the middle of a scan. Incrementing
1906*4bdced5cSSteven Rostedt (Red Hat)  * the rt_loop_next will cause the iterator to perform another scan.
19073e777f99SSteven Rostedt (VMware)  *
19083e777f99SSteven Rostedt (VMware)  */
1909*4bdced5cSSteven Rostedt (Red Hat) static int rto_next_cpu(struct rq *rq)
1910b6366f04SSteven Rostedt {
1911*4bdced5cSSteven Rostedt (Red Hat) 	struct root_domain *rd = rq->rd;
1912*4bdced5cSSteven Rostedt (Red Hat) 	int next;
1913b6366f04SSteven Rostedt 	int cpu;
1914b6366f04SSteven Rostedt 
1915b6366f04SSteven Rostedt 	/*
1916*4bdced5cSSteven Rostedt (Red Hat) 	 * When starting the IPI RT pushing, the rto_cpu is set to -1,
1917*4bdced5cSSteven Rostedt (Red Hat) 	 * rt_next_cpu() will simply return the first CPU found in
1918*4bdced5cSSteven Rostedt (Red Hat) 	 * the rto_mask.
1919*4bdced5cSSteven Rostedt (Red Hat) 	 *
1920*4bdced5cSSteven Rostedt (Red Hat) 	 * If rto_next_cpu() is called with rto_cpu is a valid cpu, it
1921*4bdced5cSSteven Rostedt (Red Hat) 	 * will return the next CPU found in the rto_mask.
1922*4bdced5cSSteven Rostedt (Red Hat) 	 *
1923*4bdced5cSSteven Rostedt (Red Hat) 	 * If there are no more CPUs left in the rto_mask, then a check is made
1924*4bdced5cSSteven Rostedt (Red Hat) 	 * against rto_loop and rto_loop_next. rto_loop is only updated with
1925*4bdced5cSSteven Rostedt (Red Hat) 	 * the rto_lock held, but any CPU may increment the rto_loop_next
1926*4bdced5cSSteven Rostedt (Red Hat) 	 * without any locking.
1927b6366f04SSteven Rostedt 	 */
1928*4bdced5cSSteven Rostedt (Red Hat) 	for (;;) {
1929*4bdced5cSSteven Rostedt (Red Hat) 
1930*4bdced5cSSteven Rostedt (Red Hat) 		/* When rto_cpu is -1 this acts like cpumask_first() */
1931*4bdced5cSSteven Rostedt (Red Hat) 		cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
1932*4bdced5cSSteven Rostedt (Red Hat) 
1933*4bdced5cSSteven Rostedt (Red Hat) 		rd->rto_cpu = cpu;
1934*4bdced5cSSteven Rostedt (Red Hat) 
1935*4bdced5cSSteven Rostedt (Red Hat) 		if (cpu < nr_cpu_ids)
1936*4bdced5cSSteven Rostedt (Red Hat) 			return cpu;
1937*4bdced5cSSteven Rostedt (Red Hat) 
1938*4bdced5cSSteven Rostedt (Red Hat) 		rd->rto_cpu = -1;
1939*4bdced5cSSteven Rostedt (Red Hat) 
1940*4bdced5cSSteven Rostedt (Red Hat) 		/*
1941*4bdced5cSSteven Rostedt (Red Hat) 		 * ACQUIRE ensures we see the @rto_mask changes
1942*4bdced5cSSteven Rostedt (Red Hat) 		 * made prior to the @next value observed.
1943*4bdced5cSSteven Rostedt (Red Hat) 		 *
1944*4bdced5cSSteven Rostedt (Red Hat) 		 * Matches WMB in rt_set_overload().
1945*4bdced5cSSteven Rostedt (Red Hat) 		 */
1946*4bdced5cSSteven Rostedt (Red Hat) 		next = atomic_read_acquire(&rd->rto_loop_next);
1947*4bdced5cSSteven Rostedt (Red Hat) 
1948*4bdced5cSSteven Rostedt (Red Hat) 		if (rd->rto_loop == next)
1949*4bdced5cSSteven Rostedt (Red Hat) 			break;
1950*4bdced5cSSteven Rostedt (Red Hat) 
1951*4bdced5cSSteven Rostedt (Red Hat) 		rd->rto_loop = next;
1952b6366f04SSteven Rostedt 	}
1953b6366f04SSteven Rostedt 
1954*4bdced5cSSteven Rostedt (Red Hat) 	return -1;
1955*4bdced5cSSteven Rostedt (Red Hat) }
1956b6366f04SSteven Rostedt 
1957*4bdced5cSSteven Rostedt (Red Hat) static inline bool rto_start_trylock(atomic_t *v)
1958*4bdced5cSSteven Rostedt (Red Hat) {
1959*4bdced5cSSteven Rostedt (Red Hat) 	return !atomic_cmpxchg_acquire(v, 0, 1);
1960*4bdced5cSSteven Rostedt (Red Hat) }
1961*4bdced5cSSteven Rostedt (Red Hat) 
1962*4bdced5cSSteven Rostedt (Red Hat) static inline void rto_start_unlock(atomic_t *v)
1963*4bdced5cSSteven Rostedt (Red Hat) {
1964*4bdced5cSSteven Rostedt (Red Hat) 	atomic_set_release(v, 0);
1965*4bdced5cSSteven Rostedt (Red Hat) }
1966*4bdced5cSSteven Rostedt (Red Hat) 
1967*4bdced5cSSteven Rostedt (Red Hat) static void tell_cpu_to_push(struct rq *rq)
1968*4bdced5cSSteven Rostedt (Red Hat) {
1969*4bdced5cSSteven Rostedt (Red Hat) 	int cpu = -1;
1970*4bdced5cSSteven Rostedt (Red Hat) 
1971*4bdced5cSSteven Rostedt (Red Hat) 	/* Keep the loop going if the IPI is currently active */
1972*4bdced5cSSteven Rostedt (Red Hat) 	atomic_inc(&rq->rd->rto_loop_next);
1973*4bdced5cSSteven Rostedt (Red Hat) 
1974*4bdced5cSSteven Rostedt (Red Hat) 	/* Only one CPU can initiate a loop at a time */
1975*4bdced5cSSteven Rostedt (Red Hat) 	if (!rto_start_trylock(&rq->rd->rto_loop_start))
1976b6366f04SSteven Rostedt 		return;
1977b6366f04SSteven Rostedt 
1978*4bdced5cSSteven Rostedt (Red Hat) 	raw_spin_lock(&rq->rd->rto_lock);
1979b6366f04SSteven Rostedt 
1980*4bdced5cSSteven Rostedt (Red Hat) 	/*
1981*4bdced5cSSteven Rostedt (Red Hat) 	 * The rto_cpu is updated under the lock, if it has a valid cpu
1982*4bdced5cSSteven Rostedt (Red Hat) 	 * then the IPI is still running and will continue due to the
1983*4bdced5cSSteven Rostedt (Red Hat) 	 * update to loop_next, and nothing needs to be done here.
1984*4bdced5cSSteven Rostedt (Red Hat) 	 * Otherwise it is finishing up and an ipi needs to be sent.
1985*4bdced5cSSteven Rostedt (Red Hat) 	 */
1986*4bdced5cSSteven Rostedt (Red Hat) 	if (rq->rd->rto_cpu < 0)
1987*4bdced5cSSteven Rostedt (Red Hat) 		cpu = rto_next_cpu(rq);
1988*4bdced5cSSteven Rostedt (Red Hat) 
1989*4bdced5cSSteven Rostedt (Red Hat) 	raw_spin_unlock(&rq->rd->rto_lock);
1990*4bdced5cSSteven Rostedt (Red Hat) 
1991*4bdced5cSSteven Rostedt (Red Hat) 	rto_start_unlock(&rq->rd->rto_loop_start);
1992*4bdced5cSSteven Rostedt (Red Hat) 
1993*4bdced5cSSteven Rostedt (Red Hat) 	if (cpu >= 0)
1994*4bdced5cSSteven Rostedt (Red Hat) 		irq_work_queue_on(&rq->rd->rto_push_work, cpu);
1995b6366f04SSteven Rostedt }
1996b6366f04SSteven Rostedt 
1997b6366f04SSteven Rostedt /* Called from hardirq context */
1998*4bdced5cSSteven Rostedt (Red Hat) void rto_push_irq_work_func(struct irq_work *work)
1999b6366f04SSteven Rostedt {
2000*4bdced5cSSteven Rostedt (Red Hat) 	struct rq *rq;
2001b6366f04SSteven Rostedt 	int cpu;
2002b6366f04SSteven Rostedt 
2003*4bdced5cSSteven Rostedt (Red Hat) 	rq = this_rq();
2004b6366f04SSteven Rostedt 
2005*4bdced5cSSteven Rostedt (Red Hat) 	/*
2006*4bdced5cSSteven Rostedt (Red Hat) 	 * We do not need to grab the lock to check for has_pushable_tasks.
2007*4bdced5cSSteven Rostedt (Red Hat) 	 * When it gets updated, a check is made if a push is possible.
2008*4bdced5cSSteven Rostedt (Red Hat) 	 */
2009b6366f04SSteven Rostedt 	if (has_pushable_tasks(rq)) {
2010b6366f04SSteven Rostedt 		raw_spin_lock(&rq->lock);
2011*4bdced5cSSteven Rostedt (Red Hat) 		push_rt_tasks(rq);
2012b6366f04SSteven Rostedt 		raw_spin_unlock(&rq->lock);
2013b6366f04SSteven Rostedt 	}
2014b6366f04SSteven Rostedt 
2015*4bdced5cSSteven Rostedt (Red Hat) 	raw_spin_lock(&rq->rd->rto_lock);
2016*4bdced5cSSteven Rostedt (Red Hat) 
2017b6366f04SSteven Rostedt 	/* Pass the IPI to the next rt overloaded queue */
2018*4bdced5cSSteven Rostedt (Red Hat) 	cpu = rto_next_cpu(rq);
2019b6366f04SSteven Rostedt 
2020*4bdced5cSSteven Rostedt (Red Hat) 	raw_spin_unlock(&rq->rd->rto_lock);
2021b6366f04SSteven Rostedt 
2022*4bdced5cSSteven Rostedt (Red Hat) 	if (cpu < 0)
2023b6366f04SSteven Rostedt 		return;
2024b6366f04SSteven Rostedt 
2025b6366f04SSteven Rostedt 	/* Try the next RT overloaded CPU */
2026*4bdced5cSSteven Rostedt (Red Hat) 	irq_work_queue_on(&rq->rd->rto_push_work, cpu);
2027b6366f04SSteven Rostedt }
2028b6366f04SSteven Rostedt #endif /* HAVE_RT_PUSH_IPI */
2029b6366f04SSteven Rostedt 
20308046d680SPeter Zijlstra static void pull_rt_task(struct rq *this_rq)
2031391e43daSPeter Zijlstra {
20328046d680SPeter Zijlstra 	int this_cpu = this_rq->cpu, cpu;
20338046d680SPeter Zijlstra 	bool resched = false;
2034391e43daSPeter Zijlstra 	struct task_struct *p;
2035391e43daSPeter Zijlstra 	struct rq *src_rq;
2036391e43daSPeter Zijlstra 
2037391e43daSPeter Zijlstra 	if (likely(!rt_overloaded(this_rq)))
20388046d680SPeter Zijlstra 		return;
2039391e43daSPeter Zijlstra 
20407c3f2ab7SPeter Zijlstra 	/*
20417c3f2ab7SPeter Zijlstra 	 * Match the barrier from rt_set_overloaded; this guarantees that if we
20427c3f2ab7SPeter Zijlstra 	 * see overloaded we must also see the rto_mask bit.
20437c3f2ab7SPeter Zijlstra 	 */
20447c3f2ab7SPeter Zijlstra 	smp_rmb();
20457c3f2ab7SPeter Zijlstra 
2046b6366f04SSteven Rostedt #ifdef HAVE_RT_PUSH_IPI
2047b6366f04SSteven Rostedt 	if (sched_feat(RT_PUSH_IPI)) {
2048b6366f04SSteven Rostedt 		tell_cpu_to_push(this_rq);
20498046d680SPeter Zijlstra 		return;
2050b6366f04SSteven Rostedt 	}
2051b6366f04SSteven Rostedt #endif
2052b6366f04SSteven Rostedt 
2053391e43daSPeter Zijlstra 	for_each_cpu(cpu, this_rq->rd->rto_mask) {
2054391e43daSPeter Zijlstra 		if (this_cpu == cpu)
2055391e43daSPeter Zijlstra 			continue;
2056391e43daSPeter Zijlstra 
2057391e43daSPeter Zijlstra 		src_rq = cpu_rq(cpu);
2058391e43daSPeter Zijlstra 
2059391e43daSPeter Zijlstra 		/*
2060391e43daSPeter Zijlstra 		 * Don't bother taking the src_rq->lock if the next highest
2061391e43daSPeter Zijlstra 		 * task is known to be lower-priority than our current task.
2062391e43daSPeter Zijlstra 		 * This may look racy, but if this value is about to go
2063391e43daSPeter Zijlstra 		 * logically higher, the src_rq will push this task away.
2064391e43daSPeter Zijlstra 		 * And if its going logically lower, we do not care
2065391e43daSPeter Zijlstra 		 */
2066391e43daSPeter Zijlstra 		if (src_rq->rt.highest_prio.next >=
2067391e43daSPeter Zijlstra 		    this_rq->rt.highest_prio.curr)
2068391e43daSPeter Zijlstra 			continue;
2069391e43daSPeter Zijlstra 
2070391e43daSPeter Zijlstra 		/*
2071391e43daSPeter Zijlstra 		 * We can potentially drop this_rq's lock in
2072391e43daSPeter Zijlstra 		 * double_lock_balance, and another CPU could
2073391e43daSPeter Zijlstra 		 * alter this_rq
2074391e43daSPeter Zijlstra 		 */
2075391e43daSPeter Zijlstra 		double_lock_balance(this_rq, src_rq);
2076391e43daSPeter Zijlstra 
2077391e43daSPeter Zijlstra 		/*
2078e23ee747SKirill Tkhai 		 * We can pull only a task, which is pushable
2079e23ee747SKirill Tkhai 		 * on its rq, and no others.
2080391e43daSPeter Zijlstra 		 */
2081e23ee747SKirill Tkhai 		p = pick_highest_pushable_task(src_rq, this_cpu);
2082391e43daSPeter Zijlstra 
2083391e43daSPeter Zijlstra 		/*
2084391e43daSPeter Zijlstra 		 * Do we have an RT task that preempts
2085391e43daSPeter Zijlstra 		 * the to-be-scheduled task?
2086391e43daSPeter Zijlstra 		 */
2087391e43daSPeter Zijlstra 		if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2088391e43daSPeter Zijlstra 			WARN_ON(p == src_rq->curr);
2089da0c1e65SKirill Tkhai 			WARN_ON(!task_on_rq_queued(p));
2090391e43daSPeter Zijlstra 
2091391e43daSPeter Zijlstra 			/*
2092391e43daSPeter Zijlstra 			 * There's a chance that p is higher in priority
2093391e43daSPeter Zijlstra 			 * than what's currently running on its cpu.
2094391e43daSPeter Zijlstra 			 * This is just that p is wakeing up and hasn't
2095391e43daSPeter Zijlstra 			 * had a chance to schedule. We only pull
2096391e43daSPeter Zijlstra 			 * p if it is lower in priority than the
2097391e43daSPeter Zijlstra 			 * current task on the run queue
2098391e43daSPeter Zijlstra 			 */
2099391e43daSPeter Zijlstra 			if (p->prio < src_rq->curr->prio)
2100391e43daSPeter Zijlstra 				goto skip;
2101391e43daSPeter Zijlstra 
21028046d680SPeter Zijlstra 			resched = true;
2103391e43daSPeter Zijlstra 
2104391e43daSPeter Zijlstra 			deactivate_task(src_rq, p, 0);
2105391e43daSPeter Zijlstra 			set_task_cpu(p, this_cpu);
2106391e43daSPeter Zijlstra 			activate_task(this_rq, p, 0);
2107391e43daSPeter Zijlstra 			/*
2108391e43daSPeter Zijlstra 			 * We continue with the search, just in
2109391e43daSPeter Zijlstra 			 * case there's an even higher prio task
2110391e43daSPeter Zijlstra 			 * in another runqueue. (low likelihood
2111391e43daSPeter Zijlstra 			 * but possible)
2112391e43daSPeter Zijlstra 			 */
2113391e43daSPeter Zijlstra 		}
2114391e43daSPeter Zijlstra skip:
2115391e43daSPeter Zijlstra 		double_unlock_balance(this_rq, src_rq);
2116391e43daSPeter Zijlstra 	}
2117391e43daSPeter Zijlstra 
21188046d680SPeter Zijlstra 	if (resched)
21198046d680SPeter Zijlstra 		resched_curr(this_rq);
2120391e43daSPeter Zijlstra }
2121391e43daSPeter Zijlstra 
2122391e43daSPeter Zijlstra /*
2123391e43daSPeter Zijlstra  * If we are not running and we are not going to reschedule soon, we should
2124391e43daSPeter Zijlstra  * try to push tasks away now
2125391e43daSPeter Zijlstra  */
2126391e43daSPeter Zijlstra static void task_woken_rt(struct rq *rq, struct task_struct *p)
2127391e43daSPeter Zijlstra {
2128391e43daSPeter Zijlstra 	if (!task_running(rq, p) &&
2129391e43daSPeter Zijlstra 	    !test_tsk_need_resched(rq->curr) &&
21304b53a341SIngo Molnar 	    p->nr_cpus_allowed > 1 &&
21311baca4ceSJuri Lelli 	    (dl_task(rq->curr) || rt_task(rq->curr)) &&
21324b53a341SIngo Molnar 	    (rq->curr->nr_cpus_allowed < 2 ||
2133391e43daSPeter Zijlstra 	     rq->curr->prio <= p->prio))
2134391e43daSPeter Zijlstra 		push_rt_tasks(rq);
2135391e43daSPeter Zijlstra }
2136391e43daSPeter Zijlstra 
2137391e43daSPeter Zijlstra /* Assumes rq->lock is held */
2138391e43daSPeter Zijlstra static void rq_online_rt(struct rq *rq)
2139391e43daSPeter Zijlstra {
2140391e43daSPeter Zijlstra 	if (rq->rt.overloaded)
2141391e43daSPeter Zijlstra 		rt_set_overload(rq);
2142391e43daSPeter Zijlstra 
2143391e43daSPeter Zijlstra 	__enable_runtime(rq);
2144391e43daSPeter Zijlstra 
2145391e43daSPeter Zijlstra 	cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
2146391e43daSPeter Zijlstra }
2147391e43daSPeter Zijlstra 
2148391e43daSPeter Zijlstra /* Assumes rq->lock is held */
2149391e43daSPeter Zijlstra static void rq_offline_rt(struct rq *rq)
2150391e43daSPeter Zijlstra {
2151391e43daSPeter Zijlstra 	if (rq->rt.overloaded)
2152391e43daSPeter Zijlstra 		rt_clear_overload(rq);
2153391e43daSPeter Zijlstra 
2154391e43daSPeter Zijlstra 	__disable_runtime(rq);
2155391e43daSPeter Zijlstra 
2156391e43daSPeter Zijlstra 	cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
2157391e43daSPeter Zijlstra }
2158391e43daSPeter Zijlstra 
2159391e43daSPeter Zijlstra /*
2160391e43daSPeter Zijlstra  * When switch from the rt queue, we bring ourselves to a position
2161391e43daSPeter Zijlstra  * that we might want to pull RT tasks from other runqueues.
2162391e43daSPeter Zijlstra  */
2163391e43daSPeter Zijlstra static void switched_from_rt(struct rq *rq, struct task_struct *p)
2164391e43daSPeter Zijlstra {
2165391e43daSPeter Zijlstra 	/*
2166391e43daSPeter Zijlstra 	 * If there are other RT tasks then we will reschedule
2167391e43daSPeter Zijlstra 	 * and the scheduling of the other RT tasks will handle
2168391e43daSPeter Zijlstra 	 * the balancing. But if we are the last RT task
2169391e43daSPeter Zijlstra 	 * we may need to handle the pulling of RT tasks
2170391e43daSPeter Zijlstra 	 * now.
2171391e43daSPeter Zijlstra 	 */
2172da0c1e65SKirill Tkhai 	if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
21731158ddb5SKirill Tkhai 		return;
21741158ddb5SKirill Tkhai 
2175fd7a4bedSPeter Zijlstra 	queue_pull_task(rq);
2176391e43daSPeter Zijlstra }
2177391e43daSPeter Zijlstra 
217811c785b7SLi Zefan void __init init_sched_rt_class(void)
2179391e43daSPeter Zijlstra {
2180391e43daSPeter Zijlstra 	unsigned int i;
2181391e43daSPeter Zijlstra 
2182391e43daSPeter Zijlstra 	for_each_possible_cpu(i) {
2183391e43daSPeter Zijlstra 		zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
2184391e43daSPeter Zijlstra 					GFP_KERNEL, cpu_to_node(i));
2185391e43daSPeter Zijlstra 	}
2186391e43daSPeter Zijlstra }
2187391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
2188391e43daSPeter Zijlstra 
2189391e43daSPeter Zijlstra /*
2190391e43daSPeter Zijlstra  * When switching a task to RT, we may overload the runqueue
2191391e43daSPeter Zijlstra  * with RT tasks. In this case we try to push them off to
2192391e43daSPeter Zijlstra  * other runqueues.
2193391e43daSPeter Zijlstra  */
2194391e43daSPeter Zijlstra static void switched_to_rt(struct rq *rq, struct task_struct *p)
2195391e43daSPeter Zijlstra {
2196391e43daSPeter Zijlstra 	/*
2197391e43daSPeter Zijlstra 	 * If we are already running, then there's nothing
2198391e43daSPeter Zijlstra 	 * that needs to be done. But if we are not running
2199391e43daSPeter Zijlstra 	 * we may need to preempt the current running task.
2200391e43daSPeter Zijlstra 	 * If that current running task is also an RT task
2201391e43daSPeter Zijlstra 	 * then see if we can move to another run queue.
2202391e43daSPeter Zijlstra 	 */
2203da0c1e65SKirill Tkhai 	if (task_on_rq_queued(p) && rq->curr != p) {
2204391e43daSPeter Zijlstra #ifdef CONFIG_SMP
22054b53a341SIngo Molnar 		if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
2206fd7a4bedSPeter Zijlstra 			queue_push_tasks(rq);
2207619bd4a7SSebastian Andrzej Siewior #endif /* CONFIG_SMP */
2208fd7a4bedSPeter Zijlstra 		if (p->prio < rq->curr->prio)
22098875125eSKirill Tkhai 			resched_curr(rq);
2210391e43daSPeter Zijlstra 	}
2211391e43daSPeter Zijlstra }
2212391e43daSPeter Zijlstra 
2213391e43daSPeter Zijlstra /*
2214391e43daSPeter Zijlstra  * Priority of the task has changed. This may cause
2215391e43daSPeter Zijlstra  * us to initiate a push or pull.
2216391e43daSPeter Zijlstra  */
2217391e43daSPeter Zijlstra static void
2218391e43daSPeter Zijlstra prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
2219391e43daSPeter Zijlstra {
2220da0c1e65SKirill Tkhai 	if (!task_on_rq_queued(p))
2221391e43daSPeter Zijlstra 		return;
2222391e43daSPeter Zijlstra 
2223391e43daSPeter Zijlstra 	if (rq->curr == p) {
2224391e43daSPeter Zijlstra #ifdef CONFIG_SMP
2225391e43daSPeter Zijlstra 		/*
2226391e43daSPeter Zijlstra 		 * If our priority decreases while running, we
2227391e43daSPeter Zijlstra 		 * may need to pull tasks to this runqueue.
2228391e43daSPeter Zijlstra 		 */
2229391e43daSPeter Zijlstra 		if (oldprio < p->prio)
2230fd7a4bedSPeter Zijlstra 			queue_pull_task(rq);
2231fd7a4bedSPeter Zijlstra 
2232391e43daSPeter Zijlstra 		/*
2233391e43daSPeter Zijlstra 		 * If there's a higher priority task waiting to run
2234fd7a4bedSPeter Zijlstra 		 * then reschedule.
2235391e43daSPeter Zijlstra 		 */
2236fd7a4bedSPeter Zijlstra 		if (p->prio > rq->rt.highest_prio.curr)
22378875125eSKirill Tkhai 			resched_curr(rq);
2238391e43daSPeter Zijlstra #else
2239391e43daSPeter Zijlstra 		/* For UP simply resched on drop of prio */
2240391e43daSPeter Zijlstra 		if (oldprio < p->prio)
22418875125eSKirill Tkhai 			resched_curr(rq);
2242391e43daSPeter Zijlstra #endif /* CONFIG_SMP */
2243391e43daSPeter Zijlstra 	} else {
2244391e43daSPeter Zijlstra 		/*
2245391e43daSPeter Zijlstra 		 * This task is not running, but if it is
2246391e43daSPeter Zijlstra 		 * greater than the current running task
2247391e43daSPeter Zijlstra 		 * then reschedule.
2248391e43daSPeter Zijlstra 		 */
2249391e43daSPeter Zijlstra 		if (p->prio < rq->curr->prio)
22508875125eSKirill Tkhai 			resched_curr(rq);
2251391e43daSPeter Zijlstra 	}
2252391e43daSPeter Zijlstra }
2253391e43daSPeter Zijlstra 
2254b18b6a9cSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS
2255391e43daSPeter Zijlstra static void watchdog(struct rq *rq, struct task_struct *p)
2256391e43daSPeter Zijlstra {
2257391e43daSPeter Zijlstra 	unsigned long soft, hard;
2258391e43daSPeter Zijlstra 
2259391e43daSPeter Zijlstra 	/* max may change after cur was read, this will be fixed next tick */
2260391e43daSPeter Zijlstra 	soft = task_rlimit(p, RLIMIT_RTTIME);
2261391e43daSPeter Zijlstra 	hard = task_rlimit_max(p, RLIMIT_RTTIME);
2262391e43daSPeter Zijlstra 
2263391e43daSPeter Zijlstra 	if (soft != RLIM_INFINITY) {
2264391e43daSPeter Zijlstra 		unsigned long next;
2265391e43daSPeter Zijlstra 
226657d2aa00SYing Xue 		if (p->rt.watchdog_stamp != jiffies) {
2267391e43daSPeter Zijlstra 			p->rt.timeout++;
226857d2aa00SYing Xue 			p->rt.watchdog_stamp = jiffies;
226957d2aa00SYing Xue 		}
227057d2aa00SYing Xue 
2271391e43daSPeter Zijlstra 		next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
2272391e43daSPeter Zijlstra 		if (p->rt.timeout > next)
2273391e43daSPeter Zijlstra 			p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
2274391e43daSPeter Zijlstra 	}
2275391e43daSPeter Zijlstra }
2276b18b6a9cSNicolas Pitre #else
2277b18b6a9cSNicolas Pitre static inline void watchdog(struct rq *rq, struct task_struct *p) { }
2278b18b6a9cSNicolas Pitre #endif
2279391e43daSPeter Zijlstra 
2280391e43daSPeter Zijlstra static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
2281391e43daSPeter Zijlstra {
2282454c7999SColin Cross 	struct sched_rt_entity *rt_se = &p->rt;
2283454c7999SColin Cross 
2284391e43daSPeter Zijlstra 	update_curr_rt(rq);
2285391e43daSPeter Zijlstra 
2286391e43daSPeter Zijlstra 	watchdog(rq, p);
2287391e43daSPeter Zijlstra 
2288391e43daSPeter Zijlstra 	/*
2289391e43daSPeter Zijlstra 	 * RR tasks need a special form of timeslice management.
2290391e43daSPeter Zijlstra 	 * FIFO tasks have no timeslices.
2291391e43daSPeter Zijlstra 	 */
2292391e43daSPeter Zijlstra 	if (p->policy != SCHED_RR)
2293391e43daSPeter Zijlstra 		return;
2294391e43daSPeter Zijlstra 
2295391e43daSPeter Zijlstra 	if (--p->rt.time_slice)
2296391e43daSPeter Zijlstra 		return;
2297391e43daSPeter Zijlstra 
2298ce0dbbbbSClark Williams 	p->rt.time_slice = sched_rr_timeslice;
2299391e43daSPeter Zijlstra 
2300391e43daSPeter Zijlstra 	/*
2301e9aa39bbSLi Bin 	 * Requeue to the end of queue if we (and all of our ancestors) are not
2302e9aa39bbSLi Bin 	 * the only element on the queue
2303391e43daSPeter Zijlstra 	 */
2304454c7999SColin Cross 	for_each_sched_rt_entity(rt_se) {
2305454c7999SColin Cross 		if (rt_se->run_list.prev != rt_se->run_list.next) {
2306391e43daSPeter Zijlstra 			requeue_task_rt(rq, p, 0);
23078aa6f0ebSKirill Tkhai 			resched_curr(rq);
2308454c7999SColin Cross 			return;
2309454c7999SColin Cross 		}
2310391e43daSPeter Zijlstra 	}
2311391e43daSPeter Zijlstra }
2312391e43daSPeter Zijlstra 
2313391e43daSPeter Zijlstra static void set_curr_task_rt(struct rq *rq)
2314391e43daSPeter Zijlstra {
2315391e43daSPeter Zijlstra 	struct task_struct *p = rq->curr;
2316391e43daSPeter Zijlstra 
231778becc27SFrederic Weisbecker 	p->se.exec_start = rq_clock_task(rq);
2318391e43daSPeter Zijlstra 
2319391e43daSPeter Zijlstra 	/* The running task is never eligible for pushing */
2320391e43daSPeter Zijlstra 	dequeue_pushable_task(rq, p);
2321391e43daSPeter Zijlstra }
2322391e43daSPeter Zijlstra 
2323391e43daSPeter Zijlstra static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
2324391e43daSPeter Zijlstra {
2325391e43daSPeter Zijlstra 	/*
2326391e43daSPeter Zijlstra 	 * Time slice is 0 for SCHED_FIFO tasks
2327391e43daSPeter Zijlstra 	 */
2328391e43daSPeter Zijlstra 	if (task->policy == SCHED_RR)
2329ce0dbbbbSClark Williams 		return sched_rr_timeslice;
2330391e43daSPeter Zijlstra 	else
2331391e43daSPeter Zijlstra 		return 0;
2332391e43daSPeter Zijlstra }
2333391e43daSPeter Zijlstra 
2334391e43daSPeter Zijlstra const struct sched_class rt_sched_class = {
2335391e43daSPeter Zijlstra 	.next			= &fair_sched_class,
2336391e43daSPeter Zijlstra 	.enqueue_task		= enqueue_task_rt,
2337391e43daSPeter Zijlstra 	.dequeue_task		= dequeue_task_rt,
2338391e43daSPeter Zijlstra 	.yield_task		= yield_task_rt,
2339391e43daSPeter Zijlstra 
2340391e43daSPeter Zijlstra 	.check_preempt_curr	= check_preempt_curr_rt,
2341391e43daSPeter Zijlstra 
2342391e43daSPeter Zijlstra 	.pick_next_task		= pick_next_task_rt,
2343391e43daSPeter Zijlstra 	.put_prev_task		= put_prev_task_rt,
2344391e43daSPeter Zijlstra 
2345391e43daSPeter Zijlstra #ifdef CONFIG_SMP
2346391e43daSPeter Zijlstra 	.select_task_rq		= select_task_rq_rt,
2347391e43daSPeter Zijlstra 
23486c37067eSPeter Zijlstra 	.set_cpus_allowed       = set_cpus_allowed_common,
2349391e43daSPeter Zijlstra 	.rq_online              = rq_online_rt,
2350391e43daSPeter Zijlstra 	.rq_offline             = rq_offline_rt,
2351391e43daSPeter Zijlstra 	.task_woken		= task_woken_rt,
2352391e43daSPeter Zijlstra 	.switched_from		= switched_from_rt,
2353391e43daSPeter Zijlstra #endif
2354391e43daSPeter Zijlstra 
2355391e43daSPeter Zijlstra 	.set_curr_task          = set_curr_task_rt,
2356391e43daSPeter Zijlstra 	.task_tick		= task_tick_rt,
2357391e43daSPeter Zijlstra 
2358391e43daSPeter Zijlstra 	.get_rr_interval	= get_rr_interval_rt,
2359391e43daSPeter Zijlstra 
2360391e43daSPeter Zijlstra 	.prio_changed		= prio_changed_rt,
2361391e43daSPeter Zijlstra 	.switched_to		= switched_to_rt,
23626e998916SStanislaw Gruszka 
23636e998916SStanislaw Gruszka 	.update_curr		= update_curr_rt,
2364391e43daSPeter Zijlstra };
2365391e43daSPeter Zijlstra 
23668887cd99SNicolas Pitre #ifdef CONFIG_RT_GROUP_SCHED
23678887cd99SNicolas Pitre /*
23688887cd99SNicolas Pitre  * Ensure that the real time constraints are schedulable.
23698887cd99SNicolas Pitre  */
23708887cd99SNicolas Pitre static DEFINE_MUTEX(rt_constraints_mutex);
23718887cd99SNicolas Pitre 
23728887cd99SNicolas Pitre /* Must be called with tasklist_lock held */
23738887cd99SNicolas Pitre static inline int tg_has_rt_tasks(struct task_group *tg)
23748887cd99SNicolas Pitre {
23758887cd99SNicolas Pitre 	struct task_struct *g, *p;
23768887cd99SNicolas Pitre 
23778887cd99SNicolas Pitre 	/*
23788887cd99SNicolas Pitre 	 * Autogroups do not have RT tasks; see autogroup_create().
23798887cd99SNicolas Pitre 	 */
23808887cd99SNicolas Pitre 	if (task_group_is_autogroup(tg))
23818887cd99SNicolas Pitre 		return 0;
23828887cd99SNicolas Pitre 
23838887cd99SNicolas Pitre 	for_each_process_thread(g, p) {
23848887cd99SNicolas Pitre 		if (rt_task(p) && task_group(p) == tg)
23858887cd99SNicolas Pitre 			return 1;
23868887cd99SNicolas Pitre 	}
23878887cd99SNicolas Pitre 
23888887cd99SNicolas Pitre 	return 0;
23898887cd99SNicolas Pitre }
23908887cd99SNicolas Pitre 
23918887cd99SNicolas Pitre struct rt_schedulable_data {
23928887cd99SNicolas Pitre 	struct task_group *tg;
23938887cd99SNicolas Pitre 	u64 rt_period;
23948887cd99SNicolas Pitre 	u64 rt_runtime;
23958887cd99SNicolas Pitre };
23968887cd99SNicolas Pitre 
23978887cd99SNicolas Pitre static int tg_rt_schedulable(struct task_group *tg, void *data)
23988887cd99SNicolas Pitre {
23998887cd99SNicolas Pitre 	struct rt_schedulable_data *d = data;
24008887cd99SNicolas Pitre 	struct task_group *child;
24018887cd99SNicolas Pitre 	unsigned long total, sum = 0;
24028887cd99SNicolas Pitre 	u64 period, runtime;
24038887cd99SNicolas Pitre 
24048887cd99SNicolas Pitre 	period = ktime_to_ns(tg->rt_bandwidth.rt_period);
24058887cd99SNicolas Pitre 	runtime = tg->rt_bandwidth.rt_runtime;
24068887cd99SNicolas Pitre 
24078887cd99SNicolas Pitre 	if (tg == d->tg) {
24088887cd99SNicolas Pitre 		period = d->rt_period;
24098887cd99SNicolas Pitre 		runtime = d->rt_runtime;
24108887cd99SNicolas Pitre 	}
24118887cd99SNicolas Pitre 
24128887cd99SNicolas Pitre 	/*
24138887cd99SNicolas Pitre 	 * Cannot have more runtime than the period.
24148887cd99SNicolas Pitre 	 */
24158887cd99SNicolas Pitre 	if (runtime > period && runtime != RUNTIME_INF)
24168887cd99SNicolas Pitre 		return -EINVAL;
24178887cd99SNicolas Pitre 
24188887cd99SNicolas Pitre 	/*
24198887cd99SNicolas Pitre 	 * Ensure we don't starve existing RT tasks.
24208887cd99SNicolas Pitre 	 */
24218887cd99SNicolas Pitre 	if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
24228887cd99SNicolas Pitre 		return -EBUSY;
24238887cd99SNicolas Pitre 
24248887cd99SNicolas Pitre 	total = to_ratio(period, runtime);
24258887cd99SNicolas Pitre 
24268887cd99SNicolas Pitre 	/*
24278887cd99SNicolas Pitre 	 * Nobody can have more than the global setting allows.
24288887cd99SNicolas Pitre 	 */
24298887cd99SNicolas Pitre 	if (total > to_ratio(global_rt_period(), global_rt_runtime()))
24308887cd99SNicolas Pitre 		return -EINVAL;
24318887cd99SNicolas Pitre 
24328887cd99SNicolas Pitre 	/*
24338887cd99SNicolas Pitre 	 * The sum of our children's runtime should not exceed our own.
24348887cd99SNicolas Pitre 	 */
24358887cd99SNicolas Pitre 	list_for_each_entry_rcu(child, &tg->children, siblings) {
24368887cd99SNicolas Pitre 		period = ktime_to_ns(child->rt_bandwidth.rt_period);
24378887cd99SNicolas Pitre 		runtime = child->rt_bandwidth.rt_runtime;
24388887cd99SNicolas Pitre 
24398887cd99SNicolas Pitre 		if (child == d->tg) {
24408887cd99SNicolas Pitre 			period = d->rt_period;
24418887cd99SNicolas Pitre 			runtime = d->rt_runtime;
24428887cd99SNicolas Pitre 		}
24438887cd99SNicolas Pitre 
24448887cd99SNicolas Pitre 		sum += to_ratio(period, runtime);
24458887cd99SNicolas Pitre 	}
24468887cd99SNicolas Pitre 
24478887cd99SNicolas Pitre 	if (sum > total)
24488887cd99SNicolas Pitre 		return -EINVAL;
24498887cd99SNicolas Pitre 
24508887cd99SNicolas Pitre 	return 0;
24518887cd99SNicolas Pitre }
24528887cd99SNicolas Pitre 
24538887cd99SNicolas Pitre static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
24548887cd99SNicolas Pitre {
24558887cd99SNicolas Pitre 	int ret;
24568887cd99SNicolas Pitre 
24578887cd99SNicolas Pitre 	struct rt_schedulable_data data = {
24588887cd99SNicolas Pitre 		.tg = tg,
24598887cd99SNicolas Pitre 		.rt_period = period,
24608887cd99SNicolas Pitre 		.rt_runtime = runtime,
24618887cd99SNicolas Pitre 	};
24628887cd99SNicolas Pitre 
24638887cd99SNicolas Pitre 	rcu_read_lock();
24648887cd99SNicolas Pitre 	ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
24658887cd99SNicolas Pitre 	rcu_read_unlock();
24668887cd99SNicolas Pitre 
24678887cd99SNicolas Pitre 	return ret;
24688887cd99SNicolas Pitre }
24698887cd99SNicolas Pitre 
24708887cd99SNicolas Pitre static int tg_set_rt_bandwidth(struct task_group *tg,
24718887cd99SNicolas Pitre 		u64 rt_period, u64 rt_runtime)
24728887cd99SNicolas Pitre {
24738887cd99SNicolas Pitre 	int i, err = 0;
24748887cd99SNicolas Pitre 
24758887cd99SNicolas Pitre 	/*
24768887cd99SNicolas Pitre 	 * Disallowing the root group RT runtime is BAD, it would disallow the
24778887cd99SNicolas Pitre 	 * kernel creating (and or operating) RT threads.
24788887cd99SNicolas Pitre 	 */
24798887cd99SNicolas Pitre 	if (tg == &root_task_group && rt_runtime == 0)
24808887cd99SNicolas Pitre 		return -EINVAL;
24818887cd99SNicolas Pitre 
24828887cd99SNicolas Pitre 	/* No period doesn't make any sense. */
24838887cd99SNicolas Pitre 	if (rt_period == 0)
24848887cd99SNicolas Pitre 		return -EINVAL;
24858887cd99SNicolas Pitre 
24868887cd99SNicolas Pitre 	mutex_lock(&rt_constraints_mutex);
24878887cd99SNicolas Pitre 	read_lock(&tasklist_lock);
24888887cd99SNicolas Pitre 	err = __rt_schedulable(tg, rt_period, rt_runtime);
24898887cd99SNicolas Pitre 	if (err)
24908887cd99SNicolas Pitre 		goto unlock;
24918887cd99SNicolas Pitre 
24928887cd99SNicolas Pitre 	raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
24938887cd99SNicolas Pitre 	tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
24948887cd99SNicolas Pitre 	tg->rt_bandwidth.rt_runtime = rt_runtime;
24958887cd99SNicolas Pitre 
24968887cd99SNicolas Pitre 	for_each_possible_cpu(i) {
24978887cd99SNicolas Pitre 		struct rt_rq *rt_rq = tg->rt_rq[i];
24988887cd99SNicolas Pitre 
24998887cd99SNicolas Pitre 		raw_spin_lock(&rt_rq->rt_runtime_lock);
25008887cd99SNicolas Pitre 		rt_rq->rt_runtime = rt_runtime;
25018887cd99SNicolas Pitre 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
25028887cd99SNicolas Pitre 	}
25038887cd99SNicolas Pitre 	raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
25048887cd99SNicolas Pitre unlock:
25058887cd99SNicolas Pitre 	read_unlock(&tasklist_lock);
25068887cd99SNicolas Pitre 	mutex_unlock(&rt_constraints_mutex);
25078887cd99SNicolas Pitre 
25088887cd99SNicolas Pitre 	return err;
25098887cd99SNicolas Pitre }
25108887cd99SNicolas Pitre 
25118887cd99SNicolas Pitre int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
25128887cd99SNicolas Pitre {
25138887cd99SNicolas Pitre 	u64 rt_runtime, rt_period;
25148887cd99SNicolas Pitre 
25158887cd99SNicolas Pitre 	rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
25168887cd99SNicolas Pitre 	rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
25178887cd99SNicolas Pitre 	if (rt_runtime_us < 0)
25188887cd99SNicolas Pitre 		rt_runtime = RUNTIME_INF;
25198887cd99SNicolas Pitre 
25208887cd99SNicolas Pitre 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
25218887cd99SNicolas Pitre }
25228887cd99SNicolas Pitre 
25238887cd99SNicolas Pitre long sched_group_rt_runtime(struct task_group *tg)
25248887cd99SNicolas Pitre {
25258887cd99SNicolas Pitre 	u64 rt_runtime_us;
25268887cd99SNicolas Pitre 
25278887cd99SNicolas Pitre 	if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
25288887cd99SNicolas Pitre 		return -1;
25298887cd99SNicolas Pitre 
25308887cd99SNicolas Pitre 	rt_runtime_us = tg->rt_bandwidth.rt_runtime;
25318887cd99SNicolas Pitre 	do_div(rt_runtime_us, NSEC_PER_USEC);
25328887cd99SNicolas Pitre 	return rt_runtime_us;
25338887cd99SNicolas Pitre }
25348887cd99SNicolas Pitre 
25358887cd99SNicolas Pitre int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
25368887cd99SNicolas Pitre {
25378887cd99SNicolas Pitre 	u64 rt_runtime, rt_period;
25388887cd99SNicolas Pitre 
25398887cd99SNicolas Pitre 	rt_period = rt_period_us * NSEC_PER_USEC;
25408887cd99SNicolas Pitre 	rt_runtime = tg->rt_bandwidth.rt_runtime;
25418887cd99SNicolas Pitre 
25428887cd99SNicolas Pitre 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
25438887cd99SNicolas Pitre }
25448887cd99SNicolas Pitre 
25458887cd99SNicolas Pitre long sched_group_rt_period(struct task_group *tg)
25468887cd99SNicolas Pitre {
25478887cd99SNicolas Pitre 	u64 rt_period_us;
25488887cd99SNicolas Pitre 
25498887cd99SNicolas Pitre 	rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
25508887cd99SNicolas Pitre 	do_div(rt_period_us, NSEC_PER_USEC);
25518887cd99SNicolas Pitre 	return rt_period_us;
25528887cd99SNicolas Pitre }
25538887cd99SNicolas Pitre 
25548887cd99SNicolas Pitre static int sched_rt_global_constraints(void)
25558887cd99SNicolas Pitre {
25568887cd99SNicolas Pitre 	int ret = 0;
25578887cd99SNicolas Pitre 
25588887cd99SNicolas Pitre 	mutex_lock(&rt_constraints_mutex);
25598887cd99SNicolas Pitre 	read_lock(&tasklist_lock);
25608887cd99SNicolas Pitre 	ret = __rt_schedulable(NULL, 0, 0);
25618887cd99SNicolas Pitre 	read_unlock(&tasklist_lock);
25628887cd99SNicolas Pitre 	mutex_unlock(&rt_constraints_mutex);
25638887cd99SNicolas Pitre 
25648887cd99SNicolas Pitre 	return ret;
25658887cd99SNicolas Pitre }
25668887cd99SNicolas Pitre 
25678887cd99SNicolas Pitre int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
25688887cd99SNicolas Pitre {
25698887cd99SNicolas Pitre 	/* Don't accept realtime tasks when there is no way for them to run */
25708887cd99SNicolas Pitre 	if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
25718887cd99SNicolas Pitre 		return 0;
25728887cd99SNicolas Pitre 
25738887cd99SNicolas Pitre 	return 1;
25748887cd99SNicolas Pitre }
25758887cd99SNicolas Pitre 
25768887cd99SNicolas Pitre #else /* !CONFIG_RT_GROUP_SCHED */
25778887cd99SNicolas Pitre static int sched_rt_global_constraints(void)
25788887cd99SNicolas Pitre {
25798887cd99SNicolas Pitre 	unsigned long flags;
25808887cd99SNicolas Pitre 	int i;
25818887cd99SNicolas Pitre 
25828887cd99SNicolas Pitre 	raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
25838887cd99SNicolas Pitre 	for_each_possible_cpu(i) {
25848887cd99SNicolas Pitre 		struct rt_rq *rt_rq = &cpu_rq(i)->rt;
25858887cd99SNicolas Pitre 
25868887cd99SNicolas Pitre 		raw_spin_lock(&rt_rq->rt_runtime_lock);
25878887cd99SNicolas Pitre 		rt_rq->rt_runtime = global_rt_runtime();
25888887cd99SNicolas Pitre 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
25898887cd99SNicolas Pitre 	}
25908887cd99SNicolas Pitre 	raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
25918887cd99SNicolas Pitre 
25928887cd99SNicolas Pitre 	return 0;
25938887cd99SNicolas Pitre }
25948887cd99SNicolas Pitre #endif /* CONFIG_RT_GROUP_SCHED */
25958887cd99SNicolas Pitre 
25968887cd99SNicolas Pitre static int sched_rt_global_validate(void)
25978887cd99SNicolas Pitre {
25988887cd99SNicolas Pitre 	if (sysctl_sched_rt_period <= 0)
25998887cd99SNicolas Pitre 		return -EINVAL;
26008887cd99SNicolas Pitre 
26018887cd99SNicolas Pitre 	if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
26028887cd99SNicolas Pitre 		(sysctl_sched_rt_runtime > sysctl_sched_rt_period))
26038887cd99SNicolas Pitre 		return -EINVAL;
26048887cd99SNicolas Pitre 
26058887cd99SNicolas Pitre 	return 0;
26068887cd99SNicolas Pitre }
26078887cd99SNicolas Pitre 
26088887cd99SNicolas Pitre static void sched_rt_do_global(void)
26098887cd99SNicolas Pitre {
26108887cd99SNicolas Pitre 	def_rt_bandwidth.rt_runtime = global_rt_runtime();
26118887cd99SNicolas Pitre 	def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
26128887cd99SNicolas Pitre }
26138887cd99SNicolas Pitre 
26148887cd99SNicolas Pitre int sched_rt_handler(struct ctl_table *table, int write,
26158887cd99SNicolas Pitre 		void __user *buffer, size_t *lenp,
26168887cd99SNicolas Pitre 		loff_t *ppos)
26178887cd99SNicolas Pitre {
26188887cd99SNicolas Pitre 	int old_period, old_runtime;
26198887cd99SNicolas Pitre 	static DEFINE_MUTEX(mutex);
26208887cd99SNicolas Pitre 	int ret;
26218887cd99SNicolas Pitre 
26228887cd99SNicolas Pitre 	mutex_lock(&mutex);
26238887cd99SNicolas Pitre 	old_period = sysctl_sched_rt_period;
26248887cd99SNicolas Pitre 	old_runtime = sysctl_sched_rt_runtime;
26258887cd99SNicolas Pitre 
26268887cd99SNicolas Pitre 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
26278887cd99SNicolas Pitre 
26288887cd99SNicolas Pitre 	if (!ret && write) {
26298887cd99SNicolas Pitre 		ret = sched_rt_global_validate();
26308887cd99SNicolas Pitre 		if (ret)
26318887cd99SNicolas Pitre 			goto undo;
26328887cd99SNicolas Pitre 
26338887cd99SNicolas Pitre 		ret = sched_dl_global_validate();
26348887cd99SNicolas Pitre 		if (ret)
26358887cd99SNicolas Pitre 			goto undo;
26368887cd99SNicolas Pitre 
26378887cd99SNicolas Pitre 		ret = sched_rt_global_constraints();
26388887cd99SNicolas Pitre 		if (ret)
26398887cd99SNicolas Pitre 			goto undo;
26408887cd99SNicolas Pitre 
26418887cd99SNicolas Pitre 		sched_rt_do_global();
26428887cd99SNicolas Pitre 		sched_dl_do_global();
26438887cd99SNicolas Pitre 	}
26448887cd99SNicolas Pitre 	if (0) {
26458887cd99SNicolas Pitre undo:
26468887cd99SNicolas Pitre 		sysctl_sched_rt_period = old_period;
26478887cd99SNicolas Pitre 		sysctl_sched_rt_runtime = old_runtime;
26488887cd99SNicolas Pitre 	}
26498887cd99SNicolas Pitre 	mutex_unlock(&mutex);
26508887cd99SNicolas Pitre 
26518887cd99SNicolas Pitre 	return ret;
26528887cd99SNicolas Pitre }
26538887cd99SNicolas Pitre 
26548887cd99SNicolas Pitre int sched_rr_handler(struct ctl_table *table, int write,
26558887cd99SNicolas Pitre 		void __user *buffer, size_t *lenp,
26568887cd99SNicolas Pitre 		loff_t *ppos)
26578887cd99SNicolas Pitre {
26588887cd99SNicolas Pitre 	int ret;
26598887cd99SNicolas Pitre 	static DEFINE_MUTEX(mutex);
26608887cd99SNicolas Pitre 
26618887cd99SNicolas Pitre 	mutex_lock(&mutex);
26628887cd99SNicolas Pitre 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
26638887cd99SNicolas Pitre 	/*
26648887cd99SNicolas Pitre 	 * Make sure that internally we keep jiffies.
26658887cd99SNicolas Pitre 	 * Also, writing zero resets the timeslice to default:
26668887cd99SNicolas Pitre 	 */
26678887cd99SNicolas Pitre 	if (!ret && write) {
26688887cd99SNicolas Pitre 		sched_rr_timeslice =
26698887cd99SNicolas Pitre 			sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
26708887cd99SNicolas Pitre 			msecs_to_jiffies(sysctl_sched_rr_timeslice);
26718887cd99SNicolas Pitre 	}
26728887cd99SNicolas Pitre 	mutex_unlock(&mutex);
26738887cd99SNicolas Pitre 	return ret;
26748887cd99SNicolas Pitre }
26758887cd99SNicolas Pitre 
2676391e43daSPeter Zijlstra #ifdef CONFIG_SCHED_DEBUG
2677391e43daSPeter Zijlstra extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
2678391e43daSPeter Zijlstra 
2679391e43daSPeter Zijlstra void print_rt_stats(struct seq_file *m, int cpu)
2680391e43daSPeter Zijlstra {
2681391e43daSPeter Zijlstra 	rt_rq_iter_t iter;
2682391e43daSPeter Zijlstra 	struct rt_rq *rt_rq;
2683391e43daSPeter Zijlstra 
2684391e43daSPeter Zijlstra 	rcu_read_lock();
2685391e43daSPeter Zijlstra 	for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
2686391e43daSPeter Zijlstra 		print_rt_rq(m, cpu, rt_rq);
2687391e43daSPeter Zijlstra 	rcu_read_unlock();
2688391e43daSPeter Zijlstra }
2689391e43daSPeter Zijlstra #endif /* CONFIG_SCHED_DEBUG */
2690