powerpc/bpf: Support indirect calls to BPF subprograms
Subsystems (5)
PATCH 1/2 powerpc/bpf: Support indirect calls to BPF subprograms
The BPF callx instruction allows indirect calls to BPF subprograms through function pointers. Unlike direct calls, where the target subprogram is known at JIT compilation time, callx requires the target address to be obtained at runtime.
Add callx instruction support to the PowerPC BPF JIT for PPC64 ELF ABI v2.
Implement handling of BPF_CALL | BPF_X instructions by loading the target register into the count register (CTR) using mtctr and invoking the target subprogram using bctrl. Move the return value from r3 into BPF_REG_0 after the call. Setting SEEN_FUNC ensures the Link Register (LR) is preserved by the prologue and epilogue.
Add bpf_jit_supports_callx() in bpf_jit_comp.c to indicate that CALLX is supported when CONFIG_PPC64_ELF_ABI_V2 is enabled.
--- arch/powerpc/net/bpf_jit_comp.c | 5 +++++ arch/powerpc/net/bpf_jit_comp64.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+)
arch/powerpc/net/bpf_jit_comp.c +5 -0
@@ -564,6 +564,11 @@ bool bpf_jit_supports_subprog_tailcalls(void)return IS_ENABLED (CONFIG_PPC64 ); } +bool bpf_jit_supports_callx (void ) +{ +return IS_ENABLED (CONFIG_PPC64_ELF_ABI_V2 ); +} +bool bpf_jit_supports_timed_may_goto (void ) {return IS_ENABLED (CONFIG_PPC64 );
arch/powerpc/net/bpf_jit_comp64.c +21 -0
@@ -1745,6 +1745,27 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code/* else fall through to the epilogue */ break ; +/* +* Indirect call to a BPF subprogram +* Supported only on PPC64 ELF ABI v2 (gated via +* bpf_jit_supports_callx()). +* +* Note: The verifier guarantees that the target is a JITed BPF +* subprog, whose prologue does not depend on r12 (the ELFv2 +* global entry point convention): r2 is reloaded from +* paca->kernel_toc, or isn't used with PCREL. So the target +* only needs to be in CTR, and r12 need not be set up. +*/ +case BPF_JMP |BPF_CALL |BPF_X : + ctx->seen |=SEEN_FUNC ; + +EMIT (PPC_RAW_MTCTR (dst_reg)); +EMIT (PPC_RAW_BCTRL ()); + +/* Move return value from r3 to BPF_REG_0 */ +EMIT (PPC_RAW_MR (bpf_to_ppc (BPF_REG_0 ), _R3)); +break ; +/* * Call kernel helper or bpf function */
Signature · 1 line
2.52.0
PATCH 2/2 selftests/bpf: Add callx tests for PPC64
Extend the existing BPF CALLX selftests to support PPC64.
Enable the callx_func_ptr_map and callx_rodata_lskel tests for PPC64 by adding a PPC64 ELF ABI v2 condition to their architecture checks. The gate uses (defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF == 2) to ensure tests run only on ELF ABI v2 builds, where JIT CALLX support is enabled, and skip cleanly on ELF ABI v1 builds.
Also enable verifier_callx and verifier_callx_rodata for the PPC64 BPF target by adding __TARGET_ARCH_powerpc to their architecture checks. This allows the verifier tests to exercise indirect calls through function pointers, including pointers stored in read-only data, for the PPC64 BPF target.
--- tools/testing/selftests/bpf/prog_tests/callx_func_ptr_map.c | 3 ++- tools/testing/selftests/bpf/prog_tests/callx_rodata_lskel.c | 3 ++- tools/testing/selftests/bpf/progs/verifier_callx.c | 3 ++- tools/testing/selftests/bpf/progs/verifier_callx_rodata.c | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/callx_func_ptr_map.c +2 -1
@@ -9,7 +9,8 @@#include <bpf/btf.h> #if defined(__x86_64__) || defined(__aarch64__) || \ - (defined(__riscv) && __riscv_xlen ==64 ) + (defined(__riscv) && __riscv_xlen ==64 ) || \ + (defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF ==2 )#define CALLEE_INSN 6#define DATA 0x1234
tools/testing/selftests/bpf/prog_tests/callx_rodata_lskel.c +2 -1
@@ -4,7 +4,8 @@#include "callx_rodata.lskel.h" #if defined(__x86_64__) || defined(__aarch64__) || \ - (defined(__riscv) && __riscv_xlen ==64 ) + (defined(__riscv) && __riscv_xlen ==64 ) || \ + (defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF ==2 )static void run (int prog_fd,int expected,const char *name) {
tools/testing/selftests/bpf/progs/verifier_callx.c +2 -1
@@ -8,7 +8,8 @@/* gcc doesn't support indirect calls */ #if defined(__clang__) && (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ - (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 ) || \ + defined(__TARGET_ARCH_powerpc))#define CALLX_INSN (DST ,SRC ,OFF ,IMM ) \ BPF_RAW_INSN(BPF_JMP | BPF_CALL | BPF_X, DST, SRC, OFF, IMM)
tools/testing/selftests/bpf/progs/verifier_callx_rodata.c +2 -1
@@ -8,7 +8,8 @@/* gcc doesn't support indirect calls */ #if defined(__clang__) && (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ - (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 ) || \ + defined(__TARGET_ARCH_powerpc))/* * Read-only data with pointers to functions, where the compiler puts tables
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH 2/2] selftests/bpf: Add callx tests for PPC64
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests/bpf: verifier_callx tests enabled on unsupported PPC targets --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests/bpf: verifier_callx tests enabled on unsupported PPC targets
--
commit 8cd8cd78cbedee87168d3e2fcad4536e0affb300
Author: Shivani Nittor <[email protected]>
selftests/bpf: Add callx tests for PPC64
This commit enables the BPF callx selftests and verifier tests for PPC64.
Quoted 19 lines
tools/testing/selftests/bpf/progs/verifier_callx.c +2
-1@@ -8,7 +8,8 @@/* gcc doesn't support indirect calls */ #if defined(__clang__) && (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ - (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 ) || \ + defined(__TARGET_ARCH_powerpc))#define CALLX_INSN (DST ,SRC ,OFF ,IMM ) \ BPF_RAW_INSN(BPF_JMP | BPF_CALL | BPF_X, DST, SRC, OFF, IMM)tools/testing/selftests/bpf/progs/verifier_callx_rodata.c +0
-0
tools/testing/selftests/bpf/progs/verifier_callx_rodata.c line 15
Quoted 9 lines
tools/testing/selftests/bpf/progs/verifier_callx_rodata.c +2
-1@@ -8,7 +8,8 @@/* gcc doesn't support indirect calls */ #if defined(__clang__) && (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \ - (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 )) + (defined(__TARGET_ARCH_riscv) && __riscv_xlen ==64 ) || \ + defined(__TARGET_ARCH_powerpc))/* * Read-only data with pointers to functions, where the compiler puts tables
[Severity: Medium]
Will gating verifier_callx.c and verifier_callx_rodata.c on __TARGET_ARCH_powerpc alone cause test_verifier_callx() and test_verifier_callx_rodata() to fail on PPC64 ELF ABI v1 and PPC32?
__TARGET_ARCH_powerpc is defined for all PowerPC builds, and RUN_JITED() in tools/testing/selftests/bpf/prog_tests/verifier.c only checks is_jit_enabled(). However, on PowerPC bpf_jit_supports_callx() only returns true when CONFIG_PPC64_ELF_ABI_V2 is enabled:
arch/powerpc/net/bpf_jit_comp.c:bpf_jit_supports_callx() {
return IS_ENABLED(CONFIG_PPC64_ELF_ABI_V2);
}On PPC64 ELF ABI v1 or PPC32 kernels with BPF JIT enabled, the callx subtests are compiled in instead of the fallback dummy program and fail verification in require_callx_jit() with -EOPNOTSUPP instead of skipping.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2