Skip to contentSkip to conversation

dma-buf/dma-fence: Prevent misordering for error checks

PATCH Superseded Philipp Stanner 2 participants 2 messages Nov 19

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Philipp Stanner

author maintainer
#0 lore raw

PATCH v3 1/1 dma-buf/dma-fence: Prevent misordering for error checks

Many users of dma_fence do things like:

// Thread A
dma_fence_set_error(f, -EINVAL);
dma_fence_signal(f); // <- test_and_set_bit() orders against the above.

// Thread B
if (dma_fence_is_signaled(f)) // <- Does test_bit(), no ordering.

	pr_info("err: %i\n", f->error); // <- Can be loaded first.

There is no memory ordering between reading of the signaled-bit and reading of the error code. This can result in outdated, wrong error codes being evaluated by the user. That's a significant bug when we consider that fences' purpose is to propagate the state of jobs running on hardware.

dma_fence_get_status() does take the fence lock, presumably to ensure ordering, but it is broken, too, because the read misordering can simply happen within the critical section.

It's not entirely clear since when this bug exists, but at least since dma_fence_get_status() was added, which is why its birth commit is marked as the Fixes commit here.

Ensure ordering for everyone evaluating the error code centrally by using test_bit_acquire() in dma_fence_test_signaled_flag().

Fixes d6c99f4bf093 ("dma-fence: Wrap querying the fence->status")

---
Let's go with Gary's proposal for now, and have this patch here remain simple so that it can be trivially be backported, since this is a severe potential bug.

Changes in v3:
- Be more precise in code comments and commit message about how the ordering works. (Gary)
- Update the function's documentation to inform about ordering semantics.

---
 include/linux/dma-fence.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
include/linux/dma-fence.h +8 -4
index dd07d128adc3..a41b9a7792e1 100644
@@ -487,16 +487,20 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence);
  * with the fence in any way. The user must, therefore, ensure through other
  * means that fences get signaled eventually.
  *
- * This function uses test_bit(), which is thread-safe. Naturally, this function
- * should be used opportunistically; a fence could get signaled at any moment
- * after the check is done.
+ * This function provides acquire ordering. This means that all stores
+ * preceding signaling a fence are visible in correct order to users of this
+ * function.
  *
  * Return: true if signaled, false otherwise.
  */
 static inline bool
 dma_fence_test_signaled_flag(struct dma_fence *fence)
 {
-	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
+	/*
+	 * The release counterpart is the test_and_set_bit() in
+	 * dma_fence_signal_timestamp_locked().
+	 */
+	return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
 }
 
 /**
Signature · 1 line
2.56.0
maintainer
#1 lore raw
Quoted 49 lines · Philipp Stanner

Many users of dma_fence do things like:

// Thread A
dma_fence_set_error(f, -EINVAL);
dma_fence_signal(f); // <- test_and_set_bit() orders against the above.

// Thread B
if (dma_fence_is_signaled(f)) // <- Does test_bit(), no ordering.

	pr_info("err: %i\n", f->error); // <- Can be loaded first.

There is no memory ordering between reading of the signaled-bit and reading of the error code. This can result in outdated, wrong error codes being evaluated by the user. That's a significant bug when we consider that fences' purpose is to propagate the state of jobs running on hardware.

dma_fence_get_status() does take the fence lock, presumably to ensure ordering, but it is broken, too, because the read misordering can simply happen within the critical section.

It's not entirely clear since when this bug exists, but at least since dma_fence_get_status() was added, which is why its birth commit is marked as the Fixes commit here.

Ensure ordering for everyone evaluating the error code centrally by using test_bit_acquire() in dma_fence_test_signaled_flag().

Fixes d6c99f4bf093 ("dma-fence: Wrap querying the fence->status")

---
Let's go with Gary's proposal for now, and have this patch here remain simple so that it can be trivially be backported, since this is a severe potential bug.

Changes in v3:
- Be more precise in code comments and commit message about how the ordering works. (Gary)
- Update the function's documentation to inform about ordering semantics.

---
 include/linux/dma-fence.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
include/linux/dma-fence.h +0 -0
index dd07d128adc3..a41b9a7792e1 100644

include/linux/dma-fence.h const char __rcu *dma_fence_timeline_name(struct dma_fence *fence); · line 492

Quoted 6 lines
include/linux/dma-fence.h +0 -3
@@ -487,16 +487,20 @@ const char __rcu *dma_fence_timeline_name(struct dma_fence *fence);
  * with the fence in any way. The user must, therefore, ensure through other
  * means that fences get signaled eventually.
  *
- * This function uses test_bit(), which is thread-safe. Naturally, this function
- * should be used opportunistically; a fence could get signaled at any moment
- * after the check is done.

Please keep the note that this function is opportunistically, e.g. that the fence can be signaled at any moment after the check is done.

Regards,
Christian.

Quoted 18 lines
include/linux/dma-fence.h +8 -1
+ * This function provides acquire ordering. This means that all stores
+ * preceding signaling a fence are visible in correct order to users of this
+ * function.
  *
  * Return: true if signaled, false otherwise.
  */
 static inline bool
 dma_fence_test_signaled_flag(struct dma_fence *fence)
 {
-	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
+	/*
+	 * The release counterpart is the test_and_set_bit() in
+	 * dma_fence_signal_timestamp_locked().
+	 */
+	return test_bit_acquire(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags);
 }
 
 /**