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 23/24] dma-buf: Add dma_buf_mapping_map() returning a type-tagged mapping and implement it for sgt
Date: Fri, 20 Nov 2026 16:56:01 +1100 [thread overview]
Message-ID: <20261120-dma_mapping_type_v4-v3-23-c0183a409bdf@nvidia.com> (raw)
In-Reply-To: <20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com>
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.
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
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(-)
diff --git a/drivers/dma-buf/dma-buf-mapping-internal.h b/drivers/dma-buf/dma-buf-mapping-internal.h
index e1337db2b205..568788fbafa9 100644
--- a/drivers/dma-buf/dma-buf-mapping-internal.h
+++ b/drivers/dma-buf/dma-buf-mapping-internal.h
@@ -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__ */
diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
index 248ac81f616b..062aa61acc05 100644
--- a/drivers/dma-buf/dma-buf-mapping.c
+++ b/drivers/dma-buf/dma-buf-mapping.c
@@ -395,6 +395,94 @@ void dma_buf_mapping_unselect(struct dma_buf_attachment *attach,
attach->selected_idx = 0;
}
+/**
+ * dma_buf_mapping_map - Map an attachment with whichever type the exporter
+ * selects
+ * @attach: the attachment
+ * @dir: DMA direction
+ *
+ * For importers that listed more than one mapping type. The exporter picks the
+ * type from the possible matches and the result records which was used, see
+ * dma_buf_mapping_type_of(). Must be called with the dma_resv lock held, and
+ * the lock must be held until the result has been acted on. Importers that
+ * listed a single type keep using that type's map function.
+ *
+ * Returns: a &struct dma_buf_mapping to pass to dma_buf_mapping_unmap(), or
+ * an ERR_PTR.
+ */
+struct dma_buf_mapping *dma_buf_mapping_map(struct dma_buf_attachment *attach,
+ enum dma_data_direction dir)
+{
+ const struct dma_buf_mapping_match *match;
+ struct dma_buf_mapping *mapping;
+ bool selected;
+ int ret;
+
+ might_sleep();
+
+ if (WARN_ON(!attach || !attach->dmabuf))
+ return ERR_PTR(-EINVAL);
+
+ dma_resv_assert_held(attach->dmabuf->resv);
+
+ selected = attach->selected;
+ ret = dma_buf_mapping_select(attach);
+ if (ret)
+ return ERR_PTR(ret);
+
+ match = dma_buf_attach_map_type(attach);
+ if (!match->type->map) {
+ ret = -EOPNOTSUPP;
+ goto err_release;
+ }
+
+ mapping = match->type->map(attach, dir);
+ if (IS_ERR(mapping)) {
+ ret = PTR_ERR(mapping);
+ goto err_release;
+ }
+
+ /* A mapping handed out again must have been made under the same entry */
+ WARN_ON_ONCE(mapping->match && mapping->match != match);
+ mapping->match = match;
+ dma_buf_mapping_commit(attach);
+ return mapping;
+
+err_release:
+ dma_buf_mapping_unselect(attach, selected);
+ return ERR_PTR(ret);
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_mapping_map, "DMA_BUF");
+
+/**
+ * dma_buf_mapping_unmap - Release a mapping from dma_buf_mapping_map()
+ * @attach: the attachment
+ * @mapping: what dma_buf_mapping_map() returned
+ * @dir: DMA direction used for the map
+ *
+ * The mapping is torn down with the type it was made with, recorded in its
+ * header, even if the attachment has been invalidated and another type
+ * selected since.
+ */
+void dma_buf_mapping_unmap(struct dma_buf_attachment *attach,
+ struct dma_buf_mapping *mapping,
+ enum dma_data_direction dir)
+{
+ const struct dma_buf_mapping_match *match;
+
+ might_sleep();
+
+ if (WARN_ON(!attach || !attach->dmabuf || !mapping))
+ return;
+
+ dma_resv_assert_held(attach->dmabuf->resv);
+
+ /* Torn down with the entry it was made with, not the selection */
+ match = mapping->match;
+ match->type->unmap(attach, mapping, dir);
+}
+EXPORT_SYMBOL_NS_GPL(dma_buf_mapping_unmap, "DMA_BUF");
+
static void
dma_buf_sgt_finish_match(struct dma_buf_match_args *args,
const struct dma_buf_mapping_match *exp,
@@ -420,5 +508,7 @@ 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,
+ .map = dma_buf_sgt_type_map,
+ .unmap = dma_buf_sgt_type_unmap,
};
EXPORT_SYMBOL_NS_GPL(dma_buf_mapping_sgt_type, "DMA_BUF");
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 8124e7bcd5c2..35863bda6ee5 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -1228,10 +1228,9 @@ static struct dma_buf_sgt_mapping *
__dma_buf_map_sgt(struct dma_buf_attachment *attach,
enum dma_data_direction direction)
{
- const struct dma_buf_mapping_match *match =
- dma_buf_attach_map_type(attach);
const struct dma_buf_mapping_sgt_exp_ops *sgt_exp_ops =
dma_buf_get_sgt_ops(attach);
+ const struct dma_buf_mapping_match *match;
struct dma_buf_sgt_mapping *mapping;
signed long ret;
@@ -1255,6 +1254,7 @@ __dma_buf_map_sgt(struct dma_buf_attachment *attach,
if (IS_ERR(mapping))
goto error_unpin;
+ match = dma_buf_attach_map_type(attach);
/* A mapping handed out again must have been made under the same entry */
WARN_ON_ONCE(mapping->base.match && mapping->base.match != match);
mapping->base.match = match;
@@ -1336,6 +1336,10 @@ static void __dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
* therefore users/importers should not hold onto a mapping for undue amounts of
* time.
*
+ * This is dma_buf_mapping_map() for importers that listed the SGT type alone:
+ * the exporter's mapping is made the same way and dma_buf_unmap_sgt() finds
+ * it again behind the returned table.
+ *
* Important: Dynamic importers must wait for the exclusive fence of the struct
* dma_resv attached to the DMA-BUF first.
*/
@@ -1343,40 +1347,54 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
enum dma_data_direction direction)
{
struct dma_buf_sgt_mapping *mapping;
+ struct dma_buf_mapping *base;
struct sg_table *sg_table;
bool selected;
- int ret;
-
- might_sleep();
- if (WARN_ON(!attach || !attach->dmabuf))
+ if (WARN_ON(!attach))
return ERR_PTR(-EINVAL);
- dma_resv_assert_held(attach->dmabuf->resv);
-
selected = attach->selected;
- ret = dma_buf_mapping_select(attach);
- if (ret)
- return ERR_PTR(ret);
+ base = dma_buf_mapping_map(attach, direction);
+ if (IS_ERR(base))
+ return ERR_CAST(base);
- mapping = __dma_buf_map_sgt(attach, direction);
- if (IS_ERR(mapping)) {
- sg_table = ERR_CAST(mapping);
+ if (WARN_ON(dma_buf_mapping_type_of(base) != &dma_buf_mapping_sgt_type)) {
+ sg_table = ERR_PTR(-EINVAL);
} else {
+ mapping = container_of(base, struct dma_buf_sgt_mapping, base);
sg_table = dma_buf_wrap_sg_table(mapping);
- if (IS_ERR(sg_table))
- __dma_buf_unmap_sgt(attach, mapping, direction);
}
- if (IS_ERR(sg_table))
+ if (IS_ERR(sg_table)) {
+ dma_buf_mapping_unmap(attach, base, direction);
dma_buf_mapping_unselect(attach, selected);
- else
- dma_buf_mapping_commit(attach);
+ }
return sg_table;
}
EXPORT_SYMBOL_NS_GPL(dma_buf_map_sgt, "DMA_BUF");
+/* &dma_buf_mapping_type.map / .unmap of the SGT type */
+struct dma_buf_mapping *dma_buf_sgt_type_map(struct dma_buf_attachment *attach,
+ enum dma_data_direction dir)
+{
+ struct dma_buf_sgt_mapping *mapping = __dma_buf_map_sgt(attach, dir);
+
+ if (IS_ERR(mapping))
+ return ERR_CAST(mapping);
+ return &mapping->base;
+}
+
+void dma_buf_sgt_type_unmap(struct dma_buf_attachment *attach,
+ struct dma_buf_mapping *base,
+ enum dma_data_direction dir)
+{
+ __dma_buf_unmap_sgt(attach,
+ container_of(base, struct dma_buf_sgt_mapping, base),
+ dir);
+}
+
/**
* dma_buf_map_sgt_unlocked - Returns the scatterlist table of the attachment;
* mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
@@ -1413,7 +1431,9 @@ EXPORT_SYMBOL_NS_GPL(dma_buf_map_sgt_unlocked, "DMA_BUF");
* @sg_table: [in] scatterlist info of the buffer to unmap
* @direction: [in] direction of DMA transfer
*
- * This unmaps a DMA mapping for @attached obtained by dma_buf_map_sgt().
+ * This unmaps a DMA mapping for @attached obtained by dma_buf_map_sgt(), with
+ * the type it was made with even if the attachment has been invalidated and
+ * another type selected since.
*/
void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
struct sg_table *sg_table,
@@ -1429,8 +1449,7 @@ void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
dma_resv_assert_held(attach->dmabuf->resv);
mapping = dma_buf_unwrap_sg_table(sg_table);
-
- __dma_buf_unmap_sgt(attach, mapping, direction);
+ dma_buf_mapping_unmap(attach, &mapping->base, direction);
}
EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_sgt, "DMA_BUF");
diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h
index 0a1c2934a932..98d5765cfdba 100644
--- a/include/linux/dma-buf-mapping.h
+++ b/include/linux/dma-buf-mapping.h
@@ -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:
+ *
+ * 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);
};
/**
@@ -91,8 +117,9 @@ struct dma_buf_mapping_type {
* 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
- * producer's map callback has returned and reads it to tear the mapping down.
+ * the SGT type, embedding this header. The core fills the header in, at the
+ * latest 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;
@@ -110,6 +137,12 @@ dma_buf_mapping_type_of(const struct dma_buf_mapping *mapping)
return mapping->match->type;
}
+struct dma_buf_mapping *dma_buf_mapping_map(struct dma_buf_attachment *attach,
+ enum dma_data_direction dir);
+void dma_buf_mapping_unmap(struct dma_buf_attachment *attach,
+ struct dma_buf_mapping *mapping,
+ 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,
@@ -294,6 +327,23 @@ dma_buf_sgt_mapping_dma_device(const struct dma_buf_sgt_mapping *mapping)
return mapping->base.match->sgt_data.importing_dma_device;
}
+/**
+ * dma_buf_sgt_mapping_table - The sg_table for a mapping of the SGT type
+ * @mapping: what dma_buf_mapping_map() returned
+ *
+ * For importers that listed more than one type and got the SGT type.
+ *
+ * Returns: the embedded &struct sg_table, or NULL if @mapping is not of the
+ * SGT type.
+ */
+static inline struct sg_table *
+dma_buf_sgt_mapping_table(struct dma_buf_mapping *mapping)
+{
+ if (WARN_ON(dma_buf_mapping_type_of(mapping) != &dma_buf_mapping_sgt_type))
+ return NULL;
+ return &container_of(mapping, struct dma_buf_sgt_mapping, base)->sgt;
+}
+
/*
* Importer SGT entry. Usable in a static table: the importing device is
* supplied by dma_buf_dynamic_attach() and recorded in the matched entry.
--
2.54.0
next prev parent reply other threads:[~2026-11-20 5:58 UTC|newest]
Thread overview: 25+ 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 ` [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 ` Alistair Popple [this message]
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-23-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
all inboxes | Powered by JetHome®