From: Srirangan Madhavan <smadhavan@nvidia.com>
To: Alison Schofield <alison.schofield@intel.com>,
Bjorn Helgaas <bhelgaas@google.com>,
Dave Jiang <dave.jiang@intel.com>,
Davidlohr Bueso <dave@stgolabs.net>,
Ira Weiny <iweiny@kernel.org>,
Jonathan Cameron <jic23@kernel.org>,
Vishal Verma <vishal.l.verma@intel.com>,
linux-cxl@vger.kernel.org, linux-pci@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: Li Ming <ming.li@zohomail.com>,
Joshua Machauer <machaujo@amazon.de>,
Alex Williamson <alex.williamson@redhat.com>,
vsethi@nvidia.com, alwilliamson@nvidia.com,
Sai Yashwanth Reddy Kancherla <skancherla@nvidia.com>,
Vishal Aslot <vaslot@nvidia.com>,
Manish Honap <mhonap@nvidia.com>, Jiandi An <jan@nvidia.com>,
Richard Cheng <icheng@nvidia.com>,
linux-tegra@vger.kernel.org,
Alejandro Lucero Palau <alejandro.lucero-palau@amd.com>,
Srirangan Madhavan <smadhavan@nvidia.com>
Subject: [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset
Date: Sat, 5 Dec 2026 04:14:47 +0000 [thread overview]
Message-ID: <20261205041453.3946529-14-smadhavan@nvidia.com> (raw)
In-Reply-To: <20261205041453.3946529-1-smadhavan@nvidia.com>
Refuse reset unless enabled system-physical HDM ranges can be reserved
exclusively and CPU-cache invalidation is available. Invalidate before
reset and again before ending IOMMU exclusion, holding range reservations
until the second invalidation completes. A later patch places state
restoration before the second invalidation.
Userspace is expected to offline CXL.mem users before reset. The
reservation enforces this for claimed ranges: the reset fails while a
conflicting resource is claimed, and no one can claim the range during
the reset. It cannot see accesses by code that uses the range without
claiming it, which the offline expectation also covers.
Reject normalized-addressing decoders because their cached ranges are not
system physical addresses. Record Normalized Addressing in the cache when
region discovery sets it, since the cache is filled earlier. A reset that
takes its snapshot between endpoint probe and that update can still
miss it.
Ignore zero-size decoders because they map no address range. Refuse reset
when the device decodes memory through its DVSEC ranges instead of HDM
decoders, since the cache holds no range to reserve for that memory, and
when CXL.cache is enabled but the device cannot write back and invalidate
its cache, since the reset would discard dirty device-cache lines.
Hold the global cxl_rwsem.region write lock only for short windows: to
take the snapshot and reserve the ranges, to mark the HDM registers
offline right before the reset starts, and to clear both marks once the
second invalidation is done. In between, the reserved ranges stay held
and a per-device reset_pending mark makes cxl_decoder_commit() fail for
the device's endpoint decoders, so no new decode can come online during
the reset. A region torn down while the registers are offline updates
only the software state and the cache. The Memory Clear wait, which lasts
up to the timeout the device advertises (at most 100 seconds), and the
cache flushes run without the lock.
Allow the existing CXL_REGION_INVALIDATION_TEST option to bypass missing
CPU cache invalidation for test environments without a provider.
Signed-off-by: Srirangan Madhavan <smadhavan@nvidia.com>
---
Changes in v18:
- Check the CXL Reset preconditions in the prepare step, under the lock,
instead of early in cxl_reset_function(), and refuse with errors other
than -ENOTTY, so a refused reset no longer falls back to FLR or PM
reset (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=13
- Record Normalized Addressing even when the root decoder lookup fails,
and keep it when a decoder refresh on rebind rebuilds the cached
settings (Sashiko)
https://sashiko.dev/#/patchset/20261105185917.2667656-1-smadhavan@nvidia.com?part=13
- Take cxl_rwsem.dpa in the cache setters here, where reset first reads
the cache concurrently with endpoint probe (Li Ming)
https://lore.kernel.org/linux-cxl/a2226978-8200-41f9-9763-7916b946899b@zohomail.com/
- Do not hold the global cxl_rwsem.region lock across the reset. Take it
only to snapshot the cache and reserve the ranges, to mark the
registers offline right before the reset, and to clear the marks after
the second invalidation; while the new per-device reset_pending mark is
set, cxl_decoder_commit() fails for the device's endpoint decoders. The
Memory Clear wait and the cache flushes run without the lock (Dave)
https://lore.kernel.org/linux-cxl/5616c825-454e-4eba-9352-b06d98dc123a@intel.com/
- Refuse reset when CXL.cache is enabled but the device cannot write back
and invalidate its cache, in both the CXL Reset and bus reset paths,
since reset would discard dirty device-cache lines (AI review)
- Do not access the HDM decoder registers while the device is in reset
when a region is torn down; update only the software state and the
cache. A separate regs_offline mark covers just that window, so a
teardown outside it still writes the registers (AI review)
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=5944c6e61f14c0dd28d54537be15edb2cadb0e8b
---
drivers/cxl/Kconfig | 3 +-
drivers/cxl/core/hdm.c | 52 +++++-
drivers/cxl/core/hdm_state.c | 341 ++++++++++++++++++++++++++++++++++-
drivers/cxl/core/region.c | 15 ++
drivers/pci/cxl.c | 159 ++++++++++++++--
include/cxl/hdm.h | 56 +++++-
6 files changed, 600 insertions(+), 26 deletions(-)
diff --git a/drivers/cxl/Kconfig b/drivers/cxl/Kconfig
index 37bfa6689b04..2211f295e1ed 100644
--- a/drivers/cxl/Kconfig
+++ b/drivers/cxl/Kconfig
@@ -226,7 +226,8 @@ config CXL_REGION_INVALIDATION_TEST
invalidation mechanism. For example usage of wbinvd is restricted to
bare metal x86. However, for testing purposes toggling this option
can disable that data integrity safety and proceed with enabling
- regions when there might be conflicting contents in the CPU cache.
+ regions or performing CXL reset when there might be conflicting
+ contents in the CPU cache.
If unsure, or if this kernel is meant for production environments,
say N.
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index f88418177582..ebc9995eec1b 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));
}
+/*
+ * While the device is in CXL Reset or behind a secondary bus reset its
+ * registers must not be accessed. The reset clears the decoders anyway, and
+ * the restore uncommits any decoder the cache shows as disabled.
+ */
+static bool cxl_decoder_regs_offline(struct cxl_decoder *cxld)
+{
+ struct cxl_port *port = to_cxl_port(cxld->dev.parent);
+
+ if (!is_endpoint_decoder(&cxld->dev))
+ return false;
+
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(port);
+
+ return pdev && cxl_hdm_cache_is_regs_offline(pdev);
+}
+
static void
cxl_endpoint_decoder_snapshot(struct cxl_endpoint_decoder *cxled,
struct cxl_endpoint_decoder_settings *settings)
@@ -818,6 +839,19 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)
return -EBUSY;
}
+ /* Don't commit a new decoder between reset prepare and restore. */
+ if (is_endpoint_decoder(&cxld->dev)) {
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(port);
+
+ if (pdev && cxl_hdm_cache_is_reset_pending(pdev)) {
+ dev_dbg(&port->dev,
+ "attempted to commit %s during reset\n",
+ dev_name(&cxld->dev));
+ return -EBUSY;
+ }
+ }
+
/*
* For endpoint decoders hosted on CXL memory devices that
* support the sanitize operation, make sure sanitize is not in-flight.
@@ -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));
+ }
cxld->config.flags &= ~CXL_DECODER_F_ENABLE;
cxl_hdm_refresh_decoder(cxlhdm, cxld);
diff --git a/drivers/cxl/core/hdm_state.c b/drivers/cxl/core/hdm_state.c
index ddb69411de1c..497709bffff2 100644
--- a/drivers/cxl/core/hdm_state.c
+++ b/drivers/cxl/core/hdm_state.c
@@ -1,8 +1,14 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */
#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/errno.h>
#include <linux/export.h>
#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/list.h>
+#include <linux/memregion.h>
+#include <linux/overflow.h>
#include <linux/pci.h>
#include <linux/rwsem.h>
#include <linux/slab.h>
@@ -13,7 +19,8 @@
/*
* CXL state that must stay available while no CXL driver is bound: the CXL
- * locks and the per-device HDM cache filled during PCI enumeration.
+ * locks, the per-device HDM cache filled during PCI enumeration, and the
+ * region quiesce used around a reset.
*/
struct cxl_rwsem cxl_rwsem = {
.region = __RWSEM_INITIALIZER(cxl_rwsem.region),
@@ -23,6 +30,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_rwsem, "cxl_core");
void cxl_hdm_cache_set_dvsec_ctrl(struct pci_dev *pdev, u16 ctrl)
{
+ guard(rwsem_write)(&cxl_rwsem.dpa);
if (!pdev->hdm)
return;
@@ -42,6 +50,7 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_dvsec_ctrl, "cxl_core");
*/
void cxl_hdm_cache_set_global_ctrl(struct pci_dev *pdev, u32 ctrl)
{
+ guard(rwsem_write)(&cxl_rwsem.dpa);
if (pdev->hdm)
pdev->hdm->global_ctrl = ctrl;
}
@@ -50,7 +59,8 @@ EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_global_ctrl, "cxl_core");
/**
* cxl_hdm_cache_refresh_decoder() - update the PCI HDM cache from a decoder
* @pdev: PCI device that owns the HDM cache
- * @hdm: mapped HDM decoder register block, for the global control register
+ * @hdm: mapped HDM decoder register block, for the global control register,
+ * or NULL to leave the cached global control alone (the device is in reset)
* @cxled: endpoint decoder whose state is recorded
*
* Record the decoder's current configuration, or just its id when it is
@@ -64,6 +74,7 @@ void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
struct cxl_endpoint_decoder_settings *settings;
struct cxl_decoder *cxld = &cxled->cxld;
struct cxl_hdm_info *info;
+ unsigned long normalized;
u32 global_ctrl;
int id;
@@ -79,10 +90,13 @@ void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
}
/* Preserve the last good value when the device does not respond. */
- global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
- if (global_ctrl != U32_MAX)
- info->global_ctrl = global_ctrl;
+ if (hdm) {
+ global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
+ if (global_ctrl != U32_MAX)
+ info->global_ctrl = global_ctrl;
+ }
settings = &info->settings[id];
+ normalized = settings->config.flags & CXL_DECODER_F_NORMALIZED_ADDRESSING;
/*
* A disabled decoder's software object may retain its old range and
@@ -94,6 +108,8 @@ void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
};
if (cxld->config.flags & CXL_DECODER_F_ENABLE) {
settings->config = cxld->config;
+ /* Region discovery sets this; a refresh on rebind must keep it. */
+ settings->config.flags |= normalized;
settings->skip = cxled->skip;
}
}
@@ -123,6 +139,93 @@ bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count)
}
EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_decoder_count_match, "cxl_core");
+/**
+ * cxl_hdm_cache_set_normalized() - record Normalized Addressing in the cache
+ * @pdev: endpoint that owns the cache
+ * @id: endpoint decoder id
+ *
+ * Normalized Addressing is determined during region discovery, after the
+ * cache was filled, so the cached decoder lacks it. Record only that flag: the
+ * decoder's software lock must not become a hardware Lock On Commit when the
+ * cache is restored.
+ */
+void cxl_hdm_cache_set_normalized(struct pci_dev *pdev, int id)
+{
+ guard(rwsem_write)(&cxl_rwsem.dpa);
+ if (!pdev->hdm || id < 0 || id >= pdev->hdm->decoder_count)
+ return;
+
+ pdev->hdm->settings[id].config.flags |=
+ CXL_DECODER_F_NORMALIZED_ADDRESSING;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_normalized, "cxl_core");
+
+/**
+ * cxl_hdm_cache_set_reset_pending() - mark a device as between reset prepare
+ * and restore
+ * @pdev: endpoint that owns the cache
+ * @pending: set or clear the mark
+ *
+ * Callers hold cxl_rwsem.region for write, which also serializes this against
+ * cxl_decoder_commit(). While the mark is set, commits of the device's
+ * decoders fail, so no new decode can come online during the reset.
+ */
+void cxl_hdm_cache_set_reset_pending(struct pci_dev *pdev, bool pending)
+{
+ lockdep_assert_held_write(&cxl_rwsem.region);
+ guard(rwsem_write)(&cxl_rwsem.dpa);
+ if (pdev->hdm)
+ pdev->hdm->reset_pending = pending;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_reset_pending, "cxl_core");
+
+/**
+ * cxl_hdm_cache_is_reset_pending() - check whether a reset of @pdev is pending
+ * @pdev: endpoint that owns the cache
+ *
+ * Return: true between cxl_hdm_cache_set_reset_pending(pdev, true) and the
+ * matching clear.
+ */
+bool cxl_hdm_cache_is_reset_pending(struct pci_dev *pdev)
+{
+ guard(rwsem_read)(&cxl_rwsem.dpa);
+ return pdev->hdm && pdev->hdm->reset_pending;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_is_reset_pending, "cxl_core");
+
+/**
+ * cxl_hdm_cache_set_regs_offline() - mark the HDM registers of a device unusable
+ * @pdev: endpoint that owns the cache
+ * @offline: set or clear the mark
+ *
+ * Set only while the device is in reset, which is shorter than the pending
+ * mark. Callers hold cxl_rwsem.region for write. While the mark is set, a
+ * decoder teardown updates the software state and the cache but leaves the
+ * registers alone; the restore then uncommits what the cache shows disabled.
+ */
+void cxl_hdm_cache_set_regs_offline(struct pci_dev *pdev, bool offline)
+{
+ lockdep_assert_held_write(&cxl_rwsem.region);
+ guard(rwsem_write)(&cxl_rwsem.dpa);
+ if (pdev->hdm)
+ pdev->hdm->regs_offline = offline;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_set_regs_offline, "cxl_core");
+
+/**
+ * cxl_hdm_cache_is_regs_offline() - check whether the HDM registers are unusable
+ * @pdev: endpoint that owns the cache
+ *
+ * Return: true between cxl_hdm_cache_set_regs_offline(pdev, true) and the
+ * matching clear.
+ */
+bool cxl_hdm_cache_is_regs_offline(struct pci_dev *pdev)
+{
+ guard(rwsem_read)(&cxl_rwsem.dpa);
+ return pdev->hdm && pdev->hdm->regs_offline;
+}
+EXPORT_SYMBOL_FOR_MODULES(cxl_hdm_cache_is_regs_offline, "cxl_core");
+
/**
* cxl_hdm_cache_release() - free cached HDM state
* @slot: cache pointer owned by the device
@@ -135,3 +238,231 @@ void cxl_hdm_cache_release(struct cxl_hdm_info **slot)
kfree(*slot);
*slot = NULL;
}
+
+/**
+ * cxl_hdm_snapshot_is_dvsec_decoded() - check a snapshot for DVSEC-decoded memory
+ * @info: snapshot from cxl_hdm_cache_snapshot()
+ *
+ * Judge the snapshot itself, not the live cache, which endpoint probe and
+ * unbind can change after the snapshot was taken.
+ *
+ * Return: true if CXL.mem is enabled while HDM decoders are globally disabled.
+ */
+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);
+}
+
+/**
+ * cxl_hdm_cache_snapshot() - copy the published HDM state
+ * @slot: cache pointer owned by the device
+ *
+ * Return: a copy the caller frees with kfree(), ERR_PTR(-ENXIO) when nothing
+ * is published, or another ERR_PTR() on failure.
+ */
+struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot)
+{
+ struct cxl_hdm_info *info, *copy;
+ size_t size;
+
+ guard(rwsem_read)(&cxl_rwsem.dpa);
+ info = *slot;
+ if (!info)
+ return ERR_PTR(-ENXIO);
+ if (info->decoder_count <= 0)
+ return ERR_PTR(-EINVAL);
+
+ size = struct_size(info, settings, info->decoder_count);
+ if (size == SIZE_MAX)
+ return ERR_PTR(-EOVERFLOW);
+
+ copy = kmemdup(info, size, GFP_KERNEL);
+ if (!copy)
+ return ERR_PTR(-ENOMEM);
+
+ return copy;
+}
+
+void cxl_region_quiesce_lock(void)
+ __acquires(cxl_region_quiesce)
+ __context_unsafe(token maps to cxl_rwsem.region)
+{
+ down_write(&cxl_rwsem.region);
+ __acquire(cxl_region_quiesce);
+}
+
+void cxl_region_quiesce_unlock(void)
+ __releases(cxl_region_quiesce)
+ __context_unsafe(token maps to cxl_rwsem.region)
+{
+ __release(cxl_region_quiesce);
+ up_write(&cxl_rwsem.region);
+}
+
+/*
+ * Bound the range twice: request_mem_region() takes resource_size_t while
+ * cpu_cache_invalidate_memregion() takes size_t, and the two differ on
+ * 32-bit builds with CONFIG_PHYS_ADDR_T_64BIT. range_len() can also reach
+ * RESOURCE_SIZE_MAX + 1 for a full-width range, and wraps to zero when
+ * resource_size_t is 64-bit, which the !len test catches.
+ */
+static int cxl_hdm_range_validate(const struct range *hpa_range)
+{
+ u64 len = range_len(hpa_range);
+
+ if (!len)
+ return -EINVAL;
+ if (hpa_range->end > RESOURCE_SIZE_MAX || len > RESOURCE_SIZE_MAX ||
+ len > SIZE_MAX)
+ return -EOVERFLOW;
+
+ return 0;
+}
+
+static int cxl_hdm_range_add(struct cxl_hdm_quiesce *q,
+ const struct range *hpa_range)
+{
+ struct cxl_hdm_range *range, *next, *new_range;
+ int rc;
+
+ rc = cxl_hdm_range_validate(hpa_range);
+ if (rc)
+ return rc;
+
+ list_for_each_entry(range, &q->ranges, list)
+ if (range_contains(&range->hpa_range, hpa_range))
+ return 0;
+
+ new_range = kzalloc_obj(*new_range);
+ if (!new_range)
+ return -ENOMEM;
+
+ new_range->hpa_range = *hpa_range;
+ new_range->len = range_len(hpa_range);
+
+ list_for_each_entry_safe(range, next, &q->ranges, list) {
+ if (range_contains(hpa_range, &range->hpa_range)) {
+ list_del(&range->list);
+ kfree(range);
+ }
+ }
+ list_add_tail(&new_range->list, &q->ranges);
+
+ return 0;
+}
+
+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;
+}
+
+static int cxl_hdm_ranges_request(struct cxl_hdm_quiesce *q)
+{
+ struct cxl_hdm_range *range;
+
+ lockdep_assert_held_write(&cxl_rwsem.region);
+
+ list_for_each_entry(range, &q->ranges, list) {
+ const struct range *hpa_range = &range->hpa_range;
+
+ range->res = request_mem_region(hpa_range->start, range->len,
+ "cxl_reset");
+ if (!range->res)
+ return -EBUSY;
+ }
+
+ return 0;
+}
+
+static int cxl_hdm_ranges_invalidate(struct cxl_hdm_quiesce *q)
+{
+ struct cxl_hdm_range *range;
+ int rc = 0;
+
+ if (!cpu_cache_has_invalidate_memregion() &&
+ IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) {
+ pr_info_once("CXL reset: bypassing CPU cache invalidation for testing\n");
+ return 0;
+ }
+
+ list_for_each_entry(range, &q->ranges, list) {
+ const struct range *hpa_range = &range->hpa_range;
+ int rc2;
+
+ rc2 = cpu_cache_invalidate_memregion(hpa_range->start, range->len);
+ rc = rc ?: rc2;
+ }
+
+ return rc;
+}
+
+/**
+ * cxl_hdm_quiesce_prepare() - take the HPA ranges of a device out of use
+ * @q: quiesce state to initialize
+ * @snapshot: HDM state from cxl_hdm_cache_snapshot()
+ *
+ * Reserve every enabled system-physical range in @snapshot exclusively. The
+ * caller then drops cxl_rwsem.region and calls cxl_hdm_quiesce_flush() to
+ * write back and invalidate CPU caches for the ranges. Call
+ * cxl_hdm_quiesce_destroy() afterwards whether or not this succeeds.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+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);
+}
+
+/* Evict lines fetched during reset before DMA exclusion ends. */
+int cxl_hdm_quiesce_flush(struct cxl_hdm_quiesce *q)
+{
+ return cxl_hdm_ranges_invalidate(q);
+}
+
+void cxl_hdm_quiesce_destroy(struct cxl_hdm_quiesce *q)
+{
+ struct cxl_hdm_range *range, *next;
+
+ list_for_each_entry_safe(range, next, &q->ranges, list) {
+ list_del(&range->list);
+ if (range->res)
+ release_mem_region(range->hpa_range.start,
+ resource_size(range->res));
+ kfree(range);
+ }
+}
diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 5bdaeeb30f4a..314347d1911e 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -13,6 +13,7 @@
#include <linux/idr.h>
#include <linux/memory-tiers.h>
#include <linux/string_choices.h>
+#include <cxl/hdm.h>
#include <cxlmem.h>
#include <cxl.h>
#include "core.h"
@@ -3852,6 +3853,20 @@ int cxl_add_to_region(struct cxl_endpoint_decoder *cxled)
struct cxl_root_decoder *cxlrd __free(put_cxl_root_decoder) =
get_cxl_root_decoder(cxled, &ctx);
+ /*
+ * Translation setup may have marked the decoder as Normalized
+ * Addressing, after the PCI cache was filled. Record it for reset,
+ * even when the root decoder lookup failed.
+ */
+ if (cxled->cxld.config.flags & CXL_DECODER_F_NORMALIZED_ADDRESSING) {
+ struct pci_dev *pdev __free(pci_dev_put) =
+ cxl_port_get_uport_pci_dev(cxled_to_port(cxled));
+
+ if (pdev)
+ cxl_hdm_cache_set_normalized(pdev,
+ cxled->cxld.config.id);
+ }
+
if (IS_ERR(cxlrd))
return PTR_ERR(cxlrd);
diff --git a/drivers/pci/cxl.c b/drivers/pci/cxl.c
index 4d6cfabab2c0..c90715c5a6d5 100644
--- a/drivers/pci/cxl.c
+++ b/drivers/pci/cxl.c
@@ -15,6 +15,7 @@
#include <linux/iommu.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
+#include <linux/memregion.h>
#include <linux/pci.h>
#include <linux/slab.h>
@@ -23,6 +24,13 @@
#include "pci.h"
+struct cxl_pci_cache {
+ struct cxl_hdm_quiesce quiesce;
+ u16 saved_ctrl2;
+ /* A cache-policy write was attempted and may need undoing. */
+ bool cache_disabled;
+};
+
/*
* Read a config word. A failed read or an all-ones value means the device is
* not responding, so return an errno rather than a value to be interpreted.
@@ -229,6 +237,93 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
cxl_hdm_cache_release(&pdev->hdm);
}
+/*
+ * Prepare window. Hold the region lock only long enough to capture the HDM
+ * state, reserve its ranges and mark the device as resetting. The caller does
+ * the host and device cache maintenance after this returns, without the lock.
+ *
+ * Return: snapshot to be freed with kfree(), or an ERR_PTR() with the lock
+ * released, no reservation held and nothing marked.
+ */
+static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,
+ int dvsec, u16 cap,
+ struct cxl_pci_cache *cache)
+{
+ struct cxl_hdm_info *snapshot;
+ int rc;
+
+ rc = cxl_pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CTRL2,
+ &cache->saved_ctrl2);
+ if (rc)
+ return ERR_PTR(rc);
+
+ cxl_region_quiesce_lock();
+ snapshot = cxl_hdm_cache_snapshot(&pdev->hdm);
+ if (IS_ERR(snapshot)) {
+ pci_err(pdev, "CXL HDM decoder state unavailable: %ld\n",
+ PTR_ERR(snapshot));
+ cxl_region_quiesce_unlock();
+ return snapshot;
+ }
+ /* 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;
+ }
+
+ 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;
+
+free_snapshot:
+ kfree(snapshot);
+ cxl_region_quiesce_unlock();
+ return ERR_PTR(rc);
+}
+
+/*
+ * Offline window. Called right before the device is reset, so that a decoder
+ * teardown from here on does not access the registers of a device in reset. A
+ * teardown before this point still writes them.
+ */
+static void cxl_reset_regs_offline(struct pci_dev *pdev)
+{
+ cxl_region_quiesce_lock();
+ cxl_hdm_cache_set_regs_offline(pdev, true);
+ cxl_region_quiesce_unlock();
+}
+
+/*
+ * Unmark window. Once the host cache flush is done, or the reset is abandoned,
+ * decoders of this device may commit again.
+ */
+static void cxl_reset_hdm_unmark(struct pci_dev *pdev)
+{
+ cxl_region_quiesce_lock();
+ cxl_hdm_cache_set_regs_offline(pdev, false);
+ cxl_hdm_cache_set_reset_pending(pdev, false);
+ cxl_region_quiesce_unlock();
+}
+
/*
* CXL r4.0 sec 9.7.2 defines the reset completion timeout encodings.
* Sec 9.7.3 leaves config-space access behavior undefined for 100 ms after
@@ -333,6 +428,21 @@ static int cxl_reset_restore_cache_policy(struct pci_dev *pdev, int dvsec,
return rc;
}
+/* 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);
+}
+
static int cxl_reset_wait_done(struct pci_dev *pdev, int dvsec, u16 cap)
{
unsigned long deadline;
@@ -390,21 +500,25 @@ static int cxl_clear_memory(struct pci_dev *pdev, int dvsec, bool initiate)
PCI_DVSEC_CXL_RST_MEM_CLR_EN);
}
-static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
+static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap,
+ struct cxl_hdm_quiesce *q)
{
int rc, rc2;
if (!pci_wait_for_pending_transaction(pdev)) {
pci_err(pdev, "timed out waiting for pending transactions\n");
+ cxl_reset_hdm_unmark(pdev);
return -ETIMEDOUT;
}
rc = pci_dev_reset_iommu_prepare(pdev);
if (rc) {
pci_err(pdev, "failed to stop IOMMU for CXL reset: %d\n", rc);
+ cxl_reset_hdm_unmark(pdev);
return rc;
}
+ cxl_reset_regs_offline(pdev);
rc = cxl_clear_memory(pdev, dvsec, true);
if (!rc)
rc = cxl_reset_wait_done(pdev, dvsec, cap);
@@ -415,25 +529,48 @@ static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap)
pci_err(pdev, "failed to clear CXL Reset Memory Clear: %d\n", rc2);
rc = rc ?: rc2;
+ 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;
}
diff --git a/include/cxl/hdm.h b/include/cxl/hdm.h
index e424ccfcf152..eac5600d2849 100644
--- a/include/cxl/hdm.h
+++ b/include/cxl/hdm.h
@@ -3,10 +3,14 @@
#ifndef __CXL_HDM_H__
#define __CXL_HDM_H__
+#include <linux/list.h>
#include <linux/types.h>
+
#include <cxl/cxl.h>
+struct device;
struct pci_dev;
+struct resource;
struct cxl_endpoint_decoder;
/**
@@ -29,9 +33,12 @@ struct cxl_endpoint_decoder_settings {
* @dvsec: config space offset of the CXL Device DVSEC
* @dvsec_ctrl: CXL DVSEC control register
* @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state
+ * @reset_pending: a reset of the device is between prepare and unmark
+ * @regs_offline: the device is being reset; do not access its HDM registers
* @settings: per-endpoint-decoder programming state
*
- * Publication through pci_dev.hdm is serialized by the PCI device lock.
+ * Publication through pci_dev.hdm is serialized by the PCI device lock. All
+ * mutable fields are protected by cxl_rwsem.dpa.
*/
struct cxl_hdm_info {
int decoder_count;
@@ -42,6 +49,8 @@ struct cxl_hdm_info {
u16 dvsec;
u16 dvsec_ctrl;
bool dvsec_ctrl_valid;
+ bool reset_pending;
+ bool regs_offline;
struct cxl_endpoint_decoder_settings settings[] __counted_by(decoder_count);
};
@@ -56,6 +65,51 @@ void cxl_hdm_cache_set_global_ctrl(struct pci_dev *pdev, u32 ctrl);
void cxl_hdm_cache_refresh_decoder(struct pci_dev *pdev, void __iomem *hdm,
struct cxl_endpoint_decoder *cxled);
bool cxl_hdm_cache_decoder_count_match(struct pci_dev *pdev, int count);
+void cxl_hdm_cache_set_normalized(struct pci_dev *pdev, int id);
+void cxl_hdm_cache_set_reset_pending(struct pci_dev *pdev, bool pending);
+bool cxl_hdm_cache_is_reset_pending(struct pci_dev *pdev);
+void cxl_hdm_cache_set_regs_offline(struct pci_dev *pdev, bool offline);
+bool cxl_hdm_cache_is_regs_offline(struct pci_dev *pdev);
void cxl_hdm_cache_release(struct cxl_hdm_info **slot);
+bool cxl_hdm_snapshot_is_dvsec_decoded(const struct cxl_hdm_info *info);
+struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot);
+
+/*
+ * Region quiesce blocks CXL region commit and teardown, and with them decoder
+ * reprogramming, while reset handling captures or restores HDM decoder state.
+ */
+token_context_lock(cxl_region_quiesce);
+
+/**
+ * struct cxl_hdm_range - HPA range held exclusively across a reset
+ * @list: entry in &cxl_hdm_quiesce.ranges
+ * @hpa_range: system physical address range decoded by the device
+ * @len: length of @hpa_range
+ * @res: reservation of @hpa_range, once requested
+ */
+struct cxl_hdm_range {
+ struct list_head list;
+ struct range hpa_range;
+ u64 len;
+ struct resource *res;
+};
+
+/**
+ * struct cxl_hdm_quiesce - HPA ranges held exclusively across a reset
+ * @ranges: list of &struct cxl_hdm_range, see cxl_hdm_quiesce_prepare()
+ */
+struct cxl_hdm_quiesce {
+ struct list_head ranges;
+};
+
+void cxl_region_quiesce_lock(void)
+ __acquires(cxl_region_quiesce);
+void cxl_region_quiesce_unlock(void)
+ __releases(cxl_region_quiesce);
+int cxl_hdm_quiesce_prepare(struct cxl_hdm_quiesce *q,
+ const struct cxl_hdm_info *snapshot)
+ __must_hold(cxl_region_quiesce);
+int cxl_hdm_quiesce_flush(struct cxl_hdm_quiesce *q);
+void cxl_hdm_quiesce_destroy(struct cxl_hdm_quiesce *q);
#endif /* __CXL_HDM_H__ */
--
2.43.0
next prev parent reply other threads:[~2026-12-05 4:15 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 ` Srirangan Madhavan [this message]
2026-12-05 4:47 ` [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset 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
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=20261205041453.3946529-14-smadhavan@nvidia.com \
--to=smadhavan@nvidia.com \
--cc=alejandro.lucero-palau@amd.com \
--cc=alex.williamson@redhat.com \
--cc=alison.schofield@intel.com \
--cc=alwilliamson@nvidia.com \
--cc=bhelgaas@google.com \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=icheng@nvidia.com \
--cc=iweiny@kernel.org \
--cc=jan@nvidia.com \
--cc=jic23@kernel.org \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-tegra@vger.kernel.org \
--cc=machaujo@amazon.de \
--cc=mhonap@nvidia.com \
--cc=ming.li@zohomail.com \
--cc=skancherla@nvidia.com \
--cc=vaslot@nvidia.com \
--cc=vishal.l.verma@intel.com \
--cc=vsethi@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