From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=35.83.148.184 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795976662; b=OUS3l0qQq7Kk28qMdoAB3U8e5jlgJHwNgaRsXKbr6RD2doQceeOjx+HQaMtOAtqKALXD T3/oK0I54eTpNQ9uGfqITRjqnDbjXfUV2dDzzLJP3GMPzQScTn4zlXMsvioXParLJGatQ 2PSK4YqrcHxOp+wq4zJQhKvTgu38Z3R+zkrnhqOpW3pFHPPhGMUOOQtRG6qsseyr488cS zBlPOWJe4DhHY6Le8PJvaGwytn+V4G7XqUu2JOFS72ZRXzWdb0S5uG1ym06zKrkDyNPzG ECtnjR/yYUPXlG5qo0HJoYxM7V6RvyCERGwH/QDVc+r0vU41L5Pa11rYtraQbLfK2pw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795976662; 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=XuX/3/giu9LqSwNPdFGYWM4bNT3JHSi+jBgjppe+Rzs=; b=4Ei2IM7BUbEsc7Sfvg1Mp5CJjbR2yy78sv1VaHFAP9PKyBVExRv8q8c1QakZlxOHwMVi E1F7vM734GH/MCVYK2MTSRnz46F8cDxY/5sIZGAsLIZd/1ewU1iXmrJbu20Fa3VuHXGUA RnMzD0bwfRJm72+PSykORgyEhno4hZ6Vekj0Ff9kQuA4jMDzLTND/3s2xI1ggrs3OJBXg yplm1GZ0ISrbcPThElLm1Wu0nRJGa6AcqwJ4m1jiLZ3w1Iq/Hw/iAVOleTEuvdFVmQIKi ZxzG3fiBCQytW3IYP4fC0xbyRTFcQQ+s819CvpJO3fTGJVIoi0wkI8o+JW1yHLmWk0w== 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=f8x0EbvV; arc=none smtp.remote-ip=35.83.148.184 Received: from pdx-out-014.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-014.esa.us-west-2.outbound.mail-perimeter.amazon.com [35.83.148.184]) (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 412B1606A323; Sun, 29 Nov 2026 18:24:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 412B1606A323 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=f8x0EbvV DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795976661; x=1827512661; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XuX/3/giu9LqSwNPdFGYWM4bNT3JHSi+jBgjppe+Rzs=; b=f8x0EbvVVcZMhyiRU9pmqKjLsauvEu+m1caiAhv+ulJ+dv5S0WhNC1hI jmFbrJQV2t0JtxyqPih+EVKWylGi9dk8chVcI6eM4WkWh5ZgE88mm/qE3 Leqc68e9qSA77kautecfTAoMvbldCmfXsEqPrB9QMObtAtuOvxzzDBBi9 oSpRBzA1GcdQVylrHPmYsvn4msg4OuwacNMnWjoRMmhR4n25uhEUY8NDy nqCmwTARRu6HaWdP6yXqHXN5XClr4sZcsI7U4YGt+EV4Ns6Tr69Znw3Fi YAzulu7M3qVWeH4z7rbqlfHyy2krSk62NehkF66Oz3AryQ4wzGFLzkTLg w==; X-CSE-ConnectionGUID: V1gSqQjeSsO84KM+ygSRXg== X-CSE-MsgGUID: Z9avz9vXT9OPFqldlNA3/g== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="35204433" 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-014.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:24:18 +0000 Received: from EX19MTAUWB001.ant.amazon.com [205.251.233.104:7700] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.6.41:2525] with esmtp (Farcaster) id 40d382aa-9233-47ec-bc82-dfabf79077d1; Sun, 29 Nov 2026 18:24:18 +0000 (UTC) X-Farcaster-Flow-ID: 40d382aa-9233-47ec-bc82-dfabf79077d1 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:24:18 +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:24:14 +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 04/23] nitro_enclaves: Split freeing the slot from freeing the enclave Date: Sun, 29 Nov 2026 18:21:33 +0000 Message-ID: X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-Mailing-List: linux-doc@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: EX19D031UWA002.ant.amazon.com (10.13.139.96) To EX19D001UWA001.ant.amazon.com (10.13.138.214) Under rare circumstances, the nitro_enclaves device may turn down an ENCLAVE_STOP or SLOT_FREE command. In preparation to handle that case, split ne_enclave_release() into ne_enclave_free_slot() and ne_enclave_free(). ne_enclave_free_slot() asks the device to stop the enclave and to free its slot. ne_enclave_free() drops the references on the memory region pages, puts the enclave's threads back into the NE CPU pool and frees the enclave. It only runs once the device has confirmed that it no longer owns what the enclave was given, which is the default behavior today. With the two apart, all donated Enclave resources like memory and CPUs can stay tracked as in-use when the device turns a command down, for as long as the device may still use them. Signed-off-by: Alexander Graf --- drivers/virt/nitro_enclaves/ne_misc_dev.c | 111 ++++++++++++++-------- 1 file changed, 74 insertions(+), 37 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index bb16a35f82fc0..1dd77134e3ccc 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -1386,7 +1386,8 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long * from the enclave data structure. * @ne_enclave : Private data associated with the current enclave. * - * Context: Process context. This function is called with the ne_enclave mutex held. + * Context: Process context. This function is called on an enclave that is on + * no list and has no file left. */ static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_enclave) { @@ -1413,7 +1414,8 @@ static void ne_enclave_remove_all_mem_region_entries(struct ne_enclave *ne_encla * the enclave data structure. * @ne_enclave : Private data associated with the current enclave. * - * Context: Process context. This function is called with the ne_enclave mutex held. + * Context: Process context. This function is called on an enclave that is on + * no list and has no file left. */ static void ne_enclave_remove_all_vcpu_id_entries(struct ne_enclave *ne_enclave) { @@ -1464,41 +1466,24 @@ static void ne_pci_dev_remove_enclave_entry(struct ne_enclave *ne_enclave, } /** - * ne_enclave_release() - Release function provided by the enclave file. - * @inode: Inode associated with this file release function. - * @file: File associated with this release function. + * ne_enclave_free_slot() - Stop an enclave and free its slot in the device. + * @ne_pci_dev : Private data associated with the PCI device. + * @ne_enclave : Private data associated with the current enclave. * - * Context: Process context. + * Context: Process context. This function is called with the ne_pci_dev enclave + * mutex and the ne_enclave mutex held. * Return: - * * 0 on success. + * * 0 once the device has freed the slot. * * Negative return value on failure. */ -static int ne_enclave_release(struct inode *inode, struct file *file) +static int ne_enclave_free_slot(struct ne_pci_dev *ne_pci_dev, + struct ne_enclave *ne_enclave) { struct ne_pci_dev_cmd_reply cmd_reply = {}; struct enclave_stop_req enclave_stop_request = {}; - struct ne_enclave *ne_enclave = file->private_data; - struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; - struct pci_dev *pdev = ne_pci_dev->pdev; - int rc = -EINVAL; struct slot_free_req slot_free_req = {}; - - if (!ne_enclave) - return 0; - - /* - * Early exit in case there is an error in the enclave creation logic - * and fput() is called on the cleanup path. - */ - if (!ne_enclave->slot_uid) - return 0; - - /* - * Acquire the enclave list mutex before the enclave mutex - * in order to avoid deadlocks with @ref ne_event_work_handler. - */ - mutex_lock(&ne_pci_dev->enclaves_list_mutex); - mutex_lock(&ne_enclave->enclave_info_mutex); + struct pci_dev *pdev = ne_pci_dev->pdev; + int rc; if (ne_enclave->state != NE_STATE_INIT && ne_enclave->state != NE_STATE_STOPPED) { enclave_stop_request.slot_uid = ne_enclave->slot_uid; @@ -1510,7 +1495,7 @@ static int ne_enclave_release(struct inode *inode, struct file *file) dev_err_ratelimited(ne_misc_dev.this_device, "Error in enclave stop [rc=%d]\n", rc); - goto unlock_mutex; + return rc; } memset(&cmd_reply, 0, sizeof(cmd_reply)); @@ -1525,22 +1510,74 @@ static int ne_enclave_release(struct inode *inode, struct file *file) dev_err_ratelimited(ne_misc_dev.this_device, "Error in slot free [rc=%d]\n", rc); - goto unlock_mutex; + return rc; } - ne_pci_dev_remove_enclave_entry(ne_enclave, ne_pci_dev); + return 0; +} + +/** + * ne_enclave_free() - Give back what an enclave was given and free it. + * @ne_enclave : Private data associated with the current enclave. + * + * Context: Process context. This function is called once the device has freed + * the slot of the enclave, on an enclave that is on no list and has no + * file left. + */ +static void ne_enclave_free(struct ne_enclave *ne_enclave) +{ ne_enclave_remove_all_mem_region_entries(ne_enclave); ne_enclave_remove_all_vcpu_id_entries(ne_enclave); - mutex_unlock(&ne_enclave->enclave_info_mutex); - mutex_unlock(&ne_pci_dev->enclaves_list_mutex); - kfree(ne_enclave); +} - return 0; +/** + * ne_enclave_release() - Release function provided by the enclave file. + * @inode: Inode associated with this file release function. + * @file: File associated with this release function. + * + * Context: Process context. + * Return: + * * 0 on success. + * * Negative return value on failure. + */ +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; + int rc; + + if (!ne_enclave) + return 0; + + /* + * Early exit in case there is an error in the enclave creation logic + * and fput() is called on the cleanup path. + */ + if (!ne_enclave->slot_uid) + return 0; + + /* + * Acquire the enclave list mutex before the enclave mutex + * in order to avoid deadlocks with @ref ne_event_work_handler. + */ + mutex_lock(&ne_pci_dev->enclaves_list_mutex); + mutex_lock(&ne_enclave->enclave_info_mutex); + + /* + * A slot the device did not free may still run on the memory and the + * CPUs of the enclave, so the enclave keeps them. + */ + rc = ne_enclave_free_slot(ne_pci_dev, ne_enclave); + if (!rc) + ne_pci_dev_remove_enclave_entry(ne_enclave, ne_pci_dev); -unlock_mutex: mutex_unlock(&ne_enclave->enclave_info_mutex); + + if (!rc) + ne_enclave_free(ne_enclave); + mutex_unlock(&ne_pci_dev->enclaves_list_mutex); return rc; -- 2.47.1