xref: /linux/drivers/dma-buf/dma-heap.c (revision f78a0607c8bf6912943527175db61996436d5792)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Framework for userspace DMA-BUF allocations
4  *
5  * Copyright (C) 2011 Google, Inc.
6  * Copyright (C) 2019 Linaro Ltd.
7  */
8 
9 #include <linux/cdev.h>
10 #include <linux/device.h>
11 #include <linux/dma-buf.h>
12 #include <linux/dma-heap.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/list.h>
16 #include <linux/nospec.h>
17 #include <linux/syscalls.h>
18 #include <linux/uaccess.h>
19 #include <linux/xarray.h>
20 #include <uapi/linux/dma-heap.h>
21 
22 #define DEVNAME "dma_heap"
23 
24 #define NUM_HEAP_MINORS 128
25 
26 /**
27  * struct dma_heap - represents a dmabuf heap in the system
28  * @name:		used for debugging/device-node name
29  * @ops:		ops struct for this heap
30  * @priv:		private data for this heap
31  * @heap_devt:		heap device node
32  * @list:		list head connecting to list of heaps
33  * @heap_cdev:		heap char device
34  *
35  * Represents a heap of memory from which buffers can be made.
36  */
37 struct dma_heap {
38 	const char *name;
39 	const struct dma_heap_ops *ops;
40 	void *priv;
41 	dev_t heap_devt;
42 	struct list_head list;
43 	struct cdev heap_cdev;
44 };
45 
46 static LIST_HEAD(heap_list);
47 static DEFINE_MUTEX(heap_list_lock);
48 static dev_t dma_heap_devt;
49 static struct class *dma_heap_class;
50 static DEFINE_XARRAY_ALLOC(dma_heap_minors);
51 
52 bool __read_mostly mem_accounting;
53 module_param(mem_accounting, bool, 0444);
54 MODULE_PARM_DESC(mem_accounting,
55 		 "Enable cgroup-based memory accounting for dma-buf heap allocations (default=false).");
56 EXPORT_SYMBOL_NS_GPL(mem_accounting, "DMA_BUF_HEAP");
57 
58 static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
59 				 u32 fd_flags,
60 				 u64 heap_flags)
61 {
62 	struct dma_buf *dmabuf;
63 	int fd;
64 
65 	/*
66 	 * Allocations from all heaps have to begin
67 	 * and end on page boundaries.
68 	 */
69 	len = PAGE_ALIGN(len);
70 	if (!len)
71 		return -EINVAL;
72 
73 	dmabuf = heap->ops->allocate(heap, len, fd_flags, heap_flags);
74 	if (IS_ERR(dmabuf))
75 		return PTR_ERR(dmabuf);
76 
77 	fd = dma_buf_fd(dmabuf, fd_flags);
78 	if (fd < 0) {
79 		dma_buf_put(dmabuf);
80 		/* just return, as put will call release and that will free */
81 	}
82 	return fd;
83 }
84 
85 static int dma_heap_open(struct inode *inode, struct file *file)
86 {
87 	struct dma_heap *heap;
88 
89 	heap = xa_load(&dma_heap_minors, iminor(inode));
90 	if (!heap) {
91 		pr_err("dma_heap: minor %d unknown.\n", iminor(inode));
92 		return -ENODEV;
93 	}
94 
95 	/* instance data as context */
96 	file->private_data = heap;
97 	nonseekable_open(inode, file);
98 
99 	return 0;
100 }
101 
102 static long dma_heap_ioctl_allocate(struct file *file, void *data)
103 {
104 	struct dma_heap_allocation_data *heap_allocation = data;
105 	struct dma_heap *heap = file->private_data;
106 	int fd;
107 
108 	if (heap_allocation->fd)
109 		return -EINVAL;
110 
111 	if (heap_allocation->fd_flags & ~DMA_HEAP_VALID_FD_FLAGS)
112 		return -EINVAL;
113 
114 	if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
115 		return -EINVAL;
116 
117 	fd = dma_heap_buffer_alloc(heap, heap_allocation->len,
118 				   heap_allocation->fd_flags,
119 				   heap_allocation->heap_flags);
120 	if (fd < 0)
121 		return fd;
122 
123 	heap_allocation->fd = fd;
124 
125 	return 0;
126 }
127 
128 static unsigned int dma_heap_ioctl_cmds[] = {
129 	DMA_HEAP_IOCTL_ALLOC,
130 };
131 
132 static long dma_heap_ioctl(struct file *file, unsigned int ucmd,
133 			   unsigned long arg)
134 {
135 	char stack_kdata[128];
136 	char *kdata = stack_kdata;
137 	unsigned int kcmd;
138 	unsigned int in_size, out_size, drv_size, ksize;
139 	int nr = _IOC_NR(ucmd);
140 	int ret = 0;
141 
142 	if (nr >= ARRAY_SIZE(dma_heap_ioctl_cmds))
143 		return -EINVAL;
144 
145 	nr = array_index_nospec(nr, ARRAY_SIZE(dma_heap_ioctl_cmds));
146 	/* Get the kernel ioctl cmd that matches */
147 	kcmd = dma_heap_ioctl_cmds[nr];
148 
149 	/* Figure out the delta between user cmd size and kernel cmd size */
150 	drv_size = _IOC_SIZE(kcmd);
151 	out_size = _IOC_SIZE(ucmd);
152 	in_size = out_size;
153 	if ((ucmd & kcmd & IOC_IN) == 0)
154 		in_size = 0;
155 	if ((ucmd & kcmd & IOC_OUT) == 0)
156 		out_size = 0;
157 	ksize = max(max(in_size, out_size), drv_size);
158 
159 	/* If necessary, allocate buffer for ioctl argument */
160 	if (ksize > sizeof(stack_kdata)) {
161 		kdata = kmalloc(ksize, GFP_KERNEL);
162 		if (!kdata)
163 			return -ENOMEM;
164 	}
165 
166 	if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
167 		ret = -EFAULT;
168 		goto err;
169 	}
170 
171 	/* zero out any difference between the kernel/user structure size */
172 	if (ksize > in_size)
173 		memset(kdata + in_size, 0, ksize - in_size);
174 
175 	switch (kcmd) {
176 	case DMA_HEAP_IOCTL_ALLOC:
177 		ret = dma_heap_ioctl_allocate(file, kdata);
178 		break;
179 	default:
180 		ret = -ENOTTY;
181 		goto err;
182 	}
183 
184 	if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
185 		ret = -EFAULT;
186 err:
187 	if (kdata != stack_kdata)
188 		kfree(kdata);
189 	return ret;
190 }
191 
192 static const struct file_operations dma_heap_fops = {
193 	.owner          = THIS_MODULE,
194 	.open		= dma_heap_open,
195 	.unlocked_ioctl = dma_heap_ioctl,
196 #ifdef CONFIG_COMPAT
197 	.compat_ioctl	= dma_heap_ioctl,
198 #endif
199 };
200 
201 /**
202  * dma_heap_get_drvdata - get per-heap driver data
203  * @heap: DMA-Heap to retrieve private data for
204  *
205  * Returns:
206  * The per-heap data for the heap.
207  */
208 void *dma_heap_get_drvdata(struct dma_heap *heap)
209 {
210 	return heap->priv;
211 }
212 EXPORT_SYMBOL_NS_GPL(dma_heap_get_drvdata, "DMA_BUF_HEAP");
213 
214 /**
215  * dma_heap_get_name - get heap name
216  * @heap: DMA-Heap to retrieve the name of
217  *
218  * Returns:
219  * The char* for the heap name.
220  */
221 const char *dma_heap_get_name(struct dma_heap *heap)
222 {
223 	return heap->name;
224 }
225 EXPORT_SYMBOL_NS_GPL(dma_heap_get_name, "DMA_BUF_HEAP");
226 
227 /**
228  * dma_heap_add - adds a heap to dmabuf heaps
229  * @exp_info: information needed to register this heap
230  */
231 struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
232 {
233 	struct dma_heap *heap, *h, *err_ret;
234 	struct device *dev_ret;
235 	unsigned int minor;
236 	int ret;
237 
238 	if (!exp_info->name || !strcmp(exp_info->name, "")) {
239 		pr_err("dma_heap: Cannot add heap without a name\n");
240 		return ERR_PTR(-EINVAL);
241 	}
242 
243 	if (!exp_info->ops || !exp_info->ops->allocate) {
244 		pr_err("dma_heap: Cannot add heap with invalid ops struct\n");
245 		return ERR_PTR(-EINVAL);
246 	}
247 
248 	heap = kzalloc_obj(*heap);
249 	if (!heap)
250 		return ERR_PTR(-ENOMEM);
251 
252 	heap->name = exp_info->name;
253 	heap->ops = exp_info->ops;
254 	heap->priv = exp_info->priv;
255 
256 	/* Find unused minor number */
257 	ret = xa_alloc(&dma_heap_minors, &minor, heap,
258 		       XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
259 	if (ret < 0) {
260 		pr_err("dma_heap: Unable to get minor number for heap\n");
261 		err_ret = ERR_PTR(ret);
262 		goto err0;
263 	}
264 
265 	/* Create device */
266 	heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), minor);
267 
268 	cdev_init(&heap->heap_cdev, &dma_heap_fops);
269 	ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
270 	if (ret < 0) {
271 		pr_err("dma_heap: Unable to add char device\n");
272 		err_ret = ERR_PTR(ret);
273 		goto err1;
274 	}
275 
276 	dev_ret = device_create(dma_heap_class,
277 				NULL,
278 				heap->heap_devt,
279 				NULL,
280 				heap->name);
281 	if (IS_ERR(dev_ret)) {
282 		pr_err("dma_heap: Unable to create device\n");
283 		err_ret = ERR_CAST(dev_ret);
284 		goto err2;
285 	}
286 
287 	mutex_lock(&heap_list_lock);
288 	/* check the name is unique */
289 	list_for_each_entry(h, &heap_list, list) {
290 		if (!strcmp(h->name, exp_info->name)) {
291 			mutex_unlock(&heap_list_lock);
292 			pr_err("dma_heap: Already registered heap named %s\n",
293 			       exp_info->name);
294 			err_ret = ERR_PTR(-EINVAL);
295 			goto err3;
296 		}
297 	}
298 
299 	/* Add heap to the list */
300 	list_add(&heap->list, &heap_list);
301 	mutex_unlock(&heap_list_lock);
302 
303 	return heap;
304 
305 err3:
306 	device_destroy(dma_heap_class, heap->heap_devt);
307 err2:
308 	cdev_del(&heap->heap_cdev);
309 err1:
310 	xa_erase(&dma_heap_minors, minor);
311 err0:
312 	kfree(heap);
313 	return err_ret;
314 }
315 EXPORT_SYMBOL_NS_GPL(dma_heap_add, "DMA_BUF_HEAP");
316 
317 static char *dma_heap_devnode(const struct device *dev, umode_t *mode)
318 {
319 	return kasprintf(GFP_KERNEL, "dma_heap/%s", dev_name(dev));
320 }
321 
322 static int dma_heap_init(void)
323 {
324 	int ret;
325 
326 	ret = alloc_chrdev_region(&dma_heap_devt, 0, NUM_HEAP_MINORS, DEVNAME);
327 	if (ret)
328 		return ret;
329 
330 	dma_heap_class = class_create(DEVNAME);
331 	if (IS_ERR(dma_heap_class)) {
332 		unregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);
333 		return PTR_ERR(dma_heap_class);
334 	}
335 	dma_heap_class->devnode = dma_heap_devnode;
336 
337 	return 0;
338 }
339 subsys_initcall(dma_heap_init);
340