Linux RDMA and InfiniBand development
 help / color / mirror / Atom feed
From: Alistair Popple <apopple@nvidia.com>
To: "Christian König" <christian.koenig@amd.com>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Simona Vetter" <simona.vetter@ffwll.ch>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Vivek Kasireddy" <vivek.kasireddy@intel.com>,
	"Dongwon Kim" <dongwon.kim@intel.com>,
	"Kevin Tian" <kevin.tian@intel.com>,
	"Xu Yilun" <yilun.xu@linux.intel.com>,
	"Karl Mehltretter" <kmehltretter@gmail.com>,
	"Baolu Lu" <baolu.lu@linux.intel.com>,
	"Felix Kuehling" <felix.kuehling@amd.com>,
	"Alex Deucher" <alexander.deucher@amd.com>,
	linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	intel-xe@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	linux-media@vger.kernel.org, linaro-mm-sig@lists.linaro.org,
	linux-rdma@vger.kernel.org, iommu@lists.linux.dev,
	"Keith Busch" <kbusch@kernel.org>, "Jens Axboe" <axboe@kernel.dk>,
	"Christoph Hellwig" <hch@lst.de>,
	"Sagi Grimberg" <sagi@grimberg.me>,
	linux-nvme@lists.infradead.org,
	"David S. Miller" <davem@davemloft.net>,
	"Eric Dumazet" <edumazet@kernel.org>,
	"Jakub Kicinski" <kuba@kernel.org>,
	"Paolo Abeni" <pabeni@redhat.com>,
	"Simon Horman" <horms@kernel.org>,
	"Mina Almasry" <almasrymina@google.com>,
	netdev@vger.kernel.org, "Alexey Kardashevskiy" <aik@amd.com>,
	zhipingz@meta.com, treding@nvidia.com, jhubbard@nvidia.com,
	chaitanyak@nvidia.com, "Jason Gunthorpe" <jgg@ziepe.ca>,
	"Leon Romanovsky" <leon@kernel.org>
Cc: Alistair Popple <apopple@nvidia.com>
Subject: [PATCH v3 10/24] drm: Convert drm_gem_map_dma_buf() and its users to the SGT mapping type
Date: Fri, 20 Nov 2026 16:55:48 +1100	[thread overview]
Message-ID: <20261120-dma_mapping_type_v4-v3-10-c0183a409bdf@nvidia.com> (raw)
In-Reply-To: <20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com>

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.

Co-developed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
 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(-)

diff --git a/drivers/accel/amdxdna/amdxdna_gem.c b/drivers/accel/amdxdna/amdxdna_gem.c
index adee6e806eed..74b8e86638b1 100644
--- a/drivers/accel/amdxdna/amdxdna_gem.c
+++ b/drivers/accel/amdxdna/amdxdna_gem.c
@@ -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)
diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index e27bda3423ed..e4a2a1de371c 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -28,6 +28,7 @@
 
 #include <linux/export.h>
 #include <linux/dma-buf.h>
+#include <linux/dma-buf-mapping.h>
 #include <linux/rbtree.h>
 #include <linux/module.h>
 
@@ -587,6 +588,16 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  * option for sharing lots of buffers for rendering.
  */
 
+static bool is_gem_map_dma_buf(struct dma_buf_attachment *attach)
+{
+	const struct dma_buf_mapping_sgt_exp_ops *sgt_exp_ops =
+		dma_buf_get_sgt_ops(attach);
+
+	if (sgt_exp_ops && sgt_exp_ops->map_dma_buf == drm_gem_map_dma_buf)
+		return true;
+	return false;
+}
+
 /**
  * drm_gem_map_attach - dma_buf attach implementation for GEM
  * @dma_buf: buffer to attach device to
@@ -608,7 +619,7 @@ int drm_gem_map_attach(struct dma_buf *dma_buf,
 	 * drm_gem_map_dma_buf() requires obj->get_sg_table(), but drivers
 	 * that implement their own ->map_dma_buf() do not.
 	 */
