Main programs receive a single 32-bit pointer as the first argument, so the current prologue converts it to the 64-bit BPF_REG_1 by moving R3 to R4 and zeroing R3. However, subprograms are called like helper functions, so there the first argument already is 64-bit - but the prologue still does its conversion, so the argument ends up with its halves swapped and the upper one zeroed. The same problem applies to the return value. In particular, in the common case where the first argument is a context pointer, the callee ends up with the upper (zeroed) 32 bits, which then triggers a kernel panic as soon as it's used. Therefore, make JIT emit the appropriate 64-bit prologue/epilogue for subprograms. Fixes: 51c66ad849a7 ("powerpc/bpf: Implement extended BPF on PPC32") Signed-off-by: Lukas Stockner --- arch/powerpc/net/bpf_jit_comp32.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/arch/powerpc/net/bpf_jit_comp32.c b/arch/powerpc/net/bpf_jit_comp32.c index f5b9441cf46a..261ac0646b96 100644 --- a/arch/powerpc/net/bpf_jit_comp32.c +++ b/arch/powerpc/net/bpf_jit_comp32.c @@ -179,9 +179,11 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx) if (ctx->seen & SEEN_TAILCALL) EMIT(PPC_RAW_STW(_R4, _R1, bpf_jit_stack_offsetof(ctx, BPF_PPC_TC))); - /* First arg comes in as a 32 bits pointer. */ - EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_1), _R3)); - EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_1) - 1, 0)); + /* Programs receive the first arg as a 32 bits pointer, subprograms don't. */ + if (!ctx->is_subprog) { + EMIT(PPC_RAW_MR(bpf_to_ppc(BPF_REG_1), _R3)); + EMIT(PPC_RAW_LI(bpf_to_ppc(BPF_REG_1) - 1, 0)); + } /* * We need a stack frame, but we don't necessarily need to @@ -231,7 +233,13 @@ static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx void bpf_jit_build_epilogue(u32 *image, u32 *fimage, struct codegen_context *ctx) { - EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0))); + /* Programs return a 32-bit value, while subprograms return 64 bit. */ + if (ctx->is_subprog) { + EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0) - 1)); + EMIT(PPC_RAW_MR(_R4, bpf_to_ppc(BPF_REG_0))); + } else { + EMIT(PPC_RAW_MR(_R3, bpf_to_ppc(BPF_REG_0))); + } bpf_jit_emit_common_epilogue(image, ctx); -- 2.56.0