vfio: Add helper to search vfio_device in a dev_set
authorYi Liu <yi.l.liu@intel.com>
Tue, 18 Jul 2023 10:55:39 +0000 (03:55 -0700)
committerAlex Williamson <alex.williamson@redhat.com>
Tue, 25 Jul 2023 16:18:02 +0000 (10:18 -0600)
There are drivers that need to search vfio_device within a given dev_set.
e.g. vfio-pci. So add a helper.

vfio_pci_is_device_in_set() now returns -EBUSY in commit a882c16a2b7e
("vfio/pci: Change vfio_pci_try_bus_reset() to use the dev_set") where
it was trying to preserve the return of vfio_pci_try_zap_and_vma_lock_cb().
However, it makes more sense to return -ENODEV.

Suggested-by: Alex Williamson <alex.williamson@redhat.com>
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Tested-by: Yanting Jiang <yanting.jiang@intel.com>
Tested-by: Terrence Xu <terrence.xu@intel.com>
Tested-by: Zhenzhong Duan <zhenzhong.duan@intel.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Link: https://lore.kernel.org/r/20230718105542.4138-8-yi.l.liu@intel.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/vfio/pci/vfio_pci_core.c
drivers/vfio/vfio_main.c
include/linux/vfio.h

index 3d595ad..5b5316a 100644 (file)
@@ -2377,12 +2377,8 @@ static bool vfio_dev_in_groups(struct vfio_pci_core_device *vdev,
 static int vfio_pci_is_device_in_set(struct pci_dev *pdev, void *data)
 {
        struct vfio_device_set *dev_set = data;
-       struct vfio_device *cur;
 
-       list_for_each_entry(cur, &dev_set->device_list, dev_set_list)
-               if (cur->dev == &pdev->dev)
-                       return 0;
-       return -EBUSY;
+       return vfio_find_device_in_devset(dev_set, &pdev->dev) ? 0 : -ENODEV;
 }
 
 /*
index f0ca33b..ab4f3a7 100644 (file)
@@ -141,6 +141,21 @@ unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set)
 }
 EXPORT_SYMBOL_GPL(vfio_device_set_open_count);
 
+struct vfio_device *
+vfio_find_device_in_devset(struct vfio_device_set *dev_set,
+                          struct device *dev)
+{
+       struct vfio_device *cur;
+
+       lockdep_assert_held(&dev_set->lock);
+
+       list_for_each_entry(cur, &dev_set->device_list, dev_set_list)
+               if (cur->dev == dev)
+                       return cur;
+       return NULL;
+}
+EXPORT_SYMBOL_GPL(vfio_find_device_in_devset);
+
 /*
  * Device objects - create, release, get, put, search
  */
index 2a45853..ee120d2 100644 (file)
@@ -244,6 +244,9 @@ void vfio_unregister_group_dev(struct vfio_device *device);
 
 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
 unsigned int vfio_device_set_open_count(struct vfio_device_set *dev_set);
+struct vfio_device *
+vfio_find_device_in_devset(struct vfio_device_set *dev_set,
+                          struct device *dev);
 
 int vfio_mig_get_next_state(struct vfio_device *device,
                            enum vfio_device_mig_state cur_fsm,