xref: /openbmc/linux/include/linux/ptr_ring.h (revision c6cd2e01)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
22e0ab8caSMichael S. Tsirkin /*
32e0ab8caSMichael S. Tsirkin  *	Definitions for the 'struct ptr_ring' datastructure.
42e0ab8caSMichael S. Tsirkin  *
52e0ab8caSMichael S. Tsirkin  *	Author:
62e0ab8caSMichael S. Tsirkin  *		Michael S. Tsirkin <mst@redhat.com>
72e0ab8caSMichael S. Tsirkin  *
82e0ab8caSMichael S. Tsirkin  *	Copyright (C) 2016 Red Hat, Inc.
92e0ab8caSMichael S. Tsirkin  *
102e0ab8caSMichael S. Tsirkin  *	This is a limited-size FIFO maintaining pointers in FIFO order, with
112e0ab8caSMichael S. Tsirkin  *	one CPU producing entries and another consuming entries from a FIFO.
122e0ab8caSMichael S. Tsirkin  *
132e0ab8caSMichael S. Tsirkin  *	This implementation tries to minimize cache-contention when there is a
142e0ab8caSMichael S. Tsirkin  *	single producer and a single consumer CPU.
152e0ab8caSMichael S. Tsirkin  */
162e0ab8caSMichael S. Tsirkin 
172e0ab8caSMichael S. Tsirkin #ifndef _LINUX_PTR_RING_H
182e0ab8caSMichael S. Tsirkin #define _LINUX_PTR_RING_H 1
192e0ab8caSMichael S. Tsirkin 
202e0ab8caSMichael S. Tsirkin #ifdef __KERNEL__
212e0ab8caSMichael S. Tsirkin #include <linux/spinlock.h>
222e0ab8caSMichael S. Tsirkin #include <linux/cache.h>
232e0ab8caSMichael S. Tsirkin #include <linux/types.h>
242e0ab8caSMichael S. Tsirkin #include <linux/compiler.h>
252e0ab8caSMichael S. Tsirkin #include <linux/slab.h>
260eac8ce9SJesper Dangaard Brouer #include <linux/mm.h>
272e0ab8caSMichael S. Tsirkin #include <asm/errno.h>
282e0ab8caSMichael S. Tsirkin #endif
292e0ab8caSMichael S. Tsirkin 
302e0ab8caSMichael S. Tsirkin struct ptr_ring {
312e0ab8caSMichael S. Tsirkin 	int producer ____cacheline_aligned_in_smp;
322e0ab8caSMichael S. Tsirkin 	spinlock_t producer_lock;
33fb9de970SMichael S. Tsirkin 	int consumer_head ____cacheline_aligned_in_smp; /* next valid entry */
34fb9de970SMichael S. Tsirkin 	int consumer_tail; /* next entry to invalidate */
352e0ab8caSMichael S. Tsirkin 	spinlock_t consumer_lock;
362e0ab8caSMichael S. Tsirkin 	/* Shared consumer/producer data */
372e0ab8caSMichael S. Tsirkin 	/* Read-only by both the producer and the consumer */
382e0ab8caSMichael S. Tsirkin 	int size ____cacheline_aligned_in_smp; /* max entries in queue */
39fb9de970SMichael S. Tsirkin 	int batch; /* number of entries to consume in a batch */
402e0ab8caSMichael S. Tsirkin 	void **queue;
412e0ab8caSMichael S. Tsirkin };
422e0ab8caSMichael S. Tsirkin 
432e0ab8caSMichael S. Tsirkin /* Note: callers invoking this in a loop must use a compiler barrier,
4484328342SMichael S. Tsirkin  * for example cpu_relax().
4584328342SMichael S. Tsirkin  *
4684328342SMichael S. Tsirkin  * NB: this is unlike __ptr_ring_empty in that callers must hold producer_lock:
4784328342SMichael S. Tsirkin  * see e.g. ptr_ring_full.
482e0ab8caSMichael S. Tsirkin  */
__ptr_ring_full(struct ptr_ring * r)492e0ab8caSMichael S. Tsirkin static inline bool __ptr_ring_full(struct ptr_ring *r)
502e0ab8caSMichael S. Tsirkin {
512e0ab8caSMichael S. Tsirkin 	return r->queue[r->producer];
522e0ab8caSMichael S. Tsirkin }
532e0ab8caSMichael S. Tsirkin 
ptr_ring_full(struct ptr_ring * r)542e0ab8caSMichael S. Tsirkin static inline bool ptr_ring_full(struct ptr_ring *r)
552e0ab8caSMichael S. Tsirkin {
565d49de53SMichael S. Tsirkin 	bool ret;
575d49de53SMichael S. Tsirkin 
585d49de53SMichael S. Tsirkin 	spin_lock(&r->producer_lock);
595d49de53SMichael S. Tsirkin 	ret = __ptr_ring_full(r);
605d49de53SMichael S. Tsirkin 	spin_unlock(&r->producer_lock);
615d49de53SMichael S. Tsirkin 
625d49de53SMichael S. Tsirkin 	return ret;
635d49de53SMichael S. Tsirkin }
645d49de53SMichael S. Tsirkin 
ptr_ring_full_irq(struct ptr_ring * r)655d49de53SMichael S. Tsirkin static inline bool ptr_ring_full_irq(struct ptr_ring *r)
665d49de53SMichael S. Tsirkin {
675d49de53SMichael S. Tsirkin 	bool ret;
685d49de53SMichael S. Tsirkin 
695d49de53SMichael S. Tsirkin 	spin_lock_irq(&r->producer_lock);
705d49de53SMichael S. Tsirkin 	ret = __ptr_ring_full(r);
715d49de53SMichael S. Tsirkin 	spin_unlock_irq(&r->producer_lock);
725d49de53SMichael S. Tsirkin 
735d49de53SMichael S. Tsirkin 	return ret;
745d49de53SMichael S. Tsirkin }
755d49de53SMichael S. Tsirkin 
ptr_ring_full_any(struct ptr_ring * r)765d49de53SMichael S. Tsirkin static inline bool ptr_ring_full_any(struct ptr_ring *r)
775d49de53SMichael S. Tsirkin {
785d49de53SMichael S. Tsirkin 	unsigned long flags;
795d49de53SMichael S. Tsirkin 	bool ret;
805d49de53SMichael S. Tsirkin 
815d49de53SMichael S. Tsirkin 	spin_lock_irqsave(&r->producer_lock, flags);
825d49de53SMichael S. Tsirkin 	ret = __ptr_ring_full(r);
835d49de53SMichael S. Tsirkin 	spin_unlock_irqrestore(&r->producer_lock, flags);
845d49de53SMichael S. Tsirkin 
855d49de53SMichael S. Tsirkin 	return ret;
865d49de53SMichael S. Tsirkin }
875d49de53SMichael S. Tsirkin 
ptr_ring_full_bh(struct ptr_ring * r)885d49de53SMichael S. Tsirkin static inline bool ptr_ring_full_bh(struct ptr_ring *r)
895d49de53SMichael S. Tsirkin {
905d49de53SMichael S. Tsirkin 	bool ret;
915d49de53SMichael S. Tsirkin 
925d49de53SMichael S. Tsirkin 	spin_lock_bh(&r->producer_lock);
935d49de53SMichael S. Tsirkin 	ret = __ptr_ring_full(r);
945d49de53SMichael S. Tsirkin 	spin_unlock_bh(&r->producer_lock);
955d49de53SMichael S. Tsirkin 
965d49de53SMichael S. Tsirkin 	return ret;
972e0ab8caSMichael S. Tsirkin }
982e0ab8caSMichael S. Tsirkin 
992e0ab8caSMichael S. Tsirkin /* Note: callers invoking this in a loop must use a compiler barrier,
1005d49de53SMichael S. Tsirkin  * for example cpu_relax(). Callers must hold producer_lock.
101a8ceb5dbSMichael S. Tsirkin  * Callers are responsible for making sure pointer that is being queued
102a8ceb5dbSMichael S. Tsirkin  * points to a valid data.
1032e0ab8caSMichael S. Tsirkin  */
__ptr_ring_produce(struct ptr_ring * r,void * ptr)1042e0ab8caSMichael S. Tsirkin static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
1052e0ab8caSMichael S. Tsirkin {
106982fb490SJason Wang 	if (unlikely(!r->size) || r->queue[r->producer])
1072e0ab8caSMichael S. Tsirkin 		return -ENOSPC;
1082e0ab8caSMichael S. Tsirkin 
109a8ceb5dbSMichael S. Tsirkin 	/* Make sure the pointer we are storing points to a valid data. */
110c6cd2e01SWill Deacon 	/* Pairs with the dependency ordering in __ptr_ring_consume. */
111a8ceb5dbSMichael S. Tsirkin 	smp_wmb();
112a8ceb5dbSMichael S. Tsirkin 
113a07d29c6SMichael S. Tsirkin 	WRITE_ONCE(r->queue[r->producer++], ptr);
1142e0ab8caSMichael S. Tsirkin 	if (unlikely(r->producer >= r->size))
1152e0ab8caSMichael S. Tsirkin 		r->producer = 0;
1162e0ab8caSMichael S. Tsirkin 	return 0;
1172e0ab8caSMichael S. Tsirkin }
1182e0ab8caSMichael S. Tsirkin 
119e7169530SMichael S. Tsirkin /*
120e7169530SMichael S. Tsirkin  * Note: resize (below) nests producer lock within consumer lock, so if you
121e7169530SMichael S. Tsirkin  * consume in interrupt or BH context, you must disable interrupts/BH when
122e7169530SMichael S. Tsirkin  * calling this.
123e7169530SMichael S. Tsirkin  */
ptr_ring_produce(struct ptr_ring * r,void * ptr)1242e0ab8caSMichael S. Tsirkin static inline int ptr_ring_produce(struct ptr_ring *r, void *ptr)
1252e0ab8caSMichael S. Tsirkin {
1262e0ab8caSMichael S. Tsirkin 	int ret;
1272e0ab8caSMichael S. Tsirkin 
1282e0ab8caSMichael S. Tsirkin 	spin_lock(&r->producer_lock);
1292e0ab8caSMichael S. Tsirkin 	ret = __ptr_ring_produce(r, ptr);
1302e0ab8caSMichael S. Tsirkin 	spin_unlock(&r->producer_lock);
1312e0ab8caSMichael S. Tsirkin 
1322e0ab8caSMichael S. Tsirkin 	return ret;
1332e0ab8caSMichael S. Tsirkin }
1342e0ab8caSMichael S. Tsirkin 
ptr_ring_produce_irq(struct ptr_ring * r,void * ptr)1352e0ab8caSMichael S. Tsirkin static inline int ptr_ring_produce_irq(struct ptr_ring *r, void *ptr)
1362e0ab8caSMichael S. Tsirkin {
1372e0ab8caSMichael S. Tsirkin 	int ret;
1382e0ab8caSMichael S. Tsirkin 
1392e0ab8caSMichael S. Tsirkin 	spin_lock_irq(&r->producer_lock);
1402e0ab8caSMichael S. Tsirkin 	ret = __ptr_ring_produce(r, ptr);
1412e0ab8caSMichael S. Tsirkin 	spin_unlock_irq(&r->producer_lock);
1422e0ab8caSMichael S. Tsirkin 
1432e0ab8caSMichael S. Tsirkin 	return ret;
1442e0ab8caSMichael S. Tsirkin }
1452e0ab8caSMichael S. Tsirkin 
ptr_ring_produce_any(struct ptr_ring * r,void * ptr)1462e0ab8caSMichael S. Tsirkin static inline int ptr_ring_produce_any(struct ptr_ring *r, void *ptr)
1472e0ab8caSMichael S. Tsirkin {
1482e0ab8caSMichael S. Tsirkin 	unsigned long flags;
1492e0ab8caSMichael S. Tsirkin 	int ret;
1502e0ab8caSMichael S. Tsirkin 
1512e0ab8caSMichael S. Tsirkin 	spin_lock_irqsave(&r->producer_lock, flags);
1522e0ab8caSMichael S. Tsirkin 	ret = __ptr_ring_produce(r, ptr);
1532e0ab8caSMichael S. Tsirkin 	spin_unlock_irqrestore(&r->producer_lock, flags);
1542e0ab8caSMichael S. Tsirkin 
1552e0ab8caSMichael S. Tsirkin 	return ret;
1562e0ab8caSMichael S. Tsirkin }
1572e0ab8caSMichael S. Tsirkin 
ptr_ring_produce_bh(struct ptr_ring * r,void * ptr)1582e0ab8caSMichael S. Tsirkin static inline int ptr_ring_produce_bh(struct ptr_ring *r, void *ptr)
1592e0ab8caSMichael S. Tsirkin {
1602e0ab8caSMichael S. Tsirkin 	int ret;
1612e0ab8caSMichael S. Tsirkin 
1622e0ab8caSMichael S. Tsirkin 	spin_lock_bh(&r->producer_lock);
1632e0ab8caSMichael S. Tsirkin 	ret = __ptr_ring_produce(r, ptr);
1642e0ab8caSMichael S. Tsirkin 	spin_unlock_bh(&r->producer_lock);
1652e0ab8caSMichael S. Tsirkin 
1662e0ab8caSMichael S. Tsirkin 	return ret;
1672e0ab8caSMichael S. Tsirkin }
1682e0ab8caSMichael S. Tsirkin 
__ptr_ring_peek(struct ptr_ring * r)1692e0ab8caSMichael S. Tsirkin static inline void *__ptr_ring_peek(struct ptr_ring *r)
1702e0ab8caSMichael S. Tsirkin {
171982fb490SJason Wang 	if (likely(r->size))
172a07d29c6SMichael S. Tsirkin 		return READ_ONCE(r->queue[r->consumer_head]);
173982fb490SJason Wang 	return NULL;
1742e0ab8caSMichael S. Tsirkin }
1752e0ab8caSMichael S. Tsirkin 
1768619d384SMichael S. Tsirkin /*
1778619d384SMichael S. Tsirkin  * Test ring empty status without taking any locks.
1788619d384SMichael S. Tsirkin  *
1798619d384SMichael S. Tsirkin  * NB: This is only safe to call if ring is never resized.
1808619d384SMichael S. Tsirkin  *
1818619d384SMichael S. Tsirkin  * However, if some other CPU consumes ring entries at the same time, the value
1828619d384SMichael S. Tsirkin  * returned is not guaranteed to be correct.
1838619d384SMichael S. Tsirkin  *
1848619d384SMichael S. Tsirkin  * In this case - to avoid incorrectly detecting the ring
1858619d384SMichael S. Tsirkin  * as empty - the CPU consuming the ring entries is responsible
1868619d384SMichael S. Tsirkin  * for either consuming all ring entries until the ring is empty,
1878619d384SMichael S. Tsirkin  * or synchronizing with some other CPU and causing it to
1888619d384SMichael S. Tsirkin  * re-test __ptr_ring_empty and/or consume the ring enteries
1898619d384SMichael S. Tsirkin  * after the synchronization point.
1908619d384SMichael S. Tsirkin  *
1918619d384SMichael S. Tsirkin  * Note: callers invoking this in a loop must use a compiler barrier,
1928619d384SMichael S. Tsirkin  * for example cpu_relax().
1938619d384SMichael S. Tsirkin  */
__ptr_ring_empty(struct ptr_ring * r)1945d49de53SMichael S. Tsirkin static inline bool __ptr_ring_empty(struct ptr_ring *r)
1955d49de53SMichael S. Tsirkin {
196a259df36SMichael S. Tsirkin 	if (likely(r->size))
197a259df36SMichael S. Tsirkin 		return !r->queue[READ_ONCE(r->consumer_head)];
198a259df36SMichael S. Tsirkin 	return true;
1995d49de53SMichael S. Tsirkin }
2005d49de53SMichael S. Tsirkin 
ptr_ring_empty(struct ptr_ring * r)2012e0ab8caSMichael S. Tsirkin static inline bool ptr_ring_empty(struct ptr_ring *r)
2022e0ab8caSMichael S. Tsirkin {
2035d49de53SMichael S. Tsirkin 	bool ret;
2045d49de53SMichael S. Tsirkin 
2055d49de53SMichael S. Tsirkin 	spin_lock(&r->consumer_lock);
2065d49de53SMichael S. Tsirkin 	ret = __ptr_ring_empty(r);
2075d49de53SMichael S. Tsirkin 	spin_unlock(&r->consumer_lock);
2085d49de53SMichael S. Tsirkin 
2095d49de53SMichael S. Tsirkin 	return ret;
2105d49de53SMichael S. Tsirkin }
2115d49de53SMichael S. Tsirkin 
ptr_ring_empty_irq(struct ptr_ring * r)2125d49de53SMichael S. Tsirkin static inline bool ptr_ring_empty_irq(struct ptr_ring *r)
2135d49de53SMichael S. Tsirkin {
2145d49de53SMichael S. Tsirkin 	bool ret;
2155d49de53SMichael S. Tsirkin 
2165d49de53SMichael S. Tsirkin 	spin_lock_irq(&r->consumer_lock);
2175d49de53SMichael S. Tsirkin 	ret = __ptr_ring_empty(r);
2185d49de53SMichael S. Tsirkin 	spin_unlock_irq(&r->consumer_lock);
2195d49de53SMichael S. Tsirkin 
2205d49de53SMichael S. Tsirkin 	return ret;
2215d49de53SMichael S. Tsirkin }
2225d49de53SMichael S. Tsirkin 
ptr_ring_empty_any(struct ptr_ring * r)2235d49de53SMichael S. Tsirkin static inline bool ptr_ring_empty_any(struct ptr_ring *r)
2245d49de53SMichael S. Tsirkin {
2255d49de53SMichael S. Tsirkin 	unsigned long flags;
2265d49de53SMichael S. Tsirkin 	bool ret;
2275d49de53SMichael S. Tsirkin 
2285d49de53SMichael S. Tsirkin 	spin_lock_irqsave(&r->consumer_lock, flags);
2295d49de53SMichael S. Tsirkin 	ret = __ptr_ring_empty(r);
2305d49de53SMichael S. Tsirkin 	spin_unlock_irqrestore(&r->consumer_lock, flags);
2315d49de53SMichael S. Tsirkin 
2325d49de53SMichael S. Tsirkin 	return ret;
2335d49de53SMichael S. Tsirkin }
2345d49de53SMichael S. Tsirkin 
ptr_ring_empty_bh(struct ptr_ring * r)2355d49de53SMichael S. Tsirkin static inline bool ptr_ring_empty_bh(struct ptr_ring *r)
2365d49de53SMichael S. Tsirkin {
2375d49de53SMichael S. Tsirkin 	bool ret;
2385d49de53SMichael S. Tsirkin 
2395d49de53SMichael S. Tsirkin 	spin_lock_bh(&r->consumer_lock);
2405d49de53SMichael S. Tsirkin 	ret = __ptr_ring_empty(r);
2415d49de53SMichael S. Tsirkin 	spin_unlock_bh(&r->consumer_lock);
2425d49de53SMichael S. Tsirkin 
2435d49de53SMichael S. Tsirkin 	return ret;
2442e0ab8caSMichael S. Tsirkin }
2452e0ab8caSMichael S. Tsirkin 
2462e0ab8caSMichael S. Tsirkin /* Must only be called after __ptr_ring_peek returned !NULL */
__ptr_ring_discard_one(struct ptr_ring * r)2472e0ab8caSMichael S. Tsirkin static inline void __ptr_ring_discard_one(struct ptr_ring *r)
2482e0ab8caSMichael S. Tsirkin {
249fb9de970SMichael S. Tsirkin 	/* Fundamentally, what we want to do is update consumer
250fb9de970SMichael S. Tsirkin 	 * index and zero out the entry so producer can reuse it.
251fb9de970SMichael S. Tsirkin 	 * Doing it naively at each consume would be as simple as:
252406de755SMichael S. Tsirkin 	 *       consumer = r->consumer;
253406de755SMichael S. Tsirkin 	 *       r->queue[consumer++] = NULL;
254406de755SMichael S. Tsirkin 	 *       if (unlikely(consumer >= r->size))
255406de755SMichael S. Tsirkin 	 *               consumer = 0;
256406de755SMichael S. Tsirkin 	 *       r->consumer = consumer;
257fb9de970SMichael S. Tsirkin 	 * but that is suboptimal when the ring is full as producer is writing
258fb9de970SMichael S. Tsirkin 	 * out new entries in the same cache line.  Defer these updates until a
259fb9de970SMichael S. Tsirkin 	 * batch of entries has been consumed.
260fb9de970SMichael S. Tsirkin 	 */
261406de755SMichael S. Tsirkin 	/* Note: we must keep consumer_head valid at all times for __ptr_ring_empty
262406de755SMichael S. Tsirkin 	 * to work correctly.
263406de755SMichael S. Tsirkin 	 */
264406de755SMichael S. Tsirkin 	int consumer_head = r->consumer_head;
265406de755SMichael S. Tsirkin 	int head = consumer_head++;
266fb9de970SMichael S. Tsirkin 
267fb9de970SMichael S. Tsirkin 	/* Once we have processed enough entries invalidate them in
268fb9de970SMichael S. Tsirkin 	 * the ring all at once so producer can reuse their space in the ring.
269fb9de970SMichael S. Tsirkin 	 * We also do this when we reach end of the ring - not mandatory
270fb9de970SMichael S. Tsirkin 	 * but helps keep the implementation simple.
271fb9de970SMichael S. Tsirkin 	 */
272406de755SMichael S. Tsirkin 	if (unlikely(consumer_head - r->consumer_tail >= r->batch ||
273406de755SMichael S. Tsirkin 		     consumer_head >= r->size)) {
274fb9de970SMichael S. Tsirkin 		/* Zero out entries in the reverse order: this way we touch the
275fb9de970SMichael S. Tsirkin 		 * cache line that producer might currently be reading the last;
276fb9de970SMichael S. Tsirkin 		 * producer won't make progress and touch other cache lines
277fb9de970SMichael S. Tsirkin 		 * besides the first one until we write out all entries.
278fb9de970SMichael S. Tsirkin 		 */
279fb9de970SMichael S. Tsirkin 		while (likely(head >= r->consumer_tail))
280fb9de970SMichael S. Tsirkin 			r->queue[head--] = NULL;
281406de755SMichael S. Tsirkin 		r->consumer_tail = consumer_head;
282fb9de970SMichael S. Tsirkin 	}
283406de755SMichael S. Tsirkin 	if (unlikely(consumer_head >= r->size)) {
284406de755SMichael S. Tsirkin 		consumer_head = 0;
285fb9de970SMichael S. Tsirkin 		r->consumer_tail = 0;
286fb9de970SMichael S. Tsirkin 	}
287a259df36SMichael S. Tsirkin 	/* matching READ_ONCE in __ptr_ring_empty for lockless tests */
288a259df36SMichael S. Tsirkin 	WRITE_ONCE(r->consumer_head, consumer_head);
2892e0ab8caSMichael S. Tsirkin }
2902e0ab8caSMichael S. Tsirkin 
__ptr_ring_consume(struct ptr_ring * r)2912e0ab8caSMichael S. Tsirkin static inline void *__ptr_ring_consume(struct ptr_ring *r)
2922e0ab8caSMichael S. Tsirkin {
2932e0ab8caSMichael S. Tsirkin 	void *ptr;
2942e0ab8caSMichael S. Tsirkin 
295e3f9f417SAndrea Parri 	/* The READ_ONCE in __ptr_ring_peek guarantees that anyone
296e3f9f417SAndrea Parri 	 * accessing data through the pointer is up to date. Pairs
297e3f9f417SAndrea Parri 	 * with smp_wmb in __ptr_ring_produce.
298e3f9f417SAndrea Parri 	 */
2992e0ab8caSMichael S. Tsirkin 	ptr = __ptr_ring_peek(r);
3002e0ab8caSMichael S. Tsirkin 	if (ptr)
3012e0ab8caSMichael S. Tsirkin 		__ptr_ring_discard_one(r);
3022e0ab8caSMichael S. Tsirkin 
3032e0ab8caSMichael S. Tsirkin 	return ptr;
3042e0ab8caSMichael S. Tsirkin }
3052e0ab8caSMichael S. Tsirkin 
__ptr_ring_consume_batched(struct ptr_ring * r,void ** array,int n)306728fc8d5SJason Wang static inline int __ptr_ring_consume_batched(struct ptr_ring *r,
307728fc8d5SJason Wang 					     void **array, int n)
308728fc8d5SJason Wang {
309728fc8d5SJason Wang 	void *ptr;
310728fc8d5SJason Wang 	int i;
311728fc8d5SJason Wang 
312728fc8d5SJason Wang 	for (i = 0; i < n; i++) {
313728fc8d5SJason Wang 		ptr = __ptr_ring_consume(r);
314728fc8d5SJason Wang 		if (!ptr)
315728fc8d5SJason Wang 			break;
316728fc8d5SJason Wang 		array[i] = ptr;
317728fc8d5SJason Wang 	}
318728fc8d5SJason Wang 
319728fc8d5SJason Wang 	return i;
320728fc8d5SJason Wang }
321728fc8d5SJason Wang 
322e7169530SMichael S. Tsirkin /*
323e7169530SMichael S. Tsirkin  * Note: resize (below) nests producer lock within consumer lock, so if you
324e7169530SMichael S. Tsirkin  * call this in interrupt or BH context, you must disable interrupts/BH when
325e7169530SMichael S. Tsirkin  * producing.
326e7169530SMichael S. Tsirkin  */
ptr_ring_consume(struct ptr_ring * r)3272e0ab8caSMichael S. Tsirkin static inline void *ptr_ring_consume(struct ptr_ring *r)
3282e0ab8caSMichael S. Tsirkin {
3292e0ab8caSMichael S. Tsirkin 	void *ptr;
3302e0ab8caSMichael S. Tsirkin 
3312e0ab8caSMichael S. Tsirkin 	spin_lock(&r->consumer_lock);
3322e0ab8caSMichael S. Tsirkin 	ptr = __ptr_ring_consume(r);
3332e0ab8caSMichael S. Tsirkin 	spin_unlock(&r->consumer_lock);
3342e0ab8caSMichael S. Tsirkin 
3352e0ab8caSMichael S. Tsirkin 	return ptr;
3362e0ab8caSMichael S. Tsirkin }
3372e0ab8caSMichael S. Tsirkin 
ptr_ring_consume_irq(struct ptr_ring * r)3382e0ab8caSMichael S. Tsirkin static inline void *ptr_ring_consume_irq(struct ptr_ring *r)
3392e0ab8caSMichael S. Tsirkin {
3402e0ab8caSMichael S. Tsirkin 	void *ptr;
3412e0ab8caSMichael S. Tsirkin 
3422e0ab8caSMichael S. Tsirkin 	spin_lock_irq(&r->consumer_lock);
3432e0ab8caSMichael S. Tsirkin 	ptr = __ptr_ring_consume(r);
3442e0ab8caSMichael S. Tsirkin 	spin_unlock_irq(&r->consumer_lock);
3452e0ab8caSMichael S. Tsirkin 
3462e0ab8caSMichael S. Tsirkin 	return ptr;
3472e0ab8caSMichael S. Tsirkin }
3482e0ab8caSMichael S. Tsirkin 
ptr_ring_consume_any(struct ptr_ring * r)3492e0ab8caSMichael S. Tsirkin static inline void *ptr_ring_consume_any(struct ptr_ring *r)
3502e0ab8caSMichael S. Tsirkin {
3512e0ab8caSMichael S. Tsirkin 	unsigned long flags;
3522e0ab8caSMichael S. Tsirkin 	void *ptr;
3532e0ab8caSMichael S. Tsirkin 
3542e0ab8caSMichael S. Tsirkin 	spin_lock_irqsave(&r->consumer_lock, flags);
3552e0ab8caSMichael S. Tsirkin 	ptr = __ptr_ring_consume(r);
3562e0ab8caSMichael S. Tsirkin 	spin_unlock_irqrestore(&r->consumer_lock, flags);
3572e0ab8caSMichael S. Tsirkin 
3582e0ab8caSMichael S. Tsirkin 	return ptr;
3592e0ab8caSMichael S. Tsirkin }
3602e0ab8caSMichael S. Tsirkin 
ptr_ring_consume_bh(struct ptr_ring * r)3612e0ab8caSMichael S. Tsirkin static inline void *ptr_ring_consume_bh(struct ptr_ring *r)
3622e0ab8caSMichael S. Tsirkin {
3632e0ab8caSMichael S. Tsirkin 	void *ptr;
3642e0ab8caSMichael S. Tsirkin 
3652e0ab8caSMichael S. Tsirkin 	spin_lock_bh(&r->consumer_lock);
3662e0ab8caSMichael S. Tsirkin 	ptr = __ptr_ring_consume(r);
3672e0ab8caSMichael S. Tsirkin 	spin_unlock_bh(&r->consumer_lock);
3682e0ab8caSMichael S. Tsirkin 
3692e0ab8caSMichael S. Tsirkin 	return ptr;
3702e0ab8caSMichael S. Tsirkin }
3712e0ab8caSMichael S. Tsirkin 
ptr_ring_consume_batched(struct ptr_ring * r,void ** array,int n)372728fc8d5SJason Wang static inline int ptr_ring_consume_batched(struct ptr_ring *r,
373728fc8d5SJason Wang 					   void **array, int n)
374728fc8d5SJason Wang {
375728fc8d5SJason Wang 	int ret;
376728fc8d5SJason Wang 
377728fc8d5SJason Wang 	spin_lock(&r->consumer_lock);
378728fc8d5SJason Wang 	ret = __ptr_ring_consume_batched(r, array, n);
379728fc8d5SJason Wang 	spin_unlock(&r->consumer_lock);
380728fc8d5SJason Wang 
381728fc8d5SJason Wang 	return ret;
382728fc8d5SJason Wang }
383728fc8d5SJason Wang 
ptr_ring_consume_batched_irq(struct ptr_ring * r,void ** array,int n)384728fc8d5SJason Wang static inline int ptr_ring_consume_batched_irq(struct ptr_ring *r,
385728fc8d5SJason Wang 					       void **array, int n)
386728fc8d5SJason Wang {
387728fc8d5SJason Wang 	int ret;
388728fc8d5SJason Wang 
389728fc8d5SJason Wang 	spin_lock_irq(&r->consumer_lock);
390728fc8d5SJason Wang 	ret = __ptr_ring_consume_batched(r, array, n);
391728fc8d5SJason Wang 	spin_unlock_irq(&r->consumer_lock);
392728fc8d5SJason Wang 
393728fc8d5SJason Wang 	return ret;
394728fc8d5SJason Wang }
395728fc8d5SJason Wang 
ptr_ring_consume_batched_any(struct ptr_ring * r,void ** array,int n)396728fc8d5SJason Wang static inline int ptr_ring_consume_batched_any(struct ptr_ring *r,
397728fc8d5SJason Wang 					       void **array, int n)
398728fc8d5SJason Wang {
399728fc8d5SJason Wang 	unsigned long flags;
400728fc8d5SJason Wang 	int ret;
401728fc8d5SJason Wang 
402728fc8d5SJason Wang 	spin_lock_irqsave(&r->consumer_lock, flags);
403728fc8d5SJason Wang 	ret = __ptr_ring_consume_batched(r, array, n);
404728fc8d5SJason Wang 	spin_unlock_irqrestore(&r->consumer_lock, flags);
405728fc8d5SJason Wang 
406728fc8d5SJason Wang 	return ret;
407728fc8d5SJason Wang }
408728fc8d5SJason Wang 
ptr_ring_consume_batched_bh(struct ptr_ring * r,void ** array,int n)409728fc8d5SJason Wang static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,
410728fc8d5SJason Wang 					      void **array, int n)
411728fc8d5SJason Wang {
412728fc8d5SJason Wang 	int ret;
413728fc8d5SJason Wang 
414728fc8d5SJason Wang 	spin_lock_bh(&r->consumer_lock);
415728fc8d5SJason Wang 	ret = __ptr_ring_consume_batched(r, array, n);
416728fc8d5SJason Wang 	spin_unlock_bh(&r->consumer_lock);
417728fc8d5SJason Wang 
418728fc8d5SJason Wang 	return ret;
419728fc8d5SJason Wang }
420728fc8d5SJason Wang 
4212e0ab8caSMichael S. Tsirkin /* Cast to structure type and call a function without discarding from FIFO.
4222e0ab8caSMichael S. Tsirkin  * Function must return a value.
4232e0ab8caSMichael S. Tsirkin  * Callers must take consumer_lock.
4242e0ab8caSMichael S. Tsirkin  */
4252e0ab8caSMichael S. Tsirkin #define __PTR_RING_PEEK_CALL(r, f) ((f)(__ptr_ring_peek(r)))
4262e0ab8caSMichael S. Tsirkin 
4272e0ab8caSMichael S. Tsirkin #define PTR_RING_PEEK_CALL(r, f) ({ \
4282e0ab8caSMichael S. Tsirkin 	typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
4292e0ab8caSMichael S. Tsirkin 	\
4302e0ab8caSMichael S. Tsirkin 	spin_lock(&(r)->consumer_lock); \
4312e0ab8caSMichael S. Tsirkin 	__PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
4322e0ab8caSMichael S. Tsirkin 	spin_unlock(&(r)->consumer_lock); \
4332e0ab8caSMichael S. Tsirkin 	__PTR_RING_PEEK_CALL_v; \
4342e0ab8caSMichael S. Tsirkin })
4352e0ab8caSMichael S. Tsirkin 
4362e0ab8caSMichael S. Tsirkin #define PTR_RING_PEEK_CALL_IRQ(r, f) ({ \
4372e0ab8caSMichael S. Tsirkin 	typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
4382e0ab8caSMichael S. Tsirkin 	\
4392e0ab8caSMichael S. Tsirkin 	spin_lock_irq(&(r)->consumer_lock); \
4402e0ab8caSMichael S. Tsirkin 	__PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
4412e0ab8caSMichael S. Tsirkin 	spin_unlock_irq(&(r)->consumer_lock); \
4422e0ab8caSMichael S. Tsirkin 	__PTR_RING_PEEK_CALL_v; \
4432e0ab8caSMichael S. Tsirkin })
4442e0ab8caSMichael S. Tsirkin 
4452e0ab8caSMichael S. Tsirkin #define PTR_RING_PEEK_CALL_BH(r, f) ({ \
4462e0ab8caSMichael S. Tsirkin 	typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
4472e0ab8caSMichael S. Tsirkin 	\
4482e0ab8caSMichael S. Tsirkin 	spin_lock_bh(&(r)->consumer_lock); \
4492e0ab8caSMichael S. Tsirkin 	__PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
4502e0ab8caSMichael S. Tsirkin 	spin_unlock_bh(&(r)->consumer_lock); \
4512e0ab8caSMichael S. Tsirkin 	__PTR_RING_PEEK_CALL_v; \
4522e0ab8caSMichael S. Tsirkin })
4532e0ab8caSMichael S. Tsirkin 
4542e0ab8caSMichael S. Tsirkin #define PTR_RING_PEEK_CALL_ANY(r, f) ({ \
4552e0ab8caSMichael S. Tsirkin 	typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
4562e0ab8caSMichael S. Tsirkin 	unsigned long __PTR_RING_PEEK_CALL_f;\
4572e0ab8caSMichael S. Tsirkin 	\
4582e0ab8caSMichael S. Tsirkin 	spin_lock_irqsave(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
4592e0ab8caSMichael S. Tsirkin 	__PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
4602e0ab8caSMichael S. Tsirkin 	spin_unlock_irqrestore(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
4612e0ab8caSMichael S. Tsirkin 	__PTR_RING_PEEK_CALL_v; \
4622e0ab8caSMichael S. Tsirkin })
4632e0ab8caSMichael S. Tsirkin 
4640bf7800fSJason Wang /* Not all gfp_t flags (besides GFP_KERNEL) are allowed. See
4650bf7800fSJason Wang  * documentation for vmalloc for which of them are legal.
4660bf7800fSJason Wang  */
__ptr_ring_init_queue_alloc(unsigned int size,gfp_t gfp)46781fbfe8aSEric Dumazet static inline void **__ptr_ring_init_queue_alloc(unsigned int size, gfp_t gfp)
4685d49de53SMichael S. Tsirkin {
46954e02162SJason Wang 	if (size > KMALLOC_MAX_SIZE / sizeof(void *))
4706e6e41c3SJason Wang 		return NULL;
4710bf7800fSJason Wang 	return kvmalloc_array(size, sizeof(void *), gfp | __GFP_ZERO);
4725d49de53SMichael S. Tsirkin }
4735d49de53SMichael S. Tsirkin 
__ptr_ring_set_size(struct ptr_ring * r,int size)474fb9de970SMichael S. Tsirkin static inline void __ptr_ring_set_size(struct ptr_ring *r, int size)
475fb9de970SMichael S. Tsirkin {
476fb9de970SMichael S. Tsirkin 	r->size = size;
477fb9de970SMichael S. Tsirkin 	r->batch = SMP_CACHE_BYTES * 2 / sizeof(*(r->queue));
478fb9de970SMichael S. Tsirkin 	/* We need to set batch at least to 1 to make logic
479fb9de970SMichael S. Tsirkin 	 * in __ptr_ring_discard_one work correctly.
480fb9de970SMichael S. Tsirkin 	 * Batching too much (because ring is small) would cause a lot of
481fb9de970SMichael S. Tsirkin 	 * burstiness. Needs tuning, for now disable batching.
482fb9de970SMichael S. Tsirkin 	 */
483fb9de970SMichael S. Tsirkin 	if (r->batch > r->size / 2 || !r->batch)
484fb9de970SMichael S. Tsirkin 		r->batch = 1;
485fb9de970SMichael S. Tsirkin }
486fb9de970SMichael S. Tsirkin 
ptr_ring_init(struct ptr_ring * r,int size,gfp_t gfp)4872e0ab8caSMichael S. Tsirkin static inline int ptr_ring_init(struct ptr_ring *r, int size, gfp_t gfp)
4882e0ab8caSMichael S. Tsirkin {
4895d49de53SMichael S. Tsirkin 	r->queue = __ptr_ring_init_queue_alloc(size, gfp);
4902e0ab8caSMichael S. Tsirkin 	if (!r->queue)
4912e0ab8caSMichael S. Tsirkin 		return -ENOMEM;
4922e0ab8caSMichael S. Tsirkin 
493fb9de970SMichael S. Tsirkin 	__ptr_ring_set_size(r, size);
494fb9de970SMichael S. Tsirkin 	r->producer = r->consumer_head = r->consumer_tail = 0;
4952e0ab8caSMichael S. Tsirkin 	spin_lock_init(&r->producer_lock);
4962e0ab8caSMichael S. Tsirkin 	spin_lock_init(&r->consumer_lock);
4972e0ab8caSMichael S. Tsirkin 
4982e0ab8caSMichael S. Tsirkin 	return 0;
4992e0ab8caSMichael S. Tsirkin }
5002e0ab8caSMichael S. Tsirkin 
501197a5212SMichael S. Tsirkin /*
502197a5212SMichael S. Tsirkin  * Return entries into ring. Destroy entries that don't fit.
503197a5212SMichael S. Tsirkin  *
504197a5212SMichael S. Tsirkin  * Note: this is expected to be a rare slow path operation.
505197a5212SMichael S. Tsirkin  *
506197a5212SMichael S. Tsirkin  * Note: producer lock is nested within consumer lock, so if you
507197a5212SMichael S. Tsirkin  * resize you must make sure all uses nest correctly.
508197a5212SMichael S. Tsirkin  * In particular if you consume ring in interrupt or BH context, you must
509197a5212SMichael S. Tsirkin  * disable interrupts/BH when doing so.
510197a5212SMichael S. Tsirkin  */
ptr_ring_unconsume(struct ptr_ring * r,void ** batch,int n,void (* destroy)(void *))511197a5212SMichael S. Tsirkin static inline void ptr_ring_unconsume(struct ptr_ring *r, void **batch, int n,
512197a5212SMichael S. Tsirkin 				      void (*destroy)(void *))
513197a5212SMichael S. Tsirkin {
514197a5212SMichael S. Tsirkin 	unsigned long flags;
515197a5212SMichael S. Tsirkin 	int head;
516197a5212SMichael S. Tsirkin 
517197a5212SMichael S. Tsirkin 	spin_lock_irqsave(&r->consumer_lock, flags);
518197a5212SMichael S. Tsirkin 	spin_lock(&r->producer_lock);
519197a5212SMichael S. Tsirkin 
520197a5212SMichael S. Tsirkin 	if (!r->size)
521197a5212SMichael S. Tsirkin 		goto done;
522197a5212SMichael S. Tsirkin 
523197a5212SMichael S. Tsirkin 	/*
524197a5212SMichael S. Tsirkin 	 * Clean out buffered entries (for simplicity). This way following code
525197a5212SMichael S. Tsirkin 	 * can test entries for NULL and if not assume they are valid.
526197a5212SMichael S. Tsirkin 	 */
527197a5212SMichael S. Tsirkin 	head = r->consumer_head - 1;
528197a5212SMichael S. Tsirkin 	while (likely(head >= r->consumer_tail))
529197a5212SMichael S. Tsirkin 		r->queue[head--] = NULL;
530197a5212SMichael S. Tsirkin 	r->consumer_tail = r->consumer_head;
531197a5212SMichael S. Tsirkin 
532197a5212SMichael S. Tsirkin 	/*
533197a5212SMichael S. Tsirkin 	 * Go over entries in batch, start moving head back and copy entries.
534197a5212SMichael S. Tsirkin 	 * Stop when we run into previously unconsumed entries.
535197a5212SMichael S. Tsirkin 	 */
536197a5212SMichael S. Tsirkin 	while (n) {
537197a5212SMichael S. Tsirkin 		head = r->consumer_head - 1;
538197a5212SMichael S. Tsirkin 		if (head < 0)
539197a5212SMichael S. Tsirkin 			head = r->size - 1;
540197a5212SMichael S. Tsirkin 		if (r->queue[head]) {
541197a5212SMichael S. Tsirkin 			/* This batch entry will have to be destroyed. */
542197a5212SMichael S. Tsirkin 			goto done;
543197a5212SMichael S. Tsirkin 		}
544197a5212SMichael S. Tsirkin 		r->queue[head] = batch[--n];
545a259df36SMichael S. Tsirkin 		r->consumer_tail = head;
546a259df36SMichael S. Tsirkin 		/* matching READ_ONCE in __ptr_ring_empty for lockless tests */
547a259df36SMichael S. Tsirkin 		WRITE_ONCE(r->consumer_head, head);
548197a5212SMichael S. Tsirkin 	}
549197a5212SMichael S. Tsirkin 
550197a5212SMichael S. Tsirkin done:
551197a5212SMichael S. Tsirkin 	/* Destroy all entries left in the batch. */
552197a5212SMichael S. Tsirkin 	while (n)
553197a5212SMichael S. Tsirkin 		destroy(batch[--n]);
554197a5212SMichael S. Tsirkin 	spin_unlock(&r->producer_lock);
555197a5212SMichael S. Tsirkin 	spin_unlock_irqrestore(&r->consumer_lock, flags);
556197a5212SMichael S. Tsirkin }
557197a5212SMichael S. Tsirkin 
__ptr_ring_swap_queue(struct ptr_ring * r,void ** queue,int size,gfp_t gfp,void (* destroy)(void *))55859e6ae53SMichael S. Tsirkin static inline void **__ptr_ring_swap_queue(struct ptr_ring *r, void **queue,
55959e6ae53SMichael S. Tsirkin 					   int size, gfp_t gfp,
5605d49de53SMichael S. Tsirkin 					   void (*destroy)(void *))
5612e0ab8caSMichael S. Tsirkin {
5625d49de53SMichael S. Tsirkin 	int producer = 0;
5635d49de53SMichael S. Tsirkin 	void **old;
5645d49de53SMichael S. Tsirkin 	void *ptr;
5655d49de53SMichael S. Tsirkin 
566e7169530SMichael S. Tsirkin 	while ((ptr = __ptr_ring_consume(r)))
5675d49de53SMichael S. Tsirkin 		if (producer < size)
5685d49de53SMichael S. Tsirkin 			queue[producer++] = ptr;
5695d49de53SMichael S. Tsirkin 		else if (destroy)
5705d49de53SMichael S. Tsirkin 			destroy(ptr);
5715d49de53SMichael S. Tsirkin 
572aff6db45SCong Wang 	if (producer >= size)
573aff6db45SCong Wang 		producer = 0;
574fb9de970SMichael S. Tsirkin 	__ptr_ring_set_size(r, size);
5755d49de53SMichael S. Tsirkin 	r->producer = producer;
576fb9de970SMichael S. Tsirkin 	r->consumer_head = 0;
577fb9de970SMichael S. Tsirkin 	r->consumer_tail = 0;
5785d49de53SMichael S. Tsirkin 	old = r->queue;
5795d49de53SMichael S. Tsirkin 	r->queue = queue;
5805d49de53SMichael S. Tsirkin 
58159e6ae53SMichael S. Tsirkin 	return old;
58259e6ae53SMichael S. Tsirkin }
58359e6ae53SMichael S. Tsirkin 
584e7169530SMichael S. Tsirkin /*
585e7169530SMichael S. Tsirkin  * Note: producer lock is nested within consumer lock, so if you
586e7169530SMichael S. Tsirkin  * resize you must make sure all uses nest correctly.
587e7169530SMichael S. Tsirkin  * In particular if you consume ring in interrupt or BH context, you must
588e7169530SMichael S. Tsirkin  * disable interrupts/BH when doing so.
589e7169530SMichael S. Tsirkin  */
ptr_ring_resize(struct ptr_ring * r,int size,gfp_t gfp,void (* destroy)(void *))59059e6ae53SMichael S. Tsirkin static inline int ptr_ring_resize(struct ptr_ring *r, int size, gfp_t gfp,
59159e6ae53SMichael S. Tsirkin 				  void (*destroy)(void *))
59259e6ae53SMichael S. Tsirkin {
59359e6ae53SMichael S. Tsirkin 	unsigned long flags;
59459e6ae53SMichael S. Tsirkin 	void **queue = __ptr_ring_init_queue_alloc(size, gfp);
59559e6ae53SMichael S. Tsirkin 	void **old;
59659e6ae53SMichael S. Tsirkin 
59759e6ae53SMichael S. Tsirkin 	if (!queue)
59859e6ae53SMichael S. Tsirkin 		return -ENOMEM;
59959e6ae53SMichael S. Tsirkin 
600e7169530SMichael S. Tsirkin 	spin_lock_irqsave(&(r)->consumer_lock, flags);
601e7169530SMichael S. Tsirkin 	spin_lock(&(r)->producer_lock);
60259e6ae53SMichael S. Tsirkin 
60359e6ae53SMichael S. Tsirkin 	old = __ptr_ring_swap_queue(r, queue, size, gfp, destroy);
60459e6ae53SMichael S. Tsirkin 
605e7169530SMichael S. Tsirkin 	spin_unlock(&(r)->producer_lock);
606e7169530SMichael S. Tsirkin 	spin_unlock_irqrestore(&(r)->consumer_lock, flags);
6075d49de53SMichael S. Tsirkin 
6080bf7800fSJason Wang 	kvfree(old);
6095d49de53SMichael S. Tsirkin 
6105d49de53SMichael S. Tsirkin 	return 0;
6115d49de53SMichael S. Tsirkin }
6125d49de53SMichael S. Tsirkin 
613e7169530SMichael S. Tsirkin /*
614e7169530SMichael S. Tsirkin  * Note: producer lock is nested within consumer lock, so if you
615e7169530SMichael S. Tsirkin  * resize you must make sure all uses nest correctly.
616e7169530SMichael S. Tsirkin  * In particular if you consume ring in interrupt or BH context, you must
617e7169530SMichael S. Tsirkin  * disable interrupts/BH when doing so.
618e7169530SMichael S. Tsirkin  */
ptr_ring_resize_multiple(struct ptr_ring ** rings,unsigned int nrings,int size,gfp_t gfp,void (* destroy)(void *))61981fbfe8aSEric Dumazet static inline int ptr_ring_resize_multiple(struct ptr_ring **rings,
62081fbfe8aSEric Dumazet 					   unsigned int nrings,
62159e6ae53SMichael S. Tsirkin 					   int size,
62259e6ae53SMichael S. Tsirkin 					   gfp_t gfp, void (*destroy)(void *))
62359e6ae53SMichael S. Tsirkin {
62459e6ae53SMichael S. Tsirkin 	unsigned long flags;
62559e6ae53SMichael S. Tsirkin 	void ***queues;
62659e6ae53SMichael S. Tsirkin 	int i;
62759e6ae53SMichael S. Tsirkin 
62881fbfe8aSEric Dumazet 	queues = kmalloc_array(nrings, sizeof(*queues), gfp);
62959e6ae53SMichael S. Tsirkin 	if (!queues)
63059e6ae53SMichael S. Tsirkin 		goto noqueues;
63159e6ae53SMichael S. Tsirkin 
63259e6ae53SMichael S. Tsirkin 	for (i = 0; i < nrings; ++i) {
63359e6ae53SMichael S. Tsirkin 		queues[i] = __ptr_ring_init_queue_alloc(size, gfp);
63459e6ae53SMichael S. Tsirkin 		if (!queues[i])
63559e6ae53SMichael S. Tsirkin 			goto nomem;
63659e6ae53SMichael S. Tsirkin 	}
63759e6ae53SMichael S. Tsirkin 
63859e6ae53SMichael S. Tsirkin 	for (i = 0; i < nrings; ++i) {
639e7169530SMichael S. Tsirkin 		spin_lock_irqsave(&(rings[i])->consumer_lock, flags);
640e7169530SMichael S. Tsirkin 		spin_lock(&(rings[i])->producer_lock);
64159e6ae53SMichael S. Tsirkin 		queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
64259e6ae53SMichael S. Tsirkin 						  size, gfp, destroy);
643e7169530SMichael S. Tsirkin 		spin_unlock(&(rings[i])->producer_lock);
644e7169530SMichael S. Tsirkin 		spin_unlock_irqrestore(&(rings[i])->consumer_lock, flags);
64559e6ae53SMichael S. Tsirkin 	}
64659e6ae53SMichael S. Tsirkin 
64759e6ae53SMichael S. Tsirkin 	for (i = 0; i < nrings; ++i)
6480bf7800fSJason Wang 		kvfree(queues[i]);
64959e6ae53SMichael S. Tsirkin 
65059e6ae53SMichael S. Tsirkin 	kfree(queues);
65159e6ae53SMichael S. Tsirkin 
65259e6ae53SMichael S. Tsirkin 	return 0;
65359e6ae53SMichael S. Tsirkin 
65459e6ae53SMichael S. Tsirkin nomem:
65559e6ae53SMichael S. Tsirkin 	while (--i >= 0)
6560bf7800fSJason Wang 		kvfree(queues[i]);
65759e6ae53SMichael S. Tsirkin 
65859e6ae53SMichael S. Tsirkin 	kfree(queues);
65959e6ae53SMichael S. Tsirkin 
66059e6ae53SMichael S. Tsirkin noqueues:
66159e6ae53SMichael S. Tsirkin 	return -ENOMEM;
66259e6ae53SMichael S. Tsirkin }
66359e6ae53SMichael S. Tsirkin 
ptr_ring_cleanup(struct ptr_ring * r,void (* destroy)(void *))6645d49de53SMichael S. Tsirkin static inline void ptr_ring_cleanup(struct ptr_ring *r, void (*destroy)(void *))
6655d49de53SMichael S. Tsirkin {
6665d49de53SMichael S. Tsirkin 	void *ptr;
6675d49de53SMichael S. Tsirkin 
6685d49de53SMichael S. Tsirkin 	if (destroy)
6695d49de53SMichael S. Tsirkin 		while ((ptr = ptr_ring_consume(r)))
6705d49de53SMichael S. Tsirkin 			destroy(ptr);
6710bf7800fSJason Wang 	kvfree(r->queue);
6722e0ab8caSMichael S. Tsirkin }
6732e0ab8caSMichael S. Tsirkin 
6742e0ab8caSMichael S. Tsirkin #endif /* _LINUX_PTR_RING_H  */
675