• 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 #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 * RR_TIMESLICE) / HZ;
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 	/*
1562 	 * Respect the sync flag as long as the task can run on this CPU.
1563 	 */
1564 	if (should_honor_rt_sync(this_cpu_rq, p, sync) &&
1565 	    cpumask_test_cpu(this_cpu, p->cpus_ptr)) {
1566 		cpu = this_cpu;
1567 		goto out_unlock;
1568 	}
1569 
1570 	if (test || !rt_task_fits_capacity(p, cpu)) {
1571 		int target = find_lowest_rq(p);
1572 
1573 		/*
1574 		 * Bail out if we were forcing a migration to find a better
1575 		 * fitting CPU but our search failed.
1576 		 */
1577 		if (!test && target != -1 && !rt_task_fits_capacity(p, target))
1578 			goto out_unlock;
1579 
1580 		/*
1581 		 * If cpu is non-preemptible, prefer remote cpu
1582 		 * even if it's running a higher-prio task.
1583 		 * Otherwise: Don't bother moving it if the destination CPU is
1584 		 * not running a lower priority task.
1585 		 */
1586 		if (target != -1 &&
1587 		    (may_not_preempt ||
1588 		     p->prio < cpu_rq(target)->rt.highest_prio.curr))
1589 			cpu = target;
1590 	}
1591 
1592 out_unlock:
1593 	rcu_read_unlock();
1594 
1595 out:
1596 	return cpu;
1597 }
1598 
check_preempt_equal_prio(struct rq * rq,struct task_struct * p)1599 static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
1600 {
1601 	/*
1602 	 * Current can't be migrated, useless to reschedule,
1603 	 * let's hope p can move out.
1604 	 */
1605 	if (rq->curr->nr_cpus_allowed == 1 ||
1606 	    !cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1607 		return;
1608 
1609 	/*
1610 	 * p is migratable, so let's not schedule it and
1611 	 * see if it is pushed or pulled somewhere else.
1612 	 */
1613 	if (p->nr_cpus_allowed != 1 &&
1614 	    cpupri_find(&rq->rd->cpupri, p, NULL))
1615 		return;
1616 
1617 	/*
1618 	 * There appear to be other CPUs that can accept
1619 	 * the current task but none can run 'p', so lets reschedule
1620 	 * to try and push the current task away:
1621 	 */
1622 	requeue_task_rt(rq, p, 1);
1623 	resched_curr(rq);
1624 }
1625 
balance_rt(struct rq * rq,struct task_struct * p,struct rq_flags * rf)1626 static int balance_rt(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1627 {
1628 	if (!on_rt_rq(&p->rt) && need_pull_rt_task(rq, p)) {
1629 		int done = 0;
1630 
1631 		/*
1632 		 * This is OK, because current is on_cpu, which avoids it being
1633 		 * picked for load-balance and preemption/IRQs are still
1634 		 * disabled avoiding further scheduler activity on it and we've
1635 		 * not yet started the picking loop.
1636 		 */
1637 		rq_unpin_lock(rq, rf);
1638 		trace_android_rvh_sched_balance_rt(rq, p, &done);
1639 		if (!done)
1640 			pull_rt_task(rq);
1641 		rq_repin_lock(rq, rf);
1642 	}
1643 
1644 	return sched_stop_runnable(rq) || sched_dl_runnable(rq) || sched_rt_runnable(rq);
1645 }
1646 #endif /* CONFIG_SMP */
1647 
1648 /*
1649  * Preempt the current task with a newly woken task if needed:
1650  */
check_preempt_curr_rt(struct rq * rq,struct task_struct * p,int flags)1651 static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
1652 {
1653 	if (p->prio < rq->curr->prio) {
1654 		resched_curr(rq);
1655 		return;
1656 	}
1657 
1658 #ifdef CONFIG_SMP
1659 	/*
1660 	 * If:
1661 	 *
1662 	 * - the newly woken task is of equal priority to the current task
1663 	 * - the newly woken task is non-migratable while current is migratable
1664 	 * - current will be preempted on the next reschedule
1665 	 *
1666 	 * we should check to see if current can readily move to a different
1667 	 * cpu.  If so, we will reschedule to allow the push logic to try
1668 	 * to move current somewhere else, making room for our non-migratable
1669 	 * task.
1670 	 */
1671 	if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr))
1672 		check_preempt_equal_prio(rq, p);
1673 #endif
1674 }
1675 
set_next_task_rt(struct rq * rq,struct task_struct * p,bool first)1676 static inline void set_next_task_rt(struct rq *rq, struct task_struct *p, bool first)
1677 {
1678 	p->se.exec_start = rq_clock_task(rq);
1679 
1680 	/* The running task is never eligible for pushing */
1681 	dequeue_pushable_task(rq, p);
1682 
1683 	if (!first)
1684 		return;
1685 
1686 	/*
1687 	 * If prev task was rt, put_prev_task() has already updated the
1688 	 * utilization. We only care of the case where we start to schedule a
1689 	 * rt task
1690 	 */
1691 	if (rq->curr->sched_class != &rt_sched_class)
1692 		update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
1693 
1694 	rt_queue_push_tasks(rq);
1695 }
1696 
pick_next_rt_entity(struct rt_rq * rt_rq)1697 static struct sched_rt_entity *pick_next_rt_entity(struct rt_rq *rt_rq)
1698 {
1699 	struct rt_prio_array *array = &rt_rq->active;
1700 	struct sched_rt_entity *next = NULL;
1701 	struct list_head *queue;
1702 	int idx;
1703 
1704 	idx = sched_find_first_bit(array->bitmap);
1705 	BUG_ON(idx >= MAX_RT_PRIO);
1706 
1707 	queue = array->queue + idx;
1708 	if (SCHED_WARN_ON(list_empty(queue)))
1709 		return NULL;
1710 	next = list_entry(queue->next, struct sched_rt_entity, run_list);
1711 
1712 	return next;
1713 }
1714 
_pick_next_task_rt(struct rq * rq)1715 static struct task_struct *_pick_next_task_rt(struct rq *rq)
1716 {
1717 	struct sched_rt_entity *rt_se;
1718 	struct rt_rq *rt_rq  = &rq->rt;
1719 
1720 	do {
1721 		rt_se = pick_next_rt_entity(rt_rq);
1722 		if (unlikely(!rt_se))
1723 			return NULL;
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 	/*
1833 	 * At this point we have built a mask of CPUs representing the
1834 	 * lowest priority tasks in the system.  Now we want to elect
1835 	 * the best one based on our affinity and topology.
1836 	 *
1837 	 * We prioritize the last CPU that the task executed on since
1838 	 * it is most likely cache-hot in that location.
1839 	 */
1840 	if (cpumask_test_cpu(cpu, lowest_mask))
1841 		return cpu;
1842 
1843 	/*
1844 	 * Otherwise, we consult the sched_domains span maps to figure
1845 	 * out which CPU is logically closest to our hot cache data.
1846 	 */
1847 	if (!cpumask_test_cpu(this_cpu, lowest_mask))
1848 		this_cpu = -1; /* Skip this_cpu opt if not among lowest */
1849 
1850 	rcu_read_lock();
1851 	for_each_domain(cpu, sd) {
1852 		if (sd->flags & SD_WAKE_AFFINE) {
1853 			int best_cpu;
1854 
1855 			/*
1856 			 * "this_cpu" is cheaper to preempt than a
1857 			 * remote processor.
1858 			 */
1859 			if (this_cpu != -1 &&
1860 			    cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
1861 				rcu_read_unlock();
1862 				return this_cpu;
1863 			}
1864 
1865 			best_cpu = cpumask_first_and(lowest_mask,
1866 						     sched_domain_span(sd));
1867 			if (best_cpu < nr_cpu_ids) {
1868 				rcu_read_unlock();
1869 				return best_cpu;
1870 			}
1871 		}
1872 	}
1873 	rcu_read_unlock();
1874 
1875 	/*
1876 	 * And finally, if there were no matches within the domains
1877 	 * just give the caller *something* to work with from the compatible
1878 	 * locations.
1879 	 */
1880 	if (this_cpu != -1)
1881 		return this_cpu;
1882 
1883 	cpu = cpumask_any(lowest_mask);
1884 	if (cpu < nr_cpu_ids)
1885 		return cpu;
1886 
1887 	return -1;
1888 }
1889 
1890 /* Will lock the rq it finds */
find_lock_lowest_rq(struct task_struct * task,struct rq * rq)1891 static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq)
1892 {
1893 	struct rq *lowest_rq = NULL;
1894 	int tries;
1895 	int cpu;
1896 
1897 	for (tries = 0; tries < RT_MAX_TRIES; tries++) {
1898 		cpu = find_lowest_rq(task);
1899 
1900 		if ((cpu == -1) || (cpu == rq->cpu))
1901 			break;
1902 
1903 		lowest_rq = cpu_rq(cpu);
1904 
1905 		if (lowest_rq->rt.highest_prio.curr <= task->prio) {
1906 			/*
1907 			 * Target rq has tasks of equal or higher priority,
1908 			 * retrying does not release any lock and is unlikely
1909 			 * to yield a different result.
1910 			 */
1911 			lowest_rq = NULL;
1912 			break;
1913 		}
1914 
1915 		/* if the prio of this runqueue changed, try again */
1916 		if (double_lock_balance(rq, lowest_rq)) {
1917 			/*
1918 			 * We had to unlock the run queue. In
1919 			 * the mean time, task could have
1920 			 * migrated already or had its affinity changed.
1921 			 * Also make sure that it wasn't scheduled on its rq.
1922 			 */
1923 			if (unlikely(task_rq(task) != rq ||
1924 				     !cpumask_test_cpu(lowest_rq->cpu, task->cpus_ptr) ||
1925 				     task_running(rq, task) ||
1926 				     !rt_task(task) ||
1927 				     !task_on_rq_queued(task))) {
1928 
1929 				double_unlock_balance(rq, lowest_rq);
1930 				lowest_rq = NULL;
1931 				break;
1932 			}
1933 		}
1934 
1935 		/* If this rq is still suitable use it. */
1936 		if (lowest_rq->rt.highest_prio.curr > task->prio)
1937 			break;
1938 
1939 		/* try again */
1940 		double_unlock_balance(rq, lowest_rq);
1941 		lowest_rq = NULL;
1942 	}
1943 
1944 	return lowest_rq;
1945 }
1946 
pick_next_pushable_task(struct rq * rq)1947 static struct task_struct *pick_next_pushable_task(struct rq *rq)
1948 {
1949 	struct task_struct *p;
1950 
1951 	if (!has_pushable_tasks(rq))
1952 		return NULL;
1953 
1954 	p = plist_first_entry(&rq->rt.pushable_tasks,
1955 			      struct task_struct, pushable_tasks);
1956 
1957 	BUG_ON(rq->cpu != task_cpu(p));
1958 	BUG_ON(task_current(rq, p));
1959 	BUG_ON(p->nr_cpus_allowed <= 1);
1960 
1961 	BUG_ON(!task_on_rq_queued(p));
1962 	BUG_ON(!rt_task(p));
1963 
1964 	return p;
1965 }
1966 
1967 /*
1968  * If the current CPU has more than one RT task, see if the non
1969  * running task can migrate over to a CPU that is running a task
1970  * of lesser priority.
1971  */
push_rt_task(struct rq * rq)1972 static int push_rt_task(struct rq *rq)
1973 {
1974 	struct task_struct *next_task;
1975 	struct rq *lowest_rq;
1976 	int ret = 0;
1977 
1978 	if (!rq->rt.overloaded)
1979 		return 0;
1980 
1981 	next_task = pick_next_pushable_task(rq);
1982 	if (!next_task)
1983 		return 0;
1984 
1985 retry:
1986 	if (WARN_ON(next_task == rq->curr))
1987 		return 0;
1988 
1989 	/*
1990 	 * It's possible that the next_task slipped in of
1991 	 * higher priority than current. If that's the case
1992 	 * just reschedule current.
1993 	 */
1994 	if (unlikely(next_task->prio < rq->curr->prio)) {
1995 		resched_curr(rq);
1996 		return 0;
1997 	}
1998 
1999 	/* We might release rq lock */
2000 	get_task_struct(next_task);
2001 
2002 	/* find_lock_lowest_rq locks the rq if found */
2003 	lowest_rq = find_lock_lowest_rq(next_task, rq);
2004 	if (!lowest_rq) {
2005 		struct task_struct *task;
2006 		/*
2007 		 * find_lock_lowest_rq releases rq->lock
2008 		 * so it is possible that next_task has migrated.
2009 		 *
2010 		 * We need to make sure that the task is still on the same
2011 		 * run-queue and is also still the next task eligible for
2012 		 * pushing.
2013 		 */
2014 		task = pick_next_pushable_task(rq);
2015 		if (task == next_task) {
2016 			/*
2017 			 * The task hasn't migrated, and is still the next
2018 			 * eligible task, but we failed to find a run-queue
2019 			 * to push it to.  Do not retry in this case, since
2020 			 * other CPUs will pull from us when ready.
2021 			 */
2022 			goto out;
2023 		}
2024 
2025 		if (!task)
2026 			/* No more tasks, just exit */
2027 			goto out;
2028 
2029 		/*
2030 		 * Something has shifted, try again.
2031 		 */
2032 		put_task_struct(next_task);
2033 		next_task = task;
2034 		goto retry;
2035 	}
2036 
2037 	deactivate_task(rq, next_task, 0);
2038 	set_task_cpu(next_task, lowest_rq->cpu);
2039 	activate_task(lowest_rq, next_task, 0);
2040 	ret = 1;
2041 
2042 	resched_curr(lowest_rq);
2043 
2044 	double_unlock_balance(rq, lowest_rq);
2045 
2046 out:
2047 	put_task_struct(next_task);
2048 
2049 	return ret;
2050 }
2051 
push_rt_tasks(struct rq * rq)2052 static void push_rt_tasks(struct rq *rq)
2053 {
2054 	/* push_rt_task will return true if it moved an RT */
2055 	while (push_rt_task(rq))
2056 		;
2057 }
2058 
2059 #ifdef HAVE_RT_PUSH_IPI
2060 
2061 /*
2062  * When a high priority task schedules out from a CPU and a lower priority
2063  * task is scheduled in, a check is made to see if there's any RT tasks
2064  * on other CPUs that are waiting to run because a higher priority RT task
2065  * is currently running on its CPU. In this case, the CPU with multiple RT
2066  * tasks queued on it (overloaded) needs to be notified that a CPU has opened
2067  * up that may be able to run one of its non-running queued RT tasks.
2068  *
2069  * All CPUs with overloaded RT tasks need to be notified as there is currently
2070  * no way to know which of these CPUs have the highest priority task waiting
2071  * to run. Instead of trying to take a spinlock on each of these CPUs,
2072  * which has shown to cause large latency when done on machines with many
2073  * CPUs, sending an IPI to the CPUs to have them push off the overloaded
2074  * RT tasks waiting to run.
2075  *
2076  * Just sending an IPI to each of the CPUs is also an issue, as on large
2077  * count CPU machines, this can cause an IPI storm on a CPU, especially
2078  * if its the only CPU with multiple RT tasks queued, and a large number
2079  * of CPUs scheduling a lower priority task at the same time.
2080  *
2081  * Each root domain has its own irq work function that can iterate over
2082  * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
2083  * tassk must be checked if there's one or many CPUs that are lowering
2084  * their priority, there's a single irq work iterator that will try to
2085  * push off RT tasks that are waiting to run.
2086  *
2087  * When a CPU schedules a lower priority task, it will kick off the
2088  * irq work iterator that will jump to each CPU with overloaded RT tasks.
2089  * As it only takes the first CPU that schedules a lower priority task
2090  * to start the process, the rto_start variable is incremented and if
2091  * the atomic result is one, then that CPU will try to take the rto_lock.
2092  * This prevents high contention on the lock as the process handles all
2093  * CPUs scheduling lower priority tasks.
2094  *
2095  * All CPUs that are scheduling a lower priority task will increment the
2096  * rt_loop_next variable. This will make sure that the irq work iterator
2097  * checks all RT overloaded CPUs whenever a CPU schedules a new lower
2098  * priority task, even if the iterator is in the middle of a scan. Incrementing
2099  * the rt_loop_next will cause the iterator to perform another scan.
2100  *
2101  */
rto_next_cpu(struct root_domain * rd)2102 static int rto_next_cpu(struct root_domain *rd)
2103 {
2104 	int next;
2105 	int cpu;
2106 
2107 	/*
2108 	 * When starting the IPI RT pushing, the rto_cpu is set to -1,
2109 	 * rt_next_cpu() will simply return the first CPU found in
2110 	 * the rto_mask.
2111 	 *
2112 	 * If rto_next_cpu() is called with rto_cpu is a valid CPU, it
2113 	 * will return the next CPU found in the rto_mask.
2114 	 *
2115 	 * If there are no more CPUs left in the rto_mask, then a check is made
2116 	 * against rto_loop and rto_loop_next. rto_loop is only updated with
2117 	 * the rto_lock held, but any CPU may increment the rto_loop_next
2118 	 * without any locking.
2119 	 */
2120 	for (;;) {
2121 
2122 		/* When rto_cpu is -1 this acts like cpumask_first() */
2123 		cpu = cpumask_next(rd->rto_cpu, rd->rto_mask);
2124 
2125 		rd->rto_cpu = cpu;
2126 
2127 		if (cpu < nr_cpu_ids)
2128 			return cpu;
2129 
2130 		rd->rto_cpu = -1;
2131 
2132 		/*
2133 		 * ACQUIRE ensures we see the @rto_mask changes
2134 		 * made prior to the @next value observed.
2135 		 *
2136 		 * Matches WMB in rt_set_overload().
2137 		 */
2138 		next = atomic_read_acquire(&rd->rto_loop_next);
2139 
2140 		if (rd->rto_loop == next)
2141 			break;
2142 
2143 		rd->rto_loop = next;
2144 	}
2145 
2146 	return -1;
2147 }
2148 
rto_start_trylock(atomic_t * v)2149 static inline bool rto_start_trylock(atomic_t *v)
2150 {
2151 	return !atomic_cmpxchg_acquire(v, 0, 1);
2152 }
2153 
rto_start_unlock(atomic_t * v)2154 static inline void rto_start_unlock(atomic_t *v)
2155 {
2156 	atomic_set_release(v, 0);
2157 }
2158 
tell_cpu_to_push(struct rq * rq)2159 static void tell_cpu_to_push(struct rq *rq)
2160 {
2161 	int cpu = -1;
2162 
2163 	/* Keep the loop going if the IPI is currently active */
2164 	atomic_inc(&rq->rd->rto_loop_next);
2165 
2166 	/* Only one CPU can initiate a loop at a time */
2167 	if (!rto_start_trylock(&rq->rd->rto_loop_start))
2168 		return;
2169 
2170 	raw_spin_lock(&rq->rd->rto_lock);
2171 
2172 	/*
2173 	 * The rto_cpu is updated under the lock, if it has a valid CPU
2174 	 * then the IPI is still running and will continue due to the
2175 	 * update to loop_next, and nothing needs to be done here.
2176 	 * Otherwise it is finishing up and an ipi needs to be sent.
2177 	 */
2178 	if (rq->rd->rto_cpu < 0)
2179 		cpu = rto_next_cpu(rq->rd);
2180 
2181 	raw_spin_unlock(&rq->rd->rto_lock);
2182 
2183 	rto_start_unlock(&rq->rd->rto_loop_start);
2184 
2185 	if (cpu >= 0) {
2186 		/* Make sure the rd does not get freed while pushing */
2187 		sched_get_rd(rq->rd);
2188 		irq_work_queue_on(&rq->rd->rto_push_work, cpu);
2189 	}
2190 }
2191 
2192 /* Called from hardirq context */
rto_push_irq_work_func(struct irq_work * work)2193 void rto_push_irq_work_func(struct irq_work *work)
2194 {
2195 	struct root_domain *rd =
2196 		container_of(work, struct root_domain, rto_push_work);
2197 	struct rq *rq;
2198 	int cpu;
2199 
2200 	rq = this_rq();
2201 
2202 	/*
2203 	 * We do not need to grab the lock to check for has_pushable_tasks.
2204 	 * When it gets updated, a check is made if a push is possible.
2205 	 */
2206 	if (has_pushable_tasks(rq)) {
2207 		raw_spin_lock(&rq->lock);
2208 		push_rt_tasks(rq);
2209 		raw_spin_unlock(&rq->lock);
2210 	}
2211 
2212 	raw_spin_lock(&rd->rto_lock);
2213 
2214 	/* Pass the IPI to the next rt overloaded queue */
2215 	cpu = rto_next_cpu(rd);
2216 
2217 	raw_spin_unlock(&rd->rto_lock);
2218 
2219 	if (cpu < 0) {
2220 		sched_put_rd(rd);
2221 		return;
2222 	}
2223 
2224 	/* Try the next RT overloaded CPU */
2225 	irq_work_queue_on(&rd->rto_push_work, cpu);
2226 }
2227 #endif /* HAVE_RT_PUSH_IPI */
2228 
pull_rt_task(struct rq * this_rq)2229 static void pull_rt_task(struct rq *this_rq)
2230 {
2231 	int this_cpu = this_rq->cpu, cpu;
2232 	bool resched = false;
2233 	struct task_struct *p;
2234 	struct rq *src_rq;
2235 	int rt_overload_count = rt_overloaded(this_rq);
2236 
2237 	if (likely(!rt_overload_count))
2238 		return;
2239 
2240 	/*
2241 	 * Match the barrier from rt_set_overloaded; this guarantees that if we
2242 	 * see overloaded we must also see the rto_mask bit.
2243 	 */
2244 	smp_rmb();
2245 
2246 	/* If we are the only overloaded CPU do nothing */
2247 	if (rt_overload_count == 1 &&
2248 	    cpumask_test_cpu(this_rq->cpu, this_rq->rd->rto_mask))
2249 		return;
2250 
2251 #ifdef HAVE_RT_PUSH_IPI
2252 	if (sched_feat(RT_PUSH_IPI)) {
2253 		tell_cpu_to_push(this_rq);
2254 		return;
2255 	}
2256 #endif
2257 
2258 	for_each_cpu(cpu, this_rq->rd->rto_mask) {
2259 		if (this_cpu == cpu)
2260 			continue;
2261 
2262 		src_rq = cpu_rq(cpu);
2263 
2264 		/*
2265 		 * Don't bother taking the src_rq->lock if the next highest
2266 		 * task is known to be lower-priority than our current task.
2267 		 * This may look racy, but if this value is about to go
2268 		 * logically higher, the src_rq will push this task away.
2269 		 * And if its going logically lower, we do not care
2270 		 */
2271 		if (src_rq->rt.highest_prio.next >=
2272 		    this_rq->rt.highest_prio.curr)
2273 			continue;
2274 
2275 		/*
2276 		 * We can potentially drop this_rq's lock in
2277 		 * double_lock_balance, and another CPU could
2278 		 * alter this_rq
2279 		 */
2280 		double_lock_balance(this_rq, src_rq);
2281 
2282 		/*
2283 		 * We can pull only a task, which is pushable
2284 		 * on its rq, and no others.
2285 		 */
2286 		p = pick_highest_pushable_task(src_rq, this_cpu);
2287 
2288 		/*
2289 		 * Do we have an RT task that preempts
2290 		 * the to-be-scheduled task?
2291 		 */
2292 		if (p && (p->prio < this_rq->rt.highest_prio.curr)) {
2293 			WARN_ON(p == src_rq->curr);
2294 			WARN_ON(!task_on_rq_queued(p));
2295 
2296 			/*
2297 			 * There's a chance that p is higher in priority
2298 			 * than what's currently running on its CPU.
2299 			 * This is just that p is wakeing up and hasn't
2300 			 * had a chance to schedule. We only pull
2301 			 * p if it is lower in priority than the
2302 			 * current task on the run queue
2303 			 */
2304 			if (p->prio < src_rq->curr->prio)
2305 				goto skip;
2306 
2307 			resched = true;
2308 
2309 			deactivate_task(src_rq, p, 0);
2310 			set_task_cpu(p, this_cpu);
2311 			activate_task(this_rq, p, 0);
2312 			/*
2313 			 * We continue with the search, just in
2314 			 * case there's an even higher prio task
2315 			 * in another runqueue. (low likelihood
2316 			 * but possible)
2317 			 */
2318 		}
2319 skip:
2320 		double_unlock_balance(this_rq, src_rq);
2321 	}
2322 
2323 	if (resched)
2324 		resched_curr(this_rq);
2325 }
2326 
2327 /*
2328  * If we are not running and we are not going to reschedule soon, we should
2329  * try to push tasks away now
2330  */
task_woken_rt(struct rq * rq,struct task_struct * p)2331 static void task_woken_rt(struct rq *rq, struct task_struct *p)
2332 {
2333 	bool need_to_push = !task_running(rq, p) &&
2334 			    !test_tsk_need_resched(rq->curr) &&
2335 			    p->nr_cpus_allowed > 1 &&
2336 			    (dl_task(rq->curr) || rt_task(rq->curr)) &&
2337 			    (rq->curr->nr_cpus_allowed < 2 ||
2338 			     rq->curr->prio <= p->prio);
2339 
2340 	if (need_to_push)
2341 		push_rt_tasks(rq);
2342 }
2343 
2344 /* Assumes rq->lock is held */
rq_online_rt(struct rq * rq)2345 static void rq_online_rt(struct rq *rq)
2346 {
2347 	if (rq->rt.overloaded)
2348 		rt_set_overload(rq);
2349 
2350 	__enable_runtime(rq);
2351 
2352 	cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
2353 }
2354 
2355 /* Assumes rq->lock is held */
rq_offline_rt(struct rq * rq)2356 static void rq_offline_rt(struct rq *rq)
2357 {
2358 	if (rq->rt.overloaded)
2359 		rt_clear_overload(rq);
2360 
2361 	__disable_runtime(rq);
2362 
2363 	cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID);
2364 }
2365 
2366 /*
2367  * When switch from the rt queue, we bring ourselves to a position
2368  * that we might want to pull RT tasks from other runqueues.
2369  */
switched_from_rt(struct rq * rq,struct task_struct * p)2370 static void switched_from_rt(struct rq *rq, struct task_struct *p)
2371 {
2372 	/*
2373 	 * If there are other RT tasks then we will reschedule
2374 	 * and the scheduling of the other RT tasks will handle
2375 	 * the balancing. But if we are the last RT task
2376 	 * we may need to handle the pulling of RT tasks
2377 	 * now.
2378 	 */
2379 	if (!task_on_rq_queued(p) || rq->rt.rt_nr_running)
2380 		return;
2381 
2382 	rt_queue_pull_task(rq);
2383 }
2384 
init_sched_rt_class(void)2385 void __init init_sched_rt_class(void)
2386 {
2387 	unsigned int i;
2388 
2389 	for_each_possible_cpu(i) {
2390 		zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i),
2391 					GFP_KERNEL, cpu_to_node(i));
2392 	}
2393 }
2394 #endif /* CONFIG_SMP */
2395 
2396 /*
2397  * When switching a task to RT, we may overload the runqueue
2398  * with RT tasks. In this case we try to push them off to
2399  * other runqueues.
2400  */
switched_to_rt(struct rq * rq,struct task_struct * p)2401 static void switched_to_rt(struct rq *rq, struct task_struct *p)
2402 {
2403 	/*
2404 	 * If we are running, update the avg_rt tracking, as the running time
2405 	 * will now on be accounted into the latter.
2406 	 */
2407 	if (task_current(rq, p)) {
2408 		update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2409 		return;
2410 	}
2411 
2412 	/*
2413 	 * If we are not running we may need to preempt the current
2414 	 * running task. If that current running task is also an RT task
2415 	 * then see if we can move to another run queue.
2416 	 */
2417 	if (task_on_rq_queued(p)) {
2418 #ifdef CONFIG_SMP
2419 		if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
2420 			rt_queue_push_tasks(rq);
2421 #endif /* CONFIG_SMP */
2422 		if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))
2423 			resched_curr(rq);
2424 	}
2425 }
2426 
2427 /*
2428  * Priority of the task has changed. This may cause
2429  * us to initiate a push or pull.
2430  */
2431 static void
prio_changed_rt(struct rq * rq,struct task_struct * p,int oldprio)2432 prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
2433 {
2434 	if (!task_on_rq_queued(p))
2435 		return;
2436 
2437 	if (rq->curr == p) {
2438 #ifdef CONFIG_SMP
2439 		/*
2440 		 * If our priority decreases while running, we
2441 		 * may need to pull tasks to this runqueue.
2442 		 */
2443 		if (oldprio < p->prio)
2444 			rt_queue_pull_task(rq);
2445 
2446 		/*
2447 		 * If there's a higher priority task waiting to run
2448 		 * then reschedule.
2449 		 */
2450 		if (p->prio > rq->rt.highest_prio.curr)
2451 			resched_curr(rq);
2452 #else
2453 		/* For UP simply resched on drop of prio */
2454 		if (oldprio < p->prio)
2455 			resched_curr(rq);
2456 #endif /* CONFIG_SMP */
2457 	} else {
2458 		/*
2459 		 * This task is not running, but if it is
2460 		 * greater than the current running task
2461 		 * then reschedule.
2462 		 */
2463 		if (p->prio < rq->curr->prio)
2464 			resched_curr(rq);
2465 	}
2466 }
2467 
2468 #ifdef CONFIG_POSIX_TIMERS
watchdog(struct rq * rq,struct task_struct * p)2469 static void watchdog(struct rq *rq, struct task_struct *p)
2470 {
2471 	unsigned long soft, hard;
2472 
2473 	/* max may change after cur was read, this will be fixed next tick */
2474 	soft = task_rlimit(p, RLIMIT_RTTIME);
2475 	hard = task_rlimit_max(p, RLIMIT_RTTIME);
2476 
2477 	if (soft != RLIM_INFINITY) {
2478 		unsigned long next;
2479 
2480 		if (p->rt.watchdog_stamp != jiffies) {
2481 			p->rt.timeout++;
2482 			p->rt.watchdog_stamp = jiffies;
2483 		}
2484 
2485 		next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ);
2486 		if (p->rt.timeout > next) {
2487 			posix_cputimers_rt_watchdog(&p->posix_cputimers,
2488 						    p->se.sum_exec_runtime);
2489 		}
2490 	}
2491 }
2492 #else
watchdog(struct rq * rq,struct task_struct * p)2493 static inline void watchdog(struct rq *rq, struct task_struct *p) { }
2494 #endif
2495 
2496 /*
2497  * scheduler tick hitting a task of our scheduling class.
2498  *
2499  * NOTE: This function can be called remotely by the tick offload that
2500  * goes along full dynticks. Therefore no local assumption can be made
2501  * and everything must be accessed through the @rq and @curr passed in
2502  * parameters.
2503  */
task_tick_rt(struct rq * rq,struct task_struct * p,int queued)2504 static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
2505 {
2506 	struct sched_rt_entity *rt_se = &p->rt;
2507 
2508 	update_curr_rt(rq);
2509 	update_rt_rq_load_avg(rq_clock_pelt(rq), rq, 1);
2510 
2511 	watchdog(rq, p);
2512 
2513 	/*
2514 	 * RR tasks need a special form of timeslice management.
2515 	 * FIFO tasks have no timeslices.
2516 	 */
2517 	if (p->policy != SCHED_RR)
2518 		return;
2519 
2520 	if (--p->rt.time_slice)
2521 		return;
2522 
2523 	p->rt.time_slice = sched_rr_timeslice;
2524 
2525 	/*
2526 	 * Requeue to the end of queue if we (and all of our ancestors) are not
2527 	 * the only element on the queue
2528 	 */
2529 	for_each_sched_rt_entity(rt_se) {
2530 		if (rt_se->run_list.prev != rt_se->run_list.next) {
2531 			requeue_task_rt(rq, p, 0);
2532 			resched_curr(rq);
2533 			return;
2534 		}
2535 	}
2536 }
2537 
get_rr_interval_rt(struct rq * rq,struct task_struct * task)2538 static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task)
2539 {
2540 	/*
2541 	 * Time slice is 0 for SCHED_FIFO tasks
2542 	 */
2543 	if (task->policy == SCHED_RR)
2544 		return sched_rr_timeslice;
2545 	else
2546 		return 0;
2547 }
2548 
2549 const struct sched_class rt_sched_class
2550 	__section("__rt_sched_class") = {
2551 	.enqueue_task		= enqueue_task_rt,
2552 	.dequeue_task		= dequeue_task_rt,
2553 	.yield_task		= yield_task_rt,
2554 
2555 	.check_preempt_curr	= check_preempt_curr_rt,
2556 
2557 	.pick_next_task		= pick_next_task_rt,
2558 	.put_prev_task		= put_prev_task_rt,
2559 	.set_next_task          = set_next_task_rt,
2560 
2561 #ifdef CONFIG_SMP
2562 	.balance		= balance_rt,
2563 	.select_task_rq		= select_task_rq_rt,
2564 	.set_cpus_allowed       = set_cpus_allowed_common,
2565 	.rq_online              = rq_online_rt,
2566 	.rq_offline             = rq_offline_rt,
2567 	.task_woken		= task_woken_rt,
2568 	.switched_from		= switched_from_rt,
2569 #endif
2570 
2571 	.task_tick		= task_tick_rt,
2572 
2573 	.get_rr_interval	= get_rr_interval_rt,
2574 
2575 	.prio_changed		= prio_changed_rt,
2576 	.switched_to		= switched_to_rt,
2577 
2578 	.update_curr		= update_curr_rt,
2579 
2580 #ifdef CONFIG_UCLAMP_TASK
2581 	.uclamp_enabled		= 1,
2582 #endif
2583 };
2584 
2585 #ifdef CONFIG_RT_GROUP_SCHED
2586 /*
2587  * Ensure that the real time constraints are schedulable.
2588  */
2589 static DEFINE_MUTEX(rt_constraints_mutex);
2590 
tg_has_rt_tasks(struct task_group * tg)2591 static inline int tg_has_rt_tasks(struct task_group *tg)
2592 {
2593 	struct task_struct *task;
2594 	struct css_task_iter it;
2595 	int ret = 0;
2596 
2597 	/*
2598 	 * Autogroups do not have RT tasks; see autogroup_create().
2599 	 */
2600 	if (task_group_is_autogroup(tg))
2601 		return 0;
2602 
2603 	css_task_iter_start(&tg->css, 0, &it);
2604 	while (!ret && (task = css_task_iter_next(&it)))
2605 		ret |= rt_task(task);
2606 	css_task_iter_end(&it);
2607 
2608 	return ret;
2609 }
2610 
2611 struct rt_schedulable_data {
2612 	struct task_group *tg;
2613 	u64 rt_period;
2614 	u64 rt_runtime;
2615 };
2616 
tg_rt_schedulable(struct task_group * tg,void * data)2617 static int tg_rt_schedulable(struct task_group *tg, void *data)
2618 {
2619 	struct rt_schedulable_data *d = data;
2620 	struct task_group *child;
2621 	unsigned long total, sum = 0;
2622 	u64 period, runtime;
2623 
2624 	period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2625 	runtime = tg->rt_bandwidth.rt_runtime;
2626 
2627 	if (tg == d->tg) {
2628 		period = d->rt_period;
2629 		runtime = d->rt_runtime;
2630 	}
2631 
2632 	/*
2633 	 * Cannot have more runtime than the period.
2634 	 */
2635 	if (runtime > period && runtime != RUNTIME_INF)
2636 		return -EINVAL;
2637 
2638 	/*
2639 	 * Ensure we don't starve existing RT tasks if runtime turns zero.
2640 	 */
2641 	if (rt_bandwidth_enabled() && !runtime &&
2642 	    tg->rt_bandwidth.rt_runtime && tg_has_rt_tasks(tg))
2643 		return -EBUSY;
2644 
2645 	total = to_ratio(period, runtime);
2646 
2647 	/*
2648 	 * Nobody can have more than the global setting allows.
2649 	 */
2650 	if (total > to_ratio(global_rt_period(), global_rt_runtime()))
2651 		return -EINVAL;
2652 
2653 	/*
2654 	 * The sum of our children's runtime should not exceed our own.
2655 	 */
2656 	list_for_each_entry_rcu(child, &tg->children, siblings) {
2657 		period = ktime_to_ns(child->rt_bandwidth.rt_period);
2658 		runtime = child->rt_bandwidth.rt_runtime;
2659 
2660 		if (child == d->tg) {
2661 			period = d->rt_period;
2662 			runtime = d->rt_runtime;
2663 		}
2664 
2665 		sum += to_ratio(period, runtime);
2666 	}
2667 
2668 	if (sum > total)
2669 		return -EINVAL;
2670 
2671 	return 0;
2672 }
2673 
__rt_schedulable(struct task_group * tg,u64 period,u64 runtime)2674 static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
2675 {
2676 	int ret;
2677 
2678 	struct rt_schedulable_data data = {
2679 		.tg = tg,
2680 		.rt_period = period,
2681 		.rt_runtime = runtime,
2682 	};
2683 
2684 	rcu_read_lock();
2685 	ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
2686 	rcu_read_unlock();
2687 
2688 	return ret;
2689 }
2690 
tg_set_rt_bandwidth(struct task_group * tg,u64 rt_period,u64 rt_runtime)2691 static int tg_set_rt_bandwidth(struct task_group *tg,
2692 		u64 rt_period, u64 rt_runtime)
2693 {
2694 	int i, err = 0;
2695 
2696 	/*
2697 	 * Disallowing the root group RT runtime is BAD, it would disallow the
2698 	 * kernel creating (and or operating) RT threads.
2699 	 */
2700 	if (tg == &root_task_group && rt_runtime == 0)
2701 		return -EINVAL;
2702 
2703 	/* No period doesn't make any sense. */
2704 	if (rt_period == 0)
2705 		return -EINVAL;
2706 
2707 	/*
2708 	 * Bound quota to defend quota against overflow during bandwidth shift.
2709 	 */
2710 	if (rt_runtime != RUNTIME_INF && rt_runtime > max_rt_runtime)
2711 		return -EINVAL;
2712 
2713 	mutex_lock(&rt_constraints_mutex);
2714 	err = __rt_schedulable(tg, rt_period, rt_runtime);
2715 	if (err)
2716 		goto unlock;
2717 
2718 	raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2719 	tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
2720 	tg->rt_bandwidth.rt_runtime = rt_runtime;
2721 
2722 	for_each_possible_cpu(i) {
2723 		struct rt_rq *rt_rq = tg->rt_rq[i];
2724 
2725 		raw_spin_lock(&rt_rq->rt_runtime_lock);
2726 		rt_rq->rt_runtime = rt_runtime;
2727 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
2728 	}
2729 	raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
2730 unlock:
2731 	mutex_unlock(&rt_constraints_mutex);
2732 
2733 	return err;
2734 }
2735 
sched_group_set_rt_runtime(struct task_group * tg,long rt_runtime_us)2736 int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
2737 {
2738 	u64 rt_runtime, rt_period;
2739 
2740 	rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
2741 	rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
2742 	if (rt_runtime_us < 0)
2743 		rt_runtime = RUNTIME_INF;
2744 	else if ((u64)rt_runtime_us > U64_MAX / NSEC_PER_USEC)
2745 		return -EINVAL;
2746 
2747 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2748 }
2749 
sched_group_rt_runtime(struct task_group * tg)2750 long sched_group_rt_runtime(struct task_group *tg)
2751 {
2752 	u64 rt_runtime_us;
2753 
2754 	if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
2755 		return -1;
2756 
2757 	rt_runtime_us = tg->rt_bandwidth.rt_runtime;
2758 	do_div(rt_runtime_us, NSEC_PER_USEC);
2759 	return rt_runtime_us;
2760 }
2761 
sched_group_set_rt_period(struct task_group * tg,u64 rt_period_us)2762 int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
2763 {
2764 	u64 rt_runtime, rt_period;
2765 
2766 	if (rt_period_us > U64_MAX / NSEC_PER_USEC)
2767 		return -EINVAL;
2768 
2769 	rt_period = rt_period_us * NSEC_PER_USEC;
2770 	rt_runtime = tg->rt_bandwidth.rt_runtime;
2771 
2772 	return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
2773 }
2774 
sched_group_rt_period(struct task_group * tg)2775 long sched_group_rt_period(struct task_group *tg)
2776 {
2777 	u64 rt_period_us;
2778 
2779 	rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
2780 	do_div(rt_period_us, NSEC_PER_USEC);
2781 	return rt_period_us;
2782 }
2783 
sched_rt_global_constraints(void)2784 static int sched_rt_global_constraints(void)
2785 {
2786 	int ret = 0;
2787 
2788 	mutex_lock(&rt_constraints_mutex);
2789 	ret = __rt_schedulable(NULL, 0, 0);
2790 	mutex_unlock(&rt_constraints_mutex);
2791 
2792 	return ret;
2793 }
2794 
sched_rt_can_attach(struct task_group * tg,struct task_struct * tsk)2795 int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
2796 {
2797 	/* Don't accept realtime tasks when there is no way for them to run */
2798 	if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
2799 		return 0;
2800 
2801 	return 1;
2802 }
2803 
2804 #else /* !CONFIG_RT_GROUP_SCHED */
sched_rt_global_constraints(void)2805 static int sched_rt_global_constraints(void)
2806 {
2807 	unsigned long flags;
2808 	int i;
2809 
2810 	raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
2811 	for_each_possible_cpu(i) {
2812 		struct rt_rq *rt_rq = &cpu_rq(i)->rt;
2813 
2814 		raw_spin_lock(&rt_rq->rt_runtime_lock);
2815 		rt_rq->rt_runtime = global_rt_runtime();
2816 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
2817 	}
2818 	raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
2819 
2820 	return 0;
2821 }
2822 #endif /* CONFIG_RT_GROUP_SCHED */
2823 
sched_rt_global_validate(void)2824 static int sched_rt_global_validate(void)
2825 {
2826 	if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
2827 		((sysctl_sched_rt_runtime > sysctl_sched_rt_period) ||
2828 		 ((u64)sysctl_sched_rt_runtime *
2829 			NSEC_PER_USEC > max_rt_runtime)))
2830 		return -EINVAL;
2831 
2832 	return 0;
2833 }
2834 
sched_rt_do_global(void)2835 static void sched_rt_do_global(void)
2836 {
2837 	unsigned long flags;
2838 
2839 	raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
2840 	def_rt_bandwidth.rt_runtime = global_rt_runtime();
2841 	def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
2842 	raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
2843 }
2844 
sched_rt_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)2845 int sched_rt_handler(struct ctl_table *table, int write, void *buffer,
2846 		size_t *lenp, loff_t *ppos)
2847 {
2848 	int old_period, old_runtime;
2849 	static DEFINE_MUTEX(mutex);
2850 	int ret;
2851 
2852 	mutex_lock(&mutex);
2853 	old_period = sysctl_sched_rt_period;
2854 	old_runtime = sysctl_sched_rt_runtime;
2855 
2856 	ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
2857 
2858 	if (!ret && write) {
2859 		ret = sched_rt_global_validate();
2860 		if (ret)
2861 			goto undo;
2862 
2863 		ret = sched_dl_global_validate();
2864 		if (ret)
2865 			goto undo;
2866 
2867 		ret = sched_rt_global_constraints();
2868 		if (ret)
2869 			goto undo;
2870 
2871 		sched_rt_do_global();
2872 		sched_dl_do_global();
2873 	}
2874 	if (0) {
2875 undo:
2876 		sysctl_sched_rt_period = old_period;
2877 		sysctl_sched_rt_runtime = old_runtime;
2878 	}
2879 	mutex_unlock(&mutex);
2880 
2881 	return ret;
2882 }
2883 
sched_rr_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)2884 int sched_rr_handler(struct ctl_table *table, int write, void *buffer,
2885 		size_t *lenp, loff_t *ppos)
2886 {
2887 	int ret;
2888 	static DEFINE_MUTEX(mutex);
2889 
2890 	mutex_lock(&mutex);
2891 	ret = proc_dointvec(table, write, buffer, lenp, ppos);
2892 	/*
2893 	 * Make sure that internally we keep jiffies.
2894 	 * Also, writing zero resets the timeslice to default:
2895 	 */
2896 	if (!ret && write) {
2897 		sched_rr_timeslice =
2898 			sysctl_sched_rr_timeslice <= 0 ? RR_TIMESLICE :
2899 			msecs_to_jiffies(sysctl_sched_rr_timeslice);
2900 
2901 		if (sysctl_sched_rr_timeslice <= 0)
2902 			sysctl_sched_rr_timeslice = jiffies_to_msecs(RR_TIMESLICE);
2903 	}
2904 	mutex_unlock(&mutex);
2905 
2906 	return ret;
2907 }
2908 
2909 #ifdef CONFIG_SCHED_DEBUG
print_rt_stats(struct seq_file * m,int cpu)2910 void print_rt_stats(struct seq_file *m, int cpu)
2911 {
2912 	rt_rq_iter_t iter;
2913 	struct rt_rq *rt_rq;
2914 
2915 	rcu_read_lock();
2916 	for_each_rt_rq(rt_rq, iter, cpu_rq(cpu))
2917 		print_rt_rq(m, cpu, rt_rq);
2918 	rcu_read_unlock();
2919 }
2920 #endif /* CONFIG_SCHED_DEBUG */
2921