[PATCH RFC 05/16] dma-shared-buf: Add dma_shared_buf_from_pages()

From: Alistair Popple

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


A struct dma_shared_buf describes the memory to map as a list of ranges.
The DMA-buf exporters handing one out so far export MMIO and already
hold their buffer as a few ranges. However many exporters, most DRM
drivers among them, hold a buffer in system memory as an array of pages
and each would have to open code turning that into a dma_shared_buf.

Add dma_shared_buf_from_pages() to build the dma_shared_buf from an
array of pages. Adjacent pages are merged into one range, the same as
sg_alloc_table_from_pages() does for an sg_table. The dma_shared_buf is
a single allocation which the caller frees with kvfree().

The length of a range is not limited. drm_prime_pages_to_sg() limits a
segment to dma_max_mapping_size() of the exporting device, however the
limit that matters is that of the device the dma_shared_buf is mapped
for, which is not known when it is built. Only dma_shared_buf_map()
could apply it, which it does not do yet.

Signed-off-by: Alistair Popple <apopple@xxxxxxxxxx>
---
drivers/dma-buf/dma-shared-buf.c | 53 ++++++++++++++++++++++++++++++++++++++++
include/linux/dma-shared-buf.h | 3 +++
2 files changed, 56 insertions(+)

diff --git a/drivers/dma-buf/dma-shared-buf.c b/drivers/dma-buf/dma-shared-buf.c
index c346d4c05de8..7e198d0b17e8 100644
--- a/drivers/dma-buf/dma-shared-buf.c
+++ b/drivers/dma-buf/dma-shared-buf.c
@@ -6,6 +6,8 @@
#include <linux/dma-shared-buf.h>
#include <linux/align.h>
#include <linux/iommu.h>
+#include <linux/export.h>
+#include <linux/mm.h>
#include <linux/overflow.h>
#include <linux/pci-p2pdma.h>
#include <linux/scatterlist.h>
@@ -293,3 +295,54 @@ void dma_addr_list_free_sgt(struct sg_table *sgt)
sgt->orig_nents = sgt->nents;
sg_free_table(sgt);
}
+
+/**
+ * dma_shared_buf_from_pages - A dma_shared_buf for an array of pages
+ * @pages: The pages, in order
+ * @nr_pages: Number of entries in @pages
+ *
+ * Builds the dma_shared_buf for memory its owner holds as an array of pages.
+ * Adjacent pages are merged into one range and it has no p2pdma provider and no
+ * DMA attributes, which the caller may set afterwards. The pages must stay
+ * where they are for as long as the dma_shared_buf is in use.
+ *
+ * Returns: the dma_shared_buf, which the caller frees with kvfree(), or a
+ * negative error value wrapped into a pointer.
+ */
+struct dma_shared_buf *dma_shared_buf_from_pages(struct page **pages,
+ unsigned long nr_pages)
+{
+ struct dma_shared_buf *shared_buf;
+ struct phys_vec *vec = NULL;
+ size_t nr_ranges = 0;
+ unsigned long i;
+
+ if (!nr_pages)
+ return ERR_PTR(-EINVAL);
+
+ for (i = 0; i < nr_pages; i++)
+ if (!i || page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1)
+ nr_ranges++;
+
+ shared_buf = kvmalloc(struct_size(shared_buf, ranges, nr_ranges),
+ GFP_KERNEL);
+ if (!shared_buf)
+ return ERR_PTR(-ENOMEM);
+
+ shared_buf->provider = NULL;
+ shared_buf->attrs = 0;
+ shared_buf->size = (size_t)nr_pages << PAGE_SHIFT;
+ shared_buf->nr_ranges = nr_ranges;
+
+ for (i = 0; i < nr_pages; i++) {
+ if (!i || page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1) {
+ vec = vec ? vec + 1 : shared_buf->ranges;
+ vec->paddr = page_to_phys(pages[i]);
+ vec->len = 0;
+ }
+ vec->len += PAGE_SIZE;
+ }
+
+ return shared_buf;
+}
+EXPORT_SYMBOL_NS_GPL(dma_shared_buf_from_pages, "DMA_BUF");
diff --git a/include/linux/dma-shared-buf.h b/include/linux/dma-shared-buf.h
index 62e614b50771..0d5dadc8de5b 100644
--- a/include/linux/dma-shared-buf.h
+++ b/include/linux/dma-shared-buf.h
@@ -11,6 +11,7 @@

struct iommu_domain;
struct p2pdma_provider;
+struct page;
struct sg_table;

/**
@@ -81,5 +82,7 @@ int dma_shared_buf_iommu_map(const struct dma_shared_buf *shared_buf,
int dma_addr_list_to_sgt(struct sg_table *sgt,
const struct dma_addr_list *addrs);
void dma_addr_list_free_sgt(struct sg_table *sgt);
+struct dma_shared_buf *dma_shared_buf_from_pages(struct page **pages,
+ unsigned long nr_pages);

#endif /* _LINUX_DMA_SHARED_BUF_H */

--
2.54.0