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 v2 3/9] drm/amdkfd: track whether MES knows each queue
Date: Thu, 26 Nov 2026 08:46:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-235053@kernel.org> (raw)
In-Reply-To: <20261125141933.154222-4-zappel@simple-co.de>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] drm/amdkfd: timeout misclassified in
  remove_queue_mes_on_reset_option()
- [High] drm/amdkfd: duplicate REMOVE_QUEUE after reset in
  reset_queues_mes()
- [High] drm/amdkfd: unhandled reset_queues_mes() error on bad-queue
  interrupt
- [High] drm/amdkfd: clobbered fence and lost timeout in
  reset_queues_mes() recovery
- [Medium] drm/amdkfd: unlocked MES state access in
  kfd_reset_queue_mes()
- [Medium] drm/amdkfd: unhalt and restore skip queues left in
  KFD_MES_QUEUE_MAPPED
- [Low] drm/amdkfd: enum kfd_mes_queue_state detaches struct queue
  kernel-doc
--

commit 137dde38b7013d2a238e16d0644e2b934e97ed33
Author: Armas Spann <zappel@simple-co.de>

drm/amdkfd: track whether MES knows each queue

This commit tracks per-queue MES mapping state, firmware generation, and
pending removal tickets in KFD so queue add, remove, and reset paths can
check whether MES still references a queue and fence off MES after an
unanswered command.

