Re: [PATCH 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family
From: Hao Li
Date: Sun Nov 29 2026 - 22:35:02 EST
On Tue, Nov 24, 2026 at 05:30:03PM +0100, Vlastimil Babka (SUSE) wrote:
> The way buckets have been implemented was to extend
> __kmalloc_node_noprof() and two more functions with the kmem_buckets
> pointer parameter, which means all kmalloc_node() callers now have to
> pass NULL. To avoid this extra parameter with CONFIG_SLAB_BUCKETS=n,
> we added macros for parameter declaring and passing.
>
> This can be avoided by creating own entry points for kmem_buckets
> allocations and not using the kmalloc ones. They all end up performing
> __do_kmalloc_node() internally.
>
> Since it's just 3 functions with few callers, do not try to do anything
> special to eliminate code with CONFIG_SLAB_BUCKETS=n. Only make sure the
> bucket parameter is changed to NULL in that case because it would be
> ZERO_SIZE_PTR otherwise.
>
> The removal of the now-unnecessary parameter macros is done separately
> next.
>
> Signed-off-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>
> ---
> include/linux/slab.h | 16 +++++++--
> mm/slub.c | 100 +++++++++++++++++++++++++++++++++++++++++----------
> 2 files changed, 94 insertions(+), 22 deletions(-)
>
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index cda126def67a..46ecf99f044e 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -1175,11 +1175,18 @@ void *kmalloc_nolock(size_t size, gfp_t gfp_flags, int node);
> #define kvzalloc_flex(P, FAM, COUNT, ...) \
> __alloc_flex(kvzalloc, default_gfp(__VA_ARGS__), typeof(P), FAM, COUNT)
>
> +void *__kmem_buckets_alloc_noprof(kmem_buckets *b, size_t size, gfp_t flags)
> + __assume_kmalloc_alignment __alloc_size(2);
> +
> +void *__kmem_buckets_alloc_node_track_caller_noprof(kmem_buckets *b, size_t size,
> + gfp_t flags, int node, unsigned long caller)
> + __assume_kmalloc_alignment __alloc_size(2);
> +
> #define kmem_buckets_alloc(_b, _size, _flags) \
> - alloc_hooks(__kmalloc_node_noprof(PASS_KMALLOC_PARAMS(_size, _b, __kmalloc_token(_size)), _flags, NUMA_NO_NODE))
> + alloc_hooks(__kmem_buckets_alloc_noprof(_b, _size, _flags))
>
> #define kmem_buckets_alloc_node_track_caller(_b, _size, _flags, _node) \
> - alloc_hooks(__kmalloc_node_track_caller_noprof(PASS_KMALLOC_PARAMS(_size, _b, __kmalloc_token(_size)), _flags, _node, _RET_IP_))
> + alloc_hooks(__kmem_buckets_alloc_node_track_caller_noprof(_b, _size, _flags, _node, _RET_IP_))
>
> #define kmem_buckets_alloc_track_caller(_b, _size, _flags) \
> kmem_buckets_alloc_node_track_caller(_b, _size, _flags, NUMA_NO_NODE)
> @@ -1360,8 +1367,11 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node);
>
> #define kvzalloc_node(_size, _flags, _node) kvmalloc_node(_size, (_flags)|__GFP_ZERO, _node)
>
> +void *__kmem_buckets_valloc_noprof(kmem_buckets *b, size_t size,
> + unsigned long align, gfp_t flags, int node) __alloc_size(2);
> +
> #define kmem_buckets_valloc(_b, _size, _flags) \
> - alloc_hooks(__kvmalloc_node_noprof(PASS_KMALLOC_PARAMS(_size, _b, __kmalloc_token(_size)), 1, _flags, NUMA_NO_NODE))
> + alloc_hooks(__kmem_buckets_valloc_noprof(_b, _size, 1, _flags, NUMA_NO_NODE))
>
> static inline __alloc_size(1, 2) void *
> _kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node, kmalloc_token_t token)
> diff --git a/mm/slub.c b/mm/slub.c
> index 70df0a2e3876..6674caabbb6e 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -5732,6 +5732,34 @@ void *__kmalloc_flags_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags,
> }
> }
>
> +void *__kmem_buckets_alloc_noprof(kmem_buckets *b, size_t size, gfp_t flags)
> +{
> + const struct slab_alloc_context ac = {
> + .caller_addr = _RET_IP_,
> + .orig_size = size,
> + .alloc_flags = SLAB_ALLOC_DEFAULT,
> + };
> +
> + if (!IS_ENABLED(CONFIG_SLAB_BUCKETS))
> + b = NULL;
> +
> + return __do_kmalloc_node(b, flags, NUMA_NO_NODE, (kmalloc_token_t){}, &ac);
> +}
> +
> +void *__kmem_buckets_alloc_node_track_caller_noprof(kmem_buckets *b, size_t size,
> + gfp_t flags, int node, unsigned long caller)
> +{
> + const struct slab_alloc_context ac = {
> + .caller_addr = caller,
> + .orig_size = size,
> + .alloc_flags = SLAB_ALLOC_DEFAULT,
> + };
> +
> + if (!IS_ENABLED(CONFIG_SLAB_BUCKETS))
> + b = NULL;
> +
> + return __do_kmalloc_node(b, flags, node, (kmalloc_token_t){}, &ac);
> +}
>
> static noinline void free_to_partial_list(
> struct kmem_cache *s, struct slab *slab,
> @@ -7106,26 +7134,10 @@ static gfp_t kmalloc_gfp_adjust(gfp_t flags, size_t size)
> return flags;
> }
>
> -void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long align,
> - gfp_t flags, int node)
> +static void *__kvmalloc_fallback(size_t size, unsigned long align, gfp_t flags,
> + int node, const void *caller)
> {
> bool allow_block;
> - void *ret;
> - const struct slab_alloc_context ac = {
> - .caller_addr = _RET_IP_,
> - .orig_size = size,
> - .alloc_flags = SLAB_ALLOC_DEFAULT,
> - };
> -
> - /*
> - * It doesn't really make sense to fallback to vmalloc for sub page
> - * requests
> - */
> - ret = __do_kmalloc_node(PASS_BUCKET_PARAM(b),
> - kmalloc_gfp_adjust(flags, size),
> - node, PASS_TOKEN_PARAM(token), &ac);
> - if (ret || size <= PAGE_SIZE)
> - return ret;
>
> /* Don't even allow crazy sizes */
> if (unlikely(size > INT_MAX)) {
> @@ -7151,10 +7163,60 @@ void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long
> */
> return __vmalloc_node_range_noprof(size, align, VMALLOC_START, VMALLOC_END,
> flags, PAGE_KERNEL, allow_block ? VM_ALLOW_HUGE_VMAP:0,
> - node, __builtin_return_address(0));
> + node, caller);
> +}
> +
> +void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long align,
> + gfp_t flags, int node)
> +{
> + void *ret;
> + const struct slab_alloc_context ac = {
> + .caller_addr = _RET_IP_,
> + .orig_size = size,
> + .alloc_flags = SLAB_ALLOC_DEFAULT,
> + };
> +
> + /*
> + * It doesn't really make sense to fallback to vmalloc for sub page
> + * requests
> + */
> + ret = __do_kmalloc_node(PASS_BUCKET_PARAM(b),
> + kmalloc_gfp_adjust(flags, size),
> + node, PASS_TOKEN_PARAM(token), &ac);
> + if (ret || size <= PAGE_SIZE)
> + return ret;
> +
> + return __kvmalloc_fallback(size, align, flags, node,
> + __builtin_return_address(0));
> }
> EXPORT_SYMBOL(__kvmalloc_node_noprof);
>
> +void *__kmem_buckets_valloc_noprof(kmem_buckets *b, size_t size,
> + unsigned long align, gfp_t flags, int node)
> +{
> + void *ret;
> + const struct slab_alloc_context ac = {
> + .caller_addr = _RET_IP_,
> + .orig_size = size,
> + .alloc_flags = SLAB_ALLOC_DEFAULT,
> + };
> +
> + if (!IS_ENABLED(CONFIG_SLAB_BUCKETS))
> + b = NULL;
> +
> + /*
> + * It doesn't really make sense to fallback to vmalloc for sub page
> + * requests
> + */
> + ret = __do_kmalloc_node(b, kmalloc_gfp_adjust(flags, size),
> + node, (kmalloc_token_t){}, &ac);
> + if (ret || size <= PAGE_SIZE)
> + return ret;
> +
> + return __kvmalloc_fallback(size, align, flags, node,
> + __builtin_return_address(0));
> +}
It looks like __kmalloc_node_noprof, __kmalloc_node_track_caller_noprof, and
__kvmalloc_node_noprof are exported without any module users.
Could we unexport them, or do we need to export their counterparts as well for
consistency?
--
Thanks,
Hao