Skip to contentSkip to conversation

drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs

PATCH In review Matthew Brost 6 participants 145 messages Jul 11 – Nov 24 Page 3 of 3

Subsystems (7)
lore
Share & tools
lore
#114 lore raw

Re: [PATCH v3 28/33] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs

In reply to Matthew Brost
Quoted 170 lines · Matthew Brost

Add support for mapping a ttm_tt's pages using the IOVA-based DMA API (dma_iova_try_alloc()/link()/sync()/unlink()/free()) as an alternative to the scatter-gather list. An IOVA mapping is a single contiguous DMA range, which is cheaper to walk and program than an sg list.

Whether the IOVA API can be used depends on the IOMMU mode, so the decision is made once per tt at xe_ttm_tt_create() time and recorded in struct xe_ttm_tt::use_iova. To keep the scope limited, only device BOs that are not GGTT-mapped are eligible: dma-buf imports manage their own mapping and GGTT bindings are programmed directly from the sg list. dma_iova_try_alloc() may still fail, in which case the tt transparently falls back to the sg list, which remains the default.

The IOVA reservation is allocated at tt creation and freed at tt destroy. xe_tt_map_iova() links the tt pages (coalescing physically contiguous runs) and syncs the IOTLB; xe_tt_unmap_iova() unlinks them but keeps the reservation so the tt can be remapped without re-allocating IOVA space. xe_tt_map()/xe_tt_unmap() dispatch between the IOVA and sg backends, and xe_bo_dma_unmap_pinned() learns about the IOVA case.

Consumers that walked the sg list via xe_res_first_sg(xe_bo_sg()) now go through the new xe_res_first_tt() helper, which initializes either an IOVA cursor (xe_res_first_iova()) or an sg cursor depending on the tt's mapping mode.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

drivers/gpu/drm/xe/tests/xe_migrate.c |   6 +-
drivers/gpu/drm/xe/xe_bo.c            | 239 ++++++++++++++++++++++++--
drivers/gpu/drm/xe/xe_bo.h            |   5 +-
drivers/gpu/drm/xe/xe_ggtt.c          |   2 +-
drivers/gpu/drm/xe/xe_migrate.c       |  14 +-
drivers/gpu/drm/xe/xe_pt.c            |   2 +-
6 files changed, 237 insertions(+), 31 deletions(-)
drivers/gpu/drm/xe/tests/xe_migrate.c +3 -3
index 41de648b2c86..119134e67c89 100644
@@ -253,7 +253,7 @@ static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test,
 	if (xe_bo_is_vram(pt))
 		xe_res_first(pt->ttm.resource, 0, xe_bo_size(pt), &src_it);
 	else
-		xe_res_first_sg(xe_bo_sg(pt), 0, xe_bo_size(pt), &src_it);
+		xe_res_first_tt(pt->ttm.ttm, 0, xe_bo_size(pt), &src_it);
 
 	emit_pte(m, bb, NUM_KERNEL_PDE - 1, xe_bo_is_vram(pt), false,
 		 &src_it, XE_PAGE_SIZE, pt->ttm.resource);
@@ -385,12 +385,12 @@ static struct dma_fence *blt_copy(struct xe_tile *tile,
 	bool dst_is_vram = mem_type_is_vram(dst->mem_type);
 
 	if (!src_is_vram)
-		xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
+		xe_res_first_tt(src_bo->ttm.ttm, 0, size, &src_it);
 	else
 		xe_res_first(src, 0, size, &src_it);
 
 	if (!dst_is_vram)
-		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
drivers/gpu/drm/xe/xe_bo.c +63 -0
index caf5aa0bab53..d54e69aefeba 100644
@@ -6,6 +6,7 @@
 #include "xe_bo.h"
 
 #include <linux/dma-buf.h>
+#include <linux/dma-mapping.h>
 #include <linux/nospec.h>
 
 #include <drm/drm_drv.h>
@@ -15,6 +16,7 @@
 #include <drm/ttm/ttm_backup.h>
 #include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_pool.h>
 #include <drm/ttm/ttm_tt.h>
 #include <uapi/drm/xe_drm.h>
 
@@ -420,6 +422,32 @@ struct xe_ttm_tt {
 	struct ttm_tt ttm;
 	struct sg_table sgt;
 	struct sg_table *sg;
+	/**
+	 * @iova: IOVA state describing the single contiguous IOVA mapping of
+	 * the tt pages. Only valid when @use_iova is true.
+	 */
+	struct dma_iova_state iova;
+	/**
+	 * @use_iova: Whether this tt is DMA mapped using the IOVA-based DMA
+	 * API instead of a scatter-gather list. Decided once at tt creation
+	 * time and stable for the lifetime of the tt.
+	 */
+	bool use_iova;
+	/**
+	 * @iova_linked: Whether the tt pages are currently linked into the
+	 * IOVA mapping. Only meaningful when @use_iova is true.
+	 */
+	bool iova_linked;
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+	/**
+	 * @iova_dma_pages: Per-order count of DMA-mapped pages this tt
+	 * accounted into xe->mem.dma_mapped_pages[] for its IOVA mapping,
+	 * recorded at link time so the unlink can apply an exactly matching
+	 * decrement without re-walking tt->pages (which TTM may mutate, e.g.
+	 * during defrag). Counter ownership stays isolated in xe_bo.c.
+	 */
+	unsigned long iova_dma_pages[NR_PAGE_ORDERS];
+#endif
 	/** @purgeable: Whether the content of the pages of @ttm is purgeable. */
 	bool purgeable;
 };
@@ -468,6 +496,52 @@ static void xe_tt_account_dma_pages(struct xe_device *xe,
 }
 #endif
 
+/*
+ * xe_tt_account_iova_add - account one freshly linked IOVA folio
+ * @xe: the xe device
+ * @xe_tt: the xe_ttm_tt being mapped
+ * @order: page order of the linked folio
+ *
+ * Add the folio's 2^order pages to xe->mem.dma_mapped_pages[order] and record
+ * the same amount in @xe_tt so xe_tt_unaccount_iova() can later subtract an
+ * exactly matching count without re-walking tt->pages.
+ */
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+static void xe_tt_account_iova_add(struct xe_device *xe,
+				   struct xe_ttm_tt *xe_tt, unsigned int order)
+{
+	unsigned long chunk = 1UL << order;
+
+	xe_tt->iova_dma_pages[order] += chunk;
+	atomic_long_add(chunk, &xe->mem.dma_mapped_pages[order]);
+}
+
+static void xe_tt_unaccount_iova(struct xe_device *xe,
+				 struct xe_ttm_tt *xe_tt)
+{
+	unsigned int order;
+
+	for (order = 0; order < NR_PAGE_ORDERS; order++) {
+		unsigned long pages = xe_tt->iova_dma_pages[order];
+
+		if (pages) {
+			atomic_long_sub(pages, &xe->mem.dma_mapped_pages[order]);
+			xe_tt->iova_dma_pages[order] = 0;
+		}
+	}
+}
+#else

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_account_dma_pages(struct xe_device *xe, · line 543

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+static void xe_tt_account_iova_add(struct xe_device *xe,
+				   struct xe_ttm_tt *xe_tt, unsigned int order)
+{
+}
+
+static void xe_tt_unaccount_iova(struct xe_device *xe,
+				 struct xe_ttm_tt *xe_tt)
+{
+}
+#endif
Assisted-by LLM

NIT..As mentioned in patch 12, I am not sure if CONFIG_DEBUG_FS is the right config for this. May be XE_DEBUG would be better?

Quoted 24 lines
drivers/gpu/drm/xe/xe_bo.c +13 -2
+
 static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt)
 {
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
@@ -514,28 +588,129 @@ static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt)
 	}
 }
 
-struct sg_table *xe_bo_sg(struct xe_bo *bo)
+/*
+ * xe_tt_map_iova - DMA map a tt using the IOVA-based DMA API.
+ *
+ * Link all of the tt's pages into the IOVA range reserved at tt creation
+ * time, one folio at a time, and sync the IOTLB once at the end. The IOVA
+ * reservation itself is kept across map/unmap cycles and only released when
+ * the tt is destroyed.
+ */
+static int xe_tt_map_iova(struct xe_device *xe, struct ttm_tt *tt)
 {
-	struct ttm_tt *tt = bo->ttm.ttm;
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+	struct device *dev = xe->drm.dev;
+	unsigned long num_pages = tt->num_pages;
+	unsigned long i = 0;

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt) · line 614

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+	size_t mapped = 0;
+	int ret;
+
+	if (xe_tt->iova_linked)
+		return 0;
+
+	while (i < num_pages) {
+		struct page *page = tt->pages[i];
+		unsigned int order = ttm_pool_page_order_nodma(page);
+		size_t len = PAGE_SIZE << order;

NIT...as ttm_pool_page_order_nodma() is works only at chunk head (where order is stored in page->private), it should be fine. But if we ever endup here for pages[i] other than chunk head, it would become hard to debug. Is there a way we can assert for that here?

Quoted 119 lines
drivers/gpu/drm/xe/xe_bo.c +94 -9
+
+		ret = dma_iova_link(dev, &xe_tt->iova, page_to_phys(page),
+				    mapped, len, DMA_BIDIRECTIONAL,
+				    DMA_ATTR_SKIP_CPU_SYNC);
+		if (ret)
+			goto err_unlink;
 
-	return xe_tt->sg;
+		xe_tt_account_iova_add(xe, xe_tt, order);
+		mapped += len;
+		i += 1UL << order;
+	}
+
+	ret = dma_iova_sync(dev, &xe_tt->iova, 0, mapped);
+	if (ret)
+		goto err_unlink;
+
+	xe_tt->iova_linked = true;
+	return 0;
+
+err_unlink:
+	if (mapped) {
+		dma_iova_unlink(dev, &xe_tt->iova, 0, mapped,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+		xe_tt_unaccount_iova(xe, xe_tt);
+	}
+	return ret;
 }
 
-/**
- * xe_tt_sg() - Return the DMA scatter-gather table backing a ttm_tt.
- * @tt: The ttm_tt to query.
+/*
+ * xe_tt_unmap_iova - Unlink a tt's pages from its IOVA mapping.
 *
- * Like xe_bo_sg(), but takes the ttm_tt directly. Used by the defrag copy
- * path which needs the old (source) tt's sg table without a backing xe_bo.
+ * Unlink the pages but keep the IOVA reservation so the tt can be remapped
+ * later without re-allocating IOVA space.
+ */
+static void xe_tt_unmap_iova(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->iova_linked) {
+		/*
+		 * Unmap with attrs 0 (matching the sg unmap path) so that the
+		 * CPU copy-back sync is performed for non-coherent or bounced
+		 * (SWIOTLB) mappings, even though the link was done with
+		 * DMA_ATTR_SKIP_CPU_SYNC.
+		 */
+		dma_iova_unlink(xe->drm.dev, &xe_tt->iova, 0,
+				dma_iova_size(&xe_tt->iova),
+				DMA_BIDIRECTIONAL, 0);
+		xe_tt_unaccount_iova(xe, xe_tt);
+		xe_tt->iova_linked = false;
+	}
+}
+
+/*
+ * xe_tt_map - DMA map a tt, using IOVA when available and sg otherwise.
+ */
+static int xe_tt_map(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->use_iova)
+		return xe_tt_map_iova(xe, tt);
+
+	return xe_tt_map_sg(xe, tt);
+}
+
+/*
+ * xe_tt_unmap - Undo xe_tt_map().
+ */
+static void xe_tt_unmap(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->use_iova)
+		xe_tt_unmap_iova(xe, tt);
+	else
+		xe_tt_unmap_sg(xe, tt);
+}
+
+/**
+ * xe_res_first_tt() - Initialize a resource cursor over a ttm_tt's DMA mapping.
+ * @tt: The ttm_tt to walk.
+ * @start: Start of the range, in bytes.
+ * @size: Size of the range, in bytes.
+ * @cur: The cursor to initialize.
 *
- * Return: The sg table, or NULL if the tt is not currently mapped.
+ * Initialize @cur to walk the DMA addresses backing @tt, transparently
+ * handling both the IOVA-based mapping (single contiguous range) and the
+ * scatter-gather list fallback.
 */
-struct sg_table *xe_tt_sg(struct ttm_tt *tt)
+void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
+		     struct xe_res_cursor *cur)
 {
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
 
-	return xe_tt->sg;
+	if (xe_tt->use_iova)
+		xe_res_first_iova(&xe_tt->iova, start, size, cur);
+	else
+		xe_res_first_sg(xe_tt->sg, start, size, cur);
 }
 
 /*
@@ -651,6 +826,21 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
 		}
 	}
 
+	/*
+	 * Eligible BOs are DMA mapped using the IOVA-based DMA API, which
+	 * yields a single contiguous IOVA range instead of a scatter-gather
+	 * list. Only device BOs that are not GGTT-mapped qualify: dma-buf

drivers/gpu/drm/xe/xe_bo.c static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo, · line 842

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+	 * imports manage their own mapping and GGTT bindings are programmed
+	 * directly from the sg list. dma_iova_try_alloc() may still fail
+	 * depending on the IOMMU mode, in which case we transparently fall
+	 * back to the sg list. The reservation lives for the tt's lifetime.
+	 */
+	if (ttm_bo->type != ttm_bo_type_sg &&
+	    !(bo->flags & (XE_BO_FLAG_GGTT | XE_BO_FLAG_GGTT_ALL)) &&
+	    dma_iova_try_alloc(xe->drm.dev, &xe_tt->iova, 0,
+			       (size_t)tt->num_pages << PAGE_SHIFT))
+		xe_tt->use_iova = true;

Shouldn't we be allocating 2M aligned iova here by passing 2M to 'phys' instead of 0? We need both 2M aligned iova and 2M aligned physical address (beneficial order) which defragmentation expected to provide by benefecial order, right?

Niranjana

Quoted 186 lines
drivers/gpu/drm/xe/xe_bo.c +22 -6
+
 	return tt;
 }
 
@@ -700,7 +890,7 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 	    !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE))
 		return;
 
-	xe_tt_unmap_sg(xe, tt);
+	xe_tt_unmap(xe, tt);
 
 	ttm_pool_free(&ttm_dev->pool, tt);
 	xe_ttm_tt_account_subtract(xe, tt);
@@ -709,6 +899,19 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 
 static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 {
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	/*
+	 * Release the IOVA reservation made in xe_ttm_tt_create(). The pages
+	 * must already have been unlinked by xe_tt_unmap().
+	 */
+	if (xe_tt->use_iova) {
+		struct xe_device *xe = ttm_to_xe_device(ttm_dev);
+
+		xe_assert(xe, !xe_tt->iova_linked);
+		dma_iova_free(xe->drm.dev, &xe_tt->iova);
+	}
+
 	ttm_tt_fini(tt);
 	kfree(tt);
 }
@@ -1557,7 +1760,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	/* Bo creation path, moving to system or TT. */
 	if ((!old_mem && ttm) && !handle_system_ccs) {
 		if (new_mem->mem_type == XE_PL_TT)
-			ret = xe_tt_map_sg(xe, ttm);
+			ret = xe_tt_map(xe, ttm);
 		if (!ret)
 			ttm_bo_move_null(ttm_bo, new_mem);
 		goto out;
@@ -1587,7 +1790,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		(!ttm && ttm_bo->type == ttm_bo_type_device);
 
 	if (new_mem->mem_type == XE_PL_TT) {
-		ret = xe_tt_map_sg(xe, ttm);
+		ret = xe_tt_map(xe, ttm);
 		if (ret)
 			goto out;
 	}
@@ -1757,7 +1960,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		if (IS_VF_CCS_READY(xe))
 			xe_sriov_vf_ccs_detach_bo(bo);
 
-		xe_tt_unmap_sg(xe, ttm_bo->ttm);
+		xe_tt_unmap(xe, ttm_bo->ttm);
 	}
 
 	xe_bo_defrag_update(bo);
@@ -1785,7 +1988,7 @@ static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx,
 		if (lret)
 			return lret;
 
-		xe_tt_unmap_sg(xe, bo->ttm);
+		xe_tt_unmap(xe, bo->ttm);
 		ttm_bo_move_null(bo, new_resource);
 	}
 
@@ -2212,6 +2415,8 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
 						 DMA_BIDIRECTIONAL);
 			ttm_bo->sg = NULL;
 			xe_tt->sg = NULL;
+		} else if (xe_tt->use_iova) {
+			xe_tt_unmap_iova(ttm_to_xe_device(ttm_bo->bdev), tt);
 		} else if (xe_tt->sg) {
 			dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev,
 					  xe_tt->sg,
@@ -3779,7 +3984,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,
+		xe_res_first_tt(bo->ttm.ttm, page << PAGE_SHIFT,
 				page_size, &cur);
 		return xe_res_dma(&cur) + offset;
 	} else {
drivers/gpu/drm/xe/xe_bo.h +3 -2
index 18fcccd082b7..3e2eaefecb8f 100644
@@ -552,8 +552,9 @@ void xe_bo_dev_init(struct xe_bo_dev *bo_device);
 
 void xe_bo_dev_fini(struct xe_bo_dev *bo_device);
 
-struct sg_table *xe_bo_sg(struct xe_bo *bo);
-struct sg_table *xe_tt_sg(struct ttm_tt *tt);
+struct xe_res_cursor;
+void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
+		     struct xe_res_cursor *cur);
 
 /*
 * xe_sg_segment_size() - Provides upper limit for sg segment size.
drivers/gpu/drm/xe/xe_ggtt.c +1 -1
index 8ec23862477f..4084bc183e06 100644
@@ -699,7 +699,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_tt(bo->ttm.ttm, 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));
drivers/gpu/drm/xe/xe_migrate.c +7 -7
index 9990a7144a9f..666c715a9067 100644
@@ -1005,16 +1005,16 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		return ERR_PTR(-EINVAL);
 
 	if (!src_is_vram)
-		xe_res_first_sg(src_tt ? xe_tt_sg(src_tt) : xe_bo_sg(src_bo), 0, size, &src_it);
+		xe_res_first_tt(src_tt ? src_tt : src_bo->ttm.ttm, 0, size, &src_it);
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
 	if (copy_system_ccs)
-		xe_res_first_sg(src_tt ? xe_tt_sg(src_tt) : xe_bo_sg(src_bo),
+		xe_res_first_tt(src_tt ? src_tt : src_bo->ttm.ttm,
 				xe_bo_ccs_pages_start(src_bo),
 				PAGE_ALIGN(xe_device_ccs_bytes(xe, size)),
 				&ccs_it);
@@ -1383,9 +1383,9 @@ int xe_migrate_ccs_rw_copy(struct xe_tile *tile, struct xe_exec_queue *q,
 
 	ctx = &xe->sriov.vf.ccs.contexts[read_write];
 
-	xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
+	xe_res_first_tt(src_bo->ttm.ttm, 0, size, &src_it);
 
-	xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo),
+	xe_res_first_tt(src_bo->ttm.ttm, xe_bo_ccs_pages_start(src_bo),
 			PAGE_ALIGN(xe_device_ccs_bytes(xe, size)),
 			&ccs_it);
 
@@ -1588,7 +1588,7 @@ struct dma_fence *xe_migrate_vram_copy_chunk(struct xe_bo *vram_bo, u64 vram_off
 	xe_assert(xe, !range_overflows(sysmem_offset, size, (u64)sysmem_bo->ttm.base.size));
 
 	xe_res_first(vram, vram_offset, size, &vram_it);
-	xe_res_first_sg(xe_bo_sg(sysmem_bo), sysmem_offset, size, &sysmem_it);
+	xe_res_first_tt(sysmem_bo->ttm.ttm, sysmem_offset, size, &sysmem_it);
 
 	while (size) {
 		u32 pte_flags = PTE_UPDATE_FLAG_IS_VRAM;
@@ -1800,7 +1800,7 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 		clear_only_system_ccs = true;
 
 	if (!clear_vram)
-		xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &src_it);
+		xe_res_first_tt(bo->ttm.ttm, 0, xe_bo_size(bo), &src_it);
 	else
 		xe_res_first(src, 0, xe_bo_size(bo), &src_it);
 
drivers/gpu/drm/xe/xe_pt.c +1 -1
index 7111b5d0f6aa..185d15fb0a97 100644
@@ -797,7 +797,7 @@ 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_res_first_tt(bo->ttm.ttm, xe_vma_bo_offset(vma),
 					xe_vma_size(vma), &curs);
 	} else if (!range) {
 		curs.size = xe_vma_size(vma);
Signature · 1 line
2.34.1
author maintainer
#115 lore raw

Re: [PATCH v3 28/33] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs

Quoted 175 lines · Niranjana Vishwanathapura
Quoted 174 lines · Matthew Brost

Add support for mapping a ttm_tt's pages using the IOVA-based DMA API (dma_iova_try_alloc()/link()/sync()/unlink()/free()) as an alternative to the scatter-gather list. An IOVA mapping is a single contiguous DMA range, which is cheaper to walk and program than an sg list.

Whether the IOVA API can be used depends on the IOMMU mode, so the decision is made once per tt at xe_ttm_tt_create() time and recorded in struct xe_ttm_tt::use_iova. To keep the scope limited, only device BOs that are not GGTT-mapped are eligible: dma-buf imports manage their own mapping and GGTT bindings are programmed directly from the sg list. dma_iova_try_alloc() may still fail, in which case the tt transparently falls back to the sg list, which remains the default.

The IOVA reservation is allocated at tt creation and freed at tt destroy. xe_tt_map_iova() links the tt pages (coalescing physically contiguous runs) and syncs the IOTLB; xe_tt_unmap_iova() unlinks them but keeps the reservation so the tt can be remapped without re-allocating IOVA space. xe_tt_map()/xe_tt_unmap() dispatch between the IOVA and sg backends, and xe_bo_dma_unmap_pinned() learns about the IOVA case.

Consumers that walked the sg list via xe_res_first_sg(xe_bo_sg()) now go through the new xe_res_first_tt() helper, which initializes either an IOVA cursor (xe_res_first_iova()) or an sg cursor depending on the tt's mapping mode.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

drivers/gpu/drm/xe/tests/xe_migrate.c |   6 +-
drivers/gpu/drm/xe/xe_bo.c            | 239 ++++++++++++++++++++++++--
drivers/gpu/drm/xe/xe_bo.h            |   5 +-
drivers/gpu/drm/xe/xe_ggtt.c          |   2 +-
drivers/gpu/drm/xe/xe_migrate.c       |  14 +-
drivers/gpu/drm/xe/xe_pt.c            |   2 +-
6 files changed, 237 insertions(+), 31 deletions(-)
drivers/gpu/drm/xe/tests/xe_migrate.c +3 -3
index 41de648b2c86..119134e67c89 100644
@@ -253,7 +253,7 @@ static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test,
 	if (xe_bo_is_vram(pt))
 		xe_res_first(pt->ttm.resource, 0, xe_bo_size(pt), &src_it);
 	else
-		xe_res_first_sg(xe_bo_sg(pt), 0, xe_bo_size(pt), &src_it);
+		xe_res_first_tt(pt->ttm.ttm, 0, xe_bo_size(pt), &src_it);
 
 	emit_pte(m, bb, NUM_KERNEL_PDE - 1, xe_bo_is_vram(pt), false,
 		 &src_it, XE_PAGE_SIZE, pt->ttm.resource);
@@ -385,12 +385,12 @@ static struct dma_fence *blt_copy(struct xe_tile *tile,
 	bool dst_is_vram = mem_type_is_vram(dst->mem_type);
 
 	if (!src_is_vram)
-		xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
+		xe_res_first_tt(src_bo->ttm.ttm, 0, size, &src_it);
 	else
 		xe_res_first(src, 0, size, &src_it);
 
 	if (!dst_is_vram)
-		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
drivers/gpu/drm/xe/xe_bo.c +67 -0
index caf5aa0bab53..d54e69aefeba 100644
@@ -6,6 +6,7 @@
 #include "xe_bo.h"
 
 #include <linux/dma-buf.h>
+#include <linux/dma-mapping.h>
 #include <linux/nospec.h>
 
 #include <drm/drm_drv.h>
@@ -15,6 +16,7 @@
 #include <drm/ttm/ttm_backup.h>
 #include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_pool.h>
 #include <drm/ttm/ttm_tt.h>
 #include <uapi/drm/xe_drm.h>
 
@@ -420,6 +422,32 @@ struct xe_ttm_tt {
 	struct ttm_tt ttm;
 	struct sg_table sgt;
 	struct sg_table *sg;
+	/**
+	 * @iova: IOVA state describing the single contiguous IOVA mapping of
+	 * the tt pages. Only valid when @use_iova is true.
+	 */
+	struct dma_iova_state iova;
+	/**
+	 * @use_iova: Whether this tt is DMA mapped using the IOVA-based DMA
+	 * API instead of a scatter-gather list. Decided once at tt creation
+	 * time and stable for the lifetime of the tt.
+	 */
+	bool use_iova;
+	/**
+	 * @iova_linked: Whether the tt pages are currently linked into the
+	 * IOVA mapping. Only meaningful when @use_iova is true.
+	 */
+	bool iova_linked;
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+	/**
+	 * @iova_dma_pages: Per-order count of DMA-mapped pages this tt
+	 * accounted into xe->mem.dma_mapped_pages[] for its IOVA mapping,
+	 * recorded at link time so the unlink can apply an exactly matching
+	 * decrement without re-walking tt->pages (which TTM may mutate, e.g.
+	 * during defrag). Counter ownership stays isolated in xe_bo.c.
+	 */
+	unsigned long iova_dma_pages[NR_PAGE_ORDERS];
+#endif
 	/** @purgeable: Whether the content of the pages of @ttm is purgeable. */
 	bool purgeable;
 };
@@ -468,6 +496,52 @@ static void xe_tt_account_dma_pages(struct xe_device *xe,
 }
 #endif
 
+/*
+ * xe_tt_account_iova_add - account one freshly linked IOVA folio
+ * @xe: the xe device
+ * @xe_tt: the xe_ttm_tt being mapped
+ * @order: page order of the linked folio
+ *
+ * Add the folio's 2^order pages to xe->mem.dma_mapped_pages[order] and record
+ * the same amount in @xe_tt so xe_tt_unaccount_iova() can later subtract an
+ * exactly matching count without re-walking tt->pages.
+ */
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+static void xe_tt_account_iova_add(struct xe_device *xe,
+				   struct xe_ttm_tt *xe_tt, unsigned int order)
+{
+	unsigned long chunk = 1UL << order;
+
+	xe_tt->iova_dma_pages[order] += chunk;
+	atomic_long_add(chunk, &xe->mem.dma_mapped_pages[order]);
+}
+
+static void xe_tt_unaccount_iova(struct xe_device *xe,
+				 struct xe_ttm_tt *xe_tt)
+{
+	unsigned int order;
+
+	for (order = 0; order < NR_PAGE_ORDERS; order++) {
+		unsigned long pages = xe_tt->iova_dma_pages[order];
+
+		if (pages) {
+			atomic_long_sub(pages, &xe->mem.dma_mapped_pages[order]);
+			xe_tt->iova_dma_pages[order] = 0;
+		}
+	}
+}
+#else
+static void xe_tt_account_iova_add(struct xe_device *xe,
+				   struct xe_ttm_tt *xe_tt, unsigned int order)
+{
+}
Quoted 9 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_account_dma_pages(struct xe_device *xe, · line 543

Quoted 6 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
+
+static void xe_tt_unaccount_iova(struct xe_device *xe,
+				 struct xe_ttm_tt *xe_tt)
+{
+}
+#endif
Assisted-by LLM

Yes.

Quoted 3 lines

NIT..As mentioned in patch 12, I am not sure if CONFIG_DEBUG_FS is the right config for this. May be XE_DEBUG would be better?

Again I think CONFIG_DEBUG_FS is desired config. We want these stats on most production builds for debug - not just CI configs.

The only case we wouldn't want these is some crazy data center config that literally turns off all debug things. Same idea as GT stats not having its own Kconfig.

Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/xe/xe_bo.c +20 -2
+
 static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt)
 {
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
@@ -514,28 +588,129 @@ static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt)
 	}
 }
 
-struct sg_table *xe_bo_sg(struct xe_bo *bo)
+/*
+ * xe_tt_map_iova - DMA map a tt using the IOVA-based DMA API.
+ *
+ * Link all of the tt's pages into the IOVA range reserved at tt creation
+ * time, one folio at a time, and sync the IOTLB once at the end. The IOVA
+ * reservation itself is kept across map/unmap cycles and only released when
+ * the tt is destroyed.
+ */
+static int xe_tt_map_iova(struct xe_device *xe, struct ttm_tt *tt)
 {
-	struct ttm_tt *tt = bo->ttm.ttm;
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+	struct device *dev = xe->drm.dev;
+	unsigned long num_pages = tt->num_pages;
+	unsigned long i = 0;
+	size_t mapped = 0;
+	int ret;
+
+	if (xe_tt->iova_linked)
+		return 0;
+
+	while (i < num_pages) {
Quoted 9 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt) · line 614

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct page *page = tt->pages[i];
+		unsigned int order = ttm_pool_page_order_nodma(page);
+		size_t len = PAGE_SIZE << order;

NIT...as ttm_pool_page_order_nodma() is works only at chunk head (where order is stored in page->private), it should be fine. But if we ever endup here for pages[i] other than chunk head, it would become hard to debug. Is there a way we can assert for that here?

I don't see an easy to determine page is a head page or not. TTM pages are all non-compound - we do want to fix that - right now AFIAK we set p->private to the order for only head pages and zero is a valid order.

Quoted 126 lines
Quoted 126 lines
drivers/gpu/drm/xe/xe_bo.c +101 -9
+
+		ret = dma_iova_link(dev, &xe_tt->iova, page_to_phys(page),
+				    mapped, len, DMA_BIDIRECTIONAL,
+				    DMA_ATTR_SKIP_CPU_SYNC);
+		if (ret)
+			goto err_unlink;
 
-	return xe_tt->sg;
+		xe_tt_account_iova_add(xe, xe_tt, order);
+		mapped += len;
+		i += 1UL << order;
+	}
+
+	ret = dma_iova_sync(dev, &xe_tt->iova, 0, mapped);
+	if (ret)
+		goto err_unlink;
+
+	xe_tt->iova_linked = true;
+	return 0;
+
+err_unlink:
+	if (mapped) {
+		dma_iova_unlink(dev, &xe_tt->iova, 0, mapped,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+		xe_tt_unaccount_iova(xe, xe_tt);
+	}
+	return ret;
 }
 
-/**
- * xe_tt_sg() - Return the DMA scatter-gather table backing a ttm_tt.
- * @tt: The ttm_tt to query.
+/*
+ * xe_tt_unmap_iova - Unlink a tt's pages from its IOVA mapping.
 *
- * Like xe_bo_sg(), but takes the ttm_tt directly. Used by the defrag copy
- * path which needs the old (source) tt's sg table without a backing xe_bo.
+ * Unlink the pages but keep the IOVA reservation so the tt can be remapped
+ * later without re-allocating IOVA space.
+ */
+static void xe_tt_unmap_iova(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->iova_linked) {
+		/*
+		 * Unmap with attrs 0 (matching the sg unmap path) so that the
+		 * CPU copy-back sync is performed for non-coherent or bounced
+		 * (SWIOTLB) mappings, even though the link was done with
+		 * DMA_ATTR_SKIP_CPU_SYNC.
+		 */
+		dma_iova_unlink(xe->drm.dev, &xe_tt->iova, 0,
+				dma_iova_size(&xe_tt->iova),
+				DMA_BIDIRECTIONAL, 0);
+		xe_tt_unaccount_iova(xe, xe_tt);
+		xe_tt->iova_linked = false;
+	}
+}
+
+/*
+ * xe_tt_map - DMA map a tt, using IOVA when available and sg otherwise.
+ */
+static int xe_tt_map(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->use_iova)
+		return xe_tt_map_iova(xe, tt);
+
+	return xe_tt_map_sg(xe, tt);
+}
+
+/*
+ * xe_tt_unmap - Undo xe_tt_map().
+ */
+static void xe_tt_unmap(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->use_iova)
+		xe_tt_unmap_iova(xe, tt);
+	else
+		xe_tt_unmap_sg(xe, tt);
+}
+
+/**
+ * xe_res_first_tt() - Initialize a resource cursor over a ttm_tt's DMA mapping.
+ * @tt: The ttm_tt to walk.
+ * @start: Start of the range, in bytes.
+ * @size: Size of the range, in bytes.
+ * @cur: The cursor to initialize.
 *
- * Return: The sg table, or NULL if the tt is not currently mapped.
+ * Initialize @cur to walk the DMA addresses backing @tt, transparently
+ * handling both the IOVA-based mapping (single contiguous range) and the
+ * scatter-gather list fallback.
 */
-struct sg_table *xe_tt_sg(struct ttm_tt *tt)
+void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
+		     struct xe_res_cursor *cur)
 {
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
 
-	return xe_tt->sg;
+	if (xe_tt->use_iova)
+		xe_res_first_iova(&xe_tt->iova, start, size, cur);
+	else
+		xe_res_first_sg(xe_tt->sg, start, size, cur);
 }
 
 /*
@@ -651,6 +826,21 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
 		}
 	}
 
+	/*
+	 * Eligible BOs are DMA mapped using the IOVA-based DMA API, which
+	 * yields a single contiguous IOVA range instead of a scatter-gather
+	 * list. Only device BOs that are not GGTT-mapped qualify: dma-buf
+	 * imports manage their own mapping and GGTT bindings are programmed
+	 * directly from the sg list. dma_iova_try_alloc() may still fail
+	 * depending on the IOMMU mode, in which case we transparently fall
+	 * back to the sg list. The reservation lives for the tt's lifetime.
+	 */
+	if (ttm_bo->type != ttm_bo_type_sg &&
+	    !(bo->flags & (XE_BO_FLAG_GGTT | XE_BO_FLAG_GGTT_ALL)) &&
Quoted 9 lines

drivers/gpu/drm/xe/xe_bo.c static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo, · line 842

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+	    dma_iova_try_alloc(xe->drm.dev, &xe_tt->iova, 0,
+			       (size_t)tt->num_pages << PAGE_SHIFT))
+		xe_tt->use_iova = true;

Shouldn't we be allocating 2M aligned iova here by passing 2M to 'phys' instead of 0? We need both 2M aligned iova and 2M aligned physical address (beneficial order) which defragmentation expected to provide by benefecial order, right?

I thought this API worked that way and when I add iova support to GPUSVM I coded it this way, but it appears to not be needed:

git format-patch -1 9175f49330d19

^^^ This removed passing in 2M.

Matt

Quoted 188 lines

Niranjana

Quoted 186 lines
drivers/gpu/drm/xe/xe_bo.c +22 -6
+
 	return tt;
 }
 
@@ -700,7 +890,7 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 	    !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE))
 		return;
 
-	xe_tt_unmap_sg(xe, tt);
+	xe_tt_unmap(xe, tt);
 
 	ttm_pool_free(&ttm_dev->pool, tt);
 	xe_ttm_tt_account_subtract(xe, tt);
@@ -709,6 +899,19 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 
 static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 {
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	/*
+	 * Release the IOVA reservation made in xe_ttm_tt_create(). The pages
+	 * must already have been unlinked by xe_tt_unmap().
+	 */
+	if (xe_tt->use_iova) {
+		struct xe_device *xe = ttm_to_xe_device(ttm_dev);
+
+		xe_assert(xe, !xe_tt->iova_linked);
+		dma_iova_free(xe->drm.dev, &xe_tt->iova);
+	}
+
 	ttm_tt_fini(tt);
 	kfree(tt);
 }
@@ -1557,7 +1760,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	/* Bo creation path, moving to system or TT. */
 	if ((!old_mem && ttm) && !handle_system_ccs) {
 		if (new_mem->mem_type == XE_PL_TT)
-			ret = xe_tt_map_sg(xe, ttm);
+			ret = xe_tt_map(xe, ttm);
 		if (!ret)
 			ttm_bo_move_null(ttm_bo, new_mem);
 		goto out;
@@ -1587,7 +1790,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		(!ttm && ttm_bo->type == ttm_bo_type_device);
 
 	if (new_mem->mem_type == XE_PL_TT) {
-		ret = xe_tt_map_sg(xe, ttm);
+		ret = xe_tt_map(xe, ttm);
 		if (ret)
 			goto out;
 	}
@@ -1757,7 +1960,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		if (IS_VF_CCS_READY(xe))
 			xe_sriov_vf_ccs_detach_bo(bo);
 
-		xe_tt_unmap_sg(xe, ttm_bo->ttm);
+		xe_tt_unmap(xe, ttm_bo->ttm);
 	}
 
 	xe_bo_defrag_update(bo);
@@ -1785,7 +1988,7 @@ static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx,
 		if (lret)
 			return lret;
 
-		xe_tt_unmap_sg(xe, bo->ttm);
+		xe_tt_unmap(xe, bo->ttm);
 		ttm_bo_move_null(bo, new_resource);
 	}
 
@@ -2212,6 +2415,8 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
 						 DMA_BIDIRECTIONAL);
 			ttm_bo->sg = NULL;
 			xe_tt->sg = NULL;
+		} else if (xe_tt->use_iova) {
+			xe_tt_unmap_iova(ttm_to_xe_device(ttm_bo->bdev), tt);
 		} else if (xe_tt->sg) {
 			dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev,
 					  xe_tt->sg,
@@ -3779,7 +3984,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,
+		xe_res_first_tt(bo->ttm.ttm, page << PAGE_SHIFT,
 				page_size, &cur);
 		return xe_res_dma(&cur) + offset;
 	} else {
drivers/gpu/drm/xe/xe_bo.h +3 -2
index 18fcccd082b7..3e2eaefecb8f 100644
@@ -552,8 +552,9 @@ void xe_bo_dev_init(struct xe_bo_dev *bo_device);
 
 void xe_bo_dev_fini(struct xe_bo_dev *bo_device);
 
-struct sg_table *xe_bo_sg(struct xe_bo *bo);
-struct sg_table *xe_tt_sg(struct ttm_tt *tt);
+struct xe_res_cursor;
+void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
+		     struct xe_res_cursor *cur);
 
 /*
 * xe_sg_segment_size() - Provides upper limit for sg segment size.
drivers/gpu/drm/xe/xe_ggtt.c +1 -1
index 8ec23862477f..4084bc183e06 100644
@@ -699,7 +699,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_tt(bo->ttm.ttm, 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));
drivers/gpu/drm/xe/xe_migrate.c +7 -7
index 9990a7144a9f..666c715a9067 100644
@@ -1005,16 +1005,16 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		return ERR_PTR(-EINVAL);
 
 	if (!src_is_vram)
-		xe_res_first_sg(src_tt ? xe_tt_sg(src_tt) : xe_bo_sg(src_bo), 0, size, &src_it);
+		xe_res_first_tt(src_tt ? src_tt : src_bo->ttm.ttm, 0, size, &src_it);
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
 	if (copy_system_ccs)
-		xe_res_first_sg(src_tt ? xe_tt_sg(src_tt) : xe_bo_sg(src_bo),
+		xe_res_first_tt(src_tt ? src_tt : src_bo->ttm.ttm,
 				xe_bo_ccs_pages_start(src_bo),
 				PAGE_ALIGN(xe_device_ccs_bytes(xe, size)),
 				&ccs_it);
@@ -1383,9 +1383,9 @@ int xe_migrate_ccs_rw_copy(struct xe_tile *tile, struct xe_exec_queue *q,
 
 	ctx = &xe->sriov.vf.ccs.contexts[read_write];
 
-	xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
+	xe_res_first_tt(src_bo->ttm.ttm, 0, size, &src_it);
 
-	xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo),
+	xe_res_first_tt(src_bo->ttm.ttm, xe_bo_ccs_pages_start(src_bo),
 			PAGE_ALIGN(xe_device_ccs_bytes(xe, size)),
 			&ccs_it);
 
@@ -1588,7 +1588,7 @@ struct dma_fence *xe_migrate_vram_copy_chunk(struct xe_bo *vram_bo, u64 vram_off
 	xe_assert(xe, !range_overflows(sysmem_offset, size, (u64)sysmem_bo->ttm.base.size));
 
 	xe_res_first(vram, vram_offset, size, &vram_it);
-	xe_res_first_sg(xe_bo_sg(sysmem_bo), sysmem_offset, size, &sysmem_it);
+	xe_res_first_tt(sysmem_bo->ttm.ttm, sysmem_offset, size, &sysmem_it);
 
 	while (size) {
 		u32 pte_flags = PTE_UPDATE_FLAG_IS_VRAM;
@@ -1800,7 +1800,7 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 		clear_only_system_ccs = true;
 
 	if (!clear_vram)
-		xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &src_it);
+		xe_res_first_tt(bo->ttm.ttm, 0, xe_bo_size(bo), &src_it);
 	else
 		xe_res_first(src, 0, xe_bo_size(bo), &src_it);
 
drivers/gpu/drm/xe/xe_pt.c +1 -1
index 7111b5d0f6aa..185d15fb0a97 100644
@@ -797,7 +797,7 @@ 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_res_first_tt(bo->ttm.ttm, xe_vma_bo_offset(vma),
 					xe_vma_size(vma), &curs);
 	} else if (!range) {
 		curs.size = xe_vma_size(vma);
Signature · 1 line
2.34.1
#116 lore raw

Re: [PATCH v3 28/33] drm/xe: Use IOVA-based DMA mapping for eligible tt BOs

Quoted 192 lines · Matthew Brost
Quoted 175 lines · Niranjana Vishwanathapura
Quoted 174 lines · Matthew Brost

Add support for mapping a ttm_tt's pages using the IOVA-based DMA API (dma_iova_try_alloc()/link()/sync()/unlink()/free()) as an alternative to the scatter-gather list. An IOVA mapping is a single contiguous DMA range, which is cheaper to walk and program than an sg list.

Whether the IOVA API can be used depends on the IOMMU mode, so the decision is made once per tt at xe_ttm_tt_create() time and recorded in struct xe_ttm_tt::use_iova. To keep the scope limited, only device BOs that are not GGTT-mapped are eligible: dma-buf imports manage their own mapping and GGTT bindings are programmed directly from the sg list. dma_iova_try_alloc() may still fail, in which case the tt transparently falls back to the sg list, which remains the default.

The IOVA reservation is allocated at tt creation and freed at tt destroy. xe_tt_map_iova() links the tt pages (coalescing physically contiguous runs) and syncs the IOTLB; xe_tt_unmap_iova() unlinks them but keeps the reservation so the tt can be remapped without re-allocating IOVA space. xe_tt_map()/xe_tt_unmap() dispatch between the IOVA and sg backends, and xe_bo_dma_unmap_pinned() learns about the IOVA case.

Consumers that walked the sg list via xe_res_first_sg(xe_bo_sg()) now go through the new xe_res_first_tt() helper, which initializes either an IOVA cursor (xe_res_first_iova()) or an sg cursor depending on the tt's mapping mode.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

drivers/gpu/drm/xe/tests/xe_migrate.c |   6 +-
drivers/gpu/drm/xe/xe_bo.c            | 239 ++++++++++++++++++++++++--
drivers/gpu/drm/xe/xe_bo.h            |   5 +-
drivers/gpu/drm/xe/xe_ggtt.c          |   2 +-
drivers/gpu/drm/xe/xe_migrate.c       |  14 +-
drivers/gpu/drm/xe/xe_pt.c            |   2 +-
6 files changed, 237 insertions(+), 31 deletions(-)
drivers/gpu/drm/xe/tests/xe_migrate.c +3 -3
index 41de648b2c86..119134e67c89 100644
@@ -253,7 +253,7 @@ static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test,
 	if (xe_bo_is_vram(pt))
 		xe_res_first(pt->ttm.resource, 0, xe_bo_size(pt), &src_it);
 	else
-		xe_res_first_sg(xe_bo_sg(pt), 0, xe_bo_size(pt), &src_it);
+		xe_res_first_tt(pt->ttm.ttm, 0, xe_bo_size(pt), &src_it);
 
 	emit_pte(m, bb, NUM_KERNEL_PDE - 1, xe_bo_is_vram(pt), false,
 		 &src_it, XE_PAGE_SIZE, pt->ttm.resource);
@@ -385,12 +385,12 @@ static struct dma_fence *blt_copy(struct xe_tile *tile,
 	bool dst_is_vram = mem_type_is_vram(dst->mem_type);
 
 	if (!src_is_vram)
-		xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
+		xe_res_first_tt(src_bo->ttm.ttm, 0, size, &src_it);
 	else
 		xe_res_first(src, 0, size, &src_it);
 
 	if (!dst_is_vram)
-		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
drivers/gpu/drm/xe/xe_bo.c +67 -0
index caf5aa0bab53..d54e69aefeba 100644
@@ -6,6 +6,7 @@
 #include "xe_bo.h"
 
 #include <linux/dma-buf.h>
+#include <linux/dma-mapping.h>
 #include <linux/nospec.h>
 
 #include <drm/drm_drv.h>
@@ -15,6 +16,7 @@
 #include <drm/ttm/ttm_backup.h>
 #include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_pool.h>
 #include <drm/ttm/ttm_tt.h>
 #include <uapi/drm/xe_drm.h>
 
@@ -420,6 +422,32 @@ struct xe_ttm_tt {
 	struct ttm_tt ttm;
 	struct sg_table sgt;
 	struct sg_table *sg;
+	/**
+	 * @iova: IOVA state describing the single contiguous IOVA mapping of
+	 * the tt pages. Only valid when @use_iova is true.
+	 */
+	struct dma_iova_state iova;
+	/**
+	 * @use_iova: Whether this tt is DMA mapped using the IOVA-based DMA
+	 * API instead of a scatter-gather list. Decided once at tt creation
+	 * time and stable for the lifetime of the tt.
+	 */
+	bool use_iova;
+	/**
+	 * @iova_linked: Whether the tt pages are currently linked into the
+	 * IOVA mapping. Only meaningful when @use_iova is true.
+	 */
+	bool iova_linked;
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+	/**
+	 * @iova_dma_pages: Per-order count of DMA-mapped pages this tt
+	 * accounted into xe->mem.dma_mapped_pages[] for its IOVA mapping,
+	 * recorded at link time so the unlink can apply an exactly matching
+	 * decrement without re-walking tt->pages (which TTM may mutate, e.g.
+	 * during defrag). Counter ownership stays isolated in xe_bo.c.
+	 */
+	unsigned long iova_dma_pages[NR_PAGE_ORDERS];
+#endif
 	/** @purgeable: Whether the content of the pages of @ttm is purgeable. */
 	bool purgeable;
 };
@@ -468,6 +496,52 @@ static void xe_tt_account_dma_pages(struct xe_device *xe,
 }
 #endif
 
+/*
+ * xe_tt_account_iova_add - account one freshly linked IOVA folio
+ * @xe: the xe device
+ * @xe_tt: the xe_ttm_tt being mapped
+ * @order: page order of the linked folio
+ *
+ * Add the folio's 2^order pages to xe->mem.dma_mapped_pages[order] and record
+ * the same amount in @xe_tt so xe_tt_unaccount_iova() can later subtract an
+ * exactly matching count without re-walking tt->pages.
+ */
+#if IS_ENABLED(CONFIG_DEBUG_FS)
+static void xe_tt_account_iova_add(struct xe_device *xe,
+				   struct xe_ttm_tt *xe_tt, unsigned int order)
+{
+	unsigned long chunk = 1UL << order;
+
+	xe_tt->iova_dma_pages[order] += chunk;
+	atomic_long_add(chunk, &xe->mem.dma_mapped_pages[order]);
+}
+
+static void xe_tt_unaccount_iova(struct xe_device *xe,
+				 struct xe_ttm_tt *xe_tt)
+{
+	unsigned int order;
+
+	for (order = 0; order < NR_PAGE_ORDERS; order++) {
+		unsigned long pages = xe_tt->iova_dma_pages[order];
+
+		if (pages) {
+			atomic_long_sub(pages, &xe->mem.dma_mapped_pages[order]);
+			xe_tt->iova_dma_pages[order] = 0;
+		}
+	}
+}
+#else
+static void xe_tt_account_iova_add(struct xe_device *xe,
+				   struct xe_ttm_tt *xe_tt, unsigned int order)
+{
+}
Quoted 9 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_account_dma_pages(struct xe_device *xe, · line 543

Quoted 6 lines
drivers/gpu/drm/xe/xe_bo.c +6 -0
+
+static void xe_tt_unaccount_iova(struct xe_device *xe,
+				 struct xe_ttm_tt *xe_tt)
+{
+}
+#endif
Assisted-by LLM

Yes.

Quoted 3 lines

NIT..As mentioned in patch 12, I am not sure if CONFIG_DEBUG_FS is the right config for this. May be XE_DEBUG would be better?

Quoted 7 lines

Again I think CONFIG_DEBUG_FS is desired config. We want these stats on most production builds for debug - not just CI configs.

The only case we wouldn't want these is some crazy data center config that literally turns off all debug things. Same idea as GT stats not having its own Kconfig.

Sure.

Quoted 41 lines
Quoted 31 lines
Quoted 31 lines
drivers/gpu/drm/xe/xe_bo.c +20 -2
+
 static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt)
 {
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
@@ -514,28 +588,129 @@ static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt)
 	}
 }
 
-struct sg_table *xe_bo_sg(struct xe_bo *bo)
+/*
+ * xe_tt_map_iova - DMA map a tt using the IOVA-based DMA API.
+ *
+ * Link all of the tt's pages into the IOVA range reserved at tt creation
+ * time, one folio at a time, and sync the IOTLB once at the end. The IOVA
+ * reservation itself is kept across map/unmap cycles and only released when
+ * the tt is destroyed.
+ */
+static int xe_tt_map_iova(struct xe_device *xe, struct ttm_tt *tt)
 {
-	struct ttm_tt *tt = bo->ttm.ttm;
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+	struct device *dev = xe->drm.dev;
+	unsigned long num_pages = tt->num_pages;
+	unsigned long i = 0;
+	size_t mapped = 0;
+	int ret;
+
+	if (xe_tt->iova_linked)
+		return 0;
+
+	while (i < num_pages) {
Quoted 9 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_unmap_sg(struct xe_device *xe, struct ttm_tt *tt) · line 614

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+		struct page *page = tt->pages[i];
+		unsigned int order = ttm_pool_page_order_nodma(page);
+		size_t len = PAGE_SIZE << order;

NIT...as ttm_pool_page_order_nodma() is works only at chunk head (where order is stored in page->private), it should be fine. But if we ever endup here for pages[i] other than chunk head, it would become hard to debug. Is there a way we can assert for that here?

Quoted 4 lines

I don't see an easy to determine page is a head page or not. TTM pages are all non-compound - we do want to fix that - right now AFIAK we set p->private to the order for only head pages and zero is a valid order.

ok.

Quoted 136 lines
Quoted 126 lines
Quoted 126 lines
drivers/gpu/drm/xe/xe_bo.c +101 -9
+
+		ret = dma_iova_link(dev, &xe_tt->iova, page_to_phys(page),
+				    mapped, len, DMA_BIDIRECTIONAL,
+				    DMA_ATTR_SKIP_CPU_SYNC);
+		if (ret)
+			goto err_unlink;
 
-	return xe_tt->sg;
+		xe_tt_account_iova_add(xe, xe_tt, order);
+		mapped += len;
+		i += 1UL << order;
+	}
+
+	ret = dma_iova_sync(dev, &xe_tt->iova, 0, mapped);
+	if (ret)
+		goto err_unlink;
+
+	xe_tt->iova_linked = true;
+	return 0;
+
+err_unlink:
+	if (mapped) {
+		dma_iova_unlink(dev, &xe_tt->iova, 0, mapped,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+		xe_tt_unaccount_iova(xe, xe_tt);
+	}
+	return ret;
 }
 
-/**
- * xe_tt_sg() - Return the DMA scatter-gather table backing a ttm_tt.
- * @tt: The ttm_tt to query.
+/*
+ * xe_tt_unmap_iova - Unlink a tt's pages from its IOVA mapping.
 *
- * Like xe_bo_sg(), but takes the ttm_tt directly. Used by the defrag copy
- * path which needs the old (source) tt's sg table without a backing xe_bo.
+ * Unlink the pages but keep the IOVA reservation so the tt can be remapped
+ * later without re-allocating IOVA space.
+ */
+static void xe_tt_unmap_iova(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->iova_linked) {
+		/*
+		 * Unmap with attrs 0 (matching the sg unmap path) so that the
+		 * CPU copy-back sync is performed for non-coherent or bounced
+		 * (SWIOTLB) mappings, even though the link was done with
+		 * DMA_ATTR_SKIP_CPU_SYNC.
+		 */
+		dma_iova_unlink(xe->drm.dev, &xe_tt->iova, 0,
+				dma_iova_size(&xe_tt->iova),
+				DMA_BIDIRECTIONAL, 0);
+		xe_tt_unaccount_iova(xe, xe_tt);
+		xe_tt->iova_linked = false;
+	}
+}
+
+/*
+ * xe_tt_map - DMA map a tt, using IOVA when available and sg otherwise.
+ */
+static int xe_tt_map(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->use_iova)
+		return xe_tt_map_iova(xe, tt);
+
+	return xe_tt_map_sg(xe, tt);
+}
+
+/*
+ * xe_tt_unmap - Undo xe_tt_map().
+ */
+static void xe_tt_unmap(struct xe_device *xe, struct ttm_tt *tt)
+{
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	if (xe_tt->use_iova)
+		xe_tt_unmap_iova(xe, tt);
+	else
+		xe_tt_unmap_sg(xe, tt);
+}
+
+/**
+ * xe_res_first_tt() - Initialize a resource cursor over a ttm_tt's DMA mapping.
+ * @tt: The ttm_tt to walk.
+ * @start: Start of the range, in bytes.
+ * @size: Size of the range, in bytes.
+ * @cur: The cursor to initialize.
 *
- * Return: The sg table, or NULL if the tt is not currently mapped.
+ * Initialize @cur to walk the DMA addresses backing @tt, transparently
+ * handling both the IOVA-based mapping (single contiguous range) and the
+ * scatter-gather list fallback.
 */
-struct sg_table *xe_tt_sg(struct ttm_tt *tt)
+void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
+		     struct xe_res_cursor *cur)
 {
 	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
 
-	return xe_tt->sg;
+	if (xe_tt->use_iova)
+		xe_res_first_iova(&xe_tt->iova, start, size, cur);
+	else
+		xe_res_first_sg(xe_tt->sg, start, size, cur);
 }
 
 /*
@@ -651,6 +826,21 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
 		}
 	}
 
+	/*
+	 * Eligible BOs are DMA mapped using the IOVA-based DMA API, which
+	 * yields a single contiguous IOVA range instead of a scatter-gather
+	 * list. Only device BOs that are not GGTT-mapped qualify: dma-buf
+	 * imports manage their own mapping and GGTT bindings are programmed
+	 * directly from the sg list. dma_iova_try_alloc() may still fail
+	 * depending on the IOMMU mode, in which case we transparently fall
+	 * back to the sg list. The reservation lives for the tt's lifetime.
+	 */
+	if (ttm_bo->type != ttm_bo_type_sg &&
+	    !(bo->flags & (XE_BO_FLAG_GGTT | XE_BO_FLAG_GGTT_ALL)) &&
Quoted 9 lines

drivers/gpu/drm/xe/xe_bo.c static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo, · line 842

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+	    dma_iova_try_alloc(xe->drm.dev, &xe_tt->iova, 0,
+			       (size_t)tt->num_pages << PAGE_SHIFT))
+		xe_tt->use_iova = true;

Shouldn't we be allocating 2M aligned iova here by passing 2M to 'phys' instead of 0? We need both 2M aligned iova and 2M aligned physical address (beneficial order) which defragmentation expected to provide by benefecial order, right?

Quoted 7 lines

I thought this API worked that way and when I add iova support to GPUSVM I coded it this way, but it appears to not be needed:

git format-patch -1 9175f49330d19

^^^ This removed passing in 2M.

Ok, yah, it looks like api automatically aligns iova based on the size.

Niranjana

Quoted 190 lines

Matt

Quoted 188 lines

Niranjana

Quoted 186 lines
drivers/gpu/drm/xe/xe_bo.c +22 -6
+
 	return tt;
 }
 
@@ -700,7 +890,7 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 	    !(tt->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE))
 		return;
 
-	xe_tt_unmap_sg(xe, tt);
+	xe_tt_unmap(xe, tt);
 
 	ttm_pool_free(&ttm_dev->pool, tt);
 	xe_ttm_tt_account_subtract(xe, tt);
@@ -709,6 +899,19 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 
 static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
 {
+	struct xe_ttm_tt *xe_tt = container_of(tt, struct xe_ttm_tt, ttm);
+
+	/*
+	 * Release the IOVA reservation made in xe_ttm_tt_create(). The pages
+	 * must already have been unlinked by xe_tt_unmap().
+	 */
+	if (xe_tt->use_iova) {
+		struct xe_device *xe = ttm_to_xe_device(ttm_dev);
+
+		xe_assert(xe, !xe_tt->iova_linked);
+		dma_iova_free(xe->drm.dev, &xe_tt->iova);
+	}
+
 	ttm_tt_fini(tt);
 	kfree(tt);
 }
@@ -1557,7 +1760,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	/* Bo creation path, moving to system or TT. */
 	if ((!old_mem && ttm) && !handle_system_ccs) {
 		if (new_mem->mem_type == XE_PL_TT)
-			ret = xe_tt_map_sg(xe, ttm);
+			ret = xe_tt_map(xe, ttm);
 		if (!ret)
 			ttm_bo_move_null(ttm_bo, new_mem);
 		goto out;
@@ -1587,7 +1790,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		(!ttm && ttm_bo->type == ttm_bo_type_device);
 
 	if (new_mem->mem_type == XE_PL_TT) {
-		ret = xe_tt_map_sg(xe, ttm);
+		ret = xe_tt_map(xe, ttm);
 		if (ret)
 			goto out;
 	}
@@ -1757,7 +1960,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		if (IS_VF_CCS_READY(xe))
 			xe_sriov_vf_ccs_detach_bo(bo);
 
-		xe_tt_unmap_sg(xe, ttm_bo->ttm);
+		xe_tt_unmap(xe, ttm_bo->ttm);
 	}
 
 	xe_bo_defrag_update(bo);
@@ -1785,7 +1988,7 @@ static long xe_bo_shrink_purge(struct ttm_operation_ctx *ctx,
 		if (lret)
 			return lret;
 
-		xe_tt_unmap_sg(xe, bo->ttm);
+		xe_tt_unmap(xe, bo->ttm);
 		ttm_bo_move_null(bo, new_resource);
 	}
 
@@ -2212,6 +2415,8 @@ int xe_bo_dma_unmap_pinned(struct xe_bo *bo)
 						 DMA_BIDIRECTIONAL);
 			ttm_bo->sg = NULL;
 			xe_tt->sg = NULL;
+		} else if (xe_tt->use_iova) {
+			xe_tt_unmap_iova(ttm_to_xe_device(ttm_bo->bdev), tt);
 		} else if (xe_tt->sg) {
 			dma_unmap_sgtable(ttm_to_xe_device(ttm_bo->bdev)->drm.dev,
 					  xe_tt->sg,
@@ -3779,7 +3984,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,
+		xe_res_first_tt(bo->ttm.ttm, page << PAGE_SHIFT,
 				page_size, &cur);
 		return xe_res_dma(&cur) + offset;
 	} else {
drivers/gpu/drm/xe/xe_bo.h +3 -2
index 18fcccd082b7..3e2eaefecb8f 100644
@@ -552,8 +552,9 @@ void xe_bo_dev_init(struct xe_bo_dev *bo_device);
 
 void xe_bo_dev_fini(struct xe_bo_dev *bo_device);
 
-struct sg_table *xe_bo_sg(struct xe_bo *bo);
-struct sg_table *xe_tt_sg(struct ttm_tt *tt);
+struct xe_res_cursor;
+void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
+		     struct xe_res_cursor *cur);
 
 /*
 * xe_sg_segment_size() - Provides upper limit for sg segment size.
drivers/gpu/drm/xe/xe_ggtt.c +1 -1
index 8ec23862477f..4084bc183e06 100644
@@ -699,7 +699,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_tt(bo->ttm.ttm, 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));
drivers/gpu/drm/xe/xe_migrate.c +7 -7
index 9990a7144a9f..666c715a9067 100644
@@ -1005,16 +1005,16 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		return ERR_PTR(-EINVAL);
 
 	if (!src_is_vram)
-		xe_res_first_sg(src_tt ? xe_tt_sg(src_tt) : xe_bo_sg(src_bo), 0, size, &src_it);
+		xe_res_first_tt(src_tt ? src_tt : src_bo->ttm.ttm, 0, size, &src_it);
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
 	if (copy_system_ccs)
-		xe_res_first_sg(src_tt ? xe_tt_sg(src_tt) : xe_bo_sg(src_bo),
+		xe_res_first_tt(src_tt ? src_tt : src_bo->ttm.ttm,
 				xe_bo_ccs_pages_start(src_bo),
 				PAGE_ALIGN(xe_device_ccs_bytes(xe, size)),
 				&ccs_it);
@@ -1383,9 +1383,9 @@ int xe_migrate_ccs_rw_copy(struct xe_tile *tile, struct xe_exec_queue *q,
 
 	ctx = &xe->sriov.vf.ccs.contexts[read_write];
 
-	xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
+	xe_res_first_tt(src_bo->ttm.ttm, 0, size, &src_it);
 
-	xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo),
+	xe_res_first_tt(src_bo->ttm.ttm, xe_bo_ccs_pages_start(src_bo),
 			PAGE_ALIGN(xe_device_ccs_bytes(xe, size)),
 			&ccs_it);
 
@@ -1588,7 +1588,7 @@ struct dma_fence *xe_migrate_vram_copy_chunk(struct xe_bo *vram_bo, u64 vram_off
 	xe_assert(xe, !range_overflows(sysmem_offset, size, (u64)sysmem_bo->ttm.base.size));
 
 	xe_res_first(vram, vram_offset, size, &vram_it);
-	xe_res_first_sg(xe_bo_sg(sysmem_bo), sysmem_offset, size, &sysmem_it);
+	xe_res_first_tt(sysmem_bo->ttm.ttm, sysmem_offset, size, &sysmem_it);
 
 	while (size) {
 		u32 pte_flags = PTE_UPDATE_FLAG_IS_VRAM;
@@ -1800,7 +1800,7 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 		clear_only_system_ccs = true;
 
 	if (!clear_vram)
-		xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &src_it);
+		xe_res_first_tt(bo->ttm.ttm, 0, xe_bo_size(bo), &src_it);
 	else
 		xe_res_first(src, 0, xe_bo_size(bo), &src_it);
 
drivers/gpu/drm/xe/xe_pt.c +1 -2
index 7111b5d0f6aa..185d15fb0a97 100644
@@ -797,7 +797,7 @@ 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_res_first_tt(bo->ttm.ttm, xe_vma_bo_offset(vma),
 					xe_vma_size(vma), &curs);
 	} else if (!range) {
 		curs.size = xe_vma_size(vma);
--

2.34.1

author maintainer
#117 lore raw

PATCH v3 29/33 drm/xe: Add per-device dependency scheduler for IOVA defrag finalize

In reply to Matthew Brost

A future patch performs the destination DMA mapping of a defrag move in two stages when the BO is IOVA mapped: a temporary contiguous mapping of just tht changed pages used for the GPU copy, followed by a post-copy finalize stet that tears that mapping down and links the new backing into the (transferred) IOVA reservation. The finalize step must run only after the GPU copy it depends on has completed, so it is dispatched as an xe_dep_job,

Add a per-device xe_dep_scheduler dedicated to these finalize jobs, created in xe_bo_defrag_init() (after the migrate contexts are up) and torn down in xe_bo_defrag_fini(). No users yet; the job itself is added in a later patch.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8
---
 drivers/gpu/drm/xe/xe_bo.c           | 15 +++++++++++++++
 drivers/gpu/drm/xe/xe_device_types.h | 10 ++++++++++
 2 files changed, 25 insertions(+)
drivers/gpu/drm/xe/xe_bo.c +15 -0
index d54e69aefeba..c5022c16411f 100644
@@ -25,6 +25,7 @@
 #include <trace/events/gpu_mem.h>
 
 #include "xe_device.h"
+#include "xe_dep_scheduler.h"
 #include "xe_dma_buf.h"
 #include "xe_drm_client.h"
 #include "xe_ggtt.h"
@@ -1306,6 +1307,10 @@ static void xe_bo_defrag_fini(void *arg)
 	struct xe_device *xe = arg;
 
 	disable_delayed_work_sync(&xe->mem.defrag.worker);
+	if (xe->mem.defrag.iova_sched) {
+		xe_dep_scheduler_fini(xe->mem.defrag.iova_sched);
+		xe->mem.defrag.iova_sched = NULL;
+	}
 }
 
 /**
@@ -1342,6 +1347,16 @@ void xe_bo_defrag_init_early(struct xe_device *xe)
  */
 int xe_bo_defrag_init(struct xe_device *xe)
 {
+#define XE_BO_MAX_IOVA_DEFRAG_JOBS	16	/* Picking a reasonable value */
+	struct xe_dep_scheduler *iova_sched;
+
+	iova_sched = xe_dep_scheduler_create(xe, NULL, "xe_iova_defrag",
+					     XE_BO_MAX_IOVA_DEFRAG_JOBS);
+	if (IS_ERR(iova_sched))
+		return PTR_ERR(iova_sched);
+	xe->mem.defrag.iova_sched = iova_sched;
+#undef XE_BO_MAX_IOVA_DEFRAG_JOBS
+
 	return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini, xe);
 }
 
drivers/gpu/drm/xe/xe_device_types.h +10 -0
index 6e68add4d652..b55d549d5e30 100644
@@ -38,6 +38,7 @@
 
 struct drm_pagemap_shrinker;
 struct intel_display;
+struct xe_dep_scheduler;
 struct intel_dg_nvm_dev;
 struct xe_ggtt;
 struct xe_i2c;
@@ -325,6 +326,15 @@ struct xe_device {
 			 * @mem.defrag.worker, in milliseconds.
 			 */
 			unsigned int interval_ms;
+			/**
+			 * @mem.defrag.iova_sched: Dependency scheduler used to
+			 * run the post-copy IOVA finalize job for defrag moves
+			 * of IOVA-mapped BOs. The job tears down the temporary
+			 * copy-step IOVA mapping and links the new backing into
+			 * the (transferred) reservation once the GPU copy that
+			 * its dependencies gate has completed.
+			 */
+			struct xe_dep_scheduler *iova_sched;
 		} defrag;
 	} mem;
 
Signature · 1 line
2.34.1
#118 lore raw

Re: [PATCH v3 29/33] drm/xe: Add per-device dependency scheduler for IOVA defrag finalize

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Early return on xe_dep_scheduler_create() failure skips registering the devm teardown action for…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Early return on xe_dep_scheduler_create() failure skips registering the devm teardown action for the defrag worker, leading to a use-after-free. --

--- Patch [29]: [PATCH v3 29/33] drm/xe: Add per-device dependency scheduler for IOVA defrag finalize --- commit 4091ca99808dbd61930846733ec6c77fcca46fe9
Author: Matthew Brost <[email protected]>

drm/xe: Add per-device dependency scheduler for IOVA defrag finalize

This commit adds a per-device dependency scheduler for IOVA defrag finalize jobs, which tears down temporary copy mappings and links new backing memory. The scheduler is created in xe_bo_defrag_init() and destroyed in xe_bo_defrag_fini().

Quoted 4 lines
drivers/gpu/drm/xe/xe_bo.c +0 -0
index d54e69aefeba5..c5022c16411f6 100644

[ ... ]

drivers/gpu/drm/xe/xe_bo.c void xe_bo_defrag_init_early(struct xe_device *xe) · line 1356

Quoted 11 lines
drivers/gpu/drm/xe/xe_bo.c +7 -0
@@ -1342,6 +1347,16 @@ void xe_bo_defrag_init_early(struct xe_device *xe)
  */
 int xe_bo_defrag_init(struct xe_device *xe)
 {
+#define XE_BO_MAX_IOVA_DEFRAG_JOBS	16	/* Picking a reasonable value */
+	struct xe_dep_scheduler *iova_sched;
+
+	iova_sched = xe_dep_scheduler_create(xe, NULL, "xe_iova_defrag",
+					     XE_BO_MAX_IOVA_DEFRAG_JOBS);
+	if (IS_ERR(iova_sched))
+		return PTR_ERR(iova_sched);

[Severity: High]
If xe_dep_scheduler_create() fails and we return early, does this skip registering the teardown action xe_bo_defrag_fini?

Since xe_bo_defrag_init_early() initializes the defrag delayed worker earlier in device creation, could the worker already be armed via schedule_delayed_work() if BOs hit sub-optimal page allocations during other initializations?

If probe fails and devres unwinds, the xe_device struct is freed. Since the early return bypasses devm_add_action_or_reset(), the worker is never canceled. Could this lead to a use-after-free when the armed worker eventually fires and dereferences the freed xe_device?

Quoted 5 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+	xe->mem.defrag.iova_sched = iova_sched;
+#undef XE_BO_MAX_IOVA_DEFRAG_JOBS
+
 	return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini, xe);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=29
#119 lore raw

Re: [PATCH v3 29/33] drm/xe: Add per-device dependency scheduler for IOVA defrag finalize

In reply to Matthew Brost
Quoted 7 lines · Matthew Brost

A future patch performs the destination DMA mapping of a defrag move in two stages when the BO is IOVA mapped: a temporary contiguous mapping of just tht changed pages used for the GPU copy, followed by a post-copy finalize stet that tears that mapping down and links the new backing into the (transferred) IOVA reservation. The finalize step must run only after the GPU copy it depends on has completed, so it is dispatched as an xe_dep_job,

Assisted-by LLM

NIT...minor types 'tht', 'stet' and trailing comma.

Quoted 39 lines

Add a per-device xe_dep_scheduler dedicated to these finalize jobs, created in xe_bo_defrag_init() (after the migrate contexts are up) and torn down in xe_bo_defrag_fini(). No users yet; the job itself is added in a later patch.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---
drivers/gpu/drm/xe/xe_bo.c | 15 +++++++++++++++
drivers/gpu/drm/xe/xe_device_types.h | 10 ++++++++++

2 files changed, 25 insertions(+)
drivers/gpu/drm/xe/xe_bo.c +1 -0
index d54e69aefeba..c5022c16411f 100644
@@ -25,6 +25,7 @@
 #include <trace/events/gpu_mem.h>
 
 #include "xe_device.h"
+#include "xe_dep_scheduler.h"
 #include "xe_dma_buf.h"
 #include "xe_drm_client.h"
 #include "xe_ggtt.h"

drivers/gpu/drm/xe/xe_bo.c static void xe_bo_defrag_fini(void *arg) · line 1311

Quoted 5 lines
drivers/gpu/drm/xe/xe_bo.c +2 -0
@@ -1306,6 +1307,10 @@ static void xe_bo_defrag_fini(void *arg)
 	struct xe_device *xe = arg;
 
 	disable_delayed_work_sync(&xe->mem.defrag.worker);
+	if (xe->mem.defrag.iova_sched) {
+		xe_dep_scheduler_fini(xe->mem.defrag.iova_sched);

LLM suggests adding drm_sched_entity_flush() after calling disable_delayed_work_sync() but before calling xe_dep_scheduler_fini().

Quoted 6 lines
drivers/gpu/drm/xe/xe_bo.c +2 -0
+		xe->mem.defrag.iova_sched = NULL;
+	}
 }
 
 /**

drivers/gpu/drm/xe/xe_bo.c void xe_bo_defrag_init_early(struct xe_device *xe) · line 1354

Quoted 8 lines
drivers/gpu/drm/xe/xe_bo.c +5 -0
@@ -1342,6 +1347,16 @@ void xe_bo_defrag_init_early(struct xe_device *xe)
 */
 int xe_bo_defrag_init(struct xe_device *xe)
 {
+#define XE_BO_MAX_IOVA_DEFRAG_JOBS	16	/* Picking a reasonable value */
+	struct xe_dep_scheduler *iova_sched;
+
+	iova_sched = xe_dep_scheduler_create(xe, NULL, "xe_iova_defrag",
+					     XE_BO_MAX_IOVA_DEFRAG_JOBS);

Should we be creating this only if IOMMU is enabled (IOVA supported) and defragmentation is enabled?

drivers/gpu/drm/xe/xe_bo.c void xe_bo_defrag_init_early(struct xe_device *xe) · line 1356

Quoted 2 lines
drivers/gpu/drm/xe/xe_bo.c +2 -0
+	if (IS_ERR(iova_sched))
+		return PTR_ERR(iova_sched);

returning early here skips registering xe_bo_defrag_fini. Though it is unlikely that defrag worker getting scheduled by this time, it is probably still good to register xe_bo_defrag_fini to ensure worker is stopped.

Niranjana

Quoted 37 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+	xe->mem.defrag.iova_sched = iova_sched;
+#undef XE_BO_MAX_IOVA_DEFRAG_JOBS
+
 	return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini, xe);
 }
 
drivers/gpu/drm/xe/xe_device_types.h +10 -0
index 6e68add4d652..b55d549d5e30 100644
@@ -38,6 +38,7 @@
 
 struct drm_pagemap_shrinker;
 struct intel_display;
+struct xe_dep_scheduler;
 struct intel_dg_nvm_dev;
 struct xe_ggtt;
 struct xe_i2c;
@@ -325,6 +326,15 @@ struct xe_device {
 			 * @mem.defrag.worker, in milliseconds.
 			 */
 			unsigned int interval_ms;
+			/**
+			 * @mem.defrag.iova_sched: Dependency scheduler used to
+			 * run the post-copy IOVA finalize job for defrag moves
+			 * of IOVA-mapped BOs. The job tears down the temporary
+			 * copy-step IOVA mapping and links the new backing into
+			 * the (transferred) reservation once the GPU copy that
+			 * its dependencies gate has completed.
+			 */
+			struct xe_dep_scheduler *iova_sched;
 		} defrag;
 	} mem;
 
Signature · 1 line
2.34.1
author maintainer
#120 lore raw

Re: [PATCH v3 29/33] drm/xe: Add per-device dependency scheduler for IOVA defrag finalize

Quoted 54 lines · Niranjana Vishwanathapura
Quoted 7 lines · Matthew Brost

A future patch performs the destination DMA mapping of a defrag move in two stages when the BO is IOVA mapped: a temporary contiguous mapping of just tht changed pages used for the GPU copy, followed by a post-copy finalize stet that tears that mapping down and links the new backing into the (transferred) IOVA reservation. The finalize step must run only after the GPU copy it depends on has completed, so it is dispatched as an xe_dep_job,

Assisted-by LLM

NIT...minor types 'tht', 'stet' and trailing comma.

Quoted 41 lines

Add a per-device xe_dep_scheduler dedicated to these finalize jobs, created in xe_bo_defrag_init() (after the migrate contexts are up) and torn down in xe_bo_defrag_fini(). No users yet; the job itself is added in a later patch.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---
drivers/gpu/drm/xe/xe_bo.c | 15 +++++++++++++++
drivers/gpu/drm/xe/xe_device_types.h | 10 ++++++++++

2 files changed, 25 insertions(+)
drivers/gpu/drm/xe/xe_bo.c +1 -0
index d54e69aefeba..c5022c16411f 100644
@@ -25,6 +25,7 @@
 #include <trace/events/gpu_mem.h>
 
 #include "xe_device.h"
+#include "xe_dep_scheduler.h"
 #include "xe_dma_buf.h"
 #include "xe_drm_client.h"
 #include "xe_ggtt.h"
@@ -1306,6 +1307,10 @@ static void xe_bo_defrag_fini(void *arg)
 	struct xe_device *xe = arg;
 
Quoted 5 lines

drivers/gpu/drm/xe/xe_bo.c static void xe_bo_defrag_fini(void *arg) · line 1311

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +2 -0
 	disable_delayed_work_sync(&xe->mem.defrag.worker);
+	if (xe->mem.defrag.iova_sched) {
+		xe_dep_scheduler_fini(xe->mem.defrag.iova_sched);

LLM suggests adding drm_sched_entity_flush() after calling

Honestly I'd rather than not call drm_sched_entity_flush as that a pretty scary function.

We might be able to explode here if iova jobs are in flight but I'd really have to think one through. Even if were possible it really just an accidemic issue and no one in practice hotplugs a driver - if we find accidemic issues we can fixup later. My fix probably would be use ref counting from jobs -> xe->mem.defrag.iova_sched to make everything safe (i.e., do what we do for exec queues).

Quoted 6 lines

disable_delayed_work_sync() but before calling xe_dep_scheduler_fini().

Quoted 4 lines
drivers/gpu/drm/xe/xe_bo.c +2 -0
+		xe->mem.defrag.iova_sched = NULL;
+	}
 }
 
Quoted 13 lines

drivers/gpu/drm/xe/xe_bo.c void xe_bo_defrag_init_early(struct xe_device *xe) · line 1354

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +5 -0
 /**
@@ -1342,6 +1347,16 @@ void xe_bo_defrag_init_early(struct xe_device *xe)
 */
 int xe_bo_defrag_init(struct xe_device *xe)
 {
+#define XE_BO_MAX_IOVA_DEFRAG_JOBS	16	/* Picking a reasonable value */
+	struct xe_dep_scheduler *iova_sched;
+
+	iova_sched = xe_dep_scheduler_create(xe, NULL, "xe_iova_defrag",
+					     XE_BO_MAX_IOVA_DEFRAG_JOBS);

Should we be creating this only if IOMMU is enabled (IOVA supported) and defragmentation is enabled?

This very small piece of memory and I don't think it worth do any special casing here.

Quoted 8 lines

drivers/gpu/drm/xe/xe_bo.c void xe_bo_defrag_init_early(struct xe_device *xe) · line 1356

Quoted 3 lines
drivers/gpu/drm/xe/xe_bo.c +2 -0
+	if (IS_ERR(iova_sched))
+		return PTR_ERR(iova_sched);

returning early here skips registering xe_bo_defrag_fini. Though it is unlikely that defrag worker getting scheduled by this time, it is probably still good to register xe_bo_defrag_fini to ensure worker is stopped.

So call:
disable_delayed_work_sync(&xe->mem.defrag.worker);

Probably a good idea.

Matt

Quoted 40 lines

Niranjana

Quoted 37 lines
drivers/gpu/drm/xe/xe_bo.c +3 -0
+	xe->mem.defrag.iova_sched = iova_sched;
+#undef XE_BO_MAX_IOVA_DEFRAG_JOBS
+
 	return devm_add_action_or_reset(xe->drm.dev, xe_bo_defrag_fini, xe);
 }
 
drivers/gpu/drm/xe/xe_device_types.h +10 -0
index 6e68add4d652..b55d549d5e30 100644
@@ -38,6 +38,7 @@
 
 struct drm_pagemap_shrinker;
 struct intel_display;
+struct xe_dep_scheduler;
 struct intel_dg_nvm_dev;
 struct xe_ggtt;
 struct xe_i2c;
@@ -325,6 +326,15 @@ struct xe_device {
 			 * @mem.defrag.worker, in milliseconds.
 			 */
 			unsigned int interval_ms;
+			/**
+			 * @mem.defrag.iova_sched: Dependency scheduler used to
+			 * run the post-copy IOVA finalize job for defrag moves
+			 * of IOVA-mapped BOs. The job tears down the temporary
+			 * copy-step IOVA mapping and links the new backing into
+			 * the (transferred) reservation once the GPU copy that
+			 * its dependencies gate has completed.
+			 */
+			struct xe_dep_scheduler *iova_sched;
 		} defrag;
 	} mem;
 
Signature · 1 line
2.34.1
author maintainer
#121 lore raw

PATCH v3 30/33 drm/xe: Add packed copy-step IOVA mapping for defrag

In reply to Matthew Brost

Add machinery to map only changed pages during defrag when the destination TT is IOVA-mapped. Only the non-borrowed (freshly allocated) pages need copying; borrowed pages are aliases from the old TT.

Add xe_tt_iova_copy_init() to diff new vs old TT folios and record changed-page runs. Add xe_tt_map_iova_copy() to link those changed pages packed contiguously at the front of the new TT's IOVA reservation, synced and ready for GPU blits.

No users yet; wired up in later patches.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8
---
 drivers/gpu/drm/xe/xe_bo.c | 364 +++++++++++++++++++++++++++++++++++++
 1 file changed, 364 insertions(+)
drivers/gpu/drm/xe/xe_bo.c +364 -0
index c5022c16411f..c087f17ad6db 100644
@@ -531,6 +531,31 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 		}
 	}
 }
+
+/*
+ * xe_tt_account_iova_pages - account a tt's pages from a full folio walk
+ * @xe: the xe device
+ * @xe_tt: the xe_ttm_tt to account (its @iova_dma_pages must start zeroed)
+ *
+ * Walk @xe_tt's pages one folio at a time (the same head-page order walk used
+ * by xe_tt_map_iova()) and account each into xe->mem.dma_mapped_pages[] and
+ * @xe_tt->iova_dma_pages[]. Used to (re)derive accounting that exactly matches
+ * the pages a tt is actually mapped with.
+ */
+__maybe_unused
+static void xe_tt_account_iova_pages(struct xe_device *xe,
+				     struct xe_ttm_tt *xe_tt)
+{
+	struct ttm_tt *tt = &xe_tt->ttm;
+	unsigned long i = 0;
+
+	while (i < tt->num_pages) {
+		unsigned int order = ttm_pool_page_order_nodma(tt->pages[i]);
+
+		xe_tt_account_iova_add(xe, xe_tt, order);
+		i += 1UL << order;
+	}
+}
 #else
 static void xe_tt_account_iova_add(struct xe_device *xe,
 				   struct xe_ttm_tt *xe_tt, unsigned int order)
@@ -541,6 +566,12 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 				 struct xe_ttm_tt *xe_tt)
 {
 }
+
+__maybe_unused
+static void xe_tt_account_iova_pages(struct xe_device *xe,
+				     struct xe_ttm_tt *xe_tt)
+{
+}
 #endif
 
 static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt)
@@ -714,6 +745,339 @@ void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
 		xe_res_first_sg(xe_tt->sg, start, size, cur);
 }
 
+/**
+ * struct xe_tt_iova_run - A contiguous run of changed pages in a defrag move
+ * @start: First page offset (natural ttm_tt index) of the run.
+ * @count: Number of pages in the run.
+ *
+ * Runs are folio aligned: defrag borrows whole beneficial-order chunks, so a
+ * folio is either entirely borrowed (unchanged) or entirely freshly allocated
+ * (changed), never split across the boundary.
+ */
+struct xe_tt_iova_run {
+	pgoff_t start;
+	pgoff_t count;
+};
+
+/**
+ * struct xe_tt_iova_link - A snapshot folio link descriptor for defrag relink
+ * @phys: Physical address of the folio's head page.
+ * @offset: Natural byte offset into the reservation to link the folio at.
+ * @len: Byte length of the folio (PAGE_SIZE << order).
+ *
+ * Captured at copy-init time while the destination tt's page array is valid and
+ * stable under the BO lock. The post-copy finalize job runs asynchronously,
+ * after TTM may have mutated (or torn down) the destination tt page array, so
+ * it relinks purely from these snapshots and never dereferences that array.
+ */
+struct xe_tt_iova_link {
+	phys_addr_t phys;
+	size_t offset;
+	size_t len;
+};
+
+/**
+ * struct xe_tt_iova_copy - Changed-page subset of an IOVA defrag move
+ * @runs: Array of changed-page runs, in ascending natural tt offset.
+ * @nr_runs: Number of entries in @runs.
+ * @links: Per-folio link snapshots covering the changed runs, in ascending
+ *         natural offset. Drives the asynchronous finalize relink so it never
+ *         touches the destination tt's (possibly mutated) page array.
+ * @nr_links: Number of entries in @links.
+ * @nr_changed: Total number of changed pages (sum of the run counts). This is
+ *              also the size, in pages, of the packed copy-step IOVA mapping
+ *              produced by xe_tt_map_iova_copy().
+ */
+struct xe_tt_iova_copy {
+	struct xe_tt_iova_run *runs;
+	unsigned int nr_runs;
+	struct xe_tt_iova_link *links;
+	unsigned int nr_links;
+	pgoff_t nr_changed;
+};
+
+/*
+ * xe_tt_iova_walk_changed - Walk a defrag move's changed-page runs.
+ *
+ * Iterate @new_tt one folio at a time (folio order read from TTM via
+ * ttm_pool_page_order_nodma()) and compare each folio's head page against
+ * @old_tt at the same natural offset. Pages the new backing borrowed from the
+ * old tt alias the same struct page and are unchanged; freshly allocated pages
+ * differ. Coalesce contiguous changed folios into runs and, for each run,
+ * invoke @fn. Returns the total number of changed pages.
+ */
+__maybe_unused
+static pgoff_t xe_tt_iova_walk_changed(struct ttm_tt *new_tt,
+				       struct ttm_tt *old_tt,
+				       void (*fn)(pgoff_t start, pgoff_t count,
+						  void *arg),
+				       void *arg)
+{
+	pgoff_t i = 0, n = new_tt->num_pages;
+	pgoff_t run_start = 0, nr_changed = 0;
+	bool in_run = false;
+
+	while (i < n) {
+		unsigned int order = ttm_pool_page_order_nodma(new_tt->pages[i]);
+		pgoff_t nr = 1UL << order;
+		bool changed = new_tt->pages[i] != old_tt->pages[i];
+
+		if (changed) {
+			if (!in_run) {
+				run_start = i;
+				in_run = true;
+			}
+			nr_changed += nr;
+		} else if (in_run) {
+			if (fn)
+				fn(run_start, i - run_start, arg);
+			in_run = false;
+		}
+
+		i += nr;
+	}
+
+	if (in_run && fn)
+		fn(run_start, n - run_start, arg);
+
+	return nr_changed;
+}
+
+struct xe_tt_iova_run_collect {
+	struct ttm_tt *new_tt;
+	struct xe_tt_iova_run *runs;
+	unsigned int nr_runs;
+	unsigned int runs_cap;
+	struct xe_tt_iova_link *links;
+	unsigned int nr_links;
+	unsigned int links_cap;
+	int err;
+};
+
+/* Geometrically grow @arr (element size @elem) to hold at least @need slots. */
+__maybe_unused
+static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
+			   size_t elem)
+{
+	unsigned int new_cap;
+	void *p;
+
+	if (need <= *cap)
+		return 0;
+
+	new_cap = *cap ? *cap * 2 : 64;
+	if (new_cap < need)
+		new_cap = need;
+
+	p = krealloc_array(*arr, new_cap, elem, GFP_KERNEL);
+	if (!p)
+		return -ENOMEM;
+
+	*arr = p;
+	*cap = new_cap;
+	return 0;
+}
+
+__maybe_unused
+static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
+{
+	struct xe_tt_iova_run_collect *c = arg;
+	pgoff_t i = start, end = start + count;
+
+	if (c->err)
+		return;
+
+	if (xe_tt_iova_grow((void **)&c->runs, &c->runs_cap, c->nr_runs + 1,
+			    sizeof(*c->runs))) {
+		c->err = -ENOMEM;
+		return;
+	}
+	c->runs[c->nr_runs].start = start;
+	c->runs[c->nr_runs].count = count;
+	c->nr_runs++;
+
+	while (i < end) {
+		struct page *page = c->new_tt->pages[i];
+		unsigned int order = ttm_pool_page_order_nodma(page);
+
+		if (xe_tt_iova_grow((void **)&c->links, &c->links_cap,
+				    c->nr_links + 1, sizeof(*c->links))) {
+			c->err = -ENOMEM;
+			return;
+		}
+		c->links[c->nr_links].phys = page_to_phys(page);
+		c->links[c->nr_links].offset = (size_t)i << PAGE_SHIFT;
+		c->links[c->nr_links].len = PAGE_SIZE << order;
+		c->nr_links++;
+
+		i += 1UL << order;
+	}
+}
+
+/**
+ * xe_tt_iova_copy_init() - Compute the changed-page subset of a defrag move
+ * @new_tt: The freshly (re)allocated destination tt.
+ * @old_tt: The stashed source tt (bo->defrag_old_tt).
+ * @copy: Output description of the changed pages.
+ *
+ * Diff @new_tt against @old_tt and record the runs of changed (non-borrowed)
+ * pages into @copy, along with a per-folio snapshot of their physical
+ * addresses, natural offsets and lengths. The result drives both the packed
+ * copy-step IOVA mapping (xe_tt_map_iova_copy()) and the post-copy finalize
+ * that relinks the new backing into the IOVA reservation. The finalize runs
+ * asynchronously, after TTM may have mutated @new_tt->pages, so it consumes the
+ * snapshot rather than re-reading the page array. @new_tt and @old_tt must
+ * describe the same BO (equal num_pages) and both be populated.
+ *
+ * The caller owns @copy and must release it with xe_tt_iova_copy_fini().
+ *
+ * Return: 0 on success, -ENOMEM on allocation failure.
+ */
+__maybe_unused
+static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
+				struct xe_tt_iova_copy *copy)
+{
+	struct xe_tt_iova_run_collect c = { .new_tt = new_tt };
+
+	memset(copy, 0, sizeof(*copy));
+
+	/*
+	 * Single pass: collect the changed runs and per-folio link snapshots
+	 * directly, growing the arrays on demand. Changed runs are few (the
+	 * borrowed majority is skipped), so the geometric reallocs are rare and
+	 * this avoids a second full O(num_pages) walk just to size the arrays.
+	 */
+	copy->nr_changed = xe_tt_iova_walk_changed(new_tt, old_tt,
+						   xe_tt_iova_store_run, &c);
+	if (c.err) {
+		kfree(c.runs);
+		kfree(c.links);
+		return c.err;
+	}
+	if (!c.nr_runs)
+		return 0;
+
+	copy->runs = c.runs;
+	copy->nr_runs = c.nr_runs;
+	copy->links = c.links;
+	copy->nr_links = c.nr_links;
+
+	return 0;
+}
+
+/**
+ * xe_tt_iova_copy_fini() - Release a struct xe_tt_iova_copy
+ * @copy: The copy description to release.
+ */
+__maybe_unused
+static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
+{
+	kfree(copy->runs);
+	kfree(copy->links);
+	copy->runs = NULL;
+	copy->links = NULL;
+	copy->nr_runs = 0;
+	copy->nr_links = 0;
+	copy->nr_changed = 0;
+}
+
+/**
+ * xe_tt_map_iova_copy() - DMA map the changed-page subset for a defrag copy
+ * @xe: The xe device.
+ * @new_tt: The destination tt (must be IOVA eligible, use_iova == true).
+ * @copy: The changed-page description from xe_tt_iova_copy_init().
+ *
+ * Link only the changed pages of @new_tt into its IOVA reservation, packed
+ * contiguously at the front of the range ([0, @copy->nr_changed) pages). This
+ * provides a compact, contiguous destination for the GPU defrag copy without
+ * mapping the (majority) borrowed pages, and lets the whole temporary mapping
+ * later be torn down with a single dma_iova_destroy().
+ *
+ * The reservation's remaining tail is left unlinked. This does not establish
+ * the tt's final mapping (@iova_linked stays false); the post-copy finalize is
+ * responsible for that.
+ *
+ * Return: 0 on success, negative error code on failure (any partial linkage is
+ * unwound).
+ */
+__maybe_unused
+static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
+			       const struct xe_tt_iova_copy *copy)
+{
+	struct xe_ttm_tt *xe_tt = container_of(new_tt, struct xe_ttm_tt, ttm);
+	struct device *dev = xe->drm.dev;
+	size_t mapped = 0;
+	unsigned int r;
+	int ret;
+
+	xe_assert(xe, xe_tt->use_iova);
+	xe_assert(xe, !xe_tt->iova_linked);
+
+	for (r = 0; r < copy->nr_runs; r++) {
+		pgoff_t i = copy->runs[r].start;
+		pgoff_t end = i + copy->runs[r].count;
+
+		while (i < end) {
+			struct page *page = new_tt->pages[i];
+			unsigned int order = ttm_pool_page_order_nodma(page);
+			size_t len = PAGE_SIZE << order;
+
+			ret = dma_iova_link(dev, &xe_tt->iova,
+					    page_to_phys(page), mapped, len,
+					    DMA_BIDIRECTIONAL,
+					    DMA_ATTR_SKIP_CPU_SYNC);
+			if (ret)
+				goto err_unlink;
+
+			mapped += len;
+			i += 1UL << order;
+		}
+	}
+
+	ret = dma_iova_sync(dev, &xe_tt->iova, 0, mapped);
+	if (ret)
+		goto err_unlink;
+
+	return 0;
+
+err_unlink:
+	if (mapped)
+		dma_iova_unlink(dev, &xe_tt->iova, 0, mapped,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+	return ret;
+}
+
+/*
+ * xe_tt_iova_transfer - Transfer a full IOVA reservation old_tt -> new_tt.
+ *
+ * Move the (fully linked) IOVA reservation from the old tt to the new tt,
+ * leaving the old tt without a reservation so its destroy path does not double
+ * free. The new tt keeps its existing reservation snapshotted in the finalize
+ * job for teardown. Called at the defrag finalize job's point of no failure.
+ *
+ * Also rebalance the per-order DMA-page accounting: drop the old tt's
+ * accounting and account the pages the new tt actually ends up mapped with.
+ * The finalize relink leaves the new tt's mapping equal to new_tt->pages
+ * (borrowed pages alias the old tt; changed runs use the new pages, which may
+ * have a different order distribution), so deriving the accounting from a full
+ * folio walk of the new tt keeps xe->mem.dma_mapped_pages[] exact while the new
+ * mapping lives, rather than carrying the old tt's stale distribution.
+ */
+__maybe_unused
+static void xe_tt_iova_transfer(struct xe_device *xe,
+				struct xe_ttm_tt *new_tt,
+				struct xe_ttm_tt *old_tt)
+{
+	new_tt->iova = old_tt->iova;
+	new_tt->iova_linked = true;
+
+	xe_tt_unaccount_iova(xe, old_tt);
+	xe_tt_account_iova_pages(xe, new_tt);
+
+	old_tt->iova_linked = false;
+	old_tt->use_iova = false;
+}
+
 /*
  * Account ttm pages against the device shrinker's shrinkable and
  * purgeable counts.
Signature · 1 line
2.34.1
#122 lore raw

Re: [PATCH v3 30/33] drm/xe: Add packed copy-step IOVA mapping for defrag

Quoted 41 lines · Matthew Brost

Add machinery to map only changed pages during defrag when the destination TT is IOVA-mapped. Only the non-borrowed (freshly allocated) pages need copying; borrowed pages are aliases from the old TT.

Add xe_tt_iova_copy_init() to diff new vs old TT folios and record changed-page runs. Add xe_tt_map_iova_copy() to link those changed pages packed contiguously at the front of the new TT's IOVA reservation, synced and ready for GPU blits.

No users yet; wired up in later patches.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---
drivers/gpu/drm/xe/xe_bo.c | 364 +++++++++++++++++++++++++++++++++++++

1 file changed, 364 insertions(+)
drivers/gpu/drm/xe/xe_bo.c +2 -0
index c5022c16411f..c087f17ad6db 100644
@@ -531,6 +531,31 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 		}
 	}
 }
+
+/*

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_unaccount_iova(struct xe_device *xe, · line 545

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+ * xe_tt_account_iova_pages - account a tt's pages from a full folio walk
+ * @xe: the xe device
+ * @xe_tt: the xe_ttm_tt to account (its @iova_dma_pages must start zeroed)
+ *
+ * Walk @xe_tt's pages one folio at a time (the same head-page order walk used
+ * by xe_tt_map_iova()) and account each into xe->mem.dma_mapped_pages[] and
+ * @xe_tt->iova_dma_pages[]. Used to (re)derive accounting that exactly matches
+ * the pages a tt is actually mapped with.
+ */
+__maybe_unused
Assisted-by LLM

NIT..I see may __maybe_unused added here and removed in patch 32. Maybe we can avoid adding them here to minimize the changes.

Quoted 100 lines
drivers/gpu/drm/xe/xe_bo.c +86 -0
+static void xe_tt_account_iova_pages(struct xe_device *xe,
+				     struct xe_ttm_tt *xe_tt)
+{
+	struct ttm_tt *tt = &xe_tt->ttm;
+	unsigned long i = 0;
+
+	while (i < tt->num_pages) {
+		unsigned int order = ttm_pool_page_order_nodma(tt->pages[i]);
+
+		xe_tt_account_iova_add(xe, xe_tt, order);
+		i += 1UL << order;
+	}
+}
 #else
 static void xe_tt_account_iova_add(struct xe_device *xe,
 				   struct xe_ttm_tt *xe_tt, unsigned int order)
@@ -541,6 +566,12 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 				 struct xe_ttm_tt *xe_tt)
 {
 }
+
+__maybe_unused
+static void xe_tt_account_iova_pages(struct xe_device *xe,
+				     struct xe_ttm_tt *xe_tt)
+{
+}
 #endif
 
 static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt)
@@ -714,6 +745,339 @@ void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
 		xe_res_first_sg(xe_tt->sg, start, size, cur);
 }
 
+/**
+ * struct xe_tt_iova_run - A contiguous run of changed pages in a defrag move
+ * @start: First page offset (natural ttm_tt index) of the run.
+ * @count: Number of pages in the run.
+ *
+ * Runs are folio aligned: defrag borrows whole beneficial-order chunks, so a
+ * folio is either entirely borrowed (unchanged) or entirely freshly allocated
+ * (changed), never split across the boundary.
+ */
+struct xe_tt_iova_run {
+	pgoff_t start;
+	pgoff_t count;
+};
+
+/**
+ * struct xe_tt_iova_link - A snapshot folio link descriptor for defrag relink
+ * @phys: Physical address of the folio's head page.
+ * @offset: Natural byte offset into the reservation to link the folio at.
+ * @len: Byte length of the folio (PAGE_SIZE << order).
+ *
+ * Captured at copy-init time while the destination tt's page array is valid and
+ * stable under the BO lock. The post-copy finalize job runs asynchronously,
+ * after TTM may have mutated (or torn down) the destination tt page array, so
+ * it relinks purely from these snapshots and never dereferences that array.
+ */
+struct xe_tt_iova_link {
+	phys_addr_t phys;
+	size_t offset;
+	size_t len;
+};
+
+/**
+ * struct xe_tt_iova_copy - Changed-page subset of an IOVA defrag move
+ * @runs: Array of changed-page runs, in ascending natural tt offset.
+ * @nr_runs: Number of entries in @runs.
+ * @links: Per-folio link snapshots covering the changed runs, in ascending
+ *         natural offset. Drives the asynchronous finalize relink so it never
+ *         touches the destination tt's (possibly mutated) page array.
+ * @nr_links: Number of entries in @links.
+ * @nr_changed: Total number of changed pages (sum of the run counts). This is
+ *              also the size, in pages, of the packed copy-step IOVA mapping
+ *              produced by xe_tt_map_iova_copy().
+ */
+struct xe_tt_iova_copy {
+	struct xe_tt_iova_run *runs;
+	unsigned int nr_runs;
+	struct xe_tt_iova_link *links;
+	unsigned int nr_links;
+	pgoff_t nr_changed;
+};
+
+/*
+ * xe_tt_iova_walk_changed - Walk a defrag move's changed-page runs.
+ *
+ * Iterate @new_tt one folio at a time (folio order read from TTM via
+ * ttm_pool_page_order_nodma()) and compare each folio's head page against
+ * @old_tt at the same natural offset. Pages the new backing borrowed from the
+ * old tt alias the same struct page and are unchanged; freshly allocated pages
+ * differ. Coalesce contiguous changed folios into runs and, for each run,
+ * invoke @fn. Returns the total number of changed pages.
+ */
+__maybe_unused
+static pgoff_t xe_tt_iova_walk_changed(struct ttm_tt *new_tt,
+				       struct ttm_tt *old_tt,
+				       void (*fn)(pgoff_t start, pgoff_t count,
+						  void *arg),
+				       void *arg)

drivers/gpu/drm/xe/xe_bo.c void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size, · line 824

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+{
+	pgoff_t i = 0, n = new_tt->num_pages;
+	pgoff_t run_start = 0, nr_changed = 0;
+	bool in_run = false;
+
+	while (i < n) {
+		unsigned int order = ttm_pool_page_order_nodma(new_tt->pages[i]);
+		pgoff_t nr = 1UL << order;
+		bool changed = new_tt->pages[i] != old_tt->pages[i];
+

NIT..I am seeing here and in other places, code implies "new chunk = union of whole old chunks"
(new chunk never replaces part of old chunk).
But this seems implied and I am not not sure if there is any single place where we ensure that and document it.

Niranjana

Quoted 262 lines
drivers/gpu/drm/xe/xe_bo.c +256 -0
+		if (changed) {
+			if (!in_run) {
+				run_start = i;
+				in_run = true;
+			}
+			nr_changed += nr;
+		} else if (in_run) {
+			if (fn)
+				fn(run_start, i - run_start, arg);
+			in_run = false;
+		}
+
+		i += nr;
+	}
+
+	if (in_run && fn)
+		fn(run_start, n - run_start, arg);
+
+	return nr_changed;
+}
+
+struct xe_tt_iova_run_collect {
+	struct ttm_tt *new_tt;
+	struct xe_tt_iova_run *runs;
+	unsigned int nr_runs;
+	unsigned int runs_cap;
+	struct xe_tt_iova_link *links;
+	unsigned int nr_links;
+	unsigned int links_cap;
+	int err;
+};
+
+/* Geometrically grow @arr (element size @elem) to hold at least @need slots. */
+__maybe_unused
+static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
+			   size_t elem)
+{
+	unsigned int new_cap;
+	void *p;
+
+	if (need <= *cap)
+		return 0;
+
+	new_cap = *cap ? *cap * 2 : 64;
+	if (new_cap < need)
+		new_cap = need;
+
+	p = krealloc_array(*arr, new_cap, elem, GFP_KERNEL);
+	if (!p)
+		return -ENOMEM;
+
+	*arr = p;
+	*cap = new_cap;
+	return 0;
+}
+
+__maybe_unused
+static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
+{
+	struct xe_tt_iova_run_collect *c = arg;
+	pgoff_t i = start, end = start + count;
+
+	if (c->err)
+		return;
+
+	if (xe_tt_iova_grow((void **)&c->runs, &c->runs_cap, c->nr_runs + 1,
+			    sizeof(*c->runs))) {
+		c->err = -ENOMEM;
+		return;
+	}
+	c->runs[c->nr_runs].start = start;
+	c->runs[c->nr_runs].count = count;
+	c->nr_runs++;
+
+	while (i < end) {
+		struct page *page = c->new_tt->pages[i];
+		unsigned int order = ttm_pool_page_order_nodma(page);
+
+		if (xe_tt_iova_grow((void **)&c->links, &c->links_cap,
+				    c->nr_links + 1, sizeof(*c->links))) {
+			c->err = -ENOMEM;
+			return;
+		}
+		c->links[c->nr_links].phys = page_to_phys(page);
+		c->links[c->nr_links].offset = (size_t)i << PAGE_SHIFT;
+		c->links[c->nr_links].len = PAGE_SIZE << order;
+		c->nr_links++;
+
+		i += 1UL << order;
+	}
+}
+
+/**
+ * xe_tt_iova_copy_init() - Compute the changed-page subset of a defrag move
+ * @new_tt: The freshly (re)allocated destination tt.
+ * @old_tt: The stashed source tt (bo->defrag_old_tt).
+ * @copy: Output description of the changed pages.
+ *
+ * Diff @new_tt against @old_tt and record the runs of changed (non-borrowed)
+ * pages into @copy, along with a per-folio snapshot of their physical
+ * addresses, natural offsets and lengths. The result drives both the packed
+ * copy-step IOVA mapping (xe_tt_map_iova_copy()) and the post-copy finalize
+ * that relinks the new backing into the IOVA reservation. The finalize runs
+ * asynchronously, after TTM may have mutated @new_tt->pages, so it consumes the
+ * snapshot rather than re-reading the page array. @new_tt and @old_tt must
+ * describe the same BO (equal num_pages) and both be populated.
+ *
+ * The caller owns @copy and must release it with xe_tt_iova_copy_fini().
+ *
+ * Return: 0 on success, -ENOMEM on allocation failure.
+ */
+__maybe_unused
+static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
+				struct xe_tt_iova_copy *copy)
+{
+	struct xe_tt_iova_run_collect c = { .new_tt = new_tt };
+
+	memset(copy, 0, sizeof(*copy));
+
+	/*
+	 * Single pass: collect the changed runs and per-folio link snapshots
+	 * directly, growing the arrays on demand. Changed runs are few (the
+	 * borrowed majority is skipped), so the geometric reallocs are rare and
+	 * this avoids a second full O(num_pages) walk just to size the arrays.
+	 */
+	copy->nr_changed = xe_tt_iova_walk_changed(new_tt, old_tt,
+						   xe_tt_iova_store_run, &c);
+	if (c.err) {
+		kfree(c.runs);
+		kfree(c.links);
+		return c.err;
+	}
+	if (!c.nr_runs)
+		return 0;
+
+	copy->runs = c.runs;
+	copy->nr_runs = c.nr_runs;
+	copy->links = c.links;
+	copy->nr_links = c.nr_links;
+
+	return 0;
+}
+
+/**
+ * xe_tt_iova_copy_fini() - Release a struct xe_tt_iova_copy
+ * @copy: The copy description to release.
+ */
+__maybe_unused
+static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
+{
+	kfree(copy->runs);
+	kfree(copy->links);
+	copy->runs = NULL;
+	copy->links = NULL;
+	copy->nr_runs = 0;
+	copy->nr_links = 0;
+	copy->nr_changed = 0;
+}
+
+/**
+ * xe_tt_map_iova_copy() - DMA map the changed-page subset for a defrag copy
+ * @xe: The xe device.
+ * @new_tt: The destination tt (must be IOVA eligible, use_iova == true).
+ * @copy: The changed-page description from xe_tt_iova_copy_init().
+ *
+ * Link only the changed pages of @new_tt into its IOVA reservation, packed
+ * contiguously at the front of the range ([0, @copy->nr_changed) pages). This
+ * provides a compact, contiguous destination for the GPU defrag copy without
+ * mapping the (majority) borrowed pages, and lets the whole temporary mapping
+ * later be torn down with a single dma_iova_destroy().
+ *
+ * The reservation's remaining tail is left unlinked. This does not establish
+ * the tt's final mapping (@iova_linked stays false); the post-copy finalize is
+ * responsible for that.
+ *
+ * Return: 0 on success, negative error code on failure (any partial linkage is
+ * unwound).
+ */
+__maybe_unused
+static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
+			       const struct xe_tt_iova_copy *copy)
+{
+	struct xe_ttm_tt *xe_tt = container_of(new_tt, struct xe_ttm_tt, ttm);
+	struct device *dev = xe->drm.dev;
+	size_t mapped = 0;
+	unsigned int r;
+	int ret;
+
+	xe_assert(xe, xe_tt->use_iova);
+	xe_assert(xe, !xe_tt->iova_linked);
+
+	for (r = 0; r < copy->nr_runs; r++) {
+		pgoff_t i = copy->runs[r].start;
+		pgoff_t end = i + copy->runs[r].count;
+
+		while (i < end) {
+			struct page *page = new_tt->pages[i];
+			unsigned int order = ttm_pool_page_order_nodma(page);
+			size_t len = PAGE_SIZE << order;
+
+			ret = dma_iova_link(dev, &xe_tt->iova,
+					    page_to_phys(page), mapped, len,
+					    DMA_BIDIRECTIONAL,
+					    DMA_ATTR_SKIP_CPU_SYNC);
+			if (ret)
+				goto err_unlink;
+
+			mapped += len;
+			i += 1UL << order;
+		}
+	}
+
+	ret = dma_iova_sync(dev, &xe_tt->iova, 0, mapped);
+	if (ret)
+		goto err_unlink;
+
+	return 0;
+
+err_unlink:
+	if (mapped)
+		dma_iova_unlink(dev, &xe_tt->iova, 0, mapped,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+	return ret;
+}
+
+/*
+ * xe_tt_iova_transfer - Transfer a full IOVA reservation old_tt -> new_tt.
+ *
+ * Move the (fully linked) IOVA reservation from the old tt to the new tt,
+ * leaving the old tt without a reservation so its destroy path does not double
+ * free. The new tt keeps its existing reservation snapshotted in the finalize
+ * job for teardown. Called at the defrag finalize job's point of no failure.
+ *
+ * Also rebalance the per-order DMA-page accounting: drop the old tt's
+ * accounting and account the pages the new tt actually ends up mapped with.
+ * The finalize relink leaves the new tt's mapping equal to new_tt->pages
+ * (borrowed pages alias the old tt; changed runs use the new pages, which may
+ * have a different order distribution), so deriving the accounting from a full
+ * folio walk of the new tt keeps xe->mem.dma_mapped_pages[] exact while the new
+ * mapping lives, rather than carrying the old tt's stale distribution.
+ */
+__maybe_unused
+static void xe_tt_iova_transfer(struct xe_device *xe,
+				struct xe_ttm_tt *new_tt,
+				struct xe_ttm_tt *old_tt)
+{
+	new_tt->iova = old_tt->iova;
+	new_tt->iova_linked = true;
+
+	xe_tt_unaccount_iova(xe, old_tt);
+	xe_tt_account_iova_pages(xe, new_tt);
+
+	old_tt->iova_linked = false;
+	old_tt->use_iova = false;
+}
+
 /*
 * Account ttm pages against the device shrinker's shrinkable and
 * purgeable counts.
Signature · 1 line
2.34.1
author maintainer
#123 lore raw

Re: [PATCH v3 30/33] drm/xe: Add packed copy-step IOVA mapping for defrag

Quoted 55 lines · Niranjana Vishwanathapura
Quoted 41 lines · Matthew Brost

Add machinery to map only changed pages during defrag when the destination TT is IOVA-mapped. Only the non-borrowed (freshly allocated) pages need copying; borrowed pages are aliases from the old TT.

Add xe_tt_iova_copy_init() to diff new vs old TT folios and record changed-page runs. Add xe_tt_map_iova_copy() to link those changed pages packed contiguously at the front of the new TT's IOVA reservation, synced and ready for GPU blits.

No users yet; wired up in later patches.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---
drivers/gpu/drm/xe/xe_bo.c | 364 +++++++++++++++++++++++++++++++++++++

1 file changed, 364 insertions(+)
drivers/gpu/drm/xe/xe_bo.c +2 -0
index c5022c16411f..c087f17ad6db 100644
@@ -531,6 +531,31 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 		}
 	}
 }
+
+/*

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_unaccount_iova(struct xe_device *xe, · line 545

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+ * xe_tt_account_iova_pages - account a tt's pages from a full folio walk
+ * @xe: the xe device
+ * @xe_tt: the xe_ttm_tt to account (its @iova_dma_pages must start zeroed)
+ *
+ * Walk @xe_tt's pages one folio at a time (the same head-page order walk used
+ * by xe_tt_map_iova()) and account each into xe->mem.dma_mapped_pages[] and
+ * @xe_tt->iova_dma_pages[]. Used to (re)derive accounting that exactly matches
+ * the pages a tt is actually mapped with.
+ */
+__maybe_unused
Assisted-by LLM
Quoted 3 lines

NIT..I see may __maybe_unused added here and removed in patch 32. Maybe we can avoid adding them here to minimize the changes.

It won't compile without __maybe_unused without patch 32.

Generally you want every single patch in a series to compile, thus __maybe_unused is used here.

Quoted 108 lines
Quoted 108 lines
drivers/gpu/drm/xe/xe_bo.c +94 -0
+static void xe_tt_account_iova_pages(struct xe_device *xe,
+				     struct xe_ttm_tt *xe_tt)
+{
+	struct ttm_tt *tt = &xe_tt->ttm;
+	unsigned long i = 0;
+
+	while (i < tt->num_pages) {
+		unsigned int order = ttm_pool_page_order_nodma(tt->pages[i]);
+
+		xe_tt_account_iova_add(xe, xe_tt, order);
+		i += 1UL << order;
+	}
+}
 #else
 static void xe_tt_account_iova_add(struct xe_device *xe,
 				   struct xe_ttm_tt *xe_tt, unsigned int order)
@@ -541,6 +566,12 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 				 struct xe_ttm_tt *xe_tt)
 {
 }
+
+__maybe_unused
+static void xe_tt_account_iova_pages(struct xe_device *xe,
+				     struct xe_ttm_tt *xe_tt)
+{
+}
 #endif
 
 static int xe_tt_map_sg(struct xe_device *xe, struct ttm_tt *tt)
@@ -714,6 +745,339 @@ void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size,
 		xe_res_first_sg(xe_tt->sg, start, size, cur);
 }
 
+/**
+ * struct xe_tt_iova_run - A contiguous run of changed pages in a defrag move
+ * @start: First page offset (natural ttm_tt index) of the run.
+ * @count: Number of pages in the run.
+ *
+ * Runs are folio aligned: defrag borrows whole beneficial-order chunks, so a
+ * folio is either entirely borrowed (unchanged) or entirely freshly allocated
+ * (changed), never split across the boundary.
+ */
+struct xe_tt_iova_run {
+	pgoff_t start;
+	pgoff_t count;
+};
+
+/**
+ * struct xe_tt_iova_link - A snapshot folio link descriptor for defrag relink
+ * @phys: Physical address of the folio's head page.
+ * @offset: Natural byte offset into the reservation to link the folio at.
+ * @len: Byte length of the folio (PAGE_SIZE << order).
+ *
+ * Captured at copy-init time while the destination tt's page array is valid and
+ * stable under the BO lock. The post-copy finalize job runs asynchronously,
+ * after TTM may have mutated (or torn down) the destination tt page array, so
+ * it relinks purely from these snapshots and never dereferences that array.
+ */
+struct xe_tt_iova_link {
+	phys_addr_t phys;
+	size_t offset;
+	size_t len;
+};
+
+/**
+ * struct xe_tt_iova_copy - Changed-page subset of an IOVA defrag move
+ * @runs: Array of changed-page runs, in ascending natural tt offset.
+ * @nr_runs: Number of entries in @runs.
+ * @links: Per-folio link snapshots covering the changed runs, in ascending
+ *         natural offset. Drives the asynchronous finalize relink so it never
+ *         touches the destination tt's (possibly mutated) page array.
+ * @nr_links: Number of entries in @links.
+ * @nr_changed: Total number of changed pages (sum of the run counts). This is
+ *              also the size, in pages, of the packed copy-step IOVA mapping
+ *              produced by xe_tt_map_iova_copy().
+ */
+struct xe_tt_iova_copy {
+	struct xe_tt_iova_run *runs;
+	unsigned int nr_runs;
+	struct xe_tt_iova_link *links;
+	unsigned int nr_links;
+	pgoff_t nr_changed;
+};
+
+/*
+ * xe_tt_iova_walk_changed - Walk a defrag move's changed-page runs.
+ *
+ * Iterate @new_tt one folio at a time (folio order read from TTM via
+ * ttm_pool_page_order_nodma()) and compare each folio's head page against
+ * @old_tt at the same natural offset. Pages the new backing borrowed from the
+ * old tt alias the same struct page and are unchanged; freshly allocated pages
+ * differ. Coalesce contiguous changed folios into runs and, for each run,
+ * invoke @fn. Returns the total number of changed pages.
+ */
+__maybe_unused
+static pgoff_t xe_tt_iova_walk_changed(struct ttm_tt *new_tt,
+				       struct ttm_tt *old_tt,
+				       void (*fn)(pgoff_t start, pgoff_t count,
+						  void *arg),
+				       void *arg)
+{
+	pgoff_t i = 0, n = new_tt->num_pages;
+	pgoff_t run_start = 0, nr_changed = 0;
+	bool in_run = false;
+
+	while (i < n) {
+		unsigned int order = ttm_pool_page_order_nodma(new_tt->pages[i]);
+		pgoff_t nr = 1UL << order;
Quoted 9 lines

drivers/gpu/drm/xe/xe_bo.c void xe_res_first_tt(struct ttm_tt *tt, u64 start, u64 size, · line 824

Quoted 2 lines
drivers/gpu/drm/xe/xe_bo.c +2 -0
+		bool changed = new_tt->pages[i] != old_tt->pages[i];
+

NIT..I am seeing here and in other places, code implies "new chunk = union of whole old chunks"
(new chunk never replaces part of old chunk).
But this seems implied and I am not not sure if there is any single place where we ensure that and document it.

I'm not really sure what you are suggesting here.

Matt

Quoted 264 lines

Niranjana

Quoted 262 lines
drivers/gpu/drm/xe/xe_bo.c +256 -0
+		if (changed) {
+			if (!in_run) {
+				run_start = i;
+				in_run = true;
+			}
+			nr_changed += nr;
+		} else if (in_run) {
+			if (fn)
+				fn(run_start, i - run_start, arg);
+			in_run = false;
+		}
+
+		i += nr;
+	}
+
+	if (in_run && fn)
+		fn(run_start, n - run_start, arg);
+
+	return nr_changed;
+}
+
+struct xe_tt_iova_run_collect {
+	struct ttm_tt *new_tt;
+	struct xe_tt_iova_run *runs;
+	unsigned int nr_runs;
+	unsigned int runs_cap;
+	struct xe_tt_iova_link *links;
+	unsigned int nr_links;
+	unsigned int links_cap;
+	int err;
+};
+
+/* Geometrically grow @arr (element size @elem) to hold at least @need slots. */
+__maybe_unused
+static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
+			   size_t elem)
+{
+	unsigned int new_cap;
+	void *p;
+
+	if (need <= *cap)
+		return 0;
+
+	new_cap = *cap ? *cap * 2 : 64;
+	if (new_cap < need)
+		new_cap = need;
+
+	p = krealloc_array(*arr, new_cap, elem, GFP_KERNEL);
+	if (!p)
+		return -ENOMEM;
+
+	*arr = p;
+	*cap = new_cap;
+	return 0;
+}
+
+__maybe_unused
+static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
+{
+	struct xe_tt_iova_run_collect *c = arg;
+	pgoff_t i = start, end = start + count;
+
+	if (c->err)
+		return;
+
+	if (xe_tt_iova_grow((void **)&c->runs, &c->runs_cap, c->nr_runs + 1,
+			    sizeof(*c->runs))) {
+		c->err = -ENOMEM;
+		return;
+	}
+	c->runs[c->nr_runs].start = start;
+	c->runs[c->nr_runs].count = count;
+	c->nr_runs++;
+
+	while (i < end) {
+		struct page *page = c->new_tt->pages[i];
+		unsigned int order = ttm_pool_page_order_nodma(page);
+
+		if (xe_tt_iova_grow((void **)&c->links, &c->links_cap,
+				    c->nr_links + 1, sizeof(*c->links))) {
+			c->err = -ENOMEM;
+			return;
+		}
+		c->links[c->nr_links].phys = page_to_phys(page);
+		c->links[c->nr_links].offset = (size_t)i << PAGE_SHIFT;
+		c->links[c->nr_links].len = PAGE_SIZE << order;
+		c->nr_links++;
+
+		i += 1UL << order;
+	}
+}
+
+/**
+ * xe_tt_iova_copy_init() - Compute the changed-page subset of a defrag move
+ * @new_tt: The freshly (re)allocated destination tt.
+ * @old_tt: The stashed source tt (bo->defrag_old_tt).
+ * @copy: Output description of the changed pages.
+ *
+ * Diff @new_tt against @old_tt and record the runs of changed (non-borrowed)
+ * pages into @copy, along with a per-folio snapshot of their physical
+ * addresses, natural offsets and lengths. The result drives both the packed
+ * copy-step IOVA mapping (xe_tt_map_iova_copy()) and the post-copy finalize
+ * that relinks the new backing into the IOVA reservation. The finalize runs
+ * asynchronously, after TTM may have mutated @new_tt->pages, so it consumes the
+ * snapshot rather than re-reading the page array. @new_tt and @old_tt must
+ * describe the same BO (equal num_pages) and both be populated.
+ *
+ * The caller owns @copy and must release it with xe_tt_iova_copy_fini().
+ *
+ * Return: 0 on success, -ENOMEM on allocation failure.
+ */
+__maybe_unused
+static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
+				struct xe_tt_iova_copy *copy)
+{
+	struct xe_tt_iova_run_collect c = { .new_tt = new_tt };
+
+	memset(copy, 0, sizeof(*copy));
+
+	/*
+	 * Single pass: collect the changed runs and per-folio link snapshots
+	 * directly, growing the arrays on demand. Changed runs are few (the
+	 * borrowed majority is skipped), so the geometric reallocs are rare and
+	 * this avoids a second full O(num_pages) walk just to size the arrays.
+	 */
+	copy->nr_changed = xe_tt_iova_walk_changed(new_tt, old_tt,
+						   xe_tt_iova_store_run, &c);
+	if (c.err) {
+		kfree(c.runs);
+		kfree(c.links);
+		return c.err;
+	}
+	if (!c.nr_runs)
+		return 0;
+
+	copy->runs = c.runs;
+	copy->nr_runs = c.nr_runs;
+	copy->links = c.links;
+	copy->nr_links = c.nr_links;
+
+	return 0;
+}
+
+/**
+ * xe_tt_iova_copy_fini() - Release a struct xe_tt_iova_copy
+ * @copy: The copy description to release.
+ */
+__maybe_unused
+static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
+{
+	kfree(copy->runs);
+	kfree(copy->links);
+	copy->runs = NULL;
+	copy->links = NULL;
+	copy->nr_runs = 0;
+	copy->nr_links = 0;
+	copy->nr_changed = 0;
+}
+
+/**
+ * xe_tt_map_iova_copy() - DMA map the changed-page subset for a defrag copy
+ * @xe: The xe device.
+ * @new_tt: The destination tt (must be IOVA eligible, use_iova == true).
+ * @copy: The changed-page description from xe_tt_iova_copy_init().
+ *
+ * Link only the changed pages of @new_tt into its IOVA reservation, packed
+ * contiguously at the front of the range ([0, @copy->nr_changed) pages). This
+ * provides a compact, contiguous destination for the GPU defrag copy without
+ * mapping the (majority) borrowed pages, and lets the whole temporary mapping
+ * later be torn down with a single dma_iova_destroy().
+ *
+ * The reservation's remaining tail is left unlinked. This does not establish
+ * the tt's final mapping (@iova_linked stays false); the post-copy finalize is
+ * responsible for that.
+ *
+ * Return: 0 on success, negative error code on failure (any partial linkage is
+ * unwound).
+ */
+__maybe_unused
+static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
+			       const struct xe_tt_iova_copy *copy)
+{
+	struct xe_ttm_tt *xe_tt = container_of(new_tt, struct xe_ttm_tt, ttm);
+	struct device *dev = xe->drm.dev;
+	size_t mapped = 0;
+	unsigned int r;
+	int ret;
+
+	xe_assert(xe, xe_tt->use_iova);
+	xe_assert(xe, !xe_tt->iova_linked);
+
+	for (r = 0; r < copy->nr_runs; r++) {
+		pgoff_t i = copy->runs[r].start;
+		pgoff_t end = i + copy->runs[r].count;
+
+		while (i < end) {
+			struct page *page = new_tt->pages[i];
+			unsigned int order = ttm_pool_page_order_nodma(page);
+			size_t len = PAGE_SIZE << order;
+
+			ret = dma_iova_link(dev, &xe_tt->iova,
+					    page_to_phys(page), mapped, len,
+					    DMA_BIDIRECTIONAL,
+					    DMA_ATTR_SKIP_CPU_SYNC);
+			if (ret)
+				goto err_unlink;
+
+			mapped += len;
+			i += 1UL << order;
+		}
+	}
+
+	ret = dma_iova_sync(dev, &xe_tt->iova, 0, mapped);
+	if (ret)
+		goto err_unlink;
+
+	return 0;
+
+err_unlink:
+	if (mapped)
+		dma_iova_unlink(dev, &xe_tt->iova, 0, mapped,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+	return ret;
+}
+
+/*
+ * xe_tt_iova_transfer - Transfer a full IOVA reservation old_tt -> new_tt.
+ *
+ * Move the (fully linked) IOVA reservation from the old tt to the new tt,
+ * leaving the old tt without a reservation so its destroy path does not double
+ * free. The new tt keeps its existing reservation snapshotted in the finalize
+ * job for teardown. Called at the defrag finalize job's point of no failure.
+ *
+ * Also rebalance the per-order DMA-page accounting: drop the old tt's
+ * accounting and account the pages the new tt actually ends up mapped with.
+ * The finalize relink leaves the new tt's mapping equal to new_tt->pages
+ * (borrowed pages alias the old tt; changed runs use the new pages, which may
+ * have a different order distribution), so deriving the accounting from a full
+ * folio walk of the new tt keeps xe->mem.dma_mapped_pages[] exact while the new
+ * mapping lives, rather than carrying the old tt's stale distribution.
+ */
+__maybe_unused
+static void xe_tt_iova_transfer(struct xe_device *xe,
+				struct xe_ttm_tt *new_tt,
+				struct xe_ttm_tt *old_tt)
+{
+	new_tt->iova = old_tt->iova;
+	new_tt->iova_linked = true;
+
+	xe_tt_unaccount_iova(xe, old_tt);
+	xe_tt_account_iova_pages(xe, new_tt);
+
+	old_tt->iova_linked = false;
+	old_tt->use_iova = false;
+}
+
 /*
 * Account ttm pages against the device shrinker's shrinkable and
 * purgeable counts.
Signature · 1 line
2.34.1
author maintainer
#124 lore raw

PATCH v3 31/33 drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies

In reply to Matthew Brost

A defrag move with IOVA enabled only DMA-maps the changed (non-borrowed) pages of the new tt, packed contiguously into the front of its IOVA reservation (xe_tt_map_iova_copy()). The GPU copy therefore has an asymmetric geometry: the source is read from the old tt at its natural page offsets (via the old tt's full IOVA), while the destination only exposes the changed pages, packed.

Teach __xe_migrate_copy() about this packed-destination mode:

- Add a @dst_packed_pages argument. When non-zero the destination tt cursor walks just that many packed pages and the source (not the destination) drives the chunk size, so the smaller packed prefix never clamps the source walk.

- Size the destination PTE update for each chunk from the changed-page count (xe_migrate_count_changed_pages()) rather than the full chunk, and skip the destination PTEs entirely for an all-borrowed chunk.

- Extend emit_copy_chunks() with a @pack_dst flag so each emitted run is blitted from its natural source offset into a running packed destination offset that only advances for changed runs. The all-borrowed skip leaves the packed destination cursor untouched.

Expose this via xe_migrate_copy_defrag_iova(). It is only valid when the BO needs no CCS pass, since the separate CCS pass migrates aux state for all pages and would require the full destination mapping. The existing xe_migrate_copy_defrag() (full destination mapping) remains the path for CCS BOs and the non-IOVA fallback.

No users yet; xe_bo_move() is wired to this path in a later patch.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8
---
 drivers/gpu/drm/xe/xe_migrate.c | 307 ++++++++++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_migrate.h |   8 +
 2 files changed, 296 insertions(+), 19 deletions(-)
drivers/gpu/drm/xe/xe_migrate.c +288 -19
index 666c715a9067..e1dc2fb5c5cb 100644
@@ -811,6 +811,64 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 		emit_xy_fast_copy(gt, bb, src_ofs, dst_ofs, size, pitch);
 }
 
+/*
+ * xe_migrate_count_changed_pages() - Count non-borrowed pages in a defrag chunk.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @npages: Number of pages in the L0 chunk.
+ *
+ * A defragmentation move may harvest already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) some of the same physical pages.
+ * Count the pages that actually differ (src page != dst page) within the chunk.
+ *
+ * Return: The number of non-borrowed (changed) pages.
+ */
+static u32 xe_migrate_count_changed_pages(struct ttm_tt *src_tt,
+					  struct ttm_tt *dst_tt,
+					  pgoff_t start_page, u32 npages)
+{
+	u32 i, nchg = 0;
+
+	for (i = 0; i < npages; i++)
+		if (src_tt->pages[start_page + i] !=
+		    dst_tt->pages[start_page + i])
+			nchg++;
+
+	return nchg;
+}
+
+/*
+ * xe_migrate_leading_borrowed_pages() - Count the run of borrowed pages at the
+ * head of the remaining copy.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of the next page to copy within the tt page arrays.
+ * @max_pages: Pages left to copy.
+ *
+ * A defragmentation move borrows the already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) the same physical pages for the
+ * unchanged majority of a large object. Those pages alias on both the source
+ * and destination GPU mappings and need no copy. Count how many consecutive
+ * pages starting at @start_page are borrowed so the caller can advance past the
+ * whole run at once instead of rediscovering it one L0 chunk at a time.
+ *
+ * Return: The number of leading borrowed (src page == dst page) pages, capped
+ * at @max_pages.
+ */
+static u32 xe_migrate_leading_borrowed_pages(struct ttm_tt *src_tt,
+					     struct ttm_tt *dst_tt,
+					     pgoff_t start_page, u32 max_pages)
+{
+	u32 n = 0;
+
+	while (n < max_pages &&
+	       src_tt->pages[start_page + n] == dst_tt->pages[start_page + n])
+		n++;
+
+	return n;
+}
+
 /*
  * emit_copy_chunks() - Emit the data blits for one defrag L0 chunk, skipping
  * borrowed pages.
@@ -822,6 +880,9 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
  * @src_tt: Source (old) tt providing the page pointers.
  * @dst_tt: Destination (new) tt providing the page pointers.
  * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @pack_dst: If true the destination only maps the changed pages packed
+ *	      contiguously, so the destination offset advances by changed-page
+ *	      runs instead of tracking the source page index.
  *
  * A defragmentation move may harvest already beneficial-order chunks from the
  * old tt, so the new tt references (borrows) some of the same physical pages.
@@ -830,16 +891,21 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
  * only blit runs of pages that actually differ (src page != dst page),
  * coalescing contiguous to-copy pages into a single blit.
  *
+ * When @pack_dst is set the source pages are read from their natural offsets
+ * (@src_L0_ofs + page index) while the destination pages were packed into a
+ * contiguous prefix, so the destination offset is a running counter that only
+ * advances for emitted (changed) runs.
+ *
  * Return: The number of blits emitted (or that would be emitted when @bb is
  * NULL).
  */
 static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 			    u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
 			    struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
-			    pgoff_t start_page)
+			    pgoff_t start_page, bool pack_dst)
 {
 	u32 npages = src_L0 >> PAGE_SHIFT;
-	u32 i = 0, nblits = 0;
+	u32 i = 0, pack = 0, nblits = 0;
 
 	while (i < npages) {
 		u32 run = 0;
@@ -859,9 +925,71 @@ static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 
 		if (bb)
 			emit_copy(gt, bb, src_L0_ofs + (u64)i * PAGE_SIZE,
-				  dst_L0_ofs + (u64)i * PAGE_SIZE,
+				  dst_L0_ofs +
+				  (u64)(pack_dst ? pack : i) * PAGE_SIZE,
 				  run * PAGE_SIZE, XE_PAGE_SIZE);
 		nblits++;
+		pack += run;
+		i += run;
+	}
+
+	return nblits;
+}
+
+/*
+ * emit_ccs_copy_chunks() - Emit the indirect CCS aux copies for one packed
+ * defrag L0 chunk, skipping borrowed pages.
+ * @gt: The GT.
+ * @bb: The batch buffer to emit into, or NULL to only count the copies needed.
+ * @src_L0_ofs: GPU offset of the source pages mapped (comp PAT) for this chunk.
+ * @dst_L0_ofs: GPU offset of the destination pages mapped (comp PAT) for this
+ *	        chunk (the packed changed-page prefix).
+ * @src_L0: Byte size of the L0 chunk (page aligned).
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ *
+ * Companion to emit_copy_chunks() for the packed defrag CCS pass. The flat-CCS
+ * aux state is migrated indirectly through the data-page mappings, so it
+ * follows the physical page regardless of the (packed) GPU VA layout. Borrowed
+ * pages alias the same physical page on both sides, so their aux is already
+ * identical and is skipped; only the changed runs are copied, from the source
+ * at its natural offset to the destination at its packed offset.
+ *
+ * Return: The number of CCS copies emitted (or that would be emitted when @bb
+ * is NULL).
+ */
+static u32 emit_ccs_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
+				u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
+				struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
+				pgoff_t start_page)
+{
+	u32 npages = src_L0 >> PAGE_SHIFT;
+	u32 i = 0, pack = 0, nblits = 0;
+
+	while (i < npages) {
+		u32 run = 0;
+
+		/* Skip a run of borrowed pages (src and dst alias). */
+		if (src_tt->pages[start_page + i] ==
+		    dst_tt->pages[start_page + i]) {
+			i++;
+			continue;
+		}
+
+		/* Coalesce a run of pages whose aux must be migrated. */
+		while (i + run < npages &&
+		       src_tt->pages[start_page + i + run] !=
+		       dst_tt->pages[start_page + i + run])
+			run++;
+
+		if (bb)
+			emit_copy_ccs(gt, bb,
+				      dst_L0_ofs + (u64)pack * PAGE_SIZE, true,
+				      src_L0_ofs + (u64)i * PAGE_SIZE, true,
+				      run * PAGE_SIZE);
+		nblits++;
+		pack += run;
 		i += run;
 	}
 
@@ -949,6 +1077,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 					   struct ttm_resource *src,
 					   struct ttm_resource *dst,
 					   struct ttm_tt *src_tt,
+					   pgoff_t dst_packed_pages,
 					   struct xe_migrate_copy_flags flags)
 {
 	struct xe_gt *gt = m->tile->primary_gt;
@@ -981,6 +1110,23 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	 */
 	struct ttm_tt *dst_tt = dst_bo->ttm.ttm;
 	bool skip_borrowed = flags.defrag_copy && src_tt && dst_tt;
+	/*
+	 * Defrag-IOVA copy: the destination tt only maps the changed
+	 * (non-borrowed) pages, packed contiguously into the first
+	 * @dst_packed_pages pages of its IOVA. The source is read from the old
+	 * tt at natural offsets and each changed run is blitted into the packed
+	 * destination prefix.
+	 */
+	bool pack_dst = dst_packed_pages != 0;
+	/*
+	 * Packed defrag CCS pass: migrate the flat-CCS aux state of the changed
+	 * pages only, from the source at its natural offset to the destination
+	 * packed prefix. Borrowed pages share the physical page (and thus its
+	 * aux) on both sides, so they are skipped exactly like the data pass.
+	 */
+	bool ccs_pack = flags.defrag_ccs && pack_dst && src_tt && dst_tt;
+	/* Per-run, borrowed-skipping defrag emission (data or packed CCS). */
+	bool defrag_runs = skip_borrowed || ccs_pack;
 
 	/*
 	 * For decompression operation, always use the compression PAT index.
@@ -1009,7 +1155,9 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0,
+				pack_dst ? (u64)dst_packed_pages << PAGE_SHIFT :
+				size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
@@ -1030,29 +1178,86 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		u32 pte_flags;
 		pgoff_t start_page = (xe_bo_size(src_bo) - size) >> PAGE_SHIFT;
 		u32 ndata_blits = 1;
+		u32 nchg = 0;
+		u64 dst_size;
 
 		bool usm = xe->info.has_usm;
 		u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE;
 
+		/*
+		 * Defrag fast-forward: the new (destination) tt borrows the
+		 * unchanged beneficial-order chunks from the old tt, so for a
+		 * large object the bulk of the copy is borrowed pages that alias
+		 * the same memory on both sides. Skip the entire leading
+		 * borrowed run in a single cursor stride rather than discovering
+		 * it one (8 MB) L0 chunk at a time - each such pass would
+		 * otherwise pay xe_migrate_res_sizes() + pte_update_size() + a
+		 * full chunk walk only to copy nothing. The non-vram tt cursors
+		 * are normally advanced by emit_pte(); replicate that with
+		 * xe_res_next(). In pack_dst mode the destination only maps the
+		 * changed pages, so its cursor must not advance for borrowed
+		 * pages.
+		 */
+		if (defrag_runs) {
+			u32 brun = xe_migrate_leading_borrowed_pages(src_tt,
+								     dst_tt,
+								     start_page,
+								     size >> PAGE_SHIFT);
+
+			if (brun) {
+				u64 brun_bytes = (u64)brun << PAGE_SHIFT;
+
+				xe_res_next(&src_it, brun_bytes);
+				if (!pack_dst)
+					xe_res_next(&dst_it, brun_bytes);
+				size -= brun_bytes;
+				continue;
+			}
+		}
+
 		src_L0 = xe_migrate_res_sizes(m, &src_it);
 		dst_L0 = xe_migrate_res_sizes(m, &dst_it);
 
 		drm_dbg(&xe->drm, "Pass %u, sizes: %llu & %llu\n",
 			pass++, src_L0, dst_L0);
 
-		src_L0 = min(src_L0, dst_L0);
+		/*
+		 * In pack_dst mode the destination is the (smaller) packed
+		 * prefix, so it must not clamp the source chunk size; the
+		 * source drives the walk and the destination is sized
+		 * independently from the changed-page count below.
+		 */
+		if (!pack_dst)
+			src_L0 = min(src_L0, dst_L0);
 
 		pte_flags = src_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 		pte_flags |= use_comp_pat ? PTE_UPDATE_FLAG_IS_COMP_PTE : 0;
 		batch_size += pte_update_size(m, pte_flags, src, &src_it, &src_L0,
 					      &src_L0_ofs, &src_L0_pt, 0, 0,
 					      avail_pts);
+		/*
+		 * The source chunk size may have been clamped above, so derive
+		 * the packed destination size from the changed-page count of
+		 * the final chunk.
+		 */
+		if (pack_dst) {
+			nchg = xe_migrate_count_changed_pages(src_tt, dst_tt,
+							      start_page,
+							      src_L0 >> PAGE_SHIFT);
+			dst_size = (u64)nchg << PAGE_SHIFT;
+		} else {
+			dst_size = src_L0;
+		}
 		if (flags.copy_only_ccs) {
 			dst_L0_ofs = src_L0_ofs;
+		} else if (pack_dst && !nchg) {
+			/* All borrowed: no destination PTEs for this chunk. */
+			dst_L0_ofs = 0;
+			dst_L0_pt = 0;
 		} else {
 			pte_flags = dst_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 			batch_size += pte_update_size(m, pte_flags, dst,
-						      &dst_it, &src_L0,
+						      &dst_it, &dst_size,
 						      &dst_L0_ofs, &dst_L0_pt,
 						      0, avail_pts, avail_pts);
 		}
@@ -1072,26 +1277,31 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		 * is split into one command per run of non-borrowed pages, so
 		 * size for the number of runs actually needed.
 		 */
-		if (skip_borrowed)
+		if (defrag_runs)
 			ndata_blits = emit_copy_chunks(gt, NULL, 0, 0, src_L0,
-						       src_tt, dst_tt, start_page);
+						       src_tt, dst_tt, start_page,
+						       pack_dst);
 
 		/*
 		 * If every page in this chunk was borrowed there is nothing to
 		 * copy: don't build a batch or issue any GPU commands, just
 		 * advance the cursors past the chunk. The non-vram tt cursors
-		 * would otherwise be advanced by emit_pte().
+		 * would otherwise be advanced by emit_pte(). In pack_dst mode
+		 * the destination consumed no packed pages, so leave its cursor
+		 * untouched.
 		 */
-		if (skip_borrowed && !ndata_blits) {
+		if (defrag_runs && !ndata_blits) {
 			xe_res_next(&src_it, src_L0);
-			xe_res_next(&dst_it, src_L0);
+			if (!pack_dst)
+				xe_res_next(&dst_it, src_L0);
 			size -= src_L0;
 			continue;
 		}
 
 		batch_size += ((flags.copy_only_ccs || flags.defrag_ccs) ? 0 :
 			       ndata_blits * emit_copy_cmd_len(xe)) +
-			((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));
+			(ccs_pack ? ndata_blits * EMIT_COPY_CCS_DW :
+			 (needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));
 
 		bb = xe_bb_new(gt, batch_size, usm);
 		if (IS_ERR(bb)) {
@@ -1111,7 +1321,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		else if (!flags.copy_only_ccs)
 			emit_pte(m, bb, dst_L0_pt, dst_is_vram,
 				 copy_system_ccs || flags.defrag_ccs,
-				 &dst_it, src_L0, dst);
+				 &dst_it, dst_size, dst);
 
 		if (copy_system_ccs)
 			emit_pte(m, bb, ccs_pt, false, false, &ccs_it, ccs_size, src);
@@ -1121,16 +1331,22 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 
 		if (skip_borrowed)
 			emit_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0,
-					 src_tt, dst_tt, start_page);
+					 src_tt, dst_tt, start_page, pack_dst);
 		else if (!flags.copy_only_ccs && !flags.defrag_ccs)
 			emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE);
 
-		if (needs_ccs_emit)
+		if (ccs_pack) {
+			if (emit_ccs_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs,
+						 src_L0, src_tt, dst_tt,
+						 start_page))
+				flush_flags = MI_FLUSH_DW_CCS;
+		} else if (needs_ccs_emit) {
 			flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs,
 							  IS_DGFX(xe) ? src_is_vram : src_is_pltt,
 							  dst_L0_ofs,
 							  IS_DGFX(xe) ? dst_is_vram : dst_is_pltt,
 							  src_L0, ccs_ofs, copy_ccs);
+		}
 
 		job = xe_bb_create_migration_job(m->q, bb,
 						 xe_migrate_batch_base(m, usm),
@@ -1218,7 +1434,7 @@ struct dma_fence *xe_migrate_copy(struct xe_migrate *m,
 				  struct ttm_resource *dst,
 				  bool copy_only_ccs)
 {
-	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL,
+	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .copy_only_ccs = copy_only_ccs,
 				 });
@@ -1267,7 +1483,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 bool need_ccs)
 {
 	struct dma_fence *fence2, *fence =
-		__xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				  (struct xe_migrate_copy_flags) {
 					 .defrag_copy = true,
 					 });
@@ -1275,7 +1491,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	if (IS_ERR(fence) || !need_ccs)
 		return fence;
 
-	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				   (struct xe_migrate_copy_flags) {
 				   .defrag_ccs = true,
 				   });
@@ -1286,6 +1502,59 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	return fence2;
 }
 
+/**
+ * xe_migrate_copy_defrag_iova() - Copy a defrag move into a packed IOVA dst.
+ * @m: The migration context.
+ * @bo: The buffer object being defragmented.
+ * @src: The source (old) TTM resource.
+ * @dst: The destination (new) TTM resource.
+ * @src_tt: The stashed source (old) tt providing the page pointers, fully
+ *	    IOVA-mapped at natural offsets.
+ * @dst_packed_pages: Number of changed pages packed into the destination tt's
+ *	    IOVA prefix by xe_tt_map_iova_copy().
+ * @need_ccs: Whether the BO has flat-CCS aux state that must be migrated too.
+ *
+ * Defrag copy variant for IOVA-mapped BOs where the destination only maps the
+ * changed (non-borrowed) pages, packed contiguously into the first
+ * @dst_packed_pages of its IOVA. The source is read from the old tt at its
+ * natural offsets and each changed run is blitted into the packed destination
+ * prefix. When @need_ccs is set a second pass migrates the flat-CCS aux state
+ * of the same changed pages indirectly through their (packed) data mappings;
+ * borrowed pages share the physical page and its aux, so only the changed runs
+ * are touched.
+ *
+ * Return: Pointer to a dma_fence representing the last copy batch, or an error
+ * pointer on failure.
+ */
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs)
+{
+	struct dma_fence *fence2, *fence =
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, dst_packed_pages,
+				  (struct xe_migrate_copy_flags) {
+					 .defrag_copy = true,
+				 });
+
+	if (IS_ERR(fence) || !need_ccs)
+		return fence;
+
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+				   dst_packed_pages,
+				   (struct xe_migrate_copy_flags) {
+					 .defrag_ccs = true,
+				   });
+	if (IS_ERR(fence2))
+		dma_fence_wait(fence, false);
+	dma_fence_put(fence);
+
+	return fence2;
+}
+
 /**
  * xe_migrate_resolve() - Resolve and decompress a buffer object if required.
  * @m: The migrate context
@@ -1301,7 +1570,7 @@ struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res)
 {
-	return __xe_migrate_copy(m, bo, bo, res, res, NULL,
+	return __xe_migrate_copy(m, bo, bo, res, res, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .is_vram_resolve = true,
 				 });
drivers/gpu/drm/xe/xe_migrate.h +8 -0
index 4094ea1c19c1..da066cdf01bf 100644
@@ -141,6 +141,14 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 struct ttm_tt *src_tt,
 					 bool need_ccs);
 
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs);
+
 struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res);
Signature · 1 line
2.34.1
#125 lore raw

Re: [PATCH v3 31/33] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Critical] Overloading dst_packed_pages == 0 to mean 'not a packed destination' causes out-of-bounds cursor access and memory corruption when a packed defrag move has exactly zero changed pages.

Pre-existing issues:
- [Critical] During a TT-to-TT CCS defrag pass on DGFX systems, an uninitialized stack variable (ccs_ofs) is passed to the GPU blitter, causing memory corruption. --

--- Patch [31]: [PATCH v3 31/33] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies --- commit f45aa97c2f11618672e201ef92ad9ef7898d5e9c
Author: Matthew Brost <[email protected]>

drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies

This commit introduces a packed-destination mode in __xe_migrate_copy() for defrag moves with IOVA enabled. It adds a dst_packed_pages argument to handle asymmetric geometries where the destination only exposes changed pages. The destination PTE update size is calculated based on the changed-page count.

Quoted 4 lines
drivers/gpu/drm/xe/xe_migrate.c +0 -0
index e9f050eab8871..8eb5e331d5156 100644

[ ... ]

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1186

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +3 -1
@@ -1033,7 +1179,9 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0,
+				pack_dst ? (u64)dst_packed_pages << PAGE_SHIFT :
+				size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);

[Severity: Critical]
Does overloading dst_packed_pages == 0 to mean 'not a packed destination' cause issues when a packed defrag move has exactly zero changed pages?

If a defragmentation move executes under memory pressure and fails to allocate any new pages, it results in 100% borrowed pages. The IOVA mapping function legitimately maps 0 pages, so xe_migrate_copy_defrag_iova() is called with dst_packed_pages = 0.

Because dst_packed_pages == 0, pack_dst becomes false (set earlier in __xe_migrate_copy). This forces the fallback to a full-size non-packed copy, and initializes dst_it requesting the full BO size against a destination IOVA that was packed to size 0.

Can this walk the broken cursor and fetch garbage DMA addresses for the GPU blitter, corrupting memory?

[ ... ]

Quoted 12 lines
drivers/gpu/drm/xe/xe_migrate.c +3 -2
@@ -1145,16 +1355,22 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 
 		if (skip_borrowed)
 			emit_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0,
-					 src_tt, dst_tt, start_page);
+					 src_tt, dst_tt, start_page, pack_dst);
 		else if (!flags.copy_only_ccs && !flags.defrag_ccs)
 			emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE);
 
-		if (needs_ccs_emit)
+		if (ccs_pack) {
+			if (emit_ccs_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs,

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1373

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +5 -0
+						 src_L0, src_tt, dst_tt,
+						 start_page))
+				flush_flags = MI_FLUSH_DW_CCS;
+		} else if (needs_ccs_emit) {
 			flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs,
 							  IS_DGFX(xe) ? src_is_vram : src_is_pltt,
 							  dst_L0_ofs,
 							  IS_DGFX(xe) ? dst_is_vram : dst_is_pltt,
 							  src_L0, ccs_ofs, copy_ccs);
+		}

[Severity: Critical]
This is a pre-existing issue, but can this pass an uninitialized ccs_ofs stack variable to the GPU blitter during a TT-to-TT CCS defrag pass on DGFX systems?

When __xe_migrate_copy() is called with flags.defrag_ccs = true, copy_system_ccs evaluates to false, leaving ccs_ofs uninitialized.

Because !flags.defrag_copy is true, needs_ccs_emit evaluates to true and calls xe_migrate_ccs_copy(). For a TT source on DGFX, IS_DGFX(xe) ? src_is_vram :
src_is_pltt evaluates to false (meaning src_is_indirect is false).

xe_migrate_ccs_copy() then consumes the uninitialized ccs_ofs:

drivers/gpu/drm/xe/xe_migrate.c:xe_migrate_ccs_copy() {
    ...
    } else if (copy_ccs) {
        if (!src_is_indirect)
            src_ofs = ccs_ofs;
    ...
}

Does this cause the uninitialized src_ofs to be emitted into the GPU batch buffer, resulting in memory corruption?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=31
#126 lore raw

Re: [PATCH v3 31/33] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies

In reply to Matthew Brost
Quoted 307 lines · Matthew Brost

A defrag move with IOVA enabled only DMA-maps the changed (non-borrowed) pages of the new tt, packed contiguously into the front of its IOVA reservation (xe_tt_map_iova_copy()). The GPU copy therefore has an asymmetric geometry: the source is read from the old tt at its natural page offsets (via the old tt's full IOVA), while the destination only exposes the changed pages, packed.

Teach __xe_migrate_copy() about this packed-destination mode:

- Add a @dst_packed_pages argument. When non-zero the destination tt cursor walks just that many packed pages and the source (not the destination) drives the chunk size, so the smaller packed prefix never clamps the source walk.

- Size the destination PTE update for each chunk from the changed-page count (xe_migrate_count_changed_pages()) rather than the full chunk, and skip the destination PTEs entirely for an all-borrowed chunk.

- Extend emit_copy_chunks() with a @pack_dst flag so each emitted run is blitted from its natural source offset into a running packed destination offset that only advances for changed runs. The all-borrowed skip leaves the packed destination cursor untouched.

Expose this via xe_migrate_copy_defrag_iova(). It is only valid when the BO needs no CCS pass, since the separate CCS pass migrates aux state for all pages and would require the full destination mapping. The existing xe_migrate_copy_defrag() (full destination mapping) remains the path for CCS BOs and the non-IOVA fallback.

No users yet; xe_bo_move() is wired to this path in a later patch.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---
drivers/gpu/drm/xe/xe_migrate.c | 307 ++++++++++++++++++++++++++++++--
drivers/gpu/drm/xe/xe_migrate.h | 8 +

2 files changed, 296 insertions(+), 19 deletions(-)
drivers/gpu/drm/xe/xe_migrate.c +181 -4
index 666c715a9067..e1dc2fb5c5cb 100644
@@ -811,6 +811,64 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 		emit_xy_fast_copy(gt, bb, src_ofs, dst_ofs, size, pitch);
 }
 
+/*
+ * xe_migrate_count_changed_pages() - Count non-borrowed pages in a defrag chunk.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @npages: Number of pages in the L0 chunk.
+ *
+ * A defragmentation move may harvest already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) some of the same physical pages.
+ * Count the pages that actually differ (src page != dst page) within the chunk.
+ *
+ * Return: The number of non-borrowed (changed) pages.
+ */
+static u32 xe_migrate_count_changed_pages(struct ttm_tt *src_tt,
+					  struct ttm_tt *dst_tt,
+					  pgoff_t start_page, u32 npages)
+{
+	u32 i, nchg = 0;
+
+	for (i = 0; i < npages; i++)
+		if (src_tt->pages[start_page + i] !=
+		    dst_tt->pages[start_page + i])
+			nchg++;
+
+	return nchg;
+}
+
+/*
+ * xe_migrate_leading_borrowed_pages() - Count the run of borrowed pages at the
+ * head of the remaining copy.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of the next page to copy within the tt page arrays.
+ * @max_pages: Pages left to copy.
+ *
+ * A defragmentation move borrows the already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) the same physical pages for the
+ * unchanged majority of a large object. Those pages alias on both the source
+ * and destination GPU mappings and need no copy. Count how many consecutive
+ * pages starting at @start_page are borrowed so the caller can advance past the
+ * whole run at once instead of rediscovering it one L0 chunk at a time.
+ *
+ * Return: The number of leading borrowed (src page == dst page) pages, capped
+ * at @max_pages.
+ */
+static u32 xe_migrate_leading_borrowed_pages(struct ttm_tt *src_tt,
+					     struct ttm_tt *dst_tt,
+					     pgoff_t start_page, u32 max_pages)
+{
+	u32 n = 0;
+
+	while (n < max_pages &&
+	       src_tt->pages[start_page + n] == dst_tt->pages[start_page + n])
+		n++;
+
+	return n;
+}
+
 /*
 * emit_copy_chunks() - Emit the data blits for one defrag L0 chunk, skipping
 * borrowed pages.
@@ -822,6 +880,9 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 * @src_tt: Source (old) tt providing the page pointers.
 * @dst_tt: Destination (new) tt providing the page pointers.
 * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @pack_dst: If true the destination only maps the changed pages packed
+ *	      contiguously, so the destination offset advances by changed-page
+ *	      runs instead of tracking the source page index.
 *
 * A defragmentation move may harvest already beneficial-order chunks from the
 * old tt, so the new tt references (borrows) some of the same physical pages.
@@ -830,16 +891,21 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 * only blit runs of pages that actually differ (src page != dst page),
 * coalescing contiguous to-copy pages into a single blit.
 *
+ * When @pack_dst is set the source pages are read from their natural offsets
+ * (@src_L0_ofs + page index) while the destination pages were packed into a
+ * contiguous prefix, so the destination offset is a running counter that only
+ * advances for emitted (changed) runs.
+ *
 * Return: The number of blits emitted (or that would be emitted when @bb is
 * NULL).
 */
 static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 			    u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
 			    struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
-			    pgoff_t start_page)
+			    pgoff_t start_page, bool pack_dst)
 {
 	u32 npages = src_L0 >> PAGE_SHIFT;
-	u32 i = 0, nblits = 0;
+	u32 i = 0, pack = 0, nblits = 0;
 
 	while (i < npages) {
 		u32 run = 0;
@@ -859,9 +925,71 @@ static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 
 		if (bb)
 			emit_copy(gt, bb, src_L0_ofs + (u64)i * PAGE_SIZE,
-				  dst_L0_ofs + (u64)i * PAGE_SIZE,
+				  dst_L0_ofs +
+				  (u64)(pack_dst ? pack : i) * PAGE_SIZE,
 				  run * PAGE_SIZE, XE_PAGE_SIZE);
 		nblits++;
+		pack += run;
+		i += run;
+	}
+
+	return nblits;
+}
+
+/*
+ * emit_ccs_copy_chunks() - Emit the indirect CCS aux copies for one packed
+ * defrag L0 chunk, skipping borrowed pages.
+ * @gt: The GT.
+ * @bb: The batch buffer to emit into, or NULL to only count the copies needed.
+ * @src_L0_ofs: GPU offset of the source pages mapped (comp PAT) for this chunk.
+ * @dst_L0_ofs: GPU offset of the destination pages mapped (comp PAT) for this
+ *	        chunk (the packed changed-page prefix).
+ * @src_L0: Byte size of the L0 chunk (page aligned).
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ *
+ * Companion to emit_copy_chunks() for the packed defrag CCS pass. The flat-CCS
+ * aux state is migrated indirectly through the data-page mappings, so it
+ * follows the physical page regardless of the (packed) GPU VA layout. Borrowed
+ * pages alias the same physical page on both sides, so their aux is already
+ * identical and is skipped; only the changed runs are copied, from the source
+ * at its natural offset to the destination at its packed offset.
+ *
+ * Return: The number of CCS copies emitted (or that would be emitted when @bb
+ * is NULL).
+ */
+static u32 emit_ccs_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
+				u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
+				struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
+				pgoff_t start_page)
+{
+	u32 npages = src_L0 >> PAGE_SHIFT;
+	u32 i = 0, pack = 0, nblits = 0;
+
+	while (i < npages) {
+		u32 run = 0;
+
+		/* Skip a run of borrowed pages (src and dst alias). */
+		if (src_tt->pages[start_page + i] ==
+		    dst_tt->pages[start_page + i]) {
+			i++;
+			continue;
+		}
+
+		/* Coalesce a run of pages whose aux must be migrated. */
+		while (i + run < npages &&
+		       src_tt->pages[start_page + i + run] !=
+		       dst_tt->pages[start_page + i + run])
+			run++;
+
+		if (bb)
+			emit_copy_ccs(gt, bb,
+				      dst_L0_ofs + (u64)pack * PAGE_SIZE, true,
+				      src_L0_ofs + (u64)i * PAGE_SIZE, true,
+				      run * PAGE_SIZE);
+		nblits++;
+		pack += run;
 		i += run;
 	}
 
@@ -949,6 +1077,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 					   struct ttm_resource *src,
 					   struct ttm_resource *dst,
 					   struct ttm_tt *src_tt,
+					   pgoff_t dst_packed_pages,
 					   struct xe_migrate_copy_flags flags)
 {
 	struct xe_gt *gt = m->tile->primary_gt;
@@ -981,6 +1110,23 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	 */
 	struct ttm_tt *dst_tt = dst_bo->ttm.ttm;
 	bool skip_borrowed = flags.defrag_copy && src_tt && dst_tt;
+	/*
+	 * Defrag-IOVA copy: the destination tt only maps the changed
+	 * (non-borrowed) pages, packed contiguously into the first
+	 * @dst_packed_pages pages of its IOVA. The source is read from the old
+	 * tt at natural offsets and each changed run is blitted into the packed
+	 * destination prefix.
+	 */
+	bool pack_dst = dst_packed_pages != 0;
+	/*
+	 * Packed defrag CCS pass: migrate the flat-CCS aux state of the changed
+	 * pages only, from the source at its natural offset to the destination
+	 * packed prefix. Borrowed pages share the physical page (and thus its
+	 * aux) on both sides, so they are skipped exactly like the data pass.
+	 */
+	bool ccs_pack = flags.defrag_ccs && pack_dst && src_tt && dst_tt;
+	/* Per-run, borrowed-skipping defrag emission (data or packed CCS). */
+	bool defrag_runs = skip_borrowed || ccs_pack;
 
 	/*
 	 * For decompression operation, always use the compression PAT index.
@@ -1009,7 +1155,9 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0,
+				pack_dst ? (u64)dst_packed_pages << PAGE_SHIFT :
+				size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
@@ -1030,29 +1178,86 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		u32 pte_flags;
 		pgoff_t start_page = (xe_bo_size(src_bo) - size) >> PAGE_SHIFT;
 		u32 ndata_blits = 1;
+		u32 nchg = 0;
+		u64 dst_size;
 
 		bool usm = xe->info.has_usm;
 		u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE;
 
+		/*
+		 * Defrag fast-forward: the new (destination) tt borrows the
+		 * unchanged beneficial-order chunks from the old tt, so for a
+		 * large object the bulk of the copy is borrowed pages that alias
+		 * the same memory on both sides. Skip the entire leading
+		 * borrowed run in a single cursor stride rather than discovering
+		 * it one (8 MB) L0 chunk at a time - each such pass would
+		 * otherwise pay xe_migrate_res_sizes() + pte_update_size() + a
+		 * full chunk walk only to copy nothing. The non-vram tt cursors
+		 * are normally advanced by emit_pte(); replicate that with
+		 * xe_res_next(). In pack_dst mode the destination only maps the
+		 * changed pages, so its cursor must not advance for borrowed
+		 * pages.
+		 */
+		if (defrag_runs) {
+			u32 brun = xe_migrate_leading_borrowed_pages(src_tt,
+								     dst_tt,
+								     start_page,
+								     size >> PAGE_SHIFT);
+
+			if (brun) {
+				u64 brun_bytes = (u64)brun << PAGE_SHIFT;
+
+				xe_res_next(&src_it, brun_bytes);
+				if (!pack_dst)
+					xe_res_next(&dst_it, brun_bytes);
+				size -= brun_bytes;

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1223

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +4 -0
+				continue;
+			}
+		}
+
 		src_L0 = xe_migrate_res_sizes(m, &src_it);
 		dst_L0 = xe_migrate_res_sizes(m, &dst_it);
 
 		drm_dbg(&xe->drm, "Pass %u, sizes: %llu & %llu\n",
 			pass++, src_L0, dst_L0);
 
Assisted-by LLM

With above 'continue', this drm_dbg never gets printed, is that fine?

Quoted 54 lines
drivers/gpu/drm/xe/xe_migrate.c +29 -4
-		src_L0 = min(src_L0, dst_L0);
+		/*
+		 * In pack_dst mode the destination is the (smaller) packed
+		 * prefix, so it must not clamp the source chunk size; the
+		 * source drives the walk and the destination is sized
+		 * independently from the changed-page count below.
+		 */
+		if (!pack_dst)
+			src_L0 = min(src_L0, dst_L0);
 
 		pte_flags = src_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 		pte_flags |= use_comp_pat ? PTE_UPDATE_FLAG_IS_COMP_PTE : 0;
 		batch_size += pte_update_size(m, pte_flags, src, &src_it, &src_L0,
 					      &src_L0_ofs, &src_L0_pt, 0, 0,
 					      avail_pts);
+		/*
+		 * The source chunk size may have been clamped above, so derive
+		 * the packed destination size from the changed-page count of
+		 * the final chunk.
+		 */
+		if (pack_dst) {
+			nchg = xe_migrate_count_changed_pages(src_tt, dst_tt,
+							      start_page,
+							      src_L0 >> PAGE_SHIFT);
+			dst_size = (u64)nchg << PAGE_SHIFT;
+		} else {
+			dst_size = src_L0;
+		}
 		if (flags.copy_only_ccs) {
 			dst_L0_ofs = src_L0_ofs;
+		} else if (pack_dst && !nchg) {
+			/* All borrowed: no destination PTEs for this chunk. */
+			dst_L0_ofs = 0;
+			dst_L0_pt = 0;
 		} else {
 			pte_flags = dst_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 			batch_size += pte_update_size(m, pte_flags, dst,
-						      &dst_it, &src_L0,
+						      &dst_it, &dst_size,
 						      &dst_L0_ofs, &dst_L0_pt,
 						      0, avail_pts, avail_pts);
 		}
@@ -1072,26 +1277,31 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		 * is split into one command per run of non-borrowed pages, so
 		 * size for the number of runs actually needed.
 		 */
-		if (skip_borrowed)
+		if (defrag_runs)
 			ndata_blits = emit_copy_chunks(gt, NULL, 0, 0, src_L0,
-						       src_tt, dst_tt, start_page);
+						       src_tt, dst_tt, start_page,
+						       pack_dst);
 
 		/*

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1293

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +4 -2
 		 * If every page in this chunk was borrowed there is nothing to
 		 * copy: don't build a batch or issue any GPU commands, just
 		 * advance the cursors past the chunk. The non-vram tt cursors
-		 * would otherwise be advanced by emit_pte().
+		 * would otherwise be advanced by emit_pte(). In pack_dst mode
+		 * the destination consumed no packed pages, so leave its cursor
+		 * untouched.
 		 */
-		if (skip_borrowed && !ndata_blits) {
+		if (defrag_runs && !ndata_blits) {

LLM says this 'if' clause is dead code now as defrag_runs will always have non-zero ndata_blits.

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1304

Quoted 13 lines
drivers/gpu/drm/xe/xe_migrate.c +4 -2
 			xe_res_next(&src_it, src_L0);
-			xe_res_next(&dst_it, src_L0);
+			if (!pack_dst)
+				xe_res_next(&dst_it, src_L0);
 			size -= src_L0;
 			continue;
 		}
 
 		batch_size += ((flags.copy_only_ccs || flags.defrag_ccs) ? 0 :
 			       ndata_blits * emit_copy_cmd_len(xe)) +
-			((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));
+			(ccs_pack ? ndata_blits * EMIT_COPY_CCS_DW :
+			 (needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));

We are using ndata_blits returned from emit_copy_chunks to find calculate the batch size for ccs part. Though it might work, shouldn't we be getting that from emit_ccs_copy_chunks() with 'bb' set to NULL?

Niranjana

Quoted 155 lines
drivers/gpu/drm/xe/xe_migrate.c +66 -7
 		bb = xe_bb_new(gt, batch_size, usm);
 		if (IS_ERR(bb)) {
@@ -1111,7 +1321,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		else if (!flags.copy_only_ccs)
 			emit_pte(m, bb, dst_L0_pt, dst_is_vram,
 				 copy_system_ccs || flags.defrag_ccs,
-				 &dst_it, src_L0, dst);
+				 &dst_it, dst_size, dst);
 
 		if (copy_system_ccs)
 			emit_pte(m, bb, ccs_pt, false, false, &ccs_it, ccs_size, src);
@@ -1121,16 +1331,22 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 
 		if (skip_borrowed)
 			emit_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0,
-					 src_tt, dst_tt, start_page);
+					 src_tt, dst_tt, start_page, pack_dst);
 		else if (!flags.copy_only_ccs && !flags.defrag_ccs)
 			emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE);
 
-		if (needs_ccs_emit)
+		if (ccs_pack) {
+			if (emit_ccs_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs,
+						 src_L0, src_tt, dst_tt,
+						 start_page))
+				flush_flags = MI_FLUSH_DW_CCS;
+		} else if (needs_ccs_emit) {
 			flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs,
 							  IS_DGFX(xe) ? src_is_vram : src_is_pltt,
 							  dst_L0_ofs,
 							  IS_DGFX(xe) ? dst_is_vram : dst_is_pltt,
 							  src_L0, ccs_ofs, copy_ccs);
+		}
 
 		job = xe_bb_create_migration_job(m->q, bb,
 						 xe_migrate_batch_base(m, usm),
@@ -1218,7 +1434,7 @@ struct dma_fence *xe_migrate_copy(struct xe_migrate *m,
 				  struct ttm_resource *dst,
 				  bool copy_only_ccs)
 {
-	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL,
+	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .copy_only_ccs = copy_only_ccs,
 				 });
@@ -1267,7 +1483,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 bool need_ccs)
 {
 	struct dma_fence *fence2, *fence =
-		__xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				  (struct xe_migrate_copy_flags) {
 					 .defrag_copy = true,
 					 });
@@ -1275,7 +1491,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	if (IS_ERR(fence) || !need_ccs)
 		return fence;
 
-	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				   (struct xe_migrate_copy_flags) {
 				   .defrag_ccs = true,
 				   });
@@ -1286,6 +1502,59 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	return fence2;
 }
 
+/**
+ * xe_migrate_copy_defrag_iova() - Copy a defrag move into a packed IOVA dst.
+ * @m: The migration context.
+ * @bo: The buffer object being defragmented.
+ * @src: The source (old) TTM resource.
+ * @dst: The destination (new) TTM resource.
+ * @src_tt: The stashed source (old) tt providing the page pointers, fully
+ *	    IOVA-mapped at natural offsets.
+ * @dst_packed_pages: Number of changed pages packed into the destination tt's
+ *	    IOVA prefix by xe_tt_map_iova_copy().
+ * @need_ccs: Whether the BO has flat-CCS aux state that must be migrated too.
+ *
+ * Defrag copy variant for IOVA-mapped BOs where the destination only maps the
+ * changed (non-borrowed) pages, packed contiguously into the first
+ * @dst_packed_pages of its IOVA. The source is read from the old tt at its
+ * natural offsets and each changed run is blitted into the packed destination
+ * prefix. When @need_ccs is set a second pass migrates the flat-CCS aux state
+ * of the same changed pages indirectly through their (packed) data mappings;
+ * borrowed pages share the physical page and its aux, so only the changed runs
+ * are touched.
+ *
+ * Return: Pointer to a dma_fence representing the last copy batch, or an error
+ * pointer on failure.
+ */
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs)
+{
+	struct dma_fence *fence2, *fence =
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, dst_packed_pages,
+				  (struct xe_migrate_copy_flags) {
+					 .defrag_copy = true,
+				 });
+
+	if (IS_ERR(fence) || !need_ccs)
+		return fence;
+
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+				   dst_packed_pages,
+				   (struct xe_migrate_copy_flags) {
+					 .defrag_ccs = true,
+				   });
+	if (IS_ERR(fence2))
+		dma_fence_wait(fence, false);
+	dma_fence_put(fence);
+
+	return fence2;
+}
+
 /**
 * xe_migrate_resolve() - Resolve and decompress a buffer object if required.
 * @m: The migrate context
@@ -1301,7 +1570,7 @@ struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res)
 {
-	return __xe_migrate_copy(m, bo, bo, res, res, NULL,
+	return __xe_migrate_copy(m, bo, bo, res, res, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .is_vram_resolve = true,
 				 });
drivers/gpu/drm/xe/xe_migrate.h +8 -0
index 4094ea1c19c1..da066cdf01bf 100644
@@ -141,6 +141,14 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 struct ttm_tt *src_tt,
 					 bool need_ccs);
 
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs);
+
 struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res);
Signature · 1 line
2.34.1
author maintainer
#127 lore raw

Re: [PATCH v3 31/33] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies

Quoted 315 lines · Niranjana Vishwanathapura
Quoted 314 lines · Matthew Brost

A defrag move with IOVA enabled only DMA-maps the changed (non-borrowed) pages of the new tt, packed contiguously into the front of its IOVA reservation (xe_tt_map_iova_copy()). The GPU copy therefore has an asymmetric geometry: the source is read from the old tt at its natural page offsets (via the old tt's full IOVA), while the destination only exposes the changed pages, packed.

Teach __xe_migrate_copy() about this packed-destination mode:

- Add a @dst_packed_pages argument. When non-zero the destination tt cursor walks just that many packed pages and the source (not the destination) drives the chunk size, so the smaller packed prefix never clamps the source walk.

- Size the destination PTE update for each chunk from the changed-page count (xe_migrate_count_changed_pages()) rather than the full chunk, and skip the destination PTEs entirely for an all-borrowed chunk.

- Extend emit_copy_chunks() with a @pack_dst flag so each emitted run is blitted from its natural source offset into a running packed destination offset that only advances for changed runs. The all-borrowed skip leaves the packed destination cursor untouched.

Expose this via xe_migrate_copy_defrag_iova(). It is only valid when the BO needs no CCS pass, since the separate CCS pass migrates aux state for all pages and would require the full destination mapping. The existing xe_migrate_copy_defrag() (full destination mapping) remains the path for CCS BOs and the non-IOVA fallback.

No users yet; xe_bo_move() is wired to this path in a later patch.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---
drivers/gpu/drm/xe/xe_migrate.c | 307 ++++++++++++++++++++++++++++++--
drivers/gpu/drm/xe/xe_migrate.h | 8 +

2 files changed, 296 insertions(+), 19 deletions(-)
drivers/gpu/drm/xe/xe_migrate.c +185 -4
index 666c715a9067..e1dc2fb5c5cb 100644
@@ -811,6 +811,64 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 		emit_xy_fast_copy(gt, bb, src_ofs, dst_ofs, size, pitch);
 }
 
+/*
+ * xe_migrate_count_changed_pages() - Count non-borrowed pages in a defrag chunk.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @npages: Number of pages in the L0 chunk.
+ *
+ * A defragmentation move may harvest already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) some of the same physical pages.
+ * Count the pages that actually differ (src page != dst page) within the chunk.
+ *
+ * Return: The number of non-borrowed (changed) pages.
+ */
+static u32 xe_migrate_count_changed_pages(struct ttm_tt *src_tt,
+					  struct ttm_tt *dst_tt,
+					  pgoff_t start_page, u32 npages)
+{
+	u32 i, nchg = 0;
+
+	for (i = 0; i < npages; i++)
+		if (src_tt->pages[start_page + i] !=
+		    dst_tt->pages[start_page + i])
+			nchg++;
+
+	return nchg;
+}
+
+/*
+ * xe_migrate_leading_borrowed_pages() - Count the run of borrowed pages at the
+ * head of the remaining copy.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of the next page to copy within the tt page arrays.
+ * @max_pages: Pages left to copy.
+ *
+ * A defragmentation move borrows the already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) the same physical pages for the
+ * unchanged majority of a large object. Those pages alias on both the source
+ * and destination GPU mappings and need no copy. Count how many consecutive
+ * pages starting at @start_page are borrowed so the caller can advance past the
+ * whole run at once instead of rediscovering it one L0 chunk at a time.
+ *
+ * Return: The number of leading borrowed (src page == dst page) pages, capped
+ * at @max_pages.
+ */
+static u32 xe_migrate_leading_borrowed_pages(struct ttm_tt *src_tt,
+					     struct ttm_tt *dst_tt,
+					     pgoff_t start_page, u32 max_pages)
+{
+	u32 n = 0;
+
+	while (n < max_pages &&
+	       src_tt->pages[start_page + n] == dst_tt->pages[start_page + n])
+		n++;
+
+	return n;
+}
+
 /*
 * emit_copy_chunks() - Emit the data blits for one defrag L0 chunk, skipping
 * borrowed pages.
@@ -822,6 +880,9 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 * @src_tt: Source (old) tt providing the page pointers.
 * @dst_tt: Destination (new) tt providing the page pointers.
 * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @pack_dst: If true the destination only maps the changed pages packed
+ *	      contiguously, so the destination offset advances by changed-page
+ *	      runs instead of tracking the source page index.
 *
 * A defragmentation move may harvest already beneficial-order chunks from the
 * old tt, so the new tt references (borrows) some of the same physical pages.
@@ -830,16 +891,21 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 * only blit runs of pages that actually differ (src page != dst page),
 * coalescing contiguous to-copy pages into a single blit.
 *
+ * When @pack_dst is set the source pages are read from their natural offsets
+ * (@src_L0_ofs + page index) while the destination pages were packed into a
+ * contiguous prefix, so the destination offset is a running counter that only
+ * advances for emitted (changed) runs.
+ *
 * Return: The number of blits emitted (or that would be emitted when @bb is
 * NULL).
 */
 static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 			    u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
 			    struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
-			    pgoff_t start_page)
+			    pgoff_t start_page, bool pack_dst)
 {
 	u32 npages = src_L0 >> PAGE_SHIFT;
-	u32 i = 0, nblits = 0;
+	u32 i = 0, pack = 0, nblits = 0;
 
 	while (i < npages) {
 		u32 run = 0;
@@ -859,9 +925,71 @@ static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 
 		if (bb)
 			emit_copy(gt, bb, src_L0_ofs + (u64)i * PAGE_SIZE,
-				  dst_L0_ofs + (u64)i * PAGE_SIZE,
+				  dst_L0_ofs +
+				  (u64)(pack_dst ? pack : i) * PAGE_SIZE,
 				  run * PAGE_SIZE, XE_PAGE_SIZE);
 		nblits++;
+		pack += run;
+		i += run;
+	}
+
+	return nblits;
+}
+
+/*
+ * emit_ccs_copy_chunks() - Emit the indirect CCS aux copies for one packed
+ * defrag L0 chunk, skipping borrowed pages.
+ * @gt: The GT.
+ * @bb: The batch buffer to emit into, or NULL to only count the copies needed.
+ * @src_L0_ofs: GPU offset of the source pages mapped (comp PAT) for this chunk.
+ * @dst_L0_ofs: GPU offset of the destination pages mapped (comp PAT) for this
+ *	        chunk (the packed changed-page prefix).
+ * @src_L0: Byte size of the L0 chunk (page aligned).
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ *
+ * Companion to emit_copy_chunks() for the packed defrag CCS pass. The flat-CCS
+ * aux state is migrated indirectly through the data-page mappings, so it
+ * follows the physical page regardless of the (packed) GPU VA layout. Borrowed
+ * pages alias the same physical page on both sides, so their aux is already
+ * identical and is skipped; only the changed runs are copied, from the source
+ * at its natural offset to the destination at its packed offset.
+ *
+ * Return: The number of CCS copies emitted (or that would be emitted when @bb
+ * is NULL).
+ */
+static u32 emit_ccs_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
+				u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
+				struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
+				pgoff_t start_page)
+{
+	u32 npages = src_L0 >> PAGE_SHIFT;
+	u32 i = 0, pack = 0, nblits = 0;
+
+	while (i < npages) {
+		u32 run = 0;
+
+		/* Skip a run of borrowed pages (src and dst alias). */
+		if (src_tt->pages[start_page + i] ==
+		    dst_tt->pages[start_page + i]) {
+			i++;
+			continue;
+		}
+
+		/* Coalesce a run of pages whose aux must be migrated. */
+		while (i + run < npages &&
+		       src_tt->pages[start_page + i + run] !=
+		       dst_tt->pages[start_page + i + run])
+			run++;
+
+		if (bb)
+			emit_copy_ccs(gt, bb,
+				      dst_L0_ofs + (u64)pack * PAGE_SIZE, true,
+				      src_L0_ofs + (u64)i * PAGE_SIZE, true,
+				      run * PAGE_SIZE);
+		nblits++;
+		pack += run;
 		i += run;
 	}
 
@@ -949,6 +1077,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 					   struct ttm_resource *src,
 					   struct ttm_resource *dst,
 					   struct ttm_tt *src_tt,
+					   pgoff_t dst_packed_pages,
 					   struct xe_migrate_copy_flags flags)
 {
 	struct xe_gt *gt = m->tile->primary_gt;
@@ -981,6 +1110,23 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	 */
 	struct ttm_tt *dst_tt = dst_bo->ttm.ttm;
 	bool skip_borrowed = flags.defrag_copy && src_tt && dst_tt;
+	/*
+	 * Defrag-IOVA copy: the destination tt only maps the changed
+	 * (non-borrowed) pages, packed contiguously into the first
+	 * @dst_packed_pages pages of its IOVA. The source is read from the old
+	 * tt at natural offsets and each changed run is blitted into the packed
+	 * destination prefix.
+	 */
+	bool pack_dst = dst_packed_pages != 0;
+	/*
+	 * Packed defrag CCS pass: migrate the flat-CCS aux state of the changed
+	 * pages only, from the source at its natural offset to the destination
+	 * packed prefix. Borrowed pages share the physical page (and thus its
+	 * aux) on both sides, so they are skipped exactly like the data pass.
+	 */
+	bool ccs_pack = flags.defrag_ccs && pack_dst && src_tt && dst_tt;
+	/* Per-run, borrowed-skipping defrag emission (data or packed CCS). */
+	bool defrag_runs = skip_borrowed || ccs_pack;
 
 	/*
 	 * For decompression operation, always use the compression PAT index.
@@ -1009,7 +1155,9 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0,
+				pack_dst ? (u64)dst_packed_pages << PAGE_SHIFT :
+				size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
@@ -1030,29 +1178,86 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		u32 pte_flags;
 		pgoff_t start_page = (xe_bo_size(src_bo) - size) >> PAGE_SHIFT;
 		u32 ndata_blits = 1;
+		u32 nchg = 0;
+		u64 dst_size;
 
 		bool usm = xe->info.has_usm;
 		u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE;
 
+		/*
+		 * Defrag fast-forward: the new (destination) tt borrows the
+		 * unchanged beneficial-order chunks from the old tt, so for a
+		 * large object the bulk of the copy is borrowed pages that alias
+		 * the same memory on both sides. Skip the entire leading
+		 * borrowed run in a single cursor stride rather than discovering
+		 * it one (8 MB) L0 chunk at a time - each such pass would
+		 * otherwise pay xe_migrate_res_sizes() + pte_update_size() + a
+		 * full chunk walk only to copy nothing. The non-vram tt cursors
+		 * are normally advanced by emit_pte(); replicate that with
+		 * xe_res_next(). In pack_dst mode the destination only maps the
+		 * changed pages, so its cursor must not advance for borrowed
+		 * pages.
+		 */
+		if (defrag_runs) {
+			u32 brun = xe_migrate_leading_borrowed_pages(src_tt,
+								     dst_tt,
+								     start_page,
+								     size >> PAGE_SHIFT);
+
+			if (brun) {
+				u64 brun_bytes = (u64)brun << PAGE_SHIFT;
+
+				xe_res_next(&src_it, brun_bytes);
+				if (!pack_dst)
+					xe_res_next(&dst_it, brun_bytes);
+				size -= brun_bytes;
+				continue;
+			}
+		}
+
 		src_L0 = xe_migrate_res_sizes(m, &src_it);
 		dst_L0 = xe_migrate_res_sizes(m, &dst_it);
 
Quoted 8 lines

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1223

Quoted 3 lines
drivers/gpu/drm/xe/xe_migrate.c +0 -0
 		drm_dbg(&xe->drm, "Pass %u, sizes: %llu & %llu\n",
 			pass++, src_L0, dst_L0);
 
Assisted-by LLM

With above 'continue', this drm_dbg never gets printed, is that fine?

Yes.

Quoted 53 lines
Quoted 53 lines
drivers/gpu/drm/xe/xe_migrate.c +29 -4
-		src_L0 = min(src_L0, dst_L0);
+		/*
+		 * In pack_dst mode the destination is the (smaller) packed
+		 * prefix, so it must not clamp the source chunk size; the
+		 * source drives the walk and the destination is sized
+		 * independently from the changed-page count below.
+		 */
+		if (!pack_dst)
+			src_L0 = min(src_L0, dst_L0);
 
 		pte_flags = src_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 		pte_flags |= use_comp_pat ? PTE_UPDATE_FLAG_IS_COMP_PTE : 0;
 		batch_size += pte_update_size(m, pte_flags, src, &src_it, &src_L0,
 					      &src_L0_ofs, &src_L0_pt, 0, 0,
 					      avail_pts);
+		/*
+		 * The source chunk size may have been clamped above, so derive
+		 * the packed destination size from the changed-page count of
+		 * the final chunk.
+		 */
+		if (pack_dst) {
+			nchg = xe_migrate_count_changed_pages(src_tt, dst_tt,
+							      start_page,
+							      src_L0 >> PAGE_SHIFT);
+			dst_size = (u64)nchg << PAGE_SHIFT;
+		} else {
+			dst_size = src_L0;
+		}
 		if (flags.copy_only_ccs) {
 			dst_L0_ofs = src_L0_ofs;
+		} else if (pack_dst && !nchg) {
+			/* All borrowed: no destination PTEs for this chunk. */
+			dst_L0_ofs = 0;
+			dst_L0_pt = 0;
 		} else {
 			pte_flags = dst_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 			batch_size += pte_update_size(m, pte_flags, dst,
-						      &dst_it, &src_L0,
+						      &dst_it, &dst_size,
 						      &dst_L0_ofs, &dst_L0_pt,
 						      0, avail_pts, avail_pts);
 		}
@@ -1072,26 +1277,31 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		 * is split into one command per run of non-borrowed pages, so
 		 * size for the number of runs actually needed.
 		 */
-		if (skip_borrowed)
+		if (defrag_runs)
 			ndata_blits = emit_copy_chunks(gt, NULL, 0, 0, src_L0,
-						       src_tt, dst_tt, start_page);
+						       src_tt, dst_tt, start_page,
+						       pack_dst);
 
Quoted 15 lines

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1293

Quoted 11 lines
drivers/gpu/drm/xe/xe_migrate.c +4 -2
 		/*
 		 * If every page in this chunk was borrowed there is nothing to
 		 * copy: don't build a batch or issue any GPU commands, just
 		 * advance the cursors past the chunk. The non-vram tt cursors
-		 * would otherwise be advanced by emit_pte().
+		 * would otherwise be advanced by emit_pte(). In pack_dst mode
+		 * the destination consumed no packed pages, so leave its cursor
+		 * untouched.
 		 */
-		if (skip_borrowed && !ndata_blits) {
+		if (defrag_runs && !ndata_blits) {

LLM says this 'if' clause is dead code now as defrag_runs will always have non-zero ndata_blits.

This should be dead code from my read too.

So...

if (defrag_runs) {
	ndata_blits = ...
	xe_assert(xe, ndata_blits);
}
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/xe/xe_migrate.c +2 -1
 			xe_res_next(&src_it, src_L0);
-			xe_res_next(&dst_it, src_L0);
+			if (!pack_dst)
+				xe_res_next(&dst_it, src_L0);
 			size -= src_L0;
 			continue;
 		}
 
Quoted 10 lines

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1304

Quoted 5 lines
drivers/gpu/drm/xe/xe_migrate.c +2 -1
 		batch_size += ((flags.copy_only_ccs || flags.defrag_ccs) ? 0 :
 			       ndata_blits * emit_copy_cmd_len(xe)) +
-			((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));
+			(ccs_pack ? ndata_blits * EMIT_COPY_CCS_DW :
+			 (needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));

We are using ndata_blits returned from emit_copy_chunks to find calculate the batch size for ccs part. Though it might work, shouldn't we be getting that from emit_ccs_copy_chunks() with 'bb' set to NULL?

I guess we could do that but I don't really see the point to make this function more complicated (i.e., do we really need another conditional). As you say this works and asserts in various layers prove this.

Matt

Quoted 157 lines

Niranjana

Quoted 155 lines
drivers/gpu/drm/xe/xe_migrate.c +66 -7
 		bb = xe_bb_new(gt, batch_size, usm);
 		if (IS_ERR(bb)) {
@@ -1111,7 +1321,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		else if (!flags.copy_only_ccs)
 			emit_pte(m, bb, dst_L0_pt, dst_is_vram,
 				 copy_system_ccs || flags.defrag_ccs,
-				 &dst_it, src_L0, dst);
+				 &dst_it, dst_size, dst);
 
 		if (copy_system_ccs)
 			emit_pte(m, bb, ccs_pt, false, false, &ccs_it, ccs_size, src);
@@ -1121,16 +1331,22 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 
 		if (skip_borrowed)
 			emit_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0,
-					 src_tt, dst_tt, start_page);
+					 src_tt, dst_tt, start_page, pack_dst);
 		else if (!flags.copy_only_ccs && !flags.defrag_ccs)
 			emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE);
 
-		if (needs_ccs_emit)
+		if (ccs_pack) {
+			if (emit_ccs_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs,
+						 src_L0, src_tt, dst_tt,
+						 start_page))
+				flush_flags = MI_FLUSH_DW_CCS;
+		} else if (needs_ccs_emit) {
 			flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs,
 							  IS_DGFX(xe) ? src_is_vram : src_is_pltt,
 							  dst_L0_ofs,
 							  IS_DGFX(xe) ? dst_is_vram : dst_is_pltt,
 							  src_L0, ccs_ofs, copy_ccs);
+		}
 
 		job = xe_bb_create_migration_job(m->q, bb,
 						 xe_migrate_batch_base(m, usm),
@@ -1218,7 +1434,7 @@ struct dma_fence *xe_migrate_copy(struct xe_migrate *m,
 				  struct ttm_resource *dst,
 				  bool copy_only_ccs)
 {
-	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL,
+	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .copy_only_ccs = copy_only_ccs,
 				 });
@@ -1267,7 +1483,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 bool need_ccs)
 {
 	struct dma_fence *fence2, *fence =
-		__xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				  (struct xe_migrate_copy_flags) {
 					 .defrag_copy = true,
 					 });
@@ -1275,7 +1491,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	if (IS_ERR(fence) || !need_ccs)
 		return fence;
 
-	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				   (struct xe_migrate_copy_flags) {
 				   .defrag_ccs = true,
 				   });
@@ -1286,6 +1502,59 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	return fence2;
 }
 
+/**
+ * xe_migrate_copy_defrag_iova() - Copy a defrag move into a packed IOVA dst.
+ * @m: The migration context.
+ * @bo: The buffer object being defragmented.
+ * @src: The source (old) TTM resource.
+ * @dst: The destination (new) TTM resource.
+ * @src_tt: The stashed source (old) tt providing the page pointers, fully
+ *	    IOVA-mapped at natural offsets.
+ * @dst_packed_pages: Number of changed pages packed into the destination tt's
+ *	    IOVA prefix by xe_tt_map_iova_copy().
+ * @need_ccs: Whether the BO has flat-CCS aux state that must be migrated too.
+ *
+ * Defrag copy variant for IOVA-mapped BOs where the destination only maps the
+ * changed (non-borrowed) pages, packed contiguously into the first
+ * @dst_packed_pages of its IOVA. The source is read from the old tt at its
+ * natural offsets and each changed run is blitted into the packed destination
+ * prefix. When @need_ccs is set a second pass migrates the flat-CCS aux state
+ * of the same changed pages indirectly through their (packed) data mappings;
+ * borrowed pages share the physical page and its aux, so only the changed runs
+ * are touched.
+ *
+ * Return: Pointer to a dma_fence representing the last copy batch, or an error
+ * pointer on failure.
+ */
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs)
+{
+	struct dma_fence *fence2, *fence =
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, dst_packed_pages,
+				  (struct xe_migrate_copy_flags) {
+					 .defrag_copy = true,
+				 });
+
+	if (IS_ERR(fence) || !need_ccs)
+		return fence;
+
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+				   dst_packed_pages,
+				   (struct xe_migrate_copy_flags) {
+					 .defrag_ccs = true,
+				   });
+	if (IS_ERR(fence2))
+		dma_fence_wait(fence, false);
+	dma_fence_put(fence);
+
+	return fence2;
+}
+
 /**
 * xe_migrate_resolve() - Resolve and decompress a buffer object if required.
 * @m: The migrate context
@@ -1301,7 +1570,7 @@ struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res)
 {
-	return __xe_migrate_copy(m, bo, bo, res, res, NULL,
+	return __xe_migrate_copy(m, bo, bo, res, res, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .is_vram_resolve = true,
 				 });
drivers/gpu/drm/xe/xe_migrate.h +8 -0
index 4094ea1c19c1..da066cdf01bf 100644
@@ -141,6 +141,14 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 struct ttm_tt *src_tt,
 					 bool need_ccs);
 
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs);
+
 struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res);
Signature · 1 line
2.34.1
#128 lore raw

Re: [PATCH v3 31/33] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies

In reply to Matthew Brost
Quoted 417 lines · Matthew Brost

A defrag move with IOVA enabled only DMA-maps the changed (non-borrowed) pages of the new tt, packed contiguously into the front of its IOVA reservation (xe_tt_map_iova_copy()). The GPU copy therefore has an asymmetric geometry: the source is read from the old tt at its natural page offsets (via the old tt's full IOVA), while the destination only exposes the changed pages, packed.

Teach __xe_migrate_copy() about this packed-destination mode:

- Add a @dst_packed_pages argument. When non-zero the destination tt cursor walks just that many packed pages and the source (not the destination) drives the chunk size, so the smaller packed prefix never clamps the source walk.

- Size the destination PTE update for each chunk from the changed-page count (xe_migrate_count_changed_pages()) rather than the full chunk, and skip the destination PTEs entirely for an all-borrowed chunk.

- Extend emit_copy_chunks() with a @pack_dst flag so each emitted run is blitted from its natural source offset into a running packed destination offset that only advances for changed runs. The all-borrowed skip leaves the packed destination cursor untouched.

Expose this via xe_migrate_copy_defrag_iova(). It is only valid when the BO needs no CCS pass, since the separate CCS pass migrates aux state for all pages and would require the full destination mapping. The existing xe_migrate_copy_defrag() (full destination mapping) remains the path for CCS BOs and the non-IOVA fallback.

No users yet; xe_bo_move() is wired to this path in a later patch.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---
drivers/gpu/drm/xe/xe_migrate.c | 307 ++++++++++++++++++++++++++++++--
drivers/gpu/drm/xe/xe_migrate.h | 8 +

2 files changed, 296 insertions(+), 19 deletions(-)
drivers/gpu/drm/xe/xe_migrate.c +225 -15
index 666c715a9067..e1dc2fb5c5cb 100644
@@ -811,6 +811,64 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 		emit_xy_fast_copy(gt, bb, src_ofs, dst_ofs, size, pitch);
 }
 
+/*
+ * xe_migrate_count_changed_pages() - Count non-borrowed pages in a defrag chunk.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @npages: Number of pages in the L0 chunk.
+ *
+ * A defragmentation move may harvest already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) some of the same physical pages.
+ * Count the pages that actually differ (src page != dst page) within the chunk.
+ *
+ * Return: The number of non-borrowed (changed) pages.
+ */
+static u32 xe_migrate_count_changed_pages(struct ttm_tt *src_tt,
+					  struct ttm_tt *dst_tt,
+					  pgoff_t start_page, u32 npages)
+{
+	u32 i, nchg = 0;
+
+	for (i = 0; i < npages; i++)
+		if (src_tt->pages[start_page + i] !=
+		    dst_tt->pages[start_page + i])
+			nchg++;
+
+	return nchg;
+}
+
+/*
+ * xe_migrate_leading_borrowed_pages() - Count the run of borrowed pages at the
+ * head of the remaining copy.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of the next page to copy within the tt page arrays.
+ * @max_pages: Pages left to copy.
+ *
+ * A defragmentation move borrows the already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) the same physical pages for the
+ * unchanged majority of a large object. Those pages alias on both the source
+ * and destination GPU mappings and need no copy. Count how many consecutive
+ * pages starting at @start_page are borrowed so the caller can advance past the
+ * whole run at once instead of rediscovering it one L0 chunk at a time.
+ *
+ * Return: The number of leading borrowed (src page == dst page) pages, capped
+ * at @max_pages.
+ */
+static u32 xe_migrate_leading_borrowed_pages(struct ttm_tt *src_tt,
+					     struct ttm_tt *dst_tt,
+					     pgoff_t start_page, u32 max_pages)
+{
+	u32 n = 0;
+
+	while (n < max_pages &&
+	       src_tt->pages[start_page + n] == dst_tt->pages[start_page + n])
+		n++;
+
+	return n;
+}
+
 /*
 * emit_copy_chunks() - Emit the data blits for one defrag L0 chunk, skipping
 * borrowed pages.
@@ -822,6 +880,9 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 * @src_tt: Source (old) tt providing the page pointers.
 * @dst_tt: Destination (new) tt providing the page pointers.
 * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @pack_dst: If true the destination only maps the changed pages packed
+ *	      contiguously, so the destination offset advances by changed-page
+ *	      runs instead of tracking the source page index.
 *
 * A defragmentation move may harvest already beneficial-order chunks from the
 * old tt, so the new tt references (borrows) some of the same physical pages.
@@ -830,16 +891,21 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 * only blit runs of pages that actually differ (src page != dst page),
 * coalescing contiguous to-copy pages into a single blit.
 *
+ * When @pack_dst is set the source pages are read from their natural offsets
+ * (@src_L0_ofs + page index) while the destination pages were packed into a
+ * contiguous prefix, so the destination offset is a running counter that only
+ * advances for emitted (changed) runs.
+ *
 * Return: The number of blits emitted (or that would be emitted when @bb is
 * NULL).
 */
 static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 			    u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
 			    struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
-			    pgoff_t start_page)
+			    pgoff_t start_page, bool pack_dst)
 {
 	u32 npages = src_L0 >> PAGE_SHIFT;
-	u32 i = 0, nblits = 0;
+	u32 i = 0, pack = 0, nblits = 0;
 
 	while (i < npages) {
 		u32 run = 0;
@@ -859,9 +925,71 @@ static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 
 		if (bb)
 			emit_copy(gt, bb, src_L0_ofs + (u64)i * PAGE_SIZE,
-				  dst_L0_ofs + (u64)i * PAGE_SIZE,
+				  dst_L0_ofs +
+				  (u64)(pack_dst ? pack : i) * PAGE_SIZE,
 				  run * PAGE_SIZE, XE_PAGE_SIZE);
 		nblits++;
+		pack += run;
+		i += run;
+	}
+
+	return nblits;
+}
+
+/*
+ * emit_ccs_copy_chunks() - Emit the indirect CCS aux copies for one packed
+ * defrag L0 chunk, skipping borrowed pages.
+ * @gt: The GT.
+ * @bb: The batch buffer to emit into, or NULL to only count the copies needed.
+ * @src_L0_ofs: GPU offset of the source pages mapped (comp PAT) for this chunk.
+ * @dst_L0_ofs: GPU offset of the destination pages mapped (comp PAT) for this
+ *	        chunk (the packed changed-page prefix).
+ * @src_L0: Byte size of the L0 chunk (page aligned).
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ *
+ * Companion to emit_copy_chunks() for the packed defrag CCS pass. The flat-CCS
+ * aux state is migrated indirectly through the data-page mappings, so it
+ * follows the physical page regardless of the (packed) GPU VA layout. Borrowed
+ * pages alias the same physical page on both sides, so their aux is already
+ * identical and is skipped; only the changed runs are copied, from the source
+ * at its natural offset to the destination at its packed offset.
+ *
+ * Return: The number of CCS copies emitted (or that would be emitted when @bb
+ * is NULL).
+ */
+static u32 emit_ccs_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
+				u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
+				struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
+				pgoff_t start_page)
+{
+	u32 npages = src_L0 >> PAGE_SHIFT;
+	u32 i = 0, pack = 0, nblits = 0;
+
+	while (i < npages) {
+		u32 run = 0;
+
+		/* Skip a run of borrowed pages (src and dst alias). */
+		if (src_tt->pages[start_page + i] ==
+		    dst_tt->pages[start_page + i]) {
+			i++;
+			continue;
+		}
+
+		/* Coalesce a run of pages whose aux must be migrated. */
+		while (i + run < npages &&
+		       src_tt->pages[start_page + i + run] !=
+		       dst_tt->pages[start_page + i + run])
+			run++;
+
+		if (bb)
+			emit_copy_ccs(gt, bb,
+				      dst_L0_ofs + (u64)pack * PAGE_SIZE, true,
+				      src_L0_ofs + (u64)i * PAGE_SIZE, true,
+				      run * PAGE_SIZE);
+		nblits++;
+		pack += run;
 		i += run;
 	}
 
@@ -949,6 +1077,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 					   struct ttm_resource *src,
 					   struct ttm_resource *dst,
 					   struct ttm_tt *src_tt,
+					   pgoff_t dst_packed_pages,
 					   struct xe_migrate_copy_flags flags)
 {
 	struct xe_gt *gt = m->tile->primary_gt;
@@ -981,6 +1110,23 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	 */
 	struct ttm_tt *dst_tt = dst_bo->ttm.ttm;
 	bool skip_borrowed = flags.defrag_copy && src_tt && dst_tt;
+	/*
+	 * Defrag-IOVA copy: the destination tt only maps the changed
+	 * (non-borrowed) pages, packed contiguously into the first
+	 * @dst_packed_pages pages of its IOVA. The source is read from the old
+	 * tt at natural offsets and each changed run is blitted into the packed
+	 * destination prefix.
+	 */
+	bool pack_dst = dst_packed_pages != 0;
+	/*
+	 * Packed defrag CCS pass: migrate the flat-CCS aux state of the changed
+	 * pages only, from the source at its natural offset to the destination
+	 * packed prefix. Borrowed pages share the physical page (and thus its
+	 * aux) on both sides, so they are skipped exactly like the data pass.
+	 */
+	bool ccs_pack = flags.defrag_ccs && pack_dst && src_tt && dst_tt;
+	/* Per-run, borrowed-skipping defrag emission (data or packed CCS). */
+	bool defrag_runs = skip_borrowed || ccs_pack;
 
 	/*
 	 * For decompression operation, always use the compression PAT index.
@@ -1009,7 +1155,9 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0,
+				pack_dst ? (u64)dst_packed_pages << PAGE_SHIFT :
+				size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
@@ -1030,29 +1178,86 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		u32 pte_flags;
 		pgoff_t start_page = (xe_bo_size(src_bo) - size) >> PAGE_SHIFT;
 		u32 ndata_blits = 1;
+		u32 nchg = 0;
+		u64 dst_size;
 
 		bool usm = xe->info.has_usm;
 		u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE;
 
+		/*
+		 * Defrag fast-forward: the new (destination) tt borrows the
+		 * unchanged beneficial-order chunks from the old tt, so for a
+		 * large object the bulk of the copy is borrowed pages that alias
+		 * the same memory on both sides. Skip the entire leading
+		 * borrowed run in a single cursor stride rather than discovering
+		 * it one (8 MB) L0 chunk at a time - each such pass would
+		 * otherwise pay xe_migrate_res_sizes() + pte_update_size() + a
+		 * full chunk walk only to copy nothing. The non-vram tt cursors
+		 * are normally advanced by emit_pte(); replicate that with
+		 * xe_res_next(). In pack_dst mode the destination only maps the
+		 * changed pages, so its cursor must not advance for borrowed
+		 * pages.
+		 */
+		if (defrag_runs) {
+			u32 brun = xe_migrate_leading_borrowed_pages(src_tt,
+								     dst_tt,
+								     start_page,
+								     size >> PAGE_SHIFT);
+
+			if (brun) {
+				u64 brun_bytes = (u64)brun << PAGE_SHIFT;
+
+				xe_res_next(&src_it, brun_bytes);
+				if (!pack_dst)
+					xe_res_next(&dst_it, brun_bytes);
+				size -= brun_bytes;
+				continue;
+			}
+		}
+
 		src_L0 = xe_migrate_res_sizes(m, &src_it);
 		dst_L0 = xe_migrate_res_sizes(m, &dst_it);
 
 		drm_dbg(&xe->drm, "Pass %u, sizes: %llu & %llu\n",
 			pass++, src_L0, dst_L0);
 
-		src_L0 = min(src_L0, dst_L0);
+		/*
+		 * In pack_dst mode the destination is the (smaller) packed
+		 * prefix, so it must not clamp the source chunk size; the
+		 * source drives the walk and the destination is sized
+		 * independently from the changed-page count below.
+		 */
+		if (!pack_dst)
+			src_L0 = min(src_L0, dst_L0);
 
 		pte_flags = src_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 		pte_flags |= use_comp_pat ? PTE_UPDATE_FLAG_IS_COMP_PTE : 0;
 		batch_size += pte_update_size(m, pte_flags, src, &src_it, &src_L0,
 					      &src_L0_ofs, &src_L0_pt, 0, 0,
 					      avail_pts);
+		/*
+		 * The source chunk size may have been clamped above, so derive
+		 * the packed destination size from the changed-page count of
+		 * the final chunk.
+		 */
+		if (pack_dst) {
+			nchg = xe_migrate_count_changed_pages(src_tt, dst_tt,
+							      start_page,
+							      src_L0 >> PAGE_SHIFT);
+			dst_size = (u64)nchg << PAGE_SHIFT;
+		} else {
+			dst_size = src_L0;
+		}
 		if (flags.copy_only_ccs) {
 			dst_L0_ofs = src_L0_ofs;
+		} else if (pack_dst && !nchg) {
+			/* All borrowed: no destination PTEs for this chunk. */
+			dst_L0_ofs = 0;
+			dst_L0_pt = 0;
 		} else {
 			pte_flags = dst_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 			batch_size += pte_update_size(m, pte_flags, dst,
-						      &dst_it, &src_L0,
+						      &dst_it, &dst_size,
 						      &dst_L0_ofs, &dst_L0_pt,
 						      0, avail_pts, avail_pts);
 		}
@@ -1072,26 +1277,31 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		 * is split into one command per run of non-borrowed pages, so
 		 * size for the number of runs actually needed.
 		 */
-		if (skip_borrowed)
+		if (defrag_runs)
 			ndata_blits = emit_copy_chunks(gt, NULL, 0, 0, src_L0,
-						       src_tt, dst_tt, start_page);
+						       src_tt, dst_tt, start_page,
+						       pack_dst);
 
 		/*
 		 * If every page in this chunk was borrowed there is nothing to
 		 * copy: don't build a batch or issue any GPU commands, just
 		 * advance the cursors past the chunk. The non-vram tt cursors
-		 * would otherwise be advanced by emit_pte().
+		 * would otherwise be advanced by emit_pte(). In pack_dst mode
+		 * the destination consumed no packed pages, so leave its cursor
+		 * untouched.
 		 */
-		if (skip_borrowed && !ndata_blits) {
+		if (defrag_runs && !ndata_blits) {
 			xe_res_next(&src_it, src_L0);
-			xe_res_next(&dst_it, src_L0);
+			if (!pack_dst)
+				xe_res_next(&dst_it, src_L0);
 			size -= src_L0;
 			continue;
 		}
 
 		batch_size += ((flags.copy_only_ccs || flags.defrag_ccs) ? 0 :
 			       ndata_blits * emit_copy_cmd_len(xe)) +
-			((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));
+			(ccs_pack ? ndata_blits * EMIT_COPY_CCS_DW :
+			 (needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));
 
 		bb = xe_bb_new(gt, batch_size, usm);
 		if (IS_ERR(bb)) {
@@ -1111,7 +1321,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		else if (!flags.copy_only_ccs)
 			emit_pte(m, bb, dst_L0_pt, dst_is_vram,
 				 copy_system_ccs || flags.defrag_ccs,
-				 &dst_it, src_L0, dst);
+				 &dst_it, dst_size, dst);
 
 		if (copy_system_ccs)
 			emit_pte(m, bb, ccs_pt, false, false, &ccs_it, ccs_size, src);
@@ -1121,16 +1331,22 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 
 		if (skip_borrowed)
 			emit_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0,
-					 src_tt, dst_tt, start_page);
+					 src_tt, dst_tt, start_page, pack_dst);
 		else if (!flags.copy_only_ccs && !flags.defrag_ccs)
 			emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE);
 
-		if (needs_ccs_emit)
+		if (ccs_pack) {

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1348

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +5 -0
+			if (emit_ccs_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs,
+						 src_L0, src_tt, dst_tt,
+						 start_page))
+				flush_flags = MI_FLUSH_DW_CCS;
+		} else if (needs_ccs_emit) {
 			flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs,
 							  IS_DGFX(xe) ? src_is_vram : src_is_pltt,
 							  dst_L0_ofs,
 							  IS_DGFX(xe) ? dst_is_vram : dst_is_pltt,
 							  src_L0, ccs_ofs, copy_ccs);
Assisted-by LLM

There is a sashiko reported issue here about ccs_ofs being used uninitialized. Though probably we never run into that case, probably add an assert here which ensures that ccs_ofs never used uninitialized.

Quoted 57 lines
drivers/gpu/drm/xe/xe_migrate.c +26 -3
+		}
 
 		job = xe_bb_create_migration_job(m->q, bb,
 						 xe_migrate_batch_base(m, usm),
@@ -1218,7 +1434,7 @@ struct dma_fence *xe_migrate_copy(struct xe_migrate *m,
 				  struct ttm_resource *dst,
 				  bool copy_only_ccs)
 {
-	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL,
+	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .copy_only_ccs = copy_only_ccs,
 				 });
@@ -1267,7 +1483,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 bool need_ccs)
 {
 	struct dma_fence *fence2, *fence =
-		__xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				  (struct xe_migrate_copy_flags) {
 					 .defrag_copy = true,
 					 });
@@ -1275,7 +1491,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	if (IS_ERR(fence) || !need_ccs)
 		return fence;
 
-	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				   (struct xe_migrate_copy_flags) {
 				   .defrag_ccs = true,
 				   });
@@ -1286,6 +1502,59 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	return fence2;
 }
 
+/**
+ * xe_migrate_copy_defrag_iova() - Copy a defrag move into a packed IOVA dst.
+ * @m: The migration context.
+ * @bo: The buffer object being defragmented.
+ * @src: The source (old) TTM resource.
+ * @dst: The destination (new) TTM resource.
+ * @src_tt: The stashed source (old) tt providing the page pointers, fully
+ *	    IOVA-mapped at natural offsets.
+ * @dst_packed_pages: Number of changed pages packed into the destination tt's
+ *	    IOVA prefix by xe_tt_map_iova_copy().
+ * @need_ccs: Whether the BO has flat-CCS aux state that must be migrated too.
+ *
+ * Defrag copy variant for IOVA-mapped BOs where the destination only maps the
+ * changed (non-borrowed) pages, packed contiguously into the first
+ * @dst_packed_pages of its IOVA. The source is read from the old tt at its
+ * natural offsets and each changed run is blitted into the packed destination
+ * prefix. When @need_ccs is set a second pass migrates the flat-CCS aux state
+ * of the same changed pages indirectly through their (packed) data mappings;
+ * borrowed pages share the physical page and its aux, so only the changed runs
+ * are touched.
+ *
+ * Return: Pointer to a dma_fence representing the last copy batch, or an error

drivers/gpu/drm/xe/xe_migrate.c struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m, · line 1536

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +10 -0
+ * pointer on failure.
+ */
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs)
+{

There is a sashiko reported issue which though is not a bug, it would be good to assert here that dst_packed_pages is non-zero here.

Niranjana

Quoted 55 lines
drivers/gpu/drm/xe/xe_migrate.c +22 -1
+	struct dma_fence *fence2, *fence =
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, dst_packed_pages,
+				  (struct xe_migrate_copy_flags) {
+					 .defrag_copy = true,
+				 });
+
+	if (IS_ERR(fence) || !need_ccs)
+		return fence;
+
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+				   dst_packed_pages,
+				   (struct xe_migrate_copy_flags) {
+					 .defrag_ccs = true,
+				   });
+	if (IS_ERR(fence2))
+		dma_fence_wait(fence, false);
+	dma_fence_put(fence);
+
+	return fence2;
+}
+
 /**
 * xe_migrate_resolve() - Resolve and decompress a buffer object if required.
 * @m: The migrate context
@@ -1301,7 +1570,7 @@ struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res)
 {
-	return __xe_migrate_copy(m, bo, bo, res, res, NULL,
+	return __xe_migrate_copy(m, bo, bo, res, res, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .is_vram_resolve = true,
 				 });
drivers/gpu/drm/xe/xe_migrate.h +8 -0
index 4094ea1c19c1..da066cdf01bf 100644
@@ -141,6 +141,14 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 struct ttm_tt *src_tt,
 					 bool need_ccs);
 
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs);
+
 struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res);
Signature · 1 line
2.34.1
author maintainer
#129 lore raw

Re: [PATCH v3 31/33] drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies

Quoted 431 lines · Niranjana Vishwanathapura
Quoted 417 lines · Matthew Brost

A defrag move with IOVA enabled only DMA-maps the changed (non-borrowed) pages of the new tt, packed contiguously into the front of its IOVA reservation (xe_tt_map_iova_copy()). The GPU copy therefore has an asymmetric geometry: the source is read from the old tt at its natural page offsets (via the old tt's full IOVA), while the destination only exposes the changed pages, packed.

Teach __xe_migrate_copy() about this packed-destination mode:

- Add a @dst_packed_pages argument. When non-zero the destination tt cursor walks just that many packed pages and the source (not the destination) drives the chunk size, so the smaller packed prefix never clamps the source walk.

- Size the destination PTE update for each chunk from the changed-page count (xe_migrate_count_changed_pages()) rather than the full chunk, and skip the destination PTEs entirely for an all-borrowed chunk.

- Extend emit_copy_chunks() with a @pack_dst flag so each emitted run is blitted from its natural source offset into a running packed destination offset that only advances for changed runs. The all-borrowed skip leaves the packed destination cursor untouched.

Expose this via xe_migrate_copy_defrag_iova(). It is only valid when the BO needs no CCS pass, since the separate CCS pass migrates aux state for all pages and would require the full destination mapping. The existing xe_migrate_copy_defrag() (full destination mapping) remains the path for CCS BOs and the non-IOVA fallback.

No users yet; xe_bo_move() is wired to this path in a later patch.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---
drivers/gpu/drm/xe/xe_migrate.c | 307 ++++++++++++++++++++++++++++++--
drivers/gpu/drm/xe/xe_migrate.h | 8 +

2 files changed, 296 insertions(+), 19 deletions(-)
drivers/gpu/drm/xe/xe_migrate.c +225 -15
index 666c715a9067..e1dc2fb5c5cb 100644
@@ -811,6 +811,64 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 		emit_xy_fast_copy(gt, bb, src_ofs, dst_ofs, size, pitch);
 }
 
+/*
+ * xe_migrate_count_changed_pages() - Count non-borrowed pages in a defrag chunk.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @npages: Number of pages in the L0 chunk.
+ *
+ * A defragmentation move may harvest already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) some of the same physical pages.
+ * Count the pages that actually differ (src page != dst page) within the chunk.
+ *
+ * Return: The number of non-borrowed (changed) pages.
+ */
+static u32 xe_migrate_count_changed_pages(struct ttm_tt *src_tt,
+					  struct ttm_tt *dst_tt,
+					  pgoff_t start_page, u32 npages)
+{
+	u32 i, nchg = 0;
+
+	for (i = 0; i < npages; i++)
+		if (src_tt->pages[start_page + i] !=
+		    dst_tt->pages[start_page + i])
+			nchg++;
+
+	return nchg;
+}
+
+/*
+ * xe_migrate_leading_borrowed_pages() - Count the run of borrowed pages at the
+ * head of the remaining copy.
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of the next page to copy within the tt page arrays.
+ * @max_pages: Pages left to copy.
+ *
+ * A defragmentation move borrows the already beneficial-order chunks from the
+ * old tt, so the new tt references (borrows) the same physical pages for the
+ * unchanged majority of a large object. Those pages alias on both the source
+ * and destination GPU mappings and need no copy. Count how many consecutive
+ * pages starting at @start_page are borrowed so the caller can advance past the
+ * whole run at once instead of rediscovering it one L0 chunk at a time.
+ *
+ * Return: The number of leading borrowed (src page == dst page) pages, capped
+ * at @max_pages.
+ */
+static u32 xe_migrate_leading_borrowed_pages(struct ttm_tt *src_tt,
+					     struct ttm_tt *dst_tt,
+					     pgoff_t start_page, u32 max_pages)
+{
+	u32 n = 0;
+
+	while (n < max_pages &&
+	       src_tt->pages[start_page + n] == dst_tt->pages[start_page + n])
+		n++;
+
+	return n;
+}
+
 /*
 * emit_copy_chunks() - Emit the data blits for one defrag L0 chunk, skipping
 * borrowed pages.
@@ -822,6 +880,9 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 * @src_tt: Source (old) tt providing the page pointers.
 * @dst_tt: Destination (new) tt providing the page pointers.
 * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ * @pack_dst: If true the destination only maps the changed pages packed
+ *	      contiguously, so the destination offset advances by changed-page
+ *	      runs instead of tracking the source page index.
 *
 * A defragmentation move may harvest already beneficial-order chunks from the
 * old tt, so the new tt references (borrows) some of the same physical pages.
@@ -830,16 +891,21 @@ static void emit_copy(struct xe_gt *gt, struct xe_bb *bb,
 * only blit runs of pages that actually differ (src page != dst page),
 * coalescing contiguous to-copy pages into a single blit.
 *
+ * When @pack_dst is set the source pages are read from their natural offsets
+ * (@src_L0_ofs + page index) while the destination pages were packed into a
+ * contiguous prefix, so the destination offset is a running counter that only
+ * advances for emitted (changed) runs.
+ *
 * Return: The number of blits emitted (or that would be emitted when @bb is
 * NULL).
 */
 static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 			    u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
 			    struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
-			    pgoff_t start_page)
+			    pgoff_t start_page, bool pack_dst)
 {
 	u32 npages = src_L0 >> PAGE_SHIFT;
-	u32 i = 0, nblits = 0;
+	u32 i = 0, pack = 0, nblits = 0;
 
 	while (i < npages) {
 		u32 run = 0;
@@ -859,9 +925,71 @@ static u32 emit_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
 
 		if (bb)
 			emit_copy(gt, bb, src_L0_ofs + (u64)i * PAGE_SIZE,
-				  dst_L0_ofs + (u64)i * PAGE_SIZE,
+				  dst_L0_ofs +
+				  (u64)(pack_dst ? pack : i) * PAGE_SIZE,
 				  run * PAGE_SIZE, XE_PAGE_SIZE);
 		nblits++;
+		pack += run;
+		i += run;
+	}
+
+	return nblits;
+}
+
+/*
+ * emit_ccs_copy_chunks() - Emit the indirect CCS aux copies for one packed
+ * defrag L0 chunk, skipping borrowed pages.
+ * @gt: The GT.
+ * @bb: The batch buffer to emit into, or NULL to only count the copies needed.
+ * @src_L0_ofs: GPU offset of the source pages mapped (comp PAT) for this chunk.
+ * @dst_L0_ofs: GPU offset of the destination pages mapped (comp PAT) for this
+ *	        chunk (the packed changed-page prefix).
+ * @src_L0: Byte size of the L0 chunk (page aligned).
+ * @src_tt: Source (old) tt providing the page pointers.
+ * @dst_tt: Destination (new) tt providing the page pointers.
+ * @start_page: Page offset of this L0 chunk within the tt page arrays.
+ *
+ * Companion to emit_copy_chunks() for the packed defrag CCS pass. The flat-CCS
+ * aux state is migrated indirectly through the data-page mappings, so it
+ * follows the physical page regardless of the (packed) GPU VA layout. Borrowed
+ * pages alias the same physical page on both sides, so their aux is already
+ * identical and is skipped; only the changed runs are copied, from the source
+ * at its natural offset to the destination at its packed offset.
+ *
+ * Return: The number of CCS copies emitted (or that would be emitted when @bb
+ * is NULL).
+ */
+static u32 emit_ccs_copy_chunks(struct xe_gt *gt, struct xe_bb *bb,
+				u64 src_L0_ofs, u64 dst_L0_ofs, u32 src_L0,
+				struct ttm_tt *src_tt, struct ttm_tt *dst_tt,
+				pgoff_t start_page)
+{
+	u32 npages = src_L0 >> PAGE_SHIFT;
+	u32 i = 0, pack = 0, nblits = 0;
+
+	while (i < npages) {
+		u32 run = 0;
+
+		/* Skip a run of borrowed pages (src and dst alias). */
+		if (src_tt->pages[start_page + i] ==
+		    dst_tt->pages[start_page + i]) {
+			i++;
+			continue;
+		}
+
+		/* Coalesce a run of pages whose aux must be migrated. */
+		while (i + run < npages &&
+		       src_tt->pages[start_page + i + run] !=
+		       dst_tt->pages[start_page + i + run])
+			run++;
+
+		if (bb)
+			emit_copy_ccs(gt, bb,
+				      dst_L0_ofs + (u64)pack * PAGE_SIZE, true,
+				      src_L0_ofs + (u64)i * PAGE_SIZE, true,
+				      run * PAGE_SIZE);
+		nblits++;
+		pack += run;
 		i += run;
 	}
 
@@ -949,6 +1077,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 					   struct ttm_resource *src,
 					   struct ttm_resource *dst,
 					   struct ttm_tt *src_tt,
+					   pgoff_t dst_packed_pages,
 					   struct xe_migrate_copy_flags flags)
 {
 	struct xe_gt *gt = m->tile->primary_gt;
@@ -981,6 +1110,23 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	 */
 	struct ttm_tt *dst_tt = dst_bo->ttm.ttm;
 	bool skip_borrowed = flags.defrag_copy && src_tt && dst_tt;
+	/*
+	 * Defrag-IOVA copy: the destination tt only maps the changed
+	 * (non-borrowed) pages, packed contiguously into the first
+	 * @dst_packed_pages pages of its IOVA. The source is read from the old
+	 * tt at natural offsets and each changed run is blitted into the packed
+	 * destination prefix.
+	 */
+	bool pack_dst = dst_packed_pages != 0;
+	/*
+	 * Packed defrag CCS pass: migrate the flat-CCS aux state of the changed
+	 * pages only, from the source at its natural offset to the destination
+	 * packed prefix. Borrowed pages share the physical page (and thus its
+	 * aux) on both sides, so they are skipped exactly like the data pass.
+	 */
+	bool ccs_pack = flags.defrag_ccs && pack_dst && src_tt && dst_tt;
+	/* Per-run, borrowed-skipping defrag emission (data or packed CCS). */
+	bool defrag_runs = skip_borrowed || ccs_pack;
 
 	/*
 	 * For decompression operation, always use the compression PAT index.
@@ -1009,7 +1155,9 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 	else
 		xe_res_first(src, 0, size, &src_it);
 	if (!dst_is_vram)
-		xe_res_first_tt(dst_bo->ttm.ttm, 0, size, &dst_it);
+		xe_res_first_tt(dst_bo->ttm.ttm, 0,
+				pack_dst ? (u64)dst_packed_pages << PAGE_SHIFT :
+				size, &dst_it);
 	else
 		xe_res_first(dst, 0, size, &dst_it);
 
@@ -1030,29 +1178,86 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		u32 pte_flags;
 		pgoff_t start_page = (xe_bo_size(src_bo) - size) >> PAGE_SHIFT;
 		u32 ndata_blits = 1;
+		u32 nchg = 0;
+		u64 dst_size;
 
 		bool usm = xe->info.has_usm;
 		u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE;
 
+		/*
+		 * Defrag fast-forward: the new (destination) tt borrows the
+		 * unchanged beneficial-order chunks from the old tt, so for a
+		 * large object the bulk of the copy is borrowed pages that alias
+		 * the same memory on both sides. Skip the entire leading
+		 * borrowed run in a single cursor stride rather than discovering
+		 * it one (8 MB) L0 chunk at a time - each such pass would
+		 * otherwise pay xe_migrate_res_sizes() + pte_update_size() + a
+		 * full chunk walk only to copy nothing. The non-vram tt cursors
+		 * are normally advanced by emit_pte(); replicate that with
+		 * xe_res_next(). In pack_dst mode the destination only maps the
+		 * changed pages, so its cursor must not advance for borrowed
+		 * pages.
+		 */
+		if (defrag_runs) {
+			u32 brun = xe_migrate_leading_borrowed_pages(src_tt,
+								     dst_tt,
+								     start_page,
+								     size >> PAGE_SHIFT);
+
+			if (brun) {
+				u64 brun_bytes = (u64)brun << PAGE_SHIFT;
+
+				xe_res_next(&src_it, brun_bytes);
+				if (!pack_dst)
+					xe_res_next(&dst_it, brun_bytes);
+				size -= brun_bytes;
+				continue;
+			}
+		}
+
 		src_L0 = xe_migrate_res_sizes(m, &src_it);
 		dst_L0 = xe_migrate_res_sizes(m, &dst_it);
 
 		drm_dbg(&xe->drm, "Pass %u, sizes: %llu & %llu\n",
 			pass++, src_L0, dst_L0);
 
-		src_L0 = min(src_L0, dst_L0);
+		/*
+		 * In pack_dst mode the destination is the (smaller) packed
+		 * prefix, so it must not clamp the source chunk size; the
+		 * source drives the walk and the destination is sized
+		 * independently from the changed-page count below.
+		 */
+		if (!pack_dst)
+			src_L0 = min(src_L0, dst_L0);
 
 		pte_flags = src_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 		pte_flags |= use_comp_pat ? PTE_UPDATE_FLAG_IS_COMP_PTE : 0;
 		batch_size += pte_update_size(m, pte_flags, src, &src_it, &src_L0,
 					      &src_L0_ofs, &src_L0_pt, 0, 0,
 					      avail_pts);
+		/*
+		 * The source chunk size may have been clamped above, so derive
+		 * the packed destination size from the changed-page count of
+		 * the final chunk.
+		 */
+		if (pack_dst) {
+			nchg = xe_migrate_count_changed_pages(src_tt, dst_tt,
+							      start_page,
+							      src_L0 >> PAGE_SHIFT);
+			dst_size = (u64)nchg << PAGE_SHIFT;
+		} else {
+			dst_size = src_L0;
+		}
 		if (flags.copy_only_ccs) {
 			dst_L0_ofs = src_L0_ofs;
+		} else if (pack_dst && !nchg) {
+			/* All borrowed: no destination PTEs for this chunk. */
+			dst_L0_ofs = 0;
+			dst_L0_pt = 0;
 		} else {
 			pte_flags = dst_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0;
 			batch_size += pte_update_size(m, pte_flags, dst,
-						      &dst_it, &src_L0,
+						      &dst_it, &dst_size,
 						      &dst_L0_ofs, &dst_L0_pt,
 						      0, avail_pts, avail_pts);
 		}
@@ -1072,26 +1277,31 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		 * is split into one command per run of non-borrowed pages, so
 		 * size for the number of runs actually needed.
 		 */
-		if (skip_borrowed)
+		if (defrag_runs)
 			ndata_blits = emit_copy_chunks(gt, NULL, 0, 0, src_L0,
-						       src_tt, dst_tt, start_page);
+						       src_tt, dst_tt, start_page,
+						       pack_dst);
 
 		/*
 		 * If every page in this chunk was borrowed there is nothing to
 		 * copy: don't build a batch or issue any GPU commands, just
 		 * advance the cursors past the chunk. The non-vram tt cursors
-		 * would otherwise be advanced by emit_pte().
+		 * would otherwise be advanced by emit_pte(). In pack_dst mode
+		 * the destination consumed no packed pages, so leave its cursor
+		 * untouched.
 		 */
-		if (skip_borrowed && !ndata_blits) {
+		if (defrag_runs && !ndata_blits) {
 			xe_res_next(&src_it, src_L0);
-			xe_res_next(&dst_it, src_L0);
+			if (!pack_dst)
+				xe_res_next(&dst_it, src_L0);
 			size -= src_L0;
 			continue;
 		}
 
 		batch_size += ((flags.copy_only_ccs || flags.defrag_ccs) ? 0 :
 			       ndata_blits * emit_copy_cmd_len(xe)) +
-			((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));
+			(ccs_pack ? ndata_blits * EMIT_COPY_CCS_DW :
+			 (needs_ccs_emit ? EMIT_COPY_CCS_DW : 0));
 
 		bb = xe_bb_new(gt, batch_size, usm);
 		if (IS_ERR(bb)) {
@@ -1111,7 +1321,7 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 		else if (!flags.copy_only_ccs)
 			emit_pte(m, bb, dst_L0_pt, dst_is_vram,
 				 copy_system_ccs || flags.defrag_ccs,
-				 &dst_it, src_L0, dst);
+				 &dst_it, dst_size, dst);
 
 		if (copy_system_ccs)
 			emit_pte(m, bb, ccs_pt, false, false, &ccs_it, ccs_size, src);
@@ -1121,16 +1331,22 @@ static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m,
 
 		if (skip_borrowed)
 			emit_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0,
-					 src_tt, dst_tt, start_page);
+					 src_tt, dst_tt, start_page, pack_dst);
 		else if (!flags.copy_only_ccs && !flags.defrag_ccs)
 			emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE);
 
-		if (needs_ccs_emit)
+		if (ccs_pack) {

drivers/gpu/drm/xe/xe_migrate.c static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, · line 1348

Quoted 10 lines
drivers/gpu/drm/xe/xe_migrate.c +5 -0
+			if (emit_ccs_copy_chunks(gt, bb, src_L0_ofs, dst_L0_ofs,
+						 src_L0, src_tt, dst_tt,
+						 start_page))
+				flush_flags = MI_FLUSH_DW_CCS;
+		} else if (needs_ccs_emit) {
 			flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs,
 							  IS_DGFX(xe) ? src_is_vram : src_is_pltt,
 							  dst_L0_ofs,
 							  IS_DGFX(xe) ? dst_is_vram : dst_is_pltt,
 							  src_L0, ccs_ofs, copy_ccs);
Assisted-by LLM
Quoted 4 lines

There is a sashiko reported issue here about ccs_ofs being used uninitialized. Though probably we never run into that case, probably add an assert here which ensures that ccs_ofs never used uninitialized.

How would we assert that?

Quoted 62 lines
Quoted 62 lines
drivers/gpu/drm/xe/xe_migrate.c +31 -3
+		}
 
 		job = xe_bb_create_migration_job(m->q, bb,
 						 xe_migrate_batch_base(m, usm),
@@ -1218,7 +1434,7 @@ struct dma_fence *xe_migrate_copy(struct xe_migrate *m,
 				  struct ttm_resource *dst,
 				  bool copy_only_ccs)
 {
-	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL,
+	return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .copy_only_ccs = copy_only_ccs,
 				 });
@@ -1267,7 +1483,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 bool need_ccs)
 {
 	struct dma_fence *fence2, *fence =
-		__xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				  (struct xe_migrate_copy_flags) {
 					 .defrag_copy = true,
 					 });
@@ -1275,7 +1491,7 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	if (IS_ERR(fence) || !need_ccs)
 		return fence;
 
-	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt, 0,
 				   (struct xe_migrate_copy_flags) {
 				   .defrag_ccs = true,
 				   });
@@ -1286,6 +1502,59 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 	return fence2;
 }
 
+/**
+ * xe_migrate_copy_defrag_iova() - Copy a defrag move into a packed IOVA dst.
+ * @m: The migration context.
+ * @bo: The buffer object being defragmented.
+ * @src: The source (old) TTM resource.
+ * @dst: The destination (new) TTM resource.
+ * @src_tt: The stashed source (old) tt providing the page pointers, fully
+ *	    IOVA-mapped at natural offsets.
+ * @dst_packed_pages: Number of changed pages packed into the destination tt's
+ *	    IOVA prefix by xe_tt_map_iova_copy().
+ * @need_ccs: Whether the BO has flat-CCS aux state that must be migrated too.
+ *
+ * Defrag copy variant for IOVA-mapped BOs where the destination only maps the
+ * changed (non-borrowed) pages, packed contiguously into the first
+ * @dst_packed_pages of its IOVA. The source is read from the old tt at its
+ * natural offsets and each changed run is blitted into the packed destination
+ * prefix. When @need_ccs is set a second pass migrates the flat-CCS aux state
+ * of the same changed pages indirectly through their (packed) data mappings;
+ * borrowed pages share the physical page and its aux, so only the changed runs
+ * are touched.
+ *
+ * Return: Pointer to a dma_fence representing the last copy batch, or an error
+ * pointer on failure.
+ */
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
Quoted 9 lines

drivers/gpu/drm/xe/xe_migrate.c struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m, · line 1536

Quoted 5 lines
drivers/gpu/drm/xe/xe_migrate.c +5 -0
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs)
+{

There is a sashiko reported issue which though is not a bug, it would be good to assert here that dst_packed_pages is non-zero here.

Sure.

Matt

Quoted 57 lines

Niranjana

Quoted 55 lines
drivers/gpu/drm/xe/xe_migrate.c +22 -1
+	struct dma_fence *fence2, *fence =
+		__xe_migrate_copy(m, bo, bo, src, dst, src_tt, dst_packed_pages,
+				  (struct xe_migrate_copy_flags) {
+					 .defrag_copy = true,
+				 });
+
+	if (IS_ERR(fence) || !need_ccs)
+		return fence;
+
+	fence2 = __xe_migrate_copy(m, bo, bo, src, dst, src_tt,
+				   dst_packed_pages,
+				   (struct xe_migrate_copy_flags) {
+					 .defrag_ccs = true,
+				   });
+	if (IS_ERR(fence2))
+		dma_fence_wait(fence, false);
+	dma_fence_put(fence);
+
+	return fence2;
+}
+
 /**
 * xe_migrate_resolve() - Resolve and decompress a buffer object if required.
 * @m: The migrate context
@@ -1301,7 +1570,7 @@ struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res)
 {
-	return __xe_migrate_copy(m, bo, bo, res, res, NULL,
+	return __xe_migrate_copy(m, bo, bo, res, res, NULL, 0,
 				 (struct xe_migrate_copy_flags) {
 					 .is_vram_resolve = true,
 				 });
drivers/gpu/drm/xe/xe_migrate.h +8 -0
index 4094ea1c19c1..da066cdf01bf 100644
@@ -141,6 +141,14 @@ struct dma_fence *xe_migrate_copy_defrag(struct xe_migrate *m,
 					 struct ttm_tt *src_tt,
 					 bool need_ccs);
 
+struct dma_fence *xe_migrate_copy_defrag_iova(struct xe_migrate *m,
+					      struct xe_bo *bo,
+					      struct ttm_resource *src,
+					      struct ttm_resource *dst,
+					      struct ttm_tt *src_tt,
+					      pgoff_t dst_packed_pages,
+					      bool need_ccs);
+
 struct dma_fence *xe_migrate_resolve(struct xe_migrate *m,
 				     struct xe_bo *bo,
 				     struct ttm_resource *res);
Signature · 1 line
2.34.1
author maintainer
#130 lore raw

PATCH v3 32/33 drm/xe: Finalize defrag-IOVA moves with post-copy job

In reply to Matthew Brost

Wire up end-to-end defrag for IOVA-mapped BOs. A defrag move is eligible whenever the destination TT is IOVA mapped. The packed-copy step moves only changed pages; the IOVA reservation is transferred to the new backing so device-visible addresses are preserved, avoiding GPU rebind overhead.

Add xe_bo_defrag_use_iova() to select the IOVA defrag path. Add xe_bo_defrag_iova_copy() to drive the move: diff new vs stashed old TT, link changed pages packed, blit them, then queue a post-copy finalize job.

The finalize job tears down the temporary packed mapping, transfers the full old TT reservation to the new one, and relinks the copied pages at natural offsets.

Borrowed pages skip the copy and simply adopt the old reservation.

Since reservation transfer preserves device-visible addresses, xe_bo_trigger_rebind() skips drm_gpuvm_bo_evict() marking; GPU PTEs stay valid.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

v3:
- Check dma_iova_sync(), wait the copy fence before unlink on error, and gate the fast path on old_tt->use_iova (Sashiko)

---
 drivers/gpu/drm/xe/xe_bo.c | 325 +++++++++++++++++++++++++++++++++++--
 1 file changed, 311 insertions(+), 14 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +311 -14
index c087f17ad6db..36290f58ccc1 100644
@@ -13,6 +13,7 @@
 #include <drm/drm_dumb_buffers.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/drm_managed.h>
+#include <drm/gpu_scheduler.h>
 #include <drm/ttm/ttm_backup.h>
 #include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_placement.h>
@@ -25,6 +26,7 @@
 #include <trace/events/gpu_mem.h>
 
 #include "xe_device.h"
+#include "xe_dep_job_types.h"
 #include "xe_dep_scheduler.h"
 #include "xe_dma_buf.h"
 #include "xe_drm_client.h"
@@ -542,7 +544,6 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
  * @xe_tt->iova_dma_pages[]. Used to (re)derive accounting that exactly matches
  * the pages a tt is actually mapped with.
  */
-__maybe_unused
 static void xe_tt_account_iova_pages(struct xe_device *xe,
 				     struct xe_ttm_tt *xe_tt)
 {
@@ -567,7 +568,6 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 {
 }
 
-__maybe_unused
 static void xe_tt_account_iova_pages(struct xe_device *xe,
 				     struct xe_ttm_tt *xe_tt)
 {
@@ -806,7 +806,6 @@ struct xe_tt_iova_copy {
  * differ. Coalesce contiguous changed folios into runs and, for each run,
  * invoke @fn. Returns the total number of changed pages.
  */
-__maybe_unused
 static pgoff_t xe_tt_iova_walk_changed(struct ttm_tt *new_tt,
 				       struct ttm_tt *old_tt,
 				       void (*fn)(pgoff_t start, pgoff_t count,
@@ -855,7 +854,6 @@ struct xe_tt_iova_run_collect {
 };
 
 /* Geometrically grow @arr (element size @elem) to hold at least @need slots. */
-__maybe_unused
 static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
 			   size_t elem)
 {
@@ -878,7 +876,6 @@ static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
 	return 0;
 }
 
-__maybe_unused
 static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
 {
 	struct xe_tt_iova_run_collect *c = arg;
@@ -933,7 +930,6 @@ static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
  *
  * Return: 0 on success, -ENOMEM on allocation failure.
  */
-__maybe_unused
 static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
 				struct xe_tt_iova_copy *copy)
 {
@@ -969,7 +965,6 @@ static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
  * xe_tt_iova_copy_fini() - Release a struct xe_tt_iova_copy
  * @copy: The copy description to release.
  */
-__maybe_unused
 static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
 {
 	kfree(copy->runs);
@@ -1000,7 +995,6 @@ static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
  * Return: 0 on success, negative error code on failure (any partial linkage is
  * unwound).
  */
-__maybe_unused
 static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
 			       const struct xe_tt_iova_copy *copy)
 {
@@ -1047,6 +1041,133 @@ static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
 	return ret;
 }
 
+/**
+ * struct xe_iova_defrag_job - Post-copy finalize job for an IOVA defrag move.
+ *
+ * After the GPU defrag copy into the packed copy-step mapping completes, this
+ * dependency job runs the CPU-side IOMMU fixups at the point of no failure:
+ * tear down the temporary packed mapping, then per changed run unlink the old
+ * pages and link the new pages into the (transferred) reservation at their
+ * natural offset.
+ */
+struct xe_iova_defrag_job {
+	/** @dep: base generic dependency Xe job. */
+	struct xe_dep_job dep;
+	/** @xe: The xe device. */
+	struct xe_device *xe;
+	/** @new_tt: The destination tt being finalized (owns the reservation). */
+	struct ttm_tt *new_tt;
+	/**
+	 * @copy_iova: Snapshot of the new tt's original IOVA reservation that
+	 * held the temporary packed copy-step mapping. Destroyed by the job
+	 * (the new tt itself adopts the old tt's reservation at job creation).
+	 */
+	struct dma_iova_state copy_iova;
+	/** @copy_bytes: Byte length of the packed copy-step mapping. */
+	size_t copy_bytes;
+	/** @copy: Changed-page description; the job owns and frees @copy.runs. */
+	struct xe_tt_iova_copy copy;
+};
+
+static struct dma_fence *xe_iova_defrag_job_run(struct xe_dep_job *dep_job)
+{
+	struct xe_iova_defrag_job *job =
+		container_of(dep_job, typeof(*job), dep);
+	struct xe_device *xe = job->xe;
+	struct device *dev = xe->drm.dev;
+	struct xe_ttm_tt *xe_tt =
+		container_of(job->new_tt, struct xe_ttm_tt, ttm);
+	unsigned int r, l;
+
+	/*
+	 * Tear down the temporary packed copy-step mapping and free its
+	 * reservation. The new tt has already adopted the old tt's full
+	 * reservation (transferred at job creation), so this only touches the
+	 * snapshot taken before the transfer.
+	 *
+	 * This mapping was the GPU blit's write destination, so tear it down
+	 * with attrs 0 (not DMA_ATTR_SKIP_CPU_SYNC): for non-coherent or
+	 * bounced (SWIOTLB) mappings the device->CPU copy-back / cache sync of
+	 * the freshly copied data happens during unlink, exactly as
+	 * xe_tt_unmap_iova() does. Skipping it would silently drop the
+	 * defragmented data on those configs.
+	 */
+	if (job->copy_bytes)
+		dma_iova_destroy(dev, &job->copy_iova, job->copy_bytes,
+				 DMA_BIDIRECTIONAL, 0);
+	else
+		dma_iova_free(dev, &job->copy_iova);
+
+	/*
+	 * For each changed run, unlink the old pages (still linked from the
+	 * adopted reservation).
+	 */
+	for (r = 0; r < job->copy.nr_runs; r++) {
+		size_t off = (size_t)job->copy.runs[r].start << PAGE_SHIFT;
+		size_t len = (size_t)job->copy.runs[r].count << PAGE_SHIFT;
+
+		dma_iova_unlink(dev, &xe_tt->iova, off, len,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+	}
+
+	/*
+	 * Relink the freshly copied new pages from the snapshot captured at job
+	 * creation. The destination tt's page array may have been mutated (or
+	 * torn down) by TTM since then, so it is never dereferenced here.
+	 * Borrowed pages are shared with the old tt and stay correctly linked
+	 * for free, so the snapshot only covers the changed runs.
+	 */
+	for (l = 0; l < job->copy.nr_links; l++) {
+		int ret = dma_iova_link(dev, &xe_tt->iova,
+					job->copy.links[l].phys,
+					job->copy.links[l].offset,
+					job->copy.links[l].len,
+					DMA_BIDIRECTIONAL,
+					DMA_ATTR_SKIP_CPU_SYNC);
+		/*
+		 * Point of no failure: the data is already copied and the
+		 * reservation ownership has been transferred. A relink failure
+		 * leaves a hole in the mapping; there is no safe recovery, so
+		 * just warn.
+		 */
+		drm_WARN_ON(&xe->drm, ret);
+	}
+
+	/* Sync the relinked changed runs into the device's view. */
+	for (r = 0; r < job->copy.nr_runs; r++) {
+		size_t off = (size_t)job->copy.runs[r].start << PAGE_SHIFT;
+		size_t len = (size_t)job->copy.runs[r].count << PAGE_SHIFT;
+
+		/*
+		 * Point of no failure (as with the relink above): warn on a
+		 * sync error so a stale IOMMU cache does not fail silently.
+		 */
+		drm_WARN_ON(&xe->drm, dma_iova_sync(dev, &xe_tt->iova, off, len));
+	}
+
+	/*
+	 * All work is synchronous CPU-side IOMMU fixup, so there is no hardware
+	 * fence to wait on; returning NULL makes the scheduler signal the job's
+	 * finished fence immediately.
+	 */
+	return NULL;
+}
+
+static void xe_iova_defrag_job_free(struct xe_dep_job *dep_job)
+{
+	struct xe_iova_defrag_job *job =
+		container_of(dep_job, typeof(*job), dep);
+
+	xe_tt_iova_copy_fini(&job->copy);
+	drm_sched_job_cleanup(&job->dep.drm);
+	kfree(job);
+}
+
+static const struct xe_dep_job_ops xe_iova_defrag_job_ops = {
+	.run_job = xe_iova_defrag_job_run,
+	.free_job = xe_iova_defrag_job_free,
+};
+
 /*
  * xe_tt_iova_transfer - Transfer a full IOVA reservation old_tt -> new_tt.
  *
@@ -1063,7 +1184,6 @@ static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
  * folio walk of the new tt keeps xe->mem.dma_mapped_pages[] exact while the new
  * mapping lives, rather than carrying the old tt's stale distribution.
  */
-__maybe_unused
 static void xe_tt_iova_transfer(struct xe_device *xe,
 				struct xe_ttm_tt *new_tt,
 				struct xe_ttm_tt *old_tt)
@@ -1078,6 +1198,154 @@ static void xe_tt_iova_transfer(struct xe_device *xe,
 	old_tt->use_iova = false;
 }
 
+/*
+ * xe_bo_defrag_iova_copy - Run an IOVA defrag move with a packed copy mapping.
+ *
+ * Only the changed (non-borrowed) pages of @new_tt are DMA mapped, packed
+ * contiguously into the front of its reservation, the GPU copy blits them from
+ * the old tt at natural offsets, and a post-copy finalize job adopts the old
+ * tt's full reservation and relinks the changed runs. Returns the finalize
+ * job's finished fence (the fence the move waits on), or an ERR_PTR on failure
+ * with all partial state unwound.
+ */
+static struct dma_fence *
+xe_bo_defrag_iova_copy(struct xe_device *xe, struct xe_migrate *migrate,
+		       struct xe_bo *bo, struct ttm_resource *old_mem,
+		       struct ttm_resource *new_mem, struct ttm_tt *old_tt,
+		       bool handle_system_ccs)
+{
+	struct ttm_tt *new_tt = bo->ttm.ttm;
+	struct xe_ttm_tt *new_xe_tt = container_of(new_tt, struct xe_ttm_tt, ttm);
+	struct xe_ttm_tt *old_xe_tt = container_of(old_tt, struct xe_ttm_tt, ttm);
+	struct drm_sched_entity *entity =
+		xe_dep_scheduler_entity(xe->mem.defrag.iova_sched);
+	struct xe_iova_defrag_job *job;
+	struct dma_fence *copy_fence;
+	struct dma_fence *fence;
+	struct xe_tt_iova_copy copy;
+	bool packed = false;
+	int err;
+
+	xe_assert(xe, new_xe_tt->use_iova && !new_xe_tt->iova_linked);
+	xe_assert(xe, old_xe_tt->use_iova && old_xe_tt->iova_linked);
+
+	err = xe_tt_iova_copy_init(new_tt, old_tt, &copy);
+	if (err)
+		return ERR_PTR(err);
+
+	if (copy.nr_changed) {
+		err = xe_tt_map_iova_copy(xe, new_tt, &copy);
+		if (err)
+			goto err_copy;
+		packed = true;
+
+		copy_fence = xe_migrate_copy_defrag_iova(migrate, bo, old_mem,
+							 new_mem, old_tt,
+							 copy.nr_changed,
+							 handle_system_ccs);
+	} else {
+		copy_fence = dma_fence_get_stub();
+	}
+	if (IS_ERR(copy_fence)) {
+		err = PTR_ERR(copy_fence);
+		goto err_unmap;
+	}
+
+	job = kzalloc(sizeof(*job), GFP_KERNEL);
+	if (!job) {
+		err = -ENOMEM;
+		goto err_fence;
+	}
+
+	job->dep.ops = &xe_iova_defrag_job_ops;
+	job->xe = xe;
+	job->new_tt = new_tt;
+	job->copy_iova = new_xe_tt->iova;
+	job->copy_bytes = (size_t)copy.nr_changed << PAGE_SHIFT;
+	job->copy = copy;
+
+	err = drm_sched_job_init(&job->dep.drm, entity, 1, NULL, 0);
+	if (err)
+		goto err_job;
+
+	err = drm_sched_job_add_dependency(&job->dep.drm,
+					   dma_fence_get(copy_fence));
+	if (err)
+		goto err_job_init;
+
+	/*
+	 * Point of no failure: transfer the old tt's full reservation to the
+	 * new tt. The finalize job (already armed below) destroys the packed
+	 * copy mapping snapshot and relinks the changed runs.
+	 */
+	xe_tt_iova_transfer(xe, new_xe_tt, old_xe_tt);
+	memset(&copy, 0, sizeof(copy)); /* ownership moved to the job */
+
+	drm_sched_job_arm(&job->dep.drm);
+	fence = dma_fence_get(&job->dep.drm.s_fence->finished);
+	drm_sched_entity_push_job(&job->dep.drm);
+
+	dma_fence_put(copy_fence);
+
+	return fence;
+
+err_job_init:
+	drm_sched_job_cleanup(&job->dep.drm);
+err_job:
+	kfree(job);
+err_fence:
+	/*
+	 * The GPU copy into the packed destination mapping was already
+	 * submitted; it must finish before we tear that mapping down below,
+	 * otherwise the blit would fault or scribble on unmapped IOVA. (Reached
+	 * only with a valid, submitted copy_fence; the IS_ERR(copy_fence) case
+	 * jumps straight to err_unmap with no GPU work in flight.)
+	 */
+	if (packed)
+		dma_fence_wait(copy_fence, false);
+	dma_fence_put(copy_fence);
+err_unmap:
+	if (packed)
+		dma_iova_unlink(xe->drm.dev, &new_xe_tt->iova, 0,
+				(size_t)copy.nr_changed << PAGE_SHIFT,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+err_copy:
+	xe_tt_iova_copy_fini(&copy);
+	return ERR_PTR(err);
+}
+
+/*
+ * xe_bo_defrag_use_iova - Whether a defrag move should use the packed IOVA path.
+ *
+ * Eligible when the move is a defrag (stashed old tt) and the destination tt is
+ * IOVA mapped. The IOVA reservation is transferred to the new backing, so the
+ * device-visible addresses are preserved and no GPU rebind is needed (see
+ * xe_bo_trigger_rebind()); this makes the path worthwhile regardless of BO
+ * size. A flat-CCS aux pass, if needed, is migrated packed alongside the data
+ * (changed pages only), so it does not disqualify the path. Otherwise the
+ * full-mapping defrag path is used.
+ */
+static bool xe_bo_defrag_use_iova(struct ttm_buffer_object *ttm_bo)
+{
+	struct xe_ttm_tt *xe_tt, *old_xe_tt;
+
+	if (!ttm_bo->defrag_old_tt || !ttm_bo->ttm)
+		return false;
+
+	xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt, ttm);
+	old_xe_tt = container_of(ttm_bo->defrag_old_tt, struct xe_ttm_tt, ttm);
+
+	/*
+	 * Both tts must be IOVA mapped: the packed copy reads the old tt's IOVA
+	 * and the finalize job transfers the old tt's reservation to the new
+	 * one. use_iova is a deterministic per-device decision taken at tt
+	 * creation, so for a given BO the old and new tt always agree; the
+	 * explicit old-tt check guards against ever routing a sg-fallback old
+	 * tt into xe_tt_iova_transfer() (which would copy an invalid iova).
+	 */
+	return xe_tt->use_iova && old_xe_tt->use_iova;
+}
+
 /*
  * Account ttm pages against the device shrinker's shrinkable and
  * purgeable counts.
@@ -1357,6 +1625,7 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 	struct drm_gem_object *obj = &bo->ttm.base;
 	struct drm_gpuvm_bo *vm_bo;
 	bool idle = false;
+	bool defrag_iova;
 	int ret = 0;
 
 	dma_resv_assert_held(bo->ttm.base.resv);
@@ -1369,12 +1638,25 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 		dma_resv_iter_end(&cursor);
 	}
 
+	/*
+	 * A defrag move that reallocates IOVA-mapped backing preserves the
+	 * device-visible addresses: the IOVA reservation is transferred from
+	 * the old to the new tt, so the existing GPU PTEs stay valid and the
+	 * BO must not be marked evicted (the rebind paths in xe_exec_ioctl()
+	 * and preempt_rebind_work_func() would otherwise needlessly rebind).
+	 * Preempt fences still need to be triggered, and fault-mode / L2
+	 * flush-optimized configs still need a TLB invalidation below, so only
+	 * the eviction marking is skipped.
+	 */
+	defrag_iova = xe_bo_defrag_use_iova(&bo->ttm);
+
 	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
 		struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
 		struct drm_gpuva *gpuva;
 
 		if (!xe_vm_in_fault_mode(vm)) {
-			drm_gpuvm_bo_evict(vm_bo, true);
+			if (!defrag_iova)
+				drm_gpuvm_bo_evict(vm_bo, true);
 			/*
 			 * L2 cache may not be flushed, so ensure that is done in
 			 * xe_vm_invalidate_vma() below
@@ -1394,6 +1676,8 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 			if (!ctx->no_wait_gpu &&
 			    !xe_vm_flush_vm_bo_tlb_async(vm, bo, vm_bo))
 				continue;
+			else if (defrag_iova)
+				drm_gpuvm_bo_evict(vm_bo, true);
 		}
 
 		if (!idle) {
@@ -2120,6 +2404,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	bool needs_clear;
 	bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) &&
 				  ttm && ttm_tt_is_populated(ttm)) ? true : false;
+	bool defrag_iova = xe_bo_defrag_use_iova(ttm_bo);
 	int ret = 0;
 
 	/*
@@ -2168,7 +2453,12 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
 		(!ttm && ttm_bo->type == ttm_bo_type_device);
 
-	if (new_mem->mem_type == XE_PL_TT) {
+	/*
+	 * For a defrag move using the packed IOVA path the destination tt is
+	 * not fully mapped here; only the changed pages are mapped (packed) for
+	 * the copy, and the finalize job establishes the full mapping.
+	 */
+	if (new_mem->mem_type == XE_PL_TT && !defrag_iova) {
 		ret = xe_tt_map(xe, ttm);
 		if (ret)
 			goto out;
@@ -2278,9 +2568,16 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 
 		fence = xe_migrate_clear(migrate, bo, new_mem, flags);
 	} else if (ttm_bo->defrag_old_tt) {
-		fence = xe_migrate_copy_defrag(migrate, bo, old_mem, new_mem,
-					       ttm_bo->defrag_old_tt,
-					       handle_system_ccs);
+		if (defrag_iova)
+			fence = xe_bo_defrag_iova_copy(xe, migrate, bo, old_mem,
+						       new_mem,
+						       ttm_bo->defrag_old_tt,
+						       handle_system_ccs);
+		else
+			fence = xe_migrate_copy_defrag(migrate, bo, old_mem,
+						       new_mem,
+						       ttm_bo->defrag_old_tt,
+						       handle_system_ccs);
 	} else {
 		fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem,
 					handle_system_ccs);
Signature · 1 line
2.34.1
#131 lore raw

Re: [PATCH v3 32/33] drm/xe: Finalize defrag-IOVA moves with post-copy job

Quoted 348 lines · Matthew Brost

Wire up end-to-end defrag for IOVA-mapped BOs. A defrag move is eligible whenever the destination TT is IOVA mapped. The packed-copy step moves only changed pages; the IOVA reservation is transferred to the new backing so device-visible addresses are preserved, avoiding GPU rebind overhead.

Add xe_bo_defrag_use_iova() to select the IOVA defrag path. Add xe_bo_defrag_iova_copy() to drive the move: diff new vs stashed old TT, link changed pages packed, blit them, then queue a post-copy finalize job.

The finalize job tears down the temporary packed mapping, transfers the full old TT reservation to the new one, and relinks the copied pages at natural offsets.

Borrowed pages skip the copy and simply adopt the old reservation.

Since reservation transfer preserves device-visible addresses, xe_bo_trigger_rebind() skips drm_gpuvm_bo_evict() marking; GPU PTEs stay valid.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

v3:
- Check dma_iova_sync(), wait the copy fence before unlink on error, and gate the fast path on old_tt->use_iova (Sashiko)
---
drivers/gpu/drm/xe/xe_bo.c | 325 +++++++++++++++++++++++++++++++++++--

1 file changed, 311 insertions(+), 14 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +200 -9
index c087f17ad6db..36290f58ccc1 100644
@@ -13,6 +13,7 @@
 #include <drm/drm_dumb_buffers.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/drm_managed.h>
+#include <drm/gpu_scheduler.h>
 #include <drm/ttm/ttm_backup.h>
 #include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_placement.h>
@@ -25,6 +26,7 @@
 #include <trace/events/gpu_mem.h>
 
 #include "xe_device.h"
+#include "xe_dep_job_types.h"
 #include "xe_dep_scheduler.h"
 #include "xe_dma_buf.h"
 #include "xe_drm_client.h"
@@ -542,7 +544,6 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 * @xe_tt->iova_dma_pages[]. Used to (re)derive accounting that exactly matches
 * the pages a tt is actually mapped with.
 */
-__maybe_unused
 static void xe_tt_account_iova_pages(struct xe_device *xe,
 				     struct xe_ttm_tt *xe_tt)
 {
@@ -567,7 +568,6 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 {
 }
 
-__maybe_unused
 static void xe_tt_account_iova_pages(struct xe_device *xe,
 				     struct xe_ttm_tt *xe_tt)
 {
@@ -806,7 +806,6 @@ struct xe_tt_iova_copy {
 * differ. Coalesce contiguous changed folios into runs and, for each run,
 * invoke @fn. Returns the total number of changed pages.
 */
-__maybe_unused
 static pgoff_t xe_tt_iova_walk_changed(struct ttm_tt *new_tt,
 				       struct ttm_tt *old_tt,
 				       void (*fn)(pgoff_t start, pgoff_t count,
@@ -855,7 +854,6 @@ struct xe_tt_iova_run_collect {
 };
 
 /* Geometrically grow @arr (element size @elem) to hold at least @need slots. */
-__maybe_unused
 static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
 			   size_t elem)
 {
@@ -878,7 +876,6 @@ static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
 	return 0;
 }
 
-__maybe_unused
 static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
 {
 	struct xe_tt_iova_run_collect *c = arg;
@@ -933,7 +930,6 @@ static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
 *
 * Return: 0 on success, -ENOMEM on allocation failure.
 */
-__maybe_unused
 static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
 				struct xe_tt_iova_copy *copy)
 {
@@ -969,7 +965,6 @@ static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
 * xe_tt_iova_copy_fini() - Release a struct xe_tt_iova_copy
 * @copy: The copy description to release.
 */
-__maybe_unused
 static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
 {
 	kfree(copy->runs);
@@ -1000,7 +995,6 @@ static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
 * Return: 0 on success, negative error code on failure (any partial linkage is
 * unwound).
 */
-__maybe_unused
 static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
 			       const struct xe_tt_iova_copy *copy)
 {
@@ -1047,6 +1041,133 @@ static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
 	return ret;
 }
 
+/**
+ * struct xe_iova_defrag_job - Post-copy finalize job for an IOVA defrag move.
+ *
+ * After the GPU defrag copy into the packed copy-step mapping completes, this
+ * dependency job runs the CPU-side IOMMU fixups at the point of no failure:
+ * tear down the temporary packed mapping, then per changed run unlink the old
+ * pages and link the new pages into the (transferred) reservation at their
+ * natural offset.
+ */
+struct xe_iova_defrag_job {
+	/** @dep: base generic dependency Xe job. */
+	struct xe_dep_job dep;
+	/** @xe: The xe device. */
+	struct xe_device *xe;
+	/** @new_tt: The destination tt being finalized (owns the reservation). */
+	struct ttm_tt *new_tt;
+	/**
+	 * @copy_iova: Snapshot of the new tt's original IOVA reservation that
+	 * held the temporary packed copy-step mapping. Destroyed by the job
+	 * (the new tt itself adopts the old tt's reservation at job creation).
+	 */
+	struct dma_iova_state copy_iova;
+	/** @copy_bytes: Byte length of the packed copy-step mapping. */
+	size_t copy_bytes;
+	/** @copy: Changed-page description; the job owns and frees @copy.runs. */
+	struct xe_tt_iova_copy copy;
+};
+
+static struct dma_fence *xe_iova_defrag_job_run(struct xe_dep_job *dep_job)
+{
+	struct xe_iova_defrag_job *job =
+		container_of(dep_job, typeof(*job), dep);
+	struct xe_device *xe = job->xe;
+	struct device *dev = xe->drm.dev;
+	struct xe_ttm_tt *xe_tt =
+		container_of(job->new_tt, struct xe_ttm_tt, ttm);
+	unsigned int r, l;
+
+	/*
+	 * Tear down the temporary packed copy-step mapping and free its
+	 * reservation. The new tt has already adopted the old tt's full
+	 * reservation (transferred at job creation), so this only touches the
+	 * snapshot taken before the transfer.
+	 *
+	 * This mapping was the GPU blit's write destination, so tear it down
+	 * with attrs 0 (not DMA_ATTR_SKIP_CPU_SYNC): for non-coherent or
+	 * bounced (SWIOTLB) mappings the device->CPU copy-back / cache sync of
+	 * the freshly copied data happens during unlink, exactly as
+	 * xe_tt_unmap_iova() does. Skipping it would silently drop the
+	 * defragmented data on those configs.
+	 */
+	if (job->copy_bytes)
+		dma_iova_destroy(dev, &job->copy_iova, job->copy_bytes,
+				 DMA_BIDIRECTIONAL, 0);
+	else
+		dma_iova_free(dev, &job->copy_iova);
+
+	/*
+	 * For each changed run, unlink the old pages (still linked from the
+	 * adopted reservation).
+	 */
+	for (r = 0; r < job->copy.nr_runs; r++) {
+		size_t off = (size_t)job->copy.runs[r].start << PAGE_SHIFT;
+		size_t len = (size_t)job->copy.runs[r].count << PAGE_SHIFT;
+
+		dma_iova_unlink(dev, &xe_tt->iova, off, len,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+	}
+
+	/*
+	 * Relink the freshly copied new pages from the snapshot captured at job
+	 * creation. The destination tt's page array may have been mutated (or
+	 * torn down) by TTM since then, so it is never dereferenced here.
+	 * Borrowed pages are shared with the old tt and stay correctly linked
+	 * for free, so the snapshot only covers the changed runs.
+	 */
+	for (l = 0; l < job->copy.nr_links; l++) {
+		int ret = dma_iova_link(dev, &xe_tt->iova,
+					job->copy.links[l].phys,
+					job->copy.links[l].offset,
+					job->copy.links[l].len,
+					DMA_BIDIRECTIONAL,
+					DMA_ATTR_SKIP_CPU_SYNC);
+		/*
+		 * Point of no failure: the data is already copied and the
+		 * reservation ownership has been transferred. A relink failure
+		 * leaves a hole in the mapping; there is no safe recovery, so
+		 * just warn.
+		 */
+		drm_WARN_ON(&xe->drm, ret);
+	}
+
+	/* Sync the relinked changed runs into the device's view. */
+	for (r = 0; r < job->copy.nr_runs; r++) {
+		size_t off = (size_t)job->copy.runs[r].start << PAGE_SHIFT;
+		size_t len = (size_t)job->copy.runs[r].count << PAGE_SHIFT;
+
+		/*
+		 * Point of no failure (as with the relink above): warn on a
+		 * sync error so a stale IOMMU cache does not fail silently.
+		 */
+		drm_WARN_ON(&xe->drm, dma_iova_sync(dev, &xe_tt->iova, off, len));
+	}
+
+	/*
+	 * All work is synchronous CPU-side IOMMU fixup, so there is no hardware
+	 * fence to wait on; returning NULL makes the scheduler signal the job's
+	 * finished fence immediately.
+	 */
+	return NULL;
+}
+
+static void xe_iova_defrag_job_free(struct xe_dep_job *dep_job)
+{
+	struct xe_iova_defrag_job *job =
+		container_of(dep_job, typeof(*job), dep);
+
+	xe_tt_iova_copy_fini(&job->copy);
+	drm_sched_job_cleanup(&job->dep.drm);
+	kfree(job);
+}
+
+static const struct xe_dep_job_ops xe_iova_defrag_job_ops = {
+	.run_job = xe_iova_defrag_job_run,
+	.free_job = xe_iova_defrag_job_free,
+};
+
 /*
 * xe_tt_iova_transfer - Transfer a full IOVA reservation old_tt -> new_tt.
 *
@@ -1063,7 +1184,6 @@ static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
 * folio walk of the new tt keeps xe->mem.dma_mapped_pages[] exact while the new
 * mapping lives, rather than carrying the old tt's stale distribution.
 */
-__maybe_unused
 static void xe_tt_iova_transfer(struct xe_device *xe,
 				struct xe_ttm_tt *new_tt,
 				struct xe_ttm_tt *old_tt)
@@ -1078,6 +1198,154 @@ static void xe_tt_iova_transfer(struct xe_device *xe,
 	old_tt->use_iova = false;
 }
 
+/*
+ * xe_bo_defrag_iova_copy - Run an IOVA defrag move with a packed copy mapping.
+ *
+ * Only the changed (non-borrowed) pages of @new_tt are DMA mapped, packed
+ * contiguously into the front of its reservation, the GPU copy blits them from
+ * the old tt at natural offsets, and a post-copy finalize job adopts the old
+ * tt's full reservation and relinks the changed runs. Returns the finalize
+ * job's finished fence (the fence the move waits on), or an ERR_PTR on failure
+ * with all partial state unwound.
+ */
+static struct dma_fence *
+xe_bo_defrag_iova_copy(struct xe_device *xe, struct xe_migrate *migrate,
+		       struct xe_bo *bo, struct ttm_resource *old_mem,
+		       struct ttm_resource *new_mem, struct ttm_tt *old_tt,
+		       bool handle_system_ccs)
+{
+	struct ttm_tt *new_tt = bo->ttm.ttm;
+	struct xe_ttm_tt *new_xe_tt = container_of(new_tt, struct xe_ttm_tt, ttm);
+	struct xe_ttm_tt *old_xe_tt = container_of(old_tt, struct xe_ttm_tt, ttm);
+	struct drm_sched_entity *entity =
+		xe_dep_scheduler_entity(xe->mem.defrag.iova_sched);
+	struct xe_iova_defrag_job *job;
+	struct dma_fence *copy_fence;
+	struct dma_fence *fence;
+	struct xe_tt_iova_copy copy;
+	bool packed = false;
+	int err;
+
+	xe_assert(xe, new_xe_tt->use_iova && !new_xe_tt->iova_linked);
+	xe_assert(xe, old_xe_tt->use_iova && old_xe_tt->iova_linked);
+
+	err = xe_tt_iova_copy_init(new_tt, old_tt, &copy);
+	if (err)
+		return ERR_PTR(err);
+
+	if (copy.nr_changed) {
+		err = xe_tt_map_iova_copy(xe, new_tt, &copy);
+		if (err)
+			goto err_copy;
+		packed = true;
+
+		copy_fence = xe_migrate_copy_defrag_iova(migrate, bo, old_mem,
+							 new_mem, old_tt,
+							 copy.nr_changed,
+							 handle_system_ccs);
+	} else {
+		copy_fence = dma_fence_get_stub();
+	}
+	if (IS_ERR(copy_fence)) {
+		err = PTR_ERR(copy_fence);
+		goto err_unmap;
+	}
+
+	job = kzalloc(sizeof(*job), GFP_KERNEL);
+	if (!job) {
+		err = -ENOMEM;
+		goto err_fence;
+	}
+
+	job->dep.ops = &xe_iova_defrag_job_ops;
+	job->xe = xe;
+	job->new_tt = new_tt;
+	job->copy_iova = new_xe_tt->iova;
+	job->copy_bytes = (size_t)copy.nr_changed << PAGE_SHIFT;
+	job->copy = copy;
+
+	err = drm_sched_job_init(&job->dep.drm, entity, 1, NULL, 0);
+	if (err)
+		goto err_job;
+
+	err = drm_sched_job_add_dependency(&job->dep.drm,

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_iova_transfer(struct xe_device *xe, · line 1281

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+					   dma_fence_get(copy_fence));
+	if (err)
+		goto err_job_init;
+
+	/*
+	 * Point of no failure: transfer the old tt's full reservation to the
+	 * new tt. The finalize job (already armed below) destroys the packed
+	 * copy mapping snapshot and relinks the changed runs.
+	 */
+	xe_tt_iova_transfer(xe, new_xe_tt, old_xe_tt);
Assisted-by LLM

We transfer the ownership here without waiting for blit to complete. Probbly we should wait for copy fence to complete?

Niranjana

Quoted 82 lines
drivers/gpu/drm/xe/xe_bo.c +68 -0
+	memset(&copy, 0, sizeof(copy)); /* ownership moved to the job */
+
+	drm_sched_job_arm(&job->dep.drm);
+	fence = dma_fence_get(&job->dep.drm.s_fence->finished);
+	drm_sched_entity_push_job(&job->dep.drm);
+
+	dma_fence_put(copy_fence);
+
+	return fence;
+
+err_job_init:
+	drm_sched_job_cleanup(&job->dep.drm);
+err_job:
+	kfree(job);
+err_fence:
+	/*
+	 * The GPU copy into the packed destination mapping was already
+	 * submitted; it must finish before we tear that mapping down below,
+	 * otherwise the blit would fault or scribble on unmapped IOVA. (Reached
+	 * only with a valid, submitted copy_fence; the IS_ERR(copy_fence) case
+	 * jumps straight to err_unmap with no GPU work in flight.)
+	 */
+	if (packed)
+		dma_fence_wait(copy_fence, false);
+	dma_fence_put(copy_fence);
+err_unmap:
+	if (packed)
+		dma_iova_unlink(xe->drm.dev, &new_xe_tt->iova, 0,
+				(size_t)copy.nr_changed << PAGE_SHIFT,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+err_copy:
+	xe_tt_iova_copy_fini(&copy);
+	return ERR_PTR(err);
+}
+
+/*
+ * xe_bo_defrag_use_iova - Whether a defrag move should use the packed IOVA path.
+ *
+ * Eligible when the move is a defrag (stashed old tt) and the destination tt is
+ * IOVA mapped. The IOVA reservation is transferred to the new backing, so the
+ * device-visible addresses are preserved and no GPU rebind is needed (see
+ * xe_bo_trigger_rebind()); this makes the path worthwhile regardless of BO
+ * size. A flat-CCS aux pass, if needed, is migrated packed alongside the data
+ * (changed pages only), so it does not disqualify the path. Otherwise the
+ * full-mapping defrag path is used.
+ */
+static bool xe_bo_defrag_use_iova(struct ttm_buffer_object *ttm_bo)
+{
+	struct xe_ttm_tt *xe_tt, *old_xe_tt;
+
+	if (!ttm_bo->defrag_old_tt || !ttm_bo->ttm)
+		return false;
+
+	xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt, ttm);
+	old_xe_tt = container_of(ttm_bo->defrag_old_tt, struct xe_ttm_tt, ttm);
+
+	/*
+	 * Both tts must be IOVA mapped: the packed copy reads the old tt's IOVA
+	 * and the finalize job transfers the old tt's reservation to the new
+	 * one. use_iova is a deterministic per-device decision taken at tt
+	 * creation, so for a given BO the old and new tt always agree; the
+	 * explicit old-tt check guards against ever routing a sg-fallback old
+	 * tt into xe_tt_iova_transfer() (which would copy an invalid iova).
+	 */
+	return xe_tt->use_iova && old_xe_tt->use_iova;
+}
+
 /*
 * Account ttm pages against the device shrinker's shrinkable and
 * purgeable counts.
@@ -1357,6 +1625,7 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 	struct drm_gem_object *obj = &bo->ttm.base;
 	struct drm_gpuvm_bo *vm_bo;
 	bool idle = false;
+	bool defrag_iova;
 	int ret = 0;
 
 	dma_resv_assert_held(bo->ttm.base.resv);
@@ -1369,12 +1638,25 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 		dma_resv_iter_end(&cursor);
 	}
 

drivers/gpu/drm/xe/xe_bo.c static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo, · line 1650

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+	/*
+	 * A defrag move that reallocates IOVA-mapped backing preserves the
+	 * device-visible addresses: the IOVA reservation is transferred from
+	 * the old to the new tt, so the existing GPU PTEs stay valid and the
+	 * BO must not be marked evicted (the rebind paths in xe_exec_ioctl()
+	 * and preempt_rebind_work_func() would otherwise needlessly rebind).
+	 * Preempt fences still need to be triggered, and fault-mode / L2
+	 * flush-optimized configs still need a TLB invalidation below, so only
+	 * the eviction marking is skipped.
+	 */

The commit is bit confusing compared to the code below. Eviction marking is not completely skipped, it is only skipped when it is not needed. May be update the comment?

Niranjana

Quoted 67 lines
drivers/gpu/drm/xe/xe_bo.c +23 -5
+	defrag_iova = xe_bo_defrag_use_iova(&bo->ttm);
+
 	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
 		struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
 		struct drm_gpuva *gpuva;
 
 		if (!xe_vm_in_fault_mode(vm)) {
-			drm_gpuvm_bo_evict(vm_bo, true);
+			if (!defrag_iova)
+				drm_gpuvm_bo_evict(vm_bo, true);
 			/*
 			 * L2 cache may not be flushed, so ensure that is done in
 			 * xe_vm_invalidate_vma() below
@@ -1394,6 +1676,8 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 			if (!ctx->no_wait_gpu &&
 			    !xe_vm_flush_vm_bo_tlb_async(vm, bo, vm_bo))
 				continue;
+			else if (defrag_iova)
+				drm_gpuvm_bo_evict(vm_bo, true);
 		}
 
 		if (!idle) {
@@ -2120,6 +2404,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	bool needs_clear;
 	bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) &&
 				  ttm && ttm_tt_is_populated(ttm)) ? true : false;
+	bool defrag_iova = xe_bo_defrag_use_iova(ttm_bo);
 	int ret = 0;
 
 	/*
@@ -2168,7 +2453,12 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
 		(!ttm && ttm_bo->type == ttm_bo_type_device);
 
-	if (new_mem->mem_type == XE_PL_TT) {
+	/*
+	 * For a defrag move using the packed IOVA path the destination tt is
+	 * not fully mapped here; only the changed pages are mapped (packed) for
+	 * the copy, and the finalize job establishes the full mapping.
+	 */
+	if (new_mem->mem_type == XE_PL_TT && !defrag_iova) {
 		ret = xe_tt_map(xe, ttm);
 		if (ret)
 			goto out;
@@ -2278,9 +2568,16 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 
 		fence = xe_migrate_clear(migrate, bo, new_mem, flags);
 	} else if (ttm_bo->defrag_old_tt) {
-		fence = xe_migrate_copy_defrag(migrate, bo, old_mem, new_mem,
-					       ttm_bo->defrag_old_tt,
-					       handle_system_ccs);
+		if (defrag_iova)
+			fence = xe_bo_defrag_iova_copy(xe, migrate, bo, old_mem,
+						       new_mem,
+						       ttm_bo->defrag_old_tt,
+						       handle_system_ccs);
+		else
+			fence = xe_migrate_copy_defrag(migrate, bo, old_mem,
+						       new_mem,
+						       ttm_bo->defrag_old_tt,
+						       handle_system_ccs);
 	} else {
 		fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem,
 					handle_system_ccs);
Signature · 1 line
2.34.1
author maintainer
#132 lore raw

Re: [PATCH v3 32/33] drm/xe: Finalize defrag-IOVA moves with post-copy job

Quoted 362 lines · Niranjana Vishwanathapura
Quoted 348 lines · Matthew Brost

Wire up end-to-end defrag for IOVA-mapped BOs. A defrag move is eligible whenever the destination TT is IOVA mapped. The packed-copy step moves only changed pages; the IOVA reservation is transferred to the new backing so device-visible addresses are preserved, avoiding GPU rebind overhead.

Add xe_bo_defrag_use_iova() to select the IOVA defrag path. Add xe_bo_defrag_iova_copy() to drive the move: diff new vs stashed old TT, link changed pages packed, blit them, then queue a post-copy finalize job.

The finalize job tears down the temporary packed mapping, transfers the full old TT reservation to the new one, and relinks the copied pages at natural offsets.

Borrowed pages skip the copy and simply adopt the old reservation.

Since reservation transfer preserves device-visible addresses, xe_bo_trigger_rebind() skips drm_gpuvm_bo_evict() marking; GPU PTEs stay valid.

Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

v3:
- Check dma_iova_sync(), wait the copy fence before unlink on error, and gate the fast path on old_tt->use_iova (Sashiko)
---
drivers/gpu/drm/xe/xe_bo.c | 325 +++++++++++++++++++++++++++++++++++--

1 file changed, 311 insertions(+), 14 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +200 -9
index c087f17ad6db..36290f58ccc1 100644
@@ -13,6 +13,7 @@
 #include <drm/drm_dumb_buffers.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/drm_managed.h>
+#include <drm/gpu_scheduler.h>
 #include <drm/ttm/ttm_backup.h>
 #include <drm/ttm/ttm_device.h>
 #include <drm/ttm/ttm_placement.h>
@@ -25,6 +26,7 @@
 #include <trace/events/gpu_mem.h>
 
 #include "xe_device.h"
+#include "xe_dep_job_types.h"
 #include "xe_dep_scheduler.h"
 #include "xe_dma_buf.h"
 #include "xe_drm_client.h"
@@ -542,7 +544,6 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 * @xe_tt->iova_dma_pages[]. Used to (re)derive accounting that exactly matches
 * the pages a tt is actually mapped with.
 */
-__maybe_unused
 static void xe_tt_account_iova_pages(struct xe_device *xe,
 				     struct xe_ttm_tt *xe_tt)
 {
@@ -567,7 +568,6 @@ static void xe_tt_unaccount_iova(struct xe_device *xe,
 {
 }
 
-__maybe_unused
 static void xe_tt_account_iova_pages(struct xe_device *xe,
 				     struct xe_ttm_tt *xe_tt)
 {
@@ -806,7 +806,6 @@ struct xe_tt_iova_copy {
 * differ. Coalesce contiguous changed folios into runs and, for each run,
 * invoke @fn. Returns the total number of changed pages.
 */
-__maybe_unused
 static pgoff_t xe_tt_iova_walk_changed(struct ttm_tt *new_tt,
 				       struct ttm_tt *old_tt,
 				       void (*fn)(pgoff_t start, pgoff_t count,
@@ -855,7 +854,6 @@ struct xe_tt_iova_run_collect {
 };
 
 /* Geometrically grow @arr (element size @elem) to hold at least @need slots. */
-__maybe_unused
 static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
 			   size_t elem)
 {
@@ -878,7 +876,6 @@ static int xe_tt_iova_grow(void **arr, unsigned int *cap, unsigned int need,
 	return 0;
 }
 
-__maybe_unused
 static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
 {
 	struct xe_tt_iova_run_collect *c = arg;
@@ -933,7 +930,6 @@ static void xe_tt_iova_store_run(pgoff_t start, pgoff_t count, void *arg)
 *
 * Return: 0 on success, -ENOMEM on allocation failure.
 */
-__maybe_unused
 static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
 				struct xe_tt_iova_copy *copy)
 {
@@ -969,7 +965,6 @@ static int xe_tt_iova_copy_init(struct ttm_tt *new_tt, struct ttm_tt *old_tt,
 * xe_tt_iova_copy_fini() - Release a struct xe_tt_iova_copy
 * @copy: The copy description to release.
 */
-__maybe_unused
 static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
 {
 	kfree(copy->runs);
@@ -1000,7 +995,6 @@ static void xe_tt_iova_copy_fini(struct xe_tt_iova_copy *copy)
 * Return: 0 on success, negative error code on failure (any partial linkage is
 * unwound).
 */
-__maybe_unused
 static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
 			       const struct xe_tt_iova_copy *copy)
 {
@@ -1047,6 +1041,133 @@ static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
 	return ret;
 }
 
+/**
+ * struct xe_iova_defrag_job - Post-copy finalize job for an IOVA defrag move.
+ *
+ * After the GPU defrag copy into the packed copy-step mapping completes, this
+ * dependency job runs the CPU-side IOMMU fixups at the point of no failure:
+ * tear down the temporary packed mapping, then per changed run unlink the old
+ * pages and link the new pages into the (transferred) reservation at their
+ * natural offset.
+ */
+struct xe_iova_defrag_job {
+	/** @dep: base generic dependency Xe job. */
+	struct xe_dep_job dep;
+	/** @xe: The xe device. */
+	struct xe_device *xe;
+	/** @new_tt: The destination tt being finalized (owns the reservation). */
+	struct ttm_tt *new_tt;
+	/**
+	 * @copy_iova: Snapshot of the new tt's original IOVA reservation that
+	 * held the temporary packed copy-step mapping. Destroyed by the job
+	 * (the new tt itself adopts the old tt's reservation at job creation).
+	 */
+	struct dma_iova_state copy_iova;
+	/** @copy_bytes: Byte length of the packed copy-step mapping. */
+	size_t copy_bytes;
+	/** @copy: Changed-page description; the job owns and frees @copy.runs. */
+	struct xe_tt_iova_copy copy;
+};
+
+static struct dma_fence *xe_iova_defrag_job_run(struct xe_dep_job *dep_job)
+{
+	struct xe_iova_defrag_job *job =
+		container_of(dep_job, typeof(*job), dep);
+	struct xe_device *xe = job->xe;
+	struct device *dev = xe->drm.dev;
+	struct xe_ttm_tt *xe_tt =
+		container_of(job->new_tt, struct xe_ttm_tt, ttm);
+	unsigned int r, l;
+
+	/*
+	 * Tear down the temporary packed copy-step mapping and free its
+	 * reservation. The new tt has already adopted the old tt's full
+	 * reservation (transferred at job creation), so this only touches the
+	 * snapshot taken before the transfer.
+	 *
+	 * This mapping was the GPU blit's write destination, so tear it down
+	 * with attrs 0 (not DMA_ATTR_SKIP_CPU_SYNC): for non-coherent or
+	 * bounced (SWIOTLB) mappings the device->CPU copy-back / cache sync of
+	 * the freshly copied data happens during unlink, exactly as
+	 * xe_tt_unmap_iova() does. Skipping it would silently drop the
+	 * defragmented data on those configs.
+	 */
+	if (job->copy_bytes)
+		dma_iova_destroy(dev, &job->copy_iova, job->copy_bytes,
+				 DMA_BIDIRECTIONAL, 0);
+	else
+		dma_iova_free(dev, &job->copy_iova);
+
+	/*
+	 * For each changed run, unlink the old pages (still linked from the
+	 * adopted reservation).
+	 */
+	for (r = 0; r < job->copy.nr_runs; r++) {
+		size_t off = (size_t)job->copy.runs[r].start << PAGE_SHIFT;
+		size_t len = (size_t)job->copy.runs[r].count << PAGE_SHIFT;
+
+		dma_iova_unlink(dev, &xe_tt->iova, off, len,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+	}
+
+	/*
+	 * Relink the freshly copied new pages from the snapshot captured at job
+	 * creation. The destination tt's page array may have been mutated (or
+	 * torn down) by TTM since then, so it is never dereferenced here.
+	 * Borrowed pages are shared with the old tt and stay correctly linked
+	 * for free, so the snapshot only covers the changed runs.
+	 */
+	for (l = 0; l < job->copy.nr_links; l++) {
+		int ret = dma_iova_link(dev, &xe_tt->iova,
+					job->copy.links[l].phys,
+					job->copy.links[l].offset,
+					job->copy.links[l].len,
+					DMA_BIDIRECTIONAL,
+					DMA_ATTR_SKIP_CPU_SYNC);
+		/*
+		 * Point of no failure: the data is already copied and the
+		 * reservation ownership has been transferred. A relink failure
+		 * leaves a hole in the mapping; there is no safe recovery, so
+		 * just warn.
+		 */
+		drm_WARN_ON(&xe->drm, ret);
+	}
+
+	/* Sync the relinked changed runs into the device's view. */
+	for (r = 0; r < job->copy.nr_runs; r++) {
+		size_t off = (size_t)job->copy.runs[r].start << PAGE_SHIFT;
+		size_t len = (size_t)job->copy.runs[r].count << PAGE_SHIFT;
+
+		/*
+		 * Point of no failure (as with the relink above): warn on a
+		 * sync error so a stale IOMMU cache does not fail silently.
+		 */
+		drm_WARN_ON(&xe->drm, dma_iova_sync(dev, &xe_tt->iova, off, len));
+	}
+
+	/*
+	 * All work is synchronous CPU-side IOMMU fixup, so there is no hardware
+	 * fence to wait on; returning NULL makes the scheduler signal the job's
+	 * finished fence immediately.
+	 */
+	return NULL;
+}
+
+static void xe_iova_defrag_job_free(struct xe_dep_job *dep_job)
+{
+	struct xe_iova_defrag_job *job =
+		container_of(dep_job, typeof(*job), dep);
+
+	xe_tt_iova_copy_fini(&job->copy);
+	drm_sched_job_cleanup(&job->dep.drm);
+	kfree(job);
+}
+
+static const struct xe_dep_job_ops xe_iova_defrag_job_ops = {
+	.run_job = xe_iova_defrag_job_run,
+	.free_job = xe_iova_defrag_job_free,
+};
+
 /*
 * xe_tt_iova_transfer - Transfer a full IOVA reservation old_tt -> new_tt.
 *
@@ -1063,7 +1184,6 @@ static int xe_tt_map_iova_copy(struct xe_device *xe, struct ttm_tt *new_tt,
 * folio walk of the new tt keeps xe->mem.dma_mapped_pages[] exact while the new
 * mapping lives, rather than carrying the old tt's stale distribution.
 */
-__maybe_unused
 static void xe_tt_iova_transfer(struct xe_device *xe,
 				struct xe_ttm_tt *new_tt,
 				struct xe_ttm_tt *old_tt)
@@ -1078,6 +1198,154 @@ static void xe_tt_iova_transfer(struct xe_device *xe,
 	old_tt->use_iova = false;
 }
 
+/*
+ * xe_bo_defrag_iova_copy - Run an IOVA defrag move with a packed copy mapping.
+ *
+ * Only the changed (non-borrowed) pages of @new_tt are DMA mapped, packed
+ * contiguously into the front of its reservation, the GPU copy blits them from
+ * the old tt at natural offsets, and a post-copy finalize job adopts the old
+ * tt's full reservation and relinks the changed runs. Returns the finalize
+ * job's finished fence (the fence the move waits on), or an ERR_PTR on failure
+ * with all partial state unwound.
+ */
+static struct dma_fence *
+xe_bo_defrag_iova_copy(struct xe_device *xe, struct xe_migrate *migrate,
+		       struct xe_bo *bo, struct ttm_resource *old_mem,
+		       struct ttm_resource *new_mem, struct ttm_tt *old_tt,
+		       bool handle_system_ccs)
+{
+	struct ttm_tt *new_tt = bo->ttm.ttm;
+	struct xe_ttm_tt *new_xe_tt = container_of(new_tt, struct xe_ttm_tt, ttm);
+	struct xe_ttm_tt *old_xe_tt = container_of(old_tt, struct xe_ttm_tt, ttm);
+	struct drm_sched_entity *entity =
+		xe_dep_scheduler_entity(xe->mem.defrag.iova_sched);
+	struct xe_iova_defrag_job *job;
+	struct dma_fence *copy_fence;
+	struct dma_fence *fence;
+	struct xe_tt_iova_copy copy;
+	bool packed = false;
+	int err;
+
+	xe_assert(xe, new_xe_tt->use_iova && !new_xe_tt->iova_linked);
+	xe_assert(xe, old_xe_tt->use_iova && old_xe_tt->iova_linked);
+
+	err = xe_tt_iova_copy_init(new_tt, old_tt, &copy);
+	if (err)
+		return ERR_PTR(err);
+
+	if (copy.nr_changed) {
+		err = xe_tt_map_iova_copy(xe, new_tt, &copy);
+		if (err)
+			goto err_copy;
+		packed = true;
+
+		copy_fence = xe_migrate_copy_defrag_iova(migrate, bo, old_mem,
+							 new_mem, old_tt,
+							 copy.nr_changed,
+							 handle_system_ccs);
+	} else {
+		copy_fence = dma_fence_get_stub();
+	}
+	if (IS_ERR(copy_fence)) {
+		err = PTR_ERR(copy_fence);
+		goto err_unmap;
+	}
+
+	job = kzalloc(sizeof(*job), GFP_KERNEL);
+	if (!job) {
+		err = -ENOMEM;
+		goto err_fence;
+	}
+
+	job->dep.ops = &xe_iova_defrag_job_ops;
+	job->xe = xe;
+	job->new_tt = new_tt;
+	job->copy_iova = new_xe_tt->iova;
+	job->copy_bytes = (size_t)copy.nr_changed << PAGE_SHIFT;
+	job->copy = copy;
+
+	err = drm_sched_job_init(&job->dep.drm, entity, 1, NULL, 0);
+	if (err)
+		goto err_job;
+
+	err = drm_sched_job_add_dependency(&job->dep.drm,

drivers/gpu/drm/xe/xe_bo.c static void xe_tt_iova_transfer(struct xe_device *xe, · line 1281

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+					   dma_fence_get(copy_fence));
+	if (err)
+		goto err_job_init;
+
+	/*
+	 * Point of no failure: transfer the old tt's full reservation to the
+	 * new tt. The finalize job (already armed below) destroys the packed
+	 * copy mapping snapshot and relinks the changed runs.
+	 */
+	xe_tt_iova_transfer(xe, new_xe_tt, old_xe_tt);
Assisted-by LLM
Quoted 3 lines

We transfer the ownership here without waiting for blit to complete. Probbly we should wait for copy fence to complete?

Most definitely not. This is an async pipeline, we don't block on anything by design - blocking anywhere would break the entire design as we'd miss page flips.

Here once we can't fail, we update the bookkeeping of the TTs so we can immediately issue another async job behind this will the current one is in flight.

Quoted 84 lines

Niranjana

Quoted 82 lines
drivers/gpu/drm/xe/xe_bo.c +68 -0
+	memset(&copy, 0, sizeof(copy)); /* ownership moved to the job */
+
+	drm_sched_job_arm(&job->dep.drm);
+	fence = dma_fence_get(&job->dep.drm.s_fence->finished);
+	drm_sched_entity_push_job(&job->dep.drm);
+
+	dma_fence_put(copy_fence);
+
+	return fence;
+
+err_job_init:
+	drm_sched_job_cleanup(&job->dep.drm);
+err_job:
+	kfree(job);
+err_fence:
+	/*
+	 * The GPU copy into the packed destination mapping was already
+	 * submitted; it must finish before we tear that mapping down below,
+	 * otherwise the blit would fault or scribble on unmapped IOVA. (Reached
+	 * only with a valid, submitted copy_fence; the IS_ERR(copy_fence) case
+	 * jumps straight to err_unmap with no GPU work in flight.)
+	 */
+	if (packed)
+		dma_fence_wait(copy_fence, false);
+	dma_fence_put(copy_fence);
+err_unmap:
+	if (packed)
+		dma_iova_unlink(xe->drm.dev, &new_xe_tt->iova, 0,
+				(size_t)copy.nr_changed << PAGE_SHIFT,
+				DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
+err_copy:
+	xe_tt_iova_copy_fini(&copy);
+	return ERR_PTR(err);
+}
+
+/*
+ * xe_bo_defrag_use_iova - Whether a defrag move should use the packed IOVA path.
+ *
+ * Eligible when the move is a defrag (stashed old tt) and the destination tt is
+ * IOVA mapped. The IOVA reservation is transferred to the new backing, so the
+ * device-visible addresses are preserved and no GPU rebind is needed (see
+ * xe_bo_trigger_rebind()); this makes the path worthwhile regardless of BO
+ * size. A flat-CCS aux pass, if needed, is migrated packed alongside the data
+ * (changed pages only), so it does not disqualify the path. Otherwise the
+ * full-mapping defrag path is used.
+ */
+static bool xe_bo_defrag_use_iova(struct ttm_buffer_object *ttm_bo)
+{
+	struct xe_ttm_tt *xe_tt, *old_xe_tt;
+
+	if (!ttm_bo->defrag_old_tt || !ttm_bo->ttm)
+		return false;
+
+	xe_tt = container_of(ttm_bo->ttm, struct xe_ttm_tt, ttm);
+	old_xe_tt = container_of(ttm_bo->defrag_old_tt, struct xe_ttm_tt, ttm);
+
+	/*
+	 * Both tts must be IOVA mapped: the packed copy reads the old tt's IOVA
+	 * and the finalize job transfers the old tt's reservation to the new
+	 * one. use_iova is a deterministic per-device decision taken at tt
+	 * creation, so for a given BO the old and new tt always agree; the
+	 * explicit old-tt check guards against ever routing a sg-fallback old
+	 * tt into xe_tt_iova_transfer() (which would copy an invalid iova).
+	 */
+	return xe_tt->use_iova && old_xe_tt->use_iova;
+}
+
 /*
 * Account ttm pages against the device shrinker's shrinkable and
 * purgeable counts.
@@ -1357,6 +1625,7 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 	struct drm_gem_object *obj = &bo->ttm.base;
 	struct drm_gpuvm_bo *vm_bo;
 	bool idle = false;
+	bool defrag_iova;
 	int ret = 0;
 
 	dma_resv_assert_held(bo->ttm.base.resv);
@@ -1369,12 +1638,25 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 		dma_resv_iter_end(&cursor);
 	}
 
Quoted 15 lines

drivers/gpu/drm/xe/xe_bo.c static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo, · line 1650

Quoted 10 lines
drivers/gpu/drm/xe/xe_bo.c +10 -0
+	/*
+	 * A defrag move that reallocates IOVA-mapped backing preserves the
+	 * device-visible addresses: the IOVA reservation is transferred from
+	 * the old to the new tt, so the existing GPU PTEs stay valid and the
+	 * BO must not be marked evicted (the rebind paths in xe_exec_ioctl()
+	 * and preempt_rebind_work_func() would otherwise needlessly rebind).
+	 * Preempt fences still need to be triggered, and fault-mode / L2
+	 * flush-optimized configs still need a TLB invalidation below, so only
+	 * the eviction marking is skipped.
+	 */

The commit is bit confusing compared to the code below. Eviction marking is not completely skipped, it is only skipped when it is not needed. May be update the comment?

I've read this a few times and it pretty clear to me.

Do you have different suggestion?

Matt

Quoted 69 lines

Niranjana

Quoted 67 lines
drivers/gpu/drm/xe/xe_bo.c +23 -5
+	defrag_iova = xe_bo_defrag_use_iova(&bo->ttm);
+
 	drm_gem_for_each_gpuvm_bo(vm_bo, obj) {
 		struct xe_vm *vm = gpuvm_to_vm(vm_bo->vm);
 		struct drm_gpuva *gpuva;
 
 		if (!xe_vm_in_fault_mode(vm)) {
-			drm_gpuvm_bo_evict(vm_bo, true);
+			if (!defrag_iova)
+				drm_gpuvm_bo_evict(vm_bo, true);
 			/*
 			 * L2 cache may not be flushed, so ensure that is done in
 			 * xe_vm_invalidate_vma() below
@@ -1394,6 +1676,8 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 			if (!ctx->no_wait_gpu &&
 			    !xe_vm_flush_vm_bo_tlb_async(vm, bo, vm_bo))
 				continue;
+			else if (defrag_iova)
+				drm_gpuvm_bo_evict(vm_bo, true);
 		}
 
 		if (!idle) {
@@ -2120,6 +2404,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	bool needs_clear;
 	bool handle_system_ccs = (!IS_DGFX(xe) && xe_bo_needs_ccs_pages(bo) &&
 				  ttm && ttm_tt_is_populated(ttm)) ? true : false;
+	bool defrag_iova = xe_bo_defrag_use_iova(ttm_bo);
 	int ret = 0;
 
 	/*
@@ -2168,7 +2453,12 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	needs_clear = (ttm && ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC) ||
 		(!ttm && ttm_bo->type == ttm_bo_type_device);
 
-	if (new_mem->mem_type == XE_PL_TT) {
+	/*
+	 * For a defrag move using the packed IOVA path the destination tt is
+	 * not fully mapped here; only the changed pages are mapped (packed) for
+	 * the copy, and the finalize job establishes the full mapping.
+	 */
+	if (new_mem->mem_type == XE_PL_TT && !defrag_iova) {
 		ret = xe_tt_map(xe, ttm);
 		if (ret)
 			goto out;
@@ -2278,9 +2568,16 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 
 		fence = xe_migrate_clear(migrate, bo, new_mem, flags);
 	} else if (ttm_bo->defrag_old_tt) {
-		fence = xe_migrate_copy_defrag(migrate, bo, old_mem, new_mem,
-					       ttm_bo->defrag_old_tt,
-					       handle_system_ccs);
+		if (defrag_iova)
+			fence = xe_bo_defrag_iova_copy(xe, migrate, bo, old_mem,
+						       new_mem,
+						       ttm_bo->defrag_old_tt,
+						       handle_system_ccs);
+		else
+			fence = xe_migrate_copy_defrag(migrate, bo, old_mem,
+						       new_mem,
+						       ttm_bo->defrag_old_tt,
+						       handle_system_ccs);
 	} else {
 		fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem,
 					handle_system_ccs);
Signature · 1 line
2.34.1
author maintainer
#133 lore raw

PATCH v3 33/33 drm/amdgpu: Preallocate system BO pages outside the reservation lock

In reply to Matthew Brost

Populating a GTT (system) buffer object under the reservation lock can stall in reclaim and compaction while trying to satisfy beneficial-order allocations, holding the lock for the duration.

Mirror the Xe out-of-lock preallocation for pure system BOs: when a gem_create request targets AMDGPU_GEM_DOMAIN_GTT only, fill the full page backing up front via ttm_pool_prealloc_fill_full() before taking the reservation lock. The populate under the lock then simply installs these pages instead of reclaiming and compacting in the critical section. The fill is best-effort - a short fill falls back to the normal in-lock allocation for the remaining pages, and any leftover pages are released by ttm_pool_prealloc_fini() on all exit paths.

Unlike Xe, amdgpu has no background defragmenter, so the higher-order reclaim backoff is left disabled (normal reclaim). If the pool uses dma-alloc (swiotlb), the fill bails and the feature is a silent no-op.

Factor the tt pool selection into amdgpu_ttm_tt_pool_id() and amdgpu_ttm_pool() so the preallocation targets the exact pool the populate will consume, and thread an optional prealloc bag through amdgpu_gem_object_create() and amdgpu_bo_param into the populate ctx.

Cc Alex Deucher <[email protected]>
Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

v3:
- Keep WC caching only when USWC is supported and bound the preallocation to available GTT space (Sashiko)

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 73 +++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h       |  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |  4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 39 +++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  3 +
 8 files changed, 108 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -2
index 20831dbebc31..940f58848a97 100644
@@ -345,7 +345,7 @@ create_dmamap_sg_bo(struct amdgpu_device *adev,
 
 	ret = amdgpu_gem_object_create(adev, mem->bo->tbo.base.size, 1,
 			AMDGPU_GEM_DOMAIN_CPU, AMDGPU_GEM_CREATE_PREEMPTIBLE | flags,
-			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0);
+			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0, NULL);
 
 	amdgpu_bo_unreserve(mem->bo);
 
@@ -1811,7 +1811,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 		 domain_string(alloc_domain), xcp_id);
 
 	ret = amdgpu_gem_object_create(adev, aligned_size, 1, alloc_domain, alloc_flags,
-				       bo_type, NULL, &gobj, xcp_id + 1);
+				       bo_type, NULL, &gobj, xcp_id + 1, NULL);
 	if (ret) {
 		pr_debug("Failed to create BO on domain %s. ret %d\n",
 			 domain_string(alloc_domain), ret);
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index b33c300e26e2..51510e831129 100644
@@ -435,7 +435,7 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
 
 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
 				       AMDGPU_GEM_DOMAIN_CPU, flags,
-				       ttm_bo_type_sg, resv, &gobj, 0);
+				       ttm_bo_type_sg, resv, &gobj, 0, NULL);
 	if (ret)
 		goto error;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +65 -8

138 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +4 -1
index b558336bc4c6..706aeca011f1 100644
@@ -35,6 +35,8 @@
 
 extern const struct drm_gem_object_funcs amdgpu_gem_object_funcs;
 
+struct ttm_pool_prealloc;
+
 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns);
 
 /*
@@ -45,7 +47,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1);
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc);
 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 			    struct drm_device *dev,
 			    struct drm_mode_create_dumb *args);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -1
index f98bfba59a2c..18c4cf3f35a5 100644
@@ -632,7 +632,8 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
 		/* We opt to avoid OOM on system pages allocations */
 		.gfp_retry_mayfail = true,
 		.allow_res_evict = bp->type != ttm_bo_type_kernel,
-		.resv = bp->resv
+		.resv = bp->resv,
+		.prealloc = bp->prealloc,
 	};
 	struct amdgpu_bo *bo;
 	unsigned long page_align, size = bp->size;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -0
index ff11a0903499..11f1d403f152 100644
@@ -45,6 +45,8 @@
 #define to_amdgpu_bo_user(abo) container_of((abo), struct amdgpu_bo_user, bo)
 #define to_amdgpu_bo_vm(abo) container_of((abo), struct amdgpu_bo_vm, bo)
 
+struct ttm_pool_prealloc;
+
 struct amdgpu_bo_param {
 	unsigned long			size;
 	int				byte_align;
@@ -58,6 +60,8 @@ struct amdgpu_bo_param {
 	void				(*destroy)(struct ttm_buffer_object *bo);
 	/* xcp partition number plus 1, 0 means any partition */
 	int8_t				xcp_id_plus1;
+	/* optional out-of-lock preallocated backing (system/GTT only) */
+	struct ttm_pool_prealloc	*prealloc;
 };
 
 /* bo virtual addresses in a vm */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -12
index b10b0878df37..c9cd4714f515 100644
@@ -1173,6 +1173,30 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 	kfree(sgt);
 }
 
+/*
+ * amdgpu_ttm_tt_pool_id - compute the ttm pool id backing a given xcp
+ *
+ * Mirrors the mapping used when creating the gtt ttm_tt, so callers that only
+ * have an xcp id (e.g. an out-of-lock preallocation before the bo exists) pick
+ * the same pool the populate will use.
+ */
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id)
+{
+	if (adev->gmc.mem_partitions && xcp_id >= 0)
+		return KFD_XCP_MEM_ID(adev, xcp_id);
+
+	return xcp_id;
+}
+
+/* amdgpu_ttm_pool - select the ttm pool for a given pool id */
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id)
+{
+	if (adev->mman.ttm_pools && pool_id >= 0)
+		return &adev->mman.ttm_pools[pool_id];
+
+	return &adev->mman.bdev.pool;
+}
+
 /**
  * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
  *
@@ -1194,10 +1218,7 @@ static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
 		return NULL;
 
 	gtt->gobj = &bo->base;
-	if (adev->gmc.mem_partitions && abo->xcp_id >= 0)
-		gtt->pool_id = KFD_XCP_MEM_ID(adev, abo->xcp_id);
-	else
-		gtt->pool_id = abo->xcp_id;
+	gtt->pool_id = amdgpu_ttm_tt_pool_id(adev, abo->xcp_id);
 
 	if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
 		caching = ttm_write_combined;
@@ -1239,10 +1260,7 @@ static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
 	if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
 		return 0;
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 	ret = ttm_pool_alloc(pool, ttm, ctx);
 	if (ret)
 		return ret;
@@ -1284,10 +1302,7 @@ static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
 
 	adev = amdgpu_ttm_adev(bdev);
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 
 	return ttm_pool_free(pool, ttm);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -0
index ff9e2e346609..f90844937b73 100644
@@ -207,6 +207,9 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
 
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id);
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id);
+
 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 				 struct amdgpu_hmm_range *range);
Signature · 1 line
2.34.1
#134 lore raw

Re: [PATCH v3 33/33] drm/amdgpu: Preallocate system BO pages outside the reservation lock

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] TOCTOU race on `fpriv->xcp_id` causes pool allocation mismatches and potential NUMA pool…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] TOCTOU race on fpriv->xcp_id causes pool allocation mismatches and potential NUMA pool pollution. --

--- Patch [33]: [PATCH v3 33/33] drm/amdgpu: Preallocate system BO pages outside the reservation lock --- commit 4e5e075ea8b2b453d727a0f32343eda951ff2819
Author: Matthew Brost <[email protected]>
drm/amdgpu: Preallocate system BO pages outside the reservation lock

This commit introduces out-of-lock preallocation for pure system buffer objects to avoid stalling under the reservation lock. By mirroring the Xe driver's approach, it fills the full page backing up front before taking the lock.

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +0 -0

[ ... ]

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +12 -0
@@ -443,10 +448,57 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
 	}
 
+	/*
+	 * For system-only (pure GTT) BOs, preallocate the whole page backing
+	 * up front, outside the reservation lock. Populate under the lock then
+	 * just installs these pages instead of reclaiming/compacting in the
+	 * critical section. Best-effort: a short fill falls back to the normal
+	 * in-lock allocation for the missing pages.
+	 */
+	if (args->in.domains == AMDGPU_GEM_DOMAIN_GTT) {
+		struct ttm_resource_manager *gtt_man =
+			ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
+		int32_t xcp_id = adev->gmc.mem_partitions ? fpriv->xcp_id : 0;
+		int32_t pool_id = amdgpu_ttm_tt_pool_id(adev, xcp_id);

[ ... ]

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data, · line 511

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +2 -1
@@ -455,7 +507,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 retry:
 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
 				     initial_domain,
-				     flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
+				     flags, ttm_bo_type_device, resv, &gobj,
+				     fpriv->xcp_id + 1, prealloc_pool ? &prealloc : NULL);

[Severity: Medium]
Is it possible for fpriv->xcp_id to change between these two reads?

If a concurrent thread updates fpriv->xcp_id (for example, via context creation in amdgpu_xcp_select_scheds changing it from AMDGPU_XCP_NO_PARTITION to a specific partition), the first read could select the generic TTM pool for preallocation.

The second read could then evaluate to a specific partition, initializing the BO with the partition-specific TTM pool. Could this mismatch result in pages allocated from the generic pool being consumed by the partition pool, potentially degrading NUMA locality or causing accounting mismatches?

Would it be safer to read fpriv->xcp_id once into a local variable and pass that same value to both amdgpu_ttm_tt_pool_id() and amdgpu_gem_object_create()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=33
maintainer
#135 lore raw

Re: [PATCH v3 33/33] drm/amdgpu: Preallocate system BO pages outside the reservation lock

In reply to Matthew Brost
Quoted 3 lines · Matthew Brost

Populating a GTT (system) buffer object under the reservation lock can stall in reclaim and compaction while trying to satisfy beneficial-order allocations, holding the lock for the duration.

Yeah and that is perfectly intentional behavior.

I really don't see any reason for any driver to change that.

The purpose of the dma_resv lock is to stall other allocation for the same object it protects, making sure that we don't have multiple threads allocating memory for the same buffer object.

Why in the world should that be a problem?

Regards,
Christian.

Quoted 383 lines

Mirror the Xe out-of-lock preallocation for pure system BOs: when a gem_create request targets AMDGPU_GEM_DOMAIN_GTT only, fill the full page backing up front via ttm_pool_prealloc_fill_full() before taking the reservation lock. The populate under the lock then simply installs these pages instead of reclaiming and compacting in the critical section. The fill is best-effort - a short fill falls back to the normal in-lock allocation for the remaining pages, and any leftover pages are released by ttm_pool_prealloc_fini() on all exit paths.

Unlike Xe, amdgpu has no background defragmenter, so the higher-order reclaim backoff is left disabled (normal reclaim). If the pool uses dma-alloc (swiotlb), the fill bails and the feature is a silent no-op.

Factor the tt pool selection into amdgpu_ttm_tt_pool_id() and amdgpu_ttm_pool() so the preallocation targets the exact pool the populate will consume, and thread an optional prealloc bag through amdgpu_gem_object_create() and amdgpu_bo_param into the populate ctx.

Cc Alex Deucher <[email protected]>
Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

v3:
- Keep WC caching only when USWC is supported and bound the preallocation to available GTT space (Sashiko)

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 73 +++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h       |  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |  4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 39 +++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  3 +
 8 files changed, 108 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -2
index 20831dbebc31..940f58848a97 100644
@@ -345,7 +345,7 @@ create_dmamap_sg_bo(struct amdgpu_device *adev,
 
 	ret = amdgpu_gem_object_create(adev, mem->bo->tbo.base.size, 1,
 			AMDGPU_GEM_DOMAIN_CPU, AMDGPU_GEM_CREATE_PREEMPTIBLE | flags,
-			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0);
+			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0, NULL);
 
 	amdgpu_bo_unreserve(mem->bo);
 
@@ -1811,7 +1811,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 		 domain_string(alloc_domain), xcp_id);
 
 	ret = amdgpu_gem_object_create(adev, aligned_size, 1, alloc_domain, alloc_flags,
-				       bo_type, NULL, &gobj, xcp_id + 1);
+				       bo_type, NULL, &gobj, xcp_id + 1, NULL);
 	if (ret) {
 		pr_debug("Failed to create BO on domain %s. ret %d\n",
 			 domain_string(alloc_domain), ret);
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index b33c300e26e2..51510e831129 100644
@@ -435,7 +435,7 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
 
 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
 				       AMDGPU_GEM_DOMAIN_CPU, flags,
-				       ttm_bo_type_sg, resv, &gobj, 0);
+				       ttm_bo_type_sg, resv, &gobj, 0, NULL);
 	if (ret)
 		goto error;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +65 -8
index 6a0699746fbc..e8b732218e7d 100644
@@ -39,6 +39,7 @@
 #include <drm/drm_exec.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/ttm/ttm_tt.h>
+#include <drm/ttm/ttm_pool.h>
 #include <drm/drm_syncobj.h>
 
 #include "amdgpu.h"
@@ -168,7 +169,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1)
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc)
 {
 	struct amdgpu_bo *bo;
 	struct amdgpu_bo_user *ubo;
@@ -188,6 +190,7 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 	bp.domain = initial_domain;
 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
 	bp.xcp_id_plus1 = xcp_id_plus1;
+	bp.prealloc = prealloc;
 
 	r = amdgpu_bo_create_user(adev, &bp, &ubo);
 	if (r)
@@ -412,6 +415,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 	struct dma_resv *resv = NULL;
 	struct drm_gem_object *gobj;
 	uint32_t handle, initial_domain;
+	struct ttm_pool_prealloc prealloc = {};
+	struct ttm_pool *prealloc_pool = NULL;
 	int r;
 
 	/* reject invalid gem flags */
@@ -443,10 +448,57 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
 	}
 
+	/*
+	 * For system-only (pure GTT) BOs, preallocate the whole page backing
+	 * up front, outside the reservation lock. Populate under the lock then
+	 * just installs these pages instead of reclaiming/compacting in the
+	 * critical section. Best-effort: a short fill falls back to the normal
+	 * in-lock allocation for the missing pages.
+	 */
+	if (args->in.domains == AMDGPU_GEM_DOMAIN_GTT) {
+		struct ttm_resource_manager *gtt_man =
+			ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
+		int32_t xcp_id = adev->gmc.mem_partitions ? fpriv->xcp_id : 0;
+		int32_t pool_id = amdgpu_ttm_tt_pool_id(adev, xcp_id);
+		/*
+		 * Mirror the USWC handling in amdgpu_bo_create(): the flag is
+		 * stripped when the platform can't do write-combining, in which
+		 * case the tt is created ttm_cached. The prealloc caching must
+		 * match the final tt caching exactly, otherwise ttm_pool_free()
+		 * would restore the wrong PAT state on these pages (PAT aliasing
+		 * / leak of WC pages into the cached allocator).
+		 */
+		bool uswc = (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) &&
+			amdgpu_bo_support_uswc(flags);
+		enum ttm_caching caching =
+			uswc ? ttm_write_combined : ttm_cached;
+
+		/*
+		 * Only prealloc when the request fits the currently-available
+		 * GTT (total manager size minus current usage). This mirrors the
+		 * amdgpu_bo_validate_size() bound applied later in
+		 * amdgpu_bo_create() while also skipping the up-front reclaim /
+		 * compaction when GTT is already near full or the user-controlled
+		 * size is over-large (the creation path handles those anyway).
+		 */
+		if (gtt_man) {
+			u64 used = ttm_resource_manager_usage(gtt_man);
+			u64 avail = gtt_man->size > used ?
+				gtt_man->size - used : 0;
+
+			if (size <= avail) {
+				prealloc_pool = amdgpu_ttm_pool(adev, pool_id);
+				ttm_pool_prealloc_fill_full(prealloc_pool,
+							    caching, &prealloc,
+							    PFN_UP(size), false);
+			}
+		}
+	}
+
 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
 		r = amdgpu_bo_reserve(vm->root.bo, false);
 		if (r)
-			return r;
+			goto out_prealloc;
 
 		resv = vm->root.bo->tbo.base.resv;
 	}
@@ -455,7 +507,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 retry:
 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
 				     initial_domain,
-				     flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
+				     flags, ttm_bo_type_device, resv, &gobj,
+				     fpriv->xcp_id + 1, prealloc_pool ? &prealloc : NULL);
 	if (r && r != -ERESTARTSYS) {
 		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
 			flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
@@ -479,17 +532,21 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		amdgpu_bo_unreserve(vm->root.bo);
 	}
 	if (r)
-		return r;
+		goto out_prealloc;
 
 	r = drm_gem_handle_create(filp, gobj, &handle);
 	/* drop reference from allocate - handle holds it now */
 	drm_gem_object_put(gobj);
 	if (r)
-		return r;
+		goto out_prealloc;
 
 	memset(args, 0, sizeof(*args));
 	args->out.handle = handle;
-	return 0;
+
+out_prealloc:
+	if (prealloc_pool)
+		ttm_pool_prealloc_fini(prealloc_pool, &prealloc);
+	return r;
 }
 
 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
@@ -528,7 +585,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 
 	/* create a gem object to contain this object in */
 	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
-				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
+				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1, NULL);
 	if (r)
 		return r;
 
@@ -1298,7 +1355,7 @@ int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 	domain = amdgpu_bo_get_preferred_domain(adev,
 				amdgpu_display_supported_domains(adev, flags));
 	r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
-				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
+				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1, NULL);
 	if (r)
 		return -ENOMEM;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +4 -1
index b558336bc4c6..706aeca011f1 100644
@@ -35,6 +35,8 @@
 
 extern const struct drm_gem_object_funcs amdgpu_gem_object_funcs;
 
+struct ttm_pool_prealloc;
+
 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns);
 
 /*
@@ -45,7 +47,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1);
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc);
 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 			    struct drm_device *dev,
 			    struct drm_mode_create_dumb *args);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -1
index f98bfba59a2c..18c4cf3f35a5 100644
@@ -632,7 +632,8 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
 		/* We opt to avoid OOM on system pages allocations */
 		.gfp_retry_mayfail = true,
 		.allow_res_evict = bp->type != ttm_bo_type_kernel,
-		.resv = bp->resv
+		.resv = bp->resv,
+		.prealloc = bp->prealloc,
 	};
 	struct amdgpu_bo *bo;
 	unsigned long page_align, size = bp->size;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -0
index ff11a0903499..11f1d403f152 100644
@@ -45,6 +45,8 @@
 #define to_amdgpu_bo_user(abo) container_of((abo), struct amdgpu_bo_user, bo)
 #define to_amdgpu_bo_vm(abo) container_of((abo), struct amdgpu_bo_vm, bo)
 
+struct ttm_pool_prealloc;
+
 struct amdgpu_bo_param {
 	unsigned long			size;
 	int				byte_align;
@@ -58,6 +60,8 @@ struct amdgpu_bo_param {
 	void				(*destroy)(struct ttm_buffer_object *bo);
 	/* xcp partition number plus 1, 0 means any partition */
 	int8_t				xcp_id_plus1;
+	/* optional out-of-lock preallocated backing (system/GTT only) */
+	struct ttm_pool_prealloc	*prealloc;
 };
 
 /* bo virtual addresses in a vm */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -12
index b10b0878df37..c9cd4714f515 100644
@@ -1173,6 +1173,30 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 	kfree(sgt);
 }
 
+/*
+ * amdgpu_ttm_tt_pool_id - compute the ttm pool id backing a given xcp
+ *
+ * Mirrors the mapping used when creating the gtt ttm_tt, so callers that only
+ * have an xcp id (e.g. an out-of-lock preallocation before the bo exists) pick
+ * the same pool the populate will use.
+ */
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id)
+{
+	if (adev->gmc.mem_partitions && xcp_id >= 0)
+		return KFD_XCP_MEM_ID(adev, xcp_id);
+
+	return xcp_id;
+}
+
+/* amdgpu_ttm_pool - select the ttm pool for a given pool id */
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id)
+{
+	if (adev->mman.ttm_pools && pool_id >= 0)
+		return &adev->mman.ttm_pools[pool_id];
+
+	return &adev->mman.bdev.pool;
+}
+
 /**
  * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
  *
@@ -1194,10 +1218,7 @@ static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
 		return NULL;
 
 	gtt->gobj = &bo->base;
-	if (adev->gmc.mem_partitions && abo->xcp_id >= 0)
-		gtt->pool_id = KFD_XCP_MEM_ID(adev, abo->xcp_id);
-	else
-		gtt->pool_id = abo->xcp_id;
+	gtt->pool_id = amdgpu_ttm_tt_pool_id(adev, abo->xcp_id);
 
 	if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
 		caching = ttm_write_combined;
@@ -1239,10 +1260,7 @@ static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
 	if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
 		return 0;
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 	ret = ttm_pool_alloc(pool, ttm, ctx);
 	if (ret)
 		return ret;
@@ -1284,10 +1302,7 @@ static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
 
 	adev = amdgpu_ttm_adev(bdev);
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 
 	return ttm_pool_free(pool, ttm);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -0
index ff9e2e346609..f90844937b73 100644
@@ -207,6 +207,9 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
 
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id);
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id);
+
 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 				 struct amdgpu_hmm_range *range);
author maintainer
#136 lore raw

Re: [PATCH v3 33/33] drm/amdgpu: Preallocate system BO pages outside the reservation lock

Quoted 5 lines · Christian König
Quoted 3 lines · Matthew Brost

Populating a GTT (system) buffer object under the reservation lock can stall in reclaim and compaction while trying to satisfy beneficial-order allocations, holding the lock for the duration.

Quoted 8 lines

Yeah and that is perfectly intentional behavior.

I really don't see any reason for any driver to change that.

The purpose of the dma_resv lock is to stall other allocation for the same object it protects, making sure that we don't have multiple threads allocating memory for the same buffer object.

Why in the world should that be a problem?

See my example in my reply to the cover letter: multiple threads sharing a VM, with a memory allocation performed under the VM dma-resv lock, which in turn blocks exec IOCTLs, CPU page faults or VM binds on BOs sharing the same VM dma-resv lock. With that, this code could be gating on whether the BO allocation shares a VM dma-resv lock.

The most relevant example here, I believe, is that launching a new Chrome tab could potentially stall the aforementioned operations in a different Chrome tab. Likewise, if games allocate memory on demand (they typically do not, because memory allocations are prone to introducing stalls), they could also incur the aforementioned stalls.

I think the opposite question is more appropriate here: does preallocating memory outside of dma-resv before a device can DMA to it break any invariants? The answer is no; it does not in the case of GEM create IOCTLs or the defragmentation moves introduced in this series.

Matt

Quoted 387 lines

Regards,
Christian.

Quoted 383 lines

Mirror the Xe out-of-lock preallocation for pure system BOs: when a gem_create request targets AMDGPU_GEM_DOMAIN_GTT only, fill the full page backing up front via ttm_pool_prealloc_fill_full() before taking the reservation lock. The populate under the lock then simply installs these pages instead of reclaiming and compacting in the critical section. The fill is best-effort - a short fill falls back to the normal in-lock allocation for the remaining pages, and any leftover pages are released by ttm_pool_prealloc_fini() on all exit paths.

Unlike Xe, amdgpu has no background defragmenter, so the higher-order reclaim backoff is left disabled (normal reclaim). If the pool uses dma-alloc (swiotlb), the fill bails and the feature is a silent no-op.

Factor the tt pool selection into amdgpu_ttm_tt_pool_id() and amdgpu_ttm_pool() so the preallocation targets the exact pool the populate will consume, and thread an optional prealloc bag through amdgpu_gem_object_create() and amdgpu_bo_param into the populate ctx.

Cc Alex Deucher <[email protected]>
Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

v3:
- Keep WC caching only when USWC is supported and bound the preallocation to available GTT space (Sashiko)

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 73 +++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h       |  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |  4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 39 +++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  3 +
 8 files changed, 108 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -2
index 20831dbebc31..940f58848a97 100644
@@ -345,7 +345,7 @@ create_dmamap_sg_bo(struct amdgpu_device *adev,
 
 	ret = amdgpu_gem_object_create(adev, mem->bo->tbo.base.size, 1,
 			AMDGPU_GEM_DOMAIN_CPU, AMDGPU_GEM_CREATE_PREEMPTIBLE | flags,
-			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0);
+			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0, NULL);
 
 	amdgpu_bo_unreserve(mem->bo);
 
@@ -1811,7 +1811,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 		 domain_string(alloc_domain), xcp_id);
 
 	ret = amdgpu_gem_object_create(adev, aligned_size, 1, alloc_domain, alloc_flags,
-				       bo_type, NULL, &gobj, xcp_id + 1);
+				       bo_type, NULL, &gobj, xcp_id + 1, NULL);
 	if (ret) {
 		pr_debug("Failed to create BO on domain %s. ret %d\n",
 			 domain_string(alloc_domain), ret);
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index b33c300e26e2..51510e831129 100644
@@ -435,7 +435,7 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
 
 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
 				       AMDGPU_GEM_DOMAIN_CPU, flags,
-				       ttm_bo_type_sg, resv, &gobj, 0);
+				       ttm_bo_type_sg, resv, &gobj, 0, NULL);
 	if (ret)
 		goto error;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +65 -8
index 6a0699746fbc..e8b732218e7d 100644
@@ -39,6 +39,7 @@
 #include <drm/drm_exec.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/ttm/ttm_tt.h>
+#include <drm/ttm/ttm_pool.h>
 #include <drm/drm_syncobj.h>
 
 #include "amdgpu.h"
@@ -168,7 +169,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1)
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc)
 {
 	struct amdgpu_bo *bo;
 	struct amdgpu_bo_user *ubo;
@@ -188,6 +190,7 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 	bp.domain = initial_domain;
 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
 	bp.xcp_id_plus1 = xcp_id_plus1;
+	bp.prealloc = prealloc;
 
 	r = amdgpu_bo_create_user(adev, &bp, &ubo);
 	if (r)
@@ -412,6 +415,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 	struct dma_resv *resv = NULL;
 	struct drm_gem_object *gobj;
 	uint32_t handle, initial_domain;
+	struct ttm_pool_prealloc prealloc = {};
+	struct ttm_pool *prealloc_pool = NULL;
 	int r;
 
 	/* reject invalid gem flags */
@@ -443,10 +448,57 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
 	}
 
+	/*
+	 * For system-only (pure GTT) BOs, preallocate the whole page backing
+	 * up front, outside the reservation lock. Populate under the lock then
+	 * just installs these pages instead of reclaiming/compacting in the
+	 * critical section. Best-effort: a short fill falls back to the normal
+	 * in-lock allocation for the missing pages.
+	 */
+	if (args->in.domains == AMDGPU_GEM_DOMAIN_GTT) {
+		struct ttm_resource_manager *gtt_man =
+			ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
+		int32_t xcp_id = adev->gmc.mem_partitions ? fpriv->xcp_id : 0;
+		int32_t pool_id = amdgpu_ttm_tt_pool_id(adev, xcp_id);
+		/*
+		 * Mirror the USWC handling in amdgpu_bo_create(): the flag is
+		 * stripped when the platform can't do write-combining, in which
+		 * case the tt is created ttm_cached. The prealloc caching must
+		 * match the final tt caching exactly, otherwise ttm_pool_free()
+		 * would restore the wrong PAT state on these pages (PAT aliasing
+		 * / leak of WC pages into the cached allocator).
+		 */
+		bool uswc = (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) &&
+			amdgpu_bo_support_uswc(flags);
+		enum ttm_caching caching =
+			uswc ? ttm_write_combined : ttm_cached;
+
+		/*
+		 * Only prealloc when the request fits the currently-available
+		 * GTT (total manager size minus current usage). This mirrors the
+		 * amdgpu_bo_validate_size() bound applied later in
+		 * amdgpu_bo_create() while also skipping the up-front reclaim /
+		 * compaction when GTT is already near full or the user-controlled
+		 * size is over-large (the creation path handles those anyway).
+		 */
+		if (gtt_man) {
+			u64 used = ttm_resource_manager_usage(gtt_man);
+			u64 avail = gtt_man->size > used ?
+				gtt_man->size - used : 0;
+
+			if (size <= avail) {
+				prealloc_pool = amdgpu_ttm_pool(adev, pool_id);
+				ttm_pool_prealloc_fill_full(prealloc_pool,
+							    caching, &prealloc,
+							    PFN_UP(size), false);
+			}
+		}
+	}
+
 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
 		r = amdgpu_bo_reserve(vm->root.bo, false);
 		if (r)
-			return r;
+			goto out_prealloc;
 
 		resv = vm->root.bo->tbo.base.resv;
 	}
@@ -455,7 +507,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 retry:
 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
 				     initial_domain,
-				     flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
+				     flags, ttm_bo_type_device, resv, &gobj,
+				     fpriv->xcp_id + 1, prealloc_pool ? &prealloc : NULL);
 	if (r && r != -ERESTARTSYS) {
 		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
 			flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
@@ -479,17 +532,21 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		amdgpu_bo_unreserve(vm->root.bo);
 	}
 	if (r)
-		return r;
+		goto out_prealloc;
 
 	r = drm_gem_handle_create(filp, gobj, &handle);
 	/* drop reference from allocate - handle holds it now */
 	drm_gem_object_put(gobj);
 	if (r)
-		return r;
+		goto out_prealloc;
 
 	memset(args, 0, sizeof(*args));
 	args->out.handle = handle;
-	return 0;
+
+out_prealloc:
+	if (prealloc_pool)
+		ttm_pool_prealloc_fini(prealloc_pool, &prealloc);
+	return r;
 }
 
 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
@@ -528,7 +585,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 
 	/* create a gem object to contain this object in */
 	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
-				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
+				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1, NULL);
 	if (r)
 		return r;
 
@@ -1298,7 +1355,7 @@ int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 	domain = amdgpu_bo_get_preferred_domain(adev,
 				amdgpu_display_supported_domains(adev, flags));
 	r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
-				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
+				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1, NULL);
 	if (r)
 		return -ENOMEM;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +4 -1
index b558336bc4c6..706aeca011f1 100644
@@ -35,6 +35,8 @@
 
 extern const struct drm_gem_object_funcs amdgpu_gem_object_funcs;
 
+struct ttm_pool_prealloc;
+
 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns);
 
 /*
@@ -45,7 +47,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1);
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc);
 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 			    struct drm_device *dev,
 			    struct drm_mode_create_dumb *args);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -1
index f98bfba59a2c..18c4cf3f35a5 100644
@@ -632,7 +632,8 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
 		/* We opt to avoid OOM on system pages allocations */
 		.gfp_retry_mayfail = true,
 		.allow_res_evict = bp->type != ttm_bo_type_kernel,
-		.resv = bp->resv
+		.resv = bp->resv,
+		.prealloc = bp->prealloc,
 	};
 	struct amdgpu_bo *bo;
 	unsigned long page_align, size = bp->size;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -0
index ff11a0903499..11f1d403f152 100644
@@ -45,6 +45,8 @@
 #define to_amdgpu_bo_user(abo) container_of((abo), struct amdgpu_bo_user, bo)
 #define to_amdgpu_bo_vm(abo) container_of((abo), struct amdgpu_bo_vm, bo)
 
+struct ttm_pool_prealloc;
+
 struct amdgpu_bo_param {
 	unsigned long			size;
 	int				byte_align;
@@ -58,6 +60,8 @@ struct amdgpu_bo_param {
 	void				(*destroy)(struct ttm_buffer_object *bo);
 	/* xcp partition number plus 1, 0 means any partition */
 	int8_t				xcp_id_plus1;
+	/* optional out-of-lock preallocated backing (system/GTT only) */
+	struct ttm_pool_prealloc	*prealloc;
 };
 
 /* bo virtual addresses in a vm */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -12
index b10b0878df37..c9cd4714f515 100644
@@ -1173,6 +1173,30 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 	kfree(sgt);
 }
 
+/*
+ * amdgpu_ttm_tt_pool_id - compute the ttm pool id backing a given xcp
+ *
+ * Mirrors the mapping used when creating the gtt ttm_tt, so callers that only
+ * have an xcp id (e.g. an out-of-lock preallocation before the bo exists) pick
+ * the same pool the populate will use.
+ */
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id)
+{
+	if (adev->gmc.mem_partitions && xcp_id >= 0)
+		return KFD_XCP_MEM_ID(adev, xcp_id);
+
+	return xcp_id;
+}
+
+/* amdgpu_ttm_pool - select the ttm pool for a given pool id */
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id)
+{
+	if (adev->mman.ttm_pools && pool_id >= 0)
+		return &adev->mman.ttm_pools[pool_id];
+
+	return &adev->mman.bdev.pool;
+}
+
 /**
  * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
  *
@@ -1194,10 +1218,7 @@ static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
 		return NULL;
 
 	gtt->gobj = &bo->base;
-	if (adev->gmc.mem_partitions && abo->xcp_id >= 0)
-		gtt->pool_id = KFD_XCP_MEM_ID(adev, abo->xcp_id);
-	else
-		gtt->pool_id = abo->xcp_id;
+	gtt->pool_id = amdgpu_ttm_tt_pool_id(adev, abo->xcp_id);
 
 	if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
 		caching = ttm_write_combined;
@@ -1239,10 +1260,7 @@ static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
 	if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
 		return 0;
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 	ret = ttm_pool_alloc(pool, ttm, ctx);
 	if (ret)
 		return ret;
@@ -1284,10 +1302,7 @@ static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
 
 	adev = amdgpu_ttm_adev(bdev);
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 
 	return ttm_pool_free(pool, ttm);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -0
index ff9e2e346609..f90844937b73 100644
@@ -207,6 +207,9 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
 
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id);
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id);
+
 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 				 struct amdgpu_hmm_range *range);
maintainer
#137 lore raw

Re: [PATCH v3 33/33] drm/amdgpu: Preallocate system BO pages outside the reservation lock

Quoted 12 lines · Matthew Brost
Quoted 11 lines · Christian König
Quoted 3 lines · Matthew Brost

Populating a GTT (system) buffer object under the reservation lock can stall in reclaim and compaction while trying to satisfy beneficial-order allocations, holding the lock for the duration.

Yeah and that is perfectly intentional behavior.

I really don't see any reason for any driver to change that.

The purpose of the dma_resv lock is to stall other allocation for the same object it protects, making sure that we don't have multiple threads allocating memory for the same buffer object.

Quoted 8 lines
Quoted 2 lines

Why in the world should that be a problem?

See my example in my reply to the cover letter: multiple threads sharing a VM, with a memory allocation performed under the VM dma-resv lock, which in turn blocks exec IOCTLs, CPU page faults or VM binds on BOs sharing the same VM dma-resv lock. With that, this code could be gating on whether the BO allocation shares a VM dma-resv lock.

Ah, ok I see the problem now.

Quoted 5 lines

The most relevant example here, I believe, is that launching a new Chrome tab could potentially stall the aforementioned operations in a different Chrome tab. Likewise, if games allocate memory on demand (they typically do not, because memory allocations are prone to introducing stalls), they could also incur the aforementioned stalls.

Yeah, I was already wondering how such an use case is seen with gaming or HPC.

But Chrome is indeed the right stress test for memory allocation :)

Quoted 4 lines

I think the opposite question is more appropriate here: does preallocating memory outside of dma-resv before a device can DMA to it break any invariants? The answer is no; it does not in the case of GEM create IOCTLs or the defragmentation moves introduced in this series.

Of hand it looks like a valid approach to me, but it is still quite hacky.

I would rather suggest something like that:

1. Allocate a BO #1 with a separate dma_resv lock.
2. Populate that BO with the backing store you want to have.
3. Allocate a BO #2 with the dma_resv lock of the VM, but no backing store whatsoever.
4. Lock both BO #1 and #2.
5. Use a new function ttm_bo_swap_backing_stores() to switch the backing store between the two.
6. Unlock everything and drop the reference on BO #1 to let it cleanup.

This way the memory/resources are always allocated to some resource and buffer object which makes it possible to track it.

Tracking the allocated memory is necessary for hot plug and eventually shrinking it.

Regards,
Christian.

Quoted 390 lines

Matt

Quoted 387 lines

Regards,
Christian.

Quoted 383 lines

Mirror the Xe out-of-lock preallocation for pure system BOs: when a gem_create request targets AMDGPU_GEM_DOMAIN_GTT only, fill the full page backing up front via ttm_pool_prealloc_fill_full() before taking the reservation lock. The populate under the lock then simply installs these pages instead of reclaiming and compacting in the critical section. The fill is best-effort - a short fill falls back to the normal in-lock allocation for the remaining pages, and any leftover pages are released by ttm_pool_prealloc_fini() on all exit paths.

Unlike Xe, amdgpu has no background defragmenter, so the higher-order reclaim backoff is left disabled (normal reclaim). If the pool uses dma-alloc (swiotlb), the fill bails and the feature is a silent no-op.

Factor the tt pool selection into amdgpu_ttm_tt_pool_id() and amdgpu_ttm_pool() so the preallocation targets the exact pool the populate will consume, and thread an optional prealloc bag through amdgpu_gem_object_create() and amdgpu_bo_param into the populate ctx.

Cc Alex Deucher <[email protected]>
Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

v3:
- Keep WC caching only when USWC is supported and bound the preallocation to available GTT space (Sashiko)

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 73 +++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h       |  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |  4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 39 +++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  3 +
 8 files changed, 108 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -2
index 20831dbebc31..940f58848a97 100644
@@ -345,7 +345,7 @@ create_dmamap_sg_bo(struct amdgpu_device *adev,
 
 	ret = amdgpu_gem_object_create(adev, mem->bo->tbo.base.size, 1,
 			AMDGPU_GEM_DOMAIN_CPU, AMDGPU_GEM_CREATE_PREEMPTIBLE | flags,
-			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0);
+			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0, NULL);
 
 	amdgpu_bo_unreserve(mem->bo);
 
@@ -1811,7 +1811,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 		 domain_string(alloc_domain), xcp_id);
 
 	ret = amdgpu_gem_object_create(adev, aligned_size, 1, alloc_domain, alloc_flags,
-				       bo_type, NULL, &gobj, xcp_id + 1);
+				       bo_type, NULL, &gobj, xcp_id + 1, NULL);
 	if (ret) {
 		pr_debug("Failed to create BO on domain %s. ret %d\n",
 			 domain_string(alloc_domain), ret);
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index b33c300e26e2..51510e831129 100644
@@ -435,7 +435,7 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
 
 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
 				       AMDGPU_GEM_DOMAIN_CPU, flags,
-				       ttm_bo_type_sg, resv, &gobj, 0);
+				       ttm_bo_type_sg, resv, &gobj, 0, NULL);
 	if (ret)
 		goto error;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +65 -8
index 6a0699746fbc..e8b732218e7d 100644
@@ -39,6 +39,7 @@
 #include <drm/drm_exec.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/ttm/ttm_tt.h>
+#include <drm/ttm/ttm_pool.h>
 #include <drm/drm_syncobj.h>
 
 #include "amdgpu.h"
@@ -168,7 +169,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1)
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc)
 {
 	struct amdgpu_bo *bo;
 	struct amdgpu_bo_user *ubo;
@@ -188,6 +190,7 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 	bp.domain = initial_domain;
 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
 	bp.xcp_id_plus1 = xcp_id_plus1;
+	bp.prealloc = prealloc;
 
 	r = amdgpu_bo_create_user(adev, &bp, &ubo);
 	if (r)
@@ -412,6 +415,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 	struct dma_resv *resv = NULL;
 	struct drm_gem_object *gobj;
 	uint32_t handle, initial_domain;
+	struct ttm_pool_prealloc prealloc = {};
+	struct ttm_pool *prealloc_pool = NULL;
 	int r;
 
 	/* reject invalid gem flags */
@@ -443,10 +448,57 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
 	}
 
+	/*
+	 * For system-only (pure GTT) BOs, preallocate the whole page backing
+	 * up front, outside the reservation lock. Populate under the lock then
+	 * just installs these pages instead of reclaiming/compacting in the
+	 * critical section. Best-effort: a short fill falls back to the normal
+	 * in-lock allocation for the missing pages.
+	 */
+	if (args->in.domains == AMDGPU_GEM_DOMAIN_GTT) {
+		struct ttm_resource_manager *gtt_man =
+			ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
+		int32_t xcp_id = adev->gmc.mem_partitions ? fpriv->xcp_id : 0;
+		int32_t pool_id = amdgpu_ttm_tt_pool_id(adev, xcp_id);
+		/*
+		 * Mirror the USWC handling in amdgpu_bo_create(): the flag is
+		 * stripped when the platform can't do write-combining, in which
+		 * case the tt is created ttm_cached. The prealloc caching must
+		 * match the final tt caching exactly, otherwise ttm_pool_free()
+		 * would restore the wrong PAT state on these pages (PAT aliasing
+		 * / leak of WC pages into the cached allocator).
+		 */
+		bool uswc = (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) &&
+			amdgpu_bo_support_uswc(flags);
+		enum ttm_caching caching =
+			uswc ? ttm_write_combined : ttm_cached;
+
+		/*
+		 * Only prealloc when the request fits the currently-available
+		 * GTT (total manager size minus current usage). This mirrors the
+		 * amdgpu_bo_validate_size() bound applied later in
+		 * amdgpu_bo_create() while also skipping the up-front reclaim /
+		 * compaction when GTT is already near full or the user-controlled
+		 * size is over-large (the creation path handles those anyway).
+		 */
+		if (gtt_man) {
+			u64 used = ttm_resource_manager_usage(gtt_man);
+			u64 avail = gtt_man->size > used ?
+				gtt_man->size - used : 0;
+
+			if (size <= avail) {
+				prealloc_pool = amdgpu_ttm_pool(adev, pool_id);
+				ttm_pool_prealloc_fill_full(prealloc_pool,
+							    caching, &prealloc,
+							    PFN_UP(size), false);
+			}
+		}
+	}
+
 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
 		r = amdgpu_bo_reserve(vm->root.bo, false);
 		if (r)
-			return r;
+			goto out_prealloc;
 
 		resv = vm->root.bo->tbo.base.resv;
 	}
@@ -455,7 +507,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 retry:
 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
 				     initial_domain,
-				     flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
+				     flags, ttm_bo_type_device, resv, &gobj,
+				     fpriv->xcp_id + 1, prealloc_pool ? &prealloc : NULL);
 	if (r && r != -ERESTARTSYS) {
 		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
 			flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
@@ -479,17 +532,21 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		amdgpu_bo_unreserve(vm->root.bo);
 	}
 	if (r)
-		return r;
+		goto out_prealloc;
 
 	r = drm_gem_handle_create(filp, gobj, &handle);
 	/* drop reference from allocate - handle holds it now */
 	drm_gem_object_put(gobj);
 	if (r)
-		return r;
+		goto out_prealloc;
 
 	memset(args, 0, sizeof(*args));
 	args->out.handle = handle;
-	return 0;
+
+out_prealloc:
+	if (prealloc_pool)
+		ttm_pool_prealloc_fini(prealloc_pool, &prealloc);
+	return r;
 }
 
 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
@@ -528,7 +585,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 
 	/* create a gem object to contain this object in */
 	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
-				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
+				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1, NULL);
 	if (r)
 		return r;
 
@@ -1298,7 +1355,7 @@ int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 	domain = amdgpu_bo_get_preferred_domain(adev,
 				amdgpu_display_supported_domains(adev, flags));
 	r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
-				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
+				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1, NULL);
 	if (r)
 		return -ENOMEM;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +4 -1
index b558336bc4c6..706aeca011f1 100644
@@ -35,6 +35,8 @@
 
 extern const struct drm_gem_object_funcs amdgpu_gem_object_funcs;
 
+struct ttm_pool_prealloc;
+
 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns);
 
 /*
@@ -45,7 +47,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1);
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc);
 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 			    struct drm_device *dev,
 			    struct drm_mode_create_dumb *args);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -1
index f98bfba59a2c..18c4cf3f35a5 100644
@@ -632,7 +632,8 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
 		/* We opt to avoid OOM on system pages allocations */
 		.gfp_retry_mayfail = true,
 		.allow_res_evict = bp->type != ttm_bo_type_kernel,
-		.resv = bp->resv
+		.resv = bp->resv,
+		.prealloc = bp->prealloc,
 	};
 	struct amdgpu_bo *bo;
 	unsigned long page_align, size = bp->size;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -0
index ff11a0903499..11f1d403f152 100644
@@ -45,6 +45,8 @@
 #define to_amdgpu_bo_user(abo) container_of((abo), struct amdgpu_bo_user, bo)
 #define to_amdgpu_bo_vm(abo) container_of((abo), struct amdgpu_bo_vm, bo)
 
+struct ttm_pool_prealloc;
+
 struct amdgpu_bo_param {
 	unsigned long			size;
 	int				byte_align;
@@ -58,6 +60,8 @@ struct amdgpu_bo_param {
 	void				(*destroy)(struct ttm_buffer_object *bo);
 	/* xcp partition number plus 1, 0 means any partition */
 	int8_t				xcp_id_plus1;
+	/* optional out-of-lock preallocated backing (system/GTT only) */
+	struct ttm_pool_prealloc	*prealloc;
 };
 
 /* bo virtual addresses in a vm */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -12
index b10b0878df37..c9cd4714f515 100644
@@ -1173,6 +1173,30 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 	kfree(sgt);
 }
 
+/*
+ * amdgpu_ttm_tt_pool_id - compute the ttm pool id backing a given xcp
+ *
+ * Mirrors the mapping used when creating the gtt ttm_tt, so callers that only
+ * have an xcp id (e.g. an out-of-lock preallocation before the bo exists) pick
+ * the same pool the populate will use.
+ */
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id)
+{
+	if (adev->gmc.mem_partitions && xcp_id >= 0)
+		return KFD_XCP_MEM_ID(adev, xcp_id);
+
+	return xcp_id;
+}
+
+/* amdgpu_ttm_pool - select the ttm pool for a given pool id */
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id)
+{
+	if (adev->mman.ttm_pools && pool_id >= 0)
+		return &adev->mman.ttm_pools[pool_id];
+
+	return &adev->mman.bdev.pool;
+}
+
 /**
  * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
  *
@@ -1194,10 +1218,7 @@ static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
 		return NULL;
 
 	gtt->gobj = &bo->base;
-	if (adev->gmc.mem_partitions && abo->xcp_id >= 0)
-		gtt->pool_id = KFD_XCP_MEM_ID(adev, abo->xcp_id);
-	else
-		gtt->pool_id = abo->xcp_id;
+	gtt->pool_id = amdgpu_ttm_tt_pool_id(adev, abo->xcp_id);
 
 	if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
 		caching = ttm_write_combined;
@@ -1239,10 +1260,7 @@ static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
 	if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
 		return 0;
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 	ret = ttm_pool_alloc(pool, ttm, ctx);
 	if (ret)
 		return ret;
@@ -1284,10 +1302,7 @@ static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
 
 	adev = amdgpu_ttm_adev(bdev);
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 
 	return ttm_pool_free(pool, ttm);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -0
index ff9e2e346609..f90844937b73 100644
@@ -207,6 +207,9 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
 
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id);
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id);
+
 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 				 struct amdgpu_hmm_range *range);
author maintainer
#138 lore raw

Re: [PATCH v3 33/33] drm/amdgpu: Preallocate system BO pages outside the reservation lock

Quoted 50 lines · Christian König
Quoted 12 lines · Matthew Brost
Quoted 11 lines · Christian König
Quoted 3 lines · Matthew Brost

Populating a GTT (system) buffer object under the reservation lock can stall in reclaim and compaction while trying to satisfy beneficial-order allocations, holding the lock for the duration.

Yeah and that is perfectly intentional behavior.

I really don't see any reason for any driver to change that.

The purpose of the dma_resv lock is to stall other allocation for the same object it protects, making sure that we don't have multiple threads allocating memory for the same buffer object.

Quoted 8 lines
Quoted 2 lines

Why in the world should that be a problem?

See my example in my reply to the cover letter: multiple threads sharing a VM, with a memory allocation performed under the VM dma-resv lock, which in turn blocks exec IOCTLs, CPU page faults or VM binds on BOs sharing the same VM dma-resv lock. With that, this code could be gating on whether the BO allocation shares a VM dma-resv lock.

Ah, ok I see the problem now.

Quoted 5 lines

The most relevant example here, I believe, is that launching a new Chrome tab could potentially stall the aforementioned operations in a different Chrome tab. Likewise, if games allocate memory on demand (they typically do not, because memory allocations are prone to introducing stalls), they could also incur the aforementioned stalls.

Yeah, I was already wondering how such an use case is seen with gaming or HPC.

But Chrome is indeed the right stress test for memory allocation :)

Quoted 4 lines

I think the opposite question is more appropriate here: does preallocating memory outside of dma-resv before a device can DMA to it break any invariants? The answer is no; it does not in the case of GEM create IOCTLs or the defragmentation moves introduced in this series.

Of hand it looks like a valid approach to me, but it is still quite hacky.

I would rather suggest something like that:

1. Allocate a BO #1 with a separate dma_resv lock.
2. Populate that BO with the backing store you want to have.
3. Allocate a BO #2 with the dma_resv lock of the VM, but no backing store whatsoever.
4. Lock both BO #1 and #2.
5. Use a new function ttm_bo_swap_backing_stores() to switch the backing store between the two.
6. Unlock everything and drop the reference on BO #1 to let it cleanup.

Quoted 3 lines

This way the memory/resources are always allocated to some resource and buffer object which makes it possible to track it.

Tracking the allocated memory is necessary for hot plug and eventually shrinking it.

Thanks. Let me see what the rework looks like and whether I run into any issues.

I did have a concern about the pre-allocation not being attached to any reservation, and therefore not being available for shrinking during the pre-allocation <-> reservation attachment window as well. Of course, the defrag step gets trickier here since it is not a 1:1 transfer (it's a subset plus a copy), but it is likely workable too.

Also, there's the corner case where BO #1 is immediately shrunk, though perhaps that doesn't matter and the next execution fixes it up at that point.

I'll reply here once I have a better understanding of the corner cases and whether this approach fits both goals: defragmentation and minimizing shared VM dma-resv lock hold time.

Matt

Quoted 395 lines

Regards,
Christian.

Quoted 390 lines

Matt

Quoted 387 lines

Regards,
Christian.

Quoted 383 lines

Mirror the Xe out-of-lock preallocation for pure system BOs: when a gem_create request targets AMDGPU_GEM_DOMAIN_GTT only, fill the full page backing up front via ttm_pool_prealloc_fill_full() before taking the reservation lock. The populate under the lock then simply installs these pages instead of reclaiming and compacting in the critical section. The fill is best-effort - a short fill falls back to the normal in-lock allocation for the remaining pages, and any leftover pages are released by ttm_pool_prealloc_fini() on all exit paths.

Unlike Xe, amdgpu has no background defragmenter, so the higher-order reclaim backoff is left disabled (normal reclaim). If the pool uses dma-alloc (swiotlb), the fill bails and the feature is a silent no-op.

Factor the tt pool selection into amdgpu_ttm_tt_pool_id() and amdgpu_ttm_pool() so the preallocation targets the exact pool the populate will consume, and thread an optional prealloc bag through amdgpu_gem_object_create() and amdgpu_bo_param into the populate ctx.

Cc Alex Deucher <[email protected]>
Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

---

v3:
- Keep WC caching only when USWC is supported and bound the preallocation to available GTT space (Sashiko)

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       | 73 +++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h       |  5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |  4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 39 +++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  3 +
 8 files changed, 108 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +2 -2
index 20831dbebc31..940f58848a97 100644
@@ -345,7 +345,7 @@ create_dmamap_sg_bo(struct amdgpu_device *adev,
 
 	ret = amdgpu_gem_object_create(adev, mem->bo->tbo.base.size, 1,
 			AMDGPU_GEM_DOMAIN_CPU, AMDGPU_GEM_CREATE_PREEMPTIBLE | flags,
-			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0);
+			ttm_bo_type_sg, mem->bo->tbo.base.resv, &gem_obj, 0, NULL);
 
 	amdgpu_bo_unreserve(mem->bo);
 
@@ -1811,7 +1811,7 @@ int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(
 		 domain_string(alloc_domain), xcp_id);
 
 	ret = amdgpu_gem_object_create(adev, aligned_size, 1, alloc_domain, alloc_flags,
-				       bo_type, NULL, &gobj, xcp_id + 1);
+				       bo_type, NULL, &gobj, xcp_id + 1, NULL);
 	if (ret) {
 		pr_debug("Failed to create BO on domain %s. ret %d\n",
 			 domain_string(alloc_domain), ret);
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +1 -1
index b33c300e26e2..51510e831129 100644
@@ -435,7 +435,7 @@ amdgpu_dma_buf_create_obj(struct drm_device *dev, struct dma_buf *dma_buf)
 
 	ret = amdgpu_gem_object_create(adev, dma_buf->size, PAGE_SIZE,
 				       AMDGPU_GEM_DOMAIN_CPU, flags,
-				       ttm_bo_type_sg, resv, &gobj, 0);
+				       ttm_bo_type_sg, resv, &gobj, 0, NULL);
 	if (ret)
 		goto error;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +65 -8
index 6a0699746fbc..e8b732218e7d 100644
@@ -39,6 +39,7 @@
 #include <drm/drm_exec.h>
 #include <drm/drm_gem_ttm_helper.h>
 #include <drm/ttm/ttm_tt.h>
+#include <drm/ttm/ttm_pool.h>
 #include <drm/drm_syncobj.h>
 
 #include "amdgpu.h"
@@ -168,7 +169,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1)
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc)
 {
 	struct amdgpu_bo *bo;
 	struct amdgpu_bo_user *ubo;
@@ -188,6 +190,7 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 	bp.domain = initial_domain;
 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
 	bp.xcp_id_plus1 = xcp_id_plus1;
+	bp.prealloc = prealloc;
 
 	r = amdgpu_bo_create_user(adev, &bp, &ubo);
 	if (r)
@@ -412,6 +415,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 	struct dma_resv *resv = NULL;
 	struct drm_gem_object *gobj;
 	uint32_t handle, initial_domain;
+	struct ttm_pool_prealloc prealloc = {};
+	struct ttm_pool *prealloc_pool = NULL;
 	int r;
 
 	/* reject invalid gem flags */
@@ -443,10 +448,57 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
 	}
 
+	/*
+	 * For system-only (pure GTT) BOs, preallocate the whole page backing
+	 * up front, outside the reservation lock. Populate under the lock then
+	 * just installs these pages instead of reclaiming/compacting in the
+	 * critical section. Best-effort: a short fill falls back to the normal
+	 * in-lock allocation for the missing pages.
+	 */
+	if (args->in.domains == AMDGPU_GEM_DOMAIN_GTT) {
+		struct ttm_resource_manager *gtt_man =
+			ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
+		int32_t xcp_id = adev->gmc.mem_partitions ? fpriv->xcp_id : 0;
+		int32_t pool_id = amdgpu_ttm_tt_pool_id(adev, xcp_id);
+		/*
+		 * Mirror the USWC handling in amdgpu_bo_create(): the flag is
+		 * stripped when the platform can't do write-combining, in which
+		 * case the tt is created ttm_cached. The prealloc caching must
+		 * match the final tt caching exactly, otherwise ttm_pool_free()
+		 * would restore the wrong PAT state on these pages (PAT aliasing
+		 * / leak of WC pages into the cached allocator).
+		 */
+		bool uswc = (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) &&
+			amdgpu_bo_support_uswc(flags);
+		enum ttm_caching caching =
+			uswc ? ttm_write_combined : ttm_cached;
+
+		/*
+		 * Only prealloc when the request fits the currently-available
+		 * GTT (total manager size minus current usage). This mirrors the
+		 * amdgpu_bo_validate_size() bound applied later in
+		 * amdgpu_bo_create() while also skipping the up-front reclaim /
+		 * compaction when GTT is already near full or the user-controlled
+		 * size is over-large (the creation path handles those anyway).
+		 */
+		if (gtt_man) {
+			u64 used = ttm_resource_manager_usage(gtt_man);
+			u64 avail = gtt_man->size > used ?
+				gtt_man->size - used : 0;
+
+			if (size <= avail) {
+				prealloc_pool = amdgpu_ttm_pool(adev, pool_id);
+				ttm_pool_prealloc_fill_full(prealloc_pool,
+							    caching, &prealloc,
+							    PFN_UP(size), false);
+			}
+		}
+	}
+
 	if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
 		r = amdgpu_bo_reserve(vm->root.bo, false);
 		if (r)
-			return r;
+			goto out_prealloc;
 
 		resv = vm->root.bo->tbo.base.resv;
 	}
@@ -455,7 +507,8 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 retry:
 	r = amdgpu_gem_object_create(adev, size, args->in.alignment,
 				     initial_domain,
-				     flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
+				     flags, ttm_bo_type_device, resv, &gobj,
+				     fpriv->xcp_id + 1, prealloc_pool ? &prealloc : NULL);
 	if (r && r != -ERESTARTSYS) {
 		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
 			flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
@@ -479,17 +532,21 @@ int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
 		amdgpu_bo_unreserve(vm->root.bo);
 	}
 	if (r)
-		return r;
+		goto out_prealloc;
 
 	r = drm_gem_handle_create(filp, gobj, &handle);
 	/* drop reference from allocate - handle holds it now */
 	drm_gem_object_put(gobj);
 	if (r)
-		return r;
+		goto out_prealloc;
 
 	memset(args, 0, sizeof(*args));
 	args->out.handle = handle;
-	return 0;
+
+out_prealloc:
+	if (prealloc_pool)
+		ttm_pool_prealloc_fini(prealloc_pool, &prealloc);
+	return r;
 }
 
 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
@@ -528,7 +585,7 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
 
 	/* create a gem object to contain this object in */
 	r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
-				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
+				     0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1, NULL);
 	if (r)
 		return r;
 
@@ -1298,7 +1355,7 @@ int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 	domain = amdgpu_bo_get_preferred_domain(adev,
 				amdgpu_display_supported_domains(adev, flags));
 	r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
-				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
+				     ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1, NULL);
 	if (r)
 		return -ENOMEM;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +4 -1
index b558336bc4c6..706aeca011f1 100644
@@ -35,6 +35,8 @@
 
 extern const struct drm_gem_object_funcs amdgpu_gem_object_funcs;
 
+struct ttm_pool_prealloc;
+
 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns);
 
 /*
@@ -45,7 +47,8 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
 			     int alignment, u32 initial_domain,
 			     u64 flags, enum ttm_bo_type type,
 			     struct dma_resv *resv,
-			     struct drm_gem_object **obj, int8_t xcp_id_plus1);
+			     struct drm_gem_object **obj, int8_t xcp_id_plus1,
+			     struct ttm_pool_prealloc *prealloc);
 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
 			    struct drm_device *dev,
 			    struct drm_mode_create_dumb *args);
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -1
index f98bfba59a2c..18c4cf3f35a5 100644
@@ -632,7 +632,8 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
 		/* We opt to avoid OOM on system pages allocations */
 		.gfp_retry_mayfail = true,
 		.allow_res_evict = bp->type != ttm_bo_type_kernel,
-		.resv = bp->resv
+		.resv = bp->resv,
+		.prealloc = bp->prealloc,
 	};
 	struct amdgpu_bo *bo;
 	unsigned long page_align, size = bp->size;
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +4 -0
index ff11a0903499..11f1d403f152 100644
@@ -45,6 +45,8 @@
 #define to_amdgpu_bo_user(abo) container_of((abo), struct amdgpu_bo_user, bo)
 #define to_amdgpu_bo_vm(abo) container_of((abo), struct amdgpu_bo_vm, bo)
 
+struct ttm_pool_prealloc;
+
 struct amdgpu_bo_param {
 	unsigned long			size;
 	int				byte_align;
@@ -58,6 +60,8 @@ struct amdgpu_bo_param {
 	void				(*destroy)(struct ttm_buffer_object *bo);
 	/* xcp partition number plus 1, 0 means any partition */
 	int8_t				xcp_id_plus1;
+	/* optional out-of-lock preallocated backing (system/GTT only) */
+	struct ttm_pool_prealloc	*prealloc;
 };
 
 /* bo virtual addresses in a vm */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -12
index b10b0878df37..c9cd4714f515 100644
@@ -1173,6 +1173,30 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 	kfree(sgt);
 }
 
+/*
+ * amdgpu_ttm_tt_pool_id - compute the ttm pool id backing a given xcp
+ *
+ * Mirrors the mapping used when creating the gtt ttm_tt, so callers that only
+ * have an xcp id (e.g. an out-of-lock preallocation before the bo exists) pick
+ * the same pool the populate will use.
+ */
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id)
+{
+	if (adev->gmc.mem_partitions && xcp_id >= 0)
+		return KFD_XCP_MEM_ID(adev, xcp_id);
+
+	return xcp_id;
+}
+
+/* amdgpu_ttm_pool - select the ttm pool for a given pool id */
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id)
+{
+	if (adev->mman.ttm_pools && pool_id >= 0)
+		return &adev->mman.ttm_pools[pool_id];
+
+	return &adev->mman.bdev.pool;
+}
+
 /**
  * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
  *
@@ -1194,10 +1218,7 @@ static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
 		return NULL;
 
 	gtt->gobj = &bo->base;
-	if (adev->gmc.mem_partitions && abo->xcp_id >= 0)
-		gtt->pool_id = KFD_XCP_MEM_ID(adev, abo->xcp_id);
-	else
-		gtt->pool_id = abo->xcp_id;
+	gtt->pool_id = amdgpu_ttm_tt_pool_id(adev, abo->xcp_id);
 
 	if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
 		caching = ttm_write_combined;
@@ -1239,10 +1260,7 @@ static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
 	if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
 		return 0;
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 	ret = ttm_pool_alloc(pool, ttm, ctx);
 	if (ret)
 		return ret;
@@ -1284,10 +1302,7 @@ static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
 
 	adev = amdgpu_ttm_adev(bdev);
 
-	if (adev->mman.ttm_pools && gtt->pool_id >= 0)
-		pool = &adev->mman.ttm_pools[gtt->pool_id];
-	else
-		pool = &adev->mman.bdev.pool;
+	pool = amdgpu_ttm_pool(adev, gtt->pool_id);
 
 	return ttm_pool_free(pool, ttm);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -0
index ff9e2e346609..f90844937b73 100644
@@ -207,6 +207,9 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
 
+int32_t amdgpu_ttm_tt_pool_id(struct amdgpu_device *adev, int32_t xcp_id);
+struct ttm_pool *amdgpu_ttm_pool(struct amdgpu_device *adev, int32_t pool_id);
+
 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 				 struct amdgpu_hmm_range *range);
#139 lore raw

✗ CI.checkpatch: warning for drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)
URL : https://patchwork.freedesktop.org/series/170216/
State : warning

== Summary ==

+ KERNEL=/kernel
+ git clone https://gitlab.freedesktop.org/drm/maintainer-tools mt Cloning into 'mt'...
warning: redirecting to https://gitlab.freedesktop.org/drm/maintainer-tools.git/
+ git -C mt rev-list -n1 origin/master 061140b9bc586ae7f40abc1249c97e1cc72d1b9d
+ cd /kernel
+ git config --global --add safe.directory /kernel
+ git log -n1
commit 6eb65404a477a97b6ab1a1a6627e56627848a1d3

Author: Matthew Brost <[email protected]>
Date:   Fri Jul 10 19:56:19 2026 -0700

drm/amdgpu: Preallocate system BO pages outside the reservation lock

Populating a GTT (system) buffer object under the reservation lock can stall in reclaim and compaction while trying to satisfy beneficial-order allocations, holding the lock for the duration.

Mirror the Xe out-of-lock preallocation for pure system BOs: when a gem_create request targets AMDGPU_GEM_DOMAIN_GTT only, fill the full page backing up front via ttm_pool_prealloc_fill_full() before taking the reservation lock. The populate under the lock then simply installs these pages instead of reclaiming and compacting in the critical section. The fill is best-effort - a short fill falls back to the normal in-lock allocation for the remaining pages, and any leftover pages are released by ttm_pool_prealloc_fini() on all exit paths.

Unlike Xe, amdgpu has no background defragmenter, so the higher-order reclaim backoff is left disabled (normal reclaim). If the pool uses dma-alloc (swiotlb), the fill bails and the feature is a silent no-op.

Factor the tt pool selection into amdgpu_ttm_tt_pool_id() and amdgpu_ttm_pool() so the preallocation targets the exact pool the populate will consume, and thread an optional prealloc bag through amdgpu_gem_object_create() and amdgpu_bo_param into the populate ctx.

Cc Alex Deucher <[email protected]>
Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>
Assisted-by GitHub_Copilot:claude-opus-4.8

+ /mt/dim checkpatch 7bb9a26014f9ba1b2cb1300252d1eeb83f169c06 drm-intel ca5b60f2a01e drm/ttm/pool: Allow backing off reclaim at the beneficial order 82a5853bb4f8 drm/ttm/pool: Add ttm_pool_page_order_nodma() helper 32347960f287 drm/ttm: Record sub-optimal page order allocations in ttm_tt 79e3e12a7e6c drm/ttm: Introduce ttm_pool_alloc_iter for __ttm_pool_alloc() 99b4cb07cbbc drm/ttm: Support defragmentation moves
d9cd771927d9 drm/ttm: Add fault injection for beneficial-order allocation failures a83648373640 drm/ttm: Harvest beneficial-order pages on defragmentation moves -:311: WARNING:PREFER_FALLTHROUGH: Prefer 'fallthrough;' over fallthrough comment #311: FILE: drivers/gpu/drm/ttm/ttm_pool.c:974:
+ * fall through to a fresh allocation, or a negative errno.

total: 0 errors, 1 warnings, 0 checks, 457 lines checked 8ed9f29c1b20 drm/ttm: Bound page (re)allocation per defragmentation move 9e54c19ebb56 drm/ttm: Preallocate beneficial-order defrag pages outside the lock -:355: WARNING:PREFER_FALLTHROUGH: Prefer 'fallthrough;' over fallthrough comment #355: FILE: drivers/gpu/drm/ttm/ttm_pool.c:1278:
+ * reclaim/compaction). Falls through once the bag is empty.

total: 0 errors, 1 warnings, 0 checks, 452 lines checked 0e15cbe02f3b drm/ttm: Add full out-of-lock preallocation for ttm_pool_alloc() -:313: WARNING:PREFER_FALLTHROUGH: Prefer 'fallthrough;' over fallthrough comment #313: FILE: drivers/gpu/drm/ttm/ttm_pool.c:1455:
+ * taking its order from the page. Falls through to in-line allocation

total: 0 errors, 1 warnings, 0 checks, 336 lines checked f31c496aaaad drm/gpusvm: Add a DMA-mapping accounting callback 395e8e01f190 drm/xe: Add debugfs stats for DMA-mapped pages per order afe121cac666 drm/xe: Flush L2 asynchronously in xe_bo_trigger_rebind() dd394d166c5e drm/xe: Destroy page tables after unlinking all VMAs on VM close b9ec005a2558 drm/xe: Track BOs backed at a sub-optimal page order 28e16b5385ca drm/xe: Back off beneficial-order reclaim under defrag pressure b354fd96d207 drm/xe: Add xe_migrate_copy_defrag() for on-GPU defrag copies fe324c71e70f drm/xe: Handle defrag moves in xe_bo_move()
-:66: WARNING:PREFER_FALLTHROUGH: Prefer 'fallthrough;' over fallthrough comment #66: FILE: drivers/gpu/drm/xe/xe_bo.c:1217:
+ * For a defrag move the old and new tt differ, so fallthrough to the

total: 0 errors, 1 warnings, 0 checks, 54 lines checked 7058b4dbabda drm/xe: Skip self-copies for borrowed pages on defrag moves dfca735205ea drm/xe: Add a page defragmentation worker da2e92bf7288 drm/xe: Add defrag GT stats
7f74453aa6ad drm/xe: Add Kconfig.profile options for BO defrag configuration 9f95b5c35377 drm/xe: Defrag using out-of-lock page preallocation ffab6edee52c drm/xe: Add defrag profiling tracepoints
-:124: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #124: FILE: drivers/gpu/drm/xe/xe_trace_bo.h:92:
+ TP_STRUCT__entry(

-:130: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #130: FILE: drivers/gpu/drm/xe/xe_trace_bo.h:98:
+ TP_fast_assign(

-:147: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #147: FILE: drivers/gpu/drm/xe/xe_trace_bo.h:115:
+ TP_STRUCT__entry(

-:159: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #159: FILE: drivers/gpu/drm/xe/xe_trace_bo.h:127:
+ TP_fast_assign(

total: 0 errors, 0 warnings, 4 checks, 128 lines checked cc16137f500c drm/xe: Preallocate system BO backing outside the dma-resv lock aa94481ff3fb drm/xe: Add tracepoint for xe_gem_create_ioctl -:89: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #89: FILE: drivers/gpu/drm/xe/xe_bo.c:4157:
+ div_u64(ktime_to_ns(ktime_sub(ktime_get(),
+ ioctl_start)), 1000),

-:108: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #108: FILE: drivers/gpu/drm/xe/xe_trace_bo.h:152:
+ TP_STRUCT__entry(

-:118: CHECK:OPEN_ENDED_LINE: Lines should not end with a '(' #118: FILE: drivers/gpu/drm/xe/xe_trace_bo.h:162:
+ TP_fast_assign(

total: 0 errors, 0 warnings, 3 checks, 84 lines checked
471be044e3c0 drm/xe: Add IOVA-based xe_res_cursor variant 733160fb85cc drm/xe: Use IOVA-based DMA mapping for eligible tt BOs dc6a0019172f drm/xe: Add per-device dependency scheduler for IOVA defrag finalize 1e6fd95f81a1 drm/xe: Add packed copy-step IOVA mapping for defrag c2175a7158fc drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies bf0da55143cb drm/xe: Finalize defrag-IOVA moves with post-copy job 6eb65404a477 drm/amdgpu: Preallocate system BO pages outside the reservation lock -:138: CHECK:PREFER_KERNEL_TYPES: Prefer kernel type 's32' over 'int32_t' #138: FILE: drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c:461:
+ int32_t xcp_id = adev->gmc.mem_partitions ? fpriv->xcp_id : 0;

-:139: CHECK:PREFER_KERNEL_TYPES: Prefer kernel type 's32' over 'int32_t' #139: FILE: drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c:462:
+ int32_t pool_id = amdgpu_ttm_tt_pool_id(adev, xcp_id);

total: 0 errors, 0 warnings, 2 checks, 276 lines checked

#140 lore raw

✓ CI.KUnit: success for drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)
URL : https://patchwork.freedesktop.org/series/170216/
State : success

== Summary ==

+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig [03:05:42] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[03:05:46] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=48
[03:06:18] Starting KUnit Kernel (1/1)...
[03:06:18] ============================================================
Running tests with:
$ .kunit/linux kunit.enable=1 mem=1G console=tty kunit_shutdown=halt
[03:06:18] ================== guc_buf (11 subtests) ===================
[03:06:18] [PASSED] test_smallest
[03:06:18] [PASSED] test_largest
[03:06:18] [PASSED] test_granular
[03:06:18] [PASSED] test_unique
[03:06:18] [PASSED] test_overlap
[03:06:18] [PASSED] test_reusable
[03:06:18] [PASSED] test_too_big
[03:06:18] [PASSED] test_flush
[03:06:18] [PASSED] test_lookup
[03:06:18] [PASSED] test_data
[03:06:18] [PASSED] test_class
[03:06:18] ===================== [PASSED] guc_buf =====================
[03:06:18] =================== guc_dbm (7 subtests) ===================
[03:06:18] [PASSED] test_empty
[03:06:18] [PASSED] test_default
[03:06:18] ======================== test_size  ========================
[03:06:18] [PASSED] 4
[03:06:18] [PASSED] 8
[03:06:18] [PASSED] 32
[03:06:18] [PASSED] 256
[03:06:18] ==================== [PASSED] test_size ====================
[03:06:18] ======================= test_reuse  ========================
[03:06:18] [PASSED] 4
[03:06:18] [PASSED] 8
[03:06:18] [PASSED] 32
[03:06:18] [PASSED] 256
[03:06:18] =================== [PASSED] test_reuse ====================
[03:06:18] =================== test_range_overlap  ====================
[03:06:18] [PASSED] 4
[03:06:18] [PASSED] 8
[03:06:18] [PASSED] 32
[03:06:18] [PASSED] 256
[03:06:18] =============== [PASSED] test_range_overlap ================
[03:06:18] =================== test_range_compact  ====================
[03:06:18] [PASSED] 4
[03:06:18] [PASSED] 8
[03:06:18] [PASSED] 32
[03:06:18] [PASSED] 256
[03:06:18] =============== [PASSED] test_range_compact ================
[03:06:18] ==================== test_range_spare  =====================
[03:06:18] [PASSED] 4
[03:06:18] [PASSED] 8
[03:06:18] [PASSED] 32
[03:06:18] [PASSED] 256
[03:06:18] ================ [PASSED] test_range_spare =================
[03:06:18] ===================== [PASSED] guc_dbm =====================
[03:06:18] =================== guc_idm (6 subtests) ===================
[03:06:18] [PASSED] bad_init
[03:06:18] [PASSED] no_init
[03:06:18] [PASSED] init_fini
[03:06:18] [PASSED] check_used
[03:06:18] [PASSED] check_quota
[03:06:18] [PASSED] check_all
[03:06:18] ===================== [PASSED] guc_idm =====================
[03:06:18] ================== no_relay (3 subtests) ===================
[03:06:18] [PASSED] xe_drops_guc2pf_if_not_ready
[03:06:18] [PASSED] xe_drops_guc2vf_if_not_ready
[03:06:18] [PASSED] xe_rejects_send_if_not_ready
[03:06:18] ==================== [PASSED] no_relay =====================
[03:06:18] ================== pf_relay (14 subtests) ==================
[03:06:18] [PASSED] pf_rejects_guc2pf_too_short
[03:06:18] [PASSED] pf_rejects_guc2pf_too_long
[03:06:18] [PASSED] pf_rejects_guc2pf_no_payload
[03:06:18] [PASSED] pf_fails_no_payload
[03:06:18] [PASSED] pf_fails_bad_origin
[03:06:18] [PASSED] pf_fails_bad_type
[03:06:18] [PASSED] pf_txn_reports_error
[03:06:18] [PASSED] pf_txn_sends_pf2guc
[03:06:18] [PASSED] pf_sends_pf2guc
[03:06:18] [SKIPPED] pf_loopback_nop (requires CONFIG_DRM_XE_DEBUG_SRIOV)
[03:06:18] [SKIPPED] pf_loopback_echo (requires CONFIG_DRM_XE_DEBUG_SRIOV)
[03:06:18] [SKIPPED] pf_loopback_fail (requires CONFIG_DRM_XE_DEBUG_SRIOV)
[03:06:18] [SKIPPED] pf_loopback_busy (requires CONFIG_DRM_XE_DEBUG_SRIOV)
[03:06:18] [SKIPPED] pf_loopback_retry (requires CONFIG_DRM_XE_DEBUG_SRIOV)
[03:06:18] ==================== [PASSED] pf_relay =====================
[03:06:18] ================== vf_relay (3 subtests) ===================
[03:06:18] [PASSED] vf_rejects_guc2vf_too_short
[03:06:18] [PASSED] vf_rejects_guc2vf_too_long
[03:06:18] [PASSED] vf_rejects_guc2vf_no_payload
[03:06:18] ==================== [PASSED] vf_relay =====================
[03:06:18] ================ pf_gt_config (9 subtests) =================
[03:06:18] [PASSED] fair_contexts_1vf
[03:06:18] [PASSED] fair_doorbells_1vf
[03:06:18] [PASSED] fair_ggtt_1vf
[03:06:18] ====================== fair_vram_1vf  ======================
[03:06:18] [PASSED] 3.50 GiB
[03:06:18] [PASSED] 11.5 GiB
[03:06:18] [PASSED] 15.5 GiB
[03:06:18] [PASSED] 31.5 GiB
[03:06:18] [PASSED] 63.5 GiB
[03:06:18] [PASSED] 1.91 GiB
[03:06:18] ================== [PASSED] fair_vram_1vf ==================
[03:06:18] ================ fair_vram_1vf_admin_only  =================
[03:06:18] [PASSED] 3.50 GiB
[03:06:18] [PASSED] 11.5 GiB
[03:06:18] [PASSED] 15.5 GiB
[03:06:18] [PASSED] 31.5 GiB
[03:06:18] [PASSED] 63.5 GiB
[03:06:18] [PASSED] 1.91 GiB
[03:06:18] ============ [PASSED] fair_vram_1vf_admin_only =============
[03:06:18] ====================== fair_contexts  ======================
[03:06:18] [PASSED] 1 VF
[03:06:18] [PASSED] 2 VFs
[03:06:18] [PASSED] 3 VFs
[03:06:18] [PASSED] 4 VFs
[03:06:18] [PASSED] 5 VFs
[03:06:18] [PASSED] 6 VFs
[03:06:18] [PASSED] 7 VFs
[03:06:18] [PASSED] 8 VFs
[03:06:18] [PASSED] 9 VFs
[03:06:18] [PASSED] 10 VFs
[03:06:18] [PASSED] 11 VFs
[03:06:18] [PASSED] 12 VFs
[03:06:18] [PASSED] 13 VFs
[03:06:18] [PASSED] 14 VFs
[03:06:18] [PASSED] 15 VFs
[03:06:18] [PASSED] 16 VFs
[03:06:18] [PASSED] 17 VFs
[03:06:18] [PASSED] 18 VFs
[03:06:18] [PASSED] 19 VFs
[03:06:18] [PASSED] 20 VFs
[03:06:18] [PASSED] 21 VFs
[03:06:18] [PASSED] 22 VFs
[03:06:18] [PASSED] 23 VFs
[03:06:18] [PASSED] 24 VFs
[03:06:18] [PASSED] 25 VFs
[03:06:18] [PASSED] 26 VFs
[03:06:18] [PASSED] 27 VFs
[03:06:18] [PASSED] 28 VFs
[03:06:18] [PASSED] 29 VFs
[03:06:18] [PASSED] 30 VFs
[03:06:18] [PASSED] 31 VFs
[03:06:18] [PASSED] 32 VFs
[03:06:18] [PASSED] 33 VFs
[03:06:18] [PASSED] 34 VFs
[03:06:18] [PASSED] 35 VFs
[03:06:18] [PASSED] 36 VFs
[03:06:18] [PASSED] 37 VFs
[03:06:18] [PASSED] 38 VFs
[03:06:18] [PASSED] 39 VFs
[03:06:18] [PASSED] 40 VFs
[03:06:18] [PASSED] 41 VFs
[03:06:18] [PASSED] 42 VFs
[03:06:18] [PASSED] 43 VFs
[03:06:18] [PASSED] 44 VFs
[03:06:18] [PASSED] 45 VFs
[03:06:18] [PASSED] 46 VFs
[03:06:18] [PASSED] 47 VFs
[03:06:18] [PASSED] 48 VFs
[03:06:18] [PASSED] 49 VFs
[03:06:18] [PASSED] 50 VFs
[03:06:18] [PASSED] 51 VFs
[03:06:18] [PASSED] 52 VFs
[03:06:18] [PASSED] 53 VFs
[03:06:18] [PASSED] 54 VFs
[03:06:18] [PASSED] 55 VFs
[03:06:18] [PASSED] 56 VFs
[03:06:18] [PASSED] 57 VFs
[03:06:18] [PASSED] 58 VFs
[03:06:18] [PASSED] 59 VFs
[03:06:18] [PASSED] 60 VFs
[03:06:18] [PASSED] 61 VFs
[03:06:18] [PASSED] 62 VFs
[03:06:18] [PASSED] 63 VFs
[03:06:18] ================== [PASSED] fair_contexts ==================
[03:06:18] ===================== fair_doorbells  ======================
[03:06:18] [PASSED] 1 VF
[03:06:18] [PASSED] 2 VFs
[03:06:18] [PASSED] 3 VFs
[03:06:18] [PASSED] 4 VFs
[03:06:18] [PASSED] 5 VFs
[03:06:18] [PASSED] 6 VFs
[03:06:18] [PASSED] 7 VFs
[03:06:18] [PASSED] 8 VFs
[03:06:18] [PASSED] 9 VFs
[03:06:18] [PASSED] 10 VFs
[03:06:18] [PASSED] 11 VFs
[03:06:18] [PASSED] 12 VFs
[03:06:18] [PASSED] 13 VFs
[03:06:18] [PASSED] 14 VFs
[03:06:18] [PASSED] 15 VFs
[03:06:18] [PASSED] 16 VFs
[03:06:18] [PASSED] 17 VFs
[03:06:18] [PASSED] 18 VFs
[03:06:18] [PASSED] 19 VFs
[03:06:18] [PASSED] 20 VFs
[03:06:18] [PASSED] 21 VFs
[03:06:18] [PASSED] 22 VFs
[03:06:18] [PASSED] 23 VFs
[03:06:18] [PASSED] 24 VFs
[03:06:18] [PASSED] 25 VFs
[03:06:18] [PASSED] 26 VFs
[03:06:18] [PASSED] 27 VFs
[03:06:18] [PASSED] 28 VFs
[03:06:18] [PASSED] 29 VFs
[03:06:18] [PASSED] 30 VFs
[03:06:18] [PASSED] 31 VFs
[03:06:18] [PASSED] 32 VFs
[03:06:18] [PASSED] 33 VFs
[03:06:18] [PASSED] 34 VFs
[03:06:18] [PASSED] 35 VFs
[03:06:18] [PASSED] 36 VFs
[03:06:18] [PASSED] 37 VFs
[03:06:18] [PASSED] 38 VFs
[03:06:18] [PASSED] 39 VFs
[03:06:18] [PASSED] 40 VFs
[03:06:18] [PASSED] 41 VFs
[03:06:18] [PASSED] 42 VFs
[03:06:18] [PASSED] 43 VFs
[03:06:18] [PASSED] 44 VFs
[03:06:18] [PASSED] 45 VFs
[03:06:18] [PASSED] 46 VFs
[03:06:18] [PASSED] 47 VFs
[03:06:18] [PASSED] 48 VFs
[03:06:18] [PASSED] 49 VFs
[03:06:18] [PASSED] 50 VFs
[03:06:18] [PASSED] 51 VFs
[03:06:18] [PASSED] 52 VFs
[03:06:18] [PASSED] 53 VFs
[03:06:18] [PASSED] 54 VFs
[03:06:18] [PASSED] 55 VFs
[03:06:18] [PASSED] 56 VFs
[03:06:18] [PASSED] 57 VFs
[03:06:18] [PASSED] 58 VFs
[03:06:18] [PASSED] 59 VFs
[03:06:18] [PASSED] 60 VFs
[03:06:18] [PASSED] 61 VFs
[03:06:18] [PASSED] 62 VFs
[03:06:18] [PASSED] 63 VFs
[03:06:18] ================= [PASSED] fair_doorbells ==================
[03:06:18] ======================== fair_ggtt  ========================
[03:06:18] [PASSED] 1 VF
[03:06:18] [PASSED] 2 VFs
[03:06:18] [PASSED] 3 VFs
[03:06:18] [PASSED] 4 VFs
[03:06:18] [PASSED] 5 VFs
[03:06:18] [PASSED] 6 VFs
[03:06:18] [PASSED] 7 VFs
[03:06:18] [PASSED] 8 VFs
[03:06:18] [PASSED] 9 VFs
[03:06:18] [PASSED] 10 VFs
[03:06:18] [PASSED] 11 VFs
[03:06:18] [PASSED] 12 VFs
[03:06:18] [PASSED] 13 VFs
[03:06:18] [PASSED] 14 VFs
[03:06:18] [PASSED] 15 VFs
[03:06:18] [PASSED] 16 VFs
[03:06:18] [PASSED] 17 VFs
[03:06:18] [PASSED] 18 VFs
[03:06:18] [PASSED] 19 VFs
[03:06:18] [PASSED] 20 VFs
[03:06:18] [PASSED] 21 VFs
[03:06:18] [PASSED] 22 VFs
[03:06:18] [PASSED] 23 VFs
[03:06:18] [PASSED] 24 VFs
[03:06:18] [PASSED] 25 VFs
[03:06:18] [PASSED] 26 VFs
[03:06:18] [PASSED] 27 VFs
[03:06:18] [PASSED] 28 VFs
[03:06:18] [PASSED] 29 VFs
[03:06:18] [PASSED] 30 VFs
[03:06:18] [PASSED] 31 VFs
[03:06:18] [PASSED] 32 VFs
[03:06:18] [PASSED] 33 VFs
[03:06:18] [PASSED] 34 VFs
[03:06:18] [PASSED] 35 VFs
[03:06:18] [PASSED] 36 VFs
[03:06:18] [PASSED] 37 VFs
[03:06:18] [PASSED] 38 VFs
[03:06:18] [PASSED] 39 VFs
[03:06:18] [PASSED] 40 VFs
[03:06:18] [PASSED] 41 VFs
[03:06:18] [PASSED] 42 VFs
[03:06:18] [PASSED] 43 VFs
[03:06:18] [PASSED] 44 VFs
[03:06:18] [PASSED] 45 VFs
[03:06:18] [PASSED] 46 VFs
[03:06:18] [PASSED] 47 VFs
[03:06:18] [PASSED] 48 VFs
[03:06:18] [PASSED] 49 VFs
[03:06:18] [PASSED] 50 VFs
[03:06:18] [PASSED] 51 VFs
[03:06:18] [PASSED] 52 VFs
[03:06:18] [PASSED] 53 VFs
[03:06:18] [PASSED] 54 VFs
[03:06:18] [PASSED] 55 VFs
[03:06:18] [PASSED] 56 VFs
[03:06:18] [PASSED] 57 VFs
[03:06:18] [PASSED] 58 VFs
[03:06:18] [PASSED] 59 VFs
[03:06:18] [PASSED] 60 VFs
[03:06:18] [PASSED] 61 VFs
[03:06:18] [PASSED] 62 VFs
[03:06:18] [PASSED] 63 VFs
[03:06:18] ==================== [PASSED] fair_ggtt ====================
[03:06:18] ======================== fair_vram  ========================
[03:06:18] [PASSED] 1 VF
[03:06:18] [PASSED] 2 VFs
[03:06:18] [PASSED] 3 VFs
[03:06:18] [PASSED] 4 VFs
[03:06:18] [PASSED] 5 VFs
[03:06:18] [PASSED] 6 VFs
[03:06:18] [PASSED] 7 VFs
[03:06:18] [PASSED] 8 VFs
[03:06:18] [PASSED] 9 VFs
[03:06:18] [PASSED] 10 VFs
[03:06:18] [PASSED] 11 VFs
[03:06:18] [PASSED] 12 VFs
[03:06:18] [PASSED] 13 VFs
[03:06:18] [PASSED] 14 VFs
[03:06:18] [PASSED] 15 VFs
[03:06:18] [PASSED] 16 VFs
[03:06:18] [PASSED] 17 VFs
[03:06:18] [PASSED] 18 VFs
[03:06:18] [PASSED] 19 VFs
[03:06:18] [PASSED] 20 VFs
[03:06:18] [PASSED] 21 VFs
[03:06:18] [PASSED] 22 VFs
[03:06:18] [PASSED] 23 VFs
[03:06:18] [PASSED] 24 VFs
[03:06:18] [PASSED] 25 VFs
[03:06:18] [PASSED] 26 VFs
[03:06:18] [PASSED] 27 VFs
[03:06:18] [PASSED] 28 VFs
[03:06:18] [PASSED] 29 VFs
[03:06:18] [PASSED] 30 VFs
[03:06:18] [PASSED] 31 VFs
[03:06:18] [PASSED] 32 VFs
[03:06:18] [PASSED] 33 VFs
[03:06:18] [PASSED] 34 VFs
[03:06:18] [PASSED] 35 VFs
[03:06:18] [PASSED] 36 VFs
[03:06:18] [PASSED] 37 VFs
[03:06:18] [PASSED] 38 VFs
[03:06:18] [PASSED] 39 VFs
[03:06:18] [PASSED] 40 VFs
[03:06:18] [PASSED] 41 VFs
[03:06:18] [PASSED] 42 VFs
[03:06:18] [PASSED] 43 VFs
[03:06:18] [PASSED] 44 VFs
[03:06:18] [PASSED] 45 VFs
[03:06:18] [PASSED] 46 VFs
[03:06:18] [PASSED] 47 VFs
[03:06:18] [PASSED] 48 VFs
[03:06:18] [PASSED] 49 VFs
[03:06:18] [PASSED] 50 VFs
[03:06:18] [PASSED] 51 VFs
[03:06:18] [PASSED] 52 VFs
[03:06:18] [PASSED] 53 VFs
[03:06:18] [PASSED] 54 VFs
[03:06:18] [PASSED] 55 VFs
[03:06:18] [PASSED] 56 VFs
[03:06:18] [PASSED] 57 VFs
[03:06:18] [PASSED] 58 VFs
[03:06:18] [PASSED] 59 VFs
[03:06:18] [PASSED] 60 VFs
[03:06:18] [PASSED] 61 VFs
[03:06:18] [PASSED] 62 VFs
[03:06:18] [PASSED] 63 VFs
[03:06:18] ==================== [PASSED] fair_vram ====================
[03:06:18] ================== [PASSED] pf_gt_config ===================
[03:06:18] ===================== lmtt (1 subtest) =====================
[03:06:18] ======================== test_ops  =========================
[03:06:18] [PASSED] 2-level
[03:06:18] [PASSED] multi-level
[03:06:18] ==================== [PASSED] test_ops =====================
[03:06:18] ====================== [PASSED] lmtt =======================
[03:06:18] ================= pf_service (11 subtests) =================
[03:06:18] [PASSED] pf_negotiate_any
[03:06:18] [PASSED] pf_negotiate_base_match
[03:06:18] [PASSED] pf_negotiate_base_newer
[03:06:18] [PASSED] pf_negotiate_base_next
[03:06:18] [SKIPPED] pf_negotiate_base_older (no older minor)
[03:06:18] [PASSED] pf_negotiate_base_prev
[03:06:18] [PASSED] pf_negotiate_latest_match
[03:06:18] [PASSED] pf_negotiate_latest_newer
[03:06:18] [PASSED] pf_negotiate_latest_next
[03:06:18] [SKIPPED] pf_negotiate_latest_older (no older minor)
[03:06:18] [SKIPPED] pf_negotiate_latest_prev (no prev major)
[03:06:18] =================== [PASSED] pf_service ====================
[03:06:18] ================= xe_guc_g2g (2 subtests) ==================
[03:06:18] ============== xe_live_guc_g2g_kunit_default  ==============
[03:06:18] ========= [SKIPPED] xe_live_guc_g2g_kunit_default ==========
[03:06:18] ============== xe_live_guc_g2g_kunit_allmem  ===============
[03:06:18] ========== [SKIPPED] xe_live_guc_g2g_kunit_allmem ==========
[03:06:18] =================== [SKIPPED] xe_guc_g2g ===================
[03:06:18] =================== xe_mocs (2 subtests) ===================
[03:06:18] ================ xe_live_mocs_kernel_kunit  ================
[03:06:18] =========== [SKIPPED] xe_live_mocs_kernel_kunit ============
[03:06:18] ================ xe_live_mocs_reset_kunit  =================
[03:06:18] ============ [SKIPPED] xe_live_mocs_reset_kunit ============
[03:06:18] ==================== [SKIPPED] xe_mocs =====================
[03:06:18] ================= xe_migrate (2 subtests) ==================
[03:06:18] ================= xe_migrate_sanity_kunit  =================
[03:06:18] ============ [SKIPPED] xe_migrate_sanity_kunit =============
[03:06:18] ================== xe_validate_ccs_kunit  ==================
[03:06:18] ============= [SKIPPED] xe_validate_ccs_kunit ==============
[03:06:18] =================== [SKIPPED] xe_migrate ===================
[03:06:18] ================== xe_dma_buf (1 subtest) ==================
[03:06:18] ==================== xe_dma_buf_kunit  =====================
[03:06:18] ================ [SKIPPED] xe_dma_buf_kunit ================
[03:06:18] =================== [SKIPPED] xe_dma_buf ===================
[03:06:18] ================= xe_bo_shrink (1 subtest) =================
[03:06:18] =================== xe_bo_shrink_kunit  ====================
[03:06:18] =============== [SKIPPED] xe_bo_shrink_kunit ===============
[03:06:18] ================== [SKIPPED] xe_bo_shrink ==================
[03:06:18] ==================== xe_bo (2 subtests) ====================
[03:06:18] ================== xe_ccs_migrate_kunit  ===================
[03:06:18] ============== [SKIPPED] xe_ccs_migrate_kunit ==============
[03:06:18] ==================== xe_bo_evict_kunit  ====================
[03:06:18] =============== [SKIPPED] xe_bo_evict_kunit ================
[03:06:18] ===================== [SKIPPED] xe_bo ======================
[03:06:18] ==================== args (13 subtests) ====================
[03:06:18] [PASSED] count_args_test
[03:06:18] [PASSED] call_args_example
[03:06:18] [PASSED] call_args_test
[03:06:18] [PASSED] drop_first_arg_example
[03:06:18] [PASSED] drop_first_arg_test
[03:06:18] [PASSED] first_arg_example
[03:06:18] [PASSED] first_arg_test
[03:06:18] [PASSED] last_arg_example
[03:06:18] [PASSED] last_arg_test
[03:06:18] [PASSED] pick_arg_example
[03:06:18] [PASSED] if_args_example
[03:06:18] [PASSED] if_args_test
[03:06:18] [PASSED] sep_comma_example
[03:06:18] ====================== [PASSED] args =======================
[03:06:18] =================== xe_pci (3 subtests) ====================
[03:06:18] ==================== check_graphics_ip  ====================
[03:06:18] [PASSED] 12.00 Xe_LP
[03:06:18] [PASSED] 12.10 Xe_LP+
[03:06:18] [PASSED] 12.55 Xe_HPG
[03:06:18] [PASSED] 12.60 Xe_HPC
[03:06:18] [PASSED] 12.70 Xe_LPG
[03:06:18] [PASSED] 12.71 Xe_LPG
[03:06:18] [PASSED] 12.74 Xe_LPG+
[03:06:18] [PASSED] 20.01 Xe2_HPG
[03:06:18] [PASSED] 20.02 Xe2_HPG
[03:06:18] [PASSED] 20.04 Xe2_LPG
[03:06:18] [PASSED] 30.00 Xe3_LPG
[03:06:18] [PASSED] 30.01 Xe3_LPG
[03:06:18] [PASSED] 30.03 Xe3_LPG
[03:06:18] [PASSED] 30.04 Xe3_LPG
[03:06:18] [PASSED] 30.05 Xe3_LPG
[03:06:18] [PASSED] 35.10 Xe3p_LPG
[03:06:18] [PASSED] 35.11 Xe3p_XPC
[03:06:18] ================ [PASSED] check_graphics_ip ================
[03:06:18] ===================== check_media_ip  ======================
[03:06:18] [PASSED] 12.00 Xe_M
[03:06:18] [PASSED] 12.55 Xe_HPM
[03:06:18] [PASSED] 13.00 Xe_LPM+
[03:06:18] [PASSED] 13.01 Xe2_HPM
[03:06:18] [PASSED] 20.00 Xe2_LPM
[03:06:18] [PASSED] 30.00 Xe3_LPM
[03:06:18] [PASSED] 30.02 Xe3_LPM
[03:06:18] [PASSED] 35.00 Xe3p_LPM
[03:06:18] [PASSED] 35.03 Xe3p_HPM
[03:06:18] ================= [PASSED] check_media_ip ==================
[03:06:18] =================== check_platform_desc  ===================
[03:06:18] [PASSED] 0x9A60 (TIGERLAKE)
[03:06:18] [PASSED] 0x9A68 (TIGERLAKE)
[03:06:18] [PASSED] 0x9A70 (TIGERLAKE)
[03:06:18] [PASSED] 0x9A40 (TIGERLAKE)
[03:06:18] [PASSED] 0x9A49 (TIGERLAKE)
[03:06:18] [PASSED] 0x9A59 (TIGERLAKE)
[03:06:18] [PASSED] 0x9A78 (TIGERLAKE)
[03:06:18] [PASSED] 0x9AC0 (TIGERLAKE)
[03:06:18] [PASSED] 0x9AC9 (TIGERLAKE)
[03:06:18] [PASSED] 0x9AD9 (TIGERLAKE)
[03:06:18] [PASSED] 0x9AF8 (TIGERLAKE)
[03:06:18] [PASSED] 0x4C80 (ROCKETLAKE)
[03:06:18] [PASSED] 0x4C8A (ROCKETLAKE)
[03:06:18] [PASSED] 0x4C8B (ROCKETLAKE)
[03:06:18] [PASSED] 0x4C8C (ROCKETLAKE)
[03:06:18] [PASSED] 0x4C90 (ROCKETLAKE)
[03:06:18] [PASSED] 0x4C9A (ROCKETLAKE)
[03:06:18] [PASSED] 0x4680 (ALDERLAKE_S)
[03:06:18] [PASSED] 0x4682 (ALDERLAKE_S)
[03:06:18] [PASSED] 0x4688 (ALDERLAKE_S)
[03:06:18] [PASSED] 0x468A (ALDERLAKE_S)
[03:06:18] [PASSED] 0x468B (ALDERLAKE_S)
[03:06:18] [PASSED] 0x4690 (ALDERLAKE_S)
[03:06:18] [PASSED] 0x4692 (ALDERLAKE_S)
[03:06:18] [PASSED] 0x4693 (ALDERLAKE_S)
[03:06:18] [PASSED] 0x46A0 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46A1 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46A2 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46A3 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46A6 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46A8 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46AA (ALDERLAKE_P)
[03:06:18] [PASSED] 0x462A (ALDERLAKE_P)
[03:06:18] [PASSED] 0x4626 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x4628 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46B0 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46B1 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46B2 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46B3 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46C0 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46C1 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46C2 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46C3 (ALDERLAKE_P)
[03:06:18] [PASSED] 0x46D0 (ALDERLAKE_N)
[03:06:18] [PASSED] 0x46D1 (ALDERLAKE_N)
[03:06:18] [PASSED] 0x46D2 (ALDERLAKE_N)
[03:06:18] [PASSED] 0x46D3 (ALDERLAKE_N)
[03:06:18] [PASSED] 0x46D4 (ALDERLAKE_N)
[03:06:18] [PASSED] 0xA721 (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA7A1 (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA7A9 (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA7AC (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA7AD (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA720 (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA7A0 (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA7A8 (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA7AA (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA7AB (ALDERLAKE_P)
[03:06:18] [PASSED] 0xA780 (ALDERLAKE_S)
[03:06:18] [PASSED] 0xA781 (ALDERLAKE_S)
[03:06:18] [PASSED] 0xA782 (ALDERLAKE_S)
[03:06:18] [PASSED] 0xA783 (ALDERLAKE_S)
[03:06:18] [PASSED] 0xA788 (ALDERLAKE_S)
[03:06:18] [PASSED] 0xA789 (ALDERLAKE_S)
[03:06:18] [PASSED] 0xA78A (ALDERLAKE_S)
[03:06:18] [PASSED] 0xA78B (ALDERLAKE_S)
[03:06:18] [PASSED] 0x4905 (DG1)
[03:06:18] [PASSED] 0x4906 (DG1)
[03:06:18] [PASSED] 0x4907 (DG1)
[03:06:18] [PASSED] 0x4908 (DG1)
[03:06:18] [PASSED] 0x4909 (DG1)
[03:06:18] [PASSED] 0x56C0 (DG2)
[03:06:18] [PASSED] 0x56C2 (DG2)
[03:06:18] [PASSED] 0x56C1 (DG2)
[03:06:18] [PASSED] 0x7D51 (METEORLAKE)
[03:06:18] [PASSED] 0x7DD1 (METEORLAKE)
[03:06:18] [PASSED] 0x7D41 (METEORLAKE)
[03:06:18] [PASSED] 0x7D67 (METEORLAKE)
[03:06:18] [PASSED] 0xB640 (METEORLAKE)
[03:06:18] [PASSED] 0x56A0 (DG2)
[03:06:18] [PASSED] 0x56A1 (DG2)
[03:06:18] [PASSED] 0x56A2 (DG2)
[03:06:18] [PASSED] 0x56BE (DG2)
[03:06:18] [PASSED] 0x56BF (DG2)
[03:06:18] [PASSED] 0x5690 (DG2)
[03:06:18] [PASSED] 0x5691 (DG2)
[03:06:18] [PASSED] 0x5692 (DG2)
[03:06:18] [PASSED] 0x56A5 (DG2)
[03:06:18] [PASSED] 0x56A6 (DG2)
[03:06:18] [PASSED] 0x56B0 (DG2)
[03:06:18] [PASSED] 0x56B1 (DG2)
[03:06:18] [PASSED] 0x56BA (DG2)
[03:06:18] [PASSED] 0x56BB (DG2)
[03:06:18] [PASSED] 0x56BC (DG2)
[03:06:18] [PASSED] 0x56BD (DG2)
[03:06:18] [PASSED] 0x5693 (DG2)
[03:06:18] [PASSED] 0x5694 (DG2)
[03:06:18] [PASSED] 0x5695 (DG2)
[03:06:18] [PASSED] 0x56A3 (DG2)
[03:06:18] [PASSED] 0x56A4 (DG2)
[03:06:18] [PASSED] 0x56B2 (DG2)
[03:06:18] [PASSED] 0x56B3 (DG2)
[03:06:18] [PASSED] 0x5696 (DG2)
[03:06:18] [PASSED] 0x5697 (DG2)
[03:06:18] [PASSED] 0xB69 (PVC)
[03:06:18] [PASSED] 0xB6E (PVC)
[03:06:18] [PASSED] 0xBD4 (PVC)
[03:06:18] [PASSED] 0xBD5 (PVC)
[03:06:18] [PASSED] 0xBD6 (PVC)
[03:06:18] [PASSED] 0xBD7 (PVC)
[03:06:18] [PASSED] 0xBD8 (PVC)
[03:06:18] [PASSED] 0xBD9 (PVC)
[03:06:18] [PASSED] 0xBDA (PVC)
[03:06:18] [PASSED] 0xBDB (PVC)
[03:06:18] [PASSED] 0xBE0 (PVC)
[03:06:18] [PASSED] 0xBE1 (PVC)
[03:06:18] [PASSED] 0xBE5 (PVC)
[03:06:18] [PASSED] 0x7D40 (METEORLAKE)
[03:06:18] [PASSED] 0x7D45 (METEORLAKE)
[03:06:18] [PASSED] 0x7D55 (METEORLAKE)
[03:06:18] [PASSED] 0x7D60 (METEORLAKE)
[03:06:18] [PASSED] 0x7DD5 (METEORLAKE)
[03:06:18] [PASSED] 0x6420 (LUNARLAKE)
[03:06:18] [PASSED] 0x64A0 (LUNARLAKE)
[03:06:18] [PASSED] 0x64B0 (LUNARLAKE)
[03:06:18] [PASSED] 0xE202 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE209 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE20B (BATTLEMAGE)
[03:06:18] [PASSED] 0xE20C (BATTLEMAGE)
[03:06:18] [PASSED] 0xE20D (BATTLEMAGE)
[03:06:18] [PASSED] 0xE210 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE211 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE212 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE216 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE220 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE221 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE222 (BATTLEMAGE)
[03:06:18] [PASSED] 0xE223 (BATTLEMAGE)
[03:06:18] [PASSED] 0xB080 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB081 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB082 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB083 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB084 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB085 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB086 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB087 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB08F (PANTHERLAKE)
[03:06:18] [PASSED] 0xB090 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB0A0 (PANTHERLAKE)
[03:06:18] [PASSED] 0xB0B0 (PANTHERLAKE)
[03:06:18] [PASSED] 0xFD80 (PANTHERLAKE)
[03:06:18] [PASSED] 0xFD81 (PANTHERLAKE)
[03:06:18] [PASSED] 0xD740 (NOVALAKE_S)
[03:06:18] [PASSED] 0xD741 (NOVALAKE_S)
[03:06:18] [PASSED] 0xD742 (NOVALAKE_S)
[03:06:18] [PASSED] 0xD743 (NOVALAKE_S)
[03:06:18] [PASSED] 0xD745 (NOVALAKE_S)
[03:06:18] [PASSED] 0xD74A (NOVALAKE_S)
[03:06:18] [PASSED] 0xD74B (NOVALAKE_S)
[03:06:18] [PASSED] 0x674C (CRESCENTISLAND)
[03:06:18] [PASSED] 0x674D (CRESCENTISLAND)
[03:06:18] [PASSED] 0x674E (CRESCENTISLAND)
[03:06:18] [PASSED] 0x674F (CRESCENTISLAND)
[03:06:18] [PASSED] 0x6750 (CRESCENTISLAND)
[03:06:18] [PASSED] 0xD750 (NOVALAKE_P)
[03:06:18] [PASSED] 0xD751 (NOVALAKE_P)
[03:06:18] [PASSED] 0xD752 (NOVALAKE_P)
[03:06:18] [PASSED] 0xD753 (NOVALAKE_P)
[03:06:18] [PASSED] 0xD754 (NOVALAKE_P)
[03:06:18] [PASSED] 0xD755 (NOVALAKE_P)
[03:06:18] [PASSED] 0xD756 (NOVALAKE_P)
[03:06:18] [PASSED] 0xD757 (NOVALAKE_P)
[03:06:18] [PASSED] 0xD75F (NOVALAKE_P)
[03:06:18] =============== [PASSED] check_platform_desc ===============
[03:06:18] ===================== [PASSED] xe_pci ======================
[03:06:18] ============= xe_rtp_tables_test (5 subtests) ==============
[03:06:18] ================== xe_rtp_table_gt_test  ===================
[03:06:18] [PASSED] gt_was/14011060649
[03:06:18] [PASSED] gt_was/14011059788
[03:06:18] [PASSED] gt_was/14015795083
[03:06:18] [PASSED] gt_was/16021867713
[03:06:18] [PASSED] gt_was/14019449301
[03:06:18] [PASSED] gt_was/16028005424
[03:06:18] [PASSED] gt_was/14026578760
[03:06:18] [PASSED] gt_was/1409420604
[03:06:18] [PASSED] gt_was/1408615072
[03:06:18] [PASSED] gt_was/22010523718
[03:06:18] [PASSED] gt_was/14011006942
[03:06:18] [PASSED] gt_was/14014830051
[03:06:18] [PASSED] gt_was/18018781329
[03:06:18] [PASSED] gt_was/1509235366
[03:06:18] [PASSED] gt_was/18018781329
[03:06:18] [PASSED] gt_was/16016694945
[03:06:18] [PASSED] gt_was/14018575942
[03:06:18] [PASSED] gt_was/22016670082
[03:06:18] [PASSED] gt_was/22016670082
[03:06:18] [PASSED] gt_was/14017421178
[03:06:18] [PASSED] gt_was/16025250150
[03:06:18] [PASSED] gt_was/14021871409
[03:06:18] [PASSED] gt_was/16021865536
[03:06:18] [PASSED] gt_was/14021486841
[03:06:18] [PASSED] gt_was/14025160223
[03:06:18] [PASSED] gt_was/14026144927, 16029437861, 14026127056
[03:06:18] [PASSED] gt_was/14025635424
[03:06:18] [PASSED] gt_was/16028005424
[03:06:18] ============== [PASSED] xe_rtp_table_gt_test ===============
[03:06:18] ================== xe_rtp_table_gt_test  ===================
[03:06:18] [PASSED] gt_tunings/Tuning: Blend Fill Caching Optimization Disable
[03:06:18] [PASSED] gt_tunings/Tuning: 32B Access Enable
[03:06:18] [PASSED] gt_tunings/Tuning: L3 cache
[03:06:18] [PASSED] gt_tunings/Tuning: L3 cache - media
[03:06:18] [PASSED] gt_tunings/Tuning: Compression Overfetch
[03:06:18] [PASSED] gt_tunings/Tuning: Compression Overfetch - media
[03:06:18] [PASSED] gt_tunings/Tuning: Enable compressible partial write overfetch in L3
[03:06:18] [PASSED] gt_tunings/Tuning: Enable compressible partial write overfetch in L3 - media
[03:06:18] [PASSED] gt_tunings/Tuning: L2 Overfetch Compressible Only
[03:06:18] [PASSED] gt_tunings/Tuning: L2 Overfetch Compressible Only - media
[03:06:18] [PASSED] gt_tunings/Tuning: Stateless compression control
[03:06:18] [PASSED] gt_tunings/Tuning: Stateless compression control - media
[03:06:18] [PASSED] gt_tunings/Tuning: L3 RW flush all Cache
[03:06:18] [PASSED] gt_tunings/Tuning: L3 RW flush all cache - media
[03:06:18] [PASSED] gt_tunings/Tuning: Set STLB Bank Hash Mode to 4KB
[03:06:18] ============== [PASSED] xe_rtp_table_gt_test ===============
[03:06:18] ================== xe_rtp_table_oob_test  ==================
[03:06:18] [PASSED] oob_was/1607983814
[03:06:18] [PASSED] oob_was/16010904313
[03:06:18] [PASSED] oob_was/18022495364
[03:06:18] [PASSED] oob_was/22012773006
[03:06:18] [PASSED] oob_was/14014475959
[03:06:18] [PASSED] oob_was/22011391025
[03:06:18] [PASSED] oob_was/22012727170
[03:06:18] [PASSED] oob_was/22012727685
[03:06:18] [PASSED] oob_was/22016596838
[03:06:18] [PASSED] oob_was/18020744125
[03:06:18] [PASSED] oob_was/1409600907
[03:06:18] [PASSED] oob_was/22014953428
[03:06:18] [PASSED] oob_was/16017236439
[03:06:18] [PASSED] oob_was/14019821291
[03:06:18] [PASSED] oob_was/14015076503
[03:06:18] [PASSED] oob_was/14018913170
[03:06:18] [PASSED] oob_was/14018094691
[03:06:18] [PASSED] oob_was/18024947630
[03:06:18] [PASSED] oob_was/16022287689
[03:06:18] [PASSED] oob_was/13011645652
[03:06:18] [PASSED] oob_was/14022293748
[03:06:18] [PASSED] oob_was/22019794406
[03:06:18] [PASSED] oob_was/22019338487
[03:06:18] [PASSED] oob_was/16023588340
[03:06:18] [PASSED] oob_was/14019789679
[03:06:18] [PASSED] oob_was/14022866841
[03:06:18] [PASSED] oob_was/16021333562
[03:06:18] [PASSED] oob_was/14016712196
[03:06:18] [PASSED] oob_was/14015568240
[03:06:18] [PASSED] oob_was/18013179988
[03:06:18] [PASSED] oob_was/1508761755
[03:06:18] [PASSED] oob_was/16023105232
[03:06:18] [PASSED] oob_was/16026508708
[03:06:18] [PASSED] oob_was/14020001231
[03:06:18] [PASSED] oob_was/16023683509
[03:06:18] [PASSED] oob_was/14025515070
[03:06:18] [PASSED] oob_was/15015404425_disable
[03:06:18] [PASSED] oob_was/16026007364
[03:06:18] [PASSED] oob_was/14020316580
[03:06:18] [PASSED] oob_was/14025883347
[03:06:18] [PASSED] oob_was/16029380221
[03:06:18] [PASSED] oob_was/22022079272
[03:06:18] [PASSED] oob_was/16029897822
[03:06:18] ============== [PASSED] xe_rtp_table_oob_test ==============
[03:06:18] ================ xe_rtp_table_dev_oob_test  ================
[03:06:18] [PASSED] device_oob_was/22010954014
[03:06:18] [PASSED] device_oob_was/15015404425
[03:06:18] [PASSED] device_oob_was/22019338487_display
[03:06:18] [PASSED] device_oob_was/14022085890
[03:06:18] [PASSED] device_oob_was/14026539277
[03:06:18] [PASSED] device_oob_was/14026633728
[03:06:18] [PASSED] device_oob_was/14026746987
[03:06:18] [PASSED] device_oob_was/14026779378
[03:06:18] ============ [PASSED] xe_rtp_table_dev_oob_test ============
[03:06:18] ========== xe_rtp_table_missing_upper_bound_test  ==========
[03:06:18] [PASSED] register_whitelist/WaAllowPMDepthAndInvocationCountAccessFromUMD, 1408556865
[03:06:18] [PASSED] register_whitelist/1508744258, 14012131227, 1808121037
[03:06:18] [PASSED] register_whitelist/1806527549
[03:06:18] [PASSED] register_whitelist/allow_read_ctx_timestamp
[03:06:18] [PASSED] register_whitelist/allow_read_queue_timestamp
[03:06:18] [PASSED] register_whitelist/16014440446
[03:06:18] [PASSED] register_whitelist/16017236439
[03:06:18] [PASSED] register_whitelist/16020183090
[03:06:18] [PASSED] register_whitelist/14024997852
[03:06:18] [PASSED] register_whitelist/14024997852
[03:06:18] ====== [PASSED] xe_rtp_table_missing_upper_bound_test ======
[03:06:18] =============== [PASSED] xe_rtp_tables_test ================
[03:06:18] =================== xe_rtp (3 subtests) ====================
[03:06:18] =================== xe_rtp_rules_tests  ====================
[03:06:18] [PASSED] no
[03:06:18] [PASSED] yes
[03:06:18] [PASSED] no-and-no
[03:06:18] [PASSED] no-and-yes
[03:06:18] [PASSED] yes-and-no
[03:06:18] [PASSED] yes-and-yes
[03:06:18] [PASSED] no-or-no
[03:06:18] [PASSED] no-or-yes
[03:06:18] [PASSED] yes-or-no
[03:06:18] [PASSED] yes-or-yes
[03:06:18] [PASSED] no-yes-or-yes-no
[03:06:18] [PASSED] no-yes-or-yes-yes
[03:06:18] [PASSED] yes-yes-or-no-yes
[03:06:18] [PASSED] yes-yes-or-yes-yes
[03:06:18] [PASSED] no-no-or-yes-or-no
[03:06:18] [PASSED] or
[03:06:18] [PASSED] or-yes
[03:06:18] [PASSED] or-no
[03:06:18] [PASSED] yes-or
[03:06:18] [PASSED] no-or
[03:06:18] [PASSED] no-or-or-yes
[03:06:18] [PASSED] yes-or-or-no
[03:06:18] [PASSED] no-or-or-no
[03:06:18] [PASSED] missing-context-engine-class
[03:06:18] [PASSED] missing-context-engine-class-or-yes
[03:06:18] [PASSED] missing-context-engine-class-or-or-yes
[03:06:18] =============== [PASSED] xe_rtp_rules_tests ================
[03:06:18] =============== xe_rtp_process_to_sr_tests  ================
[03:06:18] [PASSED] coalesce-same-reg
[03:06:18] [PASSED] coalesce-same-reg-literal-and-func
[03:06:18] [PASSED] no-match-no-add
[03:06:18] [PASSED] two-regs-two-entries
[03:06:18] [PASSED] clr-one-set-other
[03:06:18] [PASSED] set-field
[03:06:18] [PASSED] conflict-duplicate
[03:06:18] [PASSED] conflict-not-disjoint
[03:06:18] [PASSED] conflict-not-disjoint-literal-and-func
[03:06:18] [PASSED] conflict-reg-type
[03:06:18] [PASSED] bad-mcr-reg-forced-to-regular
[03:06:18] [PASSED] bad-regular-reg-forced-to-mcr
[03:06:18] =========== [PASSED] xe_rtp_process_to_sr_tests ============
[03:06:18] ================== xe_rtp_process_tests  ===================
[03:06:18] [PASSED] active1
[03:06:18] [PASSED] active2
[03:06:18] [PASSED] active-inactive
[03:06:18] [PASSED] inactive-active
[03:06:18] [PASSED] inactive-active-inactive
[03:06:18] [PASSED] inactive-inactive-inactive
[03:06:18] ============== [PASSED] xe_rtp_process_tests ===============
[03:06:18] ===================== [PASSED] xe_rtp ======================
[03:06:18] ==================== xe_wa (1 subtest) =====================
[03:06:18] ======================== xe_wa_gt  =========================
[03:06:18] [PASSED] TIGERLAKE B0
[03:06:18] [PASSED] DG1 A0
[03:06:18] [PASSED] DG1 B0
[03:06:18] [PASSED] ALDERLAKE_S A0
[03:06:18] [PASSED] ALDERLAKE_S B0
[03:06:18] [PASSED] ALDERLAKE_S C0
[03:06:18] [PASSED] ALDERLAKE_S D0
[03:06:18] [PASSED] ALDERLAKE_P A0
[03:06:18] [PASSED] ALDERLAKE_P B0
[03:06:18] [PASSED] ALDERLAKE_P C0
[03:06:18] [PASSED] ALDERLAKE_S RPLS D0
[03:06:18] [PASSED] ALDERLAKE_P RPLU E0
[03:06:18] [PASSED] DG2 G10 C0
[03:06:18] [PASSED] DG2 G11 B1
[03:06:18] [PASSED] DG2 G12 A1
[03:06:18] [PASSED] METEORLAKE 12.70(Xe_LPG) A0 13.00(Xe_LPM+) A0
[03:06:18] [PASSED] METEORLAKE 12.71(Xe_LPG) A0 13.00(Xe_LPM+) A0
[03:06:18] [PASSED] METEORLAKE 12.74(Xe_LPG+) A0 13.00(Xe_LPM+) A0
[03:06:18] [PASSED] LUNARLAKE 20.04(Xe2_LPG) A0 20.00(Xe2_LPM) A0
[03:06:18] [PASSED] LUNARLAKE 20.04(Xe2_LPG) B0 20.00(Xe2_LPM) A0
[03:06:18] [PASSED] BATTLEMAGE 20.01(Xe2_HPG) A0 13.01(Xe2_HPM) A1
[03:06:18] [PASSED] PANTHERLAKE 30.00(Xe3_LPG) A0 30.00(Xe3_LPM) A0
[03:06:18] ==================== [PASSED] xe_wa_gt =====================
[03:06:18] ====================== [PASSED] xe_wa ======================
[03:06:18] ============================================================
[03:06:18] Testing complete. Ran 731 tests: passed: 713, skipped: 18
[03:06:18] Elapsed time: 36.813s total, 4.378s configuring, 31.769s building, 0.641s running

+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/tests/.kunitconfig [03:06:19] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[03:06:20] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=48
[03:06:45] Starting KUnit Kernel (1/1)...
[03:06:45] ============================================================
Running tests with:
$ .kunit/linux kunit.enable=1 mem=1G console=tty kunit_shutdown=halt
[03:06:45] ============ drm_test_pick_cmdline (2 subtests) ============
[03:06:45] [PASSED] drm_test_pick_cmdline_res_1920_1080_60
[03:06:45] =============== drm_test_pick_cmdline_named  ===============
[03:06:45] [PASSED] NTSC
[03:06:45] [PASSED] NTSC-J
[03:06:45] [PASSED] PAL
[03:06:45] [PASSED] PAL-M
[03:06:45] =========== [PASSED] drm_test_pick_cmdline_named ===========
[03:06:45] ============== [PASSED] drm_test_pick_cmdline ==============
[03:06:45] == drm_test_atomic_get_connector_for_encoder (1 subtest) ===
[03:06:45] [PASSED] drm_test_drm_atomic_get_connector_for_encoder
[03:06:45] ==== [PASSED] drm_test_atomic_get_connector_for_encoder ====
[03:06:45] =========== drm_validate_clone_mode (2 subtests) ===========
[03:06:45] ============== drm_test_check_in_clone_mode  ===============
[03:06:45] [PASSED] in_clone_mode
[03:06:45] [PASSED] not_in_clone_mode
[03:06:45] ========== [PASSED] drm_test_check_in_clone_mode ===========
[03:06:45] =============== drm_test_check_valid_clones  ===============
[03:06:45] [PASSED] not_in_clone_mode
[03:06:45] [PASSED] valid_clone
[03:06:45] [PASSED] invalid_clone
[03:06:45] =========== [PASSED] drm_test_check_valid_clones ===========
[03:06:45] ============= [PASSED] drm_validate_clone_mode =============
[03:06:45] ============= drm_validate_modeset (1 subtest) =============
[03:06:45] [PASSED] drm_test_check_connector_changed_modeset
[03:06:45] ============== [PASSED] drm_validate_modeset ===============
[03:06:45] ====== drm_test_bridge_get_current_state (2 subtests) ======
[03:06:45] [PASSED] drm_test_drm_bridge_get_current_state_atomic
[03:06:45] [PASSED] drm_test_drm_bridge_get_current_state_legacy
[03:06:45] ======== [PASSED] drm_test_bridge_get_current_state ========
[03:06:45] ====== drm_test_bridge_helper_reset_crtc (4 subtests) ======
[03:06:45] [PASSED] drm_test_drm_bridge_helper_reset_crtc_atomic
[03:06:45] [PASSED] drm_test_drm_bridge_helper_reset_crtc_atomic_disabled
[03:06:45] [PASSED] drm_test_drm_bridge_helper_reset_crtc_legacy
[03:06:45] [PASSED] drm_test_drm_bridge_helper_hdmi_output_bus_fmts
[03:06:45] ======== [PASSED] drm_test_bridge_helper_reset_crtc ========
[03:06:45] ============== drm_bridge_alloc (2 subtests) ===============
[03:06:45] [PASSED] drm_test_drm_bridge_alloc_basic
[03:06:45] [PASSED] drm_test_drm_bridge_alloc_get_put
[03:06:45] ================ [PASSED] drm_bridge_alloc =================
[03:06:45] ============= drm_bridge_bus_fmt (5 subtests) ==============
[03:06:45] [PASSED] drm_test_bridge_rgb_yuv_rgb
[03:06:45] [PASSED] drm_test_bridge_must_convert_to_yuv444
[03:06:45] [PASSED] drm_test_bridge_hdmi_auto_rgb
[03:06:45] [PASSED] drm_test_bridge_auto_first
[03:06:45] [PASSED] drm_test_bridge_rgb_yuv_no_path
[03:06:45] =============== [PASSED] drm_bridge_bus_fmt ================
[03:06:45] ============= drm_cmdline_parser (40 subtests) =============
[03:06:45] [PASSED] drm_test_cmdline_force_d_only
[03:06:45] [PASSED] drm_test_cmdline_force_D_only_dvi
[03:06:45] [PASSED] drm_test_cmdline_force_D_only_hdmi
[03:06:45] [PASSED] drm_test_cmdline_force_D_only_not_digital
[03:06:45] [PASSED] drm_test_cmdline_force_e_only
[03:06:45] [PASSED] drm_test_cmdline_res
[03:06:45] [PASSED] drm_test_cmdline_res_vesa
[03:06:45] [PASSED] drm_test_cmdline_res_vesa_rblank
[03:06:45] [PASSED] drm_test_cmdline_res_rblank
[03:06:45] [PASSED] drm_test_cmdline_res_bpp
[03:06:45] [PASSED] drm_test_cmdline_res_refresh
[03:06:45] [PASSED] drm_test_cmdline_res_bpp_refresh
[03:06:45] [PASSED] drm_test_cmdline_res_bpp_refresh_interlaced
[03:06:45] [PASSED] drm_test_cmdline_res_bpp_refresh_margins
[03:06:45] [PASSED] drm_test_cmdline_res_bpp_refresh_force_off
[03:06:45] [PASSED] drm_test_cmdline_res_bpp_refresh_force_on
[03:06:45] [PASSED] drm_test_cmdline_res_bpp_refresh_force_on_analog
[03:06:45] [PASSED] drm_test_cmdline_res_bpp_refresh_force_on_digital
[03:06:45] [PASSED] drm_test_cmdline_res_bpp_refresh_interlaced_margins_force_on
[03:06:45] [PASSED] drm_test_cmdline_res_margins_force_on
[03:06:45] [PASSED] drm_test_cmdline_res_vesa_margins
[03:06:45] [PASSED] drm_test_cmdline_name
[03:06:45] [PASSED] drm_test_cmdline_name_bpp
[03:06:45] [PASSED] drm_test_cmdline_name_option
[03:06:45] [PASSED] drm_test_cmdline_name_bpp_option
[03:06:45] [PASSED] drm_test_cmdline_rotate_0
[03:06:45] [PASSED] drm_test_cmdline_rotate_90
[03:06:45] [PASSED] drm_test_cmdline_rotate_180
[03:06:45] [PASSED] drm_test_cmdline_rotate_270
[03:06:45] [PASSED] drm_test_cmdline_hmirror
[03:06:45] [PASSED] drm_test_cmdline_vmirror
[03:06:45] [PASSED] drm_test_cmdline_margin_options
[03:06:45] [PASSED] drm_test_cmdline_multiple_options
[03:06:45] [PASSED] drm_test_cmdline_bpp_extra_and_option
[03:06:45] [PASSED] drm_test_cmdline_extra_and_option
[03:06:45] [PASSED] drm_test_cmdline_freestanding_options
[03:06:45] [PASSED] drm_test_cmdline_freestanding_force_e_and_options
[03:06:45] [PASSED] drm_test_cmdline_panel_orientation
[03:06:45] ================ drm_test_cmdline_invalid  =================
[03:06:45] [PASSED] margin_only
[03:06:45] [PASSED] interlace_only
[03:06:45] [PASSED] res_missing_x
[03:06:45] [PASSED] res_missing_y
[03:06:45] [PASSED] res_bad_y
[03:06:45] [PASSED] res_missing_y_bpp
[03:06:45] [PASSED] res_bad_bpp
[03:06:45] [PASSED] res_bad_refresh
[03:06:45] [PASSED] res_bpp_refresh_force_on_off
[03:06:45] [PASSED] res_invalid_mode
[03:06:45] [PASSED] res_bpp_wrong_place_mode
[03:06:45] [PASSED] name_bpp_refresh
[03:06:45] [PASSED] name_refresh
[03:06:45] [PASSED] name_refresh_wrong_mode
[03:06:45] [PASSED] name_refresh_invalid_mode
[03:06:45] [PASSED] rotate_multiple
[03:06:45] [PASSED] rotate_invalid_val
[03:06:45] [PASSED] rotate_truncated
[03:06:45] [PASSED] invalid_option
[03:06:45] [PASSED] invalid_tv_option
[03:06:45] [PASSED] truncated_tv_option
[03:06:45] ============ [PASSED] drm_test_cmdline_invalid =============
[03:06:45] =============== drm_test_cmdline_tv_options  ===============
[03:06:45] [PASSED] NTSC
[03:06:45] [PASSED] NTSC_443
[03:06:45] [PASSED] NTSC_J
[03:06:45] [PASSED] PAL
[03:06:45] [PASSED] PAL_M
[03:06:45] [PASSED] PAL_N
[03:06:45] [PASSED] SECAM
[03:06:45] [PASSED] MONO_525
[03:06:45] [PASSED] MONO_625
[03:06:45] =========== [PASSED] drm_test_cmdline_tv_options ===========
[03:06:45] =============== [PASSED] drm_cmdline_parser ================
[03:06:45] ========== drmm_connector_hdmi_init (20 subtests) ==========
[03:06:45] [PASSED] drm_test_connector_hdmi_init_valid
[03:06:45] [PASSED] drm_test_connector_hdmi_init_bpc_8
[03:06:45] [PASSED] drm_test_connector_hdmi_init_bpc_10
[03:06:45] [PASSED] drm_test_connector_hdmi_init_bpc_12
[03:06:45] [PASSED] drm_test_connector_hdmi_init_bpc_invalid
[03:06:45] [PASSED] drm_test_connector_hdmi_init_bpc_null
[03:06:45] [PASSED] drm_test_connector_hdmi_init_formats_empty
[03:06:45] [PASSED] drm_test_connector_hdmi_init_formats_no_rgb
[03:06:45] === drm_test_connector_hdmi_init_formats_yuv420_allowed  ===
[03:06:45] [PASSED] supported_formats=0x9 yuv420_allowed=1
[03:06:45] [PASSED] supported_formats=0x9 yuv420_allowed=0
[03:06:45] [PASSED] supported_formats=0x5 yuv420_allowed=1
[03:06:45] [PASSED] supported_formats=0x5 yuv420_allowed=0
[03:06:45] === [PASSED] drm_test_connector_hdmi_init_formats_yuv420_allowed ===
[03:06:45] [PASSED] drm_test_connector_hdmi_init_null_ddc
[03:06:45] [PASSED] drm_test_connector_hdmi_init_null_product
[03:06:45] [PASSED] drm_test_connector_hdmi_init_null_vendor
[03:06:45] [PASSED] drm_test_connector_hdmi_init_product_length_exact
[03:06:45] [PASSED] drm_test_connector_hdmi_init_product_length_too_long
[03:06:45] [PASSED] drm_test_connector_hdmi_init_product_valid
[03:06:45] [PASSED] drm_test_connector_hdmi_init_vendor_length_exact
[03:06:45] [PASSED] drm_test_connector_hdmi_init_vendor_length_too_long
[03:06:45] [PASSED] drm_test_connector_hdmi_init_vendor_valid
[03:06:45] ========= drm_test_connector_hdmi_init_type_valid  =========
[03:06:45] [PASSED] HDMI-A
[03:06:45] [PASSED] HDMI-B
[03:06:45] ===== [PASSED] drm_test_connector_hdmi_init_type_valid =====
[03:06:45] ======== drm_test_connector_hdmi_init_type_invalid  ========
[03:06:45] [PASSED] Unknown
[03:06:45] [PASSED] VGA
[03:06:45] [PASSED] DVI-I
[03:06:45] [PASSED] DVI-D
[03:06:45] [PASSED] DVI-A
[03:06:45] [PASSED] Composite
[03:06:45] [PASSED] SVIDEO
[03:06:45] [PASSED] LVDS
[03:06:45] [PASSED] Component
[03:06:45] [PASSED] DIN
[03:06:45] [PASSED] DP
[03:06:45] [PASSED] TV
[03:06:45] [PASSED] eDP
[03:06:45] [PASSED] Virtual
[03:06:45] [PASSED] DSI
[03:06:45] [PASSED] DPI
[03:06:45] [PASSED] Writeback
[03:06:45] [PASSED] SPI
[03:06:45] [PASSED] USB
[03:06:45] ==== [PASSED] drm_test_connector_hdmi_init_type_invalid ====
[03:06:45] ============ [PASSED] drmm_connector_hdmi_init =============
[03:06:45] ============= drmm_connector_init (3 subtests) =============
[03:06:45] [PASSED] drm_test_drmm_connector_init
[03:06:45] [PASSED] drm_test_drmm_connector_init_null_ddc
[03:06:45] ========= drm_test_drmm_connector_init_type_valid  =========
[03:06:45] [PASSED] Unknown
[03:06:45] [PASSED] VGA
[03:06:45] [PASSED] DVI-I
[03:06:45] [PASSED] DVI-D
[03:06:45] [PASSED] DVI-A
[03:06:45] [PASSED] Composite
[03:06:45] [PASSED] SVIDEO
[03:06:45] [PASSED] LVDS
[03:06:45] [PASSED] Component
[03:06:45] [PASSED] DIN
[03:06:45] [PASSED] DP
[03:06:45] [PASSED] HDMI-A
[03:06:45] [PASSED] HDMI-B
[03:06:45] [PASSED] TV
[03:06:45] [PASSED] eDP
[03:06:45] [PASSED] Virtual
[03:06:45] [PASSED] DSI
[03:06:45] [PASSED] DPI
[03:06:45] [PASSED] Writeback
[03:06:45] [PASSED] SPI
[03:06:45] [PASSED] USB
[03:06:45] ===== [PASSED] drm_test_drmm_connector_init_type_valid =====
[03:06:45] =============== [PASSED] drmm_connector_init ===============
[03:06:45] ========= drm_connector_dynamic_init (6 subtests) ==========
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_init
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_init_null_ddc
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_init_not_added
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_init_properties
[03:06:45] ===== drm_test_drm_connector_dynamic_init_type_valid  ======
[03:06:45] [PASSED] Unknown
[03:06:45] [PASSED] VGA
[03:06:45] [PASSED] DVI-I
[03:06:45] [PASSED] DVI-D
[03:06:45] [PASSED] DVI-A
[03:06:45] [PASSED] Composite
[03:06:45] [PASSED] SVIDEO
[03:06:45] [PASSED] LVDS
[03:06:45] [PASSED] Component
[03:06:45] [PASSED] DIN
[03:06:45] [PASSED] DP
[03:06:45] [PASSED] HDMI-A
[03:06:45] [PASSED] HDMI-B
[03:06:45] [PASSED] TV
[03:06:45] [PASSED] eDP
[03:06:45] [PASSED] Virtual
[03:06:45] [PASSED] DSI
[03:06:45] [PASSED] DPI
[03:06:45] [PASSED] Writeback
[03:06:45] [PASSED] SPI
[03:06:45] [PASSED] USB
[03:06:45] = [PASSED] drm_test_drm_connector_dynamic_init_type_valid ==
[03:06:45] ======== drm_test_drm_connector_dynamic_init_name  =========
[03:06:45] [PASSED] Unknown
[03:06:45] [PASSED] VGA
[03:06:45] [PASSED] DVI-I
[03:06:45] [PASSED] DVI-D
[03:06:45] [PASSED] DVI-A
[03:06:45] [PASSED] Composite
[03:06:45] [PASSED] SVIDEO
[03:06:45] [PASSED] LVDS
[03:06:45] [PASSED] Component
[03:06:45] [PASSED] DIN
[03:06:45] [PASSED] DP
[03:06:45] [PASSED] HDMI-A
[03:06:45] [PASSED] HDMI-B
[03:06:45] [PASSED] TV
[03:06:45] [PASSED] eDP
[03:06:45] [PASSED] Virtual
[03:06:45] [PASSED] DSI
[03:06:45] [PASSED] DPI
[03:06:45] [PASSED] Writeback
[03:06:45] [PASSED] SPI
[03:06:45] [PASSED] USB
[03:06:45] ==== [PASSED] drm_test_drm_connector_dynamic_init_name =====
[03:06:45] =========== [PASSED] drm_connector_dynamic_init ============
[03:06:45] ==== drm_connector_dynamic_register_early (4 subtests) =====
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_early_on_list
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_early_defer
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_early_no_init
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_early_no_mode_object
[03:06:45] ====== [PASSED] drm_connector_dynamic_register_early =======
[03:06:45] ======= drm_connector_dynamic_register (7 subtests) ========
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_on_list
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_no_defer
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_no_init
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_mode_object
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_sysfs
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_sysfs_name
[03:06:45] [PASSED] drm_test_drm_connector_dynamic_register_debugfs
[03:06:45] ========= [PASSED] drm_connector_dynamic_register ==========
[03:06:45] = drm_connector_attach_broadcast_rgb_property (2 subtests) =
[03:06:45] [PASSED] drm_test_drm_connector_attach_broadcast_rgb_property
[03:06:45] [PASSED] drm_test_drm_connector_attach_broadcast_rgb_property_hdmi_connector
[03:06:45] === [PASSED] drm_connector_attach_broadcast_rgb_property ===
[03:06:45] ========== drm_get_tv_mode_from_name (2 subtests) ==========
[03:06:45] ========== drm_test_get_tv_mode_from_name_valid  ===========
[03:06:45] [PASSED] NTSC
[03:06:45] [PASSED] NTSC-443
[03:06:45] [PASSED] NTSC-J
[03:06:45] [PASSED] PAL
[03:06:45] [PASSED] PAL-M
[03:06:45] [PASSED] PAL-N
[03:06:45] [PASSED] SECAM
[03:06:45] [PASSED] Mono
[03:06:45] ====== [PASSED] drm_test_get_tv_mode_from_name_valid =======
[03:06:45] [PASSED] drm_test_get_tv_mode_from_name_truncated
[03:06:45] ============ [PASSED] drm_get_tv_mode_from_name ============
[03:06:45] = drm_test_connector_hdmi_compute_mode_clock (12 subtests) =
[03:06:45] [PASSED] drm_test_drm_hdmi_compute_mode_clock_rgb
[03:06:45] [PASSED] drm_test_drm_hdmi_compute_mode_clock_rgb_10bpc
[03:06:45] [PASSED] drm_test_drm_hdmi_compute_mode_clock_rgb_10bpc_vic_1
[03:06:45] [PASSED] drm_test_drm_hdmi_compute_mode_clock_rgb_12bpc
[03:06:45] [PASSED] drm_test_drm_hdmi_compute_mode_clock_rgb_12bpc_vic_1
[03:06:45] [PASSED] drm_test_drm_hdmi_compute_mode_clock_rgb_double
[03:06:45] = drm_test_connector_hdmi_compute_mode_clock_yuv420_valid  =
[03:06:45] [PASSED] VIC 96
[03:06:45] [PASSED] VIC 97
[03:06:45] [PASSED] VIC 101
[03:06:45] [PASSED] VIC 102
[03:06:45] [PASSED] VIC 106
[03:06:45] [PASSED] VIC 107
[03:06:45] === [PASSED] drm_test_connector_hdmi_compute_mode_clock_yuv420_valid ===
[03:06:45] [PASSED] drm_test_connector_hdmi_compute_mode_clock_yuv420_10_bpc
[03:06:45] [PASSED] drm_test_connector_hdmi_compute_mode_clock_yuv420_12_bpc
[03:06:45] [PASSED] drm_test_connector_hdmi_compute_mode_clock_yuv422_8_bpc
[03:06:45] [PASSED] drm_test_connector_hdmi_compute_mode_clock_yuv422_10_bpc
[03:06:45] [PASSED] drm_test_connector_hdmi_compute_mode_clock_yuv422_12_bpc
[03:06:45] === [PASSED] drm_test_connector_hdmi_compute_mode_clock ====
[03:06:45] == drm_hdmi_connector_get_broadcast_rgb_name (2 subtests) ==
[03:06:45] === drm_test_drm_hdmi_connector_get_broadcast_rgb_name  ====
[03:06:45] [PASSED] Automatic
[03:06:45] [PASSED] Full
[03:06:45] [PASSED] Limited 16:235
[03:06:45] === [PASSED] drm_test_drm_hdmi_connector_get_broadcast_rgb_name ===
[03:06:45] [PASSED] drm_test_drm_hdmi_connector_get_broadcast_rgb_name_invalid
[03:06:45] ==== [PASSED] drm_hdmi_connector_get_broadcast_rgb_name ====
[03:06:45] == drm_hdmi_connector_get_output_format_name (2 subtests) ==
[03:06:45] === drm_test_drm_hdmi_connector_get_output_format_name  ====
[03:06:45] [PASSED] RGB
[03:06:45] [PASSED] YUV 4:2:0
[03:06:45] [PASSED] YUV 4:2:2
[03:06:45] [PASSED] YUV 4:4:4
[03:06:45] === [PASSED] drm_test_drm_hdmi_connector_get_output_format_name ===
[03:06:45] [PASSED] drm_test_drm_hdmi_connector_get_output_format_name_invalid
[03:06:45] ==== [PASSED] drm_hdmi_connector_get_output_format_name ====
[03:06:45] ============= drm_damage_helper (21 subtests) ==============
[03:06:45] [PASSED] drm_test_damage_iter_no_damage
[03:06:45] [PASSED] drm_test_damage_iter_no_damage_fractional_src
[03:06:45] [PASSED] drm_test_damage_iter_no_damage_src_moved
[03:06:45] [PASSED] drm_test_damage_iter_no_damage_fractional_src_moved
[03:06:45] [PASSED] drm_test_damage_iter_no_damage_not_visible
[03:06:45] [PASSED] drm_test_damage_iter_no_damage_no_crtc
[03:06:45] [PASSED] drm_test_damage_iter_no_damage_no_fb
[03:06:45] [PASSED] drm_test_damage_iter_simple_damage
[03:06:45] [PASSED] drm_test_damage_iter_single_damage
[03:06:45] [PASSED] drm_test_damage_iter_single_damage_intersect_src
[03:06:45] [PASSED] drm_test_damage_iter_single_damage_outside_src
[03:06:45] [PASSED] drm_test_damage_iter_single_damage_fractional_src
[03:06:45] [PASSED] drm_test_damage_iter_single_damage_intersect_fractional_src
[03:06:45] [PASSED] drm_test_damage_iter_single_damage_outside_fractional_src
[03:06:45] [PASSED] drm_test_damage_iter_single_damage_src_moved
[03:06:45] [PASSED] drm_test_damage_iter_single_damage_fractional_src_moved
[03:06:45] [PASSED] drm_test_damage_iter_damage
[03:06:45] [PASSED] drm_test_damage_iter_damage_one_intersect
[03:06:45] [PASSED] drm_test_damage_iter_damage_one_outside
[03:06:45] [PASSED] drm_test_damage_iter_damage_src_moved
[03:06:45] [PASSED] drm_test_damage_iter_damage_not_visible
[03:06:45] ================ [PASSED] drm_damage_helper ================
[03:06:45] ============== drm_dp_mst_helper (3 subtests) ==============
[03:06:45] ============== drm_test_dp_mst_calc_pbn_mode  ==============
[03:06:45] [PASSED] Clock 154000 BPP 30 DSC disabled
[03:06:45] [PASSED] Clock 234000 BPP 30 DSC disabled
[03:06:45] [PASSED] Clock 297000 BPP 24 DSC disabled
[03:06:45] [PASSED] Clock 332880 BPP 24 DSC enabled
[03:06:45] [PASSED] Clock 324540 BPP 24 DSC enabled
[03:06:45] ========== [PASSED] drm_test_dp_mst_calc_pbn_mode ==========
[03:06:45] ============== drm_test_dp_mst_calc_pbn_div  ===============
[03:06:45] [PASSED] Link rate 2000000 lane count 4
[03:06:45] [PASSED] Link rate 2000000 lane count 2
[03:06:45] [PASSED] Link rate 2000000 lane count 1
[03:06:45] [PASSED] Link rate 1350000 lane count 4
[03:06:45] [PASSED] Link rate 1350000 lane count 2
[03:06:45] [PASSED] Link rate 1350000 lane count 1
[03:06:45] [PASSED] Link rate 1000000 lane count 4
[03:06:45] [PASSED] Link rate 1000000 lane count 2
[03:06:45] [PASSED] Link rate 1000000 lane count 1
[03:06:45] [PASSED] Link rate 810000 lane count 4
[03:06:45] [PASSED] Link rate 810000 lane count 2
[03:06:45] [PASSED] Link rate 810000 lane count 1
[03:06:45] [PASSED] Link rate 540000 lane count 4
[03:06:45] [PASSED] Link rate 540000 lane count 2
[03:06:45] [PASSED] Link rate 540000 lane count 1
[03:06:45] [PASSED] Link rate 270000 lane count 4
[03:06:45] [PASSED] Link rate 270000 lane count 2
[03:06:45] [PASSED] Link rate 270000 lane count 1
[03:06:45] [PASSED] Link rate 162000 lane count 4
[03:06:45] [PASSED] Link rate 162000 lane count 2
[03:06:45] [PASSED] Link rate 162000 lane count 1
[03:06:45] ========== [PASSED] drm_test_dp_mst_calc_pbn_div ===========
[03:06:45] ========= drm_test_dp_mst_sideband_msg_req_decode  =========
[03:06:45] [PASSED] DP_ENUM_PATH_RESOURCES with port number
[03:06:45] [PASSED] DP_POWER_UP_PHY with port number
[03:06:45] [PASSED] DP_POWER_DOWN_PHY with port number
[03:06:45] [PASSED] DP_ALLOCATE_PAYLOAD with SDP stream sinks
[03:06:45] [PASSED] DP_ALLOCATE_PAYLOAD with port number
[03:06:45] [PASSED] DP_ALLOCATE_PAYLOAD with VCPI
[03:06:45] [PASSED] DP_ALLOCATE_PAYLOAD with PBN
[03:06:45] [PASSED] DP_QUERY_PAYLOAD with port number
[03:06:45] [PASSED] DP_QUERY_PAYLOAD with VCPI
[03:06:45] [PASSED] DP_REMOTE_DPCD_READ with port number
[03:06:45] [PASSED] DP_REMOTE_DPCD_READ with DPCD address
[03:06:45] [PASSED] DP_REMOTE_DPCD_READ with max number of bytes
[03:06:45] [PASSED] DP_REMOTE_DPCD_WRITE with port number
[03:06:45] [PASSED] DP_REMOTE_DPCD_WRITE with DPCD address
[03:06:45] [PASSED] DP_REMOTE_DPCD_WRITE with data array
[03:06:45] [PASSED] DP_REMOTE_I2C_READ with port number
[03:06:45] [PASSED] DP_REMOTE_I2C_READ with I2C device ID
[03:06:45] [PASSED] DP_REMOTE_I2C_READ with transactions array
[03:06:45] [PASSED] DP_REMOTE_I2C_WRITE with port number
[03:06:45] [PASSED] DP_REMOTE_I2C_WRITE with I2C device ID
[03:06:45] [PASSED] DP_REMOTE_I2C_WRITE with data array
[03:06:45] [PASSED] DP_QUERY_STREAM_ENC_STATUS with stream ID
[03:06:45] [PASSED] DP_QUERY_STREAM_ENC_STATUS with client ID
[03:06:45] [PASSED] DP_QUERY_STREAM_ENC_STATUS with stream event
[03:06:45] [PASSED] DP_QUERY_STREAM_ENC_STATUS with valid stream event
[03:06:45] [PASSED] DP_QUERY_STREAM_ENC_STATUS with stream behavior
[03:06:45] [PASSED] DP_QUERY_STREAM_ENC_STATUS with a valid stream behavior
[03:06:45] ===== [PASSED] drm_test_dp_mst_sideband_msg_req_decode =====
[03:06:45] ================ [PASSED] drm_dp_mst_helper ================
[03:06:45] ================== drm_exec (7 subtests) ===================
[03:06:45] [PASSED] sanitycheck
[03:06:45] [PASSED] test_lock
[03:06:45] [PASSED] test_lock_unlock
[03:06:45] [PASSED] test_duplicates
[03:06:45] [PASSED] test_prepare
[03:06:45] [PASSED] test_prepare_array
[03:06:45] [PASSED] test_multiple_loops
[03:06:45] ==================== [PASSED] drm_exec =====================
[03:06:45] =========== drm_format_helper_test (17 subtests) ===========
[03:06:45] ============== drm_test_fb_xrgb8888_to_gray8  ==============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ========== [PASSED] drm_test_fb_xrgb8888_to_gray8 ==========
[03:06:45] ============= drm_test_fb_xrgb8888_to_rgb332  ==============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ========= [PASSED] drm_test_fb_xrgb8888_to_rgb332 ==========
[03:06:45] ============= drm_test_fb_xrgb8888_to_rgb565  ==============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ========= [PASSED] drm_test_fb_xrgb8888_to_rgb565 ==========
[03:06:45] ============ drm_test_fb_xrgb8888_to_xrgb1555  =============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ======== [PASSED] drm_test_fb_xrgb8888_to_xrgb1555 =========
[03:06:45] ============ drm_test_fb_xrgb8888_to_argb1555  =============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ======== [PASSED] drm_test_fb_xrgb8888_to_argb1555 =========
[03:06:45] ============ drm_test_fb_xrgb8888_to_rgba5551  =============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ======== [PASSED] drm_test_fb_xrgb8888_to_rgba5551 =========
[03:06:45] ============= drm_test_fb_xrgb8888_to_rgb888  ==============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ========= [PASSED] drm_test_fb_xrgb8888_to_rgb888 ==========
[03:06:45] ============= drm_test_fb_xrgb8888_to_bgr888  ==============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ========= [PASSED] drm_test_fb_xrgb8888_to_bgr888 ==========
[03:06:45] ============ drm_test_fb_xrgb8888_to_argb8888  =============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ======== [PASSED] drm_test_fb_xrgb8888_to_argb8888 =========
[03:06:45] =========== drm_test_fb_xrgb8888_to_xrgb2101010  ===========
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ======= [PASSED] drm_test_fb_xrgb8888_to_xrgb2101010 =======
[03:06:45] =========== drm_test_fb_xrgb8888_to_argb2101010  ===========
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ======= [PASSED] drm_test_fb_xrgb8888_to_argb2101010 =======
[03:06:45] ============== drm_test_fb_xrgb8888_to_mono  ===============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ========== [PASSED] drm_test_fb_xrgb8888_to_mono ===========
[03:06:45] ==================== drm_test_fb_swab  =====================
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ================ [PASSED] drm_test_fb_swab =================
[03:06:45] ============ drm_test_fb_xrgb8888_to_xbgr8888  =============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ======== [PASSED] drm_test_fb_xrgb8888_to_xbgr8888 =========
[03:06:45] ============ drm_test_fb_xrgb8888_to_abgr8888  =============
[03:06:45] [PASSED] single_pixel_source_buffer
[03:06:45] [PASSED] single_pixel_clip_rectangle
[03:06:45] [PASSED] well_known_colors
[03:06:45] [PASSED] destination_pitch
[03:06:45] ======== [PASSED] drm_test_fb_xrgb8888_to_abgr8888 =========
[03:06:45] ================= drm_test_fb_clip_offset  =================
[03:06:45] [PASSED] pass through
[03:06:45] [PASSED] horizontal offset
[03:06:45] [PASSED] vertical offset
[03:06:45] [PASSED] horizontal and vertical offset
[03:06:45] [PASSED] horizontal offset (custom pitch)
[03:06:45] [PASSED] vertical offset (custom pitch)
[03:06:45] [PASSED] horizontal and vertical offset (custom pitch)
[03:06:45] ============= [PASSED] drm_test_fb_clip_offset =============
[03:06:45] =================== drm_test_fb_memcpy  ====================
[03:06:45] [PASSED] single_pixel_source_buffer: XR24 little-endian (0x34325258)
[03:06:45] [PASSED] single_pixel_source_buffer: XRA8 little-endian (0x38415258)
[03:06:45] [PASSED] single_pixel_source_buffer: YU24 little-endian (0x34325559)
[03:06:45] [PASSED] single_pixel_clip_rectangle: XB24 little-endian (0x34324258)
[03:06:45] [PASSED] single_pixel_clip_rectangle: XRA8 little-endian (0x38415258)
[03:06:45] [PASSED] single_pixel_clip_rectangle: YU24 little-endian (0x34325559)
[03:06:45] [PASSED] well_known_colors: XB24 little-endian (0x34324258)
[03:06:45] [PASSED] well_known_colors: XRA8 little-endian (0x38415258)
[03:06:45] [PASSED] well_known_colors: YU24 little-endian (0x34325559)
[03:06:45] [PASSED] destination_pitch: XB24 little-endian (0x34324258)
[03:06:45] [PASSED] destination_pitch: XRA8 little-endian (0x38415258)
[03:06:45] [PASSED] destination_pitch: YU24 little-endian (0x34325559)
[03:06:45] =============== [PASSED] drm_test_fb_memcpy ================
[03:06:45] ============= [PASSED] drm_format_helper_test ==============
[03:06:45] ================= drm_format (18 subtests) =================
[03:06:45] [PASSED] drm_test_format_block_width_invalid
[03:06:45] [PASSED] drm_test_format_block_width_one_plane
[03:06:45] [PASSED] drm_test_format_block_width_two_plane
[03:06:45] [PASSED] drm_test_format_block_width_three_plane
[03:06:45] [PASSED] drm_test_format_block_width_tiled
[03:06:45] [PASSED] drm_test_format_block_height_invalid
[03:06:45] [PASSED] drm_test_format_block_height_one_plane
[03:06:45] [PASSED] drm_test_format_block_height_two_plane
[03:06:45] [PASSED] drm_test_format_block_height_three_plane
[03:06:45] [PASSED] drm_test_format_block_height_tiled
[03:06:45] [PASSED] drm_test_format_min_pitch_invalid
[03:06:45] [PASSED] drm_test_format_min_pitch_one_plane_8bpp
[03:06:45] [PASSED] drm_test_format_min_pitch_one_plane_16bpp
[03:06:45] [PASSED] drm_test_format_min_pitch_one_plane_24bpp
[03:06:45] [PASSED] drm_test_format_min_pitch_one_plane_32bpp
[03:06:45] [PASSED] drm_test_format_min_pitch_two_plane
[03:06:45] [PASSED] drm_test_format_min_pitch_three_plane_8bpp
[03:06:45] [PASSED] drm_test_format_min_pitch_tiled
[03:06:45] =================== [PASSED] drm_format ====================
[03:06:45] ============== drm_framebuffer (10 subtests) ===============
[03:06:45] ========== drm_test_framebuffer_check_src_coords  ==========
[03:06:45] [PASSED] Success: source fits into fb
[03:06:45] [PASSED] Fail: overflowing fb with x-axis coordinate
[03:06:45] [PASSED] Fail: overflowing fb with y-axis coordinate
[03:06:45] [PASSED] Fail: overflowing fb with source width
[03:06:45] [PASSED] Fail: overflowing fb with source height
[03:06:45] ====== [PASSED] drm_test_framebuffer_check_src_coords ======
[03:06:45] [PASSED] drm_test_framebuffer_cleanup
[03:06:45] =============== drm_test_framebuffer_create  ===============
[03:06:45] [PASSED] ABGR8888 normal sizes
[03:06:45] [PASSED] ABGR8888 max sizes
[03:06:45] [PASSED] ABGR8888 pitch greater than min required
[03:06:45] [PASSED] ABGR8888 pitch less than min required
[03:06:45] [PASSED] ABGR8888 Invalid width
[03:06:45] [PASSED] ABGR8888 Invalid buffer handle
[03:06:45] [PASSED] No pixel format
[03:06:45] [PASSED] ABGR8888 Width 0
[03:06:45] [PASSED] ABGR8888 Height 0
[03:06:45] [PASSED] ABGR8888 Out of bound height * pitch combination
[03:06:45] [PASSED] ABGR8888 Large buffer offset
[03:06:45] [PASSED] ABGR8888 Buffer offset for inexistent plane
[03:06:45] [PASSED] ABGR8888 Invalid flag
[03:06:45] [PASSED] ABGR8888 Set DRM_MODE_FB_MODIFIERS without modifiers
[03:06:45] [PASSED] ABGR8888 Valid buffer modifier
[03:06:45] [PASSED] ABGR8888 Invalid buffer modifier(DRM_FORMAT_MOD_SAMSUNG_64_32_TILE)
[03:06:45] [PASSED] ABGR8888 Extra pitches without DRM_MODE_FB_MODIFIERS
[03:06:45] [PASSED] ABGR8888 Extra pitches with DRM_MODE_FB_MODIFIERS
[03:06:45] [PASSED] NV12 Normal sizes
[03:06:45] [PASSED] NV12 Max sizes
[03:06:45] [PASSED] NV12 Invalid pitch
[03:06:45] [PASSED] NV12 Invalid modifier/missing DRM_MODE_FB_MODIFIERS flag
[03:06:45] [PASSED] NV12 different  modifier per-plane
[03:06:45] [PASSED] NV12 with DRM_FORMAT_MOD_SAMSUNG_64_32_TILE
[03:06:45] [PASSED] NV12 Valid modifiers without DRM_MODE_FB_MODIFIERS
[03:06:45] [PASSED] NV12 Modifier for inexistent plane
[03:06:45] [PASSED] NV12 Handle for inexistent plane
[03:06:45] [PASSED] NV12 Handle for inexistent plane without DRM_MODE_FB_MODIFIERS
[03:06:45] [PASSED] YVU420 DRM_MODE_FB_MODIFIERS set without modifier
[03:06:45] [PASSED] YVU420 Normal sizes
[03:06:45] [PASSED] YVU420 Max sizes
[03:06:45] [PASSED] YVU420 Invalid pitch
[03:06:45] [PASSED] YVU420 Different pitches
[03:06:45] [PASSED] YVU420 Different buffer offsets/pitches
[03:06:45] [PASSED] YVU420 Modifier set just for plane 0, without DRM_MODE_FB_MODIFIERS
[03:06:45] [PASSED] YVU420 Modifier set just for planes 0, 1, without DRM_MODE_FB_MODIFIERS
[03:06:45] [PASSED] YVU420 Modifier set just for plane 0, 1, with DRM_MODE_FB_MODIFIERS
[03:06:45] [PASSED] YVU420 Valid modifier
[03:06:45] [PASSED] YVU420 Different modifiers per plane
[03:06:45] [PASSED] YVU420 Modifier for inexistent plane
[03:06:45] [PASSED] YUV420_10BIT Invalid modifier(DRM_FORMAT_MOD_LINEAR)
[03:06:45] [PASSED] X0L2 Normal sizes
[03:06:45] [PASSED] X0L2 Max sizes
[03:06:45] [PASSED] X0L2 Invalid pitch
[03:06:45] [PASSED] X0L2 Pitch greater than minimum required
[03:06:45] [PASSED] X0L2 Handle for inexistent plane
[03:06:45] [PASSED] X0L2 Offset for inexistent plane, without DRM_MODE_FB_MODIFIERS set
[03:06:45] [PASSED] X0L2 Modifier without DRM_MODE_FB_MODIFIERS set
[03:06:45] [PASSED] X0L2 Valid modifier
[03:06:45] [PASSED] X0L2 Modifier for inexistent plane
[03:06:45] =========== [PASSED] drm_test_framebuffer_create ===========
[03:06:45] [PASSED] drm_test_framebuffer_free
[03:06:45] [PASSED] drm_test_framebuffer_init
[03:06:45] [PASSED] drm_test_framebuffer_init_bad_format
[03:06:45] [PASSED] drm_test_framebuffer_init_dev_mismatch
[03:06:45] [PASSED] drm_test_framebuffer_lookup
[03:06:45] [PASSED] drm_test_framebuffer_lookup_inexistent
[03:06:45] [PASSED] drm_test_framebuffer_modifiers_not_supported
[03:06:45] ================= [PASSED] drm_framebuffer =================
[03:06:45] ================ drm_gem_shmem (8 subtests) ================
[03:06:45] [PASSED] drm_gem_shmem_test_obj_create
[03:06:45] [PASSED] drm_gem_shmem_test_obj_create_private
[03:06:45] [PASSED] drm_gem_shmem_test_pin_pages
[03:06:45] [PASSED] drm_gem_shmem_test_vmap
[03:06:45] [PASSED] drm_gem_shmem_test_get_sg_table
[03:06:45] [PASSED] drm_gem_shmem_test_get_pages_sgt
[03:06:45] [PASSED] drm_gem_shmem_test_madvise
[03:06:45] [PASSED] drm_gem_shmem_test_purge
[03:06:45] ================== [PASSED] drm_gem_shmem ==================
[03:06:45] === drm_atomic_helper_connector_hdmi_check (29 subtests) ===
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_auto_cea_mode
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_auto_cea_mode_vic_1
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_full_cea_mode
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_full_cea_mode_vic_1
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_limited_cea_mode
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_limited_cea_mode_vic_1
[03:06:45] ====== drm_test_check_broadcast_rgb_cea_mode_yuv420  =======
[03:06:45] [PASSED] Automatic
[03:06:45] [PASSED] Full
[03:06:45] [PASSED] Limited 16:235
[03:06:45] == [PASSED] drm_test_check_broadcast_rgb_cea_mode_yuv420 ===
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_crtc_mode_changed
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_crtc_mode_not_changed
[03:06:45] [PASSED] drm_test_check_disable_connector
[03:06:45] [PASSED] drm_test_check_hdmi_funcs_reject_rate
[03:06:45] [PASSED] drm_test_check_max_tmds_rate_bpc_fallback_rgb
[03:06:45] [PASSED] drm_test_check_max_tmds_rate_bpc_fallback_yuv420
[03:06:45] [PASSED] drm_test_check_max_tmds_rate_bpc_fallback_ignore_yuv422
[03:06:45] [PASSED] drm_test_check_max_tmds_rate_bpc_fallback_ignore_yuv420
[03:06:45] [PASSED] drm_test_check_driver_unsupported_fallback_yuv420
[03:06:45] [PASSED] drm_test_check_output_bpc_crtc_mode_changed
[03:06:45] [PASSED] drm_test_check_output_bpc_crtc_mode_not_changed
[03:06:45] [PASSED] drm_test_check_output_bpc_dvi
[03:06:45] [PASSED] drm_test_check_output_bpc_format_vic_1
[03:06:45] [PASSED] drm_test_check_output_bpc_format_display_8bpc_only
[03:06:45] [PASSED] drm_test_check_output_bpc_format_display_rgb_only
[03:06:45] [PASSED] drm_test_check_output_bpc_format_driver_8bpc_only
[03:06:45] [PASSED] drm_test_check_output_bpc_format_driver_rgb_only
[03:06:45] [PASSED] drm_test_check_tmds_char_rate_rgb_8bpc
[03:06:45] [PASSED] drm_test_check_tmds_char_rate_rgb_10bpc
[03:06:45] [PASSED] drm_test_check_tmds_char_rate_rgb_12bpc
[03:06:45] ============ drm_test_check_hdmi_color_format  =============
[03:06:45] [PASSED] AUTO -> RGB
[03:06:45] [PASSED] YCBCR422 -> YUV422
[03:06:45] [PASSED] YCBCR420 -> YUV420
[03:06:45] [PASSED] YCBCR444 -> YUV444
[03:06:45] [PASSED] RGB -> RGB
[03:06:45] ======== [PASSED] drm_test_check_hdmi_color_format =========
[03:06:45] ======== drm_test_check_hdmi_color_format_420_only  ========
[03:06:45] [PASSED] RGB should fail
[03:06:45] [PASSED] YUV444 should fail
[03:06:45] [PASSED] YUV422 should fail
[03:06:45] [PASSED] YUV420 should work
[03:06:45] ==== [PASSED] drm_test_check_hdmi_color_format_420_only ====
[03:06:45] ===== [PASSED] drm_atomic_helper_connector_hdmi_check ======
[03:06:45] === drm_atomic_helper_connector_hdmi_reset (6 subtests) ====
[03:06:45] [PASSED] drm_test_check_broadcast_rgb_value
[03:06:45] [PASSED] drm_test_check_bpc_8_value
[03:06:45] [PASSED] drm_test_check_bpc_10_value
[03:06:45] [PASSED] drm_test_check_bpc_12_value
[03:06:45] [PASSED] drm_test_check_format_value
[03:06:45] [PASSED] drm_test_check_tmds_char_value
[03:06:45] ===== [PASSED] drm_atomic_helper_connector_hdmi_reset ======
[03:06:45] = drm_atomic_helper_connector_hdmi_mode_valid (7 subtests) =
[03:06:45] [PASSED] drm_test_check_mode_valid
[03:06:45] [PASSED] drm_test_check_mode_valid_reject
[03:06:45] [PASSED] drm_test_check_mode_valid_reject_rate
[03:06:45] [PASSED] drm_test_check_mode_valid_reject_max_clock
[03:06:45] [PASSED] drm_test_check_mode_valid_yuv420_only_max_clock
[03:06:45] [PASSED] drm_test_check_mode_valid_reject_yuv420_only_connector
[03:06:45] [PASSED] drm_test_check_mode_valid_accept_yuv420_also_connector_rgb
[03:06:45] === [PASSED] drm_atomic_helper_connector_hdmi_mode_valid ===
[03:06:45] = drm_atomic_helper_connector_hdmi_infoframes (5 subtests) =
[03:06:45] [PASSED] drm_test_check_infoframes
[03:06:45] [PASSED] drm_test_check_reject_avi_infoframe
[03:06:45] [PASSED] drm_test_check_reject_hdr_infoframe_bpc_8
[03:06:45] [PASSED] drm_test_check_reject_hdr_infoframe_bpc_10
[03:06:45] [PASSED] drm_test_check_reject_audio_infoframe
[03:06:45] === [PASSED] drm_atomic_helper_connector_hdmi_infoframes ===
[03:06:45] ================= drm_managed (2 subtests) =================
[03:06:45] [PASSED] drm_test_managed_release_action
[03:06:45] [PASSED] drm_test_managed_run_action
[03:06:45] =================== [PASSED] drm_managed ===================
[03:06:45] =================== drm_mm (6 subtests) ====================
[03:06:45] [PASSED] drm_test_mm_init
[03:06:45] [PASSED] drm_test_mm_debug
[03:06:45] [PASSED] drm_test_mm_align32
[03:06:45] [PASSED] drm_test_mm_align64
[03:06:45] [PASSED] drm_test_mm_lowest
[03:06:45] [PASSED] drm_test_mm_highest
[03:06:45] ===================== [PASSED] drm_mm ======================
[03:06:45] ============= drm_modes_analog_tv (5 subtests) =============
[03:06:45] [PASSED] drm_test_modes_analog_tv_mono_576i
[03:06:45] [PASSED] drm_test_modes_analog_tv_ntsc_480i
[03:06:45] [PASSED] drm_test_modes_analog_tv_ntsc_480i_inlined
[03:06:45] [PASSED] drm_test_modes_analog_tv_pal_576i
[03:06:45] [PASSED] drm_test_modes_analog_tv_pal_576i_inlined
[03:06:45] =============== [PASSED] drm_modes_analog_tv ===============
[03:06:45] ============== drm_plane_helper (2 subtests) ===============
[03:06:45] =============== drm_test_check_plane_state  ================
[03:06:45] [PASSED] clipping_simple
[03:06:45] [PASSED] clipping_rotate_reflect
[03:06:45] [PASSED] positioning_simple
[03:06:45] [PASSED] upscaling
[03:06:45] [PASSED] downscaling
[03:06:45] [PASSED] rounding1
[03:06:45] [PASSED] rounding2
[03:06:45] [PASSED] rounding3
[03:06:45] [PASSED] rounding4
[03:06:45] =========== [PASSED] drm_test_check_plane_state ============
[03:06:45] =========== drm_test_check_invalid_plane_state  ============
[03:06:45] [PASSED] positioning_invalid
[03:06:45] [PASSED] upscaling_invalid
[03:06:45] [PASSED] downscaling_invalid
[03:06:45] ======= [PASSED] drm_test_check_invalid_plane_state ========
[03:06:45] ================ [PASSED] drm_plane_helper =================
[03:06:45] ====== drm_connector_helper_tv_get_modes (1 subtest) =======
[03:06:45] ====== drm_test_connector_helper_tv_get_modes_check  =======
[03:06:45] [PASSED] None
[03:06:45] [PASSED] PAL
[03:06:45] [PASSED] NTSC
[03:06:45] [PASSED] Both, NTSC Default
[03:06:45] [PASSED] Both, PAL Default
[03:06:45] [PASSED] Both, NTSC Default, with PAL on command-line
[03:06:45] [PASSED] Both, PAL Default, with NTSC on command-line
[03:06:45] == [PASSED] drm_test_connector_helper_tv_get_modes_check ===
[03:06:45] ======== [PASSED] drm_connector_helper_tv_get_modes ========
[03:06:45] ================== drm_rect (9 subtests) ===================
[03:06:45] [PASSED] drm_test_rect_clip_scaled_div_by_zero
[03:06:45] [PASSED] drm_test_rect_clip_scaled_not_clipped
[03:06:45] [PASSED] drm_test_rect_clip_scaled_clipped
[03:06:45] [PASSED] drm_test_rect_clip_scaled_signed_vs_unsigned
[03:06:45] ================= drm_test_rect_intersect  =================
[03:06:45] [PASSED] top-left x bottom-right: 2x2+1+1 x 2x2+0+0
[03:06:45] [PASSED] top-right x bottom-left: 2x2+0+0 x 2x2+1-1
[03:06:45] [PASSED] bottom-left x top-right: 2x2+1-1 x 2x2+0+0
[03:06:45] [PASSED] bottom-right x top-left: 2x2+0+0 x 2x2+1+1
[03:06:45] [PASSED] right x left: 2x1+0+0 x 3x1+1+0
[03:06:45] [PASSED] left x right: 3x1+1+0 x 2x1+0+0
[03:06:45] [PASSED] up x bottom: 1x2+0+0 x 1x3+0-1
[03:06:45] [PASSED] bottom x up: 1x3+0-1 x 1x2+0+0
[03:06:45] [PASSED] touching corner: 1x1+0+0 x 2x2+1+1
[03:06:45] [PASSED] touching side: 1x1+0+0 x 1x1+1+0
[03:06:45] [PASSED] equal rects: 2x2+0+0 x 2x2+0+0
[03:06:45] [PASSED] inside another: 2x2+0+0 x 1x1+1+1
[03:06:45] [PASSED] far away: 1x1+0+0 x 1x1+3+6
[03:06:45] [PASSED] points intersecting: 0x0+5+10 x 0x0+5+10
[03:06:45] [PASSED] points not intersecting: 0x0+0+0 x 0x0+5+10
[03:06:45] ============= [PASSED] drm_test_rect_intersect =============
[03:06:45] ================ drm_test_rect_calc_hscale  ================
[03:06:45] [PASSED] normal use
[03:06:45] [PASSED] out of max range
[03:06:45] [PASSED] out of min range
[03:06:45] [PASSED] zero dst
[03:06:45] [PASSED] negative src
[03:06:45] [PASSED] negative dst
[03:06:45] ============ [PASSED] drm_test_rect_calc_hscale ============
[03:06:45] ================ drm_test_rect_calc_vscale  ================
[03:06:45] [PASSED] normal use
[03:06:45] [PASSED] out of max range
[03:06:45] [PASSED] out of min range
[03:06:45] [PASSED] zero dst
[03:06:45] [PASSED] negative src
[03:06:45] [PASSED] negative dst
[03:06:45] ============ [PASSED] drm_test_rect_calc_vscale ============
[03:06:45] ================== drm_test_rect_rotate  ===================
[03:06:45] [PASSED] reflect-x
[03:06:45] [PASSED] reflect-y
[03:06:45] [PASSED] rotate-0
[03:06:45] [PASSED] rotate-90
[03:06:45] [PASSED] rotate-180
[03:06:45] [PASSED] rotate-270
[03:06:45] ============== [PASSED] drm_test_rect_rotate ===============
[03:06:45] ================ drm_test_rect_rotate_inv  =================
[03:06:45] [PASSED] reflect-x
[03:06:45] [PASSED] reflect-y
[03:06:45] [PASSED] rotate-0
[03:06:45] [PASSED] rotate-90
[03:06:45] [PASSED] rotate-180
[03:06:45] [PASSED] rotate-270
[03:06:45] ============ [PASSED] drm_test_rect_rotate_inv =============
[03:06:45] ==================== [PASSED] drm_rect =====================
[03:06:45] ============ drm_sysfb_modeset_test (1 subtest) ============
[03:06:45] ============ drm_test_sysfb_build_fourcc_list  =============
[03:06:45] [PASSED] no native formats
[03:06:45] [PASSED] XRGB8888 as native format
[03:06:45] [PASSED] remove duplicates
[03:06:45] [PASSED] convert alpha formats
[03:06:45] [PASSED] random formats
[03:06:45] ======== [PASSED] drm_test_sysfb_build_fourcc_list =========
[03:06:45] ============= [PASSED] drm_sysfb_modeset_test ==============
[03:06:45] ================== drm_fixp (2 subtests) ===================
[03:06:45] [PASSED] drm_test_int2fixp
[03:06:45] [PASSED] drm_test_sm2fixp
[03:06:45] ==================== [PASSED] drm_fixp =====================
[03:06:45] ============================================================
[03:06:45] Testing complete. Ran 639 tests: passed: 639
[03:06:45] Elapsed time: 26.415s total, 1.834s configuring, 24.416s building, 0.140s running

+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/ttm/tests/.kunitconfig [03:06:45] Configuring KUnit Kernel ...
Regenerating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[03:06:47] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=48
[03:06:57] Starting KUnit Kernel (1/1)...
[03:06:57] ============================================================
Running tests with:
$ .kunit/linux kunit.enable=1 mem=1G console=tty kunit_shutdown=halt
[03:06:57] ================= ttm_device (5 subtests) ==================
[03:06:57] [PASSED] ttm_device_init_basic
[03:06:57] [PASSED] ttm_device_init_multiple
[03:06:57] [PASSED] ttm_device_fini_basic
[03:06:57] [PASSED] ttm_device_init_no_vma_man
[03:06:57] ================== ttm_device_init_pools  ==================
[03:06:57] [PASSED] No DMA allocations, no DMA32 required
[03:06:57] [PASSED] DMA allocations, DMA32 required
[03:06:57] [PASSED] No DMA allocations, DMA32 required
[03:06:57] [PASSED] DMA allocations, no DMA32 required
[03:06:57] ============== [PASSED] ttm_device_init_pools ==============
[03:06:57] =================== [PASSED] ttm_device ====================
[03:06:57] ================== ttm_pool (8 subtests) ===================
[03:06:57] ================== ttm_pool_alloc_basic  ===================
[03:06:57] [PASSED] One page
[03:06:57] [PASSED] More than one page
[03:06:57] [PASSED] Above the allocation limit
[03:06:57] [PASSED] One page, with coherent DMA mappings enabled
[03:06:57] [PASSED] Above the allocation limit, with coherent DMA mappings enabled
[03:06:57] ============== [PASSED] ttm_pool_alloc_basic ===============
[03:06:57] ============== ttm_pool_alloc_basic_dma_addr  ==============
[03:06:57] [PASSED] One page
[03:06:57] [PASSED] More than one page
[03:06:57] [PASSED] Above the allocation limit
[03:06:57] [PASSED] One page, with coherent DMA mappings enabled
[03:06:57] [PASSED] Above the allocation limit, with coherent DMA mappings enabled
[03:06:57] ========== [PASSED] ttm_pool_alloc_basic_dma_addr ==========
[03:06:57] [PASSED] ttm_pool_alloc_order_caching_match
[03:06:57] [PASSED] ttm_pool_alloc_caching_mismatch
[03:06:57] [PASSED] ttm_pool_alloc_order_mismatch
[03:06:57] [PASSED] ttm_pool_free_dma_alloc
[03:06:57] [PASSED] ttm_pool_free_no_dma_alloc
[03:06:57] [PASSED] ttm_pool_fini_basic
[03:06:57] ==================== [PASSED] ttm_pool =====================
[03:06:57] ================ ttm_resource (8 subtests) =================
[03:06:57] ================= ttm_resource_init_basic  =================
[03:06:57] [PASSED] Init resource in TTM_PL_SYSTEM
[03:06:57] [PASSED] Init resource in TTM_PL_VRAM
[03:06:57] [PASSED] Init resource in a private placement
[03:06:57] [PASSED] Init resource in TTM_PL_SYSTEM, set placement flags
[03:06:57] ============= [PASSED] ttm_resource_init_basic =============
[03:06:57] [PASSED] ttm_resource_init_pinned
[03:06:57] [PASSED] ttm_resource_fini_basic
[03:06:57] [PASSED] ttm_resource_manager_init_basic
[03:06:57] [PASSED] ttm_resource_manager_usage_basic
[03:06:57] [PASSED] ttm_resource_manager_set_used_basic
[03:06:57] [PASSED] ttm_sys_man_alloc_basic
[03:06:57] [PASSED] ttm_sys_man_free_basic
[03:06:57] ================== [PASSED] ttm_resource ===================
[03:06:57] =================== ttm_tt (15 subtests) ===================
[03:06:57] ==================== ttm_tt_init_basic  ====================
[03:06:57] [PASSED] Page-aligned size
[03:06:57] [PASSED] Extra pages requested
[03:06:57] ================ [PASSED] ttm_tt_init_basic ================
[03:06:57] [PASSED] ttm_tt_init_misaligned
[03:06:57] [PASSED] ttm_tt_fini_basic
[03:06:57] [PASSED] ttm_tt_fini_sg
[03:06:57] [PASSED] ttm_tt_fini_shmem
[03:06:57] [PASSED] ttm_tt_create_basic
[03:06:57] [PASSED] ttm_tt_create_invalid_bo_type
[03:06:57] [PASSED] ttm_tt_create_ttm_exists
[03:06:57] [PASSED] ttm_tt_create_failed
[03:06:57] [PASSED] ttm_tt_destroy_basic
[03:06:57] [PASSED] ttm_tt_populate_null_ttm
[03:06:57] [PASSED] ttm_tt_populate_populated_ttm
[03:06:57] [PASSED] ttm_tt_unpopulate_basic
[03:06:57] [PASSED] ttm_tt_unpopulate_empty_ttm
[03:06:57] [PASSED] ttm_tt_swapin_basic
[03:06:57] ===================== [PASSED] ttm_tt ======================
[03:06:57] =================== ttm_bo (14 subtests) ===================
[03:06:57] =========== ttm_bo_reserve_optimistic_no_ticket  ===========
[03:06:57] [PASSED] Cannot be interrupted and sleeps
[03:06:57] [PASSED] Cannot be interrupted, locks straight away
[03:06:57] [PASSED] Can be interrupted, sleeps
[03:06:57] ======= [PASSED] ttm_bo_reserve_optimistic_no_ticket =======
[03:06:57] [PASSED] ttm_bo_reserve_locked_no_sleep
[03:06:57] [PASSED] ttm_bo_reserve_no_wait_ticket
[03:06:57] [PASSED] ttm_bo_reserve_double_resv
[03:06:57] [PASSED] ttm_bo_reserve_interrupted
[03:06:57] [PASSED] ttm_bo_reserve_deadlock
[03:06:57] [PASSED] ttm_bo_unreserve_basic
[03:06:57] [PASSED] ttm_bo_unreserve_pinned
[03:06:57] [PASSED] ttm_bo_unreserve_bulk
[03:06:57] [PASSED] ttm_bo_fini_basic
[03:06:57] [PASSED] ttm_bo_fini_shared_resv
[03:06:57] [PASSED] ttm_bo_pin_basic
[03:06:57] [PASSED] ttm_bo_pin_unpin_resource
[03:06:57] [PASSED] ttm_bo_multiple_pin_one_unpin
[03:06:57] ===================== [PASSED] ttm_bo ======================
[03:06:57] ============== ttm_bo_validate (22 subtests) ===============
[03:06:57] ============== ttm_bo_init_reserved_sys_man  ===============
[03:06:57] [PASSED] Buffer object for userspace
[03:06:57] [PASSED] Kernel buffer object
[03:06:57] [PASSED] Shared buffer object
[03:06:57] ========== [PASSED] ttm_bo_init_reserved_sys_man ===========
[03:06:57] ============== ttm_bo_init_reserved_mock_man  ==============
[03:06:57] [PASSED] Buffer object for userspace
[03:06:57] [PASSED] Kernel buffer object
[03:06:57] [PASSED] Shared buffer object
[03:06:57] ========== [PASSED] ttm_bo_init_reserved_mock_man ==========
[03:06:57] [PASSED] ttm_bo_init_reserved_resv
[03:06:57] ================== ttm_bo_validate_basic  ==================
[03:06:57] [PASSED] Buffer object for userspace
[03:06:57] [PASSED] Kernel buffer object
[03:06:57] [PASSED] Shared buffer object
[03:06:57] ============== [PASSED] ttm_bo_validate_basic ==============
[03:06:57] [PASSED] ttm_bo_validate_invalid_placement
[03:06:57] ============= ttm_bo_validate_same_placement  ==============
[03:06:57] [PASSED] System manager
[03:06:57] [PASSED] VRAM manager
[03:06:57] ========= [PASSED] ttm_bo_validate_same_placement ==========
[03:06:57] [PASSED] ttm_bo_validate_failed_alloc
[03:06:57] [PASSED] ttm_bo_validate_pinned
[03:06:57] [PASSED] ttm_bo_validate_busy_placement
[03:06:57] ================ ttm_bo_validate_multihop  =================
[03:06:57] [PASSED] Buffer object for userspace
[03:06:57] [PASSED] Kernel buffer object
[03:06:57] [PASSED] Shared buffer object
[03:06:57] ============ [PASSED] ttm_bo_validate_multihop =============
[03:06:57] ========== ttm_bo_validate_no_placement_signaled  ==========
[03:06:57] [PASSED] Buffer object in system domain, no page vector
[03:06:57] [PASSED] Buffer object in system domain with an existing page vector
[03:06:57] ====== [PASSED] ttm_bo_validate_no_placement_signaled ======
[03:06:57] ======== ttm_bo_validate_no_placement_not_signaled  ========
[03:06:57] [PASSED] Buffer object for userspace
[03:06:57] [PASSED] Kernel buffer object
[03:06:57] [PASSED] Shared buffer object
[03:06:57] ==== [PASSED] ttm_bo_validate_no_placement_not_signaled ====
[03:06:57] [PASSED] ttm_bo_validate_move_fence_signaled
[03:06:57] ========= ttm_bo_validate_move_fence_not_signaled  =========
[03:06:57] [PASSED] Waits for GPU
[03:06:57] [PASSED] Tries to lock straight away
[03:06:57] ===== [PASSED] ttm_bo_validate_move_fence_not_signaled =====
[03:06:57] [PASSED] ttm_bo_validate_swapout
[03:06:57] [PASSED] ttm_bo_validate_happy_evict
[03:06:57] [PASSED] ttm_bo_validate_all_pinned_evict
[03:06:57] [PASSED] ttm_bo_validate_allowed_only_evict
[03:06:57] [PASSED] ttm_bo_validate_deleted_evict
[03:06:57] [PASSED] ttm_bo_validate_busy_domain_evict
[03:06:57] [PASSED] ttm_bo_validate_evict_gutting
[03:06:57] [PASSED] ttm_bo_validate_recrusive_evict
[03:06:57] ================= [PASSED] ttm_bo_validate =================
[03:06:57] ============================================================
[03:06:57] Testing complete. Ran 102 tests: passed: 102
[03:06:57] Elapsed time: 11.988s total, 1.800s configuring, 9.973s building, 0.186s running

+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel

#141 lore raw

✗ CI.checksparse: warning for drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)
URL : https://patchwork.freedesktop.org/series/170216/
State : warning

== Summary ==

+ trap cleanup EXIT
+ KERNEL=/kernel
+ MT=/root/linux/maintainer-tools
+ git clone https://gitlab.freedesktop.org/drm/maintainer-tools /root/linux/maintainer-tools Cloning into '/root/linux/maintainer-tools'...
warning: redirecting to https://gitlab.freedesktop.org/drm/maintainer-tools.git/
+ make -C /root/linux/maintainer-tools
make: Entering directory '/root/linux/maintainer-tools'
cc -O2 -g -Wextra -o remap-log remap-log.c
make: Leaving directory '/root/linux/maintainer-tools'
+ cd /kernel
+ git config --global --add safe.directory /kernel
+ /root/linux/maintainer-tools/dim sparse --fast 7bb9a26014f9ba1b2cb1300252d1eeb83f169c06 Sparse version: 0.6.4 (Ubuntu: 0.6.4-4ubuntu3)
Fast mode used, each commit won't be checked separately. +/kernel/Makefile:1280: C=1 specified, but sparse is not available or not up to date

+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel

#142 lore raw

✓ Xe.CI.BAT: success for drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)
URL : https://patchwork.freedesktop.org/series/170216/
State : success

== Summary ==

CI Bug Log - changes from xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06_BAT -> xe-pw-170216v2_BAT
====================================================

Summary
-------

**SUCCESS**

No regressions found.

Participating hosts (13 -> 13)
------------------------------

No changes in participating hosts

Changes
-------

No changes found

Build changes
-------------

* Linux: xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06 -> xe-pw-170216v2

IGT_9002: 9002
xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06: 7bb9a26014f9ba1b2cb1300252d1eeb83f169c06
xe-pw-170216v2: 170216v2

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/index.html

#143 lore raw

✓ Xe.CI.FULL: success for drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: drm/ttm, drm/xe: Minimize dma-resv hold times and defragment sub-optimally backed BOs (rev2)
URL : https://patchwork.freedesktop.org/series/170216/
State : success

== Summary ==

CI Bug Log - changes from xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06_FULL -> xe-pw-170216v2_FULL
====================================================

Summary
-------

**SUCCESS**

No regressions found.

Participating hosts (2 -> 2)
------------------------------

No changes in participating hosts

Known issues
------------

Here are the changes found in xe-pw-170216v2_FULL that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@kms_cursor_legacy@flip-vs-cursor-legacy:
- shard-bmg: [PASS][1] -> [FAIL][2] ([Intel XE#7571])
[1]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-2/igt@[email protected]
[2]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-3/igt@[email protected]

* igt@kms_flip@flip-vs-expired-vblank@b-edp1:
- shard-lnl: [PASS][3] -> [FAIL][4] ([Intel XE#301])
[3]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-lnl-8/igt@kms_flip@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-lnl-6/igt@kms_flip@[email protected]

* igt@kms_frontbuffer_tracking@drrs-2p-scndscrn-spr-indfb-move:
- shard-bmg: NOTRUN -> [SKIP][5] ([Intel XE#2311]) +2 other tests skip
[5]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-9/igt@[email protected]

* igt@kms_frontbuffer_tracking@psr-2p-primscrn-pri-indfb-draw-mmap-wc:
- shard-bmg: NOTRUN -> [SKIP][6] ([Intel XE#2313]) +1 other test skip
[6]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-9/igt@kms_frontbuffer_tracking@psr-2p-primscrn-pri-indfb-draw-mmap-wc.html

* igt@xe_exec_threads@threads-multi-queue-cm-shared-vm-userptr-invalidate-race:
- shard-bmg: NOTRUN -> [SKIP][7] ([Intel XE#8378])
[7]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-9/igt@xe_exec_threads@threads-multi-queue-cm-shared-vm-userptr-invalidate-race.html

* igt@xe_fault_injection@inject-fault-probe-function-xe_guc_relay_init:
- shard-bmg: [PASS][8] -> [ABORT][9] ([Intel XE#8007]) +2 other tests abort
[8]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-8/igt@xe_fault_injection@inject-fault-probe-function-xe_guc_relay_init.html
[9]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-3/igt@xe_fault_injection@inject-fault-probe-function-xe_guc_relay_init.html

* igt@xe_pat@pt-caching:
- shard-bmg: [PASS][10] -> [ABORT][11] ([Intel XE#7893] / [Intel XE#8300])
[10]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-5/igt@[email protected]
[11]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-2/igt@[email protected]

* igt@xe_wedged@wedged-at-any-timeout:
- shard-lnl: [PASS][12] -> [ABORT][13] ([Intel XE#3119])
[12]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-lnl-3/igt@[email protected]
[13]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-lnl-3/igt@[email protected]

#### Possible fixes ####

* igt@kms_ccs@crc-primary-suspend-4-tiled-bmg-ccs:
- shard-bmg: [INCOMPLETE][14] ([Intel XE#7084] / [Intel XE#8150]) -> [PASS][15] +1 other test pass
[14]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-7/igt@[email protected]
[15]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-9/igt@[email protected]

* igt@kms_flip@flip-vs-expired-vblank-interruptible@a-edp1:
- shard-lnl: [FAIL][16] ([Intel XE#301]) -> [PASS][17]
[16]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-lnl-4/igt@kms_flip@[email protected]
[17]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-lnl-8/igt@kms_flip@[email protected]

* igt@kms_flip@wf_vblank-ts-check:
- shard-bmg: [FAIL][18] ([Intel XE#5408] / [Intel XE#6266]) -> [PASS][19]
[18]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-6/igt@kms_flip@wf_vblank-ts-check.html
[19]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-9/igt@kms_flip@wf_vblank-ts-check.html

* igt@kms_flip@wf_vblank-ts-check@b-hdmi-a3:
- shard-bmg: [FAIL][20] ([Intel XE#6266]) -> [PASS][21]
[20]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-6/igt@kms_flip@[email protected]
[21]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-9/igt@kms_flip@[email protected]

* igt@xe_waitfence@reltime:
- shard-bmg: [FAIL][22] ([Intel XE#8017]) -> [PASS][23]
[22]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-3/igt@[email protected]
[23]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-1/igt@[email protected]

#### Warnings ####

* igt@kms_cursor_crc@cursor-offscreen-512x170:
- shard-lnl: [SKIP][24] ([Intel XE#2321] / [Intel XE#7355]) -> [ABORT][25] ([Intel XE#8007])
[24]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-lnl-7/igt@[email protected]
[25]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-lnl-6/igt@[email protected]

* igt@kms_tiled_display@basic-test-pattern:
- shard-bmg: [SKIP][26] ([Intel XE#2426] / [Intel XE#5848]) -> [FAIL][27] ([Intel XE#1729] / [Intel XE#7424])
[26]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-2/igt@[email protected]
[27]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-5/igt@[email protected]

* igt@xe_wedged@wedged-at-any-timeout:
- shard-bmg: [DMESG-WARN][28] ([Intel XE#5545]) -> [ABORT][29] ([Intel XE#5545])
[28]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06/shard-bmg-9/igt@[email protected]
[29]: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/shard-bmg-1/igt@[email protected]

[Intel XE#1729]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/1729
[Intel XE#2311]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/2311
[Intel XE#2313]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/2313
[Intel XE#2321]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/2321
[Intel XE#2426]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/2426
[Intel XE#301]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/301
[Intel XE#3119]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/3119
[Intel XE#5408]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/5408
[Intel XE#5545]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/5545
[Intel XE#5848]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/5848
[Intel XE#6266]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/6266
[Intel XE#7084]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/7084
[Intel XE#7355]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/7355
[Intel XE#7424]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/7424
[Intel XE#7571]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/7571
[Intel XE#7893]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/7893
[Intel XE#8007]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/8007
[Intel XE#8017]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/8017
[Intel XE#8150]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/8150
[Intel XE#8300]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/8300
[Intel XE#8378]: https://gitlab.freedesktop.org/drm/xe/kernel/issues/8378

Build changes
-------------

* Linux: xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06 -> xe-pw-170216v2

IGT_9002: 9002
xe-5386-7bb9a26014f9ba1b2cb1300252d1eeb83f169c06: 7bb9a26014f9ba1b2cb1300252d1eeb83f169c06
xe-pw-170216v2: 170216v2

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/intel-xe/xe-pw-170216v2/index.html

#144 lore raw
In reply to Matthew Brost
Quoted 35 lines · Matthew Brost

The overarching goal of this series is to keep user-facing IOCTLs snappy by holding the BO dma-resv lock for the shortest possible time, and to push the expensive, best-effort work - page (re)allocation, DMA mapping and placement fixups - into the background or out of the locked critical section entirely.

Two related mechanisms fall out of that goal:

- Out-of-lock backing. Page allocation and DMA mapping are the dominant cost of gem_create and of BO moves, and doing them under the dma-resv lock serializes otherwise-independent clients. This series preallocates the backing pages (and, where applicable, builds the DMA/IOVA mapping) outside the lock, then transfers the result into the BO under a brief lock hold. The user gets a fast IOCTL return; the heavy lifting happens without contending the reservation.

- Page defragmentation. A BO allocated under memory pressure keeps its scattered, sub-optimally-ordered backing for its entire lifetime, costing TLB efficiency forever. TTM grows the plumbing to track order-failure and to re-back a populated BO in place at the beneficial order, and Xe wires up a background delayed worker that promotes such BOs on the GPU once memory is available again - again, off the hot path and without stalling the submitting thread.

Since v1 [1] the series has grown considerably. The bulk of the new material is a direct result of profiling: once the defragmenter was in place, the remaining dma-resv hold times and the per-BO allocation/mapping costs showed up clearly, which motivated the out-of-lock preallocation, the IOVA-based mapping path, and the amdgpu counterpart. Rather than land the defragmenter alone, v2 folds in these optimizations since they share the same infrastructure and the same "hold dma-resv briefly, fix up in the background" architecture.

The series is organized in sections rather than described patch by patch:

Quoted 16 lines

- Patches 1-10 (drm/ttm): core TTM preparation - order-failure tracking, the defragmentation move, reclaim backoff, and out-of-lock page preallocation plumbing.
- Patches 11-14: other dependent drm/gpusvm and drm/xe patches this series builds on (DMA-mapping accounting, per-order DMA stats, async L2 flush, and a VM-teardown ordering fix).
- Patches 15-23 (drm/xe): the page defragmenter itself - BO tracking, the on-GPU defrag copy, xe_bo_move() handling, and the background worker with its stats and configuration.
- Patches 24-25 (drm/xe): out-of-lock system BO backing preallocation in gem_create, moving page allocation out of the dma-resv lock.
- Patches 26-32 (drm/xe): IOVA-based DMA mapping optimizations, building and finalizing the mapping outside the lock.
- Patch 33 (drm/amdgpu): the equivalent out-of-lock system BO preallocation for amdgpu, exercising the shared TTM plumbing.

Assisted-by LLM

All components in the patch series seems to be essential, but probably below ordering of the features along with how much incremental benefit they offer would have been easier to follow.

1. dma_iova usage
2. out-of-lock preallocation
3. Defragmentation support

Just a thought, not a suggestion to change.

LLM also suggests a light compaction (with __GFP_KSWAPD_RECLAIM) in between the backoff and heavy reclaim (RETRY_MAYFAIL), mainly for the gem_create preallocation. Not sure how much real value it adds though.

Niranjana

Quoted 120 lines

Testing
=======

- 3D benchmarks on Ubuntu and on Android, with memory intentionally fragmented by a separate program at launch (plus beneficial-order error injection). BOs are initially backed at a sub-optimal order and scores start lower; the background defrag worker then promotes the backing to the beneficial order and scores climb back in line with the unfragmented baseline.
- IGT:
https://patchwork.freedesktop.org/patch/739052/?series=170046&rev=2

Matt

v3:
- Fix valid raised raised by Sashiko on v2 [2]

[1] https://patchwork.freedesktop.org/series/169053/
[2] https://sashiko.dev/#/patchset/20260710215442.2444235-1-matthew.brost%40intel.com

Cc Alex Deucher <[email protected]>
Cc Carlos Santa <[email protected]>
Cc Ryan Neph <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Huang Rui <[email protected]>
Cc Matthew Auld <[email protected]>
Cc Maarten Lankhorst <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Thomas Zimmermann <[email protected]>
Cc David Airlie <[email protected]>
Cc Simona Vetter <[email protected]>
Cc Thomas Hellström <[email protected]>

Matthew Brost (31):
drm/ttm/pool: Allow backing off reclaim at the beneficial order drm/ttm/pool: Add ttm_pool_page_order_nodma() helper drm/ttm: Record sub-optimal page order allocations in ttm_tt drm/ttm: Introduce ttm_pool_alloc_iter for __ttm_pool_alloc() drm/ttm: Support defragmentation moves
drm/ttm: Add fault injection for beneficial-order allocation failures drm/ttm: Harvest beneficial-order pages on defragmentation moves drm/ttm: Bound page (re)allocation per defragmentation move drm/ttm: Preallocate beneficial-order defrag pages outside the lock drm/ttm: Add full out-of-lock preallocation for ttm_pool_alloc() drm/xe: Flush L2 asynchronously in xe_bo_trigger_rebind() drm/xe: Destroy page tables after unlinking all VMAs on VM close drm/xe: Track BOs backed at a sub-optimal page order drm/xe: Back off beneficial-order reclaim under defrag pressure drm/xe: Add xe_migrate_copy_defrag() for on-GPU defrag copies drm/xe: Handle defrag moves in xe_bo_move()
drm/xe: Skip self-copies for borrowed pages on defrag moves drm/xe: Add a page defragmentation worker
drm/xe: Add defrag GT stats
drm/xe: Add Kconfig.profile options for BO defrag configuration drm/xe: Defrag using out-of-lock page preallocation drm/xe: Add defrag profiling tracepoints
drm/xe: Preallocate system BO backing outside the dma-resv lock drm/xe: Add tracepoint for xe_gem_create_ioctl
drm/xe: Add IOVA-based xe_res_cursor variant
drm/xe: Use IOVA-based DMA mapping for eligible tt BOs drm/xe: Add per-device dependency scheduler for IOVA defrag finalize drm/xe: Add packed copy-step IOVA mapping for defrag drm/xe: Blit src-natural to dst-packed for defrag-IOVA copies drm/xe: Finalize defrag-IOVA moves with post-copy job drm/amdgpu: Preallocate system BO pages outside the reservation lock

Thomas Hellström (2):
drm/gpusvm: Add a DMA-mapping accounting callback drm/xe: Add debugfs stats for DMA-mapped pages per order

.../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |    4 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |    2 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c       |   73 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h       |    5 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |    3 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |    4 +
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       |   39 +-
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |    3 +
drivers/gpu/drm/drm_gpusvm.c                  |   17 +-
drivers/gpu/drm/ttm/ttm_bo.c                  |  118 +-
drivers/gpu/drm/ttm/ttm_bo_util.c             |   21 +-
drivers/gpu/drm/ttm/ttm_pool.c                |  947 ++++++++-
drivers/gpu/drm/ttm/ttm_tt.c                  |   67 +
drivers/gpu/drm/xe/Kconfig.profile            |   40 +
drivers/gpu/drm/xe/tests/xe_bo.c              |    8 +-
drivers/gpu/drm/xe/tests/xe_dma_buf.c         |    2 +-
drivers/gpu/drm/xe/tests/xe_migrate.c         |   12 +-
drivers/gpu/drm/xe/xe_bo.c                    | 1699 ++++++++++++++++-
drivers/gpu/drm/xe/xe_bo.h                    |   15 +-
drivers/gpu/drm/xe/xe_bo_types.h              |    6 +
drivers/gpu/drm/xe/xe_debugfs.c               |   26 +
drivers/gpu/drm/xe/xe_device.c                |   41 +
drivers/gpu/drm/xe/xe_device_types.h          |   57 +
drivers/gpu/drm/xe/xe_dma_buf.c               |    2 +-
drivers/gpu/drm/xe/xe_ggtt.c                  |    2 +-
drivers/gpu/drm/xe/xe_gt_stats.c              |    5 +
drivers/gpu/drm/xe/xe_gt_stats_types.h        |   17 +
drivers/gpu/drm/xe/xe_migrate.c               |  531 +++++-
drivers/gpu/drm/xe/xe_migrate.h               |   17 +
drivers/gpu/drm/xe/xe_pt.c                    |    2 +-
drivers/gpu/drm/xe/xe_res_cursor.h            |   56 +-
drivers/gpu/drm/xe/xe_svm.c                   |   44 +-
drivers/gpu/drm/xe/xe_svm.h                   |    4 +-
drivers/gpu/drm/xe/xe_trace_bo.h              |   90 +
drivers/gpu/drm/xe/xe_userptr.c               |   63 +
drivers/gpu/drm/xe/xe_userptr.h               |    1 +
drivers/gpu/drm/xe/xe_vm.c                    |  149 +-
drivers/gpu/drm/xe/xe_vm.h                    |    5 +
include/drm/drm_gpusvm.h                      |   19 +
include/drm/ttm/ttm_bo.h                      |   64 +
include/drm/ttm/ttm_pool.h                    |   46 +
include/drm/ttm/ttm_tt.h                      |   43 +-
42 files changed, 4179 insertions(+), 190 deletions(-)
Signature · 1 line
2.34.1