Skip to contentSkip to conversation

Re-working dma_fence to not call enable_signaling with

RFC In review Christian König 5 participants 14 messages Sep 11, 2024 – Sep 24, 2024

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

Hello everyone,

The enable_signaling callback is the only function the dma_fence objects calls with the fence lock held (the signaled callback might be called with the fence lock held as well, but that isn't guaranted).

The background of that decision was to avoid races with other CPUs trying to signal the fence at the same time and potentially enforce an ordering of fence signaling.

The only problem is that this never worked correctly.

First of all the enabling_signaling call can still race with signaling a fence, it's just that informing the installed callbacks is blocking for the enable signaling to finish. If that is required (radeon is an example of that) then drivers can still grab the fence themselves, everybody else doesn't need that.

Then regarding fence ordering it is perfectly possible that fences emitted in the order A,B,C call their installed callbacks in the order B, C, A. The background is that the optimization to signal fences from dma_fence_is_signaled() decouples the fence signaling from the interrupt handlers. The result is that fence C can signal because somebody queried it's state while A and B still wait for their interrupt to arrive.

While those two reasons are just unnecessary churn the documentation is simply erroneous and suggests an illegal operation to
implementations: "This function can be called from atomic context, but not from irq context, so normal spinlocks can be used.". Since the enable_signaling callback was called with interrupts disabled that practice could deadlock.

Furtunately nobody actually ran into problems with that, but considering that we should probably re-work the locking to allow dma_fence objects to exists after their drivers were unloaded this patch re-works all this to not call the callback with the dma_fence spinlock held and rather move the handling into the drivers which actually need it.

Going to send this out once more to the individual driver maintainers affected, but wanted to get a general feedback first if that is the right approach.

Please comment and/or review,
Christian.

#1 lore raw

PATCH 1/7 dma-buf: add WARN_ON() illegal dma-fence signaling

Calling the signaling a NULL fence is obviously a coding error in a driver. Those functions unfortunately just returned silently without raising a warning.

---
 drivers/dma-buf/dma-fence.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +2 -2
index 0393a9bba3a8..325a263ac798 100644
@@ -412,7 +412,7 @@ int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
 	unsigned long flags;
 	int ret;
 
-	if (!fence)
+	if (WARN_ON(!fence))
 		return -EINVAL;
 
 	spin_lock_irqsave(fence->lock, flags);
@@ -464,7 +464,7 @@ int dma_fence_signal(struct dma_fence *fence)
 	int ret;
 	bool tmp;
 
-	if (!fence)
+	if (WARN_ON(!fence))
 		return -EINVAL;
 
 	tmp = dma_fence_begin_signalling();
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/7] dma-buf: add WARN_ON() illegal dma-fence signaling

Quoted 3 lines · Christian König

Calling the signaling a NULL fence is obviously a coding error in a driver. Those functions unfortunately just returned silently without raising a warning.

Good catch

Quoted 18 lines

---
 drivers/dma-buf/dma-fence.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/dma- renamed from drivers/dma-buf/dma-fence.c +0 -0

fence.c
index 0393a9bba3a8..325a263ac798 100644

drivers/dma-buf/dma-fence.c +0 -0
@@ -412,7 +412,7 @@ int dma_fence_signal_timestamp(struct dma_fence
 *fence, ktime_t timestamp)
  	unsigned long flags;
  	int ret;

drivers/dma-buf/dma-fence.c +1 -1
-	if (!fence)
+	if (WARN_ON(!fence))
Quoted 1 line

  return -EINVAL;

While one can do that, as far as I can see there are only a hand full of users of that function anyways.

Couldn't one (additionally) add the error check of dma_fenc_signal_timestapm() to those? Like in dma_fenc_allocate_private_stub().

It seems some of them are void functions, though. Hm. There is also the attribute __must_check that could be considered now or in the future for such functions.

Regards,
P.

Quoted 11 lines


  spin_lock_irqsave(fence->lock, flags);

drivers/dma-buf/dma-fence.c +0 -0
@@ -464,7 +464,7 @@ int dma_fence_signal(struct dma_fence *fence)
  	int ret;
  	bool tmp;

drivers/dma-buf/dma-fence.c +1 -1
-	if (!fence)
+	if (WARN_ON(!fence))

  return -EINVAL;

  tmp = dma_fence_begin_signalling();

#3 lore raw

Re: [PATCH 1/7] dma-buf: add WARN_ON() illegal dma-fence signaling

Am 11.09.24 um 11:44 schrieb Philipp Stanner:

Quoted 21 lines · Christian König
Quoted 3 lines

Calling the signaling a NULL fence is obviously a coding error in a driver. Those functions unfortunately just returned silently without raising a warning.

Good catch

Quoted 15 lines
---
  drivers/dma-buf/dma-fence.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/dma- renamed from drivers/dma-buf/dma-fence.c +0 -0

fence.c
index 0393a9bba3a8..325a263ac798 100644

drivers/dma-buf/dma-fence.c +0 -0
@@ -412,7 +412,7 @@ int dma_fence_signal_timestamp(struct dma_fence
 *fence, ktime_t timestamp)
  	unsigned long flags;
  	int ret;
 
Quoted 5 lines

drivers/dma-buf/dma-fence.c int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp) · line 416

Quoted 3 lines
drivers/dma-buf/dma-fence.c +1 -1
-	if (!fence)
+	if (WARN_ON(!fence))
  		return -EINVAL;

While one can do that, as far as I can see there are only a hand full of users of that function anyways.

The dma_fence_signal() function has tons of users, it's basically the core of the DMA-buf framework.

Quoted 7 lines

Couldn't one (additionally) add the error check of dma_fenc_signal_timestapm() to those? Like in dma_fenc_allocate_private_stub().

It seems some of them are void functions, though. Hm. There is also the attribute __must_check that could be considered now or in the future for such functions.

I actually want to remove the error return from dma_fence_signal() and the other variants. There is no valid reason that those functions should fail.

The only user is some obscure use case in AMDs KFD driver and I would rather like to clean that one up.

Regards,
Christian.

Quoted 16 lines

Regards,
P.

Quoted 11 lines
drivers/dma-buf/dma-fence.c +1 -1
  	spin_lock_irqsave(fence->lock, flags);
@@ -464,7 +464,7 @@ int dma_fence_signal(struct dma_fence *fence)
  	int ret;
  	bool tmp;
 
-	if (!fence)
+	if (WARN_ON(!fence))
  		return -EINVAL;
 
  	tmp = dma_fence_begin_signalling();
#4 lore raw

Re: [PATCH 1/7] dma-buf: add WARN_ON() illegal dma-fence signaling

Quoted 32 lines · Christian König

Am 11.09.24 um 11:44 schrieb Philipp Stanner:

Quoted 22 lines · Christian König
Quoted 5 lines

Calling the signaling a NULL fence is obviously a coding error in a
driver. Those functions unfortunately just returned silently without
raising a warning.

Good catch

Quoted 14 lines

---
  drivers/dma-buf/dma-fence.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/dma-buf/dma- renamed from drivers/dma-buf/dma-fence.c +0 -0

fence.c
index 0393a9bba3a8..325a263ac798 100644

drivers/dma-buf/dma-fence.c +0 -0
@@ -412,7 +412,7 @@ int dma_fence_signal_timestamp(struct

dma_fence
*fence, ktime_t timestamp)
   unsigned long flags;

Quoted 8 lines
Quoted 5 lines

   int ret;

drivers/dma-buf/dma-fence.c +1 -1
-	if (!fence)
+	if (WARN_ON(!fence))

   return -EINVAL;

While one can do that, as far as I can see there are only a hand full
of users of that function anyways.

Quoted 2 lines

The dma_fence_signal() function has tons of users, it's basically the core of the DMA-buf framework.

I meant dma_fence_signal_timestamp() itself.

Quoted 10 lines
Quoted 9 lines

Couldn't one (additionally) add the error check of dma_fenc_signal_timestapm() to those? Like in dma_fenc_allocate_private_stub().

It seems some of them are void functions, though. Hm. There is also the attribute __must_check that could be considered now
or in the future for such functions.

Quoted 5 lines

I actually want to remove the error return from dma_fence_signal() and
the other variants. There is no valid reason that those functions should
fail.

Makes sense to me.
+1

P.

Quoted 24 lines

The only user is some obscure use case in AMDs KFD driver and I would rather like to clean that one up.

Regards,
Christian.

Quoted 16 lines

Regards,
P.

Quoted 11 lines


   spin_lock_irqsave(fence->lock, flags);

drivers/dma-buf/dma-fence.c +0 -0
@@ -464,7 +464,7 @@ int dma_fence_signal(struct dma_fence *fence)
   	int ret;
   	bool tmp;

drivers/dma-buf/dma-fence.c +1 -1
-	if (!fence)
+	if (WARN_ON(!fence))

   return -EINVAL;

   tmp = dma_fence_begin_signalling();

#5 lore raw

PATCH 2/7 dma-buf/sw_sync: remove pointless enable_signaling implementation

In reply to Christian König

Just returning true here is a no-op, remove the optional callback.

---
 drivers/dma-buf/sw_sync.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/dma-buf/sw_sync.c +0 -6
index c353029789cf..f5905d67dedb 100644
@@ -173,11 +173,6 @@ static bool timeline_fence_signaled(struct dma_fence *fence)
 	return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
 }
 
-static bool timeline_fence_enable_signaling(struct dma_fence *fence)
-{
-	return true;
-}
-
 static void timeline_fence_value_str(struct dma_fence *fence,
 				    char *str, int size)
 {
@@ -211,7 +206,6 @@ static void timeline_fence_set_deadline(struct dma_fence *fence, ktime_t deadlin
 static const struct dma_fence_ops timeline_fence_ops = {
 	.get_driver_name = timeline_fence_get_driver_name,
 	.get_timeline_name = timeline_fence_get_timeline_name,
-	.enable_signaling = timeline_fence_enable_signaling,
 	.signaled = timeline_fence_signaled,
 	.release = timeline_fence_release,
 	.fence_value_str = timeline_fence_value_str,
Signature · 1 line
2.34.1
#6 lore raw

PATCH 3/7 drm/writeback: remove pointless enable_signaling implementation

In reply to Christian König

Just returning true here is a no-op, remove the optional callback.

---
 drivers/gpu/drm/drm_writeback.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/gpu/drm/drm_writeback.c +0 -6
index a031c335bdb9..33a3c98a962d 100644
@@ -100,15 +100,9 @@ drm_writeback_fence_get_timeline_name(struct dma_fence *fence)
 	return wb_connector->timeline_name;
 }
 
-static bool drm_writeback_fence_enable_signaling(struct dma_fence *fence)
-{
-	return true;
-}
-
 static const struct dma_fence_ops drm_writeback_fence_ops = {
 	.get_driver_name = drm_writeback_fence_get_driver_name,
 	.get_timeline_name = drm_writeback_fence_get_timeline_name,
-	.enable_signaling = drm_writeback_fence_enable_signaling,
 };
 
 static int create_writeback_properties(struct drm_device *dev)
Signature · 1 line
2.34.1
Reviewed-by on v1 3/7
#7 lore raw

Re: [PATCH 3/7] drm/writeback: remove pointless enable_signaling implementation

Quoted 3 lines · Christian König

Just returning true here is a no-op, remove the optional callback.

Patches 1-3:

Quoted 27 lines
---
 drivers/gpu/drm/drm_writeback.c | 6 ------
 1 file changed, 6 deletions(-)
drivers/gpu/drm/drm_writeback.c +0 -6
index a031c335bdb9..33a3c98a962d 100644
@@ -100,15 +100,9 @@ drm_writeback_fence_get_timeline_name(struct dma_fence *fence)
 	return wb_connector->timeline_name;
 }
 
-static bool drm_writeback_fence_enable_signaling(struct dma_fence *fence)
-{
-	return true;
-}
-
 static const struct dma_fence_ops drm_writeback_fence_ops = {
 	.get_driver_name = drm_writeback_fence_get_driver_name,
 	.get_timeline_name = drm_writeback_fence_get_timeline_name,
-	.enable_signaling = drm_writeback_fence_enable_signaling,
 };
 
 static int create_writeback_properties(struct drm_device *dev)
Signature · 1 line
2.34.1
Signature · 3 lines
Simona Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#8 lore raw

PATCH 4/7 drm/nouveau: remove questionable enable_signaling implementation

In reply to Christian König

The comment that the callback is mandatory is incorrect and testing if the fence is signaled or not just duplicates the testing by the core framework. So this looks superflous.

Keep the implementation for the uevent operations.

---
 drivers/gpu/drm/nouveau/nouveau_fence.c | 49 ++++++++-----------------
 1 file changed, 16 insertions(+), 33 deletions(-)
drivers/gpu/drm/nouveau/nouveau_fence.c +16 -33
index 09686d038d60..61d193e41f8c 100644
@@ -486,31 +486,6 @@ static bool nouveau_fence_is_signaled(struct dma_fence *f)
 	return ret;
 }
 
-static bool nouveau_fence_no_signaling(struct dma_fence *f)
-{
-	struct nouveau_fence *fence = from_fence(f);
-
-	/*
-	 * caller should have a reference on the fence,
-	 * else fence could get freed here
-	 */
-	WARN_ON(kref_read(&fence->base.refcount) <= 1);
-
-	/*
-	 * This needs uevents to work correctly, but dma_fence_add_callback relies on
-	 * being able to enable signaling. It will still get signaled eventually,
-	 * just not right away.
-	 */
-	if (nouveau_fence_is_signaled(f)) {
-		list_del(&fence->head);
-
-		dma_fence_put(&fence->base);
-		return false;
-	}
-
-	return true;
-}
-
 static void nouveau_fence_release(struct dma_fence *f)
 {
 	struct nouveau_fence *fence = from_fence(f);
@@ -523,7 +498,6 @@ static void nouveau_fence_release(struct dma_fence *f)
 static const struct dma_fence_ops nouveau_fence_ops_legacy = {
 	.get_driver_name = nouveau_fence_get_get_driver_name,
 	.get_timeline_name = nouveau_fence_get_timeline_name,
-	.enable_signaling = nouveau_fence_no_signaling,
 	.signaled = nouveau_fence_is_signaled,
 	.wait = nouveau_fence_wait_legacy,
 	.release = nouveau_fence_release
@@ -533,18 +507,27 @@ static bool nouveau_fence_enable_signaling(struct dma_fence *f)
 {
 	struct nouveau_fence *fence = from_fence(f);
 	struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
-	bool ret;
+
+	/*
+	 * caller should have a reference on the fence,
+	 * else fence could get freed here
+	 */
+	WARN_ON(kref_read(&fence->base.refcount) <= 1);
 
 	if (!fctx->notify_ref++)
 		nvif_event_allow(&fctx->event);
 
-	ret = nouveau_fence_no_signaling(f);
-	if (ret)
-		set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
-	else if (!--fctx->notify_ref)
-		nvif_event_block(&fctx->event);
+	if (nouveau_fence_is_signaled(f)) {
+		list_del(&fence->head);
 
-	return ret;
+		dma_fence_put(&fence->base);
+		if (!--fctx->notify_ref)
+			nvif_event_block(&fctx->event);
+		return false;
+	} else {
+		set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
+		return true;
+	}
 }
 
 static const struct dma_fence_ops nouveau_fence_ops_uevent = {
Signature · 1 line
2.34.1
#9 lore raw

PATCH 5/7 drm/vmwgfx: rework questionable enable_signaling implementation

In reply to Christian König

The implementation just tests if the fence is signaled or not and doesn't enable any further handling. On the other hand the callback to test if a fence is signaled or not is missing.

So remove the enable_signaling callback and implement the signaled callback instead.

---
 drivers/gpu/drm/vmwgfx/vmwgfx_fence.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_fence.c +4 -4
index 588d50ababf6..255ef677c712 100644
@@ -143,7 +143,7 @@ static const char *vmw_fence_get_timeline_name(struct dma_fence *f)
 	return "svga";
 }
 
-static bool vmw_fence_enable_signaling(struct dma_fence *f)
+static bool vmw_fence_signaled(struct dma_fence *f)
 {
 	struct vmw_fence_obj *fence =
 		container_of(f, struct vmw_fence_obj, base);
@@ -153,9 +153,9 @@ static bool vmw_fence_enable_signaling(struct dma_fence *f)
 
 	u32 seqno = vmw_fence_read(dev_priv);
 	if (seqno - fence->base.seqno < VMW_FENCE_WRAP)
-		return false;
+		return true;
 
-	return true;
+	return false;
 }
 
 struct vmwgfx_wait_cb {
@@ -251,7 +251,7 @@ static long vmw_fence_wait(struct dma_fence *f, bool intr, signed long timeout)
 static const struct dma_fence_ops vmw_fence_ops = {
 	.get_driver_name = vmw_fence_get_driver_name,
 	.get_timeline_name = vmw_fence_get_timeline_name,
-	.enable_signaling = vmw_fence_enable_signaling,
+	.signaled = vmw_fence_signaled,
 	.wait = vmw_fence_wait,
 	.release = vmw_fence_obj_destroy,
 };
Signature · 1 line
2.34.1
#10 lore raw

PATCH 6/7 drm/amdgpu: rework fallback timer handling

In reply to Christian König

Arm the fallback timer directly while submitting fences, it should only be used on broken hardware anyway.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 64 ++++++-----------------
 1 file changed, 16 insertions(+), 48 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +16 -48
index 2f24a6aa13bf..a5256ba2b03f 100644
@@ -125,6 +125,19 @@ static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
 	return seq;
 }
 
+/**
+ * amdgpu_fence_schedule_fallback - schedule fallback check
+ *
+ * @ring: pointer to struct amdgpu_ring
+ *
+ * Start a timer as fallback to our interrupts.
+ */
+static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
+{
+	mod_timer(&ring->fence_drv.fallback_timer,
+		  jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
+}
+
 /**
  * amdgpu_fence_emit - emit a fence on the requested ring
  *
@@ -206,6 +219,9 @@ int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f, struct amd
 
 	*f = fence;
 
+	if (!timer_pending(&ring->fence_drv.fallback_timer))
+		amdgpu_fence_schedule_fallback(ring);
+
 	return 0;
 }
 
@@ -244,19 +260,6 @@ int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s,
 	return 0;
 }
 
-/**
- * amdgpu_fence_schedule_fallback - schedule fallback check
- *
- * @ring: pointer to struct amdgpu_ring
- *
- * Start a timer as fallback to our interrupts.
- */
-static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
-{
-	mod_timer(&ring->fence_drv.fallback_timer,
-		  jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
-}
-
 /**
  * amdgpu_fence_process - check for fence activity
  *
@@ -785,39 +788,6 @@ static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
 	return (const char *)to_amdgpu_ring(job->base.sched)->name;
 }
 
-/**
- * amdgpu_fence_enable_signaling - enable signalling on fence
- * @f: fence
- *
- * This function is called with fence_queue lock held, and adds a callback
- * to fence_queue that checks if this fence is signaled, and if so it
- * signals the fence and removes itself.
- */
-static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
-{
-	if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
-		amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
-
-	return true;
-}
-
-/**
- * amdgpu_job_fence_enable_signaling - enable signalling on job fence
- * @f: fence
- *
- * This is the simliar function with amdgpu_fence_enable_signaling above, it
- * only handles the job embedded fence.
- */
-static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f)
-{
-	struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
-
-	if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer))
-		amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched));
-
-	return true;
-}
-
 /**
  * amdgpu_fence_free - free up the fence memory
  *
@@ -877,14 +847,12 @@ static void amdgpu_job_fence_release(struct dma_fence *f)
 static const struct dma_fence_ops amdgpu_fence_ops = {
 	.get_driver_name = amdgpu_fence_get_driver_name,
 	.get_timeline_name = amdgpu_fence_get_timeline_name,
-	.enable_signaling = amdgpu_fence_enable_signaling,
 	.release = amdgpu_fence_release,
 };
 
 static const struct dma_fence_ops amdgpu_job_fence_ops = {
 	.get_driver_name = amdgpu_fence_get_driver_name,
 	.get_timeline_name = amdgpu_job_fence_get_timeline_name,
-	.enable_signaling = amdgpu_job_fence_enable_signaling,
 	.release = amdgpu_job_fence_release,
 };
 
Signature · 1 line
2.34.1
#11 lore raw

PATCH 7/7 dma-buf: rework the enable_signaling handling

In reply to Christian König

The enable_signaling callback is the only function the dma_fence objects calls with the fence lock held (the signaled callback might be called with the fence lock held as well, but that isn't guaranted).

The background of that decision was to avoid races with other CPUs trying to signal the fence at the same time and potentially enforce an ordering of fence signaling.

The only problem is that this never worked correctly.

First of all the enabling_signaling call can still race with signaling a fence, it's just that informing the installed callbacks is blocking for the enable signaling to finish. If that is required (radeon is an example of that) then drivers can still grab the fence themselves, everybody else doesn't need that.

Then regarding fence ordering it is perfectly possible that fences emitted in the order A,B,C call their installed callbacks in the order B, C, A. The background is that the optimization to signal fences from dma_fence_is_signaled() decouples the fence signaling from the interrupt handlers. The result is that fence C can signal because somebody queried it's state while A and B still wait for their interrupt to arrive.

While those two reasons are just unnecessary churn the documentation is simply erroneous and suggests an illegal operation to
implementations: "This function can be called from atomic context, but not from irq context, so normal spinlocks can be used.". Since the enable_signaling callback was called with interrupts disabled that practice could deadlock.

Furtunately nobody actually ran into problems with that, but considering that we should probably re-work the locking to allow dma_fence objects to exists after their drivers were unloaded this patch re-works all this to not call the callback with the dma_fence spinlock held and rather move the handling into the drivers which actually need it.

---
 drivers/dma-buf/dma-fence-array.c             |  7 +-
 drivers/dma-buf/dma-fence-chain.c             | 13 ++--
 drivers/dma-buf/dma-fence.c                   | 68 +++++++------------
 drivers/dma-buf/st-dma-fence-chain.c          |  4 +-
 drivers/dma-buf/st-dma-fence-unwrap.c         | 22 +++---
 drivers/dma-buf/st-dma-fence.c                | 16 ++---
 drivers/dma-buf/st-dma-resv.c                 | 10 +--
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c  | 12 ++--
 drivers/gpu/drm/i915/i915_active.c            |  2 +-
 drivers/gpu/drm/i915/i915_request.c           | 12 +++-
 drivers/gpu/drm/nouveau/nouveau_fence.c       |  9 ++-
 drivers/gpu/drm/radeon/radeon_fence.c         | 17 +++--
 drivers/gpu/drm/ttm/ttm_bo.c                  |  2 +-
 drivers/gpu/drm/xe/xe_bo.c                    |  2 +-
 drivers/gpu/drm/xe/xe_hw_fence.c              |  4 +-
 drivers/gpu/drm/xe/xe_preempt_fence.c         |  3 +-
 drivers/gpu/drm/xe/xe_pt.c                    |  2 +-
 drivers/gpu/drm/xe/xe_sched_job.c             |  2 +-
 drivers/gpu/drm/xe/xe_vm.c                    |  6 +-
 drivers/gpu/host1x/fence.c                    | 14 ++--
 include/linux/dma-fence.h                     | 35 +++-------
 21 files changed, 123 insertions(+), 139 deletions(-)
drivers/dma-buf/dma-fence-array.c +3 -4
index c74ac197d5fe..1022b08c9b42 100644
@@ -67,7 +67,7 @@ static void dma_fence_array_cb_func(struct dma_fence *f,
 		dma_fence_put(&array->base);
 }
 
-static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
+static void dma_fence_array_enable_signaling(struct dma_fence *fence)
 {
 	struct dma_fence_array *array = to_dma_fence_array(fence);
 	struct dma_fence_array_cb *cb = array->callbacks;
@@ -92,12 +92,11 @@ static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
 			dma_fence_put(&array->base);
 			if (atomic_dec_and_test(&array->num_pending)) {
 				dma_fence_array_clear_pending_error(array);
-				return false;
+				dma_fence_signal(&array->base);
+				return;
 			}
 		}
 	}
-
-	return true;
 }
 
 static bool dma_fence_array_signaled(struct dma_fence *fence)
drivers/dma-buf/dma-fence-chain.c +5 -8
index 9663ba1bb6ac..f56baa214a6c 100644
@@ -9,7 +9,7 @@
 
 #include <linux/dma-fence-chain.h>
 
-static bool dma_fence_chain_enable_signaling(struct dma_fence *fence);
+static void dma_fence_chain_enable_signaling(struct dma_fence *fence);
 
 /**
  * dma_fence_chain_get_prev - use RCU to get a reference to the previous fence
@@ -125,10 +125,7 @@ static void dma_fence_chain_irq_work(struct irq_work *work)
 
 	chain = container_of(work, typeof(*chain), work);
 
-	/* Try to rearm the callback */
-	if (!dma_fence_chain_enable_signaling(&chain->base))
-		/* Ok, we are done. No more unsignaled fences left */
-		dma_fence_signal(&chain->base);
+	dma_fence_chain_enable_signaling(&chain->base);
 	dma_fence_put(&chain->base);
 }
 
@@ -142,7 +139,7 @@ static void dma_fence_chain_cb(struct dma_fence *f, struct dma_fence_cb *cb)
 	dma_fence_put(f);
 }
 
-static bool dma_fence_chain_enable_signaling(struct dma_fence *fence)
+static void dma_fence_chain_enable_signaling(struct dma_fence *fence)
 {
 	struct dma_fence_chain *head = to_dma_fence_chain(fence);
 
@@ -153,12 +150,12 @@ static bool dma_fence_chain_enable_signaling(struct dma_fence *fence)
 		dma_fence_get(f);
 		if (!dma_fence_add_callback(f, &head->cb, dma_fence_chain_cb)) {
 			dma_fence_put(fence);
-			return true;
+			return;
 		}
 		dma_fence_put(f);
 	}
 	dma_fence_put(&head->base);
-	return false;
+	dma_fence_signal(fence);
 }
 
 static bool dma_fence_chain_signaled(struct dma_fence *fence)
drivers/dma-buf/dma-fence.c +26 -42

109 lines not on this page · raw message

drivers/dma-buf/st-dma-fence-chain.c +2 -2
index ed4b323886e4..455f6f6c3b90 100644
@@ -85,7 +85,7 @@ static int sanitycheck(void *arg)
 
 	chain = mock_chain(NULL, f, 1);
 	if (chain)
-		dma_fence_enable_sw_signaling(chain);
+		dma_fence_enable_signaling(chain);
 	else
 		err = -ENOMEM;
 
@@ -146,7 +146,7 @@ static int fence_chains_init(struct fence_chains *fc, unsigned int count,
 
 		fc->tail = fc->chains[i];
 
-		dma_fence_enable_sw_signaling(fc->chains[i]);
+		dma_fence_enable_signaling(fc->chains[i]);
 	}
 
 	fc->chain_length = i;
