dma-heap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/debugfs.h>
  10. #include <linux/device.h>
  11. #include <linux/dma-buf.h>
  12. #include <linux/err.h>
  13. #include <linux/xarray.h>
  14. #include <linux/list.h>
  15. #include <linux/slab.h>
  16. #include <linux/nospec.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/dma-heap.h>
  20. #include <uapi/linux/dma-heap.h>
  21. #define DEVNAME "dma_heap"
  22. #define NUM_HEAP_MINORS 128
  23. /**
  24. * struct dma_heap - represents a dmabuf heap in the system
  25. * @name: used for debugging/device-node name
  26. * @ops: ops struct 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. * @heap_dev heap device struct
  31. *
  32. * Represents a heap of memory from which buffers can be made.
  33. */
  34. struct dma_heap {
  35. const char *name;
  36. const struct dma_heap_ops *ops;
  37. void *priv;
  38. dev_t heap_devt;
  39. struct list_head list;
  40. struct cdev heap_cdev;
  41. struct kref refcount;
  42. struct device *heap_dev;
  43. };
  44. static LIST_HEAD(heap_list);
  45. static DEFINE_MUTEX(heap_list_lock);
  46. static dev_t dma_heap_devt;
  47. static struct class *dma_heap_class;
  48. static DEFINE_XARRAY_ALLOC(dma_heap_minors);
  49. struct dma_heap *dma_heap_find(const char *name)
  50. {
  51. struct dma_heap *h;
  52. mutex_lock(&heap_list_lock);
  53. list_for_each_entry(h, &heap_list, list) {
  54. if (!strcmp(h->name, name)) {
  55. kref_get(&h->refcount);
  56. mutex_unlock(&heap_list_lock);
  57. return h;
  58. }
  59. }
  60. mutex_unlock(&heap_list_lock);
  61. return NULL;
  62. }
  63. EXPORT_SYMBOL_GPL(dma_heap_find);
  64. void dma_heap_buffer_free(struct dma_buf *dmabuf)
  65. {
  66. dma_buf_put(dmabuf);
  67. }
  68. EXPORT_SYMBOL_GPL(dma_heap_buffer_free);
  69. struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
  70. unsigned int fd_flags,
  71. unsigned int heap_flags)
  72. {
  73. if (fd_flags & ~DMA_HEAP_VALID_FD_FLAGS)
  74. return ERR_PTR(-EINVAL);
  75. if (heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
  76. return ERR_PTR(-EINVAL);
  77. /*
  78. * Allocations from all heaps have to begin
  79. * and end on page boundaries.
  80. */
  81. len = PAGE_ALIGN(len);
  82. if (!len)
  83. return ERR_PTR(-EINVAL);
  84. return heap->ops->allocate(heap, len, fd_flags, heap_flags);
  85. }
  86. EXPORT_SYMBOL_GPL(dma_heap_buffer_alloc);
  87. int dma_heap_bufferfd_alloc(struct dma_heap *heap, size_t len,
  88. unsigned int fd_flags,
  89. unsigned int heap_flags)
  90. {
  91. struct dma_buf *dmabuf;
  92. int fd;
  93. dmabuf = dma_heap_buffer_alloc(heap, len, fd_flags, heap_flags);
  94. if (IS_ERR(dmabuf))
  95. return PTR_ERR(dmabuf);
  96. fd = dma_buf_fd(dmabuf, fd_flags);
  97. if (fd < 0) {
  98. dma_buf_put(dmabuf);
  99. /* just return, as put will call release and that will free */
  100. }
  101. return fd;
  102. }
  103. EXPORT_SYMBOL_GPL(dma_heap_bufferfd_alloc);
  104. static int dma_heap_open(struct inode *inode, struct file *file)
  105. {
  106. struct dma_heap *heap;
  107. heap = xa_load(&dma_heap_minors, iminor(inode));
  108. if (!heap) {
  109. pr_err("dma_heap: minor %d unknown.\n", iminor(inode));
  110. return -ENODEV;
  111. }
  112. /* instance data as context */
  113. file->private_data = heap;
  114. nonseekable_open(inode, file);
  115. return 0;
  116. }
  117. static long dma_heap_ioctl_allocate(struct file *file, void *data)
  118. {
  119. struct dma_heap_allocation_data *heap_allocation = data;
  120. struct dma_heap *heap = file->private_data;
  121. int fd;
  122. if (heap_allocation->fd)
  123. return -EINVAL;
  124. fd = dma_heap_bufferfd_alloc(heap, heap_allocation->len,
  125. heap_allocation->fd_flags,
  126. heap_allocation->heap_flags);
  127. if (fd < 0)
  128. return fd;
  129. heap_allocation->fd = fd;
  130. return 0;
  131. }
  132. static unsigned int dma_heap_ioctl_cmds[] = {
  133. DMA_HEAP_IOCTL_ALLOC,
  134. };
  135. static long dma_heap_ioctl(struct file *file, unsigned int ucmd,
  136. unsigned long arg)
  137. {
  138. char stack_kdata[128];
  139. char *kdata = stack_kdata;
  140. unsigned int kcmd;
  141. unsigned int in_size, out_size, drv_size, ksize;
  142. int nr = _IOC_NR(ucmd);
  143. int ret = 0;
  144. if (nr >= ARRAY_SIZE(dma_heap_ioctl_cmds))
  145. return -EINVAL;
  146. nr = array_index_nospec(nr, ARRAY_SIZE(dma_heap_ioctl_cmds));
  147. /* Get the kernel ioctl cmd that matches */
  148. kcmd = dma_heap_ioctl_cmds[nr];
  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. /* If necessary, allocate buffer for ioctl argument */
  159. if (ksize > sizeof(stack_kdata)) {
  160. kdata = kmalloc(ksize, GFP_KERNEL);
  161. if (!kdata)
  162. return -ENOMEM;
  163. }
  164. if (copy_from_user(kdata, (void __user *)arg, in_size) != 0) {
  165. ret = -EFAULT;
  166. goto err;
  167. }
  168. /* zero out any difference between the kernel/user structure size */
  169. if (ksize > in_size)
  170. memset(kdata + in_size, 0, ksize - in_size);
  171. switch (kcmd) {
  172. case DMA_HEAP_IOCTL_ALLOC:
  173. ret = dma_heap_ioctl_allocate(file, kdata);
  174. break;
  175. default:
  176. ret = -ENOTTY;
  177. goto err;
  178. }
  179. if (copy_to_user((void __user *)arg, kdata, out_size) != 0)
  180. ret = -EFAULT;
  181. err:
  182. if (kdata != stack_kdata)
  183. kfree(kdata);
  184. return ret;
  185. }
  186. static const struct file_operations dma_heap_fops = {
  187. .owner = THIS_MODULE,
  188. .open = dma_heap_open,
  189. .unlocked_ioctl = dma_heap_ioctl,
  190. #ifdef CONFIG_COMPAT
  191. .compat_ioctl = dma_heap_ioctl,
  192. #endif
  193. };
  194. /**
  195. * dma_heap_get_drvdata() - get per-subdriver data for the heap
  196. * @heap: DMA-Heap to retrieve private data for
  197. *
  198. * Returns:
  199. * The per-subdriver data for the heap.
  200. */
  201. void *dma_heap_get_drvdata(struct dma_heap *heap)
  202. {
  203. return heap->priv;
  204. }
  205. EXPORT_SYMBOL_GPL(dma_heap_get_drvdata);
  206. static void dma_heap_release(struct kref *ref)
  207. {
  208. struct dma_heap *heap = container_of(ref, struct dma_heap, refcount);
  209. int minor = MINOR(heap->heap_devt);
  210. /* Note, we already holding the heap_list_lock here */
  211. list_del(&heap->list);
  212. device_destroy(dma_heap_class, heap->heap_devt);
  213. cdev_del(&heap->heap_cdev);
  214. xa_erase(&dma_heap_minors, minor);
  215. kfree(heap);
  216. }
  217. void dma_heap_put(struct dma_heap *h)
  218. {
  219. /*
  220. * Take the heap_list_lock now to avoid racing with code
  221. * scanning the list and then taking a kref.
  222. */
  223. mutex_lock(&heap_list_lock);
  224. kref_put(&h->refcount, dma_heap_release);
  225. mutex_unlock(&heap_list_lock);
  226. }
  227. EXPORT_SYMBOL_GPL(dma_heap_put);
  228. /**
  229. * dma_heap_get_dev() - get device struct for the heap
  230. * @heap: DMA-Heap to retrieve device struct from
  231. *
  232. * Returns:
  233. * The device struct for the heap.
  234. */
  235. struct device *dma_heap_get_dev(struct dma_heap *heap)
  236. {
  237. return heap->heap_dev;
  238. }
  239. EXPORT_SYMBOL_GPL(dma_heap_get_dev);
  240. /**
  241. * dma_heap_get_name() - get heap name
  242. * @heap: DMA-Heap to retrieve private data for
  243. *
  244. * Returns:
  245. * The char* for the heap name.
  246. */
  247. const char *dma_heap_get_name(struct dma_heap *heap)
  248. {
  249. return heap->name;
  250. }
  251. EXPORT_SYMBOL_GPL(dma_heap_get_name);
  252. struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info)
  253. {
  254. struct dma_heap *heap, *h, *err_ret;
  255. unsigned int minor;
  256. int ret;
  257. if (!exp_info->name || !strcmp(exp_info->name, "")) {
  258. pr_err("dma_heap: Cannot add heap without a name\n");
  259. return ERR_PTR(-EINVAL);
  260. }
  261. if (!exp_info->ops || !exp_info->ops->allocate) {
  262. pr_err("dma_heap: Cannot add heap with invalid ops struct\n");
  263. return ERR_PTR(-EINVAL);
  264. }
  265. heap = kzalloc(sizeof(*heap), GFP_KERNEL);
  266. if (!heap)
  267. return ERR_PTR(-ENOMEM);
  268. kref_init(&heap->refcount);
  269. heap->name = exp_info->name;
  270. heap->ops = exp_info->ops;
  271. heap->priv = exp_info->priv;
  272. /* Find unused minor number */
  273. ret = xa_alloc(&dma_heap_minors, &minor, heap,
  274. XA_LIMIT(0, NUM_HEAP_MINORS - 1), GFP_KERNEL);
  275. if (ret < 0) {
  276. pr_err("dma_heap: Unable to get minor number for heap\n");
  277. err_ret = ERR_PTR(ret);
  278. goto err0;
  279. }
  280. /* Create device */
  281. heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), minor);
  282. cdev_init(&heap->heap_cdev, &dma_heap_fops);
  283. ret = cdev_add(&heap->heap_cdev, heap->heap_devt, 1);
  284. if (ret < 0) {
  285. pr_err("dma_heap: Unable to add char device\n");
  286. err_ret = ERR_PTR(ret);
  287. goto err1;
  288. }
  289. heap->heap_dev = device_create(dma_heap_class,
  290. NULL,
  291. heap->heap_devt,
  292. NULL,
  293. heap->name);
  294. if (IS_ERR(heap->heap_dev)) {
  295. pr_err("dma_heap: Unable to create device\n");
  296. err_ret = ERR_CAST(heap->heap_dev);
  297. goto err2;
  298. }
  299. /* Make sure it doesn't disappear on us */
  300. heap->heap_dev = get_device(heap->heap_dev);
  301. mutex_lock(&heap_list_lock);
  302. /* check the name is unique */
  303. list_for_each_entry(h, &heap_list, list) {
  304. if (!strcmp(h->name, exp_info->name)) {
  305. mutex_unlock(&heap_list_lock);
  306. pr_err("dma_heap: Already registered heap named %s\n",
  307. exp_info->name);
  308. err_ret = ERR_PTR(-EINVAL);
  309. put_device(heap->heap_dev);
  310. goto err3;
  311. }
  312. }
  313. /* Add heap to the list */
  314. list_add(&heap->list, &heap_list);
  315. mutex_unlock(&heap_list_lock);
  316. return heap;
  317. err3:
  318. device_destroy(dma_heap_class, heap->heap_devt);
  319. err2:
  320. cdev_del(&heap->heap_cdev);
  321. err1:
  322. xa_erase(&dma_heap_minors, minor);
  323. err0:
  324. kfree(heap);
  325. return err_ret;
  326. }
  327. EXPORT_SYMBOL_GPL(dma_heap_add);
  328. static char *dma_heap_devnode(struct device *dev, umode_t *mode)
  329. {
  330. return kasprintf(GFP_KERNEL, "dma_heap/%s", dev_name(dev));
  331. }
  332. static ssize_t total_pools_kb_show(struct kobject *kobj,
  333. struct kobj_attribute *attr, char *buf)
  334. {
  335. struct dma_heap *heap;
  336. u64 total_pool_size = 0;
  337. mutex_lock(&heap_list_lock);
  338. list_for_each_entry(heap, &heap_list, list) {
  339. if (heap->ops->get_pool_size)
  340. total_pool_size += heap->ops->get_pool_size(heap);
  341. }
  342. mutex_unlock(&heap_list_lock);
  343. return sysfs_emit(buf, "%llu\n", total_pool_size / 1024);
  344. }
  345. static struct kobj_attribute total_pools_kb_attr =
  346. __ATTR_RO(total_pools_kb);
  347. static struct attribute *dma_heap_sysfs_attrs[] = {
  348. &total_pools_kb_attr.attr,
  349. NULL,
  350. };
  351. ATTRIBUTE_GROUPS(dma_heap_sysfs);
  352. static struct kobject *dma_heap_kobject;
  353. static int dma_heap_sysfs_setup(void)
  354. {
  355. int ret;
  356. dma_heap_kobject = kobject_create_and_add("dma_heap", kernel_kobj);
  357. if (!dma_heap_kobject)
  358. return -ENOMEM;
  359. ret = sysfs_create_groups(dma_heap_kobject, dma_heap_sysfs_groups);
  360. if (ret) {
  361. kobject_put(dma_heap_kobject);
  362. return ret;
  363. }
  364. return 0;
  365. }
  366. static void dma_heap_sysfs_teardown(void)
  367. {
  368. kobject_put(dma_heap_kobject);
  369. }
  370. static int dma_heap_init(void)
  371. {
  372. int ret;
  373. ret = dma_heap_sysfs_setup();
  374. if (ret)
  375. return ret;
  376. ret = alloc_chrdev_region(&dma_heap_devt, 0, NUM_HEAP_MINORS, DEVNAME);
  377. if (ret)
  378. goto err_chrdev;
  379. dma_heap_class = class_create(THIS_MODULE, DEVNAME);
  380. if (IS_ERR(dma_heap_class)) {
  381. ret = PTR_ERR(dma_heap_class);
  382. goto err_class;
  383. }
  384. dma_heap_class->devnode = dma_heap_devnode;
  385. return 0;
  386. err_class:
  387. unregister_chrdev_region(dma_heap_devt, NUM_HEAP_MINORS);
  388. err_chrdev:
  389. dma_heap_sysfs_teardown();
  390. return ret;
  391. }
  392. subsys_initcall(dma_heap_init);