Skip to content

Commit 804d402

Browse files
Qais Yousefingomolnar
authored andcommitted
sched/rt: Make RT capacity-aware
Capacity Awareness refers to the fact that on heterogeneous systems (like Arm big.LITTLE), the capacity of the CPUs is not uniform, hence when placing tasks we need to be aware of this difference of CPU capacities. In such scenarios we want to ensure that the selected CPU has enough capacity to meet the requirement of the running task. Enough capacity means here that capacity_orig_of(cpu) >= task.requirement. The definition of task.requirement is dependent on the scheduling class. For CFS, utilization is used to select a CPU that has >= capacity value than the cfs_task.util. capacity_orig_of(cpu) >= cfs_task.util DL isn't capacity aware at the moment but can make use of the bandwidth reservation to implement that in a similar manner CFS uses utilization. The following patchset implements that: https://lore.kernel.org/lkml/20190506044836.2914-1-luca.abeni@santannapisa.it/ capacity_orig_of(cpu)/SCHED_CAPACITY >= dl_deadline/dl_runtime For RT we don't have a per task utilization signal and we lack any information in general about what performance requirement the RT task needs. But with the introduction of uclamp, RT tasks can now control that by setting uclamp_min to guarantee a minimum performance point. ATM the uclamp value are only used for frequency selection; but on heterogeneous systems this is not enough and we need to ensure that the capacity of the CPU is >= uclamp_min. Which is what implemented here. capacity_orig_of(cpu) >= rt_task.uclamp_min Note that by default uclamp.min is 1024, which means that RT tasks will always be biased towards the big CPUs, which make for a better more predictable behavior for the default case. Must stress that the bias acts as a hint rather than a definite placement strategy. For example, if all big cores are busy executing other RT tasks we can't guarantee that a new RT task will be placed there. On non-heterogeneous systems the original behavior of RT should be retained. Similarly if uclamp is not selected in the config. [ mingo: Minor edits to comments. ] Signed-off-by: Qais Yousef <qais.yousef@arm.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Dietmar Eggemann <dietmar.eggemann@arm.com> Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: https://lkml.kernel.org/r/20191009104611.15363-1-qais.yousef@arm.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 1d42509 commit 804d402

3 files changed

Lines changed: 94 additions & 18 deletions

File tree

