From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796139184; b=xJg0Fr3SQeklrwGhrrPfoc2LHLrKEr0u38aP992FpbyoLgrcPUWVNEI49lJoefH9HGBM SEzHjOoHl4NTr/N1FZk++gye09vjvE2Wme4zjmgIgp2pG+H+fldtb7ppSLssb3LAz0Zkl 9CIKc9DSpmvQhQGlmDiCW0j8jkNLkDqM6QMikKAcz8FeTg3YtgH0Xdv9cH4ZsIxwUjDCO UGgXG/P4SNGmwzeCzZPxPBNMk0THWjW5jDihOFkbwfOirOedR99jn15QssuYjT+7I7QgN kQQoiwjbDgQ0PG68m/0giizfqg4JZzhLBFKPf4gPjv4pFcrfgpyRG/whohePUpYjSSA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796139184; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=/F5zcT2+bWyVV2iiurZXGCsEq4tK4ioRWaq1UY9YePw=; b=SlC8DsYql5x9po2krV+Dya+DNKvOHRpUBP1XDQQ+p7drpdcLhgGF3YUw8gh31m+I/9mk P86OQXCXLAQ+zmNU6NdKv6A5HMgzcjix9Kdht+AQtcnA8yQjRY3Y263QoUZum4z0SKvh7 Jfe9xJQWIn2/43W0Gj1FXWvkszse5JbqDC+kOkMqdSqBBD/eqVwJPxDeK50HORS2vKbl0 qTrB96cbFytDSCLjV6gpsJRSpKDsEQulrUeF8TMgzPPrJNwtCKFmKogZ/5v4POPEnMQHO 8QrxJt2KGsic+Ptbxt2IcmQ0XlB3DnbgFzjEVI2ZLH/Mxjmt74HVI3U8X+q2KqOP5QQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lEcuNlZ9; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A2FC2606A685; Tue, 1 Dec 2026 15:33:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lEcuNlZ9 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796139184; b=BAocK9tXsE9Ys2DvkNXmwDsLbU813fKsruAnGvKYdbvatVr5SVTwk7LfKQ+vimVMn/hn tkWgvRqTzWp2hZfNRqQF6kqip9c7hoVpb/1xX/FhDZC4jYog1gZcD36eJw8XQB9NKOvx2 ykIg5JsgLDScuVp2Kw/x4/Y08EeFtmA8d0VEDIcbDwVGf46cc/3d/3mQZqTzshj+PH+2i SLNDqpv78o6Oyf8YApdANa9DewqE2YhW4UB58i/qwmjspz2yVHON/foUHJ8hNVYiJOoP1 nb0IMTFy5vnZWp0f8TRGXfJ2JFfhr+VpsKVC+swiBM92zmyxMNazZa38H/fEsX9k9fA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796139184; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=/F5zcT2+bWyVV2iiurZXGCsEq4tK4ioRWaq1UY9YePw=; b=k7nMDFr6ssXDV5ycQPTOCzgoRJjZizbxizeOkXMYeuTJ2d5IrQ4jFns/16j7EuKcu2jZ IMM+OJbc8sRBQF76tYOZ0t1ZuAXVEihDU3beCQgGdeQh8FuudnHJw8EPXEVNDvbAsc0Gm LVbB9qGozV5aC54MsTN9KYY2LAUqW2bOD0VCFNlEVyW2CUmHyZv/bhcFoJEcbrtbWCMCV gcaqe+a3kEXsFUzVTUxsdawZew2tOhesb5Y+TpQ5AKa7Wj5MWRl+tEYUAZg/5QpkyEUSp PM6X5jOhtP7kKPboDnmW+HUcjwlD8lVVoP+gLJrfNF+bTApPL8RDuKSlm5zxQdGzXcg== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 6D41A1F00934; Tue, 1 Dec 2026 15:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796139184; bh=/F5zcT2+bWyVV2iiurZXGCsEq4tK4ioRWaq1UY9YePw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=lEcuNlZ9mFsvZWjIR1Gm6ZlZnOg55Ynr9iD5rYoDud/wb61d+6GR1Y5HKBVRflgLF h+IlYRRTHSsVB/z2yD/nTMPNtNjg0+z1A+Rj6JrT/d43coYKFUdgUjZGcwRy1TPt0k RtzkKyKKfjodEkBuqCBuOTS7q7Qxv4NYRoj7S/w9KgYbJgWHkek8QfcXZ9WIo5uwdm 3q6+mg6hZEI+hT3/noeAtXUb1mKFBPQuqs2bvEVJmmB3s0veroknwv2U8gZSu+LIxv DhDa59Pz6eou5DzMRaTIBrcrOHWQR/OS38UTzbA/tLeyqyEvKCupDz24KGA30szJil Clqqr7jKD1FzQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id E6A6F4EE7C; Tue, 1 Dec 2026 15:32:33 +0000 (UTC) From: Mike Bazov via B4 Relay Date: Tue, 01 Dec 2026 17:32:14 +0200 Subject: [PATCH bpf-next v5 3/5] bpf: Check program recursion per task via bpf_ctx X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261201-bpf-preempt-v2-r2-v5-3-a6bb30daae35@sentinelone.com> References: <20261201-bpf-preempt-v2-r2-v5-0-a6bb30daae35@sentinelone.com> In-Reply-To: <20261201-bpf-preempt-v2-r2-v5-0-a6bb30daae35@sentinelone.com> To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Martin KaFai Lau , Eduard Zingerman , Kumar Kartikeya Dwivedi , Song Liu , Yonghong Song , Jiri Olsa , John Fastabend , Emil Tsalapatis , Ihor Solodrai , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , JP Kobryn , linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, Mike Bazov X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796139152; l=20306; i=mikeba@sentinelone.com; s=20261022; h=from:subject:message-id; bh=VERcSBnXqiE6feqwxzOQJiQBb8Ll6iy6Vr7WbTj5Kqg=; b=p2Evct5b9492dfXWJarXdjNLst3THdS1jCqFRuqp0g91VCokCdDwC//0Kai8zJ9ZzYbOCRC85 xSqainqbEA6C3LgfYNWwKgvzk/qKwoDnPckiQ42QoIFn/qblQDSUSjl X-Developer-Key: i=mikeba@sentinelone.com; a=ed25519; pk=DBXcQ0qoCkgacRsRwT1JU7oOWpWSYuTyLiXMfNLcP+A= X-Endpoint-Received: by B4 Relay for mikeba@sentinelone.com/20261022 with auth_id=1284 X-Original-From: Mike Bazov Reply-To: mikeba@sentinelone.com Content-Transfer-Encoding: 7bit From: Mike Bazov Trampoline programs, raw tracepoint programs and tracepoint programs on syscall tracepoints run preemptibly, but their recursion check is a per-CPU, per-program counter. When a task is preempted inside a program, the counter stays raised, and every other task that enters the program on that CPU is dropped and counted as a recursion miss, although nothing recursed. Since commit 7dadeaa6e851 ("sched: Further restrict the preemption modes") x86 and arm64 have only lazy and full preemption, so this happens in normal operation, and every drop is a lost event. Check recursion per task instead. The runners already link a stack-allocated bpf_run_ctx into current->bpf_ctx with bpf_set_run_ctx(). Move saved_run_ctx into struct bpf_run_ctx, so every frame links to the outer one, and record in the frame the program that runs in it. bpf_prog_enter_run_ctx() fills in the frame, refuses a program that a frame in the chain already runs, and links the frame only if the program may run; bpf_prog_exit_run_ctx() unlinks it. Interrupts and NMIs run on top of the chain of the task they interrupt, so recursion through them is still caught. The syscall tracepoint runner links a frame per program. An async callback doesn't run in a hook, and current may be any task. Its runner now links a frame of its own on the stack, with BPF_PROG_ASYNC_CB as the program, so that bpf_in_async_cb() still recognizes it and a program that the callback runs into is still checked against the frames below. This replaces bpf_async_run_ctx, which all CPUs shared. The frame of a marked program is unlinked before preemption is enabled: enabling preemption can switch tasks, and a program that runs in the scheduler would otherwise be dropped as recursion. A program with prog->percpu_state set runs with preemption disabled instead, so that no other task starts a run of it on the CPU before it exits. Its per-CPU data stays its own, and the runs of other tasks wait instead of being dropped. Two marked programs that share a per-CPU map are also kept apart: no other task runs on the CPU while one of them runs. The per-program counter never did that. A sleepable program can't disable preemption: it has to use bpf_preempt_disable() itself, and a sleepable struct_ops program no longer gets a private stack. An extension (freplace) runs in the run of its target, so a marked extension can only be attached to a raw tracepoint or tracepoint program that runs with preemption disabled: marked and not sleepable. Trade-offs: - A marked program can't be preempted until it exits. A program that tail-calls is marked, so its targets run that way too. - A sleepable program with per-CPU state gets no protection. - On PREEMPT_RT a marked program runs non-preemptibly too, as kprobe programs do. - An unmarked program that keeps per-CPU state the verifier doesn't see can have two runs share it, and nothing counts that. - A run that preempts another one inside a timed may_goto loop can renew the other's 250 ms budget, as it already can for lsm programs. - Attaching a marked extension to a raw tracepoint or tracepoint program that is unmarked or sleepable now fails with -EINVAL. Signed-off-by: Mike Bazov --- include/linux/bpf.h | 120 ++++++++++++++++++++++++++++++++++++++++------- kernel/bpf/helpers.c | 5 +- kernel/bpf/syscall.c | 15 ++++++ kernel/bpf/trampoline.c | 29 +++++------- kernel/bpf/verifier.c | 5 +- kernel/trace/bpf_trace.c | 11 ++--- net/bpf/test_run.c | 9 +--- net/ipv4/bpf_tcp_ops.c | 6 +-- 8 files changed, 141 insertions(+), 59 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 2dfd963a2703..2172e732ccc2 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2801,7 +2801,22 @@ int bpf_prog_array_copy(struct bpf_prog_array *old_array, struct bpf_prog_array **new_array); struct bpf_prog *bpf_prog_dummy(void); -struct bpf_run_ctx {}; +/* + * A frame in the chain of the programs that a task is running, from + * current->bpf_ctx to the outermost one. + */ +struct bpf_run_ctx { + /* The next outer frame, NULL in the outermost one */ + struct bpf_run_ctx *saved_run_ctx; + /* + * The program that runs in the frame, see bpf_prog_enter_run_ctx(). + * NULL if the runner records none, BPF_PROG_ASYNC_CB in an async callback. + */ + const struct bpf_prog *prog; +}; + +/* ->prog of the frame of an async callback, see bpf_in_async_cb() */ +#define BPF_PROG_ASYNC_CB ((const struct bpf_prog *)1) struct bpf_cg_run_ctx { struct bpf_run_ctx run_ctx; @@ -2819,25 +2834,98 @@ struct bpf_tramp_run_ctx { struct bpf_run_ctx run_ctx; u64 bpf_cookie; int retval; - struct bpf_run_ctx *saved_run_ctx; }; -static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx) +/* + * Link @new_ctx into the chain of the current task, with @prog as the program + * that runs in it. Returns the frame it pointed to before. + */ +static __always_inline struct bpf_run_ctx *bpf_set_run_ctx_prog(struct bpf_run_ctx *new_ctx, + const struct bpf_prog *prog) { struct bpf_run_ctx *old_ctx = NULL; #ifdef CONFIG_BPF_SYSCALL - old_ctx = current->bpf_ctx; - current->bpf_ctx = new_ctx; + old_ctx = READ_ONCE(current->bpf_ctx); + if (new_ctx) { + new_ctx->saved_run_ctx = old_ctx; + new_ctx->prog = prog; + } + /* Initialize the frame before interrupts and NMIs on this CPU see it. */ + barrier(); + WRITE_ONCE(current->bpf_ctx, new_ctx); +#else + if (new_ctx) + new_ctx->saved_run_ctx = NULL; #endif return old_ctx; } -static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx) +static __always_inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx) +{ + return bpf_set_run_ctx_prog(new_ctx, NULL); +} + +static __always_inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx) +{ +#ifdef CONFIG_BPF_SYSCALL + WRITE_ONCE(current->bpf_ctx, old_ctx); + /* Unlink the frame before its stack storage can be reused. */ + barrier(); +#endif +} + +/* + * Run @prog in @run_ctx: link the frame into the chain of the current task, + * unless a frame in the chain runs @prog already. That would be recursion, and + * the frame is not linked. Interrupts and NMIs run on top of the chain of the + * task they interrupt, so recursion through them is caught too. + * + * A program with per-CPU state (prog->percpu_state) runs with preemption + * disabled, so that no other run of it starts on this CPU before it exits. A + * sleepable one has to use bpf_preempt_disable() itself. The frame of a + * non-sleepable one is linked only while preemption is disabled: a task switch + * runs the scheduler, and a program there must not find @prog in the chain. + * + * Returns false if @prog must not run. Call bpf_prog_exit_run_ctx() either way. + * Always inline: the callers are notrace. + */ +static __always_inline bool bpf_prog_enter_run_ctx(struct bpf_prog *prog, + struct bpf_run_ctx *run_ctx) { + struct bpf_run_ctx *ctx, *old_ctx = NULL; + #ifdef CONFIG_BPF_SYSCALL - current->bpf_ctx = old_ctx; + old_ctx = READ_ONCE(current->bpf_ctx); #endif + run_ctx->saved_run_ctx = old_ctx; + run_ctx->prog = prog; + if (prog->percpu_state) { + if (!prog->sleepable) + preempt_disable_notrace(); + /* sleepable prog should be using explicit bpf_preempt_disable() */ + } + for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) { + if (ctx->prog == prog) + return false; + } +#ifdef CONFIG_BPF_SYSCALL + /* Initialize the frame before interrupts and NMIs on this CPU see it. */ + barrier(); + WRITE_ONCE(current->bpf_ctx, run_ctx); +#endif + return true; +} + +/* After an enter that returned false the frame is not linked, and the reset changes nothing */ +static __always_inline void bpf_prog_exit_run_ctx(struct bpf_prog *prog, + struct bpf_run_ctx *run_ctx) +{ + bpf_reset_run_ctx(run_ctx->saved_run_ctx); + if (!prog->percpu_state) + return; + if (!prog->sleepable) + preempt_enable_notrace(); } /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ @@ -3498,11 +3586,11 @@ static inline bool has_current_bpf_ctx(void) } /* An async callback runs in no hook, and current is not the task that has armed it */ -extern struct bpf_run_ctx bpf_async_run_ctx; - static inline bool bpf_in_async_cb(void) { - return current->bpf_ctx == &bpf_async_run_ctx; + struct bpf_run_ctx *ctx = current->bpf_ctx; + + return ctx && ctx->prog == BPF_PROG_ASYNC_CB; } void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog); @@ -3519,7 +3607,6 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array, { const struct bpf_prog_array_item *item; struct bpf_prog *prog; - struct bpf_run_ctx *old_run_ctx; struct bpf_trace_run_ctx run_ctx; u32 ret = 1; @@ -3530,7 +3617,6 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array, run_ctx.is_uprobe = false; - old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); item = &array->items[0]; while ((prog = READ_ONCE(item->prog))) { /* Skip dummy_bpf_prog placeholder (len == 0) */ @@ -3539,15 +3625,14 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array, continue; } - if (unlikely(!bpf_prog_get_recursion_context(prog))) { + run_ctx.bpf_cookie = item->bpf_cookie; + if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx.run_ctx))) { bpf_prog_inc_misses_counter(prog); - bpf_prog_put_recursion_context(prog); + bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx); item++; continue; } - run_ctx.bpf_cookie = item->bpf_cookie; - if (!prog->sleepable) { guard(rcu)(); ret &= run_prog(prog, ctx); @@ -3555,10 +3640,9 @@ bpf_prog_run_array_sleepable(const struct bpf_prog_array *array, ret &= run_prog(prog, ctx); } - bpf_prog_put_recursion_context(prog); + bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx); item++; } - bpf_reset_run_ctx(old_run_ctx); migrate_enable(); return ret; } diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 7b3e10a4a97e..aea624c85445 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1235,14 +1235,13 @@ static DEFINE_PER_CPU(struct bpf_hrtimer *, hrtimer_running); static void bpf_async_refcount_put(struct bpf_async_cb *cb); -struct bpf_run_ctx bpf_async_run_ctx; - static void bpf_run_async_cb(struct bpf_prog *prog, bpf_callback_t callback_fn, struct bpf_map *map, void *key, void *value) { - struct bpf_run_ctx *old_ctx = bpf_set_run_ctx(&bpf_async_run_ctx); + struct bpf_run_ctx run_ctx, *old_ctx; struct bpf_timed_may_goto saved; + old_ctx = bpf_set_run_ctx_prog(&run_ctx, BPF_PROG_ASYNC_CB); bpf_timed_may_goto_save(prog, &saved); callback_fn((u64)(long)map, (u64)(long)key, (u64)(long)value, 0, 0); bpf_timed_may_goto_restore(prog, &saved); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 0bdb4288beb2..0525dc376c9a 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3878,6 +3878,21 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, err = -EINVAL; goto out_unlock; } + /* + * An extension runs in the run of its target. One that keeps per-CPU + * state relies on its runs not overlapping on a CPU, so a target that + * is checked per task must run with preemption disabled: marked and + * not sleepable. + */ + if (prog->type == BPF_PROG_TYPE_EXT && prog->percpu_state && + (tgt_prog->type == BPF_PROG_TYPE_RAW_TRACEPOINT || + tgt_prog->type == BPF_PROG_TYPE_TRACEPOINT) && + (!tgt_prog->percpu_state || tgt_prog->sleepable)) { + bpf_log(log, + "Freplace prog with per-CPU state needs a target that runs with preemption disabled.\n"); + err = -EINVAL; + goto out_unlock; + } /* * The size of these contexts depends on what the program is attached * to, or run with. That is compared with max_ctx_offset of the program, diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 96454443441b..619297eae8c1 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -1341,9 +1341,7 @@ static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tram { rcu_read_lock_dont_migrate(); - run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); - - if (unlikely(!bpf_prog_get_recursion_context(prog))) { + if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx->run_ctx))) { bpf_prog_inc_misses_counter(prog); if (prog->aux->recursion_detected) prog->aux->recursion_detected(prog); @@ -1384,10 +1382,9 @@ static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx) __releases(RCU) { - bpf_reset_run_ctx(run_ctx->saved_run_ctx); - + /* Before the exit, which can enable preemption: time off the CPU is not run time */ update_prog_stats(prog, start); - bpf_prog_put_recursion_context(prog); + bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx); rcu_read_unlock_migrate(); } @@ -1400,7 +1397,7 @@ static u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog, */ rcu_read_lock_dont_migrate(); - run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); + bpf_set_run_ctx(&run_ctx->run_ctx); return NO_START_TIME; } @@ -1409,7 +1406,7 @@ static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx) __releases(RCU) { - bpf_reset_run_ctx(run_ctx->saved_run_ctx); + bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx); rcu_read_unlock_migrate(); } @@ -1421,9 +1418,7 @@ u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, migrate_disable(); might_fault(); - run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); - - if (unlikely(!bpf_prog_get_recursion_context(prog))) { + if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx->run_ctx))) { bpf_prog_inc_misses_counter(prog); if (prog->aux->recursion_detected) prog->aux->recursion_detected(prog); @@ -1435,10 +1430,8 @@ u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx) { - bpf_reset_run_ctx(run_ctx->saved_run_ctx); - update_prog_stats(prog, start); - bpf_prog_put_recursion_context(prog); + bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx); migrate_enable(); rcu_read_unlock_trace(); } @@ -1450,7 +1443,7 @@ static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, migrate_disable(); might_fault(); - run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); + bpf_set_run_ctx(&run_ctx->run_ctx); return bpf_prog_start_time(); } @@ -1458,7 +1451,7 @@ static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, static void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx) { - bpf_reset_run_ctx(run_ctx->saved_run_ctx); + bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx); update_prog_stats(prog, start); migrate_enable(); @@ -1471,7 +1464,7 @@ static u64 notrace __bpf_prog_enter(struct bpf_prog *prog, { rcu_read_lock_dont_migrate(); - run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); + bpf_set_run_ctx(&run_ctx->run_ctx); return bpf_prog_start_time(); } @@ -1480,7 +1473,7 @@ static void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx) __releases(RCU) { - bpf_reset_run_ctx(run_ctx->saved_run_ctx); + bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx); update_prog_stats(prog, start); rcu_read_unlock_migrate(); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index dd35abb37a23..2e3be7c11221 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -5434,7 +5434,8 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog) * program attached to both a syscall and a regular tracepoint. They always * use the task's kernel stack. Kprobe multi, kprobe session and perf_event * programs are serialized per CPU by bpf_prog_active, and struct_ops - * programs that request a private stack keep per-CPU recursion protection. + * programs that get a private stack run with preemption disabled, see + * bpf_prog_enter_run_ctx(). A sleepable one can't, so it gets none. */ switch (prog->type) { case BPF_PROG_TYPE_KPROBE: @@ -5450,7 +5451,7 @@ static enum priv_stack_mode bpf_enable_priv_stack(struct bpf_prog *prog) case BPF_PROG_TYPE_PERF_EVENT: return PRIV_STACK_ADAPTIVE; case BPF_PROG_TYPE_STRUCT_OPS: - if (prog->aux->priv_stack_requested) + if (prog->aux->priv_stack_requested && !prog->sleepable) return PRIV_STACK_ADAPTIVE; fallthrough; default: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 7b9bd6f560a5..2031c3727320 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2125,7 +2125,6 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) struct srcu_ctr __percpu *scp = NULL; struct bpf_prog *prog = link->link.prog; bool sleepable = prog->sleepable; - struct bpf_run_ctx *old_run_ctx; struct bpf_trace_run_ctx run_ctx; if (sleepable) { @@ -2135,19 +2134,15 @@ void __bpf_trace_run(struct bpf_raw_tp_link *link, u64 *args) rcu_read_lock_dont_migrate(); } - if (unlikely(!bpf_prog_get_recursion_context(prog))) { + run_ctx.bpf_cookie = link->cookie; + if (unlikely(!bpf_prog_enter_run_ctx(prog, &run_ctx.run_ctx))) { bpf_prog_inc_misses_counter(prog); goto out; } - 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); out: - bpf_prog_put_recursion_context(prog); + bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx); if (sleepable) { migrate_enable(); diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index cc3236e3c0c4..8b8c4e9ded9d 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -748,9 +748,6 @@ __bpf_prog_test_run_raw_tp(void *data) struct bpf_raw_tp_test_run_info *info = data; struct srcu_ctr __percpu *scp = NULL; struct bpf_trace_run_ctx run_ctx = {}; - struct bpf_run_ctx *old_run_ctx; - - old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx); if (info->prog->sleepable) { scp = rcu_read_lock_tasks_trace(); @@ -759,7 +756,7 @@ __bpf_prog_test_run_raw_tp(void *data) rcu_read_lock(); } - if (unlikely(!bpf_prog_get_recursion_context(info->prog))) { + if (unlikely(!bpf_prog_enter_run_ctx(info->prog, &run_ctx.run_ctx))) { bpf_prog_inc_misses_counter(info->prog); goto out; } @@ -767,7 +764,7 @@ __bpf_prog_test_run_raw_tp(void *data) info->retval = bpf_prog_run(info->prog, info->ctx); out: - bpf_prog_put_recursion_context(info->prog); + bpf_prog_exit_run_ctx(info->prog, &run_ctx.run_ctx); if (info->prog->sleepable) { migrate_enable(); @@ -775,8 +772,6 @@ __bpf_prog_test_run_raw_tp(void *data) } else { rcu_read_unlock(); } - - bpf_reset_run_ctx(old_run_ctx); } int bpf_prog_test_run_raw_tp(struct bpf_prog *prog, diff --git a/net/ipv4/bpf_tcp_ops.c b/net/ipv4/bpf_tcp_ops.c index 926b29d36883..6b8ad08fa54d 100644 --- a/net/ipv4/bpf_tcp_ops.c +++ b/net/ipv4/bpf_tcp_ops.c @@ -207,11 +207,11 @@ BPF_CALL_0(bpf_tcp_ops_get_retval) * return value chained across the bpf_tcp_ops attached to the cgroup * and is this program's saved_run_ctx. */ - if (bpf_in_async_cb() || WARN_ON_ONCE(!ctx->saved_run_ctx)) + if (bpf_in_async_cb() || WARN_ON_ONCE(!ctx->run_ctx.saved_run_ctx)) return 0; - return container_of(ctx->saved_run_ctx, struct bpf_tramp_run_ctx, - run_ctx)->retval; + return container_of(ctx->run_ctx.saved_run_ctx, + struct bpf_tramp_run_ctx, run_ctx)->retval; } const struct bpf_func_proto bpf_tcp_ops_get_retval_proto = { -- 2.43.0