AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdgpu: grab a BO reference in vm_lock_done_list.
@ 2025-10-31  8:25 Christian König
  2025-10-31  8:39 ` Khatri, Sunil
  0 siblings, 1 reply; 3+ messages in thread
From: Christian König @ 2025-10-31  8:25 UTC (permalink / raw)
  To: amd-gfx; +Cc: Sunil.Khatri

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

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
index db66b4232de0..c3dfb949a9b8 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
@@ -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);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/amdgpu: grab a BO reference in vm_lock_done_list.
  2025-10-31  8:25 [PATCH] drm/amdgpu: grab a BO reference in vm_lock_done_list Christian König
@ 2025-10-31  8:39 ` Khatri, Sunil
  2025-10-31  8:41   ` Christian König
  0 siblings, 1 reply; 3+ messages in thread
From: Khatri, Sunil @ 2025-10-31  8:39 UTC (permalink / raw)
  To: Christian König, amd-gfx; +Cc: Sunil.Khatri

[-- Attachment #1: Type: text/plain, Size: 1620 bytes --]


On 31-10-2025 01:55 pm, Christian König wrote:
> Otherwise it is possible that between dropping the status lock and
> locking the BO that the BO is freed up.
>
> Signed-off-by: Christian König<christian.koenig@amd.com>
> ---
>   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> index db66b4232de0..c3dfb949a9b8 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
> @@ -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);

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 
<sunil.khatri@amd.com>

> +			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);

[-- Attachment #2: Type: text/html, Size: 2380 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/amdgpu: grab a BO reference in vm_lock_done_list.
  2025-10-31  8:39 ` Khatri, Sunil
@ 2025-10-31  8:41   ` Christian König
  0 siblings, 0 replies; 3+ messages in thread
From: Christian König @ 2025-10-31  8:41 UTC (permalink / raw)
  To: Khatri, Sunil, amd-gfx; +Cc: Sunil.Khatri

On 10/31/25 09:39, Khatri, Sunil wrote:
> 
> On 31-10-2025 01:55 pm, Christian König wrote:
>> Otherwise it is possible that between dropping the status lock and
>> locking the BO that the BO is freed up.
>>
>> Signed-off-by: Christian König <christian.koenig@amd.com>
>> ---
>>  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 ++++++--
>>  1 file changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
>> index db66b4232de0..c3dfb949a9b8 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c
>> @@ -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);
> 
> 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.

Yes, exactly that.

> Reviewed-by: Sunil Khatri <sunil.khatri@amd.com>

Thanks!

Christian.

> 
>> +			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);


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-31  8:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-31  8:25 [PATCH] drm/amdgpu: grab a BO reference in vm_lock_done_list Christian König
2025-10-31  8:39 ` Khatri, Sunil
2025-10-31  8:41   ` Christian König

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox