AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code
@ 2023-09-27  3:00 Xiaogang.Chen
  2023-09-27  3:13 ` Zhang, Jesse(Jie)
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Xiaogang.Chen @ 2023-09-27  3:00 UTC (permalink / raw)
  To: amd-gfx
  Cc: Alex.Sierra, Philip.Yang, felix.kuehling, Jesse.Zhang,
	Xiaogang . Chen

From: Xiaogang Chen <xiaogang.chen@amd.com>

prange->svm_bo unref can happen in both mmu callback and a callback after
migrate to system ram. Both are async call in different tasks. Sync svm_bo
unref operation to avoid random "use-after-free".

Signed-off-by: Xiaogang.Chen <Xiaogang.Chen@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 70aa882636ab..8e246e848018 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -637,6 +637,15 @@ void svm_range_vram_node_free(struct svm_range *prange)
 {
 	svm_range_bo_unref(prange->svm_bo);
 	prange->ttm_res = NULL;
+	/* serialize prange->svm_bo unref */
+	mutex_lock(&prange->lock);
+	/* prange->svm_bo has not been unref */
+	if (prange->ttm_res) {
+		prange->ttm_res = NULL;
+		mutex_unlock(&prange->lock);
+		svm_range_bo_unref(prange->svm_bo);
+	} else
+		mutex_unlock(&prange->lock);
 }
 
 struct kfd_node *
-- 
2.25.1


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

* RE: [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code
  2023-09-27  3:00 [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code Xiaogang.Chen
@ 2023-09-27  3:13 ` Zhang, Jesse(Jie)
  2023-09-27 13:59 ` Philip Yang
  2023-09-27 14:19 ` Eric Huang
  2 siblings, 0 replies; 5+ messages in thread
From: Zhang, Jesse(Jie) @ 2023-09-27  3:13 UTC (permalink / raw)
  To: Chen, Xiaogang, amd-gfx@lists.freedesktop.org
  Cc: Sierra Guiza, Alejandro (Alex), Yang, Philip, Kuehling, Felix,
	Chen, Xiaogang

[AMD Official Use Only - General]

Reviewed-by: Jesse Zhang <Jesse.Zhang@amd.com>
-----Original Message-----
From: Xiaogang.Chen <xiaogang.chen@amd.com>
Sent: Wednesday, September 27, 2023 11:00 AM
To: amd-gfx@lists.freedesktop.org
Cc: Yang, Philip <Philip.Yang@amd.com>; Kuehling, Felix <Felix.Kuehling@amd.com>; Zhang, Jesse(Jie) <Jesse.Zhang@amd.com>; Sierra Guiza, Alejandro (Alex) <Alex.Sierra@amd.com>; Chen, Xiaogang <Xiaogang.Chen@amd.com>; Chen, Xiaogang <Xiaogang.Chen@amd.com>
Subject: [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code

From: Xiaogang Chen <xiaogang.chen@amd.com>

prange->svm_bo unref can happen in both mmu callback and a callback
prange->after
migrate to system ram. Both are async call in different tasks. Sync svm_bo unref operation to avoid random "use-after-free".

Signed-off-by: Xiaogang.Chen <Xiaogang.Chen@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 70aa882636ab..8e246e848018 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -637,6 +637,15 @@ void svm_range_vram_node_free(struct svm_range *prange)  {
        svm_range_bo_unref(prange->svm_bo);
        prange->ttm_res = NULL;
+       /* serialize prange->svm_bo unref */
+       mutex_lock(&prange->lock);
+       /* prange->svm_bo has not been unref */
+       if (prange->ttm_res) {
+               prange->ttm_res = NULL;
+               mutex_unlock(&prange->lock);
+               svm_range_bo_unref(prange->svm_bo);
+       } else
+               mutex_unlock(&prange->lock);
 }

 struct kfd_node *
--
2.25.1


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

* Re: [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code
  2023-09-27  3:00 [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code Xiaogang.Chen
  2023-09-27  3:13 ` Zhang, Jesse(Jie)
@ 2023-09-27 13:59 ` Philip Yang
  2023-09-27 14:19 ` Eric Huang
  2 siblings, 0 replies; 5+ messages in thread
From: Philip Yang @ 2023-09-27 13:59 UTC (permalink / raw)
  To: Xiaogang.Chen, amd-gfx
  Cc: Alex.Sierra, Philip.Yang, felix.kuehling, Jesse.Zhang

[-- Attachment #1: Type: text/html, Size: 1929 bytes --]

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

* Re: [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code
  2023-09-27  3:00 [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code Xiaogang.Chen
  2023-09-27  3:13 ` Zhang, Jesse(Jie)
  2023-09-27 13:59 ` Philip Yang
@ 2023-09-27 14:19 ` Eric Huang
  2023-09-27 15:10   ` Chen, Xiaogang
  2 siblings, 1 reply; 5+ messages in thread
From: Eric Huang @ 2023-09-27 14:19 UTC (permalink / raw)
  To: amd-gfx


On 2023-09-26 23:00, Xiaogang.Chen wrote:
> From: Xiaogang Chen <xiaogang.chen@amd.com>
>
> prange->svm_bo unref can happen in both mmu callback and a callback after
> migrate to system ram. Both are async call in different tasks. Sync svm_bo
> unref operation to avoid random "use-after-free".
>
> Signed-off-by: Xiaogang.Chen <Xiaogang.Chen@amd.com>
> ---
>   drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++
>   1 file changed, 9 insertions(+)
>
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> index 70aa882636ab..8e246e848018 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> @@ -637,6 +637,15 @@ void svm_range_vram_node_free(struct svm_range *prange)
>   {
>   	svm_range_bo_unref(prange->svm_bo);
>   	prange->ttm_res = NULL;
Are above two lines not removed?

Regards,
Eric
> +	/* serialize prange->svm_bo unref */
> +	mutex_lock(&prange->lock);
> +	/* prange->svm_bo has not been unref */
> +	if (prange->ttm_res) {
> +		prange->ttm_res = NULL;
> +		mutex_unlock(&prange->lock);
> +		svm_range_bo_unref(prange->svm_bo);
> +	} else
> +		mutex_unlock(&prange->lock);
>   }
>   
>   struct kfd_node *


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

* Re: [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code
  2023-09-27 14:19 ` Eric Huang
@ 2023-09-27 15:10   ` Chen, Xiaogang
  0 siblings, 0 replies; 5+ messages in thread
From: Chen, Xiaogang @ 2023-09-27 15:10 UTC (permalink / raw)
  To: Eric Huang, amd-gfx


On 9/27/2023 9:19 AM, Eric Huang wrote:
> Caution: This message originated from an External Source. Use proper 
> caution when opening attachments, clicking links, or responding.
>
>
> On 2023-09-26 23:00, Xiaogang.Chen wrote:
>> From: Xiaogang Chen <xiaogang.chen@amd.com>
>>
>> prange->svm_bo unref can happen in both mmu callback and a callback 
>> after
>> migrate to system ram. Both are async call in different tasks. Sync 
>> svm_bo
>> unref operation to avoid random "use-after-free".
>>
>> Signed-off-by: Xiaogang.Chen <Xiaogang.Chen@amd.com>
>> ---
>>   drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++
>>   1 file changed, 9 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c 
>> b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> index 70aa882636ab..8e246e848018 100644
>> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
>> @@ -637,6 +637,15 @@ void svm_range_vram_node_free(struct svm_range 
>> *prange)
>>   {
>>       svm_range_bo_unref(prange->svm_bo);
>>       prange->ttm_res = NULL;
> Are above two lines not removed?

you are right, It was caused by copy-paste when edit. I tested it 
without these two lines. I will remove these two lines when send to gerrit.

Thanks

Xiaogang

>
> Regards,
> Eric
>> +     /* serialize prange->svm_bo unref */
>> +     mutex_lock(&prange->lock);
>> +     /* prange->svm_bo has not been unref */
>> +     if (prange->ttm_res) {
>> +             prange->ttm_res = NULL;
>> +             mutex_unlock(&prange->lock);
>> +             svm_range_bo_unref(prange->svm_bo);
>> +     } else
>> +             mutex_unlock(&prange->lock);
>>   }
>>
>>   struct kfd_node *
>

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

end of thread, other threads:[~2023-09-27 15:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-27  3:00 [PATCH] drm/amdkfd: Fix a race condition of vram buffer unref in svm code Xiaogang.Chen
2023-09-27  3:13 ` Zhang, Jesse(Jie)
2023-09-27 13:59 ` Philip Yang
2023-09-27 14:19 ` Eric Huang
2023-09-27 15:10   ` Chen, Xiaogang

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