^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2020 Intel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Based on drivers/base/devres.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <drm/drm_managed.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <drm/drm_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <drm/drm_print.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "drm_internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * DOC: managed resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Inspired by struct &device managed resources, but tied to the lifetime of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * struct &drm_device, which can outlive the underlying physical device, usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * when userspace has some open files and other handles to resources still open.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Release actions can be added with drmm_add_action(), memory allocations can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * be done directly with drmm_kmalloc() and the related functions. Everything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * will be released on the final drm_dev_put() in reverse order of how the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * release actions have been added and memory has been allocated since driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * loading started with devm_drm_dev_alloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Note that release actions and managed memory can also be added and removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * during the lifetime of the driver, all the functions are fully concurrent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * safe. But it is recommended to use managed resources only for resources that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * change rarely, if ever, during the lifetime of the &drm_device instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct drmres_node {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct list_head entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) drmres_release_t release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct drmres {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct drmres_node node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Some archs want to perform DMA into kmalloc caches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * and need a guaranteed alignment larger than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * the alignment of a 64-bit integer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * buffer alignment as if it was allocated by plain kmalloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static void free_dr(struct drmres *dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) kfree_const(dr->node.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) kfree(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) void drm_managed_release(struct drm_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct drmres *dr, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) drm_dbg_drmres(dev, "drmres release begin\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) list_for_each_entry_safe(dr, tmp, &dev->managed.resources, node.entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) drm_dbg_drmres(dev, "REL %p %s (%zu bytes)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dr, dr->node.name, dr->node.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (dr->node.release)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dr->node.release(dev, dr->node.size ? *(void **)&dr->data : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) list_del(&dr->node.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) free_dr(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) drm_dbg_drmres(dev, "drmres release end\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Always inline so that kmalloc_track_caller tracks the actual interesting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * caller outside of drm_managed.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static __always_inline struct drmres * alloc_dr(drmres_release_t release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) size_t size, gfp_t gfp, int nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) size_t tot_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct drmres *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* We must catch any near-SIZE_MAX cases that could overflow. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (unlikely(check_add_overflow(sizeof(*dr), size, &tot_size)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) dr = kmalloc_node_track_caller(tot_size, gfp, nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (unlikely(!dr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) memset(dr, 0, offsetof(struct drmres, data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) INIT_LIST_HEAD(&dr->node.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) dr->node.release = release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) dr->node.size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static void del_dr(struct drm_device *dev, struct drmres *dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) list_del_init(&dr->node.entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) drm_dbg_drmres(dev, "DEL %p %s (%lu bytes)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) dr, dr->node.name, (unsigned long) dr->node.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void add_dr(struct drm_device *dev, struct drmres *dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) spin_lock_irqsave(&dev->managed.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) list_add(&dr->node.entry, &dev->managed.resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) spin_unlock_irqrestore(&dev->managed.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) drm_dbg_drmres(dev, "ADD %p %s (%lu bytes)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) dr, dr->node.name, (unsigned long) dr->node.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) void drmm_add_final_kfree(struct drm_device *dev, void *container)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) WARN_ON(dev->managed.final_kfree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) WARN_ON(dev < (struct drm_device *) container);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) dev->managed.final_kfree = container;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int __drmm_add_action(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) drmres_release_t action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void *data, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct drmres *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void **void_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) dr = alloc_dr(action, data ? sizeof(void*) : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) GFP_KERNEL | __GFP_ZERO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) dev_to_node(dev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (!dr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) drm_dbg_drmres(dev, "failed to add action %s for %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) name, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) dr->node.name = kstrdup_const(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) void_ptr = (void **)&dr->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *void_ptr = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) add_dr(dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) EXPORT_SYMBOL(__drmm_add_action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int __drmm_add_action_or_reset(struct drm_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) drmres_release_t action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) void *data, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = __drmm_add_action(dev, action, data, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) action(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) EXPORT_SYMBOL(__drmm_add_action_or_reset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * drmm_kmalloc - &drm_device managed kmalloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * @size: size of the memory allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * @gfp: GFP allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * This is a &drm_device managed version of kmalloc(). The allocated memory is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * automatically freed on the final drm_dev_put(). Memory can also be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * before the final drm_dev_put() by calling drmm_kfree().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct drmres *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) dr = alloc_dr(NULL, size, gfp, dev_to_node(dev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (!dr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) drm_dbg_drmres(dev, "failed to allocate %zu bytes, %u flags\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) size, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) dr->node.name = kstrdup_const("kmalloc", GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) add_dr(dev, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return dr->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) EXPORT_SYMBOL(drmm_kmalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * drmm_kstrdup - &drm_device managed kstrdup()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * @s: 0-terminated string to be duplicated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * @gfp: GFP allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * This is a &drm_device managed version of kstrdup(). The allocated memory is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * automatically freed on the final drm_dev_put() and works exactly like a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * memory allocation obtained by drmm_kmalloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) size = strlen(s) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) buf = drmm_kmalloc(dev, size, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) memcpy(buf, s, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) EXPORT_SYMBOL_GPL(drmm_kstrdup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * drmm_kfree - &drm_device managed kfree()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * @dev: DRM device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * @data: memory allocation to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * This is a &drm_device managed version of kfree() which can be used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * release memory allocated through drmm_kmalloc() or any of its related
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * functions before the final drm_dev_put() of @dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) void drmm_kfree(struct drm_device *dev, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct drmres *dr_match = NULL, *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) spin_lock_irqsave(&dev->managed.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) list_for_each_entry(dr, &dev->managed.resources, node.entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (dr->data == data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) dr_match = dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) del_dr(dev, dr_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) spin_unlock_irqrestore(&dev->managed.lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (WARN_ON(!dr_match))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) free_dr(dr_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) EXPORT_SYMBOL(drmm_kfree);