mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexander Graf <graf@amazon.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Arnd Bergmann <arnd@arndb.de>
Cc: The AWS Nitro Enclaves Team <aws-nitro-enclaves-devel@amazon.com>,
	<linux-kernel@vger.kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	"Stefano Garzarella" <sgarzare@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Marek Szyprowski <m.szyprowski@samsung.com>,
	Robin Murphy <robin.murphy@arm.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	"David Hildenbrand" <david@kernel.org>,
	Mike Rapoport <rppt@kernel.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>, <diapop@amazon.com>,
	<mancio@amazon.com>, <mknaust@amazon.com>,
	<nh-open-source@amazon.com>, <kvm@vger.kernel.org>,
	<virtualization@lists.linux.dev>, <iommu@lists.linux.dev>,
	<linux-mm@kvack.org>, <linux-doc@vger.kernel.org>
Subject: [PATCH v2 19/23] nitro_enclaves: Build the boot-time CPU pool once the CPUs are online
Date: Sun, 29 Nov 2026 18:21:48 +0000	[thread overview]
Message-ID: <d770219a7418f734c5e5d091b8a33cedc3e14f19.1795965039.git.graf@amazon.com> (raw)
In-Reply-To: <cover.1794820000.git.graf@amazon.com>

An admin who passes nitro_enclaves.ne_cpus= to a kernel with the driver
built in gets no CPU pool, and this not very informative error message:

  Booting kernel: `2-5' invalid for parameter `nitro_enclaves.ne_cpus'

The parameter setter runs from parse_args() in start_kernel(), before
the page allocator and the secondary CPUs are up. Building a pool needs
both.

Keep a value that arrives while system_state is SYSTEM_BOOTING in
ne_cpus, note its parameter in ne_cpus_kp, and build the pool from it in
ne_init(), which runs once both are up. The pool building moves into
ne_replace_cpu_pool() for that. That way a valid list on the command
line gets its pool, and an invalid one reports why. A list that ne_init()
rejects is cleared out of ne_cpus, so the parameter does not keep naming
a pool that was never built.

Fixes: ff8a4d3e3a99 ("nitro_enclaves: Add logic for setting an enclave vCPU")
Signed-off-by: Alexander Graf <graf@amazon.com>
---
v1 -> v2:

  - Clear ne_cpus when ne_init() rejects the boot-time list (Sashiko)

 drivers/virt/nitro_enclaves/ne_misc_dev.c | 67 +++++++++++++++++++++--
 1 file changed, 62 insertions(+), 5 deletions(-)

diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index 232bdc651f833..ed7af30f69afa 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -14,6 +14,7 @@
 #include <linux/device.h>
 #include <linux/file.h>
 #include <linux/hugetlb.h>
+#include <linux/kernel.h>
 #include <linux/limits.h>
 #include <linux/list.h>
 #include <linux/miscdevice.h>
@@ -104,6 +105,13 @@ module_param_cb(ne_cpus, &ne_cpu_pool_ops, &ne_cpus_arg, 0644);
 /* https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html#cpu-lists */
 MODULE_PARM_DESC(ne_cpus, "<cpu-list> - CPU pool used for Nitro Enclaves");
 
+/*
+ * The parameter a value from the kernel command line is kept in until ne_init()
+ * builds its pool. Nothing else writes ne_cpus, so it is set whenever ne_cpus
+ * holds a value, and no sysfs write can reach the setter before ne_init() runs.
+ */
+static const struct kernel_param *ne_cpus_kp;
+
 /**
  * struct ne_cpu_pool - CPU pool used for Nitro Enclaves.
  * @avail_threads_per_core:	Available full CPU cores to be dedicated to
@@ -450,7 +458,7 @@ static void ne_snapshot_cpu_pool(struct cpumask *cpu_pool)
 }
 
 /**
- * ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter.
+ * ne_replace_cpu_pool() - Replace the NE CPU pool with the one a CPU list names.
  * @val:	NE CPU pool string value.
  * @kp :	NE kernel parameter associated with the NE CPU pool.
  *
@@ -459,7 +467,7 @@ static void ne_snapshot_cpu_pool(struct cpumask *cpu_pool)
  * * 0 on success.
  * * Negative return value on failure.
  */
-static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
+static int ne_replace_cpu_pool(const char *val, const struct kernel_param *kp)
 {
 	char error_val[] = "";
 	cpumask_var_t cpu_pool;
@@ -469,9 +477,6 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 	unsigned int nr_cores = 0;
 	int rc = -EINVAL;
 
-	if (!capable(CAP_SYS_ADMIN))
-		return -EPERM;
-
 	if (!zalloc_cpumask_var(&cpu_pool, GFP_KERNEL))
 		return -ENOMEM;
 
@@ -593,6 +598,39 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 	return rc;
 }
 
