Skip to contentSkip to conversation

drm/amdgpu: Simplify user queue locking with global device mutex

PATCH In review Jesse.Zhang 3 participants 7 messages Sep 12, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 drm/amdgpu: Simplify user queue locking with global device mutex

The current user queue implementation uses a dual-mutex scheme with both per-device (adev->userq_mutex) and per-process (uq_mgr->userq_mutex) locking. This overcomplicated design creates potential deadlock scenarios and makes the code harder to maintain.

Simplify the locking by switching entirely to the global adev->userq_mutex for all user queue operations. This approach:
- Eliminates potential deadlocks between the two mutexes
- Serializes all user queue handling at the kernel level
- Provides adequate protection since user queue operations aren't performance-critical paths
- Makes the code more maintainable with a single locking scheme

Key changes:
1. Remove uq_mgr->userq_mutex entirely from amdgpu_userq_mgr
2. Consistently use adev->userq_mutex across all user queue operations
3. Add proper work flushing in amdgpu_userq_ensure_ev_fence() to prevent races when ensuring eviction fence availability
4. Maintain proper locking order throughout all user queue functions

The serialization of all user queue operations is acceptable since these are management operations that don't need high parallelism.

---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    |  7 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     | 32 ++++++-------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  1 -
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  5 +--
 4 files changed, 16 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +4 -3
index 23d7d0b0d625..d6a07fac7df2 100644
@@ -105,9 +105,10 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
 	struct amdgpu_eviction_fence_mgr *evf_mgr = work_to_evf_mgr(work, suspend_work.work);
 	struct amdgpu_fpriv *fpriv = evf_mgr_to_fpriv(evf_mgr);
 	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
 	struct amdgpu_eviction_fence *ev_fence;
 
-	mutex_lock(&uq_mgr->userq_mutex);
+	mutex_lock(&adev->userq_mutex);
 	spin_lock(&evf_mgr->ev_fence_lock);
 	ev_fence = evf_mgr->ev_fence;
 	if (ev_fence)
@@ -118,13 +119,13 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
 
 	amdgpu_userq_evict(uq_mgr, ev_fence);
 
-	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 	dma_fence_put(&ev_fence->base);
 	return;
 
 unlock:
 	spin_unlock(&evf_mgr->ev_fence_lock);
-	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 }
 
 static bool amdgpu_eviction_fence_enable_signaling(struct dma_fence *f)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -23
index b649ac0cedff..af92450ea6eb 100644
@@ -198,17 +198,18 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
 			     struct amdgpu_eviction_fence_mgr *evf_mgr)
 {
 	struct amdgpu_eviction_fence *ev_fence;
+	struct amdgpu_device *adev = uq_mgr->adev;
 
 retry:
 	/* Flush any pending resume work to create ev_fence */
 	flush_delayed_work(&uq_mgr->resume_work);
 
-	mutex_lock(&uq_mgr->userq_mutex);
+	mutex_lock(&adev->userq_mutex);
 	spin_lock(&evf_mgr->ev_fence_lock);
 	ev_fence = evf_mgr->ev_fence;
 	spin_unlock(&evf_mgr->ev_fence_lock);
 	if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) {
-		mutex_unlock(&uq_mgr->userq_mutex);
+		mutex_unlock(&adev->userq_mutex);
 		/*
 		 * Looks like there was no pending resume work,
 		 * add one now to create a valid eviction fence
@@ -362,12 +363,12 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 	int r = 0;
 
 	cancel_delayed_work_sync(&uq_mgr->resume_work);
-	mutex_lock(&uq_mgr->userq_mutex);
+	mutex_lock(&adev->userq_mutex);
 
 	queue = amdgpu_userq_find(uq_mgr, queue_id);
 	if (!queue) {
 		drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n");
-		mutex_unlock(&uq_mgr->userq_mutex);
+		mutex_unlock(&adev->userq_mutex);
 		return -EINVAL;
 	}
 	amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
@@ -388,7 +389,7 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		queue->state = AMDGPU_USERQ_STATE_HUNG;
 	}
 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
-	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 
 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -478,7 +479,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 		return r;
 	}
-
 	/*
 	 * There could be a situation that we are creating a new queue while
 	 * the other queues under this UQ_mgr are suspended. So if there is any
@@ -486,7 +486,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	 *
 	 * This will also make sure we have a valid eviction fence ready to be used.
 	 */
-	mutex_lock(&adev->userq_mutex);
 	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
 
 	uq_funcs = adev->userq_funcs[args->in.ip_type];
@@ -588,9 +587,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	kfree(queue_name);
 
 	args->out.queue_id = qid;
-
 unlock:
-	mutex_unlock(&uq_mgr->userq_mutex);
 	mutex_unlock(&adev->userq_mutex);
 
 	return r;
@@ -820,11 +817,12 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 {
 	struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_device *adev = uq_mgr->adev;
 	int ret;
 
 	flush_delayed_work(&fpriv->evf_mgr.suspend_work);
 
-	mutex_lock(&uq_mgr->userq_mutex);
+	mutex_lock(&adev->userq_mutex);
 
 	ret = amdgpu_userq_validate_bos(uq_mgr);
 	if (ret) {
@@ -839,7 +837,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 	}
 
 unlock:
-	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 }
 
 static int
@@ -919,7 +917,6 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
 			  struct amdgpu_device *adev)
 {
-	mutex_init(&userq_mgr->userq_mutex);
 	idr_init_base(&userq_mgr->userq_idr, 1);
 	userq_mgr->adev = adev;
 	userq_mgr->file = file_priv;
@@ -942,7 +939,6 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 	cancel_delayed_work_sync(&userq_mgr->resume_work);
 
 	mutex_lock(&adev->userq_mutex);
-	mutex_lock(&userq_mgr->userq_mutex);
 	idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) {
 		amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
 		amdgpu_userq_unmap_helper(userq_mgr, queue);
@@ -956,9 +952,7 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 		}
 	}
 	idr_destroy(&userq_mgr->userq_idr);
-	mutex_unlock(&userq_mgr->userq_mutex);
 	mutex_unlock(&adev->userq_mutex);
-	mutex_destroy(&userq_mgr->userq_mutex);
 }
 
 int amdgpu_userq_suspend(struct amdgpu_device *adev)
@@ -975,13 +969,11 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)
 	mutex_lock(&adev->userq_mutex);
 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
 		cancel_delayed_work_sync(&uqm->resume_work);
-		mutex_lock(&uqm->userq_mutex);
 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
 			r = amdgpu_userq_unmap_helper(uqm, queue);
 			if (r)
 				ret = r;
 		}
-		mutex_unlock(&uqm->userq_mutex);
 	}
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
@@ -1000,13 +992,11 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
 
 	mutex_lock(&adev->userq_mutex);
 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-		mutex_lock(&uqm->userq_mutex);
 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
 			r = amdgpu_userq_map_helper(uqm, queue);
 			if (r)
 				ret = r;
 		}
-		mutex_unlock(&uqm->userq_mutex);
 	}
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
@@ -1031,7 +1021,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 	adev->userq_halt_for_enforce_isolation = true;
 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
 		cancel_delayed_work_sync(&uqm->resume_work);
-		mutex_lock(&uqm->userq_mutex);
 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1041,7 +1030,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 					ret = r;
 			}
 		}
-		mutex_unlock(&uqm->userq_mutex);
 	}
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
@@ -1065,7 +1053,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 		dev_warn(adev->dev, "userq scheduling already started!\n");
 	adev->userq_halt_for_enforce_isolation = false;
 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-		mutex_lock(&uqm->userq_mutex);
 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1075,7 +1062,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 					ret = r;
 			}
 		}
-		mutex_unlock(&uqm->userq_mutex);
 	}
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -1
index c027dd916672..2d63308d55c3 100644
@@ -89,7 +89,6 @@ struct amdgpu_userq_funcs {
 /* Usermode queues for gfx */
 struct amdgpu_userq_mgr {
 	struct idr			userq_idr;
-	struct mutex			userq_mutex;
 	struct amdgpu_device		*adev;
 	struct delayed_work		resume_work;
 	struct list_head		list;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +3 -2
index 95e91d1dc58a..daf3be92a39c 100644
@@ -464,6 +464,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 	u32 *bo_handles_write, num_write_bo_handles;
 	u32 *syncobj_handles, num_syncobj_handles;
 	u32 *bo_handles_read, num_read_bo_handles;
+	struct amdgpu_device *adev = userq_mgr->adev;
 	int r, i, entry, rentry, wentry;
 	struct dma_fence *fence;
 	struct drm_exec exec;
@@ -557,14 +558,14 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 	/* Create a new fence */
 	r = amdgpu_userq_fence_create(queue, userq_fence, wptr, &fence);
 	if (r) {
-		mutex_unlock(&userq_mgr->userq_mutex);
+		mutex_unlock(&adev->userq_mutex);
 		kmem_cache_free(amdgpu_userq_fence_slab, userq_fence);
 		goto put_gobj_write;
 	}
 
 	dma_fence_put(queue->last_fence);
 	queue->last_fence = dma_fence_get(fence);
-	mutex_unlock(&userq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 
 	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT,
 		      (num_read_bo_handles + num_write_bo_handles));
Signature · 1 line
2.49.0
#1 lore raw

PATCH v2 2/2 drm/amdgpu: Implement user queue reset functionality

This patch adds robust reset handling for user queues (userq) to improve recovery from queue failures. The key components include:

1. Queue detection and reset logic:
- amdgpu_userq_detect_and_reset_queues() identifies failed queues
- Per-IP detect_and_reset callbacks for targeted recovery
- Falls back to full GPU reset when needed

2. Reset infrastructure:
- Adds userq_reset_work workqueue for async reset handling
- Implements pre/post reset handlers for queue state management
- Integrates with existing GPU reset framework

3. Error handling improvements:
- Enhanced state tracking with HUNG state
- Automatic reset triggering on critical failures
- VRAM loss handling during recovery

4. Integration points:
- Added to device init/reset paths
- Called during queue destroy, suspend, and isolation events
- Handles both individual queue and full GPU resets

The reset functionality works with both gfx/compute and sdma queues, providing better resilience against queue failures while minimizing disruption to unaffected queues.

v2: add detection and reset calls when preemption/unmaped fails.
add a per device userq counter for each user queue type.(Alex)
v3: make sure we hold the adev->userq_mutex when we call amdgpu_userq_detect_and_reset_queues. (Alex)
warn if the adev->userq_mutex is not held.
v4: make sure we have all of the uqm->userq_mutex held.
warn if the uqm->userq_mutex is not held.

v5: Use array for user queue type counters.(Alex)

    all of the uqm->userq_mutex need to be held when calling detect and reset.  (Alex)

v6: simply the userq lock (Alex)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |   8 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c  | 192 +++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h  |   5 +
 5 files changed, 193 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
index c56839528843..93c255d1ddfa 100644
@@ -1311,6 +1311,7 @@ struct amdgpu_device {
 	struct list_head		userq_mgr_list;
 	struct mutex                    userq_mutex;
 	bool                            userq_halt_for_enforce_isolation;
+	struct work_struct              userq_reset_work;
 	struct amdgpu_uid *uid_info;
 
 	/* KFD
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +8 -0
index b20383021b50..f1d3c16c67cd 100644
@@ -4534,6 +4534,7 @@ int amdgpu_device_init(struct amdgpu_device *adev,
 	}
 
 	INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
+	INIT_WORK(&adev->userq_reset_work, amdgpu_userq_reset_work);
 
 	adev->gfx.gfx_off_req_count = 1;
 	adev->gfx.gfx_off_residency = 0;
@@ -5951,6 +5952,10 @@ int amdgpu_device_reinit_after_reset(struct amdgpu_reset_context *reset_context)
 				if (r)
 					goto out;
 
+				r = amdgpu_userq_post_reset(tmp_adev, vram_lost);
+				if (r)
+					goto out;
+
 				drm_client_dev_resume(adev_to_drm(tmp_adev), false);
 
 				/*
@@ -6173,6 +6178,7 @@ static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev)
 	if (!amdgpu_sriov_vf(adev))
 		cancel_work(&adev->reset_work);
 #endif
+	cancel_work(&adev->userq_reset_work);
 
 	if (adev->kfd.dev)
 		cancel_work(&adev->kfd.reset_work);
@@ -6293,6 +6299,8 @@ static void amdgpu_device_halt_activities(struct amdgpu_device *adev,
 		    amdgpu_device_ip_need_full_reset(tmp_adev))
 			amdgpu_ras_suspend(tmp_adev);
 
+		amdgpu_userq_pre_reset(tmp_adev);
+
 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
 			struct amdgpu_ring *ring = tmp_adev->rings[i];
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +1 -0
index 80b85547c810..cf21f36b5761 100644
@@ -83,6 +83,7 @@ enum amdgpu_ring_type {
 	AMDGPU_RING_TYPE_MES,
 	AMDGPU_RING_TYPE_UMSCH_MM,
 	AMDGPU_RING_TYPE_CPER,
+	AMDGPU_RING_TYPE_MAX,
 };
 
 enum amdgpu_ib_pool_type {
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +178 -14

338 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +5 -0
index 2d63308d55c3..7cd5344c0344 100644
@@ -93,6 +93,7 @@ struct amdgpu_userq_mgr {
 	struct delayed_work		resume_work;
 	struct list_head		list;
 	struct drm_file			*file;
+	atomic_t			userq_count[AMDGPU_RING_TYPE_MAX];
 };
 
 struct amdgpu_db_info {
@@ -136,6 +137,10 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 						   u32 idx);
 
+void amdgpu_userq_reset_work(struct work_struct *work);
+void amdgpu_userq_pre_reset(struct amdgpu_device *adev);
+int amdgpu_userq_post_reset(struct amdgpu_device *adev, bool vram_lost);
+
 int amdgpu_userq_input_va_validate(struct amdgpu_vm *vm, u64 addr,
 				   u64 expected_size);
 #endif
Signature · 1 line
2.49.0
maintainer
#2 lore raw
In reply to Jesse.Zhang
Quoted 20 lines · Jesse.Zhang

The current user queue implementation uses a dual-mutex scheme with both per-device (adev->userq_mutex) and per-process (uq_mgr->userq_mutex) locking. This overcomplicated design creates potential deadlock scenarios and makes the code harder to maintain.

Simplify the locking by switching entirely to the global adev->userq_mutex for all user queue operations. This approach:
- Eliminates potential deadlocks between the two mutexes
- Serializes all user queue handling at the kernel level
- Provides adequate protection since user queue operations aren't performance-critical paths
- Makes the code more maintainable with a single locking scheme

Key changes:
1. Remove uq_mgr->userq_mutex entirely from amdgpu_userq_mgr
2. Consistently use adev->userq_mutex across all user queue operations
3. Add proper work flushing in amdgpu_userq_ensure_ev_fence() to prevent races when ensuring eviction fence availability
4. Maintain proper locking order throughout all user queue functions

Quoted 3 lines

The serialization of all user queue operations is acceptable since these are management operations that don't need high parallelism.

Absolutely clear NAK to that.

This also serialized eviction handling making it completely useless.

The global mutex should only be used when new clients open up their connection for the first time or closes the last reference and never otherwise.

Regards,
Christian.

Quoted 266 lines
---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    |  7 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     | 32 ++++++-------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  1 -
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  5 +--
 4 files changed, 16 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +4 -3
index 23d7d0b0d625..d6a07fac7df2 100644
@@ -105,9 +105,10 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
 	struct amdgpu_eviction_fence_mgr *evf_mgr = work_to_evf_mgr(work, suspend_work.work);
 	struct amdgpu_fpriv *fpriv = evf_mgr_to_fpriv(evf_mgr);
 	struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
 	struct amdgpu_eviction_fence *ev_fence;
 
-	mutex_lock(&uq_mgr->userq_mutex);
+	mutex_lock(&adev->userq_mutex);
 	spin_lock(&evf_mgr->ev_fence_lock);
 	ev_fence = evf_mgr->ev_fence;
 	if (ev_fence)
@@ -118,13 +119,13 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
 
 	amdgpu_userq_evict(uq_mgr, ev_fence);
 
-	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 	dma_fence_put(&ev_fence->base);
 	return;
 
 unlock:
 	spin_unlock(&evf_mgr->ev_fence_lock);
-	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 }
 
 static bool amdgpu_eviction_fence_enable_signaling(struct dma_fence *f)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -23
index b649ac0cedff..af92450ea6eb 100644
@@ -198,17 +198,18 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
 			     struct amdgpu_eviction_fence_mgr *evf_mgr)
 {
 	struct amdgpu_eviction_fence *ev_fence;
+	struct amdgpu_device *adev = uq_mgr->adev;
 
 retry:
 	/* Flush any pending resume work to create ev_fence */
 	flush_delayed_work(&uq_mgr->resume_work);
 
-	mutex_lock(&uq_mgr->userq_mutex);
+	mutex_lock(&adev->userq_mutex);
 	spin_lock(&evf_mgr->ev_fence_lock);
 	ev_fence = evf_mgr->ev_fence;
 	spin_unlock(&evf_mgr->ev_fence_lock);
 	if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) {
-		mutex_unlock(&uq_mgr->userq_mutex);
+		mutex_unlock(&adev->userq_mutex);
 		/*
 		 * Looks like there was no pending resume work,
 		 * add one now to create a valid eviction fence
@@ -362,12 +363,12 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 	int r = 0;
 
 	cancel_delayed_work_sync(&uq_mgr->resume_work);
-	mutex_lock(&uq_mgr->userq_mutex);
+	mutex_lock(&adev->userq_mutex);
 
 	queue = amdgpu_userq_find(uq_mgr, queue_id);
 	if (!queue) {
 		drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n");
-		mutex_unlock(&uq_mgr->userq_mutex);
+		mutex_unlock(&adev->userq_mutex);
 		return -EINVAL;
 	}
 	amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
@@ -388,7 +389,7 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
 		queue->state = AMDGPU_USERQ_STATE_HUNG;
 	}
 	amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
-	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 
 	pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
 	pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -478,7 +479,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 		return r;
 	}
-
 	/*
 	 * There could be a situation that we are creating a new queue while
 	 * the other queues under this UQ_mgr are suspended. So if there is any
@@ -486,7 +486,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	 *
 	 * This will also make sure we have a valid eviction fence ready to be used.
 	 */
-	mutex_lock(&adev->userq_mutex);
 	amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
 
 	uq_funcs = adev->userq_funcs[args->in.ip_type];
@@ -588,9 +587,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	kfree(queue_name);
 
 	args->out.queue_id = qid;
-
 unlock:
-	mutex_unlock(&uq_mgr->userq_mutex);
 	mutex_unlock(&adev->userq_mutex);
 
 	return r;
@@ -820,11 +817,12 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 {
 	struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
 	struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+	struct amdgpu_device *adev = uq_mgr->adev;
 	int ret;
 
 	flush_delayed_work(&fpriv->evf_mgr.suspend_work);
 
-	mutex_lock(&uq_mgr->userq_mutex);
+	mutex_lock(&adev->userq_mutex);
 
 	ret = amdgpu_userq_validate_bos(uq_mgr);
 	if (ret) {
@@ -839,7 +837,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 	}
 
 unlock:
-	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 }
 
 static int
@@ -919,7 +917,6 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
 			  struct amdgpu_device *adev)
 {
-	mutex_init(&userq_mgr->userq_mutex);
 	idr_init_base(&userq_mgr->userq_idr, 1);
 	userq_mgr->adev = adev;
 	userq_mgr->file = file_priv;
@@ -942,7 +939,6 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 	cancel_delayed_work_sync(&userq_mgr->resume_work);
 
 	mutex_lock(&adev->userq_mutex);
-	mutex_lock(&userq_mgr->userq_mutex);
 	idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) {
 		amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
 		amdgpu_userq_unmap_helper(userq_mgr, queue);
@@ -956,9 +952,7 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
 		}
 	}
 	idr_destroy(&userq_mgr->userq_idr);
-	mutex_unlock(&userq_mgr->userq_mutex);
 	mutex_unlock(&adev->userq_mutex);
-	mutex_destroy(&userq_mgr->userq_mutex);
 }
 
 int amdgpu_userq_suspend(struct amdgpu_device *adev)
@@ -975,13 +969,11 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)
 	mutex_lock(&adev->userq_mutex);
 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
 		cancel_delayed_work_sync(&uqm->resume_work);
-		mutex_lock(&uqm->userq_mutex);
 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
 			r = amdgpu_userq_unmap_helper(uqm, queue);
 			if (r)
 				ret = r;
 		}
-		mutex_unlock(&uqm->userq_mutex);
 	}
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
@@ -1000,13 +992,11 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
 
 	mutex_lock(&adev->userq_mutex);
 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-		mutex_lock(&uqm->userq_mutex);
 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
 			r = amdgpu_userq_map_helper(uqm, queue);
 			if (r)
 				ret = r;
 		}
-		mutex_unlock(&uqm->userq_mutex);
 	}
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
@@ -1031,7 +1021,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 	adev->userq_halt_for_enforce_isolation = true;
 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
 		cancel_delayed_work_sync(&uqm->resume_work);
-		mutex_lock(&uqm->userq_mutex);
 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1041,7 +1030,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
 					ret = r;
 			}
 		}
-		mutex_unlock(&uqm->userq_mutex);
 	}
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
@@ -1065,7 +1053,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 		dev_warn(adev->dev, "userq scheduling already started!\n");
 	adev->userq_halt_for_enforce_isolation = false;
 	list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-		mutex_lock(&uqm->userq_mutex);
 		idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
 			if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
 			     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1075,7 +1062,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
 					ret = r;
 			}
 		}
-		mutex_unlock(&uqm->userq_mutex);
 	}
 	mutex_unlock(&adev->userq_mutex);
 	return ret;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -1
index c027dd916672..2d63308d55c3 100644
@@ -89,7 +89,6 @@ struct amdgpu_userq_funcs {
 /* Usermode queues for gfx */
 struct amdgpu_userq_mgr {
 	struct idr			userq_idr;
-	struct mutex			userq_mutex;
 	struct amdgpu_device		*adev;
 	struct delayed_work		resume_work;
 	struct list_head		list;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +3 -2
index 95e91d1dc58a..daf3be92a39c 100644
@@ -464,6 +464,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 	u32 *bo_handles_write, num_write_bo_handles;
 	u32 *syncobj_handles, num_syncobj_handles;
 	u32 *bo_handles_read, num_read_bo_handles;
+	struct amdgpu_device *adev = userq_mgr->adev;
 	int r, i, entry, rentry, wentry;
 	struct dma_fence *fence;
 	struct drm_exec exec;
@@ -557,14 +558,14 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
 	/* Create a new fence */
 	r = amdgpu_userq_fence_create(queue, userq_fence, wptr, &fence);
 	if (r) {
-		mutex_unlock(&userq_mgr->userq_mutex);
+		mutex_unlock(&adev->userq_mutex);
 		kmem_cache_free(amdgpu_userq_fence_slab, userq_fence);
 		goto put_gobj_write;
 	}
 
 	dma_fence_put(queue->last_fence);
 	queue->last_fence = dma_fence_get(fence);
-	mutex_unlock(&userq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 
 	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT,
 		      (num_read_bo_handles + num_write_bo_handles));
#3 lore raw
Quoted 26 lines · Christian König
Quoted 20 lines · Jesse.Zhang

The current user queue implementation uses a dual-mutex scheme with both per-device (adev->userq_mutex) and per-process (uq_mgr->userq_mutex) locking. This overcomplicated design creates potential deadlock scenarios and makes the code harder to maintain.

Simplify the locking by switching entirely to the global adev->userq_mutex for all user queue operations. This approach:
- Eliminates potential deadlocks between the two mutexes
- Serializes all user queue handling at the kernel level
- Provides adequate protection since user queue operations aren't performance-critical paths
- Makes the code more maintainable with a single locking scheme

Key changes:
1. Remove uq_mgr->userq_mutex entirely from amdgpu_userq_mgr
2. Consistently use adev->userq_mutex across all user queue operations
3. Add proper work flushing in amdgpu_userq_ensure_ev_fence() to prevent races when ensuring eviction fence availability
4. Maintain proper locking order throughout all user queue functions

Quoted 3 lines

The serialization of all user queue operations is acceptable since these are management operations that don't need high parallelism.

Quoted 5 lines

Absolutely clear NAK to that.

This also serialized eviction handling making it completely useless.

The global mutex should only be used when new clients open up their connection for the first time or closes the last reference and never otherwise.

The problem is that the firmware reset code can affect multiple queues so we need some way to lock all queues during resets so we can properly update their status.

Alex

Quoted 271 lines

Regards,
Christian.

Quoted 266 lines
---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    |  7 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     | 32 ++++++-------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  1 -
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  5 +--
 4 files changed, 16 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +4 -3
index 23d7d0b0d625..d6a07fac7df2 100644
@@ -105,9 +105,10 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
      struct amdgpu_eviction_fence_mgr *evf_mgr = work_to_evf_mgr(work, suspend_work.work);
      struct amdgpu_fpriv *fpriv = evf_mgr_to_fpriv(evf_mgr);
      struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+     struct amdgpu_device *adev = uq_mgr->adev;
      struct amdgpu_eviction_fence *ev_fence;
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
      spin_lock(&evf_mgr->ev_fence_lock);
      ev_fence = evf_mgr->ev_fence;
      if (ev_fence)
@@ -118,13 +119,13 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
 
      amdgpu_userq_evict(uq_mgr, ev_fence);
 
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
      dma_fence_put(&ev_fence->base);
      return;
 
 unlock:
      spin_unlock(&evf_mgr->ev_fence_lock);
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 }
 
 static bool amdgpu_eviction_fence_enable_signaling(struct dma_fence *f)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -23
index b649ac0cedff..af92450ea6eb 100644
@@ -198,17 +198,18 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
                           struct amdgpu_eviction_fence_mgr *evf_mgr)
 {
      struct amdgpu_eviction_fence *ev_fence;
+     struct amdgpu_device *adev = uq_mgr->adev;
 
 retry:
      /* Flush any pending resume work to create ev_fence */
      flush_delayed_work(&uq_mgr->resume_work);
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
      spin_lock(&evf_mgr->ev_fence_lock);
      ev_fence = evf_mgr->ev_fence;
      spin_unlock(&evf_mgr->ev_fence_lock);
      if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) {
-             mutex_unlock(&uq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              /*
               * Looks like there was no pending resume work,
               * add one now to create a valid eviction fence
@@ -362,12 +363,12 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
      int r = 0;
 
      cancel_delayed_work_sync(&uq_mgr->resume_work);
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
 
      queue = amdgpu_userq_find(uq_mgr, queue_id);
      if (!queue) {
              drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n");
-             mutex_unlock(&uq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              return -EINVAL;
      }
      amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
@@ -388,7 +389,7 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
              queue->state = AMDGPU_USERQ_STATE_HUNG;
      }
      amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
      pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -478,7 +479,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
              pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
              return r;
      }
-
      /*
       * There could be a situation that we are creating a new queue while
       * the other queues under this UQ_mgr are suspended. So if there is any
@@ -486,7 +486,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
       *
       * This will also make sure we have a valid eviction fence ready to be used.
       */
-     mutex_lock(&adev->userq_mutex);
      amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
 
      uq_funcs = adev->userq_funcs[args->in.ip_type];
@@ -588,9 +587,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
      kfree(queue_name);
 
      args->out.queue_id = qid;
-
 unlock:
-     mutex_unlock(&uq_mgr->userq_mutex);
      mutex_unlock(&adev->userq_mutex);
 
      return r;
@@ -820,11 +817,12 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 {
      struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
      struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+     struct amdgpu_device *adev = uq_mgr->adev;
      int ret;
 
      flush_delayed_work(&fpriv->evf_mgr.suspend_work);
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
 
      ret = amdgpu_userq_validate_bos(uq_mgr);
      if (ret) {
@@ -839,7 +837,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
      }
 
 unlock:
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 }
 
 static int
@@ -919,7 +917,6 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
                        struct amdgpu_device *adev)
 {
-     mutex_init(&userq_mgr->userq_mutex);
      idr_init_base(&userq_mgr->userq_idr, 1);
      userq_mgr->adev = adev;
      userq_mgr->file = file_priv;
@@ -942,7 +939,6 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
      cancel_delayed_work_sync(&userq_mgr->resume_work);
 
      mutex_lock(&adev->userq_mutex);
-     mutex_lock(&userq_mgr->userq_mutex);
      idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) {
              amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
              amdgpu_userq_unmap_helper(userq_mgr, queue);
@@ -956,9 +952,7 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
              }
      }
      idr_destroy(&userq_mgr->userq_idr);
-     mutex_unlock(&userq_mgr->userq_mutex);
      mutex_unlock(&adev->userq_mutex);
-     mutex_destroy(&userq_mgr->userq_mutex);
 }
 
 int amdgpu_userq_suspend(struct amdgpu_device *adev)
