From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796404646; b=Av2Sz5kJAmIxYMiMbVvpMZV1vw8m45FA8lOAqTk5iHmKJXao7rd8utuM8Lbbp5kx5avd Tr+497WivNmlwwN2LZ2v2w/khB5qbxg/0pNybll4IQ2ipSVY0RNXMK2XlIk251sBIZqgs naS7NQ1yKBEWod/mUTfoOun7HCsR3MlZrfdMjfChVOILcJXT2Vdg9X4G/tTr5FmgcjfYZ 3AhCg0TDPqNxbnE5wsIq5jZ1KDf0p4w74a+640CGVNQrp447eojQLuRtMJSHfZgzwllT0 ARCzREIYWeCMxsVWR8XUBHly8X0wKSbefcXgChS8zs5iG0goFBAFoIyghWnMa2fCmIw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796404646; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:X-B4-Tracking:X-Change-ID:To:Cc: X-Mailer; bh=t39x0PhPigcfjQFHPYhjl85pot9XgmrTTsmbxc872P4=; b=Z8QhfCeH4Dv06DzBY4y6a+3s3iz8phmeqPP2DfrWT4mYbr2fsN1QXa5u/k+8uDtE9ZuG L2QLOm3uXWJ2HR97QTmqUYPbM4ZiBmWiymAkn4kLRI+mvkM4pqdNIxEAIIQmS8VZsFMpC y41PGLPNKtM0XLfoeYfQqzIHsJ0oFnLJbLSCU+oXxuTfqasmZ1UPAnOHeGMF7r5SSUfpx QiDYaRsBYnbhDvfdywb9dH9AXwFCWQyR98wuVNjTNQTYrTDsqR5krCeQppoSvjSvd058j NRyE/qIiABfLV/j/RONKWlSMnNuFQWjZ/kseVofoZJJKNlBNBpWjrtH78o7Ojs7W5wg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aUPfaHGB; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02654606A32C; Fri, 4 Dec 2026 17:17:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aUPfaHGB Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE9711F0093B; Fri, 4 Dec 2026 17:17:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796404645; bh=t39x0PhPigcfjQFHPYhjl85pot9XgmrTTsmbxc872P4=; h=From:Date:Subject:To:Cc; b=aUPfaHGBiyFKEIjCI5M4lhQxrUpJfCw/4olBzYMioscXi6yrncm0zjIFNcwLQlpFM WLvjOd8L+hUXdHlRb/mfpdkbaUmxD4djKkRbf0TSmzttQ6/WOYapAYoKZGqeZn0J77 /xFGf9V4a2/RYt1anTHPVOjhuzH+gCxgK3O9nUyKzOxOO7CR/fnwOnamm+BHLqfujf SjjFYNwzaM3HUZyj/37cfP+0b4ztJGfxSA1ztTDXNsZuqIAA0zP0uR2+f/wWgiaEAX xvZymD+lHjUPxVGIBkY4qee6UruYFe1pNT8+QxGhIMwLQjQ8Vc6Gr7cGb2qrMNMmm6 955pnm4lrZSOA== From: "Vlastimil Babka (SUSE)" Date: Fri, 04 Dec 2026 18:17:19 +0100 Subject: [PATCH] mm, slab: guarantee kmalloc alignment for kmem_buckets_alloc() X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-buckets-align-fix-v1-1-1526f39fde03@kernel.org> X-B4-Tracking: v=1; b=H4sIAJ71EmsC/yXMQQqDMBCF4avIrDsQo8nCq0gXOh11VKJktBQkd 29sl9+D91+gHIUVmuKCyG9R2UJG+SiApi6MjPLKBmusL62psT9p4UOxW2UMOMgHmdzgK0eVsQ7 yb4+c51+zff6tZz8zHXcIUvoC0sBoh3UAAAA= X-Change-ID: 20261204-buckets-align-fix-ec5f635c3025 To: Harry Yoo Cc: Hao Li , Kees Cook , Catalin Marinas , Chris Down , Koichiro Den , Christoph Lameter , David Rientjes , Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org, sashiko-bot@kernel.org, stable@vger.kernel.org, "Vlastimil Babka (SUSE)" X-Mailer: b4 0.16.0 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) --- 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