AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Armas Spann" <zappel@simple-co.de>, amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH 3/8] drm/amdkfd: track whether MES knows each queue
Date: Fri, 04 Dec 2026 15:25:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-250097@kernel.org> (raw)
In-Reply-To: <20261204125251.10703-4-zappel@simple-co.de>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] drm/amdkfd: unlocked KGD reset in reset_queue_mes() races on
  ticket and fence
- [High] drm/amdkfd: late REMOVE_QUEUE completion skips GPU reset and
  leaves scheds stopped
- [Medium] drm/amdkfd: kfd_mes_fence() installs permanent fence on
  answered ticket
- [Medium] drm/amdkfd: ticketless MES fence permanently locks DQM when
  gpu_recovery is 0
- [Medium] drm/amdkfd: TOCTOU in __remove_queue_mes() and
  reset_queues_mes() drops DONE
- [Medium] drm/amdkfd: reset_queue_mes() swallows -EIO from
  remove-after-reset cleanup
- [Medium] drm/amdkfd: reset_queues_mes() omits sched_halt and
  sched_running check
--

commit 5f554466aebb58f6575996926feaccfc3cb95937
Author: Armas Spann <zappel@simple-co.de>

drm/amdkfd: track whether MES knows each queue

This commit adds per-queue MES state, firmware generation, and command
ticket tracking in the device queue manager, along with a grace wait and
MES fencing when queue commands time out.

> 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 cc7a1f770408f..5e58509787385 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c

[ ... ]

> +	dqm->mes_fenced = true;
> +	dqm->mes_fence_gen = kfd_mes_gen(dqm);
> +	dqm->mes_fence_has_seq = kfd_mes_ticket_pending(dqm, q);
> +	if (dqm->mes_fence_has_seq)
> +		dqm->mes_fence_ticket = q->mes_ticket;
> +}
> +
> +/*
> + * A command for @q, or the recovery that was run for it, failed with @r.
> + * Fence MES off if that was for want of an answer. Returns true when MES
> + * is fenced off, by this or by a failure further down that was only passed
> + * up: the caller then has to get the GPU reset, nothing else lifts the
> + * fence of a scheduler that does not answer.
> + */
> +static bool kfd_mes_fence_unanswered(struct device_queue_manager *dqm,
> +				     struct queue *q, int r)
> +{
> +	if (!kfd_mes_queue_retired(dqm, q) &&
> +	    (r == -ETIMEDOUT || kfd_mes_ticket_pending(dqm, q)))
> +		kfd_mes_fence(dqm, q);
> +
> +	return kfd_mes_fenced(dqm);
> +}

[Severity: Medium]
Can kfd_mes_fence() install a permanent, sequence-less fence for a ticket
that MES has already answered?

In reset_queues_mes() -> amdgpu_gfx_reset_mes_compute(), if
remove_hw_queue() returns -ETIMEDOUT with a valid q->mes_ticket,
amdgpu_mes_resume() still runs for up to 2.1 s before -ETIMEDOUT is
returned.  If MES catches up during resume and answers the ticket with
AMDGPU_MES_TICKET_FAILED, kfd_mes_fence_unanswered() sees
!kfd_mes_queue_retired() && r == -ETIMEDOUT and calls kfd_mes_fence().

Inside kfd_mes_fence(), kfd_mes_ticket_pending() returns false for an
already-answered ticket, setting dqm->mes_fence_has_seq to false.  The
fence then can never be lifted by amdgpu_mes_ticket_answered() and only
clears on a firmware restart, triggering spurious GPU resets (or wedging
the DQM with -EIO when amdgpu_gpu_recovery is 0).

Checking whether q->mes_ticket is valid and already answered before
fencing would avoid conflating an answered ticket with a ticketless
command.

[ ... ]

> @@ -270,14 +440,30 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,

[ ... ]

>  	if (r) {
>  		dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=0x%x\n",
>  			q->properties.doorbell_off);
>  		dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
> +		/*
> +		 * No answer: anything sent from now on would time out behind
> +		 * this command. ADD_QUEUE carries no ticket, so the fence is
> +		 * lifted by the firmware restart that the GPU reset asked for
> +		 * here performs. A rejection means MES answers, no fence.
> +		 */
> +		if (r == -ETIMEDOUT)
> +			kfd_mes_fence(dqm, q);
>  		kfd_hws_hang(dqm);
>  		return r;
>  	}

[Severity: Medium]
Does arming a ticketless fence here permanently wedge the DQM when
amdgpu_gpu_recovery is 0?

ADD_QUEUE carries no ticket, so kfd_mes_fence() sets mes_fence_has_seq
to false, meaning only an increment of mes.fw_gen lifts the fence.  When
amdgpu_gpu_recovery is 0, kfd_hws_hang() -> amdgpu_amdkfd_gpu_reset()
skips scheduling reset_work, so every later KFD queue command fails with
-EIO even if MES recovers.

