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