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

From: Andrii Nakryiko

Date: Wed Nov 25 2026 - 00:35:25 EST


On Tue, Nov 24, 2026 at 6: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.

it's hard to weed through that verbiage in section 6, are you
proposing to mark "needs per-cpu protection" if BPF program is calling
bpf_get_smp_processor_id()?

>
> > 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.

.percpu is backed by PERCPU_ARRAY, so we don't need any data to say
that it will trigger the protection, do we?

>
> > 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.

yeah, I was lazy :)

> 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

yeah, this is true, just a stack-like approach doesn't give 100%
guarantee (though in practice it has been good enough so far, though
perhaps I just didn't heavily run into new preemptible kernels), but a
better approach would be to use bitset + find first unset bit
approach. I'll play with it.

>
> 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.
>
> Now the bigger question: do we need preempt_disable() at all?
> We do. Without it progs of 17 out of 69 projects in [1] corrupt their
> data. 12 of them have progs that are clean today.
>

"Can corrupt in rare circumstances (in practice)", but sure, sure :)

Still, choosing between handling all events and having bugs (fixable)
of handling per-CPU data in my BPF program, I'll choose the former.
Even if we don't want to make that a default, that's fine, but please
at least give me a way to opt out from the kernel's
over-protectiveness :) and not using per-cpu maps is not a way to opt
out, it has its valid uses even with preemption. Think about simple
per-CPU stats/counters updated with atomic operations and aggregated
across CPUs in user space, periodically. E.g., I believe BPF
selftests's bench uses this approach, why would it need preemption
disablement just for this?

[...]