All of lore.kernel.org
 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 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method
Date: Sat, 05 Dec 2026 04:48:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-251100@kernel.org> (raw)
In-Reply-To: <20261205041453.3946529-17-smadhavan@nvidia.com>

Thank you for your contribution! Sashiko AI review found 11 potential
issues to consider:
- [High] PCI/CXL: cxl_reset_function() omits cxl_reset when enabled
  after probe
- [High] PCI/CXL: cxl_reset_function() falls back to FLR with single CXL
  protocol
- [High] PCI/CXL: VFIO guest can clear DVSEC control to bypass
  cxl_reset_function()
- [High] PCI/CXL: cxl_reset_function() can run before Normalized
  Addressing discovery
- [High] PCI/CXL: cxl_reset_function() state_saved check breaks raw
  reset callers
- [Medium] cxl: devm_cxl_enable_hdm() fails to update cached global_ctrl
- [Medium] pci/cxl: cxl_reset_wait_cache_wbi() omits final status poll
  after timeout
- [Medium] PCI/CXL: cxl_reset_execute() writes stale dvsec_ctrl after
  cxl_port unbind
- [Medium] PCI/CXL: unlocked bus->devices check and TOCTOU race in
  cxl_reset_function()
- [Medium] PCI/CXL: cxl_port probe/unbind races with
  cxl_reset_function()
- [Medium] PCI/CXL: cxl_reset_hdm_unmark() runs before state cleanup and
  HPA release
--

commit 3a501efb8899451afa7458cc7065fa2f49abb4dc
Author: Srirangan Madhavan <smadhavan@nvidia.com>

PCI/CXL: Expose CXL Reset as a PCI reset method

This commit registers cxl_reset_function() in pci_reset_fn_methods[] ahead
of FLR, adjusts its probe error returns to -ENOTTY, and rejects execution
when pdev->state_saved is false.

> diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
> index 1053aa2f66071..cc2d5a4f67df5 100644
> --- a/drivers/pci/cxl.c
> +++ b/drivers/pci/cxl.c

[ ... ]

> @@ -823,7 +823,7 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)
>  
>  	rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL, &ctrl);
>  	if (rc)
> -		return rc;
> +		return probe ? -ENOTTY : rc;
>  	if (!(ctrl & PCI_DVSEC_CXL_CACHE_ENABLE) ||
>  	    !(ctrl & PCI_DVSEC_CXL_MEM_ENABLE))
>  		return -ENOTTY;

[Severity: High]
Does checking PCI_DVSEC_CXL_CTRL before the probe early-return permanently
exclude cxl_reset when firmware leaves CXL.mem or CXL.cache disabled at
boot?

pci_init_reset_methods() runs once in pci_init_capabilities() before any
driver binds, and nothing refreshes dev->reset_methods[] when
cxl_hdm_decode_init() -> devm_cxl_enable_mem() later enables
PCI_DVSEC_CXL_MEM_ENABLE.  Any subsequent PCI reset then skips cxl_reset
and runs FLR instead, leaving active CXL protocol state unreset and CXL
memory uncleared.

Should the probe path check only static PCI_DVSEC_CXL_CAP bits and defer
the dynamic PCI_DVSEC_CXL_CTRL checks until reset execution?

[Severity: High]
Can returning -ENOTTY when only one of CXL.cache or CXL.mem is enabled
cause __pci_reset_function_locked() to fall through to FLR while the other
CXL protocol is still active?

For example, cxl_pci only sets PCI_DVSEC_CXL_MEM_ENABLE, and a failed CXL
reset that keeps caching off also leaves MEM_ENABLE set with CACHE_ENABLE
cleared.  Because cxl_reset sits immediately ahead of flr, returning
-ENOTTY lets the next reset execute FLR without resetting the active CXL
protocol or clearing CXL memory.

Should -ENOTTY be returned only when neither protocol is enabled, with a
non--ENOTTY error if single-protocol states cannot be reset?

[Severity: High]
Can a VFIO guest bypass cxl_reset by clearing PCI_DVSEC_CXL_CACHE_ENABLE
or PCI_DVSEC_CXL_MEM_ENABLE in hardware config space?

vfio_pci_init_perm_bits() sets ecap_perms[PCI_EXT_CAP_ID_DVSEC].writefn to
vfio_raw_config_write, passing guest DVSEC writes directly to hardware.
If a guest clears either bit before device release, cxl_reset_function()
reads the live register and returns -ENOTTY, letting
__pci_reset_function_locked() fall through to FLR.  FLR then succeeds and
vfio_pci_core_disable() clears vdev->needs_reset, leaving guest CXL memory
and protocol state uncleared across reassignment.

