Commit 09ea48ef authored by Jason Gunthorpe's avatar Jason Gunthorpe Committed by Alex Williamson

vfio: Make vfio_(un)register_notifier accept a vfio_device

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: default avatarEric Farman <farman@linux.ibm.com>
Reviewed-by: default avatarJason J. Herne <jjherne@linux.ibm.com>
Reviewed-by: default avatarTony Krowiak <akrowiak@linux.ibm.com>
Reviewed-by: default avatarKevin Tian <kevin.tian@intel.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Link: https://lore.kernel.org/r/1-v4-8045e76bf00b+13d-vfio_mdev_no_group_jgg@nvidia.comSigned-off-by: default avatarAlex Williamson <alex.williamson@redhat.com>
parent a77109ff
...@@ -810,8 +810,8 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev) ...@@ -810,8 +810,8 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
vgpu->group_notifier.notifier_call = intel_vgpu_group_notifier; vgpu->group_notifier.notifier_call = intel_vgpu_group_notifier;
events = VFIO_IOMMU_NOTIFY_DMA_UNMAP; events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
ret = vfio_register_notifier(vfio_dev->dev, VFIO_IOMMU_NOTIFY, &events, ret = vfio_register_notifier(vfio_dev, VFIO_IOMMU_NOTIFY, &events,
&vgpu->iommu_notifier); &vgpu->iommu_notifier);
if (ret != 0) { if (ret != 0) {
gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n", gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
ret); ret);
...@@ -819,8 +819,8 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev) ...@@ -819,8 +819,8 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
} }
events = VFIO_GROUP_NOTIFY_SET_KVM; events = VFIO_GROUP_NOTIFY_SET_KVM;
ret = vfio_register_notifier(vfio_dev->dev, VFIO_GROUP_NOTIFY, &events, ret = vfio_register_notifier(vfio_dev, VFIO_GROUP_NOTIFY, &events,
&vgpu->group_notifier); &vgpu->group_notifier);
if (ret != 0) { if (ret != 0) {
gvt_vgpu_err("vfio_register_notifier for group failed: %d\n", gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
ret); ret);
...@@ -873,12 +873,12 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev) ...@@ -873,12 +873,12 @@ static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
vgpu->vfio_group = NULL; vgpu->vfio_group = NULL;
undo_register: undo_register:
vfio_unregister_notifier(vfio_dev->dev, VFIO_GROUP_NOTIFY, vfio_unregister_notifier(vfio_dev, VFIO_GROUP_NOTIFY,
&vgpu->group_notifier); &vgpu->group_notifier);
undo_iommu: undo_iommu:
vfio_unregister_notifier(vfio_dev->dev, VFIO_IOMMU_NOTIFY, vfio_unregister_notifier(vfio_dev, VFIO_IOMMU_NOTIFY,
&vgpu->iommu_notifier); &vgpu->iommu_notifier);
out: out:
return ret; return ret;
} }
...@@ -907,13 +907,13 @@ static void __intel_vgpu_release(struct intel_vgpu *vgpu) ...@@ -907,13 +907,13 @@ static void __intel_vgpu_release(struct intel_vgpu *vgpu)
intel_gvt_release_vgpu(vgpu); intel_gvt_release_vgpu(vgpu);
ret = vfio_unregister_notifier(vgpu->vfio_device.dev, VFIO_IOMMU_NOTIFY, ret = vfio_unregister_notifier(&vgpu->vfio_device, VFIO_IOMMU_NOTIFY,
&vgpu->iommu_notifier); &vgpu->iommu_notifier);
drm_WARN(&i915->drm, ret, drm_WARN(&i915->drm, ret,
"vfio_unregister_notifier for iommu failed: %d\n", ret); "vfio_unregister_notifier for iommu failed: %d\n", ret);
ret = vfio_unregister_notifier(vgpu->vfio_device.dev, VFIO_GROUP_NOTIFY, ret = vfio_unregister_notifier(&vgpu->vfio_device, VFIO_GROUP_NOTIFY,
&vgpu->group_notifier); &vgpu->group_notifier);
drm_WARN(&i915->drm, ret, drm_WARN(&i915->drm, ret,
"vfio_unregister_notifier for group failed: %d\n", ret); "vfio_unregister_notifier for group failed: %d\n", ret);
......
...@@ -183,7 +183,7 @@ static int vfio_ccw_mdev_open_device(struct vfio_device *vdev) ...@@ -183,7 +183,7 @@ static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
private->nb.notifier_call = vfio_ccw_mdev_notifier; 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); &events, &private->nb);
if (ret) if (ret)
return ret; return ret;
...@@ -204,8 +204,7 @@ static int vfio_ccw_mdev_open_device(struct vfio_device *vdev) ...@@ -204,8 +204,7 @@ static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
out_unregister: out_unregister:
vfio_ccw_unregister_dev_regions(private); vfio_ccw_unregister_dev_regions(private);
vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY, vfio_unregister_notifier(vdev, VFIO_IOMMU_NOTIFY, &private->nb);
&private->nb);
return ret; return ret;
} }
...@@ -223,7 +222,7 @@ static void vfio_ccw_mdev_close_device(struct vfio_device *vdev) ...@@ -223,7 +222,7 @@ static void vfio_ccw_mdev_close_device(struct vfio_device *vdev)
cp_free(&private->cp); cp_free(&private->cp);
vfio_ccw_unregister_dev_regions(private); 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, static ssize_t vfio_ccw_mdev_read_io_region(struct vfio_ccw_private *private,
......
...@@ -1406,21 +1406,21 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev) ...@@ -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; matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
events = VFIO_GROUP_NOTIFY_SET_KVM; events = VFIO_GROUP_NOTIFY_SET_KVM;
ret = vfio_register_notifier(vdev->dev, VFIO_GROUP_NOTIFY, ret = vfio_register_notifier(vdev, VFIO_GROUP_NOTIFY, &events,
&events, &matrix_mdev->group_notifier); &matrix_mdev->group_notifier);
if (ret) if (ret)
return ret; return ret;
matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier; matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
events = VFIO_IOMMU_NOTIFY_DMA_UNMAP; events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY, ret = vfio_register_notifier(vdev, VFIO_IOMMU_NOTIFY, &events,
&events, &matrix_mdev->iommu_notifier); &matrix_mdev->iommu_notifier);
if (ret) if (ret)
goto out_unregister_group; goto out_unregister_group;
return 0; return 0;
out_unregister_group: out_unregister_group:
vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY, vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
&matrix_mdev->group_notifier); &matrix_mdev->group_notifier);
return ret; return ret;
} }
...@@ -1430,9 +1430,9 @@ static void vfio_ap_mdev_close_device(struct vfio_device *vdev) ...@@ -1430,9 +1430,9 @@ static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
struct ap_matrix_mdev *matrix_mdev = struct ap_matrix_mdev *matrix_mdev =
container_of(vdev, struct ap_matrix_mdev, vdev); 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); &matrix_mdev->iommu_notifier);
vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY, vfio_unregister_notifier(vdev, VFIO_GROUP_NOTIFY,
&matrix_mdev->group_notifier); &matrix_mdev->group_notifier);
vfio_ap_mdev_unset_kvm(matrix_mdev); vfio_ap_mdev_unset_kvm(matrix_mdev);
} }
......
...@@ -2484,19 +2484,16 @@ static int vfio_unregister_group_notifier(struct vfio_group *group, ...@@ -2484,19 +2484,16 @@ static int vfio_unregister_group_notifier(struct vfio_group *group,
return ret; return ret;
} }
int vfio_register_notifier(struct device *dev, enum vfio_notify_type type, int vfio_register_notifier(struct vfio_device *device,
unsigned long *events, struct notifier_block *nb) enum vfio_notify_type type, unsigned long *events,
struct notifier_block *nb)
{ {
struct vfio_group *group; struct vfio_group *group = device->group;
int ret; int ret;
if (!dev || !nb || !events || (*events == 0)) if (!nb || !events || (*events == 0))
return -EINVAL; return -EINVAL;
group = vfio_group_get_from_dev(dev);
if (!group)
return -ENODEV;
switch (type) { switch (type) {
case VFIO_IOMMU_NOTIFY: case VFIO_IOMMU_NOTIFY:
ret = vfio_register_iommu_notifier(group, events, nb); ret = vfio_register_iommu_notifier(group, events, nb);
...@@ -2507,25 +2504,20 @@ int vfio_register_notifier(struct device *dev, enum vfio_notify_type type, ...@@ -2507,25 +2504,20 @@ int vfio_register_notifier(struct device *dev, enum vfio_notify_type type,
default: default:
ret = -EINVAL; ret = -EINVAL;
} }
vfio_group_put(group);
return ret; return ret;
} }
EXPORT_SYMBOL(vfio_register_notifier); 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 notifier_block *nb)
{ {
struct vfio_group *group; struct vfio_group *group = device->group;
int ret; int ret;
if (!dev || !nb) if (!nb)
return -EINVAL; return -EINVAL;
group = vfio_group_get_from_dev(dev);
if (!group)
return -ENODEV;
switch (type) { switch (type) {
case VFIO_IOMMU_NOTIFY: case VFIO_IOMMU_NOTIFY:
ret = vfio_unregister_iommu_notifier(group, nb); ret = vfio_unregister_iommu_notifier(group, nb);
...@@ -2536,8 +2528,6 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type, ...@@ -2536,8 +2528,6 @@ int vfio_unregister_notifier(struct device *dev, enum vfio_notify_type type,
default: default:
ret = -EINVAL; ret = -EINVAL;
} }
vfio_group_put(group);
return ret; return ret;
} }
EXPORT_SYMBOL(vfio_unregister_notifier); EXPORT_SYMBOL(vfio_unregister_notifier);
......
...@@ -178,11 +178,11 @@ enum vfio_notify_type { ...@@ -178,11 +178,11 @@ enum vfio_notify_type {
/* events for VFIO_GROUP_NOTIFY */ /* events for VFIO_GROUP_NOTIFY */
#define VFIO_GROUP_NOTIFY_SET_KVM BIT(0) #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, enum vfio_notify_type type,
unsigned long *required_events, unsigned long *required_events,
struct notifier_block *nb); 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, enum vfio_notify_type type,
struct notifier_block *nb); struct notifier_block *nb);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment