vfio: Make vfio_(un)register_notifier accept a vfio_device
authorJason Gunthorpe <jgg@nvidia.com>
Wed, 11 May 2022 19:12:58 +0000 (13:12 -0600)
committerAlex Williamson <alex.williamson@redhat.com>
Wed, 11 May 2022 19:12:58 +0000 (13:12 -0600)
All callers have a struct vfio_device trivially available, pass it in
directly and avoid calling the expensive vfio_group_get_from_dev().

Acked-by: Eric Farman <farman@linux.ibm.com>
Reviewed-by: Jason J. Herne <jjherne@linux.ibm.com>
Reviewed-by: Tony Krowiak <akrowiak@linux.ibm.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/1-v4-8045e76bf00b+13d-vfio_mdev_no_group_jgg@nvidia.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/gpu/drm/i915/gvt/kvmgt.c
drivers/s390/cio/vfio_ccw_ops.c
drivers/s390/crypto/vfio_ap_ops.c
drivers/vfio/vfio.c
include/linux/vfio.h

index 0787ba5c301f5ecff4ef6e34a969018a0a123605..1cec4f1fdfacedee9a88dd19e7b646ba910cdac6 100644 (file)
@@ -810,8 +810,8 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
        vgpu->group_notifier.notifier_call = intel_vgpu_group_notifier;
 
        events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
-       ret = vfio_register_notifier(vfio_dev->dev, VFIO_IOMMU_NOTIFY, &events,
-                               &vgpu->iommu_notifier);
+       ret = vfio_register_notifier(vfio_dev, VFIO_IOMMU_NOTIFY, &events,
+                                    &vgpu->iommu_notifier);
        if (ret != 0) {
                gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
                        ret);
@@ -819,8 +819,8 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
        }
 
        events = VFIO_GROUP_NOTIFY_SET_KVM;
-       ret = vfio_register_notifier(vfio_dev->dev, VFIO_GROUP_NOTIFY, &events,
-                               &vgpu->group_notifier);
+       ret = vfio_register_notifier(vfio_dev, VFIO_GROUP_NOTIFY, &events,
+                                    &vgpu->group_notifier);
        if (ret != 0) {
                gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
                        ret);
@@ -873,12 +873,12 @@ undo_group:
        vgpu->vfio_group = NULL;
 
 undo_register:
-       vfio_unregister_notifier(vfio_dev->dev, VFIO_GROUP_NOTIFY,
-                                       &vgpu->group_notifier);
+       vfio_unregister_notifier(vfio_dev, VFIO_GROUP_NOTIFY,
+                                &vgpu->group_notifier);
 
 undo_iommu:
-       vfio_unregister_notifier(vfio_dev->dev, VFIO_IOMMU_NOTIFY,
-                                       &vgpu->iommu_notifier);
+       vfio_unregister_notifier(vfio_dev, VFIO_IOMMU_NOTIFY,
+                                &vgpu->iommu_notifier);
 out:
        return ret;
 }
@@ -907,13 +907,13 @@ static void __intel_vgpu_release(struct intel_vgpu *vgpu)
 
        intel_gvt_release_vgpu(vgpu);
 
-       ret = vfio_unregister_notifier(vgpu->vfio_device.dev, VFIO_IOMMU_NOTIFY,
-                                       &vgpu->iommu_notifier);
+       ret = vfio_unregister_notifier(&vgpu->vfio_device, VFIO_IOMMU_NOTIFY,
+                                      &vgpu->iommu_notifier);
        drm_WARN(&i915->drm, ret,
                 "vfio_unregister_notifier for iommu failed: %d\n", ret);
 
-       ret = vfio_unregister_notifier(vgpu->vfio_device.dev, VFIO_GROUP_NOTIFY,
-                                       &vgpu->group_notifier);
+       ret = vfio_unregister_notifier(&vgpu->vfio_device, VFIO_GROUP_NOTIFY,
+                                      &vgpu->group_notifier);
        drm_WARN(&i915->drm, ret,
                 "vfio_unregister_notifier for group failed: %d\n", ret);
 
index c4d60cdbf247bfeecd7d8039448a2f1d92ba2268..b49e2e9db2dc6fe9f43d40777d4acb54c8940e0d 100644 (file)
@@ -183,7 +183,7 @@ static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
 
        private->nb.notifier_call = vfio_ccw_mdev_notifier;
 
-       ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
+       ret = vfio_register_notifier(vdev, VFIO_IOMMU_NOTIFY,
                                     &events, &private->nb);
        if (ret)
                return ret;
@@ -204,8 +204,7 @@ static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
 
 out_unregister:
        vfio_ccw_unregister_dev_regions(private);
-       vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
-                                &private->nb);
+       vfio_unregister_notifier(vdev, VFIO_IOMMU_NOTIFY, &private->nb);
        return ret;
 }
 
