Skip to contentSkip to conversation

vfio/platform: fix module_put call in error flow

PATCH In review Max Gurtovoy 3 participants 8 messages May 18, 2021 – May 26, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 vfio/platform: fix module_put call in error flow

The ->parent_module is the one that use in try_module_get. It should also be the one the we use in module_put during vfio_platform_open().

Fixes 32a2d71c4e808 ("vfio: platform: introduce vfio-platform-base module")
---
 drivers/vfio/platform/vfio_platform_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/platform/vfio_platform_common.c +1 -1
index 361e5b57e369..470fcf7dac56 100644
@@ -291,7 +291,7 @@ static int vfio_platform_open(struct vfio_device *core_vdev)
 	vfio_platform_regions_cleanup(vdev);
 err_reg:
 	mutex_unlock(&driver_lock);
-	module_put(THIS_MODULE);
+	module_put(vdev->parent_module);
 	return ret;
 }
 
Signature · 1 line
2.18.1
#1 lore raw

PATCH 2/3 vfio: centralize module refcount in subsystem layer

Remove code duplication and move module refcounting to the subsystem module.

---
 drivers/vfio/fsl-mc/vfio_fsl_mc.c            | 16 +++-------------
 drivers/vfio/mdev/vfio_mdev.c                | 13 +------------
 drivers/vfio/pci/vfio_pci.c                  |  7 -------
 drivers/vfio/platform/vfio_platform_common.c |  6 ------
 drivers/vfio/vfio.c                          | 10 ++++++++++
 5 files changed, 14 insertions(+), 38 deletions(-)
drivers/vfio/fsl-mc/vfio_fsl_mc.c +3 -13
index 980e59551301..90cad109583b 100644
@@ -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,
drivers/vfio/mdev/vfio_mdev.c +1 -12
index 922729071c5a..5ef4815609ed 100644
@@ -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,
drivers/vfio/pci/vfio_pci.c +0 -7
index bd7c482c948a..f6729baa1bf4 100644
@@ -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;
 }
 
drivers/vfio/platform/vfio_platform_common.c +0 -6
index 470fcf7dac56..703164df7637 100644
@@ -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 @@ 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;
 }
 
drivers/vfio/vfio.c +10 -0
index 5e631c359ef2..02cc51ce6891 100644
@@ -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);
Signature · 1 line
2.18.1
maintainer Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] vfio: centralize module refcount in subsystem layer

Hi Max,

Quoted 4 lines · Max Gurtovoy

Remove code duplication and move module refcounting to the subsystem module.

Thanks

Eric

Quoted 193 lines
---
 drivers/vfio/fsl-mc/vfio_fsl_mc.c            | 16 +++-------------
 drivers/vfio/mdev/vfio_mdev.c                | 13 +------------
 drivers/vfio/pci/vfio_pci.c                  |  7 -------
 drivers/vfio/platform/vfio_platform_common.c |  6 ------
 drivers/vfio/vfio.c                          | 10 ++++++++++
 5 files changed, 14 insertions(+), 38 deletions(-)
drivers/vfio/fsl-mc/vfio_fsl_mc.c +3 -13
index 980e59551301..90cad109583b 100644
@@ -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,
drivers/vfio/mdev/vfio_mdev.c +1 -12
index 922729071c5a..5ef4815609ed 100644
@@ -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,
drivers/vfio/pci/vfio_pci.c +0 -7
index bd7c482c948a..f6729baa1bf4 100644
@@ -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;
 }
 
drivers/vfio/platform/vfio_platform_common.c +0 -6
index 470fcf7dac56..703164df7637 100644
@@ -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 @@ 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;
 }
 
drivers/vfio/vfio.c +10 -0
index 5e631c359ef2..02cc51ce6891 100644
@@ -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);
#3 lore raw

PATCH 3/3 vfio/platform: remove unneeded parent_module attribute

In reply to Max Gurtovoy

The vfio core driver is now taking refcount on the provider drivers, remove redundant parent_module attribute from vfio_platform_device structure.

---
 drivers/vfio/platform/vfio_amba.c             | 1 -
 drivers/vfio/platform/vfio_platform.c         | 1 -
 drivers/vfio/platform/vfio_platform_private.h | 1 -
 3 files changed, 3 deletions(-)
drivers/vfio/platform/vfio_amba.c +0 -1
index f970eb2a999f..badfffea14fb 100644
@@ -59,7 +59,6 @@ static int vfio_amba_probe(struct amba_device *adev, const struct amba_id *id)
 	vdev->flags = VFIO_DEVICE_FLAGS_AMBA;
 	vdev->get_resource = get_amba_resource;
 	vdev->get_irq = get_amba_irq;
-	vdev->parent_module = THIS_MODULE;
 	vdev->reset_required = false;
 
 	ret = vfio_platform_probe_common(vdev, &adev->dev);
drivers/vfio/platform/vfio_platform.c +0 -1
index e4027799a154..68a1c87066d7 100644
@@ -50,7 +50,6 @@ static int vfio_platform_probe(struct platform_device *pdev)
 	vdev->flags = VFIO_DEVICE_FLAGS_PLATFORM;
 	vdev->get_resource = get_platform_resource;
 	vdev->get_irq = get_platform_irq;
-	vdev->parent_module = THIS_MODULE;
 	vdev->reset_required = reset_required;
 
 	ret = vfio_platform_probe_common(vdev, &pdev->dev);
drivers/vfio/platform/vfio_platform_private.h +0 -1
index a5ba82c8cbc3..dfb834c13659 100644
@@ -50,7 +50,6 @@ struct vfio_platform_device {
 	u32				num_irqs;
 	int				refcnt;
 	struct mutex			igate;
-	struct module			*parent_module;
 	const char			*compat;
 	const char			*acpihid;
 	struct module			*reset_module;
Signature · 1 line
2.18.1
maintainer Acked-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] vfio/platform: remove unneeded parent_module attribute

Hi Max,

Quoted 5 lines · Max Gurtovoy

The vfio core driver is now taking refcount on the provider drivers, remove redundant parent_module attribute from vfio_platform_device structure.

Thanks

Eric

Quoted 43 lines
---
 drivers/vfio/platform/vfio_amba.c             | 1 -
 drivers/vfio/platform/vfio_platform.c         | 1 -
 drivers/vfio/platform/vfio_platform_private.h | 1 -
 3 files changed, 3 deletions(-)
drivers/vfio/platform/vfio_amba.c +0 -1
index f970eb2a999f..badfffea14fb 100644
@@ -59,7 +59,6 @@ static int vfio_amba_probe(struct amba_device *adev, const struct amba_id *id)
 	vdev->flags = VFIO_DEVICE_FLAGS_AMBA;
 	vdev->get_resource = get_amba_resource;
 	vdev->get_irq = get_amba_irq;
-	vdev->parent_module = THIS_MODULE;
 	vdev->reset_required = false;
 
 	ret = vfio_platform_probe_common(vdev, &adev->dev);
drivers/vfio/platform/vfio_platform.c +0 -1
index e4027799a154..68a1c87066d7 100644
@@ -50,7 +50,6 @@ static int vfio_platform_probe(struct platform_device *pdev)
 	vdev->flags = VFIO_DEVICE_FLAGS_PLATFORM;
 	vdev->get_resource = get_platform_resource;
 	vdev->get_irq = get_platform_irq;
-	vdev->parent_module = THIS_MODULE;
 	vdev->reset_required = reset_required;
 
 	ret = vfio_platform_probe_common(vdev, &pdev->dev);
drivers/vfio/platform/vfio_platform_private.h +0 -1
index a5ba82c8cbc3..dfb834c13659 100644
@@ -50,7 +50,6 @@ struct vfio_platform_device {
 	u32				num_irqs;
 	int				refcnt;
 	struct mutex			igate;
-	struct module			*parent_module;
 	const char			*compat;
 	const char			*acpihid;
 	struct module			*reset_module;
#5 lore raw
In reply to Max Gurtovoy
Quoted 15 lines · On Tue, 18 May 2021 22:21:31 +0300 Max Gurtovoy <[email protected]> wrote:

The ->parent_module is the one that use in try_module_get. It should also be the one the we use in module_put during vfio_platform_open().

Fixes 32a2d71c4e808 ("vfio: platform: introduce vfio-platform-base module")
---
 drivers/vfio/platform/vfio_platform_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/platform/vfio_platform_common.c +0 -0
index 361e5b57e369..470fcf7dac56 100644

drivers/vfio/platform/vfio_platform_common.c static int vfio_platform_open(struct vfio_device *core_vdev) · line 297

Quoted 8 lines
drivers/vfio/platform/vfio_platform_common.c +1 -1
@@ -291,7 +291,7 @@ static int vfio_platform_open(struct vfio_device *core_vdev)
 	vfio_platform_regions_cleanup(vdev);
 err_reg:
 	mutex_unlock(&driver_lock);
-	module_put(THIS_MODULE);
+	module_put(vdev->parent_module);
 	return ret;
 }
 

The series looks good to me. This one is an obvious fix, so I'll queue that for v5.13 and save the latter two for the v5.14 merge window. Please do make use of cover letters in the future. Thanks,

Alex

#6 lore raw
Quoted 13 lines · Alex Williamson
Quoted 10 lines · On Tue, 18 May 2021 22:21:31 +0300 Max Gurtovoy <[email protected]> wrote:

The ->parent_module is the one that use in try_module_get. It should also be the one the we use in module_put during vfio_platform_open().

Fixes 32a2d71c4e808 ("vfio: platform: introduce vfio-platform-base module")
---
  drivers/vfio/platform/vfio_platform_common.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

drivers/vfio/platform/vfio_platform_common.c static int vfio_platform_open(struct vfio_device *core_vdev) · line 297

Quoted 13 lines
drivers/vfio/platform/vfio_platform_common.c +1 -1
index 361e5b57e369..470fcf7dac56 100644
@@ -291,7 +291,7 @@ static int vfio_platform_open(struct vfio_device *core_vdev)
  	vfio_platform_regions_cleanup(vdev);
  err_reg:
  	mutex_unlock(&driver_lock);
-	module_put(THIS_MODULE);
+	module_put(vdev->parent_module);
  	return ret;
  }
 

The series looks good to me. This one is an obvious fix, so I'll queue that for v5.13 and save the latter two for the v5.14 merge window. Please do make use of cover letters in the future. Thanks,

Thanks Alex.

I'll keep that in mind.

Quoted 3 lines

Alex

maintainer Acked-by on v1 1/3
#7 lore raw
In reply to Max Gurtovoy

Hi Max,

Quoted 6 lines · Max Gurtovoy

The ->parent_module is the one that use in try_module_get. It should also be the one the we use in module_put during vfio_platform_open().

Fixes 32a2d71c4e808 ("vfio: platform: introduce vfio-platform-base module")

Thanks!

Eric

Quoted 18 lines
---
 drivers/vfio/platform/vfio_platform_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/platform/vfio_platform_common.c +1 -1
index 361e5b57e369..470fcf7dac56 100644
@@ -291,7 +291,7 @@ static int vfio_platform_open(struct vfio_device *core_vdev)
 	vfio_platform_regions_cleanup(vdev);
 err_reg:
 	mutex_unlock(&driver_lock);
-	module_put(THIS_MODULE);
+	module_put(vdev->parent_module);
 	return ret;
 }