Skip to contentSkip to conversation

DMA-buf mapping types

PATCH In review Alistair Popple 3 participants 30 messages Nov 20 – Nov 21

Subsystems (45)
lore
Share & tools
lore
#0 lore raw

v3 cover DMA-buf mapping types

This is v3 of the DMA-buf mapping types series [1]. The main difference from v2 is exporters have been changed to allocate a type-specific mapping struct and the PCIe P2P changes have been deferred to a follow-up patch series. The exporter conversion was primarily motivated by a need to have the importer call the correct unmap functions even if the selected attachment type has subsequently changed after an invalidation.

Doing this conversion though will make it easier in future to move away from providing the sgt data type to importers. A future patch series will be posted to do that.

The PCIe P2P work was deferred in order to keep this series mostly a mechanical "no behavioural change" that introduces the types, does the conversion of exporters and adds the type selection contract/logic.

A follow up adding PCIe P2P as its own type will be posted shortly after this.

Patch Summary
-------------

Patch 1 renames dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt().

Patches 2-6 introduce the DMA-buf mapping type system, initially developed by Jason Gunthorpe.

Patches 7-19 convert exporters to allocate and return a dma_buf_sgt_mapping embedding an sg_table instead of an sg_table directly. These are mechanical and should not change behaviour.

The mechanical changes for smaller drivers has been split up by class based on how the sg_table is allocated (eg. per-map, attachment, etc.) with corner cases getting their own patch. Larger drivers, such as Intel Xe and amdgpu also get their own patch.

Patch 20 removes the compatibility code the conversion needed and patch 21 adds KUnit tests for the matching rules.

Patches 22-24 add the contract and logic for making and changing exporter types. Nothing currently exports two types, but a follow up series building on this with a PCIe P2P type will be posted shortly.

Future Work
-----------

Several series will build on top of this. Current proposals are:

1. A PCIe P2P mapping type series to handle P2P negotiation.
2. An exporter type that allows conversion to iommufd or new DMA address list instead of sg_table.
3. Possible exporters/importers for private interconnects such as converting existing XGMI code to use this or future UALink exporters/importers.

Why mapping types
-----------------

Since its introduction, DMA-buf has only supported using scatterlist for the exporter and importer to exchange address information. This is not sufficient for all use cases as dma_addr_t is a very specific and limited type that should not be abused for things unrelated to the DMA API.

There are several motivations for addressing this now:
1) VFIO to IOMMUFD and KVM requires a physical address, not a dma_addr_t scatterlist, it cannot be represented in the scatterlist structure
2) xe vGPU requires the host driver to accept a DMABUF from VFIO of its own VF and convert it into an internal VRAM address on the PF
3) We are starting to look at replacement datastructures for scatterlist
4) Ideas around UALink/etc are suggesting not using the DMA API
5) Private interconnects, such as XGMI, are forced to work around this using driver-specific hacks.

None of these can be achieved using scatterlist. This series introduces a "mapping type" mechanism allowing dma-buf to negotiate compatibility between different types and support alternate mapping options. See the description of patch 2 for more details.

Selecting mapping type
----------------------

Importers and exporters list possible mapping types with the exporter responsible for selecting the final type at pin/map time under the dma_resv lock. A matching phase at attachment time eliminates types which statically cannot be supported for a given exporter/importer.

At pin/map the exporter chooses one of the types matched, taking the other attachments and the buffer's placement into account. That choice is held by the core until the exporter explicitly invalidates the attachment. This ensures unmap/remap cannot cause buffer movement and ping/pong.

The dma-buf core enforces this by calling select_mapping() only on the first pin/map after attach or after an invalidate. The pin/map type can therefore only change once the exporter has explicitly invalidated the mapping and the choice is remembered across unmap/map.

Testing
-------

- KUnit dma-buf-mapping suite passes with new tests to address issue found by Karl previously.
- iommufd self-test passes.
- Every patch compile-tested with a config enabling all converted drivers.
- A small test exporting via vfio-pci and importing and pinning via iommufd.

Changes since v2
----------------

Link to v2: https://patch.msgid.link/[email protected]

- PCIe P2P is no longer negotiated in this series. P2P keeps working through allow_peer2peer and attach->peer2peer exactly as today, a future series will introduce a PCIe P2P type to introduce negotiation.
- Exporters allocate a mapping object with sg_table embedded and their unmap takes it back.
- Exporter unmap callbacks take the DMA device from the mapping rather than from the attachment so a mapping that outlives an invalidate and a new selection is unmapped against the device it was made for.
- dma_buf_map_sgt() and dma_buf_unmap_sgt() are now wrappers around the generic dma_buf_mapping_map() and dma_buf_mapping_unmap().
- The single "Convert all the simple exporters" patch is split by class of change.
- A pre-existing leak in fsl_asrc_m2m's unmap was fixed as a side-effect.
- Added more KUnit tests to test corner cases identified by Karl in previous postings.

Changes since v1 [2]
--------------------

- Rebased onto v7.4-rc1. The rename also covers the new nvme-pci DMA-buf importer and devmem's switch to pinning. xe's dynamic-debug knob for the p2pdma distance check (3bc497a40178) is dropped, since the check is now done by the core at match time, and amdgpu's PREEMPT-domain import decision (78cdd96ab497) asks the negotiated match set instead of attach->peer2peer.
- The dma_buf_map_attachment()/dma_buf_unmap_attachment() rename is now the first patch, squashed into one, and uses the shorter dma_buf_map_sgt() and dma_buf_unmap_sgt() names (Christian [3]).
- Dropped dma_buf_mapping_attach() and the dma_buf_sgt_attach() / dma_buf_sgt_dynamic_attach() renames. Importers that support more than the SGT type list them in a static dma_buf_attach_ops ->supported_mapping_types table. The importing device is passed to the mapping types through the match arguments rather than stored in each entry. dma_buf_attach() and dma_buf_dynamic_attach() are unchanged for everyone else (Christian [3]).
- The selected type is an index into the attachment's match set, read through dma_buf_attach_map_type(), instead of a copy in attach->map_type. finish_match() fills the match set entry directly and select_mapping() returns the index.
- A failed pin or map only undoes a selection it made itself, so a map failing under a held pin keeps the pinned type and a failed first pin leaves the attachment unselected; KUnit tests for both.
- dma_buf_match_mapping() returns -EOPNOTSUPP when the two sides have no type in common, keeping -EINVAL for an empty list, which restores the error xe's own tests expect (Sashiko).
- New DMA_SGT_EXPORTER_REQUIRES_P2P flavour that requires a P2P capable importer without a pci_p2pdma_distance() check at match time, used by vfio and uverbs so iommufd's non-PCI device can still attach and so no pci_dev is dereferenced outside the dma_resv lock (Sashiko).
- The SGT P2P distance check is quiet, as it was in xe and amdgpu before the conversion (Sashiko).
- Fixed a NULL importer_ops dereference in dma_buf_dynamic_attach() and the NULL check order in dma_buf_unmap_sgt() (Sashiko).
- Signed-off-by added to every patch (Jason). Patch 4 is now authored by me with Jason as co-developer, as little of the original remains.
- Not addressed: the Tegra fault with CONFIG_DMABUF_DEBUG that Karl reported [4] is a pre-existing Tegra bug being fixed separately.

Changes since RFC v1 [1]
------------------------

- Dropped the PAL type and its vfio/iommufd users (RFC 21-25).
- Rebased onto v7.3-rc5: move_notify is now invalidate_mappings, CONFIG_DMABUF_MOVE_NOTIFY is gone, new amdxdna/panfrost/panthor exporters converted, kzalloc_obj().
- Patch 1 records the whole match set in a fixed-size array in the attachment; patch 3 initialises the attachment before match_mapping() (Karl); patch 15 holds a pci_dev reference instead of asserting the resv lock (Baolu).
- New patches 5, 6 and 7: exporter selection at pin/map with core-enforced stickiness, match set queries, and dma_buf_mapping_map()/unmap().
- xe and amdgpu conversions written against the final API; amdgpu's XGMI override rewrites the recorded match set entry.
- New patch 26 with KUnit tests for the selection contract.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/

---
Alistair Popple (14):
dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach()
dma-buf: Allow single exporter drivers to avoid the match_mapping function
dma-buf: Convert exporters allocating a table per map to the SGT mapping type iommufd/selftest: Convert the test exporter to the SGT mapping type
dma-buf: Convert exporters keeping a table per attachment to the SGT mapping type
drm: Convert drm_gem_map_dma_buf() and its users to the SGT mapping type
udmabuf: Convert to the SGT mapping type
drm/omapdrm: Convert to the SGT mapping type
accel/ivpu: Convert the userptr exporter to the SGT mapping type
dma-buf: Convert dma_buf_phys_vec_to_sgt() and its users to the SGT mapping type
dma-buf: Remove unused SGT stuff from the common structures
dma-buf: Let the exporter select mapping type at pin/map
dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt
dma-buf: Add kunit tests for mapping type selection

Jason Gunthorpe (10):
treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt()
dma-buf: Introduce DMA-buf mapping types
dma-buf: Add the SGT DMA mapping type
dma-buf: Route SGT related actions through the attachment's mapping type drm/xe/dma-buf: Use the SGT mapping type
drm/amdgpu: Use the SGT mapping type
drm/vmwgfx: Use match_mapping instead of dummy calls
iio: buffer: convert to use the SGT mapping type
functionfs: convert to use the SGT mapping type
dma-buf: Add kunit tests for mapping type

 drivers/accel/amdxdna/amdxdna_cbuf.c               |   30 +-
 drivers/accel/amdxdna/amdxdna_gem.c                |   11 +-
 drivers/accel/amdxdna/amdxdna_ubuf.c               |   34 +-
 drivers/accel/habanalabs/common/memory.c           |   52 +-
 drivers/accel/ivpu/ivpu_gem.c                      |    6 +-
 drivers/accel/ivpu/ivpu_gem_userptr.c              |   29 +-
 drivers/accel/qaic/qaic_data.c                     |    6 +-
 drivers/dma-buf/Makefile                           |    1 +
 drivers/dma-buf/dma-buf-mapping-internal.h         |   39 +
 drivers/dma-buf/dma-buf-mapping.c                  |  307 +++++-
 drivers/dma-buf/dma-buf.c                          |  349 ++++--
 drivers/dma-buf/heaps/cma_heap.c                   |   33 +-
 drivers/dma-buf/heaps/system_heap.c                |   34 +-
 drivers/dma-buf/st-dma-mapping.c                   | 1106 ++++++++++++++++++++
 drivers/dma-buf/udmabuf.c                          |   71 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c        |   85 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c            |   29 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h            |    4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c       |   30 +-
 drivers/gpu/drm/armada/armada_gem.c                |   52 +-
 drivers/gpu/drm/drm_prime.c                        |   78 +-
 drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c         |   31 +-
 drivers/gpu/drm/i915/gem/i915_gem_object.c         |    2 +-
 .../gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c   |    4 +-
 drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c   |   30 +-
 drivers/gpu/drm/msm/msm_gem_prime.c                |    7 +-
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c          |   51 +-
 drivers/gpu/drm/panfrost/panfrost_gem.c            |   27 +-
 drivers/gpu/drm/panthor/panthor_gem.c              |   25 +-
 drivers/gpu/drm/tegra/gem.c                        |   44 +-
 drivers/gpu/drm/virtio/virtgpu_drv.h               |    9 +-
 drivers/gpu/drm/virtio/virtgpu_prime.c             |   30 +-
 drivers/gpu/drm/virtio/virtgpu_vram.c              |   25 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_prime.c              |   32 +-
 drivers/gpu/drm/xe/xe_bo.c                         |   16 +-
 drivers/gpu/drm/xe/xe_dma_buf.c                    |   62 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c               |   22 +-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.h               |    2 +-
 drivers/iio/industrialio-buffer.c                  |   13 +-
 drivers/infiniband/core/umem_dmabuf.c              |    8 +-
 drivers/infiniband/core/uverbs_std_types_dmabuf.c  |   23 +-
 drivers/iommu/iommufd/selftest.c                   |   22 +-
 drivers/media/common/videobuf2/videobuf2-core.c    |    2 +-
 .../media/common/videobuf2/videobuf2-dma-contig.c  |   42 +-
 drivers/media/common/videobuf2/videobuf2-dma-sg.c  |   37 +-
 drivers/media/common/videobuf2/videobuf2-vmalloc.c |   32 +-
 .../media/platform/nvidia/tegra-vde/dmabuf-cache.c |    6 +-
 drivers/misc/fastrpc.c                             |   36 +-
 drivers/nvme/host/pci-dmabuf.c                     |    6 +-
 drivers/tee/tee_heap.c                             |   28 +-
 drivers/usb/gadget/function/f_fs.c                 |    9 +-
 drivers/vfio/pci/vfio_pci_dmabuf.c                 |   24 +-
 drivers/xen/gntdev-dmabuf.c                        |   75 +-
 include/drm/drm_prime.h                            |    8 +-
 include/linux/dma-buf-mapping.h                    |  366 ++++++-
 include/linux/dma-buf.h                            |  212 ++--
 include/trace/events/dma_buf.h                     |    2 +-
 io_uring/zcrx.c                                    |    6 +-
 net/core/devmem.c                                  |    8 +-
 samples/vfio-mdev/mbochs.c                         |   31 +-
 sound/soc/fsl/fsl_asrc_m2m.c                       |   32 +-
 61 files changed, 2972 insertions(+), 861 deletions(-)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261105-dma_mapping_type_v4-f71180b977e8

Best regards,

Signature · 1 line
Alistair Popple <[email protected]>
#1 lore raw

PATCH v3 1/24 treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt()

From: Jason Gunthorpe <[email protected]>

These map and unmap functions only work with SGT importers. Rename them, and their _unlocked variants, so the scatterlist based interface is clearly named as such before further mapping types are added.

---
 drivers/accel/amdxdna/amdxdna_gem.c                |  6 +--
 drivers/accel/ivpu/ivpu_gem.c                      |  6 +--
 drivers/accel/qaic/qaic_data.c                     |  6 +--
 drivers/dma-buf/dma-buf.c                          | 54 +++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c            |  4 +-
 drivers/gpu/drm/armada/armada_gem.c                | 16 +++----
 drivers/gpu/drm/drm_prime.c                        |  6 +--
 drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c         |  8 ++--
 drivers/gpu/drm/i915/gem/i915_gem_object.c         |  2 +-
 .../gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c   |  4 +-
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c          |  4 +-
 drivers/gpu/drm/tegra/gem.c                        | 14 +++---
 drivers/gpu/drm/virtio/virtgpu_prime.c             |  8 ++--
 drivers/gpu/drm/xe/xe_bo.c                         | 16 +++----
 drivers/iio/industrialio-buffer.c                  |  6 +--
 drivers/infiniband/core/umem_dmabuf.c              |  8 ++--
 drivers/media/common/videobuf2/videobuf2-core.c    |  2 +-
 .../media/common/videobuf2/videobuf2-dma-contig.c  |  8 ++--
 drivers/media/common/videobuf2/videobuf2-dma-sg.c  |  4 +-
 .../media/platform/nvidia/tegra-vde/dmabuf-cache.c |  6 +--
 drivers/misc/fastrpc.c                             |  6 +--
 drivers/nvme/host/pci-dmabuf.c                     |  6 +--
 drivers/usb/gadget/function/f_fs.c                 |  4 +-
 drivers/xen/gntdev-dmabuf.c                        |  8 ++--
 include/linux/dma-buf.h                            | 26 +++++------
 io_uring/zcrx.c                                    |  6 +--
 net/core/devmem.c                                  |  8 ++--
 27 files changed, 126 insertions(+), 126 deletions(-)
drivers/accel/amdxdna/amdxdna_gem.c +3 -3
index f14484bc31ec..adee6e806eed 100644
@@ -701,7 +701,7 @@ static struct dma_buf *amdxdna_gem_prime_export(struct drm_gem_object *gobj, int
 
 static void amdxdna_imported_obj_free(struct amdxdna_gem_obj *abo)
 {
-	dma_buf_unmap_attachment_unlocked(abo->attach, abo->base.sgt, DMA_BIDIRECTIONAL);
+	dma_buf_unmap_sgt_unlocked(abo->attach, abo->base.sgt, DMA_BIDIRECTIONAL);
 	dma_buf_detach(abo->dma_buf, abo->attach);
 	dma_buf_put(abo->dma_buf);
 	drm_gem_object_release(to_gobj(abo));
@@ -1075,7 +1075,7 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
 		goto put_buf;
 	}
 
-	sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+	sgt = dma_buf_map_sgt_unlocked(attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR(sgt)) {
 		ret = PTR_ERR(sgt);
 		goto fail_detach;
@@ -1096,7 +1096,7 @@ amdxdna_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)
 	return gobj;
 
 fail_unmap:
-	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+	dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
 fail_detach:
 	dma_buf_detach(dma_buf, attach);
 put_buf:
drivers/accel/ivpu/ivpu_gem.c +3 -3
index c521a53f89bb..276f2a04aeda 100644
@@ -54,7 +54,7 @@ static struct sg_table *ivpu_bo_map_attachment(struct ivpu_device *vdev, struct
 
 	sgt = bo->base.sgt;
 	if (!sgt) {
-		sgt = dma_buf_map_attachment(bo->base.base.import_attach, DMA_BIDIRECTIONAL);
+		sgt = dma_buf_map_sgt(bo->base.base.import_attach, DMA_BIDIRECTIONAL);
 		if (IS_ERR(sgt))
 			ivpu_err(vdev, "Failed to map BO in IOMMU: %ld\n", PTR_ERR(sgt));
 		else
@@ -158,8 +158,8 @@ static void ivpu_bo_unbind_locked(struct ivpu_bo *bo)
 
 	if (bo->base.sgt) {
 		if (drm_gem_is_imported(&bo->base.base)) {
-			dma_buf_unmap_attachment(bo->base.base.import_attach,
-						 bo->base.sgt, DMA_BIDIRECTIONAL);
+			dma_buf_unmap_sgt(bo->base.base.import_attach,
+					  bo->base.sgt, DMA_BIDIRECTIONAL);
 		} else {
 			dma_unmap_sgtable(vdev->drm.dev, bo->base.sgt, DMA_BIDIRECTIONAL, 0);
 			sg_free_table(bo->base.sgt);
drivers/accel/qaic/qaic_data.c +3 -3
index ee35e13091b3..d65578f0368e 100644
@@ -869,7 +869,7 @@ struct drm_gem_object *qaic_gem_prime_import(struct drm_device *dev, struct dma_
 
 	drm_gem_private_object_init(dev, obj, attach->dmabuf->size);
 	/*
-	 * skipping dma_buf_map_attachment() as we do not know the direction
+	 * skipping dma_buf_map_sgt() as we do not know the direction
 	 * just yet. Once the direction is known in the subsequent IOCTL to
 	 * attach slicing, we can do it then.
 	 */
@@ -895,7 +895,7 @@ static int qaic_prepare_import_bo(struct qaic_bo *bo, struct qaic_attach_slice_h
 	struct sg_table *sgt;
 	int ret;
 
-	sgt = dma_buf_map_attachment(obj->import_attach, hdr->dir);
+	sgt = dma_buf_map_sgt(obj->import_attach, hdr->dir);
 	if (IS_ERR(sgt)) {
 		ret = PTR_ERR(sgt);
 		return ret;
@@ -936,7 +936,7 @@ static int qaic_prepare_bo(struct qaic_device *qdev, struct qaic_bo *bo,
 
 static void qaic_unprepare_import_bo(struct qaic_bo *bo)
 {
-	dma_buf_unmap_attachment(bo->base.import_attach, bo->sgt, bo->dir);
+	dma_buf_unmap_sgt(bo->base.import_attach, bo->sgt, bo->dir);
 	bo->sgt = NULL;
 }
 
drivers/dma-buf/dma-buf.c +27 -27

147 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -2
index 0f86777250fc..30ec5cb9ddf5 100644
@@ -962,7 +962,7 @@ static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
 			struct sg_table *sgt;
 
 			attach = gtt->gobj->import_attach;
-			sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+			sgt = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
 			if (IS_ERR(sgt))
 				return PTR_ERR(sgt);
 
@@ -1083,7 +1083,7 @@ static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
 		struct dma_buf_attachment *attach;
 
 		attach = gtt->gobj->import_attach;
-		dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
+		dma_buf_unmap_sgt(attach, ttm->sg, DMA_BIDIRECTIONAL);
 		ttm->sg = NULL;
 	}
 
drivers/gpu/drm/armada/armada_gem.c +8 -8
index da7335cbe82d..044acdef6351 100644
@@ -67,8 +67,8 @@ void armada_gem_free_object(struct drm_gem_object *obj)
 	if (dobj->obj.import_attach) {
 		/* We only ever display imported data */
 		if (dobj->sgt)
-			dma_buf_unmap_attachment_unlocked(dobj->obj.import_attach,
-							  dobj->sgt, DMA_TO_DEVICE);
+			dma_buf_unmap_sgt_unlocked(dobj->obj.import_attach,
+						   dobj->sgt, DMA_TO_DEVICE);
 		drm_prime_gem_destroy(&dobj->obj, NULL);
 	}
 
@@ -529,7 +529,7 @@ armada_gem_prime_import(struct drm_device *dev, struct dma_buf *buf)
 	get_dma_buf(buf);
 
 	/*
-	 * Don't call dma_buf_map_attachment() here - it maps the
+	 * Don't call dma_buf_map_sgt() here - it maps the
 	 * scatterlist immediately for DMA, and this is not always
 	 * an appropriate thing to do.
 	 */
@@ -540,20 +540,20 @@ int armada_gem_map_import(struct armada_gem_object *dobj)
 {
 	int ret;
 
-	dobj->sgt = dma_buf_map_attachment_unlocked(dobj->obj.import_attach,
-						    DMA_TO_DEVICE);
+	dobj->sgt = dma_buf_map_sgt_unlocked(dobj->obj.import_attach,
+					     DMA_TO_DEVICE);
 	if (IS_ERR(dobj->sgt)) {
 		ret = PTR_ERR(dobj->sgt);
 		dobj->sgt = NULL;
-		DRM_ERROR("dma_buf_map_attachment() error: %d\n", ret);
+		DRM_ERROR("dma_buf_map_sgt() error: %d\n", ret);
 		return ret;
 	}
 	if (dobj->sgt->nents > 1) {
-		DRM_ERROR("dma_buf_map_attachment() returned an (unsupported) scattered list\n");
+		DRM_ERROR("dma_buf_map_sgt() returned an (unsupported) scattered list\n");
 		return -EINVAL;
 	}
 	if (sg_dma_len(dobj->sgt->sgl) < dobj->obj.size) {
-		DRM_ERROR("dma_buf_map_attachment() returned a small buffer\n");
+		DRM_ERROR("dma_buf_map_sgt() returned a small buffer\n");
 		return -EINVAL;
 	}
 	dobj->dev_addr = sg_dma_address(dobj->sgt->sgl);
drivers/gpu/drm/drm_prime.c +3 -3
index fe3436d1235d..e27bda3423ed 100644
@@ -993,7 +993,7 @@ struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
 
 	get_dma_buf(dma_buf);
 
-	sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+	sgt = dma_buf_map_sgt_unlocked(attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR(sgt)) {
 		ret = PTR_ERR(sgt);
 		goto fail_detach;
@@ -1011,7 +1011,7 @@ struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
 	return obj;
 
 fail_unmap:
-	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+	dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
 fail_detach:
 	dma_buf_detach(dma_buf, attach);
 	dma_buf_put(dma_buf);
@@ -1109,7 +1109,7 @@ void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
 
 	attach = obj->import_attach;
 	if (sg)
-		dma_buf_unmap_attachment_unlocked(attach, sg, DMA_BIDIRECTIONAL);
+		dma_buf_unmap_sgt_unlocked(attach, sg, DMA_BIDIRECTIONAL);
 	dma_buf = attach->dmabuf;
 	dma_buf_detach(attach->dmabuf, attach);
 	/* remove the reference */
drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c +4 -4
index b43d34c7d641..4ab54d2e616f 100644
@@ -240,8 +240,8 @@ static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
 
 	assert_object_held(obj);
 
-	sgt = dma_buf_map_attachment(obj->base.import_attach,
-				     DMA_BIDIRECTIONAL);
+	sgt = dma_buf_map_sgt(obj->base.import_attach,
+			      DMA_BIDIRECTIONAL);
 	if (IS_ERR(sgt))
 		return PTR_ERR(sgt);
 
@@ -268,8 +268,8 @@ static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
 static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj,
 					     struct sg_table *sgt)
 {
-	dma_buf_unmap_attachment(obj->base.import_attach, sgt,
-				 DMA_BIDIRECTIONAL);
+	dma_buf_unmap_sgt(obj->base.import_attach, sgt,
+			  DMA_BIDIRECTIONAL);
 }
 
 static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
drivers/gpu/drm/i915/gem/i915_gem_object.c +1 -1
index 9e01f8b2079a..1e0aafc3643b 100644
@@ -367,7 +367,7 @@ void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj)
 	atomic_set(&obj->mm.pages_pin_count, 0);
 
 	/*
-	 * dma_buf_unmap_attachment() requires reservation to be
+	 * dma_buf_unmap_sgt() requires reservation to be
 	 * locked. The imported GEM shouldn't share reservation lock
 	 * and ttm_bo_cleanup_memtype_use() shouldn't be invoked for
 	 * dma-buf, so it's safe to take the lock.
drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c +2 -2
index 2fda549dd82d..af9ced05f7a8 100644
@@ -293,7 +293,7 @@ static int igt_dmabuf_import_same_driver(struct drm_i915_private *i915,
 		goto out_import;
 	}
 
-	st = dma_buf_map_attachment_unlocked(import_attach, DMA_BIDIRECTIONAL);
+	st = dma_buf_map_sgt_unlocked(import_attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR(st)) {
 		err = PTR_ERR(st);
 		goto out_detach;
@@ -306,7 +306,7 @@ static int igt_dmabuf_import_same_driver(struct drm_i915_private *i915,
 		timeout = -ETIME;
 	}
 	err = timeout > 0 ? 0 : timeout;
-	dma_buf_unmap_attachment_unlocked(import_attach, st, DMA_BIDIRECTIONAL);
+	dma_buf_unmap_sgt_unlocked(import_attach, st, DMA_BIDIRECTIONAL);
 out_detach:
 	dma_buf_detach(dmabuf, import_attach);
 out_import:
drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +2 -2
index 30cf1cdc1aa3..afae0a56da8c 100644
@@ -120,7 +120,7 @@ struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
 
 	get_dma_buf(dma_buf);
 
-	sgt = dma_buf_map_attachment_unlocked(attach, DMA_TO_DEVICE);
+	sgt = dma_buf_map_sgt_unlocked(attach, DMA_TO_DEVICE);
 	if (IS_ERR(sgt)) {
 		ret = PTR_ERR(sgt);
 		goto fail_detach;
@@ -137,7 +137,7 @@ struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
 	return obj;
 
 fail_unmap:
-	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_TO_DEVICE);
+	dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_TO_DEVICE);
 fail_detach:
 	dma_buf_detach(dma_buf, attach);
 	dma_buf_put(dma_buf);
drivers/gpu/drm/tegra/gem.c +7 -7
index f76af733ea79..e6aa6dae4ec8 100644
@@ -86,7 +86,7 @@ static struct host1x_bo_mapping *tegra_bo_pin(struct device *dev, struct host1x_
 			goto free;
 		}
 
-		map->sgt = dma_buf_map_attachment_unlocked(map->attach, direction);
+		map->sgt = dma_buf_map_sgt_unlocked(map->attach, direction);
 		if (IS_ERR(map->sgt)) {
 			dma_buf_detach(buf, map->attach);
 			err = PTR_ERR(map->sgt);
@@ -162,8 +162,8 @@ static struct host1x_bo_mapping *tegra_bo_pin(struct device *dev, struct host1x_
 static void tegra_bo_unpin(struct host1x_bo_mapping *map)
 {
 	if (map->attach) {
-		dma_buf_unmap_attachment_unlocked(map->attach, map->sgt,
-						  map->direction);
+		dma_buf_unmap_sgt_unlocked(map->attach, map->sgt,
+					   map->direction);
 		dma_buf_detach(map->attach->dmabuf, map->attach);
 	} else {
 		dma_unmap_sgtable(map->dev, map->sgt, map->direction, 0);
@@ -479,7 +479,7 @@ static struct tegra_bo *tegra_bo_import(struct drm_device *drm,
 			goto free;
 		}
 
-		bo->sgt = dma_buf_map_attachment_unlocked(attach, DMA_TO_DEVICE);
+		bo->sgt = dma_buf_map_sgt_unlocked(attach, DMA_TO_DEVICE);
 		if (IS_ERR(bo->sgt)) {
 			err = PTR_ERR(bo->sgt);
 			goto detach;
@@ -499,7 +499,7 @@ static struct tegra_bo *tegra_bo_import(struct drm_device *drm,
 
 detach:
 	if (!IS_ERR_OR_NULL(bo->sgt))
-		dma_buf_unmap_attachment_unlocked(attach, bo->sgt, DMA_TO_DEVICE);
+		dma_buf_unmap_sgt_unlocked(attach, bo->sgt, DMA_TO_DEVICE);
 
 	dma_buf_detach(buf, attach);
 	dma_buf_put(buf);
@@ -520,8 +520,8 @@ void tegra_bo_free_object(struct drm_gem_object *gem)
 		tegra_bo_iommu_unmap(tegra, bo);
 
 		if (drm_gem_is_imported(gem)) {
-			dma_buf_unmap_attachment_unlocked(gem->import_attach, bo->sgt,
-							  DMA_TO_DEVICE);
+			dma_buf_unmap_sgt_unlocked(gem->import_attach, bo->sgt,
+						   DMA_TO_DEVICE);
 			dma_buf_detach(gem->import_attach->dmabuf, gem->import_attach);
 		}
 	}
drivers/gpu/drm/virtio/virtgpu_prime.c +4 -4
index ebf471044d06..a47783b47dcc 100644
@@ -161,13 +161,13 @@ int virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
 	if (ret <= 0)
 		return ret < 0 ? ret : -ETIMEDOUT;
 
-	sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+	sgt = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR(sgt))
 		return PTR_ERR(sgt);
 
 	*ents = kvmalloc_objs(struct virtio_gpu_mem_entry, sgt->nents);
 	if (!(*ents)) {
-		dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
+		dma_buf_unmap_sgt(attach, sgt, DMA_BIDIRECTIONAL);
 		return -ENOMEM;
 	}
 
@@ -192,8 +192,8 @@ static void virtgpu_dma_buf_unmap(struct virtio_gpu_object *bo)
 		virtio_gpu_detach_object_fenced(bo);
 
 		if (bo->sgt)
-			dma_buf_unmap_attachment(attach, bo->sgt,
-						 DMA_BIDIRECTIONAL);
+			dma_buf_unmap_sgt(attach, bo->sgt,
+					  DMA_BIDIRECTIONAL);
 
 		bo->sgt = NULL;
 	}
drivers/gpu/drm/xe/xe_bo.c +8 -8
index 12e523c61c1f..14b71d951659 100644
@@ -869,7 +869,7 @@ static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
 	    ttm_bo->sg) {
 		dma_resv_wait_timeout(ttm_bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
 				      false, MAX_SCHEDULE_TIMEOUT);
-		dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
+		dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
 		ttm_bo->sg = NULL;
 	}
 
@@ -877,11 +877,11 @@ static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
 		goto out;
 
 	if (ttm_bo->sg) {
-		dma_buf_unmap_attachment(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
+		dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
 		ttm_bo->sg = NULL;
 	}
 
-	sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
+	sg = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR(sg))
 		return PTR_ERR(sg);
 
@@ -1750,9 +1750,9 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
 		struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);
 
 		if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
-			dma_buf_unmap_attachment(ttm_bo->base.import_attach,
-						 ttm_bo->sg,
-						 DMA_BIDIRECTIONAL);
+			dma_buf_unmap_sgt(ttm_bo->base.import_attach,
+					  ttm_bo->sg,
+					  DMA_BIDIRECTIONAL);
 			ttm_bo->sg = NULL;
 			xe_tt->sg = NULL;
 		} else if (xe_tt->sg) {
@@ -1867,8 +1867,8 @@ static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
 		struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
 						       struct xe_ttm_tt, ttm);
 
-		dma_buf_unmap_attachment(ttm_bo->base.import_attach, ttm_bo->sg,
-					 DMA_BIDIRECTIONAL);
+		dma_buf_unmap_sgt(ttm_bo->base.import_attach, ttm_bo->sg,
+				  DMA_BIDIRECTIONAL);
 		ttm_bo->sg = NULL;
 		xe_tt->sg = NULL;
 	}
drivers/iio/industrialio-buffer.c +3 -3
index 4294101810fd..4a1063e4891f 100644
@@ -1593,7 +1593,7 @@ static void iio_buffer_dmabuf_release(struct kref *ref)
 	struct iio_buffer *buffer = priv->buffer;
 	struct dma_buf *dmabuf = attach->dmabuf;
 
-	dma_buf_unmap_attachment_unlocked(attach, priv->sgt, priv->dir);
+	dma_buf_unmap_sgt_unlocked(attach, priv->sgt, priv->dir);
 
 	buffer->access->detach_dmabuf(buffer, priv->block);
 
@@ -1733,7 +1733,7 @@ static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib,
 	priv->dir = buffer->direction == IIO_BUFFER_DIRECTION_IN
 		? DMA_FROM_DEVICE : DMA_TO_DEVICE;
 
-	priv->sgt = dma_buf_map_attachment(attach, priv->dir);
+	priv->sgt = dma_buf_map_sgt(attach, priv->dir);
 	if (IS_ERR(priv->sgt)) {
 		err = PTR_ERR(priv->sgt);
 		dev_err(&indio_dev->dev, "Unable to map attachment: %d\n", err);
@@ -1781,7 +1781,7 @@ static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib,
 	return 0;
 
 err_dmabuf_unmap_attachment:
-	dma_buf_unmap_attachment(attach, priv->sgt, priv->dir);
+	dma_buf_unmap_sgt(attach, priv->sgt, priv->dir);
 err_resv_unlock:
 	dma_resv_unlock(dmabuf->resv);
 err_dmabuf_detach:
drivers/infiniband/core/umem_dmabuf.c +4 -4
index 39b5564a4c35..f233fdac9a17 100644
@@ -29,8 +29,8 @@ int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
 	if (umem_dmabuf->sgt)
 		goto wait_fence;
 
-	sgt = dma_buf_map_attachment(umem_dmabuf->attach,
-				     DMA_BIDIRECTIONAL);
+	sgt = dma_buf_map_sgt(umem_dmabuf->attach,
+			      DMA_BIDIRECTIONAL);
 	if (IS_ERR(sgt))
 		return PTR_ERR(sgt);
 
@@ -106,8 +106,8 @@ void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf)
 		umem_dmabuf->last_sg_trim = 0;
 	}
 
-	dma_buf_unmap_attachment(umem_dmabuf->attach, umem_dmabuf->sgt,
-				 DMA_BIDIRECTIONAL);
+	dma_buf_unmap_sgt(umem_dmabuf->attach, umem_dmabuf->sgt,
+			  DMA_BIDIRECTIONAL);
 
 	umem_dmabuf->sgt = NULL;
 }
drivers/media/common/videobuf2/videobuf2-core.c +1 -1
index b0a6084f1757..61a614074222 100644
@@ -1464,7 +1464,7 @@ static int __prepare_dmabuf(struct vb2_buffer *vb)
 			vb->planes[plane].mem_priv = mem_priv;
 
 			/*
-			 * This pins the buffer(s) with dma_buf_map_attachment()). It's done
+			 * This pins the buffer(s) with dma_buf_map_sgt()). It's done
 			 * here instead just before the DMA, while queueing the buffer(s) so
 			 * userspace knows sooner rather than later if the dma-buf map fails.
 			 */
drivers/media/common/videobuf2/videobuf2-dma-contig.c +4 -4
index 9ce6284cd5f2..284482335c1a 100644
@@ -705,7 +705,7 @@ static int vb2_dc_map_dmabuf(void *mem_priv)
 	}
 
 	/* get the associated scatterlist for this buffer */
-	sgt = dma_buf_map_attachment_unlocked(buf->db_attach, buf->dma_dir);
+	sgt = dma_buf_map_sgt_unlocked(buf->db_attach, buf->dma_dir);
 	if (IS_ERR(sgt)) {
 		pr_err("Error getting dmabuf scatterlist\n");
 		return -EINVAL;
@@ -716,8 +716,8 @@ static int vb2_dc_map_dmabuf(void *mem_priv)
 	if (contig_size < buf->size) {
 		pr_err("contiguous chunk is too small %lu/%lu\n",
 		       contig_size, buf->size);
-		dma_buf_unmap_attachment_unlocked(buf->db_attach, sgt,
-						  buf->dma_dir);
+		dma_buf_unmap_sgt_unlocked(buf->db_attach, sgt,
+					   buf->dma_dir);
 		return -EFAULT;
 	}
 
@@ -748,7 +748,7 @@ static void vb2_dc_unmap_dmabuf(void *mem_priv)
 		dma_buf_vunmap_unlocked(buf->db_attach->dmabuf, &map);
 		buf->vaddr = NULL;
 	}
-	dma_buf_unmap_attachment_unlocked(buf->db_attach, sgt, buf->dma_dir);
+	dma_buf_unmap_sgt_unlocked(buf->db_attach, sgt, buf->dma_dir);
 
 	buf->dma_addr = 0;
 	buf->dma_sgt = NULL;
drivers/media/common/videobuf2/videobuf2-dma-sg.c +2 -2
index b1d0695cda26..54b07e3840ac 100644
@@ -565,7 +565,7 @@ static int vb2_dma_sg_map_dmabuf(void *mem_priv)
 	}
 
 	/* get the associated scatterlist for this buffer */
-	sgt = dma_buf_map_attachment_unlocked(buf->db_attach, buf->dma_dir);
+	sgt = dma_buf_map_sgt_unlocked(buf->db_attach, buf->dma_dir);
 	if (IS_ERR(sgt)) {
 		pr_err("Error getting dmabuf scatterlist\n");
 		return -EINVAL;
@@ -597,7 +597,7 @@ static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
 		dma_buf_vunmap_unlocked(buf->db_attach->dmabuf, &map);
 		buf->vaddr = NULL;
 	}
-	dma_buf_unmap_attachment_unlocked(buf->db_attach, sgt, buf->dma_dir);
+	dma_buf_unmap_sgt_unlocked(buf->db_attach, sgt, buf->dma_dir);
 
 	buf->dma_sgt = NULL;
 }
drivers/media/platform/nvidia/tegra-vde/dmabuf-cache.c +3 -3
index 7752064be783..d9bac7cd855a 100644
@@ -38,7 +38,7 @@ static void tegra_vde_release_entry(struct tegra_vde_cache_entry *entry)
 	if (entry->vde->domain)
 		tegra_vde_iommu_unmap(entry->vde, entry->iova);
 
-	dma_buf_unmap_attachment_unlocked(entry->a, entry->sgt, entry->dma_dir);
+	dma_buf_unmap_sgt_unlocked(entry->a, entry->sgt, entry->dma_dir);
 	dma_buf_detach(dmabuf, entry->a);
 	dma_buf_put(dmabuf);
 
@@ -102,7 +102,7 @@ int tegra_vde_dmabuf_cache_map(struct tegra_vde *vde,
 		goto err_unlock;
 	}
 
-	sgt = dma_buf_map_attachment_unlocked(attachment, dma_dir);
+	sgt = dma_buf_map_sgt_unlocked(attachment, dma_dir);
 	if (IS_ERR(sgt)) {
 		dev_err(dev, "Failed to get dmabufs sg_table\n");
 		err = PTR_ERR(sgt);
@@ -152,7 +152,7 @@ int tegra_vde_dmabuf_cache_map(struct tegra_vde *vde,
 err_free:
 	kfree(entry);
 err_unmap:
-	dma_buf_unmap_attachment_unlocked(attachment, sgt, dma_dir);
+	dma_buf_unmap_sgt_unlocked(attachment, sgt, dma_dir);
 err_detach:
 	dma_buf_detach(dmabuf, attachment);
 err_unlock:
drivers/misc/fastrpc.c +3 -3
index af18ff1992ee..76c8dc05fa10 100644
@@ -401,8 +401,8 @@ static void fastrpc_free_map(struct kref *ref)
 				return;
 			}
 		}
-		dma_buf_unmap_attachment_unlocked(map->attach, map->table,
-						  DMA_BIDIRECTIONAL);
+		dma_buf_unmap_sgt_unlocked(map->attach, map->table,
+					   DMA_BIDIRECTIONAL);
 		dma_buf_detach(map->buf, map->attach);
 		dma_buf_put(map->buf);
 	}
@@ -911,7 +911,7 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
 		goto attach_err;
 	}
 
-	table = dma_buf_map_attachment_unlocked(map->attach, DMA_BIDIRECTIONAL);
+	table = dma_buf_map_sgt_unlocked(map->attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR(table)) {
 		err = PTR_ERR(table);
 		goto map_err;
drivers/nvme/host/pci-dmabuf.c +3 -3
index 161e4ffa6919..fe741dc0d54a 100644
@@ -41,7 +41,7 @@ static struct dma_buf_io_map *nvme_dma_buf_io_map(struct dma_buf_io_ctx *ctx)
 		return ERR_PTR(-ENOMEM);
 	map->nr_entries = nr_entries;
 
-	sgt = dma_buf_map_attachment(attach, ctx->dir);
+	sgt = dma_buf_map_sgt(attach, ctx->dir);
 	if (IS_ERR(sgt)) {
 		ret = PTR_ERR(sgt);
 		sgt = NULL;
@@ -71,7 +71,7 @@ static struct dma_buf_io_map *nvme_dma_buf_io_map(struct dma_buf_io_ctx *ctx)
 	return &map->base;
 err:
 	if (sgt)
-		dma_buf_unmap_attachment(attach, sgt, ctx->dir);
+		dma_buf_unmap_sgt(attach, sgt, ctx->dir);
 	kvfree(map);
 	return ERR_PTR(ret);
 }
@@ -84,7 +84,7 @@ static void nvme_dma_buf_io_unmap(struct dma_buf_io_ctx *ctx,
 
 	dma_resv_assert_held(ctx->dmabuf->resv);
 
-	dma_buf_unmap_attachment(nvme_ctx->attach, map->sgt, ctx->dir);
+	dma_buf_unmap_sgt(nvme_ctx->attach, map->sgt, ctx->dir);
 	kvfree(map);
 }
 
drivers/usb/gadget/function/f_fs.c +2 -2
index c64a268e98a4..2f221d0a983f 100644
@@ -1365,7 +1365,7 @@ static void ffs_dmabuf_release(struct kref *ref)
 	struct dma_buf *dmabuf = attach->dmabuf;
 
 	pr_vdebug("FFS DMABUF release\n");
-	dma_buf_unmap_attachment_unlocked(attach, priv->sgt, priv->dir);
+	dma_buf_unmap_sgt_unlocked(attach, priv->sgt, priv->dir);
 
 	dma_buf_detach(attach->dmabuf, attach);
 	dma_buf_put(dmabuf);
@@ -1566,7 +1566,7 @@ static int ffs_dmabuf_attach(struct file *file, int fd)
 	if (err)
 		goto err_free_priv;
 
-	sg_table = dma_buf_map_attachment(attach, dir);
+	sg_table = dma_buf_map_sgt(attach, dir);
 	dma_resv_unlock(dmabuf->resv);
 
 	if (IS_ERR(sg_table)) {
drivers/xen/gntdev-dmabuf.c +4 -4
index df1883aca850..e07e34515421 100644
@@ -587,7 +587,7 @@ dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev,
 
 	gntdev_dmabuf->u.imp.attach = attach;
 
-	sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
+	sgt = dma_buf_map_sgt_unlocked(attach, DMA_BIDIRECTIONAL);
 	if (IS_ERR(sgt)) {
 		ret = ERR_CAST(sgt);
 		goto fail_detach;
@@ -650,7 +650,7 @@ dmabuf_imp_to_refs(struct gntdev_dmabuf_priv *priv, struct device *dev,
 fail_end_access:
 	dmabuf_imp_end_foreign_access(gntdev_dmabuf->u.imp.refs, count);
 fail_unmap:
-	dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
+	dma_buf_unmap_sgt_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
 fail_detach:
 	dma_buf_detach(dma_buf, attach);
 fail_free_obj:
@@ -700,8 +700,8 @@ static int dmabuf_imp_release(struct gntdev_dmabuf_priv *priv, u32 fd)
 	attach = gntdev_dmabuf->u.imp.attach;
 
 	if (gntdev_dmabuf->u.imp.sgt)
-		dma_buf_unmap_attachment_unlocked(attach, gntdev_dmabuf->u.imp.sgt,
-						  DMA_BIDIRECTIONAL);
+		dma_buf_unmap_sgt_unlocked(attach, gntdev_dmabuf->u.imp.sgt,
+					   DMA_BIDIRECTIONAL);
 	dma_buf = attach->dmabuf;
 	dma_buf_detach(attach->dmabuf, attach);
 	dma_buf_put(dma_buf);
include/linux/dma-buf.h +13 -13
index d15b2b31d3c9..cedeb8ebc99e 100644
@@ -116,7 +116,7 @@ struct dma_buf_ops {
 	/**
 	 * @map_dma_buf:
 	 *
-	 * This is called by dma_buf_map_attachment() and is used to map a
+	 * This is called by dma_buf_map_sgt() and is used to map a
 	 * shared &dma_buf into device address space, and it is mandatory. It
 	 * can only be called if @attach has been called successfully.
 	 *
@@ -173,7 +173,7 @@ struct dma_buf_ops {
 	/**
 	 * @unmap_dma_buf:
 	 *
-	 * This is called by dma_buf_unmap_attachment() and should unmap and
+	 * This is called by dma_buf_unmap_sgt() and should unmap and
 	 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
 	 * For static dma_buf handling this might also unpin the backing
 	 * storage if this is the last mapping of the DMA buffer.
@@ -470,7 +470,7 @@ struct dma_buf_attach_ops {
  * @peer2peer: true if the importer can handle peer resources without pages.
  * @priv: exporter specific attachment data.
  * @importer_ops: importer operations for this attachment, if provided
- * dma_buf_map/unmap_attachment() must be called with the dma_resv lock held.
+ * dma_buf_map/unmap_sgt() must be called with the dma_resv lock held.
  * @importer_priv: importer specific attachment data.
  *
  * This structure holds the attachment information between the dma_buf buffer
@@ -479,8 +479,8 @@ struct dma_buf_attach_ops {
  *
  * An attachment is created by calling dma_buf_attach(), and released again by
  * calling dma_buf_detach(). The DMA mapping itself needed to initiate a
- * transfer is created by dma_buf_map_attachment() and freed again by calling
- * dma_buf_unmap_attachment().
+ * transfer is created by dma_buf_map_sgt() and freed again by calling
+ * dma_buf_unmap_sgt().
  */
 struct dma_buf_attachment {
 	struct dma_buf *dmabuf;
@@ -571,10 +571,10 @@ void dma_buf_fd_install(struct dma_buf *dmabuf, int fd);
 struct dma_buf *dma_buf_get(int fd);
 void dma_buf_put(struct dma_buf *dmabuf);
 
-struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
-					enum dma_data_direction);
-void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
-				enum dma_data_direction);
+struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *,
+				 enum dma_data_direction);
+void dma_buf_unmap_sgt(struct dma_buf_attachment *, struct sg_table *,
+		       enum dma_data_direction);
 void dma_buf_invalidate_mappings(struct dma_buf *dma_buf);
 bool dma_buf_attach_revocable(struct dma_buf_attachment *attach);
 int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
@@ -582,11 +582,11 @@ int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
 int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
 			   enum dma_data_direction dir);
 struct sg_table *
-dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach,
+dma_buf_map_sgt_unlocked(struct dma_buf_attachment *attach,
+			 enum dma_data_direction direction);
+void dma_buf_unmap_sgt_unlocked(struct dma_buf_attachment *attach,
+				struct sg_table *sg_table,
 				enum dma_data_direction direction);
-void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach,
-				       struct sg_table *sg_table,
-				       enum dma_data_direction direction);
 
 int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
 		 unsigned long);
io_uring/zcrx.c +3 -3
index 6d7abb1636e7..7c8ce3d2188a 100644
@@ -132,8 +132,8 @@ static void io_unmap_dmabuf(struct io_zcrx_mem *mem)
 	if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER))
 		return;
 	if (mem->sgt)
-		dma_buf_unmap_attachment_unlocked(mem->attach, mem->sgt,
-						  DMA_FROM_DEVICE);
+		dma_buf_unmap_sgt_unlocked(mem->attach, mem->sgt,
+					   DMA_FROM_DEVICE);
 	if (mem->attach)
 		dma_buf_detach(mem->dmabuf, mem->attach);
 	mem->sgt = NULL;
@@ -182,7 +182,7 @@ static int io_import_dmabuf(struct io_zcrx_ifq *ifq,
 		goto err;
 	}
 
-	mem->sgt = dma_buf_map_attachment_unlocked(mem->attach, DMA_FROM_DEVICE);
+	mem->sgt = dma_buf_map_sgt_unlocked(mem->attach, DMA_FROM_DEVICE);
 	if (IS_ERR(mem->sgt)) {
 		ret = PTR_ERR(mem->sgt);
 		mem->sgt = NULL;
net/core/devmem.c +4 -4
index 32fafbfa1359..5a984df959f7 100644
@@ -75,8 +75,8 @@ void __net_devmem_dmabuf_binding_free(struct work_struct *wq)
 
 	kvfree(binding->area.niovs);
 	dma_resv_lock(binding->dmabuf->resv, NULL);
-	dma_buf_unmap_attachment(binding->attachment, binding->sgt,
-				 binding->direction);
+	dma_buf_unmap_sgt(binding->attachment, binding->sgt,
+			  binding->direction);
 	dma_buf_unpin(binding->attachment);
 	dma_resv_unlock(binding->dmabuf->resv);
 	dma_buf_detach(binding->dmabuf, binding->attachment);
@@ -250,7 +250,7 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
 		goto err_unlock;
 	}
 
-	binding->sgt = dma_buf_map_attachment(binding->attachment, direction);
+	binding->sgt = dma_buf_map_sgt(binding->attachment, direction);
 	if (IS_ERR(binding->sgt)) {
 		err = PTR_ERR(binding->sgt);
 		NL_SET_ERR_MSG(extack, "Failed to map dmabuf attachment");
@@ -357,7 +357,7 @@ net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
 	kvfree(binding->tx_vec);
 err_unmap:
 	dma_resv_lock(dmabuf->resv, NULL);
-	dma_buf_unmap_attachment(binding->attachment, binding->sgt, direction);
+	dma_buf_unmap_sgt(binding->attachment, binding->sgt, direction);
 err_unpin:
 	dma_buf_unpin(binding->attachment);
 err_unlock:
Signature · 1 line
2.54.0
#2 lore raw

PATCH v3 2/24 dma-buf: Introduce DMA-buf mapping types

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

DMA-buf mapping types allow the importer and exporter to negotiate the format of the map/unmap to be used during the attachment.

Currently DMA-buf only supports struct scatterlist as the attachment map operation. This is not sufficient for all use cases as dma_addr_t is a very specific and limited type.

With mapping types the importing driver can declare what it supports. For example:

	static const struct dma_buf_mapping_match imp_match[] = {
		DMA_BUF_IMAPPING_MY_DRIVER(...),
		DMA_BUF_IMAPPING_SGT(),
	};
	static const struct dma_buf_attach_ops importer_ops = {
		.supported_mapping_types = imp_match,
		.nr_supported_mapping_types = ARRAY_SIZE(imp_match),
		...
	};
	attach = dma_buf_dynamic_attach(dmabuf, dev, &importer_ops, ...)

And the exporting driver can declare what it supports:

int exporter_match_mapping(struct dma_buf_match_args *args)

{
	struct dma_buf_mapping_match exp_match[] = {
		DMA_BUF_EMAPPING_MY_DRIVER(my_ops, dev, ...),
		DMA_BUF_EMAPPING_SGT(sgt_ops),
	};
	return dma_buf_match_mapping(args, exp_match, ...);
}

During dma_buf_dynamic_attach() the core code records every mutually acceptable (exporter entry, importer entry) pair in attach->matches, a small fixed-size array in the attachment, in the exporter's order of preference. The first entry is the default and is what dma_buf_attach_map_type() returns; later patches let the exporter choose a different entry of the set
at pin or map time, when the buffer's placement and the other attachments are known. Importers and exporters that list a single type see exactly one entry.

Add the basic types:

struct dma_buf_mapping_type
Type tag and ops for each mapping type.

struct dma_buf_mapping_match
Entry in a list of importer or exporter match specifications. The match specification can be extended by the mapping type with unique data.

dma_buf_match_mapping() / struct dma_buf_match_args
Helper to do the matching. Called by the exporting driver via a dma_buf_ops callback.

struct dma_buf_mapping_exp_ops
Base type for the per-mapping type exporter provided functions. This would be the map/unmap callbacks. Each mapping type can provide its own functions for map/unmap type operations with optimal type signatures.

---
 drivers/dma-buf/dma-buf-mapping.c | 65 +++++++++++++++++++++++++++
 include/linux/dma-buf-mapping.h   | 95 +++++++++++++++++++++++++++++++++++++++
 include/linux/dma-buf.h           | 29 ++++++++++++
 3 files changed, 189 insertions(+)
drivers/dma-buf/dma-buf-mapping.c +65 -0
index 833be519e1e6..d27f02610c2a 100644
@@ -5,6 +5,7 @@
  */
 #include <linux/dma-buf-mapping.h>
 #include <linux/dma-resv.h>
+#include <linux/dma-buf.h>
 #include <linux/overflow.h>
 #include <linux/align.h>
 
@@ -257,3 +258,67 @@ void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
 
 }
 EXPORT_SYMBOL_NS_GPL(dma_buf_free_sgt, "DMA_BUF");
+
+/**
+ * dma_buf_match_mapping - Record the mapping types agreed upon by exporter
+ *                         and importer
+ * @args: Match arguments from attach
+ * @exp: Array of mapping types supported by the exporter, in priority order
+ * @exp_len: Number of entries in @exp
+ *
+ * Walk the exporter's list in order and, for each entry, find the first
+ * importer entry of the same type that the type's match() callback accepts.
+ * Every such pair is recorded in @args->attach->matches, so the match set
+ * preserves the exporter's priority. The first entry is the default returned
+ * by dma_buf_attach_map_type().
+ *
+ * Because the exporter list is walked in order, the exporter controls the
+ * priority of mapping types.
+ *
+ * Returns: 0 if at least one type matched, -EOPNOTSUPP if the exporter and
+ * importer have no type in common, -EINVAL if either list is empty, -E2BIG if
+ * more than DMA_BUF_MAX_MATCHES types matched, or the error a match() callback
+ * aborted the search with.
+ */
+int dma_buf_match_mapping(struct dma_buf_match_args *args,
+			  const struct dma_buf_mapping_match *exp,
+			  size_t exp_len)
+{
+	const struct dma_buf_mapping_match *exp_end = exp + exp_len;
+	const struct dma_buf_mapping_match *imp_end =
+		args->imp_matches + args->imp_len;
+	struct dma_buf_attachment *attach = args->attach;
+	unsigned int nr = 0;
+	int ret;
+
+	if (!exp_len || !args->imp_len)
+		return -EINVAL;
+
+	for (; exp != exp_end; exp++) {
+		const struct dma_buf_mapping_match *imp = args->imp_matches;
+
+		for (; imp != imp_end; imp++) {
+			if (exp->type != imp->type)
+				continue;
+			if (exp->type->match) {
+				ret = exp->type->match(args, exp, imp);
+				if (ret == -EOPNOTSUPP)
+					continue;
+				if (ret != 0)
+					return ret;
+			}
+			if (nr == ARRAY_SIZE(attach->matches))
+				return -E2BIG;
+			exp->type->finish_match(args, exp, imp,
+						&attach->matches[nr++]);
+			break;
+		}
+	}
+
+	if (!nr)
+		return -EOPNOTSUPP;
+
+	attach->nr_matches = nr;
+	return 0;
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_match_mapping, "DMA_BUF");
include/linux/dma-buf-mapping.h +95 -0

105 lines not on this page · raw message

include/linux/dma-buf.h +29 -0
index cedeb8ebc99e..81bbcd420d60 100644
@@ -27,6 +27,28 @@
 struct device;
 struct dma_buf;
 struct dma_buf_attachment;
+struct dma_buf_mapping_type;
+struct dma_buf_mapping_exp_ops;
+
+/*
+ * Upper bound on the number of mapping types an attachment can record as
+ * mutually acceptable. An exporter offering more compatible types than this
+ * to one importer fails the attach with -E2BIG.
+ */
+#define DMA_BUF_MAX_MATCHES 4
+
+/*
+ * Match items are generated by the importer using the DMA_BUF_IMAPPING_*() and
+ * the exporter using the DMA_BUF_EMAPPING_*() functions. Each mapping type
+ * defines its own signature with its own data to make the match and attachment.
+ */
+struct dma_buf_mapping_match {
+	const struct dma_buf_mapping_type *type;
+	const struct dma_buf_mapping_exp_ops *exp_ops;
+	union {
+		/* Each mapping_type has unique match parameters here */
+	};
+};
 
 /**
  * struct dma_buf_ops - operations possible on struct dma_buf
@@ -472,6 +494,11 @@ struct dma_buf_attach_ops {
  * @importer_ops: importer operations for this attachment, if provided
  * dma_buf_map/unmap_sgt() must be called with the dma_resv lock held.
  * @importer_priv: importer specific attachment data.
+ * @matches: The match set negotiated at attach time: every mapping type both
+ *           sides accept, in the exporter's order of preference. The first
+ *           entry is the exporter's default; dma_buf_attach_map_type()
+ *           returns the entry the attachment is mapped with.
+ * @nr_matches: Number of valid entries in @matches.
  *
  * This structure holds the attachment information between the dma_buf buffer
  * and its user device(s). The list contains one attachment struct per device
@@ -490,6 +517,8 @@ struct dma_buf_attachment {
 	const struct dma_buf_attach_ops *importer_ops;
 	void *importer_priv;
 	void *priv;
+	struct dma_buf_mapping_match matches[DMA_BUF_MAX_MATCHES];
+	unsigned int nr_matches;
 };
 
 /**
Signature · 1 line
2.54.0
#3 lore raw

PATCH v3 3/24 dma-buf: Add the SGT DMA mapping type

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

The SGT (Scatter Gather Table) DMA mapping type represents the existing sg_table/scatterlist based DMA mapping. For now it continues to use a sg_table for map/unmap, but this will allow a future patch series to convert to using a different data structure for importers without having to convert all at once.

dma_buf_sgt_compat_match_mapping() is used during the conversion of exporters to transparently wrap an unaware exporter's sg_table. It is removed later in this series once all exporters have been converted.

A mapping is an object allocated by an exporter which embeds the generic struct dma_buf_mapping type header which core dma-buf code fills in. For the SGT type that is currently a struct dma_buf_sgt_mapping embedding a struct sg_table.

Importers continue to receive a struct sg_table from dma_buf_map_sgt() and are unaffected. A future series will convert the embedded sg_table into a list of DMA addresses more appropriate for importers.

---

Changes for v3:

- PCIe P2P will be added as its own type in future, so dropped the P2P flags from the SGT type.
- Exporters allocate and return struct dma_buf_sgt_mapping with the sg_table embedded instead of a bare struct sg_table. This ensures an unmap happens against the correct type once multiple mapping types are possible.

---
 drivers/dma-buf/dma-buf-mapping.c | 104 +++++++++++++++++++++++++++++
 include/linux/dma-buf-mapping.h   | 135 ++++++++++++++++++++++++++++++++++++++
 include/linux/dma-buf.h           |   6 +-
 3 files changed, 244 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-buf-mapping.c +104 -0
index d27f02610c2a..cc3d8d6fe009 100644
@@ -6,6 +6,7 @@
 #include <linux/dma-buf-mapping.h>
 #include <linux/dma-resv.h>
 #include <linux/dma-buf.h>
+#include <linux/seq_file.h>
 #include <linux/overflow.h>
 #include <linux/align.h>
 
@@ -322,3 +323,106 @@ int dma_buf_match_mapping(struct dma_buf_match_args *args,
 	return 0;
 }
 EXPORT_SYMBOL_NS_GPL(dma_buf_match_mapping, "DMA_BUF");
+
+static void
+dma_buf_sgt_finish_match(struct dma_buf_match_args *args,
+			 const struct dma_buf_mapping_match *exp,
+			 const struct dma_buf_mapping_match *imp,
+			 struct dma_buf_mapping_match *match)
+{
+	struct dma_buf_attachment *attach = args->attach;
+
+	*match = (struct dma_buf_mapping_match) {
+		.type = &dma_buf_mapping_sgt_type,
+		.exp_ops = exp->exp_ops,
+		.sgt_data = {
+			.importing_dma_device = args->dev,
+		},
+	};
+
+	/*
+	 * Setup the SGT type variables stored in attach because importers and
+	 * exporters that do not natively use mappings expect them to be there.
+	 * When converting to use mappings users should use the match versions
+	 * of these instead.
+	 */
+	attach->dev = args->dev;
+}
+
+static void dma_buf_sgt_debugfs_dump(struct seq_file *s,
+				     struct dma_buf_attachment *attach)
+{
+	seq_printf(s, " %s", dev_name(dma_buf_sgt_dma_device(attach)));
+}
+
+const struct dma_buf_mapping_type dma_buf_mapping_sgt_type = {
+	.name = "DMA Mapped Scatter Gather Table",
+	.finish_match = dma_buf_sgt_finish_match,
+	.debugfs_dump = dma_buf_sgt_debugfs_dump,
+};
+EXPORT_SYMBOL_NS_GPL(dma_buf_mapping_sgt_type, "DMA_BUF");
+
+/* An unaware exporter's sg_table presented as an SGT mapping */
+struct dma_buf_sgt_compat_mapping {
+	struct dma_buf_sgt_mapping mapping;
+	struct sg_table *sgt;
+};
+
+static struct dma_buf_sgt_mapping *
+dma_buf_sgt_compat_map_dma_buf(struct dma_buf_attachment *attach,
+			       enum dma_data_direction dir)
+{
+	struct dma_buf_sgt_compat_mapping *compat;
+	struct sg_table *sgt;
+
+	sgt = attach->dmabuf->ops->map_dma_buf(attach, dir);
+	if (!sgt)
+		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(sgt))
+		return ERR_CAST(sgt);
+
+	compat = kzalloc_obj(*compat);
+	if (!compat) {
+		attach->dmabuf->ops->unmap_dma_buf(attach, sgt, dir);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	/* The exporter keeps its table; the mapping shares its entries */
+	compat->sgt = sgt;
+	compat->mapping.sgt = *sgt;
+	return &compat->mapping;
+}
+
+static void dma_buf_sgt_compat_unmap_dma_buf(struct dma_buf_attachment *attach,
+					     struct dma_buf_sgt_mapping *mapping,
+					     enum dma_data_direction dir)
+{
+	struct dma_buf_sgt_compat_mapping *compat =
+		container_of(mapping, struct dma_buf_sgt_compat_mapping, mapping);
+
+	attach->dmabuf->ops->unmap_dma_buf(attach, compat->sgt, dir);
+	kfree(compat);
+}
+
+/* Route the classic map/unmap ops through the exp ops for old importers */
+static const struct dma_buf_mapping_sgt_exp_ops dma_buf_sgt_compat_exp_ops = {
+	.map_dma_buf = dma_buf_sgt_compat_map_dma_buf,
+	.unmap_dma_buf = dma_buf_sgt_compat_unmap_dma_buf,
+};
+
+/*
+ * This entry is used for unaware exporters that do not support
+ * match_mapping(). It wraps the dma_buf ops for SGT mappings into a mapping
+ * type so aware importers can transparently work with unaware exporters. This
+ * does not require p2p because old exporters will check it through the
+ * attach->peer2peer mechanism.
+ */
+static const struct dma_buf_mapping_match dma_buf_sgt_exp_compat_matches[] = {
+	DMA_BUF_EMAPPING_SGT(&dma_buf_sgt_compat_exp_ops),
+};
+
+int dma_buf_sgt_compat_match_mapping(struct dma_buf_match_args *args)
+{
+	return dma_buf_match_mapping(args, dma_buf_sgt_exp_compat_matches,
+				     ARRAY_SIZE(dma_buf_sgt_exp_compat_matches));
+}
include/linux/dma-buf-mapping.h +135 -0

145 lines not on this page · raw message

include/linux/dma-buf.h +5 -1
index 81bbcd420d60..6b423e22174a 100644
@@ -46,7 +46,11 @@ struct dma_buf_mapping_match {
 	const struct dma_buf_mapping_type *type;
 	const struct dma_buf_mapping_exp_ops *exp_ops;
 	union {
-		/* Each mapping_type has unique match parameters here */
+		/* For dma_buf_mapping_sgt_type */
+		struct {
+			/* Filled from the attaching device, not by importers */
+			struct device *importing_dma_device;
+		} sgt_data;
 	};
 };
 
Signature · 1 line
2.54.0
#4 lore raw

PATCH v3 4/24 dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach()

In reply to Alistair Popple

Mapping types need to be negotiated based on what importer and exporter can accept.

Add an optional dma_buf_attach_ops->supported_mapping_types table that lists types supported by an importer in most preferred order. The entries carry no device so the table can be static and shared by all of the importer's attachments.

Instead the device passed to dma_buf_dynamic_attach() is passed to the mapping types through the match callback arguments. Importers that do not provide a table continue to support the SGT type.

dma_buf_dynamic_attach() checks if the exporter is mapping aware and calls its ops->match_mapping() function to get the exporter match list and call dma_buf_match_mapping(), otherwise it falls back to using the SGT compatibility type. The resulting set of possible matches is then stored in attach->matches the exporter's priority order.

The actual match which will be used is deferred until map time as future attachments could change what's possible. Exporters that need to behave differently based on the importer's capability can declare two exporter entries with different map/unmap functions. This will be used in a future series to negotiate PCIe P2P mappings.

---

Changes for v3:

- Move P2P negotiation to a later series.

---
 drivers/dma-buf/dma-buf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++---
 include/linux/dma-buf.h   | 40 +++++++++++++++++++++++++++++++++++
 2 files changed, 91 insertions(+), 3 deletions(-)
drivers/dma-buf/dma-buf.c +51 -3
index 0dd8d0999a92..4aee479c4a7e 100644
@@ -14,6 +14,7 @@
 #include <linux/fs.h>
 #include <linux/slab.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-fence.h>
 #include <linux/dma-fence-unwrap.h>
 #include <linux/anon_inodes.h>
@@ -714,11 +715,19 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 	int ret;
 
 	if (WARN_ON(!exp_info->priv || !exp_info->ops
-		    || !exp_info->ops->map_dma_buf
-		    || !exp_info->ops->unmap_dma_buf
 		    || !exp_info->ops->release))
 		return ERR_PTR(-EINVAL);
 
+	if (exp_info->ops->match_mapping) {
+		if (WARN_ON(exp_info->ops->map_dma_buf ||
+			    exp_info->ops->unmap_dma_buf))
+			return ERR_PTR(-EINVAL);
+	} else {
+		if (WARN_ON(!exp_info->ops->map_dma_buf ||
+			    !exp_info->ops->unmap_dma_buf))
+			return ERR_PTR(-EINVAL);
+	}
+
 	if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
 		return ERR_PTR(-EINVAL);
 
@@ -1027,12 +1036,24 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
  * Note that this can fail if the backing storage of @dmabuf is in a place not
  * accessible to @dev, and cannot be moved to a more suitable place. This is
  * indicated with the error code -EBUSY.
+ *
+ * The mapping types the importer supports come from
+ * &dma_buf_attach_ops.supported_mapping_types. Importers without it only
+ * support the SGT type. This records the types supported by both importer and
+ * exporter in &dma_buf_attachment.matches. Later attachments do not change
+ * the set but may affect what type the exporter selects when the attachment
+ * is mapped or pinned.
  */
 struct dma_buf_attachment *
 dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
 		       const struct dma_buf_attach_ops *importer_ops,
 		       void *importer_priv)
 {
+	struct dma_buf_mapping_match default_matches[1];
+	struct dma_buf_match_args match_args = {
+		.dmabuf = dmabuf,
+		.dev = dev,
+	};
 	struct dma_buf_attachment *attach;
 	int ret;
 
@@ -1043,13 +1064,40 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
 	if (!attach)
 		return ERR_PTR(-ENOMEM);
 
-	attach->dev = dev;
 	attach->dmabuf = dmabuf;
 	if (importer_ops)
 		attach->peer2peer = importer_ops->allow_peer2peer;
 	attach->importer_ops = importer_ops;
 	attach->importer_priv = importer_priv;
 
+	/*
+	 * The exporter's match_mapping() may look at the importer side of the
+	 * attachment, so initialise it before the match.
+	 */
+	match_args.attach = attach;
+	if (importer_ops && importer_ops->supported_mapping_types) {
+		if (WARN_ON(!importer_ops->nr_supported_mapping_types)) {
+			ret = -EINVAL;
+			goto err_attach;
+		}
+		match_args.imp_matches = importer_ops->supported_mapping_types;
+		match_args.imp_len = importer_ops->nr_supported_mapping_types;
+	} else {
+		default_matches[0] = DMA_BUF_IMAPPING_SGT();
+		match_args.imp_matches = default_matches;
+		match_args.imp_len = ARRAY_SIZE(default_matches);
+	}
+
+	if (dmabuf->ops->match_mapping) {
+		ret = dmabuf->ops->match_mapping(&match_args);
+		if (ret)
+			goto err_attach;
+	} else {
+		ret = dma_buf_sgt_compat_match_mapping(&match_args);
+		if (ret)
+			goto err_attach;
+	}
+
 	if (dmabuf->ops->attach) {
 		ret = dmabuf->ops->attach(dmabuf, attach);
 		if (ret)
include/linux/dma-buf.h +40 -0
index 6b423e22174a..cfbb800aa12d 100644
@@ -29,6 +29,7 @@ struct dma_buf;
 struct dma_buf_attachment;
 struct dma_buf_mapping_type;
 struct dma_buf_mapping_exp_ops;
+struct dma_buf_match_args;
 
 /*
  * Upper bound on the number of mapping types an attachment can record as
@@ -303,6 +304,24 @@ struct dma_buf_ops {
 
 	int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map);
 	void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map);
+
+	/**
+	 * @match_mapping:
+	 *
+	 * Called during attach without the dma_resv lock held. Allows the exporter to
+	 * build its own struct dma_buf_mapping_match[] and call dma_buf_match_mapping()
+	 * to record mutually acceptable types in &dma_buf_attachment.matches in exporter
+	 * order of preference.
+	 *
+	 * The match set is fixed for the lifetime of the attachment and so every
+	 * input determining match compatibility must hold for the lifetime of the
+	 * attachment.
+	 *
+	 * It must not depend on the set of attachments or on current buffer
+	 * placement as matches are static and buffer placement can change at
+	 * any time until the final location is resolved during map/pin.
+	 */
+	int (*match_mapping)(struct dma_buf_match_args *args);
 };
 
 /**
@@ -486,6 +505,27 @@ struct dma_buf_attach_ops {
 	 * required behavior.
 	 */
 	void (*invalidate_mappings)(struct dma_buf_attachment *attach);
+
+	/**
+	 * @supported_mapping_types: [optional] mapping types the importer
+	 * can use
+	 *
+	 * Array of @nr_supported_mapping_types DMA_BUF_IMAPPING_*() entries
+	 * the importer can use in priority order. Entries are static so can be
+	 * shared by all of the importer's attachments. Any required dynamic
+	 * information, such as the importing device, is passed to each type's
+	 * callbacks by dma_buf_dynamic_attach() for recording in the
+	 * negotiated entry.
+	 *
+	 * If not provided the importer only supports the SGT mapping type.
+	 */
+	const struct dma_buf_mapping_match *supported_mapping_types;
+
+	/**
+	 * @nr_supported_mapping_types: number of entries in
+	 * @supported_mapping_types
+	 */
+	unsigned int nr_supported_mapping_types;
 };
 
 /**
Signature · 1 line
2.54.0
#5 lore raw

PATCH v3 5/24 dma-buf: Route SGT related actions through the attachment's mapping type

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

Now that dma_buf_dynamic_attach() ensures a mapping type exists, even for exporters and importers that don't provide it, route operations through dma_buf_attach_map_type().

For map/unmap this will go through dma_buf_sgt_compat_map_dma_buf() which calls the same attach->dmabuf->ops->map_dma_buf().

The exporter ops now return a struct dma_buf_sgt_mapping, so dma_buf_map_sgt() fills in its header and hands the importer the embedded sg_table, or under CONFIG_DMABUF_DEBUG the page-less copy, which now points back at the mapping. dma_buf_unmap_sgt() finds the mapping behind the importer's table again and passes it to the exporter.

Move the debugfs processing unique to SGT into a callback too.

---
 drivers/dma-buf/dma-buf.c | 84 +++++++++++++++++++++++++++++++----------------
 1 file changed, 55 insertions(+), 29 deletions(-)
drivers/dma-buf/dma-buf.c +55 -29
index 4aee479c4a7e..76340d067f6c 100644
@@ -56,7 +56,7 @@
 
 /* Wrapper to hide the sg_table page link from the importer */
 struct dma_buf_sg_table_wrapper {
-	struct sg_table *original;
+	struct dma_buf_sgt_mapping *mapping;
 	struct sg_table wrapper;
 };
 
@@ -883,15 +883,16 @@ void dma_buf_put(struct dma_buf *dmabuf)
 }
 EXPORT_SYMBOL_NS_GPL(dma_buf_put, "DMA_BUF");
 
-static int dma_buf_wrap_sg_table(struct sg_table **sg_table)
+static struct sg_table *
+dma_buf_wrap_sg_table(struct dma_buf_sgt_mapping *mapping)
 {
+	struct sg_table *from = &mapping->sgt;
 	struct scatterlist *to_sg, *from_sg;
-	struct sg_table *from = *sg_table;
 	struct dma_buf_sg_table_wrapper *to;
 	int i, ret;
 
 	if (!IS_ENABLED(CONFIG_DMABUF_DEBUG))
-		return 0;
+		return from;
 
 	/*
 	 * To catch abuse of the underlying struct page by importers copy the
@@ -900,11 +901,13 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table)
 	 */
 	to = kzalloc_obj(*to);
 	if (!to)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	ret = sg_alloc_table(&to->wrapper, from->nents, GFP_KERNEL);
-	if (ret)
-		goto free_to;
+	if (ret) {
+		kfree(to);
+		return ERR_PTR(ret);
+	}
 
 	to_sg = to->wrapper.sgl;
 	for_each_sgtable_dma_sg(from, from_sg, i) {
@@ -916,26 +919,24 @@ static int dma_buf_wrap_sg_table(struct sg_table **sg_table)
 		to_sg = sg_next(to_sg);
 	}
 
-	to->original = from;
-	*sg_table = &to->wrapper;
-	return 0;
-
-free_to:
-	kfree(to);
-	return ret;
+	to->mapping = mapping;
+	return &to->wrapper;
 }
 
-static void dma_buf_unwrap_sg_table(struct sg_table **sg_table)
+static struct dma_buf_sgt_mapping *
+dma_buf_unwrap_sg_table(struct sg_table *sg_table)
 {
+	struct dma_buf_sgt_mapping *mapping;
 	struct dma_buf_sg_table_wrapper *copy;
 
 	if (!IS_ENABLED(CONFIG_DMABUF_DEBUG))
-		return;
+		return container_of(sg_table, struct dma_buf_sgt_mapping, sgt);
 
-	copy = container_of(*sg_table, typeof(*copy), wrapper);
-	*sg_table = copy->original;
+	copy = container_of(sg_table, typeof(*copy), wrapper);
+	mapping = copy->mapping;
 	sg_free_table(&copy->wrapper);
 	kfree(copy);
+	return mapping;
 }
 
 static inline bool
@@ -1236,6 +1237,8 @@ EXPORT_SYMBOL_NS_GPL(dma_buf_unpin, "DMA_BUF");
 struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
 				 enum dma_data_direction direction)
 {
+	const struct dma_buf_mapping_sgt_exp_ops *sgt_exp_ops;
+	struct dma_buf_sgt_mapping *mapping;
 	struct sg_table *sg_table;
 	signed long ret;
 
@@ -1244,6 +1247,10 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
 	if (WARN_ON(!attach || !attach->dmabuf))
 		return ERR_PTR(-EINVAL);
 
+	sgt_exp_ops = dma_buf_get_sgt_ops(attach);
+	if (WARN_ON(!sgt_exp_ops))
+		return ERR_PTR(-EINVAL);
+
 	dma_resv_assert_held(attach->dmabuf->resv);
 
 	if (dma_buf_pin_on_map(attach)) {
@@ -1257,11 +1264,14 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
 			return ERR_PTR(ret);
 	}
 
-	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
-	if (!sg_table)
-		sg_table = ERR_PTR(-ENOMEM);
-	if (IS_ERR(sg_table))
+	mapping = sgt_exp_ops->map_dma_buf(attach, direction);
+	if (!mapping)
+		mapping = ERR_PTR(-ENOMEM);
+	if (IS_ERR(mapping)) {
+		sg_table = ERR_CAST(mapping);
 		goto error_unpin;
+	}
+	mapping->base.match = dma_buf_attach_map_type(attach);
 
 	/*
 	 * Importers with static attachments don't wait for fences.
@@ -1273,9 +1283,11 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
 		if (ret < 0)
 			goto error_unmap;
 	}
-	ret = dma_buf_wrap_sg_table(&sg_table);
-	if (ret)
+	sg_table = dma_buf_wrap_sg_table(mapping);
+	if (IS_ERR(sg_table)) {
+		ret = PTR_ERR(sg_table);
 		goto error_unmap;
+	}
 
 	if (IS_ENABLED(CONFIG_DMA_API_DEBUG)) {
 		struct scatterlist *sg;
@@ -1283,7 +1295,7 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
 		int len;
 		int i;
 
-		for_each_sgtable_dma_sg(sg_table, sg, i) {
+		for_each_sgtable_dma_sg(&mapping->sgt, sg, i) {
 			addr = sg_dma_address(sg);
 			len = sg_dma_len(sg);
 			if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(len)) {
@@ -1296,7 +1308,7 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
 	return sg_table;
 
 error_unmap:
-	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+	sgt_exp_ops->unmap_dma_buf(attach, mapping, direction);
 	sg_table = ERR_PTR(ret);
 
 error_unpin:
@@ -1349,6 +1361,9 @@ void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
 				struct sg_table *sg_table,
 				enum dma_data_direction direction)
 {
+	const struct dma_buf_mapping_sgt_exp_ops *sgt_exp_ops;
+	struct dma_buf_sgt_mapping *mapping;
+
 	might_sleep();
 
 	if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
@@ -1356,8 +1371,13 @@ void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
 
 	dma_resv_assert_held(attach->dmabuf->resv);
 
-	dma_buf_unwrap_sg_table(&sg_table);
-	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+	mapping = dma_buf_unwrap_sg_table(sg_table);
+	if (WARN_ON(dma_buf_mapping_type_of(&mapping->base) !=
+		    &dma_buf_mapping_sgt_type))
+		return;
+	sgt_exp_ops = container_of(mapping->base.match->exp_ops,
+				   struct dma_buf_mapping_sgt_exp_ops, ops);
+	sgt_exp_ops->unmap_dma_buf(attach, mapping, direction);
 
 	if (dma_buf_pin_on_map(attach))
 		attach->dmabuf->ops->unpin(attach);
@@ -1838,7 +1858,13 @@ static int dma_buf_debug_show(struct seq_file *s, void *unused)
 		attach_count = 0;
 
 		list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
-			seq_printf(s, "\t%s\n", dev_name(attach_obj->dev));
+			const struct dma_buf_mapping_type *type =
+				dma_buf_attach_map_type(attach_obj)->type;
+
+			seq_printf(s, "\t%s:", type->name);
+			if (type->debugfs_dump)
+				type->debugfs_dump(s, attach_obj);
+			seq_putc(s, '\n');
 			attach_count++;
 		}
 		dma_resv_unlock(buf_obj->resv);
Signature · 1 line
2.54.0
#6 lore raw

PATCH v3 6/24 dma-buf: Allow single exporter drivers to avoid the match_mapping function

In reply to Alistair Popple

Introduce single_exporter_match into the dma_buf_ops so that single exporter drivers can simply set it using a static initializer to use the mapping type APIs. Provide a helper macro DMA_BUF_SIMPLE_SGT_EXP_MATCH() that generates the initializer for simple drivers.

More complex exporters, such as those offering more than one entry or that need to look at the importer when matching, implement the match_mapping callback instead.

---
 drivers/dma-buf/dma-buf.c       | 14 ++++++++++++--
 include/linux/dma-buf-mapping.h | 13 +++++++++++++
 include/linux/dma-buf.h         |  9 +++++++++
 3 files changed, 34 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-buf.c +12 -2
index 76340d067f6c..6188cd11b004 100644
@@ -718,10 +718,14 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 		    || !exp_info->ops->release))
 		return ERR_PTR(-EINVAL);
 
-	if (exp_info->ops->match_mapping) {
+	if (exp_info->ops->match_mapping ||
+	    exp_info->ops->single_exporter_match) {
 		if (WARN_ON(exp_info->ops->map_dma_buf ||
 			    exp_info->ops->unmap_dma_buf))
 			return ERR_PTR(-EINVAL);
+		if (WARN_ON(exp_info->ops->match_mapping &&
+			    exp_info->ops->single_exporter_match))
+			return ERR_PTR(-EINVAL);
 	} else {
 		if (WARN_ON(!exp_info->ops->map_dma_buf ||
 			    !exp_info->ops->unmap_dma_buf))
@@ -1094,7 +1098,13 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
 		if (ret)
 			goto err_attach;
 	} else {
-		ret = dma_buf_sgt_compat_match_mapping(&match_args);
+		const struct dma_buf_mapping_match *exp_match =
+			dmabuf->ops->single_exporter_match;
+
+		if (exp_match)
+			ret = dma_buf_match_mapping(&match_args, exp_match, 1);
+		else
+			ret = dma_buf_sgt_compat_match_mapping(&match_args);
 		if (ret)
 			goto err_attach;
 	}
include/linux/dma-buf-mapping.h +13 -0
index 8be03b4ba6cc..e442ed0ede71 100644
@@ -244,4 +244,17 @@ dma_buf_sgt_mapping_dma_device(const struct dma_buf_sgt_mapping *mapping)
 /* Match on behalf of an exporter that has no match_mapping() */
 int dma_buf_sgt_compat_match_mapping(struct dma_buf_match_args *args);
 
+/*
+ * dma_buf_ops initializer helper for simple drivers that use a single
+ * SGT map/unmap operation.
+ */
+#define DMA_BUF_SIMPLE_SGT_EXP_MATCH(_map, _unmap)                       \
+	.single_exporter_match = &((const struct dma_buf_mapping_match){ \
+		.type = &dma_buf_mapping_sgt_type,                       \
+		.exp_ops = &((const struct dma_buf_mapping_sgt_exp_ops){ \
+			.map_dma_buf = _map,                             \
+			.unmap_dma_buf = _unmap,                         \
+		}).ops,                                                  \
+	})
+
 #endif
include/linux/dma-buf.h +9 -0
index cfbb800aa12d..5c8f090a9b8a 100644
@@ -322,6 +322,15 @@ struct dma_buf_ops {
 	 * any time until the final location is resolved during map/pin.
 	 */
 	int (*match_mapping)(struct dma_buf_match_args *args);
+
+	/**
+	 * @single_exporter_match:
+	 *
+	 * Should only be set by the DMA_BUF_SIMPLE_*_EXP_MATCH() helper macros.
+	 * Exactly one of @match_mapping or @single_exporter_match must be
+	 * provided.
+	 */
+	const struct dma_buf_mapping_match *single_exporter_match;
 };
 
 /**
Signature · 1 line
2.54.0
#7 lore raw

PATCH v3 7/24 dma-buf: Convert exporters allocating a table per map to the SGT mapping type

In reply to Alistair Popple

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.

---
 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(-)
drivers/accel/amdxdna/amdxdna_cbuf.c +18 -12
index a504560aae98..945436516cc6 100644
@@ -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)
drivers/accel/amdxdna/amdxdna_ubuf.c +20 -14
index 0e0cd69cd1fb..ed8b0ab9e554 100644
@@ -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,
drivers/accel/habanalabs/common/memory.c +29 -23

134 lines not on this page · raw message

drivers/gpu/drm/armada/armada_gem.c +21 -15
index 044acdef6351..1eff00567386 100644
@@ -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 *
drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c +17 -13
index f2863da7f5c1..bba066f3cb50 100644
@@ -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)
drivers/gpu/drm/tegra/gem.c +18 -12
index e6aa6dae4ec8..8e288a34f10f 100644
@@ -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,
samples/vfio-mdev/mbochs.c +18 -13
index 64ea19253ee3..3502d0b37bd1 100644
@@ -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,
sound/soc/fsl/fsl_asrc_m2m.c +20 -12
index 58101ce07c9f..79fde68f9394 100644
@@ -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,
Signature · 1 line
2.54.0
maintainer Acked-by on v3 7/24
#8 lore raw

Re: [PATCH v3 07/24] dma-buf: Convert exporters allocating a table per map to the SGT mapping type

Quoted 808 lines · Alistair Popple

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.

---
  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(-)
drivers/accel/amdxdna/amdxdna_cbuf.c +18 -12
index a504560aae98..945436516cc6 100644
@@ -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)
drivers/accel/amdxdna/amdxdna_ubuf.c +20 -14
index 0e0cd69cd1fb..ed8b0ab9e554 100644
@@ -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,
drivers/accel/habanalabs/common/memory.c +29 -23
index 361cff577381..fc39ccd6f8db 100644
@@ -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,
drivers/gpu/drm/armada/armada_gem.c +21 -15
index 044acdef6351..1eff00567386 100644
@@ -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 *
drivers/gpu/drm/i915/gem/selftests/mock_dmabuf.c +17 -13
index f2863da7f5c1..bba066f3cb50 100644
@@ -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)
drivers/gpu/drm/tegra/gem.c +18 -12
index e6aa6dae4ec8..8e288a34f10f 100644
@@ -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,
samples/vfio-mdev/mbochs.c +18 -13
index 64ea19253ee3..3502d0b37bd1 100644
@@ -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,
sound/soc/fsl/fsl_asrc_m2m.c +18 -10
index 58101ce07c9f..79fde68f9394 100644
@@ -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)
 

sound/soc/fsl/fsl_asrc_m2m.c static void fsl_asrc_m2m_release(struct dma_buf *dmabuf) · line 468

Quoted 10 lines
sound/soc/fsl/fsl_asrc_m2m.c +2 -2
  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,
#9 lore raw

PATCH v3 8/24 iommufd/selftest: Convert the test exporter to the SGT mapping type

In reply to Alistair Popple

The selftest's dma-buf exporter has stub map and unmap callbacks returning -EOPNOTSUPP so only the prototypes change.

Also drop the attach and detach callbacks which are optional and don't do anything.

---

Changes for v3:

- Split out of the single exporter conversion patch.

---
 drivers/iommu/iommufd/selftest.c | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)
drivers/iommu/iommufd/selftest.c +5 -17
index 25387969ab75..b3fac041eb7e 100644
@@ -6,6 +6,7 @@
 #include <linux/anon_inodes.h>
 #include <linux/debugfs.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-resv.h>
 #include <linux/fault-inject.h>
 #include <linux/file.h>
@@ -1959,18 +1960,7 @@ struct iommufd_test_dma_buf {
 	bool revoked;
 };
 
-static int iommufd_test_dma_buf_attach(struct dma_buf *dmabuf,
-				       struct dma_buf_attachment *attachment)
-{
-	return 0;
-}
-
-static void iommufd_test_dma_buf_detach(struct dma_buf *dmabuf,
-					struct dma_buf_attachment *attachment)
-{
-}
-
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
 iommufd_test_dma_buf_map(struct dma_buf_attachment *attachment,
 			 enum dma_data_direction dir)
 {
@@ -1978,7 +1968,7 @@ iommufd_test_dma_buf_map(struct dma_buf_attachment *attachment,
 }
 
 static void iommufd_test_dma_buf_unmap(struct dma_buf_attachment *attachment,
-				       struct sg_table *sgt,
+				       struct dma_buf_sgt_mapping *mapping,
 				       enum dma_data_direction dir)
 {
 }
@@ -1992,11 +1982,9 @@ static void iommufd_test_dma_buf_release(struct dma_buf *dmabuf)
 }
 
 static const struct dma_buf_ops iommufd_test_dmabuf_ops = {
-	.attach = iommufd_test_dma_buf_attach,
-	.detach = iommufd_test_dma_buf_detach,
-	.map_dma_buf = iommufd_test_dma_buf_map,
 	.release = iommufd_test_dma_buf_release,
-	.unmap_dma_buf = iommufd_test_dma_buf_unmap,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(iommufd_test_dma_buf_map,
+				     iommufd_test_dma_buf_unmap),
 };
 
 int iommufd_test_dma_buf_iommufd_map(struct dma_buf_attachment *attachment,
Signature · 1 line
2.54.0
#10 lore raw

PATCH v3 9/24 dma-buf: Convert exporters keeping a table per attachment to the SGT mapping type

In reply to Alistair Popple

These exporters all keep one struct sg_table per attachment. It is embedded in either attachment private data or allocated and cached on first map. The same table is returned on every map_dma_buf() and nothing is freed in unmap_dma_buf(). Instead the table is freed during detach.

The private/cached struct sg_table becomes a struct dma_buf_sgt_mapping and map_dma_buf() returns it every time. That is safe because the core only fills in the mapping's header, and fills it identically for every attachment map.

---
 drivers/dma-buf/heaps/cma_heap.c                   | 33 ++++++-----
 drivers/dma-buf/heaps/system_heap.c                | 34 ++++++-----
 .../media/common/videobuf2/videobuf2-dma-contig.c  | 34 +++++------
 drivers/media/common/videobuf2/videobuf2-dma-sg.c  | 33 ++++++-----
 drivers/media/common/videobuf2/videobuf2-vmalloc.c | 32 ++++++-----
 drivers/misc/fastrpc.c                             | 30 +++++-----
 drivers/tee/tee_heap.c                             | 28 ++++-----
 drivers/xen/gntdev-dmabuf.c                        | 67 ++++++++++++----------
 8 files changed, 158 insertions(+), 133 deletions(-)
drivers/dma-buf/heaps/cma_heap.c +18 -15
index 3fb4b946c91a..79bbb31e31cf 100644
@@ -14,6 +14,7 @@
 
 #include <linux/cma.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-heap.h>
 #include <linux/dma-map-ops.h>
 #include <linux/err.h>
@@ -48,7 +49,7 @@ struct cma_heap_buffer {
 
 struct dma_heap_attachment {
 	struct device *dev;
-	struct sg_table table;
+	struct dma_buf_sgt_mapping mapping;
 	struct list_head list;
 	bool mapped;
 };
@@ -64,7 +65,7 @@ static int cma_heap_attach(struct dma_buf *dmabuf,
 	if (!a)
 		return -ENOMEM;
 
-	ret = sg_alloc_table_from_pages(&a->table, buffer->pages,
+	ret = sg_alloc_table_from_pages(&a->mapping.sgt, buffer->pages,
 					buffer->pagecount, 0,
 					buffer->pagecount << PAGE_SHIFT,
 					GFP_KERNEL);
@@ -73,7 +74,7 @@ static int cma_heap_attach(struct dma_buf *dmabuf,
 		return ret;
 	}
 
-	a->dev = attachment->dev;
+	a->dev = dma_buf_sgt_dma_device(attachment);
 	INIT_LIST_HEAD(&a->list);
 	a->mapped = false;
 
@@ -96,32 +97,35 @@ static void cma_heap_detach(struct dma_buf *dmabuf,
 	list_del(&a->list);
 	mutex_unlock(&buffer->lock);
 
-	sg_free_table(&a->table);
+	sg_free_table(&a->mapping.sgt);
 	kfree(a);
 }
 
-static struct sg_table *cma_heap_map_dma_buf(struct dma_buf_attachment *attachment,
-					     enum dma_data_direction direction)
+static struct dma_buf_sgt_mapping *
+cma_heap_map_dma_buf(struct dma_buf_attachment *attachment,
+		     enum dma_data_direction direction)
 {
 	struct dma_heap_attachment *a = attachment->priv;
-	struct sg_table *table = &a->table;
+	struct sg_table *table = &a->mapping.sgt;
 	int ret;
 
-	ret = dma_map_sgtable(attachment->dev, table, direction, 0);
+	ret = dma_map_sgtable(dma_buf_sgt_dma_device(attachment), table,
+			      direction, 0);
 	if (ret)
 		return ERR_PTR(-ENOMEM);
 	a->mapped = true;
-	return table;
+	return &a->mapping;
 }
 
 static void cma_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
-				   struct sg_table *table,
+				   struct dma_buf_sgt_mapping *mapping,
 				   enum dma_data_direction direction)
 {
 	struct dma_heap_attachment *a = attachment->priv;
 
 	a->mapped = false;
-	dma_unmap_sgtable(attachment->dev, table, direction, 0);
+	dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping),
+			  &mapping->sgt, direction, 0);
 }
 
 static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
@@ -138,7 +142,7 @@ static int cma_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
 	list_for_each_entry(a, &buffer->attachments, list) {
 		if (!a->mapped)
 			continue;
-		dma_sync_sgtable_for_cpu(a->dev, &a->table, direction);
+		dma_sync_sgtable_for_cpu(a->dev, &a->mapping.sgt, direction);
 	}
 	mutex_unlock(&buffer->lock);
 
@@ -159,7 +163,7 @@ static int cma_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
 	list_for_each_entry(a, &buffer->attachments, list) {
 		if (!a->mapped)
 			continue;
-		dma_sync_sgtable_for_device(a->dev, &a->table, direction);
+		dma_sync_sgtable_for_device(a->dev, &a->mapping.sgt, direction);
 	}
 	mutex_unlock(&buffer->lock);
 
@@ -268,14 +272,13 @@ static void cma_heap_dma_buf_release(struct dma_buf *dmabuf)
 static const struct dma_buf_ops cma_heap_buf_ops = {
 	.attach = cma_heap_attach,
 	.detach = cma_heap_detach,
-	.map_dma_buf = cma_heap_map_dma_buf,
-	.unmap_dma_buf = cma_heap_unmap_dma_buf,
 	.begin_cpu_access = cma_heap_dma_buf_begin_cpu_access,
 	.end_cpu_access = cma_heap_dma_buf_end_cpu_access,
 	.mmap = cma_heap_mmap,
 	.vmap = cma_heap_vmap,
 	.vunmap = cma_heap_vunmap,
 	.release = cma_heap_dma_buf_release,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(cma_heap_map_dma_buf, cma_heap_unmap_dma_buf),
 };
 
 static struct dma_buf *cma_heap_allocate(struct dma_heap *heap,
drivers/dma-buf/heaps/system_heap.c +19 -15
index c8959eadc71d..4c306f7fe506 100644
@@ -12,6 +12,7 @@
 
 #include <linux/cc_platform.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-mapping.h>
 #include <linux/dma-heap.h>
 #include <linux/err.h>
@@ -42,7 +43,7 @@ struct system_heap_buffer {
 
 struct dma_heap_attachment {
 	struct device *dev;
-	struct sg_table table;
+	struct dma_buf_sgt_mapping mapping;
 	struct list_head list;
 	bool mapped;
 	bool cc_shared;
@@ -122,13 +123,13 @@ static int system_heap_attach(struct dma_buf *dmabuf,
 	if (!a)
 		return -ENOMEM;
 
-	ret = dup_sg_table(&buffer->sg_table, &a->table);
+	ret = dup_sg_table(&buffer->sg_table, &a->mapping.sgt);
 	if (ret) {
 		kfree(a);
 		return ret;
 	}
 
-	a->dev = attachment->dev;
+	a->dev = dma_buf_sgt_dma_device(attachment);
 	INIT_LIST_HEAD(&a->list);
 	a->mapped = false;
 	a->cc_shared = buffer->cc_shared;
@@ -152,35 +153,38 @@ static void system_heap_detach(struct dma_buf *dmabuf,
 	list_del(&a->list);
 	mutex_unlock(&buffer->lock);
 
-	sg_free_table(&a->table);
+	sg_free_table(&a->mapping.sgt);
 	kfree(a);
 }
 
-static struct sg_table *system_heap_map_dma_buf(struct dma_buf_attachment *attachment,
-						enum dma_data_direction direction)
+static struct dma_buf_sgt_mapping *
+system_heap_map_dma_buf(struct dma_buf_attachment *attachment,
+			enum dma_data_direction direction)
 {
 	struct dma_heap_attachment *a = attachment->priv;
-	struct sg_table *table = &a->table;
+	struct sg_table *table = &a->mapping.sgt;
 	unsigned long attrs;
 	int ret;
 
 	attrs = cc_shared_buffer(a) ? DMA_ATTR_CC_SHARED : 0;
-	ret = dma_map_sgtable(attachment->dev, table, direction, attrs);
+	ret = dma_map_sgtable(dma_buf_sgt_dma_device(attachment), table,
+			      direction, attrs);
 	if (ret)
 		return ERR_PTR(ret);
 
 	a->mapped = true;
-	return table;
+	return &a->mapping;
 }
 
 static void system_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
-				      struct sg_table *table,
+				      struct dma_buf_sgt_mapping *mapping,
 				      enum dma_data_direction direction)
 {
 	struct dma_heap_attachment *a = attachment->priv;
 
 	a->mapped = false;
-	dma_unmap_sgtable(attachment->dev, table, direction, 0);
+	dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping),
+			  &mapping->sgt, direction, 0);
 }
 
 static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
@@ -197,7 +201,7 @@ static int system_heap_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
 	list_for_each_entry(a, &buffer->attachments, list) {
 		if (!a->mapped)
 			continue;
-		dma_sync_sgtable_for_cpu(a->dev, &a->table, direction);
+		dma_sync_sgtable_for_cpu(a->dev, &a->mapping.sgt, direction);
 	}
 	mutex_unlock(&buffer->lock);
 
@@ -218,7 +222,7 @@ static int system_heap_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
 	list_for_each_entry(a, &buffer->attachments, list) {
 		if (!a->mapped)
 			continue;
-		dma_sync_sgtable_for_device(a->dev, &a->table, direction);
+		dma_sync_sgtable_for_device(a->dev, &a->mapping.sgt, direction);
 	}
 	mutex_unlock(&buffer->lock);
 
@@ -365,14 +369,14 @@ static void system_heap_dma_buf_release(struct dma_buf *dmabuf)
 static const struct dma_buf_ops system_heap_buf_ops = {
 	.attach = system_heap_attach,
 	.detach = system_heap_detach,
-	.map_dma_buf = system_heap_map_dma_buf,
-	.unmap_dma_buf = system_heap_unmap_dma_buf,
 	.begin_cpu_access = system_heap_dma_buf_begin_cpu_access,
 	.end_cpu_access = system_heap_dma_buf_end_cpu_access,
 	.mmap = system_heap_mmap,
 	.vmap = system_heap_vmap,
 	.vunmap = system_heap_vunmap,
 	.release = system_heap_dma_buf_release,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(system_heap_map_dma_buf,
+				     system_heap_unmap_dma_buf),
 };
 
 static struct page *alloc_largest_available(unsigned long size,
drivers/media/common/videobuf2/videobuf2-dma-contig.c +18 -16
index 284482335c1a..5f410c6c653d 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/module.h>
 #include <linux/refcount.h>
 #include <linux/scatterlist.h>
@@ -311,7 +312,7 @@ static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
 /*********************************************/
 
 struct vb2_dc_attachment {
-	struct sg_table sgt;
+	struct dma_buf_sgt_mapping mapping;
 	enum dma_data_direction dma_dir;
 };
 
@@ -329,7 +330,7 @@ static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf,
 	if (!attach)
 		return -ENOMEM;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 	/* Copy the buf->base_sgt scatter list to the attachment, as we can't
 	 * map the same scatter list to multiple attachments at the same time.
 	 */
@@ -362,7 +363,7 @@ static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
 	if (!attach)
 		return;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 
 	/* release the scatterlist cache */
 	if (attach->dma_dir != DMA_NONE)
@@ -372,28 +373,29 @@ static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
 		 * memory locations do not require any explicit cache
 		 * maintenance prior or after being used by the device.
 		 */
-		dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir,
-				  DMA_ATTR_SKIP_CPU_SYNC);
+		dma_unmap_sgtable(dma_buf_sgt_dma_device(db_attach), sgt,
+				  attach->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
 	sg_free_table(sgt);
 	kfree(attach);
 	db_attach->priv = NULL;
 }
 
