amdgpu_trap_alloc() first acquires reservation_ww_class_mutex via
drm_exec, and then calls amdgpu_bo_create_kernel() while still holding
that lock. amdgpu_bo_create_kernel() internally calls
amdgpu_bo_create_reserved() which tries to acquire the same
reservation_ww_class_mutex again — causing a recursive lock and
triggering a lockdep deadlock warning on Navi44:

  WARNING: possible recursive locking detected
  6.19.0-kfd-root #38 Not tainted
  --------------------------------------------
  kworker/0:2/959 is trying to acquire lock:
  ffff8bb17995d9e8 (reservation_ww_class_mutex){+.+.}-{4:4}, at: 
amdgpu_bo_create_reserved+0xd2/0x2a0 [amdgpu]

  but task is already holding lock:
  ffffcc23c3397ba0 (reservation_ww_class_mutex){+.+.}-{4:4}, at: 
amdgpu_trap_alloc+0xac/0x230 [amdgpu]

  Possible unsafe locking scenario:
        CPU0
        ----
   lock(reservation_ww_class_mutex);
   lock(reservation_ww_class_mutex);

   *** DEADLOCK ***

  Call Trace:
   amdgpu_bo_create_kernel+0x1a/0x80 [amdgpu]
   amdgpu_trap_alloc+0x18d/0x230 [amdgpu]
   amdgpu_driver_open_kms+0x2f1/0x390 [amdgpu]
   drm_file_alloc+0x20a/0x2d0 [drm]
   drm_client_init+0x75/0x100 [drm]
   amdgpu_amdkfd_drm_client_create+0x53/0x90 [amdgpu]
   amdgpu_pci_probe+0x463/0x630 [amdgpu]

Fix this by creating the TMA buffer object before drm_exec is
initialized, so the lock is not held during BO creation. Then lock
the TMA BO together with the TBA BO inside the drm_exec section as
required for the subsequent map operations.

Fixes: c50a45b2e98d ("drm/amdgpu: Add cwsr functions")
Cc: Lijo Lazar <[email protected]>
Cc: Christian König <[email protected]>
Cc: Alex Deucher <[email protected]>
Signed-off-by: Srinivasan Shanmugam <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c | 32 ++++++++++++++++--------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c 
b/drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c
index 31f653ec3fb1..3981e9447a26 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c
@@ -390,40 +390,50 @@ int amdgpu_trap_alloc(struct amdgpu_device *adev, struct 
amdgpu_vm *vm,
        if (!cwsr)
                return -ENOMEM;
 
+       /*
+        * Create TMA BO before acquiring drm_exec locks to avoid recursive
+        * ww_mutex acquisition. amdgpu_bo_create_kernel() calls
+        * amdgpu_bo_create_reserved() which acquires 
reservation_ww_class_mutex,
+        * but drm_exec already holds it — causing a lockdep deadlock warning.
+        */
+       r = amdgpu_bo_create_kernel(adev, AMDGPU_TRAP_TMA_MAX_SIZE, PAGE_SIZE,
+                                   AMDGPU_GEM_DOMAIN_GTT, &cwsr->tma_bo, NULL,
+                                   &cwsr->tma_cpu_addr);
+       if (r)
+               goto err;
+
        bo = adev->trap_info->isa_bo;
        drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
        drm_exec_until_all_locked(&exec) {
                r = amdgpu_vm_lock_pd(vm, &exec, 0);
                if (likely(!r))
                        r = drm_exec_lock_obj(&exec, &bo->tbo.base);
+               if (likely(!r))
+                       r = drm_exec_lock_obj(&exec, &cwsr->tma_bo->tbo.base);
                drm_exec_retry_on_contention(&exec);
                if (unlikely(r)) {
                        dev_err(adev->dev,
                                "failed to reserve for CWSR allocs: err=%d\n",
                                r);
-                       goto err;
+                       goto err_exec;
                }
        }
 
-       r = amdgpu_bo_create_kernel(adev, AMDGPU_TRAP_TMA_MAX_SIZE, PAGE_SIZE,
-                                   AMDGPU_GEM_DOMAIN_GTT, &cwsr->tma_bo, NULL,
-                                   &cwsr->tma_cpu_addr);
-       if (r)
-               goto err;
-
        r = amdgpu_trap_map_region(adev, vm, cwsr, AMDGPU_TRAP_TMA);
        if (r)
-               goto err;
+               goto err_exec;
        r = amdgpu_trap_map_region(adev, vm, cwsr, AMDGPU_TRAP_TBA);
        if (r) {
                amdgpu_trap_unmap_region(adev, cwsr, AMDGPU_TRAP_TMA);
-               goto err;
+               goto err_exec;
        }
 
-err:
+err_exec:
        drm_exec_fini(&exec);
-       if (r) {
+       if (r)
                amdgpu_bo_free_kernel(&cwsr->tma_bo, NULL, NULL);
+err:
+       if (r) {
                kfree(cwsr);
                *trap_obj = NULL;
        } else {
-- 
2.34.1

Reply via email to