Skip to contentSkip to conversation

Implement clear VRAM on free

PATCH Superseded Matthew Brost 2 participants 6 messages Jun 23, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Matthew Brost

author maintainer
#0 lore raw

v2 cover Implement clear VRAM on free

Do not use a delayed worker to destroy BO VMAs. Drop the BO argument from xe_migrate_clear, as this function can be called with a BO that has a zero reference count. Finally, implement VRAM clearing on free.

Avoiding a delayed worker for BO VMA destruction ensures timely finalization of the BO during unbind, which should improve the performance of clear-on-free. This change is therefore included in this series.

Matt

Matthew Brost (4):
drm/xe: Only use delayed worker to destroy userptr VMAs drm/xe: Drop BO argument from xe_migrate_clear drm/xe: Add xe_bo_tt_has_data helper
drm/xe: Implement clear VRAM on free

 drivers/gpu/drm/xe/xe_bo.c           | 75 ++++++++++++++++++++++++--
 drivers/gpu/drm/xe/xe_bo.h           |  2 +
 drivers/gpu/drm/xe/xe_migrate.c      | 50 +++++++++++------
 drivers/gpu/drm/xe/xe_migrate.h      |  9 +++-
 drivers/gpu/drm/xe/xe_res_cursor.h   | 26 +++++++++
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c |  6 ++-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.h |  6 +++
 drivers/gpu/drm/xe/xe_vm.c           | 80 +++++++++++++++-------------
 drivers/gpu/drm/xe/xe_vm_types.h     | 13 +++--
 9 files changed, 198 insertions(+), 69 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v2 1/4 drm/xe: Only use delayed worker to destroy userptr VMAs

TTM has a delayed BO destruction mechanism based on dma-resv, which eliminates the need for the delayed worker to destroy VMAs containing BOs. However, userptr still requires this mechanism because DMA mappings and notifiers cannot be removed until the GPU bindings are gone. Therefore, move the delayed worker responsible for destroying userptr VMAs to the userptr code and bypass this process for VMAs containing a BO.

---
 drivers/gpu/drm/xe/xe_vm.c       | 80 +++++++++++++++++---------------
 drivers/gpu/drm/xe/xe_vm_types.h | 13 +++---
 2 files changed, 48 insertions(+), 45 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +42 -38
index 04d1a43b81e3..9309a5956f00 100644
@@ -1270,54 +1270,47 @@ static struct xe_vma *xe_vma_create(struct xe_vm *vm,
 	return vma;
 }
 
-static void xe_vma_destroy_late(struct xe_vma *vma)
+static void xe_vma_userptr_destroy_late(struct xe_userptr_vma *uvma)
 {
+	struct xe_vma *vma = &uvma->vma;
+	struct xe_userptr *userptr = &uvma->userptr;
 	struct xe_vm *vm = xe_vma_vm(vma);
 
-	if (vma->ufence) {
-		xe_sync_ufence_put(vma->ufence);
-		vma->ufence = NULL;
-	}
-
-	if (xe_vma_is_userptr(vma)) {
-		struct xe_userptr_vma *uvma = to_userptr_vma(vma);
-		struct xe_userptr *userptr = &uvma->userptr;
+	xe_assert(vm->xe, xe_vma_is_userptr(vma));
 
-		if (userptr->sg)
-			xe_hmm_userptr_free_sg(uvma);
+	if (userptr->sg)
+		xe_hmm_userptr_free_sg(uvma);
 
-		/*
-		 * Since userptr pages are not pinned, we can't remove
-		 * the notifier until we're sure the GPU is not accessing
-		 * them anymore
-		 */
-		mmu_interval_notifier_remove(&userptr->notifier);
-		mutex_destroy(&userptr->unmap_mutex);
-		xe_vm_put(vm);
-	} else if (xe_vma_is_null(vma) || xe_vma_is_cpu_addr_mirror(vma)) {
-		xe_vm_put(vm);
-	} else {
-		xe_bo_put(xe_vma_bo(vma));
-	}
+	/*
+	 * Since userptr pages are not pinned, we can't remove
+	 * the notifier until we're sure the GPU is not accessing
+	 * them anymore
+	 */
+	mmu_interval_notifier_remove(&userptr->notifier);
+	mutex_destroy(&userptr->unmap_mutex);
+	xe_vm_put(vm);
 
 	xe_vma_free(vma);
 }
 
