linux-stable/include/drm/drm_managed.h
Matthew Auld c21f11d182 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: e13f13e039 ("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
2023-05-22 12:23:50 +02:00

126 lines
3.9 KiB
C

// SPDX-License-Identifier: GPL-2.0
#ifndef _DRM_MANAGED_H_
#define _DRM_MANAGED_H_
#include <linux/gfp.h>
#include <linux/overflow.h>
#include <linux/types.h>
struct drm_device;
struct mutex;
typedef void (*drmres_release_t)(struct drm_device *dev, void *res);
/**
* drmm_add_action - add a managed release action to a &drm_device
* @dev: DRM device
* @action: function which should be called when @dev is released
* @data: opaque pointer, passed to @action
*
* This function adds the @release action with optional parameter @data to the
* list of cleanup actions for @dev. The cleanup actions will be run in reverse
* order in the final drm_dev_put() call for @dev.
*/
#define drmm_add_action(dev, action, data) \
__drmm_add_action(dev, action, data, #action)
int __must_check __drmm_add_action(struct drm_device *dev,
drmres_release_t action,
void *data, const char *name);
/**
* drmm_add_action_or_reset - add a managed release action to a &drm_device
* @dev: DRM device
* @action: function which should be called when @dev is released
* @data: opaque pointer, passed to @action
*
* Similar to drmm_add_action(), with the only difference that upon failure
* @action is directly called for any cleanup work necessary on failures.
*/
#define drmm_add_action_or_reset(dev, action, data) \
__drmm_add_action_or_reset(dev, action, data, #action)
int __must_check __drmm_add_action_or_reset(struct drm_device *dev,
drmres_release_t action,
void *data, const char *name);
void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp) __malloc;
/**
* drmm_kzalloc - &drm_device managed kzalloc()
* @dev: DRM device
* @size: size of the memory allocation
* @gfp: GFP allocation flags
*
* This is a &drm_device managed version of kzalloc(). The allocated memory is
* automatically freed on the final drm_dev_put(). Memory can also be freed
* before the final drm_dev_put() by calling drmm_kfree().
*/
static inline void *drmm_kzalloc(struct drm_device *dev, size_t size, gfp_t gfp)
{
return drmm_kmalloc(dev, size, gfp | __GFP_ZERO);
}
/**
* drmm_kmalloc_array - &drm_device managed kmalloc_array()
* @dev: DRM device
* @n: number of array elements to allocate
* @size: size of array member
* @flags: GFP allocation flags
*
* This is a &drm_device managed version of kmalloc_array(). The allocated
* memory is automatically freed on the final drm_dev_put() and works exactly
* like a memory allocation obtained by drmm_kmalloc().
*/
static inline void *drmm_kmalloc_array(struct drm_device *dev,
size_t n, size_t size, gfp_t flags)
{
size_t bytes;
if (unlikely(check_mul_overflow(n, size, &bytes)))
return NULL;
return drmm_kmalloc(dev, bytes, flags);
}
/**
* drmm_kcalloc - &drm_device managed kcalloc()
* @dev: DRM device
* @n: number of array elements to allocate
* @size: size of array member
* @flags: GFP allocation flags
*
* This is a &drm_device managed version of kcalloc(). The allocated memory is
* automatically freed on the final drm_dev_put() and works exactly like a
* memory allocation obtained by drmm_kmalloc().
*/
static inline void *drmm_kcalloc(struct drm_device *dev,
size_t n, size_t size, gfp_t flags)
{
return drmm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
}
char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp);
void drmm_kfree(struct drm_device *dev, void *data);
void __drmm_mutex_release(struct drm_device *dev, void *res);
/**
* drmm_mutex_init - &drm_device-managed mutex_init()
* @dev: DRM device
* @lock: lock to be initialized
*
* Returns:
* 0 on success, or a negative errno code otherwise.
*
* This is a &drm_device-managed version of mutex_init(). The initialized
* lock is automatically destroyed on the final drm_dev_put().
*/
#define drmm_mutex_init(dev, lock) ({ \
mutex_init(lock); \
drmm_add_action_or_reset(dev, __drmm_mutex_release, lock); \
}) \
#endif