ftrace: make extra rcu_is_watching() validation check optional
Andrii Nakryiko
maintainerPATCH v3 1/2 ftrace: make extra rcu_is_watching() validation check optional
Introduce CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING config option to control whether ftrace low-level code performs additional rcu_is_watching()-based validation logic in an attempt to catch noinstr violations.
This check is expected to never be true and is mostly useful for low-level validation of ftrace subsystem invariants. For most users it should probably be kept disabled to eliminate unnecessary runtime overhead.
This improves BPF multi-kretprobe (relying on ftrace and rethook infrastructure) runtime throughput by 2%, according to BPF benchmarks ([0]).
[0] https://lore.kernel.org/bpf/CAEf4BzauQ2WKMjZdc9s0rBWa01BYbgwHN6aNDXQSHYia47pQ-w@mail.gmail.com/
--- include/linux/trace_recursion.h | 2 +- kernel/trace/Kconfig | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-)
include/linux/trace_recursion.h +1 -1
@@ -135,7 +135,7 @@ extern void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip);# define do_ftrace_record_recursion (ip, pip) do { } while (0)#endif -#ifdef CONFIG_ARCH_WANTS_NO_INSTR +#ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING # define trace_warn_on_no_rcu (ip) \ ({ \ bool __ret = !rcu_is_watching(); \
kernel/trace/Kconfig +13 -0
@@ -974,6 +974,19 @@ config FTRACE_RECORD_RECURSION_SIZE This file can be reset, but the limit can not change in size at runtime. +config FTRACE_VALIDATE_RCU_IS_WATCHING + bool "Validate RCU is on during ftrace execution" + depends on FUNCTION_TRACER + depends on ARCH_WANTS_NO_INSTR + help + All callbacks that attach to the function tracing have some sort of + protection against recursion. This option is only to verify that + ftrace (and other users of ftrace_test_recursion_trylock()) are not + called outside of RCU, as if they are, it can cause a race. But it + also has a noticeable overhead when enabled. + + If unsure, say N + config RING_BUFFER_RECORD_RECURSION bool "Record functions that recurse in the ring buffer" depends on FTRACE_RECORD_RECURSION
Signature · 1 line
2.43.0
Andrii Nakryiko
maintainerPATCH v3 2/2 rethook: honor CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING in rethook_try_get()
Take into account CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING when validating that RCU is watching when trying to setup rethooko on a function entry.
This further (in addition to improvements in the previous patch) improves BPF multi-kretprobe (which rely on rethook) runtime throughput by 2.3%, according to BPF benchmarks ([0]).
[0] https://lore.kernel.org/bpf/CAEf4BzauQ2WKMjZdc9s0rBWa01BYbgwHN6aNDXQSHYia47pQ-w@mail.gmail.com/
--- kernel/trace/rethook.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/trace/rethook.c +2 -0
@@ -166,6 +166,7 @@ struct rethook_node *rethook_try_get(struct rethook *rh)if (unlikely (!handler))return NULL ; +#ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING /* * This expects the caller will set up a rethook on a function entry. * When the function returns, the rethook will eventually be reclaimed
@@ -174,6 +175,7 @@ struct rethook_node *rethook_try_get(struct rethook *rh)*/ if (unlikely (!rcu_is_watching ()))return NULL ; +#endif return (struct rethook_node *)objpool_pop (&rh->pool); }
Signature · 1 line
2.43.0
Masami Hiramatsu (Google)
maintainerRe: [PATCH v3 2/2] rethook: honor CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING in rethook_try_get()
Quoted 9 lines · On Wed, 3 Apr 2024 15:03:28 -0700 Andrii Nakryiko <[email protected]> wrote:
Take into account
CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHINGwhen validating that RCU is watching when trying to setup rethooko on a function entry.This further (in addition to improvements in the previous patch) improves BPF multi-kretprobe (which rely on rethook) runtime throughput by 2.3%, according to BPF benchmarks ([0]).
[0] https://lore.kernel.org/bpf/CAEf4BzauQ2WKMjZdc9s0rBWa01BYbgwHN6aNDXQSHYia47pQ-w@mail.gmail.com/
Hi Andrii,
Can you make this part depends on !KPROBE_EVENTS_ON_NOTRACE (with this option, kretprobes can be used without ftrace, but with original int3) ? This option should be set N on production system because of safety, just for testing raw kretprobes.
Thank you,
Quoted 28 lines
Signed-off-by Andrii Nakryiko <[email protected]>--- kernel/trace/rethook.c | 2 ++ 1 file changed, 2 insertions(+)kernel/trace/rethook.c +2
-0@@ -166,6 +166,7 @@ struct rethook_node *rethook_try_get(struct rethook *rh)if (unlikely (!handler))return NULL ; +#ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING /* * This expects the caller will set up a rethook on a function entry. * When the function returns, the rethook will eventually be reclaimed @@ -174,6 +175,7 @@ struct rethook_node *rethook_try_get(struct rethook *rh)*/ if (unlikely (!rcu_is_watching ()))return NULL ; +#endif return (struct rethook_node *)objpool_pop (&rh->pool); }Signature · 1 line
2.43.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Re: [PATCH v3 2/2] rethook: honor CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING in rethook_try_get()
Quoted 11 lines · Masami Hiramatsu
Quoted 7 lines · On Wed, 3 Apr 2024 15:03:28 -0700 Andrii Nakryiko <[email protected]> wrote:
Take into account
CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHINGwhen validating that RCU is watching when trying to setup rethooko on a function entry.This further (in addition to improvements in the previous patch) improves BPF multi-kretprobe (which rely on rethook) runtime throughput by 2.3%, according to BPF benchmarks ([0]).
Quoted 7 lines
Quoted 2 lines
[0] https://lore.kernel.org/bpf/CAEf4BzauQ2WKMjZdc9s0rBWa01BYbgwHN6aNDXQSHYia47pQ-w@mail.gmail.com/
Hi Andrii,
Can you make this part depends on !KPROBE_EVENTS_ON_NOTRACE (with this option, kretprobes can be used without ftrace, but with original int3) ?
Sorry for the late response, I was out on vacation. Makes sense about KPROBE_EVENTS_ON_NOTRACE, I went with this condition:
#if defined(CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING) || defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE)
Will send an updated revision shortly.
Quoted 37 lines
This option should be set N on production system because of safety, just for testing raw kretprobes.
Thank you,
Quoted 26 lines
Signed-off-by Andrii Nakryiko <[email protected]>--- kernel/trace/rethook.c | 2 ++ 1 file changed, 2 insertions(+)kernel/trace/rethook.c +2
-1@@ -166,6 +166,7 @@ struct rethook_node *rethook_try_get(struct rethook *rh)if (unlikely (!handler))return NULL ; +#ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING /* * This expects the caller will set up a rethook on a function entry. * When the function returns, the rethook will eventually be reclaimed @@ -174,6 +175,7 @@ struct rethook_node *rethook_try_get(struct rethook *rh)*/ if (unlikely (!rcu_is_watching ()))return NULL ; +#endif return (struct rethook_node *)objpool_pop (&rh->pool); } --Quoted 2 lines
2.43.0
--
Masami Hiramatsu (Google) <[email protected]>
Quoted 10 lines · On Wed, 3 Apr 2024 15:03:27 -0700 Andrii Nakryiko <[email protected]> wrote:
Introduce
CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHINGconfig option to control whether ftrace low-level code performs additional rcu_is_watching()-based validation logic in an attempt to catch noinstr violations.This check is expected to never be true and is mostly useful for low-level validation of ftrace subsystem invariants. For most users it should probably be kept disabled to eliminate unnecessary runtime overhead.
Quoted 5 lines
This improves BPF multi-kretprobe (relying on ftrace and rethook infrastructure) runtime throughput by 2%, according to BPF benchmarks ([0]).
[0] https://lore.kernel.org/bpf/CAEf4BzauQ2WKMjZdc9s0rBWa01BYbgwHN6aNDXQSHYia47pQ-w@mail.gmail.com/
This looks good to me :)
Thank you,
Quoted 49 lines
Cc Steven Rostedt <[email protected]>Cc Masami Hiramatsu <[email protected]>Cc Paul E. McKenney <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>--- include/linux/trace_recursion.h | 2 +- kernel/trace/Kconfig | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-)include/linux/trace_recursion.h +1
-1@@ -135,7 +135,7 @@ extern void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip);# define do_ftrace_record_recursion (ip, pip) do { } while (0)#endif -#ifdef CONFIG_ARCH_WANTS_NO_INSTR +#ifdef CONFIG_FTRACE_VALIDATE_RCU_IS_WATCHING # define trace_warn_on_no_rcu (ip) \ ({ \ bool __ret = !rcu_is_watching(); \kernel/trace/Kconfig +13
-0@@ -974,6 +974,19 @@ config FTRACE_RECORD_RECURSION_SIZE This file can be reset, but the limit can not change in size at runtime. +config FTRACE_VALIDATE_RCU_IS_WATCHING + bool "Validate RCU is on during ftrace execution" + depends on FUNCTION_TRACER + depends on ARCH_WANTS_NO_INSTR + help + All callbacks that attach to the function tracing have some sort of + protection against recursion. This option is only to verify that + ftrace (and other users of ftrace_test_recursion_trylock()) are not + called outside of RCU, as if they are, it can cause a race. But it + also has a noticeable overhead when enabled. + + If unsure, say N + config RING_BUFFER_RECORD_RECURSION bool "Record functions that recurse in the ring buffer" depends on FTRACE_RECORD_RECURSIONSignature · 1 line
2.43.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>