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