blob: 6051a5fe69131d880fd0a0e1cb379e4b2208a4cd [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. Tsirkin84328342018-01-26 01:36:32 +020048 * for example cpu_relax().
49 *
50 * NB: this is unlike __ptr_ring_empty in that callers must hold producer_lock:
51 * see e.g. ptr_ring_full.
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +030052 */
53static inline bool __ptr_ring_full(struct ptr_ring *r)
54{
55 return r->queue[r->producer];
56}
57
58static inline bool ptr_ring_full(struct ptr_ring *r)
59{
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +030060 bool ret;
61
62 spin_lock(&r->producer_lock);
63 ret = __ptr_ring_full(r);
64 spin_unlock(&r->producer_lock);
65
66 return ret;
67}
68
69static inline bool ptr_ring_full_irq(struct ptr_ring *r)
70{
71 bool ret;
72
73 spin_lock_irq(&r->producer_lock);
74 ret = __ptr_ring_full(r);
75 spin_unlock_irq(&r->producer_lock);
76
77 return ret;
78}
79
80static inline bool ptr_ring_full_any(struct ptr_ring *r)
81{
82 unsigned long flags;
83 bool ret;
84
85 spin_lock_irqsave(&r->producer_lock, flags);
86 ret = __ptr_ring_full(r);
87 spin_unlock_irqrestore(&r->producer_lock, flags);
88
89 return ret;
90}
91
92static inline bool ptr_ring_full_bh(struct ptr_ring *r)
93{
94 bool ret;
95
96 spin_lock_bh(&r->producer_lock);
97 ret = __ptr_ring_full(r);
98 spin_unlock_bh(&r->producer_lock);
99
100 return ret;
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300101}
102
103/* Note: callers invoking this in a loop must use a compiler barrier,
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300104 * for example cpu_relax(). Callers must hold producer_lock.
Michael S. Tsirkina8ceb5d2017-12-05 21:29:37 +0200105 * Callers are responsible for making sure pointer that is being queued
106 * points to a valid data.
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300107 */
108static inline int __ptr_ring_produce(struct ptr_ring *r, void *ptr)
109{
Jason Wang982fb492016-06-30 14:45:31 +0800110 if (unlikely(!r->size) || r->queue[r->producer])
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300111 return -ENOSPC;
112
Michael S. Tsirkina8ceb5d2017-12-05 21:29:37 +0200113 /* Make sure the pointer we are storing points to a valid data. */
114 /* Pairs with smp_read_barrier_depends in __ptr_ring_consume. */
115 smp_wmb();
116
Michael S. Tsirkina07d29c2018-01-26 01:36:38 +0200117 WRITE_ONCE(r->queue[r->producer++], ptr);
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300118 if (unlikely(r->producer >= r->size))
119 r->producer = 0;
120 return 0;
121}
122
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200123/*
124 * Note: resize (below) nests producer lock within consumer lock, so if you
125 * consume in interrupt or BH context, you must disable interrupts/BH when
126 * calling this.
127 */
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300128static inline int ptr_ring_produce(struct ptr_ring *r, void *ptr)
129{
130 int ret;
131
132 spin_lock(&r->producer_lock);
133 ret = __ptr_ring_produce(r, ptr);
134 spin_unlock(&r->producer_lock);
135
136 return ret;
137}
138
139static inline int ptr_ring_produce_irq(struct ptr_ring *r, void *ptr)
140{
141 int ret;
142
143 spin_lock_irq(&r->producer_lock);
144 ret = __ptr_ring_produce(r, ptr);
145 spin_unlock_irq(&r->producer_lock);
146
147 return ret;
148}
149
150static inline int ptr_ring_produce_any(struct ptr_ring *r, void *ptr)
151{
152 unsigned long flags;
153 int ret;
154
155 spin_lock_irqsave(&r->producer_lock, flags);
156 ret = __ptr_ring_produce(r, ptr);
157 spin_unlock_irqrestore(&r->producer_lock, flags);
158
159 return ret;
160}
161
162static inline int ptr_ring_produce_bh(struct ptr_ring *r, void *ptr)
163{
164 int ret;
165
166 spin_lock_bh(&r->producer_lock);
167 ret = __ptr_ring_produce(r, ptr);
168 spin_unlock_bh(&r->producer_lock);
169
170 return ret;
171}
172
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300173static inline void *__ptr_ring_peek(struct ptr_ring *r)
174{
Jason Wang982fb492016-06-30 14:45:31 +0800175 if (likely(r->size))
Michael S. Tsirkina07d29c2018-01-26 01:36:38 +0200176 return READ_ONCE(r->queue[r->consumer_head]);
Jason Wang982fb492016-06-30 14:45:31 +0800177 return NULL;
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300178}
179
Michael S. Tsirkin8619d382018-01-26 01:36:28 +0200180/*
181 * Test ring empty status without taking any locks.
182 *
183 * NB: This is only safe to call if ring is never resized.
184 *
185 * However, if some other CPU consumes ring entries at the same time, the value
186 * returned is not guaranteed to be correct.
187 *
188 * In this case - to avoid incorrectly detecting the ring
189 * as empty - the CPU consuming the ring entries is responsible
190 * for either consuming all ring entries until the ring is empty,
191 * or synchronizing with some other CPU and causing it to
192 * re-test __ptr_ring_empty and/or consume the ring enteries
193 * after the synchronization point.
194 *
195 * Note: callers invoking this in a loop must use a compiler barrier,
196 * for example cpu_relax().
197 */
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300198static inline bool __ptr_ring_empty(struct ptr_ring *r)
199{
Michael S. Tsirkina259df32018-01-26 01:36:29 +0200200 if (likely(r->size))
201 return !r->queue[READ_ONCE(r->consumer_head)];
202 return true;
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300203}
204
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300205static inline bool ptr_ring_empty(struct ptr_ring *r)
206{
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300207 bool ret;
208
209 spin_lock(&r->consumer_lock);
210 ret = __ptr_ring_empty(r);
211 spin_unlock(&r->consumer_lock);
212
213 return ret;
214}
215
216static inline bool ptr_ring_empty_irq(struct ptr_ring *r)
217{
218 bool ret;
219
220 spin_lock_irq(&r->consumer_lock);
221 ret = __ptr_ring_empty(r);
222 spin_unlock_irq(&r->consumer_lock);
223
224 return ret;
225}
226
227static inline bool ptr_ring_empty_any(struct ptr_ring *r)
228{
229 unsigned long flags;
230 bool ret;
231
232 spin_lock_irqsave(&r->consumer_lock, flags);
233 ret = __ptr_ring_empty(r);
234 spin_unlock_irqrestore(&r->consumer_lock, flags);
235
236 return ret;
237}
238
239static inline bool ptr_ring_empty_bh(struct ptr_ring *r)
240{
241 bool ret;
242
243 spin_lock_bh(&r->consumer_lock);
244 ret = __ptr_ring_empty(r);
245 spin_unlock_bh(&r->consumer_lock);
246
247 return ret;
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300248}
249
250/* Must only be called after __ptr_ring_peek returned !NULL */
251static inline void __ptr_ring_discard_one(struct ptr_ring *r)
252{
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300253 /* Fundamentally, what we want to do is update consumer
254 * index and zero out the entry so producer can reuse it.
255 * Doing it naively at each consume would be as simple as:
Michael S. Tsirkin406de752018-01-26 01:36:27 +0200256 * consumer = r->consumer;
257 * r->queue[consumer++] = NULL;
258 * if (unlikely(consumer >= r->size))
259 * consumer = 0;
260 * r->consumer = consumer;
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300261 * but that is suboptimal when the ring is full as producer is writing
262 * out new entries in the same cache line. Defer these updates until a
263 * batch of entries has been consumed.
264 */
Michael S. Tsirkin406de752018-01-26 01:36:27 +0200265 /* Note: we must keep consumer_head valid at all times for __ptr_ring_empty
266 * to work correctly.
267 */
268 int consumer_head = r->consumer_head;
269 int head = consumer_head++;
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300270
271 /* Once we have processed enough entries invalidate them in
272 * the ring all at once so producer can reuse their space in the ring.
273 * We also do this when we reach end of the ring - not mandatory
274 * but helps keep the implementation simple.
275 */
Michael S. Tsirkin406de752018-01-26 01:36:27 +0200276 if (unlikely(consumer_head - r->consumer_tail >= r->batch ||
277 consumer_head >= r->size)) {
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300278 /* Zero out entries in the reverse order: this way we touch the
279 * cache line that producer might currently be reading the last;
280 * producer won't make progress and touch other cache lines
281 * besides the first one until we write out all entries.
282 */
283 while (likely(head >= r->consumer_tail))
284 r->queue[head--] = NULL;
Michael S. Tsirkin406de752018-01-26 01:36:27 +0200285 r->consumer_tail = consumer_head;
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300286 }
Michael S. Tsirkin406de752018-01-26 01:36:27 +0200287 if (unlikely(consumer_head >= r->size)) {
288 consumer_head = 0;
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300289 r->consumer_tail = 0;
290 }
Michael S. Tsirkina259df32018-01-26 01:36:29 +0200291 /* matching READ_ONCE in __ptr_ring_empty for lockless tests */
292 WRITE_ONCE(r->consumer_head, consumer_head);
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300293}
294
295static inline void *__ptr_ring_consume(struct ptr_ring *r)
296{
297 void *ptr;
298
299 ptr = __ptr_ring_peek(r);
300 if (ptr)
301 __ptr_ring_discard_one(r);
302
Michael S. Tsirkina8ceb5d2017-12-05 21:29:37 +0200303 /* Make sure anyone accessing data through the pointer is up to date. */
304 /* Pairs with smp_wmb in __ptr_ring_produce. */
305 smp_read_barrier_depends();
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300306 return ptr;
307}
308
Jason Wang728fc8d2017-05-17 12:14:39 +0800309static inline int __ptr_ring_consume_batched(struct ptr_ring *r,
310 void **array, int n)
311{
312 void *ptr;
313 int i;
314
315 for (i = 0; i < n; i++) {
316 ptr = __ptr_ring_consume(r);
317 if (!ptr)
318 break;
319 array[i] = ptr;
320 }
321
322 return i;
323}
324
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200325/*
326 * Note: resize (below) nests producer lock within consumer lock, so if you
327 * call this in interrupt or BH context, you must disable interrupts/BH when
328 * producing.
329 */
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300330static inline void *ptr_ring_consume(struct ptr_ring *r)
331{
332 void *ptr;
333
334 spin_lock(&r->consumer_lock);
335 ptr = __ptr_ring_consume(r);
336 spin_unlock(&r->consumer_lock);
337
338 return ptr;
339}
340
341static inline void *ptr_ring_consume_irq(struct ptr_ring *r)
342{
343 void *ptr;
344
345 spin_lock_irq(&r->consumer_lock);
346 ptr = __ptr_ring_consume(r);
347 spin_unlock_irq(&r->consumer_lock);
348
349 return ptr;
350}
351
352static inline void *ptr_ring_consume_any(struct ptr_ring *r)
353{
354 unsigned long flags;
355 void *ptr;
356
357 spin_lock_irqsave(&r->consumer_lock, flags);
358 ptr = __ptr_ring_consume(r);
359 spin_unlock_irqrestore(&r->consumer_lock, flags);
360
361 return ptr;
362}
363
364static inline void *ptr_ring_consume_bh(struct ptr_ring *r)
365{
366 void *ptr;
367
368 spin_lock_bh(&r->consumer_lock);
369 ptr = __ptr_ring_consume(r);
370 spin_unlock_bh(&r->consumer_lock);
371
372 return ptr;
373}
374
Jason Wang728fc8d2017-05-17 12:14:39 +0800375static inline int ptr_ring_consume_batched(struct ptr_ring *r,
376 void **array, int n)
377{
378 int ret;
379
380 spin_lock(&r->consumer_lock);
381 ret = __ptr_ring_consume_batched(r, array, n);
382 spin_unlock(&r->consumer_lock);
383
384 return ret;
385}
386
387static inline int ptr_ring_consume_batched_irq(struct ptr_ring *r,
388 void **array, int n)
389{
390 int ret;
391
392 spin_lock_irq(&r->consumer_lock);
393 ret = __ptr_ring_consume_batched(r, array, n);
394 spin_unlock_irq(&r->consumer_lock);
395
396 return ret;
397}
398
399static inline int ptr_ring_consume_batched_any(struct ptr_ring *r,
400 void **array, int n)
401{
402 unsigned long flags;
403 int ret;
404
405 spin_lock_irqsave(&r->consumer_lock, flags);
406 ret = __ptr_ring_consume_batched(r, array, n);
407 spin_unlock_irqrestore(&r->consumer_lock, flags);
408
409 return ret;
410}
411
412static inline int ptr_ring_consume_batched_bh(struct ptr_ring *r,
413 void **array, int n)
414{
415 int ret;
416
417 spin_lock_bh(&r->consumer_lock);
418 ret = __ptr_ring_consume_batched(r, array, n);
419 spin_unlock_bh(&r->consumer_lock);
420
421 return ret;
422}
423
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300424/* Cast to structure type and call a function without discarding from FIFO.
425 * Function must return a value.
426 * Callers must take consumer_lock.
427 */
428#define __PTR_RING_PEEK_CALL(r, f) ((f)(__ptr_ring_peek(r)))
429
430#define PTR_RING_PEEK_CALL(r, f) ({ \
431 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
432 \
433 spin_lock(&(r)->consumer_lock); \
434 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
435 spin_unlock(&(r)->consumer_lock); \
436 __PTR_RING_PEEK_CALL_v; \
437})
438
439#define PTR_RING_PEEK_CALL_IRQ(r, f) ({ \
440 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
441 \
442 spin_lock_irq(&(r)->consumer_lock); \
443 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
444 spin_unlock_irq(&(r)->consumer_lock); \
445 __PTR_RING_PEEK_CALL_v; \
446})
447
448#define PTR_RING_PEEK_CALL_BH(r, f) ({ \
449 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
450 \
451 spin_lock_bh(&(r)->consumer_lock); \
452 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
453 spin_unlock_bh(&(r)->consumer_lock); \
454 __PTR_RING_PEEK_CALL_v; \
455})
456
457#define PTR_RING_PEEK_CALL_ANY(r, f) ({ \
458 typeof((f)(NULL)) __PTR_RING_PEEK_CALL_v; \
459 unsigned long __PTR_RING_PEEK_CALL_f;\
460 \
461 spin_lock_irqsave(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
462 __PTR_RING_PEEK_CALL_v = __PTR_RING_PEEK_CALL(r, f); \
463 spin_unlock_irqrestore(&(r)->consumer_lock, __PTR_RING_PEEK_CALL_f); \
464 __PTR_RING_PEEK_CALL_v; \
465})
466
Eric Dumazet81fbfe82017-08-16 10:36:47 -0700467static inline void **__ptr_ring_init_queue_alloc(unsigned int size, gfp_t gfp)
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300468{
Jason Wang6e6e41c2018-02-09 17:45:49 +0800469 if (size * sizeof(void *) > KMALLOC_MAX_SIZE)
470 return NULL;
Michael S. Tsirkin9fb582b2018-01-26 01:36:35 +0200471 return kcalloc(size, sizeof(void *), gfp);
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300472}
473
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300474static inline void __ptr_ring_set_size(struct ptr_ring *r, int size)
475{
476 r->size = 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.
482 */
483 if (r->batch > r->size / 2 || !r->batch)
484 r->batch = 1;
485}
486
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300487static inline int ptr_ring_init(struct ptr_ring *r, int size, gfp_t gfp)
488{
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300489 r->queue = __ptr_ring_init_queue_alloc(size, gfp);
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300490 if (!r->queue)
491 return -ENOMEM;
492
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300493 __ptr_ring_set_size(r, size);
494 r->producer = r->consumer_head = r->consumer_tail = 0;
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300495 spin_lock_init(&r->producer_lock);
496 spin_lock_init(&r->consumer_lock);
497
498 return 0;
499}
500
Michael S. Tsirkin197a5212017-05-17 12:14:37 +0800501/*
502 * Return entries into ring. Destroy entries that don't fit.
503 *
504 * Note: this is expected to be a rare slow path operation.
505 *
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.
510 */
511static inline void ptr_ring_unconsume(struct ptr_ring *r, void **batch, int n,
512 void (*destroy)(void *))
513{
514 unsigned long flags;
515 int head;
516
517 spin_lock_irqsave(&r->consumer_lock, flags);
518 spin_lock(&r->producer_lock);
519
520 if (!r->size)
521 goto done;
522
523 /*
524 * Clean out buffered entries (for simplicity). This way following code
525 * can test entries for NULL and if not assume they are valid.
526 */
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;
531
532 /*
533 * Go over entries in batch, start moving head back and copy entries.
534 * Stop when we run into previously unconsumed entries.
535 */
536 while (n) {
537 head = r->consumer_head - 1;
538 if (head < 0)
539 head = r->size - 1;
540 if (r->queue[head]) {
541 /* This batch entry will have to be destroyed. */
542 goto done;
543 }
544 r->queue[head] = batch[--n];
Michael S. Tsirkina259df32018-01-26 01:36:29 +0200545 r->consumer_tail = head;
546 /* matching READ_ONCE in __ptr_ring_empty for lockless tests */
547 WRITE_ONCE(r->consumer_head, head);
Michael S. Tsirkin197a5212017-05-17 12:14:37 +0800548 }
549
550done:
551 /* Destroy all entries left in the batch. */
552 while (n)
553 destroy(batch[--n]);
554 spin_unlock(&r->producer_lock);
555 spin_unlock_irqrestore(&r->consumer_lock, flags);
556}
557
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800558static inline void **__ptr_ring_swap_queue(struct ptr_ring *r, void **queue,
559 int size, gfp_t gfp,
560 void (*destroy)(void *))
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300561{
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300562 int producer = 0;
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300563 void **old;
564 void *ptr;
565
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200566 while ((ptr = __ptr_ring_consume(r)))
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300567 if (producer < size)
568 queue[producer++] = ptr;
569 else if (destroy)
570 destroy(ptr);
571
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300572 __ptr_ring_set_size(r, size);
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300573 r->producer = producer;
Michael S. Tsirkinfb9de972017-04-07 08:25:09 +0300574 r->consumer_head = 0;
575 r->consumer_tail = 0;
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300576 old = r->queue;
577 r->queue = queue;
578
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800579 return old;
580}
581
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200582/*
583 * Note: producer lock is nested within consumer lock, so if you
584 * resize you must make sure all uses nest correctly.
585 * In particular if you consume ring in interrupt or BH context, you must
586 * disable interrupts/BH when doing so.
587 */
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800588static inline int ptr_ring_resize(struct ptr_ring *r, int size, gfp_t gfp,
589 void (*destroy)(void *))
590{
591 unsigned long flags;
592 void **queue = __ptr_ring_init_queue_alloc(size, gfp);
593 void **old;
594
595 if (!queue)
596 return -ENOMEM;
597
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200598 spin_lock_irqsave(&(r)->consumer_lock, flags);
599 spin_lock(&(r)->producer_lock);
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800600
601 old = __ptr_ring_swap_queue(r, queue, size, gfp, destroy);
602
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200603 spin_unlock(&(r)->producer_lock);
604 spin_unlock_irqrestore(&(r)->consumer_lock, flags);
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300605
606 kfree(old);
607
608 return 0;
609}
610
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200611/*
612 * Note: producer lock is nested within consumer lock, so if you
613 * resize you must make sure all uses nest correctly.
614 * In particular if you consume ring in interrupt or BH context, you must
615 * disable interrupts/BH when doing so.
616 */
Eric Dumazet81fbfe82017-08-16 10:36:47 -0700617static inline int ptr_ring_resize_multiple(struct ptr_ring **rings,
618 unsigned int nrings,
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800619 int size,
620 gfp_t gfp, void (*destroy)(void *))
621{
622 unsigned long flags;
623 void ***queues;
624 int i;
625
Eric Dumazet81fbfe82017-08-16 10:36:47 -0700626 queues = kmalloc_array(nrings, sizeof(*queues), gfp);
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800627 if (!queues)
628 goto noqueues;
629
630 for (i = 0; i < nrings; ++i) {
631 queues[i] = __ptr_ring_init_queue_alloc(size, gfp);
632 if (!queues[i])
633 goto nomem;
634 }
635
636 for (i = 0; i < nrings; ++i) {
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200637 spin_lock_irqsave(&(rings[i])->consumer_lock, flags);
638 spin_lock(&(rings[i])->producer_lock);
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800639 queues[i] = __ptr_ring_swap_queue(rings[i], queues[i],
640 size, gfp, destroy);
Michael S. Tsirkine7169532017-02-19 07:17:17 +0200641 spin_unlock(&(rings[i])->producer_lock);
642 spin_unlock_irqrestore(&(rings[i])->consumer_lock, flags);
Michael S. Tsirkin59e6ae52016-06-30 14:45:33 +0800643 }
644
645 for (i = 0; i < nrings; ++i)
646 kfree(queues[i]);
647
648 kfree(queues);
649
650 return 0;
651
652nomem:
653 while (--i >= 0)
654 kfree(queues[i]);
655
656 kfree(queues);
657
658noqueues:
659 return -ENOMEM;
660}
661
Michael S. Tsirkin5d49de52016-06-13 23:54:45 +0300662static inline void ptr_ring_cleanup(struct ptr_ring *r, void (*destroy)(void *))
663{
664 void *ptr;
665
666 if (destroy)
667 while ((ptr = ptr_ring_consume(r)))
668 destroy(ptr);
Michael S. Tsirkin2e0ab8c2016-06-13 23:54:31 +0300669 kfree(r->queue);
670}
671
672#endif /* _LINUX_PTR_RING_H */