2 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
8 #include <linux/slab.h>
10 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
);
12 struct rt_bandwidth def_rt_bandwidth
;
14 static enum hrtimer_restart
sched_rt_period_timer(struct hrtimer
*timer
)
16 struct rt_bandwidth
*rt_b
=
17 container_of(timer
, struct rt_bandwidth
, rt_period_timer
);
23 now
= hrtimer_cb_get_time(timer
);
24 overrun
= hrtimer_forward(timer
, now
, rt_b
->rt_period
);
29 idle
= do_sched_rt_period_timer(rt_b
, overrun
);
32 return idle
? HRTIMER_NORESTART
: HRTIMER_RESTART
;
35 void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
)
37 rt_b
->rt_period
= ns_to_ktime(period
);
38 rt_b
->rt_runtime
= runtime
;
40 raw_spin_lock_init(&rt_b
->rt_runtime_lock
);
42 hrtimer_init(&rt_b
->rt_period_timer
,
43 CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
44 rt_b
->rt_period_timer
.function
= sched_rt_period_timer
;
47 static void start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
49 if (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
)
52 if (hrtimer_active(&rt_b
->rt_period_timer
))
55 raw_spin_lock(&rt_b
->rt_runtime_lock
);
56 start_bandwidth_timer(&rt_b
->rt_period_timer
, rt_b
->rt_period
);
57 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
60 void init_rt_rq(struct rt_rq
*rt_rq
, struct rq
*rq
)
62 struct rt_prio_array
*array
;
65 array
= &rt_rq
->active
;
66 for (i
= 0; i
< MAX_RT_PRIO
; i
++) {
67 INIT_LIST_HEAD(array
->queue
+ i
);
68 __clear_bit(i
, array
->bitmap
);
70 /* delimiter for bitsearch: */
71 __set_bit(MAX_RT_PRIO
, array
->bitmap
);
73 #if defined CONFIG_SMP
74 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
75 rt_rq
->highest_prio
.next
= MAX_RT_PRIO
;
76 rt_rq
->rt_nr_migratory
= 0;
77 rt_rq
->overloaded
= 0;
78 plist_head_init(&rt_rq
->pushable_tasks
);
82 rt_rq
->rt_throttled
= 0;
83 rt_rq
->rt_runtime
= 0;
84 raw_spin_lock_init(&rt_rq
->rt_runtime_lock
);
87 #ifdef CONFIG_RT_GROUP_SCHED
88 static void destroy_rt_bandwidth(struct rt_bandwidth
*rt_b
)
90 hrtimer_cancel(&rt_b
->rt_period_timer
);
93 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
95 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
97 #ifdef CONFIG_SCHED_DEBUG
98 WARN_ON_ONCE(!rt_entity_is_task(rt_se
));
100 return container_of(rt_se
, struct task_struct
, rt
);
103 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
108 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
113 void free_rt_sched_group(struct task_group
*tg
)
118 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
120 for_each_possible_cpu(i
) {
131 void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
132 struct sched_rt_entity
*rt_se
, int cpu
,
133 struct sched_rt_entity
*parent
)
135 struct rq
*rq
= cpu_rq(cpu
);
137 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
138 rt_rq
->rt_nr_boosted
= 0;
142 tg
->rt_rq
[cpu
] = rt_rq
;
143 tg
->rt_se
[cpu
] = rt_se
;
149 rt_se
->rt_rq
= &rq
->rt
;
151 rt_se
->rt_rq
= parent
->my_q
;
154 rt_se
->parent
= parent
;
155 INIT_LIST_HEAD(&rt_se
->run_list
);
158 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
161 struct sched_rt_entity
*rt_se
;
164 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
167 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
171 init_rt_bandwidth(&tg
->rt_bandwidth
,
172 ktime_to_ns(def_rt_bandwidth
.rt_period
), 0);
174 for_each_possible_cpu(i
) {
175 rt_rq
= kzalloc_node(sizeof(struct rt_rq
),
176 GFP_KERNEL
, cpu_to_node(i
));
180 rt_se
= kzalloc_node(sizeof(struct sched_rt_entity
),
181 GFP_KERNEL
, cpu_to_node(i
));
185 init_rt_rq(rt_rq
, cpu_rq(i
));
186 rt_rq
->rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
187 init_tg_rt_entry(tg
, rt_rq
, rt_se
, i
, parent
->rt_se
[i
]);
198 #else /* CONFIG_RT_GROUP_SCHED */
200 #define rt_entity_is_task(rt_se) (1)
202 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
204 return container_of(rt_se
, struct task_struct
, rt
);
207 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
209 return container_of(rt_rq
, struct rq
, rt
);
212 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
214 struct task_struct
*p
= rt_task_of(rt_se
);
215 struct rq
*rq
= task_rq(p
);
220 void free_rt_sched_group(struct task_group
*tg
) { }
222 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
226 #endif /* CONFIG_RT_GROUP_SCHED */
230 static inline int rt_overloaded(struct rq
*rq
)
232 return atomic_read(&rq
->rd
->rto_count
);
235 static inline void rt_set_overload(struct rq
*rq
)
240 cpumask_set_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
242 * Make sure the mask is visible before we set
243 * the overload count. That is checked to determine
244 * if we should look at the mask. It would be a shame
245 * if we looked at the mask, but the mask was not
249 atomic_inc(&rq
->rd
->rto_count
);
252 static inline void rt_clear_overload(struct rq
*rq
)
257 /* the order here really doesn't matter */
258 atomic_dec(&rq
->rd
->rto_count
);
259 cpumask_clear_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
262 static void update_rt_migration(struct rt_rq
*rt_rq
)
264 if (rt_rq
->rt_nr_migratory
&& rt_rq
->rt_nr_total
> 1) {
265 if (!rt_rq
->overloaded
) {
266 rt_set_overload(rq_of_rt_rq(rt_rq
));
267 rt_rq
->overloaded
= 1;
269 } else if (rt_rq
->overloaded
) {
270 rt_clear_overload(rq_of_rt_rq(rt_rq
));
271 rt_rq
->overloaded
= 0;
275 static void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
277 if (!rt_entity_is_task(rt_se
))
280 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
282 rt_rq
->rt_nr_total
++;
283 if (rt_se
->nr_cpus_allowed
> 1)
284 rt_rq
->rt_nr_migratory
++;
286 update_rt_migration(rt_rq
);
289 static void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
291 if (!rt_entity_is_task(rt_se
))
294 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
296 rt_rq
->rt_nr_total
--;
297 if (rt_se
->nr_cpus_allowed
> 1)
298 rt_rq
->rt_nr_migratory
--;
300 update_rt_migration(rt_rq
);
303 static inline int has_pushable_tasks(struct rq
*rq
)
305 return !plist_head_empty(&rq
->rt
.pushable_tasks
);
308 static void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
310 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
311 plist_node_init(&p
->pushable_tasks
, p
->prio
);
312 plist_add(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
314 /* Update the highest prio pushable task */
315 if (p
->prio
< rq
->rt
.highest_prio
.next
)
316 rq
->rt
.highest_prio
.next
= p
->prio
;
319 static void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
321 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
323 /* Update the new highest prio pushable task */
324 if (has_pushable_tasks(rq
)) {
325 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
326 struct task_struct
, pushable_tasks
);
327 rq
->rt
.highest_prio
.next
= p
->prio
;
329 rq
->rt
.highest_prio
.next
= MAX_RT_PRIO
;
334 static inline void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
338 static inline void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
343 void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
348 void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
352 #endif /* CONFIG_SMP */
354 static inline int on_rt_rq(struct sched_rt_entity
*rt_se
)
356 return !list_empty(&rt_se
->run_list
);
359 #ifdef CONFIG_RT_GROUP_SCHED
361 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
366 return rt_rq
->rt_runtime
;
369 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
371 return ktime_to_ns(rt_rq
->tg
->rt_bandwidth
.rt_period
);
374 typedef struct task_group
*rt_rq_iter_t
;
376 static inline struct task_group
*next_task_group(struct task_group
*tg
)
379 tg
= list_entry_rcu(tg
->list
.next
,
380 typeof(struct task_group
), list
);
381 } while (&tg
->list
!= &task_groups
&& task_group_is_autogroup(tg
));
383 if (&tg
->list
== &task_groups
)
389 #define for_each_rt_rq(rt_rq, iter, rq) \
390 for (iter = container_of(&task_groups, typeof(*iter), list); \
391 (iter = next_task_group(iter)) && \
392 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
394 static inline void list_add_leaf_rt_rq(struct rt_rq
*rt_rq
)
396 list_add_rcu(&rt_rq
->leaf_rt_rq_list
,
397 &rq_of_rt_rq(rt_rq
)->leaf_rt_rq_list
);
400 static inline void list_del_leaf_rt_rq(struct rt_rq
*rt_rq
)
402 list_del_rcu(&rt_rq
->leaf_rt_rq_list
);
405 #define for_each_leaf_rt_rq(rt_rq, rq) \
406 list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
408 #define for_each_sched_rt_entity(rt_se) \
409 for (; rt_se; rt_se = rt_se->parent)
411 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
416 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, bool head
);
417 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
);
419 static void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
421 struct task_struct
*curr
= rq_of_rt_rq(rt_rq
)->curr
;
422 struct sched_rt_entity
*rt_se
;
424 int cpu
= cpu_of(rq_of_rt_rq(rt_rq
));
426 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
428 if (rt_rq
->rt_nr_running
) {
429 if (rt_se
&& !on_rt_rq(rt_se
))
430 enqueue_rt_entity(rt_se
, false);
431 if (rt_rq
->highest_prio
.curr
< curr
->prio
)
436 static void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
438 struct sched_rt_entity
*rt_se
;
439 int cpu
= cpu_of(rq_of_rt_rq(rt_rq
));
441 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
443 if (rt_se
&& on_rt_rq(rt_se
))
444 dequeue_rt_entity(rt_se
);
447 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
449 return rt_rq
->rt_throttled
&& !rt_rq
->rt_nr_boosted
;
452 static int rt_se_boosted(struct sched_rt_entity
*rt_se
)
454 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
455 struct task_struct
*p
;
458 return !!rt_rq
->rt_nr_boosted
;
460 p
= rt_task_of(rt_se
);
461 return p
->prio
!= p
->normal_prio
;
465 static inline const struct cpumask
*sched_rt_period_mask(void)
467 return cpu_rq(smp_processor_id())->rd
->span
;
470 static inline const struct cpumask
*sched_rt_period_mask(void)
472 return cpu_online_mask
;
477 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
479 return container_of(rt_b
, struct task_group
, rt_bandwidth
)->rt_rq
[cpu
];
482 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
484 return &rt_rq
->tg
->rt_bandwidth
;
487 #else /* !CONFIG_RT_GROUP_SCHED */
489 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
491 return rt_rq
->rt_runtime
;
494 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
496 return ktime_to_ns(def_rt_bandwidth
.rt_period
);
499 typedef struct rt_rq
*rt_rq_iter_t
;
501 #define for_each_rt_rq(rt_rq, iter, rq) \
502 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
504 static inline void list_add_leaf_rt_rq(struct rt_rq
*rt_rq
)
508 static inline void list_del_leaf_rt_rq(struct rt_rq
*rt_rq
)
512 #define for_each_leaf_rt_rq(rt_rq, rq) \
513 for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
515 #define for_each_sched_rt_entity(rt_se) \
516 for (; rt_se; rt_se = NULL)
518 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
523 static inline void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
525 if (rt_rq
->rt_nr_running
)
526 resched_task(rq_of_rt_rq(rt_rq
)->curr
);
529 static inline void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
533 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
535 return rt_rq
->rt_throttled
;
538 static inline const struct cpumask
*sched_rt_period_mask(void)
540 return cpu_online_mask
;
544 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
546 return &cpu_rq(cpu
)->rt
;
549 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
551 return &def_rt_bandwidth
;
554 #endif /* CONFIG_RT_GROUP_SCHED */
558 * We ran out of runtime, see if we can borrow some from our neighbours.
560 static int do_balance_runtime(struct rt_rq
*rt_rq
)
562 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
563 struct root_domain
*rd
= cpu_rq(smp_processor_id())->rd
;
564 int i
, weight
, more
= 0;
567 weight
= cpumask_weight(rd
->span
);
569 raw_spin_lock(&rt_b
->rt_runtime_lock
);
570 rt_period
= ktime_to_ns(rt_b
->rt_period
);
571 for_each_cpu(i
, rd
->span
) {
572 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
578 raw_spin_lock(&iter
->rt_runtime_lock
);
580 * Either all rqs have inf runtime and there's nothing to steal
581 * or __disable_runtime() below sets a specific rq to inf to
582 * indicate its been disabled and disalow stealing.
584 if (iter
->rt_runtime
== RUNTIME_INF
)
588 * From runqueues with spare time, take 1/n part of their
589 * spare time, but no more than our period.
591 diff
= iter
->rt_runtime
- iter
->rt_time
;
593 diff
= div_u64((u64
)diff
, weight
);
594 if (rt_rq
->rt_runtime
+ diff
> rt_period
)
595 diff
= rt_period
- rt_rq
->rt_runtime
;
596 iter
->rt_runtime
-= diff
;
597 rt_rq
->rt_runtime
+= diff
;
599 if (rt_rq
->rt_runtime
== rt_period
) {
600 raw_spin_unlock(&iter
->rt_runtime_lock
);
605 raw_spin_unlock(&iter
->rt_runtime_lock
);
607 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
613 * Ensure this RQ takes back all the runtime it lend to its neighbours.
615 static void __disable_runtime(struct rq
*rq
)
617 struct root_domain
*rd
= rq
->rd
;
621 if (unlikely(!scheduler_running
))
624 for_each_rt_rq(rt_rq
, iter
, rq
) {
625 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
629 raw_spin_lock(&rt_b
->rt_runtime_lock
);
630 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
632 * Either we're all inf and nobody needs to borrow, or we're
633 * already disabled and thus have nothing to do, or we have
634 * exactly the right amount of runtime to take out.
636 if (rt_rq
->rt_runtime
== RUNTIME_INF
||
637 rt_rq
->rt_runtime
== rt_b
->rt_runtime
)
639 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
642 * Calculate the difference between what we started out with
643 * and what we current have, that's the amount of runtime
644 * we lend and now have to reclaim.
646 want
= rt_b
->rt_runtime
- rt_rq
->rt_runtime
;
649 * Greedy reclaim, take back as much as we can.
651 for_each_cpu(i
, rd
->span
) {
652 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
656 * Can't reclaim from ourselves or disabled runqueues.
658 if (iter
== rt_rq
|| iter
->rt_runtime
== RUNTIME_INF
)
661 raw_spin_lock(&iter
->rt_runtime_lock
);
663 diff
= min_t(s64
, iter
->rt_runtime
, want
);
664 iter
->rt_runtime
-= diff
;
667 iter
->rt_runtime
-= want
;
670 raw_spin_unlock(&iter
->rt_runtime_lock
);
676 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
678 * We cannot be left wanting - that would mean some runtime
679 * leaked out of the system.
684 * Disable all the borrow logic by pretending we have inf
685 * runtime - in which case borrowing doesn't make sense.
687 rt_rq
->rt_runtime
= RUNTIME_INF
;
688 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
689 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
693 static void disable_runtime(struct rq
*rq
)
697 raw_spin_lock_irqsave(&rq
->lock
, flags
);
698 __disable_runtime(rq
);
699 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
702 static void __enable_runtime(struct rq
*rq
)
707 if (unlikely(!scheduler_running
))
711 * Reset each runqueue's bandwidth settings
713 for_each_rt_rq(rt_rq
, iter
, rq
) {
714 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
716 raw_spin_lock(&rt_b
->rt_runtime_lock
);
717 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
718 rt_rq
->rt_runtime
= rt_b
->rt_runtime
;
720 rt_rq
->rt_throttled
= 0;
721 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
722 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
726 static void enable_runtime(struct rq
*rq
)
730 raw_spin_lock_irqsave(&rq
->lock
, flags
);
731 __enable_runtime(rq
);
732 raw_spin_unlock_irqrestore(&rq
->lock
, flags
);
735 int update_runtime(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
737 int cpu
= (int)(long)hcpu
;
740 case CPU_DOWN_PREPARE
:
741 case CPU_DOWN_PREPARE_FROZEN
:
742 disable_runtime(cpu_rq(cpu
));
745 case CPU_DOWN_FAILED
:
746 case CPU_DOWN_FAILED_FROZEN
:
748 case CPU_ONLINE_FROZEN
:
749 enable_runtime(cpu_rq(cpu
));
757 static int balance_runtime(struct rt_rq
*rt_rq
)
761 if (!sched_feat(RT_RUNTIME_SHARE
))
764 if (rt_rq
->rt_time
> rt_rq
->rt_runtime
) {
765 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
766 more
= do_balance_runtime(rt_rq
);
767 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
772 #else /* !CONFIG_SMP */
773 static inline int balance_runtime(struct rt_rq
*rt_rq
)
777 #endif /* CONFIG_SMP */
779 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
)
781 int i
, idle
= 1, throttled
= 0;
782 const struct cpumask
*span
;
784 span
= sched_rt_period_mask();
785 for_each_cpu(i
, span
) {
787 struct rt_rq
*rt_rq
= sched_rt_period_rt_rq(rt_b
, i
);
788 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
790 raw_spin_lock(&rq
->lock
);
791 if (rt_rq
->rt_time
) {
794 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
795 if (rt_rq
->rt_throttled
)
796 balance_runtime(rt_rq
);
797 runtime
= rt_rq
->rt_runtime
;
798 rt_rq
->rt_time
-= min(rt_rq
->rt_time
, overrun
*runtime
);
799 if (rt_rq
->rt_throttled
&& rt_rq
->rt_time
< runtime
) {
800 rt_rq
->rt_throttled
= 0;
804 * Force a clock update if the CPU was idle,
805 * lest wakeup -> unthrottle time accumulate.
807 if (rt_rq
->rt_nr_running
&& rq
->curr
== rq
->idle
)
808 rq
->skip_clock_update
= -1;
810 if (rt_rq
->rt_time
|| rt_rq
->rt_nr_running
)
812 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
813 } else if (rt_rq
->rt_nr_running
) {
815 if (!rt_rq_throttled(rt_rq
))
818 if (rt_rq
->rt_throttled
)
822 sched_rt_rq_enqueue(rt_rq
);
823 raw_spin_unlock(&rq
->lock
);
826 if (!throttled
&& (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
))
832 static inline int rt_se_prio(struct sched_rt_entity
*rt_se
)
834 #ifdef CONFIG_RT_GROUP_SCHED
835 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
838 return rt_rq
->highest_prio
.curr
;
841 return rt_task_of(rt_se
)->prio
;
844 static int sched_rt_runtime_exceeded(struct rt_rq
*rt_rq
)
846 u64 runtime
= sched_rt_runtime(rt_rq
);
848 if (rt_rq
->rt_throttled
)
849 return rt_rq_throttled(rt_rq
);
851 if (runtime
>= sched_rt_period(rt_rq
))
854 balance_runtime(rt_rq
);
855 runtime
= sched_rt_runtime(rt_rq
);
856 if (runtime
== RUNTIME_INF
)
859 if (rt_rq
->rt_time
> runtime
) {
860 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
863 * Don't actually throttle groups that have no runtime assigned
864 * but accrue some time due to boosting.
866 if (likely(rt_b
->rt_runtime
)) {
867 static bool once
= false;
869 rt_rq
->rt_throttled
= 1;
873 printk_sched("sched: RT throttling activated\n");
877 * In case we did anyway, make it go away,
878 * replenishment is a joke, since it will replenish us
884 if (rt_rq_throttled(rt_rq
)) {
885 sched_rt_rq_dequeue(rt_rq
);
894 * Update the current task's runtime statistics. Skip current tasks that
895 * are not in our scheduling class.
897 static void update_curr_rt(struct rq
*rq
)
899 struct task_struct
*curr
= rq
->curr
;
900 struct sched_rt_entity
*rt_se
= &curr
->rt
;
901 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
904 if (curr
->sched_class
!= &rt_sched_class
)
907 delta_exec
= rq
->clock_task
- curr
->se
.exec_start
;
908 if (unlikely((s64
)delta_exec
< 0))
911 schedstat_set(curr
->se
.statistics
.exec_max
,
912 max(curr
->se
.statistics
.exec_max
, delta_exec
));
914 curr
->se
.sum_exec_runtime
+= delta_exec
;
915 account_group_exec_runtime(curr
, delta_exec
);
917 curr
->se
.exec_start
= rq
->clock_task
;
918 cpuacct_charge(curr
, delta_exec
);
920 sched_rt_avg_update(rq
, delta_exec
);
922 if (!rt_bandwidth_enabled())
925 for_each_sched_rt_entity(rt_se
) {
926 rt_rq
= rt_rq_of_se(rt_se
);
928 if (sched_rt_runtime(rt_rq
) != RUNTIME_INF
) {
929 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
930 rt_rq
->rt_time
+= delta_exec
;
931 if (sched_rt_runtime_exceeded(rt_rq
))
933 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
938 #if defined CONFIG_SMP
941 inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
943 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
945 if (rq
->online
&& prio
< prev_prio
)
946 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, prio
);
950 dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
952 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
954 if (rq
->online
&& rt_rq
->highest_prio
.curr
!= prev_prio
)
955 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rt_rq
->highest_prio
.curr
);
958 #else /* CONFIG_SMP */
961 void inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
963 void dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
965 #endif /* CONFIG_SMP */
967 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
969 inc_rt_prio(struct rt_rq
*rt_rq
, int prio
)
971 int prev_prio
= rt_rq
->highest_prio
.curr
;
973 if (prio
< prev_prio
)
974 rt_rq
->highest_prio
.curr
= prio
;
976 inc_rt_prio_smp(rt_rq
, prio
, prev_prio
);
980 dec_rt_prio(struct rt_rq
*rt_rq
, int prio
)
982 int prev_prio
= rt_rq
->highest_prio
.curr
;
984 if (rt_rq
->rt_nr_running
) {
986 WARN_ON(prio
< prev_prio
);
989 * This may have been our highest task, and therefore
990 * we may have some recomputation to do
992 if (prio
== prev_prio
) {
993 struct rt_prio_array
*array
= &rt_rq
->active
;
995 rt_rq
->highest_prio
.curr
=
996 sched_find_first_bit(array
->bitmap
);
1000 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
1002 dec_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1007 static inline void inc_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1008 static inline void dec_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1010 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1012 #ifdef CONFIG_RT_GROUP_SCHED
1015 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1017 if (rt_se_boosted(rt_se
))
1018 rt_rq
->rt_nr_boosted
++;
1021 start_rt_bandwidth(&rt_rq
->tg
->rt_bandwidth
);
1025 dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1027 if (rt_se_boosted(rt_se
))
1028 rt_rq
->rt_nr_boosted
--;
1030 WARN_ON(!rt_rq
->rt_nr_running
&& rt_rq
->rt_nr_boosted
);
1033 #else /* CONFIG_RT_GROUP_SCHED */
1036 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1038 start_rt_bandwidth(&def_rt_bandwidth
);
1042 void dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
) {}
1044 #endif /* CONFIG_RT_GROUP_SCHED */
1047 void inc_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1049 int prio
= rt_se_prio(rt_se
);
1051 WARN_ON(!rt_prio(prio
));
1052 rt_rq
->rt_nr_running
++;
1054 inc_rt_prio(rt_rq
, prio
);
1055 inc_rt_migration(rt_se
, rt_rq
);
1056 inc_rt_group(rt_se
, rt_rq
);
1060 void dec_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1062 WARN_ON(!rt_prio(rt_se_prio(rt_se
)));
1063 WARN_ON(!rt_rq
->rt_nr_running
);
1064 rt_rq
->rt_nr_running
--;
1066 dec_rt_prio(rt_rq
, rt_se_prio(rt_se
));
1067 dec_rt_migration(rt_se
, rt_rq
);
1068 dec_rt_group(rt_se
, rt_rq
);
1071 static void __enqueue_rt_entity(struct sched_rt_entity
*rt_se
, bool head
)
1073 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1074 struct rt_prio_array
*array
= &rt_rq
->active
;
1075 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1076 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1079 * Don't enqueue the group if its throttled, or when empty.
1080 * The latter is a consequence of the former when a child group
1081 * get throttled and the current group doesn't have any other
1084 if (group_rq
&& (rt_rq_throttled(group_rq
) || !group_rq
->rt_nr_running
))
1087 if (!rt_rq
->rt_nr_running
)
1088 list_add_leaf_rt_rq(rt_rq
);
1091 list_add(&rt_se
->run_list
, queue
);
1093 list_add_tail(&rt_se
->run_list
, queue
);
1094 __set_bit(rt_se_prio(rt_se
), array
->bitmap
);
1096 inc_rt_tasks(rt_se
, rt_rq
);
1099 static void __dequeue_rt_entity(struct sched_rt_entity
*rt_se
)
1101 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1102 struct rt_prio_array
*array
= &rt_rq
->active
;
1104 list_del_init(&rt_se
->run_list
);
1105 if (list_empty(array
->queue
+ rt_se_prio(rt_se
)))
1106 __clear_bit(rt_se_prio(rt_se
), array
->bitmap
);
1108 dec_rt_tasks(rt_se
, rt_rq
);
1109 if (!rt_rq
->rt_nr_running
)
1110 list_del_leaf_rt_rq(rt_rq
);
1114 * Because the prio of an upper entry depends on the lower
1115 * entries, we must remove entries top - down.
1117 static void dequeue_rt_stack(struct sched_rt_entity
*rt_se
)
1119 struct sched_rt_entity
*back
= NULL
;
1121 for_each_sched_rt_entity(rt_se
) {
1126 for (rt_se
= back
; rt_se
; rt_se
= rt_se
->back
) {
1127 if (on_rt_rq(rt_se
))
1128 __dequeue_rt_entity(rt_se
);
1132 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, bool head
)
1134 dequeue_rt_stack(rt_se
);
1135 for_each_sched_rt_entity(rt_se
)
1136 __enqueue_rt_entity(rt_se
, head
);
1139 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
)
1141 dequeue_rt_stack(rt_se
);
1143 for_each_sched_rt_entity(rt_se
) {
1144 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1146 if (rt_rq
&& rt_rq
->rt_nr_running
)
1147 __enqueue_rt_entity(rt_se
, false);
1152 * Adding/removing a task to/from a priority array:
1155 enqueue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1157 struct sched_rt_entity
*rt_se
= &p
->rt
;
1159 if (flags
& ENQUEUE_WAKEUP
)
1162 enqueue_rt_entity(rt_se
, flags
& ENQUEUE_HEAD
);
1164 if (!task_current(rq
, p
) && p
->rt
.nr_cpus_allowed
> 1)
1165 enqueue_pushable_task(rq
, p
);
1170 static void dequeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1172 struct sched_rt_entity
*rt_se
= &p
->rt
;
1175 dequeue_rt_entity(rt_se
);
1177 dequeue_pushable_task(rq
, p
);
1183 * Put task to the head or the end of the run list without the overhead of
1184 * dequeue followed by enqueue.
1187 requeue_rt_entity(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
, int head
)
1189 if (on_rt_rq(rt_se
)) {
1190 struct rt_prio_array
*array
= &rt_rq
->active
;
1191 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1194 list_move(&rt_se
->run_list
, queue
);
1196 list_move_tail(&rt_se
->run_list
, queue
);
1200 static void requeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int head
)
1202 struct sched_rt_entity
*rt_se
= &p
->rt
;
1203 struct rt_rq
*rt_rq
;
1205 for_each_sched_rt_entity(rt_se
) {
1206 rt_rq
= rt_rq_of_se(rt_se
);
1207 requeue_rt_entity(rt_rq
, rt_se
, head
);
1211 static void yield_task_rt(struct rq
*rq
)
1213 requeue_task_rt(rq
, rq
->curr
, 0);
1217 static int find_lowest_rq(struct task_struct
*task
);
1220 select_task_rq_rt(struct task_struct
*p
, int sd_flag
, int flags
)
1222 struct task_struct
*curr
;
1228 if (p
->rt
.nr_cpus_allowed
== 1)
1231 /* For anything but wake ups, just return the task_cpu */
1232 if (sd_flag
!= SD_BALANCE_WAKE
&& sd_flag
!= SD_BALANCE_FORK
)
1238 curr
= ACCESS_ONCE(rq
->curr
); /* unlocked access */
1241 * If the current task on @p's runqueue is an RT task, then
1242 * try to see if we can wake this RT task up on another
1243 * runqueue. Otherwise simply start this RT task
1244 * on its current runqueue.
1246 * We want to avoid overloading runqueues. If the woken
1247 * task is a higher priority, then it will stay on this CPU
1248 * and the lower prio task should be moved to another CPU.
1249 * Even though this will probably make the lower prio task
1250 * lose its cache, we do not want to bounce a higher task
1251 * around just because it gave up its CPU, perhaps for a
1254 * For equal prio tasks, we just let the scheduler sort it out.
1256 * Otherwise, just let it ride on the affined RQ and the
1257 * post-schedule router will push the preempted task away
1259 * This test is optimistic, if we get it wrong the load-balancer
1260 * will have to sort it out.
1262 if (curr
&& unlikely(rt_task(curr
)) &&
1263 (curr
->rt
.nr_cpus_allowed
< 2 ||
1264 curr
->prio
<= p
->prio
) &&
1265 (p
->rt
.nr_cpus_allowed
> 1)) {
1266 int target
= find_lowest_rq(p
);
1277 static void check_preempt_equal_prio(struct rq
*rq
, struct task_struct
*p
)
1279 if (rq
->curr
->rt
.nr_cpus_allowed
== 1)
1282 if (p
->rt
.nr_cpus_allowed
!= 1
1283 && cpupri_find(&rq
->rd
->cpupri
, p
, NULL
))
1286 if (!cpupri_find(&rq
->rd
->cpupri
, rq
->curr
, NULL
))
1290 * There appears to be other cpus that can accept
1291 * current and none to run 'p', so lets reschedule
1292 * to try and push current away:
1294 requeue_task_rt(rq
, p
, 1);
1295 resched_task(rq
->curr
);
1298 #endif /* CONFIG_SMP */
1301 * Preempt the current task with a newly woken task if needed:
1303 static void check_preempt_curr_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1305 if (p
->prio
< rq
->curr
->prio
) {
1306 resched_task(rq
->curr
);
1314 * - the newly woken task is of equal priority to the current task
1315 * - the newly woken task is non-migratable while current is migratable
1316 * - current will be preempted on the next reschedule
1318 * we should check to see if current can readily move to a different
1319 * cpu. If so, we will reschedule to allow the push logic to try
1320 * to move current somewhere else, making room for our non-migratable
1323 if (p
->prio
== rq
->curr
->prio
&& !test_tsk_need_resched(rq
->curr
))
1324 check_preempt_equal_prio(rq
, p
);
1328 static struct sched_rt_entity
*pick_next_rt_entity(struct rq
*rq
,
1329 struct rt_rq
*rt_rq
)
1331 struct rt_prio_array
*array
= &rt_rq
->active
;
1332 struct sched_rt_entity
*next
= NULL
;
1333 struct list_head
*queue
;
1336 idx
= sched_find_first_bit(array
->bitmap
);
1337 BUG_ON(idx
>= MAX_RT_PRIO
);
1339 queue
= array
->queue
+ idx
;
1340 next
= list_entry(queue
->next
, struct sched_rt_entity
, run_list
);
1345 static struct task_struct
*_pick_next_task_rt(struct rq
*rq
)
1347 struct sched_rt_entity
*rt_se
;
1348 struct task_struct
*p
;
1349 struct rt_rq
*rt_rq
;
1353 if (!rt_rq
->rt_nr_running
)
1356 if (rt_rq_throttled(rt_rq
))
1360 rt_se
= pick_next_rt_entity(rq
, rt_rq
);
1362 rt_rq
= group_rt_rq(rt_se
);
1365 p
= rt_task_of(rt_se
);
1366 p
->se
.exec_start
= rq
->clock_task
;
1371 static struct task_struct
*pick_next_task_rt(struct rq
*rq
)
1373 struct task_struct
*p
= _pick_next_task_rt(rq
);
1375 /* The running task is never eligible for pushing */
1377 dequeue_pushable_task(rq
, p
);
1381 * We detect this state here so that we can avoid taking the RQ
1382 * lock again later if there is no need to push
1384 rq
->post_schedule
= has_pushable_tasks(rq
);
1390 static void put_prev_task_rt(struct rq
*rq
, struct task_struct
*p
)
1395 * The previous task needs to be made eligible for pushing
1396 * if it is still active
1398 if (on_rt_rq(&p
->rt
) && p
->rt
.nr_cpus_allowed
> 1)
1399 enqueue_pushable_task(rq
, p
);
1404 /* Only try algorithms three times */
1405 #define RT_MAX_TRIES 3
1407 static int pick_rt_task(struct rq
*rq
, struct task_struct
*p
, int cpu
)
1409 if (!task_running(rq
, p
) &&
1410 (cpu
< 0 || cpumask_test_cpu(cpu
, tsk_cpus_allowed(p
))) &&
1411 (p
->rt
.nr_cpus_allowed
> 1))
1416 /* Return the second highest RT task, NULL otherwise */
1417 static struct task_struct
*pick_next_highest_task_rt(struct rq
*rq
, int cpu
)
1419 struct task_struct
*next
= NULL
;
1420 struct sched_rt_entity
*rt_se
;
1421 struct rt_prio_array
*array
;
1422 struct rt_rq
*rt_rq
;
1425 for_each_leaf_rt_rq(rt_rq
, rq
) {
1426 array
= &rt_rq
->active
;
1427 idx
= sched_find_first_bit(array
->bitmap
);
1429 if (idx
>= MAX_RT_PRIO
)
1431 if (next
&& next
->prio
<= idx
)
1433 list_for_each_entry(rt_se
, array
->queue
+ idx
, run_list
) {
1434 struct task_struct
*p
;
1436 if (!rt_entity_is_task(rt_se
))
1439 p
= rt_task_of(rt_se
);
1440 if (pick_rt_task(rq
, p
, cpu
)) {
1446 idx
= find_next_bit(array
->bitmap
, MAX_RT_PRIO
, idx
+1);
1454 static DEFINE_PER_CPU(cpumask_var_t
, local_cpu_mask
);
1456 static int find_lowest_rq(struct task_struct
*task
)
1458 struct sched_domain
*sd
;
1459 struct cpumask
*lowest_mask
= __get_cpu_var(local_cpu_mask
);
1460 int this_cpu
= smp_processor_id();
1461 int cpu
= task_cpu(task
);
1463 /* Make sure the mask is initialized first */
1464 if (unlikely(!lowest_mask
))
1467 if (task
->rt
.nr_cpus_allowed
== 1)
1468 return -1; /* No other targets possible */
1470 if (!cpupri_find(&task_rq(task
)->rd
->cpupri
, task
, lowest_mask
))
1471 return -1; /* No targets found */
1474 * At this point we have built a mask of cpus representing the
1475 * lowest priority tasks in the system. Now we want to elect
1476 * the best one based on our affinity and topology.
1478 * We prioritize the last cpu that the task executed on since
1479 * it is most likely cache-hot in that location.
1481 if (cpumask_test_cpu(cpu
, lowest_mask
))
1485 * Otherwise, we consult the sched_domains span maps to figure
1486 * out which cpu is logically closest to our hot cache data.
1488 if (!cpumask_test_cpu(this_cpu
, lowest_mask
))
1489 this_cpu
= -1; /* Skip this_cpu opt if not among lowest */
1492 for_each_domain(cpu
, sd
) {
1493 if (sd
->flags
& SD_WAKE_AFFINE
) {
1497 * "this_cpu" is cheaper to preempt than a
1500 if (this_cpu
!= -1 &&
1501 cpumask_test_cpu(this_cpu
, sched_domain_span(sd
))) {
1506 best_cpu
= cpumask_first_and(lowest_mask
,
1507 sched_domain_span(sd
));
1508 if (best_cpu
< nr_cpu_ids
) {
1517 * And finally, if there were no matches within the domains
1518 * just give the caller *something* to work with from the compatible
1524 cpu
= cpumask_any(lowest_mask
);
1525 if (cpu
< nr_cpu_ids
)
1530 /* Will lock the rq it finds */
1531 static struct rq
*find_lock_lowest_rq(struct task_struct
*task
, struct rq
*rq
)
1533 struct rq
*lowest_rq
= NULL
;
1537 for (tries
= 0; tries
< RT_MAX_TRIES
; tries
++) {
1538 cpu
= find_lowest_rq(task
);
1540 if ((cpu
== -1) || (cpu
== rq
->cpu
))
1543 lowest_rq
= cpu_rq(cpu
);
1545 /* if the prio of this runqueue changed, try again */
1546 if (double_lock_balance(rq
, lowest_rq
)) {
1548 * We had to unlock the run queue. In
1549 * the mean time, task could have
1550 * migrated already or had its affinity changed.
1551 * Also make sure that it wasn't scheduled on its rq.
1553 if (unlikely(task_rq(task
) != rq
||
1554 !cpumask_test_cpu(lowest_rq
->cpu
,
1555 tsk_cpus_allowed(task
)) ||
1556 task_running(rq
, task
) ||
1559 raw_spin_unlock(&lowest_rq
->lock
);
1565 /* If this rq is still suitable use it. */
1566 if (lowest_rq
->rt
.highest_prio
.curr
> task
->prio
)
1570 double_unlock_balance(rq
, lowest_rq
);
1577 static struct task_struct
*pick_next_pushable_task(struct rq
*rq
)
1579 struct task_struct
*p
;
1581 if (!has_pushable_tasks(rq
))
1584 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
1585 struct task_struct
, pushable_tasks
);
1587 BUG_ON(rq
->cpu
!= task_cpu(p
));
1588 BUG_ON(task_current(rq
, p
));
1589 BUG_ON(p
->rt
.nr_cpus_allowed
<= 1);
1592 BUG_ON(!rt_task(p
));
1598 * If the current CPU has more than one RT task, see if the non
1599 * running task can migrate over to a CPU that is running a task
1600 * of lesser priority.
1602 static int push_rt_task(struct rq
*rq
)
1604 struct task_struct
*next_task
;
1605 struct rq
*lowest_rq
;
1608 if (!rq
->rt
.overloaded
)
1611 next_task
= pick_next_pushable_task(rq
);
1615 #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
1616 if (unlikely(task_running(rq
, next_task
)))
1621 if (unlikely(next_task
== rq
->curr
)) {
1627 * It's possible that the next_task slipped in of
1628 * higher priority than current. If that's the case
1629 * just reschedule current.
1631 if (unlikely(next_task
->prio
< rq
->curr
->prio
)) {
1632 resched_task(rq
->curr
);
1636 /* We might release rq lock */
1637 get_task_struct(next_task
);
1639 /* find_lock_lowest_rq locks the rq if found */
1640 lowest_rq
= find_lock_lowest_rq(next_task
, rq
);
1642 struct task_struct
*task
;
1644 * find_lock_lowest_rq releases rq->lock
1645 * so it is possible that next_task has migrated.
1647 * We need to make sure that the task is still on the same
1648 * run-queue and is also still the next task eligible for
1651 task
= pick_next_pushable_task(rq
);
1652 if (task_cpu(next_task
) == rq
->cpu
&& task
== next_task
) {
1654 * The task hasn't migrated, and is still the next
1655 * eligible task, but we failed to find a run-queue
1656 * to push it to. Do not retry in this case, since
1657 * other cpus will pull from us when ready.
1663 /* No more tasks, just exit */
1667 * Something has shifted, try again.
1669 put_task_struct(next_task
);
1674 deactivate_task(rq
, next_task
, 0);
1675 set_task_cpu(next_task
, lowest_rq
->cpu
);
1676 activate_task(lowest_rq
, next_task
, 0);
1679 resched_task(lowest_rq
->curr
);
1681 double_unlock_balance(rq
, lowest_rq
);
1684 put_task_struct(next_task
);
1689 static void push_rt_tasks(struct rq
*rq
)
1691 /* push_rt_task will return true if it moved an RT */
1692 while (push_rt_task(rq
))
1696 static int pull_rt_task(struct rq
*this_rq
)
1698 int this_cpu
= this_rq
->cpu
, ret
= 0, cpu
;
1699 struct task_struct
*p
;
1702 if (likely(!rt_overloaded(this_rq
)))
1705 for_each_cpu(cpu
, this_rq
->rd
->rto_mask
) {
1706 if (this_cpu
== cpu
)
1709 src_rq
= cpu_rq(cpu
);
1712 * Don't bother taking the src_rq->lock if the next highest
1713 * task is known to be lower-priority than our current task.
1714 * This may look racy, but if this value is about to go
1715 * logically higher, the src_rq will push this task away.
1716 * And if its going logically lower, we do not care
1718 if (src_rq
->rt
.highest_prio
.next
>=
1719 this_rq
->rt
.highest_prio
.curr
)
1723 * We can potentially drop this_rq's lock in
1724 * double_lock_balance, and another CPU could
1727 double_lock_balance(this_rq
, src_rq
);
1730 * Are there still pullable RT tasks?
1732 if (src_rq
->rt
.rt_nr_running
<= 1)
1735 p
= pick_next_highest_task_rt(src_rq
, this_cpu
);
1738 * Do we have an RT task that preempts
1739 * the to-be-scheduled task?
1741 if (p
&& (p
->prio
< this_rq
->rt
.highest_prio
.curr
)) {
1742 WARN_ON(p
== src_rq
->curr
);
1746 * There's a chance that p is higher in priority
1747 * than what's currently running on its cpu.
1748 * This is just that p is wakeing up and hasn't
1749 * had a chance to schedule. We only pull
1750 * p if it is lower in priority than the
1751 * current task on the run queue
1753 if (p
->prio
< src_rq
->curr
->prio
)
1758 deactivate_task(src_rq
, p
, 0);
1759 set_task_cpu(p
, this_cpu
);
1760 activate_task(this_rq
, p
, 0);
1762 * We continue with the search, just in
1763 * case there's an even higher prio task
1764 * in another runqueue. (low likelihood
1769 double_unlock_balance(this_rq
, src_rq
);
1775 static void pre_schedule_rt(struct rq
*rq
, struct task_struct
*prev
)
1777 /* Try to pull RT tasks here if we lower this rq's prio */
1778 if (rq
->rt
.highest_prio
.curr
> prev
->prio
)
1782 static void post_schedule_rt(struct rq
*rq
)
1788 * If we are not running and we are not going to reschedule soon, we should
1789 * try to push tasks away now
1791 static void task_woken_rt(struct rq
*rq
, struct task_struct
*p
)
1793 if (!task_running(rq
, p
) &&
1794 !test_tsk_need_resched(rq
->curr
) &&
1795 has_pushable_tasks(rq
) &&
1796 p
->rt
.nr_cpus_allowed
> 1 &&
1797 rt_task(rq
->curr
) &&
1798 (rq
->curr
->rt
.nr_cpus_allowed
< 2 ||
1799 rq
->curr
->prio
<= p
->prio
))
1803 static void set_cpus_allowed_rt(struct task_struct
*p
,
1804 const struct cpumask
*new_mask
)
1809 BUG_ON(!rt_task(p
));
1814 weight
= cpumask_weight(new_mask
);
1817 * Only update if the process changes its state from whether it
1818 * can migrate or not.
1820 if ((p
->rt
.nr_cpus_allowed
> 1) == (weight
> 1))
1826 * The process used to be able to migrate OR it can now migrate
1829 if (!task_current(rq
, p
))
1830 dequeue_pushable_task(rq
, p
);
1831 BUG_ON(!rq
->rt
.rt_nr_migratory
);
1832 rq
->rt
.rt_nr_migratory
--;
1834 if (!task_current(rq
, p
))
1835 enqueue_pushable_task(rq
, p
);
1836 rq
->rt
.rt_nr_migratory
++;
1839 update_rt_migration(&rq
->rt
);
1842 /* Assumes rq->lock is held */
1843 static void rq_online_rt(struct rq
*rq
)
1845 if (rq
->rt
.overloaded
)
1846 rt_set_overload(rq
);
1848 __enable_runtime(rq
);
1850 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rq
->rt
.highest_prio
.curr
);
1853 /* Assumes rq->lock is held */
1854 static void rq_offline_rt(struct rq
*rq
)
1856 if (rq
->rt
.overloaded
)
1857 rt_clear_overload(rq
);
1859 __disable_runtime(rq
);
1861 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, CPUPRI_INVALID
);
1865 * When switch from the rt queue, we bring ourselves to a position
1866 * that we might want to pull RT tasks from other runqueues.
1868 static void switched_from_rt(struct rq
*rq
, struct task_struct
*p
)
1871 * If there are other RT tasks then we will reschedule
1872 * and the scheduling of the other RT tasks will handle
1873 * the balancing. But if we are the last RT task
1874 * we may need to handle the pulling of RT tasks
1877 if (p
->on_rq
&& !rq
->rt
.rt_nr_running
)
1881 void init_sched_rt_class(void)
1885 for_each_possible_cpu(i
) {
1886 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask
, i
),
1887 GFP_KERNEL
, cpu_to_node(i
));
1890 #endif /* CONFIG_SMP */
1893 * When switching a task to RT, we may overload the runqueue
1894 * with RT tasks. In this case we try to push them off to
1897 static void switched_to_rt(struct rq
*rq
, struct task_struct
*p
)
1899 int check_resched
= 1;
1902 * If we are already running, then there's nothing
1903 * that needs to be done. But if we are not running
1904 * we may need to preempt the current running task.
1905 * If that current running task is also an RT task
1906 * then see if we can move to another run queue.
1908 if (p
->on_rq
&& rq
->curr
!= p
) {
1910 if (rq
->rt
.overloaded
&& push_rt_task(rq
) &&
1911 /* Don't resched if we changed runqueues */
1914 #endif /* CONFIG_SMP */
1915 if (check_resched
&& p
->prio
< rq
->curr
->prio
)
1916 resched_task(rq
->curr
);
1921 * Priority of the task has changed. This may cause
1922 * us to initiate a push or pull.
1925 prio_changed_rt(struct rq
*rq
, struct task_struct
*p
, int oldprio
)
1930 if (rq
->curr
== p
) {
1933 * If our priority decreases while running, we
1934 * may need to pull tasks to this runqueue.
1936 if (oldprio
< p
->prio
)
1939 * If there's a higher priority task waiting to run
1940 * then reschedule. Note, the above pull_rt_task
1941 * can release the rq lock and p could migrate.
1942 * Only reschedule if p is still on the same runqueue.
1944 if (p
->prio
> rq
->rt
.highest_prio
.curr
&& rq
->curr
== p
)
1947 /* For UP simply resched on drop of prio */
1948 if (oldprio
< p
->prio
)
1950 #endif /* CONFIG_SMP */
1953 * This task is not running, but if it is
1954 * greater than the current running task
1957 if (p
->prio
< rq
->curr
->prio
)
1958 resched_task(rq
->curr
);
1962 static void watchdog(struct rq
*rq
, struct task_struct
*p
)
1964 unsigned long soft
, hard
;
1966 /* max may change after cur was read, this will be fixed next tick */
1967 soft
= task_rlimit(p
, RLIMIT_RTTIME
);
1968 hard
= task_rlimit_max(p
, RLIMIT_RTTIME
);
1970 if (soft
!= RLIM_INFINITY
) {
1974 next
= DIV_ROUND_UP(min(soft
, hard
), USEC_PER_SEC
/HZ
);
1975 if (p
->rt
.timeout
> next
)
1976 p
->cputime_expires
.sched_exp
= p
->se
.sum_exec_runtime
;
1980 static void task_tick_rt(struct rq
*rq
, struct task_struct
*p
, int queued
)
1987 * RR tasks need a special form of timeslice management.
1988 * FIFO tasks have no timeslices.
1990 if (p
->policy
!= SCHED_RR
)
1993 if (--p
->rt
.time_slice
)
1996 p
->rt
.time_slice
= RR_TIMESLICE
;
1999 * Requeue to the end of queue if we are not the only element
2002 if (p
->rt
.run_list
.prev
!= p
->rt
.run_list
.next
) {
2003 requeue_task_rt(rq
, p
, 0);
2004 set_tsk_need_resched(p
);
2008 static void set_curr_task_rt(struct rq
*rq
)
2010 struct task_struct
*p
= rq
->curr
;
2012 p
->se
.exec_start
= rq
->clock_task
;
2014 /* The running task is never eligible for pushing */
2015 dequeue_pushable_task(rq
, p
);
2018 static unsigned int get_rr_interval_rt(struct rq
*rq
, struct task_struct
*task
)
2021 * Time slice is 0 for SCHED_FIFO tasks
2023 if (task
->policy
== SCHED_RR
)
2024 return RR_TIMESLICE
;
2029 const struct sched_class rt_sched_class
= {
2030 .next
= &fair_sched_class
,
2031 .enqueue_task
= enqueue_task_rt
,
2032 .dequeue_task
= dequeue_task_rt
,
2033 .yield_task
= yield_task_rt
,
2035 .check_preempt_curr
= check_preempt_curr_rt
,
2037 .pick_next_task
= pick_next_task_rt
,
2038 .put_prev_task
= put_prev_task_rt
,
2041 .select_task_rq
= select_task_rq_rt
,
2043 .set_cpus_allowed
= set_cpus_allowed_rt
,
2044 .rq_online
= rq_online_rt
,
2045 .rq_offline
= rq_offline_rt
,
2046 .pre_schedule
= pre_schedule_rt
,
2047 .post_schedule
= post_schedule_rt
,
2048 .task_woken
= task_woken_rt
,
2049 .switched_from
= switched_from_rt
,
2052 .set_curr_task
= set_curr_task_rt
,
2053 .task_tick
= task_tick_rt
,
2055 .get_rr_interval
= get_rr_interval_rt
,
2057 .prio_changed
= prio_changed_rt
,
2058 .switched_to
= switched_to_rt
,
2061 #ifdef CONFIG_SCHED_DEBUG
2062 extern void print_rt_rq(struct seq_file
*m
, int cpu
, struct rt_rq
*rt_rq
);
2064 void print_rt_stats(struct seq_file
*m
, int cpu
)
2067 struct rt_rq
*rt_rq
;
2070 for_each_rt_rq(rt_rq
, iter
, cpu_rq(cpu
))
2071 print_rt_rq(m
, cpu
, rt_rq
);
2074 #endif /* CONFIG_SCHED_DEBUG */