Skip to contentSkip to conversation

dma-fence: fix dma_fence_get_rcu_safe v2

PATCH In review Christian König 2 participants 2 messages Sep 15, 2017 – Sep 21, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dma-fence: fix dma_fence_get_rcu_safe v2

From: Christian König <[email protected]>

When dma_fence_get_rcu() fails to acquire a reference it doesn't necessary mean that there is no fence at all.

It usually mean that the fence was replaced by a new one and in this situation we certainly want to have the new one as result and *NOT* NULL.

v2: Keep extra check after dma_fence_get_rcu().

---
 include/linux/dma-fence.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/dma-fence.h +4 -1
index 0a186c4..f4f23cb 100644
@@ -248,9 +248,12 @@ dma_fence_get_rcu_safe(struct dma_fence * __rcu *fencep)
 		struct dma_fence *fence;
 
 		fence = rcu_dereference(*fencep);
-		if (!fence || !dma_fence_get_rcu(fence))
+		if (!fence)
 			return NULL;
 
+		if (!dma_fence_get_rcu(fence))
+			continue;
+
 		/* The atomic_inc_not_zero() inside dma_fence_get_rcu()
 		 * provides a full memory barrier upon success (such as now).
 		 * This is paired with the write barrier from assigning
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw

Op 15-09-17 om 11:53 schreef Christian König:

Quoted 29 lines

From: Christian König <[email protected]>

When dma_fence_get_rcu() fails to acquire a reference it doesn't necessary mean that there is no fence at all.

It usually mean that the fence was replaced by a new one and in this situation we certainly want to have the new one as result and *NOT* NULL.

v2: Keep extra check after dma_fence_get_rcu().

---
 include/linux/dma-fence.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/dma-fence.h +0 -0
index 0a186c4..f4f23cb 100644
@@ -248,9 +248,12 @@ dma_fence_get_rcu_safe(struct dma_fence * __rcu *fencep)
 		struct dma_fence *fence;
 
 		fence = rcu_dereference(*fencep);

include/linux/dma-fence.h dma_fence_get_rcu_safe(struct dma_fence * __rcu *fencep) · line 259

Quoted 10 lines
include/linux/dma-fence.h +4 -1
-		if (!fence || !dma_fence_get_rcu(fence))
+		if (!fence)
 			return NULL;
 
+		if (!dma_fence_get_rcu(fence))
+			continue;
+
 		/* The atomic_inc_not_zero() inside dma_fence_get_rcu()
 		 * provides a full memory barrier upon success (such as now).
 		 * This is paired with the write barrier from assigning

Should be safe from an infinite loop since the old fence is only unreffed after the new pointer is written, so we'll always make progress. :)