Skip to contentSkip to conversation

powerpc/bpf: ensure module addresses are supported

PATCH Superseded Hari Bathini 2 participants 3 messages Dec 20, 2023 – Dec 22, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hari Bathini

author maintainer
#0 lore raw

PATCH 1/2 powerpc/bpf: ensure module addresses are supported

Currently, bpf jit code on powerpc assumes all the bpf functions and helpers to be kernel text. This is false for kfunc case, as function addresses are mostly module addresses in that case. Ensure module addresses are supported to enable kfunc support.

This effectively reverts commit feb6307289d8 ("powerpc64/bpf: Optimize instruction sequence used for function calls") and commit 43d636f8b4fd ("powerpc64/bpf elfv1: Do not load TOC before calling functions") that assumed only kernel text for bpf functions/helpers.

Also, commit b10cb163c4b3 ("powerpc64/bpf elfv2: Setup kernel TOC in r2 on entry") that paved the way for the commits mentioned above is reverted.

---
 arch/powerpc/net/bpf_jit.h        |  2 +-
 arch/powerpc/net/bpf_jit_comp32.c |  8 +--
 arch/powerpc/net/bpf_jit_comp64.c | 90 +++++++++++++++++--------------
 3 files changed, 52 insertions(+), 48 deletions(-)
arch/powerpc/net/bpf_jit.h +1 -1
index cdea5dccaefe..48503caa5b58 100644
@@ -160,7 +160,7 @@ static inline void bpf_clear_seen_register(struct codegen_context *ctx, int i)
 }
 
 void bpf_jit_init_reg_mapping(struct codegen_context *ctx);
-int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *ctx, u64 func);
+void bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *ctx, u64 func);
 int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct codegen_context *ctx,
 		       u32 *addrs, int pass, bool extra_pass);
 void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx);
arch/powerpc/net/bpf_jit_comp32.c +2 -6
index 2f39c50ca729..1236a75c04ea 100644
@@ -201,7 +201,7 @@ void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
 }
 
 /* Relative offset needs to be calculated based on final image location */
-int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *ctx, u64 func)
+void bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *ctx, u64 func)
 {
 	s32 rel = (s32)func - (s32)(fimage + ctx->idx);
 
@@ -214,8 +214,6 @@ int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, struct codegen_context *
 		EMIT(PPC_RAW_MTCTR(_R0));
 		EMIT(PPC_RAW_BCTRL());
 	}
-
-	return 0;
 }
 
 static int bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32 out)
@@ -1054,9 +1052,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, u32 *fimage, struct code
 				EMIT(PPC_RAW_STW(bpf_to_ppc(BPF_REG_5), _R1, 12));
 			}
 
-			ret = bpf_jit_emit_func_call_rel(image, fimage, ctx, func_addr);
-			if (ret)
-				return ret;
+			bpf_jit_emit_func_call_rel(image, fimage, ctx, func_addr);
 
 			EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_0) - 1, _R3));
 			EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_0), _R4));
arch/powerpc/net/bpf_jit_comp64.c +49 -41

152 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 2/2 powerpc/bpf: enable kfunc call

With module addresses supported, override bpf_jit_supports_kfunc_call() to enable kfunc support. Module address offsets can be more than 32-bit long, so override bpf_jit_supports_far_kfunc_call() to enable 64-bit pointers.

---
 arch/powerpc/net/bpf_jit_comp.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/powerpc/net/bpf_jit_comp.c +10 -0
index 0f9a21783329..a6151a5ef9a5 100644
@@ -359,3 +359,13 @@ void bpf_jit_free(struct bpf_prog *fp)
 
 	bpf_prog_unlock_free(fp);
 }
+
+bool bpf_jit_supports_kfunc_call(void)
+{
+	return true;
+}
+
+bool bpf_jit_supports_far_kfunc_call(void)
+{
+	return true;
+}
Signature · 1 line
2.43.0
reviewer
#2 lore raw
In reply to Hari Bathini
Quoted 5 lines · Hari Bathini

Currently, bpf jit code on powerpc assumes all the bpf functions and helpers to be kernel text. This is false for kfunc case, as function addresses are mostly module addresses in that case. Ensure module addresses are supported to enable kfunc support.

Quoted 8 lines

This effectively reverts commit feb6307289d8 ("powerpc64/bpf: Optimize instruction sequence used for function calls") and commit 43d636f8b4fd ("powerpc64/bpf elfv1: Do not load TOC before calling functions") that assumed only kernel text for bpf functions/helpers.

Also, commit b10cb163c4b3 ("powerpc64/bpf elfv2: Setup kernel TOC in r2 on entry") that paved the way for the commits mentioned above is reverted.

Instead of that, can we detect kfunc and use separate set of instructions just for those?

Unless unavoidable, I would prefer to retain the existing optimal sequence using TOC for calls to bpf kernel helpers, since those are a lot more common than kfunc.

- Naveen