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=1796446074; b=iFTS9MtET0089VMR9m1s8Jd45pv+gdsnZrVjiUU/aOlkwaZqflip7QsuwHbWHdeO92M2 7KeUgNNEirADS7odW03AjIe8QdyzG7YgU+qwG7qjdjvytqkATw66oyrYfeaf16Ib58o5Y YSNpoJM2PHa/MSwChe4x9aduyiw25w8lw4hjfv9xldcrwDwcCmG+ZhbL/qsPmRtmaNH5H SQckXX6blyGp2md06DBKcAuMk4OUTvfAcFL42PlmQeOQXF1Ut5wOWcK0/aIxbFaitcz+D 6WrzxJTF1qhimcUi3tq5c+0b7LSUtTyZzVBkqMWpWFIQLoonWanw0wk03NjMiI+cDUQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796446074; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=vtIPKpEtAuKc4cxqBVxej6RYhKOW8Qg1G1wJpcfd7JI=; b=efG/0Ff5VcDBHTck80AUQw9gfKzb7LxKRvaEMUeuvZJwuPMpnPjM1i6yzae+4ltc+XST geX6CvkUfccpU//NthheFmKGZkHnGKxGZ8sxaYwqUzX3P8WZlxp6a3bm1BCjFKw3Za5o/ mjTvYAicMSP5OD13nlmHXime/jkTcKeVj+ldpApXIQVgKGxd+3A+0yONDbrAaILDtm36q UHANAtoTlwEd4YtM7/nNXBjLwL7jj+DdzwGbe2ZE7YJPlu5i/VW9QIMVOTPTp9ST0M+gS mCbsuhaBOZZ19Yeh62LkHV5H5NElr1usFSJrBP/p0khSC7khf/znvVFIKAqokOI1lpw== 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=l1frOcWz; 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 78B98606A322; Sat, 5 Dec 2026 04:47:54 +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=l1frOcWz Received: by smtp.kernel.org (Postfix) with ESMTPSA id E54641F00936; Sat, 5 Dec 2026 04:47:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796446074; bh=vtIPKpEtAuKc4cxqBVxej6RYhKOW8Qg1G1wJpcfd7JI=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=l1frOcWz5hKSmp8i8ScjeQ79+WrefAI7tHMF5Yrs7LvkDMvZzadgL2pzLGTDT8ySN pxu5lEv/84x8gqo4n81WlfPAlNahG+o5lFVlcsdYvcAiKtJrj1yetvEYayplB7U9xK MS2FuZO0t43T4Bu4RKe/qocyZDCKgKCkABDunFssfy89iyNP/8011GZE5nw48Ukih+ RSPO6m0FvE2GLnsvfo4t0ADu/NcFTY/Nq+ntq5/tiNHHCyYWC+H5gEOuI6v3UinT3u DK91gEERhTb9R8HEELiyRMKZG90PO6vDlX/Rk+2eFlkB4XPtqeIp2qeXbgAwI3UPEH iUbwbzLL8qI1Q== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v18 17/19] PCI/CXL: Restore CXL state after 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-18-smadhavan@nvidia.com> References: <20261205041453.3946529-1-smadhavan@nvidia.com> <20261205041453.3946529-18-smadhavan@nvidia.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Sat, 05 Dec 2026 04:47:53 +0000 X-Mailing-List: linux-pci@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 4 potential issues to consider: - [High] PCI/CXL: bus reset precedes cxl_bus and bypasses CXL state restore - [Medium] PCI/CXL: cxl_restore_command_after_pci_reset() enables BME before CXL.cache - [Medium] PCI/CXL: clear HDM reset flags before cxl_reset_save_disabled_state() - [Medium] PCI/CXL: cxl_hdm_cache_copy_decoders() restores stale global/DVSEC control -- commit c319055ba82e6bf1029bb7ad4f1963f988e31965 Author: Srirangan Madhavan PCI/CXL: Restore CXL state after CXL bus reset This commit saves and restores cached CXL HDM decoder and DVSEC control state across a CXL bus reset, refreshing per-decoder settings from the live cache before restoring them. > diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c > index 497709bffff2b..139a141c0eac3 100644 > --- a/drivers/cxl/core/hdm_state.c > +++ b/drivers/cxl/core/hdm_state.c > @@ -284,6 +284,37 @@ struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct c= xl_hdm_info *const *slot) > return copy; > } > =20 > +/** > + * cxl_hdm_cache_copy_decoders() - refresh the decoders of a snapshot > + * @slot: cache pointer owned by the device > + * @dst: snapshot of the same device from cxl_hdm_cache_snapshot() > + * > + * Copy only the per-decoder settings, which region commit and teardown = keep > + * current. Leave the rest of @dst, including global and DVSEC control, = as > + * captured: a refresh during a reset may have read those registers from= a > + * device that returned all-ones. > + * > + * Return: 0 on success, -ENXIO when nothing is published, or -EINVAL wh= en > + * @dst holds a different number of decoders. > + */ > +int cxl_hdm_cache_copy_decoders(struct cxl_hdm_info *const *slot, > + struct cxl_hdm_info *dst) > +{ > + struct cxl_hdm_info *info; > + > + lockdep_assert_held_write(&cxl_rwsem.region); > + guard(rwsem_read)(&cxl_rwsem.dpa); > + info =3D *slot; > + if (!info) > + return -ENXIO; > + if (info->decoder_count !=3D dst->decoder_count) > + return -EINVAL; > + > + memcpy(dst->settings, info->settings, > + flex_array_size(info, settings, info->decoder_count)); > + return 0; > +} [Severity: Medium] Can leaving global_ctrl and dvsec_ctrl unrefreshed restore stale control bits over a concurrent cxl_port unbind or bind? cxl_port bind and unbind hold device_lock(&endpoint->dev), not device_lock(&pdev->dev) or cxl_rwsem.region, and clear_mem_enable() / disable_hdm() do not check reset_pending or regs_offline: CPU0 (PCI reset) CPU1 (cxl_port unbind) cxl_reset_hdm_prepare() snapshot pdev->hdm (MEM_ENABLE=3D1, HDM_ENABLE=3D1) ... clear_mem_enable() cxl_set_mem_enable(..., 0) clears MEM_ENABLE in HW and pdev->hdm->dvsec_ctrl cxl_reset_hdm_restore() cxl_hdm_cache_copy_decoders() copies settings[] only pci_cxl_restore_state() writes stale dvsec_ctrl <-- re-enables MEM_ENABLE after unbind Hardware is left with PCI_DVSEC_CXL_MEM_ENABLE and CXL_HDM_DECODER_ENABLE re-enabled while the endpoint is unbound and pdev->hdm records them cleared; a later cxl_port rebind then sees those bits already set and skips registering disable_hdm() and clear_mem_enable(), losing teardown ownership. Also, the comment's reason for skipping global_ctrl and dvsec_ctrl does not appear to hold, since the cache writers (cxl_set_mem_enable(), disable_hdm(), and cxl_hdm_read_state()) already filter out all-ones reads before updating pdev->hdm. Should cxl_port bind/unbind be serialized against reset and global_ctrl/dvsec_ctrl refreshed along with settings[]? > diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c > index cc2d5a4f67df5..0d5225e8141b2 100644 > --- a/drivers/pci/cxl.c > +++ b/drivers/pci/cxl.c > @@ -479,9 +479,14 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(st= ruct pci_dev *pdev, > } > =20 > /* > - * Restore window. Take the region lock again to write the HDM state bac= k. The > - * pending mark stays set: the caller does the host cache flush without = the > - * lock and then calls cxl_reset_hdm_unmark(). > + * Restore window. Take the region lock to refresh the decoder settings = in > + * @snapshot from the cache and write the HDM state back. Leave @pdev di= sabled > + * on failure. > + * > + * Refresh the decoders now rather than rely on the copy taken before the > + * reset: a region commit or teardown during the reset has updated the c= ache, > + * and restoring older decoders would leave the device decoding ranges t= he > + * CXL core considers free. > */ > static int cxl_reset_hdm_restore(struct pci_dev *pdev, > struct cxl_hdm_info *snapshot, u16 *command) > @@ -489,7 +494,9 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev, > int rc; > =20 > cxl_region_quiesce_lock(); > - rc =3D pci_cxl_restore_state(pdev, snapshot, command); > + rc =3D cxl_hdm_cache_copy_decoders(&pdev->hdm, snapshot); > + if (!rc) > + 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); > cxl_region_quiesce_unlock(); [ ... ] > +int cxl_restore_state_after_pci_reset(struct pci_dev *pdev, > + struct cxl_hdm_info *snapshot, > + u16 *command) > +{ > + int rc; > + > + device_lock_assert(&pdev->dev); > + rc =3D cxl_reset_hdm_restore(pdev, snapshot, command); > + if (rc) > + cxl_reset_save_disabled_state(pdev); > + > + return rc; > +} [Severity: Medium] Does clearing regs_offline and dropping cxl_region_quiesce_lock() before cxl_reset_save_disabled_state() open a race with concurrent region commits and teardowns on restore failure? cxl_decoder_commit() and cxl_decoder_reset() synchronize on cxl_rwsem.region and the reset flags rather than device_lock(&pdev->dev). If pci_cxl_restore_state() fails after re-enabling PCI_COMMAND_MEMORY (and similarly after commit "PCI/CXL: Flush host and device caches for CXL bus reset" adds cxl_reset_hdm_unmark() before cxl_reset_save_disabled_state()), a concurrent commit can run while the device is about to be disabled: CPU0 (reset restore error) CPU1 (region commit) cxl_reset_hdm_restore() pci_cxl_restore_state() enables PCI_COMMAND_MEMORY fails set regs_offline =3D false cxl_region_quiesce_unlock() commit_store() -> cxl_decoder_commit() programs HDM MMIO cxl_reset_save_disabled_state() clears PCI_COMMAND_MEMORY <-- disables device under live commit In addition, if cxl_hdm_cache_copy_decoders() fails before pci_cxl_restore_state() runs, regs_offline is still cleared even though BARs and PCI_COMMAND_MEMORY were never restored, allowing a concurrent cxl_decoder_reset() to access disabled HDM MMIO registers. Should cxl_reset_save_disabled_state() run before unmarking reset_pending and keep regs_offline set when restoration fails? > +int cxl_restore_command_after_pci_reset(struct pci_dev *pdev, > + const struct cxl_hdm_info *snapshot, > + u16 command) > +{ > + int rc; > + > + device_lock_assert(&pdev->dev); > + rc =3D cxl_reset_save_restored_state(pdev, command); > + if (rc) > + return rc; > + > + rc =3D cxl_reset_enable_cache(pdev, snapshot); > + if (rc) > + cxl_reset_save_disabled_state(pdev); > + > + return rc; > +} [Severity: Medium] Should cxl_reset_enable_cache() run before cxl_reset_save_restored_state() here? Calling cxl_reset_save_restored_state() first re-enables Bus Master Enable and interrupts and overwrites saved PCI state before CXL.cache is restored. If cxl_reset_enable_cache() then fails, BME has already been enabled in hardware before cxl_reset_save_disabled_state() attempts to disable it again, whereas cxl_reset_execute() enables CXL.cache first and calls cxl_reset_save_restored_state() last. > diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c > index 9ab647719c9f3..3d9647c110dda 100644 > --- a/drivers/pci/pci.c > +++ b/drivers/pci/pci.c [ ... ] > @@ -5112,7 +5118,15 @@ static int cxl_reset_bus_function(struct pci_dev *= dev, bool probe) > pci_write_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL, > reg); > =20 > + if (!rc && snapshot) > + rc =3D cxl_restore_state_after_pci_reset(dev, snapshot, &command); > + > pci_dev_reset_iommu_done(dev); > + > + /* Enable bus mastering only once the IOMMU translates again. */ > + if (!rc && snapshot) > + rc =3D cxl_restore_command_after_pci_reset(dev, snapshot, command); > + > return rc; > } [Severity: High] Can standard bus reset still bypass CXL state restoration when SBR is already unmasked on the upstream CXL port? In pci_reset_fn_methods[], pci_reset_bus_function ("bus") precedes cxl_reset_bus_function ("cxl_bus"), and pci_reset_bus_function() only returns -ENOTTY for a CXL port when cxl_sbr_masked(bridge) is true. When PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR is already set, standard bus reset probes and executes first (for example via VFIO device reset or pci_reset_function()), wiping active HDM decoder and DVSEC control registers without restoring them while the CXL core still assumes they are programmed. Should cxl_bus precede bus in pci_reset_fn_methods[], or should pci_reset_bus_function() decline devices below a CXL port even when SBR is already unmasked? --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261205041453.3946= 529-1-smadhavan@nvidia.com?part=3D17