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 1/8] drm/amdgpu/mes: distinguish rejected from timed out commands
Date: Fri, 04 Dec 2026 14:30:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-249977@kernel.org> (raw)
In-Reply-To: <20261204125251.10703-2-zappel@simple-co.de>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] drm/amdgpu/mes: failed reset breaks amdgpu_mes_ticket_poll()
  reset check
- [Medium] drm/amdgpu/mes: mes_v*_submit_pkt_ticket() misclassifies
  reset-forced fence
- [Medium] drm/amdgpu: amdgpu_mes_ticket_poll() misreports state on
  failed reset/resume
- [Low] drm/amdgpu/mes: missing writer barriers pairing with ticket
  smp_rmb()
--

commit 1712012870309920f5ca4962dcbb7f16034640d2
Author: Armas Spann <zappel@simple-co.de>

drm/amdgpu/mes: distinguish rejected from timed out commands

This commit returns AMDGPU_MES_CMD_REJECTED (-EREMOTEIO) instead of
-ETIMEDOUT when MES answers a command with a failure status, and adds a
per-XCC firmware generation counter and command tickets so callers can
track late completions of timed out REMOVE_QUEUE commands.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c
> index 27d6371d8d791..ef226183d7eb5 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c

[ ... ]

> +	/*
> +	 * The fence was passed without a success status. That is a failure
> +	 * reported by MES only if MES moved the fence. A GPU reset moves it
> +	 * as well (amdgpu_device_pre_asic_reset()), and so does the first
> +	 * command of the next firmware instance, both before fw_gen is
> +	 * incremented. Neither says anything about a command the old
> +	 * instance never answered. The reset flag is set before the fence
> +	 * is forced and cleared after fw_gen went up, so it is read after
> +	 * the fence and before the generation.
> +	 */
> +	if (amdgpu_in_reset(adev))
> +		return AMDGPU_MES_TICKET_STALE;
> +	/* Read the generation after the reset flag, see above */
> +	smp_rmb();
> +	if (ticket->fw_gen != amdgpu_mes_fw_gen(&adev->mes, ticket->xcc_id))
> +		return AMDGPU_MES_TICKET_STALE;
> +
> +	return AMDGPU_MES_TICKET_FAILED;
> +}

[Severity: Medium]
Does the invariant that in_gpu_reset is only cleared after fw_gen goes up
hold when a GPU reset fails or aborts?

amdgpu_device_pre_asic_reset() forces *mes_ring->fence_drv.cpu_addr to
sync_seq before the reset runs.  If amdgpu_device_asic_reset() then aborts
early (for example, amdgpu_ras_is_rma() returning -ENODEV before MES is
halted), amdgpu_device_gpu_recover() jumps to reset_unlock and clears
in_gpu_reset without incrementing fw_gen.

Once in_gpu_reset is 0, amdgpu_mes_ticket_poll() permanently returns
AMDGPU_MES_TICKET_FAILED and amdgpu_mes_ticket_answered() returns true for
a command MES never processed.  If KFD later resumes scheduling without
reinitializing MES (such as on the unwind_userq path of
amdgpu_device_suspend()), callers can call amdgpu_mes_ticket_release() and
return the writeback status slot to adev->wb.used while the unanswered
command is still in the live MES ring, allowing a late DMA write to
corrupt the next owner of that slot.

> +bool amdgpu_mes_ticket_answered(struct amdgpu_device *adev,
> +				const struct amdgpu_mes_cmd_ticket *ticket)
> +{
> +	bool passed;
> +
> +	if (!ticket->valid ||
> +	    ticket->fw_gen != amdgpu_mes_fw_gen(&adev->mes, ticket->xcc_id))
> +		return true;
> +
> +	passed = amdgpu_mes_fence_passed(&adev->mes.ring[ticket->ring_idx],
> +					 ticket->seq);
> +
> +	/*
> +	 * A GPU reset forces the fence, see amdgpu_mes_ticket_poll(). The
> +	 * flag is set before that happens, so read it after the fence.
> +	 */
> +	smp_rmb();
> +	if (amdgpu_in_reset(adev))
> +		return false;
> +
> +	return passed;
> +}

[Severity: Low]
Are the smp_rmb() barriers in amdgpu_mes_ticket_poll() and
amdgpu_mes_ticket_answered() paired with explicit write barriers on the
writer side?

atomic_inc(&adev->mes.fw_gen[...]) is relaxed, and
amdgpu_device_unlock_reset_domain() clears in_gpu_reset with a relaxed
atomic_set() before up_write(), so nothing explicitly orders the fw_gen
increment before in_gpu_reset becomes 0.  Likewise,
amdgpu_device_lock_reset_domain() sets in_gpu_reset with a relaxed
atomic_set() before acquire-only down_write(), and amdgpu_fence_write()
writes *drv->cpu_addr with an unmarked plain store rather than
WRITE_ONCE().

On weakly ordered SMP architectures, store reordering can let
amdgpu_mes_ticket_poll() observe in_gpu_reset == 0 with the old fw_gen and
return AMDGPU_MES_TICKET_FAILED instead of AMDGPU_MES_TICKET_STALE, or let
amdgpu_mes_ticket_answered() observe the forced fence with
in_gpu_reset == 0 and return true mid-reset.  The unmarked store in
amdgpu_fence_write() is also a KCSAN data race against READ_ONCE() in
amdgpu_mes_fence_passed().  Adding smp_wmb() around the in_gpu_reset
updates (or atomic_set_release() on unlock) and WRITE_ONCE() in
amdgpu_fence_write() would pair the barriers properly.

> diff --git a/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c
> index 7d8a56859fce5..fc8822458adda 100644
> --- a/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c
> +++ b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c

[ ... ]

> @@ -264,9 +271,20 @@ static int mes_v11_0_submit_pkt_and_poll_completion(struct amdgpu_mes *mes,
>  			x_pkt->header.opcode);
>  
>  	r = amdgpu_fence_wait_polling(ring, seq, timeout);
> +	/* The status is written before the fence, read it after the fence */
> +	dma_rmb();
>  	if (r < 1 || !*status_ptr) {
> +		/* The fence was reached, so MES processed the command */
> +		bool answered = r >= 1;

[Severity: Medium]
Can a reset-forced ring fence cause mes_v11_0_submit_pkt_ticket() to
misclassify an unanswered command as rejected by MES?

CPU0                              CPU1
kfd_dbg_set_mes_debug_mode()      amdgpu_device_pre_asic_reset()
  mes_v11_0_submit_pkt_ticket()
    amdgpu_fence_wait_polling()
                                    amdgpu_fence_driver_force_completion()
    /* r >= 1, *status_ptr == 0 */
    answered = true;              <-- returns AMDGPU_MES_CMD_REJECTED

amdgpu_device_pre_asic_reset() forces every ready MES ring fence to
sync_seq without holding adev->mes.mutex_hidden and before
amdgpu_device_ip_suspend() stops MES.  Several callers submit MES packets
without adev->reset_domain->sem, including kfd_dbg_set_mes_debug_mode(),
mes_notify_unmap_work_handler(), and amdgpu_userq_preempt_helper().

When the forced fence wakes a concurrent poller with status 0, this path
logs a bogus rejection with status 0, returns AMDGPU_MES_CMD_REJECTED
(-EREMOTEIO) instead of -ETIMEDOUT (propagating -EREMOTEIO to userspace
on KFD debug ioctls), and frees the writeback slot before MES is stopped.
The same issue is present in mes_v12_0_submit_pkt_ticket(),
mes_v12_1_submit_pkt_ticket(), and mes_v13_0_submit_pkt_ticket().

> diff --git a/drivers/gpu/drm/amd/amdgpu/mes_v12_1.c b/drivers/gpu/drm/amd/amdgpu/mes_v12_1.c
> index 84a3693db6ad4..d7cdce5be4af3 100644
> --- a/drivers/gpu/drm/amd/amdgpu/mes_v12_1.c
> +++ b/drivers/gpu/drm/amd/amdgpu/mes_v12_1.c

[ ... ]

> @@ -1909,6 +1966,12 @@ static int mes_v12_1_xcc_hw_init(struct amdgpu_ip_block *ip_block, int xcc_id)
>  	if (r)
>  		goto failure;
>  
> +	/*
> +	 * A new scheduler instance is up on this XCC, it knows none of the
> +	 * old queues. The other XCCs keep theirs.
> +	 */
> +	atomic_inc(&adev->mes.fw_gen[xcc_id]);
> +
>  	mes_v12_1_set_hw_resources_1(&adev->mes, AMDGPU_MES_SCHED_PIPE, xcc_id);
>  	mes_v12_1_init_aggregated_doorbell(&adev->mes, xcc_id);

[Severity: Medium]
Does advancing the MES ring fence in mes_v12_1_set_hw_resources() before
incrementing adev->mes.fw_gen[xcc_id] break ticket polling outside GPU
reset?

During S3/S4 resume or a suspend error unwind, amdgpu_in_reset(adev) is 0.
mes_v12_1_set_hw_resources() submits a command that moves
*ring->fence_drv.cpu_addr past any prior ticket's seq before fw_gen is
incremented, and if that command is rejected, goto failure skips the
atomic_inc() altogether.

A ticket created before suspend then sees a passed fence with status 0,
in_gpu_reset == 0, and an unchanged fw_gen, so amdgpu_mes_ticket_poll()
returns AMDGPU_MES_TICKET_FAILED instead of AMDGPU_MES_TICKET_STALE and
amdgpu_mes_ticket_answered() returns true.  On a suspend unwind into
amdgpu_amdkfd_resume(), that lets amdgpu_mes_ticket_release() free the
writeback status slot prematurely.  The same ordering exists in
mes_v11_0_hw_init(), mes_v12_0_hw_init(), and mes_v13_0_hw_init().

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

  reply	other threads:[~2026-12-04 14:30 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 [this message]
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
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-249977@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