Skip to contentSkip to conversation

drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

PATCH In review Prike Liang 2 participants 10 messages May 26 – Jun 2

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

PATCH 1/3 drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

Avoid xarray extraction and temporary array allocation in amdgpu_userq_fence_alloc() when there are no pending wait-side fence driver references. This keeps the common fence emit path cheaper and efficient.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +4 -2
index 008330a0d852..2a2bf13a513d 100644
@@ -226,7 +226,7 @@ static int amdgpu_userq_fence_alloc(struct amdgpu_usermode_queue *userq,
 	struct amdgpu_userq_fence *userq_fence;
 	void *entry;
 
-	userq_fence = kmalloc(sizeof(*userq_fence), GFP_KERNEL);
+	userq_fence = kzalloc(sizeof(*userq_fence), GFP_KERNEL);
 	if (!userq_fence)
 		return -ENOMEM;
 
@@ -235,6 +235,8 @@ static int amdgpu_userq_fence_alloc(struct amdgpu_usermode_queue *userq,
 	 * used as size to allocate the array.
 	 */
 	mutex_lock(&userq->fence_drv_lock);
+	if (xa_empty(&userq->fence_drv_xa))
+		goto unlock;
 	XA_STATE(xas, &userq->fence_drv_xa, 0);
 
 	rcu_read_lock();
@@ -256,7 +258,7 @@ static int amdgpu_userq_fence_alloc(struct amdgpu_usermode_queue *userq,
 	xa_extract(&userq->fence_drv_xa, (void **)userq_fence->fence_drv_array,
 		   0, ULONG_MAX, xas.xa_index, XA_PRESENT);
 	xa_destroy(&userq->fence_drv_xa);
-
+unlock:
 	mutex_unlock(&userq->fence_drv_lock);
 
 	amdgpu_userq_fence_driver_get(fence_drv);
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/3 drm/amdgpu: only flush restore work for rearming the eviction fence

The userq signal path calls amdgpu_userq_ensure_ev_fence() for every emitted userq fence. That helper unconditionally flushed resume_work even when the current eviction fence was already valid and unsignaled. This adds expensive workqueue synchronization to the normal submit path.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -3
index 38e310a8694d..f650d8d0ef53 100644
@@ -412,8 +412,6 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
 	struct dma_fence *ev_fence;
 
 retry:
-	/* Flush any pending resume work to create ev_fence */
-	flush_delayed_work(&uq_mgr->resume_work);
 
 	mutex_lock(&uq_mgr->userq_mutex);
 	ev_fence = amdgpu_evf_mgr_get_fence(evf_mgr);
@@ -422,9 +420,12 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
 		mutex_unlock(&uq_mgr->userq_mutex);
 		/*
 		 * Looks like there was no pending resume work,
-		 * add one now to create a valid eviction fence
+		 * add one now to create a valid eviction fence.
+		 * Only flush the restore worker when the current eviction
+		 * fence has already signaled and a new one needs to be rearmed.
 		 */
 		schedule_delayed_work(&uq_mgr->resume_work, 0);
+		flush_delayed_work(&uq_mgr->resume_work);
 		goto retry;
 	}
 	dma_fence_put(ev_fence);
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH 2/3] drm/amdgpu: only flush restore work for rearming the eviction fence

Quoted 4 lines · Prike Liang

The userq signal path calls amdgpu_userq_ensure_ev_fence() for every emitted userq fence. That helper unconditionally flushed resume_work even when the current eviction fence was already valid and unsignaled. This adds expensive workqueue synchronization to the normal submit path.

Clear NAK. That handling is completely intentional like that and doesn't add any additional overhead.

Regards,
Christian.

Quoted 33 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +4 -3
index 38e310a8694d..f650d8d0ef53 100644
@@ -412,8 +412,6 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
 	struct dma_fence *ev_fence;
 
 retry:
-	/* Flush any pending resume work to create ev_fence */
-	flush_delayed_work(&uq_mgr->resume_work);
 
 	mutex_lock(&uq_mgr->userq_mutex);
 	ev_fence = amdgpu_evf_mgr_get_fence(evf_mgr);
@@ -422,9 +420,12 @@ amdgpu_userq_ensure_ev_fence(struct amdgpu_userq_mgr *uq_mgr,
 		mutex_unlock(&uq_mgr->userq_mutex);
 		/*
 		 * Looks like there was no pending resume work,
-		 * add one now to create a valid eviction fence
+		 * add one now to create a valid eviction fence.
+		 * Only flush the restore worker when the current eviction
+		 * fence has already signaled and a new one needs to be rearmed.
 		 */
 		schedule_delayed_work(&uq_mgr->resume_work, 0);
+		flush_delayed_work(&uq_mgr->resume_work);
 		goto retry;
 	}
 	dma_fence_put(ev_fence);
#3 lore raw

PATCH 3/3 drm/amdgpu: reserve TTM move fences slot for rearming eviction fences

In reply to Prike Liang

The eviction rearming does not cover possible TTM move fences. If TTM moves the BO and consumes move fence slots, the later eviction fence add can hit the dma_resv_add_fence() BUG.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c   | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
index ea743407dd06..22ec4eb6e17f 100644
@@ -249,7 +249,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
-		r = drm_exec_prepare_obj(&exec, &abo->tbo.base, 1);
+		r = drm_exec_prepare_obj(&exec, &abo->tbo.base, TTM_NUM_MOVE_FENCES + 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(r))
 			goto out_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
index f650d8d0ef53..39b188848927 100644
@@ -978,7 +978,7 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)
 		if (unlikely(ret))
 			goto unlock_all;
 
-		ret = amdgpu_vm_lock_individual(vm, &exec, 1);
+		ret = amdgpu_vm_lock_individual(vm, &exec, TTM_NUM_MOVE_FENCES + 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto unlock_all;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 519352378b9f..51ddd267da63 100644
@@ -473,7 +473,7 @@ int amdgpu_vm_lock_individual(struct amdgpu_vm *vm, struct drm_exec *exec,
 			amdgpu_bo_ref(bo);
 			spin_unlock(&vm->individual_lock);
 
-			ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 1);
+			ret = drm_exec_prepare_obj(exec, &bo->tbo.base, num_fences);
 			amdgpu_bo_unref(&bo);
 			if (unlikely(ret))
 				return ret;
Signature · 1 line
2.34.1
maintainer
#4 lore raw

Re: [PATCH 3/3] drm/amdgpu: reserve TTM move fences slot for rearming eviction fences

Quoted 5 lines · Prike Liang

The eviction rearming does not cover possible TTM move fences. If TTM moves the BO and consumes move fence slots, the later eviction fence add can hit the dma_resv_add_fence() BUG.

Good catch, Reviewed-by: Christian König <[email protected]>

Quoted 45 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c   | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c    | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +1 -1
index ea743407dd06..22ec4eb6e17f 100644
@@ -249,7 +249,7 @@ static int amdgpu_gem_object_open(struct drm_gem_object *obj,
 
 	drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
 	drm_exec_until_all_locked(&exec) {
-		r = drm_exec_prepare_obj(&exec, &abo->tbo.base, 1);
+		r = drm_exec_prepare_obj(&exec, &abo->tbo.base, TTM_NUM_MOVE_FENCES + 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(r))
 			goto out_unlock;
drivers/gpu/drm/amd/amdgpu/amdgpu_userq.c +1 -1
index f650d8d0ef53..39b188848927 100644
@@ -978,7 +978,7 @@ amdgpu_userq_vm_validate(struct amdgpu_userq_mgr *uq_mgr)
 		if (unlikely(ret))
 			goto unlock_all;
 
-		ret = amdgpu_vm_lock_individual(vm, &exec, 1);
+		ret = amdgpu_vm_lock_individual(vm, &exec, TTM_NUM_MOVE_FENCES + 1);
 		drm_exec_retry_on_contention(&exec);
 		if (unlikely(ret))
 			goto unlock_all;
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -1
index 519352378b9f..51ddd267da63 100644
@@ -473,7 +473,7 @@ int amdgpu_vm_lock_individual(struct amdgpu_vm *vm, struct drm_exec *exec,
 			amdgpu_bo_ref(bo);
 			spin_unlock(&vm->individual_lock);
 
-			ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 1);
+			ret = drm_exec_prepare_obj(exec, &bo->tbo.base, num_fences);
 			amdgpu_bo_unref(&bo);
 			if (unlikely(ret))
 				return ret;
maintainer
#5 lore raw
In reply to Prike Liang
Quoted 4 lines · Prike Liang

Avoid xarray extraction and temporary array allocation in amdgpu_userq_fence_alloc() when there are no pending wait-side fence driver references. This keeps the common fence emit path cheaper and efficient.

That's an absolute corner case we clearly don't need to optimize for.

In almost all cases we should have at least one remote fence driver here.

Regards,
Christian.

Quoted 37 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +4 -2
index 008330a0d852..2a2bf13a513d 100644
@@ -226,7 +226,7 @@ static int amdgpu_userq_fence_alloc(struct amdgpu_usermode_queue *userq,
 	struct amdgpu_userq_fence *userq_fence;
 	void *entry;
 
-	userq_fence = kmalloc(sizeof(*userq_fence), GFP_KERNEL);
+	userq_fence = kzalloc(sizeof(*userq_fence), GFP_KERNEL);
 	if (!userq_fence)
 		return -ENOMEM;
 
@@ -235,6 +235,8 @@ static int amdgpu_userq_fence_alloc(struct amdgpu_usermode_queue *userq,
 	 * used as size to allocate the array.
 	 */
 	mutex_lock(&userq->fence_drv_lock);
+	if (xa_empty(&userq->fence_drv_xa))
+		goto unlock;
 	XA_STATE(xas, &userq->fence_drv_xa, 0);
 
 	rcu_read_lock();
@@ -256,7 +258,7 @@ static int amdgpu_userq_fence_alloc(struct amdgpu_usermode_queue *userq,
 	xa_extract(&userq->fence_drv_xa, (void **)userq_fence->fence_drv_array,
 		   0, ULONG_MAX, xas.xa_index, XA_PRESENT);
 	xa_destroy(&userq->fence_drv_xa);
-
+unlock:
 	mutex_unlock(&userq->fence_drv_lock);
 
 	amdgpu_userq_fence_driver_get(fence_drv);
#6 lore raw

AMD General

Regards,
Prike

Quoted 11 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, May 26, 2026 6:48 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 1/3] drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

On 5/26/26 11:32, Prike Liang wrote:

Quoted 8 lines
Quoted 4 lines

Avoid xarray extraction and temporary array allocation in amdgpu_userq_fence_alloc() when there are no pending wait-side fence driver references. This keeps the common fence emit path cheaper and efficient.

That's an absolute corner case we clearly don't need to optimize for.

In almost all cases we should have at least one remote fence driver here.

When only the desktop compositor is running, there're many no-wait fences are generated while emitting userq fences. Repeatedly attempting to extract the wait fence array takes more than 10µs (with a maximum cost of around 30µs). Additionally, zero-initializing the userq fence allocation can help reduce overhead in the userq fence put routine.

This patch can return a userq fence driver even when falling back from an empty fence_drv_xa, benefiting on reducing the latency of userq fence driver extraction and free operations when there is no pending wait-side fence.

Quoted 44 lines

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 12 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0 -0
index 008330a0d852..2a2bf13a513d 100644
@@ -226,7 +226,7 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
    struct amdgpu_userq_fence *userq_fence;
    void *entry;
 
-   userq_fence = kmalloc(sizeof(*userq_fence), GFP_KERNEL);
+   userq_fence = kzalloc(sizeof(*userq_fence), GFP_KERNEL);
    if (!userq_fence)
            return -ENOMEM;
 
@@ -235,6 +235,8 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -0
     * used as size to allocate the array.
     */
    mutex_lock(&userq->fence_drv_lock);
+   if (xa_empty(&userq->fence_drv_xa))
+           goto unlock;
    XA_STATE(xas, &userq->fence_drv_xa, 0);
 
    rcu_read_lock();
@@ -256,7 +258,7 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
    xa_extract(&userq->fence_drv_xa, (void **)userq_fence->fence_drv_array,
               0, ULONG_MAX, xas.xa_index, XA_PRESENT);
    xa_destroy(&userq->fence_drv_xa);
-
+unlock:
    mutex_unlock(&userq->fence_drv_lock);
 
    amdgpu_userq_fence_driver_get(fence_drv);
maintainer
#7 lore raw
Quoted 21 lines · Prike

AMD General

Regards,
Prike

Quoted 16 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, May 26, 2026 6:48 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 1/3] drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

Quoted 4 lines · Prike Liang

Avoid xarray extraction and temporary array allocation in amdgpu_userq_fence_alloc() when there are no pending wait-side fence driver references. This keeps the common fence emit path cheaper and efficient.

Quoted 5 lines
Quoted 3 lines

That's an absolute corner case we clearly don't need to optimize for.

In almost all cases we should have at least one remote fence driver here.

When only the desktop compositor is running, there're many no-wait fences are generated while emitting userq fences.

That sounds like a bug to me. In almost all cases we should have always at least one wait fence in here.

Otherwise the synchronization between X/Wayland and rendering client isn't working properly.

Can you investigate why we don't have a fence dependency here?

What could be is that we filter out that dependency in the wait IOCTL because it is already signaled.

Regards,
Christian.

Quoted 49 lines

Repeatedly attempting to extract the wait fence array takes more than 10µs (with a maximum cost of around 30µs). Additionally, zero-initializing the userq fence allocation can help reduce overhead in the userq fence put routine.

This patch can return a userq fence driver even when falling back from an empty fence_drv_xa, benefiting on reducing the latency of userq fence driver extraction and free operations when there is no pending wait-side fence.

Quoted 44 lines

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 12 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0 -0
index 008330a0d852..2a2bf13a513d 100644
@@ -226,7 +226,7 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
    struct amdgpu_userq_fence *userq_fence;
    void *entry;
 
-   userq_fence = kmalloc(sizeof(*userq_fence), GFP_KERNEL);
+   userq_fence = kzalloc(sizeof(*userq_fence), GFP_KERNEL);
    if (!userq_fence)
            return -ENOMEM;
 
@@ -235,6 +235,8 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -0
     * used as size to allocate the array.
     */
    mutex_lock(&userq->fence_drv_lock);
+   if (xa_empty(&userq->fence_drv_xa))
+           goto unlock;
    XA_STATE(xas, &userq->fence_drv_xa, 0);
 
    rcu_read_lock();
@@ -256,7 +258,7 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
    xa_extract(&userq->fence_drv_xa, (void **)userq_fence->fence_drv_array,
               0, ULONG_MAX, xas.xa_index, XA_PRESENT);
    xa_destroy(&userq->fence_drv_xa);
-
+unlock:
    mutex_unlock(&userq->fence_drv_lock);
 
    amdgpu_userq_fence_driver_get(fence_drv);
#8 lore raw

AMD General

Regards,
Prike

Quoted 42 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, May 27, 2026 3:59 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 1/3] drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

Quoted 21 lines · Prike

AMD General

Regards,
Prike

Quoted 16 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, May 26, 2026 6:48 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 1/3] drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

Quoted 4 lines · Prike Liang

Avoid xarray extraction and temporary array allocation in amdgpu_userq_fence_alloc() when there are no pending wait-side fence driver references. This keeps the common fence emit path cheaper and efficient.

Quoted 5 lines
Quoted 3 lines

That's an absolute corner case we clearly don't need to optimize for.

In almost all cases we should have at least one remote fence driver here.

When only the desktop compositor is running, there're many no-wait fences are

generated while emitting userq fences.

That sounds like a bug to me. In almost all cases we should have always at least one wait fence in here.

Quoted 7 lines

Otherwise the synchronization between X/Wayland and rendering client isn't working properly.

Can you investigate why we don't have a fence dependency here?

What could be is that we filter out that dependency in the wait IOCTL because it is already signaled.

When only the desktop compositor is running, the wait fence ioctl gathers only the VM timeline fence. No userq fences from generic syncobjs or BO implicit sync are present, so no userq fence driver is referenced during the wait path. In this case, skipping the allocation and extraction of the userq fence driver would reduce the per submission overhead of emitting userq fences.

Quoted 57 lines

Regards,
Christian.

Quoted 1 line

Repeatedly attempting to extract the wait fence array takes more than 10µs (with a

maximum cost of around 30µs). Additionally, zero-initializing the userq fence allocation can help reduce overhead in the userq fence put routine.

Quoted 2 lines

This patch can return a userq fence driver even when falling back from an empty

fence_drv_xa, benefiting on reducing the latency of userq fence driver extraction and free operations when there is no pending wait-side fence.

Quoted 46 lines
Quoted 45 lines

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 12 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0 -0
index 008330a0d852..2a2bf13a513d 100644
@@ -226,7 +226,7 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
    struct amdgpu_userq_fence *userq_fence;
    void *entry;
 
-   userq_fence = kmalloc(sizeof(*userq_fence), GFP_KERNEL);
+   userq_fence = kzalloc(sizeof(*userq_fence), GFP_KERNEL);
    if (!userq_fence)
            return -ENOMEM;
 
@@ -235,6 +235,8 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -0
     * used as size to allocate the array.
     */
    mutex_lock(&userq->fence_drv_lock);
+   if (xa_empty(&userq->fence_drv_xa))
+           goto unlock;
    XA_STATE(xas, &userq->fence_drv_xa, 0);
 
    rcu_read_lock();
@@ -256,7 +258,7 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
    xa_extract(&userq->fence_drv_xa, (void **)userq_fence->fence_drv_array,
               0, ULONG_MAX, xas.xa_index, XA_PRESENT);
    xa_destroy(&userq->fence_drv_xa);
-
+unlock:
    mutex_unlock(&userq->fence_drv_lock);
 
    amdgpu_userq_fence_driver_get(fence_drv);
#9 lore raw

AMD General

Regards,
Prike

Quoted 62 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Liang, Prike
Sent: Friday, May 29, 2026 4:20 PM
To: Koenig, Christian <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: RE: [PATCH 1/3] drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

AMD General

Regards,
Prike

Quoted 49 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Wednesday, May 27, 2026 3:59 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 1/3] drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

Quoted 22 lines · Prike

AMD General

Regards,
Prike

Quoted 17 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, May 26, 2026 6:48 PM
To: Liang, Prike <[email protected]>; [email protected]
Cc: Deucher, Alexander <[email protected]>
Subject: Re: [PATCH 1/3] drm/amdgpu: avoid extracting fence_drv_array for empty wait fences

Quoted 4 lines · Prike Liang

Avoid xarray extraction and temporary array allocation in amdgpu_userq_fence_alloc() when there are no pending wait-side fence driver references. This keeps the common fence emit path cheaper and efficient.

Quoted 6 lines
Quoted 3 lines

That's an absolute corner case we clearly don't need to optimize for.

In almost all cases we should have at least one remote fence driver here.

When only the desktop compositor is running, there're many no-wait fences are

generated while emitting userq fences.

That sounds like a bug to me. In almost all cases we should have always at least one wait fence in here.

Otherwise the synchronization between X/Wayland and rendering client isn't working properly.

Can you investigate why we don't have a fence dependency here?

Quoted 8 lines
Quoted 2 lines

What could be is that we filter out that dependency in the wait IOCTL because it is already signaled.

When only the desktop compositor is running, the wait fence ioctl gathers only the VM timeline fence. No userq fences from generic syncobjs or BO implicit sync are present, so no userq fence driver is referenced during the wait path. In this case, skipping the allocation and extraction of the userq fence driver would reduce the per submission overhead of emitting userq fences.

Hi Christian, there is a case where no userq wait fences are present except the VM timeline fence. Since the VM timeline fence is a kernel side fence and is not passed back to userspace as a FWM packet, would it be worthwhile to reduce the overhead by skipping the allocation of the userq fence storage array in this case?

Quoted 59 lines
Quoted 59 lines

Regards,
Christian.

Quoted 2 lines

Repeatedly attempting to extract the wait fence array takes more than 10µs (with a

maximum cost of around 30µs). Additionally, zero-initializing the userq fence allocation can help reduce overhead in the userq fence put routine.

Quoted 3 lines

This patch can return a userq fence driver even when falling back from an empty

fence_drv_xa, benefiting on reducing the latency of userq fence driver extraction and free operations when there is no pending wait-side fence.

Quoted 46 lines
Quoted 45 lines

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 12 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +0 -0
index 008330a0d852..2a2bf13a513d 100644
@@ -226,7 +226,7 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
    struct amdgpu_userq_fence *userq_fence;
    void *entry;
 
-   userq_fence = kmalloc(sizeof(*userq_fence), GFP_KERNEL);
+   userq_fence = kzalloc(sizeof(*userq_fence), GFP_KERNEL);
    if (!userq_fence)
            return -ENOMEM;
 
@@ -235,6 +235,8 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c static int amdgpu_userq_fence_alloc(struct

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +2 -0
     * used as size to allocate the array.
     */
    mutex_lock(&userq->fence_drv_lock);
+   if (xa_empty(&userq->fence_drv_xa))
+           goto unlock;
    XA_STATE(xas, &userq->fence_drv_xa, 0);
 
    rcu_read_lock();
@@ -256,7 +258,7 @@ static int amdgpu_userq_fence_alloc(struct

amdgpu_usermode_queue *userq,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_userq_fence.c +1 -1
    xa_extract(&userq->fence_drv_xa, (void **)userq_fence->fence_drv_array,
               0, ULONG_MAX, xas.xa_index, XA_PRESENT);
    xa_destroy(&userq->fence_drv_xa);
-
+unlock:
    mutex_unlock(&userq->fence_drv_lock);
 
    amdgpu_userq_fence_driver_get(fence_drv);