From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796510425; b=yK9JleFMp59bAw6bp+594qoL/pniRF6J2Ok55heDVbRavDEy93spNM3z/bAlZHKuFJE/ HfInox1IdO3y8Lzt4iWi+JgcS5s4jVokJkbefbZYXpg95WkDMiQfFLaycsqrymlTEWdCw cXFXUshLcvgFkc7dvhH4Irt/4xKpY2Arwz6lNjWHMqAWqGG3cjqV6b+p7LRU0EDm5eWKD KSN6HjmDuKj6BBMklLz1RjLItpJZC2lShE9D+3QZOTNmnUSl9pApyYa241LRMF+rkwbh4 ooDb055RJ+r7z2dyeICJwdMaMr5QEQ87+WbFYwAE2qEUP0CN1wxbxMFRYMxpIXr+F9w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796510425; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=S04GPqPQjPrXG58WPG39/ZzgscqPBA+Ugjsy/2Hfe8o=; b=nBLxW5LmTEtodBq8ZC56epHmFd0oEHBHYkozdOPklC2gz0xsKizC3rhxuhVV3KTkHxfW pHm7l+b9+IQFrR3TQFs5iI8VeAzB43G7f4aVY72Vb+641AgPflMTjSt2R21FuT2pISwdp Oz+/cvvd4wB5mio2yE+3wW9QBtyk84UhSEi9hJnfCdi8Cv6RL2MISFZIqYbQwCO+jead6 KEIFVAc0g83OvRkMTDhRvaP+3zAcIhJZz2/VZcIxArL9UIEqp0Wr1tSw0MnR2rPyRB/8q ERmEpgCWpjaskm6qj5nzHC9ucKwfjSmVE3jqubO+2zvRbMLMpKMOmQ0W6FlUwcPuFQA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=vG2Nrccf; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ADE2A606A31F for ; Sat, 5 Dec 2026 22:40:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=vG2Nrccf Received: by smtp.kernel.org (Postfix) with ESMTPSA id 532081F00936; Sat, 5 Dec 2026 22:40:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796510425; bh=S04GPqPQjPrXG58WPG39/ZzgscqPBA+Ugjsy/2Hfe8o=; h=Date:To:From:In-Reply-To:References:Subject; b=vG2Nrccf6Q7QF0XZWLlGnr0crXlmGoG0ldBr44YUD2Fa8P/hQkgC5eJQlGYtIiDEP B+WFGAPoWqlj5qWL1OeXKSGkyLycR7ks3okr6UyoiniqjP54YW0nKJfs6N6odgZEIX J2UJ4Q+4z0gsB5Ms36z+lmDjRSWq5/jYajZ/XrzA= Date: Sat, 05 Dec 2026 14:40:24 -0800 To: mm-commits@vger.kernel.org,kent.overstreet@linux.dev,nogunix@gmail.com, akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261205-genradix-kunit-v1-1-c8ca8001a982@gmail.com> References: <20261205-genradix-kunit-v1-1-c8ca8001a982@gmail.com> Subject: + lib-tests-add-kunit-tests-for-generic-radix-trees.patch added to mm-nonmm-unstable branch Message-Id: <20261205224025.532081F00936@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 Signed-off-by: Andrew Morton Cc: Kent Overstreet 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 +#include + +#include +#include +#include +#include + +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 _ Patches currently in -mm which might be from nogunix@gmail.com are lib-tests-add-kunit-tests-for-generic-radix-trees.patch