-static void vma_destroy_work_func(struct work_struct *w)
+static void userptr_destroy_work_func(struct work_struct *w)
 {
-	struct xe_vma *vma =
-		container_of(w, struct xe_vma, destroy_work);
+	struct xe_userptr *userptr =
+		container_of(w, typeof(*userptr), destroy_work);
+	struct xe_userptr_vma *uvma = container_of(userptr, typeof(*uvma),
+						   userptr);
 
-	xe_vma_destroy_late(vma);
+	xe_vma_userptr_destroy_late(uvma);
 }
 
-static void vma_destroy_cb(struct dma_fence *fence,
-			   struct dma_fence_cb *cb)
+static void userptr_destroy_cb(struct dma_fence *fence,
+			       struct dma_fence_cb *cb)
 {
-	struct xe_vma *vma = container_of(cb, struct xe_vma, destroy_cb);
+	struct xe_userptr *userptr =
+		container_of(cb, typeof(*userptr), destroy_cb);
 
-	INIT_WORK(&vma->destroy_work, vma_destroy_work_func);
-	queue_work(system_unbound_wq, &vma->destroy_work);
+	INIT_WORK(&userptr->destroy_work, userptr_destroy_work_func);
+	queue_work(system_unbound_wq, &userptr->destroy_work);
 }
 
 static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence)
@@ -1327,6 +1320,11 @@ static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence)
 	lockdep_assert_held_write(&vm->lock);
 	xe_assert(vm->xe, list_empty(&vma->combined_links.destroy));
 
+	if (vma->ufence) {
+		xe_sync_ufence_put(vma->ufence);
+		vma->ufence = NULL;
+	}
+
 	if (xe_vma_is_userptr(vma)) {
 		xe_assert(vm->xe, vma->gpuva.flags & XE_VMA_DESTROYED);
 
@@ -1338,19 +1336,25 @@ static void xe_vma_destroy(struct xe_vma *vma, struct dma_fence *fence)
 		xe_bo_assert_held(xe_vma_bo(vma));
 
 		drm_gpuva_unlink(&vma->gpuva);
+		xe_bo_put(xe_vma_bo(vma));
+	} else {
+		xe_vm_put(vm);
 	}
 
 	xe_vm_assert_held(vm);
-	if (fence) {
-		int ret = dma_fence_add_callback(fence, &vma->destroy_cb,
-						 vma_destroy_cb);
+	if (fence && xe_vma_is_userptr(vma)) {
+		struct xe_userptr *userptr = &to_userptr_vma(vma)->userptr;
+		int ret = dma_fence_add_callback(fence, &userptr->destroy_cb,
+						 userptr_destroy_cb);
 
 		if (ret) {
 			XE_WARN_ON(ret != -ENOENT);
-			xe_vma_destroy_late(vma);
+			xe_vma_userptr_destroy_late(to_userptr_vma(vma));
 		}
+	} else if (xe_vma_is_userptr(vma)) {
+		xe_vma_userptr_destroy_late(to_userptr_vma(vma));
 	} else {
-		xe_vma_destroy_late(vma);
+		xe_vma_free(vma);
 	}
 }
 
