Skip to contentSkip to conversation

drm/amdkfd: Don't call mmput from MMU notifier callback

PATCH In review Philip Yang 4 participants 6 messages Jun 23, 2025 – Jun 24, 2025

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

PATCH 1/1 drm/amdkfd: Don't call mmput from MMU notifier callback

If the process is exiting, the mmput inside mmu notifier callback from compactd or fork or numa balancing could release the last reference of mm struct to call exit_mmap and free_pgtable, this triggers deadlock with below backtrace.

The deadlock will leak kfd process as mmu notifier release is not called and cause VRAM leaking.

The fix is to take mm reference mmget_non_zero when adding prange to the deferred list to pair with mmput in deferred list work.

The backtrace of hung task:

 INFO: task python:348105 blocked for more than 64512 seconds.
 Call Trace:
  __schedule+0x1c3/0x550
  schedule+0x46/0xb0
  rwsem_down_write_slowpath+0x24b/0x4c0
  unlink_anon_vmas+0xb1/0x1c0
  free_pgtables+0xa9/0x130
  exit_mmap+0xbc/0x1a0
  mmput+0x5a/0x140
  svm_range_cpu_invalidate_pagetables+0x2b/0x40 [amdgpu]
  mn_itree_invalidate+0x72/0xc0
  __mmu_notifier_invalidate_range_start+0x48/0x60
  try_to_unmap_one+0x10fa/0x1400
  rmap_walk_anon+0x196/0x460
  try_to_unmap+0xbb/0x210
  migrate_page_unmap+0x54d/0x7e0
  migrate_pages_batch+0x1c3/0xae0
  migrate_pages_sync+0x98/0x240
  migrate_pages+0x25c/0x520
  compact_zone+0x29d/0x590
  compact_zone_order+0xb6/0xf0
  try_to_compact_pages+0xbe/0x220
  __alloc_pages_direct_compact+0x96/0x1a0
  __alloc_pages_slowpath+0x410/0x930
  __alloc_pages_nodemask+0x3a9/0x3e0
  do_huge_pmd_anonymous_page+0xd7/0x3e0
  __handle_mm_fault+0x5e3/0x5f0
  handle_mm_fault+0xf7/0x2e0
  hmm_vma_fault.isra.0+0x4d/0xa0
  walk_pmd_range.isra.0+0xa8/0x310
  walk_pud_range+0x167/0x240
  walk_pgd_range+0x55/0x100
  __walk_page_range+0x87/0x90
  walk_page_range+0xf6/0x160
  hmm_range_fault+0x4f/0x90
  amdgpu_hmm_range_get_pages+0x123/0x230 [amdgpu]
  amdgpu_ttm_tt_get_user_pages+0xb1/0x150 [amdgpu]
  init_user_pages+0xb1/0x2a0 [amdgpu]
  amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu+0x543/0x7d0 [amdgpu]
  kfd_ioctl_alloc_memory_of_gpu+0x24c/0x4e0 [amdgpu]
  kfd_ioctl+0x29d/0x500 [amdgpu]
Fixes fa582c6f3684 ("drm/amdkfd: Use mmget_not_zero in MMU notifier")
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +11 -12
index 10d1276f8e1c..5fe92f9a1ce1 100644
@@ -2392,15 +2392,17 @@ svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange,
 		    prange->work_item.op != SVM_OP_UNMAP_RANGE)
 			prange->work_item.op = op;
 	} else {
-		prange->work_item.op = op;
-
-		/* Pairs with mmput in deferred_list_work */
-		mmget(mm);
-		prange->work_item.mm = mm;
-		list_add_tail(&prange->deferred_list,
-			      &prange->svms->deferred_range_list);
-		pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
-			 prange, prange->start, prange->last, op);
+		/* Pairs with mmput in deferred_list_work.
+		 * If process is exiting and mm is gone, don't update mmu notifier.
+		 */
+		if (mmget_not_zero(mm)) {
+			prange->work_item.mm = mm;
+			prange->work_item.op = op;
+			list_add_tail(&prange->deferred_list,
+				      &prange->svms->deferred_range_list);
+			pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
+				 prange, prange->start, prange->last, op);
+		}
 	}
 	spin_unlock(&svms->deferred_list_lock);
 }
@@ -2568,8 +2570,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
 
 	if (range->event == MMU_NOTIFY_RELEASE)
 		return true;
-	if (!mmget_not_zero(mni->mm))
-		return true;
 
 	start = mni->interval_tree.start;
 	last = mni->interval_tree.last;
@@ -2596,7 +2596,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
 	}
 
 	svm_range_unlock(prange);
-	mmput(mni->mm);
 
 	return true;
 }
Signature · 1 line
2.49.0
#1 lore raw
Quoted 72 lines · Philip Yang

If the process is exiting, the mmput inside mmu notifier callback from compactd or fork or numa balancing could release the last reference of mm struct to call exit_mmap and free_pgtable, this triggers deadlock with below backtrace.

The deadlock will leak kfd process as mmu notifier release is not called and cause VRAM leaking.

The fix is to take mm reference mmget_non_zero when adding prange to the deferred list to pair with mmput in deferred list work.

The backtrace of hung task:

  INFO: task python:348105 blocked for more than 64512 seconds.
  Call Trace:
   __schedule+0x1c3/0x550
   schedule+0x46/0xb0
   rwsem_down_write_slowpath+0x24b/0x4c0
   unlink_anon_vmas+0xb1/0x1c0
   free_pgtables+0xa9/0x130
   exit_mmap+0xbc/0x1a0
   mmput+0x5a/0x140
   svm_range_cpu_invalidate_pagetables+0x2b/0x40 [amdgpu]
   mn_itree_invalidate+0x72/0xc0
   __mmu_notifier_invalidate_range_start+0x48/0x60
   try_to_unmap_one+0x10fa/0x1400
   rmap_walk_anon+0x196/0x460
   try_to_unmap+0xbb/0x210
   migrate_page_unmap+0x54d/0x7e0
   migrate_pages_batch+0x1c3/0xae0
   migrate_pages_sync+0x98/0x240
   migrate_pages+0x25c/0x520
   compact_zone+0x29d/0x590
   compact_zone_order+0xb6/0xf0
   try_to_compact_pages+0xbe/0x220
   __alloc_pages_direct_compact+0x96/0x1a0
   __alloc_pages_slowpath+0x410/0x930
   __alloc_pages_nodemask+0x3a9/0x3e0
   do_huge_pmd_anonymous_page+0xd7/0x3e0
   __handle_mm_fault+0x5e3/0x5f0
   handle_mm_fault+0xf7/0x2e0
   hmm_vma_fault.isra.0+0x4d/0xa0
   walk_pmd_range.isra.0+0xa8/0x310
   walk_pud_range+0x167/0x240
   walk_pgd_range+0x55/0x100
   __walk_page_range+0x87/0x90
   walk_page_range+0xf6/0x160
   hmm_range_fault+0x4f/0x90
   amdgpu_hmm_range_get_pages+0x123/0x230 [amdgpu]
   amdgpu_ttm_tt_get_user_pages+0xb1/0x150 [amdgpu]
   init_user_pages+0xb1/0x2a0 [amdgpu]
   amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu+0x543/0x7d0 [amdgpu]
   kfd_ioctl_alloc_memory_of_gpu+0x24c/0x4e0 [amdgpu]
   kfd_ioctl+0x29d/0x500 [amdgpu]
Fixes fa582c6f3684 ("drm/amdkfd: Use mmget_not_zero in MMU notifier")
---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 23 +++++++++++------------
  1 file changed, 11 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -3
