[PATCH 1/3] mm/slab: add ___do_kmalloc_node() to deduplicate code

From: Vlastimil Babka (SUSE)

Date: Tue Nov 24 2026 - 11:30:22 EST


Most kmalloc family entry points end up performing __do_kmalloc_node().
What it does are roughly two phases. First, selecting the right cache
via kmalloc_slab(). Second, performing slab_alloc_node(),
kasan_kmalloc() and trace_kmalloc().

__kmalloc_cache_noprof() and __kmalloc_cache_node_noprof() already have
the cache pointer, so they both opencode the second phase. Add
___do_kmalloc_node() for the second phase.

It's useful to inline the first phase as some parameters might be
hardcoded NULL, but for the second phase it means duplicating a lot of
code to avoid any calls for the allocation fastpath. So let's try not
forcing ___do_kmalloc_node() to be inlined. It could almost be a tail
call, but isn't, likely due to the on-stack slab_alloc_context.

This is the effect on slub.o. Almost the size of the new function is
removed from all its callers, as expected.

./scripts/bloat-o-meter slub.o.base slub.o.1
add/remove: 2/1 grow/shrink: 7/9 up/down: 1005/-5353 (-4348)
Function old new delta
___do_kmalloc_node - 768 +768
sysfs_slab_alias 166 221 +55
sysfs_slab_add 469 511 +42
do_kmem_cache_create 1677 1714 +37
slab_memory_callback 677 713 +36
slub_cpu_setup 415 446 +31
show_slab_objects 707 723 +16
calculate_sizes 1328 1344 +16
process_slab 1209 1193 -16
check_bytes_and_report 226 210 -16
__kmalloc_node_track_caller_noprof 1087 382 -705
__kmalloc_node_noprof 1092 387 -705
__kmalloc_noprof 1100 375 -725
__kmalloc_cache_noprof 790 62 -728
__kmalloc_cache_node_noprof 827 57 -770
__kvmalloc_node_noprof 1442 637 -805
__kmalloc_flags_noprof 1307 428 -879
Total: Before=82451, After=78103, chg -5.27%

Signed-off-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>
---
mm/slub.c | 39 +++++++++++++++++++++------------------
1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index c35a50538d00..70df0a2e3876 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -5512,6 +5512,23 @@ void *__kmalloc_large_node_noprof(size_t size, gfp_t flags, int node)
}
EXPORT_SYMBOL(__kmalloc_large_node_noprof);

+/*
+ * Deliberately not inline to have all the callers share the code, using a
+ * single call.
+ */
+static
+void *___do_kmalloc_node(struct kmem_cache *s, gfp_t flags, int node,
+ const struct slab_alloc_context *ac)
+{
+ void *ret;
+
+ ret = slab_alloc_node(s, flags, node, ac);
+ ret = kasan_kmalloc(s, ret, ac->orig_size, flags);
+ trace_kmalloc(ac->caller_addr, ret, ac->orig_size, s->size, flags, node);
+
+ return ret;
+}
+
static __always_inline
void *__do_kmalloc_node(kmem_buckets *b, gfp_t flags, int node,
kmalloc_token_t token, const struct slab_alloc_context *ac)
@@ -5532,11 +5549,9 @@ void *__do_kmalloc_node(kmem_buckets *b, gfp_t flags, int node,

s = kmalloc_slab(size, b, flags, token, ac->alloc_flags);

- ret = slab_alloc_node(s, flags, node, ac);
- ret = kasan_kmalloc(s, ret, size, flags);
- trace_kmalloc(ac->caller_addr, ret, size, s->size, flags, node);
- return ret;
+ return ___do_kmalloc_node(s, flags, node, ac);
}
+
void *__kmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags, int node)
{
const struct slab_alloc_context ac = {
@@ -5671,38 +5686,26 @@ EXPORT_SYMBOL(__kmalloc_node_track_caller_noprof);

void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t gfpflags, size_t size)
{
- void *ret;
const struct slab_alloc_context ac = {
.caller_addr = _RET_IP_,
.orig_size = size,
.alloc_flags = SLAB_ALLOC_DEFAULT,
};

- ret = slab_alloc_node(s, gfpflags, NUMA_NO_NODE, &ac);
-
- trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, NUMA_NO_NODE);
-
- ret = kasan_kmalloc(s, ret, size, gfpflags);
- return ret;
+ return ___do_kmalloc_node(s, gfpflags, NUMA_NO_NODE, &ac);
}
EXPORT_SYMBOL(__kmalloc_cache_noprof);

void *__kmalloc_cache_node_noprof(struct kmem_cache *s, gfp_t gfpflags,
int node, size_t size)
{
- void *ret;
const struct slab_alloc_context ac = {
.caller_addr = _RET_IP_,
.orig_size = size,
.alloc_flags = SLAB_ALLOC_DEFAULT,
};

- ret = slab_alloc_node(s, gfpflags, node, &ac);
-
- trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, node);
-
- ret = kasan_kmalloc(s, ret, size, gfpflags);
- return ret;
+ return ___do_kmalloc_node(s, gfpflags, node, &ac);
}
EXPORT_SYMBOL(__kmalloc_cache_node_noprof);


--
2.56.1