Skip to contentSkip to conversation

drm/amdgpu: don't sync the user queue eviction fence

PATCH New Prike Liang 1 participant 3 messages Apr 27, 2025

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

PATCH v2 1/3 drm/amdgpu: don't sync the user queue eviction fence

Don't return and sync the user queue eviction fence; otherwise, the eviction fence will be returned as a dependent fence during VM update and refer to the fence result in leakage.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c | 11 +++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c           |  4 ++++
 3 files changed, 16 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +11 -0
index d86e611a9ff4..6c9b2b43a929 100644
@@ -224,6 +224,17 @@ void amdgpu_eviction_fence_detach(struct amdgpu_eviction_fence_mgr *evf_mgr,
 	dma_fence_put(stub);
 }
 
+bool amdgpu_eviction_fence_valid(struct dma_fence *f)
+{
+
+	if(!f)
+		return false;
+	if (f->ops == &amdgpu_eviction_fence_ops)
+		return true;
+
+	return false;
+}
+
 int amdgpu_eviction_fence_init(struct amdgpu_eviction_fence_mgr *evf_mgr)
 {
 	/* This needs to be done one time per open */
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h +1 -0
index fcd867b7147d..d4e1975cac71 100644
@@ -42,6 +42,7 @@ struct amdgpu_eviction_fence_mgr {
 };
 
 /* Eviction fence helper functions */
+bool amdgpu_eviction_fence_valid(struct dma_fence *f);
 struct amdgpu_eviction_fence *
 amdgpu_eviction_fence_create(struct amdgpu_eviction_fence_mgr *evf_mgr);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c +4 -0
index 5576ed0b508f..d22e6763bfb9 100644
@@ -185,6 +185,10 @@ static bool amdgpu_sync_test_fence(struct amdgpu_device *adev,
 {
 	void *fence_owner = amdgpu_sync_get_owner(f);
 
+	/* don't sync the kgd userq eviction fence*/
+	if(amdgpu_eviction_fence_valid(f))
+		return false;
+
 	/* Always sync to moves, no matter what */
 	if (fence_owner == AMDGPU_FENCE_OWNER_UNDEFINED)
 		return true;
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 2/3 drm/amdgpu: fix the eviction fence dereference

The dma_resv_add_fence() already refers to the added fence. So when attaching the evciton fence to the gem bo, it needn't refer to it anymore.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +2 -5
index 6c9b2b43a929..7a5f02ef45a7 100644
@@ -189,7 +189,6 @@ void amdgpu_eviction_fence_destroy(struct amdgpu_eviction_fence_mgr *evf_mgr)
 int amdgpu_eviction_fence_attach(struct amdgpu_eviction_fence_mgr *evf_mgr,
 				 struct amdgpu_bo *bo)
 {
-	struct dma_fence *ef;
 	struct amdgpu_eviction_fence *ev_fence;
 	struct dma_resv *resv = bo->tbo.base.resv;
 	int ret;
@@ -205,10 +204,8 @@ int amdgpu_eviction_fence_attach(struct amdgpu_eviction_fence_mgr *evf_mgr,
 
 	spin_lock(&evf_mgr->ev_fence_lock);
 	ev_fence = evf_mgr->ev_fence;
-	if (ev_fence) {
-		ef = dma_fence_get(&ev_fence->base);
-		dma_resv_add_fence(resv, ef, DMA_RESV_USAGE_BOOKKEEP);
-	}
+	if (ev_fence)
+		dma_resv_add_fence(resv, &ev_fence->base, DMA_RESV_USAGE_BOOKKEEP);
 	spin_unlock(&evf_mgr->ev_fence_lock);
 
 	return 0;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 3/3 drm/amdgpu: validate the eviction fence before attaching/detaching

In reply to Prike Liang

Before the user queue BOs resume workqueue is scheduled; there's no valid eviction fence to attach the gem obj. For this case, it doesn't need to attach/detach the eviction fence. Also, it needs to unlock the bo first before returning from the eviction fence attached error.

---
 .../gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c   |  3 +++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c          | 16 ++++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +3 -0
index 7a5f02ef45a7..aa9f523e62c0 100644
@@ -216,6 +216,9 @@ void amdgpu_eviction_fence_detach(struct amdgpu_eviction_fence_mgr *evf_mgr,
 {
 	struct dma_fence *stub = dma_fence_get_stub();
 
+	if (dma_fence_is_signaled(&evf_mgr->ev_fence->base))
+		return;
+
 	dma_resv_replace_fences(bo->tbo.base.resv, evf_mgr->ev_fence_ctx,
 				stub, DMA_RESV_USAGE_BOOKKEEP);
 	dma_fence_put(stub);
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10 -6
index f03fc3cf4d50..11d4a9af7bc5 100644
@@ -294,11 +294,14 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
 	else
 		++bo_va->ref_count;
 
-	/* attach gfx eviction fence */
-	r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
-	if (r) {
-		DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
-		return r;
+	/* attach gfx the validated eviction fence */
+	if (!IS_ERR_OR_NULL(fpriv->evf_mgr.ev_fence)) {
+		r = amdgpu_eviction_fence_attach(&fpriv->evf_mgr, abo);
+		if (r) {
+			DRM_DEBUG_DRIVER("Failed to attach eviction fence to BO\n");
+			amdgpu_bo_unreserve(abo);
+			return r;
+		}
 	}
 
 	amdgpu_bo_unreserve(abo);
@@ -364,7 +367,8 @@ static void amdgpu_gem_object_close(struct drm_gem_object *obj,
 			goto out_unlock;
 	}
 
-	if (!amdgpu_vm_is_bo_always_valid(vm, bo))
+	if (!amdgpu_vm_is_bo_always_valid(vm, bo) &&
+			!IS_ERR_OR_NULL(fpriv->evf_mgr.ev_fence))
 		amdgpu_eviction_fence_detach(&fpriv->evf_mgr, bo);
 
 	bo_va = amdgpu_vm_bo_find(vm, bo);
Signature · 1 line
2.34.1