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