From: sashiko-bot@kernel.org
To: "Armas Spann" <zappel@simple-co.de>, amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH 5/8] drm/amdkfd: keep queue storage until MES confirmed retirement
Date: Fri, 04 Dec 2026 16:31:21 +0000 [thread overview]
Message-ID: <sashiko-outbox-250194@kernel.org> (raw)
In-Reply-To: <20261204125251.10703-6-zappel@simple-co.de>
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 <zappel@simple-co.de>
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/drivers/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_manager *dqm, struct queue *q)
> */
>
> /* dqm lock held. @q is off qpd->queues_list already. */
> -static void __maybe_unused dqm_quarantine_queue(struct device_queue_manager *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 = qpd_to_pdd(qpd);
>
[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 = 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);
>
> + /*
> + * 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 = kfd_mes_queue_retired(dqm, q);
> + if (retired) {
> + deallocate_doorbell(qpd, q);
> + if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
> + q->properties.type == 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_mes()
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);
> }
>
> + /*
> + * 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 = 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 = 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;
> }
>
> +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 = amdgpu_vm_bo_find(vm, bo);
> + if (bo_va)
> + bo_va->queue_refcount++;
> +}
[ ... ]
> + err = 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 = NULL;
> + properties->rptr_bo = NULL;
> + properties->ring_bo = NULL;
> + properties->eop_buf_bo = NULL;
> + properties->cwsr_bo = 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 = bo_va2->base.next
kfree(bo_va2)
base = 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 != 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 = 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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204125251.10703-1-zappel@simple-co.de?part=5
next prev parent reply other threads:[~2026-12-04 16:31 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 12:52 [PATCH 0/8] drm/amdkfd: keep queue storage until MES has retired the queue Armas Spann
2026-12-04 12:52 ` [PATCH 1/8] drm/amdgpu/mes: distinguish rejected from timed out commands Armas Spann
2026-12-04 14:30 ` sashiko-bot
2026-12-04 12:52 ` [PATCH 2/8] drm/amdkfd: make the per-queue MES resource cleanup usable by the dqm Armas Spann
2026-12-04 12:52 ` [PATCH 3/8] drm/amdkfd: track whether MES knows each queue Armas Spann
2026-12-04 15:25 ` sashiko-bot
2026-12-04 12:52 ` [PATCH 4/8] drm/amdkfd: add a quarantine for queues MES has not retired Armas Spann
2026-12-04 16:00 ` sashiko-bot
2026-12-04 12:52 ` [PATCH 5/8] drm/amdkfd: keep queue storage until MES confirmed retirement Armas Spann
2026-12-04 16:31 ` sashiko-bot [this message]
2026-12-04 12:52 ` [PATCH 6/8] drm/amdkfd: keep eviction and restore consistent when MES fails Armas Spann
2026-12-04 17:07 ` sashiko-bot
2026-12-04 12:52 ` [PATCH 7/8] drm/amdkfd: apply queue property updates only after unmap Armas Spann
2026-12-04 17:30 ` sashiko-bot
2026-12-04 12:52 ` [PATCH 8/8] drm/amdkfd: reconcile queue state with MES after a restart Armas Spann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-250194@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=amd-gfx@lists.freedesktop.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=zappel@simple-co.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox