Skip to contentSkip to conversation

drm/amdgpu: fix PTs flush race with pending TLB fence in vm_fini

PATCH In review Prike Liang 3 participants 3 messages Mar 25 – Jul 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: fix PTs flush race with pending TLB fence in vm_fini

amdgpu_vm_fini() relies on vm->last_tlb_flush to wait for TLB activity to complete before calling amdgpu_vm_pt_free_root(). Because vm->last_tlb_flush tracks only the vm commit fence and never the TLB fence itself, so fini() may proceed to free page tables while the TLB fence work item is still running amdgpu_gmc_flush_gpu_tlb_pasid().

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -0
index 937a6dd3a4b5..53d0ac8bf98f 100644
@@ -1093,6 +1093,13 @@ 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);
 
+		/*
+		 * Update last_tlb_flush to the TLB fence so that
+		 * amdgpu_vm_fini() waits for the actual TLB flush to
+		 * complete, not just its commit fence.
+		 */
+		dma_fence_put(vm->last_tlb_flush);
+		vm->last_tlb_flush = 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);
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 5 lines · Prike Liang

amdgpu_vm_fini() relies on vm->last_tlb_flush to wait for TLB activity to complete before calling amdgpu_vm_pt_free_root(). Because vm->last_tlb_flush tracks only the vm commit fence and never the TLB fence itself, so fini() may proceed to free page tables while the TLB fence work item is still running amdgpu_gmc_flush_gpu_tlb_pasid().

That is not correct.

The PDs/PTs can be freed without waiting for the TLB flush to complete because dma_resv_add_fence() adds the fence to the PDs/PTs and make sure that their backing store stays around until that is finished.

The reason why we have vm->last_tlb_flush is to track the SDMA job which will trigger the TLB flush callback for kernel queues.

And your patch here breaks exactly that, so absolutely clear NAK from my side for this.

Regards,
Christian.

Quoted 24 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -0
index 937a6dd3a4b5..53d0ac8bf98f 100644
@@ -1093,6 +1093,13 @@ 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);
 
+		/*
+		 * Update last_tlb_flush to the TLB fence so that
+		 * amdgpu_vm_fini() waits for the actual TLB flush to
+		 * complete, not just its commit fence.
+		 */
+		dma_fence_put(vm->last_tlb_flush);
+		vm->last_tlb_flush = 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);
#2 lore raw

Hi all,

Not a patch review, just a field data point on this fd-close/VM-teardown race, since the discussion here (and on Vitaly's "wait for HW fences before VM teardown on fd close") turned on whether dma_resv/TTM already keeps the PDs/PTs alive long enough on kernel queues.

On GFX11 it does not appear to, in practice.

Hardware / software:
- Navi 32 (RDNA3, GFX11), PCI 1002:747e, sdma_v6_0 / mes_v11_0
- Linux 7.1.4, Mesa 26.1.3, amdgpu defaults (vm_update_mode=-1)

In normal desktop use (closing games/browsers) I hit, ~once a day:

amdgpu: ring sdma0 timeout, signaled seq=354165, emitted seq=354166
amdgpu: Starting sdma0 ring reset -> Ring sdma0 reset succeeded
amdgpu: [drm] device wedged, but no recovery needed

i.e. exactly one in-flight SDMA job never signals, and it is always immediately preceded by VM-teardown for an exiting process:

amdgpu: VM memory stats for proc <steam/Xwayland/vesktop/...> is
non-zero when fini

Building Vitaly's IGT amd_close_race and running only the mildest subtest (close-race-low, 2 racer threads) reproduces it deterministically within seconds, and on this GFX11 part it escalates hard:

[gfxhub] page fault (src_id:0 ring:64 vmid:7 pasid:2931) GCVM_L2_PROTECTION_FAULT_STATUS:0x00700881

     Faulty UTCL2 client ID: CPF (0x4)
     PERMISSION_FAULTS: 0x8

... GPU reset succeeded / VRAM is lost due to GPU reset! (x6)
... MES failed to respond to msg=RESET / REMOVE_QUEUE (x20)
reset via MES failed and try pipe reset -110
Ring gfx_0.0.0 reset failed

The GPU only self-recovered after the 12th reset; a couple more rounds and it would have needed a power cycle. The CPF client + PERMISSION_FAULTS look like the GPU walking PTEs that teardown has already invalidated - which is the point in question here.

Full details, dmesg, environment and a captured devcoredump are in:
https://gitlab.freedesktop.org/drm/amd/-/work_items/5512

Happy to run further instrumentation (tracepoints, IB dumps, specific debugfs) on this hardware if it helps narrow down why the fence doesn't gate teardown on GFX11.

This investigation, reproduction and write-up were prepared with AI assistance.

Thanks,
Liav