From: Mykyta Yatsenko Modify __bpf_trace_run() to support both sleepable and non-sleepable BPF programs. When the program is sleepable: - Skip cant_sleep() and instead call might_fault() to annotate the faultable context - Use migrate_disable()/migrate_enable() instead of rcu_read_lock()/rcu_read_unlock() to allow sleeping while still protecting percpu data access - The outer rcu_tasks_trace lock is already held by the faultable tracepoint callback (__DECLARE_TRACE_SYSCALL), providing lifetime protection for the BPF program For non-sleepable programs, behavior is unchanged: cant_sleep() check, rcu_read_lock() protection. This allows multiple BPF programs with different sleepable settings to coexist on the same faultable tracepoint, since __bpf_trace_run() is invoked per-link. Signed-off-by: Mykyta Yatsenko --- kernel/trace/bpf_trace.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 9bc0dfd235af..2dd345e0fdb0 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2076,7 +2076,7 @@ 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; - rcu_read_lock_dont_migrate(); + migrate_disable(); if (unlikely(!bpf_prog_get_recursion_context(prog))) { bpf_prog_inc_misses_counter(prog); goto out; @@ -2085,12 +2085,26 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) run_ctx.bpf_cookie = link->cookie; old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); - (void) bpf_prog_run(prog, args); + if (prog->sleepable) { + might_fault(); + (void)bpf_prog_run(prog, args); + } else { + /* + * Non-sleepable programs may run in the faultable context, + * do cant_sleep() only if program is non-sleepable and context + * is non-faultable. + */ + if (!link->link.sleepable) + cant_sleep(); + rcu_read_lock(); + (void)bpf_prog_run(prog, args); + rcu_read_unlock(); + } bpf_reset_run_ctx(old_run_ctx); out: bpf_prog_put_recursion_context(prog); - rcu_read_unlock_migrate(); + migrate_enable(); } #define UNPACK(...) __VA_ARGS__ -- 2.47.3