Skip to contentSkip to conversation

bpf: Add rcu_read_unlock_special() into btf id deny list

PATCH In review Sechang Lim 2 participants 2 messages Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 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. It is static with one caller and gcc inlines it, which leaves the entry unresolved and breaks the build with CONFIG_WERROR=y, so mark it noinline.

rcu_unlock_needs_exp_handling() also runs with .s set. It is normally inlined, but an out-of-line copy would recurse the same way, so mark it __always_inline.

---
v3:
- Mark rcu_read_unlock_special() noinline for gcc (Alexei)
- Mark rcu_unlock_needs_exp_handling() __always_inline (Alexei)
- Drop Bradley's Reviewed-by, since the patch now changes RCU

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

 kernel/bpf/verifier.c    |  1 +
 kernel/rcu/tree_plugin.h | 12 +++++++-----
 2 files changed, 8 insertions(+), 5 deletions(-)
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)
 
kernel/rcu/tree_plugin.h +7 -5
index ae5d21bae7bc..40996dc29b28 100644
@@ -689,10 +689,10 @@ 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,
-				      struct rcu_data *rdp,
-				      struct rcu_node *rnp,
-				      bool irqs_were_disabled)
+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)
 {
 	/*
 	 * Check if this task is blocking an expedited grace period. If the
@@ -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;
Signature · 1 line
2.56.1
maintainer
#1 lore raw
Quoted 11 lines · Sechang Lim

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:

Quoted 9 lines
 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
  ...

Can this problem not happen also if you have an RCU read-side critical section within an ftrace probe on sensitive RCU functions?

If yes, I wonder if we need a more general fix than BPF-specific, such as using notrace on the sensitive function.

Quoted 60 lines

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. It is static with one caller and gcc inlines it, which leaves the entry unresolved and breaks the build with CONFIG_WERROR=y, so mark it noinline.

rcu_unlock_needs_exp_handling() also runs with .s set. It is normally inlined, but an out-of-line copy would recurse the same way, so mark it __always_inline.

---
v3:
- Mark rcu_read_unlock_special() noinline for gcc (Alexei)
- Mark rcu_unlock_needs_exp_handling() __always_inline (Alexei)
- Drop Bradley's Reviewed-by, since the patch now changes RCU

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

kernel/bpf/verifier.c | 1 +
kernel/rcu/tree_plugin.h | 12 +++++++-----

2 files changed, 8 insertions(+), 5 deletions(-)
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)
 
kernel/rcu/tree_plugin.h +4 -4
index ae5d21bae7bc..40996dc29b28 100644
@@ -689,10 +689,10 @@ 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,
-                      struct rcu_data *rdp,
-                      struct rcu_node *rnp,
-                      bool irqs_were_disabled)
+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)
 {
   /*
    * Check if this task is blocking an expedited grace period. If the

kernel/rcu/tree_plugin.h static bool rcu_unlock_needs_exp_handling(struct task_struct *t, · line 750

Quoted 6 lines
kernel/rcu/tree_plugin.h +2 -0
@@ -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.
 */

Please drop this comment.

Thanks.

Quoted 8 lines
kernel/rcu/tree_plugin.h +1 -2
-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;
--

2.56.1