mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexander Graf <graf@amazon.com>
To: <linux-kernel@vger.kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>, <nh-open-source@amazon.com>,
	"The AWS Nitro Enclaves Team"
	<aws-nitro-enclaves-devel@amazon.com>
Subject: [PATCH 12/19] nitro_enclaves: Put the CPU pool back when the new CPU list is refused
Date: Thu, 12 Nov 2026 03:07:08 +0000	[thread overview]
Message-ID: <b9098275c3fc6be796306fbaea09d0def4674965.1794393487.git.graf@amazon.com> (raw)
In-Reply-To: <cover.1794300000.git.graf@amazon.com>

An admin who writes a CPU list that the driver refuses, for example one
that names CPU 0, leaves out a sibling or spans two NUMA nodes, loses
the pool that was there. The checks need every CPU of the list online
and a CPU the pool holds is offline, so they can only run after the old
pool is torn down.

Snapshot the CPUs of the pool before the teardown, and set the pool up
again from the snapshot when the new list is refused. Where that fails
as well, clear ne_cpus so that it does not name a pool that is gone.

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

diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index de3bf93a4ef0f..70d740d64ee17 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -382,6 +382,24 @@ static void ne_teardown_cpu_pool(void)
 	mutex_unlock(&ne_cpu_pool.mutex);
 }
 
+/**
+ * ne_snapshot_cpu_pool() - Copy out the CPUs the NE CPU pool holds.
+ * @cpu_pool:	CPU mask to fill in with the CPUs from the NE CPU pool.
+ *
+ * Context: Process context.
+ */
+static void ne_snapshot_cpu_pool(struct cpumask *cpu_pool)
+{
+	unsigned int i = 0;
+
+	mutex_lock(&ne_cpu_pool.mutex);
+
+	for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
+		cpumask_or(cpu_pool, cpu_pool, ne_cpu_pool.avail_threads_per_core[i]);
+
+	mutex_unlock(&ne_cpu_pool.mutex);
+}
+
 /**
  * ne_set_kernel_param() - Set the NE CPU pool value via the NE kernel parameter.
  * @val:	NE CPU pool string value.
@@ -396,6 +414,7 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 {
 	char error_val[] = "";
 	cpumask_var_t cpu_pool;
+	cpumask_var_t old_pool;
 	struct ne_pci_dev *ne_pci_dev = NULL;
 	int rc = -EINVAL;
 
@@ -405,6 +424,12 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 	if (!zalloc_cpumask_var(&cpu_pool, GFP_KERNEL))
 		return -ENOMEM;
 
+	if (!zalloc_cpumask_var(&old_pool, GFP_KERNEL)) {
+		free_cpumask_var(cpu_pool);
+
+		return -ENOMEM;
+	}
+
 	rc = down_read_killable(&ne_devs.lock);
 	if (rc)
 		goto free_cpumasks;
@@ -443,13 +468,16 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 		goto unlock;
 	}
 
+	ne_snapshot_cpu_pool(old_pool);
+
 	ne_teardown_cpu_pool();
 
 	rc = ne_setup_cpu_pool(cpu_pool);
 	if (rc < 0) {
 		pr_err("%s: Error in setup CPU pool [rc=%d]\n", ne_misc_dev.name, rc);
 
-		param_set_copystring(error_val, kp);
+		if (cpumask_empty(old_pool) || ne_setup_cpu_pool(old_pool) < 0)
+			param_set_copystring(error_val, kp);
 
 		goto unlock;
 	}
@@ -460,7 +488,8 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 
 		ne_teardown_cpu_pool();
 
-		param_set_copystring(error_val, kp);
+		if (cpumask_empty(old_pool) || ne_setup_cpu_pool(old_pool) < 0)
+			param_set_copystring(error_val, kp);
 	}
 
 unlock:
@@ -471,6 +500,8 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 	up_read(&ne_devs.lock);
 
 free_cpumasks:
+	free_cpumask_var(old_pool);
+
 	free_cpumask_var(cpu_pool);
 
 	return rc;
-- 
2.47.1


  parent reply	other threads:[~2026-11-12  3:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 13:49 [PATCH 00/19] nitro_enclaves: Fix enclave release, command retries and the CPU pool Alexander Graf
2026-11-11 13:49 ` [PATCH 01/19] nitro_enclaves: Release the reference on a rejected region page Alexander Graf
2026-11-11 13:49 ` [PATCH 02/19] nitro_enclaves: Split freeing the slot from freeing the enclave Alexander Graf
2026-11-11 13:49 ` [PATCH 03/19] nitro_enclaves: Ignore the reply of a failed slot info command Alexander Graf
2026-11-11 13:49 ` [PATCH 04/19] nitro_enclaves: Retry a command the device asks us to retry Alexander Graf
2026-11-11 13:49 ` [PATCH 05/19] nitro_enclaves: Stop sending commands to a broken device Alexander Graf
2026-11-11 13:49 ` [PATCH 06/19] nitro_enclaves: Set up the device state before requesting its interrupts Alexander Graf
2026-11-11 13:49 ` [PATCH 07/19] nitro_enclaves: Wait for file operations before unbinding the PCI device Alexander Graf
2026-11-12  3:07 ` [PATCH 08/19] nitro_enclaves: Let the region tracking arrays fall back to vmalloc Alexander Graf
2026-11-12  3:07 ` [PATCH 09/19] nitro_enclaves: Do not return enclave threads to a replaced CPU pool Alexander Graf
2026-11-12  3:07 ` [PATCH 10/19] nitro_enclaves: Do not report a torn down CPU pool as set Alexander Graf
2026-11-12  3:07 ` [PATCH 11/19] nitro_enclaves: Keep the CPU pool when the new CPU list does not parse Alexander Graf
2026-11-12  3:07 ` Alexander Graf [this message]
2026-11-12  3:07 ` [PATCH 13/19] nitro_enclaves: Initialise the CPU pool mutex statically Alexander Graf
2026-11-12  3:07 ` [PATCH 14/19] nitro_enclaves: Build the boot-time CPU pool once the CPUs are online Alexander Graf
2026-11-12  3:07 ` [PATCH 15/19] nitro_enclaves: Set up the CPU pool under the CPU hotplug read lock Alexander Graf
2026-11-12  3:07 ` [PATCH 16/19] nitro_enclaves: Let 32-bit processes use the ioctl interface Alexander Graf
2026-11-12  3:07 ` [PATCH 17/19] nitro_enclaves: Index CPU pool cores by sibling mask Alexander Graf
2026-11-12  3:07 ` [PATCH 18/19] nitro_enclaves: Time the wait for the device to turn off by the clock Alexander Graf
2026-11-12  3:07 ` [PATCH 19/19] MAINTAINERS: Take over the Nitro Enclaves driver 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=b9098275c3fc6be796306fbaea09d0def4674965.1794393487.git.graf@amazon.com \
    --to=graf@amazon.com \
    --cc=arnd@arndb.de \
    --cc=aws-nitro-enclaves-devel@amazon.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nh-open-source@amazon.com \
    /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®