Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] drm/amdkfd: unlocked zombie_queues traversal in find_queue_by_doorbell_offset() - [High] drm/amdkfd: unsignaled eviction fence and context_id alias on zombie wait - [Medium] drm/amdkfd: per-process mes_hold stalls teardown on other GPUs - [Medium] drm/amdkfd: cancel_delayed_work_sync() on uninitialized svms.restore_work - [Medium] drm/amdkfd: restore_userptr_work not cancelled on notifier release - [Medium] drm/amdkfd: svm_range_evict() can re-queue restore_work after notifier release --
commit cec5d10251f9be4279a707b9cd0296feef08c241 Author: Armas Spann <[email protected]> drm/amdkfd: add a quarantine for queues MES has not retired This commit introduces a per-device zombie queue list and a delayed reaper work item to hold queue and process storage until MES confirms that a destroyed queue has been retired. > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device.c > b/drivers/gpu/drm/amd/amdkfd/kfd_device.c > index edafbd9377b5d..f631047e88b9d 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_device.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device.c > @@ -1824,6 +1819,17 @@ void kgd2kfd_teardown_processes(struct amdgpu_device > *adev) > if (!adev->kfd.dev) > return; > > + /* > + * Queues that MES never confirmed as removed keep their process > + * alive, and such a process is counted in kfd_processes_count after > + * it has left the table. The device is going away, so stop waiting > + * for MES, whether the table is empty or not. > + */ > + if (adev->kfd.dev->init_complete) { > + for (unsigned int i = 0; i < adev->kfd.dev->num_nodes; i++) > + kfd_dqm_retire_all(adev->kfd.dev->nodes[i]->dqm); > + } > + > /* > * Wait for all KFD processes using adev to terminate. Must be > * unbounded: device state is freed once they are gone. [Severity: Medium] Can an unretired zombie queue on another GPU cause kgd2kfd_teardown_processes() to spin forever on adev->kfd.dev->kfd_processes_count? kfd_init_apertures() creates a PDD and increments kfd_processes_count on every GPU in the topology, while kfd_process_destroy_pdds() only decrements kfd_processes_count after p->mes_hold drops to zero for the whole process. If an exited process has a zombie queue on GPU B, kfd_dqm_retire_all() on GPU A does not retire GPU B's queue, so GPU A's kfd_processes_count never reaches zero and teardown of GPU A hangs waiting for GPU B. > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c > b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c > index 646bc0ce05be8..0c72cf55224cc 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c [ ... ] > @@ -684,7 +688,13 @@ static int reset_queue_mes(struct device_queue_manager > *dqm, struct queue *q, > * its storage until MES is restarted. > */ > r = remove_queue_mes_on_reset_option(dqm, q, &pdd->qpd, true, true); > - set_queue_as_reset(dqm, q, &pdd->qpd, true); > + /* > + * A quarantined queue is destroyed as far as its process is > + * concerned: it is not counted, and the process is not punished > + * for a queue it no longer owns. Only the MES side was needed. > + */ > + if (!q->is_zombie) > + set_queue_as_reset(dqm, q, &pdd->qpd, true); [ ... ] > @@ -2993,6 +3143,12 @@ static struct queue > *find_queue_by_doorbell_offset(struct device_queue_manager * > } > } > > + /* MES may report a quarantined queue as hung, it still has it */ > + list_for_each_entry(q, &dqm->zombie_queues, zombie_node) { > + if (doorbell_offset == q->properties.doorbell_off) > + return q; > + } > + > return NULL; > } [Severity: High] Can find_queue_by_doorbell_offset() and reset_queue_mes() race with kfd_dqm_zombie_worker() on dqm->zombie_queues and use a zombie queue after it is freed? When amdgpu_gfx_reset_mes_compute() is called from KGD reset paths such as gfx_v11_0_reset_kcq(), gfx_v12_0_reset_kcq(), or amdgpu_userq_hang_detect_work(), only adev->gfx.mec.reset_mutex is held, not dqm_lock(dqm). kfd_reset_queue_mes() then walks dqm->zombie_queues and calls reset_queue_mes() without dqm_lock(dqm): CPU0 CPU1 (kfd_dqm_zombie_worker) amdgpu_gfx_reset_mes_compute() ... kfd_reset_queue_mes() find_queue_by_doorbell_offset() /* walks dqm->zombie_queues without dqm_lock */ reset_queue_mes(dqm, q, ...) remove_queue_mes_on_reset_option() q->mes_state = KFD_MES_QUEUE_UNMAPPED dqm_lock(dqm) kfd_mes_queue_retired(dqm, q) == true list_move_tail(&q->zombie_node, &retired) dqm_unlock(dqm) kfd_dqm_release_zombie(dqm, q) uninit_queue(q) /* frees q */ if (!q->is_zombie) <-- use-after-free Even before reset_queue_mes() runs, walking dqm->zombie_queues in find_queue_by_doorbell_offset() without dqm_lock(dqm) races with dqm_quarantine_queue() and kfd_dqm_zombie_worker() modifying the list. > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c > b/drivers/gpu/drm/amd/amdkfd/kfd_process.c > index 546721b4d59ac..51c3fa39fcc20 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c > @@ -1331,20 +1331,15 @@ static void kfd_process_wq_release(struct work_struct > *work) > release_work); > struct dma_fence *ef; > > + /* Second run, queued when the last quarantined queue was freed */ > + if (p->mes_hold_dropped) > + goto release_storage; > + > /* > * If GPU in reset, user queues may still running, wait for reset > complete. > */ > kfd_process_wait_gpu_reset_complete(p); > > - /* Signal the eviction fence after user mode queues are > - * destroyed. This allows any BOs to be freed without > - * triggering pointless evictions or waiting for fences. > - */ > - synchronize_rcu(); > - ef = rcu_access_pointer(p->ef); > - if (ef) > - dma_fence_signal(ef); > - > if (p->context_id != KFD_CONTEXT_ID_PRIMARY) > kfd_process_free_id(p); > else > @@ -1359,6 +1354,32 @@ static void kfd_process_wq_release(struct work_struct > *work) > */ > kfd_process_remove_sysfs(p); > > + /* > + * Queues that MES has not let go of still use the pdds, the process > + * context, the doorbell page and the GPU VM. Everything that carries > + * the pid is gone at this point, so stop here. The reaper queues > + * this work again when it frees the last of those queues. Do not > + * wait instead: kfd_create_process() flushes this workqueue. A work > + * item never runs twice at the same time, so the second run cannot > + * free the process under us. > + */ > + p->mes_hold_dropped = true; > + if (!atomic_dec_and_test(&p->mes_hold)) { > + pr_warn("Process pid %d: release waits for MES to retire %d > queue(s), a GPU reset retires them\n", > + p->lead_thread->pid, atomic_read(&p->mes_hold)); > + return; > + } [Severity: Medium] When kfd_process_wq_release() returns early here, can p->kgd_process_info->restore_userptr_work keep running against a post-execve mm? During exit_mmap(), mn_itree_release() invalidates userptr BOs and queues process_info->restore_userptr_work before mn_hlist_release() calls kfd_process_notifier_release_internal() and removes p from kfd_processes_table. Because restore_userptr_work is only cancelled in amdgpu_amdkfd_gpuvm_destroy_cb() during kfd_process_destroy_pdds(), deferring release_storage allows amdgpu_amdkfd_restore_userptr_worker() to look up process_info->pid after execve(), acquire the new mm, fail get_user_pages() on the old mm with -ESRCH, and reschedule itself every 10 ms against the new mm until all zombies retire. > + > +release_storage: > + /* Signal the eviction fence after user mode queues are > + * destroyed. This allows any BOs to be freed without > + * triggering pointless evictions or waiting for fences. > + */ > + synchronize_rcu(); > + ef = rcu_access_pointer(p->ef); > + if (ef) > + dma_fence_signal(ef); [Severity: High] Does deferring dma_fence_signal(ef) until release_storage stall in-flight TTM evictions and alias p->context_id while zombies are quarantined? If TTM enabled signaling on p->ef before kfd_process_table_remove(p), amdkfd_fence_enable_signaling() scheduled p->eviction_work and returned true without signaling p->ef. kfd_process_notifier_release_internal() then cancels p->eviction_work, so returning early before dma_fence_signal(ef) leaves p->ef unsignaled with DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT set, stalling any TTM move job waiting on p->ef until every zombie queue retires. In addition, for a secondary context, kfd_process_free_id(p) frees p->context_id before the early return while p->ef is still unsignaled and attached to p's BOs. If a new secondary context in the same mm reuses that context_id, a later eviction of the old context's BOs matches the new context in kfd_lookup_process_by_id(), evicting the new context while leaving the old p->ef unsignaled. [ ... ] > @@ -1422,6 +1454,12 @@ void kfd_process_notifier_release_internal(struct > kfd_process *p) > kfd_process_table_remove(p); > cancel_delayed_work_sync(&p->eviction_work); > cancel_delayed_work_sync(&p->restore_work); > + /* > + * Not left to svm_range_list_fini(): the release worker may now > + * run long after this, and the SVM restore work would look up the > + * mm of the lead thread, which can be a new one by then. > + */ [Severity: Medium] Can svm_range_evict() re-queue p->svms.restore_work after cancel_delayed_work_sync() returns here? prange->notifier is only unregistered in svm_range_list_fini() at release_storage, and mn_itree_release() leaves interval notifiers linked in the old mm until exit_mmap() finishes tearing down VMAs and page tables. Because svm_range_evict() ignores the -ESRCH return from kgd2kfd_quiesce_mm() and unconditionally queues svms->restore_work, any concurrent non-UNMAP invalidation on the old mm after this point can re-queue p->svms.restore_work, which then looks up p->lead_thread->mm after execve(). Using disable_delayed_work_sync(&p->svms.restore_work) (or checking r != -ESRCH in svm_range_evict()) would prevent it from being queued again. > + cancel_delayed_work_sync(&p->svms.restore_work); [Severity: Medium] Does calling cancel_delayed_work_sync(&p->svms.restore_work) unconditionally trigger WARN_ON(!work->func) in __flush_work() when CONFIG_HSA_AMD_SVM is disabled? When CONFIG_HSA_AMD_SVM=n, svm_range_list_init() is a no-op stub and never initializes p->svms.restore_work, so every exiting KFD process hits the warning in __flush_work(). -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=4
