[PATCH v3 5/9] vfio/pci: Provide a user-facing name for BAR mappings

From: Matt Evans

Date: Wed Jun 10 2026 - 12:23:54 EST


Since converting BAR mmap()s to using DMABUFs, we lose the original
device path in /proc/<pid>/maps, lsof, etc. Generate a debug-oriented
synthetic 'filename' based on the cdev, plus BDF, plus resource index.

This applies only to BAR mappings via the VFIO device fd, as
explicitly-exported DMABUFs are named by userspace via the
DMA_BUF_SET_NAME ioctl.

Signed-off-by: Matt Evans <matt@xxxxxxxxxx>
---
drivers/vfio/pci/vfio_pci_dmabuf.c | 27 +++++++++++++++++++++++++--
1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c b/drivers/vfio/pci/vfio_pci_dmabuf.c
index 2fd3629789bf..8f7f1b909b94 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -4,6 +4,7 @@
#include <linux/dma-buf-mapping.h>
#include <linux/pci-p2pdma.h>
#include <linux/dma-resv.h>
+#include <uapi/linux/dma-buf.h>

#include "vfio_pci_priv.h"

@@ -470,6 +471,7 @@ int vfio_pci_core_mmap_prep_dmabuf(struct vfio_pci_core_device *vdev,
{
struct vfio_pci_dma_buf *priv;
unsigned long vma_pgoff = vma->vm_pgoff & (VFIO_PCI_OFFSET_MASK >> PAGE_SHIFT);
+ char *bufname;
int ret;

priv = kzalloc_obj(*priv);
@@ -482,6 +484,20 @@ int vfio_pci_core_mmap_prep_dmabuf(struct vfio_pci_core_device *vdev,
goto err_free_priv;
}

+ bufname = kzalloc(DMA_BUF_NAME_LEN, GFP_KERNEL);
+ if (!bufname) {
+ ret = -ENOMEM;
+ goto err_free_phys;
+ }
+
+ /*
+ * Maximum size of the friendly debug name is
+ * vfio1234567890:ffff:ff:3f.7/5 = 30, which fits within
+ * DMA_BUF_NAME_LEN.
+ */
+ snprintf(bufname, DMA_BUF_NAME_LEN, "%s:%s/%x",
+ dev_name(&vdev->vdev.device), pci_name(vdev->pdev), res_index);
+
/*
* The DMABUF begins from the mmap()'s BAR offset, i.e. the
* start of the VMA corresponds to byte 0 of the DMABUF and
@@ -500,7 +516,7 @@ int vfio_pci_core_mmap_prep_dmabuf(struct vfio_pci_core_device *vdev,
priv->provider = pcim_p2pdma_provider(vdev->pdev, res_index);
if (!priv->provider) {
ret = -EINVAL;
- goto err_free_phys;
+ goto err_free_name;
}

priv->phys_vec[0].paddr = phys_start + ((u64)vma_pgoff << PAGE_SHIFT);
@@ -508,7 +524,7 @@ int vfio_pci_core_mmap_prep_dmabuf(struct vfio_pci_core_device *vdev,

ret = vfio_pci_dmabuf_export(vdev, priv, O_CLOEXEC | O_RDWR);
if (ret)
- goto err_free_phys;
+ goto err_free_name;

/*
* Ownership of the DMABUF file transfers to the VMA so that
@@ -523,8 +539,15 @@ int vfio_pci_core_mmap_prep_dmabuf(struct vfio_pci_core_device *vdev,
vma->vm_file = priv->dmabuf->file;
vma->vm_private_data = priv;

+ spin_lock(&priv->dmabuf->name_lock);
+ kfree(priv->dmabuf->name);
+ priv->dmabuf->name = bufname;
+ spin_unlock(&priv->dmabuf->name_lock);
+
return 0;

+err_free_name:
+ kfree(bufname);
err_free_phys:
kfree(priv->phys_vec);
err_free_priv:
--
2.50.1 (Apple Git-155)