sched: Fix cluster scheduling in the presence of asymmetric capacity
cover sched: Fix cluster scheduling in the presence of asymmetric capacity
Cluster scheduling balances load among clusters of CPUs sharing a resource [1]. It was broken on Intel hybrid processors using asymmetric packing of tasks. Tim fixed that [2]. It is broken again when combined with asymmetric CPU capacity.
The diagram below shows a processor with big (B) and small (s) CPUs. Also, small CPUs are grouped in cluster sharing mid-level cache. This topology is common in Intel hybrid processors.
------ ------
| B | | B | ----------------- -----------------
| | | | | s | s | s | s | | s | s | s | s |
------ ------ ----------------- -----------------
| L2 | | L2 | | L2 | | L2 |
-------------------------------------------------------
| L3 |
-------------------------------------------------------On a partially busy system (one with idle CPUs; busy CPUs have one task each), scheduling for asymmetric capacity ensures that misfit tasks are placed on the big CPUs. The remaining tasks, misfit or not, run on the small CPUs. If CONFIG_SCHED_CLUSTER is enabled, these remaining tasks should be evenly spread between the two small-CPU clusters.
This does not happen today because various checks in the load balancer prevent a small CPU in one cluster from pulling tasks from another:
* A bug in update_sd_pick_busiest() causes it to not check for capacity when preferring a fully_busy big CPU (which it cannot help) vs a has_ spare small-CPU cluster (which it can).
* Accounting misfit load in a group is pointless if the destination CPU is equally a small CPU. Moreover, update_sd_pick_busiest() will not pick such group as busiest anyway.
* Once a busiest group has been identified, sched_balance_find_src_rq() will refuse to migrate tasks to CPUs of equal capacity.
* The SD_PREFER_SIBLING flag is removed from scheduling domains with asymmetric capacity.
I address these issues in this series. Details are in the changelog of each patch.
I tested these patches on an Alder Lake system with Hyper-Threading disabled. I also tested with CONFIG_SCHED_CLUSTER=n to ensure that processors without clusters continue to work.
[1]. https://lore.kernel.org/r/[email protected]/
[2]. https://lore.kernel.org/r/[email protected]/
---
Ricardo Neri (4):
sched/fair: Always skip fully_busy higher-capacity groups for load balance sched/fair: Ignore misfit load if the destination CPU cannot help sched/fair: Allow load balancing between CPUs of equal capacity sched/topology: Keep SD_PREFER_SIBLING for domains with clusters
kernel/sched/fair.c | 27 +++++++++++++++------------ kernel/sched/topology.c | 11 +++++++++-- 2 files changed, 24 insertions(+), 14 deletions(-)
---
base-commit: e51a38e71974982abb3f2f16141763a1511f7a3f
change-id: 20250620-rneri-fix-cas-clusters-bb4287d1e152
Best regards,
Signature · 1 line
Ricardo Neri <[email protected]>
PATCH RESEND 1/4 sched/fair: Always skip fully_busy higher-capacity groups for load balance
update_sd_pick_busiest() is supposed to avoid picking as busiest a candidate scheduling group with no more than one task if its per-CPU capacity is greater than that of the destination CPU.
update_sd_pick_busiest() selects as busiest a group if its type is greater than has_spare (the type of the busiest group is initialized as has_spare). As a result, a fully_busy group with higher per-CPU capacity can still be selected as busiest.
Relocate the existing comparison of capacities to occur before comparing the types of the candidate and busiest groups.
Remove unnecessary parentheses while here.
--- kernel/sched/fair.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
kernel/sched/fair.c +11 -11
@@ -10372,6 +10372,17 @@ static bool update_sd_pick_busiest(struct lb_env *env, sds->local_stat.group_type != group_has_spare))return false; +/* +* Candidate sg has no more than one task per CPU and has higher +* per-CPU capacity. Migrating tasks to less capable CPUs may harm +* throughput. Maximize throughput, power/energy consequences are not +* considered. +*/ +if (env->sd->flags &SD_ASYM_CPUCAPACITY && + sgs->group_type <= group_fully_busy && +capacity_greater (sg->sgc->min_capacity,capacity_of (env->dst_cpu))) +return false; +if (sgs->group_type > busiest->group_type)return true;
@@ -10474,17 +10485,6 @@ static bool update_sd_pick_busiest(struct lb_env *env,break ; } -/* -* Candidate sg has no more than one task per CPU and has higher -* per-CPU capacity. Migrating tasks to less capable CPUs may harm -* throughput. Maximize throughput, power/energy consequences are not -* considered. -*/ -if ((env->sd->flags &SD_ASYM_CPUCAPACITY ) && - (sgs->group_type <= group_fully_busy) && - (capacity_greater (sg->sgc->min_capacity,capacity_of (env->dst_cpu)))) -return false; -return true; }
Signature · 1 line
2.43.0
PATCH RESEND 2/4 sched/fair: Ignore misfit load if the destination CPU cannot help
In reply to Ricardo NeriThere is no point in identifying scheduling groups with misfit tasks if the destination CPU cannot help (i.e., it has less than 20% greater capacity than the most performant CPU in the group).
Since migrating misfit tasks takes precedence over relieving fully_busy groups, identifying a group with misfit tasks causes a destination CPU of smaller maximum capacity to back off (see capacity checks in update_sd_ pick_busiest()) even if it can help: it could help a group of equally small maximum capacity if classified as fully_busy or has_spare.
The described situation can happen if a scheduling domain has groups of big CPUs alongside two or more clusters of smaller CPUs that share L2 cache. Load should be balanced between these sets of smaller CPUs when CONFIG_SCHED_CLUSTER is enabled.
--- kernel/sched/fair.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/sched/fair.c +3 -1
@@ -10302,7 +10302,9 @@ static inline void update_sg_lb_stats(struct lb_env *env,if (local_group)continue ; -if (sd_flags &SD_ASYM_CPUCAPACITY ) { +/* Only look for misfit load if dst_cpu can help */ +if (sd_flags &SD_ASYM_CPUCAPACITY && +capacity_greater (capacity_of (env->dst_cpu), group->sgc->max_capacity)) {/* Check for a misfit task on the cpu */ if (sgs->group_misfit_task_load < rq->misfit_task_load) { sgs->group_misfit_task_load = rq->misfit_task_load;
Signature · 1 line
2.43.0
Re: [PATCH RESEND 2/4] sched/fair: Ignore misfit load if the destination CPU cannot help
Quoted 3 lines · Ricardo Neri
There is no point in identifying scheduling groups with misfit tasks if the destination CPU cannot help (i.e., it has less than 20% greater capacity than the most performant CPU in the group).
There's a mismatch here because capacity_greater() is 5%? You could use 20% fits fits_capacity() I'd say it's too strict here.
Quoted 23 lines
Since migrating misfit tasks takes precedence over relieving
fully_busygroups, identifying a group with misfit tasks causes a destination CPU of smaller maximum capacity to back off (see capacity checks inupdate_sd_pick_busiest()) even if it can help: it could help a group of equally small maximum capacity if classified asfully_busyorhas_spare.The described situation can happen if a scheduling domain has groups of big CPUs alongside two or more clusters of smaller CPUs that share L2 cache. Load should be balanced between these sets of smaller CPUs when
CONFIG_SCHED_CLUSTERis enabled.Signed-off-by Ricardo Neri <[email protected]>--- kernel/sched/fair.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)kernel/sched/fair.c +0
-0
kernel/sched/fair.c static inline void update_sg_lb_stats(struct lb_env *env, · line 10310
Quoted 10 lines
kernel/sched/fair.c +3
-1@@ -10302,7 +10302,9 @@ static inline void update_sg_lb_stats(struct lb_env *env,if (local_group)continue ; -if (sd_flags &SD_ASYM_CPUCAPACITY ) { +/* Only look for misfit load if dst_cpu can help */ +if (sd_flags &SD_ASYM_CPUCAPACITY && +capacity_greater (capacity_of (env->dst_cpu), group->sgc->max_capacity)) {/* Check for a misfit task on the cpu */ if (sgs->group_misfit_task_load < rq->misfit_task_load) { sgs->group_misfit_task_load = rq->misfit_task_load;
Apart from the above nit:
Re: [PATCH RESEND 2/4] sched/fair: Ignore misfit load if the destination CPU cannot help
Quoted 4 lines · Christian Loehle
Quoted 3 lines · Ricardo Neri
There is no point in identifying scheduling groups with misfit tasks if the destination CPU cannot help (i.e., it has less than 20% greater capacity than the most performant CPU in the group).
Thanks for looking at the patches Christian!
Quoted 3 lines
There's a mismatch here because
capacity_greater()is 5%? You could use 20% fitsfits_capacity()I'd say it's too strict here.
My intent was to describe the difference as 5%. I made a mistake here. I think checking for a 5% mismatch is more appropriate since should skip misfit load if dst_cpu and the max per-CPU capacities are similar.
Quoted 37 lines
Quoted 24 lines
Since migrating misfit tasks takes precedence over relieving
fully_busygroups, identifying a group with misfit tasks causes a destination CPU of smaller maximum capacity to back off (see capacity checks inupdate_sd_pick_busiest()) even if it can help: it could help a group of equally small maximum capacity if classified asfully_busyorhas_spare.The described situation can happen if a scheduling domain has groups of big CPUs alongside two or more clusters of smaller CPUs that share L2 cache. Load should be balanced between these sets of smaller CPUs when
CONFIG_SCHED_CLUSTERis enabled.Signed-off-by Ricardo Neri <[email protected]>--- kernel/sched/fair.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)kernel/sched/fair.c +0
-0kernel/sched/fair.c static inline void update_sg_lb_stats(struct lb_env *env, · line 10310
Quoted 10 lines
kernel/sched/fair.c +3
-1@@ -10302,7 +10302,9 @@ static inline void update_sg_lb_stats(struct lb_env *env,if (local_group)continue ; -if (sd_flags &SD_ASYM_CPUCAPACITY ) { +/* Only look for misfit load if dst_cpu can help */ +if (sd_flags &SD_ASYM_CPUCAPACITY && +capacity_greater (capacity_of (env->dst_cpu), group->sgc->max_capacity)) {/* Check for a misfit task on the cpu */ if (sgs->group_misfit_task_load < rq->misfit_task_load) { sgs->group_misfit_task_load = rq->misfit_task_load;Apart from the above nit:
Quoted 1 line
Reviewed-by Christian Loehle <[email protected]>
Thank you!
PATCH RESEND 3/4 sched/fair: Allow load balancing between CPUs of equal capacity
In reply to Ricardo Nerisched_balance_find_src_rq() is supposed to avoid picking as busiest a runqueue with a single running task since that would result in the task migrating to a lower-capacity CPU. It also prevents migrations between CPUs of equal capacity.
Migrating tasks between CPUs of equal capacity helps when balancing load in a scheduling domain in which there are CPUs of different capacity and are grouped in clusters of CPUs of equal capacity that share L2 cache. Load should be balanced among these clusters when CONFIG_SCHED_CLUSTER is enabled.
--- kernel/sched/fair.c | 1 + 1 file changed, 1 insertion(+)
kernel/sched/fair.c +1 -0
@@ -11388,6 +11388,7 @@ static struct rq *sched_balance_find_src_rq(struct lb_env *env,* average load. */ if (env->sd->flags &SD_ASYM_CPUCAPACITY && +capacity_of (env->dst_cpu) != capacity && !capacity_greater (capacity_of (env->dst_cpu), capacity) && nr_running ==1 )continue ;
Signature · 1 line
2.43.0
Re: [PATCH RESEND 3/4] sched/fair: Allow load balancing between CPUs of equal capacity
Quoted 22 lines · Ricardo Neri
sched_balance_find_src_rq()is supposed to avoid picking as busiest a runqueue with a single running task since that would result in the task migrating to a lower-capacity CPU. It also prevents migrations between CPUs of equal capacity.Migrating tasks between CPUs of equal capacity helps when balancing load in a scheduling domain in which there are CPUs of different capacity and are grouped in clusters of CPUs of equal capacity that share L2 cache. Load should be balanced among these clusters when
CONFIG_SCHED_CLUSTERis enabled.Signed-off-by Ricardo Neri <[email protected]>--- kernel/sched/fair.c | 1 + 1 file changed, 1 insertion(+)kernel/sched/fair.c +0
-0
kernel/sched/fair.c static struct rq *sched_balance_find_src_rq(struct lb_env *env, · line 11394
Quoted 7 lines
kernel/sched/fair.c +1
-0@@ -11388,6 +11388,7 @@ static struct rq *sched_balance_find_src_rq(struct lb_env *env,* average load. */ if (env->sd->flags &SD_ASYM_CPUCAPACITY && +capacity_of (env->dst_cpu) != capacity && !capacity_greater (capacity_of (env->dst_cpu), capacity) && nr_running ==1 )continue ;
For consistency, doesn't it make sense to just invert capacity_greater() here? So:
capacity_greater(capacity, capacity_of(env->dst_cpu))
if (env->sd->flags & SD_ASYM_CPUCAPACITY &&
capacity_greater(capacity, capacity_of(env->dst_cpu)) && nr_running == 1)
continue;
Re: [PATCH RESEND 3/4] sched/fair: Allow load balancing between CPUs of equal capacity
Quoted 34 lines · Christian Loehle
Quoted 22 lines · Ricardo Neri
sched_balance_find_src_rq()is supposed to avoid picking as busiest a runqueue with a single running task since that would result in the task migrating to a lower-capacity CPU. It also prevents migrations between CPUs of equal capacity.Migrating tasks between CPUs of equal capacity helps when balancing load in a scheduling domain in which there are CPUs of different capacity and are grouped in clusters of CPUs of equal capacity that share L2 cache. Load should be balanced among these clusters when
CONFIG_SCHED_CLUSTERis enabled.Signed-off-by Ricardo Neri <[email protected]>--- kernel/sched/fair.c | 1 + 1 file changed, 1 insertion(+)kernel/sched/fair.c +0
-0kernel/sched/fair.c static struct rq *sched_balance_find_src_rq(struct lb_env *env, · line 11394
Quoted 7 lines
kernel/sched/fair.c +1
-0@@ -11388,6 +11388,7 @@ static struct rq *sched_balance_find_src_rq(struct lb_env *env,* average load. */ if (env->sd->flags &SD_ASYM_CPUCAPACITY && +capacity_of (env->dst_cpu) != capacity && !capacity_greater (capacity_of (env->dst_cpu), capacity) && nr_running ==1 )continue ;For consistency, doesn't it make sense to just invert
capacity_greater()here? So:
Quoted 5 lines
capacity_greater(capacity, capacity_of(env->dst_cpu))if (
env->sd->flags&SD_ASYM_CPUCAPACITY&&
capacity_greater(capacity, capacity_of(env->dst_cpu))&&nr_running== 1)
continue;
Sure! It also makes the code more readable.
PATCH RESEND 4/4 sched/topology: Keep SD_PREFER_SIBLING for domains with clusters
In reply to Ricardo NeriThere are topologies with scheduling domains that contain CPUs of asymmetric capacity and grouped into two or more clusters of CPUs of equal capacity sharing L2 cache. CONFIG_SCHED_CLUSTER requires to balance load among clusters sharing a resource.
Keep the SD_PREFER_SIBLING in the child domains to indicate to the load balancer that it should spread load among cluster siblings.
Checks for capacity in the load balancer will prevent migrations from high- to low-capacity CPUs. Likewise, misfit load will still be used to move high-load tasks to bigger CPUs.
Remove unnecessary parentheses while here.
--- kernel/sched/topology.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
kernel/sched/topology.c +9 -2
@@ -1689,8 +1689,15 @@ sd_init(struct sched_domain_topology_level *tl,/* * Convert topological properties into behaviour. */ -/* Don't attempt to spread across CPUs of different capacities. */ -if ((sd->flags &SD_ASYM_CPUCAPACITY ) && sd->child) +/* +* Don't attempt to spread across CPUs of different capacities. An +* exception to this rule are domains in which there are clusters of +* CPUs sharing a resource. Keep the flag in such case to balance load +* among them. The load balancer will prevent task migrations from +* high- to low-capacity CPUs. +*/ +if (sd->flags &SD_ASYM_CPUCAPACITY && sd->child && + !(sd->child->flags &SD_CLUSTER )) sd->child->flags &= ~SD_PREFER_SIBLING ;if (sd->flags &SD_SHARE_CPUCAPACITY ) {
Signature · 1 line
2.43.0