* [PATCH v4] dma-buf/dma-fence: Prevent misordering for error checks
@ 2026-11-20 11:16 Philipp Stanner
2026-11-20 12:36 ` Christian König
0 siblings, 1 reply; 3+ messages in thread
From: Philipp Stanner @ 2026-11-20 11:16 UTC (permalink / raw)
To: Sumit Semwal, Christian König, Philipp Stanner,
Simona Vetter, Chris Wilson, Danilo Krummrich, Gary Guo,
Alice Ryhl, Daniel Almeida, Tvrtko Ursulin, Paul E . McKenney
Cc: 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); // <- test_and_set_bit() 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. That's a significant bug when we
consider that fences' purpose is to propagate the state of jobs running
on hardware.
dma_fence_get_status() does take the fence lock, presumably to ensure
ordering, but it is broken, too, because the read 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 in v4:
- Keep the hint at the function being opportunistic. (Christian)
Changes in v3:
- Be more precise in code comments and commit message about how the
ordering works. (Gary)
- Update the function's documentation to inform about ordering
semantics.
---
include/linux/dma-fence.h | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index dd07d128adc3..64330bf52561 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -487,16 +487,23 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence);
* with the fence in any way. The user must, therefore, ensure through other
* means that fences get signaled eventually.
*
- * This function uses test_bit(), which is thread-safe. Naturally, this function
- * should be used opportunistically; a fence could get signaled at any moment
- * after the check is done.
+ * Naturally, this function should be used opportunistically; a fence could get
+ * signaled at any moment after the check is done.
+ *
+ * This function provides acquire ordering. This means that all stores
+ * preceding the signaling of a fence are visible in correct order to users of
+ * this function.
*
* Return: true if signaled, false otherwise.
*/
static inline bool
dma_fence_test_signaled_flag(struct dma_fence *fence)
{
- return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
+ /*
+ * The release counterpart is the test_and_set_bit() in
+ * dma_fence_signal_timestamp_locked().
+ */
+ return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
}
/**
--
2.56.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v4] dma-buf/dma-fence: Prevent misordering for error checks
2026-11-20 11:16 [PATCH v4] dma-buf/dma-fence: Prevent misordering for error checks Philipp Stanner
@ 2026-11-20 12:36 ` Christian König
2026-11-20 13:13 ` Philipp Stanner
0 siblings, 1 reply; 3+ messages in thread
From: Christian König @ 2026-11-20 12:36 UTC (permalink / raw)
To: Philipp Stanner, Sumit Semwal, Simona Vetter, Chris Wilson,
Danilo Krummrich, Gary Guo, Alice Ryhl, Daniel Almeida,
Tvrtko Ursulin, Paul E . McKenney
Cc: dri-devel, linux-kernel, stable
On 11/20/26 12:16, Philipp Stanner wrote:
> Many users of dma_fence do things like:
>
> // Thread A
> dma_fence_set_error(f, -EINVAL);
> dma_fence_signal(f); // <- test_and_set_bit() 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. That's a significant bug when we
> consider that fences' purpose is to propagate the state of jobs running
> on hardware.
>
> dma_fence_get_status() does take the fence lock, presumably to ensure
> ordering, but it is broken, too, because the read 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>
Reviewed-by: Christian König <christian.koenig@amd.com>
> ---
> Changes in v4:
> - Keep the hint at the function being opportunistic. (Christian)
>
> Changes in v3:
> - Be more precise in code comments and commit message about how the
> ordering works. (Gary)
> - Update the function's documentation to inform about ordering
> semantics.
> ---
> include/linux/dma-fence.h | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index dd07d128adc3..64330bf52561 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -487,16 +487,23 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence);
> * with the fence in any way. The user must, therefore, ensure through other
> * means that fences get signaled eventually.
> *
> - * This function uses test_bit(), which is thread-safe. Naturally, this function
> - * should be used opportunistically; a fence could get signaled at any moment
> - * after the check is done.
> + * Naturally, this function should be used opportunistically; a fence could get
> + * signaled at any moment after the check is done.
> + *
> + * This function provides acquire ordering. This means that all stores
> + * preceding the signaling of a fence are visible in correct order to users of
> + * this function.
> *
> * Return: true if signaled, false otherwise.
> */
> static inline bool
> dma_fence_test_signaled_flag(struct dma_fence *fence)
> {
> - return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
> + /*
> + * The release counterpart is the test_and_set_bit() in
> + * dma_fence_signal_timestamp_locked().
> + */
> + return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
> }
>
> /**
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v4] dma-buf/dma-fence: Prevent misordering for error checks
2026-11-20 12:36 ` Christian König
@ 2026-11-20 13:13 ` Philipp Stanner
0 siblings, 0 replies; 3+ messages in thread
From: Philipp Stanner @ 2026-11-20 13:13 UTC (permalink / raw)
To: Christian König, Philipp Stanner, Sumit Semwal,
Simona Vetter, Chris Wilson, Danilo Krummrich, Gary Guo,
Alice Ryhl, Daniel Almeida, Tvrtko Ursulin, Paul E . McKenney
Cc: dri-devel, linux-kernel, stable
On Fri, 2026-11-20 at 13:36 +0100, Christian König wrote:
> On 11/20/26 12:16, Philipp Stanner wrote:
> > 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>
>
> Reviewed-by: Christian König <christian.koenig@amd.com>
Pushed it to misc-fixes. Thx.
P.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-20 13:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 11:16 [PATCH v4] dma-buf/dma-fence: Prevent misordering for error checks Philipp Stanner
2026-11-20 12:36 ` Christian König
2026-11-20 13:13 ` Philipp Stanner
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.