Skip to contentSkip to conversation

dma-fence: Properly order timestamps

PATCH In review Philipp Stanner 5 participants 13 messages Dec 1

Subsystems (8)
lore
Share & tools
lore

Philipp Stanner

author maintainer
#0 lore raw

v2 cover dma-fence: Properly order timestamps

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

Signature · 1 line
2.56.0
author maintainer
#1 lore raw

PATCH v2 1/3 dma-buf: dma-fence: Fix timestamp misordering

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.

Fixes 76250f2b743b ("dma-buf/fence: Avoid use of uninitialised timestamp")
---
 drivers/dma-buf/dma-fence.c | 9 +++++++++
 include/linux/dma-fence.h   | 6 +++++-
 2 files changed, 14 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence.c +9 -0
index bd58688b81a7..ccb8258690f7 100644
@@ -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) {
include/linux/dma-fence.h +5 -1
index 64330bf52561..fcfdf302f1d9 100644
@@ -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;
Signature · 1 line
2.56.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering

Quoted 52 lines · Philipp Stanner

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.

Fixes 76250f2b743b ("dma-buf/fence: Avoid use of uninitialised timestamp")
---
  drivers/dma-buf/dma-fence.c | 9 +++++++++
  include/linux/dma-fence.h   | 6 +++++-
  2 files changed, 14 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence.c +9 -0
index bd58688b81a7..ccb8258690f7 100644
@@ -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) {
include/linux/dma-fence.h +0 -0
index 64330bf52561..fcfdf302f1d9 100644
@@ -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();

include/linux/dma-fence.h static inline ktime_t dma_fence_timestamp(struct dma_fence *fence) · line 728

Quoted 10 lines
include/linux/dma-fence.h +5 -1
 
-	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.

Regards,

Tvrtko

maintainer Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] dma-buf: dma-fence: Fix timestamp misordering

In reply to Philipp Stanner
Quoted 14 lines · Philipp Stanner

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.

Quoted 6 lines

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

Fixes 76250f2b743b ("dma-buf/fence: Avoid use of uninitialised timestamp")
Quoted 42 lines
---
 drivers/dma-buf/dma-fence.c | 9 +++++++++
 include/linux/dma-fence.h   | 6 +++++-
 2 files changed, 14 insertions(+), 1 deletion(-)
drivers/dma-buf/dma-fence.c +9 -0
index bd58688b81a7..ccb8258690f7 100644
@@ -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) {
include/linux/dma-fence.h +5 -1
index 64330bf52561..fcfdf302f1d9 100644
@@ -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;
author maintainer
#4 lore raw

PATCH v2 2/3 drm/sched: Prevent overwrite of non-existing timestamp

In reply to Philipp Stanner

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

Fixes f7fe64ad0f22 ("drm/sched: Split free_job into own work item")
---
 drivers/gpu/drm/scheduler/sched_main.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +13 -2
index 99252674e266..1a40a3549d7a 100644
@@ -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);
Signature · 1 line
2.56.0
maintainer Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] drm/sched: Prevent overwrite of non-existing timestamp

Quoted 8 lines · Philipp Stanner

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.

Quoted 5 lines

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?

Quoted 8 lines

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

Fixes f7fe64ad0f22 ("drm/sched: Split free_job into own work item")
Quoted 30 lines
---
 drivers/gpu/drm/scheduler/sched_main.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/gpu/drm/scheduler/sched_main.c +13 -2
index 99252674e266..1a40a3549d7a 100644
@@ -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);
author maintainer
#6 lore raw

PATCH v2 3/3 drm/amdgpu: Fix timestamp misordering

In reply to Philipp Stanner

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

Fixes af0b54167009 ("drm/amdgpu: Convert to common fdinfo format v5")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -2
index 2f0349233695..415f23aa08a0 100644
@@ -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,
Signature · 1 line
2.56.0
maintainer
#7 lore raw

Re: [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering

Quoted 24 lines · Philipp Stanner

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

Fixes af0b54167009 ("drm/amdgpu: Convert to common fdinfo format v5")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
index 2f0349233695..415f23aa08a0 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence) · line 180

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
@@ -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.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
 		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,
#8 lore raw

Re: [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering

Quoted 2 lines · Christian König
Quoted 1 line · Philipp Stanner

[…]

Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence) · line 180

Quoted 5 lines


  /* When the fence is not even scheduled it can't have spend time */ s_fence = to_drm_sched_fence(fence);

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
-	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.

maintainer
#9 lore raw

Re: [PATCH v2 3/3] drm/amdgpu: Fix timestamp misordering

Quoted 14 lines · Philipp Stanner
Quoted 2 lines · Christian König
Quoted 1 line · Philipp Stanner

[…]

Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static ktime_t amdgpu_ctx_fence_time(struct dma_fence *fence) · line 180

Quoted 5 lines


  /* When the fence is not even scheduled it can't have spend time */ s_fence = to_drm_sched_fence(fence);

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -1
-	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.

Quoted 3 lines

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.

Quoted 11 lines

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.

maintainer
#10 lore raw
In reply to Philipp Stanner
Quoted 1 line · Philipp Stanner

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

Quoted 3 lines

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.

#11 lore raw
Quoted 8 lines · Danilo Krummrich
Quoted 1 line · Philipp Stanner

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

Quoted 5 lines

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/[email protected]/

Quoted 3 lines

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.

maintainer
#12 lore raw
Quoted 1 line · Philipp Stanner

Clear now?

This sounds reasonable. Please make sure to provide such context in the cover letter.