From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D06AA606A32F for ; Fri, 4 Dec 2026 16:00:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=opDrxIlX Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 737C310EC0D; Fri, 4 Dec 2026 16:00:46 +0000 (UTC) Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7E924113072 for ; Fri, 4 Dec 2026 16:00:40 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 6D19960CF8; Fri, 4 Dec 2026 16:00:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1112C1F000C2; Fri, 4 Dec 2026 16:00:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796400039; bh=uoe5drQHyanj2wmYwPuqw38ICVEY5CQTJzZjtTnv1nE=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=opDrxIlX/uiTCBRbAAMlUQGRoKxFLnX2FHB3VHdF6CfHkqa/5cgiq/faWN61Yj9y2 CPCgrjw6P1UvmmhWZnBfLEfMcXfUPeaoN9sohnA8TfV+Z4S6gC6dIgujCiV+VZvkVu 0UI9G5uRHjAnqKjzIEAGWKDfR8vw3p1uCQBnktd1ZlQL0zxS793pmP3s1kXpoiA/zW lXU8lPJ9ON2kucVo+fP1R2W6mlq/IUUYmK7fODKSEiVbEhjtITNuJSET19tHEtYptx 6NAYsTIu53RASX8XPp1iJpMtN097EJyYS3tCfbD2c4s0qmxyVY6Y62FWQm/OE2QqdL xKAj4/upR2mCA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 4/8] drm/amdkfd: add a quarantine for queues MES has not retired To: "Armas Spann" , amd-gfx@lists.freedesktop.org In-Reply-To: <20261204125251.10703-5-zappel@simple-co.de> References: <20261204125251.10703-1-zappel@simple-co.de> <20261204125251.10703-5-zappel@simple-co.de> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 16:00:38 +0000 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "amd-gfx" X-Spam-Status: No, score=-11.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 9 potential issues to consider: - [Critical] drm/amdkfd: unlocked zombie_queues walk and reset race with zombie reaper - [High] drm/amdkfd: zombie_work races with suspend, S0ix halt, and GPU reset - [High] drm/amdkfd: remove_all_kfd_queues_mes() skips quarantined zombie queues - [High] drm/amdkfd: self-deadlock in restore workers calling synchronous mmput() - [High] drm/amdkfd: per-process mes_hold deadlocks multi-GPU PCI bridge hot-unplug - [High] drm/amdkfd: svm_range_list_stop_restore() fails for secondary kfd_process - [Medium] drm/amdkfd: amdgpu_amdkfd_criu_resume() races with stop_userptr_restore() - [Medium] drm/amdkfd: busy-spin in kgd2kfd_teardown_processes() during cross-GPU wait - [Medium] drm/amdkfd: kfd_dqm_zombie_worker() runs during sched_halt S0ix suspend -- commit 814b42641595b4e0eb635afb5ca2d6b3f95a1173 Author: Armas Spann drm/amdkfd: add a quarantine for queues MES has not retired This commit adds a per-device zombie queue list and delayed reaper worker to keep queue and process storage alive until MES confirms retirement or restarts. It also stops the SVM and userptr restore works during MMU notifier release and retires device queues during device teardown. > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/g= pu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c > index 36840569b0377..9b79d43ab9ed4 100644 > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c > @@ -1643,6 +1643,36 @@ uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(= void *drm_priv) > return avm->pd_phys_addr; > } > =20 [ ... ] > +void amdgpu_amdkfd_gpuvm_stop_userptr_restore(void *drm_priv) > +{ > + struct amdgpu_vm *avm =3D drm_priv_to_vm(drm_priv); > + struct amdkfd_process_info *pinfo =3D avm->process_info; > + > + if (!pinfo) > + return; > + > + mutex_lock(&pinfo->notifier_lock); > + mutex_unlock(&pinfo->notifier_lock); > + > + cancel_delayed_work_sync(&pinfo->restore_userptr_work); > +} [Severity: Medium] Can amdgpu_amdkfd_criu_resume() re-queue pinfo->restore_userptr_work after amdgpu_amdkfd_gpuvm_stop_userptr_restore() returns? amdgpu_amdkfd_gpuvm_stop_userptr_restore() only drains pinfo->notifier_lock and does not record a stopped state under pinfo->lock: CPU0 CPU1 criu_resume() kfd_lookup_process_by_pid() mutex_lock(&p->mutex) kfd_process_notifier_release_internal() kfd_process_table_remove(p) ..._stop_userptr_restore() cancel_delayed_work_sync() kfd_process_dequeue_from_all_devices() mutex_lock(&p->mutex) (blocks) amdgpu_amdkfd_criu_resume() queue_delayed_work() <-- re-queued If a queue is then quarantined, restore_userptr_work keeps running and rescheduling itself against the process pid until the zombie retires. Recording a stopped flag on pinfo under pinfo->lock and checking it in amdgpu_amdkfd_criu_resume() would close this race. > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device.c b/drivers/gpu/drm/am= d/amdkfd/kfd_device.c > index 2200f5c7c38d6..faf882986f1bb 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_device.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device.c [ ... ] > @@ -1840,12 +1836,40 @@ void kgd2kfd_teardown_processes(struct amdgpu_dev= ice *adev) > if (!adev->kfd.dev) > return; > =20 > + /* > + * 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. The caller has > + * suspended KFD on a device that is still there, so the queues that > + * MES would give up are off the hardware and the scheduler is > + * stopped; nothing more could be sent for the others. > + */ > + if (adev->kfd.dev->init_complete) { > + for (unsigned int i =3D 0; i < adev->kfd.dev->num_nodes; i++) > + kfd_dqm_retire_all(adev->kfd.dev->nodes[i]->dqm); > + } [Severity: High] Can this deadlock multi-GPU PCIe bridge hot-unplug when a process has active queues on two MES GPUs behind the same bridge? On surprise hot-unplug, pciehp_unconfigure_device() marks all downstream GP= Us disconnected, takes pci_rescan_remove_lock, and removes them serially. kgd2kfd_teardown_processes() on GPU 0 signals the process with SIGBUS, calls kfd_dqm_retire_all() only on GPU 0, and waits for GPU 0's kfd_processes_cou= nt to reach zero. When the process exits, GPU 1 has not entered amdgpu_pci_remove() yet, so i= ts dqm->mes_retire_all is still false and kfd_mes_queue_retired() does not che= ck pci_dev_is_disconnected(). remove_queue_mes() on disconnected GPU 1 times out, GPU reset aborts on the disconnected bus, and the queue is quarantined with a p->mes_hold reference. Because kfd_process_wq_release() defers kfd_process_destroy_pdds() for all GPUs until p->mes_hold reaches zero, GPU= 0 waits forever while holding pci_rescan_remove_lock, preventing GPU 1's removal from ever running. Checking pci_dev_is_disconnected(adev->pdev) in kfd_mes_queue_retired() wou= ld let disconnected sibling GPUs retire their queues instead of deadlocking br= idge removal. > + warn_at =3D jiffies + 10 * HZ; > + while (!!atomic_read(&adev->kfd.dev->kfd_processes_count)) { > + if (warn_at && time_after(jiffies, warn_at)) { > + dev_warn(adev->dev, > + "waiting for %d KFD process(es) to release the device, exited ones = are kept by queues quarantined on other GPUs until those GPUs are reset\n", > + atomic_read(&adev->kfd.dev->kfd_processes_count)); > + warn_at =3D 0; > + } > cond_resched(); > + } [Severity: Medium] Should this loop sleep instead of busy-spinning with cond_resched()? Now that this wait can last past 10 seconds or indefinitely when an exited process is held by a quarantined queue on another GPU, cond_resched() keeps the task in TASK_RUNNING and burns 100% of a CPU core for the entire wait. Pairing atomic_dec_and_test(&pdd->dev->kfd->kfd_processes_count) in kfd_process_destroy_pdds() with wake_up_var() and waiting via wait_var_event_timeout() / wait_var_event() would avoid pegging a CPU. > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c b/driv= ers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c > index 5e58509787385..f31765cb8cc5d 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c [ ... ] > @@ -1803,6 +1813,158 @@ static int dqm_repin_mqd_bo(struct device_queue_m= anager *dqm, struct queue *q) > return 0; > } [ ... ] > +static void kfd_dqm_zombie_worker(struct work_struct *work) > +{ > + struct device_queue_manager *dqm =3D > + container_of(to_delayed_work(work), > + struct device_queue_manager, zombie_work); > + struct queue *q, *tmp; > + LIST_HEAD(retired); > + bool more; > + > + dqm_lock(dqm); > + /* > + * The scheduler is stopped for a suspend or a GPU reset. Nothing can > + * be sent to MES then, and the device is in no state to have buffers > + * unmapped and freed. start_cpsch() kicks the reaper when that is > + * over. A device that is being removed stays stopped, and its > + * zombies have to go now. > + */ > + if (!dqm->sched_running && !dqm->mes_retire_all) { > + dqm_unlock(dqm); > + return; > + } [Severity: Medium] Should this guard also check dqm->sched_halt? During S0ix suspend, amdgpu_amdkfd_suspend() calls kgd2kfd_stop_sched_all_nodes() -> halt_cpsch(), which sets dqm->sched_halt =3D true while leaving dqm->sched_running true. Because zombie_work runs on the non-freezable system_dfl_wq and ignores sched_halt, it keeps polling every 500 ms across S0ix and will unmap, repin, and free BOs while the device is suspending into S0ix/D3 if a queue retires. Pausing on dqm->sched_halt and kicking zombie_work in unhalt_cpsch() (matching start_cpsch()) would handle S0ix suspend and resume. [ ... ] > + more =3D !list_empty(&dqm->zombie_queues); > + dqm_unlock(dqm); > + > + list_for_each_entry_safe(q, tmp, &retired, zombie_node) { > + list_del(&q->zombie_node); > + kfd_dqm_release_zombie(dqm, q); > + } > + > + if (more) > + queue_delayed_work(system_dfl_wq, &dqm->zombie_work, > + msecs_to_jiffies(KFD_MES_ZOMBIE_POLL_MS)); > +} [Severity: High] Can kfd_dqm_release_zombie() run concurrently with system suspend or a GPU reset after dqm_unlock(dqm)? stop_cpsch() sets dqm->sched_running =3D false under dqm_lock(dqm) and retu= rns without waiting for an in-flight zombie_work to finish: kworker CPU1 kfd_dqm_zombie_worker() dqm_lock(dqm) list_move_tail(..., &retired) dqm_unlock(dqm) amdgpu_device_suspend() stop_cpsch() dqm->sched_running =3D false amdgpu_device_evict_resources() amdgpu_ttm_disable_buffer_funcs() kfd_dqm_release_zombie() kfd_queue_unref_bo_vas() <-- races with suspend / reset dqm_repin_mqd_bo() kfd_dqm_release_zombie() unmaps BO VAs, repins VRAM MQDs, frees kernel BOs, and can trigger kfd_process_wq_release() while amdgpu_device_suspend() is evicting VRAM and tearing down fences (or while kgd2kfd_pre_reset() proceeds into a GPU reset). Cancelling zombie_work outside dqm_lock(dqm) when stopping the scheduler would prevent this overlap. > +/* > + * The device is being removed. KFD has been suspended by then, which to= ok > + * every queue off the hardware that MES would let go of and stopped the > + * scheduler, or the device is gone altogether. Either way no command ca= n be > + * sent for what is left, and MES is shut down right after, so nothing a > + * queue holds can be handed to a new owner that MES would then corrupt. > + * Treat every queue as retired and release all zombies, so that process > + * teardown can finish. > + */ > +void kfd_dqm_retire_all(struct device_queue_manager *dqm) > +{ > + dqm_lock(dqm); > + dqm->mes_retire_all =3D true; > + dqm_unlock(dqm); > + > + mod_delayed_work(system_dfl_wq, &dqm->zombie_work, 0); > +} [Severity: High] Are KFD_MES_QUEUE_MAPPED zombies actually removed from MES before kfd_dqm_retire_all() frees their storage? remove_all_kfd_queues_mes() (called from stop_cpsch() and halt_cpsch()) only walks dqm->queues and each qpd->queues_list, skipping dqm->zombie_queues. = If a queue was quarantined in KFD_MES_QUEUE_MAPPED state (for example, destroy= ed while the scheduler was halted or a reset held reset_domain->sem), amdgpu_amdkfd_suspend() during amdgpu_device_ip_fini_early() leaves it mapp= ed on MES. kfd_dqm_retire_all() then immediately marks it retired and kfd_dqm_release_zombie() frees its MQD, gang context, wptr mapping, and GPU VM while MES is still running, before amdgpu_ip_block_hw_fini() shuts MES down. Walking dqm->zombie_queues in remove_all_kfd_queues_mes() would unmap still-mapped zombies on suspend and unload. [ ... ] > @@ -3051,6 +3217,12 @@ static struct queue *find_queue_by_doorbell_offset= (struct device_queue_manager * > } > } > =20 > + /* 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 =3D=3D q->properties.doorbell_off) > + return q; > + } > + > return NULL; > } [Severity: Critical] Can this unlocked walk of dqm->zombie_queues race with kfd_dqm_zombie_worker() freeing a zombie? KGD-initiated per-queue resets (amdgpu_gfx_reset_mes_compute() -> amdgpu_amdkfd_reset_mes_queue() -> kfd_reset_queue_mes()) call find_queue_by_doorbell_offset() for every hung doorbell holding only adev->gfx.mec.reset_mutex, without dqm_lock(dqm): CPU0 kworker amdgpu_gfx_reset_mes_compute() kfd_reset_queue_mes() find_queue_by_doorbell_offset() walk dqm->zombie_queues kfd_dqm_zombie_worker() dqm_lock(dqm) list_move_tail(..., &retired) dqm_unlock(dqm) list_del(&q->zombie_node) kfd_dqm_release_zombie() uninit_queue(q) dereference q / next <-- use-after-free / LIST_POISON1 Splicing a node onto the worker's stack-local retired list redirects the unlocked walker off dqm->zombie_queues onto the stack head or hits LIST_POISON1 written by list_del(). Even if find_queue_by_doorbell_offset() returns q first, nothing pins q, pdd, or p, so reset_queue_mes() can dereference freed memory after kfd_dqm_release_zombie() runs. On the same unlocked path, reset_queue_mes() -> remove_queue_mes_on_reset_option() -> kfd_mes_queue_retired() -> amdgpu_mes_ticket_release() can also race with kfd_dqm_zombie_worker() and double free ticket->status_offs via amdgpu_wb_free(). > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/a= md/amdkfd/kfd_process.c > index d30386893c6aa..878a6666fc13a 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c [ ... ] > @@ -1469,6 +1511,21 @@ 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() and to the destruction of the > + * GPU VMs: the release worker may now run long after this, and the > + * SVM and userptr restore works look up the mm of the task, which > + * can be a new one by then. Both keep queueing themselves as long > + * as they fail. > + */ > + svm_range_list_stop_restore(p); > + for (i =3D 0; i < p->n_pdds; i++) { > + /* One call covers the process, its VMs share the work */ > + if (p->pdds[i]->drm_priv) { > + amdgpu_amdkfd_gpuvm_stop_userptr_restore(p->pdds[i]->drm_priv); > + break; > + } > + } [Severity: High] Does calling cancel_delayed_work_sync() for the SVM and userptr restore wor= ks here self-deadlock when either worker drops the final mm_users reference? Both svm_range_restore_work() and amdgpu_amdkfd_restore_userptr_worker() ta= ke the mm via get_task_mm() and release it with synchronous mmput(mm) instead = of mmput_async(mm). If the process exits or calls execve() while either worker is running, that worker holds the last mm_users reference: svm_range_restore_work() [or amdgpu_amdkfd_restore_userptr_worker()] mmput(mm) __mmput(mm) -> exit_mmap(mm) -> mmu_notifier_release(mm) kfd_process_notifier_release_internal(p) cancel_delayed_work_sync(&svms->restore_work) <-- self-deadlock The system_freezable_wq kworker waits forever for itself while holding the global mmu_notifier SRCU read lock, which also blocks later synchronize_srcu() calls in mm/mmu_notifier.c and prevents system suspend. Switching both workers to mmput_async(mm) (as svm_range_deferred_list_work() already does) avoids the self-deadlock. > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/a= mdkfd/kfd_svm.c > index 9affc79eb8330..e36bfba0fb644 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c > @@ -2150,9 +2150,19 @@ svm_range_evict(struct svm_range *prange, struct m= m_struct *mm, > if (r) > pr_debug("failed to quiesce KFD\n"); > =20 > - pr_debug("schedule to restore svm %p ranges\n", svms); > - queue_delayed_work(system_freezable_wq, &svms->restore_work, > - msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS)); > + /* > + * -ESRCH: the process is being released, there are > + * no queues to restore, and the restore work has > + * been stopped by svm_range_list_stop_restore(). > + */ > + if (r !=3D -ESRCH) { > + unsigned long delay =3D > + msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS); > + > + pr_debug("schedule to restore svm %p ranges\n", svms); > + queue_delayed_work(system_freezable_wq, > + &svms->restore_work, delay); > + } > } > mutex_unlock(&svms->evict_lock); [Severity: High] Does relying on kgd2kfd_quiesce_mm() returning -ESRCH stop svms->restore_work for a secondary kfd_process? kgd2kfd_quiesce_mm() looks up the process via find_process_by_mm(), which matches process->context_id =3D=3D KFD_CONTEXT_ID_PRIMARY. When a secondary kfd_process is closed via kfd_release() -> kfd_process_notifier_release_internal() while the primary process for the same mm is still alive, a subsequent MMU invalidation on an unregistered SVM range (created via svm_range_restore_pages() on a GPU retry fault, with XNA= CK then disabled via kfd_ioctl_set_xnack_mode()) gets 0 from kgd2kfd_quiesce_mm() instead of -ESRCH and re-queues the secondary process's svms->restore_work. If that invalidation runs during svm_range_list_fini() between cancel_delayed_work_sync(&p->svms.restore_work) and svm_range_remove_notifier(), svms->restore_work fires after kfree(p), causi= ng a use-after-free in svm_range_restore_work(). Setting a per-svms stopped flag under svms->evict_lock in svm_range_list_stop_restore() and checking it before queueing svms->restore_work would handle secondary processes as well. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204125251.1070= 3-1-zappel@simple-co.de?part=3D4