Skip to contentSkip to conversation

sched/cache: Avoid publishing partial LLC utilization

PATCH Superseded Jemmy Wong 3 participants 4 messages Oct 10 – Oct 12 Scheduler

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jemmy Wong

author
#0 lore raw

PATCH 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. On a two-CPU LLC this replaces 1800/2048 with 900/2048.

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.

Publish either shared LLC statistic only when env->cpus covers the whole group. A partial scan keeps the previous complete snapshot; other groups still refresh. 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 | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/sched/fair.c +7 -0
index aa4a7d22d1df..a98a62f729d4 100644
@@ -12121,6 +12121,13 @@ 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;
 
+	/*
+	 * Affinity retries can remove CPUs from env->cpus. Do not publish
+	 * partial utilization against the capacity of the whole group.
+	 */
+	if (!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.25.1

#1 lore raw

Hello Jemmy,

kernel/sched/fair.c static void record_sg_llc_stats(struct lb_env *env, · line 12130

Quoted 13 lines · Jemmy Wong
kernel/sched/fair.c +7 -0
@@ -12121,6 +12121,13 @@ 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;
 
+	/*
+	 * Affinity retries can remove CPUs from env->cpus. Do not publish
+	 * partial utilization against the capacity of the whole group.
+	 */
+	if (!cpumask_subset(sched_group_span(group), env->cpus))
+		return;
+

nit.

When sched_balance_rq() does a "redo" and arrives here, env->flags will always have LBF_ALL_PINNED set.

That would be a much cheaper check compared to cpumask_subset() to ensure env->cpus covers all CPUs.

Thoughts?

Quoted 5 lines
kernel/sched/fair.c +0 -1
 	/*
 	 * At this point we know this group spans a LLC domain.
 	 * Record the statistic of this group in its corresponding
--

2.25.1

Signature · 2 lines
Thanks and Regards,
Prateek
author
#2 lore raw

Hi Prateek,

Quoted 27 lines · K Prateek Nayak

Hello Jemmy,

kernel/sched/fair.c static void record_sg_llc_stats(struct lb_env *env, · line 12130

Quoted 13 lines · Jemmy Wong
kernel/sched/fair.c +7 -0
@@ -12121,6 +12121,13 @@ 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;
 
+ /*
+  * Affinity retries can remove CPUs from env->cpus. Do not publish
+  * partial utilization against the capacity of the whole group.
+  */
+ if (!cpumask_subset(sched_group_span(group), env->cpus))
+ return;
+

nit.

When sched_balance_rq() does a "redo" and arrives here, env->flags will always have LBF_ALL_PINNED set.

That would be a much cheaper check compared to cpumask_subset() to ensure env->cpus covers all CPUs.

Quoted 11 lines

Thoughts?

Quoted 5 lines
kernel/sched/fair.c +0 -1
 /*
 * At this point we know this group spans a LLC domain.
 * Record the statistic of this group in its corresponding
--

2.25.1

Signature · 2 lines
Thanks and Regards,
Prateek

The redo part is right, but LBF_ALL_PINNED reflects the previous busiest rq, not whether the group being recorded is fully covered.

For example, if cpu1 in LLC0 {cpu0, cpu1} is rejected as all-pinned, we remove it from env->cpus and redo. The next scan still carries LBF_ALL_PINNED while recording LLC1 {cpu2, cpu3}, which remains fully covered. The flag check would skip that update; cpumask_subset() would not.

It also misses the hotplug window: when cpu1 in LLC0 {cpu0, cpu1} becomes inactive, a new balance pass can exclude cpu1 from env->cpus before the domains are rebuilt. The group span and capacity still cover both CPUs, but utilization is summed only over cpu0.

LBF_ALL_PINNED is clear on this first pass, so the flag check would allow that partial publication; cpumask_subset() would prevent it.

So I would keep cpumask_subset().

Thanks,
Jemmy

#3 lore raw
Quoted 55 lines · Jemmy Wong

Hi Prateek,

Quoted 27 lines · K Prateek Nayak

Hello Jemmy,

kernel/sched/fair.c static void record_sg_llc_stats(struct lb_env *env, · line 12130

Quoted 13 lines · Jemmy Wong
kernel/sched/fair.c +7 -0
@@ -12121,6 +12121,13 @@ 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;
 
+ /*
+  * Affinity retries can remove CPUs from env->cpus. Do not publish
+  * partial utilization against the capacity of the whole group.
+  */
+ if (!cpumask_subset(sched_group_span(group), env->cpus))
+ return;
+

nit.

When sched_balance_rq() does a "redo" and arrives here, env->flags will always have LBF_ALL_PINNED set.

That would be a much cheaper check compared to cpumask_subset() to ensure env->cpus covers all CPUs.

Quoted 11 lines

Thoughts?

Quoted 5 lines
kernel/sched/fair.c +0 -1
 /*
 * At this point we know this group spans a LLC domain.
 * Record the statistic of this group in its corresponding
--

2.25.1

Signature · 2 lines
Thanks and Regards,
Prateek

The redo part is right, but LBF_ALL_PINNED reflects the previous busiest rq, not whether the group being recorded is fully covered.

For example, if cpu1 in LLC0 {cpu0, cpu1} is rejected as all-pinned, we remove it from env->cpus and redo. The next scan still carries LBF_ALL_PINNED while recording LLC1 {cpu2, cpu3}, which remains fully covered. The flag check would skip that update; cpumask_subset() would not.

It also misses the hotplug window: when cpu1 in LLC0 {cpu0, cpu1} becomes inactive, a new balance pass can exclude cpu1 from env->cpus before the domains are rebuilt. The group span and capacity still cover both CPUs, but utilization is summed only over cpu0.

Quoted 4 lines

LBF_ALL_PINNED is clear on this first pass, so the flag check would allow that partial publication; cpumask_subset() would prevent it.

So I would keep cpumask_subset().

+ Chen Yu who wrote this function.

The partial group issue you pointed out is valid.

I think Pratek has a good point.
Using LBF_ALL_PINNED is a cheap check and can eliminate
a lot of un-needed cpumask checks. We can do that first and only resort to cpumask_subset() when necessary. Something like:

          /*
           * 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;                                                    

You should also add a Fixes tag.

Tim

Quoted 3 lines

Thanks,
Jemmy