mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [bug report] nvme-pci: NVMe lost after S3 resume, CSTS reads all-ones for a few 100 ms
@ 2026-11-15  2:24 Moritz Hiller
  2026-11-20 18:08 ` Christoph Hellwig
  0 siblings, 1 reply; 3+ messages in thread
From: Moritz Hiller @ 2026-11-15  2:24 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg
  Cc: Bjorn Helgaas, Rafael J. Wysocki, linux-nvme, linux-pci,
	linux-pm, linux-kernel

Hi,

on my desktop the root NVMe drive sporadically does not come back after
S3 resume (roughly 5-10% of resumes in automated runs):

  nvme nvme0: Disabling device after reset failure: -19

The single readl(CSTS) in nvme_pci_enable() returns all-ones about
200 ms after resume. The controller is not dead, it just needs a few
hundred ms more. A small wait loop (diff below) fixes it here, tested
on CachyOS kernels and on vanilla v7.4-rc2. I am not asking for the
diff to be merged as is, I would like to know where this should be
fixed properly.

System:
- ASUS ROG STRIX X570-F GAMING (BIOS 5044), Ryzen 7 5800X, S3 (deep)
- Samsung 9100 PRO 2TB [144d:a810], FW 0B2QNXH7, at 0000:01:00.0,
  directly behind root port 0000:00:01.1 [1022:1483]

Observations:
- pci_pm_resume_noirq takes ~0.3 ms for the SSD, config space answers.
- At the failing CSTS read config space is intact, the link is up
  (16 GT/s x4), the device is in D0, no AER errors:

  vendor=144d cmd=0006 (saved 0002) bar0=fcf00004 (saved fcf00004)
  pmcsr=0008 lnksta=1044 rp_lnksta=3044

- sysfs remove + rescan brings the drive back after ~3 s.
- ASPM is off on this link, pcie_aspm=off and
  nvme_core.default_ps_max_latency_us=0 make no difference.
- Unpatched, 2 of 15 and 2 of 24 cycles failed in automated runs
  (90 s sleep, RTC wake). With the wait, no cycle failed (30/30 on
  CachyOS, 15/15 on v7.4-rc2); the controller answered after
  200-400 ms whenever the wait triggered.

My reading: pci_power_up() finds PMCSR already in D0 and does not
wait, and pci_bridge_wait_for_secondary_bus() only runs after D3cold
and only checks config space. So nothing waits for MMIO to come back,
and nvme_pci_enable() gives up on the first all-ones read.

The diff polls CSTS every 100 ms for up to 10 s, only if
NVME_CC_SHN_NORMAL is still set (the was_suspend test in
nvme_reset_work()) and while pci_device_is_present(). Probe and
runtime resets are unchanged. Known downsides:
- The 10 s bound is arbitrary.
- It also triggers on thaw/restore after hibernation (same bit set).
- The wait sleeps with shutdown_lock held, as nvme_wait_ready() already
  does in the same call chain.
- A controller that answers config space but is really dead after
  resume is declared dead 10 s later than today.
- A wait that times out is not logged.

Should this go into nvme-pci, the PCI core, or a quirk for this board
or drive? I can provide lspci -vvv, full dmesg or test patches.

The problem has existed on this machine for about a year, possibly
also with the previous SSD in this slot. It was hard to pin down: when
it happens the root filesystem is gone, so nothing gets written to
disk. I only got the first log of a failure in August 2026 via
netconsole, and every failure captured since then looks the same.

Disclosure: I am not a kernel developer. Log analysis, code reading
and the diff were done with an LLM assistant (Claude); I ran all tests
on my hardware. I can send patch with Signed-off-by if useful.

Thanks,
Moritz Hiller

---8<--- proof of concept, against v7.3.4 ---8<---

--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -7,6 +7,7 @@
 #include <linux/acpi.h>
 #include <linux/async.h>
 #include <linux/blkdev.h>
+#include <linux/delay.h>
 #include <linux/blk-mq-dma.h>
 #include <linux/blk-integrity.h>
 #include <linux/dmi.h>
@@ -3206,6 +3207,38 @@
 	return true;
 }
 
+/*
+ * A controller that was shut down for suspend may have lost power while the
+ * PCI core still treats it as D3hot, so nothing waited for it to come back.
+ * Config space can already answer while MMIO still reads all-ones. Give such
+ * a controller some time instead of declaring it dead on the first read.
+ */
+#define NVME_RESUME_MMIO_TIMEOUT_MS	10000
+#define NVME_RESUME_MMIO_POLL_MS	100
+
+static bool nvme_pci_wait_mmio_after_suspend(struct nvme_dev *dev)
+{
+	struct pci_dev *pdev = to_pci_dev(dev->dev);
+	unsigned int waited = 0;
+
+	/* Same test nvme_reset_work() uses to detect a resume. */
+	if (!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL))
+		return false;
+
+	while (waited < NVME_RESUME_MMIO_TIMEOUT_MS &&
+	       pci_device_is_present(pdev)) {
+		msleep(NVME_RESUME_MMIO_POLL_MS);
+		waited += NVME_RESUME_MMIO_POLL_MS;
+		if (readl(dev->bar + NVME_REG_CSTS) != -1) {
+			dev_warn(dev->ctrl.device,
+				 "CSTS read all-ones after resume, controller ready after %u ms\n",
+				 waited);
+			return true;
+		}
+	}
+	return false;
+}
+
 static int nvme_pci_enable(struct nvme_dev *dev)
 {
 	int result = -ENOMEM;
@@ -3217,7 +3250,8 @@
 
 	pci_set_master(pdev);
 
-	if (readl(dev->bar + NVME_REG_CSTS) == -1) {
+	if (readl(dev->bar + NVME_REG_CSTS) == -1 &&
+	    !nvme_pci_wait_mmio_after_suspend(dev)) {
 		dev_dbg(dev->ctrl.device, "reading CSTS register failed\n");
 		result = -ENODEV;
 		goto disable;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [bug report] nvme-pci: NVMe lost after S3 resume, CSTS reads all-ones for a few 100 ms
  2026-11-15  2:24 [bug report] nvme-pci: NVMe lost after S3 resume, CSTS reads all-ones for a few 100 ms Moritz Hiller
@ 2026-11-20 18:08 ` Christoph Hellwig
  2026-11-27 10:46   ` [PATCH] nvme-pci: wait for MMIO to come back after resume from suspend Moritz Hiller
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Hellwig @ 2026-11-20 18:08 UTC (permalink / raw)
  To: Moritz Hiller
  Cc: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg,
	Bjorn Helgaas, Rafael J. Wysocki, linux-nvme, linux-pci,
	linux-pm, linux-kernel

> +/*
> + * A controller that was shut down for suspend may have lost power while the
> + * PCI core still treats it as D3hot, so nothing waited for it to come back.
> + * Config space can already answer while MMIO still reads all-ones. Give such
> + * a controller some time instead of declaring it dead on the first read.
> + */
> +#define NVME_RESUME_MMIO_TIMEOUT_MS	10000
> +#define NVME_RESUME_MMIO_POLL_MS	100
> +
> +static bool nvme_pci_wait_mmio_after_suspend(struct nvme_dev *dev)

I'd probably rename this function and do the initial read of
NVME_REG_CSTS inside of the helper as well to keep everything in a
single place.  Otherwise this does look reasonable to me as it
automatically takes care of this issue without affecting setups
that don't have this quirk.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] nvme-pci: wait for MMIO to come back after resume from suspend
  2026-11-20 18:08 ` Christoph Hellwig
@ 2026-11-27 10:46   ` Moritz Hiller
  0 siblings, 0 replies; 3+ messages in thread
From: Moritz Hiller @ 2026-11-27 10:46 UTC (permalink / raw)
  To: Keith Busch, Jens Axboe, Christoph Hellwig, Sagi Grimberg
  Cc: Moritz Hiller, Bjorn Helgaas, Rafael J. Wysocki, linux-nvme,
	linux-pci, linux-pm, linux-kernel

On an ASUS ROG STRIX X570-F GAMING (BIOS 5044) with a Samsung 9100 PRO
attached directly to a CPU root port, the root NVMe drive sporadically
does not come back after resume from S3 (roughly 5-10% of resumes):

  nvme nvme0: Disabling device after reset failure: -19

The single readl() of CSTS in nvme_pci_enable() returns all-ones about
200 ms after resume. At that point config space answers, the link is
up, the device is in D0 and there are no AER errors. The controller is
not dead, it just needs a few hundred milliseconds longer before its
MMIO space responds.

The platform apparently removes power from the device during S3 while
the PCI core still treats it as D3hot. pci_power_up() finds PMCSR
already in D0 and does not wait, and pci_bridge_wait_for_secondary_bus()
only runs after D3cold and only checks config space, so nothing waits
for MMIO to come back.

Move the CSTS check into a helper. If CSTS reads all-ones and the
controller was shut down for suspend (NVME_CC_SHN_NORMAL still set in
ctrl_config, the same test nvme_reset_work() uses), poll CSTS every
100 ms for up to 10 s while the device is still present. Controllers
that answer on the first read, probe and other resets are not affected.

Without the patch, 4 of 39 automated S3 cycles on the machine above
failed. With it, none of 45 failed (30 on a v7.3.4 based kernel, 15 on
v7.4-rc2), and whenever the wait triggered the controller answered
after 200-400 ms. This version has been running on the same machine
for several days of normal use: 9 resumes from S3, none failed, and
the wait triggered 4 times (200-400 ms).

The failure analysis, the code reading and this patch were done with
the help of an LLM assistant (Claude). I reviewed the change and ran
all tests on my hardware.

Link: https://lore.kernel.org/r/598ee2b4-32aa-4ad3-99c7-db7f9a7e7736@pc-heini.de
Signed-off-by: Moritz Hiller <m.hiller@pc-heini.de>
---

Notes:
    Changes since the proof of concept in the bug report:
    - Renamed the helper to nvme_pci_csts_readable() and moved the initial
      CSTS read into it, so the whole check is in one place (Christoph)
    - Rebased onto nvme-7.5, sorted the linux/delay.h include
    
    Open points from the report that I left as they were:
    - The 10 s upper bound is arbitrary.
    - The wait also triggers on thaw/restore after hibernation, because the
      same bit is set there.
    - The wait sleeps with shutdown_lock held, as nvme_wait_ready() already
      does in the same call chain.
    
    Build-tested with W=1 on nvme-7.5, checkpatch --strict is clean.

 drivers/nvme/host/pci.c | 38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
index f8088d5aa..342cd0609 100644
--- a/drivers/nvme/host/pci.c
+++ b/drivers/nvme/host/pci.c
@@ -9,6 +9,7 @@
 #include <linux/blkdev.h>
 #include <linux/blk-mq-dma.h>
 #include <linux/blk-integrity.h>
+#include <linux/delay.h>
 #include <linux/dmi.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
@@ -3427,6 +3428,41 @@ static bool nvme_pci_update_nr_queues(struct nvme_dev *dev)
 	return true;
 }
 
+/*
+ * A controller that was shut down for suspend may have lost power while the
+ * PCI core still treats it as D3hot, so nothing waited for it to come back.
+ * Config space can already answer while MMIO still reads all-ones. Give such
+ * a controller some time instead of declaring it dead on the first read.
+ */
+#define NVME_RESUME_MMIO_TIMEOUT_MS	10000
+#define NVME_RESUME_MMIO_POLL_MS	100
+
+static bool nvme_pci_csts_readable(struct nvme_dev *dev)
+{
+	struct pci_dev *pdev = to_pci_dev(dev->dev);
+	unsigned int waited = 0;
+
+	if (readl(dev->bar + NVME_REG_CSTS) != -1)
+		return true;
+
+	/* Same test nvme_reset_work() uses to detect a resume. */
+	if (!(dev->ctrl.ctrl_config & NVME_CC_SHN_NORMAL))
+		return false;
+
+	while (waited < NVME_RESUME_MMIO_TIMEOUT_MS &&
+	       pci_device_is_present(pdev)) {
+		msleep(NVME_RESUME_MMIO_POLL_MS);
+		waited += NVME_RESUME_MMIO_POLL_MS;
+		if (readl(dev->bar + NVME_REG_CSTS) != -1) {
+			dev_warn(dev->ctrl.device,
+				 "CSTS read all-ones after resume, controller ready after %u ms\n",
+				 waited);
+			return true;
+		}
+	}
+	return false;
+}
+
 static int nvme_pci_enable(struct nvme_dev *dev)
 {
 	int result = -ENOMEM;
@@ -3438,7 +3474,7 @@ static int nvme_pci_enable(struct nvme_dev *dev)
 
 	pci_set_master(pdev);
 
-	if (readl(dev->bar + NVME_REG_CSTS) == -1) {
+	if (!nvme_pci_csts_readable(dev)) {
 		dev_dbg(dev->ctrl.device, "reading CSTS register failed\n");
 		result = -ENODEV;
 		goto disable;

base-commit: 524994430562e49610802a2c0c0859ce316a72f0
-- 
2.47.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-27 10:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15  2:24 [bug report] nvme-pci: NVMe lost after S3 resume, CSTS reads all-ones for a few 100 ms Moritz Hiller
2026-11-20 18:08 ` Christoph Hellwig
2026-11-27 10:46   ` [PATCH] nvme-pci: wait for MMIO to come back after resume from suspend Moritz Hiller

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®