Skip to contentSkip to conversation

drm/amdgpu: fix userq VM validation v2

PATCH In review Christian König 2 participants 4 messages Aug 28, 2025

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

PATCH 1/3 drm/amdgpu: fix userq VM validation v2

That was actually complete nonsense and not validating the BOs at all. The code just cleared all VM areas were it couldn't grab the lock for a BO.

Try to fix this. Only compile tested at the moment.

v2: fix fence slot reservation as well as pointed out by Sunil.
also validate PDs, PTs, per VM BOs and update PDEs

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 118 ++++++++++------------
 1 file changed, 56 insertions(+), 62 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +56 -62
index 424831997cb1..11edad1951c7 100644
@@ -545,108 +545,102 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
 	return ret;
 }
 
+static int amdgpu_userq_validate_vm(void *param, struct amdgpu_bo *bo)
+{
+	struct ttm_operation_ctx ctx = { false, false };
+
+	amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
+	return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+}
+
 static int
-amdgpu_userq_validate_vm_bo(void *_unused, struct amdgpu_bo *bo)
+amdgpu_userq_validate_bos(struct amdgpu_device *adev, struct drm_exec *exec,
+			  struct amdgpu_vm *vm)
 {
 	struct ttm_operation_ctx ctx = { false, false };
+	struct amdgpu_bo_va *bo_va;
+	struct amdgpu_bo *bo;
 	int ret;
 
-	amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
+	spin_lock(&vm->status_lock);
+	while (!list_empty(&vm->invalidated)) {
+		bo_va = list_first_entry(&vm->invalidated,
+					 struct amdgpu_bo_va,
+					 base.vm_status);
+		spin_unlock(&vm->status_lock);
 
-	ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
-	if (ret)
-		DRM_ERROR("Fail to validate\n");
+		bo = bo_va->base.bo;
+		ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 2);
+		if (unlikely(ret))
+			return ret;
 
-	return ret;
+		amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
+		ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+		if (ret)
+			return ret;
+
+		/* This moves the bo_va to the done list */
+		ret = amdgpu_vm_bo_update(adev, bo_va, false);
+		if (ret)
+			return ret;
+
+		spin_lock(&vm->status_lock);
+	}
+	spin_unlock(&vm->status_lock);
+
+	return 0;
 }
 
 static int
-amdgpu_userq_validate_bos(struct amdgpu_userq_mgr *uq_mgr)
+amdgpu_userq_update_vm(struct amdgpu_userq_mgr *uq_mgr)
 {
 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
-	struct amdgpu_vm *vm = &fpriv->vm;
 	struct amdgpu_device *adev = uq_mgr->adev;
+	struct amdgpu_vm *vm = &fpriv->vm;
 	struct amdgpu_bo_va *bo_va;
-	struct ww_acquire_ctx *ticket;
 	struct drm_exec exec;
 	struct amdgpu_bo *bo;
-	struct dma_resv *resv;
-	bool clear, unlock;
 	int ret = 0;
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
-		ret = amdgpu_vm_lock_pd(vm, &exec, 2);
+		ret = amdgpu_vm_lock_pd(vm, &exec, 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret)) {
 			drm_file_err(uq_mgr->file, "Failed to lock PD\n");
 			goto unlock_all;
 		}
 
-		/* Lock the done list */
 		list_for_each_entry(bo_va, &vm->done, base.vm_status) {
 			bo = bo_va->base.bo;
 			if (!bo)
 				continue;
 
-			ret = drm_exec_lock_obj(&exec, &bo->tbo.base);
+			ret = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1);
 			drm_exec_retry_on_contention(&exec);
 			if (unlikely(ret))
 				goto unlock_all;
 		}
-	}
 
-	spin_lock(&vm->status_lock);
-	while (!list_empty(&vm->moved)) {
-		bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va,
-					 base.vm_status);
-		spin_unlock(&vm->status_lock);
-
-		/* Per VM BOs never need to bo cleared in the page tables */
-		ret = amdgpu_vm_bo_update(adev, bo_va, false);
-		if (ret)
+		ret = amdgpu_vm_validate(adev, vm, NULL,
+					 amdgpu_userq_validate_vm,
+					 NULL);
+		if (unlikely(ret))
 			goto unlock_all;
-		spin_lock(&vm->status_lock);
-	}
 
-	ticket = &exec.ticket;
-	while (!list_empty(&vm->invalidated)) {
-		bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
-					 base.vm_status);
-		resv = bo_va->base.bo->tbo.base.resv;
-		spin_unlock(&vm->status_lock);
-
-		bo = bo_va->base.bo;
-		ret = amdgpu_userq_validate_vm_bo(NULL, bo);
-		if (ret) {
-			drm_file_err(uq_mgr->file, "Failed to validate BO\n");
+		ret = amdgpu_userq_validate_bos(adev, &exec, vm);
+		drm_exec_retry_on_contention(&exec);
+		if (unlikely(ret))
 			goto unlock_all;
-		}
-
-		/* Try to reserve the BO to avoid clearing its ptes */
-		if (!adev->debug_vm && dma_resv_trylock(resv)) {
-			clear = false;
-			unlock = true;
-		/* The caller is already holding the reservation lock */
-		} else if (dma_resv_locking_ctx(resv) == ticket) {
-			clear = false;
-			unlock = false;
-		/* Somebody else is using the BO right now */
-		} else {
-			clear = true;
-			unlock = false;
-		}
-
-		ret = amdgpu_vm_bo_update(adev, bo_va, clear);
+	}
 
-		if (unlock)
-			dma_resv_unlock(resv);
-		if (ret)
-			goto unlock_all;
+	ret = amdgpu_vm_handle_moved(adev, vm, NULL);
+	if (ret)
+		goto unlock_all;
 
-		spin_lock(&vm->status_lock);
-	}
-	spin_unlock(&vm->status_lock);
+	ret = amdgpu_vm_update_pdes(adev, vm, false);
+	if (ret)
+		goto unlock_all;
 
 	ret = amdgpu_eviction_fence_replace_fence(&fpriv->evf_mgr, &exec);
 	if (ret)
@@ -667,7 +661,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 
 	mutex_lock(&uq_mgr->userq_mutex);
 
