From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796070606; b=sTydUX0GaKPiXLIcztsi71FBZx3QFMjGQDeF0gvq92bb8Qp8WIHsXqZfzI/QBvBusKCq trTyS7SmTGVg6XpQVC3wRTfCgL8HkO8b8KlIzYLBPI3QHXFoONguN9omMmJS5A3V9VjY0 WKuXhsP6ryiBZlMWsjiXwBTX8NGzWTnF5pmBPgvsHKnRpFdaUiSBddzNZvJVIxSQxIyeR 35MVAIcIloy/12lHPZb/rxzpK4MPKeKLS2N7/Qh4st220kP3cqGSJuv2VIHNb2PQHL21N FG4OBtHISl58G0wU54BRwzm1504tCoq9UAlkKddIGQD3fakRlW4v9dOsj3jSj3C6p4Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796070606; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=cl7LvcgBOTXSeXKgOWuNNDHfqstrKox9kzUMuc2zoFM=; b=XAwAGjBtOGmeYqPFyBRu9QiQQ+4v1mmGu4XjRXklqMQvRrl/aG/DqFmGgyrp/ZxWdmdm c4pe5FuPjrPb6k4SYDrIWHthTelVYMnJ8cz1LWp9LGcdXcKevK9jK1wHPe4PGeTEZjzOe Y5isJOb+LOgtyG26jNWvwNNf62kzpobfD7HBq67s36D93wfut8CQfUty2XOAtyHu1oiXU 6PJkmuCCsuU0s3mAQjlT/O6pAaPqSvi1S1C0xe2NK4rOebiQoKo5Mdyh/iz7mAo1N5ZBw b51TvQqe5seZ0Yr66q7QfGudOd7jc3PL6HUh0vplyPt/g9tKtZjGchxiGslJT+Wu66A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Y+UMIZ0E; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 316B0606A327; Mon, 30 Nov 2026 20:30:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Y+UMIZ0E Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B6841F00897; Mon, 30 Nov 2026 20:30:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796070605; bh=cl7LvcgBOTXSeXKgOWuNNDHfqstrKox9kzUMuc2zoFM=; h=Date:From:To:Cc:Subject:In-Reply-To; b=Y+UMIZ0E3U/MI7wNAQ4aB/VHNzJStGwLlg7UzCKy+G1LPOZ9Qc+O6H8kNd2eLVO4o WNXX2PFB8VFqey45Bd9iHrVeb0q0GxkNGo6GyO8/tjzte1Q+eC5gvtCjkhyMZiKw8V 8g4wMedh5ztqjQxL7LvQS8slBC11sgDqLOLXsf7+bCexeqAP25e5M1wSyuBsaF5vCu YLcqyqKkntcjmOJ8njxgD/MZ1pdouFyKLoVRA+dU2BJ2XM2VhoN+g8KDucNagyFRQI nDXuoVG1juaY08pfNJfWGmDtzlEoqXTWgYnzNng8I+XbUnTCf06qiTJNb5nXY7rpbq EKimrQemvB3ZA== Date: Mon, 30 Nov 2026 14:30:04 -0600 From: Bjorn Helgaas To: Moritz Hiller Cc: Keith Busch , Jens Axboe , Christoph Hellwig , Sagi Grimberg , Bjorn Helgaas , "Rafael J. Wysocki" , linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] nvme-pci: wait for MMIO to come back after resume from suspend Message-ID: <20261130203004.GA356922@bhelgaas> 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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261127104637.219891-1-m.hiller@pc-heini.de> On Fri, Nov 27, 2026 at 11:46:21AM +0100, Moritz Hiller wrote: > 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). This sounds suspiciously similar to pci_suspend_retains_context(), see https://git.kernel.org/linus/ada02d0894fd ("PCI: Add pci_suspend_retains_context() to check if device state is preserved during suspend"). But pci_bridge_wait_for_secondary_bus() waits for a PCI/PCIe device to be ready, and AFAIK the PCIe spec doesn't require any extra time after that for the device to be ready for MMIO access. Maybe this is some NVMe-specific requirement, or maybe pci_bridge_wait_for_secondary_bus() is missing something. > 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 > --- > > 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 > #include > #include > +#include > #include > #include > #include > @@ -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) I see that this is merged already, but PCI_POSSIBLE_ERROR() or similar might be appropriate and might help future readers find things related to this case. > + 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 >