[PATCH RFC 07/16] dma-buf: Add dma_buf_iommu_map() for IOMMU domain importers

From: Alistair Popple

Date: Tue Dec 01 2026 - 02:08:03 EST


iommufd imports DMA-bufs to program their memory into IOMMU domains.
Today it does so through a private interface with vfio-pci that hands
it the address of the BAR, but this doesn't work with other drivers such
as GPU drivers meaning their DMA-bufs cannot be used with iommufd to
orchestrate things such as direct PCIe P2P storage access.

So add an importer for IOMMU domains. The iommufd importer calls
dma_buf_iommu_map() with its domain, an IOVA and a window into the
buffer, and dma_buf_iommu_unmap() to undo it.

The core gets the dma_shared_buf from the exporter and has
dma_shared_buf_iommu_map() program the part inside the window into the
domain, marking it IOMMU_MMIO when the dma_shared_buf has a p2pdma
provider.

Only an exporter implementing the map_dma_shared_buf() ops can be
mapped this way. The functions are exported to iommufd only as there are
no other users of this functionality.

Signed-off-by: Alistair Popple <apopple@xxxxxxxxxx>
---
drivers/dma-buf/dma-buf-mapping.c | 124 ++++++++++++++++++++++++++++++++++++++
include/linux/dma-buf-mapping.h | 15 +++++
2 files changed, 139 insertions(+)

diff --git a/drivers/dma-buf/dma-buf-mapping.c b/drivers/dma-buf/dma-buf-mapping.c
index 5167cda7ea21..ec9776b1dff0 100644
--- a/drivers/dma-buf/dma-buf-mapping.c
+++ b/drivers/dma-buf/dma-buf-mapping.c
@@ -5,6 +5,7 @@
*/
#include <linux/dma-buf-mapping.h>
#include <linux/dma-resv.h>
+#include <linux/iommu.h>
#include <linux/overflow.h>
#include <linux/align.h>

@@ -259,3 +260,126 @@ void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,

}
EXPORT_SYMBOL_NS_GPL(dma_buf_free_sgt, "DMA_BUF");
+
+/*
+ * IOMMU domain importers
+ *
+ * An importer that programs an IOMMU domain itself hands over the domain and
+ * an IOVA window, the exporter hands over its dma_shared_buf, and the core,
+ * which sees both, has it programmed into the domain. Neither side sees
+ * the other's data. Only exporters handing out a dma_shared_buf can be mapped
+ * this way, and only iommufd may ask.
+ */
+
+/**
+ * dma_buf_iommu_mappable - Can the buffer be mapped into an IOMMU domain
+ * @dmabuf: the buffer
+ *
+ * Lets an importer fail at import time rather than when a domain is first
+ * filled.
+ *
+ * Returns: true when the exporter hands out a dma_shared_buf, so
+ * dma_buf_iommu_map() can work.
+ */
+bool dma_buf_iommu_mappable(struct dma_buf *dmabuf)
+{
+ return dmabuf->ops->map_dma_shared_buf;
+}
+EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_mappable, "iommufd");
+
+/**
+ * dma_buf_iommu_map - Map a window of the buffer into an IOMMU domain
+ * @attach: the attachment
+ * @domain: the domain to program
+ * @iova: where in @domain the window starts
+ * @offset: where in the buffer the window starts
+ * @size: length of the window
+ * @prot: IOMMU_READ/IOMMU_WRITE/... for the mapping. IOMMU_MMIO is added
+ * and IOMMU_CACHE removed when the buffer is MMIO.
+ * @gfp: allocation flags for the page tables
+ *
+ * Called with the dma_resv lock held and the attachment pinned. Asks the
+ * exporter for its dma_shared_buf, has the part of it inside the window
+ * programmed into @domain and releases it again: nothing about the
+ * buffer's addresses is kept between calls. The importer undoes the mapping
+ * with dma_buf_iommu_unmap(), which it must do before the exporter revokes,
+ * i.e. from its invalidate_mappings().
+ *
+ * The buffer's DMA_RESV_USAGE_KERNEL fences are waited for before the domain
+ * is programmed, as the importer has no way to wait for them once the memory
+ * is reachable through @domain.
+ *
+ * Returns: 0 on success, -EOPNOTSUPP if the exporter does not hand out a
+ * dma_shared_buf or the buffer is MMIO and the importer does not allow
+ * peer2peer, -EINVAL if the window is outside the buffer, -ERESTARTSYS if the
+ * fence wait is interrupted by a signal, or the error from the exporter or
+ * iommu_map().
+ */
+int dma_buf_iommu_map(struct dma_buf_attachment *attach,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t offset, size_t size, int prot, gfp_t gfp)
+{
+ const struct dma_buf_ops *ops = attach->dmabuf->ops;
+ struct dma_shared_buf *shared_buf;
+ long wait;
+ int ret;
+
+ dma_resv_assert_held(attach->dmabuf->resv);
+
+ if (!ops->map_dma_shared_buf)
+ return -EOPNOTSUPP;
+
+ shared_buf = ops->map_dma_shared_buf(attach);
+ if (!shared_buf)
+ return -ENOMEM;
+ if (IS_ERR(shared_buf))
+ return PTR_ERR(shared_buf);
+
+ if (shared_buf->provider && !attach->peer2peer) {
+ ret = -EOPNOTSUPP;
+ goto out_unmap_list;
+ }
+
+ /*
+ * The exporter may still be moving the buffer to where it was pinned,
+ * wait until that is finished.
+ */
+ wait = dma_resv_wait_timeout(attach->dmabuf->resv,
+ DMA_RESV_USAGE_KERNEL, true,
+ MAX_SCHEDULE_TIMEOUT);
+ if (wait < 0) {
+ ret = wait;
+ goto out_unmap_list;
+ }
+
+ ret = dma_shared_buf_iommu_map(shared_buf, domain, iova, offset,
+ size, prot, gfp);
+
+out_unmap_list:
+ ops->unmap_dma_shared_buf(attach, shared_buf);
+ return ret;
+}
+EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_map, "iommufd");
+
+/**
+ * dma_buf_iommu_unmap - Undo dma_buf_iommu_map() for a window
+ * @attach: the attachment the window was mapped for
+ * @domain: the domain it was mapped into
+ * @iova: where in @domain the window starts
+ * @size: length of the window
+ *
+ * The exporter is not involved and no lock is needed: this only unmaps the
+ * IOVA range. It exists so the importer never operates on a buffer's domain
+ * mappings itself, and must be called before the exporter revokes the
+ * buffer, so from the importer's invalidate_mappings().
+ */
+void dma_buf_iommu_unmap(struct dma_buf_attachment *attach,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t size)
+{
+ size_t unmapped = iommu_unmap(domain, iova, size);
+
+ /* Anything else is a bug here or in the IOMMU driver */
+ WARN_ON(unmapped != size);
+}
+EXPORT_SYMBOL_FOR_MODULES(dma_buf_iommu_unmap, "iommufd");
diff --git a/include/linux/dma-buf-mapping.h b/include/linux/dma-buf-mapping.h
index 09bde3f748e4..04fab6bd5dc7 100644
--- a/include/linux/dma-buf-mapping.h
+++ b/include/linux/dma-buf-mapping.h
@@ -14,4 +14,19 @@ struct sg_table *dma_buf_phys_vec_to_sgt(struct dma_buf_attachment *attach,
enum dma_data_direction dir);
void dma_buf_free_sgt(struct dma_buf_attachment *attach, struct sg_table *sgt,
enum dma_data_direction dir);
+
+struct iommu_domain;
+
+/*
+ * Importers programming an IOMMU domain themselves. Restricted to iommufd:
+ * the exporter's dma_shared_buf goes into the domain through the core, so
+ * neither driver sees the other's objects.
+ */
+bool dma_buf_iommu_mappable(struct dma_buf *dmabuf);
+int dma_buf_iommu_map(struct dma_buf_attachment *attach,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t offset, size_t size, int prot, gfp_t gfp);
+void dma_buf_iommu_unmap(struct dma_buf_attachment *attach,
+ struct iommu_domain *domain, unsigned long iova,
+ size_t size);
#endif

--
2.54.0