vfio/pci: Add CXL Type-2 device passthrough support
Subsystems (8)
v5 cover vfio/pci: Add CXL Type-2 device passthrough support
From: Manish Honap <[email protected]>
This series adds VFIO passthrough for CXL Type-2 accelerators. The guest drives its own virtual HDM decoder and can reset the device. The host owns the physical decoder and the host physical address the memory lands at. The guest only picks a guest physical address. This series targets a single, non-interleaved, firmware-committed endpoint decoder.
Base and dependencies
---------------------
Base: Linux 7.3-rc1 (cee9395acd80), plus the cxl-reset dependency:
- CXL reset core from Srirangan's cxl_reset series (v12) [1], which caches the endpoint decoder settings in pdev->hdm and keeps the HDM decoder and reset helpers in drivers/cxl/core.
- This series adds a function-scoped reset entry (cxl_reset_dvsec_sequence) on top and drives it at the vfio reset points.
- Prerequisite commits from Alejandro's type-2 device support and Dan's devm_cxl_probe_mem are already upstreamed in this kernel release and those series are not listed as dependencies now.
Changes since v4
----------------
v4 [2] introduced the vfio-cxl provider: the register emulation lives in a separate module that vfio-pci-core loads on demand, and cxl-core keeps only the reset entry and a few enabling helpers. v5 keeps that model and folds the v4 review, reshaping the region and reset handling. The patch count is unchanged at 27; several patches were split, a few were dropped, and the composition changed.
Structural
- The v4 "expose the HDM memory and trap the decoder registers" patch is split into three: the mmap-able HDM memory region, memory-failure containment for that struct-page-less range, and the read-only decoder-register region.
- "Guard HDM access on device state" is split into two: excluding the decoder block from the direct BAR, and clearing the HDM access gate after a secondary bus reset.
- The decoder register block is read live; the static decoder snapshot and its refresh-after-reset patch are gone.
- The CXL DVSEC is virtualized through the config-space permission hooks in vfio_pci_config.c; the separate DVSEC-shadow patch is gone.
- The cxl-core media-readiness patch is dropped; a mailbox-less Type-2 memdev sets media ready in the provider at bind (mirrors efx_cxl).
- The cxl-core register-map rework is split into request/ioremap helpers plus an owned-resource record, replacing the v4 bar_owned flag.
- The v4 resource.c include/export patch is dropped; the include ships in the cxl_reset base and the disputed re-export no longer exists.
Behavioral
- The HDM decoder registers are served by live reads with guest writes absorbed; the host committed and locked the physical decoder, so the guest never drives it.
- fd read/write of the HDM memory region is serviced by a device-state guarded write-back copy (Memory-Space enabled, decoder known-good, and media ready) and refused only while that gate is closed, rather than unconditionally.
- The coherent CXL.mem range is exported as a dma-buf and mapped into the guest IOAS with IOMMU_IOAS_MAP_FILE, replacing the out-of-tree PFNMAP workaround v4 needed for device-side ATS.
- Reset is wired through the pci_error_handlers .reset_prepare and .reset_done callbacks and the CXL reset method rather than open-coded; a bus hot reset no longer rejects sibling functions.
- The excluded-range facility is generic (a list of bar/start/size/flags) and the existing MSI-X exclusion is migrated onto it.
Reviewer feedback addressed
---------------------------
The v4 [2] thread has the full discussion; this is where each objection landed.
Alex Williamson
- A generic excluded-range list replaces the CXL one-off; MSI-X is migrated onto it; an intersecting host read fills -1 and a write is dropped rather than erroring.
- The component-register block is exposed as a live read-only view, not a static shadow; the refresh-after-reset patch is eliminated.
- The HDM memory fd read/write is device-state guarded rather than a bare
-EIO.
- Error containment and the decoder-register region are their own patches.
- Reset uses .reset_prepare/.reset_done; the multifunction/sibling hot-reset rejection is dropped; the VM-centric framing is removed.
- CXL init failure is non-fatal and falls back to plain vfio-pci; disable_cxl is a convenience opt-out, not the failure path.
- The DVSEC virtualization lives in vfio_pci_config.c as an ecap perm map; the config path takes no CXL module dependency.
- Both regions use VFIO_REGION_TYPE_PCI_VENDOR_TYPE with the CXL vendor id.
- Kconfig/Makefile ordering fixed; the .open_device/.close_device hook names are kept to mirror vfio_device_ops; the bind-time rejects log consistently.
Dave Jiang
- Component-register ownership uses the owned-resource API, not a bar_owned flag.
- Media readiness is set in the provider at bind (efx_cxl precedent); the cxl-core media-readiness change is dropped.
- The component register defines live in uapi/cxl/cxl_regs.h and the message names the selftest as the consumer.
- The cheap topology rejects are grouped before the range computation.
Jonathan Cameron
- The v4 resource.c include/export patch is gone: the include arrives with the cxl_reset base and the re-export it objected to no longer exists.
Shuai Xue
- The selftest maps the HDM range through the dma-buf + IOMMU_IOAS_MAP_FILE path instead of failing a VA-based IOAS map.
- The guest cache write-back-invalidate doorbell completes in the shadow; the host runs the real WBI inside the reset sequence.
Patch order
-----------
The patches are ordered so the tree builds at every commit and each change sits next to the code it depends on. Five groups:
Part 1 CXL core (patches 1-4)
- Split the BAR block request and ioremap helpers, and let a BAR-owning driver own the component register block, so the HDM/RAS sub-blocks are left unclaimed for vfio-cxl.
- Move the component register defines to include/uapi/cxl/cxl_regs.h so a VMM (and the selftest) can consume them.
- Add the function-scoped cxl_reset_dvsec_sequence() for vfio-pci.
Part 2 vfio-pci-core enabling (patches 5-14)
- The CXL provider ops registration interface, on-demand provider load, -EPROBE_DEFER handling for the built-in case, and the non-fatal fallback to plain vfio-pci when CXL init fails.
- A generic excluded-range list and the MSI-X migration onto it.
- The CXL DVSEC virtualization in config space, the open/close hooks, the reset brackets, and the disable_cxl opt-out.
Part 3 vfio-cxl provider and HDM regions (patches 15-24)
- The vfio-cxl module, the CXL memdev created at bind, and ownership of the whole component BAR.
- The mmap-able HDM memory region, memory-failure containment for it, and the read-only decoder-register region.
- Excluding the decoder block from the direct BAR, the hot-reset access gate, the device/decoder geometry cap, and the dma-buf export.
Part 4 vfio-cxl reset (patch 25)
- Run the CXL DVSEC reset at every vfio reset point.
Part 5 Documentation and selftests (patches 26-27)
Subsystem boundary
------------------
vfio-pci-core does not implement CXL registers. vfio-cxl is a separate module that registers a struct vfio_cxl_ops at init. vfio-pci-core loads it on demand for a CXL device (request_module plus pcie_is_cxl) and pins it per bound device. If a modular provider is missing or fails to load, the device is driven as plain vfio-pci. The bind only defers for the built-in initcall-order case, where request_module cannot help.
The core exposes only the primitives that have to live in core (memory_lock, mapping revoke, dma-buf quiesce, and the BAR sub-range exclusion). The CXL-specific work stays behind the ops.
Memory ownership
----------------
The host resolves the host physical address once, at bind, through devm_cxl_probe_mem(). The memdev is owned for the bind lifetime and torn down at unbind. The HPA range is claimed IORESOURCE_EXCLUSIVE so no mismatched cacheable alias can form, including one mapped through /dev/mem.
The guest programs a guest physical address into a trapped virtual decoder and polls a shadow for commit. It never reaches the physical decoder
registers: those are served only through the live-read trap and are excluded from the direct BAR mapping, so the guest cannot move the host physical window.
HDM region access
-----------------
The HDM region carries the coherent device memory. A VMM mmaps it and maps it into the guest through stage-2; that is the primary access path. The region advertises READ and WRITE so a VMM can derive an accessible (non PROT_NONE) mmap protection. fd read/write is serviced by a write-back copy gated on the same device state as the fault path, and refused while that gate is closed.
The fault path inserts the pfn only while the decoder is in a known-good restored state, the device has PCI Memory-Space enabled, and the media is ready, so a host access cannot reach a revoked or disabled decoder. The fault inserts the HPA pfn at the largest aligned order, including 2 MB PMDs. The struct-page-less range is registered with the memory-failure machinery so a memory error is contained to unmapping the range and a SIGBUS rather than a host SError.
DMA and iommufd
---------------
A Type-2 accelerator issues ATS-translated DMA to addresses inside its own HDM window, so that range must be present in the guest IOAS. The HDM range is struct-page-less coherent memory that a userspace-VA IOMMU_IOAS_MAP cannot pin, so the HDM memory region is exportable as a dma-buf:VFIO_DEVICE_FEATURE_DMA_BUF returns an fd that iommufd maps with IOMMU_IOAS_MAP_FILE, without a VA or a page pin. The dma-buf is revoked whenever the mapping is torn down, so a stale stage-2 mapping cannot outlive the HDM window.
Reset
-----
A CXL Type-2 device is reset through its DVSEC sequence at every path that can reset it, not through an FLR (an FLR would corrupt CXL.mem). The reset runs at every vfio reset point through one shared helper: VM enable and close, the VFIO_DEVICE_RESET ioctl, a config-space FLR, and a guest write of Initiate_CXL_Reset in the CXL DVSEC. Under memory_lock, with the HDM mapping and the dma-buf revoked, the host runs cxl_reset_dvsec_sequence(), which always clears device memory on the v12 base and restores and re-samples the firmware-committed decoder. The guest's cache write-back-invalidate doorbell completes in the shadow and the real invalidate runs inside that sequence; the reset outcome comes back through DVSEC STATUS2 for the guest to poll.
A CXL port masks Secondary Bus Reset by default, so a VFIO_DEVICE_PCI_HOT_RESET does not reach the endpoint and the HDM state is untouched; a hot reset no longer rejects sibling functions. If the port has SBR unmasked the reset can decommit the decoder without restoring it, so the reset_done handler gates HDM access; a later VFIO_DEVICE_RESET runs the CXL reset sequence and restores it.
A bound CXL Type-2 device is kept out of idle D3. Powering a Type-2 function down and back up reinitializes it and discards its coherent memory, which only the accelerator's own driver re-initializes, so vfio-cxl sets disable_idle_d3 to keep the device in D0 while it is bound.
Validation
----------
- Each patch builds (drivers/cxl and drivers/vfio) and passes scripts/checkpatch.pl --codespell --strict with no errors, warnings, or checks.
- The series applies in sequence on the stated base and dependencies.
- The coherent CXL.mem range is mapped into the guest IOAS through the in-series dma-buf export (IOMMU_IOAS_MAP_FILE); the out-of-tree PFNMAP workaround v4 required for device-side ATS is no longer needed.
- A selftest, tools/testing/selftests/vfio/vfio_cxl_type2_test.c, exercises region discovery, the sparse mmap, the huge-page fault, the dma-buf IOAS map, the live decoder reads, aligned/range access rejects, the commit/lock FSM, the DVSEC virtualization, and a real device reset.
- Built and functionally tested against a CXL Type-2 device: guest boot, decoder commit and mapping, and guest-triggered CXL reset.
Follow-on UAPI
--------------
- The trapped component-register region spans the whole HDM decoder block, so a multi-decoder device needs no new region or cap: a VMM reads the decoder count and each committed base from every decoder in the block.
- The COMP_REGS geometry cap keeps a reserved field as a versioning anchor.
- Further trapped surfaces such as CXL RAS are planned as new CXL region subtypes rather than by extending this cap.
- The single committed, non-interleaved decoder is a bind-time policy in one place, not an ABI assumption, so multi-decoder and switched topologies relax only there.
Deferred
--------
- Topology reach. Switched, multi-decoder, and interleaved decoders stay rejected at bind.
- Non firmware-committed decoder support.
AI assistance disclosure
------------------------
This series was developed with substantial AI assistance (Anthropic Claude, via the Claude Code CLI), used for design exploration and trade-off analysis, multi-agent regression and adversarial review against the v4 feedback, and drafting the selftests and the documentation. Following Documentation/process/coding-assistants.rst, each patch carries an
testing were done by me, not the assistant. I have reviewed every change and take full DCO responsibility for the series.
References
----------
[1] [PATCH v12 00/12] PCI/CXL: Add CXL reset support for Type 2 devices
https://lore.kernel.org/linux-cxl/[email protected]/
[2] [PATCH v4 00/27] vfio/pci: Add CXL Type-2 device passthrough support
https://lore.kernel.org/linux-cxl/[email protected]
Manish Honap (27):
cxl/regs: Split the BAR block request and ioremap helpers cxl/regs: Let a BAR-owning driver own the component register block
cxl: Move component register defines to uapi/cxl/cxl_regs.h
cxl: Add cxl_reset_dvsec_sequence() for vfio-pci
vfio/pci: Add the CXL provider ops registration interface vfio/pci: Detect CXL devices and load the CXL provider on demand vfio/pci: Honor -EPROBE_DEFER from CXL provider probe vfio/pci: Fall back to plain vfio-pci when CXL init fails vfio/pci: Add a generic excluded-range list
vfio/pci: Migrate MSI-X exclusion onto the generic excluded-range list vfio/pci: Virtualize the CXL DVSEC in vfio_pci_config.c vfio/pci: Call the CXL open and close hooks around device use vfio/pci: Bracket PCI resets with the CXL reset hooks vfio/pci: Provide an opt-out for the CXL Type-2 extensions vfio/cxl: Add the vfio-cxl provider module skeleton vfio/cxl: Create the CXL memdev and set media ready at bind vfio/cxl: Own the whole component register BAR
vfio/cxl: Expose the HDM memory region to the guest vfio/cxl: Contain HDM memory errors with memory_failure() vfio/cxl: Expose the HDM decoder registers read-only to the guest vfio/cxl: Exclude the HDM decoder registers from direct BAR access vfio/cxl: Clear the HDM access gate after a hot reset vfio/cxl: Describe the CXL device and decoder geometry to userspace vfio/cxl: Export the HDM memory region as a dma-buf vfio/cxl: Run the CXL reset at the vfio reset points
Documentation: vfio-pci: Document CXL Type-2 device passthrough selftests/vfio: Add CXL Type-2 passthrough tests
Documentation/driver-api/index.rst | 1 + Documentation/driver-api/vfio-pci-cxl.rst | 188 +++++ MAINTAINERS | 10 + drivers/cxl/core/regs.c | 35 +- drivers/cxl/core/resource.c | 53 ++ drivers/cxl/cxl.h | 47 +- drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/cxl/Kconfig | 11 + drivers/vfio/pci/cxl/Makefile | 3 + drivers/vfio/pci/cxl/vfio_cxl_core.c | 704 ++++++++++++++++ drivers/vfio/pci/vfio_pci.c | 13 + drivers/vfio/pci/vfio_pci_config.c | 177 +++- drivers/vfio/pci/vfio_pci_core.c | 544 +++++++++++- drivers/vfio/pci/vfio_pci_dmabuf.c | 27 +- drivers/vfio/pci/vfio_pci_priv.h | 10 + drivers/vfio/pci/vfio_pci_rdwr.c | 50 +- include/cxl/cxl.h | 14 + include/cxl/pci.h | 3 + include/linux/vfio_pci_core.h | 40 + include/uapi/cxl/cxl_regs.h | 53 ++ include/uapi/linux/vfio.h | 24 + tools/testing/selftests/vfio/Makefile | 1 + .../vfio/lib/include/libvfio/iommu.h | 3 + tools/testing/selftests/vfio/lib/iommu.c | 29 + .../selftests/vfio/lib/vfio_pci_device.c | 57 +- .../selftests/vfio/vfio_cxl_type2_test.c | 780 ++++++++++++++++++ 27 files changed, 2789 insertions(+), 92 deletions(-) create mode 100644 Documentation/driver-api/vfio-pci-cxl.rst create mode 100644 drivers/vfio/pci/cxl/Kconfig create mode 100644 drivers/vfio/pci/cxl/Makefile create mode 100644 drivers/vfio/pci/cxl/vfio_cxl_core.c create mode 100644 include/uapi/cxl/cxl_regs.h create mode 100644 tools/testing/selftests/vfio/vfio_cxl_type2_test.c
Signature · 1 line
2.25.1
PATCH v5 2/27 cxl/regs: Let a BAR-owning driver own the component register block
From: Manish Honap <[email protected]>
cxl_map_component_regs() claims each mapped sub-block with devm_request_mem_region(). A driver that already requested the whole component register BAR, such as vfio-cxl, would then collide with that claim and fail to map the HDM decoder and RAS blocks.
Add cxl_reg_map_add_owned_resource() so such a driver records the resource it already owns on the register map, and skip the sub-block request when the block falls within an owned resource.
--- drivers/cxl/core/regs.c | 17 +++++++++++++++-- include/cxl/cxl.h | 2 ++ include/cxl/pci.h | 3 +++ 3 files changed, 20 insertions(+), 2 deletions(-)
drivers/cxl/core/regs.c +15 -2
@@ -216,6 +216,13 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr, }EXPORT_SYMBOL_NS_GPL (devm_cxl_iomap_block,"CXL" ); +void cxl_reg_map_add_owned_resource (struct cxl_register_map *map, +struct resource *res) +{ + map->owned = res; +} +EXPORT_SYMBOL_NS_GPL (cxl_reg_map_add_owned_resource,"CXL" ); +int cxl_map_component_regs (const struct cxl_register_map *map,struct cxl_component_regs *regs,unsigned long map_mask)
@@ -234,6 +241,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,struct mapinfo *mi = &mapinfo[i];resource_size_t addr;resource_size_t length; +struct resource res;if (!mi->rmap->valid)continue ;
@@ -241,8 +249,13 @@ int cxl_map_component_regs(const struct cxl_register_map *map,continue ; addr = map->resource + mi->rmap->offset; length = mi->rmap->size; - *(mi->addr) =devm_cxl_iomap_block (host, addr, length); -if (!*(mi->addr)) + res =DEFINE_RES_MEM (addr, length); + +if (map->owned &&resource_contains (map->owned, &res)) + *mi->addr =devm_cxl_ioremap_block (host, addr, length); +else + *mi->addr =devm_cxl_iomap_block (host, addr, length); +if (!*mi->addr)return -ENOMEM ; }
include/cxl/cxl.h +2 -0
@@ -217,6 +217,7 @@ struct cxl_pmu_reg_map {* @resource: physical resource base of the register block * @max_size: maximum mapping size to perform register search * @reg_type: see enum cxl_regloc_type +* @owned: driver-owned resource whose sub-blocks are mapped without re-requesting * @component_map: cxl_reg_map for component registers * @device_map: cxl_reg_maps for device registers * @pmu_map: cxl_reg_maps for CXL Performance Monitoring Units
@@ -227,6 +228,7 @@ struct cxl_register_map {resource_size_t resource;resource_size_t max_size;u8 reg_type; +struct resource *owned;union {struct cxl_component_reg_map component_map;struct cxl_device_reg_map device_map;
include/cxl/pci.h +3 -0
@@ -16,7 +16,10 @@ enum cxl_regloc_type {struct cxl_register_map ;struct pci_dev ; +struct resource ;int cxl_pci_setup_regs (struct pci_dev *pdev,enum cxl_regloc_type type,struct cxl_register_map *map); +void cxl_reg_map_add_owned_resource (struct cxl_register_map *map, +struct resource *res);#endif
Signature · 1 line
2.25.1
PATCH v5 1/27 cxl/regs: Split the BAR block request and ioremap helpers
In reply to [email protected]From: Manish Honap <[email protected]>
devm_cxl_iomap_block() claims a register block with devm_request_mem_region() and then maps it with devm_ioremap().
A driver that already owns the parent BAR needs to skip the sub-block request while still mapping it, so the request and the map must be callable independently.
Split them into devm_cxl_request_block() and devm_cxl_ioremap_block(). devm_cxl_iomap_block() keeps calling both in turn, so there is no functional change.
--- drivers/cxl/core/regs.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/cxl/core/regs.c +16 -2
@@ -177,6 +177,20 @@ void cxl_probe_device_regs(struct device *dev, void __iomem *base, }EXPORT_SYMBOL_NS_GPL (cxl_probe_device_regs,"CXL" ); +static struct resource *devm_cxl_request_block (struct device *dev, +resource_size_t addr, +resource_size_t length) +{ +return devm_request_mem_region (dev, addr, length,dev_name (dev)); +} + +static void __iomem *devm_cxl_ioremap_block (struct device *dev, +resource_size_t addr, +resource_size_t length) +{ +return devm_ioremap (dev, addr, length); +} +void __iomem *devm_cxl_iomap_block (struct device *dev,resource_size_t addr,resource_size_t length) {
@@ -186,7 +200,7 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,if (WARN_ON_ONCE (addr ==CXL_RESOURCE_NONE ))return NULL ; - res =devm_request_mem_region (dev, addr, length,dev_name (dev)); + res =devm_cxl_request_block (dev, addr, length);if (!res) {resource_size_t end = addr + length -1 ;
@@ -194,7 +208,7 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr,return NULL ; } - ret_val =devm_ioremap (dev, addr, length); + ret_val =devm_cxl_ioremap_block (dev, addr, length);if (!ret_val)dev_err (dev,"Failed to map region %pr\n" , res);
Signature · 1 line
2.25.1
PATCH v5 3/27 cxl: Move component register defines to uapi/cxl/cxl_regs.h
In reply to [email protected]From: Manish Honap <[email protected]>
The CXL component and HDM decoder register layout is spec-defined and is needed by a userspace consumer that cannot include the private drivers/cxl/cxl.h
The vfio-cxl selftest (tools/testing/selftests/vfio/vfio_cxl_type2_test.c) builds against installed uapi headers only. Move those defines to a new uapi header, the way PCI register offsets live in uapi pci_regs.h, and include it from drivers/cxl/cxl.h so existing in-kernel users are unchanged.
No functional change; the GENMASK and BIT forms are written as plain values as a uapi header requirement.
--- MAINTAINERS | 1 + drivers/cxl/cxl.h | 47 +------------------------------- include/uapi/cxl/cxl_regs.h | 53 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 46 deletions(-) create mode 100644 include/uapi/cxl/cxl_regs.h
MAINTAINERS +1 -0
@@ -6573,6 +6573,7 @@ F: Documentation/driver-api/cxl F: Documentation/userspace-api/fwctl/fwctl-cxl.rst F: drivers/cxl/ F: include/cxl/ +F: include/uapi/cxl/ F: include/uapi/linux/cxl_mem.h F: tools/testing/cxl/
drivers/cxl/cxl.h +1 -46
59 lines not on this page · raw message
include/uapi/cxl/cxl_regs.h new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v5 4/27 cxl: Add cxl_reset_dvsec_sequence() for vfio-pci
In reply to [email protected]From: Manish Honap <[email protected]>
vfio-pci needs to run a CXL reset on a passed-through Type-2 device without the host-memory handling in cxl_reset_function(): the memory behind the decoder belongs to the guest, so the HDM range collection and host CPU cache flush that the host path performs must be skipped.
Add cxl_reset_dvsec_sequence(), which runs the DVSEC reset under the device lock and restores HDM state through cxl_restore_state_after_pci_reset(), and cxl_reset_capable() to gate it on a function-scoped reset. Both are exported to the vfio-cxl module so vfio-pci core does not import the CXL namespace.
CXL Reset always clears memory, so there is no caller Memory Clear choice.
--- drivers/cxl/core/resource.c | 53 +++++++++++++++++++++++++++++++++++++ include/cxl/cxl.h | 12 +++++++++ 2 files changed, 65 insertions(+)
drivers/cxl/core/resource.c +53 -0
@@ -1398,3 +1398,56 @@ int cxl_reset_function(struct pci_dev *pdev, bool probe)cxl_pci_target_reset_done (pdev, &target_prepared);return rc; } + +/* True when a function-scoped CXL reset is available for @pdev. */ +bool cxl_reset_capable (struct pci_dev *pdev) +{ +u16 cap; + +if (cxl_reset_get_dvsec (pdev, &cap) <0 ) +return false; + +if (pdev->multifunction ||pci_num_vf (pdev)) +return false; + +return cxl_reset_hdm_available (pdev); +} +EXPORT_SYMBOL_FOR_MODULES (cxl_reset_capable,"vfio-cxl" ); + +/* +* Run the DVSEC reset sequence and restore HDM state for a caller that owns +* device quiesce and PCI config save/restore, such as vfio-pci. +* The HDM range collection and host CPU cache flush that cxl_reset_function() +* performs for host-owned memory are skipped as that memory belongs to the +* guest. +*/ +int cxl_reset_dvsec_sequence (struct pci_dev *pdev) +{ +bool target_prepared = false; +bool reset_initiated = false; +int dvsec; +int rc; +u16 cap; + + dvsec =cxl_reset_get_dvsec (pdev, &cap); +if (dvsec <0 ) +return dvsec; + +if (pdev->multifunction ||pci_num_vf (pdev)) +return -ENOTTY ; + +if (!pci_dev_trylock (pdev)) +return -EBUSY ; + + rc =cxl_reset_execute (pdev, &target_prepared, &reset_initiated, dvsec, + cap); +if (!rc) + rc =cxl_restore_state_after_pci_reset (pdev); +else if (reset_initiated) +cxl_reset_save_disabled_state (pdev); + +cxl_pci_target_reset_done (pdev, &target_prepared); +pci_dev_unlock (pdev); +return rc; +} +EXPORT_SYMBOL_FOR_MODULES (cxl_reset_dvsec_sequence,"vfio-cxl" );
include/cxl/cxl.h +12 -0
@@ -168,6 +168,8 @@ void pci_cxl_hdm_init(struct pci_dev *pdev);void pci_cxl_hdm_release (struct pci_dev *pdev);int cxl_restore_state_after_pci_reset (struct pci_dev *pdev);int cxl_reset_function (struct pci_dev *pdev,bool probe); +bool cxl_reset_capable (struct pci_dev *pdev); +int cxl_reset_dvsec_sequence (struct pci_dev *pdev);#else static inline void pci_cxl_hdm_init (struct pci_dev *pdev) {
@@ -186,6 +188,16 @@ static inline int cxl_reset_function(struct pci_dev *pdev, bool probe) {return -ENOTTY ; } + +static inline bool cxl_reset_capable (struct pci_dev *pdev) +{ +return false; +} + +static inline int cxl_reset_dvsec_sequence (struct pci_dev *pdev) +{ +return -ENOTTY ; +}#endif struct cxl_reg_map {
Signature · 1 line
2.25.1
PATCH v5 5/27 vfio/pci: Add the CXL provider ops registration interface
In reply to [email protected]From: Manish Honap <[email protected]>
vfio-pci-core must stay free of any CXL header dependency, so CXL Type-2 handling lives in a separate vfio-cxl module that plugs in a set of callbacks.
Add the registration interface:
- vfio-cxl registers a single struct vfio_cxl_ops at module_init
- vfio-pci-core stores it under an rwsem.
A second registration is refused with -EBUSY.
The owner field will be used to pin vfio-cxl for the lifetime of each bound CXL device.
--- drivers/vfio/pci/vfio_pci_core.c | 24 ++++++++++++++++++++++++ include/linux/vfio_pci_core.h | 10 ++++++++++ 2 files changed, 34 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +24 -0
@@ -2178,6 +2178,30 @@ static void vfio_pci_vga_uninit(struct vfio_pci_core_device *vdev) VGA_RSRC_LEGACY_MEM); } +static const struct vfio_cxl_ops *vfio_pci_cxl_ops; +static DECLARE_RWSEM (vfio_pci_cxl_ops_rwsem); + +int vfio_pci_core_register_cxl_ops (const struct vfio_cxl_ops *ops) +{ +guard (rwsem_write)(&vfio_pci_cxl_ops_rwsem); + +if (vfio_pci_cxl_ops) +return -EBUSY ; + + vfio_pci_cxl_ops = ops; +return 0 ; +} +EXPORT_SYMBOL_GPL (vfio_pci_core_register_cxl_ops); + +void vfio_pci_core_unregister_cxl_ops (const struct vfio_cxl_ops *ops) +{ +guard (rwsem_write)(&vfio_pci_cxl_ops_rwsem); + +if (vfio_pci_cxl_ops == ops) + vfio_pci_cxl_ops =NULL ; +} +EXPORT_SYMBOL_GPL (vfio_pci_core_unregister_cxl_ops); +int vfio_pci_core_init_dev (struct vfio_device *core_vdev) { struct vfio_pci_core_device *vdev =
include/linux/vfio_pci_core.h +10 -0
@@ -66,6 +66,16 @@ struct vfio_pci_device_ops { size_t nr_ranges); }; +struct vfio_cxl_ops { +int (*init)(struct vfio_pci_core_device *vdev); +void (*release)(struct vfio_pci_core_device *vdev); +/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ +struct module *owner; +}; + +int vfio_pci_core_register_cxl_ops (const struct vfio_cxl_ops *ops); +void vfio_pci_core_unregister_cxl_ops (const struct vfio_cxl_ops *ops); +#if IS_ENABLED (CONFIG_VFIO_PCI_DMABUF ) int vfio_pci_core_fill_phys_vec(struct phys_vec *phys_vec, struct vfio_region_dma_range *dma_ranges,
Signature · 1 line
2.25.1
PATCH v5 6/27 vfio/pci: Detect CXL devices and load the CXL provider on demand
In reply to [email protected]From: Manish Honap <[email protected]>
A CXL device needs the vfio-cxl callbacks, but pulling vfio-cxl and the CXL core in unconditionally would bloat every vfio-pci setup. Detect a CXL Type-2 device at bind and request_module("vfio-cxl") only then, then hand the device to the registered ops.
Each bound CXL device pins the provider through vfio_pci_get_cxl_ops() (try_module_get) and drops it with vfio_pci_put_cxl_ops() at release, so vfio-cxl can unload once no CXL device is bound.
If the provider is absent the device is driven as plain vfio-pci. A built-in provider whose initcall has not run yet is waited for with -EPROBE_DEFER; the deferred-probe machinery will retry the bind once the provider registers.
--- drivers/vfio/pci/vfio_pci_core.c | 84 ++++++++++++++++++++++++++++++++ include/linux/vfio_pci_core.h | 3 ++ 2 files changed, 87 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +84 -0
@@ -2202,6 +2202,84 @@ void vfio_pci_core_unregister_cxl_ops(const struct vfio_cxl_ops *ops) }EXPORT_SYMBOL_GPL (vfio_pci_core_unregister_cxl_ops); +static const struct vfio_cxl_ops *vfio_pci_get_cxl_ops (void ) +{ +guard (rwsem_read)(&vfio_pci_cxl_ops_rwsem); + +if (vfio_pci_cxl_ops &&try_module_get (vfio_pci_cxl_ops->owner)) +return vfio_pci_cxl_ops; + +return NULL ; +} + +static void vfio_pci_put_cxl_ops (const struct vfio_cxl_ops *ops) +{ +module_put (ops->owner); +} + +/* +* A CXL Type-2 device advertises both CXL.cache and CXL.mem in its CXL DVSEC. +* pcie_is_cxl() is also true for Type-1 (cache only) and Type-3 (mem only) +* devices, which the vfio-cxl provider does not handle, so confirm the Type-2 +* identity before engaging it. +*/ +static bool vfio_pci_is_cxl_type2 (struct pci_dev *pdev) +{ +u16 dvsec, cap; + +if (!pcie_is_cxl (pdev)) +return false; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return false; + +if (pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CAP , &cap)) +return false; + +return (cap &PCI_DVSEC_CXL_CACHE_CAPABLE ) && + (cap &PCI_DVSEC_CXL_MEM_CAPABLE ); +} + +/* +* Load vfio-cxl on demand for a CXL Type-2 device and hand the device to its +* ops. If the provider is absent the device is driven as plain vfio-pci; a +* built-in provider whose initcall has not run yet is waited for with +* -EPROBE_DEFER. +*/ +static int vfio_pci_core_cxl_init (struct vfio_pci_core_device *vdev) +{ +const struct vfio_cxl_ops *ops; +int ret; + +if (!vfio_pci_is_cxl_type2 (vdev->pdev)) +return 0 ; + +request_module ("vfio-cxl" ); + ops =vfio_pci_get_cxl_ops (); +if (!ops) +return IS_BUILTIN (CONFIG_VFIO_CXL ) ? -EPROBE_DEFER :0 ; + + ret = ops->init (vdev); +if (ret) { +vfio_pci_put_cxl_ops (ops); +return ret; + } + + vdev->cxl_ops = ops; +return 0 ; +} + +static void vfio_pci_core_cxl_release (struct vfio_pci_core_device *vdev) +{ +if (!vdev->cxl_ops) +return ; + + vdev->cxl_ops->release (vdev); +vfio_pci_put_cxl_ops (vdev->cxl_ops); +} +int vfio_pci_core_init_dev (struct vfio_device *core_vdev) { struct vfio_pci_core_device *vdev =
@@ -2223,6 +2301,10 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)init_rwsem (&vdev->memory_lock);xa_init (&vdev->ctx); + ret =vfio_pci_core_cxl_init (vdev); +if (ret) +return ret; +return 0 ; }EXPORT_SYMBOL_GPL (vfio_pci_core_init_dev);
@@ -2232,6 +2314,8 @@ void vfio_pci_core_release_dev(struct vfio_device *core_vdev)struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev); +vfio_pci_core_cxl_release (vdev); +mutex_destroy (&vdev->igate);mutex_destroy (&vdev->ioeventfds_lock);kfree (vdev->region);
include/linux/vfio_pci_core.h +3 -0
@@ -29,6 +29,7 @@ struct vfio_pci_core_device;struct vfio_pci_region ;struct p2pdma_provider ;struct dma_buf_attachment ; +struct vfio_cxl_state ;struct vfio_pci_eventfd {struct eventfd_ctx *ctx;
@@ -109,6 +110,8 @@ struct vfio_pci_core_device {struct vfio_device vdev;struct pci_dev *pdev;const struct vfio_pci_device_ops *pci_ops; +const struct vfio_cxl_ops *cxl_ops; +struct vfio_cxl_state *cxl;void __iomem *barmap[PCI_STD_NUM_BARS ];bool bar_mmap_supported[PCI_STD_NUM_BARS ];/* Flags modified at runtime - dedicated storage unit */
Signature · 1 line
2.25.1
PATCH v5 7/27 vfio/pci: Honor -EPROBE_DEFER from CXL provider probe
In reply to [email protected]From: Manish Honap <[email protected]>
The CXL provider .init() can return -EPROBE_DEFER when the device is not ready yet, for example when its CXL port has not enumerated. Handle that return separately from other init failures and propagate it, so the bind is retried later.
This keeps the deferred path distinct from the general failure path that a following patch makes non-fatal: an -EPROBE_DEFER must retry, not fall back to plain vfio-pci.
--- drivers/vfio/pci/vfio_pci_core.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +9 -0
@@ -2262,6 +2262,15 @@ static int vfio_pci_core_cxl_init(struct vfio_pci_core_device *vdev)return IS_BUILTIN (CONFIG_VFIO_CXL ) ? -EPROBE_DEFER :0 ; ret = ops->init (vdev); +/* +* A provider that is not ready yet (for example its CXL port has not +* enumerated) returns -EPROBE_DEFER. Propagate it so the bind retries +* rather than falling back to plain vfio-pci. +*/ +if (ret == -EPROBE_DEFER ) { +vfio_pci_put_cxl_ops (ops); +return ret; + }if (ret) {vfio_pci_put_cxl_ops (ops);return ret;
Signature · 1 line
2.25.1
PATCH v5 8/27 vfio/pci: Fall back to plain vfio-pci when CXL init fails
In reply to [email protected]From: Manish Honap <[email protected]>
A CXL Type-2 device is also a valid plain vfio-pci device. If the vfio-cxl provider cannot bring it up as Type-2, failing the bind would regress a device that previously worked as plain vfio-pci.
Make a non-deferred CXL init failure non-fatal: log it, drop the provider reference, leave cxl_ops NULL, and continue driving the device as plain vfio-pci. The -EPROBE_DEFER path added earlier still retries rather than falling back.
--- drivers/vfio/pci/vfio_pci_core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/vfio/pci/vfio_pci_core.c +9 -1
@@ -2271,9 +2271,17 @@ static int vfio_pci_core_cxl_init(struct vfio_pci_core_device *vdev)vfio_pci_put_cxl_ops (ops);return ret; } +/* +* Any other failure is non-fatal: a CXL device that cannot be brought +* up as Type-2 still works as plain vfio-pci, so log and continue +* rather than failing the bind. +*/ if (ret) { +pci_warn (vdev->pdev, +"CXL init failed (%d), continuing as plain vfio-pci\n" , + ret);vfio_pci_put_cxl_ops (ops); -return ret; +return 0 ; } vdev->cxl_ops = ops;
Signature · 1 line
2.25.1
PATCH v5 9/27 vfio/pci: Add a generic excluded-range list
In reply to [email protected]From: Manish Honap <[email protected]>
The MSI-X table is virtualized in vfio_pci_bar_rw() by an open-coded x_start/x_end window that fills reads with -1 and drops writes. Now that a generic excluded-range list expresses the same fill/drop behavior, register the MSI-X table as a read and write excluded range instead of special-casing it in the read/write path.
Add the range when the MSI-X capability is parsed in vfio_pci_core_enable() and clear the list in vfio_pci_core_disable() alongside the config teardown. The read/write path now relies solely on vfio_pci_bar_find_exclusion(), so MSI-X and a provider's (e.g. vfio-cxl) trapped registers share one mechanism.
No functional change.
--- drivers/vfio/pci/vfio_pci_core.c | 209 +++++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_priv.h | 9 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 8 ++ include/linux/vfio_pci_core.h | 14 +++ 4 files changed, 240 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +209 -0
@@ -24,6 +24,7 @@#include <linux/pci.h> #include <linux/pm_runtime.h> #include <linux/slab.h> +#include <linux/sort.h> #include <linux/types.h> #include <linux/uaccess.h> #include <linux/vgaarb.h>
@@ -1012,6 +1013,204 @@ static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,return vfio_info_add_capability (caps, &header,sizeof (header)); } +struct vfio_pci_excluded_range { +struct list_head entry; +int bar; +u64 start; +u64 size; +u32 flags; +}; + +int vfio_pci_core_add_excluded_range (struct vfio_pci_core_device *vdev,int bar, +u64 start,u64 size,u32 flags) +{ +struct vfio_pci_excluded_range *range; + + range =kzalloc_obj (*range); +if (!range) +return -ENOMEM ; + + range->bar = bar; + range->start = start; + range->size = size; + range->flags = flags; +list_add_tail (&range->entry, &vdev->excluded_ranges); + +return 0 ; +} +EXPORT_SYMBOL_GPL (vfio_pci_core_add_excluded_range); + +static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device *vdev) +{ +struct vfio_pci_excluded_range *range, *tmp; + +list_for_each_entry_safe (range, tmp, &vdev->excluded_ranges, entry) { +list_del (&range->entry); +kfree (range); + } +} + +bool vfio_pci_bar_find_exclusion (struct vfio_pci_core_device *vdev,int bar, +loff_t pos,size_t count,bool iswrite, +size_t *x_start,size_t *x_end) +{ +u32 want = iswrite ?VFIO_PCI_EXCLUDE_WRITE :VFIO_PCI_EXCLUDE_READ ; +struct vfio_pci_excluded_range *range; +bool found = false; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != bar || !(range->flags & want)) +continue ; +if (pos < range->start + range->size && + pos + count > range->start) { +/* +* A BAR can carry more than one excluded window (e.g. +* the MSI-X table and a CXL HDM decoder block). Return +* the overlapping window with the lowest start so the +* caller can walk them in order. +*/ +if (!found || range->start < *x_start) { + *x_start = range->start; + *x_end = range->start + range->size; + found = true; + } + } + } + +return found; +} + +/* True when [start, start + len) on @bar overlaps an mmap-excluded range. */ +static bool vfio_pci_bar_mmap_excluded (struct vfio_pci_core_device *vdev, +int bar,u64 start,u64 len) +{ +struct vfio_pci_excluded_range *range; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != bar || + !(range->flags &VFIO_PCI_EXCLUDE_MMAP )) +continue ; +if (start < range->start + range->size && + start + len > range->start) +return true; + } + +return false; +} + +/* A page-aligned mmap hole, derived from an mmap-excluded range. */ +struct vfio_pci_mmap_hole { +u64 start; +u64 end; +}; + +static int vfio_pci_mmap_hole_cmp (const void *a,const void *b) +{ +const struct vfio_pci_mmap_hole *x = a, *y = b; + +if (x->start < y->start) +return -1 ; +return x->start > y->start; +} + +/* +* Advertise the BAR as mmappable minus every page-aligned mmap-excluded hole. +* A BAR can carry several holes at unrelated offsets (for example an MSI-X +* table and one or more trapped CXL component sub-blocks, which the CXL spec +* locates by pointer, not at fixed offsets). +* Collect the holes, page-align and sort them, coalesce any that overlap or +* touch, and advertise the gaps. +*/ +static int vfio_pci_excluded_sparse_cap (struct vfio_pci_core_device *vdev, +int index,struct vfio_info_cap *caps) +{ +u64 bar_len =pci_resource_len (vdev->pdev, index); +struct vfio_region_info_cap_sparse_mmap *sparse; +struct vfio_pci_excluded_range *range; +struct vfio_pci_mmap_hole *holes; +int nr_holes =0 , nr_areas =0 , i, j; +size_t size; +u64 pos; +int ret; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) +if (range->bar == index && + (range->flags &VFIO_PCI_EXCLUDE_MMAP )) + nr_holes++; + +if (!nr_holes) +return 0 ; + + holes =kmalloc_array (nr_holes,sizeof (*holes),GFP_KERNEL ); +if (!holes) +return -ENOMEM ; + +/* +* mmap is page granular, so each hole rounds out to the page boundaries +* enclosing its excluded sub-range. The byte-granular exclusion still +* governs the fault and read/write paths; only the advertised mmap areas +* round to whole pages. +*/ + i =0 ; +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != index || + !(range->flags &VFIO_PCI_EXCLUDE_MMAP )) +continue ; + holes[i].start =ALIGN_DOWN (range->start,PAGE_SIZE ); + holes[i].end =ALIGN (range->start + range->size,PAGE_SIZE ); + i++; + } + +sort (holes, nr_holes,sizeof (*holes), vfio_pci_mmap_hole_cmp,NULL ); + +/* Coalesce holes that overlap or touch after page alignment. */ +for (i =0 , j =0 ; i < nr_holes; i++) { +if (j && holes[i].start <= holes[j -1 ].end) + holes[j -1 ].end =max (holes[j -1 ].end, holes[i].end); +else + holes[j++] = holes[i]; + } + nr_holes = j; + +/* One mmappable area per gap: before, between, and after the holes. */ +for (i =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) + nr_areas++; + pos = holes[i].end; + } +if (pos < bar_len) + nr_areas++; + + size =struct_size (sparse, areas, nr_areas); + sparse =kzalloc (size,GFP_KERNEL ); +if (!sparse) { +kfree (holes); +return -ENOMEM ; + } + + sparse->header.id =VFIO_REGION_INFO_CAP_SPARSE_MMAP ; + sparse->header.version =1 ; + sparse->nr_areas = nr_areas; + +for (i =0 , j =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = holes[i].start - pos; + j++; + } + pos = holes[i].end; + } +if (pos < bar_len) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = bar_len - pos; + } + +kfree (holes); + ret =vfio_info_add_capability (caps, &sparse->header, size); +kfree (sparse); +return ret; +} + int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops,
@@ -1160,6 +1359,10 @@ int vfio_pci_ioctl_get_region_info(struct vfio_device *core_vdev,if (ret)return ret; } + ret =vfio_pci_excluded_sparse_cap (vdev, info->index, + caps); +if (ret) +return ret; }break ;
@@ -1853,6 +2056,10 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vmaif (req_start + req_len > phys_len)return -EINVAL ; +/* An excluded sub-range is reachable only through its trap, not mmap. */ +if (vfio_pci_bar_mmap_excluded (vdev, index, req_start, req_len)) +return -EINVAL ; +/* * Ensure the BAR resource region is reserved for use. */
@@ -2317,6 +2524,7 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)INIT_LIST_HEAD (&vdev->dmabufs);init_rwsem (&vdev->memory_lock);xa_init (&vdev->ctx); +INIT_LIST_HEAD (&vdev->excluded_ranges); ret =vfio_pci_core_cxl_init (vdev);if (ret)
@@ -2332,6 +2540,7 @@ void vfio_pci_core_release_dev(struct vfio_device *core_vdev) container_of(core_vdev, struct vfio_pci_core_device, vdev);vfio_pci_core_cxl_release (vdev); +vfio_pci_free_excluded_ranges (vdev);mutex_destroy (&vdev->igate);mutex_destroy (&vdev->ioeventfds_lock);
drivers/vfio/pci/vfio_pci_priv.h +9 -0
@@ -44,6 +44,15 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,ssize_t vfio_pci_bar_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite); +/* +* If a read (or write) to [pos, pos + count) on @bar overlaps an excluded +* range, report the byte window do_io_rw() should fill with -1 (or drop) and +* return true. A single access spans at most one such window. +*/ +bool vfio_pci_bar_find_exclusion (struct vfio_pci_core_device *vdev,int bar, +loff_t pos,size_t count,bool iswrite, +size_t *x_start,size_t *x_end); +#ifdef CONFIG_VFIO_PCI_VGA ssize_t vfio_pci_vga_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite);
drivers/vfio/pci/vfio_pci_rdwr.c +8 -0
@@ -261,6 +261,14 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, x_end = vdev->msix_offset + vdev->msix_size; } +/* +* A provider-excluded sub-range is filled with -1 on read and dropped on +* write for the same reason: the guest reaches it only through the trap. +* An access spans at most one exclusion window. +*/ +vfio_pci_bar_find_exclusion (vdev, bar, pos, count, iswrite, + &x_start, &x_end); + done =vfio_pci_core_do_io_rw (vdev, res->flags &IORESOURCE_MEM , io, buf, pos, count, x_start, x_end, iswrite, max_width);
include/linux/vfio_pci_core.h +14 -0
@@ -162,6 +162,7 @@ struct vfio_pci_core_device {struct notifier_block nb;struct rw_semaphore memory_lock;struct list_head dmabufs; +struct list_head excluded_ranges; };enum vfio_pci_io_width {
@@ -172,6 +173,19 @@ enum vfio_pci_io_width { };/* Will be exported for vfio pci drivers usage */ +/* +* A provider can keep a BAR sub-range off the direct guest path, reached only +* through its own trap. The flags select which paths are excluded: mmap, and +* region read and write (an excluded read fills -1, an excluded write is +* dropped). +*/ +#define VFIO_PCI_EXCLUDE_MMAP BIT(0) +#define VFIO_PCI_EXCLUDE_READ BIT(1) +#define VFIO_PCI_EXCLUDE_WRITE BIT(2) + +int vfio_pci_core_add_excluded_range (struct vfio_pci_core_device *vdev,int bar, +u64 start,u64 size,u32 flags); + int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops,
Signature · 1 line
2.25.1
PATCH v5 10/27 vfio/pci: Migrate MSI-X exclusion onto the generic excluded-range list
In reply to [email protected]From: Manish Honap <[email protected]>
The MSI-X table is virtualized in vfio_pci_bar_rw() by an open-coded x_start/x_end window that fills reads with -1 and drops writes. Now that a generic excluded-range list expresses the same fill/drop behavior, register the MSI-X table as a read and write excluded range instead of special-casing it in the read/write path.
Add the range when the MSI-X capability is parsed in vfio_pci_core_enable() and clear the list in vfio_pci_core_disable() alongside the config teardown. The read/write path now relies solely on vfio_pci_bar_find_exclusion(), so MSI-X and a provider's (e.g. vfio-cxl) trapped registers share one mechanism.
No behavioral change: an access to the MSI-X table still reads -1 and drops writes.
--- drivers/vfio/pci/vfio_pci_core.c | 19 +++++++++++ drivers/vfio/pci/vfio_pci_rdwr.c | 56 ++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 13 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +19 -0
@@ -589,6 +589,8 @@ static const struct dev_pm_ops vfio_pci_core_pm_ops = { NULL) }; +static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device *vdev); +int vfio_pci_core_enable (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;
@@ -655,6 +657,22 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) vdev->msix_offset = table &PCI_MSIX_TABLE_OFFSET ; vdev->msix_size = ((flags &PCI_MSIX_FLAGS_QSIZE ) +1 ) *16 ; vdev->has_dyn_msix =pci_msix_can_alloc_dyn (pdev); + +/* +* Virtualize the MSI-X table through the excluded-range list: +* reads fill -1 and writes are dropped so the guest never +* reaches the hardware table directly. +*/ + ret =vfio_pci_core_add_excluded_range (vdev, vdev->msix_bar, + vdev->msix_offset, + vdev->msix_size, +VFIO_PCI_EXCLUDE_READ | +VFIO_PCI_EXCLUDE_WRITE ); +if (ret) { +vfio_pci_free_excluded_ranges (vdev); +vfio_config_free (vdev); + goto out_free_zdev; + } }else { vdev->msix_bar =0xFF ; vdev->has_dyn_msix = false;
@@ -741,6 +759,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) vdev->region =NULL ;/* don't krealloc a freed pointer */ vfio_config_free (vdev); +vfio_pci_free_excluded_ranges (vdev);for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { bar = i +PCI_STD_RESOURCES ;
drivers/vfio/pci/vfio_pci_rdwr.c +43 -13
64 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v5 11/27 vfio/pci: Virtualize the CXL DVSEC in vfio_pci_config.c
In reply to [email protected]From: Manish Honap <[email protected]>
A CXL Type-2 device is reprogrammable through its CXL DVSEC: a guest could set Config Lock, toggle CXL.cache and CXL.mem enable, or rewrite the HDM range registers that govern host memory decode. Virtualize the DVSEC so the guest sees a shadow it cannot use to reprogram the hardware.
Build a per-device cxl_perm permission map, modeled on msi_perm, when the device is bound through the CXL provider (e.g. vfio-cxl). The whole CXL DVSEC is served from the vconfig shadow; only Control and Control2 are guest programmable, while Capability, Status, Lock and the Range registers keep their firmware snapshot. A vendor DVSEC on the same device is
unaffected: the map is selected only for the CXL DVSEC offset.
Control2 carries the CXL reset and cache write-back-invalidate initiate bits as self-clearing doorbells. vfio never forwards them to hardware, so a custom writefn synthesizes their completion in the shadow: the initiate bit self-clears and the matching Status2 bit (Cache Invalid or Reset Done) is set, so a guest following the spec reset sequence (INIT_CACHE_WBI, poll Cache Invalid, INIT_CXL_RST, poll Reset Done) progresses instead of timing out. The host performs the real cache write-back and reset at the vfio reset points.
--- drivers/vfio/pci/vfio_pci_config.c | 132 ++++++++++++++++++++++++++++- include/linux/vfio_pci_core.h | 3 + 2 files changed, 133 insertions(+), 2 deletions(-)
drivers/vfio/pci/vfio_pci_config.c +130 -2
@@ -1747,6 +1747,119 @@ static const struct pci_device_id known_bogus_vf_intx_pin[] = {* for each area requiring emulated bits, but the array of pointers * would be comparable in size (at least for standard config space). */ +static int vfio_cxl_dvsec_write (struct vfio_pci_core_device *vdev,int pos, +int count,struct perm_bits *perm, +int offset,__le32 val) +{ +__le16 *pctrl2, *pstatus2; +u16 ctrl2, status2; +int start; + + count =vfio_default_config_write (vdev, pos, count, perm, offset, val); +if (count <0 ) +return count; + +/* Only Control2 carries the self-clearing doorbells. */ +if (offset >PCI_DVSEC_CXL_CTRL2 || + offset + count <=PCI_DVSEC_CXL_CTRL2 ) +return count; + + start =vfio_find_cap_start (vdev, pos); + pctrl2 = (__le16 *)&vdev->vconfig[start +PCI_DVSEC_CXL_CTRL2 ]; + pstatus2 = (__le16 *)&vdev->vconfig[start +PCI_DVSEC_CXL_STATUS2 ]; + ctrl2 =le16_to_cpu (*pctrl2); + status2 =le16_to_cpu (*pstatus2); + +/* +* INIT_CACHE_WBI and INIT_CXL_RST are self-clearing doorbells that vfio +* never forwards to hardware. Synthesize their completion in the shadow +* so the guest poll finishes: clear the initiate bit and set the +* matching Status2 completion. +*/ +if (ctrl2 &PCI_DVSEC_CXL_INIT_CACHE_WBI ) { + ctrl2 &= ~PCI_DVSEC_CXL_INIT_CACHE_WBI ; + status2 |=PCI_DVSEC_CXL_CACHE_INV ; + } +if (ctrl2 &PCI_DVSEC_CXL_INIT_CXL_RST ) { + ctrl2 &= ~PCI_DVSEC_CXL_INIT_CXL_RST ; + status2 &= ~PCI_DVSEC_CXL_RST_ERR ; + status2 |=PCI_DVSEC_CXL_RST_DONE ; + } + + *pctrl2 =cpu_to_le16 (ctrl2); + *pstatus2 =cpu_to_le16 (status2); + +return count; +} + +static int init_cxl_dvsec_perm (struct perm_bits *perm,int len) +{ +int i; + +if (alloc_perm_bits (perm, len)) +return -ENOMEM ; + + perm->writefn = vfio_cxl_dvsec_write; + +/* Serve the whole CXL DVSEC from the shadow. */ +for (i =0 ; i < len; i++) +p_setb (perm, i, (u8 )ALL_VIRT ,NO_WRITE ); + +/* +* Control and Control2 are guest programmable; Capability, Status, +* Lock and the Range registers keep their firmware snapshot, so the +* guest cannot set Config Lock or rewrite the capability and ranges. +*/ +p_setw (perm,PCI_DVSEC_CXL_CTRL , (u16 )ALL_VIRT , (u16 )ALL_WRITE ); +p_setw (perm,PCI_DVSEC_CXL_CTRL2 , (u16 )ALL_VIRT , (u16 )ALL_WRITE ); + +return 0 ; +} + +/* Virtualize the CXL DVSEC so a guest cannot reprogram the device through it. */ +static int vfio_cxl_dvsec_init (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +u32 dword; +u16 dvsec; +int len, ret; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return 0 ; + + ret =pci_read_config_dword (pdev, dvsec +PCI_DVSEC_HEADER1 , &dword); +if (ret) +return pcibios_err_to_errno (ret); + len =PCI_DVSEC_HEADER1_LEN (dword); + +/* +* The virtualization writes fixed DVSEC offsets up to Status2 (the reset +* doorbell stamps it). A device that reports a shorter DVSEC is not a +* usable Type-2 function; leave it as plain vfio-pci rather than index the +* device-length-sized perm allocation past its end. +*/ +if (len <PCI_DVSEC_CXL_STATUS2 +2 ) +return 0 ; + + vdev->cxl_perm = kmalloc_obj(struct perm_bits, GFP_KERNEL_ACCOUNT); +if (!vdev->cxl_perm) +return -ENOMEM ; + + ret =init_cxl_dvsec_perm (vdev->cxl_perm, len); +if (ret) { +kfree (vdev->cxl_perm); + vdev->cxl_perm =NULL ; +return ret; + } + + vdev->cxl_dvsec = dvsec; + vdev->cxl_dvsec_len = len; + +return 0 ; +} +int vfio_config_init (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;
@@ -1842,6 +1955,12 @@ int vfio_config_init(struct vfio_pci_core_device *vdev)if (ret) goto out; +if (vdev->cxl_ops) { + ret =vfio_cxl_dvsec_init (vdev); +if (ret) + goto out; + } +return 0 ; out:
@@ -1863,6 +1982,12 @@ void vfio_config_free(struct vfio_pci_core_device *vdev)kfree (vdev->msi_perm); vdev->msi_perm =NULL ; } +if (vdev->cxl_perm) { +free_perm_bits (vdev->cxl_perm); +kfree (vdev->cxl_perm); + vdev->cxl_perm =NULL ; + vdev->cxl_dvsec =0 ; + } }/*
@@ -1926,12 +2051,15 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,* of the extended capability list. Use default, ro * access, which will virtualize the id and next values. */ + cap_start =vfio_find_cap_start (vdev, *ppos); +if (cap_id >PCI_EXT_CAP_ID_MAX ) perm = &direct_ro_perms; +else if (cap_id ==PCI_EXT_CAP_ID_DVSEC && vdev->cxl_perm && + cap_start == vdev->cxl_dvsec) + perm = vdev->cxl_perm;else perm = &ecap_perms[cap_id]; - - cap_start =vfio_find_cap_start (vdev, *ppos); }else {WARN_ON (cap_id >PCI_CAP_ID_MAX );
include/linux/vfio_pci_core.h +3 -0
@@ -120,6 +120,9 @@ struct vfio_pci_core_device {u8 *pci_config_map;u8 *vconfig;struct perm_bits *msi_perm; +struct perm_bits *cxl_perm; +u16 cxl_dvsec; +u16 cxl_dvsec_len;spinlock_t irqlock;struct mutex igate;struct xarray ctx;
Signature · 1 line
2.25.1
PATCH v5 12/27 vfio/pci: Call the CXL open and close hooks around device use
In reply to [email protected]From: Manish Honap <[email protected]>
Volatile CXL register state must be sampled from live hardware when the guest opens the device, not at bind, because a low-power transition between bind and open can leave the bind-time values stale.
Add open and close to the CXL ops and call them from the common enable and close paths. A failed open unwinds the enable in reverse order:
unmap the BARs, drop the excluded ranges, then free the virtual config.
Factor the BAR unmap out of vfio_pci_core_disable() into a helper so the enable failure path and the disable path share it.
--- drivers/vfio/pci/vfio_pci_core.c | 40 +++++++++++++++++++++++++------- include/linux/vfio_pci_core.h | 2 ++ 2 files changed, 33 insertions(+), 9 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +31 -9
@@ -591,6 +591,21 @@ static const struct dev_pm_ops vfio_pci_core_pm_ops = {static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device *vdev); +static void vfio_pci_core_unmap_bars (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +int i, bar; + +for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { + bar = i +PCI_STD_RESOURCES ; +if (IS_ERR_OR_NULL (vdev->barmap[bar])) +continue ; +pci_iounmap (pdev, vdev->barmap[bar]); +pci_release_selected_regions (pdev,1 << bar); + vdev->barmap[bar] =NULL ; + } +} +int vfio_pci_core_enable (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;
@@ -683,8 +698,19 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)vfio_pci_core_map_bars (vdev); +if (vdev->cxl_ops) { + ret = vdev->cxl_ops->open_device (vdev); +if (ret) + goto out_free_config; + } +return 0 ; +out_free_config: +/* A failed first open never reaches vfio_pci_core_disable(). */ +vfio_pci_core_unmap_bars (vdev); +vfio_pci_free_excluded_ranges (vdev); +vfio_config_free (vdev); out_free_zdev:vfio_pci_zdev_close_device (vdev); out_free_state:
@@ -705,7 +731,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)struct pci_dev *pdev = vdev->pdev;struct vfio_pci_dummy_resource *dummy_res, *tmp;struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; -int i, bar; +int i;/* For needs_reset */ lockdep_assert_held (&vdev->vdev.dev_set->lock);
@@ -761,14 +787,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)vfio_config_free (vdev);vfio_pci_free_excluded_ranges (vdev); -for (i =0 ; i <PCI_STD_NUM_BARS ; i++) { - bar = i +PCI_STD_RESOURCES ; -if (IS_ERR_OR_NULL (vdev->barmap[bar])) -continue ; -pci_iounmap (pdev, vdev->barmap[bar]); -pci_release_selected_regions (pdev,1 << bar); - vdev->barmap[bar] =NULL ; - } +vfio_pci_core_unmap_bars (vdev);list_for_each_entry_safe (dummy_res, tmp, &vdev->dummy_resources_list, res_next) {
@@ -851,6 +870,9 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev)#endif vfio_pci_dma_buf_cleanup (vdev); +if (vdev->cxl_ops) + vdev->cxl_ops->close_device (vdev); +vfio_pci_core_disable (vdev);mutex_lock (&vdev->igate);
include/linux/vfio_pci_core.h +2 -0
@@ -70,6 +70,8 @@ struct vfio_pci_device_ops {struct vfio_cxl_ops {int (*init)(struct vfio_pci_core_device *vdev);void (*release)(struct vfio_pci_core_device *vdev); +int (*open_device)(struct vfio_pci_core_device *vdev); +void (*close_device)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ struct module *owner; };
Signature · 1 line
2.25.1
PATCH v5 13/27 vfio/pci: Bracket PCI resets with the CXL reset hooks
In reply to [email protected]From: Manish Honap <[email protected]>
A CXL Type-2 device must quiesce its coherent link before any reset and restore its HDM decoder state afterward, or a reset can leave the device mastering the bus over decoders that are no longer valid. This has to happen around every reset the PCI core drives for the device, not only the ones VFIO issues directly.
Add reset_prepare and reset_done to the CXL ops and register the matching pci_error_handlers callbacks.
--- drivers/vfio/pci/vfio_pci_core.c | 18 ++++++++++++++++++ include/linux/vfio_pci_core.h | 2 ++ 2 files changed, 20 insertions(+)
drivers/vfio/pci/vfio_pci_core.c +18 -0
@@ -2794,8 +2794,26 @@ int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, }EXPORT_SYMBOL_GPL (vfio_pci_core_sriov_configure); +static void vfio_pci_core_reset_prepare (struct pci_dev *pdev) +{ +struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev); + +if (vdev->cxl_ops) + vdev->cxl_ops->reset_prepare (vdev); +} + +static void vfio_pci_core_reset_done (struct pci_dev *pdev) +{ +struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev); + +if (vdev->cxl_ops) + vdev->cxl_ops->reset_done (vdev); +} +const struct pci_error_handlers vfio_pci_core_err_handlers = { .error_detected = vfio_pci_core_aer_err_detected, + .reset_prepare = vfio_pci_core_reset_prepare, + .reset_done = vfio_pci_core_reset_done, };EXPORT_SYMBOL_GPL (vfio_pci_core_err_handlers);
include/linux/vfio_pci_core.h +2 -0
@@ -72,6 +72,8 @@ struct vfio_cxl_ops {void (*release)(struct vfio_pci_core_device *vdev);int (*open_device)(struct vfio_pci_core_device *vdev);void (*close_device)(struct vfio_pci_core_device *vdev); +void (*reset_prepare)(struct vfio_pci_core_device *vdev); +void (*reset_done)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ struct module *owner; };
Signature · 1 line
2.25.1
PATCH v5 14/27 vfio/pci: Provide an opt-out for the CXL Type-2 extensions
In reply to [email protected]From: Manish Honap <[email protected]>
Let an admin or a variant driver keep the CXL Type-2 extensions out of the path and drive the device as plain vfio-pci. Add a vfio_pci.disable_cxl module parameter, and let a variant driver set vdev->disable_cxl in its own init callback for per-device control. The parameter is built only when the CXL provider is configured.
--- drivers/vfio/pci/vfio_pci.c | 13 +++++++++++++ include/linux/vfio_pci_core.h | 1 + 2 files changed, 14 insertions(+)
drivers/vfio/pci/vfio_pci.c +13 -0
@@ -60,6 +60,16 @@ static bool disable_denylist;module_param (disable_denylist, bool,0444 );MODULE_PARM_DESC (disable_denylist,"Disable use of device denylist. Disabling the denylist allows binding to devices with known errata that may lead to exploitable stability or security issues when accessed by untrusted users." ); +#if IS_ENABLED (CONFIG_VFIO_CXL ) +/* +* A variant driver may instead set vdev->disable_cxl in its own init callback +* for per-device control without this module-wide parameter. +*/ +static bool disable_cxl; +module_param (disable_cxl, bool,0644 ); +MODULE_PARM_DESC (disable_cxl,"Disable CXL Type-2 extensions for all devices bound to vfio-pci." ); +#endif +static bool vfio_pci_dev_in_denylist (struct pci_dev *pdev) {switch (pdev->vendor) {
@@ -142,6 +152,9 @@ static int vfio_pci_init_dev(struct vfio_device *core_vdev)#ifdef CONFIG_VFIO_PCI_VGA vdev->disable_vga = disable_vga;#endif +#if IS_ENABLED (CONFIG_VFIO_CXL ) + vdev->disable_cxl = disable_cxl; +#endif return vfio_pci_core_init_dev (core_vdev); }
include/linux/vfio_pci_core.h +1 -0
@@ -149,6 +149,7 @@ struct vfio_pci_core_device { bool disable_idle_d3:1; bool nointxmask:1; bool disable_vga:1; + bool disable_cxl:1;/* Flags modified at runtime - dedicated storage unit */ bool needs_reset;bool pm_intx_masked;
Signature · 1 line
2.25.1
PATCH v5 15/27 vfio/cxl: Add the vfio-cxl provider module skeleton
In reply to [email protected]From: Manish Honap <[email protected]>
Add the vfio-cxl module that vfio-pci-core loads on demand for a CXL Type-2 device, registering its callbacks at module_init.
vfio-cxl is a feature of vfio-pci-core rather than a variant driver, so its Kconfig sources above the variant drivers and its objects link right after vfio-pci-core while still being loaded on demand through the vfio-cxl MODULE_ALIAS.
--- MAINTAINERS | 7 ++++ drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/cxl/Kconfig | 11 +++++ drivers/vfio/pci/cxl/Makefile | 3 ++ drivers/vfio/pci/cxl/vfio_cxl_core.c | 61 ++++++++++++++++++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 drivers/vfio/pci/cxl/Kconfig create mode 100644 drivers/vfio/pci/cxl/Makefile create mode 100644 drivers/vfio/pci/cxl/vfio_cxl_core.c
MAINTAINERS +7 -0
@@ -28639,6 +28639,13 @@ L: [email protected] S: Maintained F: drivers/vfio/cdx/* +VFIO CXL DRIVER +M: Manish Honap <[email protected]> +L: [email protected] +L: [email protected] +S: Supported +F: drivers/vfio/pci/cxl/ + VFIO DRIVER M: Alex Williamson <[email protected]> L: [email protected]
drivers/vfio/pci/Kconfig +2 -0
@@ -58,6 +58,8 @@ config VFIO_PCI_ZDEV_KVM config VFIO_PCI_DMABUF def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER +source "drivers/vfio/pci/cxl/Kconfig" + source "drivers/vfio/pci/mlx5/Kconfig" source "drivers/vfio/pci/ism/Kconfig"
drivers/vfio/pci/Makefile +2 -0
@@ -5,6 +5,8 @@ vfio-pci-core-$(CONFIG_VFIO_PCI_ZDEV_KVM) += vfio_pci_zdev.o vfio-pci-core-$(CONFIG_VFIO_PCI_DMABUF) += vfio_pci_dmabuf.o obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o +obj-$(CONFIG_VFIO_CXL) += cxl/ + vfio-pci-y := vfio_pci.o vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
drivers/vfio/pci/cxl/Kconfig new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0-only +config VFIO_CXL + tristate "VFIO support for CXL Type-2 devices" + depends on CXL_BUS && CXL_MEM + select VFIO_PCI_CORE + help + Add CXL HDM handling to vfio-pci so a CXL Type-2 device, such as a + CXL-attached accelerator, can be assigned to a virtual machine. + vfio-pci loads this module on demand when it binds a CXL device. + + If you don't know what to do here, say N.
drivers/vfio/pci/cxl/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_VFIO_CXL) += vfio-cxl.o +vfio-cxl-y := vfio_cxl_core.o
drivers/vfio/pci/cxl/vfio_cxl_core.c new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v5 16/27 vfio/cxl: Create the CXL memdev and set media ready at bind
In reply to [email protected]From: Manish Honap <[email protected]>
At bind, build the CXL memory device for the passed-through Type-2 accelerator so it joins the CXL topology and its HDM region resolves to a host physical range. A Type-2 device has no mailbox, so there is no media-ready register to poll: set media ready directly once the component registers validate (mirroring drivers/net/ethernet/sfc/efx_cxl.c)
As per current vfio-cxl support, reject a device with:
- more than one HDM decoder
- interleaving enabled
- whose reset the host cannot service
The CXL-core allocations are grouped with devres so a failed bind unwinds
them: init failure falls back to plain vfio-pci with the device still bound, so devm would otherwise hold them until unbind.
A low-power transition would reset the CXL Type-2 function and lose its CXL.mem contents, so keep it in D0 while it is assigned.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 118 +++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_core.c | 16 ++++ 2 files changed, 134 insertions(+)
drivers/vfio/pci/cxl/vfio_cxl_core.c +118 -0
@@ -6,15 +6,132 @@*/ #include <linux/module.h> +#include <linux/pci.h> +#include <linux/range.h> #include <linux/vfio_pci_core.h> +#include <cxl/cxl.h> +#include <cxl/pci.h> + +/** +* struct vfio_cxl_state - per-device state for a vfio-cxl device +* @cxlds: CXL device state; kept first for devm_cxl_dev_state_create() +* @cxlmd: memory device joined to the CXL topology at bind +* @hpa_range: host physical range of the HDM region +*/ +struct vfio_cxl_state { +struct cxl_dev_state cxlds; +struct cxl_memdev *cxlmd; +struct range hpa_range; +};static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) { +struct pci_dev *pdev = vdev->pdev; +struct vfio_cxl_state *cxl; +struct cxl_memdev *cxlmd; +u64 hdm_size, serial; +u16 dvsec; +int ret; + +/* +* pdev->hdm is cached at PCI enumeration, before any driver binds, so a +* device without it has no usable HDM decoder. Fall back to plain +* vfio-pci rather than deferring the bind forever. +*/ +if (!pdev->hdm) +return -ENODEV ; + +/* The guest drives one virtual decoder; multiple are unsupported. */ +if (pdev->hdm->decoder_count !=1 ) +return -EOPNOTSUPP ; + +/* An interleaved decoder cannot be mapped 1:1 to the guest. */ +if (pdev->hdm->settings[0 ].interleave_ways !=1 ) +return -EOPNOTSUPP ; + +/* +* The guest drives resets through the CXL Device DVSEC and polls the +* shadow for completion. If the host cannot service a function-scoped +* CXL reset, that request could never complete, so refuse the device +* rather than advertise a reset the guest would poll on forever. +*/ +if (!cxl_reset_capable (pdev)) +return -EOPNOTSUPP ; + + hdm_size =range_len (&pdev->hdm->settings[0 ].hpa_range); +if (!hdm_size) +return -ENXIO ; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); + serial =pci_get_dsn (pdev); + +/* +* Group the CXL-core allocations so a later failure unwinds them here. +* A failed init falls back to plain vfio-pci with the device still +* bound, so devm would otherwise hold them until unbind. +*/ +if (!devres_open_group (&pdev->dev,NULL ,GFP_KERNEL )) +return -ENOMEM ; + + cxl =devm_cxl_dev_state_create (&pdev->dev,CXL_DEVTYPE_DEVMEM , serial, + dvsec, struct vfio_cxl_state, cxlds, + false); +if (!cxl) { + ret = -ENOMEM ; + goto err; + } + + ret =cxl_pci_setup_regs (pdev,CXL_REGLOC_RBI_COMPONENT , + &cxl->cxlds.reg_map); +if (ret) { +pci_err (pdev,"vfio-cxl: no component registers\n" ); + goto err; + } + +if (!cxl->cxlds.reg_map.component_map.hdm_decoder.valid) { +pci_err (pdev,"vfio-cxl: HDM decoder registers not found\n" ); + ret = -ENODEV ; + goto err; + } + +/* +* A Type-2 accelerator has no mailbox and no media-ready register, so +* set media ready directly. +*/ + cxl->cxlds.media_ready = true; + + ret =cxl_set_capacity (&cxl->cxlds, hdm_size); +if (ret) + goto err; + + cxlmd =devm_cxl_probe_mem (&cxl->cxlds, &cxl->hpa_range); +if (IS_ERR (cxlmd)) { + ret =PTR_ERR (cxlmd); + goto err; + } + + cxl->cxlmd = cxlmd; +devres_close_group (&pdev->dev,NULL ); + +/* +* Powering a CXL Type-2 function down and back up reinitializes its +* device state and discards the contents of its coherent memory. Pin +* it in D0 for as long as it is assigned so CXL.mem stays intact. +*/ + vdev->disable_idle_d3 = true; + vdev->cxl = cxl; +return 0 ; + +err: +devres_release_group (&pdev->dev,NULL ); +return ret; }static void vfio_cxl_release_device (struct vfio_pci_core_device *vdev) { + vdev->cxl =NULL ; }static int vfio_cxl_open_device (struct vfio_pci_core_device *vdev)
@@ -59,3 +176,4 @@ module_exit(vfio_cxl_exit);MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("VFIO support for CXL Type-2 devices" ); +MODULE_IMPORT_NS ("CXL" );
drivers/vfio/pci/vfio_pci_core.c +16 -0
@@ -325,6 +325,14 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t statbool needs_restore = false, needs_save = false;int ret; +/* +* A low-power transition would reset the CXL Type-2 function and lose +* its CXL.mem contents, so keep it in D0 regardless of the guest or +* VMM request. +*/ +if (vdev->cxl_ops && state >PCI_D0 ) + state =PCI_D0 ; +/* Prevent changing power state for PFs with VFs enabled */ if (state >PCI_D0 ) {lockdep_assert_held_write (&vdev->memory_lock);
@@ -373,6 +381,14 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t statstatic int vfio_pci_runtime_pm_entry (struct vfio_pci_core_device *vdev,struct eventfd_ctx *efdctx) { +/* +* Low power entry lets the PCI core autosuspend the device to D3hot, +* which would soft-reset a CXL Type-2 device and lose its coherent HDM +* memory. Refuse it for CXL; the device stays in D0. +*/ +if (vdev->cxl_ops) +return -EINVAL ; +/* * The vdev power related flags are protected with 'memory_lock' * semaphore.
Signature · 1 line
2.25.1
PATCH v5 17/27 vfio/cxl: Own the whole component register BAR
In reply to [email protected]From: Manish Honap <[email protected]>
devm_cxl_probe_mem() maps the component registers through the CXL core, which by default claims each HDM and RAS sub-block. That collides with the full-BAR request vfio-pci-core makes when the guest opens the device.
After the component registers are discovered, declare the containing BAR owned with cxl_reg_map_add_owned_resource(). The core then ioremaps the sub-blocks without requesting them, so vfio-pci-core owns the whole BAR and the requests do not collide.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/vfio/pci/cxl/vfio_cxl_core.c +9 -0
@@ -89,6 +89,15 @@ static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) goto err; } +/* +* vfio-pci-core requests the whole component-register BAR when the +* guest opens the device. Declare that BAR owned so the CXL core +* ioremaps the HDM and RAS sub-blocks without claiming them, and the +* full-BAR request does not collide. +*/ +cxl_reg_map_add_owned_resource (&cxl->cxlds.reg_map, +pci_resource_n (pdev, pdev->hdm->hdm_bar)); +if (!cxl->cxlds.reg_map.component_map.hdm_decoder.valid) {pci_err (pdev,"vfio-cxl: HDM decoder registers not found\n" ); ret = -ENODEV ;
Signature · 1 line
2.25.1
PATCH v5 18/27 vfio/cxl: Expose the HDM memory region to the guest
In reply to [email protected]From: Manish Honap <[email protected]>
A CXL Type-2 guest maps the device HDM memory to use its coherent CXL.mem. The HDM memory is a host physical range with no struct page, so the guest and KVM need a write-back mapping of it.
Register the range as an mmap-able region under VFIO_REGION_TYPE_PCI_VENDOR_TYPE with the CXL vendor id rather than a bespoke region type, per open because vfio_pci_core_disable() tears down all dynamic regions on close.
Own the resolved host physical range exclusively (IORESOURCE_EXCLUSIVE) so nothing, /dev/mem included, can map a conflicting cacheable alias that would fault the host once the range is mapped write-back. There is no devm form of the exclusive request, so pair it with a devm release action.
vfio_pci_zap_bars() only unmaps the fixed PCI BAR range, so revoke mmap-capable device-specific regions there too. Otherwise a Memory-Space disable, a D3 transition, or a reset would leave the guest a live mapping into quiesced device memory; the fault handler re-gates on device state before it inserts a pfn again.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 186 +++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_core.c | 37 ++++++ include/linux/vfio_pci_core.h | 1 + include/uapi/linux/vfio.h | 4 + 4 files changed, 228 insertions(+)
drivers/vfio/pci/cxl/vfio_cxl_core.c +186 -0
@@ -5,9 +5,13 @@* Copyright (c) 2026 NVIDIA Corporation & Affiliates */ +#include <linux/cleanup.h> +#include <linux/io.h> +#include <linux/mm.h> #include <linux/module.h> #include <linux/pci.h> #include <linux/range.h> +#include <linux/uaccess.h> #include <linux/vfio_pci_core.h> #include <cxl/cxl.h> #include <cxl/pci.h>
@@ -17,13 +21,144 @@* @cxlds: CXL device state; kept first for devm_cxl_dev_state_create() * @cxlmd: memory device joined to the CXL topology at bind * @hpa_range: host physical range of the HDM region +* @hdm_valid: true when host CPU access to the HDM range is safe; under memory_lock */ struct vfio_cxl_state {struct cxl_dev_state cxlds;struct cxl_memdev *cxlmd;struct range hpa_range; +bool hdm_valid; }; +static unsigned long vfio_cxl_mem_pgoff (struct vm_area_struct *vma, +unsigned long addr) +{ +unsigned long mask = (1U << (VFIO_PCI_OFFSET_SHIFT -PAGE_SHIFT )) -1 ; + +return (vma->vm_pgoff & mask) + ((addr - vma->vm_start) >>PAGE_SHIFT ); +} + +static vm_fault_t vfio_cxl_mem_huge_fault (struct vm_fault *vmf, +unsigned int order) +{ +struct vm_area_struct *vma = vmf->vma; +struct vfio_pci_core_device *vdev = vma->vm_private_data; +struct vfio_cxl_state *cxl = vdev->cxl; +unsigned long addr =ALIGN_DOWN (vmf->address,PAGE_SIZE << order); +unsigned long pfn =PHYS_PFN (cxl->hpa_range.start) + +vfio_cxl_mem_pgoff (vma, addr); +vm_fault_t ret =VM_FAULT_FALLBACK ; + +if (is_aligned_for_order (vma, addr, pfn, order)) { + scoped_guard(rwsem_read, &vdev->memory_lock) { +/* +* Insert a PFN only for a known-good decoder whose +* media is ready and whose Memory-Space is enabled. +*/ +if (__vfio_pci_memory_enabled (vdev) && + cxl->hdm_valid && cxl->cxlds.media_ready) + ret =vfio_pci_vmf_insert_pfn (vdev, vmf, pfn, + order); +else + ret =VM_FAULT_SIGBUS ; + } + } + +return ret; +} + +static vm_fault_t vfio_cxl_mem_fault (struct vm_fault *vmf) +{ +return vfio_cxl_mem_huge_fault (vmf,0 ); +} + +static const struct vm_operations_struct vfio_cxl_mem_vm_ops = { + .fault = vfio_cxl_mem_fault, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .huge_fault = vfio_cxl_mem_huge_fault, +#endif +}; + +static int vfio_cxl_mem_mmap (struct vfio_pci_core_device *vdev, +struct vfio_pci_region *region, +struct vm_area_struct *vma) +{ +unsigned long mask = (1U << (VFIO_PCI_OFFSET_SHIFT -PAGE_SHIFT )) -1 ; +u64 req_start = (vma->vm_pgoff & mask) <<PAGE_SHIFT ; +u64 req_len = vma->vm_end - vma->vm_start; + +if (req_start + req_len > region->size) +return -EINVAL ; + +/* +* CXL.mem is coherent memory, so leave the mapping write-back cacheable. +*/ +vm_flags_set (vma,VM_IO |VM_PFNMAP |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &vfio_cxl_mem_vm_ops; + vma->vm_private_data = vdev; + +return 0 ; +} + +static ssize_t vfio_cxl_mem_rw (struct vfio_pci_core_device *vdev, +char __user *buf,size_t count,loff_t *ppos, +bool iswrite) +{ +struct vfio_cxl_state *cxl = vdev->cxl; +u64 pos = *ppos &VFIO_PCI_OFFSET_MASK ; +void *mem; +ssize_t done; + +if (pos >=range_len (&cxl->hpa_range)) +return -EINVAL ; + count =min_t (size_t, count,range_len (&cxl->hpa_range) - pos); + + scoped_guard(rwsem_read, &vdev->memory_lock) { +/* +* Same gate as the fault path: only touch the HDM range with +* the decoder in a known-good state AND Memory-Space enabled, +* or a host CPU access aborts as a fatal SError. +*/ +if (!cxl->hdm_valid || !__vfio_pci_memory_enabled (vdev)) +return -EIO ; + + mem =memremap (cxl->hpa_range.start + pos, count,MEMREMAP_WB ); +if (!mem) +return -ENOMEM ; +if (iswrite) + done =copy_from_user (mem, buf, count) ? -EFAULT : count; +else + done =copy_to_user (buf, mem, count) ? -EFAULT : count; +memunmap (mem); + } +if (done >0 ) + *ppos += done; + +return done; +} + +/* +* The CXL regions carry no per-region state (region->data is the shared, +* devm-managed vfio_cxl_state), so releasing a region is a no-op. +*/ +static void vfio_cxl_region_release (struct vfio_pci_core_device *vdev, +struct vfio_pci_region *region) +{ +} + +static const struct vfio_pci_regops vfio_cxl_mem_regops = { + .rw = vfio_cxl_mem_rw, + .mmap = vfio_cxl_mem_mmap, + .release = vfio_cxl_region_release, +}; + +static void vfio_cxl_release_hpa (void *data) +{ +struct vfio_cxl_state *cxl = data; + +release_mem_region (cxl->hpa_range.start,range_len (&cxl->hpa_range)); +} +static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;
@@ -120,6 +255,21 @@ static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) goto err; } +/* +* Claim the range IORESOURCE_EXCLUSIVE so no conflicting cacheable +* alias can fault the host once it is mapped write-back; there is no +* devm form, so pair it with a devm release action. +*/ +if (!request_mem_region_exclusive (cxl->hpa_range.start, +range_len (&cxl->hpa_range), +"vfio-cxl-hdm" )) { + ret = -EBUSY ; + goto err; + } + ret =devm_add_action_or_reset (&pdev->dev, vfio_cxl_release_hpa, cxl); +if (ret) + goto err; + cxl->cxlmd = cxlmd;devres_close_group (&pdev->dev,NULL );
@@ -143,13 +293,49 @@ static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev) vdev->cxl =NULL ; } +static int vfio_cxl_add_region (struct vfio_pci_core_device *vdev,u32 subtype, +const struct vfio_pci_regops *ops,size_t size, +u32 flags) +{ +u32 type =VFIO_REGION_TYPE_PCI_VENDOR_TYPE |PCI_VENDOR_ID_CXL ; + +return vfio_pci_core_register_dev_region (vdev, type, subtype, ops, + size, flags, vdev->cxl); +} +static int vfio_cxl_open_device (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; +int ret; + +/* +* vfio_pci_core_disable() frees all dynamic regions on close, so register +* them here per open rather than at bind. A failed first open never +* reaches close_device(), so unwind on error. +*/ + ret =vfio_cxl_add_region (vdev,VFIO_REGION_SUBTYPE_CXL_MEM , + &vfio_cxl_mem_regops,range_len (&cxl->hpa_range), +VFIO_REGION_INFO_FLAG_READ | +VFIO_REGION_INFO_FLAG_WRITE | +VFIO_REGION_INFO_FLAG_MMAP ); +if (ret) +return ret; + +/* +* The decoder is firmware-committed, so host access to the HDM range is +* safe. Open the access gate; reset and power transitions clear it until +* the decoder is restored. +*/ + cxl->hdm_valid = true; +return 0 ; }static void vfio_cxl_close_device (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; + + cxl->hdm_valid = false; }static void vfio_cxl_reset_prepare (struct vfio_pci_core_device *vdev)
drivers/vfio/pci/vfio_pci_core.c +37 -0
@@ -1295,6 +1295,23 @@ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, }EXPORT_SYMBOL_GPL (vfio_pci_core_register_dev_region); +/* +* Unregister the most recently registered dynamic region. Used to unwind a +* partially built region set on an open-time error; regions are otherwise +* released together in vfio_pci_core_disable(). +*/ +void vfio_pci_core_unregister_dev_region (struct vfio_pci_core_device *vdev) +{ +struct vfio_pci_region *region; + +if (WARN_ON (!vdev->num_regions)) +return ; + + region = &vdev->region[--vdev->num_regions]; + region->ops->release (vdev, region); +} +EXPORT_SYMBOL_GPL (vfio_pci_core_unregister_dev_region); +static int vfio_pci_info_atomic_cap (struct vfio_pci_core_device *vdev,struct vfio_info_cap *caps) {
@@ -1972,8 +1989,28 @@ static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev)loff_t start =VFIO_PCI_INDEX_TO_OFFSET (VFIO_PCI_BAR0_REGION_INDEX );loff_t end =VFIO_PCI_INDEX_TO_OFFSET (VFIO_PCI_ROM_REGION_INDEX );loff_t len = end - start; +unsigned int i;unmap_mapping_range (core_vdev->inode->i_mapping, start, len, true); + +/* +* The unmap above covers the PCI BARs; mmap-capable device-specific +* regions (e.g. a vfio-cxl HDM window) sit above that range, so revoke +* them here too, or a Memory-Space disable, D3/PM transition, or reset +* would leave the guest a live mapping into quiesced device memory. +* Callers hold memory_lock, so the region array is stable. +*/ +for (i =0 ; i < vdev->num_regions; i++) { +struct vfio_pci_region *region = &vdev->region[i]; +loff_t roff; + +if (!(region->flags &VFIO_REGION_INFO_FLAG_MMAP )) +continue ; + + roff =VFIO_PCI_INDEX_TO_OFFSET (VFIO_PCI_NUM_REGIONS + i); +unmap_mapping_range (core_vdev->inode->i_mapping, roff, + region->size, true); + } }void vfio_pci_zap_and_down_write_memory_lock (struct vfio_pci_core_device *vdev)
include/linux/vfio_pci_core.h +1 -0
@@ -198,6 +198,7 @@ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops, size_t size, u32 flags, void *data); +void vfio_pci_core_unregister_dev_region (struct vfio_pci_core_device *vdev);void vfio_pci_core_close_device (struct vfio_device *core_vdev);int vfio_pci_core_init_dev (struct vfio_device *core_vdev);void vfio_pci_core_release_dev (struct vfio_device *core_vdev);
include/uapi/linux/vfio.h +4 -0
@@ -370,6 +370,10 @@ struct vfio_region_info_cap_type {*/ #define VFIO_REGION_SUBTYPE_IBM_NVLINK2_ATSD (1) +/* CXL Type-2 device (0x1e98) sub-types for VFIO_REGION_TYPE_PCI_VENDOR_TYPE */ +/* CXL.mem HDM region of a Type-2 device, mmap-able */ +#define VFIO_REGION_SUBTYPE_CXL_MEM (1) +/* sub-types for VFIO_REGION_TYPE_GFX */ #define VFIO_REGION_SUBTYPE_GFX_EDID (1)
Signature · 1 line
2.25.1
PATCH v5 19/27 vfio/cxl: Contain HDM memory errors with memory_failure()
In reply to [email protected]From: Manish Honap <[email protected]>
The HDM memory region is mmap-able, so an fd holder can fault its struct-page-less device memory in from the host CPU. A memory error on that range cannot be routed through the normal struct-page path and would otherwise escalate to a fatal host SError.
Register the range with memory_failure() via register_pfn_address_space() so such an error is instead contained to unmapping the range and delivering a SIGBUS to the fd holder. Provide a pfn-to-vma-offset callback so memory_failure() can map a poisoned pfn back to the file offset of each user mapping and unmap it; the region is a single linear range at hpa_range.start, so recover the offset the same way the fault handler derived the pfn.
register_pfn_address_space() returns -EOPNOTSUPP when CONFIG_MEMORY_FAILURE is off, in which case expose the region without containment. Unregister the range on close.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+)
drivers/vfio/pci/cxl/vfio_cxl_core.c +76 -0
@@ -7,10 +7,12 @@#include <linux/cleanup.h> #include <linux/io.h> +#include <linux/memory-failure.h> #include <linux/mm.h> #include <linux/module.h> #include <linux/pci.h> #include <linux/range.h> +#include <linux/slab.h> #include <linux/uaccess.h> #include <linux/vfio_pci_core.h> #include <cxl/cxl.h>
@@ -21,12 +23,14 @@* @cxlds: CXL device state; kept first for devm_cxl_dev_state_create() * @cxlmd: memory device joined to the CXL topology at bind * @hpa_range: host physical range of the HDM region +* @hdm_pfn_space: HDM-region pfn range registered with memory_failure() * @hdm_valid: true when host CPU access to the HDM range is safe; under memory_lock */ struct vfio_cxl_state {struct cxl_dev_state cxlds;struct cxl_memdev *cxlmd;struct range hpa_range; +struct pfn_address_space hdm_pfn_space;bool hdm_valid; };
@@ -152,6 +156,62 @@ static const struct vfio_pci_regops vfio_cxl_mem_regops = { .release = vfio_cxl_region_release, }; +/* +* Map a poisoned HDM-region pfn back to the file offset of each user mapping so +* memory_failure() can unmap it and signal the fd holder. The region is a +* single linear range at hpa_range.start; recover the per-vma file offset the +* same way the fault handler derived the pfn. +*/ +static int vfio_cxl_pfn_to_vma_pgoff (struct vm_area_struct *vma, +unsigned long pfn,pgoff_t *pgoff) +{ +struct vfio_pci_core_device *vdev; +struct vfio_cxl_state *cxl; +pgoff_t vma_off, pfn_off; +unsigned long start_pfn; + +if (vma->vm_ops != &vfio_cxl_mem_vm_ops) +return -ENOENT ; + + vdev = vma->vm_private_data; + cxl = vdev->cxl; + + start_pfn =PHYS_PFN (cxl->hpa_range.start); +if (pfn < start_pfn || + pfn >= start_pfn + (range_len (&cxl->hpa_range) >>PAGE_SHIFT )) +return -EFAULT ; + + pfn_off = pfn - start_pfn; + vma_off = vma->vm_pgoff & + ((1UL << (VFIO_PCI_OFFSET_SHIFT -PAGE_SHIFT )) -1 ); +/* Skip VMAs that do not map the pfn, e.g. a partial mmap of the region. */ +if (pfn_off < vma_off || pfn_off - vma_off >=vma_pages (vma)) +return -EFAULT ; + + *pgoff = vma->vm_pgoff + (pfn_off - vma_off); +return 0 ; +} + +/* +* The HDM region is struct-page-less device memory, so a memory error on it +* cannot be routed through the normal page path. Register the range with +* memory_failure() so such an error is contained to unmapping the range and a +* SIGBUS to the fd holder instead of escalating to a host SError. +*/ +static int vfio_cxl_register_pfn_space (struct vfio_pci_core_device *vdev) +{ +struct vfio_cxl_state *cxl = vdev->cxl; +unsigned long start_pfn =PHYS_PFN (cxl->hpa_range.start); + + cxl->hdm_pfn_space.node.start = start_pfn; + cxl->hdm_pfn_space.node.last = + start_pfn + (range_len (&cxl->hpa_range) >>PAGE_SHIFT ) -1 ; + cxl->hdm_pfn_space.mapping = vdev->vdev.inode->i_mapping; + cxl->hdm_pfn_space.pfn_to_vma_pgoff = vfio_cxl_pfn_to_vma_pgoff; + +return register_pfn_address_space (&cxl->hdm_pfn_space); +} +static void vfio_cxl_release_hpa (void *data) {struct vfio_cxl_state *cxl = data;
@@ -321,6 +381,16 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)if (ret)return ret; +/* +* The HDM region is advertised mmap-able, so a fd holder can fault its +* struct-page-less device memory in from the host CPU. Register it with +* memory_failure() to contain a memory error. -EOPNOTSUPP means +* CONFIG_MEMORY_FAILURE is off, so run without containment. +*/ + ret =vfio_cxl_register_pfn_space (vdev); +if (ret && ret != -EOPNOTSUPP ) + goto err_unregister_mem; +/* * The decoder is firmware-committed, so host access to the HDM range is * safe. Open the access gate; reset and power transitions clear it until
@@ -329,6 +399,11 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev) cxl->hdm_valid = true;return 0 ; + +err_unregister_mem: +vfio_pci_core_unregister_dev_region (vdev); + +return ret; }static void vfio_cxl_close_device (struct vfio_pci_core_device *vdev)
@@ -336,6 +411,7 @@ static void vfio_cxl_close_device(struct vfio_pci_core_device *vdev)struct vfio_cxl_state *cxl = vdev->cxl; cxl->hdm_valid = false; +unregister_pfn_address_space (&cxl->hdm_pfn_space); }static void vfio_cxl_reset_prepare (struct vfio_pci_core_device *vdev)
Signature · 1 line
2.25.1
PATCH v5 20/27 vfio/cxl: Expose the HDM decoder registers read-only to the guest
In reply to [email protected]From: Manish Honap <[email protected]>
A CXL Type-2 guest reads the HDM decoder registers to learn the HDM region it was handed. Those registers live in the component BAR that vfio-pci owns.
Map the decoder block at bind: its location comes from the pdev->hdm enumeration cache, and vfio-pci owns the BAR, so map it without claiming the block and expose it as a second, read-only region under VFIO_REGION_TYPE_PCI_VENDOR_TYPE with the CXL vendor id, registered per open like the HDM memory region.
Serve reads live from the mapped block and absorb writes without forwarding them to hardware.
Registering a second region is the first point at which an open-time failure must unwind an already-registered region, so add vfio_pci_core_unregister_dev_region() to drop the most recently registered region, and use it to unwind the HDM memory region if the decoder region fails to register.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 81 +++++++++++++++++++++++++++- include/uapi/linux/vfio.h | 2 + 2 files changed, 82 insertions(+), 1 deletion(-)
drivers/vfio/pci/cxl/vfio_cxl_core.c +80 -1
@@ -24,6 +24,8 @@* @cxlmd: memory device joined to the CXL topology at bind * @hpa_range: host physical range of the HDM region * @hdm_pfn_space: HDM-region pfn range registered with memory_failure() +* @hdm_regs: mapped HDM decoder registers, read live by the decoder region +* @hdm_len: length of the HDM decoder register block * @hdm_valid: true when host CPU access to the HDM range is safe; under memory_lock */ struct vfio_cxl_state {
@@ -31,6 +33,8 @@ struct vfio_cxl_state {struct cxl_memdev *cxlmd;struct range hpa_range;struct pfn_address_space hdm_pfn_space; +void __iomem *hdm_regs; +u32 hdm_len;bool hdm_valid; };
@@ -212,6 +216,56 @@ static int vfio_cxl_register_pfn_space(struct vfio_pci_core_device *vdev)return register_pfn_address_space (&cxl->hdm_pfn_space); } +static ssize_t vfio_cxl_comp_rw (struct vfio_pci_core_device *vdev, +char __user *buf,size_t count,loff_t *ppos, +bool iswrite) +{ +struct vfio_cxl_state *cxl = vdev->cxl; +loff_t pos = *ppos &VFIO_PCI_OFFSET_MASK ; +void *tmp; + +if (pos >= cxl->hdm_len) +return -EINVAL ; + +/* The decoder registers take only aligned dword accesses. */ +if (pos %sizeof (u32) || count %sizeof (u32)) +return -EINVAL ; + + count =min_t (size_t, count, cxl->hdm_len - pos); + +/* +* The host committed and locked the physical decoder before the guest +* saw the device, so the guest never drives it: absorb writes without +* forwarding them to hardware. The guest programs a GPA that the VMM +* virtualizes; reads return the live registers, which already report the +* decoder committed. BASE_LOW and BASE_HIGH carry the host HPA, visible +* only to the trusted VMM that virtualizes it away from the guest. +*/ +if (iswrite) { + *ppos += count; +return count; + } + + tmp =kmalloc (count,GFP_KERNEL ); +if (!tmp) +return -ENOMEM ; + +memcpy_fromio (tmp, cxl->hdm_regs + pos, count); +if (copy_to_user (buf, tmp, count)) { +kfree (tmp); +return -EFAULT ; + } +kfree (tmp); + + *ppos += count; +return count; +} + +static const struct vfio_pci_regops vfio_cxl_comp_regops = { + .rw = vfio_cxl_comp_rw, + .release = vfio_cxl_region_release, +}; +static void vfio_cxl_release_hpa (void *data) {struct vfio_cxl_state *cxl = data;
@@ -299,6 +353,21 @@ static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) goto err; } +/* +* Map the HDM decoder registers so the decoder region can read them +* live. The block location comes from the enumeration cache in +* pdev->hdm; vfio-pci owns the BAR, so map without claiming the block. +*/ + cxl->hdm_regs =devm_ioremap (&pdev->dev, +pci_resource_start (pdev, pdev->hdm->hdm_bar) + + pdev->hdm->hdm_offset, pdev->hdm->hdm_size); +if (!cxl->hdm_regs) { + ret = -ENOMEM ; + goto err; + } + + cxl->hdm_len = pdev->hdm->hdm_size; +/* * A Type-2 accelerator has no mailbox and no media-ready register, so * set media ready directly.
@@ -381,6 +450,13 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)if (ret)return ret; + ret =vfio_cxl_add_region (vdev,VFIO_REGION_SUBTYPE_CXL_COMP_REGS , + &vfio_cxl_comp_regops, cxl->hdm_len, +VFIO_REGION_INFO_FLAG_READ | +VFIO_REGION_INFO_FLAG_WRITE ); +if (ret) + goto err_unregister_mem; +/* * The HDM region is advertised mmap-able, so a fd holder can fault its * struct-page-less device memory in from the host CPU. Register it with
@@ -389,7 +465,7 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)*/ ret =vfio_cxl_register_pfn_space (vdev);if (ret && ret != -EOPNOTSUPP ) - goto err_unregister_mem; + goto err_unregister_comp;/* * The decoder is firmware-committed, so host access to the HDM range is
@@ -400,6 +476,9 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)return 0 ; +err_unregister_comp: +vfio_pci_core_unregister_dev_region (vdev); + err_unregister_mem:vfio_pci_core_unregister_dev_region (vdev);
include/uapi/linux/vfio.h +2 -0
@@ -373,6 +373,8 @@ struct vfio_region_info_cap_type {/* CXL Type-2 device (0x1e98) sub-types for VFIO_REGION_TYPE_PCI_VENDOR_TYPE */ /* CXL.mem HDM region of a Type-2 device, mmap-able */ #define VFIO_REGION_SUBTYPE_CXL_MEM (1) +/* CXL HDM decoder registers: read live, guest writes are absorbed */ +#define VFIO_REGION_SUBTYPE_CXL_COMP_REGS (2)/* sub-types for VFIO_REGION_TYPE_GFX */ #define VFIO_REGION_SUBTYPE_GFX_EDID (1)
Signature · 1 line
2.25.1
PATCH v5 21/27 vfio/cxl: Exclude the HDM decoder registers from direct BAR access
In reply to [email protected]From: Manish Honap <[email protected]>
The HDM decoder register block lives in the component BAR that vfio-pci owns. The guest reads the decoder only through the trapped, read-only decoder region; it must not reach the physical registers through the BAR directly, and a host read of the range through a kernel mapping could abort as a fatal host SError.
Add the block to the generic excluded-range list with VFIO_PCI_EXCLUDE_MMAP | VFIO_PCI_EXCLUDE_READ | VFIO_PCI_EXCLUDE_WRITE similar to the MSI-X exclusion.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
drivers/vfio/pci/cxl/vfio_cxl_core.c +17 -0
@@ -435,8 +435,25 @@ static int vfio_cxl_add_region(struct vfio_pci_core_device *vdev, u32 subtype,static int vfio_cxl_open_device (struct vfio_pci_core_device *vdev) {struct vfio_cxl_state *cxl = vdev->cxl; +struct pci_dev *pdev = vdev->pdev;int ret; +/* +* Keep the HDM decoder block out of the guest's direct BAR access: the +* guest reaches it only through the trapped decoder region, and a host +* read of the range through a kernel mapping could abort as an SError. +* Exclude it from mmap, fill reads with -1 and drop writes. The list is +* cleared on close, so re-add it per open. +*/ + ret =vfio_pci_core_add_excluded_range (vdev, pdev->hdm->hdm_bar, + pdev->hdm->hdm_offset, + cxl->hdm_len, +VFIO_PCI_EXCLUDE_MMAP | +VFIO_PCI_EXCLUDE_READ | +VFIO_PCI_EXCLUDE_WRITE ); +if (ret) +return ret; +/* * vfio_pci_core_disable() frees all dynamic regions on close, so register * them here per open rather than at bind. A failed first open never
Signature · 1 line
2.25.1
PATCH v5 22/27 vfio/cxl: Clear the HDM access gate after a hot reset
In reply to [email protected]From: Manish Honap <[email protected]>
The HDM memory fault inserts a PFN only when the decoder is in a known-good state (cxl->hdm_valid), so the host CPU never faults the coherent range into a disabled decoder.
VFIO_DEVICE_PCI_HOT_RESET drives a plain secondary bus reset rather than the CXL reset sequence, so nothing restores the decoder in the reset_done handler. A secondary bus reset only reaches a CXL endpoint when the upstream port has SBR unmasked (CXL r3.1 sec 8.1.5.2); in that case the reset decommits the decoder, so clear cxl->hdm_valid to keep a later fault from inserting a PFN into a dead decoder.
A subsequent VFIO_DEVICE_RESET runs the CXL reset sequence and restores it, and a masked SBR is a no-op that leaves the decoder intact.
vfio_cxl_sbr_unmasked() mirrors the cxl_sbr_masked() check the PCI core uses (drivers/pci/pci.c) for its own CXL bus reset.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
drivers/vfio/pci/cxl/vfio_cxl_core.c +38 -0
@@ -514,8 +514,46 @@ static void vfio_cxl_reset_prepare(struct vfio_pci_core_device *vdev) { } +/* +* A secondary bus reset only reaches a CXL endpoint when the upstream port +* has SBR unmasked (CXL r3.1 sec 8.1.5.2); otherwise the Bridge Control SBR +* bit is ignored and the decoder is left intact. Mirrors the cxl_sbr_masked() +* check the PCI core uses for its own CXL bus reset. +*/ +static bool vfio_cxl_sbr_unmasked (struct pci_dev *pdev) +{ +struct pci_dev *bridge =pci_upstream_bridge (pdev); +u16 dvsec, ctl; + +if (!bridge) +return false; + + dvsec =pci_find_dvsec_capability (bridge,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_PORT ); +if (!dvsec) +return false; + +if (pci_read_config_word (bridge, dvsec +PCI_DVSEC_CXL_PORT_CTL , &ctl)) +return false; + +return ctl &PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR ; +} +static void vfio_cxl_reset_done (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; + +/* +* VFIO_DEVICE_PCI_HOT_RESET drives a plain secondary bus reset, not the +* CXL-aware cxl_reset_bus_function(), so nothing restores the HDM +* decoder here. When the upstream port has SBR unmasked the reset +* decommits the decoder; gate host access to the HDM range so a later +* fault cannot insert a PFN into a dead decoder. A VFIO_DEVICE_RESET +* then runs the CXL reset sequence and restores it. A masked SBR is a +* no-op and leaves the decoder intact. +*/ +if (vfio_cxl_sbr_unmasked (vdev->pdev)) + cxl->hdm_valid = false; }static const struct vfio_cxl_ops vfio_cxl_ops = {
Signature · 1 line
2.25.1
PATCH v5 23/27 vfio/cxl: Describe the CXL device and decoder geometry to userspace
In reply to [email protected]From: Manish Honap <[email protected]>
Advertise the assignment with a device-info flag so a VMM can tell a CXL Type-2 device apart from a plain vfio-pci one. On the trapped decoder register region, add a capability carrying the BAR and offset of the HDM decoder block so the VMM can place the trapped window where the guest expects to find it and size the guest's fixed memory window.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 16 ++++++++++++++++ drivers/vfio/pci/vfio_pci_core.c | 3 +++ include/uapi/linux/vfio.h | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+)
drivers/vfio/pci/cxl/vfio_cxl_core.c +16 -0
@@ -261,9 +261,25 @@ static ssize_t vfio_cxl_comp_rw(struct vfio_pci_core_device *vdev,return count; } +static int vfio_cxl_comp_add_capability (struct vfio_pci_core_device *vdev, +struct vfio_pci_region *region, +struct vfio_info_cap *caps) +{ +struct cxl_hdm_info *hdm = vdev->pdev->hdm; +struct vfio_region_info_cap_cxl_comp_regs cap = { + .header.id =VFIO_REGION_INFO_CAP_CXL_COMP_REGS , + .header.version =1 , + .bar = hdm->hdm_bar, + .offset = hdm->hdm_offset, + }; + +return vfio_info_add_capability (caps, &cap.header,sizeof (cap)); +} +static const struct vfio_pci_regops vfio_cxl_comp_regops = { .rw = vfio_cxl_comp_rw, .release = vfio_cxl_region_release, + .add_capability = vfio_cxl_comp_add_capability, };static void vfio_cxl_release_hpa (void *data)
drivers/vfio/pci/vfio_pci_core.c +3 -0
@@ -1364,6 +1364,9 @@ static int vfio_pci_ioctl_get_info(struct vfio_pci_core_device *vdev,if (vdev->reset_works) info.flags |=VFIO_DEVICE_FLAGS_RESET ; +if (vdev->cxl_ops) + info.flags |=VFIO_DEVICE_FLAGS_CXL ; + info.num_regions =VFIO_PCI_NUM_REGIONS + vdev->num_regions; info.num_irqs =VFIO_PCI_NUM_IRQS ;
include/uapi/linux/vfio.h +18 -0
@@ -215,6 +215,7 @@ struct vfio_device_info {#define VFIO_DEVICE_FLAGS_FSL_MC (1 << 6)/* vfio-fsl-mc device */ #define VFIO_DEVICE_FLAGS_CAPS (1 << 7)/* Info supports caps */ #define VFIO_DEVICE_FLAGS_CDX (1 << 8)/* vfio-cdx device */ +#define VFIO_DEVICE_FLAGS_CXL (1 << 9)/* vfio-cxl device */ __u32 num_regions;/* Max region index + 1 */ __u32 num_irqs;/* Max IRQ index + 1 */ __u32 cap_offset;/* Offset within info struct of first cap */
@@ -503,6 +504,23 @@ struct vfio_region_info_cap_nvlink2_lnkspd {__u32 __pad; }; +/* +* Geometry of a CXL Type-2 device's HDM decoder registers, so a VMM can place +* the trapped component register window where the guest expects it. The trapped +* region spans the whole HDM decoder block (every decoder), not just decoder 0: +* a VMM reads the decoder count and each decoder's committed base from the block +* itself. Additional trapped component capabilities, such as CXL RAS, are +* exposed as their own region subtypes rather than by extending this cap. +*/ +#define VFIO_REGION_INFO_CAP_CXL_COMP_REGS 6 + +struct vfio_region_info_cap_cxl_comp_regs { +struct vfio_info_cap_header header; +__u32 bar; +__u32 __resv; +__aligned_u64 offset; +}; +/** * VFIO_DEVICE_GET_IRQ_INFO - _IOWR(VFIO_TYPE, VFIO_BASE + 9, * struct vfio_irq_info)
Signature · 1 line
2.25.1
PATCH v5 24/27 vfio/cxl: Export the HDM memory region as a dma-buf
In reply to [email protected]From: Manish Honap <[email protected]>
A Type-2 accelerator issues ATS-translated DMA to addresses inside its own HDM window, so that coherent host range must be present in the guest's IOAS (the iommufd IOAS backing the nested SMMU stage-2). iommufd maps a struct-page-less range only by fd, via IOMMU_IOAS_MAP_FILE over a dma-buf; a userspace-VA IOMMU_IOAS_MAP of the HDM mmap is rejected because the VMA is VM_IO | VM_PFNMAP. Without a dma-buf the range could only be mapped through an out-of-tree PFNMAP work-around.
vfio-pci already exports BAR memory as a P2P dma-buf, but the exporter is BAR-only: vfio_pci_core_feature_dma_buf() rejects any region index at or above the ROM index, and vfio_pci_core_get_dmabuf_phys() resolves the physical range from a PCI BAR. The HDM memory region is a dynamic device-specific region, not a BAR.
Let a device-specific region reach the device's get_dmabuf_phys(): a region index at or above VFIO_PCI_NUM_REGIONS skips the BAR-resource check and is validated by the driver instead, bounded to the regions that exist. Install a CXL-aware get_dmabuf_phys() in the vfio-cxl provider that returns cxl->hpa_range for the HDM memory region and delegates real BARs to the core, keeping the BAR path unchanged and the core free of CXL knowledge.
The HDM window is coherent host memory with no p2pdma provider of its own, so borrow BAR 0's, matching nvgrace-gpu's handling of its non-BAR device memory. The iommufd importer does not consume the provider; the scatterlist map path (real peer DMA) is left to a follow-up once upstream grows a negotiated interconnect for coherent CXL memory.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 60 ++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_dmabuf.c | 27 +++++++++++-- 2 files changed, 83 insertions(+), 4 deletions(-)
drivers/vfio/pci/cxl/vfio_cxl_core.c +60 -0
@@ -11,6 +11,7 @@#include <linux/mm.h> #include <linux/module.h> #include <linux/pci.h> +#include <linux/pci-p2pdma.h> #include <linux/range.h> #include <linux/slab.h> #include <linux/uaccess.h>
@@ -27,6 +28,7 @@* @hdm_regs: mapped HDM decoder registers, read live by the decoder region * @hdm_len: length of the HDM decoder register block * @hdm_valid: true when host CPU access to the HDM range is safe; under memory_lock +* @mem_region_index: vfio region index of the mmap-able HDM memory region */ struct vfio_cxl_state {struct cxl_dev_state cxlds;
@@ -36,6 +38,7 @@ struct vfio_cxl_state {void __iomem *hdm_regs;u32 hdm_len;bool hdm_valid; +unsigned int mem_region_index; }; static unsigned long vfio_cxl_mem_pgoff(struct vm_area_struct *vma,
@@ -289,6 +292,50 @@ static void vfio_cxl_release_hpa(void *data)release_mem_region (cxl->hpa_range.start,range_len (&cxl->hpa_range)); } +/* +* Resolve the physical range that backs a dma-buf export. The core exporter +* only knows BARs; teach it the HDM memory region so a guest IOAS can map the +* coherent window by fd (IOMMU_IOAS_MAP_FILE) instead of the removed PFNMAP +* work-around. Real BARs stay on the byte-identical core path. +*/ +static int vfio_cxl_get_dmabuf_phys (struct vfio_pci_core_device *vdev, +struct p2pdma_provider **provider, +unsigned int region_index, +struct phys_vec *phys_vec, +struct vfio_region_dma_range *dma_ranges, +size_t nr_ranges) +{ +struct vfio_cxl_state *cxl = vdev->cxl; + +/* Real BARs go through the core P2P exporter unchanged. */ +if (region_index <VFIO_PCI_NUM_REGIONS ) +return vfio_pci_core_get_dmabuf_phys (vdev, provider, + region_index, phys_vec, + dma_ranges, nr_ranges); + +/* Of the device regions, only the HDM memory window is exportable. */ +if (region_index != cxl->mem_region_index) +return -EINVAL ; + +/* +* The HDM window is coherent host memory, not BAR MMIO, so it has no +* p2pdma provider of its own. Borrow BAR 0's: the P2P properties match +* and the iommufd importer does not consume the provider. The sgt map +* path (real peer DMA) is not supported for the HDM window. +*/ + *provider =pcim_p2pdma_provider (vdev->pdev,0 ); +if (!*provider) +return -EINVAL ; + +return vfio_pci_core_fill_phys_vec (phys_vec, dma_ranges, nr_ranges, + cxl->hpa_range.start, +range_len (&cxl->hpa_range)); +} + +static const struct vfio_pci_device_ops vfio_cxl_pci_dev_ops = { + .get_dmabuf_phys = vfio_cxl_get_dmabuf_phys, +}; +static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;
@@ -483,6 +530,19 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)if (ret)return ret; +/* Record where the HDM memory region landed for the dma-buf export. */ + cxl->mem_region_index =VFIO_PCI_NUM_REGIONS + vdev->num_regions -1 ; + +/* +* Override the device ops so a dma-buf export of the HDM memory region +* resolves to the coherent host range. This is done at open, not init: +* vfio_pci_probe() resets pci_ops after vfio_alloc_device() returns, so +* an override installed during init would be clobbered. Only a CXL device +* reaches this hook (cxl_ops is set on init success), so a fallback to +* plain vfio-pci keeps the core ops. +*/ + vdev->pci_ops = &vfio_cxl_pci_dev_ops; + ret = vfio_cxl_add_region(vdev, VFIO_REGION_SUBTYPE_CXL_COMP_REGS, &vfio_cxl_comp_regops, cxl->hdm_len, VFIO_REGION_INFO_FLAG_READ |
drivers/vfio/pci/vfio_pci_dmabuf.c +23 -4
@@ -178,6 +178,15 @@ int vfio_pci_core_get_dmabuf_phys(struct vfio_pci_core_device *vdev, {struct pci_dev *pdev = vdev->pdev; +/* +* This resolver only handles PCI BARs. A device-specific region index +* (>= PCI_STD_NUM_BARS) would index pdev->resource[] out of bounds via +* pcim_p2pdma_provider(), so reject it; a driver that exports such a +* region installs its own get_dmabuf_phys. +*/ +if (region_index >=PCI_STD_NUM_BARS ) +return -EINVAL ; + *provider =pcim_p2pdma_provider (pdev, region_index);if (!*provider)return -EINVAL ;
@@ -227,6 +236,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,DEFINE_DMA_BUF_EXPORT_INFO (exp_info);struct vfio_pci_dma_buf *priv;size_t length; +u32 index;int ret;if (!vdev->pci_ops || !vdev->pci_ops->get_dmabuf_phys)
@@ -243,13 +253,22 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,if (!get_dma_buf.nr_ranges || get_dma_buf.flags)return -EINVAL ; + index = get_dma_buf.region_index; +/* -* For PCI the region_index is the BAR number like everything -* else. Check that PCI resources have been claimed for it. +* A fixed region index is the BAR number; only a BAR can be exported +* and its PCI resource must be claimed. A device-specific region (index +* >= VFIO_PCI_NUM_REGIONS) has no BAR resource and is validated by the +* device's get_dmabuf_phys instead, but the index must name a region +* that exists. */ -if (get_dma_buf.region_index >=VFIO_PCI_ROM_REGION_INDEX || -IS_ERR (vfio_pci_core_get_iomap (vdev, get_dma_buf.region_index))) +if (index <VFIO_PCI_NUM_REGIONS ) { +if (index >=VFIO_PCI_ROM_REGION_INDEX || +IS_ERR (vfio_pci_core_get_iomap (vdev, index))) +return -ENODEV ; + }else if (index -VFIO_PCI_NUM_REGIONS >= vdev->num_regions) {return -ENODEV ; + } dma_ranges =memdup_array_user (&arg->dma_ranges, get_dma_buf.nr_ranges,sizeof (*dma_ranges));
Signature · 1 line
2.25.1
PATCH v5 25/27 vfio/cxl: Run the CXL reset at the vfio reset points
In reply to [email protected]From: Manish Honap <[email protected]>
A CXL Type-2 function must not take an FLR: it resets the coherent CXL.mem state and corrupts the HDM decoder. The PCI core already reflects this, ordering cxl_reset ahead of flr in pci_reset_fn_methods[], so a function reset of a CXL device runs the DVSEC reset sequence rather than FLR.
Route the vfio function-reset points (VFIO_DEVICE_RESET and the virtualized PCIe/AF FLR writes) through a CXL reset op that runs cxl_reset_dvsec_sequence(). The sequence resets the function, always clearing device memory, and restores the HDM decoder and PCI config state, so it is a complete replacement for pci_try_reset_function() on a CXL device. The op runs under memory_lock and not the PCI device lock, so cxl_reset_dvsec_sequence() can take the device lock itself.
Clear hdm_valid for the duration of the reset so a fault cannot insert a PFN into a decoder that is being torn down, and restore it once the sequence has put the decoder back.
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 41 +++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 49 +++++++++++++++--- drivers/vfio/pci/vfio_pci_core.c | 77 +++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_priv.h | 1 + include/linux/vfio_pci_core.h | 4 ++ 5 files changed, 152 insertions(+), 20 deletions(-)
drivers/vfio/pci/cxl/vfio_cxl_core.c +41 -0
@@ -632,6 +632,45 @@ static void vfio_cxl_reset_done(struct vfio_pci_core_device *vdev) cxl->hdm_valid = false; } +/* +* Run the CXL DVSEC reset sequence in place of a PCI function reset. A CXL +* Type-2 function must not take an FLR (it would corrupt CXL.mem), so the vfio +* reset points route here. The sequence resets the function, always clearing +* device memory, and restores the HDM decoder. The caller holds memory_lock, +* and this path does not hold the PCI device lock, so cxl_reset_dvsec_sequence() +* can take it. +*/ +static int vfio_cxl_reset (struct vfio_pci_core_device *vdev) +{ +struct vfio_cxl_state *cxl = vdev->cxl; +int ret; + +lockdep_assert_held_write (&vdev->memory_lock); + +/* Host CPU access to the HDM range is unsafe until the decoder is back. */ + cxl->hdm_valid = false; + + ret =cxl_reset_dvsec_sequence (vdev->pdev); +if (!ret) + cxl->hdm_valid = true; + +return ret; +} + +/* +* The HDM dma-buf may be armed only while the decoder is valid. After a failed +* reset hdm_valid is clear, so the generic memory-enable re-arm must skip the +* dma-buf rather than map DMA onto an unrestored decoder. +*/ +static bool vfio_cxl_hdm_active (struct vfio_pci_core_device *vdev) +{ +struct vfio_cxl_state *cxl = vdev->cxl; + +lockdep_assert_held_write (&vdev->memory_lock); + +return cxl->hdm_valid; +} +static const struct vfio_cxl_ops vfio_cxl_ops = { .init = vfio_cxl_init_device, .release = vfio_cxl_release_device,
@@ -639,6 +678,8 @@ static const struct vfio_cxl_ops vfio_cxl_ops = { .close_device = vfio_cxl_close_device, .reset_prepare = vfio_cxl_reset_prepare, .reset_done = vfio_cxl_reset_done, + .reset = vfio_cxl_reset, + .hdm_active = vfio_cxl_hdm_active, .owner = THIS_MODULE, };
drivers/vfio/pci/vfio_pci_config.c +41 -8
80 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_core.c +65 -12
103 lines not on this page · raw message
drivers/vfio/pci/vfio_pci_priv.h +1 -0
@@ -78,6 +78,7 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t state);void vfio_pci_zap_and_down_write_memory_lock (struct vfio_pci_core_device *vdev); +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev);u16 vfio_pci_memory_lock_and_enable (struct vfio_pci_core_device *vdev);void vfio_pci_memory_unlock_and_restore (struct vfio_pci_core_device *vdev,u16 cmd);
include/linux/vfio_pci_core.h +4 -0
@@ -74,6 +74,10 @@ struct vfio_cxl_ops {void (*close_device)(struct vfio_pci_core_device *vdev);void (*reset_prepare)(struct vfio_pci_core_device *vdev);void (*reset_done)(struct vfio_pci_core_device *vdev); +/* Run the CXL reset (always clears CXL.mem) in place of FLR */ +int (*reset)(struct vfio_pci_core_device *vdev); +/* True while the HDM range is valid and its dma-buf may be armed */ +bool (*hdm_active)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ struct module *owner; };
Signature · 1 line
2.25.1
PATCH v5 26/27 Documentation: vfio-pci: Document CXL Type-2 device passthrough
In reply to [email protected]From: Manish Honap <[email protected]>
Describe the vfio-cxl provider module:
- Address model,
- Two regions,
- Live decoder region and the guest commit,
- DVSEC virtualization,
- The dma-buf export for iommufd stage-2 mapping,
- The reset contract
so the supported topology and the kernel/VMM split are on record.
--- Documentation/driver-api/index.rst | 1 + Documentation/driver-api/vfio-pci-cxl.rst | 188 ++++++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 190 insertions(+) create mode 100644 Documentation/driver-api/vfio-pci-cxl.rst
Documentation/driver-api/index.rst +1 -0
@@ -46,6 +46,7 @@ of interest to most developers working on device drivers. uio-howto vfio-mediated-device vfio + vfio-pci-cxl vfio-pci-device-specific-driver-acceptance Bus-level documentation
Documentation/driver-api/vfio-pci-cxl.rst new file +188 -0
188 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -28644,6 +28644,7 @@ M: Manish Honap <[email protected]> L: [email protected] L: [email protected] S: Supported +F: Documentation/driver-api/vfio-pci-cxl.rst F: drivers/vfio/pci/cxl/ VFIO DRIVER
Signature · 1 line
2.25.1
PATCH v5 27/27 selftests/vfio: Add CXL Type-2 passthrough tests
In reply to [email protected]From: Manish Honap <[email protected]>
Exercise the vfio-cxl contract on a bound Type-2 device: the CXL device-info flag and the two vendor-type regions, the HDM memory mmap (including a 2 MB huge fault) and its survival across a Memory Space toggle, the read-only trapped decoder region against the live host-committed decoder, and the guest's DVSEC reset doorbell and manual HDM discovery.
Map the HDM memory the way a VMM does: export it as a dma-buf with VFIO_DEVICE_FEATURE_DMA_BUF and map the fd into the IOAS with IOMMU_IOAS_MAP_FILE. The struct-page-less range cannot be pinned through a userspace VA, so the by-fd path is the one that works. Skip when the kernel has no CXL dma-buf exporter, so the test becomes a real pass once that support is present.
Add a partial-mmap path to the vfio selftest library so the trapped component BAR maps its sparse ranges, and an __iommu_map_file() helper for the dma-buf mapping.
--- MAINTAINERS | 1 + tools/testing/selftests/vfio/Makefile | 1 + .../vfio/lib/include/libvfio/iommu.h | 3 + tools/testing/selftests/vfio/lib/iommu.c | 29 + .../selftests/vfio/lib/vfio_pci_device.c | 57 +- .../selftests/vfio/vfio_cxl_type2_test.c | 780 ++++++++++++++++++ 6 files changed, 868 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/vfio/vfio_cxl_type2_test.c
MAINTAINERS +1 -0
@@ -28646,6 +28646,7 @@ L: [email protected] S: Supported F: Documentation/driver-api/vfio-pci-cxl.rst F: drivers/vfio/pci/cxl/ +F: tools/testing/selftests/vfio/vfio_cxl_type2_test.c VFIO DRIVER M: Alex Williamson <[email protected]>
tools/testing/selftests/vfio/Makefile +1 -0
@@ -13,6 +13,7 @@ TEST_GEN_PROGS += vfio_pci_device_test TEST_GEN_PROGS += vfio_pci_device_init_perf_test TEST_GEN_PROGS += vfio_pci_driver_test TEST_GEN_PROGS += vfio_pci_sriov_uapi_test +TEST_GEN_PROGS += vfio_cxl_type2_test TEST_FILES += scripts/cleanup.sh TEST_FILES += scripts/lib.sh
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +3 -0
@@ -42,6 +42,9 @@ static inline void iommu_map(struct iommu *iommu, struct dma_region *region)VFIO_ASSERT_EQ (__iommu_map (iommu, region),0 ); } +int __iommu_map_file (struct iommu *iommu,int fd,u64 start,u64 length, +iova_t iova); +int __iommu_unmap (struct iommu *iommu,struct dma_region *region,u64 *unmapped);static inline void iommu_unmap (struct iommu *iommu,struct dma_region *region)
tools/testing/selftests/vfio/lib/iommu.c +29 -0
@@ -149,6 +149,35 @@ int __iommu_map(struct iommu *iommu, struct dma_region *region)return 0 ; } +/* +* Map a range of a file (a memfd or a supported dma-buf, such as a VFIO PCI +* dma-buf from VFIO_DEVICE_FEATURE_DMA_BUF) into the IOAS by fd. This is an +* iommufd-only ioctl; the legacy VFIO container has no equivalent. +*/ +int __iommu_map_file (struct iommu *iommu,int fd,u64 start,u64 length, +iova_t iova) +{ +struct iommu_ioas_map_file args = { + .size =sizeof (args), + .flags =IOMMU_IOAS_MAP_READABLE | +IOMMU_IOAS_MAP_WRITEABLE | +IOMMU_IOAS_MAP_FIXED_IOVA , + .ioas_id = iommu->ioas_id, + .fd = fd, + .start = start, + .length = length, + .iova = iova, + }; + +if (!iommu->iommufd) +return -EINVAL ; + +if (ioctl (iommu->iommufd,IOMMU_IOAS_MAP_FILE , &args)) +return -errno; + +return 0 ; +} +static int __vfio_iommu_unmap (int fd,u64 iova,u64 size,u32 flags,u64 *unmapped) {struct vfio_iommu_type1_dma_unmap args = {
tools/testing/selftests/vfio/lib/vfio_pci_device.c +54 -3
72 lines not on this page · raw message
tools/testing/selftests/vfio/vfio_cxl_type2_test.c new file +780 -0
780 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v5 26/27] Documentation: vfio-pci: Document CXL Type-2 device passthrough
In reply to [email protected]Quoted 2 lines · On Thu, Sep 17, 2026 at 12:05:39AM +0530, [email protected] wrote:
From: Manish Honap <[email protected]>
1) Thank you so much for writing documentation, i truly appreciate this.
2) I apologize in advance for my terseness, I know writing is hard, please do not interpret this as disliking your writing or series.
Quoted 5 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +5
-0+Address model +============= + +The HDM memory is a coherent host physical range (HPA). The host kernel +resolves that range before the guest sees the device, and owns it for the
Documentation/driver-api/vfio-pci-cxl.rst line 33
Quoted 10 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +10
-0+bind lifetime. The guest only chooses where the memory appears in its own +physical address space (GPA), by programming a virtual endpoint HDM +decoder. The guest never reprograms the physical decoder. + +The kernel holds the HPA and does not see the GPA. The guest programs a +GPA and does not see the HPA. The VMM holds the device fd, reads the +committed base from the decoder-register region described below, and maps +the HPA-backed HDM region at the GPA the guest committed. The base the +guest reads back is the GPA, not the HPA. +
I think this must be slightly inaccurate / imprecise wording.
The host *must* provide some form of physical memory window to the guest at initialization time, otherwise the guest has no way to know - at boot time - that there's even a window of memory it can use.
That's what the CFMWS is. This is initialized by the hypervisor - which is controlled by the host.
So the host (at least the VMM) must know, for the region the entire device *could* inhabit, what that GPA is - because it's the one that makes the CFMWS for the guest.
If this is not the case, then something is missing from this documentation to explain why.
If you're actually trying to say is that the GPA's programmed into the virtual decoders are largely symbolic - this at best feels a bit inaccurate and simply an implementation detail.
The host's virtio device could enforce ....:
Host Range:
CFMWS HPA - [0x10000, 0x20000]
| | Guest Range: | |
CFMWS GPA - [0x50000, 0x60000]
In that case, you'd get the following translation...
vdecoder0.0 - [0x58000, 0x60000]
CFMWS GPA - [0x58000, 0x60000]
CFMWS HPA - [0x18000, 0x20000]
Or the virtio device could not enforce that and let the host page-fault just hand it a random page from the actual CXL device.
vdecoder0.0 - [0x58000, 0x60000]
CFMWS GPA - [0x58000, 0x60000]
|
No discrete host mapping
The former makes sense if the device (accelerator) requires exact physical placement to do its accelerator nonsense.
The latter makes sense if the device (accelerator) doesn't care about placement (compressed memory).
This is not saying we need support both out of the box, but we shouldn't lock ourselves into the former unless there's some reason why the latter is not reasonable.
Can you please help document what the actual expected behavior is with examples in the Address model section so it's easier to understand the intent? That will help quite a bit.
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 7 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +7
-0+Guest decoder and commit +======================== + +The guest programs its virtual endpoint decoder through the trapped +region: it writes a base (a GPA), a size, and then the COMMIT bit. The +host already resolved and committed the physical placement before the +guest ran,
So the host does know GPA, just not exact placement.
Documentation/driver-api/vfio-pci-cxl.rst line 92
Quoted 4 lines
so a live read of the decoder always shows COMMITTED and the
Documentation/driver-api/vfio-pci-cxl.rst new file +3
-0+guest's commit poll completes. The physical decoder is never rewritten; +the guest's writes are absorbed. +
Rather clunky, round-about way to say "The guest decoders are
virtualized". If possible, it would be nice to formalize this concept
into "Virtual Decoders" - since that's what this is.
With that concept i think you can probably generate some nice diagrams that show how the guest vdecoder's interact with the host drivers.
Documentation/driver-api/vfio-pci-cxl.rst line 95
Quoted 3 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +3
-0+The VMM observes the commit, reads the committed base, and maps the HDM +region at that GPA. +
So the host does know the GPA.
Documentation/driver-api/vfio-pci-cxl.rst line 103
Quoted 8 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +8
-0+CXL Device DVSEC +================ + +The kernel virtualizes the CXL Device DVSEC body through the config-space +permission hooks. Reads and writes inside the DVSEC body use a per-open +shadow; a guest write stays in the shadow and does not reach hardware. +Accesses outside the DVSEC body go to the device as usual. +
"The kernel" - what part? vfio-pci ? the vmm ?
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 6 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +6
-0+DMA and iommufd +=============== + +A Type-2 accelerator issues ATS-translated DMA to addresses inside its own +HDM window, so that range must be present in the guest IOAS that backs the +nested stage-2 translation.
Type-2, ATS, DMA, HDM window, guest IOAS, stage-2 translation
I think the only thing i don't know in this sentence is "guest IOAS" and it's still hurting my brain to read.
Are all accelerators expect to have this particular interaction, or just yours?
Review
Quoted 3 lines
The HDM range is struct-page-less coherent
(quoted diff) +2
-0+memory, which a userspace-VA ``IOMMU_IOAS_MAP`` cannot pin. +
The hardest part about writing about virtualization is keeping a consistent mental model from section to section.
which userspace? guest? host? (i presume guest here)
struct-page-less coherent memory
e.g. the host never hotplugs this, it hands the entire region directly to the VFIO device, right?
I think this would be nice to spell out somewhere.
Documentation/driver-api/vfio-pci-cxl.rst line 124
Quoted 7 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +7
-0+The HDM memory region is therefore exportable as a dma-buf: +``VFIO_DEVICE_FEATURE_DMA_BUF`` on that region returns an fd that iommufd +maps with ``IOMMU_IOAS_MAP_FILE``, mapping the physical range without a VA +or a page pin. The dma-buf is revoked whenever the mapping is torn down +(reset, power transition, teardown), so a stale stage-2 mapping cannot +outlive the HDM window. +
For the sake of readers, I think either a little bit more information on this "stage-2 mapping" concept is needed to make sense of what's going on here and why it mustn't outlive the HDM window.
Documentation/driver-api/vfio-pci-cxl.rst line 133
Quoted 9 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +9
-0+Reset +===== + +A CXL Type-2 function must not take a Function Level Reset: an FLR resets +the coherent CXL.mem state and the HDM decoder. The PCI core reflects this +by preferring the CXL reset over FLR, so a function reset of a CXL device +runs the CXL DVSEC reset sequence, which resets the function and then +restores the HDM decoder and the PCI config state. +
I think what you're trying to say is that FLRs are never passed to the device because it can cause physical device effects that defeat the purpose of the virtualization, yes?
So we virtualize FLRs...
Documentation/driver-api/vfio-pci-cxl.rst line 136
Quoted 3 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +3
-0+A guest requests a reset by writing Initiate CXL Reset in the DVSEC. That +write only stamps completion in the shadow. The real reset runs at the vfio +reset points (the reset ioctl and a virtualized FLR through config space):
As you describe here.
So it's not that an accelerator "must not take an FLR" - it's that FLRs are virtualized to prevent deleterious effects on the host / hardware.
Am I misunderstanding this?
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 13 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +13
-0+the kernel zaps the HDM mapping and revokes the dma-buf, then runs the CXL +reset, which always clears the device memory, and restores and re-samples +the decoder afterwards. A CXL port masks Secondary Bus Reset by default, so a +``VFIO_DEVICE_PCI_HOT_RESET`` does not reach the endpoint and the HDM +state is untouched. If the port has SBR unmasked the reset can decommit +the decoder without restoring it, so the reset_done handler gates HDM +access; a ``VFIO_DEVICE_RESET`` then runs the CXL reset sequence and +restores it. + +The decoder register region is served by live reads of the hardware +decoder with guest writes absorbed: the decoder is committed and locked by +the host, so a guest can neither decommit nor reprogram it, and the kernel +keeps no shadow of the decoder state.
This is basically what I said at the beginning - it must either be that the host provides locked auto-decoders at boot, or it must provide proper virtualization so that the decoders settings are fully virtualized.
Seems it's the former, and that makes sense. Please correct me if i'm misunderstanding.
Documentation/driver-api/vfio-pci-cxl.rst line 154
Quoted 6 lines
After a reset the kernel restores and
Documentation/driver-api/vfio-pci-cxl.rst new file +5
-0+re-samples the firmware-committed decoder, so the geometry the guest reads +back is unchanged. A VMM that dropped its HDM mapping, for example across a +reset or a D3hot->D0 transition, must rescan the decoder and rebuild its +stage-2 mapping before it resumes HDM access. +
Yeah i think we need a bit more information about this stage-2 mapping rebuild to make sense of this. Maybe I'm just not read-up enough on this particular setup - is there another part of the docs you can link to that talk about this, or are you able to share some details as to what this rebuild process looks like?
~Gregory
Re: [PATCH v5 21/27] vfio/cxl: Exclude the HDM decoder registers from direct BAR access
In reply to [email protected]Quoted 8 lines · On Thu, Sep 17, 2026 at 12:05:34AM +0800, [email protected] wrote:
From: Manish Honap <[email protected]>
The HDM decoder register block lives in the component BAR that vfio-pci owns. The guest reads the decoder only through the trapped, read-only decoder region; it must not reach the physical registers through the BAR directly, and a host read of the range through a kernel mapping could abort as a fatal host SError.
Quoted 4 lines
Add the block to the generic excluded-range list with
VFIO_PCI_EXCLUDE_MMAP|VFIO_PCI_EXCLUDE_READ|VFIO_PCI_EXCLUDE_WRITEsimilar to the MSI-X exclusion.
Hi Manish,
I think we need to block eventfd write and BAR DMA as well ?
Have some comments below.
Quoted 20 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +4
-0@@ -435,8 +435,25 @@ static int vfio_cxl_add_region(struct vfio_pci_core_device *vdev, u32 subtype,static int vfio_cxl_open_device (struct vfio_pci_core_device *vdev) {struct vfio_cxl_state *cxl = vdev->cxl; +struct pci_dev *pdev = vdev->pdev;int ret; +/* +* Keep the HDM decoder block out of the guest's direct BAR access: the +* guest reaches it only through the trapped decoder region, and a host
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_add_region(struct vfio_pci_core_device *vdev, u32 subtype, · line 453
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+* read of the range through a kernel mapping could abort as an SError. +* Exclude it from mmap, fill reads with -1 and drop writes. The list is +* cleared on close, so re-add it per open. +*/ + ret =vfio_pci_core_add_excluded_range (vdev, pdev->hdm->hdm_bar, + pdev->hdm->hdm_offset, + cxl->hdm_len, +VFIO_PCI_EXCLUDE_MMAP | +VFIO_PCI_EXCLUDE_READ | +VFIO_PCI_EXCLUDE_WRITE );
This adds the HDM reg to the exclusion list, but the eventfd and BAR DMA path doesn't check that.
Maybe we need to block those 2 path as well ?
Best regards,
Richard Cheng.
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0+if (ret) +return ret; +/* * vfio_pci_core_disable() frees all dynamic regions on close, so register * them here per open rather than at bind. A failed first open never Signature · 1 line
2.25.1
Re: [PATCH v5 24/27] vfio/cxl: Export the HDM memory region as a dma-buf
In reply to [email protected]Quoted 91 lines · On Thu, Sep 17, 2026 at 12:05:37AM +0800, [email protected] wrote:
From: Manish Honap <[email protected]>
A Type-2 accelerator issues ATS-translated DMA to addresses inside its own HDM window, so that coherent host range must be present in the guest's IOAS (the iommufd IOAS backing the nested SMMU stage-2). iommufd maps a struct-page-less range only by fd, via
IOMMU_IOAS_MAP_FILEover a dma-buf; a userspace-VAIOMMU_IOAS_MAPof the HDM mmap is rejected because the VMA isVM_IO|VM_PFNMAP. Without a dma-buf the range could only be mapped through an out-of-tree PFNMAP work-around.vfio-pci already exports BAR memory as a P2P dma-buf, but the exporter is BAR-only:
vfio_pci_core_feature_dma_buf()rejects any region index at or above the ROM index, andvfio_pci_core_get_dmabuf_phys()resolves the physical range from a PCI BAR. The HDM memory region is a dynamic device-specific region, not a BAR.Let a device-specific region reach the device's
get_dmabuf_phys(): a region index at or aboveVFIO_PCI_NUM_REGIONSskips the BAR-resource check and is validated by the driver instead, bounded to the regions that exist. Install a CXL-awareget_dmabuf_phys()in the vfio-cxl provider that returnscxl->hpa_rangefor the HDM memory region and delegates real BARs to the core, keeping the BAR path unchanged and the core free of CXL knowledge.The HDM window is coherent host memory with no p2pdma provider of its own, so borrow BAR 0's, matching nvgrace-gpu's handling of its non-BAR device memory. The iommufd importer does not consume the provider; the scatterlist map path (real peer DMA) is left to a follow-up once upstream grows a negotiated interconnect for coherent CXL memory.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 60 ++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_dmabuf.c | 27 +++++++++++-- 2 files changed, 83 insertions(+), 4 deletions(-)drivers/vfio/pci/cxl/vfio_cxl_core.c +25
-0@@ -11,6 +11,7 @@#include <linux/mm.h> #include <linux/module.h> #include <linux/pci.h> +#include <linux/pci-p2pdma.h> #include <linux/range.h> #include <linux/slab.h> #include <linux/uaccess.h> @@ -27,6 +28,7 @@* @hdm_regs: mapped HDM decoder registers, read live by the decoder region * @hdm_len: length of the HDM decoder register block * @hdm_valid: true when host CPU access to the HDM range is safe; under memory_lock +* @mem_region_index: vfio region index of the mmap-able HDM memory region */ struct vfio_cxl_state {struct cxl_dev_state cxlds;@@ -36,6 +38,7 @@ struct vfio_cxl_state {void __iomem *hdm_regs;u32 hdm_len;bool hdm_valid; +unsigned int mem_region_index; }; static unsigned long vfio_cxl_mem_pgoff(struct vm_area_struct *vma,@@ -289,6 +292,50 @@ static void vfio_cxl_release_hpa(void *data)release_mem_region (cxl->hpa_range.start,range_len (&cxl->hpa_range)); } +/* +* Resolve the physical range that backs a dma-buf export. The core exporter +* only knows BARs; teach it the HDM memory region so a guest IOAS can map the +* coherent window by fd (IOMMU_IOAS_MAP_FILE) instead of the removed PFNMAP +* work-around. Real BARs stay on the byte-identical core path. +*/ +static int vfio_cxl_get_dmabuf_phys (struct vfio_pci_core_device *vdev, +struct p2pdma_provider **provider, +unsigned int region_index, +struct phys_vec *phys_vec, +struct vfio_region_dma_range *dma_ranges, +size_t nr_ranges) +{ +struct vfio_cxl_state *cxl = vdev->cxl; + +/* Real BARs go through the core P2P exporter unchanged. */ +if (region_index <VFIO_PCI_NUM_REGIONS ) +return vfio_pci_core_get_dmabuf_phys (vdev, provider, + region_index, phys_vec, + dma_ranges, nr_ranges); + +/* Of the device regions, only the HDM memory window is exportable. */
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data) · line 326
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+if (region_index != cxl->mem_region_index) +return -EINVAL ; + +/* +* The HDM window is coherent host memory, not BAR MMIO, so it has no +* p2pdma provider of its own. Borrow BAR 0's: the P2P properties match +* and the iommufd importer does not consume the provider. The sgt map +* path (real peer DMA) is not supported for the HDM window. +*/ + *provider =pcim_p2pdma_provider (vdev->pdev,0 );
I think of a weird scenario which might make peer DMA work, not sure if that's possible.
userspace can give this dma-buf fd to an NIC driver or so to do RDMA ? use HDM memory as network buffer ?
If that's the case and direct P2P addressing is selected, it applies BAR 0's bus offset to HDM physical address.
Maybe explicitly reject peer-DMA mapping would be safer ?
Best regards,
Richard Cheng.
Quoted 94 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +25
-0+if (!*provider) +return -EINVAL ; + +return vfio_pci_core_fill_phys_vec (phys_vec, dma_ranges, nr_ranges, + cxl->hpa_range.start, +range_len (&cxl->hpa_range)); +} + +static const struct vfio_pci_device_ops vfio_cxl_pci_dev_ops = { + .get_dmabuf_phys = vfio_cxl_get_dmabuf_phys, +}; +static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;@@ -483,6 +530,19 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)if (ret)return ret; +/* Record where the HDM memory region landed for the dma-buf export. */ + cxl->mem_region_index =VFIO_PCI_NUM_REGIONS + vdev->num_regions -1 ; + +/* +* Override the device ops so a dma-buf export of the HDM memory region +* resolves to the coherent host range. This is done at open, not init: +* vfio_pci_probe() resets pci_ops after vfio_alloc_device() returns, so +* an override installed during init would be clobbered. Only a CXL device +* reaches this hook (cxl_ops is set on init success), so a fallback to +* plain vfio-pci keeps the core ops. +*/ + vdev->pci_ops = &vfio_cxl_pci_dev_ops; + ret = vfio_cxl_add_region(vdev, VFIO_REGION_SUBTYPE_CXL_COMP_REGS, &vfio_cxl_comp_regops, cxl->hdm_len, VFIO_REGION_INFO_FLAG_READ |drivers/vfio/pci/vfio_pci_dmabuf.c +23
-4@@ -178,6 +178,15 @@ int vfio_pci_core_get_dmabuf_phys(struct vfio_pci_core_device *vdev, {struct pci_dev *pdev = vdev->pdev; +/* +* This resolver only handles PCI BARs. A device-specific region index +* (>= PCI_STD_NUM_BARS) would index pdev->resource[] out of bounds via +* pcim_p2pdma_provider(), so reject it; a driver that exports such a +* region installs its own get_dmabuf_phys. +*/ +if (region_index >=PCI_STD_NUM_BARS ) +return -EINVAL ; + *provider =pcim_p2pdma_provider (pdev, region_index);if (!*provider)return -EINVAL ;@@ -227,6 +236,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,DEFINE_DMA_BUF_EXPORT_INFO (exp_info);struct vfio_pci_dma_buf *priv;size_t length; +u32 index;int ret;if (!vdev->pci_ops || !vdev->pci_ops->get_dmabuf_phys)@@ -243,13 +253,22 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,if (!get_dma_buf.nr_ranges || get_dma_buf.flags)return -EINVAL ; + index = get_dma_buf.region_index; +/* -* For PCI the region_index is the BAR number like everything -* else. Check that PCI resources have been claimed for it. +* A fixed region index is the BAR number; only a BAR can be exported +* and its PCI resource must be claimed. A device-specific region (index +* >= VFIO_PCI_NUM_REGIONS) has no BAR resource and is validated by the +* device's get_dmabuf_phys instead, but the index must name a region +* that exists. */ -if (get_dma_buf.region_index >=VFIO_PCI_ROM_REGION_INDEX || -IS_ERR (vfio_pci_core_get_iomap (vdev, get_dma_buf.region_index))) +if (index <VFIO_PCI_NUM_REGIONS ) { +if (index >=VFIO_PCI_ROM_REGION_INDEX || +IS_ERR (vfio_pci_core_get_iomap (vdev, index))) +return -ENODEV ; + }else if (index -VFIO_PCI_NUM_REGIONS >= vdev->num_regions) {return -ENODEV ; + } dma_ranges =memdup_array_user (&arg->dma_ranges, get_dma_buf.nr_ranges,sizeof (*dma_ranges));Signature · 1 line
2.25.1
Re: [PATCH v5 25/27] vfio/cxl: Run the CXL reset at the vfio reset points
In reply to [email protected]Quoted 186 lines · On Thu, Sep 17, 2026 at 12:05:38AM +0800, [email protected] wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 function must not take an FLR: it resets the coherent CXL.mem state and corrupts the HDM decoder. The PCI core already reflects this, ordering
cxl_resetahead of flr in pci_reset_fn_methods[], so a function reset of a CXL device runs the DVSEC reset sequence rather than FLR.Route the vfio function-reset points (
VFIO_DEVICE_RESETand the virtualized PCIe/AF FLR writes) through a CXL reset op that runscxl_reset_dvsec_sequence(). The sequence resets the function, always clearing device memory, and restores the HDM decoder and PCI config state, so it is a complete replacement forpci_try_reset_function()on a CXL device. The op runs undermemory_lockand not the PCI device lock, socxl_reset_dvsec_sequence()can take the device lock itself.Clear
hdm_validfor the duration of the reset so a fault cannot insert a PFN into a decoder that is being torn down, and restore it once the sequence has put the decoder back.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 41 +++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 49 +++++++++++++++--- drivers/vfio/pci/vfio_pci_core.c | 77 +++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_priv.h | 1 + include/linux/vfio_pci_core.h | 4 ++ 5 files changed, 152 insertions(+), 20 deletions(-)drivers/vfio/pci/cxl/vfio_cxl_core.c +41
-0@@ -632,6 +632,45 @@ static void vfio_cxl_reset_done(struct vfio_pci_core_device *vdev) cxl->hdm_valid = false; } +/* +* Run the CXL DVSEC reset sequence in place of a PCI function reset. A CXL +* Type-2 function must not take an FLR (it would corrupt CXL.mem), so the vfio +* reset points route here. The sequence resets the function, always clearing +* device memory, and restores the HDM decoder. The caller holds memory_lock, +* and this path does not hold the PCI device lock, so cxl_reset_dvsec_sequence() +* can take it. +*/ +static int vfio_cxl_reset (struct vfio_pci_core_device *vdev) +{ +struct vfio_cxl_state *cxl = vdev->cxl; +int ret; + +lockdep_assert_held_write (&vdev->memory_lock); + +/* Host CPU access to the HDM range is unsafe until the decoder is back. */ + cxl->hdm_valid = false; + + ret =cxl_reset_dvsec_sequence (vdev->pdev); +if (!ret) + cxl->hdm_valid = true; + +return ret; +} + +/* +* The HDM dma-buf may be armed only while the decoder is valid. After a failed +* reset hdm_valid is clear, so the generic memory-enable re-arm must skip the +* dma-buf rather than map DMA onto an unrestored decoder. +*/ +static bool vfio_cxl_hdm_active (struct vfio_pci_core_device *vdev) +{ +struct vfio_cxl_state *cxl = vdev->cxl; + +lockdep_assert_held_write (&vdev->memory_lock); + +return cxl->hdm_valid; +} +static const struct vfio_cxl_ops vfio_cxl_ops = { .init = vfio_cxl_init_device, .release = vfio_cxl_release_device,@@ -639,6 +678,8 @@ static const struct vfio_cxl_ops vfio_cxl_ops = { .close_device = vfio_cxl_close_device, .reset_prepare = vfio_cxl_reset_prepare, .reset_done = vfio_cxl_reset_done, + .reset = vfio_cxl_reset, + .hdm_active = vfio_cxl_hdm_active, .owner = THIS_MODULE, };drivers/vfio/pci/vfio_pci_config.c +41
-8@@ -630,7 +630,14 @@ static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos, *virt_cmd &=cpu_to_le16 (~mask); *virt_cmd |=cpu_to_le16 (new_cmd & mask); -if (__vfio_pci_memory_enabled (vdev)) +/* +* Re-arm the dma-bufs on memory-enable, but keep a CXL device's +* HDM dma-buf revoked while the decoder is unrestored (a failed +* reset leaves hdm_valid clear); re-arming would map DMA onto a +* decoder the fault path still gates. Plain vfio-pci is unchanged. +*/ +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || vdev->cxl_ops->hdm_active (vdev)))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -720,7 +727,8 @@ static void vfio_lock_and_set_power_state(struct vfio_pci_core_device *vdev, }vfio_pci_set_power_state (vdev, state); -if (__vfio_pci_memory_enabled (vdev)) +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || vdev->cxl_ops->hdm_active (vdev)))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -910,8 +918,14 @@ static int vfio_exp_config_write(struct vfio_pci_core_device *vdev, int pos,if (!ret && (cap &PCI_EXP_DEVCAP_FLR )) {vfio_pci_zap_and_down_write_memory_lock (vdev);vfio_pci_dma_buf_move (vdev, true); -pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Keep the HDM dma-buf revoked if a CXL reset +* failed; re-arming would map DMA onto an +* unrestored decoder. Mirrors the reset ioctl. +*/ +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -995,8 +1009,14 @@ static int vfio_af_config_write(struct vfio_pci_core_device *vdev, int pos,if (!ret && (cap &PCI_AF_CAP_FLR ) && (cap &PCI_AF_CAP_TP )) {vfio_pci_zap_and_down_write_memory_lock (vdev);vfio_pci_dma_buf_move (vdev, true); -pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Keep the HDM dma-buf revoked if a CXL reset +* failed; re-arming would map DMA onto an +* unrestored decoder. Mirrors the reset ioctl. +*/ +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -1781,9 +1801,22 @@ static int vfio_cxl_dvsec_write(struct vfio_pci_core_device *vdev, int pos, status2 |=PCI_DVSEC_CXL_CACHE_INV ; }if (ctrl2 &PCI_DVSEC_CXL_INIT_CXL_RST ) { +int ret =0 ; + ctrl2 &= ~PCI_DVSEC_CXL_INIT_CXL_RST ; - status2 &= ~PCI_DVSEC_CXL_RST_ERR ; - status2 |=PCI_DVSEC_CXL_RST_DONE ; + +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +vfio_pci_zap_and_down_write_memory_lock (vdev); +vfio_pci_dma_buf_move (vdev, true); + ret =vfio_pci_reset_function (vdev); +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret)) +vfio_pci_dma_buf_move (vdev, false); +up_write (&vdev->memory_lock); + } + + status2 &= ~(PCI_DVSEC_CXL_RST_DONE |PCI_DVSEC_CXL_RST_ERR ); + status2 |= ret ?PCI_DVSEC_CXL_RST_ERR :PCI_DVSEC_CXL_RST_DONE ; } *pctrl2 =cpu_to_le16 (ctrl2);drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -643,8 +643,27 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) goto out_power;/* If reset fails because of the device lock, fail this path entirely */
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 653
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +8
-2- ret =pci_try_reset_function (pdev); -if (ret == -EAGAIN ) +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +/* +* VM power-on resets a CXL Type-2 device through its DVSEC +* sequence. vconfig is not built yet here, so take memory_lock +* and call the op directly rather than the wrapper. +*/ +down_write (&vdev->memory_lock); + ret = vdev->cxl_ops->reset (vdev);
CXL reset restores PCI config from pdev's saved state. Where is that state refreshed before this call ?
Best regards,
Richard Cheng.
Quoted 124 lines
drivers/vfio/pci/vfio_pci_core.c +57
-10+up_write (&vdev->memory_lock); + }else { + ret =pci_try_reset_function (pdev); + } + +/* +* -EAGAIN means the reset could not run. For a CXL device any reset +* error must also fail the open: a failed DVSEC reset can leave the HDM +* decoder cleared or unrestored, and continuing would expose the HDM +* region for host access through a decoder in an unknown state. +*/ +if (ret == -EAGAIN || + (vdev->cxl_ops && vdev->cxl_ops->reset && ret)) goto out_disable_device; vdev->reset_works = !ret;@@ -845,16 +864,30 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)* overwrite the previously restored configuration information. */ if (vdev->reset_works) { - bridge =pci_upstream_bridge (pdev); -if (bridge && !pci_dev_trylock (bridge)) - goto out_restore_state; -if (pci_dev_trylock (pdev)) { -if (!__pci_reset_function_locked (pdev)) +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +/* +* VM power-off resets a CXL Type-2 device through its +* DVSEC sequence. The sequence takes its own device lock, +* so run it outside the lock below. +* vconfig is already freed here, so call the op directly +* under memory_lock rather than the wrapper. +*/ +down_write (&vdev->memory_lock); +if (!vdev->cxl_ops->reset (vdev)) vdev->needs_reset = false; -pci_dev_unlock (pdev); +up_write (&vdev->memory_lock); + }else { + bridge =pci_upstream_bridge (pdev); +if (bridge && !pci_dev_trylock (bridge)) + goto out_restore_state; +if (pci_dev_trylock (pdev)) { +if (!__pci_reset_function_locked (pdev)) + vdev->needs_reset = false; +pci_dev_unlock (pdev); + } +if (bridge) +pci_dev_unlock (bridge); } -if (bridge) -pci_dev_unlock (bridge); } out_restore_state:@@ -1592,6 +1625,20 @@ static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev,return ret; } +/* +* Reset the function. A CXL device runs the CXL DVSEC reset sequence in place +* of a PCI function reset: it replaces FLR (which would corrupt CXL.mem), +* always clears device memory, and restores the HDM decoder. Callers hold +* memory_lock for write. +*/ +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev) +{ +if (!vdev->cxl_ops || !vdev->cxl_ops->reset) +return pci_try_reset_function (vdev->pdev); + +return vdev->cxl_ops->reset (vdev); +} +static int vfio_pci_ioctl_reset (struct vfio_pci_core_device *vdev,void __user *arg) {@@ -1614,8 +1661,14 @@ static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev,vfio_pci_set_power_state (vdev,PCI_D0 );vfio_pci_dma_buf_move (vdev, true); - ret =pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Re-arm the dma-bufs on success. A CXL device whose reset failed leaves +* the HDM decoder unrestored and hdm_valid clear, so re-arming its HDM +* dma-buf would map device DMA onto a decoder the fault path still gates; +* keep it revoked until a reset succeeds. Plain vfio-pci is unchanged. +*/ +if (__vfio_pci_memory_enabled (vdev) && (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock);drivers/vfio/pci/vfio_pci_priv.h +1
-0@@ -78,6 +78,7 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t state);void vfio_pci_zap_and_down_write_memory_lock (struct vfio_pci_core_device *vdev); +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev);u16 vfio_pci_memory_lock_and_enable (struct vfio_pci_core_device *vdev);void vfio_pci_memory_unlock_and_restore (struct vfio_pci_core_device *vdev,u16 cmd);include/linux/vfio_pci_core.h +4
-0@@ -74,6 +74,10 @@ struct vfio_cxl_ops {void (*close_device)(struct vfio_pci_core_device *vdev);void (*reset_prepare)(struct vfio_pci_core_device *vdev);void (*reset_done)(struct vfio_pci_core_device *vdev); +/* Run the CXL reset (always clears CXL.mem) in place of FLR */ +int (*reset)(struct vfio_pci_core_device *vdev); +/* True while the HDM range is valid and its dma-buf may be armed */ +bool (*hdm_active)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ struct module *owner; };Signature · 1 line
2.25.1
Re: [PATCH v5 06/27] vfio/pci: Detect CXL devices and load the CXL provider on demand
In reply to [email protected]Quoted 37 lines · On Thu, Sep 17, 2026 at 12:05:19AM +0800, [email protected] wrote:
From: Manish Honap <[email protected]>
A CXL device needs the vfio-cxl callbacks, but pulling vfio-cxl and the CXL core in unconditionally would bloat every vfio-pci setup. Detect a CXL Type-2 device at bind and
request_module("vfio-cxl")only then, then hand the device to the registered ops.Each bound CXL device pins the provider through
vfio_pci_get_cxl_ops()(try_module_get) and drops it withvfio_pci_put_cxl_ops()at release, so vfio-cxl can unload once no CXL device is bound.If the provider is absent the device is driven as plain vfio-pci. A built-in provider whose initcall has not run yet is waited for with -EPROBE_DEFER; the deferred-probe machinery will retry the bind once the provider registers.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 84 ++++++++++++++++++++++++++++++++ include/linux/vfio_pci_core.h | 3 ++ 2 files changed, 87 insertions(+)drivers/vfio/pci/vfio_pci_core.c +5
-0@@ -2202,6 +2202,84 @@ void vfio_pci_core_unregister_cxl_ops(const struct vfio_cxl_ops *ops) }EXPORT_SYMBOL_GPL (vfio_pci_core_unregister_cxl_ops); +static const struct vfio_cxl_ops *vfio_pci_get_cxl_ops (void ) +{ +guard (rwsem_read)(&vfio_pci_cxl_ops_rwsem); + +if (vfio_pci_cxl_ops &&try_module_get (vfio_pci_cxl_ops->owner))
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_unregister_cxl_ops(const struct vfio_cxl_ops *ops) · line 2219
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+return vfio_pci_cxl_ops; + +return NULL ; +} + +static void vfio_pci_put_cxl_ops (const struct vfio_cxl_ops *ops) +{ +module_put (ops->owner); +} +
Anything guarantees that we can finish devres cleanup before dropping module ref ?
This function drops the module ref during vfio_pci_remove(), while devres might still pending.
I see in patch 18 it register vfio_cxl_release_hpa() as a devres callback, and it lives in vfio-cxl. If this drops the last module ref, another process could unload vfio-cxl before devres invokes that callback.
Best regards,
Richard Cheng.
Quoted 111 lines
drivers/vfio/pci/vfio_pci_core.c +69
-0+/* +* A CXL Type-2 device advertises both CXL.cache and CXL.mem in its CXL DVSEC. +* pcie_is_cxl() is also true for Type-1 (cache only) and Type-3 (mem only) +* devices, which the vfio-cxl provider does not handle, so confirm the Type-2 +* identity before engaging it. +*/ +static bool vfio_pci_is_cxl_type2 (struct pci_dev *pdev) +{ +u16 dvsec, cap; + +if (!pcie_is_cxl (pdev)) +return false; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return false; + +if (pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CAP , &cap)) +return false; + +return (cap &PCI_DVSEC_CXL_CACHE_CAPABLE ) && + (cap &PCI_DVSEC_CXL_MEM_CAPABLE ); +} + +/* +* Load vfio-cxl on demand for a CXL Type-2 device and hand the device to its +* ops. If the provider is absent the device is driven as plain vfio-pci; a +* built-in provider whose initcall has not run yet is waited for with +* -EPROBE_DEFER. +*/ +static int vfio_pci_core_cxl_init (struct vfio_pci_core_device *vdev) +{ +const struct vfio_cxl_ops *ops; +int ret; + +if (!vfio_pci_is_cxl_type2 (vdev->pdev)) +return 0 ; + +request_module ("vfio-cxl" ); + ops =vfio_pci_get_cxl_ops (); +if (!ops) +return IS_BUILTIN (CONFIG_VFIO_CXL ) ? -EPROBE_DEFER :0 ; + + ret = ops->init (vdev); +if (ret) { +vfio_pci_put_cxl_ops (ops); +return ret; + } + + vdev->cxl_ops = ops; +return 0 ; +} + +static void vfio_pci_core_cxl_release (struct vfio_pci_core_device *vdev) +{ +if (!vdev->cxl_ops) +return ; + + vdev->cxl_ops->release (vdev); +vfio_pci_put_cxl_ops (vdev->cxl_ops); +} +int vfio_pci_core_init_dev (struct vfio_device *core_vdev) { struct vfio_pci_core_device *vdev =@@ -2223,6 +2301,10 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)init_rwsem (&vdev->memory_lock);xa_init (&vdev->ctx); + ret =vfio_pci_core_cxl_init (vdev); +if (ret) +return ret; +return 0 ; }EXPORT_SYMBOL_GPL (vfio_pci_core_init_dev);@@ -2232,6 +2314,8 @@ void vfio_pci_core_release_dev(struct vfio_device *core_vdev)struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev); +vfio_pci_core_cxl_release (vdev); +mutex_destroy (&vdev->igate);mutex_destroy (&vdev->ioeventfds_lock);kfree (vdev->region);include/linux/vfio_pci_core.h +3
-0@@ -29,6 +29,7 @@ struct vfio_pci_core_device;struct vfio_pci_region ;struct p2pdma_provider ;struct dma_buf_attachment ; +struct vfio_cxl_state ;struct vfio_pci_eventfd {struct eventfd_ctx *ctx;@@ -109,6 +110,8 @@ struct vfio_pci_core_device {struct vfio_device vdev;struct pci_dev *pdev;const struct vfio_pci_device_ops *pci_ops; +const struct vfio_cxl_ops *cxl_ops; +struct vfio_cxl_state *cxl;void __iomem *barmap[PCI_STD_NUM_BARS ];bool bar_mmap_supported[PCI_STD_NUM_BARS ];/* Flags modified at runtime - dedicated storage unit */ Signature · 1 line
2.25.1
RE: [PATCH v5 26/27] Documentation: vfio-pci: Document CXL Type-2 device passthrough
In reply to Gregory PriceHello Gregory,
Thank you for the careful read of the documentation. I will rework the sections you flagged as below:
- Address model
I will update the wording to mention that VMM does know the GPA: it builds the guest's CFMWS window and so chooses the guest-physical range the device can inhabit. What the host owns is the HPA and the placement within it, not knowledge of the GPA.
I will add the two models you described, fixed placement for an accelerator that needs exact physical placement versus no fixed placement for pooled or compressed memory, with a small HPA/GPA example, and note that the current series under discussion implements the fixed-offset case.
- Virtual decoders
I will rename the The "Guest decoder and commit" section to "Virtual decoders" and add a short flow showing a guest vdecoder write being absorbed and a live read returning COMMITTED from the host-locked physical decoder.
- "The kernel virtualizes ..."
I will update it to say vfio-pci-core (its config-space permission hooks).
- guest IOAS, stage-2 mapping, and struct-page-less coherent memory
I will define these terms where first used and include that the range is handed to the device whole and never onlined as system RAM. I will also add details why a stale stage-2 mapping must not outlive the HDM window, and how the VMM rebuilds it.
- Reset
I will reword this section. FLRs are virtualized so a guest reset can't inflict physical CXL.mem/decoder effects on the host rather than loose wording "must not take an FLR"
I can share the revised text ahead of the v6 posting if that is easier to review.
Thanks,
Manish
Quoted 253 lines
-----Original Message-----
From: Gregory Price <[email protected]>
Sent: Thursday, September 17, 2026 1:03 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 26/27] Documentation: vfio-pci: Document CXL Type- 2 device passthroughExternal email: Use caution opening links or attachments
Quoted 2 lines · On Thu, Sep 17, 2026 at 12:05:39AM +0530, [email protected] wrote:
From: Manish Honap <[email protected]>
1) Thank you so much for writing documentation, i truly appreciate this.
2) I apologize in advance for my terseness, I know writing is hard, please do not interpret this as disliking your writing or series.
Review
Quoted 8 lines
(quoted diff) +8
-0+Address model +============= + +The HDM memory is a coherent host physical range (HPA). The host +kernel resolves that range before the guest sees the device, and owns +it for the bind lifetime. The guest only chooses where the memory +appears in its own physical address space (GPA), by programming a +virtual endpoint HDM decoder. The guest never reprograms the physicaldecoder.
Review
Quoted 7 lines
(quoted diff) +7
-0+ +The kernel holds the HPA and does not see the GPA. The guest programs +a GPA and does not see the HPA. The VMM holds the device fd, reads +the committed base from the decoder-register region described below, +and maps the HPA-backed HDM region at the GPA the guest committed. +The base the guest reads back is the GPA, not the HPA. +I think this must be slightly inaccurate / imprecise wording.
The host *must* provide some form of physical memory window to the guest at initialization time, otherwise the guest has no way to know - at boot time - that there's even a window of memory it can use.
That's what the CFMWS is. This is initialized by the hypervisor - which is controlled by the host.
So the host (at least the VMM) must know, for the region the entire device *could* inhabit, what that GPA is - because it's the one that makes the CFMWS for the guest.
If this is not the case, then something is missing from this documentation to explain why.
If you're actually trying to say is that the GPA's programmed into the virtual decoders are largely symbolic - this at best feels a bit inaccurate and simply an implementation detail.
The host's virtio device could enforce ....:
Host Range:
CFMWS HPA - [0x10000, 0x20000] | |Guest Range: | |
CFMWS GPA - [0x50000,0x60000]In that case, you'd get the following translation...
vdecoder0.0 - [0x58000,0x60000]
CFMWS GPA - [0x58000,0x60000]
CFMWS HPA - [0x18000,0x20000]Or the virtio device could not enforce that and let the host page-fault just hand it a random page from the actual CXL device.
vdecoder0.0 - [0x58000, 0x60000] CFMWS GPA - [0x58000, 0x60000] | No discrete host mappingThe former makes sense if the device (accelerator) requires exact physical placement to do its accelerator nonsense.
The latter makes sense if the device (accelerator) doesn't care about placement (compressed memory).
This is not saying we need support both out of the box, but we shouldn't lock ourselves into the former unless there's some reason why the latter is not reasonable.
Can you please help document what the actual expected behavior is with examples in the Address model section so it's easier to understand the intent? That will help quite a bit.
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 7 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +7
-0+Guest decoder and commit +======================== + +The guest programs its virtual endpoint decoder through the trapped +region: it writes a base (a GPA), a size, and then the COMMIT bit. +The host already resolved and committed the physical placement before +the guest ran,So the host does know GPA, just not exact placement.
Review
Quoted 4 lines
so a live read of the decoder always shows COMMITTED and the
(quoted diff) +3
-0+guest's commit poll completes. The physical decoder is never +rewritten; the guest's writes are absorbed. +Rather clunky, round-about way to say "The guest decoders are
virtualized". If possible, it would be nice to formalize this concept
into "Virtual Decoders" - since that's what this is.With that concept i think you can probably generate some nice diagrams that show how the guest vdecoder's interact with the host drivers.
Review
Quoted 3 lines
(quoted diff) +3
-0+The VMM observes the commit, reads the committed base, and maps the +HDM region at that GPA. +So the host does know the GPA.
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 6 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +6
-0+CXL Device DVSEC +================ + +The kernel virtualizes the CXL Device DVSEC body through the +config-space permission hooks. Reads and writes inside the DVSEC body +use a per-open shadow; a guest write stays in the shadow and does notreach hardware.
Review
Quoted 2 lines
(quoted diff) +2
-0+Accesses outside the DVSEC body go to the device as usual. +"The kernel" - what part? vfio-pci ? the vmm ?
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 6 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +6
-0+DMA and iommufd +=============== + +A Type-2 accelerator issues ATS-translated DMA to addresses inside +its own HDM window, so that range must be present in the guest IOAS +that backs the nested stage-2 translation.Type-2, ATS, DMA, HDM window, guest IOAS, stage-2 translation
I think the only thing i don't know in this sentence is "guest IOAS" and it's still hurting my brain to read.
Are all accelerators expect to have this particular interaction, or just yours?
Review
Quoted 3 lines
The HDM range is struct-page-less coherent
(quoted diff) +2
-0+memory, which a userspace-VA ``IOMMU_IOAS_MAP`` cannot pin. +The hardest part about writing about virtualization is keeping a consistent mental model from section to section.
which userspace? guest? host? (i presume guest here)
struct-page-less coherent memory
e.g. the host never hotplugs this, it hands the entire region directly to the VFIO device, right?I think this would be nice to spell out somewhere.
Review
Quoted 3 lines
(quoted diff) +3
-0+The HDM memory region is therefore exportable as a dma-buf: +``VFIO_DEVICE_FEATURE_DMA_BUF`` on that region returns an fd that +iommufd maps with ``IOMMU_IOAS_MAP_FILE``, mapping the physicalrange
Review
Quoted 4 lines
(quoted diff) +4
-0+without a VA or a page pin. The dma-buf is revoked whenever the +mapping is torn down (reset, power transition, teardown), so a stale +stage-2 mapping cannot outlive the HDM window. +For the sake of readers, I think either a little bit more information on this "stage-2 mapping" concept is needed to make sense of what's going on here and why it mustn't outlive the HDM window.
Review
Quoted 9 lines
(quoted diff) +9
-0+Reset +===== + +A CXL Type-2 function must not take a Function Level Reset: an FLR +resets the coherent CXL.mem state and the HDM decoder. The PCI core +reflects this by preferring the CXL reset over FLR, so a function +reset of a CXL device runs the CXL DVSEC reset sequence, which resets +the function and then restores the HDM decoder and the PCI config state. +I think what you're trying to say is that FLRs are never passed to the device because it can cause physical device effects that defeat the purpose of the virtualization, yes?
So we virtualize FLRs...
Review
Quoted 3 lines
(quoted diff) +3
-0+A guest requests a reset by writing Initiate CXL Reset in the DVSEC. +That write only stamps completion in the shadow. The real reset runs +at the vfio reset points (the reset ioctl and a virtualized FLR through configspace):
As you describe here.
So it's not that an accelerator "must not take an FLR" - it's that FLRs are virtualized to prevent deleterious effects on the host / hardware.
Am I misunderstanding this?
Review
Quoted 13 lines
(quoted diff) +13
-0+the kernel zaps the HDM mapping and revokes the dma-buf, then runs +the CXL reset, which always clears the device memory, and restores +and re-samples the decoder afterwards. A CXL port masks Secondary Bus +Reset by default, so a ``VFIO_DEVICE_PCI_HOT_RESET`` does not reach +the endpoint and the HDM state is untouched. If the port has SBR +unmasked the reset can decommit the decoder without restoring it, so +the reset_done handler gates HDM access; a ``VFIO_DEVICE_RESET`` then +runs the CXL reset sequence and restores it. + +The decoder register region is served by live reads of the hardware +decoder with guest writes absorbed: the decoder is committed and +locked by the host, so a guest can neither decommit nor reprogram it, +and the kernel keeps no shadow of the decoder state.This is basically what I said at the beginning - it must either be that the host provides locked auto-decoders at boot, or it must provide proper virtualization so that the decoders settings are fully virtualized.
Seems it's the former, and that makes sense. Please correct me if i'm misunderstanding.
Review
Quoted 7 lines
After a reset the kernel restores and
(quoted diff) +6
-0+re-samples the firmware-committed decoder, so the geometry the guest +reads back is unchanged. A VMM that dropped its HDM mapping, for +example across a reset or a D3hot->D0 transition, must rescan the +decoder and rebuild its +stage-2 mapping before it resumes HDM access. +Yeah i think we need a bit more information about this stage-2 mapping rebuild to make sense of this. Maybe I'm just not read-up enough on this particular setup - is there another part of the docs you can link to that talk about this, or are you able to share some details as to what this rebuild process looks like?
~Gregory
RE: [PATCH v5 21/27] vfio/cxl: Exclude the HDM decoder registers from direct BAR access
In reply to Richard ChengQuoted 81 lines
-----Original Message-----
From: Richard Cheng <[email protected]>
Sent: Thursday, September 17, 2026 12:59 PM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 21/27] vfio/cxl: Exclude the HDM decoder registers from direct BAR accessQuoted 10 lines · On Thu, Sep 17, 2026 at 12:05:34AM +0800, [email protected] wrote:
From: Manish Honap <[email protected]>
The HDM decoder register block lives in the component BAR that vfio-pci owns. The guest reads the decoder only through the trapped, read-only decoder region; it must not reach the physical registers through the BAR directly, and a host read of the range through a kernel mapping could abort as a fatal host SError.
Add the block to the generic excluded-range list with
VFIO_PCI_EXCLUDE_MMAP|VFIO_PCI_EXCLUDE_READ|
VFIO_PCI_EXCLUDE_WRITEQuoted 2 lines
similar to the MSI-X exclusion.
Hi Manish,
I think we need to block eventfd write and BAR DMA as well ?
Have some comments below.
Quoted 14 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0@@ -435,8 +435,25 @@ static int vfio_cxl_add_region(struct
vfio_pci_core_device*vdev, u32 subtype, static intvfio_cxl_open_device(struct vfio_pci_core_device *vdev){drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_add_region(struct vfio_pci_core_device *vdev, u32 subtype,
Quoted 6 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0struct vfio_cxl_state *cxl = vdev->cxl; +struct pci_dev *pdev = vdev->pdev;int ret; +/* +* Keep the HDM decoder block out of the guest's direct BAR access: the
drivers/vfio/pci/cxl/vfio_cxl_core.c
Quoted 1 line
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0+* guest reaches it only through the trapped decoder region, and a host
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_add_region(struct vfio_pci_core_device *vdev, u32 subtype, · line 453
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+* read of the range through a kernel mapping could abort as an SError. +* Exclude it from mmap, fill reads with -1 and drop writes. The list is +* cleared on close, so re-add it per open. +*/ + ret =vfio_pci_core_add_excluded_range (vdev, pdev->hdm->hdm_bar, + pdev->hdm->hdm_offset, + cxl->hdm_len, +VFIO_PCI_EXCLUDE_MMAP | +VFIO_PCI_EXCLUDE_READ | +VFIO_PCI_EXCLUDE_WRITE );This adds the HDM reg to the exclusion list, but the eventfd and BAR DMA path doesn't check that.
Quoted 2 lines
Maybe we need to block those 2 path as well ?
Thanks, yes, I Agree on both. The excluded-range list was only consulted on the fd read/write and mmap paths, so, I think, the decoder block is still reachable two other ways suggested above.
For ioeventfd: I will add a guard in vfio_pci_ioeventfd() to reject a registration whose [pos, pos + count) overlaps a write-excluded range, right next to the existing MSI-X guard.
For BAR DMA. I will add a check in vfio_pci_core_feature_dma_buf() to reject an export whose ranges overlap an excluded range on that BAR.
Thanks,
Manish
Quoted 15 lines
Best regards,
Richard Cheng.drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_add_region(struct vfio_pci_core_device *vdev, u32 subtype,
Quoted 5 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0+if (ret) +return ret; +/* * vfio_pci_core_disable() frees all dynamic regions on close, so register
Quoted 5 lines
* them here per open rather than at bind. A failed first open never
--
2.25.1
RE: [PATCH v5 24/27] vfio/cxl: Export the HDM memory region as a dma-buf
In reply to Richard ChengQuoted 145 lines
-----Original Message-----
From: Richard Cheng <[email protected]>
Sent: Thursday, September 17, 2026 1:25 PM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 24/27] vfio/cxl: Export the HDM memory region as a dma-bufQuoted 7 lines · On Thu, Sep 17, 2026 at 12:05:37AM +0800, [email protected] wrote:
From: Manish Honap <[email protected]>
A Type-2 accelerator issues ATS-translated DMA to addresses inside its own HDM window, so that coherent host range must be present in the guest's IOAS (the iommufd IOAS backing the nested SMMU stage-2). iommufd maps a struct-page-less range only by fd, via
IOMMU_IOAS_MAP_FILEover a dma-buf; a userspace-VA
IOMMU_IOAS_MAPofQuoted 1 line
the HDM mmap is rejected because the VMA is
VM_IO|VM_PFNMAP.Without
Quoted 1 line
a dma-buf the range could only be mapped through an out-of-tree PFNMAP
work-around.
Quoted 21 lines
vfio-pci already exports BAR memory as a P2P dma-buf, but the exporter is BAR-only:
vfio_pci_core_feature_dma_buf()rejects any region index at or above the ROM index, andvfio_pci_core_get_dmabuf_phys()resolves the physical range from a PCI BAR. The HDM memory region is a dynamic device-specific region, not a BAR.Let a device-specific region reach the device's
get_dmabuf_phys(): a region index at or aboveVFIO_PCI_NUM_REGIONSskips the BAR-resource check and is validated by the driver instead, bounded to the regions that exist. Install a CXL-awareget_dmabuf_phys()in the vfio-cxl provider that returnscxl->hpa_rangefor the HDM memory region and delegates real BARs to the core, keeping the BAR path unchanged and the core free of CXL knowledge.The HDM window is coherent host memory with no p2pdma provider of its own, so borrow BAR 0's, matching nvgrace-gpu's handling of its non-BAR device memory. The iommufd importer does not consume the provider; the scatterlist map path (real peer DMA) is left to a follow-up once upstream grows a negotiated interconnect for coherent CXL memory.
Quoted 4 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 60++++++++++++++++++++++++++++
Quoted 17 lines
drivers/vfio/pci/vfio_pci_dmabuf.c | 27 +++++++++++-- 2 files changed, 83 insertions(+), 4 deletions(-)drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0@@ -11,6 +11,7 @@#include <linux/mm.h> #include <linux/module.h> #include <linux/pci.h> +#include <linux/pci-p2pdma.h> #include <linux/range.h> #include <linux/slab.h> #include <linux/uaccess.h> drivers/vfio/pci/cxl/vfio_cxl_core.c line 28
Quoted 1 line
drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0@@ -27,6 +28,7 @@* @hdm_regs: mapped HDM decoder registers, read live by the decoder region
Quoted 17 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0* @hdm_len: length of the HDM decoder register block * @hdm_valid: true when host CPU access to the HDM range is safe; under
memory_lock
+ * @mem_region_index: vfio region index of the mmap-able HDM memory
+ region
*/
structvfio_cxl_state{drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0struct cxl_dev_state cxlds;@@ -36,6 +38,7 @@ struct vfio_cxl_state {void __iomem *hdm_regs;u32 hdm_len;bool hdm_valid; +unsigned int mem_region_index; }; static unsigned long vfio_cxl_mem_pgoff(struct vm_area_struct *vma,drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data) · line 300
Quoted 9 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +6
-0@@ -289,6 +292,50 @@ static void vfio_cxl_release_hpa(void *data)release_mem_region (cxl->hpa_range.start,range_len (&cxl->hpa_range)); } +/* +* Resolve the physical range that backs a dma-buf export. The core +exporter +* only knows BARs; teach it the HDM memory region so a guest IOAS +can map the +* coherent window by fd (IOMMU_IOAS_MAP_FILE) instead of the removed
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data)
Quoted 8 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +8
-0+PFNMAP +* work-around. Real BARs stay on the byte-identical core path. +*/ +static int vfio_cxl_get_dmabuf_phys (struct vfio_pci_core_device *vdev, +struct p2pdma_provider **provider, +unsigned int region_index, +struct phys_vec *phys_vec, + struct vfio_region_dma_range
*dma_ranges,drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data)
Quoted 11 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +11
-0+ size_t nr_ranges) +{ +struct vfio_cxl_state *cxl = vdev->cxl; + +/* Real BARs go through the core P2P exporter unchanged. */ +if (region_index <VFIO_PCI_NUM_REGIONS ) +return vfio_pci_core_get_dmabuf_phys (vdev, provider, + region_index, phys_vec, + dma_ranges, nr_ranges); + +/* Of the device regions, only the HDM memory window is exportable. */
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data)
Quoted 5 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +5
-0+if (region_index != cxl->mem_region_index) +return -EINVAL ; + +/* +* The HDM window is coherent host memory, not BAR MMIO, so it has no
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data) · line 325
Quoted 1 line
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0+* p2pdma provider of its own. Borrow BAR 0's: the P2P properties match
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data) · line 326
Quoted 1 line
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0+* and the iommufd importer does not consume the provider. The sgt map
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data) · line 326
Quoted 3 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0+* path (real peer DMA) is not supported for the HDM window. +*/ + *provider =pcim_p2pdma_provider (vdev->pdev,0 );I think of a weird scenario which might make peer DMA work, not sure if that's possible.
Quoted 7 lines
userspace can give this dma-buf fd to an NIC driver or so to do RDMA ? use HDM memory as network buffer ?
If that's the case and direct P2P addressing is selected, it applies BAR 0's bus offset to HDM physical address.
Maybe explicitly reject peer-DMA mapping would be safer ?
Yes, I think this will require handling.
For v6, I will tag the HDM export with no_peer2peer and vfio_pci_dma_buf_map() return -EOPNOTSUPP for it, so a peer-DMA mapping is refused cleanly instead of being handed a wrong address.
Thanks,
Manish
Quoted 113 lines
Best regards,
Richard Cheng.drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_open_device(struct
Quoted 14 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +11
-0+if (!*provider) +return -EINVAL ; + +return vfio_pci_core_fill_phys_vec (phys_vec, dma_ranges, nr_ranges, + cxl->hpa_range.start, +range_len (&cxl->hpa_range)); +} + +static const struct vfio_pci_device_ops vfio_cxl_pci_dev_ops = { + .get_dmabuf_phys = vfio_cxl_get_dmabuf_phys, }; +static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;@@ -483,6 +530,19 @@ static int vfio_cxl_open_device(struct
vfio_pci_core_device*vdev)drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)
Quoted 4 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0if (ret)return ret; +/* Record where the HDM memory region landed for the dma-buf export. */
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_hpa(void *data)
Quoted 6 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0+ cxl->mem_region_index = VFIO_PCI_NUM_REGIONS + vdev-
num_regions-
+1;drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0+ +/* +* Override the device ops so a dma-buf export of the HDM memory region
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)
Quoted 3 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0+* resolves to the coherent host range. This is done at open, not init: +* vfio_pci_probe() resets pci_ops after vfio_alloc_device() returns, so +* an override installed during init would be clobbered. Only a CXL device
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)
Quoted 6 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +5
-0+* reaches this hook (cxl_ops is set on init success), so a fallback to +* plain vfio-pci keeps the core ops. +*/ + vdev->pci_ops = &vfio_cxl_pci_dev_ops; + ret = vfio_cxl_add_region(vdev,
VFIO_REGION_SUBTYPE_CXL_COMP_REGS,Quoted 8 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0&vfio_cxl_comp_regops, cxl->hdm_len, VFIO_REGION_INFO_FLAG_READ |drivers/vfio/pci/vfio_pci_dmabuf.c +0
-0drivers/vfio/pci/vfio_pci_dmabuf.c int vfio_pci_core_get_dmabuf_phys(struct · line 183
Quoted 6 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +3
-0@@ -178,6 +178,15 @@ int vfio_pci_core_get_dmabuf_phys(struct vfio_pci_core_device *vdev, {struct pci_dev *pdev = vdev->pdev; +/* +* This resolver only handles PCI BARs. A device-specific region index +* (>= PCI_STD_NUM_BARS) would index pdev->resource[] out of bounds via
drivers/vfio/pci/vfio_pci_dmabuf.c int vfio_pci_core_feature_dma_buf(struct
Quoted 10 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +6
-0+* pcim_p2pdma_provider(), so reject it; a driver that exports such a +* region installs its own get_dmabuf_phys. +*/ +if (region_index >=PCI_STD_NUM_BARS ) +return -EINVAL ; + *provider =pcim_p2pdma_provider (pdev, region_index);if (!*provider)return -EINVAL ;@@ -227,6 +236,7 @@ int vfio_pci_core_feature_dma_buf(struct
vfio_pci_core_device*vdev, u32 flags,drivers/vfio/pci/vfio_pci_dmabuf.c int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
Quoted 7 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +1
-0DEFINE_DMA_BUF_EXPORT_INFO (exp_info);struct vfio_pci_dma_buf *priv;size_t length; +u32 index;int ret; if (!vdev->pci_ops || !vdev->pci_ops->get_dmabuf_phys) @@ -243,13
Quoted 1 line
+253,22 @@ int vfio_pci_core_feature_dma_buf(struct
vfio_pci_core_device*vdev, u32 flags,drivers/vfio/pci/vfio_pci_dmabuf.c int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
Quoted 11 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +5
-2if (!get_dma_buf.nr_ranges || get_dma_buf.flags)return -EINVAL ; + index = get_dma_buf.region_index; +/* -* For PCI the region_index is the BAR number like everything -* else. Check that PCI resources have been claimed for it. +* A fixed region index is the BAR number; only a BAR can be exported +* and its PCI resource must be claimed. A device-specific region (index +* >= VFIO_PCI_NUM_REGIONS) has no BAR resource and is validated by the
drivers/vfio/pci/vfio_pci_dmabuf.c
Quoted 1 line
drivers/vfio/pci/vfio_pci_dmabuf.c +1
-0+* device's get_dmabuf_phys instead, but the index must name a region
drivers/vfio/pci/vfio_pci_dmabuf.c int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
Quoted 4 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +1
-2+* that exists. */ -if (get_dma_buf.region_index >=VFIO_PCI_ROM_REGION_INDEX || - IS_ERR(vfio_pci_core_get_iomap(vdev,get_dma_buf.region_index)))
drivers/vfio/pci/vfio_pci_dmabuf.c int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
Quoted 9 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +6
-0+if (index <VFIO_PCI_NUM_REGIONS ) { +if (index >=VFIO_PCI_ROM_REGION_INDEX || +IS_ERR (vfio_pci_core_get_iomap (vdev, index))) +return -ENODEV ; + }else if (index -VFIO_PCI_NUM_REGIONS >= vdev->num_regions) {return -ENODEV ; + } dma_ranges = memdup_array_user(&arg->dma_ranges,get_dma_buf.nr_ranges,
Quoted 5 lines
sizeof(*dma_ranges));
--
2.25.1
RE: [PATCH v5 25/27] vfio/cxl: Run the CXL reset at the vfio reset points
In reply to Richard ChengQuoted 231 lines
-----Original Message-----
From: Richard Cheng <[email protected]>
Sent: Thursday, September 17, 2026 1:41 PM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 25/27] vfio/cxl: Run the CXL reset at the vfio reset pointsdrivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_reset_done(struct
Quoted 37 lines · On Thu, Sep 17, 2026 at 12:05:38AM +0800, [email protected] wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 function must not take an FLR: it resets the coherent CXL.mem state and corrupts the HDM decoder. The PCI core already reflects this, ordering
cxl_resetahead of flr in pci_reset_fn_methods[], so a function reset of a CXL device runs the DVSEC reset sequence rather than FLR.Route the vfio function-reset points (
VFIO_DEVICE_RESETand the virtualized PCIe/AF FLR writes) through a CXL reset op that runscxl_reset_dvsec_sequence(). The sequence resets the function, always clearing device memory, and restores the HDM decoder and PCI config state, so it is a complete replacement forpci_try_reset_function()on a CXL device. The op runs undermemory_lockand not the PCI device lock, socxl_reset_dvsec_sequence()can take the device lock itself.Clear
hdm_validfor the duration of the reset so a fault cannot insert a PFN into a decoder that is being torn down, and restore it once the sequence has put the decoder back.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 41 +++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 49 +++++++++++++++--- drivers/vfio/pci/vfio_pci_core.c | 77 +++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_priv.h | 1 + include/linux/vfio_pci_core.h | 4 ++ 5 files changed, 152 insertions(+), 20 deletions(-)drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0@@ -632,6 +632,45 @@ static void vfio_cxl_reset_done(struct
vfio_pci_core_device*vdev)Quoted 13 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0cxl->hdm_valid = false; } +/* +* Run the CXL DVSEC reset sequence in place of a PCI function reset. +A CXL +* Type-2 function must not take an FLR (it would corrupt CXL.mem), +so the vfio +* reset points route here. The sequence resets the function, always +clearing +* device memory, and restores the HDM decoder. The caller holds +memory_lock, +* and this path does not hold the PCI device lock, so drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_reset_done(struct · line 654
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+cxl_reset_dvsec_sequence() +* can take it. +*/ +static int vfio_cxl_reset (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; +int ret; + +lockdep_assert_held_write (&vdev->memory_lock); + +/* Host CPU access to the HDM range is unsafe until the decoder is back. */
drivers/vfio/pci/vfio_pci_config.c static int vfio_basic_config_write(struct
Quoted 42 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +26
-0+ cxl->hdm_valid = false; + + ret =cxl_reset_dvsec_sequence (vdev->pdev); +if (!ret) + cxl->hdm_valid = true; + +return ret; +} + +/* +* The HDM dma-buf may be armed only while the decoder is valid. +After a failed +* reset hdm_valid is clear, so the generic memory-enable re-arm must +skip the +* dma-buf rather than map DMA onto an unrestored decoder. +*/ +static bool vfio_cxl_hdm_active (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; + +lockdep_assert_held_write (&vdev->memory_lock); + +return cxl->hdm_valid; +} +static const struct vfio_cxl_ops vfio_cxl_ops = { .init = vfio_cxl_init_device, .release = vfio_cxl_release_device,@@ -639,6 +678,8 @@ static const struct vfio_cxl_ops vfio_cxl_ops = { .close_device = vfio_cxl_close_device, .reset_prepare = vfio_cxl_reset_prepare, .reset_done = vfio_cxl_reset_done, + .reset = vfio_cxl_reset, + .hdm_active = vfio_cxl_hdm_active, .owner = THIS_MODULE, };drivers/vfio/pci/vfio_pci_config.c +0
-0@@ -630,7 +630,14 @@ static int vfio_basic_config_write(struct
vfio_pci_core_device*vdev, int pos,drivers/vfio/pci/vfio_pci_config.c static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos,
Quoted 6 lines
drivers/vfio/pci/vfio_pci_config.c +2
-1*virt_cmd &=cpu_to_le16 (~mask); *virt_cmd |=cpu_to_le16 (new_cmd & mask); -if (__vfio_pci_memory_enabled (vdev)) +/* +* Re-arm the dma-bufs on memory-enable, but keep a CXL device's
drivers/vfio/pci/vfio_pci_config.c static int vfio_basic_config_write(struct · line 635
Quoted 1 line
drivers/vfio/pci/vfio_pci_config.c +1
-0+* HDM dma-buf revoked while the decoder is unrestored (a failed
drivers/vfio/pci/vfio_pci_config.c static int vfio_basic_config_write(struct · line 636
Quoted 1 line
drivers/vfio/pci/vfio_pci_config.c +1
-0+* reset leaves hdm_valid clear); re-arming would map DMA onto a
drivers/vfio/pci/vfio_pci_config.c static void vfio_lock_and_set_power_state(struct
Quoted 8 lines
drivers/vfio/pci/vfio_pci_config.c +4
-0+* decoder the fault path still gates. Plain vfio-pci is unchanged. +*/ +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || vdev->cxl_ops->hdm_active (vdev)))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -720,7 +727,8 @@ static void vfio_lock_and_set_power_state(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_config.c static int vfio_exp_config_write(struct
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +2
-1}vfio_pci_set_power_state (vdev, state); -if (__vfio_pci_memory_enabled (vdev)) +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || vdev->cxl_ops->hdm_active (vdev)))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -910,8 +918,14 @@ static int vfio_exp_config_write(struct
vfio_pci_core_device*vdev, int pos,drivers/vfio/pci/vfio_pci_config.c static int vfio_af_config_write(struct
Quoted 17 lines
drivers/vfio/pci/vfio_pci_config.c +8
-2if (!ret && (cap &PCI_EXP_DEVCAP_FLR )) {vfio_pci_zap_and_down_write_memory_lock (vdev);vfio_pci_dma_buf_move (vdev, true); -pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Keep the HDM dma-buf revoked if a CXL reset +* failed; re-arming would map DMA onto an +* unrestored decoder. Mirrors the reset ioctl. +*/ +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -995,8 +1009,14 @@ static int vfio_af_config_write(struct
vfio_pci_core_device*vdev, int pos,drivers/vfio/pci/vfio_pci_config.c static int vfio_af_config_write(struct · line 1009
Quoted 1 line
drivers/vfio/pci/vfio_pci_config.c +0
-0if (!ret && (cap & PCI_AF_CAP_FLR) && (cap &
PCI_AF_CAP_TP)) {drivers/vfio/pci/vfio_pci_config.c static int vfio_cxl_dvsec_write(struct
Quoted 16 lines
drivers/vfio/pci/vfio_pci_config.c +8
-2vfio_pci_zap_and_down_write_memory_lock (vdev);vfio_pci_dma_buf_move (vdev, true); -pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Keep the HDM dma-buf revoked if a CXL reset +* failed; re-arming would map DMA onto an +* unrestored decoder. Mirrors the reset ioctl. +*/ +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -1781,9 +1801,22 @@ static int vfio_cxl_dvsec_write(struct
vfio_pci_core_device*vdev, int pos,Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +4
-2status2 |=PCI_DVSEC_CXL_CACHE_INV ; }if (ctrl2 &PCI_DVSEC_CXL_INIT_CXL_RST ) { +int ret =0 ; + ctrl2 &= ~PCI_DVSEC_CXL_INIT_CXL_RST ; - status2 &= ~PCI_DVSEC_CXL_RST_ERR ; - status2 |=PCI_DVSEC_CXL_RST_DONE ; + +if (vdev->cxl_ops && vdev->cxl_ops->reset) {drivers/vfio/pci/vfio_pci_config.c static int vfio_cxl_dvsec_write(struct vfio_pci_core_device *vdev, int pos,
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+vfio_pci_zap_and_down_write_memory_lock (vdev); +vfio_pci_dma_buf_move (vdev, true); + ret =vfio_pci_reset_function (vdev); +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret)) +vfio_pci_dma_buf_move (vdev, false); +up_write (&vdev->memory_lock); + } + + status2 &= ~(PCI_DVSEC_CXL_RST_DONE |
PCI_DVSEC_CXL_RST_ERR);drivers/vfio/pci/vfio_pci_config.c static int vfio_cxl_dvsec_write(struct · line 1819
Quoted 1 line
drivers/vfio/pci/vfio_pci_config.c +1
-0+ status2 |= ret ? PCI_DVSEC_CXL_RST_ERR :
PCI_DVSEC_CXL_RST_DONE;drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct
Quoted 9 lines
drivers/vfio/pci/vfio_pci_config.c +0
-0} *pctrl2 =cpu_to_le16 (ctrl2);drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -643,8 +643,27 @@ int vfio_pci_core_enable(struct
vfio_pci_core_device*vdev)Quoted 2 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0goto out_power;
Quoted 14 lines
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 653
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +8
-2/* If reset fails because of the device lock, fail this path entirely */ - ret =pci_try_reset_function (pdev); -if (ret == -EAGAIN ) +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +/* +* VM power-on resets a CXL Type-2 device through its DVSEC +* sequence. vconfig is not built yet here, so take memory_lock +* and call the op directly rather than the wrapper. +*/ +down_write (&vdev->memory_lock); + ret = vdev->cxl_ops->reset (vdev);CXL reset restores PCI config from pdev's saved state. Where is that state refreshed before this call ?
Hmm, yes, on the enable path it is not refreshed. cxl_reset_dvsec_sequence() does not snapshot the state before resetting.
For v6, I will add call to pci_save_state() from vfio_pci_core_enable(). The disable path already saves before its reset, so only enable path will need the fix.
Thanks,
Manish
Quoted 146 lines
Best regards,
Richard Cheng.drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +9
-0+up_write (&vdev->memory_lock); + }else { + ret =pci_try_reset_function (pdev); + } + +/* +* -EAGAIN means the reset could not run. For a CXL device any reset +* error must also fail the open: a failed DVSEC reset can leave the HDM +* decoder cleared or unrestored, and continuing would expose the HDM
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0+* region for host access through a decoder in an unknown state. +*/ +if (ret == -EAGAIN || + (vdev->cxl_ops && vdev->cxl_ops->reset && ret)) goto out_disable_device; vdev->reset_works = !ret;@@ -845,16 +864,30 @@ void vfio_pci_core_disable(struct
vfio_pci_core_device*vdev)drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +3
-5* overwrite the previously restored configuration information. */ if (vdev->reset_works) { - bridge =pci_upstream_bridge (pdev); -if (bridge && !pci_dev_trylock (bridge)) - goto out_restore_state; -if (pci_dev_trylock (pdev)) { -if (!__pci_reset_function_locked (pdev)) +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +/* +* VM power-off resets a CXL Type-2 device through its
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct · line 870
Quoted 1 line
drivers/vfio/pci/vfio_pci_core.c +1
-0+* DVSEC sequence. The sequence takes its own device lock,
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_set_irqs(struct
Quoted 27 lines
drivers/vfio/pci/vfio_pci_core.c +18
-3+* so run it outside the lock below. +* vconfig is already freed here, so call the op directly +* under memory_lock rather than the wrapper. +*/ +down_write (&vdev->memory_lock); +if (!vdev->cxl_ops->reset (vdev)) vdev->needs_reset = false; -pci_dev_unlock (pdev); +up_write (&vdev->memory_lock); + }else { + bridge =pci_upstream_bridge (pdev); +if (bridge && !pci_dev_trylock (bridge)) + goto out_restore_state; +if (pci_dev_trylock (pdev)) { +if (!__pci_reset_function_locked (pdev)) + vdev->needs_reset = false; +pci_dev_unlock (pdev); + } +if (bridge) +pci_dev_unlock (bridge); } -if (bridge) -pci_dev_unlock (bridge); } out_restore_state:@@ -1592,6 +1625,20 @@ static int vfio_pci_ioctl_set_irqs(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_reset(struct
Quoted 23 lines
drivers/vfio/pci/vfio_pci_core.c +16
-0return ret; } +/* +* Reset the function. A CXL device runs the CXL DVSEC reset sequence +in place +* of a PCI function reset: it replaces FLR (which would corrupt +CXL.mem), +* always clears device memory, and restores the HDM decoder. Callers +hold +* memory_lock for write. +*/ +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev) { +if (!vdev->cxl_ops || !vdev->cxl_ops->reset) +return pci_try_reset_function (vdev->pdev); + +return vdev->cxl_ops->reset (vdev); +} +static int vfio_pci_ioctl_reset (struct vfio_pci_core_device *vdev,void __user *arg) {@@ -1614,8 +1661,14 @@ static int vfio_pci_ioctl_reset(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev,
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +3
-2vfio_pci_set_power_state (vdev,PCI_D0 );vfio_pci_dma_buf_move (vdev, true); - ret =pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Re-arm the dma-bufs on success. A CXL device whose reset failed leaves
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_reset(struct · line 1667
Quoted 1 line
drivers/vfio/pci/vfio_pci_core.c +1
-0+* the HDM decoder unrestored and hdm_valid clear, so re-arming its HDM
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_reset(struct · line 1668
Quoted 1 line
drivers/vfio/pci/vfio_pci_core.c +1
-0+* dma-buf would map device DMA onto a decoder the fault path still gates;
drivers/vfio/pci/vfio_pci_priv.h int vfio_pci_set_power_state(struct
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+* keep it revoked until a reset succeeds. Plain vfio-pci is unchanged. +*/ +if (__vfio_pci_memory_enabled (vdev) && (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock);drivers/vfio/pci/vfio_pci_priv.h +0
-0@@ -78,6 +78,7 @@ int vfio_pci_set_power_state(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_priv.h int vfio_pci_set_power_state(struct · line 84
Quoted 7 lines
drivers/vfio/pci/vfio_pci_priv.h +1
-0pci_power_t state);void vfio_pci_zap_and_down_write_memory_lock (struct vfio_pci_core_device *vdev); +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev);u16 vfio_pci_memory_lock_and_enable (struct vfio_pci_core_device *vdev); void vfio_pci_memory_unlock_and_restore(struct
vfio_pci_core_device*vdev,Quoted 7 lines
drivers/vfio/pci/vfio_pci_priv.h +0
-0u16 cmd);(quoted diff) +0
-0
b/include/linux/vfio_pci_core.hindex39a28cc6ae8c..231679dead45100644include/linux/vfio_pci_core.h struct vfio_cxl_ops { · line 81
Quoted 8 lines
include/linux/vfio_pci_core.h +4
-0@@ -74,6 +74,10 @@ struct vfio_cxl_ops {void (*close_device)(struct vfio_pci_core_device *vdev);void (*reset_prepare)(struct vfio_pci_core_device *vdev);void (*reset_done)(struct vfio_pci_core_device *vdev); +/* Run the CXL reset (always clears CXL.mem) in place of FLR */ +int (*reset)(struct vfio_pci_core_device *vdev); +/* True while the HDM range is valid and its dma-buf may be armed */ +bool (*hdm_active)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */
Quoted 6 lines
include/linux/vfio_pci_core.h +0
-1struct module *owner; }; --2.25.1
RE: [PATCH v5 06/27] vfio/pci: Detect CXL devices and load the CXL provider on demand
In reply to Richard ChengQuoted 77 lines
-----Original Message-----
From: Richard Cheng <[email protected]>
Sent: Thursday, September 17, 2026 2:18 PM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 06/27] vfio/pci: Detect CXL devices and load the CXL provider on demandQuoted 17 lines · On Thu, Sep 17, 2026 at 12:05:19AM +0800, [email protected] wrote:
From: Manish Honap <[email protected]>
A CXL device needs the vfio-cxl callbacks, but pulling vfio-cxl and the CXL core in unconditionally would bloat every vfio-pci setup. Detect a CXL Type-2 device at bind and
request_module("vfio-cxl")only then, then hand the device to the registered ops.Each bound CXL device pins the provider through
vfio_pci_get_cxl_ops()(try_module_get) and drops it withvfio_pci_put_cxl_ops()at release, so vfio-cxl can unload once no CXL device is bound.If the provider is absent the device is driven as plain vfio-pci. A built-in provider whose initcall has not run yet is waited for with -EPROBE_DEFER; the deferred-probe machinery will retry the bind once the provider registers.
Quoted 4 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 84++++++++++++++++++++++++++++++++
Quoted 15 lines
include/linux/vfio_pci_core.h | 3 ++ 2 files changed, 87 insertions(+)drivers/vfio/pci/vfio_pci_core.c +3
-0@@ -2202,6 +2202,84 @@ void vfio_pci_core_unregister_cxl_ops(const struct vfio_cxl_ops *ops) }EXPORT_SYMBOL_GPL (vfio_pci_core_unregister_cxl_ops); +static const struct vfio_cxl_ops *vfio_pci_get_cxl_ops (void ) { +guard (rwsem_read)(&vfio_pci_cxl_ops_rwsem); +drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_unregister_cxl_ops(const · line 2217
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+if (vfio_pci_cxl_ops &&try_module_get (vfio_pci_cxl_ops->owner)) +return vfio_pci_cxl_ops; + +return NULL ; +} + +static void vfio_pci_put_cxl_ops (const struct vfio_cxl_ops *ops) { +module_put (ops->owner); +} +Anything guarantees that we can finish devres cleanup before dropping module ref ?
This function drops the module ref during
vfio_pci_remove(), while devres might still pending.
Quoted 8 lines
I see in patch 18 it register
vfio_cxl_release_hpa()as a devres callback, and it lives in vfio-cxl.
If this drops the last module ref, another process could unload vfio-cxl before devres invokes that callback.Best regards,
Richard Cheng.
Yes, I think it is possible that a concurrent unload can free the module text the callback points at.
For v6, I dropped the devm action entirely and released the exclusive HDM range in the provider .release op instead.
vfio_pci_core_cxl_release() calls ->release() and then vfio_pci_put_cxl_ops(), so the range is released synchronously before the module_put; this way no vfio-cxl callback outlives the module reference.
Thanks,
Manish
Quoted 120 lines
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_unregister_cxl_ops(const · line 2219
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0+/* +* A CXL Type-2 device advertises both CXL.cache and CXL.mem in its CXL DVSEC.
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_init_dev(struct vfio_device
Quoted 68 lines
drivers/vfio/pci/vfio_pci_core.c +65
-0+* pcie_is_cxl() is also true for Type-1 (cache only) and Type-3 (mem +only) +* devices, which the vfio-cxl provider does not handle, so confirm +the Type-2 +* identity before engaging it. +*/ +static bool vfio_pci_is_cxl_type2 (struct pci_dev *pdev) { +u16 dvsec, cap; + +if (!pcie_is_cxl (pdev)) +return false; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return false; + +if (pci_read_config_word (pdev, dvsec +PCI_DVSEC_CXL_CAP , &cap)) +return false; + +return (cap &PCI_DVSEC_CXL_CACHE_CAPABLE ) && + (cap &PCI_DVSEC_CXL_MEM_CAPABLE ); +} + +/* +* Load vfio-cxl on demand for a CXL Type-2 device and hand the +device to its +* ops. If the provider is absent the device is driven as plain +vfio-pci; a +* built-in provider whose initcall has not run yet is waited for +with +* -EPROBE_DEFER. +*/ +static int vfio_pci_core_cxl_init (struct vfio_pci_core_device *vdev) +{ +const struct vfio_cxl_ops *ops; +int ret; + +if (!vfio_pci_is_cxl_type2 (vdev->pdev)) +return 0 ; + +request_module ("vfio-cxl" ); + ops =vfio_pci_get_cxl_ops (); +if (!ops) +return IS_BUILTIN (CONFIG_VFIO_CXL ) ? -EPROBE_DEFER :0 ; + + ret = ops->init (vdev); +if (ret) { +vfio_pci_put_cxl_ops (ops); +return ret; + } + + vdev->cxl_ops = ops; +return 0 ; +} + +static void vfio_pci_core_cxl_release (struct vfio_pci_core_device +*vdev) { +if (!vdev->cxl_ops) +return ; + + vdev->cxl_ops->release (vdev); +vfio_pci_put_cxl_ops (vdev->cxl_ops); +} +int vfio_pci_core_init_dev (struct vfio_device *core_vdev) { struct vfio_pci_core_device *vdev =@@ -2223,6 +2301,10 @@ int vfio_pci_core_init_dev(struct vfio_device
*core_vdev)drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_release_dev(struct
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0init_rwsem (&vdev->memory_lock);xa_init (&vdev->ctx); + ret =vfio_pci_core_cxl_init (vdev); +if (ret) +return ret; +return 0 ; }EXPORT_SYMBOL_GPL (vfio_pci_core_init_dev);@@ -2232,6 +2314,8 @@ void vfio_pci_core_release_dev(struct
vfio_device*core_vdev)Quoted 20 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0struct vfio_pci_core_device *vdev = container_of(core_vdev, struct vfio_pci_core_device, vdev); +vfio_pci_core_cxl_release (vdev); +mutex_destroy (&vdev->igate);mutex_destroy (&vdev->ioeventfds_lock);kfree (vdev->region);(quoted diff) +0
-0
b/include/linux/vfio_pci_core.hindex9fe0d1a3a370..7f3a2bcb5830100644include/linux/vfio_pci_core.h +1
-0@@ -29,6 +29,7 @@ struct vfio_pci_core_device; struct vfio_pci_region;struct p2pdma_provider ;struct dma_buf_attachment ; +struct vfio_cxl_state ;struct vfio_pci_eventfd {struct eventfd_ctx *ctx;include/linux/vfio_pci_core.h struct vfio_pci_core_device { · line 116
Quoted 7 lines
include/linux/vfio_pci_core.h +2
-0@@ -109,6 +110,8 @@ struct vfio_pci_core_device {struct vfio_device vdev;struct pci_dev *pdev;const struct vfio_pci_device_ops *pci_ops; +const struct vfio_cxl_ops *cxl_ops; +struct vfio_cxl_state *cxl;void __iomem *barmap[PCI_STD_NUM_BARS ];bool bar_mmap_supported[PCI_STD_NUM_BARS];Quoted 5 lines
include/linux/vfio_pci_core.h +0
-1/* Flags modified at runtime - dedicated storage unit */ --2.25.1
Re: [PATCH v5 01/27] cxl/regs: Split the BAR block request and ioremap helpers
In reply to [email protected]Quoted 9 lines · On Thu, 17 Sep 2026 00:05:14 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
devm_cxl_iomap_block()claims a register block withdevm_request_mem_region()and then maps it withdevm_ioremap().A driver that already owns the parent BAR needs to skip the sub-block request while still mapping it, so the request and the map must be callable independently.
Quoted 6 lines
Split them into
devm_cxl_request_block()anddevm_cxl_ioremap_block().devm_cxl_iomap_block()keeps calling both in turn, so there is no functional change.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>
Jonathan Cameron
maintainerRe: [PATCH v5 02/27] cxl/regs: Let a BAR-owning driver own the component register block
In reply to [email protected]Quoted 10 lines · On Thu, 17 Sep 2026 00:05:15 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
cxl_map_component_regs()claims each mapped sub-block withdevm_request_mem_region(). A driver that already requested the whole component register BAR, such as vfio-cxl, would then collide with that claim and fail to map the HDM decoder and RAS blocks.Add
cxl_reg_map_add_owned_resource()so such a driver records the resource it already owns on the register map, and skip the sub-block request when the block falls within an owned resource.
This seems a slightly odd intermediate point.
The assumption is that if owned the whole bar is owned - hence only handle one owned region (no check if there is already one set?) yet the code will try to map it if we get a request for something outside of the region mapped (which can't exist).
This all makes me a little nervous.
Quoted 14 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/cxl/core/regs.c | 17 +++++++++++++++-- include/cxl/cxl.h | 2 ++ include/cxl/pci.h | 3 +++ 3 files changed, 20 insertions(+), 2 deletions(-)drivers/cxl/core/regs.c +0
-0
drivers/cxl/core/regs.c void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr, · line 222
Quoted 7 lines
drivers/cxl/core/regs.c +4
-0@@ -216,6 +216,13 @@ void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr, }EXPORT_SYMBOL_NS_GPL (devm_cxl_iomap_block,"CXL" ); +void cxl_reg_map_add_owned_resource (struct cxl_register_map *map, +struct resource *res) +{ + map->owned = res;
As above, I think this needs a sanity check that a future driver author doesn't think they can just add lots of these. Absolute minimum is add some Docs.
Quoted 15 lines
drivers/cxl/core/regs.c +4
-0+} +EXPORT_SYMBOL_NS_GPL (cxl_reg_map_add_owned_resource,"CXL" ); +int cxl_map_component_regs (const struct cxl_register_map *map,struct cxl_component_regs *regs,unsigned long map_mask)@@ -234,6 +241,7 @@ int cxl_map_component_regs(const struct cxl_register_map *map,struct mapinfo *mi = &mapinfo[i];resource_size_t addr;resource_size_t length; +struct resource res;if (!mi->rmap->valid)continue ;
drivers/cxl/core/regs.c int cxl_map_component_regs(const struct cxl_register_map *map, · line 254
Quoted 8 lines
drivers/cxl/core/regs.c +3
-2@@ -241,8 +249,13 @@ int cxl_map_component_regs(const struct cxl_register_map *map,continue ; addr = map->resource + mi->rmap->offset; length = mi->rmap->size; - *(mi->addr) =devm_cxl_iomap_block (host, addr, length); -if (!*(mi->addr)) + res =DEFINE_RES_MEM (addr, length); + +if (map->owned &&resource_contains (map->owned, &res))
If assumption is whole bar or none, should we just fail if map->owned is true but the resource contains fails?
Quoted 7 lines
drivers/cxl/core/regs.c +4
-0+ *mi->addr =devm_cxl_ioremap_block (host, addr, length); + else + *mi->addr =devm_cxl_iomap_block (host, addr, length); +if (!*mi->addr)return -ENOMEM ; }
Jonathan Cameron
maintainerRe: [PATCH v5 03/27] cxl: Move component register defines to uapi/cxl/cxl_regs.h
In reply to [email protected]Quoted 6 lines · On Thu, 17 Sep 2026 00:05:16 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
The CXL component and HDM decoder register layout is spec-defined and is needed by a userspace consumer that cannot include the private
drivers/cxl/cxl.h
Quoted 8 lines
The vfio-cxl selftest (
tools/testing/selftests/vfio/vfio_cxl_type2_test.c) builds against installed uapi headers only. Move those defines to a new uapi header, the way PCI register offsets live in uapipci_regs.h, and include it fromdrivers/cxl/cxl.hso existing in-kernel users are unchanged.No functional change; the GENMASK and BIT forms are written as plain values as a uapi header requirement.
I thought we'd fixed that these days... And indeed we have see include/uapi/linux/bits.h for __GENMASK()
and include/uapi/linux/const.h for _BITUL()
Both are in use in pci_regs.h for instance.
Jonathan
Alex Williamson
maintainerRe: [PATCH v5 18/27] vfio/cxl: Expose the HDM memory region to the guest
In reply to [email protected]Quoted 230 lines · On Thu, 17 Sep 2026 00:05:31 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 guest maps the device HDM memory to use its coherent CXL.mem. The HDM memory is a host physical range with no struct page, so the guest and KVM need a write-back mapping of it.
Register the range as an mmap-able region under
VFIO_REGION_TYPE_PCI_VENDOR_TYPEwith the CXL vendor id rather than a bespoke region type, per open becausevfio_pci_core_disable()tears down all dynamic regions on close.Own the resolved host physical range exclusively (
IORESOURCE_EXCLUSIVE) so nothing,/dev/memincluded, can map a conflicting cacheable alias that would fault the host once the range is mapped write-back. There is no devm form of the exclusive request, so pair it with a devm release action.
vfio_pci_zap_bars()only unmaps the fixed PCI BAR range, so revoke mmap-capable device-specific regions there too. Otherwise a Memory-Space disable, a D3 transition, or a reset would leave the guest a live mapping into quiesced device memory; the fault handler re-gates on device state before it inserts a pfn again.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 186 +++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_core.c | 37 ++++++ include/linux/vfio_pci_core.h | 1 + include/uapi/linux/vfio.h | 4 + 4 files changed, 228 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +159
-0@@ -5,9 +5,13 @@* Copyright (c) 2026 NVIDIA Corporation & Affiliates */ +#include <linux/cleanup.h> +#include <linux/io.h> +#include <linux/mm.h> #include <linux/module.h> #include <linux/pci.h> #include <linux/range.h> +#include <linux/uaccess.h> #include <linux/vfio_pci_core.h> #include <cxl/cxl.h> #include <cxl/pci.h> @@ -17,13 +21,144 @@* @cxlds: CXL device state; kept first for devm_cxl_dev_state_create() * @cxlmd: memory device joined to the CXL topology at bind * @hpa_range: host physical range of the HDM region +* @hdm_valid: true when host CPU access to the HDM range is safe; under memory_lock */ struct vfio_cxl_state {struct cxl_dev_state cxlds;struct cxl_memdev *cxlmd;struct range hpa_range; +bool hdm_valid; }; +static unsigned long vfio_cxl_mem_pgoff (struct vm_area_struct *vma, +unsigned long addr) +{ +unsigned long mask = (1U << (VFIO_PCI_OFFSET_SHIFT -PAGE_SHIFT )) -1 ; + +return (vma->vm_pgoff & mask) + ((addr - vma->vm_start) >>PAGE_SHIFT ); +} + +static vm_fault_t vfio_cxl_mem_huge_fault (struct vm_fault *vmf, +unsigned int order) +{ +struct vm_area_struct *vma = vmf->vma; +struct vfio_pci_core_device *vdev = vma->vm_private_data; +struct vfio_cxl_state *cxl = vdev->cxl; +unsigned long addr =ALIGN_DOWN (vmf->address,PAGE_SIZE << order); +unsigned long pfn =PHYS_PFN (cxl->hpa_range.start) + +vfio_cxl_mem_pgoff (vma, addr); +vm_fault_t ret =VM_FAULT_FALLBACK ; + +if (is_aligned_for_order (vma, addr, pfn, order)) { + scoped_guard(rwsem_read, &vdev->memory_lock) { +/* +* Insert a PFN only for a known-good decoder whose +* media is ready and whose Memory-Space is enabled. +*/ +if (__vfio_pci_memory_enabled (vdev) && + cxl->hdm_valid && cxl->cxlds.media_ready) + ret =vfio_pci_vmf_insert_pfn (vdev, vmf, pfn, + order); +else + ret =VM_FAULT_SIGBUS ; + } + } + +return ret; +} + +static vm_fault_t vfio_cxl_mem_fault (struct vm_fault *vmf) +{ +return vfio_cxl_mem_huge_fault (vmf,0 ); +} + +static const struct vm_operations_struct vfio_cxl_mem_vm_ops = { + .fault = vfio_cxl_mem_fault, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .huge_fault = vfio_cxl_mem_huge_fault, +#endif +}; + +static int vfio_cxl_mem_mmap (struct vfio_pci_core_device *vdev, +struct vfio_pci_region *region, +struct vm_area_struct *vma) +{ +unsigned long mask = (1U << (VFIO_PCI_OFFSET_SHIFT -PAGE_SHIFT )) -1 ; +u64 req_start = (vma->vm_pgoff & mask) <<PAGE_SHIFT ; +u64 req_len = vma->vm_end - vma->vm_start; + +if (req_start + req_len > region->size) +return -EINVAL ; + +/* +* CXL.mem is coherent memory, so leave the mapping write-back cacheable. +*/ +vm_flags_set (vma,VM_IO |VM_PFNMAP |VM_DONTEXPAND |VM_DONTDUMP ); + vma->vm_ops = &vfio_cxl_mem_vm_ops; + vma->vm_private_data = vdev; + +return 0 ; +} + +static ssize_t vfio_cxl_mem_rw (struct vfio_pci_core_device *vdev, +char __user *buf,size_t count,loff_t *ppos, +bool iswrite) +{ +struct vfio_cxl_state *cxl = vdev->cxl; +u64 pos = *ppos &VFIO_PCI_OFFSET_MASK ; +void *mem; +ssize_t done; + +if (pos >=range_len (&cxl->hpa_range)) +return -EINVAL ; + count =min_t (size_t, count,range_len (&cxl->hpa_range) - pos); + + scoped_guard(rwsem_read, &vdev->memory_lock) { +/* +* Same gate as the fault path: only touch the HDM range with +* the decoder in a known-good state AND Memory-Space enabled, +* or a host CPU access aborts as a fatal SError. +*/ +if (!cxl->hdm_valid || !__vfio_pci_memory_enabled (vdev)) +return -EIO ; + + mem =memremap (cxl->hpa_range.start + pos, count,MEMREMAP_WB ); +if (!mem) +return -ENOMEM ; +if (iswrite) + done =copy_from_user (mem, buf, count) ? -EFAULT : count; +else + done =copy_to_user (buf, mem, count) ? -EFAULT : count; +memunmap (mem); + } +if (done >0 ) + *ppos += done; + +return done; +} + +/* +* The CXL regions carry no per-region state (region->data is the shared, +* devm-managed vfio_cxl_state), so releasing a region is a no-op. +*/ +static void vfio_cxl_region_release (struct vfio_pci_core_device *vdev, +struct vfio_pci_region *region) +{ +} + +static const struct vfio_pci_regops vfio_cxl_mem_regops = { + .rw = vfio_cxl_mem_rw, + .mmap = vfio_cxl_mem_mmap, + .release = vfio_cxl_region_release, +}; + +static void vfio_cxl_release_hpa (void *data) +{ +struct vfio_cxl_state *cxl = data; + +release_mem_region (cxl->hpa_range.start,range_len (&cxl->hpa_range)); +} +static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;@@ -120,6 +255,21 @@ static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) goto err; } +/* +* Claim the range IORESOURCE_EXCLUSIVE so no conflicting cacheable +* alias can fault the host once it is mapped write-back; there is no +* devm form, so pair it with a devm release action. +*/ +if (!request_mem_region_exclusive (cxl->hpa_range.start, +range_len (&cxl->hpa_range), +"vfio-cxl-hdm" )) { + ret = -EBUSY ; + goto err; + } + ret =devm_add_action_or_reset (&pdev->dev, vfio_cxl_release_hpa, cxl); +if (ret) + goto err; + cxl->cxlmd = cxlmd;devres_close_group (&pdev->dev,NULL );@@ -143,13 +293,49 @@ static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev) vdev->cxl =NULL ; } +static int vfio_cxl_add_region (struct vfio_pci_core_device *vdev,u32 subtype, +const struct vfio_pci_regops *ops,size_t size, +u32 flags) +{ +u32 type =VFIO_REGION_TYPE_PCI_VENDOR_TYPE |PCI_VENDOR_ID_CXL ; + +return vfio_pci_core_register_dev_region (vdev, type, subtype, ops, + size, flags, vdev->cxl); +}
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev) · line 314
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +8
-0+static int vfio_cxl_open_device (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; +int ret; + +/* +* vfio_pci_core_disable() frees all dynamic regions on close, so register +* them here per open rather than at bind. A failed first open never +* reaches close_device(), so unwind on error.
There's no unwind here.
Quoted 15 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +15
-0+*/ + ret =vfio_cxl_add_region (vdev,VFIO_REGION_SUBTYPE_CXL_MEM , + &vfio_cxl_mem_regops,range_len (&cxl->hpa_range), +VFIO_REGION_INFO_FLAG_READ | +VFIO_REGION_INFO_FLAG_WRITE | +VFIO_REGION_INFO_FLAG_MMAP ); +if (ret) +return ret; + +/* +* The decoder is firmware-committed, so host access to the HDM range is +* safe. Open the access gate; reset and power transitions clear it until +* the decoder is restored. +*/ + cxl->hdm_valid = true;
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev) · line 339
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +4
-0+return 0 ; }static void vfio_cxl_close_device (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; + + cxl->hdm_valid = false; }
If hdm_valid is set on open_device and cleared on close_device, how is this not redundant to open_count? It's totally superfluous everywhere it's tested in this patch.
Quoted 16 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0static void vfio_cxl_reset_prepare (struct vfio_pci_core_device *vdev)drivers/vfio/pci/vfio_pci_core.c +6
-0@@ -1295,6 +1295,23 @@ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, }EXPORT_SYMBOL_GPL (vfio_pci_core_register_dev_region); +/* +* Unregister the most recently registered dynamic region. Used to unwind a +* partially built region set on an open-time error; regions are otherwise +* released together in vfio_pci_core_disable(). +*/ +void vfio_pci_core_unregister_dev_region (struct vfio_pci_core_device *vdev)
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, · line 1313
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+{ +struct vfio_pci_region *region; + +if (WARN_ON (!vdev->num_regions)) +return ; + + region = &vdev->region[--vdev->num_regions]; + region->ops->release (vdev, region); +} +EXPORT_SYMBOL_GPL (vfio_pci_core_unregister_dev_region);
This is not a good interface, implicitly popping the last dev region added. The whole interface should change to push and pop semantics if we're doing this, but we shouldn't do this. It would be better to ask to remove a specific region, identified by some compliment of the creation parameters, with a memmove + collapse on the array. Or perhaps this justifies another data structure.
This is also unused in this patch. Why's it added here? This patch already does too much. We could have lead with read/write access and followed with mmap support in another patch.
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +1
-0+static int vfio_pci_info_atomic_cap (struct vfio_pci_core_device *vdev,struct vfio_info_cap *caps) {@@ -1972,8 +1989,28 @@ static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev)loff_t start =VFIO_PCI_INDEX_TO_OFFSET (VFIO_PCI_BAR0_REGION_INDEX );loff_t end =VFIO_PCI_INDEX_TO_OFFSET (VFIO_PCI_ROM_REGION_INDEX );loff_t len = end - start;
drivers/vfio/pci/vfio_pci_core.c static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev) · line 2001
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+unsigned int i;unmap_mapping_range (core_vdev->inode->i_mapping, start, len, true); + +/* +* The unmap above covers the PCI BARs; mmap-capable device-specific +* regions (e.g. a vfio-cxl HDM window) sit above that range, so revoke +* them here too, or a Memory-Space disable, D3/PM transition, or reset +* would leave the guest a live mapping into quiesced device memory. +* Callers hold memory_lock, so the region array is stable.
memory_lock doesn't protect the region array, it's allocated at init/open and considered stable. Like v4, this is arguing where it's convenient that we need to guard against PM transitions while also preventing PM transitions. Why don't we just support PM?
drivers/vfio/pci/vfio_pci_core.c static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev) · line 2013
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c +12
-0+*/ +for (i =0 ; i < vdev->num_regions; i++) { +struct vfio_pci_region *region = &vdev->region[i]; +loff_t roff; + +if (!(region->flags &VFIO_REGION_INFO_FLAG_MMAP )) +continue ; + + roff =VFIO_PCI_INDEX_TO_OFFSET (VFIO_PCI_NUM_REGIONS + i); +unmap_mapping_range (core_vdev->inode->i_mapping, roff, + region->size, true); + }
There's an assumption here that doesn't seem well founded that a device specific region that support mmap is necessarily dependent on the memory enable state of the device. We could have chosen to report the coherent memory nvgrace uses as a device specific region, we know it's not tied to memory enable. We could support mmap on something like the IGD OpRegion if we chose to map a whole page for it. There's an implicit dependency here that I don't see baked in to simply supporting mmap. Thanks,
Alex
Quoted 30 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0}void vfio_pci_zap_and_down_write_memory_lock (struct vfio_pci_core_device *vdev)include/linux/vfio_pci_core.h +1
-0@@ -198,6 +198,7 @@ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops, size_t size, u32 flags, void *data); +void vfio_pci_core_unregister_dev_region (struct vfio_pci_core_device *vdev);void vfio_pci_core_close_device (struct vfio_device *core_vdev);int vfio_pci_core_init_dev (struct vfio_device *core_vdev);void vfio_pci_core_release_dev (struct vfio_device *core_vdev);include/uapi/linux/vfio.h +4
-0@@ -370,6 +370,10 @@ struct vfio_region_info_cap_type {*/ #define VFIO_REGION_SUBTYPE_IBM_NVLINK2_ATSD (1) +/* CXL Type-2 device (0x1e98) sub-types for VFIO_REGION_TYPE_PCI_VENDOR_TYPE */ +/* CXL.mem HDM region of a Type-2 device, mmap-able */ +#define VFIO_REGION_SUBTYPE_CXL_MEM (1) +/* sub-types for VFIO_REGION_TYPE_GFX */ #define VFIO_REGION_SUBTYPE_GFX_EDID (1)
Alex Williamson
maintainerRe: [PATCH v5 20/27] vfio/cxl: Expose the HDM decoder registers read-only to the guest
In reply to [email protected]Quoted 79 lines · On Thu, 17 Sep 2026 00:05:33 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 guest reads the HDM decoder registers to learn the HDM region it was handed. Those registers live in the component BAR that vfio-pci owns.
Map the decoder block at bind: its location comes from the
pdev->hdmenumeration cache, and vfio-pci owns the BAR, so map it without claiming the block and expose it as a second, read-only region underVFIO_REGION_TYPE_PCI_VENDOR_TYPEwith the CXL vendor id, registered per open like the HDM memory region.Serve reads live from the mapped block and absorb writes without forwarding them to hardware.
Registering a second region is the first point at which an open-time failure must unwind an already-registered region, so add
vfio_pci_core_unregister_dev_region()to drop the most recently registered region, and use it to unwind the HDM memory region if the decoder region fails to register.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 81 +++++++++++++++++++++++++++- include/uapi/linux/vfio.h | 2 + 2 files changed, 82 insertions(+), 1 deletion(-)drivers/vfio/pci/cxl/vfio_cxl_core.c +29
-0@@ -24,6 +24,8 @@* @cxlmd: memory device joined to the CXL topology at bind * @hpa_range: host physical range of the HDM region * @hdm_pfn_space: HDM-region pfn range registered with memory_failure() +* @hdm_regs: mapped HDM decoder registers, read live by the decoder region +* @hdm_len: length of the HDM decoder register block * @hdm_valid: true when host CPU access to the HDM range is safe; under memory_lock */ struct vfio_cxl_state {@@ -31,6 +33,8 @@ struct vfio_cxl_state {struct cxl_memdev *cxlmd;struct range hpa_range;struct pfn_address_space hdm_pfn_space; +void __iomem *hdm_regs; +u32 hdm_len;bool hdm_valid; };@@ -212,6 +216,56 @@ static int vfio_cxl_register_pfn_space(struct vfio_pci_core_device *vdev)return register_pfn_address_space (&cxl->hdm_pfn_space); } +static ssize_t vfio_cxl_comp_rw (struct vfio_pci_core_device *vdev, +char __user *buf,size_t count,loff_t *ppos, +bool iswrite) +{ +struct vfio_cxl_state *cxl = vdev->cxl; +loff_t pos = *ppos &VFIO_PCI_OFFSET_MASK ; +void *tmp; + +if (pos >= cxl->hdm_len) +return -EINVAL ; + +/* The decoder registers take only aligned dword accesses. */ +if (pos %sizeof (u32) || count %sizeof (u32)) +return -EINVAL ; + + count =min_t (size_t, count, cxl->hdm_len - pos); + +/* +* The host committed and locked the physical decoder before the guest +* saw the device, so the guest never drives it: absorb writes without +* forwarding them to hardware. The guest programs a GPA that the VMM +* virtualizes; reads return the live registers, which already report the +* decoder committed. BASE_LOW and BASE_HIGH carry the host HPA, visible +* only to the trusted VMM that virtualizes it away from the guest. +*/
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_register_pfn_space(struct vfio_pci_core_device *vdev) · line 253
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+if (iswrite) { + *ppos += count; +return count; + } + + tmp =kmalloc (count,GFP_KERNEL ); +if (!tmp) +return -ENOMEM ; + +memcpy_fromio (tmp, cxl->hdm_regs + pos, count);
There's no memory-enabled gate on this access to mapped BAR space.
Quoted 64 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +38
-1+if (copy_to_user (buf, tmp, count)) { +kfree (tmp); +return -EFAULT ; + } +kfree (tmp); + + *ppos += count; +return count; +} + +static const struct vfio_pci_regops vfio_cxl_comp_regops = { + .rw = vfio_cxl_comp_rw, + .release = vfio_cxl_region_release, +}; +static void vfio_cxl_release_hpa (void *data) {struct vfio_cxl_state *cxl = data;@@ -299,6 +353,21 @@ static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) goto err; } +/* +* Map the HDM decoder registers so the decoder region can read them +* live. The block location comes from the enumeration cache in +* pdev->hdm; vfio-pci owns the BAR, so map without claiming the block. +*/ + cxl->hdm_regs =devm_ioremap (&pdev->dev, +pci_resource_start (pdev, pdev->hdm->hdm_bar) + + pdev->hdm->hdm_offset, pdev->hdm->hdm_size); +if (!cxl->hdm_regs) { + ret = -ENOMEM ; + goto err; + } + + cxl->hdm_len = pdev->hdm->hdm_size; +/* * A Type-2 accelerator has no mailbox and no media-ready register, so * set media ready directly. @@ -381,6 +450,13 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)if (ret)return ret; + ret =vfio_cxl_add_region (vdev,VFIO_REGION_SUBTYPE_CXL_COMP_REGS , + &vfio_cxl_comp_regops, cxl->hdm_len, +VFIO_REGION_INFO_FLAG_READ | +VFIO_REGION_INFO_FLAG_WRITE ); +if (ret) + goto err_unregister_mem; +/* * The HDM region is advertised mmap-able, so a fd holder can fault its * struct-page-less device memory in from the host CPU. Register it with @@ -389,7 +465,7 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)*/ ret =vfio_cxl_register_pfn_space (vdev);if (ret && ret != -EOPNOTSUPP ) - goto err_unregister_mem; + goto err_unregister_comp;/* * The decoder is firmware-committed, so host access to the HDM range is
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev) · line 483
Quoted 8 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0@@ -400,6 +476,9 @@ static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)return 0 ; +err_unregister_comp: +vfio_pci_core_unregister_dev_region (vdev); + err_unregister_mem:vfio_pci_core_unregister_dev_region (vdev);
As noted previously, this is a bad interface, this is why. Thanks,
Alex
Quoted 14 lines
include/uapi/linux/vfio.h +2
-0@@ -373,6 +373,8 @@ struct vfio_region_info_cap_type {/* CXL Type-2 device (0x1e98) sub-types for VFIO_REGION_TYPE_PCI_VENDOR_TYPE */ /* CXL.mem HDM region of a Type-2 device, mmap-able */ #define VFIO_REGION_SUBTYPE_CXL_MEM (1) +/* CXL HDM decoder registers: read live, guest writes are absorbed */ +#define VFIO_REGION_SUBTYPE_CXL_COMP_REGS (2)/* sub-types for VFIO_REGION_TYPE_GFX */ #define VFIO_REGION_SUBTYPE_GFX_EDID (1)
Alex Williamson
maintainerRe: [PATCH v5 08/27] vfio/pci: Fall back to plain vfio-pci when CXL init fails
In reply to [email protected]Quoted 30 lines · On Thu, 17 Sep 2026 00:05:21 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 device is also a valid plain vfio-pci device. If the vfio-cxl provider cannot bring it up as Type-2, failing the bind would regress a device that previously worked as plain vfio-pci.
Make a non-deferred CXL init failure non-fatal: log it, drop the provider reference, leave
cxl_opsNULL, and continue driving the device as plain vfio-pci. The -EPROBE_DEFER path added earlier still retries rather than falling back.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)drivers/vfio/pci/vfio_pci_core.c +5
-0@@ -2271,9 +2271,17 @@ static int vfio_pci_core_cxl_init(struct vfio_pci_core_device *vdev)vfio_pci_put_cxl_ops (ops);return ret; } +/* +* Any other failure is non-fatal: a CXL device that cannot be brought +* up as Type-2 still works as plain vfio-pci, so log and continue +* rather than failing the bind. +*/
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_cxl_init(struct vfio_pci_core_device *vdev) · line 2287
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +4
-1if (ret) { +pci_warn (vdev->pdev, +"CXL init failed (%d), continuing as plain vfio-pci\n" , + ret);vfio_pci_put_cxl_ops (ops); -return ret; +return 0 ; } vdev->cxl_ops = ops;
Why isn't this the initial implementation in patch 6? I don't fully understand why patches 7 and 8 are circling back to modify the broken behavior established in patch 6 rather than resolving it in the patch where it was introduced.
Perhaps rephrasing the message, "... continuing without CXL support". "plain vfio-pci" doesn't mean anything to a user that only knows to use vfio-pci. Thanks,
Alex
Alex Williamson
maintainerRe: [PATCH v5 09/27] vfio/pci: Add a generic excluded-range list
In reply to [email protected]Quoted 8 lines · On Thu, 17 Sep 2026 00:05:22 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
The MSI-X table is virtualized in
vfio_pci_bar_rw()by an open-coded x_start/x_end window that fills reads with -1 and drops writes. Now that a generic excluded-range list expresses the same fill/drop behavior, register the MSI-X table as a read and write excluded range instead of special-casing it in the read/write path.
Quoted 5 lines
Add the range when the MSI-X capability is parsed in
vfio_pci_core_enable()and clear the list invfio_pci_core_disable()alongside the config teardown. The read/write path now relies solely onvfio_pci_bar_find_exclusion(), so MSI-X and a provider's (e.g. vfio-cxl) trapped registers share one mechanism.
This is kind of a backwards introduction.
"Now that a generic excluded-range list expresses the same fill/drop behavior, register the MSI-X table as a read and write excluded range instead of special-casing it in the read/write path."
The generic excluded-range list doesn't exist now, we're adding it here. We don't change MSI-X to use it until the next patch. We're not doing anything noted in the second paragraph in this patch either. The next patch has a nearly identical commit log, please be precise in what each patch does.
Quoted 24 lines
No functional change.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 209 +++++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_priv.h | 9 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 8 ++ include/linux/vfio_pci_core.h | 14 +++ 4 files changed, 240 insertions(+)drivers/vfio/pci/vfio_pci_core.c +1
-0@@ -24,6 +24,7 @@#include <linux/pci.h> #include <linux/pm_runtime.h> #include <linux/slab.h> +#include <linux/sort.h> #include <linux/types.h> #include <linux/uaccess.h> #include <linux/vgaarb.h>
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1022
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0@@ -1012,6 +1013,204 @@ static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,return vfio_info_add_capability (caps, &header,sizeof (header)); } +struct vfio_pci_excluded_range { +struct list_head entry; +int bar; +u64 start; +u64 size; +u32 flags; +};
Better packed to move the bar to the end, it only needs to be a u8, but is it instead better to make a per-BAR list and keep it sorted on insert to avoid the runtime complexity?
There should be a comment somewhere relative to the list only being filled at the already serialized open_device and therefore safe for lockless access runtime.
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +9
-0+ +int vfio_pci_core_add_excluded_range (struct vfio_pci_core_device *vdev,int bar, +u64 start,u64 size,u32 flags) +{ +struct vfio_pci_excluded_range *range; + + range =kzalloc_obj (*range); +if (!range) +return -ENOMEM ;
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1041
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ + range->bar = bar; + range->start = start; + range->size = size; + range->flags = flags; +list_add_tail (&range->entry, &vdev->excluded_ranges); + +return 0 ; +} +EXPORT_SYMBOL_GPL (vfio_pci_core_add_excluded_range);
If this does an ordered insert, I think we could also require that ranges cannot overlap. Everything we're considering currently, MSI-X vector table, HDM registers, are distinct. They could theoretically overlap relative to the sparse mmap capability once page aligned, but the excluded range list itself should only contain distinct, non-overlapping ranges, and I think that simplifies things a little.
Quoted 25 lines
drivers/vfio/pci/vfio_pci_core.c +25
-0+ +static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device *vdev) +{ +struct vfio_pci_excluded_range *range, *tmp; + +list_for_each_entry_safe (range, tmp, &vdev->excluded_ranges, entry) { +list_del (&range->entry); +kfree (range); + } +} + +bool vfio_pci_bar_find_exclusion (struct vfio_pci_core_device *vdev,int bar, +loff_t pos,size_t count,bool iswrite, +size_t *x_start,size_t *x_end) +{ +u32 want = iswrite ?VFIO_PCI_EXCLUDE_WRITE :VFIO_PCI_EXCLUDE_READ ; +struct vfio_pci_excluded_range *range; +bool found = false; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != bar || !(range->flags & want)) +continue ; +if (pos < range->start + range->size && + pos + count > range->start) { +/*
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1076
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+* A BAR can carry more than one excluded window (e.g. +* the MSI-X table and a CXL HDM decoder block). Return +* the overlapping window with the lowest start so the +* caller can walk them in order. +*/ +if (!found || range->start < *x_start) { + *x_start = range->start; + *x_end = range->start + range->size; + found = true; + }
For example, we wouldn't need to iterate if the list were sorted.
Quoted 28 lines
drivers/vfio/pci/vfio_pci_core.c +28
-0+ } + } + +return found; +} + +/* True when [start, start + len) on @bar overlaps an mmap-excluded range. */ +static bool vfio_pci_bar_mmap_excluded (struct vfio_pci_core_device *vdev, +int bar,u64 start,u64 len) +{ +struct vfio_pci_excluded_range *range; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != bar || + !(range->flags &VFIO_PCI_EXCLUDE_MMAP )) +continue ; +if (start < range->start + range->size && + start + len > range->start) +return true; + } + +return false; +} + +/* A page-aligned mmap hole, derived from an mmap-excluded range. */ +struct vfio_pci_mmap_hole { +u64 start; +u64 end;
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1114
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+}; + +static int vfio_pci_mmap_hole_cmp (const void *a,const void *b) +{ +const struct vfio_pci_mmap_hole *x = a, *y = b; + +if (x->start < y->start) +return -1 ; +return x->start > y->start; +}
We wouldn't need sorting if we just did an ordered insert.
Quoted 88 lines
drivers/vfio/pci/vfio_pci_core.c +88
-0+ +/* +* Advertise the BAR as mmappable minus every page-aligned mmap-excluded hole. +* A BAR can carry several holes at unrelated offsets (for example an MSI-X +* table and one or more trapped CXL component sub-blocks, which the CXL spec +* locates by pointer, not at fixed offsets). +* Collect the holes, page-align and sort them, coalesce any that overlap or +* touch, and advertise the gaps. +*/ +static int vfio_pci_excluded_sparse_cap (struct vfio_pci_core_device *vdev, +int index,struct vfio_info_cap *caps) +{ +u64 bar_len =pci_resource_len (vdev->pdev, index); +struct vfio_region_info_cap_sparse_mmap *sparse; +struct vfio_pci_excluded_range *range; +struct vfio_pci_mmap_hole *holes; +int nr_holes =0 , nr_areas =0 , i, j; +size_t size; +u64 pos; +int ret; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) +if (range->bar == index && + (range->flags &VFIO_PCI_EXCLUDE_MMAP )) + nr_holes++; + +if (!nr_holes) +return 0 ; + + holes =kmalloc_array (nr_holes,sizeof (*holes),GFP_KERNEL ); +if (!holes) +return -ENOMEM ; + +/* +* mmap is page granular, so each hole rounds out to the page boundaries +* enclosing its excluded sub-range. The byte-granular exclusion still +* governs the fault and read/write paths; only the advertised mmap areas +* round to whole pages. +*/ + i =0 ; +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != index || + !(range->flags &VFIO_PCI_EXCLUDE_MMAP )) +continue ; + holes[i].start =ALIGN_DOWN (range->start,PAGE_SIZE ); + holes[i].end =ALIGN (range->start + range->size,PAGE_SIZE ); + i++; + } + +sort (holes, nr_holes,sizeof (*holes), vfio_pci_mmap_hole_cmp,NULL ); + +/* Coalesce holes that overlap or touch after page alignment. */ +for (i =0 , j =0 ; i < nr_holes; i++) { +if (j && holes[i].start <= holes[j -1 ].end) + holes[j -1 ].end =max (holes[j -1 ].end, holes[i].end); +else + holes[j++] = holes[i]; + } + nr_holes = j; + +/* One mmappable area per gap: before, between, and after the holes. */ +for (i =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) + nr_areas++; + pos = holes[i].end; + } +if (pos < bar_len) + nr_areas++; + + size =struct_size (sparse, areas, nr_areas); + sparse =kzalloc (size,GFP_KERNEL ); +if (!sparse) { +kfree (holes); +return -ENOMEM ; + } + + sparse->header.id =VFIO_REGION_INFO_CAP_SPARSE_MMAP ; + sparse->header.version =1 ; + sparse->nr_areas = nr_areas; + +for (i =0 , j =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = holes[i].start - pos; + j++; + } + pos = holes[i].end; + }
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1212
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+if (pos < bar_len) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = bar_len - pos; + } + +kfree (holes); + ret =vfio_info_add_capability (caps, &sparse->header, size); +kfree (sparse); +return ret; +}
I imagine this could be simplified quite a bit:
- walk the list once, count mmap exclusions, if none return
- allocate sparse structure assuming # mmap exclusions + 1 == # areas
- walk list again aligning to page alignment, fill in areas, coalesce as needed, set resulting nr_areas.
Quoted 68 lines
drivers/vfio/pci/vfio_pci_core.c +11
-0+ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops,@@ -1160,6 +1359,10 @@ int vfio_pci_ioctl_get_region_info(struct vfio_device *core_vdev,if (ret)return ret; } + ret =vfio_pci_excluded_sparse_cap (vdev, info->index, + caps); +if (ret) +return ret; }break ;@@ -1853,6 +2056,10 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vmaif (req_start + req_len > phys_len)return -EINVAL ; +/* An excluded sub-range is reachable only through its trap, not mmap. */ +if (vfio_pci_bar_mmap_excluded (vdev, index, req_start, req_len)) +return -EINVAL ; +/* * Ensure the BAR resource region is reserved for use. */ @@ -2317,6 +2524,7 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)INIT_LIST_HEAD (&vdev->dmabufs);init_rwsem (&vdev->memory_lock);xa_init (&vdev->ctx); +INIT_LIST_HEAD (&vdev->excluded_ranges); ret =vfio_pci_core_cxl_init (vdev);if (ret)@@ -2332,6 +2540,7 @@ void vfio_pci_core_release_dev(struct vfio_device *core_vdev) container_of(core_vdev, struct vfio_pci_core_device, vdev);vfio_pci_core_cxl_release (vdev); +vfio_pci_free_excluded_ranges (vdev);mutex_destroy (&vdev->igate);mutex_destroy (&vdev->ioeventfds_lock);drivers/vfio/pci/vfio_pci_priv.h +9
-0@@ -44,6 +44,15 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,ssize_t vfio_pci_bar_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite); +/* +* If a read (or write) to [pos, pos + count) on @bar overlaps an excluded +* range, report the byte window do_io_rw() should fill with -1 (or drop) and +* return true. A single access spans at most one such window. +*/ +bool vfio_pci_bar_find_exclusion (struct vfio_pci_core_device *vdev,int bar, +loff_t pos,size_t count,bool iswrite, +size_t *x_start,size_t *x_end); +#ifdef CONFIG_VFIO_PCI_VGA ssize_t vfio_pci_vga_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite);drivers/vfio/pci/vfio_pci_rdwr.c +0
-0@@ -261,6 +261,14 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, x_end = vdev->msix_offset + vdev->msix_size;
drivers/vfio/pci/vfio_pci_rdwr.c ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, · line 271
Quoted 10 lines
drivers/vfio/pci/vfio_pci_rdwr.c +8
-0} +/* +* A provider-excluded sub-range is filled with -1 on read and dropped on +* write for the same reason: the guest reaches it only through the trap. +* An access spans at most one exclusion window. +*/ +vfio_pci_bar_find_exclusion (vdev, bar, pos, count, iswrite, + &x_start, &x_end); +
This is not well integrated, we're stomping on the MSI-X and ROM set exclusion range just above. As a result, we likely need to integrate support for both at the same time, and we also need to address the multiple exclusion range per access range, that's only in the next patch for some reason.
I don't think we want to move the pci_map_rom() call elsewhere, we try to do it in correlation to the access. People are crazy enough to update device firmware while in use as well, so I'm not sure the length read from the ROM is static. Maybe this should instead be handled as a hard stop at the end of the BAR vs a soft stop at the end of the data, filled with -1 on read, and have that live outside the excluded range list? It needs some finesse, this is clunky.
Also, what about ioeventfds and dmabufs? Thanks,
Alex
Quoted 35 lines
drivers/vfio/pci/vfio_pci_rdwr.c +0
-0done =vfio_pci_core_do_io_rw (vdev, res->flags &IORESOURCE_MEM , io, buf, pos, count, x_start, x_end, iswrite, max_width);include/linux/vfio_pci_core.h +14
-0@@ -162,6 +162,7 @@ struct vfio_pci_core_device {struct notifier_block nb;struct rw_semaphore memory_lock;struct list_head dmabufs; +struct list_head excluded_ranges; };enum vfio_pci_io_width {@@ -172,6 +173,19 @@ enum vfio_pci_io_width { };/* Will be exported for vfio pci drivers usage */ +/* +* A provider can keep a BAR sub-range off the direct guest path, reached only +* through its own trap. The flags select which paths are excluded: mmap, and +* region read and write (an excluded read fills -1, an excluded write is +* dropped). +*/ +#define VFIO_PCI_EXCLUDE_MMAP BIT(0) +#define VFIO_PCI_EXCLUDE_READ BIT(1) +#define VFIO_PCI_EXCLUDE_WRITE BIT(2) + +int vfio_pci_core_add_excluded_range (struct vfio_pci_core_device *vdev,int bar, +u64 start,u64 size,u32 flags); + int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops,
Alex Williamson
maintainerRe: [PATCH v5 11/27] vfio/pci: Virtualize the CXL DVSEC in vfio_pci_config.c
In reply to [email protected]Quoted 30 lines · On Thu, 17 Sep 2026 00:05:24 +0530 <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_config.c +30
-0+/* Virtualize the CXL DVSEC so a guest cannot reprogram the device through it. */ +static int vfio_cxl_dvsec_init (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +u32 dword; +u16 dvsec; +int len, ret; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return 0 ; + + ret =pci_read_config_dword (pdev, dvsec +PCI_DVSEC_HEADER1 , &dword); +if (ret) +return pcibios_err_to_errno (ret); + len =PCI_DVSEC_HEADER1_LEN (dword); + +/* +* The virtualization writes fixed DVSEC offsets up to Status2 (the reset +* doorbell stamps it). A device that reports a shorter DVSEC is not a +* usable Type-2 function; leave it as plain vfio-pci rather than index the +* device-length-sized perm allocation past its end. +*/ +if (len <PCI_DVSEC_CXL_STATUS2 +2 ) +return 0 ; + + vdev->cxl_perm = kmalloc_obj(struct perm_bits, GFP_KERNEL_ACCOUNT); +if (!vdev->cxl_perm) +return -ENOMEM ;
drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = { · line 1858
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+ + ret =init_cxl_dvsec_perm (vdev->cxl_perm, len); +if (ret) { +kfree (vdev->cxl_perm); + vdev->cxl_perm =NULL ; +return ret; + } + + vdev->cxl_dvsec = dvsec; + vdev->cxl_dvsec_len = len;
cxl_dvsec_len is never used here or elsewhere in the series. Thanks,
Alex
Quoted 4 lines
drivers/vfio/pci/vfio_pci_config.c +4
-0+ +return 0 ; +} +
Alex Williamson
maintainerRe: [PATCH v5 22/27] vfio/cxl: Clear the HDM access gate after a hot reset
In reply to [email protected]Quoted 13 lines · On Thu, 17 Sep 2026 00:05:35 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
The HDM memory fault inserts a PFN only when the decoder is in a known-good state (
cxl->hdm_valid), so the host CPU never faults the coherent range into a disabled decoder.
VFIO_DEVICE_PCI_HOT_RESETdrives a plain secondary bus reset rather than the CXL reset sequence, so nothing restores the decoder in thereset_donehandler. A secondary bus reset only reaches a CXL endpoint when the upstream port has SBR unmasked (CXL r3.1 sec 8.1.5.2); in that case the reset decommits the decoder, so clearcxl->hdm_validto keep a later fault from inserting a PFN into a dead decoder.
Quoted 5 lines
A subsequent
VFIO_DEVICE_RESETruns the CXL reset sequence and restores it, and a masked SBR is a no-op that leaves the decoder intact.
vfio_cxl_sbr_unmasked()mirrors thecxl_sbr_masked()check the PCI core uses (drivers/pci/pci.c) for its own CXL bus reset.
This all sounds very broken. If a hot reset is masked, it should fail, not silently do nothing. If a hot reset is unmasked, we can't just invent that the user needs to follow it with a reset ioctl, and the decoder is dead for the remainder of their session otherwise.
So on one hand, we have a reset we can't use because it doesn't do anything, and on the other we have a reset that we can't use because in doing something it makes the device effectively unrecoverable, without some assumption to perform a reset ioctl.
Are bus resets just not supported on CXL devices through vfio-pci? Thanks,
Alex
Quoted 57 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +38
-0@@ -514,8 +514,46 @@ static void vfio_cxl_reset_prepare(struct vfio_pci_core_device *vdev) { } +/* +* A secondary bus reset only reaches a CXL endpoint when the upstream port +* has SBR unmasked (CXL r3.1 sec 8.1.5.2); otherwise the Bridge Control SBR +* bit is ignored and the decoder is left intact. Mirrors the cxl_sbr_masked() +* check the PCI core uses for its own CXL bus reset. +*/ +static bool vfio_cxl_sbr_unmasked (struct pci_dev *pdev) +{ +struct pci_dev *bridge =pci_upstream_bridge (pdev); +u16 dvsec, ctl; + +if (!bridge) +return false; + + dvsec =pci_find_dvsec_capability (bridge,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_PORT ); +if (!dvsec) +return false; + +if (pci_read_config_word (bridge, dvsec +PCI_DVSEC_CXL_PORT_CTL , &ctl)) +return false; + +return ctl &PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR ; +} +static void vfio_cxl_reset_done (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; + +/* +* VFIO_DEVICE_PCI_HOT_RESET drives a plain secondary bus reset, not the +* CXL-aware cxl_reset_bus_function(), so nothing restores the HDM +* decoder here. When the upstream port has SBR unmasked the reset +* decommits the decoder; gate host access to the HDM range so a later +* fault cannot insert a PFN into a dead decoder. A VFIO_DEVICE_RESET +* then runs the CXL reset sequence and restores it. A masked SBR is a +* no-op and leaves the decoder intact. +*/ +if (vfio_cxl_sbr_unmasked (vdev->pdev)) + cxl->hdm_valid = false; }static const struct vfio_cxl_ops vfio_cxl_ops = {
Alex Williamson
maintainerRe: [PATCH v5 13/27] vfio/pci: Bracket PCI resets with the CXL reset hooks
In reply to [email protected]Quoted 31 lines · On Thu, 17 Sep 2026 00:05:26 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 device must quiesce its coherent link before any reset and restore its HDM decoder state afterward, or a reset can leave the device mastering the bus over decoders that are no longer valid. This has to happen around every reset the PCI core drives for the device, not only the ones VFIO issues directly.
Add
reset_prepareandreset_doneto the CXL ops and register the matchingpci_error_handlerscallbacks.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 18 ++++++++++++++++++ include/linux/vfio_pci_core.h | 2 ++ 2 files changed, 20 insertions(+)drivers/vfio/pci/vfio_pci_core.c +5
-0@@ -2794,8 +2794,26 @@ int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, }EXPORT_SYMBOL_GPL (vfio_pci_core_sriov_configure); +static void vfio_pci_core_reset_prepare (struct pci_dev *pdev) +{ +struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev); + +if (vdev->cxl_ops)
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, · line 2811
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ vdev->cxl_ops->reset_prepare (vdev); +} + +static void vfio_pci_core_reset_done (struct pci_dev *pdev) +{ +struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev); + +if (vdev->cxl_ops) + vdev->cxl_ops->reset_done (vdev); +}
Flagging this because it's introducing one more vfio-pci-core use of the convention of storing the vfio_pci_core_device in drvdata that's a barrier for a Rust-based variant driver. Likely this needs to be added to the per-variant boilerplate or macro so variants provide the vfio_pci_core_device pointer rather than expecting it in drvdata. Thanks,
Alex
Quoted 21 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+const struct pci_error_handlers vfio_pci_core_err_handlers = { .error_detected = vfio_pci_core_aer_err_detected, + .reset_prepare = vfio_pci_core_reset_prepare, + .reset_done = vfio_pci_core_reset_done, };EXPORT_SYMBOL_GPL (vfio_pci_core_err_handlers);include/linux/vfio_pci_core.h +2
-0@@ -72,6 +72,8 @@ struct vfio_cxl_ops {void (*release)(struct vfio_pci_core_device *vdev);int (*open_device)(struct vfio_pci_core_device *vdev);void (*close_device)(struct vfio_pci_core_device *vdev); +void (*reset_prepare)(struct vfio_pci_core_device *vdev); +void (*reset_done)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ struct module *owner; };
Alex Williamson
maintainerRe: [PATCH v5 25/27] vfio/cxl: Run the CXL reset at the vfio reset points
In reply to [email protected]Quoted 51 lines · On Thu, 17 Sep 2026 00:05:38 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 function must not take an FLR: it resets the coherent CXL.mem state and corrupts the HDM decoder. The PCI core already reflects this, ordering
cxl_resetahead of flr in pci_reset_fn_methods[], so a function reset of a CXL device runs the DVSEC reset sequence rather than FLR.Route the vfio function-reset points (
VFIO_DEVICE_RESETand the virtualized PCIe/AF FLR writes) through a CXL reset op that runscxl_reset_dvsec_sequence(). The sequence resets the function, always clearing device memory, and restores the HDM decoder and PCI config state, so it is a complete replacement forpci_try_reset_function()on a CXL device. The op runs undermemory_lockand not the PCI device lock, socxl_reset_dvsec_sequence()can take the device lock itself.Clear
hdm_validfor the duration of the reset so a fault cannot insert a PFN into a decoder that is being torn down, and restore it once the sequence has put the decoder back.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 41 +++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 49 +++++++++++++++--- drivers/vfio/pci/vfio_pci_core.c | 77 +++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_priv.h | 1 + include/linux/vfio_pci_core.h | 4 ++ 5 files changed, 152 insertions(+), 20 deletions(-)drivers/vfio/pci/cxl/vfio_cxl_core.c +13
-0@@ -632,6 +632,45 @@ static void vfio_cxl_reset_done(struct vfio_pci_core_device *vdev) cxl->hdm_valid = false; } +/* +* Run the CXL DVSEC reset sequence in place of a PCI function reset. A CXL +* Type-2 function must not take an FLR (it would corrupt CXL.mem), so the vfio +* reset points route here. The sequence resets the function, always clearing +* device memory, and restores the HDM decoder. The caller holds memory_lock, +* and this path does not hold the PCI device lock, so cxl_reset_dvsec_sequence() +* can take it. +*/ +static int vfio_cxl_reset (struct vfio_pci_core_device *vdev) +{ +struct vfio_cxl_state *cxl = vdev->cxl; +int ret; +
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_reset_done(struct vfio_pci_core_device *vdev) · line 657
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+lockdep_assert_held_write (&vdev->memory_lock); + +/* Host CPU access to the HDM range is unsafe until the decoder is back. */ + cxl->hdm_valid = false; + + ret =cxl_reset_dvsec_sequence (vdev->pdev); +if (!ret) + cxl->hdm_valid = true; + +return ret;
Success oriented flow:
if (ret) return ret; cxl->hdm_valid = true; return 0;
But I don't see how the case where hdm_valid remains false is really a valid place for the user to land. We don't really have a "your device is now borked, give up" signal to the user.
Quoted 20 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +16
-0+} + +/* +* The HDM dma-buf may be armed only while the decoder is valid. After a failed +* reset hdm_valid is clear, so the generic memory-enable re-arm must skip the +* dma-buf rather than map DMA onto an unrestored decoder. +*/ +static bool vfio_cxl_hdm_active (struct vfio_pci_core_device *vdev) +{ +struct vfio_cxl_state *cxl = vdev->cxl; + +lockdep_assert_held_write (&vdev->memory_lock); + +return cxl->hdm_valid; +} +static const struct vfio_cxl_ops vfio_cxl_ops = { .init = vfio_cxl_init_device, .release = vfio_cxl_release_device,
drivers/vfio/pci/cxl/vfio_cxl_core.c static const struct vfio_cxl_ops vfio_cxl_ops = { · line 684
Quoted 7 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +2
-0@@ -639,6 +678,8 @@ static const struct vfio_cxl_ops vfio_cxl_ops = { .close_device = vfio_cxl_close_device, .reset_prepare = vfio_cxl_reset_prepare, .reset_done = vfio_cxl_reset_done, + .reset = vfio_cxl_reset, + .hdm_active = vfio_cxl_hdm_active, .owner = THIS_MODULE, };
It's vfio_cxl_ops, so it's unique to CXL, but still leaking HDM to vfio-pci-core as something it needs to care about is pretty ugly. It should be something generic, but also this should be wrapped in some abstraction in vfio-pci-core.
Quoted 8 lines
drivers/vfio/pci/vfio_pci_config.c +0
-0@@ -630,7 +630,14 @@ static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos, *virt_cmd &=cpu_to_le16 (~mask); *virt_cmd |=cpu_to_le16 (new_cmd & mask);
drivers/vfio/pci/vfio_pci_config.c static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos, · line 640
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +8
-1-if (__vfio_pci_memory_enabled (vdev)) +/* +* Re-arm the dma-bufs on memory-enable, but keep a CXL device's +* HDM dma-buf revoked while the decoder is unrestored (a failed +* reset leaves hdm_valid clear); re-arming would map DMA onto a +* decoder the fault path still gates. Plain vfio-pci is unchanged. +*/ +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || vdev->cxl_ops->hdm_active (vdev)))
HDM space and BAR MMIO space are governed by different things, how can we combine them here to say that dmabufs are invalid until both are active? That's not how the hardware works. Does the HDM need a separate address space of dmabufs to toggle independently?
Quoted 15 lines
drivers/vfio/pci/vfio_pci_config.c +2
-1vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -720,7 +727,8 @@ static void vfio_lock_and_set_power_state(struct vfio_pci_core_device *vdev, }vfio_pci_set_power_state (vdev, state); -if (__vfio_pci_memory_enabled (vdev)) +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || vdev->cxl_ops->hdm_active (vdev)))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -910,8 +918,14 @@ static int vfio_exp_config_write(struct vfio_pci_core_device *vdev, int pos,if (!ret && (cap &PCI_EXP_DEVCAP_FLR )) {
drivers/vfio/pci/vfio_pci_config.c static int vfio_exp_config_write(struct vfio_pci_core_device *vdev, int pos, · line 926
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +6
-2vfio_pci_zap_and_down_write_memory_lock (vdev);vfio_pci_dma_buf_move (vdev, true); -pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Keep the HDM dma-buf revoked if a CXL reset +* failed; re-arming would map DMA onto an +* unrestored decoder. Mirrors the reset ioctl. +*/
We don't have granularity of "the HDM dma-buf".
Quoted 7 lines
drivers/vfio/pci/vfio_pci_config.c +2
-0+if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -995,8 +1009,14 @@ static int vfio_af_config_write(struct vfio_pci_core_device *vdev, int pos,if (!ret && (cap &PCI_AF_CAP_FLR ) && (cap &PCI_AF_CAP_TP )) {
drivers/vfio/pci/vfio_pci_config.c static int vfio_af_config_write(struct vfio_pci_core_device *vdev, int pos, · line 1017
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +6
-2vfio_pci_zap_and_down_write_memory_lock (vdev);vfio_pci_dma_buf_move (vdev, true); -pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Keep the HDM dma-buf revoked if a CXL reset +* failed; re-arming would map DMA onto an +* unrestored decoder. Mirrors the reset ioctl. +*/
Same.
Quoted 14 lines
drivers/vfio/pci/vfio_pci_config.c +4
-2+if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -1781,9 +1801,22 @@ static int vfio_cxl_dvsec_write(struct vfio_pci_core_device *vdev, int pos, status2 |=PCI_DVSEC_CXL_CACHE_INV ; }if (ctrl2 &PCI_DVSEC_CXL_INIT_CXL_RST ) { +int ret =0 ; + ctrl2 &= ~PCI_DVSEC_CXL_INIT_CXL_RST ; - status2 &= ~PCI_DVSEC_CXL_RST_ERR ; - status2 |=PCI_DVSEC_CXL_RST_DONE ;
drivers/vfio/pci/vfio_pci_config.c static int vfio_cxl_dvsec_write(struct vfio_pci_core_device *vdev, int pos, · line 1816
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+ +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +vfio_pci_zap_and_down_write_memory_lock (vdev); +vfio_pci_dma_buf_move (vdev, true); + ret =vfio_pci_reset_function (vdev); +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret)) +vfio_pci_dma_buf_move (vdev, false); +up_write (&vdev->memory_lock); + }
Isn't this branch deterministic? IIRC, we only install this handler when vdev->cxl_ops and that ops always registers a reset function.
Quoted 12 lines
drivers/vfio/pci/vfio_pci_config.c +3
-0+ + status2 &= ~(PCI_DVSEC_CXL_RST_DONE |PCI_DVSEC_CXL_RST_ERR ); + status2 |= ret ?PCI_DVSEC_CXL_RST_ERR :PCI_DVSEC_CXL_RST_DONE ; } *pctrl2 =cpu_to_le16 (ctrl2);drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -643,8 +643,27 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) goto out_power;
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 651
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +6
-2/* If reset fails because of the device lock, fail this path entirely */ - ret =pci_try_reset_function (pdev); -if (ret == -EAGAIN ) +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +/* +* VM power-on resets a CXL Type-2 device through its DVSEC +* sequence. vconfig is not built yet here, so take memory_lock +* and call the op directly rather than the wrapper. +*/
B does not follow from A: what's vconfig got to do with taking memory_lock here?
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +5
-0+down_write (&vdev->memory_lock); + ret = vdev->cxl_ops->reset (vdev); +up_write (&vdev->memory_lock); + }else { + ret =pci_try_reset_function (pdev);
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 666
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ } + +/* +* -EAGAIN means the reset could not run. For a CXL device any reset +* error must also fail the open: a failed DVSEC reset can leave the HDM +* decoder cleared or unrestored, and continuing would expose the HDM +* region for host access through a decoder in an unknown state. +*/ +if (ret == -EAGAIN || + (vdev->cxl_ops && vdev->cxl_ops->reset && ret))
This suggests to me that our abstraction is lacking.
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +0
-2goto out_disable_device; vdev->reset_works = !ret;@@ -845,16 +864,30 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)* overwrite the previously restored configuration information. */ if (vdev->reset_works) { - bridge =pci_upstream_bridge (pdev); -if (bridge && !pci_dev_trylock (bridge))
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) · line 873
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +7
-3- goto out_restore_state; -if (pci_dev_trylock (pdev)) { -if (!__pci_reset_function_locked (pdev)) +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +/* +* VM power-off resets a CXL Type-2 device through its +* DVSEC sequence. The sequence takes its own device lock, +* so run it outside the lock below. +* vconfig is already freed here, so call the op directly +* under memory_lock rather than the wrapper.
Again, the comment doesn't actually justify the behavior. In the previous, vconfig is not setup, so take the lock, here vconfig is already freed, so take the lock... meaningless.
Also, are we dropping the try-lock semantics? How's that justified?
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +5
-1+*/ +down_write (&vdev->memory_lock); +if (!vdev->cxl_ops->reset (vdev)) vdev->needs_reset = false; -pci_dev_unlock (pdev); +up_write (&vdev->memory_lock); + }else {
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) · line 889
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ bridge =pci_upstream_bridge (pdev); +if (bridge && !pci_dev_trylock (bridge)) + goto out_restore_state; +if (pci_dev_trylock (pdev)) { +if (!__pci_reset_function_locked (pdev)) + vdev->needs_reset = false; +pci_dev_unlock (pdev); + } +if (bridge) +pci_dev_unlock (bridge);
Abstraction leaves a lot to be desired here.
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c +2
-2} -if (bridge) -pci_dev_unlock (bridge); } out_restore_state:@@ -1592,6 +1625,20 @@ static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev,return ret; } +/* +* Reset the function. A CXL device runs the CXL DVSEC reset sequence in place
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev, · line 1639
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+* of a PCI function reset: it replaces FLR (which would corrupt CXL.mem), +* always clears device memory, and restores the HDM decoder. Callers hold +* memory_lock for write. +*/ +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev) +{ +if (!vdev->cxl_ops || !vdev->cxl_ops->reset) +return pci_try_reset_function (vdev->pdev); + +return vdev->cxl_ops->reset (vdev);
This is all inverted logic and I don't understand why we're trying to leave the reset op optional, make it mandatory:
if (vdev->cxl_ops) return vdev->cxl_ops->reset(vdev); return pci_try_reset_function(vdev->pdev);
But we're again losing the try semantics on the cxl path(?) and naming of the wrapper drops the try semantics as well.
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +2
-1+} +static int vfio_pci_ioctl_reset (struct vfio_pci_core_device *vdev,void __user *arg) {@@ -1614,8 +1661,14 @@ static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev,vfio_pci_set_power_state (vdev,PCI_D0 );vfio_pci_dma_buf_move (vdev, true); - ret =pci_try_reset_function (vdev->pdev);
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev, · line 1672
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +8
-1-if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Re-arm the dma-bufs on success. A CXL device whose reset failed leaves +* the HDM decoder unrestored and hdm_valid clear, so re-arming its HDM +* dma-buf would map device DMA onto a decoder the fault path still gates; +* keep it revoked until a reset succeeds. Plain vfio-pci is unchanged. +*/ +if (__vfio_pci_memory_enabled (vdev) && (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);
I don't think we're doing dmabufs correctly for CXL, I don't see how they're the same address space, or TBH, how we can have a reset fail so catastrophically. Thanks,
Alex
Quoted 29 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0up_write (&vdev->memory_lock);drivers/vfio/pci/vfio_pci_priv.h +1
-0@@ -78,6 +78,7 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t state);void vfio_pci_zap_and_down_write_memory_lock (struct vfio_pci_core_device *vdev); +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev);u16 vfio_pci_memory_lock_and_enable (struct vfio_pci_core_device *vdev);void vfio_pci_memory_unlock_and_restore (struct vfio_pci_core_device *vdev,u16 cmd);include/linux/vfio_pci_core.h +4
-0@@ -74,6 +74,10 @@ struct vfio_cxl_ops {void (*close_device)(struct vfio_pci_core_device *vdev);void (*reset_prepare)(struct vfio_pci_core_device *vdev);void (*reset_done)(struct vfio_pci_core_device *vdev); +/* Run the CXL reset (always clears CXL.mem) in place of FLR */ +int (*reset)(struct vfio_pci_core_device *vdev); +/* True while the HDM range is valid and its dma-buf may be armed */ +bool (*hdm_active)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ struct module *owner; };
Alex Williamson
maintainerRe: [PATCH v5 16/27] vfio/cxl: Create the CXL memdev and set media ready at bind
In reply to [email protected]Quoted 13 lines · On Thu, 17 Sep 2026 00:05:29 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
At bind, build the CXL memory device for the passed-through Type-2 accelerator so it joins the CXL topology and its HDM region resolves to a host physical range. A Type-2 device has no mailbox, so there is no media-ready register to poll: set media ready directly once the component registers validate (mirroring
drivers/net/ethernet/sfc/efx_cxl.c)As per current vfio-cxl support, reject a device with:
- more than one HDM decoder
- interleaving enabled
- whose reset the host cannot service
Quoted 6 lines
The CXL-core allocations are grouped with devres so a failed bind unwinds
them: init failure falls back to plain vfio-pci with the device still bound, so devm would otherwise hold them until unbind.A low-power transition would reset the CXL Type-2 function and lose its CXL.mem contents, so keep it in D0 while it is assigned.
Is it the configuration or the mem contents? Isn't it the user's problem if they put the device in low power state and lose their own data? If it's our problem, what are we protecting? It's not clear to me how we can save and restore the HDM state around reset, but can't allow low power.
This also undercuts your patch 12 justification for the open/close hooks under the rationale that we cannot sample the hardware while it might be in a low power state.
If we do block the low power features (less desirable), it should also be at the probe and dispatch level (-ENOTTY) rather than failing the SET operation with -EINVAL while reporting the feature available via PROBE. Thanks,
Alex
Alex Williamson
maintainerRe: [PATCH v5 17/27] vfio/cxl: Own the whole component register BAR
In reply to [email protected]Quoted 24 lines · On Thu, 17 Sep 2026 00:05:30 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
devm_cxl_probe_mem()maps the component registers through the CXL core, which by default claims each HDM and RAS sub-block. That collides with the full-BAR request vfio-pci-core makes when the guest opens the device.After the component registers are discovered, declare the containing BAR owned with
cxl_reg_map_add_owned_resource(). The core then ioremaps the sub-blocks without requesting them, so vfio-pci-core owns the whole BAR and the requests do not collide.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0@@ -89,6 +89,15 @@ static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) goto err; }
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) · line 100
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +9
-0+/* +* vfio-pci-core requests the whole component-register BAR when the +* guest opens the device. Declare that BAR owned so the CXL core +* ioremaps the HDM and RAS sub-blocks without claiming them, and the +* full-BAR request does not collide. +*/ +cxl_reg_map_add_owned_resource (&cxl->cxlds.reg_map, +pci_resource_n (pdev, pdev->hdm->hdm_bar)); +
The result of this operation is setting a singleton "owned" pointer to this resource, whereas the name suggests it's adding the resource to some set of owned resources. Is "set" really the more idiomatic verb for this than "add"? Thanks,
Alex
Quoted 3 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0if (!cxl->cxlds.reg_map.component_map.hdm_decoder.valid) {pci_err (pdev,"vfio-cxl: HDM decoder registers not found\n" ); ret = -ENODEV ;
Jonathan Cameron
maintainerRe: [PATCH v5 26/27] Documentation: vfio-pci: Document CXL Type-2 device passthrough
In reply to Manish HonapQuoted 5 lines · On Mon, 21 Sep 2026 09:43:34 +0000 Manish Honap <[email protected]> wrote:
Hello Gregory,
Thank you for the careful read of the documentation. I will rework the sections you flagged as below:
Hi Manish,
It is much more helpful to reply inline with the relevant sections so we can see the context wrt to what Gregory was asking. That saves others reading the discussion from having to carefully build up a global model of what was said to understand what the replies are about. Also I'll hazzard a guess that Gregory may have read enough code since he sent the review that he has already forgotten some of his own comments! (maybe that's just me!) I appreciate that doesn't always work if you are going to address the comment via more radical restructuring. Or in short, please don't top post!
Thanks,
Jonathan
Quoted 293 lines
- Address model
I will update the wording to mention that VMM does know the GPA: it builds the guest's CFMWS window and so chooses the guest-physical range the device can inhabit. What the host owns is the HPA and the placement within it, not knowledge of the GPA.
I will add the two models you described, fixed placement for an accelerator that needs exact physical placement versus no fixed placement for pooled or compressed memory, with a small HPA/GPA example, and note that the current series under discussion implements the fixed-offset case.- Virtual decoders
I will rename the The "Guest decoder and commit" section to "Virtual decoders" and add a short flow showing a guest vdecoder write being absorbed and a live read returning COMMITTED from the host-locked physical decoder.
- "The kernel virtualizes ..."
I will update it to say vfio-pci-core (its config-space permission hooks).
- guest IOAS, stage-2 mapping, and struct-page-less coherent memory
I will define these terms where first used and include that the range is handed to the device whole and never onlined as system RAM. I will also add details why a stale stage-2 mapping must not outlive the HDM window, and how the VMM rebuilds it.
- Reset
I will reword this section. FLRs are virtualized so a guest reset can't inflict physical CXL.mem/decoder effects on the host rather than loose wording "must not take an FLR"
I can share the revised text ahead of the v6 posting if that is easier to review.
Thanks,
ManishQuoted 253 lines
-----Original Message-----
From: Gregory Price <[email protected]>
Sent: Thursday, September 17, 2026 1:03 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 26/27] Documentation: vfio-pci: Document CXL Type- 2 device passthroughExternal email: Use caution opening links or attachments
Quoted 2 lines · On Thu, Sep 17, 2026 at 12:05:39AM +0530, [email protected] wrote:
From: Manish Honap <[email protected]>
1) Thank you so much for writing documentation, i truly appreciate this.
2) I apologize in advance for my terseness, I know writing is hard, please do not interpret this as disliking your writing or series.
Review
Quoted 8 lines
(quoted diff) +8
-0+Address model +============= + +The HDM memory is a coherent host physical range (HPA). The host +kernel resolves that range before the guest sees the device, and owns +it for the bind lifetime. The guest only chooses where the memory +appears in its own physical address space (GPA), by programming a +virtual endpoint HDM decoder. The guest never reprograms the physicaldecoder.
Review
Quoted 7 lines
(quoted diff) +7
-0+ +The kernel holds the HPA and does not see the GPA. The guest programs +a GPA and does not see the HPA. The VMM holds the device fd, reads +the committed base from the decoder-register region described below, +and maps the HPA-backed HDM region at the GPA the guest committed. +The base the guest reads back is the GPA, not the HPA. +I think this must be slightly inaccurate / imprecise wording.
The host *must* provide some form of physical memory window to the guest at initialization time, otherwise the guest has no way to know - at boot time - that there's even a window of memory it can use.
That's what the CFMWS is. This is initialized by the hypervisor - which is controlled by the host.
So the host (at least the VMM) must know, for the region the entire device *could* inhabit, what that GPA is - because it's the one that makes the CFMWS for the guest.
If this is not the case, then something is missing from this documentation to explain why.
If you're actually trying to say is that the GPA's programmed into the virtual decoders are largely symbolic - this at best feels a bit inaccurate and simply an implementation detail.
The host's virtio device could enforce ....:
Host Range:
CFMWS HPA - [0x10000, 0x20000] | |Guest Range: | |
CFMWS GPA - [0x50000,0x60000]In that case, you'd get the following translation...
vdecoder0.0 - [0x58000,0x60000]
CFMWS GPA - [0x58000,0x60000]
CFMWS HPA - [0x18000,0x20000]Or the virtio device could not enforce that and let the host page-fault just hand it a random page from the actual CXL device.
vdecoder0.0 - [0x58000, 0x60000] CFMWS GPA - [0x58000, 0x60000] | No discrete host mappingThe former makes sense if the device (accelerator) requires exact physical placement to do its accelerator nonsense.
The latter makes sense if the device (accelerator) doesn't care about placement (compressed memory).
This is not saying we need support both out of the box, but we shouldn't lock ourselves into the former unless there's some reason why the latter is not reasonable.
Can you please help document what the actual expected behavior is with examples in the Address model section so it's easier to understand the intent? That will help quite a bit.
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 7 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +7
-0+Guest decoder and commit +======================== + +The guest programs its virtual endpoint decoder through the trapped +region: it writes a base (a GPA), a size, and then the COMMIT bit. +The host already resolved and committed the physical placement before +the guest ran,So the host does know GPA, just not exact placement.
Review
Quoted 4 lines
so a live read of the decoder always shows COMMITTED and the
(quoted diff) +3
-0+guest's commit poll completes. The physical decoder is never +rewritten; the guest's writes are absorbed. +Rather clunky, round-about way to say "The guest decoders are
virtualized". If possible, it would be nice to formalize this concept
into "Virtual Decoders" - since that's what this is.With that concept i think you can probably generate some nice diagrams that show how the guest vdecoder's interact with the host drivers.
Review
Quoted 3 lines
(quoted diff) +3
-0+The VMM observes the commit, reads the committed base, and maps the +HDM region at that GPA. +So the host does know the GPA.
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 6 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +6
-0+CXL Device DVSEC +================ + +The kernel virtualizes the CXL Device DVSEC body through the +config-space permission hooks. Reads and writes inside the DVSEC body +use a per-open shadow; a guest write stays in the shadow and does notreach hardware.
Review
Quoted 2 lines
(quoted diff) +2
-0+Accesses outside the DVSEC body go to the device as usual. +"The kernel" - what part? vfio-pci ? the vmm ?
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 6 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +6
-0+DMA and iommufd +=============== + +A Type-2 accelerator issues ATS-translated DMA to addresses inside +its own HDM window, so that range must be present in the guest IOAS +that backs the nested stage-2 translation.Type-2, ATS, DMA, HDM window, guest IOAS, stage-2 translation
I think the only thing i don't know in this sentence is "guest IOAS" and it's still hurting my brain to read.
Are all accelerators expect to have this particular interaction, or just yours?
Review
Quoted 3 lines
The HDM range is struct-page-less coherent
(quoted diff) +2
-0+memory, which a userspace-VA ``IOMMU_IOAS_MAP`` cannot pin. +The hardest part about writing about virtualization is keeping a consistent mental model from section to section.
which userspace? guest? host? (i presume guest here)
struct-page-less coherent memory
e.g. the host never hotplugs this, it hands the entire region directly to the VFIO device, right?I think this would be nice to spell out somewhere.
Review
Quoted 3 lines
(quoted diff) +3
-0+The HDM memory region is therefore exportable as a dma-buf: +``VFIO_DEVICE_FEATURE_DMA_BUF`` on that region returns an fd that +iommufd maps with ``IOMMU_IOAS_MAP_FILE``, mapping the physicalrange
Review
Quoted 4 lines
(quoted diff) +4
-0+without a VA or a page pin. The dma-buf is revoked whenever the +mapping is torn down (reset, power transition, teardown), so a stale +stage-2 mapping cannot outlive the HDM window. +For the sake of readers, I think either a little bit more information on this "stage-2 mapping" concept is needed to make sense of what's going on here and why it mustn't outlive the HDM window.
Review
Quoted 9 lines
(quoted diff) +9
-0+Reset +===== + +A CXL Type-2 function must not take a Function Level Reset: an FLR +resets the coherent CXL.mem state and the HDM decoder. The PCI core +reflects this by preferring the CXL reset over FLR, so a function +reset of a CXL device runs the CXL DVSEC reset sequence, which resets +the function and then restores the HDM decoder and the PCI config state. +I think what you're trying to say is that FLRs are never passed to the device because it can cause physical device effects that defeat the purpose of the virtualization, yes?
So we virtualize FLRs...
Review
Quoted 3 lines
(quoted diff) +3
-0+A guest requests a reset by writing Initiate CXL Reset in the DVSEC. +That write only stamps completion in the shadow. The real reset runs +at the vfio reset points (the reset ioctl and a virtualized FLR through configspace):
As you describe here.
So it's not that an accelerator "must not take an FLR" - it's that FLRs are virtualized to prevent deleterious effects on the host / hardware.
Am I misunderstanding this?
Review
Quoted 13 lines
(quoted diff) +13
-0+the kernel zaps the HDM mapping and revokes the dma-buf, then runs +the CXL reset, which always clears the device memory, and restores +and re-samples the decoder afterwards. A CXL port masks Secondary Bus +Reset by default, so a ``VFIO_DEVICE_PCI_HOT_RESET`` does not reach +the endpoint and the HDM state is untouched. If the port has SBR +unmasked the reset can decommit the decoder without restoring it, so +the reset_done handler gates HDM access; a ``VFIO_DEVICE_RESET`` then +runs the CXL reset sequence and restores it. + +The decoder register region is served by live reads of the hardware +decoder with guest writes absorbed: the decoder is committed and +locked by the host, so a guest can neither decommit nor reprogram it, +and the kernel keeps no shadow of the decoder state.This is basically what I said at the beginning - it must either be that the host provides locked auto-decoders at boot, or it must provide proper virtualization so that the decoders settings are fully virtualized.
Seems it's the former, and that makes sense. Please correct me if i'm misunderstanding.
Review
Quoted 7 lines
After a reset the kernel restores and
(quoted diff) +6
-0+re-samples the firmware-committed decoder, so the geometry the guest +reads back is unchanged. A VMM that dropped its HDM mapping, for +example across a reset or a D3hot->D0 transition, must rescan the +decoder and rebuild its +stage-2 mapping before it resumes HDM access. +Yeah i think we need a bit more information about this stage-2 mapping rebuild to make sense of this. Maybe I'm just not read-up enough on this particular setup - is there another part of the docs you can link to that talk about this, or are you able to share some details as to what this rebuild process looks like?
~Gregory
Jonathan Cameron
maintainerRe: [PATCH v5 04/27] cxl: Add cxl_reset_dvsec_sequence() for vfio-pci
In reply to [email protected]Quoted 6 lines · On Thu, 17 Sep 2026 00:05:17 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
vfio-pci needs to run a CXL reset on a passed-through Type-2 device without the host-memory handling in
cxl_reset_function(): the memory behind the decoder belongs to the guest, so the HDM range collection and host CPU cache flush that the host path performs must be skipped.
Hi Manish,
This cache flush being skipped bit concerns me. Who is issuing the necessary flushes in the vfio case? That is there to ensure we don't end up with random lines cached by PA (as well as VA if relevant), so they affect a guest just as much as the host.
I'm not sure why it makes any difference who the memory belongs to.
Quoted 11 lines
Add
cxl_reset_dvsec_sequence(), which runs the DVSEC reset under the device lock and restores HDM state throughcxl_restore_state_after_pci_reset(), andcxl_reset_capable()to gate it on a function-scoped reset. Both are exported to the vfio-cxl module so vfio-pci core does not import the CXL namespace.CXL Reset always clears memory, so there is no caller Memory Clear choice.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>
Jonathan Cameron
maintainerRe: [PATCH v5 09/27] vfio/pci: Add a generic excluded-range list
In reply to [email protected]Quoted 14 lines · On Thu, 17 Sep 2026 00:05:22 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
The MSI-X table is virtualized in
vfio_pci_bar_rw()by an open-coded x_start/x_end window that fills reads with -1 and drops writes. Now that a generic excluded-range list expresses the same fill/drop behavior, register the MSI-X table as a read and write excluded range instead of special-casing it in the read/write path.Add the range when the MSI-X capability is parsed in
vfio_pci_core_enable()and clear the list invfio_pci_core_disable()alongside the config teardown. The read/write path now relies solely onvfio_pci_bar_find_exclusion(), so MSI-X and a provider's (e.g. vfio-cxl) trapped registers share one mechanism.
Quoted 4 lines
No functional change.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>
A few things inline that might tidy up the implementation and make it a little more readable. Also you've use object allocators in some places but not universally.
Jonathan
Quoted 30 lines
--- drivers/vfio/pci/vfio_pci_core.c | 209 +++++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_priv.h | 9 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 8 ++ include/linux/vfio_pci_core.h | 14 +++ 4 files changed, 240 insertions(+)drivers/vfio/pci/vfio_pci_core.c +8
-0@@ -24,6 +24,7 @@#include <linux/pci.h> #include <linux/pm_runtime.h> #include <linux/slab.h> +#include <linux/sort.h> #include <linux/types.h> #include <linux/uaccess.h> #include <linux/vgaarb.h> @@ -1012,6 +1013,204 @@ static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,return vfio_info_add_capability (caps, &header,sizeof (header)); } +struct vfio_pci_excluded_range { +struct list_head entry; +int bar; +u64 start; +u64 size; +u32 flags; +};
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1032
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ +int vfio_pci_core_add_excluded_range (struct vfio_pci_core_device *vdev,int bar, +u64 start,u64 size,u32 flags) +{ +struct vfio_pci_excluded_range *range; + + range =kzalloc_obj (*range); +if (!range) +return -ENOMEM ; +
I'd be tempted to do kmalloc_obj and
*range = (struct vfio_pci_excluded_range) {
.bar = bar,
.start = start,
.size = size,
.flags = flags,
};
list_add_tail(...)Quoted 17 lines
drivers/vfio/pci/vfio_pci_core.c +17
-0+ range->bar = bar; + range->start = start; + range->size = size; + range->flags = flags; +list_add_tail (&range->entry, &vdev->excluded_ranges); + +return 0 ; +} +EXPORT_SYMBOL_GPL (vfio_pci_core_add_excluded_range); + +static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device *vdev) +{ +struct vfio_pci_excluded_range *range, *tmp; + +list_for_each_entry_safe (range, tmp, &vdev->excluded_ranges, entry) { +list_del (&range->entry); +kfree (range);
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1105
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+ } +} + +/* A page-aligned mmap hole, derived from an mmap-excluded range. */ +struct vfio_pci_mmap_hole { +u64 start; +u64 end; +};
This is just a range. Do we need another define for this specific case?
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +6
-0+/* +* Advertise the BAR as mmappable minus every page-aligned mmap-excluded hole. +* A BAR can carry several holes at unrelated offsets (for example an MSI-X +* table and one or more trapped CXL component sub-blocks, which the CXL spec +* locates by pointer, not at fixed offsets). +* Collect the holes, page-align and sort them, coalesce any that overlap or
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1131
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+* touch, and advertise the gaps. +*/ +static int vfio_pci_excluded_sparse_cap (struct vfio_pci_core_device *vdev, +int index,struct vfio_info_cap *caps) +{ +u64 bar_len =pci_resource_len (vdev->pdev, index); +struct vfio_region_info_cap_sparse_mmap *sparse; +struct vfio_pci_excluded_range *range; +struct vfio_pci_mmap_hole *holes; +int nr_holes =0 , nr_areas =0 , i, j;
Generally avoid mixing declarations with assignment with those that don't on a single line. It is harder to read than splitting them into each time of declaration.
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1143
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c +12
-0+size_t size; +u64 pos; +int ret; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) +if (range->bar == index && + (range->flags &VFIO_PCI_EXCLUDE_MMAP )) + nr_holes++; + +if (!nr_holes) +return 0 ; +
struct vfio_pci_mmap_hole *holes __free(kfree) = kzalloc_objs(*holes, nr_holes);
or something like that. Kees is well his way to getting rid of almost all places where objects are allocated in non typesafe ways.
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +5
-0+ holes =kmalloc_array (nr_holes,sizeof (*holes),GFP_KERNEL ); +if (!holes) +return -ENOMEM ; + +/*
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1158
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+* mmap is page granular, so each hole rounds out to the page boundaries +* enclosing its excluded sub-range. The byte-granular exclusion still +* governs the fault and read/write paths; only the advertised mmap areas +* round to whole pages. +*/ + i =0 ; +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != index || + !(range->flags &VFIO_PCI_EXCLUDE_MMAP )) +continue ;
holes[i++] = (struct vfio_pci_mmap_hole) {
.start = ALIGN_DOWN(range->start, PAGE_SIZE),
.end = ALIGN(range->start + range->size, PAGE_SIZE),
};Mind you vfio_pci_mmap_hole seems a lot like a range. So maybe just use a struct range then you get
holes[i++] = DEFINE_RANGE(ALIGN_DOWN(range->start, PAGE_SIZE), ALIGN(range->start + range->size, PAGE_SIZE));
Quoted 16 lines
drivers/vfio/pci/vfio_pci_core.c +16
-0+ holes[i].start =ALIGN_DOWN (range->start,PAGE_SIZE ); + holes[i].end =ALIGN (range->start + range->size,PAGE_SIZE ); + i++; + } + +sort (holes, nr_holes,sizeof (*holes), vfio_pci_mmap_hole_cmp,NULL ); + +/* Coalesce holes that overlap or touch after page alignment. */ +for (i =0 , j =0 ; i < nr_holes; i++) { +if (j && holes[i].start <= holes[j -1 ].end) + holes[j -1 ].end =max (holes[j -1 ].end, holes[i].end); +else + holes[j++] = holes[i]; + } + nr_holes = j; +
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1184
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+/* One mmappable area per gap: before, between, and after the holes. */ +for (i =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) + nr_areas++; + pos = holes[i].end; + } +if (pos < bar_len) + nr_areas++; + + size =struct_size (sparse, areas, nr_areas);
struct vfio_region_info_cap_sparse_mmap *sparse = kzalloc_flex(*sparse, areas, nr_areas);
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1187
Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+ sparse =kzalloc (size,GFP_KERNEL ); +if (!sparse) { +kfree (holes);
with the __free above this can simply return.
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1199
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c +12
-0+return -ENOMEM ; + } + + sparse->header.id =VFIO_REGION_INFO_CAP_SPARSE_MMAP ; + sparse->header.version =1 ; + sparse->nr_areas = nr_areas; + +for (i =0 , j =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = holes[i].start - pos; + j++;
Bit of a long line because of the indent but I'd still go for setting whole structure in one shot - so something like:
sparse->areas[j++] = (struct vfio_region_sparse_mmap_area) {
.offset = pos,
.size = holes[i].start - pos,
};drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1205
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +6
-0+ } + pos = holes[i].end; + } +if (pos < bar_len) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = bar_len - pos;
Similar for setting it in one go so we only do the indexing once + looks more like above (where this trick is more advantageous!)
sparse->areas[j] = (struct vfio_sparse_mmap_area) {
.offset = pos,
.size = bar_len - pos,
};drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1187
Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+ } + +kfree (holes);
This looks like a good place to use __free magic to simplify things.
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +5
-0+ ret =vfio_info_add_capability (caps, &sparse->header, size); +kfree (sparse); +return ret; +} +
Jonathan Cameron
maintainerRe: [PATCH v5 11/27] vfio/pci: Virtualize the CXL DVSEC in vfio_pci_config.c
In reply to [email protected]Quoted 23 lines · On Thu, 17 Sep 2026 00:05:24 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 device is reprogrammable through its CXL DVSEC: a guest could set Config Lock, toggle CXL.cache and CXL.mem enable, or rewrite the HDM range registers that govern host memory decode. Virtualize the DVSEC so the guest sees a shadow it cannot use to reprogram the hardware.
Build a per-device
cxl_permpermission map, modeled onmsi_perm, when the device is bound through the CXL provider (e.g. vfio-cxl). The whole CXL DVSEC is served from the vconfig shadow; only Control and Control2 are guest programmable, while Capability, Status, Lock and the Range registers keep their firmware snapshot. A vendor DVSEC on the same device is
unaffected: the map is selected only for the CXL DVSEC offset.Control2 carries the CXL reset and cache write-back-invalidate initiate bits as self-clearing doorbells. vfio never forwards them to hardware, so a custom writefn synthesizes their completion in the shadow: the initiate bit self-clears and the matching Status2 bit (Cache Invalid or Reset Done) is set, so a guest following the spec reset sequence (
INIT_CACHE_WBI, poll Cache Invalid,INIT_CXL_RST, poll Reset Done) progresses instead of timing out. The host performs the real cache write-back and reset at the vfio reset points.
Quoted 2 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>
A few minor comments inline.
J
Quoted 12 lines
--- drivers/vfio/pci/vfio_pci_config.c | 132 ++++++++++++++++++++++++++++- include/linux/vfio_pci_core.h | 3 + 2 files changed, 133 insertions(+), 2 deletions(-)drivers/vfio/pci/vfio_pci_config.c +2
-0+ +static int init_cxl_dvsec_perm (struct perm_bits *perm,int len)
drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = { · line 1805
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+{ +int i; + +if (alloc_perm_bits (perm, len)) +return -ENOMEM ; + + perm->writefn = vfio_cxl_dvsec_write; + +/* Serve the whole CXL DVSEC from the shadow. */ +for (i =0 ; i < len; i++)
for (int i = 0;
Is mostly acceptable in the kernel these days and keeps the scope tightly defined.
Quoted 31 lines
drivers/vfio/pci/vfio_pci_config.c +31
-0+p_setb (perm, i, (u8 )ALL_VIRT ,NO_WRITE ); + +/* +* Control and Control2 are guest programmable; Capability, Status, +* Lock and the Range registers keep their firmware snapshot, so the +* guest cannot set Config Lock or rewrite the capability and ranges. +*/ +p_setw (perm,PCI_DVSEC_CXL_CTRL , (u16 )ALL_VIRT , (u16 )ALL_WRITE ); +p_setw (perm,PCI_DVSEC_CXL_CTRL2 , (u16 )ALL_VIRT , (u16 )ALL_WRITE ); + +return 0 ; +} + +/* Virtualize the CXL DVSEC so a guest cannot reprogram the device through it. */ +static int vfio_cxl_dvsec_init (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +u32 dword; +u16 dvsec; +int len, ret; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return 0 ; + + ret =pci_read_config_dword (pdev, dvsec +PCI_DVSEC_HEADER1 , &dword); +if (ret) +return pcibios_err_to_errno (ret); + len =PCI_DVSEC_HEADER1_LEN (dword); +
drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = { · line 1846
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+/* +* The virtualization writes fixed DVSEC offsets up to Status2 (the reset +* doorbell stamps it). A device that reports a shorter DVSEC is not a +* usable Type-2 function; leave it as plain vfio-pci rather than index the +* device-length-sized perm allocation past its end. +*/ +if (len <PCI_DVSEC_CXL_STATUS2 +2 ) +return 0 ; + + vdev->cxl_perm = kmalloc_obj(struct perm_bits, GFP_KERNEL_ACCOUNT);
I'd use *vdev->cxl_perm instead of struct perm_bits just because that saves anyone checking types.
Quoted 33 lines
drivers/vfio/pci/vfio_pci_config.c +22
-0+if (!vdev->cxl_perm) +return -ENOMEM ; + + ret =init_cxl_dvsec_perm (vdev->cxl_perm, len); +if (ret) { +kfree (vdev->cxl_perm); + vdev->cxl_perm =NULL ; +return ret; + } + + vdev->cxl_dvsec = dvsec; + vdev->cxl_dvsec_len = len; + +return 0 ; +} +int vfio_config_init (struct vfio_pci_core_device *vdev) {struct pci_dev *pdev = vdev->pdev;@@ -1842,6 +1955,12 @@ int vfio_config_init(struct vfio_pci_core_device *vdev)if (ret) goto out; +if (vdev->cxl_ops) { + ret =vfio_cxl_dvsec_init (vdev); +if (ret) + goto out; + } +return 0 ; out:
drivers/vfio/pci/vfio_pci_config.c void vfio_config_free(struct vfio_pci_core_device *vdev) · line 1989
Quoted 8 lines
drivers/vfio/pci/vfio_pci_config.c +5
-0@@ -1863,6 +1982,12 @@ void vfio_config_free(struct vfio_pci_core_device *vdev)kfree (vdev->msi_perm); vdev->msi_perm =NULL ; } +if (vdev->cxl_perm) { +free_perm_bits (vdev->cxl_perm); +kfree (vdev->cxl_perm); + vdev->cxl_perm =NULL ; + vdev->cxl_dvsec =0 ;
I'd define a vfio_cxl_dvsec_exit() or _unint() for this so it is clear it pairs with vfio_cxl_dvsec_init() above.
Quoted 5 lines
drivers/vfio/pci/vfio_pci_config.c +1
-0+ } }/*
drivers/vfio/pci/vfio_pci_config.c ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev, · line 2059
Quoted 9 lines
drivers/vfio/pci/vfio_pci_config.c +4
-0@@ -1926,12 +2051,15 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,* of the extended capability list. Use default, ro * access, which will virtualize the id and next values. */ + cap_start =vfio_find_cap_start (vdev, *ppos); +if (cap_id >PCI_EXT_CAP_ID_MAX ) perm = &direct_ro_perms; +else if (cap_id ==PCI_EXT_CAP_ID_DVSEC && vdev->cxl_perm && + cap_start == vdev->cxl_dvsec)
If it is only used here (I haven't read on in series)
vfio_find_cap_start(vdev, *ppos) == vdev->cxl_dvsec)
seems fine to me. It's only just over 80 chars and hopeful this bit of the kernel is flexible on that!
Quoted 8 lines
drivers/vfio/pci/vfio_pci_config.c +1
-2+ perm = vdev->cxl_perm; else perm = &ecap_perms[cap_id]; - - cap_start =vfio_find_cap_start (vdev, *ppos); }else {WARN_ON (cap_id >PCI_CAP_ID_MAX );
Jonathan Cameron
maintainerRe: [PATCH v5 12/27] vfio/pci: Call the CXL open and close hooks around device use
In reply to [email protected]Quoted 25 lines · On Thu, 17 Sep 2026 00:05:25 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
Volatile CXL register state must be sampled from live hardware when the guest opens the device, not at bind, because a low-power transition between bind and open can leave the bind-time values stale.
Add open and close to the CXL ops and call them from the common enable and close paths. A failed open unwinds the enable in reverse order:
unmap the BARs, drop the excluded ranges, then free the virtual config.Factor the BAR unmap out of
vfio_pci_core_disable()into a helper so the enable failure path and the disable path share it.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 40 +++++++++++++++++++++++++------- include/linux/vfio_pci_core.h | 2 ++ 2 files changed, 33 insertions(+), 9 deletions(-)drivers/vfio/pci/vfio_pci_core.c +0
-0
drivers/vfio/pci/vfio_pci_core.c static const struct dev_pm_ops vfio_pci_core_pm_ops = { · line 599
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +6
-0@@ -591,6 +591,21 @@ static const struct dev_pm_ops vfio_pci_core_pm_ops = {static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device *vdev); +static void vfio_pci_core_unmap_bars (struct vfio_pci_core_device *vdev) +{ +struct pci_dev *pdev = vdev->pdev; +int i, bar; + +for (i =0 ; i <PCI_STD_NUM_BARS ; i++) {
for (int i = 0; i < PCI_STD_NUM_BARS; i++) int bar = i + PCI_STD_RESOURCES;
Nice to keep things tightly scoped where we can.
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+ bar = i +PCI_STD_RESOURCES ; +if (IS_ERR_OR_NULL (vdev->barmap[bar])) +continue ; +pci_iounmap (pdev, vdev->barmap[bar]); +pci_release_selected_regions (pdev,1 << bar); + vdev->barmap[bar] =NULL ; + } +}
Jonathan Cameron
maintainerRe: [PATCH v5 16/27] vfio/cxl: Create the CXL memdev and set media ready at bind
In reply to [email protected]Quoted 17 lines · On Thu, 17 Sep 2026 00:05:29 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
At bind, build the CXL memory device for the passed-through Type-2 accelerator so it joins the CXL topology and its HDM region resolves to a host physical range. A Type-2 device has no mailbox, so there is no media-ready register to poll: set media ready directly once the component registers validate (mirroring
drivers/net/ethernet/sfc/efx_cxl.c)As per current vfio-cxl support, reject a device with:
- more than one HDM decoder
- interleaving enabled
- whose reset the host cannot serviceThe CXL-core allocations are grouped with devres so a failed bind unwinds
them: init failure falls back to plain vfio-pci with the device still bound, so devm would otherwise hold them until unbind.
Quoted 5 lines
A low-power transition would reset the CXL Type-2 function and lose its CXL.mem contents, so keep it in D0 while it is assigned.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>
A small thing inline.
Quoted 71 lines
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 118 +++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_core.c | 16 ++++ 2 files changed, 134 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +54
-0@@ -6,15 +6,132 @@*/ #include <linux/module.h> +#include <linux/pci.h> +#include <linux/range.h> #include <linux/vfio_pci_core.h> +#include <cxl/cxl.h> +#include <cxl/pci.h> + +/** +* struct vfio_cxl_state - per-device state for a vfio-cxl device +* @cxlds: CXL device state; kept first for devm_cxl_dev_state_create() +* @cxlmd: memory device joined to the CXL topology at bind +* @hpa_range: host physical range of the HDM region +*/ +struct vfio_cxl_state { +struct cxl_dev_state cxlds; +struct cxl_memdev *cxlmd; +struct range hpa_range; +};static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) { +struct pci_dev *pdev = vdev->pdev; +struct vfio_cxl_state *cxl; +struct cxl_memdev *cxlmd; +u64 hdm_size, serial; +u16 dvsec; +int ret; + +/* +* pdev->hdm is cached at PCI enumeration, before any driver binds, so a +* device without it has no usable HDM decoder. Fall back to plain +* vfio-pci rather than deferring the bind forever. +*/ +if (!pdev->hdm) +return -ENODEV ; + +/* The guest drives one virtual decoder; multiple are unsupported. */ +if (pdev->hdm->decoder_count !=1 ) +return -EOPNOTSUPP ; + +/* An interleaved decoder cannot be mapped 1:1 to the guest. */ +if (pdev->hdm->settings[0 ].interleave_ways !=1 ) +return -EOPNOTSUPP ; + +/* +* The guest drives resets through the CXL Device DVSEC and polls the +* shadow for completion. If the host cannot service a function-scoped +* CXL reset, that request could never complete, so refuse the device +* rather than advertise a reset the guest would poll on forever. +*/ +if (!cxl_reset_capable (pdev)) +return -EOPNOTSUPP ; + + hdm_size =range_len (&pdev->hdm->settings[0 ].hpa_range); +if (!hdm_size) +return -ENXIO ; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE );
drivers/vfio/pci/cxl/vfio_cxl_core.c line 76
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+ serial =pci_get_dsn (pdev); + +/* +* Group the CXL-core allocations so a later failure unwinds them here. +* A failed init falls back to plain vfio-pci with the device still +* bound, so devm would otherwise hold them until unbind. +*/ +if (!devres_open_group (&pdev->dev,NULL ,GFP_KERNEL )) +return -ENOMEM ; +
I'd be tempted to factor out the stuff that is unwound via the group. If this were simple a call to a helper function, that helper could return directly on error giving us simpler code flow. Then you'd just check the helper return to find out if you should close or release the group.
Quoted 66 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +54
-0+ cxl =devm_cxl_dev_state_create (&pdev->dev,CXL_DEVTYPE_DEVMEM , serial, + dvsec, struct vfio_cxl_state, cxlds, + false); +if (!cxl) { + ret = -ENOMEM ; + goto err; + } + + ret =cxl_pci_setup_regs (pdev,CXL_REGLOC_RBI_COMPONENT , + &cxl->cxlds.reg_map); +if (ret) { +pci_err (pdev,"vfio-cxl: no component registers\n" ); + goto err; + } + +if (!cxl->cxlds.reg_map.component_map.hdm_decoder.valid) { +pci_err (pdev,"vfio-cxl: HDM decoder registers not found\n" ); + ret = -ENODEV ; + goto err; + } + +/* +* A Type-2 accelerator has no mailbox and no media-ready register, so +* set media ready directly. +*/ + cxl->cxlds.media_ready = true; + + ret =cxl_set_capacity (&cxl->cxlds, hdm_size); +if (ret) + goto err; + + cxlmd =devm_cxl_probe_mem (&cxl->cxlds, &cxl->hpa_range); +if (IS_ERR (cxlmd)) { + ret =PTR_ERR (cxlmd); + goto err; + } + + cxl->cxlmd = cxlmd; +devres_close_group (&pdev->dev,NULL ); + +/* +* Powering a CXL Type-2 function down and back up reinitializes its +* device state and discards the contents of its coherent memory. Pin +* it in D0 for as long as it is assigned so CXL.mem stays intact. +*/ + vdev->disable_idle_d3 = true; + vdev->cxl = cxl; +return 0 ; + +err: +devres_release_group (&pdev->dev,NULL ); +return ret; }static void vfio_cxl_release_device (struct vfio_pci_core_device *vdev) { + vdev->cxl =NULL ; }static int vfio_cxl_open_device (struct vfio_pci_core_device *vdev)@@ -59,3 +176,4 @@ module_exit(vfio_cxl_exit);MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("VFIO support for CXL Type-2 devices" ); +MODULE_IMPORT_NS ("CXL" );
RE: [PATCH v5 02/27] cxl/regs: Let a BAR-owning driver own the component register block
In reply to Jonathan CameronQuoted 38 lines
-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Tuesday, September 22, 2026 7:06 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 02/27] cxl/regs: Let a BAR-owning driver own the component register blockExternal email: Use caution opening links or attachments
Quoted 10 lines · On Thu, 17 Sep 2026 00:05:15 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
cxl_map_component_regs()claims each mapped sub-block withdevm_request_mem_region(). A driver that already requested the whole component register BAR, such as vfio-cxl, would then collide with that claim and fail to map the HDM decoder and RAS blocks.Add
cxl_reg_map_add_owned_resource()so such a driver records the resource it already owns on the register map, and skip the sub-block request when the block falls within an owned resource.
Quoted 9 lines
This seems a slightly odd intermediate point.
The assumption is that if owned the whole bar is owned - hence only handle one owned region (no check if there is already one set?) yet the code will try to map it if we get a request for something outside of the region mapped (which can't exist).
This all makes me a little nervous.
Agreed; current model is to have one owned resource - the whole component register BAR and I will enforce this in v6.
1. Helper is going to be renamed to cxl_reg_map_set_owned_resource and it will return -EBUSY when resource is already set.
2. cxl_map_component_regs will fail with -ENXIO when a block of an owned map lies outside the owned resource.
Quoted 18 lines
drivers/cxl/core/regs.c void __iomem *devm_cxl_iomap_block(struct
Quoted 14 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/cxl/core/regs.c | 17 +++++++++++++++-- include/cxl/cxl.h | 2 ++ include/cxl/pci.h | 3 +++ 3 files changed, 20 insertions(+), 2 deletions(-)drivers/cxl/core/regs.c index renamed from drivers/cxl/core/regs.c +0
-0
d79550dbb484..58a7c5cafb45100644drivers/cxl/core/regs.c +0
-0@@ -216,6 +216,13 @@ void __iomem *devm_cxl_iomap_block(structdevice
Quoted 3 lines
drivers/cxl/core/regs.c +0
-0*dev, resource_size_t addr, }EXPORT_SYMBOL_NS_GPL (devm_cxl_iomap_block,"CXL" );
Quoted 7 lines
drivers/cxl/core/regs.c void __iomem *devm_cxl_iomap_block(struct device *dev, resource_size_t addr, · line 222
Quoted 3 lines
drivers/cxl/core/regs.c +3
-0+void cxl_reg_map_add_owned_resource (struct cxl_register_map *map, +struct resource *res) { + map->owned = res;As above, I think this needs a sanity check that a future driver author doesn't think they can just add lots of these.
Absolute minimum is add some Docs.
Yes, I will add the documentation.
Quoted 26 lines
drivers/cxl/core/regs.c int cxl_map_component_regs(const struct
Quoted 16 lines
drivers/cxl/core/regs.c +3
-0+} +EXPORT_SYMBOL_NS_GPL (cxl_reg_map_add_owned_resource,"CXL" ); +int cxl_map_component_regs (const struct cxl_register_map *map,struct cxl_component_regs *regs, unsigned long map_mask) @@ -234,6 +241,7 @@int cxl_map_component_regs(const struct
cxl_register_map*map,struct mapinfo *mi = &mapinfo[i]; resource_size_t addr; resource_size_t length;drivers/cxl/core/regs.c +1
-0+struct resource res;if (!mi->rmap->valid)continue ;@@ -241,8 +249,13 @@ int cxl_map_component_regs(const struct
cxl_register_map*map,drivers/cxl/core/regs.c int cxl_map_component_regs(const struct cxl_register_map *map, · line 254
Quoted 8 lines
drivers/cxl/core/regs.c +3
-2continue ; addr = map->resource + mi->rmap->offset; length = mi->rmap->size; - *(mi->addr) =devm_cxl_iomap_block (host, addr, length); -if (!*(mi->addr)) + res =DEFINE_RES_MEM (addr, length); + +if (map->owned &&resource_contains (map->owned, &res))
Quoted 2 lines
If assumption is whole bar or none, should we just fail if
map->ownedis true but the resource contains fails?
Yes, agreed; I will also add this.
Quoted 8 lines
Quoted 8 lines
drivers/cxl/core/regs.c +4
-0+ *mi->addr =devm_cxl_ioremap_block (host, addr, length); + else + *mi->addr =devm_cxl_iomap_block (host, addr, length); +if (!*mi->addr)return -ENOMEM ; }
RE: [PATCH v5 03/27] cxl: Move component register defines to uapi/cxl/cxl_regs.h
In reply to Jonathan CameronQuoted 39 lines
-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Tuesday, September 22, 2026 7:14 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 03/27] cxl: Move component register defines touapi/cxl/cxl_regs.hExternal email: Use caution opening links or attachments
Quoted 12 lines · On Thu, 17 Sep 2026 00:05:16 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
The CXL component and HDM decoder register layout is spec-defined and is needed by a userspace consumer that cannot include the private
drivers/cxl/cxl.hThe vfio-cxl selftest (
tools/testing/selftests/vfio/vfio_cxl_type2_test.c) builds against installed uapi headers only. Move those defines to a new uapi header, the way PCI register offsets live in uapipci_regs.h, and include it fromdrivers/cxl/cxl.hso existing in-kernel users are unchanged.
Quoted 8 lines
Quoted 2 lines
No functional change; the GENMASK and BIT forms are written as plain values as a uapi header requirement.
I thought we'd fixed that these days... And indeed we have see
include/uapi/linux/bits.hfor__GENMASK()andinclude/uapi/linux/const.hfor_BITUL()Both are in use in
pci_regs.hfor instance.
Thank you. I think I missed these. I will update the file and the commit message accordingly.
Quoted 2 lines
Jonathan
RE: [PATCH v5 04/27] cxl: Add cxl_reset_dvsec_sequence() for vfio-pci
In reply to Jonathan CameronQuoted 33 lines
-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Saturday, September 26, 2026 2:11 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 04/27] cxl: Addcxl_reset_dvsec_sequence()for vfio-pciExternal email: Use caution opening links or attachments
Quoted 6 lines · On Thu, 17 Sep 2026 00:05:17 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
vfio-pci needs to run a CXL reset on a passed-through Type-2 device without the host-memory handling in
cxl_reset_function(): the memory behind the decoder belongs to the guest, so the HDM range collection and host CPU cache flush that the host path performs must be skipped.
Quoted 7 lines
Hi Manish,
This cache flush being skipped bit concerns me. Who is issuing the necessary flushes in the vfio case? That is there to ensure we don't end up with random lines cached by PA (as well as VA if relevant), so they affect a guest just as much as the host.
I'm not sure why it makes any difference who the memory belongs to.
Yes, you are right. I feel the reasoning in the commit message is wrong. cxl_reset v14 already makes the invalidation mandatory on the host
path: it invalidates before the reset and again after the restore, and refuses the reset when cpu_cache_has_invalidate_memregion() is false. For v6, I will run the same sequence in vfio reset entry.
On a host where no cache maintenance driver has registered, invalidate memregion check fails and CXL reset can't be run. vfio-cxl checks the same condition at bind and continues without CXL support, so it never gives a guest HDM memory it could not clear and invalidate on the next reset.
Quoted 12 lines
Quoted 12 lines
Add
cxl_reset_dvsec_sequence(), which runs the DVSEC reset under the device lock and restores HDM state throughcxl_restore_state_after_pci_reset(), andcxl_reset_capable()to gate it on a function-scoped reset. Both are exported to the vfio-cxl module so vfio-pci core does not import the CXL namespace.CXL Reset always clears memory, so there is no caller Memory Clear choice.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>
RE: [PATCH v5 08/27] vfio/pci: Fall back to plain vfio-pci when CXL init fails
In reply to Alex WilliamsonQuoted 70 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:45 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 08/27] vfio/pci: Fall back to plain vfio-pci when CXL init failsExternal email: Use caution opening links or attachments
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_cxl_init(struct
Quoted 23 lines · On Thu, 17 Sep 2026 00:05:21 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 device is also a valid plain vfio-pci device. If the vfio-cxl provider cannot bring it up as Type-2, failing the bind would regress a device that previously worked as plain vfio-pci.
Make a non-deferred CXL init failure non-fatal: log it, drop the provider reference, leave
cxl_opsNULL, and continue driving the device as plain vfio-pci. The -EPROBE_DEFER path added earlier still retries rather than falling back.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -2271,9 +2271,17 @@ static int vfio_pci_core_cxl_init(struct
vfio_pci_core_device*vdev)Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +5
-0vfio_pci_put_cxl_ops (ops);return ret; } +/* +* Any other failure is non-fatal: a CXL device that cannot be brought +* up as Type-2 still works as plain vfio-pci, so log and continue +* rather than failing the bind. +*/ drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_core_cxl_init(struct vfio_pci_core_device *vdev) · line 2287
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +4
-1if (ret) { +pci_warn (vdev->pdev, +"CXL init failed (%d), continuing as plain vfio-pci\n" , + ret);vfio_pci_put_cxl_ops (ops); -return ret; +return 0 ; } vdev->cxl_ops = ops;
Quoted 9 lines
Why isn't this the initial implementation in patch 6? I don't fully understand why patches 7 and 8 are circling back to modify the broken behavior established in patch 6 rather than resolving it in the patch where it was introduced.
Perhaps rephrasing the message, "... continuing without CXL support". "plain vfio-pci" doesn't mean anything to a user that only knows to use vfio-pci. Thanks,
Agreed. Sorry for this churn; In v6 I will fold 07 and 08 into 06, so the provider hookup lands with its final error handling: -EPROBE_DEFER from the provider defers the bind, and any other init error leaves the device bound without the CXL extensions.
I will also update the error message to mention "continuing without CXL support" and remove any references to "plain vfio-pci".
Quoted 1 line
Alex
RE: [PATCH v5 09/27] vfio/pci: Add a generic excluded-range list
In reply to Alex WilliamsonQuoted 42 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:44 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 09/27] vfio/pci: Add a generic excluded-range listExternal email: Use caution opening links or attachments
Quoted 8 lines · On Thu, 17 Sep 2026 00:05:22 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
The MSI-X table is virtualized in
vfio_pci_bar_rw()by an open-coded x_start/x_end window that fills reads with -1 and drops writes. Now that a generic excluded-range list expresses the same fill/drop behavior, register the MSI-X table as a read and write excluded range instead of special-casing it in the read/write path.Quoted 5 lines
Add the range when the MSI-X capability is parsed in
vfio_pci_core_enable()and clear the list invfio_pci_core_disable()alongside the config teardown. The read/write path now relies solely onvfio_pci_bar_find_exclusion(), so MSI-X and a provider's (e.g. vfio-cxl) trapped registers share one mechanism.This is kind of a backwards introduction.
Quoted 8 lines
"Now that a generic excluded-range list expresses the same fill/drop behavior, register the MSI-X table as a read and write excluded range instead of special- casing it in the read/write path."
The generic excluded-range list doesn't exist now, we're adding it here. We don't change MSI-X to use it until the next patch. We're not doing anything noted in the second paragraph in this patch either. The next patch has a nearly identical commit log, please be precise in what each patch does.
Okay, in v6, I will reorganize the split as below instead of only wording change.
1. Add the excluded-range list and make the read/write path use it, with MSI-X moved onto the list and accesses that span several windows handled in the same patch.
2. Build the sparse mmap capability from the list.
3. Make ioeventfd registration and dma-buf export consult the list.
Quoted 31 lines
Quoted 7 lines
No functional change.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 209+++++++++++++++++++++++++++++++
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct
Quoted 19 lines
drivers/vfio/pci/vfio_pci_priv.h | 9 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 8 ++ include/linux/vfio_pci_core.h | 14 +++ 4 files changed, 240 insertions(+)drivers/vfio/pci/vfio_pci_core.c +1
-0@@ -24,6 +24,7 @@#include <linux/pci.h> #include <linux/pm_runtime.h> #include <linux/slab.h> +#include <linux/sort.h> #include <linux/types.h> #include <linux/uaccess.h> #include <linux/vgaarb.h> @@ -1012,6 +1013,204 @@ static int msix_mmappable_cap(struct
vfio_pci_core_device*vdev,Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0return vfio_info_add_capability (caps, &header,sizeof (header)); }
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1022
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0+struct vfio_pci_excluded_range { +struct list_head entry; +int bar; +u64 start; +u64 size; +u32 flags; +};Better packed to move the bar to the end, it only needs to be a u8, but is it instead better to make a per-BAR list and keep it sorted on insert to avoid the runtime complexity?
Okay, I will make this per-BAR and sorted on insert. With one list head per BAR the entry no longer needs a bar field.
Quoted 4 lines
There should be a comment somewhere relative to the list only being filled at the already serialized
open_deviceand therefore safe for lockless access runtime.
I Will add it on the list heads. Ranges are added only from vfio_pci_core_enable() and freed in vfio_pci_core_disable(), and every reader runs on an open device in between.
Quoted 28 lines
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct · line 1024
Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0+ +int vfio_pci_core_add_excluded_range(struct vfio_pci_core_device *vdev,int bar,
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +6
-0+ u64 start, u64 size, u32 flags) { + struct vfio_pci_excluded_range *range; + + range =kzalloc_obj (*range); +if (!range) +return -ENOMEM ;drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1041
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ + range->bar = bar; + range->start = start; + range->size = size; + range->flags = flags; +list_add_tail (&range->entry, &vdev->excluded_ranges); + +return 0 ; +} +EXPORT_SYMBOL_GPL (vfio_pci_core_add_excluded_range);If this does an ordered insert, I think we could also require that ranges cannot overlap. Everything we're considering currently, MSI-X vector table, HDM registers, are distinct. They could theoretically overlap relative to the sparse mmap capability once page aligned, but the excluded range list itself should only contain distinct, non-overlapping ranges, and I think that simplifies things a little.
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1052
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +11
-0+ +static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device +*vdev) { +struct vfio_pci_excluded_range *range, *tmp; + +list_for_each_entry_safe (range, tmp, &vdev->excluded_ranges, entry) { +list_del (&range->entry); +kfree (range); + } +} +
Agreed. The insert will return -EINVAL for a range that overlaps another on the same BAR.
I will update v6 with the new flow you have suggested.
Quoted 175 lines
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0+bool vfio_pci_bar_find_exclusion (struct vfio_pci_core_device *vdev,int bar, +loff_t pos,size_t count,bool iswrite, +size_t *x_start,size_t *x_end) { + u32 want = iswrite ? VFIO_PCI_EXCLUDE_WRITE :
VFIO_PCI_EXCLUDE_READ;Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +9
-0+struct vfio_pci_excluded_range *range; +bool found = false; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != bar || !(range->flags & want)) +continue ; +if (pos < range->start + range->size && + pos + count > range->start) { +/* drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1076
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+* A BAR can carry more than one excluded window (e.g. +* the MSI-X table and a CXL HDM decoder block). Return +* the overlapping window with the lowest start so the +* caller can walk them in order. +*/ +if (!found || range->start < *x_start) { + *x_start = range->start; + *x_end = range->start + range->size; + found = true; + }For example, we wouldn't need to iterate if the list were sorted.
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+ } + } + +return found; +} + +/* True when [start, start + len) on @bar overlaps an mmap-excluded +range. */ static bool vfio_pci_bar_mmap_excluded(struct
vfio_pci_core_device*vdev,Quoted 18 lines
drivers/vfio/pci/vfio_pci_core.c +18
-0+ int bar, u64 start, u64 len) { + struct vfio_pci_excluded_range *range; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != bar || + !(range->flags &VFIO_PCI_EXCLUDE_MMAP )) +continue ; +if (start < range->start + range->size && + start + len > range->start) +return true; + } + +return false; +} + +/* A page-aligned mmap hole, derived from an mmap-excluded range. */ +struct vfio_pci_mmap_hole { +u64 start;drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1114
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+u64 end; +}; + +static int vfio_pci_mmap_hole_cmp (const void *a,const void *b) { +const struct vfio_pci_mmap_hole *x = a, *y = b; + +if (x->start < y->start) +return -1 ; +return x->start > y->start; +}We wouldn't need sorting if we just did an ordered insert.
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+ +/* +* Advertise the BAR as mmappable minus every page-aligned mmap- excluded hole.
Quoted 25 lines
drivers/vfio/pci/vfio_pci_core.c +25
-0+* A BAR can carry several holes at unrelated offsets (for example an +MSI-X +* table and one or more trapped CXL component sub-blocks, which the +CXL spec +* locates by pointer, not at fixed offsets). +* Collect the holes, page-align and sort them, coalesce any that +overlap or +* touch, and advertise the gaps. +*/ +static int vfio_pci_excluded_sparse_cap (struct vfio_pci_core_device *vdev, +int index,struct vfio_info_cap +*caps) { +u64 bar_len =pci_resource_len (vdev->pdev, index); +struct vfio_region_info_cap_sparse_mmap *sparse; +struct vfio_pci_excluded_range *range; +struct vfio_pci_mmap_hole *holes; +int nr_holes =0 , nr_areas =0 , i, j; +size_t size; +u64 pos; +int ret; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) +if (range->bar == index && + (range->flags &VFIO_PCI_EXCLUDE_MMAP )) + nr_holes++;drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ +if (!nr_holes) +return 0 ; + + holes =kmalloc_array (nr_holes,sizeof (*holes),GFP_KERNEL ); +if (!holes) +return -ENOMEM ; + +/* +* mmap is page granular, so each hole rounds out to the page boundaries
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct · line 1150
Quoted 2 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0+* enclosing its excluded sub-range. The byte-granular exclusion still +* governs the fault and read/write paths; only the advertised mmap areas
Quoted 52 lines
drivers/vfio/pci/vfio_pci_core.c +52
-0+* round to whole pages. +*/ + i =0 ; +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != index || + !(range->flags &VFIO_PCI_EXCLUDE_MMAP )) +continue ; + holes[i].start =ALIGN_DOWN (range->start,PAGE_SIZE ); + holes[i].end =ALIGN (range->start + range->size,PAGE_SIZE ); + i++; + } + +sort (holes, nr_holes,sizeof (*holes), vfio_pci_mmap_hole_cmp, +NULL ); + +/* Coalesce holes that overlap or touch after page alignment. */ +for (i =0 , j =0 ; i < nr_holes; i++) { +if (j && holes[i].start <= holes[j -1 ].end) + holes[j -1 ].end =max (holes[j -1 ].end, holes[i].end); +else + holes[j++] = holes[i]; + } + nr_holes = j; + +/* One mmappable area per gap: before, between, and after the holes. */ +for (i =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) + nr_areas++; + pos = holes[i].end; + } +if (pos < bar_len) + nr_areas++; + + size =struct_size (sparse, areas, nr_areas); + sparse =kzalloc (size,GFP_KERNEL ); +if (!sparse) { +kfree (holes); +return -ENOMEM ; + } + + sparse->header.id =VFIO_REGION_INFO_CAP_SPARSE_MMAP ; + sparse->header.version =1 ; + sparse->nr_areas = nr_areas; + +for (i =0 , j =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = holes[i].start - pos; + j++; + } + pos = holes[i].end; + }drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1212
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+if (pos < bar_len) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = bar_len - pos; + } + +kfree (holes); + ret =vfio_info_add_capability (caps, &sparse->header, size); +kfree (sparse); +return ret; +}I imagine this could be simplified quite a bit:
Quoted 5 lines
- walk the list once, count mmap exclusions, if none return
- allocate sparse structure assuming # mmap exclusions + 1 == # areas
- walk list again aligning to page alignment, fill in areas, coalesce as needed, set resultingnr_areas.
Okay, I will update accordingly in the v6.
Quoted 91 lines
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_ioctl_get_region_info(struct
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +1
-0+ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops,@@ -1160,6 +1359,10 @@ int vfio_pci_ioctl_get_region_info(struct
vfio_device*core_vdev,drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_mmap(struct vfio_device
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0if (ret)return ret; } + ret =vfio_pci_excluded_sparse_cap (vdev, info->index, + caps); +if (ret) +return ret; }break ;@@ -1853,6 +2056,10 @@ int vfio_pci_core_mmap(struct vfio_device
*core_vdev, structvm_area_struct*vmadrivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +1
-0if (req_start + req_len > phys_len)return -EINVAL ; +/* An excluded sub-range is reachable only through its trap, not mmap. */
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_init_dev(struct vfio_device
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+if (vfio_pci_bar_mmap_excluded (vdev, index, req_start, req_len)) +return -EINVAL ; +/* * Ensure the BAR resource region is reserved for use. */ @@ -2317,6 +2524,7 @@ int vfio_pci_core_init_dev(struct vfio_device
*core_vdev)drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_release_dev(struct
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +1
-0INIT_LIST_HEAD (&vdev->dmabufs);init_rwsem (&vdev->memory_lock);xa_init (&vdev->ctx); +INIT_LIST_HEAD (&vdev->excluded_ranges); ret =vfio_pci_core_cxl_init (vdev);if (ret)@@ -2332,6 +2540,7 @@ void vfio_pci_core_release_dev(struct
vfio_device*core_vdev)Quoted 14 lines
drivers/vfio/pci/vfio_pci_core.c +1
-0container_of(core_vdev, struct vfio_pci_core_device, vdev);vfio_pci_core_cxl_release (vdev); +vfio_pci_free_excluded_ranges (vdev);mutex_destroy (&vdev->igate);mutex_destroy(&vdev->ioeventfds_lock);drivers/vfio/pci/vfio_pci_priv.h +0
-0@@ -44,6 +44,15 @@ ssize_t vfio_pci_config_rw_single(structQuoted 1 line
vfio_pci_core_device*vdev,ssize_tvfio_pci_bar_rw(struct
vfio_pci_core_device*vdev, char__user*buf,drivers/vfio/pci/vfio_pci_rdwr.c ssize_t vfio_pci_bar_rw(struct
Quoted 22 lines
size_t count, loff_t *ppos, bool iswrite);drivers/vfio/pci/vfio_pci_priv.h +11
-1+/* +* If a read (or write) to [pos, pos + count) on @bar overlaps an +excluded +* range, report the byte window do_io_rw() should fill with -1 (or +drop) and +* return true. A single access spans at most one such window. +*/ +bool vfio_pci_bar_find_exclusion (struct vfio_pci_core_device *vdev,int bar, +loff_t pos,size_t count,bool iswrite, +size_t *x_start,size_t *x_end); +#ifdef CONFIG_VFIO_PCI_VGA ssize_t vfio_pci_vga_rw (struct vfio_pci_core_device *vdev,char __user *buf,size_t count,loff_t *ppos,bool iswrite); diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c
b/drivers/vfio/pci/vfio_pci_rdwr.c
index7f14dd46de17..48da1cb08296100644drivers/vfio/pci/vfio_pci_rdwr.c +0
-0@@ -261,6 +261,14 @@ ssize_t vfio_pci_bar_rw(struct
vfio_pci_core_device*vdev, char__user*buf,drivers/vfio/pci/vfio_pci_rdwr.c ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, · line 271
Quoted 11 lines
drivers/vfio/pci/vfio_pci_rdwr.c +8
-0x_end = vdev->msix_offset + vdev->msix_size; } +/* +* A provider-excluded sub-range is filled with -1 on read and dropped on +* write for the same reason: the guest reaches it only through the trap. +* An access spans at most one exclusion window. +*/ +vfio_pci_bar_find_exclusion (vdev, bar, pos, count, iswrite, + &x_start, &x_end); +
Quoted 4 lines
This is not well integrated, we're stomping on the MSI-X and ROM set exclusion range just above. As a result, we likely need to integrate support for both at the same time, and we also need to address the multiple exclusion range per access range, that's only in the next patch for some reason.
Yes, agreed. I will follow this in the first patch reorganization mentioned above.
Quoted 7 lines
I don't think we want to move the
pci_map_rom()call elsewhere, we try to do it in correlation to the access. People are crazy enough to update device firmware while in use as well, so I'm not sure the length read from the ROM is static. Maybe this should instead be handled as a hard stop at the end of the BAR vs a soft stop at the end of the data, filled with -1 on read, and have that live outside the excluded range list? It needs some finesse, this is clunky.
I will leave pci_map_rom() in the access path as it is upstream. The ROM stays out of the list: a read past the end of the ROM data and inside the BAR returns -1, and the BAR size is the hard limit.
Quoted 2 lines
Also, what about ioeventfds and dmabufs? Thanks,
Richard asked the same on patch 21; I will add the checks in v6
vfio_pci_ioeventfd() refuses a registration that overlaps a write-excluded range, and a dma-buf export is refused when its ranges overlap an excluded range on that BAR.
v6 will move these into this part of the series (patch 3 above), so the list covers every access path from the start.
Quoted 44 lines
Alex
drivers/vfio/pci/vfio_pci_rdwr.c ssize_t vfio_pci_bar_rw(struct · line 272
Quoted 1 line
drivers/vfio/pci/vfio_pci_rdwr.c +0
-0done = vfio_pci_core_do_io_rw(vdev, res->flags & IORESOURCE_MEM,io, buf, pos,
Quoted 23 lines
drivers/vfio/pci/vfio_pci_rdwr.c +0
-0count, x_start, x_end, iswrite, max_width);(quoted diff) +0
-0
b/include/linux/vfio_pci_core.hindex7f3a2bcb5830..92e3db116068100644include/linux/vfio_pci_core.h +5
-0@@ -162,6 +162,7 @@ struct vfio_pci_core_device {struct notifier_block nb;struct rw_semaphore memory_lock;struct list_head dmabufs; +struct list_head excluded_ranges; };enum vfio_pci_io_width {@@ -172,6 +173,19 @@ enum vfio_pci_io_width { };/* Will be exported for vfio pci drivers usage */ +/* +* A provider can keep a BAR sub-range off the direct guest path, +reached only +* through its own trap. The flags select which paths are excluded: include/linux/vfio_pci_core.h enum vfio_pci_io_width { }; · line 188
Quoted 10 lines
include/linux/vfio_pci_core.h +10
-0+mmap, and +* region read and write (an excluded read fills -1, an excluded +write is +* dropped). +*/ +#define VFIO_PCI_EXCLUDE_MMAP BIT(0) +#define VFIO_PCI_EXCLUDE_READ BIT(1) +#define VFIO_PCI_EXCLUDE_WRITE BIT(2) + +int vfio_pci_core_add_excluded_range(struct vfio_pci_core_device *vdev,int bar,
Quoted 5 lines
include/linux/vfio_pci_core.h +2
-0+ u64 start, u64 size, u32 flags); + int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops,
RE: [PATCH v5 09/27] vfio/pci: Add a generic excluded-range list
In reply to Jonathan CameronQuoted 42 lines
-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Saturday, September 26, 2026 3:35 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 09/27] vfio/pci: Add a generic excluded-range listExternal email: Use caution opening links or attachments
Quoted 16 lines · On Thu, 17 Sep 2026 00:05:22 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
The MSI-X table is virtualized in
vfio_pci_bar_rw()by an open-coded x_start/x_end window that fills reads with -1 and drops writes. Now that a generic excluded-range list expresses the same fill/drop behavior, register the MSI-X table as a read and write excluded range instead of special-casing it in the read/write path.Add the range when the MSI-X capability is parsed in
vfio_pci_core_enable()and clear the list invfio_pci_core_disable()alongside the config teardown. The read/write path now relies solely onvfio_pci_bar_find_exclusion(), so MSI-X and a provider's (e.g. vfio-cxl) trapped registers share one mechanism.No functional change.
Quoted 7 lines
Quoted 2 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>A few things inline that might tidy up the implementation and make it a little more readable. Also you've use object allocators in some places but not universally.
Jonathan
Thank you; Alex asked on this patch for a sorted per-BAR list with non-overlapping ranges and a two-pass build of the sparse capability, which rewrites most of this code. The capability is then filled straight from the list, so the holes array and struct vfio_pci_mmap_hole go away, which also settles your struct range point.
In the code that remains, v6 will use kmalloc_obj(), kzalloc_flex() with __free(kfree) for the sparse capability and separate declaration lines for initialized and uninitialized variables as suggested.
Quoted 236 lines
Quoted 3 lines
--- drivers/vfio/pci/vfio_pci_core.c | 209+++++++++++++++++++++++++++++++
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct
Quoted 19 lines
drivers/vfio/pci/vfio_pci_priv.h | 9 ++ drivers/vfio/pci/vfio_pci_rdwr.c | 8 ++ include/linux/vfio_pci_core.h | 14 +++ 4 files changed, 240 insertions(+)drivers/vfio/pci/vfio_pci_core.c +1
-0@@ -24,6 +24,7 @@#include <linux/pci.h> #include <linux/pm_runtime.h> #include <linux/slab.h> +#include <linux/sort.h> #include <linux/types.h> #include <linux/uaccess.h> #include <linux/vgaarb.h> @@ -1012,6 +1013,204 @@ static int msix_mmappable_cap(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c +9
-0return vfio_info_add_capability (caps, &header,sizeof (header)); } +struct vfio_pci_excluded_range { +struct list_head entry; +int bar; +u64 start; +u64 size; +u32 flags; +}; + +int vfio_pci_core_add_excluded_range(struct vfio_pci_core_device *vdev,int bar,
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1032
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0+ u64 start, u64 size, u32 flags) { + struct vfio_pci_excluded_range *range; + + range =kzalloc_obj (*range); +if (!range) +return -ENOMEM ; +I'd be tempted to do
kmalloc_objand*range = (struct vfio_pci_excluded_range) { .bar = bar, .start = start, .size = size, .flags = flags, }; list_add_tail(...)Quoted 17 lines
drivers/vfio/pci/vfio_pci_core.c +17
-0+ range->bar = bar; + range->start = start; + range->size = size; + range->flags = flags; +list_add_tail (&range->entry, &vdev->excluded_ranges); + +return 0 ; +} +EXPORT_SYMBOL_GPL (vfio_pci_core_add_excluded_range); + +static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device +*vdev) { +struct vfio_pci_excluded_range *range, *tmp; + +list_for_each_entry_safe (range, tmp, &vdev->excluded_ranges, entry) { +list_del (&range->entry); +kfree (range);drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1105
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+ } +} + +/* A page-aligned mmap hole, derived from an mmap-excluded range. */ +struct vfio_pci_mmap_hole { +u64 start; +u64 end; +};This is just a range. Do we need another define for this specific case?
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct · line 1060
Quoted 2 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0+/* +* Advertise the BAR as mmappable minus every page-aligned mmap- excluded hole.
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0+ * A BAR can carry several holes at unrelated offsets (for example an +MSI-X + * table and one or more trapped CXL component sub-blocks, which the +CXL spec + * locates by pointer, not at fixed offsets). + * Collect the holes, page-align and sort them, coalesce any that +overlap ordrivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1131
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+* touch, and advertise the gaps. +*/ +static int vfio_pci_excluded_sparse_cap (struct vfio_pci_core_device *vdev, +int index,struct vfio_info_cap +*caps) { +u64 bar_len =pci_resource_len (vdev->pdev, index); +struct vfio_region_info_cap_sparse_mmap *sparse; +struct vfio_pci_excluded_range *range; +struct vfio_pci_mmap_hole *holes; +int nr_holes =0 , nr_areas =0 , i, j;Generally avoid mixing declarations with assignment with those that don't on a single line. It is harder to read than splitting them into each time of declaration.
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1143
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c +12
-0+size_t size; +u64 pos; +int ret; + +list_for_each_entry (range, &vdev->excluded_ranges, entry) +if (range->bar == index && + (range->flags &VFIO_PCI_EXCLUDE_MMAP )) + nr_holes++; + +if (!nr_holes) +return 0 ; +struct
vfio_pci_mmap_hole*holes__free(kfree)=
kzalloc_objs(*holes, nr_holes); or something like that. Kees is well his
way to getting rid of almost all places where objects are allocated in non typesafe ways.drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev,
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +6
-0+ holes =kmalloc_array (nr_holes,sizeof (*holes),GFP_KERNEL ); +if (!holes) +return -ENOMEM ; + +/* +* mmap is page granular, so each hole rounds out to the page boundaries
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct · line 1097
Quoted 2 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0+* enclosing its excluded sub-range. The byte-granular exclusion still +* governs the fault and read/write paths; only the advertised mmap areas
drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1158
Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +7
-0+* round to whole pages. +*/ + i =0 ; +list_for_each_entry (range, &vdev->excluded_ranges, entry) { +if (range->bar != index || + !(range->flags &VFIO_PCI_EXCLUDE_MMAP )) +continue ;holes[i++] = (struct vfio_pci_mmap_hole) { .start = ALIGN_DOWN(range->start, PAGE_SIZE), .end = ALIGN(range->start + range->size, PAGE_SIZE), };Mind you
vfio_pci_mmap_holeseems a lot like a range. So maybe just use a struct range then you getholes[i++] = DEFINE_RANGE(ALIGN_DOWN(range->start, PAGE_SIZE), ALIGN(range->start + range->size, PAGE_SIZE));Quoted 17 lines
drivers/vfio/pci/vfio_pci_core.c +17
-0+ holes[i].start =ALIGN_DOWN (range->start,PAGE_SIZE ); + holes[i].end =ALIGN (range->start + range->size,PAGE_SIZE ); + i++; + } + +sort (holes, nr_holes,sizeof (*holes), vfio_pci_mmap_hole_cmp, +NULL ); + +/* Coalesce holes that overlap or touch after page alignment. */ +for (i =0 , j =0 ; i < nr_holes; i++) { +if (j && holes[i].start <= holes[j -1 ].end) + holes[j -1 ].end =max (holes[j -1 ].end, holes[i].end); +else + holes[j++] = holes[i]; + } + nr_holes = j; +drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1184
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+/* One mmappable area per gap: before, between, and after the holes. */ +for (i =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) + nr_areas++; + pos = holes[i].end; + } +if (pos < bar_len) + nr_areas++; + + size =struct_size (sparse, areas, nr_areas);struct
vfio_region_info_cap_sparse_mmap*sparse =
kzalloc_flex(*sparse, areas, nr_areas);drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1187
Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+ sparse =kzalloc (size,GFP_KERNEL ); +if (!sparse) { +kfree (holes);with the
__freeabove this can simply return.drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1199
Quoted 12 lines
drivers/vfio/pci/vfio_pci_core.c +12
-0+return -ENOMEM ; + } + + sparse->header.id =VFIO_REGION_INFO_CAP_SPARSE_MMAP ; + sparse->header.version =1 ; + sparse->nr_areas = nr_areas; + +for (i =0 , j =0 , pos =0 ; i < nr_holes; i++) { +if (holes[i].start > pos) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = holes[i].start - pos; + j++;Bit of a long line because of the indent but I'd still go for setting whole structure in one shot - so something like:
sparse->areas[j++] = (struct
vfio_region_sparse_mmap_area) {
.offset = pos,
.size = holes[i].start - pos,
};drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1205
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +6
-0+ } + pos = holes[i].end; + } +if (pos < bar_len) { + sparse->areas[j].offset = pos; + sparse->areas[j].size = bar_len - pos;Similar for setting it in one go so we only do the indexing once + looks more like above (where this trick is more advantageous!)
sparse->areas[j] = (struct
vfio_sparse_mmap_area) {
.offset = pos,
.size =bar_len- pos,
};drivers/vfio/pci/vfio_pci_core.c static int msix_mmappable_cap(struct vfio_pci_core_device *vdev, · line 1187
Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+ } + +kfree (holes);This looks like a good place to use
__freemagic to simplify things.Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +5
-0+ ret =vfio_info_add_capability (caps, &sparse->header, size); +kfree (sparse); +return ret; +} +
RE: [PATCH v5 12/27] vfio/pci: Call the CXL open and close hooks around device use
In reply to Jonathan CameronQuoted 68 lines
-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Saturday, September 26, 2026 3:48 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 12/27] vfio/pci: Call the CXL open and close hooks around device useExternal email: Use caution opening links or attachments
Quoted 27 lines · On Thu, 17 Sep 2026 00:05:25 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
Volatile CXL register state must be sampled from live hardware when the guest opens the device, not at bind, because a low-power transition between bind and open can leave the bind-time values stale.
Add open and close to the CXL ops and call them from the common enable and close paths. A failed open unwinds the enable in reverse order:
unmap the BARs, drop the excluded ranges, then free the virtual config.Factor the BAR unmap out of
vfio_pci_core_disable()into a helper so the enable failure path and the disable path share it.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 40 +++++++++++++++++++++++++------- include/linux/vfio_pci_core.h | 2 ++ 2 files changed, 33 insertions(+), 9 deletions(-)drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -591,6 +591,21 @@ static const struct dev_pm_ops vfio_pci_core_pm_ops = {drivers/vfio/pci/vfio_pci_core.c static const struct dev_pm_ops · line 601
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +6
-0static void vfio_pci_free_excluded_ranges (struct vfio_pci_core_device *vdev); +static void vfio_pci_core_unmap_bars (struct vfio_pci_core_device +*vdev) { +struct pci_dev *pdev = vdev->pdev; +int i, bar; + +for (i =0 ; i <PCI_STD_NUM_BARS ; i++) {for (int i = 0; i < PCI_STD_NUM_BARS; i++) int bar = i + PCI_STD_RESOURCES;
Quoted 1 line
Nice to keep things tightly scoped where we can.
Agreed; I will change this in v6.
Quoted 10 lines
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +8
-0+ bar = i +PCI_STD_RESOURCES ; +if (IS_ERR_OR_NULL (vdev->barmap[bar])) +continue ; +pci_iounmap (pdev, vdev->barmap[bar]); +pci_release_selected_regions (pdev,1 << bar); + vdev->barmap[bar] =NULL ; + } +}
RE: [PATCH v5 13/27] vfio/pci: Bracket PCI resets with the CXL reset hooks
In reply to Alex WilliamsonQuoted 76 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:45 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; Simon Song (SW-GPU) <[email protected]>
Subject: Re: [PATCH v5 13/27] vfio/pci: Bracket PCI resets with the CXL reset hooksExternal email: Use caution opening links or attachments
Quoted 29 lines · On Thu, 17 Sep 2026 00:05:26 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 device must quiesce its coherent link before any reset and restore its HDM decoder state afterward, or a reset can leave the device mastering the bus over decoders that are no longer valid. This has to happen around every reset the PCI core drives for the device, not only the ones VFIO issues directly.
Add
reset_prepareandreset_doneto the CXL ops and register the matchingpci_error_handlerscallbacks.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/vfio_pci_core.c | 18 ++++++++++++++++++ include/linux/vfio_pci_core.h | 2 ++ 2 files changed, 20 insertions(+)drivers/vfio/pci/vfio_pci_core.c +2
-0@@ -2794,8 +2794,26 @@ int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, }EXPORT_SYMBOL_GPL (vfio_pci_core_sriov_configure ); +static void vfio_pci_core_reset_prepare (struct pci_dev *pdev) { +struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev);drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_sriov_configure(struct · line 2808
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ +if (vdev->cxl_ops) + vdev->cxl_ops->reset_prepare (vdev); +} + +static void vfio_pci_core_reset_done (struct pci_dev *pdev) { +struct vfio_pci_core_device *vdev =dev_get_drvdata (&pdev->dev); + +if (vdev->cxl_ops) + vdev->cxl_ops->reset_done (vdev); }Flagging this because it's introducing one more vfio-pci-core use of the convention of storing the
vfio_pci_core_devicein drvdata that's a barrier for a Rust-based variant driver. Likely this needs to be added to the per-variant boilerplate or macro so variants provide thevfio_pci_core_devicepointer rather than expecting it in drvdata.
Thanks,
Quoted 1 line
Alex
Understood. In v6 the hooks will take the device:vfio_pci_core_reset_prepare() and vfio_pci_core_reset_done() are exported with a struct vfio_pci_core_device argument, and vfio-pci calls them from its own pci_error_handlers in vfio_pci.c, which already owns the drvdata.
vfio-pci-core will not add dev_get_drvdata() call, and a
variant that binds CXL devices will call the same two above mentioned helpers from its own handlers. I am leaving vfio_pci_core_aer_err_detected() as it is in this series.
Quoted 23 lines
Quoted 23 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+const struct pci_error_handlers vfio_pci_core_err_handlers = { .error_detected = vfio_pci_core_aer_err_detected, + .reset_prepare = vfio_pci_core_reset_prepare, + .reset_done = vfio_pci_core_reset_done, };EXPORT_SYMBOL_GPL (vfio_pci_core_err_handlers);(quoted diff) +0
-0
b/include/linux/vfio_pci_core.hindex643f32c2141f..6ebf9a26275e100644include/linux/vfio_pci_core.h +2
-0@@ -72,6 +72,8 @@ struct vfio_cxl_ops {void (*release)(struct vfio_pci_core_device *vdev);int (*open_device)(struct vfio_pci_core_device *vdev);void (*close_device)(struct vfio_pci_core_device *vdev); +void (*reset_prepare)(struct vfio_pci_core_device *vdev); +void (*reset_done)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ struct module *owner; };
RE: [PATCH v5 16/27] vfio/cxl: Create the CXL memdev and set media ready at bind
In reply to Alex WilliamsonQuoted 54 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:44 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 16/27] vfio/cxl: Create the CXL memdev and set media ready at bindExternal email: Use caution opening links or attachments
Quoted 14 lines · On Thu, 17 Sep 2026 00:05:29 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
At bind, build the CXL memory device for the passed-through Type-2 accelerator so it joins the CXL topology and its HDM region resolves to a host physical range. A Type-2 device has no mailbox, so there is no media-ready register to poll: set media ready directly once the component registers validate (mirroring
drivers/net/ethernet/sfc/efx_cxl.c)As per current vfio-cxl support, reject a device with:
- more than one HDM decoder
- interleaving enabled
- whose reset the host cannot serviceQuoted 7 lines
The CXL-core allocations are grouped with devres so a failed bind unwinds
them: init failure falls back to plain vfio-pci with the device still bound, so devm would otherwise hold them until unbind.A low-power transition would reset the CXL Type-2 function and lose its CXL.mem contents, so keep it in D0 while it is assigned.
Is it the configuration or the mem contents? Isn't it the user's problem if they put the device in low power state and lose their own data? If it's our problem, what are we protecting? It's not clear to me how we can save and restore the HDM state around reset, but can't allow low power.
Quoted 7 lines
This also undercuts your patch 12 justification for the open/close hooks under the rationale that we cannot sample the hardware while it might be in a low power state.
If we do block the low power features (less desirable), it should also be at the probe and dispatch level (
-ENOTTY) rather than failing the SET operation with - EINVAL while reporting the feature available via PROBE. Thanks,
On giving this another thought, I agree, nothing stops us from restoring HDM state after low power either.
In v6 I will drop disable_idle_d3, the D0 clamp in vfio_pci_set_power_state() and the LOW_POWER_ENTRY refusal, and handle low power the way reset works:
- On entry to D3hot or runtime low power, vfio-cxl revokes the HDM mapping and its dma-buf and closes the HDM access gate.
- On return to D0, after vfio-pci-core has restored config space (the existing needs_pm_restore path), vfio-cxl restores the decoder with the same cxl-core helper the reset path uses and reopens the gate.
With low power supported, for patch 12, the device can sit in low power between bind and open. vfio-cxl will sample the live registers at open.
Quoted 2 lines
Alex
RE: [PATCH v5 16/27] vfio/cxl: Create the CXL memdev and set media ready at bind
In reply to Jonathan CameronQuoted 132 lines
-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Saturday, September 26, 2026 3:53 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 16/27] vfio/cxl: Create the CXL memdev and set media ready at bindExternal email: Use caution opening links or attachments
Quoted 19 lines · On Thu, 17 Sep 2026 00:05:29 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
At bind, build the CXL memory device for the passed-through Type-2 accelerator so it joins the CXL topology and its HDM region resolves to a host physical range. A Type-2 device has no mailbox, so there is no media-ready register to poll: set media ready directly once the component registers validate (mirroring
drivers/net/ethernet/sfc/efx_cxl.c)As per current vfio-cxl support, reject a device with:
- more than one HDM decoder
- interleaving enabled
- whose reset the host cannot serviceThe CXL-core allocations are grouped with devres so a failed bind unwinds
them: init failure falls back to plain vfio-pci with the device still bound, so devm would otherwise hold them until unbind.Quoted 5 lines
A low-power transition would reset the CXL Type-2 function and lose its CXL.mem contents, so keep it in D0 while it is assigned.
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>A small thing inline.
Quoted 2 lines
--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 118+++++++++++++++++++++++++++
Quoted 76 lines
drivers/vfio/pci/vfio_pci_core.c | 16 ++++ 2 files changed, 134 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +61
-0@@ -6,15 +6,132 @@*/ #include <linux/module.h> +#include <linux/pci.h> +#include <linux/range.h> #include <linux/vfio_pci_core.h> +#include <cxl/cxl.h> +#include <cxl/pci.h> + +/** +* struct vfio_cxl_state - per-device state for a vfio-cxl device +* @cxlds: CXL device state; kept first for +devm_cxl_dev_state_create() +* @cxlmd: memory device joined to the CXL topology at bind +* @hpa_range: host physical range of the HDM region */ struct +vfio_cxl_state { +struct cxl_dev_state cxlds; +struct cxl_memdev *cxlmd; +struct range hpa_range; +};static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) { +struct pci_dev *pdev = vdev->pdev; +struct vfio_cxl_state *cxl; +struct cxl_memdev *cxlmd; +u64 hdm_size, serial; +u16 dvsec; +int ret; + +/* +* pdev->hdm is cached at PCI enumeration, before any driver binds, so a +* device without it has no usable HDM decoder. Fall back to plain +* vfio-pci rather than deferring the bind forever. +*/ +if (!pdev->hdm) +return -ENODEV ; + +/* The guest drives one virtual decoder; multiple are unsupported. */ +if (pdev->hdm->decoder_count !=1 ) +return -EOPNOTSUPP ; + +/* An interleaved decoder cannot be mapped 1:1 to the guest. */ +if (pdev->hdm->settings[0 ].interleave_ways !=1 ) +return -EOPNOTSUPP ; + +/* +* The guest drives resets through the CXL Device DVSEC and polls the +* shadow for completion. If the host cannot service a function-scoped +* CXL reset, that request could never complete, so refuse the device +* rather than advertise a reset the guest would poll on forever. +*/ +if (!cxl_reset_capable (pdev)) +return -EOPNOTSUPP ; + + hdm_size =range_len (&pdev->hdm->settings[0 ].hpa_range); +if (!hdm_size) +return -ENXIO ; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); + serial =pci_get_dsn (pdev); + +/* +* Group the CXL-core allocations so a later failure unwinds them here. +* A failed init falls back to plain vfio-pci with the device still +* bound, so devm would otherwise hold them until unbind. +*/
Quoted 8 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c line 76
Quoted 3 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0+if (!devres_open_group (&pdev->dev,NULL ,GFP_KERNEL )) +return -ENOMEM ; +I'd be tempted to factor out the stuff that is unwound via the group. If this were simple a call to a helper function, that helper could return directly on error giving us simpler code flow. Then you'd just check the helper return to find out if you should close or release the group.
Okay, agreed.
I will move the memdev setup into a helper that returns on the first error, and the caller closes the group on success or releases it on failure.
Quoted 68 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c line 76
Quoted 2 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0+ cxl = devm_cxl_dev_state_create(&pdev->dev, CXL_DEVTYPE_DEVMEM,serial,
Quoted 65 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +53
-1+ dvsec, struct vfio_cxl_state, cxlds, + false); +if (!cxl) { + ret = -ENOMEM ; + goto err; + } + + ret =cxl_pci_setup_regs (pdev,CXL_REGLOC_RBI_COMPONENT , + &cxl->cxlds.reg_map); +if (ret) { +pci_err (pdev,"vfio-cxl: no component registers\n" ); + goto err; + } + +if (!cxl->cxlds.reg_map.component_map.hdm_decoder.valid) { +pci_err (pdev,"vfio-cxl: HDM decoder registers not found\n" ); + ret = -ENODEV ; + goto err; + } + +/* +* A Type-2 accelerator has no mailbox and no media-ready register, so +* set media ready directly. +*/ + cxl->cxlds.media_ready = true; + + ret =cxl_set_capacity (&cxl->cxlds, hdm_size); +if (ret) + goto err; + + cxlmd =devm_cxl_probe_mem (&cxl->cxlds, &cxl->hpa_range); +if (IS_ERR (cxlmd)) { + ret =PTR_ERR (cxlmd); + goto err; + } + + cxl->cxlmd = cxlmd; +devres_close_group (&pdev->dev,NULL ); + +/* +* Powering a CXL Type-2 function down and back up reinitializes its +* device state and discards the contents of its coherent memory. Pin +* it in D0 for as long as it is assigned so CXL.mem stays intact. +*/ + vdev->disable_idle_d3 = true; + vdev->cxl = cxl; +return 0 ; + +err: +devres_release_group (&pdev->dev,NULL ); +return ret; }static void vfio_cxl_release_device (struct vfio_pci_core_device *vdev) { + vdev->cxl =NULL ; } static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev) @@ -59,3 +176,4 @@ module_exit(vfio_cxl_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("VFIO support for CXL Type-2 devices"); +MODULE_IMPORT_NS ("CXL" );
RE: [PATCH v5 17/27] vfio/cxl: Own the whole component register BAR
In reply to Alex WilliamsonQuoted 66 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:44 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 17/27] vfio/cxl: Own the whole component register BARExternal email: Use caution opening links or attachments
Quoted 5 lines · On Thu, 17 Sep 2026 00:05:30 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
devm_cxl_probe_mem()maps the component registers through the CXL core, which by default claims each HDM and RAS sub-block. That collides with the full-BAR request vfio-pci-core makes when the guest opensthe device.
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_init_device(struct
Quoted 18 lines
After the component registers are discovered, declare the containing BAR owned with
cxl_reg_map_add_owned_resource(). The core then ioremaps the sub-blocks without requesting them, so vfio-pci-core owns the whole BAR and the requests do not collide.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0@@ -89,6 +89,15 @@ static int vfio_cxl_init_device(struct
vfio_pci_core_device*vdev)drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) · line 100
Quoted 13 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0goto err; } +/* +* vfio-pci-core requests the whole component-register BAR when the +* guest opens the device. Declare that BAR owned so the CXL core +* ioremaps the HDM and RAS sub-blocks without claiming them, and the +* full-BAR request does not collide. +*/ +cxl_reg_map_add_owned_resource (&cxl->cxlds.reg_map, +pci_resource_n (pdev, + pdev->hdm->hdm_bar)); +
Quoted 5 lines
The result of this operation is setting a singleton "owned" pointer to this resource, whereas the name suggests it's adding the resource to some set of owned resources. Is "set" really the more idiomatic verb for this than "add"? Thanks,
Yes. I will rename it to cxl_reg_map_set_owned_resource() in v6.
Quoted 5 lines
Alex
Quoted 3 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0if (!cxl->cxlds.reg_map.component_map.hdm_decoder.valid) {pci_err (pdev,"vfio-cxl: HDM decoder registers not found\n" ); ret = -ENODEV ;
RE: [PATCH v5 18/27] vfio/cxl: Expose the HDM memory region to the guest
In reply to Alex WilliamsonQuoted 252 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:44 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 18/27] vfio/cxl: Expose the HDM memory region to the guestExternal email: Use caution opening links or attachments
Quoted 8 lines · On Thu, 17 Sep 2026 00:05:31 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 guest maps the device HDM memory to use its coherent CXL.mem. The HDM memory is a host physical range with no struct page, so the guest and KVM need a write-back mapping of it.
Register the range as an mmap-able region under
VFIO_REGION_TYPE_PCI_VENDOR_TYPEwith the CXL vendor id rather thana
Quoted 15 lines
bespoke region type, per open because
vfio_pci_core_disable()tears down all dynamic regions on close.Own the resolved host physical range exclusively (
IORESOURCE_EXCLUSIVE) so nothing,/dev/memincluded, can map a conflicting cacheable alias that would fault the host once the range is mapped write-back. There is no devm form of the exclusive request, so pair it with a devm release action.
vfio_pci_zap_bars()only unmaps the fixed PCI BAR range, so revoke mmap-capable device-specific regions there too. Otherwise a Memory-Space disable, a D3 transition, or a reset would leave the guest a live mapping into quiesced device memory; the fault handler re-gates on device state before it inserts a pfn again.Quoted 4 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 186+++++++++++++++++++++++++++
Quoted 76 lines
drivers/vfio/pci/vfio_pci_core.c | 37 ++++++ include/linux/vfio_pci_core.h | 1 + include/uapi/linux/vfio.h | 4 + 4 files changed, 228 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +45
-0@@ -5,9 +5,13 @@* Copyright (c) 2026 NVIDIA Corporation & Affiliates */ +#include <linux/cleanup.h> +#include <linux/io.h> +#include <linux/mm.h> #include <linux/module.h> #include <linux/pci.h> #include <linux/range.h> +#include <linux/uaccess.h> #include <linux/vfio_pci_core.h> #include <cxl/cxl.h> #include <cxl/pci.h> @@ -17,13 +21,144 @@* @cxlds: CXL device state; kept first for devm_cxl_dev_state_create() * @cxlmd: memory device joined to the CXL topology at bind * @hpa_range: host physical range of the HDM region +* @hdm_valid: true when host CPU access to the HDM range is safe; +under memory_lock */ struct vfio_cxl_state {struct cxl_dev_state cxlds;struct cxl_memdev *cxlmd;struct range hpa_range; +bool hdm_valid; }; +static unsigned long vfio_cxl_mem_pgoff (struct vm_area_struct *vma, +unsigned long addr) { +unsigned long mask = (1U << (VFIO_PCI_OFFSET_SHIFT - +PAGE_SHIFT )) -1 ; + +return (vma->vm_pgoff & mask) + ((addr - vma->vm_start) >> +PAGE_SHIFT ); } + +static vm_fault_t vfio_cxl_mem_huge_fault (struct vm_fault *vmf, +unsigned int order) { +struct vm_area_struct *vma = vmf->vma; +struct vfio_pci_core_device *vdev = vma->vm_private_data; +struct vfio_cxl_state *cxl = vdev->cxl; +unsigned long addr =ALIGN_DOWN (vmf->address,PAGE_SIZE << order); +unsigned long pfn =PHYS_PFN (cxl->hpa_range.start) + +vfio_cxl_mem_pgoff (vma, addr); +vm_fault_t ret =VM_FAULT_FALLBACK ; + +if (is_aligned_for_order (vma, addr, pfn, order)) { + scoped_guard(rwsem_read, &vdev->memory_lock) { +/* +* Insert a PFN only for a known-good decoder whose +* media is ready and whose Memory-Space is enabled. +*/ +if (__vfio_pci_memory_enabled (vdev) && + cxl->hdm_valid && cxl->cxlds.media_ready) + ret =vfio_pci_vmf_insert_pfn (vdev, vmf, pfn, + order); +else + ret =VM_FAULT_SIGBUS ; + } + } + +return ret; +} + +static vm_fault_t vfio_cxl_mem_fault (struct vm_fault *vmf) { +return vfio_cxl_mem_huge_fault (vmf,0 ); }drivers/vfio/pci/cxl/vfio_cxl_core.c line 81
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+ +static const struct vm_operations_struct vfio_cxl_mem_vm_ops = { + .fault = vfio_cxl_mem_fault, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .huge_fault = vfio_cxl_mem_huge_fault, #endif }; + +static int vfio_cxl_mem_mmap (struct vfio_pci_core_device *vdev, +struct vfio_pci_region *region, +struct vm_area_struct *vma) { + unsigned long mask = (1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) -1;
drivers/vfio/pci/cxl/vfio_cxl_core.c
Quoted 8 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +8
-0+u64 req_start = (vma->vm_pgoff & mask) <<PAGE_SHIFT ; +u64 req_len = vma->vm_end - vma->vm_start; + +if (req_start + req_len > region->size) +return -EINVAL ; + +/* +* CXL.mem is coherent memory, so leave the mapping write-back cacheable.
drivers/vfio/pci/cxl/vfio_cxl_core.c line 91
Quoted 2 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +2
-0+*/ + vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND |
VM_DONTDUMP);Quoted 18 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +18
-0+ vma->vm_ops = &vfio_cxl_mem_vm_ops; + vma->vm_private_data = vdev; + +return 0 ; +} + +static ssize_t vfio_cxl_mem_rw (struct vfio_pci_core_device *vdev, +char __user *buf,size_t count,loff_t *ppos, +bool iswrite) { +struct vfio_cxl_state *cxl = vdev->cxl; +u64 pos = *ppos &VFIO_PCI_OFFSET_MASK ; +void *mem; +ssize_t done; + +if (pos >=range_len (&cxl->hpa_range)) +return -EINVAL ; + count =min_t (size_t, count,range_len (&cxl->hpa_range) - pos); +drivers/vfio/pci/cxl/vfio_cxl_core.c
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+ scoped_guard(rwsem_read, &vdev->memory_lock) { +/* +* Same gate as the fault path: only touch the HDM range with +* the decoder in a known-good state AND Memory-Space enabled, +* or a host CPU access aborts as a fatal SError. +*/ +if (!cxl->hdm_valid || !__vfio_pci_memory_enabled (vdev)) +return -EIO ; + + mem = memremap(cxl->hpa_range.start + pos, count,
MEMREMAP_WB);drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_device(struct
Quoted 58 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +48
-0+if (!mem) +return -ENOMEM ; +if (iswrite) + done =copy_from_user (mem, buf, count) ? -EFAULT : count; +else + done =copy_to_user (buf, mem, count) ? -EFAULT : count; +memunmap (mem); + } +if (done >0 ) + *ppos += done; + +return done; +} + +/* +* The CXL regions carry no per-region state (region->data is the +shared, +* devm-managed vfio_cxl_state), so releasing a region is a no-op. +*/ +static void vfio_cxl_region_release (struct vfio_pci_core_device *vdev, +struct vfio_pci_region *region) { } + +static const struct vfio_pci_regops vfio_cxl_mem_regops = { + .rw = vfio_cxl_mem_rw, + .mmap = vfio_cxl_mem_mmap, + .release = vfio_cxl_region_release, }; + +static void vfio_cxl_release_hpa (void *data) { +struct vfio_cxl_state *cxl = data; + +release_mem_region (cxl->hpa_range.start, +range_len (&cxl->hpa_range)); } +static int vfio_cxl_init_device (struct vfio_pci_core_device *vdev) { struct pci_dev *pdev = vdev->pdev; @@ -120,6 +255,21 @@ static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) goto err; } +/* +* Claim the range IORESOURCE_EXCLUSIVE so no conflicting cacheable +* alias can fault the host once it is mapped write-back; there is no +* devm form, so pair it with a devm release action. +*/ +if (!request_mem_region_exclusive (cxl->hpa_range.start, +range_len (&cxl->hpa_range), +"vfio-cxl-hdm" )) { + ret = -EBUSY ; + goto err; + } + ret =devm_add_action_or_reset (&pdev->dev, vfio_cxl_release_hpa, cxl); +if (ret) + goto err; + cxl->cxlmd = cxlmd;devres_close_group (&pdev->dev,NULL );@@ -143,13 +293,49 @@ static void vfio_cxl_release_device(struct
vfio_pci_core_device*vdev)drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev)
Quoted 4 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0vdev->cxl =NULL ; } +static int vfio_cxl_add_region(struct vfio_pci_core_device *vdev, u32subtype,
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_device(struct · line 299
Quoted 3 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0+ const struct vfio_pci_regops *ops, size_t size, + u32 flags) { + u32 type = VFIO_REGION_TYPE_PCI_VENDOR_TYPE |
PCI_VENDOR_ID_CXL;
Quoted 15 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev) · line 314
Quoted 13 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +12
-0+ +return vfio_pci_core_register_dev_region (vdev, type, subtype, ops, + size, flags, +vdev->cxl); } +static int vfio_cxl_open_device (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; +int ret; + +/* +* vfio_pci_core_disable() frees all dynamic regions on close, so register +* them here per open rather than at bind. A failed first open never +* reaches close_device(), so unwind on error. There's no unwind here.
Yes, the comment is wrong. I will update it.
Quoted 21 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev)
Quoted 12 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +11
-0+*/ + ret =vfio_cxl_add_region (vdev,VFIO_REGION_SUBTYPE_CXL_MEM , + &vfio_cxl_mem_regops,range_len (&cxl->hpa_range), +VFIO_REGION_INFO_FLAG_READ | +VFIO_REGION_INFO_FLAG_WRITE | +VFIO_REGION_INFO_FLAG_MMAP ); +if (ret) +return ret; + +/* +* The decoder is firmware-committed, so host access to the HDM range is
Quoted 8 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +5
-0+* safe. Open the access gate; reset and power transitions clear it until +* the decoder is restored. +*/ + cxl->hdm_valid = true; +return 0 ; }
Quoted 11 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev) · line 339
Quoted 6 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0static void vfio_cxl_close_device (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; + + cxl->hdm_valid = false; }If
hdm_validis set onopen_deviceand cleared onclose_device, how is this not redundant toopen_count? It's totally superfluous everywhere it's tested in this patch.
Yes, in this patch it is redundant.hdm_valid is meant to say that the HDM decoder is known to be committed, and that only changes across reset, bus reset and low power.
In v6 it will be set at bind once the committed decoder is verified, introduced in the first patch that changes it and not touched at open or close.
Quoted 35 lines
Quoted 18 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0static void vfio_cxl_reset_prepare (struct vfio_pci_core_device *vdev)drivers/vfio/pci/vfio_pci_core.c +7
-0@@ -1295,6 +1295,23 @@ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev, }EXPORT_SYMBOL_GPL (vfio_pci_core_register_dev_region ); +/* +* Unregister the most recently registered dynamic region. Used to +unwind a +* partially built region set on an open-time error; regions are +otherwise +* released together in vfio_pci_core_disable(). +*/ drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_register_dev_region(struct · line 1314
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+void vfio_pci_core_unregister_dev_region (struct vfio_pci_core_device +*vdev) { +struct vfio_pci_region *region; + +if (WARN_ON (!vdev->num_regions)) +return ; + + region = &vdev->region[--vdev->num_regions]; + region->ops->release (vdev, region); } +EXPORT_SYMBOL_GPL (vfio_pci_core_unregister_dev_region);This is not a good interface, implicitly popping the last dev region added. The whole interface should change to push and pop semantics if we're doing this, but we shouldn't do this. It would be better to ask to remove a specific region, identified by some compliment of the creation parameters, with a memmove
+ collapse on the array. Or perhaps this justifies another data structure.
Quoted 4 lines
This is also unused in this patch. Why's it added here? This patch already does too much. We could have lead with read/write access and followed with mmap support in another patch.
Okay; in v6, I will drop vfio_pci_core_unregister_dev_region(). Its only use is to undo regions after a failed open, and vfio_pci_core_enable() can do that for every region: I will move the region release loop from vfio_pci_core_disable() into a helper and call it from the enable error path as well.
I will also split this patch into read/write access first and mmap second.
Quoted 22 lines
drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev,
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +1
-1+static int vfio_pci_info_atomic_cap (struct vfio_pci_core_device *vdev, struct vfio_info_cap *caps) { @@ -1972,8 +1989,28 @@ static void vfio_pci_zap_bars(struct
vfio_pci_core_device*vdev)Quoted 1 line
loff_tstart =
VFIO_PCI_INDEX_TO_OFFSET(VFIO_PCI_BAR0_REGION_INDEX);Quoted 1 line
loff_tend =
VFIO_PCI_INDEX_TO_OFFSET(VFIO_PCI_ROM_REGION_INDEX);drivers/vfio/pci/vfio_pci_core.c static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev) · line 2001
Quoted 12 lines
loff_t len = end - start;+ unsigned int i;
unmap_mapping_range(core_vdev->inode->i_mapping, start, len,true);
drivers/vfio/pci/vfio_pci_core.c +7
-0+ +/* +* The unmap above covers the PCI BARs; mmap-capable device-specific +* regions (e.g. a vfio-cxl HDM window) sit above that range, so revoke +* them here too, or a Memory-Space disable, D3/PM transition, or reset +* would leave the guest a live mapping into quiesced device memory. +* Callers hold memory_lock, so the region array is stable.
Quoted 4 lines
memory_lockdoesn't protect the region array, it's allocated at init/open and considered stable. Like v4, this is arguing where it's convenient that we need to guard against PM transitions while also preventing PM transitions. Why don't we just support PM?
Agreed on both. I will remove the comment and v6 will supports PM. (added more details in patch 16 reply)
Quoted 14 lines
drivers/vfio/pci/vfio_pci_core.c static void vfio_pci_zap_bars(struct vfio_pci_core_device *vdev) · line 2013
Quoted 13 lines
drivers/vfio/pci/vfio_pci_core.c +12
-0+*/ +for (i =0 ; i < vdev->num_regions; i++) { +struct vfio_pci_region *region = &vdev->region[i]; +loff_t roff; + +if (!(region->flags &VFIO_REGION_INFO_FLAG_MMAP )) +continue ; + + roff =VFIO_PCI_INDEX_TO_OFFSET (VFIO_PCI_NUM_REGIONS + i); +unmap_mapping_range (core_vdev->inode->i_mapping, roff, + region->size, true); + }
Quoted 9 lines
There's an assumption here that doesn't seem well founded that a device specific region that support mmap is necessarily dependent on the memory enable state of the device. We could have chosen to report the coherent memory nvgrace uses as a device specific region, we know it's not tied to memory enable. We could support mmap on something like the IGD OpRegion if we chose to map a whole page for it. There's an implicit dependency here that I don't see baked in to simply supporting mmap. Thanks,
Okay. In v6 I will make vfio_pci_zap_bars() cover only the BARs. vfio-pci-core will export a helper that revokes one device-specific region, its mmap and its dma-bufs, and one helper to re-arm it.
vfio-cxl will calls these on the events that change the decoder state: reset, bus reset and low power.
Quoted 37 lines
Alex
include/linux/vfio_pci_core.h int vfio_pci_core_register_dev_region(struct
Quoted 9 lines
}
void vfio_pci_zap_and_down_write_memory_lock(struct
vfio_pci_core_device*vdev) diff --gita/include/linux/vfio_pci_core.hb/include/linux/vfio_pci_core.hindex475a0ecf9e4f..39a28cc6ae8c100644include/linux/vfio_pci_core.h +0
-0@@ -198,6 +198,7 @@ int vfio_pci_core_register_dev_region(struct
vfio_pci_core_device*vdev,Quoted 25 lines
include/linux/vfio_pci_core.h +2
-0unsigned int type, unsigned int subtype, const struct vfio_pci_regops *ops, size_t size, u32 flags, void *data); +void vfio_pci_core_unregister_dev_region (struct vfio_pci_core_device +*vdev);void vfio_pci_core_close_device (struct vfio_device *core_vdev);int
vfio_pci_core_init_dev(struct vfio_device *core_vdev); voidvfio_pci_core_release_dev(struct vfio_device *core_vdev); diff --gita/include/uapi/linux/vfio.hb/include/uapi/linux/vfio.hindexe41437fa17ad..1bf86763c0f7100644include/uapi/linux/vfio.h +5
-0@@ -370,6 +370,10 @@ struct vfio_region_info_cap_type {*/ #define VFIO_REGION_SUBTYPE_IBM_NVLINK2_ATSD (1) +/* CXL Type-2 device (0x1e98) sub-types for +VFIO_REGION_TYPE_PCI_VENDOR_TYPE */ +/* CXL.mem HDM region of a Type-2 device, mmap-able */ +#define VFIO_REGION_SUBTYPE_CXL_MEM (1) +/* sub-types for VFIO_REGION_TYPE_GFX */ #define VFIO_REGION_SUBTYPE_GFX_EDID (1)
RE: [PATCH v5 20/27] vfio/cxl: Expose the HDM decoder registers read-only to the guest
In reply to Alex WilliamsonQuoted 111 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:44 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 20/27] vfio/cxl: Expose the HDM decoder registers read-only to the guestExternal email: Use caution opening links or attachments
Quoted 21 lines · On Thu, 17 Sep 2026 00:05:33 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 guest reads the HDM decoder registers to learn the HDM region it was handed. Those registers live in the component BAR that vfio-pci owns.
Map the decoder block at bind: its location comes from the
pdev->hdmenumeration cache, and vfio-pci owns the BAR, so map it without claiming the block and expose it as a second, read-only region underVFIO_REGION_TYPE_PCI_VENDOR_TYPEwith the CXL vendor id, registered per open like the HDM memory region.Serve reads live from the mapped block and absorb writes without forwarding them to hardware.
Registering a second region is the first point at which an open-time failure must unwind an already-registered region, so add
vfio_pci_core_unregister_dev_region()to drop the most recently registered region, and use it to unwind the HDM memory region if the decoder region fails to register.Quoted 4 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 81+++++++++++++++++++++++++++-
Quoted 9 lines
include/uapi/linux/vfio.h | 2 + 2 files changed, 82 insertions(+), 1 deletion(-)drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0drivers/vfio/pci/cxl/vfio_cxl_core.c line 31
Quoted 8 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0@@ -24,6 +24,8 @@* @cxlmd: memory device joined to the CXL topology at bind * @hpa_range: host physical range of the HDM region * @hdm_pfn_space: HDM-region pfn range registered with memory_failure() +* @hdm_regs: mapped HDM decoder registers, read live by the decoder +region +* @hdm_len: length of the HDM decoder register block * @hdm_valid: true when host CPU access to the HDM range is safe; under
memory_lockdrivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_register_pfn_space(struct
Quoted 12 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +2
-0*/ struct vfio_cxl_state {@@ -31,6 +33,8 @@ struct vfio_cxl_state {struct cxl_memdev *cxlmd;struct range hpa_range;struct pfn_address_space hdm_pfn_space; +void __iomem *hdm_regs; +u32 hdm_len;bool hdm_valid; };@@ -212,6 +216,56 @@ static int vfio_cxl_register_pfn_space(struct
vfio_pci_core_device*vdev)Quoted 13 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0return register_pfn_address_space (&cxl->hdm_pfn_space); } +static ssize_t vfio_cxl_comp_rw (struct vfio_pci_core_device *vdev, +char __user *buf,size_t count,loff_t *ppos, +bool iswrite) { +struct vfio_cxl_state *cxl = vdev->cxl; +loff_t pos = *ppos &VFIO_PCI_OFFSET_MASK ; +void *tmp; + +if (pos >= cxl->hdm_len) +return -EINVAL ; +drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_register_pfn_space(struct vfio_pci_core_device *vdev)
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+/* The decoder registers take only aligned dword accesses. */ +if (pos %sizeof (u32) || count %sizeof (u32)) +return -EINVAL ; + + count =min_t (size_t, count, cxl->hdm_len - pos); + +/* +* The host committed and locked the physical decoder before the guest +* saw the device, so the guest never drives it: absorb writes without +* forwarding them to hardware. The guest programs a GPA that the VMM
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_register_pfn_space(struct · line 240
Quoted 2 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +2
-0+* virtualizes; reads return the live registers, which already report the +* decoder committed. BASE_LOW and BASE_HIGH carry the host HPA, visible
Quoted 14 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_register_pfn_space(struct vfio_pci_core_device *vdev) · line 253
Quoted 12 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +12
-0+* only to the trusted VMM that virtualizes it away from the guest. +*/ +if (iswrite) { + *ppos += count; +return count; + } + + tmp =kmalloc (count,GFP_KERNEL ); +if (!tmp) +return -ENOMEM ; + +memcpy_fromio (tmp, cxl->hdm_regs + pos, count);There's no memory-enabled gate on this access to mapped BAR space.
Right. In v6, the read will take a memory_lock and return -EIO while memory is Disabled.
Quoted 70 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_open_device(struct
Quoted 40 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +29
-0+if (copy_to_user (buf, tmp, count)) { +kfree (tmp); +return -EFAULT ; + } +kfree (tmp); + + *ppos += count; +return count; +} + +static const struct vfio_pci_regops vfio_cxl_comp_regops = { + .rw = vfio_cxl_comp_rw, + .release = vfio_cxl_region_release, }; +static void vfio_cxl_release_hpa (void *data) { struct vfio_cxl_state *cxl = data; @@ -299,6 +353,21 @@ static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) goto err; } +/* +* Map the HDM decoder registers so the decoder region can read them +* live. The block location comes from the enumeration cache in +* pdev->hdm; vfio-pci owns the BAR, so map without claiming the block. +*/ + cxl->hdm_regs =devm_ioremap (&pdev->dev, +pci_resource_start (pdev, pdev->hdm->hdm_bar) + + pdev->hdm->hdm_offset, pdev->hdm->hdm_size); +if (!cxl->hdm_regs) { + ret = -ENOMEM ; + goto err; + } + + cxl->hdm_len = pdev->hdm->hdm_size; +/* * A Type-2 accelerator has no mailbox and no media-ready register, so * set media ready directly. @@ -381,6 +450,13 @@ static int vfio_cxl_open_device(struct
vfio_pci_core_device*vdev)drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev)
Quoted 4 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0if (ret)return ret; + ret = vfio_cxl_add_region(vdev,
VFIO_REGION_SUBTYPE_CXL_COMP_REGS,Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +6
-0+ &vfio_cxl_comp_regops, cxl->hdm_len, +VFIO_REGION_INFO_FLAG_READ | + VFIO_REGION_INFO_FLAG_WRITE); +if (ret) + goto err_unregister_mem; +/* * The HDM region is advertised mmap-able, so a fd holder can fault its * struct-page-less device memory in from the host CPU. Register it with @@ -389,7 +465,7 @@ static int vfio_cxl_open_device(struct
vfio_pci_core_device*vdev)Quoted 13 lines
*/ ret = vfio_cxl_register_pfn_space(vdev); if (ret && ret != -EOPNOTSUPP)drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-1- goto err_unregister_mem; + goto err_unregister_comp;/* * The decoder is firmware-committed, so host access to the HDM range is @@ -400,6 +476,9 @@ static int
vfio_cxl_open_device(struct vfio_pci_core_device *vdev)return 0;
Quoted 7 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev) · line 483
Quoted 5 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +3
-0+err_unregister_comp: +vfio_pci_core_unregister_dev_region (vdev); + err_unregister_mem:vfio_pci_core_unregister_dev_region (vdev);As noted previously, this is a bad interface, this is why. Thanks,
Understood; These calls go away with the patch 18 change.
Quoted 18 lines
Alex
Quoted 15 lines
include/uapi/linux/vfio.h +0
-0@@ -373,6 +373,8 @@ struct vfio_region_info_cap_type {/* CXL Type-2 device (0x1e98) sub-types for
VFIO_REGION_TYPE_PCI_VENDOR_TYPE*/
/* CXL.mem HDM region of a Type-2 device, mmap-able */
#defineVFIO_REGION_SUBTYPE_CXL_MEM(1)include/uapi/linux/vfio.h +2
-0+/* CXL HDM decoder registers: read live, guest writes are absorbed */ +#define VFIO_REGION_SUBTYPE_CXL_COMP_REGS (2)/* sub-types for VFIO_REGION_TYPE_GFX */ #define VFIO_REGION_SUBTYPE_GFX_EDID (1)
RE: [PATCH v5 22/27] vfio/cxl: Clear the HDM access gate after a hot reset
In reply to Alex WilliamsonQuoted 51 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:43 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 22/27] vfio/cxl: Clear the HDM access gate after a hot resetExternal email: Use caution opening links or attachments
Quoted 13 lines · On Thu, 17 Sep 2026 00:05:35 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
The HDM memory fault inserts a PFN only when the decoder is in a known-good state (
cxl->hdm_valid), so the host CPU never faults the coherent range into a disabled decoder.
VFIO_DEVICE_PCI_HOT_RESETdrives a plain secondary bus reset rather than the CXL reset sequence, so nothing restores the decoder in thereset_donehandler. A secondary bus reset only reaches a CXL endpoint when the upstream port has SBR unmasked (CXL r3.1 sec 8.1.5.2); in that case the reset decommits the decoder, so clearcxl->hdm_validto keep a later fault from inserting a PFN into a dead decoder.Quoted 5 lines
A subsequent
VFIO_DEVICE_RESETruns the CXL reset sequence and restores it, and a masked SBR is a no-op that leaves the decoder intact.
vfio_cxl_sbr_unmasked()mirrors thecxl_sbr_masked()check the PCI core uses (drivers/pci/pci.c) for its own CXL bus reset.This all sounds very broken. If a hot reset is masked, it should fail, not silently do nothing. If a hot reset is unmasked, we can't just invent that the user needs to follow it with a reset ioctl, and the decoder is dead for the remainder of their session otherwise.
Quoted 7 lines
So on one hand, we have a reset we can't use because it doesn't do anything, and on the other we have a reset that we can't use because in doing something it makes the device effectively unrecoverable, without some assumption to perform a reset ioctl.
Are bus resets just not supported on CXL devices through vfio-pci? Thanks,
I will try to handle both these cases in v6 correctly:
1. Masked
with Unmask SBR clear in the port's CXL DVSEC, the SBR does nothing to the device (CXL r3.1 sec 8.1.5.2). The PCI core already refuses pci_reset_bus_function() in that case. I will add the same check to the bus and slot reset paths and their probes, so VFIO_DEVICE_GET_PCI_HOT_RESET_INFO and VFIO_DEVICE_PCI_HOT_RESET fail for the device instead of doing nothing. A VMM will then use VFIO_DEVICE_RESET, which runs the CXL reset. I will add the PCI core change in its own patch.
2. Unmasked
The reset clears the decoder, so vfio-cxl's reset_done restores it with the same cxl-core helper the PCI core's cxl_bus reset method calls after its SBR, and reopens the HDM gate. If the restore fails, the hot reset ioctl returns the error. No follow-up VFIO_DEVICE_RESET is needed.
Quoted 65 lines
Alex
Quoted 27 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)drivers/vfio/pci/cxl/vfio_cxl_core.c +12
-0@@ -514,8 +514,46 @@ static void vfio_cxl_reset_prepare(struct vfio_pci_core_device *vdev) { } +/* +* A secondary bus reset only reaches a CXL endpoint when the +upstream port +* has SBR unmasked (CXL r3.1 sec 8.1.5.2); otherwise the Bridge +Control SBR +* bit is ignored and the decoder is left intact. Mirrors the +cxl_sbr_masked() +* check the PCI core uses for its own CXL bus reset. +*/ +static bool vfio_cxl_sbr_unmasked (struct pci_dev *pdev) { +struct pci_dev *bridge =pci_upstream_bridge (pdev); +u16 dvsec, ctl;drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_reset_prepare(struct · line 537
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+ +if (!bridge) +return false; + + dvsec =pci_find_dvsec_capability (bridge,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_PORT ); +if (!dvsec) +return false; + + if (pci_read_config_word(bridge, dvsec + PCI_DVSEC_CXL_PORT_CTL,&ctl))
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_reset_prepare(struct vfio_pci_core_device *vdev)
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +9
-0+return false; + +return ctl &PCI_DVSEC_CXL_PORT_CTL_UNMASK_SBR ; +} +static void vfio_cxl_reset_done (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; + +/* +* VFIO_DEVICE_PCI_HOT_RESET drives a plain secondary bus reset, not the
Quoted 12 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +9
-0+* CXL-aware cxl_reset_bus_function(), so nothing restores the HDM +* decoder here. When the upstream port has SBR unmasked the reset +* decommits the decoder; gate host access to the HDM range so a later +* fault cannot insert a PFN into a dead decoder. A VFIO_DEVICE_RESET +* then runs the CXL reset sequence and restores it. A masked SBR is a +* no-op and leaves the decoder intact. +*/ +if (vfio_cxl_sbr_unmasked (vdev->pdev)) + cxl->hdm_valid = false; }static const struct vfio_cxl_ops vfio_cxl_ops = {
RE: [PATCH v5 25/27] vfio/cxl: Run the CXL reset at the vfio reset points
In reply to Alex WilliamsonQuoted 106 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:43 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 25/27] vfio/cxl: Run the CXL reset at the vfio reset pointsExternal email: Use caution opening links or attachments
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_reset_done(struct
Quoted 37 lines · On Thu, 17 Sep 2026 00:05:38 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 function must not take an FLR: it resets the coherent CXL.mem state and corrupts the HDM decoder. The PCI core already reflects this, ordering
cxl_resetahead of flr in pci_reset_fn_methods[], so a function reset of a CXL device runs the DVSEC reset sequence rather than FLR.Route the vfio function-reset points (
VFIO_DEVICE_RESETand the virtualized PCIe/AF FLR writes) through a CXL reset op that runscxl_reset_dvsec_sequence(). The sequence resets the function, always clearing device memory, and restores the HDM decoder and PCI config state, so it is a complete replacement forpci_try_reset_function()on a CXL device. The op runs undermemory_lockand not the PCI device lock, socxl_reset_dvsec_sequence()can take the device lock itself.Clear
hdm_validfor the duration of the reset so a fault cannot insert a PFN into a decoder that is being torn down, and restore it once the sequence has put the decoder back.Assisted-by LLMSigned-off-by Manish Honap <[email protected]>--- drivers/vfio/pci/cxl/vfio_cxl_core.c | 41 +++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 49 +++++++++++++++--- drivers/vfio/pci/vfio_pci_core.c | 77 +++++++++++++++++++++++----- drivers/vfio/pci/vfio_pci_priv.h | 1 + include/linux/vfio_pci_core.h | 4 ++ 5 files changed, 152 insertions(+), 20 deletions(-)drivers/vfio/pci/cxl/vfio_cxl_core.c +0
-0@@ -632,6 +632,45 @@ static void vfio_cxl_reset_done(struct
vfio_pci_core_device*vdev)Quoted 13 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0cxl->hdm_valid = false; } +/* +* Run the CXL DVSEC reset sequence in place of a PCI function reset. +A CXL +* Type-2 function must not take an FLR (it would corrupt CXL.mem), +so the vfio +* reset points route here. The sequence resets the function, always +clearing +* device memory, and restores the HDM decoder. The caller holds +memory_lock, +* and this path does not hold the PCI device lock, so drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_reset_done(struct · line 654
Quoted 10 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +10
-0+cxl_reset_dvsec_sequence() +* can take it. +*/ +static int vfio_cxl_reset (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; +int ret; + +lockdep_assert_held_write (&vdev->memory_lock); + +/* Host CPU access to the HDM range is unsafe until the decoder is back. */
drivers/vfio/pci/cxl/vfio_cxl_core.c static void vfio_cxl_reset_done(struct vfio_pci_core_device *vdev) · line 657
Quoted 7 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +7
-0+ cxl->hdm_valid = false; + + ret =cxl_reset_dvsec_sequence (vdev->pdev); +if (!ret) + cxl->hdm_valid = true; + +return ret;Success oriented flow:
if (ret) return ret; cxl->hdm_valid = true; return 0;
Quoted 3 lines
But I don't see how the case where
hdm_validremains false is really a valid place for the user to land. We don't really have a "your device is now borked, give up" signal to the user.
Okay, I will use the success-oriented flow.
hdm_valid stays false only on a hardware failure: the device reports Reset Error in Status2 (-EIO), does not finish within the timeout its DVSEC advertises (-ETIMEDOUT), or the decoder restore fails. The reset ioctl will return that error.
For the cxl_reset code, it leaves memory decode and
bus mastering disabled when the restore fails, as the host reset path does. Due to this the HDM window stays closed, with faults returning SIGBUS and its dma-buf revoked. I will not add a new signal for this case of failure.
Quoted 26 lines
Quoted 26 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +18
-0+} + +/* +* The HDM dma-buf may be armed only while the decoder is valid. +After a failed +* reset hdm_valid is clear, so the generic memory-enable re-arm must +skip the +* dma-buf rather than map DMA onto an unrestored decoder. +*/ +static bool vfio_cxl_hdm_active (struct vfio_pci_core_device *vdev) { +struct vfio_cxl_state *cxl = vdev->cxl; + +lockdep_assert_held_write (&vdev->memory_lock); + +return cxl->hdm_valid; +} +static const struct vfio_cxl_ops vfio_cxl_ops = { .init = vfio_cxl_init_device, .release = vfio_cxl_release_device,@@ -639,6 +678,8 @@ static const struct vfio_cxl_ops vfio_cxl_ops = { .close_device = vfio_cxl_close_device, .reset_prepare = vfio_cxl_reset_prepare, .reset_done = vfio_cxl_reset_done, + .reset = vfio_cxl_reset,
Quoted 9 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c static const struct vfio_cxl_ops vfio_cxl_ops = { · line 684
Quoted 3 lines
drivers/vfio/pci/cxl/vfio_cxl_core.c +1
-0+ .hdm_active = vfio_cxl_hdm_active, .owner = THIS_MODULE, };It's
vfio_cxl_ops, so it's unique to CXL, but still leaking HDM to vfio-pci-core as something it needs to care about is pretty ugly.
It should be something generic, but also this should be wrapped in some abstraction in vfio-pci-core.
Agreed. I will drop this in v6.
Quoted 21 lines
drivers/vfio/pci/vfio_pci_config.c static int vfio_basic_config_write(struct
Quoted 7 lines
drivers/vfio/pci/vfio_pci_config.c +0
-0@@ -630,7 +630,14 @@ static int vfio_basic_config_write(struct
vfio_pci_core_device*vdev, int pos,drivers/vfio/pci/vfio_pci_config.c static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos, · line 640
Quoted 12 lines
drivers/vfio/pci/vfio_pci_config.c +8
-1*virt_cmd &=cpu_to_le16 (~mask); *virt_cmd |=cpu_to_le16 (new_cmd & mask); -if (__vfio_pci_memory_enabled (vdev)) +/* +* Re-arm the dma-bufs on memory-enable, but keep a CXL device's +* HDM dma-buf revoked while the decoder is unrestored (a failed +* reset leaves hdm_valid clear); re-arming would map DMA onto a +* decoder the fault path still gates. Plain vfio-pci is unchanged. +*/ +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || vdev->cxl_ops->hdm_active (vdev)))
Quoted 4 lines
HDM space and BAR MMIO space are governed by different things, how can we combine them here to say that dmabufs are invalid until both are active? That's not how the hardware works. Does the HDM need a separate address space of dmabufs to toggle independently?
Okay. In v6 each vfio dma-buf records the region it was exported from. vfio_pci_dma_buf_move() keeps moving the BAR dma-bufs on memory enable changes, as upstream, and a new export revokes or re-arms the dma-bufs of one device-specific region. vfio-cxl calls it around reset, bus reset and low power.
I will revert the memory enable paths in vfio_pci_config.c back to their upstream forms.
Quoted 29 lines
drivers/vfio/pci/vfio_pci_config.c static void vfio_lock_and_set_power_state(struct
Quoted 5 lines
drivers/vfio/pci/vfio_pci_config.c +0
-0vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -720,7 +727,8 @@ static void vfio_lock_and_set_power_state(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_config.c static int vfio_exp_config_write(struct
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +2
-1}vfio_pci_set_power_state (vdev, state); -if (__vfio_pci_memory_enabled (vdev)) +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || vdev->cxl_ops->hdm_active (vdev)))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -910,8 +918,14 @@ static int vfio_exp_config_write(struct
vfio_pci_core_device*vdev, int pos,drivers/vfio/pci/vfio_pci_config.c static int vfio_exp_config_write(struct vfio_pci_core_device *vdev, int pos, · line 926
Quoted 11 lines
drivers/vfio/pci/vfio_pci_config.c +6
-2if (!ret && (cap &PCI_EXP_DEVCAP_FLR )) {vfio_pci_zap_and_down_write_memory_lock (vdev);vfio_pci_dma_buf_move (vdev, true); -pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Keep the HDM dma-buf revoked if a CXL reset +* failed; re-arming would map DMA onto an +* unrestored decoder. Mirrors the reset ioctl. +*/
Quoted 1 line
We don't have granularity of "the HDM dma-buf".
Okay; the per-dma-buf region index above will add this part.
Quoted 41 lines
drivers/vfio/pci/vfio_pci_config.c static int vfio_af_config_write(struct
Quoted 7 lines
drivers/vfio/pci/vfio_pci_config.c +2
-0+if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -995,8 +1009,14 @@ static int vfio_af_config_write(struct
vfio_pci_core_device*vdev, int pos,drivers/vfio/pci/vfio_pci_config.c static int vfio_af_config_write(struct vfio_pci_core_device *vdev, int pos, · line 1017
Quoted 11 lines
drivers/vfio/pci/vfio_pci_config.c +6
-2if (!ret && (cap &PCI_AF_CAP_FLR ) && (cap &PCI_AF_CAP_TP )) {vfio_pci_zap_and_down_write_memory_lock (vdev);vfio_pci_dma_buf_move (vdev, true); -pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Keep the HDM dma-buf revoked if a CXL reset +* failed; re-arming would map DMA onto an +* unrestored decoder. Mirrors the reset ioctl. +*/ Same.
drivers/vfio/pci/vfio_pci_config.c static int vfio_cxl_dvsec_write(struct
Quoted 6 lines
drivers/vfio/pci/vfio_pci_config.c +2
-0+if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);up_write (&vdev->memory_lock); }@@ -1781,9 +1801,22 @@ static int vfio_cxl_dvsec_write(struct
vfio_pci_core_device*vdev, int pos,Quoted 12 lines
drivers/vfio/pci/vfio_pci_config.c +6
-2status2 |=PCI_DVSEC_CXL_CACHE_INV ; }if (ctrl2 &PCI_DVSEC_CXL_INIT_CXL_RST ) { +int ret =0 ; + ctrl2 &= ~PCI_DVSEC_CXL_INIT_CXL_RST ; - status2 &= ~PCI_DVSEC_CXL_RST_ERR ; - status2 |=PCI_DVSEC_CXL_RST_DONE ; + +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +vfio_pci_zap_and_down_write_memory_lock (vdev); +vfio_pci_dma_buf_move (vdev, true);
Quoted 9 lines
drivers/vfio/pci/vfio_pci_config.c static int vfio_cxl_dvsec_write(struct vfio_pci_core_device *vdev, int pos, · line 1816
Quoted 6 lines
drivers/vfio/pci/vfio_pci_config.c +6
-0+ ret =vfio_pci_reset_function (vdev); +if (__vfio_pci_memory_enabled (vdev) && + (!vdev->cxl_ops || !ret)) +vfio_pci_dma_buf_move (vdev, false); +up_write (&vdev->memory_lock); + }Isn't this branch deterministic? IIRC, we only install this handler when vdev-
Quoted 1 line
cxl_opsand that ops always registers a reset function.
Yes. v6 will make the .reset mandatory: registration refuses ops without it, and I can remove the checks.
Quoted 28 lines
drivers/vfio/pci/vfio_pci_config.c
Quoted 3 lines
drivers/vfio/pci/vfio_pci_config.c +2
-0+ + status2 &= ~(PCI_DVSEC_CXL_RST_DONE |
PCI_DVSEC_CXL_RST_ERR);drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct
Quoted 11 lines
drivers/vfio/pci/vfio_pci_config.c +2
-0+ status2 |= ret ?PCI_DVSEC_CXL_RST_ERR : +PCI_DVSEC_CXL_RST_DONE ; } *pctrl2 =cpu_to_le16 (ctrl2);drivers/vfio/pci/vfio_pci_core.c +0
-0@@ -643,8 +643,27 @@ int vfio_pci_core_enable(struct
vfio_pci_core_device*vdev)drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 651
Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +6
-2goto out_power;/* If reset fails because of the device lock, fail this path entirely */ - ret =pci_try_reset_function (pdev); -if (ret == -EAGAIN ) +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +/* +* VM power-on resets a CXL Type-2 device through its DVSEC +* sequence. vconfig is not built yet here, so take memory_lock +* and call the op directly rather than the wrapper. +*/
Quoted 2 lines
B does not follow from A: what's vconfig got to do with taking
memory_lockhere?
This is a mistake in the comment added here. I will update this in v6.
Quoted 17 lines
Quoted 6 lines
drivers/vfio/pci/vfio_pci_core.c +5
-0+down_write (&vdev->memory_lock); + ret = vdev->cxl_ops->reset (vdev); +up_write (&vdev->memory_lock); + }else { + ret =pci_try_reset_function (pdev);drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) · line 666
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ } + +/* +* -EAGAIN means the reset could not run. For a CXL device any reset +* error must also fail the open: a failed DVSEC reset can leave the HDM +* decoder cleared or unrestored, and continuing would expose the HDM +* region for host access through a decoder in an unknown state. +*/ +if (ret == -EAGAIN || + (vdev->cxl_ops && vdev->cxl_ops->reset && ret))
Quoted 1 line
This suggests to me that our abstraction is lacking.
With the HDM window closed after any failed reset, the enable path will not need a CXL case. It fails the open only on -EAGAIN, as upstream, and a device whose reset failed at open keeps its HDM window closed until a reset succeeds.
Quoted 22 lines
drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +0
-0goto out_disable_device; vdev->reset_works = !ret;@@ -845,16 +864,30 @@ void vfio_pci_core_disable(struct
vfio_pci_core_device*vdev)Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +0
-2* overwrite the previously restored configuration information. */ if (vdev->reset_works) { - bridge =pci_upstream_bridge (pdev); -if (bridge && !pci_dev_trylock (bridge))drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) · line 873
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +7
-3- goto out_restore_state; -if (pci_dev_trylock (pdev)) { -if (!__pci_reset_function_locked (pdev)) +if (vdev->cxl_ops && vdev->cxl_ops->reset) { +/* +* VM power-off resets a CXL Type-2 device through its +* DVSEC sequence. The sequence takes its own device lock, +* so run it outside the lock below. +* vconfig is already freed here, so call the op directly +* under memory_lock rather than the wrapper.
Quoted 5 lines
Again, the comment doesn't actually justify the behavior. In the previous, vconfig is not setup, so take the lock, here vconfig is already freed, so take the lock... meaningless.
Also, are we dropping the try-lock semantics? How's that justified?
Sorry for the churn; No, I am not dropping the try-lock semantics. cxl_reset_dvsec_sequence() takes the device lock with pci_dev_trylock(); it returned -EBUSY whereas pci_try_reset_function() returns -EAGAIN.
In v6, I will return -EAGAIN so callers see one errno for contention.
Quoted 19 lines
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +5
-1+*/ +down_write (&vdev->memory_lock); +if (!vdev->cxl_ops->reset (vdev)) vdev->needs_reset = false; -pci_dev_unlock (pdev); +up_write (&vdev->memory_lock); + }else {drivers/vfio/pci/vfio_pci_core.c void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) · line 889
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+ bridge =pci_upstream_bridge (pdev); +if (bridge && !pci_dev_trylock (bridge)) + goto out_restore_state; +if (pci_dev_trylock (pdev)) { +if (!__pci_reset_function_locked (pdev)) + vdev->needs_reset = false; +pci_dev_unlock (pdev); + } +if (bridge) +pci_dev_unlock (bridge);
Quoted 1 line
Abstraction leaves a lot to be desired here.
I will refactor this part for correct upstream shape.
Quoted 30 lines
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_set_irqs(struct
Quoted 8 lines
drivers/vfio/pci/vfio_pci_core.c +0
-2} -if (bridge) -pci_dev_unlock (bridge); } out_restore_state:@@ -1592,6 +1625,20 @@ static int vfio_pci_ioctl_set_irqs(struct
vfio_pci_core_device*vdev,Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +4
-0return ret; } +/* +* Reset the function. A CXL device runs the CXL DVSEC reset sequence +in place +* of a PCI function reset: it replaces FLR (which would corrupt drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev, · line 1639
Quoted 10 lines
drivers/vfio/pci/vfio_pci_core.c +10
-0+CXL.mem), +* always clears device memory, and restores the HDM decoder. Callers +hold +* memory_lock for write. +*/ +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev) { +if (!vdev->cxl_ops || !vdev->cxl_ops->reset) +return pci_try_reset_function (vdev->pdev); + +return vdev->cxl_ops->reset (vdev);This is all inverted logic and I don't understand why we're trying to leave the reset op optional, make it mandatory:
Quoted 7 lines
if (vdev->cxl_ops) return vdev->cxl_ops->reset(vdev); return pci_try_reset_function(vdev->pdev);But we're again losing the try semantics on the cxl path(?) and naming of the wrapper drops the try semantics as well.
Agreed. I will rename this to vfio_pci_try_reset_function(), and CXL path will keep the trylock.
Quoted 19 lines
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c +2
-0+} +static int vfio_pci_ioctl_reset (struct vfio_pci_core_device *vdev, void __user *arg) { @@ -1614,8 +1661,14@@ static int vfio_pci_ioctl_reset(struct
vfio_pci_core_device*vdev,vfio_pci_set_power_state(vdev, PCI_D0); vfio_pci_dma_buf_move(vdev, true);drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev,
Quoted 5 lines
drivers/vfio/pci/vfio_pci_core.c +3
-2- ret =pci_try_reset_function (vdev->pdev); -if (__vfio_pci_memory_enabled (vdev)) + ret =vfio_pci_reset_function (vdev); +/* +* Re-arm the dma-bufs on success. A CXL device whose reset failed leaves
Quoted 1 line
+ * the HDM decoder unrestored and
hdm_validclear, so re-arming itsHDM
Quoted 1 line
+ * dma-buf would map device DMA onto a decoder the fault path still
gates;
Quoted 9 lines
drivers/vfio/pci/vfio_pci_core.c static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev, · line 1672
Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +3
-0+* keep it revoked until a reset succeeds. Plain vfio-pci is unchanged. +*/ +if (__vfio_pci_memory_enabled (vdev) && (!vdev->cxl_ops || !ret))vfio_pci_dma_buf_move (vdev, false);I don't think we're doing dmabufs correctly for CXL, I don't see how they're the same address space, or TBH, how we can have a reset fail so catastrophically. Thanks,
In v6 I will separate them as described above.
A failed reset will leave the HDM window closed until a later reset succeeds.
Quoted 35 lines
Alex
drivers/vfio/pci/vfio_pci_priv.h int vfio_pci_set_power_state(struct
Quoted 8 lines
up_write(&vdev->memory_lock);drivers/vfio/pci/vfio_pci_priv.h +0
-0@@ -78,6 +78,7 @@ int vfio_pci_set_power_state(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_priv.h int vfio_pci_set_power_state(struct · line 84
Quoted 7 lines
drivers/vfio/pci/vfio_pci_priv.h +1
-0pci_power_t state);void vfio_pci_zap_and_down_write_memory_lock (struct vfio_pci_core_device *vdev); +int vfio_pci_reset_function (struct vfio_pci_core_device *vdev);u16 vfio_pci_memory_lock_and_enable (struct vfio_pci_core_device *vdev); void vfio_pci_memory_unlock_and_restore(struct
vfio_pci_core_device*vdev,Quoted 16 lines
u16 cmd); diff --git
a/include/linux/vfio_pci_core.hb/include/linux/vfio_pci_core.hindex39a28cc6ae8c..231679dead45100644include/linux/vfio_pci_core.h +4
-0@@ -74,6 +74,10 @@ struct vfio_cxl_ops {void (*close_device)(struct vfio_pci_core_device *vdev);void (*reset_prepare)(struct vfio_pci_core_device *vdev);void (*reset_done)(struct vfio_pci_core_device *vdev); +/* Run the CXL reset (always clears CXL.mem) in place of FLR */ +int (*reset)(struct vfio_pci_core_device *vdev); +/* True while the HDM range is valid and its dma-buf may be armed */ +bool (*hdm_active)(struct vfio_pci_core_device *vdev);/* Pinned per bound CXL device so vfio-cxl cannot unload under usage */ struct module *owner; };
RE: [PATCH v5 11/27] vfio/pci: Virtualize the CXL DVSEC in vfio_pci_config.c
In reply to Alex WilliamsonQuoted 56 lines
-----Original Message-----
From: Alex Williamson <[email protected]>
Sent: Tuesday, September 22, 2026 7:45 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 11/27] vfio/pci: Virtualize the CXL DVSEC invfio_pci_config.cExternal email: Use caution opening links or attachments
Quoted 4 lines · On Thu, 17 Sep 2026 00:05:24 +0530 <[email protected]> wrote:
drivers/vfio/pci/vfio_pci_config.c +4
-0+/* Virtualize the CXL DVSEC so a guest cannot reprogram the device +through it. */ static int vfio_cxl_dvsec_init (struct +vfio_pci_core_device *vdev) { +struct pci_dev *pdev = vdev->pdev;drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = {
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+u32 dword; +u16 dvsec; +int len, ret; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return 0 ; + + ret = pci_read_config_dword(pdev, dvsec + PCI_DVSEC_HEADER1,&dword);
Quoted 4 lines
drivers/vfio/pci/vfio_pci_config.c +4
-0+if (ret) +return pcibios_err_to_errno (ret); + len =PCI_DVSEC_HEADER1_LEN (dword); +drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = {
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+/* +* The virtualization writes fixed DVSEC offsets up to Status2 (the reset +* doorbell stamps it). A device that reports a shorter DVSEC is not a +* usable Type-2 function; leave it as plain vfio-pci rather than index the +* device-length-sized perm allocation past its end. +*/ +if (len <PCI_DVSEC_CXL_STATUS2 +2 ) +return 0 ; + + vdev->cxl_perm = kmalloc_obj(struct perm_bits,
GFP_KERNEL_ACCOUNT);
Quoted 14 lines
drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = { · line 1858
Quoted 12 lines
drivers/vfio/pci/vfio_pci_config.c +12
-0+if (!vdev->cxl_perm) +return -ENOMEM ; + + ret =init_cxl_dvsec_perm (vdev->cxl_perm, len); +if (ret) { +kfree (vdev->cxl_perm); + vdev->cxl_perm =NULL ; +return ret; + } + + vdev->cxl_dvsec = dvsec; + vdev->cxl_dvsec_len = len;
cxl_dvsec_lenis never used here or elsewhere in the series. Thanks,
Yes, I will remove this in v6.
Quoted 7 lines
Alex
Quoted 4 lines
drivers/vfio/pci/vfio_pci_config.c +4
-0+ +return 0 ; +} +
RE: [PATCH v5 11/27] vfio/pci: Virtualize the CXL DVSEC in vfio_pci_config.c
In reply to Jonathan CameronQuoted 82 lines
-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Saturday, September 26, 2026 3:46 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 11/27] vfio/pci: Virtualize the CXL DVSEC invfio_pci_config.cExternal email: Use caution opening links or attachments
Quoted 24 lines · On Thu, 17 Sep 2026 00:05:24 +0530 <[email protected]> wrote:
From: Manish Honap <[email protected]>
A CXL Type-2 device is reprogrammable through its CXL DVSEC: a guest could set Config Lock, toggle CXL.cache and CXL.mem enable, or rewrite the HDM range registers that govern host memory decode. Virtualize the DVSEC so the guest sees a shadow it cannot use to reprogram the hardware.
Build a per-device
cxl_permpermission map, modeled onmsi_perm, when the device is bound through the CXL provider (e.g. vfio-cxl). The whole CXL DVSEC is served from the vconfig shadow; only Control and Control2 are guest programmable, while Capability, Status, Lock and the Range registers keep their firmware snapshot. A vendor DVSEC on the same device is
unaffected: the map is selected only for the CXL DVSEC offset.Control2 carries the CXL reset and cache write-back-invalidate initiate bits as self-clearing doorbells. vfio never forwards them to hardware, so a custom writefn synthesizes their completion in the
shadow: the initiate bit self-clears and the matching Status2 bit (Cache Invalid or Reset Done) is set, so a guest following the spec reset sequence (INIT_CACHE_WBI, poll Cache Invalid,INIT_CXL_RST, poll Reset Done) progresses instead of timing out. The host performs the real cache write-back and reset at the vfio reset points.Quoted 2 lines
Assisted-by LLMSigned-off-by Manish Honap <[email protected]>A few minor comments inline.
J
Quoted 2 lines
--- drivers/vfio/pci/vfio_pci_config.c | 132++++++++++++++++++++++++++++-
Quoted 10 lines
include/linux/vfio_pci_core.h | 3 + 2 files changed, 133 insertions(+), 2 deletions(-)drivers/vfio/pci/vfio_pci_config.c +1
-0+drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = { · line 1805
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+static int init_cxl_dvsec_perm (struct perm_bits *perm,int len) { +int i; + +if (alloc_perm_bits (perm, len)) +return -ENOMEM ; + + perm->writefn = vfio_cxl_dvsec_write; + +/* Serve the whole CXL DVSEC from the shadow. */ +for (i =0 ; i < len; i++)for (int i = 0;
Quoted 2 lines
Is mostly acceptable in the kernel these days and keeps the scope tightly defined.
Okay, I will add this.
Quoted 40 lines
Quoted 19 lines
drivers/vfio/pci/vfio_pci_config.c +18
-0+p_setb (perm, i, (u8 )ALL_VIRT ,NO_WRITE ); + +/* +* Control and Control2 are guest programmable; Capability, Status, +* Lock and the Range registers keep their firmware snapshot, so the +* guest cannot set Config Lock or rewrite the capability and ranges. +*/ +p_setw (perm,PCI_DVSEC_CXL_CTRL , (u16 )ALL_VIRT , (u16 )ALL_WRITE ); +p_setw (perm,PCI_DVSEC_CXL_CTRL2 , (u16 )ALL_VIRT , + (u16 )ALL_WRITE ); + +return 0 ; +} + +/* Virtualize the CXL DVSEC so a guest cannot reprogram the device +through it. */ static int vfio_cxl_dvsec_init (struct +vfio_pci_core_device *vdev) { +struct pci_dev *pdev = vdev->pdev;drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = {
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+u32 dword; +u16 dvsec; +int len, ret; + + dvsec =pci_find_dvsec_capability (pdev,PCI_VENDOR_ID_CXL , +PCI_DVSEC_CXL_DEVICE ); +if (!dvsec) +return 0 ; + + ret = pci_read_config_dword(pdev, dvsec + PCI_DVSEC_HEADER1,&dword);
Quoted 10 lines
drivers/vfio/pci/vfio_pci_config.c +10
-0+if (ret) +return pcibios_err_to_errno (ret); + len =PCI_DVSEC_HEADER1_LEN (dword); + +/* +* The virtualization writes fixed DVSEC offsets up to Status2 (the reset +* doorbell stamps it). A device that reports a shorter DVSEC is not a +* usable Type-2 function; leave it as plain vfio-pci rather than index the +* device-length-sized perm allocation past its end. +*/
Quoted 8 lines
drivers/vfio/pci/vfio_pci_config.c static const struct pci_device_id known_bogus_vf_intx_pin[] = {
Quoted 5 lines
drivers/vfio/pci/vfio_pci_config.c +5
-0+if (len <PCI_DVSEC_CXL_STATUS2 +2 ) +return 0 ; + + vdev->cxl_perm = kmalloc_obj(struct perm_bits, +GFP_KERNEL_ACCOUNT );I'd use
*vdev->cxl_perminstead of structperm_bitsjust because that saves anyone checking types.
Okay, I will update this in v6.
Quoted 43 lines
drivers/vfio/pci/vfio_pci_config.c void vfio_config_free(struct
Quoted 33 lines
drivers/vfio/pci/vfio_pci_config.c +16
-0+if (!vdev->cxl_perm) +return -ENOMEM ; + + ret =init_cxl_dvsec_perm (vdev->cxl_perm, len); +if (ret) { +kfree (vdev->cxl_perm); + vdev->cxl_perm =NULL ; +return ret; + } + + vdev->cxl_dvsec = dvsec; + vdev->cxl_dvsec_len = len; + +return 0 ; +} +int vfio_config_init (struct vfio_pci_core_device *vdev) { struct pci_dev *pdev = vdev->pdev; @@ -1842,6 +1955,12 @@ int
vfio_config_init(struct vfio_pci_core_device *vdev)if (ret) goto out;drivers/vfio/pci/vfio_pci_config.c +6
-0+if (vdev->cxl_ops) { + ret =vfio_cxl_dvsec_init (vdev); +if (ret) + goto out; + } +return 0 ; out:@@ -1863,6 +1982,12 @@ void vfio_config_free(struct
vfio_pci_core_device*vdev)drivers/vfio/pci/vfio_pci_config.c void vfio_config_free(struct vfio_pci_core_device *vdev) · line 1989
Quoted 8 lines
drivers/vfio/pci/vfio_pci_config.c +5
-0kfree (vdev->msi_perm); vdev->msi_perm =NULL ; } +if (vdev->cxl_perm) { +free_perm_bits (vdev->cxl_perm); +kfree (vdev->cxl_perm); + vdev->cxl_perm =NULL ; + vdev->cxl_dvsec =0 ;
Quoted 2 lines
I'd define a
vfio_cxl_dvsec_exit()or_unint()for this so it is clear it pairs withvfio_cxl_dvsec_init()above.
Ack. Alex pointed out that cxl_dvsec_len is unused, so the exit helper clears only cxl_perm and cxl_dvsec.
Quoted 16 lines
drivers/vfio/pci/vfio_pci_config.c ssize_t vfio_pci_config_rw_single(struct
Quoted 7 lines
drivers/vfio/pci/vfio_pci_config.c +1
-0+ } }/* @@ -1926,12 +2051,15 @@ ssize_t vfio_pci_config_rw_single(struct
vfio_pci_core_device*vdev,drivers/vfio/pci/vfio_pci_config.c ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
Quoted 8 lines
drivers/vfio/pci/vfio_pci_config.c +3
-0* of the extended capability list. Use default, ro * access, which will virtualize the id and next values. */ + cap_start =vfio_find_cap_start (vdev, *ppos); +if (cap_id >PCI_EXT_CAP_ID_MAX ) perm = &direct_ro_perms; + else if (cap_id == PCI_EXT_CAP_ID_DVSEC && vdev->cxl_perm
Quoted 7 lines
&&
drivers/vfio/pci/vfio_pci_config.c ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev, · line 2059
Quoted 1 line
drivers/vfio/pci/vfio_pci_config.c +1
-0+ cap_start == vdev->cxl_dvsec)If it is only used here (I haven't read on in series)
vfio_find_cap_start(vdev, *ppos) == vdev->cxl_dvsec) seemsfine to me. It's only just over 80 chars and hopeful this bit of the kernel is flexible on that!
cap_start is also used after the branch for the offset into the capability. I will keep the variable in v6.
Quoted 9 lines
Quoted 9 lines
drivers/vfio/pci/vfio_pci_config.c +1
-2+ perm = vdev->cxl_perm; else perm = &ecap_perms[cap_id]; - - cap_start =vfio_find_cap_start (vdev, *ppos); }else {WARN_ON (cap_id >PCI_CAP_ID_MAX );
RE: [PATCH v5 26/27] Documentation: vfio-pci: Document CXL Type-2 device passthrough
In reply to Jonathan CameronQuoted 44 lines
-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Thursday, September 24, 2026 6:24 AM
To: Manish Honap <[email protected]>
Cc: Gregory Price <[email protected]>; [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; alejandro.lucero- [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v5 26/27] Documentation: vfio-pci: Document CXL Type- 2 device passthroughExternal email: Use caution opening links or attachments
Quoted 5 lines · On Mon, 21 Sep 2026 09:43:34 +0000 Manish Honap <[email protected]> wrote:
Hello Gregory,
Thank you for the careful read of the documentation. I will rework the sections you flagged as below:
Hi Manish,
It is much more helpful to reply inline with the relevant sections so we can see the context wrt to what Gregory was asking. That saves others reading the discussion from having to carefully build up a global model of what was said to understand what the replies are about. Also I'll hazzard a guess that Gregory may have read enough code since he sent the review that he has already forgotten some of his own comments! (maybe that's just
me!) I appreciate that doesn't always work if you are going to address the comment via more radical restructuring. Or in short, please don't top post!
Quoted 4 lines
Thanks,
Jonathan
Understood, sorry for the churn. I have answered Gregory's points inline in a reply to his original mail. I will keep replies inline from now.
Quoted 318 lines
Quoted 4 lines
- Address model
I will update the wording to mention that VMM does know the GPA: it
builds
Quoted 1 line
the guest's CFMWS window and so chooses the guest-physical range the
device
Quoted 4 lines
can inhabit. What the host owns is the HPA and the placement within it, not knowledge of the GPA.
I will add the two models you described, fixed placement for an accelerator that needs exact physical placement versus no fixed placement for pooledor
Quoted 1 line
compressed memory, with a small HPA/GPA example, and note that the
current
Quoted 6 lines
series under discussion implements the fixed-offset case.
- Virtual decoders
I will rename the The "Guest decoder and commit" section to "Virtual decoders" and add a short flow showing a guest vdecoder write being
absorbed
Quoted 1 line
and a live read returning COMMITTED from the host-locked physical
decoder.
Quoted 9 lines
- "The kernel virtualizes ..."
I will update it to say vfio-pci-core (its config-space permission hooks).
- guest IOAS, stage-2 mapping, and struct-page-less coherent memory
I will define these terms where first used and include that the range is handed to the device whole and never onlined as system RAM. I will also
add
Quoted 1 line
details why a stale stage-2 mapping must not outlive the HDM window, and
how
Quoted 59 lines
the VMM rebuilds it.
- Reset
I will reword this section. FLRs are virtualized so a guest reset can't inflict physical CXL.mem/decoder effects on the host rather than loose wording "must not take an FLR"
I can share the revised text ahead of the v6 posting if that is easier to review.
Thanks,
ManishQuoted 45 lines
-----Original Message-----
From: Gregory Price <[email protected]>
Sent: Thursday, September 17, 2026 1:03 AM
To: Manish Honap <[email protected]>
Cc: [email protected]; [email protected]; Ankit Agrawal <[email protected]>; [email protected]; [email protected]; [email protected]; Srirangan Madhavan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yishai Hadas <[email protected]>; Shameer Kolothum Thodi <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Neo Jia <[email protected]>; Krishnakant Jaju <[email protected]>; Vikram Sethi <[email protected]>; Zhi Wang <[email protected]>; linux- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v5 26/27] Documentation: vfio-pci: Document CXL Type-
2 device passthroughExternal email: Use caution opening links or attachments
Quoted 2 lines · On Thu, Sep 17, 2026 at 12:05:39AM +0530, [email protected] wrote:
From: Manish Honap <[email protected]>
1) Thank you so much for writing documentation, i truly appreciate this.
2) I apologize in advance for my terseness, I know writing is hard, please do not interpret this as disliking your writing or series.
Review
Quoted 9 lines
(quoted diff) +9
-0+Address model +============= + +The HDM memory is a coherent host physical range (HPA). The host +kernel resolves that range before the guest sees the device, and +owns it for the bind lifetime. The guest only chooses where the +memory appears in its own physical address space (GPA), by +programming a virtual endpoint HDM decoder. The guest never +reprograms the physicaldecoder.
Quoted 5 lines
Quoted 5 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ +The kernel holds the HPA and does not see the GPA. The guest +programs a GPA and does not see the HPA. The VMM holds the device +fd, reads the committed base from the decoder-register region +described below, and maps the HPA-backed HDM region at the GPA theguest committed.
Quoted 53 lines
Quoted 53 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+The base the guest reads back is the GPA, not the HPA. +I think this must be slightly inaccurate / imprecise wording.
The host *must* provide some form of physical memory window to the guest at initialization time, otherwise the guest has no way to know
- at boot time - that there's even a window of memory it can use.That's what the CFMWS is. This is initialized by the hypervisor - which is controlled by the host.
So the host (at least the VMM) must know, for the region the entire device
*could* inhabit, what that GPA is - because it's the one that makes the CFMWS for the guest.If this is not the case, then something is missing from this documentation to explain why.
If you're actually trying to say is that the GPA's programmed into the virtual decoders are largely symbolic - this at best feels a bit inaccurate and simply an implementation detail.
The host's virtio device could enforce ....:
Host Range:
CFMWS HPA - [0x10000, 0x20000] | |Guest Range: | |
CFMWS GPA - [0x50000,0x60000]In that case, you'd get the following translation...
vdecoder0.0 - [0x58000,0x60000]
CFMWS GPA - [0x58000,0x60000]
CFMWS HPA - [0x18000,0x20000]Or the virtio device could not enforce that and let the host page-fault just hand it a random page from the actual CXL device.
vdecoder0.0 - [0x58000, 0x60000] CFMWS GPA - [0x58000, 0x60000] | No discrete host mappingThe former makes sense if the device (accelerator) requires exact physical placement to do its accelerator nonsense.
The latter makes sense if the device (accelerator) doesn't care about placement (compressed memory).
Quoted 6 lines
Quoted 6 lines
This is not saying we need support both out of the box, but we shouldn't lock ourselves into the former unless there's some reason why the latter is not reasonable.
Can you please help document what the actual expected behavior is with examples in the Address model section so it's easier to understand the
intent?
Quoted 22 lines
Quoted 22 lines
That will help quite a bit.
Review
Quoted 8 lines
(quoted diff) +8
-0+Guest decoder and commit +======================== + +The guest programs its virtual endpoint decoder through the +trapped +region: it writes a base (a GPA), a size, and then the COMMIT bit. +The host already resolved and committed the physical placement +before the guest ran,So the host does know GPA, just not exact placement.
Review
Quoted 4 lines
so a live read of the decoder always shows COMMITTED and the
(quoted diff) +3
-0+guest's commit poll completes. The physical decoder is never +rewritten; the guest's writes are absorbed. +Rather clunky, round-about way to say "The guest decoders are
virtualized". If possible, it would be nice to formalize this concept
into "Virtual Decoders" - since that's what this is.Quoted 2 lines
Quoted 2 lines
With that concept i think you can probably generate some nice diagrams that show how the guest vdecoder's interact with the host
drivers.
Quoted 65 lines
Quoted 65 lines
Review
Quoted 4 lines
(quoted diff) +3
-0+The VMM observes the commit, reads the committed base, and maps +the HDM region at that GPA. +So the host does know the GPA.
Review
Quoted 7 lines
(quoted diff) +7
-0+CXL Device DVSEC +================ + +The kernel virtualizes the CXL Device DVSEC body through the +config-space permission hooks. Reads and writes inside the DVSEC +body use a per-open shadow; a guest write stays in the shadow and +does notreach hardware.
Review
Quoted 2 lines
(quoted diff) +2
-0+Accesses outside the DVSEC body go to the device as usual. +"The kernel" - what part? vfio-pci ? the vmm ?
Documentation/driver-api/vfio-pci-cxl.rst
Quoted 6 lines
Documentation/driver-api/vfio-pci-cxl.rst new file +6
-0+DMA and iommufd +=============== + +A Type-2 accelerator issues ATS-translated DMA to addresses +inside its own HDM window, so that range must be present in the +guest IOAS that backs the nested stage-2 translation.Type-2, ATS, DMA, HDM window, guest IOAS, stage-2 translation
I think the only thing i don't know in this sentence is "guest IOAS" and it's still hurting my brain to read.
Are all accelerators expect to have this particular interaction, or just yours?
Review
Quoted 3 lines
The HDM range is struct-page-less coherent
(quoted diff) +2
-0+memory, which a userspace-VA ``IOMMU_IOAS_MAP`` cannot pin. +The hardest part about writing about virtualization is keeping a consistent mental model from section to section.
which userspace? guest? host? (i presume guest here)
struct-page-less coherent memory
e.g. the host never hotplugs this, it hands the entire region directly to the VFIO device, right?I think this would be nice to spell out somewhere.
Review
Quoted 3 lines
(quoted diff) +3
-0+The HDM memory region is therefore exportable as a dma-buf: +``VFIO_DEVICE_FEATURE_DMA_BUF`` on that region returns an fd that +iommufd maps with ``IOMMU_IOAS_MAP_FILE``, mapping the physicalrange
Review
Quoted 5 lines
(quoted diff) +5
-0+without a VA or a page pin. The dma-buf is revoked whenever the +mapping is torn down (reset, power transition, teardown), so a +stale +stage-2 mapping cannot outlive the HDM window. +For the sake of readers, I think either a little bit more information on this
"stage-2 mapping" concept is needed to make sense of what's going on here and why it mustn't outlive the HDM window.Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+Reset +===== + +A CXL Type-2 function must not take a Function Level Reset: an +FLR resets the coherent CXL.mem state and the HDM decoder. The +PCI core reflects this by preferring the CXL reset over FLR, so a +function reset of a CXL device runs the CXL DVSEC reset sequence, +which resets the function and then restores the HDM decoder and thePCI config state.
Quoted 21 lines
Quoted 21 lines
Quoted 1 line
+
I think what you're trying to say is that FLRs are never passed to the device because it can cause physical device effects that defeat the purpose of the virtualization, yes?
So we virtualize FLRs...
Review
Quoted 4 lines
(quoted diff) +4
-0+A guest requests a reset by writing Initiate CXL Reset in the DVSEC. +That write only stamps completion in the shadow. The real reset +runs at the vfio reset points (the reset ioctl and a virtualized +FLR through configspace):
As you describe here.
So it's not that an accelerator "must not take an FLR" - it's that FLRs are virtualized to prevent deleterious effects on the host / hardware.
Am I misunderstanding this?
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
(quoted diff) +8
-0+the kernel zaps the HDM mapping and revokes the dma-buf, then +runs the CXL reset, which always clears the device memory, and +restores and re-samples the decoder afterwards. A CXL port masks +Secondary Bus Reset by default, so a +``VFIO_DEVICE_PCI_HOT_RESET`` does not reach the endpoint and the +HDM state is untouched. If the port has SBR unmasked the reset +can decommit the decoder without restoring it, so the reset_done +handler gates HDM access; a ``VFIO_DEVICE_RESET`` then runs the CXLreset sequence and restores it.
Quoted 9 lines
Quoted 9 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ +The decoder register region is served by live reads of the +hardware decoder with guest writes absorbed: the decoder is +committed and locked by the host, so a guest can neither decommit +nor reprogram it, and the kernel keeps no shadow of the decoder state.This is basically what I said at the beginning - it must either be that the host provides locked auto-decoders at boot, or it must provide proper virtualization so that the decoders settings are fully
virtualized.
Quoted 19 lines
Quoted 19 lines
Seems it's the former, and that makes sense. Please correct me if i'm misunderstanding.
Review
Quoted 7 lines
After a reset the kernel restores and
(quoted diff) +6
-0+re-samples the firmware-committed decoder, so the geometry the +guest reads back is unchanged. A VMM that dropped its HDM +mapping, for example across a reset or a D3hot->D0 transition, +must rescan the decoder and rebuild its +stage-2 mapping before it resumes HDM access. +Yeah i think we need a bit more information about this stage-2 mapping rebuild to make sense of this. Maybe I'm just not read-up enough on this particular setup - is there another part of the docs you can link to that talk about this, or are you able to share some details as to what this rebuild process looks like?
~Gregory