Skip to contentSkip to conversation

amdkfd: survive PCIe surprise removal with live KFD processes

PATCH Superseded Olivier Kaloudoff 3 participants 16 messages Oct 3 – Oct 8

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover amdkfd: survive PCIe surprise removal with live KFD processes

Hi,

two small fixes for the KFD teardown path on PCIe surprise removal (e.g. Thunderbolt eGPU link drop), observed on real hardware: an AMD Instinct MI50 eGPU (Razer Core X, TB3) behind which a 19 GB ROCm llama.cpp workload runs 24/7 with several KFD processes live.

Patch 1 fixes a NULL deref + unbounded wait in kgd2kfd_teardown_processes(): on the disconnect path amdgpu_amdkfd_device_fini_sw() frees adev->kfd.dev before amdgpu_device_ip_fini_early() reaches teardown_processes, which then dereferences adev->kfd.dev->kfd_processes_count. We captured the oops in EFI pstore: NULL deref at +0x1b4 in the pciehp removal thread, panic via panic_on_oops. The wait is also unbounded — processes blocked on dead MMIO may never terminate, which wedges pciehp in cond_resched() forever and cascades into hung-task/softlockup panics.

Patch 2 stops amdgpu_amdkfd_restore_userptr_worker() from rescheduling itself forever when the device is gone: the VM update paths already fail fast with -ENODEV on unplug (existing drm_dev_enter guards in amdgpu_vm_update_pdes/vm_update_range), but the worker ignores the error code. Observed: ~11k rescheduled restore cycles before the host soft-locked up.

Both verified to apply and build against v7.3-rc5; observed and debugged on 7.2.6 with the identical code shape. Pstore traces and the full writeup are in our public repo:

https://codeberg.org/kendoulov/linux-gfx906-kernel-patches
docs/UPSTREAM-ISSUE-2-unplug-teardown.md

These are independent of the KFD SVM evict/restore work — this is the hot-unplug ordering path added by the recent kfd hot-unplug support (6cca686dfce7, d12d05c4bc4c). CCing Xiaogang who has been working in this area.

Thanks,
Olivier Kaloudoff

#1 lore raw

PATCH 1/2 drm/amdkfd: fix NULL deref and unbounded wait in teardown_processes on surprise removal

On PCIe surprise removal (e.g. Thunderbolt eGPU link drop), amdgpu_device_fini_hw() calls amdgpu_amdkfd_device_fini_sw() when pci_dev_is_disconnected() is true, which frees adev->kfd.dev and sets it to NULL. amdgpu_device_ip_fini_early() then runs a few calls later in the same function and reaches kgd2kfd_teardown_processes(), which unconditionally dereferences adev->kfd.dev->kfd_processes_count.

Observed on real hardware (MI50 eGPU behind TB3): NULL deref at +0x1b4 (kfd_processes_count) inside kgd2kfd_teardown_processes, called from the pciehp removal thread; panic via panic_on_oops (pstore capture).

Additionally, the wait-for-termination loop is unbounded: on a physically gone device, processes can be unable to make forward progress, wedging the pciehp removal thread in cond_resched() forever and cascading into hung-task/softlockup panics.

Guard the wait on adev->kfd.dev and bound it to 30s with a dev_warn so removal can complete and the kernel survives.

