dma-buf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Framework for buffer objects that can be shared across devices/subsystems.
  3. *
  4. * Copyright(C) 2011 Linaro Limited. All rights reserved.
  5. * Author: Sumit Semwal <sumit.semwal@ti.com>
  6. *
  7. * Many thanks to linaro-mm-sig list, and specially
  8. * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
  9. * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
  10. * refining of this idea.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2 as published by
  14. * the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include <linux/dma-buf.h>
  27. #include <linux/anon_inodes.h>
  28. #include <linux/export.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. static inline int is_dma_buf_file(struct file *);
  32. struct dma_buf_list {
  33. struct list_head head;
  34. struct mutex lock;
  35. };
  36. static struct dma_buf_list db_list;
  37. static int dma_buf_release(struct inode *inode, struct file *file)
  38. {
  39. struct dma_buf *dmabuf;
  40. if (!is_dma_buf_file(file))
  41. return -EINVAL;
  42. dmabuf = file->private_data;
  43. BUG_ON(dmabuf->vmapping_counter);
  44. dmabuf->ops->release(dmabuf);
  45. mutex_lock(&db_list.lock);
  46. list_del(&dmabuf->list_node);
  47. mutex_unlock(&db_list.lock);
  48. kfree(dmabuf);
  49. return 0;
  50. }
  51. static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
  52. {
  53. struct dma_buf *dmabuf;
  54. if (!is_dma_buf_file(file))
  55. return -EINVAL;
  56. dmabuf = file->private_data;
  57. /* check for overflowing the buffer's size */
  58. if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
  59. dmabuf->size >> PAGE_SHIFT)
  60. return -EINVAL;
  61. return dmabuf->ops->mmap(dmabuf, vma);
  62. }
  63. static const struct file_operations dma_buf_fops = {
  64. .release = dma_buf_release,
  65. .mmap = dma_buf_mmap_internal,
  66. };
  67. /*
  68. * is_dma_buf_file - Check if struct file* is associated with dma_buf
  69. */
  70. static inline int is_dma_buf_file(struct file *file)
  71. {
  72. return file->f_op == &dma_buf_fops;
  73. }
  74. /**
  75. * dma_buf_export_named - Creates a new dma_buf, and associates an anon file
  76. * with this buffer, so it can be exported.
  77. * Also connect the allocator specific data and ops to the buffer.
  78. * Additionally, provide a name string for exporter; useful in debugging.
  79. *
  80. * @priv: [in] Attach private data of allocator to this buffer
  81. * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
  82. * @size: [in] Size of the buffer
  83. * @flags: [in] mode flags for the file.
  84. * @exp_name: [in] name of the exporting module - useful for debugging.
  85. *
  86. * Returns, on success, a newly created dma_buf object, which wraps the
  87. * supplied private data and operations for dma_buf_ops. On either missing
  88. * ops, or error in allocating struct dma_buf, will return negative error.
  89. *
  90. */
  91. struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops,
  92. size_t size, int flags, const char *exp_name)
  93. {
  94. struct dma_buf *dmabuf;
  95. struct file *file;
  96. if (WARN_ON(!priv || !ops
  97. || !ops->map_dma_buf
  98. || !ops->unmap_dma_buf
  99. || !ops->release
  100. || !ops->kmap_atomic
  101. || !ops->kmap
  102. || !ops->mmap)) {
  103. return ERR_PTR(-EINVAL);
  104. }
  105. dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
  106. if (dmabuf == NULL)
  107. return ERR_PTR(-ENOMEM);
  108. dmabuf->priv = priv;
  109. dmabuf->ops = ops;
  110. dmabuf->size = size;
  111. dmabuf->exp_name = exp_name;
  112. file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
  113. dmabuf->file = file;
  114. mutex_init(&dmabuf->lock);
  115. INIT_LIST_HEAD(&dmabuf->attachments);
  116. mutex_lock(&db_list.lock);
  117. list_add(&dmabuf->list_node, &db_list.head);
  118. mutex_unlock(&db_list.lock);
  119. return dmabuf;
  120. }
  121. EXPORT_SYMBOL_GPL(dma_buf_export_named);
  122. /**
  123. * dma_buf_fd - returns a file descriptor for the given dma_buf
  124. * @dmabuf: [in] pointer to dma_buf for which fd is required.
  125. * @flags: [in] flags to give to fd
  126. *
  127. * On success, returns an associated 'fd'. Else, returns error.
  128. */
  129. int dma_buf_fd(struct dma_buf *dmabuf, int flags)
  130. {
  131. int fd;
  132. if (!dmabuf || !dmabuf->file)
  133. return -EINVAL;
  134. fd = get_unused_fd_flags(flags);
  135. if (fd < 0)
  136. return fd;
  137. fd_install(fd, dmabuf->file);
  138. return fd;
  139. }
  140. EXPORT_SYMBOL_GPL(dma_buf_fd);
  141. /**
  142. * dma_buf_get - returns the dma_buf structure related to an fd
  143. * @fd: [in] fd associated with the dma_buf to be returned
  144. *
  145. * On success, returns the dma_buf structure associated with an fd; uses
  146. * file's refcounting done by fget to increase refcount. returns ERR_PTR
  147. * otherwise.
  148. */
  149. struct dma_buf *dma_buf_get(int fd)
  150. {
  151. struct file *file;
  152. file = fget(fd);
  153. if (!file)
  154. return ERR_PTR(-EBADF);
  155. if (!is_dma_buf_file(file)) {
  156. fput(file);
  157. return ERR_PTR(-EINVAL);
  158. }
  159. return file->private_data;
  160. }
  161. EXPORT_SYMBOL_GPL(dma_buf_get);
  162. /**
  163. * dma_buf_put - decreases refcount of the buffer
  164. * @dmabuf: [in] buffer to reduce refcount of
  165. *
  166. * Uses file's refcounting done implicitly by fput()
  167. */
  168. void dma_buf_put(struct dma_buf *dmabuf)
  169. {
  170. if (WARN_ON(!dmabuf || !dmabuf->file))
  171. return;
  172. fput(dmabuf->file);
  173. }
  174. EXPORT_SYMBOL_GPL(dma_buf_put);
  175. /**
  176. * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
  177. * calls attach() of dma_buf_ops to allow device-specific attach functionality
  178. * @dmabuf: [in] buffer to attach device to.
  179. * @dev: [in] device to be attached.
  180. *
  181. * Returns struct dma_buf_attachment * for this attachment; may return negative
  182. * error codes.
  183. *
  184. */
  185. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  186. struct device *dev)
  187. {
  188. struct dma_buf_attachment *attach;
  189. int ret;
  190. if (WARN_ON(!dmabuf || !dev))
  191. return ERR_PTR(-EINVAL);
  192. attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
  193. if (attach == NULL)
  194. return ERR_PTR(-ENOMEM);
  195. attach->dev = dev;
  196. attach->dmabuf = dmabuf;
  197. mutex_lock(&dmabuf->lock);
  198. if (dmabuf->ops->attach) {
  199. ret = dmabuf->ops->attach(dmabuf, dev, attach);
  200. if (ret)
  201. goto err_attach;
  202. }
  203. list_add(&attach->node, &dmabuf->attachments);
  204. mutex_unlock(&dmabuf->lock);
  205. return attach;
  206. err_attach:
  207. kfree(attach);
  208. mutex_unlock(&dmabuf->lock);
  209. return ERR_PTR(ret);
  210. }
  211. EXPORT_SYMBOL_GPL(dma_buf_attach);
  212. /**
  213. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
  214. * optionally calls detach() of dma_buf_ops for device-specific detach
  215. * @dmabuf: [in] buffer to detach from.
  216. * @attach: [in] attachment to be detached; is free'd after this call.
  217. *
  218. */
  219. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  220. {
  221. if (WARN_ON(!dmabuf || !attach))
  222. return;
  223. mutex_lock(&dmabuf->lock);
  224. list_del(&attach->node);
  225. if (dmabuf->ops->detach)
  226. dmabuf->ops->detach(dmabuf, attach);
  227. mutex_unlock(&dmabuf->lock);
  228. kfree(attach);
  229. }
  230. EXPORT_SYMBOL_GPL(dma_buf_detach);
  231. /**
  232. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  233. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  234. * dma_buf_ops.
  235. * @attach: [in] attachment whose scatterlist is to be returned
  236. * @direction: [in] direction of DMA transfer
  237. *
  238. * Returns sg_table containing the scatterlist to be returned; may return NULL
  239. * or ERR_PTR.
  240. *
  241. */
  242. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  243. enum dma_data_direction direction)
  244. {
  245. struct sg_table *sg_table = ERR_PTR(-EINVAL);
  246. might_sleep();
  247. if (WARN_ON(!attach || !attach->dmabuf))
  248. return ERR_PTR(-EINVAL);
  249. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  250. return sg_table;
  251. }
  252. EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
  253. /**
  254. * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
  255. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  256. * dma_buf_ops.
  257. * @attach: [in] attachment to unmap buffer from
  258. * @sg_table: [in] scatterlist info of the buffer to unmap
  259. * @direction: [in] direction of DMA transfer
  260. *
  261. */
  262. void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  263. struct sg_table *sg_table,
  264. enum dma_data_direction direction)
  265. {
  266. might_sleep();
  267. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  268. return;
  269. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
  270. direction);
  271. }
  272. EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
  273. /**
  274. * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
  275. * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
  276. * preparations. Coherency is only guaranteed in the specified range for the
  277. * specified access direction.
  278. * @dmabuf: [in] buffer to prepare cpu access for.
  279. * @start: [in] start of range for cpu access.
  280. * @len: [in] length of range for cpu access.
  281. * @direction: [in] length of range for cpu access.
  282. *
  283. * Can return negative error values, returns 0 on success.
  284. */
  285. int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  286. enum dma_data_direction direction)
  287. {
  288. int ret = 0;
  289. if (WARN_ON(!dmabuf))
  290. return -EINVAL;
  291. if (dmabuf->ops->begin_cpu_access)
  292. ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
  293. return ret;
  294. }
  295. EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
  296. /**
  297. * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
  298. * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
  299. * actions. Coherency is only guaranteed in the specified range for the
  300. * specified access direction.
  301. * @dmabuf: [in] buffer to complete cpu access for.
  302. * @start: [in] start of range for cpu access.
  303. * @len: [in] length of range for cpu access.
  304. * @direction: [in] length of range for cpu access.
  305. *
  306. * This call must always succeed.
  307. */
  308. void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  309. enum dma_data_direction direction)
  310. {
  311. WARN_ON(!dmabuf);
  312. if (dmabuf->ops->end_cpu_access)
  313. dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
  314. }
  315. EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
  316. /**
  317. * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
  318. * space. The same restrictions as for kmap_atomic and friends apply.
  319. * @dmabuf: [in] buffer to map page from.
  320. * @page_num: [in] page in PAGE_SIZE units to map.
  321. *
  322. * This call must always succeed, any necessary preparations that might fail
  323. * need to be done in begin_cpu_access.
  324. */
  325. void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
  326. {
  327. WARN_ON(!dmabuf);
  328. return dmabuf->ops->kmap_atomic(dmabuf, page_num);
  329. }
  330. EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
  331. /**
  332. * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
  333. * @dmabuf: [in] buffer to unmap page from.
  334. * @page_num: [in] page in PAGE_SIZE units to unmap.
  335. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
  336. *
  337. * This call must always succeed.
  338. */
  339. void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
  340. void *vaddr)
  341. {
  342. WARN_ON(!dmabuf);
  343. if (dmabuf->ops->kunmap_atomic)
  344. dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
  345. }
  346. EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
  347. /**
  348. * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
  349. * same restrictions as for kmap and friends apply.
  350. * @dmabuf: [in] buffer to map page from.
  351. * @page_num: [in] page in PAGE_SIZE units to map.
  352. *
  353. * This call must always succeed, any necessary preparations that might fail
  354. * need to be done in begin_cpu_access.
  355. */
  356. void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
  357. {
  358. WARN_ON(!dmabuf);
  359. return dmabuf->ops->kmap(dmabuf, page_num);
  360. }
  361. EXPORT_SYMBOL_GPL(dma_buf_kmap);
  362. /**
  363. * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
  364. * @dmabuf: [in] buffer to unmap page from.
  365. * @page_num: [in] page in PAGE_SIZE units to unmap.
  366. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
  367. *
  368. * This call must always succeed.
  369. */
  370. void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
  371. void *vaddr)
  372. {
  373. WARN_ON(!dmabuf);
  374. if (dmabuf->ops->kunmap)
  375. dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
  376. }
  377. EXPORT_SYMBOL_GPL(dma_buf_kunmap);
  378. /**
  379. * dma_buf_mmap - Setup up a userspace mmap with the given vma
  380. * @dmabuf: [in] buffer that should back the vma
  381. * @vma: [in] vma for the mmap
  382. * @pgoff: [in] offset in pages where this mmap should start within the
  383. * dma-buf buffer.
  384. *
  385. * This function adjusts the passed in vma so that it points at the file of the
  386. * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
  387. * checking on the size of the vma. Then it calls the exporters mmap function to
  388. * set up the mapping.
  389. *
  390. * Can return negative error values, returns 0 on success.
  391. */
  392. int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
  393. unsigned long pgoff)
  394. {
  395. struct file *oldfile;
  396. int ret;
  397. if (WARN_ON(!dmabuf || !vma))
  398. return -EINVAL;
  399. /* check for offset overflow */
  400. if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
  401. return -EOVERFLOW;
  402. /* check for overflowing the buffer's size */
  403. if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
  404. dmabuf->size >> PAGE_SHIFT)
  405. return -EINVAL;
  406. /* readjust the vma */
  407. get_file(dmabuf->file);
  408. oldfile = vma->vm_file;
  409. vma->vm_file = dmabuf->file;
  410. vma->vm_pgoff = pgoff;
  411. ret = dmabuf->ops->mmap(dmabuf, vma);
  412. if (ret) {
  413. /* restore old parameters on failure */
  414. vma->vm_file = oldfile;
  415. fput(dmabuf->file);
  416. } else {
  417. if (oldfile)
  418. fput(oldfile);
  419. }
  420. return ret;
  421. }
  422. EXPORT_SYMBOL_GPL(dma_buf_mmap);
  423. /**
  424. * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
  425. * address space. Same restrictions as for vmap and friends apply.
  426. * @dmabuf: [in] buffer to vmap
  427. *
  428. * This call may fail due to lack of virtual mapping address space.
  429. * These calls are optional in drivers. The intended use for them
  430. * is for mapping objects linear in kernel space for high use objects.
  431. * Please attempt to use kmap/kunmap before thinking about these interfaces.
  432. */
  433. void *dma_buf_vmap(struct dma_buf *dmabuf)
  434. {
  435. void *ptr;
  436. if (WARN_ON(!dmabuf))
  437. return NULL;
  438. if (!dmabuf->ops->vmap)
  439. return NULL;
  440. mutex_lock(&dmabuf->lock);
  441. if (dmabuf->vmapping_counter) {
  442. dmabuf->vmapping_counter++;
  443. BUG_ON(!dmabuf->vmap_ptr);
  444. ptr = dmabuf->vmap_ptr;
  445. goto out_unlock;
  446. }
  447. BUG_ON(dmabuf->vmap_ptr);
  448. ptr = dmabuf->ops->vmap(dmabuf);
  449. if (IS_ERR_OR_NULL(ptr))
  450. goto out_unlock;
  451. dmabuf->vmap_ptr = ptr;
  452. dmabuf->vmapping_counter = 1;
  453. out_unlock:
  454. mutex_unlock(&dmabuf->lock);
  455. return ptr;
  456. }
  457. EXPORT_SYMBOL_GPL(dma_buf_vmap);
  458. /**
  459. * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
  460. * @dmabuf: [in] buffer to vunmap
  461. * @vaddr: [in] vmap to vunmap
  462. */
  463. void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
  464. {
  465. if (WARN_ON(!dmabuf))
  466. return;
  467. BUG_ON(!dmabuf->vmap_ptr);
  468. BUG_ON(dmabuf->vmapping_counter == 0);
  469. BUG_ON(dmabuf->vmap_ptr != vaddr);
  470. mutex_lock(&dmabuf->lock);
  471. if (--dmabuf->vmapping_counter == 0) {
  472. if (dmabuf->ops->vunmap)
  473. dmabuf->ops->vunmap(dmabuf, vaddr);
  474. dmabuf->vmap_ptr = NULL;
  475. }
  476. mutex_unlock(&dmabuf->lock);
  477. }
  478. EXPORT_SYMBOL_GPL(dma_buf_vunmap);
  479. #ifdef CONFIG_DEBUG_FS
  480. static int dma_buf_describe(struct seq_file *s)
  481. {
  482. int ret;
  483. struct dma_buf *buf_obj;
  484. struct dma_buf_attachment *attach_obj;
  485. int count = 0, attach_count;
  486. size_t size = 0;
  487. ret = mutex_lock_interruptible(&db_list.lock);
  488. if (ret)
  489. return ret;
  490. seq_printf(s, "\nDma-buf Objects:\n");
  491. seq_printf(s, "\texp_name\tsize\tflags\tmode\tcount\n");
  492. list_for_each_entry(buf_obj, &db_list.head, list_node) {
  493. ret = mutex_lock_interruptible(&buf_obj->lock);
  494. if (ret) {
  495. seq_printf(s,
  496. "\tERROR locking buffer object: skipping\n");
  497. continue;
  498. }
  499. seq_printf(s, "\t");
  500. seq_printf(s, "\t%s\t%08zu\t%08x\t%08x\t%08ld\n",
  501. buf_obj->exp_name, buf_obj->size,
  502. buf_obj->file->f_flags, buf_obj->file->f_mode,
  503. (long)(buf_obj->file->f_count.counter));
  504. seq_printf(s, "\t\tAttached Devices:\n");
  505. attach_count = 0;
  506. list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
  507. seq_printf(s, "\t\t");
  508. seq_printf(s, "%s\n", attach_obj->dev->init_name);
  509. attach_count++;
  510. }
  511. seq_printf(s, "\n\t\tTotal %d devices attached\n",
  512. attach_count);
  513. count++;
  514. size += buf_obj->size;
  515. mutex_unlock(&buf_obj->lock);
  516. }
  517. seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
  518. mutex_unlock(&db_list.lock);
  519. return 0;
  520. }
  521. static int dma_buf_show(struct seq_file *s, void *unused)
  522. {
  523. void (*func)(struct seq_file *) = s->private;
  524. func(s);
  525. return 0;
  526. }
  527. static int dma_buf_debug_open(struct inode *inode, struct file *file)
  528. {
  529. return single_open(file, dma_buf_show, inode->i_private);
  530. }
  531. static const struct file_operations dma_buf_debug_fops = {
  532. .open = dma_buf_debug_open,
  533. .read = seq_read,
  534. .llseek = seq_lseek,
  535. .release = single_release,
  536. };
  537. static struct dentry *dma_buf_debugfs_dir;
  538. static int dma_buf_init_debugfs(void)
  539. {
  540. int err = 0;
  541. dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL);
  542. if (IS_ERR(dma_buf_debugfs_dir)) {
  543. err = PTR_ERR(dma_buf_debugfs_dir);
  544. dma_buf_debugfs_dir = NULL;
  545. return err;
  546. }
  547. err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe);
  548. if (err)
  549. pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
  550. return err;
  551. }
  552. static void dma_buf_uninit_debugfs(void)
  553. {
  554. if (dma_buf_debugfs_dir)
  555. debugfs_remove_recursive(dma_buf_debugfs_dir);
  556. }
  557. int dma_buf_debugfs_create_file(const char *name,
  558. int (*write)(struct seq_file *))
  559. {
  560. struct dentry *d;
  561. d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir,
  562. write, &dma_buf_debug_fops);
  563. if (IS_ERR(d))
  564. return PTR_ERR(d);
  565. return 0;
  566. }
  567. #else
  568. static inline int dma_buf_init_debugfs(void)
  569. {
  570. return 0;
  571. }
  572. static inline void dma_buf_uninit_debugfs(void)
  573. {
  574. }
  575. #endif
  576. static int __init dma_buf_init(void)
  577. {
  578. mutex_init(&db_list.lock);
  579. INIT_LIST_HEAD(&db_list.head);
  580. dma_buf_init_debugfs();
  581. return 0;
  582. }
  583. subsys_initcall(dma_buf_init);
  584. static void __exit dma_buf_deinit(void)
  585. {
  586. dma_buf_uninit_debugfs();
  587. }
  588. __exitcall(dma_buf_deinit);