* [PATCH] lib/tests: add KUnit tests for generic radix trees
@ 2026-12-05 9:16 Masaharu Noguchi
2026-12-05 22:40 ` + lib-tests-add-kunit-tests-for-generic-radix-trees.patch added to mm-nonmm-unstable branch Andrew Morton
0 siblings, 1 reply; 2+ messages in thread
From: Masaharu Noguchi @ 2026-12-05 9:16 UTC (permalink / raw)
To: Andrew Morton, Kent Overstreet
Cc: linux-kernel, kunit-dev, linux-kselftest, Masaharu Noguchi
lib/generic-radix-tree.c provides typed sparse arrays (GENRADIX) used
across the kernel, but currently has no KUnit regression tests.
Add a KUnit test suite (lib/tests/genradix_kunit.c) covering the core
semantics of generic radix trees:
- empty tree lookups and forward/reverse iteration
- basic allocation, zero-initialization, and idempotent re-allocation
- sparse indexing across multiple tree depths
- non-power-of-two element sizes (12-byte struct with node remainder)
for offset calculation and forward/reverse iteration
- genradix_for_each_from() and genradix_for_each_reverse()
- genradix_prealloc() and genradix_ptr_alloc_preallocated()
- genradix_free() and tree reuse
- boundary iterator peek behavior at SIZE_MAX and past tree depth
Tested under ARCH=um with:
./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_GENRADIX_KUNIT_TEST=y 'genradix'
Sample KUnit output:
[PASSED] genradix_test_empty
[PASSED] genradix_test_basic_alloc_and_lookup
[PASSED] genradix_test_sparse_growth
[PASSED] genradix_test_non_pow2_struct
[PASSED] genradix_test_iter_from_and_reverse
[PASSED] genradix_test_prealloc
[PASSED] genradix_test_ptr_alloc_preallocated
[PASSED] genradix_test_free_and_reuse
[PASSED] genradix_test_iter_boundary
==================== [PASSED] genradix =====================
Testing complete. Ran 9 tests: passed: 9
Also verified W=1 compilation of lib/tests/genradix_kunit.o with no
warnings.
Signed-off-by: Masaharu Noguchi <nogunix@gmail.com>
---
Note: An LLM (Gemini) was used to help draft the initial test cases and
commit message; all changes were reviewed, verified, and signed off by
the submitter.
---
MAINTAINERS | 1 +
lib/Kconfig.debug | 15 +++
lib/tests/Makefile | 1 +
lib/tests/genradix_kunit.c | 321 +++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 338 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c49..3856db8e9424 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11304,6 +11304,7 @@ S: Supported
C: irc://irc.oftc.net/bcache
F: include/linux/generic-radix-tree.h
F: lib/generic-radix-tree.c
+F: lib/tests/genradix_kunit.c
GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER
M: Eugen Hristev <ehristev@kernel.org>
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 2d22638ec13a..01771c362fba 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2845,6 +2845,21 @@ config ERRSEQ_KUNIT_TEST
If unsure, say N.
+config GENRADIX_KUNIT_TEST
+ tristate "KUnit test for generic radix trees" if !KUNIT_ALL_TESTS
+ depends on KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ This builds the generic radix tree KUnit test suite.
+ It tests allocation, sparse lookup, forward and reverse iteration,
+ non-power-of-two element sizes, preallocation, and cleanup of
+ generic radix trees (lib/generic-radix-tree.c).
+
+ For more information on KUnit and unit tests in general please refer
+ to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
+
config KFIFO_KUNIT_TEST
tristate "KUnit Test for the generic kernel FIFO implementation" if !KUNIT_ALL_TESTS
depends on KUNIT
diff --git a/lib/tests/Makefile b/lib/tests/Makefile
index df0537e1b91f..9658b0051058 100644
--- a/lib/tests/Makefile
+++ b/lib/tests/Makefile
@@ -23,6 +23,7 @@ CFLAGS_fortify_kunit.o += $(DISABLE_STRUCTLEAK_PLUGIN)
obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o
CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE)
obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o
+obj-$(CONFIG_GENRADIX_KUNIT_TEST) += genradix_kunit.o
obj-$(CONFIG_GLOB_KUNIT_TEST) += glob_kunit.o
obj-$(CONFIG_HASHTABLE_KUNIT_TEST) += hashtable_test.o
obj-$(CONFIG_HASH_KUNIT_TEST) += test_hash.o
diff --git a/lib/tests/genradix_kunit.c b/lib/tests/genradix_kunit.c
new file mode 100644
index 000000000000..93b20ee14cc5
--- /dev/null
+++ b/lib/tests/genradix_kunit.c
@@ -0,0 +1,321 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for the generic radix tree (lib/generic-radix-tree.c).
+ *
+ * Exercises allocation, zero-initialization, sparse indexing across
+ * multiple tree depths, power-of-two and non-power-of-two element sizes,
+ * forward and reverse iteration, preallocation, boundary peek behavior,
+ * and freeing/reinitialization.
+ */
+#include <kunit/resource.h>
+#include <kunit/test.h>
+
+#include <linux/generic-radix-tree.h>
+#include <linux/gfp.h>
+#include <linux/limits.h>
+#include <linux/module.h>
+
+struct odd_entry {
+ u32 a;
+ u32 b;
+ u32 c;
+};
+
+struct u64_radix_ctx {
+ GENRADIX(u64) radix;
+};
+
+struct odd_radix_ctx {
+ GENRADIX(struct odd_entry) radix;
+};
+
+static void genradix_test_free_action(void *data)
+{
+ __genradix_free(data);
+}
+
+static struct u64_radix_ctx *alloc_u64_radix(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ctx);
+ genradix_init(&ctx->radix);
+ KUNIT_ASSERT_EQ(test,
+ kunit_add_action_or_reset(test, genradix_test_free_action,
+ &ctx->radix.tree),
+ 0);
+ return ctx;
+}
+
+static void genradix_test_empty(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ struct genradix_iter iter;
+ u64 *p;
+ unsigned int count = 0;
+
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 0));
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 1024));
+ KUNIT_EXPECT_NULL(test, genradix_ptr_inlined(&ctx->radix, 0));
+
+ genradix_for_each(&ctx->radix, iter, p)
+ count++;
+ KUNIT_EXPECT_EQ(test, count, 0U);
+
+ genradix_for_each_reverse(&ctx->radix, iter, p)
+ count++;
+ KUNIT_EXPECT_EQ(test, count, 0U);
+}
+
+static void genradix_test_basic_alloc_and_lookup(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ u64 *p1, *p2;
+
+ p1 = genradix_ptr_alloc(&ctx->radix, 0, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p1);
+ KUNIT_EXPECT_EQ(test, *p1, 0ULL);
+
+ *p1 = 0xdeadbeefcafebabeULL;
+
+ /* Re-allocating the same index returns the same slot without zeroing. */
+ p2 = genradix_ptr_alloc(&ctx->radix, 0, GFP_KERNEL);
+ KUNIT_EXPECT_PTR_EQ(test, p2, p1);
+ KUNIT_EXPECT_EQ(test, *p2, 0xdeadbeefcafebabeULL);
+
+ KUNIT_EXPECT_PTR_EQ(test, genradix_ptr(&ctx->radix, 0), p1);
+ KUNIT_EXPECT_PTR_EQ(test, genradix_ptr_inlined(&ctx->radix, 0), p1);
+}
+
+static void genradix_test_sparse_growth(struct kunit *test)
+{
+ static const size_t indices[] = { 0, 1, 63, 64, 512, 4096, 100000, 1000000 };
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(indices); i++) {
+ u64 *p = genradix_ptr_alloc(&ctx->radix, indices[i], GFP_KERNEL);
+
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, 0ULL);
+ *p = (u64)indices[i] + 1;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(indices); i++) {
+ u64 *p = genradix_ptr(&ctx->radix, indices[i]);
+
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, (u64)indices[i] + 1);
+ }
+
+ /* Unallocated intermediate and out-of-range indices return NULL. */
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 50000));
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 2000000));
+}
+
+static void genradix_test_non_pow2_struct(struct kunit *test)
+{
+ /*
+ * 12 bytes does not evenly divide GENRADIX_NODE_SIZE (512), leaving
+ * a node remainder and exercising the non-power-of-two offset and
+ * iterator advance/rewind paths.
+ */
+ static const size_t indices[] = { 0, 1, 41, 42, 43, 84, 200, 5000 };
+ struct odd_radix_ctx *ctx;
+ struct genradix_iter iter;
+ struct odd_entry *p;
+ unsigned int i, found;
+
+ static_assert(sizeof(struct odd_entry) == 12);
+ static_assert(GENRADIX_NODE_SIZE % sizeof(struct odd_entry) != 0);
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ctx);
+ genradix_init(&ctx->radix);
+ KUNIT_ASSERT_EQ(test,
+ kunit_add_action_or_reset(test, genradix_test_free_action,
+ &ctx->radix.tree),
+ 0);
+
+ for (i = 0; i < ARRAY_SIZE(indices); i++) {
+ p = genradix_ptr_alloc(&ctx->radix, indices[i], GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, p->a, 0U);
+ KUNIT_EXPECT_EQ(test, p->b, 0U);
+ KUNIT_EXPECT_EQ(test, p->c, 0U);
+
+ p->a = indices[i] + 1;
+ p->b = (indices[i] + 1) * 3;
+ p->c = (indices[i] + 1) * 7;
+ }
+
+ /* Forward iteration visits all populated entries in ascending order. */
+ found = 0;
+ genradix_for_each(&ctx->radix, iter, p) {
+ if (!p->a)
+ continue;
+ KUNIT_ASSERT_LT(test, found, (unsigned int)ARRAY_SIZE(indices));
+ KUNIT_EXPECT_EQ(test, iter.pos, indices[found]);
+ KUNIT_EXPECT_EQ(test, p->a, (u32)(indices[found] + 1));
+ KUNIT_EXPECT_EQ(test, p->b, (u32)((indices[found] + 1) * 3));
+ KUNIT_EXPECT_EQ(test, p->c, (u32)((indices[found] + 1) * 7));
+ found++;
+ }
+ KUNIT_EXPECT_EQ(test, found, (unsigned int)ARRAY_SIZE(indices));
+
+ /* Reverse iteration visits all populated entries in descending order. */
+ found = ARRAY_SIZE(indices);
+ genradix_for_each_reverse(&ctx->radix, iter, p) {
+ if (!p->a)
+ continue;
+ KUNIT_ASSERT_GT(test, found, 0U);
+ found--;
+ KUNIT_EXPECT_EQ(test, iter.pos, indices[found]);
+ KUNIT_EXPECT_EQ(test, p->a, (u32)(indices[found] + 1));
+ }
+ KUNIT_EXPECT_EQ(test, found, 0U);
+}
+
+static void genradix_test_iter_from_and_reverse(struct kunit *test)
+{
+ static const size_t indices[] = { 3, 70, 1024, 65536 };
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ struct genradix_iter iter;
+ u64 *p;
+ unsigned int i, found;
+
+ for (i = 0; i < ARRAY_SIZE(indices); i++) {
+ p = genradix_ptr_alloc(&ctx->radix, indices[i], GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ *p = indices[i] + 10;
+ }
+
+ /* genradix_for_each_from() skips entries strictly below @start. */
+ found = 1;
+ genradix_for_each_from(&ctx->radix, iter, p, 70) {
+ if (!*p)
+ continue;
+ KUNIT_ASSERT_LT(test, found, (unsigned int)ARRAY_SIZE(indices));
+ KUNIT_EXPECT_EQ(test, iter.pos, indices[found]);
+ KUNIT_EXPECT_EQ(test, *p, (u64)(indices[found] + 10));
+ found++;
+ }
+ KUNIT_EXPECT_EQ(test, found, (unsigned int)ARRAY_SIZE(indices));
+
+ /* Reverse iteration covers all entries in strictly descending order. */
+ found = ARRAY_SIZE(indices);
+ genradix_for_each_reverse(&ctx->radix, iter, p) {
+ if (!*p)
+ continue;
+ KUNIT_ASSERT_GT(test, found, 0U);
+ found--;
+ KUNIT_EXPECT_EQ(test, iter.pos, indices[found]);
+ KUNIT_EXPECT_EQ(test, *p, (u64)(indices[found] + 10));
+ }
+ KUNIT_EXPECT_EQ(test, found, 0U);
+}
+
+static void genradix_test_prealloc(struct kunit *test)
+{
+ const size_t nr = 300;
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ size_t i;
+
+ KUNIT_ASSERT_EQ(test, genradix_prealloc(&ctx->radix, nr, GFP_KERNEL), 0);
+
+ for (i = 0; i < nr; i++) {
+ u64 *p = genradix_ptr(&ctx->radix, i);
+
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, 0ULL);
+ *p = i + 1;
+ }
+
+ for (i = 0; i < nr; i++) {
+ u64 *p = genradix_ptr(&ctx->radix, i);
+
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, (u64)(i + 1));
+ }
+}
+
+static void genradix_test_ptr_alloc_preallocated(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ struct genradix_node *prealloc;
+ u64 *p;
+
+ prealloc = genradix_alloc_node(GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, prealloc);
+
+ p = genradix_ptr_alloc_preallocated(&ctx->radix, 5, &prealloc, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_NULL(test, prealloc);
+ KUNIT_EXPECT_EQ(test, *p, 0ULL);
+
+ *p = 0x12345678ULL;
+ KUNIT_EXPECT_PTR_EQ(test, genradix_ptr(&ctx->radix, 5), p);
+ KUNIT_EXPECT_EQ(test, *genradix_ptr(&ctx->radix, 5), 0x12345678ULL);
+}
+
+static void genradix_test_free_and_reuse(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ u64 *p;
+
+ p = genradix_ptr_alloc(&ctx->radix, 128, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ *p = 0xabcdULL;
+
+ genradix_free(&ctx->radix);
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 128));
+
+ p = genradix_ptr_alloc(&ctx->radix, 128, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, 0ULL);
+}
+
+static void genradix_test_iter_boundary(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ struct genradix_iter iter;
+ u64 *p;
+
+ p = genradix_ptr_alloc(&ctx->radix, 0, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ *p = 42;
+
+ /* Peeking at SIZE_MAX offset must cleanly return NULL. */
+ iter.offset = SIZE_MAX;
+ iter.pos = SIZE_MAX;
+ KUNIT_EXPECT_NULL(test, genradix_iter_peek(&iter, &ctx->radix));
+ KUNIT_EXPECT_NULL(test, genradix_iter_peek_prev(&iter, &ctx->radix));
+
+ /* Peeking past the allocated tree depth must cleanly return NULL. */
+ iter = genradix_iter_init(&ctx->radix, 1000000);
+ KUNIT_EXPECT_NULL(test, genradix_iter_peek(&iter, &ctx->radix));
+}
+
+static struct kunit_case genradix_test_cases[] = {
+ KUNIT_CASE(genradix_test_empty),
+ KUNIT_CASE(genradix_test_basic_alloc_and_lookup),
+ KUNIT_CASE(genradix_test_sparse_growth),
+ KUNIT_CASE(genradix_test_non_pow2_struct),
+ KUNIT_CASE(genradix_test_iter_from_and_reverse),
+ KUNIT_CASE(genradix_test_prealloc),
+ KUNIT_CASE(genradix_test_ptr_alloc_preallocated),
+ KUNIT_CASE(genradix_test_free_and_reuse),
+ KUNIT_CASE(genradix_test_iter_boundary),
+ {},
+};
+
+static struct kunit_suite genradix_test_suite = {
+ .name = "genradix",
+ .test_cases = genradix_test_cases,
+};
+
+kunit_test_suite(genradix_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for generic radix trees");
+MODULE_LICENSE("GPL");
---
base-commit: 0810bd27d7bbccb4f579f1fbb1afc98687c4c7c3
change-id: 20261205-genradix-kunit-cb6485433664
Best regards,
--
Masaharu Noguchi <nogunix@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread* + lib-tests-add-kunit-tests-for-generic-radix-trees.patch added to mm-nonmm-unstable branch
2026-12-05 9:16 [PATCH] lib/tests: add KUnit tests for generic radix trees Masaharu Noguchi
@ 2026-12-05 22:40 ` Andrew Morton
0 siblings, 0 replies; 2+ messages in thread
From: Andrew Morton @ 2026-12-05 22:40 UTC (permalink / raw)
To: mm-commits, kent.overstreet, nogunix, akpm
The patch titled
Subject: lib/tests: add KUnit tests for generic radix trees
has been added to the -mm mm-nonmm-unstable branch. Its filename is
lib-tests-add-kunit-tests-for-generic-radix-trees.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/lib-tests-add-kunit-tests-for-generic-radix-trees.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a
reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Masaharu Noguchi <nogunix@gmail.com>
Subject: lib/tests: add KUnit tests for generic radix trees
Date: Sat, 05 Dec 2026 18:16:42 +0900
lib/generic-radix-tree.c provides typed sparse arrays (GENRADIX) used
across the kernel, but currently has no KUnit regression tests.
Add a KUnit test suite (lib/tests/genradix_kunit.c) covering the core
semantics of generic radix trees:
- empty tree lookups and forward/reverse iteration
- basic allocation, zero-initialization, and idempotent re-allocation
- sparse indexing across multiple tree depths
- non-power-of-two element sizes (12-byte struct with node remainder)
for offset calculation and forward/reverse iteration
- genradix_for_each_from() and genradix_for_each_reverse()
- genradix_prealloc() and genradix_ptr_alloc_preallocated()
- genradix_free() and tree reuse
- boundary iterator peek behavior at SIZE_MAX and past tree depth
Tested under ARCH=um with:
./tools/testing/kunit/kunit.py run \
--kconfig_add CONFIG_GENRADIX_KUNIT_TEST=y 'genradix'
Sample KUnit output:
[PASSED] genradix_test_empty
[PASSED] genradix_test_basic_alloc_and_lookup
[PASSED] genradix_test_sparse_growth
[PASSED] genradix_test_non_pow2_struct
[PASSED] genradix_test_iter_from_and_reverse
[PASSED] genradix_test_prealloc
[PASSED] genradix_test_ptr_alloc_preallocated
[PASSED] genradix_test_free_and_reuse
[PASSED] genradix_test_iter_boundary
==================== [PASSED] genradix =====================
Testing complete. Ran 9 tests: passed: 9
Also verified W=1 compilation of lib/tests/genradix_kunit.o with no
warnings.
Link: https://patch.msgid.link/20261205-genradix-kunit-v1-1-c8ca8001a982@gmail.com
Signed-off-by: Masaharu Noguchi <nogunix@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Kent Overstreet <kent.overstreet@linux.dev>
MAINTAINERS | 1
lib/Kconfig.debug | 15 +
lib/tests/Makefile | 1
lib/tests/genradix_kunit.c | 321 +++++++++++++++++++++++++++++++++++
4 files changed, 338 insertions(+)
--- a/lib/Kconfig.debug~lib-tests-add-kunit-tests-for-generic-radix-trees
+++ a/lib/Kconfig.debug
@@ -2845,6 +2845,21 @@ config ERRSEQ_KUNIT_TEST
If unsure, say N.
+config GENRADIX_KUNIT_TEST
+ tristate "KUnit test for generic radix trees" if !KUNIT_ALL_TESTS
+ depends on KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ This builds the generic radix tree KUnit test suite.
+ It tests allocation, sparse lookup, forward and reverse iteration,
+ non-power-of-two element sizes, preallocation, and cleanup of
+ generic radix trees (lib/generic-radix-tree.c).
+
+ For more information on KUnit and unit tests in general please refer
+ to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
+
config KFIFO_KUNIT_TEST
tristate "KUnit Test for the generic kernel FIFO implementation" if !KUNIT_ALL_TESTS
depends on KUNIT
diff --git a/lib/tests/genradix_kunit.c a/lib/tests/genradix_kunit.c
new file mode 100644
--- /dev/null
+++ a/lib/tests/genradix_kunit.c
@@ -0,0 +1,321 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for the generic radix tree (lib/generic-radix-tree.c).
+ *
+ * Exercises allocation, zero-initialization, sparse indexing across
+ * multiple tree depths, power-of-two and non-power-of-two element sizes,
+ * forward and reverse iteration, preallocation, boundary peek behavior,
+ * and freeing/reinitialization.
+ */
+#include <kunit/resource.h>
+#include <kunit/test.h>
+
+#include <linux/generic-radix-tree.h>
+#include <linux/gfp.h>
+#include <linux/limits.h>
+#include <linux/module.h>
+
+struct odd_entry {
+ u32 a;
+ u32 b;
+ u32 c;
+};
+
+struct u64_radix_ctx {
+ GENRADIX(u64) radix;
+};
+
+struct odd_radix_ctx {
+ GENRADIX(struct odd_entry) radix;
+};
+
+static void genradix_test_free_action(void *data)
+{
+ __genradix_free(data);
+}
+
+static struct u64_radix_ctx *alloc_u64_radix(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ctx);
+ genradix_init(&ctx->radix);
+ KUNIT_ASSERT_EQ(test,
+ kunit_add_action_or_reset(test, genradix_test_free_action,
+ &ctx->radix.tree),
+ 0);
+ return ctx;
+}
+
+static void genradix_test_empty(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ struct genradix_iter iter;
+ u64 *p;
+ unsigned int count = 0;
+
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 0));
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 1024));
+ KUNIT_EXPECT_NULL(test, genradix_ptr_inlined(&ctx->radix, 0));
+
+ genradix_for_each(&ctx->radix, iter, p)
+ count++;
+ KUNIT_EXPECT_EQ(test, count, 0U);
+
+ genradix_for_each_reverse(&ctx->radix, iter, p)
+ count++;
+ KUNIT_EXPECT_EQ(test, count, 0U);
+}
+
+static void genradix_test_basic_alloc_and_lookup(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ u64 *p1, *p2;
+
+ p1 = genradix_ptr_alloc(&ctx->radix, 0, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p1);
+ KUNIT_EXPECT_EQ(test, *p1, 0ULL);
+
+ *p1 = 0xdeadbeefcafebabeULL;
+
+ /* Re-allocating the same index returns the same slot without zeroing. */
+ p2 = genradix_ptr_alloc(&ctx->radix, 0, GFP_KERNEL);
+ KUNIT_EXPECT_PTR_EQ(test, p2, p1);
+ KUNIT_EXPECT_EQ(test, *p2, 0xdeadbeefcafebabeULL);
+
+ KUNIT_EXPECT_PTR_EQ(test, genradix_ptr(&ctx->radix, 0), p1);
+ KUNIT_EXPECT_PTR_EQ(test, genradix_ptr_inlined(&ctx->radix, 0), p1);
+}
+
+static void genradix_test_sparse_growth(struct kunit *test)
+{
+ static const size_t indices[] = { 0, 1, 63, 64, 512, 4096, 100000, 1000000 };
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(indices); i++) {
+ u64 *p = genradix_ptr_alloc(&ctx->radix, indices[i], GFP_KERNEL);
+
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, 0ULL);
+ *p = (u64)indices[i] + 1;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(indices); i++) {
+ u64 *p = genradix_ptr(&ctx->radix, indices[i]);
+
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, (u64)indices[i] + 1);
+ }
+
+ /* Unallocated intermediate and out-of-range indices return NULL. */
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 50000));
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 2000000));
+}
+
+static void genradix_test_non_pow2_struct(struct kunit *test)
+{
+ /*
+ * 12 bytes does not evenly divide GENRADIX_NODE_SIZE (512), leaving
+ * a node remainder and exercising the non-power-of-two offset and
+ * iterator advance/rewind paths.
+ */
+ static const size_t indices[] = { 0, 1, 41, 42, 43, 84, 200, 5000 };
+ struct odd_radix_ctx *ctx;
+ struct genradix_iter iter;
+ struct odd_entry *p;
+ unsigned int i, found;
+
+ static_assert(sizeof(struct odd_entry) == 12);
+ static_assert(GENRADIX_NODE_SIZE % sizeof(struct odd_entry) != 0);
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ctx);
+ genradix_init(&ctx->radix);
+ KUNIT_ASSERT_EQ(test,
+ kunit_add_action_or_reset(test, genradix_test_free_action,
+ &ctx->radix.tree),
+ 0);
+
+ for (i = 0; i < ARRAY_SIZE(indices); i++) {
+ p = genradix_ptr_alloc(&ctx->radix, indices[i], GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, p->a, 0U);
+ KUNIT_EXPECT_EQ(test, p->b, 0U);
+ KUNIT_EXPECT_EQ(test, p->c, 0U);
+
+ p->a = indices[i] + 1;
+ p->b = (indices[i] + 1) * 3;
+ p->c = (indices[i] + 1) * 7;
+ }
+
+ /* Forward iteration visits all populated entries in ascending order. */
+ found = 0;
+ genradix_for_each(&ctx->radix, iter, p) {
+ if (!p->a)
+ continue;
+ KUNIT_ASSERT_LT(test, found, (unsigned int)ARRAY_SIZE(indices));
+ KUNIT_EXPECT_EQ(test, iter.pos, indices[found]);
+ KUNIT_EXPECT_EQ(test, p->a, (u32)(indices[found] + 1));
+ KUNIT_EXPECT_EQ(test, p->b, (u32)((indices[found] + 1) * 3));
+ KUNIT_EXPECT_EQ(test, p->c, (u32)((indices[found] + 1) * 7));
+ found++;
+ }
+ KUNIT_EXPECT_EQ(test, found, (unsigned int)ARRAY_SIZE(indices));
+
+ /* Reverse iteration visits all populated entries in descending order. */
+ found = ARRAY_SIZE(indices);
+ genradix_for_each_reverse(&ctx->radix, iter, p) {
+ if (!p->a)
+ continue;
+ KUNIT_ASSERT_GT(test, found, 0U);
+ found--;
+ KUNIT_EXPECT_EQ(test, iter.pos, indices[found]);
+ KUNIT_EXPECT_EQ(test, p->a, (u32)(indices[found] + 1));
+ }
+ KUNIT_EXPECT_EQ(test, found, 0U);
+}
+
+static void genradix_test_iter_from_and_reverse(struct kunit *test)
+{
+ static const size_t indices[] = { 3, 70, 1024, 65536 };
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ struct genradix_iter iter;
+ u64 *p;
+ unsigned int i, found;
+
+ for (i = 0; i < ARRAY_SIZE(indices); i++) {
+ p = genradix_ptr_alloc(&ctx->radix, indices[i], GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ *p = indices[i] + 10;
+ }
+
+ /* genradix_for_each_from() skips entries strictly below @start. */
+ found = 1;
+ genradix_for_each_from(&ctx->radix, iter, p, 70) {
+ if (!*p)
+ continue;
+ KUNIT_ASSERT_LT(test, found, (unsigned int)ARRAY_SIZE(indices));
+ KUNIT_EXPECT_EQ(test, iter.pos, indices[found]);
+ KUNIT_EXPECT_EQ(test, *p, (u64)(indices[found] + 10));
+ found++;
+ }
+ KUNIT_EXPECT_EQ(test, found, (unsigned int)ARRAY_SIZE(indices));
+
+ /* Reverse iteration covers all entries in strictly descending order. */
+ found = ARRAY_SIZE(indices);
+ genradix_for_each_reverse(&ctx->radix, iter, p) {
+ if (!*p)
+ continue;
+ KUNIT_ASSERT_GT(test, found, 0U);
+ found--;
+ KUNIT_EXPECT_EQ(test, iter.pos, indices[found]);
+ KUNIT_EXPECT_EQ(test, *p, (u64)(indices[found] + 10));
+ }
+ KUNIT_EXPECT_EQ(test, found, 0U);
+}
+
+static void genradix_test_prealloc(struct kunit *test)
+{
+ const size_t nr = 300;
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ size_t i;
+
+ KUNIT_ASSERT_EQ(test, genradix_prealloc(&ctx->radix, nr, GFP_KERNEL), 0);
+
+ for (i = 0; i < nr; i++) {
+ u64 *p = genradix_ptr(&ctx->radix, i);
+
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, 0ULL);
+ *p = i + 1;
+ }
+
+ for (i = 0; i < nr; i++) {
+ u64 *p = genradix_ptr(&ctx->radix, i);
+
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, (u64)(i + 1));
+ }
+}
+
+static void genradix_test_ptr_alloc_preallocated(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ struct genradix_node *prealloc;
+ u64 *p;
+
+ prealloc = genradix_alloc_node(GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, prealloc);
+
+ p = genradix_ptr_alloc_preallocated(&ctx->radix, 5, &prealloc, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_NULL(test, prealloc);
+ KUNIT_EXPECT_EQ(test, *p, 0ULL);
+
+ *p = 0x12345678ULL;
+ KUNIT_EXPECT_PTR_EQ(test, genradix_ptr(&ctx->radix, 5), p);
+ KUNIT_EXPECT_EQ(test, *genradix_ptr(&ctx->radix, 5), 0x12345678ULL);
+}
+
+static void genradix_test_free_and_reuse(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ u64 *p;
+
+ p = genradix_ptr_alloc(&ctx->radix, 128, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ *p = 0xabcdULL;
+
+ genradix_free(&ctx->radix);
+ KUNIT_EXPECT_NULL(test, genradix_ptr(&ctx->radix, 128));
+
+ p = genradix_ptr_alloc(&ctx->radix, 128, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ KUNIT_EXPECT_EQ(test, *p, 0ULL);
+}
+
+static void genradix_test_iter_boundary(struct kunit *test)
+{
+ struct u64_radix_ctx *ctx = alloc_u64_radix(test);
+ struct genradix_iter iter;
+ u64 *p;
+
+ p = genradix_ptr_alloc(&ctx->radix, 0, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, p);
+ *p = 42;
+
+ /* Peeking at SIZE_MAX offset must cleanly return NULL. */
+ iter.offset = SIZE_MAX;
+ iter.pos = SIZE_MAX;
+ KUNIT_EXPECT_NULL(test, genradix_iter_peek(&iter, &ctx->radix));
+ KUNIT_EXPECT_NULL(test, genradix_iter_peek_prev(&iter, &ctx->radix));
+
+ /* Peeking past the allocated tree depth must cleanly return NULL. */
+ iter = genradix_iter_init(&ctx->radix, 1000000);
+ KUNIT_EXPECT_NULL(test, genradix_iter_peek(&iter, &ctx->radix));
+}
+
+static struct kunit_case genradix_test_cases[] = {
+ KUNIT_CASE(genradix_test_empty),
+ KUNIT_CASE(genradix_test_basic_alloc_and_lookup),
+ KUNIT_CASE(genradix_test_sparse_growth),
+ KUNIT_CASE(genradix_test_non_pow2_struct),
+ KUNIT_CASE(genradix_test_iter_from_and_reverse),
+ KUNIT_CASE(genradix_test_prealloc),
+ KUNIT_CASE(genradix_test_ptr_alloc_preallocated),
+ KUNIT_CASE(genradix_test_free_and_reuse),
+ KUNIT_CASE(genradix_test_iter_boundary),
+ {},
+};
+
+static struct kunit_suite genradix_test_suite = {
+ .name = "genradix",
+ .test_cases = genradix_test_cases,
+};
+
+kunit_test_suite(genradix_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for generic radix trees");
+MODULE_LICENSE("GPL");
--- a/lib/tests/Makefile~lib-tests-add-kunit-tests-for-generic-radix-trees
+++ a/lib/tests/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fort
CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE)
obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o
obj-$(CONFIG_GENALLOC_KUNIT_TEST) += genalloc_kunit.o
+obj-$(CONFIG_GENRADIX_KUNIT_TEST) += genradix_kunit.o
obj-$(CONFIG_GLOB_KUNIT_TEST) += glob_kunit.o
obj-$(CONFIG_HASHTABLE_KUNIT_TEST) += hashtable_test.o
obj-$(CONFIG_HASH_KUNIT_TEST) += test_hash.o
--- a/MAINTAINERS~lib-tests-add-kunit-tests-for-generic-radix-trees
+++ a/MAINTAINERS
@@ -11287,6 +11287,7 @@ S: Supported
C: irc://irc.oftc.net/bcache
F: include/linux/generic-radix-tree.h
F: lib/generic-radix-tree.c
+F: lib/tests/genradix_kunit.c
GENERIC RESISTIVE TOUCHSCREEN ADC DRIVER
M: Eugen Hristev <ehristev@kernel.org>
_
Patches currently in -mm which might be from nogunix@gmail.com are
lib-tests-add-kunit-tests-for-generic-radix-trees.patch
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-05 22:40 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 9:16 [PATCH] lib/tests: add KUnit tests for generic radix trees Masaharu Noguchi
2026-12-05 22:40 ` + lib-tests-add-kunit-tests-for-generic-radix-trees.patch added to mm-nonmm-unstable branch Andrew Morton
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.