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 1/2] tracing: Keep an instance alive while ftrace_dump() dumps it
Date: Fri,  4 Dec 2026 10:49:57 -0800	[thread overview]
Message-ID: <20261204184958.1687218-1-praneethb2002@gmail.com> (raw)

ftrace_dump_by_param() looks up the instances named in
ftrace_dump_on_oops with trace_array_find(), whose comment says that
trace_types_lock must be held, and then dumps them with
ftrace_dump_one(). ftrace_dump() runs from the oops, panic and SysRq
paths and cannot take that mutex, so it holds neither the lock nor a
reference. If the instance is removed meanwhile (instance_rmdir() ->
__remove_instance()), the dump can follow a list entry that list_del()
has poisoned, or use a trace_array and buffers that have been freed.

ftrace_dump_one() does not sleep, so the dump can stay in an RCU
read-side critical section. Look the instance up with a new
trace_array_find_rcu() under rcu_read_lock() and stay in the critical
section until the instance has been dumped. On the update side, use
list_add_rcu() and list_del_rcu() for ftrace_trace_arrays, and always
wait for a grace period in __remove_instance() before the instance is
torn down. It already waited when the trace marker copy was enabled.

The unlocked lookup 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: 19f0423fd55c ("tracing: Support to dump instance traces by ftrace_dump_on_oops")
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 | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 4ccdefb5c3ef..faa17589df70 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -8765,7 +8765,7 @@ trace_array_create_systems(const char *name, const char *systems,
 	} else
 		__trace_early_add_events(tr);
 
-	list_add(&tr->list, &ftrace_trace_arrays);
+	list_add_rcu(&tr->list, &ftrace_trace_arrays);
 
 	tr->ref++;
 
@@ -8890,14 +8890,20 @@ static int __remove_instance(struct trace_array *tr)
 	if (tr->ref > 1 || (tr->current_trace && tr->trace_ref))
 		return -EBUSY;
 
-	list_del(&tr->list);
+	list_del_rcu(&tr->list);
 
 	if (printk_trace == tr)
 		update_printk_trace(&global_trace);
 
 	/* Must be done before disabling all the flags */
-	if (update_marker_trace(tr, 0))
-		synchronize_rcu();
+	update_marker_trace(tr, 0);
+
+	/*
+	 * Wait for RCU readers that may still use tr: the trace marker
+	 * copies, and ftrace_dump(), which finds instances without taking
+	 * trace_types_lock.
+	 */
+	synchronize_rcu();
 
 	/* Disable all the flags that were enabled coming in */
 	for (i = 0; i < TRACE_FLAGS_MAX_SIZE; i++) {
@@ -9570,6 +9576,23 @@ static void ftrace_dump_one(struct trace_array *tr, enum ftrace_dump_mode dump_m
 	local_irq_restore(flags);
 }
 
+/*
+ * Like trace_array_find(), for ftrace_dump(), which cannot take
+ * trace_types_lock. Must be called under rcu_read_lock(), and the
+ * returned trace array may only be used until rcu_read_unlock().
+ */
+static struct trace_array *trace_array_find_rcu(const char *instance)
+{
+	struct trace_array *tr;
+
+	list_for_each_entry_rcu(tr, &ftrace_trace_arrays, list) {
+		if (tr->name && strcmp(tr->name, instance) == 0)
+			return tr;
+	}
+
+	return NULL;
+}
+
 static void ftrace_dump_by_param(void)
 {
 	bool first_param = true;
@@ -9597,8 +9620,14 @@ static void ftrace_dump_by_param(void)
 		}
 
 		inst_name = strsep(&token, "=");
-		tr = trace_array_find(inst_name);
+		/*
+		 * trace_types_lock cannot be taken here; RCU keeps the
+		 * instance from being freed until the dump is done.
+		 */
+		rcu_read_lock();
+		tr = trace_array_find_rcu(inst_name);
 		if (!tr) {
+			rcu_read_unlock();
 			printk(KERN_TRACE "Instance %s not found\n", inst_name);
 			continue;
 		}
@@ -9608,6 +9637,7 @@ static void ftrace_dump_by_param(void)
 			ftrace_dump_one(tr, DUMP_ORIG);
 		else
 			ftrace_dump_one(tr, DUMP_ALL);
+		rcu_read_unlock();
 	}
 }
 
-- 
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 Praneeth Balasubramanian [this message]
2026-12-04 18:49 ` [PATCH 2/2] tracing: Hold trace_event_read_lock() in ftrace_dump() when possible Praneeth Balasubramanian

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-1-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