The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/3] dma-fence: Properly order timestamps
@ 2026-12-01  9:17 Philipp Stanner
  2026-12-01  9:17 ` [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering Philipp Stanner
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-12-01  9:17 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Tvrtko Ursulin, Pierre-Eric Pelloux-Prayer, Shahyan Soltani,
	Ruslan Vagner, Mario Limonciello (AMD), Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx

Changes in v2:
  - Expand commit messages. (Tvrtko)
  - Avoid "hotfix" rhetoric. (Danilo)

I propose stuffing these hotfixes into the stable kernel / misc-fixes
while we move on on misc-next with a more proper solution. I'll post a
series for that soonish, too.

P.

Philipp Stanner (3):
  dma-buf: dma-fence: Fix timestamp misordering
  drm/sched: Prevent overwrite of non-existing timestamp
  drm/amdgpu: Fix timestamp misordering

 drivers/dma-buf/dma-fence.c             |  9 +++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c |  4 ++--
 drivers/gpu/drm/scheduler/sched_main.c  | 15 +++++++++++++--
 include/linux/dma-fence.h               |  6 +++++-
 4 files changed, 29 insertions(+), 5 deletions(-)


base-commit: 9f333c38eac95cef6e1d54681b010c6531a7dcb2
-- 
2.56.0


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

* [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering
  2026-12-01  9:17 [PATCH v2 0/3] dma-fence: Properly order timestamps Philipp Stanner
@ 2026-12-01  9:17 ` Philipp Stanner
  2026-12-01  9:17 ` [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp Philipp Stanner
  2026-12-01  9:17 ` [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering Philipp Stanner
  2 siblings, 0 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-12-01  9:17 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Tvrtko Ursulin, Pierre-Eric Pelloux-Prayer, Shahyan Soltani,
	Ruslan Vagner, Mario Limonciello (AMD), Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-gfx, stable

fence timestamps can be written and read without any memory ordering
since, presumably, forever. This can cause real bugs for parties whose
algorithms depend on valid timestamp values.

The current misordering allows for the bit DMA_FENCE_FLAG_TIMESTAMP_BIT
to be read as true before the union containing the timestamp actually
gets the timestamp set. Thus, a reader could interpret the list-head in
the union as a timestamp value by accident.

So currently, both direct users of dma_fence_timestamp() and parties
that check the flag DMA_FENCE_FLAG_TIMESTAMP_BIT manually rely on UB,
which is especially likely to produce bugs on weakly ordered
architectures.

Fix the problem for now by adding appropriate memory barriers to ensure
ordering.

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   | 6 +++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/dma-buf/dma-fence.c b/drivers/dma-buf/dma-fence.c
index bd58688b81a7..ccb8258690f7 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?) fix to prevent misordering until we can
+	 * completely remove the TIMESTAMP bit.
+	 *
+	 * The counterpart of this barrier is the test_bit_acquire() 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 64330bf52561..fcfdf302f1d9 100644
--- a/include/linux/dma-fence.h
+++ b/include/linux/dma-fence.h
@@ -718,7 +718,11 @@ 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();
 
-	while (!test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags))
+	/*
+	 * The test_bit_acquire() calls order against the
+	 * smp_mb__before_atomic() in dma_fence_signal_timestamp_locked().
+	 */
+	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] 4+ messages in thread

* [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp
  2026-12-01  9:17 [PATCH v2 0/3] dma-fence: Properly order timestamps Philipp Stanner
  2026-12-01  9:17 ` [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering Philipp Stanner
@ 2026-12-01  9:17 ` Philipp Stanner
  2026-12-01  9:17 ` [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering Philipp Stanner
  2 siblings, 0 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-12-01  9:17 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Tvrtko Ursulin, Pierre-Eric Pelloux-Prayer, Shahyan Soltani,
	Ruslan Vagner, Mario Limonciello (AMD), Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-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. On more weakly ordered architectures, however, bugs could arise
at any moment since this is UB.

The current misordering allows for the bit DMA_FENCE_FLAG_TIMESTAMP_BIT
to be read as true before the union containing the timestamp actually
has the timestamp set.

drm_sched currently (sort of illegally) overwrites a fence's timestamp.
To do that somewhat safely, you have to wait for the timestamp to be
present, otherwise you overwrite another member of the union whose
memory the timestamp shares.

Ensure correct ordering in the scheduler's timestamp evaluation through
test_bit_acquire().

Cc: stable@vger.kernel.org
Fixes: f7fe64ad0f22 ("drm/sched: Split free_job into own work item")
Signed-off-by: Philipp Stanner <phasta@kernel.org>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
---
 drivers/gpu/drm/scheduler/sched_main.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 99252674e266..1a40a3549d7a 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -928,8 +928,19 @@ 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:
+			 * Stop overwriting a fence's timestamp.
+			 *
+			 * test_bit_acquire() ensures ordering so that only
+			 * timestamps that are actually present already get
+			 * overwritten (timestamp is a union).
+			 *
+			 * 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] 4+ messages in thread

* [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering
  2026-12-01  9:17 [PATCH v2 0/3] dma-fence: Properly order timestamps Philipp Stanner
  2026-12-01  9:17 ` [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering Philipp Stanner
  2026-12-01  9:17 ` [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp Philipp Stanner
@ 2026-12-01  9:17 ` Philipp Stanner
  2 siblings, 0 replies; 4+ messages in thread
From: Philipp Stanner @ 2026-12-01  9:17 UTC (permalink / raw)
  To: Sumit Semwal, Christian König, Philipp Stanner, Alex Deucher,
	David Airlie, Simona Vetter, Matthew Brost, Danilo Krummrich,
	Maarten Lankhorst, Maxime Ripard, Thomas Zimmermann,
	Tvrtko Ursulin, Pierre-Eric Pelloux-Prayer, Shahyan Soltani,
	Ruslan Vagner, Mario Limonciello (AMD), Gustavo Padovan,
	Chris Wilson
  Cc: linux-media, dri-devel, linux-kernel, amd-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.

The current misordering allows for the bit DMA_FENCE_FLAG_TIMESTAMP_BIT
to be read as true before the union containing the timestamp actually
gets the timestamp set. Thus, amdgpu could interpret the list-head
pointer in the union as a timestamp value by accident.

Fix amdgpu's manual timestamp evaluation with test_bit_acquire().

Cc: stable@vger.kernel.org
Fixes: af0b54167009 ("drm/amdgpu: Convert to common fdinfo format v5")
Signed-off-by: Philipp Stanner <phasta@kernel.org>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
index 2f0349233695..415f23aa08a0 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c
@@ -177,11 +177,11 @@ 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 (!test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->scheduled.flags))
 		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))
+	if (!test_bit_acquire(DMA_FENCE_FLAG_TIMESTAMP_BIT, &s_fence->finished.flags))
 		return ktime_sub(ktime_get(), s_fence->scheduled.timestamp);
 
 	return ktime_sub(s_fence->finished.timestamp,
-- 
2.56.0


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

end of thread, other threads:[~2026-12-01  9:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  9:17 [PATCH v2 0/3] dma-fence: Properly order timestamps Philipp Stanner
2026-12-01  9:17 ` [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering Philipp Stanner
2026-12-01  9:17 ` [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp Philipp Stanner
2026-12-01  9:17 ` [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering Philipp Stanner

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