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