1 // SPDX-License-Identifier: GPL-2.0-only
3 * Framework for buffer objects that can be shared across devices/subsystems.
5 * Copyright(C) 2011 Linaro Limited. All rights reserved.
8 * Many thanks to linaro-mm-sig list, and specially
11 * refining of this idea.
15 #include <linux/slab.h>
16 #include <linux/dma-buf.h>
17 #include <linux/dma-fence.h>
18 #include <linux/dma-fence-unwrap.h>
19 #include <linux/anon_inodes.h>
20 #include <linux/export.h>
21 #include <linux/debugfs.h>
22 #include <linux/module.h>
23 #include <linux/seq_file.h>
24 #include <linux/sync_file.h>
25 #include <linux/poll.h>
26 #include <linux/dma-resv.h>
28 #include <linux/mount.h>
29 #include <linux/pseudo_fs.h>
31 #include <uapi/linux/dma-buf.h>
32 #include <uapi/linux/magic.h>
34 #include "dma-buf-sysfs-stats.h"
36 static inline int is_dma_buf_file(struct file *);
39 struct list_head head;
43 static struct dma_buf_list db_list;
45 static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen)
47 struct dma_buf *dmabuf;
48 char name[DMA_BUF_NAME_LEN];
51 dmabuf = dentry->d_fsdata;
52 spin_lock(&dmabuf->name_lock);
54 ret = strlcpy(name, dmabuf->name, DMA_BUF_NAME_LEN);
55 spin_unlock(&dmabuf->name_lock);
57 return dynamic_dname(buffer, buflen, "/%s:%s",
58 dentry->d_name.name, ret > 0 ? name : "");
61 static void dma_buf_release(struct dentry *dentry)
63 struct dma_buf *dmabuf;
65 dmabuf = dentry->d_fsdata;
66 if (unlikely(!dmabuf))
69 BUG_ON(dmabuf->vmapping_counter);
72 * If you hit this BUG() it could mean:
73 * * There's a file reference imbalance in dma_buf_poll / dma_buf_poll_cb or somewhere else
74 * * dmabuf->cb_in/out.active are non-0 despite no pending fence callback
76 BUG_ON(dmabuf->cb_in.active || dmabuf->cb_out.active);
78 dma_buf_stats_teardown(dmabuf);
79 dmabuf->ops->release(dmabuf);
81 if (dmabuf->resv == (struct dma_resv *)&dmabuf[1])
82 dma_resv_fini(dmabuf->resv);
84 WARN_ON(!list_empty(&dmabuf->attachments));
85 module_put(dmabuf->owner);
90 static int dma_buf_file_release(struct inode *inode, struct file *file)
92 struct dma_buf *dmabuf;
94 if (!is_dma_buf_file(file))
97 dmabuf = file->private_data;
99 mutex_lock(&db_list.lock);
100 list_del(&dmabuf->list_node);
101 mutex_unlock(&db_list.lock);
107 static const struct dentry_operations dma_buf_dentry_ops = {
108 .d_dname = dmabuffs_dname,
109 .d_release = dma_buf_release,
112 static struct vfsmount *dma_buf_mnt;
114 static int dma_buf_fs_init_context(struct fs_context *fc)
116 struct pseudo_fs_context *ctx;
118 ctx = init_pseudo(fc, DMA_BUF_MAGIC);
121 ctx->dops = &dma_buf_dentry_ops;
125 static struct file_system_type dma_buf_fs_type = {
127 .init_fs_context = dma_buf_fs_init_context,
128 .kill_sb = kill_anon_super,
131 static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
133 struct dma_buf *dmabuf;
136 if (!is_dma_buf_file(file))
139 dmabuf = file->private_data;
141 /* check if buffer supports mmap */
142 if (!dmabuf->ops->mmap)
145 /* check for overflowing the buffer's size */
146 if (vma->vm_pgoff + vma_pages(vma) >
147 dmabuf->size >> PAGE_SHIFT)
150 dma_resv_lock(dmabuf->resv, NULL);
151 ret = dmabuf->ops->mmap(dmabuf, vma);
152 dma_resv_unlock(dmabuf->resv);
157 static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
159 struct dma_buf *dmabuf;
162 if (!is_dma_buf_file(file))
165 dmabuf = file->private_data;
167 /* only support discovering the end of the buffer,
168 but also allow SEEK_SET to maintain the idiomatic
169 SEEK_END(0), SEEK_CUR(0) pattern */
170 if (whence == SEEK_END)
172 else if (whence == SEEK_SET)
180 return base + offset;
184 * DOC: implicit fence polling
186 * To support cross-device and cross-driver synchronization of buffer access
187 * implicit fences (represented internally in the kernel with &struct dma_fence)
188 * can be attached to a &dma_buf. The glue for that and a few related things are
189 * provided in the &dma_resv structure.
191 * Userspace can query the state of these implicitly tracked fences using poll()
192 * and related system calls:
194 * - Checking for EPOLLIN, i.e. read access, can be use to query the state of the
195 * most recent write or exclusive fence.
197 * - Checking for EPOLLOUT, i.e. write access, can be used to query the state of
198 * all attached fences, shared and exclusive ones.
200 * Note that this only signals the completion of the respective fences, i.e. the
201 * DMA transfers are complete. Cache flushing and any other necessary
202 * preparations before CPU access can begin still need to happen.
204 * As an alternative to poll(), the set of fences on DMA buffer can be
205 * exported as a &sync_file using &dma_buf_sync_file_export.
208 static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
210 struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb;
211 struct dma_buf *dmabuf = container_of(dcb->poll, struct dma_buf, poll);
214 spin_lock_irqsave(&dcb->poll->lock, flags);
215 wake_up_locked_poll(dcb->poll, dcb->active);
217 spin_unlock_irqrestore(&dcb->poll->lock, flags);
218 dma_fence_put(fence);
219 /* Paired with get_file in dma_buf_poll */
223 static bool dma_buf_poll_add_cb(struct dma_resv *resv, bool write,
224 struct dma_buf_poll_cb_t *dcb)
226 struct dma_resv_iter cursor;
227 struct dma_fence *fence;
230 dma_resv_for_each_fence(&cursor, resv, dma_resv_usage_rw(write),
232 dma_fence_get(fence);
233 r = dma_fence_add_callback(fence, &dcb->cb, dma_buf_poll_cb);
236 dma_fence_put(fence);
242 static __poll_t dma_buf_poll(struct file *file, poll_table *poll)
244 struct dma_buf *dmabuf;
245 struct dma_resv *resv;
248 dmabuf = file->private_data;
249 if (!dmabuf || !dmabuf->resv)
254 poll_wait(file, &dmabuf->poll, poll);
256 events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT);
260 dma_resv_lock(resv, NULL);
262 if (events & EPOLLOUT) {
263 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_out;
265 /* Check that callback isn't busy */
266 spin_lock_irq(&dmabuf->poll.lock);
270 dcb->active = EPOLLOUT;
271 spin_unlock_irq(&dmabuf->poll.lock);
273 if (events & EPOLLOUT) {
274 /* Paired with fput in dma_buf_poll_cb */
275 get_file(dmabuf->file);
277 if (!dma_buf_poll_add_cb(resv, true, dcb))
278 /* No callback queued, wake up any other waiters */
279 dma_buf_poll_cb(NULL, &dcb->cb);
285 if (events & EPOLLIN) {
286 struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_in;
288 /* Check that callback isn't busy */
289 spin_lock_irq(&dmabuf->poll.lock);
293 dcb->active = EPOLLIN;
294 spin_unlock_irq(&dmabuf->poll.lock);
296 if (events & EPOLLIN) {
297 /* Paired with fput in dma_buf_poll_cb */
298 get_file(dmabuf->file);
300 if (!dma_buf_poll_add_cb(resv, false, dcb))
301 /* No callback queued, wake up any other waiters */
302 dma_buf_poll_cb(NULL, &dcb->cb);
308 dma_resv_unlock(resv);
313 * dma_buf_set_name - Set a name to a specific dma_buf to track the usage.
314 * It could support changing the name of the dma-buf if the same
315 * piece of memory is used for multiple purpose between different devices.
317 * @dmabuf: [in] dmabuf buffer that will be renamed.
318 * @buf: [in] A piece of userspace memory that contains the name of
321 * Returns 0 on success. If the dma-buf buffer is already attached to
322 * devices, return -EBUSY.
325 static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf)
327 char *name = strndup_user(buf, DMA_BUF_NAME_LEN);
330 return PTR_ERR(name);
332 spin_lock(&dmabuf->name_lock);
335 spin_unlock(&dmabuf->name_lock);
340 #if IS_ENABLED(CONFIG_SYNC_FILE)
341 static long dma_buf_export_sync_file(struct dma_buf *dmabuf,
342 void __user *user_data)
344 struct dma_buf_export_sync_file arg;
345 enum dma_resv_usage usage;
346 struct dma_fence *fence = NULL;
347 struct sync_file *sync_file;
350 if (copy_from_user(&arg, user_data, sizeof(arg)))
353 if (arg.flags & ~DMA_BUF_SYNC_RW)
356 if ((arg.flags & DMA_BUF_SYNC_RW) == 0)
359 fd = get_unused_fd_flags(O_CLOEXEC);
363 usage = dma_resv_usage_rw(arg.flags & DMA_BUF_SYNC_WRITE);
364 ret = dma_resv_get_singleton(dmabuf->resv, usage, &fence);
369 fence = dma_fence_get_stub();
371 sync_file = sync_file_create(fence);
373 dma_fence_put(fence);
381 if (copy_to_user(user_data, &arg, sizeof(arg))) {
386 fd_install(fd, sync_file->file);
391 fput(sync_file->file);
397 static long dma_buf_import_sync_file(struct dma_buf *dmabuf,
398 const void __user *user_data)
400 struct dma_buf_import_sync_file arg;
401 struct dma_fence *fence, *f;
402 enum dma_resv_usage usage;
403 struct dma_fence_unwrap iter;
404 unsigned int num_fences;
407 if (copy_from_user(&arg, user_data, sizeof(arg)))
410 if (arg.flags & ~DMA_BUF_SYNC_RW)
413 if ((arg.flags & DMA_BUF_SYNC_RW) == 0)
416 fence = sync_file_get_fence(arg.fd);
420 usage = (arg.flags & DMA_BUF_SYNC_WRITE) ? DMA_RESV_USAGE_WRITE :
424 dma_fence_unwrap_for_each(f, &iter, fence)
427 if (num_fences > 0) {
428 dma_resv_lock(dmabuf->resv, NULL);
430 ret = dma_resv_reserve_fences(dmabuf->resv, num_fences);
432 dma_fence_unwrap_for_each(f, &iter, fence)
433 dma_resv_add_fence(dmabuf->resv, f, usage);
436 dma_resv_unlock(dmabuf->resv);
439 dma_fence_put(fence);
445 static long dma_buf_ioctl(struct file *file,
446 unsigned int cmd, unsigned long arg)
448 struct dma_buf *dmabuf;
449 struct dma_buf_sync sync;
450 enum dma_data_direction direction;
453 dmabuf = file->private_data;
456 case DMA_BUF_IOCTL_SYNC:
457 if (copy_from_user(&sync, (void __user *) arg, sizeof(sync)))
460 if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK)
463 switch (sync.flags & DMA_BUF_SYNC_RW) {
464 case DMA_BUF_SYNC_READ:
465 direction = DMA_FROM_DEVICE;
467 case DMA_BUF_SYNC_WRITE:
468 direction = DMA_TO_DEVICE;
470 case DMA_BUF_SYNC_RW:
471 direction = DMA_BIDIRECTIONAL;
477 if (sync.flags & DMA_BUF_SYNC_END)
478 ret = dma_buf_end_cpu_access(dmabuf, direction);
480 ret = dma_buf_begin_cpu_access(dmabuf, direction);
484 case DMA_BUF_SET_NAME_A:
485 case DMA_BUF_SET_NAME_B:
486 return dma_buf_set_name(dmabuf, (const char __user *)arg);
488 #if IS_ENABLED(CONFIG_SYNC_FILE)
489 case DMA_BUF_IOCTL_EXPORT_SYNC_FILE:
490 return dma_buf_export_sync_file(dmabuf, (void __user *)arg);
491 case DMA_BUF_IOCTL_IMPORT_SYNC_FILE:
492 return dma_buf_import_sync_file(dmabuf, (const void __user *)arg);
500 static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file)
502 struct dma_buf *dmabuf = file->private_data;
504 seq_printf(m, "size:\t%zu\n", dmabuf->size);
505 /* Don't count the temporary reference taken inside procfs seq_show */
506 seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1);
507 seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name);
508 spin_lock(&dmabuf->name_lock);
510 seq_printf(m, "name:\t%s\n", dmabuf->name);
511 spin_unlock(&dmabuf->name_lock);
514 static const struct file_operations dma_buf_fops = {
515 .release = dma_buf_file_release,
516 .mmap = dma_buf_mmap_internal,
517 .llseek = dma_buf_llseek,
518 .poll = dma_buf_poll,
519 .unlocked_ioctl = dma_buf_ioctl,
520 .compat_ioctl = compat_ptr_ioctl,
521 .show_fdinfo = dma_buf_show_fdinfo,
525 * is_dma_buf_file - Check if struct file* is associated with dma_buf
527 static inline int is_dma_buf_file(struct file *file)
529 return file->f_op == &dma_buf_fops;
532 static struct file *dma_buf_getfile(size_t size, int flags)
534 static atomic64_t dmabuf_inode = ATOMIC64_INIT(0);
535 struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb);
539 return ERR_CAST(inode);
541 inode->i_size = size;
542 inode_set_bytes(inode, size);
545 * The ->i_ino acquired from get_next_ino() is not unique thus
546 * not suitable for using it as dentry name by dmabuf stats.
547 * Override ->i_ino with the unique and dmabuffs specific
550 inode->i_ino = atomic64_add_return(1, &dmabuf_inode);
551 flags &= O_ACCMODE | O_NONBLOCK;
552 file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf",
553 flags, &dma_buf_fops);
565 * DOC: dma buf device access
567 * For device DMA access to a shared DMA buffer the usual sequence of operations
570 * 1. The exporter defines his exporter instance using
571 * DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private
572 * buffer object into a &dma_buf. It then exports that &dma_buf to userspace
573 * as a file descriptor by calling dma_buf_fd().
575 * 2. Userspace passes this file-descriptors to all drivers it wants this buffer
576 * to share with: First the file descriptor is converted to a &dma_buf using
577 * dma_buf_get(). Then the buffer is attached to the device using
580 * Up to this stage the exporter is still free to migrate or reallocate the
583 * 3. Once the buffer is attached to all devices userspace can initiate DMA
584 * access to the shared buffer. In the kernel this is done by calling
585 * dma_buf_map_attachment() and dma_buf_unmap_attachment().
587 * 4. Once a driver is done with a shared buffer it needs to call
588 * dma_buf_detach() (after cleaning up any mappings) and then release the
589 * reference acquired with dma_buf_get() by calling dma_buf_put().
591 * For the detailed semantics exporters are expected to implement see
596 * dma_buf_export - Creates a new dma_buf, and associates an anon file
597 * with this buffer, so it can be exported.
598 * Also connect the allocator specific data and ops to the buffer.
599 * Additionally, provide a name string for exporter; useful in debugging.
601 * @exp_info: [in] holds all the export related information provided
602 * by the exporter. see &struct dma_buf_export_info
603 * for further details.
605 * Returns, on success, a newly created struct dma_buf object, which wraps the
606 * supplied private data and operations for struct dma_buf_ops. On either
607 * missing ops, or error in allocating struct dma_buf, will return negative
610 * For most cases the easiest way to create @exp_info is through the
611 * %DEFINE_DMA_BUF_EXPORT_INFO macro.
613 struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
615 struct dma_buf *dmabuf;
616 struct dma_resv *resv = exp_info->resv;
618 size_t alloc_size = sizeof(struct dma_buf);
621 if (WARN_ON(!exp_info->priv || !exp_info->ops
622 || !exp_info->ops->map_dma_buf
623 || !exp_info->ops->unmap_dma_buf
624 || !exp_info->ops->release))
625 return ERR_PTR(-EINVAL);
627 if (WARN_ON(exp_info->ops->cache_sgt_mapping &&
628 (exp_info->ops->pin || exp_info->ops->unpin)))
629 return ERR_PTR(-EINVAL);
631 if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
632 return ERR_PTR(-EINVAL);
634 if (!try_module_get(exp_info->owner))
635 return ERR_PTR(-ENOENT);
637 file = dma_buf_getfile(exp_info->size, exp_info->flags);
644 alloc_size += sizeof(struct dma_resv);
646 /* prevent &dma_buf[1] == dma_buf->resv */
648 dmabuf = kzalloc(alloc_size, GFP_KERNEL);
654 dmabuf->priv = exp_info->priv;
655 dmabuf->ops = exp_info->ops;
656 dmabuf->size = exp_info->size;
657 dmabuf->exp_name = exp_info->exp_name;
658 dmabuf->owner = exp_info->owner;
659 spin_lock_init(&dmabuf->name_lock);
660 init_waitqueue_head(&dmabuf->poll);
661 dmabuf->cb_in.poll = dmabuf->cb_out.poll = &dmabuf->poll;
662 dmabuf->cb_in.active = dmabuf->cb_out.active = 0;
663 INIT_LIST_HEAD(&dmabuf->attachments);
666 dmabuf->resv = (struct dma_resv *)&dmabuf[1];
667 dma_resv_init(dmabuf->resv);
672 ret = dma_buf_stats_setup(dmabuf, file);
676 file->private_data = dmabuf;
677 file->f_path.dentry->d_fsdata = dmabuf;
680 mutex_lock(&db_list.lock);
681 list_add(&dmabuf->list_node, &db_list.head);
682 mutex_unlock(&db_list.lock);
688 dma_resv_fini(dmabuf->resv);
693 module_put(exp_info->owner);
696 EXPORT_SYMBOL_NS_GPL(dma_buf_export, DMA_BUF);
699 * dma_buf_fd - returns a file descriptor for the given struct dma_buf
700 * @dmabuf: [in] pointer to dma_buf for which fd is required.
701 * @flags: [in] flags to give to fd
703 * On success, returns an associated 'fd'. Else, returns error.
705 int dma_buf_fd(struct dma_buf *dmabuf, int flags)
709 if (!dmabuf || !dmabuf->file)
712 fd = get_unused_fd_flags(flags);
716 fd_install(fd, dmabuf->file);
720 EXPORT_SYMBOL_NS_GPL(dma_buf_fd, DMA_BUF);
723 * dma_buf_get - returns the struct dma_buf related to an fd
724 * @fd: [in] fd associated with the struct dma_buf to be returned
726 * On success, returns the struct dma_buf associated with an fd; uses
727 * file's refcounting done by fget to increase refcount. returns ERR_PTR
730 struct dma_buf *dma_buf_get(int fd)
737 return ERR_PTR(-EBADF);
739 if (!is_dma_buf_file(file)) {
741 return ERR_PTR(-EINVAL);
744 return file->private_data;
746 EXPORT_SYMBOL_NS_GPL(dma_buf_get, DMA_BUF);
749 * dma_buf_put - decreases refcount of the buffer
750 * @dmabuf: [in] buffer to reduce refcount of
752 * Uses file's refcounting done implicitly by fput().
754 * If, as a result of this call, the refcount becomes 0, the 'release' file
755 * operation related to this fd is called. It calls &dma_buf_ops.release vfunc
756 * in turn, and frees the memory allocated for dmabuf when exported.
758 void dma_buf_put(struct dma_buf *dmabuf)
760 if (WARN_ON(!dmabuf || !dmabuf->file))
765 EXPORT_SYMBOL_NS_GPL(dma_buf_put, DMA_BUF);
767 static void mangle_sg_table(struct sg_table *sg_table)
769 #ifdef CONFIG_DMABUF_DEBUG
771 struct scatterlist *sg;
773 /* To catch abuse of the underlying struct page by importers mix
774 * up the bits, but take care to preserve the low SG_ bits to
775 * not corrupt the sgt. The mixing is undone in __unmap_dma_buf
776 * before passing the sgt back to the exporter. */
777 for_each_sgtable_sg(sg_table, sg, i)
778 sg->page_link ^= ~0xffUL;
782 static struct sg_table * __map_dma_buf(struct dma_buf_attachment *attach,
783 enum dma_data_direction direction)
785 struct sg_table *sg_table;
788 sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
789 if (IS_ERR_OR_NULL(sg_table))
792 if (!dma_buf_attachment_is_dynamic(attach)) {
793 ret = dma_resv_wait_timeout(attach->dmabuf->resv,
794 DMA_RESV_USAGE_KERNEL, true,
795 MAX_SCHEDULE_TIMEOUT);
797 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
803 mangle_sg_table(sg_table);
808 * DOC: locking convention
810 * In order to avoid deadlock situations between dma-buf exports and importers,
811 * all dma-buf API users must follow the common dma-buf locking convention.
813 * Convention for importers
815 * 1. Importers must hold the dma-buf reservation lock when calling these
820 * - dma_buf_map_attachment()
821 * - dma_buf_unmap_attachment()
825 * 2. Importers must not hold the dma-buf reservation lock when calling these
829 * - dma_buf_dynamic_attach()
836 * - dma_buf_begin_cpu_access()
837 * - dma_buf_end_cpu_access()
838 * - dma_buf_map_attachment_unlocked()
839 * - dma_buf_unmap_attachment_unlocked()
840 * - dma_buf_vmap_unlocked()
841 * - dma_buf_vunmap_unlocked()
843 * Convention for exporters
845 * 1. These &dma_buf_ops callbacks are invoked with unlocked dma-buf
846 * reservation and exporter can take the lock:
848 * - &dma_buf_ops.attach()
849 * - &dma_buf_ops.detach()
850 * - &dma_buf_ops.release()
851 * - &dma_buf_ops.begin_cpu_access()
852 * - &dma_buf_ops.end_cpu_access()
854 * 2. These &dma_buf_ops callbacks are invoked with locked dma-buf
855 * reservation and exporter can't take the lock:
857 * - &dma_buf_ops.pin()
858 * - &dma_buf_ops.unpin()
859 * - &dma_buf_ops.map_dma_buf()
860 * - &dma_buf_ops.unmap_dma_buf()
861 * - &dma_buf_ops.mmap()
862 * - &dma_buf_ops.vmap()
863 * - &dma_buf_ops.vunmap()
865 * 3. Exporters must hold the dma-buf reservation lock when calling these
868 * - dma_buf_move_notify()
872 * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list
873 * @dmabuf: [in] buffer to attach device to.
874 * @dev: [in] device to be attached.
875 * @importer_ops: [in] importer operations for the attachment
876 * @importer_priv: [in] importer private pointer for the attachment
878 * Returns struct dma_buf_attachment pointer for this attachment. Attachments
879 * must be cleaned up by calling dma_buf_detach().
881 * Optionally this calls &dma_buf_ops.attach to allow device-specific attach
886 * A pointer to newly created &dma_buf_attachment on success, or a negative
887 * error code wrapped into a pointer on failure.
889 * Note that this can fail if the backing storage of @dmabuf is in a place not
890 * accessible to @dev, and cannot be moved to a more suitable place. This is
891 * indicated with the error code -EBUSY.
893 struct dma_buf_attachment *
894 dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
895 const struct dma_buf_attach_ops *importer_ops,
898 struct dma_buf_attachment *attach;
901 if (WARN_ON(!dmabuf || !dev))
902 return ERR_PTR(-EINVAL);
904 if (WARN_ON(importer_ops && !importer_ops->move_notify))
905 return ERR_PTR(-EINVAL);
907 attach = kzalloc(sizeof(*attach), GFP_KERNEL);
909 return ERR_PTR(-ENOMEM);
912 attach->dmabuf = dmabuf;
914 attach->peer2peer = importer_ops->allow_peer2peer;
915 attach->importer_ops = importer_ops;
916 attach->importer_priv = importer_priv;
918 if (dmabuf->ops->attach) {
919 ret = dmabuf->ops->attach(dmabuf, attach);
923 dma_resv_lock(dmabuf->resv, NULL);
924 list_add(&attach->node, &dmabuf->attachments);
925 dma_resv_unlock(dmabuf->resv);
927 /* When either the importer or the exporter can't handle dynamic
928 * mappings we cache the mapping here to avoid issues with the
929 * reservation object lock.
931 if (dma_buf_attachment_is_dynamic(attach) !=
932 dma_buf_is_dynamic(dmabuf)) {
933 struct sg_table *sgt;
935 dma_resv_lock(attach->dmabuf->resv, NULL);
936 if (dma_buf_is_dynamic(attach->dmabuf)) {
937 ret = dmabuf->ops->pin(attach);
942 sgt = __map_dma_buf(attach, DMA_BIDIRECTIONAL);
944 sgt = ERR_PTR(-ENOMEM);
949 dma_resv_unlock(attach->dmabuf->resv);
951 attach->dir = DMA_BIDIRECTIONAL;
961 if (dma_buf_is_dynamic(attach->dmabuf))
962 dmabuf->ops->unpin(attach);
965 dma_resv_unlock(attach->dmabuf->resv);
967 dma_buf_detach(dmabuf, attach);
970 EXPORT_SYMBOL_NS_GPL(dma_buf_dynamic_attach, DMA_BUF);
973 * dma_buf_attach - Wrapper for dma_buf_dynamic_attach
974 * @dmabuf: [in] buffer to attach device to.
975 * @dev: [in] device to be attached.
977 * Wrapper to call dma_buf_dynamic_attach() for drivers which still use a static
980 struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
983 return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL);
985 EXPORT_SYMBOL_NS_GPL(dma_buf_attach, DMA_BUF);
987 static void __unmap_dma_buf(struct dma_buf_attachment *attach,
988 struct sg_table *sg_table,
989 enum dma_data_direction direction)
991 /* uses XOR, hence this unmangles */
992 mangle_sg_table(sg_table);
994 attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
998 * dma_buf_detach - Remove the given attachment from dmabuf's attachments list
999 * @dmabuf: [in] buffer to detach from.
1000 * @attach: [in] attachment to be detached; is free'd after this call.
1002 * Clean up a device attachment obtained by calling dma_buf_attach().
1004 * Optionally this calls &dma_buf_ops.detach for device-specific detach.
1006 void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
1008 if (WARN_ON(!dmabuf || !attach || dmabuf != attach->dmabuf))
1011 dma_resv_lock(dmabuf->resv, NULL);
1015 __unmap_dma_buf(attach, attach->sgt, attach->dir);
1017 if (dma_buf_is_dynamic(attach->dmabuf))
1018 dmabuf->ops->unpin(attach);
1020 list_del(&attach->node);
1022 dma_resv_unlock(dmabuf->resv);
1024 if (dmabuf->ops->detach)
1025 dmabuf->ops->detach(dmabuf, attach);
1029 EXPORT_SYMBOL_NS_GPL(dma_buf_detach, DMA_BUF);
1032 * dma_buf_pin - Lock down the DMA-buf
1033 * @attach: [in] attachment which should be pinned
1035 * Only dynamic importers (who set up @attach with dma_buf_dynamic_attach()) may
1036 * call this, and only for limited use cases like scanout and not for temporary
1037 * pin operations. It is not permitted to allow userspace to pin arbitrary
1038 * amounts of buffers through this interface.
1040 * Buffers must be unpinned by calling dma_buf_unpin().
1043 * 0 on success, negative error code on failure.
1045 int dma_buf_pin(struct dma_buf_attachment *attach)
1047 struct dma_buf *dmabuf = attach->dmabuf;
1050 WARN_ON(!dma_buf_attachment_is_dynamic(attach));
1052 dma_resv_assert_held(dmabuf->resv);
1054 if (dmabuf->ops->pin)
1055 ret = dmabuf->ops->pin(attach);
1059 EXPORT_SYMBOL_NS_GPL(dma_buf_pin, DMA_BUF);
1062 * dma_buf_unpin - Unpin a DMA-buf
1063 * @attach: [in] attachment which should be unpinned
1065 * This unpins a buffer pinned by dma_buf_pin() and allows the exporter to move
1066 * any mapping of @attach again and inform the importer through
1067 * &dma_buf_attach_ops.move_notify.
1069 void dma_buf_unpin(struct dma_buf_attachment *attach)
1071 struct dma_buf *dmabuf = attach->dmabuf;
1073 WARN_ON(!dma_buf_attachment_is_dynamic(attach));
1075 dma_resv_assert_held(dmabuf->resv);
1077 if (dmabuf->ops->unpin)
1078 dmabuf->ops->unpin(attach);
1080 EXPORT_SYMBOL_NS_GPL(dma_buf_unpin, DMA_BUF);
1083 * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
1084 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
1086 * @attach: [in] attachment whose scatterlist is to be returned
1087 * @direction: [in] direction of DMA transfer
1089 * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
1090 * on error. May return -EINTR if it is interrupted by a signal.
1092 * On success, the DMA addresses and lengths in the returned scatterlist are
1093 * PAGE_SIZE aligned.
1095 * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that
1096 * the underlying backing storage is pinned for as long as a mapping exists,
1097 * therefore users/importers should not hold onto a mapping for undue amounts of
1100 * Important: Dynamic importers must wait for the exclusive fence of the struct
1101 * dma_resv attached to the DMA-BUF first.
1103 struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
1104 enum dma_data_direction direction)
1106 struct sg_table *sg_table;
1111 if (WARN_ON(!attach || !attach->dmabuf))
1112 return ERR_PTR(-EINVAL);
1114 dma_resv_assert_held(attach->dmabuf->resv);
1118 * Two mappings with different directions for the same
1119 * attachment are not allowed.
1121 if (attach->dir != direction &&
1122 attach->dir != DMA_BIDIRECTIONAL)
1123 return ERR_PTR(-EBUSY);
1128 if (dma_buf_is_dynamic(attach->dmabuf)) {
1129 if (!IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)) {
1130 r = attach->dmabuf->ops->pin(attach);
1136 sg_table = __map_dma_buf(attach, direction);
1138 sg_table = ERR_PTR(-ENOMEM);
1140 if (IS_ERR(sg_table) && dma_buf_is_dynamic(attach->dmabuf) &&
1141 !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY))
1142 attach->dmabuf->ops->unpin(attach);
1144 if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) {
1145 attach->sgt = sg_table;
1146 attach->dir = direction;
1149 #ifdef CONFIG_DMA_API_DEBUG
1150 if (!IS_ERR(sg_table)) {
1151 struct scatterlist *sg;
1156 for_each_sgtable_dma_sg(sg_table, sg, i) {
1157 addr = sg_dma_address(sg);
1158 len = sg_dma_len(sg);
1159 if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(len)) {
1160 pr_debug("%s: addr %llx or len %x is not page aligned!\n",
1161 __func__, addr, len);
1165 #endif /* CONFIG_DMA_API_DEBUG */
1168 EXPORT_SYMBOL_NS_GPL(dma_buf_map_attachment, DMA_BUF);
1171 * dma_buf_map_attachment_unlocked - Returns the scatterlist table of the attachment;
1172 * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
1174 * @attach: [in] attachment whose scatterlist is to be returned
1175 * @direction: [in] direction of DMA transfer
1177 * Unlocked variant of dma_buf_map_attachment().
1180 dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
1181 enum dma_data_direction direction)
1183 struct sg_table *sg_table;
1187 if (WARN_ON(!attach || !attach->dmabuf))
1188 return ERR_PTR(-EINVAL);
1190 dma_resv_lock(attach->dmabuf->resv, NULL);
1191 sg_table = dma_buf_map_attachment(attach, direction);
1192 dma_resv_unlock(attach->dmabuf->resv);
1196 EXPORT_SYMBOL_NS_GPL(dma_buf_map_attachment_unlocked, DMA_BUF);
1199 * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
1200 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
1202 * @attach: [in] attachment to unmap buffer from
1203 * @sg_table: [in] scatterlist info of the buffer to unmap
1204 * @direction: [in] direction of DMA transfer
1206 * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment().
1208 void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
1209 struct sg_table *sg_table,
1210 enum dma_data_direction direction)
1214 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
1217 dma_resv_assert_held(attach->dmabuf->resv);
1219 if (attach->sgt == sg_table)
1222 __unmap_dma_buf(attach, sg_table, direction);
1224 if (dma_buf_is_dynamic(attach->dmabuf) &&
1225 !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY))
1226 dma_buf_unpin(attach);
1228 EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_attachment, DMA_BUF);
1231 * dma_buf_unmap_attachment_unlocked - unmaps and decreases usecount of the buffer;might
1232 * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
1234 * @attach: [in] attachment to unmap buffer from
1235 * @sg_table: [in] scatterlist info of the buffer to unmap
1236 * @direction: [in] direction of DMA transfer
1238 * Unlocked variant of dma_buf_unmap_attachment().
1240 void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
1241 struct sg_table *sg_table,
1242 enum dma_data_direction direction)
1246 if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
1249 dma_resv_lock(attach->dmabuf->resv, NULL);
1250 dma_buf_unmap_attachment(attach, sg_table, direction);
1251 dma_resv_unlock(attach->dmabuf->resv);
1253 EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_attachment_unlocked, DMA_BUF);
1256 * dma_buf_move_notify - notify attachments that DMA-buf is moving
1258 * @dmabuf: [in] buffer which is moving
1260 * Informs all attachments that they need to destroy and recreate all their
1263 void dma_buf_move_notify(struct dma_buf *dmabuf)
1265 struct dma_buf_attachment *attach;
1267 dma_resv_assert_held(dmabuf->resv);
1269 list_for_each_entry(attach, &dmabuf->attachments, node)
1270 if (attach->importer_ops)
1271 attach->importer_ops->move_notify(attach);
1273 EXPORT_SYMBOL_NS_GPL(dma_buf_move_notify, DMA_BUF);
1278 * There are multiple reasons for supporting CPU access to a dma buffer object:
1280 * - Fallback operations in the kernel, for example when a device is connected
1281 * over USB and the kernel needs to shuffle the data around first before
1282 * sending it away. Cache coherency is handled by bracketing any transactions
1283 * with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access()
1286 * Since for most kernel internal dma-buf accesses need the entire buffer, a
1287 * vmap interface is introduced. Note that on very old 32-bit architectures
1288 * vmalloc space might be limited and result in vmap calls failing.
1292 * void \*dma_buf_vmap(struct dma_buf \*dmabuf, struct iosys_map \*map)
1293 * void dma_buf_vunmap(struct dma_buf \*dmabuf, struct iosys_map \*map)
1295 * The vmap call can fail if there is no vmap support in the exporter, or if
1296 * it runs out of vmalloc space. Note that the dma-buf layer keeps a reference
1297 * count for all vmap access and calls down into the exporter's vmap function
1298 * only when no vmapping exists, and only unmaps it once. Protection against
1299 * concurrent vmap/vunmap calls is provided by taking the &dma_buf.lock mutex.
1301 * - For full compatibility on the importer side with existing userspace
1302 * interfaces, which might already support mmap'ing buffers. This is needed in
1303 * many processing pipelines (e.g. feeding a software rendered image into a
1304 * hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION
1305 * framework already supported this and for DMA buffer file descriptors to
1306 * replace ION buffers mmap support was needed.
1308 * There is no special interfaces, userspace simply calls mmap on the dma-buf
1309 * fd. But like for CPU access there's a need to bracket the actual access,
1310 * which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that
1311 * DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must
1314 * Some systems might need some sort of cache coherency management e.g. when
1315 * CPU and GPU domains are being accessed through dma-buf at the same time.
1316 * To circumvent this problem there are begin/end coherency markers, that
1317 * forward directly to existing dma-buf device drivers vfunc hooks. Userspace
1318 * can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The
1319 * sequence would be used like following:
1322 * - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write
1323 * to mmap area 3. SYNC_END ioctl. This can be repeated as often as you
1324 * want (with the new data being consumed by say the GPU or the scanout
1326 * - munmap once you don't need the buffer any more
1328 * For correctness and optimal performance, it is always required to use
1329 * SYNC_START and SYNC_END before and after, respectively, when accessing the
1330 * mapped address. Userspace cannot rely on coherent access, even when there
1331 * are systems where it just works without calling these ioctls.
1333 * - And as a CPU fallback in userspace processing pipelines.
1335 * Similar to the motivation for kernel cpu access it is again important that
1336 * the userspace code of a given importing subsystem can use the same
1337 * interfaces with a imported dma-buf buffer object as with a native buffer
1338 * object. This is especially important for drm where the userspace part of
1339 * contemporary OpenGL, X, and other drivers is huge, and reworking them to
1340 * use a different way to mmap a buffer rather invasive.
1342 * The assumption in the current dma-buf interfaces is that redirecting the
1343 * initial mmap is all that's needed. A survey of some of the existing
1344 * subsystems shows that no driver seems to do any nefarious thing like
1345 * syncing up with outstanding asynchronous processing on the device or
1346 * allocating special resources at fault time. So hopefully this is good
1347 * enough, since adding interfaces to intercept pagefaults and allow pte
1348 * shootdowns would increase the complexity quite a bit.
1352 * int dma_buf_mmap(struct dma_buf \*, struct vm_area_struct \*,
1355 * If the importing subsystem simply provides a special-purpose mmap call to
1356 * set up a mapping in userspace, calling do_mmap with &dma_buf.file will
1357 * equally achieve that for a dma-buf object.
1360 static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
1361 enum dma_data_direction direction)
1363 bool write = (direction == DMA_BIDIRECTIONAL ||
1364 direction == DMA_TO_DEVICE);
1365 struct dma_resv *resv = dmabuf->resv;
1368 /* Wait on any implicit rendering fences */
1369 ret = dma_resv_wait_timeout(resv, dma_resv_usage_rw(write),
1370 true, MAX_SCHEDULE_TIMEOUT);
1378 * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
1379 * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
1380 * preparations. Coherency is only guaranteed in the specified range for the
1381 * specified access direction.
1382 * @dmabuf: [in] buffer to prepare cpu access for.
1383 * @direction: [in] direction of access.
1385 * After the cpu access is complete the caller should call
1386 * dma_buf_end_cpu_access(). Only when cpu access is bracketed by both calls is
1387 * it guaranteed to be coherent with other DMA access.
1389 * This function will also wait for any DMA transactions tracked through
1390 * implicit synchronization in &dma_buf.resv. For DMA transactions with explicit
1391 * synchronization this function will only ensure cache coherency, callers must
1392 * ensure synchronization with such DMA transactions on their own.
1394 * Can return negative error values, returns 0 on success.
1396 int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
1397 enum dma_data_direction direction)
1401 if (WARN_ON(!dmabuf))
1404 might_lock(&dmabuf->resv->lock.base);
1406 if (dmabuf->ops->begin_cpu_access)
1407 ret = dmabuf->ops->begin_cpu_access(dmabuf, direction);
1409 /* Ensure that all fences are waited upon - but we first allow
1410 * the native handler the chance to do so more efficiently if it
1411 * chooses. A double invocation here will be reasonably cheap no-op.
1414 ret = __dma_buf_begin_cpu_access(dmabuf, direction);
1418 EXPORT_SYMBOL_NS_GPL(dma_buf_begin_cpu_access, DMA_BUF);
1421 * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
1422 * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
1423 * actions. Coherency is only guaranteed in the specified range for the
1424 * specified access direction.
1425 * @dmabuf: [in] buffer to complete cpu access for.
1426 * @direction: [in] direction of access.
1428 * This terminates CPU access started with dma_buf_begin_cpu_access().
1430 * Can return negative error values, returns 0 on success.
1432 int dma_buf_end_cpu_access(struct dma_buf *dmabuf,
1433 enum dma_data_direction direction)
1439 might_lock(&dmabuf->resv->lock.base);
1441 if (dmabuf->ops->end_cpu_access)
1442 ret = dmabuf->ops->end_cpu_access(dmabuf, direction);
1446 EXPORT_SYMBOL_NS_GPL(dma_buf_end_cpu_access, DMA_BUF);
1450 * dma_buf_mmap - Setup up a userspace mmap with the given vma
1451 * @dmabuf: [in] buffer that should back the vma
1452 * @vma: [in] vma for the mmap
1453 * @pgoff: [in] offset in pages where this mmap should start within the
1456 * This function adjusts the passed in vma so that it points at the file of the
1457 * dma_buf operation. It also adjusts the starting pgoff and does bounds
1458 * checking on the size of the vma. Then it calls the exporters mmap function to
1459 * set up the mapping.
1461 * Can return negative error values, returns 0 on success.
1463 int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
1464 unsigned long pgoff)
1468 if (WARN_ON(!dmabuf || !vma))
1471 /* check if buffer supports mmap */
1472 if (!dmabuf->ops->mmap)
1475 /* check for offset overflow */
1476 if (pgoff + vma_pages(vma) < pgoff)
1479 /* check for overflowing the buffer's size */
1480 if (pgoff + vma_pages(vma) >
1481 dmabuf->size >> PAGE_SHIFT)
1484 /* readjust the vma */
1485 vma_set_file(vma, dmabuf->file);
1486 vma->vm_pgoff = pgoff;
1488 dma_resv_lock(dmabuf->resv, NULL);
1489 ret = dmabuf->ops->mmap(dmabuf, vma);
1490 dma_resv_unlock(dmabuf->resv);
1494 EXPORT_SYMBOL_NS_GPL(dma_buf_mmap, DMA_BUF);
1497 * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
1498 * address space. Same restrictions as for vmap and friends apply.
1499 * @dmabuf: [in] buffer to vmap
1500 * @map: [out] returns the vmap pointer
1502 * This call may fail due to lack of virtual mapping address space.
1503 * These calls are optional in drivers. The intended use for them
1504 * is for mapping objects linear in kernel space for high use objects.
1506 * To ensure coherency users must call dma_buf_begin_cpu_access() and
1507 * dma_buf_end_cpu_access() around any cpu access performed through this
1510 * Returns 0 on success, or a negative errno code otherwise.
1512 int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map)
1514 struct iosys_map ptr;
1517 iosys_map_clear(map);
1519 if (WARN_ON(!dmabuf))
1522 dma_resv_assert_held(dmabuf->resv);
1524 if (!dmabuf->ops->vmap)
1527 if (dmabuf->vmapping_counter) {
1528 dmabuf->vmapping_counter++;
1529 BUG_ON(iosys_map_is_null(&dmabuf->vmap_ptr));
1530 *map = dmabuf->vmap_ptr;
1534 BUG_ON(iosys_map_is_set(&dmabuf->vmap_ptr));
1536 ret = dmabuf->ops->vmap(dmabuf, &ptr);
1537 if (WARN_ON_ONCE(ret))
1540 dmabuf->vmap_ptr = ptr;
1541 dmabuf->vmapping_counter = 1;
1543 *map = dmabuf->vmap_ptr;
1547 EXPORT_SYMBOL_NS_GPL(dma_buf_vmap, DMA_BUF);
1550 * dma_buf_vmap_unlocked - Create virtual mapping for the buffer object into kernel
1551 * address space. Same restrictions as for vmap and friends apply.
1552 * @dmabuf: [in] buffer to vmap
1553 * @map: [out] returns the vmap pointer
1555 * Unlocked version of dma_buf_vmap()
1557 * Returns 0 on success, or a negative errno code otherwise.
1559 int dma_buf_vmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map)
1563 iosys_map_clear(map);
1565 if (WARN_ON(!dmabuf))
1568 dma_resv_lock(dmabuf->resv, NULL);
1569 ret = dma_buf_vmap(dmabuf, map);
1570 dma_resv_unlock(dmabuf->resv);
1574 EXPORT_SYMBOL_NS_GPL(dma_buf_vmap_unlocked, DMA_BUF);
1577 * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
1578 * @dmabuf: [in] buffer to vunmap
1579 * @map: [in] vmap pointer to vunmap
1581 void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map)
1583 if (WARN_ON(!dmabuf))
1586 dma_resv_assert_held(dmabuf->resv);
1588 BUG_ON(iosys_map_is_null(&dmabuf->vmap_ptr));
1589 BUG_ON(dmabuf->vmapping_counter == 0);
1590 BUG_ON(!iosys_map_is_equal(&dmabuf->vmap_ptr, map));
1592 if (--dmabuf->vmapping_counter == 0) {
1593 if (dmabuf->ops->vunmap)
1594 dmabuf->ops->vunmap(dmabuf, map);
1595 iosys_map_clear(&dmabuf->vmap_ptr);
1598 EXPORT_SYMBOL_NS_GPL(dma_buf_vunmap, DMA_BUF);
1601 * dma_buf_vunmap_unlocked - Unmap a vmap obtained by dma_buf_vmap.
1602 * @dmabuf: [in] buffer to vunmap
1603 * @map: [in] vmap pointer to vunmap
1605 void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map)
1607 if (WARN_ON(!dmabuf))
1610 dma_resv_lock(dmabuf->resv, NULL);
1611 dma_buf_vunmap(dmabuf, map);
1612 dma_resv_unlock(dmabuf->resv);
1614 EXPORT_SYMBOL_NS_GPL(dma_buf_vunmap_unlocked, DMA_BUF);
1616 #ifdef CONFIG_DEBUG_FS
1617 static int dma_buf_debug_show(struct seq_file *s, void *unused)
1619 struct dma_buf *buf_obj;
1620 struct dma_buf_attachment *attach_obj;
1621 int count = 0, attach_count;
1625 ret = mutex_lock_interruptible(&db_list.lock);
1630 seq_puts(s, "\nDma-buf Objects:\n");
1631 seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\tname\n",
1632 "size", "flags", "mode", "count", "ino");
1634 list_for_each_entry(buf_obj, &db_list.head, list_node) {
1636 ret = dma_resv_lock_interruptible(buf_obj->resv, NULL);
1641 spin_lock(&buf_obj->name_lock);
1642 seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n",
1644 buf_obj->file->f_flags, buf_obj->file->f_mode,
1645 file_count(buf_obj->file),
1647 file_inode(buf_obj->file)->i_ino,
1648 buf_obj->name ?: "<none>");
1649 spin_unlock(&buf_obj->name_lock);
1651 dma_resv_describe(buf_obj->resv, s);
1653 seq_puts(s, "\tAttached Devices:\n");
1656 list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
1657 seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
1660 dma_resv_unlock(buf_obj->resv);
1662 seq_printf(s, "Total %d devices attached\n\n",
1666 size += buf_obj->size;
1669 seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
1671 mutex_unlock(&db_list.lock);
1675 mutex_unlock(&db_list.lock);
1679 DEFINE_SHOW_ATTRIBUTE(dma_buf_debug);
1681 static struct dentry *dma_buf_debugfs_dir;
1683 static int dma_buf_init_debugfs(void)
1688 d = debugfs_create_dir("dma_buf", NULL);
1692 dma_buf_debugfs_dir = d;
1694 d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir,
1695 NULL, &dma_buf_debug_fops);
1697 pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
1698 debugfs_remove_recursive(dma_buf_debugfs_dir);
1699 dma_buf_debugfs_dir = NULL;
1706 static void dma_buf_uninit_debugfs(void)
1708 debugfs_remove_recursive(dma_buf_debugfs_dir);
1711 static inline int dma_buf_init_debugfs(void)
1715 static inline void dma_buf_uninit_debugfs(void)
1720 static int __init dma_buf_init(void)
1724 ret = dma_buf_init_sysfs_statistics();
1728 dma_buf_mnt = kern_mount(&dma_buf_fs_type);
1729 if (IS_ERR(dma_buf_mnt))
1730 return PTR_ERR(dma_buf_mnt);
1732 mutex_init(&db_list.lock);
1733 INIT_LIST_HEAD(&db_list.head);
1734 dma_buf_init_debugfs();
1737 subsys_initcall(dma_buf_init);
1739 static void __exit dma_buf_deinit(void)
1741 dma_buf_uninit_debugfs();
1742 kern_unmount(dma_buf_mnt);
1743 dma_buf_uninit_sysfs_statistics();
1745 __exitcall(dma_buf_deinit);