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