Skip to content

Commit

Permalink
vfio: centralize module refcount in subsystem layer
Browse files Browse the repository at this point in the history
Remove code duplication and move module refcounting to the subsystem
module.

Signed-off-by: Max Gurtovoy <[email protected]>
Reviewed-by: Eric Auger <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alex Williamson <[email protected]>
  • Loading branch information
mgurtovoy authored and awilliam committed Jun 15, 2021
1 parent 009c9aa commit 9dcf01d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 38 deletions.
16 changes: 3 additions & 13 deletions drivers/vfio/fsl-mc/vfio_fsl_mc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 1 addition & 12 deletions drivers/vfio/mdev/vfio_mdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions drivers/vfio/pci/vfio_pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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;
}

Expand Down
6 changes: 0 additions & 6 deletions drivers/vfio/platform/vfio_platform_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -291,7 +286,6 @@ static int vfio_platform_open(struct vfio_device *core_vdev)
vfio_platform_regions_cleanup(vdev);
err_reg:
mutex_unlock(&driver_lock);
module_put(vdev->parent_module);
return ret;
}

Expand Down
10 changes: 10 additions & 0 deletions drivers/vfio/vfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9dcf01d

Please sign in to comment.