Skip to content

bpf: Introduce 64-bit bitops kfuncs

Leon Hwang bpf linux-arm-kernel linux-kselftest lkml llvm netdev oe-kbuild oe-kbuild-all 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest6 patches 14 repliesIn review

Changes in v2 from the cover letter

* Drop RFC.
* Add __cpu_feature annotation for CPU-feature-gated tests.
* Add JIT disassembly tests for 64-bit bitops kfuncs
* Address comments from Alexei:
* Drop KF_MUST_INLINE.
* Drop internal BPF_ALU64 opcode BPF_BITOPS.
* Mark all of the kfuncs as fastcall and do push/pop in JIT when necessary.
* v1: https://lore.kernel.org/bpf/[email protected]/

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/6bpf: Introduce 64-bit bitops kfuncsinclude/linux/filter.h kernel/bpf/core.c kernel/bpf/helpers.c +1 more+118 -13 replies
2/6bpf, x86: Add 64-bit bitops kfuncs support for x86_64arch/x86/net/bpf_jit_comp.c+141 -07 replies
3/6bpf, arm64: Add 64-bit bitops kfuncs supportarch/arm64/net/bpf_jit_comp.c+123 -04 replies
4/6selftests/bpf: Add tests for 64-bit bitops kfuncstools/testing/selftests/bpf/bpf_experimental.h tools/testing/selftests/bpf/prog_tests/bitops.c tools/testing/selftests/bpf/progs/bitops.c+259 -00 replies
5/6selftests/bpf: Add __cpu_feature annotation for CPU-feature-gated teststools/testing/selftests/bpf/progs/bpf_misc.h tools/testing/selftests/bpf/test_loader.c+157 -00 replies
6/6selftests/bpf: Add JIT disassembly tests for 64-bit bitops kfuncstools/testing/selftests/bpf/prog_tests/bitops.c tools/testing/selftests/bpf/progs/bitops_jit.c+159 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH bpf-next v2 0/6] bpf: Introduce 64-bit bitops kfuncs

This series adds generic 64-bit bitops kfuncs and JIT inlining support on x86_64 and arm64.

The new kfuncs are:

* bpf_clz64(): Count leading zeros.
* bpf_ctz64(): Count trailing zeros.
* bpf_ffs64(): Find first set bit, 1-based index, returns 0 when input is 0.
* bpf_fls64(): Find last set bit, 1-based index.
* bpf_bitrev64(): Reverse bits.
* bpf_popcnt64(): Population count.
* bpf_rol64(): Rotate left.
* bpf_ror64(): Rotate right.

Defined zero-input behavior:

* bpf_clz64(0) = 64
* bpf_ctz64(0) = 64
* bpf_ffs64(0) = 0
* bpf_fls64(0) = 0

bpf_ffs64() was previously discussed in
"bpf: Add generic kfunc bpf_ffs64()" [1].

The main concern in that discussion was ABI overhead: a regular kfunc call follows the BPF calling convention and can introduce extra spill/fill compared to dedicated instructions.

This series keeps the user-facing API as kfuncs while avoiding that overhead on hot paths. When the JIT/backend and CPU support it, calls are inlined into native instructions; otherwise they fall back to regular function calls.

Links:
[1] https://lore.kernel.org/bpf/[email protected]/

Changes:
v1 -> v2:
* Drop RFC.
* Add __cpu_feature annotation for CPU-feature-gated tests.
* Add JIT disassembly tests for 64-bit bitops kfuncs
* Address comments from Alexei:
* Drop KF_MUST_INLINE.
* Drop internal BPF_ALU64 opcode BPF_BITOPS.
* Mark all of the kfuncs as fastcall and do push/pop in JIT when necessary.
* v1: https://lore.kernel.org/bpf/[email protected]/

Leon Hwang (6):
bpf: Introduce 64-bit bitops kfuncs
bpf, x86: Add 64-bit bitops kfuncs support for x86_64 bpf, arm64: Add 64-bit bitops kfuncs support selftests/bpf: Add tests for 64-bit bitops kfuncs selftests/bpf: Add __cpu_feature annotation for CPU-feature-gated
tests
selftests/bpf: Add JIT disassembly tests for 64-bit bitops kfuncs

 arch/arm64/net/bpf_jit_comp.c                 | 123 ++++++++++++
 arch/x86/net/bpf_jit_comp.c                   | 141 +++++++++++++
 include/linux/filter.h                        |  10 +
 kernel/bpf/core.c                             |   6 +
 kernel/bpf/helpers.c                          |  50 +++++
 kernel/bpf/verifier.c                         |  53 ++++-
 .../testing/selftests/bpf/bpf_experimental.h  |   9 +
 .../testing/selftests/bpf/prog_tests/bitops.c | 188 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/bitops.c    |  68 +++++++
 .../testing/selftests/bpf/progs/bitops_jit.c  | 153 ++++++++++++++
 tools/testing/selftests/bpf/progs/bpf_misc.h  |   7 +
 tools/testing/selftests/bpf/test_loader.c     | 150 ++++++++++++++
 12 files changed, 957 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/bitops.c
 create mode 100644 tools/testing/selftests/bpf/progs/bitops.c
 create mode 100644 tools/testing/selftests/bpf/progs/bitops_jit.c
Signature · 1 line
2.52.0