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