Skip to content

Commit ae2e7f2

Browse files
committed
dma-buf: Document dynamic locking convention
Add documentation for the dynamic locking convention. The documentation tells dma-buf API users when they should take the reservation lock and when not. Acked-by: Sumit Semwal <[email protected]> Reviewed-by: Christian König <[email protected]> Signed-off-by: Dmitry Osipenko <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent d078fd9 commit ae2e7f2

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Documentation/driver-api/dma-buf.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ DMA Buffer ioctls
119119

120120
.. kernel-doc:: include/uapi/linux/dma-buf.h
121121

122+
DMA-BUF locking convention
123+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124+
125+
.. kernel-doc:: drivers/dma-buf/dma-buf.c
126+
:doc: locking convention
127+
122128
Kernel Functions and Structures Reference
123129
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124130

drivers/dma-buf/dma-buf.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,70 @@ static struct sg_table * __map_dma_buf(struct dma_buf_attachment *attach,
795795
return sg_table;
796796
}
797797

798+
/**
799+
* DOC: locking convention
800+
*
801+
* In order to avoid deadlock situations between dma-buf exports and importers,
802+
* all dma-buf API users must follow the common dma-buf locking convention.
803+
*
804+
* Convention for importers
805+
*
806+
* 1. Importers must hold the dma-buf reservation lock when calling these
807+
* functions:
808+
*
809+
* - dma_buf_pin()
810+
* - dma_buf_unpin()
811+
* - dma_buf_map_attachment()
812+
* - dma_buf_unmap_attachment()
813+
* - dma_buf_vmap()
814+
* - dma_buf_vunmap()
815+
*
816+
* 2. Importers must not hold the dma-buf reservation lock when calling these
817+
* functions:
818+
*
819+
* - dma_buf_attach()
820+
* - dma_buf_dynamic_attach()
821+
* - dma_buf_detach()
822+
* - dma_buf_export(
823+
* - dma_buf_fd()
824+
* - dma_buf_get()
825+
* - dma_buf_put()
826+
* - dma_buf_mmap()
827+
* - dma_buf_begin_cpu_access()
828+
* - dma_buf_end_cpu_access()
829+
* - dma_buf_map_attachment_unlocked()
830+
* - dma_buf_unmap_attachment_unlocked()
831+
* - dma_buf_vmap_unlocked()
832+
* - dma_buf_vunmap_unlocked()
833+
*
834+
* Convention for exporters
835+
*
836+
* 1. These &dma_buf_ops callbacks are invoked with unlocked dma-buf
837+
* reservation and exporter can take the lock:
838+
*
839+
* - &dma_buf_ops.attach()
840+
* - &dma_buf_ops.detach()
841+
* - &dma_buf_ops.release()
842+
* - &dma_buf_ops.begin_cpu_access()
843+
* - &dma_buf_ops.end_cpu_access()
844+
*
845+
* 2. These &dma_buf_ops callbacks are invoked with locked dma-buf
846+
* reservation and exporter can't take the lock:
847+
*
848+
* - &dma_buf_ops.pin()
849+
* - &dma_buf_ops.unpin()
850+
* - &dma_buf_ops.map_dma_buf()
851+
* - &dma_buf_ops.unmap_dma_buf()
852+
* - &dma_buf_ops.mmap()
853+
* - &dma_buf_ops.vmap()
854+
* - &dma_buf_ops.vunmap()
855+
*
856+
* 3. Exporters must hold the dma-buf reservation lock when calling these
857+
* functions:
858+
*
859+
* - dma_buf_move_notify()
860+
*/
861+
798862
/**
799863
* dma_buf_dynamic_attach - Add the device to dma_buf's attachments list
800864
* @dmabuf: [in] buffer to attach device to.

0 commit comments

Comments
 (0)