Skip to contentSkip to conversation

drm/amdgpu: fix userq eviction-fence rearm reservation

PATCH In review [email protected] 2 participants 5 messages Sep 17

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

cover drm/amdgpu: fix userq eviction-fence rearm reservation

From: Vitaly Prosyak <[email protected]>

During user queue restore, the eviction-fence rearm path can attach a replacement BOOKKEEP fence after the BO reservation has been unlocked. Because dma_resv_unlock() discards unused pre-reserved capacity, a BO with no remaining fences can reach dma_resv_add_fence() with max_fences equal to zero. This triggers the dma-resv BUG_ON at dma-resv.c:319 and can crash amdgpu_userq_restore_worker.

The first two patches revert earlier caller-side reservations. The root PD reservation and the WPTR-caller reservation are no longer required once the slot is reserved at the operation that actually attaches the replacement eviction fence. Removing them avoids retaining redundant fence capacity and keeps the reservation accounting local to the rearm path.

The third patch reserves one fence slot in amdgpu_evf_mgr_rearm(), immediately before amdgpu_evf_mgr_attach_fence() is called for each locked BO. The BO is already held by the drm_exec context, so the reservation remains valid for dma_resv_add_fence(). The reservation is kept explicit in the rearm path rather than hidden in the attach helper.

The fix was validated with repeated amd_deadlock runs, including the userqueue restore reproducer.

Vitaly Prosyak (3):
Revert "drm/amdgpu: reserve root PD fence slots for userq
eviction-fence rearm"
Revert "drm/amdgpu: reserve eviction-fence slot at WPTR caller" drm/amdgpu: Reserve eviction fence slot during userq rearm

 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c | 8 ++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c          | 6 +-----
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c         | 3 +--
 3 files changed, 10 insertions(+), 7 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/3 Revert "drm/amdgpu: reserve root PD fence slots for userq eviction-fence rearm"

From: Vitaly Prosyak <[email protected]>

The root PD reservation is no longer needed after reserving the eviction-fence slot at the userq rearm attach site. The rearm path now reserves capacity immediately before attaching the replacement BOOKKEEP fence, where the reservation lifetime and the locked BO are known. Keeping the caller-side root PD reservation would retain unnecessary fence capacity and duplicate the protection.

This reverts commit 00af92666f29 ("drm/amdgpu: reserve root PD fence slots for userq eviction-fence rearm").

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Prike Liang <[email protected]>
Change-Id I98c1c9b096ec03fe8e27eda867ca1836abbe6322
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -5
index 5d07320a29b2..d83953a1c076 100644
@@ -1043,11 +1043,7 @@ amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)
 retry_lock:
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
-		/*
-		 * Rearm adds one BOOKKEEP fence and validation may queue move fences
-		 * on the root PD BO, so reserve caller-side slots accordingly.
-		 */
-		ret = amdgpu_vm_lock_pd(vm, &exec, TTM_NUM_MOVE_FENCES + 1);
+		ret = amdgpu_vm_lock_pd(vm, &exec, 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto unlock_all;
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/3 Revert "drm/amdgpu: reserve eviction-fence slot at WPTR caller"

In reply to [email protected]

From: Vitaly Prosyak <[email protected]>

The WPTR caller no longer needs to reserve an eviction-fence slot. The reservation is now performed in amdgpu_evf_mgr_rearm() immediately before the replacement BOOKKEEP fence is attached. That is the operation that can add the fence after the BO reservation was unlocked, so the caller-side reservation is unnecessary and should be removed.

This reverts commit 118037152820 ("drm/amdgpu: reserve eviction-fence slot at WPTR caller").

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Prike Liang <[email protected]>
Change-Id I6158879300c8dba05e8e56da5573b8ab46c1d2c0
---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +1 -2
index fe49aa4dc5ff..68beccee9fc0 100644
@@ -60,8 +60,7 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 		}
 
 		obj = wptr_mapping->bo_va->base.bo;
-		ret = drm_exec_prepare_obj(&exec, &obj->tbo.base,
-					   TTM_NUM_MOVE_FENCES + 1);
+		ret = drm_exec_lock_obj(&exec, &obj->tbo.base);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto fail_lock;
Signature · 1 line
2.55.0
#3 lore raw

PATCH 3/3 drm/amdgpu: Reserve eviction fence slot during userq rearm

In reply to [email protected]

From: Vitaly Prosyak <[email protected]>

