xref: /linux-6.15/include/linux/ptr_ring.h (revision a259df36)
1 /*
2  *	Definitions for the 'struct ptr_ring' datastructure.
3  *
4  *	Author:
5  *		Michael S. Tsirkin <[email protected]>
6  *
7  *	Copyright (C) 2016 Red Hat, Inc.
8  *
9  *	This program is free software; you can redistribute it and/or modify it
10  *	under the terms of the GNU General Public License as published by the
11  *	Free Software Foundation; either version 2 of the License, or (at your
12  *	option) any later version.
13  *
14  *	This is a limited-size FIFO maintaining pointers in FIFO order, with
15  *	one CPU producing entries and another consuming entries from a FIFO.
16  *
17  *	This implementation tries to minimize cache-contention when there is a
18  *	single producer and a single consumer CPU.
19  */
20 
21 #ifndef _LINUX_PTR_RING_H
22 #define _LINUX_PTR_RING_H 1
23 
24 #ifdef __KERNEL__
25 #include <linux/spinlock.h>
26 #include <linux/cache.h>
27 #include <linux/types.h>
28 #include <linux/compiler.h>
29 #include <linux/cache.h>
30 #include <linux/slab.h>
31 #include <asm/errno.h>
32 #endif
33 
34 struct ptr_ring {
35 	int producer ____cacheline_aligned_in_smp;
36 	spinlock_t producer_lock;
37 	int consumer_head ____cacheline_aligned_in_smp; /* next valid entry */
38 	int consumer_tail; /* next entry to invalidate */
39 	spinlock_t consumer_lock;
40 	/* Shared consumer/producer data */
41 	/* Read-only by both the producer and the consumer */
42 	int size ____cacheline_aligned_in_smp; /* max entries in queue */
43 	int batch; /* number of entries to consume in a batch */
44 	void **queue;
45 };
46 
47 /* Note: callers invoking this in a loop must use a compiler barrier,
48  * for example cpu_relax().  If ring is ever resized, callers must hold
49  * producer_lock - see e.g. ptr_ring_full.  Otherwise, if callers don't hold
50  * producer_lock, the next call to __ptr_ring_produce may fail.
51  */
52 static inline bool __ptr_ring_full(struct ptr_ring *r)
53 {
54 	return r->queue[r->producer];
55 }
56 
57 static inline bool ptr_ring_full(struct ptr_ring *r)
58 {
59 	bool ret;
60 
61 	spin_lock(&r->producer_lock);
62 	ret = __ptr_ring_full(r);
63 	spin_unlock(&r->producer_lock);
64 
65 	return ret;
66 }
67 
68 static inline bool ptr_ring_full_irq(struct ptr_ring *r)
69 {
70 	bool ret;
71 
72 	spin_lock_irq(&r->producer_lock);
73 	ret = __ptr_ring_full(r);
74 	spin_unlock_irq(&r->producer_lock);
75 
76 	return ret;
77 }
78 
79 static inline bool ptr_ring_full_any(struct ptr_ring *r)
80 {
81 	unsigned long flags;
82 	bool ret;
83 
84 	spin_lock_irqsave(&r->producer_lock, flags);
85 	ret = __ptr_ring_full(r);
86 	spin_unlock_irqrestore(&r->producer_lock, flags);
87 
88 	return ret;
89 }
90 
91 static inline bool ptr_ring_full_bh(struct ptr_ring *r)
92 {
93 	bool ret;
94 
95 	spin_lock_bh(&r->producer_lock);
96 	ret = __ptr_ring_full(r);
97 	spin_unlock_bh(&r->producer_lock);
98 
99 	return ret;
100 }
101 
102 /* Note: callers invoking this in a loop must use a compiler barrier,
103  * for example cpu_relax(). Callers must hold producer_lock.
104  * Callers are responsible for making sure pointer that is being queued
105  * points to a valid data.
106  */
107 static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
108 {
109 	if (unlikely(!r->size) || r->queue[r->producer])
110 		return -ENOSPC;
111 
112 	/* Make sure the pointer we are storing points to a valid data. */
113 	/* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
114 	smp_wmb();
115 
116 	r->queue[r->producer++] = ptr;
117 	if (unlikely(r->producer >= r->size))
118 		r->producer = 0;
119 	return 0;
120 }
121 
122 /*
123  * Note: resize (below) nests producer lock within consumer lock, so if you
124  * consume in interrupt or BH context, you must disable interrupts/BH when
125  * calling this.
126  */
127 static inline int ptr_ring_produce(struct ptr_ring *r, void *ptr)
128 {
129 	int ret;
130 
131 	spin_lock(&r->producer_lock);
132 	ret = __ptr_ring_produce(r, ptr);
133 	spin_unlock(&r->producer_lock);
134 
135 	return ret;
136 }
137 
138 static inline int ptr_ring_produce_irq(struct ptr_ring *r, void *ptr)
139 {
140 	int ret;
141 
142 	spin_lock_irq(&r->producer_lock);
143 	ret = __ptr_ring_produce(r, ptr);
144 	spin_unlock_irq(&r->producer_lock);
145 
146 	return ret;
147 }
148 
149 static inline int ptr_ring_produce_any(struct ptr_ring *r, void *ptr)
150 {
151 	unsigned long flags;
152 	int ret;
153 
154 	spin_lock_irqsave(&r->producer_lock, flags);
155 	ret = __ptr_ring_produce(r, ptr);
156 	spin_unlock_irqrestore(&r->producer_lock, flags);
157 
158 	return ret;
159 }
160 
161 static inline int ptr_ring_produce_bh(struct ptr_ring *r, void *ptr)
162 {
163 	int ret;
164 
165 	spin_lock_bh(&r->producer_lock);
166 	ret = __ptr_ring_produce(r, ptr);
167 	spin_unlock_bh(&r->producer_lock);
168 
169 	return ret;
170 }
171 
172 static inline void *__ptr_ring_peek(struct ptr_ring *r)
173 {
174 	if (likely(r->size))
175 		return r->queue[r->consumer_head];
176 	return NULL;
177 }
178 
179 /*
180  * Test ring empty status without taking any locks.
181  *
182  * NB: This is only safe to call if ring is never resized.
183  *
184  * However, if some other CPU consumes ring entries at the same time, the value
185  * returned is not guaranteed to be correct.
186  *
187  * In this case - to avoid incorrectly detecting the ring
188  * as empty - the CPU consuming the ring entries is responsible
189  * for either consuming all ring entries until the ring is empty,
190  * or synchronizing with some other CPU and causing it to
191  * re-test __ptr_ring_empty and/or consume the ring enteries
192  * after the synchronization point.
193  *
194  * Note: callers invoking this in a loop must use a compiler barrier,
195  * for example cpu_relax().
196  */
197 static inline bool __ptr_ring_empty(struct ptr_ring *r)
198 {
199 	if (likely(r->size))
200 		return !r->queue[READ_ONCE(r->consumer_head)];
201 	return true;
202 }
203 
204 static inline bool ptr_ring_empty(struct ptr_ring *r)
205 {
206 	bool ret;
207 
208 	spin_lock(&r->consumer_lock);
209 	ret = __ptr_ring_empty(r);
210 	spin_unlock(&r->consumer_lock);
211 
212 	return ret;
213 }
214 
215 static inline bool ptr_ring_empty_irq(struct ptr_ring *r)
216 {
217 	bool ret;
218 
219 	spin_lock_irq(&r->consumer_lock);
220 	ret = __ptr_ring_empty(r);
221 	spin_unlock_irq(&r->consumer_lock);
222 
223 	return ret;
224 }
225 
226 static inline bool ptr_ring_empty_any(struct ptr_ring *r)
227 {
228 	unsigned long flags;
229 	bool ret;
230 
231 	spin_lock_irqsave(&r->consumer_lock, flags);
232 	ret = __ptr_ring_empty(r);
233 	spin_unlock_irqrestore(&r->consumer_lock, flags);
234 
235 	return ret;
236 }
237 
238 static inline bool ptr_ring_empty_bh(struct ptr_ring *r)
239 {
240 	bool ret;
241 
242 	spin_lock_bh(&r->consumer_lock);
243 	ret = __ptr_ring_empty(r);
244 	spin_unlock_bh(&r->consumer_lock);
245 
246 	return ret;
247 }
248 
249 /* Must only be called after __ptr_ring_peek returned !NULL */
250 static inline void __ptr_ring_discard_one(struct ptr_ring *r)
251 {
252 	/* Fundamentally, what we want to do is update consumer
253 	 * index and zero out the entry so producer can reuse it.
254 	 * Doing it naively at each consume would be as simple as:
255 	 *       consumer = r->consumer;
256 	 *       r->queue[consumer++] = NULL;
257 	 *       if (unlikely(consumer >= r->size))
258 	 *               consumer = 0;
259 	 *       r->consumer = consumer;
260 	 * but that is suboptimal when the ring is full as producer is writing
261 	 * out new entries in the same cache line.  Defer these updates until a
262 	 * batch of entries has been consumed.
263 	 */
264 	/* Note: we must keep consumer_head valid at all times for __ptr_ring_empty
265 	 * to work correctly.
266 	 */
267 	int consumer_head = r->consumer_head;
268 	int head = consumer_head++;
269 
270 	/* Once we have processed enough entries invalidate them in
271 	 * the ring all at once so producer can reuse their space in the ring.
272 	 * We also do this when we reach end of the ring - not mandatory
273 	 * but helps keep the implementation simple.
274 	 */
275 	if (unlikely(consumer_head - r->consumer_tail >= r->batch ||
276 		     consumer_head >= r->size)) {
277 		/* Zero out entries in the reverse order: this way we touch the
278 		 * cache line that producer might currently be reading the last;
279 		 * producer won't make progress and touch other cache lines
280 		 * besides the first one until we write out all entries.
281 		 */
282 		while (likely(head >= r->consumer_tail))
283 			r->queue[head--] = NULL;
284 		r->consumer_tail = consumer_head;
285 	}
286 	if (unlikely(consumer_head >= r->size)) {
287 		consumer_head = 0;
288 		r->consumer_tail = 0;
289 	}
290 	/* matching READ_ONCE in __ptr_ring_empty for lockless tests */
291 	WRITE_ONCE(r->consumer_head, consumer_head);
292 }
293 
294 static inline void *__ptr_ring_consume(struct ptr_ring *r)
295 {
296 	void *ptr;
297 
298 	ptr = __ptr_ring_peek(r);
299 	if (ptr)
300 		__ptr_ring_discard_one(r);
301 
302 	/* Make sure anyone accessing data through the pointer is up to date. */
303 	/* Pairs with smp_wmb in __ptr_ring_produce. */
304 	smp_read_barrier_depends();
305 	return ptr;
306 }
307 
308 static inline int __ptr_ring_consume_batched(struct ptr_ring *r,
309 					     void **array, int n)
310 {
311 	void *ptr;
312 	int i;
313 
314 	for (i = 0; i < n; i++) {
315 		ptr = __ptr_ring_consume(r);
316 		if (!ptr)
317 			break;
318 		array[i] = ptr;
319 	}
320 
321 	return i;
322 }
323 
324 /*
325  * Note: resize (below) nests producer lock within consumer lock, so if you
326  * call this in interrupt or BH context, you must disable interrupts/BH when
327  * producing.
328  */
329 static inline void *ptr_ring_consume(struct ptr_ring *r)
330 {
331 	void *ptr;
332 
333 	spin_lock(&r->consumer_lock);
334 	ptr = __ptr_ring_consume(r);
335 	spin_unlock(&r->consumer_lock);
336 
337 	return ptr;
338 }
339 
340 static inline void *ptr_ring_consume_irq(struct ptr_ring *r)
341 {
342 	void *ptr;
343 
344 	spin_lock_irq(&r->consumer_lock);
345 	ptr = __ptr_ring_consume(r);
346 	spin_unlock_irq(&r->consumer_lock);
347 
348 	return ptr;
349 }
350 
351 static inline void *ptr_ring_consume_any(struct ptr_ring *r)
352 {
353 	unsigned long flags;
354 	void *ptr;
355 
356 	spin_lock_irqsave(&r->consumer_lock, flags);
357 	ptr = __ptr_ring_consume(r);
358 	spin_unlock_irqrestore(&r->consumer_lock, flags);
359 
360 	return ptr;
361 }
362 
363 static inline void *ptr_ring_consume_bh(struct ptr_ring *r)
364 {
365 	void *ptr;
366 
367 	spin_lock_bh(&r->consumer_lock);
368 	ptr = __ptr_ring_consume(r);
369 	spin_unlock_bh(&r->consumer_lock);
370 
371 	return ptr;
372 }
373 
374 static inline int ptr_ring_consume_batched(struct ptr_ring *r,
375 					   void **array, int n)
376 {
377 	int ret;
378 
379 	spin_lock(&r->consumer_lock);
380 	ret = __ptr_ring_consume_batched(r, array, n);
381 	spin_unlock(&r->consumer_lock);
382 
383 	return ret;
384 }
385 
386 static inline int ptr_ring_consume_batched_irq(struct ptr_ring *r,
387 					       void **array, int n)
388 {
389 	int ret;
390 
391 	spin_lock_irq(&r->consumer_lock);
392 	ret = __ptr_ring_consume_batched(r, array, n);
393 	spin_unlock_irq(&r->consumer_lock);
394 
395 	return ret;
396 }
397 
398 static inline int ptr_ring_consume_batched_any(struct ptr_ring *r,
399 					       void **array, int n)
400 {
401 	unsigned long flags;
402 	int ret;
403 
404 	spin_lock_irqsave(&r->consumer_lock, flags);
405 	ret = __ptr_ring_consume_batched(r, array, n);
406 	spin_unlock_irqrestore(&r->consumer_lock, flags);
407 
408 	return ret;
409 }
410 
411 static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,
412 					      void **array, int n)
413 {
414 	int ret;
415 
416 	spin_lock_bh(&r->consumer_lock);
417 	ret = __ptr_ring_consume_batched(r, array, n);
418 	spin_unlock_bh(&r->consumer_lock);
419 
420 	return ret;
421 }
422 
423 /* Cast to structure type and call a function without discarding from FIFO.
424  * Function must return a value.
425  * Callers must take consumer_lock.
426  */
427 #define __PTR_RING_PEEK_CALL(r, f) ((f)(__ptr_ring_peek(r)))
428 
429 #define PTR_RING_PEEK_CALL(r, f) ({ \
430 	typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
431 	\
432 	spin_lock(&(r)->consumer_lock); \
433 	__PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
434 	spin_unlock(&(r)->consumer_lock); \
435 	__PTR_RING_PEEK_CALL_v; \
436 })
437 
438 #define PTR_RING_PEEK_CALL_IRQ(r, f) ({ \
439 	typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
440 	\
441 	spin_lock_irq(&(r)->consumer_lock); \
442 	__PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
443 	spin_unlock_irq(&(r)->consumer_lock); \
444 	__PTR_RING_PEEK_CALL_v; \
445 })
446 
447 #define PTR_RING_PEEK_CALL_BH(r, f) ({ \
448 	typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
449 	\
450 	spin_lock_bh(&(r)->consumer_lock); \
451 	__PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
452 	spin_unlock_bh(&(r)->consumer_lock); \
453 	__PTR_RING_PEEK_CALL_v; \
454 })
455 
456 #define PTR_RING_PEEK_CALL_ANY(r, f) ({ \
457 	typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
458 	unsigned long __PTR_RING_PEEK_CALL_f;\
459 	\
460 	spin_lock_irqsave(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
461 	__PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
462 	spin_unlock_irqrestore(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
463 	__PTR_RING_PEEK_CALL_v; \
464 })
465 
466 static inline void **__ptr_ring_init_queue_alloc(unsigned int size, gfp_t gfp)
467 {
468 	/* Allocate an extra dummy element at end of ring to avoid consumer head
469 	 * or produce head access past the end of the array. Possible when
470 	 * producer/consumer operations and __ptr_ring_peek operations run in
471 	 * parallel.
472 	 */
473 	return kcalloc(size + 1, sizeof(void *), gfp);
474 }
475 
476 static inline void __ptr_ring_set_size(struct ptr_ring *r, int size)
477 {
478 	r->size = size;
479 	r->batch = SMP_CACHE_BYTES * 2 / sizeof(*(r->queue));
480 	/* We need to set batch at least to 1 to make logic
481 	 * in __ptr_ring_discard_one work correctly.
482 	 * Batching too much (because ring is small) would cause a lot of
483 	 * burstiness. Needs tuning, for now disable batching.
484 	 */
485 	if (r->batch > r->size / 2 || !r->batch)
486 		r->batch = 1;
487 }
488 
489 static inline int ptr_ring_init(struct ptr_ring *r, int size, gfp_t gfp)
490 {
491 	r->queue = __ptr_ring_init_queue_alloc(size, gfp);
492 	if (!r->queue)
493 		return -ENOMEM;
494 
495 	__ptr_ring_set_size(r, size);
496 	r->producer = r->consumer_head = r->consumer_tail = 0;
497 	spin_lock_init(&r->producer_lock);
498 	spin_lock_init(&r->consumer_lock);
499 
500 	return 0;
501 }
502 
503 /*
504  * Return entries into ring. Destroy entries that don't fit.
505  *
506  * Note: this is expected to be a rare slow path operation.
507  *
508  * Note: producer lock is nested within consumer lock, so if you
509  * resize you must make sure all uses nest correctly.
510  * In particular if you consume ring in interrupt or BH context, you must
511  * disable interrupts/BH when doing so.
512  */
513 static inline void ptr_ring_unconsume(struct ptr_ring *r, void **batch, int n,
514 				      void (*destroy)(void *))
515 {
516 	unsigned long flags;
517 	int head;
518 
519 	spin_lock_irqsave(&r->consumer_lock, flags);
520 	spin_lock(&r->producer_lock);
521 
522 	if (!r->size)
523 		goto done;
524 
525 	/*
526 	 * Clean out buffered entries (for simplicity). This way following code
527 	 * can test entries for NULL and if not assume they are valid.
528 	 */
529 	head = r->consumer_head - 1;
530 	while (likely(head >= r->consumer_tail))
531 		r->queue[head--] = NULL;
532 	r->consumer_tail = r->consumer_head;
533 
534 	/*
535 	 * Go over entries in batch, start moving head back and copy entries.
536 	 * Stop when we run into previously unconsumed entries.
537 	 */
538 	while (n) {
539 		head = r->consumer_head - 1;
540 		if (head < 0)
541 			head = r->size - 1;
542 		if (r->queue[head]) {
543 			/* This batch entry will have to be destroyed. */
544 			goto done;
545 		}
546 		r->queue[head] = batch[--n];
547 		r->consumer_tail = head;
548 		/* matching READ_ONCE in __ptr_ring_empty for lockless tests */
549 		WRITE_ONCE(r->consumer_head, head);
550 	}
551 
552 done:
553 	/* Destroy all entries left in the batch. */
554 	while (n)
555 		destroy(batch[--n]);
556 	spin_unlock(&r->producer_lock);
557 	spin_unlock_irqrestore(&r->consumer_lock, flags);
558 }
559 
560 static inline void **__ptr_ring_swap_queue(struct ptr_ring *r, void **queue,
561 					   int size, gfp_t gfp,
562 					   void (*destroy)(void *))
563 {
564 	int producer = 0;
565 	void **old;
566 	void *ptr;
567 
568 	while ((ptr = __ptr_ring_consume(r)))
569 		if (producer < size)
570 			queue[producer++] = ptr;
571 		else if (destroy)
572 			destroy(ptr);
573 
574 	__ptr_ring_set_size(r, size);
575 	r->producer = producer;
576 	r->consumer_head = 0;
577 	r->consumer_tail = 0;
578 	old = r->queue;
579 	r->queue = queue;
580 
581 	return old;
582 }
583 
584 /*
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.
589  */
590 static inline int ptr_ring_resize(struct ptr_ring *r, int size, gfp_t gfp,
591 				  void (*destroy)(void *))
592 {
593 	unsigned long flags;
594 	void **queue = __ptr_ring_init_queue_alloc(size, gfp);
595 	void **old;
596 
597 	if (!queue)
598 		return -ENOMEM;
599 
600 	spin_lock_irqsave(&(r)->consumer_lock, flags);
601 	spin_lock(&(r)->producer_lock);
602 
603 	old = __ptr_ring_swap_queue(r, queue, size, gfp, destroy);
604 
605 	spin_unlock(&(r)->producer_lock);
606 	spin_unlock_irqrestore(&(r)->consumer_lock, flags);
607 
608 	kfree(old);
609 
610 	return 0;
611 }
612 
613 /*
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.
618  */
619 static inline int ptr_ring_resize_multiple(struct ptr_ring **rings,
620 					   unsigned int nrings,
621 					   int size,
622 					   gfp_t gfp, void (*destroy)(void *))
623 {
624 	unsigned long flags;
625 	void ***queues;
626 	int i;
627 
628 	queues = kmalloc_array(nrings, sizeof(*queues), gfp);
629 	if (!queues)
630 		goto noqueues;
631 
632 	for (i = 0; i < nrings; ++i) {
633 		queues[i] = __ptr_ring_init_queue_alloc(size, gfp);
634 		if (!queues[i])
635 			goto nomem;
636 	}
637 
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],
642 						  size, gfp, destroy);
643 		spin_unlock(&(rings[i])->producer_lock);
644 		spin_unlock_irqrestore(&(rings[i])->consumer_lock, flags);
645 	}
646 
647 	for (i = 0; i < nrings; ++i)
648 		kfree(queues[i]);
649 
650 	kfree(queues);
651 
652 	return 0;
653 
654 nomem:
655 	while (--i >= 0)
656 		kfree(queues[i]);
657 
658 	kfree(queues);
659 
660 noqueues:
661 	return -ENOMEM;
662 }
663 
664 static inline void ptr_ring_cleanup(struct ptr_ring *r, void (*destroy)(void *))
665 {
666 	void *ptr;
667 
668 	if (destroy)
669 		while ((ptr = ptr_ring_consume(r)))
670 			destroy(ptr);
671 	kfree(r->queue);
672 }
673 
674 #endif /* _LINUX_PTR_RING_H  */
675