Skip to content

Commit ac80cd1

Browse files
j-xiongdanvet
authored andcommitted
dma-buf: Clarify that dma-buf sg lists are page aligned
The dma-buf API have been used under the assumption that the sg lists returned from dma_buf_map_attachment() are fully page aligned. Lots of stuff can break otherwise all over the place. Clarify this in the documentation and add a check when DMA API debug is enabled. Signed-off-by: Jianxin Xiong <[email protected]> Reviewed-by: Christian König <[email protected]> Signed-off-by: Daniel Vetter <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 73b62cd commit ac80cd1

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

drivers/dma-buf/dma-buf.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,9 @@ EXPORT_SYMBOL_GPL(dma_buf_unpin);
849849
* Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
850850
* on error. May return -EINTR if it is interrupted by a signal.
851851
*
852+
* On success, the DMA addresses and lengths in the returned scatterlist are
853+
* PAGE_SIZE aligned.
854+
*
852855
* A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that
853856
* the underlying backing storage is pinned for as long as a mapping exists,
854857
* therefore users/importers should not hold onto a mapping for undue amounts of
@@ -902,6 +905,24 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
902905
attach->dir = direction;
903906
}
904907

908+
#ifdef CONFIG_DMA_API_DEBUG
909+
{
910+
struct scatterlist *sg;
911+
u64 addr;
912+
int len;
913+
int i;
914+
915+
for_each_sgtable_dma_sg(sg_table, sg, i) {
916+
addr = sg_dma_address(sg);
917+
len = sg_dma_len(sg);
918+
if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(len)) {
919+
pr_debug("%s: addr %llx or len %x is not page aligned!\n",
920+
__func__, addr, len);
921+
}
922+
}
923+
}
924+
#endif /* CONFIG_DMA_API_DEBUG */
925+
905926
return sg_table;
906927
}
907928
EXPORT_SYMBOL_GPL(dma_buf_map_attachment);

include/linux/dma-buf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ struct dma_buf_ops {
146146
*
147147
* A &sg_table scatter list of or the backing storage of the DMA buffer,
148148
* already mapped into the device address space of the &device attached
149-
* with the provided &dma_buf_attachment.
149+
* with the provided &dma_buf_attachment. The addresses and lengths in
150+
* the scatter list are PAGE_SIZE aligned.
150151
*
151152
* On failure, returns a negative error value wrapped into a pointer.
152153
* May also return -EINTR when a signal was received while being

0 commit comments

Comments
 (0)