Print the PCI device syspath to a vfio device's fdinfo. This enables tools to query which device is associated with a given vfio device fd. This results in output like below: $ cat /proc/"$SOME_PID"/fdinfo/"$VFIO_FD" | grep vfio vfio-device-syspath: /sys/devices/pci0000:e0/0000:e0:01.1/0000:e1:00.0/0000:e2:05.0/0000:e8:00.0 Signed-off-by: Alex Mastro --- Based on the feedback received from Jason G. and Alex W. in v1, print the PCI device syspath to fdinfo instead of the BDF. This provides a more specific waypoint for user space to query for any other relevant information about the device. There was discussion about removing vfio_device_ops callbacks, and relying on just dev_name() in vfio_main.c, but since the concept of PCI syspath is implementation-specific, I think we need to keep some form of callback. Signed-off-by: Alex Mastro Changes in v2: - Instead of PCI bdf, print the fully-qualified syspath (prefixed by /sys) to fdinfo. - Rename the field to "vfio-device-syspath". The term "syspath" was chosen for consistency e.g. libudev's usage of the term. - Link to v1: https://lore.kernel.org/r/20250623-vfio-fdinfo-v1-1-c9cec65a2922@fb.com --- drivers/vfio/pci/vfio_pci.c | 21 +++++++++++++++++++++ drivers/vfio/vfio_main.c | 14 ++++++++++++++ include/linux/vfio.h | 2 ++ 3 files changed, 37 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 5ba39f7623bb..bf3e7d873990 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -17,10 +17,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -125,6 +127,22 @@ static int vfio_pci_open_device(struct vfio_device *core_vdev) return 0; } +#ifdef CONFIG_PROC_FS +static void vfio_pci_core_show_fdinfo(struct vfio_device *core_vdev, struct seq_file *m) +{ + char *path; + struct vfio_pci_core_device *vdev = + container_of(core_vdev, struct vfio_pci_core_device, vdev); + + path = kobject_get_path(&vdev->pdev->dev.kobj, GFP_KERNEL); + if (!path) + return; + + seq_printf(m, "vfio-device-syspath: /sys%s\n", path); + kfree(path); +} +#endif + static const struct vfio_device_ops vfio_pci_ops = { .name = "vfio-pci", .init = vfio_pci_core_init_dev, @@ -138,6 +156,9 @@ static const struct vfio_device_ops vfio_pci_ops = { .mmap = vfio_pci_core_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, +#ifdef CONFIG_PROC_FS + .show_fdinfo = vfio_pci_core_show_fdinfo, +#endif .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas, diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 1fd261efc582..6e883c0c320b 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1354,6 +1354,17 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma) return device->ops->mmap(device, vma); } +#ifdef CONFIG_PROC_FS +static void vfio_device_show_fdinfo(struct seq_file *m, struct file *filep) +{ + struct vfio_device_file *df = filep->private_data; + struct vfio_device *device = df->device; + + if (device->ops->show_fdinfo) + device->ops->show_fdinfo(device, m); +} +#endif + const struct file_operations vfio_device_fops = { .owner = THIS_MODULE, .open = vfio_device_fops_cdev_open, @@ -1363,6 +1374,9 @@ const struct file_operations vfio_device_fops = { .unlocked_ioctl = vfio_device_fops_unl_ioctl, .compat_ioctl = compat_ptr_ioctl, .mmap = vfio_device_fops_mmap, +#ifdef CONFIG_PROC_FS + .show_fdinfo = vfio_device_show_fdinfo, +#endif }; static struct vfio_device *vfio_device_from_file(struct file *file) diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 707b00772ce1..54076045a44f 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -16,6 +16,7 @@ #include #include #include +#include struct kvm; struct iommufd_ctx; @@ -135,6 +136,7 @@ struct vfio_device_ops { void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length); int (*device_feature)(struct vfio_device *device, u32 flags, void __user *arg, size_t argsz); + void (*show_fdinfo)(struct vfio_device *device, struct seq_file *m); }; #if IS_ENABLED(CONFIG_IOMMUFD) --- base-commit: c1d9dac0db168198b6f63f460665256dedad9b6e change-id: 20250724-show-fdinfo-9a916c6779f5 Best regards, -- Alex Mastro