Skip to contentSkip to conversation

drm/amdgpu: don't wait for the SPM VMID's jobs under the VMID manager lock

PATCH New Jesse Zhang 1 participant 1 message Oct 22

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

PATCH v3 1/3 drm/amdgpu: don't wait for the SPM VMID's jobs under the VMID manager lock

amdgpu_vmid_free_spm() waits for the jobs of the reserved VMID while holding the VMID manager lock of the hub, which every VMID grab on the hub takes as well. Freeing the reservation, by UNRESERVE_VMID or when the VM is destroyed, so stalls the kernel queue submissions of every process on the GFXHUB until those jobs completed, or until the job timeout if one of them hangs. A full GPU reset even deadlocks, since amdgpu_vmid_reset_all() needs the lock before the scheduler is started again to signal the jobs.

On navi31, with a GFX job of the reserving process that waits for 1s, an unrelated process' SDMA NOP takes 0.8s instead of no time when the VMID gets unreserved meanwhile.

Wait for the submissions of the VM in the callers instead, through the fences of the root PD, before taking the lock. Use DMA_RESV_USAGE_READ, which covers the command submissions but not the KFD eviction fence. UNRESERVE_VMID keeps the root PD reserved while waiting and freeing the VMID, so that no new submission can grab the VMID in between, and only waits when the VM actually has the VMID reserved.

v2: wait for the root PD's reservation object in the callers instead of

    accessing id->active without the lock (Christian)
Fixes 9b47649cabf4 ("drm/amdgpu: bind the SPM VMID's page table to the reserving VM")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c |  8 +++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  | 16 +++++++++++++++-
 2 files changed, 18 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +3 -5
index 1482fc031d44..9c879ac68352 100644
@@ -634,8 +634,9 @@ int amdgpu_vmid_alloc_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
  * @vm: the VM with the reserved ID
  * @vmhub: the VMHUB which should be used
  *
- * Waits for all work that used the reserved VMID to finish before unbinding
- * its page table, so submissions still in flight don't suddenly fault.
+ * The caller must wait for all work that used the reserved VMID to finish
+ * before, so submissions still in flight don't suddenly fault when its page
+ * table gets unbound.
  */
 void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 			       unsigned vmhub)
@@ -649,9 +650,6 @@ void amdgpu_vmid_free_spm(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		vm->reserved_vmid[vmhub] = NULL;
 		id_mgr->reserved_vmid = false;
 
-		/* Only for kernel queues and kfd/userqueues by pass that */
-		amdgpu_sync_wait(&id->active, false);
-
 		if (adev->vm_manager.spm_vmid && adev->gfxhub.funcs)
 			amdgpu_gmc_bind_vmid(adev, AMDGPU_GFXHUB(0),
 					     adev->vm_manager.spm_vmid, 0);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +15 -1
index 3f19d03fd7d8..c5643344a3f1 100644
@@ -2852,6 +2852,10 @@ void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
 
 	root = amdgpu_bo_ref(vm->root.bo);
 	amdgpu_bo_reserve(root, true);
+	/* Wait for the jobs which might still use the reserved VMID */
+	if (vm->reserved_vmid[AMDGPU_GFXHUB(0)])
+		dma_resv_wait_timeout(root->tbo.base.resv, DMA_RESV_USAGE_READ,
+				      false, MAX_SCHEDULE_TIMEOUT);
 	dma_fence_wait(vm->last_unlocked, false);
 	dma_fence_put(vm->last_unlocked);
 	dma_fence_wait(vm->last_tlb_flush, false);
@@ -3002,9 +3006,19 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 		}
 		return r;
 	}
-	case AMDGPU_VM_OP_UNRESERVE_VMID:
+	case AMDGPU_VM_OP_UNRESERVE_VMID: {
+		struct amdgpu_bo *root = vm->root.bo;
+
+		amdgpu_bo_reserve(root, true);
+		/* Wait for the jobs which might still use the reserved VMID */
+		if (vm->reserved_vmid[AMDGPU_GFXHUB(0)])
+			dma_resv_wait_timeout(root->tbo.base.resv,
+					      DMA_RESV_USAGE_READ, false,
+					      MAX_SCHEDULE_TIMEOUT);
 		amdgpu_vmid_free_spm(adev, vm, AMDGPU_GFXHUB(0));
+		amdgpu_bo_unreserve(root);
 		break;
+	}
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.50.1