* [PATCH v2 0/3] kmem_buckets entry point rework
@ 2026-12-03 13:42 Vlastimil Babka (SUSE)
2026-12-03 13:42 ` [PATCH v2 1/3] mm/slab: add ___do_kmalloc_node() to deduplicate code Vlastimil Babka (SUSE)
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-12-03 13:42 UTC (permalink / raw)
To: Harry Yoo
Cc: Chris Down, Kees Cook, Hao Li, Christoph Lameter, David Rientjes,
Roman Gushchin, linux-mm, linux-kernel, Vlastimil Babka (SUSE)
When pondering the boot-time slab hardening features patchset [1] it
occured to me that the kmem_buckets handling could be redone in a way
that there are no kmem_buckets pointers in kmalloc_node() and two more
functions, and we can drop some of the tedious parameter declaring and
passing macros (except those for randomizing tokens).
So instead add own entry points for kmem_buckets enabled allocations.
The first patch is a preparatory cleanup. In v1 it also attempted to
make the new entry points cheaper (in terms of binary size) by making a
bet that one extra call in the allocation fastpath is better than many
functions inlining the same code.
Since it was however visible as 10% slowdown in the proposed slab kunit
benchmark [2], in v2 the new intermediate function is also inlined. It
still removes some duplicated code) and we pay the binary size of the
new entry points (about 1k each). In the last patch we however save
passing an unnecessary bucket parameter in all the kmalloc_node()
callers.
With v2, the benchmark [2] shows no difference in performance.
If this is accepted, [1] should be rebased on it as it makes the
slab_buckets boot-time handling really straightforward. In fact
CONFIG_SLAB_BUCKETS could be repurposed to only control the default
behavior, while the code is always compiled in.
[1] https://lore.kernel.org/all/cover.1794506262.git.chris@chrisdown.name/
[2] https://lore.kernel.org/all/20261129212551.1812084-3-rientjes@google.com/
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
Changes in v2:
- Make sure token parameter is passed to the new entry points (Harry).
- Inline ___do_kmalloc_node() as explained above.
- Link to v1: https://patch.msgid.link/20261124-kmalloc-entry-rework-v1-0-37b1631a67b5@kernel.org
---
Vlastimil Babka (SUSE) (3):
mm/slab: add ___do_kmalloc_node() to deduplicate code
mm/slab: add own entry points for kmem_buckets_alloc() family
mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros
include/linux/slab.h | 64 ++++++++++------------
mm/slub.c | 149 +++++++++++++++++++++++++++++++++++----------------
2 files changed, 133 insertions(+), 80 deletions(-)
---
base-commit: bc8dbe6c5b86e3e6331f589166afb7d310b604d2
change-id: 20261116-kmalloc-entry-rework-a0162912968b
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/3] mm/slab: add ___do_kmalloc_node() to deduplicate code
2026-12-03 13:42 [PATCH v2 0/3] kmem_buckets entry point rework Vlastimil Babka (SUSE)
@ 2026-12-03 13:42 ` Vlastimil Babka (SUSE)
2026-12-03 13:42 ` [PATCH v2 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family Vlastimil Babka (SUSE)
2026-12-03 13:42 ` [PATCH v2 3/3] mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros Vlastimil Babka (SUSE)
2 siblings, 0 replies; 6+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-12-03 13:42 UTC (permalink / raw)
To: Harry Yoo
Cc: Chris Down, Kees Cook, Hao Li, Christoph Lameter, David Rientjes,
Roman Gushchin, linux-mm, linux-kernel, Vlastimil Babka (SUSE)
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, called by both of those and
__do_kmalloc_node().
Make it also inline so the whole kmalloc fast path keeps being inlined.
An attempt to not make it inlined resulted in a visible slab kunit
benchmark regression.
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
mm/slub.c | 35 +++++++++++++++++------------------
1 file changed, 17 insertions(+), 18 deletions(-)
diff --git a/mm/slub.c b/mm/slub.c
index 145a32c334cb..ff69988204c1 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -5512,6 +5512,19 @@ void *__kmalloc_large_node_noprof(size_t size, gfp_t flags, int node)
}
EXPORT_SYMBOL(__kmalloc_large_node_noprof);
+static __always_inline
+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 +5545,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 +5682,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
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family
2026-12-03 13:42 [PATCH v2 0/3] kmem_buckets entry point rework Vlastimil Babka (SUSE)
2026-12-03 13:42 ` [PATCH v2 1/3] mm/slab: add ___do_kmalloc_node() to deduplicate code Vlastimil Babka (SUSE)
@ 2026-12-03 13:42 ` Vlastimil Babka (SUSE)
2026-12-03 14:42 ` sashiko-bot
2026-12-03 13:42 ` [PATCH v2 3/3] mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros Vlastimil Babka (SUSE)
2 siblings, 1 reply; 6+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-12-03 13:42 UTC (permalink / raw)
To: Harry Yoo
Cc: Chris Down, Kees Cook, Hao Li, Christoph Lameter, David Rientjes,
Roman Gushchin, linux-mm, linux-kernel, Vlastimil Babka (SUSE)
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 | 27 +++++++++++---
mm/slub.c | 101 +++++++++++++++++++++++++++++++++++++++++----------
2 files changed, 103 insertions(+), 25 deletions(-)
diff --git a/include/linux/slab.h b/include/linux/slab.h
index cda126def67a..d9592e700d03 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -1175,11 +1175,22 @@ 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)
-#define kmem_buckets_alloc(_b, _size, _flags) \
- alloc_hooks(__kmalloc_node_noprof(PASS_KMALLOC_PARAMS(_size, _b, __kmalloc_token(_size)), _flags, NUMA_NO_NODE))
+void *__kmem_buckets_alloc_noprof(kmem_buckets *b,
+ DECL_TOKEN_PARAMS(size, token), gfp_t flags)
+ __assume_kmalloc_alignment __alloc_size(2);
-#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_))
+void *__kmem_buckets_alloc_node_track_caller_noprof(kmem_buckets *b,
+ DECL_TOKEN_PARAMS(size, token), gfp_t flags, int node, unsigned long caller)
+ __assume_kmalloc_alignment __alloc_size(2);
+
+#define kmem_buckets_alloc(_b, _size, _flags) \
+ alloc_hooks(__kmem_buckets_alloc_noprof(_b, PASS_TOKEN_PARAMS(_size, \
+ __kmalloc_token(_size)), _flags))
+
+#define kmem_buckets_alloc_node_track_caller(_b, _size, _flags, _node) \
+ alloc_hooks(__kmem_buckets_alloc_node_track_caller_noprof(_b, \
+ PASS_TOKEN_PARAMS(_size, __kmalloc_token(_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 +1371,12 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node);
#define kvzalloc_node(_size, _flags, _node) kvmalloc_node(_size, (_flags)|__GFP_ZERO, _node)
-#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))
+void *__kmem_buckets_valloc_noprof(kmem_buckets *b, DECL_TOKEN_PARAMS(size, token),
+ unsigned long align, gfp_t flags, int node) __alloc_size(2);
+
+#define kmem_buckets_valloc(_b, _size, _flags) \
+ alloc_hooks(__kmem_buckets_valloc_noprof(_b, PASS_TOKEN_PARAMS(_size, \
+ __kmalloc_token(_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 ff69988204c1..17199fef1a2d 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -5728,6 +5728,35 @@ void *__kmalloc_flags_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags,
}
}
+void *__kmem_buckets_alloc_noprof(kmem_buckets *b, DECL_TOKEN_PARAMS(size, token),
+ 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, PASS_TOKEN_PARAM(token), &ac);
+}
+
+void *__kmem_buckets_alloc_node_track_caller_noprof(kmem_buckets *b,
+ DECL_TOKEN_PARAMS(size, token), 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, PASS_TOKEN_PARAM(token), &ac);
+}
static noinline void free_to_partial_list(
struct kmem_cache *s, struct slab *slab,
@@ -7102,26 +7131,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)) {
@@ -7147,10 +7160,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, DECL_TOKEN_PARAMS(size, 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,
+ };
+
+ 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, PASS_TOKEN_PARAM(token), &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
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 3/3] mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros
2026-12-03 13:42 [PATCH v2 0/3] kmem_buckets entry point rework Vlastimil Babka (SUSE)
2026-12-03 13:42 ` [PATCH v2 1/3] mm/slab: add ___do_kmalloc_node() to deduplicate code Vlastimil Babka (SUSE)
2026-12-03 13:42 ` [PATCH v2 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family Vlastimil Babka (SUSE)
@ 2026-12-03 13:42 ` Vlastimil Babka (SUSE)
2026-12-03 14:42 ` sashiko-bot
2 siblings, 1 reply; 6+ messages in thread
From: Vlastimil Babka (SUSE) @ 2026-12-03 13:42 UTC (permalink / raw)
To: Harry Yoo
Cc: Chris Down, Kees Cook, Hao Li, Christoph Lameter, David Rientjes,
Roman Gushchin, linux-mm, linux-kernel, Vlastimil Babka (SUSE)
Now that kmem_buckets allocations have their own entry points, we can
drop the corresponding macros for kmalloc functions. BUCKET_PARAMS can
be dropped completely and KMALLOC_PARAMS users can use TOKEN_PARAMS for
handling just the randomization tokens.
Passing hardcoded NULL kmem_buckets pointer to __do_kmalloc_node() is
cheap because it (and everything that cares about the pointer) is
inlined.
This has a minor reduction effect on kmalloc_node() family of callers
not passing the NULL buckets pointer anymore with CONFIG_SLAB_BUCKETS=y,
as expected.
add/remove: 1/1 grow/shrink: 56/214 up/down: 1413/-1734 (-321)
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
include/linux/slab.h | 37 ++++++++-----------------------------
mm/slub.c | 19 ++++++++-----------
2 files changed, 16 insertions(+), 40 deletions(-)
diff --git a/include/linux/slab.h b/include/linux/slab.h
index d9592e700d03..8655778830ad 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -933,27 +933,6 @@ void *kmem_cache_alloc_from_sheaf_noprof(struct kmem_cache *cachep, gfp_t gfp,
unsigned int kmem_cache_sheaf_size(struct slab_sheaf *sheaf);
-/*
- * These macros allow declaring a kmem_buckets * parameter alongside size, which
- * can be compiled out with CONFIG_SLAB_BUCKETS=n so that a large number of call
- * sites don't have to pass NULL.
- */
-#ifdef CONFIG_SLAB_BUCKETS
-#define DECL_BUCKET_PARAMS(_size, _b) size_t (_size), kmem_buckets *(_b)
-#define PASS_BUCKET_PARAMS(_size, _b) (_size), (_b)
-#define PASS_BUCKET_PARAM(_b) (_b)
-#else
-#define DECL_BUCKET_PARAMS(_size, _b) size_t (_size)
-#define PASS_BUCKET_PARAMS(_size, _b) (_size)
-#define PASS_BUCKET_PARAM(_b) NULL
-#endif
-
-#define DECL_KMALLOC_PARAMS(_size, _b, _token) DECL_BUCKET_PARAMS(_size, _b) \
- DECL_TOKEN_PARAM(_token)
-
-#define PASS_KMALLOC_PARAMS(_size, _b, _token) PASS_BUCKET_PARAMS(_size, _b) \
- _PASS_TOKEN_PARAM(_token)
-
/*
* The following functions are not to be used directly and are intended only
* for internal use from kmalloc() and kmalloc_node()
@@ -963,7 +942,7 @@ unsigned int kmem_cache_sheaf_size(struct slab_sheaf *sheaf);
void *__kmalloc_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags)
__assume_kmalloc_alignment __alloc_size(1);
-void *__kmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags, int node)
+void *__kmalloc_node_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags, int node)
__assume_kmalloc_alignment __alloc_size(1);
void *__kmalloc_cache_noprof(struct kmem_cache *s, gfp_t flags, size_t size)
@@ -1208,7 +1187,7 @@ static __always_inline __alloc_size(1) void *_kmalloc_node_noprof(size_t size, g
kmalloc_caches[kmalloc_type(flags, token)][index],
flags, node, size);
}
- return __kmalloc_node_noprof(PASS_KMALLOC_PARAMS(size, NULL, token), flags, node);
+ return __kmalloc_node_noprof(PASS_TOKEN_PARAMS(size, token), flags, node);
}
#define kmalloc_node_noprof(...) _kmalloc_node_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
#define kmalloc_node(...) alloc_hooks(kmalloc_node_noprof(__VA_ARGS__))
@@ -1276,10 +1255,10 @@ void *krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t flags);
*/
#define kcalloc(n, size, flags) kmalloc_array(n, size, (flags) | __GFP_ZERO)
-void *__kmalloc_node_track_caller_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags, int node,
+void *__kmalloc_node_track_caller_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags, int node,
unsigned long caller) __alloc_size(1);
#define kmalloc_node_track_caller_noprof(size, flags, node, caller) \
- __kmalloc_node_track_caller_noprof(PASS_KMALLOC_PARAMS(size, NULL, __kmalloc_token(size)), flags, node, caller)
+ __kmalloc_node_track_caller_noprof(PASS_TOKEN_PARAMS(size, __kmalloc_token(size)), flags, node, caller)
#define kmalloc_node_track_caller(...) \
alloc_hooks(kmalloc_node_track_caller_noprof(__VA_ARGS__, _RET_IP_))
@@ -1305,7 +1284,7 @@ static inline __alloc_size(1, 2) void *_kmalloc_array_node_noprof(size_t n, size
return NULL;
if (__builtin_constant_p(n) && __builtin_constant_p(size))
return _kmalloc_node_noprof(bytes, flags, node, token);
- return __kmalloc_node_noprof(PASS_KMALLOC_PARAMS(bytes, NULL, token), flags, node);
+ return __kmalloc_node_noprof(PASS_TOKEN_PARAMS(bytes, token), flags, node);
}
#define kmalloc_array_node_noprof(...) _kmalloc_array_node_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
#define kmalloc_array_node(...) alloc_hooks(kmalloc_array_node_noprof(__VA_ARGS__))
@@ -1334,10 +1313,10 @@ void *kzalloc(size_t size, gfp_t flags);
#define kzalloc(size, flags) alloc_hooks(kzalloc_noprof(size, flags))
#define kzalloc_node(_size, _flags, _node) kmalloc_node(_size, (_flags)|__GFP_ZERO, _node)
-void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long align,
+void *__kvmalloc_node_noprof(DECL_TOKEN_PARAMS(size, token), unsigned long align,
gfp_t flags, int node) __alloc_size(1);
#define kvmalloc_node_align_noprof(_size, _align, _flags, _node) \
- __kvmalloc_node_noprof(PASS_KMALLOC_PARAMS(_size, NULL, __kmalloc_token(_size)), _align, _flags, _node)
+ __kvmalloc_node_noprof(PASS_TOKEN_PARAMS(_size, __kmalloc_token(_size)), _align, _flags, _node)
#define kvmalloc_node_align(...) \
alloc_hooks(kvmalloc_node_align_noprof(__VA_ARGS__))
#if 0 /* kernel-doc */
@@ -1386,7 +1365,7 @@ _kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node, kmallo
if (unlikely(check_mul_overflow(n, size, &bytes)))
return NULL;
- return __kvmalloc_node_noprof(PASS_KMALLOC_PARAMS(bytes, NULL, token), 1, flags, node);
+ return __kvmalloc_node_noprof(PASS_TOKEN_PARAMS(bytes, token), 1, flags, node);
}
#define kvmalloc_array_node_noprof(...) _kvmalloc_array_node_noprof(__VA_ARGS__, __kmalloc_token(__VA_ARGS__))
#define kvmalloc_array_noprof(...) kvmalloc_array_node_noprof(__VA_ARGS__, NUMA_NO_NODE)
diff --git a/mm/slub.c b/mm/slub.c
index 17199fef1a2d..bffb113ae3b0 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -5548,7 +5548,7 @@ void *__do_kmalloc_node(kmem_buckets *b, gfp_t flags, int node,
return ___do_kmalloc_node(s, flags, node, ac);
}
-void *__kmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags, int node)
+void *__kmalloc_node_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags, int node)
{
const struct slab_alloc_context ac = {
.caller_addr = _RET_IP_,
@@ -5556,8 +5556,7 @@ void *__kmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags, in
.alloc_flags = SLAB_ALLOC_DEFAULT,
};
- return __do_kmalloc_node(PASS_BUCKET_PARAM(b), flags, node,
- PASS_TOKEN_PARAM(token), &ac);
+ return __do_kmalloc_node(NULL, flags, node, PASS_TOKEN_PARAM(token), &ac);
}
EXPORT_SYMBOL(__kmalloc_node_noprof);
@@ -5666,7 +5665,7 @@ void *_kmalloc_nolock_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t gfp_flags, in
}
EXPORT_SYMBOL_GPL(_kmalloc_nolock_noprof);
-void *__kmalloc_node_track_caller_noprof(DECL_KMALLOC_PARAMS(size, b, token), gfp_t flags,
+void *__kmalloc_node_track_caller_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t flags,
int node, unsigned long caller)
{
const struct slab_alloc_context ac = {
@@ -5675,8 +5674,7 @@ void *__kmalloc_node_track_caller_noprof(DECL_KMALLOC_PARAMS(size, b, token), gf
.alloc_flags = SLAB_ALLOC_DEFAULT,
};
- return __do_kmalloc_node(PASS_BUCKET_PARAM(b), flags, node,
- PASS_TOKEN_PARAM(token), &ac);
+ return __do_kmalloc_node(NULL, flags, node, PASS_TOKEN_PARAM(token), &ac);
}
EXPORT_SYMBOL(__kmalloc_node_track_caller_noprof);
@@ -7078,7 +7076,7 @@ __do_krealloc(const void *p, size_t new_size, unsigned long align, gfp_t flags,
return (void *)p;
alloc_new:
- ret = __kmalloc_node_track_caller_noprof(PASS_KMALLOC_PARAMS(new_size, NULL, token), flags, nid, _RET_IP_);
+ ret = __kmalloc_node_track_caller_noprof(PASS_TOKEN_PARAMS(new_size, token), flags, nid, _RET_IP_);
if (ret && p) {
/* Disable KASAN checks as the object's redzone is accessed. */
kasan_disable_current();
@@ -7163,7 +7161,7 @@ static void *__kvmalloc_fallback(size_t size, unsigned long align, gfp_t flags,
node, caller);
}
-void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long align,
+void *__kvmalloc_node_noprof(DECL_TOKEN_PARAMS(size, token), unsigned long align,
gfp_t flags, int node)
{
void *ret;
@@ -7177,8 +7175,7 @@ void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long
* 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),
+ ret = __do_kmalloc_node(NULL, kmalloc_gfp_adjust(flags, size),
node, PASS_TOKEN_PARAM(token), &ac);
if (ret || size <= PAGE_SIZE)
return ret;
@@ -7278,7 +7275,7 @@ void *kvrealloc_node_align_noprof(const void *p, DECL_TOKEN_PARAMS(size, token),
n = krealloc_node_align_noprof(p, PASS_TOKEN_PARAMS(size, token), align, kmalloc_gfp_adjust(flags, size), nid);
if (!n) {
/* We failed to krealloc(), fall back to kvmalloc(). */
- n = __kvmalloc_node_noprof(PASS_KMALLOC_PARAMS(size, NULL, token), align, flags, nid);
+ n = __kvmalloc_node_noprof(PASS_TOKEN_PARAMS(size, token), align, flags, nid);
if (!n)
return NULL;
--
2.56.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family
2026-12-03 13:42 ` [PATCH v2 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family Vlastimil Babka (SUSE)
@ 2026-12-03 14:42 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-03 14:42 UTC (permalink / raw)
To: Vlastimil Babka (SUSE), Harry Yoo
Cc: Chris Down, Kees Cook, Hao Li, Christoph Lameter, David Rientjes,
Roman Gushchin, linux-mm
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mm: missing EXPORT_SYMBOL for new kmem_buckets allocation
entry points
- [Medium] mm: __assume_kmalloc_alignment on kmem_buckets allocators
exceeds cache align
--
commit 090eb1fb34ca44eca95fa86e7bfc2e6da9eb1e43
Author: Vlastimil Babka (SUSE) <vbabka@kernel.org>
mm/slab: add own entry points for kmem_buckets_alloc() family
This commit adds dedicated __kmem_buckets_alloc_noprof(),
__kmem_buckets_alloc_node_track_caller_noprof(), and
__kmem_buckets_valloc_noprof() entry points for the kmem_buckets
allocation macros instead of routing them through the kmalloc entry
points. It also factors out __kvmalloc_fallback() to share the vmalloc
fallback path with __kvmalloc_node_noprof().
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index cda126def67af..d9592e700d03f 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -1175,11 +1175,22 @@ 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)
>
> -#define kmem_buckets_alloc(_b, _size, _flags) \
> - alloc_hooks(__kmalloc_node_noprof(PASS_KMALLOC_PARAMS(_size, _b, __kmalloc_token(_size)), _flags, NUMA_NO_NODE))
> +void *__kmem_buckets_alloc_noprof(kmem_buckets *b,
> + DECL_TOKEN_PARAMS(size, token), gfp_t flags)
> + __assume_kmalloc_alignment __alloc_size(2);
>
> -#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_))
> +void *__kmem_buckets_alloc_node_track_caller_noprof(kmem_buckets *b,
> + DECL_TOKEN_PARAMS(size, token), gfp_t flags, int node, unsigned long caller)
> + __assume_kmalloc_alignment __alloc_size(2);
[Severity: Medium]
Do caches created by kmem_buckets_create() actually guarantee
__assume_kmalloc_alignment?
kmem_buckets_create() passes align = 0 to kmem_cache_create_usercopy(),
so calculate_alignment() only clamps s->align to arch_slab_minalign().
When ARCH_KMALLOC_MINALIGN exceeds ARCH_SLAB_MINALIGN (e.g., ARM or
PowerPC), calculate_sizes() aligns s->red_left_pad and the object stride
only to s->align, so returned pointers can violate ARCH_KMALLOC_MINALIGN
under slab debug, KASAN, or a ctor.
Previously __kmalloc_node_track_caller_noprof() lacked this attribute, so
this adds the false assumption to the track_caller path, risking
miscompilation or alignment faults.
Should these prototypes use __assume_slab_alignment instead, or should
kmem_buckets_create() enforce ARCH_KMALLOC_MINALIGN?
[ ... ]
> diff --git a/mm/slub.c b/mm/slub.c
> index ff69988204c13..17199fef1a2d1 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
[ ... ]
> EXPORT_SYMBOL(__kvmalloc_node_noprof);
>
> +void *__kmem_buckets_valloc_noprof(kmem_buckets *b, DECL_TOKEN_PARAMS(size, 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,
> + };
> +
> + 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, PASS_TOKEN_PARAM(token), &ac);
> + if (ret || size <= PAGE_SIZE)
> + return ret;
> +
> + return __kvmalloc_fallback(size, align, flags, node,
> + __builtin_return_address(0));
> +}
[Severity: Medium]
As reported by Hao Li on v1 [1], should __kmem_buckets_alloc_noprof(),
__kmem_buckets_alloc_node_track_caller_noprof(), and
__kmem_buckets_valloc_noprof() be exported with EXPORT_SYMBOL()?
kmem_buckets_create() is exported via EXPORT_SYMBOL(), and these macros
previously expanded to exported kmalloc/kvmalloc symbols. Without
exporting the new entry points (or unexporting kmem_buckets_create()),
modules can create buckets but will fail to link when allocating from them.
[1] https://lore.kernel.org/r/aw1ciiXYI06gegvd@fedora
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-kmalloc-entry-rework-v2-0-d718dc4fd906@kernel.org?part=2
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 3/3] mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros
2026-12-03 13:42 ` [PATCH v2 3/3] mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros Vlastimil Babka (SUSE)
@ 2026-12-03 14:42 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-12-03 14:42 UTC (permalink / raw)
To: Vlastimil Babka (SUSE), Harry Yoo
Cc: Chris Down, Kees Cook, Hao Li, Christoph Lameter, David Rientjes,
Roman Gushchin, linux-mm
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] mm/slab: leave unused DECL_TOKEN_PARAM and _PASS_TOKEN_PARAM
macros
- [Low] mm/slab: leave stale DECL_BUCKET_PARAMS entries in kdoc and
tests
--
commit 9da411099f8a63bf0f793cbe5e608f3a522dd3a7
Author: Vlastimil Babka (SUSE) <vbabka@kernel.org>
mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros
This commit removes the BUCKET_PARAMS and KMALLOC_PARAMS macros and
switches the remaining kmalloc_node() and kvmalloc_node() entry points to
use TOKEN_PARAMS with a hardcoded NULL kmem_buckets pointer.
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index d9592e700d03f..8655778830adb 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -933,27 +933,6 @@ void *kmem_cache_alloc_from_sheaf_noprof(struct kmem_cache *cachep, gfp_t gfp,
>
> unsigned int kmem_cache_sheaf_size(struct slab_sheaf *sheaf);
>
> -/*
> - * These macros allow declaring a kmem_buckets * parameter alongside size, which
> - * can be compiled out with CONFIG_SLAB_BUCKETS=n so that a large number of call
> - * sites don't have to pass NULL.
> - */
> -#ifdef CONFIG_SLAB_BUCKETS
> -#define DECL_BUCKET_PARAMS(_size, _b) size_t (_size), kmem_buckets *(_b)
> -#define PASS_BUCKET_PARAMS(_size, _b) (_size), (_b)
> -#define PASS_BUCKET_PARAM(_b) (_b)
> -#else
> -#define DECL_BUCKET_PARAMS(_size, _b) size_t (_size)
> -#define PASS_BUCKET_PARAMS(_size, _b) (_size)
> -#define PASS_BUCKET_PARAM(_b) NULL
> -#endif
[Severity: Low]
As reported by Hao Li on v1 [1], should the leftover DECL_BUCKET_PARAMS
entries also be removed from CTransforms.function_xforms in
tools/lib/python/kdoc/xforms_lists.py and
TestSubWithLocalXforms.function_xforms in
tools/unittests/test_cmatch.py? Neither file has any remaining users of
the deleted macro.
> -
> -#define DECL_KMALLOC_PARAMS(_size, _b, _token) DECL_BUCKET_PARAMS(_size, _b) \
> - DECL_TOKEN_PARAM(_token)
> -
> -#define PASS_KMALLOC_PARAMS(_size, _b, _token) PASS_BUCKET_PARAMS(_size, _b) \
> - _PASS_TOKEN_PARAM(_token)
[Severity: Low]
Can DECL_TOKEN_PARAM and _PASS_TOKEN_PARAM also be removed from both
CONFIG_KMALLOC_PARTITION_CACHES branches in include/linux/slab.h? With
DECL_KMALLOC_PARAMS and PASS_KMALLOC_PARAMS gone, they have no remaining
users in the tree.
[1] https://lore.kernel.org/r/awzv3V3ioGi1SQN6@fedora
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-kmalloc-entry-rework-v2-0-d718dc4fd906@kernel.org?part=3
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-03 14:43 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 13:42 [PATCH v2 0/3] kmem_buckets entry point rework Vlastimil Babka (SUSE)
2026-12-03 13:42 ` [PATCH v2 1/3] mm/slab: add ___do_kmalloc_node() to deduplicate code Vlastimil Babka (SUSE)
2026-12-03 13:42 ` [PATCH v2 2/3] mm/slab: add own entry points for kmem_buckets_alloc() family Vlastimil Babka (SUSE)
2026-12-03 14:42 ` sashiko-bot
2026-12-03 13:42 ` [PATCH v2 3/3] mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros Vlastimil Babka (SUSE)
2026-12-03 14:42 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox