The userptr dma-buf keeps one struct sg_table for its lifetime in dma_buf->priv and map_dma_buf() DMA maps and returns that same table for every attachment. A mapping is per attachment and per map, so each map_dma_buf() must allocate a struct dma_buf_sgt_mapping holding a copy of the table header and DMA map through it. The entries remain shared as before. unmap_dma_buf() DMA unmaps through the copy and frees the mapping. The table itself is still freed in release.
Note that sharing one scatterlist between mappings is already broken. dma_map_sgtable() writes the DMA addresses, lengths and nents into the shared entries, so a second attachment mapping the buffer overwrites the first importer's addresses. Unmap would tear down whichever mapping the entries hold at the time. This is a pre-existing issue though which needs to be addressed by a separate fix - this patch doesn't change any behaviour. Signed-off-by: Alistair Popple <[email protected]> --- drivers/accel/ivpu/ivpu_gem_userptr.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/drivers/accel/ivpu/ivpu_gem_userptr.c b/drivers/accel/ivpu/ivpu_gem_userptr.c index 7cbf3a4cdc73..35e66b84a64b 100644 --- a/drivers/accel/ivpu/ivpu_gem_userptr.c +++ b/drivers/accel/ivpu/ivpu_gem_userptr.c @@ -4,6 +4,7 @@ */ #include <linux/dma-buf.h> +#include <linux/dma-buf-mapping.h> #include <linux/err.h> #include <linux/highmem.h> #include <linux/mm.h> @@ -19,25 +20,37 @@ #include "ivpu_drv.h" #include "ivpu_gem.h" -static struct sg_table * +static struct dma_buf_sgt_mapping * ivpu_gem_userptr_dmabuf_map(struct dma_buf_attachment *attachment, enum dma_data_direction direction) { struct sg_table *sgt = attachment->dmabuf->priv; + struct dma_buf_sgt_mapping *mapping; int ret; - ret = dma_map_sgtable(attachment->dev, sgt, direction, DMA_ATTR_SKIP_CPU_SYNC); - if (ret) + mapping = kzalloc_obj(*mapping); + if (!mapping) + return ERR_PTR(-ENOMEM); + + /* Header copy only; the table and its entries belong to the dma_buf */ + mapping->sgt = *sgt; + ret = dma_map_sgtable(dma_buf_sgt_dma_device(attachment), &mapping->sgt, + direction, DMA_ATTR_SKIP_CPU_SYNC); + if (ret) { + kfree(mapping); return ERR_PTR(ret); + } - return sgt; + return mapping; } static void ivpu_gem_userptr_dmabuf_unmap(struct dma_buf_attachment *attachment, - struct sg_table *sgt, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction direction) { - dma_unmap_sgtable(attachment->dev, sgt, direction, DMA_ATTR_SKIP_CPU_SYNC); + dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping), + &mapping->sgt, direction, DMA_ATTR_SKIP_CPU_SYNC); + kfree(mapping); } static void ivpu_gem_userptr_dmabuf_release(struct dma_buf *dma_buf) @@ -56,9 +69,9 @@ static void ivpu_gem_userptr_dmabuf_release(struct dma_buf *dma_buf) } static const struct dma_buf_ops ivpu_gem_userptr_dmabuf_ops = { - .map_dma_buf = ivpu_gem_userptr_dmabuf_map, - .unmap_dma_buf = ivpu_gem_userptr_dmabuf_unmap, .release = ivpu_gem_userptr_dmabuf_release, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(ivpu_gem_userptr_dmabuf_map, + ivpu_gem_userptr_dmabuf_unmap), }; static struct dma_buf * -- 2.54.0
