All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alex Deucher" <alexander.deucher@amd.com>,
	amd-gfx@lists.freedesktop.org
Subject: Re: [PATCH 10/11] drm/amdgpu/userq: enable support for creating gangs
Date: Wed, 02 Dec 2026 16:09:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-246196@kernel.org> (raw)
In-Reply-To: <20261202155137.1102730-10-alexander.deucher@amd.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/amdgpu/userq: lock inversion in amdgpu_userq_create_gang()
- [High] drm/amdgpu/userq: race with GPU reset in amdgpu_userq_destroy()
  gang cleanup
--

commit 486a3969869006b255cd94bce57543c91555d429
Author: Alex Deucher <alexander.deucher@amd.com>

drm/amdgpu/userq: enable support for creating gangs

This commit adds the AMDGPU_USERQ_OP_CREATE_GANG ioctl operation to pair a
leader and follower usermode queue into a gang, and cleans up the gang
links when a queue is destroyed.

> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> index 0785759967144..6135a5c110ef4 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c
> @@ -566,6 +566,7 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
>  {
>  	struct amdgpu_device *adev = uq_mgr->adev;
>  	const struct amdgpu_userq_funcs *uq_funcs = adev->userq_funcs[queue->queue_type];
> +	struct amdgpu_usermode_queue *other_gang_queue;
>  	int r = 0;
>  
>  	trace_amdgpu_userq_destroy_start(queue);
> @@ -585,6 +586,17 @@ amdgpu_userq_destroy(struct amdgpu_userq_mgr *uq_mgr, struct amdgpu_usermode_que
>  	debugfs_remove_recursive(queue->debugfs_queue);
>  #endif
>  	r = amdgpu_userq_unmap_helper(queue);
> +	/* clean up gangs */
> +	if (queue->gang_leader) {
> +		other_gang_queue = queue->gang_leader;
> +		other_gang_queue->gang_follower = NULL;
> +		queue->gang_leader = NULL;
> +	}
> +	if (queue->gang_follower) {
> +		other_gang_queue = queue->gang_follower;
> +		other_gang_queue->gang_leader = NULL;
> +		queue->gang_follower = NULL;
> +	}
>  	atomic_dec(&uq_mgr->userq_count[queue->queue_type]);
>  	amdgpu_userq_fence_driver_free(queue);
>  	queue->fence_drv = NULL;

[Severity: High]
Can amdgpu_userq_post_reset() access a follower queue while or after
amdgpu_userq_destroy() frees it?

amdgpu_userq_pre_reset() and amdgpu_userq_post_reset() walk
adev->userq_doorbell_xa under down_write(&adev->reset_domain->sem) without
taking uq_mgr->userq_mutex.  In amdgpu_userq_destroy(),
amdgpu_userq_detach_doorbell() and amdgpu_userq_unmap_helper() each take
and release down_read(&adev->reset_domain->sem) before gang_follower is
cleared here, and kfree(queue) runs without reset_domain->sem:

CPU0                                  CPU1
amdgpu_userq_destroy(follower)
  amdgpu_userq_detach_doorbell()
  amdgpu_userq_unmap_helper()
    /* releases reset_domain->sem */
                                      amdgpu_userq_post_reset()
                                        /* holds reset_domain->sem */
                                        amdgpu_userq_set_gang_helper(leader)
                                          follower = leader->gang_follower
  leader->gang_follower = NULL
  mutex_unlock(&uq_mgr->userq_mutex)
  uq_funcs->mqd_destroy(follower)
  kfree(follower)
                                          follower->state
                                          userq_funcs->set_gang(leader,
                                            follower)  <-- use-after-free

Even if the reset runs earlier, between amdgpu_userq_detach_doorbell() and
amdgpu_userq_unmap_helper(), follower has already been erased from
userq_doorbell_xa while follower->state is still AMDGPU_USERQ_STATE_MAPPED,
so amdgpu_userq_post_reset() skips remapping follower after the reset but
still calls set_gang(leader, follower) on it.

> @@ -838,6 +850,106 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
>  	return r;
>  }
>  
> +static int
> +amdgpu_userq_create_gang(struct drm_file *filp, union drm_amdgpu_userq *args)
> +{
> +	struct amdgpu_usermode_queue *leader_queue, *follower_queue;
> +	struct amdgpu_fpriv *fpriv = filp->driver_priv;
> +	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
> +	const struct amdgpu_userq_funcs *userq_funcs;
> +	struct amdgpu_device *adev = uq_mgr->adev;
> +	int leader_queue_id = args->in_cg.leader_queue_id;
> +	int follower_queue_id = args->in_cg.follower_queue_id;
> +	int r;
> +
> +	/* The reset changes the queue state and the MES is unusable meanwhile */
> +	guard(rwsem_read)(&adev->reset_domain->sem);
> +
> +	mutex_lock(&uq_mgr->userq_mutex);
> +	leader_queue = amdgpu_userq_get(uq_mgr, leader_queue_id);
> +	follower_queue = amdgpu_userq_get(uq_mgr, follower_queue_id);

[ ... ]

> +out:
> +	mutex_unlock(&uq_mgr->userq_mutex);
> +	amdgpu_userq_put(leader_queue);
> +	amdgpu_userq_put(follower_queue);
> +
> +	return r;
> +}

[Severity: High]
Does taking adev->reset_domain->sem before uq_mgr->userq_mutex invert the
lock hierarchy?

All other userq paths and amdgpu_lockdep_init() take uq_mgr->userq_mutex
outer to adev->reset_domain->sem (which is acquired inside
amdgpu_userq_map_helper(), amdgpu_userq_unmap_helper(), and
amdgpu_userq_detach_doorbell()):

CPU0                              CPU1
----                              ----
lock(&uq_mgr->userq_mutex)
                                  down_read(&adev->reset_domain->sem)
down_read(&adev->reset_domain->sem)
                                  lock(&uq_mgr->userq_mutex)

 *** DEADLOCK ***

CPU0: amdgpu_userq_destroy() -> amdgpu_userq_unmap_helper()
CPU1: amdgpu_userq_create_gang()

When a GPU reset writer is queued on adev->reset_domain->sem, CPU0's
down_read() blocks behind it while CPU1 blocks on uq_mgr->userq_mutex.

In addition, because guard(rwsem_read)(&adev->reset_domain->sem) is scoped
to the whole function, amdgpu_userq_put() at out: runs with
reset_domain->sem still read-locked.  If a concurrent AMDGPU_USERQ_OP_FREE
drops its reference first, amdgpu_userq_put() here enters
amdgpu_userq_destroy() under reset_domain->sem, where
cancel_delayed_work_sync(&queue->hang_detect_work) and the inner
down_read(&adev->reset_domain->sem) calls can also deadlock against a reset.

Acquiring reset_domain->sem only around userq_funcs->set_gang() (or inside
amdgpu_userq_set_gang_helper() while holding uq_mgr->userq_mutex) would
preserve the lock order and release the semaphore before amdgpu_userq_put().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202155137.1102730-1-alexander.deucher@amd.com?part=10

  reply	other threads:[~2026-12-02 16:09 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 15:51 [PATCH 01/11] drm/amdgpu/userq: Add INFO IOCTL query for doorbell ranges Alex Deucher
2026-12-02 15:51 ` [PATCH 02/11] drm/amdgpu/mes11: add API for gang submit Alex Deucher
2026-12-02 15:51 ` [PATCH 03/11] drm/amdgpu/mes: add callback " Alex Deucher
2026-12-02 15:51 ` [PATCH 04/11] drm/amdgpu/mes11: add support for setting " Alex Deucher
2026-12-02 15:51 ` [PATCH 05/11] drm/amdgpu/mes12: " Alex Deucher
2026-12-02 15:51 ` [PATCH 06/11] drm/amdgpu/userq: add callback for gang setup Alex Deucher
2026-12-02 15:51 ` [PATCH 07/11] drm/amdgpu/userq/mes: implement set gang submit Alex Deucher
2026-12-02 15:51 ` [PATCH 08/11] drm/amdgpu/userq: add handling for set_gang Alex Deucher
2026-12-02 16:10   ` sashiko-bot
2026-12-02 15:51 ` [PATCH 09/11] drm/amdgpu: add UAPI to create user queue gangs Alex Deucher
2026-12-02 16:16   ` Mohan Marimuthu, Yogesh
2026-12-02 15:51 ` [PATCH 10/11] drm/amdgpu/userq: enable support for creating gangs Alex Deucher
2026-12-02 16:09   ` sashiko-bot [this message]
2026-12-02 15:51 ` [PATCH 11/11] drm/amdgpu: bump driver version for userq updates Alex Deucher
  -- strict thread matches above, loose matches on Subject: below --
2026-11-18 22:37 [PATCH 01/11] drm/amdgpu/userq: Add INFO IOCTL query for doorbell ranges Alex Deucher
2026-11-18 22:37 ` [PATCH 10/11] drm/amdgpu/userq: enable support for creating gangs Alex Deucher
2026-11-18 22:58   ` 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-246196@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexander.deucher@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.