mbox series

[bpf-next,v3,0/3] bpf: Add kfuncs for read-only string operations

Message ID cover.1741874348.git.vmalik@redhat.com (mailing list archive)
Headers show
Series bpf: Add kfuncs for read-only string operations | expand

Message

Viktor Malik March 24, 2025, 12:03 p.m. UTC
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