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 02/16] dma-shared-buf: Add struct dma_shared_buf
Date: Tue, 01 Dec 2026 18:06:18 +1100	[thread overview]
Message-ID: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-2-b672717c7fc9@nvidia.com> (raw)
In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com>

Currently the sg_table is used by dma-buf exporters to describe the
range of physical addresses to map to the DMA API. This is then passed
directly back to importers.

This is problematic for a number of reasons. It exposes exporter side
information to importers and doesn't have the capability to describe
memory that isn't backed by struct page which is what MMIO such as PCI
BAR needs.

Add the dma-shared-buf as a library to handle this. A new type to
replace the exporter's sg_table, struct dma_shared_buf, is added
to describe a range of physical regions for mapping by the library.
dma_shared_buf_map() can then be used to map this for a particular
device.

The mapping function handles either direct P2P mappings using the PCI
peer-to-peer core if a p2pdma provider is listed, or system memory
otherwise. This allows dma-buf exporters to use common logic to
create mappings instead of open-coding it which is hard to get right,
especially for P2P cases with complex routing and IOMMU requirements.

This is kept in a file of its own as none of it depends on a DMA-buf and
it could be a library elsewhere.

Signed-off-by: Alistair Popple <apopple@nvidia.com>

---

This is supposed to be used as a library for dma-buf exporters/importers
so could be moved. I don't love the name so happy for suggestions or
alternatives.
---
 MAINTAINERS                      |   1 +
 drivers/dma-buf/Makefile         |   3 +-
 drivers/dma-buf/dma-shared-buf.c | 138 +++++++++++++++++++++++++++++++++++++++
 include/linux/dma-shared-buf.h   |  77 ++++++++++++++++++++++
 4 files changed, 218 insertions(+), 1 deletion(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c49..679986e43aca 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -7879,6 +7879,7 @@ F:	include/linux/*fence.h
 F:	include/linux/dma-buf.h
 F:	include/linux/dma-buf/
 F:	include/linux/dma-resv.h
+F:	include/linux/dma-shared-buf.h
 F:	rust/helpers/dma-resv.c
 F:	rust/helpers/dma_fence.c
 F:	rust/kernel/dma_buf/
diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile
index 523731b0f83e..ea24bd1bd75c 100644
--- a/drivers/dma-buf/Makefile
+++ b/drivers/dma-buf/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y := dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \
-	 dma-fence-unwrap.o dma-resv.o dma-buf-mapping.o dma-buf-io.o
+	 dma-fence-unwrap.o dma-resv.o dma-buf-mapping.o dma-buf-io.o \
+	 dma-shared-buf.o
 obj-$(CONFIG_DMABUF_HEAPS)	+= dma-heap.o
 obj-$(CONFIG_DMABUF_HEAPS)	+= heaps/
 obj-$(CONFIG_SYNC_FILE)		+= sync_file.o
diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
new file mode 100644
index 000000000000..75ab08828ee8
--- /dev/null
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -0,0 +1,138 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * struct dma_shared_buf. Memory described by its owner as a list of ranges, to
+ * be mapped for whichever device needs to access it.
+ */
+#include <linux/dma-shared-buf.h>
+#include <linux/overflow.h>
+#include <linux/pci-p2pdma.h>
+#include <linux/slab.h>
+
+/**
+ * dma_shared_buf_map - Map a dma_shared_buf for a device
+ * @dev: The device to map for
+ * @shared_buf: The memory to map
+ * @dir: Direction of the DMA transfer
+ *
+ * A dma_shared_buf with a p2pdma provider goes where the P2P core directs it.
+ * Bus addresses need no mapping at all, otherwise it is mapped through the host
+ * bridge like system memory with DMA_ATTR_MMIO. Either way the ranges are
+ * linked into a single IOVA when @dev has one and mapped one by one otherwise,
+ * so the result holds one DMA range or one per range of @shared_buf.
+ *
+ * Nothing of @shared_buf is referenced once this returns.
+ *
+ * Returns: the DMA addresses, to be released with dma_shared_buf_unmap(),
+ * or a negative error value wrapped into a pointer.
+ */
+struct dma_addr_list *
+dma_shared_buf_map(struct device *dev, const struct dma_shared_buf *shared_buf,
+		   enum dma_data_direction dir)
+{
+	struct dma_addr_list *addrs;
+	size_t mapped = 0;
+	size_t i;
+	int ret;
+
+	if (WARN_ON(!shared_buf->nr_ranges || shared_buf->nr_ranges > UINT_MAX))
+		return ERR_PTR(-EINVAL);
+
+	addrs = kvzalloc(struct_size(addrs, ranges, shared_buf->nr_ranges),
+			 GFP_KERNEL);
+	if (!addrs)
+		return ERR_PTR(-ENOMEM);
+	addrs->size = shared_buf->size;
+	addrs->nr_ranges = shared_buf->nr_ranges;
+	addrs->attrs = shared_buf->attrs;
+
+	if (shared_buf->provider) {
+		addrs->attrs |= DMA_ATTR_MMIO;
+		switch (pci_p2pdma_map_type(shared_buf->provider, dev)) {
+		case PCI_P2PDMA_MAP_BUS_ADDR:
+			for (i = 0; i < shared_buf->nr_ranges; i++) {
+				const struct phys_vec *vec =
+					&shared_buf->ranges[i];
+
+				addrs->ranges[i].addr =
+					pci_p2pdma_bus_addr_map(shared_buf->provider,
+								vec->paddr);
+				addrs->ranges[i].len = vec->len;
+			}
+			addrs->bus_addr = true;
+			return addrs;
+		case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
+			break;
+		default:
+			ret = -EINVAL;
+			goto err_free;
+		}
+	}
+
+	if (dma_iova_try_alloc(dev, &addrs->state, 0, shared_buf->size)) {
+		for (i = 0; i < shared_buf->nr_ranges; i++) {
+			ret = dma_iova_link(dev, &addrs->state,
+					    shared_buf->ranges[i].paddr, mapped,
+					    shared_buf->ranges[i].len, dir,
+					    addrs->attrs);
+			if (ret)
+				goto err_iova;
+			mapped += shared_buf->ranges[i].len;
+		}
+		WARN_ON_ONCE(mapped != shared_buf->size);
+		ret = dma_iova_sync(dev, &addrs->state, 0, mapped);
+		if (ret)
+			goto err_iova;
+		addrs->ranges[0].addr = addrs->state.addr;
+		addrs->ranges[0].len = mapped;
+		addrs->nr_ranges = 1;
+		return addrs;
+	}
+
+	for (i = 0; i < shared_buf->nr_ranges; i++) {
+		dma_addr_t addr = dma_map_phys(dev, shared_buf->ranges[i].paddr,
+					       shared_buf->ranges[i].len, dir,
+					       addrs->attrs);
+
+		ret = dma_mapping_error(dev, addr);
+		if (ret)
+			goto err_unmap;
+		addrs->ranges[i].addr = addr;
+		addrs->ranges[i].len = shared_buf->ranges[i].len;
+	}
+	return addrs;
+
+err_unmap:
+	while (i--)
+		dma_unmap_phys(dev, addrs->ranges[i].addr, addrs->ranges[i].len,
+			       dir, addrs->attrs);
+	goto err_free;
+err_iova:
+	dma_iova_destroy(dev, &addrs->state, mapped, dir, addrs->attrs);
+err_free:
+	kvfree(addrs);
+	return ERR_PTR(ret);
+}
+
+/**
+ * dma_shared_buf_unmap - Unmap and free the result of dma_shared_buf_map()
+ * @dev: The device the dma_shared_buf was mapped for
+ * @addrs: The DMA addresses it was mapped to
+ * @dir: The direction it was mapped with
+ */
+void dma_shared_buf_unmap(struct device *dev, struct dma_addr_list *addrs,
+			  enum dma_data_direction dir)
+{
+	unsigned int i;
+
+	if (addrs->bus_addr) {
+		; /* Bus addresses: nothing was mapped */
+	} else if (dma_use_iova(&addrs->state)) {
+		dma_iova_destroy(dev, &addrs->state, addrs->size, dir,
+				 addrs->attrs);
+	} else {
+		for (i = 0; i < addrs->nr_ranges; i++)
+			dma_unmap_phys(dev, addrs->ranges[i].addr,
+				       addrs->ranges[i].len, dir, addrs->attrs);
+	}
+	kvfree(addrs);
+}
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
new file mode 100644
index 000000000000..53f506e0cc85
--- /dev/null
+++ b/include/linux/dma-shared-buf.h
@@ -0,0 +1,77 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * struct dma_shared_buf. Memory described by its owner as a list of ranges, to
+ * be mapped for whichever device needs to access it.
+ */
+#ifndef _LINUX_DMA_SHARED_BUF_H
+#define _LINUX_DMA_SHARED_BUF_H
+
+#include <linux/dma-mapping.h>
+#include <linux/types.h>
+
+struct p2pdma_provider;
+
+/**
+ * struct dma_shared_buf - Memory to be DMA mapped, as described by its owner
+ * @provider: The P2P provider of all of @ranges when they are MMIO, NULL for
+ *            system memory
+ * @attrs: DMA attributes to map with, eg. DMA_ATTR_SKIP_CPU_SYNC when the
+ *         owner looks after CPU cache synchronisation itself. DMA_ATTR_MMIO
+ *         is added when there is a @provider.
+ * @size: Sum of the range lengths
+ * @nr_ranges: Number of entries in @ranges
+ * @ranges: The ranges, in order
+ */
+struct dma_shared_buf {
+	struct p2pdma_provider *provider;
+	unsigned long attrs;
+	size_t size;
+	size_t nr_ranges;
+	struct phys_vec ranges[] __counted_by(nr_ranges);
+};
+
+/**
+ * struct dma_addr_range - A run of addresses in a device's DMA address space
+ * @addr: Start of the run
+ * @len: Length of the run in bytes
+ */
+struct dma_addr_range {
+	dma_addr_t addr;
+	size_t len;
+};
+
+/**
+ * struct dma_addr_list - Memory as a list of ranges of a device's DMA address
+ *                        space
+ * @size: Sum of the range lengths
+ * @nr_ranges: Number of entries in @ranges
+ * @priv: For the owner of the list, which is whoever called
+ *        dma_shared_buf_map() or built the list itself
+ * @state: IOVA state when the list was linked into a single IOVA
+ * @attrs: The DMA attributes the list was mapped with
+ * @bus_addr: True when @ranges are PCI bus addresses, which needed no mapping
+ * @ranges: The DMA addresses covering the memory in order. For a mapped
+ *          dma_shared_buf that is one entry per range of it, or a single
+ *          entry when it was linked into one IOVA.
+ *
+ * Returned by dma_shared_buf_map() and released by dma_shared_buf_unmap(),
+ * which is all @state, @attrs and @bus_addr are for. Anyone the owner hands
+ * the list to reads @size, @nr_ranges and @ranges and nothing else.
+ */
+struct dma_addr_list {
+	size_t size;
+	unsigned int nr_ranges;
+	void *priv;
+	struct dma_iova_state state;
+	unsigned long attrs;
+	bool bus_addr;
+	struct dma_addr_range ranges[] __counted_by(nr_ranges);
+};
+
+struct dma_addr_list *
+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);
+
+#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 ` Alistair Popple [this message]
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 ` [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-2-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®