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 6/8] drm/amdkfd: keep eviction and restore consistent when MES fails
Date: Fri, 04 Dec 2026 17:07:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-250263@kernel.org> (raw)
In-Reply-To: <20261204125251.10703-7-zappel@simple-co.de>

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 <zappel@simple-co.de>

> 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 <zappel@simple-co.de>
> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

[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 reset")

> 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 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 device_queue_manager *dqm,
>  	return ret;
>  }
>  
> +/*
> + * 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 = 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 == qpd_to_pdd(qpd) under the held dqm lock would cover those
queues too.

> @@ -2026,8 +2043,18 @@ static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
>  	int retval = 0;
>  
>  	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 = -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 == 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 device_queue_manager *dqm,
>  		decrement_queue_count(dqm, qpd, q);
>  
>  		if (dqm->dev->kfd->shared_resources.enable_mes) {
> -			retval = 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 = 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 != -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 = -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 device_queue_manager *dqm,
>  	/* activate all active queues on the qpd */
>  	list_for_each_entry(q, &qpd->queues_list, list) {
>  		q->properties.is_evicted = 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 == KFD_MES_QUEUE_MAPPED &&
> +		    !kfd_mes_queue_retired(dqm, q)) {
> +			retval = 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 = true and pdd->has_reset_queue = 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 = true;
> -		increment_queue_count(dqm, &pdd->qpd, q);
> -
>  		retval = 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;
>  		}
>  
> +		/* Counted first: add_queue_mes() looks at the active count */
> +		q->properties.is_active = true;
> +		increment_queue_count(dqm, &pdd->qpd, q);
> +
>  		if (dqm->dev->kfd->shared_resources.enable_mes) {
>  			retval = 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 = 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 device_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 = true;
> +		if (!q->properties.is_active)
> +			continue;
> +
> +		q->properties.is_active = 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.

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

  reply	other threads:[~2026-12-04 17:08 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
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 [this message]
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-250263@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