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 18/19] PCI/CXL: Flush host and device caches for CXL bus reset
Date: Sat, 05 Dec 2026 04:54:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-251105@kernel.org> (raw)
In-Reply-To: <20261205041453.3946529-19-smadhavan@nvidia.com>

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

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,
>  
>  	cxl_region_quiesce_lock();
>  	rc = 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 = 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 = 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=0
                                   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 = 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 = 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 != 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 = 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 = 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 == -ENOTTY) if cleanup fails with rc2 or
policy_rc.

Evaluating rc = 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)
>  
>  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 = 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);
>  	}
>  
> -	rc = pci_reset_bus_function(dev, probe);
> +	if (cxl_sbr_masked(bridge)) {
> +		/* The unmask did not take effect, so SBR would do nothing. */
> +		rc = -ENOTTY;
> +	} else {
> +		if (snapshot)
> +			cxl_reset_regs_offline(dev);
> +		rc = 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 = 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 = 0
pci_cxl_reset_prepare()
  reset_pending = 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=0, 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=1; 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=0 MMIO
accesses, though the abort path would also need to reconcile any decoders
torn down during prepare against hardware before restoring PCI_COMMAND.

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

  reply	other threads:[~2026-12-05  4:54 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
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 [this message]
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-251105@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