From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=52.35.192.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795976872; b=dT5Edpxv+3OBQSeAuJv/CN8lNpPZL9Pcvu/qTj0aPLUN3G/GpKtOufLCTZ/ofBLhSdWX U8NM0qo/hO3/Y7pz81xrk28DExGR+ReKtoFOrQ8onsUkUBaS67QOfzAPpSCqf7KiVmlU4 UD9QFY6hRVxsWCqXtH6aK5gN+zP2xkqbCo6fXmAT+++DYgyYyRFKmWCHUIzxYd4WS9nue 3BFUFIol0FukKyeIaXFRf7WHvejsr9UePlMIBlBUkneL1RktW5adI41Cn62jpCWIc+Qnx rjF53Ck92x2Ua6hK77QjypHuI+a6HZvKG2eIJLRnLK3388+Aw/8HkUN2jaI1mC4RLZw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795976872; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=QsENTMfw+XgoRJeBI1z5fv7kQ6H+mdNeY8b7GqWB3Wk=; b=BNx+83oRyjinRc2UIa5jLyZiWS7iCSu+CjoBEXoUECq5Lpr9Y6BnReCWIxdqznM76VbG +wsA/xgOiNezxAxqpzDMFUNacACxFCHemOO9rEke6GWyD5WiQgZbfl7kMLAkmU770YShz bfCXmnl62JM8xRyRvHtrdhylfgp9HE9Z5K/ryhca4cmY+vIjFuNfuQtCingsGv4QDzXEp E/Iz9bkz8fmIYp+h+NxKvOjb9eZ8Wa1+5Y4KGWgfDrFo/kArB39oK4Qi3xhLAE24WC+xN cb9BVpKfnhA+O1K25Ji/1Zy0BihZ20xm7NKfWaQDJqtGiZkHEMggflWkS8DjJQJrcag== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.com; spf=pass smtp.mailfrom=amazon.de; dkim=pass header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=q9E1HJS6; arc=none smtp.remote-ip=52.35.192.45 Received: from pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com [52.35.192.45]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09CD6606A323; Sun, 29 Nov 2026 18:27:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 09CD6606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=q9E1HJS6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795976872; x=1827512872; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QsENTMfw+XgoRJeBI1z5fv7kQ6H+mdNeY8b7GqWB3Wk=; b=q9E1HJS6W+QNbqB5cUZ8cEqDLPN04cChGLpXkM5vk8sI1tdwoen+caS+ tJK0+3uPUfFigiDL9ZN+g9NvCkEOF6Lw+mP27+tEv6jxxzcMGODfhM4xm RaDmCn9Kh9FSxaJl157r4IV+kO9JbhNVicUqMDs4X8lVgm0hykJKlAYgf u6S0WijVu/uE5Fh8IOeEzH8tdZvxz7DXv9tjNTNjNfv3eGG6LdfnyzaZh sJA1yQZEvx2GC+gdef7PNy2Gij6OXTJ+h3NIkx83zzweBEwY64MFwT4O6 u5ia8ujnrU852qvGutboDR6oMZTZr5yu1exo1EWvWqoaWg2x2Wy+Fno1t w==; X-CSE-ConnectionGUID: 8636HtZxQDCfaCLguzNTFg== X-CSE-MsgGUID: mVdnIRieR06EoGvGpnKOkw== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="35140062" Received: from ip-10-5-0-115.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.0.115]) by internal-pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:27:49 +0000 Received: from EX19MTAUWB001.ant.amazon.com [205.251.233.104:31640] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.27.231:2525] with esmtp (Farcaster) id 8e510dc5-35f0-41f9-935d-4343a9480796; Sun, 29 Nov 2026 18:27:49 +0000 (UTC) X-Farcaster-Flow-ID: 8e510dc5-35f0-41f9-935d-4343a9480796 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Sun, 29 Nov 2026 18:27:48 +0000 Received: from ip-10-253-83-51.amazon.com (172.19.99.218) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Sun, 29 Nov 2026 18:27:45 +0000 From: Alexander Graf To: Greg Kroah-Hartman , Arnd Bergmann CC: The AWS Nitro Enclaves Team , , Paolo Bonzini , "Stefano Garzarella" , Stefan Hajnoczi , Marek Szyprowski , Robin Murphy , Andrew Morton , "David Hildenbrand" , Mike Rapoport , "Jonathan Corbet" , Shuah Khan , "Randy Dunlap" , , , , , , , , , Subject: [PATCH v2 10/23] nitro_enclaves: Wait for file operations before unbinding the PCI device Date: Sun, 29 Nov 2026 18:21:39 +0000 Message-ID: <260f73b637234ef5b7fcc0bd0cbc1d47183e39ec.1795965038.git.graf@amazon.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWC003.ant.amazon.com (10.13.139.252) To EX19D001UWA001.ant.amazon.com (10.13.138.214) User space may hold enclave fds and /dev/nitro_enclaves fds across a sysfs unbind or a hot-unplug of the NE PCI device. Their ioctls and their release then use ne_devs.ne_pci_dev and ne_misc_dev.this_device after ne_pci_remove() freed both, or oops on the NULL pointer it leaves behind. Add ne_devs.lock, a rw_semaphore that probe and remove hold for writing and the file operations hold for reading, so that remove waits for the ones in flight. A release or a remove can hold it for minutes while the device frees a slot, so the ioctls take it with down_read_killable(). Remove then marks every enclave detached, which wakes its poll like an out-of-band event, and an ioctl on a detached enclave, or on the misc device while no device is bound, fails with -ENODEV. A detached enclave keeps its memory and CPUs on release, because the device that could confirm that they are free is gone. A second NE PCI function would overwrite ne_devs.ne_pci_dev at probe, so refuse it with -EBUSY. Kiro, an AI coding assistant, found this while checking device removal with enclave fds still open. Fixes: 38907e124088 ("nitro_enclaves: Add logic for creating an enclave VM") Signed-off-by: Alexander Graf --- drivers/virt/nitro_enclaves/ne_misc_dev.c | 93 +++++++++++++++++++---- drivers/virt/nitro_enclaves/ne_misc_dev.h | 8 ++ drivers/virt/nitro_enclaves/ne_pci_dev.c | 62 +++++++++++++++ 3 files changed, 148 insertions(+), 15 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index 049ea089d0cb4..5cf51995cf9a3 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -78,6 +78,7 @@ static struct miscdevice ne_misc_dev = { struct ne_devs ne_devs = { .ne_misc_dev = &ne_misc_dev, + .lock = __RWSEM_INITIALIZER(ne_devs.lock), }; /* @@ -147,18 +148,22 @@ struct ne_phys_contig_mem_regions { */ static bool ne_check_enclaves_created(void) { - struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; + struct ne_pci_dev *ne_pci_dev = NULL; bool ret = false; - if (!ne_pci_dev) - return ret; + down_read(&ne_devs.lock); - mutex_lock(&ne_pci_dev->enclaves_list_mutex); + 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; + if (!list_empty(&ne_pci_dev->enclaves_list)) + ret = true; - mutex_unlock(&ne_pci_dev->enclaves_list_mutex); + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); + } + + up_read(&ne_devs.lock); return ret; } @@ -1181,17 +1186,18 @@ static int ne_start_enclave_ioctl(struct ne_enclave *ne_enclave, } /** - * ne_enclave_ioctl() - Ioctl function provided by the enclave file. + * ne_enclave_do_ioctl() - Ioctl function provided by the enclave file. * @file: File associated with this ioctl function. * @cmd: The command that is set for the ioctl call. * @arg: The argument that is provided for the ioctl call. * - * Context: Process context. + * Context: Process context. This function is called with ne_devs.lock held + * for reading, on an enclave that is not detached. * Return: * * 0 on success. * * Negative return value on failure. */ -static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +static long ne_enclave_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { struct ne_enclave *ne_enclave = file->private_data; @@ -1438,6 +1444,36 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long return 0; } +/** + * ne_enclave_ioctl() - Ioctl function provided by the enclave file. + * @file: File associated with this ioctl function. + * @cmd: The command that is set for the ioctl call. + * @arg: The argument that is provided for the ioctl call. + * + * Context: Process context. + * Return: + * * 0 on success. + * * Negative return value on failure. + */ +static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct ne_enclave *ne_enclave = file->private_data; + long rc; + + rc = down_read_killable(&ne_devs.lock); + if (rc) + return rc; + + if (ne_enclave->detached) + rc = -ENODEV; + else + rc = ne_enclave_do_ioctl(file, cmd, arg); + + up_read(&ne_devs.lock); + + return rc; +} + /** * ne_enclave_remove_all_mem_region_entries() - Remove all memory region entries * from the enclave data structure. @@ -1590,7 +1626,7 @@ static void ne_enclave_free(struct ne_enclave *ne_enclave) static int ne_enclave_release(struct inode *inode, struct file *file) { struct ne_enclave *ne_enclave = file->private_data; - struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; + struct ne_pci_dev *ne_pci_dev = NULL; int rc; if (!ne_enclave) @@ -1603,6 +1639,21 @@ static int ne_enclave_release(struct inode *inode, struct file *file) if (!ne_enclave->slot_uid) return 0; + down_read(&ne_devs.lock); + + /* + * The device the enclave was created on is gone, and with it the only + * party that can confirm that the slot no longer uses its memory and + * its CPUs, so the enclave keeps both. + */ + if (ne_enclave->detached) { + rc = -ENODEV; + + goto unlock_devs; + } + + ne_pci_dev = ne_devs.ne_pci_dev; + /* * Acquire the enclave list mutex before the enclave mutex * in order to avoid deadlocks with @ref ne_event_work_handler. @@ -1625,6 +1676,9 @@ static int ne_enclave_release(struct inode *inode, struct file *file) mutex_unlock(&ne_pci_dev->enclaves_list_mutex); +unlock_devs: + up_read(&ne_devs.lock); + return rc; } @@ -1822,16 +1876,25 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { switch (cmd) { case NE_CREATE_VM: { - struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; + struct ne_pci_dev *ne_pci_dev = NULL; u64 __user *slot_uid = (void __user *)arg; int rc; - rc = mutex_lock_killable(&ne_pci_dev->enclaves_list_mutex); + rc = down_read_killable(&ne_devs.lock); if (rc) return rc; - rc = ne_create_vm_ioctl(ne_pci_dev, slot_uid); - mutex_unlock(&ne_pci_dev->enclaves_list_mutex); + rc = -ENODEV; + ne_pci_dev = ne_devs.ne_pci_dev; + if (ne_pci_dev) { + rc = mutex_lock_killable(&ne_pci_dev->enclaves_list_mutex); + if (!rc) { + rc = ne_create_vm_ioctl(ne_pci_dev, slot_uid); + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); + } + } + + up_read(&ne_devs.lock); return rc; } diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.h b/drivers/virt/nitro_enclaves/ne_misc_dev.h index 2a4d2224babac..4031d5d23eb00 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.h +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "ne_pci_dev.h" @@ -35,6 +36,8 @@ struct ne_mem_region { /** * struct ne_enclave - Per-enclave data used for enclave lifetime management. * @enclave_info_mutex : Mutex for accessing this internal state. + * @detached: The NE PCI device the enclave was created on is + * gone. Written with ne_devs.lock held for writing. * @enclave_list_entry : Entry in the list of created enclaves. * @eventq: Wait queue used for out-of-band event notifications * triggered from the PCI device event handler to @@ -63,6 +66,7 @@ struct ne_mem_region { */ struct ne_enclave { struct mutex enclave_info_mutex; + bool detached; struct list_head enclave_list_entry; wait_queue_head_t eventq; bool has_event; @@ -97,10 +101,14 @@ enum ne_state { * struct ne_devs - Data structure to keep refs to the NE misc and PCI devices. * @ne_misc_dev: Nitro Enclaves misc device. * @ne_pci_dev : Nitro Enclaves PCI device. + * @lock: Held for writing while the NE PCI device binds or unbinds, and for + * reading by the file operations that reach it or log through the + * misc device, which goes away with it. */ struct ne_devs { struct miscdevice *ne_misc_dev; struct ne_pci_dev *ne_pci_dev; + struct rw_semaphore lock; }; /* Nitro Enclaves (NE) data structure for keeping refs to the NE misc and PCI devices. */ diff --git a/drivers/virt/nitro_enclaves/ne_pci_dev.c b/drivers/virt/nitro_enclaves/ne_pci_dev.c index d2c430d10758f..cd8e934e968c3 100644 --- a/drivers/virt/nitro_enclaves/ne_pci_dev.c +++ b/drivers/virt/nitro_enclaves/ne_pci_dev.c @@ -574,6 +574,38 @@ static void ne_pci_dev_disable(struct pci_dev *pdev) dev_err(&pdev->dev, "Error in pci dev disable cmd\n"); } +/** + * ne_pci_dev_detach_enclaves() - Cut the enclaves off the NE PCI device that + * goes away. + * @ne_pci_dev: Private data associated with the PCI device. + * + * Context: Process context. This function is called with ne_devs.lock held + * for writing. + */ +static void ne_pci_dev_detach_enclaves(struct ne_pci_dev *ne_pci_dev) +{ + struct ne_enclave *ne_enclave = NULL; + struct ne_enclave *ne_enclave_tmp = NULL; + + mutex_lock(&ne_pci_dev->enclaves_list_mutex); + + list_for_each_entry_safe(ne_enclave, ne_enclave_tmp, + &ne_pci_dev->enclaves_list, enclave_list_entry) { + list_del(&ne_enclave->enclave_list_entry); + + mutex_lock(&ne_enclave->enclave_info_mutex); + + ne_enclave->detached = true; + ne_enclave->has_event = true; + + wake_up_interruptible(&ne_enclave->eventq); + + mutex_unlock(&ne_enclave->enclave_info_mutex); + } + + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); +} + /** * ne_pci_probe() - Probe function for the NE PCI device. * @pdev: PCI device to match with the NE PCI driver. @@ -643,6 +675,20 @@ static int ne_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) goto teardown_msix; } + down_write(&ne_devs.lock); + + /* + * The driver keeps a single device in ne_devs, and the enclaves and the + * misc device belong to it. + */ + if (ne_devs.ne_pci_dev) { + dev_err(&pdev->dev, "Another NE PCI device is bound already\n"); + + rc = -EBUSY; + + goto unlock_ne_devs; + } + ne_devs.ne_pci_dev = ne_pci_dev; rc = misc_register(ne_devs.ne_misc_dev); @@ -652,10 +698,14 @@ static int ne_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) goto disable_ne_pci_dev; } + up_write(&ne_devs.lock); + return 0; disable_ne_pci_dev: ne_devs.ne_pci_dev = NULL; +unlock_ne_devs: + up_write(&ne_devs.lock); ne_pci_dev_disable(pdev); teardown_msix: ne_teardown_msix(pdev); @@ -682,6 +732,10 @@ static void ne_pci_remove(struct pci_dev *pdev) { struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev); + down_write(&ne_devs.lock); + + ne_pci_dev_detach_enclaves(ne_pci_dev); + misc_deregister(ne_devs.ne_misc_dev); ne_devs.ne_pci_dev = NULL; @@ -699,6 +753,8 @@ static void ne_pci_remove(struct pci_dev *pdev) pci_disable_device(pdev); kfree(ne_pci_dev); + + up_write(&ne_devs.lock); } /** @@ -714,6 +770,10 @@ static void ne_pci_shutdown(struct pci_dev *pdev) if (!ne_pci_dev) return; + down_write(&ne_devs.lock); + + ne_pci_dev_detach_enclaves(ne_pci_dev); + misc_deregister(ne_devs.ne_misc_dev); ne_devs.ne_pci_dev = NULL; @@ -731,6 +791,8 @@ static void ne_pci_shutdown(struct pci_dev *pdev) pci_disable_device(pdev); kfree(ne_pci_dev); + + up_write(&ne_devs.lock); } /* -- 2.47.1