drivers/gpu/drm/xe/xe_vm_types.h +6 -7
index bed6088e1bb3..cb4b9fbcf75d 100644
@@ -64,6 +64,12 @@ struct xe_userptr {
 	unsigned long notifier_seq;
 	/** @unmap_mutex: Mutex protecting dma-unmapping */
 	struct mutex unmap_mutex;
+	union {
+		/** @destroy_cb: callback to destroy usertpr when unbind job is done */
+		struct dma_fence_cb destroy_cb;
+		/** @destroy_work: worker to destroy this usertpr */
+		struct work_struct destroy_work;
+	};
 	/**
 	 * @initial_bind: user pointer has been bound at least once.
 	 * write: vm->userptr.notifier_lock in read mode and vm->resv held.
@@ -93,13 +99,6 @@ struct xe_vma {
 		struct list_head destroy;
 	} combined_links;
 
-	union {
-		/** @destroy_cb: callback to destroy VMA when unbind job is done */
-		struct dma_fence_cb destroy_cb;
-		/** @destroy_work: worker to destroy this BO */
-		struct work_struct destroy_work;
-	};
-
 	/**
 	 * @tile_invalidated: Tile mask of binding are invalidated for this VMA.
 	 * protected by BO's resv and for userptrs, vm->userptr.notifier_lock in
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH v2 2/4 drm/xe: Drop BO argument from xe_migrate_clear

In reply to Matthew Brost

xe_migrate_clear() can be called when a BO's reference count is zero. As a result, having a BO passed to xe_migrate_clear() can lead to unsafe behavior (e.g. attempting to take a BO reference). Avoid this issue by removing the BO from xe_migrate_clear()'s argument list.

---
 drivers/gpu/drm/xe/xe_bo.c      |  6 +++++-
 drivers/gpu/drm/xe/xe_migrate.c | 21 ++++++++++++---------
 drivers/gpu/drm/xe/xe_migrate.h |  8 ++++++--
 3 files changed, 23 insertions(+), 12 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +5 -1
index 4e39188a021a..c93381fa4858 100644
@@ -908,6 +908,8 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 	}
 
 	if (move_lacks_source) {
+		struct sg_table *sgt = mem_type_is_vram(new_mem->mem_type) ?
+			NULL : xe_bo_sg(bo);
 		u32 flags = 0;
 
 		if (mem_type_is_vram(new_mem->mem_type))
@@ -915,7 +917,9 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		else if (handle_system_ccs)
 			flags |= XE_MIGRATE_CLEAR_FLAG_CCS_DATA;
 
-		fence = xe_migrate_clear(migrate, bo, new_mem, flags);
+		fence = xe_migrate_clear(migrate, new_mem, ttm_bo->base.resv,
+					 sgt, bo->size, flags,
+					 &bo->ccs_cleared);
 	} else {
 		fence = xe_migrate_copy(migrate, bo, bo, old_mem, new_mem,
 					handle_system_ccs);
drivers/gpu/drm/xe/xe_migrate.c +12 -9
index 8f8e9fdfb2a8..18030f9613ae 100644
@@ -1039,23 +1039,27 @@ static void emit_clear(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs,
 /**
  * xe_migrate_clear() - Copy content of TTM resources.
  * @m: The migration context.
- * @bo: The buffer object @dst is currently bound to.
  * @dst: The dst TTM resource to be cleared.
+ * @resv: dma-resv for the TTM resource to be cleared
+ * @sgt: Scatter gather table if @dst is not in VRAM, NULL otherwise
+ * @size: Size of the TTM resource to be cleared.
  * @clear_flags: flags to specify which data to clear: CCS, BO, or both.
+ * @ccs_cleared: CCS has been cleared, passed by reference, set in this function
  *
  * Clear the contents of @dst to zero when XE_MIGRATE_CLEAR_FLAG_BO_DATA is set.
  * On flat CCS devices, the CCS metadata is cleared to zero with XE_MIGRATE_CLEAR_FLAG_CCS_DATA.
  * Set XE_MIGRATE_CLEAR_FLAG_FULL to clear bo as well as CCS metadata.
- * TODO: Eliminate the @bo argument.
  *
  * Return: Pointer to a dma_fence representing the last clear batch, or
  * an error pointer on failure. If there is a failure, any clear operation
  * started by the function call has been synced.
  */
 struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
-				   struct xe_bo *bo,
 				   struct ttm_resource *dst,
