From mboxrd@z Thu Jan 1 00:00:00 1970 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=1795928179; b=Qw2dDKZemhiAJCR/7HQLhkZExdLv/Rnoa4DuChbtvI/0WH6KoA2R3/lcc+PszAt3JYgl 5W56aD+Lt1yxypcGtpdELFqWj8pCWCRm4kddgRD8mIR5d3ECJjV6V1XlCbHvq1yyiFB3W sJLUD3QV2iqFsFjF+AN9ysOKJNqHBcIM6zyPXhFVV7wxdEJ6qfPsCcVgRWt0P8wjKZ8N2 m4vg9TIOS3yVf2IHt/owQEoVF9FHkyTB/dQpRkEQvEWLXom6Y3U+KxzDhXxOc53KelSyq gt4bkUosnBB5+mq1TXJ5UpJco/0togNjW8MfKbJDECdkPBC/JgA9rL1RBHD0U2TQQYw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795928179; 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=ZnJ7sua6dV8A3veVetYPKIM0Lyn64Sv0FV49uX05MhY=; b=4xh4eMwuAynFu4T8fOlliOtpjtxIwgc6X4Vuf4GnB4OVRo36NSK3lVjqejQnk0RxITOu 03fIJHZyxx0tOC9j6hnFF8Gvkq4341FbsCJfZfvcU9JdVSCkhJJYzs0UJG3FtXMB9VpMt m8E/wt21JMJy/Aya7gSlVExTc5SDGkfzC+68qU0tdoDqrhXqYNACg177Z12ZiXI8+XWZO j69COOT0IhIu7C25jKt0nNGWlJ1/IcoITkfCNNfxAjBWRyhKO/5ePLLCtIX3UrcsSZQu8 oB6I04bbxs4HjJ7nHh+W/bSSxGClRBrUffioAwZfT/xj8yl8xUIQUmhrEk6f6R+tRiw== 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=McCYiQeP; dkim=pass header.d=purelymail.com header.i=@purelymail.com header.a=rsa-sha256 header.s=purelymail3 header.b=oAJfbjqB; 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 7CC06606A333 for ; Sun, 29 Nov 2026 04:56:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7CC06606A333 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=McCYiQeP; dkim=pass (2048-bit key, unprotected) header.d=purelymail.com header.i=@purelymail.com header.a=rsa-sha256 header.s=purelymail3 header.b=oAJfbjqB DKIM-Signature: a=rsa-sha256; b=McCYiQePZrPvDsVWPP3Ut8wAQ/58adq/+0sHIcz3PApyx7NnDcB+yC+W/GZiPG++gCJtr5+PlmxA0K/1LXuhTBfjmlemEwUjhYzGF3WRqZ3sLVMSO51A/Q6lReglSrqCRr2+t4q5B+W071NIhR/k7ZUBeTqzWaEjmGjm2+kRJ+ktw8l3GY7aQuWA2QYyVqmUDpmyyL5fMq4jLr34wRqWdIAIcV9QMuWrNQotk3tHyWIg5gkaH+GHMmlxA3xBpXZuibri7CyWkNYlt9fkNr7f6P//0JQ4yApqX5hxS5WMwTKPexPR4M16xUR82puYNoC1vRH9c9Zg3QAzALVlpWpvRw==; s=purelymail3; d=atomlin.com; v=1; bh=Vt+fLhw025Gn1IUztJydZm20cUi6gqYK4O47pYmWyyo=; h=Received:From:To:Subject:Date:Message-ID; DKIM-Signature: a=rsa-sha256; b=oAJfbjqB+8C5YFMSfY7Mvk94L2Lk1r1FjWIIi86SSXrPghdm+Lrbq88OcUNbUxKmYGK9IeBDScfDg/tTY5w2VATO6xBAjonxv+wpGBq2uibP3xR1cp1b38mkSwCEFo1taE3d8QpY8cck3/8Ji0hFPiDRWO8p8bMxgI+CWg2nSMuZKJxi0mZndrgtH8LoYrUjwZlKYyOYfD8BWw6VRxtEraT/qRy+vgbTaO1G8Yel+3uG03/UcC7eLLpV2UOgbF154GZPmItMdnIfR0OLwupwp0FrCcygVoTq81CoKPdbC/Gypre2GCsY2aFjBruxvHnn8HTGiNk8TlzHuDCTW57YKA==; s=purelymail3; d=purelymail.com; v=1; bh=Vt+fLhw025Gn1IUztJydZm20cUi6gqYK4O47pYmWyyo=; 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 1193360044; (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384); Sun, 29 Nov 2026 04:54:39 +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 v17 03/10] lib/group_cpus: Add group_mask_cpus_evenly() Date: Sat, 28 Nov 2026 23:54:27 -0500 Message-ID: <20261129045436.255364-4-atomlin@atomlin.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261129045436.255364-1-atomlin@atomlin.com> References: <20261129045436.255364-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 Co-developed-by: Aaron Tomlin Signed-off-by: Aaron Tomlin --- include/linux/group_cpus.h | 3 + lib/group_cpus.c | 110 +++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) 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..75bd082e00bf 100644 --- a/lib/group_cpus.c +++ b/lib/group_cpus.c @@ -563,3 +563,113 @@ struct cpumask *group_cpus_evenly(unsigned int numgrp= s, unsigned int *nummasks) =09return masks; } EXPORT_SYMBOL_GPL(group_cpus_evenly); + +/** + * 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. 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 + * the same group. + * + * 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_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, local_mask, npresmsk; +=09int ret =3D -ENOMEM; +=09struct cpumask *masks =3D NULL; + +=09if (numgrps =3D=3D 0) +=09=09return NULL; + +=09if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL)) +=09=09return NULL; + +=09if (!zalloc_cpumask_var(&local_mask, GFP_KERNEL)) +=09=09goto fail_nmsk; + +=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; + +=09masks =3D kzalloc_objs(*masks, numgrps); +=09if (!masks) +=09=09goto fail_node_to_cpumask; + +=09build_node_to_cpumask(node_to_cpumask); + +=09/* +=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(local_mask, mask); + +=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) +=09=09goto fail_node_to_cpumask; +=09nr_present =3D ret; + +=09/* +=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 * allocated out groups. +=09 */ +=09if (nr_present >=3D numgrps) +=09=09curgrp =3D 0; +=09else +=09=09curgrp =3D nr_present; +=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; + +fail_node_to_cpumask: +=09free_node_to_cpumask(node_to_cpumask); + +fail_npresmsk: +=09free_cpumask_var(npresmsk); + +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); --=20 2.55.0