mirror of https://lore.kernel.org/lkml/
 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
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ 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] 12+ 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 10:57   ` Tvrtko Ursulin
  2026-12-01 13:43   ` Christian König
  2026-12-01  9:17 ` [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp Philipp Stanner
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 12+ 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	[flat|nested] 12+ 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 14:09   ` Christian König
  2026-12-01  9:17 ` [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering Philipp Stanner
  2026-12-01 14:36 ` [PATCH v2 0/3] dma-fence: Properly order timestamps Danilo Krummrich
  3 siblings, 1 reply; 12+ 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	[flat|nested] 12+ 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
  2026-12-01 14:10   ` Christian König
  2026-12-01 14:36 ` [PATCH v2 0/3] dma-fence: Properly order timestamps Danilo Krummrich
  3 siblings, 1 reply; 12+ 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	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering
  2026-12-01  9:17 ` [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering Philipp Stanner
@ 2026-12-01 10:57   ` Tvrtko Ursulin
  2026-12-01 13:43   ` Christian König
  1 sibling, 0 replies; 12+ messages in thread
From: Tvrtko Ursulin @ 2026-12-01 10:57 UTC (permalink / raw)
  To: Philipp Stanner, Sumit Semwal, Christian König,
	Alex Deucher, David Airlie, Simona Vetter, Matthew Brost,
	Danilo Krummrich, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, 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


On 01/12/2026 09:17, Philipp Stanner wrote:
> 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;

LGTM.

Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>

Regards,

Tvrtko


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

* Re: [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering
  2026-12-01  9:17 ` [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering Philipp Stanner
  2026-12-01 10:57   ` Tvrtko Ursulin
@ 2026-12-01 13:43   ` Christian König
  1 sibling, 0 replies; 12+ messages in thread
From: Christian König @ 2026-12-01 13:43 UTC (permalink / raw)
  To: Philipp Stanner, Sumit Semwal, 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

On 12/1/26 10:17, Philipp Stanner wrote:
> 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>

Reviewed-by: Christian König <christian.koenig@amd.com>

> ---
>  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;


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

* Re: [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp
  2026-12-01  9:17 ` [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp Philipp Stanner
@ 2026-12-01 14:09   ` Christian König
  0 siblings, 0 replies; 12+ messages in thread
From: Christian König @ 2026-12-01 14:09 UTC (permalink / raw)
  To: Philipp Stanner, Sumit Semwal, 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



On 12/1/26 10:17, Philipp Stanner wrote:
> 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.

That is unproblematic as far as I can see.

> 
> 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.

That here is the real problem, but is that actually possible?

> 
> 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>

Reviewed-by: Christian König <christian.koenig@amd.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);


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

* Re: [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering
  2026-12-01  9:17 ` [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering Philipp Stanner
@ 2026-12-01 14:10   ` Christian König
  2026-12-01 14:18     ` Philipp Stanner
  0 siblings, 1 reply; 12+ messages in thread
From: Christian König @ 2026-12-01 14:10 UTC (permalink / raw)
  To: Philipp Stanner, Sumit Semwal, 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

On 12/1/26 10:17, Philipp Stanner wrote:
> 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))

That one looks unnecessary to me because we don't really care about the timestamp value.

Christian.

>  		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,


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

* Re: [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering
  2026-12-01 14:10   ` Christian König
@ 2026-12-01 14:18     ` Philipp Stanner
  2026-12-01 14:43       ` Christian König
  0 siblings, 1 reply; 12+ messages in thread
From: Philipp Stanner @ 2026-12-01 14:18 UTC (permalink / raw)
  To: Christian König, Philipp Stanner, Sumit Semwal,
	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

On Tue, 2026-12-01 at 15:10 +0100, Christian König wrote:
> On 12/1/26 10:17, Philipp Stanner wrote:
> > 

[…]

> >  
> >  	/* 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))
> 
> That one looks unnecessary to me because we don't really care about the timestamp value.

You do care, you read it at the end of the function. It's just that
coincidentally another memory barrier is in between which should do the
ordering.

I suggest we do this formally correct here.
Originally btw I wrote it like that because the entire code can be made
more readable with two helper variables. So this code here could then
use the new dma_fence_timestamp().

But ultimately your choice, since it's your user whose bypassing the
API here.


P.

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

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

On Tue Dec 1, 2026 at 10:17 AM CET, Philipp Stanner wrote:
>   - Avoid "hotfix" rhetoric. (Danilo)

This wasn't about terminology. I'm questioning why this is needed as a late
cycle fix.

You pointed out that this gap already exists for a very long time and we don't
have any reports for this causing issues in practice.

Additionally, you mention below that you work on a proper solution that you will
post soon.

So, why do we need this as a quick late cycle fix? Why can't we just go with
your proper fix? What does the proper fix look like?

Now, of course the regression potential of this series is surveyable, but given
that you say it is temporary and you send a proper fix soon, it seems
unnecessary.

> 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.

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

* Re: [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering
  2026-12-01 14:18     ` Philipp Stanner
@ 2026-12-01 14:43       ` Christian König
  0 siblings, 0 replies; 12+ messages in thread
From: Christian König @ 2026-12-01 14:43 UTC (permalink / raw)
  To: phasta, Sumit Semwal, 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

On 12/1/26 15:18, Philipp Stanner wrote:
> On Tue, 2026-12-01 at 15:10 +0100, Christian König wrote:
>> On 12/1/26 10:17, Philipp Stanner wrote:
>>>
> 
> […]
> 
>>>  
>>>  	/* 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))
>>
>> That one looks unnecessary to me because we don't really care about the timestamp value.
> 
> You do care, you read it at the end of the function. It's just that
> coincidentally another memory barrier is in between which should do the
> ordering.

Yeah good point. That is actually the reason why I don't like the acquire/release API and prefer the barrier API, that was just so much easier to understand.

Regards,
Christian.

> 
> I suggest we do this formally correct here.
> Originally btw I wrote it like that because the entire code can be made
> more readable with two helper variables. So this code here could then
> use the new dma_fence_timestamp().
> 
> But ultimately your choice, since it's your user whose bypassing the
> API here.
> 
> 
> P.


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

* Re: [PATCH v2 0/3] dma-fence: Properly order timestamps
  2026-12-01 14:36 ` [PATCH v2 0/3] dma-fence: Properly order timestamps Danilo Krummrich
@ 2026-12-01 14:46   ` Philipp Stanner
  0 siblings, 0 replies; 12+ messages in thread
From: Philipp Stanner @ 2026-12-01 14:46 UTC (permalink / raw)
  To: Danilo Krummrich, Philipp Stanner
  Cc: Sumit Semwal, Christian König, Alex Deucher, David Airlie,
	Simona Vetter, Matthew Brost, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, Tvrtko Ursulin, Pierre-Eric Pelloux-Prayer,
	Shahyan Soltani, Ruslan Vagner, Mario Limonciello (AMD),
	Gustavo Padovan, Chris Wilson, linux-media, dri-devel,
	linux-kernel, amd-gfx

On Tue, 2026-12-01 at 15:36 +0100, Danilo Krummrich wrote:
> On Tue Dec 1, 2026 at 10:17 AM CET, Philipp Stanner wrote:
> >   - Avoid "hotfix" rhetoric. (Danilo)
> 
> This wasn't about terminology. I'm questioning why this is needed as a late
> cycle fix.
> 
> You pointed out that this gap already exists for a very long time and we don't
> have any reports for this causing issues in practice.

Yes, I have understood that already.

This series is about: this is a problem, so let's merge into into drm-
misc-fixes, some time in the near to mid future.

This is not intended as a "late cycle fix", just as a fix.

> Additionally, you mention below that you work on a proper solution that you will
> post soon.
> 
> So, why do we need this as a quick late cycle fix? Why can't we just go with
> your proper fix? What does the proper fix look like?

https://lore.kernel.org/dri-devel/20261127101812.2706432-2-phasta@kernel.org/


> Now, of course the regression potential of this series is surveyable, but given
> that you say it is temporary and you send a proper fix soon, it seems
> unnecessary.


It's small and easy to backport.

The "proper solution", linked above, once again might not be so easy to
roll out, because (surprise /s):

- fence lock and fence context lock are shared, for example in
  sync_file.
- Some parties call dma_fence_timestamp() from a fence callback,
  thereby deadlocking because the fence callbacks are called with the
  lock held. I guess these we *might* be able to port to just
  ktime_get().
- Users have ossified the API, most notably i915. It could be that
  removal of the TIMESTAMP bit would have to be reverted should i915
  regress.


So same story as always. The toughest nut to crack for a cleaner
solution would be sync_file, which doesn't use inline_lock right now,
therefore is condemned to a manual test_bit().

I currently am inclined to think that dma_fence_timestamp() should work
with manual barriers for now, then the broken users be ported, and then
one day dma_fence_timestamp() can be locked. Maybe together with the
signaled-bit, depending on how our discussions there unfold.


Clear now?

P.

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

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

Thread overview: 12+ 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 10:57   ` Tvrtko Ursulin
2026-12-01 13:43   ` Christian König
2026-12-01  9:17 ` [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp Philipp Stanner
2026-12-01 14:09   ` Christian König
2026-12-01  9:17 ` [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering Philipp Stanner
2026-12-01 14:10   ` Christian König
2026-12-01 14:18     ` Philipp Stanner
2026-12-01 14:43       ` Christian König
2026-12-01 14:36 ` [PATCH v2 0/3] dma-fence: Properly order timestamps Danilo Krummrich
2026-12-01 14:46   ` 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®