Buffer Sharing and Synchronization

dma-buf
reservation
fence

The dma-buf subsystem provides the framework for sharing buffers for hardware (DMA) access across multiple device drivers and subsystems, and for synchronizing asynchronous hardware access.

This is used, for example, by drm "prime" multi-GPU support, but is of course not limited to GPU use cases.

The three main components of this are: (1) dma-buf, representing a sg_table and exposed to userspace as a file descriptor to allow passing between devices, (2) fence, which provides a mechanism to signal when one device as finished access, and (3) reservation, which manages the shared or exclusive fence(s) associated with the buffer.

dma-buf

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.
dma_buf_fd — returns a file descriptor for the given dma_buf
dma_buf_get — returns the dma_buf structure related to an fd
dma_buf_put — decreases refcount of the buffer
dma_buf_attach — Add the device to dma_buf's attachments list; optionally, calls attach of dma_buf_ops to allow device-specific attach functionality
dma_buf_detach — Remove the given attachment from dmabuf's attachments list; optionally calls detach of dma_buf_ops for device-specific detach
dma_buf_map_attachment — Returns the scatterlist table of the attachment; mapped into _device_ address space. Is a wrapper for map_dma_buf of the dma_buf_ops.
dma_buf_unmap_attachment — unmaps and decreases usecount of the buffer;might deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf of dma_buf_ops.
dma_buf_begin_cpu_access — Must be called before accessing a dma_buf from the cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific preparations. Coherency is only guaranteed in the specified range for the specified access direction.
dma_buf_end_cpu_access — Must be called after accessing a dma_buf from the cpu in the kernel context. Calls end_cpu_access to allow exporter-specific actions. Coherency is only guaranteed in the specified range for the specified access direction.
dma_buf_kmap_atomic — Map a page of the buffer object into kernel address space. The same restrictions as for kmap_atomic and friends apply.
dma_buf_kunmap_atomic — Unmap a page obtained by dma_buf_kmap_atomic.
dma_buf_kmap — Map a page of the buffer object into kernel address space. The same restrictions as for kmap and friends apply.
dma_buf_kunmap — Unmap a page obtained by dma_buf_kmap.
dma_buf_mmap — Setup up a userspace mmap with the given vma
dma_buf_vmap — Create virtual mapping for the buffer object into kernel address space. Same restrictions as for vmap and friends apply.
dma_buf_vunmap — Unmap a vmap obtained by dma_buf_vmap.
struct dma_buf_ops — operations possible on struct dma_buf
struct dma_buf — shared buffer object
struct dma_buf_attachment — holds device-buffer attachment data
struct dma_buf_export_info — holds information needed to export a dma_buf
DEFINE_DMA_BUF_EXPORT_INFO
get_dma_buf — convenience wrapper for get_file.

reservation

reservation_object_reserve_shared — Reserve space to add a shared fence to a reservation_object.
reservation_object_add_shared_fence — Add a fence to a shared slot
reservation_object_add_excl_fence — Add an exclusive fence.
reservation_object_get_fences_rcu — Get an object's shared and exclusive fences without update side lock held
reservation_object_wait_timeout_rcu — Wait on reservation's objects shared and/or exclusive fences.
reservation_object_test_signaled_rcu — Test if a reservation object's fences have been signaled.
struct reservation_object_list — a list of shared fences
struct reservation_object — a reservation object manages fences for a buffer
reservation_object_init — initialize a reservation object
reservation_object_fini — destroys a reservation object
reservation_object_get_list — get the reservation object's shared fence list, with update-side lock held
reservation_object_get_excl — get the reservation object's exclusive fence, with update-side lock held
reservation_object_get_excl_rcu — get the reservation object's exclusive fence, without lock held.

The reservation object provides a mechanism to manage shared and exclusive fences associated with a buffer. A reservation object can have attached one exclusive fence (normally associated with write operations) or N shared fences (read operations). The RCU mechanism is used to protect read access to fences from locked write-side updates.

fence

fence_context_alloc — allocate an array of fence contexts
fence_signal_locked — signal completion of a fence
fence_signal — signal completion of a fence
fence_wait_timeout — sleep until the fence gets signaled or until timeout elapses
fence_enable_sw_signaling — enable signaling on fence
fence_add_callback — add a callback to be called when the fence is signaled
fence_remove_callback — remove a callback from the signaling list
fence_default_wait — default sleep until the fence gets signaled or until timeout elapses
fence_wait_any_timeout — sleep until any fence gets signaled or until timeout elapses
fence_init — Initialize a custom fence.
struct fence — software synchronization primitive
struct fence_cb — callback for fence_add_callback
struct fence_ops — operations implemented for fence
fence_get — increases refcount of the fence
fence_get_rcu — get a fence from a reservation_object_list with rcu read lock
fence_put — decreases refcount of the fence
fence_is_signaled_locked — Return an indication if the fence is signaled yet.
fence_is_signaled — Return an indication if the fence is signaled yet.
fence_is_later — return if f1 is chronologically later than f2
fence_later — return the chronologically later fence
fence_wait — sleep until the fence gets signaled
drivers/dma-buf/seqno-fence.c — Document generation inconsistency
to_seqno_fence — cast a fence to a seqno_fence
seqno_fence_init — initialize a seqno fence
sync_file_create — creates a sync file
struct sync_file — sync file to export to the userspace