ptr_ring_resize_multiple_bh() allocates all the new queues before it swaps any of them in, so it either resizes every ring or none. A caller that has to do other work that can fail between allocating and swapping cannot use it. pfifo_fast needs this to change the rings of all tx queues of a device together. Split it into ptr_ring_alloc_queues(), which can fail, and ptr_ring_swap_queues_bh(), which cannot. ptr_ring_free_queues() frees an allocation that is not swapped in. ptr_ring_resize_multiple_bh() now calls the first two in turn; no functional change. The patch was written with Claude Code (Claude Opus 5.5). Signed-off-by: SeungJong Ha --- include/linux/ptr_ring.h | 79 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/include/linux/ptr_ring.h b/include/linux/ptr_ring.h index 4e710397903b..539063e29a55 100644 --- a/include/linux/ptr_ring.h +++ b/include/linux/ptr_ring.h @@ -645,22 +645,18 @@ static inline int ptr_ring_resize_noprof(struct ptr_ring *r, int size, gfp_t gfp #define ptr_ring_resize(...) alloc_hooks(ptr_ring_resize_noprof(__VA_ARGS__)) /* - * Note: producer lock is nested within consumer lock, so if you - * resize you must make sure all uses nest correctly. - * In particular if you consume ring in BH context, you must - * disable BH when doing so. + * Allocate @nrings queues of @size entries for ptr_ring_swap_queues_bh(). + * On failure, return NULL with nothing allocated. */ -static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings, - unsigned int nrings, - int size, gfp_t gfp, - void (*destroy)(void *)) +static inline void ***ptr_ring_alloc_queues_noprof(unsigned int nrings, + int size, gfp_t gfp) { void ***queues; int i; queues = kmalloc_array_noprof(nrings, sizeof(*queues), gfp); if (!queues) - goto noqueues; + return NULL; for (i = 0; i < nrings; ++i) { queues[i] = __ptr_ring_init_queue_alloc_noprof(size, gfp); @@ -668,6 +664,41 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings, goto nomem; } + return queues; + +nomem: + while (--i >= 0) + kvfree(queues[i]); + + kfree(queues); + + return NULL; +} +#define ptr_ring_alloc_queues(...) \ + alloc_hooks(ptr_ring_alloc_queues_noprof(__VA_ARGS__)) + +/* Free @queues from ptr_ring_alloc_queues() without swapping them in. */ +static inline void ptr_ring_free_queues(void ***queues, unsigned int nrings) +{ + int i; + + for (i = 0; i < nrings; ++i) + kvfree(queues[i]); + + kfree(queues); +} + +/* + * Swap in @queues from ptr_ring_alloc_queues() with the same @nrings and + * @size, then free the old queues and @queues. Locking is as for + * ptr_ring_resize_multiple_bh(). + */ +static inline void ptr_ring_swap_queues_bh(struct ptr_ring **rings, + void ***queues, unsigned int nrings, + int size, void (*destroy)(void *)) +{ + int i; + for (i = 0; i < nrings; ++i) { spin_lock_bh(&(rings[i])->consumer_lock); spin_lock(&(rings[i])->producer_lock); @@ -677,21 +708,29 @@ static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings, spin_unlock_bh(&(rings[i])->consumer_lock); } - for (i = 0; i < nrings; ++i) - kvfree(queues[i]); - - kfree(queues); + ptr_ring_free_queues(queues, nrings); +} - return 0; +/* + * Note: producer lock is nested within consumer lock, so if you + * resize you must make sure all uses nest correctly. + * In particular if you consume ring in BH context, you must + * disable BH when doing so. + */ +static inline int ptr_ring_resize_multiple_bh_noprof(struct ptr_ring **rings, + unsigned int nrings, + int size, gfp_t gfp, + void (*destroy)(void *)) +{ + void ***queues; -nomem: - while (--i >= 0) - kvfree(queues[i]); + queues = ptr_ring_alloc_queues_noprof(nrings, size, gfp); + if (!queues) + return -ENOMEM; - kfree(queues); + ptr_ring_swap_queues_bh(rings, queues, nrings, size, destroy); -noqueues: - return -ENOMEM; + return 0; } #define ptr_ring_resize_multiple_bh(...) \ alloc_hooks(ptr_ring_resize_multiple_bh_noprof(__VA_ARGS__)) -- 2.54.0