Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
From: Mike Bazov
Date: Wed Nov 25 2026 - 07:03:43 EST
On Tue, Nov 24, 2026 at 08:26 PM Alexei Starovoitov wrote:
> Keep count and timestamp per-cpu. Call bpf_timed_may_goto_save() in
> bpf_prog_enter_run_ctx() and bpf_timed_may_goto_restore() in
> bpf_prog_exit_run_ctx(), like bpf_timer_cb() does.
> Then the preempted run gets its count and timestamp back.
i tried it, it works when the run that preempts finishes first, as
with bpf_timer_cb(). With preemption it can be preempted too, and the
preempted run can go on before it ends:
task A, cpu 0 task B, cpu 0
loops, preempted
save: A's budget into B's stack,
fresh one in the per-cpu struct
loops, preempted
goes on with B's fresh budget
exits: owner is B's, no restore
goes on, exits: restores A's,
which nobody uses any more
A fentry prog spins in a can_loop loop, 3 SCHED_OTHER tasks on one
CPU, 4 runs each:
kernel runs dropped longest over 500 ms
bpf-next 4 8 250 ms 0
save/restore 12 0 1253 ms 7 of 12
save/restore 12 0 1752 ms 9 of 12
save/restore (*) 24 0 2263 ms 22 of 24
(*) 6 tasks instead of 3
You can see it in the run times: they land near multiples of 250 ms,
one extra 250 ms for every fresh budget a run picked up from another.
An lsm prog has no prog->active, so the same test gives the same
result on plain bpf-next: 1253 ms, 8 of 12 over 500 ms. The overwrite
already exists for all progs that can be preempted (lsm, cgroup,
struct_ops, uprobe, iter, sk_msg, syscall).
A fix for all of them has to change how may_goto keeps its budget:
lsm, cgroup and sk_msg progs never go through bpf_prog_enter_run_ctx().
I'd rather split it out of this series: v4 takes may_goto out of the
mark, so tracing progs behave like lsm ones today, and a separate
series fixes the budget for all prog types. Does that work for you?
Thanks,
Mike