Skip to contentSkip to conversation

drm/amdgpu: grab a BO reference in vm_lock_done_list.

PATCH In review Christian König 3 participants 3 messages Oct 31, 2025

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

PATCH 1/1 drm/amdgpu: grab a BO reference in vm_lock_done_list.

Otherwise it is possible that between dropping the status lock and locking the BO that the BO is freed up.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +6 -2
index db66b4232de0..c3dfb949a9b8 100644
@@ -484,15 +484,19 @@ int amdgpu_vm_lock_done_list(struct amdgpu_vm *vm, struct drm_exec *exec,
 	spin_lock(&vm->status_lock);
 	while (!list_is_head(prev->next, &vm->done)) {
 		bo_va = list_entry(prev->next, typeof(*bo_va), base.vm_status);
-		spin_unlock(&vm->status_lock);
 
 		bo = bo_va->base.bo;
 		if (bo) {
+			amdgpu_bo_ref(bo);
+			spin_unlock(&vm->status_lock);
+
 			ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 1);
+			amdgpu_bo_unref(&bo);
 			if (unlikely(ret))
 				return ret;
+
+			spin_lock(&vm->status_lock);
 		}
-		spin_lock(&vm->status_lock);
 		prev = prev->next;
 	}
 	spin_unlock(&vm->status_lock);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 14 lines · Christian König

Otherwise it is possible that between dropping the status lock and locking the BO that the BO is freed up.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index db66b4232de0..c3dfb949a9b8 100644
@@ -484,15 +484,19 @@ int amdgpu_vm_lock_done_list(struct amdgpu_vm *vm, struct drm_exec *exec,
  	spin_lock(&vm->status_lock);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_lock_done_list(struct amdgpu_vm *vm, struct drm_exec *exec, · line 493

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -1
  	while (!list_is_head(prev->next, &vm->done)) {
  		bo_va = list_entry(prev->next, typeof(*bo_va), base.vm_status);
-		spin_unlock(&vm->status_lock);
 
  		bo = bo_va->base.bo;
  		if (bo) {
+			amdgpu_bo_ref(bo);
+			spin_unlock(&vm->status_lock);
+
  			ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 1);

Just for my understanding, there is a possibility that a bo is free after spin_unlock and we might be using a stale/NULL ptr of bo.  So we are taking a reference before releasing the lock to make

sure the bo is valid. Now calling drm_exec_prepare_obj take a recount to make sure the bo is always from now on. Reviewed-by: Sunil Khatri <[email protected]>

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -1
+			amdgpu_bo_unref(&bo);
  			if (unlikely(ret))
  				return ret;
+
+			spin_lock(&vm->status_lock);
  		}
-		spin_lock(&vm->status_lock);
  		prev = prev->next;
  	}
  	spin_unlock(&vm->status_lock);
maintainer
#2 lore raw
Quoted 29 lines · Sunil
Quoted 14 lines · Christian König

Otherwise it is possible that between dropping the status lock and locking the BO that the BO is freed up.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
index db66b4232de0..c3dfb949a9b8 100644
@@ -484,15 +484,19 @@ int amdgpu_vm_lock_done_list(struct amdgpu_vm *vm, struct drm_exec *exec,
 	spin_lock(&vm->status_lock);

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c int amdgpu_vm_lock_done_list(struct amdgpu_vm *vm, struct drm_exec *exec, · line 493

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -1
 	while (!list_is_head(prev->next, &vm->done)) {
 		bo_va = list_entry(prev->next, typeof(*bo_va), base.vm_status);
-		spin_unlock(&vm->status_lock);
 
 		bo = bo_va->base.bo;
 		if (bo) {
+			amdgpu_bo_ref(bo);
+			spin_unlock(&vm->status_lock);
+
 			ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 1);

Just for my understanding, there is a possibility that a bo is free after spin_unlock and we might be using a stale/NULL ptr of bo.  So we are taking a reference before releasing the lock to make

Quoted 1 line

sure the bo is valid. Now calling drm_exec_prepare_obj take a recount to make sure the bo is always from now on.

Yes, exactly that.

Quoted 1 line

Thanks!

Christian.

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +3 -1
+			amdgpu_bo_unref(&bo);
 			if (unlikely(ret))
 				return ret;
+
+			spin_lock(&vm->status_lock);
 		}
-		spin_lock(&vm->status_lock);
 		prev = prev->next;
 	}
 	spin_unlock(&vm->status_lock);