Skip to contentSkip to conversation

Balance mutex_init and mutex_destroy calls

PATCH Superseded Maíra Canal 2 participants 5 messages Nov 7, 2022 – Nov 8, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Maíra Canal

author maintainer
#0 lore raw

cover Balance mutex_init and mutex_destroy calls

This series introduces some changes to assure the correct resource release on the V3D driver, especially the mutex. Currently, the V3D has no mutex_destroy() calls, which means that a mutex is being instantiated, but it is not being released by the end of its use.

So, use the DRM-managed mutex_init variants when possible to manage the mutex release and add mutex_destroy() calls when not possible.

Best Regards,
- Maíra Canal

Maíra Canal (2):
drm/v3d: switch to drmm_mutex_init
drm/v3d: add missing mutex_destroy

 drivers/gpu/drm/v3d/v3d_gem.c     | 17 +++++++++++++----
 drivers/gpu/drm/v3d/v3d_perfmon.c |  2 ++
 2 files changed, 15 insertions(+), 4 deletions(-)
Signature · 1 line
2.38.1
author maintainer
#1 lore raw

PATCH 1/2 drm/v3d: switch to drmm_mutex_init

mutex_init() is supposed to be balanced by a call to mutex_destroy(), but this is not currently happening on the v3d driver.

Considering the introduction of a DRM-managed mutex_init variant, switch to drmm_mutex_init.

---
 drivers/gpu/drm/v3d/v3d_gem.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/v3d/v3d_gem.c +13 -4
index b8980440d137..96af1cb5202a 100644
@@ -10,6 +10,7 @@
 #include <linux/sched/signal.h>
 #include <linux/uaccess.h>
 
+#include <drm/drm_managed.h>
 #include <drm/drm_syncobj.h>
 #include <uapi/drm/v3d_drm.h>
 
@@ -1075,10 +1076,18 @@ v3d_gem_init(struct drm_device *dev)
 
 	spin_lock_init(&v3d->mm_lock);
 	spin_lock_init(&v3d->job_lock);
-	mutex_init(&v3d->bo_lock);
-	mutex_init(&v3d->reset_lock);
-	mutex_init(&v3d->sched_lock);
-	mutex_init(&v3d->cache_clean_lock);
+	ret = drmm_mutex_init(dev, &v3d->bo_lock);
+	if (ret)
+		return ret;
+	ret = drmm_mutex_init(dev, &v3d->reset_lock);
+	if (ret)
+		return ret;
+	ret = drmm_mutex_init(dev, &v3d->sched_lock);
+	if (ret)
+		return ret;
+	ret = drmm_mutex_init(dev, &v3d->cache_clean_lock);
+	if (ret)
+		return ret;
 
 	/* Note: We don't allocate address 0.  Various bits of HW
 	 * treat 0 as special, such as the occlusion query counters
Signature · 1 line
2.38.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] drm/v3d: switch to drmm_mutex_init

Quoted 7 lines · Maíra Canal

mutex_init() is supposed to be balanced by a call to mutex_destroy(), but this is not currently happening on the v3d driver.

Considering the introduction of a DRM-managed mutex_init variant, switch to drmm_mutex_init.

Quoted 42 lines
---
 drivers/gpu/drm/v3d/v3d_gem.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
drivers/gpu/drm/v3d/v3d_gem.c +13 -4
index b8980440d137..96af1cb5202a 100644
@@ -10,6 +10,7 @@
 #include <linux/sched/signal.h>
 #include <linux/uaccess.h>
 
+#include <drm/drm_managed.h>
 #include <drm/drm_syncobj.h>
 #include <uapi/drm/v3d_drm.h>
 
@@ -1075,10 +1076,18 @@ v3d_gem_init(struct drm_device *dev)
 
 	spin_lock_init(&v3d->mm_lock);
 	spin_lock_init(&v3d->job_lock);
-	mutex_init(&v3d->bo_lock);
-	mutex_init(&v3d->reset_lock);
-	mutex_init(&v3d->sched_lock);
-	mutex_init(&v3d->cache_clean_lock);
+	ret = drmm_mutex_init(dev, &v3d->bo_lock);
+	if (ret)
+		return ret;
+	ret = drmm_mutex_init(dev, &v3d->reset_lock);
+	if (ret)
+		return ret;
+	ret = drmm_mutex_init(dev, &v3d->sched_lock);
+	if (ret)
+		return ret;
+	ret = drmm_mutex_init(dev, &v3d->cache_clean_lock);
+	if (ret)
+		return ret;
 
 	/* Note: We don't allocate address 0.  Various bits of HW
 	 * treat 0 as special, such as the occlusion query counters
Signature · 1 line
2.38.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
author maintainer
#3 lore raw

PATCH 2/2 drm/v3d: add missing mutex_destroy

In reply to Maíra Canal

v3d_perfmon_open_file() instantiates a mutex for a particular file instance, but it never destroys it by calling mutex_destroy() in v3d_perfmon_close_file().

Similarly, v3d_perfmon_create_ioctl() instantiates a mutex for a particular perfmon, but it never destroys it by calling mutex_destroy() in v3d_perfmon_destroy_ioctl().

So, add the missing mutex_destroy on both cases.

---
 drivers/gpu/drm/v3d/v3d_perfmon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/v3d/v3d_perfmon.c +2 -0
index 48aaaa972c49..292c73544255 100644
@@ -113,6 +113,7 @@ void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv)
 	idr_for_each(&v3d_priv->perfmon.idr, v3d_perfmon_idr_del, NULL);
 	idr_destroy(&v3d_priv->perfmon.idr);
 	mutex_unlock(&v3d_priv->perfmon.lock);
+	mutex_destroy(&v3d_priv->perfmon.lock);
 }
 
 int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,
@@ -177,6 +178,7 @@ int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
 	if (!perfmon)
 		return -EINVAL;
 
+	mutex_destroy(&perfmon->lock);
 	v3d_perfmon_put(perfmon);
 
 	return 0;
Signature · 1 line
2.38.1
#4 lore raw

Re: [PATCH 2/2] drm/v3d: add missing mutex_destroy

Quoted 28 lines · Maíra Canal

v3d_perfmon_open_file() instantiates a mutex for a particular file instance, but it never destroys it by calling mutex_destroy() in v3d_perfmon_close_file().

Similarly, v3d_perfmon_create_ioctl() instantiates a mutex for a particular perfmon, but it never destroys it by calling mutex_destroy() in v3d_perfmon_destroy_ioctl().

So, add the missing mutex_destroy on both cases.

---
 drivers/gpu/drm/v3d/v3d_perfmon.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/v3d/v3d_perfmon.c +1 -0
index 48aaaa972c49..292c73544255 100644
@@ -113,6 +113,7 @@ void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv)
 	idr_for_each(&v3d_priv->perfmon.idr, v3d_perfmon_idr_del, NULL);
 	idr_destroy(&v3d_priv->perfmon.idr);
 	mutex_unlock(&v3d_priv->perfmon.lock);
+	mutex_destroy(&v3d_priv->perfmon.lock);
 }
 
 int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,

drivers/gpu/drm/v3d/v3d_perfmon.c int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data, · line 181

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +1 -0
@@ -177,6 +178,7 @@ int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
 	if (!perfmon)
 		return -EINVAL;
 
+	mutex_destroy(&perfmon->lock);

This one looks wrong, I think this should be put into v3d_perfmon_put. There is also an error case in v3d_perfmon_create_ioctl() that I think you missed, right above the kfree(perfmon).

With these two fixes this is Reviewed-by: Daniel Vetter <[email protected]>

Quoted 6 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +0 -0
 	v3d_perfmon_put(perfmon);
 
 	return 0;
Signature · 1 line
2.38.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch