Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21F6D547050; Thu, 17 Sep 2026 18:58:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789671511; cv=none; b=UePQmfnbEZrXyTsMoHLRYohac2lvfTiuyUmVyNKCWRWXg3MBhPCqU2HOCXYNaogtAX+hyxAe3THkCYqfpxfaFRR2jlvCPD4nNuzw69cDXljOe4KyozhKDPdAQsI2d2Dv7N6L2pbXwhz7B6KpUWx/1v1ikgw5ihyPoJRukEMF4ug= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789671511; c=relaxed/simple; bh=KeHuPjxSGDWA1aeWhnswrmwufHSnFa0AmMS3FUPZYQQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KgcJSKeqPTqF+u1kFcI+VFOg5hozs2T+098v8zjSLAWoYrs/wEyp0IwLdzV+Q3DzB9XfDrqrdnp+JFp9+pM0tXR2N/cDKF1urVzYeXqbq18pZlLIRxxn9zlDhNRg8l0GZ+22H68KVbXLJKgznQi9TRuSQiqF3HJZErZRPX6I0m4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=FfX7OTKX; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="FfX7OTKX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 706751F000FF; Thu, 17 Sep 2026 18:58:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1789671510; bh=OpA1whJoKcsJpUejhw0/GGMbqqlHn7gZCcVPqMwohI4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FfX7OTKXkJasOfCTcAt8GTGR6tC3G+N5yTfW4bJ6msHOUS6c9lupoLGSAOgJV8C9C V3yzAOBX8G/gfNkbGPqf8DaUgh+BE22jJ5YTtDGGDbBYToHRgbmntM0iequ+2po3rG vNTdj3yL8dtAaGc7AZ67MtLFicXyzjLa1l76+EpM= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Harry Yoo , Suren Baghdasaryan , "Liam R. Howlett" , Vlastimil Babka , Sasha Levin Subject: [PATCH 6.12 0999/1102] mm/slab: move and refactor __kmem_cache_alias() Date: Thu, 17 Sep 2026 16:15:42 +0100 Message-ID: <20260917151605.809517074@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260917151539.408551884@linuxfoundation.org> References: <20260917151539.408551884@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Vlastimil Babka [ Upstream commit 8598351edc42f38d2a1eaed9abca39c98e7b0bbf ] Move __kmem_cache_alias() to slab_common.c since it's called by __kmem_cache_create_args() and calls find_mergeable() that both are in this file. We can remove two slab.h declarations and make them static. Instead declare sysfs_slab_alias() from slub.c so that __kmem_cache_alias() can keep calling it. Add args parameter to __kmem_cache_alias() and find_mergeable() instead of align and ctor. With that we can also move the checks for usersize and sheaf_capacity there from __kmem_cache_create_args() and make the result more symmetric with slab_unmergeable(). No functional changes intended. Reviewed-by: Harry Yoo Reviewed-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett Signed-off-by: Vlastimil Babka Stable-dep-of: 7e98f8563956 ("mm/slub: fix missing debugfs entries for caches created before sysfs init") Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- mm/slab.h | 8 +++----- mm/slab_common.c | 41 +++++++++++++++++++++++++++++++++-------- mm/slub.c | 29 +---------------------------- 3 files changed, 37 insertions(+), 41 deletions(-) --- a/mm/slab.h +++ b/mm/slab.h @@ -319,9 +319,12 @@ struct kmem_cache { #define SLAB_SUPPORTS_SYSFS 1 void sysfs_slab_unlink(struct kmem_cache *s); void sysfs_slab_release(struct kmem_cache *s); +int sysfs_slab_alias(struct kmem_cache *s, const char *name); #else static inline void sysfs_slab_unlink(struct kmem_cache *s) { } static inline void sysfs_slab_release(struct kmem_cache *s) { } +static inline int sysfs_slab_alias(struct kmem_cache *s, const char *name) + { return 0; } #endif void *fixup_red_left(struct kmem_cache *s, void *p); @@ -442,11 +445,6 @@ extern void create_boot_cache(struct kme unsigned int useroffset, unsigned int usersize); int slab_unmergeable(struct kmem_cache *s); -struct kmem_cache *find_mergeable(unsigned size, unsigned align, - slab_flags_t flags, const char *name, void (*ctor)(void *)); -struct kmem_cache * -__kmem_cache_alias(const char *name, unsigned int size, unsigned int align, - slab_flags_t flags, void (*ctor)(void *)); slab_flags_t kmem_cache_flags(slab_flags_t flags, const char *name); --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -170,15 +170,19 @@ int slab_unmergeable(struct kmem_cache * return 0; } -struct kmem_cache *find_mergeable(unsigned int size, unsigned int align, - slab_flags_t flags, const char *name, void (*ctor)(void *)) +static struct kmem_cache *find_mergeable(unsigned int size, slab_flags_t flags, + const char *name, struct kmem_cache_args *args) { struct kmem_cache *s; + unsigned int align; if (slab_nomerge) return NULL; - if (ctor) + if (args->ctor) + return NULL; + + if (IS_ENABLED(CONFIG_HARDENED_USERCOPY) && args->usersize) return NULL; flags = kmem_cache_flags(flags, name); @@ -187,7 +191,7 @@ struct kmem_cache *find_mergeable(unsign return NULL; size = ALIGN(size, sizeof(void *)); - align = calculate_alignment(flags, align, size); + align = calculate_alignment(flags, args->align, size); size = ALIGN(size, align); list_for_each_entry_reverse(s, &slab_caches, list) { @@ -251,6 +255,30 @@ out: return ERR_PTR(err); } +static struct kmem_cache * +__kmem_cache_alias(const char *name, unsigned int size, slab_flags_t flags, + struct kmem_cache_args *args) +{ + struct kmem_cache *s; + + s = find_mergeable(size, flags, name, args); + if (s) { + if (sysfs_slab_alias(s, name)) + return NULL; + + s->refcount++; + + /* + * Adjust the object sizes so that we clear + * the complete object on kzalloc. + */ + s->object_size = max(s->object_size, size); + s->inuse = max(s->inuse, ALIGN(size, sizeof(void *))); + } + + return s; +} + /** * __kmem_cache_create_args - Create a kmem cache. * @name: A string which is used in /proc/slabinfo to identify this cache. @@ -317,9 +345,7 @@ struct kmem_cache *__kmem_cache_create_a object_size - args->usersize < args->useroffset)) args->usersize = args->useroffset = 0; - if (!args->usersize) - s = __kmem_cache_alias(name, object_size, args->align, flags, - args->ctor); + s = __kmem_cache_alias(name, object_size, flags, args); if (s) goto out_unlock; @@ -1350,4 +1376,3 @@ EXPORT_TRACEPOINT_SYMBOL(kmalloc); EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); EXPORT_TRACEPOINT_SYMBOL(kfree); EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free); - --- a/mm/slub.c +++ b/mm/slub.c @@ -333,11 +333,8 @@ enum track_item { TRACK_ALLOC, TRACK_FRE #ifdef SLAB_SUPPORTS_SYSFS static int sysfs_slab_add(struct kmem_cache *); -static int sysfs_slab_alias(struct kmem_cache *, const char *); #else static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; } -static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p) - { return 0; } #endif #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG) @@ -6061,30 +6058,6 @@ void __init kmem_cache_init_late(void) #endif } -struct kmem_cache * -__kmem_cache_alias(const char *name, unsigned int size, unsigned int align, - slab_flags_t flags, void (*ctor)(void *)) -{ - struct kmem_cache *s; - - s = find_mergeable(size, align, flags, name, ctor); - if (s) { - if (sysfs_slab_alias(s, name)) - return NULL; - - s->refcount++; - - /* - * Adjust the object sizes so that we clear - * the complete object on kzalloc. - */ - s->object_size = max(s->object_size, size); - s->inuse = max(s->inuse, ALIGN(size, sizeof(void *))); - } - - return s; -} - int do_kmem_cache_create(struct kmem_cache *s, const char *name, unsigned int size, struct kmem_cache_args *args, slab_flags_t flags) @@ -7250,7 +7223,7 @@ struct saved_alias { static struct saved_alias *alias_list; -static int sysfs_slab_alias(struct kmem_cache *s, const char *name) +int sysfs_slab_alias(struct kmem_cache *s, const char *name) { struct saved_alias *al;