Skip to contentSkip to conversation

drm/amdkfd: fix wedged teardown on surprise removal

PATCH Superseded Olivier Kaloudoff 1 participant 4 messages Oct 15

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover drm/amdkfd: fix wedged teardown on surprise removal

On surprise removal of a GPU behind Thunderbolt (MI50, PCIe slot power cut), the KFD teardown path wedges the host:

- the killed KFD process faults on its own SVM ranges during exit / coredump (SIGBUS -> get_dump_page -> do_swap_page -> svm_migrate_to_ram), submits SDMA work to an already-killed drm_sched entity, and waits uninterruptibly on a fence that can never signal;
- the dying task holds svms->lock, which wedges svm_range_restore_work() and prevents kfd_processes_count from draining;
- kgd2kfd_teardown_processes() therefore spins forever in cond_resched() while holding the pciehp removal thread, producing soft-lockup and hung-task splats.

This series:

1. tears KFD processes down in amdgpu_device_fini_hw() *before* amdgpu_amdkfd_device_fini_sw() frees kfd.dev on disconnect, so the drain happens while kfd.dev is still valid, and guards the second kgd2kfd_teardown_processes() call against kfd.dev already being gone;

2. makes the userptr and SVM range restore workers stop rescheduling themselves on -ENODEV;

3. guards the hardware-accessing SVM migration sections with drm_dev_enter()/drm_dev_exit() and bounds the job fence wait with AMDGPU_FENCE_JIFFIES_TIMEOUT, so a migration cannot race with unplug and a fence on a dead device cannot stall the critical section or the faulting task indefinitely. A failed copy now also rolls back migrate->dst and propagates the error instead of completing as a successful migration.

Verified on MI50 behind Thunderbolt (2 GB SVM workload, PCIe slot power cut): the workload takes SIGBUS, coredump completes, kfd_processes_count drains in ~9s, pciehp removal finishes without warnings, and the device re-enumerates cleanly on slot re-power. Previously the same repro wedged the host every time.

Changes in v6:
- patch 3: check svm_migrate_copy_done() return in both callers; on failure release dst pages, clear migrate->dst before finalize, and propagate the error (Felix Kuehling)
- patch 3: dropped the dead ret < 0 branch (Christian König)

Changes in v5:
- patch 3: dropped the drm_dev_is_unplugged() and pci_dev_is_disconnected() checks entirely; drm_dev_enter()/exit() plus the bounded fence wait is sufficient (Felix Kuehling)

Changes in v4:
- patch 3: AMDGPU_FENCE_JIFFIES_TIMEOUT instead of an open-coded 10s; drm_dev_enter()/exit() guarding the whole migration section including the bounded wait (Felix Kuehling)
- patch 2: folded in the svm_range_restore_work() -ENODEV change; fixed a latent uninitialized ret read on the evicted_bos-mismatch and confirm_valid-WARN paths

Changes in v3:
- patch 1: NULL guard for the second teardown call (Christian König)
- patch 3: new, fail fast on gone device in SVM migration paths

Changes in v2:
- patch 1: drain before fini_sw instead of bounding the wait (Christian König)
- patch 2: new, stop restore retries on unplug

Cc Christian König <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Xiaogang Chen <[email protected]>

Olivier Kaloudoff (3):
drm/amdkfd: tear down KFD processes before early fini_sw on disconnect drm/amdkfd: stop restore retries on device unplug drm/amdkfd: bound SVM migration fence wait and guard with drm_dev_enter

 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c        |  5 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c      | 29 ++++--
 drivers/gpu/drm/amd/amdkfd/kfd_device.c           |  9 +-
 drivers/gpu/drm/amd/amdkfd/kfd_migrate.c          | 97 ++++++++++++++---
 drivers/gpu/drm/amd/amdkfd/kfd_svm.c              |  2 +-
 5 files changed, 113 insertions(+), 29 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH v6 1/3 drm/amdkfd: tear down KFD processes before early fini_sw on disconnect

On surprise removal, amdgpu_device_fini_hw() calls amdgpu_amdkfd_device_fini_sw() when pci_dev_is_disconnected() is true. fini_sw frees kfd.dev, but KFD processes may still be alive and hold references to it: kfd_process_destroy() dereferences kfd.dev via kfd_processes_remove(), and svm_range_list_fini() / kfd_process_free_outstanding_kfd_bos() touch per-device SVM state.

The kfd_processes_count wait only happens later in kgd2kfd_teardown_processes(), called from the top-half remove path well after fini_sw. In the observed failure the killed KFD process (SIGBUS on unplug) stuck in D state on its exit path, so kfd_processes_count never drained and the PCIe removal thread wedged permanently in cond_resched(), producing soft-lockup and hung-task splats.

Tear KFD processes down before freeing kfd.dev on the disconnect path, and guard kgd2kfd_teardown_processes() against kfd.dev already being gone when it runs a second time.

---
Changes in v6: none
Changes in v5: none
Changes in v4: none
Changes in v3:
- added NULL guard for the second teardown_processes call once kfd.dev is already freed (Christian König)
- dropped the bounded-wait mechanism from v1; the drain now happens before fini_sw instead (Christian König)
Changes in v2: none
---

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -1
index 9269e78..bd9d73a 100644
@@ -4315,8 +4315,11 @@ void amdgpu_device_fini_hw(struct amdgpu_device *adev)
 	 * before ip_fini_early to prevent kfd locking refcount issues by calling
 	 * amdgpu_amdkfd_suspend()
 	 */
-	if (pci_dev_is_disconnected(adev->pdev))
+	if (pci_dev_is_disconnected(adev->pdev)) {
+		/* Drain KFD processes while kfd.dev is still valid */
+		amdgpu_amdkfd_teardown_processes(adev);
 		amdgpu_amdkfd_device_fini_sw(adev);
+	}
 
 	amdgpu_coredump_fini(adev);
 	amdgpu_device_ip_fini_early(adev);
drivers/gpu/drm/amd/amdkfd/kfd_device.c +8 -1
index 586e640..43029b2 100644
@@ -1801,7 +1801,14 @@ void kgd2kfd_teardown_processes(struct amdgpu_device *adev)
 
 	mutex_unlock(&kfd_processes_mutex);
 
-	/* wait all kfd processes use adev terminate */
+	/* kfd.dev may already be freed on surprise removal */
+	if (!adev->kfd.dev)
+		return;
+
+	/*
+	 * Wait for all KFD processes using adev to terminate. Must be
+	 * unbounded: device state is freed once they are gone.
+	 */
 	while (!!atomic_read(&adev->kfd.dev->kfd_processes_count))
 		cond_resched();
 }
Signature · 1 line
2.47.3
#2 lore raw

PATCH v6 2/3 drm/amdkfd: stop restore retries on device unplug

In reply to Olivier Kaloudoff

amdgpu_amdkfd_restore_userptr_worker() and svm_range_restore_work() reschedule themselves when validation fails with evicted objects pending. After a surprise removal the restore paths fail with -ENODEV permanently, so rescheduling just spins the worker uselessly until cancel_delayed_work_sync() in fini finally stops it - which can take a while, since process teardown itself is delayed by the dead device.

Treat -ENODEV as terminal in both workers and also stop setting evicted_bos after it.

---
Changes in v6: none
Changes in v5: none
Changes in v4:
- moved the svm_range_restore_work() change here from the SVM migration patch (Felix Kuehling)
- assign ret on the evicted_bos-mismatch and confirm_valid-WARN paths, fixing a latent uninitialized read introduced in v2
Changes in v3:
- retitled; now covers both restore workers
Changes in v2:
- new
---

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +13 -5
index d668816..2ad6777 100644
@@ -2827,6 +2827,7 @@ static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work)
 	struct task_struct *usertask;
 	struct mm_struct *mm;
 	uint32_t evicted_bos;
+	int ret;
 
 	mutex_lock(&process_info->notifier_lock);
 	evicted_bos = process_info->evicted_bos;
@@ -2846,14 +2847,16 @@ static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work)
 
 	mutex_lock(&process_info->lock);
 
-	if (update_invalid_user_pages(process_info, mm))
+	ret = update_invalid_user_pages(process_info, mm);
+	if (ret)
 		goto unlock_out;
 	/* userptr_inval_list can be empty if all evicted userptr BOs
 	 * have been freed. In that case there is nothing to validate
 	 * and we can just restart the queues.
 	 */
 	if (!list_empty(&process_info->userptr_inval_list)) {
-		if (validate_invalid_user_pages(process_info))
+		ret = validate_invalid_user_pages(process_info);
+		if (ret)
 			goto unlock_out;
 	}
 	/* Final check for concurrent evicton and atomic update. If
@@ -2862,11 +2865,14 @@ static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work)
 	 * reference counting inside KFD will handle this case.
 	 */
 	mutex_lock(&process_info->notifier_lock);