-	ret = amdgpu_userq_validate_bos(uq_mgr);
+	ret = amdgpu_userq_update_vm(uq_mgr);
 	if (ret) {
 		drm_file_err(uq_mgr->file, "Failed to validate BOs to restore\n");
 		goto unlock;
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/3 drm/amdgpu: remove check for BO reservation add assert instead

We should leave such checks to lockdep and not implement something manually.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -12
index bf42246a3db2..1dec07513cd6 100644
@@ -616,18 +616,7 @@ int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 		spin_unlock(&vm->status_lock);
 
 		bo = bo_base->bo;
-
-		if (dma_resv_locking_ctx(bo->tbo.base.resv) != ticket) {
-			struct amdgpu_task_info *ti = amdgpu_vm_get_task_info_vm(vm);
-
-			pr_warn_ratelimited("Evicted user BO is not reserved\n");
-			if (ti) {
-				pr_warn_ratelimited("pid %d\n", ti->pid);
-				amdgpu_vm_put_task_info(ti);
-			}
-
-			return -EINVAL;
-		}
+		dma_resv_assert_held(bo->tbo.base.resv);
 
 		r = validate(param, bo);
 		if (r)
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/3 drm/amdgpu: revert "Rename VM invalidate to status lock" v2

In reply to Christian König

This reverts commit 0479956c94b1cfa6a1ab9206eff76072944ece8b.

It turned out that protecting the status of each bo_va only with a spinlock was just hiding problems instead of solving them.

Revert the whole approach, add a separate stats_lock and lockdep assertions that the correct reservation lock is held all over the place.

While at it also re-order fields in the VM structure and try to improve the documentation a bit.

v2: re-add missing check

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c |   8 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 160 ++++++++++------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h    |  25 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c |   4 -
 4 files changed, 92 insertions(+), 105 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -4
index 11edad1951c7..b8b71df3e6a9 100644
@@ -562,12 +562,12 @@ amdgpu_userq_validate_bos(struct amdgpu_device *adev, struct drm_exec *exec,
 	struct amdgpu_bo *bo;
 	int ret;
 
-	spin_lock(&vm->status_lock);
+	spin_lock(&vm->invalidated_lock);
 	while (!list_empty(&vm->invalidated)) {
 		bo_va = list_first_entry(&vm->invalidated,
 					 struct amdgpu_bo_va,
 					 base.vm_status);
-		spin_unlock(&vm->status_lock);
+		spin_unlock(&vm->invalidated_lock);
 
 		bo = bo_va->base.bo;
 		ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 2);
@@ -584,9 +584,9 @@ amdgpu_userq_validate_bos(struct amdgpu_device *adev, struct drm_exec *exec,
 		if (ret)
 			return ret;
 
-		spin_lock(&vm->status_lock);
+		spin_lock(&vm->invalidated_lock);
 	}
-	spin_unlock(&vm->status_lock);
+	spin_unlock(&vm->invalidated_lock);
 
 	return 0;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +76 -84

427 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +12 -13
index f9549f6b3d1f..c90891f1f835 100644
@@ -203,11 +203,11 @@ struct amdgpu_vm_bo_base {
 	/* protected by bo being reserved */
 	struct amdgpu_vm_bo_base	*next;
 
-	/* protected by vm status_lock */
+	/* protected by vm reservation and status_lock */
 	struct list_head		vm_status;
 
 	/* if the bo is counted as shared in mem stats
-	 * protected by vm status_lock */
+	 * protected by vm BO being reserved */
 	bool				shared;
 
 	/* protected by the BO being reserved */
@@ -344,18 +344,13 @@ struct amdgpu_vm {
 	bool			evicting;
 	unsigned int		saved_flags;
 
-	/* Lock to protect vm_bo add/del/move on all lists of vm */
-	spinlock_t		status_lock;
-
-	/* Memory statistics for this vm, protected by status_lock */
+	/* Memory statistics for this vm, protected by stats_lock */
+	spinlock_t		stats_lock;
 	struct amdgpu_mem_stats stats[__AMDGPU_PL_NUM];
 
 	/* Per-VM and PT BOs who needs a validation */
 	struct list_head	evicted;
 
-	/* BOs for user mode queues that need a validation */
-	struct list_head	evicted_user;
-
 	/* PT BOs which relocated and their parent need an update */
 	struct list_head	relocated;
 
@@ -365,15 +360,19 @@ struct amdgpu_vm {
 	/* All BOs of this VM not currently in the state machine */
 	struct list_head	idle;
 
-	/* regular invalidated BOs, but not yet updated in the PT */
+	/* Regular BOs for KFD queues that need a validation */
+	struct list_head	evicted_user;
+
+	/* Regular invalidated BOs, need to be validated and updated in the PT */
+	spinlock_t		invalidated_lock;
 	struct list_head	invalidated;
 
+	/* Regular BOs which are validated and location has been updated in the PTs */
+	struct list_head        done;
+
 	/* BO mappings freed, but not yet updated in the PT */
 	struct list_head	freed;
 
-	/* BOs which are invalidated, has been updated in the PTs */
-	struct list_head        done;
-
 	/* contains the page directory */
 	struct amdgpu_vm_bo_base     root;
 	struct dma_fence	*last_update;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm_pt.c +0 -4
index 30022123b0bf..f57c48b74274 100644
@@ -541,9 +541,7 @@ static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
 	entry->bo->vm_bo = NULL;
 	ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
 
-	spin_lock(&entry->vm->status_lock);
 	list_del(&entry->vm_status);
-	spin_unlock(&entry->vm->status_lock);
 	amdgpu_bo_unref(&entry->bo);
 }
 
@@ -587,7 +585,6 @@ static void amdgpu_vm_pt_add_list(struct amdgpu_vm_update_params *params,
 	struct amdgpu_vm_pt_cursor seek;
 	struct amdgpu_vm_bo_base *entry;
 
-	spin_lock(&params->vm->status_lock);
 	for_each_amdgpu_vm_pt_dfs_safe(params->adev, params->vm, cursor, seek, entry) {
 		if (entry && entry->bo)
 			list_move(&entry->vm_status, &params->tlb_flush_waitlist);
@@ -595,7 +592,6 @@ static void amdgpu_vm_pt_add_list(struct amdgpu_vm_update_params *params,
 
 	/* enter start node now */
 	list_move(&cursor->entry->vm_status, &params->tlb_flush_waitlist);
-	spin_unlock(&params->vm->status_lock);
 }
 
 /**
Signature · 1 line
2.43.0
#3 lore raw
In reply to Christian König
Quoted 20 lines · Christian König

That was actually complete nonsense and not validating the BOs at all. The code just cleared all VM areas were it couldn't grab the lock for a BO.

Try to fix this. Only compile tested at the moment.

v2: fix fence slot reservation as well as pointed out by Sunil.
also validate PDs, PTs, per VM BOs and update PDEs

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 118 ++++++++++------------
 1 file changed, 56 insertions(+), 62 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -0
index 424831997cb1..11edad1951c7 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) · line 548

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
@@ -545,108 +545,102 @@ amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr)
        return ret;
 }
 
+static int amdgpu_userq_validate_vm(void *param, struct amdgpu_bo *bo)

I would rename this amdgpu_userq_bo_validate() for consistency (e.g., amdgpu_cs_bo_validate,()) and to make it clearer what it is doing.

Quoted 44 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +31 -6
+{
+       struct ttm_operation_ctx ctx = { false, false };
+
+       amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
+       return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+}
+
 static int
-amdgpu_userq_validate_vm_bo(void *_unused, struct amdgpu_bo *bo)
+amdgpu_userq_validate_bos(struct amdgpu_device *adev, struct drm_exec *exec,
+                         struct amdgpu_vm *vm)
 {
        struct ttm_operation_ctx ctx = { false, false };
+       struct amdgpu_bo_va *bo_va;
+       struct amdgpu_bo *bo;
        int ret;
 
-       amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
+       spin_lock(&vm->status_lock);
+       while (!list_empty(&vm->invalidated)) {
+               bo_va = list_first_entry(&vm->invalidated,
+                                        struct amdgpu_bo_va,
+                                        base.vm_status);
+               spin_unlock(&vm->status_lock);
 
-       ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
-       if (ret)
-               DRM_ERROR("Fail to validate\n");
+               bo = bo_va->base.bo;
+               ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 2);
+               if (unlikely(ret))
+                       return ret;
 
-       return ret;
+               amdgpu_bo_placement_from_domain(bo, bo->allowed_domains);
+               ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
+               if (ret)
+                       return ret;
+
+               /* This moves the bo_va to the done list */
+               ret = amdgpu_vm_bo_update(adev, bo_va, false);
+               if (ret)
+                       return ret;
+

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_restore_all(struct amdgpu_userq_mgr *uq_mgr) · line 595

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +6 -1
+               spin_lock(&vm->status_lock);
+       }
+       spin_unlock(&vm->status_lock);
+
+       return 0;
 }
 
 static int
-amdgpu_userq_validate_bos(struct amdgpu_userq_mgr *uq_mgr)
+amdgpu_userq_update_vm(struct amdgpu_userq_mgr *uq_mgr)

Please add some comments to this function to make it clear what each step is doing and why we are doing it. Many of these functions have very similar names and it's hard to follow what they are all doing without double checking their implementations. It would also be helpful to plug in a comment about where we should plug in usr ptr checks. amdgpu_cs_parser_bos() could use similar comments if you have a chance.

Other than that, the patch looks good to me.

Alex

Quoted 117 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +18 -56
 {
        struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
-       struct amdgpu_vm *vm = &fpriv->vm;
        struct amdgpu_device *adev = uq_mgr->adev;
+       struct amdgpu_vm *vm = &fpriv->vm;
        struct amdgpu_bo_va *bo_va;
-       struct ww_acquire_ctx *ticket;
        struct drm_exec exec;
        struct amdgpu_bo *bo;
-       struct dma_resv *resv;
-       bool clear, unlock;
        int ret = 0;
 
        drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
        drm_exec_until_all_locked(&exec) {
-               ret = amdgpu_vm_lock_pd(vm, &exec, 2);
+               ret = amdgpu_vm_lock_pd(vm, &exec, 1);
                drm_exec_retry_on_contention(&exec);
                if (unlikely(ret)) {
                        drm_file_err(uq_mgr->file, "Failed to lock PD\n");
                        goto unlock_all;
                }
 
-               /* Lock the done list */
                list_for_each_entry(bo_va, &vm->done, base.vm_status) {
                        bo = bo_va->base.bo;
                        if (!bo)
                                continue;
 
-                       ret = drm_exec_lock_obj(&exec, &bo->tbo.base);
+                       ret = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1);
                        drm_exec_retry_on_contention(&exec);
                        if (unlikely(ret))
                                goto unlock_all;
                }
-       }
 
-       spin_lock(&vm->status_lock);
-       while (!list_empty(&vm->moved)) {
-               bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va,
-                                        base.vm_status);
-               spin_unlock(&vm->status_lock);
-
-               /* Per VM BOs never need to bo cleared in the page tables */
-               ret = amdgpu_vm_bo_update(adev, bo_va, false);
-               if (ret)
+               ret = amdgpu_vm_validate(adev, vm, NULL,
+                                        amdgpu_userq_validate_vm,
+                                        NULL);
+               if (unlikely(ret))
                        goto unlock_all;
-               spin_lock(&vm->status_lock);
-       }
 
-       ticket = &exec.ticket;
-       while (!list_empty(&vm->invalidated)) {
-               bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
-                                        base.vm_status);
-               resv = bo_va->base.bo->tbo.base.resv;
-               spin_unlock(&vm->status_lock);
-
-               bo = bo_va->base.bo;
-               ret = amdgpu_userq_validate_vm_bo(NULL, bo);
-               if (ret) {
-                       drm_file_err(uq_mgr->file, "Failed to validate BO\n");
+               ret = amdgpu_userq_validate_bos(adev, &exec, vm);
+               drm_exec_retry_on_contention(&exec);
+               if (unlikely(ret))
                        goto unlock_all;
-               }
-
-               /* Try to reserve the BO to avoid clearing its ptes */
-               if (!adev->debug_vm && dma_resv_trylock(resv)) {
-                       clear = false;
-                       unlock = true;
-               /* The caller is already holding the reservation lock */
-               } else if (dma_resv_locking_ctx(resv) == ticket) {
-                       clear = false;
-                       unlock = false;
-               /* Somebody else is using the BO right now */
-               } else {
-                       clear = true;
-                       unlock = false;
-               }
-
-               ret = amdgpu_vm_bo_update(adev, bo_va, clear);
+       }
 
-               if (unlock)
-                       dma_resv_unlock(resv);
-               if (ret)
-                       goto unlock_all;
+       ret = amdgpu_vm_handle_moved(adev, vm, NULL);
+       if (ret)
+               goto unlock_all;
 
-               spin_lock(&vm->status_lock);
-       }
-       spin_unlock(&vm->status_lock);
+       ret = amdgpu_vm_update_pdes(adev, vm, false);
+       if (ret)
+               goto unlock_all;
 
        ret = amdgpu_eviction_fence_replace_fence(&fpriv->evf_mgr, &exec);
        if (ret)
@@ -667,7 +661,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 
        mutex_lock(&uq_mgr->userq_mutex);
 
-       ret = amdgpu_userq_validate_bos(uq_mgr);
+       ret = amdgpu_userq_update_vm(uq_mgr);
        if (ret) {
                drm_file_err(uq_mgr->file, "Failed to validate BOs to restore\n");
                goto unlock;
--

2.43.0