All of 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 v1 3/9] drm/amdkfd: track whether MES knows each queue
Date: Tue, 24 Nov 2026 16:30:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-231571@kernel.org> (raw)
In-Reply-To: <20261124143433.49736-4-zappel@simple-co.de>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] drm/amdkfd: ignored remove-after-reset error and unfenced
  timeout in reset_queue_mes()
- [High] drm/amdkfd: duplicate REMOVE_QUEUE after late completion in
  reset_queues_mes()
- [High] drm/amdkfd: false success in remove_queue_mes_on_reset_option()
  when queue is not retired
- [High] drm/amdkfd: kfd_dqm_suspend_bad_queue_mes() leaves unremoved
  queue in MES
- [Medium] drm/amdkfd: missing kfd_mes_fence() call on command timeout
  in add_queue_mes()
- [Medium] drm/amdkfd: reset_queues_mes() called behind unanswered
  REMOVE_QUEUE before fencing
- [Medium] drm/amdkfd: missing gang_context_array_index and ticket in
  reset_queues_mes()
- [Medium] drm/amdkfd: unlocked MES state access in
  kfd_reset_queue_mes()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] drm/amdkfd: unbalanced queue unmap in update_queue() on reset
  queue [1]
--

commit 646aa63a006f03da9e5f44bcc16133cd8b988d3e
Author: Armas Spann <zappel@simple-co.de>

drm/amdkfd: track whether MES knows each queue

This commit tracks the per-queue MES mapping state, firmware generation,
and pending REMOVE_QUEUE ticket so KFD can tell whether MES still
references a queue and avoid sending further commands behind an unanswered
one.

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

[ ... ]

> @@ -270,10 +399,18 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
>  	queue_input.vm_cntx_cntl = qpd->vm_cntx_cntl;
>  	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
>  
> +	/* Cannot change under us, reset_domain->sem is held for reading */
> +	gen = kfd_mes_gen(dqm);
> +
>  	amdgpu_mes_lock(&adev->mes);
>  	r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
>  	amdgpu_mes_unlock(&adev->mes);
>  	up_read(&adev->reset_domain->sem);
> +
> +	/* A failed ADD_QUEUE may still be executed, or may have been */
> +	q->mes_fw_gen = gen;
> +	q->mes_state = r ? KFD_MES_QUEUE_UNKNOWN : KFD_MES_QUEUE_MAPPED;
> +
>  	if (r) {
>  		dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=0x%x\n",
>  			q->properties.doorbell_off);

[Severity: Medium]
Should add_queue_mes() call kfd_mes_fence(dqm, q) when add_hw_queue() fails
with -ETIMEDOUT?

kfd_hws_hang() only schedules an asynchronous reset work item, so without
fencing MES here, callers that iterate over multiple queues under dqm_lock
(such as kfd_resume_all_processes() or set_perfcount()) keep submitting
commands behind the unanswered ADD_QUEUE and suffer cascading timeouts
while holding reset_domain->sem.

> @@ -299,24 +436,38 @@ static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
>  	return r;
>  }
>  
> -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)
> +/*
> + * Send one REMOVE_QUEUE and record the outcome in q->mes_state.
> + *
> + * Returns -EIO when nothing was sent because MES is fenced off or a GPU
> + * reset is in progress, and -ETIMEDOUT when MES did not answer (this
> + * command, or an earlier one for the same queue).
> + */
> +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)
> -		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 (q->mes_ticket.valid &&
> +	    amdgpu_mes_ticket_poll(adev, &q->mes_ticket) ==
> +	    AMDGPU_MES_TICKET_PENDING)
> +		return -ETIMEDOUT;
>  
> -	if (!dqm->sched_running || dqm->sched_halt)
> -		return 0;
>  	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);
> +
>  	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
>  	queue_input.doorbell_offset = q->properties.doorbell_off;
>  	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
> @@ -324,23 +475,74 @@ static int remove_queue_mes_on_reset_option(struct device_queue_manager *dqm, st
>  	queue_input.remove_queue_after_reset = flush_mes_queue;
>  	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
>  	queue_input.gang_context_array_index = q->gang_ctx_array_index;
> +	queue_input.ticket = &q->mes_ticket;
>  
>  	amdgpu_mes_lock(&adev->mes);
>  	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
>  	amdgpu_mes_unlock(&adev->mes);
>  	up_read(&adev->reset_domain->sem);
>  
> +	q->mes_state = r ? KFD_MES_QUEUE_UNKNOWN : KFD_MES_QUEUE_UNMAPPED;
> +
> +	return r;
> +}
> +
> +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;
> +
> +	/* queue was already removed during reset */
> +	if (q->properties.is_reset)
> +		return 0;
> +
> +	if (!dqm->sched_running || dqm->sched_halt)
> +		return 0;

[Severity: High]
When kfd_mes_queue_retired() returns false, MES may still have the queue;
doesn't returning 0 on q->properties.is_reset, !dqm->sched_running, or
dqm->sched_halt tell callers that the queue was unmapped while leaving it
in MES?

