Re: [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx
From: Andrii Nakryiko
Date: Tue Nov 24 2026 - 17:05:09 EST
On Tue, Nov 17, 2026 at 3:29 AM Mike Bazov via B4 Relay
<devnull+mikeba.sentinelone.com@xxxxxxxxxx> wrote:
>
> From: Mike Bazov <mikeba@xxxxxxxxxxxxxxx>
>
> 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. A program can't avoid that:
> the counter is checked before its first insn.
>
> Since commit 7dadeaa6e851 ("sched: Further restrict the preemption
> modes") x86 and arm64 have only lazy and full preemption, so this
> happens in normal operation. A security agent that pairs fentry and
> fexit programs on inet_sendmsg() and inet_recvmsg() loses one half of a
> pair with every drop. With 200,000 HTTP requests and four CPU-bound
> tasks on a 4-CPU VM, 9,254 of 7,966,760 runs of these programs were
> dropped on bpf-next, and none of 7,975,426 with this series.
>
> Check recursion per task instead. The runners already link a
> stack-allocated bpf_run_ctx into current->bpf_ctx. Move saved_run_ctx
> into struct bpf_run_ctx, so every frame links to the outer one, and
> record in each frame the program it runs. A program is recursing if a
> frame in the current task's chain already runs it. Interrupts and NMIs
> run on top of the interrupted task's chain, so recursion through them
> is still caught.
>
> bpf_prog_enter_run_ctx() and bpf_prog_exit_run_ctx() do the check for
> all runners. The syscall tracepoint runner linked one frame around its
> program array and now links one per program. bpf_set_run_ctx() accepts
> a NULL ctx, which io_uring passes to hide the BPF context; the frames
> below are not walked while it is hidden.
>
> Programs marked by the previous patch keep the per-CPU counter and
> behave exactly as before, so no counted drop is traded for corrupted
> per-CPU data or a renewed may_goto budget. An extension (freplace) runs
> under the check of its target, so a marked one can't be attached to a
> target that is checked per task.
>
> Trade-offs:
>
> - An unmarked program that keeps per-CPU slots of its own, see the
> previous patch, can have two runs use one slot, and nothing counts
> that.
> - Marked programs lose runs to preemption as before.
> - bench trig-rawtp is about 2% slower: 42.6 and 42.1 M/s on bpf-next,
> 41.3 and 41.5 M/s with the series. trig-fentry and trig-fexit are
> not slower.
> - Attaching a marked extension to an unmarked raw_tp, tp_btf or
> tracepoint program now fails with -EINVAL.
>
> Signed-off-by: Mike Bazov <mikeba@xxxxxxxxxxxxxxx>
> ---
> include/linux/bpf.h | 95 ++++++++++++++++++++++++++++++++++++++------
> include/linux/bpf_verifier.h | 19 +++++++++
> kernel/bpf/syscall.c | 10 +++++
> kernel/bpf/trampoline.c | 26 +++++-------
> kernel/trace/bpf_trace.c | 11 ++---
> net/bpf/test_run.c | 9 +----
> net/ipv4/bpf_tcp_ops.c | 6 +--
> 7 files changed, 129 insertions(+), 47 deletions(-)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index 7873f0ef2817..97cbcd4b0d67 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -2783,7 +2783,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;
> + /* Set while a program whose recursion is checked per task runs in the frame */
> + const struct bpf_prog *prog;
> +};
>
> struct bpf_cg_run_ctx {
> struct bpf_run_ctx run_ctx;
> @@ -2801,7 +2810,6 @@ 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)
> @@ -2809,8 +2817,17 @@ static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
> 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 = NULL;
> + }
[...]
> /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
> @@ -3492,7 +3565,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;
>
> @@ -3503,7 +3575,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) */
> @@ -3512,15 +3583,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))) {
it feels like bpf_prog_enter_run_ctx() is doing too much and the logic
becomes convoluted... we had old_run_ctx above after setting current
run_ctx, why can't we pass that *old* run_ctx and our prog pointer
into bpf_prog_enter_run_ctx() to check that our prog is not already
running (and do that recursion context accounting). And before that
you already optimistically set run_ctx.bpf_cookie, you can just as
much set program pointer.
So in short, bpf_set_run_ctx() sets up chain of run_ctx, and then in
the inner loop we set up bpf_cookie + current prog (sort of
speculatively), and then bpf_prog_enter_run_cxt() checks if we can run
prog or not.
Am I missing some complications that would prevent that separation of concerns?
> 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);
> @@ -3528,10 +3598,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);
same here, what's wrong with doing reset just once, all we really need
is to update bpf_cookie and prog pointer inside that while() loop over
bpf_prog_array items, right? the chaining itself doesn't really change
and shouldn't be done for each item separately.
> migrate_enable();
> return ret;
[...]