Skip to contentSkip to conversation

drm/amdgpu: Fix circular locking in userq creation

PATCH Superseded Jesse.Zhang 2 participants 2 messages May 13, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix circular locking in userq creation

A circular locking dependency was detected between the global adev->userq_mutex and per-file userq_mgr->userq_mutex when creating user queues. The issue occurs because:

1. amdgpu_userq_suspend()and amdgpu_userq_resume take adev->userq_mutex first, then userq_mgr->userq_mutex
2. While amdgpu_userq_create() takes them in reverse order

This patch resolves the issue by:
1. Moving the adev->userq_mutex lock earlier in amdgpu_userq_create() to cover the amdgpu_userq_ensure_ev_fence() call
2. Releasing it after we're done with queue creation
3. Maintaining consistent lock ordering (adev->userq_mutex always before userq_mgr->userq_mutex) in all code paths

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -2
index 697dd3cbd114..6ba3c0b14279 100644
@@ -531,6 +531,7 @@ 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];
@@ -594,7 +595,6 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 	}
 
 	/* don't map the queue if scheduling is halted */
-	mutex_lock(&adev->userq_mutex);
 	if (adev->userq_halt_for_enforce_isolation &&
 	    ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
 	     (queue->queue_type == AMDGPU_HW_IP_COMPUTE)))
@@ -613,13 +613,13 @@ amdgpu_userq_create(struct drm_file *filp, union drm_amdgpu_userq *args)
 			goto unlock;
 		}
 	}
-	mutex_unlock(&adev->userq_mutex);
 
 
 	args->out.queue_id = qid;
 
 unlock:
 	mutex_unlock(&uq_mgr->userq_mutex);
+	mutex_unlock(&adev->userq_mutex);
 
 	return r;
 }
Signature · 1 line
2.49.0
#1 lore raw

[Public]

Quoted 52 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Jesse.Zhang
Sent: Tuesday, May 13, 2025 10:21 AM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Koenig, Christian <[email protected]>; Zhang, Jesse(Jie) <[email protected]>
Subject: [PATCH] drm/amdgpu: Fix circular locking in userq creation

A circular locking dependency was detected between the global `adev-

Quoted 1 line

userq_mutex and per-file userq_mgr->userq_mutex` when creating user

queues. The issue occurs because:

1. amdgpu_userq_suspend()and amdgpu_userq_resume take `adev-

Quoted 1 line

userq_mutex` first, then

userq_mgr->userq_mutex
2. While amdgpu_userq_create() takes them in reverse order

This patch resolves the issue by:
1. Moving the adev->userq_mutex lock earlier in amdgpu_userq_create() to cover the amdgpu_userq_ensure_ev_fence() call 2. Releasing it after we're
done with queue creation 3. Maintaining consistent lock ordering (adev-

Quoted 1 line

userq_mutex always before

userq_mgr->userq_mutex) in all code paths

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -0
index 697dd3cbd114..6ba3c0b14279 100644
@@ -531,6 +531,7 @@ 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];
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +0 -1
@@ -594,7 +595,6 @@ amdgpu_userq_create(struct drm_file *filp, union
 drm_amdgpu_userq *args)
      }
 
      /* don't map the queue if scheduling is halted */
-     mutex_lock(&adev->userq_mutex);
      if (adev->userq_halt_for_enforce_isolation &&
          ((queue->queue_type == AMDGPU_HW_IP_GFX) ||
Quoted 6 lines
           (queue->queue_type == AMDGPU_HW_IP_COMPUTE))) @@ -613,13
+613,13 @@ amdgpu_userq_create(struct drm_file *filp, union
drm_amdgpu_userq *args)
                      goto unlock;
              }
      }

In the following userq map error handler also needs to remove unlocking userq_mutex as well.

if (!skip_map_queue) {
->>>>>>>->>>>>>>r = amdgpu_userq_map_helper(uq_mgr, queue); ->>>>>>>->>>>>>>if (r) { ->>>>>>>->>>>>>>->>>>>>>mutex_unlock(&adev->userq_mutex); ->>>>>>>->>>>>>>->>>>>>>drm_file_err(uq_mgr->file, "Failed to map Queue\n"); ->>>>>>>->>>>>>>->>>>>>>idr_remove(&uq_mgr->userq_idr, qid); ->>>>>>>->>>>>>>->>>>>>>amdgpu_userq_fence_driver_free(queue); ->>>>>>>->>>>>>>->>>>>>>uq_funcs->mqd_destroy(uq_mgr, queue); ->>>>>>>->>>>>>>->>>>>>>kfree(queue); ->>>>>>>->>>>>>>->>>>>>>goto unlock;
->>>>>>>->>>>>>>}

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -2
-     mutex_unlock(&adev->userq_mutex);
 
 
      args->out.queue_id = qid;
 
 unlock:
      mutex_unlock(&uq_mgr->userq_mutex);
+     mutex_unlock(&adev->userq_mutex);
 
      return r;
 }
--

2.49.0