Linux PCI subsystem development
 help / color / mirror / Atom feed
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 17/19] PCI/CXL: Restore CXL state after CXL bus reset
Date: Sat, 05 Dec 2026 04:47:53 +0000	[thread overview]
Message-ID: <sashiko-outbox-251099@kernel.org> (raw)
In-Reply-To: <20261205041453.3946529-18-smadhavan@nvidia.com>

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 <smadhavan@nvidia.com>

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 cxl_hdm_info *const *slot)
>  	return copy;
>  }
>  
> +/**
> + * 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 when
> + * @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 = *slot;
> +	if (!info)
> +		return -ENXIO;
> +	if (info->decoder_count != 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=1, HDM_ENABLE=1)
  ...                             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(struct pci_dev *pdev,
>  }
>  
>  /*
> - * Restore window. Take the region lock again to write the HDM state back. 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 disabled
> + * 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 cache,
> + * and restoring older decoders would leave the device decoding ranges the
> + * 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;
>  
>  	cxl_region_quiesce_lock();
> -	rc = pci_cxl_restore_state(pdev, snapshot, command);
> +	rc = cxl_hdm_cache_copy_decoders(&pdev->hdm, snapshot);
> +	if (!rc)
> +		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();

[ ... ]

> +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 = 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 = 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 = cxl_reset_save_restored_state(pdev, command);
> +	if (rc)
> +		return rc;
> +
> +	rc = 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);
>  
> +	if (!rc && snapshot)
> +		rc = 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 = 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?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205041453.3946529-1-smadhavan@nvidia.com?part=17

  reply	other threads:[~2026-12-05  4:47 UTC|newest]

Thread overview: 33+ 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
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 [this message]
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
2026-12-05  4:54 ` [PATCH v18 00/19] PCI/CXL: Add CXL reset support for Type 2 devices 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-251099@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