‎kernel/sched/cpupri.c‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ static int convert_prio(int prio)
4646
* @cp: The cpupri context
4747
* @p: The task
4848
* @lowest_mask: A mask to fill in with selected CPUs (or NULL)
49+
* @fitness_fn: A pointer to a function to do custom checks whether the CPU
50+
* fits a specific criteria so that we only return those CPUs.
4951
*
5052
* Note: This function returns the recommended CPUs as calculated during the
5153
* current invocation. By the time the call returns, the CPUs may have in
@@ -57,7 +59,8 @@ static int convert_prio(int prio)
5759
* Return: (int)bool - CPUs were found
5860
*/
5961
int cpupri_find(struct cpupri *cp, struct task_struct *p,
60-
struct cpumask *lowest_mask)
62+
struct cpumask *lowest_mask,
63+
bool (*fitness_fn)(struct task_struct *p, int cpu))
6164
{
6265
int idx = 0;
6366
int task_pri = convert_prio(p->prio);
@@ -98,6 +101,8 @@ int cpupri_find(struct cpupri *cp, struct task_struct *p,
98101
continue;
99102

100103
if (lowest_mask) {
104+
int cpu;
105+
101106
cpumask_and(lowest_mask, p->cpus_ptr, vec->mask);
102107

103108
/*
@@ -108,7 +113,23 @@ int cpupri_find(struct cpupri *cp, struct task_struct *p,
108113
* condition, simply act as though we never hit this
109114
* priority level and continue on.
110115
*/
111-
if (cpumask_any(lowest_mask) >= nr_cpu_ids)
116+
if (cpumask_empty(lowest_mask))
117+
continue;
118+
119+
if (!fitness_fn)
120+
return 1;
121+
122+
/* Ensure the capacity of the CPUs fit the task */
123+
for_each_cpu(cpu, lowest_mask) {
124+
if (!fitness_fn(p, cpu))
125+
cpumask_clear_cpu(cpu, lowest_mask);
126+
}
127+
128+
/*
129+
* If no CPU at the current priority can fit the task
130+
* continue looking
131+
*/
132+
if (cpumask_empty(lowest_mask))
112133
continue;
113134
}
114135

‎kernel/sched/cpupri.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ struct cpupri {
1818
};
1919

2020
#ifdef CONFIG_SMP
21-
int cpupri_find(struct cpupri *cp, struct task_struct *p, struct cpumask *lowest_mask);
21+
int cpupri_find(struct cpupri *cp, struct task_struct *p,
22+
struct cpumask *lowest_mask,
23+
bool (*fitness_fn)(struct task_struct *p, int cpu));
2224
void cpupri_set(struct cpupri *cp, int cpu, int pri);
2325
int cpupri_init(struct cpupri *cp);
2426
void cpupri_cleanup(struct cpupri *cp);

‎kernel/sched/rt.c‎

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,45 @@ static inline int on_rt_rq(struct sched_rt_entity *rt_se)
437437
return rt_se->on_rq;
438438
}
439439

440+
#ifdef CONFIG_UCLAMP_TASK
441+
/*
442+
* Verify the fitness of task @p to run on @cpu taking into account the uclamp
443+
* settings.
444+
*
445+
* This check is only important for heterogeneous systems where uclamp_min value
446+
* is higher than the capacity of a @cpu. For non-heterogeneous system this
447+
* function will always return true.
448+
*
449+
* The function will return true if the capacity of the @cpu is >= the
450+
* uclamp_min and false otherwise.
451+
*
452+
* Note that uclamp_min will be clamped to uclamp_max if uclamp_min
453+
* > uclamp_max.
454+
*/
455+
static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
456+
{
457+
unsigned int min_cap;
458+
unsigned int max_cap;
459+
unsigned int cpu_cap;
460+
461+
/* Only heterogeneous systems can benefit from this check */
462+
if (!static_branch_unlikely(&sched_asym_cpucapacity))
463+
return true;
464+
465+
min_cap = uclamp_eff_value(p, UCLAMP_MIN);
466+
max_cap = uclamp_eff_value(p, UCLAMP_MAX);
467+
468+
cpu_cap = capacity_orig_of(cpu);
469+
470+
return cpu_cap >= min(min_cap, max_cap);
471+
}
472+
#else
473+
static inline bool rt_task_fits_capacity(struct task_struct *p, int cpu)
474+
{
475+
return true;
476+
}
477+
#endif
478+
440479
#ifdef CONFIG_RT_GROUP_SCHED
441480

442481
static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
@@ -1391,6 +1430,7 @@ select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
13911430
{
13921431
struct task_struct *curr;
13931432
struct rq *rq;
1433+
bool test;
13941434

13951435
/* For anything but wake ups, just return the task_cpu */
13961436
if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
@@ -1422,10 +1462,16 @@ select_task_rq_rt(struct task_struct *p, int cpu, int sd_flag, int flags)
14221462
*
14231463
* This test is optimistic, if we get it wrong the load-balancer
14241464
* will have to sort it out.
1465+
*
1466+
* We take into account the capacity of the CPU to ensure it fits the
1467+
* requirement of the task - which is only important on heterogeneous
1468+
* systems like big.LITTLE.
14251469
*/
1426-
if (curr && unlikely(rt_task(curr)) &&
1427-
(curr->nr_cpus_allowed < 2 ||
1428-
curr->prio <= p->prio)) {
1470+
test = curr &&
1471+
unlikely(rt_task(curr)) &&
1472+
(curr->nr_cpus_allowed < 2 || curr->prio <= p->prio);
1473+
1474+
if (test || !rt_task_fits_capacity(p, cpu)) {
14291475
int target = find_lowest_rq(p);
14301476

14311477
/*
@@ -1449,15 +1495,15 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
14491495
* let's hope p can move out.
14501496
*/
14511497
if (rq->curr->nr_cpus_allowed == 1 ||
1452-
!cpupri_find(&rq->rd->cpupri, rq->curr, NULL))
1498+
!cpupri_find(&rq->rd->cpupri, rq->curr, NULL, NULL))
14531499
return;
14541500

14551501
/*
14561502
* p is migratable, so let's not schedule it and
14571503
* see if it is pushed or pulled somewhere else.
14581504
*/
1459-
if (p->nr_cpus_allowed != 1
1460-
&& cpupri_find(&rq->rd->cpupri, p, NULL))
1505+
if (p->nr_cpus_allowed != 1 &&
1506+
cpupri_find(&rq->rd->cpupri, p, NULL, NULL))
14611507
return;
14621508

14631509
/*
@@ -1601,7 +1647,8 @@ static void put_prev_task_rt(struct rq *rq, struct task_struct *p)
16011647
static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu)
16021648
{
16031649
if (!task_running(rq, p) &&
1604-
cpumask_test_cpu(cpu, p->cpus_ptr))
1650+
cpumask_test_cpu(cpu, p->cpus_ptr) &&
1651+
rt_task_fits_capacity(p, cpu))
16051652
return 1;
16061653

16071654
return 0;
@@ -1643,7 +1690,8 @@ static int find_lowest_rq(struct task_struct *task)
16431690
if (task->nr_cpus_allowed == 1)
16441691
return -1; /* No other targets possible */
16451692

1646-
if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask))
1693+
if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask,
1694+
rt_task_fits_capacity))
16471695
return -1; /* No targets found */
16481696

