mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Andrii Nakryiko" <andrii.nakryiko@gmail.com>
Cc: <mikeba@sentinelone.com>, <bpf@vger.kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"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>
Subject: Re: [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs
Date: Wed, 25 Nov 2026 02:00:15 +0000	[thread overview]
Message-ID: <DN3X5OIRXSWR.2Y70DWFMPDXQF@gmail.com> (raw)
In-Reply-To: <CAEf4BzavErcSOZhGobqEo68d+2bCnJ8vFxxT_vqb+oQw1j1d7g@mail.gmail.com>

On Tue, Nov 24, 2026 at 01:47 PM Andrii Nakryiko <andrii.nakryiko@gmail.com> 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.

> 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.

> Should we give a way to opt-out of this protection explicitly while
> still using per-CPU maps?

After looking at the data, I think we shouldn't allow it.

An opt-out is for a prog that protects its per-cpu data on its own.
In the v1 thread your example was stack_trace_scratch in wprof.
It doesn't work. wprof is broken today, without this series.
An event gets args or a stack of another task. wprof 8dd41e18 on
bpf-next, corrupt events / all events:

  -U                             what's wrong  lazy      full
  -----------------------------  ------------  --------  ---------
  u:func (arg:0, arg:1:str)      args          1928/16M  9111/17M
  u:func (..., stack)            stack         683/15M   3419/15M
  raw_tp:sys_enter (arg:id) and  args          3491/48M  16673/44M
  tp:syscalls:sys_enter_openat

utrace_scratch has no protection.
stack_trace_cur handles nesting, but not preemption:

  task A                task B                task C
  idx 0, st_cnt = 1
  preempted
                        idx 1, st_cnt = 2
                        preempted
  put, st_cnt = 1
                                              idx 1    <- slot of B

With -S offcpu task C is wprof_task_switch. Then the utrace event has
a record of kind ST_OFFCPU.
uprobes have no protection. That's what an opt-out gives to raw_tp, tp
and fentry progs:

  -U, lazy                 today     preempt off  opt-out
  -----------------------  --------  -----------  --------
  raw_tp only, args        0         0            2849/45M
  raw_tp only, stack       0         0            4081/27M
  raw_tp and tp, args      3491/48M  0            3166/54M

The series doesn't touch uprobes, so wprof needs its own fix for them.
The default mode of wprof, without -U, is fine.

Now the bigger question: do we need preempt_disable() at all?
We do. Without it progs of 17 out of 69 projects in [1] corrupt their
data. 12 of them have progs that are clean today.

Three kernels, bpf-next a58375b858fa plus patches 1-3:

  today        prog->active
  preempt off  walk + preempt_disable() for marked non-sleepable progs
  walk         walk only

Objects from [1] with small test loaders, 4 vCPUs, PREEMPT_RCU, 6-20 sec,
SCHED_OTHER tasks only. The load saturates the cpus. These are not
production rates.
The numbers are corrupt events / all events.

1. Clean today, corrupt with walk

The per-cpu buffer has one entry prog, so prog->active protects it.
"today" and "preempt off" have 0 corrupt events in each row.

  prog                            default  walk, lazy  walk, full
  ------------------------------  -------  ----------  ----------
  tetragon 1.8 event_execve       yes      25/172K     243/174K
  tetragon 1.8 tp on syscalls/*   policy   88/6.0M     478/6.8M
  tetragon 1.7 generic raw_tp     policy   40/290K     225/271K
  runtime-enforcer                yes      8/1.3M      252/1.0M
  pulsar sched_process_exec       yes      24/1.8M     188/1.5M
  kubescape fork                  yes      79/2.5M     475/2.7M
  coolbpf proctrace               yes      3/1.3M      398/1.4M
  inspektor-gadget finit_module   yes      5/38K       3/38K
  ptcpdump mount                  option   36/9.3M     101/8.9M
  wprof -U raw_tp only, args      option   2849/45M    16140/44M
  wprof -U raw_tp only, stack     option   4081/27M    20194/26M

  prog                       what breaks
  -------------------------  ------------------------------------------
  tetragon event_execve      pid, uid, argv of another task.
                             execve_map keeps the wrong pid.
  tetragon tp on syscalls/*  the signal goes to a task that doesn't
                             match the policy. Sigkill is the same code.
  runtime-enforcer           fmod_ret returns -EPERM for an allowed exec
  the rest                   path, argv, args or stack of another task

bpftool prog profile breaks too. Cycles per run of a tp_btf prog, lazy:
1540 today, 1236 with preempt off, 387 with walk. With full preemption
the sum is negative.
Same pattern in the source, not run: perf stat -b, bpfsnoop -g and
--output-lbr, exec and setsockopt in the fentry mode of datadog CWS.

2. Corrupt today

Several progs share the buffer. prog->active is per prog, so it doesn't
help. Lazy preemption:

  prog                        today      walk       preempt off
  --------------------------  ---------  ---------  -----------
  tracee syscalls             2153/9.3M  3619/22M   0
  tracee exec                 180/1.1M   181/1.5M   0
  datadog CWS, fentry mode    2268/7.0M  4065/12M   0
  elastic ebpf, no cgrp path  315/13M    290/14M    0
  skywalking-rover            196/21M    345/30M    0
  inspektor-gadget traceloop  28/7.5M    238/12M    0
  tetragon 1.7 fentry         2199/19M   3100/18M   0
  falco libs 0.25.4           2700/56M   4019/82M   879/72M

So you're right, for these the guarantee was never there.
For most of them walk is as bad per event as today.
preempt off fixes a bug that they have now.
Same pattern: open, exec, rename, http of inspektor-gadget and kubescape.
In the source, not run: systing, dynolog, coolbpf ifc engine.

3. What prog->active costs

Same loads, lazy preemption:

  lost                                today      walk     preempt off
  ----------------------------------  ---------  -------  -----------
  tetragon, match without a signal    616K/4.9M  57/4.2M  0
  runtime-enforcer, denied exec runs  175/336K   2/440K   0
  falco libs 0.26.1, lost events      16.5M      2.7M     0.5M

walk is better than today here. preempt off is better than both.
The 0.5M of falco come from progs that are not marked, see 6.

4. How precise is the mark

About 2000 progs run in the affected runners. 930 are marked.
From the source: 420 corrupt their data without preempt_disable(),
230 of them are datadog in fentry mode. 350 are marked for nothing:
the hook is atomic, the map is read only, or the prog has its own guard.
The rest lose a counter update at worst, or have a bug of their own.

5. Sleepable

No prog has fentry.s, fexit.s, raw_tp.s or tp.s. Two loaders set
BPF_F_SLEEPABLE: tracexec and datadog shared-libraries. Their progs have
no per-cpu state. So nothing in [1] needs protection for sleepable progs
in these runners.
tracexec has a pool of 2 * ncpu entries. Without prog->active it
overflows and cuts argv in 0.14% (lazy) and 5% (full) of execs.
Today it loses 42% and 66% of execs in the same load.

6. What the mark misses

array[bpf_get_smp_processor_id()] without a tail call.
That's the 879 in the falco row, and the batch of closed connections in
datadog tracer-fentry up to 7.78. Both releases don't load on bpf-next
for other reasons.
bpftrace is not in [1]. createScratchBuffer() indexes a global array by
cpu id for each object above on_stack_limit, 32 bytes by default.
str() and large printf() go there:

  tracepoint:syscalls:sys_enter_openat { printf("%s", str(args.filename)) }

From the source this is clean today and breaks with or without
preempt off. Not run. Procmon and Sysmon do the same on raw_tp/sys_enter.
A mark for bpf_get_smp_processor_id() fixes them. It marks up to 350 more
progs in [1], and most of them are safe.

7. The case for no protection

lsm lost prog->active in 6.2 (271de525e1d7). uprobe is preemptible since
6.0 (8c7dcb84e3b7). About 330 progs of 21 projects in such runners use
a per-cpu scratch without a guard today.
But projects started to notice after 7.0. obi, falco, fact and tetragon
added guards in Aug-Oct 2026. falco saw "one crash every 10 minutes per
agent" on 7.x [2].

What a prog can do on its own is limited. bpf_preempt_disable() cannot
span a tail call. A depth counter needs LIFO order. A heap keyed by task,
a slot with an owner and ringbuf reserve work.

The progs that do it right, falco 0.26 and tetragon 1.8 fentry,
do tail calls. An opt-out for per-cpu maps doesn't remove their mark.
A false mark costs scheduling latency only.

Not covered: PREEMPT_RT, may_goto.

[1] https://github.com/4ast/bpf-corpus
[2] https://github.com/falcosecurity/falco/issues/3955

  reply	other threads:[~2026-11-25  2:00 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 11:29 Mike Bazov via B4 Relay
2026-11-17 11:29 ` [PATCH bpf-next v3 1/4] bpf: Avoid per-CPU private stack in preemptible tracing progs Mike Bazov via B4 Relay
2026-11-17 11:29 ` [PATCH bpf-next v3 2/4] bpf: Mark programs whose runs must not overlap on a CPU Mike Bazov via B4 Relay
2026-11-19  5:04   ` JP Kobryn (CrowdStrike)
2026-11-19 11:23     ` [EXT.MSG] " Mike Bazov
2026-11-17 11:29 ` [PATCH bpf-next v3 3/4] bpf: Check program recursion per task via bpf_ctx Mike Bazov via B4 Relay
2026-11-17 12:20   ` bot+bpf-ci
2026-11-19  5:05   ` JP Kobryn (CrowdStrike)
2026-11-19 11:35     ` [EXT.MSG] " Mike Bazov
2026-11-23 12:49     ` Mike Bazov
2026-11-24 22:04   ` Andrii Nakryiko
2026-11-17 11:29 ` [PATCH bpf-next v3 4/4] selftests/bpf: Add preemption recursion test for tracing programs Mike Bazov via B4 Relay
2026-11-19  5:09   ` JP Kobryn (CrowdStrike)
2026-11-19 12:13     ` [EXT.MSG] " Mike Bazov
2026-11-19  5:02 ` [PATCH bpf-next v3 0/4] bpf: Check recursion per task for preemptible programs JP Kobryn (CrowdStrike)
     [not found]   ` <CAPnbwjpmRCR3F4V3StUz=702AdWZzHHZjP68GepnYdYykjoC8g@mail.gmail.com>
2026-11-19 11:22     ` [EXT.MSG] " Mike Bazov
2026-11-21  3:01       ` JP Kobryn (CrowdStrike)
2026-11-24  6:21 ` Alexei Starovoitov
2026-11-24 16:10   ` Mike Bazov
2026-11-24 20:26     ` Alexei Starovoitov
2026-11-24 21:47   ` Andrii Nakryiko
2026-11-25  2:00     ` Alexei Starovoitov [this message]
2026-11-25  2:06 ` JP Kobryn (CrowdStrike)

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=DN3X5OIRXSWR.2Y70DWFMPDXQF@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@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®