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 242EF606A339 for ; Fri, 4 Dec 2026 17:08:07 +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=MWqyVjsQ Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B51F610E8AC; Fri, 4 Dec 2026 17:08:01 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id EAA1710EC29 for ; Fri, 4 Dec 2026 17:07:55 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id B99314122D; Fri, 4 Dec 2026 17:07:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 823A81F00A12; Fri, 4 Dec 2026 17:07:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796404075; bh=XFN05YNZ+jrF1qzqFc92JylCT2RuAMqbbGYuJi5wL7g=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=MWqyVjsQZsxsCPa6PMDuonbDOQlzyiSCqyqi30htziI10WjaVIj8Nhwbp9p1H9GSI kBTGQg03WEyDs9ealk0vioIhDNGZS2XE+D+EhuRP82WsewwqXw8ZKn80DzJI+eT7U9 xOzb1awWFNPE4RzjgKFyHL/5yElaQzUVlbYx04uXVhjd6ZaJyhVZoNdLLQbMA2u0Bp cpIHMWfKJ1IYqLMiWHBo/UzFc1XM8zF2aUzKEKtvv6nw4cUZAk0hgcZ0vS/ZSNTIo1 seMHbKRqIiVvXTT162uWR75UgyJW6RFaUTqXF+YDdYHQtAwmzJNaJUj6ug0gca0f8t s6sfFEWZ+rEsQ== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH 6/8] drm/amdkfd: keep eviction and restore consistent when MES fails To: "Armas Spann" , amd-gfx@lists.freedesktop.org In-Reply-To: <20261204125251.10703-7-zappel@simple-co.de> References: <20261204125251.10703-1-zappel@simple-co.de> <20261204125251.10703-7-zappel@simple-co.de> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Fri, 04 Dec 2026 17:07:54 +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=-16.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,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 10 potential issues to consider: - [High] drm/amdkfd: interleaved stale queue removal and missing TLB flush on restore - [High] drm/amdkfd: first eviction skips inactive unretired MES queues - [High] drm/amdkfd: eviction ignores quarantined queues on dqm->zombie_queues - [High] drm/amdkfd: nested eviction increments qpd->evicted before returning -EIO - [Medium] drm/amdkfd: premature kfd_last_flushed_seq update while MES holds queues - [Medium] drm/amdkfd: kfd_flush_tlb() in restore rollback skips unchanged tlb_seq - [Medium] drm/amdkfd: restore_process_queues_cpsch() re-adds reset hung MES queue - [Medium] drm/amdkfd: kfd_flush_tlb() bypasses MES fence on evict/restore fail - [Medium] drm/amdkfd: add_queue_mes() skips retired check while halted - [Low] drm/amdkfd: missing Fixes trailers for MES eviction and -EIO bugs -- commit 2506f1259cff9bbb9ada0657a6cfb1b38e190baa Author: Armas Spann > drm/amdkfd: keep eviction and restore consistent when MES fails > > evict_process_queues_cpsch() returns at the first REMOVE_QUEUE that > fails. The remaining queues are neither removed nor marked inactive and > the TLB flush is skipped, while qpd->evicted is already raised. During a > GPU reset every REMOVE_QUEUE fails with -EIO, so all queues but the > first stay marked active until the reset is over. > > kfd_process_evict_queues() returns the result of the last device. When > an earlier device reported -EIO, the caller is told that everything was > saved, signals the eviction fence and schedules a restore for a process > that still has a queue on the hardware of that device. > > restore_process_queues_cpsch() marks a queue active and counts it before > ADD_QUEUE is sent. When a later queue fails, qpd->evicted stays set, and > the retry counts the queues that were restored already and adds them > again. Until the retry, the process is half restored: an eviction that > comes first finds qpd->evicted set and leaves the restored queues > running while their buffers are moved. > > Evict every queue and report MES failures as -EIO, which > kfd_process_evict_queues() already takes to mean "evicted, do not add > back", and let it keep that answer when any device gave it. On a failed > restore, take the queues restored in this attempt off the hardware > again, so that the process is fully evicted and the retry starts from a > known state. A queue is counted before ADD_QUEUE is sent, because the > oversubscription check in add_queue_mes() wants to see it, and taken > out of the count again when the command fails. The rollback ends with > the TLB flush that an eviction ends with: the queues it takes off have > been running, and the next eviction finds the process evicted already > and does nothing. > > That leaves the queues whose removal could not be sent, at the eviction > or in the rollback. They stay on the hardware while the process counts > as evicted, and two places take that for a clean state: > > - An eviction that finds qpd->evicted set returns 0 without looking. > Make it return -EIO as long as MES has a queue of the process, so > that a second eviction does not signal the fence the first one held > back. > - A restore finds such a queue mapped and leaves it as it is, with the > page table base from before the eviction. Make it remove the queue > first and fail, to be retried, if that is still not possible. > > One case is left as it is: a queue that the bad queue interrupt path > has deactivated and that MES would not give up is skipped by the first > eviction of its process, which only looks at active queues. Its storage > is safe, the destroy and termination paths ask kfd_mes_queue_retired() > whatever is_active says. > > Signed-off-by: Armas Spann > Co-Authored-By: Claude Fable 5.2 [Severity: Low] This fixes pre-existing bugs in the MES eviction/restore path and in multi-device -EIO propagation in kfd_process_evict_queues(). Should this include: Fixes: cc009e613de6 ("drm/amdkfd: Add KFD support for soc21 v3") Fixes: b8c20c74ab8c ("drm/amd/amdkfd: Don't sent command to HWS on kfd re= set") > 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 58c8efd168011..f20fcccbe88f0 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c > @@ -2017,6 +2017,23 @@ static int evict_process_queues_nocpsch(struct dev= ice_queue_manager *dqm, > return ret; > } > =20 > +/* > + * dqm lock held. True if MES still has a queue of a process that counts= as > + * evicted: a removal could not be sent, or was not confirmed. > + */ > +static bool kfd_mes_has_queue_of(struct device_queue_manager *dqm, > + struct qcm_process_device *qpd) > +{ > + struct queue *q; > + > + list_for_each_entry(q, &qpd->queues_list, list) { > + if (!kfd_mes_queue_retired(dqm, q)) > + return true; > + } > + > + return false; > +} [Severity: High] Does kfd_mes_has_queue_of() miss quarantined queues on dqm->zombie_queues? When destroy_queue_cpsch() destroys a queue that MES has not retired, it unlinks the queue from qpd->queues_list and moves it to dqm->zombie_queues with q->zombie_pdd =3D pdd. If a first eviction fails with -EIO and userspace destroys that queue while the process stays alive, a subsequent eviction sees nothing unretired on qpd->queues_list and returns 0. evict_process_worker() then signals p->ef, letting TTM move or reallocate the live process's VRAM BOs while MES still holds the quarantined queue mapped in its GPU VM. Scanning dqm->zombie_queues for entries with q->zombie_pdd =3D=3D qpd_to_pdd(qpd) under the held dqm lock would cover th= ose queues too. > @@ -2026,8 +2043,18 @@ static int evict_process_queues_cpsch(struct devic= e_queue_manager *dqm, > int retval =3D 0; > =20 > dqm_lock(dqm); > - if (qpd->evicted++ > 0) /* already evicted, do nothing */ > + if (qpd->evicted++ > 0) { /* already evicted, do nothing */ > + /* > + * Nothing to do is not the same as nothing left running. If > + * the eviction that came first, or the rollback of a failed > + * restore, could not get a queue off the hardware, this > + * caller must not be told that its memory is safe to move > + * either. > + */ > + if (kfd_mes_has_queue_of(dqm, qpd)) > + retval =3D -EIO; > goto out; > + } [Severity: High] Can incrementing qpd->evicted before returning -EIO leak the eviction refcount and deadlock p->ef? kfd_process_evict_queues() preserves -EIO without rolling back qpd->evicted, and evict_process_worker() takes its error branch on any non-zero return without calling kfd_process_restore_queues(), scheduling p->restore_work, or signaling p->ef: restore_process_worker evict_process_worker restore_process_helper() ...restore_process_bos() attach new p->ef restore_process_queues_cpsch() add Q1, fail Q2 rollback: remove Q1 -> -EIO (qpd->evicted =3D=3D 1, Q1 mapped) schedule retry kfd_process_evict_queues() evict_process_queues_cpsch() qpd->evicted++ (1 -> 2) kfd_mes_has_queue_of() -> true return -EIO return without restore or signal restore_process_queues_cpsch() qpd->evicted > 1: qpd->evicted-- (2 -> 1) <-- leaks refcount, p->ef stuck return 0 The retry stops because restore_process_queues_cpsch() returned 0, leaving the process permanently evicted across all its GPUs and TTM waiters blocked on p->ef indefinitely. criu_process_info() leaks the same +1 when kfd_process_evict_queues() returns -EIO. > @@ -2056,11 +2083,24 @@ static int evict_process_queues_cpsch(struct devi= ce_queue_manager *dqm, > decrement_queue_count(dqm, qpd, q); > =20 > if (dqm->dev->kfd->shared_resources.enable_mes) { > - retval =3D remove_queue_mes(dqm, q, qpd); > - if (retval) { > - dev_err(dev, "Failed to evict queue %d\n", > - q->properties.queue_id); > - goto out; > + int r =3D remove_queue_mes(dqm, q, qpd); > + > + if (r) { > + /* > + * -EIO means a GPU reset is under way or MES > + * is fenced off, both reported already. > + */ > + if (r !=3D -EIO) > + dev_err(dev, "Failed to evict queue %d\n", > + q->properties.queue_id); > + /* > + * Keep going so that every queue ends up in > + * the evicted state. The caller treats -EIO > + * as evicted, not to be added back. MES may > + * still use this MQD, so it stays pinned. > + */ > + retval =3D -EIO; > + continue; > } > } [Severity: High] Can the first eviction return 0 while MES still holds an unretired inactive queue? Unlike the nested eviction branch above, this loop skips any queue with !q->properties.is_active and never checks kfd_mes_has_queue_of() afterward. In kfd_dqm_suspend_bad_queue_mes() and reset_queue_mes(), a queue is marked inactive even when MES rejects its removal, leaving it in KFD_MES_QUEUE_UNKNOWN. While destroy and termination protect that queue's own MQD and ring storage, returning 0 from the first eviction lets evict_process_worker() signal p->ef so TTM can move or reallocate the process's user VRAM BOs while MES still holds the queue. Checking kfd_mes_has_queue_of(dqm, qpd) after the loop would keep the first eviction consistent with nested evictions. > @@ -2270,25 +2310,48 @@ static int restore_process_queues_cpsch(struct de= vice_queue_manager *dqm, > /* activate all active queues on the qpd */ > list_for_each_entry(q, &qpd->queues_list, list) { > q->properties.is_evicted =3D false; > + > + /* > + * The eviction, or the rollback of an earlier restore, could > + * not take this queue off the hardware (-EIO). MES still has > + * it, with the page table base it was added with, and > + * add_queue_mes() would leave it at that. Remove it first, > + * now that MES can be talked to again. A removal that was > + * sent and failed is not repeated; add_queue_mes() refuses > + * such a queue below. > + */ > + if (q->mes_state =3D=3D KFD_MES_QUEUE_MAPPED && > + !kfd_mes_queue_retired(dqm, q)) { > + retval =3D remove_queue_mes(dqm, q, qpd); > + if (retval) { > + dev_err(dev, "Failed to evict queue %d before its restore\n", > + q->properties.queue_id); > + goto rollback; > + } > + } [Severity: Medium] Does add_queue_mes() actually refuse a KFD_MES_QUEUE_UNKNOWN queue when dqm->sched_halt is set? In add_queue_mes(), the early return for (!dqm->sched_running || dqm->sched_halt) runs before the !kfd_mes_queue_retired() check, so it returns 0 instead of -EBUSY while halted. If restore_process_queues_cpsch() runs during a halt_cpsch() window, it marks the unknown queue active and clears qpd->evicted to 0. When unhalt_cpsch() later runs add_all_kfd_queues_mes(), add_queue_mes() returns -EBUSY on that queue and aborts remapping every remaining active queue across all processes on the device. Checking !kfd_mes_queue_retired() before the sched_halt check in add_queue_mes(), or rejecting KFD_MES_QUEUE_UNKNOWN queues here, would prevent that. > if (!QUEUE_IS_ACTIVE(q->properties)) > continue; [Severity: Medium] Can this re-add a hung MES queue that was just reset by the pre-restore remove_queue_mes() above? If the stale KFD_MES_QUEUE_MAPPED queue is hung, remove_queue_mes() triggers reset_queues_mes() -> reset_queue_mes(), which resets the queue, sets q->properties.is_reset =3D true and pdd->has_reset_queue =3D true, signals = the reset and wedged events to userspace, and returns 0. Because QUEUE_IS_ACTIVE() does not check is_reset, the loop immediately marks the reset queue active and maps it back onto MES via add_queue_mes(). > - q->properties.is_active =3D true; > - increment_queue_count(dqm, &pdd->qpd, q); > - > retval =3D dqm_repin_mqd_bo(dqm, q); > if (retval) { > dev_err(dev, "Failed to repin MQD for queue %d\n", > q->properties.queue_id); > - goto out; > + goto rollback; > } > =20 > + /* Counted first: add_queue_mes() looks at the active count */ > + q->properties.is_active =3D true; > + increment_queue_count(dqm, &pdd->qpd, q); > + > if (dqm->dev->kfd->shared_resources.enable_mes) { > retval =3D add_queue_mes(dqm, q, qpd); > if (retval) { > dev_err(dev, "Failed to restore queue %d\n", > q->properties.queue_id); > - goto out; > + q->properties.is_active =3D false; > + decrement_queue_count(dqm, &pdd->qpd, q); > + goto rollback; > } > } > } [Severity: High] Can removing stale queues and re-adding queues in the same loop leave MES with a stale process-context page_table_base and unflushed TLB entries? In MES, page_table_base_addr belongs to the process context. If Q1 is unmapped and Q2 is still KFD_MES_QUEUE_MAPPED, iteration 1 calls add_queue_mes() on Q1 while Q2 is still mapped under the old page_table_base (and if removing Q2 then fails, Q1 was already restarted on hardware). Even with a single stale KFD_MES_QUEUE_MAPPED queue, remove_queue_mes() is followed immediately by add_queue_mes() without a heavyweight TLB flush, so stale TLB entries and in-flight accesses from the old page tables are not drained. Removing all stale mapped queues in a first pass, flushing the TLB if any were removed, and repinning/adding in a second pass would fix both. > @@ -2302,6 +2365,35 @@ static int restore_process_queues_cpsch(struct dev= ice_queue_manager *dqm, > out: > dqm_unlock(dqm); > return retval; > + > +rollback: > + /* > + * A half restored process is a problem for the next eviction: it > + * finds qpd->evicted set and leaves the queues restored so far > + * running. Take those off the hardware again, so that the process > + * is fully evicted and the retry starts from a known state. A > + * removal that cannot be sent (-EIO) leaves the queue mapped and > + * marked evicted, as evict_process_queues_cpsch() does. > + */ > + list_for_each_entry(q, &qpd->queues_list, list) { > + q->properties.is_evicted =3D true; > + if (!q->properties.is_active) > + continue; > + > + q->properties.is_active =3D false; > + decrement_queue_count(dqm, qpd, q); > + if (dqm->dev->kfd->shared_resources.enable_mes) > + remove_queue_mes(dqm, q, qpd); > + } > + > + /* > + * As at the end of an eviction: the queues that were taken off > + * above have been running, MES does not flush the TLB for them, and > + * the next eviction finds qpd->evicted set and does nothing. > + */ > + if (dqm->dev->kfd->shared_resources.enable_mes) > + kfd_flush_tlb(pdd); > + goto out; > } [Severity: Medium] Can calling kfd_flush_tlb(pdd) here and in evict_process_queues_cpsch() while MES still holds queues suppress a later required TLB flush? amdgpu_vm_flush_compute_tlb() advances vm->kfd_last_flushed_seq to vm->tlb_seq before flushing and never rolls it back if the flush fails. When remove_queue_mes() fails with -EIO and a queue remains mapped in MES, that running queue can refill the TLB after kfd_last_flushed_seq has already been updated, causing a later sequence-gated kfd_flush_tlb() for the same tlb_seq to return early as a no-op. [Severity: Medium] Can kfd_flush_tlb(pdd) in this rollback path become a no-op and fail to drain in-flight accesses from the queues that were just removed? amdgpu_vm_flush_compute_tlb() skips amdgpu_gmc_flush_gpu_tlb_pasid_xccs() whenever vm->kfd_last_flushed_seq already equals vm->tlb_seq. On a restore retry (or a direct kfd_process_restore_queues() without a page-table update), vm->tlb_seq has not advanced since the previous flush, so kfd_flush_tlb(pdd) does nothing even though the rolled-back queues just ran on hardware. [Severity: Medium] Does calling kfd_flush_tlb(pdd) unconditionally here and after failed removals in evict_process_queues_cpsch() bypass kfd_mes_fenced(dqm) and stall GPU reset? When remove_queue_mes() or add_queue_mes() times out, it fences MES and schedules GPU reset asynchronously. Before the reset worker takes down_write(&adev->reset_domain->sem), kfd_flush_tlb(pdd) acquires down_read_trylock() and on GFX12.0/GFX12.1 submits a synchronous MES TLB invalidation that busy-polls in amdgpu_fence_wait_polling() with udelay(2) for 2.1s (9s on SR-IOV) per master XCC while holding dqm_lock(dqm) and reset_domain->sem, starving the pending GPU reset. Skipping kfd_flush_tlb() when kfd_mes_fenced(dqm) is true (and in rollback when no queue was activated) would avoid that stall. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261204125251.1070= 3-1-zappel@simple-co.de?part=3D6