mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mike Bazov via B4 Relay <devnull+mikeba.sentinelone.com@kernel.org>
To: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
	 Eduard Zingerman <eddyz87@gmail.com>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	 John Fastabend <john.fastabend@gmail.com>,
	 Emil Tsalapatis <emil@etsalapatis.com>,
	 Ihor Solodrai <ihor.solodrai@linux.dev>,
	 Steven Rostedt <rostedt@goodmis.org>,
	 Masami Hiramatsu <mhiramat@kernel.org>,
	 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	 JP Kobryn <jp.kobryn@linux.dev>,
	linux-trace-kernel@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Mike Bazov <mikeba@sentinelone.com>
Subject: [PATCH bpf-next v4 0/6] bpf: Check recursion per task for preemptible programs
Date: Mon, 30 Nov 2026 13:54:00 +0200	[thread overview]
Message-ID: <20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com> (raw)

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 9,254 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.
- An async callback starts a chain of its own, so a program that runs
  in the task it interrupted can run once more inside the callback.
- 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 an unmarked raw_tp,
  tp_btf, tracepoint or fmod_ret program: -EINVAL.

  1. bpf: Avoid per-CPU private stack in preemptible tracing progs
  2. bpf: Mark programs that keep per-CPU state
  3. bpf: Link the async run context without writing to it
  4. bpf: Check program recursion per task via bpf_ctx
  5. bpf: Remove the per-CPU recursion counter of programs
  6. 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 24 subtests pass. On bpf-next 21 fail: B is dropped in every round,
and the freplace attach is not rejected. Only the three that check true
recursion pass on both.

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 9 runs over 3 boots per kernel, change vs bpf-next. Every run
is within 0.5% of its median.

               trig-fentry      trig-fexit       trig-rawtp
  bpf-next     47.05            38.19            47.88
  series       47.09  (+0.1%)   38.42  (+0.6%)   44.33  (-7.4%)
  series *without* the bpf_get_smp_processor_id() mark of patch 2:
               48.74  (+3.6%)   39.61  (+3.7%)   46.03  (-3.9%)

Without the mark, fentry and fexit get faster without the per-CPU
counter, and raw_tp gets about 2 cycles per run slower: the bench runs
its program from a driver program, so the walk visits one frame, and
the program's atomic increment of its hit counter waits for the stores
that link the frame.

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:
fentry and fexit lose their gain, and trig-rawtp another 3.5%. Should
the bench programs stop calling the helper?

unlikely() on the walk made no measurable difference, so the series
doesn't use it.

Changes in v4:

- Marked programs run with preemption disabled instead of keeping the
  per-CPU counter, so they don't drop runs any more.
- The per-CPU counter is gone, and every program records itself in its
  frame.
- The runners link the frame first and then check, walking from the
  outer frame. The program is recorded when the frame is linked, by
  bpf_set_run_ctx_prog(), so each field of the frame is written once.
- Timed may_goto no longer marks a program. In v3 a mark kept the
  per-CPU counter; in v4 it disables preemption for the whole run,
  which would make a may_goto loop non-preemptible for up to 250 ms.
  Saving and restoring the budget around each run doesn't cover
  preemption, see the v3 thread.
- Programs that call bpf_get_smp_processor_id() are marked.
- New patch 3: bpf_async_run_ctx, which all CPUs share, is linked
  without writing to it. With the frame fields of patch 4, linking it
  with bpf_set_run_ctx() would let a program inside an async callback
  walk into another task's frames.
- Selftest: marked programs now expect B to run after A; new fexit,
  fmod_ret and smp_processor_id rows.

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 (6):
      bpf: Avoid per-CPU private stack in preemptible tracing progs
      bpf: Mark programs that keep per-CPU state
      bpf: Link the async run context without writing to it
      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                                | 126 +++++---
 include/linux/bpf_verifier.h                       |  23 ++
 kernel/bpf/core.c                                  |  12 -
 kernel/bpf/helpers.c                               |   4 +-
 kernel/bpf/syscall.c                               |  10 +
 kernel/bpf/trampoline.c                            |  34 ++-
 kernel/bpf/verifier.c                              |  58 +++-
 kernel/trace/bpf_trace.c                           |  12 +-
 net/bpf/test_run.c                                 |  10 +-
 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  | 338 +++++++++++++++++++++
 .../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       | 275 +++++++++++++++++
 .../selftests/bpf/progs/verifier_private_stack.c   |  81 ++---
 .../testing/selftests/bpf/progs/verifier_unwind.c  |  33 +-
 19 files changed, 919 insertions(+), 154 deletions(-)
---
base-commit: b81e432151a69b8dc07f4a398e2166268bee9a85
change-id: 20261111-bpf-preempt-v2-r2-f9f9a0dbd5d4

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



             reply	other threads:[~2026-11-30 11:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 11:54 Mike Bazov via B4 Relay [this message]
2026-11-30 11:54 ` [PATCH bpf-next v4 1/6] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 2/6] bpf: Mark programs that keep per-CPU state Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 3/6] bpf: Link the async run context without writing to it Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 4/6] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 5/6] bpf: Remove the per-CPU recursion counter of programs Mike Bazov via B4 Relay
2026-11-30 11:54 ` [PATCH bpf-next v4 6/6] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130-bpf-preempt-v2-r2-v4-0-bd28389dfd64@sentinelone.com \
    --to=devnull+mikeba.sentinelone.com@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=jp.kobryn@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=memxor@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=mikeba@sentinelone.com \
    --cc=rostedt@goodmis.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®