* [PATCH 0/2] vfio/pci: CXL DVSEC config space fixes
@ 2026-12-02 9:11 mhonap
2026-12-02 9:11 ` [PATCH 1/2] vfio/pci: Find the start of an abutting extended capability mhonap
2026-12-02 9:11 ` [PATCH 2/2] vfio/pci: Make the CXL DVSEC read-only mhonap
0 siblings, 2 replies; 5+ messages in thread
From: mhonap @ 2026-12-02 9:11 UTC (permalink / raw)
To: alex, ankita, satyanarayana.k.v.p; +Cc: kjaju, mhonap, kvm, linux-cxl
From: Manish Honap <mhonap@nvidia.com>
Since commit 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config
Capability to user visible list."), vfio-pci shows every DVSEC to the
user and passes user writes to the device. The two patches posted in
this series fix two problems this can cause on CXL devices.
Patch 1 fixes how vfio-pci finds the start of an extended capability.
pci_config_map records the capability ID, and every DVSEC has ID 0x23,
so for a DVSEC that directly follows another DVSEC the start comes out
as the start of the first one. The user then reads the Next pointer
from the device and can reach a capability that vfio hid. Patch 1 takes
the start from the capability chain in vconfig.
Patch 2 makes the CXL Device DVSEC read-only on a function that is
CXL.cache and CXL.mem capable. Today the user can set Initiate CXL
Reset, which starts a reset the host does not know about, or set
CONFIG_LOCK, which stays set after the user closes the device. vfio-pci
gives such a user no device memory, so the user has nothing to enable
in the DVSEC. Patch 2 depends on patch 1 for a CXL DVSEC that follows
another DVSEC.
Both patches come from the review of 11/31 of the vfio-cxl v6 series
[1]. Alex asked for the read-only change as a separate patch with a
Fixes: tag [2].
Changes from vfio-cxl v6 11/31:
- Patch 1 is new. v6 worked around the wrong start inside the CXL DVSEC
code only; patch 1 fixes vfio_find_cap_start() for every extended
capability (Alex, 11/31).
- Patch 2 replaces the v6 guard map with read-only permissions for the
whole DVSEC, and adds the Fixes: tag (Alex, 11/31).
[1] https://lore.kernel.org/all/20261016161839.2765592-1-mhonap@nvidia.com/
[2] https://lore.kernel.org/all/20261106151043.4f47679f@shazbot.org/
Testing:
- Series applies cleanly to v7.4-rc4 (b6a9fa34ce41)
- Tested with a passthrough guest on VR-NVL72 system with running
CUDA/UVM workloads in guest.
- Ran a host function profile of above run to confirm config space
accesses through vfio_pci_config_rw_single()
- No vfio selftests, kmemleak or sparse were run.
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Manish Honap (2):
vfio/pci: Find the start of an abutting extended capability
vfio/pci: Make the CXL DVSEC read-only
drivers/vfio/pci/vfio_pci_config.c | 44 ++++++++++++++++++++++++++++++
include/linux/vfio_pci_core.h | 1 +
2 files changed, 45 insertions(+)
--
2.53.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH 1/2] vfio/pci: Find the start of an abutting extended capability 2026-12-02 9:11 [PATCH 0/2] vfio/pci: CXL DVSEC config space fixes mhonap @ 2026-12-02 9:11 ` mhonap 2026-12-02 9:21 ` sashiko-bot 2026-12-02 9:11 ` [PATCH 2/2] vfio/pci: Make the CXL DVSEC read-only mhonap 1 sibling, 1 reply; 5+ messages in thread From: mhonap @ 2026-12-02 9:11 UTC (permalink / raw) To: alex, ankita, satyanarayana.k.v.p; +Cc: kjaju, mhonap, kvm, linux-cxl From: Manish Honap <mhonap@nvidia.com> vfio_find_cap_start() walks through pci_config_map while the bytes hold same capability ID. When two DVSEC's are back-to-back, we cannot correctly tell what is the start of second capability. Due to this the offset into the second capability resolves to the start of first capability. For above case, walk the extended vconfig capability chain for determining the correct start of the abutting capabilities. Standard capabilities keep the walk back through the map. Signed-off-by: Manish Honap <mhonap@nvidia.com> --- drivers/vfio/pci/vfio_pci_config.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index 9914f3ac69ae..dd0b899fbdfd 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -1129,6 +1129,23 @@ int __init vfio_pci_init_perm_bits(void) return ret; } +static int vfio_find_ecap_start(struct vfio_pci_core_device *vdev, int pos) +{ + struct pci_dev *pdev = vdev->pdev; + int loops = (pdev->cfg_size - PCI_CFG_SPACE_SIZE) / PCI_CAP_SIZEOF; + int epos = PCI_CFG_SPACE_SIZE, start = PCI_CFG_SPACE_SIZE; + + while (loops-- && epos >= PCI_CFG_SPACE_SIZE) { + u32 header = le32_to_cpu(*(__le32 *)&vdev->vconfig[epos]); + + if (epos <= pos && epos > start) + start = epos; + epos = PCI_EXT_CAP_NEXT(header); + } + + return start; +} + static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos) { u8 cap; @@ -1139,6 +1156,10 @@ static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos) if (cap == PCI_CAP_ID_BASIC) return 0; + /* Abutting DVSECs share one map ID */ + if (pos >= PCI_CFG_SPACE_SIZE) + return vfio_find_ecap_start(vdev, pos); + /* XXX Can we have to abutting capabilities of the same type? */ while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap) pos--; -- 2.53.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] vfio/pci: Find the start of an abutting extended capability 2026-12-02 9:11 ` [PATCH 1/2] vfio/pci: Find the start of an abutting extended capability mhonap @ 2026-12-02 9:21 ` sashiko-bot 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-12-02 9:21 UTC (permalink / raw) To: mhonap; +Cc: kvm, linux-cxl Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] vfio/pci: missing Fixes trailer for abutting extended capability start lookup -- commit e03c58b536a2d7efffec688f5ba3ace062705c91 Author: Manish Honap <mhonap@nvidia.com> > vfio/pci: Find the start of an abutting extended capability > > vfio_find_cap_start() walks through pci_config_map while the bytes hold > same capability ID. When two DVSEC's are back-to-back, we cannot > correctly tell what is the start of second capability. Due to this > the offset into the second capability resolves to the start of first > capability. > > For above case, walk the extended vconfig capability chain for > determining the correct start of the abutting capabilities. > > Standard capabilities keep the walk back through the map. > > Signed-off-by: Manish Honap <mhonap@nvidia.com> [Severity: Low] This fixes extended capability start resolution in vfio_find_cap_start() when extended capabilities with the same capability ID abut each other. Should this include: Fixes: 89e1f7d4c66d ("vfio: Add PCI device driver") -- Sashiko AI review · https://sashiko.dev/#/patchset/20261202091105.1674275-1-mhonap@nvidia.com?part=1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/2] vfio/pci: Make the CXL DVSEC read-only 2026-12-02 9:11 [PATCH 0/2] vfio/pci: CXL DVSEC config space fixes mhonap 2026-12-02 9:11 ` [PATCH 1/2] vfio/pci: Find the start of an abutting extended capability mhonap @ 2026-12-02 9:11 ` mhonap 2026-12-02 9:22 ` sashiko-bot 1 sibling, 1 reply; 5+ messages in thread From: mhonap @ 2026-12-02 9:11 UTC (permalink / raw) To: alex, ankita, satyanarayana.k.v.p; +Cc: kjaju, mhonap, kvm, linux-cxl From: Manish Honap <mhonap@nvidia.com> Since commit 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config Capability to user visible list."), vfio-pci passes user writes to every DVSEC through to the device. Before that commit DVSECs were hidden. For a function that is CXL.cache and CXL.mem capable, the user can then: - clear Cache_Enable or Mem_Enable in DVSEC Control, - set Initiate CXL Reset or Initiate Cache Writeback and Invalidation in DVSEC Control2, which the host does not see or handle, - write the DVSEC Range registers, - set Config Lock, after which the host cannot change the locked registers. This change keeps the CXL Device DVSEC visible and makes it read-only: reads return the device values and writes are dropped. The capability is matched by its start, so a CXL DVSEC that directly follows another DVSEC relies on the earlier patch to correctly calculate start of abutting capabilities. Fixes: 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config Capability to user visible list.") Signed-off-by: Manish Honap <mhonap@nvidia.com> --- drivers/vfio/pci/vfio_pci_config.c | 23 +++++++++++++++++++++++ include/linux/vfio_pci_core.h | 1 + 2 files changed, 24 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index dd0b899fbdfd..2bc0c7cbc4b7 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -1746,6 +1746,23 @@ static int vfio_ecap_init(struct vfio_pci_core_device *vdev) return 0; } +static u16 vfio_pci_cxl_dvsec(struct pci_dev *pdev) +{ + u16 dvsec, cap; + + dvsec = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL, + PCI_DVSEC_CXL_DEVICE); + if (!dvsec || + pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CAP, &cap)) + return 0; + + if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) || + !(cap & PCI_DVSEC_CXL_MEM_CAPABLE)) + return 0; + + return dvsec; +} + /* * Nag about hardware bugs, hopefully to have vendors fix them, but at least * to collect a list of dependencies for the VF INTx pin quirk below. @@ -1863,6 +1880,8 @@ int vfio_config_init(struct vfio_pci_core_device *vdev) if (ret) goto out; + vdev->cxl_dvsec = vfio_pci_cxl_dvsec(vdev->pdev); + return 0; out: @@ -1953,6 +1972,10 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev, perm = &ecap_perms[cap_id]; cap_start = vfio_find_cap_start(vdev, *ppos); + + if (cap_id == PCI_EXT_CAP_ID_DVSEC && + cap_start == vdev->cxl_dvsec) + perm = &direct_ro_perms; } else { WARN_ON(cap_id > PCI_CAP_ID_MAX); diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 2f3fa2787737..dc25f737fe93 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -109,6 +109,7 @@ struct vfio_pci_core_device { u8 *pci_config_map; u8 *vconfig; struct perm_bits *msi_perm; + u16 cxl_dvsec; spinlock_t irqlock; struct mutex igate; struct xarray ctx; -- 2.53.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] vfio/pci: Make the CXL DVSEC read-only 2026-12-02 9:11 ` [PATCH 2/2] vfio/pci: Make the CXL DVSEC read-only mhonap @ 2026-12-02 9:22 ` sashiko-bot 0 siblings, 0 replies; 5+ messages in thread From: sashiko-bot @ 2026-12-02 9:22 UTC (permalink / raw) To: mhonap; +Cc: kvm, linux-cxl Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] vfio/pci: vfio_pci_cxl_dvsec() leaves single-capability CXL DVSECs writable -- commit 19e2a95ba8c72061f8495872d7deca8225c841c5 Author: Manish Honap <mhonap@nvidia.com> vfio/pci: Make the CXL DVSEC read-only This commit locates the PCIe DVSEC for CXL Devices capability during config space initialization and switches its permissions to read-only. > diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c > index dd0b899fbdfdf..2bc0c7cbc4b7b 100644 > --- a/drivers/vfio/pci/vfio_pci_config.c > +++ b/drivers/vfio/pci/vfio_pci_config.c > @@ -1746,6 +1746,23 @@ static int vfio_ecap_init(struct vfio_pci_core_device *vdev) > return 0; > } > > +static u16 vfio_pci_cxl_dvsec(struct pci_dev *pdev) > +{ > + u16 dvsec, cap; > + > + dvsec = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL, > + PCI_DVSEC_CXL_DEVICE); > + if (!dvsec || > + pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CAP, &cap)) > + return 0; > + > + if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) || > + !(cap & PCI_DVSEC_CXL_MEM_CAPABLE)) > + return 0; [Severity: High] Does vfio_pci_cxl_dvsec() leave PCI_DVSEC_CXL_DEVICE writable on CXL functions that advertise only CXL.mem or only CXL.cache? Using || requires both capability bits to be set, so Type 3 (CXL.mem-only) and Type 1 (CXL.cache-only) functions return 0 and keep vfio_raw_config_write(), allowing userspace to modify Mem_Enable or Cache_Enable, trigger an uncoordinated CXL reset, or set Config Lock. Should this check !(cap & (PCI_DVSEC_CXL_CACHE_CAPABLE | PCI_DVSEC_CXL_MEM_CAPABLE)) instead? -- Sashiko AI review · https://sashiko.dev/#/patchset/20261202091105.1674275-1-mhonap@nvidia.com?part=2 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-02 9:22 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-02 9:11 [PATCH 0/2] vfio/pci: CXL DVSEC config space fixes mhonap 2026-12-02 9:11 ` [PATCH 1/2] vfio/pci: Find the start of an abutting extended capability mhonap 2026-12-02 9:21 ` sashiko-bot 2026-12-02 9:11 ` [PATCH 2/2] vfio/pci: Make the CXL DVSEC read-only mhonap 2026-12-02 9:22 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox