public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/3] bpf: Add kfuncs for read-only string operations
@ 2025-03-24 12:03 Viktor Malik
  2025-03-24 12:03 ` [PATCH bpf-next v3 1/3] " Viktor Malik
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Viktor Malik @ 2025-03-24 12:03 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Mykola Lysenko, Shuah Khan, Viktor Malik

String operations are commonly used in programming and BPF programs are
no exception. Since it is cumbersome to reimplement them over and over,
this series introduce kfuncs which provide the most common operations.
For now, we only limit ourselves to functions which do not copy memory
since these usually introduce undefined behaviour in case the
source/destination buffers overlap which would have to be prevented by
the verifier.

The kernel already contains implementations for all of these, however,
it is not possible to use them from BPF context. The main reason is that
the verifier is not able to check that it is safe to access the entire
string if the string size is not passed to the kfunc. Therefore, the
unbounded variants of the operations are open-coded with
__get_kernel_nofault used instead of plain dereference to make them
safe. 

On the contrary, safety of the bounded variants can be checked by the
verifier so we reuse the kernel implementations which are sometimes
highly optimized in assembly.

The last patch of the series adds a benchmark for comparing performance
of the bounded and unbounded variants which shows that on architectures
with optimized bounded string functions (e.g. strnlen on arm64), the
performance benefit can be significant (140% for 4095B strings).

Changes in v3:
- Open-code unbounded variants with __get_kernel_nofault instead of
  dereference (suggested by Alexei).
- Use the __sz suffix for size parameters in bounded variants (suggested
  by Eduard and Alexei).
- Make tests more compact (suggested by Eduard).
- Add benchmark.

Viktor Malik (3):
  bpf: Add kfuncs for read-only string operations
  selftests/bpf: Add tests for string kfuncs
  selftests/bpf: Add benchmark for bounded/unbounded string kfuncs

 kernel/bpf/helpers.c                          | 299 ++++++++++++++++++
 tools/testing/selftests/bpf/Makefile          |   2 +
 tools/testing/selftests/bpf/bench.c           |  21 ++
 .../bpf/benchs/bench_string_kfuncs.c          | 259 +++++++++++++++
 .../bpf/benchs/run_bench_string_kfuncs.sh     |  34 ++
 .../selftests/bpf/prog_tests/string_kfuncs.c  |  10 +
 .../selftests/bpf/progs/string_kfuncs.c       |  58 ++++
 .../selftests/bpf/progs/string_kfuncs_bench.c |  88 ++++++
 8 files changed, 771 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/benchs/bench_string_kfuncs.c
 create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_string_kfuncs.sh
 create mode 100644 tools/testing/selftests/bpf/prog_tests/string_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/string_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/string_kfuncs_bench.c

-- 
2.48.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-04-01 20:27 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-24 12:03 [PATCH bpf-next v3 0/3] bpf: Add kfuncs for read-only string operations Viktor Malik
2025-03-24 12:03 ` [PATCH bpf-next v3 1/3] " Viktor Malik
2025-03-28 22:48   ` Andrii Nakryiko
2025-04-01 12:48     ` Viktor Malik
2025-04-01 20:20       ` Andrii Nakryiko
2025-04-01 20:27         ` Alexei Starovoitov
2025-03-24 12:03 ` [PATCH bpf-next v3 2/3] selftests/bpf: Add tests for string kfuncs Viktor Malik
2025-03-25  8:33   ` Jiri Olsa
2025-03-24 12:03 ` [PATCH bpf-next v3 3/3] selftests/bpf: Add benchmark for bounded/unbounded " Viktor Malik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox