mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] dma-fence: Fix timestamp memory ordering
@ 2026-11-19 13:18 Philipp Stanner
  2026-11-19 13:18 ` [PATCH 1/3] dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp() Philipp Stanner
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-11-19 13:18 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,
	Yunxiang Li, Alex Deucher, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: linux-media, dri-devel, linux-kernel

Based on misc-fixes

The memory ordering for obtaining a fences timestamp seems to be broken.
Moreover, there is no canonical way for users to obtain the timestamp,
as proven by the broken ordering in drm_sched.

See also my patch for the signaled flag [1].

@Tvrtko, would you mind basing your patch [2] on this series? That way
we'd solve both bugs.


[1] https://lore.kernel.org/dri-devel/20261119104030.2381685-3-phasta@kernel.org/
[2] https://lore.kernel.org/dri-devel/20261117094324.8256-1-tvrtko.ursulin@igalia.com/

Philipp Stanner (3):
  dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp()
  dma-buf: dma-fence: Add dma_fence_overwrite_timestamp()
  drm/sched: Fix memory ordering with dma_fence_overwrite_timestamp()

 drivers/dma-buf/dma-fence.c            | 28 ++++++++++++++++++++++++++
 drivers/gpu/drm/scheduler/sched_main.c | 28 +++++++++++++++++++++-----
 include/linux/dma-fence.h              | 15 +++++++++-----
 3 files changed, 61 insertions(+), 10 deletions(-)


base-commit: 7488ee210542b918b9c5749a29fd1502c49a0665
-- 
2.56.0


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

* [PATCH 1/3] dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp()
  2026-11-19 13:18 [PATCH 0/3] dma-fence: Fix timestamp memory ordering Philipp Stanner
@ 2026-11-19 13:18 ` Philipp Stanner
  2026-11-19 13:18 ` [PATCH 2/3] dma-buf: dma-fence: Add dma_fence_overwrite_timestamp() Philipp Stanner
  2026-11-19 13:18 ` [PATCH 3/3] drm/sched: Fix memory ordering with dma_fence_overwrite_timestamp() Philipp Stanner
  2 siblings, 0 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-11-19 13:18 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,
	Yunxiang Li, Alex Deucher, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: linux-media, dri-devel, linux-kernel, stable

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.

Cc: stable@vger.kernel.org
Fixes: b83ce9cb4a46 ("dma-buf: add dma_fence_timestamp helper")
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 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 *,
-- 
2.56.0


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

* [PATCH 2/3] dma-buf: dma-fence: Add dma_fence_overwrite_timestamp()
  2026-11-19 13:18 [PATCH 0/3] dma-fence: Fix timestamp memory ordering Philipp Stanner
  2026-11-19 13:18 ` [PATCH 1/3] dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp() Philipp Stanner
@ 2026-11-19 13:18 ` Philipp Stanner
  2026-11-19 13:18 ` [PATCH 3/3] drm/sched: Fix memory ordering with dma_fence_overwrite_timestamp() Philipp Stanner
  2 siblings, 0 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-11-19 13:18 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,
	Yunxiang Li, Alex Deucher, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: linux-media, dri-devel, linux-kernel, stable

A fence's timestamp is just an opportunistic data point for debugging
information etc. drm_sched, for example, uses the timestamps for its
scheduling algorithms.

In rare situations it can make sense to allow users to overwrite a
timestamp with a more accurate value. This is relatively harmless since
the timestamp has no meaning for the implementation itself.

Add dma_fence_overwrite_timestamp().

Cc: stable@vger.kernel.org
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/dma-buf/dma-fence.c | 28 ++++++++++++++++++++++++++++
 include/linux/dma-fence.h   |  1 +
 2 files changed, 29 insertions(+)

diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
index bd58688b81a7..f1b7e90864bf 100644
--- a/drivers/dma-buf/dma-fence.c
+++ b/drivers/dma-buf/dma-fence.c
@@ -412,6 +412,34 @@ void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
 }
 EXPORT_SYMBOL(dma_fence_signal_timestamp);
 
+/**
+ * dma_fence_overwrite_timestamp - overwrite a fence's timestamp
+ * @fence: the fence whose timestamp shall be overwritten
+ * @timestamp: the timestamp
+ *
+ * A dma_fence's timestamp is merely an opportunistically evaluated value to
+ * obtain information such as scheduling granularity. In some situations it can
+ * make sense to overwrite with a custom value. This function provides said
+ * functionality with the correct memory ordering.
+ *
+ * Return: 0 on success, -EAGAIN if the fence was not yet signaled.
+ */
+int dma_fence_overwrite_timestamp(struct dma_fence *fence, ktime_t timestamp)
+{
+	unsigned long flags;
+	int ret = 0;
+
+	dma_fence_lock_irqsave(fence, flags);
+	if (dma_fence_test_signaled_flag(fence))
+		fence->timestamp = timestamp;
+	else
+		ret = -EAGAIN;
+	dma_fence_unlock_irqrestore(fence, flags);
+
+	return ret;
+}
+EXPORT_SYMBOL(dma_fence_overwrite_timestamp);
+
 /**
  * dma_fence_signal_locked - signal completion of a fence
  * @fence: the fence to signal
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index 34aefb63a7fe..815471b844f0 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -447,6 +447,7 @@ bool dma_fence_check_and_signal_locked(struct dma_fence *fence);
 void dma_fence_signal_locked(struct dma_fence *fence);
 void dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp);
 void dma_fence_signal_timestamp_locked(struct dma_fence *fence, ktime_t timestamp);
+int dma_fence_overwrite_timestamp(struct dma_fence *fence, ktime_t timestamp);
 signed long dma_fence_default_wait(struct dma_fence *fence,
 				   bool intr, signed long timeout);
 int dma_fence_add_callback(struct dma_fence *fence,
-- 
2.56.0


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

* [PATCH 3/3] drm/sched: Fix memory ordering with dma_fence_overwrite_timestamp()
  2026-11-19 13:18 [PATCH 0/3] dma-fence: Fix timestamp memory ordering Philipp Stanner
  2026-11-19 13:18 ` [PATCH 1/3] dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp() Philipp Stanner
  2026-11-19 13:18 ` [PATCH 2/3] dma-buf: dma-fence: Add dma_fence_overwrite_timestamp() Philipp Stanner
@ 2026-11-19 13:18 ` Philipp Stanner
  2 siblings, 0 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-11-19 13:18 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,
	Yunxiang Li, Alex Deucher, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: linux-media, dri-devel, linux-kernel, stable