Fixes d12d05c4bc4c ("drm/amdkfd: Check if there are kfd porcesses using adev by kfd_processes_count")
Cc Xiaogang Chen <[email protected]>
---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +20 -2
index 586e640..9326447 100644
@@ -1780,6 +1780,7 @@ bool kgd2kfd_vmfault_fast_path(struct amdgpu_device *adev, struct amdgpu_iv_entr
 void kgd2kfd_teardown_processes(struct amdgpu_device *adev)
 {
 	struct hlist_node *p_temp;
+	unsigned long timeout;
 	struct kfd_process *p;
 	struct kfd_node *dev;
 	unsigned int temp;
@@ -1801,9 +1802,26 @@ void kgd2kfd_teardown_processes(struct amdgpu_device *adev)
 
 	mutex_unlock(&kfd_processes_mutex);
 
-	/* wait all kfd processes use adev terminate */
-	while (!!atomic_read(&adev->kfd.dev->kfd_processes_count))
+	/* On surprise removal amdgpu_amdkfd_device_fini_sw() has already
+	 * freed kfd.dev before we get here.
+	 */
+	if (!adev->kfd.dev)
+		return;
+
+	/* Wait for all kfd processes using adev to terminate. Bound the
+	 * wait: on a physically gone device processes may never finish,
+	 * and spinning here forever wedges the pciehp removal thread.
+	 */
+	timeout = jiffies + msecs_to_jiffies(30000);
+	while (atomic_read(&adev->kfd.dev->kfd_processes_count)) {
+		if (time_after(jiffies, timeout)) {
+			dev_warn(adev->dev,
+				 "kfd: %d processes still attached to removed device\n",
+				 atomic_read(&adev->kfd.dev->kfd_processes_count));
+			break;
+		}
 		cond_resched();
+	}
 }
 
 int kgd2kfd_reset_mes_queue(struct kfd_dev *kfd, uint32_t node_id,
Signature · 1 line
2.54.0 (Apple Git-157)
maintainer
#2 lore raw

Re: [PATCH 1/2] drm/amdkfd: fix NULL deref and unbounded wait in teardown_processes on surprise removal

Quoted 11 lines · Olivier Kaloudoff

On PCIe surprise removal (e.g. Thunderbolt eGPU link drop), amdgpu_device_fini_hw() calls amdgpu_amdkfd_device_fini_sw() when pci_dev_is_disconnected() is true, which frees adev->kfd.dev and sets it to NULL. amdgpu_device_ip_fini_early() then runs a few calls later in the same function and reaches kgd2kfd_teardown_processes(), which unconditionally dereferences adev->kfd.dev->kfd_processes_count.

Observed on real hardware (MI50 eGPU behind TB3): NULL deref at +0x1b4 (kfd_processes_count) inside kgd2kfd_teardown_processes, called from the pciehp removal thread; panic via panic_on_oops (pstore capture).

Quoted 7 lines

Additionally, the wait-for-termination loop is unbounded: on a physically gone device, processes can be unable to make forward progress, wedging the pciehp removal thread in cond_resched() forever and cascading into hung-task/softlockup panics.

Guard the wait on adev->kfd.dev and bound it to 30s with a dev_warn so removal can complete and the kernel survives.

That is clearly a really bad idea. Waiting for the termination *must* be unbounded.

The real question is why those KFD processes doesn't terminate?

Regards,
Christian.

Quoted 49 lines
Fixes d12d05c4bc4c ("drm/amdkfd: Check if there are kfd porcesses using adev by kfd_processes_count")
Cc Xiaogang Chen <[email protected]>
---
 drivers/gpu/drm/amd/amdkfd/kfd_device.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +20 -2
index 586e640..9326447 100644
@@ -1780,6 +1780,7 @@ bool kgd2kfd_vmfault_fast_path(struct amdgpu_device *adev, struct amdgpu_iv_entr
 void kgd2kfd_teardown_processes(struct amdgpu_device *adev)
 {
 	struct hlist_node *p_temp;
+	unsigned long timeout;
 	struct kfd_process *p;
 	struct kfd_node *dev;
 	unsigned int temp;
@@ -1801,9 +1802,26 @@ void kgd2kfd_teardown_processes(struct amdgpu_device *adev)
 
 	mutex_unlock(&kfd_processes_mutex);
 
-	/* wait all kfd processes use adev terminate */
-	while (!!atomic_read(&adev->kfd.dev->kfd_processes_count))
+	/* On surprise removal amdgpu_amdkfd_device_fini_sw() has already
+	 * freed kfd.dev before we get here.
+	 */
+	if (!adev->kfd.dev)
+		return;
+
+	/* Wait for all kfd processes using adev to terminate. Bound the
+	 * wait: on a physically gone device processes may never finish,
+	 * and spinning here forever wedges the pciehp removal thread.
+	 */
+	timeout = jiffies + msecs_to_jiffies(30000);
+	while (atomic_read(&adev->kfd.dev->kfd_processes_count)) {
+		if (time_after(jiffies, timeout)) {
+			dev_warn(adev->dev,
+				 "kfd: %d processes still attached to removed device\n",
+				 atomic_read(&adev->kfd.dev->kfd_processes_count));
+			break;
+		}
 		cond_resched();
+	}
 }
 
 int kgd2kfd_reset_mes_queue(struct kfd_dev *kfd, uint32_t node_id,
maintainer
#3 lore raw

Re: [PATCH 1/2] drm/amdkfd: fix NULL deref and unbounded wait in teardown_processes on surprise removal

Quoted 17 lines · Christian König
Quoted 16 lines · Olivier Kaloudoff

On PCIe surprise removal (e.g. Thunderbolt eGPU link drop), amdgpu_device_fini_hw() calls amdgpu_amdkfd_device_fini_sw() when pci_dev_is_disconnected() is true, which frees adev->kfd.dev and sets it to NULL. amdgpu_device_ip_fini_early() then runs a few calls later in the same function and reaches kgd2kfd_teardown_processes(), which unconditionally dereferences adev->kfd.dev->kfd_processes_count.

Observed on real hardware (MI50 eGPU behind TB3): NULL deref at +0x1b4 (kfd_processes_count) inside kgd2kfd_teardown_processes, called from the pciehp removal thread; panic via panic_on_oops (pstore capture).

Additionally, the wait-for-termination loop is unbounded: on a physically gone device, processes can be unable to make forward progress, wedging the pciehp removal thread in cond_resched() forever and cascading into hung-task/softlockup panics.

Quoted 5 lines
Quoted 2 lines

Guard the wait on adev->kfd.dev and bound it to 30s with a dev_warn so removal can complete and the kernel survives.

That is clearly a really bad idea. Waiting for the termination *must* be unbounded.

The real question is why those KFD processes doesn't terminate?

kgd2kfd_teardown_processes sends SIGBUS to those processes. The kfd_processes_count is decremented in kfd_process_destroy_pdds, called from kfd_process_wq_release in a cleanup worker that is typically scheduled by the MMU release notifier or when the process ref count drops to 0. A reference count leak of the kfd_process or the mm_struct could prevent this from happening.

Do we have a sequencing issues? Should kgd2kfd_teardown_processes run before the KFD dev is destroyed?

Regards,
  Felix

Quoted 52 lines

Regards,
Christian.

Quoted 48 lines
Fixes d12d05c4bc4c ("drm/amdkfd: Check if there are kfd porcesses using adev by kfd_processes_count")
Cc Xiaogang Chen <[email protected]>
---
  drivers/gpu/drm/amd/amdkfd/kfd_device.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_device.c +20 -2
index 586e640..9326447 100644
@@ -1780,6 +1780,7 @@ bool kgd2kfd_vmfault_fast_path(struct amdgpu_device *adev, struct amdgpu_iv_entr
  void kgd2kfd_teardown_processes(struct amdgpu_device *adev)
  {
  	struct hlist_node *p_temp;
+	unsigned long timeout;
  	struct kfd_process *p;
  	struct kfd_node *dev;
  	unsigned int temp;
@@ -1801,9 +1802,26 @@ void kgd2kfd_teardown_processes(struct amdgpu_device *adev)
 
  	mutex_unlock(&kfd_processes_mutex);
 
-	/* wait all kfd processes use adev terminate */
-	while (!!atomic_read(&adev->kfd.dev->kfd_processes_count))
+	/* On surprise removal amdgpu_amdkfd_device_fini_sw() has already
+	 * freed kfd.dev before we get here.
+	 */
+	if (!adev->kfd.dev)
+		return;
+
+	/* Wait for all kfd processes using adev to terminate. Bound the
+	 * wait: on a physically gone device processes may never finish,
+	 * and spinning here forever wedges the pciehp removal thread.
+	 */
+	timeout = jiffies + msecs_to_jiffies(30000);
+	while (atomic_read(&adev->kfd.dev->kfd_processes_count)) {
+		if (time_after(jiffies, timeout)) {
+			dev_warn(adev->dev,
+				 "kfd: %d processes still attached to removed device\n",
+				 atomic_read(&adev->kfd.dev->kfd_processes_count));
+			break;
+		}
  		cond_resched();
+	}
  }
 
  int kgd2kfd_reset_mes_queue(struct kfd_dev *kfd, uint32_t node_id,
#4 lore raw

Re: [PATCH 1/2] drm/amdkfd: fix NULL deref and unbounded wait in teardown_processes on surprise removal

Hi Felix,

Quoted 2 lines

Do we have a sequencing issues? Should kgd2kfd_teardown_processes run before the KFD dev is destroyed?

Yes — that's exactly what the v3 resend does (patch 1/3): on the pci_dev_is_disconnected() branch, amdgpu_device_fini_hw() now calls amdgpu_amdkfd_teardown_processes() before the early amdgpu_amdkfd_device_fini_sw(), so the drain runs while kfd.dev and kfd_processes_count are still valid. ftrace on the repro confirms the
ordering: teardown from fini_hw, count drains, fini_sw ~8s later.

Quoted 2 lines

A reference count leak of the kfd_process or the mm_struct could prevent this from happening.

Doesn't look like a leak — the SIGBUS'd process simply never gets to exit. Its own fault path wedges: coredump -> gup -> do_swap_page -> svm_migrate_to_ram submits an SDMA copy to the already-killed sched entity and then svm_migrate_copy_done() waits uninterruptibly on a fence that can never signal, all while holding svms->lock. Exit never completes, the MMU notifier/cleanup path never runs, and the count never drains. Patch 3/3 makes the SVM migration paths return -ENODEV on a gone device; with that, the process dies and the count drains in ~7s on real hardware (MI50 behind TB3, slot power-off).

Olivier

#5 lore raw

Re: [PATCH 1/2] drm/amdkfd: fix NULL deref and unbounded wait in teardown_processes on surprise removal

In reply to Christian König

Hi Christian,

Quoted 3 lines

That is clearly a really bad idea. Waiting for the termination *must* be unbounded. The real question is why those KFD processes doesn't terminate?

Ok, dropped timeout and chased the termination instead. Reproduced it reliably (MI50 on Thunderbolt 3, ~2GB of SVM managed ranges + queues, then pciehp Surprise+ slot power-off). The answer from the hung-task dumps:

The process can't terminate because its own exit path needs the GPU. SIGBUS -> elf_core_dump -> get_dump_page -> __get_user_pages -> do_swap_page hits the SVM managed ranges and calls svm_migrate_to_ram(). That submits an SDMA copy job to a killed drm_sched entity (refcount_t warns + "push to a killed entity"), then svm_migrate_copy_done() waits uninterruptibly on a job fence that can never signal. The task wedges in D holding svms->lock, which wedges svm_range_restore_work (rwsem writer) and the userptr restore worker behind it -> kfd_processes_count never drains.

So the bounded wait was indeed masking the bug. v3 keeps the wait unbounded and instead:

1/3 teardown ordering: on the disconnected path, fini_hw frees kfd.dev before teardown_processes runs, so the drain wait can't even observe the count (that's also where the NULL deref came from). Signal + drain now happens while kfd.dev is still valid; the NULL guard covers the second call.

2/3 unchanged from the v2 I posted (no ret pre-init, no warn).

3/3 new: SVM migration fails fast with -ENODEV on unplugged / pci_dev_is_disconnected device (entry points + the copy-done fence wait + stop rescheduling svm_range_restore_work on -ENODEV). The coredump fault then just turns into VM_FAULT_SIGBUS and the task dies normally.

Retested with the same workload + removal: process died by SIGBUS + core in ~4s, count drained, removal completed in ~7s, zero hung tasks or refcount warnings, and the eGPU re-enumerated cleanly when the slot re-powered. Host survived; previously it wedged hard.

One thing worth flagging separately: the refcount_t saturation / "push to a killed entity" warns show amdgpu_job_submit / amdgpu_copy_buffer happily arm jobs on dead sched entities - worth a gate there too, but that's beyond this series.

Related: the April "drm/amdkfd: Terminate queues on surprise unplug with running processes" thread touches the same area (process termination around kfd_cleanup_nodes) - 1/3 lands teardown earlier for the same reason, and is compatible with that approach.

Series + repro details below.

#6 lore raw

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

In reply to Olivier Kaloudoff

amdgpu_amdkfd_restore_userptr_worker() reschedules itself every AMDGPU_USERPTR_RESTORE_DELAY_MS whenever validation of invalidated userptr BOs fails. On PCIe surprise removal the VM update paths already fail fast: amdgpu_vm_update_pdes() and amdgpu_vm_update_range() return -ENODEV when drm_dev_enter() fails, which propagates up through process_update_pds()/update_gpuvm_pte()/validate_invalid_user_pages(). The worker ignores the error code and reschedules unconditionally, producing an endless retry loop on a device that is permanently gone.

Observed on MI50 eGPU TB3 surprise removal: ~11k rescheduled restore cycles, the worker holding reclaim-related locks until the system soft-locked up.

Stop rescheduling when the validation failure is -ENODEV: the device is gone for good and the affected processes are being torn down anyway. Other errors keep the existing retry behavior, as they may be transient.

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 22 ++++++++++++++-----
 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +16 -6
index d668816..6c040ed 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 = 0;
 
 	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
@@ -2886,11 +2889,18 @@ unlock_out:
 
 	/* If validation failed, reschedule another attempt */
 	if (evicted_bos) {
-		queue_delayed_work(system_freezable_wq,
-			&process_info->restore_userptr_work,
-			msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
+		if (ret == -ENODEV) {
+			/* Device is gone for good, stop retrying */
+			pr_warn_once("%s: device unplugged, aborting userptr restore (pid %d, %s)\n",
+				     __func__, pid_nr(process_info->pid),
+				     usertask->comm);
+		} else {
+			queue_delayed_work(system_freezable_wq,
+				&process_info->restore_userptr_work,
+				msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
 
-		kfd_smi_event_queue_restore_rescheduled(mm);
+			kfd_smi_event_queue_restore_rescheduled(mm);
+		}
 	}
 	mmput(mm);
 	put_task_struct(usertask);
Signature · 1 line
2.54.0 (Apple Git-157)
maintainer
#7 lore raw

Re: [PATCH 2/2] drm/amdkfd: stop userptr restore retries on device unplug

Quoted 29 lines · Olivier Kaloudoff

amdgpu_amdkfd_restore_userptr_worker() reschedules itself every AMDGPU_USERPTR_RESTORE_DELAY_MS whenever validation of invalidated userptr BOs fails. On PCIe surprise removal the VM update paths already fail fast: amdgpu_vm_update_pdes() and amdgpu_vm_update_range() return -ENODEV when drm_dev_enter() fails, which propagates up through process_update_pds()/update_gpuvm_pte()/validate_invalid_user_pages(). The worker ignores the error code and reschedules unconditionally, producing an endless retry loop on a device that is permanently gone.

Observed on MI50 eGPU TB3 surprise removal: ~11k rescheduled restore cycles, the worker holding reclaim-related locks until the system soft-locked up.

Stop rescheduling when the validation failure is -ENODEV: the device is gone for good and the affected processes are being torn down anyway. Other errors keep the existing retry behavior, as they may be transient.

---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 22 ++++++++++++++-----
 1 file changed, 16 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index d668816..6c040ed 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work) · line 2830

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
@@ -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 = 0;

Please don't initialize return values when it isn't necessary.

Initialize it before the goto.

Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -2
 	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
@@ -2886,11 +2889,18 @@ unlock_out:
 

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c unlock_out: · line 2896

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +5 -3
 	/* If validation failed, reschedule another attempt */
 	if (evicted_bos) {
-		queue_delayed_work(system_freezable_wq,
-			&process_info->restore_userptr_work,
-			msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
+		if (ret == -ENODEV) {
+			/* Device is gone for good, stop retrying */
+			pr_warn_once("%s: device unplugged, aborting userptr restore (pid %d, %s)\n",
+				     __func__, pid_nr(process_info->pid),
+				     usertask->comm);

That warning is most likely unnecessary and maybe even harmful.

That the device is hot removed should just be a perfectly normal abortion condition for the worker.

Regards,
Christian.

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6 -1
+		} else {
+			queue_delayed_work(system_freezable_wq,
+				&process_info->restore_userptr_work,
+				msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
 
-		kfd_smi_event_queue_restore_rescheduled(mm);
+			kfd_smi_event_queue_restore_rescheduled(mm);
+		}
 	}
 	mmput(mm);
 	put_task_struct(usertask);
#8 lore raw

Re: [PATCH 2/2] drm/amdkfd: stop userptr restore retries on device unplug

Quoted 5 lines · Christian König

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work) · line 2830

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
+	int ret = 0;

Please don't initialize return values when it isn't necessary.

Initialize it before the goto.

Done, ret is now declared uninitialized and assigned at the two call sites before each goto.

For completeness: every path reaching the "if (evicted_bos)" reschedule check has ret assigned -- the two failure gotos carry the error, and unlock_notifier_out is only reachable after update_invalid_user_pages() (and validate_invalid_user_pages() when the list is non-empty) returned 0. So the concurrent-eviction and WARN paths still reschedule exactly as before.

Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c unlock_out: · line 2894

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
+			pr_warn_once("%s: device unplugged, aborting userptr restore (pid %d, %s)\n",

That warning is most likely unnecessary and maybe even harmful.

That the device is hot removed should just be a perfectly normal abortion condition for the worker.

Agreed, dropped entirely. The worker now just skips the reschedule when ret == -ENODEV, treated as a normal termination condition. This also correctly suppresses the KFD_SMI_EVENT restore-rescheduled notification, which would otherwise falsely tell userspace that another attempt is coming.

v2 follows.

Thanks,
Olivier

maintainer
#9 lore raw

Re: [PATCH 2/2] drm/amdkfd: stop userptr restore retries on device unplug

In reply to Christian König
Quoted 9 lines · Christian König
Quoted 8 lines · Olivier Kaloudoff

amdgpu_amdkfd_restore_userptr_worker() reschedules itself every AMDGPU_USERPTR_RESTORE_DELAY_MS whenever validation of invalidated userptr BOs fails. On PCIe surprise removal the VM update paths already fail fast: amdgpu_vm_update_pdes() and amdgpu_vm_update_range() return -ENODEV when drm_dev_enter() fails, which propagates up through process_update_pds()/update_gpuvm_pte()/validate_invalid_user_pages(). The worker ignores the error code and reschedules unconditionally, producing an endless retry loop on a device that is permanently gone.

The worker should get canceled in amdgpu_amdkfd_gpuvm_destroy_cb when the VM is destroyed (when the render node device is closed as part of process termination). So I'm not sure why we need a special case in the restore worker for this.

Regards,
  Felix

Quoted 81 lines
Quoted 21 lines

Observed on MI50 eGPU TB3 surprise removal: ~11k rescheduled restore cycles, the worker holding reclaim-related locks until the system soft-locked up.

Stop rescheduling when the validation failure is -ENODEV: the device is gone for good and the affected processes are being torn down anyway. Other errors keep the existing retry behavior, as they may be transient.

---
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 22 ++++++++++++++-----
  1 file changed, 16 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +0 -0
index d668816..6c040ed 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c static void amdgpu_amdkfd_restore_userptr_worker(struct work_struct *work) · line 2830

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -0
@@ -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 = 0;

Please don't initialize return values when it isn't necessary.

Initialize it before the goto.

Quoted 24 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +4 -2
  	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
@@ -2886,11 +2889,18 @@ unlock_out:
 

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c unlock_out: · line 2896

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +5 -3
  	/* If validation failed, reschedule another attempt */
  	if (evicted_bos) {
-		queue_delayed_work(system_freezable_wq,
-			&process_info->restore_userptr_work,
-			msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
+		if (ret == -ENODEV) {
+			/* Device is gone for good, stop retrying */
+			pr_warn_once("%s: device unplugged, aborting userptr restore (pid %d, %s)\n",
+				     __func__, pid_nr(process_info->pid),
+				     usertask->comm);

That warning is most likely unnecessary and maybe even harmful.

That the device is hot removed should just be a perfectly normal abortion condition for the worker.

Regards,
Christian.

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6 -1
+		} else {
+			queue_delayed_work(system_freezable_wq,
+				&process_info->restore_userptr_work,
+				msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
 
-		kfd_smi_event_queue_restore_rescheduled(mm);
+			kfd_smi_event_queue_restore_rescheduled(mm);
+		}
  	}
  	mmput(mm);
  	put_task_struct(usertask);
#10 lore raw

Re: [PATCH 2/2] drm/amdkfd: stop userptr restore retries on device unplug

Hi Felix,

The worker is only canceled when the last compute VM is destroyed in amdgpu_amdkfd_gpuvm_destroy_cb() — i.e., late in process exit. On surprise removal that window is unbounded: in my repro the SIGBUS exit path deadlocked in SVM migration (patch 3/3 in the v3 resend), so process termination never reached the destroy callback. The worker kept rescheduling at 1ms delay, ~11k cycles, taking process_info->lock and notifier_lock each round until the system soft-locked up.

There's also a self-reschedule race with cancel_delayed_work_sync():
if the handler is running when the cancel executes, it re-arms itself at the bottom of the function after cancel returns — then process_info is freed right after cancel_delayed_work_sync() in destroy_cb, leaving a pending delayed work on freed memory. The retry loop needs a stop condition; returning -ENODEV on a gone device is it, and it also avoids retrying validation that can never succeed again.

Transient errors still reschedule as before.

Olivier

maintainer
#11 lore raw

Re: [PATCH 2/2] drm/amdkfd: stop userptr restore retries on device unplug

Quoted 10 lines · Olivier Kaloudoff

Hi Felix,

The worker is only canceled when the last compute VM is destroyed in amdgpu_amdkfd_gpuvm_destroy_cb() — i.e., late in process exit. On surprise removal that window is unbounded: in my repro the SIGBUS exit path deadlocked in SVM migration (patch 3/3 in the v3 resend), so process termination never reached the destroy callback. The worker kept rescheduling at 1ms delay, ~11k cycles, taking process_info->lock and notifier_lock each round until the system soft-locked up.

Quoted 7 lines

There's also a self-reschedule race with cancel_delayed_work_sync():
if the handler is running when the cancel executes, it re-arms itself at the bottom of the function after cancel returns — then process_info is freed right after cancel_delayed_work_sync() in destroy_cb, leaving a pending delayed work on freed memory. The retry loop needs a stop condition; returning -ENODEV on a gone device is it, and it also avoids retrying validation that can never succeed again.

Shouldn't cancel_delayed_work_sync wait for the worker function to exit? I don't see how that can race with the re-scheduling in the worker function itself.

Regards,
  Felix

Quoted 4 lines

Transient errors still reschedule as before.

Olivier

#12 lore raw

Re: [PATCH 2/2] drm/amdkfd: stop userptr restore retries on device unplug

Hi Felix,

Quoted 3 lines

Shouldn't cancel_delayed_work_sync wait for the worker function to exit? I don't see how that can race with the re-scheduling in the worker function itself.

You're right, my UAF framing was wrong on current kernels. I checked the workqueue code: __cancel_work_sync() marks the work disabled in its off-queue data while it flushes, and queue_delayed_work_on() drops the requeue via clear_pending_if_disabled(). Self-reschedule from a running handler, delayed or not, can't survive the cancel.

The argument that does stand: the cancel only runs at last-VM destroy during process exit (gpuvm_destroy_cb, when n_vms hits 0). On surprise removal that path itself can stall — the SIGBUS'd task deadlocks in its own exit path (SVM migrate -> fence that never signals, patch 3/3), so process_info teardown never runs and nobody ever calls the cancel. That whole window is where the ~11k-cycle spin at 1ms delay happened, holding process_info->lock and notifier_lock each round until soft-lockup.

So the -ENODEV check isn't about racing the cancel — it's the stop condition for the window where the device's lifetime has ended but process_info's hasn't. Transient errors still retry as before.

Olivier

maintainer
#13 lore raw

Re: [PATCH 2/2] drm/amdkfd: stop userptr restore retries on device unplug

Quoted 11 lines · Olivier Kaloudoff

Hi Felix,

Quoted 3 lines

Shouldn't cancel_delayed_work_sync wait for the worker function to exit? I don't see how that can race with the re-scheduling in the worker function itself.

You're right, my UAF framing was wrong on current kernels. I checked the workqueue code: __cancel_work_sync() marks the work disabled in its off-queue data while it flushes, and queue_delayed_work_on() drops the requeue via clear_pending_if_disabled(). Self-reschedule from a running handler, delayed or not, can't survive the cancel.

Quoted 6 lines

The argument that does stand: the cancel only runs at last-VM destroy during process exit (gpuvm_destroy_cb, when n_vms hits 0). On surprise removal that path itself can stall — the SIGBUS'd task deadlocks in its own exit path (SVM migrate -> fence that never signals, patch 3/3), so process_info teardown never runs and nobody ever calls the cancel.

On that path you changed the dma_fence_wait to dma_fence_wait_timeout. That should be sufficient to prevent the stall. I believe the extra checks for pci_dev_is_disconnected are unnecessary. IMO the cleanest solution is the one with the least special case handling deep in the KFD code. drm_dev_enter/exit is meant to abstract that away from most of the driver code. Therefore, just guarding the critical sections that need HW access between drm_dev_enter/exit, and not having any unbounded fence-wait inside those critical sections, should be sufficient.

Regards,
  Felix

Quoted 9 lines

That whole window is where the ~11k-cycle spin at 1ms delay
happened, holding process_info->lock and notifier_lock each round until soft-lockup.

So the -ENODEV check isn't about racing the cancel — it's the stop condition for the window where the device's lifetime has ended but process_info's hasn't. Transient errors still retry as before.

Olivier

#14 lore raw

Re: [PATCH v4 2/2] drm/amdgpu: fail fast on unplugged device in VM and userptr restore paths

Hi Felix,

Quoted 6 lines

the extra checks for pci_dev_is_disconnected are unnecessary. IMO the cleanest solution is the one with the least special case handling deep in the KFD code. I.e. just guarding the critical sections that need HW access between drm_dev_enter/exit, and not having any unbounded fence-wait inside those critical sections, should be sufficient.

Agreed — that's cleaner. v5 drops all the unplugged/disconnected checks; drm_dev_enter() fails fast once unplug starts, and the AMDGPU_FENCE_JIFFIES_TIMEOUT bound covers the window where the link is dead but drm_dev_unplug() hasn't run yet.

One observation for the record: in that dead-but-not-yet-unplugged window dev_enter still succeeds, so each migration attempt submits to hardware that can't complete and pays one ~500ms timeout instead of failing instantly. In practice the window is short and the total removal completed in ~7s on my repro, so the simpler code wins.

Re-verified the minimal version on the MI50/Thunderbolt repro: SIGBUS delivered, coredump completes, kfd_processes_count drains in ~7s, pciehp removal finishes, device re-enumerates on re-power.

Olivier

#15 lore raw

PATCH v2 2/2 drm/amdkfd: stop userptr restore retries on device unplug

In reply to Olivier Kaloudoff

amdgpu_amdkfd_restore_userptr_worker() reschedules itself every AMDGPU_USERPTR_RESTORE_DELAY_MS whenever validation of invalidated userptr BOs fails. On PCIe surprise removal the VM update paths already fail fast: amdgpu_vm_update_pdes() and amdgpu_vm_update_range() return -ENODEV when drm_dev_enter() fails, which propagates up through process_update_pds()/update_gpuvm_pte()/validate_invalid_user_pages(). The worker ignores the error code and reschedules unconditionally, producing an endless retry loop on a device that is permanently gone.

Observed on MI50 eGPU TB3 surprise removal: ~11k rescheduled restore cycles, the worker holding reclaim-related locks until the system soft-locked up.

Stop rescheduling when the validation failure is -ENODEV: the device is gone for good and the affected processes are being torn down anyway. Other errors keep the existing retry behavior, as they may be transient.

---
Changes in v2:
- Don't pre-initialize ret; every path to the reschedule check is dominated by an assignment (Christian König)
- Drop the pr_warn_once; hot removal is a normal abort condition for the worker (Christian König)

 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |  9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +6 -3
index d668816..dc94d32 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
@@ -2884,8 +2887,8 @@ unlock_notifier_out:
 unlock_out:
 	mutex_unlock(&process_info->lock);
 
 	/* If validation failed, reschedule another attempt */
-	if (evicted_bos) {
+	if (evicted_bos && ret != -ENODEV) {
 		queue_delayed_work(system_freezable_wq,
 			&process_info->restore_userptr_work,
 			msecs_to_jiffies(AMDGPU_USERPTR_RESTORE_DELAY_MS));
Signature · 1 line
2.54.0 (Apple Git-157)