From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=35.155.198.111 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795976942; b=12MONk2A1CRDsqeGt2+Br7PTgWh4vjmT+vk988/d+jOdCgibRk/HRp6+r/jAWpX8Yyey sZu25QKtyUIPSb3YRKh2Fpsrr9CcRnUYQKGtfekkhztv4iXc4B5EU0p/96e818NzoypPl t09sLsDm4195p/dZWR1DPshDvzVp5fH82Fr/3yEYhQNno5nUSFcCSre30KLAYXdoAOVQ8 kQjzyRBkWaDPcertwy0TVOJnsFJClNhoBabku28eSM4TSxSOxlBQDxG9PhOC0SbYsSDha myC+Bt2uJtXL9R4r6uBQWMwCcYu7zd1ko/mPVuxun+iarp7oPnzBhl+PbrJhxpmwoeg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795976942; 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=3h7u79xX+G1qiw6bMM9rb079bll7MQ8tqiYwSXj3KJA=; b=zKEiMWJTGii5l6sDyUJd3naEFc4qnhasQbXuqMN9/LmrsQIfry3NHMNOuTaHUBJpuKgs vfNHJWacRUWfcHg6VvGFq2tTptrsvncjGryBVIR/pBNlNtpphroaxjCrSeAZzaqZewuKc mAMpHvMcQlt5dHlFGThZh3fQkyKGxxiCLtq78BSS6LYYQhdg7S5Xg/Wwfnk+iKCx6unyZ VScu/n1gfvup2nJ5PAkwT13Ypqtz5Zv0ARaJVG53DFOfSq2LWjuMw2IO1HcfM2AvAEjhK yG5W4AxyvPgYgCkGzbbLCWv+Z/PDUPhB/IwVA0S/wjzu1OIaVHzyA90XthedLBOKkxg== 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=JGPrYYNG; arc=none smtp.remote-ip=35.155.198.111 Received: from pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com [35.155.198.111]) (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 325D9606A32C; Sun, 29 Nov 2026 18:29:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 325D9606A32C 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=JGPrYYNG DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795976942; x=1827512942; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3h7u79xX+G1qiw6bMM9rb079bll7MQ8tqiYwSXj3KJA=; b=JGPrYYNGCIbEUSYS2hsbAVLz0EbQLC7bcnQBSfe2rn1EFoGrQq2AQ678 EiXWwVpY91cCH7nx0bwJ05tks0Ty2MMaTKyesEfqNr797IB2auGP1VDJu DGyB6oSxAS04c5B1caa4eyxMQ/kNEpKKVKX+GLLg+kF8I/d3nkYwyRgME XnMSjYHIKXoKja/7b7AK8F31Qd/N8ZHaotOpKiZhoSVeC8k9dE9vKZRmH NFOeDyD/Ewji8ypOIm0aOGTOpc7qB1czZps63EIrIDOcEob/PnbGhvF6t 6Dx9Dyr79SFzAiayUg3iMnnQvtEQ9qLtWCEHubF9oJ7AkmLdCcDGYwdGW Q==; X-CSE-ConnectionGUID: L1abw1YgQgCuW0Fh6JSEpQ== X-CSE-MsgGUID: QnYjnD02SnSy/oNS1INGng== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="35329001" Received: from ip-10-5-9-48.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.9.48]) by internal-pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:28:59 +0000 Received: from EX19MTAUWB001.ant.amazon.com [205.251.233.104:27385] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.30.201:2525] with esmtp (Farcaster) id 8529ab13-8acb-4295-b672-b011bae5fd75; Sun, 29 Nov 2026 18:28:59 +0000 (UTC) X-Farcaster-Flow-ID: 8529ab13-8acb-4295-b672-b011bae5fd75 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:28:58 +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:28:55 +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 12/23] nitro_enclaves: Do not return enclave threads to a replaced CPU pool Date: Sun, 29 Nov 2026 18:21:41 +0000 Message-ID: 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: EX19D031UWC004.ant.amazon.com (10.13.139.246) To EX19D001UWA001.ant.amazon.com (10.13.138.214) 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 and take the pool out of the driver's view before dropping it, and take it and ne_devs.lock killable, as the ioctls do. An NE_CREATE_VM that runs while the pool is being replaced then sees no pool and is refused, instead of caching the one that is going away. The CPUs of the old and the new pool change state only once the locks drop: remove_cpu() and add_cpu() take device_hotplug_lock, which ACPI hotplug holds around the probe and remove callbacks that take ne_devs.lock for writing. Fixes: 9c8eb50fe9e2 ("nitro_enclaves: Add logic for terminating an enclave") Signed-off-by: Alexander Graf --- v1 -> v2: - Drop the driver locks before onlining or offlining CPUs (Sashiko) - Refuse an NE_CREATE_VM that races a replace instead of blocking it drivers/virt/nitro_enclaves/ne_misc_dev.c | 89 ++++++++++++++--------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index 3d2817455ee25..38fbc09cf6076 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_free_core_slots() - Free an array of cpumasks that holds the threads of * full CPU cores. @@ -483,18 +452,61 @@ 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; + cpumask_var_t *threads_per_core = NULL; + unsigned int nr_cores = 0; 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(); + /* + * Take the pool out of the driver's view while the enclave list is + * still locked, so an enclave created once the locks drop sees no pool + * instead of the one that is going away. The CPUs change state only + * after the locks drop: remove_cpu() and add_cpu() take + * device_hotplug_lock, which ACPI hotplug holds around the probe and + * remove callbacks, and those take ne_devs.lock. + */ + ne_detach_cpu_pool(&threads_per_core, &nr_cores); + + if (ne_pci_dev) + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); + + up_read(&ne_devs.lock); + + if (threads_per_core) { + ne_online_core_slots(threads_per_core, nr_cores); + + ne_free_core_slots(threads_per_core, nr_cores); + } rc = ne_setup_cpu_pool(val); if (rc < 0) { @@ -517,6 +529,15 @@ static int ne_set_kernel_param(const char *val, const struct kernel_param *kp) } 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