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 16/16] drm/xe: Import DMA-bufs as a DMA address list
Date: Tue, 01 Dec 2026 18:06:32 +1100	[thread overview]
Message-ID: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-16-b672717c7fc9@nvidia.com> (raw)
In-Reply-To: <20261201-dmabuf_shared_buf_v1_xe_rc5-v1-0-b672717c7fc9@nvidia.com>

Xe maps an imported DMA-buf with dma_buf_map_sgt() and keeps the
sg_table as the backing of the buffer object. However the only thing it
ever reads from the table is the DMA address and length of each entry,
which the resource cursor walks when the buffer is bound to a VM or
mapped into the GGTT.

Map the attachment with dma_buf_map_dma_list() instead, which returns
just the DMA address ranges of the buffer. The list is kept in struct
xe_bo for as long as the buffer is mapped. It takes over from the sg
pointer in the TTM buffer object, which Xe no longer sets, as the record
of whether the attachment is mapped.

Add xe_res_first_bo_dma() to start a walk over either the list of an
imported buffer or the sg_table of a buffer's own pages, and use it
where an imported buffer can turn up: binding to a VM, mapping into the
GGTT and xe_bo_addr(). The other users of xe_bo_sg() only ever see
buffers which Xe allocated itself so they are left alone.

This works with any exporter. The DMA-buf core copies the addresses out
of the sg_table of an exporter which still maps one itself.

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

---

Note it wouldn't be too difficult to remove sg_table usage entirely
and instead use a dma_shared_buf. However this change is meant as an
example for how drivers could be switched to the new interface, so I
haven't done that.
---
 drivers/gpu/drm/xe/xe_bo.c         | 52 +++++++++++--------------
 drivers/gpu/drm/xe/xe_bo_types.h   |  6 +++
 drivers/gpu/drm/xe/xe_ggtt.c       |  2 +-
 drivers/gpu/drm/xe/xe_pt.c         |  4 +-
 drivers/gpu/drm/xe/xe_res_cursor.h | 78 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 109 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index 14b71d951659..475df4f24966 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -856,37 +856,35 @@ static int xe_bo_move_dmabuf(struct ttm_buffer_object *ttm_bo,
 			     struct ttm_resource *new_res)
 {
 	struct dma_buf_attachment *attach = ttm_bo->base.import_attach;
-	struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt,
-					       ttm);
+	struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
 	struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
 	bool device_unplugged = drm_dev_is_unplugged(&xe->drm);
-	struct sg_table *sg;
+	const struct dma_addr_list *list;
 
 	xe_assert(xe, attach);
 	xe_assert(xe, ttm_bo->ttm);
 
 	if (device_unplugged && new_res->mem_type == XE_PL_SYSTEM &&
-	    ttm_bo->sg) {
+	    bo->dma_list) {
 		dma_resv_wait_timeout(ttm_bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
 				      false, MAX_SCHEDULE_TIMEOUT);
-		dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
-		ttm_bo->sg = NULL;
+		dma_buf_unmap_dma_list(attach, bo->dma_list, DMA_BIDIRECTIONAL);
+		bo->dma_list = NULL;
 	}
 
 	if (new_res->mem_type == XE_PL_SYSTEM)
 		goto out;
 