A user queue restore can rearm an eviction fence after the BO reservation has been unlocked. Since dma_resv_unlock() discards unused pre-reserved capacity, a BO with no remaining fences can reach rearm with max_fences equal to zero. Reserve one fence slot immediately before attaching the replacement BOOKKEEP eviction fence. The BO is already held by the drm_exec context at this point, so the reservation remains valid for dma_resv_add_fence(). Keep the reservation in the rearm path rather than hiding it in amdgpu_evf_mgr_attach_fence(), so the caller-side accounting remains explicit.
This fixes the dma-resv BUG_ON at dma-resv.c:319 and the resulting crash in amdgpu_userq_restore_worker. The earlier root-PD and WPTR-caller reservations are reverted by the preceding commits because they do not cover this reservation lifetime. Validated with repeated amd_deadlock runs, including the userqueue restore reproducer.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Prike Liang <[email protected]>
Change-Id Id156389c1b2193fc3e1354acd5194f756faf39c9
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +8 -0
index a0802012de49..6c1f994ea78b 100644
@@ -125,6 +125,7 @@ int amdgpu_evf_mgr_rearm(struct amdgpu_eviction_fence_mgr *evf_mgr,
 	struct amdgpu_eviction_fence *ev_fence;
 	struct drm_gem_object *obj;
 	unsigned long index;
+	int ret;
 
 	/* Create and initialize a new eviction fence */
 	ev_fence = kzalloc_obj(*ev_fence);
@@ -146,6 +147,13 @@ int amdgpu_evf_mgr_rearm(struct amdgpu_eviction_fence_mgr *evf_mgr,
 	drm_exec_for_each_locked_object(exec, index, obj) {
 		struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
 
+		ret = dma_resv_reserve_fences(bo->tbo.base.resv, 1);
+		if (ret) {
+                    pr_err("userq evf rearm attach failed index=%lu bo=%p ret=%d\n",
+                           index, bo, ret);
+                    return ret;
+                }
+
 		amdgpu_evf_mgr_attach_fence(evf_mgr, bo);
 	}
 	return 0;
Signature · 1 line
2.55.0
maintainer
#4 lore raw

Re: [PATCH 3/3] drm/amdgpu: Reserve eviction fence slot during userq rearm

Quoted 36 lines · On 9/17/26 17:32, [email protected] wrote:

From: Vitaly Prosyak <[email protected]>

A user queue restore can rearm an eviction fence after the BO reservation has been unlocked. Since dma_resv_unlock() discards unused pre-reserved capacity, a BO with no remaining fences can reach rearm with max_fences equal to zero. Reserve one fence slot immediately before attaching the replacement BOOKKEEP eviction fence. The BO is already held by the drm_exec context at this point, so the reservation remains valid for dma_resv_add_fence(). Keep the reservation in the rearm path rather than hiding it in amdgpu_evf_mgr_attach_fence(), so the caller-side accounting remains explicit.
This fixes the dma-resv BUG_ON at dma-resv.c:319 and the resulting crash in amdgpu_userq_restore_worker. The earlier root-PD and WPTR-caller reservations are reverted by the preceding commits because they do not cover this reservation lifetime. Validated with repeated amd_deadlock runs, including the userqueue restore reproducer.

Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Prike Liang <[email protected]>
Change-Id Id156389c1b2193fc3e1354acd5194f756faf39c9
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +1 -0
index a0802012de49..6c1f994ea78b 100644
@@ -125,6 +125,7 @@ int amdgpu_evf_mgr_rearm(struct amdgpu_eviction_fence_mgr *evf_mgr,
 	struct amdgpu_eviction_fence *ev_fence;
 	struct drm_gem_object *obj;
 	unsigned long index;
+	int ret;
 
 	/* Create and initialize a new eviction fence */
 	ev_fence = kzalloc_obj(*ev_fence);

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c int amdgpu_evf_mgr_rearm(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 150

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +1 -0
@@ -146,6 +147,13 @@ int amdgpu_evf_mgr_rearm(struct amdgpu_eviction_fence_mgr *evf_mgr,
 	drm_exec_for_each_locked_object(exec, index, obj) {
 		struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
 
+		ret = dma_resv_reserve_fences(bo->tbo.base.resv, 1);

That doesn't work like this.

The problem is that dma_resv_reserve_fences() allocates memory and that in turn can invalidate the eviction fence we just created again.

The patches you want to revert actually look correct to me. The fence slots usually needs to be reserved directly after we locked the BOs.

Regards,
Christian.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +6 -0
+		if (ret) {
+                    pr_err("userq evf rearm attach failed index=%lu bo=%p ret=%d\n",
+                           index, bo, ret);
+                    return ret;
+                }
+
 		amdgpu_evf_mgr_attach_fence(evf_mgr, bo);
 	}
 	return 0;