Skip to contentSkip to conversation

drm/amdgpu: implicitly sync the dependent read fences

PATCH In review Prike Liang 2 participants 5 messages Apr 23, 2025

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

PATCH 1/2 drm/amdgpu: implicitly sync the dependent read fences

The driver doesn't want to sync on the DMA_RESV_USAGE_BOOKKEEP usage fences, so here only return and sync the dependent read fences.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_sync.c +2 -3
index 5576ed0b508f..4e1d30ecb6cc 100644
@@ -249,9 +249,8 @@ int amdgpu_sync_resv(struct amdgpu_device *adev, struct amdgpu_sync *sync,
 
 	if (resv == NULL)
 		return -EINVAL;
-
-	/* TODO: Use DMA_RESV_USAGE_READ here */
-	dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, f) {
+	/*Only return and sync the fences of usage <= DMA_RESV_USAGE_READ.*/
+	dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_READ, f) {
 		dma_fence_chain_for_each(f, f) {
 			struct dma_fence *tmp = dma_fence_chain_contained(f);
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/amdgpu: properly release the eviction fences

The following cases require releasing and deferring the eviction fences properly

1) Detach the old eviction fences before attaching a new one.
2) Drop the eviction fence init reference.
3) Correct the attached eviction fence reference.
4) Free the eviction fence when the attached BOs are released.

---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    | 53 +++++++++++++++----
 .../drm/amd/amdgpu/amdgpu_eviction_fence.h    |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  1 +
 3 files changed, 45 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +42 -11
index d86e611a9ff4..a47db865c530 100644
@@ -70,11 +70,6 @@ amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 		return -ENOMEM;
 	}
 
-	/* Update the eviction fence now */
-	spin_lock(&evf_mgr->ev_fence_lock);
-	old_ef = evf_mgr->ev_fence;
-	evf_mgr->ev_fence = new_ef;
-	spin_unlock(&evf_mgr->ev_fence_lock);
 
 	/* Attach the new fence */
 	drm_exec_for_each_locked_object(exec, index, obj) {
@@ -82,6 +77,16 @@ amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 
 		if (!bo)
 			continue;
+
+		/*detach the old eviction fence first*/
+		amdgpu_eviction_fence_detach(evf_mgr, bo);
+
+		/* Update the eviction fence now */
+		spin_lock(&evf_mgr->ev_fence_lock);
+		old_ef = evf_mgr->ev_fence;
+		evf_mgr->ev_fence = new_ef;
+		spin_unlock(&evf_mgr->ev_fence_lock);
+
 		ret = amdgpu_eviction_fence_attach(evf_mgr, bo);
 		if (ret) {
 			DRM_ERROR("Failed to attch new eviction fence\n");
@@ -89,9 +94,10 @@ amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 		}
 	}
 
-	/* Free old fence */
+	/* Free the init eviction fence which was referred by the dma_init*/
 	if (old_ef)
 		dma_fence_put(&old_ef->base);
+
 	return 0;
 
 free_err:
@@ -189,7 +195,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 +210,12 @@ 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);
-	}
+	/*
+	 * The dma_resv_add_fence() already refer to the added fence, hence in this
+	 * placement needn't refer to the armed fence anymore.
+	 */
+	if (ev_fence)
+		dma_resv_add_fence(resv, &ev_fence->base, DMA_RESV_USAGE_BOOKKEEP);
 	spin_unlock(&evf_mgr->ev_fence_lock);
 
 	return 0;
@@ -224,6 +231,30 @@ void amdgpu_eviction_fence_detach(struct amdgpu_eviction_fence_mgr *evf_mgr,
 	dma_fence_put(stub);
 }
 
+void amdgpu_remove_all_eviction_fences(struct amdgpu_bo *bo)
+{
+	struct dma_resv *resv = &bo->tbo.base._resv;
+	struct dma_fence *fence, *stub;
+	struct dma_resv_iter cursor;
+
+	dma_resv_assert_held(resv);
+
+	stub = dma_fence_get_stub();
+	dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, fence) {
+		struct amdgpu_eviction_fence *ev_fence;
+
+		ev_fence = fence_to_evf(fence);
+		if (!ev_fence || !dma_fence_is_signaled(&ev_fence->base))
+			continue;
+
+		dma_resv_replace_fences(resv, fence->context, stub,
+				DMA_RESV_USAGE_BOOKKEEP);
+
+	}
+
+	dma_fence_put(stub);
+}
+
 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 +2 -0
index fcd867b7147d..7e6c55a334e0 100644
@@ -42,6 +42,7 @@ struct amdgpu_eviction_fence_mgr {
 };
 
 /* Eviction fence helper functions */
+#define fence_to_evf(f)  container_of(f, struct amdgpu_eviction_fence, base)
 struct amdgpu_eviction_fence *
 amdgpu_eviction_fence_create(struct amdgpu_eviction_fence_mgr *evf_mgr);
 
@@ -66,4 +67,5 @@ amdgpu_eviction_fence_signal(struct amdgpu_eviction_fence_mgr *evf_mgr,
 int
 amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 				    struct drm_exec *exec);
+void amdgpu_remove_all_eviction_fences(struct amdgpu_bo *bo);
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
index dbe57996a481..6d3eacffbcb9 100644
@@ -1322,6 +1322,7 @@ void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)
 	amdgpu_vram_mgr_set_cleared(bo->resource);
 	dma_resv_add_fence(&bo->base._resv, fence, DMA_RESV_USAGE_KERNEL);
 	dma_fence_put(fence);
+	amdgpu_remove_all_eviction_fences(abo);
 
 out:
 	dma_resv_unlock(&bo->base._resv);
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: properly release the eviction fences

Quoted 20 lines · Prike Liang

The following cases require releasing and deferring the eviction fences properly

1) Detach the old eviction fences before attaching a new one.
2) Drop the eviction fence init reference.
3) Correct the attached eviction fence reference.
4) Free the eviction fence when the attached BOs are released.

---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    | 53 +++++++++++++++----
 .../drm/amd/amdgpu/amdgpu_eviction_fence.h    |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  1 +
 3 files changed, 45 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
index d86e611a9ff4..a47db865c530 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 77

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -5
@@ -70,11 +70,6 @@ amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 		return -ENOMEM;
 	}
 
-	/* Update the eviction fence now */
-	spin_lock(&evf_mgr->ev_fence_lock);
-	old_ef = evf_mgr->ev_fence;
-	evf_mgr->ev_fence = new_ef;
-	spin_unlock(&evf_mgr->ev_fence_lock);

That is certainly incorrect. The new fence *must* be made public before attaching it to the existing BOs.

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 82

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +3 -0
 	/* Attach the new fence */
 	drm_exec_for_each_locked_object(exec, index, obj) {
@@ -82,6 +77,16 @@ amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 
 		if (!bo)
 			continue;
+
+		/*detach the old eviction fence first*/
+		amdgpu_eviction_fence_detach(evf_mgr, bo);

That's completely nonsense. The old eviction fence is signaled at this state and doesn't need to be detached.

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +7 -0
+
+		/* Update the eviction fence now */
+		spin_lock(&evf_mgr->ev_fence_lock);
+		old_ef = evf_mgr->ev_fence;
+		evf_mgr->ev_fence = new_ef;
+		spin_unlock(&evf_mgr->ev_fence_lock);
+
 		ret = amdgpu_eviction_fence_attach(evf_mgr, bo);
 		if (ret) {
 			DRM_ERROR("Failed to attch new eviction fence\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 99

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +1 -1
@@ -89,9 +94,10 @@ amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 		}
 	}
 
-	/* Free old fence */
+	/* Free the init eviction fence which was referred by the dma_init*/
 	if (old_ef)
 		dma_fence_put(&old_ef->base);

The comment isn't really helpful. Additional to that it's valid to call dma_fence_put() with a NULL fence to we can drop the extra if check.

Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +1 -1
+
 	return 0;
 
 free_err:
@@ -189,7 +195,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 +210,12 @@ int amdgpu_eviction_fence_attach(struct amdgpu_eviction_fence_mgr *evf_mgr,
 

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +4 -4
 	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);
-	}
+	/*
+	 * The dma_resv_add_fence() already refer to the added fence, hence in this
+	 * placement needn't refer to the armed fence anymore.
+	 */

Please drop that comment.

Quoted 59 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +26 -0
+	if (ev_fence)
+		dma_resv_add_fence(resv, &ev_fence->base, DMA_RESV_USAGE_BOOKKEEP);
 	spin_unlock(&evf_mgr->ev_fence_lock);
 
 	return 0;
@@ -224,6 +231,30 @@ void amdgpu_eviction_fence_detach(struct amdgpu_eviction_fence_mgr *evf_mgr,
 	dma_fence_put(stub);
 }
 
+void amdgpu_remove_all_eviction_fences(struct amdgpu_bo *bo)
+{
+	struct dma_resv *resv = &bo->tbo.base._resv;
+	struct dma_fence *fence, *stub;
+	struct dma_resv_iter cursor;
+
+	dma_resv_assert_held(resv);
+
+	stub = dma_fence_get_stub();
+	dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, fence) {
+		struct amdgpu_eviction_fence *ev_fence;
+
+		ev_fence = fence_to_evf(fence);
+		if (!ev_fence || !dma_fence_is_signaled(&ev_fence->base))
+			continue;
+
+		dma_resv_replace_fences(resv, fence->context, stub,
+				DMA_RESV_USAGE_BOOKKEEP);
+
+	}
+
+	dma_fence_put(stub);
+}
+
 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 +2 -0
index fcd867b7147d..7e6c55a334e0 100644
@@ -42,6 +42,7 @@ struct amdgpu_eviction_fence_mgr {
 };
 
 /* Eviction fence helper functions */
+#define fence_to_evf(f)  container_of(f, struct amdgpu_eviction_fence, base)
 struct amdgpu_eviction_fence *
 amdgpu_eviction_fence_create(struct amdgpu_eviction_fence_mgr *evf_mgr);
 
@@ -66,4 +67,5 @@ amdgpu_eviction_fence_signal(struct amdgpu_eviction_fence_mgr *evf_mgr,
 int
 amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,
 				    struct drm_exec *exec);
+void amdgpu_remove_all_eviction_fences(struct amdgpu_bo *bo);
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index dbe57996a481..6d3eacffbcb9 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_release_notify(struct ttm_buffer_object *bo) · line 1325

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
@@ -1322,6 +1322,7 @@ void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)
 	amdgpu_vram_mgr_set_cleared(bo->resource);
 	dma_resv_add_fence(&bo->base._resv, fence, DMA_RESV_USAGE_KERNEL);
 	dma_fence_put(fence);
+	amdgpu_remove_all_eviction_fences(abo);

That's a really bad idea. We only did that for the KFD eviction fence because we didn't had any other choice.

For the user queue eviction fence we should rather do that in amdgpu_gem_object_close() e.g. when amdgpu_vm_bo_del() is called.

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
 out:
 	dma_resv_unlock(&bo->base._resv);
#3 lore raw

RE: [PATCH 2/2] drm/amdgpu: properly release the eviction fences

[Public]

Quoted 39 lines

From: Koenig, Christian <[email protected]>
Sent: Wednesday, April 23, 2025 9:58 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: properly release the eviction fences

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct

Quoted 21 lines · Prike Liang

The following cases require releasing and deferring the eviction fences properly

1) Detach the old eviction fences before attaching a new one.
2) Drop the eviction fence init reference.
3) Correct the attached eviction fence reference.
4) Free the eviction fence when the attached BOs are released.

---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    | 53 +++++++++++++++----
 .../drm/amd/amdgpu/amdgpu_eviction_fence.h    |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  1 +
 3 files changed, 45 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
index d86e611a9ff4..a47db865c530 100644
@@ -70,11 +70,6 @@ amdgpu_eviction_fence_replace_fence(struct

amdgpu_eviction_fence_mgr *evf_mgr,

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 77

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -5
            return -ENOMEM;
    }
 
-   /* Update the eviction fence now */
-   spin_lock(&evf_mgr->ev_fence_lock);
-   old_ef = evf_mgr->ev_fence;
-   evf_mgr->ev_fence = new_ef;
-   spin_unlock(&evf_mgr->ev_fence_lock);
Quoted 2 lines

That is certainly incorrect. The new fence *must* be made public before attaching it to the existing BOs.

Here the new eviction fence also will be updated in the evf_mgr before attaching it to the vm hold BOs, but it looks much better as before to cache and update the eviction fence outside of the VM hold BOs iterations attached fences.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
    /* Attach the new fence */
    drm_exec_for_each_locked_object(exec, index, obj) { @@ -82,6 +77,16

@@ amdgpu_eviction_fence_replace_fence(struct
amdgpu_eviction_fence_mgr *evf_mgr,

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 82

Quoted 5 lines
            if (!bo)
                    continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +3 -0
+
+           /*detach the old eviction fence first*/
+           amdgpu_eviction_fence_detach(evf_mgr, bo);

That's completely nonsense. The old eviction fence is signaled at this state and doesn't need to be detached.

By using the amdgpu_eviction_fence_detach want to drop the old eviction fence which added the Bos before, so instead of confusing here and drop the old eviction of the BO directly before attach a new fence?

Quoted 123 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +7 -0
+
+           /* Update the eviction fence now */
+           spin_lock(&evf_mgr->ev_fence_lock);
+           old_ef = evf_mgr->ev_fence;
+           evf_mgr->ev_fence = new_ef;
+           spin_unlock(&evf_mgr->ev_fence_lock);
+
            ret = amdgpu_eviction_fence_attach(evf_mgr, bo);
            if (ret) {
                    DRM_ERROR("Failed to attch new eviction fence\n"); @@ -

89,9 +94,10

Quoted 1 line

@@ amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr

*evf_mgr,

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 99

Quoted 7 lines
            }
    }
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +1 -1
-   /* Free old fence */
+   /* Free the init eviction fence which was referred by the dma_init*/
    if (old_ef)
            dma_fence_put(&old_ef->base);

The comment isn't really helpful. Additional to that it's valid to call dma_fence_put() with a NULL fence to we can drop the extra if check.

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +1 -0
+
    return 0;
 
 free_err:
@@ -189,7 +195,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,

Quoted 7 lines

struct amdgpu_bo *bo)
{

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -1
-   struct dma_fence *ef;
    struct amdgpu_eviction_fence *ev_fence;
    struct dma_resv *resv = bo->tbo.base.resv;
    int ret;

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c int amdgpu_eviction_fence_attach(struct · line 215

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +2 -4
@@ -205,10 +210,12 @@ 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);
-   }
+   /*
+    * The dma_resv_add_fence() already refer to the added fence, hence in

this

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +2 -0
+    * placement needn't refer to the armed fence anymore.
+    */

Please drop that comment.

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c int amdgpu_eviction_fence_attach(struct · line 219

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +2 -0
+   if (ev_fence)
+           dma_resv_add_fence(resv, &ev_fence->base,

DMA_RESV_USAGE_BOOKKEEP);

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c void amdgpu_eviction_fence_detach(struct

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
    spin_unlock(&evf_mgr->ev_fence_lock);
 
    return 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
@@ -224,6 +231,30 @@ void amdgpu_eviction_fence_detach(struct

amdgpu_eviction_fence_mgr *evf_mgr,

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c void amdgpu_eviction_fence_detach(struct amdgpu_eviction_fence_mgr *evf_mgr,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +9 -0
    dma_fence_put(stub);
 }
 
+void amdgpu_remove_all_eviction_fences(struct amdgpu_bo *bo) {
+   struct dma_resv *resv = &bo->tbo.base._resv;
+   struct dma_fence *fence, *stub;
+   struct dma_resv_iter cursor;
+
+   dma_resv_assert_held(resv);
+
+   stub = dma_fence_get_stub();
+   dma_resv_for_each_fence(&cursor, resv,

DMA_RESV_USAGE_BOOKKEEP, fence) {

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +14 -0
+           struct amdgpu_eviction_fence *ev_fence;
+
+           ev_fence = fence_to_evf(fence);
+           if (!ev_fence || !dma_fence_is_signaled(&ev_fence->base))
+                   continue;
+
+           dma_resv_replace_fences(resv, fence->context, stub,
+                           DMA_RESV_USAGE_BOOKKEEP);
+
+   }
+
+   dma_fence_put(stub);
+}
+
 int amdgpu_eviction_fence_init(struct amdgpu_eviction_fence_mgr
 *evf_mgr)  {
    /* This needs to be done one time per open */ diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h index fcd867b7147d..7e6c55a334e0 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h +2 -0
@@ -42,6 +42,7 @@ struct amdgpu_eviction_fence_mgr {  };
 
 /* Eviction fence helper functions */
+#define fence_to_evf(f)  container_of(f, struct
+amdgpu_eviction_fence, base)
 struct amdgpu_eviction_fence *
 amdgpu_eviction_fence_create(struct amdgpu_eviction_fence_mgr
 *evf_mgr);
@@ -66,4 +67,5 @@ amdgpu_eviction_fence_signal(struct
Quoted 2 lines

amdgpu_eviction_fence_mgr *evf_mgr, int amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr

*evf_mgr,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_release_notify(struct

Quoted 9 lines
                                struct drm_exec *exec);
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h +1 -0
+void amdgpu_remove_all_eviction_fences(struct amdgpu_bo *bo);
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index dbe57996a481..6d3eacffbcb9 100644
@@ -1322,6 +1322,7 @@ void amdgpu_bo_release_notify(struct

ttm_buffer_object *bo)

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_release_notify(struct · line 1323

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
    amdgpu_vram_mgr_set_cleared(bo->resource);
    dma_resv_add_fence(&bo->base._resv, fence,

DMA_RESV_USAGE_KERNEL);

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_release_notify(struct ttm_buffer_object *bo) · line 1325

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
    dma_fence_put(fence);
+   amdgpu_remove_all_eviction_fences(abo);

That's a really bad idea. We only did that for the KFD eviction fence because we didn't had any other choice.

For the user queue eviction fence we should rather do that in amdgpu_gem_object_close() e.g. when amdgpu_vm_bo_del() is called.

Thanks for the suggestion, will further check it here.

Thanks,
Prike

Quoted 6 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
 out:
    dma_resv_unlock(&bo->base._resv);
maintainer
#4 lore raw

Re: [PATCH 2/2] drm/amdgpu: properly release the eviction fences

Quoted 41 lines · Prike

[Public]

Quoted 39 lines

From: Koenig, Christian <[email protected]>
Sent: Wednesday, April 23, 2025 9:58 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: properly release the eviction fences

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct

Quoted 21 lines · Prike Liang

The following cases require releasing and deferring the eviction fences properly

1) Detach the old eviction fences before attaching a new one.
2) Drop the eviction fence init reference.
3) Correct the attached eviction fence reference.
4) Free the eviction fence when the attached BOs are released.

---
 .../drm/amd/amdgpu/amdgpu_eviction_fence.c    | 53 +++++++++++++++----
 .../drm/amd/amdgpu/amdgpu_eviction_fence.h    |  2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  1 +
 3 files changed, 45 insertions(+), 11 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
index d86e611a9ff4..a47db865c530 100644
@@ -70,11 +70,6 @@ amdgpu_eviction_fence_replace_fence(struct

amdgpu_eviction_fence_mgr *evf_mgr,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -5
            return -ENOMEM;
    }
 
-   /* Update the eviction fence now */
-   spin_lock(&evf_mgr->ev_fence_lock);
-   old_ef = evf_mgr->ev_fence;
-   evf_mgr->ev_fence = new_ef;
-   spin_unlock(&evf_mgr->ev_fence_lock);
Quoted 3 lines
Quoted 2 lines

That is certainly incorrect. The new fence *must* be made public before attaching it to the existing BOs.

Here the new eviction fence also will be updated in the evf_mgr before attaching it to the vm hold BOs, but it looks much better as before to cache and update the eviction fence outside of the VM hold BOs iterations attached fences.

Well I don't care if that looks better. This is simply technically incorrect.

It's a must to switch out the old eviction fence for the new one or otherwise somebody could try to attach the old one to newly allocated BOs while we are attaching the new one to the existing BOs.

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
    /* Attach the new fence */
    drm_exec_for_each_locked_object(exec, index, obj) { @@ -82,6 +77,16

@@ amdgpu_eviction_fence_replace_fence(struct
amdgpu_eviction_fence_mgr *evf_mgr,

            if (!bo)
                    continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +3 -0
+
+           /*detach the old eviction fence first*/
+           amdgpu_eviction_fence_detach(evf_mgr, bo);
Quoted 4 lines
Quoted 2 lines

That's completely nonsense. The old eviction fence is signaled at this state and doesn't need to be detached.

By using the amdgpu_eviction_fence_detach want to drop the old eviction fence which added the Bos before, so instead of confusing here and drop the old eviction of the BO directly before attach a new fence?

You don't need to drop the old fence. See the old fence is already signaled and just waiting for cleanup. Trying to replace that will do exactly nothing.

Regards,
Christian.

Quoted 145 lines
Quoted 124 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr,

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +7 -0
+
+           /* Update the eviction fence now */
+           spin_lock(&evf_mgr->ev_fence_lock);
+           old_ef = evf_mgr->ev_fence;
+           evf_mgr->ev_fence = new_ef;
+           spin_unlock(&evf_mgr->ev_fence_lock);
+
            ret = amdgpu_eviction_fence_attach(evf_mgr, bo);
            if (ret) {
                    DRM_ERROR("Failed to attch new eviction fence\n"); @@ -

89,9 +94,10

Quoted 1 line

@@ amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr

*evf_mgr,

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr *evf_mgr, · line 99

Quoted 7 lines
            }
    }
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +1 -1
-   /* Free old fence */
+   /* Free the init eviction fence which was referred by the dma_init*/
    if (old_ef)
            dma_fence_put(&old_ef->base);

The comment isn't really helpful. Additional to that it's valid to call dma_fence_put() with a NULL fence to we can drop the extra if check.

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +1 -0
+
    return 0;
 
 free_err:
@@ -189,7 +195,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,

Quoted 7 lines

struct amdgpu_bo *bo)
{

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -1
-   struct dma_fence *ef;
    struct amdgpu_eviction_fence *ev_fence;
    struct dma_resv *resv = bo->tbo.base.resv;
    int ret;

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c int amdgpu_eviction_fence_attach(struct · line 215

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +2 -4
@@ -205,10 +210,12 @@ 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);
-   }
+   /*
+    * The dma_resv_add_fence() already refer to the added fence, hence in

this

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +2 -0
+    * placement needn't refer to the armed fence anymore.
+    */

Please drop that comment.

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c int amdgpu_eviction_fence_attach(struct · line 219

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +2 -0
+   if (ev_fence)
+           dma_resv_add_fence(resv, &ev_fence->base,

DMA_RESV_USAGE_BOOKKEEP);

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c void amdgpu_eviction_fence_detach(struct

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
    spin_unlock(&evf_mgr->ev_fence_lock);
 
    return 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +0 -0
@@ -224,6 +231,30 @@ void amdgpu_eviction_fence_detach(struct

amdgpu_eviction_fence_mgr *evf_mgr,

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c void amdgpu_eviction_fence_detach(struct amdgpu_eviction_fence_mgr *evf_mgr,

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +9 -0
    dma_fence_put(stub);
 }
 
+void amdgpu_remove_all_eviction_fences(struct amdgpu_bo *bo) {
+   struct dma_resv *resv = &bo->tbo.base._resv;
+   struct dma_fence *fence, *stub;
+   struct dma_resv_iter cursor;
+
+   dma_resv_assert_held(resv);
+
+   stub = dma_fence_get_stub();
+   dma_resv_for_each_fence(&cursor, resv,

DMA_RESV_USAGE_BOOKKEEP, fence) {

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.c +14 -0
+           struct amdgpu_eviction_fence *ev_fence;
+
+           ev_fence = fence_to_evf(fence);
+           if (!ev_fence || !dma_fence_is_signaled(&ev_fence->base))
+                   continue;
+
+           dma_resv_replace_fences(resv, fence->context, stub,
+                           DMA_RESV_USAGE_BOOKKEEP);
+
+   }
+
+   dma_fence_put(stub);
+}
+
 int amdgpu_eviction_fence_init(struct amdgpu_eviction_fence_mgr
 *evf_mgr)  {
    /* This needs to be done one time per open */ diff --git

a/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h index fcd867b7147d..7e6c55a334e0 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h +2 -0
@@ -42,6 +42,7 @@ struct amdgpu_eviction_fence_mgr {  };
 
 /* Eviction fence helper functions */
+#define fence_to_evf(f)  container_of(f, struct
+amdgpu_eviction_fence, base)
 struct amdgpu_eviction_fence *
 amdgpu_eviction_fence_create(struct amdgpu_eviction_fence_mgr
 *evf_mgr);
@@ -66,4 +67,5 @@ amdgpu_eviction_fence_signal(struct
Quoted 2 lines

amdgpu_eviction_fence_mgr *evf_mgr, int amdgpu_eviction_fence_replace_fence(struct amdgpu_eviction_fence_mgr

*evf_mgr,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_release_notify(struct

Quoted 9 lines
                                struct drm_exec *exec);
drivers/gpu/drm/amd/amdgpu/amdgpu_eviction_fence.h +1 -0
+void amdgpu_remove_all_eviction_fences(struct amdgpu_bo *bo);
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
index dbe57996a481..6d3eacffbcb9 100644
@@ -1322,6 +1322,7 @@ void amdgpu_bo_release_notify(struct

ttm_buffer_object *bo)

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_release_notify(struct · line 1323

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
    amdgpu_vram_mgr_set_cleared(bo->resource);
    dma_resv_add_fence(&bo->base._resv, fence,

DMA_RESV_USAGE_KERNEL);

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c void amdgpu_bo_release_notify(struct ttm_buffer_object *bo) · line 1325

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
    dma_fence_put(fence);
+   amdgpu_remove_all_eviction_fences(abo);

That's a really bad idea. We only did that for the KFD eviction fence because we didn't had any other choice.

For the user queue eviction fence we should rather do that in amdgpu_gem_object_close() e.g. when amdgpu_vm_bo_del() is called.

Thanks for the suggestion, will further check it here.

Thanks,
Prike

Quoted 6 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
 out:
    dma_resv_unlock(&bo->base._resv);