Commit a8ceb5db authored by Michael S. Tsirkin's avatar Michael S. Tsirkin Committed by David S. Miller

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.
Reported-by: default avatarGeorge Cherian <george.cherian@cavium.com>
Suggested-by: default avatarJason Wang <jasowang@redhat.com>
Signed-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Acked-by: default avatarJason Wang <jasowang@redhat.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent f0f1d016
...@@ -101,12 +101,18 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r) ...@@ -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, /* Note: callers invoking this in a loop must use a compiler barrier,
* for example cpu_relax(). Callers must hold producer_lock. * 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) static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
{ {
if (unlikely(!r->size) || r->queue[r->producer]) if (unlikely(!r->size) || r->queue[r->producer])
return -ENOSPC; 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; r->queue[r->producer++] = ptr;
if (unlikely(r->producer >= r->size)) if (unlikely(r->producer >= r->size))
r->producer = 0; r->producer = 0;
...@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r) ...@@ -275,6 +281,9 @@ static inline void *__ptr_ring_consume(struct ptr_ring *r)
if (ptr) if (ptr)
__ptr_ring_discard_one(r); __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; return ptr;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment