[RFC v2 PATCH 01/10] vfio/pci: Set up VFIO barmap before creating a DMABUF

From: Matt Evans

Date: Thu Mar 12 2026 - 14:48:42 EST


A DMABUF exports access to BAR resources which need to be requested
before the DMABUF is handed out. Usually the resources are requested
when setting up the barmap when the VFIO device fd is mmap()ed, but
there's no guarantee that's done before a DMABUF is created.

Set up the barmap (and so request resources) in the DMABUF-creation
path.

Fixes: 5d74781ebc86c ("vfio/pci: Add dma-buf export support for MMIO regions")
Signed-off-by: Matt Evans <mattev@xxxxxxxx>
---
drivers/vfio/pci/vfio_pci_dmabuf.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index 3a803923141b..44558cc2948e 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -269,6 +269,17 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
goto err_free_priv;
}

+ /*
+ * Just like the vfio_pci_core_mmap() path, we need to ensure
+ * PCI regions have been requested before returning DMABUFs
+ * that reference them. It's possible to create a DMABUF for
+ * a BAR without the BAR having already been mmap()ed. The
+ * barmap setup requests the regions for us:
+ */
+ ret = vfio_pci_core_setup_barmap(vdev, get_dma_buf.region_index);
+ if (ret)
+ goto err_free_phys;
+
priv->vdev = vdev;
priv->nr_ranges = get_dma_buf.nr_ranges;
priv->size = length;
--
2.47.3