[PATCH 6/8] slub_kunit: Add a test for the slab_buckets boot parameter

From: Chris Down

Date: Tue Oct 27 2026 - 03:42:27 EST


Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
lib/tests/slub_kunit.c | 37 +++++++++++++++++++++++++++++++++++++
mm/slab.h | 4 ++++
mm/slab_common.c | 4 +++-
3 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/lib/tests/slub_kunit.c b/lib/tests/slub_kunit.c
index 49b9942bc564..646596663b33 100644
--- a/lib/tests/slub_kunit.c
+++ b/lib/tests/slub_kunit.c
@@ -11,6 +11,8 @@
#include <linux/kprobes.h>
#include <linux/string_choices.h>
#include <linux/swab.h>
+#include <linux/mman.h>
+#include <linux/string.h>
#include "../mm/slab.h"

static struct kunit_resource resource;
@@ -396,6 +398,38 @@ static void test_krealloc_redzone_zeroing(struct kunit *test)
kmem_cache_destroy(s);
}

+#ifdef CONFIG_SLAB_BUCKETS
+static void test_slab_buckets(struct kunit *test)
+{
+ unsigned long user_addr;
+ const char *name;
+ void *p;
+
+ if (!slab_buckets_enabled)
+ KUNIT_EXPECT_NULL(test, kmem_buckets_create("TestSlub_buckets",
+ 0, 0, INT_MAX, NULL));
+
+ user_addr = kunit_vm_mmap(test, NULL, 0, PAGE_SIZE,
+ PROT_READ | PROT_WRITE,
+ MAP_ANONYMOUS | MAP_PRIVATE, 0);
+ KUNIT_ASSERT_NE_MSG(test, user_addr, 0, "Could not create userspace mm");
+ KUNIT_ASSERT_LT_MSG(test, user_addr, (unsigned long)TASK_SIZE,
+ "Failed to allocate user memory");
+
+ p = memdup_user((void __user *)user_addr, 64);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
+
+ name = virt_to_slab(p)->slab_cache->name;
+ if (slab_buckets_enabled)
+ KUNIT_EXPECT_TRUE_MSG(test, strstarts(name, "memdup_user-"),
+ "allocated from %s", name);
+ else
+ KUNIT_EXPECT_TRUE_MSG(test, strstarts(name, "kmalloc-"),
+ "allocated from %s", name);
+ kfree(p);
+}
+#endif
+
#if defined(CONFIG_PERF_EVENTS) || (defined(CONFIG_KPROBES) && defined(CONFIG_SMP))
#define NR_ITERATIONS 1000
#define NR_OBJECTS 1000
@@ -591,6 +625,9 @@ static struct kunit_case test_cases[] = {
KUNIT_CASE(test_kfree_rcu_wq_destroy),
KUNIT_CASE(test_leak_destroy),
KUNIT_CASE(test_krealloc_redzone_zeroing),
+#ifdef CONFIG_SLAB_BUCKETS
+ KUNIT_CASE(test_slab_buckets),
+#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 b648b33f25c3..185df5e68419 100644
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -390,6 +390,10 @@ extern struct list_head slab_caches;
/* The slab cache that manages slab cache information */
extern struct kmem_cache *kmem_cache;

+#if IS_ENABLED(CONFIG_KUNIT)
+extern bool slab_buckets_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 b7a46c368ab1..9fe48da12010 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -29,6 +29,7 @@
#include <linux/memcontrol.h>
#include <linux/stackdepot.h>
#include <trace/events/rcu.h>
+#include <kunit/visibility.h>

#include "../kernel/rcu/rcu.h"
#include "internal.h"
@@ -410,7 +411,8 @@ EXPORT_SYMBOL(__kmem_cache_create_args);

static struct kmem_cache *kmem_buckets_cache __ro_after_init;

-static bool slab_buckets_enabled __ro_after_init = IS_ENABLED(CONFIG_SLAB_BUCKETS);
+VISIBLE_IF_KUNIT bool slab_buckets_enabled __ro_after_init = IS_ENABLED(CONFIG_SLAB_BUCKETS);
+EXPORT_SYMBOL_IF_KUNIT(slab_buckets_enabled);

#ifdef CONFIG_SLAB_BUCKETS
static int __init setup_slab_buckets(char *str)
--
2.54.0