From: Leon Hwang <leon.hwang@linux.dev>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Shuah Khan <shuah@kernel.org>, Leon Hwang <leon.hwang@linux.dev>,
Rong Tao <rongtao@cestc.cn>,
Yuzuki Ishiyama <ishiyama@hpc.is.uec.ac.jp>,
Viktor Malik <vmalik@redhat.com>,
linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: [RFC PATCH bpf-next 0/6] bpf: Optimize string kfuncs
Date: Tue, 28 Jul 2026 22:57:21 +0800 [thread overview]
Message-ID: <20260728145727.45153-1-leon.hwang@linux.dev> (raw)
My friend Gray reported that bpf_strnstr() kfunc has poorer performance
than his SWAR(SIMD Within A Register)-alike pure-bpf implementation [1].
I built his microbench, and ran it on a 16-cores 16GiB QEMU VM:
taskset -c 1 ./bpf_swar_benchmark
BPF HTTP Host search benchmark
kernel=7.2.0-rc4-00662-g771a7ae30d95 arch=amd64 repeat=100000 trials=9 warmup=1000
Scenario Bytes Byte ns/op SWAR ns/op Kfunc ns/op SWAR speedup Kfunc speedup
first 64 492.0 246.0 345.0 2.00x 1.43x
middle 512 2761.0 780.0 1193.0 3.54x 2.31x
late 1536 11801.0 2829.0 4469.0 4.17x 2.64x
absent 2048 19874.0 4557.0 7389.0 4.36x 2.69x
The result verified his report.
Then, with LLM assistance, I optimized all string kfuncs with
word-at-a-time, include bpf_strnstr(). And, re-ran the microbench:
taskset -c 1 ./bpf_swar_benchmark
BPF HTTP Host search benchmark
kernel=7.2.0-rc4-00667-g707cce56283d arch=amd64 repeat=100000 trials=9 warmup=1000
Scenario Bytes Byte ns/op SWAR ns/op Kfunc ns/op SWAR speedup Kfunc speedup
first 64 487.0 245.0 261.0 1.99x 1.87x
middle 512 2827.0 781.0 352.0 3.62x 8.03x
late 1536 12127.0 2822.0 708.0 4.30x 17.13x
absent 2048 20463.0 4548.0 982.0 4.50x 20.84x
The optimized bpf_strnstr() kfunc was 10x faster than the original
bpf_strnstr() kfunc.
Next, I (LLM) implemented the benchmarks for the 4 kinds of string kfuncs:
cd tools/testing/selftests/bpf
bash ./benchs/run_bench_bpf_str_kfuncs.sh
BPF string kfunc benchmark comparison
baseline kernel=7.2.0-rc4-00661-g4748a67f7111 arch=x86_64 repeat=100000 trials=9 warmup=1000
current kernel=7.2.0-rc4-00671-g239632cc49ee arch=x86_64 repeat=100000 trials=9 warmup=1000
scan (bpf_strnchr)
Scenario Bytes Baseline ns/op Current ns/op Speedup
first 64 174.0 179.0 0.97x
middle 512 489.0 241.0 2.03x
late 1536 1706.0 506.0 3.37x
absent 2048 2781.0 727.0 3.83x
comparison (bpf_strcmp)
Scenario Bytes Baseline ns/op Current ns/op Speedup
first 64 186.0 189.0 0.98x
middle 512 614.0 238.0 2.58x
late 1536 2234.0 458.0 4.88x
equal 2048 3663.0 634.0 5.78x
span (bpf_strcspn)
Scenario Bytes Baseline ns/op Current ns/op Speedup
first 64 179.0 195.0 0.92x
middle 512 1047.0 266.0 3.94x
late 1536 4489.0 482.0 9.31x
absent 2048 7468.0 661.0 11.30x
substring (bpf_strnstr)
Scenario Bytes Baseline ns/op Current ns/op Speedup
first 64 277.0 218.0 1.27x
middle 512 1115.0 296.0 3.77x
late 1536 4427.0 613.0 7.22x
absent 2048 7261.0 854.0 8.50x
The optimized kfuncs win most of the benchmarks.
For the implementation details, pls look into the first 4 patches.
The checkpatch.pl reports
"WARNING: Macros with flow control statements should be avoided" about
those two macros. I will address it in the next revision.
Links:
[1] https://github.com/jschwinger233/bpf_swar_benchmark
Leon Hwang (6):
bpf: Optimize string scan kfuncs
bpf: Optimize string comparison kfuncs
bpf: Optimize string span kfuncs
bpf: Optimize string substring kfuncs
selftests/bpf: Exercise word-at-a-time string kfuncs
selftests/bpf: Benchmark string kfuncs
kernel/bpf/helpers.c | 557 +++++++++++++-----
tools/testing/selftests/bpf/Makefile | 2 +
tools/testing/selftests/bpf/bench.c | 11 +
tools/testing/selftests/bpf/bench.h | 2 +
.../bpf/benchs/bench_bpf_str_kfuncs.c | 228 +++++++
.../bpf/benchs/run_bench_bpf_str_kfuncs.sh | 98 +++
.../bpf/progs/bpf_str_kfuncs_bench.c | 48 ++
.../bpf/progs/string_kfuncs_failure1.c | 58 ++
.../bpf/progs/string_kfuncs_success.c | 86 +++
9 files changed, 957 insertions(+), 133 deletions(-)
create mode 100644 tools/testing/selftests/bpf/benchs/bench_bpf_str_kfuncs.c
create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_bpf_str_kfuncs.sh
create mode 100644 tools/testing/selftests/bpf/progs/bpf_str_kfuncs_bench.c
--
2.55.0
next reply other threads:[~2026-07-28 14:58 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 14:57 Leon Hwang [this message]
2026-07-28 14:57 ` [RFC PATCH bpf-next 1/6] bpf: Optimize string scan kfuncs Leon Hwang
2026-07-28 14:57 ` [RFC PATCH bpf-next 2/6] bpf: Optimize string comparison kfuncs Leon Hwang
2026-07-28 14:57 ` [RFC PATCH bpf-next 3/6] bpf: Optimize string span kfuncs Leon Hwang
2026-07-28 14:57 ` [RFC PATCH bpf-next 4/6] bpf: Optimize string substring kfuncs Leon Hwang
2026-07-28 14:57 ` [RFC PATCH bpf-next 5/6] selftests/bpf: Exercise word-at-a-time string kfuncs Leon Hwang
2026-07-28 14:57 ` [RFC PATCH bpf-next 6/6] selftests/bpf: Benchmark " Leon Hwang
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=20260728145727.45153-1-leon.hwang@linux.dev \
--to=leon.hwang@linux.dev \
--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=ishiyama@hpc.is.uec.ac.jp \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=rongtao@cestc.cn \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=vmalik@redhat.com \
--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