[PATCH] mempool: optimize mempool resizing
Andrew Morton
akpm at linux-foundation.org
Thu Jun 25 13:17:38 PDT 2026
On Thu, 25 Jun 2026 21:49:15 +0200 Vitaly Wool <vitaly.wool at konsulko.se> wrote:
> From: Vitaly Vul <vitaly.vul at partner.samsung.com>
>
> Resizing mempool to a bigger size currently requires a new allocation and a
> data copy to a new larger elements array which doesn't go well with the
> idea of having a fast and deadlock free memory allocations during exreme VM
> load.
>
> This patch introduces a new parameter max_nr for a part of the mempool API,
> which denotes the maximum size of the pool. With it in place we can avoid
> any allocations in mempool_resize() since we can either grant the resizing
> request or reject it basing on thr maximum allowed size of the elements
> array. For those few users of the mempool API that actually use
> mempool_resize() it is a clear upgrade because the maximum number of
> elements is known upfront.
>
> Derivative APIs (like mempool_init_kmalloc_pool()) are mostly left intact,
> substituted by the new mempool_init_kmalloc_resizable_pool() API where the
> pool is actually meant to be resizable.
Thanks.
It would be helpful to have a description of what inspired this work.
Presumably there is some observed problem in nvme or in zone-blockdev
use? Can you please describe the problems and also describe what
effect the patch had upon them?
>
> ...
>
> int mempool_resize(struct mempool *pool, int new_min_nr)
> {
> void *element;
> - void **new_elements;
> unsigned long flags;
>
> - BUG_ON(new_min_nr <= 0);
> - might_sleep();
> + if (WARN_ON(new_min_nr < 0))
> + return -EINVAL;
> +
> + if (new_min_nr > pool->max_nr)
> + return -ENOSPC;
Seems strange to return -EINVAL if new_min_nr is too low, but -ENOSPC
if it is too large.
Also, (pet peeve), ENOSPC means "No space left on device". If this
errno ever makes it back to userspace, the operator will be running
`df' and wondering what the heck happened.
Also, AI review had things to say:
https://sashiko.dev/#/patchset/20260625194915.387663-1-vitaly.wool@konsulko.se
More information about the Linux-nvme
mailing list