[PATCH v2 18/23] nitro_enclaves: Free the CPU pool that ne_exit() does not see

From: Alexander Graf

Date: Sun Nov 29 2026 - 13:32:33 EST


When the kernel command line carries nitro_enclaves.ne_cpus=, modprobe
passes that value to the module ahead of its own arguments. A
"modprobe nitro_enclaves ne_cpus=banana" then builds the pool from the
command line value and offlines its CPUs. The second value fails to
parse, which keeps that pool and aborts the load. ne_exit() does not run
for a failed load, so the pool stays allocated with its CPUs offline.
Every later modprobe fails with "CPU 2 is offline, has to be online to
get its metadata" until someone onlines the CPUs by hand. Any other load
failure after a pool was built ends the same way.

rmmod can leave a pool behind too. A write to the ne_cpus parameter
takes no reference on the module, so ne_exit() can tear down the pool
while the write is still offlining the CPUs of a new one. The write then
publishes its pool after the module tore down the old one.

The module loader calls the free callback of every parameter when a load
fails at or after the point where it parses the arguments. It also calls
it on unload, after removing the parameter files, which waits for any
write in progress and refuses new ones. Tear down the pool there.

Fixes: ff8a4d3e3a99 ("nitro_enclaves: Add logic for setting an enclave vCPU")
Signed-off-by: Alexander Graf <graf@xxxxxxxxxx>
---
drivers/virt/nitro_enclaves/ne_misc_dev.c | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)

diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index f5d723cebb348..232bdc651f833 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -86,10 +86,12 @@ struct ne_devs ne_devs = {
* a kernel parameter e.g. if multiple sysfs files needed.
*/
static int ne_set_kernel_param(const char *val, const struct kernel_param *kp);
+static void ne_free_kernel_param(void *arg);

static const struct kernel_param_ops ne_cpu_pool_ops = {
.get = param_get_string,
.set = ne_set_kernel_param,
+ .free = ne_free_kernel_param,
};

static char ne_cpus[NE_CPUS_SIZE];
@@ -591,6 +593,23 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
return rc;
}

+/**
+ * ne_free_kernel_param() - Tear down the NE CPU pool when the module goes away.
+ * @arg: The NE kernel parameter argument, unused.
+ *
+ * The module loader calls this when a load fails at or after argument
+ * parsing, and on unload once the parameter files are gone, so no write to
+ * the parameter can still be running. ne_exit() does not run for a failed
+ * load and can run while a write is still building a pool, so tear the pool
+ * down here too.
+ *
+ * Context: Process context. No driver lock may be held.
+ */
+static void ne_free_kernel_param(void *arg)
+{
+ ne_teardown_cpu_pool();
+}
+
/**
* ne_donated_cpu() - Check if the provided CPU is already used by the enclave.
* @ne_enclave : Private data associated with the current enclave.
--
2.47.1