drivers/dma-buf/dma-buf.c
Source file repositories/reference/linux-study-clean/drivers/dma-buf/dma-buf.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/dma-buf/dma-buf.c- Extension
.c- Size
- 52216 bytes
- Lines
- 1853
- Domain
- Driver Families
- Bucket
- drivers/dma-buf
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/fs.hlinux/slab.hlinux/dma-buf.hlinux/dma-fence.hlinux/dma-fence-unwrap.hlinux/anon_inodes.hlinux/export.hlinux/debugfs.hlinux/list.hlinux/module.hlinux/mutex.hlinux/seq_file.hlinux/sync_file.hlinux/poll.hlinux/dma-resv.hlinux/mm.hlinux/mount.hlinux/pseudo_fs.huapi/linux/dma-buf.huapi/linux/magic.htrace/events/dma_buf.h
Detected Declarations
struct dma_buf_sg_table_wrapperfunction Copyrightfunction __dma_buf_list_addfunction __dma_buf_list_delfunction dma_buf_iter_nextfunction dma_buf_iter_nextfunction dma_buf_releasefunction dma_buf_file_releasefunction dma_buf_fs_init_contextfunction dma_buf_mmap_internalfunction dma_buf_llseekfunction fencesfunction dma_buf_poll_add_cbfunction dma_resv_for_each_fencefunction dma_buf_pollfunction dma_buf_set_namefunction dma_buf_export_sync_filefunction dma_buf_import_sync_filefunction dma_buf_ioctlfunction dma_buf_show_fdinfofunction is_dma_buf_filefunction dma_buf_fdfunction dma_buf_putfunction dma_buf_wrap_sg_tablefunction dma_buf_unwrap_sg_tablefunction dma_buf_attachment_is_dynamicfunction dma_buf_pin_on_mapfunction dma_buf_detachfunction dma_buf_dynamic_attachfunction dma_buf_attachfunction importersfunction dma_buf_pinfunction dma_buf_unmap_attachmentfunction for_each_sgtable_dma_sgfunction dma_buf_map_attachmentfunction dma_buf_unmap_attachmentfunction dma_buf_unmap_attachmentfunction dma_buf_invalidate_mappingsfunction dma_buf_invalidate_mappingsfunction __dma_buf_begin_cpu_accessfunction dma_buf_end_cpu_accessfunction dma_buf_begin_cpu_accessfunction dma_buf_mmapfunction dma_buf_begin_cpu_accessfunction dma_buf_vmapfunction dma_buf_vunmapfunction dma_buf_vunmap_unlockedfunction dma_buf_debug_show
Annotated Snippet
static const struct file_operations dma_buf_fops = {
.release = dma_buf_file_release,
.mmap = dma_buf_mmap_internal,
.llseek = dma_buf_llseek,
.poll = dma_buf_poll,
.unlocked_ioctl = dma_buf_ioctl,
.compat_ioctl = compat_ptr_ioctl,
.show_fdinfo = dma_buf_show_fdinfo,
};
/*
* is_dma_buf_file - Check if struct file* is associated with dma_buf
*/
static inline int is_dma_buf_file(struct file *file)
{
return file->f_op == &dma_buf_fops;
}
static struct file *dma_buf_getfile(size_t size, int flags)
{
static atomic64_t dmabuf_inode = ATOMIC64_INIT(0);
struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb);
struct file *file;
if (IS_ERR(inode))
return ERR_CAST(inode);
inode->i_size = size;
inode_set_bytes(inode, size);
/*
* The ->i_ino acquired from get_next_ino() is not unique thus
* not suitable for using it as dentry name by dmabuf stats.
* Override ->i_ino with the unique and dmabuffs specific
* value.
*/
inode->i_ino = atomic64_inc_return(&dmabuf_inode);
flags &= O_ACCMODE | O_NONBLOCK;
file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf",
flags, &dma_buf_fops);
if (IS_ERR(file))
goto err_alloc_file;
return file;
err_alloc_file:
iput(inode);
return file;
}
/**
* DOC: dma buf device access
*
* For device DMA access to a shared DMA buffer the usual sequence of operations
* is fairly simple:
*
* 1. The exporter defines his exporter instance using
* DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private
* buffer object into a &dma_buf. It then exports that &dma_buf to userspace
* as a file descriptor by calling dma_buf_fd().
*
* 2. Userspace passes this file-descriptors to all drivers it wants this buffer
* to share with: First the file descriptor is converted to a &dma_buf using
* dma_buf_get(). Then the buffer is attached to the device using
* dma_buf_attach().
*
* Up to this stage the exporter is still free to migrate or reallocate the
* backing storage.
*
* 3. Once the buffer is attached to all devices userspace can initiate DMA
* access to the shared buffer. In the kernel this is done by calling
* dma_buf_map_attachment() and dma_buf_unmap_attachment().
*
* 4. Once a driver is done with a shared buffer it needs to call
* dma_buf_detach() (after cleaning up any mappings) and then release the
* reference acquired with dma_buf_get() by calling dma_buf_put().
*
* For the detailed semantics exporters are expected to implement see
* &dma_buf_ops.
*/
/**
* dma_buf_export - Creates a new dma_buf, and associates an anon file
* with this buffer, so it can be exported.
* Also connect the allocator specific data and ops to the buffer.
* Additionally, provide a name string for exporter; useful in debugging.
*
* @exp_info: [in] holds all the export related information provided
* by the exporter. see &struct dma_buf_export_info
* for further details.
Annotation
- Immediate include surface: `linux/fs.h`, `linux/slab.h`, `linux/dma-buf.h`, `linux/dma-fence.h`, `linux/dma-fence-unwrap.h`, `linux/anon_inodes.h`, `linux/export.h`, `linux/debugfs.h`.
- Detected declarations: `struct dma_buf_sg_table_wrapper`, `function Copyright`, `function __dma_buf_list_add`, `function __dma_buf_list_del`, `function dma_buf_iter_next`, `function dma_buf_iter_next`, `function dma_buf_release`, `function dma_buf_file_release`, `function dma_buf_fs_init_context`, `function dma_buf_mmap_internal`.
- Atlas domain: Driver Families / drivers/dma-buf.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.