[PATCH] mm, slab: guarantee kmalloc alignment for kmem_buckets_alloc()
From: Vlastimil Babka (SUSE)
Date: Fri Dec 04 2026 - 12:17:29 EST
Sashiko report for my patch suggested that it broke
__assume_kmalloc_alignment for kmem_buckets_alloc_node_track_caller().
However it seems to me that's already the case for kmem_buckets_alloc()
which does have this annotation but indeed the buckets are created with
no special alignment.
Since kmem_buckets work like a private kmalloc() array of caches, it
makes sense that they provide the same alignment guarantees, which
actually goes beyond ARCH_KMALLOC_MINALIGN guaranteed by
__assume_kmalloc_alignment.
Thus ensure same guarantees are provided by extracting the code from
create_boot_cache() to kmalloc_cache_align() and have
kmem_buckets_create() use it, with bit of a cleanup and comment
clarifying that __kmalloc_minalign() is observed too (I initially
thought it wasn't either).
As a side-effect, the non-kmalloc boot caches (kmem_cache and
kmem_cache_node) will now be aligned only to arch_slab_minalign()
instead of ARCH_KMALLOC_MINALIGN, but that's sufficient for those.
Reported-by: sashiko-bot@xxxxxxxxxx
Closes: https://lore.kernel.org/all/sashiko-outbox-247997@xxxxxxxxxx/
Fixes: b32801d1255b ("mm/slab: Introduce kmem_buckets_create() and family")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@xxxxxxxxxx>
---
Based on/intended for slab/for-7.5/misc. Not urgent for rc hotfix.
---
mm/slab_common.c | 39 ++++++++++++++++++++++++++++-----------
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/mm/slab_common.c b/mm/slab_common.c
index 71ac646349e3..41d0edd0285c 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -149,6 +149,17 @@ static unsigned int calculate_alignment(slab_flags_t flags,
return ALIGN(align, sizeof(void *));
}
+/*
+ * kmalloc caches guarantee alignment of at least the largest power-of-two
+ * divisor of the size. For power-of-two sizes, it is the size itself.
+ */
+static inline unsigned int kmalloc_cache_align(unsigned int size)
+{
+ unsigned int align = ARCH_KMALLOC_MINALIGN;
+
+ return max(align, 1U << (ffs(size) - 1));
+}
+
/*
* Find a mergeable slab cache
*/
@@ -461,15 +472,19 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
char *short_size, *cache_name;
unsigned int cache_useroffset, cache_usersize;
unsigned int size, aligned_idx;
+ struct kmem_cache *ref_kmalloc_cache;
- if (!kmalloc_caches[KMALLOC_NORMAL][idx])
+ ref_kmalloc_cache = kmalloc_caches[KMALLOC_NORMAL][idx];
+
+ if (!ref_kmalloc_cache)
continue;
- size = kmalloc_caches[KMALLOC_NORMAL][idx]->object_size;
+ /* Includes possible size adjustment due to __kmalloc_minalign() */
+ size = ref_kmalloc_cache->object_size;
if (!size)
continue;
- short_size = strchr(kmalloc_caches[KMALLOC_NORMAL][idx]->name, '-');
+ short_size = strchr(ref_kmalloc_cache->name, '-');
if (WARN_ON(!short_size))
goto fail;
@@ -483,11 +498,18 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
aligned_idx = __kmalloc_index(size, false);
if (!(*b)[aligned_idx]) {
+ /*
+ * buckets should provide the same alignment guarantees
+ * as kmalloc to prevent surprises when converting
+ * kmalloc users to it
+ */
+ unsigned int align = kmalloc_cache_align(size);
+
cache_name = kasprintf(GFP_KERNEL, "%s-%s", name, short_size + 1);
if (WARN_ON(!cache_name))
goto fail;
(*b)[aligned_idx] = kmem_cache_create_usercopy(cache_name, size,
- 0, flags, cache_useroffset,
+ align, flags, cache_useroffset,
cache_usersize, ctor);
kfree(cache_name);
if (WARN_ON(!(*b)[aligned_idx]))
@@ -713,16 +735,11 @@ void __init create_boot_cache(struct kmem_cache *s, const char *name,
unsigned int useroffset, unsigned int usersize)
{
int err;
- unsigned int align = ARCH_KMALLOC_MINALIGN;
+ unsigned int align = 0;
struct kmem_cache_args kmem_args = {};
- /*
- * kmalloc caches guarantee alignment of at least the largest
- * power-of-two divisor of the size. For power-of-two sizes,
- * it is the size itself.
- */
if (flags & SLAB_KMALLOC)
- align = max(align, 1U << (ffs(size) - 1));
+ align = kmalloc_cache_align(size);
kmem_args.align = calculate_alignment(flags, align, size);
#ifdef CONFIG_HARDENED_USERCOPY
---
base-commit: 152bb61a6134d1d2b736cead67f653ce57ba559b
change-id: 20261204-buckets-align-fix-ec5f635c3025