Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
From: Andrii Nakryiko
Date: Tue Nov 24 2026 - 16:48:16 EST
On Mon, Nov 23, 2026 at 10:21 PM Alexei Starovoitov
<alexei.starovoitov@xxxxxxxxx> wrote:
>
> On Tue, Nov 17, 2026 at 03:29 AM Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@xxxxxxxxxx> wrote:
> > - Marked programs are not fixed. A program with a per-CPU map or a
> > tail call loses runs to preemption as it does today.
>
> I suspect users will hate the behavior introdcued by patch 2+3,
> since adding a per-cpu map will suddenly make the prog to miss events.
> But the idea has legs.
>
> Earlier I proposed to insert bpf_preempt_disable() by the verifier.
> No need for that. bpf_prog_exit_run_ctx() can do it when per-cpu access
> is detected. Like below:
>
> > One question: the marking is automatic. A flag that the program's
> > author sets would avoid the second trade-off, but existing programs
> > would keep losing runs until they are rebuilt. Which do you prefer?
>
> No new uapi.
>
> > series #2 42.34 [42.12-42.78] 39.87 [39.46-41.38] 41.53 [40.79-41.75]
>
> Good enough. let's keep the walk. No need for the array.
>
> +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
> + 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) {
do you mind checking if unlikely() around ctx improves performance?
> + 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();
> }
>
> I think this should minimize the breakage and fix the issue.
>
So basically to opt out from this behavior BPF program would need to
stop using any per-CPU maps and just implement its own poor man's
per-CPU storage? And even using SEC(".percpu") would still be detected
as per-CPU usage, right?
Should we give a way to opt-out of this protection explicitly while
still using per-CPU maps?
> pw-bot: cr
>