From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796401333; b=wqPILcxsHmirLt3a3gyYoEVpBDPOmIjcdFCSfm3TbkHIx4jO2BhlNit95aqailKbVeuo 7E+XIhwag0w5/IVBf/OfksVnaRjJJi//25JO0uX54vc5MLDLEzevqNgwuddGfOowkfC3o DHNIlZuNyz+/1RX6PXMQY6qTfOHMKGLcJDX1ibyle5uRYX7eK8iRHNaeKfpMPF8anxGL2 C85JOTkXr6olyol00nPR9oq0a8swE6HCn8t6oJO5iDhTBhKbCdko8J3DwLc8esrUDDbkR Gq2zz+KCfqWiUYiqUNFRmPP6fi9snCmfqVBzjonMKpZXWX2/SUvrJ8D4AljBdKy7fvA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401333; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=KfWi/qRsFa0kmYWs1MVTc3Dkj4oP6TtO3npvEPfbN50=; b=pIDL7wOYvl2JjsqY8VJx3gPM9Q7g/1MG7vIE+7wB9b9CyVR6T9rsT4EVdbpr8IZhscUB Xcz+nOzo6SXvcet0fV8QItESK6a0eZEpQEc0D1hABhtc5js06u30XbTiApfshYRGKmsBc zI8M6/i4EcYwGdhH1ZKyviUBtld8KPPR/qGK0KmkQz6cKLCWat1sJwow2GTNmZANDWwxV e9e90hbQd2p808o8JYinq6xA926Kbf7/zQQaruJri8CeDYSGPzxBNy+mBPm1gpPIasIbV UPhaXFbTya+nH7Eu9h6VOyTJapEiNdMd28FFSbKVPNG9KJ+IHv7IB+HTYguN6L/QQMg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NELPf9Zf; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EEAC606A33D; Fri, 4 Dec 2026 16:22:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=NELPf9Zf Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0F6421F000CE; Fri, 4 Dec 2026 16:22:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401333; bh=KfWi/qRsFa0kmYWs1MVTc3Dkj4oP6TtO3npvEPfbN50=; h=From:To:Cc:Subject:Date; b=NELPf9Zf4EqYGVdCGUHQjERuX7BicRexi8LaIeGPNMrl/nOTvNLz9sN3nAAcg5rjM IcSk46/1u0xxQpgI2anu64e11MDK8gKEzu9S+ttc2YW7jQiR/c6s27fyUKV+NBnzsl iERw/c818CdRTGaFQ+d1YInI24/Y4WECGCFnGyJbAWh3hgM/z5inS+ZasSg2Y4SuIV oy4iCkxaEGrZfALvfbzcVdB5rtIqtIFt5j6oOVqiILskMgnlG0J/jAvWT/c3ok25M+ 3tKdwrz8FrReu4dz18LnyPypI0b5f292Bvhm6BhnDlpN4WnGU23WwgI8VQETMdZvts pXJ34qj/WK7mg== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Mathieu Desnoyers Cc: Antoine Tenart , 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 Message-ID: <20261204161150.21170-1-mhiramat@kernel.org> X-Mailer: UndineD 0.1 X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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) --- 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) base-commit: 76a0a6d58318fff8b14530ed9b99c7065995db29 prerequisite-patch-id: d1ada55f79bf2b6f064df7d3291428c6938878d9 prerequisite-patch-id: 86b4e93aa82c742f87657404f8af3d199b0b8fec