drm/amdkfd: Fix a race condition of vram buffer unref in svm code
Subsystems (3)
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
@@ -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
[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 callbackprange->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
@@ -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
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 *
Quoted 16 lines · Xiaogang.Chen
From: Xiaogang Chen <[email protected]>
prange->svm_bounref can happen in both mmu callback and a callback after migrate to system ram. Both are async call in different tasks. Syncsvm_bounref operation to avoid random "use-after-free".Signed-off-by Xiaogang.Chen <[email protected]>--- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0
-0
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 *
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_bounref can happen in both mmu callback and a callback after migrate to system ram. Both are async call in different tasks. Syncsvm_bounref operation to avoid random "use-after-free".Signed-off-by Xiaogang.Chen <[email protected]>---
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@@ -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,
EricQuoted 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*