index 10d1276f8e1c..5fe92f9a1ce1 100644
@@ -2392,15 +2392,17 @@ svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange,
  		    prange->work_item.op != SVM_OP_UNMAP_RANGE)
  			prange->work_item.op = op;
  	} else {
-		prange->work_item.op = op;
-
-		/* Pairs with mmput in deferred_list_work */

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange, · line 2398

Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -6
-		mmget(mm);
-		prange->work_item.mm = mm;
-		list_add_tail(&prange->deferred_list,
-			      &prange->svms->deferred_range_list);
-		pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
-			 prange, prange->start, prange->last, op);
+		/* Pairs with mmput in deferred_list_work.
+		 * If process is exiting and mm is gone, don't update mmu notifier.
+		 */
+		if (mmget_not_zero(mm)) {

If process is exiting we not need do schedule_deferred_list_work neither. I think this part code need be reorganized with mmget_not_zero(mm) check.

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
+			prange->work_item.mm = mm;
+			prange->work_item.op = op;
+			list_add_tail(&prange->deferred_list,
+				      &prange->svms->deferred_range_list);
+			pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
+				 prange, prange->start, prange->last, op);
+		}
  	}
  	spin_unlock(&svms->deferred_list_lock);
  }

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni, · line 2572

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -2
@@ -2568,8 +2570,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
 
  	if (range->event == MMU_NOTIFY_RELEASE)
  		return true;
-	if (!mmget_not_zero(mni->mm))
-		return true;

Why remove mmget_not_zero(mni->mm) /mmput(mni->mm) here? I think they are for different purpose from mmget_not_zero(mm) at svm_range_add_list_work.

Regards

Xiaogang

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -1
  	start = mni->interval_tree.start;
  	last = mni->interval_tree.last;
@@ -2596,7 +2596,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
  	}
 
  	svm_range_unlock(prange);
-	mmput(mni->mm);
 
  	return true;
  }
#2 lore raw
Quoted 78 lines · Xiaogang
Quoted 75 lines · Philip Yang

If the process is exiting, the mmput inside mmu notifier callback from compactd or fork or numa balancing could release the last reference of mm struct to call exit_mmap and free_pgtable, this triggers deadlock with below backtrace.

The deadlock will leak kfd process as mmu notifier release is not called and cause VRAM leaking.

The fix is to take mm reference mmget_non_zero when adding prange to the deferred list to pair with mmput in deferred list work.

The backtrace of hung task:

  INFO: task python:348105 blocked for more than 64512 seconds.
  Call Trace:
   __schedule+0x1c3/0x550
   schedule+0x46/0xb0
   rwsem_down_write_slowpath+0x24b/0x4c0
   unlink_anon_vmas+0xb1/0x1c0
   free_pgtables+0xa9/0x130
   exit_mmap+0xbc/0x1a0
   mmput+0x5a/0x140
   svm_range_cpu_invalidate_pagetables+0x2b/0x40 [amdgpu]
   mn_itree_invalidate+0x72/0xc0
   __mmu_notifier_invalidate_range_start+0x48/0x60
   try_to_unmap_one+0x10fa/0x1400
   rmap_walk_anon+0x196/0x460
   try_to_unmap+0xbb/0x210
   migrate_page_unmap+0x54d/0x7e0
   migrate_pages_batch+0x1c3/0xae0
   migrate_pages_sync+0x98/0x240
   migrate_pages+0x25c/0x520
   compact_zone+0x29d/0x590
   compact_zone_order+0xb6/0xf0
   try_to_compact_pages+0xbe/0x220
   __alloc_pages_direct_compact+0x96/0x1a0
   __alloc_pages_slowpath+0x410/0x930
   __alloc_pages_nodemask+0x3a9/0x3e0
   do_huge_pmd_anonymous_page+0xd7/0x3e0
   __handle_mm_fault+0x5e3/0x5f0
   handle_mm_fault+0xf7/0x2e0
   hmm_vma_fault.isra.0+0x4d/0xa0
   walk_pmd_range.isra.0+0xa8/0x310
   walk_pud_range+0x167/0x240
   walk_pgd_range+0x55/0x100
   __walk_page_range+0x87/0x90
   walk_page_range+0xf6/0x160
   hmm_range_fault+0x4f/0x90
   amdgpu_hmm_range_get_pages+0x123/0x230 [amdgpu]
   amdgpu_ttm_tt_get_user_pages+0xb1/0x150 [amdgpu]
   init_user_pages+0xb1/0x2a0 [amdgpu]
   amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu+0x543/0x7d0 [amdgpu]
   kfd_ioctl_alloc_memory_of_gpu+0x24c/0x4e0 [amdgpu]
   kfd_ioctl+0x29d/0x500 [amdgpu]
