Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH bpf-next v5 0/5] bpf: Check recursion per task for preemptible programs
@ 2026-12-01 15:32 Mike Bazov via B4 Relay
  2026-12-01 15:32 ` [PATCH bpf-next v5 1/5] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Mike Bazov via B4 Relay @ 2026-12-01 15:32 UTC (permalink / raw)
  To: bpf, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Martin KaFai Lau, Eduard Zingerman, Kumar Kartikeya Dwivedi,
	Song Liu, Yonghong Song, Jiri Olsa, John Fastabend,
	Emil Tsalapatis, Ihor Solodrai, Steven Rostedt, Masami Hiramatsu,
	Mathieu Desnoyers, JP Kobryn, linux-trace-kernel, linux-kernel,
	Mike Bazov

fentry, fexit, raw_tp and syscall tracepoint programs can be preempted
while they run, but their recursion check is a per-CPU counter:

  task A enters prog on CPU 0       prog->active = 1
  task A is preempted inside prog
  task B enters prog on CPU 0       dropped, counted as recursion miss

Nothing recursed, but B's event is lost. Since 7.0, x86 and arm64 have
only lazy and full preemption, so this happens on stock kernels. Our
fentry/fexit probe on inet_sendmsg() and inet_recvmsg() loses 8,572 of
8 million runs under HTTP load on a 4-CPU VM. With the series it loses
none.

The series checks recursion per task instead, and removes the per-CPU
counter: a program is recursing only if the current task is already
inside it.

A program that keeps per-CPU state relies on no other run of it
starting on the CPU while it runs. The verifier marks such programs,
and they run with preemption disabled instead of dropping other runs:

  - per-CPU maps, bpf_this_cpu_ptr(), bpf_per_cpu_ptr()
  - bpf_get_smp_processor_id(): the CPU id often indexes per-CPU slots
    that the program keeps itself
  - tail calls: the target runs in the caller's run
  - a private stack (struct_ops that request one)

That also keeps apart programs that share a per-CPU map, which the
per-program counter never did.

Trade-offs:

- A marked program can't be preempted until it exits, and a program
  that tail-calls is marked.
- A sleepable program with per-CPU state gets no protection; it has to
  use bpf_preempt_disable() itself.
- Every program that calls bpf_get_smp_processor_id() is marked, also
  one that only records the CPU id. Per-CPU state that the verifier
  doesn't see at all stays unprotected.
- A run that preempts another one inside a timed may_goto loop can
  renew the other's 250 ms budget, as it already can for lsm programs.
  A fix for all prog types belongs to may_goto and is left for a
  separate series.
- Tracing, raw_tp and tracepoint programs lose the private stack
  (patch 1). It is already unsafe for them: two raw_tp programs that
  tail-call one handler let two tasks share its stack today. A program
  with two 2000-byte frames no longer loads.
- A marked freplace program can't be attached to a raw_tp or tracepoint
  program that is unmarked or sleepable: -EINVAL.

  1. bpf: Avoid per-CPU private stack in preemptible tracing progs
  2. bpf: Mark programs that keep per-CPU state
  3. bpf: Check program recursion per task via bpf_ctx
  4. bpf: Remove the per-CPU recursion counter of programs
  5. selftests/bpf: Add preemption recursion test for tracing programs

Testing, x86-64:

The selftest pins two tasks to one CPU and makes B enter the program
while A is preempted inside it, 20 rounds per subtest. With the series
all 25 subtests pass. On bpf-next 21 fail: B is dropped in every round,
or in two_dispatchers runs the shared handler inside A and corrupts
A's stack, and the freplace attach is not rejected. Only the four that
check true recursion pass on both. On v4, timer_cb and ext_attach fail.

The bench ran on an idle bare-metal x86 machine, used only for this
test, to avoid the variation of a VM: there, the same kernels moved by
up to 2% between boots.

bench -w3 -d9 --prod-affinity 5 --trig-batch-iters 1000 trig-<x>, M/s,
median of 3 runs, change vs bpf-next. Every run is within 0.1% of its
median.

               trig-fentry      trig-fexit       trig-rawtp
  bpf-next     46.92            38.18            47.88
  series       47.06  (+0.3%)   38.23  (+0.1%)   44.34  (-7.4%)

Every trig bench program calls bpf_get_smp_processor_id() to index its
hit counter, so with the mark they all run with preemption disabled.
Most of the raw_tp cost comes from that, see the v4 thread.

Changes in v5:

- Enter and exit follow Alexei's suggestion in v3: the frame of a marked
  program is linked and unlinked only while preemption is disabled, in
  every runner. v4's split layout is reverted: the order of linking and
  preemption has to stay in one place because of scheduling.
- An async callback links a frame of its own on the stack, per
  Alexei's suggestion in v4, so a program that the callback runs into
  is checked against the frames below. bpf_async_run_ctx is gone, and
  the async patch is folded into patch 3, which drops the v4 trade-off.
- The freplace check is done in bpf_tracing_prog_attach() without the
  helpers, rejects sleepable targets too, and logs why.
  RAW_TRACEPOINT_WRITABLE isn't listed: bpf_check_attach_target()
  already refuses to extend it.
- A sleepable struct_ops program no longer gets a private stack: it
  can't run with preemption disabled.
- __always_inline on the run context helpers.
- Selftest: new timer_cb subtest, and ext_attach covers sleepable and
  marked targets.
- Rebased on bpf-next.

v4: https://lore.kernel.org/bpf/20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com/
v3: https://lore.kernel.org/bpf/20261117-bpf-preempt-v2-r2-v3-0-586376ec3672@sentinelone.com/
v2: https://lore.kernel.org/bpf/20261111-bpf-preempt-v2-r2-v2-0-9d8f6b952ad2@sentinelone.com/
v1: https://lore.kernel.org/bpf/20261025-b4-bpf-trampoline-preempt-v1-0-198dcdc0c6ac@sentinelone.com/

---
Mike Bazov (5):
      bpf: Avoid per-CPU private stack in preemptible tracing progs
      bpf: Mark programs that keep per-CPU state
      bpf: Check program recursion per task via bpf_ctx
      bpf: Remove the per-CPU recursion counter of programs
      selftests/bpf: Add preemption recursion test for tracing programs

 include/linux/bpf.h                                | 160 ++++++---
 kernel/bpf/core.c                                  |  12 -
 kernel/bpf/helpers.c                               |   5 +-
 kernel/bpf/syscall.c                               |  15 +
 kernel/bpf/trampoline.c                            |  29 +-
 kernel/bpf/verifier.c                              |  58 +++-
 kernel/trace/bpf_trace.c                           |  11 +-
 net/bpf/test_run.c                                 |   9 +-
 net/ipv4/bpf_tcp_ops.c                             |   6 +-
 .../selftests/bpf/prog_tests/may_goto_priv_stack.c |   6 +-
 .../selftests/bpf/prog_tests/timed_may_goto.c      |   6 +-
 .../selftests/bpf/prog_tests/trampoline_preempt.c  | 383 +++++++++++++++++++++
 .../selftests/bpf/progs/may_goto_priv_stack.c      |  14 +-
 .../selftests/bpf/progs/test_global_func3.c        |   9 +
 .../selftests/bpf/progs/timed_may_goto_user.c      |  16 +-
 .../selftests/bpf/progs/trampoline_preempt.c       | 382 ++++++++++++++++++++
 .../selftests/bpf/progs/verifier_private_stack.c   |  81 +++--
 .../selftests/bpf/progs/verifier_stack_arg.c       |  38 +-
 .../testing/selftests/bpf/progs/verifier_unwind.c  |  33 +-
 19 files changed, 1101 insertions(+), 172 deletions(-)
---
base-commit: 595782f6799a3eeed50b3dd1fb954cc8ce3689ac
change-id: 20261111-bpf-preempt-v2-r2-f9f9a0dbd5d4

Best regards,
-- 
Mike Bazov <mikeba@sentinelone.com>



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-01 16:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 15:32 [PATCH bpf-next v5 0/5] bpf: Check recursion per task for preemptible programs Mike Bazov via B4 Relay
2026-12-01 15:32 ` [PATCH bpf-next v5 1/5] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
2026-12-01 15:46   ` sashiko-bot
2026-12-01 15:32 ` [PATCH bpf-next v5 2/5] bpf: Mark programs that keep per-CPU state Mike Bazov via B4 Relay
2026-12-01 15:32 ` [PATCH bpf-next v5 3/5] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
2026-12-01 16:05   ` sashiko-bot
2026-12-01 15:32 ` [PATCH bpf-next v5 4/5] bpf: Remove the per-CPU recursion counter of programs Mike Bazov via B4 Relay
2026-12-01 15:32 ` [PATCH bpf-next v5 5/5] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox