Skip to contentSkip to conversation

bpf: Add rcu_read_unlock_special() into btf id deny list

PATCH Superseded Sechang Lim 3 participants 3 messages Nov 25

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: Add rcu_read_unlock_special() into btf id deny list

The BPF trampoline takes and drops the RCU read lock itself. When the outermost rcu_read_unlock() finds ->rcu_read_unlock_special.s set, it calls rcu_read_unlock_special(). If a fentry program is attached to rcu_read_unlock_special(), the unlock on the trampoline exit path still sees .s set and calls rcu_read_unlock_special() again, which 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
   ...

With fexit it recurses even earlier, from percpu_ref_get() in __bpf_tramp_enter(), which takes and drops the RCU read lock before the program runs.

__rcu_read_unlock() is in the deny list for the same reason. Add rcu_read_unlock_special() too.

---
v2:
- Deny the attach in btf_id_deny instead of marking the function notrace (Alexei)

v1: https://lore.kernel.org/rcu/[email protected]/

 kernel/bpf/verifier.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
index e7013ed8cadd..0e8afc0250b5 100644
@@ -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)
 
Signature · 1 line
2.56.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] bpf: unresolved BTF_ID for static inlinable rcu_read_unlock_special() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] bpf: unresolved BTF_ID for static inlinable rcu_read_unlock_special()
--

commit d423a98c605ab7698cdc1fbadbd9de2303d91def
Author: Sechang Lim <[email protected]>

bpf: Add rcu_read_unlock_special() into btf id deny list

This commit adds rcu_read_unlock_special() to the btf_id_deny set under CONFIG_PREEMPT_RCU to prevent infinite recursion when attaching fentry or fexit BPF programs to it.

kernel/bpf/verifier.c BTF_ID(func, preempt_count_sub) · line 24740

Quoted 11 lines
kernel/bpf/verifier.c +1 -0
index e7013ed8cadd7..0e8afc0250b54 100644
@@ -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)

[Severity: Medium]
Can resolve_btfids fail to resolve rcu_read_unlock_special when the compiler inlines it?

rcu_read_unlock_special() is defined in kernel/rcu/tree_plugin.h as a static function without noinline, and __rcu_read_unlock() is its only caller. When the compiler inlines it and elides the out-of-line definition, no BTF_KIND_FUNC entry is emitted in vmlinux, causing resolve_btfids to emit an unresolved symbol warning (or fail the build when CONFIG_WERROR=y passes --fatal_warnings). Should rcu_read_unlock_special() be marked noinline as well?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Sechang Lim
Quoted 3 lines · Sechang Lim

BTF_ID(func, __rcu_read_lock)
BTF_ID(func, __rcu_read_unlock)
+BTF_ID(func, rcu_read_unlock_special)

This will break gcc builds with KASAN and WERROR.
It was my suggestion, sorry.
The other entries are global functions. This one is static and has one caller, so gcc inlines it in KASAN builds.
The splat in commit 684c68d92e2e has no such frame:

  rcu_preempt_deferred_qs_irqrestore+0x74/0xed0
  __rcu_read_unlock+0x290/0x3b0

On x86 __rcu_read_unlock() is 0x2f0 bytes in commit caf3ef7468f7 and 0x120 bytes in your log.
Without a symbol there is no FUNC in BTF, and id_patch() prints:

WARN: resolve_btfids: unresolved symbol rcu_read_unlock_special

WERROR makes it fatal. Sasha fixed such breaks last month:
https://lore.kernel.org/bpf/[email protected]/

rcu_unlock_needs_exp_handling() also runs with .s set and no guard. fentry on an out-of-line copy recurses the same way. It's normally inlined, so it cannot go into btf_id_deny. Plain inline is not enough. Song's series drops notrace from it:
https://lore.kernel.org/bpf/[email protected]/

Two attributes in tree_plugin.h on top of your patch cover both. clang builds tree.o with them. I didn't try gcc, the BTF step or a boot.

kernel/rcu/tree_plugin.h +4 -2
index ae5d21b..5f2f56c 100644
@@ -689,7 +689,7 @@ static void rcu_preempt_deferred_qs_handler(struct irq_work *iwp)
  *
  * Returns true if expedited processing of the rcu_read_unlock() is needed.
  */
-static bool rcu_unlock_needs_exp_handling(struct task_struct *t,
+static __always_inline bool rcu_unlock_needs_exp_handling(struct task_struct *t,
 				      struct rcu_data *rdp,
 				      struct rcu_node *rnp,
 				      bool irqs_were_disabled)
@@ -745,8 +745,10 @@ static bool rcu_unlock_needs_exp_handling(struct task_struct *t,
  * Handle special cases during rcu_read_unlock(), such as needing to
  * notify RCU core processing or task having blocked during the RCU
  * read-side critical section.
+ *
+ * noinline: btf_id_deny in kernel/bpf/verifier.c needs it in BTF.
  */
-static void rcu_read_unlock_special(struct task_struct *t)
+static noinline void rcu_read_unlock_special(struct task_struct *t)
 {
 	unsigned long flags;
 	bool irqs_were_disabled;

Pls fold it in and build with gcc and KASAN.

Please cc Paul and rcu folks on a respin.

pw-bot: cr