drivers/dma-buf/st-dma-fence-unwrap.c +11 -11
index f0cee984b6c7..1a6cef01fda9 100644
@@ -102,7 +102,7 @@ static int sanitycheck(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	array = mock_array(1, f);
 	if (!array)
@@ -126,7 +126,7 @@ static int unwrap_array(void *arg)
 	if (!f1)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
 	f2 = mock_fence();
 	if (!f2) {
@@ -134,7 +134,7 @@ static int unwrap_array(void *arg)
 		return -ENOMEM;
 	}
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
 	array = mock_array(2, f1, f2);
 	if (!array)
@@ -170,7 +170,7 @@ static int unwrap_chain(void *arg)
 	if (!f1)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
 	f2 = mock_fence();
 	if (!f2) {
@@ -178,7 +178,7 @@ static int unwrap_chain(void *arg)
 		return -ENOMEM;
 	}
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
 	chain = mock_chain(f1, f2);
 	if (!chain)
@@ -214,7 +214,7 @@ static int unwrap_chain_array(void *arg)
 	if (!f1)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
 	f2 = mock_fence();
 	if (!f2) {
@@ -222,7 +222,7 @@ static int unwrap_chain_array(void *arg)
 		return -ENOMEM;
 	}
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
 	array = mock_array(2, f1, f2);
 	if (!array)
@@ -262,7 +262,7 @@ static int unwrap_merge(void *arg)
 	if (!f1)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
 	f2 = mock_fence();
 	if (!f2) {
@@ -270,7 +270,7 @@ static int unwrap_merge(void *arg)
 		goto error_put_f1;
 	}
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
 	f3 = dma_fence_unwrap_merge(f1, f2);
 	if (!f3) {
@@ -314,13 +314,13 @@ static int unwrap_merge_complex(void *arg)
 	if (!f1)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
 	f2 = mock_fence();
 	if (!f2)
 		goto error_put_f1;
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
 	f3 = dma_fence_unwrap_merge(f1, f2);
 	if (!f3)
drivers/dma-buf/st-dma-fence.c +8 -8
index 6a1bfcd0cc21..eb740b4fe4fe 100644
@@ -102,7 +102,7 @@ static int sanitycheck(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	dma_fence_signal(f);
 	dma_fence_put(f);
@@ -119,7 +119,7 @@ static int test_signaling(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	if (dma_fence_is_signaled(f)) {
 		pr_err("Fence unexpectedly signaled on creation\n");
@@ -194,7 +194,7 @@ static int test_late_add_callback(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	dma_fence_signal(f);
 
@@ -288,7 +288,7 @@ static int test_status(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	if (dma_fence_get_status(f)) {
 		pr_err("Fence unexpectedly has signaled status on creation\n");
@@ -316,7 +316,7 @@ static int test_error(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	dma_fence_set_error(f, -EIO);
 
@@ -347,7 +347,7 @@ static int test_wait(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	if (dma_fence_wait_timeout(f, false, 0) != -ETIME) {
 		pr_err("Wait reported complete before being signaled\n");
@@ -391,7 +391,7 @@ static int test_wait_timeout(void *arg)
 	if (!wt.f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(wt.f);
+	dma_fence_enable_signaling(wt.f);
 
 	if (dma_fence_wait_timeout(wt.f, false, 1) != -ETIME) {
 		pr_err("Wait reported complete before being signaled\n");
@@ -472,7 +472,7 @@ static int thread_signal_callback(void *arg)
 			break;
 		}
 
-		dma_fence_enable_sw_signaling(f1);
+		dma_fence_enable_signaling(f1);
 
 		rcu_assign_pointer(t->fences[t->id], f1);
 		smp_wmb();
drivers/dma-buf/st-dma-resv.c +5 -5
index 15dbea1462ed..f030b43ee4da 100644
@@ -45,7 +45,7 @@ static int sanitycheck(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	dma_fence_signal(f);
 	dma_fence_put(f);
@@ -71,7 +71,7 @@ static int test_signaling(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	dma_resv_init(&resv);
 	r = dma_resv_lock(&resv, NULL);
@@ -118,7 +118,7 @@ static int test_for_each(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	dma_resv_init(&resv);
 	r = dma_resv_lock(&resv, NULL);
@@ -179,7 +179,7 @@ static int test_for_each_unlocked(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	dma_resv_init(&resv);
 	r = dma_resv_lock(&resv, NULL);
@@ -252,7 +252,7 @@ static int test_get_fences(void *arg)
 	if (!f)
 		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
 	dma_resv_init(&resv);
 	r = dma_resv_lock(&resv, NULL);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +6 -6
index 1ef758ac5076..9ec4daa23665 100644
@@ -116,24 +116,24 @@ static const char *amdkfd_fence_get_timeline_name(struct dma_fence *f)
  *
  *  @f: dma_fence
  */
-static bool amdkfd_fence_enable_signaling(struct dma_fence *f)
+static void amdkfd_fence_enable_signaling(struct dma_fence *f)
 {
 	struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
 
 	if (!fence)
-		return false;
+		return;
 
 	if (dma_fence_is_signaled(f))
-		return true;
+		return;
 
 	if (!fence->svm_bo) {
 		if (!kgd2kfd_schedule_evict_and_restore_process(fence->mm, f))
-			return true;
+			return;
 	} else {
 		if (!svm_range_schedule_evict_svm_bo(fence))
-			return true;
+			return;
 	}
-	return false;
+	dma_fence_signal(f);
 }
 
 /**
drivers/gpu/drm/i915/i915_active.c +1 -1
index 5ec293011d99..3f2425966263 100644
@@ -562,7 +562,7 @@ static void enable_signaling(struct i915_active_fence *active)
 	if (!fence)
 		return;
 
-	dma_fence_enable_sw_signaling(fence);
+	dma_fence_enable_signaling(fence);
 	dma_fence_put(fence);
 }
 
drivers/gpu/drm/i915/i915_request.c +10 -2
index 519e096c607c..db4891c7cc7b 100644
@@ -90,9 +90,17 @@ static bool i915_fence_signaled(struct dma_fence *fence)
 	return i915_request_completed(to_request(fence));
 }
 
-static bool i915_fence_enable_signaling(struct dma_fence *fence)
+static void i915_fence_enable_signaling(struct dma_fence *fence)
 {
-	return i915_request_enable_breadcrumb(to_request(fence));
+	struct i915_request *rq = to_request(fence);
+	unsigned long flags;
+	bool ret;
+
+	spin_lock_irqsave(&rq->lock, flags);
+	ret = i915_request_enable_breadcrumb(to_request(fence));
+	spin_unlock_irqrestore(&rq->lock, flags);
+	if (!ret)
+		dma_fence_signal(fence);
 }
 
 static signed long i915_fence_wait(struct dma_fence *fence,
drivers/gpu/drm/nouveau/nouveau_fence.c +6 -3
index 61d193e41f8c..1ad692a033c0 100644
@@ -503,10 +503,11 @@ static const struct dma_fence_ops nouveau_fence_ops_legacy = {
 	.release = nouveau_fence_release
 };
 
-static bool nouveau_fence_enable_signaling(struct dma_fence *f)
+static void nouveau_fence_enable_signaling(struct dma_fence *f)
 {
 	struct nouveau_fence *fence = from_fence(f);
 	struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
+	unsigned long flags;
 
 	/*
 	 * caller should have a reference on the fence,
@@ -514,6 +515,7 @@ static bool nouveau_fence_enable_signaling(struct dma_fence *f)
 	 */
 	WARN_ON(kref_read(&fence->base.refcount) <= 1);
 
+	spin_lock_irqsave(&fctx->lock, flags);
 	if (!fctx->notify_ref++)
 		nvif_event_allow(&fctx->event);
 
@@ -523,10 +525,11 @@ static bool nouveau_fence_enable_signaling(struct dma_fence *f)
 		dma_fence_put(&fence->base);
 		if (!--fctx->notify_ref)
 			nvif_event_block(&fctx->event);
-		return false;
+		spin_unlock_irqrestore(&fctx->lock, flags);
+		dma_fence_signal(f);
 	} else {
 		set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
-		return true;
+		spin_unlock_irqrestore(&fctx->lock, flags);
 	}
 }
 
drivers/gpu/drm/radeon/radeon_fence.c +12 -5
index daff61586be5..fd1d617c3331 100644
@@ -378,14 +378,19 @@ static bool radeon_fence_is_signaled(struct dma_fence *f)
  * to fence_queue that checks if this fence is signaled, and if so it
  * signals the fence and removes itself.
  */
-static bool radeon_fence_enable_signaling(struct dma_fence *f)
+static void radeon_fence_enable_signaling(struct dma_fence *f)
 {
 	struct radeon_fence *fence = to_radeon_fence(f);
 	struct radeon_device *rdev = fence->rdev;
+	unsigned long flags;
 
-	if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq)
-		return false;
+	if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq)
+	    >= fence->seq) {
+		dma_fence_signal(f);
+		return;
+	}
 
+	spin_lock_irqsave(&rdev->fence_queue.lock, flags);
 	if (down_read_trylock(&rdev->exclusive_lock)) {
 		radeon_irq_kms_sw_irq_get(rdev, fence->ring);
 
@@ -396,7 +401,9 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
 		if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq) {
 			radeon_irq_kms_sw_irq_put(rdev, fence->ring);
 			up_read(&rdev->exclusive_lock);
-			return false;
+			spin_unlock_irqrestore(&rdev->fence_queue.lock, flags);
+			dma_fence_signal(f);
+			return;
 		}
 
 		up_read(&rdev->exclusive_lock);
@@ -412,7 +419,7 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
 	fence->fence_wake.func = radeon_fence_check_signaled;
 	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
 	dma_fence_get(f);
-	return true;
+	spin_unlock_irqrestore(&rdev->fence_queue.lock, flags);
 }
 
 /**
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
index 320592435252..9b060a82c6df 100644
@@ -219,7 +219,7 @@ static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
 	dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
 	dma_resv_for_each_fence_unlocked(&cursor, fence) {
 		if (!fence->ops->signaled)
-			dma_fence_enable_sw_signaling(fence);
+			dma_fence_enable_signaling(fence);
 	}
 	dma_resv_iter_end(&cursor);
 }
drivers/gpu/drm/xe/xe_bo.c +1 -1
index 25d0c939ba31..3a1840560ef9 100644
@@ -495,7 +495,7 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
 		dma_resv_iter_begin(&cursor, bo->ttm.base.resv,
 				    DMA_RESV_USAGE_BOOKKEEP);
 		dma_resv_for_each_fence_unlocked(&cursor, fence)
-			dma_fence_enable_sw_signaling(fence);
+			dma_fence_enable_signaling(fence);
 		dma_resv_iter_end(&cursor);
 	}
 
drivers/gpu/drm/xe/xe_hw_fence.c +1 -3
index 0b4f12be3692..77c5b16f835d 100644
@@ -168,7 +168,7 @@ static bool xe_hw_fence_signaled(struct dma_fence *dma_fence)
 		!__dma_fence_is_later(dma_fence->seqno, seqno, dma_fence->ops);
 }
 
-static bool xe_hw_fence_enable_signaling(struct dma_fence *dma_fence)
+static void xe_hw_fence_enable_signaling(struct dma_fence *dma_fence)
 {
 	struct xe_hw_fence *fence = to_xe_hw_fence(dma_fence);
 	struct xe_hw_fence_irq *irq = xe_hw_fence_irq(fence);
@@ -179,8 +179,6 @@ static bool xe_hw_fence_enable_signaling(struct dma_fence *dma_fence)
 	/* SW completed (no HW IRQ) so kick handler to signal fence */
 	if (xe_hw_fence_signaled(dma_fence))
 		xe_hw_fence_irq_run(irq);
-
-	return true;
 }
 
 static void xe_hw_fence_release(struct dma_fence *dma_fence)
drivers/gpu/drm/xe/xe_preempt_fence.c +1 -2
index 83fbeea5aa20..963af6825875 100644
@@ -56,7 +56,7 @@ preempt_fence_get_timeline_name(struct dma_fence *fence)
 	return "preempt";
 }
 
-static bool preempt_fence_enable_signaling(struct dma_fence *fence)
+static void preempt_fence_enable_signaling(struct dma_fence *fence)
 {
 	struct xe_preempt_fence *pfence =
 		container_of(fence, typeof(*pfence), base);
@@ -64,7 +64,6 @@ static bool preempt_fence_enable_signaling(struct dma_fence *fence)
 
 	pfence->error = q->ops->suspend(q);
 	queue_work(q->vm->xe->preempt_fence_wq, &pfence->preempt_work);
-	return true;
 }
 
 static const struct dma_fence_ops preempt_fence_ops = {
drivers/gpu/drm/xe/xe_pt.c +1 -1
index 579ed31b46db..7556bc45e592 100644
@@ -1236,7 +1236,7 @@ static int vma_check_userptr(struct xe_vm *vm, struct xe_vma *vma,
 			dma_resv_iter_begin(&cursor, xe_vm_resv(vm),
 					    DMA_RESV_USAGE_BOOKKEEP);
 			dma_resv_for_each_fence_unlocked(&cursor, fence)
-				dma_fence_enable_sw_signaling(fence);
+				dma_fence_enable_signaling(fence);
 			dma_resv_iter_end(&cursor);
 
 			err = dma_resv_wait_timeout(xe_vm_resv(vm),
drivers/gpu/drm/xe/xe_sched_job.c +1 -1
index 55d47450b2c6..fe2d5edac92d 100644
@@ -210,7 +210,7 @@ void xe_sched_job_set_error(struct xe_sched_job *job, int error)
 
 	trace_xe_sched_job_set_error(job);
 
-	dma_fence_enable_sw_signaling(job->fence);
+	dma_fence_enable_signaling(job->fence);
 	xe_hw_fence_irq_run(job->q->fence_irq);
 }
 
drivers/gpu/drm/xe/xe_vm.c +3 -3
index 3eb76d874eb2..b33636976669 100644
@@ -259,7 +259,7 @@ int xe_vm_add_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
 	 */
 	wait = __xe_vm_userptr_needs_repin(vm) || preempt_fences_waiting(vm);
 	if (wait)
-		dma_fence_enable_sw_signaling(pfence);
+		dma_fence_enable_signaling(pfence);
 
 	up_read(&vm->userptr.notifier_lock);
 
@@ -289,7 +289,7 @@ void xe_vm_remove_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
 		--vm->preempt.num_exec_queues;
 	}
 	if (q->lr.pfence) {
-		dma_fence_enable_sw_signaling(q->lr.pfence);
+		dma_fence_enable_signaling(q->lr.pfence);
 		dma_fence_put(q->lr.pfence);
 		q->lr.pfence = NULL;
 	}
@@ -634,7 +634,7 @@ static bool vma_userptr_invalidate(struct mmu_interval_notifier *mni,
 	dma_resv_iter_begin(&cursor, xe_vm_resv(vm),
 			    DMA_RESV_USAGE_BOOKKEEP);
 	dma_resv_for_each_fence_unlocked(&cursor, fence)
-		dma_fence_enable_sw_signaling(fence);
+		dma_fence_enable_signaling(fence);
 	dma_resv_iter_end(&cursor);
 
 	err = dma_resv_wait_timeout(xe_vm_resv(vm),
drivers/gpu/host1x/fence.c +9 -5
index 139ad1afd935..0581aa23cd41 100644
@@ -30,12 +30,17 @@ static struct host1x_syncpt_fence *to_host1x_fence(struct dma_fence *f)
 	return container_of(f, struct host1x_syncpt_fence, base);
 }
 
-static bool host1x_syncpt_fence_enable_signaling(struct dma_fence *f)
+static void host1x_syncpt_fence_enable_signaling(struct dma_fence *f)
 {
 	struct host1x_syncpt_fence *sf = to_host1x_fence(f);
+	unsigned long flags;
 
-	if (host1x_syncpt_is_expired(sf->sp, sf->threshold))
-		return false;
+	spin_lock_irqsave(f->lock, flags);
+	if (host1x_syncpt_is_expired(sf->sp, sf->threshold)) {
+		spin_unlock_irqrestore(f->lock, flags);
+		dma_fence_signal(f);
+		return;
+	}
 
 	/* Reference for interrupt path. */
 	dma_fence_get(f);
@@ -62,8 +67,7 @@ static bool host1x_syncpt_fence_enable_signaling(struct dma_fence *f)
 	 * so we need to initialize all state used by signalling
 	 * before it.
 	 */
-
-	return true;
+	spin_unlock_irqrestore(f->lock, flags);
 }
 
 static const struct dma_fence_ops host1x_syncpt_fence_ops = {
include/linux/dma-fence.h +10 -25
index e7ad819962e3..93b6176bbf48 100644
@@ -158,39 +158,24 @@ struct dma_fence_ops {
 	/**
 	 * @enable_signaling:
 	 *
-	 * Enable software signaling of fence.
+	 * Guarantee that installed callbacks are called at some point.
 	 *
-	 * For fence implementations that have the capability for hw->hw
-	 * signaling, they can implement this op to enable the necessary
-	 * interrupts, or insert commands into cmdstream, etc, to avoid these
-	 * costly operations for the common case where only hw->hw
-	 * synchronization is required.  This is called in the first
-	 * dma_fence_wait() or dma_fence_add_callback() path to let the fence
-	 * implementation know that there is another driver waiting on the
-	 * signal (ie. hw->sw case).
+	 * Drivers can implement this op to enable the necessary interrupts or
+	 * tell the hardware through other means that somebody is requesting
+	 * this fence to signal. This is called in the first dma_fence_wait()
+	 * or dma_fence_add_callback() path.
 	 *
 	 * This function can be called from atomic context, but not
 	 * from irq context, so normal spinlocks can be used.
 	 *
-	 * A return value of false indicates the fence already passed,
-	 * or some failure occurred that made it impossible to enable
-	 * signaling. True indicates successful enabling.
-	 *
-	 * &dma_fence.error may be set in enable_signaling, but only when false
-	 * is returned.
-	 *
-	 * Since many implementations can call dma_fence_signal() even when before
-	 * @enable_signaling has been called there's a race window, where the
-	 * dma_fence_signal() might result in the final fence reference being
-	 * released and its memory freed. To avoid this, implementations of this
-	 * callback should grab their own reference using dma_fence_get(), to be
-	 * released when the fence is signalled (through e.g. the interrupt
-	 * handler).
+	 * dma_fence_set_error() as well as dma_fence_signal() may be used in
+	 * enable_signaling if the implementation detects an error or that the
+	 * fence already completed succesfully.
 	 *
 	 * This callback is optional. If this callback is not present, then the
 	 * driver must always have signaling enabled.
 	 */
-	bool (*enable_signaling)(struct dma_fence *fence);
+	void (*enable_signaling)(struct dma_fence *fence);
 
 	/**
 	 * @signaled:
@@ -400,7 +385,7 @@ int dma_fence_add_callback(struct dma_fence *fence,
 			   dma_fence_func_t func);
 bool dma_fence_remove_callback(struct dma_fence *fence,
 			       struct dma_fence_cb *cb);
-void dma_fence_enable_sw_signaling(struct dma_fence *fence);
+void dma_fence_enable_signaling(struct dma_fence *fence);
 
 /**
  * dma_fence_is_signaled_locked - Return an indication if the fence
Signature · 1 line
2.34.1
#12 lore raw

Re: [PATCH 7/7] dma-buf: rework the enable_signaling handling

kernel test robot noticed the following build errors:

Hi Christian,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm-xe/drm-xe-next]
[also build test ERROR on drm/drm-next drm-exynos/exynos-drm-next drm-misc/drm-misc-next drm-tip/drm-tip next-20240912] [cannot apply to drm-intel/for-linux-next drm-intel/for-linux-next-fixes linus/master v6.11-rc7]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Christian-K-nig/dma-buf-sw_sync-remove-pointless-enable_signaling-implementation/20240911-175943
base:   https://gitlab.freedesktop.org/drm/xe/kernel.git drm-xe-next
patch link:    https://lore.kernel.org/r/20240911085903.1496-8-christian.koenig%40amd.com
patch subject: [PATCH 7/7] dma-buf: rework the enable_signaling handling
config: loongarch-allmodconfig (https://download.01.org/0day-ci/archive/20240913/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 14.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240913/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c: In function 'dma_resv_kunit_active_fence_init':

>> drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c:72:9: error: implicit declaration of function 'dma_fence_enable_sw_signaling'; did you mean 'dma_fence_enable_signaling'? [-Wimplicit-function-declaration]
      72 |         dma_fence_enable_sw_signaling(fence);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |         dma_fence_enable_signaling

vim +72 drivers/gpu/drm/ttm/tests/ttm_bo_validate_test.c

8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  64  
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  65  static void dma_resv_kunit_active_fence_init(struct kunit *test,
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  66  					     struct dma_resv *resv,
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  67  					     enum dma_resv_usage usage)
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  68  {
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  69  	struct dma_fence *fence;
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  70  
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  71  	fence = alloc_mock_fence(test);
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12 @72  	dma_fence_enable_sw_signaling(fence);
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  73  
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  74  	dma_resv_lock(resv, NULL);
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  75  	dma_resv_reserve_fences(resv, 1);
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  76  	dma_resv_add_fence(resv, fence, usage);
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  77  	dma_resv_unlock(resv);
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  78  }
8eda41dfc9b2f0 Karolina Stolarek 2024-06-12  79  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer
#13 lore raw

Re: [PATCH 7/7] dma-buf: rework the enable_signaling handling

In reply to Christian König
Quoted 145 lines · Christian König

The enable_signaling callback is the only function the dma_fence objects calls with the fence lock held (the signaled callback might be called with the fence lock held as well, but that isn't guaranted).

The background of that decision was to avoid races with other CPUs trying to signal the fence at the same time and potentially enforce an ordering of fence signaling.

The only problem is that this never worked correctly.

First of all the enabling_signaling call can still race with signaling a fence, it's just that informing the installed callbacks is blocking for the enable signaling to finish. If that is required (radeon is an example of that) then drivers can still grab the fence themselves, everybody else doesn't need that.

Then regarding fence ordering it is perfectly possible that fences emitted in the order A,B,C call their installed callbacks in the order B, C, A. The background is that the optimization to signal fences from dma_fence_is_signaled() decouples the fence signaling from the interrupt handlers. The result is that fence C can signal because somebody queried it's state while A and B still wait for their interrupt to arrive.

While those two reasons are just unnecessary churn the documentation is simply erroneous and suggests an illegal operation to
implementations: "This function can be called from atomic context, but not from irq context, so normal spinlocks can be used.". Since the enable_signaling callback was called with interrupts disabled that practice could deadlock.

Furtunately nobody actually ran into problems with that, but considering that we should probably re-work the locking to allow dma_fence objects to exists after their drivers were unloaded this patch re-works all this to not call the callback with the dma_fence spinlock held and rather move the handling into the drivers which actually need it.

---
  drivers/dma-buf/dma-fence-array.c             |  7 +-
  drivers/dma-buf/dma-fence-chain.c             | 13 ++--
  drivers/dma-buf/dma-fence.c                   | 68 +++++++------------
  drivers/dma-buf/st-dma-fence-chain.c          |  4 +-
  drivers/dma-buf/st-dma-fence-unwrap.c         | 22 +++---
  drivers/dma-buf/st-dma-fence.c                | 16 ++---
  drivers/dma-buf/st-dma-resv.c                 | 10 +--
  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c  | 12 ++--
  drivers/gpu/drm/i915/i915_active.c            |  2 +-
  drivers/gpu/drm/i915/i915_request.c           | 12 +++-
  drivers/gpu/drm/nouveau/nouveau_fence.c       |  9 ++-
  drivers/gpu/drm/radeon/radeon_fence.c         | 17 +++--
  drivers/gpu/drm/ttm/ttm_bo.c                  |  2 +-
  drivers/gpu/drm/xe/xe_bo.c                    |  2 +-
  drivers/gpu/drm/xe/xe_hw_fence.c              |  4 +-
  drivers/gpu/drm/xe/xe_preempt_fence.c         |  3 +-
  drivers/gpu/drm/xe/xe_pt.c                    |  2 +-
  drivers/gpu/drm/xe/xe_sched_job.c             |  2 +-
  drivers/gpu/drm/xe/xe_vm.c                    |  6 +-
  drivers/gpu/host1x/fence.c                    | 14 ++--
  include/linux/dma-fence.h                     | 35 +++-------
  21 files changed, 123 insertions(+), 139 deletions(-)
drivers/dma-buf/dma-fence-array.c +3 -4
index c74ac197d5fe..1022b08c9b42 100644
@@ -67,7 +67,7 @@ static void dma_fence_array_cb_func(struct dma_fence *f,
  		dma_fence_put(&array->base);
  }
 
-static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
+static void dma_fence_array_enable_signaling(struct dma_fence *fence)
  {
  	struct dma_fence_array *array = to_dma_fence_array(fence);
  	struct dma_fence_array_cb *cb = array->callbacks;
@@ -92,12 +92,11 @@ static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
  			dma_fence_put(&array->base);
  			if (atomic_dec_and_test(&array->num_pending)) {
  				dma_fence_array_clear_pending_error(array);
-				return false;
+				dma_fence_signal(&array->base);
+				return;
  			}
  		}
  	}
-
-	return true;
  }
 
  static bool dma_fence_array_signaled(struct dma_fence *fence)
drivers/dma-buf/dma-fence-chain.c +5 -8
index 9663ba1bb6ac..f56baa214a6c 100644
@@ -9,7 +9,7 @@
 
  #include <linux/dma-fence-chain.h>
 
-static bool dma_fence_chain_enable_signaling(struct dma_fence *fence);
+static void dma_fence_chain_enable_signaling(struct dma_fence *fence);
 
  /**
   * dma_fence_chain_get_prev - use RCU to get a reference to the previous fence
@@ -125,10 +125,7 @@ static void dma_fence_chain_irq_work(struct irq_work *work)
 
  	chain = container_of(work, typeof(*chain), work);
 
-	/* Try to rearm the callback */
-	if (!dma_fence_chain_enable_signaling(&chain->base))
-		/* Ok, we are done. No more unsignaled fences left */
-		dma_fence_signal(&chain->base);
+	dma_fence_chain_enable_signaling(&chain->base);
  	dma_fence_put(&chain->base);
  }
 
@@ -142,7 +139,7 @@ static void dma_fence_chain_cb(struct dma_fence *f, struct dma_fence_cb *cb)
  	dma_fence_put(f);
  }
 
-static bool dma_fence_chain_enable_signaling(struct dma_fence *fence)
+static void dma_fence_chain_enable_signaling(struct dma_fence *fence)
  {
  	struct dma_fence_chain *head = to_dma_fence_chain(fence);
 
@@ -153,12 +150,12 @@ static bool dma_fence_chain_enable_signaling(struct dma_fence *fence)
  		dma_fence_get(f);
  		if (!dma_fence_add_callback(f, &head->cb, dma_fence_chain_cb)) {
  			dma_fence_put(fence);
-			return true;
+			return;
  		}
  		dma_fence_put(f);
  	}
  	dma_fence_put(&head->base);
-	return false;
+	dma_fence_signal(fence);
  }
 
  static bool dma_fence_chain_signaled(struct dma_fence *fence)
drivers/dma-buf/dma-fence.c +0 -0
index 325a263ac798..f0084a7aeebe 100644

drivers/dma-buf/dma-fence.c dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout) · line 512

Quoted 5 lines
drivers/dma-buf/dma-fence.c +1 -1
@@ -509,7 +509,7 @@ dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
 
  	__dma_fence_might_wait();
 
-	dma_fence_enable_sw_signaling(fence);
+	dma_fence_enable_signaling(fence);

I like the rename since sw in the name always annoyed me. What about splitting it out to a standalone patch though? If it comes first, this one gets a bit smaller and easier to review.

Quoted 71 lines
drivers/dma-buf/dma-fence.c +11 -29
  	trace_dma_fence_wait_start(fence);
  	if (fence->ops->wait)
@@ -576,47 +576,30 @@ void dma_fence_free(struct dma_fence *fence)
  }
  EXPORT_SYMBOL(dma_fence_free);
 
-static bool __dma_fence_enable_signaling(struct dma_fence *fence)
+/**
+ * dma_fence_enable_signaling - tell implementation that fence must signal
+ * @fence: the fence to enable
+ *
+ * This will request for signaling to be enabled, to make the fence complete
+ * as soon as possible. This calls &dma_fence_ops.enable_signaling internally.
+ */
+void dma_fence_enable_signaling(struct dma_fence *fence)
  {
  	bool was_set;
 
-	lockdep_assert_held(fence->lock);
-
  	was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
  				   &fence->flags);
 
  	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
-		return false;
+		return;
 
  	if (!was_set && fence->ops->enable_signaling) {
  		trace_dma_fence_enable_signal(fence);
 
-		if (!fence->ops->enable_signaling(fence)) {
-			dma_fence_signal_locked(fence);
-			return false;
-		}
+		fence->ops->enable_signaling(fence);
  	}
-
-	return true;
-}
-
-/**
- * dma_fence_enable_sw_signaling - enable signaling on fence
- * @fence: the fence to enable
- *
- * This will request for sw signaling to be enabled, to make the fence
- * complete as soon as possible. This calls &dma_fence_ops.enable_signaling
- * internally.
- */
-void dma_fence_enable_sw_signaling(struct dma_fence *fence)
-{
-	unsigned long flags;
-
-	spin_lock_irqsave(fence->lock, flags);
-	__dma_fence_enable_signaling(fence);
-	spin_unlock_irqrestore(fence->lock, flags);
  }
-EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
+EXPORT_SYMBOL(dma_fence_enable_signaling);
 
  /**
   * dma_fence_add_callback - add a callback to be called when the fence
@@ -644,29 +627,30 @@ int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
  			   dma_fence_func_t func)
  {
  	unsigned long flags;
-	int ret = 0;
 
  	if (WARN_ON(!fence || !func))
  		return -EINVAL;
 

drivers/dma-buf/dma-fence.c int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb, · line 639

Quoted 10 lines
drivers/dma-buf/dma-fence.c +4 -4
-	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
-		INIT_LIST_HEAD(&cb->node);
-		return -ENOENT;
-	}
+	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
+		goto out_signaled;
+
+	dma_fence_enable_signaling(fence);
 
  	spin_lock_irqsave(fence->lock, flags);

On a low-level, if time comes to that after the high-level discussion, this affectively drops and re-acquires the lock. Which raises questions about correctness and, if not that, why not optimize it.

Quoted 332 lines
drivers/dma-buf/dma-fence.c +10 -8
+	if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
+		goto out_unlock;
 
-	if (__dma_fence_enable_signaling(fence)) {
-		cb->func = func;
-		list_add_tail(&cb->node, &fence->cb_list);
-	} else {
-		INIT_LIST_HEAD(&cb->node);
-		ret = -ENOENT;
-	}
+	cb->func = func;
+	list_add_tail(&cb->node, &fence->cb_list);
+	spin_unlock_irqrestore(fence->lock, flags);
+	return 0;
 
+out_unlock:
  	spin_unlock_irqrestore(fence->lock, flags);
 
-	return ret;
+out_signaled:
+	INIT_LIST_HEAD(&cb->node);
+	return -ENOENT;
  }
  EXPORT_SYMBOL(dma_fence_add_callback);
 
drivers/dma-buf/st-dma-fence-chain.c +2 -2
index ed4b323886e4..455f6f6c3b90 100644
@@ -85,7 +85,7 @@ static int sanitycheck(void *arg)
 
  	chain = mock_chain(NULL, f, 1);
  	if (chain)
-		dma_fence_enable_sw_signaling(chain);
+		dma_fence_enable_signaling(chain);
  	else
  		err = -ENOMEM;
 
@@ -146,7 +146,7 @@ static int fence_chains_init(struct fence_chains *fc, unsigned int count,
 
  		fc->tail = fc->chains[i];
 
-		dma_fence_enable_sw_signaling(fc->chains[i]);
+		dma_fence_enable_signaling(fc->chains[i]);
  	}
 
  	fc->chain_length = i;
drivers/dma-buf/st-dma-fence-unwrap.c +11 -11
index f0cee984b6c7..1a6cef01fda9 100644
@@ -102,7 +102,7 @@ static int sanitycheck(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	array = mock_array(1, f);
  	if (!array)
@@ -126,7 +126,7 @@ static int unwrap_array(void *arg)
  	if (!f1)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
  	f2 = mock_fence();
  	if (!f2) {
@@ -134,7 +134,7 @@ static int unwrap_array(void *arg)
  		return -ENOMEM;
  	}
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
  	array = mock_array(2, f1, f2);
  	if (!array)
@@ -170,7 +170,7 @@ static int unwrap_chain(void *arg)
  	if (!f1)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
  	f2 = mock_fence();
  	if (!f2) {
@@ -178,7 +178,7 @@ static int unwrap_chain(void *arg)
  		return -ENOMEM;
  	}
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
  	chain = mock_chain(f1, f2);
  	if (!chain)
@@ -214,7 +214,7 @@ static int unwrap_chain_array(void *arg)
  	if (!f1)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
  	f2 = mock_fence();
  	if (!f2) {
@@ -222,7 +222,7 @@ static int unwrap_chain_array(void *arg)
  		return -ENOMEM;
  	}
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
  	array = mock_array(2, f1, f2);
  	if (!array)
@@ -262,7 +262,7 @@ static int unwrap_merge(void *arg)
  	if (!f1)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
  	f2 = mock_fence();
  	if (!f2) {
@@ -270,7 +270,7 @@ static int unwrap_merge(void *arg)
  		goto error_put_f1;
  	}
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
  	f3 = dma_fence_unwrap_merge(f1, f2);
  	if (!f3) {
@@ -314,13 +314,13 @@ static int unwrap_merge_complex(void *arg)
  	if (!f1)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f1);
+	dma_fence_enable_signaling(f1);
 
  	f2 = mock_fence();
  	if (!f2)
  		goto error_put_f1;
 
-	dma_fence_enable_sw_signaling(f2);
+	dma_fence_enable_signaling(f2);
 
  	f3 = dma_fence_unwrap_merge(f1, f2);
  	if (!f3)
drivers/dma-buf/st-dma-fence.c +8 -8
index 6a1bfcd0cc21..eb740b4fe4fe 100644
@@ -102,7 +102,7 @@ static int sanitycheck(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	dma_fence_signal(f);
  	dma_fence_put(f);
@@ -119,7 +119,7 @@ static int test_signaling(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	if (dma_fence_is_signaled(f)) {
  		pr_err("Fence unexpectedly signaled on creation\n");
@@ -194,7 +194,7 @@ static int test_late_add_callback(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	dma_fence_signal(f);
 
@@ -288,7 +288,7 @@ static int test_status(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	if (dma_fence_get_status(f)) {
  		pr_err("Fence unexpectedly has signaled status on creation\n");
@@ -316,7 +316,7 @@ static int test_error(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	dma_fence_set_error(f, -EIO);
 
@@ -347,7 +347,7 @@ static int test_wait(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	if (dma_fence_wait_timeout(f, false, 0) != -ETIME) {
  		pr_err("Wait reported complete before being signaled\n");
@@ -391,7 +391,7 @@ static int test_wait_timeout(void *arg)
  	if (!wt.f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(wt.f);
+	dma_fence_enable_signaling(wt.f);
 
  	if (dma_fence_wait_timeout(wt.f, false, 1) != -ETIME) {
  		pr_err("Wait reported complete before being signaled\n");
@@ -472,7 +472,7 @@ static int thread_signal_callback(void *arg)
  			break;
  		}
 
-		dma_fence_enable_sw_signaling(f1);
+		dma_fence_enable_signaling(f1);
 
  		rcu_assign_pointer(t->fences[t->id], f1);
  		smp_wmb();
drivers/dma-buf/st-dma-resv.c +5 -5
index 15dbea1462ed..f030b43ee4da 100644
@@ -45,7 +45,7 @@ static int sanitycheck(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	dma_fence_signal(f);
  	dma_fence_put(f);
@@ -71,7 +71,7 @@ static int test_signaling(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	dma_resv_init(&resv);
  	r = dma_resv_lock(&resv, NULL);
@@ -118,7 +118,7 @@ static int test_for_each(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	dma_resv_init(&resv);
  	r = dma_resv_lock(&resv, NULL);
@@ -179,7 +179,7 @@ static int test_for_each_unlocked(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	dma_resv_init(&resv);
  	r = dma_resv_lock(&resv, NULL);
@@ -252,7 +252,7 @@ static int test_get_fences(void *arg)
  	if (!f)
  		return -ENOMEM;
 
-	dma_fence_enable_sw_signaling(f);
+	dma_fence_enable_signaling(f);
 
  	dma_resv_init(&resv);
  	r = dma_resv_lock(&resv, NULL);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_fence.c +6 -6
index 1ef758ac5076..9ec4daa23665 100644
@@ -116,24 +116,24 @@ static const char *amdkfd_fence_get_timeline_name(struct dma_fence *f)
   *
   *  @f: dma_fence
   */
-static bool amdkfd_fence_enable_signaling(struct dma_fence *f)
+static void amdkfd_fence_enable_signaling(struct dma_fence *f)
  {
  	struct amdgpu_amdkfd_fence *fence = to_amdgpu_amdkfd_fence(f);
 
  	if (!fence)
-		return false;
+		return;
 
  	if (dma_fence_is_signaled(f))
-		return true;
+		return;
 
  	if (!fence->svm_bo) {
  		if (!kgd2kfd_schedule_evict_and_restore_process(fence->mm, f))
-			return true;
+			return;
  	} else {
  		if (!svm_range_schedule_evict_svm_bo(fence))
-			return true;
+			return;
  	}
-	return false;
+	dma_fence_signal(f);
  }
 
  /**
drivers/gpu/drm/i915/i915_active.c +1 -1
index 5ec293011d99..3f2425966263 100644
@@ -562,7 +562,7 @@ static void enable_signaling(struct i915_active_fence *active)
  	if (!fence)
  		return;
 
-	dma_fence_enable_sw_signaling(fence);
+	dma_fence_enable_signaling(fence);
  	dma_fence_put(fence);
  }
 
drivers/gpu/drm/i915/i915_request.c +1 -1
index 519e096c607c..db4891c7cc7b 100644
@@ -90,9 +90,17 @@ static bool i915_fence_signaled(struct dma_fence *fence)
  	return i915_request_completed(to_request(fence));
  }
 
-static bool i915_fence_enable_signaling(struct dma_fence *fence)
+static void i915_fence_enable_signaling(struct dma_fence *fence)
  {

drivers/gpu/drm/i915/i915_request.c static bool i915_fence_signaled(struct dma_fence *fence) · line 103

Quoted 10 lines
drivers/gpu/drm/i915/i915_request.c +9 -1
-	return i915_request_enable_breadcrumb(to_request(fence));
+	struct i915_request *rq = to_request(fence);
+	unsigned long flags;
+	bool ret;
+
+	spin_lock_irqsave(&rq->lock, flags);
+	ret = i915_request_enable_breadcrumb(to_request(fence));
+	spin_unlock_irqrestore(&rq->lock, flags);
+	if (!ret)
+		dma_fence_signal(fence);

I didn't immediately pick up where do you add (document?) the requirement to signal from enable signaling?

Also, again on a low level, here too would be nicer if re-lock dance was avoided here (and in the other call backs).

Quoted 255 lines
drivers/gpu/drm/i915/i915_request.c +0 -0
  }
 
  static signed long i915_fence_wait(struct dma_fence *fence,
drivers/gpu/drm/nouveau/nouveau_fence.c +6 -3
index 61d193e41f8c..1ad692a033c0 100644
@@ -503,10 +503,11 @@ static const struct dma_fence_ops nouveau_fence_ops_legacy = {
  	.release = nouveau_fence_release
  };
 
-static bool nouveau_fence_enable_signaling(struct dma_fence *f)
+static void nouveau_fence_enable_signaling(struct dma_fence *f)
  {
  	struct nouveau_fence *fence = from_fence(f);
  	struct nouveau_fence_chan *fctx = nouveau_fctx(fence);
+	unsigned long flags;
 
  	/*
  	 * caller should have a reference on the fence,
@@ -514,6 +515,7 @@ static bool nouveau_fence_enable_signaling(struct dma_fence *f)
  	 */
  	WARN_ON(kref_read(&fence->base.refcount) <= 1);
 
+	spin_lock_irqsave(&fctx->lock, flags);
  	if (!fctx->notify_ref++)
  		nvif_event_allow(&fctx->event);
 
@@ -523,10 +525,11 @@ static bool nouveau_fence_enable_signaling(struct dma_fence *f)
  		dma_fence_put(&fence->base);
  		if (!--fctx->notify_ref)
  			nvif_event_block(&fctx->event);
-		return false;
+		spin_unlock_irqrestore(&fctx->lock, flags);
+		dma_fence_signal(f);
  	} else {
  		set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags);
-		return true;
+		spin_unlock_irqrestore(&fctx->lock, flags);
  	}
  }
 
drivers/gpu/drm/radeon/radeon_fence.c +12 -5
index daff61586be5..fd1d617c3331 100644
@@ -378,14 +378,19 @@ static bool radeon_fence_is_signaled(struct dma_fence *f)
   * to fence_queue that checks if this fence is signaled, and if so it
   * signals the fence and removes itself.
   */
-static bool radeon_fence_enable_signaling(struct dma_fence *f)
+static void radeon_fence_enable_signaling(struct dma_fence *f)
  {
  	struct radeon_fence *fence = to_radeon_fence(f);
  	struct radeon_device *rdev = fence->rdev;
+	unsigned long flags;
 
-	if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq)
-		return false;
+	if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq)
+	    >= fence->seq) {
+		dma_fence_signal(f);
+		return;
+	}
 
+	spin_lock_irqsave(&rdev->fence_queue.lock, flags);
  	if (down_read_trylock(&rdev->exclusive_lock)) {
  		radeon_irq_kms_sw_irq_get(rdev, fence->ring);
 
@@ -396,7 +401,9 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
  		if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq) {
  			radeon_irq_kms_sw_irq_put(rdev, fence->ring);
  			up_read(&rdev->exclusive_lock);
-			return false;
+			spin_unlock_irqrestore(&rdev->fence_queue.lock, flags);
+			dma_fence_signal(f);
+			return;
  		}
 
  		up_read(&rdev->exclusive_lock);
@@ -412,7 +419,7 @@ static bool radeon_fence_enable_signaling(struct dma_fence *f)
  	fence->fence_wake.func = radeon_fence_check_signaled;
  	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
  	dma_fence_get(f);
-	return true;
+	spin_unlock_irqrestore(&rdev->fence_queue.lock, flags);
  }
 
  /**
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
index 320592435252..9b060a82c6df 100644
@@ -219,7 +219,7 @@ static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo)
  	dma_resv_iter_begin(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP);
  	dma_resv_for_each_fence_unlocked(&cursor, fence) {
  		if (!fence->ops->signaled)
-			dma_fence_enable_sw_signaling(fence);
+			dma_fence_enable_signaling(fence);
  	}
  	dma_resv_iter_end(&cursor);
  }
drivers/gpu/drm/xe/xe_bo.c +1 -1
index 25d0c939ba31..3a1840560ef9 100644
@@ -495,7 +495,7 @@ static int xe_bo_trigger_rebind(struct xe_device *xe, struct xe_bo *bo,
  		dma_resv_iter_begin(&cursor, bo->ttm.base.resv,
  				    DMA_RESV_USAGE_BOOKKEEP);
  		dma_resv_for_each_fence_unlocked(&cursor, fence)
-			dma_fence_enable_sw_signaling(fence);
+			dma_fence_enable_signaling(fence);
  		dma_resv_iter_end(&cursor);
  	}
 
drivers/gpu/drm/xe/xe_hw_fence.c +1 -3
index 0b4f12be3692..77c5b16f835d 100644
@@ -168,7 +168,7 @@ static bool xe_hw_fence_signaled(struct dma_fence *dma_fence)
  		!__dma_fence_is_later(dma_fence->seqno, seqno, dma_fence->ops);
  }
 
-static bool xe_hw_fence_enable_signaling(struct dma_fence *dma_fence)
+static void xe_hw_fence_enable_signaling(struct dma_fence *dma_fence)
  {
  	struct xe_hw_fence *fence = to_xe_hw_fence(dma_fence);
  	struct xe_hw_fence_irq *irq = xe_hw_fence_irq(fence);
@@ -179,8 +179,6 @@ static bool xe_hw_fence_enable_signaling(struct dma_fence *dma_fence)
  	/* SW completed (no HW IRQ) so kick handler to signal fence */
  	if (xe_hw_fence_signaled(dma_fence))
  		xe_hw_fence_irq_run(irq);
-
-	return true;
  }
 
  static void xe_hw_fence_release(struct dma_fence *dma_fence)
drivers/gpu/drm/xe/xe_preempt_fence.c +1 -2
index 83fbeea5aa20..963af6825875 100644
@@ -56,7 +56,7 @@ preempt_fence_get_timeline_name(struct dma_fence *fence)
  	return "preempt";
  }
 
-static bool preempt_fence_enable_signaling(struct dma_fence *fence)
+static void preempt_fence_enable_signaling(struct dma_fence *fence)
  {
  	struct xe_preempt_fence *pfence =
  		container_of(fence, typeof(*pfence), base);
@@ -64,7 +64,6 @@ static bool preempt_fence_enable_signaling(struct dma_fence *fence)
 
  	pfence->error = q->ops->suspend(q);
  	queue_work(q->vm->xe->preempt_fence_wq, &pfence->preempt_work);
-	return true;
  }
 
  static const struct dma_fence_ops preempt_fence_ops = {
drivers/gpu/drm/xe/xe_pt.c +1 -1
index 579ed31b46db..7556bc45e592 100644
@@ -1236,7 +1236,7 @@ static int vma_check_userptr(struct xe_vm *vm, struct xe_vma *vma,
  			dma_resv_iter_begin(&cursor, xe_vm_resv(vm),
  					    DMA_RESV_USAGE_BOOKKEEP);
  			dma_resv_for_each_fence_unlocked(&cursor, fence)
-				dma_fence_enable_sw_signaling(fence);
+				dma_fence_enable_signaling(fence);
  			dma_resv_iter_end(&cursor);
 
  			err = dma_resv_wait_timeout(xe_vm_resv(vm),
drivers/gpu/drm/xe/xe_sched_job.c +1 -1
index 55d47450b2c6..fe2d5edac92d 100644
@@ -210,7 +210,7 @@ void xe_sched_job_set_error(struct xe_sched_job *job, int error)
 
  	trace_xe_sched_job_set_error(job);
 
-	dma_fence_enable_sw_signaling(job->fence);
+	dma_fence_enable_signaling(job->fence);
  	xe_hw_fence_irq_run(job->q->fence_irq);
  }
 
drivers/gpu/drm/xe/xe_vm.c +3 -3
index 3eb76d874eb2..b33636976669 100644
@@ -259,7 +259,7 @@ int xe_vm_add_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
  	 */
  	wait = __xe_vm_userptr_needs_repin(vm) || preempt_fences_waiting(vm);
  	if (wait)
-		dma_fence_enable_sw_signaling(pfence);
+		dma_fence_enable_signaling(pfence);
 
  	up_read(&vm->userptr.notifier_lock);
 
@@ -289,7 +289,7 @@ void xe_vm_remove_compute_exec_queue(struct xe_vm *vm, struct xe_exec_queue *q)
  		--vm->preempt.num_exec_queues;
  	}
  	if (q->lr.pfence) {
-		dma_fence_enable_sw_signaling(q->lr.pfence);
+		dma_fence_enable_signaling(q->lr.pfence);
  		dma_fence_put(q->lr.pfence);
  		q->lr.pfence = NULL;
  	}
@@ -634,7 +634,7 @@ static bool vma_userptr_invalidate(struct mmu_interval_notifier *mni,
  	dma_resv_iter_begin(&cursor, xe_vm_resv(vm),
  			    DMA_RESV_USAGE_BOOKKEEP);
  	dma_resv_for_each_fence_unlocked(&cursor, fence)
-		dma_fence_enable_sw_signaling(fence);
+		dma_fence_enable_signaling(fence);
  	dma_resv_iter_end(&cursor);
 
  	err = dma_resv_wait_timeout(xe_vm_resv(vm),
drivers/gpu/host1x/fence.c +9 -5
index 139ad1afd935..0581aa23cd41 100644
@@ -30,12 +30,17 @@ static struct host1x_syncpt_fence *to_host1x_fence(struct dma_fence *f)
  	return container_of(f, struct host1x_syncpt_fence, base);
  }
 
-static bool host1x_syncpt_fence_enable_signaling(struct dma_fence *f)
+static void host1x_syncpt_fence_enable_signaling(struct dma_fence *f)
  {
  	struct host1x_syncpt_fence *sf = to_host1x_fence(f);
+	unsigned long flags;
 
-	if (host1x_syncpt_is_expired(sf->sp, sf->threshold))
-		return false;
+	spin_lock_irqsave(f->lock, flags);
+	if (host1x_syncpt_is_expired(sf->sp, sf->threshold)) {
+		spin_unlock_irqrestore(f->lock, flags);
+		dma_fence_signal(f);
+		return;
+	}
 
  	/* Reference for interrupt path. */
  	dma_fence_get(f);
@@ -62,8 +67,7 @@ static bool host1x_syncpt_fence_enable_signaling(struct dma_fence *f)
  	 * so we need to initialize all state used by signalling
  	 * before it.
  	 */
-
-	return true;
+	spin_unlock_irqrestore(f->lock, flags);
  }
 
  static const struct dma_fence_ops host1x_syncpt_fence_ops = {
include/linux/dma-fence.h +0 -0
index e7ad819962e3..93b6176bbf48 100644

include/linux/dma-fence.h struct dma_fence_ops { · line 161

Quoted 5 lines
include/linux/dma-fence.h +1 -1
@@ -158,39 +158,24 @@ struct dma_fence_ops {
  	/**
  	 * @enable_signaling:
  	 *
-	 * Enable software signaling of fence.
+	 * Guarantee that installed callbacks are called at some point.

This is one of the big/key changes, right? I would suggest to improve the commit message to talk about its what why and how directly and explicity.

I am not sure how it guarantees it when it is left to driver implementations? That is, dma_fence_signal_locked is removed from dma_fence_enable_signaling.

Or you mean because you added dma_fence_enable_signaling to dma_fence_add_callback?

But then there is a lock re-acquire there and if signal raced "correctly" with dma_fence_add_callback, callback may not get called.

Quoted 6 lines
include/linux/dma-fence.h +0 -5
  	 *
-	 * For fence implementations that have the capability for hw->hw
-	 * signaling, they can implement this op to enable the necessary
-	 * interrupts, or insert commands into cmdstream, etc, to avoid these
-	 * costly operations for the common case where only hw->hw
-	 * synchronization is required.  This is called in the first

include/linux/dma-fence.h struct dma_fence_ops { · line 169

Quoted 10 lines
include/linux/dma-fence.h +4 -3
-	 * dma_fence_wait() or dma_fence_add_callback() path to let the fence
-	 * implementation know that there is another driver waiting on the
-	 * signal (ie. hw->sw case).
+	 * Drivers can implement this op to enable the necessary interrupts or
+	 * tell the hardware through other means that somebody is requesting
+	 * this fence to signal. This is called in the first dma_fence_wait()
+	 * or dma_fence_add_callback() path.
  	 *
  	 * This function can be called from atomic context, but not
  	 * from irq context, so normal spinlocks can be used.

I thought commit message stated this paragraph was wrong. Or it becomes right after this patch?

Regards,

Tvrtko

Quoted 36 lines
include/linux/dma-fence.h +5 -16
  	 *
-	 * A return value of false indicates the fence already passed,
-	 * or some failure occurred that made it impossible to enable
-	 * signaling. True indicates successful enabling.
-	 *
-	 * &dma_fence.error may be set in enable_signaling, but only when false
-	 * is returned.
-	 *
-	 * Since many implementations can call dma_fence_signal() even when before
-	 * @enable_signaling has been called there's a race window, where the
-	 * dma_fence_signal() might result in the final fence reference being
-	 * released and its memory freed. To avoid this, implementations of this
-	 * callback should grab their own reference using dma_fence_get(), to be
-	 * released when the fence is signalled (through e.g. the interrupt
-	 * handler).
+	 * dma_fence_set_error() as well as dma_fence_signal() may be used in
+	 * enable_signaling if the implementation detects an error or that the
+	 * fence already completed succesfully.
  	 *
  	 * This callback is optional. If this callback is not present, then the
  	 * driver must always have signaling enabled.
  	 */
-	bool (*enable_signaling)(struct dma_fence *fence);
+	void (*enable_signaling)(struct dma_fence *fence);
 
  	/**
  	 * @signaled:
@@ -400,7 +385,7 @@ int dma_fence_add_callback(struct dma_fence *fence,
  			   dma_fence_func_t func);
  bool dma_fence_remove_callback(struct dma_fence *fence,
  			       struct dma_fence_cb *cb);
-void dma_fence_enable_sw_signaling(struct dma_fence *fence);
+void dma_fence_enable_signaling(struct dma_fence *fence);
 
  /**
   * dma_fence_is_signaled_locked - Return an indication if the fence