sched/cache: Avoid publishing partial LLC utilization
PATCH v2 1/1 sched/cache: Avoid publishing partial LLC utilization
update_sg_lb_stats() sums utilization over the CPUs in both sched_group_span(group) and env->cpus, but takes capacity from group->sgc->capacity for the whole group. record_sg_llc_stats() publishes both.
Rejecting an all-affinity-pinned source runqueue removes that CPU from env->cpus and retries group selection. The retry can publish the partial utilization with the full capacity.
can_migrate_llc() then treats a busy preferred LLC as having spare capacity. More tasks can aggregate into it, migrations out can be blocked, and two busy LLCs may not fall back to generic balancing. This increases CPU contention and task latency. Found by inspection.
Skip publication on affinity retries using LBF_ALL_PINNED, retaining the first-pass snapshots and avoiding repeated cpumask checks. On the first pass, publish either shared LLC statistic only when env->cpus covers the whole group. Load balancing still uses the affinity-filtered statistics.
The same window exists during CPU hotplug. env->cpus drops a CPU at set_cpu_active(false), but the group span and sgc->capacity still include it until cpuset_cpu_inactive() rebuilds the domains. Keeping the previous snapshot avoids the mismatched publish. After the rebuild, the next periodic balance records both statistics again.
---
v1: https://lore.kernel.org/r/[email protected]/
Changes since v1:
- Skip publication on affinity retries using LBF_ALL_PINNED, retaining first-pass snapshots and avoiding repeated cpumask checks, as suggested by Prateek and Tim. Keep cpumask_subset() for partial group coverage during CPU hotplug on the first pass.
- Update the comment and commit message to explain the retry behavior.
- Add Fixes and Suggested-by tags.
kernel/sched/fair.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/sched/fair.c +8 -0
@@ -12111,6 +12111,14 @@ static void record_sg_llc_stats(struct lb_env *env,if (env->sd->child !=rcu_dereference_all (per_cpu (sd_llc, env->dst_cpu)))return ; +/* +* A redo repeats stats published on the first pass. Hotplug can +* still leave env->cpus partial, so util would not match capacity. +*/ +if ((env->flags &LBF_ALL_PINNED ) || + !cpumask_subset (sched_group_span (group), env->cpus)) +return ; +/* * At this point we know this group spans a LLC domain. * Record the statistic of this group in its corresponding
--
2.54.0 (Apple Git-157)
Quoted 20 lines · Jemmy Wong
update_sg_lb_stats()sums utilization over the CPUs in bothsched_group_span(group)andenv->cpus, but takes capacity fromgroup->sgc->capacityfor the whole group.record_sg_llc_stats()publishes both.Rejecting an all-affinity-pinned source runqueue removes that CPU from
env->cpusand retries group selection. The retry can publish the partial utilization with the full capacity.
can_migrate_llc()then treats a busy preferred LLC as having spare capacity. More tasks can aggregate into it, migrations out can be blocked, and two busy LLCs may not fall back to generic balancing. This increases CPU contention and task latency. Found by inspection.Skip publication on affinity retries using
LBF_ALL_PINNED, retaining the first-pass snapshots and avoiding repeated cpumask checks. On the first pass, publish either shared LLC statistic only whenenv->cpuscovers the whole group. Load balancing still uses the affinity-filtered statistics.
Quoted 5 lines
The same window exists during CPU hotplug.
env->cpusdrops a CPU atset_cpu_active(false), but the group span andsgc->capacitystill include it untilcpuset_cpu_inactive()rebuilds the domains. Keeping the previous snapshot avoids the mismatched publish. After the rebuild, the next periodic balance records both statistics again.
Thanks.
Tim
Quoted 42 lines
Suggested-by K Prateek Nayak <[email protected]>Suggested-by Tim Chen <[email protected]>Fixes f025ef275388 ("sched/cache: Record per LLC utilization to guide cache aware scheduling decisions")Signed-off-by Jemmy Wong <[email protected]>---
v1: https://lore.kernel.org/r/[email protected]/
Changes since v1:
- Skip publication on affinity retries usingLBF_ALL_PINNED, retaining first-pass snapshots and avoiding repeated cpumask checks, as suggested by Prateek and Tim. Keepcpumask_subset()for partial group coverage during CPU hotplug on the first pass.
- Update the comment and commit message to explain the retry behavior.
- Add Fixes and Suggested-by tags.kernel/sched/fair.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/sched/fair.c +8
-1@@ -12111,6 +12111,14 @@ static void record_sg_llc_stats(struct lb_env *env,if (env->sd->child !=rcu_dereference_all (per_cpu (sd_llc, env->dst_cpu)))return ; +/* +* A redo repeats stats published on the first pass. Hotplug can +* still leave env->cpus partial, so util would not match capacity. +*/ +if ((env->flags &LBF_ALL_PINNED ) || + !cpumask_subset (sched_group_span (group), env->cpus)) +return ; +/* * At this point we know this group spans a LLC domain. * Record the statistic of this group in its corresponding -- 2.54.0 (Apple Git-157)
Quoted 26 lines · Jemmy Wong
update_sg_lb_stats()sums utilization over the CPUs in bothsched_group_span(group)andenv->cpus, but takes capacity fromgroup->sgc->capacityfor the whole group.record_sg_llc_stats()publishes both.Rejecting an all-affinity-pinned source runqueue removes that CPU from
env->cpusand retries group selection. The retry can publish the partial utilization with the full capacity.
can_migrate_llc()then treats a busy preferred LLC as having spare capacity. More tasks can aggregate into it, migrations out can be blocked, and two busy LLCs may not fall back to generic balancing. This increases CPU contention and task latency. Found by inspection.Skip publication on affinity retries using
LBF_ALL_PINNED, retaining the first-pass snapshots and avoiding repeated cpumask checks. On the first pass, publish either shared LLC statistic only whenenv->cpuscovers the whole group. Load balancing still uses the affinity-filtered statistics.The same window exists during CPU hotplug.
env->cpusdrops a CPU atset_cpu_active(false), but the group span andsgc->capacitystill include it untilcpuset_cpu_inactive()rebuilds the domains. Keeping the previous snapshot avoids the mismatched publish. After the rebuild, the next periodic balance records both statistics again.
Quoted 4 lines
Suggested-by K Prateek Nayak <[email protected]>Suggested-by Tim Chen <[email protected]>Fixes f025ef275388 ("sched/cache: Record per LLC utilization to guide cache aware scheduling decisions")Signed-off-by Jemmy Wong <[email protected]>
BTW, I think this is also a candidate for 7.2.x stable.
Tim
Quoted 38 lines
---
v1: https://lore.kernel.org/r/[email protected]/
Changes since v1:
- Skip publication on affinity retries usingLBF_ALL_PINNED, retaining first-pass snapshots and avoiding repeated cpumask checks, as suggested by Prateek and Tim. Keepcpumask_subset()for partial group coverage during CPU hotplug on the first pass.
- Update the comment and commit message to explain the retry behavior.
- Add Fixes and Suggested-by tags.kernel/sched/fair.c | 8 ++++++++ 1 file changed, 8 insertions(+)kernel/sched/fair.c +8
-1@@ -12111,6 +12111,14 @@ static void record_sg_llc_stats(struct lb_env *env,if (env->sd->child !=rcu_dereference_all (per_cpu (sd_llc, env->dst_cpu)))return ; +/* +* A redo repeats stats published on the first pass. Hotplug can +* still leave env->cpus partial, so util would not match capacity. +*/ +if ((env->flags &LBF_ALL_PINNED ) || + !cpumask_subset (sched_group_span (group), env->cpus)) +return ; +/* * At this point we know this group spans a LLC domain. * Record the statistic of this group in its corresponding -- 2.54.0 (Apple Git-157)
Quoted 9 lines · Tim Chen
[…]
BTW, I think this is also a candidate for 7.2.x stable.
Tim
Hi Tim,
Thanks for the review and the stable suggestion. Should I send a v3 with Cc: [email protected] # 7.2.x, along with the comment clarification suggested by Chen Yu and the Reviewed-by tags?
Thanks,
Jemmy
Quoted 7 lines · Jemmy Wong
Quoted 7 lines · Tim Chen
[…]
BTW, I think this is also a candidate for 7.2.x stable.
Quoted 7 lines
Quoted 1 line
Tim
Hi Tim,
Thanks for the review and the stable suggestion. Should I send a v3 with
Cc: [email protected] # 7.2.x, along with the comment clarification suggested by Chen Yu and the Reviewed-by tags?
Yes, please do that.
Thanks.
Tim
Quoted 3 lines
Thanks,
Jemmy
Quoted 13 lines · Jemmy Wong
Date: Tue, 13 Oct 2026 16:43:38 +0800
From: Jemmy Wong <[email protected]>
To: [email protected], [email protected], [email protected],
[email protected], [email protected]
Cc: [email protected], [email protected],
[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Jemmy Wong <[email protected]>
Subject: [PATCH v2] sched/cache: Avoid publishing partial LLC utilization
X-Mailer: git-send-email 2.54.0Skip publication on affinity retries using
LBF_ALL_PINNED, retaining the first-pass snapshots and avoiding repeated cpumask checks. On the
[ ... ]
kernel/sched/fair.c static void record_sg_llc_stats(struct lb_env *env, · line 12115
Quoted 10 lines
kernel/sched/fair.c +2
-0@@ -12111,6 +12111,14 @@ static void record_sg_llc_stats(struct lb_env *env,if (env->sd->child !=rcu_dereference_all (per_cpu (sd_llc, env->dst_cpu)))return ; +/* +* A redo repeats stats published on the first pass. Hotplug can
It took me a while to understand the check of LBF_ALL_PINNED, to make it clear how about adding comment below for future reference:
Once we enter record_sg_llc_stats(), LBF_ALL_PINNED being set is a necessary and sufficient condition for being in a redo. Since env->cpus may be incomplete when LBF_ALL_PINNED is set, and to avoid recomputing the statistics of the same group during a redo, we skip this update. The impact of skipping is small, because the interval between the first pass and the subsequent redo is short.
Otherwise it looks good to me and
thanks,
Chenyu
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: fcceb7da94659695d6cba97190e18e87d9cce82c Gitweb: https://git.kernel.org/tip/fcceb7da94659695d6cba97190e18e87d9cce82c Author: Jemmy Wong <[email protected]> AuthorDate: Tue, 13 Oct 2026 16:43:38 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Fri, 16 Oct 2026 09:49:04 +02:00
sched/cache: Avoid publishing partial LLC utilization
update_sg_lb_stats() sums utilization over the CPUs in both sched_group_span(group) and env->cpus, but takes capacity from group->sgc->capacity for the whole group. record_sg_llc_stats() publishes both.
Rejecting an all-affinity-pinned source runqueue removes that CPU from env->cpus and retries group selection. The retry can publish the partial utilization with the full capacity.
can_migrate_llc() then treats a busy preferred LLC as having spare capacity. More tasks can aggregate into it, migrations out can be blocked, and two busy LLCs may not fall back to generic balancing. This increases CPU contention and task latency. Found by inspection.
Skip publication on affinity retries using LBF_ALL_PINNED, retaining the first-pass snapshots and avoiding repeated cpumask checks. On the first pass, publish either shared LLC statistic only when env->cpus covers the whole group. Load balancing still uses the affinity-filtered statistics.
The same window exists during CPU hotplug. env->cpus drops a CPU at set_cpu_active(false), but the group span and sgc->capacity still include it until cpuset_cpu_inactive() rebuilds the domains. Keeping the previous snapshot avoids the mismatched publish. After the rebuild, the next periodic balance records both statistics again.
--- kernel/sched/fair.c | 8 ++++++++ 1 file changed, 8 insertions(+)
kernel/sched/fair.c +8 -0
@@ -12084,6 +12084,14 @@ static void record_sg_llc_stats(struct lb_env *env,return ;/* +* A redo repeats stats published on the first pass. Hotplug can +* still leave env->cpus partial, so util would not match capacity. +*/ +if ((env->flags &LBF_ALL_PINNED ) || + !cpumask_subset (sched_group_span (group), env->cpus)) +return ; + +/* * At this point we know this group spans a LLC domain. * Record the statistic of this group in its corresponding * shared LLC domain.