+/**
+ * ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter.
+ * @val:	NE CPU pool string value.
+ * @kp :	NE kernel parameter associated with the NE CPU pool.
+ *
+ * Context: Process context.
+ * Return:
+ * * 0 on success.
+ * * Negative return value on failure.
+ */
+static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
+{
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	/*
+	 * A value on the kernel command line reaches this from the parse_args()
+	 * call in start_kernel(), which runs before mm_core_init() has brought
+	 * up the allocator a cpumask comes from and long before smp_init() has
+	 * brought online the CPUs a pool is built out of. Keep the value and
+	 * leave the pool to ne_init(), which runs from do_basic_setup() with
+	 * both of those behind it. A value from modprobe or from sysfs arrives
+	 * with the system already up and is applied here.
+	 */
+	if (system_state == SYSTEM_BOOTING) {
+		ne_cpus_kp = kp;
+
+		return param_set_copystring(val, kp);
+	}
+
+	return ne_replace_cpu_pool(val, kp);
+}
+
 /**
  * ne_free_kernel_param() - Tear down the NE CPU pool when the module goes away.
  * @arg:	The NE kernel parameter argument, unused.
@@ -2005,8 +2043,27 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 static int __init ne_init(void)
 {
+	static char cpu_list[NE_CPUS_SIZE] __initdata;
 	int rc;
 
+	/*
+	 * Build the pool of a CPU list that came from the kernel command line,
+	 * which the setter only kept. The value is taken out of the parameter
+	 * string first, because building the pool writes that string. A list
+	 * the build rejects is taken out of the parameter too, so ne_cpus does
+	 * not name a pool that was never built.
+	 */
+	if (ne_cpus_kp && ne_cpus[0]) {
+		memcpy(cpu_list, ne_cpus, sizeof(cpu_list));
+
+		if (ne_replace_cpu_pool(cpu_list, ne_cpus_kp) < 0) {
+			param_set_copystring("", ne_cpus_kp);
+
+			pr_err("%s: No CPU pool from nitro_enclaves.ne_cpus=%s\n",
+			       ne_misc_dev.name, cpu_list);
+		}
+	}
+
 	rc = pci_register_driver(&ne_pci_driver);
 	if (rc < 0)
 		ne_teardown_cpu_pool();
-- 
2.47.1


  parent reply	other threads:[~2026-11-29 18:33 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 18:21 [PATCH v2 00/23] nitro_enclaves: Fix enclave release, command retries and the CPU pool Alexander Graf
2026-11-29 18:21 ` [PATCH v2 01/23] MAINTAINERS: Take over the Nitro Enclaves driver Alexander Graf
2026-11-29 18:21 ` [PATCH v2 02/23] nitro_enclaves: Release the reference on a rejected region page Alexander Graf
2026-11-29 18:21 ` [PATCH v2 03/23] nitro_enclaves: Request write access to enclave memory regions Alexander Graf
2026-11-29 18:21 ` [PATCH v2 04/23] nitro_enclaves: Split freeing the slot from freeing the enclave Alexander Graf
2026-11-29 18:21 ` [PATCH v2 05/23] nitro_enclaves: Ignore the reply of a failed slot info command Alexander Graf
2026-11-29 18:21 ` [PATCH v2 06/23] nitro_enclaves: Retry a command the device asks us to retry Alexander Graf
2026-11-29 18:21 ` [PATCH v2 07/23] nitro_enclaves: Stop sending commands to a broken device Alexander Graf
2026-11-29 18:21 ` [PATCH v2 08/23] nitro_enclaves: Set up the device state before requesting its interrupts Alexander Graf
2026-11-29 18:21 ` [PATCH v2 09/23] nitro_enclaves: Online and offline pool CPUs with no pool lock held Alexander Graf
2026-11-29 18:21 ` [PATCH v2 10/23] nitro_enclaves: Wait for file operations before unbinding the PCI device Alexander Graf
2026-11-29 18:21 ` [PATCH v2 11/23] nitro_enclaves: Let the region tracking arrays fall back to vmalloc Alexander Graf
2026-11-29 18:21 ` [PATCH v2 12/23] nitro_enclaves: Do not return enclave threads to a replaced CPU pool Alexander Graf
2026-11-29 18:21 ` [PATCH v2 13/23] nitro_enclaves: Do not report a torn down CPU pool as set Alexander Graf
2026-11-29 18:21 ` [PATCH v2 14/23] nitro_enclaves: Keep the CPU pool when the new CPU list does not parse Alexander Graf
2026-11-29 18:21 ` [PATCH v2 15/23] nitro_enclaves: Put the CPU pool back when the new CPU list is refused Alexander Graf
2026-11-29 18:21 ` [PATCH v2 16/23] nitro_enclaves: Initialise the CPU pool mutex statically Alexander Graf
2026-11-29 18:21 ` [PATCH v2 17/23] nitro_enclaves: Free the CPU pool when driver registration fails Alexander Graf
2026-11-29 18:21 ` [PATCH v2 18/23] nitro_enclaves: Free the CPU pool that ne_exit() does not see Alexander Graf
2026-11-29 18:21 ` Alexander Graf [this message]
2026-11-29 18:21 ` [PATCH v2 20/23] nitro_enclaves: Set up the CPU pool under the CPU hotplug read lock Alexander Graf
2026-11-29 18:21 ` [PATCH v2 21/23] nitro_enclaves: Let 32-bit processes use the ioctl interface Alexander Graf
2026-11-29 18:21 ` [PATCH v2 22/23] nitro_enclaves: Index CPU pool cores by sibling mask Alexander Graf
2026-11-29 18:21 ` [PATCH v2 23/23] nitro_enclaves: Time the wait for the device to turn off by the clock Alexander Graf

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=d770219a7418f734c5e5d091b8a33cedc3e14f19.1795965039.git.graf@amazon.com \
    --to=graf@amazon.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=aws-nitro-enclaves-devel@amazon.com \
    --cc=corbet@lwn.net \
    --cc=david@kernel.org \
    --cc=diapop@amazon.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=iommu@lists.linux.dev \
    --cc=kvm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=m.szyprowski@samsung.com \
    --cc=mancio@amazon.com \
    --cc=mknaust@amazon.com \
    --cc=nh-open-source@amazon.com \
    --cc=pbonzini@redhat.com \
    --cc=rdunlap@infradead.org \
    --cc=robin.murphy@arm.com \
    --cc=rppt@kernel.org \
    --cc=sgarzare@redhat.com \
    --cc=skhan@linuxfoundation.org \
    --cc=stefanha@redhat.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®