dma-heap.c 7.7 KB

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