1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Definitions for the 'struct ptr_ring' datastructure.
6 * Michael S. Tsirkin <mst@redhat.com>
8 * Copyright (C) 2016 Red Hat, Inc.
10 * This is a limited-size FIFO maintaining pointers in FIFO order, with
11 * one CPU producing entries and another consuming entries from a FIFO.
13 * This implementation tries to minimize cache-contention when there is a
14 * single producer and a single consumer CPU.
17 #ifndef _LINUX_PTR_RING_H
18 #define _LINUX_PTR_RING_H 1
21 #include <linux/spinlock.h>
22 #include <linux/cache.h>
23 #include <linux/types.h>
24 #include <linux/compiler.h>
25 #include <linux/slab.h>
27 #include <asm/errno.h>
31 int producer ____cacheline_aligned_in_smp;
32 spinlock_t producer_lock;
33 int consumer_head ____cacheline_aligned_in_smp; /* next valid entry */
34 int consumer_tail; /* next entry to invalidate */
35 spinlock_t consumer_lock;
36 /* Shared consumer/producer data */
37 /* Read-only by both the producer and the consumer */
38 int size ____cacheline_aligned_in_smp; /* max entries in queue */
39 int batch; /* number of entries to consume in a batch */
43 /* Note: callers invoking this in a loop must use a compiler barrier,
44 * for example cpu_relax().
46 * NB: this is unlike __ptr_ring_empty in that callers must hold producer_lock:
47 * see e.g. ptr_ring_full.
49 static inline bool __ptr_ring_full(struct ptr_ring *r)
51 return r->queue[r->producer];
54 static inline bool ptr_ring_full(struct ptr_ring *r)
58 spin_lock(&r->producer_lock);
59 ret = __ptr_ring_full(r);
60 spin_unlock(&r->producer_lock);
65 static inline bool ptr_ring_full_irq(struct ptr_ring *r)
69 spin_lock_irq(&r->producer_lock);
70 ret = __ptr_ring_full(r);
71 spin_unlock_irq(&r->producer_lock);
76 static inline bool ptr_ring_full_any(struct ptr_ring *r)
81 spin_lock_irqsave(&r->producer_lock, flags);
82 ret = __ptr_ring_full(r);
83 spin_unlock_irqrestore(&r->producer_lock, flags);
88 static inline bool ptr_ring_full_bh(struct ptr_ring *r)
92 spin_lock_bh(&r->producer_lock);
93 ret = __ptr_ring_full(r);
94 spin_unlock_bh(&r->producer_lock);
99 /* Note: callers invoking this in a loop must use a compiler barrier,
100 * for example cpu_relax(). Callers must hold producer_lock.
101 * Callers are responsible for making sure pointer that is being queued
102 * points to a valid data.
104 static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
106 if (unlikely(!r->size) || r->queue[r->producer])
109 /* Make sure the pointer we are storing points to a valid data. */
110 /* Pairs with the dependency ordering in __ptr_ring_consume. */
113 WRITE_ONCE(r->queue[r->producer++], ptr);
114 if (unlikely(r->producer >= r->size))
120 * Note: resize (below) nests producer lock within consumer lock, so if you
121 * consume in interrupt or BH context, you must disable interrupts/BH when
124 static inline int ptr_ring_produce(struct ptr_ring *r, void *ptr)
128 spin_lock(&r->producer_lock);
129 ret = __ptr_ring_produce(r, ptr);
130 spin_unlock(&r->producer_lock);
135 static inline int ptr_ring_produce_irq(struct ptr_ring *r, void *ptr)
139 spin_lock_irq(&r->producer_lock);
140 ret = __ptr_ring_produce(r, ptr);
141 spin_unlock_irq(&r->producer_lock);
146 static inline int ptr_ring_produce_any(struct ptr_ring *r, void *ptr)
151 spin_lock_irqsave(&r->producer_lock, flags);
152 ret = __ptr_ring_produce(r, ptr);
153 spin_unlock_irqrestore(&r->producer_lock, flags);
158 static inline int ptr_ring_produce_bh(struct ptr_ring *r, void *ptr)
162 spin_lock_bh(&r->producer_lock);
163 ret = __ptr_ring_produce(r, ptr);
164 spin_unlock_bh(&r->producer_lock);
169 static inline void *__ptr_ring_peek(struct ptr_ring *r)
172 return READ_ONCE(r->queue[r->consumer_head]);
177 * Test ring empty status without taking any locks.
179 * NB: This is only safe to call if ring is never resized.
181 * However, if some other CPU consumes ring entries at the same time, the value
182 * returned is not guaranteed to be correct.
184 * In this case - to avoid incorrectly detecting the ring
185 * as empty - the CPU consuming the ring entries is responsible
186 * for either consuming all ring entries until the ring is empty,
187 * or synchronizing with some other CPU and causing it to
188 * re-test __ptr_ring_empty and/or consume the ring enteries
189 * after the synchronization point.
191 * Note: callers invoking this in a loop must use a compiler barrier,
192 * for example cpu_relax().
194 static inline bool __ptr_ring_empty(struct ptr_ring *r)
197 return !r->queue[READ_ONCE(r->consumer_head)];
201 static inline bool ptr_ring_empty(struct ptr_ring *r)
205 spin_lock(&r->consumer_lock);
206 ret = __ptr_ring_empty(r);
207 spin_unlock(&r->consumer_lock);
212 static inline bool ptr_ring_empty_irq(struct ptr_ring *r)
216 spin_lock_irq(&r->consumer_lock);
217 ret = __ptr_ring_empty(r);
218 spin_unlock_irq(&r->consumer_lock);
223 static inline bool ptr_ring_empty_any(struct ptr_ring *r)
228 spin_lock_irqsave(&r->consumer_lock, flags);
229 ret = __ptr_ring_empty(r);
230 spin_unlock_irqrestore(&r->consumer_lock, flags);
235 static inline bool ptr_ring_empty_bh(struct ptr_ring *r)
239 spin_lock_bh(&r->consumer_lock);
240 ret = __ptr_ring_empty(r);
241 spin_unlock_bh(&r->consumer_lock);
246 /* Must only be called after __ptr_ring_peek returned !NULL */
247 static inline void __ptr_ring_discard_one(struct ptr_ring *r)
249 /* Fundamentally, what we want to do is update consumer
250 * index and zero out the entry so producer can reuse it.
251 * Doing it naively at each consume would be as simple as:
252 * consumer = r->consumer;
253 * r->queue[consumer++] = NULL;
254 * if (unlikely(consumer >= r->size))
256 * r->consumer = consumer;
257 * but that is suboptimal when the ring is full as producer is writing
258 * out new entries in the same cache line. Defer these updates until a
259 * batch of entries has been consumed.
261 /* Note: we must keep consumer_head valid at all times for __ptr_ring_empty
264 int consumer_head = r->consumer_head;
265 int head = consumer_head++;
267 /* Once we have processed enough entries invalidate them in
268 * the ring all at once so producer can reuse their space in the ring.
269 * We also do this when we reach end of the ring - not mandatory
270 * but helps keep the implementation simple.
272 if (unlikely(consumer_head - r->consumer_tail >= r->batch ||
273 consumer_head >= r->size)) {
274 /* Zero out entries in the reverse order: this way we touch the
275 * cache line that producer might currently be reading the last;
276 * producer won't make progress and touch other cache lines
277 * besides the first one until we write out all entries.
279 while (likely(head >= r->consumer_tail))
280 r->queue[head--] = NULL;
281 r->consumer_tail = consumer_head;
283 if (unlikely(consumer_head >= r->size)) {
285 r->consumer_tail = 0;
287 /* matching READ_ONCE in __ptr_ring_empty for lockless tests */
288 WRITE_ONCE(r->consumer_head, consumer_head);
291 static inline void *__ptr_ring_consume(struct ptr_ring *r)
295 /* The READ_ONCE in __ptr_ring_peek guarantees that anyone
296 * accessing data through the pointer is up to date. Pairs
297 * with smp_wmb in __ptr_ring_produce.
299 ptr = __ptr_ring_peek(r);
301 __ptr_ring_discard_one(r);
306 static inline int __ptr_ring_consume_batched(struct ptr_ring *r,
312 for (i = 0; i < n; i++) {
313 ptr = __ptr_ring_consume(r);
323 * Note: resize (below) nests producer lock within consumer lock, so if you
324 * call this in interrupt or BH context, you must disable interrupts/BH when
327 static inline void *ptr_ring_consume(struct ptr_ring *r)
331 spin_lock(&r->consumer_lock);
332 ptr = __ptr_ring_consume(r);
333 spin_unlock(&r->consumer_lock);
338 static inline void *ptr_ring_consume_irq(struct ptr_ring *r)
342 spin_lock_irq(&r->consumer_lock);
343 ptr = __ptr_ring_consume(r);
344 spin_unlock_irq(&r->consumer_lock);
349 static inline void *ptr_ring_consume_any(struct ptr_ring *r)
354 spin_lock_irqsave(&r->consumer_lock, flags);
355 ptr = __ptr_ring_consume(r);
356 spin_unlock_irqrestore(&r->consumer_lock, flags);
361 static inline void *ptr_ring_consume_bh(struct ptr_ring *r)
365 spin_lock_bh(&r->consumer_lock);
366 ptr = __ptr_ring_consume(r);
367 spin_unlock_bh(&r->consumer_lock);
372 static inline int ptr_ring_consume_batched(struct ptr_ring *r,
377 spin_lock(&r->consumer_lock);
378 ret = __ptr_ring_consume_batched(r, array, n);
379 spin_unlock(&r->consumer_lock);
384 static inline int ptr_ring_consume_batched_irq(struct ptr_ring *r,
389 spin_lock_irq(&r->consumer_lock);
390 ret = __ptr_ring_consume_batched(r, array, n);
391 spin_unlock_irq(&r->consumer_lock);
396 static inline int ptr_ring_consume_batched_any(struct ptr_ring *r,
402 spin_lock_irqsave(&r->consumer_lock, flags);
403 ret = __ptr_ring_consume_batched(r, array, n);
404 spin_unlock_irqrestore(&r->consumer_lock, flags);
409 static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,
414 spin_lock_bh(&r->consumer_lock);
415 ret = __ptr_ring_consume_batched(r, array, n);
416 spin_unlock_bh(&r->consumer_lock);
421 /* Cast to structure type and call a function without discarding from FIFO.
422 * Function must return a value.
423 * Callers must take consumer_lock.
425 #define __PTR_RING_PEEK_CALL(r, f) ((f)(__ptr_ring_peek(r)))
427 #define PTR_RING_PEEK_CALL(r, f) ({ \
428 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
430 spin_lock(&(r)->consumer_lock); \
431 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
432 spin_unlock(&(r)->consumer_lock); \
433 __PTR_RING_PEEK_CALL_v; \
436 #define PTR_RING_PEEK_CALL_IRQ(r, f) ({ \
437 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
439 spin_lock_irq(&(r)->consumer_lock); \
440 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
441 spin_unlock_irq(&(r)->consumer_lock); \
442 __PTR_RING_PEEK_CALL_v; \
445 #define PTR_RING_PEEK_CALL_BH(r, f) ({ \
446 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
448 spin_lock_bh(&(r)->consumer_lock); \
449 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
450 spin_unlock_bh(&(r)->consumer_lock); \
451 __PTR_RING_PEEK_CALL_v; \
454 #define PTR_RING_PEEK_CALL_ANY(r, f) ({ \
455 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
456 unsigned long __PTR_RING_PEEK_CALL_f;\
458 spin_lock_irqsave(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
459 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
460 spin_unlock_irqrestore(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
461 __PTR_RING_PEEK_CALL_v; \
464 /* Not all gfp_t flags (besides GFP_KERNEL) are allowed. See
465 * documentation for vmalloc for which of them are legal.
467 static inline void **__ptr_ring_init_queue_alloc(unsigned int size, gfp_t gfp)
469 if (size > KMALLOC_MAX_SIZE / sizeof(void *))
471 return kvmalloc_array(size, sizeof(void *), gfp | __GFP_ZERO);
474 static inline void __ptr_ring_set_size(struct ptr_ring *r, int size)
477 r->batch = SMP_CACHE_BYTES * 2 / sizeof(*(r->queue));
478 /* We need to set batch at least to 1 to make logic
479 * in __ptr_ring_discard_one work correctly.
480 * Batching too much (because ring is small) would cause a lot of
481 * burstiness. Needs tuning, for now disable batching.
483 if (r->batch > r->size / 2 || !r->batch)
487 static inline int ptr_ring_init(struct ptr_ring *r, int size, gfp_t gfp)
489 r->queue = __ptr_ring_init_queue_alloc(size, gfp);
493 __ptr_ring_set_size(r, size);
494 r->producer = r->consumer_head = r->consumer_tail = 0;
495 spin_lock_init(&r->producer_lock);
496 spin_lock_init(&r->consumer_lock);
502 * Return entries into ring. Destroy entries that don't fit.
504 * Note: this is expected to be a rare slow path operation.
506 * Note: producer lock is nested within consumer lock, so if you
507 * resize you must make sure all uses nest correctly.
508 * In particular if you consume ring in interrupt or BH context, you must
509 * disable interrupts/BH when doing so.
511 static inline void ptr_ring_unconsume(struct ptr_ring *r, void **batch, int n,
512 void (*destroy)(void *))
517 spin_lock_irqsave(&r->consumer_lock, flags);
518 spin_lock(&r->producer_lock);
524 * Clean out buffered entries (for simplicity). This way following code
525 * can test entries for NULL and if not assume they are valid.
527 head = r->consumer_head - 1;
528 while (likely(head >= r->consumer_tail))
529 r->queue[head--] = NULL;
530 r->consumer_tail = r->consumer_head;
533 * Go over entries in batch, start moving head back and copy entries.
534 * Stop when we run into previously unconsumed entries.
537 head = r->consumer_head - 1;
540 if (r->queue[head]) {
541 /* This batch entry will have to be destroyed. */
544 r->queue[head] = batch[--n];
545 r->consumer_tail = head;
546 /* matching READ_ONCE in __ptr_ring_empty for lockless tests */
547 WRITE_ONCE(r->consumer_head, head);
551 /* Destroy all entries left in the batch. */
554 spin_unlock(&r->producer_lock);
555 spin_unlock_irqrestore(&r->consumer_lock, flags);
558 static inline void **__ptr_ring_swap_queue(struct ptr_ring *r, void **queue,
560 void (*destroy)(void *))
566 while ((ptr = __ptr_ring_consume(r)))
568 queue[producer++] = ptr;
572 if (producer >= size)
574 __ptr_ring_set_size(r, size);
575 r->producer = producer;
576 r->consumer_head = 0;
577 r->consumer_tail = 0;
585 * Note: producer lock is nested within consumer lock, so if you
586 * resize you must make sure all uses nest correctly.
587 * In particular if you consume ring in interrupt or BH context, you must
588 * disable interrupts/BH when doing so.
590 static inline int ptr_ring_resize(struct ptr_ring *r, int size, gfp_t gfp,
591 void (*destroy)(void *))
594 void **queue = __ptr_ring_init_queue_alloc(size, gfp);
600 spin_lock_irqsave(&(r)->consumer_lock, flags);
601 spin_lock(&(r)->producer_lock);
603 old = __ptr_ring_swap_queue(r, queue, size, gfp, destroy);
605 spin_unlock(&(r)->producer_lock);
606 spin_unlock_irqrestore(&(r)->consumer_lock, flags);
614 * Note: producer lock is nested within consumer lock, so if you
615 * resize you must make sure all uses nest correctly.
616 * In particular if you consume ring in interrupt or BH context, you must
617 * disable interrupts/BH when doing so.
619 static inline int ptr_ring_resize_multiple(struct ptr_ring **rings,
622 gfp_t gfp, void (*destroy)(void *))
628 queues = kmalloc_array(nrings, sizeof(*queues), gfp);
632 for (i = 0; i < nrings; ++i) {
633 queues[i] = __ptr_ring_init_queue_alloc(size, gfp);
638 for (i = 0; i < nrings; ++i) {
639 spin_lock_irqsave(&(rings[i])->consumer_lock, flags);
640 spin_lock(&(rings[i])->producer_lock);
641 queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
643 spin_unlock(&(rings[i])->producer_lock);
644 spin_unlock_irqrestore(&(rings[i])->consumer_lock, flags);
647 for (i = 0; i < nrings; ++i)
664 static inline void ptr_ring_cleanup(struct ptr_ring *r, void (*destroy)(void *))
669 while ((ptr = ptr_ring_consume(r)))
674 #endif /* _LINUX_PTR_RING_H */