@@ -975,13 +969,11 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)
      mutex_lock(&adev->userq_mutex);
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
              cancel_delayed_work_sync(&uqm->resume_work);
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      r = amdgpu_userq_unmap_helper(uqm, queue);
                      if (r)
                              ret = r;
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1000,13 +992,11 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
 
      mutex_lock(&adev->userq_mutex);
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      r = amdgpu_userq_map_helper(uqm, queue);
                      if (r)
                              ret = r;
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1031,7 +1021,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
      adev->userq_halt_for_enforce_isolation = true;
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
              cancel_delayed_work_sync(&uqm->resume_work);
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
                           (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1041,7 +1030,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                      ret = r;
                      }
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1065,7 +1053,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
              dev_warn(adev->dev, "userq scheduling already started!\n");
      adev->userq_halt_for_enforce_isolation = false;
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
                           (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1075,7 +1062,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                      ret = r;
                      }
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -1
index c027dd916672..2d63308d55c3 100644
@@ -89,7 +89,6 @@ struct amdgpu_userq_funcs {
 /* Usermode queues for gfx */
 struct amdgpu_userq_mgr {
      struct idr                      userq_idr;
-     struct mutex                    userq_mutex;
      struct amdgpu_device            *adev;
      struct delayed_work             resume_work;
      struct list_head                list;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +3 -2
index 95e91d1dc58a..daf3be92a39c 100644
@@ -464,6 +464,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
      u32 *bo_handles_write, num_write_bo_handles;
      u32 *syncobj_handles, num_syncobj_handles;
      u32 *bo_handles_read, num_read_bo_handles;
+     struct amdgpu_device *adev = userq_mgr->adev;
      int r, i, entry, rentry, wentry;
      struct dma_fence *fence;
      struct drm_exec exec;
@@ -557,14 +558,14 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
      /* Create a new fence */
      r = amdgpu_userq_fence_create(queue, userq_fence, wptr, &fence);
      if (r) {
-             mutex_unlock(&userq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              kmem_cache_free(amdgpu_userq_fence_slab, userq_fence);
              goto put_gobj_write;
      }
 
      dma_fence_put(queue->last_fence);
      queue->last_fence = dma_fence_get(fence);
-     mutex_unlock(&userq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT,
                    (num_read_bo_handles + num_write_bo_handles));
maintainer
#4 lore raw
Quoted 30 lines · Alex Deucher
Quoted 28 lines · Christian König
Quoted 20 lines · Jesse.Zhang

The current user queue implementation uses a dual-mutex scheme with both per-device (adev->userq_mutex) and per-process (uq_mgr->userq_mutex) locking. This overcomplicated design creates potential deadlock scenarios and makes the code harder to maintain.

Simplify the locking by switching entirely to the global adev->userq_mutex for all user queue operations. This approach:
- Eliminates potential deadlocks between the two mutexes
- Serializes all user queue handling at the kernel level
- Provides adequate protection since user queue operations aren't performance-critical paths
- Makes the code more maintainable with a single locking scheme

Key changes:
1. Remove uq_mgr->userq_mutex entirely from amdgpu_userq_mgr
2. Consistently use adev->userq_mutex across all user queue operations
3. Add proper work flushing in amdgpu_userq_ensure_ev_fence() to prevent races when ensuring eviction fence availability
4. Maintain proper locking order throughout all user queue functions

Quoted 3 lines

The serialization of all user queue operations is acceptable since these are management operations that don't need high parallelism.

Absolutely clear NAK to that.

Quoted 7 lines
Quoted 3 lines

This also serialized eviction handling making it completely useless.

The global mutex should only be used when new clients open up their connection for the first time or closes the last reference and never otherwise.

The problem is that the firmware reset code can affect multiple queues so we need some way to lock all queues during resets so we can properly update their status.

That shouldn't be necessary.

When all queues are affected we should use a sequence number to indicate the queue generation.

When only a subset of queues are affected then we would need to identify the queues and then set exactly those to an error.

So what exactly do we get on information from the FW?

Christian.

Quoted 274 lines

Alex

Quoted 271 lines

Regards,
Christian.

Quoted 266 lines
---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    |  7 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     | 32 ++++++-------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  1 -
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  5 +--
 4 files changed, 16 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +4 -3
index 23d7d0b0d625..d6a07fac7df2 100644
@@ -105,9 +105,10 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
      struct amdgpu_eviction_fence_mgr *evf_mgr = work_to_evf_mgr(work, suspend_work.work);
      struct amdgpu_fpriv *fpriv = evf_mgr_to_fpriv(evf_mgr);
      struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+     struct amdgpu_device *adev = uq_mgr->adev;
      struct amdgpu_eviction_fence *ev_fence;
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
      spin_lock(&evf_mgr->ev_fence_lock);
      ev_fence = evf_mgr->ev_fence;
      if (ev_fence)
@@ -118,13 +119,13 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
 
      amdgpu_userq_evict(uq_mgr, ev_fence);
 
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
      dma_fence_put(&ev_fence->base);
      return;
 
 unlock:
      spin_unlock(&evf_mgr->ev_fence_lock);
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 }
 
 static bool amdgpu_eviction_fence_enable_signaling(struct dma_fence *f)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -23
index b649ac0cedff..af92450ea6eb 100644
@@ -198,17 +198,18 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
                           struct amdgpu_eviction_fence_mgr *evf_mgr)
 {
      struct amdgpu_eviction_fence *ev_fence;
+     struct amdgpu_device *adev = uq_mgr->adev;
 
 retry:
      /* Flush any pending resume work to create ev_fence */
      flush_delayed_work(&uq_mgr->resume_work);
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
      spin_lock(&evf_mgr->ev_fence_lock);
      ev_fence = evf_mgr->ev_fence;
      spin_unlock(&evf_mgr->ev_fence_lock);
      if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) {
-             mutex_unlock(&uq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              /*
               * Looks like there was no pending resume work,
               * add one now to create a valid eviction fence
@@ -362,12 +363,12 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
      int r = 0;
 
      cancel_delayed_work_sync(&uq_mgr->resume_work);
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
 
      queue = amdgpu_userq_find(uq_mgr, queue_id);
      if (!queue) {
              drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n");
-             mutex_unlock(&uq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              return -EINVAL;
      }
      amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
@@ -388,7 +389,7 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
              queue->state = AMDGPU_USERQ_STATE_HUNG;
      }
      amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
      pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -478,7 +479,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
              pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
              return r;
      }
-
      /*
       * There could be a situation that we are creating a new queue while
       * the other queues under this UQ_mgr are suspended. So if there is any
@@ -486,7 +486,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
       *
       * This will also make sure we have a valid eviction fence ready to be used.
       */
-     mutex_lock(&adev->userq_mutex);
      amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
 
      uq_funcs = adev->userq_funcs[args->in.ip_type];
@@ -588,9 +587,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
      kfree(queue_name);
 
      args->out.queue_id = qid;
-
 unlock:
-     mutex_unlock(&uq_mgr->userq_mutex);
      mutex_unlock(&adev->userq_mutex);
 
      return r;
@@ -820,11 +817,12 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 {
      struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
      struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+     struct amdgpu_device *adev = uq_mgr->adev;
      int ret;
 
      flush_delayed_work(&fpriv->evf_mgr.suspend_work);
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
 
      ret = amdgpu_userq_validate_bos(uq_mgr);
      if (ret) {
@@ -839,7 +837,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
      }
 
 unlock:
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 }
 
 static int
@@ -919,7 +917,6 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
                        struct amdgpu_device *adev)
 {
-     mutex_init(&userq_mgr->userq_mutex);
      idr_init_base(&userq_mgr->userq_idr, 1);
      userq_mgr->adev = adev;
      userq_mgr->file = file_priv;
@@ -942,7 +939,6 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
      cancel_delayed_work_sync(&userq_mgr->resume_work);
 
      mutex_lock(&adev->userq_mutex);
-     mutex_lock(&userq_mgr->userq_mutex);
      idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) {
              amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
              amdgpu_userq_unmap_helper(userq_mgr, queue);
@@ -956,9 +952,7 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
              }
      }
      idr_destroy(&userq_mgr->userq_idr);
-     mutex_unlock(&userq_mgr->userq_mutex);
      mutex_unlock(&adev->userq_mutex);
-     mutex_destroy(&userq_mgr->userq_mutex);
 }
 
 int amdgpu_userq_suspend(struct amdgpu_device *adev)
@@ -975,13 +969,11 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)
      mutex_lock(&adev->userq_mutex);
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
              cancel_delayed_work_sync(&uqm->resume_work);
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      r = amdgpu_userq_unmap_helper(uqm, queue);
                      if (r)
                              ret = r;
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1000,13 +992,11 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
 
      mutex_lock(&adev->userq_mutex);
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      r = amdgpu_userq_map_helper(uqm, queue);
                      if (r)
                              ret = r;
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1031,7 +1021,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
      adev->userq_halt_for_enforce_isolation = true;
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
              cancel_delayed_work_sync(&uqm->resume_work);
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
                           (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1041,7 +1030,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                      ret = r;
                      }
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1065,7 +1053,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
              dev_warn(adev->dev, "userq scheduling already started!\n");
      adev->userq_halt_for_enforce_isolation = false;
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
                           (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1075,7 +1062,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                      ret = r;
                      }
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -1
index c027dd916672..2d63308d55c3 100644
@@ -89,7 +89,6 @@ struct amdgpu_userq_funcs {
 /* Usermode queues for gfx */
 struct amdgpu_userq_mgr {
      struct idr                      userq_idr;
-     struct mutex                    userq_mutex;
      struct amdgpu_device            *adev;
      struct delayed_work             resume_work;
      struct list_head                list;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +3 -2
index 95e91d1dc58a..daf3be92a39c 100644
@@ -464,6 +464,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
      u32 *bo_handles_write, num_write_bo_handles;
      u32 *syncobj_handles, num_syncobj_handles;
      u32 *bo_handles_read, num_read_bo_handles;
+     struct amdgpu_device *adev = userq_mgr->adev;
      int r, i, entry, rentry, wentry;
      struct dma_fence *fence;
      struct drm_exec exec;
@@ -557,14 +558,14 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
      /* Create a new fence */
      r = amdgpu_userq_fence_create(queue, userq_fence, wptr, &fence);
      if (r) {
-             mutex_unlock(&userq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              kmem_cache_free(amdgpu_userq_fence_slab, userq_fence);
              goto put_gobj_write;
      }
 
      dma_fence_put(queue->last_fence);
      queue->last_fence = dma_fence_get(fence);
-     mutex_unlock(&userq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT,
                    (num_read_bo_handles + num_write_bo_handles));
#5 lore raw
Quoted 40 lines · Christian König
Quoted 30 lines · Alex Deucher
Quoted 28 lines · Christian König
Quoted 20 lines · Jesse.Zhang

The current user queue implementation uses a dual-mutex scheme with both per-device (adev->userq_mutex) and per-process (uq_mgr->userq_mutex) locking. This overcomplicated design creates potential deadlock scenarios and makes the code harder to maintain.

Simplify the locking by switching entirely to the global adev->userq_mutex for all user queue operations. This approach:
- Eliminates potential deadlocks between the two mutexes
- Serializes all user queue handling at the kernel level
- Provides adequate protection since user queue operations aren't performance-critical paths
- Makes the code more maintainable with a single locking scheme

Key changes:
1. Remove uq_mgr->userq_mutex entirely from amdgpu_userq_mgr
2. Consistently use adev->userq_mutex across all user queue operations
3. Add proper work flushing in amdgpu_userq_ensure_ev_fence() to prevent races when ensuring eviction fence availability
4. Maintain proper locking order throughout all user queue functions

Quoted 3 lines

The serialization of all user queue operations is acceptable since these are management operations that don't need high parallelism.

Absolutely clear NAK to that.

Quoted 7 lines
Quoted 3 lines

This also serialized eviction handling making it completely useless.

The global mutex should only be used when new clients open up their connection for the first time or closes the last reference and never otherwise.

The problem is that the firmware reset code can affect multiple queues so we need some way to lock all queues during resets so we can properly update their status.

Quoted 7 lines

That shouldn't be necessary.

When all queues are affected we should use a sequence number to indicate the queue generation.

When only a subset of queues are affected then we would need to identify the queues and then set exactly those to an error.

So what exactly do we get on information from the FW?

FW gives us a list of doorbells for queues that were hung and reset.

Alex

Quoted 279 lines

Christian.

Quoted 274 lines

Alex

Quoted 271 lines

Regards,
Christian.

Quoted 266 lines
---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    |  7 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     | 32 ++++++-------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  1 -
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  5 +--
 4 files changed, 16 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +4 -3
index 23d7d0b0d625..d6a07fac7df2 100644
@@ -105,9 +105,10 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
      struct amdgpu_eviction_fence_mgr *evf_mgr = work_to_evf_mgr(work, suspend_work.work);
      struct amdgpu_fpriv *fpriv = evf_mgr_to_fpriv(evf_mgr);
      struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+     struct amdgpu_device *adev = uq_mgr->adev;
      struct amdgpu_eviction_fence *ev_fence;
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
      spin_lock(&evf_mgr->ev_fence_lock);
      ev_fence = evf_mgr->ev_fence;
      if (ev_fence)
@@ -118,13 +119,13 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
 
      amdgpu_userq_evict(uq_mgr, ev_fence);
 
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
      dma_fence_put(&ev_fence->base);
      return;
 
 unlock:
      spin_unlock(&evf_mgr->ev_fence_lock);
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 }
 
 static bool amdgpu_eviction_fence_enable_signaling(struct dma_fence *f)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -23
index b649ac0cedff..af92450ea6eb 100644
@@ -198,17 +198,18 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
                           struct amdgpu_eviction_fence_mgr *evf_mgr)
 {
      struct amdgpu_eviction_fence *ev_fence;
+     struct amdgpu_device *adev = uq_mgr->adev;
 
 retry:
      /* Flush any pending resume work to create ev_fence */
      flush_delayed_work(&uq_mgr->resume_work);
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
      spin_lock(&evf_mgr->ev_fence_lock);
      ev_fence = evf_mgr->ev_fence;
      spin_unlock(&evf_mgr->ev_fence_lock);
      if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) {
-             mutex_unlock(&uq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              /*
               * Looks like there was no pending resume work,
               * add one now to create a valid eviction fence
@@ -362,12 +363,12 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
      int r = 0;
 
      cancel_delayed_work_sync(&uq_mgr->resume_work);
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
 
      queue = amdgpu_userq_find(uq_mgr, queue_id);
      if (!queue) {
              drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n");
-             mutex_unlock(&uq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              return -EINVAL;
      }
      amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
@@ -388,7 +389,7 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
              queue->state = AMDGPU_USERQ_STATE_HUNG;
      }
      amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
      pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -478,7 +479,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
              pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
              return r;
      }
-
      /*
       * There could be a situation that we are creating a new queue while
       * the other queues under this UQ_mgr are suspended. So if there is any
@@ -486,7 +486,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
       *
       * This will also make sure we have a valid eviction fence ready to be used.
       */
-     mutex_lock(&adev->userq_mutex);
      amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
 
      uq_funcs = adev->userq_funcs[args->in.ip_type];
@@ -588,9 +587,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
      kfree(queue_name);
 
      args->out.queue_id = qid;
-
 unlock:
-     mutex_unlock(&uq_mgr->userq_mutex);
      mutex_unlock(&adev->userq_mutex);
 
      return r;
@@ -820,11 +817,12 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 {
      struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
      struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+     struct amdgpu_device *adev = uq_mgr->adev;
      int ret;
 
      flush_delayed_work(&fpriv->evf_mgr.suspend_work);
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
 
      ret = amdgpu_userq_validate_bos(uq_mgr);
      if (ret) {
@@ -839,7 +837,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
      }
 
 unlock:
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 }
 
 static int
@@ -919,7 +917,6 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
                        struct amdgpu_device *adev)
 {
-     mutex_init(&userq_mgr->userq_mutex);
      idr_init_base(&userq_mgr->userq_idr, 1);
      userq_mgr->adev = adev;
      userq_mgr->file = file_priv;
@@ -942,7 +939,6 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
      cancel_delayed_work_sync(&userq_mgr->resume_work);
 
      mutex_lock(&adev->userq_mutex);
-     mutex_lock(&userq_mgr->userq_mutex);
      idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) {
              amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
              amdgpu_userq_unmap_helper(userq_mgr, queue);
@@ -956,9 +952,7 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
              }
      }
      idr_destroy(&userq_mgr->userq_idr);
-     mutex_unlock(&userq_mgr->userq_mutex);
      mutex_unlock(&adev->userq_mutex);
-     mutex_destroy(&userq_mgr->userq_mutex);
 }
 
 int amdgpu_userq_suspend(struct amdgpu_device *adev)