Fixes fa582c6f3684 ("drm/amdkfd: Use mmget_not_zero in MMU notifier")
---
  drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 23 +++++++++++------------
  1 file changed, 11 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -6
index 10d1276f8e1c..5fe92f9a1ce1 100644
@@ -2392,15 +2392,17 @@ svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange,
  		    prange->work_item.op != SVM_OP_UNMAP_RANGE)
  			prange->work_item.op = op;
  	} else {
-		prange->work_item.op = op;
-
-		/* Pairs with mmput in deferred_list_work */
-		mmget(mm);
-		prange->work_item.mm = mm;
-		list_add_tail(&prange->deferred_list,
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange, · line 2398

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -3
-			      &prange->svms->deferred_range_list);
-		pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
-			 prange, prange->start, prange->last, op);
+		/* Pairs with mmput in deferred_list_work.
+		 * If process is exiting and mm is gone, don't update mmu notifier.
+		 */
+		if (mmget_not_zero(mm)) {

If process is exiting we not need do schedule_deferred_list_work neither. I think this part code need be reorganized with mmget_not_zero(mm) check.

yes, that is right, we could change svm_range_add_list_work to return true only if new work_item is added to the deferred list, and schedule deferred work. This optimization could be another patch, not related to the deadlock issue.

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
+			prange->work_item.mm = mm;
+			prange->work_item.op = op;
+			list_add_tail(&prange->deferred_list,
+				      &prange->svms->deferred_range_list);
+			pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
+				 prange, prange->start, prange->last, op);
+		}
  	}
  	spin_unlock(&svms->deferred_list_lock);
  }
@@ -2568,8 +2570,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
 
Quoted 7 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni, · line 2572

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -2
  	if (range->event == MMU_NOTIFY_RELEASE)
  		return true;
-	if (!mmget_not_zero(mni->mm))
-		return true;

Why remove mmget_not_zero(mni->mm) /mmput(mni->mm) here? I think they are for different purpose from mmget_not_zero(mm) at svm_range_add_list_work.

Yes, the mmget_non_zero/mmput inside MMU notifier was added to fix another issue that deferred work access invalid mm, to ensure deferred work_item.mm hold mm reference. But in the backtrace case, mmput inside MMU notifier callback release the last reference and cause deadlock.

Regards,

Philip

Quoted 15 lines

Regards

Xiaogang

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -1
  	start = mni->interval_tree.start;
  	last = mni->interval_tree.last;
@@ -2596,7 +2596,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
  	}
 
  	svm_range_unlock(prange);
-	mmput(mni->mm);
 
  	return true;
  }
#3 lore raw
Quoted 111 lines · Philip Yang
Quoted 80 lines · Xiaogang
Quoted 77 lines · Philip Yang

If the process is exiting, the mmput inside mmu notifier callback from compactd or fork or numa balancing could release the last reference of mm struct to call exit_mmap and free_pgtable, this triggers deadlock with below backtrace.

The deadlock will leak kfd process as mmu notifier release is not called and cause VRAM leaking.

The fix is to take mm reference mmget_non_zero when adding prange to the deferred list to pair with mmput in deferred list work.

