Skip to contentSkip to conversation

sched/fair: Only update stats for allowed CPUs when looking for dst group

PATCH RESEND Applied Adam Li 5 participants 8 messages Oct 11, 2025 – Oct 16, 2025 Scheduler

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Adam Li

author
#0 lore raw

PATCH RESEND 1/1 sched/fair: Only update stats for allowed CPUs when looking for dst group

Load imbalance is observed when the workload frequently forks new threads. Due to CPU affinity, the workload can run on CPU 0-7 in the first group, and only on CPU 8-11 in the second group. CPU 12-15 are always idle.

{ 0 1 2 3 4 5 6 7 } {8 9 10 11 12 13 14 15}
* * * * * * * * * * * *

When looking for dst group for newly forked threads, in many times update_sg_wakeup_stats() reports the second group has more idle CPUs than the first group. The scheduler thinks the second group is less busy. Then it selects least busy CPUs among CPU 8-11. Therefore CPU 8-11 can be crowded with newly forked threads, at the same time CPU 0-7 can be idle.

A task may not use all the CPUs in a schedule group due to CPU affinity. Only update schedule group statistics for allowed CPUs.

---
Resending this patch from the patchset:
https://lore.kernel.org/lkml/[email protected]/

Only changed commit message. The single patch may be easier for reviewing.

---
 kernel/sched/fair.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
index bc0b7ce8a65d..d5ec15050ebc 100644
@@ -10671,7 +10671,7 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
 	if (sd->flags & SD_ASYM_CPUCAPACITY)
 		sgs->group_misfit_task_load = 1;
 
-	for_each_cpu(i, sched_group_span(group)) {
+	for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {
 		struct rq *rq = cpu_rq(i);
 		unsigned int local;
 
Signature · 1 line
2.34.1
#1 lore raw

kernel/sched/fair.c static inline void update_sg_wakeup_stats(struct sched_domain *sd, · line 10674

Quoted 10 lines · Adam Li
kernel/sched/fair.c +1 -1
index bc0b7ce8a65d..d5ec15050ebc 100644
@@ -10671,7 +10671,7 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
  	if (sd->flags & SD_ASYM_CPUCAPACITY)
  		sgs->group_misfit_task_load = 1;
 
-	for_each_cpu(i, sched_group_span(group)) {
+	for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {

Looks good to me. One minor question, would pre-calculating the mask be better? Copied from select_idle_cpu():

cpus = this_cpu_cpumask_var_ptr(select_rq_mask); cpumask_and(cpus, sched_group_span(sd), p->cpus_ptr); for_each_cpu(i, cpus) {

thanks,
Chenyu

author
#2 lore raw

Hi Chenyu,

Thanks for your comments.

Quoted 12 lines · Yu C

kernel/sched/fair.c static inline void update_sg_wakeup_stats(struct sched_domain *sd, · line 10674

Quoted 9 lines · Adam Li
kernel/sched/fair.c +1 -0
index bc0b7ce8a65d..d5ec15050ebc 100644
@@ -10671,7 +10671,7 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
       if (sd->flags & SD_ASYM_CPUCAPACITY)
           sgs->group_misfit_task_load = 1;
   -    for_each_cpu(i, sched_group_span(group)) {
+    for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {

Looks good to me. One minor question, would pre-calculating the mask be better?

I do agree pre-calculating the cpumask can save cpu cycles, without doing mask AND at each loop.

Quoted 6 lines

Copied from select_idle_cpu():

cpus = this_cpu_cpumask_var_ptr(select_rq_mask); cpumask_and(cpus, sched_group_span(sd), p->cpus_ptr); for_each_cpu(i, cpus) {

But I am not sure if it is safe to use the percpu 'select_rq_mask' in update_sg_wakeup_stats(). Or we have to allocate a 'struct cpumask'.

I tested bellow patch. It can work and fix the bug. If it is safe to use 'select_rq_mask' , I can submit V2 patch.

kernel/sched/fair.c +3 -1
@@ -10664,6 +10664,7 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
                                          struct task_struct *p)
 {
        int i, nr_running;
+       struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
 
        memset(sgs, 0, sizeof(*sgs));
 
@@ -10671,7 +10672,8 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
        if (sd->flags & SD_ASYM_CPUCAPACITY)
                sgs->group_misfit_task_load = 1;
 
-       for_each_cpu(i, sched_group_span(group)) {
+       cpumask_and(cpus, sched_group_span(group), p->cpus_ptr);
+       for_each_cpu(i, cpus) {
                struct rq *rq = cpu_rq(i);
                unsigned int local;
 

Thanks,
-adam

Reviewed-by
#3 lore raw
Quoted 20 lines · Adam Li

Hi Chenyu,

Thanks for your comments.

Quoted 12 lines · Yu C

kernel/sched/fair.c static inline void update_sg_wakeup_stats(struct sched_domain *sd, · line 10674

Quoted 9 lines · Adam Li
kernel/sched/fair.c +1 -0
index bc0b7ce8a65d..d5ec15050ebc 100644
@@ -10671,7 +10671,7 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
       if (sd->flags & SD_ASYM_CPUCAPACITY)
           sgs->group_misfit_task_load = 1;
   -    for_each_cpu(i, sched_group_span(group)) {
+    for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {

Looks good to me. One minor question, would pre-calculating the mask be better?

I do agree pre-calculating the cpumask can save cpu cycles, without doing mask AND at each loop.

Quoted 9 lines
Quoted 6 lines

Copied from select_idle_cpu():

cpus = this_cpu_cpumask_var_ptr(select_rq_mask); cpumask_and(cpus, sched_group_span(sd), p->cpus_ptr); for_each_cpu(i, cpus) {

But I am not sure if it is safe to use the percpu 'select_rq_mask' in update_sg_wakeup_stats(). Or we have to allocate a 'struct cpumask'.

Allocating dynamically would be costly. Using percpu select_rq_mask is safe in this scenario: the waker's CPU has already disabled local irq via raw_spinlock_irqsave(&p->pi_lock), so I suppose no one can modify it simultaneously. Moreover, if the fast wakeup path select_idle_sibling()
can use it, the slow path sched_balance_find_dst_cpu() should also be able
to do so IMO.

Quoted 14 lines

I tested bellow patch. It can work and fix the bug. If it is safe to use 'select_rq_mask' , I can submit V2 patch.

kernel/sched/fair.c +1 -0
@@ -10664,6 +10664,7 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
                                           struct task_struct *p)
  {
         int i, nr_running;
+       struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
 
         memset(sgs, 0, sizeof(*sgs));
 

kernel/sched/fair.c static inline void update_sg_wakeup_stats(struct sched_domain *sd, · line 10674

Quoted 4 lines
kernel/sched/fair.c +0 -1
@@ -10671,7 +10672,8 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
         if (sd->flags & SD_ASYM_CPUCAPACITY)
                 sgs->group_misfit_task_load = 1;
 
-       for_each_cpu(i, sched_group_span(group)) {

nice-to-have:
maybe add a comment here that cpus is not empty, because
we have cpumask_intersects() check in sched_balance_find_dst_group(), (just in case sgs->group_type incorrectly remain 0 which is group_has_spare, if the cpus is empty)

kernel/sched/fair.c static inline void update_sg_wakeup_stats(struct sched_domain *sd, · line 10679

Quoted 6 lines
kernel/sched/fair.c +2 -0
+       cpumask_and(cpus, sched_group_span(group), p->cpus_ptr);
+       for_each_cpu(i, cpus) {
                 struct rq *rq = cpu_rq(i);
                 unsigned int local;
 

and from my understanding, for this percpu version,

thanks,
Chenyu

author
#4 lore raw
Quoted 1 line · Yu C

On 10/14/2025 6:51 PM, Adam Li wrote:

[...]

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

But I am not sure if it is safe to use the percpu 'select_rq_mask' in update_sg_wakeup_stats(). Or we have to allocate a 'struct cpumask'.

Quoted 7 lines

Allocating dynamically would be costly. Using percpu select_rq_mask is safe in this scenario: the waker's CPU has already disabled local irq via raw_spinlock_irqsave(&p->pi_lock), so I suppose no one can modify it simultaneously. Moreover, if the fast wakeup path select_idle_sibling() can use it, the slow path sched_balance_find_dst_cpu() should also be able to do so IMO.

Yes. Agree.>> I tested bellow patch. It can work and fix the bug.

Quoted 16 lines
Quoted 16 lines

If it is safe to use 'select_rq_mask' , I can submit V2 patch.

kernel/sched/fair.c +1 -0
@@ -10664,6 +10664,7 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
                                            struct task_struct *p)
   {
          int i, nr_running;
+       struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
 
          memset(sgs, 0, sizeof(*sgs));
 
@@ -10671,7 +10672,8 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
          if (sd->flags & SD_ASYM_CPUCAPACITY)
                  sgs->group_misfit_task_load = 1;
 
Quoted 8 lines

kernel/sched/fair.c static inline void update_sg_wakeup_stats(struct sched_domain *sd, · line 10674

Quoted 1 line
kernel/sched/fair.c +0 -1
-       for_each_cpu(i, sched_group_span(group)) {

nice-to-have:
maybe add a comment here that cpus is not empty, because
we have cpumask_intersects() check in sched_balance_find_dst_group(), (just in case sgs->group_type incorrectly remain 0 which is group_has_spare, if the cpus is empty)

OK.

As Peter suggested [1] for this patch I will keep 'for_each_cpu_and()'.

I will try this cpumask pre-calculation optimization as next step, for both update_sg_lb_stats() and update_sg_wakeup_stats().

Quoted 11 lines

kernel/sched/fair.c static inline void update_sg_wakeup_stats(struct sched_domain *sd, · line 10679

Quoted 6 lines
kernel/sched/fair.c +2 -0
+       cpumask_and(cpus, sched_group_span(group), p->cpus_ptr);
+       for_each_cpu(i, cpus) {
                  struct rq *rq = cpu_rq(i);
                  unsigned int local;
 

and from my understanding, for this percpu version,

Thanks for your review.

[1]: https://lore.kernel.org/all/[email protected]/

-adam

maintainer
#5 lore raw
In reply to Adam Li
Quoted 14 lines · Adam Li

Load imbalance is observed when the workload frequently forks new threads. Due to CPU affinity, the workload can run on CPU 0-7 in the first group, and only on CPU 8-11 in the second group. CPU 12-15 are always idle.

{ 0 1 2 3 4 5 6 7 } {8 9 10 11 12 13 14 15}
* * * * * * * * * * * *

When looking for dst group for newly forked threads, in many times update_sg_wakeup_stats() reports the second group has more idle CPUs than the first group. The scheduler thinks the second group is less busy. Then it selects least busy CPUs among CPU 8-11. Therefore CPU 8-11 can be crowded with newly forked threads, at the same time CPU 0-7 can be idle.

Quoted 8 lines

A task may not use all the CPUs in a schedule group due to CPU affinity. Only update schedule group statistics for allowed CPUs.

---
Resending this patch from the patchset:
https://lore.kernel.org/lkml/[email protected]/

Right, lets start with this then ;-)

No need to do the cpumask_and() thing, that's just more changes vs update_sg_lb_stats().

#6 lore raw
Quoted 24 lines · Peter Zijlstra
Quoted 14 lines · Adam Li

Load imbalance is observed when the workload frequently forks new threads. Due to CPU affinity, the workload can run on CPU 0-7 in the first group, and only on CPU 8-11 in the second group. CPU 12-15 are always idle.

{ 0 1 2 3 4 5 6 7 } {8 9 10 11 12 13 14 15}
* * * * * * * * * * * *

When looking for dst group for newly forked threads, in many times update_sg_wakeup_stats() reports the second group has more idle CPUs than the first group. The scheduler thinks the second group is less busy. Then it selects least busy CPUs among CPU 8-11. Therefore CPU 8-11 can be crowded with newly forked threads, at the same time CPU 0-7 can be idle.

Quoted 8 lines

A task may not use all the CPUs in a schedule group due to CPU affinity. Only update schedule group statistics for allowed CPUs.

---
Resending this patch from the patchset:
https://lore.kernel.org/lkml/[email protected]/

Quoted 5 lines

Right, lets start with this then ;-)

No need to do the cpumask_and() thing, that's just more changes vs update_sg_lb_stats().

I just saw Peter's comments on this. I'm OK with the current version and Adam please feel free to keep my Reviewed-by tag for this non-cpumask_and version.

thanks,
Chenyu

merge bot Applied
#7 lore raw
In reply to Adam Li
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:     82d6e01a0699800efd8b048eb584c907ccb47b7a
Gitweb:        https://git.kernel.org/tip/82d6e01a0699800efd8b048eb584c907ccb47b7a
Author:        Adam Li <[email protected]>
AuthorDate:    Sat, 11 Oct 2025 06:43:22 
Committer:     Peter Zijlstra <[email protected]>
CommitterDate: Thu, 16 Oct 2025 11:13:50 +02:00

sched/fair: Only update stats for allowed CPUs when looking for dst group

Load imbalance is observed when the workload frequently forks new threads. Due to CPU affinity, the workload can run on CPU 0-7 in the first group, and only on CPU 8-11 in the second group. CPU 12-15 are always idle.

{ 0 1 2 3 4 5 6 7 } {8 9 10 11 12 13 14 15}
* * * * * * * * * * * *

When looking for dst group for newly forked threads, in many times update_sg_wakeup_stats() reports the second group has more idle CPUs than the first group. The scheduler thinks the second group is less busy. Then it selects least busy CPUs among CPU 8-11. Therefore CPU 8-11 can be crowded with newly forked threads, at the same time CPU 0-7 can be idle.

A task may not use all the CPUs in a schedule group due to CPU affinity. Only update schedule group statistics for allowed CPUs.

---
 kernel/sched/fair.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/fair.c +1 -1
index 00f9d6c..ac881df 100644
@@ -10683,7 +10683,7 @@ static inline void update_sg_wakeup_stats(struct sched_domain *sd,
 	if (sd->flags & SD_ASYM_CPUCAPACITY)
 		sgs->group_misfit_task_load = 1;
 
-	for_each_cpu(i, sched_group_span(group)) {
+	for_each_cpu_and(i, sched_group_span(group), p->cpus_ptr) {
 		struct rq *rq = cpu_rq(i);
 		unsigned int local;