-	if (dma_buf->ops->map_dma_buf == drm_gem_map_dma_buf &&
+	if (is_gem_map_dma_buf(attach) &&
 	    !obj->funcs->get_sg_table)
 		return -ENOSYS;
 
@@ -657,16 +668,18 @@ EXPORT_SYMBOL(drm_gem_map_detach);
  * @dir: direction of DMA transfer
  *
  * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This
- * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together
- * with drm_gem_unmap_dma_buf().
+ * can be used as the &dma_buf_mapping_sgt_exp_ops.map_dma_buf callback. Should
+ * be used together with drm_gem_unmap_dma_buf().
  *
- * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR
- * on error. May return -EINTR if it is interrupted by a signal.
+ * Returns: the mapping containing the scatterlist to be returned; returns
+ * ERR_PTR on error. May return -EINTR if it is interrupted by a signal.
  */
-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)
 {
 	struct drm_gem_object *obj = attach->dmabuf->priv;
+	struct dma_buf_sgt_mapping *mapping;
 	struct sg_table *sgt;
 	int ret;
 
@@ -676,40 +689,51 @@ struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
 	if (WARN_ON(!obj->funcs->get_sg_table))
 		return ERR_PTR(-ENOSYS);
 
+	mapping = kzalloc_obj(*mapping);
+	if (!mapping)
+		return ERR_PTR(-ENOMEM);
+
 	sgt = obj->funcs->get_sg_table(obj);
-	if (IS_ERR(sgt))
-		return sgt;
+	if (IS_ERR(sgt)) {
+		kfree(mapping);
+		return ERR_CAST(sgt);
+	}
 
-	ret = dma_map_sgtable(attach->dev, sgt, dir,
-			      DMA_ATTR_SKIP_CPU_SYNC);
+	/* Take over the table header; the copy now owns the scatterlist. */
+	mapping->sgt = *sgt;
+	kfree(sgt);
+
+	ret = dma_map_sgtable(dma_buf_sgt_dma_device(attach), &mapping->sgt,
+			      dir, DMA_ATTR_SKIP_CPU_SYNC);
 	if (ret) {
-		sg_free_table(sgt);
-		kfree(sgt);
-		sgt = ERR_PTR(ret);
+		sg_free_table(&mapping->sgt);
+		kfree(mapping);
+		return ERR_PTR(ret);
 	}
 
-	return sgt;
+	return mapping;
 }
 EXPORT_SYMBOL(drm_gem_map_dma_buf);
 
 /**
  * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
  * @attach: attachment to unmap buffer from
- * @sgt: scatterlist info of the buffer to unmap
+ * @mapping: mapping of the buffer to unmap
  * @dir: direction of DMA transfer
  *
- * This can be used as the &dma_buf_ops.unmap_dma_buf callback.
+ * This can be used as the &dma_buf_mapping_sgt_exp_ops.unmap_dma_buf callback.
  */
 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)
 {
-	if (!sgt)
+	if (!mapping)
 		return;
 
-	dma_unmap_sgtable(attach->dev, sgt, dir, DMA_ATTR_SKIP_CPU_SYNC);
-	sg_free_table(sgt);
-	kfree(sgt);
+	dma_unmap_sgtable(dma_buf_sgt_mapping_dma_device(mapping),
+			  &mapping->sgt, dir, DMA_ATTR_SKIP_CPU_SYNC);
+	sg_free_table(&mapping->sgt);
+	kfree(mapping);
 }
 EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
 
@@ -827,12 +851,12 @@ EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
 static const struct dma_buf_ops drm_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 = 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(drm_gem_map_dma_buf,
+				     drm_gem_unmap_dma_buf),
 };
 
 /**
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c
index 4ab54d2e616f..41210ca14a92 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c
@@ -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)
diff --git a/drivers/gpu/drm/msm/msm_gem_prime.c b/drivers/gpu/drm/msm/msm_gem_prime.c
index e1e74e9b7e6a..b6aae3c85250 100644
--- a/drivers/gpu/drm/msm/msm_gem_prime.c
+++ b/drivers/gpu/drm/msm/msm_gem_prime.c
@@ -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");
diff --git a/drivers/gpu/drm/panfrost/panfrost_gem.c b/drivers/gpu/drm/panfrost/panfrost_gem.c
index 518f24549b9d..e54818736a31 100644
--- a/drivers/gpu/drm/panfrost/panfrost_gem.c
+++ b/drivers/gpu/drm/panfrost/panfrost_gem.c
@@ -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 *
diff --git a/drivers/gpu/drm/panthor/panthor_gem.c b/drivers/gpu/drm/panthor/panthor_gem.c
index 22b57ab6f929..002fea6e5b06 100644
--- a/drivers/gpu/drm/panthor/panthor_gem.c
+++ b/drivers/gpu/drm/panthor/panthor_gem.c
@@ -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 *
diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h
index f3bbbe4468bf..35956816b6e2 100644
--- a/drivers/gpu/drm/virtio/virtgpu_drv.h
+++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
@@ -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);
 
diff --git a/drivers/gpu/drm/virtio/virtgpu_prime.c b/drivers/gpu/drm/virtio/virtgpu_prime.c
index a47783b47dcc..a01c70f4d6e8 100644
--- a/drivers/gpu/drm/virtio/virtgpu_prime.c
+++ b/drivers/gpu/drm/virtio/virtgpu_prime.c
@@ -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,
diff --git a/drivers/gpu/drm/virtio/virtgpu_vram.c b/drivers/gpu/drm/virtio/virtgpu_vram.c
index 01241ce4d07c..0a430b04cc2f 100644
--- a/drivers/gpu/drm/virtio/virtgpu_vram.c
+++ b/drivers/gpu/drm/virtio/virtgpu_vram.c
@@ -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 = {
diff --git a/include/drm/drm_prime.h b/include/drm/drm_prime.h
index f50f862f0d8b..4fceac922a7a 100644
--- a/include/drm/drm_prime.h
+++ b/include/drm/drm_prime.h
@@ -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);

-- 
2.54.0


  parent reply	other threads:[~2026-11-20  5:57 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  5:55 [PATCH v3 00/24] DMA-buf mapping types Alistair Popple
2026-11-20  5:55 ` [PATCH v3 01/24] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-11-20  5:55 ` [PATCH v3 02/24] dma-buf: Introduce DMA-buf mapping types Alistair Popple
2026-11-20  5:55 ` [PATCH v3 03/24] dma-buf: Add the SGT DMA mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 04/24] dma-buf: Negotiate the mapping type in dma_buf_dynamic_attach() Alistair Popple
2026-11-20  5:55 ` [PATCH v3 05/24] dma-buf: Route SGT related actions through the attachment's mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 06/24] dma-buf: Allow single exporter drivers to avoid the match_mapping function Alistair Popple
2026-11-20  5:55 ` [PATCH v3 07/24] dma-buf: Convert exporters allocating a table per map to the SGT mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 08/24] iommufd/selftest: Convert the test exporter " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 09/24] dma-buf: Convert exporters keeping a table per attachment " Alistair Popple
2026-11-20  5:55 ` Alistair Popple [this message]
2026-11-20  5:55 ` [PATCH v3 11/24] drm/xe/dma-buf: Use " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 12/24] drm/amdgpu: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 13/24] udmabuf: Convert to " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 14/24] drm/omapdrm: " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 15/24] accel/ivpu: Convert the userptr exporter " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 16/24] drm/vmwgfx: Use match_mapping instead of dummy calls Alistair Popple
2026-11-20  5:55 ` [PATCH v3 17/24] dma-buf: Convert dma_buf_phys_vec_to_sgt() and its users to the SGT mapping type Alistair Popple
2026-11-20  5:55 ` [PATCH v3 18/24] iio: buffer: convert to use " Alistair Popple
2026-11-20  5:55 ` [PATCH v3 19/24] functionfs: " Alistair Popple

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120-dma_mapping_type_v4-v3-10-c0183a409bdf@nvidia.com \
    --to=apopple@nvidia.com \
    --cc=aik@amd.com \
    --cc=alexander.deucher@amd.com \
    --cc=almasrymina@google.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=axboe@kernel.dk \
    --cc=baolu.lu@linux.intel.com \
    --cc=chaitanyak@nvidia.com \
    --cc=christian.koenig@amd.com \
    --cc=davem@davemloft.net \
    --cc=dongwon.kim@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=edumazet@kernel.org \
    --cc=felix.kuehling@amd.com \
    --cc=hch@lst.de \
    --cc=horms@kernel.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=kbusch@kernel.org \
    --cc=kevin.tian@intel.com \
    --cc=kmehltretter@gmail.com \
    --cc=kuba@kernel.org \
    --cc=leon@kernel.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sagi@grimberg.me \
    --cc=simona.vetter@ffwll.ch \
    --cc=sumit.semwal@linaro.org \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=treding@nvidia.com \
    --cc=vivek.kasireddy@intel.com \
    --cc=yilun.xu@linux.intel.com \
    --cc=zhipingz@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox