From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=44.246.77.92 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795976767; b=pQmgyc6Brq2US6iwOwyVlCfSv6XOmoUQwXNyXsYwaN6EeoafTrbeupSpnvoFjGIvItu3 M34ZtrsH5/5y9Wvxx31lna92LOHk5x4DtzpZDg8yLVqwbXF5SbOCiYBp0mMnLEUn7pol3 IW73G3uepx0ENg2NjOXXUAkrgntzVK9RL8NSNy2/0QP2A4Y2J9+KCWWg5hs4++DaipBlL ZRFoPuEQ8uVKEZHAZrGPV5ZFK8CncBwl3nBOAofvPW1oXaiL0y2kEmtKWTfmQMsN7b2QO OR3kvCqlY5CBZ3JCZWgBRB26kooiQkrxvgruYygrf405dgp0Wx3nSGqJowvSRezi6VQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795976767; 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=xa/Bq6jq9zqEwFicGzHc9DwYx55a+r2GfXTdMQg1ZvY=; b=UQY4jtLix9ANiq63spiZ8u34oUSXLWttvrv515lvn2aELHkggNY11u/zOjW7TcEf1AM6 oQUCIx9qMGJIuXGbn9+0GM+tEo0NNJKrz2tKX3l43Z5WKg0NzFc3nbEIJKHDeMeyqD13I TE1LO52WNhxx2liwl2TMGgsD898vX5afpcyejnhz5m16a4cdEkCu8uVDAoPUL+fr5AKnh RPjUwCESGMaOIGfIQx2gcpNQ1TnsbnOUi9SVXj/tiz+cOXowVXBIUz30WhfhOw4H4BAhf NCCHGsgd6T5xaJXmLcAp6p8EoiOwJ9pXUx3F4Q4EWk7n46FdaXim86xWsop/q3If5MQ== 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=qT4gSEfR; arc=none smtp.remote-ip=44.246.77.92 Received: from pdx-out-004.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-004.esa.us-west-2.outbound.mail-perimeter.amazon.com [44.246.77.92]) (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 AFCD4606A31C; Sun, 29 Nov 2026 18:26:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AFCD4606A31C 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=qT4gSEfR DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795976766; x=1827512766; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xa/Bq6jq9zqEwFicGzHc9DwYx55a+r2GfXTdMQg1ZvY=; b=qT4gSEfR/ZlUW7aQt2LBAiDQw5isJFR6h4w3rsrOTFGe7X/M+IVIZkGE CxiJ+/vE3GS4Y5ZyUN2+kyQlTX3JQuuCZwRPBULKCFWybWwi4yUoz2IkR TfC3Tc3c5MrttU9vSupcUAYOIPZg4CpABVSKNPon4gWUK+dIpiJqkU6sw O18wTgxKdg6DpGLnd0m3mJMLWYKLPkQOLahYeu/oUNbexqgNAlOFrELgp Vk5GRsxEtYFPto637fZD1PK5UNZ19Y5cDwcELC7BaJncQxTlfiJQjY5ZY Kof0Gu0Nqm2BCrDjs/YQg9jAOHrsLz66QzM8HXtsyPKQjulN5qnYk9Rxn w==; X-CSE-ConnectionGUID: QoS62T5fSBCyWK/5l8PDYQ== X-CSE-MsgGUID: 3SUyUlCeSeSmzkdHZwoDmA== X-IronPort-AV: E=Sophos;i="6.27,241,1787011200"; d="scan'208";a="35371081" Received: from ip-10-5-6-203.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.6.203]) by internal-pdx-out-004.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Nov 2026 18:26:04 +0000 Received: from EX19MTAUWC001.ant.amazon.com [205.251.233.53:2346] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.27.231:2525] with esmtp (Farcaster) id 2c7b6034-d97a-45aa-ae9e-b889b1eb32a7; Sun, 29 Nov 2026 18:26:04 +0000 (UTC) X-Farcaster-Flow-ID: 2c7b6034-d97a-45aa-ae9e-b889b1eb32a7 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) 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:26:03 +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:26:00 +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 07/23] nitro_enclaves: Stop sending commands to a broken device Date: Sun, 29 Nov 2026 18:21:36 +0000 Message-ID: <6a29782085adb4a42be80d861f746d5fa1d14933.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: EX19D031UWA001.ant.amazon.com (10.13.139.88) To EX19D001UWA001.ant.amazon.com (10.13.138.214) The Nitro Enclaves device always answers quickly to its commands. Consider it broken if it does not reply to a command within 120s. Except for STOP/FREE commands which legitimately may take longer because the hypervisor performs cleanup work that may take some time. For those, use a 10 minute timeout. Rework the request handler logic to enforce these limits. But still keep processes killable during that time. Except for stop commands, which are used as part of the slot shutdown sequence and have to block. If a STOP/FREE command failed to respond in time (10 minutes), the device is clearly broken. Switch to a mode then where it no longer accepts any new commands. Signed-off-by: Alexander Graf --- drivers/virt/nitro_enclaves/ne_misc_dev.c | 20 ++----- drivers/virt/nitro_enclaves/ne_pci_dev.c | 63 ++++++++++++++++++----- drivers/virt/nitro_enclaves/ne_pci_dev.h | 4 ++ 3 files changed, 59 insertions(+), 28 deletions(-) diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c index b94c300b92e06..45ed6c3d89fdb 100644 --- a/drivers/virt/nitro_enclaves/ne_misc_dev.c +++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c @@ -1500,29 +1500,17 @@ static int ne_enclave_free_slot(struct ne_pci_dev *ne_pci_dev, rc = ne_do_request(pdev, ENCLAVE_STOP, &enclave_stop_request, sizeof(enclave_stop_request), &cmd_reply, sizeof(cmd_reply)); - if (rc < 0) { - dev_err_ratelimited(ne_misc_dev.this_device, - "Error in enclave stop [rc=%d]\n", rc); - + if (rc < 0) return rc; - } memset(&cmd_reply, 0, sizeof(cmd_reply)); } slot_free_req.slot_uid = ne_enclave->slot_uid; - rc = ne_do_request(pdev, SLOT_FREE, - &slot_free_req, sizeof(slot_free_req), - &cmd_reply, sizeof(cmd_reply)); - if (rc < 0) { - dev_err_ratelimited(ne_misc_dev.this_device, - "Error in slot free [rc=%d]\n", rc); - - return rc; - } - - return 0; + return ne_do_request(pdev, SLOT_FREE, + &slot_free_req, sizeof(slot_free_req), + &cmd_reply, sizeof(cmd_reply)); } /** diff --git a/drivers/virt/nitro_enclaves/ne_pci_dev.c b/drivers/virt/nitro_enclaves/ne_pci_dev.c index 199c3c7f8c42e..70fda4d29891f 100644 --- a/drivers/virt/nitro_enclaves/ne_pci_dev.c +++ b/drivers/virt/nitro_enclaves/ne_pci_dev.c @@ -46,9 +46,10 @@ #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. + * NE_FREE_TIMEOUT_MSECS - How long the driver waits for the reply to + * ENCLAVE_STOP or SLOT_FREE, and how long after a first + * -EAGAIN it sends either again. Scrubbing a large + * enclave can take minutes. */ #define NE_FREE_TIMEOUT_MSECS (600000) /* 10 min */ @@ -98,13 +99,14 @@ static void ne_retrieve_reply(struct pci_dev *pdev, struct ne_pci_dev_cmd_reply /** * ne_wait_for_reply() - Wait for a reply of a PCI device command. * @pdev: PCI device for which a reply is waited. + * @timeout_msecs: How long to wait for the reply. * * Context: Process context. This function is called with the ne_pci_dev mutex held. * Return: * * 0 on success. * * Negative return value on failure. */ -static int ne_wait_for_reply(struct pci_dev *pdev) +static int ne_wait_for_reply(struct pci_dev *pdev, unsigned int timeout_msecs) { struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev); int rc = -EINVAL; @@ -112,10 +114,13 @@ static int ne_wait_for_reply(struct pci_dev *pdev) /* * TODO: Update to _interruptible and handle interrupted wait event * e.g. -ERESTARTSYS, incoming signals + update timeout, if needed. + * + * A slot that is being scrubbed holds the reply for minutes, which is + * no hung task, so wait idle. */ - rc = wait_event_timeout(ne_pci_dev->cmd_reply_wait_q, - atomic_read(&ne_pci_dev->cmd_reply_avail) != 0, - msecs_to_jiffies(NE_DEFAULT_TIMEOUT_MSECS)); + rc = wait_event_idle_timeout(ne_pci_dev->cmd_reply_wait_q, + atomic_read(&ne_pci_dev->cmd_reply_avail) != 0, + msecs_to_jiffies(timeout_msecs)); if (!rc) return -ETIMEDOUT; @@ -179,9 +184,23 @@ int ne_do_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type, retry: /* * Use this mutex so that the PCI device handles one command request at - * a time. + * a time. A slot being freed can hold it for minutes, so let a task + * that waits for it be killed, unless the task frees a slot itself: + * release has to do that for a process that is being killed too. */ - mutex_lock(&ne_pci_dev->pci_dev_mutex); + if (ne_cmd_frees_slot(cmd_type)) { + mutex_lock(&ne_pci_dev->pci_dev_mutex); + } else { + rc = mutex_lock_killable(&ne_pci_dev->pci_dev_mutex); + if (rc) + return rc; + } + + if (ne_pci_dev->failed) { + rc = -ENODEV; + + goto unlock_mutex; + } if (deadline && time_after_eq(jiffies, deadline)) { dev_err_ratelimited(&pdev->dev, @@ -197,10 +216,18 @@ int ne_do_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type, ne_submit_request(pdev, cmd_type, cmd_request, cmd_request_size); - rc = ne_wait_for_reply(pdev); + rc = ne_wait_for_reply(pdev, ne_cmd_frees_slot(cmd_type) ? + NE_FREE_TIMEOUT_MSECS : + NE_DEFAULT_TIMEOUT_MSECS); if (rc < 0) { - dev_err_ratelimited(&pdev->dev, "Error in wait for reply for cmd type=%u [rc=%d]\n", - cmd_type, rc); + /* + * A reply does not name the command it answers, so a reply that + * comes after this would be read as the answer to the next one. + */ + ne_pci_dev->failed = true; + + dev_err(&pdev->dev, "No reply for cmd type=%u, not sending more cmds\n", + cmd_type); goto unlock_mutex; } @@ -242,6 +269,18 @@ int ne_do_request(struct pci_dev *pdev, enum ne_pci_dev_cmd_type cmd_type, rc = 0; unlock_mutex: + /* + * A device that does not stop or free a slot leaves the driver unable + * to tell what the slot still uses, so treat it like one that did not + * answer. + */ + if (rc < 0 && !ne_pci_dev->failed && ne_cmd_frees_slot(cmd_type)) { + ne_pci_dev->failed = true; + + dev_err(&pdev->dev, "Slot not freed, cmd type=%u [rc=%d], not sending more cmds\n", + cmd_type, rc); + } + mutex_unlock(&ne_pci_dev->pci_dev_mutex); return rc; diff --git a/drivers/virt/nitro_enclaves/ne_pci_dev.h b/drivers/virt/nitro_enclaves/ne_pci_dev.h index 6e9f28971a4e0..19e129d14a3a0 100644 --- a/drivers/virt/nitro_enclaves/ne_pci_dev.h +++ b/drivers/virt/nitro_enclaves/ne_pci_dev.h @@ -279,6 +279,9 @@ struct ne_pci_dev_cmd_reply { * struct ne_pci_dev - Nitro Enclaves (NE) PCI device. * @cmd_reply_avail: Variable set if a reply has been sent by the * PCI device. + * @failed: A command got no reply in time, or the device did + * not stop or free a slot. No command is sent to + * the device after that. * @cmd_reply_wait_q: Wait queue for handling command reply from the * PCI device. * @enclaves_list: List of the enclaves managed by the PCI device. @@ -294,6 +297,7 @@ struct ne_pci_dev_cmd_reply { */ struct ne_pci_dev { atomic_t cmd_reply_avail; + bool failed; wait_queue_head_t cmd_reply_wait_q; struct list_head enclaves_list; struct mutex enclaves_list_mutex; -- 2.47.1