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 13/19] cxl: Validate and synchronize HDM ranges around reset
Date: Sat, 05 Dec 2026 04:47:26 +0000 [thread overview]
Message-ID: <sashiko-outbox-251098@kernel.org> (raw)
In-Reply-To: <20261205041453.3946529-14-smadhavan@nvidia.com>
Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] cxl: normalized-addressing decoders undetected without CXL
driver
- [High] PCI/CXL: cxl_reset_hdm_prepare() checks stale cached dvsec_ctrl
- [Medium] cxl: devm_cxl_enable_hdm() omits global_ctrl cache update
- [Medium] cxl: TOCTOU on regs_offline in cxl_hdm_refresh_decoder()
- [Medium] cxl: cxl_port probe/unbind races with CXL reset regs_offline
- [Medium] PCI/CXL: regs_offline misses BAR-disabled window during CXL
reset
- [Medium] PCI/CXL: cxl_reset_hdm_unmark() clears reset_pending too
early
- [Medium] cxl: cxl_hdm_quiesce_prepare() rejects reset with zero
enabled HDM ranges
- [Low] PCI/CXL: cxl_reset_wait_cache_wbi() misses final check after
timeout
--
commit fbf810078bf8100238ebf8edce3e20d5244b645e
Author: Srirangan Madhavan <smadhavan@nvidia.com>
cxl: Validate and synchronize HDM ranges around reset
This commit reserves enabled system-physical HDM ranges exclusively and
invalidates CPU caches around CXL Reset, while using per-device
reset_pending and regs_offline marks to coordinate with concurrent
decoder commits and teardowns.
> diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
> index f884181775824..ebc9995eec1be 100644
> --- a/drivers/cxl/core/hdm.c
> +++ b/drivers/cxl/core/hdm.c
> @@ -764,10 +764,31 @@ static void cxl_hdm_refresh_decoder(struct cxl_hdm *cxlhdm,
> cxl_port_get_uport_pci_dev(cxlhdm->port);
> if (!pdev)
> return;
> - cxl_hdm_cache_refresh_decoder(pdev, hdm,
> + /* Do not read the device while it is in reset. */
> + cxl_hdm_cache_refresh_decoder(pdev,
> + cxl_hdm_cache_is_regs_offline(pdev) ?
> + NULL : hdm,
> to_cxl_endpoint_decoder(&cxld->dev));
> }
[Severity: Medium]
Can this race with cxl_reset_regs_offline() when called from
devm_cxl_enumerate_decoders()?
Unlike cxl_decoder_commit() and cxl_decoder_reset(),
devm_cxl_enumerate_decoders() does not hold cxl_rwsem.region or
device_lock(&pdev->dev). Because cxl_hdm_cache_is_regs_offline() releases
cxl_rwsem.dpa before cxl_hdm_cache_refresh_decoder() reacquires it, the
reset path can start in between:
CPU0 CPU1
devm_cxl_enumerate_decoders()
cxl_hdm_refresh_decoder()
cxl_hdm_cache_is_regs_offline()
-> false
cxl_reset_regs_offline()
regs_offline = true
cxl_clear_memory(..., true)
cxl_hdm_cache_refresh_decoder()
readl(hdm + CTRL_OFFSET) <-- MMIO read during reset
This violates CXL r4.0 sec 9.7.3 and can overwrite cached global_ctrl with
a reset value such as 0 whenever the device does not return U32_MAX.
Checking !info->regs_offline inside cxl_hdm_cache_refresh_decoder() under
cxl_rwsem.dpa would close this window.
[ ... ]
> @@ -911,14 +945,16 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)
> "%s: out of order reset, expected decoder%d.%d\n",
> dev_name(&cxld->dev), port->id, port->commit_end);
>
> - ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
> - ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT;
> - writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
> + if (!cxl_decoder_regs_offline(cxld)) {
> + ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
> + ctrl &= ~CXL_HDM_DECODER0_CTRL_COMMIT;
> + writel(ctrl, hdm + CXL_HDM_DECODER0_CTRL_OFFSET(id));
>
> - writel(0, hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id));
> - writel(0, hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id));
> - writel(0, hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
> - writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
> + writel(0, hdm + CXL_HDM_DECODER0_SIZE_HIGH_OFFSET(id));
> + writel(0, hdm + CXL_HDM_DECODER0_SIZE_LOW_OFFSET(id));
> + writel(0, hdm + CXL_HDM_DECODER0_BASE_HIGH_OFFSET(id));
> + writel(0, hdm + CXL_HDM_DECODER0_BASE_LOW_OFFSET(id));
> + }
[Severity: Medium]
Does regs_offline also need to guard cxl_endpoint_port_probe() and cxl_port
unbind while CXL Reset is in progress?
PCI reset holds device_lock(&pdev->dev), but devm_cxl_add_endpoint()
parents the endpoint cxl_port under the upstream port host rather than
&pdev->dev, and neither probe nor devres teardown holds cxl_rwsem.region.
Because cxl_rwsem.region is dropped across the up-to-100-second reset wait,
a concurrent probe or unbind can access the resetting device:
CPU0 CPU1
__cxl_reset_execute()
cxl_reset_regs_offline()
cxl_clear_memory(..., true)
cxl_endpoint_port_probe()
devm_cxl_enumerate_decoders()
init_hdm_decoder() <-- reads 0
cxl_hdm_refresh_decoder()
wipes pdev->hdm->settings[]
On probe, this issues prohibited config and MMIO accesses during the reset
quiet period and wipes cached decoder settings that post-reset restoration
depends on. On unbind, clear_mem_enable() and disable_hdm() similarly
access DVSEC config space and HDM MMIO without checking regs_offline, and
disable_hdm() can write U32_MAX & ~CXL_HDM_DECODER_ENABLE to hardware while
skipping the cache update.
> diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c
> index ddb69411de1c7..497709bffff2b 100644
> --- a/drivers/cxl/core/hdm_state.c
> +++ b/drivers/cxl/core/hdm_state.c
[ ... ]
> +bool cxl_hdm_snapshot_is_dvsec_decoded(const struct cxl_hdm_info *info)
> +{
> + return (info->dvsec_ctrl & PCI_DVSEC_CXL_MEM_ENABLE) &&
> + !(info->global_ctrl & CXL_HDM_DECODER_ENABLE);
> +}
[Severity: Medium]
Can this falsely classify an endpoint as DVSEC-decoded because
devm_cxl_enable_hdm() does not update cached global_ctrl?
In drivers/cxl/core/pci.c, devm_cxl_enable_hdm() sets
CXL_HDM_DECODER_ENABLE in hardware without calling
cxl_hdm_cache_set_global_ctrl(), unlike disable_hdm() right above it. In
cxl_hdm_decode_init(), devm_cxl_enable_mem() runs immediately afterwards
and updates pdev->hdm->dvsec_ctrl with PCI_DVSEC_CXL_MEM_ENABLE, before
devm_cxl_enumerate_decoders() sleeps for 20 ms in cxl_settle_decoders().
During that window, a snapshot has MEM_ENABLE set and HDM_DECODER_ENABLE
clear, causing cxl_reset_hdm_prepare() to refuse reset with -EOPNOTSUPP.
[ ... ]
> +static int cxl_hdm_ranges_collect(struct cxl_hdm_quiesce *q,
> + const struct cxl_hdm_info *snapshot)
> +{
> + int rc;
> +
> + for (int i = 0; i < snapshot->decoder_count; i++) {
> + const struct cxl_decoder_config *config =
> + &snapshot->settings[i].config;
> +
> + /* A committed zero-size decoder maps no HPA. */
> + if (!(config->flags & CXL_DECODER_F_ENABLE) ||
> + !range_len(&config->hpa_range))
> + continue;
> +
> + if (config->flags & CXL_DECODER_F_NORMALIZED_ADDRESSING)
> + return -EOPNOTSUPP;
> +
> + rc = cxl_hdm_range_add(q, &config->hpa_range);
> + if (rc)
> + return rc;
> + }
> +
> + return 0;
> +}
[Severity: High]
As reported by Alison Schofield on v17 [1], what prevents a reset from
using untranslated normalized-addressing ranges when no CXL driver is bound
or before cxl_add_to_region() completes?
Hardware HDM decoder registers do not indicate Normalized Addressing, and
CXL_DECODER_F_NORMALIZED_ADDRESSING is only recorded into pdev->hdm by
cxl_add_to_region() after PRM translation setup runs. When a device is
bound to vfio-pci or reset before region discovery finishes, that flag is
absent and cxl_hdm_ranges_request() calls request_mem_region() directly
without checking that the range lies within a platform CXL window. If the
untranslated address happens to be unclaimed in iomem_resource, the reset
proceeds without reserving or invalidating the true CXL SPA range, risking
data corruption or machine checks on active CXL memory.
[ ... ]
> +int cxl_hdm_quiesce_prepare(struct cxl_hdm_quiesce *q,
> + const struct cxl_hdm_info *snapshot)
> +{
> + int rc;
> +
> + lockdep_assert_held_write(&cxl_rwsem.region);
> + INIT_LIST_HEAD(&q->ranges);
> +
> + if (!cpu_cache_has_invalidate_memregion() &&
> + !IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST))
> + return -ENXIO;
> +
> + rc = cxl_hdm_ranges_collect(q, snapshot);
> + if (rc)
> + return rc;
> +
> + return cxl_hdm_ranges_request(q);
> +}
[Severity: Medium]
Should this check cpu_cache_has_invalidate_memregion() only after
cxl_hdm_ranges_collect() finds at least one active range?
When all HDM decoders are disabled or zero-sized, q->ranges is empty and
cxl_hdm_ranges_invalidate() performs no cache invalidation calls anyway.
Returning -ENXIO upfront rejects reset on x86 VMs and architectures without
a cache invalidation provider even when no HPA range is decoded, and
because -ENXIO is not -ENOTTY, __pci_reset_function_locked() aborts without
trying fallback reset methods.
> diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
> index 4d6cfabab2c0e..c90715c5a6d52 100644
> --- a/drivers/pci/cxl.c
> +++ b/drivers/pci/cxl.c
[ ... ]
> + /* Judge the snapshot: the live cache can change after it was taken. */
> + if (cxl_hdm_snapshot_is_dvsec_decoded(snapshot)) {
> + rc = -EOPNOTSUPP;
> + goto free_snapshot;
> + }
> + /*
> + * With CXL.cache enabled the device cache may hold dirty lines that only
> + * Cache Writeback and Invalidate can push back to memory. Reset would
> + * discard them, so refuse when the device cannot do it.
> + */
> + if ((snapshot->dvsec_ctrl & PCI_DVSEC_CXL_CACHE_ENABLE) &&
> + !(cap & PCI_DVSEC_CXL_CACHE_WBI_CAPABLE)) {
> + pci_err(pdev, "CXL reset refused: CXL.cache is enabled but the device cannot write back and invalidate its cache\n");
> + rc = -EOPNOTSUPP;
> + goto free_snapshot;
> + }
[Severity: High]
Can snapshot->dvsec_ctrl be stale here compared to the live
PCI_DVSEC_CXL_CTRL register?
pdev->hdm->dvsec_ctrl is populated once at PCI enumeration and is only
updated by cxl_set_mem_enable(), never when CXL.cache is enabled or when a
VFIO user writes PCI_DVSEC_CXL_CTRL directly. If PCI_DVSEC_CXL_CACHE_ENABLE
was clear at enumeration and enabled later on a device without
PCI_DVSEC_CXL_CACHE_WBI_CAPABLE, cxl_reset_function() reads live hardware
and enters cxl_reset_execute(), while this check sees 0 in
snapshot->dvsec_ctrl and allows CXL Reset to proceed without WBI,
discarding dirty device-cache lines. Similarly, CXL.mem enabled outside
cxl_set_mem_enable() with HDM globally disabled can bypass the
DVSEC-decoded check. Reading live PCI_DVSEC_CXL_CTRL here and checking it
alongside snapshot->dvsec_ctrl would close both gaps.
> +
> + rc = cxl_hdm_quiesce_prepare(&cache->quiesce, snapshot);
> + if (rc) {
> + pci_err(pdev, "failed to prepare CXL reset ranges: %d\n", rc);
> + cxl_hdm_quiesce_destroy(&cache->quiesce);
> + goto free_snapshot;
> + }
> +
> + /* From here until restore, no decoder of this device can commit. */
> + cxl_hdm_cache_set_reset_pending(pdev, true);
> + cxl_region_quiesce_unlock();
> + return snapshot;
[Severity: Medium]
Aren't the device's HDM BAR registers already offline at this point, before
cxl_reset_regs_offline() is called?
Standard PCI reset paths call pci_dev_save_and_disable() before
cxl_reset_function(), which clears PCI_COMMAND_MEMORY in PCI_COMMAND until
pci_dev_restore() runs. Because cxl_reset_hdm_prepare() drops
cxl_rwsem.region with regs_offline still false, a concurrent region
teardown can run before cxl_reset_regs_offline():
CPU0 CPU1
pci_dev_save_and_disable()
PCI_COMMAND_MEMORY = 0
cxl_reset_hdm_prepare()
reset_pending = true
cxl_region_quiesce_unlock()
cxl_decoder_reset()
cxl_decoder_regs_offline() -> false
readl/writel(hdm + ...) <-- BAR off
clears ENABLE & cache settings
cxl_reset_cache_prepare() -> fails
The MMIO accesses hit a disabled BAR (returning 0xFFFFFFFF and dropping the
uncommit writes), and if preparation then fails before CXL Reset starts,
the hardware decoder remains committed while software and pdev->hdm record
it as disabled. The same gap exists after cxl_reset_hdm_unmark() on error
paths before pci_dev_restore() re-enables PCI_COMMAND_MEMORY.
[ ... ]
> +/* Shared host invalidation and device cache writeback, without the region lock. */
> +static int cxl_reset_cache_prepare(struct pci_dev *pdev, int dvsec, u16 cap,
> + struct cxl_pci_cache *cache)
> +{
> + int rc;
> +
> + rc = cxl_hdm_quiesce_flush(&cache->quiesce);
> + if (rc)
> + return rc;
> +
> + /* Track an attempted change so partial failures still restore policy. */
> + cache->cache_disabled = true;
> + return cxl_reset_disable_cache(pdev, dvsec, cap);
> +}
[Severity: Low]
Can cxl_reset_wait_cache_wbi(), called from cxl_reset_disable_cache(),
return a spurious -ETIMEDOUT after hardware has already finished WBI?
drivers/pci/cxl.c:cxl_reset_wait_cache_wbi() {
...
deadline = jiffies + usecs_to_jiffies(CXL_CACHE_WBI_TIMEOUT_US);
while (time_before(jiffies, deadline)) {
usleep_range(CXL_CACHE_WBI_POLL_US,
CXL_CACHE_WBI_POLL_US * 2);
...
}
pci_warn(pdev, "timed out waiting for CXL cache writeback and invalidate\n");
return -ETIMEDOUT;
}
If the task is preempted past deadline before the loop or after the last
sleep, it exits without a final read of PCI_DVSEC_CXL_STATUS2 to check
PCI_DVSEC_CXL_CACHE_INV, unlike cxl_reset_wait_done().
[ ... ]
> + 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);
> +
> pci_dev_reset_iommu_done(pdev);
> return rc;
> }
>
> +/*
> + * Three short region-lock windows surround the reset: cxl_reset_hdm_prepare()
> + * captures the state and reserves the ranges, cxl_reset_regs_offline() keeps
> + * region teardown off the registers just before the reset starts, and
> + * cxl_reset_hdm_unmark() lets decoders commit again after the host flush.
> + * The wait for Memory Clear, up to 100 s, and the flush run without the lock.
> + */
> static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
> {
> - u16 saved_ctrl2;
> + struct cxl_pci_cache cache = {};
> int rc, rc2;
>
> - rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2,
> - &saved_ctrl2);
> - if (rc)
> - return rc;
> -
> - rc = cxl_reset_disable_cache(pdev, dvsec, cap);
> - if (!rc)
> - rc = __cxl_reset_execute(pdev, dvsec, cap);
> + struct cxl_hdm_info *snapshot __free(kfree) =
> + cxl_reset_hdm_prepare(pdev, dvsec, cap, &cache);
> + if (IS_ERR(snapshot))
> + return PTR_ERR(snapshot);
> +
> + rc = cxl_reset_cache_prepare(pdev, dvsec, cap, &cache);
> + if (!rc) {
> + rc = __cxl_reset_execute(pdev, dvsec, cap, &cache.quiesce);
> + } else {
> + /* Nothing was reset: clear the mark and drop the reservation. */
> + cxl_reset_hdm_unmark(pdev);
> + if (!cache.cache_disabled) {
> + pci_err(pdev, "failed to prepare CXL reset ranges: %d\n", rc);
> + cxl_hdm_quiesce_destroy(&cache.quiesce);
> + return rc;
> + }
> + }
> /* Restore cache policy after any attempt to disable caching. */
> - rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, saved_ctrl2);
> + rc2 = cxl_reset_restore_cache_policy(pdev, dvsec, cache.saved_ctrl2);
> + cxl_hdm_quiesce_destroy(&cache.quiesce);
> return rc ?: rc2;
> }
[Severity: Medium]
Does cxl_reset_hdm_unmark() unblock concurrent decoder commits too early?
On both normal completion and error paths, cxl_reset_hdm_unmark() clears
reset_pending and releases cxl_rwsem.region before
pci_dev_reset_iommu_done(), cxl_reset_restore_cache_policy(), and
cxl_hdm_quiesce_destroy() have finished. A concurrent commit_store() can
commit an endpoint decoder while IOMMU exclusion and DISABLE_CACHING are
still active and while the exclusive cxl_reset iomem_resource reservations
are still held, causing the region's consumer driver probe to fail with
-EBUSY. Moving cxl_reset_hdm_unmark() after those cleanup steps complete
would avoid the race.
[1] https://lore.kernel.org/r/avyPsvwV1_lc9va5@aschofie-mobl2.lan
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205041453.3946529-1-smadhavan@nvidia.com?part=13
next prev parent 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 [this message]
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
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-251098@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