kmem_buckets entry point rework
Subsystems (2)
Vlastimil Babka (SUSE)
maintainercover kmem_buckets entry point rework
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 that also makes 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. If that bet is wrong (I haven't measured it yet) we can add an inline there (it still removes some duplicated code) and pay the binary size of the new entry points, but it shouldn't compromise the whole idea.
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/[email protected]/
---
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 | 53 +++++++----------- mm/slub.c | 152 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 128 insertions(+), 77 deletions(-)
---
base-commit: 0b4d8fca3d41123c2419b8e489ea7dba34d5b7e0
change-id: 20261116-kmalloc-entry-rework-a0162912968b
Vlastimil Babka (SUSE)
maintainerPATCH 1/3 mm/slab: add ___do_kmalloc_node() to deduplicate code
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%
--- mm/slub.c | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-)
mm/slub.c +21 -18
@@ -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);
Signature · 1 line
2.56.1
Vlastimil Babka (SUSE)
maintainerPATCH 2/3 mm/slab: add own entry points for kmem_buckets_alloc() family
In reply to 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.
--- include/linux/slab.h | 16 +++++++-- mm/slub.c | 100 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 94 insertions(+), 22 deletions(-)
include/linux/slab.h +13 -3
@@ -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)
mm/slub.c +81 -19
123 lines not on this page · raw message
Signature · 1 line
2.56.1
Vlastimil Babka (SUSE)
maintainerPATCH 3/3 mm/slab: drop BUCKET_PARAMS and KMALLOC_PARAMS macros
In reply to 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: 51/217 up/down: 1316/-1798 (-482)
--- include/linux/slab.h | 37 ++++++++----------------------------- mm/slub.c | 19 ++++++++----------- 2 files changed, 16 insertions(+), 40 deletions(-)
include/linux/slab.h +8 -29
@@ -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)
@@ -1204,7 +1183,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__))
@@ -1272,10 +1251,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_))
@@ -1301,7 +1280,7 @@ static inline __alloc_size(1, 2) void *_kmalloc_array_node_noprof(size_t n, sizereturn 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__))
@@ -1330,10 +1309,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 */
@@ -1381,7 +1360,7 @@ _kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node, kmalloif (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)
mm/slub.c +8 -11
@@ -5552,7 +5552,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_,
@@ -5560,8 +5560,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);
@@ -5670,7 +5669,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 = {
@@ -5679,8 +5678,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);
@@ -7081,7 +7079,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 ();
@@ -7166,7 +7164,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;
@@ -7180,8 +7178,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;
@@ -7281,7 +7278,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 ;
Signature · 1 line
2.56.1