Re: improve the kmem_cache_alloc_bulk API
From: Vlastimil Babka (SUSE)
Date: Wed May 27 2026 - 05:18:18 EST
On 5/27/26 09:02, Christoph Hellwig wrote:
> Hi all,
>
> kmem_cache_alloc_bulk has a very unintuitive and undocumented return
> value convention. Fix that and add documentation.
>
> Note that the few comments explaining it mention that the gfp flags
> must allow "spinning". That's not really a term used in the memory
> allocator, is this supposed to mean "block" or "sleep"?
Page allocator now has alloc_pages_nolock() for when no spinning is
possible, and it uses ALLOC_TRYLOCK internally.
Slab has kmalloc_nolock() relying on that when it needs new pages.
In terms of gfp flags, such context is currently indicated by lack of
__GFP_KSWAPD_RECLAIM, where lack of __GFP_DIRECT_RECLAIM only means "no
sleeping" - see gfpflags_allow_spinning(). Slab uses it internally as
there's no ALLOC_TRYLOCK, but also there are callers from memcg and stackdepot.
Like the rest of gfp flags it's far from ideal, maybe we'll figure out a
better design eventually.
> Diffstat:
> drivers/gpu/drm/msm/msm_iommu.c | 6 +--
> drivers/gpu/drm/panthor/panthor_mmu.c | 12 ++-----
> include/linux/slab.h | 6 ++-
> io_uring/io_uring.c | 23 +++++--------
> lib/test_meminit.c | 19 +++++------
> mm/kasan/kasan_test_c.c | 5 +-
> mm/kfence/kfence_test.c | 9 ++---
> mm/slub.c | 58 ++++++++++++++++++----------------
> net/bpf/test_run.c | 7 +---
> net/core/skbuff.c | 23 +++++++------
> tools/include/linux/slab.h | 2 -
> tools/testing/shared/linux.c | 19 ++++-------
> 12 files changed, 92 insertions(+), 97 deletions(-)