The backtrace of hung task:

  INFO: task python:348105 blocked for more than 64512 seconds.
  Call Trace:
   __schedule+0x1c3/0x550
   schedule+0x46/0xb0
   rwsem_down_write_slowpath+0x24b/0x4c0
   unlink_anon_vmas+0xb1/0x1c0
   free_pgtables+0xa9/0x130
   exit_mmap+0xbc/0x1a0
   mmput+0x5a/0x140
   svm_range_cpu_invalidate_pagetables+0x2b/0x40 [amdgpu]
   mn_itree_invalidate+0x72/0xc0
   __mmu_notifier_invalidate_range_start+0x48/0x60
   try_to_unmap_one+0x10fa/0x1400
   rmap_walk_anon+0x196/0x460
   try_to_unmap+0xbb/0x210
   migrate_page_unmap+0x54d/0x7e0
   migrate_pages_batch+0x1c3/0xae0
   migrate_pages_sync+0x98/0x240
   migrate_pages+0x25c/0x520
   compact_zone+0x29d/0x590
   compact_zone_order+0xb6/0xf0
   try_to_compact_pages+0xbe/0x220
   __alloc_pages_direct_compact+0x96/0x1a0
   __alloc_pages_slowpath+0x410/0x930
   __alloc_pages_nodemask+0x3a9/0x3e0
   do_huge_pmd_anonymous_page+0xd7/0x3e0
   __handle_mm_fault+0x5e3/0x5f0
   handle_mm_fault+0xf7/0x2e0
   hmm_vma_fault.isra.0+0x4d/0xa0
   walk_pmd_range.isra.0+0xa8/0x310
   walk_pud_range+0x167/0x240
   walk_pgd_range+0x55/0x100
   __walk_page_range+0x87/0x90
   walk_page_range+0xf6/0x160
   hmm_range_fault+0x4f/0x90
   amdgpu_hmm_range_get_pages+0x123/0x230 [amdgpu]
   amdgpu_ttm_tt_get_user_pages+0xb1/0x150 [amdgpu]
   init_user_pages+0xb1/0x2a0 [amdgpu]
   amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu+0x543/0x7d0 [amdgpu]
   kfd_ioctl_alloc_memory_of_gpu+0x24c/0x4e0 [amdgpu]
   kfd_ioctl+0x29d/0x500 [amdgpu]
Fixes fa582c6f3684 ("drm/amdkfd: Use mmget_not_zero in MMU notifier")

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

  1 file changed, 11 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -7
index 10d1276f8e1c..5fe92f9a1ce1 100644
@@ -2392,15 +2392,17 @@ svm_range_add_list_work(struct 
 svm_range_list *svms, struct svm_range *prange,
               prange->work_item.op != SVM_OP_UNMAP_RANGE)
               prange->work_item.op = op;
       } else {
-        prange->work_item.op = op;
-
-        /* Pairs with mmput in deferred_list_work */
-        mmget(mm);
-        prange->work_item.mm = mm;
-        list_add_tail(&prange->deferred_list,
- &prange->svms->deferred_range_list);
Quoted 8 lines
Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +2 -2
-        pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
-             prange, prange->start, prange->last, op);
+        /* Pairs with mmput in deferred_list_work.
+         * If process is exiting and mm is gone, don't update mmu 

notifier.
+         */
+        if (mmget_not_zero(mm)) {

If process is exiting we not need do schedule_deferred_list_work neither. I think this part code need be reorganized with mmget_not_zero(mm) check.

yes, that is right, we could change svm_range_add_list_work to return true only if new work_item is added to the deferred list, and schedule deferred work. This optimization could be another patch, not related to the deadlock issue.

Quoted 11 lines
Quoted 11 lines

+            prange->work_item.mm = mm;
+            prange->work_item.op = op;
+            list_add_tail(&prange->deferred_list,
+ &prange->svms->deferred_range_list);
+            pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
+                 prange, prange->start, prange->last, op); +        }
      }
      spin_unlock(&svms->deferred_list_lock);
  }

Quoted 9 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_cpu_invalidate_pagetables(struct · line 2572

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -2
@@ -2568,8 +2570,6 @@ svm_range_cpu_invalidate_pagetables(struct 
 mmu_interval_notifier *mni,
         if (range->event == MMU_NOTIFY_RELEASE)
           return true;
-    if (!mmget_not_zero(mni->mm))
-        return true;

Why remove mmget_not_zero(mni->mm) /mmput(mni->mm) here? I think they are for different purpose from mmget_not_zero(mm) at svm_range_add_list_work.

Quoted 1 line

Yes, the mmget_non_zero/mmput inside MMU notifier was added to fix another issue that deferred work access invalid mm, to ensure deferred work_item.mm hold mm reference. But in the backtrace case, mmput inside MMU notifier callback release the last reference and cause deadlock.

You have used mmget_not_zero at svm_range_add_list_work to prevent this deadlock. So mmput inside MMU notifier callback should not cause deadlock now. mmget_non_zero/mmput inside MMU notifier is used at up level to prevent invalid mm being used in all cases. They should be still there.

Regards

Xiaogang

Quoted 18 lines

Regards,

Philip

Quoted 13 lines

Regards

Xiaogang

Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -1
         start = mni->interval_tree.start;
       last = mni->interval_tree.last;
@@ -2596,7 +2596,6 @@ svm_range_cpu_invalidate_pagetables(struct 
 mmu_interval_notifier *mni,
       }
         svm_range_unlock(prange);
-    mmput(mni->mm);
         return true;
   }
maintainer
#4 lore raw
Quoted 109 lines · Xiaogang
Quoted 107 lines · Philip Yang
Quoted 78 lines · Xiaogang
Quoted 75 lines · Philip Yang

If the process is exiting, the mmput inside mmu notifier callback from compactd or fork or numa balancing could release the last reference of mm struct to call exit_mmap and free_pgtable, this triggers deadlock with below backtrace.

The deadlock will leak kfd process as mmu notifier release is not called and cause VRAM leaking.

The fix is to take mm reference mmget_non_zero when adding prange to the deferred list to pair with mmput in deferred list work.

The backtrace of hung task:

  INFO: task python:348105 blocked for more than 64512 seconds.
  Call Trace:
   __schedule+0x1c3/0x550
   schedule+0x46/0xb0
   rwsem_down_write_slowpath+0x24b/0x4c0
   unlink_anon_vmas+0xb1/0x1c0
   free_pgtables+0xa9/0x130
   exit_mmap+0xbc/0x1a0
   mmput+0x5a/0x140
   svm_range_cpu_invalidate_pagetables+0x2b/0x40 [amdgpu]
   mn_itree_invalidate+0x72/0xc0
   __mmu_notifier_invalidate_range_start+0x48/0x60
   try_to_unmap_one+0x10fa/0x1400
   rmap_walk_anon+0x196/0x460
   try_to_unmap+0xbb/0x210
   migrate_page_unmap+0x54d/0x7e0
   migrate_pages_batch+0x1c3/0xae0
   migrate_pages_sync+0x98/0x240
   migrate_pages+0x25c/0x520
   compact_zone+0x29d/0x590
   compact_zone_order+0xb6/0xf0
   try_to_compact_pages+0xbe/0x220
   __alloc_pages_direct_compact+0x96/0x1a0
   __alloc_pages_slowpath+0x410/0x930
   __alloc_pages_nodemask+0x3a9/0x3e0
   do_huge_pmd_anonymous_page+0xd7/0x3e0
   __handle_mm_fault+0x5e3/0x5f0
   handle_mm_fault+0xf7/0x2e0
   hmm_vma_fault.isra.0+0x4d/0xa0
   walk_pmd_range.isra.0+0xa8/0x310
   walk_pud_range+0x167/0x240
   walk_pgd_range+0x55/0x100
   __walk_page_range+0x87/0x90
   walk_page_range+0xf6/0x160
   hmm_range_fault+0x4f/0x90
   amdgpu_hmm_range_get_pages+0x123/0x230 [amdgpu]
   amdgpu_ttm_tt_get_user_pages+0xb1/0x150 [amdgpu]
   init_user_pages+0xb1/0x2a0 [amdgpu]
   amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu+0x543/0x7d0 [amdgpu]
   kfd_ioctl_alloc_memory_of_gpu+0x24c/0x4e0 [amdgpu]
   kfd_ioctl+0x29d/0x500 [amdgpu]
Fixes fa582c6f3684 ("drm/amdkfd: Use mmget_not_zero in MMU notifier")

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

  1 file changed, 11 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -6
index 10d1276f8e1c..5fe92f9a1ce1 100644
@@ -2392,15 +2392,17 @@ svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange,
               prange->work_item.op != SVM_OP_UNMAP_RANGE)
               prange->work_item.op = op;
       } else {
-        prange->work_item.op = op;
-
-        /* Pairs with mmput in deferred_list_work */
-        mmget(mm);
-        prange->work_item.mm = mm;
-        list_add_tail(&prange->deferred_list,
Quoted 8 lines

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange, · line 2398

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -3
- &prange->svms->deferred_range_list);
-        pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
-             prange, prange->start, prange->last, op);
+        /* Pairs with mmput in deferred_list_work.
+         * If process is exiting and mm is gone, don't update mmu notifier.
+         */
+        if (mmget_not_zero(mm)) {

If process is exiting we not need do schedule_deferred_list_work neither. I think this part code need be reorganized with mmget_not_zero(mm) check.

yes, that is right, we could change svm_range_add_list_work to return true only if new work_item is added to the deferred list, and schedule deferred work. This optimization could be another patch, not related to the deadlock issue.

Quoted 18 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +7 -0
+            prange->work_item.mm = mm;
+            prange->work_item.op = op;
+            list_add_tail(&prange->deferred_list,
+ &prange->svms->deferred_range_list);
+            pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
+                 prange, prange->start, prange->last, op);
+        }
       }
       spin_unlock(&svms->deferred_list_lock);
   }

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni, · line 2571

