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

From: Andrii Nakryiko

Date: Tue Dec 01 2026 - 16:06:33 EST


On Wed, Nov 25, 2026 at 8:29 PM Alexei Starovoitov
<alexei.starovoitov@xxxxxxxxx> wrote:
>
> On Wed Nov 25, 2026 at 5:35 AM UTC, Andrii Nakryiko wrote:
> > 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()?
>
> yes, because it fixes real prog (falco).

I see, just wanted to confirm, as bpf_get_smp_processor_id() call
doesn't necessarily mean BPF program implements its own per-CPU
storage.

>
> >>
> >> > 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?
>
> I think it's slightly different vs normal percpu-array.
> .percpu is pseudo_map_value
> map is pseudo_map_fd.
> So we can distinguish if we want to.
> What I was trying to say is that .percpu is likely only in Leon's tool.
> So this one is easy to fix is we do _not_ want 'needs_percpu_prot' on it.
> I'm fine whichever way.

both are just variations of using PERCPU ARRAY map, so IMO keeping the
behavior the same would minimize surprises

>
> > Still, choosing between handling all events and having bugs (fixable)
> > of handling per-CPU data in my BPF program, I'll choose the former.
>
> but with the diff I proposed you get both:
> per-cpu maps work and there are no missed events.
>

ok, my bad, I didn't understand that what you propose is to inject
implicit preempt disable/enable instead of per-cpu-based "recursion
protection". Given this is non-sleepable BPF program, preemption
disable is probably not a big deal, agreed.

> > 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
>
> I don't follow what opt-out you're asking about.
> Out of implicit preempt disable when you're using per-cpu array?
> Why? How does it hurt your prog?

see above, I thought you still want to keep per-cpu-based recursion
protection for those cases, not just disable/enable preemption. In
that case yep, I don't think opt-out is warranted

>
> > out, it has its valid uses even with preemption. Think about simple
> > per-CPU stats/counters updated with atomic operations and aggregated
>
> wait what? per-cpu _with_ atomic? who does that?

you have userspace that can at any random time collect data across all
CPUs with no synchronization, so why not atomic? Also, uncontended
atomic isn't expensive and there is no cache line bouncing (most of
the time).

> bpf gained _local_ atomic just weeks ago.
> per-cpu with full strong atomic is odd. Why would anyone use that?
> The only case I can think of is nmi-safe-counters.

this as well, think about generic tool like bpftrace/wprof/whatnot
which can run arbitrary kprobe/tracepoint, you can't ever be sure if
it will run in NMI context. but anyways, I think my ask for opt-out
was based on not understanding that you just want to disable
preemption, so ignore me

> When we introduced per-cpu maps 10+ years ago the use case was
> packet bytes and count, so that bpf prog can do
> map_val->pkt_cnt++; map_val->pkt_bytes++;
> and that's what everyone was doing since then.
> combination of migrate_disable and preempt=lazy broke that accounting
> pattern, so implicit preempt_disable unbreaks them.
>

yep