[PATCH 8/8] slub_kunit: Add a test for the kmalloc_partition boot parameter
From: Chris Down
Date: Tue Oct 27 2026 - 03:38:45 EST
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
lib/tests/slub_kunit.c | 46 ++++++++++++++++++++++++++++++++++++++++++
mm/slab.h | 4 ++++
mm/slab_common.c | 3 ++-
3 files changed, 52 insertions(+), 1 deletion(-)
diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c
index 646596663b33..0fe10cbfc83d 100644
--- a/lib/tests/slub_kunit.c
+++ b/lib/tests/slub_kunit.c
@@ -430,6 +430,49 @@ static void test_slab_buckets(struct kunit *test)
}
#endif
+#ifdef CONFIG_KMALLOC_PARTITION_CACHES
+/*
+ * Each partition copy is a separate cache, unless the copies were disabled
+ * with kmalloc_partition=0: then they alias the KMALLOC_NORMAL caches.
+ */
+static void test_kmalloc_partition(struct kunit *test)
+{
+ enum kmalloc_cache_type type;
+ struct kmem_cache *s;
+ unsigned int idx;
+ bool found;
+ void *p;
+
+ for (type = KMALLOC_PARTITION_START + 1; type <= KMALLOC_PARTITION_END; type++) {
+ for (idx = 0; idx < ARRAY_SIZE(kmalloc_caches[type]); idx++) {
+ struct kmem_cache *normal = kmalloc_caches[KMALLOC_NORMAL][idx];
+ struct kmem_cache *part = kmalloc_caches[type][idx];
+
+ if (!kmalloc_partition_enabled) {
+ KUNIT_EXPECT_PTR_EQ(test, part, normal);
+ } else if (normal) {
+ KUNIT_EXPECT_NOT_NULL(test, part);
+ KUNIT_EXPECT_PTR_NE(test, part, normal);
+ }
+ }
+ }
+
+ p = kmalloc(64, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ s = virt_to_slab(p)->slab_cache;
+ idx = kmalloc_index(64);
+ if (!kmalloc_partition_enabled) {
+ KUNIT_EXPECT_PTR_EQ(test, s, kmalloc_caches[KMALLOC_NORMAL][idx]);
+ } else {
+ found = false;
+ for (type = KMALLOC_PARTITION_START; type <= KMALLOC_PARTITION_END; type++)
+ found |= s == kmalloc_caches[type][idx];
+ KUNIT_EXPECT_TRUE_MSG(test, found, "allocated from %s", s->name);
+ }
+ kfree(p);
+}
+#endif
+
#if defined(CONFIG_PERF_EVENTS) || (defined(CONFIG_KPROBES) && defined(CONFIG_SMP))
#define NR_ITERATIONS 1000
#define NR_OBJECTS 1000
@@ -628,6 +671,9 @@ static struct kunit_case test_cases[] = {
#ifdef CONFIG_SLAB_BUCKETS
KUNIT_CASE(test_slab_buckets),
#endif
+#ifdef CONFIG_KMALLOC_PARTITION_CACHES
+ KUNIT_CASE(test_kmalloc_partition),
+#endif
#ifdef CONFIG_PERF_EVENTS
KUNIT_CASE_SLOW(test_kmalloc_nolock_and_friends_perf),
#endif
diff --git a/mm/slab.h b/mm/slab.h
index 185df5e68419..934b9db1ef6b 100644
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -394,6 +394,10 @@ extern struct kmem_cache *kmem_cache;
extern bool slab_buckets_enabled;
#endif
+#if IS_ENABLED(CONFIG_KUNIT) && defined(CONFIG_KMALLOC_PARTITION_CACHES)
+extern bool kmalloc_partition_enabled;
+#endif
+
/* A table of kmalloc cache names and sizes */
extern const struct kmalloc_info_struct {
const char *name[NR_KMALLOC_TYPES];
diff --git a/mm/slab_common.c b/mm/slab_common.c
index 5391149a2eed..406e8688ee0e 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -782,7 +782,8 @@ EXPORT_SYMBOL(random_kmalloc_seed);
#endif
#ifdef CONFIG_KMALLOC_PARTITION_CACHES
-static bool kmalloc_partition_enabled __ro_after_init = true;
+VISIBLE_IF_KUNIT bool kmalloc_partition_enabled __ro_after_init = true;
+EXPORT_SYMBOL_IF_KUNIT(kmalloc_partition_enabled);
static int __init setup_kmalloc_partition(char *str)
{
--
2.54.0