PCI/CXL: Add CXL reset support for Type 2 devices
Subsystems (3)
v18 cover PCI/CXL: Add CXL reset support for Type 2 devices
Hi folks,
This series adds CXL Reset for Type 2 devices through the existing PCI reset_method and reset sysfs attributes. It disables CXL.cache, performs supported cache writeback, initiates CXL Reset with Memory Clear, and restores cached HDM decoder and CXL Device DVSEC state before PCI use resumes. Active HDM ranges are reserved and CPU caches invalidated before and after reset. The method remains limited to eligible single-function devices with both CXL.cache and CXL.mem enabled. CXL bus reset restores the same cached state and flushes the host and device caches around the reset.
The reset sequence follows the CXL 4.0 specification [5], including CXL.cache disable, optional cache writeback, CXL Reset Memory Clear, ResetComplete polling, and ResetError reporting. Sections 9.7.2 and 9.7.3 define the completion timeout encodings and reset access restrictions.
The userspace ABI is the existing PCI reset interface:
/sys/bus/pci/devices/.../reset_method
/sys/bus/pci/devices/.../resetUserspace selects "cxl_reset" in reset_method and triggers the reset through reset. Following Dan's v6 feedback, this uses the PCI interface instead of adding a memdev-specific sysfs ABI.
For sysfs reset, the generic PCI path invokes the driver's reset_prepare() callback when provided and disables PCI I/O, memory decoding, and bus mastering. The CXL path reserves affected HDM ranges exclusively and blocks IOMMU translations where applicable. It does not offline memory or revoke mappings: userspace is expected to offline CXL.mem users before reset. The reservation enforces this for claimed ranges by failing the reset while a conflicting resource is claimed.
Locking changed in v18. The global region lock is no longer held across the reset. Both reset paths take cxl_rwsem.region only for four short windows:
prepare: snapshot the cache, validate and reserve the HPA ranges,
set reset_pending
(no region lock) host cache flush, device cache writeback
offline: set regs_offline
(no region lock) reset, Memory Clear wait for cxl_reset
restore: re-read the cached decoders, restore the state,
clear regs_offline
(no region lock) host cache flush
unmark: clear reset_pending
While reset_pending is set, cxl_decoder_commit() returns -EBUSY for that device, so no region using it can commit in the middle of a reset. While regs_offline is set, a region teardown only updates the software state and the cache and leaves the registers alone; the restore then programs the decoders from the updated cache. Dave's lockdep reproducer [6] (a region read, a device remove and a slot power-off during the reset) no longer hangs.
Changes from v17 [1]:
- Rebased onto cxl/for-7.5/cxl-kunit (Alison)
- Reworked the locking as described above, for both cxl_reset and cxl_bus (Dave, Alison) [6][7]. disable_hdm() now also updates the cached global control, and the restore re-reads the decoders from the cache (Dave)
- Fixes from the Sashiko review [10]: the reset preconditions are checked in the prepare step, so a refused CXL reset no longer falls back to FLR; DVSEC-decoded memory is judged from the snapshot; Normalized Addressing survives decoder refreshes; CXL.cache stays off until the host caches are flushed; a bus reset checks that it can run before touching the device; function 0 of an ARI device is rejected when other functions share the bus; all-ones config reads are rejected when filling the cache
- cxl_reset now refuses, as cxl_bus already did, when CXL.cache is enabled but the device cannot write back its cache
- Smaller cleanups in patches 2 and 9-12 (Dave, Li Ming). Each patch has the details.
- Dropped the Assisted-by trailers and added Dave's Reviewed-by to patches 7 and 8
Still open: Alison's question about resetting before Normalized Addressing is known [8] and Alejandro's CXL.io/IOMMU question [9]. I'll follow up in those threads. Earlier history is in the v16 [2] and v15
[3] cover letters.
HDM caching needs the CXL.cache, CXL.mem, CXL Reset and Reset Memory Clear capabilities, and CXL bus reset restores its state from the same cache.
CXL Reset also needs a CPU cache clean/invalidate provider, such as the SMCCC series [4]. Without one, cxl_reset is still listed but fails with -ENXIO and does not fall back to FLR, unless the test-only CXL_REGION_INVALIDATION_TEST option is set.
The KUnit suite covers HDM range boundaries, zero-size and uncommitted decoders, lock and target-type decoding, and 12-way interleave.
AI coding assistants were used to help write and review parts of this series, and an AI review found two of the v18 fixes. I have reviewed all AI-assisted changes.
Testing: I ran v18 in QEMU with two cxl-type2 devices behind two CXL host bridges and lockdep on, for both cxl_reset and cxl_bus, with a delay added in the prepare step. A region read and a device remove complete during the delay, and lockdep reports nothing for the CXL locks. The pciehp slot power-off path still shows the existing pci_rescan_remove_lock vs reset_lock warning, which also shows up without this series. Each patch builds with W=1, and Clang 23 context analysis is clean for the touched files. The CXL KUnit suites, including cxl_hdm_unpack_kunit, pass.
On hardware, I ran v18 with the SMCCC provider series [4] (provider in use, CXL_REGION_INVALIDATION_TEST off) on an ARM64 system with four CXL Type 2 devices and the vendor driver bound. On each device I onlined the memory as a NUMA node, left dirty CPU cache lines in it, offlined it, and then issued the driver's CXL reset and CXL bus reset requests (which select cxl_reset and cxl_bus) once each. All of them succeeded. Afterwards all four devices still had the CXL DVSEC, CXL.cache and CXL.mem were still enabled on the device I checked, and a CPU fill and verify of the device memory matched.
[1] https://lore.kernel.org/linux-cxl/[email protected]/
[2] https://lore.kernel.org/linux-cxl/[email protected]/
[3] https://lore.kernel.org/linux-cxl/[email protected]/
[4] https://lore.kernel.org/linux-arm-kernel/[email protected]/
[5] https://computeexpresslink.org/wp-content/uploads/2026/02/CXL-Specification_rev4p0_ver1p0_2026February26_clean_evalcopy_v2.pdf
[6] https://lore.kernel.org/linux-cxl/[email protected]/
[7] https://lore.kernel.org/linux-cxl/[email protected]/
[8] https://lore.kernel.org/linux-cxl/[email protected]/
[9] https://lore.kernel.org/linux-cxl/[email protected]/
[10] https://sashiko.dev/#/patchset/[email protected]
Srirangan Madhavan (19):
cxl: Clarify decoder interleave limit
cxl: Share CXL port upstream PCI device lookup
cxl: Move decoder declarations to shared header
cxl: Embed decoder configuration in a standalone structure
cxl: Introduce endpoint HDM decoder settings
cxl: Move HDM decoder helpers to built-in code
cxl: Share HDM decoder register unpacking
cxl: Move cxl_rwsem into built-in code
cxl: Refresh cached PCI HDM decoder settings
cxl: Build register block helpers into the kernel PCI/CXL: Cache endpoint HDM state during PCI enumeration PCI/CXL: Add CXL Device Reset sequencing
cxl: Validate and synchronize HDM ranges around reset PCI/CXL: Reject reset with unsafe function scope PCI/CXL: Restore CXL state after PCI reset PCI/CXL: Expose CXL Reset as a PCI reset method PCI/CXL: Restore CXL state after CXL bus reset PCI/CXL: Flush host and device caches for CXL bus reset
cxl: Add KUnit checks for HDM decoder unpacking
MAINTAINERS | 1 + drivers/cxl/Kconfig | 8 +- drivers/cxl/acpi.c | 26 +- drivers/cxl/core/Makefile | 2 +- drivers/cxl/core/atl.c | 4 +- drivers/cxl/core/core.h | 15 + drivers/cxl/core/hdm.c | 315 ++++---- drivers/cxl/core/hdm_regs.c | 443 +++++++++++ drivers/cxl/core/hdm_state.c | 499 +++++++++++++ drivers/cxl/core/pci.c | 140 ++-- drivers/cxl/core/port.c | 62 +- drivers/cxl/core/region.c | 117 +-- drivers/cxl/core/regs.c | 9 + drivers/cxl/cxl.h | 46 +- drivers/cxl/pci.c | 2 +- drivers/cxl/port.c | 4 +- drivers/cxl/tests/Makefile | 3 + drivers/cxl/tests/hdm_unpack_kunit.c | 91 +++ drivers/pci/Makefile | 1 + drivers/pci/bus.c | 1 + drivers/pci/cxl.c | 1029 ++++++++++++++++++++++++++ drivers/pci/pci.c | 57 +- drivers/pci/pci.h | 51 ++ drivers/pci/probe.c | 1 + include/cxl/cxl.h | 57 +- include/cxl/hdm.h | 119 +++ include/cxl/pci.h | 13 + include/linux/pci.h | 8 +- include/uapi/linux/pci_regs.h | 14 + tools/testing/cxl/Kbuild | 1 - tools/testing/cxl/test/cxl.c | 158 ++-- 31 files changed, 2850 insertions(+), 447 deletions(-) create mode 100644 drivers/cxl/core/hdm_regs.c create mode 100644 drivers/cxl/core/hdm_state.c create mode 100644 drivers/cxl/tests/hdm_unpack_kunit.c create mode 100644 drivers/pci/cxl.c create mode 100644 include/cxl/hdm.h
base-commit: 533928808ac1e6a96a7daa74b6ad68c3f7f43a28
Signature · 1 line
2.43.0
PATCH v18 1/19 cxl: Clarify decoder interleave limit
CXL 2.0 added 16-way memory interleaving, making the comment that the specification supports only 8-way interleaving stale. Explain that 16 is the largest interleave a decoder can express, without changing the limit.
--- drivers/cxl/cxl.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/cxl/cxl.h +1 -4
@@ -275,10 +275,7 @@ enum cxl_decoder_type { CXL_DECODER_HOSTONLYMEM = 3, }; -/* -* Current specification goes up to 8, double that seems a reasonable -* software max for the foreseeable future -*/ +/* 16 is the largest interleave a decoder can express (CXL 4.0 8.2.4.20.7). */ #define CXL_DECODER_MAX_INTERLEAVE 16#define CXL_QOS_CLASS_INVALID -1
Signature · 1 line
2.43.0
PATCH v18 2/19 cxl: Share CXL port upstream PCI device lookup
In reply to Srirangan Madhavanread_cdat_data() resolves a CXL port's upstream device to its backing PCI device. HDM cache updates need the same mapping.
Factor the lookup into cxl_port_get_uport_pci_dev() and return a referenced PCI device to make caller ownership explicit.
Use automatic reference cleanup in read_cdat_data(), as suggested by Li Ming, so early returns release the PCI device reference.
---
Changes in v18:
- Restructure read_cdat_data() around a cxl_cdat_read_validate() helper and __free(pci_dev_put), removing the goto/out layout (Dave Jiang)
--- drivers/cxl/core/core.h | 1 + drivers/cxl/core/pci.c | 67 ++++++++++++++++++----------------------- drivers/cxl/core/port.c | 26 ++++++++++++++++ 3 files changed, 57 insertions(+), 37 deletions(-)
drivers/cxl/core/core.h +1 -0
@@ -157,6 +157,7 @@ long cxl_pci_get_latency(struct pci_dev *pdev);int cxl_pci_get_bandwidth (struct pci_dev *pdev,struct access_coordinate *c);int cxl_port_get_switch_dport_bandwidth (struct cxl_port *port,struct access_coordinate *c); +struct pci_dev *cxl_port_get_uport_pci_dev (struct cxl_port *port);static inline struct device *port_to_host (struct cxl_port *port) {
drivers/cxl/core/pci.c +30 -37
92 lines not on this page · raw message
drivers/cxl/core/port.c +26 -0
@@ -34,6 +34,32 @@static DEFINE_IDA (cxl_port_ida);static DEFINE_XARRAY (cxl_root_buses); +/** +* cxl_port_get_uport_pci_dev - get the PCI device for a port's upstream device +* @port: CXL port to map to a PCI device +* +* Return: A referenced PCI device, or NULL if the upstream device is not PCI. +* The caller must release the returned reference with pci_dev_put(). +*/ +struct pci_dev *cxl_port_get_uport_pci_dev (struct cxl_port *port) +{ +struct device *uport = port->uport_dev; +struct device *host; + +if (is_cxl_memdev (uport)) { +struct cxl_memdev *cxlmd =to_cxl_memdev (uport); + + host = cxlmd->dev.parent; + }else { + host = uport; + } + +if (!host || !dev_is_pci (host)) +return NULL ; + +return pci_dev_get (to_pci_dev (host)); +} +/* * The terminal device in PCI is NULL and @platform_bus * for platform devices (for cxl_test)
Signature · 1 line
2.43.0
PATCH v18 3/19 cxl: Move decoder declarations to shared header
In reply to Srirangan MadhavanMove the common decoder declarations to include/cxl/cxl.h for use by shared decoder settings. Preserve their layout and documentation.
--- drivers/cxl/cxl.h | 38 -------------------------------------- include/cxl/cxl.h | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 39 deletions(-)
drivers/cxl/cxl.h +0 -38
@@ -270,46 +270,8 @@ int cxl_dport_map_rcd_linkcap(struct pci_dev *pdev, struct cxl_dport *dport);#define CXL_DECODER_F_NORMALIZED_ADDRESSING BIT(6)#define CXL_DECODER_F_RESET_MASK (CXL_DECODER_F_ENABLE | CXL_DECODER_F_LOCK) -enum cxl_decoder_type { -CXL_DECODER_DEVMEM =2 , -CXL_DECODER_HOSTONLYMEM =3 , -}; - -/* 16 is the largest interleave a decoder can express (CXL 4.0 8.2.4.20.7). */ -#define CXL_DECODER_MAX_INTERLEAVE 16 -#define CXL_QOS_CLASS_INVALID -1 -/** -* struct cxl_decoder - Common CXL HDM Decoder Attributes -* @dev: this decoder's device -* @id: kernel device name id -* @hpa_range: Host physical address range mapped by this decoder -* @interleave_ways: number of cxl_dports in this decode -* @interleave_granularity: data stride per dport -* @target_type: accelerator vs expander (type2 vs type3) selector -* @region: currently assigned region for this decoder -* @flags: memory type capabilities and locking -* @target_map: cached copy of hardware port-id list, available at init -* before all @dport objects have been instantiated. While -* dport id is 8bit, CFMWS interleave targets are 32bits. -* @commit: device/decoder-type specific callback to commit settings to hw -* @reset: device/decoder-type specific callback to reset hw settings -*/ -struct cxl_decoder { -struct device dev; -int id; -struct range hpa_range; -int interleave_ways; -int interleave_granularity; -enum cxl_decoder_type target_type; -struct cxl_region *region; -unsigned long flags; -u32 target_map[CXL_DECODER_MAX_INTERLEAVE ]; -int (*commit)(struct cxl_decoder *cxld); -void (*reset)(struct cxl_decoder *cxld); -}; -/* * Track whether this decoder is free for userspace provisioning, reserved for * region autodiscovery, whether it is started connecting (awaiting other
include/cxl/cxl.h +41 -1
56 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v18 4/19 cxl: Embed decoder configuration in a standalone structure
In reply to Srirangan MadhavanDefine cxl_decoder_config independently and embed it in cxl_decoder. Update decoder field accesses without changing behavior, so reset snapshots can reuse the configuration type without struct_group_tagged().
--- drivers/cxl/acpi.c | 26 +++--- drivers/cxl/core/atl.c | 4 +- drivers/cxl/core/hdm.c | 118 +++++++++++++------------- drivers/cxl/core/pci.c | 8 +- drivers/cxl/core/port.c | 36 ++++---- drivers/cxl/core/region.c | 102 +++++++++++----------- drivers/cxl/cxl.h | 2 +- drivers/cxl/pci.c | 2 +- drivers/cxl/port.c | 4 +- include/cxl/cxl.h | 29 +++++-- tools/testing/cxl/test/cxl.c | 158 +++++++++++++++++------------------ 11 files changed, 248 insertions(+), 241 deletions(-)
drivers/cxl/acpi.c +13 -13
@@ -109,16 +109,16 @@ static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,return rc;/* Does this CXIMS entry apply to the given CXL Window? */ -if (hbig != cxld->interleave_granularity) +if (hbig != cxld->config.interleave_granularity)return 0 ;/* IW 1,3 do not use xormaps and skip this parsing entirely */ -if (is_power_of_2 (cxld->interleave_ways)) +if (is_power_of_2 (cxld->config.interleave_ways))/* 2, 4, 8, 16 way */ - nr_maps =ilog2 (cxld->interleave_ways); + nr_maps =ilog2 (cxld->config.interleave_ways);else /* 6, 12 way */ - nr_maps =ilog2 (cxld->interleave_ways /3 ); + nr_maps =ilog2 (cxld->config.interleave_ways /3 );if (cxims->nr_xormaps < nr_maps) { dev_dbg(dev, "CXIMS nr_xormaps[%d] expected[%d]\n",
@@ -369,7 +369,7 @@ static int add_or_reset_cxl_resource(struct resource *parent, struct resource *rstatic void cxl_setup_extended_linear_cache (struct cxl_root_decoder *cxlrd) {struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; -struct range *hpa = &cxld->hpa_range; +struct range *hpa = &cxld->config.hpa_range;resource_size_t size =range_len (hpa);resource_size_t start = hpa->start;resource_size_t cache_size;
@@ -442,13 +442,13 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,return PTR_ERR (cxlrd); cxld = &cxlrd->cxlsd.cxld; - cxld->flags =cfmws_to_decoder_flags (cfmws->restrictions); - cxld->target_type =CXL_DECODER_HOSTONLYMEM ; - cxld->hpa_range = (struct range ) { + cxld->config.flags =cfmws_to_decoder_flags (cfmws->restrictions); + cxld->config.target_type =CXL_DECODER_HOSTONLYMEM ; + cxld->config.hpa_range = (struct range ) { .start = cfmws->base_hpa, .end = cfmws->base_hpa + cfmws->window_size -1 , }; - cxld->interleave_ways = ways; + cxld->config.interleave_ways = ways;for (i =0 ; i < ways; i++) cxld->target_map[i] = cfmws->interleave_targets[i];/*
@@ -457,7 +457,7 @@ static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws,*/ if (ways ==1 ) ig =CXL_DECODER_MIN_GRANULARITY ; - cxld->interleave_granularity = ig; + cxld->config.interleave_granularity = ig;if (cfmws->interleave_arithmetic ==ACPI_CEDT_CFMWS_ARITHMETIC_XOR ) {if (ways !=1 && ways !=3 ) {
@@ -758,7 +758,7 @@ static int add_root_nvdimm_bridge(struct device *match, void *data)return 0 ; cxld =to_cxl_decoder (match); -if (!(cxld->flags &CXL_DECODER_F_PMEM )) +if (!(cxld->config.flags &CXL_DECODER_F_PMEM ))return 0 ; cxl_nvb =devm_cxl_add_nvdimm_bridge (host, root_port);
@@ -875,8 +875,8 @@ static int pair_cxl_resource(struct device *dev, void *data)struct cxl_root_decoder *cxlrd =to_cxl_root_decoder (dev);struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;struct resource res = { - .start = cxld->hpa_range.start, - .end = cxld->hpa_range.end, + .start = cxld->config.hpa_range.start, + .end = cxld->config.hpa_range.end, .flags =IORESOURCE_MEM , };
drivers/cxl/core/atl.c +2 -2
@@ -172,8 +172,8 @@ static int cxl_prm_setup_root(struct cxl_root *cxl_root, void *data)* * Indicate that Normalized Addressing is enabled. */ - cxld->flags |=CXL_DECODER_F_LOCK ; - cxld->flags |=CXL_DECODER_F_NORMALIZED_ADDRESSING ; + cxld->config.flags |=CXL_DECODER_F_LOCK ; + cxld->config.flags |=CXL_DECODER_F_NORMALIZED_ADDRESSING ; ctx->hpa_range = hpa_range; ctx->interleave_ways = ways;
drivers/cxl/core/hdm.c +59 -59
342 lines not on this page · raw message
drivers/cxl/core/pci.c +4 -4
@@ -231,10 +231,10 @@ static int dvsec_range_allowed(struct device *dev, const void *arg) cxld =to_cxl_decoder (dev); -if (!(cxld->flags &CXL_DECODER_F_RAM )) +if (!(cxld->config.flags &CXL_DECODER_F_RAM ))return 0 ; -return range_contains (&cxld->hpa_range, dev_range); +return range_contains (&cxld->config.hpa_range, dev_range); }static void disable_hdm (void *_cxlhdm)
@@ -673,7 +673,7 @@ static int __cxl_endpoint_decoder_reset_detected(struct device *dev, void *data)return 0 ; cxld =to_cxl_decoder (dev); -if ((cxld->flags &CXL_DECODER_F_ENABLE ) ==0 ) +if ((cxld->config.flags &CXL_DECODER_F_ENABLE ) ==0 )return 0 ;/*
@@ -687,7 +687,7 @@ static int __cxl_endpoint_decoder_reset_detected(struct device *dev, void *data) cxlhdm =dev_get_drvdata (&port->dev); hdm = cxlhdm->regs.hdm_decoder; - ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->id)); + ctrl =readl (hdm +CXL_HDM_DECODER0_CTRL_OFFSET (cxld->config.id));return !FIELD_GET (CXL_HDM_DECODER0_CTRL_COMMITTED , ctrl); }
drivers/cxl/core/port.c +18 -18
@@ -129,7 +129,7 @@ static ssize_t start_show(struct device *dev, struct device_attribute *attr, {struct cxl_decoder *cxld =to_cxl_decoder (dev); -return sysfs_emit (buf,"%#llx\n" , cxld->hpa_range.start); +return sysfs_emit (buf,"%#llx\n" , cxld->config.hpa_range.start); }static DEVICE_ATTR_ADMIN_RO (start);
@@ -138,7 +138,7 @@ static ssize_t size_show(struct device *dev, struct device_attribute *attr, {struct cxl_decoder *cxld =to_cxl_decoder (dev); -return sysfs_emit (buf,"%#llx\n" ,range_len (&cxld->hpa_range)); +return sysfs_emit (buf,"%#llx\n" ,range_len (&cxld->config.hpa_range)); }static DEVICE_ATTR_RO (size);
@@ -149,7 +149,7 @@ static ssize_t name##_show(struct device *dev, \struct cxl_decoder *cxld =to_cxl_decoder (dev); \ \return sysfs_emit (buf,"%s\n" , \ - (cxld->flags & (flag)) ?"1" :"0" ); \ + (cxld->config.flags & (flag)) ?"1" :"0" ); \ } \static DEVICE_ATTR_RO (name)
@@ -164,7 +164,7 @@ static ssize_t target_type_show(struct device *dev, {struct cxl_decoder *cxld =to_cxl_decoder (dev); -switch (cxld->target_type) { +switch (cxld->config.target_type) {case CXL_DECODER_DEVMEM :return sysfs_emit (buf,"accelerator\n" );case CXL_DECODER_HOSTONLYMEM :
@@ -180,7 +180,7 @@ static DEVICE_ATTR_RO(target_type);*/ static int cxlsd_nr_used_targets (struct cxl_switch_decoder *cxlsd) { -return min (cxlsd->cxld.interleave_ways, cxlsd->nr_targets); +return min (cxlsd->cxld.config.interleave_ways, cxlsd->nr_targets); }static ssize_t emit_target_list (struct cxl_switch_decoder *cxlsd,char *buf)
@@ -323,7 +323,7 @@ static ssize_t interleave_granularity_show(struct device *dev, {struct cxl_decoder *cxld =to_cxl_decoder (dev); -return sysfs_emit (buf,"%d\n" , cxld->interleave_granularity); +return sysfs_emit (buf,"%d\n" , cxld->config.interleave_granularity); }static DEVICE_ATTR_RO (interleave_granularity);
@@ -333,7 +333,7 @@ static ssize_t interleave_ways_show(struct device *dev, {struct cxl_decoder *cxld =to_cxl_decoder (dev); -return sysfs_emit (buf,"%d\n" , cxld->interleave_ways); +return sysfs_emit (buf,"%d\n" , cxld->config.interleave_ways); }static DEVICE_ATTR_RO (interleave_ways);
@@ -377,14 +377,14 @@ static bool can_create_pmem(struct cxl_root_decoder *cxlrd) {unsigned long flags =CXL_DECODER_F_TYPE3 |CXL_DECODER_F_PMEM ; -return (cxlrd->cxlsd.cxld.flags & flags) == flags; +return (cxlrd->cxlsd.cxld.config.flags & flags) == flags; }static bool can_create_ram (struct cxl_root_decoder *cxlrd) {unsigned long flags =CXL_DECODER_F_TYPE3 |CXL_DECODER_F_RAM ; -return (cxlrd->cxlsd.cxld.flags & flags) == flags; +return (cxlrd->cxlsd.cxld.config.flags & flags) == flags; }static umode_t cxl_root_decoder_visible (struct kobject *kobj,struct attribute *a,int n)
@@ -459,7 +459,7 @@ static void __cxl_decoder_release(struct cxl_decoder *cxld) {struct cxl_port *port =to_cxl_port (cxld->dev.parent); -ida_free (&port->decoder_ida, cxld->id); +ida_free (&port->decoder_ida, cxld->config.id);put_device (&port->dev); }
@@ -2071,7 +2071,7 @@ static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld)/* need parent to stick around to release the id */ get_device (&port->dev); - cxld->id = rc; + cxld->config.id = rc; dev = &cxld->dev;device_initialize (dev);
@@ -2081,10 +2081,10 @@ static int cxl_decoder_init(struct cxl_port *port, struct cxl_decoder *cxld) dev->bus = &cxl_bus_type;/* Pre initialize an "empty" decoder */ - cxld->interleave_ways =1 ; - cxld->interleave_granularity =PAGE_SIZE ; - cxld->target_type =CXL_DECODER_HOSTONLYMEM ; - cxld->hpa_range = (struct range ) { + cxld->config.interleave_ways =1 ; + cxld->config.interleave_granularity =PAGE_SIZE ; + cxld->config.target_type =CXL_DECODER_HOSTONLYMEM ; + cxld->config.hpa_range = (struct range ) { .start =0 , .end =-1 , };
@@ -2255,7 +2255,7 @@ int cxl_decoder_add_locked(struct cxl_decoder *cxld)if (WARN_ON_ONCE (IS_ERR (cxld)))return PTR_ERR (cxld); -if (cxld->interleave_ways <1 ) +if (cxld->config.interleave_ways <1 )return -EINVAL ; dev = &cxld->dev;
@@ -2265,14 +2265,14 @@ int cxl_decoder_add_locked(struct cxl_decoder *cxld)struct cxl_switch_decoder *cxlsd =to_cxl_switch_decoder (dev); rc =decoder_populate_targets (cxlsd, port); -if (rc && (cxld->flags &CXL_DECODER_F_ENABLE )) { +if (rc && (cxld->config.flags &CXL_DECODER_F_ENABLE )) {dev_err (&port->dev,"Failed to populate active decoder targets\n" );return rc; } } - rc =dev_set_name (dev,"decoder%d.%d" , port->id, cxld->id); + rc =dev_set_name (dev,"decoder%d.%d" , port->id, cxld->config.id);if (rc)return rc;
drivers/cxl/core/region.c +51 -51
280 lines not on this page · raw message
drivers/cxl/cxl.h +1 -1
@@ -287,7 +287,7 @@ enum cxl_decoder_state {* struct cxl_endpoint_decoder - Endpoint / SPA to DPA decoder * @cxld: base cxl_decoder_object * @dpa_res: actively claimed DPA span of this decoder -* @skip: offset into @dpa_res where @cxld.hpa_range maps +* @skip: offset into @dpa_res where @cxld.config.hpa_range maps * @state: autodiscovery state * @part: partition index this decoder maps * @pos: interleave position in @cxld.region
drivers/cxl/pci.c +1 -1
@@ -1096,7 +1096,7 @@ static int cxl_endpoint_decoder_clear_reset_flags(struct device *dev, void *datareturn 0 ; cxled =to_cxl_endpoint_decoder (dev); - cxled->cxld.flags &= ~CXL_DECODER_F_RESET_MASK ; + cxled->cxld.config.flags &= ~CXL_DECODER_F_RESET_MASK ;return 0 ; }
drivers/cxl/port.c +2 -2
@@ -40,7 +40,7 @@ static int discover_region(struct device *dev, void *unused)return 0 ; cxled =to_cxl_endpoint_decoder (dev); -if ((cxled->cxld.flags &CXL_DECODER_F_ENABLE ) ==0 ) +if ((cxled->cxld.config.flags &CXL_DECODER_F_ENABLE ) ==0 )return 0 ;if (cxled->state !=CXL_DECODER_STATE_AUTO )
@@ -56,7 +56,7 @@ static int discover_region(struct device *dev, void *unused) rc =cxl_add_to_region (cxled);if (rc)dev_dbg (dev,"failed to add to region: %#llx-%#llx\n" , - cxled->cxld.hpa_range.start, cxled->cxld.hpa_range.end); + cxled->cxld.config.hpa_range.start, cxled->cxld.config.hpa_range.end);return 0 ; }
include/cxl/cxl.h +20 -9
@@ -36,15 +36,31 @@ enum cxl_decoder_type {#define CXL_DECODER_MAX_INTERLEAVE 16/** -* struct cxl_decoder - Common CXL HDM Decoder Attributes -* @dev: this decoder's device +* struct cxl_decoder_config - common CXL decoder configuration and state * @id: kernel device name id * @hpa_range: Host physical address range mapped by this decoder * @interleave_ways: number of cxl_dports in this decode * @interleave_granularity: data stride per dport * @target_type: accelerator vs expander (type2 vs type3) selector -* @region: currently assigned region for this decoder * @flags: memory type capabilities and locking +* +* This is not a complete snapshot of the hardware registers. Switch target +* lists and endpoint DPA skip values are not tracked in this common structure. +*/ +struct cxl_decoder_config { +int id; +struct range hpa_range; +int interleave_ways; +int interleave_granularity; +enum cxl_decoder_type target_type; +unsigned long flags; +}; + +/** +* struct cxl_decoder - Common CXL HDM Decoder Attributes +* @dev: this decoder's device +* @config: common decoder configuration and state +* @region: currently assigned region for this decoder * @target_map: cached copy of hardware port-id list, available at init * before all @dport objects have been instantiated. While * dport id is 8bit, CFMWS interleave targets are 32bits.
@@ -53,13 +69,8 @@ enum cxl_decoder_type {*/ struct cxl_decoder {struct device dev; -int id; -struct range hpa_range; -int interleave_ways; -int interleave_granularity; -enum cxl_decoder_type target_type; +struct cxl_decoder_config config;struct cxl_region *region; -unsigned long flags;u32 target_map[CXL_DECODER_MAX_INTERLEAVE ];int (*commit)(struct cxl_decoder *cxld);void (*reset)(struct cxl_decoder *cxld);
tools/testing/cxl/test/cxl.c +77 -81
346 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v18 5/19 cxl: Introduce endpoint HDM decoder settings
In reply to Srirangan MadhavanKeep common decoder configuration in cxl_decoder_config and represent endpoint DPA skip in cxl_endpoint_decoder_settings. Share range programming, but keep endpoint skip and switch target-list writes in their respective commit paths.
Separate commit initiation from completion waiting so reset restoration can reuse register programming without changing normal DPA-lock policy.
--- drivers/cxl/core/core.h | 8 ++++ drivers/cxl/core/hdm.c | 95 ++++++++++++++++++++++++++++------------- include/cxl/hdm.h | 19 +++++++++ 3 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 include/cxl/hdm.h
drivers/cxl/core/core.h +8 -0
@@ -7,6 +7,8 @@#include <cxl/mailbox.h> #include <linux/rwsem.h> +struct cxl_endpoint_decoder_settings ; +extern const struct device_type cxl_nvdimm_bridge_type;extern const struct device_type cxl_nvdimm_type;extern const struct device_type cxl_pmu_type;
@@ -139,6 +141,12 @@ struct cxl_rwsem {extern struct cxl_rwsem cxl_rwsem; +void cxl_commit_start_endpoint (const struct cxl_endpoint_decoder_settings *settings, +void __iomem *hdm); +void cxl_commit_start_switch (const struct cxl_decoder_config *config, +u64 targets,void __iomem *hdm); +int cxld_await_commit (void __iomem *hdm,int id); +int cxl_memdev_init (void );void cxl_memdev_exit (void );void cxl_mbox_init (void );
drivers/cxl/core/hdm.c +65 -30
152 lines not on this page · raw message
include/cxl/hdm.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */ +#ifndef __CXL_HDM_H__ +#define __CXL_HDM_H__ + +#include <linux/types.h> +#include <cxl/cxl.h> + +/** +* struct cxl_endpoint_decoder_settings - endpoint HDM decoder programming +* @config: common decoder configuration +* @skip: length of DPA space skipped before this decoder's DPA mapping +*/ +struct cxl_endpoint_decoder_settings { +struct cxl_decoder_config config; +u64 skip; +}; + +#endif /* __CXL_HDM_H__ */
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 05/19] cxl: Introduce endpoint HDM decoder settings
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] cxl: inconsistent hdm parameter order and naming in HDM commit helpers --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] cxl: inconsistent hdm parameter order and naming in HDM commit helpers
--
commit 9094ddf76539be8f49ccc9904f1cd487f715a696
Author: Srirangan Madhavan <[email protected]>
cxl: Introduce endpoint HDM decoder settings
This commit introduces struct cxl_endpoint_decoder_settings to pair common decoder configuration with endpoint DPA skip, and splits HDM decoder register programming into cxl_commit_start_endpoint(), cxl_commit_start_switch(), and cxld_await_commit() helpers.
Quoted 4 lines
drivers/cxl/core/core.h +0
-0
[ ... ]
drivers/cxl/core/core.h struct cxl_rwsem { · line 152
Quoted 13 lines
drivers/cxl/core/core.h +6
-0@@ -139,6 +141,12 @@ struct cxl_rwsem {extern struct cxl_rwsem cxl_rwsem; +void cxl_commit_start_endpoint (const struct cxl_endpoint_decoder_settings *settings, +void __iomem *hdm); +void cxl_commit_start_switch (const struct cxl_decoder_config *config, +u64 targets,void __iomem *hdm); +int cxld_await_commit (void __iomem *hdm,int id); +int cxl_memdev_init (void );void cxl_memdev_exit (void );void cxl_mbox_init (void );
[Severity: Low]
As reported by Li Ming on v10 [1], should void __iomem *hdm be the first parameter in cxl_commit_start_endpoint() and cxl_commit_start_switch() to match cxld_await_commit() and cxl_hdm_write_range()?
The naming prefixes across these three helpers are also inconsistent:
cxl_commit_start_* omits hdm while cxld_await_commit() keeps the cxld_ prefix even though it takes raw hdm and id parameters instead of a struct cxl_decoder.
Using a uniform prefix such as cxl_hdm_* with hdm first across all of these helpers would keep the internal HDM programming API consistent.
[1] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v18 6/19 cxl: Move HDM decoder helpers to built-in code
In reply to Srirangan MadhavanThe PCI core will restore HDM decoders after a reset, possibly while no CXL driver is loaded, so it cannot call into cxl_core.
Move the HDM register programming helpers from hdm.c to hdm_regs.c and build that file into the kernel whenever CXL_BUS is enabled, including when CXL_BUS is modular. Export only the symbols that cxl_core uses.
Keep the DPA lock assertion in the normal decoder programming caller so the register helpers do not depend on cxl_rwsem. Leave cxl_rwsem in cxl_core until built-in cache management needs it.
---
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=e0b476ea99567fb5aa416eac0cf61bca20724880
--- drivers/cxl/core/Makefile | 1 + drivers/cxl/core/hdm.c | 102 +------------------------------ drivers/cxl/core/hdm_regs.c | 118 ++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 100 deletions(-) create mode 100644 drivers/cxl/core/hdm_regs.c
drivers/cxl/core/Makefile +1 -0
@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_CXL_BUS) += cxl_core.o +obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o obj-$(CONFIG_CXL_SUSPEND) += suspend.o obj-$(CONFIG_CXL_KUNIT_TEST) += tests/
drivers/cxl/core/hdm.c +2 -100
120 lines not on this page · raw message
drivers/cxl/core/hdm_regs.c new file +118 -0
118 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v18 7/19 cxl: Share HDM decoder register unpacking
In reply to Srirangan MadhavanFactor common HDM register parsing into cxl_hdm_unpack_decoder(). Validate range and interleave parameters in locals before publishing the decoder configuration, including enable and lock flags. Reject nonzero ranges whose end overflows the address space while preserving the zero-size range representation.
Read decoder base and size registers low then high through a shared helper used by enumeration and the reset snapshot.
Read switch target lists and endpoint DPA skip in their respective branches of init_hdm_decoder().
Preserve the decoder ID allocated by cxl_decoder_init() when publishing the parsed configuration.
---
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=7d6ed7faf7d8e14abce8ae23902d5323738868d2
--- drivers/cxl/core/core.h | 5 +++ drivers/cxl/core/hdm.c | 63 ++++++++----------------------- drivers/cxl/core/hdm_regs.c | 74 +++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 48 deletions(-)
drivers/cxl/core/core.h +5 -0
@@ -147,6 +147,11 @@ void cxl_commit_start_switch(const struct cxl_decoder_config *config, u64 targets, void __iomem *hdm);int cxld_await_commit (void __iomem *hdm,int id); +int cxl_hdm_unpack_decoder (struct cxl_decoder_config *config,int id, +u32 ctrl,u64 base,u64 size); +int cxl_hdm_read_decoder (struct cxl_decoder_config *config,void __iomem *hdm, +int id,u32 ctrl); +int cxl_memdev_init (void );void cxl_memdev_exit (void );void cxl_mbox_init (void );
drivers/cxl/core/hdm.c +15 -48
94 lines not on this page · raw message
drivers/cxl/core/hdm_regs.c +74 -0
77 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 07/19] cxl: Share HDM decoder register unpacking
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] cxl: commit message describes untouched target list and DPA skip reads - [Low] cxl: unused cxl_core…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] cxl: commit message describes untouched target list and DPA skip reads
- [Low] cxl: unused cxl_core export of cxl_hdm_unpack_decoder() --
commit 198782984626bdb9622fed167b6f217bc24d7c42
Author: Srirangan Madhavan <[email protected]>
cxl: Share HDM decoder register unpacking
This commit factors HDM decoder register parsing and base/size reads into cxl_hdm_unpack_decoder() and cxl_hdm_read_decoder() in hdm_regs.c, and updates init_hdm_decoder() to use them.
Quoted 2 lines
Read switch target lists and endpoint DPA skip in their respective branches of
init_hdm_decoder().
[Severity: Low]
Is this paragraph accurate? Both the switch target list reads and the endpoint DPA skip reads were already in those branches of init_hdm_decoder() and are untouched by this patch. If the intent is to note what cxl_hdm_read_decoder() leaves out, saying those reads remain in init_hdm_decoder() would be clearer.
[ ... ]
Quoted 4 lines
drivers/cxl/core/hdm_regs.c +0
-0
[ ... ]
drivers/cxl/core/hdm_regs.c void cxl_commit_start_switch(const struct cxl_decoder_config *config, · line 176
Quoted 3 lines
drivers/cxl/core/hdm_regs.c +3
-0+return 0 ; +} +EXPORT_SYMBOL_FOR_MODULES (cxl_hdm_unpack_decoder,"cxl_core" );
[Severity: Low]
Does cxl_hdm_unpack_decoder() need to be exported to cxl_core? cxl_core only calls cxl_hdm_read_decoder(), and the only caller of cxl_hdm_unpack_decoder() here is cxl_hdm_read_decoder() in the same file.
Even after commit "cxl: Add KUnit checks for HDM decoder unpacking" adds cxl_hdm_unpack_kunit to the export list, cxl_core remains unused and the symbol stays exported when KUnit is disabled. Dropping the export here and exporting it only for KUnit in that later patch would avoid the dead export.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v18 8/19 cxl: Move cxl_rwsem into built-in code
In reply to Srirangan MadhavanKeep the region and DPA locks available to built-in CXL state management when cxl_core is modular.
--- drivers/cxl/core/Makefile | 2 +- drivers/cxl/core/hdm.c | 5 ----- drivers/cxl/core/hdm_state.c | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 drivers/cxl/core/hdm_state.c
drivers/cxl/core/Makefile +1 -1
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_CXL_BUS) += cxl_core.o -obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o +obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o hdm_state.o obj-$(CONFIG_CXL_SUSPEND) += suspend.o obj-$(CONFIG_CXL_KUNIT_TEST) += tests/
drivers/cxl/core/hdm.c +0 -5
@@ -18,11 +18,6 @@* for enumerating these registers and capabilities. */ -struct cxl_rwsem cxl_rwsem = { - .region =__RWSEM_INITIALIZER (cxl_rwsem.region), - .dpa =__RWSEM_INITIALIZER (cxl_rwsem.dpa), -}; -static int add_hdm_decoder (struct cxl_port *port,struct cxl_decoder *cxld) {int rc;
drivers/cxl/core/hdm_state.c new file +15 -0
@@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */ +#include <linux/export.h> +#include <linux/rwsem.h> + +#include <cxl/hdm.h> + +#include "cxl.h" +#include "core.h" + +struct cxl_rwsem cxl_rwsem = { + .region =__RWSEM_INITIALIZER (cxl_rwsem.region), + .dpa =__RWSEM_INITIALIZER (cxl_rwsem.dpa), +}; +EXPORT_SYMBOL_FOR_MODULES (cxl_rwsem,"cxl_core" );
Signature · 1 line
2.43.0
PATCH v18 9/19 cxl: Refresh cached PCI HDM decoder settings
In reply to Srirangan MadhavanEarly PCI discovery creates the HDM cache, while later CXL enumeration and decoder operations provide updated programming state.
Refresh the PCI snapshot when decoders are enumerated, committed, or reset so reset recovery need not walk the CXL topology. Keep cache access and locking in the built-in HDM state helpers. Ignore updates when no cache exists and reject decoder-count mismatches.
Use CXL_RESET for the PCI HDM cache. Shared HDM register helpers do not require it.
Skip cache checks and updates for DVSEC-emulated decoders, which do not use HDM registers. Keep cached global control when MMIO reads return all ones.
Non-PCI mock ports have no upstream PCI device, so the cache helpers ignore them. This avoids test wrappers and a circular dependency between cxl_core and cxl_mock.
---
Changes in v18:
- Declare the pci_dev reference at its initializer, add kernel-doc for the exported cache helpers, make the decoder-count check return bool as cxl_hdm_cache_decoder_count_match(), and drop an unused forward declaration (Dave Jiang)
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=50a88f5194e5df38590d413f925d8d5afa8a8488
--- drivers/cxl/Kconfig | 5 +++ drivers/cxl/core/core.h | 1 + drivers/cxl/core/hdm.c | 30 ++++++++++++++ drivers/cxl/core/hdm_state.c | 78 ++++++++++++++++++++++++++++++++++++ drivers/cxl/core/pci.c | 27 +++++++++++++ include/cxl/hdm.h | 32 +++++++++++++++ include/linux/pci.h | 6 +++ 7 files changed, 179 insertions(+)
drivers/cxl/Kconfig +5 -0
@@ -6,6 +6,7 @@ menuconfig CXL_BUS select FW_UPLOAD select PCI_DOE select FIRMWARE_TABLE + select CXL_RESET select NUMA_KEEP_MEMINFO if NUMA_MEMBLKS select FWCTL if CXL_FEATURES help
@@ -260,3 +261,7 @@ config CXL_KUNIT_TEST If unsure, say N. endif + +# CXL_BUS requires the PCI HDM cache and built-in state helpers, even when modular. +config CXL_RESET + bool
drivers/cxl/core/core.h +1 -0
@@ -220,6 +220,7 @@ static inline void devm_cxl_dport_ras_setup(struct cxl_dport *dport) { }int cxl_gpf_port_setup (struct cxl_dport *dport);struct cxl_hdm ; +int cxl_hdm_check_decoder_count (struct cxl_hdm *cxlhdm);int cxl_hdm_decode_init (struct cxl_dev_state *cxlds,struct cxl_hdm *cxlhdm,struct cxl_endpoint_dvsec_info *info);int cxl_port_get_possible_dports (struct cxl_port *port);
drivers/cxl/core/hdm.c +30 -0
@@ -750,6 +750,24 @@ static void cxlsd_set_targets(struct cxl_switch_decoder *cxlsd, u64 *tgt) *tgt |=FIELD_PREP (GENMASK_ULL (63 ,56 ), t[7 ]->port_id); } +static void cxl_hdm_refresh_decoder (struct cxl_hdm *cxlhdm, +struct cxl_decoder *cxld) +{ +void __iomem *hdm = cxlhdm->regs.hdm_decoder; + +if (!hdm) +return ; +if (!is_endpoint_decoder (&cxld->dev)) +return ; + + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (cxlhdm->port); +if (!pdev) +return ; +cxl_hdm_cache_refresh_decoder (pdev, hdm, +to_cxl_endpoint_decoder (&cxld->dev)); +} +static void cxl_endpoint_decoder_snapshot (struct cxl_endpoint_decoder *cxled,struct cxl_endpoint_decoder_settings *settings)
@@ -830,6 +848,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) } port->commit_end++; cxld->config.flags |=CXL_DECODER_F_ENABLE ; +cxl_hdm_refresh_decoder (cxlhdm, cxld);return 0 ; }
@@ -902,6 +921,7 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)writel (0 , hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); cxld->config.flags &= ~CXL_DECODER_F_ENABLE ; +cxl_hdm_refresh_decoder (cxlhdm, cxld);/* Userspace is now responsible for reconfiguring this decoder */ if (is_endpoint_decoder (&cxld->dev)) {
@@ -1121,6 +1141,8 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, {void __iomem *hdm = cxlhdm->regs.hdm_decoder;struct cxl_port *port = cxlhdm->port; +/* DVSEC-emulated decoders do not describe the cached HDM registers. */ +bool emulated =should_emulate_decoders (info);u64 dpa_base =0 ;int i, rc;
@@ -1136,6 +1158,12 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,return -ENXIO ; } +if (is_cxl_endpoint (port) && hdm && !emulated) { + rc =cxl_hdm_check_decoder_count (cxlhdm); +if (rc) +return rc; + } +cxl_settle_decoders (cxlhdm);/*
@@ -1182,6 +1210,8 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,put_device (&cxld->dev);return rc; } +if (!emulated) +cxl_hdm_refresh_decoder (cxlhdm, cxld); rc =add_hdm_decoder (port, cxld);if (rc) { dev_warn(&port->dev,
drivers/cxl/core/hdm_state.c +78 -0
87 lines not on this page · raw message
drivers/cxl/core/pci.c +27 -0
@@ -6,6 +6,7 @@#include <linux/delay.h> #include <linux/pci.h> #include <linux/pci-doe.h> +#include <cxl/hdm.h> #include <cxl/pci.h> #include <linux/aer.h> #include <cxlpci.h>
@@ -180,6 +181,32 @@ int cxl_await_media_ready(struct cxl_dev_state *cxlds) }EXPORT_SYMBOL_NS_GPL (cxl_await_media_ready,"CXL" ); +/** +* cxl_hdm_check_decoder_count() - check enumeration against the PCI HDM cache +* @cxlhdm: HDM capability containing the decoder count to check +* +* Take cxl_rwsem.dpa for read while comparing the cached count. Call only +* for hardware HDM decoders, since DVSEC-emulated ranges do not describe +* the HDM registers represented by the cache. +* +* Return: 0 if the counts match or no upstream PCI device or cache exists, +* or -ENXIO if the cached and enumerated decoder counts differ. +*/ +int cxl_hdm_check_decoder_count (struct cxl_hdm *cxlhdm) +{ + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (cxlhdm->port); + +if (!pdev) +return 0 ; + +if (!cxl_hdm_cache_decoder_count_match (pdev, cxlhdm->decoder_count)) +return -ENXIO ; + +return 0 ; +} +EXPORT_SYMBOL_NS_GPL (cxl_hdm_check_decoder_count,"CXL" ); +static int cxl_set_mem_enable (struct cxl_dev_state *cxlds,u16 val) {struct pci_dev *pdev =to_pci_dev (cxlds->dev);
include/cxl/hdm.h +32 -0
@@ -6,6 +6,9 @@#include <linux/types.h> #include <cxl/cxl.h> +struct pci_dev ; +struct cxl_endpoint_decoder ; +/** * struct cxl_endpoint_decoder_settings - endpoint HDM decoder programming * @config: common decoder configuration
@@ -16,4 +19,33 @@ struct cxl_endpoint_decoder_settings {u64 skip; }; +/** +* struct cxl_hdm_info - cached CXL HDM state for a PCI device +* @decoder_count: number of entries in @settings +* @hdm_bar: PCI BAR containing the HDM decoder capability +* @hdm_offset: offset of the HDM decoder capability in @hdm_bar +* @hdm_size: size of the HDM decoder register block +* @global_ctrl: HDM decoder global control register +* @dvsec_ctrl: CXL DVSEC control register +* @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state +* @settings: per-endpoint-decoder programming state +* +* Publication through pci_dev.hdm and all mutable fields are protected by +* cxl_rwsem.dpa. +*/ +struct cxl_hdm_info { +int decoder_count; +int hdm_bar; +resource_size_t hdm_offset; +resource_size_t hdm_size; +u32 global_ctrl; +u16 dvsec_ctrl; +bool dvsec_ctrl_valid; + struct cxl_endpoint_decoder_settings settings[] __counted_by(decoder_count); +}; + +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); +#endif /* __CXL_HDM_H__ */
include/linux/pci.h +6 -0
@@ -339,6 +339,9 @@ struct pcie_link_state;struct pci_sriov ;struct pci_p2pdma ;struct rcec_ea ; +#ifdef CONFIG_CXL_RESET +struct cxl_hdm_info ; +#endif /* struct pci_dev - describes a PCI device *
@@ -567,6 +570,9 @@ struct pci_dev {#ifdef CONFIG_PCI_DOE struct xarray doe_mbs;/* Data Object Exchange mailboxes */ #endif +#ifdef CONFIG_CXL_RESET +struct cxl_hdm_info *hdm;/* CXL HDM decoder state */ +#endif #ifdef CONFIG_PCI_NPEM struct npem *npem;/* Native PCIe Enclosure Management */ #endif
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 09/19] cxl: Refresh cached PCI HDM decoder settings
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] cxl: stale PCI HDM cache after cxl_endpoint_decoder_clear_reset_flags() - [Medium] cxl: stale…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] cxl: stale PCI HDM cache after cxl_endpoint_decoder_clear_reset_flags()
- [Medium] cxl: stale emulated flag in devm_cxl_enumerate_decoders() caches DVSEC state
- [Medium] cxl: TOCTOU race on cxled->skip in cxl_decoder_commit() cache refresh
- [Low] cxl: unnecessary CXL export of cxl_hdm_check_decoder_count() --
commit 4920baebe7a19ee7d660630b38e29f5630e4ed37
Author: Srirangan Madhavan <[email protected]>
cxl: Refresh cached PCI HDM decoder settings
This commit adds the struct cxl_hdm_info cache to struct pci_dev under CONFIG_CXL_RESET and refreshes cached endpoint decoder settings when decoders are enumerated, committed, or reset.
Quoted 4 lines
drivers/cxl/core/hdm.c +0
-0
[ ... ]
drivers/cxl/core/hdm.c static int cxl_decoder_commit(struct cxl_decoder *cxld) · line 854
Quoted 8 lines
drivers/cxl/core/hdm.c +1
-0@@ -830,6 +848,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) } port->commit_end++; cxld->config.flags |=CXL_DECODER_F_ENABLE ; +cxl_hdm_refresh_decoder (cxlhdm, cxld);return 0 ; }
[Severity: Medium]
Can cxl_hdm_refresh_decoder() cache a zeroed cxled->skip after hardware committed a non-zero skip?
CPU0 CPU1commit_store() -> __commit()
down_write(&cxl_rwsem.region)
cxl_decoder_commit()
down_read(&cxl_rwsem.dpa)
setup_hw_decoder() // skip != 0
up_read(&cxl_rwsem.dpa)
cxld_await_commit() // sleeps
device_unbind_cleanup(&port->dev)
cxl_dpa_release(cxled)
down_write(&cxl_rwsem.dpa)
cxled->skip = 0;
up_write(&cxl_rwsem.dpa)
cxld->config.flags |= ...ENABLE
cxl_hdm_refresh_decoder()
down_write(&cxl_rwsem.dpa)
settings->skip = cxled->skip <-- caches 0 while HW has skip != 0cxl_dpa_alloc() registers cxl_dpa_release() on &port->dev after cxl_decoder_autoremove() registers cxld_unregister(), so LIFO devres teardown on port unbind runs cxl_dpa_release() first without holding cxl_rwsem.region. If a PCI or CXL reset then restores from pdev->hdm->settings[id] (or permanently if CXL_REGION_F_LOCK prevents reset teardown), hardware is reprogrammed with skip = 0 instead of the committed DPA offset.
Passing the snapshot captured under cxl_rwsem.dpa in setup_hw_decoder() to the cache refresh helper would avoid re-reading a mutated cxled->skip.
drivers/cxl/core/hdm.c static void cxl_decoder_reset(struct cxl_decoder *cxld) · line 927
Quoted 8 lines
drivers/cxl/core/hdm.c +1
-0@@ -902,6 +921,7 @@ static void cxl_decoder_reset(struct cxl_decoder *cxld)writel (0 , hdm +CXL_HDM_DECODER0_BASE_LOW_OFFSET (id)); cxld->config.flags &= ~CXL_DECODER_F_ENABLE ; +cxl_hdm_refresh_decoder (cxlhdm, cxld);/* Userspace is now responsible for reconfiguring this decoder */ if (is_endpoint_decoder (&cxld->dev)) {
[Severity: Medium]
Does pdev->hdm->settings[id] stay stale when cxl_endpoint_decoder_clear_reset_flags() clears CXL_DECODER_F_ENABLE?
When an SBR wipes active HDM decoders, cxl_reset_done() in drivers/cxl/pci.c calls cxl_endpoint_decoder_clear_reset_flags(), which clears CXL_DECODER_F_RESET_MASK (including CXL_DECODER_F_ENABLE) on cxled->cxld.config.flags without refreshing pdev->hdm.
When the region is later torn down, cxl_decoder_reset() sees CXL_DECODER_F_ENABLE already cleared and returns early before reaching cxl_hdm_refresh_decoder(). The stale enabled decoder and HPA range remain in pdev->hdm->settings[id], so a subsequent CXL or PCI reset can try to reserve the old HPA range or reprogram a torn-down decoder.
Quoted 8 lines
drivers/cxl/core/hdm.c +2
-0@@ -1121,6 +1141,8 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, {void __iomem *hdm = cxlhdm->regs.hdm_decoder;struct cxl_port *port = cxlhdm->port; +/* DVSEC-emulated decoders do not describe the cached HDM registers. */ +bool emulated =should_emulate_decoders (info);u64 dpa_base =0 ;int i, rc;
[ ... ]
drivers/cxl/core/hdm.c static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm, · line 1217
Quoted 9 lines
drivers/cxl/core/hdm.c +2
-0@@ -1182,6 +1210,8 @@ static int devm_cxl_enumerate_decoders(struct cxl_hdm *cxlhdm,put_device (&cxld->dev);return rc; } +if (!emulated) +cxl_hdm_refresh_decoder (cxlhdm, cxld); rc =add_hdm_decoder (port, cxld);if (rc) { dev_warn(&port->dev,
[Severity: Medium]
Can a concurrent PCI reset cause devm_cxl_enumerate_decoders() to write DVSEC-emulated decoder state into pdev->hdm?
CPU0 CPU1devm_cxl_enumerate_decoders()
emulated = should_emulate_decoders() // false (HDM enabled) cxl_settle_decoders() // sleeps up to 20ms
pci_try_reset_bridge()
SBR clears HDM global ctrl to 0
init_hdm_decoder()
should_emulate_decoders() // now true cxl_setup_hdm_decoder_from_dvsec()
cxld->config.flags |= ENABLE | LOCK
if (!emulated) // still false
cxl_hdm_refresh_decoder() <-- caches DVSEC range & LOCK in pdev->hdm
devm_cxl_enumerate_decoders() does not hold device_lock(&pdev->dev) or cxl_rwsem.region, and init_hdm_decoder() re-evaluates should_emulate_decoders(info) on each decoder after cxl_settle_decoders(). If a reset clears CXL_HDM_DECODER_ENABLE in between, init_hdm_decoder() falls back to cxl_setup_hdm_decoder_from_dvsec(), while the stale emulated == false still lets cxl_hdm_refresh_decoder() record the DVSEC-emulated range, CXL_DECODER_F_LOCK, and zeroed global_ctrl into pdev->hdm.
Passing emulated into init_hdm_decoder() (or gating the refresh on whether init_hdm_decoder() actually initialized a hardware decoder) would avoid this mismatch.
Quoted 4 lines
drivers/cxl/core/pci.c +0
-0
[ ... ]
Quoted 4 lines
drivers/cxl/core/pci.c +4
-0+int cxl_hdm_check_decoder_count (struct cxl_hdm *cxlhdm) +{ + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (cxlhdm->port);
drivers/cxl/core/pci.c int cxl_await_media_ready(struct cxl_dev_state *cxlds) · line 208
Quoted 10 lines
drivers/cxl/core/pci.c +10
-0+ +if (!pdev) +return 0 ; + +if (!cxl_hdm_cache_decoder_count_match (pdev, cxlhdm->decoder_count)) +return -ENXIO ; + +return 0 ; +} +EXPORT_SYMBOL_NS_GPL (cxl_hdm_check_decoder_count,"CXL" );
[Severity: Low]
Does cxl_hdm_check_decoder_count() need to be exported? Its declaration lives only in the cxl_core-private drivers/cxl/core/core.h and its only caller is devm_cxl_enumerate_decoders() in drivers/cxl/core/hdm.c, which is linked into the same module. Dropping the export (or making it a static helper alongside cxl_hdm_refresh_decoder() in drivers/cxl/core/hdm.c) avoids widening the CXL module ABI unnecessarily.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v18 10/19 cxl: Build register block helpers into the kernel
In reply to Srirangan MadhavanBuild register discovery with the built-in HDM helpers so PCI enumeration can use it even when cxl_core is modular. Export the interfaces used by cxl_core.
---
Changes in v18:
- Keep only the built-in register helper changes here; the BAR/offset fields and cxl_dvsec_hdm_count_valid() move to the patch that uses them, and the exports use EXPORT_SYMBOL_FOR_MODULES() (Dave Jiang)
--- drivers/cxl/core/Makefile | 3 +-- drivers/cxl/core/regs.c | 4 ++++ drivers/cxl/cxl.h | 3 --- include/cxl/pci.h | 3 +++ tools/testing/cxl/Kbuild | 1 - 5 files changed, 8 insertions(+), 6 deletions(-)
drivers/cxl/core/Makefile +1 -2
@@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_CXL_BUS) += cxl_core.o -obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o hdm_state.o +obj-$(subst m,y,$(CONFIG_CXL_BUS)) += hdm_regs.o hdm_state.o regs.o obj-$(CONFIG_CXL_SUSPEND) += suspend.o obj-$(CONFIG_CXL_KUNIT_TEST) += tests/
@@ -9,7 +9,6 @@ CFLAGS_trace.o = -DTRACE_INCLUDE_PATH=. -I$(src) cxl_core-y := port.o cxl_core-y += pmem.o -cxl_core-y += regs.o cxl_core-y += memdev.o cxl_core-y += mbox.o cxl_core-y += pci.o
drivers/cxl/core/regs.c +4 -0
@@ -200,6 +200,7 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,return ret_val; } +EXPORT_SYMBOL_FOR_MODULES (devm_cxl_iomap_block,"cxl_core" ); int cxl_map_component_regs(const struct cxl_register_map *map, struct cxl_component_regs *regs,
@@ -532,6 +533,7 @@ u16 cxl_rcrb_to_aer(struct device *dev, resource_size_t rcrb)return offset; } +EXPORT_SYMBOL_FOR_MODULES (cxl_rcrb_to_aer,"cxl_core" );static resource_size_t cxl_rcrb_to_linkcap (struct device *dev,struct cxl_dport *dport) {
@@ -648,6 +650,7 @@ resource_size_t __rcrb_to_component(struct device *dev, struct cxl_rcrb_info *rireturn component_reg_phys; } +EXPORT_SYMBOL_FOR_MODULES (__rcrb_to_component,"cxl_core" );resource_size_t cxl_rcd_component_reg_phys (struct device *dev,struct cxl_dport *dport)
@@ -656,3 +659,4 @@ resource_size_t cxl_rcd_component_reg_phys(struct device *dev,return CXL_RESOURCE_NONE ;return __rcrb_to_component (dev, &dport->rcrb,CXL_RCRB_UPSTREAM ); } +EXPORT_SYMBOL_FOR_MODULES (cxl_rcd_component_reg_phys,"cxl_core" );
drivers/cxl/cxl.h +0 -3
@@ -245,9 +245,6 @@ enum cxl_regloc_type;int cxl_count_regblock (struct pci_dev *pdev,enum cxl_regloc_type type);int cxl_find_regblock_instance (struct pci_dev *pdev,enum cxl_regloc_type type,struct cxl_register_map *map,unsigned int index); -int cxl_find_regblock (struct pci_dev *pdev,enum cxl_regloc_type type, -struct cxl_register_map *map); -int cxl_setup_regs (struct cxl_register_map *map);struct cxl_dport ;int cxl_dport_map_rcd_linkcap (struct pci_dev *pdev,struct cxl_dport *dport);
include/cxl/pci.h +3 -0
@@ -19,4 +19,7 @@ struct pci_dev;int cxl_pci_setup_regs (struct pci_dev *pdev,enum cxl_regloc_type type,struct cxl_register_map *map); +int cxl_find_regblock (struct pci_dev *pdev,enum cxl_regloc_type type, +struct cxl_register_map *map); +int cxl_setup_regs (struct cxl_register_map *map);#endif
tools/testing/cxl/Kbuild +0 -1
@@ -59,7 +59,6 @@ obj-m += cxl_core.o cxl_core-y := $(CXL_CORE_SRC)/port.o cxl_core-y += $(CXL_CORE_SRC)/pmem.o -cxl_core-y += $(CXL_CORE_SRC)/regs.o cxl_core-y += $(CXL_CORE_SRC)/memdev.o cxl_core-y += $(CXL_CORE_SRC)/mbox.o cxl_core-y += $(CXL_CORE_SRC)/pci.o
Signature · 1 line
2.43.0
PATCH v18 11/19 PCI/CXL: Cache endpoint HDM state during PCI enumeration
In reply to Srirangan MadhavanCache HDM state in pci_dev->hdm after BAR resource setup and before driver binding, so reset can restore it without a bound CXL driver.
Limit caching to devices that advertise CXL.cache, CXL.mem, CXL Reset, and Reset Memory Clear. Save their HDM register location, decoder settings, and Device DVSEC state.
Validate capability reads and decoder counts, and restore PCI_COMMAND after temporary MMIO access, reporting restoration failures. Publish the cache under the PCI device lock.
Update the cached global control when the CXL driver disables HDM on unbind, so a later reset does not restore a stale enable bit.
Preserve the register block BAR and offset in struct cxl_register_map, so the cache can map the HDM registers again after a reset.
---
Changes in v18:
- Drop the cxl_rwsem.dpa locking from the cache helpers here, since no reset path reads the cache yet; it returns with the readers in the reset locking patch. Free the cache directly on release, simplify __cxl_pci_hdm_read_info() and move the publish comment above guard(pci_dev) (Li Ming)
https://lore.kernel.org/linux-cxl/[email protected]/
- Reject all-ones reads of PCI_COMMAND, the DVSEC Control register, the global HDM decoder control and the decoder skip registers when filling the cache, so an unresponsive device is not cached as valid state (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=11
- Move the register block BAR and offset fields and cxl_dvsec_hdm_count_valid() here from the register helper patch, where they are first used (Dave)
https://lore.kernel.org/linux-cxl/[email protected]/
- Update the cached global control when the CXL driver disables HDM on unbind, so a later reset does not restore a stale enable bit (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=11
- Add cxl_pci_read_config_word(), which maps a failed or all-ones config read to an errno, and use it for the reads checked here and in the later reset patches (Li Ming)
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=b12baa1105f33cc1a10f9c9c0c6376194397d9f3
--- MAINTAINERS | 1 + drivers/cxl/core/hdm_regs.c | 102 +++++++++++++++ drivers/cxl/core/hdm_state.c | 46 ++++++- drivers/cxl/core/pci.c | 38 ++++-- drivers/cxl/core/regs.c | 5 + drivers/pci/Makefile | 1 + drivers/pci/bus.c | 1 + drivers/pci/cxl.c | 226 ++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 8 ++ drivers/pci/probe.c | 1 + include/cxl/cxl.h | 4 + include/cxl/hdm.h | 14 ++- include/cxl/pci.h | 10 ++ include/uapi/linux/pci_regs.h | 2 + 14 files changed, 445 insertions(+), 14 deletions(-) create mode 100644 drivers/pci/cxl.c
MAINTAINERS +1 -0
@@ -6735,6 +6735,7 @@ S: Maintained F: Documentation/driver-api/cxl F: Documentation/userspace-api/fwctl/fwctl-cxl.rst F: drivers/cxl/ +F: drivers/pci/cxl.c F: include/cxl/ F: include/uapi/linux/cxl_mem.h F: tools/testing/cxl/
drivers/cxl/core/hdm_regs.c +102 -0
111 lines not on this page · raw message
drivers/cxl/core/hdm_state.c +45 -1
69 lines not on this page · raw message
drivers/cxl/core/pci.c +27 -11
@@ -212,22 +212,31 @@ static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)struct pci_dev *pdev =to_pci_dev (cxlds->dev);int d = cxlds->cxl_dvsec;u16 ctrl; -int rc; +int rc, ret =1 ; rc =pci_read_config_word (pdev, d +PCI_DVSEC_CXL_CTRL , &ctrl);if (rc)return pcibios_err_to_errno (rc); +/* An all-ones read must not be written back or cached. */ +if (PCI_POSSIBLE_ERROR (ctrl)) +return -ENODEV ; -if ((ctrl &PCI_DVSEC_CXL_MEM_ENABLE ) == val) -return 1 ; - ctrl &= ~PCI_DVSEC_CXL_MEM_ENABLE ; - ctrl |= val; +if ((ctrl &PCI_DVSEC_CXL_MEM_ENABLE ) != val) { + ctrl &= ~PCI_DVSEC_CXL_MEM_ENABLE ; + ctrl |= val; - rc =pci_write_config_word (pdev, d +PCI_DVSEC_CXL_CTRL , ctrl); -if (rc) -return pcibios_err_to_errno (rc); + rc =pci_write_config_word (pdev, d +PCI_DVSEC_CXL_CTRL , ctrl); +if (rc) +return pcibios_err_to_errno (rc); + ret =0 ; + } -return 0 ; +/* +* Refresh the PCI-core cache from the live register even when +* MEM_ENABLE already matched: a reset restores the cached value. +*/ +cxl_hdm_cache_set_dvsec_ctrl (pdev, ctrl); +return ret; }static void clear_mem_enable (void *cxlds)
@@ -266,13 +275,20 @@ static int dvsec_range_allowed(struct device *dev, const void *arg)static void disable_hdm (void *_cxlhdm) { -u32 global_ctrl;struct cxl_hdm *cxlhdm = _cxlhdm;void __iomem *hdm = cxlhdm->regs.hdm_decoder; + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (cxlhdm->port); +u32 global_ctrl; global_ctrl =readl (hdm +CXL_HDM_DECODER_CTRL_OFFSET );writel (global_ctrl & ~CXL_HDM_DECODER_ENABLE , hdm +CXL_HDM_DECODER_CTRL_OFFSET ); + +/* Keep the reset cache up to date. */ +if (pdev && global_ctrl !=U32_MAX ) +cxl_hdm_cache_set_global_ctrl (pdev, + global_ctrl & ~CXL_HDM_DECODER_ENABLE ); }static int devm_cxl_enable_hdm (struct device *host,struct cxl_hdm *cxlhdm)
@@ -317,7 +333,7 @@ int cxl_dvsec_rr_decode(struct cxl_dev_state *cxlds,* capable, there is no point in continuing to enable CXL.mem. */ hdm_count =FIELD_GET (PCI_DVSEC_CXL_HDM_COUNT , cap); -if (!hdm_count || hdm_count >2 ) +if (!cxl_dvsec_hdm_count_valid (hdm_count))return -EINVAL ;/*
drivers/cxl/core/regs.c +5 -0
@@ -300,6 +300,8 @@ static bool cxl_decode_regblock(struct pci_dev *pdev, u32 reg_lo, u32 reg_hi, } map->reg_type = reg_type; + map->bar = bar; + map->bar_offset = offset; map->resource =pci_resource_start (pdev, bar) + offset; map->max_size =pci_resource_len (pdev, bar) - offset;return true;
@@ -324,6 +326,7 @@ static int __cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_ty *map = (struct cxl_register_map ) { .host = &pdev->dev, .resource =CXL_RESOURCE_NONE , + .bar =-1 , }; regloc = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL,
@@ -354,6 +357,8 @@ static int __cxl_find_regblock_instance(struct pci_dev *pdev, enum cxl_regloc_ty } map->resource =CXL_RESOURCE_NONE ; + map->bar =-1 ; + map->bar_offset =0 ;if (index ==CXL_INSTANCES_COUNT )return instance;
drivers/pci/Makefile +1 -0
@@ -34,6 +34,7 @@ obj-$(CONFIG_PCI_P2PDMA) += p2pdma.o obj-$(CONFIG_XEN_PCIDEV_FRONTEND) += xen-pcifront.o obj-$(CONFIG_VGA_ARB) += vgaarb.o obj-$(CONFIG_PCI_DOE) += doe.o +obj-$(CONFIG_CXL_RESET) += cxl.o obj-$(CONFIG_PCI_IDE) += ide.o obj-$(CONFIG_PCI_TSM) += tsm.o obj-$(CONFIG_PCI_DYNAMIC_OF_NODES) += of_property.o
drivers/pci/bus.c +1 -0
@@ -359,6 +359,7 @@ void pci_bus_add_device(struct pci_dev *dev)/* Save config space for error recoverability */ pci_save_state (dev); +pci_cxl_hdm_cache_init (dev);/* * Enable runtime PM, which potentially allows the device to
drivers/pci/cxl.c new file +226 -0
226 lines not on this page · raw message
drivers/pci/pci.h +8 -0
@@ -736,6 +736,14 @@ static inline void pci_doe_destroy(struct pci_dev *pdev) { }static inline void pci_doe_disconnected (struct pci_dev *pdev) { }#endif +#ifdef CONFIG_CXL_RESET +void pci_cxl_hdm_cache_init (struct pci_dev *pdev); +void pci_cxl_hdm_cache_release (struct pci_dev *pdev); +#else +static inline void pci_cxl_hdm_cache_init (struct pci_dev *pdev) { } +static inline void pci_cxl_hdm_cache_release (struct pci_dev *pdev) { } +#endif +#ifdef CONFIG_PCI_NPEM void pci_npem_create (struct pci_dev *dev);void pci_npem_remove (struct pci_dev *dev);
drivers/pci/probe.c +1 -0
@@ -2491,6 +2491,7 @@ static void pci_release_dev(struct device *dev)struct pci_dev *pci_dev; pci_dev =to_pci_dev (dev); +pci_cxl_hdm_cache_release (pci_dev);pci_release_capabilities (pci_dev);pci_release_of_node (pci_dev);pcibios_release_device (pci_dev);
include/cxl/cxl.h +4 -0
@@ -149,6 +149,8 @@ struct cxl_pmu_reg_map {* @base: virtual base of the register-block-BAR + @block_offset * @resource: physical resource base of the register block * @max_size: maximum mapping size to perform register search +* @bar: PCI BAR containing the register block +* @bar_offset: register block offset relative to @bar * @reg_type: see enum cxl_regloc_type * @component_map: cxl_reg_map for component registers * @device_map: cxl_reg_maps for device registers
@@ -159,6 +161,8 @@ struct cxl_register_map {void __iomem *base;resource_size_t resource;resource_size_t max_size; +int bar; +resource_size_t bar_offset;u8 reg_type;union {struct cxl_component_reg_map component_map;
include/cxl/hdm.h +12 -2
@@ -26,12 +26,12 @@ struct cxl_endpoint_decoder_settings {* @hdm_offset: offset of the HDM decoder capability in @hdm_bar * @hdm_size: size of the HDM decoder register block * @global_ctrl: HDM decoder global control register +* @dvsec: config space offset of the CXL Device DVSEC * @dvsec_ctrl: CXL DVSEC control register * @dvsec_ctrl_valid: whether @dvsec_ctrl contains valid state * @settings: per-endpoint-decoder programming state * -* Publication through pci_dev.hdm and all mutable fields are protected by -* cxl_rwsem.dpa. +* Publication through pci_dev.hdm is serialized by the PCI device lock. */ struct cxl_hdm_info {int decoder_count;
@@ -39,13 +39,23 @@ struct cxl_hdm_info {resource_size_t hdm_offset;resource_size_t hdm_size;u32 global_ctrl; +u16 dvsec;u16 dvsec_ctrl;bool dvsec_ctrl_valid; struct cxl_endpoint_decoder_settings settings[] __counted_by(decoder_count); }; +struct device ; + +int cxl_hdm_decoder_count_mapped (struct device *dev,void __iomem *hdm, +resource_size_t size); +int cxl_hdm_read_state (struct device *dev,void __iomem *hdm, +struct cxl_hdm_info *info); +void cxl_hdm_cache_set_dvsec_ctrl (struct pci_dev *pdev,u16 ctrl); +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_release (struct cxl_hdm_info **slot);#endif /* __CXL_HDM_H__ */
include/cxl/pci.h +10 -0
@@ -4,6 +4,8 @@#ifndef __CXL_CXL_PCI_H__#define __CXL_CXL_PCI_H__ +#include <linux/types.h> +/* Register Block Identifier (RBI) */ enum cxl_regloc_type { CXL_REGLOC_RBI_EMPTY = 0,
@@ -22,4 +24,12 @@ int cxl_pci_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,int cxl_find_regblock (struct pci_dev *pdev,enum cxl_regloc_type type,struct cxl_register_map *map);int cxl_setup_regs (struct cxl_register_map *map); +/* +* CXL r4.0 sec 8.1.3.1: a CXL.mem capable device reports one or two HDM +* ranges. 00b is illegal when Mem_Capable is set and 11b is reserved. +*/ +static inline bool cxl_dvsec_hdm_count_valid (unsigned int hdm_count) +{ +return hdm_count ==1 || hdm_count ==2 ; +}#endif
include/uapi/linux/pci_regs.h +2 -0
@@ -1352,6 +1352,8 @@#define PCI_DVSEC_CXL_CACHE_CAPABLE _BITUL(0)#define PCI_DVSEC_CXL_MEM_CAPABLE _BITUL(2)#define PCI_DVSEC_CXL_HDM_COUNT __GENMASK(5, 4) +#define PCI_DVSEC_CXL_RST_CAPABLE _BITUL(7) +#define PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE _BITUL(11)#define PCI_DVSEC_CXL_CTRL 0xC#define PCI_DVSEC_CXL_MEM_ENABLE _BITUL(2)#define PCI_DVSEC_CXL_RANGE_SIZE_HIGH (i) (0x18 + (i * 0x10))
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 11/19] PCI/CXL: Cache endpoint HDM state during PCI enumeration
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] PCI/CXL: pci_cxl_hdm_cache_init() runs before x86 BAR survey and assignment - [High] PCI/CXL…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] PCI/CXL: pci_cxl_hdm_cache_init() runs before x86 BAR survey and assignment
- [High] PCI/CXL: cxl_pci_hdm_read_info() skips upstream bridge memory enablement
- [High] PCI/CXL: HDM cache enables reset before Normalized Addressing discovery
- [Medium] PCI/CXL: out-of-bounds MMIO read in cxl_pci_hdm_map_regs()
- [Medium] cxl: devm_cxl_enable_hdm() omits cxl_hdm_cache_set_global_ctrl() update
- [Medium] cxl/pci: disable_hdm() leaves stale HDM enable in cache on U32_MAX read
- [Medium] PCI/CXL: __pci_cxl_hdm_cache_init() excludes CXL.mem-only devices
--
commit cd94cb85d3b6b7e41ad48b2c419445f63d976837
Author: Srirangan Madhavan <[email protected]>
PCI/CXL: Cache endpoint HDM state during PCI enumeration
This commit caches endpoint HDM decoder and DVSEC state in pci_dev->hdm during PCI enumeration and updates the cache when CXL enables or disables memory and HDM decode.
Quoted 4 lines
drivers/cxl/core/pci.c +0
-0
[ ... ]
Quoted 9 lines
drivers/cxl/core/pci.c +2
-1@@ -266,13 +275,20 @@ static int dvsec_range_allowed(struct device *dev, const void *arg)static void disable_hdm (void *_cxlhdm) { -u32 global_ctrl;struct cxl_hdm *cxlhdm = _cxlhdm;void __iomem *hdm = cxlhdm->regs.hdm_decoder; + struct pci_dev *pdev __free(pci_dev_put) = +cxl_port_get_uport_pci_dev (cxlhdm->port);
drivers/cxl/core/pci.c static int dvsec_range_allowed(struct device *dev, const void *arg) · line 291
Quoted 10 lines
drivers/cxl/core/pci.c +6
-0+u32 global_ctrl; global_ctrl =readl (hdm +CXL_HDM_DECODER_CTRL_OFFSET );writel (global_ctrl & ~CXL_HDM_DECODER_ENABLE , hdm +CXL_HDM_DECODER_CTRL_OFFSET ); + +/* Keep the reset cache up to date. */ +if (pdev && global_ctrl !=U32_MAX ) +cxl_hdm_cache_set_global_ctrl (pdev, + global_ctrl & ~CXL_HDM_DECODER_ENABLE );
[Severity: Medium]
Does skipping cxl_hdm_cache_set_global_ctrl() when readl() returns U32_MAX leave a stale CXL_HDM_DECODER_ENABLE bit in pdev->hdm->global_ctrl?
When cxl_error_detected() unbinds the memdev on a frozen PCIe channel, disable_hdm() runs while MMIO reads return U32_MAX. Leaving the enable bit set in the cache causes a later reset to restore HDM global enable on hardware even though the driver unbound and disabled it. Clearing CXL_HDM_DECODER_ENABLE in the cached global_ctrl on a U32_MAX read would keep the cache consistent.
drivers/cxl/core/pci.c static int dvsec_range_allowed(struct device *dev, const void *arg) · line 294
Quoted 3 lines
drivers/cxl/core/pci.c +0
-0}static int devm_cxl_enable_hdm (struct device *host,struct cxl_hdm *cxlhdm)
[Severity: Medium]
Should devm_cxl_enable_hdm() also call cxl_hdm_cache_set_global_ctrl() after enabling CXL_HDM_DECODER_ENABLE?
The kerneldoc for cxl_hdm_cache_set_global_ctrl() notes that it tracks enabling HDM on bind as well as disabling it on unbind, but devm_cxl_enable_hdm() does not update pdev->hdm->global_ctrl. During cxl_endpoint_port_probe(), devm_cxl_enable_mem() updates cached dvsec_ctrl with PCI_DVSEC_CXL_MEM_ENABLE set, and devm_cxl_enumerate_decoders() then sleeps 20 ms in cxl_settle_decoders() before refreshing global_ctrl:
CPU0 CPU1cxl_endpoint_port_probe()
devm_cxl_enable_hdm()
writel(... | ENABLE)
devm_cxl_enable_mem()
cache dvsec_ctrl (MEM_ENABLE=1)
cxl_settle_decoders()
msleep(20)
pci_reset_function()
cxl_reset_hdm_prepare()
snapshot sees MEM_ENABLE=1, HDM_ENABLE=0 <-- -EOPNOTSUPP
A concurrent reset during that window either fails with -EOPNOTSUPP or, if it lands before devm_cxl_enable_mem(), restores CXL_HDM_DECODER_ENABLE cleared to hardware.
[ ... ]
drivers/pci/bus.c void pci_bus_add_device(struct pci_dev *dev) · line 362
Quoted 9 lines
drivers/pci/bus.c +1
-0@@ -359,6 +359,7 @@ void pci_bus_add_device(struct pci_dev *dev)/* Save config space for error recoverability */ pci_save_state (dev); +pci_cxl_hdm_cache_init (dev);
[Severity: High]
As reported by Richard Cheng on v8 [1], can pci_cxl_hdm_cache_init() run safely here before x86 BAR resource survey and assignment?
During x86 ACPI boot, acpi_pci_root_add() calls pci_bus_add_devices() during subsys_initcall(acpi_init), before pcibios_resource_survey() and fs_initcall(pcibios_assign_resources). If firmware left the component BAR unassigned or requiring relocation, cxl_pci_bar_assigned() fails and cache initialization is never retried, leaving pdev->hdm permanently NULL and breaking post-reset HDM restoration.
In addition, cxl_pci_bar_assigned() does not check res->parent, so a conflicting firmware BAR that has not yet been surveyed can be temporarily enabled with PCI_COMMAND_MEMORY and ioremapped before resource survey clears the conflict.
Quoted 5 lines
drivers/pci/cxl.c new file +0
-0
[ ... ]
Quoted 8 lines
drivers/pci/cxl.c new file +8
-0+static void __iomem *cxl_pci_hdm_map_regs (struct pci_dev *pdev, +struct cxl_register_map *map, +resource_size_t *hdm_offset, +int *decoder_count) +{ +struct cxl_reg_map *hdm_map = &map->component_map.hdm_decoder; +void __iomem *hdm; +int rc;
drivers/pci/cxl.c line 76
Quoted 10 lines
drivers/pci/cxl.c new file +10
-0+ +/* cxl_setup_regs() populates hdm_map, including its valid bit. */ + rc =cxl_setup_regs (map); +if (rc) +return ERR_PTR (rc); +if (!hdm_map->valid) +return ERR_PTR (-ENODEV ); +if (hdm_map->offset > map->max_size || + hdm_map->size > map->max_size - hdm_map->offset) +return ERR_PTR (-ENXIO );
[Severity: Medium]
Can cxl_setup_regs() read past the end of the ioremap() window if map->max_size is smaller than the component register block?
cxl_decode_regblock() allows map->max_size down to 0, and cxl_map_regblock() maps only map->max_size bytes. Before the bounds check here runs, cxl_probe_component_regs() unconditionally advances base by CXL_CM_OFFSET (0x1000) and reads the capability header and capability offsets without checking map->max_size, which will fault on an unmapped vmalloc guard page for a non-conforming device with a small BAR.
[ ... ]
drivers/pci/cxl.c line 157
Quoted 8 lines
drivers/pci/cxl.c new file +8
-0+if (!(command &PCI_COMMAND_MEMORY )) { + rc =pci_write_config_word (pdev,PCI_COMMAND , + command |PCI_COMMAND_MEMORY ); +if (rc) +return ERR_PTR (pcibios_err_to_errno (rc)); + } + + info =__cxl_pci_hdm_read_info (pdev, map, dvsec);
[Severity: High]
Does setting PCI_COMMAND_MEMORY only on pdev work when upstream PCIe bridges do not yet have memory decoding enabled?
During boot enumeration in subsys_initcall(acpi_init), pcieport has not bound yet (pcie_portdrv_init() is a device_initcall) and __pci_setup_bridge() does not set PCI_COMMAND_MEMORY on bridges. If any upstream Root Port or Switch Port has memory forwarding disabled, the MMIO reads in __cxl_pci_hdm_read_info() receive Unsupported Request responses (0xffffffff) and cache initialization fails silently, leaving pdev->hdm permanently NULL.
[ ... ]
drivers/pci/cxl.c line 196
Quoted 6 lines
drivers/pci/cxl.c new file +6
-0+if (!(cap &PCI_DVSEC_CXL_CACHE_CAPABLE ) || + !(cap &PCI_DVSEC_CXL_MEM_CAPABLE ) || + !(cap &PCI_DVSEC_CXL_RST_CAPABLE ) || + !(cap &PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE ) || + !cxl_dvsec_hdm_count_valid (FIELD_GET (PCI_DVSEC_CXL_HDM_COUNT , cap))) +return 0 ;
[Severity: Medium]
As reported by Jonathan Cameron on v12 [2], should this require PCI_DVSEC_CXL_CACHE_CAPABLE?
Gating cache initialization on PCI_DVSEC_CXL_CACHE_CAPABLE excludes CXL.mem-only endpoints (such as non-class-code-matching Type 3 devices) from ever populating pdev->hdm, so they miss HDM range synchronization and post-reset HDM restoration on CXL bus reset even though the reset helpers in commit "PCI/CXL: Flush host and device caches for CXL bus reset" check PCI_DVSEC_CXL_CACHE_CAPABLE dynamically.
[ ... ]
drivers/pci/cxl.c line 209
Quoted 5 lines
drivers/pci/cxl.c new file +5
-0+ read_info =cxl_pci_hdm_read_info (pdev, &map, dvsec); +if (IS_ERR (read_info)) +return PTR_ERR (read_info); + + pdev->hdm = read_info;
[Severity: High]
As reported by Alison Schofield on v17 [3], does publishing pdev->hdm here allow CXL reset to run before Normalized Addressing has been discovered?
Normalized Addressing cannot be determined from endpoint HDM registers alone; CXL_DECODER_F_NORMALIZED_ADDRESSING is only recorded later during cxl_add_to_region() when CXL endpoint discovery runs. Without a gate checking whether discovery has verified address translation, resetting a device without a bound CXL driver (or before region discovery completes) causes request_mem_region() and cpu_cache_invalidate_memregion() to operate on raw normalized endpoint addresses rather than the translated System Physical Addresses, which can bypass region exclusivity and wipe actively used CXL memory or flush the wrong host cache range.
[1] https://lore.kernel.org/r/akyNQjte2TmeLhE8@MWDK4CY14F
[2] https://lore.kernel.org/r/20260912022617.47860862@jic23-hlaptop
[3] https://lore.kernel.org/r/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
PATCH v18 12/19 PCI/CXL: Add CXL Device Reset sequencing
In reply to Srirangan MadhavanImplement CXL Device Reset for eligible Type 2 devices: disable CXL.cache, perform supported writeback, drain transactions, prepare the IOMMU, initiate reset with Memory Clear enabled, and wait for completion.
Observe the 100 ms post-reset quiet period before polling Status2, and treat an all-ones read as not ready. Wait for the timeout the device advertises, up to 100 seconds, and access only Status2 until reset completion, error or expiry, as CXL 4.0 section 9.7.3 requires. Control2 cleanup runs only afterwards. Treat Control2 command bits as transient state, restore cache policy on exit, preserve the primary error, and abort when IOMMU preparation fails.
Reuse Device DVSEC discovery from cache initialization and keep the reset entry point declaration private to PCI core.
---
Changes in v18:
- Reword the reset wait message to "Waiting for CXL reset to complete" and log an error when the wait times out (Li Ming)
https://lore.kernel.org/linux-cxl/[email protected]/
- Use cxl_pci_read_config_word() for the Control, Control2 and Status2 reads that reject an all-ones value, instead of open-coding the check (Li Ming)
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=04f8f84816d724f8b47f4d47a77916ac37c63a49
--- drivers/pci/cxl.c | 241 +++++++++++++++++++++++++++++++++- drivers/pci/pci.h | 5 + include/uapi/linux/pci_regs.h | 12 ++ 3 files changed, 257 insertions(+), 1 deletion(-)
drivers/pci/cxl.c +240 -1
@@ -3,13 +3,17 @@/* * CXL reset support in the PCI core. Cache the HDM decoder state of a CXL * memory device at enumeration, so it can be restored after a reset without -* a CXL driver. HDM register formats and programming live in drivers/cxl. +* a CXL driver, and implement CXL Device Reset. HDM register formats and +* programming live in drivers/cxl. */ #include <linux/bitfield.h> #include <linux/cleanup.h> +#include <linux/delay.h> #include <linux/errno.h> #include <linux/io.h> #include <linux/ioport.h> +#include <linux/iommu.h> +#include <linux/jiffies.h> #include <linux/kernel.h> #include <linux/pci.h> #include <linux/slab.h>
@@ -224,3 +228,238 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev) {cxl_hdm_cache_release (&pdev->hdm); } + +/* +* 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 +* initiating CXL Reset, then limits software to CXL Status2 access until +* reset completion, timeout, or error. Wait for the full advertised timeout +* so that Control2 is not accessed while the device may still be resetting. +*/ +#define CXL_RESET_RRS_WAIT_MS 100 +#define CXL_RESET_STATUS_POLL_MS 20 +static const u32 cxl_reset_timeout_ms[] = { +10 ,100 ,1000 ,10000 ,100000 , +}; + +/* +* CXL 4.0 section 9.6 permits using the device cache size to determine +* a timeout. Use a conservative fixed estimate of 100 ms. +*/ +#define CXL_CACHE_WBI_TIMEOUT_US 100000 +#define CXL_CACHE_WBI_POLL_US 100 + +#define CXL_RESET_CTRL2_CMD_MASK \ + (PCI_DVSEC_CXL_INIT_CACHE_WBI | PCI_DVSEC_CXL_INIT_CXL_RST) + +static int cxl_reset_update_ctrl2 (struct pci_dev *pdev,int dvsec, +u16 set,u16 clear) +{ +u16 ctrl2; +int rc; + + rc =cxl_pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL2 , &ctrl2); +if (rc) +return rc; + +/* +* INIT_CACHE_WBI and INIT_CXL_RST are commands. Do not replay a command +* that remains set in the value read back while updating persistent +* Control2 state. +*/ + ctrl2 &= ~CXL_RESET_CTRL2_CMD_MASK ; + ctrl2 &= ~clear; + ctrl2 |= set; + + rc =pci_write_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL2 , ctrl2); +if (rc) +return pcibios_err_to_errno (rc); + +return 0 ; +} + +static int cxl_reset_wait_cache_wbi (struct pci_dev *pdev,int dvsec) +{ +unsigned long deadline; +u16 status2; +int rc; + + rc =cxl_reset_update_ctrl2 (pdev, dvsec, +PCI_DVSEC_CXL_INIT_CACHE_WBI ,0 ); +if (rc) +return rc; + + 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 ); + + rc =cxl_pci_read_config_word (pdev, + dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return rc; +if (status2 &PCI_DVSEC_CXL_CACHE_INV ) +return 0 ; + } + +pci_warn (pdev,"timed out waiting for CXL cache writeback and invalidate\n" ); +return -ETIMEDOUT ; +} + +static int cxl_reset_disable_cache (struct pci_dev *pdev,int dvsec,u16 cap) +{ +int rc; + + rc =cxl_reset_update_ctrl2 (pdev, dvsec, +PCI_DVSEC_CXL_DISABLE_CACHING ,0 ); +if (rc || !(cap &PCI_DVSEC_CXL_CACHE_WBI_CAPABLE )) +return rc; + +return cxl_reset_wait_cache_wbi (pdev, dvsec); +} + +static int cxl_reset_restore_cache_policy (struct pci_dev *pdev,int dvsec, +u16 saved_ctrl2) +{ +int rc; + + rc =cxl_reset_update_ctrl2 (pdev, dvsec, + saved_ctrl2 &PCI_DVSEC_CXL_DISABLE_CACHING , +PCI_DVSEC_CXL_DISABLE_CACHING ); +if (rc) +pci_err (pdev,"failed to restore CXL cache policy: %d\n" , rc); + +return rc; +} + +static int cxl_reset_wait_done (struct pci_dev *pdev,int dvsec,u16 cap) +{ +unsigned long deadline; +u32 timeout_ms; +u16 status2; +int idx, rc; + + idx =FIELD_GET (PCI_DVSEC_CXL_RST_TIMEOUT , cap); +if (idx >=ARRAY_SIZE (cxl_reset_timeout_ms)) { + idx =ARRAY_SIZE (cxl_reset_timeout_ms) -1 ; +pci_warn (pdev, +"unknown CXL reset timeout encoding; using %u ms\n" , + cxl_reset_timeout_ms[idx]); + } + +/* Allow one retry after the mandatory 100 ms quiet period. */ + timeout_ms =max_t (u32, cxl_reset_timeout_ms[idx], +CXL_RESET_RRS_WAIT_MS +CXL_RESET_STATUS_POLL_MS ); +pci_info (pdev,"Waiting for CXL reset to complete (max wait: %u ms)\n" , + timeout_ms); +msleep (CXL_RESET_RRS_WAIT_MS ); + deadline = jiffies +msecs_to_jiffies (timeout_ms - +CXL_RESET_RRS_WAIT_MS ); + +for (;;) { + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return pcibios_err_to_errno (rc); +if (status2 !=U16_MAX ) { +if (status2 &PCI_DVSEC_CXL_RST_ERR ) +return -EIO ; +if (status2 &PCI_DVSEC_CXL_RST_DONE ) +return 0 ; + } + +if (time_after_eq (jiffies, deadline)) { +pci_err (pdev,"timed out waiting for CXL reset to complete\n" ); +return -ETIMEDOUT ; + } + +msleep (CXL_RESET_STATUS_POLL_MS ); + } +} + +static int cxl_clear_memory (struct pci_dev *pdev,int dvsec,bool initiate) +{ +if (initiate) +return cxl_reset_update_ctrl2 (pdev, dvsec, +PCI_DVSEC_CXL_INIT_CXL_RST | +PCI_DVSEC_CXL_RST_MEM_CLR_EN , +0 ); + +return cxl_reset_update_ctrl2 (pdev, dvsec,0 , +PCI_DVSEC_CXL_RST_MEM_CLR_EN ); +} + +static int __cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap) +{ +int rc, rc2; + +if (!pci_wait_for_pending_transaction (pdev)) { +pci_err (pdev,"timed out waiting for pending transactions\n" ); +return -ETIMEDOUT ; + } + + rc =pci_dev_reset_iommu_prepare (pdev); +if (rc) { +pci_err (pdev,"failed to stop IOMMU for CXL reset: %d\n" , rc); +return rc; + } + + rc =cxl_clear_memory (pdev, dvsec, true); +if (!rc) + rc =cxl_reset_wait_done (pdev, dvsec, cap); + +/* Always clear Memory Clear; successful cleanup must not hide reset failure. */ + rc2 =cxl_clear_memory (pdev, dvsec, false); +if (rc2) +pci_err (pdev,"failed to clear CXL Reset Memory Clear: %d\n" , rc2); + rc = rc ?: rc2; + +pci_dev_reset_iommu_done (pdev); +return rc; +} + +static int cxl_reset_execute (struct pci_dev *pdev,int dvsec,u16 cap) +{ +u16 saved_ctrl2; +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); +/* Restore cache policy after any attempt to disable caching. */ + rc2 =cxl_reset_restore_cache_policy (pdev, dvsec, saved_ctrl2); +return rc ?: rc2; +} + +int cxl_reset_function (struct pci_dev *pdev,bool probe) +{ +u16 dvsec, cap, ctrl; +int rc; + + rc =cxl_pci_device_dvsec (pdev, &dvsec, &cap); +if (rc) +return rc; +if (!(cap &PCI_DVSEC_CXL_CACHE_CAPABLE ) || + !(cap &PCI_DVSEC_CXL_MEM_CAPABLE ) || + !(cap &PCI_DVSEC_CXL_RST_CAPABLE ) || + !(cap &PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE )) +return -ENOTTY ; + + rc =cxl_pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , &ctrl); +if (rc) +return rc; +if (!(ctrl &PCI_DVSEC_CXL_CACHE_ENABLE ) || + !(ctrl &PCI_DVSEC_CXL_MEM_ENABLE )) +return -ENOTTY ; + +if (probe) +return 0 ; + +return cxl_reset_execute (pdev, dvsec, cap); +}
drivers/pci/pci.h +5 -0
@@ -739,9 +739,14 @@ static inline void pci_doe_disconnected(struct pci_dev *pdev) { }#ifdef CONFIG_CXL_RESET void pci_cxl_hdm_cache_init (struct pci_dev *pdev);void pci_cxl_hdm_cache_release (struct pci_dev *pdev); +int cxl_reset_function (struct pci_dev *pdev,bool probe);#else static inline void pci_cxl_hdm_cache_init (struct pci_dev *pdev) { }static inline void pci_cxl_hdm_cache_release (struct pci_dev *pdev) { } +static inline int cxl_reset_function (struct pci_dev *pdev,bool probe) +{ +return -ENOTTY ; +}#endif #ifdef CONFIG_PCI_NPEM
include/uapi/linux/pci_regs.h +12 -0
@@ -1352,10 +1352,22 @@#define PCI_DVSEC_CXL_CACHE_CAPABLE _BITUL(0)#define PCI_DVSEC_CXL_MEM_CAPABLE _BITUL(2)#define PCI_DVSEC_CXL_HDM_COUNT __GENMASK(5, 4) +#define PCI_DVSEC_CXL_CACHE_WBI_CAPABLE _BITUL(6)#define PCI_DVSEC_CXL_RST_CAPABLE _BITUL(7) +#define PCI_DVSEC_CXL_RST_TIMEOUT __GENMASK(10, 8)#define PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE _BITUL(11)#define PCI_DVSEC_CXL_CTRL 0xC +#define PCI_DVSEC_CXL_CACHE_ENABLE _BITUL(0)#define PCI_DVSEC_CXL_MEM_ENABLE _BITUL(2) +#define PCI_DVSEC_CXL_CTRL2 0x10 +#define PCI_DVSEC_CXL_DISABLE_CACHING _BITUL(0) +#define PCI_DVSEC_CXL_INIT_CACHE_WBI _BITUL(1) +#define PCI_DVSEC_CXL_INIT_CXL_RST _BITUL(2) +#define PCI_DVSEC_CXL_RST_MEM_CLR_EN _BITUL(3) +#define PCI_DVSEC_CXL_STATUS2 0x12 +#define PCI_DVSEC_CXL_CACHE_INV _BITUL(0) +#define PCI_DVSEC_CXL_RST_DONE _BITUL(1) +#define PCI_DVSEC_CXL_RST_ERR _BITUL(2)#define PCI_DVSEC_CXL_RANGE_SIZE_HIGH (i) (0x18 + (i * 0x10))#define PCI_DVSEC_CXL_RANGE_SIZE_LOW (i) (0x1C + (i * 0x10))#define PCI_DVSEC_CXL_MEM_INFO_VALID _BITUL(0)
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 12/19] PCI/CXL: Add CXL Device Reset sequencing
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [Medium] PCI/CXL: cxl_reset_function() gates probe on dynamic CXL enable bits - [Medium] PCI/CXL…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [Medium] PCI/CXL: cxl_reset_function() gates probe on dynamic CXL enable bits
- [Medium] PCI/CXL: cxl_reset_wait_cache_wbi() misses post-deadline status read
- [Medium] PCI/CXL: cxl_reset_wait_done() checks deadline after status read
- [Medium] PCI/CXL: cxl_reset_function() requires CXL.cache capability and enable
--
commit 7d770790d7d42c69d9dc663a5c3fce0ee4f98b17
Author: Srirangan Madhavan <[email protected]>
PCI/CXL: Add CXL Device Reset sequencing
This commit adds CXL Device Reset sequencing for Type 2 devices, including cache writeback and invalidation, transaction draining, IOMMU preparation, reset initiation with Memory Clear, and completion polling.
Quoted 4 lines
drivers/pci/cxl.c +0
-0
[ ... ]
Quoted 1 line
drivers/pci/cxl.c +0
-0@@ -224,3 +228,238 @@ void pci_cxl_hdm_cache_release(struct pci_dev *pdev)
[ ... ]
Quoted 18 lines
drivers/pci/cxl.c +18
-0+static int cxl_reset_wait_cache_wbi (struct pci_dev *pdev,int dvsec) +{ +unsigned long deadline; +u16 status2; +int rc; + + rc =cxl_reset_update_ctrl2 (pdev, dvsec, +PCI_DVSEC_CXL_INIT_CACHE_WBI ,0 ); +if (rc) +return rc; + + 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 ); + + rc = cxl_pci_read_config_word(pdev, + dvsec + PCI_DVSEC_CXL_STATUS2,
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 308
Quoted 10 lines
drivers/pci/cxl.c +10
-0+ &status2); +if (rc) +return rc; +if (status2 &PCI_DVSEC_CXL_CACHE_INV ) +return 0 ; + } + +pci_warn (pdev,"timed out waiting for CXL cache writeback and invalidate\n" ); +return -ETIMEDOUT ; +}
[Severity: Medium]
Can cxl_reset_wait_cache_wbi() return a spurious -ETIMEDOUT without re-checking PCI_DVSEC_CXL_STATUS2 after the deadline passes?
If the thread is preempted across the 100 ms deadline right after computing deadline, or after a poll where PCI_DVSEC_CXL_CACHE_INV was still clear, time_before() exits the loop without reading Status2 again. That aborts the reset even if hardware completed writeback and invalidation in time.
Sampling whether the timeout expired before reading Status2 (as poll_timeout_us() does) ensures one final check after the deadline.
[ ... ]
Quoted 9 lines
drivers/pci/cxl.c +9
-0+for (;;) { + rc =pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_STATUS2 , + &status2); +if (rc) +return pcibios_err_to_errno (rc); +if (status2 !=U16_MAX ) { +if (status2 &PCI_DVSEC_CXL_RST_ERR ) +return -EIO ; +if (status2 &PCI_DVSEC_CXL_RST_DONE )
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 378
Quoted 10 lines
drivers/pci/cxl.c +10
-0+return 0 ; + } + +if (time_after_eq (jiffies, deadline)) { +pci_err (pdev,"timed out waiting for CXL reset to complete\n" ); +return -ETIMEDOUT ; + } + +msleep (CXL_RESET_STATUS_POLL_MS ); + }
[Severity: Medium]
Can cxl_reset_wait_done() also report a spurious -ETIMEDOUT by checking time_after_eq(jiffies, deadline) after pci_read_config_word()?
For a device advertising a 10 ms or 100 ms reset timeout, deadline is only 20 ms after the 100 ms quiet period. If the first config read stalls in RRS retries or the thread is preempted before the timeout check, the loop returns -ETIMEDOUT without reading Status2 again after the deadline.
Recording expiration before reading PCI_DVSEC_CXL_STATUS2 avoids failing a reset that completed before the deadline.
[ ... ]
Quoted 10 lines
drivers/pci/cxl.c +10
-0+int cxl_reset_function (struct pci_dev *pdev,bool probe) +{ +u16 dvsec, cap, ctrl; +int rc; + + rc =cxl_pci_device_dvsec (pdev, &dvsec, &cap); +if (rc) +return rc; + if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) || + !(cap & PCI_DVSEC_CXL_MEM_CAPABLE) ||
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 459
Quoted 10 lines
drivers/pci/cxl.c +10
-0+ !(cap &PCI_DVSEC_CXL_RST_CAPABLE ) || + !(cap & PCI_DVSEC_CXL_RST_MEM_CLR_CAPABLE)) + return -ENOTTY; + + rc =cxl_pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CTRL , &ctrl); +if (rc) +return rc; +if (!(ctrl &PCI_DVSEC_CXL_CACHE_ENABLE ) || + !(ctrl &PCI_DVSEC_CXL_MEM_ENABLE )) +return -ENOTTY ;
[Severity: Medium]
As reported by Jonathan Cameron on v12 [1], should cxl_reset_function() require PCI_DVSEC_CXL_CACHE_CAPABLE and PCI_DVSEC_CXL_CACHE_ENABLE?
A Type 2 device running with CXL.mem enabled and CXL.cache disabled, or a CXL.mem-only device with CXL Reset and Reset Memory Clear, gets -ENOTTY here and falls through to FLR without clearing device memory or resetting CXL.mem state.
Relaxing the CXL.cache requirement here and gating cache disable, WBI, and policy restore in cxl_reset_execute() on CXL.cache being capable and enabled would cover those devices as well.
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 462
Quoted 3 lines
drivers/pci/cxl.c +3
-0+ +if (probe) +return 0 ;
[Severity: Medium]
Should the PCI_DVSEC_CXL_CTRL enable check happen after the probe return rather than before it?
pci_init_reset_methods() probes reset methods once during PCI enumeration, before any driver binds to enable PCI_DVSEC_CXL_MEM_ENABLE or PCI_DVSEC_CXL_CACHE_ENABLE. If firmware leaves them disabled, cxl_reset is permanently excluded from dev->reset_methods[] and later resets fall back to FLR or bus reset without CXL Memory Clear.
Checking only static capability bits in PCI_DVSEC_CXL_CAP during probe and checking PCI_DVSEC_CXL_CTRL only when executing the reset lets __pci_reset_function_locked() fall through cleanly when CXL is disabled.
[1] https://lore.kernel.org/r/20260912022617.47860862@jic23-hlaptop
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH v18 13/19 cxl: Validate and synchronize HDM ranges around reset
In reply to Srirangan MadhavanRefuse 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.
---
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/[email protected]?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/[email protected]?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/[email protected]/
- 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/[email protected]/
- 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(-)
drivers/cxl/Kconfig +2 -1
@@ -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.
drivers/cxl/core/hdm.c +44 -8
74 lines not on this page · raw message
drivers/cxl/core/hdm_state.c +336 -5
401 lines not on this page · raw message
drivers/cxl/core/region.c +15 -0
@@ -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);
drivers/pci/cxl.c +148 -11
218 lines not on this page · raw message
include/cxl/hdm.h +55 -1
86 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 13/19] cxl: Validate and synchronize HDM ranges around reset
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…
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 <[email protected]>
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.
Quoted 5 lines
drivers/cxl/core/hdm.c +0
-0
drivers/cxl/core/hdm.c static void cxl_hdm_refresh_decoder(struct cxl_hdm *cxlhdm, · line 772
Quoted 10 lines
drivers/cxl/core/hdm.c +4
-1@@ -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 CPU1devm_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.
[ ... ]
Quoted 11 lines
drivers/cxl/core/hdm.c +4
-3@@ -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));
drivers/cxl/core/hdm.c static void cxl_decoder_reset(struct cxl_decoder *cxld) · line 957
Quoted 10 lines
drivers/cxl/core/hdm.c +5
-4-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.
Quoted 4 lines
drivers/cxl/core/hdm_state.c +0
-0
[ ... ]
drivers/cxl/core/hdm_state.c void cxl_hdm_cache_release(struct cxl_hdm_info **slot) · line 255
Quoted 5 lines
drivers/cxl/core/hdm_state.c +5
-0+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.
[ ... ]
Quoted 14 lines
drivers/cxl/core/hdm_state.c +14
-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 ; +
drivers/cxl/core/hdm_state.c void cxl_hdm_cache_release(struct cxl_hdm_info **slot) · line 378
Quoted 10 lines
drivers/cxl/core/hdm_state.c +10
-0+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.
[ ... ]
Quoted 8 lines
drivers/cxl/core/hdm_state.c +8
-0+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); +
drivers/cxl/core/hdm_state.c void cxl_hdm_cache_release(struct cxl_hdm_info **slot) · line 449
Quoted 10 lines
drivers/cxl/core/hdm_state.c +10
-0+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.
Quoted 4 lines
drivers/pci/cxl.c +0
-0
[ ... ]
Quoted 6 lines
drivers/pci/cxl.c +6
-0+/* 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; + } +/*
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 283
Quoted 10 lines
drivers/pci/cxl.c +10
-0+* 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.
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 295
Quoted 12 lines
drivers/pci/cxl.c +12
-0+ + 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 CPU1pci_dev_save_and_disable()
PCI_COMMAND_MEMORY = 0cxl_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 settingscxl_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.
[ ... ]
Quoted 4 lines
drivers/pci/cxl.c +4
-0+/* 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) +{
drivers/pci/cxl.c static int cxl_reset_restore_cache_policy(struct pci_dev *pdev, int dvsec, · line 444
Quoted 10 lines
drivers/pci/cxl.c +10
-0+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().
[ ... ]
Quoted 44 lines
drivers/pci/cxl.c +27
-9+ 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);
drivers/pci/cxl.c static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) · line 575
Quoted 10 lines
drivers/pci/cxl.c +6
-1+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/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
PATCH v18 14/19 PCI/CXL: Reject reset with unsafe function scope
In reply to Srirangan MadhavanThe per-device PCI reset interface must not reset sibling functions. Reject cxl_reset and cxl_bus for VFs, multifunction devices, including function 0 of an ARI device that has other functions, and PFs with enabled VFs.
---
Changes in v18:
- Also reject function 0 of an ARI device when other functions share the bus, since ->multifunction is not set for it (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=14
- Check for ARI siblings only when the reset executes. While reset methods are probed, the device is not on its bus list yet and its siblings are not enumerated, so the check could not tell whether siblings exist and rejected cxl_reset and cxl_bus for the first device on an ARI-forwarding bus. Found while testing on an ARM64 system with a CXL Type 2 device.
- Keep Bjorn's Acked-by and Richard's Reviewed-by, since only the point where the sibling check runs changed
--- drivers/pci/cxl.c | 10 ++++++++++ drivers/pci/pci.c | 13 +++++++++++++ 2 files changed, 23 insertions(+)
drivers/pci/cxl.c +10 -0
@@ -579,6 +579,16 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)u16 dvsec, cap, ctrl;int rc; +/* +* ->multifunction is not set for function 0 of an ARI device, so look +* for siblings on the bus. Skip this when probing: @pdev is not on the +* bus list yet and its siblings are not enumerated. +*/ +if (pdev->is_virtfn || pdev->multifunction ||pci_num_vf (pdev) || + (!probe &&pci_ari_enabled (pdev->bus) && + !list_is_singular (&pdev->bus->devices))) +return -ENOTTY ; + rc =cxl_pci_device_dvsec (pdev, &dvsec, &cap);if (rc)return rc;
drivers/pci/pci.c +13 -0
@@ -5064,6 +5064,19 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)u16 dvsec, reg, val;int rc; +/* +* Reject VFs and multifunction devices before walking the bridge's +* capabilities: every VF behind the same bridge would repeat the walk. +* +* ->multifunction is not set for function 0 of an ARI device, so look +* for siblings on the bus. Skip this when probing: @dev is not on the +* bus list yet and its siblings are not enumerated. +*/ +if (dev->is_virtfn || dev->multifunction ||pci_num_vf (dev) || + (!probe &&pci_ari_enabled (dev->bus) && + !list_is_singular (&dev->bus->devices))) +return -ENOTTY ; + bridge =pci_upstream_bridge (dev);if (!bridge)return -ENOTTY ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 14/19] PCI/CXL: Reject reset with unsafe function scope
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] PCI/CXL: bus reset precedes cxl_bus and bypasses cxl_reset_bus_function() - [Medium] PCI/CXL: !probe…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] PCI/CXL: bus reset precedes cxl_bus and bypasses cxl_reset_bus_function()
- [Medium] PCI/CXL: !probe ARI guard falsely advertises cxl_reset and cxl_bus
- [Medium] PCI/CXL: unlocked list_is_singular() read of bus->devices without pci_bus_sem
- [Medium] PCI/CXL: probe-time scope check misses fn > 0 with MFD=0 and ARI fn 0 in sysfs
- [Low] PCI/CXL: unlocked pci_num_vf() read in reset_method_store() probe path
- [Low] PCI/CXL: duplicated function-scope guard in cxl.c and pci.c
- [Low] PCI/CXL: missing Fixes trailer for cxl_reset_bus_function() scope bug
--
commit edf4c40d8768b9c15e4a2d492055de063f8458bf
Author: Srirangan Madhavan <[email protected]>
Quoted 7 lines
PCI/CXL: Reject reset with unsafe function scope
The per-device PCI reset interface must not reset sibling functions. Reject
cxl_resetandcxl_busfor VFs, multifunction devices, including function 0 of an ARI device that has other functions, and PFs with enabled VFs.
Quoted 6 lines
Suggested-by Alex Williamson <[email protected]>Signed-off-by Srirangan Madhavan <[email protected]>Acked-by Bjorn Helgaas <[email protected]>Reviewed-by Richard Cheng <[email protected]>
[Severity: Low]
This fixes a pre-existing bug in cxl_reset_bus_function(), which could advertise cxl_bus and reset sibling functions on VFs, multifunction devices, or PFs with enabled VFs. Should this include:
Quoted 11 lines
drivers/pci/cxl.c +3
-0@@ -579,6 +579,16 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)u16 dvsec, cap, ctrl;int rc; +/* +* ->multifunction is not set for function 0 of an ARI device, so look +* for siblings on the bus. Skip this when probing: @pdev is not on the
drivers/pci/cxl.c int cxl_reset_function(struct pci_dev *pdev, bool probe) · line 594
Quoted 10 lines
drivers/pci/cxl.c +7
-0+* bus list yet and its siblings are not enumerated. +*/ +if (pdev->is_virtfn || pdev->multifunction ||pci_num_vf (pdev) || + (!probe &&pci_ari_enabled (pdev->bus) && + !list_is_singular (&pdev->bus->devices))) +return -ENOTTY ; + rc =cxl_pci_device_dvsec (pdev, &dvsec, &cap);if (rc)return rc;
[Severity: Medium]
Does gating the ARI sibling check on !probe cause cxl_reset and cxl_bus to be falsely advertised on Function 0 of an ARI multifunction device?
Because !probe skips the check, pci_init_reset_methods() records cxl_reset and cxl_bus in dev->reset_methods[], and post-enumeration writes to sysfs reset_method still pass PCI_RESET_PROBE in reset_method_store() instead of returning -EINVAL. If no other reset method is available, pci_reset_function() then runs pci_dev_save_and_disable() (calling err_handler->reset_prepare(), forcing D0, and clearing PCI_COMMAND) before __pci_reset_function_locked() returns -ENOTTY.
Checking PCI_ARI_CAP_NFN on ARI Function 0 (as next_ari_fn() does) and scanning bus->devices for any entry other than dev (as pci_parent_bus_reset() does) avoids the !probe bypass in both cxl_reset_function() and cxl_reset_bus_function().
[Severity: Medium]
Can non-zero functions with PCI_HEADER_TYPE_MFD clear slip past this check during initial probe?
In pci_scan_slot(), pci_scan_single_device() runs pci_device_add() -> pci_init_reset_methods() before dev->multifunction = 1 is set for fn > 0:
drivers/pci/probe.c:pci_scan_slot() {
...
dev = pci_scan_single_device(bus, devfn + fn);
if (dev) {
if (!pci_dev_is_added(dev))
nr++;
if (fn > 0)
dev->multifunction = 1;
}
...
}For ARI sibling functions (where PCI_HEADER_TYPE_MFD is 0) and conventional multifunction devices that leave PCI_HEADER_TYPE_MFD clear on fn > 0, dev->multifunction is still 0 during PCI_RESET_PROBE. Both cxl_reset_function() and cxl_reset_bus_function() then walk DVSEC config space unnecessarily and record unusable cxl_reset and cxl_bus methods in dev->reset_methods[]. Rejecting non-zero function numbers directly would catch this during initial enumeration.
[Severity: Medium]
Is list_is_singular(&pdev->bus->devices) safe without holding pci_bus_sem?
pci_device_add() and pci_destroy_dev() mutate bus->devices under down_write(&pci_bus_sem) without taking device_lock on sibling functions, while callers of __pci_reset_function_locked() hold device_lock(&dev->dev) and not pci_bus_sem:
CPU0 CPU1pci_reset_function(fn0)
device_lock(&fn0->dev)
cxl_reset_function(fn0, false)
list_is_singular(&bus->devices) <-- data race
pci_scan_single_device(bus, fn1)
pci_device_add(fn1)
down_write(&pci_bus_sem)
list_add_tail(..., &bus->devices)
up_write(&pci_bus_sem)
cxl_reset_execute(fn0, ...) <-- resets active sibling fn1This is a data race on bus->devices in both cxl_reset_function() and cxl_reset_bus_function(), and also leaves a window during reset execution where a newly rescanned or hot-added ARI sibling can be reset while active. Taking down_read(&pci_bus_sem) around the bus->devices check is lock-order safe under device_lock, as pci_bridge_wait_for_secondary_bus() already does on the reset path.
[Severity: Low]
Can calling pci_num_vf() when probe is true race with sriov_numvfs_store()?
reset_method_store() calls pci_init_reset_methods() and the reset probe callback without holding device_lock(&pdev->dev), whereas sriov_enable() and sriov_disable() write pdev->sriov->num_VFs under device_lock(&pdev->dev) (as documented in sriov_numvfs_show()). Gating the dynamic pci_num_vf() check on !probe, where __pci_reset_function_locked() holds device_lock, would avoid this data race in both cxl_reset_function() and cxl_reset_bus_function().
Quoted 14 lines
drivers/pci/pci.c +6
-0@@ -5064,6 +5064,19 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)u16 dvsec, reg, val;int rc; +/* +* Reject VFs and multifunction devices before walking the bridge's +* capabilities: every VF behind the same bridge would repeat the walk. +* +* ->multifunction is not set for function 0 of an ARI device, so look +* for siblings on the bus. Skip this when probing: @dev is not on the
drivers/pci/pci.c static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) · line 5082
Quoted 10 lines
drivers/pci/pci.c +7
-0+* bus list yet and its siblings are not enumerated. +*/ +if (dev->is_virtfn || dev->multifunction ||pci_num_vf (dev) || + (!probe &&pci_ari_enabled (dev->bus) && + !list_is_singular (&dev->bus->devices))) +return -ENOTTY ; + bridge =pci_upstream_bridge (dev);if (!bridge)return -ENOTTY ;
[Severity: Low]
This isn't a bug, but this function-scope guard and comment are duplicated verbatim between cxl_reset_function() and cxl_reset_bus_function(). Extracting a small shared helper in drivers/pci/pci.h would keep both CXL reset methods in sync.
[Severity: High]
Can generic bus reset still bypass cxl_reset_bus_function() and these scope checks when SBR is already unmasked on the upstream CXL Port?
In drivers/pci/pci.c, pci_reset_fn_methods[] places pci_reset_bus_function ahead of cxl_reset_bus_function, and pci_reset_bus_function() only returns -ENOTTY when cxl_sbr_masked() is true:
drivers/pci/pci.c:pci_reset_bus_function() {
...
if (bridge && pcie_is_cxl(bridge) && cxl_sbr_masked(bridge))
return -ENOTTY;
...
}drivers/pci/pci.c:pci_reset_fn_methods[] {
...
{ pci_reset_bus_function, .name = "bus" },
{ cxl_reset_bus_function, .name = "cxl_bus" },
}When PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR is already set (for example by firmware or an administrator), cxl_sbr_masked() returns false, so generic bus reset runs first and never reaches cxl_reset_bus_function(). That bypasses the scope checks added here, allowing hotplug slot resets of VFs, PFs with enabled VFs, and ARI Function 0 with siblings via pci_dev_reset_slot_function(), and also bypasses any CXL-specific reset handling in cxl_reset_bus_function().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
PATCH v18 15/19 PCI/CXL: Restore CXL state after PCI reset
In reply to Srirangan MadhavanRestore the PCI configuration needed for HDM access, followed by cached HDM decoder and CXL Device DVSEC state. Set Lock On Commit before recommitting a cleared locked decoder; leave surviving locked state unchanged.
Stop at the first restoration failure and preserve a disabled PCI command snapshot. Keep IOMMU exclusion active through restoration and post-reset cache invalidation.
Program the decoders from built-in CXL code that takes only a mapped register block, and keep PCI state handling in drivers/pci/cxl.c.
The restore helper structure and diagnostics follow Dave Jiang's reference implementation on his cxl-type2-reset branch.
---
Changes in v18:
- Restore the state in a short region-lock window of its own, cxl_reset_hdm_restore(), now that the lock is no longer held across the reset, and let a region teardown write the registers again there (Dave)
https://lore.kernel.org/linux-cxl/[email protected]/
- Follow the bus reset rules when a CXL Reset fails: save the disabled state if the cache policy cannot be restored after an aborted reset, and keep caching off after a reset that was started and failed (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=15
https://sashiko.dev/#/patchset/[email protected]?part=18
- Restore the DVSEC Control with CXL.cache off and turn it on only after the host caches are flushed and the cache policy is restored, so the device does not cache again before the flush (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=18
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=81fff5893a3140361cac625b09ada798afb92e03
--- drivers/cxl/core/core.h | 2 +- drivers/cxl/core/hdm.c | 2 +- drivers/cxl/core/hdm_regs.c | 177 +++++++++++++++++++++++-- drivers/pci/cxl.c | 249 ++++++++++++++++++++++++++++++++++-- include/cxl/hdm.h | 2 + 5 files changed, 403 insertions(+), 29 deletions(-)
drivers/cxl/core/core.h +1 -1
@@ -145,7 +145,7 @@ void cxl_commit_start_endpoint(const struct cxl_endpoint_decoder_settings *setti void __iomem *hdm);void cxl_commit_start_switch (const struct cxl_decoder_config *config,u64 targets,void __iomem *hdm); -int cxld_await_commit (void __iomem *hdm,int id); +int cxld_await_commit (void __iomem *hdm,int id,bool committed);int cxl_hdm_unpack_decoder (struct cxl_decoder_config *config,int id,u32 ctrl,u64 base,u64 size);
drivers/cxl/core/hdm.c +1 -1
@@ -874,7 +874,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld) scoped_guard(rwsem_read, &cxl_rwsem.dpa)setup_hw_decoder (cxld, hdm); - rc =cxld_await_commit (hdm, cxld->config.id); + rc =cxld_await_commit (hdm, cxld->config.id, true);if (rc) {dev_dbg (&port->dev,"%s: error %d committing decoder\n" ,dev_name (&cxld->dev), rc);
drivers/cxl/core/hdm_regs.c +163 -14
225 lines not on this page · raw message
drivers/pci/cxl.c +236 -13
320 lines not on this page · raw message
include/cxl/hdm.h +2 -0
@@ -60,6 +60,8 @@ int cxl_hdm_decoder_count_mapped(struct device *dev, void __iomem *hdm, resource_size_t size);int cxl_hdm_read_state (struct device *dev,void __iomem *hdm,struct cxl_hdm_info *info); +int cxl_hdm_restore_state (void __iomem *hdm, +const struct cxl_hdm_info *snapshot);void cxl_hdm_cache_set_dvsec_ctrl (struct pci_dev *pdev,u16 ctrl);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,
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 15/19] PCI/CXL: Restore CXL state after PCI reset
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [Medium] cxl: cxl_hdm_restore_state() uncommits decoders in ascending order - [Medium] PCI/CXL…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [Medium] cxl: cxl_hdm_restore_state() uncommits decoders in ascending order
- [Medium] PCI/CXL: regs_offline and reset_pending cleared before reset cleanup finishes
- [Medium] PCI/CXL: cxl_reset_save_disabled_state() clobbers saved PCI state on error
- [Medium] PCI/CXL: cxl_reset_hdm_restore() writes stale dvsec_ctrl and global_ctrl
- [Medium] PCI/CXL: cxl_reset_save_disabled_state() leaves CXL.mem and CXL.cache enabled
- [Medium] PCI/CXL: race between cxl_reset_execute() and cxl_port probe/unbind
- [Medium] PCI/CXL: cxl_reset_execute() enables MSE/BME before outer pci_restore_state()
--
commit 46128862bc7d9a15961d5d75eb89380f613d4b4f
Author: Srirangan Madhavan <[email protected]>
PCI/CXL: Restore CXL state after PCI reset
This commit adds built-in HDM decoder restoration helpers in drivers/cxl/core/hdm_regs.c and hooks them into the CXL Device Reset path in drivers/pci/cxl.c to restore PCI state, HDM decoders, and CXL Device DVSEC control after reset.
Quoted 4 lines
drivers/cxl/core/hdm_regs.c +0
-0
[ ... ]
Quoted 10 lines
drivers/cxl/core/hdm_regs.c +10
-0+static int +cxl_hdm_restore_decoder (void __iomem *hdm, +const struct cxl_endpoint_decoder_settings *settings) +{ +const struct cxl_decoder_config *config = &settings->config; +bool retained; +u32 ctrl; +int rc; + + rc =cxl_hdm_decoder_uncommit (hdm, config->id, &retained);
[ ... ]
Quoted 8 lines
drivers/cxl/core/hdm_regs.c +8
-0+int cxl_hdm_restore_state (void __iomem *hdm, +const struct cxl_hdm_info *snapshot) +{ +int count; +int rc; + + count =cxl_hdm_decoder_count (readl (hdm +CXL_HDM_DECODER_CAP_OFFSET )); +if (count != snapshot->decoder_count)
drivers/cxl/core/hdm_regs.c int cxl_hdm_read_state(struct device *dev, void __iomem *hdm, · line 437
Quoted 10 lines
drivers/cxl/core/hdm_regs.c +10
-0+return -ENXIO ; + +/* Restore global HDM control before committing individual decoders. */ +writel (snapshot->global_ctrl, hdm +CXL_HDM_DECODER_CTRL_OFFSET ); + +for (int i =0 ; i < snapshot->decoder_count; i++) { + rc =cxl_hdm_restore_decoder (hdm, &snapshot->settings[i]); +if (rc) +return rc; + }
[Severity: Medium]
Can this ascending loop uncommit HDM decoders out of order when multiple decoders survive reset?
CXL requires HDM decoders to be uncommitted in descending order and committed in ascending order. If decoder 0 and decoder 1 remain committed in hardware, iteration 0 clears Commit on decoder 0 while decoder 1 is still committed.
Spec-compliant hardware rejects out-of-order uncommit with Commit Error (-EIO) or never clears Committed (-ETIMEDOUT), aborting restoration and leaving the PCI device disabled. Performing a descending uncommit pass down to any locked committed decoder before the ascending commit pass would preserve hardware ordering.
Quoted 4 lines
drivers/pci/cxl.c +0
-0
[ ... ]
Quoted 23 lines
drivers/pci/cxl.c +23
-0+static int cxl_restore_dvsec_ctrl (struct pci_dev *pdev, +const struct cxl_hdm_info *snapshot) +{ +int rc; + +if (!snapshot->dvsec_ctrl_valid) { +pci_err (pdev,"CXL DVSEC control snapshot is invalid\n" ); +return -ENXIO ; + } + +/* Leave CXL.cache off until the host caches are flushed. */ + rc =pci_write_config_word (pdev, snapshot->dvsec +PCI_DVSEC_CXL_CTRL , + snapshot->dvsec_ctrl & + ~PCI_DVSEC_CXL_CACHE_ENABLE ); +if (rc) { + rc =pcibios_err_to_errno (rc); +pci_err (pdev,"failed to restore CXL DVSEC control: %d\n" , rc); + } + +return rc; +} + +/* Turn CXL.cache back on after the host caches are flushed. */
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 327
Quoted 10 lines
drivers/pci/cxl.c +10
-0+static int cxl_reset_enable_cache (struct pci_dev *pdev, +const struct cxl_hdm_info *snapshot) +{ +int rc; + +if (!(snapshot->dvsec_ctrl &PCI_DVSEC_CXL_CACHE_ENABLE )) +return 0 ; + + rc =pci_write_config_word (pdev, snapshot->dvsec +PCI_DVSEC_CXL_CTRL , + snapshot->dvsec_ctrl);
[Severity: Medium]
Can cxl_restore_dvsec_ctrl(), cxl_reset_enable_cache(), and cxl_hdm_restore_state() write stale pre-reset dvsec_ctrl and global_ctrl values?
Because cxl_rwsem.region is dropped after cxl_reset_hdm_prepare() and again after cxl_reset_hdm_restore(), a concurrent cxl_port unbind can run clear_mem_enable() and disable_hdm(), clearing PCI_DVSEC_CXL_MEM_ENABLE and CXL_HDM_DECODER_ENABLE in both hardware and pdev->hdm.
The restore path still writes the pre-reset snapshot->global_ctrl and the full snapshot->dvsec_ctrl word, re-enabling CXL.mem or global HDM decoding while cxl_port is unbound. If unbind runs between cxl_reset_hdm_restore() and cxl_reset_enable_cache(), writing the full snapshot->dvsec_ctrl word turns PCI_DVSEC_CXL_MEM_ENABLE back on while CXL_HDM_DECODER_ENABLE remains cleared.
Later commit "PCI/CXL: Restore CXL state after CXL bus reset" refreshes per-decoder settings from pdev->hdm, but leaves global_ctrl and dvsec_ctrl stale. Refreshing global_ctrl and dvsec_ctrl from pdev->hdm and using read-modify-write for PCI_DVSEC_CXL_CACHE_ENABLE would avoid restoring stale enable bits.
[ ... ]
Quoted 14 lines
drivers/pci/cxl.c +14
-0+static void cxl_reset_save_disabled_state (struct pci_dev *pdev) +{ +u32 *saved_command = &pdev->saved_config_space[PCI_COMMAND /4 ]; +int rc; + + rc =pci_write_config_word (pdev,PCI_COMMAND ,PCI_COMMAND_INTX_DISABLE ); +if (rc) +pci_err (pdev, +"failed to disable device after CXL reset failure: %d\n" , +pcibios_err_to_errno (rc)); + +/* Preserve the pre-reset snapshot if PCI state has not been restored. */ +if (!pdev->state_saved) { + rc =pci_save_state (pdev);
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 384
Quoted 10 lines
drivers/pci/cxl.c +10
-0+if (rc) +pci_err (pdev, +"failed to save disabled state after CXL reset failure: %d\n" , + rc); + } + +/* Ensure the outer PCI restore cannot re-enable MSE or BME. */ + *saved_command &=GENMASK (31 ,16 ); + *saved_command |=PCI_COMMAND_INTX_DISABLE ; +}
[Severity: Medium]
Can calling pci_save_state() here overwrite the intact pre-reset saved PCI state with 0xffffffff when the device is unresponsive?
cxl_restore_pci_state_for_hdm_restore() calls pci_restore_state(), which clears pdev->state_saved while leaving pdev->saved_config_space[] and saved capability buffers intact. If a later step fails because the device stopped responding, !pdev->state_saved is true and pci_save_state() reads all 16 header dwords and saved capabilities without checking for read errors or 0xffffffff.
That overwrites the saved BARs and capability buffers with all-ones, which the outer pci_dev_restore() then applies. Preserving the existing saved buffers instead of re-reading from a failed device would avoid clobbering them.
drivers/pci/cxl.c void pci_cxl_hdm_cache_release(struct pci_dev *pdev) · line 398
Quoted 13 lines
drivers/pci/cxl.c +13
-0+static int cxl_reset_save_restored_state (struct pci_dev *pdev,u16 command) +{ +int rc; + + rc =pci_write_config_word (pdev,PCI_COMMAND , command); +if (rc) { + rc =pcibios_err_to_errno (rc); +pci_err (pdev,"failed to restore PCI command state: %d\n" , rc); +cxl_reset_save_disabled_state (pdev); +return rc; + } + + rc =pci_save_state (pdev);
[Severity: Medium]
Does enabling MSE and BME here before returning cause the outer pci_dev_restore() -> pci_restore_state() call to reprogram PCI capabilities on an active device?
cxl_restore_pci_state_for_hdm_restore() already runs a full pci_restore_state(), and cxl_reset_save_restored_state() then writes PCI_COMMAND (enabling MSE and BME) and re-saves state. When __pci_reset_function_locked() returns, pci_dev_restore() unconditionally calls pci_restore_state() a second time while MSE, BME, and all PCI capabilities are already enabled.
On that second pass, pci_restore_rebar_state() writes PCI_REBAR_CTRL while PCI_COMMAND_MEMORY is set (violating the PCIe Resizable BAR requirement), pci_restore_vc_state() reloads VC arbitration tables with extended VCs active, pci_restore_pri_state() writes PCI_PRI_ALLOC_REQ while PRI is enabled, and MSI/MSI-X are masked and rewritten while BME is active.
[ ... ]
Quoted 13 lines
drivers/pci/cxl.c +13
-0+static int cxl_reset_hdm_restore (struct pci_dev *pdev, +struct cxl_hdm_info *snapshot,u16 *command) +{ +int rc; + +cxl_region_quiesce_lock (); + 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 (); + +return rc; +}
[ ... ]
drivers/pci/cxl.c static int __cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap, · line 745
Quoted 10 lines
drivers/pci/cxl.c +2
-0rc2 =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); +if (rc) +cxl_reset_save_disabled_state (pdev);pci_dev_reset_iommu_done (pdev);return rc;
[Severity: Medium]
Can clearing regs_offline and reset_pending before reset cleanup finishes let concurrent decoder operations access HDM MMIO prematurely?
In cxl_reset_hdm_restore(), regs_offline is cleared even when pci_cxl_restore_state() fails, allowing a concurrent cxl_decoder_reset() to write HDM registers on an unrestored device before cxl_reset_save_disabled_state() clears PCI_COMMAND_MEMORY.
Similarly, __cxl_reset_execute() calls cxl_reset_hdm_unmark() before cxl_reset_save_disabled_state(), pci_dev_reset_iommu_done(), cache policy and CXL.cache restoration, and cxl_reset_save_restored_state(). Because cxl_decoder_commit() synchronizes on cxl_rwsem.region rather than device_lock(&pdev->dev), it can pass cxl_hdm_cache_is_reset_pending() and program HDM registers while PCI_COMMAND_MEMORY is being cleared on error or while reset teardown is still incomplete.
[ ... ]
drivers/pci/cxl.c static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) · line 789
Quoted 8 lines
drivers/pci/cxl.c +8
-0+ keep_caching_off = attempted && rc; +if (cache.cache_disabled && !keep_caching_off) { + rc2 =cxl_reset_restore_cache_policy (pdev, dvsec, cache.saved_ctrl2); +if (!rc2 && attempted) + rc2 =cxl_reset_enable_cache (pdev, snapshot); +if (rc2) +cxl_reset_save_disabled_state (pdev); + }
[Severity: Medium]
Can cxl_reset_execute() race with concurrent cxl_port probe or unbind on PCI_DVSEC_CXL_CTRL and CXL_HDM_DECODER_CTRL_OFFSET?
cxl_port probe and unbind hold only device_lock(&port->dev), never device_lock(&pdev->dev) or cxl_rwsem.region, and kernel PCI config accesses bypass pci_cfg_access_lock(). That allows an interleaving such as:
CPU0 (cxl_reset_execute) CPU1 (cxl_port unbind)cxl_reset_hdm_restore()
cxl_restore_dvsec_ctrl()
write PCI_DVSEC_CXL_CTRL
(CACHE_ENABLE=0) cxl_region_quiesce_unlock()
clear_mem_enable()
cxl_set_mem_enable(cxlds, 0)
read PCI_DVSEC_CXL_CTRL (CACHE=0) write 0; cache dvsec_ctrl = 0
disable_hdm()
clear CXL_HDM_DECODER_ENABLEcxl_reset_enable_cache()
write snapshot->dvsec_ctrl <-- re-enables MEM_ENABLE with HDM off
This caches the transient CACHE_ENABLE=0 value into pdev->hdm->dvsec_ctrl and then re-enables PCI_DVSEC_CXL_MEM_ENABLE in hardware while CXL_HDM_DECODER_ENABLE is cleared. In addition, during cxl_clear_memory() and cxl_reset_wait_done(), cxl_set_mem_enable(), disable_hdm(), and devm_cxl_enable_hdm() do not check regs_offline or reset_pending and can issue PCI config and HDM MMIO accesses while the device is in reset.
drivers/pci/cxl.c static int cxl_reset_execute(struct pci_dev *pdev, int dvsec, u16 cap) · line 797
Quoted 9 lines
drivers/pci/cxl.c +7
-1cxl_hdm_quiesce_destroy (&cache.quiesce); -return rc ?: rc2; + +if (rc) +return rc; +if (rc2) +return rc2; + +return cxl_reset_save_restored_state (pdev, command);
[Severity: Medium]
Can post-restore failures leave CXL.mem or CXL.cache enabled in hardware after cxl_reset_save_disabled_state() runs?
cxl_reset_save_disabled_state() only clears PCI_COMMAND (MSE and BME), which gates CXL.io and PCIe DMA rather than CXL.mem or CXL.cache. If post-reset cxl_hdm_quiesce_flush() fails after cxl_reset_hdm_restore() succeeded, PCI_DVSEC_CXL_MEM_ENABLE and the committed HDM decoders remain active even though CPU cache invalidation failed and HPA reservations are released.
In addition, cxl_reset_execute() restores cache policy, re-enables PCI_DVSEC_CXL_CACHE_ENABLE, and calls cxl_hdm_quiesce_destroy() before cxl_reset_save_restored_state(). If cxl_reset_save_restored_state() fails, both CXL.mem and CXL.cache remain enabled in hardware.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
PATCH v18 16/19 PCI/CXL: Expose CXL Reset as a PCI reset method
In reply to Srirangan MadhavanRegister cxl_reset with the PCI reset-method interface and place it before FLR, which does not reset CXL.cache or CXL.mem protocol state.
Return -ENOTTY when device capabilities or function scope do not permit a reset, so that the next method is tried. Refuse with another error when cached HDM state or required cache synchronization cannot make the reset safe, since FLR would not reset the CXL state.
---
Changes in v18:
- Return -ENOTTY only when the device or its function scope does not permit CXL Reset. Refuse with another error when cached HDM state or cache synchronization cannot make it safe, so a refused reset no longer falls back to FLR, which would not reset the CXL state (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=13
--- drivers/pci/cxl.c | 10 ++++++++-- drivers/pci/pci.c | 1 + include/linux/pci.h | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-)
drivers/pci/cxl.c +8 -2
@@ -814,7 +814,7 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe) rc =cxl_pci_device_dvsec (pdev, &dvsec, &cap);if (rc) -return rc; +return probe ? -ENOTTY : rc; if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) || !(cap & PCI_DVSEC_CXL_MEM_CAPABLE) || !(cap & PCI_DVSEC_CXL_RST_CAPABLE) ||
@@ -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 ;
@@ -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 ; + } +return cxl_reset_execute (pdev, dvsec, cap); }
drivers/pci/pci.c +1 -0
@@ -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" },
include/linux/pci.h +1 -1
@@ -52,7 +52,7 @@ PCI_STATUS_PARITY)/* Number of reset methods used in pci_reset_fn_methods array in pci.c */ -#define PCI_NUM_RESET_METHODS 8 +#define PCI_NUM_RESET_METHODS 9#define PCI_RESET_PROBE true#define PCI_RESET_DO_RESET false
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 16/19] PCI/CXL: Expose CXL Reset as a PCI reset method
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…
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 <[email protected]>
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.
Quoted 4 lines
drivers/pci/cxl.c +0
-0
[ ... ]
drivers/pci/cxl.c int cxl_reset_function(struct pci_dev *pdev, bool probe) · line 829
Quoted 9 lines
drivers/pci/cxl.c +1
-1@@ -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.
drivers/pci/cxl.c int cxl_reset_function(struct pci_dev *pdev, bool probe) · line 838
Quoted 9 lines
drivers/pci/cxl.c +5
-0@@ -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.
drivers/pci/cxl.c int cxl_reset_function(struct pci_dev *pdev, bool probe) · line 841
Quoted 3 lines
drivers/pci/cxl.c +1
-0+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.
drivers/pci/pci.c const struct pci_reset_fn_method pci_reset_fn_methods[] = { · line 5210
Quoted 12 lines
drivers/pci/pci.c +1
-0@@ -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/[email protected]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
PATCH v18 17/19 PCI/CXL: Restore CXL state after CXL bus reset
In reply to Srirangan MadhavanCXL bus reset can clear HDM decoder programming and CXL Device DVSEC control state. Restore the cached state after a successful cxl_bus reset while IOMMU exclusion remains active.
Restore PCI configuration first so HDM MMIO is accessible, and preserve a disabled state if restoration fails.
Copy the cached state before the reset, then refresh its decoder settings from the cache under the CXL region quiesce once the reset completes. A region decommit during the reset updates the cache, and restoring the earlier decoders would leave the endpoint decoding an HPA range that the CXL core considers free. Keep global and DVSEC control from before the reset, since a refresh during the reset can read them as all-ones.
The prepare/restore interface and decoder-refresh structure follow Dave Jiang's cxl-type2-reset reference branch.
---
Changes in v18:
- Refresh the decoders from the cache in the shared restore window, so the CXL Reset path also restores the decoders as the CXL core last left them, now that a region can be torn down during the reset (Dave)
https://lore.kernel.org/linux-cxl/[email protected]/
- Turn CXL.cache back on in cxl_restore_command_after_pci_reset(), after the DVSEC Control is restored with it off, so the device does not cache again before the host caches are flushed (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=18
- Declare and initialize the __free() snapshot in cxl_reset_bus_function() in one statement (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=17
Reference branch: https://git.kernel.org/pub/scm/linux/kernel/git/djiang/linux.git/commit/?id=b93f0c8e67b198a63786fd3721b7177ab9df60be
--- drivers/cxl/core/hdm_state.c | 31 +++++++++++ drivers/pci/cxl.c | 104 +++++++++++++++++++++++++++++++++-- drivers/pci/pci.c | 14 +++++ drivers/pci/pci.h | 28 ++++++++++ include/cxl/hdm.h | 2 + 5 files changed, 175 insertions(+), 4 deletions(-)
drivers/cxl/core/hdm_state.c +31 -0
@@ -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 ; +} + void cxl_region_quiesce_lock(void) __acquires(cxl_region_quiesce) __context_unsafe(token maps to cxl_rwsem.region)
drivers/pci/cxl.c +100 -4
122 lines not on this page · raw message
drivers/pci/pci.c +14 -0
@@ -5062,6 +5062,7 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) {struct pci_dev *bridge;u16 dvsec, reg, val; +u16 command;int rc;/*
@@ -5092,6 +5093,11 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)if (rc)return -ENOTTY ; + struct cxl_hdm_info *snapshot __free(kfree) = +pci_cxl_reset_prepare (dev); +if (IS_ERR (snapshot)) +return PTR_ERR (snapshot); + rc =pci_dev_reset_iommu_prepare (dev);if (rc) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , rc);
@@ -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; }
drivers/pci/pci.h +28 -0
@@ -736,10 +736,18 @@ static inline void pci_doe_destroy(struct pci_dev *pdev) { }static inline void pci_doe_disconnected (struct pci_dev *pdev) { }#endif +struct cxl_hdm_info ;#ifdef CONFIG_CXL_RESET void pci_cxl_hdm_cache_init (struct pci_dev *pdev);void pci_cxl_hdm_cache_release (struct pci_dev *pdev);int cxl_reset_function (struct pci_dev *pdev,bool probe); +struct cxl_hdm_info *pci_cxl_reset_prepare (struct pci_dev *pdev); +int cxl_restore_state_after_pci_reset (struct pci_dev *pdev, +struct cxl_hdm_info *snapshot, +u16 *command); +int cxl_restore_command_after_pci_reset (struct pci_dev *pdev, +const struct cxl_hdm_info *snapshot, +u16 command);#else static inline void pci_cxl_hdm_cache_init (struct pci_dev *pdev) { }static inline void pci_cxl_hdm_cache_release (struct pci_dev *pdev) { }
@@ -747,6 +755,26 @@ static inline int cxl_reset_function(struct pci_dev *pdev, bool probe) {return -ENOTTY ; } + +static inline struct cxl_hdm_info *pci_cxl_reset_prepare (struct pci_dev *pdev) +{ +return NULL ; +} + +static inline int +cxl_restore_state_after_pci_reset (struct pci_dev *pdev, +struct cxl_hdm_info *snapshot,u16 *command) +{ +return 0 ; +} + +static inline int +cxl_restore_command_after_pci_reset (struct pci_dev *pdev, +const struct cxl_hdm_info *snapshot, +u16 command) +{ +return 0 ; +}#endif #ifdef CONFIG_PCI_NPEM
include/cxl/hdm.h +2 -0
@@ -75,6 +75,8 @@ 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); +int cxl_hdm_cache_copy_decoders (struct cxl_hdm_info *const *slot, +struct cxl_hdm_info *dst);/* * Region quiesce blocks CXL region commit and teardown, and with them decoder
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 17/19] PCI/CXL: Restore CXL state after CXL bus reset
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…
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 <[email protected]>
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.
Quoted 28 lines
drivers/cxl/core/hdm_state.c +20
-0@@ -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);
drivers/cxl/core/hdm_state.c struct cxl_hdm_info *cxl_hdm_cache_snapshot(struct cxl_hdm_info *const *slot) · line 316
Quoted 10 lines
drivers/cxl/core/hdm_state.c +10
-0+ 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[]?
Quoted 33 lines
drivers/pci/cxl.c +11
-4@@ -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 ();
[ ... ]
drivers/pci/cxl.c static void cxl_reset_hdm_unmark(struct pci_dev *pdev) · line 590
Quoted 13 lines
drivers/pci/cxl.c +13
-0+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 MMIOcxl_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?
Quoted 7 lines
drivers/pci/cxl.c +7
-0+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);
drivers/pci/cxl.c static void cxl_reset_hdm_unmark(struct pci_dev *pdev) · line 618
Quoted 10 lines
drivers/pci/cxl.c +10
-0+ 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.
Quoted 4 lines
drivers/pci/pci.c +0
-0
[ ... ]
Quoted 5 lines
drivers/pci/pci.c +1
-0@@ -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)
drivers/pci/pci.c static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) · line 5131
Quoted 10 lines
drivers/pci/pci.c +7
-0+ 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?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
PATCH v18 18/19 PCI/CXL: Flush host and device caches for CXL bus reset
In reply to Srirangan MadhavanCXL bus reset can reset a Type 2 device while its host and device caches still contain data for the device's HDM ranges. Flush the host caches, disable device caching, and trigger device cache writeback and invalidation when supported before reset. Invalidate the host caches again before ending IOMMU exclusion.
Hold the CXL region lock only to take the snapshot and reserve the affected HPA ranges, to mark the HDM registers offline right before the reset, to restore the state after the reset, and to clear the marks once the second invalidation is done. Do the host cache flushes and the bus reset without the lock. The reserved ranges and the pending mark keep new decode from coming online in between, and a region torn down during the reset does not access the device's registers. Preserve the device's cache policy across the operation.
If reset was not attempted, undo preparation without changing the saved PCI command unless cleanup fails. After an attempted reset, keep the device disabled on recovery failure and do not re-enable caching against unrestored HDM state. Enable bus mastering only after the IOMMU is restored, so that no DMA request reaches the blocking domain.
If no cached HDM state exists, retain the existing CXL bus reset path. For a device with cached state, reject the reset if its cache-maintenance state cannot be safely prepared or restored. An invalidation provider is required unless the existing test-only CXL_REGION_INVALIDATION_TEST bypass is enabled.
---
Changes in v18:
- Do not hold the region lock across the bus reset. Share the prepare, offline, restore and unmark windows of the CXL Reset path, and do the host cache flushes and the bus reset without the lock (Dave)
https://lore.kernel.org/linux-cxl/[email protected]/
- Check that a slot or parent bus reset can run before touching the device, and that the SBR unmask took effect before resetting, so a bus reset that cannot run no longer leaves the device disabled with caching off. Any result from the bus reset itself still counts as an attempted reset (Sashiko)
https://sashiko.dev/#/patchset/[email protected]?part=18
--- drivers/pci/cxl.c | 164 ++++++++++++++++++++++++++++++++++++---------- drivers/pci/pci.c | 35 ++++++++-- drivers/pci/pci.h | 20 ++++-- 3 files changed, 173 insertions(+), 46 deletions(-)
drivers/pci/cxl.c +128 -36
@@ -415,6 +415,11 @@ static int pci_cxl_restore_state(struct pci_dev *pdev,return cxl_restore_state (pdev, snapshot); } +static int cxl_reset_restore_cache_policy (struct pci_dev *pdev,int dvsec, +u16 saved_ctrl2); +static int cxl_reset_cache_prepare (struct pci_dev *pdev,int dvsec,u16 cap, +struct cxl_pci_cache *cache); +/* * 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
@@ -430,10 +435,12 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,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); +if (cap &PCI_DVSEC_CXL_CACHE_CAPABLE ) { + 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);
@@ -443,6 +450,10 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev,cxl_region_quiesce_unlock ();return snapshot; } +if (!snapshot->dvsec_ctrl_valid) { + rc = -ENXIO ; + goto free_snapshot; + }/* Judge the snapshot: the live cache can change after it was taken. */ if (cxl_hdm_snapshot_is_dvsec_decoded (snapshot)) { rc = -EOPNOTSUPP ;
@@ -479,14 +490,13 @@ static struct cxl_hdm_info *cxl_reset_hdm_prepare(struct pci_dev *pdev, }/* -* 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. +* Restore window. Take the region lock again to bring the cached decoders up +* to date and 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(). * -* 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. +* A region torn down during the reset has already refreshed the cache, so the +* decoders are re-read here instead of restoring the snapshot's stale copy. */ static int cxl_reset_hdm_restore (struct pci_dev *pdev,struct cxl_hdm_info *snapshot,u16 *command)
@@ -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);
@@ -509,7 +521,7 @@ static int cxl_reset_hdm_restore(struct pci_dev *pdev,* 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) +void cxl_reset_regs_offline (struct pci_dev *pdev) {cxl_region_quiesce_lock ();cxl_hdm_cache_set_regs_offline (pdev, true);
@@ -529,63 +541,143 @@ static void cxl_reset_hdm_unmark(struct pci_dev *pdev) }/** -* pci_cxl_reset_prepare() - allocate HDM state storage ahead of a bus reset +* pci_cxl_reset_prepare() - prepare HDM state and caches before a bus reset * @pdev: device about to be reset +* @cachep: receives the cache state to pass to +* cxl_restore_state_after_pci_reset() * -* Copy the cached HDM state before the reset, so that restoring cannot fail -* for lack of memory. The decoder settings are refreshed when the state is -* restored. +* Capture HDM state and reserve its ranges under the region lock, then drop +* the lock and complete host and device cache maintenance. No region lock is +* held across the reset: the device's decoders cannot commit until +* cxl_restore_state_after_pci_reset() clears the pending mark. * -* Return: storage for cxl_restore_state_after_pci_reset(), to be freed with -* kfree(); NULL when @pdev has no cached HDM state; or an ERR_PTR() on -* failure. +* Return: snapshot to be freed with kfree(), NULL when no cached HDM state is +* present, or an ERR_PTR() on failure. A cached device without a valid DVSEC +* snapshot is rejected because its cache-maintenance scope is unknown. +* Only a successful non-NULL snapshot returns with *@cachep set. The caller +* must pass both to cxl_restore_state_after_pci_reset() even if reset is never +* attempted. The snapshot remains caller-owned. All other returns leave +* *@cachep NULL and nothing reserved or marked. */ -struct cxl_hdm_info *pci_cxl_reset_prepare (struct pci_dev *pdev) +struct cxl_hdm_info *pci_cxl_reset_prepare (struct pci_dev *pdev, +struct cxl_pci_cache **cachep) {struct cxl_hdm_info *snapshot; +struct cxl_pci_cache *cache; +u16 dvsec, cap, command; +int rc;device_lock_assert (&pdev->dev); - snapshot =cxl_hdm_cache_snapshot (&pdev->hdm); -if (IS_ERR (snapshot)) -return PTR_ERR (snapshot) == -ENXIO ?NULL : snapshot; + *cachep =NULL ; +if (!pdev->hdm) +return NULL ; + rc =cxl_pci_device_dvsec (pdev, &dvsec, &cap); +if (rc == -ENOTTY ) +return ERR_PTR (-ENODEV ); +if (rc) +return ERR_PTR (rc);/* Raw reset callers may not have saved the PCI state needed for restore. */ if (!pdev->state_saved) {pci_err (pdev,"CXL bus reset requires saved PCI state\n" ); -kfree (snapshot);return ERR_PTR (-EINVAL ); } + cache =kzalloc_obj (*cache); +if (!cache) +return ERR_PTR (-ENOMEM ); + + snapshot =cxl_reset_hdm_prepare (pdev, dvsec, cap, cache); +if (IS_ERR (snapshot)) { +kfree (cache); +return snapshot; + } + + rc =cxl_reset_cache_prepare (pdev, dvsec, cap, cache); +if (!rc && !pci_wait_for_pending_transaction (pdev)) + rc = -ETIMEDOUT ; +if (rc) { + rc =cxl_restore_state_after_pci_reset (pdev, snapshot, cache, false, + rc, &command); +kfree (snapshot); +return ERR_PTR (rc); + } + + *cachep = cache;return snapshot; }/** -* cxl_restore_state_after_pci_reset() - restore CXL state after a bus reset +* cxl_restore_state_after_pci_reset() - finish a prepared reset and release its quiesce * @pdev: device that was reset * @snapshot: storage from pci_cxl_reset_prepare() +* @cache: quiesce state from pci_cxl_reset_prepare() +* @reset_attempted: reset helper was invoked and may have affected hardware +* @rc: preparation or PCI reset result * @command: returns the saved PCI_COMMAND value to apply once the IOMMU is * restored * -* Under the region quiesce, refresh the decoder settings in @snapshot from the -* cache and restore: first the PCI configuration needed to reach HDM MMIO, -* then the HDM decoder and CXL Device DVSEC state. Leave @pdev disabled on -* failure. +* Restore state through pci_cxl_restore_state(), complete host invalidation, +* and restore the cache policy changed by planned-reset preparation. When +* @reset_attempted is false, undo preparation without changing the saved PCI +* command, unless cleanup itself fails. After an attempted reset, keep caching +* and the device disabled if state restoration or host invalidation fails. +* A reset-helper error does not prove that hardware was untouched. +* Unplanned recovery uses pci_cxl_restore_state() directly and owns its own +* quiesce and cache-maintenance sequencing. * * Bus mastering stays off, because the caller has not yet restored IOMMU * translation and a DMA request now would hit the blocking domain. The caller * calls cxl_restore_command_after_pci_reset() once the IOMMU is restored. +* +* The region lock is taken only to restore the HDM state and, after the host +* cache invalidation, to clear the pending mark that blocks decoder commits. +* Complete host cache invalidation before PCI operation resumes, then release +* the reserved ranges and free @cache on every exit. @snapshot remains +* caller-owned. */ int cxl_restore_state_after_pci_reset (struct pci_dev *pdev,struct cxl_hdm_info *snapshot, -u16 *command) +struct cxl_pci_cache *cache, +bool reset_attempted,int rc,u16 *command) { -int rc; +int rc2 =0 , policy_rc =0 ; + *command =0 ; +if (!cache) +return rc;device_lock_assert (&pdev->dev); - rc =cxl_reset_hdm_restore (pdev, snapshot, command); -if (rc) -cxl_reset_save_disabled_state (pdev); +/* The restore result matters only after an attempted, uncorrupted reset. */ +if (reset_attempted && !rc) + rc =cxl_reset_hdm_restore (pdev, snapshot, command); + +/* 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); + } +cxl_reset_hdm_unmark (pdev); +/* 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); +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; + } + +cxl_hdm_quiesce_destroy (&cache->quiesce); +kfree (cache);return rc; }
@@ -728,7 +820,7 @@ static int cxl_reset_cache_prepare(struct pci_dev *pdev, int dvsec, u16 cap,int rc; rc =cxl_hdm_quiesce_flush (&cache->quiesce); -if (rc) +if (rc || !(cap &PCI_DVSEC_CXL_CACHE_CAPABLE ))return rc;/* Track an attempted change so partial failures still restore policy. */
drivers/pci/pci.c +30 -5
@@ -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;/*
@@ -5089,19 +5091,30 @@ static int cxl_reset_bus_function(struct pci_dev *dev, bool probe)if (probe)return 0 ; +/* +* Check that a bus reset can run before touching device state, so that +* a later -ENOTTY cannot mean "nothing was reset". +*/ + rc =pci_dev_reset_slot_function (dev,PCI_RESET_PROBE ); +if (rc == -ENOTTY ) + rc =pci_parent_bus_reset (dev,PCI_RESET_PROBE ); +if (rc) +return rc; + rc =pci_read_config_word (bridge, dvsec +PCI_DVSEC_CXL_PORT_CTL , ®);if (rc)return -ENOTTY ; struct cxl_hdm_info *snapshot __free(kfree) = -pci_cxl_reset_prepare (dev); +pci_cxl_reset_prepare (dev, &cache);if (IS_ERR (snapshot))return PTR_ERR (snapshot); rc =pci_dev_reset_iommu_prepare (dev);if (rc) {pci_err (dev,"failed to stop IOMMU for a PCI reset: %d\n" , rc); -return rc; +return cxl_restore_state_after_pci_reset (dev, snapshot, cache, + false, rc, &command); }if (reg &PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR ) {
@@ -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; + }if (reg != val)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); + rc =cxl_restore_state_after_pci_reset (dev, snapshot, cache, attempted, + rc, &command);pci_dev_reset_iommu_done (dev);
drivers/pci/pci.h +15 -5
@@ -737,14 +737,18 @@ static inline void pci_doe_disconnected(struct pci_dev *pdev) { }#endif struct cxl_hdm_info ; +struct cxl_pci_cache ;#ifdef CONFIG_CXL_RESET void pci_cxl_hdm_cache_init (struct pci_dev *pdev);void pci_cxl_hdm_cache_release (struct pci_dev *pdev);int cxl_reset_function (struct pci_dev *pdev,bool probe); -struct cxl_hdm_info *pci_cxl_reset_prepare (struct pci_dev *pdev); +struct cxl_hdm_info *pci_cxl_reset_prepare (struct pci_dev *pdev, +struct cxl_pci_cache **cachep); +void cxl_reset_regs_offline (struct pci_dev *pdev);int cxl_restore_state_after_pci_reset (struct pci_dev *pdev,struct cxl_hdm_info *snapshot, -u16 *command); +struct cxl_pci_cache *cache, +bool reset_attempted,int rc,u16 *command);int cxl_restore_command_after_pci_reset (struct pci_dev *pdev,const struct cxl_hdm_info *snapshot,u16 command);
@@ -756,16 +760,22 @@ static inline int cxl_reset_function(struct pci_dev *pdev, bool probe)return -ENOTTY ; } -static inline struct cxl_hdm_info *pci_cxl_reset_prepare (struct pci_dev *pdev) +static inline struct cxl_hdm_info * +pci_cxl_reset_prepare (struct pci_dev *pdev,struct cxl_pci_cache **cachep) { + *cachep =NULL ;return NULL ; } +static inline void cxl_reset_regs_offline (struct pci_dev *pdev) { } +static inline int cxl_restore_state_after_pci_reset (struct pci_dev *pdev, -struct cxl_hdm_info *snapshot,u16 *command) +struct cxl_hdm_info *snapshot, +struct cxl_pci_cache *cache, +bool reset_attempted,int rc,u16 *command) { -return 0 ; +return rc; } static inline int
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 18/19] PCI/CXL: Flush host and device caches for CXL bus reset
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…
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 <[email protected]>
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.
Quoted 4 lines
drivers/pci/cxl.c +0
-0
[ ... ]
drivers/pci/cxl.c static int cxl_reset_hdm_restore(struct pci_dev *pdev, · line 513
Quoted 11 lines
drivers/pci/cxl.c +3
-1@@ -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 = 0pci_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
SBRcxl_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.
[ ... ]
drivers/pci/cxl.c static void cxl_reset_hdm_unmark(struct pci_dev *pdev) · line 662
Quoted 8 lines
drivers/pci/cxl.c +8
-0+/* 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.
drivers/pci/cxl.c static void cxl_reset_hdm_unmark(struct pci_dev *pdev) · line 663
Quoted 1 line
drivers/pci/cxl.c +1
-0+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.
drivers/pci/cxl.c static void cxl_reset_hdm_unmark(struct pci_dev *pdev) · line 667
Quoted 4 lines
drivers/pci/cxl.c +4
-0+/* 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.
drivers/pci/cxl.c static void cxl_reset_hdm_unmark(struct pci_dev *pdev) · line 677
Quoted 10 lines
drivers/pci/cxl.c +10
-0+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.
[ ... ]
Quoted 5 lines
drivers/pci/pci.c +0
-0
drivers/pci/pci.c static int pci_reset_bus_function(struct pci_dev *dev, bool probe) · line 5068
Quoted 9 lines
drivers/pci/pci.c +2
-0@@ -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.
[ ... ]
Quoted 8 lines
drivers/pci/pci.c +3
-1@@ -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 ;
drivers/pci/pci.c static int cxl_reset_bus_function(struct pci_dev *dev, bool probe) · line 5140
Quoted 10 lines
drivers/pci/pci.c +10
-0+ }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 = 0pci_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.regionprepare fails / cxl_sbr_masked()cxl_restore_state_after_pci_reset()
!reset_attempted -> skip restorepci_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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
PATCH v18 19/19 cxl: Add KUnit checks for HDM decoder unpacking
In reply to Srirangan MadhavanTest cxl_hdm_unpack_decoder() without a PCI device or HDM MMIO. Cover range boundaries and overflow, an all-ones base, zero-size and uncommitted decoders, and a locked host-only decoder with 12-way interleave.
Run the checks as a KUnit suite rather than during cxl_test module loading, so a failure does not prevent unrelated CXL mock tests from running. An uncommitted decoder has no meaningful target type, so do not check it.
---
Changes in v18:
- Rebase onto the KUnit support in the base tree: drop the Kconfig and Makefile pieces it now provides, and allow the KUnit module to use the cxl_hdm_unpack_decoder() export
--- drivers/cxl/core/hdm_regs.c | 2 +- drivers/cxl/tests/Makefile | 3 + drivers/cxl/tests/hdm_unpack_kunit.c | 91 ++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 drivers/cxl/tests/hdm_unpack_kunit.c
drivers/cxl/core/hdm_regs.c +1 -1
@@ -188,7 +188,7 @@ int cxl_hdm_unpack_decoder(struct cxl_decoder_config *config, int id,return 0 ; } -EXPORT_SYMBOL_FOR_MODULES (cxl_hdm_unpack_decoder,"cxl_core" ); +EXPORT_SYMBOL_FOR_MODULES (cxl_hdm_unpack_decoder,"cxl_core,cxl_hdm_unpack_kunit" );int cxl_hdm_read_decoder (struct cxl_decoder_config *config,void __iomem *hdm,int id,u32 ctrl)
drivers/cxl/tests/Makefile +3 -0
@@ -3,3 +3,6 @@ ccflags-y += -I$(srctree)/drivers/cxl obj-$(CONFIG_CXL_KUNIT_TEST) += cxl_hdm_encoding_kunit.o cxl_hdm_encoding_kunit-y := hdm_encoding_kunit.o + +obj-$(CONFIG_CXL_KUNIT_TEST) += cxl_hdm_unpack_kunit.o +cxl_hdm_unpack_kunit-y := hdm_unpack_kunit.o
drivers/cxl/tests/hdm_unpack_kunit.c new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v18 19/19] cxl: Add KUnit checks for HDM decoder unpacking
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Low] cxl: KUnit test violates CXL placement, module, and suite naming rules - [Low] cxl: unconditional…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Low] cxl: KUnit test violates CXL placement, module, and suite naming rules
- [Low] cxl: unconditional non-KUnit export and unused cxl_core allowlist entry
--
commit 898a6ebae0ecbe24f0a8fe116f43eeee40e4d537
Author: Srirangan Madhavan <[email protected]>
cxl: Add KUnit checks for HDM decoder unpacking
This commit adds a KUnit test suite for cxl_hdm_unpack_decoder() and extends its module export list so the new test module can call it.
drivers/cxl/core/hdm_regs.c int cxl_hdm_unpack_decoder(struct cxl_decoder_config *config, int id, · line 191
Quoted 10 lines
drivers/cxl/core/hdm_regs.c +1
-1@@ -188,7 +188,7 @@ int cxl_hdm_unpack_decoder(struct cxl_decoder_config *config, int id,return 0 ; } -EXPORT_SYMBOL_FOR_MODULES (cxl_hdm_unpack_decoder,"cxl_core" ); +EXPORT_SYMBOL_FOR_MODULES (cxl_hdm_unpack_decoder,"cxl_core,cxl_hdm_unpack_kunit" );
[Severity: Low]
Does cxl_hdm_unpack_decoder() still need an unconditional export or cxl_core in its allowlist?
Outside drivers/cxl/core/hdm_regs.c, only this KUnit test calls cxl_hdm_unpack_decoder(); cxl_core calls cxl_hdm_read_decoder() instead. This keeps a test-only helper exported in vmlinux even when KUnit is disabled.
Using VISIBLE_IF_KUNIT and EXPORT_SYMBOL_IF_KUNIT() would restrict the export to KUnit builds.
[ ... ]
Quoted 14 lines
drivers/cxl/tests/hdm_unpack_kunit.c new file +8
-0@@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2026 NVIDIA Corporation & Affiliates */ +#include <kunit/test.h> +#include <linux/bitfield.h> +#include <linux/sizes.h> + +#include "cxl.h" +#include "../core/core.h"
[ ... ]
drivers/cxl/tests/hdm_unpack_kunit.c line 86
Quoted 4 lines
drivers/cxl/tests/hdm_unpack_kunit.c new file +4
-0+static struct kunit_suite cxl_hdm_test_suite = { + .name ="cxl-hdm-unpack" , + .test_cases = cxl_hdm_test_cases, +};
[Severity: Low]
Should this suite live in drivers/cxl/core/tests/ and use underscores in its name?
Because cxl_hdm_test_suite sets .name to "cxl-hdm-unpack" with hyphens instead of underscores, running KUnit with a cxl_* filter glob silently skips it while running the sibling CXL suites (cxl_hdm_encoding and cxl_core_mbox).
Documentation/driver-api/cxl/testing.rst also expects tests beside the code under test with path-derived underscore names, matching drivers/cxl/core/tests/mbox_kunit.c.
Moving this file to drivers/cxl/core/tests/hdm_unpack_kunit.c with module cxl_core_hdm_unpack_kunit and suite cxl_core_hdm_unpack would follow that convention and avoid including "../core/core.h".
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
[email protected]
AI reviewSashiko has finished reviewing this 19-patch series:
Sashiko has finished reviewing this 19-patch series:
- Patches 1-4, 6, 8, and 10: no issues found.
- Patch 5 ("cxl: Introduce endpoint HDM decoder settings"): 1 potential issue (1 Low)
- Patch 7 ("cxl: Share HDM decoder register unpacking"): 2 potential issues (2 Low)
- Patch 9 ("cxl: Refresh cached PCI HDM decoder settings"): 4 potential issues (3 Medium, 1 Low)
- Patch 11 ("PCI/CXL: Cache endpoint HDM state during PCI enumera..."):
7 potential issues (3 High, 4 Medium)
- Patch 12 ("PCI/CXL: Add CXL Device Reset sequencing"): 4 potential issues (4 Medium)
- Patch 13 ("cxl: Validate and synchronize HDM ranges around reset"): 9 potential issues (2 High, 6 Medium, 1 Low)
- Patch 14 ("PCI/CXL: Reject reset with unsafe function scope"): 7 potential issues (1 High, 3 Medium, 3 Low)
- Patch 15 ("PCI/CXL: Restore CXL state after PCI reset"): 7 potential issues (7 Medium)
- Patch 16 ("PCI/CXL: Expose CXL Reset as a PCI reset method"): 11 potential issues (5 High, 6 Medium)
- Patch 17 ("PCI/CXL: Restore CXL state after CXL bus reset"): 4 potential issues (1 High, 3 Medium)
- Patch 18 ("PCI/CXL: Flush host and device caches for CXL bus reset"):
7 potential issues (2 High, 5 Medium)
- Patch 19 ("cxl: Add KUnit checks for HDM decoder unpacking"): 2 potential issues (2 Low)
Detailed reviews have been sent as separate replies to patches 5, 7, 9, and 11-19.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]