These exporters allocate a struct sg_table in map_dma_buf() and free it again in unmap_dma_buf().
The conversion is mechanical and no behaviour change is expected. Instead of map_dma_buf() allocating a sg_table it instead allocates and returns a dma_buf_sgt_mapping, which the unmap callbacks also take. As a side effect this fixes a leak in fsl_asrc_m2m. Its unmap_dma_buf() only DMA unmapped the per-map table and never freed it. It now frees the mapping including the table. Co-developed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]> Signed-off-by: Alistair Popple <[email protected]> --- drivers/accel/amdxdna/amdxdna_cbuf.c | 30 ++++++++------ drivers/accel/amdxdna/amdxdna_ubuf.c | 34 +++++++++------- drivers/accel/habanalabs/common/memory.c | 52 +++++++++++++----------- drivers/gpu/drm/armada/armada_gem.c | 36 +++++++++------- drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c | 30 ++++++++------ drivers/gpu/drm/tegra/gem.c | 30 ++++++++------ samples/vfio-mdev/mbochs.c | 31 ++++++++------ sound/soc/fsl/fsl_asrc_m2m.c | 32 +++++++++------ 8 files changed, 161 insertions(+), 114 deletions(-) diff --git a/drivers/accel/amdxdna/amdxdna_cbuf.c b/drivers/accel/amdxdna/amdxdna_cbuf.c index a504560aae98..945436516cc6 100644 --- a/drivers/accel/amdxdna/amdxdna_cbuf.c +++ b/drivers/accel/amdxdna/amdxdna_cbuf.c @@ -5,6 +5,7 @@ #include <drm/drm_mm.h> #include <drm/drm_prime.h> +#include <linux/dma-buf-mapping.h> #include "amdxdna_cbuf.h" #include "amdxdna_pci_drv.h" @@ -81,11 +82,13 @@ struct amdxdna_cbuf_priv { struct drm_mm_node node; }; -static struct sg_table *amdxdna_cbuf_map(struct dma_buf_attachment *attach, - enum dma_data_direction direction) +static struct dma_buf_sgt_mapping * +amdxdna_cbuf_map(struct dma_buf_attachment *attach, + enum dma_data_direction direction) { struct amdxdna_cbuf_priv *cbuf = attach->dmabuf->priv; - struct device *dev = attach->dev; + struct device *dev = dma_buf_sgt_dma_device(attach); + struct dma_buf_sgt_mapping *mapping; struct scatterlist *sgl, *sg; int ret, n_entries, i; struct sg_table *sgt; @@ -93,9 +96,10 @@ static struct sg_table *amdxdna_cbuf_map(struct dma_buf_attachment *attach, size_t dma_size; size_t max_seg; - sgt = kzalloc_obj(*sgt); - if (!sgt) + mapping = kzalloc_obj(*mapping); + if (!mapping) return ERR_PTR(-ENOMEM); + sgt = &mapping->sgt; max_seg = min_t(size_t, UINT_MAX, dma_max_mapping_size(dev)); n_entries = (cbuf->node.size + max_seg - 1) / max_seg; @@ -127,24 +131,27 @@ static struct sg_table *amdxdna_cbuf_map(struct dma_buf_attachment *attach, dma_size -= len; } - return sgt; + return mapping; free_sgl: kfree(sgl); free_sgt: - kfree(sgt); + kfree(mapping); return ERR_PTR(ret); } static void amdxdna_cbuf_unmap(struct dma_buf_attachment *attach, - struct sg_table *sgt, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction direction) { - dma_unmap_resource(attach->dev, sg_dma_address(sgt->sgl), + struct sg_table *sgt = &mapping->sgt; + + dma_unmap_resource(dma_buf_sgt_mapping_dma_device(mapping), + sg_dma_address(sgt->sgl), drm_prime_get_contiguous_size(sgt), direction, DMA_ATTR_SKIP_CPU_SYNC); sg_free_table(sgt); - kfree(sgt); + kfree(mapping); } static void amdxdna_cbuf_release(struct dma_buf *dbuf) @@ -210,12 +217,11 @@ static void amdxdna_cbuf_vunmap(struct dma_buf *dbuf, struct iosys_map *map) } static const struct dma_buf_ops amdxdna_cbuf_dmabuf_ops = { - .map_dma_buf = amdxdna_cbuf_map, - .unmap_dma_buf = amdxdna_cbuf_unmap, .release = amdxdna_cbuf_release, .mmap = amdxdna_cbuf_mmap, .vmap = amdxdna_cbuf_vmap, .vunmap = amdxdna_cbuf_vunmap, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(amdxdna_cbuf_map, amdxdna_cbuf_unmap), }; static int amdxdna_cbuf_clear(struct dma_buf *dbuf) diff --git a/drivers/accel/amdxdna/amdxdna_ubuf.c b/drivers/accel/amdxdna/amdxdna_ubuf.c index 0e0cd69cd1fb..ed8b0ab9e554 100644 --- a/drivers/accel/amdxdna/amdxdna_ubuf.c +++ b/drivers/accel/amdxdna/amdxdna_ubuf.c @@ -7,6 +7,7 @@ #include <drm/drm_device.h> #include <drm/drm_print.h> #include <linux/dma-buf.h> +#include <linux/dma-buf-mapping.h> #include <linux/overflow.h> #include <linux/pagemap.h> #include <linux/vmalloc.h> @@ -20,42 +21,48 @@ struct amdxdna_ubuf_priv { struct mm_struct *mm; }; -static struct sg_table *amdxdna_ubuf_map(struct dma_buf_attachment *attach, - enum dma_data_direction direction) +static struct dma_buf_sgt_mapping * +amdxdna_ubuf_map(struct dma_buf_attachment *attach, + enum dma_data_direction direction) { struct amdxdna_ubuf_priv *ubuf = attach->dmabuf->priv; + struct dma_buf_sgt_mapping *mapping; struct sg_table *sg; int ret; - sg = kzalloc_obj(*sg); - if (!sg) + mapping = kzalloc_obj(*mapping); + if (!mapping) return ERR_PTR(-ENOMEM); + sg = &mapping->sgt; ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->nr_pages, 0, ubuf->nr_pages << PAGE_SHIFT, GFP_KERNEL); if (ret) - goto err_free_sg; + goto err_free_mapping; - ret = dma_map_sgtable(attach->dev, sg, direction, 0); + ret = dma_map_sgtable(dma_buf_sgt_dma_device(attach), sg, direction, 0); if (ret) goto err_free_table; - return sg; + return mapping; err_free_table: sg_free_table(sg); -err_free_sg: - kfree(sg); +err_free_mapping: + kfree(mapping); return ERR_PTR(ret); } static void amdxdna_ubuf_unmap(struct dma_buf_attachment *attach, - struct sg_table *sg, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction direction) { - dma_unmap_sgtable(attach->dev, sg, direction, 0); + struct sg_table *sg = &mapping->sgt; + + dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping), sg, + direction, 0); sg_free_table(sg); - kfree(sg); + kfree(mapping); } static void amdxdna_ubuf_release(struct dma_buf *dbuf) @@ -70,9 +77,8 @@ static void amdxdna_ubuf_release(struct dma_buf *dbuf) } static const struct dma_buf_ops amdxdna_ubuf_dmabuf_ops = { - .map_dma_buf = amdxdna_ubuf_map, - .unmap_dma_buf = amdxdna_ubuf_unmap, .release = amdxdna_ubuf_release, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(amdxdna_ubuf_map, amdxdna_ubuf_unmap), }; struct dma_buf *amdxdna_get_ubuf(struct drm_device *dev, diff --git a/drivers/accel/habanalabs/common/memory.c b/drivers/accel/habanalabs/common/memory.c index 361cff577381..fc39ccd6f8db 100644 --- a/drivers/accel/habanalabs/common/memory.c +++ b/drivers/accel/habanalabs/common/memory.c @@ -9,6 +9,7 @@ #include "habanalabs.h" #include "../include/hw_ip/mmu/mmu_general.h" +#include <linux/dma-buf-mapping.h> #include <linux/uaccess.h> #include <linux/slab.h> #include <linux/vmalloc.h> @@ -1531,13 +1532,15 @@ static int set_dma_sg(struct scatterlist *sg, u64 bar_address, u64 chunk_size, return 0; } -static struct sg_table *alloc_sgt_from_device_pages(struct hl_device *hdev, u64 *pages, u64 npages, - u64 page_size, u64 exported_size, u64 offset, - struct device *dev, enum dma_data_direction dir) +static struct dma_buf_sgt_mapping * +alloc_sgt_from_device_pages(struct hl_device *hdev, u64 *pages, u64 npages, u64 page_size, + u64 exported_size, u64 offset, struct device *dev, + enum dma_data_direction dir) { u64 dma_max_seg_size, curr_page, size, chunk_size, left_size_to_export, left_size_in_page, left_size_in_dma_seg, device_address, bar_address, start_page; struct asic_fixed_properties *prop = &hdev->asic_prop; + struct dma_buf_sgt_mapping *mapping; struct scatterlist *sg; unsigned int nents, i; struct sg_table *sgt; @@ -1553,9 +1556,10 @@ static struct sg_table *alloc_sgt_from_device_pages(struct hl_device *hdev, u64 return ERR_PTR(-EINVAL); } - sgt = kzalloc_obj(*sgt); - if (!sgt) + mapping = kzalloc_obj(*mapping); + if (!mapping) return ERR_PTR(-ENOMEM); + sgt = &mapping->sgt; /* Use the offset to move to the actual first page that is exported */ for (start_page = 0 ; start_page < npages ; ++start_page) { @@ -1686,7 +1690,7 @@ static struct sg_table *alloc_sgt_from_device_pages(struct hl_device *hdev, u64 "SG entry %d: address %#llx, length %#x\n", i, sg_dma_address(sg), sg_dma_len(sg)); - return sgt; + return mapping; err_unmap: for_each_sgtable_dma_sg(sgt, sg, i) { @@ -1700,7 +1704,7 @@ static struct sg_table *alloc_sgt_from_device_pages(struct hl_device *hdev, u64 sg_free_table(sgt); err_free_sgt: - kfree(sgt); + kfree(mapping); return ERR_PTR(rc); } @@ -1714,22 +1718,22 @@ static int hl_dmabuf_attach(struct dma_buf *dmabuf, hl_dmabuf = dmabuf->priv; hdev = hl_dmabuf->ctx->hdev; - rc = pci_p2pdma_distance(hdev->pdev, attachment->dev, true); + rc = pci_p2pdma_distance(hdev->pdev, dma_buf_sgt_dma_device(attachment), true); if (rc < 0) attachment->peer2peer = false; return 0; } -static struct sg_table *hl_map_dmabuf(struct dma_buf_attachment *attachment, - enum dma_data_direction dir) +static struct dma_buf_sgt_mapping * +hl_map_dmabuf(struct dma_buf_attachment *attachment, enum dma_data_direction dir) { u64 *pages, npages, page_size, exported_size, offset; struct dma_buf *dma_buf = attachment->dmabuf; struct hl_vm_phys_pg_pack *phys_pg_pack; + struct dma_buf_sgt_mapping *mapping; struct hl_dmabuf_priv *hl_dmabuf; struct hl_device *hdev; - struct sg_table *sgt; hl_dmabuf = dma_buf->priv; hdev = hl_dmabuf->ctx->hdev; @@ -1753,18 +1757,21 @@ static struct sg_table *hl_map_dmabuf(struct dma_buf_attachment *attachment, page_size = hl_dmabuf->dmabuf->size; } - sgt = alloc_sgt_from_device_pages(hdev, pages, npages, page_size, exported_size, offset, - attachment->dev, dir); - if (IS_ERR(sgt)) - dev_err(hdev->dev, "failed (%ld) to initialize sgt for dmabuf\n", PTR_ERR(sgt)); + mapping = alloc_sgt_from_device_pages(hdev, pages, npages, page_size, + exported_size, offset, + dma_buf_sgt_dma_device(attachment), + dir); + if (IS_ERR(mapping)) + dev_err(hdev->dev, "failed (%ld) to initialize sgt for dmabuf\n", PTR_ERR(mapping)); - return sgt; + return mapping; } static void hl_unmap_dmabuf(struct dma_buf_attachment *attachment, - struct sg_table *sgt, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction dir) { + struct sg_table *sgt = &mapping->sgt; struct scatterlist *sg; int i; @@ -1776,14 +1783,14 @@ static void hl_unmap_dmabuf(struct dma_buf_attachment *attachment, * a sync of the memory to the CPU's cache, as it never resided inside that cache. */ for_each_sgtable_dma_sg(sgt, sg, i) - dma_unmap_resource(attachment->dev, sg_dma_address(sg), - sg_dma_len(sg), dir, - DMA_ATTR_SKIP_CPU_SYNC); + dma_unmap_resource(dma_buf_sgt_mapping_dma_device(mapping), + sg_dma_address(sg), sg_dma_len(sg), dir, + DMA_ATTR_SKIP_CPU_SYNC); /* Need to restore orig_nents because sg_free_table use that field */ sgt->orig_nents = sgt->nents; sg_free_table(sgt); - kfree(sgt); + kfree(mapping); } static struct hl_vm_hash_node *memhash_node_export_get(struct hl_ctx *ctx, u64 addr) @@ -1850,9 +1857,8 @@ static void hl_release_dmabuf(struct dma_buf *dmabuf) static const struct dma_buf_ops habanalabs_dmabuf_ops = { .attach = hl_dmabuf_attach, - .map_dma_buf = hl_map_dmabuf, - .unmap_dma_buf = hl_unmap_dmabuf, .release = hl_release_dmabuf, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(hl_map_dmabuf, hl_unmap_dmabuf), }; static int export_dmabuf(struct hl_ctx *ctx, diff --git a/drivers/gpu/drm/armada/armada_gem.c b/drivers/gpu/drm/armada/armada_gem.c index 044acdef6351..1eff00567386 100644 --- a/drivers/gpu/drm/armada/armada_gem.c +++ b/drivers/gpu/drm/armada/armada_gem.c @@ -4,6 +4,7 @@ */ #include <linux/dma-buf.h> +#include <linux/dma-buf-mapping.h> #include <linux/dma-mapping.h> #include <linux/mman.h> #include <linux/shmem_fs.h> @@ -383,41 +384,44 @@ int armada_gem_pwrite_ioctl(struct drm_device *dev, void *data, } /* Prime support */ -static struct sg_table * +static struct dma_buf_sgt_mapping * armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, enum dma_data_direction dir) { + struct device *dma_dev = dma_buf_sgt_dma_device(attach); struct drm_gem_object *obj = attach->dmabuf->priv; struct armada_gem_object *dobj = drm_to_armada_gem(obj); + struct dma_buf_sgt_mapping *mapping; struct scatterlist *sg; struct sg_table *sgt; int i; - sgt = kmalloc_obj(*sgt); - if (!sgt) + mapping = kzalloc_obj(*mapping); + if (!mapping) return NULL; + sgt = &mapping->sgt; if (dobj->obj.filp) { - struct address_space *mapping; + struct address_space *file_mapping; int count; count = dobj->obj.size / PAGE_SIZE; if (sg_alloc_table(sgt, count, GFP_KERNEL)) goto free_sgt; - mapping = dobj->obj.filp->f_mapping; + file_mapping = dobj->obj.filp->f_mapping; for_each_sgtable_sg(sgt, sg, i) { struct page *page; - page = shmem_read_mapping_page(mapping, i); + page = shmem_read_mapping_page(file_mapping, i); if (IS_ERR(page)) goto release; sg_set_page(sg, page, PAGE_SIZE, 0); } - if (dma_map_sgtable(attach->dev, sgt, dir, 0)) + if (dma_map_sgtable(dma_dev, sgt, dir, 0)) goto release; } else if (dobj->page) { /* Single contiguous page */ @@ -426,7 +430,7 @@ armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, sg_set_page(sgt->sgl, dobj->page, dobj->obj.size, 0); - if (dma_map_sgtable(attach->dev, sgt, dir, 0)) + if (dma_map_sgtable(dma_dev, sgt, dir, 0)) goto free_table; } else if (dobj->linear) { /* Single contiguous physical region - no struct page */ @@ -437,7 +441,7 @@ armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, } else { goto free_sgt; } - return sgt; + return mapping; release: for_each_sgtable_sg(sgt, sg, i) @@ -446,19 +450,21 @@ armada_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, free_table: sg_free_table(sgt); free_sgt: - kfree(sgt); + kfree(mapping); return NULL; } static void armada_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach, - struct sg_table *sgt, enum dma_data_direction dir) + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction dir) { struct drm_gem_object *obj = attach->dmabuf->priv; struct armada_gem_object *dobj = drm_to_armada_gem(obj); + struct sg_table *sgt = &mapping->sgt; int i; if (!dobj->linear) - dma_unmap_sgtable(attach->dev, sgt, dir, 0); + dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping), sgt, + dir, 0); if (dobj->obj.filp) { struct scatterlist *sg; @@ -468,7 +474,7 @@ static void armada_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach, } sg_free_table(sgt); - kfree(sgt); + kfree(mapping); } static int @@ -478,10 +484,10 @@ armada_gem_dmabuf_mmap(struct dma_buf *buf, struct vm_area_struct *vma) } static const struct dma_buf_ops armada_gem_prime_dmabuf_ops = { - .map_dma_buf = armada_gem_prime_map_dma_buf, - .unmap_dma_buf = armada_gem_prime_unmap_dma_buf, .release = drm_gem_dmabuf_release, .mmap = armada_gem_dmabuf_mmap, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(armada_gem_prime_map_dma_buf, + armada_gem_prime_unmap_dma_buf), }; struct dma_buf * diff --git a/drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c b/drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c index f2863da7f5c1..bba066f3cb50 100644 --- a/drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c +++ b/drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c @@ -4,20 +4,24 @@ * Copyright © 2016 Intel Corporation */ +#include <linux/dma-buf-mapping.h> #include <linux/vmalloc.h> #include "mock_dmabuf.h" -static struct sg_table *mock_map_dma_buf(struct dma_buf_attachment *attachment, - enum dma_data_direction dir) +static struct dma_buf_sgt_mapping * +mock_map_dma_buf(struct dma_buf_attachment *attachment, + enum dma_data_direction dir) { struct mock_dmabuf *mock = to_mock(attachment->dmabuf); + struct dma_buf_sgt_mapping *mapping; struct sg_table *st; struct scatterlist *sg; int i, err; - st = kmalloc_obj(*st); - if (!st) + mapping = kzalloc_obj(*mapping); + if (!mapping) return ERR_PTR(-ENOMEM); + st = &mapping->sgt; err = sg_alloc_table(st, mock->npages, GFP_KERNEL); if (err) @@ -29,26 +33,27 @@ static struct sg_table *mock_map_dma_buf(struct dma_buf_attachment *attachment, sg = sg_next(sg); } - err = dma_map_sgtable(attachment->dev, st, dir, 0); + err = dma_map_sgtable(dma_buf_sgt_dma_device(attachment), st, dir, 0); if (err) goto err_st; - return st; + return mapping; err_st: sg_free_table(st); err_free: - kfree(st); + kfree(mapping); return ERR_PTR(err); } static void mock_unmap_dma_buf(struct dma_buf_attachment *attachment, - struct sg_table *st, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction dir) { - dma_unmap_sgtable(attachment->dev, st, dir, 0); - sg_free_table(st); - kfree(st); + dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping), + &mapping->sgt, dir, 0); + sg_free_table(&mapping->sgt); + kfree(mapping); } static void mock_dmabuf_release(struct dma_buf *dma_buf) @@ -88,12 +93,11 @@ static int mock_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma) } static const struct dma_buf_ops mock_dmabuf_ops = { - .map_dma_buf = mock_map_dma_buf, - .unmap_dma_buf = mock_unmap_dma_buf, .release = mock_dmabuf_release, .mmap = mock_dmabuf_mmap, .vmap = mock_dmabuf_vmap, .vunmap = mock_dmabuf_vunmap, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(mock_map_dma_buf, mock_unmap_dma_buf), }; static struct dma_buf *mock_dmabuf(int npages) diff --git a/drivers/gpu/drm/tegra/gem.c b/drivers/gpu/drm/tegra/gem.c index e6aa6dae4ec8..8e288a34f10f 100644 --- a/drivers/gpu/drm/tegra/gem.c +++ b/drivers/gpu/drm/tegra/gem.c @@ -11,6 +11,7 @@ */ #include <linux/dma-buf.h> +#include <linux/dma-buf-mapping.h> #include <linux/iommu.h> #include <linux/module.h> #include <linux/pagemap.h> @@ -626,51 +627,56 @@ int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma) return __tegra_gem_mmap(gem, vma); } -static struct sg_table * +static struct dma_buf_sgt_mapping * tegra_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, enum dma_data_direction dir) { + struct device *dma_dev = dma_buf_sgt_dma_device(attach); struct drm_gem_object *gem = attach->dmabuf->priv; struct tegra_bo *bo = to_tegra_bo(gem); + struct dma_buf_sgt_mapping *mapping; struct sg_table *sgt; - sgt = kmalloc_obj(*sgt); - if (!sgt) + mapping = kzalloc_obj(*mapping); + if (!mapping) return NULL; + sgt = &mapping->sgt; if (bo->pages) { if (sg_alloc_table_from_pages(sgt, bo->pages, bo->num_pages, 0, gem->size, GFP_KERNEL) < 0) goto free; } else { - if (dma_get_sgtable(attach->dev, sgt, bo->vaddr, bo->iova, + if (dma_get_sgtable(dma_dev, sgt, bo->vaddr, bo->iova, gem->size) < 0) goto free; } - if (dma_map_sgtable(attach->dev, sgt, dir, 0)) + if (dma_map_sgtable(dma_dev, sgt, dir, 0)) goto free; - return sgt; + return mapping; free: sg_free_table(sgt); - kfree(sgt); + kfree(mapping); return NULL; } static void tegra_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach, - struct sg_table *sgt, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction dir) { struct drm_gem_object *gem = attach->dmabuf->priv; struct tegra_bo *bo = to_tegra_bo(gem); + struct sg_table *sgt = &mapping->sgt; if (bo->pages) - dma_unmap_sgtable(attach->dev, sgt, dir, 0); + dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping), sgt, + dir, 0); sg_free_table(sgt); - kfree(sgt); + kfree(mapping); } static void tegra_gem_prime_release(struct dma_buf *buf) @@ -740,14 +746,14 @@ static void tegra_gem_prime_vunmap(struct dma_buf *buf, struct iosys_map *map) } static const struct dma_buf_ops tegra_gem_prime_dmabuf_ops = { - .map_dma_buf = tegra_gem_prime_map_dma_buf, - .unmap_dma_buf = tegra_gem_prime_unmap_dma_buf, .release = tegra_gem_prime_release, .begin_cpu_access = tegra_gem_prime_begin_cpu_access, .end_cpu_access = tegra_gem_prime_end_cpu_access, .mmap = tegra_gem_prime_mmap, .vmap = tegra_gem_prime_vmap, .vunmap = tegra_gem_prime_vunmap, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(tegra_gem_prime_map_dma_buf, + tegra_gem_prime_unmap_dma_buf), }; struct dma_buf *tegra_gem_prime_export(struct drm_gem_object *gem, diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c index 64ea19253ee3..3502d0b37bd1 100644 --- a/samples/vfio-mdev/mbochs.c +++ b/samples/vfio-mdev/mbochs.c @@ -32,6 +32,7 @@ #include <linux/pci.h> #include <linux/dma-buf.h> #include <linux/highmem.h> +#include <linux/dma-buf-mapping.h> #include <drm/drm_fourcc.h> #include <drm/drm_rect.h> #include <drm/drm_modeset_lock.h> @@ -857,46 +858,51 @@ static void mbochs_print_dmabuf(struct mbochs_dmabuf *dmabuf, dmabuf->mode.offset, dmabuf->mode.size, dmabuf->pagecount); } -static struct sg_table *mbochs_map_dmabuf(struct dma_buf_attachment *at, - enum dma_data_direction direction) +static struct dma_buf_sgt_mapping * +mbochs_map_dmabuf(struct dma_buf_attachment *at, + enum dma_data_direction direction) { struct mbochs_dmabuf *dmabuf = at->dmabuf->priv; struct device *dev = mdev_dev(dmabuf->mdev_state->mdev); + struct dma_buf_sgt_mapping *mapping; struct sg_table *sg; dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id); - sg = kzalloc(sizeof(*sg), GFP_KERNEL); - if (!sg) + mapping = kzalloc_obj(*mapping); + if (!mapping) goto err1; + sg = &mapping->sgt; if (sg_alloc_table_from_pages(sg, dmabuf->pages, dmabuf->pagecount, 0, dmabuf->mode.size, GFP_KERNEL) < 0) goto err2; - if (dma_map_sgtable(at->dev, sg, direction, 0)) + if (dma_map_sgtable(dma_buf_sgt_dma_device(at), sg, direction, 0)) goto err3; - return sg; + return mapping; err3: sg_free_table(sg); err2: - kfree(sg); + kfree(mapping); err1: return ERR_PTR(-ENOMEM); } static void mbochs_unmap_dmabuf(struct dma_buf_attachment *at, - struct sg_table *sg, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction direction) { struct mbochs_dmabuf *dmabuf = at->dmabuf->priv; struct device *dev = mdev_dev(dmabuf->mdev_state->mdev); + struct sg_table *sg = &mapping->sgt; dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id); - dma_unmap_sgtable(at->dev, sg, direction, 0); + dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping), sg, + direction, 0); sg_free_table(sg); - kfree(sg); + kfree(mapping); } static void mbochs_release_dmabuf(struct dma_buf *buf) @@ -918,11 +924,10 @@ static void mbochs_release_dmabuf(struct dma_buf *buf) mutex_unlock(&mdev_state->ops_lock); } -static struct dma_buf_ops mbochs_dmabuf_ops = { - .map_dma_buf = mbochs_map_dmabuf, - .unmap_dma_buf = mbochs_unmap_dmabuf, +static const struct dma_buf_ops mbochs_dmabuf_ops = { .release = mbochs_release_dmabuf, .mmap = mbochs_mmap_dmabuf, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(mbochs_map_dmabuf, mbochs_unmap_dmabuf), }; static struct mbochs_dmabuf *mbochs_dmabuf_alloc(struct mdev_state *mdev_state, diff --git a/sound/soc/fsl/fsl_asrc_m2m.c b/sound/soc/fsl/fsl_asrc_m2m.c index 58101ce07c9f..79fde68f9394 100644 --- a/sound/soc/fsl/fsl_asrc_m2m.c +++ b/sound/soc/fsl/fsl_asrc_m2m.c @@ -7,6 +7,7 @@ #include <linux/dma/imx-dma.h> #include <linux/dma-buf.h> +#include <linux/dma-buf-mapping.h> #include <linux/dma-mapping.h> #include <linux/pm_runtime.h> #include <sound/asound.h> @@ -414,35 +415,42 @@ static int fsl_asrc_m2m_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) return snd_dma_buffer_mmap(dmab, vma); } -static struct sg_table *fsl_asrc_m2m_map_dma_buf(struct dma_buf_attachment *attachment, - enum dma_data_direction direction) +static struct dma_buf_sgt_mapping * +fsl_asrc_m2m_map_dma_buf(struct dma_buf_attachment *attachment, + enum dma_data_direction direction) { + struct device *dma_dev = dma_buf_sgt_dma_device(attachment); struct snd_dma_buffer *dmab = attachment->dmabuf->priv; + struct dma_buf_sgt_mapping *mapping; struct sg_table *sgt; - sgt = kmalloc_obj(*sgt); - if (!sgt) + mapping = kzalloc_obj(*mapping); + if (!mapping) return NULL; + sgt = &mapping->sgt; - if (dma_get_sgtable(attachment->dev, sgt, dmab->area, dmab->addr, dmab->bytes) < 0) + if (dma_get_sgtable(dma_dev, sgt, dmab->area, dmab->addr, dmab->bytes) < 0) goto free; - if (dma_map_sgtable(attachment->dev, sgt, direction, 0)) + if (dma_map_sgtable(dma_dev, sgt, direction, 0)) goto free; - return sgt; + return mapping; free: sg_free_table(sgt); - kfree(sgt); + kfree(mapping); return NULL; } static void fsl_asrc_m2m_unmap_dma_buf(struct dma_buf_attachment *attachment, - struct sg_table *table, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction direction) { - dma_unmap_sgtable(attachment->dev, table, direction, 0); + dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping), + &mapping->sgt, direction, 0); + sg_free_table(&mapping->sgt); + kfree(mapping); } static void fsl_asrc_m2m_release(struct dma_buf *dmabuf) @@ -452,9 +460,9 @@ static void fsl_asrc_m2m_release(struct dma_buf *dmabuf) static const struct dma_buf_ops fsl_asrc_m2m_dma_buf_ops = { .mmap = fsl_asrc_m2m_mmap, - .map_dma_buf = fsl_asrc_m2m_map_dma_buf, - .unmap_dma_buf = fsl_asrc_m2m_unmap_dma_buf, .release = fsl_asrc_m2m_release, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(fsl_asrc_m2m_map_dma_buf, + fsl_asrc_m2m_unmap_dma_buf), }; static int fsl_asrc_m2m_comp_task_create(struct snd_compr_stream *stream, -- 2.54.0