The same happens at the unanswered: label in
remove_queue_mes_on_reset_option() on a ring-full REMOVE_QUEUE, where
kfd_mes_fence_unanswered() runs before checking amdgpu_gpu_recovery.
Only arming a ticketless fence when GPU recovery is enabled would keep
the fence paired with the reset that lifts it.

[ ... ]

> +static int __remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
> +			      bool flush_mes_queue)
>  {
> -	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
> -	int r;
> +	struct amdgpu_device *adev = dqm->dev->adev;
>  	struct mes_remove_queue_input queue_input;
> +	int r;
>  
> -	/* queue was already removed during reset */
> -	if (q->properties.is_reset)
> +	/* An earlier REMOVE_QUEUE completed late: a second one is rejected */
> +	if (kfd_mes_queue_retired(dqm, q))
>  		return 0;
>  
> -	if (!dqm->sched_running || dqm->sched_halt)
> -		return 0;
> +	if (kfd_mes_fenced(dqm))
> +		return -EIO;
> +
> +	/*
> +	 * An earlier REMOVE_QUEUE for this queue is still in the MES ring.
> +	 * Do not send another one behind it, and keep its status slot.
> +	 */
> +	if (kfd_mes_ticket_pending(dqm, q))
> +		return -ETIMEDOUT;
> +
>  	if (!down_read_trylock(&adev->reset_domain->sem))
>  		return -EIO;
>  
> +	/* The earlier command, if there was one, has been answered */
> +	amdgpu_mes_ticket_release(adev, &q->mes_ticket);

[Severity: Medium]
Can a late AMDGPU_MES_TICKET_DONE completion be dropped between
kfd_mes_queue_retired() and kfd_mes_ticket_pending()?

If MES finishes an earlier timed-out REMOVE_QUEUE right after
kfd_mes_queue_retired() returns false, kfd_mes_fenced() sees the fence
passed and lifts the fence, and kfd_mes_ticket_pending() returns false
because the state is now AMDGPU_MES_TICKET_DONE.  The ticket is then
released without setting q->mes_state to KFD_MES_QUEUE_UNMAPPED.

A duplicate REMOVE_QUEUE is sent for an already-removed queue, which MES
rejects, escalating to recovery and a spurious GPU reset.  The same
window exists in reset_queues_mes(); re-checking kfd_mes_queue_retired()
after kfd_mes_fenced() (or polling the ticket once) would close it.

[ ... ]

> +	/* remove_hw_queue failure indicates a queue hang. reset the queue */
>  	r = reset_queues_mes(dqm, q);
> -	if (r && amdgpu_gpu_recovery) {
> -		dev_err(adev->dev, "failed to remove queue from MES, doorbell=0x%x\n",
> -			q->properties.doorbell_off);
> +	if (!r)
> +		return 0;
> +
> +unanswered:
> +	/*
> +	 * MES may have let go of @q in the meantime, whatever else failed.
> +	 * That settles @q, but not a fence: the recovery may have been left
> +	 * without an answer for another queue it reset on the way, and this
> +	 * is where the GPU reset that lifts the fence is asked for.
> +	 */
> +	retired = kfd_mes_queue_retired(dqm, q);
> +	if (!kfd_mes_fence_unanswered(dqm, q, r) && retired)
> +		return 0;

[Severity: High]
Can returning 0 here skip kfd_hws_hang() and leave all KGD compute DRM
schedulers permanently stopped?

When reset_queues_mes() calls amdgpu_gfx_reset_mes_compute(), it stops
the compute schedulers with amdgpu_gfx_reset_stop_compute_scheds().  If
reset_queue_mes() times out on q's remove-after-reset cleanup, it fences
MES on q->mes_ticket and returns -ETIMEDOUT, so
amdgpu_gfx_reset_mes_compute() aborts its hung-queue loop, calls
amdgpu_mes_resume(), and skips amdgpu_gfx_reset_start_compute_scheds()
because ring_err is -ETIMEDOUT.

If MES finishes q's REMOVE_QUEUE with AMDGPU_MES_TICKET_DONE during
amdgpu_mes_resume(), reset_queues_mes() still exits via goto fail
without signaling reset or wedged events.  Here, kfd_mes_queue_retired()
returns true and kfd_mes_fence_unanswered() lifts the fence and returns
false, so remove_queue_mes_on_reset_option() returns 0 without calling
kfd_hws_hang().

Because no GPU reset is scheduled, the compute DRM schedulers stay
stopped, any other hung queues in the batch remain unreset, and
userspace receives no reset notification.

[ ... ]

> @@ -435,9 +720,24 @@ static int reset_queue_mes(struct device_queue_manager *dqm, struct queue *q,
>  						ffs(dqm->dev->xcc_mask) - 1);
>  	if (r)
>  		return r;
> -	/* Proceed remove_queue with reset=true */
> -	remove_queue_mes_on_reset_option(dqm, q, &pdd->qpd, true, true);
> +	/* For reset_queues_mes(): @q is one of the queues MES reset */
> +	q->mes_reset_seen = true;
> +	/*
> +	 * Proceed remove_queue with reset=true. Its result matters: a
> +	 * REMOVE_QUEUE that MES did not answer fails this reset, so that the
> +	 * caller escalates to a GPU reset instead of sending more commands
> +	 * behind it. A rejected cleanup does not fail it, MES answers and
> +	 * the queue itself has been reset; the queue stays UNKNOWN and keeps
> +	 * 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);

[Severity: High]
Does calling remove_queue_mes_on_reset_option() from reset_queue_mes()
without dqm_lock(dqm) introduce writeback pool corruption, torn fence
state, and a widened use-after-free on struct queue?

KGD-initiated per-queue resets (for example gfx_v11_0_reset_kcq() ->
amdgpu_gfx_reset_mes_compute() -> amdgpu_amdkfd_reset_mes_queue() ->
kfd_reset_queue_mes() -> reset_queue_mes()) hold only
adev->gfx.mec.reset_mutex without dqm_lock(dqm), racing with KFD
threads tearing down or removing the same queue:

CPU0 (KGD reset, no dqm_lock)     CPU1 (KFD destroy, holds dqm_lock)
reset_queue_mes()
  remove_queue_mes_on_reset_option()
    __remove_queue_mes()
      amdgpu_mes_ticket_release()
        if (ticket->valid)
                                  remove_queue_mes() /
                                  kfd_queue_free_mes_resources()
                                    amdgpu_mes_ticket_release()
                                      if (ticket->valid)
                                      amdgpu_wb_free(status_offs)
                                      ticket->valid = false
        amdgpu_wb_free(status_offs)  <-- double free in adev->wb.used
        ticket->valid = false

Because amdgpu_wb_get() is a global first-fit allocator, double-freeing
status_offs can clear a slot just reassigned to an unrelated ring or MES
command.

Two other hazards occur on the same unlocked path.  On timeout,
kfd_mes_fence() writes dqm->mes_fenced = true before writing
mes_fence_gen, mes_fence_has_seq, and mes_fence_ticket with no lock or
barriers, so a concurrent kfd_mes_fenced() can observe stale state and
drop the fence or poll a torn ticket.  In addition,
kfd_mes_wait_retired() now sleeps up to 4 s while dereferencing raw
pointer q (looked up without a reference), widening the window for a
concurrent KFD queue destroy to free q before set_queue_as_reset() runs.

> +	if (r == -ETIMEDOUT)
> +		return r;
> +	if (r)
> +		dev_warn(adev->dev,
> +			 "MES did not remove reset queue doorbell=0x%x (%d), keeping its storage\n",
> +			 q->properties.doorbell_off, r);
>  	return 0;
>  }

[Severity: Medium]
Should reset_queue_mes() also propagate -EIO instead of returning 0?

remove_queue_mes_on_reset_option() returns -EIO without sending
REMOVE_QUEUE when reset_domain->sem is write-locked, MES is fenced, or
scheduling is halted/stopped.  Swallowing -EIO lets KGD callers of
amdgpu_gfx_reset_mes_compute() treat the reset as successful, resume MES
with the reset KFD queue still mapped, and restart compute schedulers
even while a GPU reset holds reset_domain->sem.

On the KFD path, leaving q->mes_reset_seen set while returning 0 also
causes reset_queues_mes() to misreport the unsent -EIO cleanup as
AMDGPU_MES_CMD_REJECTED.

[ ... ]

> @@ -471,6 +774,25 @@ static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
>  	if (WARN_ON(dqm->detect_hang_count > 0))
>  		return 0;
>  
> +	/* Gone from MES in the meantime, nothing left to reset */
> +	if (kfd_mes_queue_retired(dqm, q))
> +		return 0;
> +
> +	/*
> +	 * SUSPEND, DETECT_AND_RESET and RESUME would each time out behind
> +	 * a command that MES has not answered.
> +	 */
> +	if (kfd_mes_fenced(dqm)) {
> +		r = -EIO;
> +		goto fail;
> +	}

[Severity: Medium]
Should reset_queues_mes() and __remove_queue_mes() also check
!dqm->sched_running || dqm->sched_halt?

halt_cpsch() (for example under enforce_isolation or S0ix suspend) sets
dqm->sched_halt without clearing q->properties.is_active or flushing
kfd->ih_wq.  If remove_all_kfd_queues_mes() fails partway through
without fencing MES, a bad-opcode interrupt for a remaining active queue
enters kfd_dqm_suspend_bad_queue_mes() -> reset_queues_mes() while
halted.

reset_queues_mes() then issues MES suspend/reset/resume commands in
violation of the halt contract, and afterwards either calls
__remove_queue_mes() directly (bypassing the halt check) or, if q itself
was reset, misreports the -EIO from remove_queue_mes_on_reset_option()
as AMDGPU_MES_CMD_REJECTED.  Checking !dqm->sched_running ||
dqm->sched_halt in both functions and returning -EIO would keep them
consistent with add_queue_mes() and remove_queue_mes_on_reset_option().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204125251.10703-1-zappel@simple-co.de?part=3

  reply	other threads:[~2026-12-04 15:26 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 [this message]
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
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-250097@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