-				   u32 clear_flags)
+				   struct dma_resv *resv,
+				   struct sg_table *sgt,
+				   u64 size, u32 clear_flags,
+				   bool *ccs_cleared)
 {
 	bool clear_vram = mem_type_is_vram(dst->mem_type);
 	bool clear_bo_data = XE_MIGRATE_CLEAR_FLAG_BO_DATA & clear_flags;
@@ -1064,7 +1068,6 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 	struct xe_device *xe = gt_to_xe(gt);
 	bool clear_only_system_ccs = false;
 	struct dma_fence *fence = NULL;
-	u64 size = bo->size;
 	struct xe_res_cursor src_it;
 	struct ttm_resource *src = dst;
 	int err;
@@ -1076,9 +1079,9 @@ 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, bo->size, &src_it);
+		xe_res_first_sg(sgt, 0, size, &src_it);
 	else
-		xe_res_first(src, 0, bo->size, &src_it);
+		xe_res_first(src, 0, size, &src_it);
 
 	while (size) {
 		u64 clear_L0_ofs;
@@ -1153,7 +1156,7 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 			 * fences, which are always tracked as
 			 * DMA_RESV_USAGE_KERNEL.
 			 */
-			err = xe_sched_job_add_deps(job, bo->ttm.base.resv,
+			err = xe_sched_job_add_deps(job, resv,
 						    DMA_RESV_USAGE_KERNEL);
 			if (err)
 				goto err_job;
@@ -1188,7 +1191,7 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 	}
 
 	if (clear_ccs)
-		bo->ccs_cleared = true;
+		*ccs_cleared = true;
 
 	return fence;
 }
drivers/gpu/drm/xe/xe_migrate.h +6 -2
index fb9839c1bae0..04b3cd86ba90 100644
@@ -9,7 +9,9 @@
 #include <linux/types.h>
 
 struct dma_fence;
+struct dma_resv;
 struct iosys_map;
+struct sg_table;
 struct ttm_resource;
 
 struct xe_bo;
@@ -121,9 +123,11 @@ int xe_migrate_access_memory(struct xe_migrate *m, struct xe_bo *bo,
 #define XE_MIGRATE_CLEAR_FLAG_FULL	(XE_MIGRATE_CLEAR_FLAG_BO_DATA | \
 					XE_MIGRATE_CLEAR_FLAG_CCS_DATA)
 struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
-				   struct xe_bo *bo,
 				   struct ttm_resource *dst,
-				   u32 clear_flags);
+				   struct dma_resv *resv,
+				   struct sg_table *sgt,
+				   u64 size, u32 clear_flags,
+				   bool *ccs_cleared);
 
 struct xe_vm *xe_migrate_get_vm(struct xe_migrate *m);
 
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH v2 3/4 drm/xe: Add xe_bo_tt_has_data helper

In reply to Matthew Brost

Add xe_bo_tt_has_data helper which determines if a BO has TT data (i.e., a BO has SRAM or swap backing store).

---
 drivers/gpu/drm/xe/xe_bo.c | 21 ++++++++++++++++++---
 drivers/gpu/drm/xe/xe_bo.h |  2 ++
 2 files changed, 20 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +18 -3
index c93381fa4858..7a412121477e 100644
@@ -766,6 +766,23 @@ static int xe_bo_move_notify(struct xe_bo *bo,
 	return 0;
 }
 
+/**
+ * xe_bo_tt_has_data() - Xe BO has TT data (i.e., Xe BO has SRAM or swap backing
+ * store)
+ *
+ * Helper to determine if a BO has TT data. Useful to determine if copies or
+ * clears are needed on BO when it is moving.
+ *
+ * Return: True if a BO has TT data, False otherwise
+ */
+bool xe_bo_tt_has_data(struct xe_bo *bo)
+{
+	struct ttm_tt *ttm = bo->ttm.ttm;
+
+	return ttm && (ttm_tt_is_populated(ttm) ||
+		       (ttm->page_flags & TTM_TT_FLAG_SWAPPED));
+}
+
 static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		      struct ttm_operation_ctx *ctx,
 		      struct ttm_resource *new_mem,
