Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs

From: Andrii Nakryiko

Date: Wed Dec 02 2026 - 12:30:29 EST


On Tue, Nov 24, 2026 at 7:00 PM Alexei Starovoitov
<alexei.starovoitov@xxxxxxxxx> wrote:
>
> On Tue, Nov 24, 2026 at 01:47 PM Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote:
> >> + for (ctx = old_ctx; ctx; ctx = ctx->saved_run_ctx) {
> >
> > do you mind checking if unlikely() around ctx improves performance?
>
> Mike,
> pls try it on respin.
>
> > 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?
>
> poor mans's per-cpu with bpf_get_smp_processor_id() shouldn't work
> as an opt-out. See section 6.
>
> > And even using SEC(".percpu") would still be detected
> > as per-CPU usage, right?
>
> This one is pretty much brand new, so there is no data to make
> a decision. I can see arguments both ways.
>
> > Should we give a way to opt-out of this protection explicitly while
> > still using per-CPU maps?
>
> After looking at the data, I think we shouldn't allow it.
>
> An opt-out is for a prog that protects its per-cpu data on its own.
> In the v1 thread your example was stack_trace_scratch in wprof.
> It doesn't work. wprof is broken today, without this series.
> An event gets args or a stack of another task. wprof 8dd41e18 on
> bpf-next, corrupt events / all events:
>
> -U what's wrong lazy full
> ----------------------------- ------------ -------- ---------
> u:func (arg:0, arg:1:str) args 1928/16M 9111/17M
> u:func (..., stack) stack 683/15M 3419/15M
> raw_tp:sys_enter (arg:id) and args 3491/48M 16673/44M
> tp:syscalls:sys_enter_openat
>
> utrace_scratch has no protection.

now it is with the work I mention below

> stack_trace_cur handles nesting, but not preemption:
>
> task A task B task C
> idx 0, st_cnt = 1
> preempted
> idx 1, st_cnt = 2
> preempted
> put, st_cnt = 1
> idx 1 <- slot of B
>

Took a bit longer with holidays and travel to finalize and polish, but
I've switched this to the bitset-based approach, see
https://github.com/anakryiko/wprof/pull/170/ if you are curious.

The was one bit of interesting "regression" in terms of verification
complexity (from [1]):

slot = (old & 1) ? scratch_slot_get_slow(mask, old, depth) : zero;
if (slot < 0)
return NULL;

hdr = bpf_map_lookup_elem(map, &slot);

If I change "zero" above to actual "0", it causes 3x more verification
instructions (which for the task switch handler gets me over 1mln),
because verifier starts tracking zero const value as special branch
everywhere, with eliding NULL check after map lookup, etc. zero global
var mitigates that.

If you get a chance, ask your AI to check if there are still holes in
that logic, thanks!

[1] https://github.com/anakryiko/wprof/pull/170/changes#diff-8b068f1af39425e57cf43ea5f26b4dd320f0ccd1e93990c13cdb9513cfa986b3R592

> With -S offcpu task C is wprof_task_switch. Then the utrace event has
> a record of kind ST_OFFCPU.
> uprobes have no protection. That's what an opt-out gives to raw_tp, tp
> and fentry progs:
>
> -U, lazy today preempt off opt-out
> ----------------------- -------- ----------- --------
> raw_tp only, args 0 0 2849/45M
> raw_tp only, stack 0 0 4081/27M
> raw_tp and tp, args 3491/48M 0 3166/54M
>
> The series doesn't touch uprobes, so wprof needs its own fix for them.
> The default mode of wprof, without -U, is fine.
>

[...]