-static struct sg_table *vb2_dc_dmabuf_ops_map(
-	struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
+static struct dma_buf_sgt_mapping *
+vb2_dc_dmabuf_ops_map(struct dma_buf_attachment *db_attach,
+		      enum dma_data_direction dma_dir)
 {
 	struct vb2_dc_attachment *attach = db_attach->priv;
 	struct sg_table *sgt;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 	/* return previously mapped sg table */
 	if (attach->dma_dir == dma_dir)
-		return sgt;
+		return &attach->mapping;
 
 	/* release any previous cache */
 	if (attach->dma_dir != DMA_NONE) {
-		dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir,
-				  DMA_ATTR_SKIP_CPU_SYNC);
+		dma_unmap_sgtable(dma_buf_sgt_dma_device(db_attach), sgt,
+				  attach->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
 		attach->dma_dir = DMA_NONE;
 	}
 
@@ -401,7 +403,7 @@ static struct sg_table *vb2_dc_dmabuf_ops_map(
 	 * mapping to the client with new direction, no cache sync
 	 * required see comment in vb2_dc_dmabuf_ops_detach()
 	 */
-	if (dma_map_sgtable(db_attach->dev, sgt, dma_dir,
+	if (dma_map_sgtable(dma_buf_sgt_dma_device(db_attach), sgt, dma_dir,
 			    DMA_ATTR_SKIP_CPU_SYNC)) {
 		pr_err("failed to map scatterlist\n");
 		return ERR_PTR(-EIO);
@@ -409,11 +411,11 @@ static struct sg_table *vb2_dc_dmabuf_ops_map(
 
 	attach->dma_dir = dma_dir;
 
-	return sgt;
+	return &attach->mapping;
 }
 
 static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
-	struct sg_table *sgt, enum dma_data_direction dma_dir)
+	struct dma_buf_sgt_mapping *mapping, enum dma_data_direction dma_dir)
 {
 	/* nothing to be done here */
 }
@@ -462,13 +464,13 @@ static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
 static const struct dma_buf_ops vb2_dc_dmabuf_ops = {
 	.attach = vb2_dc_dmabuf_ops_attach,
 	.detach = vb2_dc_dmabuf_ops_detach,
-	.map_dma_buf = vb2_dc_dmabuf_ops_map,
-	.unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
 	.begin_cpu_access = vb2_dc_dmabuf_ops_begin_cpu_access,
 	.end_cpu_access = vb2_dc_dmabuf_ops_end_cpu_access,
 	.vmap = vb2_dc_dmabuf_ops_vmap,
 	.mmap = vb2_dc_dmabuf_ops_mmap,
 	.release = vb2_dc_dmabuf_ops_release,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(vb2_dc_dmabuf_ops_map,
+				     vb2_dc_dmabuf_ops_unmap),
 };
 
 static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
drivers/media/common/videobuf2/videobuf2-dma-sg.c +19 -14
index 54b07e3840ac..c00863ac0f88 100644
@@ -10,6 +10,7 @@
  * the Free Software Foundation.
  */
 
+#include <linux/dma-buf-mapping.h>
 #include <linux/module.h>
 #include <linux/mm.h>
 #include <linux/refcount.h>
@@ -362,7 +363,7 @@ static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
 /*********************************************/
 
 struct vb2_dma_sg_attachment {
-	struct sg_table sgt;
+	struct dma_buf_sgt_mapping mapping;
 	enum dma_data_direction dma_dir;
 };
 
@@ -380,7 +381,7 @@ static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf,
 	if (!attach)
 		return -ENOMEM;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 	/* Copy the buf->base_sgt scatter list to the attachment, as we can't
 	 * map the same scatter list to multiple attachments at the same time.
 	 */
@@ -413,46 +414,50 @@ static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
 	if (!attach)
 		return;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 
 	/* release the scatterlist cache */
 	if (attach->dma_dir != DMA_NONE)
-		dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
+		dma_unmap_sgtable(dma_buf_sgt_dma_device(db_attach), sgt,
+				  attach->dma_dir, 0);
 	sg_free_table(sgt);
 	kfree(attach);
 	db_attach->priv = NULL;
 }
 
-static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
-	struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
+static struct dma_buf_sgt_mapping *
+vb2_dma_sg_dmabuf_ops_map(struct dma_buf_attachment *db_attach,
+			  enum dma_data_direction dma_dir)
 {
 	struct vb2_dma_sg_attachment *attach = db_attach->priv;
 	struct sg_table *sgt;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 	/* return previously mapped sg table */
 	if (attach->dma_dir == dma_dir)
-		return sgt;
+		return &attach->mapping;
 
 	/* release any previous cache */
 	if (attach->dma_dir != DMA_NONE) {
-		dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
+		dma_unmap_sgtable(dma_buf_sgt_dma_device(db_attach), sgt,
+				  attach->dma_dir, 0);
 		attach->dma_dir = DMA_NONE;
 	}
 
 	/* mapping to the client with new direction */
-	if (dma_map_sgtable(db_attach->dev, sgt, dma_dir, 0)) {
+	if (dma_map_sgtable(dma_buf_sgt_dma_device(db_attach), sgt, dma_dir,
+			    0)) {
 		pr_err("failed to map scatterlist\n");
 		return ERR_PTR(-EIO);
 	}
 
 	attach->dma_dir = dma_dir;
 
-	return sgt;
+	return &attach->mapping;
 }
 
 static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
-	struct sg_table *sgt, enum dma_data_direction dma_dir)
+	struct dma_buf_sgt_mapping *mapping, enum dma_data_direction dma_dir)
 {
 	/* nothing to be done here */
 }
@@ -510,13 +515,13 @@ static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
 static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
 	.attach = vb2_dma_sg_dmabuf_ops_attach,
 	.detach = vb2_dma_sg_dmabuf_ops_detach,
-	.map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
-	.unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
 	.begin_cpu_access = vb2_dma_sg_dmabuf_ops_begin_cpu_access,
 	.end_cpu_access = vb2_dma_sg_dmabuf_ops_end_cpu_access,
 	.vmap = vb2_dma_sg_dmabuf_ops_vmap,
 	.mmap = vb2_dma_sg_dmabuf_ops_mmap,
 	.release = vb2_dma_sg_dmabuf_ops_release,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(vb2_dma_sg_dmabuf_ops_map,
+				     vb2_dma_sg_dmabuf_ops_unmap),
 };
 
 static struct dma_buf *vb2_dma_sg_get_dmabuf(struct vb2_buffer *vb,
drivers/media/common/videobuf2/videobuf2-vmalloc.c +18 -14
index 63f023696f3e..0c4d14fe62da 100644
@@ -10,6 +10,7 @@
  * the Free Software Foundation.
  */
 
+#include <linux/dma-buf-mapping.h>
 #include <linux/io.h>
 #include <linux/module.h>
 #include <linux/mm.h>
@@ -205,7 +206,7 @@ static int vb2_vmalloc_mmap(void *buf_priv, struct vm_area_struct *vma)
 /*********************************************/
 
 struct vb2_vmalloc_attachment {
-	struct sg_table sgt;
+	struct dma_buf_sgt_mapping mapping;
 	enum dma_data_direction dma_dir;
 };
 
@@ -225,7 +226,7 @@ static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf *dbuf,
 	if (!attach)
 		return -ENOMEM;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 	ret = sg_alloc_table(sgt, num_pages, GFP_KERNEL);
 	if (ret) {
 		kfree(attach);
@@ -257,46 +258,49 @@ static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf *dbuf,
 	if (!attach)
 		return;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 
 	/* release the scatterlist cache */
 	if (attach->dma_dir != DMA_NONE)
-		dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
+		dma_unmap_sgtable(dma_buf_sgt_dma_device(db_attach), sgt,
+				  attach->dma_dir, 0);
 	sg_free_table(sgt);
 	kfree(attach);
 	db_attach->priv = NULL;
 }
 
-static struct sg_table *vb2_vmalloc_dmabuf_ops_map(
-	struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
+static struct dma_buf_sgt_mapping *
+vb2_vmalloc_dmabuf_ops_map(struct dma_buf_attachment *db_attach,
+			   enum dma_data_direction dma_dir)
 {
+	struct device *dma_dev = dma_buf_sgt_dma_device(db_attach);
 	struct vb2_vmalloc_attachment *attach = db_attach->priv;
 	struct sg_table *sgt;
 
-	sgt = &attach->sgt;
+	sgt = &attach->mapping.sgt;
 	/* return previously mapped sg table */
 	if (attach->dma_dir == dma_dir)
-		return sgt;
+		return &attach->mapping;
 
 	/* release any previous cache */
 	if (attach->dma_dir != DMA_NONE) {
-		dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
+		dma_unmap_sgtable(dma_dev, sgt, attach->dma_dir, 0);
 		attach->dma_dir = DMA_NONE;
 	}
 
 	/* mapping to the client with new direction */
-	if (dma_map_sgtable(db_attach->dev, sgt, dma_dir, 0)) {
+	if (dma_map_sgtable(dma_dev, sgt, dma_dir, 0)) {
 		pr_err("failed to map scatterlist\n");
 		return ERR_PTR(-EIO);
 	}
 
 	attach->dma_dir = dma_dir;
 
-	return sgt;
+	return &attach->mapping;
 }
 
 static void vb2_vmalloc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
-	struct sg_table *sgt, enum dma_data_direction dma_dir)
+	struct dma_buf_sgt_mapping *mapping, enum dma_data_direction dma_dir)
 {
 	/* nothing to be done here */
 }
@@ -326,11 +330,11 @@ static int vb2_vmalloc_dmabuf_ops_mmap(struct dma_buf *dbuf,
 static const struct dma_buf_ops vb2_vmalloc_dmabuf_ops = {
 	.attach = vb2_vmalloc_dmabuf_ops_attach,
 	.detach = vb2_vmalloc_dmabuf_ops_detach,
-	.map_dma_buf = vb2_vmalloc_dmabuf_ops_map,
-	.unmap_dma_buf = vb2_vmalloc_dmabuf_ops_unmap,
 	.vmap = vb2_vmalloc_dmabuf_ops_vmap,
 	.mmap = vb2_vmalloc_dmabuf_ops_mmap,
 	.release = vb2_vmalloc_dmabuf_ops_release,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(vb2_vmalloc_dmabuf_ops_map,
+				     vb2_vmalloc_dmabuf_ops_unmap),
 };
 
 static struct dma_buf *vb2_vmalloc_get_dmabuf(struct vb2_buffer *vb,
drivers/misc/fastrpc.c +15 -15
index 76c8dc05fa10..cbad55a93312 100644
@@ -5,6 +5,7 @@
 #include <linux/completion.h>
 #include <linux/device.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-mapping.h>
 #include <linux/dma-resv.h>
 #include <linux/idr.h>
@@ -234,7 +235,7 @@ struct fastrpc_buf {
 
 struct fastrpc_dma_buf_attachment {
 	struct device *dev;
-	struct sg_table sgt;
+	struct dma_buf_sgt_mapping mapping;
 	struct list_head node;
 };
 
@@ -770,27 +771,26 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 	return ERR_PTR(ret);
 }
 
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
 fastrpc_map_dma_buf(struct dma_buf_attachment *attachment,
 		    enum dma_data_direction dir)
 {
 	struct fastrpc_dma_buf_attachment *a = attachment->priv;
-	struct sg_table *table;
 	int ret;
 
-	table = &a->sgt;
-
-	ret = dma_map_sgtable(attachment->dev, table, dir, 0);
+	ret = dma_map_sgtable(dma_buf_sgt_dma_device(attachment),
+			      &a->mapping.sgt, dir, 0);
 	if (ret)
-		table = ERR_PTR(ret);
-	return table;
+		return ERR_PTR(ret);
+	return &a->mapping;
 }
 
 static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach,
-				  struct sg_table *table,
+				  struct dma_buf_sgt_mapping *mapping,
 				  enum dma_data_direction dir)
 {
-	dma_unmap_sgtable(attach->dev, table, dir, 0);
+	dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping),
+			  &mapping->sgt, dir, 0);
 }
 
 static void fastrpc_release(struct dma_buf *dmabuf)
@@ -811,7 +811,7 @@ static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
 	if (!a)
 		return -ENOMEM;
 
-	ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt,
+	ret = dma_get_sgtable(buffer->dev, &a->mapping.sgt, buffer->virt,
 			      fastrpc_ipa_to_dma_addr(buffer->fl->cctx, buffer->dma_addr),
 			      buffer->size);
 	if (ret < 0) {
@@ -820,7 +820,7 @@ static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf,
 		return -EINVAL;
 	}
 
-	a->dev = attachment->dev;
+	a->dev = dma_buf_sgt_dma_device(attachment);
 	INIT_LIST_HEAD(&a->node);
 	attachment->priv = a;
 
@@ -840,7 +840,7 @@ static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf,
 	mutex_lock(&buffer->lock);
 	list_del(&a->node);
 	mutex_unlock(&buffer->lock);
-	sg_free_table(&a->sgt);
+	sg_free_table(&a->mapping.sgt);
 	kfree(a);
 }
 
@@ -868,11 +868,11 @@ static int fastrpc_mmap(struct dma_buf *dmabuf,
 static const struct dma_buf_ops fastrpc_dma_buf_ops = {
 	.attach = fastrpc_dma_buf_attach,
 	.detach = fastrpc_dma_buf_detatch,
-	.map_dma_buf = fastrpc_map_dma_buf,
-	.unmap_dma_buf = fastrpc_unmap_dma_buf,
 	.mmap = fastrpc_mmap,
 	.vmap = fastrpc_vmap,
 	.release = fastrpc_release,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(fastrpc_map_dma_buf,
+				     fastrpc_unmap_dma_buf),
 };
 
 static dma_addr_t fastrpc_compute_dma_addr(struct fastrpc_user *fl, dma_addr_t sg_dma_addr)
drivers/tee/tee_heap.c +15 -13
index 007c00156520..1d8a941891e9 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-heap.h>
 #include <linux/genalloc.h>
 #include <linux/module.h>
@@ -33,7 +34,7 @@ struct tee_heap_buffer {
 };
 
 struct tee_heap_attachment {
-	struct sg_table table;
+	struct dma_buf_sgt_mapping mapping;
 	struct device *dev;
 };
 
@@ -98,13 +99,13 @@ static int tee_heap_attach(struct dma_buf *dmabuf,
 	if (!a)
 		return -ENOMEM;
 
-	ret = copy_sg_table(&a->table, &buf->table);
+	ret = copy_sg_table(&a->mapping.sgt, &buf->table);
 	if (ret) {
 		kfree(a);
 		return ret;
 	}
 
-	a->dev = attachment->dev;
+	a->dev = dma_buf_sgt_dma_device(attachment);
 	attachment->priv = a;
 
 	return 0;
@@ -115,35 +116,36 @@ static void tee_heap_detach(struct dma_buf *dmabuf,
 {
 	struct tee_heap_attachment *a = attachment->priv;
 
-	sg_free_table(&a->table);
+	sg_free_table(&a->mapping.sgt);
 	kfree(a);
 }
 
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
 tee_heap_map_dma_buf(struct dma_buf_attachment *attachment,
 		     enum dma_data_direction direction)
 {
 	struct tee_heap_attachment *a = attachment->priv;
 	int ret;
 
-	ret = dma_map_sgtable(attachment->dev, &a->table, direction,
+	ret = dma_map_sgtable(dma_buf_sgt_dma_device(attachment),
+			      &a->mapping.sgt, direction,
 			      DMA_ATTR_SKIP_CPU_SYNC);
 	if (ret)
 		return ERR_PTR(ret);
 
-	return &a->table;
+	return &a->mapping;
 }
 
 static void tee_heap_unmap_dma_buf(struct dma_buf_attachment *attachment,
-				   struct sg_table *table,
+				   struct dma_buf_sgt_mapping *mapping,
 				   enum dma_data_direction direction)
 {
 	struct tee_heap_attachment *a = attachment->priv;
 
-	WARN_ON(&a->table != table);
+	WARN_ON(&a->mapping != mapping);
 
-	dma_unmap_sgtable(attachment->dev, table, direction,
-			  DMA_ATTR_SKIP_CPU_SYNC);
+	dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping),
+			  &mapping->sgt, direction, DMA_ATTR_SKIP_CPU_SYNC);
 }
 
 static void tee_heap_buf_free(struct dma_buf *dmabuf)
@@ -160,9 +162,9 @@ static void tee_heap_buf_free(struct dma_buf *dmabuf)
 static const struct dma_buf_ops tee_heap_buf_ops = {
 	.attach = tee_heap_attach,
 	.detach = tee_heap_detach,
-	.map_dma_buf = tee_heap_map_dma_buf,
-	.unmap_dma_buf = tee_heap_unmap_dma_buf,
 	.release = tee_heap_buf_free,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(tee_heap_map_dma_buf,
+				     tee_heap_unmap_dma_buf),
 };
 
 static struct dma_buf *tee_dma_heap_alloc(struct dma_heap *heap,
drivers/xen/gntdev-dmabuf.c +36 -31

151 lines not on this page · raw message

Signature · 1 line
2.54.0
#11 lore raw

PATCH v3 10/24 drm: Convert drm_gem_map_dma_buf() and its users to the SGT mapping type

In reply to Alistair Popple

drm_gem_map_dma_buf() gets its table from the object's get_sg_table(), which allocates and drm_gem_unmap_dma_buf() frees it.

Rather than pass a caller-provided table through every get_sg_table() implementation have the mapping take over the table. It copies the headers into the embedded struct dma_buf_sgt_mapping.sgt and frees the container. Unmap then frees the entries through the copy.

drm_gem_map_attach() checked dma_buf->ops->map_dma_buf to see whether drm_gem_map_dma_buf() is in use. That callback now lives in the SGT exporter ops, so check those instead. virtio-gpu's prime callbacks wrap drm_gem_map_dma_buf() and so are converted here too.

---
 drivers/accel/amdxdna/amdxdna_gem.c        |  5 ++-
 drivers/gpu/drm/drm_prime.c                | 72 ++++++++++++++++++++----------
 drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c | 23 ++++++----
 drivers/gpu/drm/msm/msm_gem_prime.c        |  7 ++-
 drivers/gpu/drm/panfrost/panfrost_gem.c    | 27 ++++++-----
 drivers/gpu/drm/panthor/panthor_gem.c      | 25 ++++++-----
 drivers/gpu/drm/virtio/virtgpu_drv.h       |  9 ++--
 drivers/gpu/drm/virtio/virtgpu_prime.c     | 22 +++++----
 drivers/gpu/drm/virtio/virtgpu_vram.c      | 25 ++++++-----
 include/drm/drm_prime.h                    |  8 ++--
 10 files changed, 139 insertions(+), 84 deletions(-)
drivers/accel/amdxdna/amdxdna_gem.c +3 -2
index adee6e806eed..74b8e86638b1 100644
@@ -11,6 +11,7 @@
 #include <drm/drm_print.h>
 #include <drm/gpu_scheduler.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-direct.h>
 #include <linux/iosys-map.h>
 #include <linux/pagemap.h>
@@ -669,12 +670,12 @@ static int amdxdna_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struc
 static const struct dma_buf_ops amdxdna_dmabuf_ops = {
 	.attach = drm_gem_map_attach,
 	.detach = drm_gem_map_detach,
-	.map_dma_buf = drm_gem_map_dma_buf,
-	.unmap_dma_buf = drm_gem_unmap_dma_buf,
 	.release = drm_gem_dmabuf_release,
 	.mmap = amdxdna_gem_dmabuf_mmap,
 	.vmap = drm_gem_dmabuf_vmap,
 	.vunmap = drm_gem_dmabuf_vunmap,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(drm_gem_map_dma_buf,
+				     drm_gem_unmap_dma_buf),
 };
 
 static struct dma_buf *amdxdna_gem_prime_export(struct drm_gem_object *gobj, int flags)
drivers/gpu/drm/drm_prime.c +48 -24

135 lines not on this page · raw message

drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c +14 -9
index 4ab54d2e616f..41210ca14a92 100644
@@ -4,6 +4,7 @@
  */
 
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/highmem.h>
 #include <linux/dma-resv.h>
 #include <linux/module.h>
@@ -24,10 +25,12 @@ static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
 	return to_intel_bo(buf->priv);
 }
 
-static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attach,
-					     enum dma_data_direction dir)
+static struct dma_buf_sgt_mapping *
+i915_gem_map_dma_buf(struct dma_buf_attachment *attach,
+		     enum dma_data_direction dir)
 {
 	struct drm_i915_gem_object *obj = dma_buf_to_obj(attach->dmabuf);
+	struct dma_buf_sgt_mapping *mapping;
 	struct sg_table *sgt;
 	struct scatterlist *src, *dst;
 	int ret, i;
@@ -36,11 +39,12 @@ static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attach,
 	 * Make a copy of the object's sgt, so that we can make an independent
 	 * mapping
 	 */
-	sgt = kmalloc_obj(*sgt);
-	if (!sgt) {
+	mapping = kzalloc_obj(*mapping);
+	if (!mapping) {
 		ret = -ENOMEM;
 		goto err;
 	}
+	sgt = &mapping->sgt;
 
 	ret = sg_alloc_table(sgt, obj->mm.pages->orig_nents, GFP_KERNEL);
 	if (ret)
@@ -52,16 +56,17 @@ static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attach,
 		dst = sg_next(dst);
 	}
 
-	ret = dma_map_sgtable(attach->dev, sgt, dir, DMA_ATTR_SKIP_CPU_SYNC);
+	ret = dma_map_sgtable(dma_buf_sgt_dma_device(attach), sgt, dir,
+			      DMA_ATTR_SKIP_CPU_SYNC);
 	if (ret)
 		goto err_free_sg;
 
-	return sgt;
+	return mapping;
 
 err_free_sg:
 	sg_free_table(sgt);
 err_free:
-	kfree(sgt);
+	kfree(mapping);
 err:
 	return ERR_PTR(ret);
 }
@@ -203,14 +208,14 @@ static void i915_gem_dmabuf_detach(struct dma_buf *dmabuf,
 static const struct dma_buf_ops i915_dmabuf_ops =  {
 	.attach = i915_gem_dmabuf_attach,
 	.detach = i915_gem_dmabuf_detach,
-	.map_dma_buf = i915_gem_map_dma_buf,
-	.unmap_dma_buf = drm_gem_unmap_dma_buf,
 	.release = drm_gem_dmabuf_release,
 	.mmap = i915_gem_dmabuf_mmap,
 	.vmap = i915_gem_dmabuf_vmap,
 	.vunmap = i915_gem_dmabuf_vunmap,
 	.begin_cpu_access = i915_gem_begin_cpu_access,
 	.end_cpu_access = i915_gem_end_cpu_access,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(i915_gem_map_dma_buf,
+				     drm_gem_unmap_dma_buf),
 };
 
 struct dma_buf *i915_gem_prime_export(struct drm_gem_object *gem_obj, int flags)
drivers/gpu/drm/msm/msm_gem_prime.c +5 -2
index e1e74e9b7e6a..b6aae3c85250 100644
@@ -5,6 +5,7 @@
  */
 
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 
 #include <drm/drm_drv.h>
 #include <drm/drm_prime.h>
@@ -54,12 +55,12 @@ static void msm_gem_dmabuf_release(struct dma_buf *dma_buf)
 static const struct dma_buf_ops msm_gem_prime_dmabuf_ops =  {
 	.attach = drm_gem_map_attach,
 	.detach = drm_gem_map_detach,
-	.map_dma_buf = drm_gem_map_dma_buf,
-	.unmap_dma_buf = drm_gem_unmap_dma_buf,
 	.release = msm_gem_dmabuf_release,
 	.mmap = drm_gem_dmabuf_mmap,
 	.vmap = drm_gem_dmabuf_vmap,
 	.vunmap = drm_gem_dmabuf_vunmap,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(drm_gem_map_dma_buf,
+				     drm_gem_unmap_dma_buf),
 };
 
 struct drm_gem_object *msm_gem_prime_import(struct drm_device *dev,
@@ -125,3 +126,5 @@ void msm_gem_prime_unpin(struct drm_gem_object *obj)
 
 	msm_gem_unpin_pages_locked(obj);
 }
+
+MODULE_IMPORT_NS("DMA_BUF");
drivers/gpu/drm/panfrost/panfrost_gem.c +16 -11
index 518f24549b9d..e54818736a31 100644
@@ -6,6 +6,7 @@
 #include <linux/err.h>
 #include <linux/slab.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-mapping.h>
 
 #include <drm/panfrost_drm.h>
@@ -23,6 +24,8 @@ MODULE_PARM_DESC(transparent_hugepage, "Use a dedicated tmpfs mount point with T
 #define panfrost_transparent_hugepage false
 #endif
 
+MODULE_IMPORT_NS("DMA_BUF");
+
 int panfrost_gem_init(struct panfrost_device *pfdev)
 {
 	if (panfrost_transparent_hugepage) {
@@ -289,25 +292,25 @@ static size_t panfrost_gem_rss(struct drm_gem_object *obj)
 	return 0;
 }
 
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
 panfrost_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
 			       enum dma_data_direction dir)
 {
-	struct sg_table *sgt = drm_gem_map_dma_buf(attach, dir);
+	struct dma_buf_sgt_mapping *mapping = drm_gem_map_dma_buf(attach, dir);
 
-	if (!IS_ERR(sgt))
-		attach->priv = sgt;
+	if (!IS_ERR(mapping))
+		attach->priv = &mapping->sgt;
 
-	return sgt;
+	return mapping;
 }
 
 static void
 panfrost_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)
 {
 	attach->priv = NULL;
-	drm_gem_unmap_dma_buf(attach, sgt, dir);
+	drm_gem_unmap_dma_buf(attach, mapping, dir);
 }
 
 static int
@@ -330,7 +333,8 @@ panfrost_gem_prime_begin_cpu_access(struct dma_buf *dma_buf,
 		struct sg_table *sgt = attach->priv;
 
 		if (sgt)
-			dma_sync_sgtable_for_cpu(attach->dev, sgt, dir);
+			dma_sync_sgtable_for_cpu(dma_buf_sgt_dma_device(attach),
+						 sgt, dir);
 	}
 	dma_resv_unlock(obj->resv);
 
@@ -351,7 +355,8 @@ panfrost_gem_prime_end_cpu_access(struct dma_buf *dma_buf,
 		struct sg_table *sgt = attach->priv;
 
 		if (sgt)
-			dma_sync_sgtable_for_device(attach->dev, sgt, dir);
+			dma_sync_sgtable_for_device(dma_buf_sgt_dma_device(attach),
+						    sgt, dir);
 	}
 
 	if (shmem->vaddr)
@@ -367,14 +372,14 @@ panfrost_gem_prime_end_cpu_access(struct dma_buf *dma_buf,
 static const struct dma_buf_ops panfrost_dma_buf_ops = {
 	.attach = drm_gem_map_attach,
 	.detach = drm_gem_map_detach,
-	.map_dma_buf = panfrost_gem_prime_map_dma_buf,
-	.unmap_dma_buf = panfrost_gem_prime_unmap_dma_buf,
 	.release = drm_gem_dmabuf_release,
 	.mmap = drm_gem_dmabuf_mmap,
 	.vmap = drm_gem_dmabuf_vmap,
 	.vunmap = drm_gem_dmabuf_vunmap,
 	.begin_cpu_access = panfrost_gem_prime_begin_cpu_access,
 	.end_cpu_access = panfrost_gem_prime_end_cpu_access,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(panfrost_gem_prime_map_dma_buf,
+				     panfrost_gem_prime_unmap_dma_buf),
 };
 
 static struct dma_buf *
drivers/gpu/drm/panthor/panthor_gem.c +14 -11
index 22b57ab6f929..002fea6e5b06 100644
@@ -7,6 +7,7 @@
 #include <linux/cleanup.h>
 #include <linux/debugfs.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-mapping.h>
 #include <linux/err.h>
 #include <linux/slab.h>
@@ -471,25 +472,25 @@ static void panthor_gem_free_object(struct drm_gem_object *obj)
 	drm_gem_object_put(vm_root_gem);
 }
 
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
 panthor_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
 			      enum dma_data_direction dir)
 {
-	struct sg_table *sgt = drm_gem_map_dma_buf(attach, dir);
+	struct dma_buf_sgt_mapping *mapping = drm_gem_map_dma_buf(attach, dir);
 
-	if (!IS_ERR(sgt))
-		attach->priv = sgt;
+	if (!IS_ERR(mapping))
+		attach->priv = &mapping->sgt;
 
-	return sgt;
+	return mapping;
 }
 
 static void
 panthor_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)
 {
 	attach->priv = NULL;
-	drm_gem_unmap_dma_buf(attach, sgt, dir);
+	drm_gem_unmap_dma_buf(attach, mapping, dir);
 }
 
 static int
@@ -512,7 +513,8 @@ panthor_gem_prime_begin_cpu_access(struct dma_buf *dma_buf,
 		struct sg_table *sgt = attach->priv;
 
 		if (sgt)
-			dma_sync_sgtable_for_cpu(attach->dev, sgt, dir);
+			dma_sync_sgtable_for_cpu(dma_buf_sgt_dma_device(attach),
+						 sgt, dir);
 	}
 	dma_resv_unlock(obj->resv);
 
@@ -533,7 +535,8 @@ panthor_gem_prime_end_cpu_access(struct dma_buf *dma_buf,
 		struct sg_table *sgt = attach->priv;
 
 		if (sgt)
-			dma_sync_sgtable_for_device(attach->dev, sgt, dir);
+			dma_sync_sgtable_for_device(dma_buf_sgt_dma_device(attach),
+						    sgt, dir);
 	}
 
 	if (bo->cmap.vaddr)
@@ -549,14 +552,14 @@ panthor_gem_prime_end_cpu_access(struct dma_buf *dma_buf,
 static const struct dma_buf_ops panthor_dma_buf_ops = {
 	.attach = drm_gem_map_attach,
 	.detach = drm_gem_map_detach,
-	.map_dma_buf = panthor_gem_prime_map_dma_buf,
-	.unmap_dma_buf = panthor_gem_prime_unmap_dma_buf,
 	.release = drm_gem_dmabuf_release,
 	.mmap = drm_gem_dmabuf_mmap,
 	.vmap = drm_gem_dmabuf_vmap,
 	.vunmap = drm_gem_dmabuf_vunmap,
 	.begin_cpu_access = panthor_gem_prime_begin_cpu_access,
 	.end_cpu_access = panthor_gem_prime_end_cpu_access,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(panthor_gem_prime_map_dma_buf,
+				     panthor_gem_prime_unmap_dma_buf),
 };
 
 static struct dma_buf *
drivers/gpu/drm/virtio/virtgpu_drv.h +5 -4
index f3bbbe4468bf..35956816b6e2 100644
@@ -153,6 +153,7 @@ struct virtio_gpu_object_array {
 	struct drm_gem_object *objs[] __counted_by(total);
 };
 
+struct dma_buf_sgt_mapping;
 struct virtio_gpu_vbuffer;
 struct virtio_gpu_device;
 
@@ -555,11 +556,11 @@ bool virtio_gpu_is_vram(struct virtio_gpu_object *bo);
 int virtio_gpu_vram_create(struct virtio_gpu_device *vgdev,
 			   struct virtio_gpu_object_params *params,
 			   struct virtio_gpu_object **bo_ptr);
-struct sg_table *virtio_gpu_vram_map_dma_buf(struct virtio_gpu_object *bo,
-					     struct device *dev,
-					     enum dma_data_direction dir);
+struct dma_buf_sgt_mapping *
+virtio_gpu_vram_map_dma_buf(struct virtio_gpu_object *bo, struct device *dev,
+			    enum dma_data_direction dir);
 void virtio_gpu_vram_unmap_dma_buf(struct device *dev,
-				   struct sg_table *sgt,
+				   struct dma_buf_sgt_mapping *mapping,
 				   enum dma_data_direction dir);
 void virtio_gpu_vram_map_deferred(struct virtio_gpu_object_vram *vram);
 
drivers/gpu/drm/virtio/virtgpu_prime.c +14 -8
index a47783b47dcc..a01c70f4d6e8 100644
@@ -24,6 +24,7 @@
 
 #include <drm/drm_prime.h>
 #include <drm/drm_print.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/virtio_dma_buf.h>
 
 #include "virtgpu_drv.h"
@@ -46,44 +47,49 @@ static int virtgpu_virtio_get_uuid(struct dma_buf *buf,
 	return 0;
 }
 
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
 virtgpu_gem_map_dma_buf(struct dma_buf_attachment *attach,
 			enum dma_data_direction dir)
 {
 	struct drm_gem_object *obj = attach->dmabuf->priv;
 	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
 
-	if (virtio_gpu_is_vram(bo))
-		return virtio_gpu_vram_map_dma_buf(bo, attach->dev, dir);
+	if (virtio_gpu_is_vram(bo)) {
+		struct device *dev = dma_buf_sgt_dma_device(attach);
+
+		return virtio_gpu_vram_map_dma_buf(bo, dev, dir);
+	}
 
 	return drm_gem_map_dma_buf(attach, dir);
 }
 
 static void virtgpu_gem_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 *obj = attach->dmabuf->priv;
 	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
 
 	if (virtio_gpu_is_vram(bo)) {
-		virtio_gpu_vram_unmap_dma_buf(attach->dev, sgt, dir);
+		struct device *dev = dma_buf_sgt_mapping_dma_device(mapping);
+
+		virtio_gpu_vram_unmap_dma_buf(dev, mapping, dir);
 		return;
 	}
 
-	drm_gem_unmap_dma_buf(attach, sgt, dir);
+	drm_gem_unmap_dma_buf(attach, mapping, dir);
 }
 
 static const struct virtio_dma_buf_ops virtgpu_dmabuf_ops =  {
 	.ops = {
 		.attach = virtio_dma_buf_attach,
 		.detach = drm_gem_map_detach,
-		.map_dma_buf = virtgpu_gem_map_dma_buf,
-		.unmap_dma_buf = virtgpu_gem_unmap_dma_buf,
 		.release = drm_gem_dmabuf_release,
 		.mmap = drm_gem_dmabuf_mmap,
 		.vmap = drm_gem_dmabuf_vmap,
 		.vunmap = drm_gem_dmabuf_vunmap,
+		DMA_BUF_SIMPLE_SGT_EXP_MATCH(virtgpu_gem_map_dma_buf,
+					     virtgpu_gem_unmap_dma_buf),
 	},
 	.device_attach = drm_gem_map_attach,
 	.get_uuid = virtgpu_virtio_get_uuid,
drivers/gpu/drm/virtio/virtgpu_vram.c +15 -10
index 01241ce4d07c..0a430b04cc2f 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include "virtgpu_drv.h"
 
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-mapping.h>
 
 static DEFINE_MUTEX(map_lock);
@@ -77,19 +78,21 @@ static int virtio_gpu_vram_mmap(struct drm_gem_object *obj,
 	return ret;
 }
 
-struct sg_table *virtio_gpu_vram_map_dma_buf(struct virtio_gpu_object *bo,
-					     struct device *dev,
-					     enum dma_data_direction dir)
+struct dma_buf_sgt_mapping *
+virtio_gpu_vram_map_dma_buf(struct virtio_gpu_object *bo, struct device *dev,
+			    enum dma_data_direction dir)
 {
 	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
 	struct virtio_gpu_object_vram *vram = to_virtio_gpu_vram(bo);
+	struct dma_buf_sgt_mapping *mapping;
 	struct sg_table *sgt;
 	dma_addr_t addr;
 	int ret;
 
-	sgt = kzalloc_obj(*sgt);
-	if (!sgt)
+	mapping = kzalloc_obj(*mapping);
+	if (!mapping)
 		return ERR_PTR(-ENOMEM);
+	sgt = &mapping->sgt;
 
 	if (!(bo->blob_flags & VIRTGPU_BLOB_FLAG_USE_MAPPABLE)) {
 		// Virtio devices can access the dma-buf via its UUID. Return a stub
@@ -98,7 +101,7 @@ struct sg_table *virtio_gpu_vram_map_dma_buf(struct virtio_gpu_object *bo,
 			ret = -EIO;
 			goto out;
 		}
-		return sgt;
+		return mapping;
 	}
 
 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
@@ -116,24 +119,26 @@ struct sg_table *virtio_gpu_vram_map_dma_buf(struct virtio_gpu_object *bo,
 	sg_dma_address(sgt->sgl) = addr;
 	sg_dma_len(sgt->sgl) = vram->vram_node.size;
 
-	return sgt;
+	return mapping;
 out:
 	sg_free_table(sgt);
-	kfree(sgt);
+	kfree(mapping);
 	return ERR_PTR(ret);
 }
 
 void virtio_gpu_vram_unmap_dma_buf(struct device *dev,
-				   struct sg_table *sgt,
+				   struct dma_buf_sgt_mapping *mapping,
 				   enum dma_data_direction dir)
 {
+	struct sg_table *sgt = &mapping->sgt;
+
 	if (sgt->nents) {
 		dma_unmap_resource(dev, sg_dma_address(sgt->sgl),
 				   sg_dma_len(sgt->sgl), dir,
 				   DMA_ATTR_SKIP_CPU_SYNC);
 	}
 	sg_free_table(sgt);
-	kfree(sgt);
+	kfree(mapping);
 }
 
 static const struct drm_gem_object_funcs virtio_gpu_vram_funcs = {
include/drm/drm_prime.h +5 -3
index f50f862f0d8b..4fceac922a7a 100644
@@ -54,6 +54,7 @@ struct device;
 struct dma_buf_export_info;
 struct dma_buf;
 struct dma_buf_attachment;
+struct dma_buf_sgt_mapping;
 struct iosys_map;
 
 enum dma_data_direction;
@@ -81,10 +82,11 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 		       struct dma_buf_attachment *attach);
 void drm_gem_map_detach(struct dma_buf *dma_buf,
 			struct dma_buf_attachment *attach);
-struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
-				     enum dma_data_direction dir);
+struct dma_buf_sgt_mapping *
+drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
+		    enum dma_data_direction dir);
 void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
-			   struct sg_table *sgt,
+			   struct dma_buf_sgt_mapping *mapping,
 			   enum dma_data_direction dir);
 int drm_gem_dmabuf_vmap(struct dma_buf *dma_buf, struct iosys_map *map);
 void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, struct iosys_map *map);
Signature · 1 line
2.54.0
#12 lore raw

PATCH v3 11/24 drm/xe/dma-buf: Use the SGT mapping type

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

Move xe's map and unmap callbacks to the SGT exporter ops through DMA_BUF_SIMPLE_SGT_EXP_MATCH() and take the importing device from the match entry with dma_buf_sgt_dma_device().

The SGT exporter ops return a struct dma_buf_sgt_mapping, so xe_dma_buf_map() must allocate one and build the table in it. Doing so requires the TT path to take over the table entries drm_prime_pages_to_sg() returns, while xe_ttm_vram_mgr_alloc_sgt() and xe_ttm_vram_mgr_free_sgt() now take caller provided tables instead of allocating and freeing themselves.

---
 drivers/gpu/drm/xe/xe_dma_buf.c      | 62 ++++++++++++++++++++++++------------
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 22 ++++---------
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.h |  2 +-
 3 files changed, 49 insertions(+), 37 deletions(-)
drivers/gpu/drm/xe/xe_dma_buf.c +41 -21
index 6a85b292dee7..2394330e93bb 100644
@@ -7,6 +7,7 @@
 
 #include <kunit/test.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dynamic_debug.h>
 #include <linux/pci-p2pdma.h>
 
@@ -45,7 +46,9 @@ static int xe_dma_buf_attach(struct dma_buf *dmabuf,
 	xe_dma_buf_p2p_debug_flip(&verbose_p2p_dist);
 
 	if (attach->peer2peer &&
-	    pci_p2pdma_distance(to_pci_dev(obj->dev->dev), attach->dev, verbose_p2p_dist) < 0)
+	    pci_p2pdma_distance(to_pci_dev(obj->dev->dev),
+				dma_buf_sgt_dma_device(attach),
+				verbose_p2p_dist) < 0)
 		attach->peer2peer = false;
 
 	if (!attach->peer2peer && !xe_bo_can_migrate(gem_to_xe_bo(obj), XE_PL_TT))
@@ -111,13 +114,15 @@ static void xe_dma_buf_unpin(struct dma_buf_attachment *attach)
 	xe_bo_unpin_external(bo);
 }
 
-static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
-				       enum dma_data_direction dir)
+static struct dma_buf_sgt_mapping *
+xe_dma_buf_map(struct dma_buf_attachment *attach, enum dma_data_direction dir)
 {
+	struct device *dma_dev = dma_buf_sgt_dma_device(attach);
 	struct dma_buf *dma_buf = attach->dmabuf;
 	struct drm_gem_object *obj = dma_buf->priv;
 	struct xe_bo *bo = gem_to_xe_bo(obj);
 	struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED;
+	struct dma_buf_sgt_mapping *mapping;
 	struct sg_table *sgt;
 	int r = 0;
 
@@ -136,51 +141,67 @@ static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach,
 			return ERR_PTR(r);
 	}
 
+	mapping = kzalloc_obj(*mapping);
+	if (!mapping)
+		return ERR_PTR(-ENOMEM);
+
 	switch (bo->ttm.resource->mem_type) {
 	case XE_PL_TT:
 		sgt = drm_prime_pages_to_sg(obj->dev,
 					    bo->ttm.ttm->pages,
 					    obj->size >> PAGE_SHIFT);
-		if (IS_ERR(sgt))
-			return sgt;
+		if (IS_ERR(sgt)) {
+			r = PTR_ERR(sgt);
+			goto error_free_mapping;
+		}
+		/* The mapping takes over the table's entries */
+		mapping->sgt = *sgt;
+		kfree(sgt);
 
-		if (dma_map_sgtable(attach->dev, sgt, dir,
-				    DMA_ATTR_SKIP_CPU_SYNC))
+		if (dma_map_sgtable(dma_dev, &mapping->sgt, dir,
+				    DMA_ATTR_SKIP_CPU_SYNC)) {
+			r = -EBUSY;
 			goto error_free;
+		}
 		break;
 
 	case XE_PL_VRAM0:
 	case XE_PL_VRAM1:
 		r = xe_ttm_vram_mgr_alloc_sgt(xe_bo_device(bo),
 					      bo->ttm.resource, 0,
-					      bo->ttm.base.size, attach->dev,
-					      dir, &sgt);
+					      bo->ttm.base.size, dma_dev,
+					      dir, &mapping->sgt);
 		if (r)
-			return ERR_PTR(r);
+			goto error_free_mapping;
 		break;
 	default:
-		return ERR_PTR(-EINVAL);
+		r = -EINVAL;
+		goto error_free_mapping;
 	}
 
-	return sgt;
+	return mapping;
 
 error_free:
-	sg_free_table(sgt);
-	kfree(sgt);
-	return ERR_PTR(-EBUSY);
+	sg_free_table(&mapping->sgt);
+error_free_mapping:
+	kfree(mapping);
+	return ERR_PTR(r);
 }
 
 static void xe_dma_buf_unmap(struct dma_buf_attachment *attach,
-			     struct sg_table *sgt,
+			     struct dma_buf_sgt_mapping *mapping,
 			     enum dma_data_direction dir)
 {
+	struct device *dma_dev = dma_buf_sgt_mapping_dma_device(mapping);
+	struct sg_table *sgt = &mapping->sgt;
+
 	if (sg_page(sgt->sgl)) {
-		dma_unmap_sgtable(attach->dev, sgt, dir, 0);
+		dma_unmap_sgtable(dma_dev, sgt, dir, 0);
 		sg_free_table(sgt);
-		kfree(sgt);
 	} else {
-		xe_ttm_vram_mgr_free_sgt(attach->dev, dir, sgt);
+		xe_ttm_vram_mgr_free_sgt(dma_dev, dir, sgt);
 	}
+	kfree(mapping);
 }
 
 static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
@@ -230,13 +251,12 @@ static const struct dma_buf_ops xe_dmabuf_ops = {
 	.detach = xe_dma_buf_detach,
 	.pin = xe_dma_buf_pin,
 	.unpin = xe_dma_buf_unpin,
-	.map_dma_buf = xe_dma_buf_map,
-	.unmap_dma_buf = xe_dma_buf_unmap,
 	.release = xe_dma_buf_release,
 	.begin_cpu_access = xe_dma_buf_begin_cpu_access,
 	.mmap = drm_gem_dmabuf_mmap,
 	.vmap = drm_gem_dmabuf_vmap,
 	.vunmap = drm_gem_dmabuf_vunmap,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(xe_dma_buf_map, xe_dma_buf_unmap),
 };
 
 struct dma_buf *xe_gem_prime_export(struct drm_gem_object *obj, int flags)
drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +7 -15
index 9a514d983e90..f5801e81e080 100644
@@ -505,7 +505,7 @@ int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
 			      u64 offset, u64 length,
 			      struct device *dev,
 			      enum dma_data_direction dir,
-			      struct sg_table **sgt)
+			      struct sg_table *sgt)
 {
 	struct xe_tile *tile = &xe->tiles[res->mem_type - XE_PL_VRAM0];
 	struct xe_ttm_vram_mgr_resource *vres = to_xe_ttm_vram_mgr_resource(res);
@@ -517,10 +517,6 @@ int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
 	if (vres->used_visible_size < res->size)
 		return -EOPNOTSUPP;
 
-	*sgt = kmalloc_obj(**sgt);
-	if (!*sgt)
-		return -ENOMEM;
-
 	/* Determine the number of GPU_BUDDY blocks to export */
 	xe_res_first(res, offset, length, &cursor);
 	while (cursor.remaining) {
@@ -529,12 +525,12 @@ int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
 		xe_res_next(&cursor, min_t(u64, cursor.size, SZ_2G));
 	}
 
-	r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL);
+	r = sg_alloc_table(sgt, num_entries, GFP_KERNEL);
 	if (r)
-		goto error_free;
+		return r;
 
 	/* Initialize scatterlist nodes of sg_table */
-	for_each_sgtable_sg((*sgt), sg, i)
+	for_each_sgtable_sg(sgt, sg, i)
 		sg->length = 0;
 
 	/*
@@ -544,7 +540,7 @@ int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
 	 * GPU_BUDDY block(s) if more buffer needs to exported
 	 */
 	xe_res_first(res, offset, length, &cursor);
-	for_each_sgtable_sg((*sgt), sg, i) {
+	for_each_sgtable_sg(sgt, sg, i) {
 		phys_addr_t phys = cursor.start + xe_vram_region_io_start(tile->mem.vram);
 		size_t size = min_t(u64, cursor.size, SZ_2G);
 		dma_addr_t addr;
@@ -565,7 +561,7 @@ int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
 	return 0;
 
 error_unmap:
-	for_each_sgtable_sg((*sgt), sg, i) {
+	for_each_sgtable_sg(sgt, sg, i) {
 		if (!sg->length)
 			continue;
 
@@ -573,10 +569,7 @@ int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
 				   sg->length, dir,
 				   DMA_ATTR_SKIP_CPU_SYNC);
 	}
-	sg_free_table(*sgt);
-
-error_free:
-	kfree(*sgt);
+	sg_free_table(sgt);
 	return r;
 }
 
@@ -591,7 +584,6 @@ void xe_ttm_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir,
 				   sg->length, dir,
 				   DMA_ATTR_SKIP_CPU_SYNC);
 	sg_free_table(sgt);
-	kfree(sgt);
 }
 
 u64 xe_ttm_vram_get_cpu_visible_size(struct ttm_resource_manager *man)
drivers/gpu/drm/xe/xe_ttm_vram_mgr.h +1 -1
index d77f067d197b..d6531f0e0317 100644
@@ -23,7 +23,7 @@ int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
 			      u64 offset, u64 length,
 			      struct device *dev,
 			      enum dma_data_direction dir,
-			      struct sg_table **sgt);
+			      struct sg_table *sgt);
 void xe_ttm_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir,
 			      struct sg_table *sgt);
 
Signature · 1 line
2.54.0
#13 lore raw

PATCH v3 12/24 drm/amdgpu: Use the SGT mapping type

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

Move amdgpu's map and unmap callbacks to the SGT exporter ops through DMA_BUF_SIMPLE_SGT_EXP_MATCH() and take the importing device from the match entry with dma_buf_sgt_dma_device().

The SGT exporter ops return a struct dma_buf_sgt_mapping, so amdgpu_dma_buf_map() must allocate one and build the table in it. The GTT path takes over the entries of the table drm_prime_pages_to_sg() returns, and amdgpu_vram_mgr_alloc_sgt(), amdgpu_ttm_mmio_remap_alloc_sgt() and their free counterparts fill and tear down a caller-provided table instead of doing allocation and free themselves.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c  | 85 +++++++++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c      | 25 +++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h      |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 30 ++++------
 4 files changed, 74 insertions(+), 70 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +52 -33
index bbe518757907..7b110405b08d 100644
@@ -41,6 +41,7 @@
 #include <drm/amdgpu_drm.h>
 #include <drm/ttm/ttm_tt.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-fence-array.h>
 #include <linux/pci-p2pdma.h>
 #include <linux/pm_runtime.h>
@@ -98,7 +99,8 @@ static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
 		attach->peer2peer = false;
 
 	if (!amdgpu_dmabuf_is_xgmi_accessible(attach_adev, bo) &&
-	    pci_p2pdma_distance(adev->pdev, attach->dev, false) < 0)
+	    pci_p2pdma_distance(adev->pdev, dma_buf_sgt_dma_device(attach),
+				false) < 0)
 		attach->peer2peer = false;
 
 	/*
@@ -203,7 +205,7 @@ static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach)
 }
 
 /**
- * amdgpu_dma_buf_map - &dma_buf_ops.map_dma_buf implementation
+ * amdgpu_dma_buf_map - &dma_buf_mapping_sgt_exp_ops.map_dma_buf implementation
  * @attach: DMA-buf attachment
  * @dir: DMA direction
  *
@@ -215,13 +217,16 @@ static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach)
  * sg_table filled with the DMA addresses to use or ERR_PRT with negative error
  * code.
  */
-static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
-					   enum dma_data_direction dir)
+static struct dma_buf_sgt_mapping *
+amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
+		   enum dma_data_direction dir)
 {
+	struct device *dma_dev = dma_buf_sgt_dma_device(attach);
 	struct dma_buf *dma_buf = attach->dmabuf;
 	struct drm_gem_object *obj = dma_buf->priv;
 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
+	struct dma_buf_sgt_mapping *mapping;
 	struct sg_table *sgt;
 	long r;
 
@@ -241,80 +246,95 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach,
 			return ERR_PTR(r);
 	}
 
+	mapping = kzalloc_obj(*mapping);
+	if (!mapping)
+		return ERR_PTR(-ENOMEM);
+
 	switch (bo->tbo.resource->mem_type) {
 	case TTM_PL_TT:
 		sgt = drm_prime_pages_to_sg(obj->dev,
 					    bo->tbo.ttm->pages,
 					    bo->tbo.ttm->num_pages);
-		if (IS_ERR(sgt))
-			return sgt;
+		if (IS_ERR(sgt)) {
+			r = PTR_ERR(sgt);
+			goto error_free_mapping;
+		}
+		/* The mapping takes over the table's entries */
+		mapping->sgt = *sgt;
+		kfree(sgt);
 
-		if (dma_map_sgtable(attach->dev, sgt, dir,
-				    DMA_ATTR_SKIP_CPU_SYNC))
+		if (dma_map_sgtable(dma_dev, &mapping->sgt, dir,
+				    DMA_ATTR_SKIP_CPU_SYNC)) {
+			r = -EBUSY;
 			goto error_free;
+		}
 		break;
 
 	case TTM_PL_VRAM:
 		/* XGMI-accessible memory should never be DMA-mapped */
 		if (WARN_ON(amdgpu_dmabuf_is_xgmi_accessible(
-				dma_buf_attach_adev(attach), bo)))
-			return ERR_PTR(-EINVAL);
+				dma_buf_attach_adev(attach), bo))) {
+			r = -EINVAL;
+			goto error_free_mapping;
+		}
 
 		r = amdgpu_vram_mgr_alloc_sgt(adev, bo->tbo.resource, 0,
-					      bo->tbo.base.size, attach->dev,
-					      dir, &sgt);
+					      bo->tbo.base.size, dma_dev,
+					      dir, &mapping->sgt);
 		if (r)
-			return ERR_PTR(r);
+			goto error_free_mapping;
 		break;
 
 	case AMDGPU_PL_MMIO_REMAP:
 		r = amdgpu_ttm_mmio_remap_alloc_sgt(adev, bo->tbo.resource,
-						    attach->dev, dir, &sgt);
+						    dma_dev, dir,
+						    &mapping->sgt);
 		if (r)
-			return ERR_PTR(r);
+			goto error_free_mapping;
 		break;
 
 	default:
-		return ERR_PTR(-EINVAL);
+		r = -EINVAL;
+		goto error_free_mapping;
 	}
 
-	return sgt;
+	return mapping;
 
 error_free:
-	sg_free_table(sgt);
-	kfree(sgt);
-	return ERR_PTR(-EBUSY);
+	sg_free_table(&mapping->sgt);
+error_free_mapping:
+	kfree(mapping);
+	return ERR_PTR(r);
 }
 
 /**
- * amdgpu_dma_buf_unmap - &dma_buf_ops.unmap_dma_buf implementation
+ * amdgpu_dma_buf_unmap - &dma_buf_mapping_sgt_exp_ops.unmap_dma_buf implementation
  * @attach: DMA-buf attachment
- * @sgt: sg_table to unmap
+ * @mapping: mapping to unmap
  * @dir: DMA direction
  *
  * This is called when a shared DMA buffer no longer needs to be accessible by
  * another device. For now, simply unpins the buffer from GTT.
  */
 static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach,
