From: Chris Down <chris@chrisdown.name>
To: Vlastimil Babka <vbabka@kernel.org>, Harry Yoo <harry@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>
Cc: Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
David Rientjes <rientjes@google.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
Kees Cook <kees@kernel.org>, Marco Elver <elver@google.com>,
GONG Ruiqi <gongruiqi@huaweicloud.com>,
Jonathan Corbet <corbet@lwn.net>,
linux-mm@kvack.org, linux-hardening@vger.kernel.org,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
kernel-team@meta.com
Subject: [PATCH v2 5/6] mm/slab: Support disabling KMALLOC_PARTITION_CACHES at boot
Date: Thu, 29 Oct 2026 14:09:09 +0800 [thread overview]
Message-ID: <617bbfdce4fb2689c2510f0cd7f9adbb3ab8edfa.1793253633.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1793253633.git.chris@chrisdown.name>
KMALLOC_PARTITION_CACHES creates several copies of each normal kmalloc
cache and spreads allocations across them. That costs a fair bit of
memory: Harry measured about 18 MiB more slab memory after boot on a 64
CPU, 64 GiB VM, mostly from under-used slabs [1]. Like SLAB_BUCKETS, it
can only be chosen at build time.
Add a kmalloc_partition= early parameter, enabled by default on kernels
built with CONFIG_KMALLOC_PARTITION_CACHES=y. When it's off, the copies
alias the normal caches rather than being created.
Let's leave kmalloc_type() unchanged since each index simply resolves to
the same cache when partitioning is off.
Link: https://lore.kernel.org/r/179312451613.474906.1475329637924619088@kernel.org [1]
Signed-off-by: Chris Down <chris@chrisdown.name>
---
.../admin-guide/kernel-parameters.txt | 7 +++++++
mm/Kconfig | 5 +++++
mm/slab_common.c | 19 ++++++++++++++++++-
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index de2fd6963a16..6633a553a79c 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -3054,6 +3054,13 @@ Kernel parameters
Configure the RouterBoard 532 series on-chip
Ethernet adapter MAC address.
+ kmalloc_partition= [MM,EARLY]
+ Format: <bool>
+ Enable or disable kmalloc cache partitioning on
+ kernels built with CONFIG_KMALLOC_PARTITION_CACHES=y.
+ Ignored without that option.
+ Default: on.
+
kmemleak= [KNL,EARLY] Boot-time kmemleak enable/disable
Valid arguments: on, off
Default: on
diff --git a/mm/Kconfig b/mm/Kconfig
index 48078c1fd26f..b90b3915fcd5 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -266,6 +266,11 @@ config KMALLOC_PARTITION_CACHES
limited degree of memory and CPU overhead that relates to hardware
and system workload.
+ The partitioning can be disabled at boot with the
+ "kmalloc_partition=0" kernel parameter, in which case the copies
+ are not created and all normal kmalloc allocations use the
+ regular kmalloc caches.
+
choice
prompt "Partitioned slab cache mode"
depends on KMALLOC_PARTITION_CACHES
diff --git a/mm/slab_common.c b/mm/slab_common.c
index 9fe48da12010..5391149a2eed 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -781,6 +781,16 @@ unsigned long random_kmalloc_seed __ro_after_init;
EXPORT_SYMBOL(random_kmalloc_seed);
#endif
+#ifdef CONFIG_KMALLOC_PARTITION_CACHES
+static bool kmalloc_partition_enabled __ro_after_init = true;
+
+static int __init setup_kmalloc_partition(char *str)
+{
+ return kstrtobool(str, &kmalloc_partition_enabled);
+}
+early_param("kmalloc_partition", setup_kmalloc_partition);
+#endif
+
/*
* Conversion table for small slabs sizes / 8 to the index in the
* kmalloc array. This is necessary for slabs < 192 since we have non power
@@ -1013,8 +1023,15 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type)
}
#ifdef CONFIG_KMALLOC_PARTITION_CACHES
- if (type >= KMALLOC_PARTITION_START && type <= KMALLOC_PARTITION_END)
+ if (!kmalloc_partition_enabled) {
+ /* Use the normal caches, which have already been created. */
+ if (type > KMALLOC_PARTITION_START && type <= KMALLOC_PARTITION_END) {
+ kmalloc_caches[type][idx] = kmalloc_caches[KMALLOC_NORMAL][idx];
+ return;
+ }
+ } else if (type >= KMALLOC_PARTITION_START && type <= KMALLOC_PARTITION_END) {
flags |= SLAB_NO_MERGE;
+ }
#endif
/*
--
2.54.0
next prev parent reply other threads:[~2026-10-29 6:09 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 6:07 [PATCH v2 0/6] mm/slab: Support selecting slab hardening features " Chris Down
2026-10-29 6:07 ` [PATCH v2 1/6] mm/slab: Enable SLAB_FREELIST_HARDENED by default Chris Down
2026-10-29 6:07 ` [PATCH v2 2/6] mm/slab: Enable SLAB_FREELIST_RANDOM " Chris Down
2026-10-29 6:08 ` [PATCH v2 3/6] mm/slab: Support disabling SLAB_BUCKETS at boot Chris Down
2026-10-29 6:08 ` [PATCH v2 4/6] slub_kunit: Add a test for the slab_buckets boot parameter Chris Down
2026-10-29 6:09 ` Chris Down [this message]
2026-10-29 6:09 ` [PATCH v2 6/6] slub_kunit: Add a test for the kmalloc_partition " Chris Down
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=617bbfdce4fb2689c2510f0cd7f9adbb3ab8edfa.1793253633.git.chris@chrisdown.name \
--to=chris@chrisdown.name \
--cc=akpm@linux-foundation.org \
--cc=cl@gentwo.org \
--cc=corbet@lwn.net \
--cc=elver@google.com \
--cc=gongruiqi@huaweicloud.com \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=kees@kernel.org \
--cc=kernel-team@meta.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=vbabka@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®