@@ -801,9 +818,7 @@ static int xe_bo_move(struct ttm_buffer_object *ttm_bo, bool evict,
 		return ret;
 	}
 
-	tt_has_data = ttm && (ttm_tt_is_populated(ttm) ||
-			      (ttm->page_flags & TTM_TT_FLAG_SWAPPED));
-
+	tt_has_data = xe_bo_tt_has_data(bo);
 	move_lacks_source = !old_mem || (handle_system_ccs ? (!bo->ccs_cleared) :
 					 (!mem_type_is_vram(old_mem_type) && !tt_has_data));
 
drivers/gpu/drm/xe/xe_bo.h +2 -0
index 02ada1fb8a23..150e98ce6fac 100644
@@ -374,6 +374,8 @@ void xe_bo_dev_fini(struct xe_bo_dev *bo_device);
 
 struct sg_table *xe_bo_sg(struct xe_bo *bo);
 
+bool xe_bo_tt_has_data(struct xe_bo *bo);
+
 /*
  * xe_sg_segment_size() - Provides upper limit for sg segment size.
  * @dev: device pointer
Signature · 1 line
2.34.1
author maintainer
#4 lore raw

PATCH v2 4/4 drm/xe: Implement clear VRAM on free

In reply to Matthew Brost

Clearing on free should hide latency of BO clears on new user BO allocations.

Implemented via calling xe_migrate_clear in release notify and updating iterator in xe_migrate_clear to skip cleared buddy blocks. Only user BOs cleared in release notify as kernel BOs could still be in use (e.g., PT BOs need to wait for dma-resv to be idle).

v2:
- Update to new xe_migrate_clear arguments (Thomas)
- Wait on BOOKKEEP slots for clear onn free (Thomas)
- s/XE_MIGRATE_CLEAR_NON_DIRTY/XE_MIGRATE_CLEAR_FLAG_ON_FREE
- Do not requested clear memory in VRAM manager if BO as TT data (Auld)

---
 drivers/gpu/drm/xe/xe_bo.c           | 48 ++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_migrate.c      | 31 ++++++++++++------
 drivers/gpu/drm/xe/xe_migrate.h      |  1 +
 drivers/gpu/drm/xe/xe_res_cursor.h   | 26 +++++++++++++++
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c |  6 +++-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.h |  6 ++++
 6 files changed, 108 insertions(+), 10 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +48 -0
index 7a412121477e..900f67deef9d 100644
@@ -1453,6 +1453,52 @@ static bool xe_ttm_bo_lock_in_destructor(struct ttm_buffer_object *ttm_bo)
 	return locked;
 }
 
+static void xe_ttm_bo_release_clear(struct ttm_buffer_object *ttm_bo)
+{
+	struct xe_device *xe = ttm_to_xe_device(ttm_bo->bdev);
+	struct xe_bo *bo = ttm_to_xe_bo(ttm_bo);
+	struct dma_fence *fence;
+	int err, idx;
+
+	xe_bo_assert_held(ttm_to_xe_bo(ttm_bo));
+
+	if (ttm_bo->type != ttm_bo_type_device)
+		return;
+
+	if (xe_device_wedged(xe))
+		return;
+
+	if (!ttm_bo->resource || !mem_type_is_vram(ttm_bo->resource->mem_type))
+		return;
+
+	if (!drm_dev_enter(&xe->drm, &idx))
+		return;
+
+	if (!xe_pm_runtime_get_if_active(xe))
+		goto unbind;
+
+	err = dma_resv_reserve_fences(&ttm_bo->base._resv, 1);
+	if (err)
+		goto put_pm;
+
+	fence = xe_migrate_clear(mem_type_to_migrate(xe, ttm_bo->resource->mem_type),
+				 ttm_bo->resource, &ttm_bo->base._resv, NULL,
+				 bo->size, XE_MIGRATE_CLEAR_FLAG_FULL |
+				 XE_MIGRATE_CLEAR_FLAG_ON_FREE, &bo->ccs_cleared);
+	if (XE_WARN_ON(IS_ERR(fence)))
+		goto put_pm;
+
+	xe_ttm_vram_mgr_resource_set_cleared(ttm_bo->resource);
+	dma_resv_add_fence(&ttm_bo->base._resv, fence,
+			   DMA_RESV_USAGE_KERNEL);
+	dma_fence_put(fence);
+
+put_pm:
+	xe_pm_runtime_put(xe);
+unbind:
+	drm_dev_exit(idx);
+}
+
 static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo)
 {
 	struct dma_resv_iter cursor;
@@ -1497,6 +1543,8 @@ static void xe_ttm_bo_release_notify(struct ttm_buffer_object *ttm_bo)
 	}
 	dma_fence_put(replacement);
 
+	xe_ttm_bo_release_clear(ttm_bo);
+
 	dma_resv_unlock(ttm_bo->base.resv);
 }
 
drivers/gpu/drm/xe/xe_migrate.c +22 -9
index 18030f9613ae..7620f4eb8fb3 100644
@@ -1067,7 +1067,7 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 	struct xe_gt *gt = m->tile->primary_gt;
 	struct xe_device *xe = gt_to_xe(gt);
 	bool clear_only_system_ccs = false;
-	struct dma_fence *fence = NULL;
+	struct dma_fence *fence = dma_fence_get_stub();
 	struct xe_res_cursor src_it;
 	struct ttm_resource *src = dst;
 	int err;
@@ -1078,10 +1078,13 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 	if (!clear_bo_data && clear_ccs && !IS_DGFX(xe))
 		clear_only_system_ccs = true;
 
-	if (!clear_vram)
+	if (!clear_vram) {
 		xe_res_first_sg(sgt, 0, size, &src_it);
-	else
+	} else {
 		xe_res_first(src, 0, size, &src_it);
+		if (!(clear_flags & XE_MIGRATE_CLEAR_FLAG_ON_FREE))
+			size -= xe_res_next_dirty(&src_it);
+	}
 
 	while (size) {
 		u64 clear_L0_ofs;
@@ -1128,6 +1131,9 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 			emit_pte(m, bb, clear_L0_pt, clear_vram, clear_only_system_ccs,
 				 &src_it, clear_L0, dst);
 
+		if (clear_vram && !(clear_flags & XE_MIGRATE_CLEAR_FLAG_ON_FREE))
+			size -= xe_res_next_dirty(&src_it);
+
 		bb->cs[bb->len++] = MI_BATCH_BUFFER_END;
 		update_idx = bb->len;
 
@@ -1149,15 +1155,22 @@ struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
 		}
 
 		xe_sched_job_add_migrate_flush(job, flush_flags);
-		if (!fence) {
+		if (fence == dma_fence_get_stub()) {
+			enum dma_resv_usage usage =
+			       (clear_flags & XE_MIGRATE_CLEAR_FLAG_ON_FREE) ?
+			       DMA_RESV_USAGE_BOOKKEEP : DMA_RESV_USAGE_KERNEL;
+
 			/*
-			 * There can't be anything userspace related at this
-			 * point, so we just need to respect any potential move
-			 * fences, which are always tracked as
+			 * For initial clears, there can't be anything userspace
+			 * related at this point, so we just need to respect any
+			 * potential move fences, which are always tracked as
 			 * DMA_RESV_USAGE_KERNEL.
+			 *
+			 * For clear on free need to respect all fences as
+			 * memory could still be in use by the GPU which is
+			 * tracked in DMA_RESV_USAGE_BOOKKEEP.
 			 */
-			err = xe_sched_job_add_deps(job, resv,
-						    DMA_RESV_USAGE_KERNEL);
+			err = xe_sched_job_add_deps(job, resv, usage);
 			if (err)
 				goto err_job;
 		}
drivers/gpu/drm/xe/xe_migrate.h +1 -0
index 04b3cd86ba90..104c61812ba1 100644
@@ -120,6 +120,7 @@ int xe_migrate_access_memory(struct xe_migrate *m, struct xe_bo *bo,
 
 #define XE_MIGRATE_CLEAR_FLAG_BO_DATA		BIT(0)
 #define XE_MIGRATE_CLEAR_FLAG_CCS_DATA		BIT(1)
+#define XE_MIGRATE_CLEAR_FLAG_ON_FREE		BIT(2)
 #define XE_MIGRATE_CLEAR_FLAG_FULL	(XE_MIGRATE_CLEAR_FLAG_BO_DATA | \
 					XE_MIGRATE_CLEAR_FLAG_CCS_DATA)
 struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
drivers/gpu/drm/xe/xe_res_cursor.h +26 -0
index d1a403cfb628..630082e809ba 100644
@@ -315,6 +315,32 @@ static inline void xe_res_next(struct xe_res_cursor *cur, u64 size)
 	}
 }
 
