blob: 796b90f6d4e90028783a7b085456f48f1150ae21 [file] [log] [blame]
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +03001/*
2 * Definitions for the 'struct ptr_ring' datastructure.
3 *
4 * Author:
5 * Michael S. Tsirkin <mst@redhat.com>
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
34struct ptr_ring {
35 int producer ____cacheline_aligned_in_smp;
36 spinlock_t producer_lock;
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +030037 int consumer_head ____cacheline_aligned_in_smp; /* next valid entry */
38 int consumer_tail; /* next entry to invalidate */
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +030039 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 */
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +030043 int batch; /* number of entries to consume in a batch */
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +030044 void **queue;
45};
46
47/* Note: callers invoking this in a loop must use a compiler barrier,
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +030048 * 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.
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +030051 */
52static inline bool __ptr_ring_full(struct ptr_ring *r)
53{
54 return r->queue[r->producer];
55}
56
57static inline bool ptr_ring_full(struct ptr_ring *r)
58{
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +030059 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
68static 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
79static 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
91static 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;
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300100}
101
102/* Note: callers invoking this in a loop must use a compiler barrier,
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300103 * for example cpu_relax(). Callers must hold producer_lock.
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300104 */
105static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
106{
Jason Wang982fb492016-06-30 14:45:31 +0800107 if (unlikely(!r->size) || r->queue[r->producer])
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300108 return -ENOSPC;
109
110 r->queue[r->producer++] = ptr;
111 if (unlikely(r->producer >= r->size))
112 r->producer = 0;
113 return 0;
114}
115
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200116/*
117 * Note: resize (below) nests producer lock within consumer lock, so if you
118 * consume in interrupt or BH context, you must disable interrupts/BH when
119 * calling this.
120 */
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300121static inline int ptr_ring_produce(struct ptr_ring *r, void *ptr)
122{
123 int ret;
124
125 spin_lock(&r->producer_lock);
126 ret = __ptr_ring_produce(r, ptr);
127 spin_unlock(&r->producer_lock);
128
129 return ret;
130}
131
132static inline int ptr_ring_produce_irq(struct ptr_ring *r, void *ptr)
133{
134 int ret;
135
136 spin_lock_irq(&r->producer_lock);
137 ret = __ptr_ring_produce(r, ptr);
138 spin_unlock_irq(&r->producer_lock);
139
140 return ret;
141}
142
143static inline int ptr_ring_produce_any(struct ptr_ring *r, void *ptr)
144{
145 unsigned long flags;
146 int ret;
147
148 spin_lock_irqsave(&r->producer_lock, flags);
149 ret = __ptr_ring_produce(r, ptr);
150 spin_unlock_irqrestore(&r->producer_lock, flags);
151
152 return ret;
153}
154
155static inline int ptr_ring_produce_bh(struct ptr_ring *r, void *ptr)
156{
157 int ret;
158
159 spin_lock_bh(&r->producer_lock);
160 ret = __ptr_ring_produce(r, ptr);
161 spin_unlock_bh(&r->producer_lock);
162
163 return ret;
164}
165
166/* Note: callers invoking this in a loop must use a compiler barrier,
167 * for example cpu_relax(). Callers must take consumer_lock
168 * if they dereference the pointer - see e.g. PTR_RING_PEEK_CALL.
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300169 * If ring is never resized, and if the pointer is merely
170 * tested, there's no need to take the lock - see e.g. __ptr_ring_empty.
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300171 */
172static inline void *__ptr_ring_peek(struct ptr_ring *r)
173{
Jason Wang982fb492016-06-30 14:45:31 +0800174 if (likely(r->size))
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300175 return r->queue[r->consumer_head];
Jason Wang982fb492016-06-30 14:45:31 +0800176 return NULL;
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300177}
178
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300179/* Note: callers invoking this in a loop must use a compiler barrier,
180 * for example cpu_relax(). Callers must take consumer_lock
181 * if the ring is ever resized - see e.g. ptr_ring_empty.
182 */
183static inline bool __ptr_ring_empty(struct ptr_ring *r)
184{
185 return !__ptr_ring_peek(r);
186}
187
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300188static inline bool ptr_ring_empty(struct ptr_ring *r)
189{
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300190 bool ret;
191
192 spin_lock(&r->consumer_lock);
193 ret = __ptr_ring_empty(r);
194 spin_unlock(&r->consumer_lock);
195
196 return ret;
197}
198
199static inline bool ptr_ring_empty_irq(struct ptr_ring *r)
200{
201 bool ret;
202
203 spin_lock_irq(&r->consumer_lock);
204 ret = __ptr_ring_empty(r);
205 spin_unlock_irq(&r->consumer_lock);
206
207 return ret;
208}
209
210static inline bool ptr_ring_empty_any(struct ptr_ring *r)
211{
212 unsigned long flags;
213 bool ret;
214
215 spin_lock_irqsave(&r->consumer_lock, flags);
216 ret = __ptr_ring_empty(r);
217 spin_unlock_irqrestore(&r->consumer_lock, flags);
218
219 return ret;
220}
221
222static inline bool ptr_ring_empty_bh(struct ptr_ring *r)
223{
224 bool ret;
225
226 spin_lock_bh(&r->consumer_lock);
227 ret = __ptr_ring_empty(r);
228 spin_unlock_bh(&r->consumer_lock);
229
230 return ret;
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300231}
232
233/* Must only be called after __ptr_ring_peek returned !NULL */
234static inline void __ptr_ring_discard_one(struct ptr_ring *r)
235{
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300236 /* Fundamentally, what we want to do is update consumer
237 * index and zero out the entry so producer can reuse it.
238 * Doing it naively at each consume would be as simple as:
239 * r->queue[r->consumer++] = NULL;
240 * if (unlikely(r->consumer >= r->size))
241 * r->consumer = 0;
242 * but that is suboptimal when the ring is full as producer is writing
243 * out new entries in the same cache line. Defer these updates until a
244 * batch of entries has been consumed.
245 */
246 int head = r->consumer_head++;
247
248 /* Once we have processed enough entries invalidate them in
249 * the ring all at once so producer can reuse their space in the ring.
250 * We also do this when we reach end of the ring - not mandatory
251 * but helps keep the implementation simple.
252 */
253 if (unlikely(r->consumer_head - r->consumer_tail >= r->batch ||
254 r->consumer_head >= r->size)) {
255 /* Zero out entries in the reverse order: this way we touch the
256 * cache line that producer might currently be reading the last;
257 * producer won't make progress and touch other cache lines
258 * besides the first one until we write out all entries.
259 */
260 while (likely(head >= r->consumer_tail))
261 r->queue[head--] = NULL;
262 r->consumer_tail = r->consumer_head;
263 }
264 if (unlikely(r->consumer_head >= r->size)) {
265 r->consumer_head = 0;
266 r->consumer_tail = 0;
267 }
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300268}
269
270static inline void *__ptr_ring_consume(struct ptr_ring *r)
271{
272 void *ptr;
273
274 ptr = __ptr_ring_peek(r);
275 if (ptr)
276 __ptr_ring_discard_one(r);
277
278 return ptr;
279}
280
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200281/*
282 * Note: resize (below) nests producer lock within consumer lock, so if you
283 * call this in interrupt or BH context, you must disable interrupts/BH when
284 * producing.
285 */
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300286static inline void *ptr_ring_consume(struct ptr_ring *r)
287{
288 void *ptr;
289
290 spin_lock(&r->consumer_lock);
291 ptr = __ptr_ring_consume(r);
292 spin_unlock(&r->consumer_lock);
293
294 return ptr;
295}
296
297static inline void *ptr_ring_consume_irq(struct ptr_ring *r)
298{
299 void *ptr;
300
301 spin_lock_irq(&r->consumer_lock);
302 ptr = __ptr_ring_consume(r);
303 spin_unlock_irq(&r->consumer_lock);
304
305 return ptr;
306}
307
308static inline void *ptr_ring_consume_any(struct ptr_ring *r)
309{
310 unsigned long flags;
311 void *ptr;
312
313 spin_lock_irqsave(&r->consumer_lock, flags);
314 ptr = __ptr_ring_consume(r);
315 spin_unlock_irqrestore(&r->consumer_lock, flags);
316
317 return ptr;
318}
319
320static inline void *ptr_ring_consume_bh(struct ptr_ring *r)
321{
322 void *ptr;
323
324 spin_lock_bh(&r->consumer_lock);
325 ptr = __ptr_ring_consume(r);
326 spin_unlock_bh(&r->consumer_lock);
327
328 return ptr;
329}
330
331/* Cast to structure type and call a function without discarding from FIFO.
332 * Function must return a value.
333 * Callers must take consumer_lock.
334 */
335#define __PTR_RING_PEEK_CALL(r, f) ((f)(__ptr_ring_peek(r)))
336
337#define PTR_RING_PEEK_CALL(r, f) ({ \
338 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
339 \
340 spin_lock(&(r)->consumer_lock); \
341 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
342 spin_unlock(&(r)->consumer_lock); \
343 __PTR_RING_PEEK_CALL_v; \
344})
345
346#define PTR_RING_PEEK_CALL_IRQ(r, f) ({ \
347 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
348 \
349 spin_lock_irq(&(r)->consumer_lock); \
350 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
351 spin_unlock_irq(&(r)->consumer_lock); \
352 __PTR_RING_PEEK_CALL_v; \
353})
354
355#define PTR_RING_PEEK_CALL_BH(r, f) ({ \
356 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
357 \
358 spin_lock_bh(&(r)->consumer_lock); \
359 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
360 spin_unlock_bh(&(r)->consumer_lock); \
361 __PTR_RING_PEEK_CALL_v; \
362})
363
364#define PTR_RING_PEEK_CALL_ANY(r, f) ({ \
365 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
366 unsigned long __PTR_RING_PEEK_CALL_f;\
367 \
368 spin_lock_irqsave(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
369 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
370 spin_unlock_irqrestore(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
371 __PTR_RING_PEEK_CALL_v; \
372})
373
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300374static inline void **__ptr_ring_init_queue_alloc(int size, gfp_t gfp)
375{
376 return kzalloc(ALIGN(size * sizeof(void *), SMP_CACHE_BYTES), gfp);
377}
378
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300379static inline void __ptr_ring_set_size(struct ptr_ring *r, int size)
380{
381 r->size = size;
382 r->batch = SMP_CACHE_BYTES * 2 / sizeof(*(r->queue));
383 /* We need to set batch at least to 1 to make logic
384 * in __ptr_ring_discard_one work correctly.
385 * Batching too much (because ring is small) would cause a lot of
386 * burstiness. Needs tuning, for now disable batching.
387 */
388 if (r->batch > r->size / 2 || !r->batch)
389 r->batch = 1;
390}
391
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300392static inline int ptr_ring_init(struct ptr_ring *r, int size, gfp_t gfp)
393{
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300394 r->queue = __ptr_ring_init_queue_alloc(size, gfp);
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300395 if (!r->queue)
396 return -ENOMEM;
397
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300398 __ptr_ring_set_size(r, size);
399 r->producer = r->consumer_head = r->consumer_tail = 0;
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300400 spin_lock_init(&r->producer_lock);
401 spin_lock_init(&r->consumer_lock);
402
403 return 0;
404}
405
Michael S. Tsirkin197a5212017-05-17 12:14:37 +0800406/*
407 * Return entries into ring. Destroy entries that don't fit.
408 *
409 * Note: this is expected to be a rare slow path operation.
410 *
411 * Note: producer lock is nested within consumer lock, so if you
412 * resize you must make sure all uses nest correctly.
413 * In particular if you consume ring in interrupt or BH context, you must
414 * disable interrupts/BH when doing so.
415 */
416static inline void ptr_ring_unconsume(struct ptr_ring *r, void **batch, int n,
417 void (*destroy)(void *))
418{
419 unsigned long flags;
420 int head;
421
422 spin_lock_irqsave(&r->consumer_lock, flags);
423 spin_lock(&r->producer_lock);
424
425 if (!r->size)
426 goto done;
427
428 /*
429 * Clean out buffered entries (for simplicity). This way following code
430 * can test entries for NULL and if not assume they are valid.
431 */
432 head = r->consumer_head - 1;
433 while (likely(head >= r->consumer_tail))
434 r->queue[head--] = NULL;
435 r->consumer_tail = r->consumer_head;
436
437 /*
438 * Go over entries in batch, start moving head back and copy entries.
439 * Stop when we run into previously unconsumed entries.
440 */
441 while (n) {
442 head = r->consumer_head - 1;
443 if (head < 0)
444 head = r->size - 1;
445 if (r->queue[head]) {
446 /* This batch entry will have to be destroyed. */
447 goto done;
448 }
449 r->queue[head] = batch[--n];
450 r->consumer_tail = r->consumer_head = head;
451 }
452
453done:
454 /* Destroy all entries left in the batch. */
455 while (n)
456 destroy(batch[--n]);
457 spin_unlock(&r->producer_lock);
458 spin_unlock_irqrestore(&r->consumer_lock, flags);
459}
460
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800461static inline void **__ptr_ring_swap_queue(struct ptr_ring *r, void **queue,
462 int size, gfp_t gfp,
463 void (*destroy)(void *))
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300464{
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300465 int producer = 0;
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300466 void **old;
467 void *ptr;
468
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200469 while ((ptr = __ptr_ring_consume(r)))
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300470 if (producer < size)
471 queue[producer++] = ptr;
472 else if (destroy)
473 destroy(ptr);
474
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300475 __ptr_ring_set_size(r, size);
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300476 r->producer = producer;
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300477 r->consumer_head = 0;
478 r->consumer_tail = 0;
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300479 old = r->queue;
480 r->queue = queue;
481
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800482 return old;
483}
484
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200485/*
486 * Note: producer lock is nested within consumer lock, so if you
487 * resize you must make sure all uses nest correctly.
488 * In particular if you consume ring in interrupt or BH context, you must
489 * disable interrupts/BH when doing so.
490 */
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800491static inline int ptr_ring_resize(struct ptr_ring *r, int size, gfp_t gfp,
492 void (*destroy)(void *))
493{
494 unsigned long flags;
495 void **queue = __ptr_ring_init_queue_alloc(size, gfp);
496 void **old;
497
498 if (!queue)
499 return -ENOMEM;
500
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200501 spin_lock_irqsave(&(r)->consumer_lock, flags);
502 spin_lock(&(r)->producer_lock);
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800503
504 old = __ptr_ring_swap_queue(r, queue, size, gfp, destroy);
505
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200506 spin_unlock(&(r)->producer_lock);
507 spin_unlock_irqrestore(&(r)->consumer_lock, flags);
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300508
509 kfree(old);
510
511 return 0;
512}
513
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200514/*
515 * Note: producer lock is nested within consumer lock, so if you
516 * resize you must make sure all uses nest correctly.
517 * In particular if you consume ring in interrupt or BH context, you must
518 * disable interrupts/BH when doing so.
519 */
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800520static inline int ptr_ring_resize_multiple(struct ptr_ring **rings, int nrings,
521 int size,
522 gfp_t gfp, void (*destroy)(void *))
523{
524 unsigned long flags;
525 void ***queues;
526 int i;
527
528 queues = kmalloc(nrings * sizeof *queues, gfp);
529 if (!queues)
530 goto noqueues;
531
532 for (i = 0; i < nrings; ++i) {
533 queues[i] = __ptr_ring_init_queue_alloc(size, gfp);
534 if (!queues[i])
535 goto nomem;
536 }
537
538 for (i = 0; i < nrings; ++i) {
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200539 spin_lock_irqsave(&(rings[i])->consumer_lock, flags);
540 spin_lock(&(rings[i])->producer_lock);
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800541 queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
542 size, gfp, destroy);
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200543 spin_unlock(&(rings[i])->producer_lock);
544 spin_unlock_irqrestore(&(rings[i])->consumer_lock, flags);
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800545 }
546
547 for (i = 0; i < nrings; ++i)
548 kfree(queues[i]);
549
550 kfree(queues);
551
552 return 0;
553
554nomem:
555 while (--i >= 0)
556 kfree(queues[i]);
557
558 kfree(queues);
559
560noqueues:
561 return -ENOMEM;
562}
563
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300564static inline void ptr_ring_cleanup(struct ptr_ring *r, void (*destroy)(void *))
565{
566 void *ptr;
567
568 if (destroy)
569 while ((ptr = ptr_ring_consume(r)))
570 destroy(ptr);
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300571 kfree(r->queue);
572}
573
574#endif /* _LINUX_PTR_RING_H */