Skip to contentSkip to conversation

drm/amdgpu: wait for PT updates in amdgpu_vm_make_compute

PATCH New Amir Shetaia 2 participants 2 messages Dec 4

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

PATCH 1/1 drm/amdgpu: wait for PT updates in amdgpu_vm_make_compute

amdgpu_vm_make_compute() switches a VM to CPU page table updates when compute VMs use the CPU path (vm_update_mode, the default when all of VRAM is CPU visible). Before switching it waits for the last SDMA update/clear of the root PD with amdgpu_bo_sync_wait().

That helper goes through amdgpu_sync_resv(), which only iterates DMA_RESV_USAGE_READ fences. Since commit 4247084057cf ("drm/amdgpu: use DMA_RESV_USAGE_BOOKKEEP v2") page table updates add their fence to the root reservation with DMA_RESV_USAGE_BOOKKEEP, so the wait no longer covers the clear of the root PD issued at VM creation.

A process that maps memory right after AMDKFD_IOC_ACQUIRE_VM can then write a root PDE with the CPU before the pending SDMA clear of the root PD has executed, and the clear wipes the entry. The GPU later takes a not-present fault on the mapped address. We hit this as a CPC fault on a user queue's write pointer page; walking the VM with UMR while the fault was pending showed the root entry as 0x8000000000000000. Adding a short delay after ACQUIRE_VM made the fault go away.

Wait for all fences on the root reservation, including BOOKKEEP, before switching to CPU updates. The dma_resv_wait_timeout() result is kept in a long, since MAX_SCHEDULE_TIMEOUT does not fit in an int.

Tested on gfx1201 with a v7.0 based kernel and a reproducer that acquires a VM, maps buffers and creates an SDMA queue right away:
8 of 8 runs faulted without this patch, 0 of 8 with it.

Fixes 4247084057cf ("drm/amdgpu: use DMA_RESV_USAGE_BOOKKEEP v2")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +10 -4
index 0b8a8f82ecab..cc743df64af6 100644
@@ -2728,6 +2728,7 @@ int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  */
 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 {
+	long wait;
 	int r;
 
 	r = amdgpu_bo_reserve(vm->root.bo, true);
@@ -2744,11 +2745,16 @@ int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 		  "CPU update of VM recommended only for large BAR system\n");
 
 	if (vm->use_cpu_for_update) {
-		/* Sync with last SDMA update/clear before switching to CPU */
-		r = amdgpu_bo_sync_wait(vm->root.bo,
-					AMDGPU_FENCE_OWNER_UNDEFINED, true);
-		if (r)
+		/* Sync with last SDMA update/clear before switching to CPU.
+		 * PT updates are BOOKKEEP fences, amdgpu_bo_sync_wait() skips them.
+		 */
+		wait = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv,
+					     DMA_RESV_USAGE_BOOKKEEP, true,
+					     MAX_SCHEDULE_TIMEOUT);
+		if (wait < 0) {
+			r = wait;
 			goto unreserve_bo;
+		}
 
 		vm->update_funcs = &amdgpu_vm_cpu_funcs;
 		r = amdgpu_vm_pt_map_tables(adev, vm);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/amdgpu: BOOKKEEP wait in amdgpu_vm_make_compute() evicts KGD userqs - [Low] drm/amdgpu: Fixes…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/amdgpu: BOOKKEEP wait in amdgpu_vm_make_compute() evicts KGD userqs
- [Low] drm/amdgpu: Fixes tag cites 4247084057cf instead of 926c79ad6ecd --

commit f7d48a981689f9a016015e4327294adae6c1d421
Author: Amir Shetaia <[email protected]>

Quoted 25 lines

drm/amdgpu: wait for PT updates in amdgpu_vm_make_compute

amdgpu_vm_make_compute() switches a VM to CPU page table updates when compute VMs use the CPU path (vm_update_mode, the default when all of VRAM is CPU visible). Before switching it waits for the last SDMA update/clear of the root PD with amdgpu_bo_sync_wait().

That helper goes through amdgpu_sync_resv(), which only iterates DMA_RESV_USAGE_READ fences. Since commit 4247084057cf ("drm/amdgpu: use DMA_RESV_USAGE_BOOKKEEP v2") page table updates add their fence to the root reservation with DMA_RESV_USAGE_BOOKKEEP, so the wait no longer covers the clear of the root PD issued at VM creation.

A process that maps memory right after AMDKFD_IOC_ACQUIRE_VM can then write a root PDE with the CPU before the pending SDMA clear of the root PD has executed, and the clear wipes the entry. The GPU later takes a not-present fault on the mapped address. We hit this as a CPC fault on a user queue's write pointer page; walking the VM with UMR while the fault was pending showed the root entry as 0x8000000000000000. Adding a short delay after ACQUIRE_VM made the fault go away.

Wait for all fences on the root reservation, including BOOKKEEP, before switching to CPU updates. The dma_resv_wait_timeout() result is kept in a long, since MAX_SCHEDULE_TIMEOUT does not fit in an int.

Quoted 6 lines

Tested on gfx1201 with a v7.0 based kernel and a reproducer that acquires a VM, maps buffers and creates an SDMA queue right away:
8 of 8 runs faulted without this patch, 0 of 8 with it.

Fixes 4247084057cf ("drm/amdgpu: use DMA_RESV_USAGE_BOOKKEEP v2")

[Severity: Low]
Should the Fixes tag (and commit message) cite 926c79ad6ecd ("drm/amdgpu:
promote the implicit sync to the dependent read fences") instead of 4247084057cf ("drm/amdgpu: use DMA_RESV_USAGE_BOOKKEEP v2")?

amdgpu_sync_resv() still iterated DMA_RESV_USAGE_BOOKKEEP fences until the 2025 commit changed it to DMA_RESV_USAGE_READ, so amdgpu_bo_sync_wait() kept waiting on PT updates prior to that. Citing the earlier commit can cause stable backport tooling to flag unaffected LTS kernels.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index a3758c654dd42..130dbcbf3c85c 100644

[ ... ]

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -4
@@ -2721,11 +2722,16 @@ int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 		  "CPU update of VM recommended only for large BAR system\n");
 
 	if (vm->use_cpu_for_update) {
-		/* Sync with last SDMA update/clear before switching to CPU */
-		r = amdgpu_bo_sync_wait(vm->root.bo,
-					AMDGPU_FENCE_OWNER_UNDEFINED, true);
-		if (r)

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) · line 2734

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +9 -0
+		/* Sync with last SDMA update/clear before switching to CPU.
+		 * PT updates are BOOKKEEP fences, amdgpu_bo_sync_wait() skips them.
+		 */
+		wait = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv,
+					     DMA_RESV_USAGE_BOOKKEEP, true,
+					     MAX_SCHEDULE_TIMEOUT);
+		if (wait < 0) {
+			r = wait;
 			goto unreserve_bo;
+		}

[Severity: Medium]
Can waiting on all DMA_RESV_USAGE_BOOKKEEP fences here trigger an unintended eviction of active KGD user queues?

If a process creates a KGD user queue on a DRM render fd before calling AMDKFD_IOC_ACQUIRE_VM on it, amdgpu_evf_mgr_attach_fence() attaches an unsignaled amdgpu_eviction_fence to vm->root.bo with DMA_RESV_USAGE_BOOKKEEP.

Waiting on that fence enables signaling and schedules suspend_work -> amdgpu_userq_evict(), draining every active user queue's last_fence uninterruptibly and unmapping all user queues via MES while vm->root.bo is reserved, stalling AMDKFD_IOC_ACQUIRE_VM until they are evicted and later remapped.

Switching to amdgpu_vm_cpu_funcs only needs to wait for kernel/move fences and pending SDMA PT updates (AMDGPU_FENCE_OWNER_VM), and can skip waiting for SDMA updates when vm->update_funcs is already &amdgpu_vm_cpu_funcs.

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