Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
From: Andrii Nakryiko
Date: Fri Dec 04 2026 - 14:42:20 EST
On Thu, Dec 3, 2026 at 12:03 PM Alexei Starovoitov
<alexei.starovoitov@xxxxxxxxx> wrote:
>
> On Thu Dec 3, 2026 at 1:44 AM UTC, Alexei Starovoitov wrote:
> > On Wed Dec 2, 2026 at 5:29 PM UTC, Andrii Nakryiko wrote:
> > >
> > > slot = (old & 1) ? scratch_slot_get_slow(mask, old, depth) : zero;
> > >
> > > 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.
> >
> > Will check.
>
> I see the growth since 7.1. That line alone doesn't give me 3x and 1M
> together. Processed insns of wprof_task_switch in pull 170, -mcpu=v4,
> default rodata:
>
> clang 20 clang 24
> kernel zero 0 zero 0
> ---------------- ---- ---- ---- ----
> 6.18 164K 166K 157K 159K
> 7.1 218K 259K 206K 248K
> 7.3 234K 320K 212K 294K
> 7.4-rc 159K 245K 137K 219K
> bpf-next 174K 242K 314K 511K
> bpf-next + [1] 93K 94K 112K 112K
>
> Which kernel, clang and options did you use?
> Did you change only this line?
scrolling back through history, that 3x was a different issue still.
By removing scratch_slot_put() in one of code paths for ut_xxx family
of programs, there was a major change in register allocation and
Meta-internal 6.9 kernel's verifier went crazy. I undid this
"optimization" and that 3x issue went away. Sorry for false alarm.
The "0" vs "zero" effect I was seeing is similar to what you have in
the table above, up to 60%-ish percent increase, not 3x.
>
> The elision is there since 6.14 and costs nothing up to 7.1.
> The growth has two causes. clang 20, each cause removed:
>
> kernel zero 0 no spill same ids both
> -------- ---- ---- -------- -------- ----
> 7.1 218K 259K 218K 259K 218K
> 7.3 234K 320K 255K 292K 234K
> 7.4-rc 159K 245K 180K 217K 159K
> bpf-next 174K 242K 180K 243K 181K
>
> - Spill, since 7.1. With 0 clang keeps 'prev' in r3 instead of r6.
> One arm of task_id() spills it around bpf_get_smp_processor_id().
> task_infos_emit() walks items[] with a pointer. Stack liveness takes
> that as a read of the whole frame. So the dead slot keeps the two arms
> apart. The value 0 plays no role here.
> - Ids, since 7.2. The NULL check keeps the id, see 06d518a5583f.
> An elided lookup has no id, so check_ids() fails.
>
> So the gain depends on register allocation. With SCRATCH_DEPTH 4
> 7.3 takes 290K with 'zero' and 243K with 0.
>
> Pls keep 'zero' for released kernels. Without the other 'zero' lines
yep, I'm keeping it even if for latest kernel it won't matter
> 11 wprof_ut_* progs hit 1M on 6.18 to 7.4-rc.
> bpf-next loads all 46 progs without 'zero'.
>
> wprof_task_switch is slow on bpf-next for two more reasons:
>
> - task_state() returns a value of one of two maps. Such states stay
> apart.
yep, fallback from task local storage to backup hashmap, useful
> - task_infos_emit() loads tis->items[i].task with a variable offset.
> Pass 0 gets a number and fails, so more passes follow.
and this part is a necessary complexity as well, huge savings in practice
>
> [1] fixes the liveness and the two maps.
> With it 0 and 'zero' cost the same, 135-185 ms instead of 250-750 ms.
>
> [1] https://lore.kernel.org/bpf/20261203195823.4058490-1-alexei.starovoitov@xxxxxxxxx/