> 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 bd3dc4e9c9c77..646bc0ce05be8 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c
[ ... ]
> @@ -324,19 +491,91 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
[ ... ]
> +static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, struct queue *q,
> +					    struct qcm_process_device *qpd,
> +					    bool is_for_reset,
> +					    bool flush_mes_queue)
> +{
> +	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
> +	int r;
> +
> +	/* MES does not know the queue, or not any more: nothing to send */
> +	if (kfd_mes_queue_retired(dqm, q))
> +		return 0;
> +
> +	/*
> +	 * Nothing can be sent while the scheduler is stopped or halted, and
> +	 * MES still has the queue. Say so rather than report success: the
> +	 * caller then keeps the storage of the queue and leaves its MQD
> +	 * alone, and the removal is sent once the scheduler runs again.
> +	 */
> +	if (!dqm->sched_running || dqm->sched_halt)
> +		return -EIO;

[Severity: Medium]
Does an evicted queue whose removal fails with -EIO ever get removed or
re-added when the scheduler runs again?

When remove_queue_mes_on_reset_option() or __remove_queue_mes() returns
-EIO during evict_process_queues_cpsch() (for example while halted, fenced,
or when down_read_trylock(&adev->reset_domain->sem) fails during a soft
reset), q->properties.is_active is cleared while q->mes_state remains
KFD_MES_QUEUE_MAPPED.  On unhalt, add_all_kfd_queues_mes() skips inactive
queues instead of removing ones where !kfd_mes_queue_retired(dqm, q),
leaving the evicted queue mapped in MES.  When
restore_process_queues_cpsch() later updates qpd->page_table_base and calls
add_queue_mes(), add_queue_mes() sees q->mes_state == KFD_MES_QUEUE_MAPPED
and returns 0 without re-adding the queue with the new page table base.

> +
> +	r = __remove_queue_mes(dqm, q, flush_mes_queue);
> +	if (!r)
> +		return 0;
> +
> +	if (r == -ETIMEDOUT) {
> +		/* Not answered yet. The command is still queued and may complete. */
> +		if (kfd_mes_wait_retired(dqm, q, KFD_MES_REMOVE_GRACE_MS)) {
> +			dev_warn(adev->dev,
> +				 "MES confirmed REMOVE_QUEUE late, doorbell=0x%x\n",
> +				 q->properties.doorbell_off);
> +			return 0;
> +		}
> +
> +		/*
> +		 * Still no answer. MES is not working its ring, and the
> +		 * recovery below would only queue three more commands behind
> +		 * the unanswered one, each to time out under the dqm lock.
> +		 * Stop talking to MES and leave it to the GPU reset.
> +		 */
> +		if (kfd_mes_ticket_pending(dqm, q))
> +			goto hang;
> +
> +		/* Answered after all, with a failure: a hung queue */
> +		r = AMDGPU_MES_CMD_REJECTED;
> +	}

[Severity: High]
Can this overwrite -ETIMEDOUT with AMDGPU_MES_CMD_REJECTED when the MES
ring is full or when the ticket completes between checks?

__remove_queue_mes() calls amdgpu_mes_ticket_release(adev, &q->mes_ticket)
before adev->mes.funcs->remove_hw_queue().  If the MES ring is full,
mes_v11_0_submit_pkt_ticket(), mes_v12_0_submit_pkt_ticket(), and
mes_v12_1_submit_pkt_ticket() time out waiting for ring space and jump to
error_undo without setting ticket->valid = true:

drivers/gpu/drm/amd/amdgpu/mes_v12_1.c:mes_v12_1_submit_pkt_ticket() {
    ...
	r = amdgpu_fence_wait_polling(ring,
				      seq - ring->fence_drv.num_fences_mask,
				      timeout);
	if (r < 1) {
		r = -ETIMEDOUT;
		goto error_undo;
	}
    ...
}

Because q->mes_ticket.valid is false, kfd_mes_wait_retired() and
kfd_mes_ticket_pending() both return false, so r is overwritten with
AMDGPU_MES_CMD_REJECTED.  When is_for_reset is true, reset_queue_mes() sees
AMDGPU_MES_CMD_REJECTED instead of -ETIMEDOUT and returns 0 without fencing
MES or escalating to a GPU reset; when is_for_reset is false, it enters
reset_queues_mes() and queues three more commands into the full ring under
dqm_lock.

The same fallthrough happens if q->mes_ticket transitions to
AMDGPU_MES_TICKET_DONE in the window between kfd_mes_queue_retired() and
kfd_mes_ticket_pending(), treating a completed removal as
AMDGPU_MES_CMD_REJECTED and releasing the completed ticket in
reset_queues_mes() or __remove_queue_mes() without marking the queue
retired.

> +
> +	/*
> +	 * -EIO: nothing was sent, a GPU reset is under way or MES is fenced
> +	 * off. is_for_reset: the cleanup after a per-queue reset, the caller
> +	 * decides what its failure means.
> +	 */
> +	if (is_for_reset || r == -EIO)
> +		return r;
> +
> +	/* remove_hw_queue failure indicates a queue hang. reset the queue */
> +	r = reset_queues_mes(dqm, q);
> +	if (!r)
> +		return 0;
> +
> +	/* MES let go of the queue in the meantime, whatever else failed */
> +	if (kfd_mes_queue_retired(dqm, q))
> +		return 0;
> +
> +hang:
> +	if (r == -ETIMEDOUT || kfd_mes_ticket_pending(dqm, q))
> +		kfd_mes_fence(dqm, q);

[Severity: High]
What happens here when reset_queues_mes(dqm, q) fails with -ETIMEDOUT while
resetting a different hung queue q2?

During amdgpu_gfx_reset_mes_compute(), the loop over num_hung calls
reset_queue_mes() -> remove_queue_mes_on_reset_option(dqm, q2, ..., true,
true) for each hung queue.  If q2's remove-after-reset times out, that
inner call runs kfd_mes_fence(dqm, q2) and returns -ETIMEDOUT without
calling kfd_hws_hang(dqm).

If @q was already reset and retired in an earlier iteration of the
num_hung loop before q2 timed out, kfd_mes_queue_retired(dqm, q) is true in
the outer remove_queue_mes_on_reset_option() and it returns 0 before
reaching hang:, silently dropping q2's timeout and leaving dqm->mes_fenced
set with no GPU reset scheduled.  If @q was not retired, the outer call
reaches hang: with r == -ETIMEDOUT and calls kfd_mes_fence(dqm, q); because
@q's ticket was released at the start of reset_queues_mes(),
kfd_mes_fence(dqm, q) sets dqm->mes_fence_has_seq = false and wipes out
q2's recorded fence ticket.

[ ... ]
> @@ -435,9 +675,22 @@ 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);
> +	/*
> +	 * 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);
> +	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]
Can reset_queue_mes() access q->mes_state, q->mes_ticket, and
dqm->mes_fenced without holding dqm_lock(dqm)?

When amdgpu_gfx_reset_mes_compute() is called from gfx_v11_0_reset_kcq(),
gfx_v12_0_reset_kcq(), or amdgpu_userq_reset_work() and MES detects a hung
KFD queue, it calls amdgpu_amdkfd_reset_mes_queue() ->
kfd_reset_queue_mes() -> reset_queue_mes() holding only
adev->gfx.mec.reset_mutex:

CPU0                                    CPU1
gfx_v11_0_reset_kcq()                   destroy_queue_cpsch()
  amdgpu_gfx_reset_mes_compute()          dqm_lock(dqm)
    ...                                   remove_queue_mes(dqm, q, qpd)
      kfd_reset_queue_mes()                 __remove_queue_mes()
        reset_queue_mes()                     amdgpu_mes_ticket_release()
          remove_queue_mes_on_reset_option()
            __remove_queue_mes()
              amdgpu_mes_ticket_release()  <-- data race on q->mes_ticket

Concurrent KFD queue operations under dqm_lock(dqm) can race on
q->mes_ticket (corrupting or double-freeing the writeback slot in
adev->wb), q->mes_state, and dqm->mes_fenced.

> @@ -476,16 +729,50 @@ static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
[ ... ]
>  	/* pass the known bad queue info to the reset function */
>  	r = amdgpu_gfx_reset_mes_compute(adev, dqm->dev->xcp, NULL, NULL, NULL, &num_hung, &queue_input);
> -	if (r)
> +	if (r) {
> +		/* The REMOVE_QUEUE for @q may have been sent and lost */
> +		if (q->mes_state == KFD_MES_QUEUE_MAPPED)
> +			q->mes_state = KFD_MES_QUEUE_UNKNOWN;
>  		goto fail;
> +	}

[Severity: High]
When kfd_dqm_suspend_bad_queue_mes() calls reset_queues_mes(), how does a
timeout in reset_queues_mes() fence MES or escalate to a GPU reset?

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c:kfd_dqm_suspend_bad_queue_mes() {
    ...
			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;
			}
    ...
}

kfd_dqm_suspend_bad_queue_mes() ignores the return value of
reset_queues_mes().  If a per-queue remove-after-reset inside
amdgpu_gfx_reset_mes_compute() times out,
remove_queue_mes_on_reset_option(..., is_for_reset = true) sets
dqm->mes_fenced = true and returns -ETIMEDOUT without calling
kfd_hws_hang(dqm) (and amdgpu_gfx_reset_mes_compute() still sends
amdgpu_mes_resume() at out: before returning).  Because
kfd_dqm_suspend_bad_queue_mes() drops the error, kfd_hws_hang(dqm) is never
called, and every subsequent add_queue_mes() or remove_queue_mes() sees
kfd_mes_fenced(dqm) and returns -EIO without triggering a GPU reset,
leaving the node permanently wedged.  Timeouts in the other MES calls inside
reset_queues_mes() (such as remove_hw_queue() when !num_hung or
__remove_queue_mes() when num_hung > 0) also return without waiting the
grace period, fencing MES, or calling kfd_hws_hang(dqm) on this path.

[ ... ]
> @@ -508,6 +795,17 @@ static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
>  	}
>  	amdgpu_vm_put_task_info(ti);
>  
> +	/*
> +	 * MES reported other queues as hung, reset those and resumed the
> +	 * rest, @q among them: amdgpu_gfx_reset_mes_compute() removes the
> +	 * queue it was given only when nothing was reported hung. Both
> +	 * callers want @q out of MES, so remove it now that the scheduler
> +	 * answers again. If @q itself was among the hung queues, the
> +	 * per-queue reset has removed it already and it is retired.
> +	 */
> +	if (num_hung && !kfd_mes_queue_retired(dqm, q))
> +		r = __remove_queue_mes(dqm, q, false);

[Severity: High]
What if @q was among the hung queues and MES rejected its
remove-after-reset in reset_queue_mes()?

In that case reset_queue_mes() warns, sets q->properties.is_reset = true,
leaves q->mes_state as KFD_MES_QUEUE_UNKNOWN, and returns 0 so the
per-queue reset succeeds without escalating to a GPU reset.  Because
kfd_mes_queue_retired(dqm, q) is still false, reset_queues_mes() then sends
a second __remove_queue_mes(dqm, q, false) with remove_queue_after_reset =
false; when that also fails, reset_queues_mes() returns the error to
remove_queue_mes_on_reset_option(), which calls kfd_hws_hang(dqm) and
triggers a full GPU reset.  Likewise, because the q->properties.is_reset
check was removed from remove_queue_mes_on_reset_option(),
remove_all_kfd_queues_mes() on halt or stop will also call
remove_queue_mes() on any reset queue whose remove-after-reset was
rejected.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> index a725fde9f11bd..8a506501a5349 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h
> @@ -616,6 +616,13 @@ struct mqd_update_info {
>   *
>   */
>  
> +/* What MES knows about a queue, see kfd_mes_queue_retired() */
> +enum kfd_mes_queue_state {
> +	KFD_MES_QUEUE_UNMAPPED = 0,	/* never added, or removal confirmed */
> +	KFD_MES_QUEUE_MAPPED,		/* ADD_QUEUE confirmed */
> +	KFD_MES_QUEUE_UNKNOWN,		/* ADD or REMOVE sent, not confirmed */
> +};
> +
>  struct queue {

[Severity: Low]
Placing enum kfd_mes_queue_state between the struct queue kernel-doc
comment and struct queue detaches the comment from the struct and triggers
a scripts/kernel-doc warning.

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

  reply	other threads:[~2026-11-26  8:47 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 14:19 [PATCH v2 0/9] drm/amdgpu/amdkfd: fix MES queue lifecycle on multi-GPU Armas Spann
2026-11-25 14:19 ` [PATCH v2 1/9] drm/amdgpu/mes: distinguish rejected from timed out commands Armas Spann
2026-11-26  8:36   ` sashiko-bot
2026-11-25 14:19 ` [PATCH v2 2/9] drm/amdkfd: factor out freeing of per-queue MES resources Armas Spann
2026-11-25 14:19 ` [PATCH v2 3/9] drm/amdkfd: track whether MES knows each queue Armas Spann
2026-11-26  8:46   ` sashiko-bot [this message]
2026-11-25 14:19 ` [PATCH v2 4/9] drm/amdkfd: add a quarantine for queues MES has not retired Armas Spann
2026-11-26  8:49   ` sashiko-bot
2026-11-25 14:19 ` [PATCH v2 5/9] drm/amdkfd: keep queue storage until MES confirmed retirement Armas Spann
2026-11-26  8:39   ` sashiko-bot
2026-11-25 14:19 ` [PATCH v2 6/9] drm/amdkfd: keep eviction and restore consistent when MES fails Armas Spann
2026-11-26  8:36   ` sashiko-bot
2026-11-25 14:19 ` [PATCH v2 7/9] drm/amdkfd: apply queue property updates only after unmap Armas Spann
2026-11-25 14:19 ` [PATCH v2 8/9] drm/amdkfd: reconcile queue state with MES after a restart Armas Spann
2026-11-25 14:19 ` [PATCH v2 9/9] drm/amdkfd: force CWSR on with more than one MES 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-235053@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