-	if (ttm_bo->sg) {
-		dma_buf_unmap_sgt(attach, ttm_bo->sg, DMA_BIDIRECTIONAL);
-		ttm_bo->sg = NULL;
+	if (bo->dma_list) {
+		dma_buf_unmap_dma_list(attach, bo->dma_list, DMA_BIDIRECTIONAL);
+		bo->dma_list = NULL;
 	}
 
-	sg = dma_buf_map_sgt(attach, DMA_BIDIRECTIONAL);
-	if (IS_ERR(sg))
-		return PTR_ERR(sg);
+	list = dma_buf_map_dma_list(attach, DMA_BIDIRECTIONAL);
+	if (IS_ERR(list))
+		return PTR_ERR(list);
 
-	ttm_bo->sg = sg;
-	xe_tt->sg = sg;
+	bo->dma_list = list;
 
 out:
 	ttm_bo_move_null(ttm_bo, new_res);
@@ -1749,12 +1747,11 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
 	if (tt) {
 		struct xe_ttm_tt *xe_tt = container_of(tt, typeof(*xe_tt), ttm);
 
-		if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
-			dma_buf_unmap_sgt(ttm_bo->base.import_attach,
-					  ttm_bo->sg,
-					  DMA_BIDIRECTIONAL);
-			ttm_bo->sg = NULL;
-			xe_tt->sg = NULL;
+		if (bo->dma_list) {
+			dma_buf_unmap_dma_list(ttm_bo->base.import_attach,
+					       bo->dma_list,
+					       DMA_BIDIRECTIONAL);
+			bo->dma_list = NULL;
 		} else if (xe_tt->sg) {
 			dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev,
 					  xe_tt->sg,
@@ -1863,14 +1860,10 @@ static void xe_ttm_bo_delete_mem_notify(struct ttm_buffer_object *ttm_bo)
 	 * Object is idle and about to be destroyed. Release the
 	 * dma-buf attachment.
 	 */
-	if (ttm_bo->type == ttm_bo_type_sg && ttm_bo->sg) {
-		struct xe_ttm_tt *xe_tt = container_of(ttm_bo->ttm,
-						       struct xe_ttm_tt, ttm);
-
-		dma_buf_unmap_sgt(ttm_bo->base.import_attach, ttm_bo->sg,
-				  DMA_BIDIRECTIONAL);
-		ttm_bo->sg = NULL;
-		xe_tt->sg = NULL;
+	if (bo->dma_list) {
+		dma_buf_unmap_dma_list(ttm_bo->base.import_attach, bo->dma_list,
+				       DMA_BIDIRECTIONAL);
+		bo->dma_list = NULL;
 	}
 }
 
@@ -3453,8 +3446,7 @@ dma_addr_t __xe_bo_addr(struct xe_bo *bo, u64 offset, size_t page_size)
 	if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
 		xe_assert(xe, bo->ttm.ttm);
 
-		xe_res_first_sg(xe_bo_sg(bo), page << PAGE_SHIFT,
-				page_size, &cur);
+		xe_res_first_bo_dma(bo, page << PAGE_SHIFT, page_size, &cur);
 		return xe_res_dma(&cur) + offset;
 	} else {
 		struct xe_res_cursor cur;
diff --git a/drivers/gpu/drm/xe/xe_bo_types.h b/drivers/gpu/drm/xe/xe_bo_types.h
index 8ec4a01a0092..dd00fae0610e 100644
--- a/drivers/gpu/drm/xe/xe_bo_types.h
+++ b/drivers/gpu/drm/xe/xe_bo_types.h
@@ -17,6 +17,7 @@
 #include "xe_device_types.h"
 #include "xe_ggtt_types.h"
 
+struct dma_addr_list;
 struct xe_device;
 struct xe_mem_pool_node;
 struct xe_vm;
@@ -39,6 +40,11 @@ struct xe_bo {
 	struct xe_bo *parent_obj;
 	/** @dma_buf: Imported dma-buf ref to keep its resv alive. */
 	struct dma_buf *dma_buf;
+	/**
+	 * @dma_list: DMA addresses of an imported dma-buf whilst it is mapped,
+	 * NULL otherwise. Protected by the BO dma_resv.
+	 */
+	const struct dma_addr_list *dma_list;
 	/** @flags: flags for this buffer object */
 	u32 flags;
 	/** @vm: VM this BO is attached to, for extobj this will be NULL */
diff --git a/drivers/gpu/drm/xe/xe_ggtt.c b/drivers/gpu/drm/xe/xe_ggtt.c
index b7c55dc8a0bb..21ea116a5f1c 100644
--- a/drivers/gpu/drm/xe/xe_ggtt.c
+++ b/drivers/gpu/drm/xe/xe_ggtt.c
@@ -741,7 +741,7 @@ static void xe_ggtt_map_bo(struct xe_ggtt *ggtt, struct xe_ggtt_node *node,
 	if (!xe_bo_is_vram(bo) && !xe_bo_is_stolen(bo)) {
 		xe_assert(xe_bo_device(bo), bo->ttm.ttm);
 
-		for (xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &cur);
+		for (xe_res_first_bo_dma(bo, 0, xe_bo_size(bo), &cur);
 		     cur.remaining; xe_res_next(&cur, XE_PAGE_SIZE))
 			ggtt->pt_ops->ggtt_set_pte(ggtt, end - cur.remaining,
 						   pte | xe_res_dma(&cur));
diff --git a/drivers/gpu/drm/xe/xe_pt.c b/drivers/gpu/drm/xe/xe_pt.c
index aeeac0f1ab83..b745de5210df 100644
--- a/drivers/gpu/drm/xe/xe_pt.c
+++ b/drivers/gpu/drm/xe/xe_pt.c
@@ -887,8 +887,8 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
 			xe_res_first(bo->ttm.resource, xe_vma_bo_offset(vma),
 				     xe_vma_size(vma), &curs);
 		else
-			xe_res_first_sg(xe_bo_sg(bo), xe_vma_bo_offset(vma),
-					xe_vma_size(vma), &curs);
+			xe_res_first_bo_dma(bo, xe_vma_bo_offset(vma),
+					    xe_vma_size(vma), &curs);
 	} else if (!range) {
 		curs.size = xe_vma_size(vma);
 	}
diff --git a/drivers/gpu/drm/xe/xe_res_cursor.h b/drivers/gpu/drm/xe/xe_res_cursor.h
index c3a037e5f34e..e6541b0c3eb0 100644
--- a/drivers/gpu/drm/xe/xe_res_cursor.h
+++ b/drivers/gpu/drm/xe/xe_res_cursor.h
@@ -24,6 +24,7 @@
 #ifndef _XE_RES_CURSOR_H_
 #define _XE_RES_CURSOR_H_
 
+#include <linux/dma-buf.h>
 #include <linux/scatterlist.h>
 
 #include <drm/drm_pagemap.h>
@@ -57,6 +58,8 @@ struct xe_res_cursor {
 	struct scatterlist *sgl;
 	/** @dma_addr: Current element in a struct drm_pagemap_addr array */
 	const struct drm_pagemap_addr *dma_addr;
+	/** @dma_range: Current range of a struct dma_addr_list */
+	const struct dma_addr_range *dma_range;
 	/** @mm: Buddy allocator for VRAM cursor */
 	struct gpu_buddy *mm;
 	/**
@@ -93,6 +96,7 @@ static inline void xe_res_first(struct ttm_resource *res,
 {
 	cur->sgl = NULL;
 	cur->dma_addr = NULL;
+	cur->dma_range = NULL;
 	if (!res)
 		goto fallback;
 
@@ -172,6 +176,21 @@ static inline void __xe_res_sg_next(struct xe_res_cursor *cur)
 	cur->sgl = sgl;
 }
 
+static inline void __xe_res_dma_list_next(struct xe_res_cursor *cur)
+{
+	const struct dma_addr_range *range = cur->dma_range;
+	u64 start = cur->start;
+
+	while (start >= range->len) {
+		start -= range->len;
+		range++;
+	}
+
+	cur->start = start;
+	cur->size = range->len - start;
+	cur->dma_range = range;
+}
+
 /**
  * __xe_res_dma_next() - Advance the cursor when end-of-segment is reached
  * @cur: The cursor
@@ -222,11 +241,61 @@ static inline void xe_res_first_sg(const struct sg_table *sg,
 	cur->remaining = size;
 	cur->size = 0;
 	cur->dma_addr = NULL;
+	cur->dma_range = NULL;
 	cur->sgl = sg->sgl;
 	cur->mem_type = XE_PL_TT;
 	__xe_res_sg_next(cur);
 }
 
+/**
+ * xe_res_first_dma_list - initialize a xe_res_cursor with a DMA address list
+ *
+ * @list: DMA address list of an imported dma-buf to walk
+ * @start: Start of the range
+ * @size: Size of the range
+ * @cur: cursor object to initialize
+ *
+ * Start walking over the range of allocations between @start and @size.
+ */
+static inline void xe_res_first_dma_list(const struct dma_addr_list *list,
+					 u64 start, u64 size,
+					 struct xe_res_cursor *cur)
+{
+	XE_WARN_ON(!list);
+	XE_WARN_ON(start + size > list->size);
+	cur->node = NULL;
+	cur->start = start;
+	cur->remaining = size;
+	cur->size = 0;
+	cur->dma_addr = NULL;
+	cur->sgl = NULL;
+	cur->dma_range = list->ranges;
+	cur->mem_type = XE_PL_TT;
+	__xe_res_dma_list_next(cur);
+}
+
+/**
+ * xe_res_first_bo_dma - initialize a xe_res_cursor with the DMA addresses of
+ * a buffer object
+ *
+ * @bo: buffer object to walk, which must not be in VRAM or stolen memory
+ * @start: Start of the range
+ * @size: Size of the range
+ * @cur: cursor object to initialize
+ *
+ * Start walking over the range of allocations between @start and @size. The
+ * addresses are those of the exporter's DMA address list for an imported
+ * dma-buf and those of the buffer object's own pages otherwise.
+ */
+static inline void xe_res_first_bo_dma(struct xe_bo *bo, u64 start, u64 size,
+				       struct xe_res_cursor *cur)
+{
+	if (bo->dma_list)
+		xe_res_first_dma_list(bo->dma_list, start, size, cur);
+	else
+		xe_res_first_sg(xe_bo_sg(bo), start, size, cur);
+}
+
 /**
  * xe_res_first_dma - initialize a xe_res_cursor with dma_addr array
  *
@@ -255,6 +324,7 @@ static inline void xe_res_first_dma(const struct drm_pagemap_addr *dma_addr,
 	cur->dma_addr = dma_addr;
 	__xe_res_dma_next(cur);
 	cur->sgl = NULL;
+	cur->dma_range = NULL;
 	cur->mem_type = XE_PL_TT;
 }
 
@@ -296,6 +366,12 @@ static inline void xe_res_next(struct xe_res_cursor *cur, u64 size)
 		return;
 	}
 
+	if (cur->dma_range) {
+		cur->start += size;
+		__xe_res_dma_list_next(cur);
+		return;
+	}
+
 	switch (cur->mem_type) {
 	case XE_PL_STOLEN:
 		/* Just advance within the contiguous region. */
@@ -339,6 +415,8 @@ static inline u64 xe_res_dma(const struct xe_res_cursor *cur)
 		return cur->dma_start + cur->start;
 	else if (cur->sgl)
 		return sg_dma_address(cur->sgl) + cur->start;
+	else if (cur->dma_range)
+		return cur->dma_range->addr + cur->start;
 	else
 		return cur->start;
 }

-- 
2.54.0


      parent reply	other threads:[~2026-12-01  7:08 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 ` [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 ` Alistair Popple [this message]

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-16-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®