@@ -975,13 +969,11 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)
      mutex_lock(&adev->userq_mutex);
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
              cancel_delayed_work_sync(&uqm->resume_work);
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      r = amdgpu_userq_unmap_helper(uqm, queue);
                      if (r)
                              ret = r;
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1000,13 +992,11 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
 
      mutex_lock(&adev->userq_mutex);
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      r = amdgpu_userq_map_helper(uqm, queue);
                      if (r)
                              ret = r;
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1031,7 +1021,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
      adev->userq_halt_for_enforce_isolation = true;
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
              cancel_delayed_work_sync(&uqm->resume_work);
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
                           (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1041,7 +1030,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                      ret = r;
                      }
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1065,7 +1053,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
              dev_warn(adev->dev, "userq scheduling already started!\n");
      adev->userq_halt_for_enforce_isolation = false;
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
                           (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1075,7 +1062,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                      ret = r;
                      }
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -1
index c027dd916672..2d63308d55c3 100644
@@ -89,7 +89,6 @@ struct amdgpu_userq_funcs {
 /* Usermode queues for gfx */
 struct amdgpu_userq_mgr {
      struct idr                      userq_idr;
-     struct mutex                    userq_mutex;
      struct amdgpu_device            *adev;
      struct delayed_work             resume_work;
      struct list_head                list;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +3 -2
index 95e91d1dc58a..daf3be92a39c 100644
@@ -464,6 +464,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
      u32 *bo_handles_write, num_write_bo_handles;
      u32 *syncobj_handles, num_syncobj_handles;
      u32 *bo_handles_read, num_read_bo_handles;
+     struct amdgpu_device *adev = userq_mgr->adev;
      int r, i, entry, rentry, wentry;
      struct dma_fence *fence;
      struct drm_exec exec;
@@ -557,14 +558,14 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
      /* Create a new fence */
      r = amdgpu_userq_fence_create(queue, userq_fence, wptr, &fence);
      if (r) {
-             mutex_unlock(&userq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              kmem_cache_free(amdgpu_userq_fence_slab, userq_fence);
              goto put_gobj_write;
      }
 
      dma_fence_put(queue->last_fence);
      queue->last_fence = dma_fence_get(fence);
-     mutex_unlock(&userq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT,
                    (num_read_bo_handles + num_write_bo_handles));
maintainer
#6 lore raw
Quoted 44 lines · Alex Deucher
Quoted 42 lines · Christian König
Quoted 30 lines · Alex Deucher
Quoted 28 lines · Christian König
Quoted 20 lines · Jesse.Zhang

The current user queue implementation uses a dual-mutex scheme with both per-device (adev->userq_mutex) and per-process (uq_mgr->userq_mutex) locking. This overcomplicated design creates potential deadlock scenarios and makes the code harder to maintain.

Simplify the locking by switching entirely to the global adev->userq_mutex for all user queue operations. This approach:
- Eliminates potential deadlocks between the two mutexes
- Serializes all user queue handling at the kernel level
- Provides adequate protection since user queue operations aren't performance-critical paths
- Makes the code more maintainable with a single locking scheme

Key changes:
1. Remove uq_mgr->userq_mutex entirely from amdgpu_userq_mgr
2. Consistently use adev->userq_mutex across all user queue operations
3. Add proper work flushing in amdgpu_userq_ensure_ev_fence() to prevent races when ensuring eviction fence availability
4. Maintain proper locking order throughout all user queue functions

Quoted 3 lines

The serialization of all user queue operations is acceptable since these are management operations that don't need high parallelism.

Absolutely clear NAK to that.

Quoted 7 lines
Quoted 3 lines

This also serialized eviction handling making it completely useless.

The global mutex should only be used when new clients open up their connection for the first time or closes the last reference and never otherwise.

The problem is that the firmware reset code can affect multiple queues so we need some way to lock all queues during resets so we can properly update their status.

That shouldn't be necessary.

Quoted 7 lines
Quoted 5 lines

When all queues are affected we should use a sequence number to indicate the queue generation.

When only a subset of queues are affected then we would need to identify the queues and then set exactly those to an error.

So what exactly do we get on information from the FW?

FW gives us a list of doorbells for queues that were hung and reset.

Yeah, that's what I feared. In this case having two locks are mandatory or otherwise we will automatically run into deadlocks.

See the uq_mgr->userq_mutex is used to prevent new queues from being created while we validate the BOs and restore the VM page tables after an eviction.

Now validating the BOs and restoring the page tables can obviously wait for the GPU reset to finish.

So if we take the same lock inside the GPU reset we also take outside the GPU reset then that obviously can't work.

My suggestion is to drop adev->userq_mutex and use an xarray instead. The xarray has self contained locking and we would then just need to use xa_lock()/xa_unlock() to update and retrive the queue status.

Christian

Quoted 282 lines

Alex

Quoted 279 lines

Christian.

Quoted 274 lines

Alex

Quoted 271 lines

Regards,
Christian.

Quoted 266 lines
---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    |  7 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c     | 32 ++++++-------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h     |  1 -
 .../gpu/drm/amd/amdgpu/amdgpu_userq_fence.c   |  5 +--
 4 files changed, 16 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +4 -3
index 23d7d0b0d625..d6a07fac7df2 100644
@@ -105,9 +105,10 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
      struct amdgpu_eviction_fence_mgr *evf_mgr = work_to_evf_mgr(work, suspend_work.work);
      struct amdgpu_fpriv *fpriv = evf_mgr_to_fpriv(evf_mgr);
      struct amdgpu_userq_mgr *uq_mgr = &fpriv->userq_mgr;
+     struct amdgpu_device *adev = uq_mgr->adev;
      struct amdgpu_eviction_fence *ev_fence;
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
      spin_lock(&evf_mgr->ev_fence_lock);
      ev_fence = evf_mgr->ev_fence;
      if (ev_fence)
@@ -118,13 +119,13 @@ amdgpu_eviction_fence_suspend_worker(struct work_struct *work)
 
      amdgpu_userq_evict(uq_mgr, ev_fence);
 
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
      dma_fence_put(&ev_fence->base);
      return;
 
 unlock:
      spin_unlock(&evf_mgr->ev_fence_lock);
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 }
 
 static bool amdgpu_eviction_fence_enable_signaling(struct dma_fence *f)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +9 -23
index b649ac0cedff..af92450ea6eb 100644
@@ -198,17 +198,18 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
                           struct amdgpu_eviction_fence_mgr *evf_mgr)
 {
      struct amdgpu_eviction_fence *ev_fence;
+     struct amdgpu_device *adev = uq_mgr->adev;
 
 retry:
      /* Flush any pending resume work to create ev_fence */
      flush_delayed_work(&uq_mgr->resume_work);
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
      spin_lock(&evf_mgr->ev_fence_lock);
      ev_fence = evf_mgr->ev_fence;
      spin_unlock(&evf_mgr->ev_fence_lock);
      if (!ev_fence || dma_fence_is_signaled(&ev_fence->base)) {
-             mutex_unlock(&uq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              /*
               * Looks like there was no pending resume work,
               * add one now to create a valid eviction fence
@@ -362,12 +363,12 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
      int r = 0;
 
      cancel_delayed_work_sync(&uq_mgr->resume_work);
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
 
      queue = amdgpu_userq_find(uq_mgr, queue_id);
      if (!queue) {
              drm_dbg_driver(adev_to_drm(uq_mgr->adev), "Invalid queue id to destroy\n");
-             mutex_unlock(&uq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              return -EINVAL;
      }
      amdgpu_userq_wait_for_last_fence(uq_mgr, queue);
@@ -388,7 +389,7 @@ amdgpu_userq_destroy(struct drm_file *filp, int queue_id)
              queue->state = AMDGPU_USERQ_STATE_HUNG;
      }
      amdgpu_userq_cleanup(uq_mgr, queue, queue_id);
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
      pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
@@ -478,7 +479,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
              pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
              return r;
      }
-
      /*
       * There could be a situation that we are creating a new queue while
       * the other queues under this UQ_mgr are suspended. So if there is any
@@ -486,7 +486,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
       *
       * This will also make sure we have a valid eviction fence ready to be used.
       */
-     mutex_lock(&adev->userq_mutex);
      amdgpu_userq_ensure_ev_fence(&fpriv->userq_mgr, &fpriv->evf_mgr);
 
      uq_funcs = adev->userq_funcs[args->in.ip_type];
@@ -588,9 +587,7 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
      kfree(queue_name);
 
      args->out.queue_id = qid;
-
 unlock:
-     mutex_unlock(&uq_mgr->userq_mutex);
      mutex_unlock(&adev->userq_mutex);
 
      return r;
@@ -820,11 +817,12 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
 {
      struct amdgpu_userq_mgr *uq_mgr = work_to_uq_mgr(work, resume_work.work);
      struct amdgpu_fpriv *fpriv = uq_mgr_to_fpriv(uq_mgr);
+     struct amdgpu_device *adev = uq_mgr->adev;
      int ret;
 
      flush_delayed_work(&fpriv->evf_mgr.suspend_work);
 
-     mutex_lock(&uq_mgr->userq_mutex);
+     mutex_lock(&adev->userq_mutex);
 
      ret = amdgpu_userq_validate_bos(uq_mgr);
      if (ret) {
@@ -839,7 +837,7 @@ static void amdgpu_userq_restore_worker(struct work_struct *work)
      }
 
 unlock:
-     mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 }
 
 static int
@@ -919,7 +917,6 @@ amdgpu_userq_evict(struct amdgpu_userq_mgr *uq_mgr,
 int amdgpu_userq_mgr_init(struct amdgpu_userq_mgr *userq_mgr, struct drm_file *file_priv,
                        struct amdgpu_device *adev)
 {
-     mutex_init(&userq_mgr->userq_mutex);
      idr_init_base(&userq_mgr->userq_idr, 1);
      userq_mgr->adev = adev;
      userq_mgr->file = file_priv;
@@ -942,7 +939,6 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
      cancel_delayed_work_sync(&userq_mgr->resume_work);
 
      mutex_lock(&adev->userq_mutex);
-     mutex_lock(&userq_mgr->userq_mutex);
      idr_for_each_entry(&userq_mgr->userq_idr, queue, queue_id) {
              amdgpu_userq_wait_for_last_fence(userq_mgr, queue);
              amdgpu_userq_unmap_helper(userq_mgr, queue);
@@ -956,9 +952,7 @@ void amdgpu_userq_mgr_fini(struct amdgpu_userq_mgr *userq_mgr)
              }
      }
      idr_destroy(&userq_mgr->userq_idr);
-     mutex_unlock(&userq_mgr->userq_mutex);
      mutex_unlock(&adev->userq_mutex);
-     mutex_destroy(&userq_mgr->userq_mutex);
 }
 
 int amdgpu_userq_suspend(struct amdgpu_device *adev)
@@ -975,13 +969,11 @@ int amdgpu_userq_suspend(struct amdgpu_device *adev)
      mutex_lock(&adev->userq_mutex);
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
              cancel_delayed_work_sync(&uqm->resume_work);
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      r = amdgpu_userq_unmap_helper(uqm, queue);
                      if (r)
                              ret = r;
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1000,13 +992,11 @@ int amdgpu_userq_resume(struct amdgpu_device *adev)
 
      mutex_lock(&adev->userq_mutex);
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      r = amdgpu_userq_map_helper(uqm, queue);
                      if (r)
                              ret = r;
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1031,7 +1021,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
      adev->userq_halt_for_enforce_isolation = true;
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
              cancel_delayed_work_sync(&uqm->resume_work);
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
                           (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1041,7 +1030,6 @@ int amdgpu_userq_stop_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                      ret = r;
                      }
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
@@ -1065,7 +1053,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
              dev_warn(adev->dev, "userq scheduling already started!\n");
      adev->userq_halt_for_enforce_isolation = false;
      list_for_each_entry_safe(uqm, tmp, &adev->userq_mgr_list, list) {
-             mutex_lock(&uqm->userq_mutex);
              idr_for_each_entry(&uqm->userq_idr, queue, queue_id) {
                      if (((queue->queue_type == AMDGPU_HW_IP_GFX) ||
                           (queue->queue_type == AMDGPU_HW_IP_COMPUTE)) &&
@@ -1075,7 +1062,6 @@ int amdgpu_userq_start_sched_for_enforce_isolation(struct amdgpu_device *adev,
                                      ret = r;
                      }
              }
-             mutex_unlock(&uqm->userq_mutex);
      }
      mutex_unlock(&adev->userq_mutex);
      return ret;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.h +0 -1
index c027dd916672..2d63308d55c3 100644
@@ -89,7 +89,6 @@ struct amdgpu_userq_funcs {
 /* Usermode queues for gfx */
 struct amdgpu_userq_mgr {
      struct idr                      userq_idr;
-     struct mutex                    userq_mutex;
      struct amdgpu_device            *adev;
      struct delayed_work             resume_work;
      struct list_head                list;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +3 -2
index 95e91d1dc58a..daf3be92a39c 100644
@@ -464,6 +464,7 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
      u32 *bo_handles_write, num_write_bo_handles;
      u32 *syncobj_handles, num_syncobj_handles;
      u32 *bo_handles_read, num_read_bo_handles;
+     struct amdgpu_device *adev = userq_mgr->adev;
      int r, i, entry, rentry, wentry;
      struct dma_fence *fence;
      struct drm_exec exec;
@@ -557,14 +558,14 @@ int amdgpu_userq_signal_ioctl(struct drm_device *dev, void *data,
      /* Create a new fence */
      r = amdgpu_userq_fence_create(queue, userq_fence, wptr, &fence);
      if (r) {
-             mutex_unlock(&userq_mgr->userq_mutex);
+             mutex_unlock(&adev->userq_mutex);
              kmem_cache_free(amdgpu_userq_fence_slab, userq_fence);
              goto put_gobj_write;
      }
 
      dma_fence_put(queue->last_fence);
      queue->last_fence = dma_fence_get(fence);
-     mutex_unlock(&userq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT,
                    (num_read_bo_handles + num_write_bo_handles));