2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2007-2009 Kip Macy <kmacy@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #ifndef _SYS_BUF_RING_H_
31 #define _SYS_BUF_RING_H_
33 #include <machine/cpu.h>
37 #include <sys/mutex.h>
41 volatile uint32_t br_prod_head;
42 volatile uint32_t br_prod_tail;
46 volatile uint32_t br_cons_head __aligned(CACHE_LINE_SIZE);
47 volatile uint32_t br_cons_tail;
53 void *br_ring[0] __aligned(CACHE_LINE_SIZE);
57 * multi-producer safe lock-free ring buffer enqueue
61 buf_ring_enqueue(struct buf_ring *br, void *buf)
63 uint32_t prod_head, prod_next, cons_tail;
68 * Note: It is possible to encounter an mbuf that was removed
69 * via drbr_peek(), and then re-added via drbr_putback() and
70 * trigger a spurious panic.
72 for (i = br->br_cons_head; i != br->br_prod_head;
73 i = ((i + 1) & br->br_cons_mask))
74 if (br->br_ring[i] == buf)
75 panic("buf=%p already enqueue at %d prod=%d cons=%d",
76 buf, i, br->br_prod_tail, br->br_cons_tail);
80 prod_head = br->br_prod_head;
81 prod_next = (prod_head + 1) & br->br_prod_mask;
82 cons_tail = br->br_cons_tail;
84 if (prod_next == cons_tail) {
86 if (prod_head == br->br_prod_head &&
87 cons_tail == br->br_cons_tail) {
94 } while (!atomic_cmpset_acq_int(&br->br_prod_head, prod_head, prod_next));
96 if (br->br_ring[prod_head] != NULL)
97 panic("dangling value in enqueue");
99 br->br_ring[prod_head] = buf;
102 * If there are other enqueues in progress
103 * that preceded us, we need to wait for them
106 while (br->br_prod_tail != prod_head)
108 atomic_store_rel_int(&br->br_prod_tail, prod_next);
114 * multi-consumer safe dequeue
117 static __inline void *
118 buf_ring_dequeue_mc(struct buf_ring *br)
120 uint32_t cons_head, cons_next;
125 cons_head = br->br_cons_head;
126 cons_next = (cons_head + 1) & br->br_cons_mask;
128 if (cons_head == br->br_prod_tail) {
132 } while (!atomic_cmpset_acq_int(&br->br_cons_head, cons_head, cons_next));
134 buf = br->br_ring[cons_head];
136 br->br_ring[cons_head] = NULL;
139 * If there are other dequeues in progress
140 * that preceded us, we need to wait for them
143 while (br->br_cons_tail != cons_head)
146 atomic_store_rel_int(&br->br_cons_tail, cons_next);
153 * single-consumer dequeue
154 * use where dequeue is protected by a lock
155 * e.g. a network driver's tx queue lock
157 static __inline void *
158 buf_ring_dequeue_sc(struct buf_ring *br)
160 uint32_t cons_head, cons_next;
161 #ifdef PREFETCH_DEFINED
162 uint32_t cons_next_next;
168 * This is a workaround to allow using buf_ring on ARM and ARM64.
169 * ARM64TODO: Fix buf_ring in a generic way.
170 * REMARKS: It is suspected that br_cons_head does not require
171 * load_acq operation, but this change was extensively tested
172 * and confirmed it's working. To be reviewed once again in
175 * Preventing following situation:
177 * Core(0) - buf_ring_enqueue() Core(1) - buf_ring_dequeue_sc()
178 * ----------------------------------------- ----------------------------------------------
180 * cons_head = br->br_cons_head;
181 * atomic_cmpset_acq_32(&br->br_prod_head, ...));
182 * buf = br->br_ring[cons_head]; <see <1>>
183 * br->br_ring[prod_head] = buf;
184 * atomic_store_rel_32(&br->br_prod_tail, ...);
185 * prod_tail = br->br_prod_tail;
186 * if (cons_head == prod_tail)
188 * <condition is false and code uses invalid(old) buf>`
190 * <1> Load (on core 1) from br->br_ring[cons_head] can be reordered (speculative readed) by CPU.
192 #if defined(__arm__) || defined(__aarch64__)
193 cons_head = atomic_load_acq_32(&br->br_cons_head);
195 cons_head = br->br_cons_head;
197 prod_tail = atomic_load_acq_32(&br->br_prod_tail);
199 cons_next = (cons_head + 1) & br->br_cons_mask;
200 #ifdef PREFETCH_DEFINED
201 cons_next_next = (cons_head + 2) & br->br_cons_mask;
204 if (cons_head == prod_tail)
207 #ifdef PREFETCH_DEFINED
208 if (cons_next != prod_tail) {
209 prefetch(br->br_ring[cons_next]);
210 if (cons_next_next != prod_tail)
211 prefetch(br->br_ring[cons_next_next]);
214 br->br_cons_head = cons_next;
215 buf = br->br_ring[cons_head];
218 br->br_ring[cons_head] = NULL;
219 if (!mtx_owned(br->br_lock))
220 panic("lock not held on single consumer dequeue");
221 if (br->br_cons_tail != cons_head)
222 panic("inconsistent list cons_tail=%d cons_head=%d",
223 br->br_cons_tail, cons_head);
225 br->br_cons_tail = cons_next;
230 * single-consumer advance after a peek
231 * use where it is protected by a lock
232 * e.g. a network driver's tx queue lock
235 buf_ring_advance_sc(struct buf_ring *br)
237 uint32_t cons_head, cons_next;
240 cons_head = br->br_cons_head;
241 prod_tail = br->br_prod_tail;
243 cons_next = (cons_head + 1) & br->br_cons_mask;
244 if (cons_head == prod_tail)
246 br->br_cons_head = cons_next;
248 br->br_ring[cons_head] = NULL;
250 br->br_cons_tail = cons_next;
254 * Used to return a buffer (most likely already there)
255 * to the top of the ring. The caller should *not*
256 * have used any dequeue to pull it out of the ring
257 * but instead should have used the peek() function.
258 * This is normally used where the transmit queue
259 * of a driver is full, and an mbuf must be returned.
260 * Most likely whats in the ring-buffer is what
261 * is being put back (since it was not removed), but
262 * sometimes the lower transmit function may have
263 * done a pullup or other function that will have
264 * changed it. As an optimization we always put it
265 * back (since jhb says the store is probably cheaper),
266 * if we have to do a multi-queue version we will need
267 * the compare and an atomic.
270 buf_ring_putback_sc(struct buf_ring *br, void *new)
272 KASSERT(br->br_cons_head != br->br_prod_tail,
273 ("Buf-Ring has none in putback")) ;
274 br->br_ring[br->br_cons_head] = new;
278 * return a pointer to the first entry in the ring
279 * without modifying it, or NULL if the ring is empty
280 * race-prone if not protected by a lock
282 static __inline void *
283 buf_ring_peek(struct buf_ring *br)
287 if ((br->br_lock != NULL) && !mtx_owned(br->br_lock))
288 panic("lock not held on single consumer dequeue");
291 * I believe it is safe to not have a memory barrier
292 * here because we control cons and tail is worst case
293 * a lagging indicator so we worst case we might
294 * return NULL immediately after a buffer has been enqueued
296 if (br->br_cons_head == br->br_prod_tail)
299 return (br->br_ring[br->br_cons_head]);
302 static __inline void *
303 buf_ring_peek_clear_sc(struct buf_ring *br)
308 if (!mtx_owned(br->br_lock))
309 panic("lock not held on single consumer dequeue");
312 if (br->br_cons_head == br->br_prod_tail)
315 #if defined(__arm__) || defined(__aarch64__)
317 * The barrier is required there on ARM and ARM64 to ensure, that
318 * br->br_ring[br->br_cons_head] will not be fetched before the above
319 * condition is checked.
320 * Without the barrier, it is possible, that buffer will be fetched
321 * before the enqueue will put mbuf into br, then, in the meantime, the
322 * enqueue will update the array and the br_prod_tail, and the
323 * conditional check will be true, so we will return previously fetched
324 * (and invalid) buffer.
326 atomic_thread_fence_acq();
331 * Single consumer, i.e. cons_head will not move while we are
332 * running, so atomic_swap_ptr() is not necessary here.
334 ret = br->br_ring[br->br_cons_head];
335 br->br_ring[br->br_cons_head] = NULL;
338 return (br->br_ring[br->br_cons_head]);
343 buf_ring_full(struct buf_ring *br)
346 return (((br->br_prod_head + 1) & br->br_prod_mask) == br->br_cons_tail);
350 buf_ring_empty(struct buf_ring *br)
353 return (br->br_cons_head == br->br_prod_tail);
357 buf_ring_count(struct buf_ring *br)
360 return ((br->br_prod_size + br->br_prod_tail - br->br_cons_tail)
364 struct buf_ring *buf_ring_alloc(int count, struct malloc_type *type, int flags,
366 void buf_ring_free(struct buf_ring *br, struct malloc_type *type);