Commit d084dcf2 authored by Christophe Leroy's avatar Christophe Leroy Committed by Michael Ellerman
Browse files

powerpc/bpf/32: Only set a stack frame when necessary



Until now a stack frame was set at all time due to the need
to keep tail call counter in the stack.

But since commit 89d21e25 ("powerpc/bpf/32: Fix Oops on tail call
tests") the tail call counter is passed via register r4. It is therefore
not necessary anymore to have a stack frame for that.

Just like PPC64, implement bpf_has_stack_frame() and only sets the frame
when needed.

The difference with PPC64 is that PPC32 doesn't have a redzone, so
the stack is required as soon as non volatile registers are used or
when tail call count is set up.

Signed-off-by: default avatarChristophe Leroy <christophe.leroy@csgroup.eu>
[mpe: Fix commit reference in change log]
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/62d7b654a3cfe73d998697cb29bbc5ffd89bfdb1.1675245773.git.christophe.leroy@csgroup.eu
parent 6376ed8f
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -79,6 +79,20 @@ static int bpf_jit_stack_offsetof(struct codegen_context *ctx, int reg)
#define SEEN_NVREG_FULL_MASK	0x0003ffff /* Non volatile registers r14-r31 */
#define SEEN_NVREG_TEMP_MASK	0x00001e01 /* BPF_REG_5, BPF_REG_AX, TMP_REG */

static inline bool bpf_has_stack_frame(struct codegen_context *ctx)
{
	/*
	 * We only need a stack frame if:
	 * - we call other functions (kernel helpers), or
	 * - we use non volatile registers, or
	 * - we use tail call counter
	 * - the bpf program uses its stack area
	 * The latter condition is deduced from the usage of BPF_REG_FP
	 */
	return ctx->seen & (SEEN_FUNC | SEEN_TAILCALL | SEEN_NVREG_FULL_MASK) ||
	       bpf_is_seen_register(ctx, bpf_to_ppc(BPF_REG_FP));
}

void bpf_jit_realloc_regs(struct codegen_context *ctx)
{
	unsigned int nvreg_mask;
@@ -121,6 +135,7 @@ void bpf_jit_build_prologue(u32 *image, struct codegen_context *ctx)

#define BPF_TAILCALL_PROLOGUE_SIZE	4

	if (bpf_has_stack_frame(ctx))
		EMIT(PPC_RAW_STWU(_R1, _R1, -BPF_PPC_STACKFRAME(ctx)));

	if (ctx->seen & SEEN_TAILCALL)
@@ -174,6 +189,7 @@ static void bpf_jit_emit_common_epilogue(u32 *image, struct codegen_context *ctx
		EMIT(PPC_RAW_LWZ(_R0, _R1, BPF_PPC_STACKFRAME(ctx) + PPC_LR_STKOFF));

	/* Tear down our stack frame */
	if (bpf_has_stack_frame(ctx))
		EMIT(PPC_RAW_ADDI(_R1, _R1, BPF_PPC_STACKFRAME(ctx)));

	if (ctx->seen & SEEN_FUNC)