Re: [PATCH v2] lib/tests: add KUnit tests for the priority sorted list (plist)
From: Geert Uytterhoeven
Date: Fri Nov 06 2026 - 03:13:57 EST
Hu Yuehong,
On Fri, 6 Nov 2026 at 03:18, Yuehong Wu <wuyuehong@xxxxxxxxxxxxxxxxxxxx> wrote:
> lib/plist.c has no KUnit coverage; its only test is the in-module
> plist_test() under CONFIG_DEBUG_PLIST, which BUG_ON()s on the first
> violation and does not report results in KTAP format. The code has seen
> three regression-class changes since mid-2024:
>
> commit c8dab79f9eef ("lib/plist.c: avoid worst case scenario in
> plist_add")
> commit 95d4b3450ebe ("lib/plist.c: add shortcut for plist_requeue()")
> commit 43c91b152243 ("lib/plist: fix plist_requeue() corrupting order
> in the last bucket")
>
> The last one has no regression test: plist_kunit_requeue_last_bucket()
> covers exactly that wrap-around, and the other cases pin down the ordering
> invariants the previous two changes rely on. KUnit coverage makes such
> ordering regressions visible in CI, where CONFIG_DEBUG_PLIST is not
> normally enabled.
>
> Add lib/tests/plist_kunit.c, a KUnit suite that exercises plist_add(),
> plist_del() and plist_requeue() and validates the invariants documented
> in include/linux/plist.h after every operation:
>
> - node_list stays sorted by ascending numeric priority (descending
> priority order) with FIFO order inside a priority bucket;
>
> - exactly the first node of each bucket is linked on prio_list, and
> the headless prio_list ring visits the bucket heads in node_list
> order before wrapping back to the first bucket (a single bucket
> degenerates to a self loop on the first node).
>
> The fixed cases cover bucket head handover on delete (first, middle and
> last bucket), the plist_requeue() shortcut with and without a following
> bucket, requeueing the head of the first and of the last bucket where the
> ring wraps around, the INT_MIN/INT_MAX extremes, and plist_first_entry()/
> plist_last_entry(). A deterministic pseudo-random add/del/requeue churn
> test mirrors the in-module test but draws priorities from a small range
> so that multi-node buckets actually occur, and every requeue in the loop
> moves a node (a requeue without a same-priority successor is a no-op).
>
> The suite is wired up as CONFIG_PLIST_KUNIT_TEST next to the other
> library KUnit tests and is enabled by default with KUNIT_ALL_TESTS.
> lib/plist.c is built into the kernel and exports none of plist_add(),
> plist_del() or plist_requeue(), so the test cannot be built as a module:
> make it built-in only (bool, depends on KUNIT=y), like the other KUnit
> tests that use non-exported symbols.
>
> The tests were built into an x86_64 kernel and run under QEMU/KVM with
> kunit.enable=1 kunit.filter_glob=plist*: 17 of 17 tests pass.
>
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
> Closes: https://lore.kernel.org/oe-kbuild-all/202611052109.GqbzRkv1-lkp@xxxxxxxxx/
> Signed-off-by: Yuehong Wu <wuyuehong@xxxxxxxxxxxxxxxxxxxx>
Thanks for your patch!
> ---
> v2:
> - Make the test built-in only (bool + depends on KUNIT=y): lib/plist.c is
> built into the kernel and does not export plist_add()/plist_del()/
> plist_requeue(), so a modular build fails in modpost. Reported by the
> kernel test robot.
EXPORT_SYMBOL_IF_KUNIT() can solve that...
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -3065,6 +3065,25 @@ config MIN_HEAP_KUNIT_TEST
>
> If unsure, say N
>
> +config PLIST_KUNIT_TEST
> + bool "KUnit test for priority sorted list (plist)" if !KUNIT_ALL_TESTS
... so I'd rather keep it tristate.
> + depends on KUNIT=y
> + default KUNIT_ALL_TESTS
> + help
> + This builds the priority sorted list (plist) KUnit test suite.
> + It checks that plist_add(), plist_del() and plist_requeue()
> + maintain the ordering invariants of the two-level list, including
> + the priority bucket boundary cases.
> +
> + KUnit tests run during boot and print their results in KTAP format.
> + Only useful for kernel developers running the KUnit test harness,
> + not for production builds.
> +
> + 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 IS_SIGNED_TYPE_KUNIT_TEST
> tristate "Test is_signed_type() macro" if !KUNIT_ALL_TESTS
> depends on KUNIT
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@xxxxxxxxxxxxxx
In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds