#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;
 
 #define BPF_TAILCALL_PROLOGUE_SIZE     4
 
-       EMIT(PPC_RAW_STWU(_R1, _R1, -BPF_PPC_STACKFRAME(ctx)));
+       if (bpf_has_stack_frame(ctx))
+               EMIT(PPC_RAW_STWU(_R1, _R1, -BPF_PPC_STACKFRAME(ctx)));
 
        if (ctx->seen & SEEN_TAILCALL)
                EMIT(PPC_RAW_STW(_R4, _R1, bpf_jit_stack_offsetof(ctx, BPF_PPC_TC)));
                EMIT(PPC_RAW_LWZ(_R0, _R1, BPF_PPC_STACKFRAME(ctx) + PPC_LR_STKOFF));
 
        /* Tear down our stack frame */
-       EMIT(PPC_RAW_ADDI(_R1, _R1, BPF_PPC_STACKFRAME(ctx)));
+       if (bpf_has_stack_frame(ctx))
+               EMIT(PPC_RAW_ADDI(_R1, _R1, BPF_PPC_STACKFRAME(ctx)));
 
        if (ctx->seen & SEEN_FUNC)
                EMIT(PPC_RAW_MTLR(_R0));