* [RFC PATCH 1/3] bpf: Introduce classic BPF filter cache
2026-11-20 6:48 [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp Xu Kuohai
@ 2026-11-20 6:48 ` Xu Kuohai
2026-11-20 6:48 ` [RFC PATCH 2/3] seccomp: Use the " Xu Kuohai
2026-11-20 6:48 ` [RFC PATCH 3/3] selftests/bpf: Add benchmark for seccomp filter install Xu Kuohai
2 siblings, 0 replies; 4+ messages in thread
From: Xu Kuohai @ 2026-11-20 6:48 UTC (permalink / raw)
To: bpf, linux-kernel, io-uring, netdev
Cc: Kees Cook, Andy Lutomirski, Will Drewry, Alexei Starovoitov,
Daniel Borkmann, Stanislav Fomichev, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Jens Axboe
From: Xu Kuohai <xukuohai@huawei.com>
Introduce a cache for prepared classic BPF filters. It is implemented as
a hash table that maps user-space filter instructions to the prepared
bpf prog. The cache key consists of the raw instructions (stored as
orig_prog) and a snapshot of the constant blinding and JIT policy read
at load time. This ensures that sysctl changes produce different keys
even for identical instructions.
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.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
include/linux/filter.h | 25 +++-
io_uring/bpf_filter.c | 3 +-
kernel/seccomp.c | 3 +-
net/core/filter.c | 318 ++++++++++++++++++++++++++++++++++++++++-
net/packet/af_packet.c | 2 +-
5 files changed, 343 insertions(+), 8 deletions(-)
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 61448be53f8d..a3951e9bde0e 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -794,9 +794,30 @@ struct compat_sock_fprog {
compat_uptr_t filter; /* struct sock_filter * */
};
+#define CBPF_CACHE_BITS 8
+#define CBPF_CACHE_SIZE BIT(CBPF_CACHE_BITS)
+
+/* cache of classic bpf filters. Identical filters share same prog */
+struct cbpf_cache {
+ struct hlist_head hash[CBPF_CACHE_SIZE];
+ spinlock_t lock[CBPF_CACHE_SIZE]; /* per-bucket */
+};
+
+/* define a classic BPF filter cache */
+#define DEFINE_CBPF_CACHE(name) \
+ struct cbpf_cache name = { \
+ .lock = { \
+ [0 ... (CBPF_CACHE_SIZE - 1)] = \
+ __SPIN_LOCK_UNLOCKED(name.lock), \
+ }, \
+ }
+
+struct cbpf_cache_entry;
+
struct sock_fprog_kern {
u16 len;
struct sock_filter *filter;
+ struct cbpf_cache_entry *cache_entry;
};
/* Some arches need doubleword alignment for their instructions and/or data */
@@ -1360,7 +1381,9 @@ typedef int (*bpf_aux_classic_check_t)(struct sock_filter *filter,
int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog);
int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
- bpf_aux_classic_check_t trans, bool save_orig);
+ bpf_aux_classic_check_t trans, bool save_orig,
+ struct cbpf_cache *cache);
+void cbpf_cache_put_prog(struct bpf_prog *prog);
void bpf_prog_destroy(struct bpf_prog *fp);
int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk);
diff --git a/io_uring/bpf_filter.c b/io_uring/bpf_filter.c
index 4a21511c4811..6237bb2a2795 100644
--- a/io_uring/bpf_filter.c
+++ b/io_uring/bpf_filter.c
@@ -375,7 +375,8 @@ int io_register_bpf_filter(struct io_restriction *res,
fprog.filter = u64_to_user_ptr(reg.filter.filter_ptr);
ret = bpf_prog_create_from_user(&prog, &fprog,
- io_uring_check_cbpf_filter, false);
+ io_uring_check_cbpf_filter, false,
+ NULL);
if (ret)
return ret;
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 3eecba5ccdef..01e26bfc6ea1 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -701,7 +701,8 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
mutex_init(&sfilter->notify_lock);
ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
- seccomp_check_filter, save_orig);
+ seccomp_check_filter, save_orig,
+ NULL);
if (ret < 0) {
kfree(sfilter);
return ERR_PTR(ret);
diff --git a/net/core/filter.c b/net/core/filter.c
index 946b07a17fce..51c6c6c36359 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -49,6 +49,9 @@
#include <linux/if_vlan.h>
#include <linux/bpf.h>
#include <linux/btf.h>
+#include <linux/jhash.h>
+#include <linux/wait_bit.h>
+#include <linux/rculist.h>
#include <net/sch_generic.h>
#include <net/cls_cgroup.h>
#include <net/dst_metadata.h>
@@ -1166,11 +1169,14 @@ static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
fkprog = fp->orig_prog;
fkprog->len = fprog->len;
+ /* release path checks cache_entry to decide if the prog is cached */
+ fkprog->cache_entry = NULL;
fkprog->filter = kmemdup(fp->insns, fsize,
GFP_KERNEL | __GFP_NOWARN);
if (!fkprog->filter) {
kfree(fp->orig_prog);
+ fp->orig_prog = NULL;
return -ENOMEM;
}
@@ -1187,6 +1193,282 @@ static void bpf_release_orig_filter(struct bpf_prog *fp)
}
}
+struct cbpf_cache_entry {
+ struct hlist_node node;
+ struct rcu_head rcu;
+ refcount_t users;
+ struct cbpf_cache *cache; /* cache this entry belongs to */
+ struct sock_fprog_kern *orig; /* cache key: the original bpf filter */
+ u32 tag; /* cache key: JIT and blinding states */
+ u32 bucket; /* bucket index of this entry */
+ /*
+ * cache value: the shared prog. prog is set to NULL before the filter
+ * preparation, and set to the result prog on success, or ERR_PTR when
+ * preparation failed.
+ */
+ struct bpf_prog *prog;
+};
+
+/*
+ * calculate tag from the allocation-time JIT and blinding states, so sysctl
+ * changes produce a different tag.
+ */
+static u32 cbpf_cache_tag(const struct bpf_prog *prog)
+{
+ u32 tag = 0;
+
+ tag |= (prog->jit_requested << 1);
+ tag |= (prog->blinding_requested << 2);
+#ifdef CONFIG_BPF_JIT
+ tag |= (bpf_jit_enable << 3);
+#endif
+
+ return tag;
+}
+
+static u32 cbpf_cache_bucket(const struct bpf_prog *prog)
+{
+ const struct sock_fprog_kern *orig = prog->orig_prog;
+
+ return jhash(orig->filter, orig->len * sizeof(struct sock_filter),
+ cbpf_cache_tag(prog)) % CBPF_CACHE_SIZE;
+}
+
+static bool
+cbpf_cache_match(const struct cbpf_cache_entry *entry,
+ const struct bpf_prog *prog)
+{
+ const struct sock_fprog_kern *orig = prog->orig_prog;
+ size_t size;
+
+ if (entry->tag != cbpf_cache_tag(prog))
+ return false;
+
+ if (entry->orig->len != orig->len)
+ return false;
+
+ size = orig->len * sizeof(struct sock_filter);
+ return !memcmp(entry->orig->filter, orig->filter, size);
+}
+
+static void cbpf_cache_free_entry_rcu(struct rcu_head *rcu)
+{
+ struct cbpf_cache_entry *entry =
+ container_of(rcu, struct cbpf_cache_entry, rcu);
+
+ if (IS_ERR(entry->prog)) {
+ /* the failed prog was freed during the preparation */
+ kfree(entry->orig->filter);
+ kfree(entry->orig);
+ } else {
+ bpf_release_orig_filter(entry->prog);
+ bpf_prog_free(entry->prog);
+ }
+ kfree(entry);
+}
+
+static void cbpf_cache_put_entry(struct cbpf_cache_entry *entry)
+{
+ if (!refcount_dec_and_test(&entry->users))
+ return;
+
+ /* an aborted entry was already unlinked by the abort. */
+ if (!IS_ERR(entry->prog)) {
+ spin_lock_bh(&entry->cache->lock[entry->bucket]);
+ hlist_del_rcu(&entry->node);
+ entry->orig->cache_entry = NULL;
+ spin_unlock_bh(&entry->cache->lock[entry->bucket]);
+ }
+
+ call_rcu(&entry->rcu, cbpf_cache_free_entry_rcu);
+}
+
+/* Release a shared prog, the last user frees it after a grace period. */
+void cbpf_cache_put_prog(struct bpf_prog *prog)
+{
+ cbpf_cache_put_entry(prog->orig_prog->cache_entry);
+}
+
+static struct cbpf_cache_entry *
+cbpf_cache_find(struct cbpf_cache *cache, const struct bpf_prog *fp)
+{
+ struct cbpf_cache_entry *cur, *hit = NULL;
+ u32 bucket = cbpf_cache_bucket(fp);
+
+ rcu_read_lock();
+ hlist_for_each_entry_rcu(cur, &cache->hash[bucket], node) {
+ if (!cbpf_cache_match(cur, fp))
+ continue;
+ /* skip dead entries (users == 0). */
+ if (refcount_inc_not_zero(&cur->users)) {
+ hit = cur;
+ break;
+ }
+ }
+ rcu_read_unlock();
+
+ return hit;
+}
+
+/*
+ * Wait for the preparation to finish. Returns 0 when the entry
+ * holds a prepared prog, -EAGAIN when the preparation failed.
+ */
+static int cbpf_cache_wait(struct cbpf_cache_entry *entry)
+{
+ struct bpf_prog *prog;
+
+ wait_var_event(&entry->prog, READ_ONCE(entry->prog));
+
+ /* acquire pairs with the complete() release. */
+ prog = smp_load_acquire(&entry->prog);
+ if (!IS_ERR(prog))
+ return 0;
+
+ /* preparation aborted, drop our ref and signal retry. */
+ cbpf_cache_put_entry(entry);
+
+ return -EAGAIN;
+}
+
+/*
+ * Insert a preparing entry for prog. Returns 0 and the entry in *pentry
+ * when the caller is the preparer, 1 and the winner in *pentry when
+ * another entry was found in the race, or -ENOMEM out of memory.
+ */
+static int cbpf_cache_insert(struct cbpf_cache *cache, struct bpf_prog *prog,
+ struct cbpf_cache_entry **pentry)
+{
+ struct cbpf_cache_entry *entry, *hit, *cur;
+ u32 bucket = cbpf_cache_bucket(prog);
+
+ entry = kzalloc_obj(*entry, GFP_KERNEL | __GFP_NOWARN);
+ if (!entry)
+ return -ENOMEM;
+
+ entry->orig = prog->orig_prog;
+ entry->cache = cache;
+ entry->tag = cbpf_cache_tag(prog);
+ entry->bucket = bucket;
+ refcount_set(&entry->users, 1);
+
+ hit = NULL;
+ spin_lock_bh(&cache->lock[bucket]);
+ hlist_for_each_entry_rcu(cur, &cache->hash[bucket], node) {
+ if (!cbpf_cache_match(cur, prog))
+ continue;
+ if (refcount_inc_not_zero(&cur->users)) {
+ hit = cur;
+ break;
+ }
+ }
+ if (!hit)
+ hlist_add_head_rcu(&entry->node, &cache->hash[bucket]);
+ spin_unlock_bh(&cache->lock[bucket]);
+
+ if (hit) {
+ kfree(entry);
+ *pentry = hit;
+ return 1;
+ }
+
+ /*
+ * The entry owns the orig_prog now. The preparation error paths
+ * release the prog, a stale prog->orig_prog would make them free
+ * the orig_prog too, though it is owned by the cache entry.
+ */
+ prog->orig_prog = NULL;
+ *pentry = entry;
+
+ return 0;
+}
+
+/*
+ * Find the shared prog in the cache, or reserve a cache entry to prepare it.
+ * Returns 1 when a prepared entry was found: *pentry holds it with one
+ * reference. Returns 0 when the caller becomes the preparer, or with *pentry
+ * NULL when out of memory.
+ */
+static int
+cbpf_cache_find_or_reserve(struct cbpf_cache *cache, struct bpf_prog *fp,
+ struct cbpf_cache_entry **pentry)
+{
+ int ret;
+ struct cbpf_cache_entry *entry;
+
+ *pentry = NULL;
+ entry = cbpf_cache_find(cache, fp);
+ for (;;) {
+ /*
+ * Wait for the preparation of the entry, on a
+ * preparation failure find a new one, a concurrent
+ * preparer may have appeared by then.
+ */
+ while (entry) {
+ ret = cbpf_cache_wait(entry);
+ if (ret != -EAGAIN)
+ break;
+ entry = cbpf_cache_find(cache, fp);
+ }
+
+ if (entry) {
+ /* cache hit */
+ *pentry = entry;
+ return 1;
+ }
+
+ ret = cbpf_cache_insert(cache, fp, &entry);
+ if (ret == 0) {
+ /* we become the preparer now */
+ *pentry = entry;
+ return 0;
+ } else if (ret < 0) {
+ /* no memory, run the preparation uncached */
+ return 0;
+ }
+ /* lost the race, entry is the winner, wait for it */
+ }
+}
+
+/* publish the prepared result, and wake up waiters */
+static void cbpf_cache_complete(struct cbpf_cache_entry *entry,
+ struct bpf_prog *fp)
+{
+ fp->orig_prog = entry->orig;
+ fp->orig_prog->cache_entry = entry;
+
+ /* pairs with the acquire load in cbpf_cache_wait() */
+ smp_store_release(&entry->prog, fp);
+ /*
+ * pairs with the barrier in wait_var_event(), both sides write one
+ * variable then read the other.
+ *
+ * waker waiter
+ * write entry->prog | write wq_head->head
+ * this smp_mb() barrier | smp_store_mb barrier in set_current_state
+ * read wq_head->head | read entry->prog
+ */
+ smp_mb();
+ wake_up_var(&entry->prog);
+}
+
+/* preparation failed, remove the entry and wake up waiters */
+static void cbpf_cache_abort(struct cbpf_cache_entry *entry)
+{
+ spin_lock_bh(&entry->cache->lock[entry->bucket]);
+ hlist_del_rcu(&entry->node);
+ spin_unlock_bh(&entry->cache->lock[entry->bucket]);
+
+ /* pairs with the acquire load in cbpf_cache_wait() */
+ smp_store_release(&entry->prog, ERR_PTR(-EAGAIN));
+
+ /* see the comment above smp_mb() in cbpf_cache_complete() */
+ smp_mb();
+ wake_up_var(&entry->prog);
+
+ cbpf_cache_put_entry(entry);
+}
+
static void __bpf_prog_release(struct bpf_prog *prog)
{
if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
@@ -1422,15 +1704,21 @@ EXPORT_SYMBOL_GPL(bpf_prog_create);
* @fprog: the filter program
* @trans: post-classic verifier transformation handler
* @save_orig: save classic BPF program
+ * @cache: cache prepared progs in this cache, NULL disables caching.
+ * The cache needs the saved program as its key, it is
+ * saved even without @save_orig.
*
* This function effectively does the same as bpf_prog_create(), only
* that it builds up its insns buffer from user space provided buffer.
* It also allows for passing a bpf_aux_classic_check_t handler.
*/
int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
- bpf_aux_classic_check_t trans, bool save_orig)
+ bpf_aux_classic_check_t trans,
+ bool save_orig,
+ struct cbpf_cache *cache)
{
unsigned int fsize = bpf_classic_proglen(fprog);
+ struct cbpf_cache_entry *entry = NULL;
struct bpf_prog *fp;
int err;
@@ -1450,20 +1738,42 @@ int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
fp->len = fprog->len;
fp->orig_prog = NULL;
- if (save_orig) {
+ if (save_orig || cache) {
err = bpf_prog_store_orig_filter(fp, fprog);
if (err) {
+ if (save_orig) {
+ __bpf_prog_free(fp);
+ return -ENOMEM;
+ }
+ /* fall back to the uncached pipeline */
+ cache = NULL;
+ }
+ }
+
+ if (cache) {
+ err = cbpf_cache_find_or_reserve(cache, fp, &entry);
+ if (err > 0) {
+ /* cache hit */
+ bpf_release_orig_filter(fp);
__bpf_prog_free(fp);
- return -ENOMEM;
+ *pfp = entry->prog;
+ return 0;
}
+ /* we are the preparer, or uncached out of memory. */
}
/* bpf_prepare_filter() already takes care of freeing
* memory in case something goes wrong.
*/
fp = bpf_prepare_filter(fp, trans);
- if (IS_ERR(fp))
+ if (IS_ERR(fp)) {
+ if (entry)
+ cbpf_cache_abort(entry);
return PTR_ERR(fp);
+ }
+
+ if (entry)
+ cbpf_cache_complete(entry, fp);
*pfp = fp;
return 0;
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 73222563fb70..eb7cd153ae66 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -1617,7 +1617,7 @@ static int fanout_set_data_cbpf(struct packet_sock *po, sockptr_t data,
if (ret)
return ret;
- ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
+ ret = bpf_prog_create_from_user(&new, &fprog, NULL, false, NULL);
if (ret)
return ret;
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [RFC PATCH 2/3] seccomp: Use the classic BPF filter cache
2026-11-20 6:48 [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp Xu Kuohai
2026-11-20 6:48 ` [RFC PATCH 1/3] bpf: Introduce classic BPF filter cache Xu Kuohai
@ 2026-11-20 6:48 ` Xu Kuohai
2026-11-20 6:48 ` [RFC PATCH 3/3] selftests/bpf: Add benchmark for seccomp filter install Xu Kuohai
2 siblings, 0 replies; 4+ messages in thread
From: Xu Kuohai @ 2026-11-20 6:48 UTC (permalink / raw)
To: bpf, linux-kernel, io-uring, netdev
Cc: Kees Cook, Andy Lutomirski, Will Drewry, Alexei Starovoitov,
Daniel Borkmann, Stanislav Fomichev, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Jens Axboe
From: Xu Kuohai <xukuohai@huawei.com>
Define a classic BPF filter cache in seccomp. This allows identical
seccomp filters to share a single prepared program instead of each
paying the cost of the check, rewrite, migration, and JIT compilation
pipeline.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
kernel/seccomp.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 01e26bfc6ea1..b1c40ffbc6f2 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -465,6 +465,9 @@ static inline void seccomp_assign_mode(struct task_struct *task,
}
#ifdef CONFIG_SECCOMP_FILTER
+
+static DEFINE_CBPF_CACHE(seccomp_cbpf_cache);
+
/* Returns 1 if the parent is an ancestor of the child. */
static int is_ancestor(struct seccomp_filter *parent,
struct seccomp_filter *child)
@@ -526,7 +529,11 @@ static inline pid_t seccomp_can_sync_threads(void)
static inline void seccomp_filter_free(struct seccomp_filter *filter)
{
if (filter) {
- bpf_prog_destroy(filter->prog);
+ if (filter->prog->orig_prog &&
+ filter->prog->orig_prog->cache_entry)
+ cbpf_cache_put_prog(filter->prog);
+ else
+ bpf_prog_destroy(filter->prog);
kfree(filter);
}
}
@@ -702,7 +709,7 @@ static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
mutex_init(&sfilter->notify_lock);
ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
seccomp_check_filter, save_orig,
- NULL);
+ &seccomp_cbpf_cache);
if (ret < 0) {
kfree(sfilter);
return ERR_PTR(ret);
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [RFC PATCH 3/3] selftests/bpf: Add benchmark for seccomp filter install
2026-11-20 6:48 [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp Xu Kuohai
2026-11-20 6:48 ` [RFC PATCH 1/3] bpf: Introduce classic BPF filter cache Xu Kuohai
2026-11-20 6:48 ` [RFC PATCH 2/3] seccomp: Use the " Xu Kuohai
@ 2026-11-20 6:48 ` Xu Kuohai
2 siblings, 0 replies; 4+ messages in thread
From: Xu Kuohai @ 2026-11-20 6:48 UTC (permalink / raw)
To: bpf, linux-kernel, io-uring, netdev
Cc: Kees Cook, Andy Lutomirski, Will Drewry, Alexei Starovoitov,
Daniel Borkmann, Stanislav Fomichev, David S . Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, Jens Axboe
From: Xu Kuohai <xukuohai@huawei.com>
Add a benchmark to measure the cost of installing seccomp filter. Each
producer spawns one worker thread per burst. The worker installs a
batch of filters and exits. The benchmark reports the number of filters
installed per second.
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
tools/testing/selftests/bpf/bench.c | 4 +
.../bpf/benchs/bench_seccomp_install.c | 249 ++++++++++++++++++
2 files changed, 253 insertions(+)
create mode 100644 tools/testing/selftests/bpf/benchs/bench_seccomp_install.c
diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c
index fbbd55399019..b8f81868edc9 100644
--- a/tools/testing/selftests/bpf/bench.c
+++ b/tools/testing/selftests/bpf/bench.c
@@ -289,6 +289,7 @@ extern struct argp bench_sockmap_argp;
extern struct argp bench_lpm_trie_map_argp;
extern struct argp bench_xdp_lb_argp;
extern struct argp bench_libarena_argp;
+extern struct argp bench_seccomp_install_argp;
static const struct argp_child bench_parsers[] = {
{ &bench_ringbufs_argp, 0, "Ring buffers benchmark", 0 },
@@ -308,6 +309,7 @@ static const struct argp_child bench_parsers[] = {
{ &bench_lpm_trie_map_argp, 0, "LPM trie map benchmark", 0 },
{ &bench_xdp_lb_argp, 0, "XDP load-balancer benchmark", 0 },
{ &bench_libarena_argp, 0, "libarena allocator benchmark", 0 },
+ { &bench_seccomp_install_argp, 0, "seccomp install benchmark", 0 },
{},
};
@@ -578,6 +580,7 @@ extern const struct bench bench_rhtab_mem;
extern const struct bench bench_crypto_encrypt;
extern const struct bench bench_crypto_decrypt;
extern const struct bench bench_sockmap;
+extern const struct bench bench_seccomp_install;
extern const struct bench bench_lpm_trie_noop;
extern const struct bench bench_lpm_trie_baseline;
extern const struct bench bench_lpm_trie_lookup;
@@ -666,6 +669,7 @@ static const struct bench *benchs[] = {
&bench_crypto_encrypt,
&bench_crypto_decrypt,
&bench_sockmap,
+ &bench_seccomp_install,
&bench_lpm_trie_noop,
&bench_lpm_trie_baseline,
&bench_lpm_trie_lookup,
diff --git a/tools/testing/selftests/bpf/benchs/bench_seccomp_install.c b/tools/testing/selftests/bpf/benchs/bench_seccomp_install.c
new file mode 100644
index 000000000000..5044d786bdf4
--- /dev/null
+++ b/tools/testing/selftests/bpf/benchs/bench_seccomp_install.c
@@ -0,0 +1,249 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2025. Huawei Technologies Co., Ltd */
+
+/*
+ * seccomp-install benchmark: measures the cost of installing seccomp filters
+ *
+ * Every producer spawns one worker thread per burst. The worker installs a batch of filters,
+ * then exits. The throughput is collected from the completed worker batches.
+ *
+ * Options:
+ *
+ * --sync-mode yes Every worker waits until all workers of the burst are created, then all
+ * of them start installing at the same time. This models many tasks
+ * installing filters at once.
+ *
+ * --sync-mode no Each worker starts installing right away when it is created. This models
+ * sequential installs.
+ *
+ * --filter-len N Number of instructions per filter, valid range is 4..4096, default is 128
+ *
+ * --same-filter Whether to install the same filter every time, yes or no, default yes. No
+ * installs a unique filter every time.
+ *
+ * --batch N Install N filters per batch, default 8
+ */
+#include <linux/filter.h>
+#include <linux/seccomp.h>
+#include <argp.h>
+#include <errno.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/prctl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+#include "bench.h"
+
+/* per-task budget employed by kernel, a bigger batch fails the install */
+#define SECCOMP_MAX_INSNS_PER_PATH 32768
+
+static struct {
+ struct counter hits;
+ struct counter drops;
+ pthread_barrier_t barrier;
+ bool sync;
+ bool distinct;
+ unsigned int filter_len;
+ unsigned int batch;
+ /* unique filter id, its own cacheline, all workers bump it */
+ unsigned int uniq __attribute__((aligned(128)));
+} ctx;
+
+static struct {
+ unsigned int filter_len;
+ unsigned int batch;
+ unsigned int mode;
+ unsigned int same_filter;
+} args = {
+ .filter_len = 128,
+ .batch = 8,
+ .mode = 1, /* sync */
+ .same_filter = 1,
+};
+
+enum {
+ ARG_SYNC_MODE = 12500,
+ ARG_FILTER_LEN,
+ ARG_BATCH,
+ ARG_SAME_FILTER,
+};
+
+static const struct argp_option opts[] = {
+ { "sync-mode", ARG_SYNC_MODE, "SYNC_MODE", 0,
+ "Start every burst of workers at the same time: yes|no (default yes)" },
+ { "filter-len", ARG_FILTER_LEN, "FILTER_LEN", 0,
+ "Number of instructions per filter, within 4..4096 (default 128)" },
+ { "batch", ARG_BATCH, "BATCH", 0, "Filters installed per batch (default 8)" },
+ { "same-filter", ARG_SAME_FILTER, "SAME_FILTER", 0,
+ "Install the same filter every time or a unique one: yes|no (default yes)" },
+ {},
+};
+
+static error_t parse_arg(int key, char *arg, struct argp_state *state)
+{
+ switch (key) {
+ case ARG_SYNC_MODE:
+ if (strcmp(arg, "yes") && strcmp(arg, "no")) {
+ fprintf(stderr, "sync-mode must be yes or no\n");
+ argp_usage(state);
+ }
+ args.mode = !strcmp(arg, "yes");
+ break;
+ case ARG_FILTER_LEN:
+ args.filter_len = strtoul(arg, NULL, 10);
+ if (args.filter_len < 4 || args.filter_len > BPF_MAXINSNS) {
+ fprintf(stderr, "filter-len must be within 4..%d\n", BPF_MAXINSNS);
+ argp_usage(state);
+ }
+ break;
+ case ARG_BATCH:
+ args.batch = strtoul(arg, NULL, 10);
+ if (!args.batch) {
+ fprintf(stderr, "batch must be positive\n");
+ argp_usage(state);
+ }
+ break;
+ case ARG_SAME_FILTER:
+ if (strcmp(arg, "yes") && strcmp(arg, "no")) {
+ fprintf(stderr, "same-filter must be yes or no\n");
+ argp_usage(state);
+ }
+ args.same_filter = !strcmp(arg, "yes");
+ break;
+ default:
+ return ARGP_ERR_UNKNOWN;
+ }
+
+ return 0;
+};
+
+const struct argp bench_seccomp_install_argp = {
+ .options = opts,
+ .parser = parse_arg,
+};
+
+/*
+ * Fill a filter of len instructions:
+ *
+ * A += 0
+ * A += 1
+ * ...
+ * A += len - 3
+ * A += uniq
+ * RET ALLOW
+ *
+ * The plain ALU body exercises the per instruction translation cost of the pipeline without any
+ * filter logic. The k field of the second to last instruction carries the uniq value, a worker
+ * installing unique filters patches it in place for every install, the rest of the image never
+ * changes.
+ */
+static void fill_filter(struct sock_filter *insns, unsigned int len)
+{
+ int i;
+
+ for (i = 0; i < len - 2; i++)
+ insns[i] = (struct sock_filter)
+ BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, i);
+ insns[i++] = (struct sock_filter)
+ BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0);
+ insns[i++] = (struct sock_filter)
+ BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW);
+}
+
+static void *install_batch(void *arg)
+{
+ struct sock_filter *insns = arg;
+ struct sock_fprog prog = { .len = ctx.filter_len, .filter = insns };
+ sigset_t mask;
+ int i, err = 0;
+
+ sigemptyset(&mask);
+ sigaddset(&mask, SIGALRM);
+ pthread_sigmask(SIG_BLOCK, &mask, NULL);
+
+ if (ctx.sync)
+ pthread_barrier_wait(&ctx.barrier);
+
+ for (i = 0; i < ctx.batch; i++) {
+ if (ctx.distinct)
+ insns[ctx.filter_len - 2].k = __atomic_fetch_add(&ctx.uniq, 1, __ATOMIC_RELAXED);
+ if (syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, &prog)) {
+ err = 1;
+ break;
+ }
+ }
+
+ return err ? (void *)1 : NULL;
+}
+
+static void *producer(void *input)
+{
+ struct sock_filter *insns;
+ pthread_t thread;
+ void *ret;
+
+ insns = malloc(ctx.filter_len * sizeof(*insns));
+ if (!insns) {
+ atomic_inc(&ctx.drops.value);
+ return NULL;
+ }
+ fill_filter(insns, ctx.filter_len);
+
+ while (true) {
+ if (pthread_create(&thread, NULL, install_batch, insns)) {
+ atomic_inc(&ctx.drops.value);
+ continue;
+ }
+ pthread_join(thread, &ret);
+ if (ret)
+ atomic_inc(&ctx.drops.value);
+ else
+ atomic_add(&ctx.hits.value, ctx.batch);
+ }
+
+ return NULL;
+}
+
+static void measure(struct bench_res *res)
+{
+ res->hits = atomic_swap(&ctx.hits.value, 0);
+ res->drops = atomic_swap(&ctx.drops.value, 0);
+}
+
+/*
+ * Installing a seccomp filter needs no_new_privs or CAP_SYS_ADMIN, set it here so the whole bench
+ * runs unprivileged.
+ */
+static void setup(void)
+{
+ if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
+ fprintf(stderr, "PR_SET_NO_NEW_PRIVS failed: %d\n", -errno);
+ exit(1);
+ }
+
+ if ((args.filter_len + 4) * args.batch > SECCOMP_MAX_INSNS_PER_PATH) {
+ fprintf(stderr, "the batch exceeds the seccomp insns budget of one task\n");
+ exit(1);
+ }
+
+ ctx.sync = args.mode;
+ ctx.distinct = !args.same_filter;
+ ctx.filter_len = args.filter_len;
+ ctx.batch = args.batch;
+
+ if (ctx.sync)
+ pthread_barrier_init(&ctx.barrier, NULL, env.producer_cnt);
+}
+
+const struct bench bench_seccomp_install = {
+ .name = "seccomp-install",
+ .argp = &bench_seccomp_install_argp,
+ .setup = setup,
+ .producer_thread = producer,
+ .measure = measure,
+ .report_progress = hits_drops_report_progress,
+ .report_final = hits_drops_report_final,
+};
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread