BPF List
 help / color / mirror / Atom feed
* [PATCH 1/2] powerpc/bpf: Support indirect calls to BPF subprograms
@ 2026-11-30  7:03 Shivani Nittor
  2026-11-30  7:03 ` [PATCH 2/2] selftests/bpf: Add callx tests for PPC64 Shivani Nittor
  0 siblings, 1 reply; 3+ messages in thread
From: Shivani Nittor @ 2026-11-30  7:03 UTC (permalink / raw)
  To: linuxppc-dev
  Cc: maddy, hbathini, chleroy, ast, daniel, andrii, eddyz87, shuah,
	bpf, linux-kselftest, linux-kernel

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.

Signed-off-by: Shivani Nittor <shivani@linux.ibm.com>
---
 arch/powerpc/net/bpf_jit_comp.c   |  5 +++++
 arch/powerpc/net/bpf_jit_comp64.c | 21 +++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
index ce2966c52ac5..42762dc24198 100644
--- a/arch/powerpc/net/bpf_jit_comp.c
+++ b/arch/powerpc/net/bpf_jit_comp.c
@@ -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);
diff --git a/arch/powerpc/net/bpf_jit_comp64.c b/arch/powerpc/net/bpf_jit_comp64.c
index c1e86fe0bd79..e5fda9cbb44b 100644
--- a/arch/powerpc/net/bpf_jit_comp64.c
+++ b/arch/powerpc/net/bpf_jit_comp64.c
@@ -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
 		 */
-- 
2.52.0


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

* [PATCH 2/2] selftests/bpf: Add callx tests for PPC64
  2026-11-30  7:03 [PATCH 1/2] powerpc/bpf: Support indirect calls to BPF subprograms Shivani Nittor
@ 2026-11-30  7:03 ` Shivani Nittor
  2026-11-30  7:15   ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Shivani Nittor @ 2026-11-30  7:03 UTC (permalink / raw)
  To: linuxppc-dev
  Cc: maddy, hbathini, chleroy, ast, daniel, andrii, eddyz87, shuah,
	bpf, linux-kselftest, linux-kernel

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.

Signed-off-by: Shivani Nittor <shivani@linux.ibm.com>
---
 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(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/callx_func_ptr_map.c b/tools/testing/selftests/bpf/prog_tests/callx_func_ptr_map.c
index cc63b84e4d94..ddb488708b94 100644
--- a/tools/testing/selftests/bpf/prog_tests/callx_func_ptr_map.c
+++ b/tools/testing/selftests/bpf/prog_tests/callx_func_ptr_map.c
@@ -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
diff --git a/tools/testing/selftests/bpf/prog_tests/callx_rodata_lskel.c b/tools/testing/selftests/bpf/prog_tests/callx_rodata_lskel.c
index f6ef67c3141e..80f029f358e1 100644
--- a/tools/testing/selftests/bpf/prog_tests/callx_rodata_lskel.c
+++ b/tools/testing/selftests/bpf/prog_tests/callx_rodata_lskel.c
@@ -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)
 {
diff --git a/tools/testing/selftests/bpf/progs/verifier_callx.c b/tools/testing/selftests/bpf/progs/verifier_callx.c
index a12bc421e75c..b99eab7b3725 100644
--- a/tools/testing/selftests/bpf/progs/verifier_callx.c
+++ b/tools/testing/selftests/bpf/progs/verifier_callx.c
@@ -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)
diff --git a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
index 6cf58ca9ac5b..56be21e7bbcd 100644
--- a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
+++ b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
@@ -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
-- 
2.52.0


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

* Re: [PATCH 2/2] selftests/bpf: Add callx tests for PPC64
  2026-11-30  7:03 ` [PATCH 2/2] selftests/bpf: Add callx tests for PPC64 Shivani Nittor
@ 2026-11-30  7:15   ` sashiko-bot
  0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-30  7:15 UTC (permalink / raw)
  To: Shivani Nittor; +Cc: bpf

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 <shivani@linux.ibm.com>

selftests/bpf: Add callx tests for PPC64

This commit enables the BPF callx selftests and verifier tests for PPC64.

> diff --git a/tools/testing/selftests/bpf/progs/verifier_callx.c b/tools/testing/selftests/bpf/progs/verifier_callx.c
> index a12bc421e75ce..b99eab7b3725f 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_callx.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_callx.c
> @@ -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)
> diff --git a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
> index 6cf58ca9ac5bb..56be21e7bbcd3 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
> @@ -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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130070307.126765-1-shivani@linux.ibm.com?part=2

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

end of thread, other threads:[~2026-11-30  7:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30  7:03 [PATCH 1/2] powerpc/bpf: Support indirect calls to BPF subprograms Shivani Nittor
2026-11-30  7:03 ` [PATCH 2/2] selftests/bpf: Add callx tests for PPC64 Shivani Nittor
2026-11-30  7:15   ` sashiko-bot

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