There is a memory ordering bug in drm_sched_get_finished_job(). The
manual timestamp flag check with test_bit() is not ordered.

The dma_fence framework now provides dma_fence_overwrite_timestamp(), a
function to overwrite a timestamp in a safe manner with correct memory
ordering.

Solve the bug with dma_fence_overwrite_timestamp().

Cc: stable@vger.kernel.org
Fixes: f7fe64ad0f22 ("drm/sched: Split free_job into own work item")
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/gpu/drm/scheduler/sched_main.c | 28 +++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 99252674e266..a065a6d2eb1c 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -66,6 +66,7 @@
  * This implies waiting for previously executed jobs.
  */
 
+#include <linux/dma-fence.h>
 #include <linux/export.h>
 #include <linux/wait.h>
 #include <linux/sched.h>
@@ -898,6 +899,22 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
 	return IS_ERR(entity) ? NULL : entity;
 }
 
+/**
+ * drm_sched_overwrite_scheduled_timestamp - make timestamp more accurate
+ *
+ * Overwrites the timestamp of the provided scheduled fence with the one from
+ * the finished fence, if the scheduled fence is already signaled. Does nothing
+ * otherwise.
+ */
+static void
+drm_sched_overwrite_scheduled_timestamp(struct dma_fence *finished,
+					struct dma_fence *scheduled)
+{
+	ktime_t timestamp = dma_fence_timestamp(finished);
+
+	dma_fence_overwrite_timestamp(scheduled, timestamp);
+}
+
 /**
  * drm_sched_get_finished_job - fetch the next finished job to be destroyed
  *
@@ -928,11 +945,12 @@ drm_sched_get_finished_job(struct drm_gpu_scheduler *sched)
 		next = list_first_entry_or_null(&sched->pending_list,
 						typeof(*next), list);
 		if (next) {
-			/* make the scheduled timestamp more accurate */
-			if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT,
-				     &next->s_fence->scheduled.flags))
-				next->s_fence->scheduled.timestamp =
-					dma_fence_timestamp(&job->s_fence->finished);
+			/* Try to make the scheduled timestamp more accurate. */
+			drm_sched_overwrite_scheduled_timestamp(
+					&job->s_fence->finished,
+					&next->s_fence->scheduled
+					);
+
 
 			/* start TO timer for next job */
 			drm_sched_start_timeout(sched);
-- 
2.56.0


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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 13:18 [PATCH 0/3] dma-fence: Fix timestamp memory ordering Philipp Stanner
2026-11-19 13:18 ` [PATCH 1/3] dma-buf/dma-fence: Ensure correct ordering in dma_fence_timestamp() Philipp Stanner
2026-11-19 13:18 ` [PATCH 2/3] dma-buf: dma-fence: Add dma_fence_overwrite_timestamp() Philipp Stanner
2026-11-19 13:18 ` [PATCH 3/3] drm/sched: Fix memory ordering with dma_fence_overwrite_timestamp() 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®