[RFC PATCH 3/8] dma-buf: dma-fence: Ensure ordering with locks for timestamps

From: Philipp Stanner

Date: Fri Nov 27 2026 - 05:20:33 EST


The memory ordering in dma_fence_timestamp() is currently being done
manually with acquire functions. It can be done more elegantly with the
fence's spinlock. This also allows for removing the TIMESTAMP bit in the
long run.

Use locking in dma_fence_timestamp().

Signed-off-by: Philipp Stanner <phasta@xxxxxxxxxx>
---
drivers/dma-buf/dma-fence.c | 9 ---------
include/linux/dma-fence.h | 24 ++++++++++++------------
2 files changed, 12 insertions(+), 21 deletions(-)

diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
index c947aec1bf91..ab02afdb0f18 100644
--- a/drivers/dma-buf/dma-fence.c
+++ b/drivers/dma-buf/dma-fence.c
@@ -378,15 +378,6 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
list_replace(&fence->cb_list, &cb_list);

fence->timestamp = timestamp;
- /*
- * TODO:
- * This is a temporary hotfix to prevent misordering until we
- * can completely remove the TIMESTAMP bit.
- *
- * The counterpart of this barrier are the test_bit_acquire()s in
- * dma_fence_timestamp().
- */
- smp_mb__before_atomic();
set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);

list_for_each_entry_safe(cur, tmp, &cb_list, node) {
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index 29c4332c8662..1c1aa4c7635a 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -680,23 +680,23 @@ static inline void dma_fence_set_error(struct dma_fence *fence,
* dma_fence_timestamp - helper to get the completion timestamp of a fence
* @fence: fence to get the timestamp from.
*
- * After a fence is signaled the timestamp is updated with the signaling time,
- * but setting the timestamp can race with tasks waiting for the signaling. This
- * helper busy waits for the correct timestamp to appear.
+ * Return:
+ * The fence's timestamp if the fence is already signaled, the current time
+ * otherwise.
*/
static inline ktime_t dma_fence_timestamp(struct dma_fence *fence)
{
- /*
- * The test_bit_acquire() calls order against the
- * smp_mb__before_atomic() in dma_fence_signal_timestamp_locked().
- */
+ unsigned long flags;
+ ktime_t ret;
+
+ dma_fence_lock_irqsave(fence, flags);
if (WARN_ON(!test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
- return ktime_get();
+ ret = ktime_get();
+ else
+ ret = fence->timestamp;
+ dma_fence_unlock_irqrestore(fence, flags);

- while (!test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
- cpu_relax();
-
- return fence->timestamp;
+ return ret;
}

signed long dma_fence_wait_timeout(struct dma_fence *,
--
2.56.0