Any queue that was genuinely removed during halt_cpsch(), stop_cpsch(), or
reset_queue_mes() already returns 0 via kfd_mes_queue_retired().  If
halt_cpsch() fails halfway through remove_all_kfd_queues_mes() and sets
dqm->sched_halt = true with remaining active queues still mapped in MES,
update_queue_locked() on one of those active queues gets 0 from
remove_queue_mes() and rewrites its MQD in place while MES still has it
mapped (or evict_process_queues_cpsch() unpins the MQD BO while MES can
still access it).

[Severity: Medium]
Can remove_queue_mes_on_reset_option() also race on q->mes_ticket,
q->mes_state, and dqm->mes_fenced when called from kfd_reset_queue_mes()
without dqm_lock?

When a KCQ or AMDGPU userq reset runs amdgpu_gfx_reset_mes_compute() (from
gfx_v11_0_reset_kcq(), gfx_v12_0_reset_kcq(), or amdgpu_userq_reset_work()),
dqm_lock is not held:

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

Concurrent updates to q->mes_ticket and q->mes_state can corrupt the state
tracking or double-free the writeback slot in adev->wb.

> +
> +	r = __remove_queue_mes(dqm, q, flush_mes_queue);
> +	if (!r)
> +		return 0;
> +
> +	/* Not answered yet. The command is still queued and may complete. */
> +	if (r == -ETIMEDOUT &&
> +	    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;
> +	}
> +
>  	/* If is_for_reset set, it is a mes internal cleanup */
> -	if (!r || is_for_reset)
> +	if (is_for_reset || r == -EIO)
>  		return r;

[Severity: High]
Does the is_for_reset path still ignore failures of the remove-after-reset
command and skip kfd_mes_fence() on a timeout?

Returning early on is_for_reset skips kfd_mes_fence(dqm, q) when the
remove-after-reset command times out, and the only caller passing
is_for_reset = true ignores the return value:

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c:reset_queue_mes() {
    ...
	/* Proceed remove_queue with reset=true */
	remove_queue_mes_on_reset_option(dqm, q, &pdd->qpd, true, true);
	set_queue_as_reset(dqm, q, &pdd->qpd, true);
	return 0;
}

When the remove-after-reset command fails or times out, reset_queue_mes()
still marks the queue as is_reset = true and returns 0 to
amdgpu_gfx_reset_mes_compute(), leaving q->mes_state as
KFD_MES_QUEUE_UNKNOWN, skipping GPU reset recovery, and allowing further
MES commands to be queued behind an unanswered command.

>  
>  	/* 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);
> -		dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
> -		kfd_hws_hang(dqm);
> +
> +	/* The recovery dealt with other queues and left this one in MES */
> +	if (!r && !kfd_mes_queue_retired(dqm, q))
> +		r = __remove_queue_mes(dqm, q, false);
> +
> +	if (r) {
> +		if (r == -ETIMEDOUT ||
> +		    (q->mes_ticket.valid &&
> +		     amdgpu_mes_ticket_poll(adev, &q->mes_ticket) ==
> +		     AMDGPU_MES_TICKET_PENDING))
> +			kfd_mes_fence(dqm, q);
> +
> +		if (amdgpu_gpu_recovery) {
> +			dev_err(adev->dev, "failed to remove queue from MES, doorbell=0x%x\n",
> +				q->properties.doorbell_off);
> +			dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
> +			kfd_hws_hang(dqm);
> +		}
>  	}
>  
>  	return r;

[Severity: Medium]
When __remove_queue_mes() returns -ETIMEDOUT and kfd_mes_wait_retired()
returns false with q->mes_ticket still AMDGPU_MES_TICKET_PENDING (or
because amdgpu_in_reset() is true), shouldn't this fence MES and skip
reset_queues_mes()?

Calling reset_queues_mes() behind an unanswered REMOVE_QUEUE submits up to
three more MES commands (suspend, detect_and_reset, and resume) to the same
stalled ring before kfd_mes_fence() is reached, adding up to 6.3 seconds of
udelay polling under dqm_lock and blocking amdgpu_amdkfd_pre_reset().

> @@ -484,8 +686,24 @@ static int reset_queues_mes(struct device_queue_manager *dqm, struct queue *q)
>  	queue_input.xcc_id = ffs(dqm->dev->xcc_mask) - 1;
>  	/* 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);

[Severity: Medium]
Should queue_input in reset_queues_mes() also populate
gang_context_array_index and ticket like __remove_queue_mes() does?

When num_hung == 0, amdgpu_gfx_reset_mes_compute() passes queue_input to
adev->mes.funcs->remove_hw_queue().  Without setting
queue_input.gang_context_array_index = q->gang_ctx_array_index and
queue_input.ticket = &q->mes_ticket, MES v11 with use_rs64mem receives a
gang context index of 0, and if that removal times out the writeback slot
is freed early while the command is still in flight and q->mes_ticket is
left untracked.

> -	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;
> +	}
> +
> +	/*
> +	 * With no hung queue reported, amdgpu_gfx_reset_mes_compute() sent
> +	 * the REMOVE_QUEUE for @q itself and MES confirmed it. MES handles
> +	 * its ring in order, so an earlier unanswered REMOVE_QUEUE for @q
> +	 * has been dealt with as well and its status slot can go.
> +	 */
> +	if (!num_hung) {
> +		amdgpu_mes_ticket_release(adev, &q->mes_ticket);
> +		q->mes_state = KFD_MES_QUEUE_UNMAPPED;
> +	}

