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