xref: /openbmc/linux/drivers/gpu/drm/drm_managed.c (revision 82df5b73)
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 /**
18  * DOC: managed resources
19  *
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.
23  *
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().
29  *
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.
34  */
35 
36 struct drmres_node {
37 	struct list_head	entry;
38 	drmres_release_t	release;
39 	const char		*name;
40 	size_t			size;
41 };
42 
43 struct drmres {
44 	struct drmres_node		node;
45 	/*
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().
51 	 */
52 	u8 __aligned(ARCH_KMALLOC_MINALIGN) data[];
53 };
54 
55 static void free_dr(struct drmres *dr)
56 {
57 	kfree_const(dr->node.name);
58 	kfree(dr);
59 }
60 
61 void drm_managed_release(struct drm_device *dev)
62 {
63 	struct drmres *dr, *tmp;
64 
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);
69 
70 		if (dr->node.release)
71 			dr->node.release(dev, dr->node.size ? *(void **)&dr->data : NULL);
72 
73 		list_del(&dr->node.entry);
74 		free_dr(dr);
75 	}
76 	drm_dbg_drmres(dev, "drmres release end\n");
77 }
78 
79 /*
80  * Always inline so that kmalloc_track_caller tracks the actual interesting
81  * caller outside of drm_managed.c.
82  */
83 static __always_inline struct drmres * alloc_dr(drmres_release_t release,
84 						size_t size, gfp_t gfp, int nid)
85 {
86 	size_t tot_size;
87 	struct drmres *dr;
88 
89 	/* We must catch any near-SIZE_MAX cases that could overflow. */
90 	if (unlikely(check_add_overflow(sizeof(*dr), size, &tot_size)))
91 		return NULL;
92 
93 	dr = kmalloc_node_track_caller(tot_size, gfp, nid);
94 	if (unlikely(!dr))
95 		return NULL;
96 
97 	memset(dr, 0, offsetof(struct drmres, data));
98 
99 	INIT_LIST_HEAD(&dr->node.entry);
100 	dr->node.release = release;
101 	dr->node.size = size;
102 
103 	return dr;
104 }
105 
106 static void del_dr(struct drm_device *dev, struct drmres *dr)
107 {
108 	list_del_init(&dr->node.entry);
109 
110 	drm_dbg_drmres(dev, "DEL %p %s (%lu bytes)\n",
111 		       dr, dr->node.name, (unsigned long) dr->node.size);
112 }
113 
114 static void add_dr(struct drm_device *dev, struct drmres *dr)
115 {
116 	unsigned long flags;
117 
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);
121 
122 	drm_dbg_drmres(dev, "ADD %p %s (%lu bytes)\n",
123 		       dr, dr->node.name, (unsigned long) dr->node.size);
124 }
125 
126 /**
127  * drmm_add_final_kfree - add release action for the final kfree()
128  * @dev: DRM device
129  * @container: pointer to the kmalloc allocation containing @dev
130  *
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.
137  */
138 void drmm_add_final_kfree(struct drm_device *dev, void *container)
139 {
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;
144 }
145 EXPORT_SYMBOL(drmm_add_final_kfree);
146 
147 int __drmm_add_action(struct drm_device *dev,
148 		      drmres_release_t action,
149 		      void *data, const char *name)
150 {
151 	struct drmres *dr;
152 	void **void_ptr;
153 
154 	dr = alloc_dr(action, data ? sizeof(void*) : 0,
155 		      GFP_KERNEL | __GFP_ZERO,
156 		      dev_to_node(dev->dev));
157 	if (!dr) {
158 		drm_dbg_drmres(dev, "failed to add action %s for %p\n",
159 			       name, data);
160 		return -ENOMEM;
161 	}
162 
163 	dr->node.name = kstrdup_const(name, GFP_KERNEL);
164 	if (data) {
165 		void_ptr = (void **)&dr->data;
166 		*void_ptr = data;
167 	}
168 
169 	add_dr(dev, dr);
170 
171 	return 0;
172 }
173 EXPORT_SYMBOL(__drmm_add_action);
174 
175 int __drmm_add_action_or_reset(struct drm_device *dev,
176 			       drmres_release_t action,
177 			       void *data, const char *name)
178 {
179 	int ret;
180 
181 	ret = __drmm_add_action(dev, action, data, name);
182 	if (ret)
183 		action(dev, data);
184 
185 	return ret;
186 }
187 EXPORT_SYMBOL(__drmm_add_action_or_reset);
188 
189 /**
190  * drmm_kmalloc - &drm_device managed kmalloc()
191  * @dev: DRM device
192  * @size: size of the memory allocation
193  * @gfp: GFP allocation flags
194  *
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().
198  */
199 void *drmm_kmalloc(struct drm_device *dev, size_t size, gfp_t gfp)
200 {
201 	struct drmres *dr;
202 
203 	dr = alloc_dr(NULL, size, gfp, dev_to_node(dev->dev));
204 	if (!dr) {
205 		drm_dbg_drmres(dev, "failed to allocate %zu bytes, %u flags\n",
206 			       size, gfp);
207 		return NULL;
208 	}
209 	dr->node.name = kstrdup_const("kmalloc", GFP_KERNEL);
210 
211 	add_dr(dev, dr);
212 
213 	return dr->data;
214 }
215 EXPORT_SYMBOL(drmm_kmalloc);
216 
217 /**
218  * drmm_kstrdup - &drm_device managed kstrdup()
219  * @dev: DRM device
220  * @s: 0-terminated string to be duplicated
221  * @gfp: GFP allocation flags
222  *
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().
226  */
227 char *drmm_kstrdup(struct drm_device *dev, const char *s, gfp_t gfp)
228 {
229 	size_t size;
230 	char *buf;
231 
232 	if (!s)
233 		return NULL;
234 
235 	size = strlen(s) + 1;
236 	buf = drmm_kmalloc(dev, size, gfp);
237 	if (buf)
238 		memcpy(buf, s, size);
239 	return buf;
240 }
241 EXPORT_SYMBOL_GPL(drmm_kstrdup);
242 
243 /**
244  * drmm_kfree - &drm_device managed kfree()
245  * @dev: DRM device
246  * @data: memory allocation to be freed
247  *
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.
251  */
252 void drmm_kfree(struct drm_device *dev, void *data)
253 {
254 	struct drmres *dr_match = NULL, *dr;
255 	unsigned long flags;
256 
257 	if (!data)
258 		return;
259 
260 	spin_lock_irqsave(&dev->managed.lock, flags);
261 	list_for_each_entry(dr, &dev->managed.resources, node.entry) {
262 		if (dr->data == data) {
263 			dr_match = dr;
264 			del_dr(dev, dr_match);
265 			break;
266 		}
267 	}
268 	spin_unlock_irqrestore(&dev->managed.lock, flags);
269 
270 	if (WARN_ON(!dr_match))
271 		return;
272 
273 	free_dr(dr_match);
274 }
275 EXPORT_SYMBOL(drmm_kfree);
276