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=1796039659; b=nsyLP1NbyDjE+ZwmVcZhP1BwCLwj2thdQRABr6eT7ixqhqbJ/MFOttYwBMbdw4j1mgGD B16INOQWEMvQgcIpm1l+XB4/RrVKVjMCv7zX8Xukv2YaNtei7iyG4HmihwTluwEbJ55Fs rbkvXQSo9VnC291fTuki3rTNIRyidOGhXJXHU2s5v/r2aHaHy3rEwcFzm8JnXuZRRLTsG ek9XbQyFqqoBi0dzxZRyKDIbgIVcXAx5kyQQJp/OUSxuc1FC0rSI6w8aEUza0ehs4R3nz HyBBYO1ICq9NcGKjNu2yOHGwy6VECI6+Gd4uIOsVuI7LOqgrjEmharN7P+pXdwlrYlg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796039659; 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=FPz9wxQksqR1veL44wlDdxlTNkBkh2rz/52di4Wtpp0=; b=ZLn8rQIO8chGtitLm3Ap+yuzCeafUAPtqlrCUBga/4TTBZuQ+1evgbVGxHkULhu7ZMVg aNr1CnKJax07agdyGPauy/6lV58YRQf/u1YCxUr+4Zsv0VPg6H9mYVnDEe7ND+B/ao4bd OYypL6/IswGGajZqwhz4L31FL8mXN3O4TOztThgS1DgDSAJDIrlPKAS6sl5c8j6QM1Gm2 WYXpszTVFuMe5vDr1TDLdaCGp5GuWR/S1iKCJuAN7F8SLtJXsE3lFzpCDV1yjTIyK09Mq 9drzCpmXXAJyzOgcqNWsiQZAgqGHiRvLPG9dRNh9j7WU+C93UhH5KFV85ymbdOBC4/A== 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=IwIEWhle; 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 C64D9606A329; Mon, 30 Nov 2026 11:54:19 +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=IwIEWhle 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=1796039659; b=zqN6636MLECGI0LypV6R1UrgLfiJ+Et3AvDlCuIPoFJ5hUO/MbKqML7TuFrYoIuBxv5O rXNI7t9Qot1Hist4k2ZC9nBNEZgboYTHLp9zCO91lboaXVgEC/veufuPW5qshdbA9wm6R jlKmFgIonF89q03qVonef179Mhr+aDevA9coWrz51wJeFRsDN8JzsLyGIhs7rjtErmprJ EMzcLBI6yWLoPCBty56mcE++soP2f9ix6kM1qgiq1NKBABoyJKutLMddfJbZF0OKs8c+b qu/YwTImX72GJmOATSOVVjBdwD7ekMV4HI1RJfyE1lPro7K+DA6zb8y3lFdUqcgoCQg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796039659; 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=FPz9wxQksqR1veL44wlDdxlTNkBkh2rz/52di4Wtpp0=; b=VVdUcLYInGYz74Rb41zNzEuLhuLTHmq3qlxJKcoO8GBvaL9LL9Z2SHBviKBgRK+afkxp qPqJCwXXUFfaF0eGuk0D/G1bLH+iDuSTqZ9I5/uEyeOS16rBhQHKv50OInuuNZGcgk4Ht wNchYXrRdKXY4ZJfkh2VFK5lT53V56kMgWQKzseUPQqOvj9FfMVGgdyzuSs+S+m0xw6UZ 0aBjCoA+WtqRM6tDLDs41AGXyek+n9mY1l2MuzeAvthy/ByFKRjSb79AVBC1F//adjqCd 1Us2H+CRZLmxxF1En95bB1x4mTsD7xbn8LuxDRvCERxJxoNdq7XtKiEAIG7RiWfl6Kg== 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 5667B1F00932; Mon, 30 Nov 2026 11:54:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796039659; bh=FPz9wxQksqR1veL44wlDdxlTNkBkh2rz/52di4Wtpp0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=IwIEWhleiP6adRwLvcNjij06b3fyW91K+HAcmpAggML7glj1AY3GA8CpzL1hiK6CY 5MtQ5bHs/bxXoxZ8YFuJo7d2dNzZohy2j9cCH7ZZERslQfDuOUKqx7tMhtKh7vf+YJ baQq4lt53Df6tdlcSKMs/doZtjffdv1GHuSWpDbTREEkcjoH7OVdu0UME3BeIfx0qe X0ett3c7e3Vx8vhXA+9WEf3ggoGwpeATIW6UAqiJaXYPFM6EKwajmrgF5mXTXDq/rr R356V2gNmpfFWBvFMhzl+Nf6uGhl7ZXgOUkpak/X0ZuGw8HAxbHzvcm8B+aecUzksB S3tL/2vS45mNA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id D3F1F4E808; Mon, 30 Nov 2026 11:54:05 +0000 (UTC) From: Mike Bazov via B4 Relay Date: Mon, 30 Nov 2026 13:54:04 +0200 Subject: [PATCH bpf-next v4 4/6] 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: <20261130-bpf-preempt-v2-r2-v4-4-bd28389dfd64@sentinelone.com> References: <20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com> In-Reply-To: <20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@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=1796039644; l=18015; i=mikeba@sentinelone.com; s=20261022; h=from:subject:message-id; bh=MAaQQ1OVLPqgOBBNVVUsO6wOeX6x33C8tyy2aujmeqM=; b=y62zitX9/5YPxfX+3p92FB330j/vP1cN8hPHjtIo6JOMSRU9k5CIjAYWrvBQXGKWJAukk+mzE maxFFoeCQL1Ay4xALsArTa9HNsPUKg0fg8V6MllIygpYITykumQqJYJ 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 (fentry, fexit, fsession, fmod_ret and their sleepable variants), raw tracepoint programs (raw_tp, raw_tp.s, tp_btf) 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, and drop the per-CPU counter. 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. The runners that check recursion link their frame with bpf_set_run_ctx_prog(), which records the program in the same step, so each field of the frame is written once. bpf_prog_enter_run_ctx() then refuses a program that a frame further out already runs. 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 one frame for its program array and sets the program per run. A program marked by the previous patch runs with preemption disabled instead, so that no other run of it starts on the CPU before it exits. Its per-CPU data stays its own, and the runs of other tasks wait instead of being dropped. This also keeps apart programs that share a per-CPU map, which the per-program counter never did. A sleepable program can't disable preemption: it has to use bpf_preempt_disable() itself. An extension (freplace) runs in the run of its target, so a marked extension can't be attached to a target that runs preemptibly. 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. - An async callback starts a chain of its own, so a program that runs in the task it interrupted can run once more inside the callback. - 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 an unmarked raw_tp, tp_btf, tracepoint or fmod_ret program now fails with -EINVAL. Signed-off-by: Mike Bazov --- include/linux/bpf.h | 87 ++++++++++++++++++++++++++++++++++++++------ include/linux/bpf_verifier.h | 23 ++++++++++++ kernel/bpf/syscall.c | 10 +++++ kernel/bpf/trampoline.c | 34 +++++++++-------- kernel/bpf/verifier.c | 3 +- kernel/trace/bpf_trace.c | 12 +++--- net/bpf/test_run.c | 10 ++--- net/ipv4/bpf_tcp_ops.c | 6 +-- 8 files changed, 141 insertions(+), 44 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 95e0d1bcc1d5..b24f26a48b77 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2800,7 +2800,16 @@ 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() */ + const struct bpf_prog *prog; +}; struct bpf_cg_run_ctx { struct bpf_run_ctx run_ctx; @@ -2818,27 +2827,81 @@ 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 now points to. + */ +static 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 struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx) +{ + return bpf_set_run_ctx_prog(new_ctx, NULL); +} + static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx) { #ifdef CONFIG_BPF_SYSCALL - current->bpf_ctx = old_ctx; + WRITE_ONCE(current->bpf_ctx, old_ctx); + /* Unlink the frame before its stack storage can be reused. */ + barrier(); #endif } +/* + * Check whether @prog may run in the frame that the caller has just linked on + * top of @old_ctx. It must not if a frame from @old_ctx outward runs @prog + * already: that would be recursion. 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. + * + * 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 *old_ctx) +{ + struct bpf_run_ctx *ctx; + + if (prog->percpu_state && !prog->sleepable) + preempt_disable_notrace(); + for (ctx = old_ctx; ctx; ctx = READ_ONCE(ctx->saved_run_ctx)) { + if (READ_ONCE(ctx->prog) == prog) + return false; + } + return true; +} + +static __always_inline void bpf_prog_exit_run_ctx(struct bpf_prog *prog, + struct bpf_run_ctx *run_ctx) +{ + if (prog->percpu_state && !prog->sleepable) + preempt_enable_notrace(); +} + /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */ #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0) /* BPF program asks to set CN on the packet. */ @@ -3538,15 +3601,16 @@ 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; + WRITE_ONCE(run_ctx.run_ctx.prog, prog); + if (unlikely(!bpf_prog_enter_run_ctx(prog, old_run_ctx))) { bpf_prog_inc_misses_counter(prog); - bpf_prog_put_recursion_context(prog); + bpf_prog_exit_run_ctx(prog, &run_ctx.run_ctx); + WRITE_ONCE(run_ctx.run_ctx.prog, NULL); item++; continue; } - run_ctx.bpf_cookie = item->bpf_cookie; - if (!prog->sleepable) { guard(rcu)(); ret &= run_prog(prog, ctx); @@ -3554,7 +3618,8 @@ 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); + WRITE_ONCE(run_ctx.run_ctx.prog, NULL); item++; } bpf_reset_run_ctx(old_run_ctx); diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 50b3f8ab9720..787e019cd807 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -2039,6 +2039,29 @@ static inline bool bpf_prog_check_recur(const struct bpf_prog *prog) } } +/* Is @prog run through bpf_prog_enter_run_ctx()? */ +static inline bool bpf_prog_enters_run_ctx(const struct bpf_prog *prog) +{ + switch (resolve_prog_type(prog)) { + case BPF_PROG_TYPE_RAW_TRACEPOINT: + case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE: + /* It may be attached to a syscall tracepoint */ + case BPF_PROG_TYPE_TRACEPOINT: + return true; + case BPF_PROG_TYPE_TRACING: + case BPF_PROG_TYPE_STRUCT_OPS: + return bpf_prog_check_recur(prog); + default: + return false; + } +} + +/* Can two tasks run @prog at once on a CPU: it is checked per task and runs preemptibly? */ +static inline bool bpf_prog_recur_per_task(const struct bpf_prog *prog) +{ + return !prog->percpu_state && bpf_prog_enters_run_ctx(prog); +} + #define BPF_REG_TRUSTED_MODIFIERS (MEM_ALLOC | PTR_TRUSTED | NON_OWN_REF) static inline bool bpf_type_has_unsafe_modifiers(u32 type) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 198438b95849..62830776c4b9 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3879,6 +3879,16 @@ 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, which a target that + * runs preemptibly doesn't provide. + */ + if (prog->type == BPF_PROG_TYPE_EXT && prog->percpu_state && + bpf_prog_recur_per_task(tgt_prog)) { + 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..72656e85d794 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -1339,11 +1339,12 @@ static __always_inline u64 notrace bpf_prog_start_time(void) static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx) __acquires(RCU) { - rcu_read_lock_dont_migrate(); + struct bpf_run_ctx *old_run_ctx; - run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); + rcu_read_lock_dont_migrate(); - if (unlikely(!bpf_prog_get_recursion_context(prog))) { + old_run_ctx = bpf_set_run_ctx_prog(&run_ctx->run_ctx, prog); + if (unlikely(!bpf_prog_enter_run_ctx(prog, old_run_ctx))) { bpf_prog_inc_misses_counter(prog); if (prog->aux->recursion_detected) prog->aux->recursion_detected(prog); @@ -1384,10 +1385,10 @@ 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); + bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx); + bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx); update_prog_stats(prog, start); - bpf_prog_put_recursion_context(prog); rcu_read_unlock_migrate(); } @@ -1400,7 +1401,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 +1410,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(); } @@ -1417,13 +1418,14 @@ static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start, u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx) { + struct bpf_run_ctx *old_run_ctx; + rcu_read_lock_trace(); 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))) { + old_run_ctx = bpf_set_run_ctx_prog(&run_ctx->run_ctx, prog); + if (unlikely(!bpf_prog_enter_run_ctx(prog, old_run_ctx))) { bpf_prog_inc_misses_counter(prog); if (prog->aux->recursion_detected) prog->aux->recursion_detected(prog); @@ -1435,10 +1437,10 @@ 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); + bpf_prog_exit_run_ctx(prog, &run_ctx->run_ctx); + bpf_reset_run_ctx(run_ctx->run_ctx.saved_run_ctx); update_prog_stats(prog, start); - bpf_prog_put_recursion_context(prog); migrate_enable(); rcu_read_unlock_trace(); } @@ -1450,7 +1452,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 +1460,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 +1473,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 +1482,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 accded7c9280..d4805d4bb39c 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 request a private stack run with preemption disabled, see + * bpf_prog_enter_run_ctx(). */ switch (prog->type) { case BPF_PROG_TYPE_KPROBE: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 7b9bd6f560a5..4043eeb74681 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2135,19 +2135,17 @@ 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; + old_run_ctx = bpf_set_run_ctx_prog(&run_ctx.run_ctx, prog); + if (unlikely(!bpf_prog_enter_run_ctx(prog, old_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); + bpf_reset_run_ctx(old_run_ctx); if (sleepable) { migrate_enable(); diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c index cc3236e3c0c4..2667f86bec61 100644 --- a/net/bpf/test_run.c +++ b/net/bpf/test_run.c @@ -750,8 +750,6 @@ __bpf_prog_test_run_raw_tp(void *data) 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(); migrate_disable(); @@ -759,7 +757,8 @@ __bpf_prog_test_run_raw_tp(void *data) rcu_read_lock(); } - if (unlikely(!bpf_prog_get_recursion_context(info->prog))) { + old_run_ctx = bpf_set_run_ctx_prog(&run_ctx.run_ctx, info->prog); + if (unlikely(!bpf_prog_enter_run_ctx(info->prog, old_run_ctx))) { bpf_prog_inc_misses_counter(info->prog); goto out; } @@ -767,7 +766,8 @@ __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); + bpf_reset_run_ctx(old_run_ctx); if (info->prog->sleepable) { migrate_enable(); @@ -775,8 +775,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 432ca0783771..222d9420eb28 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