From: Aaron Tomlin <atomlin@atomlin.com>
To: axboe@kernel.dk, tglx@kernel.org, aacraid@microsemi.com,
James.Bottomley@HansenPartnership.com, mkp@kernel.org,
frederic@kernel.org, bigeasy@linutronix.de, gmaglione@redhat.com,
vgoyal@redhat.com, stefanha@redhat.com, miklos@szeredi.hu
Cc: atomlin@atomlin.com, dwagner@suse.de, eperezma@redhat.com,
tom.leiming@gmail.com, ionut.nechita@windriver.com,
corbet@lwn.net, vincent.guittot@linaro.org, mingo@redhat.com,
peterz@infradead.org, radu@rendec.net, akpm@linux-foundation.org,
steve@abita.co, sean@ashe.io, chjohnst@gmail.com, neelx@suse.com,
mproche@gmail.com, nick.lange@gmail.com,
marco.crivellari@suse.com, rishil1999@outlook.com,
linux-doc@vger.kernel.org, linux-block@vger.kernel.org,
linux-scsi@vger.kernel.org, virtualization@lists.linux.dev,
linux-fsdevel@vger.kernel.org, fuse-devel@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: [PATCH v18 3/9] lib/group_cpus: Add group_mask_cpus_evenly()
Date: Mon, 30 Nov 2026 16:28:06 -0500 [thread overview]
Message-ID: <20261130212830.373057-4-atomlin@atomlin.com> (raw)
In-Reply-To: <20261130212830.373057-1-atomlin@atomlin.com>
From: Daniel Wagner <wagi@kernel.org>
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@kernel.org>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Acked-by: Frederic Weisbecker <frederic@kernel.org>
Acked-by: Marco Crivellari <marco.crivellari@suse.com>
Co-developed-by: Aaron Tomlin <atomlin@atomlin.com>
Signed-off-by: Aaron Tomlin <atomlin@atomlin.com>
---
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
next prev parent reply other threads:[~2026-11-30 21:29 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 21:28 [PATCH v18 0/9] blk: honor isolcpus configuration Aaron Tomlin
2026-11-30 21:28 ` [PATCH v18 1/9] scsi: aacraid: use block layer helpers to calculate num of queues Aaron Tomlin
2026-11-30 21:28 ` [PATCH v18 2/9] lib/group_cpus: remove dead !SMP code Aaron Tomlin
2026-11-30 21:28 ` Aaron Tomlin [this message]
2026-11-30 21:28 ` [PATCH v18 4/9] blk-mq: use hk cpus only when isolcpus=managed_irq is enabled Aaron Tomlin
2026-11-30 21:28 ` [PATCH v18 5/9] blk-mq: prevent offlining hk CPUs with associated online isolated CPUs Aaron Tomlin
2026-11-30 23:03 ` [PATCH v18 6/9] virtio-fs: honour isolcpus=managed_irq in queue mapping Aaron Tomlin
2026-11-30 23:22 ` sashiko-bot
2026-12-01 2:14 ` Aaron Tomlin
2026-11-30 23:03 ` [PATCH v18 7/9] genirq/affinity: Restrict managed IRQ affinity to housekeeping CPUs Aaron Tomlin
2026-11-30 23:03 ` [PATCH v18 8/9] docs: update isolcpus=managed_irq documentation Aaron Tomlin
2026-11-30 23:03 ` [PATCH v18 9/9] genirq/affinity: Confine pre and post vectors to housekeeping CPUs Aaron Tomlin
2026-11-30 23:25 ` [PATCH v18 0/9] blk: honor isolcpus configuration sashiko-bot
2026-12-01 13:20 ` Ionut Nechita
2026-12-01 20:07 ` Aaron Tomlin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130212830.373057-4-atomlin@atomlin.com \
--to=atomlin@atomlin.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=aacraid@microsemi.com \
--cc=akpm@linux-foundation.org \
--cc=axboe@kernel.dk \
--cc=bigeasy@linutronix.de \
--cc=chjohnst@gmail.com \
--cc=corbet@lwn.net \
--cc=dwagner@suse.de \
--cc=eperezma@redhat.com \
--cc=frederic@kernel.org \
--cc=fuse-devel@lists.linux.dev \
--cc=gmaglione@redhat.com \
--cc=ionut.nechita@windriver.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=marco.crivellari@suse.com \
--cc=miklos@szeredi.hu \
--cc=mingo@redhat.com \
--cc=mkp@kernel.org \
--cc=mproche@gmail.com \
--cc=neelx@suse.com \
--cc=nick.lange@gmail.com \
--cc=peterz@infradead.org \
--cc=radu@rendec.net \
--cc=rishil1999@outlook.com \
--cc=sean@ashe.io \
--cc=stefanha@redhat.com \
--cc=steve@abita.co \
--cc=tglx@kernel.org \
--cc=tom.leiming@gmail.com \
--cc=vgoyal@redhat.com \
--cc=vincent.guittot@linaro.org \
--cc=virtualization@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.