• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
4  * policies)
5  */
6 
7 #include <trace/hooks/sched.h>
8 
9 int sched_rr_timeslice = RR_TIMESLICE;
10 /* More than 4 hours if BW_SHIFT equals 20. */
11 static const u64 max_rt_runtime = MAX_BW;
12 
13 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
14 
15 struct rt_bandwidth def_rt_bandwidth;
16 
17 /*
18  * period over which we measure -rt task CPU usage in us.
19  * default: 1s
20  */
21 unsigned int sysctl_sched_rt_period = 1000000;
22 
23 /*
24  * part of the period that we allow rt tasks to run in us.
25  * default: 0.95s
26  */
27 int sysctl_sched_rt_runtime = 950000;
28 
29 #ifdef CONFIG_SYSCTL
30 static int sysctl_sched_rr_timeslice = (MSEC_PER_SEC * RR_TIMESLICE) / HZ;
31 static int sched_rt_handler(struct ctl_table *table, int write, void *buffer,
32 		size_t *lenp, loff_t *ppos);
33 static int sched_rr_handler(struct ctl_table *table, int write, void *buffer,
34 		size_t *lenp, loff_t *ppos);
35 static struct ctl_table sched_rt_sysctls[] = {
36 	{
37 		.procname       = "sched_rt_period_us",
38 		.data           = &sysctl_sched_rt_period,
39 		.maxlen         = sizeof(unsigned int),
40 		.mode           = 0644,
41 		.proc_handler   = sched_rt_handler,
42 		.extra1         = SYSCTL_ONE,
43 		.extra2         = SYSCTL_INT_MAX,
44 	},
45 	{
46 		.procname       = "sched_rt_runtime_us",
47 		.data           = &sysctl_sched_rt_runtime,
48 		.maxlen         = sizeof(int),
49 		.mode           = 0644,
50 		.proc_handler   = sched_rt_handler,
51 		.extra1         = SYSCTL_NEG_ONE,
52 		.extra2         = SYSCTL_INT_MAX,
53 	},
54 	{
55 		.procname       = "sched_rr_timeslice_ms",
56 		.data           = &sysctl_sched_rr_timeslice,
57 		.maxlen         = sizeof(int),
58 		.mode           = 0644,
59 		.proc_handler   = sched_rr_handler,
60 	},
61 	{}
62 };
63 
sched_rt_sysctl_init(void)64 static int __init sched_rt_sysctl_init(void)
65 {
66 	register_sysctl_init("kernel", sched_rt_sysctls);
67 	return 0;
68 }
69 late_initcall(sched_rt_sysctl_init);
70 #endif
71 
sched_rt_period_timer(struct hrtimer * timer)72 static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
73 {
74 	struct rt_bandwidth *rt_b =
75 		container_of(timer, struct rt_bandwidth, rt_period_timer);
76 	int idle = 0;
77 	int overrun;
78 
79 	raw_spin_lock(&rt_b->rt_runtime_lock);
80 	for (;;) {
81 		overrun = hrtimer_forward_now(timer, rt_b->rt_period);
82 		if (!overrun)
83 			break;
84 
85 		raw_spin_unlock(&rt_b->rt_runtime_lock);
86 		idle = do_sched_rt_period_timer(rt_b, overrun);
87 		raw_spin_lock(&rt_b->rt_runtime_lock);
88 	}
89 	if (idle)
90 		rt_b->rt_period_active = 0;
91 	raw_spin_unlock(&rt_b->rt_runtime_lock);
92 
93 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
94 }
95 
init_rt_bandwidth(struct rt_bandwidth * rt_b,u64 period,u64 runtime)96 void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
97 {
98 	rt_b->rt_period = ns_to_ktime(period);
99 	rt_b->rt_runtime = runtime;
100 
101 	raw_spin_lock_init(&rt_b->rt_runtime_lock);
102 
103 	hrtimer_init(&rt_b->rt_period_timer, CLOCK_MONOTONIC,
104 		     HRTIMER_MODE_REL_HARD);
105 	rt_b->rt_period_timer.function = sched_rt_period_timer;
106 }
107 
do_start_rt_bandwidth(struct rt_bandwidth * rt_b)108 static inline void do_start_rt_bandwidth(struct rt_bandwidth *rt_b)
109 {
110 	raw_spin_lock(&rt_b->rt_runtime_lock);
111 	if (!rt_b->rt_period_active) {
112 		rt_b->rt_period_active = 1;
113 		/*
114 		 * SCHED_DEADLINE updates the bandwidth, as a run away
115 		 * RT task with a DL task could hog a CPU. But DL does
116 		 * not reset the period. If a deadline task was running
117 		 * without an RT task running, it can cause RT tasks to
118 		 * throttle when they start up. Kick the timer right away
119 		 * to update the period.
120 		 */
121 		hrtimer_forward_now(&rt_b->rt_period_timer, ns_to_ktime(0));
122 		hrtimer_start_expires(&rt_b->rt_period_timer,
123 				      HRTIMER_MODE_ABS_PINNED_HARD);
124 	}
125 	raw_spin_unlock(&rt_b->rt_runtime_lock);
126 }
127 
start_rt_bandwidth(struct rt_bandwidth * rt_b)128 static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
129 {
130 	if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
131 		return;
132 
133 	do_start_rt_bandwidth(rt_b);
134 }
135 
init_rt_rq(struct rt_rq * rt_rq)136 void init_rt_rq(struct rt_rq *rt_rq)
137 {
138 	struct rt_prio_array *array;
139 	int i;
140 
141 	array = &rt_rq->active;
142 	for (i = 0; i < MAX_RT_PRIO; i++) {
143 		INIT_LIST_HEAD(array->queue + i);
144 		__clear_bit(i, array->bitmap);
145 	}
146 	/* delimiter for bitsearch: */
147 	__set_bit(MAX_RT_PRIO, array->bitmap);
148 
149 #if defined CONFIG_SMP
150 	rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
151 	rt_rq->highest_prio.next = MAX_RT_PRIO-1;
152 	rt_rq->rt_nr_migratory = 0;
153 	rt_rq->overloaded = 0;
154 	plist_head_init(&rt_rq->pushable_tasks);
155 #endif /* CONFIG_SMP */
156 	/* We start is dequeued state, because no RT tasks are queued */
157 	rt_rq->rt_queued = 0;
158 
159 	rt_rq->rt_time = 0;
160 	rt_rq->rt_throttled = 0;
161 	rt_rq->rt_runtime = 0;
162 	raw_spin_lock_init(&rt_rq->rt_runtime_lock);
163 }
164 
165 #ifdef CONFIG_RT_GROUP_SCHED
destroy_rt_bandwidth(struct rt_bandwidth * rt_b)166 static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
167 {
168 	hrtimer_cancel(&rt_b->rt_period_timer);
169 }
170 
171 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
172 
rt_task_of(struct sched_rt_entity * rt_se)173 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
174 {
175 #ifdef CONFIG_SCHED_DEBUG
176 	WARN_ON_ONCE(!rt_entity_is_task(rt_se));
177 #endif
178 	return container_of(rt_se, struct task_struct, rt);
179 }
180 
rq_of_rt_rq(struct rt_rq * rt_rq)181 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
182 {
183 	return rt_rq->rq;
184 }
185 
rt_rq_of_se(struct sched_rt_entity * rt_se)186 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
187 {
188 	return rt_se->rt_rq;
189 }
190 
rq_of_rt_se(struct sched_rt_entity * rt_se)191 static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
192 {
193 	struct rt_rq *rt_rq = rt_se->rt_rq;
194 
195 	return rt_rq->rq;
196 }
197 
unregister_rt_sched_group(struct task_group * tg)198 void unregister_rt_sched_group(struct task_group *tg)
199 {
200 	if (tg->rt_se)
201 		destroy_rt_bandwidth(&tg->rt_bandwidth);
202 
203 }
204 
free_rt_sched_group(struct task_group * tg)205 void free_rt_sched_group(struct task_group *tg)
206 {
207 	int i;
208 
209 	for_each_possible_cpu(i) {
210 		if (tg->rt_rq)
211 			kfree(tg->rt_rq[i]);
212 		if (tg->rt_se)
213 			kfree(tg->rt_se[i]);
214 	}
215 
216 	kfree(tg->rt_rq);
217 	kfree(tg->rt_se);
218 }
219 
init_tg_rt_entry(struct task_group * tg,struct rt_rq * rt_rq,struct sched_rt_entity * rt_se,int cpu,struct sched_rt_entity * parent)220 void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
221 		struct sched_rt_entity *rt_se, int cpu,
222 		struct sched_rt_entity *parent)
223 {
224 	struct rq *rq = cpu_rq(cpu);
225 
226 	rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
227 	rt_rq->rt_nr_boosted = 0;
228 	rt_rq->rq = rq;
229 	rt_rq->tg = tg;
230 
231 	tg->rt_rq[cpu] = rt_rq;
232 	tg->rt_se[cpu] = rt_se;
233 
234 	if (!rt_se)
235 		return;
236 
237 	if (!parent)
238 		rt_se->rt_rq = &rq->rt;
239 	else
240 		rt_se->rt_rq = parent->my_q;
241 
242 	rt_se->my_q = rt_rq;
243 	rt_se->parent = parent;
244 	INIT_LIST_HEAD(&rt_se->run_list);
245 }
246 
alloc_rt_sched_group(struct task_group * tg,struct task_group * parent)247 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
248 {
249 	struct rt_rq *rt_rq;
250 	struct sched_rt_entity *rt_se;
251 	int i;
252 
253 	tg->rt_rq = kcalloc(nr_cpu_ids, sizeof(rt_rq), GFP_KERNEL);
254 	if (!tg->rt_rq)
255 		goto err;
256 	tg->rt_se = kcalloc(nr_cpu_ids, sizeof(rt_se), GFP_KERNEL);
257 	if (!tg->rt_se)
258 		goto err;
259 
260 	init_rt_bandwidth(&tg->rt_bandwidth,
261 			ktime_to_ns(def_rt_bandwidth.rt_period), 0);
262 
263 	for_each_possible_cpu(i) {
264 		rt_rq = kzalloc_node(sizeof(struct rt_rq),
265 				     GFP_KERNEL, cpu_to_node(i));
266 		if (!rt_rq)
267 			goto err;
268 
269 		rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
270 				     GFP_KERNEL, cpu_to_node(i));
271 		if (!rt_se)
272 			goto err_free_rq;
273 
274 		init_rt_rq(rt_rq);
275 		rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
276 		init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
277 	}
278 
279 	return 1;
280 
281 err_free_rq:
282 	kfree(rt_rq);
283 err:
284 	return 0;
285 }
286 
287 #else /* CONFIG_RT_GROUP_SCHED */
288 
289 #define rt_entity_is_task(rt_se) (1)
290 
rt_task_of(struct sched_rt_entity * rt_se)291 static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
292 {
293 	return container_of(rt_se, struct task_struct, rt);
294 }
295 
rq_of_rt_rq(struct rt_rq * rt_rq)296 static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
297 {
298 	return container_of(rt_rq, struct rq, rt);
299 }
300 
rq_of_rt_se(struct sched_rt_entity * rt_se)301 static inline struct rq *rq_of_rt_se(struct sched_rt_entity *rt_se)
302 {
303 	struct task_struct *p = rt_task_of(rt_se);
304 
305 	return task_rq(p);
306 }
307 
rt_rq_of_se(struct sched_rt_entity * rt_se)308 static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
309 {
310 	struct rq *rq = rq_of_rt_se(rt_se);
311 
312 	return &rq->rt;
313 }
314 
unregister_rt_sched_group(struct task_group * tg)315 void unregister_rt_sched_group(struct task_group *tg) { }
316 
free_rt_sched_group(struct task_group * tg)317 void free_rt_sched_group(struct task_group *tg) { }
318 
alloc_rt_sched_group(struct task_group * tg,struct task_group * parent)319 int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
320 {
321 	return 1;
322 }
323 #endif /* CONFIG_RT_GROUP_SCHED */
324 
325 #ifdef CONFIG_SMP
326 
need_pull_rt_task(struct rq * rq,struct task_struct * prev)327 static inline bool need_pull_rt_task(struct rq *rq, struct task_struct *prev)
328 {
329 	/* Try to pull RT tasks here if we lower this rq's prio */
330 	return rq->online && rq->rt.highest_prio.curr > prev->prio;
331 }
332 
rt_overloaded(struct rq * rq)333 static inline int rt_overloaded(struct rq *rq)
334 {
335 	return atomic_read(&rq->rd->rto_count);
336 }
337 
rt_set_overload(struct rq * rq)338 static inline void rt_set_overload(struct rq *rq)
339 {
340 	if (!rq->online)
341 		return;
342 
343 	cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
344 	/*
345 	 * Make sure the mask is visible before we set
346 	 * the overload count. That is checked to determine
347 	 * if we should look at the mask. It would be a shame
348 	 * if we looked at the mask, but the mask was not
349 	 * updated yet.
350 	 *
351 	 * Matched by the barrier in pull_rt_task().
352 	 */
353 	smp_wmb();
354 	atomic_inc(&rq->rd->rto_count);
355 }
356 
rt_clear_overload(struct rq * rq)357 static inline void rt_clear_overload(struct rq *rq)
358 {
359 	if (!rq->online)
360 		return;
361 
362 	/* the order here really doesn't matter */
363 	atomic_dec(&rq->rd->rto_count);
364 	cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
365 }
366 
update_rt_migration(struct rt_rq * rt_rq)367 static void update_rt_migration(struct rt_rq *rt_rq)
368 {
369 	if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
370 		if (!rt_rq->overloaded) {
371 			rt_set_overload(rq_of_rt_rq(rt_rq));
372 			rt_rq->overloaded = 1;
373 		}
374 	} else if (rt_rq->overloaded) {
375 		rt_clear_overload(rq_of_rt_rq(rt_rq));
376 		rt_rq->overloaded = 0;
377 	}
378 }
379 
inc_rt_migration(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)380 static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
381 {
382 	struct task_struct *p;
383 
384 	if (!rt_entity_is_task(rt_se))
385 		return;
386 
387 	p = rt_task_of(rt_se);
388 	rt_rq = &rq_of_rt_rq(rt_rq)->rt;
389 
390 	rt_rq->rt_nr_total++;
391 	if (p->nr_cpus_allowed > 1)
392 		rt_rq->rt_nr_migratory++;
393 
394 	update_rt_migration(rt_rq);
395 }
396 
dec_rt_migration(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)397 static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
398 {
399 	struct task_struct *p;
400 
401 	if (!rt_entity_is_task(rt_se))
402 		return;
403 
404 	p = rt_task_of(rt_se);
405 	rt_rq = &rq_of_rt_rq(rt_rq)->rt;
406 
407 	rt_rq->rt_nr_total--;
408 	if (p->nr_cpus_allowed > 1)
409 		rt_rq->rt_nr_migratory--;
410 
411 	update_rt_migration(rt_rq);
412 }
413 
has_pushable_tasks(struct rq * rq)414 static inline int has_pushable_tasks(struct rq *rq)
415 {
416 	return !plist_head_empty(&rq->rt.pushable_tasks);
417 }
418 
419 static DEFINE_PER_CPU(struct balance_callback, rt_push_head);
420 static DEFINE_PER_CPU(struct balance_callback, rt_pull_head);
421 
422 static void push_rt_tasks(struct rq *);
423 static void pull_rt_task(struct rq *);
424 
rt_queue_push_tasks(struct rq * rq)425 static inline void rt_queue_push_tasks(struct rq *rq)
426 {
427 	if (!has_pushable_tasks(rq))
428 		return;
429 
430 	queue_balance_callback(rq, &per_cpu(rt_push_head, rq->cpu), push_rt_tasks);
431 }
432 
rt_queue_pull_task(struct rq * rq)433 static inline void rt_queue_pull_task(struct rq *rq)
434 {
435 	queue_balance_callback(rq, &per_cpu(rt_pull_head, rq->cpu), pull_rt_task);
436 }
437 
enqueue_pushable_task(struct rq * rq,struct task_struct * p)438 static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
439 {
440 	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
441 	plist_node_init(&p->pushable_tasks, p->prio);
442 	plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
443 
444 	/* Update the highest prio pushable task */
445 	if (p->prio < rq->rt.highest_prio.next)
446 		rq->rt.highest_prio.next = p->prio;
447 }
448 
dequeue_pushable_task(struct rq * rq,struct task_struct * p)449 static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
450 {
451 	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
452 
453 	/* Update the new highest prio pushable task */
454 	if (has_pushable_tasks(rq)) {
455 		p = plist_first_entry(&rq->rt.pushable_tasks,
456 				      struct task_struct, pushable_tasks);
457 		rq->rt.highest_prio.next = p->prio;
458 	} else {
459 		rq->rt.highest_prio.next = MAX_RT_PRIO-1;
460 	}
461 }
462 
463 #else
464 
enqueue_pushable_task(struct rq * rq,struct task_struct * p)465 static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
466 {
467 }
468 
dequeue_pushable_task(struct rq * rq,struct task_struct * p)469 static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
470 {
471 }
472 
473 static inline
inc_rt_migration(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)474 void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
475 {
476 }
477 
478 static inline
dec_rt_migration(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)479 void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
480 {
481 }
482 
rt_queue_push_tasks(struct rq * rq)483 static inline void rt_queue_push_tasks(struct rq *rq)
484 {
485 }
486 #endif /* CONFIG_SMP */
487 
488 static void enqueue_top_rt_rq(struct rt_rq *rt_rq);
489 static void dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count);
490 
on_rt_rq(struct sched_rt_entity * rt_se)491 static inline int on_rt_rq(struct sched_rt_entity *rt_se)
492 {
493 	return rt_se->on_rq;
494 }
495 
496 #ifdef CONFIG_UCLAMP_TASK
497 /*
498  * Verify the fitness of task @p to run on @cpu taking into account the uclamp
499  * settings.
500  *
501  * This check is only important for heterogeneous systems where uclamp_min value
502  * is higher than the capacity of a @cpu. For non-heterogeneous system this
503  * function will always return true.
504  *
505  * The function will return true if the capacity of the @cpu is >= the
506  * uclamp_min and false otherwise.
507  *
508  * Note that uclamp_min will be clamped to uclamp_max if uclamp_min
509  * > uclamp_max.
510  */
rt_task_fits_capacity(struct task_struct * p,int cpu)511 static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
512 {
513 	unsigned int min_cap;
514 	unsigned int max_cap;
515 	unsigned int cpu_cap;
516 
517 	/* Only heterogeneous systems can benefit from this check */
518 	if (!sched_asym_cpucap_active())
519 		return true;
520 
521 	min_cap = uclamp_eff_value(p, UCLAMP_MIN);
522 	max_cap = uclamp_eff_value(p, UCLAMP_MAX);
523 
524 	cpu_cap = capacity_orig_of(cpu);
525 
526 	return cpu_cap >= min(min_cap, max_cap);
527 }
528 #else
rt_task_fits_capacity(struct task_struct * p,int cpu)529 static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
530 {
531 	return true;
532 }
533 #endif
534 
535 #ifdef CONFIG_RT_GROUP_SCHED
536 
sched_rt_runtime(struct rt_rq * rt_rq)537 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
538 {
539 	if (!rt_rq->tg)
540 		return RUNTIME_INF;
541 
542 	return rt_rq->rt_runtime;
543 }
544 
sched_rt_period(struct rt_rq * rt_rq)545 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
546 {
547 	return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
548 }
549 
550 typedef struct task_group *rt_rq_iter_t;
551 
next_task_group(struct task_group * tg)552 static inline struct task_group *next_task_group(struct task_group *tg)
553 {
554 	do {
555 		tg = list_entry_rcu(tg->list.next,
556 			typeof(struct task_group), list);
557 	} while (&tg->list != &task_groups && task_group_is_autogroup(tg));
558 
559 	if (&tg->list == &task_groups)
560 		tg = NULL;
561 
562 	return tg;
563 }
564 
565 #define for_each_rt_rq(rt_rq, iter, rq)					\
566 	for (iter = container_of(&task_groups, typeof(*iter), list);	\
567 		(iter = next_task_group(iter)) &&			\
568 		(rt_rq = iter->rt_rq[cpu_of(rq)]);)
569 
570 #define for_each_sched_rt_entity(rt_se) \
571 	for (; rt_se; rt_se = rt_se->parent)
572 
group_rt_rq(struct sched_rt_entity * rt_se)573 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
574 {
575 	return rt_se->my_q;
576 }
577 
578 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
579 static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags);
580 
sched_rt_rq_enqueue(struct rt_rq * rt_rq)581 static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
582 {
583 	struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
584 	struct rq *rq = rq_of_rt_rq(rt_rq);
585 	struct sched_rt_entity *rt_se;
586 
587 	int cpu = cpu_of(rq);
588 
589 	rt_se = rt_rq->tg->rt_se[cpu];
590 
591 	if (rt_rq->rt_nr_running) {
592 		if (!rt_se)
593 			enqueue_top_rt_rq(rt_rq);
594 		else if (!on_rt_rq(rt_se))
595 			enqueue_rt_entity(rt_se, 0);
596 
597 		if (rt_rq->highest_prio.curr < curr->prio)
598 			resched_curr(rq);
599 	}
600 }
601 
sched_rt_rq_dequeue(struct rt_rq * rt_rq)602 static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
603 {
604 	struct sched_rt_entity *rt_se;
605 	int cpu = cpu_of(rq_of_rt_rq(rt_rq));
606 
607 	rt_se = rt_rq->tg->rt_se[cpu];
608 
609 	if (!rt_se) {
610 		dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
611 		/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
612 		cpufreq_update_util(rq_of_rt_rq(rt_rq), 0);
613 	}
614 	else if (on_rt_rq(rt_se))
615 		dequeue_rt_entity(rt_se, 0);
616 }
617 
rt_rq_throttled(struct rt_rq * rt_rq)618 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
619 {
620 	return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
621 }
622 
rt_se_boosted(struct sched_rt_entity * rt_se)623 static int rt_se_boosted(struct sched_rt_entity *rt_se)
624 {
625 	struct rt_rq *rt_rq = group_rt_rq(rt_se);
626 	struct task_struct *p;
627 
628 	if (rt_rq)
629 		return !!rt_rq->rt_nr_boosted;
630 
631 	p = rt_task_of(rt_se);
632 	return p->prio != p->normal_prio;
633 }
634 
635 #ifdef CONFIG_SMP
sched_rt_period_mask(void)636 static inline const struct cpumask *sched_rt_period_mask(void)
637 {
638 	return this_rq()->rd->span;
639 }
640 #else
sched_rt_period_mask(void)641 static inline const struct cpumask *sched_rt_period_mask(void)
642 {
643 	return cpu_online_mask;
644 }
645 #endif
646 
647 static inline
sched_rt_period_rt_rq(struct rt_bandwidth * rt_b,int cpu)648 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
649 {
650 	return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
651 }
652 
sched_rt_bandwidth(struct rt_rq * rt_rq)653 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
654 {
655 	return &rt_rq->tg->rt_bandwidth;
656 }
657 
658 #else /* !CONFIG_RT_GROUP_SCHED */
659 
sched_rt_runtime(struct rt_rq * rt_rq)660 static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
661 {
662 	return rt_rq->rt_runtime;
663 }
664 
sched_rt_period(struct rt_rq * rt_rq)665 static inline u64 sched_rt_period(struct rt_rq *rt_rq)
666 {
667 	return ktime_to_ns(def_rt_bandwidth.rt_period);
668 }
669 
670 typedef struct rt_rq *rt_rq_iter_t;
671 
672 #define for_each_rt_rq(rt_rq, iter, rq) \
673 	for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
674 
675 #define for_each_sched_rt_entity(rt_se) \
676 	for (; rt_se; rt_se = NULL)
677 
group_rt_rq(struct sched_rt_entity * rt_se)678 static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
679 {
680 	return NULL;
681 }
682 
sched_rt_rq_enqueue(struct rt_rq * rt_rq)683 static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
684 {
685 	struct rq *rq = rq_of_rt_rq(rt_rq);
686 
687 	if (!rt_rq->rt_nr_running)
688 		return;
689 
690 	enqueue_top_rt_rq(rt_rq);
691 	resched_curr(rq);
692 }
693 
sched_rt_rq_dequeue(struct rt_rq * rt_rq)694 static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
695 {
696 	dequeue_top_rt_rq(rt_rq, rt_rq->rt_nr_running);
697 }
698 
rt_rq_throttled(struct rt_rq * rt_rq)699 static inline int rt_rq_throttled(struct rt_rq *rt_rq)
700 {
701 	return rt_rq->rt_throttled;
702 }
703 
sched_rt_period_mask(void)704 static inline const struct cpumask *sched_rt_period_mask(void)
705 {
706 	return cpu_online_mask;
707 }
708 
709 static inline
sched_rt_period_rt_rq(struct rt_bandwidth * rt_b,int cpu)710 struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
711 {
712 	return &cpu_rq(cpu)->rt;
713 }
714 
sched_rt_bandwidth(struct rt_rq * rt_rq)715 static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
716 {
717 	return &def_rt_bandwidth;
718 }
719 
720 #endif /* CONFIG_RT_GROUP_SCHED */
721 
sched_rt_bandwidth_account(struct rt_rq * rt_rq)722 bool sched_rt_bandwidth_account(struct rt_rq *rt_rq)
723 {
724 	struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
725 
726 	return (hrtimer_active(&rt_b->rt_period_timer) ||
727 		rt_rq->rt_time < rt_b->rt_runtime);
728 }
729 
730 #ifdef CONFIG_SMP
731 /*
732  * We ran out of runtime, see if we can borrow some from our neighbours.
733  */
do_balance_runtime(struct rt_rq * rt_rq)734 static void do_balance_runtime(struct rt_rq *rt_rq)
735 {
736 	struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
737 	struct root_domain *rd = rq_of_rt_rq(rt_rq)->rd;
738 	int i, weight;
739 	u64 rt_period;
740 
741 	weight = cpumask_weight(rd->span);
742 
743 	raw_spin_lock(&rt_b->rt_runtime_lock);
744 	rt_period = ktime_to_ns(rt_b->rt_period);
745 	for_each_cpu(i, rd->span) {
746 		struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
747 		s64 diff;
748 
749 		if (iter == rt_rq)
750 			continue;
751 
752 		raw_spin_lock(&iter->rt_runtime_lock);
753 		/*
754 		 * Either all rqs have inf runtime and there's nothing to steal
755 		 * or __disable_runtime() below sets a specific rq to inf to
756 		 * indicate its been disabled and disallow stealing.
757 		 */
758 		if (iter->rt_runtime == RUNTIME_INF)
759 			goto next;
760 
761 		/*
762 		 * From runqueues with spare time, take 1/n part of their
763 		 * spare time, but no more than our period.
764 		 */
765 		diff = iter->rt_runtime - iter->rt_time;
766 		if (diff > 0) {
767 			diff = div_u64((u64)diff, weight);
768 			if (rt_rq->rt_runtime + diff > rt_period)
769 				diff = rt_period - rt_rq->rt_runtime;
770 			iter->rt_runtime -= diff;
771 			rt_rq->rt_runtime += diff;
772 			if (rt_rq->rt_runtime == rt_period) {
773 				raw_spin_unlock(&iter->rt_runtime_lock);
774 				break;
775 			}
776 		}
777 next:
778 		raw_spin_unlock(&iter->rt_runtime_lock);
779 	}
780 	raw_spin_unlock(&rt_b->rt_runtime_lock);
781 }
782 
783 /*
784  * Ensure this RQ takes back all the runtime it lend to its neighbours.
785  */
__disable_runtime(struct rq * rq)786 static void __disable_runtime(struct rq *rq)
787 {
788 	struct root_domain *rd = rq->rd;
789 	rt_rq_iter_t iter;
790 	struct rt_rq *rt_rq;
791 
792 	if (unlikely(!scheduler_running))
793 		return;
794 
795 	for_each_rt_rq(rt_rq, iter, rq) {
796 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
797 		s64 want;
798 		int i;
799 
800 		raw_spin_lock(&rt_b->rt_runtime_lock);
801 		raw_spin_lock(&rt_rq->rt_runtime_lock);
802 		/*
803 		 * Either we're all inf and nobody needs to borrow, or we're
804 		 * already disabled and thus have nothing to do, or we have
805 		 * exactly the right amount of runtime to take out.
806 		 */
807 		if (rt_rq->rt_runtime == RUNTIME_INF ||
808 				rt_rq->rt_runtime == rt_b->rt_runtime)
809 			goto balanced;
810 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
811 
812 		/*
813 		 * Calculate the difference between what we started out with
814 		 * and what we current have, that's the amount of runtime
815 		 * we lend and now have to reclaim.
816 		 */
817 		want = rt_b->rt_runtime - rt_rq->rt_runtime;
818 
819 		/*
820 		 * Greedy reclaim, take back as much as we can.
821 		 */
822 		for_each_cpu(i, rd->span) {
823 			struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
824 			s64 diff;
825 
826 			/*
827 			 * Can't reclaim from ourselves or disabled runqueues.
828 			 */
829 			if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
830 				continue;
831 
832 			raw_spin_lock(&iter->rt_runtime_lock);
833 			if (want > 0) {
834 				diff = min_t(s64, iter->rt_runtime, want);
835 				iter->rt_runtime -= diff;
836 				want -= diff;
837 			} else {
838 				iter->rt_runtime -= want;
839 				want -= want;
840 			}
841 			raw_spin_unlock(&iter->rt_runtime_lock);
842 
843 			if (!want)
844 				break;
845 		}
846 
847 		raw_spin_lock(&rt_rq->rt_runtime_lock);
848 		/*
849 		 * We cannot be left wanting - that would mean some runtime
850 		 * leaked out of the system.
851 		 */
852 		WARN_ON_ONCE(want);
853 balanced:
854 		/*
855 		 * Disable all the borrow logic by pretending we have inf
856 		 * runtime - in which case borrowing doesn't make sense.
857 		 */
858 		rt_rq->rt_runtime = RUNTIME_INF;
859 		rt_rq->rt_throttled = 0;
860 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
861 		raw_spin_unlock(&rt_b->rt_runtime_lock);
862 
863 		/* Make rt_rq available for pick_next_task() */
864 		sched_rt_rq_enqueue(rt_rq);
865 	}
866 }
867 
__enable_runtime(struct rq * rq)868 static void __enable_runtime(struct rq *rq)
869 {
870 	rt_rq_iter_t iter;
871 	struct rt_rq *rt_rq;
872 
873 	if (unlikely(!scheduler_running))
874 		return;
875 
876 	/*
877 	 * Reset each runqueue's bandwidth settings
878 	 */
879 	for_each_rt_rq(rt_rq, iter, rq) {
880 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
881 
882 		raw_spin_lock(&rt_b->rt_runtime_lock);
883 		raw_spin_lock(&rt_rq->rt_runtime_lock);
884 		rt_rq->rt_runtime = rt_b->rt_runtime;
885 		rt_rq->rt_time = 0;
886 		rt_rq->rt_throttled = 0;
887 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
888 		raw_spin_unlock(&rt_b->rt_runtime_lock);
889 	}
890 }
891 
balance_runtime(struct rt_rq * rt_rq)892 static void balance_runtime(struct rt_rq *rt_rq)
893 {
894 	if (!sched_feat(RT_RUNTIME_SHARE))
895 		return;
896 
897 	if (rt_rq->rt_time > rt_rq->rt_runtime) {
898 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
899 		do_balance_runtime(rt_rq);
900 		raw_spin_lock(&rt_rq->rt_runtime_lock);
901 	}
902 }
903 #else /* !CONFIG_SMP */
balance_runtime(struct rt_rq * rt_rq)904 static inline void balance_runtime(struct rt_rq *rt_rq) {}
905 #endif /* CONFIG_SMP */
906 
do_sched_rt_period_timer(struct rt_bandwidth * rt_b,int overrun)907 static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
908 {
909 	int i, idle = 1, throttled = 0;
910 	const struct cpumask *span;
911 
912 	span = sched_rt_period_mask();
913 #ifdef CONFIG_RT_GROUP_SCHED
914 	/*
915 	 * FIXME: isolated CPUs should really leave the root task group,
916 	 * whether they are isolcpus or were isolated via cpusets, lest
917 	 * the timer run on a CPU which does not service all runqueues,
918 	 * potentially leaving other CPUs indefinitely throttled.  If
919 	 * isolation is really required, the user will turn the throttle
920 	 * off to kill the perturbations it causes anyway.  Meanwhile,
921 	 * this maintains functionality for boot and/or troubleshooting.
922 	 */
923 	if (rt_b == &root_task_group.rt_bandwidth)
924 		span = cpu_online_mask;
925 #endif
926 	for_each_cpu(i, span) {
927 		int enqueue = 0;
928 		struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
929 		struct rq *rq = rq_of_rt_rq(rt_rq);
930 		struct rq_flags rf;
931 		int skip;
932 
933 		/*
934 		 * When span == cpu_online_mask, taking each rq->lock
935 		 * can be time-consuming. Try to avoid it when possible.
936 		 */
937 		raw_spin_lock(&rt_rq->rt_runtime_lock);
938 		if (!sched_feat(RT_RUNTIME_SHARE) && rt_rq->rt_runtime != RUNTIME_INF)
939 			rt_rq->rt_runtime = rt_b->rt_runtime;
940 		skip = !rt_rq->rt_time && !rt_rq->rt_nr_running;
941 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
942 		if (skip)
943 			continue;
944 
945 		rq_lock(rq, &rf);
946 		update_rq_clock(rq);
947 
948 		if (rt_rq->rt_time) {
949 			u64 runtime;
950 
951 			raw_spin_lock(&rt_rq->rt_runtime_lock);
952 			if (rt_rq->rt_throttled)
953 				balance_runtime(rt_rq);
954 			runtime = rt_rq->rt_runtime;
955 			rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
956 			if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
957 				rt_rq->rt_throttled = 0;
958 				enqueue = 1;
959 
960 				/*
961 				 * When we're idle and a woken (rt) task is
962 				 * throttled check_preempt_curr() will set
963 				 * skip_update and the time between the wakeup
964 				 * and this unthrottle will get accounted as
965 				 * 'runtime'.
966 				 */
967 				if (rt_rq->rt_nr_running && rq->curr == rq->idle)
968 					rq_clock_cancel_skipupdate(rq);
969 			}
970 			if (rt_rq->rt_time || rt_rq->rt_nr_running)
971 				idle = 0;
972 			raw_spin_unlock(&rt_rq->rt_runtime_lock);
973 		} else if (rt_rq->rt_nr_running) {
974 			idle = 0;
975 			if (!rt_rq_throttled(rt_rq))
976 				enqueue = 1;
977 		}
978 		if (rt_rq->rt_throttled)
979 			throttled = 1;
980 
981 		if (enqueue)
982 			sched_rt_rq_enqueue(rt_rq);
983 		rq_unlock(rq, &rf);
984 	}
985 
986 	if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
987 		return 1;
988 
989 	return idle;
990 }
991 
rt_se_prio(struct sched_rt_entity * rt_se)992 static inline int rt_se_prio(struct sched_rt_entity *rt_se)
993 {
994 #ifdef CONFIG_RT_GROUP_SCHED
995 	struct rt_rq *rt_rq = group_rt_rq(rt_se);
996 
997 	if (rt_rq)
998 		return rt_rq->highest_prio.curr;
999 #endif
1000 
1001 	return rt_task_of(rt_se)->prio;
1002 }
1003 
sched_rt_runtime_exceeded(struct rt_rq * rt_rq)1004 static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
1005 {
1006 	u64 runtime = sched_rt_runtime(rt_rq);
1007 
1008 	if (rt_rq->rt_throttled)
1009 		return rt_rq_throttled(rt_rq);
1010 
1011 	if (runtime >= sched_rt_period(rt_rq))
1012 		return 0;
1013 
1014 	balance_runtime(rt_rq);
1015 	runtime = sched_rt_runtime(rt_rq);
1016 	if (runtime == RUNTIME_INF)
1017 		return 0;
1018 
1019 	if (rt_rq->rt_time > runtime) {
1020 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
1021 
1022 		/*
1023 		 * Don't actually throttle groups that have no runtime assigned
1024 		 * but accrue some time due to boosting.
1025 		 */
1026 		if (likely(rt_b->rt_runtime)) {
1027 			rt_rq->rt_throttled = 1;
1028 			printk_deferred_once("sched: RT throttling activated\n");
1029 
1030 			trace_android_vh_dump_throttled_rt_tasks(
1031 				raw_smp_processor_id(),
1032 				rq_clock(rq_of_rt_rq(rt_rq)),
1033 				sched_rt_period(rt_rq),
1034 				runtime,
1035 				hrtimer_get_expires_ns(&rt_b->rt_period_timer));
1036 		} else {
1037 			/*
1038 			 * In case we did anyway, make it go away,
1039 			 * replenishment is a joke, since it will replenish us
1040 			 * with exactly 0 ns.
1041 			 */
1042 			rt_rq->rt_time = 0;
1043 		}
1044 
1045 		if (rt_rq_throttled(rt_rq)) {
1046 			sched_rt_rq_dequeue(rt_rq);
1047 			return 1;
1048 		}
1049 	}
1050 
1051 	return 0;
1052 }
1053 
1054 /*
1055  * Update the current task's runtime statistics. Skip current tasks that
1056  * are not in our scheduling class.
1057  */
update_curr_rt(struct rq * rq)1058 static void update_curr_rt(struct rq *rq)
1059 {
1060 	struct task_struct *curr = rq->curr;
1061 	struct sched_rt_entity *rt_se = &curr->rt;
1062 	u64 delta_exec;
1063 	u64 now;
1064 
1065 	if (curr->sched_class != &rt_sched_class)
1066 		return;
1067 
1068 	now = rq_clock_task(rq);
1069 	delta_exec = now - curr->se.exec_start;
1070 	if (unlikely((s64)delta_exec <= 0))
1071 		return;
1072 
1073 	schedstat_set(curr->stats.exec_max,
1074 		      max(curr->stats.exec_max, delta_exec));
1075 
1076 	trace_sched_stat_runtime(curr, delta_exec, 0);
1077 
1078 	update_current_exec_runtime(curr, now, delta_exec);
1079 
1080 	trace_android_vh_sched_stat_runtime_rt(curr, delta_exec);
1081 
1082 	if (!rt_bandwidth_enabled())
1083 		return;
1084 
1085 	for_each_sched_rt_entity(rt_se) {
1086 		struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1087 		int exceeded;
1088 
1089 		if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
1090 			raw_spin_lock(&rt_rq->rt_runtime_lock);
1091 			rt_rq->rt_time += delta_exec;
1092 			exceeded = sched_rt_runtime_exceeded(rt_rq);
1093 			if (exceeded)
1094 				resched_curr(rq);
1095 			raw_spin_unlock(&rt_rq->rt_runtime_lock);
1096 			if (exceeded)
1097 				do_start_rt_bandwidth(sched_rt_bandwidth(rt_rq));
1098 		}
1099 	}
1100 }
1101 
1102 static void
dequeue_top_rt_rq(struct rt_rq * rt_rq,unsigned int count)1103 dequeue_top_rt_rq(struct rt_rq *rt_rq, unsigned int count)
1104 {
1105 	struct rq *rq = rq_of_rt_rq(rt_rq);
1106 
1107 	BUG_ON(&rq->rt != rt_rq);
1108 
1109 	if (!rt_rq->rt_queued)
1110 		return;
1111 
1112 	BUG_ON(!rq->nr_running);
1113 
1114 	sub_nr_running(rq, count);
1115 	rt_rq->rt_queued = 0;
1116 
1117 }
1118 
1119 static void
enqueue_top_rt_rq(struct rt_rq * rt_rq)1120 enqueue_top_rt_rq(struct rt_rq *rt_rq)
1121 {
1122 	struct rq *rq = rq_of_rt_rq(rt_rq);
1123 
1124 	BUG_ON(&rq->rt != rt_rq);
1125 
1126 	if (rt_rq->rt_queued)
1127 		return;
1128 
1129 	if (rt_rq_throttled(rt_rq))
1130 		return;
1131 
1132 	if (rt_rq->rt_nr_running) {
1133 		add_nr_running(rq, rt_rq->rt_nr_running);
1134 		rt_rq->rt_queued = 1;
1135 	}
1136 
1137 	/* Kick cpufreq (see the comment in kernel/sched/sched.h). */
1138 	cpufreq_update_util(rq, 0);
1139 }
1140 
1141 #if defined CONFIG_SMP
1142 
1143 static void
inc_rt_prio_smp(struct rt_rq * rt_rq,int prio,int prev_prio)1144 inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1145 {
1146 	struct rq *rq = rq_of_rt_rq(rt_rq);
1147 
1148 #ifdef CONFIG_RT_GROUP_SCHED
1149 	/*
1150 	 * Change rq's cpupri only if rt_rq is the top queue.
1151 	 */
1152 	if (&rq->rt != rt_rq)
1153 		return;
1154 #endif
1155 	if (rq->online && prio < prev_prio)
1156 		cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
1157 }
1158 
1159 static void
dec_rt_prio_smp(struct rt_rq * rt_rq,int prio,int prev_prio)1160 dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
1161 {
1162 	struct rq *rq = rq_of_rt_rq(rt_rq);
1163 
1164 #ifdef CONFIG_RT_GROUP_SCHED
1165 	/*
1166 	 * Change rq's cpupri only if rt_rq is the top queue.
1167 	 */
1168 	if (&rq->rt != rt_rq)
1169 		return;
1170 #endif
1171 	if (rq->online && rt_rq->highest_prio.curr != prev_prio)
1172 		cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
1173 }
1174 
1175 #else /* CONFIG_SMP */
1176 
1177 static inline
inc_rt_prio_smp(struct rt_rq * rt_rq,int prio,int prev_prio)1178 void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1179 static inline
dec_rt_prio_smp(struct rt_rq * rt_rq,int prio,int prev_prio)1180 void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
1181 
1182 #endif /* CONFIG_SMP */
1183 
1184 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1185 static void
inc_rt_prio(struct rt_rq * rt_rq,int prio)1186 inc_rt_prio(struct rt_rq *rt_rq, int prio)
1187 {
1188 	int prev_prio = rt_rq->highest_prio.curr;
1189 
1190 	if (prio < prev_prio)
1191 		rt_rq->highest_prio.curr = prio;
1192 
1193 	inc_rt_prio_smp(rt_rq, prio, prev_prio);
1194 }
1195 
1196 static void
dec_rt_prio(struct rt_rq * rt_rq,int prio)1197 dec_rt_prio(struct rt_rq *rt_rq, int prio)
1198 {
1199 	int prev_prio = rt_rq->highest_prio.curr;
1200 
1201 	if (rt_rq->rt_nr_running) {
1202 
1203 		WARN_ON(prio < prev_prio);
1204 
1205 		/*
1206 		 * This may have been our highest task, and therefore
1207 		 * we may have some recomputation to do
1208 		 */
1209 		if (prio == prev_prio) {
1210 			struct rt_prio_array *array = &rt_rq->active;
1211 
1212 			rt_rq->highest_prio.curr =
1213 				sched_find_first_bit(array->bitmap);
1214 		}
1215 
1216 	} else {
1217 		rt_rq->highest_prio.curr = MAX_RT_PRIO-1;
1218 	}
1219 
1220 	dec_rt_prio_smp(rt_rq, prio, prev_prio);
1221 }
1222 
1223 #else
1224 
inc_rt_prio(struct rt_rq * rt_rq,int prio)1225 static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {}
dec_rt_prio(struct rt_rq * rt_rq,int prio)1226 static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {}
1227 
1228 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1229 
1230 #ifdef CONFIG_RT_GROUP_SCHED
1231 
1232 static void
inc_rt_group(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)1233 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1234 {
1235 	if (rt_se_boosted(rt_se))
1236 		rt_rq->rt_nr_boosted++;
1237 
1238 	if (rt_rq->tg)
1239 		start_rt_bandwidth(&rt_rq->tg->rt_bandwidth);
1240 }
1241 
1242 static void
dec_rt_group(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)1243 dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1244 {
1245 	if (rt_se_boosted(rt_se))
1246 		rt_rq->rt_nr_boosted--;
1247 
1248 	WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted);
1249 }
1250 
1251 #else /* CONFIG_RT_GROUP_SCHED */
1252 
1253 static void
inc_rt_group(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)1254 inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1255 {
1256 	start_rt_bandwidth(&def_rt_bandwidth);
1257 }
1258 
1259 static inline
dec_rt_group(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)1260 void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {}
1261 
1262 #endif /* CONFIG_RT_GROUP_SCHED */
1263 
1264 static inline
rt_se_nr_running(struct sched_rt_entity * rt_se)1265 unsigned int rt_se_nr_running(struct sched_rt_entity *rt_se)
1266 {
1267 	struct rt_rq *group_rq = group_rt_rq(rt_se);
1268 
1269 	if (group_rq)
1270 		return group_rq->rt_nr_running;
1271 	else
1272 		return 1;
1273 }
1274 
1275 static inline
rt_se_rr_nr_running(struct sched_rt_entity * rt_se)1276 unsigned int rt_se_rr_nr_running(struct sched_rt_entity *rt_se)
1277 {
1278 	struct rt_rq *group_rq = group_rt_rq(rt_se);
1279 	struct task_struct *tsk;
1280 
1281 	if (group_rq)
1282 		return group_rq->rr_nr_running;
1283 
1284 	tsk = rt_task_of(rt_se);
1285 
1286 	return (tsk->policy == SCHED_RR) ? 1 : 0;
1287 }
1288 
1289 static inline
inc_rt_tasks(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)1290 void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1291 {
1292 	int prio = rt_se_prio(rt_se);
1293 
1294 	WARN_ON(!rt_prio(prio));
1295 	rt_rq->rt_nr_running += rt_se_nr_running(rt_se);
1296 	rt_rq->rr_nr_running += rt_se_rr_nr_running(rt_se);
1297 
1298 	inc_rt_prio(rt_rq, prio);
1299 	inc_rt_migration(rt_se, rt_rq);
1300 	inc_rt_group(rt_se, rt_rq);
1301 }
1302 
1303 static inline
dec_rt_tasks(struct sched_rt_entity * rt_se,struct rt_rq * rt_rq)1304 void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
1305 {
1306 	WARN_ON(!rt_prio(rt_se_prio(rt_se)));
1307 	WARN_ON(!rt_rq->rt_nr_running);
1308 	rt_rq->rt_nr_running -= rt_se_nr_running(rt_se);
1309 	rt_rq->rr_nr_running -= rt_se_rr_nr_running(rt_se);
1310 
1311 	dec_rt_prio(rt_rq, rt_se_prio(rt_se));
1312 	dec_rt_migration(rt_se, rt_rq);
1313 	dec_rt_group(rt_se, rt_rq);
1314 }
1315 
1316 /*
1317  * Change rt_se->run_list location unless SAVE && !MOVE
1318  *
1319  * assumes ENQUEUE/DEQUEUE flags match
1320  */
move_entity(unsigned int flags)1321 static inline bool move_entity(unsigned int flags)
1322 {
1323 	if ((flags & (DEQUEUE_SAVE | DEQUEUE_MOVE)) == DEQUEUE_SAVE)
1324 		return false;
1325 
1326 	return true;
1327 }
1328 
__delist_rt_entity(struct sched_rt_entity * rt_se,struct rt_prio_array * array)1329 static void __delist_rt_entity(struct sched_rt_entity *rt_se, struct rt_prio_array *array)
1330 {
1331 	list_del_init(&rt_se->run_list);
1332 
1333 	if (list_empty(array->queue + rt_se_prio(rt_se)))
1334 		__clear_bit(rt_se_prio(rt_se), array->bitmap);
1335 
1336 	rt_se->on_list = 0;
1337 }
1338 
1339 static inline struct sched_statistics *
__schedstats_from_rt_se(struct sched_rt_entity * rt_se)1340 __schedstats_from_rt_se(struct sched_rt_entity *rt_se)
1341 {
1342 #ifdef CONFIG_RT_GROUP_SCHED
1343 	/* schedstats is not supported for rt group. */
1344 	if (!rt_entity_is_task(rt_se))
1345 		return NULL;
1346 #endif
1347 
1348 	return &rt_task_of(rt_se)->stats;
1349 }
1350 
1351 static inline void
update_stats_wait_start_rt(struct rt_rq * rt_rq,struct sched_rt_entity * rt_se)1352 update_stats_wait_start_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
1353 {
1354 	struct sched_statistics *stats;
1355 	struct task_struct *p = NULL;
1356 
1357 	if (!schedstat_enabled())
1358 		return;
1359 
1360 	if (rt_entity_is_task(rt_se))
1361 		p = rt_task_of(rt_se);
1362 
1363 	stats = __schedstats_from_rt_se(rt_se);
1364 	if (!stats)
1365 		return;
1366 
1367 	__update_stats_wait_start(rq_of_rt_rq(rt_rq), p, stats);
1368 }
1369 
1370 static inline void
update_stats_enqueue_sleeper_rt(struct rt_rq * rt_rq,struct sched_rt_entity * rt_se)1371 update_stats_enqueue_sleeper_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
1372 {
1373 	struct sched_statistics *stats;
1374 	struct task_struct *p = NULL;
1375 
1376 	if (!schedstat_enabled())
1377 		return;
1378 
1379 	if (rt_entity_is_task(rt_se))
1380 		p = rt_task_of(rt_se);
1381 
1382 	stats = __schedstats_from_rt_se(rt_se);
1383 	if (!stats)
1384 		return;
1385 
1386 	__update_stats_enqueue_sleeper(rq_of_rt_rq(rt_rq), p, stats);
1387 }
1388 
1389 static inline void
update_stats_enqueue_rt(struct rt_rq * rt_rq,struct sched_rt_entity * rt_se,int flags)1390 update_stats_enqueue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
1391 			int flags)
1392 {
1393 	if (!schedstat_enabled())
1394 		return;
1395 
1396 	if (flags & ENQUEUE_WAKEUP)
1397 		update_stats_enqueue_sleeper_rt(rt_rq, rt_se);
1398 }
1399 
1400 static inline void
update_stats_wait_end_rt(struct rt_rq * rt_rq,struct sched_rt_entity * rt_se)1401 update_stats_wait_end_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se)
1402 {
1403 	struct sched_statistics *stats;
1404 	struct task_struct *p = NULL;
1405 
1406 	if (!schedstat_enabled())
1407 		return;
1408 
1409 	if (rt_entity_is_task(rt_se))
1410 		p = rt_task_of(rt_se);
1411 
1412 	stats = __schedstats_from_rt_se(rt_se);
1413 	if (!stats)
1414 		return;
1415 
1416 	__update_stats_wait_end(rq_of_rt_rq(rt_rq), p, stats);
1417 }
1418 
1419 static inline void
update_stats_dequeue_rt(struct rt_rq * rt_rq,struct sched_rt_entity * rt_se,int flags)1420 update_stats_dequeue_rt(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
1421 			int flags)
1422 {
1423 	struct task_struct *p = NULL;
1424 
1425 	if (!schedstat_enabled())
1426 		return;
1427 
1428 	if (rt_entity_is_task(rt_se))
1429 		p = rt_task_of(rt_se);
1430 
1431 	if ((flags & DEQUEUE_SLEEP) && p) {
1432 		unsigned int state;
1433 
1434 		state = READ_ONCE(p->__state);
1435 		if (state & TASK_INTERRUPTIBLE)
1436 			__schedstat_set(p->stats.sleep_start,
1437 					rq_clock(rq_of_rt_rq(rt_rq)));
1438 
1439 		if (state & TASK_UNINTERRUPTIBLE)
1440 			__schedstat_set(p->stats.block_start,
1441 					rq_clock(rq_of_rt_rq(rt_rq)));
1442 	}
1443 }
1444 
__enqueue_rt_entity(struct sched_rt_entity * rt_se,unsigned int flags)1445 static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1446 {
1447 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1448 	struct rt_prio_array *array = &rt_rq->active;
1449 	struct rt_rq *group_rq = group_rt_rq(rt_se);
1450 	struct list_head *queue = array->queue + rt_se_prio(rt_se);
1451 
1452 	/*
1453 	 * Don't enqueue the group if its throttled, or when empty.
1454 	 * The latter is a consequence of the former when a child group
1455 	 * get throttled and the current group doesn't have any other
1456 	 * active members.
1457 	 */
1458 	if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) {
1459 		if (rt_se->on_list)
1460 			__delist_rt_entity(rt_se, array);
1461 		return;
1462 	}
1463 
1464 	if (move_entity(flags)) {
1465 		WARN_ON_ONCE(rt_se->on_list);
1466 		if (flags & ENQUEUE_HEAD)
1467 			list_add(&rt_se->run_list, queue);
1468 		else
1469 			list_add_tail(&rt_se->run_list, queue);
1470 
1471 		__set_bit(rt_se_prio(rt_se), array->bitmap);
1472 		rt_se->on_list = 1;
1473 	}
1474 	rt_se->on_rq = 1;
1475 
1476 	inc_rt_tasks(rt_se, rt_rq);
1477 }
1478 
__dequeue_rt_entity(struct sched_rt_entity * rt_se,unsigned int flags)1479 static void __dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1480 {
1481 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
1482 	struct rt_prio_array *array = &rt_rq->active;
1483 
1484 	if (move_entity(flags)) {
1485 		WARN_ON_ONCE(!rt_se->on_list);
1486 		__delist_rt_entity(rt_se, array);
1487 	}
1488 	rt_se->on_rq = 0;
1489 
1490 	dec_rt_tasks(rt_se, rt_rq);
1491 }
1492 
1493 /*
1494  * Because the prio of an upper entry depends on the lower
1495  * entries, we must remove entries top - down.
1496  */
dequeue_rt_stack(struct sched_rt_entity * rt_se,unsigned int flags)1497 static void dequeue_rt_stack(struct sched_rt_entity *rt_se, unsigned int flags)
1498 {
1499 	struct sched_rt_entity *back = NULL;
1500 	unsigned int rt_nr_running;
1501 
1502 	for_each_sched_rt_entity(rt_se) {
1503 		rt_se->back = back;
1504 		back = rt_se;
1505 	}
1506 
1507 	rt_nr_running = rt_rq_of_se(back)->rt_nr_running;
1508 
1509 	for (rt_se = back; rt_se; rt_se = rt_se->back) {
1510 		if (on_rt_rq(rt_se))
1511 			__dequeue_rt_entity(rt_se, flags);
1512 	}
1513 
1514 	dequeue_top_rt_rq(rt_rq_of_se(back), rt_nr_running);
1515 }
1516 
enqueue_rt_entity(struct sched_rt_entity * rt_se,unsigned int flags)1517 static void enqueue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1518 {
1519 	struct rq *rq = rq_of_rt_se(rt_se);
1520 
1521 	update_stats_enqueue_rt(rt_rq_of_se(rt_se), rt_se, flags);
1522 
1523 	dequeue_rt_stack(rt_se, flags);
1524 	for_each_sched_rt_entity(rt_se)
1525 		__enqueue_rt_entity(rt_se, flags);
1526 	enqueue_top_rt_rq(&rq->rt);
1527 }
1528 
dequeue_rt_entity(struct sched_rt_entity * rt_se,unsigned int flags)1529 static void dequeue_rt_entity(struct sched_rt_entity *rt_se, unsigned int flags)
1530 {
1531 	struct rq *rq = rq_of_rt_se(rt_se);
1532 
1533 	update_stats_dequeue_rt(rt_rq_of_se(rt_se), rt_se, flags);
1534 
1535 	dequeue_rt_stack(rt_se, flags);
1536 
1537 	for_each_sched_rt_entity(rt_se) {
1538 		struct rt_rq *rt_rq = group_rt_rq(rt_se);
1539 
1540 		if (rt_rq && rt_rq->rt_nr_running)
1541 			__enqueue_rt_entity(rt_se, flags);
1542 	}
1543 	enqueue_top_rt_rq(&rq->rt);
1544 }
1545 
1546 #ifdef CONFIG_SMP
should_honor_rt_sync(struct rq * rq,struct task_struct * p,bool sync)1547 static inline bool should_honor_rt_sync(struct rq *rq, struct task_struct *p,
1548 					bool sync)
1549 {
1550 	/*
1551 	 * If the waker is CFS, then an RT sync wakeup would preempt the waker
1552 	 * and force it to run for a likely small time after the RT wakee is
1553 	 * done. So, only honor RT sync wakeups from RT wakers.
1554 	 */
1555 	return sync && task_has_rt_policy(rq->curr) &&
1556 		p->prio <= rq->rt.highest_prio.next &&
1557 		rq->rt.rt_nr_running <= 2;
1558 }
1559 #else
should_honor_rt_sync(struct rq * rq,struct task_struct * p,bool sync)1560 static inline bool should_honor_rt_sync(struct rq *rq, struct task_struct *p,
1561 					bool sync)
1562 {
1563 	return 0;
1564 }
1565 #endif
1566 
1567 /*
1568  * Adding/removing a task to/from a priority array:
1569  */
1570 static void
enqueue_task_rt(struct rq * rq,struct task_struct * p,int flags)1571 enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1572 {
1573 	struct sched_rt_entity *rt_se = &p->rt;
1574 	bool sync = !!(flags & ENQUEUE_WAKEUP_SYNC);
1575 
1576 	if (flags & ENQUEUE_WAKEUP)
1577 		rt_se->timeout = 0;
1578 
1579 	check_schedstat_required();
1580 	update_stats_wait_start_rt(rt_rq_of_se(rt_se), rt_se);
1581 
1582 	enqueue_rt_entity(rt_se, flags);
1583 
1584 	if (!task_current(rq, p) && p->nr_cpus_allowed > 1 &&
1585 	    !should_honor_rt_sync(rq, p, sync))
1586 		enqueue_pushable_task(rq, p);
1587 }
1588 
dequeue_task_rt(struct rq * rq,struct task_struct * p,int flags)1589 static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags)
1590 {
1591 	struct sched_rt_entity *rt_se = &p->rt;
1592 
1593 	update_curr_rt(rq);
1594 	dequeue_rt_entity(rt_se, flags);
1595 
1596 	dequeue_pushable_task(rq, p);
1597 }
1598 
1599 /*
1600  * Put task to the head or the end of the run list without the overhead of
1601  * dequeue followed by enqueue.
1602  */
1603 static void
requeue_rt_entity(struct rt_rq * rt_rq,struct sched_rt_entity * rt_se,int head)1604 requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head)
1605 {
1606 	if (on_rt_rq(rt_se)) {
1607 		struct rt_prio_array *array = &rt_rq->active;
1608 		struct list_head *queue = array->queue + rt_se_prio(rt_se);
1609 
1610 		if (head)
1611 			list_move(&rt_se->run_list, queue);
1612 		else
1613 			list_move_tail(&rt_se->run_list, queue);
1614 	}
1615 }
1616 
requeue_task_rt(struct rq * rq,struct task_struct * p,int head)1617 static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head)
1618 {
1619 	struct sched_rt_entity *rt_se = &p->rt;
1620 	struct rt_rq *rt_rq;
1621 
1622 	for_each_sched_rt_entity(rt_se) {
1623 		rt_rq = rt_rq_of_se(rt_se);
1624 		requeue_rt_entity(rt_rq, rt_se, head);
1625 	}
1626 }
1627 
yield_task_rt(struct rq * rq)1628 static void yield_task_rt(struct rq *rq)
1629 {
1630 	requeue_task_rt(rq, rq->curr, 0);
1631 }
1632 
1633 #ifdef CONFIG_SMP
1634 static int find_lowest_rq(struct task_struct *task);
1635 
1636 #ifdef CONFIG_RT_SOFTIRQ_AWARE_SCHED
1637 /*
1638  * Return whether the given cpu is currently non-preemptible
1639  * while handling a potentially long softirq, or if the current
1640  * task is likely to block preemptions soon because it is a
1641  * ksoftirq thread that is handling softirqs.
1642  */
cpu_busy_with_softirqs(int cpu)1643 bool cpu_busy_with_softirqs(int cpu)
1644 {
1645 	u32 softirqs = per_cpu(active_softirqs, cpu) |
1646 		       __cpu_softirq_pending(cpu);
1647 
1648 	return softirqs & LONG_SOFTIRQ_MASK;
1649 }
1650 #else
cpu_busy_with_softirqs(int cpu)1651 bool cpu_busy_with_softirqs(int cpu)
1652 {
1653 	return false;
1654 }
1655 #endif /* CONFIG_RT_SOFTIRQ_AWARE_SCHED */
1656 EXPORT_SYMBOL_GPL(cpu_busy_with_softirqs);
1657 
rt_task_fits_cpu(struct task_struct * p,int cpu)1658 static bool rt_task_fits_cpu(struct task_struct *p, int cpu)
1659 {
1660 	return rt_task_fits_capacity(p, cpu) && !cpu_busy_with_softirqs(cpu);
1661 }
1662 
1663 static int
select_task_rq_rt(struct task_struct * p,int cpu,int flags)1664 select_task_rq_rt(struct task_struct *p, int cpu, int flags)
1665 {
1666 	struct task_struct *curr;
1667 	struct rq *rq;
1668 	struct rq *this_cpu_rq;
1669 	bool test;
1670 	int target_cpu = -1;
1671 	bool sync = !!(flags & WF_SYNC);
1672 	int this_cpu;
1673 
1674 	trace_android_rvh_select_task_rq_rt(p, cpu, flags & 0xF,
1675 					flags, &target_cpu);
1676 	if (target_cpu >= 0)
1677 		return target_cpu;
1678 
1679 	/* For anything but wake ups, just return the task_cpu */
1680 	if (!(flags & (WF_TTWU | WF_FORK)))
1681 		goto out;
1682 
1683 	rq = cpu_rq(cpu);
1684 
1685 	rcu_read_lock();
1686 	curr = READ_ONCE(rq->curr); /* unlocked access */
1687 	this_cpu = smp_processor_id();
1688 	this_cpu_rq = cpu_rq(this_cpu);
1689 
1690 	/*
1691 	 * If the current task on @p's runqueue is an RT task, then
1692 	 * try to see if we can wake this RT task up on another
1693 	 * runqueue. Otherwise simply start this RT task
1694 	 * on its current runqueue.
1695 	 *
1696 	 * We want to avoid overloading runqueues. If the woken
1697 	 * task is a higher priority, then it will stay on this CPU
1698 	 * and the lower prio task should be moved to another CPU.
1699 	 * Even though this will probably make the lower prio task
1700 	 * lose its cache, we do not want to bounce a higher task
1701 	 * around just because it gave up its CPU, perhaps for a
1702 	 * lock?
1703 	 *
1704 	 * For equal prio tasks, we just let the scheduler sort it out.
1705 	 *
1706 	 * Otherwise, just let it ride on the affined RQ and the
1707 	 * post-schedule router will push the preempted task away
1708 	 *
1709 	 * This test is optimistic, if we get it wrong the load-balancer
1710 	 * will have to sort it out.
1711 	 *
1712 	 * We use rt_task_fits_cpu() to evaluate if the CPU is busy with
1713 	 * potentially long-running softirq work, as well as take into
1714 	 * account the capacity of the CPU to ensure it fits the
1715 	 * requirement of the task - which is only important on
1716 	 * heterogeneous systems like big.LITTLE.
1717 	 */
1718 	test = curr &&
1719 	       unlikely(rt_task(curr)) &&
1720 	       (curr->nr_cpus_allowed < 2 || curr->prio <= p->prio);
1721 
1722 	/*
1723 	 * Respect the sync flag as long as the task can run on this CPU.
1724 	 */
1725 	if (should_honor_rt_sync(this_cpu_rq, p, sync) &&
1726 	    cpumask_test_cpu(this_cpu, p->cpus_ptr)) {
1727 		cpu = this_cpu;
1728 		goto out_unlock;
1729 	}
1730 
1731 	if (test || !rt_task_fits_cpu(p, cpu)) {
1732 		int target = find_lowest_rq(p);
1733 
1734 		/*
1735 		 * Bail out if we were forcing a migration to find a better
1736 		 * fitting CPU but our search failed.
1737 		 */
1738 		if (!test && target != -1 && !rt_task_fits_cpu(p, target))
1739 			goto out_unlock;
1740 
1741 		/*
1742 		 * Don't bother moving it if the destination CPU is
1743 		 * not running a lower priority task.
1744 		 */
1745 		if (target != -1 &&
1746 		    p->prio < cpu_rq(target)->rt.highest_prio.curr)
1747 			cpu = target;
1748 	}
1749 
1750 out_unlock:
1751 	rcu_read_unlock();
1752 
1753 out:
1754 	return cpu;
1755 }
1756 
check_preempt_equal_prio(struct rq * rq,struct task_struct * p)1757 static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1758 {
1759 	/*
1760 	 * Current can't be migrated, useless to reschedule,
1761 	 * let's hope p can move out.
1762 	 */
1763 	if (rq->curr->nr_cpus_allowed == 1 ||
1764 	    !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1765 		return;
1766 
1767 	/*
1768 	 * p is migratable, so let's not schedule it and
1769 	 * see if it is pushed or pulled somewhere else.
1770 	 */
1771 	if (p->nr_cpus_allowed != 1 &&
1772 	    cpupri_find(&rq->rd->cpupri, p, NULL))
1773 		return;
1774 
1775 	/*
1776 	 * There appear to be other CPUs that can accept
1777 	 * the current task but none can run 'p', so lets reschedule
1778 	 * to try and push the current task away:
1779 	 */
1780 	requeue_task_rt(rq, p, 1);
1781 	resched_curr(rq);
1782 }
1783 
balance_rt(struct rq * rq,struct task_struct * p,struct rq_flags * rf)1784 static int balance_rt(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1785 {
1786 	if (!on_rt_rq(&p->rt) && need_pull_rt_task(rq, p)) {
1787 		int done = 0;
1788 
1789 		/*
1790 		 * This is OK, because current is on_cpu, which avoids it being
1791 		 * picked for load-balance and preemption/IRQs are still
1792 		 * disabled avoiding further scheduler activity on it and we've
1793 		 * not yet started the picking loop.
1794 		 */
1795 		rq_unpin_lock(rq, rf);
1796 		trace_android_rvh_sched_balance_rt(rq, p, &done);
1797 		if (!done)
1798 			pull_rt_task(rq);
1799 		rq_repin_lock(rq, rf);
1800 	}
1801 
1802 	return sched_stop_runnable(rq) || sched_dl_runnable(rq) || sched_rt_runnable(rq);
1803 }
1804 #endif /* CONFIG_SMP */
1805 
1806 /*
1807  * Preempt the current task with a newly woken task if needed:
1808  */
check_preempt_curr_rt(struct rq * rq,struct task_struct * p,int flags)1809 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
1810 {
1811 	if (p->prio < rq->curr->prio) {
1812 		resched_curr(rq);
1813 		return;
1814 	}
1815 
1816 #ifdef CONFIG_SMP
1817 	/*
1818 	 * If:
1819 	 *
1820 	 * - the newly woken task is of equal priority to the current task
1821 	 * - the newly woken task is non-migratable while current is migratable
1822 	 * - current will be preempted on the next reschedule
1823 	 *
1824 	 * we should check to see if current can readily move to a different
1825 	 * cpu.  If so, we will reschedule to allow the push logic to try
1826 	 * to move current somewhere else, making room for our non-migratable
1827 	 * task.
1828 	 */
1829 	if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
1830 		check_preempt_equal_prio(rq, p);
1831 #endif
1832 }
1833 
set_next_task_rt(struct rq * rq,struct task_struct * p,bool first)1834 static inline void set_next_task_rt(struct rq *rq, struct task_struct *p, bool first)
1835 {
1836 	struct sched_rt_entity *rt_se = &p->rt;
1837 	struct rt_rq *rt_rq = &rq->rt;
1838 
1839 	p->se.exec_start = rq_clock_task(rq);
1840 	if (on_rt_rq(&p->rt))
1841 		update_stats_wait_end_rt(rt_rq, rt_se);
1842 
1843 	/* The running task is never eligible for pushing */
1844 	dequeue_pushable_task(rq, p);
1845 
1846 	if (!first)
1847 		return;
1848 
1849 	/*
1850 	 * If prev task was rt, put_prev_task() has already updated the
1851 	 * utilization. We only care of the case where we start to schedule a
1852 	 * rt task
1853 	 */
1854 	if (rq->curr->sched_class != &rt_sched_class)
1855 		update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
1856 	trace_android_rvh_update_rt_rq_load_avg(rq_clock_pelt(rq), rq, p, 0);
1857 
1858 	rt_queue_push_tasks(rq);
1859 }
1860 
pick_next_rt_entity(struct rt_rq * rt_rq)1861 static struct sched_rt_entity *pick_next_rt_entity(struct rt_rq *rt_rq)
1862 {
1863 	struct rt_prio_array *array = &rt_rq->active;
1864 	struct sched_rt_entity *next = NULL;
1865 	struct list_head *queue;
1866 	int idx;
1867 
1868 	idx = sched_find_first_bit(array->bitmap);
1869 	BUG_ON(idx >= MAX_RT_PRIO);
1870 
1871 	queue = array->queue + idx;
1872 	if (SCHED_WARN_ON(list_empty(queue)))
1873 		return NULL;
1874 	next = list_entry(queue->next, struct sched_rt_entity, run_list);
1875 
1876 	return next;
1877 }
1878 
_pick_next_task_rt(struct rq * rq)1879 static struct task_struct *_pick_next_task_rt(struct rq *rq)
1880 {
1881 	struct sched_rt_entity *rt_se;
1882 	struct rt_rq *rt_rq  = &rq->rt;
1883 
1884 	do {
1885 		rt_se = pick_next_rt_entity(rt_rq);
1886 		if (unlikely(!rt_se))
1887 			return NULL;
1888 		rt_rq = group_rt_rq(rt_se);
1889 	} while (rt_rq);
1890 
1891 	return rt_task_of(rt_se);
1892 }
1893 
pick_task_rt(struct rq * rq)1894 static struct task_struct *pick_task_rt(struct rq *rq)
1895 {
1896 	struct task_struct *p;
1897 
1898 	if (!sched_rt_runnable(rq))
1899 		return NULL;
1900 
1901 	p = _pick_next_task_rt(rq);
1902 
1903 	return p;
1904 }
1905 
pick_next_task_rt(struct rq * rq)1906 static struct task_struct *pick_next_task_rt(struct rq *rq)
1907 {
1908 	struct task_struct *p = pick_task_rt(rq);
1909 
1910 	if (p)
1911 		set_next_task_rt(rq, p, true);
1912 
1913 	return p;
1914 }
1915 
put_prev_task_rt(struct rq * rq,struct task_struct * p)1916 static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
1917 {
1918 	struct sched_rt_entity *rt_se = &p->rt;
1919 	struct rt_rq *rt_rq = &rq->rt;
1920 
1921 	if (on_rt_rq(&p->rt))
1922 		update_stats_wait_start_rt(rt_rq, rt_se);
1923 
1924 	update_curr_rt(rq);
1925 
1926 	update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
1927 	trace_android_rvh_update_rt_rq_load_avg(rq_clock_pelt(rq), rq, p, 1);
1928 
1929 	/*
1930 	 * The previous task needs to be made eligible for pushing
1931 	 * if it is still active
1932 	 */
1933 	if (on_rt_rq(&p->rt) && p->nr_cpus_allowed > 1)
1934 		enqueue_pushable_task(rq, p);
1935 }
1936 
1937 #ifdef CONFIG_SMP
1938 
1939 /* Only try algorithms three times */
1940 #define RT_MAX_TRIES 3
1941 
pick_rt_task(struct rq * rq,struct task_struct * p,int cpu)1942 static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
1943 {
1944 	if (!task_on_cpu(rq, p) &&
1945 	    cpumask_test_cpu(cpu, &p->cpus_mask))
1946 		return 1;
1947 
1948 	return 0;
1949 }
1950 
1951 /*
1952  * Return the highest pushable rq's task, which is suitable to be executed
1953  * on the CPU, NULL otherwise
1954  */
pick_highest_pushable_task(struct rq * rq,int cpu)1955 struct task_struct *pick_highest_pushable_task(struct rq *rq, int cpu)
1956 {
1957 	struct plist_head *head = &rq->rt.pushable_tasks;
1958 	struct task_struct *p;
1959 
1960 	if (!has_pushable_tasks(rq))
1961 		return NULL;
1962 
1963 	plist_for_each_entry(p, head, pushable_tasks) {
1964 		if (pick_rt_task(rq, p, cpu))
1965 			return p;
1966 	}
1967 
1968 	return NULL;
1969 }
1970 EXPORT_SYMBOL_GPL(pick_highest_pushable_task);
1971 
1972 static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask);
1973 
find_lowest_rq(struct task_struct * task)1974 static int find_lowest_rq(struct task_struct *task)
1975 {
1976 	struct sched_domain *sd;
1977 	struct cpumask *lowest_mask = this_cpu_cpumask_var_ptr(local_cpu_mask);
1978 	int this_cpu = smp_processor_id();
1979 	int cpu      = -1;
1980 	int ret;
1981 
1982 	/* Make sure the mask is initialized first */
1983 	if (unlikely(!lowest_mask))
1984 		return -1;
1985 
1986 	if (task->nr_cpus_allowed == 1)
1987 		return -1; /* No other targets possible */
1988 
1989 	/*
1990 	 * If we're using the softirq optimization or if we are
1991 	 * on asym system, ensure we consider the softirq processing
1992 	 * or different capacities of the CPUs when searching for the
1993 	 * lowest_mask.
1994 	 */
1995 	if (IS_ENABLED(CONFIG_RT_SOFTIRQ_AWARE_SCHED) ||
1996 	    sched_asym_cpucap_active()) {
1997 
1998 		ret = cpupri_find_fitness(&task_rq(task)->rd->cpupri,
1999 					  task, lowest_mask,
2000 					  rt_task_fits_cpu);
2001 	} else {
2002 
2003 		ret = cpupri_find(&task_rq(task)->rd->cpupri,
2004 				  task, lowest_mask);
2005 	}
2006 
2007 	trace_android_rvh_find_lowest_rq(task, lowest_mask, ret, &cpu);
2008 	if (cpu >= 0)
2009 		return cpu;
2010 
2011 	if (!ret)
2012 		return -1; /* No targets found */
2013 
2014 	cpu = task_cpu(task);
2015 
2016 	/*
2017 	 * At this point we have built a mask of CPUs representing the
2018 	 * lowest priority tasks in the system.  Now we want to elect
2019 	 * the best one based on our affinity and topology.
2020 	 *
2021 	 * We prioritize the last CPU that the task executed on since
2022 	 * it is most likely cache-hot in that location.
2023 	 */
2024 	if (cpumask_test_cpu(cpu, lowest_mask))
2025 		return cpu;
2026 
2027 	/*
2028 	 * Otherwise, we consult the sched_domains span maps to figure
2029 	 * out which CPU is logically closest to our hot cache data.
2030 	 */
2031 	if (!cpumask_test_cpu(this_cpu, lowest_mask))
2032 		this_cpu = -1; /* Skip this_cpu opt if not among lowest */
2033 
2034 	rcu_read_lock();
2035 	for_each_domain(cpu, sd) {
2036 		if (sd->flags & SD_WAKE_AFFINE) {
2037 			int best_cpu;
2038 
2039 			/*
2040 			 * "this_cpu" is cheaper to preempt than a
2041 			 * remote processor.
2042 			 */
2043 			if (this_cpu != -1 &&
2044 			    cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
2045 				rcu_read_unlock();
2046 				return this_cpu;
2047 			}
2048 
2049 			best_cpu = cpumask_any_and_distribute(lowest_mask,
2050 							      sched_domain_span(sd));
2051 			if (best_cpu < nr_cpu_ids) {
2052 				rcu_read_unlock();
2053 				return best_cpu;
2054 			}
2055 		}
2056 	}
2057 	rcu_read_unlock();
2058 
2059 	/*
2060 	 * And finally, if there were no matches within the domains
2061 	 * just give the caller *something* to work with from the compatible
2062 	 * locations.
2063 	 */
2064 	if (this_cpu != -1)
2065 		return this_cpu;
2066 
2067 	cpu = cpumask_any_distribute(lowest_mask);
2068 	if (cpu < nr_cpu_ids)
2069 		return cpu;
2070 
2071 	return -1;
2072 }
2073 
2074 /* Will lock the rq it finds */
find_lock_lowest_rq(struct task_struct * task,struct rq * rq)2075 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
2076 {
2077 	struct rq *lowest_rq = NULL;
2078 	int tries;
2079 	int cpu;
2080 
2081 	for (tries = 0; tries < RT_MAX_TRIES; tries++) {
2082 		cpu = find_lowest_rq(task);
2083 
2084 		if ((cpu == -1) || (cpu == rq->cpu))
2085 			break;
2086 
2087 		lowest_rq = cpu_rq(cpu);
2088 
2089 		if (lowest_rq->rt.highest_prio.curr <= task->prio) {
2090 			/*
2091 			 * Target rq has tasks of equal or higher priority,
2092 			 * retrying does not release any lock and is unlikely
2093 			 * to yield a different result.
2094 			 */
2095 			lowest_rq = NULL;
2096 			break;
2097 		}
2098 
2099 		/* if the prio of this runqueue changed, try again */
2100 		if (double_lock_balance(rq, lowest_rq)) {
2101 			/*
2102 			 * We had to unlock the run queue. In
2103 			 * the mean time, task could have
2104 			 * migrated already or had its affinity changed.
2105 			 * Also make sure that it wasn't scheduled on its rq.
2106 			 * It is possible the task was scheduled, set
2107 			 * "migrate_disabled" and then got preempted, so we must
2108 			 * check the task migration disable flag here too.
2109 			 */
2110 			if (unlikely(task_rq(task) != rq ||
2111 				     !cpumask_test_cpu(lowest_rq->cpu, &task->cpus_mask) ||
2112 				     task_on_cpu(rq, task) ||
2113 				     !rt_task(task) ||
2114 				     is_migration_disabled(task) ||
2115 				     !task_on_rq_queued(task))) {
2116 
2117 				double_unlock_balance(rq, lowest_rq);
2118 				lowest_rq = NULL;
2119 				break;
2120 			}
2121 		}
2122 
2123 		/* If this rq is still suitable use it. */
2124 		if (lowest_rq->rt.highest_prio.curr > task->prio)
2125 			break;
2126 
2127 		/* try again */
2128 		double_unlock_balance(rq, lowest_rq);
2129 		lowest_rq = NULL;
2130 	}
2131 
2132 	return lowest_rq;
2133 }
2134 
pick_next_pushable_task(struct rq * rq)2135 static struct task_struct *pick_next_pushable_task(struct rq *rq)
2136 {
2137 	struct task_struct *p;
2138 
2139 	if (!has_pushable_tasks(rq))
2140 		return NULL;
2141 
2142 	p = plist_first_entry(&rq->rt.pushable_tasks,
2143 			      struct task_struct, pushable_tasks);
2144 
2145 	BUG_ON(rq->cpu != task_cpu(p));
2146 	BUG_ON(task_current(rq, p));
2147 	BUG_ON(p->nr_cpus_allowed <= 1);
2148 
2149 	BUG_ON(!task_on_rq_queued(p));
2150 	BUG_ON(!rt_task(p));
2151 
2152 	return p;
2153 }
2154 
2155 /*
2156  * If the current CPU has more than one RT task, see if the non
2157  * running task can migrate over to a CPU that is running a task
2158  * of lesser priority.
2159  */
push_rt_task(struct rq * rq,bool pull)2160 static int push_rt_task(struct rq *rq, bool pull)
2161 {
2162 	struct task_struct *next_task;
2163 	struct rq *lowest_rq;
2164 	int ret = 0;
2165 
2166 	if (!rq->rt.overloaded)
2167 		return 0;
2168 
2169 	next_task = pick_next_pushable_task(rq);
2170 	if (!next_task)
2171 		return 0;
2172 
2173 retry:
2174 	/*
2175 	 * It's possible that the next_task slipped in of
2176 	 * higher priority than current. If that's the case
2177 	 * just reschedule current.
2178 	 */
2179 	if (unlikely(next_task->prio < rq->curr->prio)) {
2180 		resched_curr(rq);
2181 		return 0;
2182 	}
2183 
2184 	if (is_migration_disabled(next_task)) {
2185 		struct task_struct *push_task = NULL;
2186 		int cpu;
2187 
2188 		if (!pull || rq->push_busy)
2189 			return 0;
2190 
2191 		/*
2192 		 * Invoking find_lowest_rq() on anything but an RT task doesn't
2193 		 * make sense. Per the above priority check, curr has to
2194 		 * be of higher priority than next_task, so no need to
2195 		 * reschedule when bailing out.
2196 		 *
2197 		 * Note that the stoppers are masqueraded as SCHED_FIFO
2198 		 * (cf. sched_set_stop_task()), so we can't rely on rt_task().
2199 		 */
2200 		if (rq->curr->sched_class != &rt_sched_class)
2201 			return 0;
2202 
2203 		cpu = find_lowest_rq(rq->curr);
2204 		if (cpu == -1 || cpu == rq->cpu)
2205 			return 0;
2206 
2207 		/*
2208 		 * Given we found a CPU with lower priority than @next_task,
2209 		 * therefore it should be running. However we cannot migrate it
2210 		 * to this other CPU, instead attempt to push the current
2211 		 * running task on this CPU away.
2212 		 */
2213 		push_task = get_push_task(rq);
2214 		if (push_task) {
2215 			preempt_disable();
2216 			raw_spin_rq_unlock(rq);
2217 			stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
2218 					    push_task, &rq->push_work);
2219 			preempt_enable();
2220 			raw_spin_rq_lock(rq);
2221 		}
2222 
2223 		return 0;
2224 	}
2225 
2226 	if (WARN_ON(next_task == rq->curr))
2227 		return 0;
2228 
2229 	/* We might release rq lock */
2230 	get_task_struct(next_task);
2231 
2232 	/* find_lock_lowest_rq locks the rq if found */
2233 	lowest_rq = find_lock_lowest_rq(next_task, rq);
2234 	if (!lowest_rq) {
2235 		struct task_struct *task;
2236 		/*
2237 		 * find_lock_lowest_rq releases rq->lock
2238 		 * so it is possible that next_task has migrated.
2239 		 *
2240 		 * We need to make sure that the task is still on the same
2241 		 * run-queue and is also still the next task eligible for
2242 		 * pushing.
2243 		 */
2244 		task = pick_next_pushable_task(rq);
2245 		if (task == next_task) {
2246 			/*
2247 			 * The task hasn't migrated, and is still the next
2248 			 * eligible task, but we failed to find a run-queue
2249 			 * to push it to.  Do not retry in this case, since
2250 			 * other CPUs will pull from us when ready.
2251 			 */
2252 			goto out;
2253 		}
2254 
2255 		if (!task)
2256 			/* No more tasks, just exit */
2257 			goto out;
2258 
2259 		/*
2260 		 * Something has shifted, try again.
2261 		 */
2262 		put_task_struct(next_task);
2263 		next_task = task;
2264 		goto retry;
2265 	}
2266 
2267 	deactivate_task(rq, next_task, 0);
2268 	set_task_cpu(next_task, lowest_rq->cpu);
2269 	activate_task(lowest_rq, next_task, 0);
2270 	resched_curr(lowest_rq);
2271 	ret = 1;
2272 
2273 	double_unlock_balance(rq, lowest_rq);
2274 out:
2275 	put_task_struct(next_task);
2276 
2277 	return ret;
2278 }
2279 
push_rt_tasks(struct rq * rq)2280 static void push_rt_tasks(struct rq *rq)
2281 {
2282 	/* push_rt_task will return true if it moved an RT */
2283 	while (push_rt_task(rq, false))
2284 		;
2285 }
2286 
2287 #ifdef HAVE_RT_PUSH_IPI
2288 
2289 /*
2290  * When a high priority task schedules out from a CPU and a lower priority
2291  * task is scheduled in, a check is made to see if there's any RT tasks
2292  * on other CPUs that are waiting to run because a higher priority RT task
2293  * is currently running on its CPU. In this case, the CPU with multiple RT
2294  * tasks queued on it (overloaded) needs to be notified that a CPU has opened
2295  * up that may be able to run one of its non-running queued RT tasks.
2296  *
2297  * All CPUs with overloaded RT tasks need to be notified as there is currently
2298  * no way to know which of these CPUs have the highest priority task waiting
2299  * to run. Instead of trying to take a spinlock on each of these CPUs,
2300  * which has shown to cause large latency when done on machines with many
2301  * CPUs, sending an IPI to the CPUs to have them push off the overloaded
2302  * RT tasks waiting to run.
2303  *
2304  * Just sending an IPI to each of the CPUs is also an issue, as on large
2305  * count CPU machines, this can cause an IPI storm on a CPU, especially
2306  * if its the only CPU with multiple RT tasks queued, and a large number
2307  * of CPUs scheduling a lower priority task at the same time.
2308  *
2309  * Each root domain has its own irq work function that can iterate over
2310  * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
2311  * task must be checked if there's one or many CPUs that are lowering
2312  * their priority, there's a single irq work iterator that will try to
2313  * push off RT tasks that are waiting to run.
2314  *
2315  * When a CPU schedules a lower priority task, it will kick off the
2316  * irq work iterator that will jump to each CPU with overloaded RT tasks.
2317  * As it only takes the first CPU that schedules a lower priority task
2318  * to start the process, the rto_start variable is incremented and if
2319  * the atomic result is one, then that CPU will try to take the rto_lock.
2320  * This prevents high contention on the lock as the process handles all
2321  * CPUs scheduling lower priority tasks.
2322  *
2323  * All CPUs that are scheduling a lower priority task will increment the
2324  * rt_loop_next variable. This will make sure that the irq work iterator
2325  * checks all RT overloaded CPUs whenever a CPU schedules a new lower
2326  * priority task, even if the iterator is in the middle of a scan. Incrementing
2327  * the rt_loop_next will cause the iterator to perform another scan.
2328  *
2329  */
rto_next_cpu(struct root_domain * rd)2330 static int rto_next_cpu(struct root_domain *rd)
2331 {
2332 	int next;
2333 	int cpu;
2334 
2335 	/*
2336 	 * When starting the IPI RT pushing, the rto_cpu is set to -1,
2337 	 * rt_next_cpu() will simply return the first CPU found in
2338 	 * the rto_mask.
2339 	 *
2340 	 * If rto_next_cpu() is called with rto_cpu is a valid CPU, it
2341 	 * will return the next CPU found in the rto_mask.
2342 	 *
2343 	 * If there are no more CPUs left in the rto_mask, then a check is made
2344 	 * against rto_loop and rto_loop_next. rto_loop is only updated with
2345 	 * the rto_lock held, but any CPU may increment the rto_loop_next
2346 	 * without any locking.
2347 	 */
2348 	for (;;) {
2349 
2350 		/* When rto_cpu is -1 this acts like cpumask_first() */
2351 		cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
2352 
2353 		/* this will be any CPU in the rd->rto_mask, and can be a halted cpu update it */
2354 		trace_android_rvh_rto_next_cpu(rd->rto_cpu, rd->rto_mask, &cpu);
2355 
2356 		rd->rto_cpu = cpu;
2357 
2358 		if (cpu < nr_cpu_ids)
2359 			return cpu;
2360 
2361 		rd->rto_cpu = -1;
2362 
2363 		/*
2364 		 * ACQUIRE ensures we see the @rto_mask changes
2365 		 * made prior to the @next value observed.
2366 		 *
2367 		 * Matches WMB in rt_set_overload().
2368 		 */
2369 		next = atomic_read_acquire(&rd->rto_loop_next);
2370 
2371 		if (rd->rto_loop == next)
2372 			break;
2373 
2374 		rd->rto_loop = next;
2375 	}
2376 
2377 	return -1;
2378 }
2379 
rto_start_trylock(atomic_t * v)2380 static inline bool rto_start_trylock(atomic_t *v)
2381 {
2382 	return !atomic_cmpxchg_acquire(v, 0, 1);
2383 }
2384 
rto_start_unlock(atomic_t * v)2385 static inline void rto_start_unlock(atomic_t *v)
2386 {
2387 	atomic_set_release(v, 0);
2388 }
2389 
tell_cpu_to_push(struct rq * rq)2390 static void tell_cpu_to_push(struct rq *rq)
2391 {
2392 	int cpu = -1;
2393 
2394 	/* Keep the loop going if the IPI is currently active */
2395 	atomic_inc(&rq->rd->rto_loop_next);
2396 
2397 	/* Only one CPU can initiate a loop at a time */
2398 	if (!rto_start_trylock(&rq->rd->rto_loop_start))
2399 		return;
2400 
2401 	raw_spin_lock(&rq->rd->rto_lock);
2402 
2403 	/*
2404 	 * The rto_cpu is updated under the lock, if it has a valid CPU
2405 	 * then the IPI is still running and will continue due to the
2406 	 * update to loop_next, and nothing needs to be done here.
2407 	 * Otherwise it is finishing up and an ipi needs to be sent.
2408 	 */
2409 	if (rq->rd->rto_cpu < 0)
2410 		cpu = rto_next_cpu(rq->rd);
2411 
2412 	raw_spin_unlock(&rq->rd->rto_lock);
2413 
2414 	rto_start_unlock(&rq->rd->rto_loop_start);
2415 
2416 	if (cpu >= 0) {
2417 		/* Make sure the rd does not get freed while pushing */
2418 		sched_get_rd(rq->rd);
2419 		irq_work_queue_on(&rq->rd->rto_push_work, cpu);
2420 	}
2421 }
2422 
2423 /* Called from hardirq context */
rto_push_irq_work_func(struct irq_work * work)2424 void rto_push_irq_work_func(struct irq_work *work)
2425 {
2426 	struct root_domain *rd =
2427 		container_of(work, struct root_domain, rto_push_work);
2428 	struct rq *rq;
2429 	int cpu;
2430 
2431 	rq = this_rq();
2432 
2433 	/*
2434 	 * We do not need to grab the lock to check for has_pushable_tasks.
2435 	 * When it gets updated, a check is made if a push is possible.
2436 	 */
2437 	if (has_pushable_tasks(rq)) {
2438 		raw_spin_rq_lock(rq);
2439 		while (push_rt_task(rq, true))
2440 			;
2441 		raw_spin_rq_unlock(rq);
2442 	}
2443 
2444 	raw_spin_lock(&rd->rto_lock);
2445 
2446 	/* Pass the IPI to the next rt overloaded queue */
2447 	cpu = rto_next_cpu(rd);
2448 
2449 	raw_spin_unlock(&rd->rto_lock);
2450 
2451 	if (cpu < 0) {
2452 		sched_put_rd(rd);
2453 		return;
2454 	}
2455 
2456 	/* Try the next RT overloaded CPU */
2457 	irq_work_queue_on(&rd->rto_push_work, cpu);
2458 }
2459 #endif /* HAVE_RT_PUSH_IPI */
2460 
pull_rt_task(struct rq * this_rq)2461 static void pull_rt_task(struct rq *this_rq)
2462 {
2463 	int this_cpu = this_rq->cpu, cpu;
2464 	bool resched = false;
2465 	struct task_struct *p, *push_task;
2466 	struct rq *src_rq;
2467 	int rt_overload_count = rt_overloaded(this_rq);
2468 
2469 	if (likely(!rt_overload_count))
2470 		return;
2471 
2472 	/*
2473 	 * Match the barrier from rt_set_overloaded; this guarantees that if we
2474 	 * see overloaded we must also see the rto_mask bit.
2475 	 */
2476 	smp_rmb();
2477 
2478 	/* If we are the only overloaded CPU do nothing */
2479 	if (rt_overload_count == 1 &&
2480 	    cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
2481 		return;
2482 
2483 #ifdef HAVE_RT_PUSH_IPI
2484 	if (sched_feat(RT_PUSH_IPI)) {
2485 		tell_cpu_to_push(this_rq);
2486 		return;
2487 	}
2488 #endif
2489 
2490 	for_each_cpu(cpu, this_rq->rd->rto_mask) {
2491 		if (this_cpu == cpu)
2492 			continue;
2493 
2494 		src_rq = cpu_rq(cpu);
2495 
2496 		/*
2497 		 * Don't bother taking the src_rq->lock if the next highest
2498 		 * task is known to be lower-priority than our current task.
2499 		 * This may look racy, but if this value is about to go
2500 		 * logically higher, the src_rq will push this task away.
2501 		 * And if its going logically lower, we do not care
2502 		 */
2503 		if (src_rq->rt.highest_prio.next >=
2504 		    this_rq->rt.highest_prio.curr)
2505 			continue;
2506 
2507 		/*
2508 		 * We can potentially drop this_rq's lock in
2509 		 * double_lock_balance, and another CPU could
2510 		 * alter this_rq
2511 		 */
2512 		push_task = NULL;
2513 		double_lock_balance(this_rq, src_rq);
2514 
2515 		/*
2516 		 * We can pull only a task, which is pushable
2517 		 * on its rq, and no others.
2518 		 */
2519 		p = pick_highest_pushable_task(src_rq, this_cpu);
2520 
2521 		/*
2522 		 * Do we have an RT task that preempts
2523 		 * the to-be-scheduled task?
2524 		 */
2525 		if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2526 			WARN_ON(p == src_rq->curr);
2527 			WARN_ON(!task_on_rq_queued(p));
2528 
2529 			/*
2530 			 * There's a chance that p is higher in priority
2531 			 * than what's currently running on its CPU.
2532 			 * This is just that p is waking up and hasn't
2533 			 * had a chance to schedule. We only pull
2534 			 * p if it is lower in priority than the
2535 			 * current task on the run queue
2536 			 */
2537 			if (p->prio < src_rq->curr->prio)
2538 				goto skip;
2539 
2540 			if (is_migration_disabled(p)) {
2541 				push_task = get_push_task(src_rq);
2542 			} else {
2543 				deactivate_task(src_rq, p, 0);
2544 				set_task_cpu(p, this_cpu);
2545 				activate_task(this_rq, p, 0);
2546 				resched = true;
2547 			}
2548 			/*
2549 			 * We continue with the search, just in
2550 			 * case there's an even higher prio task
2551 			 * in another runqueue. (low likelihood
2552 			 * but possible)
2553 			 */
2554 		}
2555 skip:
2556 		double_unlock_balance(this_rq, src_rq);
2557 
2558 		if (push_task) {
2559 			preempt_disable();
2560 			raw_spin_rq_unlock(this_rq);
2561 			stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
2562 					    push_task, &src_rq->push_work);
2563 			preempt_enable();
2564 			raw_spin_rq_lock(this_rq);
2565 		}
2566 	}
2567 
2568 	if (resched)
2569 		resched_curr(this_rq);
2570 }
2571 
2572 /*
2573  * If we are not running and we are not going to reschedule soon, we should
2574  * try to push tasks away now
2575  */
task_woken_rt(struct rq * rq,struct task_struct * p)2576 static void task_woken_rt(struct rq *rq, struct task_struct *p)
2577 {
2578 	bool need_to_push = !task_on_cpu(rq, p) &&
2579 			    !test_tsk_need_resched(rq->curr) &&
2580 			    p->nr_cpus_allowed > 1 &&
2581 			    (dl_task(rq->curr) || rt_task(rq->curr)) &&
2582 			    (rq->curr->nr_cpus_allowed < 2 ||
2583 			     rq->curr->prio <= p->prio);
2584 
2585 	if (need_to_push)
2586 		push_rt_tasks(rq);
2587 }
2588 
2589 /* Assumes rq->lock is held */
rq_online_rt(struct rq * rq)2590 static void rq_online_rt(struct rq *rq)
2591 {
2592 	if (rq->rt.overloaded)
2593 		rt_set_overload(rq);
2594 
2595 	__enable_runtime(rq);
2596 
2597 	cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
2598 }
2599 
2600 /* Assumes rq->lock is held */
rq_offline_rt(struct rq * rq)2601 static void rq_offline_rt(struct rq *rq)
2602 {
2603 	if (rq->rt.overloaded)
2604 		rt_clear_overload(rq);
2605 
2606 	__disable_runtime(rq);
2607 
2608 	cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
2609 }
2610 
2611 /*
2612  * When switch from the rt queue, we bring ourselves to a position
2613  * that we might want to pull RT tasks from other runqueues.
2614  */
switched_from_rt(struct rq * rq,struct task_struct * p)2615 static void switched_from_rt(struct rq *rq, struct task_struct *p)
2616 {
2617 	/*
2618 	 * If there are other RT tasks then we will reschedule
2619 	 * and the scheduling of the other RT tasks will handle
2620 	 * the balancing. But if we are the last RT task
2621 	 * we may need to handle the pulling of RT tasks
2622 	 * now.
2623 	 */
2624 	if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
2625 		return;
2626 
2627 	rt_queue_pull_task(rq);
2628 }
2629 
init_sched_rt_class(void)2630 void __init init_sched_rt_class(void)
2631 {
2632 	unsigned int i;
2633 
2634 	for_each_possible_cpu(i) {
2635 		zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
2636 					GFP_KERNEL, cpu_to_node(i));
2637 	}
2638 }
2639 #endif /* CONFIG_SMP */
2640 
2641 /*
2642  * When switching a task to RT, we may overload the runqueue
2643  * with RT tasks. In this case we try to push them off to
2644  * other runqueues.
2645  */
switched_to_rt(struct rq * rq,struct task_struct * p)2646 static void switched_to_rt(struct rq *rq, struct task_struct *p)
2647 {
2648 	/*
2649 	 * If we are running, update the avg_rt tracking, as the running time
2650 	 * will now on be accounted into the latter.
2651 	 */
2652 	if (task_current(rq, p)) {
2653 		update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2654 		return;
2655 	}
2656 
2657 	/*
2658 	 * If we are not running we may need to preempt the current
2659 	 * running task. If that current running task is also an RT task
2660 	 * then see if we can move to another run queue.
2661 	 */
2662 	if (task_on_rq_queued(p)) {
2663 #ifdef CONFIG_SMP
2664 		if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
2665 			rt_queue_push_tasks(rq);
2666 #endif /* CONFIG_SMP */
2667 		if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
2668 			resched_curr(rq);
2669 	}
2670 }
2671 
2672 /*
2673  * Priority of the task has changed. This may cause
2674  * us to initiate a push or pull.
2675  */
2676 static void
prio_changed_rt(struct rq * rq,struct task_struct * p,int oldprio)2677 prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
2678 {
2679 	if (!task_on_rq_queued(p))
2680 		return;
2681 
2682 	if (task_current(rq, p)) {
2683 #ifdef CONFIG_SMP
2684 		/*
2685 		 * If our priority decreases while running, we
2686 		 * may need to pull tasks to this runqueue.
2687 		 */
2688 		if (oldprio < p->prio)
2689 			rt_queue_pull_task(rq);
2690 
2691 		/*
2692 		 * If there's a higher priority task waiting to run
2693 		 * then reschedule.
2694 		 */
2695 		if (p->prio > rq->rt.highest_prio.curr)
2696 			resched_curr(rq);
2697 #else
2698 		/* For UP simply resched on drop of prio */
2699 		if (oldprio < p->prio)
2700 			resched_curr(rq);
2701 #endif /* CONFIG_SMP */
2702 	} else {
2703 		/*
2704 		 * This task is not running, but if it is
2705 		 * greater than the current running task
2706 		 * then reschedule.
2707 		 */
2708 		if (p->prio < rq->curr->prio)
2709 			resched_curr(rq);
2710 	}
2711 }
2712 
2713 #ifdef CONFIG_POSIX_TIMERS
watchdog(struct rq * rq,struct task_struct * p)2714 static void watchdog(struct rq *rq, struct task_struct *p)
2715 {
2716 	unsigned long soft, hard;
2717 
2718 	/* max may change after cur was read, this will be fixed next tick */
2719 	soft = task_rlimit(p, RLIMIT_RTTIME);
2720 	hard = task_rlimit_max(p, RLIMIT_RTTIME);
2721 
2722 	if (soft != RLIM_INFINITY) {
2723 		unsigned long next;
2724 
2725 		if (p->rt.watchdog_stamp != jiffies) {
2726 			p->rt.timeout++;
2727 			p->rt.watchdog_stamp = jiffies;
2728 		}
2729 
2730 		next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
2731 		if (p->rt.timeout > next) {
2732 			posix_cputimers_rt_watchdog(&p->posix_cputimers,
2733 						    p->se.sum_exec_runtime);
2734 		}
2735 	}
2736 }
2737 #else
watchdog(struct rq * rq,struct task_struct * p)2738 static inline void watchdog(struct rq *rq, struct task_struct *p) { }
2739 #endif
2740 
2741 /*
2742  * scheduler tick hitting a task of our scheduling class.
2743  *
2744  * NOTE: This function can be called remotely by the tick offload that
2745  * goes along full dynticks. Therefore no local assumption can be made
2746  * and everything must be accessed through the @rq and @curr passed in
2747  * parameters.
2748  */
task_tick_rt(struct rq * rq,struct task_struct * p,int queued)2749 static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
2750 {
2751 	struct sched_rt_entity *rt_se = &p->rt;
2752 
2753 	update_curr_rt(rq);
2754 	update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2755 	trace_android_rvh_update_rt_rq_load_avg(rq_clock_pelt(rq), rq, p, 1);
2756 
2757 	watchdog(rq, p);
2758 
2759 	/*
2760 	 * RR tasks need a special form of timeslice management.
2761 	 * FIFO tasks have no timeslices.
2762 	 */
2763 	if (p->policy != SCHED_RR)
2764 		return;
2765 
2766 	if (--p->rt.time_slice)
2767 		return;
2768 
2769 	p->rt.time_slice = sched_rr_timeslice;
2770 
2771 	/*
2772 	 * Requeue to the end of queue if we (and all of our ancestors) are not
2773 	 * the only element on the queue
2774 	 */
2775 	for_each_sched_rt_entity(rt_se) {
2776 		if (rt_se->run_list.prev != rt_se->run_list.next) {
2777 			requeue_task_rt(rq, p, 0);
2778 			resched_curr(rq);
2779 			return;
2780 		}
2781 	}
2782 }
2783 
get_rr_interval_rt(struct rq * rq,struct task_struct * task)2784 static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
2785 {
2786 	/*
2787 	 * Time slice is 0 for SCHED_FIFO tasks
2788 	 */
2789 	if (task->policy == SCHED_RR)
2790 		return sched_rr_timeslice;
2791 	else
2792 		return 0;
2793 }
2794 
2795 DEFINE_SCHED_CLASS(rt) = {
2796 
2797 	.enqueue_task		= enqueue_task_rt,
2798 	.dequeue_task		= dequeue_task_rt,
2799 	.yield_task		= yield_task_rt,
2800 
2801 	.check_preempt_curr	= check_preempt_curr_rt,
2802 
2803 	.pick_next_task		= pick_next_task_rt,
2804 	.put_prev_task		= put_prev_task_rt,
2805 	.set_next_task          = set_next_task_rt,
2806 
2807 #ifdef CONFIG_SMP
2808 	.balance		= balance_rt,
2809 	.pick_task		= pick_task_rt,
2810 	.select_task_rq		= select_task_rq_rt,
2811 	.set_cpus_allowed       = set_cpus_allowed_common,
2812 	.rq_online              = rq_online_rt,
2813 	.rq_offline             = rq_offline_rt,
2814 	.task_woken		= task_woken_rt,
2815 	.switched_from		= switched_from_rt,
2816 	.find_lock_rq		= find_lock_lowest_rq,
2817 #endif
2818 
2819 	.task_tick		= task_tick_rt,
2820 
2821 	.get_rr_interval	= get_rr_interval_rt,
2822 
2823 	.prio_changed		= prio_changed_rt,
2824 	.switched_to		= switched_to_rt,
2825 
2826 	.update_curr		= update_curr_rt,
2827 
2828 #ifdef CONFIG_UCLAMP_TASK
2829 	.uclamp_enabled		= 1,
2830 #endif
2831 };
2832 
2833 #ifdef CONFIG_RT_GROUP_SCHED
2834 /*
2835  * Ensure that the real time constraints are schedulable.
2836  */
2837 static DEFINE_MUTEX(rt_constraints_mutex);
2838 
tg_has_rt_tasks(struct task_group * tg)2839 static inline int tg_has_rt_tasks(struct task_group *tg)
2840 {
2841 	struct task_struct *task;
2842 	struct css_task_iter it;
2843 	int ret = 0;
2844 
2845 	/*
2846 	 * Autogroups do not have RT tasks; see autogroup_create().
2847 	 */
2848 	if (task_group_is_autogroup(tg))
2849 		return 0;
2850 
2851 	css_task_iter_start(&tg->css, 0, &it);
2852 	while (!ret && (task = css_task_iter_next(&it)))
2853 		ret |= rt_task(task);
2854 	css_task_iter_end(&it);
2855 
2856 	return ret;
2857 }
2858 
2859 struct rt_schedulable_data {
2860 	struct task_group *tg;
2861 	u64 rt_period;
2862 	u64 rt_runtime;
2863 };
2864 
tg_rt_schedulable(struct task_group * tg,void * data)2865 static int tg_rt_schedulable(struct task_group *tg, void *data)
2866 {
2867 	struct rt_schedulable_data *d = data;
2868 	struct task_group *child;
2869 	unsigned long total, sum = 0;
2870 	u64 period, runtime;
2871 
2872 	period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2873 	runtime = tg->rt_bandwidth.rt_runtime;
2874 
2875 	if (tg == d->tg) {
2876 		period = d->rt_period;
2877 		runtime = d->rt_runtime;
2878 	}
2879 
2880 	/*
2881 	 * Cannot have more runtime than the period.
2882 	 */
2883 	if (runtime > period && runtime != RUNTIME_INF)
2884 		return -EINVAL;
2885 
2886 	/*
2887 	 * Ensure we don't starve existing RT tasks if runtime turns zero.
2888 	 */
2889 	if (rt_bandwidth_enabled() && !runtime &&
2890 	    tg->rt_bandwidth.rt_runtime && tg_has_rt_tasks(tg))
2891 		return -EBUSY;
2892 
2893 	total = to_ratio(period, runtime);
2894 
2895 	/*
2896 	 * Nobody can have more than the global setting allows.
2897 	 */
2898 	if (total > to_ratio(global_rt_period(), global_rt_runtime()))
2899 		return -EINVAL;
2900 
2901 	/*
2902 	 * The sum of our children's runtime should not exceed our own.
2903 	 */
2904 	list_for_each_entry_rcu(child, &tg->children, siblings) {
2905 		period = ktime_to_ns(child->rt_bandwidth.rt_period);
2906 		runtime = child->rt_bandwidth.rt_runtime;
2907 
2908 		if (child == d->tg) {
2909 			period = d->rt_period;
2910 			runtime = d->rt_runtime;
2911 		}
2912 
2913 		sum += to_ratio(period, runtime);
2914 	}
2915 
2916 	if (sum > total)
2917 		return -EINVAL;
2918 
2919 	return 0;
2920 }
2921 
__rt_schedulable(struct task_group * tg,u64 period,u64 runtime)2922 static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
2923 {
2924 	int ret;
2925 
2926 	struct rt_schedulable_data data = {
2927 		.tg = tg,
2928 		.rt_period = period,
2929 		.rt_runtime = runtime,
2930 	};
2931 
2932 	rcu_read_lock();
2933 	ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
2934 	rcu_read_unlock();
2935 
2936 	return ret;
2937 }
2938 
tg_set_rt_bandwidth(struct task_group * tg,u64 rt_period,u64 rt_runtime)2939 static int tg_set_rt_bandwidth(struct task_group *tg,
2940 		u64 rt_period, u64 rt_runtime)
2941 {
2942 	int i, err = 0;
2943 
2944 	/*
2945 	 * Disallowing the root group RT runtime is BAD, it would disallow the
2946 	 * kernel creating (and or operating) RT threads.
2947 	 */
2948 	if (tg == &root_task_group && rt_runtime == 0)
2949 		return -EINVAL;
2950 
2951 	/* No period doesn't make any sense. */
2952 	if (rt_period == 0)
2953 		return -EINVAL;
2954 
2955 	/*
2956 	 * Bound quota to defend quota against overflow during bandwidth shift.
2957 	 */
2958 	if (rt_runtime != RUNTIME_INF && rt_runtime > max_rt_runtime)
2959 		return -EINVAL;
2960 
2961 	mutex_lock(&rt_constraints_mutex);
2962 	err = __rt_schedulable(tg, rt_period, rt_runtime);
2963 	if (err)
2964 		goto unlock;
2965 
2966 	raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2967 	tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
2968 	tg->rt_bandwidth.rt_runtime = rt_runtime;
2969 
2970 	for_each_possible_cpu(i) {
2971 		struct rt_rq *rt_rq = tg->rt_rq[i];
2972 
2973 		raw_spin_lock(&rt_rq->rt_runtime_lock);
2974 		rt_rq->rt_runtime = rt_runtime;
2975 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
2976 	}
2977 	raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2978 unlock:
2979 	mutex_unlock(&rt_constraints_mutex);
2980 
2981 	return err;
2982 }
2983 
sched_group_set_rt_runtime(struct task_group * tg,long rt_runtime_us)2984 int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
2985 {
2986 	u64 rt_runtime, rt_period;
2987 
2988 	rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2989 	rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
2990 	if (rt_runtime_us < 0)
2991 		rt_runtime = RUNTIME_INF;
2992 	else if ((u64)rt_runtime_us > U64_MAX / NSEC_PER_USEC)
2993 		return -EINVAL;
2994 
2995 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2996 }
2997 
sched_group_rt_runtime(struct task_group * tg)2998 long sched_group_rt_runtime(struct task_group *tg)
2999 {
3000 	u64 rt_runtime_us;
3001 
3002 	if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
3003 		return -1;
3004 
3005 	rt_runtime_us = tg->rt_bandwidth.rt_runtime;
3006 	do_div(rt_runtime_us, NSEC_PER_USEC);
3007 	return rt_runtime_us;
3008 }
3009 
sched_group_set_rt_period(struct task_group * tg,u64 rt_period_us)3010 int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
3011 {
3012 	u64 rt_runtime, rt_period;
3013 
3014 	if (rt_period_us > U64_MAX / NSEC_PER_USEC)
3015 		return -EINVAL;
3016 
3017 	rt_period = rt_period_us * NSEC_PER_USEC;
3018 	rt_runtime = tg->rt_bandwidth.rt_runtime;
3019 
3020 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
3021 }
3022 
sched_group_rt_period(struct task_group * tg)3023 long sched_group_rt_period(struct task_group *tg)
3024 {
3025 	u64 rt_period_us;
3026 
3027 	rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
3028 	do_div(rt_period_us, NSEC_PER_USEC);
3029 	return rt_period_us;
3030 }
3031 
3032 #ifdef CONFIG_SYSCTL
sched_rt_global_constraints(void)3033 static int sched_rt_global_constraints(void)
3034 {
3035 	int ret = 0;
3036 
3037 	mutex_lock(&rt_constraints_mutex);
3038 	ret = __rt_schedulable(NULL, 0, 0);
3039 	mutex_unlock(&rt_constraints_mutex);
3040 
3041 	return ret;
3042 }
3043 #endif /* CONFIG_SYSCTL */
3044 
sched_rt_can_attach(struct task_group * tg,struct task_struct * tsk)3045 int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
3046 {
3047 	/* Don't accept realtime tasks when there is no way for them to run */
3048 	if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
3049 		return 0;
3050 
3051 	return 1;
3052 }
3053 
3054 #else /* !CONFIG_RT_GROUP_SCHED */
3055 
3056 #ifdef CONFIG_SYSCTL
sched_rt_global_constraints(void)3057 static int sched_rt_global_constraints(void)
3058 {
3059 	unsigned long flags;
3060 	int i;
3061 
3062 	raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
3063 	for_each_possible_cpu(i) {
3064 		struct rt_rq *rt_rq = &cpu_rq(i)->rt;
3065 
3066 		raw_spin_lock(&rt_rq->rt_runtime_lock);
3067 		rt_rq->rt_runtime = global_rt_runtime();
3068 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
3069 	}
3070 	raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
3071 
3072 	return 0;
3073 }
3074 #endif /* CONFIG_SYSCTL */
3075 #endif /* CONFIG_RT_GROUP_SCHED */
3076 
3077 #ifdef CONFIG_SYSCTL
sched_rt_global_validate(void)3078 static int sched_rt_global_validate(void)
3079 {
3080 	if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
3081 		((sysctl_sched_rt_runtime > sysctl_sched_rt_period) ||
3082 		 ((u64)sysctl_sched_rt_runtime *
3083 			NSEC_PER_USEC > max_rt_runtime)))
3084 		return -EINVAL;
3085 
3086 	return 0;
3087 }
3088 
sched_rt_do_global(void)3089 static void sched_rt_do_global(void)
3090 {
3091 	unsigned long flags;
3092 
3093 	raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
3094 	def_rt_bandwidth.rt_runtime = global_rt_runtime();
3095 	def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
3096 	raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
3097 }
3098 
sched_rt_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)3099 static int sched_rt_handler(struct ctl_table *table, int write, void *buffer,
3100 		size_t *lenp, loff_t *ppos)
3101 {
3102 	int old_period, old_runtime;
3103 	static DEFINE_MUTEX(mutex);
3104 	int ret;
3105 
3106 	mutex_lock(&mutex);
3107 	old_period = sysctl_sched_rt_period;
3108 	old_runtime = sysctl_sched_rt_runtime;
3109 
3110 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
3111 
3112 	if (!ret && write) {
3113 		ret = sched_rt_global_validate();
3114 		if (ret)
3115 			goto undo;
3116 
3117 		ret = sched_dl_global_validate();
3118 		if (ret)
3119 			goto undo;
3120 
3121 		ret = sched_rt_global_constraints();
3122 		if (ret)
3123 			goto undo;
3124 
3125 		sched_rt_do_global();
3126 		sched_dl_do_global();
3127 	}
3128 	if (0) {
3129 undo:
3130 		sysctl_sched_rt_period = old_period;
3131 		sysctl_sched_rt_runtime = old_runtime;
3132 	}
3133 	mutex_unlock(&mutex);
3134 
3135 	return ret;
3136 }
3137 
sched_rr_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)3138 static int sched_rr_handler(struct ctl_table *table, int write, void *buffer,
3139 		size_t *lenp, loff_t *ppos)
3140 {
3141 	int ret;
3142 	static DEFINE_MUTEX(mutex);
3143 
3144 	mutex_lock(&mutex);
3145 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
3146 	/*
3147 	 * Make sure that internally we keep jiffies.
3148 	 * Also, writing zero resets the timeslice to default:
3149 	 */
3150 	if (!ret && write) {
3151 		sched_rr_timeslice =
3152 			sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
3153 			msecs_to_jiffies(sysctl_sched_rr_timeslice);
3154 
3155 		if (sysctl_sched_rr_timeslice <= 0)
3156 			sysctl_sched_rr_timeslice = jiffies_to_msecs(RR_TIMESLICE);
3157 	}
3158 	mutex_unlock(&mutex);
3159 
3160 	return ret;
3161 }
3162 #endif /* CONFIG_SYSCTL */
3163 
3164 #ifdef CONFIG_SCHED_DEBUG
print_rt_stats(struct seq_file * m,int cpu)3165 void print_rt_stats(struct seq_file *m, int cpu)
3166 {
3167 	rt_rq_iter_t iter;
3168 	struct rt_rq *rt_rq;
3169 
3170 	rcu_read_lock();
3171 	for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
3172 		print_rt_rq(m, cpu, rt_rq);
3173 	rcu_read_unlock();
3174 }
3175 #endif /* CONFIG_SCHED_DEBUG */
3176