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 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family
Date: Tue, 24 Nov 2026 17:30:03 +0100 [thread overview]
Message-ID: <20261124-kmalloc-entry-rework-v1-2-37b1631a67b5@kernel.org> (raw)
In-Reply-To: <20261124-kmalloc-entry-rework-v1-0-37b1631a67b5@kernel.org>
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@kernel.org>
---
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));
+}
+
/**
* kvfree() - Free memory.
* @addr: Pointer to allocated memory.
--
2.56.1
next prev parent reply other threads:[~2026-11-24 16:30 UTC|newest]
Thread overview: 13+ 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 ` [PATCH 1/3] mm/slab: add ___do_kmalloc_node() to deduplicate code Vlastimil Babka (SUSE)
2026-11-24 16:30 ` Vlastimil Babka (SUSE) [this message]
2026-11-26 14:22 ` [PATCH 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family Harry Yoo
2026-11-26 19:04 ` Vlastimil Babka (SUSE)
2026-11-26 19:59 ` Marco Elver
2026-11-27 13:56 ` Harry Yoo
2026-11-30 3:34 ` Hao Li
2026-11-30 8:31 ` Vlastimil Babka (SUSE)
2026-11-30 8:38 ` Vlastimil Babka (SUSE)
2026-11-30 12:03 ` Hao Li
2026-11-24 16:30 ` [PATCH 3/3] mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros Vlastimil Babka (SUSE)
2026-11-30 3:56 ` Hao Li
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-2-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.