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>,
	Jason Gunthorpe <jgg@ziepe.ca>
Subject: [PATCH RFC 06/16] dma-buf: Allow exporters to hand out a dma_shared_buf
Date: Tue, 01 Dec 2026 18:06:22 +1100	[thread overview]
Message-ID: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-6-b672717c7fc9@nvidia.com> (raw)
In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com>

Currently exporters must create an sg_table even if they can't use the
DMA API to create them. For example vfio-pci and uverbs export MMIO
which don't have struct page and call dma_buf_phys_vec_to_sgt() from
their map_dma_buf() to create it for the importing device. iommufd,
which needs to program domains itself, reaches it through a private vfio
interface since no dma-buf API hands out the address of a buffer.

Add a second type of dma_buf_op - {un}map_dma_shared_buf() - which
allows exporters to return a dma_shared_buf describing the memory to
be mapped instead of a sg_table. The dma-shared-buf core can then be
used to map that correctly for the given device and convert it into a
list of dma addresses for importers to program.

This will allow for the eventual removal of dma_buf_{un}map_sgt() and
the associated sg_table handling in dma-buf exporters and importers once
all have been converted to use the new operations.

Co-developed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Alistair Popple <apopple@nvidia.com>
---
 drivers/dma-buf/dma-buf.c | 134 ++++++++++++++++++++++++++++++++++++++++++++--
 include/linux/dma-buf.h   |  30 +++++++++--
 2 files changed, 156 insertions(+), 8 deletions(-)

diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c
index 0b8da0c0bc5f..70058509c397 100644
--- a/drivers/dma-buf/dma-buf.c
+++ b/drivers/dma-buf/dma-buf.c
@@ -714,11 +714,17 @@ struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
 	int ret;
 
 	if (WARN_ON(!exp_info->priv || !exp_info->ops
-		    || !exp_info->ops->map_dma_buf
-		    || !exp_info->ops->unmap_dma_buf
 		    || !exp_info->ops->release))
 		return ERR_PTR(-EINVAL);
 
+	/* Exactly one of the two map and unmap pairs */
+	if (WARN_ON(!exp_info->ops->map_dma_buf != !exp_info->ops->unmap_dma_buf ||
+		    !exp_info->ops->map_dma_shared_buf !=
+		    !exp_info->ops->unmap_dma_shared_buf ||
+		    !exp_info->ops->map_dma_buf ==
+		    !exp_info->ops->map_dma_shared_buf))
+		return ERR_PTR(-EINVAL);
+
 	if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin))
 		return ERR_PTR(-EINVAL);
 
@@ -933,6 +939,122 @@ static void dma_buf_unwrap_sg_table(struct sg_table **sg_table)
 	kfree(copy);
 }
 
+/*
+ * Get a list of DMA addresses to program into importing device hardware from an
+ * exporter that provides a dma_shared_buf.
+ */
+static struct dma_addr_list *
+dma_buf_map_list(struct dma_buf_attachment *attach,
+		 enum dma_data_direction dir)
+{
+	struct dma_shared_buf *shared_buf;
+	struct dma_addr_list *addrs;
+
+	dma_resv_assert_held(attach->dmabuf->resv);
+
+	shared_buf = attach->dmabuf->ops->map_dma_shared_buf(attach);
+	if (!shared_buf)
+		return ERR_PTR(-ENOMEM);
+	if (IS_ERR(shared_buf))
+		return ERR_CAST(shared_buf);
+
+	/*
+	 * MMIO goes only to importers prepared for memory without struct
+	 * pages.
+	 */
+	if (shared_buf->provider && !attach->peer2peer) {
+		addrs = ERR_PTR(-EOPNOTSUPP);
+		goto err_unmap_list;
+	}
+
+	addrs = dma_shared_buf_map(attach->dev, shared_buf, dir);
+	if (IS_ERR(addrs))
+		goto err_unmap_list;
+
+	addrs->priv = shared_buf;
+	return addrs;
+
+err_unmap_list:
+	attach->dmabuf->ops->unmap_dma_shared_buf(attach, shared_buf);
+	return addrs;
+}
+
+static void dma_buf_unmap_list(struct dma_buf_attachment *attach,
+			       struct dma_addr_list *addrs,
+			       enum dma_data_direction dir)
+{
+	struct dma_shared_buf *shared_buf = addrs->priv;
+
+	dma_shared_buf_unmap(attach->dev, addrs, dir);
+	attach->dmabuf->ops->unmap_dma_shared_buf(attach, shared_buf);
+}
+
+/**
+ * struct dma_buf_dma - the sg_table the core builds for an exporter that hands
+ *                      out a dma_shared_buf
+ * @sgt: A DMA-only scatterlist over the ranges of @addrs, built by
+ *       dma_addr_list_to_sgt()
+ * @addrs: The DMA addresses the exporter's dma_shared_buf was mapped to
+ */
+struct dma_buf_dma {
+	struct sg_table sgt;
+	struct dma_addr_list *addrs;
+};
+
+/*
+ * The sg_table of an attachment: made by the exporter's map_dma_buf(), or by
+ * the core from the dma_shared_buf its map_dma_shared_buf() hands out.
+ * Importers see no difference.
+ */
+static struct sg_table *
+dma_buf_map_sg_table(struct dma_buf_attachment *attach,
+		     enum dma_data_direction dir)
+{
+	struct dma_buf_dma *dma;
+	int ret;
+
+	if (attach->dmabuf->ops->map_dma_buf)
+		return attach->dmabuf->ops->map_dma_buf(attach, dir);
+
+	dma = kzalloc_obj(*dma);
+	if (!dma)
+		return ERR_PTR(-ENOMEM);
+
+	dma->addrs = dma_buf_map_list(attach, dir);
+	if (IS_ERR(dma->addrs)) {
+		ret = PTR_ERR(dma->addrs);
+		goto err_free;
+	}
+
+	ret = dma_addr_list_to_sgt(&dma->sgt, dma->addrs);
+	if (ret)
+		goto err_unmap;
+	return &dma->sgt;
+
+err_unmap:
+	dma_buf_unmap_list(attach, dma->addrs, dir);
+err_free:
+	kfree(dma);
+	return ERR_PTR(ret);
+}
+
+static void dma_buf_unmap_sg_table(struct dma_buf_attachment *attach,
+				   struct sg_table *sg_table,
+				   enum dma_data_direction dir)
+{
+	struct dma_buf_dma *dma;
+
+	if (attach->dmabuf->ops->unmap_dma_buf) {
+		attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, dir);
+		return;
+	}
+
+	dma = container_of(sg_table, struct dma_buf_dma, sgt);
+	dma_addr_list_free_sgt(&dma->sgt);
+	dma_buf_unmap_list(attach, dma->addrs, dir);
+	kfree(dma);
+}
+
 static inline bool
 dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
 {
@@ -1001,6 +1123,8 @@ dma_buf_pin_on_map(struct dma_buf_attachment *attach)
  *     - &dma_buf_ops.unpin()
  *     - &dma_buf_ops.map_dma_buf()
  *     - &dma_buf_ops.unmap_dma_buf()
+ *     - &dma_buf_ops.map_dma_shared_buf()
+ *     - &dma_buf_ops.unmap_dma_shared_buf()
  *     - &dma_buf_ops.vmap()
  *     - &dma_buf_ops.vunmap()
  *
@@ -1213,7 +1337,7 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
 			return ERR_PTR(ret);
 	}
 
-	sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
+	sg_table = dma_buf_map_sg_table(attach, direction);
 	if (!sg_table)
 		sg_table = ERR_PTR(-ENOMEM);
 	if (IS_ERR(sg_table))
@@ -1252,7 +1376,7 @@ struct sg_table *dma_buf_map_sgt(struct dma_buf_attachment *attach,
 	return sg_table;
 
 error_unmap:
-	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+	dma_buf_unmap_sg_table(attach, sg_table, direction);
 	sg_table = ERR_PTR(ret);
 
 error_unpin:
@@ -1313,7 +1437,7 @@ void dma_buf_unmap_sgt(struct dma_buf_attachment *attach,
 	dma_resv_assert_held(attach->dmabuf->resv);
 
 	dma_buf_unwrap_sg_table(&sg_table);
-	attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction);
+	dma_buf_unmap_sg_table(attach, sg_table, direction);
 
 	if (dma_buf_pin_on_map(attach))
 		attach->dmabuf->ops->unpin(attach);
diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h
index b138b77212c9..719296a3d47e 100644
--- a/include/linux/dma-buf.h
+++ b/include/linux/dma-buf.h
@@ -19,6 +19,7 @@
 #include <linux/scatterlist.h>
 #include <linux/list.h>
 #include <linux/dma-mapping.h>
+#include <linux/dma-shared-buf.h>
 #include <linux/fs.h>
 #include <linux/dma-fence.h>
 #include <linux/wait.h>
@@ -117,8 +118,10 @@ struct dma_buf_ops {
 	 * @map_dma_buf:
 	 *
 	 * This is called by dma_buf_map_sgt() and is used to map a
-	 * shared &dma_buf into device address space, and it is mandatory. It
-	 * can only be called if @attach has been called successfully.
+	 * shared &dma_buf into device address space. It can only be called if
+	 * @attach has been called successfully. An exporter provides either
+	 * this and @unmap_dma_buf or @map_dma_shared_buf and
+	 * @unmap_dma_shared_buf, see there.
 	 *
 	 * This call may sleep, e.g. when the backing storage first needs to be
 	 * allocated, or moved to a location suitable for all currently attached
@@ -174,7 +177,7 @@ struct dma_buf_ops {
 	 * @unmap_dma_buf:
 	 *
 	 * This is called by dma_buf_unmap_sgt() and should unmap and
-	 * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
+	 * release the &sg_table allocated in @map_dma_buf, and goes with it.
 	 * For static dma_buf handling this might also unpin the backing
 	 * storage if this is the last mapping of the DMA buffer.
 	 */
@@ -182,6 +185,27 @@ struct dma_buf_ops {
 			      struct sg_table *,
 			      enum dma_data_direction);
 
+	/**
+	 * @map_dma_shared_buf:
+	 *
+	 * The alternative to @map_dma_buf for an exporter that provides a
+	 * dma_shared_buf instead of a sg_table.
+	 *
+	 * Returns the dma_shared_buf, or a negative error value wrapped into a
+	 * pointer.
+	 */
+	struct dma_shared_buf *
+	(*map_dma_shared_buf)(struct dma_buf_attachment *attach);
+
+	/**
+	 * @unmap_dma_shared_buf:
+	 *
+	 * Release a dma_shared_buf returned by @map_dma_shared_buf. The core
+	 * holds none of its addresses once this is called.
+	 */
+	void (*unmap_dma_shared_buf)(struct dma_buf_attachment *attach,
+				     struct dma_shared_buf *shared_buf);
+
 	/* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
 	 * if the call would block.
 	 */

-- 
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 ` [PATCH RFC 03/16] dma-shared-buf: Add dma_shared_buf_iommu_map() Alistair Popple
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 ` Alistair Popple [this message]
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-6-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®