-	if (process_info->evicted_bos != evicted_bos)
+	if (process_info->evicted_bos != evicted_bos) {
+		ret = -EAGAIN;
 		goto unlock_notifier_out;
+	}
 
 	if (confirm_valid_user_pages_locked(process_info)) {
 		WARN(1, "User pages unexpectedly invalid");
+		ret = -EINVAL;
 		goto unlock_notifier_out;
 	}
 
@@ -2884,8 +2890,10 @@ unlock_notifier_out:
 unlock_out:
 	mutex_unlock(&process_info->lock);
 
-	/* If validation failed, reschedule another attempt */
-	if (evicted_bos) {
+	/* If validation failed, reschedule another attempt, unless the
+	 * device is gone for good
+	 */
+	if (evicted_bos && ret != -ENODEV) {
 		queue_delayed_work(system_freezable_wq,
 			&process_info->restore_userptr_work,
 			msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +4 -2
index fa4054d..3dab456 100644
@@ -2013,8 +2013,10 @@ out_reschedule:
 	mmap_write_unlock(mm);
 	mutex_unlock(&process_info->lock);
 
-	/* If validation failed, reschedule another attempt */
-	if (evicted_ranges) {
+	/* If validation failed, reschedule another attempt, unless the
+	 * device is gone for good
+	 */
+	if (evicted_ranges && r != -ENODEV) {
 		pr_debug("reschedule to restore svm range\n");
 		queue_delayed_work(system_freezable_wq, &svms->restore_work,
 			msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS));
Signature · 1 line
2.47.3
#3 lore raw

PATCH v6 3/3 drm/amdkfd: bound SVM migration fence wait and guard with drm_dev_enter

In reply to Olivier Kaloudoff

On surprise removal the killed process's own exit path can fault on its SVM managed ranges: SIGBUS -> coredump -> get_dump_page() -> __get_user_pages() -> do_swap_page() -> svm_migrate_to_ram(). Without protection, svm_migrate_vram_to_ram() submits an SDMA copy job to an already-killed drm_sched entity (observed as "drm_sched_rq_add_entity: Trying to push to a killed entity" plus refcount_t saturation warnings), then svm_migrate_copy_done() waits uninterruptibly on a job fence that can never signal. The task dies in D state holding svms->lock, which wedges svm_range_restore_work() on the rwsem writer side, so kfd_processes_count never drains and the pciehp removal thread wedges for good.

Guard the hardware-accessing migration sections with drm_dev_enter()/drm_dev_exit() so unplug cannot proceed while a migration submits jobs and waits on them, and bound the job fence wait in svm_migrate_copy_done() with AMDGPU_FENCE_JIFFIES_TIMEOUT so a fence that can never signal does not stall the critical section (or the faulting task) indefinitely.

Handle the new failure case properly: if the copy times out or fails, release the migration pages and clear migrate->dst before migrate_vma_finalize() so a failed copy cannot complete as a successful migration, and propagate the error to the caller. The cleanup is shared with the copy functions' existing error paths via new svm_migrate_vram_pages_put() / svm_migrate_ram_pages_put() helpers.

Verified on MI50 behind Thunderbolt: the workload takes SIGBUS, coredump completes, kfd_processes_count drains in ~9s, pciehp removal finishes, and the device re-enumerates cleanly on slot re-power. Previously the same repro wedged the host.

---
Changes in v6:
- check svm_migrate_copy_done() return in both callers: on failure release dst pages, clear migrate->dst before finalize, propagate the error (Felix Kuehling)
- drop the dead ret < 0 branch; a non-interruptible wait can only return 0 on timeout (Christian König)

Changes in v5:
- dropped drm_dev_is_unplugged()/pci_dev_is_disconnected() checks; drm_dev_enter()/exit() alone is sufficient (Felix Kuehling)

Changes in v4:
- use AMDGPU_FENCE_JIFFIES_TIMEOUT instead of an open-coded 10s (Felix Kuehling)
- guard migration sections with drm_dev_enter()/exit() instead of racing flag checks (Felix Kuehling)
- keep pci_dev_is_disconnected() early-out for the dead-link window
- moved svm_range_restore_work() change to patch 2

Changes in v3:
- new
---

drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +79 -28
index 8b96e1f..40e993a 100644
@@ -204,7 +204,12 @@ svm_migrate_copy_done(struct amdgpu_device *adev, struct dma_fence *mfence)
 	int r = 0;
 
 	if (mfence) {
-		r = dma_fence_wait(mfence, false);
+		/* Bound the wait: on a dying device the fence may never
+		 * signal.
+		 */
+		if (!dma_fence_wait_timeout(mfence, false,
+					    AMDGPU_FENCE_JIFFIES_TIMEOUT))
+			r = -ETIMEDOUT;
 		dma_fence_put(mfence);
 		pr_debug("sdma copy memory fence done\n");
 	}
@@ -239,6 +244,24 @@ svm_migrate_put_vram_page(struct amdgpu_device *adev, unsigned long addr)
 	put_page(page);
 }
 
+/* Release VRAM dst pages of a failed migration and clear migrate->dst
+ * so migrate_vma_finalize() leaves the source pages unmigrated.
+ */
+static void
+svm_migrate_vram_pages_put(struct amdgpu_device *adev,
+			   struct migrate_vma *migrate, u64 *dst, u64 npages)
+{
+	u64 i;
+
+	for (i = 0; i < npages; i++) {
+		if (!dst[i])
+			continue;
+		svm_migrate_put_vram_page(adev, dst[i]);
+		dst[i] = 0;
+		migrate->dst[i] = 0;
+	}
+}
+
 static unsigned long
 svm_migrate_addr(struct amdgpu_device *adev, struct page *page)
 {
@@ -359,13 +382,7 @@ svm_migrate_copy_to_vram(struct kfd_node *node, struct svm_range *prange,
 out_free_vram_pages:
 	if (r) {
 		pr_debug("failed %d to copy memory to vram\n", r);
-		for (i = 0; i < npages && mpages; i++) {
-			if (!dst[i])
-				continue;
-			svm_migrate_put_vram_page(adev, dst[i]);
-			migrate->dst[i] = 0;
-			mpages--;
-		}
+		svm_migrate_vram_pages_put(adev, migrate, dst, npages);
 	}
 
 #ifdef DEBUG_FORCE_MIXED_DOMAINS
@@ -441,7 +458,7 @@ svm_migrate_vma_to_vram(struct kfd_node *node, struct svm_range *prange,
 	unsigned long mpages = 0;
 	dma_addr_t *scratch;
 	void *buf;
-	int r = -ENOMEM;
+	int ret, r = -ENOMEM;
 
 	/* Avoid unnecessary migrations and MMU notifiers if all the data is
 	 * already resident in VRAM.
@@ -493,7 +510,13 @@ svm_migrate_vma_to_vram(struct kfd_node *node, struct svm_range *prange,
 	r = svm_migrate_copy_to_vram(node, prange, &migrate, &mfence, scratch, ttm_res_offset);
 	migrate_vma_pages(&migrate);
 
-	svm_migrate_copy_done(adev, mfence);
+	ret = svm_migrate_copy_done(adev, mfence);
+	if (ret) {
+		svm_migrate_vram_pages_put(adev, &migrate,
+					   (u64 *)(scratch + npages), npages);
+		if (!r)
+			r = ret;
+	}
 	migrate_vma_finalize(&migrate);
 
 	mpages = svm_migrate_successful_pages(&migrate);
@@ -542,6 +565,7 @@ svm_migrate_ram_to_vram(struct svm_range *prange, uint32_t best_loc,
 	u64 ttm_res_offset;
 	struct kfd_node *node;
 	unsigned long mpages = 0;
+	int idx;
 	long r = 0;
 
 	if (start_mgr < prange->start || last_mgr > prange->last) {
@@ -555,6 +579,9 @@ svm_migrate_ram_to_vram(struct svm_range *prange, uint32_t best_loc,
 		pr_debug("failed to get kfd node by id 0x%x\n", best_loc);
 		return -ENODEV;
 	}
+	/* Block unplug while the migration submits jobs and waits on them */
+	if (!drm_dev_enter(adev_to_drm(node->adev), &idx))
+		return -ENODEV;
 
 	pr_debug("svms 0x%p [0x%lx 0x%lx] in [0x%lx 0x%lx] to gpu 0x%x\n",
 		prange->svms, start_mgr, last_mgr, prange->start, prange->last,
@@ -569,6 +596,7 @@ svm_migrate_ram_to_vram(struct svm_range *prange, uint32_t best_loc,
 					node->xcp ? node->xcp->id : 0);
 	if (r) {
 		dev_dbg(node->adev->dev, "failed to reserve VRAM, r: %ld\n", r);
+		drm_dev_exit(idx);
 		return -ENOSPC;
 	}
 
@@ -613,6 +641,7 @@ out:
 					prange->npages * PAGE_SIZE,
 					KFD_IOC_ALLOC_MEM_FLAGS_VRAM,
 					node->xcp ? node->xcp->id : 0);
+	drm_dev_exit(idx);
 	return r < 0 ? r : 0;
 }
 
@@ -627,6 +656,32 @@ static void svm_migrate_folio_free(struct folio *folio)
 	}
 }
 
+/* Release system RAM dst pages of a failed migration and clear
+ * migrate->dst so migrate_vma_finalize() leaves them unmigrated.
+ */
+static void
+svm_migrate_ram_pages_put(struct device *dev, struct migrate_vma *migrate,
+			  dma_addr_t *dst, u64 npages)
+{
+	u64 i;
+
+	for (i = 0; i < npages; i++) {
+		struct page *dpage;
+
+		if (dst[i]) {
+			dma_unmap_page(dev, dst[i], PAGE_SIZE,
+				       DMA_BIDIRECTIONAL);
+			dst[i] = 0;
+		}
+		dpage = migrate_pfn_to_page(migrate->dst[i]);
+		if (!dpage)
+			continue;
+		unlock_page(dpage);
+		put_page(dpage);
+		migrate->dst[i] = 0;
+	}
+}
+
 static int
 svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,
 			struct migrate_vma *migrate, struct dma_fence **mfence,
@@ -717,22 +772,7 @@ out_oom:
 		}
 
 		/* release previous allocated sys pages and unmap dma address */
-		while (i--) {
-
-			if (dst[i]) {
-				dma_unmap_page(dev, dst[i], PAGE_SIZE,
-					       DMA_BIDIRECTIONAL);
-				dst[i] = 0;
-			}
-
-			dpage = migrate_pfn_to_page(migrate->dst[i]);
-			if (!dpage)
-				continue;
-
-			unlock_page(dpage);
-			put_page(dpage);
-			migrate->dst[i] = 0;
-		}
+		svm_migrate_ram_pages_put(dev, migrate, dst, npages);
 	}
 
 	return r;
@@ -770,7 +810,7 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,
 	struct migrate_vma migrate = { 0 };
 	dma_addr_t *scratch;
 	void *buf;
-	int r = -ENOMEM;
+	int ret, r = -ENOMEM;
 
 	memset(&migrate, 0, sizeof(migrate));
 	migrate.vma = vma;
@@ -821,11 +861,17 @@ svm_migrate_vma_to_ram(struct kfd_node *node, struct svm_range *prange,
 				    scratch, npages);
 	migrate_vma_pages(&migrate);
 
+	ret = svm_migrate_copy_done(adev, mfence);
+	if (ret) {
+		svm_migrate_ram_pages_put(adev->dev, &migrate, scratch, npages);
+		if (!r)
+			r = ret;
+	}
+
 	mpages = svm_migrate_successful_pages(&migrate);
 	pr_debug("migrated/collected/requested 0x%lx/0x%lx/0x%lx\n",
 		 mpages, cpages, migrate.npages);
 
-	svm_migrate_copy_done(adev, mfence);
 	migrate_vma_finalize(&migrate);
 
 	svm_range_dma_unmap_dev(adev->dev, scratch, 0, npages);
@@ -869,6 +915,7 @@ int svm_migrate_vram_to_ram(struct svm_range *prange, struct mm_struct *mm,
 	unsigned long start;
 	unsigned long end;
 	unsigned long mpages = 0;
+	int idx;
 	long r = 0;
 
 	/* this pragne has no any vram page to migrate to sys ram */
@@ -889,6 +936,9 @@ int svm_migrate_vram_to_ram(struct svm_range *prange, struct mm_struct *mm,
 		pr_debug("failed to get kfd node by id 0x%x\n", prange->actual_loc);
 		return -ENODEV;
 	}
+	/* Block unplug while the migration submits jobs and waits on them */
+	if (!drm_dev_enter(adev_to_drm(node->adev), &idx))
+		return -ENODEV;
 	pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] from gpu 0x%x to ram\n",
 		 prange->svms, prange, start_mgr, last_mgr,
 		 prange->actual_loc);
@@ -933,6 +983,7 @@ int svm_migrate_vram_to_ram(struct svm_range *prange, struct mm_struct *mm,
 		}
 	}
 
+	drm_dev_exit(idx);
 	return r < 0 ? r : 0;
 }
 
Signature · 1 line
2.47.3