drm/amdkfd: fix wedged teardown on surprise removal
Subsystems (3)
v7 cover drm/amdkfd: fix wedged teardown on surprise removal
v7 drops the bounded fence wait entirely per Christian's NAK: fences on a removed device must signal immediately, and blocking for all fences is the correct shutdown order. svm_migrate_copy_done() now does an unbounded dma_fence_wait() and returns the fence's error status, so the error path is real rather than a timeout escape hatch.
For that to be sound, jobs pushed to a stopped drm_sched entity must not strand their fences. drm_sched_entity_push_job() queues the job before drm_sched_rq_add_entity() notices entity->stopped and returns NULL; the job is never run and its fence never signals. The fix for that orphaning is posted separately to dri-devel:
drm/scheduler: signal fences of jobs pushed to a stopped entity
Patches 1 and 2 are unchanged from v6. Patch 3 is rewritten: the timeout is gone, the fence error propagates through both svm_migrate_vma_to_vram()/to_ram() callers (with the destination-page rollback and migrate->dst clearing Felix requested), and drm_dev_enter() guards only the job-submitting sections.
Tested on gfx906 (MI50, 16GB HBM2 behind Thunderbolt 3), vanilla 7.3.0-rc5 plus this series plus the scheduler patch:
- SIGBUS'd managed-memory hog mid-migration: process reaps in seconds, kfd_processes_count drains, no D-state tasks.
- Thunderbolt deauthorization with live KFD processes: SIGBUS to all KFD processes, teardown completes, pciehp removal finishes, host stays alive (previously: immediate wedge/reboot without trace).
- Re-authorization: clean re-enumeration, KFD usable again.
Changes since v6:
- p3: removed dma_fence_wait_timeout()/AMDGPU_FENCE_JIFFIES_TIMEOUT; unbounded wait + return mfence->error (Christian)
- fence orphaning fixed in drm/scheduler instead of worked around
- v7 split: scheduler fix is a standalone patch on dri-devel
Changes since v5:
- p3: both svm_migrate_vma_to_* callers check copy_done() and roll back failed migrations (Felix)
Regards,
Olivier
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: propagate SVM migration fence errors and guard submit with drm_dev_enter
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 18 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 5 +- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 9 +- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 108 ++++++++++++---- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 6 +- 5 files changed, 109 insertions(+), 37 deletions(-)
Signature · 1 line
2.39.0
PATCH v7 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
@@ -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 v7 2/3 drm/amdkfd: stop restore retries on device unplug
In reply to Olivier Kaloudoffamdgpu_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
@@ -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
@@ -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
PATCH v7 3/3 drm/amdkfd: propagate SVM migration fence errors and guard submit with drm_dev_enter
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(). The fault path waits on the SDMA migration fence in svm_migrate_copy_done().
That fence can complete with an error: the dying process submits the migration job on its own drm_sched entity, which process teardown has already killed, so the job is terminated and its fence completes with -ESRCH (guaranteed to signal at all by the companion drm/scheduler fix posted separately to dri-devel).
Make svm_migrate_copy_done() return the fence's error status after the unbounded wait, and make both callers handle it: release the allocated destination pages (VRAM pages via svm_migrate_put_vram_page(), system pages unmapped, unlocked and put), 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 page release loops are the same logic the copy functions already run on their own error paths, factored into helpers so both error paths share them.
Also guard the job-submitting sections of svm_migrate_ram_to_vram() and svm_migrate_vram_to_ram() with drm_dev_enter()/drm_dev_exit() so they fail fast with -ENODEV once the device is unplugged instead of submitting to dead hardware.
Rebased on amd-staging-drm-next (post window_resident landing).
Tested on gfx906 (MI50 on Thunderbolt): with a managed-memory hog process SIGBUS'd mid-migration, surprise removal now tears down cleanly, all KFD processes reap, the pciehp removal completes, the host survives and the device re-enumerates on re-authorization.
--- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 101 ++- 1 file changed, 73 insertions(+), 28 deletions(-)
drivers/gpu/drm/amd/amdkfd/kfd_migrate.c +73 -28
@@ -204,7 +204,8 @@int r =0 ;if (mfence) { - r =dma_fence_wait (mfence, false); +dma_fence_wait (mfence, false); + r = mfence->error;dma_fence_put (mfence);pr_debug ("sdma copy memory fence done\n" ); }
@@ -239,6 +240,24 @@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 +378,7 @@ 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 +454,7 @@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 +506,13 @@ 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 +561,7 @@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) {
@@ -627,6 +647,34 @@ } } +/* 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 ; + } +if (!migrate->dst[i]) +continue ; + 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 +765,7 @@ }/* 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 +803,7 @@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;
@@ -825,7 +858,12 @@pr_debug ("migrated/collected/requested 0x%lx/0x%lx/0x%lx\n" , mpages, cpages, migrate.npages); -svm_migrate_copy_done (adev, mfence); + ret =svm_migrate_copy_done (adev, mfence); +if (ret) { +svm_migrate_ram_pages_put (adev->dev, &migrate, scratch, npages); +if (!r) + r = ret; + }migrate_vma_finalize (&migrate);svm_range_dma_unmap_dev (adev->dev, scratch,0 , npages);
@@ -869,6 +907,7 @@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 +928,11 @@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 +977,7 @@ } } +drm_dev_exit (idx);return r <0 ? r :0 ; }
Signature · 1 line
2.39.0
Felix Kuehling
maintainerQuoted 12 lines · Olivier Kaloudoff
v7 drops the bounded fence wait entirely per Christian's NAK: fences on a removed device must signal immediately, and blocking for all fences is the correct shutdown order.
svm_migrate_copy_done()now does an unboundeddma_fence_wait()and returns the fence's error status, so the error path is real rather than a timeout escape hatch.For that to be sound, jobs pushed to a stopped
drm_schedentity must not strand their fences.drm_sched_entity_push_job()queues the job beforedrm_sched_rq_add_entity()noticesentity->stoppedand returns NULL; the job is never run and its fence never signals. The fix for that orphaning is posted separately to dri-devel:
Quoted 3 lines
drm/scheduler: signal fences of jobs pushed to a stopped entity
Patches 1 and 2 are unchanged from v6.
FWIW, I merged patches 1 and 2 late last week. So you won't need to send those again.
Thanks,
Felix
Quoted 41 lines
Patch 3 is rewritten: the
timeout is gone, the fence error propagates through both svm_migrate_vma_to_vram()/to_ram() callers (with the destination-page rollback andmigrate->dstclearing Felix requested), anddrm_dev_enter()guards only the job-submitting sections.Tested on gfx906 (MI50, 16GB HBM2 behind Thunderbolt 3), vanilla 7.3.0-rc5 plus this series plus the scheduler patch:
- SIGBUS'd managed-memory hog mid-migration: process reaps in seconds,
kfd_processes_countdrains, no D-state tasks.
- Thunderbolt deauthorization with live KFD processes: SIGBUS to all KFD processes, teardown completes, pciehp removal finishes, host stays alive (previously: immediate wedge/reboot without trace).
- Re-authorization: clean re-enumeration, KFD usable again.Changes since v6:
- p3: removed dma_fence_wait_timeout()/AMDGPU_FENCE_JIFFIES_TIMEOUT; unbounded wait + return mfence->error (Christian)- fence orphaning fixed in drm/scheduler instead of worked around
- v7 split: scheduler fix is a standalone patch on dri-develChanges since v5:
- p3: both svm_migrate_vma_to_* callers checkcopy_done()and roll back failed migrations (Felix)Regards,
OlivierOlivier Kaloudoff (3):
drm/amdkfd: tear down KFD processes before earlyfini_swon disconnect drm/amdkfd: stop restore retries on device unplug
drm/amdkfd: propagate SVM migration fence errors and guard submit withdrm_dev_enterdrivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 18 ++-- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 5 +- drivers/gpu/drm/amd/amdkfd/kfd_device.c | 9 +- drivers/gpu/drm/amd/amdkfd/kfd_migrate.c | 108 ++++++++++++---- drivers/gpu/drm/amd/amdkfd/kfd_svm.c | 6 +- 5 files changed, 109 insertions(+), 37 deletions(-)