@@ -223,7 +222,7 @@ static void vfio_ccw_mdev_close_device(struct vfio_device *vdev)
 
        cp_free(&private->cp);
        vfio_ccw_unregister_dev_regions(private);
-       vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY, &private->nb);
+       vfio_unregister_notifier(vdev, VFIO_IOMMU_NOTIFY, &private->nb);
 }
 
 static ssize_t vfio_ccw_mdev_read_io_region(struct vfio_ccw_private *private,
index ee0a3bf8f476ca2460202a579164d07948ca5082..bfa7ee6ef532d9f4d451057e95322826aa1ebc4b 100644 (file)
@@ -1406,21 +1406,21 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
        matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
        events = VFIO_GROUP_NOTIFY_SET_KVM;
 
-       ret = vfio_register_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
-                                    &events, &matrix_mdev->group_notifier);
+       ret = vfio_register_notifier(vdev, VFIO_GROUP_NOTIFY, &events,
+                                    &matrix_mdev->group_notifier);
        if (ret)
                return ret;
 
        matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
        events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
-       ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
-                                    &events, &matrix_mdev->iommu_notifier);
+       ret = vfio_register_notifier(vdev, VFIO_IOMMU_NOTIFY, &events,
+                                    &matrix_mdev->iommu_notifier);
        if (ret)
                goto out_unregister_group;
        return 0;
 
 out_unregister_group:
-       vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
+       vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
                                 &matrix_mdev->group_notifier);
        return ret;
 }
@@ -1430,9 +1430,9 @@ static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
        struct ap_matrix_mdev *matrix_mdev =
                container_of(vdev, struct ap_matrix_mdev, vdev);
 
-       vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
+       vfio_unregister_notifier(vdev, VFIO_IOMMU_NOTIFY,
                                 &matrix_mdev->iommu_notifier);
-       vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
+       vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
                                 &matrix_mdev->group_notifier);
        vfio_ap_mdev_unset_kvm(matrix_mdev);
 }
index 7b0a7b85e77ebb9bd25af7cb99de1f8b90c9c065..cb4730756510047237e73e73dbd170ce7e09f60b 100644 (file)
@@ -2484,19 +2484,16 @@ static int vfio_unregister_group_notifier(struct vfio_group *group,
        return ret;
 }
 
-int vfio_register_notifier(struct device *dev, enum vfio_notify_type type,
-                          unsigned long *events, struct notifier_block *nb)
+int vfio_register_notifier(struct vfio_device *device,
+                          enum vfio_notify_type type, unsigned long *events,
+                          struct notifier_block *nb)
 {
-       struct vfio_group *group;
+       struct vfio_group *group = device->group;
        int ret;
 
-       if (!dev || !nb || !events || (*events == 0))
+       if (!nb || !events || (*events == 0))
                return -EINVAL;
 
-       group = vfio_group_get_from_dev(dev);
-       if (!group)
-               return -ENODEV;
-
        switch (type) {
        case VFIO_IOMMU_NOTIFY:
                ret = vfio_register_iommu_notifier(group, events, nb);
@@ -2507,25 +2504,20 @@ int vfio_register_notifier(struct device *dev, enum vfio_notify_type type,
        default:
                ret = -EINVAL;
        }
-
-       vfio_group_put(group);
        return ret;
 }
 EXPORT_SYMBOL(vfio_register_notifier);
 
-int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
+int vfio_unregister_notifier(struct vfio_device *device,
+                            enum vfio_notify_type type,
                             struct notifier_block *nb)
 {
-       struct vfio_group *group;
+       struct vfio_group *group = device->group;
        int ret;
 
-       if (!dev || !nb)
+       if (!nb)
                return -EINVAL;
 
-       group = vfio_group_get_from_dev(dev);
-       if (!group)
-               return -ENODEV;
-
        switch (type) {
        case VFIO_IOMMU_NOTIFY:
                ret = vfio_unregister_iommu_notifier(group, nb);
@@ -2536,8 +2528,6 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
        default:
                ret = -EINVAL;
        }
-
-       vfio_group_put(group);
        return ret;
 }
 EXPORT_SYMBOL(vfio_unregister_notifier);
index 66dda06ec42d1bce221f762b87a2541da04eeb64..a00fd722f044b00f995ef67bad92bcb57b3f421d 100644 (file)
@@ -178,11 +178,11 @@ enum vfio_notify_type {
 /* events for VFIO_GROUP_NOTIFY */
 #define VFIO_GROUP_NOTIFY_SET_KVM      BIT(0)
 
-extern int vfio_register_notifier(struct device *dev,
+extern int vfio_register_notifier(struct vfio_device *device,
                                  enum vfio_notify_type type,
                                  unsigned long *required_events,
                                  struct notifier_block *nb);
-extern int vfio_unregister_notifier(struct device *dev,
+extern int vfio_unregister_notifier(struct vfio_device *device,
                                    enum vfio_notify_type type,
                                    struct notifier_block *nb);