[PATCH v3 1/8] vfio: add helper to find PCI device by vendor/device ID
Jose Ignacio Tornos Martinez
jtornosm at redhat.com
Thu Oct 29 09:37:06 PDT 2026
Add vfio_file_find_pci_device() that searches for a PCI device with
the specified vendor/device ID within a VFIO group or device file. The
function walks the group device list to find a matching PCI device, or
checks the single device for a cdev-based file.
This helper enables other subsystems (e.g. KVM PCI MSI domain) to
locate VFIO-owned PCI devices by their IDs without depending on VFIO
internals. The companion vfio_file_device() provides access to the
underlying vfio_device from a device file descriptor.
Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm at redhat.com>
---
v3: new
v2: https://lore.kernel.org/all/20261011074514.515894-1-jtornosm@redhat.com/
drivers/vfio/group.c | 41 ++++++++++++++++++++++++++++++++++++++++
drivers/vfio/vfio_main.c | 6 ++++++
include/linux/vfio.h | 10 ++++++++++
3 files changed, 57 insertions(+)
diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c
index 185f359f75d3..0b6cf90edda3 100644
--- a/drivers/vfio/group.c
+++ b/drivers/vfio/group.c
@@ -10,6 +10,7 @@
* Author: Tom Lyon, pugs at cisco.com
*/
+#include <linux/pci.h>
#include <linux/vfio.h>
#include <linux/iommufd.h>
#include <linux/anon_inodes.h>
@@ -894,6 +895,46 @@ bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
}
EXPORT_SYMBOL_GPL(vfio_file_has_dev);
+struct vfio_device *vfio_file_find_pci_device(struct file *file,
+ u16 vendor, u16 device_id)
+{
+ struct vfio_group *group;
+ struct vfio_device *vdev;
+ struct vfio_device *ret = NULL;
+
+ group = vfio_group_from_file(file);
+ if (group) {
+ mutex_lock(&group->device_lock);
+ list_for_each_entry(vdev, &group->device_list, group_next) {
+ struct pci_dev *pdev;
+
+ if (!dev_is_pci(vdev->dev))
+ continue;
+
+ pdev = to_pci_dev(vdev->dev);
+ if (pdev->vendor == vendor &&
+ pdev->device == device_id) {
+ ret = vdev;
+ break;
+ }
+ }
+ mutex_unlock(&group->device_lock);
+ return ret;
+ }
+
+ vdev = vfio_file_device(file);
+ if (vdev && dev_is_pci(vdev->dev)) {
+ struct pci_dev *pdev = to_pci_dev(vdev->dev);
+
+ if (pdev->vendor == vendor &&
+ pdev->device == device_id)
+ return vdev;
+ }
+
+ return NULL;
+}
+EXPORT_SYMBOL_GPL(vfio_file_find_pci_device);
+
static char *vfio_devnode(const struct device *dev, umode_t *mode)
{
return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev));
diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
index 4e5d738ccf4b..a83e5d805eac 100644
--- a/drivers/vfio/vfio_main.c
+++ b/drivers/vfio/vfio_main.c
@@ -1551,6 +1551,12 @@ void vfio_file_set_kvm(struct file *vfio_file, struct file *kvm)
}
EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
+struct vfio_device *vfio_file_device(struct file *file)
+{
+ return vfio_device_from_file(file);
+}
+EXPORT_SYMBOL_GPL(vfio_file_device);
+
/*
* Sub-module support
*/
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 0cc91c6f96d2..a62fbf42458a 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -364,6 +364,8 @@ struct iommu_group *vfio_file_iommu_group(struct file *file);
#if IS_ENABLED(CONFIG_VFIO_GROUP)
bool vfio_file_is_group(struct file *file);
bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
+struct vfio_device *vfio_file_find_pci_device(struct file *file,
+ u16 vendor, u16 device_id);
#else
static inline bool vfio_file_is_group(struct file *file)
{
@@ -374,10 +376,18 @@ static inline bool vfio_file_has_dev(struct file *file, struct vfio_device *devi
{
return false;
}
+
+static inline struct vfio_device *vfio_file_find_pci_device(struct file *file,
+ u16 vendor,
+ u16 device_id)
+{
+ return NULL;
+}
#endif
bool vfio_file_is_valid(struct file *file);
bool vfio_file_enforced_coherent(struct file *file);
void vfio_file_set_kvm(struct file *vfio_file, struct file *kvm);
+struct vfio_device *vfio_file_device(struct file *file);
#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
--
2.56.0
More information about the ath12k
mailing list