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