dma-buf.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Framework for buffer objects that can be shared across devices/subsystems.
  4. *
  5. * Copyright(C) 2011 Linaro Limited. All rights reserved.
  6. * Author: Sumit Semwal <sumit.semwal@ti.com>
  7. *
  8. * Many thanks to linaro-mm-sig list, and specially
  9. * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
  10. * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
  11. * refining of this idea.
  12. */
  13. #include <linux/fs.h>
  14. #include <linux/slab.h>
  15. #include <linux/dma-buf.h>
  16. #include <linux/dma-fence.h>
  17. #include <linux/dma-fence-unwrap.h>
  18. #include <linux/anon_inodes.h>
  19. #include <linux/export.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/sync_file.h>
  26. #include <linux/poll.h>
  27. #include <linux/dma-resv.h>
  28. #include <linux/mm.h>
  29. #include <linux/mount.h>
  30. #include <linux/pseudo_fs.h>
  31. #include <uapi/linux/dma-buf.h>
  32. #include <uapi/linux/magic.h>
  33. #define CREATE_TRACE_POINTS
  34. #include <trace/events/dma_buf.h>
  35. /*
  36. * dmabuf->name must be accessed with holding dmabuf->name_lock.
  37. * we need to take the lock around the tracepoint call itself where
  38. * it is called in the code.
  39. *
  40. * Note: FUNC##_enabled() is a static branch that will only
  41. * be set when the trace event is enabled.
  42. */
  43. #define DMA_BUF_TRACE(FUNC, ...) \
  44. do { \
  45. /* Always expose lock if lockdep is enabled */ \
  46. if (IS_ENABLED(CONFIG_LOCKDEP) || FUNC##_enabled()) { \
  47. guard(spinlock)(&dmabuf->name_lock); \
  48. FUNC(__VA_ARGS__); \
  49. } \
  50. } while (0)
  51. /* Wrapper to hide the sg_table page link from the importer */
  52. struct dma_buf_sg_table_wrapper {
  53. struct sg_table *original;
  54. struct sg_table wrapper;
  55. };
  56. static inline int is_dma_buf_file(struct file *);
  57. static DEFINE_MUTEX(dmabuf_list_mutex);
  58. static LIST_HEAD(dmabuf_list);
  59. static void __dma_buf_list_add(struct dma_buf *dmabuf)
  60. {
  61. mutex_lock(&dmabuf_list_mutex);
  62. list_add(&dmabuf->list_node, &dmabuf_list);
  63. mutex_unlock(&dmabuf_list_mutex);
  64. }
  65. static void __dma_buf_list_del(struct dma_buf *dmabuf)
  66. {
  67. if (!dmabuf)
  68. return;
  69. mutex_lock(&dmabuf_list_mutex);
  70. list_del(&dmabuf->list_node);
  71. mutex_unlock(&dmabuf_list_mutex);
  72. }
  73. /**
  74. * dma_buf_iter_begin - begin iteration through global list of all DMA buffers
  75. *
  76. * Returns the first buffer in the global list of DMA-bufs that's not in the
  77. * process of being destroyed. Increments that buffer's reference count to
  78. * prevent buffer destruction. Callers must release the reference, either by
  79. * continuing iteration with dma_buf_iter_next(), or with dma_buf_put().
  80. *
  81. * Return:
  82. * * First buffer from global list, with refcount elevated
  83. * * NULL if no active buffers are present
  84. */
  85. struct dma_buf *dma_buf_iter_begin(void)
  86. {
  87. struct dma_buf *ret = NULL, *dmabuf;
  88. /*
  89. * The list mutex does not protect a dmabuf's refcount, so it can be
  90. * zeroed while we are iterating. We cannot call get_dma_buf() since the
  91. * caller may not already own a reference to the buffer.
  92. */
  93. mutex_lock(&dmabuf_list_mutex);
  94. list_for_each_entry(dmabuf, &dmabuf_list, list_node) {
  95. if (file_ref_get(&dmabuf->file->f_ref)) {
  96. ret = dmabuf;
  97. break;
  98. }
  99. }
  100. mutex_unlock(&dmabuf_list_mutex);
  101. return ret;
  102. }
  103. /**
  104. * dma_buf_iter_next - continue iteration through global list of all DMA buffers
  105. * @dmabuf: [in] pointer to dma_buf
  106. *
  107. * Decrements the reference count on the provided buffer. Returns the next
  108. * buffer from the remainder of the global list of DMA-bufs with its reference
  109. * count incremented. Callers must release the reference, either by continuing
  110. * iteration with dma_buf_iter_next(), or with dma_buf_put().
  111. *
  112. * Return:
  113. * * Next buffer from global list, with refcount elevated
  114. * * NULL if no additional active buffers are present
  115. */
  116. struct dma_buf *dma_buf_iter_next(struct dma_buf *dmabuf)
  117. {
  118. struct dma_buf *ret = NULL;
  119. /*
  120. * The list mutex does not protect a dmabuf's refcount, so it can be
  121. * zeroed while we are iterating. We cannot call get_dma_buf() since the
  122. * caller may not already own a reference to the buffer.
  123. */
  124. mutex_lock(&dmabuf_list_mutex);
  125. dma_buf_put(dmabuf);
  126. list_for_each_entry_continue(dmabuf, &dmabuf_list, list_node) {
  127. if (file_ref_get(&dmabuf->file->f_ref)) {
  128. ret = dmabuf;
  129. break;
  130. }
  131. }
  132. mutex_unlock(&dmabuf_list_mutex);
  133. return ret;
  134. }
  135. static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen)
  136. {
  137. struct dma_buf *dmabuf;
  138. char name[DMA_BUF_NAME_LEN];
  139. ssize_t ret = 0;
  140. dmabuf = dentry->d_fsdata;
  141. spin_lock(&dmabuf->name_lock);
  142. if (dmabuf->name)
  143. ret = strscpy(name, dmabuf->name, sizeof(name));
  144. spin_unlock(&dmabuf->name_lock);
  145. return dynamic_dname(buffer, buflen, "/%s:%s",
  146. dentry->d_name.name, ret > 0 ? name : "");
  147. }
  148. static void dma_buf_release(struct dentry *dentry)
  149. {
  150. struct dma_buf *dmabuf;
  151. dmabuf = dentry->d_fsdata;
  152. if (unlikely(!dmabuf))
  153. return;
  154. BUG_ON(dmabuf->vmapping_counter);
  155. /*
  156. * If you hit this BUG() it could mean:
  157. * * There's a file reference imbalance in dma_buf_poll / dma_buf_poll_cb or somewhere else
  158. * * dmabuf->cb_in/out.active are non-0 despite no pending fence callback
  159. */
  160. BUG_ON(dmabuf->cb_in.active || dmabuf->cb_out.active);
  161. dmabuf->ops->release(dmabuf);
  162. if (dmabuf->resv == (struct dma_resv *)&dmabuf[1])
  163. dma_resv_fini(dmabuf->resv);
  164. WARN_ON(!list_empty(&dmabuf->attachments));
  165. module_put(dmabuf->owner);
  166. kfree(dmabuf->name);
  167. kfree(dmabuf);
  168. }
  169. static int dma_buf_file_release(struct inode *inode, struct file *file)
  170. {
  171. if (!is_dma_buf_file(file))
  172. return -EINVAL;
  173. __dma_buf_list_del(file->private_data);
  174. return 0;
  175. }
  176. static const struct dentry_operations dma_buf_dentry_ops = {
  177. .d_dname = dmabuffs_dname,
  178. .d_release = dma_buf_release,
  179. };
  180. static struct vfsmount *dma_buf_mnt;
  181. static int dma_buf_fs_init_context(struct fs_context *fc)
  182. {
  183. struct pseudo_fs_context *ctx;
  184. ctx = init_pseudo(fc, DMA_BUF_MAGIC);
  185. if (!ctx)
  186. return -ENOMEM;
  187. ctx->dops = &dma_buf_dentry_ops;
  188. return 0;
  189. }
  190. static struct file_system_type dma_buf_fs_type = {
  191. .name = "dmabuf",
  192. .init_fs_context = dma_buf_fs_init_context,
  193. .kill_sb = kill_anon_super,
  194. };
  195. static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
  196. {
  197. struct dma_buf *dmabuf;
  198. if (!is_dma_buf_file(file))
  199. return -EINVAL;
  200. dmabuf = file->private_data;
  201. /* check if buffer supports mmap */
  202. if (!dmabuf->ops->mmap)
  203. return -EINVAL;
  204. /* check for overflowing the buffer's size */
  205. if (vma->vm_pgoff + vma_pages(vma) >
  206. dmabuf->size >> PAGE_SHIFT)
  207. return -EINVAL;
  208. DMA_BUF_TRACE(trace_dma_buf_mmap_internal, dmabuf);
  209. return dmabuf->ops->mmap(dmabuf, vma);
  210. }
  211. static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
  212. {
  213. struct dma_buf *dmabuf;
  214. loff_t base;
  215. if (!is_dma_buf_file(file))
  216. return -EBADF;
  217. dmabuf = file->private_data;
  218. /* only support discovering the end of the buffer,
  219. * but also allow SEEK_SET to maintain the idiomatic
  220. * SEEK_END(0), SEEK_CUR(0) pattern.
  221. */
  222. if (whence == SEEK_END)
  223. base = dmabuf->size;
  224. else if (whence == SEEK_SET)
  225. base = 0;
  226. else
  227. return -EINVAL;
  228. if (offset != 0)
  229. return -EINVAL;
  230. return base + offset;
  231. }
  232. /**
  233. * DOC: implicit fence polling
  234. *
  235. * To support cross-device and cross-driver synchronization of buffer access
  236. * implicit fences (represented internally in the kernel with &struct dma_fence)
  237. * can be attached to a &dma_buf. The glue for that and a few related things are
  238. * provided in the &dma_resv structure.
  239. *
  240. * Userspace can query the state of these implicitly tracked fences using poll()
  241. * and related system calls:
  242. *
  243. * - Checking for EPOLLIN, i.e. read access, can be use to query the state of the
  244. * most recent write or exclusive fence.
  245. *
  246. * - Checking for EPOLLOUT, i.e. write access, can be used to query the state of
  247. * all attached fences, shared and exclusive ones.
  248. *
  249. * Note that this only signals the completion of the respective fences, i.e. the
  250. * DMA transfers are complete. Cache flushing and any other necessary
  251. * preparations before CPU access can begin still need to happen.
  252. *
  253. * As an alternative to poll(), the set of fences on DMA buffer can be
  254. * exported as a &sync_file using &dma_buf_sync_file_export.
  255. */
  256. static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
  257. {
  258. struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb;
  259. struct dma_buf *dmabuf = container_of(dcb->poll, struct dma_buf, poll);
  260. unsigned long flags;
  261. spin_lock_irqsave(&dcb->poll->lock, flags);
  262. wake_up_locked_poll(dcb->poll, dcb->active);
  263. dcb->active = 0;
  264. spin_unlock_irqrestore(&dcb->poll->lock, flags);
  265. dma_fence_put(fence);
  266. /* Paired with get_file in dma_buf_poll */
  267. fput(dmabuf->file);
  268. }
  269. static bool dma_buf_poll_add_cb(struct dma_resv *resv, bool write,
  270. struct dma_buf_poll_cb_t *dcb)
  271. {
  272. struct dma_resv_iter cursor;
  273. struct dma_fence *fence;
  274. int r;
  275. dma_resv_for_each_fence(&cursor, resv, dma_resv_usage_rw(write),
  276. fence) {
  277. dma_fence_get(fence);
  278. r = dma_fence_add_callback(fence, &dcb->cb, dma_buf_poll_cb);
  279. if (!r)
  280. return true;
  281. dma_fence_put(fence);
  282. }
  283. return false;
  284. }
  285. static __poll_t dma_buf_poll(struct file *file, poll_table *poll)
  286. {
  287. struct dma_buf *dmabuf;
  288. struct dma_resv *resv;
  289. __poll_t events;
  290. dmabuf = file->private_data;
  291. if (!dmabuf || !dmabuf->resv)
  292. return EPOLLERR;
  293. resv = dmabuf->resv;
  294. poll_wait(file, &dmabuf->poll, poll);
  295. events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT);
  296. if (!events)
  297. return 0;
  298. dma_resv_lock(resv, NULL);
  299. if (events & EPOLLOUT) {
  300. struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_out;
  301. /* Check that callback isn't busy */
  302. spin_lock_irq(&dmabuf->poll.lock);
  303. if (dcb->active)
  304. events &= ~EPOLLOUT;
  305. else
  306. dcb->active = EPOLLOUT;
  307. spin_unlock_irq(&dmabuf->poll.lock);
  308. if (events & EPOLLOUT) {
  309. /* Paired with fput in dma_buf_poll_cb */
  310. get_file(dmabuf->file);
  311. if (!dma_buf_poll_add_cb(resv, true, dcb))
  312. /* No callback queued, wake up any other waiters */
  313. dma_buf_poll_cb(NULL, &dcb->cb);
  314. else
  315. events &= ~EPOLLOUT;
  316. }
  317. }
  318. if (events & EPOLLIN) {
  319. struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_in;
  320. /* Check that callback isn't busy */
  321. spin_lock_irq(&dmabuf->poll.lock);
  322. if (dcb->active)
  323. events &= ~EPOLLIN;
  324. else
  325. dcb->active = EPOLLIN;
  326. spin_unlock_irq(&dmabuf->poll.lock);
  327. if (events & EPOLLIN) {
  328. /* Paired with fput in dma_buf_poll_cb */
  329. get_file(dmabuf->file);
  330. if (!dma_buf_poll_add_cb(resv, false, dcb))
  331. /* No callback queued, wake up any other waiters */
  332. dma_buf_poll_cb(NULL, &dcb->cb);
  333. else
  334. events &= ~EPOLLIN;
  335. }
  336. }
  337. dma_resv_unlock(resv);
  338. return events;
  339. }
  340. /**
  341. * dma_buf_set_name - Set a name to a specific dma_buf to track the usage.
  342. * It could support changing the name of the dma-buf if the same
  343. * piece of memory is used for multiple purpose between different devices.
  344. *
  345. * @dmabuf: [in] dmabuf buffer that will be renamed.
  346. * @buf: [in] A piece of userspace memory that contains the name of
  347. * the dma-buf.
  348. *
  349. * Returns 0 on success. If the dma-buf buffer is already attached to
  350. * devices, return -EBUSY.
  351. *
  352. */
  353. static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
  354. {
  355. char *name = strndup_user(buf, DMA_BUF_NAME_LEN);
  356. if (IS_ERR(name))
  357. return PTR_ERR(name);
  358. spin_lock(&dmabuf->name_lock);
  359. kfree(dmabuf->name);
  360. dmabuf->name = name;
  361. spin_unlock(&dmabuf->name_lock);
  362. return 0;
  363. }
  364. #if IS_ENABLED(CONFIG_SYNC_FILE)
  365. static long dma_buf_export_sync_file(struct dma_buf *dmabuf,
  366. void __user *user_data)
  367. {
  368. struct dma_buf_export_sync_file arg;
  369. enum dma_resv_usage usage;
  370. struct dma_fence *fence = NULL;
  371. struct sync_file *sync_file;
  372. int fd, ret;
  373. if (copy_from_user(&arg, user_data, sizeof(arg)))
  374. return -EFAULT;
  375. if (arg.flags & ~DMA_BUF_SYNC_RW)
  376. return -EINVAL;
  377. if ((arg.flags & DMA_BUF_SYNC_RW) == 0)
  378. return -EINVAL;
  379. fd = get_unused_fd_flags(O_CLOEXEC);
  380. if (fd < 0)
  381. return fd;
  382. usage = dma_resv_usage_rw(arg.flags & DMA_BUF_SYNC_WRITE);
  383. ret = dma_resv_get_singleton(dmabuf->resv, usage, &fence);
  384. if (ret)
  385. goto err_put_fd;
  386. if (!fence)
  387. fence = dma_fence_get_stub();
  388. sync_file = sync_file_create(fence);
  389. dma_fence_put(fence);
  390. if (!sync_file) {
  391. ret = -ENOMEM;
  392. goto err_put_fd;
  393. }
  394. arg.fd = fd;
  395. if (copy_to_user(user_data, &arg, sizeof(arg))) {
  396. ret = -EFAULT;
  397. goto err_put_file;
  398. }
  399. fd_install(fd, sync_file->file);
  400. return 0;
  401. err_put_file:
  402. fput(sync_file->file);
  403. err_put_fd:
  404. put_unused_fd(fd);
  405. return ret;
  406. }
  407. static long dma_buf_import_sync_file(struct dma_buf *dmabuf,
  408. const void __user *user_data)
  409. {
  410. struct dma_buf_import_sync_file arg;
  411. struct dma_fence *fence, *f;
  412. enum dma_resv_usage usage;
  413. struct dma_fence_unwrap iter;
  414. unsigned int num_fences;
  415. int ret = 0;
  416. if (copy_from_user(&arg, user_data, sizeof(arg)))
  417. return -EFAULT;
  418. if (arg.flags & ~DMA_BUF_SYNC_RW)
  419. return -EINVAL;
  420. if ((arg.flags & DMA_BUF_SYNC_RW) == 0)
  421. return -EINVAL;
  422. fence = sync_file_get_fence(arg.fd);
  423. if (!fence)
  424. return -EINVAL;
  425. usage = (arg.flags & DMA_BUF_SYNC_WRITE) ? DMA_RESV_USAGE_WRITE :
  426. DMA_RESV_USAGE_READ;
  427. num_fences = 0;
  428. dma_fence_unwrap_for_each(f, &iter, fence)
  429. ++num_fences;
  430. if (num_fences > 0) {
  431. dma_resv_lock(dmabuf->resv, NULL);
  432. ret = dma_resv_reserve_fences(dmabuf->resv, num_fences);
  433. if (!ret) {
  434. dma_fence_unwrap_for_each(f, &iter, fence)
  435. dma_resv_add_fence(dmabuf->resv, f, usage);
  436. }
  437. dma_resv_unlock(dmabuf->resv);
  438. }
  439. dma_fence_put(fence);
  440. return ret;
  441. }
  442. #endif
  443. static long dma_buf_ioctl(struct file *file,
  444. unsigned int cmd, unsigned long arg)
  445. {
  446. struct dma_buf *dmabuf;
  447. struct dma_buf_sync sync;
  448. enum dma_data_direction direction;
  449. int ret;
  450. dmabuf = file->private_data;
  451. switch (cmd) {
  452. case DMA_BUF_IOCTL_SYNC:
  453. if (copy_from_user(&sync, (void __user *) arg, sizeof(sync)))
  454. return -EFAULT;
  455. if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK)
  456. return -EINVAL;
  457. switch (sync.flags & DMA_BUF_SYNC_RW) {
  458. case DMA_BUF_SYNC_READ:
  459. direction = DMA_FROM_DEVICE;
  460. break;
  461. case DMA_BUF_SYNC_WRITE:
  462. direction = DMA_TO_DEVICE;
  463. break;
  464. case DMA_BUF_SYNC_RW:
  465. direction = DMA_BIDIRECTIONAL;
  466. break;
  467. default:
  468. return -EINVAL;
  469. }
  470. if (sync.flags & DMA_BUF_SYNC_END)
  471. ret = dma_buf_end_cpu_access(dmabuf, direction);
  472. else
  473. ret = dma_buf_begin_cpu_access(dmabuf, direction);
  474. return ret;
  475. case DMA_BUF_SET_NAME_A:
  476. case DMA_BUF_SET_NAME_B:
  477. return dma_buf_set_name(dmabuf, (const char __user *)arg);
  478. #if IS_ENABLED(CONFIG_SYNC_FILE)
  479. case DMA_BUF_IOCTL_EXPORT_SYNC_FILE:
  480. return dma_buf_export_sync_file(dmabuf, (void __user *)arg);
  481. case DMA_BUF_IOCTL_IMPORT_SYNC_FILE:
  482. return dma_buf_import_sync_file(dmabuf, (const void __user *)arg);
  483. #endif
  484. default:
  485. return -ENOTTY;
  486. }
  487. }
  488. static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
  489. {
  490. struct dma_buf *dmabuf = file->private_data;
  491. seq_printf(m, "size:\t%zu\n", dmabuf->size);
  492. /* Don't count the temporary reference taken inside procfs seq_show */
  493. seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1);
  494. seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name);
  495. spin_lock(&dmabuf->name_lock);
  496. if (dmabuf->name)
  497. seq_printf(m, "name:\t%s\n", dmabuf->name);
  498. spin_unlock(&dmabuf->name_lock);
  499. }
  500. static const struct file_operations dma_buf_fops = {
  501. .release = dma_buf_file_release,
  502. .mmap = dma_buf_mmap_internal,
  503. .llseek = dma_buf_llseek,
  504. .poll = dma_buf_poll,
  505. .unlocked_ioctl = dma_buf_ioctl,
  506. .compat_ioctl = compat_ptr_ioctl,
  507. .show_fdinfo = dma_buf_show_fdinfo,
  508. };
  509. /*
  510. * is_dma_buf_file - Check if struct file* is associated with dma_buf
  511. */
  512. static inline int is_dma_buf_file(struct file *file)
  513. {
  514. return file->f_op == &dma_buf_fops;
  515. }
  516. static struct file *dma_buf_getfile(size_t size, int flags)
  517. {
  518. static atomic64_t dmabuf_inode = ATOMIC64_INIT(0);
  519. struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb);
  520. struct file *file;
  521. if (IS_ERR(inode))
  522. return ERR_CAST(inode);
  523. inode->i_size = size;
  524. inode_set_bytes(inode, size);
  525. /*
  526. * The ->i_ino acquired from get_next_ino() is not unique thus
  527. * not suitable for using it as dentry name by dmabuf stats.
  528. * Override ->i_ino with the unique and dmabuffs specific
  529. * value.
  530. */
  531. inode->i_ino = atomic64_inc_return(&dmabuf_inode);
  532. flags &= O_ACCMODE | O_NONBLOCK;
  533. file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf",
  534. flags, &dma_buf_fops);
  535. if (IS_ERR(file))
  536. goto err_alloc_file;
  537. return file;
  538. err_alloc_file:
  539. iput(inode);
  540. return file;
  541. }
  542. /**
  543. * DOC: dma buf device access
  544. *
  545. * For device DMA access to a shared DMA buffer the usual sequence of operations
  546. * is fairly simple:
  547. *
  548. * 1. The exporter defines his exporter instance using
  549. * DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private
  550. * buffer object into a &dma_buf. It then exports that &dma_buf to userspace
  551. * as a file descriptor by calling dma_buf_fd().
  552. *
  553. * 2. Userspace passes this file-descriptors to all drivers it wants this buffer
  554. * to share with: First the file descriptor is converted to a &dma_buf using
  555. * dma_buf_get(). Then the buffer is attached to the device using
  556. * dma_buf_attach().
  557. *
  558. * Up to this stage the exporter is still free to migrate or reallocate the
  559. * backing storage.
  560. *
  561. * 3. Once the buffer is attached to all devices userspace can initiate DMA
  562. * access to the shared buffer. In the kernel this is done by calling
  563. * dma_buf_map_attachment() and dma_buf_unmap_attachment().
  564. *
  565. * 4. Once a driver is done with a shared buffer it needs to call
  566. * dma_buf_detach() (after cleaning up any mappings) and then release the
  567. * reference acquired with dma_buf_get() by calling dma_buf_put().
  568. *
  569. * For the detailed semantics exporters are expected to implement see
  570. * &dma_buf_ops.
  571. */
  572. /**
  573. * dma_buf_export - Creates a new dma_buf, and associates an anon file
  574. * with this buffer, so it can be exported.
  575. * Also connect the allocator specific data and ops to the buffer.
  576. * Additionally, provide a name string for exporter; useful in debugging.
  577. *
  578. * @exp_info: [in] holds all the export related information provided
  579. * by the exporter. see &struct dma_buf_export_info
  580. * for further details.
  581. *
  582. * Returns, on success, a newly created struct dma_buf object, which wraps the
  583. * supplied private data and operations for struct dma_buf_ops. On either
  584. * missing ops, or error in allocating struct dma_buf, will return negative
  585. * error.
  586. *
  587. * For most cases the easiest way to create @exp_info is through the
  588. * %DEFINE_DMA_BUF_EXPORT_INFO macro.
  589. */
  590. struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
  591. {
  592. struct dma_buf *dmabuf;
  593. struct dma_resv *resv = exp_info->resv;
  594. struct file *file;
  595. size_t alloc_size = sizeof(struct dma_buf);
  596. int ret;
  597. if (WARN_ON(!exp_info->priv || !exp_info->ops
  598. || !exp_info->ops->map_dma_buf
  599. || !exp_info->ops->unmap_dma_buf
  600. || !exp_info->ops->release))
  601. return ERR_PTR(-EINVAL);
  602. if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
  603. return ERR_PTR(-EINVAL);
  604. if (!try_module_get(exp_info->owner))
  605. return ERR_PTR(-ENOENT);
  606. file = dma_buf_getfile(exp_info->size, exp_info->flags);
  607. if (IS_ERR(file)) {
  608. ret = PTR_ERR(file);
  609. goto err_module;
  610. }
  611. if (!exp_info->resv)
  612. alloc_size += sizeof(struct dma_resv);
  613. else
  614. /* prevent &dma_buf[1] == dma_buf->resv */
  615. alloc_size += 1;
  616. dmabuf = kzalloc(alloc_size, GFP_KERNEL);
  617. if (!dmabuf) {
  618. ret = -ENOMEM;
  619. goto err_file;
  620. }
  621. dmabuf->priv = exp_info->priv;
  622. dmabuf->ops = exp_info->ops;
  623. dmabuf->size = exp_info->size;
  624. dmabuf->exp_name = exp_info->exp_name;
  625. dmabuf->owner = exp_info->owner;
  626. spin_lock_init(&dmabuf->name_lock);
  627. init_waitqueue_head(&dmabuf->poll);
  628. dmabuf->cb_in.poll = dmabuf->cb_out.poll = &dmabuf->poll;
  629. dmabuf->cb_in.active = dmabuf->cb_out.active = 0;
  630. INIT_LIST_HEAD(&dmabuf->attachments);
  631. if (!resv) {
  632. dmabuf->resv = (struct dma_resv *)&dmabuf[1];
  633. dma_resv_init(dmabuf->resv);
  634. } else {
  635. dmabuf->resv = resv;
  636. }
  637. file->private_data = dmabuf;
  638. file->f_path.dentry->d_fsdata = dmabuf;
  639. dmabuf->file = file;
  640. __dma_buf_list_add(dmabuf);
  641. DMA_BUF_TRACE(trace_dma_buf_export, dmabuf);
  642. return dmabuf;
  643. err_file:
  644. fput(file);
  645. err_module:
  646. module_put(exp_info->owner);
  647. return ERR_PTR(ret);
  648. }
  649. EXPORT_SYMBOL_NS_GPL(dma_buf_export, "DMA_BUF");
  650. /**
  651. * dma_buf_fd - returns a file descriptor for the given struct dma_buf
  652. * @dmabuf: [in] pointer to dma_buf for which fd is required.
  653. * @flags: [in] flags to give to fd
  654. *
  655. * On success, returns an associated 'fd'. Else, returns error.
  656. */
  657. int dma_buf_fd(struct dma_buf *dmabuf, int flags)
  658. {
  659. int fd;
  660. if (!dmabuf || !dmabuf->file)
  661. return -EINVAL;
  662. fd = FD_ADD(flags, dmabuf->file);
  663. DMA_BUF_TRACE(trace_dma_buf_fd, dmabuf, fd);
  664. return fd;
  665. }
  666. EXPORT_SYMBOL_NS_GPL(dma_buf_fd, "DMA_BUF");
  667. /**
  668. * dma_buf_get - returns the struct dma_buf related to an fd
  669. * @fd: [in] fd associated with the struct dma_buf to be returned
  670. *
  671. * On success, returns the struct dma_buf associated with an fd; uses
  672. * file's refcounting done by fget to increase refcount. returns ERR_PTR
  673. * otherwise.
  674. */
  675. struct dma_buf *dma_buf_get(int fd)
  676. {
  677. struct file *file;
  678. struct dma_buf *dmabuf;
  679. file = fget(fd);
  680. if (!file)
  681. return ERR_PTR(-EBADF);
  682. if (!is_dma_buf_file(file)) {
  683. fput(file);
  684. return ERR_PTR(-EINVAL);
  685. }
  686. dmabuf = file->private_data;
  687. DMA_BUF_TRACE(trace_dma_buf_get, dmabuf, fd);
  688. return dmabuf;
  689. }
  690. EXPORT_SYMBOL_NS_GPL(dma_buf_get, "DMA_BUF");
  691. /**
  692. * dma_buf_put - decreases refcount of the buffer
  693. * @dmabuf: [in] buffer to reduce refcount of
  694. *
  695. * Uses file's refcounting done implicitly by fput().
  696. *
  697. * If, as a result of this call, the refcount becomes 0, the 'release' file
  698. * operation related to this fd is called. It calls &dma_buf_ops.release vfunc
  699. * in turn, and frees the memory allocated for dmabuf when exported.
  700. */
  701. void dma_buf_put(struct dma_buf *dmabuf)
  702. {
  703. if (WARN_ON(!dmabuf || !dmabuf->file))
  704. return;
  705. fput(dmabuf->file);
  706. DMA_BUF_TRACE(trace_dma_buf_put, dmabuf);
  707. }
  708. EXPORT_SYMBOL_NS_GPL(dma_buf_put, "DMA_BUF");
  709. static int dma_buf_wrap_sg_table(struct sg_table **sg_table)
  710. {
  711. struct scatterlist *to_sg, *from_sg;
  712. struct sg_table *from = *sg_table;
  713. struct dma_buf_sg_table_wrapper *to;
  714. int i, ret;
  715. if (!IS_ENABLED(CONFIG_DMABUF_DEBUG))
  716. return 0;
  717. /*
  718. * To catch abuse of the underlying struct page by importers copy the
  719. * sg_table without copying the page_link and give only the copy back to
  720. * the importer.
  721. */
  722. to = kzalloc_obj(*to);
  723. if (!to)
  724. return -ENOMEM;
  725. ret = sg_alloc_table(&to->wrapper, from->nents, GFP_KERNEL);
  726. if (ret)
  727. goto free_to;
  728. to_sg = to->wrapper.sgl;
  729. for_each_sgtable_dma_sg(from, from_sg, i) {
  730. to_sg->offset = 0;
  731. to_sg->length = 0;
  732. sg_assign_page(to_sg, NULL);
  733. sg_dma_address(to_sg) = sg_dma_address(from_sg);
  734. sg_dma_len(to_sg) = sg_dma_len(from_sg);
  735. to_sg = sg_next(to_sg);
  736. }
  737. to->original = from;
  738. *sg_table = &to->wrapper;
  739. return 0;
  740. free_to:
  741. kfree(to);
  742. return ret;
  743. }
  744. static void dma_buf_unwrap_sg_table(struct sg_table **sg_table)
  745. {
  746. struct dma_buf_sg_table_wrapper *copy;
  747. if (!IS_ENABLED(CONFIG_DMABUF_DEBUG))
  748. return;
  749. copy = container_of(*sg_table, typeof(*copy), wrapper);
  750. *sg_table = copy->original;
  751. sg_free_table(&copy->wrapper);
  752. kfree(copy);
  753. }
  754. static inline bool
  755. dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
  756. {
  757. return !!attach->importer_ops;
  758. }
  759. static bool
  760. dma_buf_pin_on_map(struct dma_buf_attachment *attach)
  761. {
  762. return attach->dmabuf->ops->pin &&
  763. (!dma_buf_attachment_is_dynamic(attach) ||
  764. !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY));
  765. }
  766. /**
  767. * DOC: locking convention
  768. *
  769. * In order to avoid deadlock situations between dma-buf exports and importers,
  770. * all dma-buf API users must follow the common dma-buf locking convention.
  771. *
  772. * Convention for importers
  773. *
  774. * 1. Importers must hold the dma-buf reservation lock when calling these
  775. * functions:
  776. *
  777. * - dma_buf_pin()
  778. * - dma_buf_unpin()
  779. * - dma_buf_map_attachment()
  780. * - dma_buf_unmap_attachment()
  781. * - dma_buf_vmap()
  782. * - dma_buf_vunmap()
  783. *
  784. * 2. Importers must not hold the dma-buf reservation lock when calling these
  785. * functions:
  786. *
  787. * - dma_buf_attach()
  788. * - dma_buf_dynamic_attach()
  789. * - dma_buf_detach()
  790. * - dma_buf_export()
  791. * - dma_buf_fd()
  792. * - dma_buf_get()
  793. * - dma_buf_put()
  794. * - dma_buf_mmap()
  795. * - dma_buf_begin_cpu_access()
  796. * - dma_buf_end_cpu_access()
  797. * - dma_buf_map_attachment_unlocked()
  798. * - dma_buf_unmap_attachment_unlocked()
  799. * - dma_buf_vmap_unlocked()
  800. * - dma_buf_vunmap_unlocked()
  801. *
  802. * Convention for exporters
  803. *
  804. * 1. These &dma_buf_ops callbacks are invoked with unlocked dma-buf
  805. * reservation and exporter can take the lock:
  806. *
  807. * - &dma_buf_ops.attach()
  808. * - &dma_buf_ops.detach()
  809. * - &dma_buf_ops.release()
  810. * - &dma_buf_ops.begin_cpu_access()
  811. * - &dma_buf_ops.end_cpu_access()
  812. * - &dma_buf_ops.mmap()
  813. *
  814. * 2. These &dma_buf_ops callbacks are invoked with locked dma-buf
  815. * reservation and exporter can't take the lock:
  816. *
  817. * - &dma_buf_ops.pin()
  818. * - &dma_buf_ops.unpin()
  819. * - &dma_buf_ops.map_dma_buf()
  820. * - &dma_buf_ops.unmap_dma_buf()
  821. * - &dma_buf_ops.vmap()
  822. * - &dma_buf_ops.vunmap()
  823. *
  824. * 3. Exporters must hold the dma-buf reservation lock when calling these
  825. * functions:
  826. *
  827. * - dma_buf_move_notify()
  828. */
  829. /**
  830. * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list
  831. * @dmabuf: [in] buffer to attach device to.
  832. * @dev: [in] device to be attached.
  833. * @importer_ops: [in] importer operations for the attachment
  834. * @importer_priv: [in] importer private pointer for the attachment
  835. *
  836. * Returns struct dma_buf_attachment pointer for this attachment. Attachments
  837. * must be cleaned up by calling dma_buf_detach().
  838. *
  839. * Optionally this calls &dma_buf_ops.attach to allow device-specific attach
  840. * functionality.
  841. *
  842. * Returns:
  843. *
  844. * A pointer to newly created &dma_buf_attachment on success, or a negative
  845. * error code wrapped into a pointer on failure.
  846. *
  847. * Note that this can fail if the backing storage of @dmabuf is in a place not
  848. * accessible to @dev, and cannot be moved to a more suitable place. This is
  849. * indicated with the error code -EBUSY.
  850. */
  851. struct dma_buf_attachment *
  852. dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
  853. const struct dma_buf_attach_ops *importer_ops,
  854. void *importer_priv)
  855. {
  856. struct dma_buf_attachment *attach;
  857. int ret;
  858. if (WARN_ON(!dmabuf || !dev))
  859. return ERR_PTR(-EINVAL);
  860. if (WARN_ON(importer_ops && !importer_ops->move_notify))
  861. return ERR_PTR(-EINVAL);
  862. attach = kzalloc_obj(*attach);
  863. if (!attach)
  864. return ERR_PTR(-ENOMEM);
  865. attach->dev = dev;
  866. attach->dmabuf = dmabuf;
  867. if (importer_ops)
  868. attach->peer2peer = importer_ops->allow_peer2peer;
  869. attach->importer_ops = importer_ops;
  870. attach->importer_priv = importer_priv;
  871. if (dmabuf->ops->attach) {
  872. ret = dmabuf->ops->attach(dmabuf, attach);
  873. if (ret)
  874. goto err_attach;
  875. }
  876. dma_resv_lock(dmabuf->resv, NULL);
  877. list_add(&attach->node, &dmabuf->attachments);
  878. dma_resv_unlock(dmabuf->resv);
  879. DMA_BUF_TRACE(trace_dma_buf_dynamic_attach, dmabuf, attach,
  880. dma_buf_attachment_is_dynamic(attach), dev);
  881. return attach;
  882. err_attach:
  883. kfree(attach);
  884. return ERR_PTR(ret);
  885. }
  886. EXPORT_SYMBOL_NS_GPL(dma_buf_dynamic_attach, "DMA_BUF");
  887. /**
  888. * dma_buf_attach - Wrapper for dma_buf_dynamic_attach
  889. * @dmabuf: [in] buffer to attach device to.
  890. * @dev: [in] device to be attached.
  891. *
  892. * Wrapper to call dma_buf_dynamic_attach() for drivers which still use a static
  893. * mapping.
  894. */
  895. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  896. struct device *dev)
  897. {
  898. return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
  899. }
  900. EXPORT_SYMBOL_NS_GPL(dma_buf_attach, "DMA_BUF");
  901. /**
  902. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list
  903. * @dmabuf: [in] buffer to detach from.
  904. * @attach: [in] attachment to be detached; is free'd after this call.
  905. *
  906. * Clean up a device attachment obtained by calling dma_buf_attach().
  907. *
  908. * Optionally this calls &dma_buf_ops.detach for device-specific detach.
  909. */
  910. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  911. {
  912. if (WARN_ON(!dmabuf || !attach || dmabuf != attach->dmabuf))
  913. return;
  914. dma_resv_lock(dmabuf->resv, NULL);
  915. list_del(&attach->node);
  916. dma_resv_unlock(dmabuf->resv);
  917. if (dmabuf->ops->detach)
  918. dmabuf->ops->detach(dmabuf, attach);
  919. DMA_BUF_TRACE(trace_dma_buf_detach, dmabuf, attach,
  920. dma_buf_attachment_is_dynamic(attach), attach->dev);
  921. kfree(attach);
  922. }
  923. EXPORT_SYMBOL_NS_GPL(dma_buf_detach, "DMA_BUF");
  924. /**
  925. * dma_buf_pin - Lock down the DMA-buf
  926. * @attach: [in] attachment which should be pinned
  927. *
  928. * Only dynamic importers (who set up @attach with dma_buf_dynamic_attach()) may
  929. * call this, and only for limited use cases like scanout and not for temporary
  930. * pin operations. It is not permitted to allow userspace to pin arbitrary
  931. * amounts of buffers through this interface.
  932. *
  933. * Buffers must be unpinned by calling dma_buf_unpin().
  934. *
  935. * Returns:
  936. * 0 on success, negative error code on failure.
  937. */
  938. int dma_buf_pin(struct dma_buf_attachment *attach)
  939. {
  940. struct dma_buf *dmabuf = attach->dmabuf;
  941. int ret = 0;
  942. WARN_ON(!attach->importer_ops);
  943. dma_resv_assert_held(dmabuf->resv);
  944. if (dmabuf->ops->pin)
  945. ret = dmabuf->ops->pin(attach);
  946. return ret;
  947. }
  948. EXPORT_SYMBOL_NS_GPL(dma_buf_pin, "DMA_BUF");
  949. /**
  950. * dma_buf_unpin - Unpin a DMA-buf
  951. * @attach: [in] attachment which should be unpinned
  952. *
  953. * This unpins a buffer pinned by dma_buf_pin() and allows the exporter to move
  954. * any mapping of @attach again and inform the importer through
  955. * &dma_buf_attach_ops.move_notify.
  956. */
  957. void dma_buf_unpin(struct dma_buf_attachment *attach)
  958. {
  959. struct dma_buf *dmabuf = attach->dmabuf;
  960. WARN_ON(!attach->importer_ops);
  961. dma_resv_assert_held(dmabuf->resv);
  962. if (dmabuf->ops->unpin)
  963. dmabuf->ops->unpin(attach);
  964. }
  965. EXPORT_SYMBOL_NS_GPL(dma_buf_unpin, "DMA_BUF");
  966. /**
  967. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  968. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  969. * dma_buf_ops.
  970. * @attach: [in] attachment whose scatterlist is to be returned
  971. * @direction: [in] direction of DMA transfer
  972. *
  973. * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
  974. * on error. May return -EINTR if it is interrupted by a signal.
  975. *
  976. * On success, the DMA addresses and lengths in the returned scatterlist are
  977. * PAGE_SIZE aligned.
  978. *
  979. * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that
  980. * the underlying backing storage is pinned for as long as a mapping exists,
  981. * therefore users/importers should not hold onto a mapping for undue amounts of
  982. * time.
  983. *
  984. * Important: Dynamic importers must wait for the exclusive fence of the struct
  985. * dma_resv attached to the DMA-BUF first.
  986. */
  987. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  988. enum dma_data_direction direction)
  989. {
  990. struct sg_table *sg_table;
  991. signed long ret;
  992. might_sleep();
  993. if (WARN_ON(!attach || !attach->dmabuf))
  994. return ERR_PTR(-EINVAL);
  995. dma_resv_assert_held(attach->dmabuf->resv);
  996. if (dma_buf_pin_on_map(attach)) {
  997. ret = attach->dmabuf->ops->pin(attach);
  998. /*
  999. * Catch exporters making buffers inaccessible even when
  1000. * attachments preventing that exist.
  1001. */
  1002. WARN_ON_ONCE(ret == -EBUSY);
  1003. if (ret)
  1004. return ERR_PTR(ret);
  1005. }
  1006. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  1007. if (!sg_table)
  1008. sg_table = ERR_PTR(-ENOMEM);
  1009. if (IS_ERR(sg_table))
  1010. goto error_unpin;
  1011. /*
  1012. * Importers with static attachments don't wait for fences.
  1013. */
  1014. if (!dma_buf_attachment_is_dynamic(attach)) {
  1015. ret = dma_resv_wait_timeout(attach->dmabuf->resv,
  1016. DMA_RESV_USAGE_KERNEL, true,
  1017. MAX_SCHEDULE_TIMEOUT);
  1018. if (ret < 0)
  1019. goto error_unmap;
  1020. }
  1021. ret = dma_buf_wrap_sg_table(&sg_table);
  1022. if (ret)
  1023. goto error_unmap;
  1024. if (IS_ENABLED(CONFIG_DMA_API_DEBUG)) {
  1025. struct scatterlist *sg;
  1026. u64 addr;
  1027. int len;
  1028. int i;
  1029. for_each_sgtable_dma_sg(sg_table, sg, i) {
  1030. addr = sg_dma_address(sg);
  1031. len = sg_dma_len(sg);
  1032. if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(len)) {
  1033. pr_debug("%s: addr %llx or len %x is not page aligned!\n",
  1034. __func__, addr, len);
  1035. break;
  1036. }
  1037. }
  1038. }
  1039. return sg_table;
  1040. error_unmap:
  1041. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
  1042. sg_table = ERR_PTR(ret);
  1043. error_unpin:
  1044. if (dma_buf_pin_on_map(attach))
  1045. attach->dmabuf->ops->unpin(attach);
  1046. return sg_table;
  1047. }
  1048. EXPORT_SYMBOL_NS_GPL(dma_buf_map_attachment, "DMA_BUF");
  1049. /**
  1050. * dma_buf_map_attachment_unlocked - Returns the scatterlist table of the attachment;
  1051. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  1052. * dma_buf_ops.
  1053. * @attach: [in] attachment whose scatterlist is to be returned
  1054. * @direction: [in] direction of DMA transfer
  1055. *
  1056. * Unlocked variant of dma_buf_map_attachment().
  1057. */
  1058. struct sg_table *
  1059. dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
  1060. enum dma_data_direction direction)
  1061. {
  1062. struct sg_table *sg_table;
  1063. might_sleep();
  1064. if (WARN_ON(!attach || !attach->dmabuf))
  1065. return ERR_PTR(-EINVAL);
  1066. dma_resv_lock(attach->dmabuf->resv, NULL);
  1067. sg_table = dma_buf_map_attachment(attach, direction);
  1068. dma_resv_unlock(attach->dmabuf->resv);
  1069. return sg_table;
  1070. }
  1071. EXPORT_SYMBOL_NS_GPL(dma_buf_map_attachment_unlocked, "DMA_BUF");
  1072. /**
  1073. * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
  1074. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  1075. * dma_buf_ops.
  1076. * @attach: [in] attachment to unmap buffer from
  1077. * @sg_table: [in] scatterlist info of the buffer to unmap
  1078. * @direction: [in] direction of DMA transfer
  1079. *
  1080. * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment().
  1081. */
  1082. void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  1083. struct sg_table *sg_table,
  1084. enum dma_data_direction direction)
  1085. {
  1086. might_sleep();
  1087. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  1088. return;
  1089. dma_resv_assert_held(attach->dmabuf->resv);
  1090. dma_buf_unwrap_sg_table(&sg_table);
  1091. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
  1092. if (dma_buf_pin_on_map(attach))
  1093. attach->dmabuf->ops->unpin(attach);
  1094. }
  1095. EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_attachment, "DMA_BUF");
  1096. /**
  1097. * dma_buf_unmap_attachment_unlocked - unmaps and decreases usecount of the buffer;might
  1098. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  1099. * dma_buf_ops.
  1100. * @attach: [in] attachment to unmap buffer from
  1101. * @sg_table: [in] scatterlist info of the buffer to unmap
  1102. * @direction: [in] direction of DMA transfer
  1103. *
  1104. * Unlocked variant of dma_buf_unmap_attachment().
  1105. */
  1106. void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
  1107. struct sg_table *sg_table,
  1108. enum dma_data_direction direction)
  1109. {
  1110. might_sleep();
  1111. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  1112. return;
  1113. dma_resv_lock(attach->dmabuf->resv, NULL);
  1114. dma_buf_unmap_attachment(attach, sg_table, direction);
  1115. dma_resv_unlock(attach->dmabuf->resv);
  1116. }
  1117. EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_attachment_unlocked, "DMA_BUF");
  1118. /**
  1119. * dma_buf_move_notify - notify attachments that DMA-buf is moving
  1120. *
  1121. * @dmabuf: [in] buffer which is moving
  1122. *
  1123. * Informs all attachments that they need to destroy and recreate all their
  1124. * mappings.
  1125. */
  1126. void dma_buf_move_notify(struct dma_buf *dmabuf)
  1127. {
  1128. struct dma_buf_attachment *attach;
  1129. dma_resv_assert_held(dmabuf->resv);
  1130. list_for_each_entry(attach, &dmabuf->attachments, node)
  1131. if (attach->importer_ops)
  1132. attach->importer_ops->move_notify(attach);
  1133. }
  1134. EXPORT_SYMBOL_NS_GPL(dma_buf_move_notify, "DMA_BUF");
  1135. /**
  1136. * DOC: cpu access
  1137. *
  1138. * There are multiple reasons for supporting CPU access to a dma buffer object:
  1139. *
  1140. * - Fallback operations in the kernel, for example when a device is connected
  1141. * over USB and the kernel needs to shuffle the data around first before
  1142. * sending it away. Cache coherency is handled by bracketing any transactions
  1143. * with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
  1144. * access.
  1145. *
  1146. * Since for most kernel internal dma-buf accesses need the entire buffer, a
  1147. * vmap interface is introduced. Note that on very old 32-bit architectures
  1148. * vmalloc space might be limited and result in vmap calls failing.
  1149. *
  1150. * Interfaces:
  1151. *
  1152. * .. code-block:: c
  1153. *
  1154. * void *dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
  1155. * void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
  1156. *
  1157. * The vmap call can fail if there is no vmap support in the exporter, or if
  1158. * it runs out of vmalloc space. Note that the dma-buf layer keeps a reference
  1159. * count for all vmap access and calls down into the exporter's vmap function
  1160. * only when no vmapping exists, and only unmaps it once. Protection against
  1161. * concurrent vmap/vunmap calls is provided by taking the &dma_buf.lock mutex.
  1162. *
  1163. * - For full compatibility on the importer side with existing userspace
  1164. * interfaces, which might already support mmap'ing buffers. This is needed in
  1165. * many processing pipelines (e.g. feeding a software rendered image into a
  1166. * hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION
  1167. * framework already supported this and for DMA buffer file descriptors to
  1168. * replace ION buffers mmap support was needed.
  1169. *
  1170. * There is no special interfaces, userspace simply calls mmap on the dma-buf
  1171. * fd. But like for CPU access there's a need to bracket the actual access,
  1172. * which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that
  1173. * DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must
  1174. * be restarted.
  1175. *
  1176. * Some systems might need some sort of cache coherency management e.g. when
  1177. * CPU and GPU domains are being accessed through dma-buf at the same time.
  1178. * To circumvent this problem there are begin/end coherency markers, that
  1179. * forward directly to existing dma-buf device drivers vfunc hooks. Userspace
  1180. * can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The
  1181. * sequence would be used like following:
  1182. *
  1183. * - mmap dma-buf fd
  1184. * - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write
  1185. * to mmap area 3. SYNC_END ioctl. This can be repeated as often as you
  1186. * want (with the new data being consumed by say the GPU or the scanout
  1187. * device)
  1188. * - munmap once you don't need the buffer any more
  1189. *
  1190. * For correctness and optimal performance, it is always required to use
  1191. * SYNC_START and SYNC_END before and after, respectively, when accessing the
  1192. * mapped address. Userspace cannot rely on coherent access, even when there
  1193. * are systems where it just works without calling these ioctls.
  1194. *
  1195. * - And as a CPU fallback in userspace processing pipelines.
  1196. *
  1197. * Similar to the motivation for kernel cpu access it is again important that
  1198. * the userspace code of a given importing subsystem can use the same
  1199. * interfaces with a imported dma-buf buffer object as with a native buffer
  1200. * object. This is especially important for drm where the userspace part of
  1201. * contemporary OpenGL, X, and other drivers is huge, and reworking them to
  1202. * use a different way to mmap a buffer rather invasive.
  1203. *
  1204. * The assumption in the current dma-buf interfaces is that redirecting the
  1205. * initial mmap is all that's needed. A survey of some of the existing
  1206. * subsystems shows that no driver seems to do any nefarious thing like
  1207. * syncing up with outstanding asynchronous processing on the device or
  1208. * allocating special resources at fault time. So hopefully this is good
  1209. * enough, since adding interfaces to intercept pagefaults and allow pte
  1210. * shootdowns would increase the complexity quite a bit.
  1211. *
  1212. * Interface:
  1213. *
  1214. * .. code-block:: c
  1215. *
  1216. * int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *, unsigned long);
  1217. *
  1218. * If the importing subsystem simply provides a special-purpose mmap call to
  1219. * set up a mapping in userspace, calling do_mmap with &dma_buf.file will
  1220. * equally achieve that for a dma-buf object.
  1221. */
  1222. static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
  1223. enum dma_data_direction direction)
  1224. {
  1225. bool write = (direction == DMA_BIDIRECTIONAL ||
  1226. direction == DMA_TO_DEVICE);
  1227. struct dma_resv *resv = dmabuf->resv;
  1228. long ret;
  1229. /* Wait on any implicit rendering fences */
  1230. ret = dma_resv_wait_timeout(resv, dma_resv_usage_rw(write),
  1231. true, MAX_SCHEDULE_TIMEOUT);
  1232. if (ret < 0)
  1233. return ret;
  1234. return 0;
  1235. }
  1236. /**
  1237. * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
  1238. * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
  1239. * preparations. Coherency is only guaranteed in the specified range for the
  1240. * specified access direction.
  1241. * @dmabuf: [in] buffer to prepare cpu access for.
  1242. * @direction: [in] direction of access.
  1243. *
  1244. * After the cpu access is complete the caller should call
  1245. * dma_buf_end_cpu_access(). Only when cpu access is bracketed by both calls is
  1246. * it guaranteed to be coherent with other DMA access.
  1247. *
  1248. * This function will also wait for any DMA transactions tracked through
  1249. * implicit synchronization in &dma_buf.resv. For DMA transactions with explicit
  1250. * synchronization this function will only ensure cache coherency, callers must
  1251. * ensure synchronization with such DMA transactions on their own.
  1252. *
  1253. * Can return negative error values, returns 0 on success.
  1254. */
  1255. int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
  1256. enum dma_data_direction direction)
  1257. {
  1258. int ret = 0;
  1259. if (WARN_ON(!dmabuf))
  1260. return -EINVAL;
  1261. might_lock(&dmabuf->resv->lock.base);
  1262. if (dmabuf->ops->begin_cpu_access)
  1263. ret = dmabuf->ops->begin_cpu_access(dmabuf, direction);
  1264. /* Ensure that all fences are waited upon - but we first allow
  1265. * the native handler the chance to do so more efficiently if it
  1266. * chooses. A double invocation here will be reasonably cheap no-op.
  1267. */
  1268. if (ret == 0)
  1269. ret = __dma_buf_begin_cpu_access(dmabuf, direction);
  1270. return ret;
  1271. }
  1272. EXPORT_SYMBOL_NS_GPL(dma_buf_begin_cpu_access, "DMA_BUF");
  1273. /**
  1274. * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
  1275. * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
  1276. * actions. Coherency is only guaranteed in the specified range for the
  1277. * specified access direction.
  1278. * @dmabuf: [in] buffer to complete cpu access for.
  1279. * @direction: [in] direction of access.
  1280. *
  1281. * This terminates CPU access started with dma_buf_begin_cpu_access().
  1282. *
  1283. * Can return negative error values, returns 0 on success.
  1284. */
  1285. int dma_buf_end_cpu_access(struct dma_buf *dmabuf,
  1286. enum dma_data_direction direction)
  1287. {
  1288. int ret = 0;
  1289. WARN_ON(!dmabuf);
  1290. might_lock(&dmabuf->resv->lock.base);
  1291. if (dmabuf->ops->end_cpu_access)
  1292. ret = dmabuf->ops->end_cpu_access(dmabuf, direction);
  1293. return ret;
  1294. }
  1295. EXPORT_SYMBOL_NS_GPL(dma_buf_end_cpu_access, "DMA_BUF");
  1296. /**
  1297. * dma_buf_mmap - Setup up a userspace mmap with the given vma
  1298. * @dmabuf: [in] buffer that should back the vma
  1299. * @vma: [in] vma for the mmap
  1300. * @pgoff: [in] offset in pages where this mmap should start within the
  1301. * dma-buf buffer.
  1302. *
  1303. * This function adjusts the passed in vma so that it points at the file of the
  1304. * dma_buf operation. It also adjusts the starting pgoff and does bounds
  1305. * checking on the size of the vma. Then it calls the exporters mmap function to
  1306. * set up the mapping.
  1307. *
  1308. * Can return negative error values, returns 0 on success.
  1309. */
  1310. int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
  1311. unsigned long pgoff)
  1312. {
  1313. if (WARN_ON(!dmabuf || !vma))
  1314. return -EINVAL;
  1315. /* check if buffer supports mmap */
  1316. if (!dmabuf->ops->mmap)
  1317. return -EINVAL;
  1318. /* check for offset overflow */
  1319. if (pgoff + vma_pages(vma) < pgoff)
  1320. return -EOVERFLOW;
  1321. /* check for overflowing the buffer's size */
  1322. if (pgoff + vma_pages(vma) >
  1323. dmabuf->size >> PAGE_SHIFT)
  1324. return -EINVAL;
  1325. /* readjust the vma */
  1326. vma_set_file(vma, dmabuf->file);
  1327. vma->vm_pgoff = pgoff;
  1328. DMA_BUF_TRACE(trace_dma_buf_mmap, dmabuf);
  1329. return dmabuf->ops->mmap(dmabuf, vma);
  1330. }
  1331. EXPORT_SYMBOL_NS_GPL(dma_buf_mmap, "DMA_BUF");
  1332. /**
  1333. * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
  1334. * address space. Same restrictions as for vmap and friends apply.
  1335. * @dmabuf: [in] buffer to vmap
  1336. * @map: [out] returns the vmap pointer
  1337. *
  1338. * This call may fail due to lack of virtual mapping address space.
  1339. * These calls are optional in drivers. The intended use for them
  1340. * is for mapping objects linear in kernel space for high use objects.
  1341. *
  1342. * To ensure coherency users must call dma_buf_begin_cpu_access() and
  1343. * dma_buf_end_cpu_access() around any cpu access performed through this
  1344. * mapping.
  1345. *
  1346. * Returns 0 on success, or a negative errno code otherwise.
  1347. */
  1348. int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
  1349. {
  1350. struct iosys_map ptr;
  1351. int ret;
  1352. iosys_map_clear(map);
  1353. if (WARN_ON(!dmabuf))
  1354. return -EINVAL;
  1355. dma_resv_assert_held(dmabuf->resv);
  1356. if (!dmabuf->ops->vmap)
  1357. return -EINVAL;
  1358. if (dmabuf->vmapping_counter) {
  1359. dmabuf->vmapping_counter++;
  1360. BUG_ON(iosys_map_is_null(&dmabuf->vmap_ptr));
  1361. *map = dmabuf->vmap_ptr;
  1362. return 0;
  1363. }
  1364. BUG_ON(iosys_map_is_set(&dmabuf->vmap_ptr));
  1365. ret = dmabuf->ops->vmap(dmabuf, &ptr);
  1366. if (WARN_ON_ONCE(ret))
  1367. return ret;
  1368. dmabuf->vmap_ptr = ptr;
  1369. dmabuf->vmapping_counter = 1;
  1370. *map = dmabuf->vmap_ptr;
  1371. return 0;
  1372. }
  1373. EXPORT_SYMBOL_NS_GPL(dma_buf_vmap, "DMA_BUF");
  1374. /**
  1375. * dma_buf_vmap_unlocked - Create virtual mapping for the buffer object into kernel
  1376. * address space. Same restrictions as for vmap and friends apply.
  1377. * @dmabuf: [in] buffer to vmap
  1378. * @map: [out] returns the vmap pointer
  1379. *
  1380. * Unlocked version of dma_buf_vmap()
  1381. *
  1382. * Returns 0 on success, or a negative errno code otherwise.
  1383. */
  1384. int dma_buf_vmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map)
  1385. {
  1386. int ret;
  1387. iosys_map_clear(map);
  1388. if (WARN_ON(!dmabuf))
  1389. return -EINVAL;
  1390. dma_resv_lock(dmabuf->resv, NULL);
  1391. ret = dma_buf_vmap(dmabuf, map);
  1392. dma_resv_unlock(dmabuf->resv);
  1393. return ret;
  1394. }
  1395. EXPORT_SYMBOL_NS_GPL(dma_buf_vmap_unlocked, "DMA_BUF");
  1396. /**
  1397. * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
  1398. * @dmabuf: [in] buffer to vunmap
  1399. * @map: [in] vmap pointer to vunmap
  1400. */
  1401. void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
  1402. {
  1403. if (WARN_ON(!dmabuf))
  1404. return;
  1405. dma_resv_assert_held(dmabuf->resv);
  1406. BUG_ON(iosys_map_is_null(&dmabuf->vmap_ptr));
  1407. BUG_ON(dmabuf->vmapping_counter == 0);
  1408. BUG_ON(!iosys_map_is_equal(&dmabuf->vmap_ptr, map));
  1409. if (--dmabuf->vmapping_counter == 0) {
  1410. if (dmabuf->ops->vunmap)
  1411. dmabuf->ops->vunmap(dmabuf, map);
  1412. iosys_map_clear(&dmabuf->vmap_ptr);
  1413. }
  1414. }
  1415. EXPORT_SYMBOL_NS_GPL(dma_buf_vunmap, "DMA_BUF");
  1416. /**
  1417. * dma_buf_vunmap_unlocked - Unmap a vmap obtained by dma_buf_vmap.
  1418. * @dmabuf: [in] buffer to vunmap
  1419. * @map: [in] vmap pointer to vunmap
  1420. */
  1421. void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map)
  1422. {
  1423. if (WARN_ON(!dmabuf))
  1424. return;
  1425. dma_resv_lock(dmabuf->resv, NULL);
  1426. dma_buf_vunmap(dmabuf, map);
  1427. dma_resv_unlock(dmabuf->resv);
  1428. }
  1429. EXPORT_SYMBOL_NS_GPL(dma_buf_vunmap_unlocked, "DMA_BUF");
  1430. #ifdef CONFIG_DEBUG_FS
  1431. static int dma_buf_debug_show(struct seq_file *s, void *unused)
  1432. {
  1433. struct dma_buf *buf_obj;
  1434. struct dma_buf_attachment *attach_obj;
  1435. int count = 0, attach_count;
  1436. size_t size = 0;
  1437. int ret;
  1438. ret = mutex_lock_interruptible(&dmabuf_list_mutex);
  1439. if (ret)
  1440. return ret;
  1441. seq_puts(s, "\nDma-buf Objects:\n");
  1442. seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\tname\n",
  1443. "size", "flags", "mode", "count", "ino");
  1444. list_for_each_entry(buf_obj, &dmabuf_list, list_node) {
  1445. ret = dma_resv_lock_interruptible(buf_obj->resv, NULL);
  1446. if (ret)
  1447. goto error_unlock;
  1448. spin_lock(&buf_obj->name_lock);
  1449. seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
  1450. buf_obj->size,
  1451. buf_obj->file->f_flags, buf_obj->file->f_mode,
  1452. file_count(buf_obj->file),
  1453. buf_obj->exp_name,
  1454. file_inode(buf_obj->file)->i_ino,
  1455. buf_obj->name ?: "<none>");
  1456. spin_unlock(&buf_obj->name_lock);
  1457. dma_resv_describe(buf_obj->resv, s);
  1458. seq_puts(s, "\tAttached Devices:\n");
  1459. attach_count = 0;
  1460. list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
  1461. seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
  1462. attach_count++;
  1463. }
  1464. dma_resv_unlock(buf_obj->resv);
  1465. seq_printf(s, "Total %d devices attached\n\n",
  1466. attach_count);
  1467. count++;
  1468. size += buf_obj->size;
  1469. }
  1470. seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
  1471. mutex_unlock(&dmabuf_list_mutex);
  1472. return 0;
  1473. error_unlock:
  1474. mutex_unlock(&dmabuf_list_mutex);
  1475. return ret;
  1476. }
  1477. DEFINE_SHOW_ATTRIBUTE(dma_buf_debug);
  1478. static struct dentry *dma_buf_debugfs_dir;
  1479. static int dma_buf_init_debugfs(void)
  1480. {
  1481. struct dentry *d;
  1482. int err = 0;
  1483. d = debugfs_create_dir("dma_buf", NULL);
  1484. if (IS_ERR(d))
  1485. return PTR_ERR(d);
  1486. dma_buf_debugfs_dir = d;
  1487. d = debugfs_create_file("bufinfo", 0444, dma_buf_debugfs_dir,
  1488. NULL, &dma_buf_debug_fops);
  1489. if (IS_ERR(d)) {
  1490. pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
  1491. debugfs_remove_recursive(dma_buf_debugfs_dir);
  1492. dma_buf_debugfs_dir = NULL;
  1493. err = PTR_ERR(d);
  1494. }
  1495. return err;
  1496. }
  1497. static void dma_buf_uninit_debugfs(void)
  1498. {
  1499. debugfs_remove_recursive(dma_buf_debugfs_dir);
  1500. }
  1501. #else
  1502. static inline int dma_buf_init_debugfs(void)
  1503. {
  1504. return 0;
  1505. }
  1506. static inline void dma_buf_uninit_debugfs(void)
  1507. {
  1508. }
  1509. #endif
  1510. static int __init dma_buf_init(void)
  1511. {
  1512. dma_buf_mnt = kern_mount(&dma_buf_fs_type);
  1513. if (IS_ERR(dma_buf_mnt))
  1514. return PTR_ERR(dma_buf_mnt);
  1515. dma_buf_init_debugfs();
  1516. return 0;
  1517. }
  1518. subsys_initcall(dma_buf_init);
  1519. static void __exit dma_buf_deinit(void)
  1520. {
  1521. dma_buf_uninit_debugfs();
  1522. kern_unmount(dma_buf_mnt);
  1523. }
  1524. __exitcall(dma_buf_deinit);