[PATCH 2/3] group_cpus: don't call cpumask_weight() prematurely

From: Yury Norov (NVIDIA)

Date: Tue Nov 18 2025 - 22:13:13 EST


alloc_nodes_groups() and __group_cpus_evenly() call cpumask_weight()
unconditionally in the for_each() loops. cpumask_weight() is O(N), so
the complexity of the functions become O(MAX_NUMNODES * nr_cpu_ids).

This call may be avoided if the nmsk is empty.

Signed-off-by: Yury Norov (NVIDIA) <yury.norov@xxxxxxxxx>
---
lib/group_cpus.c | 17 ++++++-----------
1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/lib/group_cpus.c b/lib/group_cpus.c
index 6d08ac05f371..6aae1560b796 100644
--- a/lib/group_cpus.c
+++ b/lib/group_cpus.c
@@ -142,15 +142,11 @@ static void alloc_nodes_groups(unsigned int numgrps,
}

for_each_node_mask(n, nodemsk) {
- unsigned ncpus;
-
- cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
- ncpus = cpumask_weight(nmsk);
-
- if (!ncpus)
+ if (!cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]))
continue;
- remaining_ncpus += ncpus;
- node_groups[n].ncpus = ncpus;
+
+ node_groups[n].ncpus = cpumask_weight(nmsk);
+ remaining_ncpus += node_groups[n].ncpus;
}

numgrps = min_t(unsigned, remaining_ncpus, numgrps);
@@ -294,11 +290,10 @@ static int __group_cpus_evenly(unsigned int startgrp, unsigned int numgrps,
continue;

/* Get the cpus on this node which are in the mask */
- cpumask_and(nmsk, cpu_mask, node_to_cpumask[nv->id]);
- ncpus = cpumask_weight(nmsk);
- if (!ncpus)
+ if (!cpumask_and(nmsk, cpu_mask, node_to_cpumask[nv->id]))
continue;

+ ncpus = cpumask_weight(nmsk);
WARN_ON_ONCE(nv->ngroups > ncpus);

/* Account for rounding errors */
--
2.43.0