+/**
+ * xe_res_next_dirty - advance the cursor to next dirty buddy block
+ *
+ * @cur: the cursor to advance
+ *
+ * Move the cursor until dirty buddy block is found.
+ *
+ * Return: Number of bytes cursor has been advanced
+ */
+static inline u64 xe_res_next_dirty(struct xe_res_cursor *cur)
+{
+	struct drm_buddy_block *block = cur->node;
+	u64 bytes = 0;
+
+	XE_WARN_ON(cur->mem_type != XE_PL_VRAM0 &&
+		   cur->mem_type != XE_PL_VRAM1);
+
+	while (cur->remaining && drm_buddy_block_is_clear(block)) {
+		bytes += cur->size;
+		xe_res_next(cur, cur->size);
+		block = cur->node;
+	}
+
+	return bytes;
+}
+
 /**
  * xe_res_dma - return dma address of cursor at current position
  *
drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +5 -1
index 9e375a40aee9..838739e47815 100644
@@ -84,6 +84,10 @@ static int xe_ttm_vram_mgr_new(struct ttm_resource_manager *man,
 	if (place->fpfn || lpfn != man->size >> PAGE_SHIFT)
 		vres->flags |= DRM_BUDDY_RANGE_ALLOCATION;
 
+	if (tbo->type == ttm_bo_type_device &&
+	    !xe_bo_tt_has_data(ttm_to_xe_bo(tbo)))
+		vres->flags |= DRM_BUDDY_CLEAR_ALLOCATION;
+
 	if (WARN_ON(!vres->base.size)) {
 		err = -EINVAL;
 		goto error_fini;
@@ -187,7 +191,7 @@ static void xe_ttm_vram_mgr_del(struct ttm_resource_manager *man,
 	struct drm_buddy *mm = &mgr->mm;
 
 	mutex_lock(&mgr->lock);
-	drm_buddy_free_list(mm, &vres->blocks, 0);
+	drm_buddy_free_list(mm, &vres->blocks, vres->flags);
 	mgr->visible_avail += vres->used_visible_size;
 	mutex_unlock(&mgr->lock);
 
drivers/gpu/drm/xe/xe_ttm_vram_mgr.h +6 -0
index cc76050e376d..dfc0e6890b3c 100644
@@ -36,6 +36,12 @@ to_xe_ttm_vram_mgr_resource(struct ttm_resource *res)
 	return container_of(res, struct xe_ttm_vram_mgr_resource, base);
 }
 
+static inline void
+xe_ttm_vram_mgr_resource_set_cleared(struct ttm_resource *res)
+{
+	to_xe_ttm_vram_mgr_resource(res)->flags |= DRM_BUDDY_CLEARED;
+}
+
 static inline struct xe_ttm_vram_mgr *
 to_xe_ttm_vram_mgr(struct ttm_resource_manager *man)
 {
Signature · 1 line
2.34.1
#5 lore raw

✗ CI.KUnit: failure for Implement clear VRAM on free

In reply to Matthew Brost
== Series Details ==

== Series Details ==

Series: Implement clear VRAM on free
URL : https://patchwork.freedesktop.org/series/150635/
State : failure

== Summary ==

+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig
ERROR:root:In file included from ../drivers/gpu/drm/xe/xe_bo.c:3204:
../drivers/gpu/drm/xe/tests/xe_bo.c: In function ‘ccs_test_migrate’:
../drivers/gpu/drm/xe/tests/xe_bo.c:46:57: error: passing argument 2 of ‘xe_migrate_clear’ from incompatible pointer type [-Werror=incompatible-pointer-types]

   46 |                 fence = xe_migrate_clear(tile->migrate, bo, bo->ttm.resource,
      |                                                         ^~
      |                                                         |
      |                                                         struct xe_bo *
In file included from ../drivers/gpu/drm/xe/xe_bo.c:28:
../drivers/gpu/drm/xe/xe_migrate.h:127:57: note: expected ‘struct ttm_resource *’ but argument is of type ‘struct xe_bo *’
  127 |                                    struct ttm_resource *dst,
      |                                    ~~~~~~~~~~~~~~~~~~~~~^~~
../drivers/gpu/drm/xe/tests/xe_bo.c:46:68: error: passing argument 3 of ‘xe_migrate_clear’ from incompatible pointer type [-Werror=incompatible-pointer-types]
   46 |                 fence = xe_migrate_clear(tile->migrate, bo, bo->ttm.resource,
      |                                                             ~~~~~~~^~~~~~~~~
      |                                                                    |
      |                                                                    struct ttm_resource *
../drivers/gpu/drm/xe/xe_migrate.h:128:53: note: expected ‘struct dma_resv *’ but argument is of type ‘struct ttm_resource *’
  128 |                                    struct dma_resv *resv,
      |                                    ~~~~~~~~~~~~~~~~~^~~~
../drivers/gpu/drm/xe/xe_migrate.h:124:41: warning: passing argument 4 of ‘xe_migrate_clear’ makes pointer from integer without a cast [-Wint-conversion]
  124 | #define XE_MIGRATE_CLEAR_FLAG_FULL      (XE_MIGRATE_CLEAR_FLAG_BO_DATA | \
      |                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                         |
      |                                         long unsigned int
  125 |                                         XE_MIGRATE_CLEAR_FLAG_CCS_DATA)
      |                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/tests/xe_bo.c:47:42: note: in expansion of macro ‘XE_MIGRATE_CLEAR_FLAG_FULL’
   47 |                                          XE_MIGRATE_CLEAR_FLAG_FULL);
      |                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/xe_migrate.h:129:53: note: expected ‘struct sg_table *’ but argument is of type ‘long unsigned int’
  129 |                                    struct sg_table *sgt,
      |                                    ~~~~~~~~~~~~~~~~~^~~
../drivers/gpu/drm/xe/tests/xe_bo.c:46:25: error: too few arguments to function ‘xe_migrate_clear’
   46 |                 fence = xe_migrate_clear(tile->migrate, bo, bo->ttm.resource,
      |                         ^~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/xe_migrate.h:126:19: note: declared here
  126 | struct dma_fence *xe_migrate_clear(struct xe_migrate *m,
      |                   ^~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors
make[7]: *** [../scripts/Makefile.build:287: drivers/gpu/drm/xe/xe_bo.o] Error 1 make[7]: *** Waiting for unfinished jobs....
make[6]: *** [../scripts/Makefile.build:554: drivers/gpu/drm/xe] Error 2 make[6]: *** Waiting for unfinished jobs....
make[5]: *** [../scripts/Makefile.build:554: drivers/gpu/drm] Error 2 make[4]: *** [../scripts/Makefile.build:554: drivers/gpu] Error 2 make[4]: *** Waiting for unfinished jobs....
make[3]: *** [../scripts/Makefile.build:554: drivers] Error 2 make[3]: *** Waiting for unfinished jobs....
make[2]: *** [/kernel/Makefile:2003: .] Error 2
make[1]: *** [/kernel/Makefile:248: __sub-make] Error 2
make: *** [Makefile:248: __sub-make] Error 2

[14:51:23] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[14:51:30] 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
+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel