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 09/19] nitro_enclaves: Do not return enclave threads to a replaced CPU pool
Date: Thu, 12 Nov 2026 03:07:05 +0000	[thread overview]
Message-ID: <e13de8c105a9131c65672c31db28db0629f66663.1794393487.git.graf@amazon.com> (raw)
In-Reply-To: <cover.1794300000.git.graf@amazon.com>

A write to ne_cpus checks that no enclave exists, drops
enclaves_list_mutex and then replaces the CPU pool, so an NE_CREATE_VM
in between gets an enclave whose release writes into the freed pool.
Hold the mutex across the check, the teardown and the setup, and take it
and ne_devs.lock killable, as the ioctls do.

Fixes: 9c8eb50fe9e2 ("nitro_enclaves: Add logic for terminating an enclave")
Signed-off-by: Alexander Graf <graf@amazon.com>
---
 drivers/virt/nitro_enclaves/ne_misc_dev.c | 76 +++++++++++------------
 1 file changed, 37 insertions(+), 39 deletions(-)

diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index ac33fbcd41f35..0a1080443fc72 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -137,37 +137,6 @@ struct ne_phys_contig_mem_regions {
 	struct range  *regions;
 };
 
-/**
- * ne_check_enclaves_created() - Verify if at least one enclave has been created.
- * @void:	No parameters provided.
- *
- * Context: Process context.
- * Return:
- * * True if at least one enclave is created.
- * * False otherwise.
- */
-static bool ne_check_enclaves_created(void)
-{
-	struct ne_pci_dev *ne_pci_dev = NULL;
-	bool ret = false;
-
-	down_read(&ne_devs.lock);
-
-	ne_pci_dev = ne_devs.ne_pci_dev;
-	if (ne_pci_dev) {
-		mutex_lock(&ne_pci_dev->enclaves_list_mutex);
-
-		if (!list_empty(&ne_pci_dev->enclaves_list))
-			ret = true;
-
-		mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
-	}
-
-	up_read(&ne_devs.lock);
-
-	return ret;
-}
-
 /**
  * ne_setup_cpu_pool() - Set the NE CPU pool after handling sanity checks such
  *			 as not sharing CPU cores with the primary / parent VM
@@ -176,7 +145,8 @@ static bool ne_check_enclaves_created(void)
  *			 pool after the checks passed.
  * @ne_cpu_list:	The CPU list used for setting NE CPU pool.
  *
- * Context: Process context.
+ * Context: Process context. This function is called with the ne_pci_dev enclave
+ *	    mutex held, if there is a NE PCI device.
  * Return:
  * * 0 on success.
  * * Negative return value on failure.
@@ -385,7 +355,8 @@ static int ne_setup_cpu_pool(const char *ne_cpu_list)
  *			    CPU pool.
  * @void:	No parameters provided.
  *
- * Context: Process context.
+ * Context: Process context. This function is called with the ne_pci_dev enclave
+ *	    mutex held, if there is a NE PCI device.
  */
 static void ne_teardown_cpu_pool(void)
 {
@@ -435,15 +406,37 @@ static void ne_teardown_cpu_pool(void)
 static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 {
 	char error_val[] = "";
+	struct ne_pci_dev *ne_pci_dev = NULL;
 	int rc = -EINVAL;
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 
-	if (ne_check_enclaves_created()) {
+	rc = down_read_killable(&ne_devs.lock);
+	if (rc)
+		return rc;
+
+	ne_pci_dev = ne_devs.ne_pci_dev;
+
+	/*
+	 * Keep the enclave list from gaining an entry while the pool is being
+	 * replaced, so that no enclave ends up holding a core count that
+	 * describes a pool that is already gone. The list lives in the PCI
+	 * device, which is not bound yet when this runs for a value given on
+	 * the kernel command line; nothing can create an enclave then either.
+	 */
+	if (ne_pci_dev) {
+		rc = mutex_lock_killable(&ne_pci_dev->enclaves_list_mutex);
+		if (rc)
+			goto unlock_devs;
+	}
+
+	if (ne_pci_dev && !list_empty(&ne_pci_dev->enclaves_list)) {
 		pr_err("%s: The CPU pool is used by enclave(s)\n", ne_misc_dev.name);
 
-		return -EPERM;
+		rc = -EPERM;
+
+		goto unlock;
 	}
 
 	ne_teardown_cpu_pool();
@@ -454,7 +447,7 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 
 		param_set_copystring(error_val, kp);
 
-		return rc;
+		goto unlock;
 	}
 
 	rc = param_set_copystring(val, kp);
@@ -464,11 +457,16 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp)
 		ne_teardown_cpu_pool();
 
 		param_set_copystring(error_val, kp);
-
-		return rc;
 	}
 
-	return 0;
+unlock:
+	if (ne_pci_dev)
+		mutex_unlock(&ne_pci_dev->enclaves_list_mutex);
+
+unlock_devs:
+	up_read(&ne_devs.lock);
+
+	return rc;
 }
 
 /**
-- 
2.47.1


  parent reply	other threads:[~2026-11-12  3:07 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 " 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 ` Alexander Graf [this message]
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 ` [PATCH 12/19] nitro_enclaves: Put the CPU pool back when the new CPU list is refused Alexander Graf
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=e13de8c105a9131c65672c31db28db0629f66663.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®