From: Biren Pandya <birenpandya@gmail.com>
To: maarten.lankhorst@linux.intel.com, mripard@kernel.org,
tzimmermann@suse.de, airlied@gmail.com, simona@ffwll.ch
Cc: sumit.semwal@linaro.org, christian.koenig@amd.com,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org,
Biren Pandya <birenpandya@gmail.com>
Subject: [PATCH] drm/gem: modernize locks to use scoped_guard()
Date: Tue, 16 Jun 2026 23:49:57 +0530 [thread overview]
Message-ID: <20260616181956.61476-2-birenpandya@gmail.com> (raw)
Several GEM core functions manually managed mutex_lock() and
mutex_unlock() over single scopes or error paths. This adds boilerplate
and carries the risk of lock leaks if error paths are refactored.
Modernize these locks by deploying the <linux/cleanup.h> scoped_guard()
macro. This ensures that the locks are reliably dropped when the block
exits, cleanly removing goto out_unlock paths and tightening the
lifecycle.
Signed-off-by: Biren Pandya <birenpandya@gmail.com>
Compiled locally, but requires IGT validation by the DRM CI.
---
drivers/gpu/drm/drm_gem.c | 66 ++++++++++++++++++---------------------
1 file changed, 30 insertions(+), 36 deletions(-)
diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
index 891c3bff5ae0..d3a061d42ba7 100644
--- a/drivers/gpu/drm/drm_gem.c
+++ b/drivers/gpu/drm/drm_gem.c
@@ -346,13 +346,13 @@ void drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
* we checked for a name.
*/
- mutex_lock(&dev->object_name_lock);
- if (--obj->handle_count == 0) {
- drm_gem_object_handle_free(obj);
- drm_gem_object_exported_dma_buf_free(obj);
- final = true;
+ scoped_guard(mutex, &dev->object_name_lock) {
+ if (--obj->handle_count == 0) {
+ drm_gem_object_handle_free(obj);
+ drm_gem_object_exported_dma_buf_free(obj);
+ final = true;
+ }
}
- mutex_unlock(&dev->object_name_lock);
if (final)
drm_gem_object_put(obj);
@@ -374,11 +374,8 @@ drm_gem_object_release_handle(int id, void *ptr, void *data)
if (obj->funcs->close)
obj->funcs->close(obj, file_priv);
- mutex_lock(&file_priv->prime.lock);
-
- drm_prime_remove_buf_handle(&file_priv->prime, id);
-
- mutex_unlock(&file_priv->prime.lock);
+ scoped_guard(mutex, &file_priv->prime.lock)
+ drm_prime_remove_buf_handle(&file_priv->prime, id);
drm_vma_node_revoke(&obj->vma_node, file_priv);
@@ -1021,37 +1018,34 @@ int drm_gem_change_handle_ioctl(struct drm_device *dev, void *data,
goto out;
}
- mutex_lock(&file_priv->prime.lock);
+ scoped_guard(mutex, &file_priv->prime.lock) {
+ spin_lock(&file_priv->table_lock);
+ ret = idr_alloc(&file_priv->object_idr, obj, handle, handle + 1,
+ GFP_NOWAIT);
+ spin_unlock(&file_priv->table_lock);
- spin_lock(&file_priv->table_lock);
- ret = idr_alloc(&file_priv->object_idr, obj, handle, handle + 1,
- GFP_NOWAIT);
- spin_unlock(&file_priv->table_lock);
+ if (ret < 0)
+ break;
- if (ret < 0)
- goto out_unlock;
+ if (obj->dma_buf) {
+ ret = drm_prime_add_buf_handle(&file_priv->prime, obj->dma_buf,
+ handle);
+ if (ret < 0) {
+ spin_lock(&file_priv->table_lock);
+ idr_remove(&file_priv->object_idr, handle);
+ spin_unlock(&file_priv->table_lock);
+ break;
+ }
- if (obj->dma_buf) {
- ret = drm_prime_add_buf_handle(&file_priv->prime, obj->dma_buf,
- handle);
- if (ret < 0) {
- spin_lock(&file_priv->table_lock);
- idr_remove(&file_priv->object_idr, handle);
- spin_unlock(&file_priv->table_lock);
- goto out_unlock;
+ drm_prime_remove_buf_handle(&file_priv->prime, args->handle);
}
- drm_prime_remove_buf_handle(&file_priv->prime, args->handle);
- }
-
- ret = 0;
-
- spin_lock(&file_priv->table_lock);
- idr_remove(&file_priv->object_idr, args->handle);
- spin_unlock(&file_priv->table_lock);
+ ret = 0;
-out_unlock:
- mutex_unlock(&file_priv->prime.lock);
+ spin_lock(&file_priv->table_lock);
+ idr_remove(&file_priv->object_idr, args->handle);
+ spin_unlock(&file_priv->table_lock);
+ }
out:
drm_gem_object_put(obj);
base-commit: 72c25183cac9bc584c9de21797a5883af44bcc7a
--
2.50.1 (Apple Git-155)
next reply other threads:[~2026-06-16 18:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-16 18:19 Biren Pandya [this message]
2026-06-22 11:55 ` [PATCH] drm/gem: modernize locks to use scoped_guard() Laurent Pinchart
2026-06-22 12:52 ` biren pandya
2026-06-22 13:01 ` Laurent Pinchart
2026-06-22 13:39 ` Thomas Zimmermann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260616181956.61476-2-birenpandya@gmail.com \
--to=birenpandya@gmail.com \
--cc=airlied@gmail.com \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=simona@ffwll.ch \
--cc=sumit.semwal@linaro.org \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox