All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/amdkfd: fix SVM GPU unmap bookkeeping on failed PTE invalidation
@ 2026-12-01 20:01 Eric Huang
  2026-12-01 20:20 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Huang @ 2026-12-01 20:01 UTC (permalink / raw)
  To: amd-gfx; +Cc: Philip.Yang, Eric Huang

svm_range_unmap_from_gpus() cleared bitmap_mapped for a GPU before
checking whether svm_range_unmap_from_gpu() actually succeeded. When
the GPU unmap failed (e.g. amdgpu_vm_ready() false because the VM was
being evicted), the bit was cleared anyway, so every later consumer of
bitmap_mapped (TLB flush decisions, svm_range_needs_unmap(), restore
and validate/map logic) believed this GPU had no mapping left, even
though its page table entries were still valid. The CPU pages behind
that SVM range can then be freed or reused while the GPU retains a
valid translation to them.

The failure was also invisible: svm_range_unmap_from_cpu(), called
from the MMU_NOTIFY_UNMAP path of the mmu_interval_notifier callback,
is void and discarded the int returned by svm_range_unmap_from_gpus().

Fix this by:
- Only clearing bitmap_mapped after svm_range_unmap_from_gpu()
  succeeds, so the bitmap keeps reflecting an unmap that didn't
  actually happen.
- Making svm_range_unmap_from_cpu() return the first failure from its
  svm_range_unmap_from_gpus() calls, and quiescing the process's
  queues on failure (the same fallback already used a few lines above
  for the "vital queue buffer" case), so GPU work can't run against
  the known-stale mapping before the next validate/restore cycle
  retries the unmap.
- Logging the failure from the notifier callback instead of silently
  dropping it.

Signed-off-by: Eric Huang <jinhuieric.huang@amd.com>
---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 63 ++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 17 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
index 32f2ca5b1fdfd..62e54153acab2 100644
--- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
@@ -1477,12 +1477,6 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
 	p = container_of(prange->svms, struct kfd_process, svms);
 
 	for_each_set_bit(gpuidx, bitmap_unmap, MAX_GPU_INSTANCE) {
-		if (prange->start == start && prange->last == last) {
-			pr_debug("unmap svms 0x%p prange 0x%p from gpu_idx 0x%x\n",
-				 prange->svms, prange, gpuidx);
-			clear_bit(gpuidx, prange->bitmap_mapped);
-		}
-
 		pdd = kfd_process_device_from_gpuidx(p, gpuidx);
 		if (!pdd) {
 			pr_debug("failed to find device idx %d\n", gpuidx);
@@ -1498,6 +1492,17 @@ svm_range_unmap_from_gpus(struct svm_range *prange, unsigned long start,
 		if (r)
 			break;
 
+		/* Only clear bitmap_mapped once the GPU unmap actually
+		 * succeeded. If svm_range_unmap_from_gpu() above failed
+		 * (e.g. VM not ready), the PTEs are still valid, so this
+		 * GPU must still be treated as mapped for this range.
+		 */
+		if (prange->start == start && prange->last == last) {
+			pr_debug("unmap svms 0x%p prange 0x%p from gpu_idx 0x%x\n",
+				 prange->svms, prange, gpuidx);
+			clear_bit(gpuidx, prange->bitmap_mapped);
+		}
+
 		if (fence) {
 			r = dma_fence_wait(fence, false);
 			dma_fence_put(fence);
@@ -2660,7 +2665,7 @@ svm_range_unmap_split(struct svm_range *parent, struct svm_range *prange, unsign
 	}
 }
 
-static void
+static int
 svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
 			 unsigned long start, unsigned long last)
 {
@@ -2670,20 +2675,21 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
 	struct kfd_process *p;
 	unsigned long s, l;
 	bool unmap_parent;
+	int r = 0, ret;
 
 	if (atomic_read(&prange->queue_refcount)) {
-		int r;
+		int qr;
 
 		pr_warn("Freeing queue vital buffer 0x%lx, queue evicted\n",
 			prange->start << PAGE_SHIFT);
-		r = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM);
-		if (r)
-			pr_debug("failed %d to quiesce KFD queues\n", r);
+		qr = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM);
+		if (qr)
+			pr_debug("failed %d to quiesce KFD queues\n", qr);
 	}
 
 	p = kfd_lookup_process_by_mm(mm);
 	if (!p)
-		return;
+		return -ESRCH;
 	svms = &p->svms;
 
 	pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] [0x%lx 0x%lx]\n", svms,
@@ -2697,17 +2703,37 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
 		mutex_lock_nested(&pchild->lock, 1);
 		s = max(start, pchild->start);
 		l = min(last, pchild->last);
-		if (l >= s)
-			svm_range_unmap_from_gpus(pchild, s, l, prange->bitmap_mapped, trigger);
+		if (l >= s) {
+			ret = svm_range_unmap_from_gpus(pchild, s, l, prange->bitmap_mapped, trigger);
+			if (ret)
+				r = ret;
+		}
 		svm_range_unmap_split(prange, pchild, start, last);
 		mutex_unlock(&pchild->lock);
 	}
 	s = max(start, prange->start);
 	l = min(last, prange->last);
-	if (l >= s)
-		svm_range_unmap_from_gpus(prange, s, l, prange->bitmap_mapped, trigger);
+	if (l >= s) {
+		ret = svm_range_unmap_from_gpus(prange, s, l, prange->bitmap_mapped, trigger);
+		if (ret)
+			r = ret;
+	}
 	svm_range_unmap_split(prange, prange, start, last);
 
+	if (r) {
+		/* GPU PTEs for this range could not be invalidated. Quiesce
+		 * the process's queues so GPU work can't run against the
+		 * stale mapping until the next validate/restore cycle
+		 * retries the unmap (bitmap_mapped is left set for any GPU
+		 * that failed above).
+		 */
+		pr_debug("failed %d to unmap svms 0x%p prange 0x%p [0x%lx 0x%lx] from gpu, quiescing queues\n",
+			r, svms, prange, start, last);
+		ret = kgd2kfd_quiesce_mm(mm, KFD_QUEUE_EVICTION_TRIGGER_SVM);
+		if (ret)
+			pr_debug("failed %d to quiesce KFD queues\n", ret);
+	}
+
 	if (unmap_parent)
 		svm_range_add_list_work(svms, prange, mm, SVM_OP_UNMAP_RANGE);
 	else
@@ -2716,6 +2742,7 @@ svm_range_unmap_from_cpu(struct mm_struct *mm, struct svm_range *prange,
 	schedule_deferred_list_work(svms);
 
 	kfd_unref_process(p);
+	return r;
 }
 
 /**
@@ -2767,7 +2794,9 @@ svm_range_cpu_invalidate_pagetables(struct mmu_interval_notifier *mni,
 
 	switch (range->event) {
 	case MMU_NOTIFY_UNMAP:
-		svm_range_unmap_from_cpu(mni->mm, prange, start, last);
+		if (svm_range_unmap_from_cpu(mni->mm, prange, start, last))
+			pr_warn("SVM failed to fully unmap svms 0x%p prange 0x%p [0x%lx 0x%lx] from GPU\n",
+				prange->svms, prange, start, last);
 		break;
 	default:
 		svm_range_evict(prange, mni->mm, start, last, range->event);
-- 
2.43.0


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

end of thread, other threads:[~2026-12-01 20:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 20:01 [PATCH] drm/amdkfd: fix SVM GPU unmap bookkeeping on failed PTE invalidation Eric Huang
2026-12-01 20:20 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.