Skip to contentSkip to conversation

drm/amdkfd: fix some race conditions in vram buffer alloc/free of svm code

PATCH Superseded Xiaogang.Chen 2 participants 3 messages Sep 20, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdkfd: fix some race conditions in vram buffer alloc/free of svm code

From: Xiaogang Chen <[email protected]>

This patch fixes:
1: ref number of prange's svm_bo got decreased by an async call from hmm. When wait svm_bo of prange got released we shoul also wait prang->svm_bo become NULL, otherwise prange->svm_bo may be set to null after allocate new vram buffer.

2: During waiting svm_bo of prange got released in a while loop should schedule current task to give other tasks oppotunity to run, specially the the workque task that handles svm_bo ref release, otherwise we may enter to softlock.

---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -4
index bed0f8bf83c7..1074a4aedf57 100644
@@ -502,11 +502,11 @@ svm_range_validate_svm_bo(struct kfd_node *node, struct svm_range *prange)
 
 	/* We need a new svm_bo. Spin-loop to wait for concurrent
 	 * svm_range_bo_release to finish removing this range from
-	 * its range list. After this, it is safe to reuse the
-	 * svm_bo pointer and svm_bo_list head.
+	 * its range list and set prange->svm_bo to null. After this,
+	 * it is safe to reuse the svm_bo pointer and svm_bo_list head.
 	 */
-	while (!list_empty_careful(&prange->svm_bo_list))
-		;
+	while (!list_empty_careful(&prange->svm_bo_list) || prange->svm_bo)
+		schedule();
 
 	return false;
 }
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 6 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

This patch fixes:
1: ref number of prange's svm_bo got decreased by an async call from hmm. When wait svm_bo of prange got released we shoul also wait prang->svm_bo become NULL, otherwise prange->svm_bo may be set to null after allocate new vram buffer.

I agree with this part.

Quoted 4 lines

2: During waiting svm_bo of prange got released in a while loop should schedule current task to give other tasks oppotunity to run, specially the the workque task that handles svm_bo ref release, otherwise we may enter to softlock.

We had a similar discussion a few weeks back for another soft lock and I pointed to  cond_reschedule, which seems to be the preferred way to avoid soft locks in the kernel. Does cond_reschedule work for this case?

Regards,
  Felix

Quoted 26 lines
---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -4
index bed0f8bf83c7..1074a4aedf57 100644
@@ -502,11 +502,11 @@ svm_range_validate_svm_bo(struct kfd_node *node, struct svm_range *prange)
 
  	/* We need a new svm_bo. Spin-loop to wait for concurrent
  	 * svm_range_bo_release to finish removing this range from
-	 * its range list. After this, it is safe to reuse the
-	 * svm_bo pointer and svm_bo_list head.
+	 * its range list and set prange->svm_bo to null. After this,
+	 * it is safe to reuse the svm_bo pointer and svm_bo_list head.
  	 */
-	while (!list_empty_careful(&prange->svm_bo_list))
-		;
+	while (!list_empty_careful(&prange->svm_bo_list) || prange->svm_bo)
+		schedule();
 
  	return false;
  }
#2 lore raw
Quoted 13 lines · Felix Kuehling
Quoted 6 lines · Xiaogang.Chen

From: Xiaogang Chen <[email protected]>

This patch fixes:
1: ref number of prange's svm_bo got decreased by an async call from hmm. When wait svm_bo of prange got released we shoul also wait prang->svm_bo become NULL, otherwise prange->svm_bo may be set to null after allocate new vram buffer.

I agree with this part.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

2: During waiting svm_bo of prange got released in a while loop should schedule current task to give other tasks oppotunity to run, specially the the workque task that handles svm_bo ref release, otherwise we may enter to softlock.

We had a similar discussion a few weeks back for another soft lock and I pointed to  cond_reschedule, which seems to be the preferred way to avoid soft locks in the kernel. Does cond_reschedule work for this case?

cond_resched() also works. I will send new one to use cond_resched() that is safer for schedule.

Regards

Xiaogang

Quoted 30 lines

Regards,
  Felix

Quoted 25 lines

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

  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -4
index bed0f8bf83c7..1074a4aedf57 100644
@@ -502,11 +502,11 @@ svm_range_validate_svm_bo(struct kfd_node 
 *node, struct svm_range *prange)
         /* We need a new svm_bo. Spin-loop to wait for concurrent
        * svm_range_bo_release to finish removing this range from
-     * its range list. After this, it is safe to reuse the
-     * svm_bo pointer and svm_bo_list head.
+     * its range list and set prange->svm_bo to null. After this,
+     * it is safe to reuse the svm_bo pointer and svm_bo_list head.
        */
-    while (!list_empty_careful(&prange->svm_bo_list))
-        ;
+    while (!list_empty_careful(&prange->svm_bo_list) || prange->svm_bo)
+        schedule();
         return false;
   }