mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] dma-buf/dma-fence: Prevent misordering for error checks
@ 2026-11-17  9:01 Philipp Stanner
  2026-11-17 14:55 ` Christian König
  0 siblings, 1 reply; 3+ messages in thread
From: Philipp Stanner @ 2026-11-17  9:01 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner,
	Simona Vetter, Chris Wilson, Danilo Krummrich, Gary Guo,
	Alice Ryhl, Daniel Almeida
  Cc: linux-media, dri-devel, linux-kernel, stable

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().

Cc: stable@vger.kernel.org
Fixes: d6c99f4bf093 ("dma-fence: Wrap querying the fence->status")
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
Changes since the RFC:
  - Order with test_bit_acquire(). (Gary)
  - ^ also makes it easy to backport this for now.
  - Try to document roughly what is even going on.
---
 include/linux/dma-fence.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index dd07d128adc3..a8d23fed70b9 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -496,7 +496,16 @@ 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);
 }
 
 /**
-- 
2.56.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] dma-buf/dma-fence: Prevent misordering for error checks
  2026-11-17  9:01 [PATCH] dma-buf/dma-fence: Prevent misordering for error checks Philipp Stanner
@ 2026-11-17 14:55 ` Christian König
  2026-11-17 17:47   ` Philipp Stanner
  0 siblings, 1 reply; 3+ messages in thread
From: Christian König @ 2026-11-17 14:55 UTC (permalink / raw)
  To: Philipp Stanner, Sumit Semwal, Simona Vetter, Chris Wilson,
	Danilo Krummrich, Gary Guo, Alice Ryhl, Daniel Almeida
  Cc: linux-media, dri-devel, linux-kernel, stable

On 11/17/26 10:01, Philipp Stanner wrote:
> 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().
> 
> Cc: stable@vger.kernel.org
> Fixes: d6c99f4bf093 ("dma-fence: Wrap querying the fence->status")
> Signed-off-by: Philipp Stanner <phasta@kernel.org>
> ---
> Changes since the RFC:
>   - Order with test_bit_acquire(). (Gary)
>   - ^ also makes it easy to backport this for now.
>   - Try to document roughly what is even going on.
> ---
>  include/linux/dma-fence.h | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index dd07d128adc3..a8d23fed70b9 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -496,7 +496,16 @@ 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.

That should probably be a C comment, e.g. /* ... */.

Apart from that really good catch.

Regards,
Christian.

> +	return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
>  }
>  
>  /**


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] dma-buf/dma-fence: Prevent misordering for error checks
  2026-11-17 14:55 ` Christian König
@ 2026-11-17 17:47   ` Philipp Stanner
  0 siblings, 0 replies; 3+ messages in thread
From: Philipp Stanner @ 2026-11-17 17:47 UTC (permalink / raw)
  To: Christian König, Philipp Stanner, Sumit Semwal,
	Simona Vetter, Chris Wilson, Danilo Krummrich, Gary Guo,
	Alice Ryhl, Daniel Almeida
  Cc: linux-media, dri-devel, linux-kernel, stable

On Tue, 2026-11-17 at 15:55 +0100, Christian König wrote:
> On 11/17/26 10:01, Philipp Stanner wrote:
> > 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.

[…]

> > +	//      test_and_set_bit() should do the trick.
> 
> That should probably be a C comment, e.g. /* ... */.

huh, how did that happen…
-ETOOMUCHRUST

I provide a v2

> Apart from that really good catch.

Found without any LLM 8-)

I saw it while reworking Rust-fence to force users to always evaluate a
fence's error code, i.e., you can only check whether a fence is
signaled when you also check for the error.

dma_fence_is_signaled() incentivizes wrong behavior.

But btw. we really want to carefully rethink our beloved signaled-bit
and its semantics. Mind all our past discussions about it.

This patch is more like a temporary hotfix. Much of our API is quite
fragile.

Regards
Philipp

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-17 17:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17  9:01 [PATCH] dma-buf/dma-fence: Prevent misordering for error checks Philipp Stanner
2026-11-17 14:55 ` Christian König
2026-11-17 17:47   ` Philipp Stanner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®