From: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>
To: Harry Yoo <harry@kernel.org>
Cc: Chris Down <chris@chrisdown.name>, Kees Cook <kees@kernel.org>,
Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
David Rientjes <rientjes@google.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
"Vlastimil Babka (SUSE)" <vbabka@kernel.org>
Subject: [PATCH 1/3] mm/slab: add ___do_kmalloc_node() to deduplicate code
Date: Tue, 24 Nov 2026 17:30:02 +0100 [thread overview]
Message-ID: <20261124-kmalloc-entry-rework-v1-1-37b1631a67b5@kernel.org> (raw)
In-Reply-To: <20261124-kmalloc-entry-rework-v1-0-37b1631a67b5@kernel.org>
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@kernel.org>
---
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
next prev parent reply other threads:[~2026-11-24 16:30 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 16:30 [PATCH 0/3] kmem_buckets entry point rework Vlastimil Babka (SUSE)
2026-11-24 16:30 ` Vlastimil Babka (SUSE) [this message]
2026-11-24 16:30 ` [PATCH 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family Vlastimil Babka (SUSE)
2026-11-24 16:30 ` [PATCH 3/3] mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros Vlastimil Babka (SUSE)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124-kmalloc-entry-rework-v1-1-37b1631a67b5@kernel.org \
--to=vbabka@kernel.org \
--cc=chris@chrisdown.name \
--cc=cl@gentwo.org \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox