diff options
author | Jason Wang <jasowang@redhat.com> | 2016-06-30 02:45:31 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2016-07-01 05:32:16 -0400 |
commit | 982fb490c298896d15e9323a882f34a57c11ff56 (patch) | |
tree | 453a3c8afc504c726462412c5b61cb90785c1069 | |
parent | 8dc7243abbf514e208aee1c0b430f35b866f8cc4 (diff) |
ptr_ring: support zero length ring
Sometimes, we need zero length ring. But current code will crash since
we don't do any check before accessing the ring. This patch fixes this.
Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r-- | include/linux/ptr_ring.h | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h index 562a65e8bcc0..d78b8b89c707 100644 --- a/include/linux/ptr_ring.h +++ b/include/linux/ptr_ring.h | |||
@@ -102,7 +102,7 @@ static inline bool ptr_ring_full_bh(struct ptr_ring *r) | |||
102 | */ | 102 | */ |
103 | static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr) | 103 | static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr) |
104 | { | 104 | { |
105 | if (r->queue[r->producer]) | 105 | if (unlikely(!r->size) || r->queue[r->producer]) |
106 | return -ENOSPC; | 106 | return -ENOSPC; |
107 | 107 | ||
108 | r->queue[r->producer++] = ptr; | 108 | r->queue[r->producer++] = ptr; |
@@ -164,7 +164,9 @@ static inline int ptr_ring_produce_bh(struct ptr_ring *r, void *ptr) | |||
164 | */ | 164 | */ |
165 | static inline void *__ptr_ring_peek(struct ptr_ring *r) | 165 | static inline void *__ptr_ring_peek(struct ptr_ring *r) |
166 | { | 166 | { |
167 | return r->queue[r->consumer]; | 167 | if (likely(r->size)) |
168 | return r->queue[r->consumer]; | ||
169 | return NULL; | ||
168 | } | 170 | } |
169 | 171 | ||
170 | /* Note: callers invoking this in a loop must use a compiler barrier, | 172 | /* Note: callers invoking this in a loop must use a compiler barrier, |