Skip to contentSkip to conversation

ptr_ring: add barriers

PATCH In review Michael S. Tsirkin 4 participants 16 messages Dec 5, 2017 – Dec 12, 2017

Subsystems (3)
lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

PATCH 1/1 ptr_ring: add barriers

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

 include/linux/ptr_ring.h | 9 +++++++++
 1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +9 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
 
 /* Note: callers invoking this in a loop must use a compiler barrier,
  * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
  */
 static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
 {
 	if (unlikely(!r->size) || r->queue[r->producer])
 		return -ENOSPC;
 
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
 	r->queue[r->producer++] = ptr;
 	if (unlikely(r->producer >= r->size))
 		r->producer = 0;
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
 	if (ptr)
 		__ptr_ring_discard_one(r);
 
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
 	return ptr;
 }
 
Signature · 1 line
MST
#1 lore raw
Quoted 53 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

  include/linux/ptr_ring.h | 9 +++++++++
  1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +6 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
 
  /* Note: callers invoking this in a loop must use a compiler barrier,
   * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
   */
  static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
  {
  	if (unlikely(!r->size) || r->queue[r->producer])
  		return -ENOSPC;
 
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
  	r->queue[r->producer++] = ptr;
  	if (unlikely(r->producer >= r->size))
  		r->producer = 0;

include/linux/ptr_ring.h static inline void *__ptr_ring_consume(struct ptr_ring *r) · line 289

Quoted 9 lines
include/linux/ptr_ring.h +3 -0
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
  	if (ptr)
  		__ptr_ring_discard_one(r);
 
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
  	return ptr;
  }
 

I was thinking whether or not it's better to move those to the callers. Then we can save lots of barriers in e.g batch consuming.

Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
In reply to Michael S. Tsirkin
Quoted 53 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

  include/linux/ptr_ring.h | 9 +++++++++
  1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +6 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
 
  /* Note: callers invoking this in a loop must use a compiler barrier,
   * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
   */
  static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
  {
  	if (unlikely(!r->size) || r->queue[r->producer])
  		return -ENOSPC;
 
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
  	r->queue[r->producer++] = ptr;
  	if (unlikely(r->producer >= r->size))
  		r->producer = 0;

include/linux/ptr_ring.h static inline void *__ptr_ring_consume(struct ptr_ring *r) · line 289

Quoted 9 lines
include/linux/ptr_ring.h +3 -0
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
  	if (ptr)
  		__ptr_ring_discard_one(r);
 
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
  	return ptr;
  }
 

I was thinking whether or not it's better to move those to the callers. Then we can save lots of barriers in e.g batch consuming.

Thanks

author maintainer
#3 lore raw
Quoted 65 lines · Jason Wang
Quoted 51 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

  include/linux/ptr_ring.h | 9 +++++++++
  1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +6 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
  /* Note: callers invoking this in a loop must use a compiler barrier,
   * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
   */
  static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
  {
  	if (unlikely(!r->size) || r->queue[r->producer])
  		return -ENOSPC;
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
  	r->queue[r->producer++] = ptr;
  	if (unlikely(r->producer >= r->size))
  		r->producer = 0;

include/linux/ptr_ring.h static inline void *__ptr_ring_consume(struct ptr_ring *r) · line 287

Quoted 7 lines
include/linux/ptr_ring.h +3 -0
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
  	if (ptr)
  		__ptr_ring_discard_one(r);
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
  	return ptr;
  }

I was thinking whether or not it's better to move those to the callers. Then we can save lots of barriers in e.g batch consuming.

Quoted 1 line

Thanks

Batch consumers only do smp_read_barrier_depends which is free on non-alpha. I suggest we do the simple thing for stable and reserve optimizations for later.

Signature · 5 lines
MST
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
author maintainer
#4 lore raw
In reply to Jason Wang
Quoted 65 lines · Jason Wang
Quoted 51 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

  include/linux/ptr_ring.h | 9 +++++++++
  1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +6 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
  /* Note: callers invoking this in a loop must use a compiler barrier,
   * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
   */
  static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
  {
  	if (unlikely(!r->size) || r->queue[r->producer])
  		return -ENOSPC;
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
  	r->queue[r->producer++] = ptr;
  	if (unlikely(r->producer >= r->size))
  		r->producer = 0;

include/linux/ptr_ring.h static inline void *__ptr_ring_consume(struct ptr_ring *r) · line 287

Quoted 7 lines
include/linux/ptr_ring.h +3 -0
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
  	if (ptr)
  		__ptr_ring_discard_one(r);
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
  	return ptr;
  }

I was thinking whether or not it's better to move those to the callers. Then we can save lots of barriers in e.g batch consuming.

Quoted 1 line

Thanks

Batch consumers only do smp_read_barrier_depends which is free on non-alpha. I suggest we do the simple thing for stable and reserve optimizations for later.

Signature · 1 line
MST
Acked-by
#5 lore raw
Quoted 64 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 51 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

   include/linux/ptr_ring.h | 9 +++++++++
   1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +6 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
   /* Note: callers invoking this in a loop must use a compiler barrier,
    * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
    */
   static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
   {
   	if (unlikely(!r->size) || r->queue[r->producer])
   		return -ENOSPC;
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
   	r->queue[r->producer++] = ptr;
   	if (unlikely(r->producer >= r->size))
   		r->producer = 0;

include/linux/ptr_ring.h static inline void *__ptr_ring_consume(struct ptr_ring *r) · line 287

Quoted 7 lines
include/linux/ptr_ring.h +3 -0
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
   	if (ptr)
   		__ptr_ring_discard_one(r);
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
   	return ptr;
   }

I was thinking whether or not it's better to move those to the callers. Then we can save lots of barriers in e.g batch consuming.

Quoted 5 lines
Quoted 1 line

Thanks

Batch consumers only do smp_read_barrier_depends which is free on non-alpha. I suggest we do the simple thing for stable and reserve optimizations for later.

Right.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Acked-by
#6 lore raw
In reply to Michael S. Tsirkin
Quoted 64 lines · Michael S. Tsirkin
Quoted 63 lines · Jason Wang
Quoted 51 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

   include/linux/ptr_ring.h | 9 +++++++++
   1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +6 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
   /* Note: callers invoking this in a loop must use a compiler barrier,
    * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
    */
   static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
   {
   	if (unlikely(!r->size) || r->queue[r->producer])
   		return -ENOSPC;
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
   	r->queue[r->producer++] = ptr;
   	if (unlikely(r->producer >= r->size))
   		r->producer = 0;

include/linux/ptr_ring.h static inline void *__ptr_ring_consume(struct ptr_ring *r) · line 287

Quoted 7 lines
include/linux/ptr_ring.h +3 -0
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
   	if (ptr)
   		__ptr_ring_discard_one(r);
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
   	return ptr;
   }

I was thinking whether or not it's better to move those to the callers. Then we can save lots of barriers in e.g batch consuming.

Quoted 5 lines
Quoted 1 line

Thanks

Batch consumers only do smp_read_barrier_depends which is free on non-alpha. I suggest we do the simple thing for stable and reserve optimizations for later.

Right.

#7 lore raw
In reply to Michael S. Tsirkin

Hi Michael,

Quoted 4 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

This is not the exact situation we are seeing.
Let me try to explain the situation

Affected on ARM64 platform.
1) tun_net_xmit calls skb_array_produce, which pushes the skb to the ptr_ring, this push is protected by a producer_lock.

2)Prior to this call the tun_net_xmit calls skb_orphan which calls the skb->destructor and sets skb->destructor and skb->sk as NULL.

2.a) These 2 writes are getting reordered

3) At the same time in the receive side (tun_ring_recv), which gets executed in another core calls skb_array_consume which pulls the skb from ptr ring, this pull is protected by a consumer lock.

4) eventually calling the skb->destructor (sock_wfree) with stale values.

Also note that this issue is reproducible in a long run and doesn't happen immediately after the launch of multiple VM's (infact the particular test cases launches 56 VM's which does iperf back and forth)

Quoted 8 lines

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

It is not the case that producer is reading the value, but the consumer reading stale value. So we need to have a strict rmb in place .

Quoted 51 lines

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

  include/linux/ptr_ring.h | 9 +++++++++
  1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +9 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
 
  /* Note: callers invoking this in a loop must use a compiler barrier,
   * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
   */
  static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
  {
  	if (unlikely(!r->size) || r->queue[r->producer])
  		return -ENOSPC;
 
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
  	r->queue[r->producer++] = ptr;
  	if (unlikely(r->producer >= r->size))
  		r->producer = 0;
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
  	if (ptr)
  		__ptr_ring_discard_one(r);
 
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
  	return ptr;
  }
 
#9 lore raw
In reply to Michael S. Tsirkin

Hi Michael,

Quoted 4 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

This is not the exact situation we are seeing.
Let me try to explain the situation

Affected on ARM64 platform.
1) tun_net_xmit calls skb_array_produce, which pushes the skb to the ptr_ring, this push is protected by a producer_lock.

2)Prior to this call the tun_net_xmit calls skb_orphan which calls the skb->destructor and sets skb->destructor and skb->sk as NULL.

2.a) These 2 writes are getting reordered

3) At the same time in the receive side (tun_ring_recv), which gets executed in another core calls skb_array_consume which pulls the skb from ptr ring, this pull is protected by a consumer lock.

4) eventually calling the skb->destructor (sock_wfree) with stale values.

Also note that this issue is reproducible in a long run and doesn't happen immediately after the launch of multiple VM's (infact the particular test cases launches 56 VM's which does iperf back and forth)

Quoted 8 lines

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

It is not the case that producer is reading the value, but the consumer reading stale value. So we need to have a strict rmb in place .

Quoted 51 lines

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

  include/linux/ptr_ring.h | 9 +++++++++
  1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +9 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
 
  /* Note: callers invoking this in a loop must use a compiler barrier,
   * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
   */
  static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
  {
  	if (unlikely(!r->size) || r->queue[r->producer])
  		return -ENOSPC;
 
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
  	r->queue[r->producer++] = ptr;
  	if (unlikely(r->producer >= r->size))
  		r->producer = 0;
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
  	if (ptr)
  		__ptr_ring_discard_one(r);
 
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
  	return ptr;
  }
 
author maintainer
#8 lore raw
In reply to George Cherian
Quoted 9 lines · George Cherian

Hi Michael,

Quoted 4 lines · Michael S. Tsirkin

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

This is not the exact situation we are seeing.

Could you test the patch pls?

Quoted 79 lines

Let me try to explain the situation

Affected on ARM64 platform.
1) tun_net_xmit calls skb_array_produce, which pushes the skb to the ptr_ring, this push is protected by a producer_lock.

2)Prior to this call the tun_net_xmit calls skb_orphan which calls the skb->destructor and sets skb->destructor and skb->sk as NULL.

2.a) These 2 writes are getting reordered

3) At the same time in the receive side (tun_ring_recv), which gets executed in another core calls skb_array_consume which pulls the skb from ptr ring, this pull is protected by a consumer lock.

4) eventually calling the skb->destructor (sock_wfree) with stale values.

Also note that this issue is reproducible in a long run and doesn't happen immediately after the launch of multiple VM's (infact the particular test cases launches 56 VM's which does iperf back and forth)

Quoted 8 lines

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

It is not the case that producer is reading the value, but the consumer reading stale value. So we need to have a strict rmb in place .

Quoted 47 lines

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

  include/linux/ptr_ring.h | 9 +++++++++
  1 file changed, 9 insertions(+)
include/linux/ptr_ring.h +9 -0
index 37b4bb2..6866df4 100644
@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r)
  /* Note: callers invoking this in a loop must use a compiler barrier,
   * for example cpu_relax(). Callers must hold producer_lock.
+ * Callers are responsible for making sure pointer that is being queued
+ * points to a valid data.
   */
  static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
  {
  	if (unlikely(!r->size) || r->queue[r->producer])
  		return -ENOSPC;
+	/* Make sure the pointer we are storing points to a valid data. */
+	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
+	smp_wmb();
+
  	r->queue[r->producer++] = ptr;
  	if (unlikely(r->producer >= r->size))
  		r->producer = 0;
@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
  	if (ptr)
  		__ptr_ring_discard_one(r);
+	/* Make sure anyone accessing data through the pointer is up to date. */
+	/* Pairs with smp_wmb in __ptr_ring_produce. */
+	smp_read_barrier_depends();
  	return ptr;
  }
 
maintainer
#10 lore raw
In reply to Michael S. Tsirkin

From: "Michael S. Tsirkin" <[email protected]>
Date: Tue, 5 Dec 2017 21:29:37 +0200

Quoted 16 lines

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

Quoted 6 lines

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

I really need some testing feedback for this before I apply it and queue it up for -stable.

George?

maintainer
#11 lore raw
In reply to Michael S. Tsirkin

From: "Michael S. Tsirkin" <[email protected]>
Date: Tue, 5 Dec 2017 21:29:37 +0200

Quoted 16 lines

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

Quoted 6 lines

---

George, could you pls report whether this patch fixes the issue for you?

This seems to be needed in stable as well.

I really need some testing feedback for this before I apply it and queue it up for -stable.

George?

maintainer
#12 lore raw
In reply to Michael S. Tsirkin

From: "Michael S. Tsirkin" <[email protected]>
Date: Tue, 5 Dec 2017 21:29:37 +0200

Quoted 9 lines

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

Quoted 7 lines

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

I'm asked for asking for testing feedback and did not get it in a reasonable amount of time.

So I'm applying this as-is, and queueing it up for -stable.

Thank you.

maintainer
#13 lore raw
In reply to Michael S. Tsirkin

From: "Michael S. Tsirkin" <[email protected]>
Date: Tue, 5 Dec 2017 21:29:37 +0200

Quoted 9 lines

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

Quoted 7 lines

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

I'm asked for asking for testing feedback and did not get it in a reasonable amount of time.

So I'm applying this as-is, and queueing it up for -stable.

Thank you.

#14 lore raw

Hi David,

Quoted 16 lines · David Miller

From: "Michael S. Tsirkin" <[email protected]>
Date: Tue, 5 Dec 2017 21:29:37 +0200

Quoted 13 lines

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

Quoted 7 lines
Quoted 3 lines

I'm asked for asking for testing feedback and did not get it in a reasonable amount of time.

The tests have completed more than 48 hours without any failures. I won't interrupt the same and run for longer time.
In case of any issue I will report the same.

Quoted 3 lines

So I'm applying this as-is, and queueing it up for -stable.

Thank you.

Regards,
-George

Quoted 1 line
#15 lore raw
In reply to David Miller

Hi David,

Quoted 16 lines · David Miller

From: "Michael S. Tsirkin" <[email protected]>
Date: Tue, 5 Dec 2017 21:29:37 +0200

Quoted 13 lines

Users of ptr_ring expect that it's safe to give the data structure a pointer and have it be available to consumers, but that actually requires an smb_wmb or a stronger barrier.

In absence of such barriers and on architectures that reorder writes, consumer might read an un=initialized value from an skb pointer stored in the skb array. This was observed causing crashes.

To fix, add memory barriers. The barrier we use is a wmb, the assumption being that producers do not need to read the value so we do not need to order these reads.

Quoted 7 lines
Quoted 3 lines

I'm asked for asking for testing feedback and did not get it in a reasonable amount of time.

The tests have completed more than 48 hours without any failures. I won't interrupt the same and run for longer time.
In case of any issue I will report the same.

Quoted 3 lines

So I'm applying this as-is, and queueing it up for -stable.

Thank you.

Regards,
-George

Quoted 1 line