On 10/5/26 08:41, Karl Mehltretter wrote:
With DMABUF_DEBUG, dma_buf_map_attachment() hands the importer a copy of the sg_table that keeps only sg_dma_address() and sg_dma_len(). The dma_flags (SG_DMA_BUS_ADDRESS, SG_DMA_SWIOTLB) are dropped.
The flags describe the DMA side of an entry, which is the side importers may use. Copy them as well.
This is not a bug fix. No importer reads the flags today. Their only readers are dma-iommu, dma-direct and iommu_map_sg(), which are not supposed to see an importer's copy at all. The warn mode added in the next patch keeps its marker in dma_flags and lets importers that do get there continue. They should then see the same flags as without DMABUF_DEBUG.
Assisted-by: LLM Signed-off-by: Karl Mehltretter kmehltretter@gmail.com
Notes: Built alone on x86_64 with NEED_SG_DMA_FLAGS=y (dma-buf.o, W=1). The KUnit test in patch 3 checks the flags in the copy. It passes on x86_64 under QEMU in strict and in warn mode.
drivers/dma-buf/dma-buf.c | 4 ++++ 1 file changed, 4 insertions(+)
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 4c9add51f9ef..b3d311acb883 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -904,6 +904,10 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table) sg_assign_page(to_sg, NULL); sg_dma_address(to_sg) = sg_dma_address(from_sg); sg_dma_len(to_sg) = sg_dma_len(from_sg); +#ifdef CONFIG_NEED_SG_DMA_FLAGS
/* the flags describe the DMA side, e.g. SG_DMA_BUS_ADDRESS */to_sg->dma_flags = from_sg->dma_flags;
We should probably have a WARN_ON_ONCE(sg_dma_is_swiotlb(from_sg)) here as well since some importers/exporters doesn't realize that this combination will usually not work.
Apart from that looks good to me.
Regards, Christian.
+#endif to_sg = sg_next(to_sg); }
linaro-mm-sig@lists.linaro.org