[Severity: High]
What happens if a timed-out REMOVE_QUEUE in the MES ring completes after
kfd_mes_wait_retired() gives up, right before the SUSPEND command sent by
amdgpu_gfx_reset_mes_compute()?

Because MES processes its ring in order, it executes the pending
REMOVE_QUEUE (removing q and marking q->mes_ticket done) and then completes
amdgpu_mes_suspend() with num_hung == 0.  amdgpu_gfx_reset_mes_compute()
then unconditionally calls remove_hw_queue() for queue_input a second time:

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c:amdgpu_gfx_reset_mes_compute() {
    ...
	if (!num_hung && queue_input && !ring) {
		if (!down_read_trylock(&adev->reset_domain->sem)) {
			r = -EIO;
			goto out;
		}
		amdgpu_mes_lock(&adev->mes);
		r = adev->mes.funcs->remove_hw_queue(&adev->mes, queue_input);
		amdgpu_mes_unlock(&adev->mes);
		up_read(&adev->reset_domain->sem);
	}
    ...
}

MES rejects that duplicate removal with -EREMOTEIO, reset_queues_mes()
takes the error path without checking kfd_mes_queue_retired(), and
remove_queue_mes_on_reset_option() triggers a full GPU reset via
kfd_hws_hang() for a queue removal that actually succeeded.  Similarly, in
__remove_queue_mes(), if an earlier ticket transitioned to
AMDGPU_MES_TICKET_DONE after the caller's kfd_mes_queue_retired() check,
__remove_queue_mes() releases the completed ticket and sends a duplicate
REMOVE_QUEUE instead of retiring the queue.

[Severity: High]
Should the post-recovery removal when num_hung > 0 and !kfd_mes_queue_retired()
 happen inside reset_queues_mes() (or in kfd_dqm_suspend_bad_queue_mes())
rather than only in remove_queue_mes_on_reset_option()?

When kfd_dqm_suspend_bad_queue_mes() calls reset_queues_mes(dqm, q) and MES
reports other queues as hung (num_hung > 0), amdgpu_gfx_reset_mes_compute()
resumes the non-hung queues without removing q:

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;
			}
		}
    ...
}

kfd_dqm_suspend_bad_queue_mes() then clears q->properties.is_active = false
while q remains mapped in MES (q->mes_state == KFD_MES_QUEUE_MAPPED), and
remove_all_kfd_queues_mes() later skips it because it gates
remove_queue_mes() on q->properties.is_active instead of
!kfd_mes_queue_retired(dqm, q).

> @@ -1147,6 +1365,15 @@ static int update_queue_locked(struct device_queue_manager *dqm, struct queue *q
>  	/* Save the activity state for queue removal. */
>  	prev_active = q->properties.is_active;
>  
> +	/*
> +	 * The queue is inactive but MES has not confirmed its removal, so
> +	 * MES may still read and write the MQD. Leave it alone.
> +	 */
> +	if (!prev_active && !kfd_mes_queue_retired(dqm, q)) {
> +		retval = -EBUSY;
> +		return retval;
> +	}
> +
>  	/* Make sure the queue is unmapped before updating the MQD */
>  	if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
>  		if (!dqm->dev->kfd->shared_resources.enable_mes)

[Severity: High]
This problem wasn't introduced by this patch, but in update_queue_locked()
[1], pdd->has_reset_queue is checked only after unmapping dynamic
queues or calling remove_queue_mes():

drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c:update_queue_locked() {
    ...
	if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
		if (!dqm->dev->kfd->shared_resources.enable_mes)
			retval = unmap_queues_cpsch(dqm,
						    KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD, false);
		else if (prev_active)
			retval = remove_queue_mes(dqm, q, &pdd->qpd);

		/* queue is reset so inaccessable  */
		if (pdd->has_reset_queue) {
			return -EACCES;
		}
    ...
}

When pdd->has_reset_queue is set, returning -EACCES before the remapping
step at the bottom of update_queue_locked() leaves all dynamic queues on
the node unmapped on non-MES hardware scheduling, or on MES removes an
unaffected active queue of the same process while leaving
q->properties.is_active and dqm->active_queue_count set.

[1] https://sashiko.dev/#/bug/linux-39086475-ed69-4a54-b2b8-be02a185935b

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

  reply	other threads:[~2026-11-24 16:31 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 [this message]
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
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-231571@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.