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 20/23] nitro_enclaves: Set up the CPU pool under the CPU hotplug read lock
Date: Sun, 29 Nov 2026 18:21:49 +0000	[thread overview]
Message-ID: <d10c56aea437f18e430a9d36cdbd74ae81a112cc.1795965039.git.graf@amazon.com> (raw)
In-Reply-To: <cover.1794820000.git.graf@amazon.com>

An admin who writes a CPU list to ne_cpus while a CPU goes offline can
get a pool built from the topology before and after that CPU went.
ne_setup_cpu_pool() reads cpu_is_offline(), cpu_to_node() and
topology_sibling_cpumask() for the list without the CPU hotplug lock.

Take cpus_read_lock() over those reads, and drop it before the CPUs are
offlined, because remove_cpu() takes the write side. The failure paths
before the drop share one cpus_read_unlock().

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

  - Adapt to the pool setup that offlines the CPUs with no lock held
  - Unwind failures before the lock drop with one cpus_read_unlock()

 drivers/virt/nitro_enclaves/ne_misc_dev.c | 31 +++++++++++++++++------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index ed7af30f69afa..7fc6b0bb5dbc6 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -226,6 +226,11 @@ static void ne_detach_cpu_pool(cpumask_var_t **threads_per_core, unsigned int *n
  *			 pool after the checks passed.
  * @cpu_pool:	The CPU mask used for setting NE CPU pool.
  *
+ * The topology of the pool - online state, sibling masks, core ids - is read
+ * under the CPU hotplug read lock. The lock is dropped before the pool CPUs are
+ * offlined, because remove_cpu() takes the write side of the same percpu-rwsem.
+ * Nothing reads topology after the drop.
+ *
  * Context: Process context. No driver lock may be held: remove_cpu() and
  *	    add_cpu() take device_hotplug_lock. The pool mutex only guards the
  *	    publish of the finished pool.
@@ -245,13 +250,15 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 	int rc = -EINVAL;
 	cpumask_var_t *threads_per_core = NULL;
 
+	cpus_read_lock();
+
 	cpu = cpumask_any(cpu_pool);
 	if (cpu >= nr_cpu_ids) {
 		pr_err("%s: No CPUs available in CPU pool\n", ne_misc_dev.name);
 
 		rc = -EINVAL;
 
-		goto free_cores_cpumask;
+		goto unlock_hotplug;
 	}
 
 	/*
@@ -265,7 +272,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 
 			rc = -EINVAL;
 
-			goto free_cores_cpumask;
+			goto unlock_hotplug;
 		}
 
 	/*
@@ -280,7 +287,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 
 				rc = -EINVAL;
 
-				goto free_cores_cpumask;
+				goto unlock_hotplug;
 			}
 		} else {
 			if (numa_node != cpu_to_node(cpu)) {
@@ -289,7 +296,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 
 				rc = -EINVAL;
 
-				goto free_cores_cpumask;
+				goto unlock_hotplug;
 			}
 		}
 
@@ -302,7 +309,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 
 		rc = -EINVAL;
 
-		goto free_cores_cpumask;
+		goto unlock_hotplug;
 	}
 
 	for_each_cpu(cpu_sibling, topology_sibling_cpumask(0)) {
@@ -312,7 +319,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 
 			rc = -EINVAL;
 
-			goto free_cores_cpumask;
+			goto unlock_hotplug;
 		}
 	}
 
@@ -329,7 +336,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 
 				rc = -EINVAL;
 
-				goto free_cores_cpumask;
+				goto unlock_hotplug;
 			}
 		}
 	}
@@ -345,7 +352,7 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 	if (!threads_per_core) {
 		rc = -ENOMEM;
 
-		goto free_cores_cpumask;
+		goto unlock_hotplug;
 	}
 
 	for (i = 0; i < nr_parent_vm_cores; i++)
@@ -373,6 +380,8 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 		cpumask_set_cpu(cpu, threads_per_core[core_id]);
 	}
 
+	cpus_read_unlock();
+
 	/*
 	 * CPUs that are given to enclave(s) should not be considered online
 	 * by Linux anymore, as the hypervisor will degrade them to floating.
@@ -410,8 +419,14 @@ static int ne_setup_cpu_pool(const struct cpumask *cpu_pool)
 online_cpus:
 	for_each_cpu(cpu, cpu_pool)
 		add_cpu(cpu);
+	ne_free_core_slots(threads_per_core, nr_parent_vm_cores);
+
+	return rc;
+
 free_cores_cpumask:
 	ne_free_core_slots(threads_per_core, nr_parent_vm_cores);
+unlock_hotplug:
+	cpus_read_unlock();
 
 	return rc;
 }
-- 
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 ` [PATCH v2 19/23] nitro_enclaves: Build the boot-time CPU pool once the CPUs are online Alexander Graf
2026-11-29 18:21 ` Alexander Graf [this message]
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=d10c56aea437f18e430a9d36cdbd74ae81a112cc.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®