Linux Trace Kernel
 help / color / mirror / Atom feed
From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Antoine Tenart <atenart@kernel.org>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] tracing/probes: Guard kprobe and fprobe dispatchers with tracepoint_srcu
Date: Sat, 05 Dec 2026 01:22:10 +0900	[thread overview]
Message-ID: <20261204161150.21170-1-mhiramat@kernel.org> (raw)

Since commit a46023d5616e ("tracing: Guard __DECLARE_TRACE() use of
__DO_TRACE_CALL() with SRCU-fast"), tracepoint_synchronize_unregister()
waits only on rcu_tasks_trace and tracepoint_srcu, and no longer waits
for a standard RCU grace period.

However, kprobe, kretprobe, fentry, and fexit event dispatchers still
execute only under standard RCU protection. Building on top of the
trace_probe_remove_file() call_rcu() reference-counting fix and
RCU-deferred event filter freeing, teardown paths that synchronize via
tracepoint_synchronize_unregister() -- such as event_trace_del_tracer(),
__ftrace_clear_event_pids(), trigger_data_free(), and
perf_trace_event_unreg() -- can still free trace_array (file->tr), ring
buffers, PID filter lists, trigger data, or perf state while a kprobe or
fprobe handler is running.

Guard the kprobe, kretprobe, fentry, and fexit dispatchers with
guard(srcu_fast_notrace)(&tracepoint_srcu) so that probe execution is
synchronized with tracepoint_synchronize_unregister() while keeping
standard RCU protection for call_rcu()-freed objects and avoiding an
inline synchronize_rcu() in trace_probe_remove_file().

Before entering tracepoint_srcu (which uses SRCU-fast and relies on
synchronize_rcu() instead of a read-side smp_mb()), verify that
rcu_is_watching() and cpu_online(raw_smp_processor_id()) are true so
probes hitting functions in RCU-idle regions or after
rcutree_report_cpu_dead() do not race with synchronize_srcu().

NOTE: Uprobe and uretprobe dispatchers are intentionally not guarded
with tracepoint_srcu: all callers in kernel/events/uprobes.c already
hold rcu_read_lock_trace(), which tracepoint_synchronize_unregister()
waits on via synchronize_rcu_tasks_trace(), and uprobe handlers perform
faultable user-memory accesses and sleepable BPF execution that must
not stall non-faultable tracepoint_srcu grace periods.

Also update the stale synchronization comments in disable_trace_kprobe(),
disable_trace_fprobe(), and disable_trace_eprobe().

Fixes: a46023d5616e ("tracing: Guard __DECLARE_TRACE() use of __DO_TRACE_CALL() with SRCU-fast")
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
Changes in v2:
 - Drop tracepoint_srcu from uprobe/uretprobe dispatchers since
   uprobes already hold rcu_read_lock_trace().
 - Add trace_probe_can_trace() checking rcu_is_watching() and
   cpu_online() before acquiring tracepoint_srcu.
 - Clarify disable_trace_*() comments and note prerequisite patches:
   https://lore.kernel.org/all/20261201155010.3f9d268d@gandalf.local.home/
   https://lore.kernel.org/r/20261202162333.168965-1-atenart@kernel.org

 kernel/trace/trace_eprobe.c |  6 +++++-
 kernel/trace/trace_fprobe.c | 24 ++++++++++++++++++------
 kernel/trace/trace_kprobe.c | 21 ++++++++++++++++-----
 kernel/trace/trace_probe.h  | 11 +++++++++++
 4 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/kernel/trace/trace_eprobe.c b/kernel/trace/trace_eprobe.c
index f65393fb1c10..0479f0c9b3b3 100644
--- a/kernel/trace/trace_eprobe.c
+++ b/kernel/trace/trace_eprobe.c
@@ -711,7 +711,11 @@ static int disable_trace_eprobe(struct trace_event_call *call,
 	for_each_trace_eprobe_tp(ep, tp)
 		disable_eprobe(ep, file->tr);
 
-	/* Synchronization is done in below function. */
+	/*
+	 * disable_eprobe() synchronizes via tracepoint_synchronize_unregister(),
+	 * and trace_probe_remove_file() defers dropping its reference to file
+	 * and freeing link via call_rcu().
+	 */
 	trace_probe_remove_file(tp, file);
 
 	return 0;
diff --git a/kernel/trace/trace_fprobe.c b/kernel/trace/trace_fprobe.c
index 9ab0871a6461..9f30e2c280ed 100644
--- a/kernel/trace/trace_fprobe.c
+++ b/kernel/trace/trace_fprobe.c
@@ -519,9 +519,14 @@ static int fentry_dispatcher(struct fprobe *fp, unsigned long entry_ip,
 			     void *entry_data)
 {
 	struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp);
-	unsigned int flags = trace_probe_load_flag(&tf->tp);
+	unsigned int flags;
 	int ret = 0;
 
+	if (!trace_probe_can_trace())
+		return 0;
+
+	guard(srcu_fast_notrace)(&tracepoint_srcu);
+	flags = trace_probe_load_flag(&tf->tp);
 	if (flags & TP_FLAG_TRACE)
 		fentry_trace_func(tf, entry_ip, fregs);
 
@@ -538,8 +543,13 @@ static void fexit_dispatcher(struct fprobe *fp, unsigned long entry_ip,
 			     void *entry_data)
 {
 	struct trace_fprobe *tf = container_of(fp, struct trace_fprobe, fp);
-	unsigned int flags = trace_probe_load_flag(&tf->tp);
+	unsigned int flags;
+
+	if (!trace_probe_can_trace())
+		return;
 
+	guard(srcu_fast_notrace)(&tracepoint_srcu);
+	flags = trace_probe_load_flag(&tf->tp);
 	if (flags & TP_FLAG_TRACE)
 		fexit_trace_func(tf, entry_ip, ret_ip, fregs, entry_data);
 #ifdef CONFIG_PERF_EVENTS
@@ -1530,10 +1540,12 @@ static int disable_trace_fprobe(struct trace_event_call *call,
  out:
 	if (file)
 		/*
-		 * Synchronization is done in below function. For perf event,
-		 * file == NULL and perf_trace_event_unreg() calls
-		 * tracepoint_synchronize_unregister() to ensure synchronize
-		 * event. We don't need to care about it.
+		 * trace_probe_remove_file() unlinks the link and defers
+		 * dropping its reference to file and freeing link via
+		 * call_rcu(). Teardown of trace_array, triggers, PID
+		 * filters, and perf events (e.g. event_trace_del_tracer(),
+		 * perf_trace_event_unreg()) synchronizes with in-flight
+		 * dispatchers via tracepoint_synchronize_unregister().
 		 */
 		trace_probe_remove_file(tp, file);
 
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index 1dd6a6942eb7..5ee9fcb84ad3 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -437,10 +437,12 @@ static int disable_trace_kprobe(struct trace_event_call *call,
  out:
 	if (file)
 		/*
-		 * Synchronization is done in below function. For perf event,
-		 * file == NULL and perf_trace_event_unreg() calls
-		 * tracepoint_synchronize_unregister() to ensure synchronize
-		 * event. We don't need to care about it.
+		 * trace_probe_remove_file() unlinks the link and defers
+		 * dropping its reference to file and freeing link via
+		 * call_rcu(). Teardown of trace_array, triggers, PID
+		 * filters, and perf events (e.g. event_trace_del_tracer(),
+		 * perf_trace_event_unreg()) synchronizes with in-flight
+		 * dispatchers via tracepoint_synchronize_unregister().
 		 */
 		trace_probe_remove_file(tp, file);
 
@@ -1834,11 +1836,16 @@ static int kprobe_register(struct trace_event_call *event,
 static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
 {
 	struct trace_kprobe *tk = container_of(kp, struct trace_kprobe, rp.kp);
-	unsigned int flags = trace_probe_load_flag(&tk->tp);
+	unsigned int flags;
 	int ret = 0;
 
 	raw_cpu_inc(*tk->nhit);
 
+	if (!trace_probe_can_trace())
+		return 0;
+
+	guard(srcu_fast_notrace)(&tracepoint_srcu);
+	flags = trace_probe_load_flag(&tk->tp);
 	if (flags & TP_FLAG_TRACE)
 		kprobe_trace_func(tk, regs);
 #ifdef CONFIG_PERF_EVENTS
@@ -1867,6 +1874,10 @@ kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
 	tk = container_of(rp, struct trace_kprobe, rp);
 	raw_cpu_inc(*tk->nhit);
 
+	if (!trace_probe_can_trace())
+		return 0;
+
+	guard(srcu_fast_notrace)(&tracepoint_srcu);
 	flags = trace_probe_load_flag(&tk->tp);
 	if (flags & TP_FLAG_TRACE)
 		kretprobe_trace_func(tk, ri, regs);
diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h
index aecc9f24951d..1d34df1ca10d 100644
--- a/kernel/trace/trace_probe.h
+++ b/kernel/trace/trace_probe.h
@@ -284,6 +284,17 @@ struct event_file_link {
 	struct rcu_head			rcu;
 };
 
+/*
+ * Check that this CPU is tracked by RCU before entering SRCU-fast
+ * (tracepoint_srcu) or RCU read-side critical sections. Checking
+ * cpu_online() is also required because rcutree_report_cpu_dead()
+ * leaves CT_RCU_WATCHING set after removing the CPU from RCU masks.
+ */
+static nokprobe_inline bool trace_probe_can_trace(void)
+{
+	return likely(rcu_is_watching() && cpu_online(raw_smp_processor_id()));
+}
+
 static inline unsigned int trace_probe_load_flag(struct trace_probe *tp)
 {
 	return smp_load_acquire(&tp->event->flags);
-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>
base-commit: 76a0a6d58318fff8b14530ed9b99c7065995db29
prerequisite-patch-id: d1ada55f79bf2b6f064df7d3291428c6938878d9
prerequisite-patch-id: 86b4e93aa82c742f87657404f8af3d199b0b8fec

                 reply	other threads:[~2026-12-04 16:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261204161150.21170-1-mhiramat@kernel.org \
    --to=mhiramat@kernel.org \
    --cc=atenart@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=rostedt@goodmis.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