mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexander Graf <graf@amazon.com>
To: <linux-kernel@vger.kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>, <nh-open-source@amazon.com>,
	"The AWS Nitro Enclaves Team"
	<aws-nitro-enclaves-devel@amazon.com>
Subject: [PATCH 18/19] nitro_enclaves: Time the wait for the device to turn off by the clock
Date: Thu, 12 Nov 2026 03:07:14 +0000	[thread overview]
Message-ID: <cb6490e940c12707a40355943e04a537716ecc67.1794393487.git.graf@amazon.com> (raw)
In-Reply-To: <cover.1794300000.git.graf@amazon.com>

Unbind, shutdown and the next probe give the nitro_enclaves device
NE_DEFAULT_TIMEOUT_MSECS, 120 s, to turn off. The device turns off only
once it has stopped every enclave and scrubbed its memory, which can
take as long as a single ENCLAVE_STOP. ne_pci_dev_disable() also counts
the wait in 10 ms steps of msleep_interruptible(). At HZ=100 each step
sleeps two jiffies, so the wait takes 240 s, and with a signal pending
it does not sleep at all.

Allow the device NE_FREE_TIMEOUT_MSECS, 10 minutes, as for ENCLAVE_STOP,
and poll the register with readx_poll_timeout(), which keeps a ktime
deadline.

Kiro, an AI coding assistant, found this while timing an unbind on
hardware.

Fixes: 89308c11ae3b ("nitro_enclaves: Init PCI device driver")
Signed-off-by: Alexander Graf <graf@amazon.com>
---
 drivers/virt/nitro_enclaves/ne_pci_dev.c | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/drivers/virt/nitro_enclaves/ne_pci_dev.c b/drivers/virt/nitro_enclaves/ne_pci_dev.c
index cd8e934e968c3..edc72d4f623e5 100644
--- a/drivers/virt/nitro_enclaves/ne_pci_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_pci_dev.c
@@ -9,6 +9,7 @@
 
 #include <linux/delay.h>
 #include <linux/device.h>
+#include <linux/iopoll.h>
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -550,8 +551,7 @@ static void ne_pci_dev_disable(struct pci_dev *pdev)
 {
 	u8 dev_disable_reply = 0;
 	struct ne_pci_dev *ne_pci_dev = pci_get_drvdata(pdev);
-	const unsigned int sleep_time = 10; /* 10 ms */
-	unsigned int sleep_time_count = 0;
+	int rc;
 
 	iowrite8(NE_ENABLE_OFF, ne_pci_dev->iomem_base + NE_ENABLE);
 
@@ -560,17 +560,12 @@ static void ne_pci_dev_disable(struct pci_dev *pdev)
 	 * state is not immediately set to disabled and going through a
 	 * transitory state of disabling.
 	 */
-	while (sleep_time_count < NE_DEFAULT_TIMEOUT_MSECS) {
-		dev_disable_reply = ioread8(ne_pci_dev->iomem_base + NE_ENABLE);
-		if (dev_disable_reply == NE_ENABLE_OFF)
-			return;
-
-		msleep_interruptible(sleep_time);
-		sleep_time_count += sleep_time;
-	}
-
-	dev_disable_reply = ioread8(ne_pci_dev->iomem_base + NE_ENABLE);
-	if (dev_disable_reply != NE_ENABLE_OFF)
+	rc = readx_poll_timeout(ioread8, ne_pci_dev->iomem_base + NE_ENABLE,
+				dev_disable_reply,
+				dev_disable_reply == NE_ENABLE_OFF,
+				10 * USEC_PER_MSEC,
+				NE_FREE_TIMEOUT_MSECS * USEC_PER_MSEC);
+	if (rc < 0)
 		dev_err(&pdev->dev, "Error in pci dev disable cmd\n");
 }
 
-- 
2.47.1


  parent reply	other threads:[~2026-11-12  3:12 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-11 13:49 [PATCH 00/19] nitro_enclaves: Fix enclave release, command retries and the CPU pool Alexander Graf
2026-11-11 13:49 ` [PATCH 01/19] nitro_enclaves: Release the reference on a rejected region page Alexander Graf
2026-11-11 13:49 ` [PATCH 02/19] nitro_enclaves: Split freeing the slot from freeing the enclave Alexander Graf
2026-11-11 13:49 ` [PATCH 03/19] nitro_enclaves: Ignore the reply of a failed slot info command Alexander Graf
2026-11-11 13:49 ` [PATCH 04/19] nitro_enclaves: Retry a command the device asks us to retry Alexander Graf
2026-11-11 13:49 ` [PATCH 05/19] nitro_enclaves: Stop sending commands to a broken device Alexander Graf
2026-11-11 13:49 ` [PATCH 06/19] nitro_enclaves: Set up the device state before requesting its interrupts Alexander Graf
2026-11-11 13:49 ` [PATCH 07/19] nitro_enclaves: Wait for file operations before unbinding the PCI device Alexander Graf
2026-11-12  3:07 ` [PATCH 08/19] nitro_enclaves: Let the region tracking arrays fall back to vmalloc Alexander Graf
2026-11-12  3:07 ` [PATCH 09/19] nitro_enclaves: Do not return enclave threads to a replaced CPU pool Alexander Graf
2026-11-12  3:07 ` [PATCH 10/19] nitro_enclaves: Do not report a torn down CPU pool as set Alexander Graf
2026-11-12  3:07 ` [PATCH 11/19] nitro_enclaves: Keep the CPU pool when the new CPU list does not parse Alexander Graf
2026-11-12  3:07 ` [PATCH 12/19] nitro_enclaves: Put the CPU pool back when the new CPU list is refused Alexander Graf
2026-11-12  3:07 ` [PATCH 13/19] nitro_enclaves: Initialise the CPU pool mutex statically Alexander Graf
2026-11-12  3:07 ` [PATCH 14/19] nitro_enclaves: Build the boot-time CPU pool once the CPUs are online Alexander Graf
2026-11-12  3:07 ` [PATCH 15/19] nitro_enclaves: Set up the CPU pool under the CPU hotplug read lock Alexander Graf
2026-11-12  3:07 ` [PATCH 16/19] nitro_enclaves: Let 32-bit processes use the ioctl interface Alexander Graf
2026-11-12  3:07 ` [PATCH 17/19] nitro_enclaves: Index CPU pool cores by sibling mask Alexander Graf
2026-11-12  3:07 ` Alexander Graf [this message]
2026-11-12  3:07 ` [PATCH 19/19] MAINTAINERS: Take over the Nitro Enclaves driver Alexander Graf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cb6490e940c12707a40355943e04a537716ecc67.1794393487.git.graf@amazon.com \
    --to=graf@amazon.com \
    --cc=arnd@arndb.de \
    --cc=aws-nitro-enclaves-devel@amazon.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nh-open-source@amazon.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®