tracing: Allow system call tracepoints to handle page faults
Mathieu Desnoyers
reviewerv2 cover tracing: Allow system call tracepoints to handle page faults
Wire up the system call tracepoints with Tasks Trace RCU to allow the ftrace, perf, and eBPF tracers to handle page faults.
This series does the initial wire-up allowing tracers to handle page faults, but leaves out the actual handling of said page faults as future work.
This series was compile and runtime tested with ftrace and perf syscall tracing and raw syscall tracing, adding a WARN_ON_ONCE() in the generated code to validate that the intended probes are used for raw syscall tracing. The might_fault() added within those probes validate that they are called from a context where handling a page fault is OK.
This series replaces the "Faultable Tracepoints v6" series found at [1].
This has been rebased on v6.12-rc1 on top of two patches from Steven:
tracing: Remove definition of trace_*_rcuidle()
tracepoint: Remove SRCU protection
Thanks,
Mathieu
Mathieu Desnoyers (7):
tracing/ftrace: guard syscall probe with preempt_notrace tracing/perf: guard syscall probe with preempt_notrace tracing/bpf: guard syscall probe with preempt_notrace
tracing: Allow system call tracepoints to handle page faults tracing/ftrace: Add might_fault check to syscall probes tracing/perf: Add might_fault check to syscall probes tracing/bpf: Add might_fault check to syscall probes
include/linux/tracepoint.h | 18 ++++++++++----- include/trace/bpf_probe.h | 12 +++++++++- include/trace/perf.h | 42 +++++++++++++++++++++++++++++++---- include/trace/trace_events.h | 39 ++++++++++++++++++++++++++------ init/Kconfig | 1 + kernel/trace/trace_syscalls.c | 28 +++++++++++++++++++++++ 6 files changed, 123 insertions(+), 17 deletions(-)
Signature · 1 line
2.39.2
Mathieu Desnoyers
reviewerPATCH v2 1/7 tracing/ftrace: guard syscall probe with preempt_notrace
In preparation for allowing system call enter/exit instrumentation to handle page faults, make sure that ftrace can handle this change by explicitly disabling preemption within the ftrace system call tracepoint probes to respect the current expectations within ftrace ring buffer code.
This change does not yet allow ftrace to take page faults per se within its probe, but allows its existing probes to adapt to the upcoming change.
--- include/trace/trace_events.h | 38 ++++++++++++++++++++++++++++------- kernel/trace/trace_syscalls.c | 12 +++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-)
include/trace/trace_events.h +31 -7
@@ -263,6 +263,9 @@ static struct trace_event_fields trace_event_fields_##call[] = { \ tstruct \ {} }; +#undef DECLARE_EVENT_SYSCALL_CLASS +#define DECLARE_EVENT_SYSCALL_CLASS DECLARE_EVENT_CLASS +#undef DEFINE_EVENT_PRINT#define DEFINE_EVENT_PRINT (template, name, proto, args, print)
@@ -396,11 +399,11 @@ static inline notrace int trace_event_get_offsets_##call( \#include "stages/stage6_event_callback.h" -#undef DECLARE_EVENT_CLASS -#define DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ - \ + +#undef __DECLARE_EVENT_CLASS +#define __DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ static notrace void \ -trace_event_raw_event_##call(void *__data, proto) \ +do_trace_event_raw_event_##call(void *__data, proto) \ { \ struct trace_event_file *trace_file = __data; \ struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
@@ -425,15 +428,34 @@ trace_event_raw_event_##call(void *__data, proto) \ \trace_event_buffer_commit (&fbuffer); \ } + +#undef DECLARE_EVENT_CLASS +#define DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ +__DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args), PARAMS(tstruct), \ + PARAMS(assign), PARAMS(print)) \ +static notrace void \ +trace_event_raw_event_##call(void *__data, proto) \ +{ \ + do_trace_event_raw_event_##call(__data, args); \ +} + +#undef DECLARE_EVENT_SYSCALL_CLASS +#define DECLARE_EVENT_SYSCALL_CLASS (call, proto, args, tstruct, assign, print) \ +__DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args), PARAMS(tstruct), \ + PARAMS(assign), PARAMS(print)) \ +static notrace void \ +trace_event_raw_event_##call(void *__data, proto) \ +{ \ + guard(preempt_notrace)(); \ + do_trace_event_raw_event_##call(__data, args); \ +} +/* * The ftrace_test_probe is compiled out, it is only here as a build time check * to make sure that if the tracepoint handling changes, the ftrace probe will * fail to compile unless it too is updated. */ -#undef DECLARE_EVENT_SYSCALL_CLASS -#define DECLARE_EVENT_SYSCALL_CLASS DECLARE_EVENT_CLASS -#undef DEFINE_EVENT#define DEFINE_EVENT (template, call, proto, args) \ static inline void ftrace_test_probe_##call(void) \
@@ -443,6 +465,8 @@ static inline void ftrace_test_probe_##call(void) \#include TRACE_INCLUDE (TRACE_INCLUDE_FILE ) +#undef __DECLARE_EVENT_CLASS +#include "stages/stage7_class_define.h" #undef DECLARE_EVENT_CLASS
kernel/trace/trace_syscalls.c +12 -0
@@ -299,6 +299,12 @@ static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)int syscall_nr;int size; +/* +* Syscall probe called with preemption enabled, but the ring +* buffer and per-cpu data require preemption to be disabled. +*/ +guard (preempt_notrace)(); + syscall_nr =trace_get_syscall_nr (current, regs);if (syscall_nr <0 || syscall_nr >= NR_syscalls)return ;
@@ -338,6 +344,12 @@ static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)struct trace_event_buffer fbuffer;int syscall_nr; +/* +* Syscall probe called with preemption enabled, but the ring +* buffer and per-cpu data require preemption to be disabled. +*/ +guard (preempt_notrace)(); + syscall_nr =trace_get_syscall_nr (current, regs);if (syscall_nr <0 || syscall_nr >= NR_syscalls)return ;
Signature · 1 line
2.39.2
Mathieu Desnoyers
reviewerPATCH v2 2/7 tracing/perf: guard syscall probe with preempt_notrace
In reply to Mathieu DesnoyersIn preparation for allowing system call enter/exit instrumentation to handle page faults, make sure that perf can handle this change by explicitly disabling preemption within the perf system call tracepoint probes to respect the current expectations within perf ring buffer code.
This change does not yet allow perf to take page faults per se within its probe, but allows its existing probes to adapt to the upcoming change.
--- include/trace/perf.h | 41 +++++++++++++++++++++++++++++++---- kernel/trace/trace_syscalls.c | 12 ++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-)
include/trace/perf.h +37 -4
@@ -12,10 +12,10 @@#undef __perf_task#define __perf_task (t) (__task = (t)) -#undef DECLARE_EVENT_CLASS -#define DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ +#undef __DECLARE_EVENT_CLASS +#define __DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ static notrace void \ -perf_trace_##call(void *__data, proto) \ +do_perf_trace_##call(void *__data, proto) \ { \ struct trace_event_call *event_call = __data; \ struct trace_event_data_offsets_##call __maybe_unused __data_offsets;\
@@ -55,8 +55,38 @@ perf_trace_##call(void *__data, proto) \ head, __task); \ } +/* +* Define unused __count and __task variables to use @args to pass +* arguments to do_perf_trace_##call. This is needed because the +* macros __perf_count and __perf_task introduce the side-effect to +* store copies into those local variables. +*/ +#undef DECLARE_EVENT_CLASS +#define DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ +__DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args), PARAMS(tstruct), \ + PARAMS(assign), PARAMS(print)) \ +static notrace void \ +perf_trace_##call(void *__data, proto) \ +{ \ + u64 __count __attribute__((unused)); \ + struct task_struct *__task __attribute__((unused)); \ + \ + do_perf_trace_##call(__data, args); \ +} +#undef DECLARE_EVENT_SYSCALL_CLASS -#define DECLARE_EVENT_SYSCALL_CLASS DECLARE_EVENT_CLASS +#define DECLARE_EVENT_SYSCALL_CLASS (call, proto, args, tstruct, assign, print) \ +__DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args), PARAMS(tstruct), \ + PARAMS(assign), PARAMS(print)) \ +static notrace void \ +perf_trace_##call(void *__data, proto) \ +{ \ + u64 __count __attribute__((unused)); \ + struct task_struct *__task __attribute__((unused)); \ + \ + guard(preempt_notrace)(); \ + do_perf_trace_##call(__data, args); \ +}/* * This part is compiled out, it is only here as a build time check
@@ -76,4 +106,7 @@ static inline void perf_test_probe_##call(void) \ DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))#include TRACE_INCLUDE (TRACE_INCLUDE_FILE ) + +#undef __DECLARE_EVENT_CLASS +#endif /* CONFIG_PERF_EVENTS */
kernel/trace/trace_syscalls.c +12 -0
@@ -596,6 +596,12 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)int rctx;int size; +/* +* Syscall probe called with preemption enabled, but the ring +* buffer and per-cpu data require preemption to be disabled. +*/ +guard (preempt_notrace)(); + syscall_nr =trace_get_syscall_nr (current, regs);if (syscall_nr <0 || syscall_nr >= NR_syscalls)return ;
@@ -698,6 +704,12 @@ static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)int rctx;int size; +/* +* Syscall probe called with preemption enabled, but the ring +* buffer and per-cpu data require preemption to be disabled. +*/ +guard (preempt_notrace)(); + syscall_nr =trace_get_syscall_nr (current, regs);if (syscall_nr <0 || syscall_nr >= NR_syscalls)return ;
Signature · 1 line
2.39.2
Mathieu Desnoyers
reviewerPATCH v2 3/7 tracing/bpf: guard syscall probe with preempt_notrace
In reply to Mathieu DesnoyersIn preparation for allowing system call enter/exit instrumentation to handle page faults, make sure that bpf can handle this change by explicitly disabling preemption within the bpf system call tracepoint probes to respect the current expectations within bpf tracing code.
This change does not yet allow bpf to take page faults per se within its probe, but allows its existing probes to adapt to the upcoming change.
--- include/trace/bpf_probe.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
include/trace/bpf_probe.h +10 -1
@@ -53,8 +53,17 @@ __bpf_trace_##call(void *__data, proto) \#define DECLARE_EVENT_CLASS (call, proto, args, tstruct, assign, print) \ __BPF_DECLARE_TRACE(call, PARAMS(proto), PARAMS(args)) +#define __BPF_DECLARE_TRACE_SYSCALL (call, proto, args) \ +static notrace void \ +__bpf_trace_##call(void *__data, proto) \ +{ \ + guard(preempt_notrace)(); \ + CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args)); \ +} +#undef DECLARE_EVENT_SYSCALL_CLASS -#define DECLARE_EVENT_SYSCALL_CLASS DECLARE_EVENT_CLASS +#define DECLARE_EVENT_SYSCALL_CLASS (call, proto, args, tstruct, assign, print) \ + __BPF_DECLARE_TRACE_SYSCALL(call, PARAMS(proto), PARAMS(args))/* * This part is compiled out, it is only here as a build time check
Signature · 1 line
2.39.2
Mathieu Desnoyers
reviewerPATCH v2 4/7 tracing: Allow system call tracepoints to handle page faults
In reply to Mathieu DesnoyersUse Tasks Trace RCU to protect iteration of system call enter/exit tracepoint probes to allow those probes to handle page faults.
In preparation for this change, all tracers registering to system call enter/exit tracepoints should expect those to be called with preemption enabled.
This allows tracers to fault-in userspace system call arguments such as path strings within their probe callbacks.
--- include/linux/tracepoint.h | 18 +++++++++++++----- init/Kconfig | 1 + 2 files changed, 14 insertions(+), 5 deletions(-)
include/linux/tracepoint.h +13 -5
@@ -17,6 +17,7 @@#include <linux/errno.h> #include <linux/types.h> #include <linux/rcupdate.h> +#include <linux/rcupdate_trace.h> #include <linux/tracepoint-defs.h> #include <linux/static_call.h>
@@ -107,6 +108,7 @@ void for_each_tracepoint_in_module(struct module *mod,#ifdef CONFIG_TRACEPOINTS static inline void tracepoint_synchronize_unregister (void ) { +synchronize_rcu_tasks_trace ();synchronize_rcu (); }#else
@@ -197,18 +199,24 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p)* it_func[0] is never NULL because there is at least one element in the array * when the array itself is non NULL. */ -#define __DO_TRACE (name, args, cond) \ +#define __DO_TRACE (name, args, cond, syscall) \ do { \ int __maybe_unused __idx = 0; \ \ if (!(cond)) \ return; \ \ - preempt_disable_notrace(); \ + if (syscall) \ + rcu_read_lock_trace(); \ + else \ + preempt_disable_notrace(); \ \ __DO_TRACE_CALL(name, TP_ARGS(args)); \ \ - preempt_enable_notrace(); \ + if (syscall) \ + rcu_read_unlock_trace(); \ + else \ + preempt_enable_notrace(); \ } while (0)/*
@@ -238,7 +246,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) if (static_key_false(&__tracepoint_##name.key)) \__DO_TRACE (name, \TP_ARGS (args), \ -TP_CONDITION (cond)); \ +TP_CONDITION (cond),0 ); \if (IS_ENABLED (CONFIG_LOCKDEP ) && (cond)) { \WARN_ONCE (!rcu_is_watching (), \"RCU not watching for tracepoint" ); \
@@ -276,7 +284,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_ptr_t *p) if (static_key_false(&__tracepoint_##name.key)) \__DO_TRACE (name, \TP_ARGS (args), \ -TP_CONDITION (cond),0 ); \ +TP_CONDITION (cond),1 ); \if (IS_ENABLED (CONFIG_LOCKDEP ) && (cond)) { \WARN_ONCE (!rcu_is_watching (), \"RCU not watching for tracepoint" ); \
init/Kconfig +1 -0
@@ -1984,6 +1984,7 @@ config BINDGEN_VERSION_TEXT # config TRACEPOINTS bool + select TASKS_TRACE_RCU source "kernel/Kconfig.kexec"
Signature · 1 line
2.39.2
Mathieu Desnoyers
reviewerPATCH v2 5/7 tracing/ftrace: Add might_fault check to syscall probes
In reply to Mathieu DesnoyersAdd a might_fault() check to validate that the ftrace sys_enter/sys_exit probe callbacks are indeed called from a context where page faults can be handled.
--- include/trace/trace_events.h | 1 + kernel/trace/trace_syscalls.c | 2 ++ 2 files changed, 3 insertions(+)
include/trace/trace_events.h +1 -0
@@ -446,6 +446,7 @@ __DECLARE_EVENT_CLASS(call, PARAMS(proto), PARAMS(args), PARAMS(tstruct), \ static notrace void \ trace_event_raw_event_##call(void *__data, proto) \ { \ + might_fault(); \ guard(preempt_notrace)(); \ do_trace_event_raw_event_##call(__data, args); \ }
kernel/trace/trace_syscalls.c +2 -0
@@ -303,6 +303,7 @@ static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id)* Syscall probe called with preemption enabled, but the ring * buffer and per-cpu data require preemption to be disabled. */ +might_fault ();guard (preempt_notrace)(); syscall_nr =trace_get_syscall_nr (current, regs);
@@ -348,6 +349,7 @@ static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret)* Syscall probe called with preemption enabled, but the ring * buffer and per-cpu data require preemption to be disabled. */ +might_fault ();guard (preempt_notrace)(); syscall_nr =trace_get_syscall_nr (current, regs);
Signature · 1 line
2.39.2
Mathieu Desnoyers
reviewerPATCH v2 6/7 tracing/perf: Add might_fault check to syscall probes
In reply to Mathieu DesnoyersAdd a might_fault() check to validate that the perf sys_enter/sys_exit probe callbacks are indeed called from a context where page faults can be handled.
--- include/trace/perf.h | 1 + kernel/trace/trace_syscalls.c | 2 ++ 2 files changed, 3 insertions(+)
include/trace/perf.h +1 -0
@@ -84,6 +84,7 @@ perf_trace_##call(void *__data, proto) \ u64 __count __attribute__((unused)); \ struct task_struct *__task __attribute__((unused)); \ \ +might_fault (); \guard (preempt_notrace)(); \ do_perf_trace_##call(__data, args); \ }
kernel/trace/trace_syscalls.c +2 -0
@@ -602,6 +602,7 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)* Syscall probe called with preemption enabled, but the ring * buffer and per-cpu data require preemption to be disabled. */ +might_fault ();guard (preempt_notrace)(); syscall_nr =trace_get_syscall_nr (current, regs);
@@ -710,6 +711,7 @@ static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)* Syscall probe called with preemption enabled, but the ring * buffer and per-cpu data require preemption to be disabled. */ +might_fault ();guard (preempt_notrace)(); syscall_nr =trace_get_syscall_nr (current, regs);
Signature · 1 line
2.39.2
Mathieu Desnoyers
reviewerPATCH v2 7/7 tracing/bpf: Add might_fault check to syscall probes
In reply to Mathieu DesnoyersAdd a might_fault() check to validate that the bpf sys_enter/sys_exit probe callbacks are indeed called from a context where page faults can be handled.
--- include/trace/bpf_probe.h | 1 + 1 file changed, 1 insertion(+)
include/trace/bpf_probe.h +1 -0
@@ -57,6 +57,7 @@ __bpf_trace_##call(void *__data, proto) \ static notrace void \ __bpf_trace_##call(void *__data, proto) \ { \ + might_fault(); \ guard(preempt_notrace)(); \ CONCATENATE(bpf_trace_run, COUNT_ARGS(args))(__data, CAST_TO_U64(args)); \ }
Signature · 1 line
2.39.2
Mathieu Desnoyers
reviewerQuoted 13 lines · Mathieu Desnoyers
Wire up the system call tracepoints with Tasks Trace RCU to allow the ftrace, perf, and eBPF tracers to handle page faults.
This series does the initial wire-up allowing tracers to handle page faults, but leaves out the actual handling of said page faults as future work.
This series was compile and runtime tested with ftrace and perf syscall tracing and raw syscall tracing, adding a
WARN_ON_ONCE()in the generated code to validate that the intended probes are used for raw syscall tracing. Themight_fault()added within those probes validate that they are called from a context where handling a page fault is OK.
Quoted 6 lines
This series replaces the "Faultable Tracepoints v6" series found at [1].
This has been rebased on v6.12-rc1 on top of two patches from Steven:
tracing: Remove definition of trace_*_rcuidle()
tracepoint: Remove SRCU protection
I'll send an updated series which includes
"tracing: Declare system call tracepoints with TRACE_EVENT_SYSCALL" (missing here), and which rework that patch to remove the mapping from trace_sys_enter/exit to trace_syscall_sys_enter/exit which requires modifying architecture code. A lot of churn for little value add.
Thanks,
Mathieu
Quoted 37 lines
Thanks,
Mathieu
Cc Peter Zijlstra <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Yonghong Song <[email protected]>Cc Paul E. McKenney <[email protected]>Cc Ingo Molnar <[email protected]>Cc Arnaldo Carvalho de Melo <[email protected]>Cc Mark Rutland <[email protected]>Cc Alexander Shishkin <[email protected]>Cc Namhyung Kim <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Joel Fernandes <[email protected]>Mathieu Desnoyers (7):
tracing/ftrace: guard syscall probe withpreempt_notracetracing/perf: guard syscall probe withpreempt_notracetracing/bpf: guard syscall probe withpreempt_notrace
tracing: Allow system call tracepoints to handle page faults tracing/ftrace: Addmight_faultcheck to syscall probes tracing/perf: Addmight_faultcheck to syscall probes tracing/bpf: Addmight_faultcheck to syscall probesinclude/linux/tracepoint.h | 18 ++++++++++----- include/trace/bpf_probe.h | 12 +++++++++- include/trace/perf.h | 42 +++++++++++++++++++++++++++++++---- include/trace/trace_events.h | 39 ++++++++++++++++++++++++++------ init/Kconfig | 1 + kernel/trace/trace_syscalls.c | 28 +++++++++++++++++++++++ 6 files changed, 123 insertions(+), 17 deletions(-)
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com