Skip to contentSkip to conversation

drm/amdkfd: Fix a race condition of vram buffer unref in svm code

PATCH In review Xiaogang.Chen 4 participants 5 messages Sep 27, 2023

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

PATCH 1/1 drm/amdkfd: Fix a race condition of vram buffer unref in svm code

From: Xiaogang Chen <[email protected]>

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".

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +9 -0
index 70aa882636ab..8e246e848018 100644
@@ -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 *
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw

[AMD Official Use Only - General]

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

From: Xiaogang Chen <[email protected]>

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".

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +9 -1
index 70aa882636ab..8e246e848018 100644
@@ -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

#2 lore raw
In reply to Xiaogang.Chen

On 2023-09-26 23:00, Xiaogang.Chen wrote:
From: Xiaogang Chen <[email protected]> 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 <[email protected]> Reviewed-by: Philip Yang <[email protected]>
--- 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 *

#3 lore raw
In reply to Xiaogang.Chen
Quoted 16 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

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".

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++
  1 file changed, 9 insertions(+)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index 70aa882636ab..8e246e848018 100644

drivers/gpu/drm/amd/amdkfd/kfd_svm.c void svm_range_vram_node_free(struct svm_range *prange) · line 639

Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
@@ -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

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +9 -0
+	/* 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 *
#4 lore raw
Quoted 15 lines · Eric Huang

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 11 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

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".

---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++

  1 file changed, 9 insertions(+)
Quoted 10 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c void svm_range_vram_node_free(struct svm_range · line 640

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -0
index 70aa882636ab..8e246e848018 100644
@@ -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

Quoted 16 lines

Regards,
Eric

Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +9 -0
+     /* 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 *