rcu: Mark rcu_read_unlock_special() notrace
PATCH 1/1 rcu: Mark rcu_read_unlock_special() notrace
The BPF trampoline calls rcu_read_unlock() on its exit path. If the task was preempted in the RCU read-side critical section, this ends up in rcu_read_unlock_special(). When a BPF fentry or fexit program is attached to rcu_read_unlock_special() itself, the trampoline runs again before ->rcu_read_unlock_special is cleared, calls rcu_read_unlock() again, and recurses until the stack overflows:
BUG: TASK stack guard page was hit at ffffc90001657ff8 ... bpf_trampoline_6442575622+0x5e/0xac rcu_read_unlock_special+0x5/0x490 __rcu_read_unlock+0x9c/0x120 rcu_read_unlock_migrate+0x93/0x220 bpf_trampoline_6442575622+0x5e/0xac rcu_read_unlock_special+0x5/0x490 ...
Mark rcu_read_unlock_special() notrace.
--- kernel/rcu/tree_plugin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/tree_plugin.h +1 -1
@@ -746,7 +746,7 @@ static bool rcu_unlock_needs_exp_handling(struct task_struct *t,* notify RCU core processing or task having blocked during the RCU * read-side critical section. */ -static void rcu_read_unlock_special (struct task_struct *t) +static notrace void rcu_read_unlock_special(struct task_struct *t) {unsigned long flags;bool irqs_were_disabled;
Signature · 1 line
2.56.1
Quoted 17 lines · Sechang Lim
The BPF trampoline calls
rcu_read_unlock()on its exit path. If the task was preempted in the RCU read-side critical section, this ends up inrcu_read_unlock_special(). When a BPF fentry or fexit program is attached torcu_read_unlock_special()itself, the trampoline runs again before ->rcu_read_unlock_special is cleared, callsrcu_read_unlock()again, and recurses until the stack overflows:BUG: TASK stack guard page was hit at ffffc90001657ff8 ... bpf_trampoline_6442575622+0x5e/0xac rcu_read_unlock_special+0x5/0x490 __rcu_read_unlock+0x9c/0x120 rcu_read_unlock_migrate+0x93/0x220 bpf_trampoline_6442575622+0x5e/0xac rcu_read_unlock_special+0x5/0x490 ...
Quoted 1 line
Mark
rcu_read_unlock_special()notrace.
Hmm.. this seems to be edge case, but no big deal.
Quoted 12 lines
Signed-off-by Sechang Lim <[email protected]>---
kernel/rcu/tree_plugin.h| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)kernel/rcu/tree_plugin.h +0
-0
kernel/rcu/tree_plugin.h static bool rcu_unlock_needs_exp_handling(struct task_struct *t, · line 752
Quoted 8 lines
kernel/rcu/tree_plugin.h +1
-1@@ -746,7 +746,7 @@ static bool rcu_unlock_needs_exp_handling(struct task_struct *t,* notify RCU core processing or task having blocked during the RCU * read-side critical section. */ -static void rcu_read_unlock_special (struct task_struct *t) +static notrace void rcu_read_unlock_special(struct task_struct *t) {unsigned long flags;bool irqs_were_disabled;
--- Thanks!
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] rcu: missing notrace on rcu_unlock_needs_exp_handling() allows recursion --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] rcu: missing notrace on rcu_unlock_needs_exp_handling() allows recursion
--
commit 0062b967b0900da7cf7c59da2e3a111544dc9aec
Author: Sechang Lim <[email protected]>
rcu: Mark rcu_read_unlock_special() notrace
This commit marks rcu_read_unlock_special() notrace to prevent infinite recursion when a BPF trampoline attached to it calls rcu_read_unlock().
kernel/rcu/tree_plugin.h static bool rcu_unlock_needs_exp_handling(struct task_struct *t, · line 752
Quoted 13 lines
kernel/rcu/tree_plugin.h +1
-1@@ -746,7 +746,7 @@ static bool rcu_unlock_needs_exp_handling(struct task_struct *t,* notify RCU core processing or task having blocked during the RCU * read-side critical section. */ -static void rcu_read_unlock_special (struct task_struct *t) +static notrace void rcu_read_unlock_special(struct task_struct *t) {unsigned long flags;bool irqs_were_disabled;
[Severity: High]
Should rcu_unlock_needs_exp_handling() also be marked notrace (or static inline)?
In the preempt_bh_were_disabled || irqs_were_disabled path, rcu_read_unlock_special() disables IRQs and calls rcu_unlock_needs_exp_handling() while t->rcu_read_unlock_special.s is still non-zero. Because rcu_unlock_needs_exp_handling() is neither notrace nor inline, marking rcu_read_unlock_special() notrace prevents the compiler from inlining it when notrace expands to __no_instrument_function__. Attaching a BPF fentry or fexit program to rcu_unlock_needs_exp_handling() then triggers the same rcu_read_unlock() -> rcu_read_unlock_special() recursion and stack overflow.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 4 lines · Sechang Lim
The BPF trampoline calls
rcu_read_unlock()on its exit path. If the task was preempted in the RCU read-side critical section, this ends up inrcu_read_unlock_special(). When a BPF fentry or fexit program is attached torcu_read_unlock_special()itself, the trampoline runs again
Not quite. need_qs and exp_hint set rcu_read_unlock_special.s too, no preemption needed.
With fexit it recurses on entry, before the prog runs:
__rcu_read_unlock() nesting 1 -> 0, .s != 0
rcu_read_unlock_special()
__bpf_tramp_enter()
percpu_ref_get() nesting 0 -> 1 -> 0, .s != 0
rcu_read_unlock_special()Review
Quoted 2 lines
(quoted diff) +1
-1-static void rcu_read_unlock_special(struct task_struct *t) +static notrace void rcu_read_unlock_special(struct task_struct *t)
It should be fixed on bpf side. Only the bpf trampoline recurses. ftrace, fprobe and kprobes take ftrace_test_recursion_trylock(). notrace takes the function away from them and from livepatch. __rcu_read_unlock() is in btf_id_deny for the same reason.
kernel/bpf/verifier.c +1 -0
@@ -24735,6 +24735,7 @@ BTF_ID(func, preempt_count_sub)#ifdef CONFIG_PREEMPT_RCU BTF_ID(func, __rcu_read_lock) BTF_ID(func, __rcu_read_unlock) +BTF_ID(func, rcu_read_unlock_special)#endif BTF_SET_END (btf_id_deny)
Pls target bpf-next.
pw-bot: cr
Bradley Morgan
Reviewed-byQuoted 30 lines · Alexei Starovoitov
Quoted 4 lines · Sechang Lim
The BPF trampoline calls
rcu_read_unlock()on its exit path. If the task was preempted in the RCU read-side critical section, this ends up inrcu_read_unlock_special(). When a BPF fentry or fexit program is attached torcu_read_unlock_special()itself, the trampoline runs againNot quite.
need_qsandexp_hintset rcu_read_unlock_special.s too, no preemption needed.
With fexit it recurses on entry, before the prog runs:
__rcu_read_unlock()nesting 1 -> 0, .s != 0
rcu_read_unlock_special()__bpf_tramp_enter() percpu_ref_get() nesting 0 -> 1 -> 0, .s != 0 rcu_read_unlock_special()Review
Quoted 2 lines
(quoted diff) +1
-1-static void rcu_read_unlock_special(struct task_struct *t) +static notrace void rcu_read_unlock_special(struct task_struct *t)It should be fixed on bpf side. Only the bpf trampoline recurses. ftrace, fprobe and kprobes take
ftrace_test_recursion_trylock(). notrace takes the function away from them and from livepatch.__rcu_read_unlock()is inbtf_id_denyfor the same reason.kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c BTF_ID(func, preempt_count_sub) · line 24740
Quoted 6 lines
kernel/bpf/verifier.c +1
-0@@ -24735,6 +24735,7 @@ BTF_ID(func, preempt_count_sub)#ifdef CONFIG_PREEMPT_RCU BTF_ID(func, __rcu_read_lock) BTF_ID(func, __rcu_read_unlock) +BTF_ID(func, rcu_read_unlock_special)#endif BTF_SET_END (btf_id_deny)
ahh crap, brain fog, sorry.
yeah, this approach looks good, again, looks correct to me, won't blow anything up.
If a patch is submitted:
Quoted 6 lines
Pls target bpf-next.
pw-bot: cr
--- Thanks!