The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit
@ 2026-11-27 10:18 Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 1/8] dma-buf: dma-fence: Hot-fix timestamp misordering Philipp Stanner
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx

The timestamp bit is annoying and dangerous because it makes memory
ordering difficult. It's only accessor function, dma_fence_timestamp(),
is unnecessarily complicated.

The current state is largely UB. Most notably, since there is no memory
ordering, people might accidentally read a list head for a timestamp,
generating total bogus output.

I have previously proposed backporting a hot fix for the ordering [1].
Tvrtko recently dived into fixing drm_sched [2].

My hope would be that the full drm_sched fix could be set on top of this
series.

The first two patches could be backported into misc-fixes, and the rest
enter into misc-next? I'm not entirely sure what the best strategy is.

Pls review,
P.

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

Philipp Stanner (8):
  dma-buf: dma-fence: Hot-fix timestamp misordering
  drm/sched: Hot-fix timestamp misordering
  dma-buf: dma-fence: Ensure ordering with locks for timestamps
  dma-buf: dma-fence: Improve dma_fence_timestamp()
  drm/amdgpu: Stop using dma_fence internals
  dma-buf: sync_debug: don't use TIMESTAMP bit
  drm/i915: Remove TIMESTAMP bit
  dma-buf: dma-fence: Remove TIMESTAMP bit

 drivers/dma-buf/dma-fence-unwrap.c          | 20 ++++++++++-----
 drivers/dma-buf/dma-fence.c                 |  2 --
 drivers/dma-buf/sync_debug.c                |  8 +++---
 drivers/dma-buf/sync_file.c                 |  8 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c     | 11 ++++----
 drivers/gpu/drm/i915/gt/intel_breadcrumbs.c |  1 -
 drivers/gpu/drm/scheduler/sched_entity.c    |  4 +--
 drivers/gpu/drm/scheduler/sched_main.c      | 26 +++++++++++++++----
 include/linux/dma-fence.h                   | 28 +++++++++++++--------
 9 files changed, 68 insertions(+), 40 deletions(-)

-- 
2.56.0


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

* [RFC PATCH 1/8] dma-buf: dma-fence: Hot-fix timestamp misordering
  2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
@ 2026-11-27 10:18 ` Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 2/8] drm/sched: " Philipp Stanner
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx, stable

fence timestamps can be written and read without any memory ordering
since, presumably, forever. This can cause real bugs, other than false
accounting, for parties whose algorithms depend on the timestamp.

Hot-fix the problem for now so that it can be backported until we found
a suitable way forward for mainline.

Cc: stable@vger.kernel.org
Fixes: 76250f2b743b ("dma-buf/fence: Avoid use of uninitialised timestamp")
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/dma-buf/dma-fence.c | 9 +++++++++
 include/linux/dma-fence.h   | 8 ++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
index ab02afdb0f18..c947aec1bf91 100644
--- a/drivers/dma-buf/dma-fence.c
+++ b/drivers/dma-buf/dma-fence.c
@@ -378,6 +378,15 @@ 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 513d9dbbff47..29c4332c8662 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -686,10 +686,14 @@ 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)))
+	/*
+	 * The test_bit_acquire() calls order against the
+	 * smp_mb__before_atomic() in dma_fence_signal_timestamp_locked().
+	 */
+	if (WARN_ON(!test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
 		return ktime_get();
 
-	while (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
+	while (!test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
 		cpu_relax();
 
 	return fence->timestamp;
-- 
2.56.0


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

* [RFC PATCH 2/8] drm/sched: Hot-fix timestamp misordering
  2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 1/8] dma-buf: dma-fence: Hot-fix timestamp misordering Philipp Stanner
@ 2026-11-27 10:18 ` Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 3/8] dma-buf: dma-fence: Ensure ordering with locks for timestamps Philipp Stanner
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx, stable

dma_fence timestamps could so far be written and read without any memory
ordering, a bug that presumably got overlooked because x86 is strongly
ordered.

Hot-fix the scheduler's manual timestamp evaluation until we found a
more suitable solution for mainline linux.

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 | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 85a49e219837..44553db9b04c 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -928,8 +928,16 @@ 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,
+			/* Make the scheduled timestamp more accurate.
+			 *
+			 * TODO
+			 * The test_bit_acquire() is a hotfix against timestamp
+			 * misordering. Replace it with something better.
+			 *
+			 * The counter-part of the barrier is located in
+			 * dma_fence_signal_timestamp_locked().
+			 */
+			if (test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT,
 				     &next->s_fence->scheduled.flags))
 				next->s_fence->scheduled.timestamp =
 					dma_fence_timestamp(&job->s_fence->finished);
