Re: [PATCH 1/3] dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp()
From: Christian König
Date: Fri Nov 20 2026 - 04:33:52 EST
On 11/19/26 14:18, Philipp Stanner wrote:
> dma_fence_timestamp() tests two fence flags with test_bit(), not
> providing any ordering.
>
> The while loop + cpu_relax() seem to satisfy the desire to avoid
> locking, effectively rebuilding a locks spinning behavior in the rare
> case that a fence is signaling right now. Contrary to proper locking,
> however, correct memory ordering is not guaranteed.
>
> Ensure correct ordering with locking.
My words when I tried to clean that up the first time: "Another alternative would be to grab the spinlock when that happens."
I don't remember the story why the busy loop was used and can't see any reasons why it wasn't done this way in the first place.
But my suggestion is to completely drop the DMA_FENCE_FLAG_TIMESTAMP_BIT as well. As far as I can tell that was only added as indicator that the timestamp is set, but we do that now for all fences anyway.
Regards,
Christian.
> Cc: stable@xxxxxxxxxxxxxxx
> Fixes: b83ce9cb4a46 ("dma-buf: add dma_fence_timestamp helper")
> Signed-off-by: Philipp Stanner <phasta@xxxxxxxxxx>
> ---
> include/linux/dma-fence.h | 14 +++++++++-----
> 1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
> index dd07d128adc3..34aefb63a7fe 100644
> --- a/include/linux/dma-fence.h
> +++ b/include/linux/dma-fence.h
> @@ -708,13 +708,17 @@ static inline void dma_fence_set_error(struct dma_fence *fence,
> */
> static inline ktime_t dma_fence_timestamp(struct dma_fence *fence)
> {
> - if (WARN_ON(!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
> - return ktime_get();
> + unsigned long flags;
> + ktime_t ret;
>
> - while (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
> - cpu_relax();
> + dma_fence_lock_irqsave(fence, flags);
> + if (WARN_ON(!dma_fence_test_signaled_flag(fence)))
> + ret = ktime_get();
> + else
> + ret = fence->timestamp;
> + dma_fence_unlock_irqrestore(fence, flags);
>
> - return fence->timestamp;
> + return ret;
> }
>
> signed long dma_fence_wait_timeout(struct dma_fence *,