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=1796446485; b=TUlSx4ud32h2bZc5cEvbyT0stHYPwsO9+WM53GNJwOiiyjN7QkT+aV4YtaAa3zCH47X9 J4wPLv3N7mkmHwgLzYbujcbpLPk8ytd6XFPR8ApL3O+vqZeXOr5bn+fJ7FSg1+d+wTIu6 OQNDAGKkSop6GsnodMGoK1C8wf8/e3vTmUDn+rRqRYwkp1qt7zB6BCv0/CfAD8dfY8cwN xrbiNTG6/aQsV07SNAuC7ISsZx2/QKQyj4KrYJQSNjAJyc9R2OYEE4S2OLTEzsDhOehgR S21LdKRw51rQpn0TZ7ApiRhPf4RvuOHXLwIfB6c7qade/BYljeroDW4wcY0Paeb1RSw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796446485; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=Z3L3Aa4v/YfkALGzljdTEXquJAl8GGkh4e48ttwp9nA=; b=BoJDpEh921dq7tUo3GVFzGOn64tqI7VXDP+u5JAoFRV9UCQdEHkDLzWfYS2LSNi7TnOJ ZeulBOqXI3/sDulmrVyCkufihBtFYJl/JDiIN1ixItG9vPUdbEda/OdQub64NOtOigeuY bXwLlWNKOkauJGut1YjpfqJqx+PEpsFe3xQEihKvFV/okd2ZiHZ1lo1yqDZFnv2pTVkT9 CJdvazyjUlcFrfeG+mtogVUw52aMD4Y8HfQUFnFR7pI9QkdF1J4Q+ie9pA4aetqbFki/9 dDoPWS6aBFLyz5LphIIg4bBgRKjiTaKhxFozE6r+ITiKH6PpA35QnIVsRI+i253YYJw== 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=fvNTLXVm; 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 22290606A322; Sat, 5 Dec 2026 04:54:44 +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=fvNTLXVm Received: by smtp.kernel.org (Postfix) with ESMTPSA id AD2371F0092B; Sat, 5 Dec 2026 04:54:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796446484; bh=Z3L3Aa4v/YfkALGzljdTEXquJAl8GGkh4e48ttwp9nA=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=fvNTLXVmoga6ZwSKaWHcVp7I7I4nhwVSIGEcx6NpnjHX+zQCYTuDAjb3ytUXvySMs Hf2GK0uLx/TiSEarTiiTGRSNVFp0lGm0UsnRMKTlyWewupOCAbfv2PbDuT+Ta16NNR rSob0dtTNdNWdvvvB0oIyWodxWFfN547PECgUUDXLuzTRadwAX+/t9MHyqu1DWUOzG VAsv0UaHu+mXPWNeug6heKDRkmsh3Wcww9PIFfARQ8WKpjV6AV6hMw2u+YZsyzJxYa tAAkMtjUCgLr/jUwEmmcYC3IIdC6c4XaMXTQectLQcNLG/LjQRSCo8RjUSUEoHlmI/ /6JOgRCgZTaoQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v18 18/19] PCI/CXL: Flush host and device caches for CXL bus reset Reply-To: sashiko-reviews@lists.linux.dev To: "Srirangan Madhavan" , linux-cxl@vger.kernel.org, linux-pci@vger.kernel.org In-Reply-To: <20261205041453.3946529-19-smadhavan@nvidia.com> References: <20261205041453.3946529-1-smadhavan@nvidia.com> <20261205041453.3946529-19-smadhavan@nvidia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 04:54:44 +0000 X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] PCI/CXL: deferred cxl_reset_regs_offline() races with region teardown - [High] PCI/CXL: bus reset method bypasses cxl_bus when SBR is already unmasked - [Medium] PCI/CXL: cxl_restore_state_after_pci_reset() keeps -ENOTTY after disabling - [Medium] PCI/CXL: cxl_restore_state_after_pci_reset() re-enables caching on flush error - [Medium] PCI/CXL: premature unmark of regs_offline and reset_pending in bus reset - [Medium] PCI/CXL: cxl_port bind/unbind races with cxl_reset_bus_function() - [Medium] PCI/CXL: redundant host cache flush on aborted CXL bus reset -- commit 788953fa702e705049c3ab36585d1074d884a918 Author: Srirangan Madhavan PCI/CXL: Flush host and device caches for CXL bus reset This commit adds host and device cache maintenance to the CXL bus reset path for devices with cached HDM state, holding the CXL region lock only during the prepare, offline, restore, and unmark windows while running cache flushes and the bus reset unlocked. > diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c > index 0d5225e8141b2..a86d56b5879b8 100644 > --- a/drivers/pci/cxl.c > +++ b/drivers/pci/cxl.c [ ... ] > @@ -495,7 +505,9 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev, > =20 > cxl_region_quiesce_lock(); > rc =3D cxl_hdm_cache_copy_decoders(&pdev->hdm, snapshot); > - if (!rc) > + if (rc) > + pci_err(pdev, "failed to refresh cached HDM decoders: %d\n", rc); > + else > rc =3D pci_cxl_restore_state(pdev, snapshot, command); > /* The registers are usable again; later teardowns write them. */ > cxl_hdm_cache_set_regs_offline(pdev, false); [Severity: Medium] Can a concurrent cxl_port bind or unbind race with the unlocked windows of cxl_reset_bus_function() and get clobbered by stale snapshot state? cxl_port_driver allows sysfs bind and unbind under device_lock(&port->dev) alone, without taking device_lock(&pdev->dev) or cxl_rwsem.region and without checking reset_pending or regs_offline: CPU0 (cxl_reset_bus_function) CPU1 (cxl_port unbind) pci_dev_save_and_disable() PCI_COMMAND MSE =3D 0 pci_cxl_reset_prepare() snapshot global_ctrl, dvsec_ctrl unlock cxl_rwsem.region cxl_reset_cache_prepare() disable_hdm() readl/writel(HDM ctrl) <-- MSE=3D0 clear_mem_enable() clear MEM_ENABLE in HW & cache SBR cxl_reset_hdm_restore() cxl_hdm_cache_copy_decoders() pci_cxl_restore_state() <-- writes stale global_ctrl/dvsec_ctrl Because cxl_reset_hdm_restore() only refreshes per-decoder settings from pdev->hdm, pci_cxl_restore_state() writes back pre-reset global_ctrl and dvsec_ctrl, re-enabling HDM and CXL.mem after unbind. In addition, disable_hdm() reads 0xffffffff while MSE is 0 or during SBR and writes 0xfffffffd back to CXL_HDM_DECODER_CTRL_OFFSET. If cxl_set_mem_enable() runs between cxl_restore_dvsec_ctrl() and cxl_reset_enable_cache(), its read-modify-write on PCI_DVSEC_CXL_CTRL can also cache or write back a value with PCI_DVSEC_CXL_CACHE_ENABLE cleared. [ ... ] > + /* Complete host invalidation before ending IOMMU exclusion. */ > + if (reset_attempted || cache->cache_disabled) { > + rc2 =3D cxl_hdm_quiesce_flush(&cache->quiesce); > + if (rc2) > + pci_err(pdev, > + "failed to invalidate CPU cache after CXL bus reset: %d\n", > + rc2); > + } [Severity: Medium] Why run a second host cache invalidation when reset_attempted is false and cache->cache_disabled is true? cxl_reset_cache_prepare() already flushed the host caches before setting cache->cache_disabled, so on an aborted reset (such as cxl_sbr_masked() remaining true) this triggers a redundant wbinvd_on_all_cpus() per HDM range on x86. If that unnecessary flush fails, rc2 also causes cxl_reset_save_disabled_state() to disable the device and poison its saved PCI_COMMAND. The sibling path in cxl_reset_execute() skips this second flush when attempted is false; gating it on reset_attempted alone avoids both problems. > + cxl_reset_hdm_unmark(pdev); [Severity: Medium] Does cxl_reset_hdm_unmark() clear regs_offline and reset_pending too early, and should they stay set when recovery fails and disables the device? cxl_decoder_commit() and cxl_decoder_reset() hold only cxl_rwsem.region and not device_lock(&pdev->dev), so they rely on these flags to avoid touching disabled HDM MMIO. On failure paths, cxl_reset_hdm_restore() clears regs_offline even when cxl_hdm_cache_copy_decoders() or pci_cxl_restore_state() fails, and cxl_reset_hdm_unmark() clears both flags before cxl_reset_save_disabled_state() clears PCI_COMMAND_MEMORY and poisons saved_config_space. Any later cxl_decoder_reset() or cxl_decoder_commit() then issues MMIO to a disabled or unconfigured BAR. On the success path, unmarking before cxl_hdm_quiesce_destroy() and cxl_restore_command_after_pci_reset() lets a concurrent region re-commit collide with the still-held cxl_reset request_mem_region() reservation (-EBUSY). On aborts (!reset_attempted), PCI_COMMAND_MEMORY is still 0 from pci_dev_save_and_disable() until the outer pci_dev_restore() runs, so unmarking here exposes the disabled BAR to concurrent commits or teardowns. > + /* Undo preparation on abort; enable caching after reset only if safe. = */ > + if (cache->cache_disabled && (!reset_attempted || (!rc && !rc2))) > + policy_rc =3D cxl_reset_restore_cache_policy(pdev, snapshot->dvsec, > + cache->saved_ctrl2); [Severity: Medium] On an aborted reset (!reset_attempted), can this re-enable device caching even when cxl_hdm_quiesce_flush() failed (rc2 !=3D 0)? !reset_attempted short-circuits the || (!rc && !rc2) check, so cxl_reset_restore_cache_policy() clears PCI_DVSEC_CXL_DISABLE_CACHING right before the else branch calls cxl_reset_save_disabled_state() for that same rc2 error. Because cxl_reset_save_disabled_state() only clears MSE and BME in PCI_COMMAND, device caching is left enabled on a device being disabled for failed cache maintenance. Checking cache->cache_disabled && !rc2 && (!reset_attempted || !rc) would keep caching disabled when the flush fails. > + if (reset_attempted) { > + rc =3D rc ?: rc2 ?: policy_rc; > + if (rc) > + cxl_reset_save_disabled_state(pdev); > + } else { > + /* An aborted reset alone must not poison the saved PCI command. */ > + if (rc2 || policy_rc) > + cxl_reset_save_disabled_state(pdev); > + rc =3D rc ?: rc2 ?: policy_rc; > + } [Severity: Medium] Can cxl_restore_state_after_pci_reset() still return -ENOTTY after calling cxl_reset_save_disabled_state()? __pci_reset_function_locked() treats -ENOTTY as method-unsupported and continues to the next reset method in dev->reset_methods[]. That happens here after an attempted reset if pci_bridge_wait_for_secondary_bus() returns -ENOTTY when the link fails to train, and on an aborted cxl_sbr_masked() reset (rc =3D=3D -ENOTTY) if cleanup fails with rc2 or policy_rc. Evaluating rc =3D rc ?: rc2 ?: policy_rc preserves -ENOTTY and masks the cleanup error, so if another reset method follows cxl_bus, PCI core runs it on a device whose HDM state was unrestored and whose saved PCI_COMMAND was overwritten with PCI_COMMAND_INTX_DISABLE. Returning a non-ENOTTY error (such as rc2 ?: policy_rc ?: -EIO) whenever cxl_reset_save_disabled_state() runs would prevent that fallthrough. [ ... ] > diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c > index 3d9647c110dda..9fd15fdb38727 100644 > --- a/drivers/pci/pci.c > +++ b/drivers/pci/pci.c > @@ -5060,9 +5060,11 @@ static int pci_reset_bus_function(struct pci_dev *= dev, bool probe) > =20 > static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) > { > + struct cxl_pci_cache *cache; > struct pci_dev *bridge; > u16 dvsec, reg, val; > u16 command; > + bool attempted =3D false; > int rc; [Severity: High] When an upstream CXL port already has PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR set, does the plain bus reset method preempt cxl_bus and bypass all CXL cache maintenance and HDM restoration? pci_reset_fn_methods[] orders pci_reset_bus_function ("bus") ahead of cxl_reset_bus_function ("cxl_bus"), and pci_reset_bus_function() only returns -ENOTTY for a CXL downstream device when cxl_sbr_masked(bridge) is true. When UNMASK_SBR is already 1, pci_init_reset_methods() puts bus ahead of cxl_bus, so resetting a Type 2 device with cached HDM state runs plain SBR without flushing host caches, disabling or writing back device caches, or restoring HDM decoders, causing dirty cache data loss and broken HDM decode state. [ ... ] > @@ -5112,14 +5125,26 @@ static int cxl_reset_bus_function(struct pci_dev = *dev, bool probe) > val); > } > =20 > - rc =3D pci_reset_bus_function(dev, probe); > + if (cxl_sbr_masked(bridge)) { > + /* The unmask did not take effect, so SBR would do nothing. */ > + rc =3D -ENOTTY; > + } else { > + if (snapshot) > + cxl_reset_regs_offline(dev); > + rc =3D pci_reset_bus_function(dev, probe); > + /* > + * A bus reset method was verified above, so any result, even > + * -ENOTTY from the wait for the bus, may follow a reset. > + */ > + attempted =3D true; > + } [Severity: High] Does deferring cxl_reset_regs_offline() until right before pci_reset_bus_function() allow a concurrent CXL region teardown to access HDM MMIO while PCI_COMMAND_MEMORY is already cleared? pci_dev_save_and_disable() clears PCI_COMMAND_MEMORY before cxl_reset_bus_function() runs. Because cxl_reset_hdm_prepare() leaves regs_offline false and drops cxl_rwsem.region, a concurrent commit_store() teardown can run without device_lock(&pdev->dev) during the unlocked cache-prepare, pending-transaction wait, or IOMMU-prepare window: CPU0 (cxl_reset_bus_function) CPU1 (commit_store teardown) pci_dev_save_and_disable() PCI_COMMAND MSE =3D 0 pci_cxl_reset_prepare() reset_pending =3D true unlock cxl_rwsem.region cxl_reset_cache_prepare() lock cxl_rwsem.region cxl_region_decode_reset() cxl_decoder_reset() !cxl_decoder_regs_offline() readl/writel(HDM) <-- MSE=3D0, UR clear F_ENABLE & pdev->hdm unlock cxl_rwsem.region prepare fails / cxl_sbr_masked() cxl_restore_state_after_pci_reset() !reset_attempted -> skip restore pci_dev_restore() <-- MSE=3D1; HW decoder still committed Hardware rejects the uncommit writes with PCIe Unsupported Requests while cxl_decoder_reset() marks the decoder disabled in software and pdev->hdm. If the reset then aborts before attempted is set, pci_dev_restore() re-enables MSE with the hardware decoder still committed to an HPA range CXL core considers free, risking HPA decode aliasing and silent memory corruption. Setting regs_offline in cxl_reset_hdm_prepare() avoids the MSE=3D0 MMIO accesses, though the abort path would also need to reconcile any decoders torn down during prepare against hardware before restoring PCI_COMMAND. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205041453.3946= 529-1-smadhavan@nvidia.com?part=3D18