From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.173 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796462207; b=WSrfqHkTso8wCUcO1wKWC41iss4CwOEBKguGYdHQGGth4LeLnKb9Yq56xCnlVvVjB6/+ y1gfho0re/POQFZ9nwVBT+2hWiKEhWqjIkf2LjbCig0p7xHMP7bbr6usL8xKMBnJcyE2w vUUCyPO+br5AKztgQNvHw1r3DJfI8LDFKw7qkz8zVNn6k8rmuLBrI4zCYCaOkLM1474Zd SpD3la+9+HrSJBGdKVQbOVE6OemY2yAi+QDoyVFBynIHrr/p7U4BpyXiCHaAygruCAcVW FbxckqxBnA2xoDbIlqm0JJIpW+VE1ylEDFoVPQrHn8iQ17zrDug7Z3Bw3XZFw7zlStw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796462207; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:Date:Subject: MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id: X-B4-Tracking:X-Change-ID:To:Cc:X-Mailer:X-Developer-Signature: X-Developer-Key; bh=hYJN6SFZkmsLEWNhgYLZrnEiNe/SNJW9esbxt+5LSag=; b=scP61d3C9xxM18qKq4SzljfGfP0YualHyF7Uqz3tc1mN4jM+/ORP+0cczNocgk3mt1pj AzU1aEdX3VunCDhtxyHoKOrIPNKUylKMRWPbPPN1Hky/b5It6PIhGtaWrw3B/XcAeAJTt TCpZXFCdvFLiXE00imQHUUNUOcsZp4E3/oJHygzXw7b8CpAOUoYXPwproWsxOU8ZFNLFB IVzitkt7exV3nL46liYIaJKTLwRXB/pZ4xh9okqvp+wIRjQaO8TbbpYWcVb2JoupTrTmK Ti+WGAdPnVTM/8qD/pKq9sMFViCqvE4RSzUffy3IHkH5DUSf1UJL0CQ8dbqeHphLTbA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SlWD+kV2; arc=none smtp.remote-ip=209.85.214.173 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36259606A32F for ; Sat, 5 Dec 2026 09:16:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 36259606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SlWD+kV2 Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2f0b8a221acso2545955ad.2 for ; Sat, 05 Dec 2026 01:16:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796462206; x=1797067006; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:content-type :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=hYJN6SFZkmsLEWNhgYLZrnEiNe/SNJW9esbxt+5LSag=; b=SlWD+kV2ALu0ToF4m64ldOaG3YNNxNFQRdXijh1M353FnsNjzLUBD5Ps/DWXtvDWVl sX0+jukP5+a4lFLO4GajwRzS0MPejT78WzulLg/MPVNSrslv4IINOOfBf2gOBj1n5EEq Th/ZAxeCcdoN8j0uivXVyEou31LLbXdZt25YFcvlispKSLJRpeRQ1Yj0cJywxs+CWUq1 sUvYmbS2ePMj5TjlCuD4NFqHVM3YQtCGW05XdkiRXmRF4Fl7kojVXzmb0iPiTFMYWs2l +GYybu1qF2wJzOFyRjO6iOA3xoyCytHn6JuSrZFbDfqEVtCQhaMhXpJoHRrPmJK9qL4B Gljg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796462206; x=1797067006; h=cc:to:message-id:content-transfer-encoding:content-type :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=hYJN6SFZkmsLEWNhgYLZrnEiNe/SNJW9esbxt+5LSag=; b=0m/XCEPhJ019XW3Ol+4dcvPV5xHkCDftUdRSeVqdQOyb1tm3Cl0r0Khvu30HuVa69Y qLs0bC1cgwTD1iw4Y9h1TUFeKhraN4cVME3tY+xuSIRxGrSh5Lt25bpK2B02WLxhMc6A kd7FWoUYm2764znAqNn+Q7vECZ9NHaznt4cavKoA9YYtwUpar5ibq45G6kRsLo/5t1WZ ENL3mVTaMzvm8JXUUGMPQXnUP3aoa5410+8fq/VfrJ6Ozt8fRbuj0LMKdDnMLGKQS2Fu 1G5S6N3vwzUwQZYiRV+UMljZimsjjgi5h5qvrnhTIBZy5P4D6XDvNARVSee0TCJ3+3JO 1vwA== X-Gm-Message-State: AB4q+uFHUaO3v40J/lBOapawRdva5fK7vQ+Bn2P9MY+1zKFA/rL6aJZP +2z8zzHfuwuAe0Oa+E5WfkhhuLRYfkW5RPB8led7spvTqnU0NMv8yPMb X-Gm-Gg: AZFdTZd36TBotSkaKY+vTvxlJv3kMB2X5uSw6+eH3Eld7qoJ126eWNqDpkYPyuvF1cQ E708htHkeaWmjHBTL/TViREZDjbd1qbbaz2KVtIBk9T0T6Yn3iJVmozWHVhJikeKEmWhkqC8yTH bl7Q7JfS121mX3ZDzFNr+wD6qerovLUZ+p5yd4SYOSnjuiC/UIDx7LHwUFt5xgo/M0UnrUiw22j PEhS3qEux9nd6bwZbk/zrdBWdMBvs7puDPjtuTT7a80W70STM6pq7R+bN7TNwcpzO/u6znEXOD3 huenvGVQd3APvb/eBle0YoCHO2lw0zxpCQJ2m3aYVabVGeoYbZPpupFPbdImfYxWPpYcYpAbFVz T18rgEku9qI9yNMErTjLlKbYxdoEf+gFC8P9mKyUKzBKrj9TZ8bmFQIcfe1RBnWwf2lemDrFBog ISmJNnUR8oqDcgpp9ZBWi0UeMs6NMhLqO/jO+gKq6RdqMiQs8TvoOqNwhMZABp0GwTQ15aOsCzA LSNkRChnaKhTTT6zBh1D5ofSb7+xluaUPdyO4O5QX2k7Ef46behMpT0nY8Gpj8o17Ojgn+treBe vbKjTSornkIOix+ZxZdwuRUEgbKsLbItuRQb X-Received: by 2002:a17:903:2ce:b0:2f0:33d8:f88d with SMTP id d9443c01a7336-2f1e7fc1735mr32501295ad.0.1796462206293; Sat, 05 Dec 2026 01:16:46 -0800 (PST) Received: from [192.168.20.112] ([2400:4050:d860:9700:75bf:9e2e:8ac9:3001]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f1b78f9e40sm24644955ad.23.2026.12.05.01.16.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 01:16:45 -0800 (PST) From: Masaharu Noguchi Date: Sat, 05 Dec 2026 18:16:42 +0900 Subject: [PATCH] lib/tests: add KUnit tests for generic radix trees X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261205-genradix-kunit-v1-1-c8ca8001a982@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAA/x3MWwqAIBAF0K0M9zuhTCXcSvTRY7IhsNCKINp70 FnAeZA5CWd4epD4kixbhKeqIIxLHwMrmeAJutSu0qVVgWPqJ7nVekY51Dg401hT184ZFIQ98Sz 3H7bd+358F5aVYAAAAA== X-Change-ID: 20261205-genradix-kunit-cb6485433664 To: Andrew Morton , Kent Overstreet Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, Masaharu Noguchi X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796462204; l=14141; i=nogunix@gmail.com; s=20261202; h=from:subject:message-id; bh=rvGkXoecMBLTYbG55/6+2oSYxnLSomGb2XJNRwJBn60=; b=GHXeK/hVLRhpIqlU6ELehrFO2KfVkWbxhv7oipfQfOtUkkFeEuIsvy7ATLLGLHWe+ZAAKZzS5 87n8AnKDbtRB1oUQgFIotolK5rX8AWmhDNZgeiByrDiY/hBcovlHiAn X-Developer-Key: i=nogunix@gmail.com; a=ed25519; pk=NgtNsZ+zeVmdqlocewi1nv2/pD3VYxk5S++pnPHKDf8= 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 --- 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 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 +#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"); --- base-commit: 0810bd27d7bbccb4f579f1fbb1afc98687c4c7c3 change-id: 20261205-genradix-kunit-cb6485433664 Best regards, -- Masaharu Noguchi