vfio: centralize module refcount in subsystem layer
authorMax Gurtovoy <mgurtovoy@nvidia.com>
Tue, 18 May 2021 19:21:32 +0000 (22:21 +0300)
committerAlex Williamson <alex.williamson@redhat.com>
Tue, 15 Jun 2021 20:12:15 +0000 (14:12 -0600)
Remove code duplication and move module refcounting to the subsystem
module.

Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Link: https://lore.kernel.org/r/20210518192133.59195-2-mgurtovoy@nvidia.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
drivers/vfio/fsl-mc/vfio_fsl_mc.c
drivers/vfio/mdev/vfio_mdev.c
drivers/vfio/pci/vfio_pci.c
drivers/vfio/platform/vfio_platform_common.c
drivers/vfio/vfio.c

index 980e5955130197ed4104e3d59933eefc59e0a96d..90cad109583b8056ecc697ab528fa264e26cb058 100644 (file)
@@ -140,26 +140,18 @@ static int vfio_fsl_mc_open(struct vfio_device *core_vdev)
 {
        struct vfio_fsl_mc_device *vdev =
                container_of(core_vdev, struct vfio_fsl_mc_device, vdev);
-       int ret;
-
-       if (!try_module_get(THIS_MODULE))
-               return -ENODEV;
+       int ret = 0;
 
        mutex_lock(&vdev->reflck->lock);
        if (!vdev->refcnt) {
                ret = vfio_fsl_mc_regions_init(vdev);
                if (ret)
-                       goto err_reg_init;
+                       goto out;
        }
        vdev->refcnt++;
-
+out:
        mutex_unlock(&vdev->reflck->lock);
 
-       return 0;
-
-err_reg_init:
-       mutex_unlock(&vdev->reflck->lock);
-       module_put(THIS_MODULE);
        return ret;
 }
 
@@ -196,8 +188,6 @@ static void vfio_fsl_mc_release(struct vfio_device *core_vdev)
        }
 
        mutex_unlock(&vdev->reflck->lock);
-
-       module_put(THIS_MODULE);
 }
 
 static long vfio_fsl_mc_ioctl(struct vfio_device *core_vdev,
index 922729071c5a8e4cc56d7278e2570e0cc63bccf6..5ef4815609edb5044c89bbebd9de41358ac2543b 100644 (file)
@@ -26,19 +26,10 @@ static int vfio_mdev_open(struct vfio_device *core_vdev)
        struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
        struct mdev_parent *parent = mdev->type->parent;
 
-       int ret;
-
        if (unlikely(!parent->ops->open))
                return -EINVAL;
 
-       if (!try_module_get(THIS_MODULE))
-               return -ENODEV;
-
-       ret = parent->ops->open(mdev);
-       if (ret)
-               module_put(THIS_MODULE);
-
-       return ret;
+       return parent->ops->open(mdev);
 }
 
 static void vfio_mdev_release(struct vfio_device *core_vdev)
@@ -48,8 +39,6 @@ static void vfio_mdev_release(struct vfio_device *core_vdev)
 
        if (likely(parent->ops->release))
                parent->ops->release(mdev);
-
-       module_put(THIS_MODULE);
 }
 
 static long vfio_mdev_unlocked_ioctl(struct vfio_device *core_vdev,
index bd7c482c948aac2a2a2aca673966d55404300f25..f6729baa1bf45087eae2afd2dba63ae65de30910 100644 (file)
@@ -558,8 +558,6 @@ static void vfio_pci_release(struct vfio_device *core_vdev)
        }
 
        mutex_unlock(&vdev->reflck->lock);
-
-       module_put(THIS_MODULE);
 }
 
 static int vfio_pci_open(struct vfio_device *core_vdev)
@@ -568,9 +566,6 @@ static int vfio_pci_open(struct vfio_device *core_vdev)
                container_of(core_vdev, struct vfio_pci_device, vdev);
        int ret = 0;
 
-       if (!try_module_get(THIS_MODULE))
-               return -ENODEV;
-
        mutex_lock(&vdev->reflck->lock);
 
        if (!vdev->refcnt) {
@@ -584,8 +579,6 @@ static int vfio_pci_open(struct vfio_device *core_vdev)
        vdev->refcnt++;
 error:
        mutex_unlock(&vdev->reflck->lock);
-       if (ret)
-               module_put(THIS_MODULE);
        return ret;
 }
 
index 470fcf7dac564cece9a6fdc867b9e646649d6df6..703164df7637db432ee13edc0a97a97bce8a2064 100644 (file)
@@ -241,8 +241,6 @@ static void vfio_platform_release(struct vfio_device *core_vdev)
        }
 
        mutex_unlock(&driver_lock);
-
-       module_put(vdev->parent_module);
 }
 
 static int vfio_platform_open(struct vfio_device *core_vdev)
@@ -251,9 +249,6 @@ static int vfio_platform_open(struct vfio_device *core_vdev)
                container_of(core_vdev, struct vfio_platform_device, vdev);
        int ret;
 
-       if (!try_module_get(vdev->parent_module))
-               return -ENODEV;
-
        mutex_lock(&driver_lock);
 
        if (!vdev->refcnt) {
@@ -291,7 +286,6 @@ err_irq:
        vfio_platform_regions_cleanup(vdev);
 err_reg:
        mutex_unlock(&driver_lock);
-       module_put(vdev->parent_module);
        return ret;
 }
 
index 5e631c359ef23c129c5a482fed22d46aa8c98618..02cc51ce6891a9ccca70672766f6b692fc9383f4 100644 (file)
@@ -1369,8 +1369,14 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
        if (IS_ERR(device))
                return PTR_ERR(device);
 
+       if (!try_module_get(device->dev->driver->owner)) {
+               vfio_device_put(device);
+               return -ENODEV;
+       }
+
        ret = device->ops->open(device);
        if (ret) {
+               module_put(device->dev->driver->owner);
                vfio_device_put(device);
                return ret;
        }
@@ -1382,6 +1388,7 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
        ret = get_unused_fd_flags(O_CLOEXEC);
        if (ret < 0) {
                device->ops->release(device);
+               module_put(device->dev->driver->owner);
                vfio_device_put(device);
                return ret;
        }
@@ -1392,6 +1399,7 @@ static int vfio_group_get_device_fd(struct vfio_group *group, char *buf)
                put_unused_fd(ret);
                ret = PTR_ERR(filep);
                device->ops->release(device);
+               module_put(device->dev->driver->owner);
                vfio_device_put(device);
                return ret;
        }
@@ -1550,6 +1558,8 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep)
 
        device->ops->release(device);
 
+       module_put(device->dev->driver->owner);
+
        vfio_group_try_dissolve_container(device->group);
 
        vfio_device_put(device);