-- 
2.56.0


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

* [RFC PATCH 3/8] dma-buf: dma-fence: Ensure ordering with locks for timestamps
  2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 1/8] dma-buf: dma-fence: Hot-fix timestamp misordering Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 2/8] drm/sched: " Philipp Stanner
@ 2026-11-27 10:18 ` Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 4/8] dma-buf: dma-fence: Improve dma_fence_timestamp() Philipp Stanner
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx

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@kernel.org>
---
 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


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

* [RFC PATCH 4/8] dma-buf: dma-fence: Improve dma_fence_timestamp()
  2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
                   ` (2 preceding siblings ...)
  2026-11-27 10:18 ` [RFC PATCH 3/8] dma-buf: dma-fence: Ensure ordering with locks for timestamps Philipp Stanner
@ 2026-11-27 10:18 ` Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 5/8] drm/amdgpu: Stop using dma_fence internals Philipp Stanner
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx

dma_fence_timestamp() can be made a more robust and useful function by
having it return an error code that indicates whether the fence was
signaled or not. Only if it's signaled, returning a timestamp makes
sense, otherwise the user should choose what value to interpret as a
replacement.

Have dma_fence_timestamp() return an integer status code. Return the
timestamp via a pointer.

To be able to port drm_sched, move its hack for overwriting functions to
a temporary function for now.

Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/dma-buf/dma-fence-unwrap.c       | 20 +++++++++-----
 drivers/dma-buf/sync_file.c              |  8 +++---
 drivers/gpu/drm/scheduler/sched_entity.c |  4 +--
 drivers/gpu/drm/scheduler/sched_main.c   | 34 +++++++++++++++---------
 include/linux/dma-fence.h                | 20 ++++++++------
 5 files changed, 53 insertions(+), 33 deletions(-)

diff --git a/drivers/dma-buf/dma-fence-unwrap.c b/drivers/dma-buf/dma-fence-unwrap.c
index 120fe0c2a75a..a1e9b1ee2c32 100644
--- a/drivers/dma-buf/dma-fence-unwrap.c
+++ b/drivers/dma-buf/dma-fence-unwrap.c
@@ -136,10 +136,14 @@ struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences,
 				unsignaled = dma_fence_get(tmp);
 				++count;
 			} else {
-				ktime_t t = dma_fence_timestamp(tmp);
+				ktime_t fence_ts = ktime_get();
+				/* Leaves fence_ts unchanged if fence is not yet
+				 * signaled.
+				 */
+				dma_fence_timestamp(tmp, &fence_ts);
 
-				if (ktime_after(t, timestamp))
-					timestamp = t;
+				if (ktime_after(fence_ts, timestamp))
+					timestamp = fence_ts;
 			}
 		}
 	}
@@ -168,10 +172,14 @@ struct dma_fence *__dma_fence_unwrap_merge(size_t num_fences,
 			if (!dma_fence_is_signaled(tmp)) {
 				array[count++] = dma_fence_get(tmp);
 			} else {
-				ktime_t t = dma_fence_timestamp(tmp);
+				ktime_t fence_ts = ktime_get();
+				/* Leaves fence_ts unchanged if fence is not yet
+				 * signaled.
+				 */
+				dma_fence_timestamp(tmp, &fence_ts);
 
-				if (ktime_after(t, timestamp))
-					timestamp = t;
+				if (ktime_after(fence_ts, timestamp))
+					timestamp = fence_ts;
 			}
 		}
 	}
diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 2166bbdf7e4a..54ef230bcc4b 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -268,6 +268,7 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
 static int sync_fill_fence_info(struct dma_fence *fence,
 				 struct sync_fence_info *info)
 {
+	ktime_t fence_ts = ktime_set(0, 0);
 	const char __rcu *timeline;
 	const char __rcu *driver;
 
@@ -282,10 +283,9 @@ static int sync_fill_fence_info(struct dma_fence *fence,
 		sizeof(info->driver_name));
 
 	info->status = dma_fence_get_status(fence);
-	info->timestamp_ns =
-		dma_fence_is_signaled(fence) ?
-			ktime_to_ns(dma_fence_timestamp(fence)) :
-			ktime_set(0, 0);
+
+	dma_fence_timestamp(fence, &fence_ts);
+	info->timestamp_ns = ktime_to_ns(fence_ts);
 
 	rcu_read_unlock();
 
diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
index 03e68b4102d5..5ceffb4d3114 100644
--- a/drivers/gpu/drm/scheduler/sched_entity.c
+++ b/drivers/gpu/drm/scheduler/sched_entity.c
@@ -78,8 +78,8 @@ ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)
 	struct drm_sched_fence *s_fence = job->s_fence;
 	ktime_t start, end, duration;
 
-	start = dma_fence_timestamp(&s_fence->scheduled);
-	end = dma_fence_timestamp(&s_fence->finished);
+	dma_fence_timestamp(&s_fence->scheduled, &start);
+	dma_fence_timestamp(&s_fence->finished, &end);
 	duration = ktime_sub(end, start);
 
 	spin_lock(&stats->lock);
diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 44553db9b04c..e515bfa949b4 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -898,6 +898,26 @@ drm_sched_select_entity(struct drm_gpu_scheduler *sched)
 	return IS_ERR(entity) ? NULL : entity;
 }
 
+/*
+ * This is a temporary, hacky function until we can find a canonical solution
+ * for not overwriting fence timestamps.
+ *
+ * FIXME: Remove this hack.
+ */
+static void drm_sched_overwrite_scheduled_ts(struct drm_sched_job *current_job,
+					     struct drm_sched_job *next_job)
+{
+	struct dma_fence *current_fence = &current_job->s_fence->finished;
+	struct dma_fence *next_fence = &next_job->s_fence->scheduled;
+	ktime_t current_finished, next_scheduled;
+
+	if (dma_fence_timestamp(next_fence, &next_scheduled) != 0)
+		return;
+
+	if (dma_fence_timestamp(current_fence, &current_finished) == 0)
+		next_fence->timestamp = current_finished;
+}
+
 /**
  * drm_sched_get_finished_job - fetch the next finished job to be destroyed
  *
@@ -928,19 +948,7 @@ 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.
-			 *
-			 * TODO
-			 * The test_bit_acquire() is a hotfix against timestamp
-			 * misordering. Replace it with something better.
-			 *
-			 * The counter-part of the barrier is located in
-			 * dma_fence_signal_timestamp_locked().
-			 */
-			if (test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT,
-				     &next->s_fence->scheduled.flags))
-				next->s_fence->scheduled.timestamp =
-					dma_fence_timestamp(&job->s_fence->finished);
+			drm_sched_overwrite_scheduled_ts(job, next);
 
 			/* start TO timer for next job */
 			drm_sched_start_timeout(sched);
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index 1c1aa4c7635a..70bb8434ac1a 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -679,21 +679,25 @@ 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.
+ * @ts: a place to store the timestamp to.
+ *
+ * Writes the fences timestamp into @ts if the fence is already signaled.
+ * Leaves @ts unchanged otherwise.
  *
  * Return:
- * The fence's timestamp if the fence is already signaled, the current time
- * otherwise.
+ * 0 if the timestamp was written to @ts, -ENOENT if the fence was not yet
+ * signaled.
  */
-static inline ktime_t dma_fence_timestamp(struct dma_fence *fence)
+static inline int dma_fence_timestamp(struct dma_fence *fence, ktime_t *ts)
 {
 	unsigned long flags;
-	ktime_t ret;
+	int ret = -ENOENT;
 
 	dma_fence_lock_irqsave(fence, flags);
-	if (WARN_ON(!test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)))
-		ret = ktime_get();
-	else
-		ret = fence->timestamp;
+	if (dma_fence_test_signaled_flag(fence)) {
+		*ts = fence->timestamp;
+		ret = 0;
+	}
 	dma_fence_unlock_irqrestore(fence, flags);
 
 	return ret;
-- 
2.56.0


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

* [RFC PATCH 5/8] drm/amdgpu: Stop using dma_fence internals
  2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
                   ` (3 preceding siblings ...)
  2026-11-27 10:18 ` [RFC PATCH 4/8] dma-buf: dma-fence: Improve dma_fence_timestamp() Philipp Stanner
@ 2026-11-27 10:18 ` Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 6/8] dma-buf: sync_debug: don't use TIMESTAMP bit Philipp Stanner
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx

amdgpu_ctx_fence_time() directly accesses a fence's internal timestamp,
not ensuring memory ordering.

Make the code more robust and readable by using the (now improved)
dma_fence_timestamp() function.

Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
I think this should, contrary to my previous statements, be fine since
the lock taken is the s_fence lock, whereas this function's caller holds
a distinct fctx lock.
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 2f0349233695..9d6d92cb9734 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -170,6 +170,7 @@ static unsigned int amdgpu_ctx_get_hw_prio(struct amdgpu_ctx *ctx, u32 hw_ip)
 /* Calculate the time spend on the hw */
 static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence)
 {
+	ktime_t scheduled_ts, finished_ts;
 	struct drm_sched_fence *s_fence;
 
 	if (!fence)
@@ -177,15 +178,15 @@ static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence)
 
 	/* When the fence is not even scheduled it can't have spend time */
 	s_fence = to_drm_sched_fence(fence);
-	if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled.flags))
+
+	if (dma_fence_timestamp(&s_fence->scheduled, &scheduled_ts) != 0)
 		return ns_to_ktime(0);
 
 	/* When it is still running account how much already spend */
-	if (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.flags))
-		return ktime_sub(ktime_get(), s_fence->scheduled.timestamp);
+	if (dma_fence_timestamp(&s_fence->finished, &finished_ts) != 0)
+		return ktime_sub(ktime_get(), scheduled_ts);
 
-	return ktime_sub(s_fence->finished.timestamp,
-			 s_fence->scheduled.timestamp);
+	return ktime_sub(finished_ts, scheduled_ts);
 }
 
 static ktime_t amdgpu_ctx_entity_time(struct amdgpu_ctx *ctx,
-- 
2.56.0


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

* [RFC PATCH 6/8] dma-buf: sync_debug: don't use TIMESTAMP bit
  2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
                   ` (4 preceding siblings ...)
  2026-11-27 10:18 ` [RFC PATCH 5/8] drm/amdgpu: Stop using dma_fence internals Philipp Stanner
@ 2026-11-27 10:18 ` Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 7/8] drm/i915: Remove " Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 8/8] dma-buf: dma-fence: " Philipp Stanner
  7 siblings, 0 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx

dma_fence's TIMESTAMP bit doesn't really serve a purpose and should be
removed. Direct accessors should therefore be ported.

Use dma_fence_timestamp() in sync_debug.

Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/dma-buf/sync_debug.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/dma-buf/sync_debug.c b/drivers/dma-buf/sync_debug.c
index 9e5d662cd4e8..58c0d00ba5b3 100644
--- a/drivers/dma-buf/sync_debug.c
+++ b/drivers/dma-buf/sync_debug.c
@@ -46,6 +46,8 @@ static void sync_print_fence(struct seq_file *s,
 			     struct dma_fence *fence, bool show)
 {
 	struct sync_timeline *parent = dma_fence_parent(fence);
+	struct timespec64 ts64;
+	ktime_t ts;
 	int status;
 
 	status = dma_fence_get_status_locked(fence);
@@ -55,10 +57,8 @@ static void sync_print_fence(struct seq_file *s,
 		   show ? "_" : "",
 		   sync_status_str(status));
 
-	if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
-		struct timespec64 ts64 =
-			ktime_to_timespec64(fence->timestamp);
-
+	if (dma_fence_timestamp(fence, &ts) == 0) {
+		ts64 = ktime_to_timespec64(ts);
 		seq_printf(s, "@%ptSp", &ts64);
 	}
 
-- 
2.56.0


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

* [RFC PATCH 7/8] drm/i915: Remove TIMESTAMP bit
  2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
                   ` (5 preceding siblings ...)
  2026-11-27 10:18 ` [RFC PATCH 6/8] dma-buf: sync_debug: don't use TIMESTAMP bit Philipp Stanner
@ 2026-11-27 10:18 ` Philipp Stanner
  2026-11-27 10:18 ` [RFC PATCH 8/8] dma-buf: dma-fence: " Philipp Stanner
  7 siblings, 0 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx

i915 seems to have built its own half-copy of parts of the dma_buf
subsystem. This includes interferences with dma_fences' timestamp bit,
which does not provide memory ordering and which does not seem to serve
a clear purpose in i915.

Remove the set_bit().

Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/gpu/drm/i915/gt/intel_breadcrumbs.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/gpu/drm/i915/gt/intel_breadcrumbs.c b/drivers/gpu/drm/i915/gt/intel_breadcrumbs.c
index c10ac0ab3bfa..a43d774ba779 100644
--- a/drivers/gpu/drm/i915/gt/intel_breadcrumbs.c
+++ b/drivers/gpu/drm/i915/gt/intel_breadcrumbs.c
@@ -138,7 +138,6 @@ static void
 __dma_fence_signal__timestamp(struct dma_fence *fence, ktime_t timestamp)
 {
 	fence->timestamp = timestamp;
-	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
 	trace_dma_fence_signaled(fence);
 }
 
-- 
2.56.0


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

* [RFC PATCH 8/8] dma-buf: dma-fence: Remove TIMESTAMP bit
  2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
                   ` (6 preceding siblings ...)
  2026-11-27 10:18 ` [RFC PATCH 7/8] drm/i915: Remove " Philipp Stanner
@ 2026-11-27 10:18 ` Philipp Stanner
  7 siblings, 0 replies; 9+ messages in thread
From: Philipp Stanner @ 2026-11-27 10:18 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Jani Nikula, Joonas Lahtinen,
	Rodrigo Vivi, Tvrtko Ursulin, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Mario Limonciello (AMD), Ruslan Vagner, Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, intel-gfx

All users of DMA_FENCE_FLAG_TIMESTAMP_BIT have been removed. Remove the
bit now, too.

Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/dma-buf/dma-fence.c | 2 --
 include/linux/dma-fence.h   | 2 --
 2 files changed, 4 deletions(-)

diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
index ab02afdb0f18..58faee7166ae 100644
--- a/drivers/dma-buf/dma-fence.c
+++ b/drivers/dma-buf/dma-fence.c
@@ -376,9 +376,7 @@ void dma_fence_signal_timestamp_locked(struct dma_fence *fence,
 
 	/* Stash the cb_list before replacing it with the timestamp */
 	list_replace(&fence->cb_list, &cb_list);
-
 	fence->timestamp = timestamp;
-	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
 
 	list_for_each_entry_safe(cur, tmp, &cb_list, node) {
 		INIT_LIST_HEAD(&cur->node);
diff --git a/include/linux/dma-fence.h b/include/linux/dma-fence.h
index 70bb8434ac1a..bab0aa339e05 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -52,7 +52,6 @@ struct seq_file;
  * DMA_FENCE_FLAG_INITIALIZED_BIT - fence was initialized
  * DMA_FENCE_FLAG_INLINE_LOCK_BIT - use inline spinlock instead of external one
  * DMA_FENCE_FLAG_SIGNALED_BIT - fence is already signaled
- * DMA_FENCE_FLAG_TIMESTAMP_BIT - timestamp recorded for fence signaling
  * DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT - enable_signaling might have been called
  * DMA_FENCE_FLAG_USER_BITS - start of the unused bits, can be used by the
  * implementer of the fence for its own purposes. Can be used in different
@@ -114,7 +113,6 @@ enum dma_fence_flag_bits {
 	DMA_FENCE_FLAG_INLINE_LOCK_BIT,
 	DMA_FENCE_FLAG_SEQNO64_BIT,
 	DMA_FENCE_FLAG_SIGNALED_BIT,
-	DMA_FENCE_FLAG_TIMESTAMP_BIT,
 	DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
 
 	DMA_FENCE_FLAG_USER_BITS, /* must always be last member */
-- 
2.56.0


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

end of thread, other threads:[~2026-11-27 10:20 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 10:18 [RFC PATCH 0/8] dma-fence: Remove TIMESTAMP bit Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 1/8] dma-buf: dma-fence: Hot-fix timestamp misordering Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 2/8] drm/sched: " Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 3/8] dma-buf: dma-fence: Ensure ordering with locks for timestamps Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 4/8] dma-buf: dma-fence: Improve dma_fence_timestamp() Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 5/8] drm/amdgpu: Stop using dma_fence internals Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 6/8] dma-buf: sync_debug: don't use TIMESTAMP bit Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 7/8] drm/i915: Remove " Philipp Stanner
2026-11-27 10:18 ` [RFC PATCH 8/8] dma-buf: dma-fence: " Philipp Stanner

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