1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Intel
5 * Based on drivers/base/devres.c
8 #include <drm/drm_managed.h>
10 #include <linux/list.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
14 #include <drm/drm_device.h>
15 #include <drm/drm_print.h>
18 * DOC: managed resources
20 * Inspired by struct &device managed resources, but tied to the lifetime of
21 * struct &drm_device, which can outlive the underlying physical device, usually
22 * when userspace has some open files and other handles to resources still open.
24 * Release actions can be added with drmm_add_action(), memory allocations can
25 * be done directly with drmm_kmalloc() and the related functions. Everything
26 * will be released on the final drm_dev_put() in reverse order of how the
27 * release actions have been added and memory has been allocated since driver
28 * loading started with drm_dev_init().
30 * Note that release actions and managed memory can also be added and removed
31 * during the lifetime of the driver, all the functions are fully concurrent
32 * safe. But it is recommended to use managed resources only for resources that
33 * change rarely, if ever, during the lifetime of the &drm_device instance.
37 struct list_head entry;
38 drmres_release_t release;
44 struct drmres_node node;
46 * Some archs want to perform DMA into kmalloc caches
47 * and need a guaranteed alignment larger than
48 * the alignment of a 64-bit integer.
49 * Thus we use ARCH_KMALLOC_MINALIGN here and get exactly the same
50 * buffer alignment as if it was allocated by plain kmalloc().
52 u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
55 static void free_dr(struct drmres *dr)
57 kfree_const(dr->node.name);
61 void drm_managed_release(struct drm_device *dev)
63 struct drmres *dr, *tmp;
65 drm_dbg_drmres(dev, "drmres release begin\n");
66 list_for_each_entry_safe(dr, tmp, &dev->managed.resources, node.entry) {
67 drm_dbg_drmres(dev, "REL %p %s (%zu bytes)\n",
68 dr, dr->node.name, dr->node.size);
71 dr->node.release(dev, dr->node.size ? *(void **)&dr->data : NULL);
73 list_del(&dr->node.entry);
76 drm_dbg_drmres(dev, "drmres release end\n");
80 * Always inline so that kmalloc_track_caller tracks the actual interesting
81 * caller outside of drm_managed.c.
83 static __always_inline struct drmres * alloc_dr(drmres_release_t release,
84 size_t size, gfp_t gfp, int nid)
89 /* We must catch any near-SIZE_MAX cases that could overflow. */
90 if (unlikely(check_add_overflow(sizeof(*dr), size, &tot_size)))
93 dr = kmalloc_node_track_caller(tot_size, gfp, nid);
97 memset(dr, 0, offsetof(struct drmres, data));
99 INIT_LIST_HEAD(&dr->node.entry);
100 dr->node.release = release;
101 dr->node.size = size;
106 static void del_dr(struct drm_device *dev, struct drmres *dr)
108 list_del_init(&dr->node.entry);
110 drm_dbg_drmres(dev, "DEL %p %s (%lu bytes)\n",
111 dr, dr->node.name, (unsigned long) dr->node.size);
114 static void add_dr(struct drm_device *dev, struct drmres *dr)
118 spin_lock_irqsave(&dev->managed.lock, flags);
119 list_add(&dr->node.entry, &dev->managed.resources);
120 spin_unlock_irqrestore(&dev->managed.lock, flags);
122 drm_dbg_drmres(dev, "ADD %p %s (%lu bytes)\n",
123 dr, dr->node.name, (unsigned long) dr->node.size);
127 * drmm_add_final_kfree - add release action for the final kfree()
129 * @container: pointer to the kmalloc allocation containing @dev
131 * Since the allocation containing the struct &drm_device must be allocated
132 * before it can be initialized with drm_dev_init() there's no way to allocate
133 * that memory with drmm_kmalloc(). To side-step this chicken-egg problem the
134 * pointer for this final kfree() must be specified by calling this function. It
135 * will be released in the final drm_dev_put() for @dev, after all other release
136 * actions installed through drmm_add_action() have been processed.
138 void drmm_add_final_kfree(struct drm_device *dev, void *container)
140 WARN_ON(dev->managed.final_kfree);
141 WARN_ON(dev < (struct drm_device *) container);
142 WARN_ON(dev + 1 > (struct drm_device *) (container + ksize(container)));
143 dev->managed.final_kfree = container;
145 EXPORT_SYMBOL(drmm_add_final_kfree);
147 int __drmm_add_action(struct drm_device *dev,
148 drmres_release_t action,
149 void *data, const char *name)
154 dr = alloc_dr(action, data ? sizeof(void*) : 0,
155 GFP_KERNEL | __GFP_ZERO,
156 dev_to_node(dev->dev));
158 drm_dbg_drmres(dev, "failed to add action %s for %p\n",
163 dr->node.name = kstrdup_const(name, GFP_KERNEL);
165 void_ptr = (void **)&dr->data;
173 EXPORT_SYMBOL(__drmm_add_action);
175 int __drmm_add_action_or_reset(struct drm_device *dev,
176 drmres_release_t action,
177 void *data, const char *name)
181 ret = __drmm_add_action(dev, action, data, name);
187 EXPORT_SYMBOL(__drmm_add_action_or_reset);
190 * drmm_kmalloc - &drm_device managed kmalloc()
192 * @size: size of the memory allocation
193 * @gfp: GFP allocation flags
195 * This is a &drm_device managed version of kmalloc(). The allocated memory is
196 * automatically freed on the final drm_dev_put(). Memory can also be freed
197 * before the final drm_dev_put() by calling drmm_kfree().
199 void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp)
203 dr = alloc_dr(NULL, size, gfp, dev_to_node(dev->dev));
205 drm_dbg_drmres(dev, "failed to allocate %zu bytes, %u flags\n",
209 dr->node.name = kstrdup_const("kmalloc", GFP_KERNEL);
215 EXPORT_SYMBOL(drmm_kmalloc);
218 * drmm_kstrdup - &drm_device managed kstrdup()
220 * @s: 0-terminated string to be duplicated
221 * @gfp: GFP allocation flags
223 * This is a &drm_device managed version of kstrdup(). The allocated memory is
224 * automatically freed on the final drm_dev_put() and works exactly like a
225 * memory allocation obtained by drmm_kmalloc().
227 char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp)
235 size = strlen(s) + 1;
236 buf = drmm_kmalloc(dev, size, gfp);
238 memcpy(buf, s, size);
241 EXPORT_SYMBOL_GPL(drmm_kstrdup);
244 * drmm_kfree - &drm_device managed kfree()
246 * @data: memory allocation to be freed
248 * This is a &drm_device managed version of kfree() which can be used to
249 * release memory allocated through drmm_kmalloc() or any of its related
250 * functions before the final drm_dev_put() of @dev.
252 void drmm_kfree(struct drm_device *dev, void *data)
254 struct drmres *dr_match = NULL, *dr;
260 spin_lock_irqsave(&dev->managed.lock, flags);
261 list_for_each_entry(dr, &dev->managed.resources, node.entry) {
262 if (dr->data == data) {
264 del_dr(dev, dr_match);
268 spin_unlock_irqrestore(&dev->managed.lock, flags);
270 if (WARN_ON(!dr_match))
275 EXPORT_SYMBOL(drmm_kfree);