Skip to content

Commit a259df3

Browse files
mstsirkindavem330
authored andcommitted
ptr_ring: READ/WRITE_ONCE for __ptr_ring_empty
Lockless __ptr_ring_empty requires that consumer head is read and written at once, atomically. Annotate accordingly to make sure compiler does it correctly. Switch locked callers to __ptr_ring_peek which does not support the lockless operation. Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8619d38 commit a259df3

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎include/linux/ptr_ring.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ static inline void *__ptr_ring_peek(struct ptr_ring *r)
196196
*/
197197
static inline bool __ptr_ring_empty(struct ptr_ring *r)
198198
{
199-
return !__ptr_ring_peek(r);
199+
if (likely(r->size))
200+
return !r->queue[READ_ONCE(r->consumer_head)];
201+
return true;
200202
}
201203

202204
static inline bool ptr_ring_empty(struct ptr_ring *r)
@@ -285,7 +287,8 @@ static inline void __ptr_ring_discard_one(struct ptr_ring *r)
285287
consumer_head = 0;
286288
r->consumer_tail = 0;
287289
}
288-
r->consumer_head = consumer_head;
290+
/* matching READ_ONCE in __ptr_ring_empty for lockless tests */
291+
WRITE_ONCE(r->consumer_head, consumer_head);
289292
}
290293

291294
static inline void *__ptr_ring_consume(struct ptr_ring *r)
@@ -541,7 +544,9 @@ static inline void ptr_ring_unconsume(struct ptr_ring *r, void **batch, int n,
541544
goto done;
542545
}
543546
r->queue[head] = batch[--n];
544-
r->consumer_tail = r->consumer_head = head;
547+
r->consumer_tail = head;
548+
/* matching READ_ONCE in __ptr_ring_empty for lockless tests */
549+
WRITE_ONCE(r->consumer_head, head);
545550
}
546551

547552
done:

0 commit comments

Comments
 (0)