From: sashiko-bot@kernel.org
To: "Armas Spann" <zappel@simple-co.de>, amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH v1 5/9] drm/amdkfd: keep queue storage until MES confirmed retirement
Date: Tue, 24 Nov 2026 16:29:23 +0000 [thread overview]
Message-ID: <sashiko-outbox-231569@kernel.org> (raw)
In-Reply-To: <20261124143433.49736-6-zappel@simple-co.de>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] drm/amdkfd: active queue count underflow on reset of
quarantined queue
- [Medium] drm/amdkfd: bo_va->queue_refcount race and imbalance in
pqm_destroy_queue()
- [Medium] drm/amdkfd: premature release of user queue buffers when
create_queue_cpsch() quarantines queue
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] drm/amdkfd: broken queue state and count in
kfd_dqm_suspend_bad_queue_mes() [1]
--
commit 848d4620ce3c40efb53d488158a7d0f3fddf5733
Author: Armas Spann <zappel@simple-co.de>
drm/amdkfd: keep queue storage until MES confirmed retirement
This commit quarantines queues whose retirement has not been confirmed by
MES across queue creation failure, queue destruction, and process
termination so their MQDs, doorbell IDs, SDMA IDs, and buffers are not
reused while MES may still access them.
> 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 2f57cb06f540f..0d40053b34c88 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
[ ... ]
> @@ -2877,6 +2878,16 @@ static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,
> list_del(&q->list);
> if (q->properties.is_active)
> decrement_queue_count(dqm, qpd, q);
> + if (!kfd_mes_queue_retired(dqm, q)) {
> + /*
> + * ADD_QUEUE was not confirmed, MES may still map the queue.
> + * The MQD and the doorbell and SDMA ids stay with it.
> + */
> + q->zombie_no_user_bos = true;
[Severity: Medium]
Does setting q->zombie_no_user_bos here allow the user queue buffers to be
unmapped and freed while MES may still map the queue?
When pqm_create_queue() returns an error to kfd_ioctl_create_queue(), the
ioctl error path calls kfd_queue_unref_bo_vas() and
kfd_queue_release_buffers(), dropping the BO VA queue refcounts, the BO
references, and the CWSR SVM range reference immediately. Because queue
creation failed, userspace can unmap and reuse wptr_bo, rptr_bo, ring_bo,
eop_buf_bo, and cwsr_bo while the MQD submitted to MES and the pinned
wptr_bo_gart mapping still point to them. If the timed-out ADD_QUEUE
executes late, MES, CP, or SDMA can read or write unmapped or reallocated
user memory.
> + dqm_quarantine_queue(dqm, qpd, q);
> + dqm_unlock(dqm);
> + return retval;
> + }
[Severity: Medium]
Should this path also clear q->properties.is_active after calling
decrement_queue_count(), the same way destroy_queue_cpsch() and
process_termination_cpsch() do?
If MES later reports the quarantined queue as hung during a per-queue
reset, kfd_reset_queue_mes() finds it on dqm->zombie_queues via
find_queue_by_doorbell_offset() and calls reset_queue_mes() ->
set_queue_as_reset():
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c:set_queue_as_reset() {
...
if (q->properties.is_active) {
q->properties.is_active = false;
decrement_queue_count(dqm, qpd, q);
}
}
Because q->properties.is_active was left true, set_queue_as_reset() calls
decrement_queue_count() a second time and underflows the DQM active queue
counters.
[ ... ]
> @@ -3680,20 +3712,18 @@ static int process_termination_cpsch(struct device_queue_manager *dqm,
>
> /* Clear all user mode queues */
> list_for_each_entry(q, &qpd->queues_list, list) {
> - if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
> - deallocate_sdma_queue(dqm, q);
> - else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
> - deallocate_sdma_queue(dqm, q);
> -
> if (q->properties.is_active) {
> decrement_queue_count(dqm, qpd, q);
> + /* set_queue_as_reset() must not count it down again */
> + q->properties.is_active = false;
> + }
[Severity: High]
This problem wasn't introduced by this patch, but
kfd_dqm_suspend_bad_queue_mes() has the same double decrement and leaves
the queue marked inactive on reset failure [1]:
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c:kfd_dqm_suspend_bad_queue_mes() {
...
list_for_each_entry(q, &qpd->queues_list, list) {
if (q->doorbell_id == doorbell_id && q->properties.is_active) {
reset_queues_mes(dqm, q);
q->properties.is_evicted = true;
q->properties.is_active = false;
decrement_queue_count(dqm, qpd, q);
break;
}
}
...
}
When reset_queues_mes() resets the queue as hung via reset_queue_mes() ->
set_queue_as_reset(), set_queue_as_reset() already clears
q->properties.is_active and calls decrement_queue_count(), so the
unconditional decrement_queue_count() afterwards underflows
dqm->active_queue_count. If reset_queues_mes() fails instead, clearing
q->properties.is_active causes later paths to treat a still-mapped queue
as inactive.
> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
> index ef9ad6dc874dc..29827a736f7d4 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c
[ ... ]
> @@ -571,23 +590,43 @@ int pqm_destroy_queue(struct process_queue_manager *pqm, unsigned int qid)
> }
>
> if (pqn->q) {
> - retval = kfd_queue_unref_bo_vas(pdd, &pqn->q->properties);
> + struct queue *q = pqn->q;
> +
> + retval = kfd_queue_unref_bo_vas(pdd, &q->properties);
> if (retval)
> goto err_destroy_queue;
>
> - dqm = pqn->q->device->dqm;
> - retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, pqn->q);
> + dqm = q->device->dqm;
> + retval = dqm->ops.destroy_queue(dqm, &pdd->qpd, q);
> if (retval) {
> pr_err("Pasid 0x%x destroy queue %d failed, ret %d\n",
> pdd->pasid,
> - pqn->q->properties.queue_id, retval);
> - if (retval != -ETIME && retval != -EIO)
> + q->properties.queue_id, retval);
> + /* The dqm left the queue alone, it stays usable */
> + if (!q->dqm_dequeued) {
> + kfd_queue_ref_bo_vas(pdd, &q->properties);
> goto err_destroy_queue;
> + }
> + }
[Severity: Medium]
Can calling kfd_queue_unref_bo_vas() before dqm->ops.destroy_queue() race
when wait_on_destroy_queue() drops q->process->mutex for a suspended queue
of a debugged process?
CPU0 CPU1
pqm_destroy_queue(qid)
kfd_queue_unref_bo_vas()
bo_va->queue_refcount-- (1 -> 0)
destroy_queue_cpsch()
wait_on_destroy_queue()
q->properties.is_being_destroyed = true
dqm_unlock(dqm)
mutex_unlock(&q->process->mutex)
wait_event_interruptible(...)
pqm_destroy_queue(qid)
kfd_queue_unref_bo_vas()
/* refcount is 0, no-op */
destroy_queue_cpsch()
wait_on_destroy_queue()
returns -EBUSY
/* !q->dqm_dequeued */
kfd_queue_ref_bo_vas()
bo_va->queue_refcount++ (0 -> 1)
/* interrupted by signal */
returns -ERESTARTSYS
/* !q->dqm_dequeued */
kfd_queue_ref_bo_vas()
bo_va->queue_refcount++ (1 -> 2) <-- refcount leaked
Because kfd_queue_unref_bo_va() only decrements when bo_va->queue_refcount
is non-zero while kfd_queue_ref_bo_va() increments unconditionally, a
second destroy attempt during the wait_on_destroy_queue() sleep window
permanently inflates bo_va->queue_refcount and prevents the BOs from being
unmapped. While CPU0 is sleeping with bo_va->queue_refcount == 0, another
thread can also unmap the queue's BOs via unmap_bo_from_gpuvm() before the
queue is destroyed.
[1] https://sashiko.dev/#/bug/linux-7affa616-c8be-4685-80a8-eb0c3ae7d034
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124143433.49736-1-zappel@simple-co.de?part=5
next prev parent reply other threads:[~2026-11-24 16:29 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 14:34 [PATCH v1 0/9] drm/amdgpu/amdkfd: fix MES queue lifecycle on multi-GPU Armas Spann
2026-11-24 14:34 ` [PATCH v1 1/9] drm/amdgpu/mes: distinguish rejected from timed out commands Armas Spann
2026-11-24 16:19 ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 2/9] drm/amdkfd: factor out freeing of per-queue MES resources Armas Spann
2026-11-24 16:09 ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 3/9] drm/amdkfd: track whether MES knows each queue Armas Spann
2026-11-24 16:30 ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 4/9] drm/amdkfd: add a quarantine for queues MES has not retired Armas Spann
2026-11-24 16:29 ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 5/9] drm/amdkfd: keep queue storage until MES confirmed retirement Armas Spann
2026-11-24 16:29 ` sashiko-bot [this message]
2026-11-24 14:34 ` [PATCH v1 6/9] drm/amdkfd: keep eviction and restore consistent when MES fails Armas Spann
2026-11-24 16:30 ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 7/9] drm/amdkfd: apply queue property updates only after unmap Armas Spann
2026-11-24 16:42 ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 8/9] drm/amdkfd: reconcile queue state with MES after a restart Armas Spann
2026-11-24 16:53 ` sashiko-bot
2026-11-24 14:34 ` [PATCH v1 9/9] drm/amdkfd: force CWSR on with more than one MES GPU Armas Spann
2026-11-25 14:15 ` [PATCH v1 0/9] drm/amdgpu/amdkfd: fix MES queue lifecycle on multi-GPU 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-231569@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