AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jesse Zhang" <Jesse.Zhang@amd.com>, amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH v5 08/15] drm/amdgpu/userq: add helpers to keep the user queues of one engine across a reset
Date: Thu, 26 Nov 2026 10:03:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-235224@kernel.org> (raw)
In-Reply-To: <20261126093417.192538-8-Jesse.Zhang@amd.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] drm/amdgpu/userq: unsynchronized xa_for_each() UAF and race in
  HQD helpers
--

commit f5832ab9e726e4c6325d50c80fa806f8412f97f7
Author: Jesse Zhang <Jesse.Zhang@amd.com>

drm/amdgpu/userq: add helpers to keep the user queues of one engine across a reset

This commit adds helpers to save, restore, and clear the HQDs of user
queues belonging to a single engine across a soft reset.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> index ed6f3b67e8720..495198ba1b0b0 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> @@ -1594,3 +1594,110 @@ int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost)
>  
>  	return ret;
>  }
> +
> +/**
> + * amdgpu_userq_save_hqds - save the HQDs of the user queues before an engine reset
> + * @adev: amdgpu device
> + * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine
> + */
> +void amdgpu_userq_save_hqds(struct amdgpu_device *adev, u32 queue_type)
> +{
> +	struct amdgpu_gfx_saved_hqd hqd;
> +	struct amdgpu_usermode_queue *queue;
> +	unsigned long queue_id;
> +
> +	adev->gfx.num_saved_gfx_hqds = 0;
> +	if (!adev->gfx.funcs->save_hqd)
> +		return;
> +
> +	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
> +		if (adev->gfx.num_saved_gfx_hqds >= AMDGPU_MAX_GFX_QUEUES)
> +			break;
> +		if (queue->queue_type != queue_type ||
> +		    queue->state != AMDGPU_USERQ_STATE_MAPPED)
> +			continue;
> +		if (adev->gfx.funcs->save_hqd(adev, queue->doorbell_index, &hqd) &&
> +		    hqd.on_hqd)
> +			adev->gfx.saved_gfx_hqds[adev->gfx.num_saved_gfx_hqds++] = hqd;
> +	}
> +}
> +
> +static struct amdgpu_gfx_saved_hqd *
> +amdgpu_userq_find_saved_hqd(struct amdgpu_device *adev,
> +			    struct amdgpu_usermode_queue *queue)
> +{
> +	struct amdgpu_gfx_saved_hqd *hqd;
> +	u32 i;
> +
> +	for (i = 0; i < adev->gfx.num_saved_gfx_hqds; i++) {
> +		hqd = &adev->gfx.saved_gfx_hqds[i];
> +		/* Check the MQD too: a new queue may reuse the doorbell. */
> +		if (hqd->doorbell_index == queue->doorbell_index &&
> +		    hqd->mqd_addr == queue->mqd.gpu_addr)
> +			return hqd;
> +	}
> +	return NULL;
> +}
> +
> +/**
> + * amdgpu_userq_restore_hqds - restore the user queues after an engine reset
> + * @adev: amdgpu device
> + * @queue_type: AMDGPU_HW_IP_* type of the queues on the reset engine
> + * @guilty: the queue that hung, or NULL
> + *
> + * MES doesn't map a queue whose HQD the reset cleared again on its own: unmap
> + * it from MES, restore its MQD, and map it again.
> + */
> +void amdgpu_userq_restore_hqds(struct amdgpu_device *adev, u32 queue_type,
> +			       struct amdgpu_usermode_queue *guilty)
> +{
> +	const struct amdgpu_userq_funcs *userq_funcs = adev->userq_funcs[queue_type];
> +	struct amdgpu_usermode_queue *queue;
> +	struct amdgpu_gfx_saved_hqd *hqd;
> +	unsigned long queue_id;
> +	int r;
> +
> +	xa_for_each(&adev->userq_doorbell_xa, queue_id, queue) {
> +		if (queue->queue_type != queue_type ||
> +		    queue->state != AMDGPU_USERQ_STATE_MAPPED)
> +			continue;
> +
> +		if (queue == guilty) {
> +			userq_funcs->unmap(queue);
> +			amdgpu_userq_fence_driver_force_completion(queue);
> +			trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG);
> +			queue->state = AMDGPU_USERQ_STATE_HUNG;
> +			continue;
> +		}
> +
> +		hqd = amdgpu_userq_find_saved_hqd(adev, queue);
> +		if (!hqd)
> +			continue;
> +
> +		userq_funcs->unmap(queue);
> +		if (adev->gfx.funcs->restore_hqd(adev, queue->mqd.cpu_ptr,
> +						 queue->userq_prop->queue_size,
> +						 hqd))
> +			amdgpu_userq_fence_driver_force_completion_with_seqno(queue,
> +									      hqd->wptr);
> +		r = userq_funcs->map(queue);

[Severity: High]
Can amdgpu_userq_save_hqds() and amdgpu_userq_restore_hqds() race with
amdgpu_userq_destroy() and cause a use-after-free on queue?

Unlike a full GPU reset, which holds down_write(&adev->reset_domain->sem),
an engine soft reset in amdgpu_device_ip_soft_reset() only acquires
down_read(&adev->reset_domain->sem).  amdgpu_userq_detach_doorbell() and
amdgpu_userq_unmap_helper() in the queue destruction path also only acquire
down_read(&adev->reset_domain->sem), so queue destruction is not blocked
while these helpers walk userq_doorbell_xa (as the comment in
amdgpu_userq_find_saved_hqd() also notes).  Because xa_for_each() releases
rcu_read_lock() before entering the loop body and nothing pins queue or
holds queue->userq_mgr->userq_mutex:

CPU0                                  CPU1
amdgpu_userq_restore_hqds()
  xa_for_each(..., queue)
                                      amdgpu_userq_destroy()
                                        amdgpu_userq_detach_doorbell(queue)
                                        ...
                                        uq_funcs->mqd_destroy(queue)
                                        kfree(queue)
    if (queue->queue_type != ...)
    userq_funcs->unmap(queue)  <-- use-after-free

Even when the queue is not destroyed, iterating and calling
userq_funcs->unmap() and userq_funcs->map() without holding
queue->userq_mgr->userq_mutex races with concurrent eviction and restore
workers that also run under down_read(&adev->reset_domain->sem), which can
corrupt queue->state or double-unmap and double-free
queue->gang_ctx_array_index.  In addition, if a queue is destroyed and a
new queue is created between amdgpu_userq_save_hqds() and
amdgpu_userq_restore_hqds(), both the freed doorbell index and the freed
GTT MQD address (queue->mqd.gpu_addr) can be reallocated to the new queue,
causing amdgpu_userq_find_saved_hqd() to match and overwrite the new
queue's MQD with the old queue's saved HQD state.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126093417.192538-1-Jesse.Zhang@amd.com?part=8

  reply	other threads:[~2026-11-26 10:03 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  9:33 [PATCH v5 01/15] drm/amdgpu: pass the device to the multi ring reset helpers Jesse Zhang
2026-11-26  9:33 ` [PATCH v5 02/15] drm/amdgpu/ring: During multi-queue reset, only call begin helper if needed Jesse Zhang
2026-11-26  9:52   ` sashiko-bot
2026-11-26  9:33 ` [PATCH v5 03/15] drm/amdgpu: add an engine scope to the IP block soft reset Jesse Zhang
2026-11-26 10:00   ` sashiko-bot
2026-11-26  9:33 ` [PATCH v5 04/15] drm/amdgpu/gfx: add callbacks to save and restore a gfx HQD Jesse Zhang
2026-11-26  9:33 ` [PATCH v5 05/15] drm/amdgpu/gfx12: " Jesse Zhang
2026-11-26  9:33 ` [PATCH v5 06/15] drm/amdgpu/gfx11: " Jesse Zhang
2026-11-26  9:33 ` [PATCH v5 07/15] drm/amdgpu/userq: add a helper to signal the fences of a queue up to a point Jesse Zhang
2026-11-26  9:33 ` [PATCH v5 08/15] drm/amdgpu/userq: add helpers to keep the user queues of one engine across a reset Jesse Zhang
2026-11-26 10:03   ` sashiko-bot [this message]
2026-11-26  9:33 ` [PATCH v5 09/15] drm/amdgpu/gfx12: add a gfx engine soft reset Jesse Zhang
2026-11-26  9:57   ` sashiko-bot
2026-11-26  9:33 ` [PATCH v5 10/15] drm/amdgpu/gfx11: " Jesse Zhang
2026-11-26  9:59   ` sashiko-bot
2026-11-26  9:33 ` [PATCH v5 11/15] drm/amdgpu/gfx12: include SOFT_RESET_GFX in the " Jesse Zhang
2026-11-26  9:33 ` [PATCH v5 12/15] drm/amdgpu/gfx11: " Jesse Zhang
2026-11-26  9:57   ` sashiko-bot
2026-11-26  9:33 ` [PATCH v5 13/15] drm/amdgpu/userq: reset the gfx engine when a gfx user queue reset fails Jesse Zhang
2026-11-26 10:15   ` sashiko-bot
2026-11-26  9:33 ` [PATCH v5 14/15] drm/amdgpu/gfx12: enable the gfx engine soft reset Jesse Zhang
2026-11-26  9:33 ` [PATCH v5 15/15] drm/amdgpu/gfx11: " Jesse Zhang
2026-11-26 10:10   ` sashiko-bot

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-235224@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Jesse.Zhang@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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