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 --- 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 #include +#include #include #include #include @@ -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