[PATCH v18 3/9] lib/group_cpus: Add group_mask_cpus_evenly()

From: Aaron Tomlin

Date: Mon Nov 30 2026 - 16:30:13 EST


From: Daniel Wagner <wagi@xxxxxxxxxx>

This commit introduces group_mask_cpus_evenly(), which allows callers to
distribute a specific CPU mask evenly across groups. It serves as a bounded
version of group_cpus_evenly().

While group_cpus_evenly() operates on the global cpu_possible_mask,
group_mask_cpus_evenly() confines the distribution strictly within the
boundaries of the caller-provided mask. It preserves the kernel's native
two-stage spreading logic, first prioritising CPUs that are physically
present (cpu_present_mask) to prevent I/O starvation, and then distributing
any remaining vectors to non-present CPUs to maintain hotplug safety.

Signed-off-by: Daniel Wagner <wagi@xxxxxxxxxx>
Reviewed-by: Hannes Reinecke <hare@xxxxxxx>
Acked-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
Acked-by: Marco Crivellari <marco.crivellari@xxxxxxxx>
Co-developed-by: Aaron Tomlin <atomlin@xxxxxxxxxxx>
Signed-off-by: Aaron Tomlin <atomlin@xxxxxxxxxxx>
---
include/linux/group_cpus.h | 3 ++
lib/group_cpus.c | 97 ++++++++++++++++++++++++++------------
2 files changed, 69 insertions(+), 31 deletions(-)

diff --git a/include/linux/group_cpus.h b/include/linux/group_cpus.h
index 9d4e5ab6c314..defab4123a82 100644
--- a/include/linux/group_cpus.h
+++ b/include/linux/group_cpus.h
@@ -10,5 +10,8 @@
#include <linux/cpu.h>

struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks);
+struct cpumask *group_mask_cpus_evenly(unsigned int numgrps,
+ const struct cpumask *mask,
+ unsigned int *nummasks);

#endif
diff --git a/lib/group_cpus.c b/lib/group_cpus.c
index b8d54398f88a..5f0d93a9e3c6 100644
--- a/lib/group_cpus.c
+++ b/lib/group_cpus.c
@@ -467,27 +467,30 @@ static int __group_cpus_evenly(unsigned int startgrp, unsigned int numgrps,
}

/**
- * group_cpus_evenly - Group all CPUs evenly per NUMA/CPU locality
- * @numgrps: number of groups
+ * group_mask_cpus_evenly - Group all CPUs evenly per NUMA/CPU locality
+ * @numgrps: number of cpumasks to create
+ * @mask: CPUs to consider for the grouping
* @nummasks: number of initialized cpumasks
*
- * Return: cpumask array if successful, NULL otherwise. And each element
- * includes CPUs assigned to this group. nummasks contains the number
- * of initialized masks which can be less than numgrps.
+ * Return: cpumask array if successful, NULL otherwise. Only the CPUs
+ * marked in the mask will be considered for the grouping. And each
+ * element includes CPUs assigned to this group. nummasks contains the
+ * number of initialized masks which can be less than numgrps.
*
* Try to put close CPUs from viewpoint of CPU and NUMA locality into
- * same group, and run two-stage grouping:
- * 1) allocate present CPUs on these groups evenly first
- * 2) allocate other possible CPUs on these groups evenly
+ * the same group.
*
- * We guarantee in the resulted grouping that all CPUs are covered, and
- * no same CPU is assigned to multiple groups
+ * We guarantee in the resulting grouping that all CPUs specified in the
+ * provided mask are covered, and no same CPU is assigned to multiple
+ * groups.
*/
-struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks)
+struct cpumask *group_mask_cpus_evenly(unsigned int numgrps,
+ const struct cpumask *mask,
+ unsigned int *nummasks)
{
unsigned int curgrp = 0, nr_present = 0, nr_others = 0;
cpumask_var_t *node_to_cpumask;
- cpumask_var_t nmsk, npresmsk;
+ cpumask_var_t nmsk, local_mask, npresmsk;
int ret = -ENOMEM;
struct cpumask *masks = NULL;

@@ -497,9 +500,12 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks)
if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
return NULL;

- if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
+ if (!zalloc_cpumask_var(&local_mask, GFP_KERNEL))
goto fail_nmsk;

+ if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
+ goto fail_local_mask;
+
node_to_cpumask = alloc_node_to_cpumask();
if (!node_to_cpumask)
goto fail_npresmsk;
@@ -511,20 +517,19 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks)
build_node_to_cpumask(node_to_cpumask);

/*
- * Make a local cache of 'cpu_present_mask', so the two stages
- * spread can observe consistent 'cpu_present_mask' without holding
- * cpu hotplug lock, then we can reduce deadlock risk with cpu
- * hotplug code.
- *
- * Here CPU hotplug may happen when reading `cpu_present_mask`, and
- * we can live with the case because it only affects that hotplug
- * CPU is handled in the 1st or 2nd stage, and either way is correct
- * from API user viewpoint since 2-stage spread is sort of
- * optimization.
+ * Create a stable snapshot of the mask. The grouping algorithm
+ * requires the CPU count to remain constant across its multiple
+ * passes. This prevents allocation failures if the caller passes a
+ * dynamic mask (e.g., cpu_online_mask) that changes concurrently.
*/
- cpumask_copy(npresmsk, data_race(cpu_present_mask));
+ cpumask_copy(local_mask, mask);

- /* grouping present CPUs first */
+ /*
+ * Grouping present CPUs first. We intersect the provided mask with
+ * cpu_present_mask to ensure that we prioritise physically
+ * available CPUs for the initial distribution.
+ */
+ cpumask_and(npresmsk, local_mask, cpu_present_mask);
ret = __group_cpus_evenly(curgrp, numgrps, node_to_cpumask,
npresmsk, nmsk, masks);
if (ret < 0)
@@ -532,34 +537,64 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks)
nr_present = ret;

/*
- * Allocate non present CPUs starting from the next group to be
+ * Allocate non-present CPUs starting from the next group to be
* handled. If the grouping of present CPUs already exhausted the
- * group space, assign the non present CPUs to the already
+ * group space, assign the non-present CPUs to the already
* allocated out groups.
*/
if (nr_present >= numgrps)
curgrp = 0;
else
curgrp = nr_present;
- cpumask_andnot(npresmsk, cpu_possible_mask, npresmsk);
+ cpumask_andnot(npresmsk, local_mask, npresmsk);
ret = __group_cpus_evenly(curgrp, numgrps, node_to_cpumask,
npresmsk, nmsk, masks);
if (ret >= 0)
nr_others = ret;

- fail_node_to_cpumask:
+fail_node_to_cpumask:
free_node_to_cpumask(node_to_cpumask);

- fail_npresmsk:
+fail_npresmsk:
free_cpumask_var(npresmsk);

- fail_nmsk:
+fail_local_mask:
+ free_cpumask_var(local_mask);
+
+fail_nmsk:
free_cpumask_var(nmsk);
if (ret < 0) {
kfree(masks);
return NULL;
}
*nummasks = min(nr_present + nr_others, numgrps);
+ if (*nummasks == 0) {
+ kfree(masks);
+ return NULL;
+ }
return masks;
}
+EXPORT_SYMBOL_GPL(group_mask_cpus_evenly);
+
+/**
+ * group_cpus_evenly - Group all CPUs evenly per NUMA/CPU locality
+ * @numgrps: number of groups
+ * @nummasks: number of initialized cpumasks
+ *
+ * Return: cpumask array if successful, NULL otherwise. And each element
+ * includes CPUs assigned to this group. nummasks contains the number
+ * of initialized masks which can be less than numgrps.
+ *
+ * Try to put close CPUs from viewpoint of CPU and NUMA locality into
+ * same group, and run two-stage grouping:
+ * 1) allocate present CPUs on these groups evenly first
+ * 2) allocate other possible CPUs on these groups evenly
+ *
+ * We guarantee in the resulted grouping that all CPUs are covered, and
+ * no same CPU is assigned to multiple groups
+ */
+struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks)
+{
+ return group_mask_cpus_evenly(numgrps, cpu_possible_mask, nummasks);
+}
EXPORT_SYMBOL_GPL(group_cpus_evenly);
--
2.55.0