Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm: fix drmm_mutex_init()

In mutex_init() lockdep identifies a lock by defining a special static
key for each lock class. However if we wrap the macro in a function,
like in drmm_mutex_init(), we end up generating:

int drmm_mutex_init(struct drm_device *dev, struct mutex *lock)
{
static struct lock_class_key __key;

__mutex_init((lock), "lock", &__key);
....
}

The static __key here is what lockdep uses to identify the lock class,
however since this is just a normal function the key here will be
created once, where all callers then use the same key. In effect the
mutex->depmap.key will be the same pointer for different
drmm_mutex_init() callers. This then results in impossible lockdep
splats since lockdep thinks completely unrelated locks are the same lock
class.

To fix this turn drmm_mutex_init() into a macro such that it generates a
different "static struct lock_class_key __key" for each invocation,
which looks to be inline with what mutex_init() wants.

v2:
- Revamp the commit message with clearer explanation of the issue.
- Rather export __drmm_mutex_release() than static inline.

Reported-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reported-by: Sarah Walker <sarah.walker@imgtec.com>
Fixes: e13f13e039dc ("drm: Add DRM-managed mutex_init()")
Cc: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>
Cc: Boris Brezillon <boris.brezillon@collabora.com>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Cc: Jocelyn Falempe <jfalempe@redhat.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: dri-devel@lists.freedesktop.org
Signed-off-by: Matthew Auld <matthew.auld@intel.com>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Reviewed-by: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Acked-by: Thomas Zimmermann <tzimmermann@suse.de>
Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Link: https://patchwork.freedesktop.org/patch/msgid/20230519090733.489019-1-matthew.auld@intel.com

authored by

Matthew Auld and committed by
Thomas Zimmermann
c21f11d1 aa8bf931

+19 -21
+2 -20
drivers/gpu/drm/drm_managed.c
··· 264 264 } 265 265 EXPORT_SYMBOL(drmm_kfree); 266 266 267 - static void drmm_mutex_release(struct drm_device *dev, void *res) 267 + void __drmm_mutex_release(struct drm_device *dev, void *res) 268 268 { 269 269 struct mutex *lock = res; 270 270 271 271 mutex_destroy(lock); 272 272 } 273 - 274 - /** 275 - * drmm_mutex_init - &drm_device-managed mutex_init() 276 - * @dev: DRM device 277 - * @lock: lock to be initialized 278 - * 279 - * Returns: 280 - * 0 on success, or a negative errno code otherwise. 281 - * 282 - * This is a &drm_device-managed version of mutex_init(). The initialized 283 - * lock is automatically destroyed on the final drm_dev_put(). 284 - */ 285 - int drmm_mutex_init(struct drm_device *dev, struct mutex *lock) 286 - { 287 - mutex_init(lock); 288 - 289 - return drmm_add_action_or_reset(dev, drmm_mutex_release, lock); 290 - } 291 - EXPORT_SYMBOL(drmm_mutex_init); 273 + EXPORT_SYMBOL(__drmm_mutex_release);
+17 -1
include/drm/drm_managed.h
··· 105 105 106 106 void drmm_kfree(struct drm_device *dev, void *data); 107 107 108 - int drmm_mutex_init(struct drm_device *dev, struct mutex *lock); 108 + void __drmm_mutex_release(struct drm_device *dev, void *res); 109 + 110 + /** 111 + * drmm_mutex_init - &drm_device-managed mutex_init() 112 + * @dev: DRM device 113 + * @lock: lock to be initialized 114 + * 115 + * Returns: 116 + * 0 on success, or a negative errno code otherwise. 117 + * 118 + * This is a &drm_device-managed version of mutex_init(). The initialized 119 + * lock is automatically destroyed on the final drm_dev_put(). 120 + */ 121 + #define drmm_mutex_init(dev, lock) ({ \ 122 + mutex_init(lock); \ 123 + drmm_add_action_or_reset(dev, __drmm_mutex_release, lock); \ 124 + }) \ 109 125 110 126 #endif