mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alistair Popple <apopple@nvidia.com>
To: "Karol Wachowski" <karol.wachowski@linux.intel.com>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Philipp Stanner" <phasta@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Rodrigo Vivi" <rodrigo.vivi@intel.com>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Jason Gunthorpe" <jgg@ziepe.ca>,
	"Leon Romanovsky" <leon@kernel.org>,
	"Kees Cook" <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	"Alex Williamson" <alex@shazbot.org>,
	"Kevin Tian" <kevin.tian@intel.com>,
	"Joerg Roedel (AMD)" <joro@8bytes.org>,
	"Will Deacon" <will@kernel.org>,
	"Robin Murphy" <robin.murphy@arm.com>
Cc: dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org,
	 linaro-mm-sig@lists.linaro.org, intel-xe@lists.freedesktop.org,
	 linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org,
	 linux-hardening@vger.kernel.org, kvm@vger.kernel.org,
	iommu@lists.linux.dev,  Alexey Kardashevskiy <aik@amd.com>,
	Mina Almasry <almasrymina@google.com>,
	 Baolu Lu <baolu.lu@linux.intel.com>,
	chaitanyak@nvidia.com,  Dongwon Kim <dongwon.kim@intel.com>,
	 Felix Kuehling <felix.kuehling@amd.com>,
	jhubbard@nvidia.com,  Karl Mehltretter <kmehltretter@gmail.com>,
	 Vivek Kasireddy <vivek.kasireddy@intel.com>,
	 Xu Yilun <yilun.xu@linux.intel.com>,
	zhipingz@meta.com,  Alistair Popple <apopple@nvidia.com>
Subject: [PATCH RFC 03/16] dma-shared-buf: Add dma_shared_buf_iommu_map()
Date: Tue, 01 Dec 2026 18:06:19 +1100	[thread overview]
Message-ID: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-3-b672717c7fc9@nvidia.com> (raw)
In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com>

dma_shared_buf_map() maps a dma_shared_buf for a device through the DMA
API. However a caller such as iommufd manages IOMMU domains itself and
has no device to map for. It needs the memory a dma_shared_buf describes
programmed into a domain at an IOVA of its choosing.

Add dma_shared_buf_iommu_map() to map a window of a dma_shared_buf into
an IOMMU domain. The window is mapped IOMMU_MMIO when the dma_shared_buf
has a p2pdma provider. Nothing is left mapped if it fails. There is no
matching unmap function as nothing is kept for the mapping, so the
caller undoes it with iommu_unmap().

Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
 drivers/dma-buf/dma-shared-buf.c | 71 ++++++++++++++++++++++++++++++++++++++++
 include/linux/dma-shared-buf.h   |  4 +++
 2 files changed, 75 insertions(+)

diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
index 75ab08828ee8..d5d179b771f2 100644
--- a/drivers/dma-buf/dma-shared-buf.c
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -4,6 +4,7 @@
  * be mapped for whichever device needs to access it.
  */
 #include <linux/dma-shared-buf.h>
+#include <linux/iommu.h>
 #include <linux/overflow.h>
 #include <linux/pci-p2pdma.h>
 #include <linux/slab.h>
@@ -136,3 +137,73 @@ void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
 	}
 	kvfree(addrs);
 }
+
+/**
+ * dma_shared_buf_iommu_map - Map a window of a dma_shared_buf into an IOMMU
+ *                            domain
+ * @shared_buf: The memory to map
+ * @domain: The domain to program
+ * @iova: Where in @domain the window starts
+ * @offset: Where in @shared_buf 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 @shared_buf is MMIO.
+ * @gfp: Allocation flags for the page tables
+ *
+ * For a caller managing an IOMMU domain itself rather than mapping for a
+ * device. Nothing is left mapped on failure and nothing of @shared_buf is
+ * referenced once this returns. The caller undoes the mapping with
+ * iommu_unmap().
+ *
+ * Returns: 0 on success, -EINVAL if the window is outside @shared_buf, or the
+ * error from iommu_map().
+ */
+int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
+			     struct iommu_domain *domain, unsigned long iova,
+			     size_t offset, size_t size, int prot, gfp_t gfp)
+{
+	unsigned long start_iova = iova;
+	const struct phys_vec *vec;
+	size_t i;
+	int ret;
+
+	if (offset >= shared_buf->size || size > shared_buf->size - offset)
+		return -EINVAL;
+
+	if (shared_buf->provider) {
+		prot &= ~IOMMU_CACHE;
+		prot |= IOMMU_MMIO;
+	}
+
+	/* Seek to the range holding @offset, then map up to @size bytes */
+	for (i = 0, vec = shared_buf->ranges; i < shared_buf->nr_ranges;
+	     i++, vec++) {
+		size_t len;
+
+		if (offset >= vec->len) {
+			offset -= vec->len;
+			continue;
+		}
+		if (!size)
+			break;
+
+		len = min(size, vec->len - offset);
+		ret = iommu_map(domain, iova, vec->paddr + offset, len, prot,
+				gfp);
+		if (ret)
+			goto err_unmap;
+		iova += len;
+		size -= len;
+		offset = 0;
+	}
+	if (WARN_ON(size)) {
+		ret = -EINVAL;
+		goto err_unmap;
+	}
+	return 0;
+
+err_unmap:
+	if (iova != start_iova)
+		iommu_unmap(domain, start_iova, iova - start_iova);
+	return ret;
+}
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
index 53f506e0cc85..2a3f6297d23f 100644
--- a/include/linux/dma-shared-buf.h
+++ b/include/linux/dma-shared-buf.h
@@ -9,6 +9,7 @@
 #include <linux/dma-mapping.h>
 #include <linux/types.h>
 
+struct iommu_domain;
 struct p2pdma_provider;
 
 /**
@@ -73,5 +74,8 @@ dma_shared_buf_map(struct device *dev, const struct dma_shared_buf *shared_buf,
 		   enum dma_data_direction dir);
 void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
 			  enum dma_data_direction dir);
+int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
+			     struct iommu_domain *domain, unsigned long iova,
+			     size_t offset, size_t size, int prot, gfp_t gfp);
 
 #endif /* _LINUX_DMA_SHARED_BUF_H */

-- 
2.54.0


  parent reply	other threads:[~2026-12-01  7:07 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  7:06 [PATCH RFC 00/16] dma-buf: Introduce dma_shared_buf and dma_addr_list to replace sg_table Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 01/16] treewide: Rename dma_buf_(un)map_attachment() to dma_buf_(un)map_sgt() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 02/16] dma-shared-buf: Add struct dma_shared_buf Alistair Popple
2026-12-01  7:06 ` Alistair Popple [this message]
2026-12-01  7:06 ` [PATCH RFC 04/16] dma-shared-buf: Add dma_addr_list_to_sgt() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 05/16] dma-shared-buf: Add dma_shared_buf_from_pages() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 06/16] dma-buf: Allow exporters to hand out a dma_shared_buf Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 07/16] dma-buf: Add dma_buf_iommu_map() for IOMMU domain importers Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 08/16] dma-buf: Factor out the pin and fence wait around the exporter's map Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 09/16] dma-buf: Add dma_buf_map_dma_list() for DMA address only importers Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 10/16] vfio/pci: Export a dma_shared_buf instead of mapping the buffer Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 11/16] RDMA/uverbs: " Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 12/16] dma-buf: Remove dma_buf_phys_vec_to_sgt() and dma_buf_free_sgt() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 13/16] iommufd: Map DMA-bufs through dma_buf_iommu_map() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 14/16] vfio/pci: Remove vfio_pci_dma_buf_iommufd_map() Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 15/16] drm/xe/dma-buf: Export a dma_shared_buf instead of mapping the buffer Alistair Popple
2026-12-01  7:06 ` [PATCH RFC 16/16] drm/xe: Import DMA-bufs as a DMA address list 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=20261201-dmabuf_shared_buf_v1_xe_rc5-v1-3-b672717c7fc9@nvidia.com \
    --to=apopple@nvidia.com \
    --cc=aik@amd.com \
    --cc=airlied@gmail.com \
    --cc=alex@shazbot.org \
    --cc=almasrymina@google.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=chaitanyak@nvidia.com \
    --cc=christian.koenig@amd.com \
    --cc=dongwon.kim@intel.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=felix.kuehling@amd.com \
    --cc=gustavoars@kernel.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=karol.wachowski@linux.intel.com \
    --cc=kees@kernel.org \
    --cc=kevin.tian@intel.com \
    --cc=kmehltretter@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=leon@kernel.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=phasta@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rodrigo.vivi@intel.com \
    --cc=simona@ffwll.ch \
    --cc=sumit.semwal@linaro.org \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=vivek.kasireddy@intel.com \
    --cc=will@kernel.org \
    --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®