[PATCH v3 03/24] dma-buf: Add the SGT DMA mapping type
From: Alistair Popple
Date: Fri Nov 20 2026 - 00:57:32 EST
From: Jason Gunthorpe <jgg@xxxxxxxxxx>
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@xxxxxxxxxx>
Signed-off-by: Alistair Popple <apopple@xxxxxxxxxx>
---
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