All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdgpu: wait for tlb flush before freeing BO
@ 2026-10-14  2:21 Benjamin Cheng
  2026-10-14  7:17 ` Christian König
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Benjamin Cheng @ 2026-10-14  2:21 UTC (permalink / raw)
  To: Alex Deucher, Christian König, amd-gfx; +Cc: Sunil Khatri, Benjamin Cheng

On gem object close, we previously only attached the fence of its own
clear_freed() call to the BO's resv. If an unmap of this BO happened
prior to the close call, clear_freed() would be invoked, leaving
vm->freed empty, and no meaningful fence to be attached to the resv on
close. Because of that the memory may be freed before the TLB flush is
complete, and give a running userq potential access to a stale
address translation.

Fix it by tracking the last TLB fence of each VM and attaching it to the
BO on close.

Fixes: a1d201e16940 ("drm/amdgpu: enable GFX-V11 userqueue support")
Signed-off-by: Benjamin Cheng <benjamin.cheng@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 6 ++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 1 +
 3 files changed, 9 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
index 8dbd7bb77305..7484c52e5850 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -360,6 +360,8 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 
 	amdgpu_vm_bo_del(adev, bo_va);
 	amdgpu_vm_bo_update_shared(bo);
+	if (!dma_fence_is_signaled(vm->last_tlb_flush_complete))
+		amdgpu_bo_fence(bo, vm->last_tlb_flush_complete, true);
 	if (!amdgpu_vm_ready(vm))
 		goto out_unlock;
 
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index 7d3659ef11e5..c5411933a76c 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -1097,6 +1097,9 @@ amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params,
 	if (!params->unlocked && vm->need_tlb_fence) {
 		amdgpu_vm_tlb_fence_create(params->adev, vm, fence);
 
+		dma_fence_put(vm->last_tlb_flush_complete);
+		vm->last_tlb_flush_complete = dma_fence_get(*fence);
+
 		/* Makes sure no PD/PT is freed before the flush */
 		dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence,
 				   DMA_RESV_USAGE_BOOKKEEP);
@@ -2648,6 +2651,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	vm->last_update = dma_fence_get_stub();
 	vm->last_unlocked = dma_fence_get_stub();
 	vm->last_tlb_flush = dma_fence_get_stub();
+	vm->last_tlb_flush_complete = dma_fence_get_stub();
 	vm->generation = amdgpu_vm_generation(adev, NULL);
 
 	mutex_init(&vm->eviction_lock);
@@ -2693,6 +2697,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	dma_fence_put(vm->last_update);
 	dma_fence_put(vm->last_tlb_flush);
 	dma_fence_put(vm->last_unlocked);
+	dma_fence_put(vm->last_tlb_flush_complete);
 	ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move);
 	amdgpu_vm_fini_entities(vm);
 
@@ -2839,6 +2844,7 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 	dma_fence_lock_irqsave(vm->last_tlb_flush, flags);
 	dma_fence_unlock_irqrestore(vm->last_tlb_flush, flags);
 	dma_fence_put(vm->last_tlb_flush);
+	dma_fence_put(vm->last_tlb_flush_complete);
 
 	list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
 		if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) {
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
index ddcdc7256ef0..b9932c5e066f 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h
@@ -331,6 +331,7 @@ struct amdgpu_vm {
 	/* Last finished delayed update */
 	atomic64_t		tlb_seq;
 	struct dma_fence	*last_tlb_flush;
+	struct dma_fence	*last_tlb_flush_complete;
 	atomic64_t		kfd_last_flushed_seq;
 	uint64_t		tlb_fence_context;
 
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2026-11-12 17:22 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-14  2:21 [PATCH] drm/amdgpu: wait for tlb flush before freeing BO Benjamin Cheng
2026-10-14  7:17 ` Christian König
2026-10-14 12:46   ` Benjamin Cheng
2026-10-14 13:09     ` Christian König
2026-11-06 15:20 ` [PATCH v2] " Benjamin Cheng
2026-11-06 16:04   ` Christian König
2026-11-06 16:07     ` Benjamin Cheng
2026-11-09  9:58       ` Christian König
2026-11-09 14:57 ` [PATCH v3 1/2] " Benjamin Cheng
2026-11-09 14:57   ` [PATCH v3 2/2] drm/amdgpu/gem: use dma_resv_add_fence directly Benjamin Cheng
2026-11-09 15:00     ` Christian König
2026-11-09 15:02       ` Benjamin Cheng
2026-11-12 16:44 ` [PATCH 1/3] drm/amdgpu/vm: stop using amdgpu_bo_fence Benjamin Cheng
2026-11-12 16:44   ` [PATCH 2/3] drm/amdgpu/uvd: " Benjamin Cheng
2026-11-12 16:44   ` [PATCH 3/3] drm/amdgpu/object: get rid of amdgpu_bo_fence Benjamin Cheng
2026-11-12 16:52     ` sashiko-bot
2026-11-12 17:22   ` [PATCH 1/3] drm/amdgpu/vm: stop using amdgpu_bo_fence sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.