Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=34.202.193.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796074186; b=Mn2ix7ASBV2ZnCanWS3LpGIvaQDSL9TPB5bsBfUD18veuLFYEgXU69EFfBgA6AK1iZlp 3IApnWpy7oJm6G8MNKTwzHtb8jMOTySSOrXqN8xm1uzg7DORQLtlSiugNNU0o/+KsVxSK WavNavf5Z3nVgZm0u6d2zF72bTI1NnbwyjTzT64ETwtI8DILqP9bifHulZNdsbPMhRdhr 4bvMtomR6p3Na4RyVliaa5nSqcI6d6HrhuQ1HHSRHtUOkv6TBwVU8jBAqBgRfIerrLR1y v3NFdq15Xh3CvKvkr89bZaL95g7m37x1yRUreomsLWmyqMZu1c7ifxohMb4nRtorPhg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796074186; h=DMARC-Filter:DKIM-Signature:DKIM-Signature:Feedback-ID: X-Pm-Original-To:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding: X-MIME-Autoconverted:Content-Type; bh=YaLWt27T2PgYVQ73cl6oNJ8uRpnErinjLGTzmtiQJpI=; b=JH+NMhfHoiWZK7NFfIcq6xPutPKULmHdR/oO6kDuX12OGL9HXI09WbkSfB1OGmloGbve LEB8erEMcGxdS8Mct9ai+xYClajGtAX9VjBprGl7/0Ud0xJWo29yfU+OvAQgZHo/G5lzk bU9AX9ow64MCZHOYmwGdYIQWdTvuQ3BVksnIbEYT3/Wy1G0rLAGnWUhm4+xsnZ+YzABka KUY0yKlLFKqPak/dGZYzdLY6moyunyW2YzvnyPOZVBP8yqhKOUBDj43M38sLf8VMz71Sy QqSPmd4O2LV+TlnwR8Fc8C7bZvOpnfGdu+xE1d3E2qMvxR1WWaM+OnSAiP6jeFAtUbg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=atomlin.com; spf=pass smtp.mailfrom=atomlin.com; dkim=pass header.d=atomlin.com header.i=@atomlin.com header.a=rsa-sha256 header.s=purelymail3 header.b="B/N1aFs+"; dkim=pass header.d=purelymail.com header.i=@purelymail.com header.a=rsa-sha256 header.s=purelymail3 header.b=lzy3Kb4G; arc=none smtp.remote-ip=34.202.193.197 Received: from sendmail.purelymail.com (sendmail.purelymail.com [34.202.193.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAAF4606A31A for ; Mon, 30 Nov 2026 21:29:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DAAF4606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=atomlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=atomlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=atomlin.com header.i=@atomlin.com header.a=rsa-sha256 header.s=purelymail3 header.b=B/N1aFs+; dkim=pass (2048-bit key, unprotected) header.d=purelymail.com header.i=@purelymail.com header.a=rsa-sha256 header.s=purelymail3 header.b=lzy3Kb4G DKIM-Signature: a=rsa-sha256; b=B/N1aFs+75N829/XzKqQruiT6sMcL7lLhqTgFdQsIXggoUWaehN1aXYIlZR4ymO8i8v/Nf0tRy+z3zNK02FDsqv5S3hfqaXnNHrWPRiLhYIwfiWzH8fYo/MWNuuLWmU0tqK5lgxYn2pRiNqG42vdLyg7x+JfzBWT8BYlykUSwDliNAlK3M0QObox99q2Ru7gUFtAwCe91fqEzLahzykkybzwukonK4qGEqR1DN9F6SzFv4uKvK393iyxhJPp0WVgVzqsXQDitEqK99SE3OHc6NR5p5fjbJGO0AFYKQ04LohF48jouaX5WrBeS71V5i0blBAVhdU2RqARP4GkH/YXpw==; s=purelymail3; d=atomlin.com; v=1; bh=A4Tzv7ACu47itgwHEmUbbrjsiZ4oZrrQHhmFMjgwjP0=; h=Received:From:To:Subject:Date:Message-ID; DKIM-Signature: a=rsa-sha256; b=lzy3Kb4GPYOU1BSEy8DNzXCRUvQAoVGMtXN462SJlXMSVf7edUU9LRFcqa8hv68maTRqg2mrv2WpAgt1CCT0NCgzAnfP129uM+Q9ncZZD7j3F9XQTuElL8RJcWmZ09QY/HgLMj66mFKXULnN0HmVbUWK3V9jGyYhp47kvas3tyH9yGcEiitTz0zQPGMavPDKyNJpZ1MszVnW9oXaMe8I4AHEWXHdctnYflJDcWjZoE7WxRG61MDiVxz2Xw09Yl/gRd59tn+cf9GKlJw8KIqxGvI8rk8TALaxsuvHwz2KLkNxDNBdeH0TeuLl37cZo7EaLtDkct2N7KC1YNaGYFG2Lw==; s=purelymail3; d=purelymail.com; v=1; bh=A4Tzv7ACu47itgwHEmUbbrjsiZ4oZrrQHhmFMjgwjP0=; h=Feedback-ID:Received:From:To:Subject:Date:Message-ID; Feedback-ID: 1802931:67223:null:purelymail X-Pm-Original-To: linux-doc@vger.kernel.org Authentication-Results: purelymail.com; auth=pass Received: by smtp.purelymail.com (Purelymail SMTP) with ESMTPSA id 364770897; (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384); Mon, 30 Nov 2026 21:28:33 +0000 (UTC) From: Aaron Tomlin 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 Message-ID: <20261130212830.373057-4-atomlin@atomlin.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261130212830.373057-1-atomlin@atomlin.com> References: <20261130212830.373057-1-atomlin@atomlin.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by Purelymail Content-Type: text/plain; charset=UTF-8 From: Daniel Wagner 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 Reviewed-by: Hannes Reinecke Acked-by: Frederic Weisbecker Acked-by: Marco Crivellari Co-developed-by: Aaron Tomlin Signed-off-by: Aaron Tomlin --- 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 =20 struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *numm= asks); +struct cpumask *group_mask_cpus_evenly(unsigned int numgrps, +=09=09=09=09 const struct cpumask *mask, +=09=09=09=09 unsigned int *nummasks); =20 #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, } =20 /** - * 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: - *=091) allocate present CPUs on these groups evenly first - *=092) 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 *numm= asks) +struct cpumask *group_mask_cpus_evenly(unsigned int numgrps, +=09=09=09=09 const struct cpumask *mask, +=09=09=09=09 unsigned int *nummasks) { =09unsigned int curgrp =3D 0, nr_present =3D 0, nr_others =3D 0; =09cpumask_var_t *node_to_cpumask; -=09cpumask_var_t nmsk, npresmsk; +=09cpumask_var_t nmsk, local_mask, npresmsk; =09int ret =3D -ENOMEM; =09struct cpumask *masks =3D NULL; =20 @@ -497,9 +500,12 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps= , unsigned int *nummasks) =09if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL)) =09=09return NULL; =20 -=09if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL)) +=09if (!zalloc_cpumask_var(&local_mask, GFP_KERNEL)) =09=09goto fail_nmsk; =20 +=09if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL)) +=09=09goto fail_local_mask; + =09node_to_cpumask =3D alloc_node_to_cpumask(); =09if (!node_to_cpumask) =09=09goto fail_npresmsk; @@ -511,20 +517,19 @@ struct cpumask *group_cpus_evenly(unsigned int numgrp= s, unsigned int *nummasks) =09build_node_to_cpumask(node_to_cpumask); =20 =09/* -=09 * Make a local cache of 'cpu_present_mask', so the two stages -=09 * spread can observe consistent 'cpu_present_mask' without holding -=09 * cpu hotplug lock, then we can reduce deadlock risk with cpu -=09 * hotplug code. -=09 * -=09 * Here CPU hotplug may happen when reading `cpu_present_mask`, and -=09 * we can live with the case because it only affects that hotplug -=09 * CPU is handled in the 1st or 2nd stage, and either way is correct -=09 * from API user viewpoint since 2-stage spread is sort of -=09 * optimization. +=09 * Create a stable snapshot of the mask. The grouping algorithm +=09 * requires the CPU count to remain constant across its multiple +=09 * passes. This prevents allocation failures if the caller passes a +=09 * dynamic mask (e.g., cpu_online_mask) that changes concurrently. =09 */ -=09cpumask_copy(npresmsk, data_race(cpu_present_mask)); +=09cpumask_copy(local_mask, mask); =20 -=09/* grouping present CPUs first */ +=09/* +=09 * Grouping present CPUs first. We intersect the provided mask with +=09 * cpu_present_mask to ensure that we prioritise physically +=09 * available CPUs for the initial distribution. +=09 */ +=09cpumask_and(npresmsk, local_mask, cpu_present_mask); =09ret =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, =09=09=09=09 npresmsk, nmsk, masks); =09if (ret < 0) @@ -532,34 +537,64 @@ struct cpumask *group_cpus_evenly(unsigned int numgrp= s, unsigned int *nummasks) =09nr_present =3D ret; =20 =09/* -=09 * Allocate non present CPUs starting from the next group to be +=09 * Allocate non-present CPUs starting from the next group to be =09 * handled. If the grouping of present CPUs already exhausted the -=09 * group space, assign the non present CPUs to the already +=09 * group space, assign the non-present CPUs to the already =09 * allocated out groups. =09 */ =09if (nr_present >=3D numgrps) =09=09curgrp =3D 0; =09else =09=09curgrp =3D nr_present; -=09cpumask_andnot(npresmsk, cpu_possible_mask, npresmsk); +=09cpumask_andnot(npresmsk, local_mask, npresmsk); =09ret =3D __group_cpus_evenly(curgrp, numgrps, node_to_cpumask, =09=09=09=09 npresmsk, nmsk, masks); =09if (ret >=3D 0) =09=09nr_others =3D ret; =20 - fail_node_to_cpumask: +fail_node_to_cpumask: =09free_node_to_cpumask(node_to_cpumask); =20 - fail_npresmsk: +fail_npresmsk: =09free_cpumask_var(npresmsk); =20 - fail_nmsk: +fail_local_mask: +=09free_cpumask_var(local_mask); + +fail_nmsk: =09free_cpumask_var(nmsk); =09if (ret < 0) { =09=09kfree(masks); =09=09return NULL; =09} =09*nummasks =3D min(nr_present + nr_others, numgrps); +=09if (*nummasks =3D=3D 0) { +=09=09kfree(masks); +=09=09return NULL; +=09} =09return 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: + *=091) allocate present CPUs on these groups evenly first + *=092) 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 *numm= asks) +{ +=09return group_mask_cpus_evenly(numgrps, cpu_possible_mask, nummasks); +} EXPORT_SYMBOL_GPL(group_cpus_evenly); --=20 2.55.0