amdkfd: survive PCIe surprise removal with live KFD processes
Subsystems (4)
v3 cover amdkfd: survive PCIe surprise removal with live KFD processes
v1 ran into the right question from Christian: the teardown wait must stay unbounded, and the real bug is that KFD processes never terminate on a physically gone device. This series answers that with a captured reproduction and fixes the termination path so the unbounded wait can actually complete.
Root cause (full chain captured via hung-task detector on a live repro, MI50 behind Thunderbolt, pciehp Surprise+ slot power-off):
1. Removal signals the process via terminate event -> SIGBUS.
2. The process exits through elf_core_dump -> get_dump_page -> __get_user_pages -> do_swap_page on its SVM managed ranges.
3. svm_migrate_vram_to_ram submits an SDMA copy job to a killed drm_sched entity (refcount_t warns + "Trying to push to a killed entity"), then svm_migrate_copy_done waits uninterruptibly on a job fence that can never signal.
4. The dying task holds svms->lock as a reader, so svm_range_restore_work blocks on the writer side, and the amdkfd userptr restore worker piles up behind its locks.
5. kfd_processes_count never drains -> teardown waits forever -> removal thread wedges -> host dies.
Additionally, on the disconnected path amdgpu_device_fini_hw() frees kfd.dev (and the embedded kfd_processes_count) in amdgpu_amdkfd_device_fini_sw() *before* teardown runs, so the wait either NULL-derefs or can never observe the count at all. Process teardown has to happen while kfd.dev is still valid.
Changes in v3:
- 1/3: reorder - teardown processes before the early fini_sw on the disconnected path; keep the NULL guard for the second teardown call; the drain wait is unbounded again (no timeout). Fixes: tags cover both the premature free (28695ca09d32) and the unguarded count wait (d12d05c4bc4c).
- 2/3: resubmission, unchanged from the v2 already posted.
- 3/3: new - fail fast with -ENODEV in the SVM migration paths so the dying task's fault/exit path never submits to or waits on a dead device.
Validation (pvf, 7.3.0-rc5 + series, MI50/Razer Core X on TB3, svmhog2 holding ~2GB managed ranges + queues):
slot power off -> finishing device -> SIGBUS to workload
~8s later: workload dead (core dumped), ttm finalized,
pci bus released - removal complete
~60s later: slot re-powered, MI50 re-enumerated, clean re-init
ftrace confirms amdgpu_amdkfd_teardown_processes() runs from amdgpu_device_fini_hw() before amdgpu_amdkfd_device_fini_sw().
Zero refcount warnings, zero hung tasks, host stays up. Same repro on unpatched code wedged the box twice.
PATCH v3 1/3 drm/amdkfd: teardown processes before freeing kfd.dev on surprise removal
On surprise removal the PCI core runs amdgpu_device_fini_hw(), which calls amdgpu_amdkfd_device_fini_sw() as soon as pci_dev_is_disconnected() is set. That frees kfd.dev - including the kfd_processes_count it embeds - before amdgpu_amdkfd_teardown_processes() in amdgpu_device_ip_fini_early() ever runs. The unconditional adev->kfd.dev->kfd_processes_count dereference in the wait loop then crashes the pciehp removal thread (observed as a +0x1b4 NULL deref on MI50 Thunderbolt unplug).
Move the process teardown ahead of the early fini_sw() on the disconnected path so that terminate events are sent and the drain wait runs while kfd.dev is still valid. Keep a NULL guard in kgd2kfd_teardown_processes() for the second invocation from ip_fini_early, where kfd.dev is already NULL.
The wait itself stays unbounded: returning while processes still reference kfd.dev would just trade the NULL deref for a deferred use-after-free in kfd_process_remove_pdd() when it decrements kfd_processes_count on freed memory.
---
Changes in v3:
- drop the 30s bound; the drain wait is unbounded again (CK)
- run teardown before the early fini_sw() on the disconnected path so the wait actually observes kfd_processes_count while kfd.dev is still valid; NULL guard kept for the second invocation from ip_fini_early
- v1 took a bounded-wait approach; v2 of this patch was never posted
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 5 ++++- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -1
@@ -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
@@ -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
PATCH v3 2/3 drm/amdkfd: stop userptr restore retries on device unplug
In reply to Olivier Kaloudoffamdgpu_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 v3: resubmission, unchanged from the v2 already posted.
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)
drivers/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
@@ -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
@@ -2885,7 +2888,7 @@ 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.47.3
PATCH v3 3/3 drm/amdkfd: fail fast on unplugged device in SVM migration paths
In reply to Olivier KaloudoffOn 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 an unplug check, 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.
Fail fast instead of submitting work to a dead GPU. Check both drm_dev_is_unplugged() and pci_dev_is_disconnected(): the PCI flag is set as soon as the link drops, which can be well before drm_dev_unplug() runs in the removal path.
- svm_migrate_vram_to_ram / svm_migrate_ram_to_vram: return -ENODEV for a gone device, so the fault path turns it into VM_FAULT_SIGBUS and the dying task can die
- svm_migrate_copy_done: skip the fence wait on a gone device, and bound the wait to 10s otherwise to survive unplug racing in between job submit and wait
- svm_range_restore_work: do not reschedule on -ENODEV, mirroring the userptr restore worker fix
Verified on MI50 behind Thunderbolt: with these checks the workload takes SIGBUS, coredump completes, kfd_processes_count drains in ~4s, pciehp removal finishes, and the device re-enumerates cleanly on slot re-power. Previously the same repro wedged the host.
---
New in v3.
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 30 +++++++++++++++++++++++- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +29 -1
@@ -202,7 +202,21 @@ svm_migrate_copy_done(struct amdgpu_device *adev, struct dma_fence *mfence)int r =0 ;if (mfence) { - r =dma_fence_wait (mfence, false); +/* Job fences on a dead device never signal */ +if (drm_dev_is_unplugged (adev_to_drm (adev)) || +pci_dev_is_disconnected (adev->pdev)) { + r = -ENODEV ; + }else { +long ret; + +/* survive unplug between submit and wait */ + ret =dma_fence_wait_timeout (mfence, false, +msecs_to_jiffies (10000 )); +if (!ret) + r = -ETIMEDOUT ; +else if (ret <0 ) + r = ret; + }dma_fence_put (mfence);pr_debug ("sdma copy memory fence done\n" ); }
@@ -507,6 +521,13 @@ 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 ; } +/* Fail the migration on a device that is gone for good */ +if (drm_dev_is_unplugged (adev_to_drm (node->adev)) || +pci_dev_is_disconnected (node->adev->pdev)) { +pr_debug ("gpu 0x%x unplugged, skip migration to vram\n" , + best_loc); +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,
@@ -841,6 +862,13 @@ 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 ; } +/* Fail the migration on a device that is gone for good */ +if (drm_dev_is_unplugged (adev_to_drm (node->adev)) || +pci_dev_is_disconnected (node->adev->pdev)) { +pr_debug ("gpu 0x%x unplugged, skip migration to ram\n" , + prange->actual_loc); +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);
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1 -1
@@ -2014,7 +2014,7 @@ out_reschedule:mutex_unlock (&process_info->lock);/* If validation failed, reschedule another attempt */ -if (evicted_ranges) { +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
Felix Kuehling
maintainerRe: [PATCH v3 3/3] drm/amdkfd: fail fast on unplugged device in SVM migration paths
Quoted 48 lines · 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 an unplug check,svm_migrate_vram_to_ram()submits an SDMA copy job to an already-killeddrm_schedentity (observed as "drm_sched_rq_add_entity: Trying to push to a killed entity" plusrefcount_tsaturation warnings), thensvm_migrate_copy_done()waits uninterruptibly on a job fence that can never signal. The task dies in D state holdingsvms->lock, which wedgessvm_range_restore_work()on the rwsem writer side, sokfd_processes_countnever drains and the pciehp removal thread wedges for good.Fail fast instead of submitting work to a dead GPU. Check both
drm_dev_is_unplugged()andpci_dev_is_disconnected(): the PCI flag is set as soon as the link drops, which can be well beforedrm_dev_unplug()runs in the removal path.-
svm_migrate_vram_to_ram/svm_migrate_ram_to_vram: return-ENODEVfor a gone device, so the fault path turns it intoVM_FAULT_SIGBUSand the dying task can die
-svm_migrate_copy_done: skip the fence wait on a gone device, and bound the wait to 10s otherwise to survive unplug racing in between job submit and wait
-svm_range_restore_work: do not reschedule on-ENODEV, mirroring the userptr restore worker fixVerified on MI50 behind Thunderbolt: with these checks the workload takes SIGBUS, coredump completes,
kfd_processes_countdrains in ~4s, pciehp removal finishes, and the device re-enumerates cleanly on slot re-power. Previously the same repro wedged the host.Cc Xiaogang Chen <[email protected]>Signed-off-by Olivier Kaloudoff <[email protected]>---
New in v3.drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 30 +++++++++++++++++++++++- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-)drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +0
-1@@ -202,7 +202,21 @@ svm_migrate_copy_done(struct amdgpu_device *adev, struct dma_fence *mfence)int r =0 ;if (mfence) { - r =dma_fence_wait (mfence, false);
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_copy_done(struct amdgpu_device *adev, struct dma_fence *mfence) · line 214
Quoted 10 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +10
-0+/* Job fences on a dead device never signal */ +if (drm_dev_is_unplugged (adev_to_drm (adev)) || +pci_dev_is_disconnected (adev->pdev)) { + r = -ENODEV ; + }else { +long ret; + +/* survive unplug between submit and wait */ + ret =dma_fence_wait_timeout (mfence, false, +msecs_to_jiffies (10000 ));
I'd avoid arbitrary open-coded timeout values. Would AMDGPU_FENCE_JIFFIES_TIMEOUT work? It's currently half a second.
Quoted 9 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +5
-0+if (!ret) + r = -ETIMEDOUT ; +else if (ret <0 ) + r = ret; + }dma_fence_put (mfence);pr_debug ("sdma copy memory fence done\n" ); }
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c svm_migrate_ram_to_vram(struct svm_range *prange, uint32_t best_loc, · line 526
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +3
-0@@ -507,6 +521,13 @@ 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 ; } +/* Fail the migration on a device that is gone for good */ +if (drm_dev_is_unplugged (adev_to_drm (node->adev)) || +pci_dev_is_disconnected (node->adev->pdev)) {
This seems racy. Should we use drm_dev_enter/exit instead?
Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +4
-0+pr_debug ("gpu 0x%x unplugged, skip migration to vram\n" , + best_loc); +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,
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c int svm_migrate_vram_to_ram(struct svm_range *prange, struct mm_struct *mm, · line 867
Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +3
-0@@ -841,6 +862,13 @@ 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 ; } +/* Fail the migration on a device that is gone for good */ +if (drm_dev_is_unplugged (adev_to_drm (node->adev)) || +pci_dev_is_disconnected (node->adev->pdev)) {
Same as above.
Quoted 12 lines
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +4
-0+pr_debug ("gpu 0x%x unplugged, skip migration to ram\n" , + prange->actual_loc); +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);drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0
-0
drivers/gpu/drm/amd/amdkfd/kfd_svm.c out_reschedule: · line 2017
Quoted 5 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +1
-1@@ -2014,7 +2014,7 @@ out_reschedule:mutex_unlock (&process_info->lock);/* If validation failed, reschedule another attempt */ -if (evicted_ranges) { +if (evicted_ranges && r != -ENODEV ) {
I'm not convinced this is needed. cancel_delayed_work_sync in svm_range_list_fini should take care of terminating the worker, even if it rescheduled itself. It's more of an optimization to avoid rescheduling the worker unnecessarily when it's known to be useless.
Either way, this would make more sense in patch 2, since it's similar to the restore_userptr_worker change.
Regards,
Felix
Quoted 3 lines
drivers/gpu/drm/amd/amdkfd/kfd_svm.c +0
-0pr_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 ));
Re: [PATCH v3 3/3] drm/amdkfd: fail fast on unplugged device in SVM migration paths
Hi Felix,
Quoted 2 lines
I'd avoid arbitrary open-coded timeout values. Would
AMDGPU_FENCE_JIFFIES_TIMEOUTwork? It's currently half a second.
Yes, that's better — will switch to AMDGPU_FENCE_JIFFIES_TIMEOUT.
Quoted 1 line
This seems racy. Should we use drm_dev_enter/exit instead?
Agreed — that matches the drm_dev_is_unplugged() doc recommendation. New shape: drm_dev_enter() -> svm_migrate_copy_memory() submit -> drm_dev_exit() before the fence wait in svm_migrate_copy_done() (can't hold it across the wait — drm_dev_unplug() waits for exits, and a fence on a dying device may never signal). The bounded wait in copy_done then covers the submit->wait window. I'd keep pci_dev_is_disconnected() as an additional early-out since the PCI flag can be set well before drm_dev_unplug() runs.
Quoted 6 lines
I'm not convinced this is needed.
cancel_delayed_work_syncinsvm_range_list_finishould take care of terminating the worker, even if it rescheduled itself. It's more of an optimization to avoid rescheduling the worker unnecessarily when it's known to be useless. Either way, this would make more sense in patch 2, since it's similar to therestore_userptr_workerchange.
Same situation as the userptr worker: svm_range_list_fini only runs at process teardown, which on unplug can be delayed indefinitely (the deadlocked exit path above). But yes, it's an optimization rather than a correctness fix — happy to fold it into patch 2 with the userptr worker change.
Will respin as v4 with these changes.
Olivier
Re: [PATCH v3 3/3] drm/amdkfd: fail fast on unplugged device in SVM migration paths
In reply to Felix KuehlingOne correction to my previous mail before it confuses anyone: in the v4 respin I ended up holding the drm_dev_enter() section across the whole per-VMA migration loop, including the svm_migrate_copy_done() fence wait — not just the submit as I described. That also prevents sched entity teardown from slipping in between submit and wait, which is the rest of the race window. The AMDGPU_FENCE_JIFFIES_TIMEOUT bound is then what keeps drm_dev_unplug() from blocking on our exit for more than ~500ms when the device dies electrically before removal runs.
Verified that shape on the MI50/TB3 repro: drain completes, removal finishes, no warnings.
Olivier