From: Manish Honap vfio-cxl will export the HDM memory of a CXL device as a dma-buf. That memory is not inside a BAR, but each dma-buf carries a struct p2pdma_provider, and these exist only per BAR. vfio-cxl uses the one of BAR0, as nvgrace-gpu already does for its device memory. The bus_offset of a p2pdmap_provider is valid only for addresses inside its BAR. When an importer maps such a dma-buf on the PCI_P2PDMA_MAP_BUS_ADDR path, pci_p2pdma_bus_addr_map() adds the bus_offset of BAR0 to the physical address of the memory. The peer gets a bus address that does not fall into BAR decode range. Mark a dma-buf whose p2pdma_provider is not the one of its region index, and fail vfio_pci_dma_buf_map() for it with -EOPNOTSUPP. The host bridge path does not use bus_offset, but vfio-pci cannot tell which path an importer will take, since pci_p2pdma_map_type() is not exported. So refuse both. 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 dma-buf exports through vfio_pci_core_feature_dma_buf() - No vfio selftests, kmemleak or sparse were run. Reported-by: Richard Cheng Link: https://lore.kernel.org/all/aquaxVHh2O8SUCvj@MWDK4CY14F/ Signed-off-by: Manish Honap --- drivers/vfio/pci/vfio_pci_dmabuf.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c index c16f460c01d6..7d1d34029b95 100644 --- a/drivers/vfio/pci/vfio_pci_dmabuf.c +++ b/drivers/vfio/pci/vfio_pci_dmabuf.c @@ -20,6 +20,7 @@ struct vfio_pci_dma_buf { struct kref kref; struct completion comp; u8 revoked : 1; + u8 no_peer2peer : 1; }; static int vfio_pci_dma_buf_attach(struct dma_buf *dmabuf, @@ -59,6 +60,9 @@ vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment, if (priv->revoked) return ERR_PTR(-ENODEV); + if (priv->no_peer2peer) + return ERR_PTR(-EOPNOTSUPP); + ret = dma_buf_phys_vec_to_sgt(attachment, priv->provider, priv->phys_vec, priv->nr_ranges, priv->size, dir); @@ -281,6 +285,9 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags, if (ret) goto err_free_phys; + priv->no_peer2peer = priv->provider != + pcim_p2pdma_provider(vdev->pdev, get_dma_buf.region_index); + kfree(dma_ranges); dma_ranges = NULL; -- 2.53.0