Quoted 4 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -2
@@ -2568,8 +2570,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
         if (range->event == MMU_NOTIFY_RELEASE)
           return true;
-    if (!mmget_not_zero(mni->mm))
-        return true;

Why remove mmget_not_zero(mni->mm) /mmput(mni->mm) here? I think they are for different purpose from mmget_not_zero(mm) at svm_range_add_list_work.

Quoted 3 lines
Quoted 1 line

Yes, the mmget_non_zero/mmput inside MMU notifier was added to fix another issue that deferred work access invalid mm, to ensure deferred work_item.mm hold mm reference. But in the backtrace case, mmput inside MMU notifier callback release the last reference and cause deadlock.

You have used mmget_not_zero at svm_range_add_list_work to prevent this deadlock. So mmput inside MMU notifier callback should not cause deadlock now. mmget_non_zero/mmput inside MMU notifier is used at up level to prevent invalid mm being used in all cases. They should be still there.

No, it's OK. We can use the mm pointer in the MMU notifier without calling mmget or mmget_not_zero, even when the refcount is 0. There are two refcounts in the mm_struct. The mmget/mmput refcount represents the lifetime of the address space. There is also mmgrab/mmdrop that protects the data structure itself from being freed. That refcount must be held by the caller of the MMU notifier. Otherwise they couldn't rely on the mm pointer themselves.

We need to take another mm reference whenever we store an mm pointer in our own data structures, such as a work item. Philip's change accomplishes that by only taking mmget_not_zero where we pass the mm pointer into a work item, and dropping the reference in the work item itself.

Regards,
  Felix

Quoted 22 lines

Regards

Xiaogang

Quoted 17 lines

Regards,

Philip

Quoted 12 lines

Regards

Xiaogang

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -1
         start = mni->interval_tree.start;
       last = mni->interval_tree.last;
@@ -2596,7 +2596,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
       }
         svm_range_unlock(prange);
-    mmput(mni->mm);
         return true;
   }
maintainer Reviewed-by on v1 1/1
#5 lore raw
In reply to Philip Yang
Quoted 93 lines · Philip Yang

If the process is exiting, the mmput inside mmu notifier callback from compactd or fork or numa balancing could release the last reference of mm struct to call exit_mmap and free_pgtable, this triggers deadlock with below backtrace.

The deadlock will leak kfd process as mmu notifier release is not called and cause VRAM leaking.

The fix is to take mm reference mmget_non_zero when adding prange to the deferred list to pair with mmput in deferred list work.

The backtrace of hung task:

 INFO: task python:348105 blocked for more than 64512 seconds.
 Call Trace:
  __schedule+0x1c3/0x550
  schedule+0x46/0xb0
  rwsem_down_write_slowpath+0x24b/0x4c0
  unlink_anon_vmas+0xb1/0x1c0
  free_pgtables+0xa9/0x130
  exit_mmap+0xbc/0x1a0
  mmput+0x5a/0x140
  svm_range_cpu_invalidate_pagetables+0x2b/0x40 [amdgpu]
  mn_itree_invalidate+0x72/0xc0
  __mmu_notifier_invalidate_range_start+0x48/0x60
  try_to_unmap_one+0x10fa/0x1400
  rmap_walk_anon+0x196/0x460
  try_to_unmap+0xbb/0x210
  migrate_page_unmap+0x54d/0x7e0
  migrate_pages_batch+0x1c3/0xae0
  migrate_pages_sync+0x98/0x240
  migrate_pages+0x25c/0x520
  compact_zone+0x29d/0x590
  compact_zone_order+0xb6/0xf0
  try_to_compact_pages+0xbe/0x220
  __alloc_pages_direct_compact+0x96/0x1a0
  __alloc_pages_slowpath+0x410/0x930
  __alloc_pages_nodemask+0x3a9/0x3e0
  do_huge_pmd_anonymous_page+0xd7/0x3e0
  __handle_mm_fault+0x5e3/0x5f0
  handle_mm_fault+0xf7/0x2e0
  hmm_vma_fault.isra.0+0x4d/0xa0
  walk_pmd_range.isra.0+0xa8/0x310
  walk_pud_range+0x167/0x240
  walk_pgd_range+0x55/0x100
  __walk_page_range+0x87/0x90
  walk_page_range+0xf6/0x160
  hmm_range_fault+0x4f/0x90
  amdgpu_hmm_range_get_pages+0x123/0x230 [amdgpu]
  amdgpu_ttm_tt_get_user_pages+0xb1/0x150 [amdgpu]
  init_user_pages+0xb1/0x2a0 [amdgpu]
  amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu+0x543/0x7d0 [amdgpu]
  kfd_ioctl_alloc_memory_of_gpu+0x24c/0x4e0 [amdgpu]
  kfd_ioctl+0x29d/0x500 [amdgpu]
Fixes fa582c6f3684 ("drm/amdkfd: Use mmget_not_zero in MMU notifier")
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +11 -9
index 10d1276f8e1c..5fe92f9a1ce1 100644
@@ -2392,15 +2392,17 @@ svm_range_add_list_work(struct svm_range_list *svms, struct svm_range *prange,
 		    prange->work_item.op != SVM_OP_UNMAP_RANGE)
 			prange->work_item.op = op;
 	} else {
-		prange->work_item.op = op;
-
-		/* Pairs with mmput in deferred_list_work */
-		mmget(mm);
-		prange->work_item.mm = mm;
-		list_add_tail(&prange->deferred_list,
-			      &prange->svms->deferred_range_list);
-		pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
-			 prange, prange->start, prange->last, op);
+		/* Pairs with mmput in deferred_list_work.
+		 * If process is exiting and mm is gone, don't update mmu notifier.
+		 */
+		if (mmget_not_zero(mm)) {
+			prange->work_item.mm = mm;
+			prange->work_item.op = op;
+			list_add_tail(&prange->deferred_list,
+				      &prange->svms->deferred_range_list);
+			pr_debug("add prange 0x%p [0x%lx 0x%lx] to work list op %d\n",
+				 prange, prange->start, prange->last, op);
+		}
 	}
 	spin_unlock(&svms->deferred_list_lock);
 }

drivers/gpu/drm/amd/amdkfd/kfd_svm.c svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni, · line 2572

Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -2
@@ -2568,8 +2570,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
 
 	if (range->event == MMU_NOTIFY_RELEASE)
 		return true;
-	if (!mmget_not_zero(mni->mm))
-		return true;

I looked for other uses of mm inside svm_range_cpu_invalidate_pagetables, and I found this call chain: svm_range_cpu_invalidate_pagetables -> svm_range_unmap_from_cpu -> svm_range_unmap_split -> svm_range_add_child. It currently assigns mm to a work item without taking a reference count. But it seems that mm isn't even used later. So maybe we should just assign

    pchild->work_item.mm = NULL;

Then we could also remove the mm parameter from svm_range_unmap_split and svm_range_add_child.

With that fixed, the patch is

Quoted 11 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0 -1
 	start = mni->interval_tree.start;
 	last = mni->interval_tree.last;
@@ -2596,7 +2596,6 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
 	}
 
 	svm_range_unlock(prange);
-	mmput(mni->mm);
 
 	return true;
 }