omapdrm keeps one struct sg_table per object allocated with omap_gem_get_sg() and returns it for every attachment. omap_gem_put_sg() expects the same pointer back. A mapping is per attachment and per map, so it cannot embed or take over that table. Instead allocate a small container around struct dma_buf_sgt_mapping in map_dma_buf() to hold a copy of the table header whilst sharing the entries.
This way omap_gem_put_sg() can find the object's mapping to free. The map and unmap callbacks move from dma_buf_ops into DMA_BUF_SIMPLE_SGT_EXP_MATCH(). Co-developed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Signed-off-by: Alistair Popple <[email protected]> --- drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c | 47 ++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c b/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c index afae0a56da8c..1c03f892dac9 100644 --- a/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +++ b/drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c @@ -5,6 +5,7 @@ */ #include <linux/dma-buf.h> +#include <linux/dma-buf-mapping.h> #include <linux/highmem.h> #include <drm/drm_prime.h> @@ -17,24 +18,50 @@ MODULE_IMPORT_NS("DMA_BUF"); * DMABUF Export */ -static struct sg_table *omap_gem_map_dma_buf( - struct dma_buf_attachment *attachment, - enum dma_data_direction dir) +/* + * The object caches one sg_table and hands it to every attachment, so each + * mapping only carries a copy of the table header and remembers the table it + * was taken from for omap_gem_put_sg(). + */ +struct omap_gem_dmabuf_mapping { + struct dma_buf_sgt_mapping base; + struct sg_table *sg; +}; + +static struct dma_buf_sgt_mapping * +omap_gem_map_dma_buf(struct dma_buf_attachment *attachment, + enum dma_data_direction dir) { struct drm_gem_object *obj = attachment->dmabuf->priv; + struct omap_gem_dmabuf_mapping *mapping; struct sg_table *sg; + + mapping = kzalloc_obj(*mapping); + if (!mapping) + return ERR_PTR(-ENOMEM); + sg = omap_gem_get_sg(obj, dir); - if (IS_ERR(sg)) - return sg; + if (IS_ERR(sg)) { + kfree(mapping); + return ERR_CAST(sg); + } - return sg; + /* Header copy only; the table stays owned by the object. */ + mapping->sg = sg; + mapping->base.sgt = *sg; + + return &mapping->base; } static void omap_gem_unmap_dma_buf(struct dma_buf_attachment *attachment, - struct sg_table *sg, enum dma_data_direction dir) + struct dma_buf_sgt_mapping *base, enum dma_data_direction dir) { struct drm_gem_object *obj = attachment->dmabuf->priv; - omap_gem_put_sg(obj, sg); + struct omap_gem_dmabuf_mapping *mapping = + container_of(base, struct omap_gem_dmabuf_mapping, base); + + omap_gem_put_sg(obj, mapping->sg); + kfree(mapping); } static int omap_gem_dmabuf_begin_cpu_access(struct dma_buf *buffer, @@ -69,12 +96,12 @@ static int omap_gem_dmabuf_mmap(struct dma_buf *buffer, } static const struct dma_buf_ops omap_dmabuf_ops = { - .map_dma_buf = omap_gem_map_dma_buf, - .unmap_dma_buf = omap_gem_unmap_dma_buf, .release = drm_gem_dmabuf_release, .begin_cpu_access = omap_gem_dmabuf_begin_cpu_access, .end_cpu_access = omap_gem_dmabuf_end_cpu_access, .mmap = omap_gem_dmabuf_mmap, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(omap_gem_map_dma_buf, + omap_gem_unmap_dma_buf), }; struct dma_buf *omap_gem_prime_export(struct drm_gem_object *obj, int flags) -- 2.54.0
