Silence spurious warnings and crashes from kunit test suites
Subsystems (7)
cover Silence spurious warnings and crashes from kunit test suites
Running the full kunit suite on arm64 (128-core Neoverse N2) with 7.1-rc3+ produces a handful of backtraces that are not real kernel bugs but rather test-infrastructure issues: races with boot-time code, missing bounds checks, resource leaks, and sysfs duplicate-name splats.
This series fixes them one by one so that a clean kunit run no longer leaves noise in dmesg:
1. init/initramfs_test races with the async rootfs unpacker; add wait_for_initramfs() in suite .init.
2. kfence kunit cases fail outright when the pool is unavailable; skip them instead.
3. intlog2(0) / intlog10(0) hit a WARN_ON that is harmless; remove it.
4. kunit_platform_device_add() triggers sysfs_warn_dup() on the duplicate-registration test; catch duplicates early in the kunit helper (best-effort; final protection remains in driver core/sysfs).
5. misc_register() likewise hits sysfs_warn_dup(); reject duplicate names explicitly. misc->name is never NULL at this point since the existing code already dereferences it unconditionally.
6. hw_breakpoint test_many_cpus overflows its array on machines with many cores; bail out at the limit.
7. test_ratelimit stress test checks the wrong variable for kthread creation, leaks memory, and races on doneflag. Use goto-based cleanup to guarantee all started threads are stopped on failure.
All patches are independent and can be applied/reviewed separately. Tested on arm64 Neoverse N2 (128-core arm64) with CONFIG_KUNIT=y and CONFIG_KUNIT_ALL_TESTS=y.
Jia He (7):
init/initramfs_test: wait_for_initramfs() before running
kfence: kunit: skip when no pool is available lib/math/int_log: drop WARN_ON for value == 0
kunit: platform: catch duplicate (name, id) in kunit_platform_device_add()
misc: reject duplicate names in misc_register()
hw_breakpoint_test: fix test_many_cpus failure on large systems lib/tests: test_ratelimit: fix stress test thread lifecycle and leak
drivers/char/misc.c | 22 ++++++++++++++++++ init/initramfs_test.c | 17 +++++++++++++- kernel/events/hw_breakpoint_test.c | 12 ++++++++++ lib/kunit/platform.c | 36 +++++++++++++++++++++++++++++ lib/math/int_log.c | 8 ++----- lib/tests/test_ratelimit.c | 26 ++++++++++++++++----- mm/kfence/kfence_test.c | 6 +++-- 7 files changed, 115 insertions(+), 12 deletions(-)
PATCH 1/7 init/initramfs_test: wait_for_initramfs() before running
initramfs_test_extract() and friends call unpack_to_rootfs() from a kunit kthread while do_populate_rootfs() may still be running asynchronously from rootfs_initcall. unpack_to_rootfs() keeps its parser state in module-static variables (victim, byte_count, state, this_header, header_buf, name_buf, ...), so the two writers corrupt each other.
On arm64 v7.0-rc5+ this oopses early in boot:
Unable to handle kernel paging request at virtual address ffff80018f9f0ffc pc : do_reset+0x3c/0x98 Call trace: do_reset initramfs_test_extract kunit_try_run_case Initramfs unpacking failed: junk within compressed archive
do_reset() faults because 'victim' was overwritten by the boot-time unpacker; the boot unpacker meanwhile logs the bogus "junk within compressed archive" on the real initrd because the test wrecked its state machine.
Add a suite .init that calls wait_for_initramfs() so the async unpack is quiescent before the first case runs.
To: Alexander Viro <[email protected]>
To: Christian Brauner <[email protected]>
To: Jan Kara <[email protected]>
To: David Disseldorp <[email protected]>
Cc: [email protected]
--- init/initramfs_test.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
init/initramfs_test.c +16 -1
@@ -3,7 +3,9 @@#include <linux/fcntl.h> #include <linux/file.h> #include <linux/fs.h> +#include <linux/init.h> #include <linux/init_syscalls.h> +#include <linux/initrd.h> #include <linux/stringify.h> #include <linux/timekeeping.h> #include "initramfs_internal.h"
@@ -510,8 +512,21 @@ static struct kunit_case __refdata initramfs_test_cases[] = { {}, }; -static struct kunit_suite initramfs_test_suite = { +static int __init initramfs_test_init (struct kunit *test) +{ +/* +* unpack_to_rootfs() uses module-static state (victim, byte_count, +* state, ...). The boot-time async do_populate_rootfs() may still be +* running, so wait for it to finish before we call unpack_to_rootfs() +* from the test thread, otherwise the two writers race and crash. +*/ +wait_for_initramfs (); +return 0 ; +} + +static struct kunit_suite __refdata initramfs_test_suite = { .name ="initramfs" , + .init = initramfs_test_init, .test_cases = initramfs_test_cases, };kunit_test_init_section_suites (&initramfs_test_suite);
Signature · 1 line
2.34.1
Re: [PATCH 1/7] init/initramfs_test: wait_for_initramfs() before running
On Thu, 14 May 2026 05:04:49 +0000, Jia He wrote:
...
Quoted 11 lines
Add a suite .init that calls
wait_for_initramfs()so the async unpack is quiescent before the first case runs.To: Alexander Viro <[email protected]>
To: Christian Brauner <[email protected]>
To: Jan Kara <[email protected]>
To: David Disseldorp <[email protected]>
Cc: [email protected]Fixes 83c0b27266ec ("initramfs_test: kunit tests for initramfs unpacking")Signed-off-by Jia He <[email protected]>
Good catch, thanks.
[email protected]
AI reviewRe: [PATCH 1/7] init/initramfs_test: wait_for_initramfs() before running
In reply to Jia HeThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The patch uses the per-test `.init` KUnit callback instead of the suite-level `.suite_init`…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch uses the per-test .init KUnit callback instead of the suite-level .suite_init callback. --
commit 28e6f37006ed9a359c88f36fcc58752b6f3eb78e
Author: Jia He <[email protected]>
init/initramfs_test: wait_for_initramfs() before running
The commit introduces a wait_for_initramfs() call to ensure the boot-time async do_populate_rootfs() is complete before the initramfs kunit tests run. This prevents a race condition where the boot-time unpacker and the test unpacker corrupt each other's module-static state.
Quoted 4 lines
init/initramfs_test.c +0
-0
[ ... ]
Quoted 10 lines
init/initramfs_test.c +5
-1@@ -510,8 +512,21 @@ static struct kunit_case __refdata initramfs_test_cases[] = { {}, }; -static struct kunit_suite initramfs_test_suite = { +static int __init initramfs_test_init (struct kunit *test) +{ +/* +* unpack_to_rootfs() uses module-static state (victim, byte_count, +* state, ...). The boot-time async do_populate_rootfs() may still be
init/initramfs_test.c static struct kunit_case __refdata initramfs_test_cases[] = { · line 529
Quoted 10 lines
init/initramfs_test.c +9
-0+* running, so wait for it to finish before we call unpack_to_rootfs() +* from the test thread, otherwise the two writers race and crash. +*/ +wait_for_initramfs (); +return 0 ; +} + +static struct kunit_suite __refdata initramfs_test_suite = { .name ="initramfs" , + .init = initramfs_test_init,
The commit message notes this is added so the async unpack is quiescent "before the first case runs". Should this be using .suite_init instead of .init?
The .init callback is invoked by KUnit before every individual test case, meaning wait_for_initramfs() will be called once for each of the test cases.
While functionally harmless since subsequent calls return instantly, .suite_init is designed for one-time suite-level initialization and seems to better match the intent.
Quoted 3 lines
init/initramfs_test.c +0
-0.test_cases = initramfs_test_cases, };kunit_test_init_section_suites (&initramfs_test_suite);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
RE: [PATCH 1/7] init/initramfs_test: wait_for_initramfs() before running
Quoted 57 lines
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Thursday, May 14, 2026 8:30 PM
To: Justin He <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 1/7]init/initramfs_test:wait_for_initramfs()before runningThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch uses the per-test.initKUnit callback instead of the suite- level.suite_initcallback.
--commit
28e6f37006ed9a359c88f36fcc58752b6f3eb78e
Author: Jia He <[email protected]>
init/initramfs_test:wait_for_initramfs()before runningThe commit introduces a
wait_for_initramfs()call to ensure the boot-time asyncdo_populate_rootfs()is complete before the initramfs kunit tests run. This prevents a race condition where the boot-time unpacker and the test unpacker corrupt each other's module-static state.Quoted 4 lines
init/initramfs_test.c index renamed from init/initramfs_test.c +0
-0
2ce38d9a8fd0f..eb76f63f302e0100644init/initramfs_test.c +0
-0[ ... ]
init/initramfs_test.c static struct kunit_case __refdata
Quoted 1 line
init/initramfs_test.c +0
-0@@ -510,8 +512,21 @@ static struct kunit_case __refdatainitramfs_test_cases[] = {
Quoted 8 lines
init/initramfs_test.c +4
-1{}, }; -static struct kunit_suite initramfs_test_suite = { +static int __init initramfs_test_init (struct kunit *test) { +/* +* unpack_to_rootfs() uses module-static state (victim, byte_count, +* state, ...). The boot-time async do_populate_rootfs() may still be init/initramfs_test.c static struct kunit_case __refdata initramfs_test_cases[] = { · line 529
Quoted 10 lines
init/initramfs_test.c +9
-0+* running, so wait for it to finish before we call unpack_to_rootfs() +* from the test thread, otherwise the two writers race and crash. +*/ +wait_for_initramfs (); +return 0 ; +} + +static struct kunit_suite __refdata initramfs_test_suite = { .name ="initramfs" , + .init = initramfs_test_init,The commit message notes this is added so the async unpack is quiescent "before the first case runs". Should this be using .suite_init instead of .init?
Quoted 7 lines
The .init callback is invoked by KUnit before every individual test case, meaning
wait_for_initramfs()will be called once for each of the test cases.While functionally harmless since subsequent calls return instantly, .suite_init is designed for one-time suite-level initialization and seems to better match the intent.
Good, it makes sense indeed.
---
Cheers,
Justin He(Jia He)
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
PATCH 2/7 kfence: kunit: skip when no pool is available
In reply to Jia HeWhen KFENCE is compiled in but disabled at boot (KFENCE_SAMPLE_INTERVAL=0) or __kfence_pool is not allocated, every kfence kunit case fails with -EINVAL.
Use kunit_skip() so they are reported as skipped instead.
To: Alexander Potapenko <[email protected]>
To: Marco Elver <[email protected]>
To: Dmitry Vyukov <[email protected]>
To: Andrew Morton <[email protected]>
Cc: [email protected]
Cc: [email protected]
--- mm/kfence/kfence_test.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/kfence/kfence_test.c +4 -2
@@ -822,8 +822,10 @@ static int test_init(struct kunit *test)unsigned long flags;int i; -if (!__kfence_pool) -return -EINVAL ; +if (!__kfence_pool) { +kunit_skip (test,"kfence pool not allocated or kfence not enabled" ); +return 0 ; + }spin_lock_irqsave (&observed.lock, flags);for (i =0 ; i <ARRAY_SIZE (observed.lines); i++)
Signature · 1 line
2.34.1
Re: [PATCH 2/7] kfence: kunit: skip when no pool is available
Quoted 7 lines · Jia He
When KFENCE is compiled in but disabled at boot (KFENCE_SAMPLE_INTERVAL=0) or
__kfence_poolis not allocated, every kfence kunit case fails with-EINVAL.Use
kunit_skip()so they are reported as skipped instead.
Quoted 8 lines
To: Alexander Potapenko <[email protected]>
To: Marco Elver <[email protected]>
To: Dmitry Vyukov <[email protected]>
To: Andrew Morton <[email protected]>
Cc: [email protected]
Cc: [email protected]Signed-off-by Jia He <[email protected]>
Fair enough - typically this failure was a way to know something went wrong in the test setup. But I also understand that it's not always possible to just not load this test if kfence is not enabled, say with some compliance test suites or such.
Quoted 24 lines
--- mm/kfence/kfence_test.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/kfence/kfence_test.c +4
-3@@ -822,8 +822,10 @@ static int test_init(struct kunit *test)unsigned long flags;int i; -if (!__kfence_pool) -return -EINVAL ; +if (!__kfence_pool) { +kunit_skip (test,"kfence pool not allocated or kfence not enabled" ); +return 0 ; + }spin_lock_irqsave (&observed.lock, flags);for (i =0 ; i <ARRAY_SIZE (observed.lines); i++) --2.34.1
PATCH 3/7 lib/math/int_log: drop WARN_ON for value == 0
In reply to Jia Heintlog2(0) and intlog10(0) already return 0, and the math-int_log kunit suite passes {0, 0} as a valid input. The leftover WARN_ON(1) on that path produces a backtrace per test run. Just remove it.
--- lib/math/int_log.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
lib/math/int_log.c +2 -6
@@ -59,10 +59,8 @@ unsigned int intlog2(u32 value)unsigned int significand;unsigned int interpolation; -if (unlikely (value ==0 )) { -WARN_ON (1 ); +if (unlikely (value ==0 ))return 0 ; - }/* first detect the msb (count begins at 0) */ msb =fls (value) -1 ;
@@ -116,10 +114,8 @@ unsigned int intlog10(u32 value)*/ u64 log; -if (unlikely (value ==0 )) { -WARN_ON (1 ); +if (unlikely (value ==0 ))return 0 ; - } log =intlog2 (value);
Signature · 1 line
2.34.1
PATCH 4/7 kunit: platform: catch duplicate (name, id) in kunit_platform_device_add()
In reply to Jia Hekunit_platform_device_add_twice_fails_test registers the same device twice and expects failure. Without an early check the second platform_device_add() reaches sysfs_create_dir_ns() and dumps a backtrace via sysfs_warn_dup() on every run.
Walk the platform bus for a device with matching name and id and return -EEXIST. PLATFORM_DEVID_AUTO assigns its id inside add() and can't collide, so skip it.
To: Brendan Higgins <[email protected]>
To: David Gow <[email protected]>
To: Rae Moar <[email protected]>
Cc: [email protected]
Cc: [email protected]
--- lib/kunit/platform.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)
lib/kunit/platform.c +31 -0
@@ -7,6 +7,7 @@#include <linux/device/bus.h> #include <linux/device/driver.h> #include <linux/platform_device.h> +#include <linux/string.h> #include <kunit/platform_device.h> #include <kunit/resource.h>
@@ -80,6 +81,20 @@ kunit_platform_device_alloc_match(struct kunit *test,KUNIT_DEFINE_ACTION_WRAPPER (platform_device_unregister_wrapper, platform_device_unregister, struct platform_device *); + +struct kunit_pdev_dup_match { +const char *name; +int id; +}; + +static int kunit_pdev_dup_match_fn (struct device *dev,void *data) +{ +struct kunit_pdev_dup_match *m = data; +struct platform_device *p =to_platform_device (dev); + +return p->id == m->id && p->name && !strcmp (p->name, m->name); +} +/** * kunit_platform_device_add() - Register a KUnit test managed platform device * @test: test context
@@ -95,6 +110,22 @@ int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev)struct kunit_resource *res;int ret; +/* +* Detect duplicate (name, id) registrations early, before +* platform_device_add() reaches sysfs_create_dir_ns() and +* unconditionally dumps a stack trace via sysfs_warn_dup(). This keeps +* tests that intentionally exercise the duplicate-add failure path +* (e.g. kunit_platform_device_add_twice_fails_test) quiet without +* losing the negative return value they assert on. +*/ +if (pdev->name && pdev->id !=PLATFORM_DEVID_AUTO ) { +struct kunit_pdev_dup_match m = { pdev->name, pdev->id }; + +if (bus_for_each_dev (&platform_bus_type,NULL , &m, + kunit_pdev_dup_match_fn)) +return -EEXIST ; + } + ret =platform_device_add (pdev);if (ret)return ret;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 4/7] kunit: platform: catch duplicate (name, id) in kunit_platform_device_add()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Asymmetric error handling in `kunit_platform_device_add()` creates a double-free or memory leak…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Asymmetric error handling in kunit_platform_device_add() creates a double-free or memory leak trap for unmanaged devices.
- [Medium] The duplicate device check falsely rejects explicit device IDs that happen to match the internal ID of an auto-allocated device. --
commit eaa71e70d7a40c957322770b8d98b24b4def8073
Author: Jia He <[email protected]>
kunit: platform: catch duplicate (name, id) in kunit_platform_device_add()
This commit adds an early check in kunit_platform_device_add() to detect duplicate platform devices before they trigger a sysfs warning stack trace. It walks the platform bus to find matching names and IDs and returns -EEXIST if a collision is found.
Quoted 4 lines
lib/kunit/platform.c +0
-0
[ ... ]
Quoted 8 lines
lib/kunit/platform.c +4
-0@@ -80,6 +81,20 @@ kunit_platform_device_alloc_match(struct kunit *test,KUNIT_DEFINE_ACTION_WRAPPER (platform_device_unregister_wrapper, platform_device_unregister, struct platform_device *); + +struct kunit_pdev_dup_match { +const char *name; +int id;
lib/kunit/platform.c kunit_platform_device_alloc_match(struct kunit *test, · line 97
Quoted 10 lines
lib/kunit/platform.c +10
-0+}; + +static int kunit_pdev_dup_match_fn (struct device *dev,void *data) +{ +struct kunit_pdev_dup_match *m = data; +struct platform_device *p =to_platform_device (dev); + +return p->id == m->id && p->name && !strcmp (p->name, m->name); +} +
Could this match function falsely reject explicit device IDs that happen to match the internal ID of an auto-allocated device?
When platform_device_add() registers a device with PLATFORM_DEVID_AUTO, it allocates an ID and sets pdev->id_auto to true, appending an ".auto" suffix to its sysfs name.
If a KUnit test registers an explicit device that happens to share a name and ID with an existing auto-allocated device, this match function will flag a collision and return -EEXIST, even though their sysfs names are distinct and platform_device_add() would have succeeded.
Quoted 13 lines
lib/kunit/platform.c +6
-0/** * kunit_platform_device_add() - Register a KUnit test managed platform device * @test: test context @@ -95,6 +110,22 @@ int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev)struct kunit_resource *res;int ret; +/* +* Detect duplicate (name, id) registrations early, before +* platform_device_add() reaches sysfs_create_dir_ns() and +* unconditionally dumps a stack trace via sysfs_warn_dup(). This keeps +* tests that intentionally exercise the duplicate-add failure path +* (e.g. kunit_platform_device_add_twice_fails_test) quiet without
lib/kunit/platform.c int kunit_platform_device_add(struct kunit *test, struct platform_device *pdev) · line 128
Quoted 10 lines
lib/kunit/platform.c +10
-0+* losing the negative return value they assert on. +*/ +if (pdev->name && pdev->id !=PLATFORM_DEVID_AUTO ) { +struct kunit_pdev_dup_match m = { pdev->name, pdev->id }; + +if (bus_for_each_dev (&platform_bus_type,NULL , &m, + kunit_pdev_dup_match_fn)) +return -EEXIST ; + } +
Does this new error path create an ambiguous state for unmanaged devices on failure?
If kunit_platform_device_add() is called with an unmanaged device and succeeds here, but later fails during kunit_add_action_or_reset(), the action wrapper is automatically executed to unregister and free the device.
However, if it fails early in this new duplicate check, the device is not freed. The caller has no way to distinguish which step failed, meaning unconditionally calling platform_device_put() on error causes a double-free if it failed later, while not calling it leaks the platform device if it failed early.
Quoted 3 lines
lib/kunit/platform.c +0
-0ret =platform_device_add (pdev);if (ret)return ret;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH 5/7 misc: reject duplicate names in misc_register()
In reply to Jia HeThe miscdev kunit suite registers two miscdevices with the same name and expects -EEXIST. The second call currently goes all the way to sysfs_create_dir_ns(), which prints "cannot create duplicate filename" with a backtrace on every run.
Walk misc_list under misc_mtx, return -EEXIST on a name collision and free the just-allocated minor before returning.
To: Arnd Bergmann <[email protected]>
To: Greg Kroah-Hartman <[email protected]>
--- drivers/char/misc.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
drivers/char/misc.c +22 -0
@@ -248,6 +248,28 @@ int misc_register(struct miscdevice *misc) } } +/* +* Detect duplicate names up-front so the subsequent +* device_create_with_groups() does not trip +* sysfs_create_dir_ns()->sysfs_warn_dup(), which unconditionally +* dumps a stack trace. Both the existing miscdev kunit suite +* (miscdev_test_duplicate_name) and any caller racing on the same +* name would otherwise pollute dmesg on every -EEXIST. +*/ + { +struct miscdevice *c; + +list_for_each_entry (c, &misc_list, list) { +if (strcmp (c->name, misc->name) ==0 ) { +misc_minor_free (misc->minor); +if (is_dynamic) + misc->minor =MISC_DYNAMIC_MINOR ; + err = -EEXIST ; + goto out; + } + } + } + dev =MKDEV (MISC_MAJOR , misc->minor); misc->this_device =
Signature · 1 line
2.34.1
Greg Kroah-Hartman
maintainerRe: [PATCH 5/7] misc: reject duplicate names in misc_register()
Quoted 10 lines · Jia He
The miscdev kunit suite registers two miscdevices with the same name and expects
-EEXIST. The second call currently goes all the way tosysfs_create_dir_ns(), which prints "cannot create duplicate filename" with a backtrace on every run.Walk
misc_listundermisc_mtx, return-EEXISTon a name collision and free the just-allocated minor before returning.To: Arnd Bergmann <[email protected]>
To: Greg Kroah-Hartman <[email protected]>
This should be Cc: right?
Quoted 25 lines
Signed-off-by Jia He <[email protected]>--- drivers/char/misc.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)drivers/char/misc.c +11
-0@@ -248,6 +248,28 @@ int misc_register(struct miscdevice *misc) } } +/* +* Detect duplicate names up-front so the subsequent +* device_create_with_groups() does not trip +* sysfs_create_dir_ns()->sysfs_warn_dup(), which unconditionally +* dumps a stack trace. Both the existing miscdev kunit suite +* (miscdev_test_duplicate_name) and any caller racing on the same +* name would otherwise pollute dmesg on every -EEXIST. +*/ + { +struct miscdevice *c; +
drivers/char/misc.c int misc_register(struct miscdevice *misc) · line 271
Quoted 10 lines
drivers/char/misc.c +10
-0+list_for_each_entry (c, &misc_list, list) { +if (strcmp (c->name, misc->name) ==0 ) { +misc_minor_free (misc->minor); +if (is_dynamic) + misc->minor =MISC_DYNAMIC_MINOR ; + err = -EEXIST ; + goto out; + } + } + }
Don't do additional {} where not needed.
And as the current code works properly, we are relying on sysfs for the rejection, why do this now here? The stack dump is good, it shows the offending caller, so they can fix it up better. So why change this?
thanks,
greg k-h
RE: [PATCH 5/7] misc: reject duplicate names in misc_register()
Quoted 79 lines
-----Original Message-----
From: Greg Kroah-Hartman <[email protected]>
Sent: Thursday, May 14, 2026 3:00 PM
To: Justin He <[email protected]>
Cc: [email protected]; [email protected]; linux-perf- [email protected]; [email protected]; kunit- [email protected]; [email protected]; linux- [email protected]; Arnd Bergmann <[email protected]>; Alexander Viro <[email protected]>; Christian Brauner <[email protected]>; Jan Kara <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Namhyung Kim <[email protected]>; Mark Rutland <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Ian Rogers <[email protected]>; Adrian Hunter <[email protected]>; James Clark <[email protected]>; Brendan Higgins <[email protected]>; David Gow <[email protected]>; Rae Moar <[email protected]>; Alexander Potapenko <[email protected]>; Marco Elver <[email protected]>; Dmitry Vyukov <[email protected]>; Andrew Morton <[email protected]>; Paul E. McKenney <[email protected]>; Petr Mladek <[email protected]>; Kees Cook <[email protected]>; David Disseldorp <[email protected]>
Subject: Re: [PATCH 5/7] misc: reject duplicate names inmisc_register()Quoted 10 lines · Jia He
The miscdev kunit suite registers two miscdevices with the same name and expects
-EEXIST. The second call currently goes all the way tosysfs_create_dir_ns(), which prints "cannot create duplicate filename" with a backtrace on every run.Walk
misc_listundermisc_mtx, return-EEXISTon a name collision and free the just-allocated minor before returning.To: Arnd Bergmann <[email protected]>
To: Greg Kroah-Hartman <[email protected]>This should be Cc: right?
Quoted 20 lines
Signed-off-by Jia He <[email protected]>--- drivers/char/misc.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)drivers/char/misc.c index renamed from drivers/char/misc.c +0
-0
726516fb0a3b..d6ffa21ac495100644drivers/char/misc.c +6
-0@@ -248,6 +248,28 @@ int misc_register(struct miscdevice *misc) } } +/* +* Detect duplicate names up-front so the subsequent +* device_create_with_groups() does not trip +* sysfs_create_dir_ns()->sysfs_warn_dup(), which unconditionally +* dumps a stack trace. Both the existing miscdev kunit suite +* (miscdev_test_duplicate_name) and any caller racing on the same drivers/char/misc.c int misc_register(struct miscdevice *misc) · line 266
Quoted 10 lines
drivers/char/misc.c +10
-0+* name would otherwise pollute dmesg on every -EEXIST. +*/ + { +struct miscdevice *c; + +list_for_each_entry (c, &misc_list, list) { +if (strcmp (c->name, misc->name) ==0 ) { +misc_minor_free (misc->minor); +if (is_dynamic) + misc->minor =
MISC_DYNAMIC_MINOR;drivers/char/misc.c int misc_register(struct miscdevice *misc) · line 271
Quoted 5 lines
drivers/char/misc.c +5
-0+ err = -EEXIST ; + goto out; + } + } + }Don't do additional {} where not needed.
Quoted 4 lines
And as the current code works properly, we are relying on sysfs for the rejection, why do this now here? The stack dump is good, it shows the offending caller, so they can fix it up better. So why change this?
Thanks for the feedback.
I’ll drop the two patches that silence the sysfs warning for duplicate filenames.
---
Cheers,
Justin He(Jia He)
[email protected]
AI reviewRe: [PATCH 5/7] misc: reject duplicate names in misc_register()
In reply to Jia HeThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Unchecked strcmp() introduces a NULL pointer dereference kernel panic if a miscdevice is…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unchecked strcmp() introduces a NULL pointer dereference kernel panic if a miscdevice is registered with a NULL name. --
commit 45aa5c1645ea4c93820557b37841fd395426656c
Author: Jia He <[email protected]>
misc: reject duplicate names in misc_register()
This commit introduces a check in misc_register() to prevent registering duplicate miscdevice names, returning -EEXIST early. It aims to avoid triggering a backtrace from sysfs_create_dir_ns() when a duplicate name is encountered.
Quoted 3 lines
drivers/char/misc.c +0
-0
[ ... ]
drivers/char/misc.c int misc_register(struct miscdevice *misc) · line 263
Quoted 5 lines
drivers/char/misc.c +5
-0+ { +struct miscdevice *c; + +list_for_each_entry (c, &misc_list, list) { +if (strcmp (c->name, misc->name) ==0 ) {
Could this lead to a NULL pointer dereference if a caller passes a miscdevice with a NULL name? For instance, if a driver fails to allocate a name string via kasprintf() and passes NULL without checking.
Before this change, the subsystem handled NULL names somewhat gracefully. device_create_with_groups() safely formats NULL pointers as "(null)" via %s, and misc_seq_show() explicitly protects against them when reading /proc/misc.
Additionally, if a device with a NULL name is successfully registered (such as being the first device when the list is empty), would subsequent calls to misc_register() by other drivers crash when this loop evaluates the NULL c->name?
Quoted 8 lines
drivers/char/misc.c +8
-0+misc_minor_free (misc->minor); +if (is_dynamic) + misc->minor =MISC_DYNAMIC_MINOR ; + err = -EEXIST ; + goto out; + } + } + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH 6/7 hw_breakpoint_test: fix test_many_cpus failure on large systems
In reply to Jia HeOn systems with many CPUs (e.g. 128 cores x 4 HW breakpoint slots = 512 = MAX_TEST_BREAKPOINTS), test_many_cpus() advances idx to MAX_TEST_BREAKPOINTS after the last fill_bp_slots(). The subsequent register_test_bp() call hits WARN_ON(idx >= MAX_TEST_BREAKPOINTS), returns NULL, and the -ENOSPC expectation fails.
Bail out of the loop when idx reaches the limit. Earlier iterations already validate the NOSPC path on other CPUs.
To: Peter Zijlstra <[email protected]>
To: Ingo Molnar <[email protected]>
To: Arnaldo Carvalho de Melo <[email protected]>
To: Namhyung Kim <[email protected]>
To: Mark Rutland <[email protected]>
To: Alexander Shishkin <[email protected]>
To: Jiri Olsa <[email protected]>
To: Ian Rogers <[email protected]>
To: Adrian Hunter <[email protected]>
To: James Clark <[email protected]>
Cc: [email protected]
--- kernel/events/hw_breakpoint_test.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/events/hw_breakpoint_test.c +3 -0
@@ -137,6 +137,9 @@ static void test_many_cpus(struct kunit *test)for_each_online_cpu (cpu) {bool do_continue =fill_bp_slots (test, &idx, cpu,NULL ,0 ); +if (idx >=MAX_TEST_BREAKPOINTS ) +break ; +TEST_EXPECT_NOSPC (register_test_bp (cpu,NULL , idx));if (!do_continue)break ;
Signature · 1 line
2.34.1
Re: [PATCH 6/7] hw_breakpoint_test: fix test_many_cpus failure on large systems
Quoted 22 lines · Jia He
On systems with many CPUs (e.g. 128 cores x 4 HW breakpoint slots = 512 =
MAX_TEST_BREAKPOINTS),test_many_cpus()advances idx toMAX_TEST_BREAKPOINTSafter the lastfill_bp_slots(). The subsequentregister_test_bp()call hitsWARN_ON(idx >= MAX_TEST_BREAKPOINTS), returns NULL, and the-ENOSPCexpectation fails.Bail out of the loop when idx reaches the limit. Earlier iterations already validate the NOSPC path on other CPUs.
To: Peter Zijlstra <[email protected]>
To: Ingo Molnar <[email protected]>
To: Arnaldo Carvalho de Melo <[email protected]>
To: Namhyung Kim <[email protected]>
To: Mark Rutland <[email protected]>
To: Alexander Shishkin <[email protected]>
To: Jiri Olsa <[email protected]>
To: Ian Rogers <[email protected]>
To: Adrian Hunter <[email protected]>
To: James Clark <[email protected]>
Cc: [email protected]
Quoted 1 line
Signed-off-by Jia He <[email protected]>
Quoted 21 lines
--- kernel/events/hw_breakpoint_test.c | 3 +++ 1 file changed, 3 insertions(+)kernel/events/hw_breakpoint_test.c +3
-1@@ -137,6 +137,9 @@ static void test_many_cpus(struct kunit *test)for_each_online_cpu (cpu) {bool do_continue =fill_bp_slots (test, &idx, cpu,NULL ,0 ); +if (idx >=MAX_TEST_BREAKPOINTS ) +break ; +TEST_EXPECT_NOSPC (register_test_bp (cpu,NULL , idx));if (!do_continue)break ; --2.34.1
PATCH 7/7 lib/tests: test_ratelimit: fix stress test thread lifecycle and leak
In reply to Jia HeThe kthread return value was checked against the wrong variable (sktp instead of sktp[i].tp), so WARN_ON_ONCE(!sktp->tp) fired at line 87.
Replace kthread_run() with kthread_create()+wake_up_process() so the return value can be validated before assigning to sktp[i].tp. On creation failure, jump to a common cleanup path that signals doneflag, stops all already-started threads, and frees sktp. This avoids leaving orphan kthreads and leaked memory when a mid-loop failure occurs.
Also reset doneflag before spawning threads so back-to-back test invocations don't race on stale state.
To: "Paul E. McKenney" <[email protected]>
To: Petr Mladek <[email protected]>
To: Kees Cook <[email protected]>
--- lib/tests/test_ratelimit.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-)
lib/tests/test_ratelimit.c +26 -8
@@ -105,26 +105,44 @@ static void test_ratelimit_stress(struct kunit *test)const int n_stress_kthread =cpumask_weight (cpu_online_mask);struct stress_kthread skt = {0 };struct stress_kthread *sktp =kzalloc_objs (*sktp, n_stress_kthread); +int n_started =0 ; -KUNIT_EXPECT_NOT_NULL_MSG (test, sktp,"Memory allocation failure" ); +KUNIT_ASSERT_NOT_NULL_MSG (test, sktp,"Memory allocation failure" ); +WRITE_ONCE (doneflag,0 );for (i =0 ; i < n_stress_kthread; i++) { - sktp[i].tp =kthread_run (test_ratelimit_stress_child, &sktp[i],"%s/%i" , -"test_ratelimit_stress_child" , i); -KUNIT_EXPECT_NOT_NULL_MSG (test, sktp,"kthread creation failure" ); +struct task_struct *tp; + + tp =kthread_create (test_ratelimit_stress_child, &sktp[i], +"%s/%i" ,"test_ratelimit_stress_child" , i); +if (IS_ERR (tp)) { +KUNIT_FAIL (test,"kthread_create failed: %ld" ,PTR_ERR (tp)); + goto out_stop; + } + + sktp[i].tp = tp; +wake_up_process (tp); + n_started++;pr_alert ("Spawned test_ratelimit_stress_child %d\n" , i); }schedule_timeout_idle (stress_duration); + +out_stop:WRITE_ONCE (doneflag,1 ); -for (i =0 ; i < n_stress_kthread; i++) { +for (i =0 ; i < n_started; i++) {kthread_stop (sktp[i].tp); skt.nattempts += sktp[i].nattempts; skt.nunlimited += sktp[i].nunlimited; skt.nlimited += sktp[i].nlimited; skt.nmissed += sktp[i].nmissed; } -KUNIT_ASSERT_EQ_MSG (test, skt.nunlimited + skt.nlimited, skt.nattempts, -"Outcomes not equal to attempts" ); -KUNIT_ASSERT_EQ_MSG (test, skt.nlimited, skt.nmissed,"Misses not equal to limits" ); +if (n_started == n_stress_kthread) { +KUNIT_ASSERT_EQ_MSG (test, skt.nunlimited + skt.nlimited, skt.nattempts, +"Outcomes not equal to attempts" ); +KUNIT_ASSERT_EQ_MSG (test, skt.nlimited, skt.nmissed, +"Misses not equal to limits" ); + } + +kfree (sktp); }static struct kunit_case ratelimit_test_cases[] = {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 7/7] lib/tests: test_ratelimit: fix stress test thread lifecycle and leak
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Use-After-Free in kthread_stop() due to uncontrolled child thread exit --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Use-After-Free in kthread_stop() due to uncontrolled child thread exit --
commit cb9c7c9c5b3cf7b61d434d8dc2e4e72ebc4cfd3a
Author: Jia He <[email protected]>
lib/tests: test_ratelimit: fix stress test thread lifecycle and leak
This commit changes the stress test to use kthread_create() and wake_up_process() instead of kthread_run() to properly handle creation failures. It adds an out_stop label to clean up successfully started threads if a subsequent thread fails to start, and resets doneflag before spawning threads.
Quoted 4 lines
lib/tests/test_ratelimit.c +0
-0
[ ... ]
Quoted 1 line
lib/tests/test_ratelimit.c +0
-0@@ -105,26 +105,44 @@ static void test_ratelimit_stress(struct kunit *test)
[ ... ]
lib/tests/test_ratelimit.c static void test_ratelimit_stress(struct kunit *test) · line 132
Quoted 7 lines
lib/tests/test_ratelimit.c +3
-1schedule_timeout_idle (stress_duration); + +out_stop:WRITE_ONCE (doneflag,1 ); -for (i =0 ; i < n_stress_kthread; i++) { +for (i =0 ; i < n_started; i++) {kthread_stop (sktp[i].tp);
Could this trigger a use-after-free if the child threads exit before kthread_stop() is called?
When WRITE_ONCE(doneflag, 1) is executed, the child threads running test_ratelimit_stress_child() observe the flag and return, which invokes do_exit() and drops the task_struct reference.
If a child thread exits completely before the parent loop reaches its kthread_stop() call, kthread_stop() will attempt to access a freed task_struct. This appears to affect both the new error path and the normal teardown path.
Would it be safer for the parent to take an explicit reference via get_task_struct() during creation and use kthread_stop_put(), or alternatively, have the children spin on kthread_should_stop() instead of exiting immediately on doneflag?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
RE: [PATCH 7/7] lib/tests: test_ratelimit: fix stress test thread lifecycle and leak
Quoted 53 lines
-----Original Message-----
From: [email protected] <[email protected]>
Sent: Friday, May 15, 2026 2:18 AM
To: Justin He <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH 7/7] lib/tests:test_ratelimit: fix stress test thread lifecycle and leakThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Use-After-Free inkthread_stop()due to uncontrolled child thread exit --commit
cb9c7c9c5b3cf7b61d434d8dc2e4e72ebc4cfd3a
Author: Jia He <[email protected]>lib/tests:
test_ratelimit: fix stress test thread lifecycle and leakThis commit changes the stress test to use
kthread_create()andwake_up_process()instead ofkthread_run()to properly handle creation failures. It adds anout_stoplabel to clean up successfully started threads if a subsequent thread fails to start, and resets doneflag before spawning threads.Quoted 4 lines
lib/tests/test_ratelimit.c +0
-0[ ... ]
Quoted 2 lines
lib/tests/test_ratelimit.c +0
-0@@ -105,26 +105,44 @@ static void test_ratelimit_stress(struct kunit *test)[ ... ]
lib/tests/test_ratelimit.c static void test_ratelimit_stress(struct kunit *test) · line 132
Quoted 7 lines
schedule_timeout_idle(stress_duration);lib/tests/test_ratelimit.c +3
-1+ +out_stop:WRITE_ONCE (doneflag,1 ); -for (i =0 ; i < n_stress_kthread; i++) { +for (i =0 ; i < n_started; i++) {kthread_stop (sktp[i].tp);Could this trigger a use-after-free if the child threads exit before
kthread_stop()is called?When
WRITE_ONCE(doneflag, 1)is executed, the child threads runningtest_ratelimit_stress_child()observe the flag and return, which invokesdo_exit()and drops thetask_structreference.If a child thread exits completely before the parent loop reaches its
kthread_stop()call,kthread_stop()will attempt to access a freedtask_struct. This appears to affect both the new error path and the normal teardown path.
Quoted 4 lines
Would it be safer for the parent to take an explicit reference via
get_task_struct()during creation and usekthread_stop_put(), or alternatively, have the children spin onkthread_should_stop()instead of exiting immediately on doneflag?
Agree, I will sent v2 if no more comments/objections as sashiko bot suggested.
---
Cheers,
Justin He(Jia He)
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Greg Kroah-Hartman
maintainerQuoted 7 lines · Jia He
Running the full kunit suite on arm64 (128-core Neoverse N2) with 7.1-rc3+ produces a handful of backtraces that are not real kernel bugs but rather test-infrastructure issues: races with boot-time code, missing bounds checks, resource leaks, and sysfs duplicate-name splats.
This series fixes them one by one so that a clean kunit run no longer leaves noise in dmesg:
This series was sent to too many different people at once. Always break your changes up into subsystem-specific series as no one can take all of these through their tree, and our tools, as you know, have a hard time of just picking one patch out of a series.
thanks,
greg k-h
Quoted 33 lines
-----Original Message-----
From: Greg Kroah-Hartman <[email protected]>
Sent: Thursday, May 14, 2026 3:02 PM
To: Justin He <[email protected]>
Cc: [email protected]; [email protected]; linux-perf- [email protected]; [email protected]; kunit- [email protected]; [email protected]; linux- [email protected]; Arnd Bergmann <[email protected]>; Alexander Viro <[email protected]>; Christian Brauner <[email protected]>; Jan Kara <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar <[email protected]>; Arnaldo Carvalho de Melo <[email protected]>; Namhyung Kim <[email protected]>; Mark Rutland <[email protected]>; Alexander Shishkin <[email protected]>; Jiri Olsa <[email protected]>; Ian Rogers <[email protected]>; Adrian Hunter <[email protected]>; James Clark <[email protected]>; Brendan Higgins <[email protected]>; David Gow <[email protected]>; Rae Moar <[email protected]>; Alexander Potapenko <[email protected]>; Marco Elver <[email protected]>; Dmitry Vyukov <[email protected]>; Andrew Morton <[email protected]>; Paul E. McKenney <[email protected]>; Petr Mladek <[email protected]>; Kees Cook <[email protected]>; David Disseldorp <[email protected]>
Subject: Re: [PATCH 0/7] Silence spurious warnings and crashes from kunit test suitesQuoted 5 lines · Jia He
Running the full kunit suite on arm64 (128-core Neoverse N2) with 7.1-rc3+ produces a handful of backtraces that are not real kernel bugs but rather test-infrastructure issues: races with boot-time code, missing bounds checks, resource leaks, and sysfs duplicate-name splats.
Quoted 8 lines
Quoted 2 lines
This series fixes them one by one so that a clean kunit run no longer leaves noise in dmesg:
This series was sent to too many different people at once. Always break your changes up into subsystem-specific series as no one can take all of these through their tree, and our tools, as you know, have a hard time of just picking one patch out of a series.
Ok, thanks for the feedback. I understand the concern.
I’ll split the changes into subsystem‑specific series and resend them accordingly.
---
Cheers,
Justin He(Jia He)