Skip to contentSkip to conversation

dma-buf/dma_fence: Make races for dma_fence_is_signaled() less likely

RFC PATCH In review Philipp Stanner 6 participants 9 messages Jun 12 – Jun 15

Subsystems (2)
lore
Share & tools
lore

Philipp Stanner

author maintainer
#0 lore raw

RFC PATCH 1/1 dma-buf/dma_fence: Make races for dma_fence_is_signaled() less likely

dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression.

This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in:

commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race")

Make this race impossible, by simply setting the bit only once the callbacks are actually completed.

---
 drivers/dma-buf/dma-fence.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +16 -2
index c7ea1e75d38a..2416cc86ce93 100644
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 
 	dma_fence_assert_held(fence);
 
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
 		return;
 
 	trace_dma_fence_signaled(fence);
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 		INIT_LIST_HEAD(&cur->node);
 		cur->func(fence, cur);
 	}
+
+	// TODO: we need some barrier here, don't we?
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
 }
 EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
 
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] Moving the signaled bit update creates a window where `fence->ops` is cleared to NULL but the…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] Moving the signaled bit update creates a window where fence->ops is cleared to NULL but the fence is not yet marked as signaled, causing a NULL pointer dereference in lockless accessors.
- [High] Missing release memory barrier before setting the signaled bit, defeating the purpose of the patch on weakly-ordered architectures and risking use-after-free.
- [High] Delaying the signaled bit breaks the fast-path lockless check in dma_fence_is_signaled(), leading to lock contention, recursive deadlocks, and API contract violations. --

commit 0ba6de5d00b831f763affe10965d5c591e6f0b5f
Author: Philipp Stanner <[email protected]>

dma-buf/dma_fence: Make races for dma_fence_is_signaled() less likely

This commit addresses a race condition where dma_fence_is_signaled() can return true while fence callbacks are still executing. It delays setting the DMA_FENCE_FLAG_SIGNALED_BIT until after the callbacks have completed, instead of setting it at the beginning of dma_fence_signal_timestamp_locked().

Quoted 15 lines
drivers/dma-buf/dma-fence.c +6 -2
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 
 	dma_fence_assert_held(fence);
 
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in

drivers/dma-buf/dma-fence.c void dma_fence_signal_timestamp_locked(struct dma_fence *fence, · line 377

Quoted 10 lines
drivers/dma-buf/dma-fence.c +7 -0
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
 		return;
 
 	trace_dma_fence_signaled(fence);

[Severity: Critical]
Because the signaled bit is no longer set here, is there a risk of a NULL pointer dereference for concurrent lockless readers?

Just below this in dma_fence_signal_timestamp_locked(), fence->ops can be cleared:

    ops = rcu_dereference_protected(fence->ops, true);
    if (!ops->release && !ops->wait)
        RCU_INIT_POINTER(fence->ops, NULL);

With the signaled bit still false, could lockless accessors like dma_fence_driver_name() see the unsignaled state and evaluate ops->get_driver_name(fence) on the NULL ops pointer?

[ ... ]

drivers/dma-buf/dma-fence.c void dma_fence_signal_timestamp_locked(struct dma_fence *fence, · line 397

Quoted 4 lines
drivers/dma-buf/dma-fence.c +0 -0
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 		INIT_LIST_HEAD(&cur->node);
 		cur->func(fence, cur);
 	}

[Severity: High]
If a callback executes while the signaled bit is still false, won't this break the lockless fast-path check in dma_fence_is_signaled()?

If dma_fence_is_signaled() or dma_fence_timestamp() is called from within a callback, the unsignaled state would cause dma_fence_timestamp() to hit a WARN_ON(!test_bit(...)).

Additionally, dma_fence_is_signaled() would fall back to dma_fence_signal(), which attempts to acquire the fence->lock. Since the lock is already held by the current thread executing the callbacks, would this result in a recursive deadlock?

drivers/dma-buf/dma-fence.c void dma_fence_signal_timestamp_locked(struct dma_fence *fence, · line 400

Quoted 3 lines
drivers/dma-buf/dma-fence.c +3 -0
+
+	// TODO: we need some barrier here, don't we?
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);

[Severity: High]
Does this missing memory barrier reintroduce the use-after-free risk on weakly-ordered architectures?

Since set_bit() does not imply a memory barrier, could the CPU reorder this assignment to become globally visible before the memory writes performed by the callbacks?

If a concurrent lockless reader using dma_fence_test_signaled_flag() observes the bit as set but reads stale data, this might defeat the purpose of the patch. Is an explicit smp_mb__before_atomic() needed here?

Quoted 2 lines
drivers/dma-buf/dma-fence.c +0 -0
 }
 EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Philipp Stanner
Quoted 5 lines · Philipp Stanner

dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression.

Quoted 8 lines

This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in:

commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race")

Make this race impossible, by simply setting the bit only once the callbacks are actually completed.

Groundhog day, that has been suggested before and it simply doesn't work.

The flag is intentional set before calling the callbacks because the state needs to be visible.

Just see dma_fence_default_wait() for an example why that approach doesn't work.

Regards,
Christian.

Quoted 42 lines
---
 drivers/dma-buf/dma-fence.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +16 -2
index c7ea1e75d38a..2416cc86ce93 100644
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 
 	dma_fence_assert_held(fence);
 
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
 		return;
 
 	trace_dma_fence_signaled(fence);
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 		INIT_LIST_HEAD(&cur->node);
 		cur->func(fence, cur);
 	}
+
+	// TODO: we need some barrier here, don't we?
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
 }
 EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
 
#6 lore raw
In reply to Philipp Stanner
Quoted 46 lines · Philipp Stanner

dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression.

This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in:

commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race")

Make this race impossible, by simply setting the bit only once the callbacks are actually completed.

---
 drivers/dma-buf/dma-fence.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +13 -2
index c7ea1e75d38a..2416cc86ce93 100644
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 
 	dma_fence_assert_held(fence);
 
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
 		return;
 
 	trace_dma_fence_signaled(fence);

drivers/dma-buf/dma-fence.c void dma_fence_signal_timestamp_locked(struct dma_fence *fence, · line 399

Quoted 5 lines
drivers/dma-buf/dma-fence.c +2 -0
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 		INIT_LIST_HEAD(&cur->node);
 		cur->func(fence, cur);
 	}
+
+	// TODO: we need some barrier here, don't we?

You want a release barrier here, which we don't have currently, but you can use smp_mb__before_atomic.

However note that barriers must be paired, so in order for the added barrier to do anything, dma_fence_is_signaled lockless fast path would need to have an acquire barrier, which we also don't have, so you need a smp_mb() there after the flag test.

Best,
Gary

Quoted 4 lines
drivers/dma-buf/dma-fence.c +1 -0
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
 }
 EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
 
#3 lore raw
In reply to Christian König
Quoted 10 lines · Christian König
Quoted 9 lines · Philipp Stanner

dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression.

This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in:

Quoted 8 lines
Quoted 4 lines

commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race")

Make this race impossible, by simply setting the bit only once the callbacks are actually completed.

Groundhog day, that has been suggested before and it simply doesn't work.

The flag is intentional set before calling the callbacks because the state needs to be visible.

It will be visible. Just later.

Quoted 2 lines

Just see dma_fence_default_wait() for an example why that approach doesn't work.

What's the issue? It will be set. Just later. Who is ordering with whom?

I BTW suggest to write more code comments in the future to document all these supposed pitfalls for those who will hack on that code base once we have left.

P.

Quoted 46 lines

Regards,
Christian.

Quoted 42 lines

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

 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +0 -0
index c7ea1e75d38a..2416cc86ce93 100644
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,


  dma_fence_assert_held(fence);

drivers/dma-buf/dma-fence.c +13 -2
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))

  return;

  trace_dma_fence_signaled(fence);

drivers/dma-buf/dma-fence.c +3 -0
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
  		INIT_LIST_HEAD(&cur->node);
  		cur->func(fence, cur);
  	}
+
+	// TODO: we need some barrier here, don't we?
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
  }
  EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);

maintainer
#4 lore raw
Quoted 13 lines · Philipp Stanner
Quoted 12 lines · Christian König
Quoted 11 lines · Philipp Stanner

dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression.

This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in:

commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race")

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Make this race impossible, by simply setting the bit only once the callbacks are actually completed.

Groundhog day, that has been suggested before and it simply doesn't work.

The flag is intentional set before calling the callbacks because the state needs to be visible.

It will be visible. Just later.

It must be visible *before* the callbacks are called. The whole idea with the callbacks is that you can install a notification of state change.

Quoted 4 lines
Quoted 1 line

Just see dma_fence_default_wait() for an example why that approach doesn't work.

What's the issue? It will be set. Just later. Who is ordering with whom?

See the functions dma_fence_default_wait() and dma_fence_default_wait_cb().

It wakes up the sleeping thread which in turn needs to observes the new state.

Regards,
Christian.

Quoted 53 lines

I BTW suggest to write more code comments in the future to document all these supposed pitfalls for those who will hack on that code base once we have left.

P.

Quoted 46 lines

Regards,
Christian.

Quoted 42 lines

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

 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +0 -0
index c7ea1e75d38a..2416cc86ce93 100644
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,


  dma_fence_assert_held(fence);

drivers/dma-buf/dma-fence.c +13 -2
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))

  return;

  trace_dma_fence_signaled(fence);

drivers/dma-buf/dma-fence.c +3 -0
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
  		INIT_LIST_HEAD(&cur->node);
  		cur->func(fence, cur);
  	}
+
+	// TODO: we need some barrier here, don't we?
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
  }
  EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);

#5 lore raw
Quoted 25 lines · Christian König
Quoted 13 lines · Philipp Stanner
Quoted 12 lines · Christian König
Quoted 11 lines · Philipp Stanner

dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression.

This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in:

commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race")

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Make this race impossible, by simply setting the bit only once the callbacks are actually completed.

Groundhog day, that has been suggested before and it simply doesn't work.

The flag is intentional set before calling the callbacks because the state needs to be visible.

It will be visible. Just later.

It must be visible *before* the callbacks are called. The whole idea with the callbacks is that you can install a notification of state change.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Just see dma_fence_default_wait() for an example why that approach doesn't work.

What's the issue? It will be set. Just later. Who is ordering with whom?

See the functions dma_fence_default_wait() and dma_fence_default_wait_cb().

It wakes up the sleeping thread which in turn needs to observes the new state.

dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)

{
	struct default_wait_cb cb;
	unsigned long flags;
	signed long ret = timeout ? timeout : 1;

	dma_fence_lock_irqsave(fence, flags);   // <------- cool, a lock! ^_^

	if (dma_fence_test_signaled_flag(fence))
		goto out;

	if (intr && signal_pending(current)) {
		ret = -ERESTARTSYS;
		goto out;
	}

	if (!timeout) {
		ret = 0;
		goto out;
	}

	cb.base.func = dma_fence_default_wait_cb;
	cb.task = current;
	list_add(&cb.base.node, &fence->cb_list);    // <--------------- guarded by lock

	while (!dma_fence_test_signaled_flag(fence) && ret > 0) { // <-------- flag-check is guarded by lock. Fully ordered. Doesn't matter where
		if (intr)                                                      dma_fence_signal_timeout_locked() sets the bit.
			__set_current_state(TASK_INTERRUPTIBLE);
		else
			__set_current_state(TASK_UNINTERRUPTIBLE);
		dma_fence_unlock_irqrestore(fence, flags); // <------------ dma_fence_signal_timeout_locked() can change the flag only after here

		ret = schedule_timeout(ret);

		dma_fence_lock_irqsave(fence, flags);
		if (ret > 0 && intr && signal_pending(current))
			ret = -ERESTARTSYS;
	}

P.

Quoted 57 lines

Regards,
Christian.

Quoted 53 lines

I BTW suggest to write more code comments in the future to document all these supposed pitfalls for those who will hack on that code base once we have left.

P.

Quoted 46 lines

Regards,
Christian.

Quoted 42 lines

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

 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +0 -0
index c7ea1e75d38a..2416cc86ce93 100644
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,


  dma_fence_assert_held(fence);

drivers/dma-buf/dma-fence.c +13 -2
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))

  return;

  trace_dma_fence_signaled(fence);

drivers/dma-buf/dma-fence.c +3 -0
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
  		INIT_LIST_HEAD(&cur->node);
  		cur->func(fence, cur);
  	}
+
+	// TODO: we need some barrier here, don't we?
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
  }
  EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);

#8 lore raw
In reply to Philipp Stanner
Quoted 5 lines · Philipp Stanner

dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression.

Quoted 8 lines

This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in:

commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race")

Make this race impossible, by simply setting the bit only once the callbacks are actually completed.

My 2c is that I would be wary of trying to turn around the semantics of the callbacks. They are not "run these to make the fence signaled" but instead "let me know please when fence is signaled". I do not see a good justification for the change.

The bugs such as above quoted c8a5d5ea3ba6 are in fact more about nouveau itself pulling data underneath fences with callbacks and not a bug in dma-fence. With the opportunistic cleanup it is doing in nouveau_cli_work() perhaps it would want to use dma_fence_remove_callback() to appear somewhat sensible, although that too may be racy, I did not think about it. Anyway, I've sent a patch to make that one clearer and more documented, even faster for the unsignaled case.

Regards,

Tvrtko

Quoted 42 lines
---
  drivers/dma-buf/dma-fence.c | 18 ++++++++++++++++--
  1 file changed, 16 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +16 -2
index c7ea1e75d38a..2416cc86ce93 100644
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 
  	dma_fence_assert_held(fence);
 
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
  		return;
 
  	trace_dma_fence_signaled(fence);
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
  		INIT_LIST_HEAD(&cur->node);
  		cur->func(fence, cur);
  	}
+
+	// TODO: we need some barrier here, don't we?
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
  }
  EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);
 
#7 lore raw
In reply to Gary Guo
Quoted 56 lines · Gary Guo
Quoted 46 lines · Philipp Stanner

dma_fence_is_signaled() returns whether a fence has been signaled already. That function contains a fast path opportunistic check which is not guarded by the lock and, according to Christian, cannot be guarded by the lock without causing a massive performance regression.

This now means that dma_fence_is_signaled() can return true WHILE the fence callbacks are still being executed. This is razy and has lead to at least one bug solved in:

commit c8a5d5ea3ba6 ("nouveau: fix client work fence deletion race")

Make this race impossible, by simply setting the bit only once the callbacks are actually completed.

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

 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/dma-buf/dma-fence.c +0 -0
index c7ea1e75d38a..2416cc86ce93 100644
@@ -359,8 +359,19 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,


  dma_fence_assert_held(fence);

drivers/dma-buf/dma-fence.c +13 -2
-	if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
-				      &fence->flags)))
+	/*
+	 * First test the bit, so we don't signal an already signaled fence again.
+	 * The lock protects against multiple parties setting the bit. The bit
+	 * is then set at the end of the function.
+	 *
+	 * The background is that there is a fast path check in
+	 * dma_fence_is_signaled() which does not use lock protection and can
+	 * return true *while* the fence callbacks are still executing.
+	 *
+	 * This fast path check supposedly cannot be guarded by the lock because
+	 * of significant performance regressions.
+	 */
+	if (unlikely(test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))

  return;

  trace_dma_fence_signaled(fence);

drivers/dma-buf/dma-fence.c void dma_fence_signal_timestamp_locked(struct dma_fence *fence, · line 399

Quoted 5 lines
drivers/dma-buf/dma-fence.c +2 -0
@@ -384,6 +395,9 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
  		INIT_LIST_HEAD(&cur->node);
  		cur->func(fence, cur);
  	}
+
+	// TODO: we need some barrier here, don't we?

You want a release barrier here, which we don't have currently, but you can use smp_mb__before_atomic.

Quoted 4 lines

However note that barriers must be paired, so in order for the added barrier to do anything, dma_fence_is_signaled lockless fast path would need to have an acquire barrier, which we also don't have, so you need a smp_mb() there after the flag test.

Thx for the explanations.

I tend to conclude that this solution is far too fragile and without even further synchronization we couldn't solve that, because the fence-

Quoted 1 line

signaled is at other places evaluated locklessly

const char __rcu *dma_fence_timeline_name(struct dma_fence *fence)

{
	const struct dma_fence_ops *ops;

	/* RCU protection is required for safe access to returned string */
	ops = rcu_dereference(fence->ops);
	if (!dma_fence_test_signaled_flag(fence))
		return (const char __rcu *)ops->get_driver_name(fence);
	else
		return (const char __rcu *)"signaled-timeline";
}

EXPORT_SYMBOL(dma_fence_timeline_name);

The signaled flag here is ordered in the reverse order. dma_fence_signal_timestamp_locked() sets the ops pointer to NULL, so you might dereference a NULL pointer there.

P.

Quoted 9 lines

Best,
Gary

Quoted 4 lines
drivers/dma-buf/dma-fence.c +1 -0
+	set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
  }
  EXPORT_SYMBOL(dma_fence_signal_timestamp_locked);