> @@ -831,5 +831,11 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)
>  	if (probe)
>  		return 0;
>  
> +	/* Raw reset callers may not have saved the PCI state needed for restore. */
> +	if (!pdev->state_saved) {
> +		pci_err(pdev, "CXL reset requires saved PCI state\n");
> +		return -EINVAL;
> +	}

[Severity: High]
Does rejecting !pdev->state_saved with -EINVAL break raw callers of
__pci_reset_function_locked()?

__pci_reset_function_locked() does not require callers to save PCI state
beforehand, and returning -EINVAL aborts its loop without trying any later
reset method.  In xen-pciback, pcistub_init_device() clears
dev->state_saved via pci_restore_state(), and pcistub_put_pci_dev() and
pcistub_device_release() later call pcistub_reset_device_state() ->
__pci_reset_function_locked() before reloading saved state.  Because
pcistub_reset_device_state() ignores the return value, the device is left
completely unreset on guest release and unbind, letting guest state and
CXL memory persist across domain handoff.

> +
>  	return cxl_reset_execute(pdev, dvsec, cap);
>  }

[Severity: Medium]
Can cxl_reset_wait_cache_wbi() return a spurious -ETIMEDOUT if the task is
preempted past the deadline?

cxl_reset_wait_cache_wbi() checks while (time_before(jiffies, deadline))
before sleeping and reading PCI_DVSEC_CXL_STATUS2, with no final status
read after the loop exits (unlike cxl_reset_wait_done()).  If the thread
is delayed past the 100 ms deadline while hardware has already set
PCI_DVSEC_CXL_CACHE_INV, the reset aborts with -ETIMEDOUT instead of
succeeding.

[Severity: Medium]
Can cxl_reset_enable_cache() re-enable PCI_DVSEC_CXL_MEM_ENABLE after a
concurrent cxl_port unbind has disabled it?

pci_reset_function() holds device_lock(&pdev->dev) rather than
device_lock(&endpoint->dev), and drops cxl_rwsem.region after
cxl_reset_hdm_restore() re-enables MSE:

CPU0 (pci_reset_function)         CPU1 (cxl_port unbind)
cxl_reset_hdm_restore()
  /* MSE on, CACHE_ENABLE off */
  drop cxl_rwsem.region
                                  clear_mem_enable()
                                    cxl_set_mem_enable(cxlds, 0)
                                    cxl_hdm_cache_set_dvsec_ctrl()
                                  disable_hdm()
                                    /* clears HDM_DECODER_ENABLE */
cxl_reset_enable_cache()
  write snapshot->dvsec_ctrl  <-- re-enables MEM_ENABLE

Hardware is left with MEM_ENABLE set and CXL_HDM_DECODER_ENABLE cleared
while pdev->hdm->dvsec_ctrl records both MEM_ENABLE and CACHE_ENABLE as
cleared, causing a subsequent cxl_port rebind to fail with -ENXIO in
cxl_hdm_decode_init().  Note that commit "PCI/CXL: Restore CXL state after
CXL bus reset" refreshes decoder settings but still leaves
snapshot->dvsec_ctrl stale.

[Severity: Medium]
Does calling cxl_reset_hdm_unmark() before state cleanup and
cxl_hdm_quiesce_destroy() unblock concurrent decoder commits and teardowns
too early?

cxl_decoder_commit() and cxl_decoder_reset() rely on reset_pending and
regs_offline under cxl_rwsem.region, not device_lock(&pdev->dev):

CPU0 (__cxl_reset_execute)        CPU1 (commit_store)
cxl_reset_hdm_unmark(pdev)
  /* clears reset_pending */
                                  cxl_decoder_commit()
                                    /* reset_pending is false */
if (rc)
  cxl_reset_save_disabled_state()
    /* clears PCI_COMMAND_MEMORY */
                                    HDM MMIO access  <-- BAR disabled

On failure paths this allows HDM MMIO while PCI_COMMAND_MEMORY is being
cleared (or already off), producing Unsupported Requests and -EIO.  On
success paths it also lets a newly committed region fail
devm_request_mem_region() with -EBUSY before cxl_hdm_quiesce_destroy()
releases the cxl_reset HPA reservation.

> diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
> index 5a8338340d232..9ab647719c9f3 100644
> --- a/drivers/pci/pci.c
> +++ b/drivers/pci/pci.c
> @@ -5204,6 +5204,7 @@ const struct pci_reset_fn_method pci_reset_fn_methods[] = {
>  	{ },
>  	{ pci_dev_specific_reset, .name = "device_specific" },
>  	{ pci_dev_acpi_reset, .name = "acpi" },
> +	{ cxl_reset_function, .name = "cxl_reset" },
>  	{ pcie_reset_flr, .name = "flr" },
>  	{ pci_af_flr, .name = "af_flr" },
>  	{ pci_pm_reset, .name = "pm" },

[Severity: High]
As reported by Alison Schofield on v17 [1], can cxl_reset execute before
CXL endpoint and region discovery resolve Normalized Addressing?

pci_bus_add_device() runs pci_save_state() and pci_cxl_hdm_cache_init()
from raw HDM registers before CXL drivers bind, whereas
CXL_DECODER_F_NORMALIZED_ADDRESSING is only recorded into pdev->hdm later
in cxl_add_to_region() -> cxl_hdm_cache_set_normalized().  If a reset runs
before region discovery completes (or while CXL drivers are unbound),
cxl_hdm_ranges_collect() misses the flag instead of returning -EOPNOTSUPP
and passes the raw normalized base to request_mem_region() and
cpu_cache_invalidate_memregion().  cxl_clear_memory() then clears device
memory without flushing the true host SPA cachelines, risking silent data
corruption or machine checks.

[Severity: Medium]
Does devm_cxl_enable_hdm() forget to update cached global_ctrl via
cxl_hdm_cache_set_global_ctrl()?

Unlike disable_hdm() right above it, devm_cxl_enable_hdm() writes
CXL_HDM_DECODER_ENABLE to hardware without updating pdev->hdm->global_ctrl,
while devm_cxl_enable_mem() immediately updates dvsec_ctrl with
PCI_DVSEC_CXL_MEM_ENABLE.  A CXL reset racing during the 20 ms
cxl_settle_decoders() sleep in devm_cxl_enumerate_decoders() then sees
MEM_ENABLE set with CXL_HDM_DECODER_ENABLE clear, causing
cxl_hdm_snapshot_is_dvsec_decoded() to return true and
cxl_reset_hdm_prepare() to fail with -EOPNOTSUPP.

[Severity: Medium]
Is the list_is_singular(&pdev->bus->devices) check in cxl_reset_function()
missing pci_bus_sem and pci_rescan_remove_lock?

pci_device_add() and pci_destroy_dev() mutate bus->devices under
pci_bus_sem and pci_rescan_remove_lock without taking
device_lock(&pdev->dev), so reading bus->devices here is a data race and
leaves an ARI TOCTOU window across cxl_reset_execute():

CPU0 (cxl_reset_function)         CPU1 (dev_rescan_store)
list_is_singular(&bus->devices)
  /* true, unlocked */
                                  pci_lock_rescan_remove()
                                  pci_rescan_bus(pdev->bus)
                                    pci_device_add(sib)
                                      down_write(&pci_bus_sem)
                                      list_add_tail(&sib->bus_list, ...)
                                      up_write(&pci_bus_sem)
                                    /* binds driver to sib */
cxl_reset_execute(pdev, ...)
  cxl_clear_memory(...)  <-- resets active ARI sibling

If a previously removed ARI sibling is rescanned after list_is_singular()
passes, CXL Reset resets the active sibling without saving its PCI state
or quiescing its driver.

[Severity: Medium]
Can concurrent cxl_port probe or unbind issue config and HDM MMIO accesses
during the CXL Reset quiet period?

pci_reset_function() holds device_lock(&pdev->dev) and
pci_cfg_access_lock(pdev), which neither blocks kernel
pci_{read,write}_config_*() calls nor serializes against
device_lock(&port->dev) on the endpoint cxl_port:

CPU0 (__cxl_reset_execute)        CPU1 (cxl_endpoint_port_probe)
cxl_reset_regs_offline(pdev)
cxl_clear_memory(pdev, ..., true)
cxl_reset_wait_done(...)
  /* 100 ms - 100 s quiet period,
     MSE off, no cxl_rwsem.region */
                                  device_lock(&port->dev)
                                  read_cdat_data()  <-- DOE config access
                                  devm_cxl_endpoint_decoders_setup()
                                    /* DVSEC config & HDM MMIO */

Neither cxl_endpoint_port_probe() nor cxl_port unbind (clear_mem_enable()
and disable_hdm()) checks regs_offline or reset_pending.  Running during
cxl_reset_wait_done() issues non-Status2 config and HDM MMIO accesses
while MSE is cleared, risking PCIe Unsupported Requests, AER errors, and
stale pdev->hdm cache updates.

[1] https://lore.kernel.org/r/avyQ2sXKfvdz9SVs@aschofie-mobl2.lan

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

  reply	other threads:[~2026-12-05  4:48 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 [this message]
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
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-251100@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.