From: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>
To: Harry Yoo <harry@kernel.org>
Cc: Hao Li <hao.li@linux.dev>, Kees Cook <kees@kernel.org>,
Catalin Marinas <catalin.marinas@arm.com>,
Chris Down <chris@chrisdown.name>,
Koichiro Den <koichiro.den@gmail.com>,
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,
sashiko-bot@kernel.org, stable@vger.kernel.org,
"Vlastimil Babka (SUSE)" <vbabka@kernel.org>
Subject: [PATCH] mm, slab: guarantee kmalloc alignment for kmem_buckets_alloc()
Date: Fri, 04 Dec 2026 18:17:19 +0100 [thread overview]
Message-ID: <20261204-buckets-align-fix-v1-1-1526f39fde03@kernel.org> (raw)
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@kernel.org
Closes: https://lore.kernel.org/all/sashiko-outbox-247997@kernel.org/
Fixes: b32801d1255b ("mm/slab: Introduce kmem_buckets_create() and family")
Cc: stable@vger.kernel.org
Signed-off-by: Vlastimil Babka (SUSE) <vbabka@kernel.org>
---
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
reply other threads:[~2026-12-04 17:17 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261204-buckets-align-fix-v1-1-1526f39fde03@kernel.org \
--to=vbabka@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=chris@chrisdown.name \
--cc=cl@gentwo.org \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=kees@kernel.org \
--cc=koichiro.den@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=sashiko-bot@kernel.org \
--cc=stable@vger.kernel.org \
/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
all inboxes | Powered by JetHome®