Skip to contentSkip to conversation

sched/fair: use cpumask_weight_and() in sched_balance_find_dst_group()

PATCH Superseded Yury Norov 4 participants 4 messages Sep 11, 2025 Scheduler

lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH 1/1 sched/fair: use cpumask_weight_and() in sched_balance_find_dst_group()

From: Yury Norov (NVIDIA) <[email protected]>

In the group_has_spare case, the function creates a temporary cpumask to just calculate weight of (p->cpus_ptr & sched_group_span(local)).

We've got a dedicated helper for it.

---
 kernel/sched/fair.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/sched/fair.c +3 -4
index 7229339cbb1b..4ec012912cd1 100644
@@ -10821,10 +10821,9 @@ sched_balance_find_dst_group(struct sched_domain *sd, struct task_struct *p, int
 			 * take care of it.
 			 */
 			if (p->nr_cpus_allowed != NR_CPUS) {
-				struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
-
-				cpumask_and(cpus, sched_group_span(local), p->cpus_ptr);
-				imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr);
+				unsigned w = cpumask_weight_and(p->cpus_ptr,
+								sched_group_span(local));
+				imb_numa_nr = min(w, sd->imb_numa_nr);
 			}
 
 			imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus);
Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/1
#1 lore raw

Hello Yury,

Quoted 6 lines · Yury Norov

From: Yury Norov (NVIDIA) <[email protected]>

In the group_has_spare case, the function creates a temporary cpumask to just calculate weight of (p->cpus_ptr & sched_group_span(local)).

We've got a dedicated helper for it.

Neat! I didn't realize this existed back when I added that cpumask_and()
+ cpumask_weight() combo. Please feel free to include:

Quoted 24 lines
---
 kernel/sched/fair.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/sched/fair.c +3 -4
index 7229339cbb1b..4ec012912cd1 100644
@@ -10821,10 +10821,9 @@ sched_balance_find_dst_group(struct sched_domain *sd, struct task_struct *p, int
 			 * take care of it.
 			 */
 			if (p->nr_cpus_allowed != NR_CPUS) {
-				struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
-
-				cpumask_and(cpus, sched_group_span(local), p->cpus_ptr);
-				imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr);
+				unsigned w = cpumask_weight_and(p->cpus_ptr,
+								sched_group_span(local));
+				imb_numa_nr = min(w, sd->imb_numa_nr);
 			}
 
 			imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus);
Signature · 2 lines
Thanks and Regards,
Prateek
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Yury Norov
Quoted 9 lines · Yury Norov

From: Yury Norov (NVIDIA) <[email protected]>

In the group_has_spare case, the function creates a temporary cpumask to just calculate weight of (p->cpus_ptr & sched_group_span(local)).

We've got a dedicated helper for it.

Quoted 25 lines
---
 kernel/sched/fair.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
kernel/sched/fair.c +3 -5
index 7229339cbb1b..4ec012912cd1 100644
@@ -10821,10 +10821,9 @@ sched_balance_find_dst_group(struct sched_domain *sd, struct task_struct *p, int
                         * take care of it.
                         */
                        if (p->nr_cpus_allowed != NR_CPUS) {
-                               struct cpumask *cpus = this_cpu_cpumask_var_ptr(select_rq_mask);
-
-                               cpumask_and(cpus, sched_group_span(local), p->cpus_ptr);
-                               imb_numa_nr = min(cpumask_weight(cpus), sd->imb_numa_nr);
+                               unsigned w = cpumask_weight_and(p->cpus_ptr,
+                                                               sched_group_span(local));
+                               imb_numa_nr = min(w, sd->imb_numa_nr);
                        }
 
                        imbalance = abs(local_sgs.idle_cpus - idlest_sgs.idle_cpus);
--

2.43.0

Reviewed-by on v1 1/1
#3 lore raw
In reply to Yury Norov
Quoted 6 lines · Yury Norov

In the group_has_spare case, the function creates a temporary cpumask to just calculate weight of (p->cpus_ptr & sched_group_span(local)).

We've got a dedicated helper for it.

LGTM

--
Thanks,
Fernand

Amazon Development Centre (South Africa) (Proprietary) Limited 29 Gogosoa Street, Observatory, Cape Town, Western Cape, 7925, South Africa Registration Number: 2004 / 034463 / 07