1 // SPDX-License-Identifier: GPL-2.0
3 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
9 #include <linux/slab.h>
10 #include <linux/irq_work.h>
15 #include <trace/events/sched.h>
17 int sched_rr_timeslice
= RR_TIMESLICE
;
18 int sysctl_sched_rr_timeslice
= (MSEC_PER_SEC
/ HZ
) * RR_TIMESLICE
;
21 void update_rt_load_avg(u64 now
, struct sched_rt_entity
*rt_se
);
23 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
);
25 struct rt_bandwidth def_rt_bandwidth
;
27 static enum hrtimer_restart
sched_rt_period_timer(struct hrtimer
*timer
)
29 struct rt_bandwidth
*rt_b
=
30 container_of(timer
, struct rt_bandwidth
, rt_period_timer
);
34 raw_spin_lock(&rt_b
->rt_runtime_lock
);
36 overrun
= hrtimer_forward_now(timer
, rt_b
->rt_period
);
40 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
41 idle
= do_sched_rt_period_timer(rt_b
, overrun
);
42 raw_spin_lock(&rt_b
->rt_runtime_lock
);
45 rt_b
->rt_period_active
= 0;
46 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
48 return idle
? HRTIMER_NORESTART
: HRTIMER_RESTART
;
51 void init_rt_bandwidth(struct rt_bandwidth
*rt_b
, u64 period
, u64 runtime
)
53 rt_b
->rt_period
= ns_to_ktime(period
);
54 rt_b
->rt_runtime
= runtime
;
56 raw_spin_lock_init(&rt_b
->rt_runtime_lock
);
58 hrtimer_init(&rt_b
->rt_period_timer
,
59 CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
60 rt_b
->rt_period_timer
.function
= sched_rt_period_timer
;
63 static inline void do_start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
65 raw_spin_lock(&rt_b
->rt_runtime_lock
);
66 if (!rt_b
->rt_period_active
) {
67 rt_b
->rt_period_active
= 1;
69 * SCHED_DEADLINE updates the bandwidth, as a run away
70 * RT task with a DL task could hog a CPU. But DL does
71 * not reset the period. If a deadline task was running
72 * without an RT task running, it can cause RT tasks to
73 * throttle when they start up. Kick the timer right away
74 * to update the period.
76 hrtimer_forward_now(&rt_b
->rt_period_timer
, ns_to_ktime(0));
77 hrtimer_start_expires(&rt_b
->rt_period_timer
, HRTIMER_MODE_ABS_PINNED
);
79 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
82 static void start_rt_bandwidth(struct rt_bandwidth
*rt_b
)
84 if (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
)
87 do_start_rt_bandwidth(rt_b
);
90 void init_rt_rq(struct rt_rq
*rt_rq
)
92 struct rt_prio_array
*array
;
95 array
= &rt_rq
->active
;
96 for (i
= 0; i
< MAX_RT_PRIO
; i
++) {
97 INIT_LIST_HEAD(array
->queue
+ i
);
98 __clear_bit(i
, array
->bitmap
);
100 /* delimiter for bitsearch: */
101 __set_bit(MAX_RT_PRIO
, array
->bitmap
);
103 #if defined CONFIG_SMP
104 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
105 rt_rq
->highest_prio
.next
= MAX_RT_PRIO
;
106 rt_rq
->rt_nr_migratory
= 0;
107 rt_rq
->overloaded
= 0;
108 plist_head_init(&rt_rq
->pushable_tasks
);
109 atomic_long_set(&rt_rq
->removed_util_avg
, 0);
110 atomic_long_set(&rt_rq
->removed_load_avg
, 0);
111 #endif /* CONFIG_SMP */
112 /* We start is dequeued state, because no RT tasks are queued */
113 rt_rq
->rt_queued
= 0;
116 rt_rq
->rt_throttled
= 0;
117 rt_rq
->rt_runtime
= 0;
118 raw_spin_lock_init(&rt_rq
->rt_runtime_lock
);
121 #ifdef CONFIG_RT_GROUP_SCHED
122 static void destroy_rt_bandwidth(struct rt_bandwidth
*rt_b
)
124 hrtimer_cancel(&rt_b
->rt_period_timer
);
127 #define rt_entity_is_task(rt_se) (!(rt_se)->my_q)
129 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
131 #ifdef CONFIG_SCHED_DEBUG
132 WARN_ON_ONCE(!rt_entity_is_task(rt_se
));
134 return container_of(rt_se
, struct task_struct
, rt
);
137 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
142 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
147 static inline struct rq
*rq_of_rt_se(struct sched_rt_entity
*rt_se
)
149 struct rt_rq
*rt_rq
= rt_se
->rt_rq
;
154 void free_rt_sched_group(struct task_group
*tg
)
159 destroy_rt_bandwidth(&tg
->rt_bandwidth
);
161 for_each_possible_cpu(i
) {
172 void init_tg_rt_entry(struct task_group
*tg
, struct rt_rq
*rt_rq
,
173 struct sched_rt_entity
*rt_se
, int cpu
,
174 struct sched_rt_entity
*parent
)
176 struct rq
*rq
= cpu_rq(cpu
);
178 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
179 rt_rq
->rt_nr_boosted
= 0;
183 tg
->rt_rq
[cpu
] = rt_rq
;
184 tg
->rt_se
[cpu
] = rt_se
;
190 rt_se
->rt_rq
= &rq
->rt
;
192 rt_se
->rt_rq
= parent
->my_q
;
195 rt_se
->parent
= parent
;
196 INIT_LIST_HEAD(&rt_se
->run_list
);
199 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
202 struct sched_rt_entity
*rt_se
;
205 tg
->rt_rq
= kzalloc(sizeof(rt_rq
) * nr_cpu_ids
, GFP_KERNEL
);
208 tg
->rt_se
= kzalloc(sizeof(rt_se
) * nr_cpu_ids
, GFP_KERNEL
);
212 init_rt_bandwidth(&tg
->rt_bandwidth
,
213 ktime_to_ns(def_rt_bandwidth
.rt_period
), 0);
215 for_each_possible_cpu(i
) {
216 rt_rq
= kzalloc_node(sizeof(struct rt_rq
),
217 GFP_KERNEL
, cpu_to_node(i
));
221 rt_se
= kzalloc_node(sizeof(struct sched_rt_entity
),
222 GFP_KERNEL
, cpu_to_node(i
));
227 rt_rq
->rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
228 init_tg_rt_entry(tg
, rt_rq
, rt_se
, i
, parent
->rt_se
[i
]);
229 init_rt_entity_runnable_average(rt_se
);
240 #else /* CONFIG_RT_GROUP_SCHED */
242 #define rt_entity_is_task(rt_se) (1)
244 static inline struct task_struct
*rt_task_of(struct sched_rt_entity
*rt_se
)
246 return container_of(rt_se
, struct task_struct
, rt
);
249 static inline struct rq
*rq_of_rt_rq(struct rt_rq
*rt_rq
)
251 return container_of(rt_rq
, struct rq
, rt
);
254 static inline struct rq
*rq_of_rt_se(struct sched_rt_entity
*rt_se
)
256 struct task_struct
*p
= rt_task_of(rt_se
);
261 static inline struct rt_rq
*rt_rq_of_se(struct sched_rt_entity
*rt_se
)
263 struct rq
*rq
= rq_of_rt_se(rt_se
);
268 void free_rt_sched_group(struct task_group
*tg
) { }
270 int alloc_rt_sched_group(struct task_group
*tg
, struct task_group
*parent
)
274 #endif /* CONFIG_RT_GROUP_SCHED */
278 #include "sched-pelt.h"
279 #define entity_is_task(se) (!se->my_q)
281 extern u64
decay_load(u64 val
, u64 n
);
283 static u32
__accumulate_pelt_segments_rt(u64 periods
, u32 d1
, u32 d3
)
287 c1
= decay_load((u64
)d1
, periods
);
289 c2
= LOAD_AVG_MAX
- decay_load(LOAD_AVG_MAX
, periods
) - 1024;
294 #define cap_scale(v, s) ((v)*(s) >> SCHED_CAPACITY_SHIFT)
296 static __always_inline u32
297 accumulate_sum_rt(u64 delta
, int cpu
, struct sched_avg
*sa
,
298 unsigned long weight
, int running
)
300 unsigned long scale_freq
, scale_cpu
;
301 u32 contrib
= (u32
)delta
;
304 scale_freq
= arch_scale_freq_capacity(NULL
, cpu
);
305 scale_cpu
= arch_scale_cpu_capacity(NULL
, cpu
);
307 delta
+= sa
->period_contrib
;
308 periods
= delta
/ 1024;
311 sa
->load_sum
= decay_load(sa
->load_sum
, periods
);
312 sa
->util_sum
= decay_load((u64
)(sa
->util_sum
), periods
);
315 contrib
= __accumulate_pelt_segments_rt(periods
,
316 1024 - sa
->period_contrib
, delta
);
318 sa
->period_contrib
= delta
;
320 contrib
= cap_scale(contrib
, scale_freq
);
322 sa
->load_sum
+= weight
* contrib
;
325 sa
->util_sum
+= contrib
* scale_cpu
;
331 * We can represent the historical contribution to runnable average as the
332 * coefficients of a geometric series, exactly like fair task load.
333 * refer the ___update_load_avg @ fair sched class
335 static __always_inline
int
336 __update_load_avg(u64 now
, int cpu
, struct sched_avg
*sa
,
337 unsigned long weight
, int running
, struct rt_rq
*rt_rq
)
341 delta
= now
- sa
->last_update_time
;
343 if ((s64
)delta
< 0) {
344 sa
->last_update_time
= now
;
352 sa
->last_update_time
+= delta
<< 10;
357 if (!accumulate_sum_rt(delta
, cpu
, sa
, weight
, running
))
360 sa
->load_avg
= div_u64(sa
->load_sum
, LOAD_AVG_MAX
- 1024 + sa
->period_contrib
);
361 sa
->util_avg
= sa
->util_sum
/ (LOAD_AVG_MAX
- 1024 + sa
->period_contrib
);
366 static void pull_rt_task(struct rq
*this_rq
);
368 static inline bool need_pull_rt_task(struct rq
*rq
, struct task_struct
*prev
)
370 /* Try to pull RT tasks here if we lower this rq's prio */
371 return rq
->rt
.highest_prio
.curr
> prev
->prio
;
374 static inline int rt_overloaded(struct rq
*rq
)
376 return atomic_read(&rq
->rd
->rto_count
);
379 static inline void rt_set_overload(struct rq
*rq
)
384 cpumask_set_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
386 * Make sure the mask is visible before we set
387 * the overload count. That is checked to determine
388 * if we should look at the mask. It would be a shame
389 * if we looked at the mask, but the mask was not
392 * Matched by the barrier in pull_rt_task().
395 atomic_inc(&rq
->rd
->rto_count
);
398 static inline void rt_clear_overload(struct rq
*rq
)
403 /* the order here really doesn't matter */
404 atomic_dec(&rq
->rd
->rto_count
);
405 cpumask_clear_cpu(rq
->cpu
, rq
->rd
->rto_mask
);
408 static void update_rt_migration(struct rt_rq
*rt_rq
)
410 if (rt_rq
->rt_nr_migratory
&& rt_rq
->rt_nr_total
> 1) {
411 if (!rt_rq
->overloaded
) {
412 rt_set_overload(rq_of_rt_rq(rt_rq
));
413 rt_rq
->overloaded
= 1;
415 } else if (rt_rq
->overloaded
) {
416 rt_clear_overload(rq_of_rt_rq(rt_rq
));
417 rt_rq
->overloaded
= 0;
421 static void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
423 struct task_struct
*p
;
425 if (!rt_entity_is_task(rt_se
))
428 p
= rt_task_of(rt_se
);
429 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
431 rt_rq
->rt_nr_total
++;
432 if (p
->nr_cpus_allowed
> 1)
433 rt_rq
->rt_nr_migratory
++;
435 update_rt_migration(rt_rq
);
438 static void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
440 struct task_struct
*p
;
442 if (!rt_entity_is_task(rt_se
))
445 p
= rt_task_of(rt_se
);
446 rt_rq
= &rq_of_rt_rq(rt_rq
)->rt
;
448 rt_rq
->rt_nr_total
--;
449 if (p
->nr_cpus_allowed
> 1)
450 rt_rq
->rt_nr_migratory
--;
452 update_rt_migration(rt_rq
);
455 static inline int has_pushable_tasks(struct rq
*rq
)
457 return !plist_head_empty(&rq
->rt
.pushable_tasks
);
460 static DEFINE_PER_CPU(struct callback_head
, rt_push_head
);
461 static DEFINE_PER_CPU(struct callback_head
, rt_pull_head
);
463 static void push_rt_tasks(struct rq
*);
464 static void pull_rt_task(struct rq
*);
466 static inline void queue_push_tasks(struct rq
*rq
)
468 if (!has_pushable_tasks(rq
))
471 queue_balance_callback(rq
, &per_cpu(rt_push_head
, rq
->cpu
), push_rt_tasks
);
474 static inline void queue_pull_task(struct rq
*rq
)
476 queue_balance_callback(rq
, &per_cpu(rt_pull_head
, rq
->cpu
), pull_rt_task
);
479 static void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
481 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
482 plist_node_init(&p
->pushable_tasks
, p
->prio
);
483 plist_add(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
485 /* Update the highest prio pushable task */
486 if (p
->prio
< rq
->rt
.highest_prio
.next
)
487 rq
->rt
.highest_prio
.next
= p
->prio
;
490 static void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
492 plist_del(&p
->pushable_tasks
, &rq
->rt
.pushable_tasks
);
494 /* Update the new highest prio pushable task */
495 if (has_pushable_tasks(rq
)) {
496 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
497 struct task_struct
, pushable_tasks
);
498 rq
->rt
.highest_prio
.next
= p
->prio
;
500 rq
->rt
.highest_prio
.next
= MAX_RT_PRIO
;
505 static inline void enqueue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
509 static inline void dequeue_pushable_task(struct rq
*rq
, struct task_struct
*p
)
514 void inc_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
519 void dec_rt_migration(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
523 static inline bool need_pull_rt_task(struct rq
*rq
, struct task_struct
*prev
)
528 static inline void pull_rt_task(struct rq
*this_rq
)
532 static inline void queue_push_tasks(struct rq
*rq
)
535 #endif /* CONFIG_SMP */
537 static void enqueue_top_rt_rq(struct rt_rq
*rt_rq
);
538 static void dequeue_top_rt_rq(struct rt_rq
*rt_rq
);
540 static inline int on_rt_rq(struct sched_rt_entity
*rt_se
)
545 #ifdef CONFIG_RT_GROUP_SCHED
547 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
552 return rt_rq
->rt_runtime
;
555 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
557 return ktime_to_ns(rt_rq
->tg
->rt_bandwidth
.rt_period
);
560 typedef struct task_group
*rt_rq_iter_t
;
562 static inline struct task_group
*next_task_group(struct task_group
*tg
)
565 tg
= list_entry_rcu(tg
->list
.next
,
566 typeof(struct task_group
), list
);
567 } while (&tg
->list
!= &task_groups
&& task_group_is_autogroup(tg
));
569 if (&tg
->list
== &task_groups
)
575 #define for_each_rt_rq(rt_rq, iter, rq) \
576 for (iter = container_of(&task_groups, typeof(*iter), list); \
577 (iter = next_task_group(iter)) && \
578 (rt_rq = iter->rt_rq[cpu_of(rq)]);)
580 #define for_each_sched_rt_entity(rt_se) \
581 for (; rt_se; rt_se = rt_se->parent)
583 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
588 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
);
589 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
);
591 static void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
593 struct task_struct
*curr
= rq_of_rt_rq(rt_rq
)->curr
;
594 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
595 struct sched_rt_entity
*rt_se
;
597 int cpu
= cpu_of(rq
);
599 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
601 if (rt_rq
->rt_nr_running
) {
603 enqueue_top_rt_rq(rt_rq
);
604 else if (!on_rt_rq(rt_se
))
605 enqueue_rt_entity(rt_se
, 0);
607 if (rt_rq
->highest_prio
.curr
< curr
->prio
)
612 static void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
614 struct sched_rt_entity
*rt_se
;
615 int cpu
= cpu_of(rq_of_rt_rq(rt_rq
));
617 rt_se
= rt_rq
->tg
->rt_se
[cpu
];
620 dequeue_top_rt_rq(rt_rq
);
621 else if (on_rt_rq(rt_se
))
622 dequeue_rt_entity(rt_se
, 0);
625 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
627 return rt_rq
->rt_throttled
&& !rt_rq
->rt_nr_boosted
;
630 static int rt_se_boosted(struct sched_rt_entity
*rt_se
)
632 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
633 struct task_struct
*p
;
636 return !!rt_rq
->rt_nr_boosted
;
638 p
= rt_task_of(rt_se
);
639 return p
->prio
!= p
->normal_prio
;
643 static inline const struct cpumask
*sched_rt_period_mask(void)
645 return this_rq()->rd
->span
;
648 static inline const struct cpumask
*sched_rt_period_mask(void)
650 return cpu_online_mask
;
655 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
657 return container_of(rt_b
, struct task_group
, rt_bandwidth
)->rt_rq
[cpu
];
660 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
662 return &rt_rq
->tg
->rt_bandwidth
;
665 #else /* !CONFIG_RT_GROUP_SCHED */
667 static inline u64
sched_rt_runtime(struct rt_rq
*rt_rq
)
669 return rt_rq
->rt_runtime
;
672 static inline u64
sched_rt_period(struct rt_rq
*rt_rq
)
674 return ktime_to_ns(def_rt_bandwidth
.rt_period
);
677 typedef struct rt_rq
*rt_rq_iter_t
;
679 #define for_each_rt_rq(rt_rq, iter, rq) \
680 for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)
682 #define for_each_sched_rt_entity(rt_se) \
683 for (; rt_se; rt_se = NULL)
685 static inline struct rt_rq
*group_rt_rq(struct sched_rt_entity
*rt_se
)
690 static inline void sched_rt_rq_enqueue(struct rt_rq
*rt_rq
)
692 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
694 if (!rt_rq
->rt_nr_running
)
697 enqueue_top_rt_rq(rt_rq
);
701 static inline void sched_rt_rq_dequeue(struct rt_rq
*rt_rq
)
703 dequeue_top_rt_rq(rt_rq
);
706 static inline int rt_rq_throttled(struct rt_rq
*rt_rq
)
708 return rt_rq
->rt_throttled
;
711 static inline const struct cpumask
*sched_rt_period_mask(void)
713 return cpu_online_mask
;
717 struct rt_rq
*sched_rt_period_rt_rq(struct rt_bandwidth
*rt_b
, int cpu
)
719 return &cpu_rq(cpu
)->rt
;
722 static inline struct rt_bandwidth
*sched_rt_bandwidth(struct rt_rq
*rt_rq
)
724 return &def_rt_bandwidth
;
727 #endif /* CONFIG_RT_GROUP_SCHED */
729 bool sched_rt_bandwidth_account(struct rt_rq
*rt_rq
)
731 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
733 return (hrtimer_active(&rt_b
->rt_period_timer
) ||
734 rt_rq
->rt_time
< rt_b
->rt_runtime
);
739 * We ran out of runtime, see if we can borrow some from our neighbours.
741 static void do_balance_runtime(struct rt_rq
*rt_rq
)
743 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
744 struct root_domain
*rd
= rq_of_rt_rq(rt_rq
)->rd
;
748 weight
= cpumask_weight(rd
->span
);
750 raw_spin_lock(&rt_b
->rt_runtime_lock
);
751 rt_period
= ktime_to_ns(rt_b
->rt_period
);
752 for_each_cpu(i
, rd
->span
) {
753 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
759 raw_spin_lock(&iter
->rt_runtime_lock
);
761 * Either all rqs have inf runtime and there's nothing to steal
762 * or __disable_runtime() below sets a specific rq to inf to
763 * indicate its been disabled and disalow stealing.
765 if (iter
->rt_runtime
== RUNTIME_INF
)
769 * From runqueues with spare time, take 1/n part of their
770 * spare time, but no more than our period.
772 diff
= iter
->rt_runtime
- iter
->rt_time
;
774 diff
= div_u64((u64
)diff
, weight
);
775 if (rt_rq
->rt_runtime
+ diff
> rt_period
)
776 diff
= rt_period
- rt_rq
->rt_runtime
;
777 iter
->rt_runtime
-= diff
;
778 rt_rq
->rt_runtime
+= diff
;
779 if (rt_rq
->rt_runtime
== rt_period
) {
780 raw_spin_unlock(&iter
->rt_runtime_lock
);
785 raw_spin_unlock(&iter
->rt_runtime_lock
);
787 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
791 * Ensure this RQ takes back all the runtime it lend to its neighbours.
793 static void __disable_runtime(struct rq
*rq
)
795 struct root_domain
*rd
= rq
->rd
;
799 if (unlikely(!scheduler_running
))
802 for_each_rt_rq(rt_rq
, iter
, rq
) {
803 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
807 raw_spin_lock(&rt_b
->rt_runtime_lock
);
808 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
810 * Either we're all inf and nobody needs to borrow, or we're
811 * already disabled and thus have nothing to do, or we have
812 * exactly the right amount of runtime to take out.
814 if (rt_rq
->rt_runtime
== RUNTIME_INF
||
815 rt_rq
->rt_runtime
== rt_b
->rt_runtime
)
817 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
820 * Calculate the difference between what we started out with
821 * and what we current have, that's the amount of runtime
822 * we lend and now have to reclaim.
824 want
= rt_b
->rt_runtime
- rt_rq
->rt_runtime
;
827 * Greedy reclaim, take back as much as we can.
829 for_each_cpu(i
, rd
->span
) {
830 struct rt_rq
*iter
= sched_rt_period_rt_rq(rt_b
, i
);
834 * Can't reclaim from ourselves or disabled runqueues.
836 if (iter
== rt_rq
|| iter
->rt_runtime
== RUNTIME_INF
)
839 raw_spin_lock(&iter
->rt_runtime_lock
);
841 diff
= min_t(s64
, iter
->rt_runtime
, want
);
842 iter
->rt_runtime
-= diff
;
845 iter
->rt_runtime
-= want
;
848 raw_spin_unlock(&iter
->rt_runtime_lock
);
854 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
856 * We cannot be left wanting - that would mean some runtime
857 * leaked out of the system.
862 * Disable all the borrow logic by pretending we have inf
863 * runtime - in which case borrowing doesn't make sense.
865 rt_rq
->rt_runtime
= RUNTIME_INF
;
866 rt_rq
->rt_throttled
= 0;
867 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
868 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
870 /* Make rt_rq available for pick_next_task() */
871 sched_rt_rq_enqueue(rt_rq
);
875 static void __enable_runtime(struct rq
*rq
)
880 if (unlikely(!scheduler_running
))
884 * Reset each runqueue's bandwidth settings
886 for_each_rt_rq(rt_rq
, iter
, rq
) {
887 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
889 raw_spin_lock(&rt_b
->rt_runtime_lock
);
890 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
891 rt_rq
->rt_runtime
= rt_b
->rt_runtime
;
893 rt_rq
->rt_throttled
= 0;
894 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
895 raw_spin_unlock(&rt_b
->rt_runtime_lock
);
899 static void balance_runtime(struct rt_rq
*rt_rq
)
901 if (!sched_feat(RT_RUNTIME_SHARE
))
904 if (rt_rq
->rt_time
> rt_rq
->rt_runtime
) {
905 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
906 do_balance_runtime(rt_rq
);
907 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
910 #else /* !CONFIG_SMP */
911 static inline void balance_runtime(struct rt_rq
*rt_rq
) {}
912 #endif /* CONFIG_SMP */
914 static int do_sched_rt_period_timer(struct rt_bandwidth
*rt_b
, int overrun
)
916 int i
, idle
= 1, throttled
= 0;
917 const struct cpumask
*span
;
919 span
= sched_rt_period_mask();
920 #ifdef CONFIG_RT_GROUP_SCHED
922 * FIXME: isolated CPUs should really leave the root task group,
923 * whether they are isolcpus or were isolated via cpusets, lest
924 * the timer run on a CPU which does not service all runqueues,
925 * potentially leaving other CPUs indefinitely throttled. If
926 * isolation is really required, the user will turn the throttle
927 * off to kill the perturbations it causes anyway. Meanwhile,
928 * this maintains functionality for boot and/or troubleshooting.
930 if (rt_b
== &root_task_group
.rt_bandwidth
)
931 span
= cpu_online_mask
;
933 for_each_cpu(i
, span
) {
935 struct rt_rq
*rt_rq
= sched_rt_period_rt_rq(rt_b
, i
);
936 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
940 * When span == cpu_online_mask, taking each rq->lock
941 * can be time-consuming. Try to avoid it when possible.
943 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
944 if (!sched_feat(RT_RUNTIME_SHARE
) && rt_rq
->rt_runtime
!= RUNTIME_INF
)
945 rt_rq
->rt_runtime
= rt_b
->rt_runtime
;
946 skip
= !rt_rq
->rt_time
&& !rt_rq
->rt_nr_running
;
947 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
951 raw_spin_lock(&rq
->lock
);
954 if (rt_rq
->rt_time
) {
957 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
958 if (rt_rq
->rt_throttled
)
959 balance_runtime(rt_rq
);
960 runtime
= rt_rq
->rt_runtime
;
961 rt_rq
->rt_time
-= min(rt_rq
->rt_time
, overrun
*runtime
);
962 if (rt_rq
->rt_throttled
&& rt_rq
->rt_time
< runtime
) {
963 rt_rq
->rt_throttled
= 0;
967 * When we're idle and a woken (rt) task is
968 * throttled check_preempt_curr() will set
969 * skip_update and the time between the wakeup
970 * and this unthrottle will get accounted as
973 if (rt_rq
->rt_nr_running
&& rq
->curr
== rq
->idle
)
974 rq_clock_skip_update(rq
, false);
976 if (rt_rq
->rt_time
|| rt_rq
->rt_nr_running
)
978 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
979 } else if (rt_rq
->rt_nr_running
) {
981 if (!rt_rq_throttled(rt_rq
))
984 if (rt_rq
->rt_throttled
)
988 sched_rt_rq_enqueue(rt_rq
);
989 raw_spin_unlock(&rq
->lock
);
992 if (!throttled
&& (!rt_bandwidth_enabled() || rt_b
->rt_runtime
== RUNTIME_INF
))
998 static inline int rt_se_prio(struct sched_rt_entity
*rt_se
)
1000 #ifdef CONFIG_RT_GROUP_SCHED
1001 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1004 return rt_rq
->highest_prio
.curr
;
1007 return rt_task_of(rt_se
)->prio
;
1010 static int sched_rt_runtime_exceeded(struct rt_rq
*rt_rq
)
1012 u64 runtime
= sched_rt_runtime(rt_rq
);
1014 if (rt_rq
->rt_throttled
)
1015 return rt_rq_throttled(rt_rq
);
1017 if (runtime
>= sched_rt_period(rt_rq
))
1020 balance_runtime(rt_rq
);
1021 runtime
= sched_rt_runtime(rt_rq
);
1022 if (runtime
== RUNTIME_INF
)
1025 if (rt_rq
->rt_time
> runtime
) {
1026 struct rt_bandwidth
*rt_b
= sched_rt_bandwidth(rt_rq
);
1029 * Don't actually throttle groups that have no runtime assigned
1030 * but accrue some time due to boosting.
1032 if (likely(rt_b
->rt_runtime
)) {
1033 rt_rq
->rt_throttled
= 1;
1034 printk_deferred_once("sched: RT throttling activated\n");
1037 * In case we did anyway, make it go away,
1038 * replenishment is a joke, since it will replenish us
1039 * with exactly 0 ns.
1044 if (rt_rq_throttled(rt_rq
)) {
1045 sched_rt_rq_dequeue(rt_rq
);
1054 * Update the current task's runtime statistics. Skip current tasks that
1055 * are not in our scheduling class.
1057 static void update_curr_rt(struct rq
*rq
)
1059 struct task_struct
*curr
= rq
->curr
;
1060 struct sched_rt_entity
*rt_se
= &curr
->rt
;
1063 if (curr
->sched_class
!= &rt_sched_class
)
1066 delta_exec
= rq_clock_task(rq
) - curr
->se
.exec_start
;
1067 if (unlikely((s64
)delta_exec
<= 0))
1070 /* Kick cpufreq (see the comment in kernel/sched/sched.h). */
1071 cpufreq_update_util(rq
, SCHED_CPUFREQ_RT
);
1073 schedstat_set(curr
->se
.statistics
.exec_max
,
1074 max(curr
->se
.statistics
.exec_max
, delta_exec
));
1076 curr
->se
.sum_exec_runtime
+= delta_exec
;
1077 account_group_exec_runtime(curr
, delta_exec
);
1079 curr
->se
.exec_start
= rq_clock_task(rq
);
1080 cpuacct_charge(curr
, delta_exec
);
1082 sched_rt_avg_update(rq
, delta_exec
);
1084 if (!rt_bandwidth_enabled())
1087 for_each_sched_rt_entity(rt_se
) {
1088 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1091 if (sched_rt_runtime(rt_rq
) != RUNTIME_INF
) {
1092 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
1093 rt_rq
->rt_time
+= delta_exec
;
1094 exceeded
= sched_rt_runtime_exceeded(rt_rq
);
1097 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
1099 do_start_rt_bandwidth(sched_rt_bandwidth(rt_rq
));
1105 dequeue_top_rt_rq(struct rt_rq
*rt_rq
)
1107 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1109 BUG_ON(&rq
->rt
!= rt_rq
);
1111 if (!rt_rq
->rt_queued
)
1114 BUG_ON(!rq
->nr_running
);
1116 sub_nr_running(rq
, rt_rq
->rt_nr_running
);
1117 rt_rq
->rt_queued
= 0;
1121 enqueue_top_rt_rq(struct rt_rq
*rt_rq
)
1123 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1125 BUG_ON(&rq
->rt
!= rt_rq
);
1127 if (rt_rq
->rt_queued
)
1129 if (rt_rq_throttled(rt_rq
) || !rt_rq
->rt_nr_running
)
1132 add_nr_running(rq
, rt_rq
->rt_nr_running
);
1133 rt_rq
->rt_queued
= 1;
1136 #if defined CONFIG_SMP
1139 inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1141 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1143 #ifdef CONFIG_RT_GROUP_SCHED
1145 * Change rq's cpupri only if rt_rq is the top queue.
1147 if (&rq
->rt
!= rt_rq
)
1150 if (rq
->online
&& prio
< prev_prio
)
1151 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, prio
);
1155 dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
)
1157 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
1159 #ifdef CONFIG_RT_GROUP_SCHED
1161 * Change rq's cpupri only if rt_rq is the top queue.
1163 if (&rq
->rt
!= rt_rq
)
1166 if (rq
->online
&& rt_rq
->highest_prio
.curr
!= prev_prio
)
1167 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rt_rq
->highest_prio
.curr
);
1170 #else /* CONFIG_SMP */
1173 void inc_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1175 void dec_rt_prio_smp(struct rt_rq
*rt_rq
, int prio
, int prev_prio
) {}
1177 #endif /* CONFIG_SMP */
1179 #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
1181 inc_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1183 int prev_prio
= rt_rq
->highest_prio
.curr
;
1185 if (prio
< prev_prio
)
1186 rt_rq
->highest_prio
.curr
= prio
;
1188 inc_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1192 dec_rt_prio(struct rt_rq
*rt_rq
, int prio
)
1194 int prev_prio
= rt_rq
->highest_prio
.curr
;
1196 if (rt_rq
->rt_nr_running
) {
1198 WARN_ON(prio
< prev_prio
);
1201 * This may have been our highest task, and therefore
1202 * we may have some recomputation to do
1204 if (prio
== prev_prio
) {
1205 struct rt_prio_array
*array
= &rt_rq
->active
;
1207 rt_rq
->highest_prio
.curr
=
1208 sched_find_first_bit(array
->bitmap
);
1212 rt_rq
->highest_prio
.curr
= MAX_RT_PRIO
;
1214 dec_rt_prio_smp(rt_rq
, prio
, prev_prio
);
1219 static inline void inc_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1220 static inline void dec_rt_prio(struct rt_rq
*rt_rq
, int prio
) {}
1222 #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */
1224 #ifdef CONFIG_RT_GROUP_SCHED
1227 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1229 if (rt_se_boosted(rt_se
))
1230 rt_rq
->rt_nr_boosted
++;
1233 start_rt_bandwidth(&rt_rq
->tg
->rt_bandwidth
);
1237 dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1239 if (rt_se_boosted(rt_se
))
1240 rt_rq
->rt_nr_boosted
--;
1242 WARN_ON(!rt_rq
->rt_nr_running
&& rt_rq
->rt_nr_boosted
);
1245 #else /* CONFIG_RT_GROUP_SCHED */
1248 inc_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1250 start_rt_bandwidth(&def_rt_bandwidth
);
1254 void dec_rt_group(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
) {}
1256 #endif /* CONFIG_RT_GROUP_SCHED */
1259 unsigned int rt_se_nr_running(struct sched_rt_entity
*rt_se
)
1261 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1264 return group_rq
->rt_nr_running
;
1270 unsigned int rt_se_rr_nr_running(struct sched_rt_entity
*rt_se
)
1272 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1273 struct task_struct
*tsk
;
1276 return group_rq
->rr_nr_running
;
1278 tsk
= rt_task_of(rt_se
);
1280 return (tsk
->policy
== SCHED_RR
) ? 1 : 0;
1284 void inc_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1286 int prio
= rt_se_prio(rt_se
);
1288 WARN_ON(!rt_prio(prio
));
1289 rt_rq
->rt_nr_running
+= rt_se_nr_running(rt_se
);
1290 rt_rq
->rr_nr_running
+= rt_se_rr_nr_running(rt_se
);
1292 inc_rt_prio(rt_rq
, prio
);
1293 inc_rt_migration(rt_se
, rt_rq
);
1294 inc_rt_group(rt_se
, rt_rq
);
1298 void dec_rt_tasks(struct sched_rt_entity
*rt_se
, struct rt_rq
*rt_rq
)
1300 WARN_ON(!rt_prio(rt_se_prio(rt_se
)));
1301 WARN_ON(!rt_rq
->rt_nr_running
);
1302 rt_rq
->rt_nr_running
-= rt_se_nr_running(rt_se
);
1303 rt_rq
->rr_nr_running
-= rt_se_rr_nr_running(rt_se
);
1305 dec_rt_prio(rt_rq
, rt_se_prio(rt_se
));
1306 dec_rt_migration(rt_se
, rt_rq
);
1307 dec_rt_group(rt_se
, rt_rq
);
1312 attach_rt_entity_load_avg(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
)
1314 rt_se
->avg
.last_update_time
= rt_rq
->avg
.last_update_time
;
1315 rt_rq
->avg
.util_avg
+= rt_se
->avg
.util_avg
;
1316 rt_rq
->avg
.util_sum
+= rt_se
->avg
.util_sum
;
1317 rt_rq
->avg
.load_avg
+= rt_se
->avg
.load_avg
;
1318 rt_rq
->avg
.load_sum
+= rt_se
->avg
.load_sum
;
1319 #ifdef CONFIG_RT_GROUP_SCHED
1320 rt_rq
->propagate_avg
= 1;
1322 rt_rq_util_change(rt_rq
);
1326 detach_rt_entity_load_avg(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
)
1328 sub_positive(&rt_rq
->avg
.util_avg
, rt_se
->avg
.util_avg
);
1329 sub_positive(&rt_rq
->avg
.util_sum
, rt_se
->avg
.util_sum
);
1330 sub_positive(&rt_rq
->avg
.load_avg
, rt_se
->avg
.load_avg
);
1331 sub_positive(&rt_rq
->avg
.load_sum
, rt_se
->avg
.load_sum
);
1332 #ifdef CONFIG_RT_GROUP_SCHED
1333 rt_rq
->propagate_avg
= 1;
1335 rt_rq_util_change(rt_rq
);
1339 attach_rt_entity_load_avg(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
) {}
1341 detach_rt_entity_load_avg(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
) {}
1345 * Change rt_se->run_list location unless SAVE && !MOVE
1347 * assumes ENQUEUE/DEQUEUE flags match
1349 static inline bool move_entity(unsigned int flags
)
1351 if ((flags
& (DEQUEUE_SAVE
| DEQUEUE_MOVE
)) == DEQUEUE_SAVE
)
1357 static void __delist_rt_entity(struct sched_rt_entity
*rt_se
, struct rt_prio_array
*array
)
1359 list_del_init(&rt_se
->run_list
);
1361 if (list_empty(array
->queue
+ rt_se_prio(rt_se
)))
1362 __clear_bit(rt_se_prio(rt_se
), array
->bitmap
);
1367 static void __enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1369 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1370 struct rt_prio_array
*array
= &rt_rq
->active
;
1371 struct rt_rq
*group_rq
= group_rt_rq(rt_se
);
1372 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1375 * Don't enqueue the group if its throttled, or when empty.
1376 * The latter is a consequence of the former when a child group
1377 * get throttled and the current group doesn't have any other
1380 if (group_rq
&& (rt_rq_throttled(group_rq
) || !group_rq
->rt_nr_running
)) {
1382 __delist_rt_entity(rt_se
, array
);
1386 if (move_entity(flags
)) {
1387 WARN_ON_ONCE(rt_se
->on_list
);
1388 if (flags
& ENQUEUE_HEAD
)
1389 list_add(&rt_se
->run_list
, queue
);
1391 list_add_tail(&rt_se
->run_list
, queue
);
1393 __set_bit(rt_se_prio(rt_se
), array
->bitmap
);
1398 update_rt_load_avg(rq_clock_task(rq_of_rt_rq(rt_rq
)), rt_se
);
1400 if (rt_entity_is_task(rt_se
) && !rt_se
->avg
.last_update_time
)
1401 attach_rt_entity_load_avg(rt_rq
, rt_se
);
1403 inc_rt_tasks(rt_se
, rt_rq
);
1406 static void __dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1408 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1409 struct rt_prio_array
*array
= &rt_rq
->active
;
1411 if (move_entity(flags
)) {
1412 WARN_ON_ONCE(!rt_se
->on_list
);
1413 __delist_rt_entity(rt_se
, array
);
1417 update_rt_load_avg(rq_clock_task(rq_of_rt_rq(rt_rq
)), rt_se
);
1419 dec_rt_tasks(rt_se
, rt_rq
);
1423 * Because the prio of an upper entry depends on the lower
1424 * entries, we must remove entries top - down.
1426 static void dequeue_rt_stack(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1428 struct sched_rt_entity
*back
= NULL
;
1430 for_each_sched_rt_entity(rt_se
) {
1435 dequeue_top_rt_rq(rt_rq_of_se(back
));
1437 for (rt_se
= back
; rt_se
; rt_se
= rt_se
->back
) {
1438 if (on_rt_rq(rt_se
))
1439 __dequeue_rt_entity(rt_se
, flags
);
1443 static void enqueue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1445 struct rq
*rq
= rq_of_rt_se(rt_se
);
1447 dequeue_rt_stack(rt_se
, flags
);
1448 for_each_sched_rt_entity(rt_se
)
1449 __enqueue_rt_entity(rt_se
, flags
);
1450 enqueue_top_rt_rq(&rq
->rt
);
1453 static void dequeue_rt_entity(struct sched_rt_entity
*rt_se
, unsigned int flags
)
1455 struct rq
*rq
= rq_of_rt_se(rt_se
);
1457 dequeue_rt_stack(rt_se
, flags
);
1459 for_each_sched_rt_entity(rt_se
) {
1460 struct rt_rq
*rt_rq
= group_rt_rq(rt_se
);
1462 if (rt_rq
&& rt_rq
->rt_nr_running
)
1463 __enqueue_rt_entity(rt_se
, flags
);
1465 enqueue_top_rt_rq(&rq
->rt
);
1469 * Adding/removing a task to/from a priority array:
1472 enqueue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1474 struct sched_rt_entity
*rt_se
= &p
->rt
;
1476 schedtune_enqueue_task(p
, cpu_of(rq
));
1478 if (flags
& ENQUEUE_WAKEUP
)
1481 enqueue_rt_entity(rt_se
, flags
);
1482 walt_inc_cumulative_runnable_avg(rq
, p
);
1484 if (!task_current(rq
, p
) && p
->nr_cpus_allowed
> 1)
1485 enqueue_pushable_task(rq
, p
);
1488 static void dequeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1490 struct sched_rt_entity
*rt_se
= &p
->rt
;
1492 schedtune_dequeue_task(p
, cpu_of(rq
));
1495 dequeue_rt_entity(rt_se
, flags
);
1496 walt_dec_cumulative_runnable_avg(rq
, p
);
1498 dequeue_pushable_task(rq
, p
);
1502 * Put task to the head or the end of the run list without the overhead of
1503 * dequeue followed by enqueue.
1506 requeue_rt_entity(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
, int head
)
1508 if (on_rt_rq(rt_se
)) {
1509 struct rt_prio_array
*array
= &rt_rq
->active
;
1510 struct list_head
*queue
= array
->queue
+ rt_se_prio(rt_se
);
1513 list_move(&rt_se
->run_list
, queue
);
1515 list_move_tail(&rt_se
->run_list
, queue
);
1519 static void requeue_task_rt(struct rq
*rq
, struct task_struct
*p
, int head
)
1521 struct sched_rt_entity
*rt_se
= &p
->rt
;
1522 struct rt_rq
*rt_rq
;
1524 for_each_sched_rt_entity(rt_se
) {
1525 rt_rq
= rt_rq_of_se(rt_se
);
1526 requeue_rt_entity(rt_rq
, rt_se
, head
);
1530 static void yield_task_rt(struct rq
*rq
)
1532 requeue_task_rt(rq
, rq
->curr
, 0);
1538 * attach/detach/migrate_task_rt_rq() for load tracking
1541 #ifdef CONFIG_SCHED_USE_FLUID_RT
1542 static int find_lowest_rq(struct task_struct
*task
, int wake_flags
);
1544 static int find_lowest_rq(struct task_struct
*task
);
1547 select_task_rq_rt(struct task_struct
*p
, int cpu
, int sd_flag
, int flags
,
1548 int sibling_count_hint
)
1550 struct task_struct
*curr
;
1553 /* For anything but wake ups, just return the task_cpu */
1554 if (sd_flag
!= SD_BALANCE_WAKE
&& sd_flag
!= SD_BALANCE_FORK
)
1560 curr
= READ_ONCE(rq
->curr
); /* unlocked access */
1563 * If the current task on @p's runqueue is an RT task, then
1564 * try to see if we can wake this RT task up on another
1565 * runqueue. Otherwise simply start this RT task
1566 * on its current runqueue.
1568 * We want to avoid overloading runqueues. If the woken
1569 * task is a higher priority, then it will stay on this CPU
1570 * and the lower prio task should be moved to another CPU.
1571 * Even though this will probably make the lower prio task
1572 * lose its cache, we do not want to bounce a higher task
1573 * around just because it gave up its CPU, perhaps for a
1576 * For equal prio tasks, we just let the scheduler sort it out.
1578 * Otherwise, just let it ride on the affined RQ and the
1579 * post-schedule router will push the preempted task away
1581 * This test is optimistic, if we get it wrong the load-balancer
1582 * will have to sort it out.
1584 if (curr
&& unlikely(rt_task(curr
)) &&
1585 (curr
->nr_cpus_allowed
< 2 ||
1586 curr
->prio
<= p
->prio
)) {
1587 #ifdef CONFIG_SCHED_USE_FLUID_RT
1588 int target
= find_lowest_rq(p
, flags
);
1590 * Even though the destination CPU is running
1591 * a higher priority task, FluidRT can bother moving it
1592 * when its utilization is very small, and the other CPU is too busy
1593 * to accomodate the p in the point of priority and utilization.
1595 * BTW, if the curr has higher priority than p, FluidRT tries to find
1596 * the other CPUs first. In the worst case, curr can be victim, if it
1597 * has very small utilization.
1599 if (likely(target
!= -1)) {
1603 int target
= find_lowest_rq(p
);
1605 * Don't bother moving it if the destination CPU is
1606 * not running a lower priority task.
1609 p
->prio
< cpu_rq(target
)->rt
.highest_prio
.curr
)
1619 #ifdef CONFIG_RT_GROUP_SCHED
1621 * Called within set_task_rq() right before setting a task's cpu. The
1622 * caller only guarantees p->pi_lock is held; no other assumptions,
1623 * including the state of rq->lock, should be made.
1625 void set_task_rq_rt(struct sched_rt_entity
*rt_se
,
1626 struct rt_rq
*prev
, struct rt_rq
*next
)
1628 u64 p_last_update_time
;
1629 u64 n_last_update_time
;
1631 if (!sched_feat(ATTACH_AGE_LOAD
))
1634 * We are supposed to update the task to "current" time, then its up to
1635 * date and ready to go to new CPU/rt_rq. But we have difficulty in
1636 * getting what current time is, so simply throw away the out-of-date
1637 * time. This will result in the wakee task is less decayed, but giving
1638 * the wakee more load sounds not bad.
1640 if (!(rt_se
->avg
.last_update_time
&& prev
))
1642 #ifndef CONFIG_64BIT
1644 u64 p_last_update_time_copy
;
1645 u64 n_last_update_time_copy
;
1648 p_last_update_time_copy
= prev
->load_last_update_time_copy
;
1649 n_last_update_time_copy
= next
->load_last_update_time_copy
;
1653 p_last_update_time
= prev
->avg
.last_update_time
;
1654 n_last_update_time
= next
->avg
.last_update_time
;
1656 } while (p_last_update_time
!= p_last_update_time_copy
||
1657 n_last_update_time
!= n_last_update_time_copy
);
1660 p_last_update_time
= prev
->avg
.last_update_time
;
1661 n_last_update_time
= next
->avg
.last_update_time
;
1663 __update_load_avg(p_last_update_time
, cpu_of(rq_of_rt_rq(prev
)),
1664 &rt_se
->avg
, 0, 0, NULL
);
1666 rt_se
->avg
.last_update_time
= n_last_update_time
;
1668 #endif /* CONFIG_RT_GROUP_SCHED */
1670 #ifndef CONFIG_64BIT
1671 static inline u64
rt_rq_last_update_time(struct rt_rq
*rt_rq
)
1673 u64 last_update_time_copy
;
1674 u64 last_update_time
;
1677 last_update_time_copy
= rt_rq
->load_last_update_time_copy
;
1679 last_update_time
= rt_rq
->avg
.last_update_time
;
1680 } while (last_update_time
!= last_update_time_copy
);
1682 return last_update_time
;
1685 static inline u64
rt_rq_last_update_time(struct rt_rq
*rt_rq
)
1687 return rt_rq
->avg
.last_update_time
;
1692 * Synchronize entity load avg of dequeued entity without locking
1695 void sync_rt_entity_load_avg(struct sched_rt_entity
*rt_se
)
1697 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1698 u64 last_update_time
;
1700 last_update_time
= rt_rq_last_update_time(rt_rq
);
1701 __update_load_avg(last_update_time
, cpu_of(rq_of_rt_rq(rt_rq
)),
1702 &rt_se
->avg
, 0, 0, NULL
);
1706 * Task first catches up with rt_rq, and then subtract
1707 * itself from the rt_rq (task must be off the queue now).
1709 static void remove_rt_entity_load_avg(struct sched_rt_entity
*rt_se
)
1711 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1714 * tasks cannot exit without having gone through wake_up_new_task() ->
1715 * post_init_entity_util_avg() which will have added things to the
1716 * rt_rq, so we can remove unconditionally.
1718 * Similarly for groups, they will have passed through
1719 * post_init_entity_util_avg() before unregister_sched_fair_group()
1723 sync_rt_entity_load_avg(rt_se
);
1724 atomic_long_add(rt_se
->avg
.load_avg
, &rt_rq
->removed_load_avg
);
1725 atomic_long_add(rt_se
->avg
.util_avg
, &rt_rq
->removed_util_avg
);
1728 static void attach_task_rt_rq(struct task_struct
*p
)
1730 struct sched_rt_entity
*rt_se
= &p
->rt
;
1731 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1732 u64 now
= rq_clock_task(rq_of_rt_rq(rt_rq
));
1734 update_rt_load_avg(now
, rt_se
);
1735 attach_rt_entity_load_avg(rt_rq
, rt_se
);
1738 static void detach_task_rt_rq(struct task_struct
*p
)
1740 struct sched_rt_entity
*rt_se
= &p
->rt
;
1741 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
1742 u64 now
= rq_clock_task(rq_of_rt_rq(rt_rq
));
1744 update_rt_load_avg(now
, rt_se
);
1745 detach_rt_entity_load_avg(rt_rq
, rt_se
);
1748 static void migrate_task_rq_rt(struct task_struct
*p
)
1751 * We are supposed to update the task to "current" time, then its up to date
1752 * and ready to go to new CPU/cfs_rq. But we have difficulty in getting
1753 * what current time is, so simply throw away the out-of-date time. This
1754 * will result in the wakee task is less decayed, but giving the wakee more
1755 * load sounds not bad.
1757 remove_rt_entity_load_avg(&p
->rt
);
1759 /* Tell new CPU we are migrated */
1760 p
->rt
.avg
.last_update_time
= 0;
1762 /* We have migrated, no longer consider this task hot */
1763 p
->se
.exec_start
= 0;
1766 static void task_dead_rt(struct task_struct
*p
)
1768 remove_rt_entity_load_avg(&p
->rt
);
1771 #ifdef CONFIG_RT_GROUP_SCHED
1772 static void task_set_group_rt(struct task_struct
*p
)
1774 set_task_rq(p
, task_cpu(p
));
1777 static void task_move_group_rt(struct task_struct
*p
)
1779 detach_task_rt_rq(p
);
1780 set_task_rq(p
, task_cpu(p
));
1783 /* Tell se's cfs_rq has been changed -- migrated */
1784 p
->se
.avg
.last_update_time
= 0;
1786 attach_task_rt_rq(p
);
1789 static void task_change_group_rt(struct task_struct
*p
, int type
)
1792 case TASK_SET_GROUP
:
1793 task_set_group_rt(p
);
1796 case TASK_MOVE_GROUP
:
1797 task_move_group_rt(p
);
1803 static void check_preempt_equal_prio(struct rq
*rq
, struct task_struct
*p
)
1806 * Current can't be migrated, useless to reschedule,
1807 * let's hope p can move out.
1809 if (rq
->curr
->nr_cpus_allowed
== 1 ||
1810 !cpupri_find(&rq
->rd
->cpupri
, rq
->curr
, NULL
))
1814 * p is migratable, so let's not schedule it and
1815 * see if it is pushed or pulled somewhere else.
1817 if (p
->nr_cpus_allowed
!= 1
1818 && cpupri_find(&rq
->rd
->cpupri
, p
, NULL
))
1822 * There appears to be other cpus that can accept
1823 * current and none to run 'p', so lets reschedule
1824 * to try and push current away:
1826 requeue_task_rt(rq
, p
, 1);
1830 /* Give new sched_entity start runnable values to heavy its load in infant time */
1831 void init_rt_entity_runnable_average(struct sched_rt_entity
*rt_se
)
1833 struct sched_avg
*sa
= &rt_se
->avg
;
1835 sa
->last_update_time
= 0;
1837 sa
->period_contrib
= 1023;
1840 * Tasks are intialized with zero load.
1841 * Load is not actually used by RT, but can be inherited into fair task.
1846 * At this point, util_avg won't be used in select_task_rq_rt anyway
1850 /* when this task enqueue'ed, it will contribute to its cfs_rq's load_avg */
1853 void init_rt_entity_runnable_average(struct sched_rt_entity
*rt_se
) { }
1854 #endif /* CONFIG_SMP */
1856 #ifdef CONFIG_SCHED_USE_FLUID_RT
1857 static inline void set_victim_flag(struct task_struct
*p
)
1862 static inline void clear_victim_flag(struct task_struct
*p
)
1867 static inline bool test_victim_flag(struct task_struct
*p
)
1875 static inline bool test_victim_flag(struct task_struct
*p
) { return false; }
1876 static inline void clear_victim_flag(struct task_struct
*p
) {}
1879 * Preempt the current task with a newly woken task if needed:
1881 static void check_preempt_curr_rt(struct rq
*rq
, struct task_struct
*p
, int flags
)
1883 if (p
->prio
< rq
->curr
->prio
) {
1886 } else if (test_victim_flag(p
)) {
1887 requeue_task_rt(rq
, p
, 1);
1896 * - the newly woken task is of equal priority to the current task
1897 * - the newly woken task is non-migratable while current is migratable
1898 * - current will be preempted on the next reschedule
1900 * we should check to see if current can readily move to a different
1901 * cpu. If so, we will reschedule to allow the push logic to try
1902 * to move current somewhere else, making room for our non-migratable
1905 if (p
->prio
== rq
->curr
->prio
&& !test_tsk_need_resched(rq
->curr
))
1906 check_preempt_equal_prio(rq
, p
);
1910 static struct sched_rt_entity
*pick_next_rt_entity(struct rq
*rq
,
1911 struct rt_rq
*rt_rq
)
1913 struct rt_prio_array
*array
= &rt_rq
->active
;
1914 struct sched_rt_entity
*next
= NULL
;
1915 struct list_head
*queue
;
1918 idx
= sched_find_first_bit(array
->bitmap
);
1919 BUG_ON(idx
>= MAX_RT_PRIO
);
1921 queue
= array
->queue
+ idx
;
1922 next
= list_entry(queue
->next
, struct sched_rt_entity
, run_list
);
1927 static struct task_struct
*_pick_next_task_rt(struct rq
*rq
)
1929 struct sched_rt_entity
*rt_se
;
1930 struct task_struct
*p
;
1931 struct rt_rq
*rt_rq
= &rq
->rt
;
1932 u64 now
= rq_clock_task(rq
);
1935 rt_se
= pick_next_rt_entity(rq
, rt_rq
);
1937 update_rt_load_avg(now
, rt_se
);
1938 rt_rq
->curr
= rt_se
;
1939 rt_rq
= group_rt_rq(rt_se
);
1942 p
= rt_task_of(rt_se
);
1943 p
->se
.exec_start
= now
;
1948 extern int update_rt_rq_load_avg(u64 now
, int cpu
, struct rt_rq
*rt_rq
, int running
);
1950 static struct task_struct
*
1951 pick_next_task_rt(struct rq
*rq
, struct task_struct
*prev
, struct rq_flags
*rf
)
1953 struct task_struct
*p
;
1954 struct rt_rq
*rt_rq
= &rq
->rt
;
1956 if (need_pull_rt_task(rq
, prev
)) {
1958 * This is OK, because current is on_cpu, which avoids it being
1959 * picked for load-balance and preemption/IRQs are still
1960 * disabled avoiding further scheduler activity on it and we're
1961 * being very careful to re-start the picking loop.
1963 rq_unpin_lock(rq
, rf
);
1965 rq_repin_lock(rq
, rf
);
1967 * pull_rt_task() can drop (and re-acquire) rq->lock; this
1968 * means a dl or stop task can slip in, in which case we need
1969 * to re-start task selection.
1971 if (unlikely((rq
->stop
&& task_on_rq_queued(rq
->stop
)) ||
1972 rq
->dl
.dl_nr_running
))
1977 * We may dequeue prev's rt_rq in put_prev_task().
1978 * So, we update time before rt_nr_running check.
1980 if (prev
->sched_class
== &rt_sched_class
)
1983 if (!rt_rq
->rt_queued
)
1986 put_prev_task(rq
, prev
);
1988 p
= _pick_next_task_rt(rq
);
1990 /* The running task is never eligible for pushing */
1991 dequeue_pushable_task(rq
, p
);
1993 queue_push_tasks(rq
);
1996 update_rt_rq_load_avg(rq_clock_task(rq
), cpu_of(rq
), rt_rq
,
1997 rq
->curr
->sched_class
== &rt_sched_class
);
1999 clear_victim_flag(p
);
2004 static void put_prev_task_rt(struct rq
*rq
, struct task_struct
*p
)
2006 struct sched_rt_entity
*rt_se
= &p
->rt
;
2007 u64 now
= rq_clock_task(rq
);
2012 * The previous task needs to be made eligible for pushing
2013 * if it is still active
2015 if (on_rt_rq(&p
->rt
) && p
->nr_cpus_allowed
> 1)
2016 enqueue_pushable_task(rq
, p
);
2018 for_each_sched_rt_entity(rt_se
) {
2019 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
2021 update_rt_load_avg(now
, rt_se
);
2029 void rt_rq_util_change(struct rt_rq
*rt_rq
)
2031 if (&this_rq()->rt
== rt_rq
)
2032 cpufreq_update_util(rt_rq
->rq
, SCHED_CPUFREQ_RT
);
2035 #ifdef CONFIG_RT_GROUP_SCHED
2036 /* Take into account change of utilization of a child task group */
2038 update_tg_rt_util(struct rt_rq
*cfs_rq
, struct sched_rt_entity
*rt_se
)
2040 struct rt_rq
*grt_rq
= rt_se
->my_q
;
2041 long delta
= grt_rq
->avg
.util_avg
- rt_se
->avg
.util_avg
;
2043 /* Nothing to update */
2047 /* Set new sched_rt_entity's utilization */
2048 rt_se
->avg
.util_avg
= grt_rq
->avg
.util_avg
;
2049 rt_se
->avg
.util_sum
= rt_se
->avg
.util_avg
* LOAD_AVG_MAX
;
2051 /* Update parent rt_rq utilization */
2052 add_positive(&cfs_rq
->avg
.util_avg
, delta
);
2053 cfs_rq
->avg
.util_sum
= cfs_rq
->avg
.util_avg
* LOAD_AVG_MAX
;
2057 /* Take into account change of load of a child task group */
2059 update_tg_rt_load(struct rt_rq
*rt_rq
, struct sched_rt_entity
*rt_se
)
2061 struct rt_rq
*grt_rq
= rt_se
->my_q
;
2062 long delta
= grt_rq
->avg
.load_avg
- rt_se
->avg
.load_avg
;
2065 * TODO: Need to consider the TG group update
2069 /* Nothing to update */
2073 /* Set new sched_rt_entity's load */
2074 rt_se
->avg
.load_avg
= grt_rq
->avg
.load_avg
;
2075 rt_se
->avg
.load_sum
= rt_se
->avg
.load_avg
* LOAD_AVG_MAX
;
2077 /* Update parent cfs_rq load */
2078 add_positive(&rt_rq
->avg
.load_avg
, delta
);
2079 rt_rq
->avg
.load_sum
= rt_rq
->avg
.load_avg
* LOAD_AVG_MAX
;
2082 * TODO: If the sched_entity is already enqueued, should we have to update the
2083 * runnable load avg.
2087 static inline int test_and_clear_tg_rt_propagate(struct sched_rt_entity
*rt_se
)
2089 struct rt_rq
*rt_rq
= rt_se
->my_q
;
2091 if (!rt_rq
->propagate_avg
)
2094 rt_rq
->propagate_avg
= 0;
2098 /* Update task and its cfs_rq load average */
2099 static inline int propagate_entity_rt_load_avg(struct sched_rt_entity
*rt_se
)
2101 struct rt_rq
*rt_rq
;
2103 if (rt_entity_is_task(rt_se
))
2106 if (!test_and_clear_tg_rt_propagate(rt_se
))
2109 rt_rq
= rt_rq_of_se(rt_se
);
2111 rt_rq
->propagate_avg
= 1;
2113 update_tg_rt_util(rt_rq
, rt_se
);
2114 update_tg_rt_load(rt_rq
, rt_se
);
2119 static inline int propagate_entity_rt_load_avg(struct sched_rt_entity
*rt_se
) { };
2122 void update_rt_load_avg(u64 now
, struct sched_rt_entity
*rt_se
)
2124 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
2125 struct rq
*rq
= rq_of_rt_rq(rt_rq
);
2126 int cpu
= cpu_of(rq
);
2128 * Track task load average for carrying it to new CPU after migrated.
2130 if (rt_se
->avg
.last_update_time
)
2131 __update_load_avg(now
, cpu
, &rt_se
->avg
, scale_load_down(NICE_0_LOAD
),
2132 rt_rq
->curr
== rt_se
, NULL
);
2134 update_rt_rq_load_avg(now
, cpu
, rt_rq
, true);
2135 propagate_entity_rt_load_avg(rt_se
);
2137 if (entity_is_task(rt_se
))
2138 trace_sched_rt_load_avg_task(rt_task_of(rt_se
), &rt_se
->avg
);
2141 /* Only try algorithms three times */
2142 #define RT_MAX_TRIES 3
2144 static int pick_rt_task(struct rq
*rq
, struct task_struct
*p
, int cpu
)
2146 if (!task_running(rq
, p
) &&
2147 cpumask_test_cpu(cpu
, &p
->cpus_allowed
))
2153 * Return the highest pushable rq's task, which is suitable to be executed
2154 * on the cpu, NULL otherwise
2156 static struct task_struct
*pick_highest_pushable_task(struct rq
*rq
, int cpu
)
2158 struct plist_head
*head
= &rq
->rt
.pushable_tasks
;
2159 struct task_struct
*p
;
2161 if (!has_pushable_tasks(rq
))
2164 plist_for_each_entry(p
, head
, pushable_tasks
) {
2165 if (pick_rt_task(rq
, p
, cpu
))
2172 static DEFINE_PER_CPU(cpumask_var_t
, local_cpu_mask
);
2174 #ifdef CONFIG_SCHED_USE_FLUID_RT
2175 static unsigned int sched_rt_boost_threshold
= 60;
2177 static inline struct cpumask
*sched_group_cpus_rt(struct sched_group
*sg
)
2179 return to_cpumask(sg
->cpumask
);
2182 static inline int weight_from_rtprio(int prio
)
2184 int idx
= (prio
>> 1);
2187 return sched_prio_to_weight
[prio
- MAX_RT_PRIO
];
2189 if ((idx
<< 1) == prio
)
2190 return rtprio_to_weight
[idx
];
2192 return ((rtprio_to_weight
[idx
] + rtprio_to_weight
[idx
+1]) >> 1);
2196 * to find the best CPU in which the data is kept in cache-hot
2198 * In most of time, RT task is invoked because,
2199 * Case - I : it is already scheduled some time ago, or
2200 * Case - II: it is requested by some task without timedelay
2202 * In case-I, it's hardly to find the best CPU in cache-hot if the time is relatively long.
2203 * But in case-II, waker CPU is likely to keep the cache-hot data useful to wakee RT task.
2205 static inline int affordable_cpu(int cpu
, unsigned long task_load
)
2208 * If the task.state is 'TASK_INTERRUPTIBLE',
2209 * she is likely to call 'schedule()' explicitely, for waking up RT task.
2210 * and have something in common with it.
2212 if (cpu_curr(cpu
)->state
!= TASK_INTERRUPTIBLE
)
2216 * Waker CPU must accommodate the target RT task.
2218 if (capacity_of(cpu
) <= task_load
)
2222 * Future work (More concerns if needed):
2223 * - Min opportunity cost between the eviction of tasks and dismiss of target RT
2224 * : If evicted tasks are expecting too many damage for its execution,
2225 * Target RT should not be this CPU.
2226 * load(RT) >= Capa(CPU)/3 && load(evicted tasks) >= Capa(CPU)/3
2227 * - Identifying the relation:
2228 * : Is it possible to identify the relation (such as mutex owner and waiter)
2235 extern unsigned long cpu_util_wake(int cpu
, struct task_struct
*p
);
2236 extern unsigned long task_util(struct task_struct
*p
);
2239 * Must find the victim or recessive (not in lowest_mask)
2242 /* Future-safe accessor for struct task_struct's cpus_allowed. */
2243 #define rttsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)
2245 static int find_victim_rt_rq(struct task_struct
*task
, struct sched_group
*sg
, int *best_cpu
) {
2246 struct cpumask
*sg_cpus
= sched_group_cpus_rt(sg
);
2248 unsigned long victim_rtweight
, target_rtweight
, min_rtweight
;
2249 unsigned int victim_cpu_cap
, min_cpu_cap
= arch_scale_cpu_capacity(NULL
, task_cpu(task
));
2250 bool victim_rt
= true;
2255 target_rtweight
= task
->rt
.avg
.util_avg
* weight_from_rtprio(task
->prio
);
2256 min_rtweight
= target_rtweight
;
2258 for_each_cpu_and(i
, sg_cpus
, rttsk_cpus_allowed(task
)) {
2259 struct task_struct
*victim
= cpu_rq(i
)->curr
;
2261 if (victim
->nr_cpus_allowed
< 2)
2264 if (rt_task(victim
)) {
2265 victim_cpu_cap
= arch_scale_cpu_capacity(NULL
, i
);
2266 victim_rtweight
= victim
->rt
.avg
.util_avg
* weight_from_rtprio(victim
->prio
);
2268 if (min_cpu_cap
== victim_cpu_cap
) {
2269 if (victim_rtweight
< min_rtweight
) {
2270 min_rtweight
= victim_rtweight
;
2272 min_cpu_cap
= victim_cpu_cap
;
2276 * It's necessary to un-cap the cpu capacity when comparing
2277 * utilization of each CPU. This is why the Fluid RT tries to give
2278 * the green light on big CPU to the long-run RT task
2279 * in accordance with the priority.
2281 if (victim_rtweight
* min_cpu_cap
< min_rtweight
* victim_cpu_cap
) {
2282 min_rtweight
= victim_rtweight
;
2284 min_cpu_cap
= victim_cpu_cap
;
2288 /* If Non-RT CPU is exist, select it first. */
2295 if (*best_cpu
>= 0 && victim_rt
) {
2296 set_victim_flag(cpu_rq(*best_cpu
)->curr
);
2300 trace_sched_fluid_stat(task
, &task
->se
.avg
, *best_cpu
, "VICTIM-FAIR");
2302 trace_sched_fluid_stat(task
, &task
->se
.avg
, *best_cpu
, "VICTIM-RT");
2308 static int find_lowest_rq_fluid(struct task_struct
*task
, int wake_flags
)
2310 int cpu
, best_cpu
= -1;
2311 int prefer_cpu
= smp_processor_id(); /* Cache-hot with itself or waker (default). */
2313 struct cpumask
*lowest_mask
= this_cpu_cpumask_var_ptr(local_cpu_mask
);
2314 struct sched_domain
*sd
;
2315 struct sched_group
*sg
;
2316 u64 cpu_load
= ULLONG_MAX
, min_load
= ULLONG_MAX
, min_rt_load
= ULLONG_MAX
;
2317 int min_cpu
= -1, min_rt_cpu
= -1;
2319 /* Make sure the mask is initialized first */
2320 if (unlikely(!lowest_mask
))
2323 if (task
->nr_cpus_allowed
== 1)
2324 goto out
; /* No other targets possible */
2326 /* update the per-cpu local_cpu_mask (lowest_mask) */
2327 cpupri_find(&task_rq(task
)->rd
->cpupri
, task
, lowest_mask
);
2331 * Fluid Sched Core selection procedure:
2333 * 1. Cache hot : this cpu (waker if wake_list is null)
2334 * 2. idle CPU selection (prev_cpu first)
2335 * 3. recessive task first (prev_cpu first)
2336 * 4. victim task first (prev_cpu first)
2340 * 1. Cache hot : packing the callee and caller,
2341 * when there is nothing to run except callee
2343 if ((wake_flags
|| affordable_cpu(prefer_cpu
, task_util(task
))) &&
2344 cpumask_test_cpu(prefer_cpu
, cpu_online_mask
)) {
2345 best_cpu
= prefer_cpu
;
2346 trace_sched_fluid_stat(task
, &task
->se
.avg
, best_cpu
, "CACHE-HOT");
2350 prefer_cpu
= task_cpu(task
);
2353 * 2. idle CPU selection
2355 boosted
= (task
->rt
.avg
.util_avg
> sched_rt_boost_threshold
) ? (1) : (0);
2357 /* TODO: Need to refer the scheduling status of eHMP */
2358 for_each_cpu_and(cpu
, rttsk_cpus_allowed(task
), cpu_online_mask
){
2359 if (boosted
&& cpu
< cpumask_first(cpu_coregroup_mask(prefer_cpu
)))
2362 if (idle_cpu(cpu
)) {
2364 trace_sched_fluid_stat(task
, &task
->se
.avg
, best_cpu
, "IDLE-FIRST");
2372 rcu_dereference(per_cpu(sd_ea
, 0)) :
2373 rcu_dereference(per_cpu(sd_ea
, prefer_cpu
));
2381 * 3. recessive task first
2384 for_each_cpu_and(cpu
, sched_group_span(sg
), lowest_mask
) {
2386 cpu_load
= cpu_util_wake(cpu
, task
) + task_util(task
);
2388 if (rt_task(cpu_rq(cpu
)->curr
)) {
2389 if (cpu_load
< min_rt_load
||
2390 (cpu_load
== min_rt_load
&& cpu
== prefer_cpu
)) {
2391 min_rt_load
= cpu_load
;
2397 if (cpu_load
< min_load
||
2398 (cpu_load
== min_load
&& cpu
== prefer_cpu
)) {
2399 min_load
= cpu_load
;
2405 /* Fair recessive task : best min-load of non-rt cpu is exist? */
2407 ((capacity_of(min_cpu
) >= min_load
) || (min_cpu
== prefer_cpu
))) {
2409 trace_sched_fluid_stat(task
, &task
->se
.avg
, best_cpu
, "FAIR-RECESS");
2413 /* RT recessive task : best min-load of rt cpu is exist? */
2414 if (min_rt_cpu
>= 0 &&
2415 ((capacity_of(min_rt_cpu
) >= min_rt_load
) || (min_rt_cpu
== prefer_cpu
))) {
2416 best_cpu
= min_rt_cpu
;
2417 trace_sched_fluid_stat(task
, &task
->se
.avg
, best_cpu
, "RT-RECESS");
2421 } while (sg
= sg
->next
, sg
!= sd
->groups
);
2422 /* need to check the method for traversing the sg */
2427 * 4. victim task first
2430 if (find_victim_rt_rq(task
, sg
, &best_cpu
) != -1)
2432 } while (sg
= sg
->next
, sg
!= sd
->groups
);
2435 best_cpu
= prefer_cpu
;
2440 if (!cpumask_test_cpu(best_cpu
, cpu_online_mask
))
2445 #endif /* CONFIG_SCHED_USE_FLUID_RT */
2447 #ifdef CONFIG_SCHED_USE_FLUID_RT
2448 static int find_lowest_rq(struct task_struct
*task
, int wake_flags
)
2450 static int find_lowest_rq(struct task_struct
*task
)
2453 #ifdef CONFIG_SCHED_USE_FLUID_RT
2454 return find_lowest_rq_fluid(task
, wake_flags
);
2456 struct sched_domain
*sd
;
2457 struct cpumask
*lowest_mask
= this_cpu_cpumask_var_ptr(local_cpu_mask
);
2458 int this_cpu
= smp_processor_id();
2459 int cpu
= task_cpu(task
);
2461 /* Make sure the mask is initialized first */
2462 if (unlikely(!lowest_mask
))
2465 if (task
->nr_cpus_allowed
== 1)
2466 return -1; /* No other targets possible */
2468 if (!cpupri_find(&task_rq(task
)->rd
->cpupri
, task
, lowest_mask
))
2469 return -1; /* No targets found */
2472 * At this point we have built a mask of cpus representing the
2473 * lowest priority tasks in the system. Now we want to elect
2474 * the best one based on our affinity and topology.
2476 * We prioritize the last cpu that the task executed on since
2477 * it is most likely cache-hot in that location.
2479 if (cpumask_test_cpu(cpu
, lowest_mask
))
2483 * Otherwise, we consult the sched_domains span maps to figure
2484 * out which cpu is logically closest to our hot cache data.
2486 if (!cpumask_test_cpu(this_cpu
, lowest_mask
))
2487 this_cpu
= -1; /* Skip this_cpu opt if not among lowest */
2490 for_each_domain(cpu
, sd
) {
2491 if (sd
->flags
& SD_WAKE_AFFINE
) {
2495 * "this_cpu" is cheaper to preempt than a
2498 if (this_cpu
!= -1 &&
2499 cpumask_test_cpu(this_cpu
, sched_domain_span(sd
))) {
2504 best_cpu
= cpumask_first_and(lowest_mask
,
2505 sched_domain_span(sd
));
2506 if (best_cpu
< nr_cpu_ids
) {
2515 * And finally, if there were no matches within the domains
2516 * just give the caller *something* to work with from the compatible
2522 cpu
= cpumask_any(lowest_mask
);
2523 if (cpu
< nr_cpu_ids
)
2526 #endif /* CONFIG_SCHED_USE_FLUID_RT */
2529 /* Will lock the rq it finds */
2530 static struct rq
*find_lock_lowest_rq(struct task_struct
*task
, struct rq
*rq
)
2532 struct rq
*lowest_rq
= NULL
;
2536 for (tries
= 0; tries
< RT_MAX_TRIES
; tries
++) {
2537 #ifdef CONFIG_SCHED_USE_FLUID_RT
2538 cpu
= find_lowest_rq(task
, 0);
2540 cpu
= find_lowest_rq(task
);
2542 if ((cpu
== -1) || (cpu
== rq
->cpu
))
2545 lowest_rq
= cpu_rq(cpu
);
2547 #ifdef CONFIG_SCHED_USE_FLUID_RT
2549 * Even though the lowest rq has a task of higher priority,
2550 * FluidRT can expel it (victim task) if it has small utilization,
2551 * or is not current task. Just keep trying.
2554 if (lowest_rq
->rt
.highest_prio
.curr
<= task
->prio
) {
2556 * Target rq has tasks of equal or higher priority,
2557 * retrying does not release any lock and is unlikely
2558 * to yield a different result.
2565 /* if the prio of this runqueue changed, try again */
2566 if (double_lock_balance(rq
, lowest_rq
)) {
2568 * We had to unlock the run queue. In
2569 * the mean time, task could have
2570 * migrated already or had its affinity changed.
2571 * Also make sure that it wasn't scheduled on its rq.
2573 if (unlikely(task_rq(task
) != rq
||
2574 !cpumask_test_cpu(lowest_rq
->cpu
, &task
->cpus_allowed
) ||
2575 task_running(rq
, task
) ||
2577 !task_on_rq_queued(task
))) {
2579 double_unlock_balance(rq
, lowest_rq
);
2585 #ifdef CONFIG_SCHED_USE_FLUID_RT
2586 /* task is still rt task */
2587 if (likely(rt_task(task
)))
2590 /* If this rq is still suitable use it. */
2591 if (lowest_rq
->rt
.highest_prio
.curr
> task
->prio
)
2595 double_unlock_balance(rq
, lowest_rq
);
2603 static struct task_struct
*pick_next_pushable_task(struct rq
*rq
)
2605 struct task_struct
*p
;
2607 if (!has_pushable_tasks(rq
))
2610 p
= plist_first_entry(&rq
->rt
.pushable_tasks
,
2611 struct task_struct
, pushable_tasks
);
2613 BUG_ON(rq
->cpu
!= task_cpu(p
));
2614 BUG_ON(task_current(rq
, p
));
2615 BUG_ON(p
->nr_cpus_allowed
<= 1);
2617 BUG_ON(!task_on_rq_queued(p
));
2618 BUG_ON(!rt_task(p
));
2624 * If the current CPU has more than one RT task, see if the non
2625 * running task can migrate over to a CPU that is running a task
2626 * of lesser priority.
2628 static int push_rt_task(struct rq
*rq
)
2630 struct task_struct
*next_task
;
2631 struct rq
*lowest_rq
;
2634 if (!rq
->rt
.overloaded
)
2637 next_task
= pick_next_pushable_task(rq
);
2642 if (unlikely(next_task
== rq
->curr
)) {
2648 * It's possible that the next_task slipped in of
2649 * higher priority than current. If that's the case
2650 * just reschedule current.
2652 if (unlikely(next_task
->prio
< rq
->curr
->prio
)) {
2657 /* We might release rq lock */
2658 get_task_struct(next_task
);
2660 /* find_lock_lowest_rq locks the rq if found */
2661 lowest_rq
= find_lock_lowest_rq(next_task
, rq
);
2663 struct task_struct
*task
;
2665 * find_lock_lowest_rq releases rq->lock
2666 * so it is possible that next_task has migrated.
2668 * We need to make sure that the task is still on the same
2669 * run-queue and is also still the next task eligible for
2672 task
= pick_next_pushable_task(rq
);
2673 if (task
== next_task
) {
2675 * The task hasn't migrated, and is still the next
2676 * eligible task, but we failed to find a run-queue
2677 * to push it to. Do not retry in this case, since
2678 * other cpus will pull from us when ready.
2684 /* No more tasks, just exit */
2688 * Something has shifted, try again.
2690 put_task_struct(next_task
);
2695 deactivate_task(rq
, next_task
, 0);
2696 next_task
->on_rq
= TASK_ON_RQ_MIGRATING
;
2697 set_task_cpu(next_task
, lowest_rq
->cpu
);
2698 next_task
->on_rq
= TASK_ON_RQ_QUEUED
;
2699 activate_task(lowest_rq
, next_task
, 0);
2702 resched_curr(lowest_rq
);
2704 double_unlock_balance(rq
, lowest_rq
);
2707 put_task_struct(next_task
);
2712 static void push_rt_tasks(struct rq
*rq
)
2714 /* push_rt_task will return true if it moved an RT */
2715 while (push_rt_task(rq
))
2719 #ifdef HAVE_RT_PUSH_IPI
2722 * When a high priority task schedules out from a CPU and a lower priority
2723 * task is scheduled in, a check is made to see if there's any RT tasks
2724 * on other CPUs that are waiting to run because a higher priority RT task
2725 * is currently running on its CPU. In this case, the CPU with multiple RT
2726 * tasks queued on it (overloaded) needs to be notified that a CPU has opened
2727 * up that may be able to run one of its non-running queued RT tasks.
2729 * All CPUs with overloaded RT tasks need to be notified as there is currently
2730 * no way to know which of these CPUs have the highest priority task waiting
2731 * to run. Instead of trying to take a spinlock on each of these CPUs,
2732 * which has shown to cause large latency when done on machines with many
2733 * CPUs, sending an IPI to the CPUs to have them push off the overloaded
2734 * RT tasks waiting to run.
2736 * Just sending an IPI to each of the CPUs is also an issue, as on large
2737 * count CPU machines, this can cause an IPI storm on a CPU, especially
2738 * if its the only CPU with multiple RT tasks queued, and a large number
2739 * of CPUs scheduling a lower priority task at the same time.
2741 * Each root domain has its own irq work function that can iterate over
2742 * all CPUs with RT overloaded tasks. Since all CPUs with overloaded RT
2743 * tassk must be checked if there's one or many CPUs that are lowering
2744 * their priority, there's a single irq work iterator that will try to
2745 * push off RT tasks that are waiting to run.
2747 * When a CPU schedules a lower priority task, it will kick off the
2748 * irq work iterator that will jump to each CPU with overloaded RT tasks.
2749 * As it only takes the first CPU that schedules a lower priority task
2750 * to start the process, the rto_start variable is incremented and if
2751 * the atomic result is one, then that CPU will try to take the rto_lock.
2752 * This prevents high contention on the lock as the process handles all
2753 * CPUs scheduling lower priority tasks.
2755 * All CPUs that are scheduling a lower priority task will increment the
2756 * rt_loop_next variable. This will make sure that the irq work iterator
2757 * checks all RT overloaded CPUs whenever a CPU schedules a new lower
2758 * priority task, even if the iterator is in the middle of a scan. Incrementing
2759 * the rt_loop_next will cause the iterator to perform another scan.
2762 static int rto_next_cpu(struct root_domain
*rd
)
2768 * When starting the IPI RT pushing, the rto_cpu is set to -1,
2769 * rt_next_cpu() will simply return the first CPU found in
2772 * If rto_next_cpu() is called with rto_cpu is a valid cpu, it
2773 * will return the next CPU found in the rto_mask.
2775 * If there are no more CPUs left in the rto_mask, then a check is made
2776 * against rto_loop and rto_loop_next. rto_loop is only updated with
2777 * the rto_lock held, but any CPU may increment the rto_loop_next
2778 * without any locking.
2782 /* When rto_cpu is -1 this acts like cpumask_first() */
2783 cpu
= cpumask_next(rd
->rto_cpu
, rd
->rto_mask
);
2787 if (cpu
< nr_cpu_ids
)
2793 * ACQUIRE ensures we see the @rto_mask changes
2794 * made prior to the @next value observed.
2796 * Matches WMB in rt_set_overload().
2798 next
= atomic_read_acquire(&rd
->rto_loop_next
);
2800 if (rd
->rto_loop
== next
)
2803 rd
->rto_loop
= next
;
2809 static inline bool rto_start_trylock(atomic_t
*v
)
2811 return !atomic_cmpxchg_acquire(v
, 0, 1);
2814 static inline void rto_start_unlock(atomic_t
*v
)
2816 atomic_set_release(v
, 0);
2819 static void tell_cpu_to_push(struct rq
*rq
)
2823 /* Keep the loop going if the IPI is currently active */
2824 atomic_inc(&rq
->rd
->rto_loop_next
);
2826 /* Only one CPU can initiate a loop at a time */
2827 if (!rto_start_trylock(&rq
->rd
->rto_loop_start
))
2830 raw_spin_lock(&rq
->rd
->rto_lock
);
2833 * The rto_cpu is updated under the lock, if it has a valid cpu
2834 * then the IPI is still running and will continue due to the
2835 * update to loop_next, and nothing needs to be done here.
2836 * Otherwise it is finishing up and an ipi needs to be sent.
2838 if (rq
->rd
->rto_cpu
< 0)
2839 cpu
= rto_next_cpu(rq
->rd
);
2841 raw_spin_unlock(&rq
->rd
->rto_lock
);
2843 rto_start_unlock(&rq
->rd
->rto_loop_start
);
2846 /* Make sure the rd does not get freed while pushing */
2847 sched_get_rd(rq
->rd
);
2848 irq_work_queue_on(&rq
->rd
->rto_push_work
, cpu
);
2852 /* Called from hardirq context */
2853 void rto_push_irq_work_func(struct irq_work
*work
)
2855 struct root_domain
*rd
=
2856 container_of(work
, struct root_domain
, rto_push_work
);
2863 * We do not need to grab the lock to check for has_pushable_tasks.
2864 * When it gets updated, a check is made if a push is possible.
2866 if (has_pushable_tasks(rq
)) {
2867 raw_spin_lock(&rq
->lock
);
2869 raw_spin_unlock(&rq
->lock
);
2872 raw_spin_lock(&rd
->rto_lock
);
2874 /* Pass the IPI to the next rt overloaded queue */
2875 cpu
= rto_next_cpu(rd
);
2877 raw_spin_unlock(&rd
->rto_lock
);
2884 /* Try the next RT overloaded CPU */
2885 irq_work_queue_on(&rd
->rto_push_work
, cpu
);
2887 #endif /* HAVE_RT_PUSH_IPI */
2889 static void pull_rt_task(struct rq
*this_rq
)
2891 int this_cpu
= this_rq
->cpu
, cpu
;
2892 bool resched
= false;
2893 struct task_struct
*p
;
2895 int rt_overload_count
= rt_overloaded(this_rq
);
2897 if (likely(!rt_overload_count
))
2901 * Match the barrier from rt_set_overloaded; this guarantees that if we
2902 * see overloaded we must also see the rto_mask bit.
2906 /* If we are the only overloaded CPU do nothing */
2907 if (rt_overload_count
== 1 &&
2908 cpumask_test_cpu(this_rq
->cpu
, this_rq
->rd
->rto_mask
))
2911 #ifdef HAVE_RT_PUSH_IPI
2912 if (sched_feat(RT_PUSH_IPI
)) {
2913 tell_cpu_to_push(this_rq
);
2918 for_each_cpu(cpu
, this_rq
->rd
->rto_mask
) {
2919 if (this_cpu
== cpu
)
2922 src_rq
= cpu_rq(cpu
);
2925 * Don't bother taking the src_rq->lock if the next highest
2926 * task is known to be lower-priority than our current task.
2927 * This may look racy, but if this value is about to go
2928 * logically higher, the src_rq will push this task away.
2929 * And if its going logically lower, we do not care
2931 if (src_rq
->rt
.highest_prio
.next
>=
2932 this_rq
->rt
.highest_prio
.curr
)
2936 * We can potentially drop this_rq's lock in
2937 * double_lock_balance, and another CPU could
2940 double_lock_balance(this_rq
, src_rq
);
2943 * We can pull only a task, which is pushable
2944 * on its rq, and no others.
2946 p
= pick_highest_pushable_task(src_rq
, this_cpu
);
2949 * Do we have an RT task that preempts
2950 * the to-be-scheduled task?
2952 if (p
&& (p
->prio
< this_rq
->rt
.highest_prio
.curr
)) {
2953 WARN_ON(p
== src_rq
->curr
);
2954 WARN_ON(!task_on_rq_queued(p
));
2957 * There's a chance that p is higher in priority
2958 * than what's currently running on its cpu.
2959 * This is just that p is wakeing up and hasn't
2960 * had a chance to schedule. We only pull
2961 * p if it is lower in priority than the
2962 * current task on the run queue
2964 if (p
->prio
< src_rq
->curr
->prio
)
2969 deactivate_task(src_rq
, p
, 0);
2970 p
->on_rq
= TASK_ON_RQ_MIGRATING
;
2971 set_task_cpu(p
, this_cpu
);
2972 p
->on_rq
= TASK_ON_RQ_QUEUED
;
2973 activate_task(this_rq
, p
, 0);
2975 * We continue with the search, just in
2976 * case there's an even higher prio task
2977 * in another runqueue. (low likelihood
2982 double_unlock_balance(this_rq
, src_rq
);
2986 resched_curr(this_rq
);
2990 * If we are not running and we are not going to reschedule soon, we should
2991 * try to push tasks away now
2993 static void task_woken_rt(struct rq
*rq
, struct task_struct
*p
)
2995 if (!task_running(rq
, p
) &&
2996 !test_tsk_need_resched(rq
->curr
) &&
2997 p
->nr_cpus_allowed
> 1 &&
2998 (dl_task(rq
->curr
) || rt_task(rq
->curr
)) &&
2999 (rq
->curr
->nr_cpus_allowed
< 2 ||
3000 rq
->curr
->prio
<= p
->prio
))
3004 /* Assumes rq->lock is held */
3005 static void rq_online_rt(struct rq
*rq
)
3007 if (rq
->rt
.overloaded
)
3008 rt_set_overload(rq
);
3010 __enable_runtime(rq
);
3012 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, rq
->rt
.highest_prio
.curr
);
3015 /* Assumes rq->lock is held */
3016 static void rq_offline_rt(struct rq
*rq
)
3018 if (rq
->rt
.overloaded
)
3019 rt_clear_overload(rq
);
3021 __disable_runtime(rq
);
3023 cpupri_set(&rq
->rd
->cpupri
, rq
->cpu
, CPUPRI_INVALID
);
3027 * When switch from the rt queue, we bring ourselves to a position
3028 * that we might want to pull RT tasks from other runqueues.
3030 static void switched_from_rt(struct rq
*rq
, struct task_struct
*p
)
3032 detach_task_rt_rq(p
);
3034 * If there are other RT tasks then we will reschedule
3035 * and the scheduling of the other RT tasks will handle
3036 * the balancing. But if we are the last RT task
3037 * we may need to handle the pulling of RT tasks
3040 if (!task_on_rq_queued(p
) || rq
->rt
.rt_nr_running
)
3043 queue_pull_task(rq
);
3046 void __init
init_sched_rt_class(void)
3050 for_each_possible_cpu(i
) {
3051 zalloc_cpumask_var_node(&per_cpu(local_cpu_mask
, i
),
3052 GFP_KERNEL
, cpu_to_node(i
));
3056 void update_rt_load_avg(u64 now
, struct sched_rt_entity
*rt_se
)
3059 #endif /* CONFIG_SMP */
3062 copy_sched_avg(struct sched_avg
*from
, struct sched_avg
*to
, unsigned int ratio
);
3065 * When switching a task to RT, we may overload the runqueue
3066 * with RT tasks. In this case we try to push them off to
3069 static void switched_to_rt(struct rq
*rq
, struct task_struct
*p
)
3071 /* Copy fair sched avg into rt sched avg */
3072 copy_sched_avg(&p
->se
.avg
, &p
->rt
.avg
, 100);
3074 * If we are already running, then there's nothing
3075 * that needs to be done. But if we are not running
3076 * we may need to preempt the current running task.
3077 * If that current running task is also an RT task
3078 * then see if we can move to another run queue.
3080 if (task_on_rq_queued(p
) && rq
->curr
!= p
) {
3082 if (p
->nr_cpus_allowed
> 1 && rq
->rt
.overloaded
)
3083 queue_push_tasks(rq
);
3084 #endif /* CONFIG_SMP */
3085 if (p
->prio
< rq
->curr
->prio
&& cpu_online(cpu_of(rq
)))
3091 * Priority of the task has changed. This may cause
3092 * us to initiate a push or pull.
3095 prio_changed_rt(struct rq
*rq
, struct task_struct
*p
, int oldprio
)
3097 if (!task_on_rq_queued(p
))
3100 if (rq
->curr
== p
) {
3103 * If our priority decreases while running, we
3104 * may need to pull tasks to this runqueue.
3106 if (oldprio
< p
->prio
)
3107 queue_pull_task(rq
);
3110 * If there's a higher priority task waiting to run
3113 if (p
->prio
> rq
->rt
.highest_prio
.curr
)
3116 /* For UP simply resched on drop of prio */
3117 if (oldprio
< p
->prio
)
3119 #endif /* CONFIG_SMP */
3122 * This task is not running, but if it is
3123 * greater than the current running task
3126 if (p
->prio
< rq
->curr
->prio
)
3131 #ifdef CONFIG_POSIX_TIMERS
3132 static void watchdog(struct rq
*rq
, struct task_struct
*p
)
3134 unsigned long soft
, hard
;
3136 /* max may change after cur was read, this will be fixed next tick */
3137 soft
= task_rlimit(p
, RLIMIT_RTTIME
);
3138 hard
= task_rlimit_max(p
, RLIMIT_RTTIME
);
3140 if (soft
!= RLIM_INFINITY
) {
3143 if (p
->rt
.watchdog_stamp
!= jiffies
) {
3145 p
->rt
.watchdog_stamp
= jiffies
;
3148 next
= DIV_ROUND_UP(min(soft
, hard
), USEC_PER_SEC
/HZ
);
3149 if (p
->rt
.timeout
> next
)
3150 p
->cputime_expires
.sched_exp
= p
->se
.sum_exec_runtime
;
3154 static inline void watchdog(struct rq
*rq
, struct task_struct
*p
) { }
3157 static void task_tick_rt(struct rq
*rq
, struct task_struct
*p
, int queued
)
3159 struct sched_rt_entity
*rt_se
= &p
->rt
;
3160 u64 now
= rq_clock_task(rq
);
3164 for_each_sched_rt_entity(rt_se
)
3165 update_rt_load_avg(now
, rt_se
);
3170 * RR tasks need a special form of timeslice management.
3171 * FIFO tasks have no timeslices.
3173 if (p
->policy
!= SCHED_RR
)
3176 if (--p
->rt
.time_slice
)
3179 p
->rt
.time_slice
= sched_rr_timeslice
;
3182 * Requeue to the end of queue if we (and all of our ancestors) are not
3183 * the only element on the queue
3185 for_each_sched_rt_entity(rt_se
) {
3186 if (rt_se
->run_list
.prev
!= rt_se
->run_list
.next
) {
3187 requeue_task_rt(rq
, p
, 0);
3194 static void set_curr_task_rt(struct rq
*rq
)
3196 struct task_struct
*p
= rq
->curr
;
3197 struct sched_rt_entity
*rt_se
= &p
->rt
;
3199 p
->se
.exec_start
= rq_clock_task(rq
);
3201 for_each_sched_rt_entity(rt_se
) {
3202 struct rt_rq
*rt_rq
= rt_rq_of_se(rt_se
);
3203 rt_rq
->curr
= rt_se
;
3206 /* The running task is never eligible for pushing */
3207 dequeue_pushable_task(rq
, p
);
3210 static unsigned int get_rr_interval_rt(struct rq
*rq
, struct task_struct
*task
)
3213 * Time slice is 0 for SCHED_FIFO tasks
3215 if (task
->policy
== SCHED_RR
)
3216 return sched_rr_timeslice
;
3221 const struct sched_class rt_sched_class
= {
3222 .next
= &fair_sched_class
,
3223 .enqueue_task
= enqueue_task_rt
,
3224 .dequeue_task
= dequeue_task_rt
,
3225 .yield_task
= yield_task_rt
,
3227 .check_preempt_curr
= check_preempt_curr_rt
,
3229 .pick_next_task
= pick_next_task_rt
,
3230 .put_prev_task
= put_prev_task_rt
,
3233 .select_task_rq
= select_task_rq_rt
,
3235 .migrate_task_rq
= migrate_task_rq_rt
,
3236 .task_dead
= task_dead_rt
,
3237 .set_cpus_allowed
= set_cpus_allowed_common
,
3238 .rq_online
= rq_online_rt
,
3239 .rq_offline
= rq_offline_rt
,
3240 .task_woken
= task_woken_rt
,
3241 .switched_from
= switched_from_rt
,
3244 .set_curr_task
= set_curr_task_rt
,
3245 .task_tick
= task_tick_rt
,
3247 .get_rr_interval
= get_rr_interval_rt
,
3249 .prio_changed
= prio_changed_rt
,
3250 .switched_to
= switched_to_rt
,
3252 .update_curr
= update_curr_rt
,
3253 #ifdef CONFIG_RT_GROUP_SCHED
3254 .task_change_group
= task_change_group_rt
,
3258 #ifdef CONFIG_RT_GROUP_SCHED
3260 * Ensure that the real time constraints are schedulable.
3262 static DEFINE_MUTEX(rt_constraints_mutex
);
3264 /* Must be called with tasklist_lock held */
3265 static inline int tg_has_rt_tasks(struct task_group
*tg
)
3267 struct task_struct
*g
, *p
;
3270 * Autogroups do not have RT tasks; see autogroup_create().
3272 if (task_group_is_autogroup(tg
))
3275 for_each_process_thread(g
, p
) {
3276 if (rt_task(p
) && task_group(p
) == tg
)
3283 struct rt_schedulable_data
{
3284 struct task_group
*tg
;
3289 static int tg_rt_schedulable(struct task_group
*tg
, void *data
)
3291 struct rt_schedulable_data
*d
= data
;
3292 struct task_group
*child
;
3293 unsigned long total
, sum
= 0;
3294 u64 period
, runtime
;
3296 period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
3297 runtime
= tg
->rt_bandwidth
.rt_runtime
;
3300 period
= d
->rt_period
;
3301 runtime
= d
->rt_runtime
;
3305 * Cannot have more runtime than the period.
3307 if (runtime
> period
&& runtime
!= RUNTIME_INF
)
3311 * Ensure we don't starve existing RT tasks.
3313 if (rt_bandwidth_enabled() && !runtime
&& tg_has_rt_tasks(tg
))
3316 total
= to_ratio(period
, runtime
);
3319 * Nobody can have more than the global setting allows.
3321 if (total
> to_ratio(global_rt_period(), global_rt_runtime()))
3325 * The sum of our children's runtime should not exceed our own.
3327 list_for_each_entry_rcu(child
, &tg
->children
, siblings
) {
3328 period
= ktime_to_ns(child
->rt_bandwidth
.rt_period
);
3329 runtime
= child
->rt_bandwidth
.rt_runtime
;
3331 if (child
== d
->tg
) {
3332 period
= d
->rt_period
;
3333 runtime
= d
->rt_runtime
;
3336 sum
+= to_ratio(period
, runtime
);
3345 static int __rt_schedulable(struct task_group
*tg
, u64 period
, u64 runtime
)
3349 struct rt_schedulable_data data
= {
3351 .rt_period
= period
,
3352 .rt_runtime
= runtime
,
3356 ret
= walk_tg_tree(tg_rt_schedulable
, tg_nop
, &data
);
3362 static int tg_set_rt_bandwidth(struct task_group
*tg
,
3363 u64 rt_period
, u64 rt_runtime
)
3368 * Disallowing the root group RT runtime is BAD, it would disallow the
3369 * kernel creating (and or operating) RT threads.
3371 if (tg
== &root_task_group
&& rt_runtime
== 0)
3374 /* No period doesn't make any sense. */
3378 mutex_lock(&rt_constraints_mutex
);
3379 read_lock(&tasklist_lock
);
3380 err
= __rt_schedulable(tg
, rt_period
, rt_runtime
);
3384 raw_spin_lock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
3385 tg
->rt_bandwidth
.rt_period
= ns_to_ktime(rt_period
);
3386 tg
->rt_bandwidth
.rt_runtime
= rt_runtime
;
3388 for_each_possible_cpu(i
) {
3389 struct rt_rq
*rt_rq
= tg
->rt_rq
[i
];
3391 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
3392 rt_rq
->rt_runtime
= rt_runtime
;
3393 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
3395 raw_spin_unlock_irq(&tg
->rt_bandwidth
.rt_runtime_lock
);
3397 read_unlock(&tasklist_lock
);
3398 mutex_unlock(&rt_constraints_mutex
);
3403 int sched_group_set_rt_runtime(struct task_group
*tg
, long rt_runtime_us
)
3405 u64 rt_runtime
, rt_period
;
3407 rt_period
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
3408 rt_runtime
= (u64
)rt_runtime_us
* NSEC_PER_USEC
;
3409 if (rt_runtime_us
< 0)
3410 rt_runtime
= RUNTIME_INF
;
3411 else if ((u64
)rt_runtime_us
> U64_MAX
/ NSEC_PER_USEC
)
3414 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
3417 long sched_group_rt_runtime(struct task_group
*tg
)
3421 if (tg
->rt_bandwidth
.rt_runtime
== RUNTIME_INF
)
3424 rt_runtime_us
= tg
->rt_bandwidth
.rt_runtime
;
3425 do_div(rt_runtime_us
, NSEC_PER_USEC
);
3426 return rt_runtime_us
;
3429 int sched_group_set_rt_period(struct task_group
*tg
, u64 rt_period_us
)
3431 u64 rt_runtime
, rt_period
;
3433 if (rt_period_us
> U64_MAX
/ NSEC_PER_USEC
)
3436 rt_period
= rt_period_us
* NSEC_PER_USEC
;
3437 rt_runtime
= tg
->rt_bandwidth
.rt_runtime
;
3439 return tg_set_rt_bandwidth(tg
, rt_period
, rt_runtime
);
3442 long sched_group_rt_period(struct task_group
*tg
)
3446 rt_period_us
= ktime_to_ns(tg
->rt_bandwidth
.rt_period
);
3447 do_div(rt_period_us
, NSEC_PER_USEC
);
3448 return rt_period_us
;
3451 static int sched_rt_global_constraints(void)
3455 mutex_lock(&rt_constraints_mutex
);
3456 read_lock(&tasklist_lock
);
3457 ret
= __rt_schedulable(NULL
, 0, 0);
3458 read_unlock(&tasklist_lock
);
3459 mutex_unlock(&rt_constraints_mutex
);
3464 int sched_rt_can_attach(struct task_group
*tg
, struct task_struct
*tsk
)
3466 /* Don't accept realtime tasks when there is no way for them to run */
3467 if (rt_task(tsk
) && tg
->rt_bandwidth
.rt_runtime
== 0)
3473 #else /* !CONFIG_RT_GROUP_SCHED */
3474 static int sched_rt_global_constraints(void)
3476 unsigned long flags
;
3479 raw_spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
3480 for_each_possible_cpu(i
) {
3481 struct rt_rq
*rt_rq
= &cpu_rq(i
)->rt
;
3483 raw_spin_lock(&rt_rq
->rt_runtime_lock
);
3484 rt_rq
->rt_runtime
= global_rt_runtime();
3485 raw_spin_unlock(&rt_rq
->rt_runtime_lock
);
3487 raw_spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
3491 #endif /* CONFIG_RT_GROUP_SCHED */
3493 static int sched_rt_global_validate(void)
3495 if (sysctl_sched_rt_period
<= 0)
3498 if ((sysctl_sched_rt_runtime
!= RUNTIME_INF
) &&
3499 (sysctl_sched_rt_runtime
> sysctl_sched_rt_period
))
3505 static void sched_rt_do_global(void)
3507 unsigned long flags
;
3509 raw_spin_lock_irqsave(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
3510 def_rt_bandwidth
.rt_runtime
= global_rt_runtime();
3511 def_rt_bandwidth
.rt_period
= ns_to_ktime(global_rt_period());
3512 raw_spin_unlock_irqrestore(&def_rt_bandwidth
.rt_runtime_lock
, flags
);
3515 int sched_rt_handler(struct ctl_table
*table
, int write
,
3516 void __user
*buffer
, size_t *lenp
,
3519 int old_period
, old_runtime
;
3520 static DEFINE_MUTEX(mutex
);
3524 old_period
= sysctl_sched_rt_period
;
3525 old_runtime
= sysctl_sched_rt_runtime
;
3527 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3529 if (!ret
&& write
) {
3530 ret
= sched_rt_global_validate();
3534 ret
= sched_dl_global_validate();
3538 ret
= sched_rt_global_constraints();
3542 sched_rt_do_global();
3543 sched_dl_do_global();
3547 sysctl_sched_rt_period
= old_period
;
3548 sysctl_sched_rt_runtime
= old_runtime
;
3550 mutex_unlock(&mutex
);
3555 int sched_rr_handler(struct ctl_table
*table
, int write
,
3556 void __user
*buffer
, size_t *lenp
,
3560 static DEFINE_MUTEX(mutex
);
3563 ret
= proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
3565 * Make sure that internally we keep jiffies.
3566 * Also, writing zero resets the timeslice to default:
3568 if (!ret
&& write
) {
3569 sched_rr_timeslice
=
3570 sysctl_sched_rr_timeslice
<= 0 ? RR_TIMESLICE
:
3571 msecs_to_jiffies(sysctl_sched_rr_timeslice
);
3573 mutex_unlock(&mutex
);
3577 #ifdef CONFIG_SCHED_DEBUG
3578 void print_rt_stats(struct seq_file
*m
, int cpu
)
3581 struct rt_rq
*rt_rq
;
3584 for_each_rt_rq(rt_rq
, iter
, cpu_rq(cpu
))
3585 print_rt_rq(m
, cpu
, rt_rq
);
3588 #endif /* CONFIG_SCHED_DEBUG */