From: sashiko-bot@kernel.org
To: "Srirangan Madhavan" <smadhavan@nvidia.com>,
linux-cxl@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset
Date: Sat, 05 Dec 2026 04:50:11 +0000 [thread overview]
Message-ID: <sashiko-outbox-251103@kernel.org> (raw)
In-Reply-To: <20261205041453.3946529-16-smadhavan@nvidia.com>
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] cxl: cxl_hdm_restore_state() uncommits decoders in ascending
order
- [Medium] PCI/CXL: regs_offline and reset_pending cleared before reset
cleanup finishes
- [Medium] PCI/CXL: cxl_reset_save_disabled_state() clobbers saved PCI
state on error
- [Medium] PCI/CXL: cxl_reset_hdm_restore() writes stale dvsec_ctrl and
global_ctrl
- [Medium] PCI/CXL: cxl_reset_save_disabled_state() leaves CXL.mem and
CXL.cache enabled
- [Medium] PCI/CXL: race between cxl_reset_execute() and cxl_port
probe/unbind
- [Medium] PCI/CXL: cxl_reset_execute() enables MSE/BME before outer
pci_restore_state()
--
commit 46128862bc7d9a15961d5d75eb89380f613d4b4f
Author: Srirangan Madhavan <smadhavan@nvidia.com>
PCI/CXL: Restore CXL state after PCI reset
This commit adds built-in HDM decoder restoration helpers in
drivers/cxl/core/hdm_regs.c and hooks them into the CXL Device Reset
path in drivers/pci/cxl.c to restore PCI state, HDM decoders, and CXL
Device DVSEC control after reset.
> diff --git a/drivers/cxl/core/hdm_regs.c b/drivers/cxl/core/hdm_regs.c
> index cc99ae83d1cd1..358e2ba7495de 100644
> --- a/drivers/cxl/core/hdm_regs.c
> +++ b/drivers/cxl/core/hdm_regs.c
[ ... ]
> +static int
> +cxl_hdm_restore_decoder(void __iomem *hdm,
> + const struct cxl_endpoint_decoder_settings *settings)
> +{
> + const struct cxl_decoder_config *config = &settings->config;
> + bool retained;
> + u32 ctrl;
> + int rc;
> +
> + rc = cxl_hdm_decoder_uncommit(hdm, config->id, &retained);
[ ... ]
> +int cxl_hdm_restore_state(void __iomem *hdm,
> + const struct cxl_hdm_info *snapshot)
> +{
> + int count;
> + int rc;
> +
> + count = cxl_hdm_decoder_count(readl(hdm + CXL_HDM_DECODER_CAP_OFFSET));
> + if (count != snapshot->decoder_count)
> + return -ENXIO;
> +
> + /* Restore global HDM control before committing individual decoders. */
> + writel(snapshot->global_ctrl, hdm + CXL_HDM_DECODER_CTRL_OFFSET);
> +
> + for (int i = 0; i < snapshot->decoder_count; i++) {
> + rc = cxl_hdm_restore_decoder(hdm, &snapshot->settings[i]);
> + if (rc)
> + return rc;
> + }
[Severity: Medium]
Can this ascending loop uncommit HDM decoders out of order when multiple
decoders survive reset?
CXL requires HDM decoders to be uncommitted in descending order and
committed in ascending order. If decoder 0 and decoder 1 remain committed
in hardware, iteration 0 clears Commit on decoder 0 while decoder 1 is
still committed.
Spec-compliant hardware rejects out-of-order uncommit with Commit Error
(-EIO) or never clears Committed (-ETIMEDOUT), aborting restoration and
leaving the PCI device disabled. Performing a descending uncommit pass
down to any locked committed decoder before the ascending commit pass
would preserve hardware ordering.
> diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
> index 0c60f6d7bb4fa..1053aa2f66071 100644
> --- a/drivers/pci/cxl.c
> +++ b/drivers/pci/cxl.c
[ ... ]
> +static int cxl_restore_dvsec_ctrl(struct pci_dev *pdev,
> + const struct cxl_hdm_info *snapshot)
> +{
> + int rc;
> +
> + if (!snapshot->dvsec_ctrl_valid) {
> + pci_err(pdev, "CXL DVSEC control snapshot is invalid\n");
> + return -ENXIO;
> + }
> +
> + /* Leave CXL.cache off until the host caches are flushed. */
> + rc = pci_write_config_word(pdev, snapshot->dvsec + PCI_DVSEC_CXL_CTRL,
> + snapshot->dvsec_ctrl &
> + ~PCI_DVSEC_CXL_CACHE_ENABLE);
> + if (rc) {
> + rc = pcibios_err_to_errno(rc);
> + pci_err(pdev, "failed to restore CXL DVSEC control: %d\n", rc);
> + }
> +
> + return rc;
> +}
> +
> +/* Turn CXL.cache back on after the host caches are flushed. */
> +static int cxl_reset_enable_cache(struct pci_dev *pdev,
> + const struct cxl_hdm_info *snapshot)
> +{
> + int rc;
> +
> + if (!(snapshot->dvsec_ctrl & PCI_DVSEC_CXL_CACHE_ENABLE))
> + return 0;
> +
> + rc = pci_write_config_word(pdev, snapshot->dvsec + PCI_DVSEC_CXL_CTRL,
> + snapshot->dvsec_ctrl);
[Severity: Medium]
Can cxl_restore_dvsec_ctrl(), cxl_reset_enable_cache(), and
cxl_hdm_restore_state() write stale pre-reset dvsec_ctrl and global_ctrl
values?
Because cxl_rwsem.region is dropped after cxl_reset_hdm_prepare() and
again after cxl_reset_hdm_restore(), a concurrent cxl_port unbind can run
clear_mem_enable() and disable_hdm(), clearing PCI_DVSEC_CXL_MEM_ENABLE
and CXL_HDM_DECODER_ENABLE in both hardware and pdev->hdm.
The restore path still writes the pre-reset snapshot->global_ctrl and the
full snapshot->dvsec_ctrl word, re-enabling CXL.mem or global HDM
decoding while cxl_port is unbound. If unbind runs between
cxl_reset_hdm_restore() and cxl_reset_enable_cache(), writing the full
snapshot->dvsec_ctrl word turns PCI_DVSEC_CXL_MEM_ENABLE back on while
CXL_HDM_DECODER_ENABLE remains cleared.
Later commit "PCI/CXL: Restore CXL state after CXL bus reset" refreshes
per-decoder settings from pdev->hdm, but leaves global_ctrl and
dvsec_ctrl stale. Refreshing global_ctrl and dvsec_ctrl from pdev->hdm
and using read-modify-write for PCI_DVSEC_CXL_CACHE_ENABLE would avoid
restoring stale enable bits.
[ ... ]
> +static void cxl_reset_save_disabled_state(struct pci_dev *pdev)
> +{
> + u32 *saved_command = &pdev->saved_config_space[PCI_COMMAND / 4];
> + int rc;
> +
> + rc = pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
> + if (rc)
> + pci_err(pdev,
> + "failed to disable device after CXL reset failure: %d\n",
> + pcibios_err_to_errno(rc));
> +
> + /* Preserve the pre-reset snapshot if PCI state has not been restored. */
> + if (!pdev->state_saved) {
> + rc = pci_save_state(pdev);
> + if (rc)
> + pci_err(pdev,
> + "failed to save disabled state after CXL reset failure: %d\n",
> + rc);
> + }
> +
> + /* Ensure the outer PCI restore cannot re-enable MSE or BME. */
> + *saved_command &= GENMASK(31, 16);
> + *saved_command |= PCI_COMMAND_INTX_DISABLE;
> +}
[Severity: Medium]
Can calling pci_save_state() here overwrite the intact pre-reset saved
PCI state with 0xffffffff when the device is unresponsive?
cxl_restore_pci_state_for_hdm_restore() calls pci_restore_state(), which
clears pdev->state_saved while leaving pdev->saved_config_space[] and
saved capability buffers intact. If a later step fails because the device
stopped responding, !pdev->state_saved is true and pci_save_state() reads
all 16 header dwords and saved capabilities without checking for read
errors or 0xffffffff.
That overwrites the saved BARs and capability buffers with all-ones,
which the outer pci_dev_restore() then applies. Preserving the existing
saved buffers instead of re-reading from a failed device would avoid
clobbering them.
> +static int cxl_reset_save_restored_state(struct pci_dev *pdev, u16 command)
> +{
> + int rc;
> +
> + rc = pci_write_config_word(pdev, PCI_COMMAND, command);
> + if (rc) {
> + rc = pcibios_err_to_errno(rc);
> + pci_err(pdev, "failed to restore PCI command state: %d\n", rc);
> + cxl_reset_save_disabled_state(pdev);
> + return rc;
> + }
> +
> + rc = pci_save_state(pdev);
[Severity: Medium]
Does enabling MSE and BME here before returning cause the outer
pci_dev_restore() -> pci_restore_state() call to reprogram PCI
capabilities on an active device?
cxl_restore_pci_state_for_hdm_restore() already runs a full
pci_restore_state(), and cxl_reset_save_restored_state() then writes
PCI_COMMAND (enabling MSE and BME) and re-saves state. When
__pci_reset_function_locked() returns, pci_dev_restore() unconditionally
calls pci_restore_state() a second time while MSE, BME, and all PCI
capabilities are already enabled.
On that second pass, pci_restore_rebar_state() writes PCI_REBAR_CTRL
while PCI_COMMAND_MEMORY is set (violating the PCIe Resizable BAR
requirement), pci_restore_vc_state() reloads VC arbitration tables with
extended VCs active, pci_restore_pri_state() writes PCI_PRI_ALLOC_REQ
while PRI is enabled, and MSI/MSI-X are masked and rewritten while BME is
active.
[ ... ]
> +static int cxl_reset_hdm_restore(struct pci_dev *pdev,
> + struct cxl_hdm_info *snapshot, u16 *command)
> +{
> + int rc;
> +
> + cxl_region_quiesce_lock();
> + rc = 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();
> +
> + return rc;
> +}
[ ... ]
> rc2 = cxl_hdm_quiesce_flush(q);
> if (rc2)
> pci_err(pdev, "failed to invalidate CPU cache after CXL reset: %d\n", rc2);
> rc = rc ?: rc2;
> cxl_reset_hdm_unmark(pdev);
> + if (rc)
> + cxl_reset_save_disabled_state(pdev);
>
> pci_dev_reset_iommu_done(pdev);
> return rc;
[Severity: Medium]
Can clearing regs_offline and reset_pending before reset cleanup finishes
let concurrent decoder operations access HDM MMIO prematurely?
In cxl_reset_hdm_restore(), regs_offline is cleared even when
pci_cxl_restore_state() fails, allowing a concurrent cxl_decoder_reset()
to write HDM registers on an unrestored device before
cxl_reset_save_disabled_state() clears PCI_COMMAND_MEMORY.
Similarly, __cxl_reset_execute() calls cxl_reset_hdm_unmark() before
cxl_reset_save_disabled_state(), pci_dev_reset_iommu_done(), cache policy
and CXL.cache restoration, and cxl_reset_save_restored_state(). Because
cxl_decoder_commit() synchronizes on cxl_rwsem.region rather than
device_lock(&pdev->dev), it can pass cxl_hdm_cache_is_reset_pending() and
program HDM registers while PCI_COMMAND_MEMORY is being cleared on error
or while reset teardown is still incomplete.
[ ... ]
> + keep_caching_off = attempted && rc;
> + if (cache.cache_disabled && !keep_caching_off) {
> + rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, cache.saved_ctrl2);
> + if (!rc2 && attempted)
> + rc2 = cxl_reset_enable_cache(pdev, snapshot);
> + if (rc2)
> + cxl_reset_save_disabled_state(pdev);
> + }
[Severity: Medium]
Can cxl_reset_execute() race with concurrent cxl_port probe or unbind on
PCI_DVSEC_CXL_CTRL and CXL_HDM_DECODER_CTRL_OFFSET?
cxl_port probe and unbind hold only device_lock(&port->dev), never
device_lock(&pdev->dev) or cxl_rwsem.region, and kernel PCI config
accesses bypass pci_cfg_access_lock(). That allows an interleaving such
as:
CPU0 (cxl_reset_execute) CPU1 (cxl_port unbind)
cxl_reset_hdm_restore()
cxl_restore_dvsec_ctrl()
write PCI_DVSEC_CXL_CTRL
(CACHE_ENABLE=0)
cxl_region_quiesce_unlock()
clear_mem_enable()
cxl_set_mem_enable(cxlds, 0)
read PCI_DVSEC_CXL_CTRL (CACHE=0)
write 0; cache dvsec_ctrl = 0
disable_hdm()
clear CXL_HDM_DECODER_ENABLE
cxl_reset_enable_cache()
write snapshot->dvsec_ctrl <-- re-enables MEM_ENABLE with HDM off
This caches the transient CACHE_ENABLE=0 value into pdev->hdm->dvsec_ctrl
and then re-enables PCI_DVSEC_CXL_MEM_ENABLE in hardware while
CXL_HDM_DECODER_ENABLE is cleared. In addition, during cxl_clear_memory()
and cxl_reset_wait_done(), cxl_set_mem_enable(), disable_hdm(), and
devm_cxl_enable_hdm() do not check regs_offline or reset_pending and can
issue PCI config and HDM MMIO accesses while the device is in reset.
> cxl_hdm_quiesce_destroy(&cache.quiesce);
> - return rc ?: rc2;
> +
> + if (rc)
> + return rc;
> + if (rc2)
> + return rc2;
> +
> + return cxl_reset_save_restored_state(pdev, command);
[Severity: Medium]
Can post-restore failures leave CXL.mem or CXL.cache enabled in hardware
after cxl_reset_save_disabled_state() runs?
cxl_reset_save_disabled_state() only clears PCI_COMMAND (MSE and BME),
which gates CXL.io and PCIe DMA rather than CXL.mem or CXL.cache. If
post-reset cxl_hdm_quiesce_flush() fails after cxl_reset_hdm_restore()
succeeded, PCI_DVSEC_CXL_MEM_ENABLE and the committed HDM decoders remain
active even though CPU cache invalidation failed and HPA reservations are
released.
In addition, cxl_reset_execute() restores cache policy, re-enables
PCI_DVSEC_CXL_CACHE_ENABLE, and calls cxl_hdm_quiesce_destroy() before
cxl_reset_save_restored_state(). If cxl_reset_save_restored_state()
fails, both CXL.mem and CXL.cache remain enabled in hardware.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205041453.3946529-1-smadhavan@nvidia.com?part=15
next prev parent reply other threads:[~2026-12-05 4:50 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 4:14 [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 01/19] cxl: Clarify decoder interleave limit Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 02/19] cxl: Share CXL port upstream PCI device lookup Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 03/19] cxl: Move decoder declarations to shared header Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 04/19] cxl: Embed decoder configuration in a standalone structure Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 05/19] cxl: Introduce endpoint HDM decoder settings Srirangan Madhavan
2026-12-05 4:30 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 06/19] cxl: Move HDM decoder helpers to built-in code Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 07/19] cxl: Share HDM decoder register unpacking Srirangan Madhavan
2026-12-05 4:30 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 08/19] cxl: Move cxl_rwsem into built-in code Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 09/19] cxl: Refresh cached PCI HDM decoder settings Srirangan Madhavan
2026-12-05 4:38 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 10/19] cxl: Build register block helpers into the kernel Srirangan Madhavan
2026-12-05 4:14 ` [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration Srirangan Madhavan
2026-12-05 4:44 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 12/19] PCI/CXL: Add CXL Device Reset sequencing Srirangan Madhavan
2026-12-05 4:38 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset Srirangan Madhavan
2026-12-05 4:47 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope Srirangan Madhavan
2026-12-05 4:43 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset Srirangan Madhavan
2026-12-05 4:50 ` sashiko-bot [this message]
2026-12-05 4:14 ` [PATCH v18 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method Srirangan Madhavan
2026-12-05 4:48 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 17/19] PCI/CXL: Restore CXL state after CXL bus reset Srirangan Madhavan
2026-12-05 4:47 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 18/19] PCI/CXL: Flush host and device caches for " Srirangan Madhavan
2026-12-05 4:54 ` sashiko-bot
2026-12-05 4:14 ` [PATCH v18 19/19] cxl: Add KUnit checks for HDM decoder unpacking Srirangan Madhavan
2026-12-05 4:38 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-251103@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=smadhavan@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox