From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=44.246.1.125 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795976729; b=sF2Z7hd0uzxbsPpGnyX9VmMSn5IfZx4/Ksr1PtYXyNvRMIfnEv8Ud7+b+dIjYvzKy2VE OpZNIIGn2sxvDzWB6VsllpAIRiZ/URKeBjnonlAn+XBAhJhko1mTBYruaOahCoCb6M3il QcsPcAaL8IOG5cYP1hZFkj1NXnY507Tx03gnJSCX7m0HH17rJbhGmvTEVuq2Ct0GNICVf hY7M9LCRVLEN4p+UIUe/DUA1mMCHOqeq5+qmjCzCMJCYDLz2c1IUQ+pOcvwLnI8sR3ZWl yZ2qE4nqOMcUcp0OrW8oVObDxGrMzAxgFMx+D7ueaYOd7bUoTCzaD/J/iV55oxP7WZA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795976729; 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=0MW7fvVxI1hdHUojaVWa4bq0Xii6oKuuUyBpJsd/r3Y=; b=KdvPwHAPv94TSv21vAB+OUSUN9QDKkKCgs871eNuKDb4kC/q/de8+9doNnurHfr3LBMG 77LddWubo18SWyYjZ57GZmZQn2BmlxgTkPBVcB76njOnTRQo31XZVmW31xOHC+DJ3zFpI hiL04ucxIVZQgGuzY91Es+HgdqUVDHVJSGKoDnD4ywJD4+5YFPzv7s1vFrfSdqx0rhbGj h7INiJP6kVd0GvmWHpgdYzKZi2xnHs1vhVeHR8NvszDtWHV36eMn5hozTINT5RtdbQKKs R5MK731In8WEA5g0cvWU/Ac5nSJU5vszjzfi1+zbgdH29EK7iDxHCyobujPJ2ildOZQ== 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=sY4j4qc+; arc=none smtp.remote-ip=44.246.1.125 Received: from pdx-out-002.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-002.esa.us-west-2.outbound.mail-perimeter.amazon.com [44.246.1.125]) (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 79847606A323; Sun, 29 Nov 2026 18:25:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 79847606A323 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=sY4j4qc+ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795976729; x=1827512729; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0MW7fvVxI1hdHUojaVWa4bq0Xii6oKuuUyBpJsd/r3Y=; b=sY4j4qc+O+7OSfWEhqaTw43BeEl0wGE48Yfv701dZ5AseqqVmSNGzy+B PG1UySRsXqAJKHM3SXNvUBi6bmSPdrA0lurZqb8sQRAV/aoDNig+8Ba+k WR8T3C+aQ0QDG0qhQ0fZ4E24K03En8JJhVWbEX/MqzsowTU8i+YB2bgam B94HtHHtddJhTc7JqVCxpKkIIM/SXSDl+as9m7yhYfBcC1VwCxVapM9oS 6TVm+DomsVo5nMBpUJV/QrGzJV21iFGH3QfuKn4b+ktfZZ9qyuVAaIV90 yIG+8GIGTr02VXCuERVZNX8qLkst2mbycnTNC7vA2eeaTQ4iocrY9I0AY Q==; X-CSE-ConnectionGUID: g7MmWF2NSIG9GqdwGIgACA== X-CSE-MsgGUID: eOZzavODSymI2U//vwQEvA== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="35393362" 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-002.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:25:29 +0000 Received: from EX19MTAUWA001.ant.amazon.com [205.251.233.236:26199] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.48.218:2525] with esmtp (Farcaster) id 60eb98a3-84b1-4e1e-ae23-892731ba32d1; Sun, 29 Nov 2026 18:25:28 +0000 (UTC) X-Farcaster-Flow-ID: 60eb98a3-84b1-4e1e-ae23-892731ba32d1 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA001.ant.amazon.com (10.250.64.204) 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:25:28 +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:25:24 +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 06/23] nitro_enclaves: Retry a command the device asks us to retry Date: Sun, 29 Nov 2026 18:21:35 +0000 Message-ID: <912dd2023d5151105ccf4629dc7150b28faf166d.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: EX19D031UWA002.ant.amazon.com (10.13.139.96) To EX19D001UWA001.ant.amazon.com (10.13.138.214) The nitro_enclaves device may return -EAGAIN under rare circumstances, which asks the system to perform the command again. Handle it as gracefully as possible inside the kernel driver. Most commands finish almost instantly; give them a 40s timeout. But commands like free may indicate through -EAGAIN that they are still actively working on freeling a slot which can take significantly longer. Use 10 minutes for those. To ensure that user space still reacts to events during such long timeout periods, rework some of the locking ordering and use killable locks. With this patch applied, a launch or a release that runs into such a window waits for it to pass. When the device still asks for a retry once the time is up, the caller gets -EAGAIN. Signed-off-by: Alexander Graf --- drivers/virt/nitro_enclaves/ne_misc_dev.c | 28 +++++--- drivers/virt/nitro_enclaves/ne_pci_dev.c | 83 ++++++++++++++++++++++- 2 files changed, 101 insertions(+), 10 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index 1dd77134e3ccc..b94c300b92e06 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -1155,7 +1155,9 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long if (copy_from_user(&vcpu_id, (void __user *)arg, sizeof(vcpu_id))) return -EFAULT; - mutex_lock(&ne_enclave->enclave_info_mutex); + rc = mutex_lock_killable(&ne_enclave->enclave_info_mutex); + if (rc) + return rc; if (ne_enclave->state != NE_STATE_INIT) { dev_err_ratelimited(ne_misc_dev.this_device, @@ -1219,11 +1221,14 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long case NE_GET_IMAGE_LOAD_INFO: { struct ne_image_load_info image_load_info = {}; + int rc; if (copy_from_user(&image_load_info, (void __user *)arg, sizeof(image_load_info))) return -EFAULT; - mutex_lock(&ne_enclave->enclave_info_mutex); + rc = mutex_lock_killable(&ne_enclave->enclave_info_mutex); + if (rc) + return rc; if (ne_enclave->state != NE_STATE_INIT) { dev_err_ratelimited(ne_misc_dev.this_device, @@ -1267,7 +1272,9 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long return -NE_ERR_INVALID_FLAG_VALUE; } - mutex_lock(&ne_enclave->enclave_info_mutex); + rc = mutex_lock_killable(&ne_enclave->enclave_info_mutex); + if (rc) + return rc; if (ne_enclave->state != NE_STATE_INIT) { dev_err_ratelimited(ne_misc_dev.this_device, @@ -1347,7 +1354,9 @@ static long ne_enclave_ioctl(struct file *file, unsigned int cmd, unsigned long return -NE_ERR_INVALID_ENCLAVE_CID; } - mutex_lock(&ne_enclave->enclave_info_mutex); + rc = mutex_lock_killable(&ne_enclave->enclave_info_mutex); + if (rc) + return rc; if (ne_enclave->state != NE_STATE_INIT) { dev_err_ratelimited(ne_misc_dev.this_device, @@ -1777,15 +1786,18 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { switch (cmd) { case NE_CREATE_VM: { - int enclave_fd = -1; struct ne_pci_dev *ne_pci_dev = ne_devs.ne_pci_dev; u64 __user *slot_uid = (void __user *)arg; + int rc; - mutex_lock(&ne_pci_dev->enclaves_list_mutex); - enclave_fd = ne_create_vm_ioctl(ne_pci_dev, slot_uid); + rc = mutex_lock_killable(&ne_pci_dev->enclaves_list_mutex); + if (rc) + return rc; + + rc = ne_create_vm_ioctl(ne_pci_dev, slot_uid); mutex_unlock(&ne_pci_dev->enclaves_list_mutex); - return enclave_fd; + return rc; } default: diff --git a/drivers/virt/nitro_enclaves/ne_pci_dev.c b/drivers/virt/nitro_enclaves/ne_pci_dev.c index c37777bb95b49..199c3c7f8c42e 100644 --- a/drivers/virt/nitro_enclaves/ne_pci_dev.c +++ b/drivers/virt/nitro_enclaves/ne_pci_dev.c @@ -26,6 +26,32 @@ */ #define NE_DEFAULT_TIMEOUT_MSECS (120000) /* 120 sec */ +/** + * NE_CMD_RETRY_INITIAL_MSECS - Delay before the first retry of a command the + * NE PCI device asked to be sent again. + */ +#define NE_CMD_RETRY_INITIAL_MSECS (10) + +/** + * NE_CMD_RETRY_MAX_MSECS - Upper bound on the delay between two retries of a + * command the NE PCI device asked to be sent again. + */ +#define NE_CMD_RETRY_MAX_MSECS (160) + +/** + * NE_CMD_RETRY_TOTAL_MSECS - How long after its first refusal a command the + * NE PCI device asks to be sent again may still go + * out, before -EAGAIN is reported to the caller. + */ +#define NE_CMD_RETRY_TOTAL_MSECS (40000) /* 40 sec */ + +/** + * NE_FREE_TIMEOUT_MSECS - How long after a first -EAGAIN ENCLAVE_STOP and + * SLOT_FREE may still go out. Scrubbing a large enclave + * can take minutes. + */ +#define NE_FREE_TIMEOUT_MSECS (600000) /* 10 min */ + static const struct pci_device_id ne_pci_ids[] = { { PCI_DEVICE(PCI_VENDOR_ID_AMAZON, PCI_DEVICE_ID_NE) }, { 0, } @@ -96,11 +122,24 @@ static int ne_wait_for_reply(struct pci_dev *pdev) return 0; } +/** + * ne_cmd_frees_slot() - Whether a command is one of the two that free a slot. + * @cmd_type: Command type of the request. + * + * Return: true for ENCLAVE_STOP and SLOT_FREE. + */ +static bool ne_cmd_frees_slot(enum ne_pci_dev_cmd_type cmd_type) +{ + return cmd_type == ENCLAVE_STOP || cmd_type == SLOT_FREE; +} + int ne_do_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type, void *cmd_request, size_t cmd_request_size, struct ne_pci_dev_cmd_reply *cmd_reply, size_t cmd_reply_size) { + unsigned int delay_msecs = NE_CMD_RETRY_INITIAL_MSECS; struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev); + unsigned long deadline = 0; int rc = -EINVAL; if (cmd_type <= INVALID_CMD || cmd_type >= MAX_CMD) { @@ -137,12 +176,23 @@ int ne_do_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type, return -EINVAL; } +retry: /* * Use this mutex so that the PCI device handles one command request at * a time. */ mutex_lock(&ne_pci_dev->pci_dev_mutex); + if (deadline && time_after_eq(jiffies, deadline)) { + dev_err_ratelimited(&pdev->dev, + "Device asked to retry cmd type=%u, giving up\n", + cmd_type); + + rc = -EAGAIN; + + goto unlock_mutex; + } + atomic_set(&ne_pci_dev->cmd_reply_avail, 0); ne_submit_request(pdev, cmd_type, cmd_request, cmd_request_size); @@ -159,6 +209,27 @@ int ne_do_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type, atomic_set(&ne_pci_dev->cmd_reply_avail, 0); + if (cmd_reply->rc == -EAGAIN) { + /* + * The device answered, so nothing is in flight and another + * command may go out while this one waits to be sent again. + */ + mutex_unlock(&ne_pci_dev->pci_dev_mutex); + + if (!deadline) { + unsigned int budget_msecs = ne_cmd_frees_slot(cmd_type) ? + NE_FREE_TIMEOUT_MSECS : + NE_CMD_RETRY_TOTAL_MSECS; + + deadline = jiffies + msecs_to_jiffies(budget_msecs); + } + + msleep(delay_msecs); + delay_msecs = min(delay_msecs * 2, NE_CMD_RETRY_MAX_MSECS); + + goto retry; + } + if (cmd_reply->rc < 0) { rc = cmd_reply->rc; @@ -221,7 +292,14 @@ static void ne_event_work_handler(struct work_struct *work) int rc = -EINVAL; struct slot_info_req slot_info_req = {}; - mutex_lock(&ne_pci_dev->enclaves_list_mutex); + /* + * A release holds these locks for as long as the device takes to free + * the slot, which can be minutes, so wait killable: that is not + * reported as a hung task. The worker gets no signal, so the locks do + * not fail. + */ + if (mutex_lock_killable(&ne_pci_dev->enclaves_list_mutex)) + return; /* * Iterate over all enclaves registered for the Nitro Enclaves @@ -229,7 +307,8 @@ static void ne_event_work_handler(struct work_struct *work) * is corresponding to. */ list_for_each_entry(ne_enclave, &ne_pci_dev->enclaves_list, enclave_list_entry) { - mutex_lock(&ne_enclave->enclave_info_mutex); + if (mutex_lock_killable(&ne_enclave->enclave_info_mutex)) + break; /* * Enclaves that were never started cannot receive out-of-band -- 2.47.1