Skip to contentSkip to conversation

drm/amdgpu: reserve eviction-fence slot at WPTR caller

PATCH In review [email protected] 3 participants 5 messages Sep 10

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

PATCH 1/2 drm/amdgpu: reserve eviction-fence slot at WPTR caller

From: Vitaly Prosyak <[email protected]>

Root cause analysis showed that the previous workaround was fixing the symptom in the wrong layer.

Problem:
kernel BUG at drivers/dma-buf/dma-resv.c:319!
BUG_ON(fobj->num_fences >= fobj->max_fences)

  Workqueue: events amdgpu_userq_restore_worker

- amdgpu_evf_mgr_attach_fence() had been temporarily taught to reserve/add fence bookkeeping internally as a workaround.
- This hid the real contract violation and made reservation policy implicit inside a helper.

Review clarification:
- The helper-side dma_resv_add_fence-related workaround was only an intended proposal sent for review discussion, not the final direction.
- Final fix keeps fence-slot reservation explicit in callers and leaves the helper free of policy decisions.

Findings from investigation:
- drm_exec_prepare_obj() both locks and reserves fence slots.
- drm_exec_lock_obj() only locks and does not reserve slots.
- Existing attach_fence callers in the GEM/userq restore flows already reserve TTM_NUM_MOVE_FENCES + 1 before adding the bookkeeping eviction fence.
- The WPTR mapping path in mes_userq_create_wptr_mapping() used drm_exec_lock_obj() and then called amdgpu_evf_mgr_attach_fence(), so this specific path could add a fence without pre-reserved space.

Fix:
- Switch the WPTR mapping path to drm_exec_prepare_obj(..., TTM_NUM_MOVE_FENCES + 1), restoring the caller-side reservation contract.
- Remove the helper-side reservation workaround from amdgpu_evf_mgr_attach_fence().

Why this is the right layer:
- Reservation policy stays explicit at call sites that define how many fences may be added in that execution context.
- The helper only performs validate + add_fence when called with a properly prepared reservation object, matching the existing drm_exec model.

Fixes 91a358fe99d86 (drm/amdgpu: dont pin wptr bo instead use eviction fence)
Cc Sunil Khatri <[email protected]>
Cc Prike Liang <[email protected]>
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Change-Id Ibb2ec1f210a8c6e5d2a48ed9b8ec77c6dabddb7e
---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -1
index 68beccee9fc0..fe49aa4dc5ff 100644
@@ -60,7 +60,8 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 		}
 
 		obj = wptr_mapping->bo_va->base.bo;
-		ret = drm_exec_lock_obj(&exec, &obj->tbo.base);
+		ret = drm_exec_prepare_obj(&exec, &obj->tbo.base,
+					   TTM_NUM_MOVE_FENCES + 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto fail_lock;
Signature · 1 line
2.54.0
#1 lore raw

PATCH 2/2 drm/amdgpu: reserve root PD fence slots for userq eviction-fence rearm

From: Vitaly Prosyak <[email protected]>

Root cause:
- In userq restore/rearm, root PD was locked with amdgpu_vm_lock_pd(vm, &exec, 1).
- Later in the same drm_exec context, amdgpu_evf_mgr_rearm() attaches BOOKKEEP eviction fences to all locked BOs.
- In this flow, root PD validation/update can also consume move-fence slots, so reserving only +1 at PD lock time is not sufficient under stress.

Fix:
- Reserve caller-side slots at the actual source by switching to:
amdgpu_vm_lock_pd(vm, &exec, TTM_NUM_MOVE_FENCES + 1)

This keeps reservation policy in callers, consistent with Christian Konig feedback and with amdgpu_gem_object_open() where callers reserve TTM_NUM_MOVE_FENCES + 1 before attaching eviction fences.

Credit:
- Christian Konig: confirmed fence-slot reservation belongs to callers and asked to investigate why +1 was still not enough.
- Prike Liang: clarified the initial variant was workaround-only and developed that initial version independently.

Cc Prike Liang <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +5 -1
index 3938f7b8ec4b..12fbff02e4b5 100644
@@ -1043,7 +1043,11 @@ 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) {
-		ret = amdgpu_vm_lock_pd(vm, &exec, 1);
+		/*
+		 * 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);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto unlock_all;
Signature · 1 line
2.54.0
#2 lore raw

RE: [PATCH 2/2] drm/amdgpu: reserve root PD fence slots for userq eviction-fence rearm

AMD General

Regards,
Prike

Quoted 59 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of [email protected]
Sent: Thursday, September 10, 2026 8:46 AM
To: [email protected]
Cc: Prosyak, Vitaly <[email protected]>; Liang, Prike <[email protected]>; Khatri, Sunil <[email protected]>; Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>
Subject: [PATCH 2/2] drm/amdgpu: reserve root PD fence slots for userq eviction- fence rearm

From: Vitaly Prosyak <[email protected]>

Root cause:
- In userq restore/rearm, root PD was locked with amdgpu_vm_lock_pd(vm, &exec, 1).
- Later in the same drm_exec context, amdgpu_evf_mgr_rearm() attaches BOOKKEEP eviction fences to all locked BOs.
- In this flow, root PD validation/update can also consume move-fence slots, so reserving only +1 at PD lock time is not sufficient under stress.

Fix:
- Reserve caller-side slots at the actual source by switching to:
amdgpu_vm_lock_pd(vm, &exec, TTM_NUM_MOVE_FENCES + 1)

This keeps reservation policy in callers, consistent with Christian Konig feedback and with amdgpu_gem_object_open() where callers reserve TTM_NUM_MOVE_FENCES + 1 before attaching eviction fences.

Credit:
- Christian Konig: confirmed fence-slot reservation belongs to callers and asked to investigate why +1 was still not enough.
- Prike Liang: clarified the initial variant was workaround-only and developed that initial version independently.

Cc Prike Liang <[email protected]>
Cc Sunil Khatri <[email protected]>
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +2 -1
index 3938f7b8ec4b..12fbff02e4b5 100644
@@ -1043,7 +1043,11 @@ 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) {
-             ret = amdgpu_vm_lock_pd(vm, &exec, 1);
+             /*
+              * Rearm adds one BOOKKEEP fence and validation may queue

move fences

drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c amdgpu_userq_vm_validate_and_restore_queue(struct amdgpu_userq_mgr *uq_mgr)

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -0
+              * on the root PD BO, so reserve caller-side slots accordingly.
+              */
+             ret = amdgpu_vm_lock_pd(vm, &exec, TTM_NUM_MOVE_FENCES
+ 1);

IIRC, the PD reservation fence slots had been corrected a while ago on the drm-next, could you please further check on the latest drm-next branch?

Quoted 5 lines
              drm_exec_retry_on_contention(&exec);
              if (unlikely(ret))
                      goto unlock_all;

--
2.54.0

#3 lore raw
In reply to [email protected]

AMD General

With this fix, my following patch#13 could be dropped.

drm/amdgpu: allocate dma_fence slot explicitly for rearming eviction fence

The patch is Reviewed-by: Prike Liang <[email protected]>

Regards,
Prike

Quoted 82 lines

-----Original Message-----
From: [email protected] <[email protected]>
Sent: Thursday, September 10, 2026 8:46 AM
To: [email protected]
Cc: Prosyak, Vitaly <[email protected]>; Khatri, Sunil <[email protected]>; Liang, Prike <[email protected]>; Koenig, Christian <[email protected]>; Deucher, Alexander <[email protected]>
Subject: [PATCH 1/2] drm/amdgpu: reserve eviction-fence slot at WPTR caller

From: Vitaly Prosyak <[email protected]>

Root cause analysis showed that the previous workaround was fixing the symptom in the wrong layer.

Problem:
kernel BUG at drivers/dma-buf/dma-resv.c:319!
BUG_ON(fobj->num_fences >= fobj->max_fences)

  Workqueue: events amdgpu_userq_restore_worker

- amdgpu_evf_mgr_attach_fence() had been temporarily taught to reserve/add fence bookkeeping internally as a workaround.
- This hid the real contract violation and made reservation policy implicit inside a helper.

Review clarification:
- The helper-side dma_resv_add_fence-related workaround was only an intended proposal sent for review discussion, not the final direction.
- Final fix keeps fence-slot reservation explicit in callers and leaves the helper free of policy decisions.

Findings from investigation:
- drm_exec_prepare_obj() both locks and reserves fence slots.
- drm_exec_lock_obj() only locks and does not reserve slots.
- Existing attach_fence callers in the GEM/userq restore flows already reserve TTM_NUM_MOVE_FENCES + 1 before adding the bookkeeping eviction fence.
- The WPTR mapping path in mes_userq_create_wptr_mapping() used drm_exec_lock_obj() and then called amdgpu_evf_mgr_attach_fence(), so this specific path could add a fence without pre-reserved space.

Fix:
- Switch the WPTR mapping path to drm_exec_prepare_obj(..., TTM_NUM_MOVE_FENCES + 1), restoring the caller-side reservation contract.
- Remove the helper-side reservation workaround from amdgpu_evf_mgr_attach_fence().

Why this is the right layer:
- Reservation policy stays explicit at call sites that define how many fences may be added in that execution context.
- The helper only performs validate + add_fence when called with a properly prepared reservation object, matching the existing drm_exec model.

Fixes 91a358fe99d86 (drm/amdgpu: dont pin wptr bo instead use eviction fence)
Cc Sunil Khatri <[email protected]>
Cc Prike Liang <[email protected]>
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Change-Id Ibb2ec1f210a8c6e5d2a48ed9b8ec77c6dabddb7e
---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -1
index 68beccee9fc0..fe49aa4dc5ff 100644
@@ -60,7 +60,8 @@ mes_userq_create_wptr_mapping(struct amdgpu_device
 *adev,
              }
 
              obj = wptr_mapping->bo_va->base.bo;
-             ret = drm_exec_lock_obj(&exec, &obj->tbo.base);
+             ret = drm_exec_prepare_obj(&exec, &obj->tbo.base,
+                                        TTM_NUM_MOVE_FENCES + 1);
              drm_exec_retry_on_contention(&exec);
              if (unlikely(ret))
                      goto fail_lock;

--
2.54.0

maintainer Reviewed-by on v1 1/2
#4 lore raw
In reply to [email protected]
Quoted 42 lines · On 9/10/26 02:45, [email protected] wrote:

From: Vitaly Prosyak <[email protected]>

Root cause analysis showed that the previous workaround was fixing the symptom in the wrong layer.

Problem:
kernel BUG at drivers/dma-buf/dma-resv.c:319!
BUG_ON(fobj->num_fences >= fobj->max_fences)

  Workqueue: events amdgpu_userq_restore_worker

- amdgpu_evf_mgr_attach_fence() had been temporarily taught to reserve/add fence bookkeeping internally as a workaround.
- This hid the real contract violation and made reservation policy implicit inside a helper.

Review clarification:
- The helper-side dma_resv_add_fence-related workaround was only an intended proposal sent for review discussion, not the final direction.
- Final fix keeps fence-slot reservation explicit in callers and leaves the helper free of policy decisions.

Findings from investigation:
- drm_exec_prepare_obj() both locks and reserves fence slots.
- drm_exec_lock_obj() only locks and does not reserve slots.
- Existing attach_fence callers in the GEM/userq restore flows already reserve TTM_NUM_MOVE_FENCES + 1 before adding the bookkeeping eviction fence.
- The WPTR mapping path in mes_userq_create_wptr_mapping() used drm_exec_lock_obj() and then called amdgpu_evf_mgr_attach_fence(), so this specific path could add a fence without pre-reserved space.

Fix:
- Switch the WPTR mapping path to drm_exec_prepare_obj(..., TTM_NUM_MOVE_FENCES + 1), restoring the caller-side reservation contract.
- Remove the helper-side reservation workaround from amdgpu_evf_mgr_attach_fence().

Why this is the right layer:
- Reservation policy stays explicit at call sites that define how many fences may be added in that execution context.
- The helper only performs validate + add_fence when called with a properly prepared reservation object, matching the existing drm_exec model.

Quoted 8 lines
Fixes 91a358fe99d86 (drm/amdgpu: dont pin wptr bo instead use eviction fence)
Cc Sunil Khatri <[email protected]>
Cc Prike Liang <[email protected]>
Cc Christian König <[email protected]>
Cc Alex Deucher <[email protected]>
Change-Id Ibb2ec1f210a8c6e5d2a48ed9b8ec77c6dabddb7e

Could be that Prike is right and patch #2 is already fixed, but if it isn't feel free to add my rb to that one as well.

Thanks,
Christian.

Quoted 18 lines
---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +2 -1
index 68beccee9fc0..fe49aa4dc5ff 100644
@@ -60,7 +60,8 @@ mes_userq_create_wptr_mapping(struct amdgpu_device *adev,
 		}
 
 		obj = wptr_mapping->bo_va->base.bo;
-		ret = drm_exec_lock_obj(&exec, &obj->tbo.base);
+		ret = drm_exec_prepare_obj(&exec, &obj->tbo.base,
+					   TTM_NUM_MOVE_FENCES + 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto fail_lock;