Linux Media Controller 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 03/24] dma-buf: Add the SGT DMA mapping type
Date: Fri, 20 Nov 2026 16:55:41 +1100	[thread overview]
Message-ID: <20261120-dma_mapping_type_v4-v3-3-c0183a409bdf@nvidia.com> (raw)
In-Reply-To: <20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com>

From: Jason Gunthorpe <jgg@nvidia.com>

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.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Alistair Popple <apopple@nvidia.com>

---

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

diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
index d27f02610c2a..cc3d8d6fe009 100644
--- a/drivers/dma-buf/dma-buf-mapping.c
+++ b/drivers/dma-buf/dma-buf-mapping.c
@@ -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));
+}
diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h
index bb826bdc2911..8be03b4ba6cc 100644
--- a/include/linux/dma-buf-mapping.h
+++ b/include/linux/dma-buf-mapping.h
@@ -84,6 +84,30 @@ struct dma_buf_mapping_type {
 			     struct dma_buf_attachment *attach);
 };
 
+/**
+ * struct dma_buf_mapping - header of a mapping made through an attachment
+ * @match: The match set entry the mapping was made with
+ *
+ * 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
+ * producer's map callback has returned and reads it to tear the mapping down.
+ */
+struct dma_buf_mapping {
+	const struct dma_buf_mapping_match *match;
+};
+
+/**
+ * dma_buf_mapping_type_of - The type a mapping was made with
+ * @mapping: the mapping
+ *
+ * Returns: the type of the match set entry recorded in @mapping.
+ */
+static inline const struct dma_buf_mapping_type *
+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,
@@ -109,4 +133,115 @@ dma_buf_attach_map_type(const struct dma_buf_attachment *attach)
 	return &attach->matches[0];
 }
 
+/*
+ * DMA Mapped Scatterlist Type
+ *
+ * When this type is matched the map/unmap functions are:
+ *
+ *  dma_buf_map_sgt()
+ *  dma_buf_unmap_sgt()
+ *
+ * The struct sg_table returned by those functions has only the DMA portions
+ * available. The caller must not try to use the struct page * information.
+ *
+ * importing_dma_device is the device passed to dma_buf_dynamic_attach(),
+ * recorded at match time, and is passed to the DMA API to provide the
+ * dma_addr_t's.
+ */
+extern const struct dma_buf_mapping_type dma_buf_mapping_sgt_type;
+
+/**
+ * struct dma_buf_sgt_mapping - A mapping of the SGT type
+ * @base: Common header, filled in by the core
+ * @sgt: The DMA mapped scatterlist, filled in by the exporter
+ *
+ * Allocated by the exporter's &dma_buf_mapping_sgt_exp_ops.map_dma_buf, which
+ * may embed it in a larger structure of its own, and released by its
+ * unmap_dma_buf. The core only fills in @base, with the same values for every
+ * map of an attachment, so an exporter that keeps one table per attachment
+ * may keep one of these per attachment and return it on every map. A mapping
+ * must not be shared between attachments. Importers are handed @sgt by
+ * dma_buf_map_sgt().
+ */
+struct dma_buf_sgt_mapping {
+	struct dma_buf_mapping base;
+	struct sg_table sgt;
+};
+
+/**
+ * struct dma_buf_mapping_sgt_exp_ops - Exporter ops of the SGT type
+ * @ops: Type tag
+ * @map_dma_buf: Produce a &struct dma_buf_sgt_mapping for @attach, with
+ *               &dma_buf_sgt_mapping.sgt mapped for the device returned by
+ *               dma_buf_sgt_dma_device(). Called with the dma_resv lock held.
+ *               Returns the mapping, or an ERR_PTR; NULL is taken as -ENOMEM.
+ * @unmap_dma_buf: Unmap and release a mapping returned by @map_dma_buf.
+ */
+struct dma_buf_mapping_sgt_exp_ops {
+	struct dma_buf_mapping_exp_ops ops;
+	struct dma_buf_sgt_mapping *(*map_dma_buf)(struct dma_buf_attachment *attach,
+						   enum dma_data_direction dir);
+	void (*unmap_dma_buf)(struct dma_buf_attachment *attach,
+			      struct dma_buf_sgt_mapping *mapping,
+			      enum dma_data_direction dir);
+};
+
+/**
+ * dma_buf_sgt_dma_device - Return the device to use for DMA mapping
+ * @attach: sgt mapping type attachment
+ *
+ * Called by the exporter to get the struct device to pass to the DMA API
+ * during the map callback. The unmap callback takes it from the mapping with
+ * dma_buf_sgt_mapping_dma_device().
+ *
+ * Returns: the importing device, or NULL if the attachment is not mapped with
+ * the SGT type.
+ */
+static inline struct device *
+dma_buf_sgt_dma_device(struct dma_buf_attachment *attach)
+{
+	if (dma_buf_attach_map_type(attach)->type != &dma_buf_mapping_sgt_type)
+		return NULL;
+	return dma_buf_attach_map_type(attach)->sgt_data.importing_dma_device;
+}
+
+static inline const struct dma_buf_mapping_sgt_exp_ops *
+dma_buf_get_sgt_ops(struct dma_buf_attachment *attach)
+{
+	if (dma_buf_attach_map_type(attach)->type != &dma_buf_mapping_sgt_type)
+		return NULL;
+	return container_of(dma_buf_attach_map_type(attach)->exp_ops,
+			    struct dma_buf_mapping_sgt_exp_ops, ops);
+}
+
+/**
+ * dma_buf_sgt_mapping_dma_device - Return the device a mapping was made for
+ * @mapping: the mapping passed to the exporter's unmap callback
+ *
+ * Called by the exporter to get the struct device to pass to the DMA API
+ * during the unmap callback. It comes from the match set entry recorded in
+ * the mapping, so it is the device the mapping was DMA mapped for whatever
+ * the attachment is mapped with by the time of the unmap.
+ *
+ * Returns: the device the mapping was DMA mapped for.
+ */
+static inline struct device *
+dma_buf_sgt_mapping_dma_device(const struct dma_buf_sgt_mapping *mapping)
+{
+	return mapping->base.match->sgt_data.importing_dma_device;
+}
+
+/*
+ * Importer SGT entry. Usable in a static table: the importing device is
+ * supplied by dma_buf_dynamic_attach() and recorded in the matched entry.
+ */
+#define DMA_BUF_IMAPPING_SGT()                                              \
+	((struct dma_buf_mapping_match){ .type = &dma_buf_mapping_sgt_type })
+#define DMA_BUF_EMAPPING_SGT(_exp_ops)                                      \
+	((struct dma_buf_mapping_match){ .type = &dma_buf_mapping_sgt_type, \
+					 .exp_ops = &((_exp_ops)->ops) })
+
+/* Match on behalf of an exporter that has no match_mapping() */
+int dma_buf_sgt_compat_match_mapping(struct dma_buf_match_args *args);
+
 #endif
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index 81bbcd420d60..6b423e22174a 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -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;
 	};
 };
 

-- 
2.54.0


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

Thread overview: 26+ 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 ` Alistair Popple [this message]
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  9:02   ` Karol Wachowski
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 ` [PATCH v3 10/24] drm: Convert drm_gem_map_dma_buf() and its users " Alistair Popple
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
2026-11-20  5:55 ` [PATCH v3 20/24] dma-buf: Remove unused SGT stuff from the common structures Alistair Popple
2026-11-20  5:55 ` [PATCH v3 21/24] dma-buf: Add kunit tests for mapping type Alistair Popple
2026-11-20  5:56 ` [PATCH v3 22/24] dma-buf: Let the exporter select mapping type at pin/map Alistair Popple
2026-11-20  5:56 ` [PATCH v3 23/24] dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt Alistair Popple
2026-11-20  5:56 ` [PATCH v3 24/24] dma-buf: Add kunit tests for mapping type selection 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-3-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