-				 struct sg_table *sgt,
+				 struct dma_buf_sgt_mapping *mapping,
 				 enum dma_data_direction dir)
 {
+	struct device *dma_dev = dma_buf_sgt_mapping_dma_device(mapping);
 	struct drm_gem_object *obj = attach->dmabuf->priv;
 	struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+	struct sg_table *sgt = &mapping->sgt;
 
 	if (bo->tbo.resource &&
 	    bo->tbo.resource->mem_type == AMDGPU_PL_MMIO_REMAP) {
-		amdgpu_ttm_mmio_remap_free_sgt(attach->dev, dir, sgt);
-		return;
-	}
-
-	if (sg_page(sgt->sgl)) {
-		dma_unmap_sgtable(attach->dev, sgt, dir, 0);
+		amdgpu_ttm_mmio_remap_free_sgt(dma_dev, dir, sgt);
+	} else if (sg_page(sgt->sgl)) {
+		dma_unmap_sgtable(dma_dev, sgt, dir, 0);
 		sg_free_table(sgt);
-		kfree(sgt);
 	} else {
-		amdgpu_vram_mgr_free_sgt(attach->dev, dir, sgt);
+		amdgpu_vram_mgr_free_sgt(dma_dev, dir, sgt);
 	}
+	kfree(mapping);
 }
 
 /**
@@ -393,13 +413,12 @@ const struct dma_buf_ops amdgpu_dmabuf_ops = {
 	.detach = amdgpu_dma_buf_detach,
 	.pin = amdgpu_dma_buf_pin,
 	.unpin = amdgpu_dma_buf_unpin,
-	.map_dma_buf = amdgpu_dma_buf_map,
-	.unmap_dma_buf = amdgpu_dma_buf_unmap,
 	.release = drm_gem_dmabuf_release,
 	.begin_cpu_access = amdgpu_dma_buf_begin_cpu_access,
 	.mmap = drm_gem_dmabuf_mmap,
 	.vmap = amdgpu_dma_buf_vmap,
 	.vunmap = amdgpu_dma_buf_vunmap,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(amdgpu_dma_buf_map, amdgpu_dma_buf_unmap),
 };
 
 /**
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -16
index 30ec5cb9ddf5..9104b36f6184 100644
@@ -1114,9 +1114,9 @@ static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev,
  * amdgpu_ttm_mmio_remap_alloc_sgt - build an sg_table for MMIO_REMAP I/O aperture
  * @adev: amdgpu device providing the remap BAR base (adev->rmmio_remap.bus_addr)
  * @res:  TTM resource of the BO to export; expected to live in AMDGPU_PL_MMIO_REMAP
- * @dev:  importing device to map for (typically @attach->dev in dma-buf paths)
+ * @dev:  importing device to map for (dma_buf_sgt_dma_device() in dma-buf paths)
  * @dir:  DMA data direction for the importer (passed to dma_map_resource())
- * @sgt:  output; on success, set to a newly allocated sg_table describing the I/O span
+ * @sgt:  sg_table to fill with the I/O span
  *
  * The HDP flush page (AMDGPU_PL_MMIO_REMAP) is a fixed hardware I/O window in a PCI
  * BAR—there are no struct pages to back it. Importers still need a DMA address list,
@@ -1131,9 +1131,9 @@ static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev,
  * - Peer reachability / p2pdma policy checks must be done by the caller.
  *
  * Return:
- * * 0 on success, with *@sgt set to a valid table that must be freed via
+ * * 0 on success, with @sgt filled; tear it down via
  *   amdgpu_ttm_mmio_remap_free_sgt().
- * * -ENOMEM if allocation of the sg_table fails.
+ * * -ENOMEM if allocation of the sg_table entries fails.
  * * -EIO if dma_map_resource() fails.
  *
  */
@@ -1141,7 +1141,7 @@ int amdgpu_ttm_mmio_remap_alloc_sgt(struct amdgpu_device *adev,
 				    struct ttm_resource *res,
 				    struct device *dev,
 				    enum dma_data_direction dir,
-				    struct sg_table **sgt)
+				    struct sg_table *sgt)
 {
 	struct amdgpu_res_cursor cur;
 	dma_addr_t dma;
@@ -1156,21 +1156,15 @@ int amdgpu_ttm_mmio_remap_alloc_sgt(struct amdgpu_device *adev,
 	phys = adev->rmmio_remap.bus_addr + cur.start;
 
 	/* Build a single-entry sg_table mapped as I/O (no struct page backing). */
-	*sgt = kzalloc_obj(**sgt);
-	if (!*sgt)
-		return -ENOMEM;
-	r = sg_alloc_table(*sgt, 1, GFP_KERNEL);
-	if (r) {
-		kfree(*sgt);
+	r = sg_alloc_table(sgt, 1, GFP_KERNEL);
+	if (r)
 		return r;
-	}
-	sg = (*sgt)->sgl;
+	sg = sgt->sgl;
 	sg_set_page(sg, NULL, cur.size, 0);  /* WHY: I/O space → no pages */
 
 	dma = dma_map_resource(dev, phys, cur.size, dir, DMA_ATTR_SKIP_CPU_SYNC);
 	if (dma_mapping_error(dev, dma)) {
-		sg_free_table(*sgt);
-		kfree(*sgt);
+		sg_free_table(sgt);
 		return -EIO;
 	}
 	sg_dma_address(sg) = dma;
@@ -1187,7 +1181,6 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 	dma_unmap_resource(dev, sg_dma_address(sg), sg_dma_len(sg),
 			   dir, DMA_ATTR_SKIP_CPU_SYNC);
 	sg_free_table(sgt);
-	kfree(sgt);
 }
 
 /**
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +2 -2
index adb6cfae5052..c20db985137a 100644
@@ -164,7 +164,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
 			      u64 offset, u64 size,
 			      struct device *dev,
 			      enum dma_data_direction dir,
-			      struct sg_table **sgt);
+			      struct sg_table *sgt);
 void amdgpu_vram_mgr_free_sgt(struct device *dev,
 			      enum dma_data_direction dir,
 			      struct sg_table *sgt);
@@ -266,7 +266,7 @@ int amdgpu_ttm_mmio_remap_alloc_sgt(struct amdgpu_device *adev,
 				    struct ttm_resource *res,
 				    struct device *dev,
 				    enum dma_data_direction dir,
-				    struct sg_table **sgt);
+				    struct sg_table *sgt);
 void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 				    enum dma_data_direction dir,
 				    struct sg_table *sgt);
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +11 -19
index 249f07f30951..3d245467b983 100644
@@ -671,26 +671,22 @@ static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man,
  * @length: number of bytes to export in sg_table
  * @dev: the other device
  * @dir: dma direction
- * @sgt: resulting sg table
+ * @sgt: sg table to fill
  *
- * Allocate and fill a sg table from a VRAM allocation.
+ * Fill a sg table from a VRAM allocation.
  */
 int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
 			      struct ttm_resource *res,
 			      u64 offset, u64 length,
 			      struct device *dev,
 			      enum dma_data_direction dir,
-			      struct sg_table **sgt)
+			      struct sg_table *sgt)
 {
 	struct amdgpu_res_cursor cursor;
 	struct scatterlist *sg;
 	int num_entries = 0;
 	int i, r;
 
-	*sgt = kmalloc_obj(**sgt);
-	if (!*sgt)
-		return -ENOMEM;
-
 	/* Determine the number of GPU_BUDDY blocks to export */
 	amdgpu_res_first(res, offset, length, &cursor);
 	while (cursor.remaining) {
@@ -698,12 +694,12 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
 		amdgpu_res_next(&cursor, min(cursor.size, AMDGPU_MAX_SG_SEGMENT_SIZE));
 	}
 
-	r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL);
+	r = sg_alloc_table(sgt, num_entries, GFP_KERNEL);
 	if (r)
-		goto error_free;
+		return r;
 
 	/* Initialize scatterlist nodes of sg_table */
-	for_each_sgtable_sg((*sgt), sg, i)
+	for_each_sgtable_sg(sgt, sg, i)
 		sg->length = 0;
 
 	/*
@@ -713,7 +709,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
 	 * GPU_BUDDY block(s) if more buffer needs to exported
 	 */
 	amdgpu_res_first(res, offset, length, &cursor);
-	for_each_sgtable_sg((*sgt), sg, i) {
+	for_each_sgtable_sg(sgt, sg, i) {
 		phys_addr_t phys = cursor.start + adev->gmc.aper_base;
 		unsigned long size = min(cursor.size, AMDGPU_MAX_SG_SEGMENT_SIZE);
 		dma_addr_t addr;
@@ -734,7 +730,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
 	return 0;
 
 error_unmap:
-	for_each_sgtable_sg((*sgt), sg, i) {
+	for_each_sgtable_sg(sgt, sg, i) {
 		if (!sg->length)
 			continue;
 
@@ -742,10 +738,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
 				   sg->length, dir,
 				   DMA_ATTR_SKIP_CPU_SYNC);
 	}
-	sg_free_table(*sgt);
-
-error_free:
-	kfree(*sgt);
+	sg_free_table(sgt);
 	return r;
 }
 
@@ -754,9 +747,9 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev,
  *
  * @dev: device pointer
  * @dir: data direction of resource to unmap
- * @sgt: sg table to free
+ * @sgt: sg table to tear down
  *
- * Free a previously allocate sg table.
+ * Unmap and free the entries of a table filled by amdgpu_vram_mgr_alloc_sgt().
  */
 void amdgpu_vram_mgr_free_sgt(struct device *dev,
 			      enum dma_data_direction dir,
@@ -770,7 +763,6 @@ void amdgpu_vram_mgr_free_sgt(struct device *dev,
 				   sg->length, dir,
 				   DMA_ATTR_SKIP_CPU_SYNC);
 	sg_free_table(sgt);
-	kfree(sgt);
 }
 
 /**
Signature · 1 line
2.54.0
#14 lore raw

PATCH v3 13/24 udmabuf: Convert to the SGT mapping type

In reply to Alistair Popple

udmabuf's get_sg_table() and put_sg_table() allocate and free the struct sg_table they build and tear down. Both the dma-buf map path and the CPU access path use the table kept in ubuf->sg. Make them work on a caller-provided table by having map_udmabuf() pass a table embedded in a struct dma_buf_sgt_mapping map_udmabuf() allocates instead.

---
 drivers/dma-buf/udmabuf.c | 71 ++++++++++++++++++++++++++---------------------
 1 file changed, 40 insertions(+), 31 deletions(-)
drivers/dma-buf/udmabuf.c +40 -31
index df6dd0046242..ef77ae063ae4 100644
@@ -2,6 +2,7 @@
 #include <linux/cred.h>
 #include <linux/device.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-resv.h>
 #include <linux/highmem.h>
 #include <linux/init.h>
@@ -128,33 +129,22 @@ static void vunmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
 	vm_unmap_ram(map->vaddr, ubuf->pagecount);
 }
 
-static struct sg_table *get_sg_table(struct device *dev, struct dma_buf *buf,
-				     enum dma_data_direction direction)
+static int get_sg_table(struct device *dev, struct dma_buf *buf,
+			struct sg_table *sg, enum dma_data_direction direction)
 {
 	struct udmabuf *ubuf = buf->priv;
-	struct sg_table *sg;
 	int ret;
 
-	sg = kzalloc_obj(*sg);
-	if (!sg)
-		return ERR_PTR(-ENOMEM);
-
 	ret = sg_alloc_table_from_pages(sg, ubuf->pages, ubuf->pagecount, 0,
 					ubuf->pagecount << PAGE_SHIFT,
 					GFP_KERNEL);
 	if (ret < 0)
-		goto err_alloc;
+		return ret;
 
 	ret = dma_map_sgtable(dev, sg, direction, DMA_ATTR_SKIP_CPU_SYNC);
 	if (ret < 0)
-		goto err_map;
-	return sg;
-
-err_map:
-	sg_free_table(sg);
-err_alloc:
-	kfree(sg);
-	return ERR_PTR(ret);
+		sg_free_table(sg);
+	return ret;
 }
 
 static void put_sg_table(struct device *dev, struct sg_table *sg,
@@ -162,20 +152,35 @@ static void put_sg_table(struct device *dev, struct sg_table *sg,
 {
 	dma_unmap_sgtable(dev, sg, direction, DMA_ATTR_SKIP_CPU_SYNC);
 	sg_free_table(sg);
-	kfree(sg);
 }
 
-static struct sg_table *map_udmabuf(struct dma_buf_attachment *at,
-				    enum dma_data_direction direction)
+static struct dma_buf_sgt_mapping *map_udmabuf(struct dma_buf_attachment *at,
+					       enum dma_data_direction direction)
 {
-	return get_sg_table(at->dev, at->dmabuf, direction);
+	struct dma_buf_sgt_mapping *mapping;
+	int ret;
+
+	mapping = kzalloc_obj(*mapping);
+	if (!mapping)
+		return ERR_PTR(-ENOMEM);
+
+	ret = get_sg_table(dma_buf_sgt_dma_device(at), at->dmabuf,
+			   &mapping->sgt, direction);
+	if (ret) {
+		kfree(mapping);
+		return ERR_PTR(ret);
+	}
+
+	return mapping;
 }
 
 static void unmap_udmabuf(struct dma_buf_attachment *at,
-			  struct sg_table *sg,
+			  struct dma_buf_sgt_mapping *mapping,
 			  enum dma_data_direction direction)
 {
-	return put_sg_table(at->dev, sg, direction);
+	put_sg_table(dma_buf_sgt_mapping_dma_device(mapping), &mapping->sgt,
+		     direction);
+	kfree(mapping);
 }
 
 static void unpin_all_folios(struct udmabuf *ubuf)
@@ -212,8 +217,10 @@ static void release_udmabuf(struct dma_buf *buf)
 	struct udmabuf *ubuf = buf->priv;
 	struct device *dev = ubuf->device->this_device;
 
-	if (ubuf->sg)
+	if (ubuf->sg) {
 		put_sg_table(dev, ubuf->sg, ubuf->sg_dir);
+		kfree(ubuf->sg);
+	}
 
 	deinit_udmabuf(ubuf);
 	kfree(ubuf);
@@ -226,16 +233,19 @@ static int begin_cpu_udmabuf(struct dma_buf *buf,
 	struct device *dev = ubuf->device->this_device;
 
 	if (!ubuf->sg) {
-		ubuf->sg = get_sg_table(dev, buf, direction);
-		if (IS_ERR(ubuf->sg)) {
-			int ret;
+		int ret;
+
+		ubuf->sg = kzalloc_obj(*ubuf->sg);
+		if (!ubuf->sg)
+			return -ENOMEM;
 
-			ret = PTR_ERR(ubuf->sg);
+		ret = get_sg_table(dev, buf, ubuf->sg, direction);
+		if (ret) {
+			kfree(ubuf->sg);
 			ubuf->sg = NULL;
 			return ret;
-		} else {
-			ubuf->sg_dir = direction;
 		}
+		ubuf->sg_dir = direction;
 	}
 
 	dma_sync_sgtable_for_cpu(dev, ubuf->sg, direction);
@@ -256,14 +266,13 @@ static int end_cpu_udmabuf(struct dma_buf *buf,
 }
 
 static const struct dma_buf_ops udmabuf_ops = {
-	.map_dma_buf	   = map_udmabuf,
-	.unmap_dma_buf	   = unmap_udmabuf,
 	.release	   = release_udmabuf,
 	.mmap		   = mmap_udmabuf,
 	.vmap		   = vmap_udmabuf,
 	.vunmap		   = vunmap_udmabuf,
 	.begin_cpu_access  = begin_cpu_udmabuf,
 	.end_cpu_access    = end_cpu_udmabuf,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(map_udmabuf, unmap_udmabuf),
 };
 
 #define SEALS_WANTED (F_SEAL_SHRINK)
Signature · 1 line
2.54.0
#15 lore raw

Re: [PATCH v3 13/24] udmabuf: Convert to the SGT mapping type

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] udmabuf: premature publication of ubuf->sg in begin_cpu_udmabuf() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] udmabuf: premature publication of ubuf->sg in begin_cpu_udmabuf()
--

commit f138ffdf3b226fef46f9eda6001d750e3e320cdd
Author: Alistair Popple <[email protected]>

udmabuf: Convert to the SGT mapping type

This commit converts udmabuf to the SGT mapping type by having get_sg_table() and put_sg_table() operate on a caller-allocated struct sg_table and embedding the DMA-buf mapping table in struct dma_buf_sgt_mapping.

Quoted 4 lines
drivers/dma-buf/udmabuf.c +0 -0
index df6dd00462423..ef77ae063ae4c 100644

[ ... ]

Quoted 16 lines
drivers/dma-buf/udmabuf.c +7 -4
@@ -226,16 +233,19 @@ static int begin_cpu_udmabuf(struct dma_buf *buf,
 	struct device *dev = ubuf->device->this_device;
 
 	if (!ubuf->sg) {
-		ubuf->sg = get_sg_table(dev, buf, direction);
-		if (IS_ERR(ubuf->sg)) {
-			int ret;
+		int ret;
+
+		ubuf->sg = kzalloc_obj(*ubuf->sg);
+		if (!ubuf->sg)
+			return -ENOMEM;
 
-			ret = PTR_ERR(ubuf->sg);
+		ret = get_sg_table(dev, buf, ubuf->sg, direction);
+		if (ret) {

drivers/dma-buf/udmabuf.c static int begin_cpu_udmabuf(struct dma_buf *buf, · line 251

Quoted 10 lines
drivers/dma-buf/udmabuf.c +2 -2
+			kfree(ubuf->sg);
 			ubuf->sg = NULL;
 			return ret;
-		} else {
-			ubuf->sg_dir = direction;
 		}
+		ubuf->sg_dir = direction;
 	}
 
 	dma_sync_sgtable_for_cpu(dev, ubuf->sg, direction);

[Severity: High]
Can assigning ubuf->sg before get_sg_table() finishes expose an uninitialized, unmapped, or freed sg_table to concurrent DMA_BUF_IOCTL_SYNC callers?

dma_buf_ioctl() invokes begin_cpu_udmabuf() and end_cpu_udmabuf() without holding a lock. Previously, get_sg_table() allocated, populated, and DMA-mapped a local struct sg_table before returning it to be stored in ubuf->sg. With ubuf->sg assigned immediately after kzalloc_obj(), a second thread can observe a non-NULL ubuf->sg while the first thread is still inside get_sg_table() (or after kfree(ubuf->sg) on the error path):

CPU0 CPU1
begin_cpu_udmabuf()
ubuf->sg = kzalloc_obj(...)
get_sg_table(..., ubuf->sg, ...)

    sg_alloc_table_from_pages()
                                      begin_cpu_udmabuf()
                                        if (!ubuf->sg) /* false */
                                        dma_sync_sgtable_for_cpu(...,
                                                                 ubuf->sg,
                                                                 ...)
                                          /* uninitialized or freed sgt */

If two threads both enter the !ubuf->sg block, the second kzalloc_obj() also overwrites ubuf->sg while the first thread passes ubuf->sg to get_sg_table(), leaking the first allocation and initializing the same struct sg_table concurrently.

Allocating into a local struct sg_table *sg and assigning ubuf->sg only after get_sg_table() succeeds avoids publishing the table prematurely.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#16 lore raw

PATCH v3 14/24 drm/omapdrm: Convert to the SGT mapping type

In reply to Alistair Popple

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().

---
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c | 47 ++++++++++++++++++++++++-------
 1 file changed, 37 insertions(+), 10 deletions(-)
drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c +37 -10
index afae0a56da8c..1c03f892dac9 100644
@@ -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)
Signature · 1 line
2.54.0
#17 lore raw

PATCH v3 15/24 accel/ivpu: Convert the userptr exporter to the SGT mapping type

In reply to Alistair Popple

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.

---
 drivers/accel/ivpu/ivpu_gem_userptr.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/accel/ivpu/ivpu_gem_userptr.c +21 -8
index 7cbf3a4cdc73..35e66b84a64b 100644
@@ -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 *
Signature · 1 line
2.54.0
#18 lore raw

PATCH v3 16/24 drm/vmwgfx: Use match_mapping instead of dummy calls

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

vmwgfx is creating a DMA-buf that cannot be attached by providing always fail functions for dma_buf_ops.

The attach/detach callbacks are already optional inside DMA-buf, but dma_buf_export() is checking for non-null map/unmap callbacks.

Instead use the mapping type interface and provide an always fail match_mapping(). Remove the unused SGT and attach/detach functions, they can never be called if match_mapping() fails.

---
 drivers/gpu/drm/vmwgfx/vmwgfx_prime.c | 32 +++++---------------------------
 1 file changed, 5 insertions(+), 27 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_prime.c +5 -27
index 598b90ac7590..90e4342a378d 100644
@@ -35,41 +35,19 @@
 #include "vmwgfx_bo.h"
 #include "ttm_object.h"
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 
 /*
- * DMA-BUF attach- and mapping methods. No need to implement
- * these until we have other virtual devices use them.
+ * No need to implement these until we have other virtual devices use them.
  */
-
-static int vmw_prime_map_attach(struct dma_buf *dma_buf,
-				struct dma_buf_attachment *attach)
-{
-	return -ENOSYS;
-}
-
-static void vmw_prime_map_detach(struct dma_buf *dma_buf,
-				 struct dma_buf_attachment *attach)
-{
-}
-
-static struct sg_table *vmw_prime_map_dma_buf(struct dma_buf_attachment *attach,
-					      enum dma_data_direction dir)
-{
-	return ERR_PTR(-ENOSYS);
-}
-
-static void vmw_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
-				    struct sg_table *sgb,
-				    enum dma_data_direction dir)
+static int vmw_prime_match_mapping(struct dma_buf_match_args *args)
 {
+	return -EOPNOTSUPP;
 }
 
 const struct dma_buf_ops vmw_prime_dmabuf_ops =  {
-	.attach = vmw_prime_map_attach,
-	.detach = vmw_prime_map_detach,
-	.map_dma_buf = vmw_prime_map_dma_buf,
-	.unmap_dma_buf = vmw_prime_unmap_dma_buf,
 	.release = NULL,
+	.match_mapping = vmw_prime_match_mapping,
 };
 
 int vmw_prime_fd_to_handle(struct drm_device *dev,
Signature · 1 line
2.54.0
#19 lore raw

PATCH v3 17/24 dma-buf: Convert dma_buf_phys_vec_to_sgt() and its users to the SGT mapping type

In reply to Alistair Popple

Currently dma_buf_phys_vec_to_sgt() allocates and returns an sgt. Instead convert it to allocate and return a dma_buf_sgt_mapping.

vfio-pci and RDMA/uverbs can then return the mapping directly. Their map and unmap callbacks can then move from dma_buf_ops into DMA_BUF_SIMPLE_SGT_EXP_MATCH().

---
 drivers/dma-buf/dma-buf-mapping.c                 | 57 ++++++++++++-----------
 drivers/infiniband/core/uverbs_std_types_dmabuf.c | 23 +++++----
 drivers/vfio/pci/vfio_pci_dmabuf.c                | 24 +++++-----
 include/linux/dma-buf-mapping.h                   | 13 +++---
 4 files changed, 61 insertions(+), 56 deletions(-)
drivers/dma-buf/dma-buf-mapping.c +31 -26
index cc3d8d6fe009..d2730be4a9f5 100644
@@ -66,12 +66,12 @@ static unsigned int calc_sg_nents(struct dma_iova_state *state,
 
 /**
  * struct dma_buf_dma - holds DMA mapping information
- * @sgt:    Scatter-gather table
+ * @mapping: The SGT mapping handed to the core, with the scatter-gather table
  * @state:  DMA IOVA state relevant in IOMMU-based DMA
  * @size:   Total size of DMA transfer
  */
 struct dma_buf_dma {
-	struct sg_table sgt;
+	struct dma_buf_sgt_mapping mapping;
 	struct dma_iova_state *state;
 	size_t size;
 };
@@ -99,12 +99,13 @@ struct dma_buf_dma {
  * mandatory exporter should call routing pci_p2pdma_map_type() before calling
  * this function.
  */
-struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
-					 struct p2pdma_provider *provider,
-					 struct phys_vec *phys_vec,
-					 size_t nr_ranges, size_t size,
-					 enum dma_data_direction dir)
+struct dma_buf_sgt_mapping *
+dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
+			struct p2pdma_provider *provider,
+			struct phys_vec *phys_vec, size_t nr_ranges,
+			size_t size, enum dma_data_direction dir)
 {
+	struct device *dma_dev = dma_buf_sgt_dma_device(attach);
 	struct dma_buf_dma *dma;
 	struct scatterlist *sgl;
 	size_t mapped_len = 0;
@@ -123,7 +124,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
 	if (!dma)
 		return ERR_PTR(-ENOMEM);
 
-	switch (pci_p2pdma_map_type(provider, attach->dev)) {
+	switch (pci_p2pdma_map_type(provider, dma_dev)) {
 	case PCI_P2PDMA_MAP_BUS_ADDR:
 		/*
 		 * There is no need in IOVA at all for this flow.
@@ -136,7 +137,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
 			goto err_free_dma;
 		}
 
-		dma_iova_try_alloc(attach->dev, dma->state, 0, size);
+		dma_iova_try_alloc(dma_dev, dma->state, 0, size);
 		break;
 	default:
 		ret = -EINVAL;
@@ -146,18 +147,18 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
 	nents = calc_sg_nents(dma->state, phys_vec, nr_ranges, size);
 
 	/* sg_alloc_table will cleanly fail and return -EINVAL if nents == 0 */
-	ret = sg_alloc_table(&dma->sgt, nents, GFP_KERNEL | __GFP_ZERO);
+	ret = sg_alloc_table(&dma->mapping.sgt, nents, GFP_KERNEL | __GFP_ZERO);
 	if (ret)
 		goto err_free_state;
 
-	sgl = dma->sgt.sgl;
+	sgl = dma->mapping.sgt.sgl;
 
 	for (i = 0; i < nr_ranges; i++) {
 		if (!dma->state) {
 			addr = pci_p2pdma_bus_addr_map(provider,
 						       phys_vec[i].paddr);
 		} else if (dma_use_iova(dma->state)) {
-			ret = dma_iova_link(attach->dev, dma->state,
+			ret = dma_iova_link(dma_dev, dma->state,
 					    phys_vec[i].paddr, 0,
 					    phys_vec[i].len, dir,
 					    DMA_ATTR_MMIO);
@@ -166,10 +167,10 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
 
 			mapped_len += phys_vec[i].len;
 		} else {
-			addr = dma_map_phys(attach->dev, phys_vec[i].paddr,
+			addr = dma_map_phys(dma_dev, phys_vec[i].paddr,
 					    phys_vec[i].len, dir,
 					    DMA_ATTR_MMIO);
-			ret = dma_mapping_error(attach->dev, addr);
+			ret = dma_mapping_error(dma_dev, addr);
 			if (ret)
 				goto err_unmap_dma;
 		}
@@ -180,7 +181,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
 
 	if (dma->state && dma_use_iova(dma->state)) {
 		WARN_ON_ONCE(mapped_len != size);
-		ret = dma_iova_sync(attach->dev, dma->state, 0, mapped_len);
+		ret = dma_iova_sync(dma_dev, dma->state, 0, mapped_len);
 		if (ret)
 			goto err_unmap_dma;
 
@@ -193,7 +194,7 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
 	 * No CPU list included — set orig_nents = 0 so others can detect
 	 * this via SG table (use nents only).
 	 */
-	dma->sgt.orig_nents = 0;
+	dma->mapping.sgt.orig_nents = 0;
 
 
 	/*
@@ -201,20 +202,20 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
 	 * and we allocated correct number of entries in sg_alloc_table()
 	 */
 	WARN_ON_ONCE(sgl);
-	return &dma->sgt;
+	return &dma->mapping;
 
 err_unmap_dma:
 	if (!i || !dma->state) {
 		; /* Do nothing */
 	} else if (dma_use_iova(dma->state)) {
-		dma_iova_destroy(attach->dev, dma->state, mapped_len, dir,
+		dma_iova_destroy(dma_dev, dma->state, mapped_len, dir,
 				 DMA_ATTR_MMIO);
 	} else {
-		for_each_sgtable_dma_sg(&dma->sgt, sgl, i)
-			dma_unmap_phys(attach->dev, sg_dma_address(sgl),
+		for_each_sgtable_dma_sg(&dma->mapping.sgt, sgl, i)
+			dma_unmap_phys(dma_dev, sg_dma_address(sgl),
 				       sg_dma_len(sgl), dir, DMA_ATTR_MMIO);
 	}
-	sg_free_table(&dma->sgt);
+	sg_free_table(&dma->mapping.sgt);
 err_free_state:
 	kfree(dma->state);
 err_free_dma:
@@ -226,16 +227,20 @@ EXPORT_SYMBOL_NS_GPL(dma_buf_phys_vec_to_sgt, "DMA_BUF");
 /**
  * dma_buf_free_sgt- unmaps the buffer
  * @attach:	[in]	attachment to unmap buffer from
- * @sgt:	[in]	scatterlist info of the buffer to unmap
+ * @mapping:	[in]	the mapping returned by dma_buf_phys_vec_to_sgt()
  * @dir:	[in]	direction of DMA transfer
  *
  * This unmaps a DMA mapping for @attached obtained
  * by dma_buf_phys_vec_to_sgt().
  */
-void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
+void dma_buf_free_sgt(struct dma_buf_attachment *attach,
+		      struct dma_buf_sgt_mapping *mapping,
 		      enum dma_data_direction dir)
 {
-	struct dma_buf_dma *dma = container_of(sgt, struct dma_buf_dma, sgt);
+	struct dma_buf_dma *dma =
+		container_of(mapping, struct dma_buf_dma, mapping);
+	struct sg_table *sgt = &mapping->sgt;
+	struct device *dma_dev = dma_buf_sgt_mapping_dma_device(mapping);
 	int i;
 
 	dma_resv_assert_held(attach->dmabuf->resv);
@@ -243,13 +248,13 @@ void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
 	if (!dma->state) {
 		; /* Do nothing */
 	} else if (dma_use_iova(dma->state)) {
-		dma_iova_destroy(attach->dev, dma->state, dma->size, dir,
+		dma_iova_destroy(dma_dev, dma->state, dma->size, dir,
 				 DMA_ATTR_MMIO);
 	} else {
 		struct scatterlist *sgl;
 
 		for_each_sgtable_dma_sg(sgt, sgl, i)
-			dma_unmap_phys(attach->dev, sg_dma_address(sgl),
+			dma_unmap_phys(dma_dev, sg_dma_address(sgl),
 				       sg_dma_len(sgl), dir, DMA_ATTR_MMIO);
 	}
 
drivers/infiniband/core/uverbs_std_types_dmabuf.c +11 -12
index 8d8c997a4883..7463877db3d3 100644
@@ -21,36 +21,36 @@ static int uverbs_dmabuf_attach(struct dma_buf *dmabuf,
 	return 0;
 }
 
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
 uverbs_dmabuf_map(struct dma_buf_attachment *attachment,
 		  enum dma_data_direction dir)
 {
 	struct ib_uverbs_dmabuf_file *priv = attachment->dmabuf->priv;
-	struct sg_table *ret;
+	struct dma_buf_sgt_mapping *mapping;
 
 	dma_resv_assert_held(attachment->dmabuf->resv);
 
 	if (priv->revoked)
 		return ERR_PTR(-ENODEV);
 
-	ret = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
-				      &priv->phys_vec, 1, priv->phys_vec.len,
-				      dir);
-	if (IS_ERR(ret))
-		return ret;
+	mapping = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
+					  &priv->phys_vec, 1,
+					  priv->phys_vec.len, dir);
+	if (IS_ERR(mapping))
+		return ERR_CAST(mapping);
 
 	kref_get(&priv->kref);
-	return ret;
+	return mapping;
 }
 
 static void uverbs_dmabuf_unmap(struct dma_buf_attachment *attachment,
-				struct sg_table *sgt,
+				struct dma_buf_sgt_mapping *mapping,
 				enum dma_data_direction dir)
 {
 	struct ib_uverbs_dmabuf_file *priv = attachment->dmabuf->priv;
 
 	dma_resv_assert_held(attachment->dmabuf->resv);
-	dma_buf_free_sgt(attachment, sgt, dir);
+	dma_buf_free_sgt(attachment, mapping, dir);
 	kref_put(&priv->kref, ib_uverbs_dmabuf_done);
 }
 
@@ -97,11 +97,10 @@ static void uverbs_dmabuf_release(struct dma_buf *dmabuf)
 
 static const struct dma_buf_ops uverbs_dmabuf_ops = {
 	.attach = uverbs_dmabuf_attach,
-	.map_dma_buf = uverbs_dmabuf_map,
-	.unmap_dma_buf = uverbs_dmabuf_unmap,
 	.pin = uverbs_dmabuf_pin,
 	.unpin = uverbs_dmabuf_unpin,
 	.release = uverbs_dmabuf_release,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(uverbs_dmabuf_map, uverbs_dmabuf_unmap),
 };
 
 static int UVERBS_HANDLER(UVERBS_METHOD_DMABUF_ALLOC)(
drivers/vfio/pci/vfio_pci_dmabuf.c +12 -12
index c16f460c01d6..ab3a7217e0a3 100644
@@ -47,37 +47,37 @@ static void vfio_pci_dma_buf_done(struct kref *kref)
 	complete(&priv->comp);
 }
 
-static struct sg_table *
+static struct dma_buf_sgt_mapping *
 vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment,
 		     enum dma_data_direction dir)
 {
 	struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv;
-	struct sg_table *ret;
+	struct dma_buf_sgt_mapping *mapping;
 
 	dma_resv_assert_held(priv->dmabuf->resv);
 
 	if (priv->revoked)
 		return ERR_PTR(-ENODEV);
 
-	ret = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
-				      priv->phys_vec, priv->nr_ranges,
-				      priv->size, dir);
-	if (IS_ERR(ret))
-		return ret;
+	mapping = dma_buf_phys_vec_to_sgt(attachment, priv->provider,
+					  priv->phys_vec, priv->nr_ranges,
+					  priv->size, dir);
+	if (IS_ERR(mapping))
+		return ERR_CAST(mapping);
 
 	kref_get(&priv->kref);
-	return ret;
+	return mapping;
 }
 
 static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment,
-				   struct sg_table *sgt,
+				   struct dma_buf_sgt_mapping *mapping,
 				   enum dma_data_direction dir)
 {
 	struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv;
 
 	dma_resv_assert_held(priv->dmabuf->resv);
 
-	dma_buf_free_sgt(attachment, sgt, dir);
+	dma_buf_free_sgt(attachment, mapping, dir);
 	kref_put(&priv->kref, vfio_pci_dma_buf_done);
 }
 
@@ -101,9 +101,9 @@ static void vfio_pci_dma_buf_release(struct dma_buf *dmabuf)
 
 static const struct dma_buf_ops vfio_pci_dmabuf_ops = {
 	.attach = vfio_pci_dma_buf_attach,
-	.map_dma_buf = vfio_pci_dma_buf_map,
-	.unmap_dma_buf = vfio_pci_dma_buf_unmap,
 	.release = vfio_pci_dma_buf_release,
+	DMA_BUF_SIMPLE_SGT_EXP_MATCH(vfio_pci_dma_buf_map,
+				     vfio_pci_dma_buf_unmap),
 };
 
 /*
include/linux/dma-buf-mapping.h +7 -6
index e442ed0ede71..d73bff5d6853 100644
@@ -108,12 +108,13 @@ dma_buf_mapping_type_of(const struct dma_buf_mapping *mapping)
 	return mapping->match->type;
 }
 
-struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
-					 struct p2pdma_provider *provider,
-					 struct phys_vec *phys_vec,
-					 size_t nr_ranges, size_t size,
-					 enum dma_data_direction dir);
-void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
+struct dma_buf_sgt_mapping *
+dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
+			struct p2pdma_provider *provider,
+			struct phys_vec *phys_vec, size_t nr_ranges,
+			size_t size, enum dma_data_direction dir);
+void dma_buf_free_sgt(struct dma_buf_attachment *attach,
+		      struct dma_buf_sgt_mapping *mapping,
 		      enum dma_data_direction dir);
 
 int dma_buf_match_mapping(struct dma_buf_match_args *args,
Signature · 1 line
2.54.0
#20 lore raw

PATCH v3 18/24 iio: buffer: convert to use the SGT mapping type

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

These importer helper functions should call dma_buf_sgt_dma_device() as they are always working with SGTs.

---
 drivers/iio/industrialio-buffer.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/industrialio-buffer.c +4 -3
index 4a1063e4891f..f907fb6ca936 100644
@@ -16,6 +16,7 @@
 #include <linux/export.h>
 #include <linux/device.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-fence.h>
 #include <linux/dma-resv.h>
 #include <linux/file.h>
@@ -1676,7 +1677,7 @@ iio_buffer_find_attachment(struct iio_dev_buffer_pair *ib,
 	guard(mutex)(&buffer->dmabufs_mutex);
 
 	list_for_each_entry(priv, &buffer->dmabufs, entry) {
-		if (priv->attach->dev == dma_dev
+		if (dma_buf_sgt_dma_device(priv->attach) == dma_dev
 		    && priv->attach->dmabuf == dmabuf) {
 			attach = priv->attach;
 			break;
@@ -1760,7 +1761,7 @@ static int iio_buffer_attach_dmabuf(struct iio_dev_buffer_pair *ib,
 	 * combo. If we do, refuse to attach.
 	 */
 	list_for_each_entry(each, &buffer->dmabufs, entry) {
-		if (each->attach->dev == dma_dev
+		if (dma_buf_sgt_dma_device(each->attach) == dma_dev
 		    && each->attach->dmabuf == dmabuf) {
 			/*
 			 * We unlocked the reservation object, so going through
@@ -1814,7 +1815,7 @@ static int iio_buffer_detach_dmabuf(struct iio_dev_buffer_pair *ib,
 	guard(mutex)(&buffer->dmabufs_mutex);
 
 	list_for_each_entry(priv, &buffer->dmabufs, entry) {
-		if (priv->attach->dev == dma_dev
+		if (dma_buf_sgt_dma_device(priv->attach) == dma_dev
 		    && priv->attach->dmabuf == dmabuf) {
 			list_del(&priv->entry);
 
Signature · 1 line
2.54.0
#21 lore raw

PATCH v3 19/24 functionfs: convert to use the SGT mapping type

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

These importer helper functions should call dma_buf_sgt_dma_device() as they are always working with SGTs.

---
 drivers/usb/gadget/function/f_fs.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/f_fs.c +3 -2
index 2f221d0a983f..1efadf2c0f73 100644
@@ -16,6 +16,7 @@
 
 #include <linux/blkdev.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/dma-fence.h>
 #include <linux/dma-resv.h>
 #include <linux/pagemap.h>
@@ -1514,7 +1515,7 @@ ffs_dmabuf_find_attachment(struct ffs_epfile *epfile, struct dma_buf *dmabuf)
 	mutex_lock(&epfile->dmabufs_mutex);
 
 	list_for_each_entry(priv, &epfile->dmabufs, entry) {
-		if (priv->attach->dev == dev
+		if (dma_buf_sgt_dma_device(priv->attach) == dev
 		    && priv->attach->dmabuf == dmabuf) {
 			attach = priv->attach;
 			break;
@@ -1616,7 +1617,7 @@ static int ffs_dmabuf_detach(struct file *file, int fd)
 	mutex_lock(&epfile->dmabufs_mutex);
 
 	list_for_each_entry_safe(priv, tmp, &epfile->dmabufs, entry) {
-		if (priv->attach->dev == dev
+		if (dma_buf_sgt_dma_device(priv->attach) == dev
 		    && priv->attach->dmabuf == dmabuf) {
 			/* Cancel any pending transfer */
 			spin_lock_irq(&ffs->eps_lock);
Signature · 1 line
2.54.0
#22 lore raw

PATCH v3 20/24 dma-buf: Remove unused SGT stuff from the common structures

In reply to Alistair Popple

Now that all exporters are converted these compatibility things can be removed:

dma_buf_ops: map_dma_buf/unmap_dma_buf
Moved to dma_buf_mapping_sgt_exp_ops

dma_buf_attachment: dev
Moved to the match set entry's sgt_data.importing_dma_device, accessed via dma_buf_sgt_dma_device()

dma_buf_sgt_compat_match_mapping(): No compatibility exporters anymore

dma_buf_attachment: peer2peer stays until the PCIe P2P mapping type replaces it.

---
 drivers/dma-buf/dma-buf-mapping.c | 75 ---------------------------------------
 drivers/dma-buf/dma-buf.c         | 33 ++++++-----------
 include/linux/dma-buf-mapping.h   | 73 +++++++++++++++++++++++++++++++++----
 include/linux/dma-buf.h           | 73 +------------------------------------
 include/trace/events/dma_buf.h    |  2 +-
 5 files changed, 78 insertions(+), 178 deletions(-)
drivers/dma-buf/dma-buf-mapping.c +0 -75
index d2730be4a9f5..cf3d25aa3884 100644
@@ -335,8 +335,6 @@ dma_buf_sgt_finish_match(struct dma_buf_match_args *args,
 			 const struct dma_buf_mapping_match *imp,
 			 struct dma_buf_mapping_match *match)
 {
-	struct dma_buf_attachment *attach = args->attach;
-
 	*match = (struct dma_buf_mapping_match) {
 		.type = &dma_buf_mapping_sgt_type,
 		.exp_ops = exp->exp_ops,
@@ -344,14 +342,6 @@ dma_buf_sgt_finish_match(struct dma_buf_match_args *args,
 			.importing_dma_device = args->dev,
 		},
 	};
-
-	/*
-	 * Setup the SGT type variables stored in attach because importers and
-	 * exporters that do not natively use mappings expect them to be there.
-	 * When converting to use mappings users should use the match versions
-	 * of these instead.
-	 */
-	attach->dev = args->dev;
 }
 
 static void dma_buf_sgt_debugfs_dump(struct seq_file *s,
@@ -366,68 +356,3 @@ const struct dma_buf_mapping_type dma_buf_mapping_sgt_type = {
 	.debugfs_dump = dma_buf_sgt_debugfs_dump,
 };
 EXPORT_SYMBOL_NS_GPL(dma_buf_mapping_sgt_type, "DMA_BUF");
-
-/* An unaware exporter's sg_table presented as an SGT mapping */
-struct dma_buf_sgt_compat_mapping {
-	struct dma_buf_sgt_mapping mapping;
-	struct sg_table *sgt;
-};
-
-static struct dma_buf_sgt_mapping *
-dma_buf_sgt_compat_map_dma_buf(struct dma_buf_attachment *attach,
-			       enum dma_data_direction dir)
-{
-	struct dma_buf_sgt_compat_mapping *compat;
-	struct sg_table *sgt;
-
-	sgt = attach->dmabuf->ops->map_dma_buf(attach, dir);
-	if (!sgt)
-		return ERR_PTR(-ENOMEM);
-	if (IS_ERR(sgt))
-		return ERR_CAST(sgt);
-
-	compat = kzalloc_obj(*compat);
-	if (!compat) {
-		attach->dmabuf->ops->unmap_dma_buf(attach, sgt, dir);
-		return ERR_PTR(-ENOMEM);
-	}
-
-	/* The exporter keeps its table; the mapping shares its entries */
-	compat->sgt = sgt;
-	compat->mapping.sgt = *sgt;
-	return &compat->mapping;
-}
-
-static void dma_buf_sgt_compat_unmap_dma_buf(struct dma_buf_attachment *attach,
-					     struct dma_buf_sgt_mapping *mapping,
-					     enum dma_data_direction dir)
-{
-	struct dma_buf_sgt_compat_mapping *compat =
-		container_of(mapping, struct dma_buf_sgt_compat_mapping, mapping);
-
-	attach->dmabuf->ops->unmap_dma_buf(attach, compat->sgt, dir);
-	kfree(compat);
-}
-
-/* Route the classic map/unmap ops through the exp ops for old importers */
-static const struct dma_buf_mapping_sgt_exp_ops dma_buf_sgt_compat_exp_ops = {
-	.map_dma_buf = dma_buf_sgt_compat_map_dma_buf,
-	.unmap_dma_buf = dma_buf_sgt_compat_unmap_dma_buf,
-};
-
-/*
- * This entry is used for unaware exporters that do not support
- * match_mapping(). It wraps the dma_buf ops for SGT mappings into a mapping
- * type so aware importers can transparently work with unaware exporters. This
- * does not require p2p because old exporters will check it through the
- * attach->peer2peer mechanism.
- */
-static const struct dma_buf_mapping_match dma_buf_sgt_exp_compat_matches[] = {
-	DMA_BUF_EMAPPING_SGT(&dma_buf_sgt_compat_exp_ops),
-};
-
-int dma_buf_sgt_compat_match_mapping(struct dma_buf_match_args *args)
-{
-	return dma_buf_match_mapping(args, dma_buf_sgt_exp_compat_matches,
-				     ARRAY_SIZE(dma_buf_sgt_exp_compat_matches));
-}
drivers/dma-buf/dma-buf.c +10 -23
index 6188cd11b004..20e397683826 100644
@@ -718,19 +718,9 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 		    || !exp_info->ops->release))
 		return ERR_PTR(-EINVAL);
 
-	if (exp_info->ops->match_mapping ||
-	    exp_info->ops->single_exporter_match) {
-		if (WARN_ON(exp_info->ops->map_dma_buf ||
-			    exp_info->ops->unmap_dma_buf))
-			return ERR_PTR(-EINVAL);
-		if (WARN_ON(exp_info->ops->match_mapping &&
-			    exp_info->ops->single_exporter_match))
-			return ERR_PTR(-EINVAL);
-	} else {
-		if (WARN_ON(!exp_info->ops->map_dma_buf ||
-			    !exp_info->ops->unmap_dma_buf))
-			return ERR_PTR(-EINVAL);
-	}
+	if (WARN_ON(!exp_info->ops->match_mapping &&
+		    !exp_info->ops->single_exporter_match))
+		return ERR_PTR(-EINVAL);
 
 	if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
 		return ERR_PTR(-EINVAL);
@@ -1009,8 +999,8 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
  *
  *     - &dma_buf_ops.pin()
  *     - &dma_buf_ops.unpin()
- *     - &dma_buf_ops.map_dma_buf()
- *     - &dma_buf_ops.unmap_dma_buf()
+ *     - &dma_buf_mapping_sgt_exp_ops.map_dma_buf()
+ *     - &dma_buf_mapping_sgt_exp_ops.unmap_dma_buf()
  *     - &dma_buf_ops.vmap()
  *     - &dma_buf_ops.vunmap()
  *
@@ -1098,13 +1088,9 @@ dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
 		if (ret)
 			goto err_attach;
 	} else {
-		const struct dma_buf_mapping_match *exp_match =
-			dmabuf->ops->single_exporter_match;
-
-		if (exp_match)
-			ret = dma_buf_match_mapping(&match_args, exp_match, 1);
-		else
-			ret = dma_buf_sgt_compat_match_mapping(&match_args);
+		ret = dma_buf_match_mapping(&match_args,
+					    dmabuf->ops->single_exporter_match,
+					    1);
 		if (ret)
 			goto err_attach;
 	}
@@ -1166,7 +1152,8 @@ void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
 		dmabuf->ops->detach(dmabuf, attach);
 
 	DMA_BUF_TRACE(trace_dma_buf_detach, dmabuf, attach,
-		dma_buf_attachment_is_dynamic(attach), attach->dev);
+		dma_buf_attachment_is_dynamic(attach),
+		dma_buf_sgt_dma_device(attach));
 
 	kfree(attach);
 }
include/linux/dma-buf-mapping.h +66 -7

90 lines not on this page · raw message

include/linux/dma-buf.h +1 -72

97 lines not on this page · raw message

include/trace/events/dma_buf.h +1 -1
index 3bb88d05bcc8..13d3a82856b6 100644
@@ -40,7 +40,7 @@ DECLARE_EVENT_CLASS(dma_buf_attach_dev,
 	TP_ARGS(dmabuf, attach, is_dynamic, dev),
 
 	TP_STRUCT__entry(
-		__string(	dev_name,			dev_name(dev))
+		__string(	dev_name,			dev ? dev_name(dev) : "none")
 		__string(	exp_name,			dmabuf->exp_name)
 		__field(	size_t,				size)
 		__field(	ino_t,				ino)
Signature · 1 line
2.54.0
#23 lore raw

PATCH v3 21/24 dma-buf: Add kunit tests for mapping type

In reply to Alistair Popple

From: Jason Gunthorpe <[email protected]>

Some basic coverage of common flows:
- Check dma_buf_match_mapping()'s rules. These choices effectively become driver facing API and it would be a pain to change them later
- Test the dma_buf_sgt attachment flow to see that the new wrappers work

---
 drivers/dma-buf/Makefile         |   1 +
 drivers/dma-buf/st-dma-mapping.c | 403 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 404 insertions(+)
drivers/dma-buf/Makefile +1 -0
index 523731b0f83e..02745fca8b88 100644
@@ -11,6 +11,7 @@ dmabuf_kunit-y := \
 	st-dma-fence.o \
 	st-dma-fence-chain.o \
 	st-dma-fence-unwrap.o \
+	st-dma-mapping.o \
 	st-dma-resv.o
 
 obj-$(CONFIG_DMABUF_KUNIT_TEST) += dmabuf_kunit.o
drivers/dma-buf/st-dma-mapping.c new file +403 -0

403 lines not on this page · raw message

Signature · 1 line
2.54.0
#24 lore raw

PATCH v3 22/24 dma-buf: Let the exporter select mapping type at pin/map

In reply to Alistair Popple

Currently an attachment is always mapped with the first entry of the match set negotiated at attach. The match set only records which types both sides could use. Which of them the exporter can actually use time depends on where the buffer is and on the other attachments at the time an importer first pins or maps.

However currently an exporter offering more than one mapping type has no way to select the mapping type to use. To allow that add an optional dma_buf_ops.select_mapping() callback.

dma_buf_pin() and the map functions call it under the dma_resv lock before the exporter's own pin or map callback. The exporter returns the index of the entry to use given the current placement and attachments, or -EBUSY if the attachment cannot be used without disturbing a mapping that cannot be invalidated.

To avoid ping-ponging of buffers and other sub-optimal behaviour the selection is not allowed to change without existing attachments being explicitly invalidated by the exporter.

The core enforces this by recording the selection on first map/pin and not calling it again across subsequent unmap/map until dma_buf_invalidate_mappings() has been called on the attachment. This means an importer remapping cannot bounce the buffer between placements as the type cannot change unless the exporter invalidates first. An attachment that is never invalidated keeps its type for life.

Mappings are unmapped via the returned mapping type to ensure the correct unmap function is called. This is because whilst mappings should no longer be used after an invalidate the mapping itself can outlive the invalidate. Thus the attachment selection may have changed between map and unmap.

---

Changes for v3:

- Return a type specific struct and unmap through it to ensure the correct unmap callbacks are called.

---
 drivers/dma-buf/dma-buf-mapping-internal.h |  29 +++++
 drivers/dma-buf/dma-buf-mapping.c          |  68 ++++++++++-
 drivers/dma-buf/dma-buf.c                  | 179 +++++++++++++++++++----------
 include/linux/dma-buf-mapping.h            |   9 +-
 include/linux/dma-buf.h                    |  35 ++++++
 5 files changed, 256 insertions(+), 64 deletions(-)
drivers/dma-buf/dma-buf-mapping-internal.h new file +29 -0
new file mode 100644
index 000000000000..e1337db2b205
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * DMA-buf mapping type internals shared by dma-buf.c and dma-buf-mapping.c.
+ * Nothing in here is for exporters or importers.
+ */
+#ifndef __DMA_BUF_MAPPING_INTERNAL_H__
+#define __DMA_BUF_MAPPING_INTERNAL_H__
+
+#include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
+
+/*
+ * Internal: Selection is made by exporters through &dma_buf_ops.select_mapping
+ * in dma_buf_pin() or map paths. Importers never call this.
+ */
+int dma_buf_mapping_select(struct dma_buf_attachment *attach);
+void dma_buf_mapping_unselect(struct dma_buf_attachment *attach,
+			      bool was_selected);
+
+/*
+ * Internal: Records the selection was successful after the pin or map that
+ * followed dma_buf_mapping_select() succeeds.
+ */
+static inline void dma_buf_mapping_commit(struct dma_buf_attachment *attach)
+{
+	attach->selected = true;
+}
+
+#endif /* __DMA_BUF_MAPPING_INTERNAL_H__ */
drivers/dma-buf/dma-buf-mapping.c +67 -1

86 lines not on this page · raw message

drivers/dma-buf/dma-buf.c +120 -59

264 lines not on this page · raw message

include/linux/dma-buf-mapping.h +5 -4
index 850a76ffba14..0a1c2934a932 100644
@@ -86,7 +86,9 @@ struct dma_buf_mapping_type {
 
 /**
  * struct dma_buf_mapping - header of a mapping made through an attachment
- * @match: The match set entry the mapping was made with
+ * @match: The entry of &dma_buf_attachment.matches the mapping was made with.
+ *         The mapping is torn down through it, whatever the attachment has
+ *         selected by then.
  *
  * A mapping is an object allocated by whoever produces it, the exporter for
  * the SGT type, embedding this header. The core fills the header in once the
@@ -125,13 +127,12 @@ int dma_buf_match_mapping(struct dma_buf_match_args *args,
  * dma_buf_attach_map_type - The match this attachment is mapped with
  * @attach: the attachment
  *
- * Returns: the entry of @attach->matches in force for the attachment: the
- * exporter's default, which is the first entry.
+ * Returns: the entry of @attach->matches the attachment is mapped with.
  */
 static inline const struct dma_buf_mapping_match *
 dma_buf_attach_map_type(const struct dma_buf_attachment *attach)
 {
-	return &attach->matches[0];
+	return &attach->matches[attach->selected_idx];
 }
 
 /*
include/linux/dma-buf.h +35 -0
index b1d1a239fa27..c60d9f96ec36 100644
@@ -254,6 +254,30 @@ struct dma_buf_ops {
 	 */
 	int (*match_mapping)(struct dma_buf_match_args *args);
 
+	/**
+	 * @select_mapping:
+	 *
+	 * Called before the type's map callback with the dma_resv lock held
+	 * from dma_buf_pin() or the map functions to choose which entry of
+	 * &dma_buf_attachment.matches maps this attachment. The exporter decides the
+	 * buffer's final placement here taking all live mappings and pinned attachments
+	 * into account.
+	 *
+	 * The core records the choice and does not ask again until the exporter
+	 * has delivered &dma_buf_attach_ops.invalidate_mappings to this
+	 * attachment. The exporter must therefore keep the recorded type
+	 * servable - moving the buffer somewhere the selected type cannot be
+	 * served from requires invalidating first. Attachments that cannot
+	 * be invalidated keep their first choice for life.
+	 *
+	 * Optional. Without it the first entry of the match set is used.
+	 *
+	 * Returns: the index into &dma_buf_attachment.matches to use, or a
+	 * negative errno. -EBUSY means the attachment cannot be served without
+	 * disturbing a mapping that cannot be invalidated.
+	 */
+	int (*select_mapping)(struct dma_buf_attachment *attach);
+
 	/**
 	 * @single_exporter_match:
 	 *
@@ -482,6 +506,15 @@ struct dma_buf_attach_ops {
  *           entry is the exporter's default; dma_buf_attach_map_type()
  *           returns the entry the attachment is mapped with.
  * @nr_matches: Number of valid entries in @matches.
+ * @selected_idx: Index into @matches of the entry the attachment is mapped
+ *                with, chosen by &dma_buf_ops.select_mapping at the first pin
+ *                or map and kept, enforced by the core, until
+ *                &dma_buf_attach_ops.invalidate_mappings is delivered to this
+ *                attachment. Zero, the exporter's default, before the first
+ *                selection.
+ * @selected: True while @selected_idx is in force, i.e. the next pin or map
+ *            must not select again; cleared by dma_buf_invalidate_mappings(),
+ *            or when the pin or map that made the selection fails.
  *
  * This structure holds the attachment information between the dma_buf buffer
  * and its user device(s). The list contains one attachment struct per device
@@ -501,6 +534,8 @@ struct dma_buf_attachment {
 	void *priv;
 	struct dma_buf_mapping_match matches[DMA_BUF_MAX_MATCHES];
 	unsigned int nr_matches;
+	unsigned int selected_idx;
+	bool selected;
 };
 
 /**
Signature · 1 line
2.54.0
#25 lore raw

PATCH v3 23/24 dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt

In reply to Alistair Popple

An importer listing more than one mapping type cannot know which type-specific map function to call, because the exporter selects the type at map time. Create a generic entry point to determine the correct type.

dma_buf_mapping_map() selects via dma_buf_mapping_select() and calls the correct map op based on dma_buf_mapping_type. That returns the struct dma_buf_mapping header embedded in the type's own result.

dma_buf_mapping_unmap() calls the unmap op of the type recorded in the returned dma_buf_mapping, so a mapping that outlives an invalidate and a new selection is still torn down with the type and device it was made with. Selection therefore only applies to new pins/maps.

The SGT implementation uses the existing exporter ops. The existing dma_buf_{map|unmap}_sgt() functions call the generic functions instead. Their result is the exporter's struct dma_buf_sgt_mapping, with dma_buf_sgt_mapping_table() returning the embedded sg_table. A future series will implement a new importer type that returns a list of DMA addresses instead of a sg_table.

This will allow existing importers to be converted over to the new type without having to convert all at the same time. Once all importers have been converted the current SGT import type can be removed.

---

Changes for v3:

- The SGT type's result is the exporter's own struct dma_buf_sgt_mapping.
- dma_buf_map_sgt() and dma_buf_unmap_sgt() become wrappers for the generic functions.

---
 drivers/dma-buf/dma-buf-mapping-internal.h | 10 ++++
 drivers/dma-buf/dma-buf-mapping.c          | 90 ++++++++++++++++++++++++++++++
 drivers/dma-buf/dma-buf.c                  | 63 +++++++++++++--------
 include/linux/dma-buf-mapping.h            | 54 +++++++++++++++++-
 4 files changed, 193 insertions(+), 24 deletions(-)
drivers/dma-buf/dma-buf-mapping-internal.h +10 -0
index e1337db2b205..568788fbafa9 100644
@@ -26,4 +26,14 @@ static inline void dma_buf_mapping_commit(struct dma_buf_attachment *attach)
 	attach->selected = true;
 }
 
+/*
+ * Internal: .map / .unmap of the SGT type. Implemented in dma-buf.c using
+ * the exporter's &dma_buf_mapping_sgt_exp_ops.
+ */
+struct dma_buf_mapping *dma_buf_sgt_type_map(struct dma_buf_attachment *attach,
+					     enum dma_data_direction dir);
+void dma_buf_sgt_type_unmap(struct dma_buf_attachment *attach,
+			    struct dma_buf_mapping *mapping,
+			    enum dma_data_direction dir);
+
 #endif /* __DMA_BUF_MAPPING_INTERNAL_H__ */
drivers/dma-buf/dma-buf-mapping.c +90 -0

101 lines not on this page · raw message

drivers/dma-buf/dma-buf.c +41 -22

118 lines not on this page · raw message

include/linux/dma-buf-mapping.h +52 -2

78 lines not on this page · raw message

Signature · 1 line
2.54.0
#26 lore raw

Re: [PATCH v3 23/24] dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] dma-buf: wrong dma_buf_attach_map_type() doc on dma_buf_mapping_type.unmap --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] dma-buf: wrong dma_buf_attach_map_type() doc on dma_buf_mapping_type.unmap
--

commit bd9afecc501b0a6674a8f68fd07c287949304430
Author: Alistair Popple <[email protected]>

dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt

This commit adds generic dma_buf_mapping_map() and dma_buf_mapping_unmap() entry points that select a mapping type and dispatch through its callbacks, and converts dma_buf_map_sgt() and dma_buf_unmap_sgt() to use them.

Quoted 25 lines
include/linux/dma-buf-mapping.h +17 -0
index 0a1c2934a932a..98d5765cfdba9 100644
@@ -82,6 +82,32 @@ struct dma_buf_mapping_type {
 	 */
 	void (*debugfs_dump)(struct seq_file *s,
 			     struct dma_buf_attachment *attach);
+
+	/**
+	 * @map:
+	 *
+	 * Produce a mapping of this type for @attach, for which this type has
+	 * already been selected. Called by dma_buf_mapping_map() with the
+	 * dma_resv lock held. The result embeds &struct dma_buf_mapping, which
+	 * the core fills in once this returns. A map that may unmap its result
+	 * on its own unwind must record the entry in it first, since every
+	 * unmap reads it. Responsible for pin-on-map of static importers if
+	 * the type supports them.
+	 */
+	struct dma_buf_mapping *(*map)(struct dma_buf_attachment *attach,
+				       enum dma_data_direction dir);
+
+	/**
+	 * @unmap:

include/linux/dma-buf-mapping.h struct dma_buf_mapping_type { · line 111

Quoted 10 lines
include/linux/dma-buf-mapping.h +9 -0
+	 *
+	 * Release a mapping returned by @map. Called by dma_buf_mapping_unmap()
+	 * with the dma_resv lock held and with dma_buf_attach_map_type()
+	 * naming the entry the mapping was made with, which need not be the
+	 * attachment's current selection.
+	 */
+	void (*unmap)(struct dma_buf_attachment *attach,
+		      struct dma_buf_mapping *mapping,
+		      enum dma_data_direction dir);
 };

[Severity: Low]
Should this @unmap comment refer to mapping->match rather than dma_buf_attach_map_type()?

dma_buf_attach_map_type() returns &attach->matches[attach->selected_idx], and dma_buf_mapping_unmap() calls match->type->unmap() without changing attach->selected_idx, so dma_buf_attach_map_type() still returns the attachment's current selection while mapping->match holds the entry the mapping was made with.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
#27 lore raw

PATCH v3 24/24 dma-buf: Add kunit tests for mapping type selection

In reply to Alistair Popple

Add an offer list and a select_mapping() callback to the mock exporter along with map/unmap for the two mock types.

Adds tests for the exporter/importer contract.

- test_match_set_is_final: a later importer attaching does not change an existing attachment's match set or default. Later attachments influence what the exporter selects at pin and map time but not previous attachments.

- test_select_at_map: the exporter chooses a non-default entry on first map and the result is tagged with it. A pin taken while there is a live mapping inherits the type and the choice survives unmap/remap. Only dma_buf_invalidate_mappings() allows the exporter to make a different selection. An -EBUSY from selection leaves nothing established.

- test_select_at_pin: pin selects, and the choice survives map, unmap and unpin so a pinned importer that is never invalidated keeps its first type for the life of the import.

- test_unmap_uses_own_type: after an invalidate the exporter may select another type for the next map. A mapping made with the previous one is still live, and each mapping must be torn down with the type it was made with.

- test_select_survives_own_invalidate: a pin or map whose exporter callback invalidates every attachment still ends up selected, and one that invalidates and fails does not.

---
 drivers/dma-buf/st-dma-mapping.c | 703 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 703 insertions(+)
drivers/dma-buf/st-dma-mapping.c +703 -0
index 8c69ae212455..42e6748f0ae6 100644
@@ -7,6 +7,7 @@
 #include <kunit/test.h>
 #include <linux/dma-buf.h>
 #include <linux/dma-buf-mapping.h>
+#include <linux/dma-resv.h>
 #include <linux/errno.h>
 
 /* Mock tracking state -- reset before each test */
@@ -21,6 +22,27 @@ static const struct dma_buf_match_args *mock_finish_args_arg;
 static const struct dma_buf_mapping_match *mock_finish_exp_arg;
 static const struct dma_buf_mapping_match *mock_finish_imp_arg;
 
+/* Mapping types the configurable mock exporter currently offers */
+static const struct dma_buf_mapping_match *mock_exp_offer;
+static size_t mock_exp_offer_len;
+
+/* Which match set entry the selecting mock exporter picks, or an error */
+static int mock_select_idx;
+static int mock_select_err;
+static int mock_select_count;
+
+/* Make the mock exporter's pin and map invalidate everyone, as a move does */
+static bool mock_invalidate_in_callback;
+
+/* What the mock types' unmap saw: the mapping's header type and its map's */
+static const struct dma_buf_mapping_type *mock_unmap_type;
+static const struct dma_buf_mapping_type *mock_unmap_made_by;
+
+/* Failure injection for the mock types' map and the mock exporter's pin */
+static int mock_map_err;
+static int mock_pin_err;
+static int mock_pin_count;
+
 static int reset_mock_state(struct kunit *test)
 {
 	mock_match_called = false;
@@ -32,6 +54,17 @@ static int reset_mock_state(struct kunit *test)
 	mock_finish_args_arg = NULL;
 	mock_finish_exp_arg = NULL;
 	mock_finish_imp_arg = NULL;
+	mock_exp_offer = NULL;
+	mock_exp_offer_len = 0;
+	mock_select_idx = 0;
+	mock_select_err = 0;
+	mock_select_count = 0;
+	mock_invalidate_in_callback = false;
+	mock_unmap_type = NULL;
+	mock_unmap_made_by = NULL;
+	mock_map_err = 0;
+	mock_pin_err = 0;
+	mock_pin_count = 0;
 	return 0;
 }
 
@@ -374,6 +407,485 @@ static void test_mock_attach(struct kunit *test)
 	dma_buf_put(dmabuf);
 }
 
+static int mock_dmabuf_offer_match_mapping(struct dma_buf_match_args *args)
+{
+	return dma_buf_match_mapping(args, mock_exp_offer, mock_exp_offer_len);
+}
+
+static const struct dma_buf_ops mock_dmabuf_offer_ops = {
+	.release = mock_dmabuf_release,
+	.match_mapping = mock_dmabuf_offer_match_mapping,
+};
+
+/*
+ * The match set is final once attached. A later importer attaching must not
+ * change an existing attachment even if the exporter returns a different set.
+ *
+ * Later attachments influence what the exporter selects at pin or map time,
+ * not what was previously negotiated.
+ */
+static void test_match_set_is_final(struct kunit *test)
+{
+	static const struct dma_buf_mapping_match offer_ab[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	static const struct dma_buf_mapping_match offer_b[] = {
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp1[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp2[] = { { .type = &mock_type_b } };
+
+	MOCK_IMPORTER_OPS(importer1_ops, imp1);
+	MOCK_IMPORTER_OPS(importer2_ops, imp2);
+	struct dma_buf_attachment *attach1, *attach2;
+	struct dma_buf *dmabuf;
+	struct device *dev;
+
+	dev = kunit_device_register(test, "dma-buf-test");
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	dmabuf = mock_dmabuf_export(&mock_dmabuf_offer_ops);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmabuf);
+
+	mock_exp_offer = offer_ab;
+	mock_exp_offer_len = ARRAY_SIZE(offer_ab);
+	attach1 = dma_buf_dynamic_attach(dmabuf, dev, &importer1_ops, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, attach1);
+	KUNIT_ASSERT_EQ(test, attach1->nr_matches, 2);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach1)->type,
+			    &mock_type_a);
+
+	/* Exporter's answer for new importers changes; attach1 does not */
+	mock_exp_offer = offer_b;
+	mock_exp_offer_len = ARRAY_SIZE(offer_b);
+	attach2 = dma_buf_dynamic_attach(dmabuf, dev, &importer2_ops, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, attach2);
+	KUNIT_ASSERT_EQ(test, attach2->nr_matches, 1);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach2)->type,
+			    &mock_type_b);
+	KUNIT_EXPECT_EQ(test, attach1->nr_matches, 2);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach1)->type,
+			    &mock_type_a);
+
+	dma_buf_detach(dmabuf, attach2);
+	KUNIT_EXPECT_EQ(test, attach1->nr_matches, 2);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach1)->type,
+			    &mock_type_a);
+
+	dma_buf_detach(dmabuf, attach1);
+	dma_buf_put(dmabuf);
+}
+
+/* Mock mapping result for the generic map path */
+struct mock_mapping {
+	struct dma_buf_mapping base;
+	const struct dma_buf_mapping_type *made_by;
+};
+
+static struct dma_buf_mapping *mock_type_map(struct dma_buf_attachment *attach,
+					     enum dma_data_direction dir)
+{
+	struct mock_mapping *m;
+
+	if (mock_invalidate_in_callback)
+		dma_buf_invalidate_mappings(attach->dmabuf);
+	if (mock_map_err)
+		return ERR_PTR(mock_map_err);
+	m = kzalloc_obj(*m);
+	if (!m)
+		return ERR_PTR(-ENOMEM);
+	m->made_by = dma_buf_attach_map_type(attach)->type;
+	return &m->base;
+}
+
+static void mock_type_unmap(struct dma_buf_attachment *attach,
+			    struct dma_buf_mapping *mapping,
+			    enum dma_data_direction dir)
+{
+	struct mock_mapping *m = container_of(mapping, struct mock_mapping, base);
+
+	mock_unmap_type = dma_buf_mapping_type_of(mapping);
+	mock_unmap_made_by = m->made_by;
+	kfree(m);
+}
+
+/* Exporter choosing the match set entry the test asks for */
+static int mock_dmabuf_select_mapping(struct dma_buf_attachment *attach)
+{
+	mock_select_count++;
+	if (mock_select_err)
+		return mock_select_err;
+	return mock_select_idx;
+}
+
+static int mock_dmabuf_pin(struct dma_buf_attachment *attach)
+{
+	if (mock_invalidate_in_callback)
+		dma_buf_invalidate_mappings(attach->dmabuf);
+	if (mock_pin_err)
+		return mock_pin_err;
+	mock_pin_count++;
+	return 0;
+}
+
+static void mock_dmabuf_unpin(struct dma_buf_attachment *attach)
+{
+	mock_pin_count--;
+}
+
+static const struct dma_buf_ops mock_dmabuf_select_ops = {
+	.release = mock_dmabuf_release,
+	.match_mapping = mock_dmabuf_offer_match_mapping,
+	.select_mapping = mock_dmabuf_select_mapping,
+	.pin = mock_dmabuf_pin,
+	.unpin = mock_dmabuf_unpin,
+};
+
+/*
+ * Exporter decides type at first map from the match set. The result is tagged
+ * with it and the core holds the choice across unmap/remap until the attachment
+ * has been explicitly invalidated.
+ */
+static void test_select_at_map(struct kunit *test)
+{
+	static const struct dma_buf_mapping_match offer_ab[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	MOCK_IMPORTER_OPS(importer_ops, imp);
+	struct dma_buf_attachment *attach;
+	struct dma_buf_mapping *mapping;
+	struct dma_buf *dmabuf;
+	struct device *dev;
+
+	mock_type_a.map = mock_type_map;
+	mock_type_a.unmap = mock_type_unmap;
+	mock_type_b.map = mock_type_map;
+	mock_type_b.unmap = mock_type_unmap;
+
+	dev = kunit_device_register(test, "dma-buf-test");
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	dmabuf = mock_dmabuf_export(&mock_dmabuf_select_ops);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmabuf);
+
+	mock_exp_offer = offer_ab;
+	mock_exp_offer_len = ARRAY_SIZE(offer_ab);
+	attach = dma_buf_dynamic_attach(dmabuf, dev, &importer_ops, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, attach);
+	KUNIT_EXPECT_FALSE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_a);
+
+	dma_resv_lock(dmabuf->resv, NULL);
+
+	/* Exporter picks the second entry (mock_type_b) */
+	mock_select_idx = 1;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_b);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_b);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+
+	/* A pin taken whilst mapping is live gets the same type */
+	mock_select_idx = 0;
+	KUNIT_EXPECT_EQ(test, dma_buf_pin(attach), 0);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_b);
+	dma_buf_unpin(attach);
+
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+
+	/*
+	 * Unmapped the exporter would now pick A, but no invalidate has
+	 * happened so remap must get B again to avoid ping-ponging.
+	 */
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_b);
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+
+	/* Only an explicit invalidation allows the type to change */
+	dma_buf_invalidate_mappings(dmabuf);
+	KUNIT_EXPECT_FALSE(test, attach->selected);
+	/* Nothing is reselected until the next pin or map */
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_b);
+
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_a);
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+
+	/* Exporter cannot serve this importer right now */
+	dma_buf_invalidate_mappings(dmabuf);
+	mock_select_err = -EBUSY;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_EXPECT_EQ(test, PTR_ERR(mapping), -EBUSY);
+	KUNIT_EXPECT_FALSE(test, attach->selected);
+	mock_select_err = 0;
+
+	dma_resv_unlock(dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	dma_buf_put(dmabuf);
+
+	mock_type_a.map = NULL;
+	mock_type_a.unmap = NULL;
+	mock_type_b.map = NULL;
+	mock_type_b.unmap = NULL;
+}
+
+/*
+ * Pin selects too and the choice survives map, unmap and unpin. A pinned
+ * importer that is never invalidated keeps its first type for the life of the
+ * attachment.
+ */
+static void test_select_at_pin(struct kunit *test)
+{
+	static const struct dma_buf_mapping_match offer_ab[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	MOCK_IMPORTER_OPS(importer_ops, imp);
+	struct dma_buf_attachment *attach;
+	struct dma_buf_mapping *mapping;
+	struct dma_buf *dmabuf;
+	struct device *dev;
+
+	mock_type_a.map = mock_type_map;
+	mock_type_a.unmap = mock_type_unmap;
+	mock_type_b.map = mock_type_map;
+	mock_type_b.unmap = mock_type_unmap;
+
+	dev = kunit_device_register(test, "dma-buf-test");
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	dmabuf = mock_dmabuf_export(&mock_dmabuf_select_ops);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmabuf);
+
+	mock_exp_offer = offer_ab;
+	mock_exp_offer_len = ARRAY_SIZE(offer_ab);
+	attach = dma_buf_dynamic_attach(dmabuf, dev, &importer_ops, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, attach);
+
+	dma_resv_lock(dmabuf->resv, NULL);
+
+	mock_select_idx = 1;
+	KUNIT_EXPECT_EQ(test, dma_buf_pin(attach), 0);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_b);
+
+	/* Map after pin uses the pinned type regardless of the exporter now */
+	mock_select_idx = 0;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_b);
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_b);
+
+	dma_buf_unpin(attach);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_b);
+
+	dma_resv_unlock(dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	dma_buf_put(dmabuf);
+
+	mock_type_a.map = NULL;
+	mock_type_a.unmap = NULL;
+	mock_type_b.map = NULL;
+	mock_type_b.unmap = NULL;
+}
+
+/*
+ * If the first map/pin fails it shouldn't pin the selection. However all
+ * subsequent map/pin failure must not reset the selection state, otherwise a
+ * retry could reselect without an invalidate.
+ */
+static void test_select_survives_failed_map(struct kunit *test)
+{
+	static const struct dma_buf_mapping_match offer_ab[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	MOCK_IMPORTER_OPS(importer_ops, imp);
+	struct dma_buf_attachment *attach;
+	struct dma_buf_mapping *mapping;
+	struct dma_buf *dmabuf;
+	struct device *dev;
+
+	mock_type_a.map = mock_type_map;
+	mock_type_a.unmap = mock_type_unmap;
+	mock_type_b.map = mock_type_map;
+	mock_type_b.unmap = mock_type_unmap;
+
+	dev = kunit_device_register(test, "dma-buf-test");
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	dmabuf = mock_dmabuf_export(&mock_dmabuf_select_ops);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmabuf);
+
+	mock_exp_offer = offer_ab;
+	mock_exp_offer_len = ARRAY_SIZE(offer_ab);
+	attach = dma_buf_dynamic_attach(dmabuf, dev, &importer_ops, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, attach);
+
+	dma_resv_lock(dmabuf->resv, NULL);
+
+	/* A map that selected and then failed does not change the selected state */
+	mock_select_idx = 1;
+	mock_map_err = -EIO;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_EXPECT_EQ(test, PTR_ERR(mapping), -EIO);
+	KUNIT_EXPECT_FALSE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_a);
+	mock_map_err = 0;
+
+	/* Pin selects B and holds it */
+	KUNIT_EXPECT_EQ(test, dma_buf_pin(attach), 0);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_b);
+	KUNIT_EXPECT_EQ(test, mock_pin_count, 1);
+
+	/* A map failing under that pin must not allow a different selection */
+	mock_select_idx = 0;
+	mock_map_err = -EIO;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_EXPECT_EQ(test, PTR_ERR(mapping), -EIO);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_b);
+	KUNIT_EXPECT_EQ(test, mock_pin_count, 1);
+	mock_map_err = 0;
+
+	/* The retry gets the pinned type, not a new selection */
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_b);
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+
+	dma_buf_unpin(attach);
+	KUNIT_EXPECT_EQ(test, mock_pin_count, 0);
+
+	dma_resv_unlock(dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	dma_buf_put(dmabuf);
+
+	mock_type_a.map = NULL;
+	mock_type_a.unmap = NULL;
+	mock_type_b.map = NULL;
+	mock_type_b.unmap = NULL;
+}
+
+/*
+ * The same rule for a failing exporter pin. A pin that selected and then
+ * failed resets the selection state, and one that failed after an existing map
+ * does not.
+ */
+static void test_select_after_failed_pin(struct kunit *test)
+{
+	static const struct dma_buf_mapping_match offer_ab[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	MOCK_IMPORTER_OPS(importer_ops, imp);
+	struct dma_buf_attachment *attach;
+	struct dma_buf_mapping *mapping;
+	struct dma_buf *dmabuf;
+	struct device *dev;
+
+	mock_type_a.map = mock_type_map;
+	mock_type_a.unmap = mock_type_unmap;
+	mock_type_b.map = mock_type_map;
+	mock_type_b.unmap = mock_type_unmap;
+
+	dev = kunit_device_register(test, "dma-buf-test");
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	dmabuf = mock_dmabuf_export(&mock_dmabuf_select_ops);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmabuf);
+
+	mock_exp_offer = offer_ab;
+	mock_exp_offer_len = ARRAY_SIZE(offer_ab);
+	attach = dma_buf_dynamic_attach(dmabuf, dev, &importer_ops, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, attach);
+
+	dma_resv_lock(dmabuf->resv, NULL);
+
+	/* First pin selects B but the exporter cannot pin: nothing is kept */
+	mock_select_idx = 1;
+	mock_pin_err = -ENOSPC;
+	KUNIT_EXPECT_EQ(test, dma_buf_pin(attach), -ENOSPC);
+	KUNIT_EXPECT_FALSE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_a);
+	KUNIT_EXPECT_EQ(test, mock_pin_count, 0);
+	mock_pin_err = 0;
+
+	/* A live mapping selects A */
+	mock_select_idx = 0;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_a);
+
+	/* A pin failing under it leaves that selection in place */
+	mock_select_idx = 1;
+	mock_pin_err = -ENOSPC;
+	KUNIT_EXPECT_EQ(test, dma_buf_pin(attach), -ENOSPC);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_a);
+	mock_pin_err = 0;
+
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+	dma_resv_unlock(dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	dma_buf_put(dmabuf);
+
+	mock_type_a.map = NULL;
+	mock_type_a.unmap = NULL;
+	mock_type_b.map = NULL;
+	mock_type_b.unmap = NULL;
+}
+
 static const struct dma_exporter dma_exporter_params[] = {
 	{ &mock_dmabuf_simple_sgt_ops, "simple_sgt" },
 	{ &mock_dmabuf_two_exp_ops, "two_exp" },
@@ -381,6 +893,190 @@ static const struct dma_exporter dma_exporter_params[] = {
 
 KUNIT_ARRAY_PARAM_DESC(dma_exporter, dma_exporter_params, desc);
 
+/*
+ * After an invalidate the exporter may select another type for the next map
+ * while a mapping made with the previous selection is still live. Since
+ * importers don't have to unmap on invalidate the attachment selection may
+ * have changed by the time unmap is called. Therefore every mapping must be
+ * torn down with the type it was made with, and the new selection governs new
+ * maps only not unmap.
+ */
+static void test_unmap_uses_own_type(struct kunit *test)
+{
+	static const struct dma_buf_mapping_match offer_ab[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	MOCK_IMPORTER_OPS(importer_ops, imp);
+	struct dma_buf_mapping *mapping, *mapping2;
+	struct dma_buf_attachment *attach;
+	struct dma_buf *dmabuf;
+	struct device *dev;
+
+	mock_type_a.map = mock_type_map;
+	mock_type_a.unmap = mock_type_unmap;
+	mock_type_b.map = mock_type_map;
+	mock_type_b.unmap = mock_type_unmap;
+
+	dev = kunit_device_register(test, "dma-buf-test");
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	dmabuf = mock_dmabuf_export(&mock_dmabuf_select_ops);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmabuf);
+
+	mock_exp_offer = offer_ab;
+	mock_exp_offer_len = ARRAY_SIZE(offer_ab);
+	attach = dma_buf_dynamic_attach(dmabuf, dev, &importer_ops, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, attach);
+
+	dma_resv_lock(dmabuf->resv, NULL);
+
+	/* Mapped with the exporter's second entry */
+	mock_select_idx = 1;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_b);
+	KUNIT_EXPECT_PTR_EQ(test, mapping->match, &attach->matches[1]);
+	KUNIT_EXPECT_EQ(test, mock_select_count, 1);
+
+	/* Invalidated while that mapping is live: the next map reselects */
+	dma_buf_invalidate_mappings(dmabuf);
+	KUNIT_EXPECT_FALSE(test, attach->selected);
+	mock_select_idx = 0;
+	mapping2 = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping2);
+	KUNIT_EXPECT_PTR_EQ(test, mapping2->type, &mock_type_a);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_EQ(test, mock_select_count, 2);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_a);
+
+	/* The old mapping is still torn down as B, the new one as A */
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+	KUNIT_EXPECT_PTR_EQ(test, mock_unmap_type, &mock_type_b);
+	KUNIT_EXPECT_PTR_EQ(test, mock_unmap_made_by, &mock_type_b);
+	/* The selection itself is untouched by the unmap */
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_a);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+
+	dma_buf_mapping_unmap(attach, mapping2, DMA_BIDIRECTIONAL);
+	KUNIT_EXPECT_PTR_EQ(test, mock_unmap_type, &mock_type_a);
+	KUNIT_EXPECT_PTR_EQ(test, mock_unmap_made_by, &mock_type_a);
+
+	/* Nothing live, but the selection stands until the next invalidate */
+	mock_select_idx = 1;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_a);
+	KUNIT_EXPECT_EQ(test, mock_select_count, 2);
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+
+	dma_resv_unlock(dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	dma_buf_put(dmabuf);
+
+	mock_type_a.map = NULL;
+	mock_type_a.unmap = NULL;
+	mock_type_b.map = NULL;
+	mock_type_b.unmap = NULL;
+}
+
+/*
+ * An exporter that moves the buffer during map/pin after invalidating must not
+ * leave the attachment in an unselected state after the map/pin succeeds. Once
+ * a mapping succeeds it must lock in the selection made after the invalidate and
+ * move and subsequent map/pin operations must get the same type.
+ */
+static void test_select_survives_own_invalidate(struct kunit *test)
+{
+	static const struct dma_buf_mapping_match offer_ab[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	MOCK_IMPORTER_OPS(importer_ops, imp);
+	struct dma_buf_mapping *mapping, *mapping2;
+	struct dma_buf_attachment *attach;
+	struct dma_buf *dmabuf;
+	struct device *dev;
+
+	mock_type_a.map = mock_type_map;
+	mock_type_a.unmap = mock_type_unmap;
+	mock_type_b.map = mock_type_map;
+	mock_type_b.unmap = mock_type_unmap;
+
+	dev = kunit_device_register(test, "dma-buf-test");
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	dmabuf = mock_dmabuf_export(&mock_dmabuf_select_ops);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dmabuf);
+
+	mock_exp_offer = offer_ab;
+	mock_exp_offer_len = ARRAY_SIZE(offer_ab);
+	attach = dma_buf_dynamic_attach(dmabuf, dev, &importer_ops, NULL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, attach);
+
+	mock_invalidate_in_callback = true;
+	dma_resv_lock(dmabuf->resv, NULL);
+
+	/* The map's move invalidates the attachment selection. */
+	mock_select_idx = 1;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_mapping_type_of(mapping),
+			    &mock_type_b);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_EQ(test, mock_select_count, 1);
+
+	/* A second map does not reselect. */
+	mock_select_idx = 0;
+	mapping2 = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping2);
+	KUNIT_EXPECT_PTR_EQ(test, mapping2->type, &mock_type_b);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_EQ(test, mock_select_count, 1);
+
+	/* Nor does a pin taken under it */
+	KUNIT_EXPECT_EQ(test, dma_buf_pin(attach), 0);
+	KUNIT_EXPECT_TRUE(test, attach->selected);
+	KUNIT_EXPECT_EQ(test, mock_select_count, 1);
+	dma_buf_unpin(attach);
+
+	dma_buf_mapping_unmap(attach, mapping2, DMA_BIDIRECTIONAL);
+	dma_buf_mapping_unmap(attach, mapping, DMA_BIDIRECTIONAL);
+
+	/* A map that invalidates and then fails establishes nothing */
+	dma_buf_invalidate_mappings(dmabuf);
+	mock_map_err = -EIO;
+	mapping = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_EXPECT_EQ(test, PTR_ERR(mapping), -EIO);
+	KUNIT_EXPECT_FALSE(test, attach->selected);
+	KUNIT_EXPECT_PTR_EQ(test, dma_buf_attach_map_type(attach)->type,
+			    &mock_type_a);
+	mock_map_err = 0;
+
+	dma_resv_unlock(dmabuf->resv);
+
+	dma_buf_detach(dmabuf, attach);
+	dma_buf_put(dmabuf);
+
+	mock_type_a.map = NULL;
+	mock_type_a.unmap = NULL;
+	mock_type_b.map = NULL;
+	mock_type_b.unmap = NULL;
+}
+
 static struct kunit_case dma_mapping_cases[] = {
 	KUNIT_CASE(test_match_fail),
 	KUNIT_CASE(test_match_no_match_callback),
@@ -389,6 +1085,13 @@ static struct kunit_case dma_mapping_cases[] = {
 	KUNIT_CASE(test_match_importer_priority),
 	KUNIT_CASE_PARAM(test_sgt_attach, dma_exporter_gen_params),
 	KUNIT_CASE(test_mock_attach),
+	KUNIT_CASE(test_match_set_is_final),
+	KUNIT_CASE(test_select_at_map),
+	KUNIT_CASE(test_select_at_pin),
+	KUNIT_CASE(test_select_survives_failed_map),
+	KUNIT_CASE(test_select_after_failed_pin),
+	KUNIT_CASE(test_unmap_uses_own_type),
+	KUNIT_CASE(test_select_survives_own_invalidate),
 	{}
 };
 
Signature · 1 line
2.54.0
#28 lore raw

Re: [PATCH v3 24/24] dma-buf: Add kunit tests for mapping type selection

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] dma-buf: build failure from mapping2->type access in selection KUnit tests - [Low] dma-buf…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] dma-buf: build failure from mapping2->type access in selection KUnit tests
- [Low] dma-buf: duplicated mock_type_a and mock_type_b callback setup across tests
- [Low] dma-buf: commit message omits failed map and pin selection KUnit tests
--

commit fdd7641c86ac3670b97b442d9f583c8418cdf65b
Author: Alistair Popple <[email protected]>

Quoted 28 lines

dma-buf: Add kunit tests for mapping type selection

Add an offer list and a select_mapping() callback to the mock exporter along with map/unmap for the two mock types.

Adds tests for the exporter/importer contract.

- test_match_set_is_final: a later importer attaching does not change an existing attachment's match set or default. Later attachments influence what the exporter selects at pin and map time but not previous attachments.

- test_select_at_map: the exporter chooses a non-default entry on first map and the result is tagged with it. A pin taken while there is a live mapping inherits the type and the choice survives unmap/remap. Only dma_buf_invalidate_mappings() allows the exporter to make a different selection. An -EBUSY from selection leaves nothing established.

- test_select_at_pin: pin selects, and the choice survives map, unmap and unpin so a pinned importer that is never invalidated keeps its first type for the life of the import.

- test_unmap_uses_own_type: after an invalidate the exporter may select another type for the next map. A mapping made with the previous one is still live, and each mapping must be torn down with the type it was made with.

Quoted 5 lines

- test_select_survives_own_invalidate: a pin or map whose exporter callback invalidates every attachment still ends up selected, and one that invalidates and fails does not.

[Severity: Low]
This isn't a bug, but the commit message lists five of the new test cases while omitting test_select_survives_failed_map and test_select_after_failed_pin.

Quoted 4 lines
drivers/dma-buf/st-dma-mapping.c +0 -0
index 8c69ae212455c..42e6748f0ae65 100644

[ ... ]

Quoted 21 lines
drivers/dma-buf/st-dma-mapping.c +20 -0
@@ -374,6 +407,485 @@ static void test_mock_attach(struct kunit *test)
+static void test_select_at_map(struct kunit *test)
+{
+	static const struct dma_buf_mapping_match offer_ab[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	struct dma_buf_mapping_match imp[] = {
+		{ .type = &mock_type_a },
+		{ .type = &mock_type_b },
+	};
+	MOCK_IMPORTER_OPS(importer_ops, imp);
+	struct dma_buf_attachment *attach;
+	struct dma_buf_mapping *mapping;
+	struct dma_buf *dmabuf;
+	struct device *dev;
+
+	mock_type_a.map = mock_type_map;
+	mock_type_a.unmap = mock_type_unmap;
+	mock_type_b.map = mock_type_map;
+	mock_type_b.unmap = mock_type_unmap;

[ ... ]

drivers/dma-buf/st-dma-mapping.c static void test_mock_attach(struct kunit *test) · line 650

Quoted 8 lines
drivers/dma-buf/st-dma-mapping.c +8 -0
+	dma_buf_detach(dmabuf, attach);
+	dma_buf_put(dmabuf);
+
+	mock_type_a.map = NULL;
+	mock_type_a.unmap = NULL;
+	mock_type_b.map = NULL;
+	mock_type_b.unmap = NULL;
+}

[Severity: Low]
This isn't a bug, but all six selection tests assign and clear .map and .unmap on mock_type_a and mock_type_b, and an early KUNIT_ASSERT failure skips the teardown since reset_mock_state() does not reset them. Can mock_type_a and mock_type_b initialize .map = mock_type_map and .unmap = mock_type_unmap statically (or manage them in reset_mock_state())?

Quoted 3 lines
drivers/dma-buf/st-dma-mapping.c +2 -0
@@ -381,6 +893,190 @@ static const struct dma_exporter dma_exporter_params[] = {
+static void test_unmap_uses_own_type(struct kunit *test)
+{

[ ... ]

Quoted 7 lines
drivers/dma-buf/st-dma-mapping.c +7 -0
+	/* Invalidated while that mapping is live: the next map reselects */
+	dma_buf_invalidate_mappings(dmabuf);
+	KUNIT_EXPECT_FALSE(test, attach->selected);
+	mock_select_idx = 0;
+	mapping2 = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping2);
+	KUNIT_EXPECT_PTR_EQ(test, mapping2->type, &mock_type_a);

[ ... ]

Quoted 2 lines
drivers/dma-buf/st-dma-mapping.c +2 -0
+static void test_select_survives_own_invalidate(struct kunit *test)
+{

[ ... ]

drivers/dma-buf/st-dma-mapping.c static const struct dma_exporter dma_exporter_params[] = { · line 1046

Quoted 5 lines
drivers/dma-buf/st-dma-mapping.c +5 -0
+	/* A second map does not reselect. */
+	mock_select_idx = 0;
+	mapping2 = dma_buf_mapping_map(attach, DMA_BIDIRECTIONAL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mapping2);
+	KUNIT_EXPECT_PTR_EQ(test, mapping2->type, &mock_type_b);

[Severity: High]
Does accessing mapping2->type break the build when CONFIG_DMABUF_KUNIT_TEST is enabled?

struct dma_buf_mapping only has a match member, not type, so these two checks in test_unmap_uses_own_type() and test_select_survives_own_invalidate() should use dma_buf_mapping_type_of(mapping2).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
#29 lore raw
In reply to Alistair Popple
Sashiko has finished reviewing this 24-patch series:

Sashiko has finished reviewing this 24-patch series:

- Patches 1-12 and 14-22: no issues found.
- Patch 13 ("udmabuf: Convert to the SGT mapping type"): 1 potential issue (1 High)
- Patch 23 ("dma-buf: Add dma_buf_mapping_map() returning a type-..."):
1 potential issue (1 Low)
- Patch 24 ("dma-buf: Add kunit tests for mapping type selection"): 3 potential issues (1 High, 2 Low)

Detailed reviews have been sent as separate replies to patches 13, 23, and 24.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]