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 12F63606A322 for ; Fri, 4 Dec 2026 16:31:34 +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=cq/kViS0 Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A8BAD10E7AA; Fri, 4 Dec 2026 16:31:28 +0000 (UTC) Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4DF0A113060 for ; Fri, 4 Dec 2026 16:31:23 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 40DDA61241; Fri, 4 Dec 2026 16:31:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D7F8C1F00897; Fri, 4 Dec 2026 16:31:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401882; bh=vpRWg73KDmxjs8rP4MRYPRHrgojcsHDMUy6AXBcnMgA=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=cq/kViS0yJPKOJKbGvWyflzA3DYYAxyRcbtu8aC6iVKXTTdYc8TZBo41kpqJ1Q9AR UKhXZBJsyh+1YPm6GYiDm3dK3T4zZ8gu9IzPZaKEV1youT26OpK5L7d4PH4HmVn+wf Mc4l6rp46EY+xyjtBo8NrWGMducah8ta5Bqc4GZ3DtLqUnLejHHKq0gfqg+5FQj8ka ZCC8SUhtytvKQoKzcGtWzc10k31zPmIMNub05sRBXeQXtKNtkAJvejYvfoCyhsAtV8 YlbnNWuMpshc4tFNquifdF9rOahywQZ7PPqN/hRfKXnTwXBXbdR1N67dz9q+kCAXIp sUxszGjn/eImQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 5/8] drm/amdkfd: keep queue storage until MES confirmed retirement To: "Armas Spann" , amd-gfx@lists.freedesktop.org In-Reply-To: <20261204125251.10703-6-zappel@simple-co.de> References: <20261204125251.10703-1-zappel@simple-co.de> <20261204125251.10703-6-zappel@simple-co.de> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 16:31:21 +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 5 potential issues to consider: - [High] drm/amdkfd: lockless zombie_queues walk and UAF in kfd_reset_queue_mes() - [Medium] drm/amdkfd: remove_all_kfd_queues_mes() skips quarantined zombie queues - [Medium] drm/amdkfd: kfd_process_dequeue_from_device() flushes MES ctx with zombies - [Medium] drm/amdkfd: kfd_queue_hold_buffers() leaves zombie_no_svm_ref unset - [Medium] drm/amdkfd: kfd_queue_hold_bo() calls amdgpu_vm_bo_find() unlocked -- commit d3b0d0328e67d21504e168f543e5bf4060e74d28 Author: Armas Spann drm/amdkfd: keep queue storage until MES confirmed retirement This commit wires up the MES queue quarantine across queue creation failure, queue destruction, and process termination so that doorbell and SDMA IDs, MQDs, MES resources, and user buffer references are kept until MES confirms queue retirement. It also defers releasing IDs and buffer references until after the DQM unlinks a queue and ensures is_active is cleared on deactivation. > 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 f31765cb8cc5d..58c8efd168011 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c [ ... ] > @@ -1832,9 +1833,9 @@ static int dqm_repin_mqd_bo(struct device_queue_man= ager *dqm, struct queue *q) > */ > =20 > /* dqm lock held. @q is off qpd->queues_list already. */ > -static void __maybe_unused dqm_quarantine_queue(struct device_queue_mana= ger *dqm, > - struct qcm_process_device *qpd, > - struct queue *q) > +static void dqm_quarantine_queue(struct device_queue_manager *dqm, > + struct qcm_process_device *qpd, > + struct queue *q) > { > struct kfd_process_device *pdd =3D qpd_to_pdd(qpd); > =20 [Severity: High] Can populating dqm->zombie_queues race with KGD-initiated per-queue resets in kfd_reset_queue_mes()? dqm_quarantine_queue() and kfd_dqm_zombie_worker() mutate dqm->zombie_queues under dqm_lock(dqm). When KGD resets a hung compute queue via gfx_v11_0_reset_kcq() -> amdgpu_gfx_reset_mes_compute(), it holds only adev->gfx.mec.reset_mutex and calls kfd_reset_queue_mes() without dqm_lock(dqm): CPU0 CPU1 gfx_v11_0_reset_kcq() amdgpu_gfx_reset_mes_compute() kfd_reset_queue_mes() find_queue_by_doorbell_offset() /* walks dqm->zombie_queues without dqm_lock */ kfd_dqm_zombie_worker() dqm_lock(dqm) list_move_tail(&q->zombie_node, &retired) dqm_unlock(dqm) list_del(&q->zombie_node) kfd_dqm_release_zombie(dqm, q) uninit_queue(q) reset_queue_mes(dqm, q) q->mes_reset_seen =3D true <-- use-after-free CPU0 can follow LIST_POISON1 or walk into CPU1's stack-local retired list head during traversal, or write to the freed struct queue and concurrently release q->mes_ticket in reset_queue_mes(). Note that kfd_reset_queue_mes() cannot unconditionally take dqm_lock(dqm) because KFD's own reset_queues_mes() invokes amdgpu_gfx_reset_mes_compute() with dqm_lock(dqm) already held. [ ... ] > @@ -3620,6 +3639,21 @@ static int destroy_queue_cpsch(struct device_queue= _manager *dqm, > pr_debug("Total of %d queues are accountable so far\n", > dqm->total_queue_count); > =20 > + /* > + * MES identifies the queue by its doorbell. The doorbell and SDMA > + * ids can be handed out again only when MES has forgotten the queue. > + * Until then the queue keeps them, together with its MQD. > + */ > + retired =3D kfd_mes_queue_retired(dqm, q); > + if (retired) { > + deallocate_doorbell(qpd, q); > + if (q->properties.type =3D=3D KFD_QUEUE_TYPE_SDMA || > + q->properties.type =3D=3D KFD_QUEUE_TYPE_SDMA_XGMI) > + deallocate_sdma_queue(dqm, q); > + } else { > + dqm_quarantine_queue(dqm, qpd, q); > + } > + [Severity: Medium] Should remove_all_kfd_queues_mes() also walk dqm->zombie_queues? When remove_queue_mes() returns -EIO without submitting REMOVE_QUEUE (for example during reset_domain->sem contention or a temporary MES fence), destroy_queue_cpsch() and process_termination_cpsch() move the queue onto dqm->zombie_queues in KFD_MES_QUEUE_MAPPED state. remove_all_kfd_queues_me= s() only iterates dqm->queues, so halt_cpsch() and stop_cpsch() return 0 with that zombie still mapped in MES. Callers such as psp_performance_monitor_hw() then fail amdgpu_device_ip_wait_for_idle() on GFX, and stop_cpsch() clears dqm->sched_running so kfd_dqm_zombie_worker() cannot remove the zombie until resume. [ ... ] > @@ -3879,6 +3913,25 @@ static int process_termination_cpsch(struct device= _queue_manager *dqm, > up_read(&dqm->dev->adev->reset_domain->sem); > } > =20 > + /* > + * Quarantine what MES has not let go of. All of it now, before the > + * lock is dropped for the first time below: the process is off > + * dqm->queues, so from here on the zombie list is the only place > + * where a per-queue reset finds a queue that MES reports as hung. > + * Whether MES has let go is decided once per queue. What is left on > + * the list afterwards is retired and stays that way, and its SDMA id > + * goes with its MQD. > + */ > + list_for_each_entry_safe(q, q_next, &qpd->queues_list, list) { > + if (kfd_mes_queue_retired(dqm, q)) > + continue; > + > + list_del(&q->list); > + q->dqm_dequeued =3D true; > + qpd->queue_count--; > + dqm_quarantine_queue(dqm, qpd, q); > + } > + [Severity: Medium] Does kfd_process_dequeue_from_device() flush the MES process context even when process_termination_cpsch() quarantined queues for that process? Right after process_termination_cpsch() returns, kfd_process_dequeue_from_device() unconditionally calls amdgpu_mes_flush_shader_debugger() with flags.process_ctx_flush =3D true. When MES is still responsive and owns a quarantined queue, this tells MES to retire the process context while that queue still references pdd->proc_ctx_gpu_addr and pdd->proc_ctx_array_index. When MES is fenced off after a timed-out REMOVE_QUEUE, amdgpu_mes_flush_shader_debugger() also bypasses kfd_mes_fenced() and submits another blocking packet to the stalled MES ring, stalling process teardown and the GPU reset needed for recovery. [ ... ] > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_queue.c b/drivers/gpu/drm/amd= /amdkfd/kfd_queue.c > index 435393221930a..94b23009ca890 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_queue.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_queue.c [ ... ] > @@ -426,6 +427,81 @@ int kfd_queue_unref_bo_vas(struct kfd_process_device= *pdd, > return 0; > } > =20 > +static void kfd_queue_hold_bo(struct amdgpu_vm *vm, struct amdgpu_bo *bo) > +{ > + struct amdgpu_bo_va *bo_va; > + > + if (!bo) > + return; > + > + amdgpu_bo_ref(bo); > + bo_va =3D amdgpu_vm_bo_find(vm, bo); > + if (bo_va) > + bo_va->queue_refcount++; > +} [ ... ] > + err =3D amdgpu_bo_reserve(vm->root.bo, true); > + if (err) { > + /* > + * Nothing was taken. The pointers are copies of the ones > + * the caller of the create owns, forget them so that the > + * release has nothing to drop. > + */ > + properties->wptr_bo =3D NULL; > + properties->rptr_bo =3D NULL; > + properties->ring_bo =3D NULL; > + properties->eop_buf_bo =3D NULL; > + properties->cwsr_bo =3D NULL; > + return err; > + } > + > + kfd_queue_hold_bo(vm, properties->wptr_bo); > + kfd_queue_hold_bo(vm, properties->rptr_bo); > + kfd_queue_hold_bo(vm, properties->ring_bo); > + kfd_queue_hold_bo(vm, properties->eop_buf_bo); > + kfd_queue_hold_bo(vm, properties->cwsr_bo); [Severity: Medium] Can kfd_queue_hold_bo() trigger a use-after-free in amdgpu_vm_bo_find() because each queue BO is not reserved? kfd_queue_hold_buffers() reserves only vm->root.bo, but KFD user BOs have individual dma_resv locks and amdgpu_vm_bo_find() walks bo->vm_bo across all VMs mapping the BO. If another VM shares the BO, it can concurrently unlink and free its amdgpu_bo_va under bo->tbo.base.resv: CPU0 CPU1 kfd_queue_hold_buffers() amdgpu_bo_reserve(vm->root.bo, ...) kfd_queue_hold_bo(vm, bo) amdgpu_vm_bo_find(vm, bo) /* walks bo->vm_bo unlocked */ amdgpu_vm_bo_del(adev, bo_va2) /* holds bo->tbo.base.resv */ *base =3D bo_va2->base.next kfree(bo_va2) base =3D base->next <-- use-after-free This can also miss incrementing bo_va->queue_refcount, leaving an unbalanced decrement when the zombie is later released. [ ... ] > + /* The CWSR area of a compute queue is a BO, or an SVM range */ > + if (properties->type !=3D KFD_QUEUE_TYPE_COMPUTE || properties->cwsr_bo) > + return 0; [Severity: Medium] Does returning 0 here leave q->zombie_no_svm_ref unset for SDMA and BO-backed CWSR queues? pqm_create_queue() only sets q->zombie_no_svm_ref =3D true when kfd_queue_hold_buffers() returns non-zero. When a quarantined SDMA or BO-backed CWSR queue from a failed create is later reaped, kfd_dqm_release_zombie() calls kfd_queue_release_buffers() with false, which unconditionally calls kfd_queue_buffer_svm_put() on properties->ctx_save_restore_area_address. That drops an SVM queue_refcount belonging to another active compute queue sharing the range, allowing munmap() to skip kgd2kfd_quiesce_mm() while GPU waves are still running. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204125251.1070= 3-1-zappel@simple-co.de?part=3D5