16491697
/*
@@ -2147,12 +2195,14 @@ static void pull_rt_task(struct rq *this_rq)
21472195
*/
21482196
static void task_woken_rt(struct rq *rq, struct task_struct *p)
21492197
{
2150-
if (!task_running(rq, p) &&
2151-
!test_tsk_need_resched(rq->curr) &&
2152-
p->nr_cpus_allowed > 1 &&
2153-
(dl_task(rq->curr) || rt_task(rq->curr)) &&
2154-
(rq->curr->nr_cpus_allowed < 2 ||
2155-
rq->curr->prio <= p->prio))
2198+
bool need_to_push = !task_running(rq, p) &&
2199+
!test_tsk_need_resched(rq->curr) &&
2200+
p->nr_cpus_allowed > 1 &&
2201+
(dl_task(rq->curr) || rt_task(rq->curr)) &&
2202+
(rq->curr->nr_cpus_allowed < 2 ||
2203+
rq->curr->prio <= p->prio);
2204+
2205+
if (need_to_push || !rt_task_fits_capacity(p, cpu_of(rq)))
21562206
push_rt_tasks(rq);
21572207
}
21582208

@@ -2224,7 +2274,10 @@ static void switched_to_rt(struct rq *rq, struct task_struct *p)
22242274
*/
22252275
if (task_on_rq_queued(p) && rq->curr != p) {
22262276
#ifdef CONFIG_SMP
2227-
if (p->nr_cpus_allowed > 1 && rq->rt.overloaded)
2277+
bool need_to_push = rq->rt.overloaded ||
2278+
!rt_task_fits_capacity(p, cpu_of(rq));
2279+
2280+
if (p->nr_cpus_allowed > 1 && need_to_push)
22282281
rt_queue_push_tasks(rq);
22292282
#endif /* CONFIG_SMP */
22302283
if (p->prio < rq->curr->prio && cpu_online(cpu_of(rq)))

0 commit comments

Comments
 (0)