From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9E7A0606A322 for ; Fri, 4 Dec 2026 15:26:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=WI53+FR3 Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 365F1113043; Fri, 4 Dec 2026 15:26:03 +0000 (UTC) Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id 9E683113028 for ; Fri, 4 Dec 2026 15:25:56 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 1621D620EF; Fri, 4 Dec 2026 15:25:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5C0A1F000CE; Fri, 4 Dec 2026 15:25:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796397955; bh=O3Roj7X3wLRZ/zV4OaM+Ob3VZWGvMQrQz77oRCjp0mE=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=WI53+FR3Vr5Z4IyRBHppp6qX5VSUVxI4d9scNupKr1hbnhg+x0N/ij/EKtLk9ZKsY 4mbysJFRL8YAj5XZoy7RvkaGfKfmGTFymgQimct2gp0aQ8xKWFgo9nFWGJhQhJACAc KwN+bcsZzSzz47lQtN8+YFdh5J20aCMH2V3/IOP5MJVIglvwTviwubdS/vXNVlGg38 btPdpH7k2GSq//jTPp4AtXGL0jbbZbLthdbPiDrHbUsNv+ct0R6MrBd2wpSNCV9dPt FyVuiSt29+7wtR37RsWNh5FDm5HaJ4v5Nj1EjcyBCUv49k7wZ9E+x+ftBIcMgHOkSD Y7iAMB69C+cZA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 3/8] drm/amdkfd: track whether MES knows each queue To: "Armas Spann" , amd-gfx@lists.freedesktop.org In-Reply-To: <20261204125251.10703-4-zappel@simple-co.de> References: <20261204125251.10703-1-zappel@simple-co.de> <20261204125251.10703-4-zappel@simple-co.de> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 15:25:55 +0000 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "amd-gfx" X-Spam-Status: No, score=-11.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: 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 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/driv= ers/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 =3D true; > + dqm->mes_fence_gen =3D kfd_mes_gen(dqm); > + dqm->mes_fence_has_seq =3D kfd_mes_ticket_pending(dqm, q); > + if (dqm->mes_fence_has_seq) > + dqm->mes_fence_ticket =3D 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 pas= sed > + * 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 =3D=3D -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 =3D=3D -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_manage= r *dqm, struct queue *q, [ ... ] > if (r) { > dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=3D0x= %x\n", > q->properties.doorbell_off); > dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU r= eset\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 =3D=3D -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 q= ueue *q, > + bool flush_mes_queue) > { > - struct amdgpu_device *adev =3D (struct amdgpu_device *)dqm->dev->adev; > - int r; > + struct amdgpu_device *adev =3D dqm->dev->adev; > struct mes_remove_queue_input queue_input; > + int r; > =20 > - /* 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; > =20 > - 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; > =20 > + /* 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 =3D reset_queues_mes(dqm, q); > - if (r && amdgpu_gpu_recovery) { > - dev_err(adev->dev, "failed to remove queue from MES, doorbell=3D0x%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 =3D 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_manag= er *dqm, struct queue *q, > ffs(dqm->dev->xcc_mask) - 1); > if (r) > return r; > - /* Proceed remove_queue with reset=3Dtrue */ > - 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 =3D true; > + /* > + * Proceed remove_queue with reset=3Dtrue. 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 =3D 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 =3D false amdgpu_wb_free(status_offs) <-- double free in adev->wb.used ticket->valid =3D 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 =3D 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 =3D=3D -ETIMEDOUT) > + return r; > + if (r) > + dev_warn(adev->dev, > + "MES did not remove reset queue doorbell=3D0x%x (%d), keeping its st= orage\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_mana= ger *dqm, struct queue *q) > if (WARN_ON(dqm->detect_hang_count > 0)) > return 0; > =20 > + /* 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 =3D -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(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204125251.1070= 3-1-zappel@simple-co.de?part=3D3