Thread View: linux.kernel.netdev
8 messages
8 total messages
Started by Xu Kuohai
Fri, 20 Nov 2026 06:48
[RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp
Author: Xu Kuohai
Date: Fri, 20 Nov 2026 06:48
Date: Fri, 20 Nov 2026 06:48
69 lines
12203 bytes
12203 bytes
Sandboxes like Firecracker use seccomp filters to restrict allowed
syscalls. Hundreds to thousands of Firecracker VM instances can run
from a single template. Each instance loads identical seccomp filters.
The kernel repeatedly translates and JIT-compiles these same cBPF
programs, wasting CPU cycles and slowing down bulk startup of
identically sandboxed workloads. cBPF lacks dynamic instruction patching
features like tail calls or tracing. Its JITed instructions remain
immutable at runtime. This makes it possible to share the same cBPF
filter between different processes.
This series introduces a classic BPF filter cache. It is implemented as
a hash table that maps user-space filter instructions to the prepared
bpf prog.
The cache lookup occurs before filter preparation:
- cache hit: Returns the shared bpf prog directly.
- cache miss: Reserves an entry. Concurrent tasks loading the same filter
wait on a wait queue until preparation completes.
The cache is instantiated per subsystem. Currently, seccomp is the only
user.
This series ends with a benchmark test. Every producer spawns one worker
thread at a time, the worker installs a batch of filters and exits, the
benchmark reports the number of filters installed per second. The table
below benchmarks the batch size and the instruction length in one round,
running the command below with different option values. The base column
runs the same command on a kernel without this series. All data was
measured on a local test setup with 32 producers.
$ ./bench --producers 32 --duration 10 seccomp-install \
--sync-mode yes --filter-len 128 --batch 4 --same-filter yes
batch filter-len base cache hit cache miss
(--same-filter yes) (--same-filter no)
1 32 0.044M/s 0.064M/s(1.5x) 0.040M/s(0.9x)
1 128 0.043M/s 0.062M/s(1.4x) 0.039M/s(0.9x)
1 512 0.037M/s 0.052M/s(1.4x) 0.034M/s(0.9x)
1 2048 0.023M/s 0.037M/s(1.6x) 0.023M/s(1.0x)
4 32 0.065M/s 0.187M/s(2.9x) 0.059M/s(0.9x)
4 128 0.062M/s 0.190M/s(3.1x) 0.055M/s(0.9x)
4 512 0.052M/s 0.167M/s(3.2x) 0.046M/s(0.9x)
4 2048 0.030M/s 0.119M/s(4.0x) 0.028M/s(0.9x)
On cache hits, the cache delivers 1.4x to 4.0x throughput. Hits bypass
the translation and JIT compilation pipeline entirely, so gains grow with
filter length. The worker thread setup hides the win at one install per
worker, a larger batch amortizes it. On cache misses, a miss costs a hash
lookup and an entry allocation, the throughput degradation stays within
about 10%.
Xu Kuohai (3):
bpf: Introduce classic BPF filter cache
seccomp: Use the classic BPF filter cache
selftests/bpf: Add benchmark for seccomp filter install
include/linux/filter.h | 25 +-
io_uring/bpf_filter.c | 3 +-
kernel/seccomp.c | 12 +-
net/core/filter.c | 318 +++++++++++++++++-
net/packet/af_packet.c | 2 +-
tools/testing/selftests/bpf/bench.c | 4 +
.../bpf/benchs/bench_seccomp_install.c | 249 ++++++++++++++
7 files changed, 604 insertions(+), 9 deletions(-)
create mode 100644 tools/testing/selftests/bpf/benchs/bench_seccomp_install.c
--
2.43.0
Re: [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp
Author: "Alexei Starovoitov"
Date: Fri, 20 Nov 2026 19:37
Date: Fri, 20 Nov 2026 19:37
24 lines
12037 bytes
12037 bytes
On Thu, Nov 19, 2026 at 10:48 PM Xu Kuohai <xukuohai@huaweicloud.com> wrote: > The kernel repeatedly translates and JIT-compiles these same cBPF > programs, wasting CPU cycles and slowing down bulk startup of > identically sandboxed workloads. cBPF lacks dynamic instruction patching There is no VM startup number in the series which makes me question the whole thing. > features like tail calls or tracing. Its JITed instructions remain > immutable at runtime. This makes it possible to share the same cBPF > filter between different processes. Immutable is not enough. The image depends on the caps of the task that loads the filter. Also any task can time seccomp() and learn that somebody has this filter. seccomp_get_filter() needs CAP_SYS_ADMIN for that today. fork() shares the filter already, see copy_seccomp(). A launcher can install the common filter once and fork the VMs. I don't think we need this. pw-bot: cr
Re: [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp
Author: Kees Cook
Date: Mon, 23 Nov 2026 02:47
Date: Mon, 23 Nov 2026 02:47
55 lines
10189 bytes
10189 bytes
On November 20, 2026 4:48:49 PM GMT+10:00, Xu Kuohai <xukuohai@huaweicloud.com> wrote: >Sandboxes like Firecracker use seccomp filters to restrict allowed >syscalls. Hundreds to thousands of Firecracker VM instances can run >from a single template. Each instance loads identical seccomp filters. >The kernel repeatedly translates and JIT-compiles these same cBPF >programs, wasting CPU cycles and slowing down bulk startup of >identically sandboxed workloads. cBPF lacks dynamic instruction patching >features like tail calls or tracing. Its JITed instructions remain >immutable at runtime. This makes it possible to share the same cBPF >filter between different processes. > >This series introduces a classic BPF filter cache. It is implemented as >a hash table that maps user-space filter instructions to the prepared >bpf prog. > >The cache lookup occurs before filter preparation: >- cache hit: Returns the shared bpf prog directly. >- cache miss: Reserves an entry. Concurrent tasks loading the same filter > wait on a wait queue until preparation completes. > >The cache is instantiated per subsystem. Currently, seccomp is the only >user. > >This series ends with a benchmark test. Every producer spawns one worker >thread at a time, the worker installs a batch of filters and exits, the >benchmark reports the number of filters installed per second. The table >below benchmarks the batch size and the instruction length in one round, >running the command below with different option values. The base column >runs the same command on a kernel without this series. All data was >measured on a local test setup with 32 producers. I would expect either memory usage or launch benchmarks, not just filter install timings. Is there a workload where there is a problem? It is hinted at in the opening paragraph, but I'd want to better understand the actual constraints before jumping to a solution. E.g. it's already possible to share filters by way of process hierarchy, so perhaps the issue is lack of hierarchy? I've seen proposals in the past about having handles to pre-constructed filters, which could be applied (i.e. much of the existing caching and lifetime management remained unchanged). -Kees -- Kees Cook
Re: [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp
Author: Xu Kuohai
Date: Thu, 26 Nov 2026 15:02
Date: Thu, 26 Nov 2026 15:02
54 lines
11641 bytes
11641 bytes
On 11/21/2026 3:37 AM, Alexei Starovoitov wrote: > On Thu, Nov 19, 2026 at 10:48 PM Xu Kuohai <xukuohai@huaweicloud.com> wrote: >> The kernel repeatedly translates and JIT-compiles these same cBPF >> programs, wasting CPU cycles and slowing down bulk startup of >> identically sandboxed workloads. cBPF lacks dynamic instruction patching > > There is no VM startup number in the series > which makes me question the whole thing. > The problem comes from an agent sandbox deployment, where sandboxes are snapshotted to disk and resumed on demand for each new request. Below are the Firecracker microVM startup latencies I measured on a local server at different VM request rates. Parentheses show the improvement over the base kernel. VM req/s base p50 series p50 (gain) base p95 series p95 (gain) base p99 series p99 (gain) 400 20.2 ms 19.7 ms (+2%) 29.2 ms 27.9 ms (+5%) 34.7 ms 33.3 ms (+4%) 500 28.2 ms 27.1 ms (+4%) 96.8 ms 69.4 ms (+28%) 164.0 ms 107.9 ms (+34%) 600 56.6 ms 45.0 ms (+21%) 602 ms 154.5 ms (+74%) 831.8 ms 223.6 ms (+73%) 700 65.5 ms 41.1 ms (+37%) 1.63 s 134.1 ms (+92%) 2.43 s 194.1 ms (+92%) 800 65.4 ms 39.4 ms (+40%) 2.97 s 113.8 ms (+96%) 4.60 s 167.3 ms (+96%) 900 61.9 ms 35.7 ms (+42%) 3.04 s 95.2 ms (+97%) 5.13 s 147.3 ms (+97%) 1000 53.2 ms 32.2 ms (+39%) 3.38 s 80.0 ms (+98%) 5.78 s 125.1 ms (+98%) At 400 VM/s, the two kernels perform similarly. At higher rates, the base kernel suffers severe tail latency collapse, while this series keeps p99 below 225 ms, reducing it by up to 98% at 1000 VM/s. After further profiling, I found that this collapse is mainly caused by Firecracker instances queuing on the global pack_mutex in bpf_prog_pack_alloc(). So a better approach is to replace the global mutex to per-pack locks. I will work on that instead. >> features like tail calls or tracing. Its JITed instructions remain >> immutable at runtime. This makes it possible to share the same cBPF >> filter between different processes. > > Immutable is not enough. The image depends on the caps of the task > that loads the filter. > Also any task can time seccomp() and learn that somebody has this filter. > seccomp_get_filter() needs CAP_SYS_ADMIN for that today. > > fork() shares the filter already, see copy_seccomp(). > A launcher can install the common filter once and fork the VMs. > > I don't think we need this. > > pw-bot: cr
Re: [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp
Author: "Alexei Starovoitov"
Date: Thu, 26 Nov 2026 20:17
Date: Thu, 26 Nov 2026 20:17
30 lines
12578 bytes
12578 bytes
On Thu, Nov 26, 2026 at 07:02 AM Xu Kuohai <xukuohai@huaweicloud.com> wrote:
> After further profiling, I found that this collapse is
> mainly caused by Firecracker instances queuing on the
> global pack_mutex in bpf_prog_pack_alloc(). So a better
> approach is to replace the global mutex to per-pack locks.
> I will work on that instead.
Makes sense to fix it there. Every prog load benefits.
But I doubt that per-pack locks help.
bpf_prog_pack_alloc() walks pack_list from the head and takes the first
pack that has room. All loaders pick the same pack and queue on its lock.
I suspect the hold time comes from these:
path under pack_mutex what it does
----- -------------------------- --------------------------------
alloc bpf_arch_pred_flush() x86: IBPB via on_each_cpu()
free bpf_arch_text_invalidate() x86: text_poke_set(), text_mutex
free bpf_arch_text_invalidate() arm64: kick_all_cpus_sync()
Every free marks its pack dirty. A cBPF alloc from a dirty pack flushes.
The log of commit a9b1f19a6a67 ("bpf: Prefer packs that won't trigger
an IBPB flush on allocation") has ideas how to flush less.
bpf_arch_text_invalidate() doesn't need pack_mutex. The bits of the
chunks are still set in pack->bitmap at that point, so nobody can take
the chunks or free the pack.
What do you see in your profile?
Re: [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp
Author: Xu Kuohai
Date: Fri, 27 Nov 2026 15:15
Date: Fri, 27 Nov 2026 15:15
65 lines
11829 bytes
11829 bytes
On 11/27/2026 4:17 AM, Alexei Starovoitov wrote:
> On Thu, Nov 26, 2026 at 07:02 AM Xu Kuohai <xukuohai@huaweicloud.com> wrote:
>> After further profiling, I found that this collapse is
>> mainly caused by Firecracker instances queuing on the
>> global pack_mutex in bpf_prog_pack_alloc(). So a better
>> approach is to replace the global mutex to per-pack locks.
>> I will work on that instead.
>
> Makes sense to fix it there. Every prog load benefits.
> But I doubt that per-pack locks help.
> bpf_prog_pack_alloc() walks pack_list from the head and takes the first
> pack that has room. All loaders pick the same pack and queue on its lock.
>
> I suspect the hold time comes from these:
>
> path under pack_mutex what it does
> ----- -------------------------- --------------------------------
> alloc bpf_arch_pred_flush() x86: IBPB via on_each_cpu()
> free bpf_arch_text_invalidate() x86: text_poke_set(), text_mutex
> free bpf_arch_text_invalidate() arm64: kick_all_cpus_sync()
>
> Every free marks its pack dirty. A cBPF alloc from a dirty pack flushes.
> The log of commit a9b1f19a6a67 ("bpf: Prefer packs that won't trigger
> an IBPB flush on allocation") has ideas how to flush less.
>
> bpf_arch_text_invalidate() doesn't need pack_mutex. The bits of the
> chunks are still set in pack->bitmap at that point, so nobody can take
> the chunks or free the pack.
>
> What do you see in your profile?
Hi Alexei,
You are correct.
The benchmark server is arm64. The lock hold times in bpf_prog_pack_alloc()
and bpf_prog_pack_free() are typically 8–16 us and 256–512 us, respectively.
The free path is dominated by bpf_arch_text_invalidate(), which sends IPIs
to all CPUs and waits for completion. The wait happens after put_cpu(), so
the task is preemptible. If the task is preempted, other lock waiters remain
blocked until it is scheduled back and releases the lock. This can extend
the lock hold time to seconds.
Below is a sample of the captured preemption stack:
-> bpf_prog_pack_free
-> bpf_arch_text_invalidate
-> aarch64_insn_set
-> __text_poke
-> kick_all_cpus_sync
-> smp_call_function_many_cond
-> el1h_64_irq
-> el1h_64_irq_handler
-> el1_interrupt
-> arm64_exit_to_kernel_mode_preempt
-> irqentry_exit_cond_resched
-> preempt_schedule_irq
-> __schedule
After moving bpf_arch_text_invalidate() out of pack_mutex, the p99 VM
startup latency drops from 2.79 s to 170 ms under the same workload.
I will post the patch once it is ready.
Re: [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp
Author: "Alexei Starovoitov"
Date: Sat, 28 Nov 2026 00:33
Date: Sat, 28 Nov 2026 00:33
22 lines
12156 bytes
12156 bytes
On Fri, Nov 27, 2026 at 07:15 AM Xu Kuohai <xukuohai@huaweicloud.com> wrote: > The free path is dominated by bpf_arch_text_invalidate(), which sends IPIs > to all CPUs and waits for completion. The wait happens after put_cpu(), so > the task is preemptible. If the task is preempted, other lock waiters remain > blocked until it is scheduled back and releases the lock. This can extend > the lock hold time to seconds. > After moving bpf_arch_text_invalidate() out of pack_mutex, the p99 VM > startup latency drops from 2.79 s to 170 ms under the same workload. Awesome! btw every free still sends these IPIs. We can probably remove them. aarch64_insn_set() can do caches_clean_inval_pou() only like aarch64_insn_patch_text_nosync(). bpf_arch_text_invalidate() is its only caller and nothing runs in the freed chunks. It can be a separate patch, of course.
Re: [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp
Author: Xu Kuohai
Date: Sat, 28 Nov 2026 06:14
Date: Sat, 28 Nov 2026 06:14
25 lines
10198 bytes
10198 bytes
On 11/28/2026 8:33 AM, Alexei Starovoitov wrote: > On Fri, Nov 27, 2026 at 07:15 AM Xu Kuohai <xukuohai@huaweicloud.com> wrote: >> The free path is dominated by bpf_arch_text_invalidate(), which sends IPIs >> to all CPUs and waits for completion. The wait happens after put_cpu(), so >> the task is preemptible. If the task is preempted, other lock waiters remain >> blocked until it is scheduled back and releases the lock. This can extend >> the lock hold time to seconds. > >> After moving bpf_arch_text_invalidate() out of pack_mutex, the p99 VM >> startup latency drops from 2.79 s to 170 ms under the same workload. > > Awesome! > > btw every free still sends these IPIs. > We can probably remove them. > > aarch64_insn_set() can do caches_clean_inval_pou() only > like aarch64_insn_patch_text_nosync(). > bpf_arch_text_invalidate() is its only caller and nothing runs in > the freed chunks. > > It can be a separate patch, of course. Yeah, I'll look into it.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads