[PATCH bpf] bpf: Take the RCU lock inside the raw_tp recursion guard

From: Sechang Lim

Date: Mon Nov 30 2026 - 10:18:11 EST


__bpf_trace_run() takes the RCU read lock before
bpf_prog_get_recursion_context() and drops it after
bpf_prog_put_recursion_context(). With lockdep, rcu_read_unlock() hits
the lock_release tracepoint, so a raw_tp program attached there runs
again from its own unlock until the stack overflows, even when the
program is empty. lock_acquire recurses the same way.

Disable migration first, check the recursion guard, and take the RCU
read lock inside it.

Fixes: f7d327654b88 ("bpf: Have __bpf_trace_run() use rcu_read_lock_dont_migrate()")
Signed-off-by: Sechang Lim <rhkrqnwk98@xxxxxxxxx>
---
kernel/trace/bpf_trace.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 19e6b51bb2f0..69cc4d23ff3d 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2134,33 +2134,31 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args)
struct bpf_run_ctx *old_run_ctx;
struct bpf_trace_run_ctx run_ctx;

- if (sleepable) {
+ if (sleepable)
scp = rcu_read_lock_tasks_trace();
- migrate_disable();
- } else {
- rcu_read_lock_dont_migrate();
- }
+ migrate_disable();

if (unlikely(!bpf_prog_get_recursion_context(prog))) {
bpf_prog_inc_misses_counter(prog);
goto out;
}

+ if (!sleepable)
+ rcu_read_lock();
run_ctx.bpf_cookie = link->cookie;
old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);

(void)bpf_prog_run(prog, args);

bpf_reset_run_ctx(old_run_ctx);
+ if (!sleepable)
+ rcu_read_unlock();
out:
bpf_prog_put_recursion_context(prog);

- if (sleepable) {
- migrate_enable();
+ migrate_enable();
+ if (sleepable)
rcu_read_unlock_tasks_trace(scp);
- } else {
- rcu_read_unlock_migrate();
- }
}

#define UNPACK(...) __VA_ARGS__
--
2.56.1