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 4/8] drm/amdkfd: add a quarantine for queues MES has not retired
Date: Fri, 04 Dec 2026 16:00:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-250148@kernel.org> (raw)
In-Reply-To: <20261204125251.10703-5-zappel@simple-co.de>

Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [Critical] drm/amdkfd: unlocked zombie_queues walk and reset race with
  zombie reaper
- [High] drm/amdkfd: zombie_work races with suspend, S0ix halt, and GPU
  reset
- [High] drm/amdkfd: remove_all_kfd_queues_mes() skips quarantined
  zombie queues
- [High] drm/amdkfd: self-deadlock in restore workers calling
  synchronous mmput()
- [High] drm/amdkfd: per-process mes_hold deadlocks multi-GPU PCI bridge
  hot-unplug
- [High] drm/amdkfd: svm_range_list_stop_restore() fails for secondary
  kfd_process
- [Medium] drm/amdkfd: amdgpu_amdkfd_criu_resume() races with
  stop_userptr_restore()
- [Medium] drm/amdkfd: busy-spin in kgd2kfd_teardown_processes() during
  cross-GPU wait
- [Medium] drm/amdkfd: kfd_dqm_zombie_worker() runs during sched_halt
  S0ix suspend
--

commit 814b42641595b4e0eb635afb5ca2d6b3f95a1173
Author: Armas Spann <zappel@simple-co.de>

drm/amdkfd: add a quarantine for queues MES has not retired

This commit adds a per-device zombie queue list and delayed reaper worker to
keep queue and process storage alive until MES confirms retirement or
restarts.  It also stops the SVM and userptr restore works during MMU
notifier release and retires device queues during device teardown.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> index 36840569b0377..9b79d43ab9ed4 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> @@ -1643,6 +1643,36 @@ uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *drm_priv)
>  	return avm->pd_phys_addr;
>  }
>  

[ ... ]

> +void amdgpu_amdkfd_gpuvm_stop_userptr_restore(void *drm_priv)
> +{
> +	struct amdgpu_vm *avm = drm_priv_to_vm(drm_priv);
> +	struct amdkfd_process_info *pinfo = avm->process_info;
> +
> +	if (!pinfo)
> +		return;
> +
> +	mutex_lock(&pinfo->notifier_lock);
> +	mutex_unlock(&pinfo->notifier_lock);
> +
> +	cancel_delayed_work_sync(&pinfo->restore_userptr_work);
> +}

[Severity: Medium]
Can amdgpu_amdkfd_criu_resume() re-queue pinfo->restore_userptr_work after
amdgpu_amdkfd_gpuvm_stop_userptr_restore() returns?

amdgpu_amdkfd_gpuvm_stop_userptr_restore() only drains pinfo->notifier_lock
and does not record a stopped state under pinfo->lock:

CPU0                              CPU1
criu_resume()
  kfd_lookup_process_by_pid()
  mutex_lock(&p->mutex)
                                  kfd_process_notifier_release_internal()
                                    kfd_process_table_remove(p)
                                    ..._stop_userptr_restore()
                                      cancel_delayed_work_sync()
                                    kfd_process_dequeue_from_all_devices()
                                      mutex_lock(&p->mutex)  (blocks)
  amdgpu_amdkfd_criu_resume()
    queue_delayed_work()  <-- re-queued

If a queue is then quarantined, restore_userptr_work keeps running and
rescheduling itself against the process pid until the zombie retires.
Recording a stopped flag on pinfo under pinfo->lock and checking it in
amdgpu_amdkfd_criu_resume() would close this race.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_device.c b/drivers/gpu/drm/amd/amdkfd/kfd_device.c
> index 2200f5c7c38d6..faf882986f1bb 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_device.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_device.c

[ ... ]

> @@ -1840,12 +1836,40 @@ void kgd2kfd_teardown_processes(struct amdgpu_device *adev)
>  	if (!adev->kfd.dev)
>  		return;
>  
> +	/*
> +	 * Queues that MES never confirmed as removed keep their process
> +	 * alive, and such a process is counted in kfd_processes_count after
> +	 * it has left the table. The device is going away, so stop waiting
> +	 * for MES, whether the table is empty or not. The caller has
> +	 * suspended KFD on a device that is still there, so the queues that
> +	 * MES would give up are off the hardware and the scheduler is
> +	 * stopped; nothing more could be sent for the others.
> +	 */
> +	if (adev->kfd.dev->init_complete) {
> +		for (unsigned int i = 0; i < adev->kfd.dev->num_nodes; i++)
> +			kfd_dqm_retire_all(adev->kfd.dev->nodes[i]->dqm);
> +	}

[Severity: High]
Can this deadlock multi-GPU PCIe bridge hot-unplug when a process has active
queues on two MES GPUs behind the same bridge?

On surprise hot-unplug, pciehp_unconfigure_device() marks all downstream GPUs
disconnected, takes pci_rescan_remove_lock, and removes them serially.
kgd2kfd_teardown_processes() on GPU 0 signals the process with SIGBUS, calls
kfd_dqm_retire_all() only on GPU 0, and waits for GPU 0's kfd_processes_count
to reach zero.

When the process exits, GPU 1 has not entered amdgpu_pci_remove() yet, so its
dqm->mes_retire_all is still false and kfd_mes_queue_retired() does not check
pci_dev_is_disconnected().  remove_queue_mes() on disconnected GPU 1 times
out, GPU reset aborts on the disconnected bus, and the queue is quarantined
with a p->mes_hold reference.  Because kfd_process_wq_release() defers
kfd_process_destroy_pdds() for all GPUs until p->mes_hold reaches zero, GPU 0
waits forever while holding pci_rescan_remove_lock, preventing GPU 1's
removal from ever running.

Checking pci_dev_is_disconnected(adev->pdev) in kfd_mes_queue_retired() would
let disconnected sibling GPUs retire their queues instead of deadlocking bridge
removal.

> +	warn_at = jiffies + 10 * HZ;
> +	while (!!atomic_read(&adev->kfd.dev->kfd_processes_count)) {
> +		if (warn_at && time_after(jiffies, warn_at)) {
> +			dev_warn(adev->dev,
> +				 "waiting for %d KFD process(es) to release the device, exited ones are kept by queues quarantined on other GPUs until those GPUs are reset\n",
> +				 atomic_read(&adev->kfd.dev->kfd_processes_count));
> +			warn_at = 0;
> +		}
>  		cond_resched();
> +	}

[Severity: Medium]
Should this loop sleep instead of busy-spinning with cond_resched()?

Now that this wait can last past 10 seconds or indefinitely when an exited
process is held by a quarantined queue on another GPU, cond_resched() keeps
the task in TASK_RUNNING and burns 100% of a CPU core for the entire wait.
Pairing atomic_dec_and_test(&pdd->dev->kfd->kfd_processes_count) in
kfd_process_destroy_pdds() with wake_up_var() and waiting via
wait_var_event_timeout() / wait_var_event() would avoid pegging a CPU.

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

[ ... ]

> @@ -1803,6 +1813,158 @@ static int dqm_repin_mqd_bo(struct device_queue_manager *dqm, struct queue *q)
>  	return 0;
>  }

[ ... ]

> +static void kfd_dqm_zombie_worker(struct work_struct *work)
> +{
> +	struct device_queue_manager *dqm =
> +		container_of(to_delayed_work(work),
> +			     struct device_queue_manager, zombie_work);
> +	struct queue *q, *tmp;
> +	LIST_HEAD(retired);
> +	bool more;
> +
> +	dqm_lock(dqm);
> +	/*
> +	 * The scheduler is stopped for a suspend or a GPU reset. Nothing can
> +	 * be sent to MES then, and the device is in no state to have buffers
> +	 * unmapped and freed. start_cpsch() kicks the reaper when that is
> +	 * over. A device that is being removed stays stopped, and its
> +	 * zombies have to go now.
> +	 */
> +	if (!dqm->sched_running && !dqm->mes_retire_all) {
> +		dqm_unlock(dqm);
> +		return;
> +	}

[Severity: Medium]
Should this guard also check dqm->sched_halt?

During S0ix suspend, amdgpu_amdkfd_suspend() calls
kgd2kfd_stop_sched_all_nodes() -> halt_cpsch(), which sets
dqm->sched_halt = true while leaving dqm->sched_running true.  Because
zombie_work runs on the non-freezable system_dfl_wq and ignores sched_halt,
it keeps polling every 500 ms across S0ix and will unmap, repin, and free
BOs while the device is suspending into S0ix/D3 if a queue retires.  Pausing
on dqm->sched_halt and kicking zombie_work in unhalt_cpsch() (matching
start_cpsch()) would handle S0ix suspend and resume.

[ ... ]

> +	more = !list_empty(&dqm->zombie_queues);
> +	dqm_unlock(dqm);
> +
> +	list_for_each_entry_safe(q, tmp, &retired, zombie_node) {
> +		list_del(&q->zombie_node);
> +		kfd_dqm_release_zombie(dqm, q);
> +	}
> +
> +	if (more)
> +		queue_delayed_work(system_dfl_wq, &dqm->zombie_work,
> +				   msecs_to_jiffies(KFD_MES_ZOMBIE_POLL_MS));
> +}

[Severity: High]
Can kfd_dqm_release_zombie() run concurrently with system suspend or a GPU
reset after dqm_unlock(dqm)?

stop_cpsch() sets dqm->sched_running = false under dqm_lock(dqm) and returns
without waiting for an in-flight zombie_work to finish:

kworker                           CPU1
kfd_dqm_zombie_worker()
  dqm_lock(dqm)
  list_move_tail(..., &retired)
  dqm_unlock(dqm)
                                  amdgpu_device_suspend()
                                    stop_cpsch()
                                      dqm->sched_running = false
                                    amdgpu_device_evict_resources()
                                    amdgpu_ttm_disable_buffer_funcs()
  kfd_dqm_release_zombie()
    kfd_queue_unref_bo_vas()      <-- races with suspend / reset
    dqm_repin_mqd_bo()

kfd_dqm_release_zombie() unmaps BO VAs, repins VRAM MQDs, frees kernel BOs,
and can trigger kfd_process_wq_release() while amdgpu_device_suspend() is
evicting VRAM and tearing down fences (or while kgd2kfd_pre_reset() proceeds
into a GPU reset).  Cancelling zombie_work outside dqm_lock(dqm) when
stopping the scheduler would prevent this overlap.

> +/*
> + * The device is being removed. KFD has been suspended by then, which took
> + * every queue off the hardware that MES would let go of and stopped the
> + * scheduler, or the device is gone altogether. Either way no command can be
> + * sent for what is left, and MES is shut down right after, so nothing a
> + * queue holds can be handed to a new owner that MES would then corrupt.
> + * Treat every queue as retired and release all zombies, so that process
> + * teardown can finish.
> + */
> +void kfd_dqm_retire_all(struct device_queue_manager *dqm)
> +{
> +	dqm_lock(dqm);
> +	dqm->mes_retire_all = true;
> +	dqm_unlock(dqm);
> +
> +	mod_delayed_work(system_dfl_wq, &dqm->zombie_work, 0);
> +}

[Severity: High]
Are KFD_MES_QUEUE_MAPPED zombies actually removed from MES before
kfd_dqm_retire_all() frees their storage?

remove_all_kfd_queues_mes() (called from stop_cpsch() and halt_cpsch()) only
walks dqm->queues and each qpd->queues_list, skipping dqm->zombie_queues.  If
a queue was quarantined in KFD_MES_QUEUE_MAPPED state (for example, destroyed
while the scheduler was halted or a reset held reset_domain->sem),
amdgpu_amdkfd_suspend() during amdgpu_device_ip_fini_early() leaves it mapped
on MES.  kfd_dqm_retire_all() then immediately marks it retired and
kfd_dqm_release_zombie() frees its MQD, gang context, wptr mapping, and GPU
VM while MES is still running, before amdgpu_ip_block_hw_fini() shuts MES
down.

Walking dqm->zombie_queues in remove_all_kfd_queues_mes() would unmap
still-mapped zombies on suspend and unload.

[ ... ]

> @@ -3051,6 +3217,12 @@ static struct queue *find_queue_by_doorbell_offset(struct device_queue_manager *
>  		}
>  	}
>  
> +	/* MES may report a quarantined queue as hung, it still has it */
> +	list_for_each_entry(q, &dqm->zombie_queues, zombie_node) {
> +		if (doorbell_offset == q->properties.doorbell_off)
> +			return q;
> +	}
> +
>  	return NULL;
>  }

[Severity: Critical]
Can this unlocked walk of dqm->zombie_queues race with
kfd_dqm_zombie_worker() freeing a zombie?

KGD-initiated per-queue resets (amdgpu_gfx_reset_mes_compute() ->
amdgpu_amdkfd_reset_mes_queue() -> kfd_reset_queue_mes()) call
find_queue_by_doorbell_offset() for every hung doorbell holding only
adev->gfx.mec.reset_mutex, without dqm_lock(dqm):

CPU0                              kworker
amdgpu_gfx_reset_mes_compute()
  kfd_reset_queue_mes()
    find_queue_by_doorbell_offset()
      walk dqm->zombie_queues
                                  kfd_dqm_zombie_worker()
                                    dqm_lock(dqm)
                                    list_move_tail(..., &retired)
                                    dqm_unlock(dqm)
                                    list_del(&q->zombie_node)
                                    kfd_dqm_release_zombie()
                                      uninit_queue(q)
      dereference q / next        <-- use-after-free / LIST_POISON1

Splicing a node onto the worker's stack-local retired list redirects the
unlocked walker off dqm->zombie_queues onto the stack head or hits
LIST_POISON1 written by list_del().  Even if find_queue_by_doorbell_offset()
returns q first, nothing pins q, pdd, or p, so reset_queue_mes() can
dereference freed memory after kfd_dqm_release_zombie() runs.

On the same unlocked path, reset_queue_mes() ->
remove_queue_mes_on_reset_option() -> kfd_mes_queue_retired() ->
amdgpu_mes_ticket_release() can also race with kfd_dqm_zombie_worker() and
double free ticket->status_offs via amdgpu_wb_free().

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_process.c b/drivers/gpu/drm/amd/amdkfd/kfd_process.c
> index d30386893c6aa..878a6666fc13a 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_process.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_process.c

[ ... ]

> @@ -1469,6 +1511,21 @@ void kfd_process_notifier_release_internal(struct kfd_process *p)
>  	kfd_process_table_remove(p);
>  	cancel_delayed_work_sync(&p->eviction_work);
>  	cancel_delayed_work_sync(&p->restore_work);
> +	/*
> +	 * Not left to svm_range_list_fini() and to the destruction of the
> +	 * GPU VMs: the release worker may now run long after this, and the
> +	 * SVM and userptr restore works look up the mm of the task, which
> +	 * can be a new one by then. Both keep queueing themselves as long
> +	 * as they fail.
> +	 */
> +	svm_range_list_stop_restore(p);
> +	for (i = 0; i < p->n_pdds; i++) {
> +		/* One call covers the process, its VMs share the work */
> +		if (p->pdds[i]->drm_priv) {
> +			amdgpu_amdkfd_gpuvm_stop_userptr_restore(p->pdds[i]->drm_priv);
> +			break;
> +		}
> +	}

[Severity: High]
Does calling cancel_delayed_work_sync() for the SVM and userptr restore works
here self-deadlock when either worker drops the final mm_users reference?

Both svm_range_restore_work() and amdgpu_amdkfd_restore_userptr_worker() take
the mm via get_task_mm() and release it with synchronous mmput(mm) instead of
mmput_async(mm).  If the process exits or calls execve() while either worker
is running, that worker holds the last mm_users reference:

svm_range_restore_work()  [or amdgpu_amdkfd_restore_userptr_worker()]
  mmput(mm)
    __mmput(mm) -> exit_mmap(mm) -> mmu_notifier_release(mm)
      kfd_process_notifier_release_internal(p)
        cancel_delayed_work_sync(&svms->restore_work)  <-- self-deadlock

The system_freezable_wq kworker waits forever for itself while holding the
global mmu_notifier SRCU read lock, which also blocks later
synchronize_srcu() calls in mm/mmu_notifier.c and prevents system suspend.
Switching both workers to mmput_async(mm) (as svm_range_deferred_list_work()
already does) avoids the self-deadlock.

> diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> index 9affc79eb8330..e36bfba0fb644 100644
> --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c
> @@ -2150,9 +2150,19 @@ svm_range_evict(struct svm_range *prange, struct mm_struct *mm,
>  			if (r)
>  				pr_debug("failed to quiesce KFD\n");
>  
> -			pr_debug("schedule to restore svm %p ranges\n", svms);
> -			queue_delayed_work(system_freezable_wq, &svms->restore_work,
> -					   msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS));
> +			/*
> +			 * -ESRCH: the process is being released, there are
> +			 * no queues to restore, and the restore work has
> +			 * been stopped by svm_range_list_stop_restore().
> +			 */
> +			if (r != -ESRCH) {
> +				unsigned long delay =
> +					msecs_to_jiffies(AMDGPU_SVM_RANGE_RESTORE_DELAY_MS);
> +
> +				pr_debug("schedule to restore svm %p ranges\n", svms);
> +				queue_delayed_work(system_freezable_wq,
> +						   &svms->restore_work, delay);
> +			}
>  		}
>  		mutex_unlock(&svms->evict_lock);

[Severity: High]
Does relying on kgd2kfd_quiesce_mm() returning -ESRCH stop
svms->restore_work for a secondary kfd_process?

kgd2kfd_quiesce_mm() looks up the process via find_process_by_mm(), which
matches process->context_id == KFD_CONTEXT_ID_PRIMARY.  When a secondary
kfd_process is closed via kfd_release() ->
kfd_process_notifier_release_internal() while the primary process for the
same mm is still alive, a subsequent MMU invalidation on an unregistered SVM
range (created via svm_range_restore_pages() on a GPU retry fault, with XNACK
then disabled via kfd_ioctl_set_xnack_mode()) gets 0 from
kgd2kfd_quiesce_mm() instead of -ESRCH and re-queues the secondary process's
svms->restore_work.

If that invalidation runs during svm_range_list_fini() between
cancel_delayed_work_sync(&p->svms.restore_work) and
svm_range_remove_notifier(), svms->restore_work fires after kfree(p), causing
a use-after-free in svm_range_restore_work().  Setting a per-svms stopped
flag under svms->evict_lock in svm_range_list_stop_restore() and checking it
before queueing svms->restore_work would handle secondary processes as well.

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

  reply	other threads:[~2026-12-04 16:00 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 [this message]
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-250148@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