Skip to contentSkip to conversation

dma-buf/dma-fence: Prevent misordering for error checks

PATCH Superseded Philipp Stanner 3 participants 4 messages Nov 18

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Philipp Stanner

author maintainer
#0 lore raw

PATCH v2 1/1 dma-buf/dma-fence: Prevent misordering for error checks

Many users of dma_fence do things like:

// Thread A
dma_fence_set_error(f, -EINVAL);
dma_fence_signal(f); // <- Takes lock, which orders against the above.

// Thread B
if (dma_fence_is_signaled(f)) // <- Does test_bit(), no ordering.

	pr_info("err: %i\n", f->error); // <- Can be loaded first.

There is no memory ordering between reading of the signaled-bit and reading of the error code. This can result in outdated, wrong error codes being evaluated by the user.

Though dma_fence_get_status() does take the fence lock, it is equally broken because the misordering can simply happen within the critical section.

It's not entirely clear since when this bug exists, but at least since dma_fence_get_status() was added, which is why its birth commit is marked as the Fixes commit here.

Ensure ordering for everyone evaluating the error code centrally by using test_bit_acquire() in dma_fence_test_signaled_flag().

Fixes d6c99f4bf093 ("dma-fence: Wrap querying the fence->status")

---
Changes in v2:
- Use classic C style comments. (Christian)

---
 include/linux/dma-fence.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
include/linux/dma-fence.h +12 -1
index dd07d128adc3..a3fa1341765e 100644
@@ -496,7 +496,18 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence);
 static inline bool
 dma_fence_test_signaled_flag(struct dma_fence *fence)
 {
-	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
+	/*
+	 * Best-effort ordering of the locklessly evaluated signaled-bit,
+	 * especially so that reads of fence->error are not misordered.
+	 *
+	 * The release counterpart is typically ensured like this:
+	 *   a) Error codes must only be set before a fence signals and
+	 *   b) The fence's spinlock's barriers effective in dma_fence_signal()
+	 *      ensure ordering.
+	 *   c) For users of dma_fence_signal_locked(), the barrier implied in
+	 *      test_and_set_bit() should do the trick.
+	 */
+	return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
 }
 
 /**
Signature · 1 line
2.56.0
#1 lore raw
Quoted 40 lines · Philipp Stanner

Many users of dma_fence do things like:

// Thread A
dma_fence_set_error(f, -EINVAL);
dma_fence_signal(f); // <- Takes lock, which orders against the above.

// Thread B
if (dma_fence_is_signaled(f)) // <- Does test_bit(), no ordering.

	pr_info("err: %i\n", f->error); // <- Can be loaded first.

There is no memory ordering between reading of the signaled-bit and reading of the error code. This can result in outdated, wrong error codes being evaluated by the user.

Though dma_fence_get_status() does take the fence lock, it is equally broken because the misordering can simply happen within the critical section.

It's not entirely clear since when this bug exists, but at least since dma_fence_get_status() was added, which is why its birth commit is marked as the Fixes commit here.

Ensure ordering for everyone evaluating the error code centrally by using test_bit_acquire() in dma_fence_test_signaled_flag().

Fixes d6c99f4bf093 ("dma-fence: Wrap querying the fence->status")

---
Changes in v2:
- Use classic C style comments. (Christian)

---
 include/linux/dma-fence.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
include/linux/dma-fence.h +0 -0
index dd07d128adc3..a3fa1341765e 100644

include/linux/dma-fence.h const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); · line 501

Quoted 7 lines
include/linux/dma-fence.h +3 -1
@@ -496,7 +496,18 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence);
 static inline bool
 dma_fence_test_signaled_flag(struct dma_fence *fence)
 {
-	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
+	/*
+	 * Best-effort ordering of the locklessly evaluated signaled-bit,
+	 * especially so that reads of fence->error are not misordered.

I am not sure if this should focus on the error, or it should just provide a general guarantee that anything happens before dma_fence_signal is visible when signal test returns true. I would lean on the latter?

include/linux/dma-fence.h const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); · line 506

Quoted 5 lines
include/linux/dma-fence.h +5 -0
+	 *
+	 * The release counterpart is typically ensured like this:
+	 *   a) Error codes must only be set before a fence signals and
+	 *   b) The fence's spinlock's barriers effective in dma_fence_signal()
+	 *      ensure ordering.

Spinlock has nothing to do with this. The only thing that guarantees the ordering is test_and_set_bit in (c).

So probably easier just to say something like

    /* Pairs with test_and_set_bit() in dma_fence_signal_timestamp_locked */

Best,
Gary

Quoted 7 lines
include/linux/dma-fence.h +4 -0
+	 *   c) For users of dma_fence_signal_locked(), the barrier implied in
+	 *      test_and_set_bit() should do the trick.
+	 */
+	return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
 }
 
 /**
#2 lore raw
Quoted 1 line · Gary Guo

On Wed Nov 18, 2026 at 11:09 AM GMT, Philipp Stanner wrote:

[…]

Quoted 11 lines

include/linux/dma-fence.h const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); · line 501

Quoted 7 lines

 static inline bool dma_fence_test_signaled_flag(struct dma_fence *fence) {

include/linux/dma-fence.h +3 -1
-	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
+	/*
+	 * Best-effort ordering of the locklessly evaluated signaled-bit,
+	 * especially so that reads of fence->error are not misordered.

I am not sure if this should focus on the error, or it should just provide a general guarantee that anything happens before dma_fence_signal is visible when signal test returns true. I would lean on the latter?

See below.

Quoted 9 lines

include/linux/dma-fence.h const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); · line 506

Quoted 6 lines
include/linux/dma-fence.h +5 -0
+	 *
+	 * The release counterpart is typically ensured like this:
+	 *   a) Error codes must only be set before a fence signals and
+	 *   b) The fence's spinlock's barriers effective in dma_fence_signal()
+	 *      ensure ordering.

Spinlock has nothing to do with this. The only thing that guarantees the ordering is test_and_set_bit in (c).

Why not? A spinlock includes full barriers. If it were set_bit() instead of test_and_set_bit(), it would still work.

Quoted 4 lines

So probably easier just to say something like

    /* Pairs with test_and_set_bit() in dma_fence_signal_timestamp_locked */

It is easier.

I am, however, unhappy with the unclear state of the API contract. It's still unclear what the signaled-flag even is supposed to mean. So this comment was also supposed to hint at the unclear state.

The desired meaning aimed for by Christian is that it's only meaning is "hardware is done".

But as we see here it has implications for the entire semantic, too:
a) signaled == true -> the error code is valid (what else?)
b) The fence has either signaled or "is still signaling", i.e. running callbacks

I still maintain the position that it is inconsistent to run test_and_set_bit() under lock protection and evaluate it without the lock. Should be one or the other, not both.

Note that this patch intends just to hot-fix the error code evaluation, not to make the fence API fully consistent. It's currently not even clear whether manual flag evaluation is legal. It used to be legal. I'm the one who added dma_fence_test_signaled_flag(), but mostly for readability, not for semantic correctness.

It should be illegal to evaluate error codes with anything other than dma_fence_get_status(). But there then the meaning of the spinlock is currently also undefined. The lock there, with the current thinking, would seem surplus with this patch here.

P.

#3 lore raw
Quoted 29 lines · Philipp Stanner
Quoted 1 line · Gary Guo

On Wed Nov 18, 2026 at 11:09 AM GMT, Philipp Stanner wrote:

[…]

Quoted 11 lines

include/linux/dma-fence.h const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); · line 501

Quoted 7 lines

 static inline bool dma_fence_test_signaled_flag(struct dma_fence *fence) {

include/linux/dma-fence.h +3 -1
-	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
+	/*
+	 * Best-effort ordering of the locklessly evaluated signaled-bit,
+	 * especially so that reads of fence->error are not misordered.

I am not sure if this should focus on the error, or it should just provide a general guarantee that anything happens before dma_fence_signal is visible when signal test returns true. I would lean on the latter?

See below.

Quoted 9 lines

include/linux/dma-fence.h const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); · line 506

Quoted 6 lines
include/linux/dma-fence.h +5 -0
+	 *
+	 * The release counterpart is typically ensured like this:
+	 *   a) Error codes must only be set before a fence signals and
+	 *   b) The fence's spinlock's barriers effective in dma_fence_signal()
+	 *      ensure ordering.

Spinlock has nothing to do with this. The only thing that guarantees the ordering is test_and_set_bit in (c).

Quoted 2 lines

Why not? A spinlock includes full barriers. If it were set_bit() instead of test_and_set_bit(), it would still work.

No. spin_lock is a marked acquire access and spin_unlock is a marked release access. The order guarantee for these are restricted to the spinlock variable only (there is no acquire or release barriers in the kernel).

If you need to establish order using spinlock, you can only establish it with

    CPU 0  A --> spin_unlock 
                            \
                             \
                              \
    CPU 1                     spin_lock -> B

You cannot establish relation just by putting some atomic operation inside spinlock critical sections.

You can make spin_lock a full barrier by using smp_mb__after_spinlock(), but it's not there. I also think it's much easier to reason about test_bit_acquire() pairing with test_and_set_bit() than "this code will only be executed after a spin_lock".

Quoted 5 lines
Quoted 4 lines

So probably easier just to say something like

    /* Pairs with test_and_set_bit() in dma_fence_signal_timestamp_locked */

Quoted 8 lines

It is easier.

I am, however, unhappy with the unclear state of the API contract. It's still unclear what the signaled-flag even is supposed to mean. So this comment was also supposed to hint at the unclear state.

The desired meaning aimed for by Christian is that it's only meaning is "hardware is done".

Hmm, if the dma-fence doesn't provide ordering guarantee, then how would a consumer get ordering guarantee without knowing about the implementation detail on the producer side?

To me it seems that providing ordering guarantee that "when a DMA fence is observed to be signaled, everything prior to dma_fence_signal is visible" wouldn't be that expensive to implement (practically just need to upgrade a few test_bit to test_bit_acquire).

Best,
Gary

Quoted 23 lines

But as we see here it has implications for the entire semantic, too:
a) signaled == true -> the error code is valid (what else?)
b) The fence has either signaled or "is still signaling", i.e. running callbacks

I still maintain the position that it is inconsistent to run test_and_set_bit() under lock protection and evaluate it without the lock. Should be one or the other, not both.

Note that this patch intends just to hot-fix the error code evaluation, not to make the fence API fully consistent. It's currently not even clear whether manual flag evaluation is legal. It used to be legal. I'm the one who added dma_fence_test_signaled_flag(), but mostly for readability, not for semantic correctness.

It should be illegal to evaluate error codes with anything other than dma_fence_get_status(). But there then the meaning of the spinlock is currently also undefined. The lock there, with the current thinking, would seem surplus with this patch here.

P.