Skip to contentSkip to conversation

tracing: Fix crash passing ERR_PTR to kthread_stop()

PATCH In review Hui Su 3 participants 3 messages Aug 17

Subsystems (2)
lore
Share & tools
lore

Hui Su

author
#0 lore raw

PATCH v2 1/1 tracing: Fix crash passing ERR_PTR to kthread_stop()

event_test_stuff() calls kthread_run() and unconditionally passes the returned task_struct pointer to kthread_stop(). kthread_run() returns an error pointer such as ERR_PTR(-ENOMEM) when kthread creation fails, for example under memory pressure during the boot-time event self-test. kthread_stop() then dereferences the invalid pointer, crashing the kernel.

Check the result of kthread_run() before passing it to kthread_stop(). Use WARN_ON() so that a failure to create the self-test thread does not go unnoticed, matching the ring-buffer self-test fix in commit 91542863abad ("ring-buffer: Fix crash passing ERR_PTR to kthread_stop()").

Fixes e6187007d6c3 ("tracing/events: add startup tests for events")

---

Changes in v2:
- Use WARN_ON(IS_ERR(test_thread)) so thread creation failures are not silently ignored, as suggested by Steven Rostedt.

v1: https://lore.kernel.org/r/[email protected]/

 kernel/trace/trace_events.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/trace/trace_events.c +2 -0
index 3650d84d4f16..28167b229419 100644
@@ -4901,6 +4901,8 @@ static __init void event_test_stuff(void)
 	struct task_struct *test_thread;
 
 	test_thread = kthread_run(event_test_thread, NULL, "test-events");
+	if (WARN_ON(IS_ERR(test_thread)))
+		return;
 	msleep(1);
 	kthread_stop(test_thread);
 }
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Using `WARN_ON` inside `event_test_stuff()` causes a massive warning storm and potential soft…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Using WARN_ON inside event_test_stuff() causes a massive warning storm and potential soft lockup if kthread creation repeatedly fails. --

--- Patch [1]: [PATCH v2] tracing: Fix crash passing ERR_PTR to kthread_stop() --- commit 692df106ab8d8e56701ad9c14c135871ee02df4a
Author: Hui Su <[email protected]>

tracing: Fix crash passing ERR_PTR to kthread_stop()

This commit adds a check for the return value of kthread_run() in event_test_stuff() to prevent passing an ERR_PTR to kthread_stop() if thread creation fails under memory pressure during boot-time trace self tests.

kernel/trace/trace_events.c static __init void event_test_stuff(void) · line 3870

Quoted 10 lines
kernel/trace/trace_events.c +2 -0
index 0356cae0cf74e..9e941e5805f5e 100644
@@ -3866,6 +3866,8 @@ static __init void event_test_stuff(void)
 	struct task_struct *test_thread;
 
 	test_thread = kthread_run(event_test_thread, NULL, "test-events");
+	if (WARN_ON(IS_ERR(test_thread)))
+		return;

[Severity: High]
Does this risk causing a console flood and soft lockup if thread creation persistently fails?

During boot-time trace self tests, event_test_stuff() is called within large loops spanning all trace events:

kernel/trace/trace_events.c:event_trace_self_tests() {
    ...
	list_for_each_entry(file, &tr->events, list) {
        ...
		ftrace_event_enable_disable(file, 1);
		event_test_stuff();
		ftrace_event_enable_disable(file, 0);
    ...
}

If kthread creation fails, WARN_ON prints a full stack trace and returns. Since event_test_stuff() returns void, the caller blindly continues to the next trace event, potentially leading to thousands of consecutive stack dumps.

Could this be changed to WARN_ON_ONCE() or modified to abort the test loop gracefully?

Quoted 3 lines
kernel/trace/trace_events.c +0 -0
 	msleep(1);
 	kthread_stop(test_thread);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
Quoted 7 lines · On Mon, 17 Aug 2026 12:15:32 +0000 [email protected] wrote:
Quoted 7 lines
kernel/trace/trace_events.c +0 -0
index 0356cae0cf74e..9e941e5805f5e 100644
@@ -3866,6 +3866,8 @@ static __init void event_test_stuff(void)
 	struct task_struct *test_thread;
 
Quoted 8 lines

kernel/trace/trace_events.c static __init void event_test_stuff(void) · line 4905

Quoted 3 lines
kernel/trace/trace_events.c +2 -0
 	test_thread = kthread_run(event_test_thread, NULL, "test-events");
+	if (WARN_ON(IS_ERR(test_thread)))
+		return;  

[Severity: High]
Does this risk causing a console flood and soft lockup if thread creation persistently fails?

If that happens than the system is doomed to begin with. We don't care here. This patch is good enough. It's for a debugging feature, not anything that would be run in production.

Just look at the name of the function: event_test_stuff()

This is where AI has a hard time with subtle requirements.

Thanks,

-- Steve