The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Praneeth Balasubramanian <praneethb2002@gmail.com>
To: Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Praneeth Balasubramanian <praneethb2002@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH 2/2] tracing: Hold trace_event_read_lock() in ftrace_dump() when possible
Date: Fri,  4 Dec 2026 10:49:58 -0800	[thread overview]
Message-ID: <20261204184958.1687218-2-praneethb2002@gmail.com> (raw)
In-Reply-To: <20261204184958.1687218-1-praneethb2002@gmail.com>

ftrace_dump_one() formats each entry with print_trace_line(), which
looks up the entry's event with ftrace_find_event(). The comment above
ftrace_find_event() says that it must be called with
trace_event_read_lock() held, and the trace and trace_pipe readers take
it, but the dump does not. If a dynamic event is removed during a dump,
for example by writing "-:group/event" to kprobe_events, the event is
unregistered under trace_event_sem and then freed while the dump may
still be using it.

ftrace_dump() runs from the oops, panic and SysRq paths and must not
sleep; SysRq handlers even run under rcu_read_lock(). So add
trace_event_read_trylock(), take the lock with it in task context only,
and dump without it, as before, when it is not available, for example
in NMI context or when the crashing task holds trace_event_sem.

The missing lock was found by a static check that turns the locking rule
in a function's comment into a Clang context-analysis requirement on its
callers.

Fixes: 4f5359685af6 ("tracing: add trace_event_read_lock()")
Cc: stable@vger.kernel.org
Signed-off-by: Praneeth Balasubramanian <praneethb2002@gmail.com>
---

Notes:
    This was found by a compiler-based check, part of research at UCSB SecLab.
    The locking rule stated in the kernel-doc comment of the called function
    was turned into a Clang context-analysis requirement (__must_hold) on that
    function, and Clang 23 with CONFIG_WARN_CONTEXT_ANALYSIS then checked every
    caller. An LLM was used only to turn the comment into a machine-readable
    rule; the report itself comes from the compiler. The patch and changelog
    were drafted with Claude Opus 5.5. I reviewed the patch and take
    responsibility for it.
    
    Testing: builds without new warnings with W=1 (x86_64, clang 23,
    allmodconfig). Not runtime-tested, and I have not observed the race
    myself.

 kernel/trace/trace.c        | 13 +++++++++++++
 kernel/trace/trace_output.c |  6 ++++++
 kernel/trace/trace_output.h |  1 +
 3 files changed, 20 insertions(+)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index faa17589df70..754bbe55666f 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -9495,6 +9495,7 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m
 	static struct trace_iterator iter;
 	unsigned int old_userobj;
 	unsigned long flags;
+	bool event_locked;
 	int cnt = 0;
 
 	/*
@@ -9507,6 +9508,15 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m
 	 */
 	tracer_tracing_off(tr);
 
+	/*
+	 * print_trace_line() looks up the event of each entry, which needs
+	 * trace_event_read_lock() so that the event is not unregistered and
+	 * freed meanwhile. A dump must not sleep and can run in any context,
+	 * so only try the lock, and only in task context. If it is not
+	 * available, dump without it as before.
+	 */
+	event_locked = in_task() && trace_event_read_trylock();
+
 	local_irq_save(flags);
 
 	/* Simulate the iterator */
@@ -9574,6 +9584,9 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m
 
 	tracer_tracing_enable(tr);
 	local_irq_restore(flags);
+
+	if (event_locked)
+		trace_event_read_unlock();
 }
 
 /*
diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c
index a5ad76175d10..c9118f6d7be0 100644
--- a/kernel/trace/trace_output.c
+++ b/kernel/trace/trace_output.c
@@ -864,6 +864,12 @@ void trace_event_read_unlock(void)
 	up_read(&trace_event_sem);
 }
 
+/* Returns true if the lock was taken; release it with trace_event_read_unlock() */
+bool trace_event_read_trylock(void)
+{
+	return down_read_trylock(&trace_event_sem);
+}
+
 /**
  * register_trace_event - register output for an event type
  * @event: the event type to register
diff --git a/kernel/trace/trace_output.h b/kernel/trace/trace_output.h
index 99b676733d46..620c6d8f5015 100644
--- a/kernel/trace/trace_output.h
+++ b/kernel/trace/trace_output.h
@@ -35,6 +35,7 @@ extern enum print_line_t print_event_fields(struct trace_iterator *iter,
 
 extern void trace_event_read_lock(void);
 extern void trace_event_read_unlock(void);
+extern bool trace_event_read_trylock(void);
 extern struct trace_event *ftrace_find_event(int type);
 
 extern enum print_line_t trace_nop_print(struct trace_iterator *iter,
-- 
2.25.1


      reply	other threads:[~2026-12-04 18:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:49 [PATCH 1/2] tracing: Keep an instance alive while ftrace_dump() dumps it Praneeth Balasubramanian
2026-12-04 18:49 ` Praneeth Balasubramanian [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204184958.1687218-2-praneethb2002@gmail.com \
    --to=praneethb2002@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox