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 <[email protected]>
---
 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 f7797f58d44b..733607371082 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"
 
@@ -476,6 +477,7 @@ int vfio_pci_core_mmap_prep_dmabuf(struct 
vfio_pci_core_device *vdev,
        struct vfio_pci_dma_buf *priv;
        const unsigned int nr_ranges = 1;
        unsigned long vma_pgoff = vma->vm_pgoff & (VFIO_PCI_OFFSET_MASK >> 
PAGE_SHIFT);
+       char *bufname;
        int ret;
 
        priv = kzalloc_obj(*priv);
@@ -488,6 +490,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
@@ -506,7 +522,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);
@@ -514,7 +530,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;
 
        /*
         * The VMA gets the DMABUF file so that other users can locate
@@ -526,8 +542,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.47.3

Reply via email to