From: Abhishek Dubey <[email protected]>

Ensure the dummy trampoline address field present between the OOL stub
and the long branch stub is 8-byte aligned, for memory compatibility
when content loaded to a register.

Reported-by: Hari Bathini <[email protected]>
Fixes: d243b62b7bd3 ("powerpc64/bpf: Add support for bpf trampolines")
Cc: [email protected]
Signed-off-by: Abhishek Dubey <[email protected]>
---
 arch/powerpc/net/bpf_jit.h        |  4 ++--
 arch/powerpc/net/bpf_jit_comp.c   | 34 ++++++++++++++++++++++++++-----
 arch/powerpc/net/bpf_jit_comp64.c |  4 ++--
 3 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/arch/powerpc/net/bpf_jit.h b/arch/powerpc/net/bpf_jit.h
index 7354e1d72f79..1184ad15d5a4 100644
--- a/arch/powerpc/net/bpf_jit.h
+++ b/arch/powerpc/net/bpf_jit.h
@@ -208,8 +208,8 @@ int bpf_jit_emit_func_call_rel(u32 *image, u32 *fimage, 
struct codegen_context *
 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);
-void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx);
-void bpf_jit_build_fentry_stubs(u32 *image, struct codegen_context *ctx);
+void bpf_jit_build_epilogue(u32 *image, u32 *fimage, struct codegen_context 
*ctx);
+void bpf_jit_build_fentry_stubs(u32 *image, u32 *fimage, struct 
codegen_context *ctx);
 void bpf_jit_realloc_regs(struct codegen_context *ctx);
 int bpf_jit_emit_exit_insn(u32 *image, struct codegen_context *ctx, int 
tmp_reg, long exit_addr);
 
diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
index a62a9a92b7b5..c255b30a37b0 100644
--- a/arch/powerpc/net/bpf_jit_comp.c
+++ b/arch/powerpc/net/bpf_jit_comp.c
@@ -49,11 +49,34 @@ asm (
 "      .popsection                             ;"
 );
 
-void bpf_jit_build_fentry_stubs(u32 *image, struct codegen_context *ctx)
+void bpf_jit_build_fentry_stubs(u32 *image, u32 *fimage, struct 
codegen_context *ctx)
 {
        int ool_stub_idx, long_branch_stub_idx;
 
        /*
+        * In the final pass, align the mis-aligned dummy_tramp_addr field
+        * in the fimage. The alignment NOP must appear before OOL stub,
+        * to make ool_stub_idx & long_branch_stub_idx constant from end.
+        *
+        * Need alignment NOP in following conditions:
+        *
+        * OOL stub aligned     CONFIG_PPC_FTRACE_OUT_OF_LINE   Alignment NOP
+        *      Y                               Y                     N
+        *      Y                               N                     Y
+        *      N                               Y                     Y
+        *      N                               N                     N
+        */
+#ifdef CONFIG_PPC64
+       if (fimage && image) {
+               unsigned long pc = (unsigned long)fimage + CTX_NIA(ctx);
+
+               if (IS_ALIGNED(pc, 8) ^
+                       IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE))
+                       EMIT(PPC_RAW_NOP());
+       }
+#endif
+
+       /*      nop     // optional, for alignment of dummy_tramp_addr
         * Out-of-line stub:
         *      mflr    r0
         *      [b|bl]  tramp
@@ -70,7 +93,7 @@ void bpf_jit_build_fentry_stubs(u32 *image, struct 
codegen_context *ctx)
 
        /*
         * Long branch stub:
-        *      .long   <dummy_tramp_addr>
+        *      .long   <dummy_tramp_addr>  // 8-byte aligned
         *      mflr    r11
         *      bcl     20,31,$+4
         *      mflr    r12
@@ -81,6 +104,7 @@ void bpf_jit_build_fentry_stubs(u32 *image, struct 
codegen_context *ctx)
         */
        if (image)
                *((unsigned long *)&image[ctx->idx]) = (unsigned 
long)dummy_tramp;
+
        ctx->idx += SZL / 4;
        long_branch_stub_idx = ctx->idx;
        EMIT(PPC_RAW_MFLR(_R11));
@@ -107,7 +131,7 @@ int bpf_jit_emit_exit_insn(u32 *image, struct 
codegen_context *ctx, int tmp_reg,
                PPC_JMP(ctx->alt_exit_addr);
        } else {
                ctx->alt_exit_addr = ctx->idx * 4;
-               bpf_jit_build_epilogue(image, ctx);
+               bpf_jit_build_epilogue(image, NULL, ctx);
        }
 
        return 0;
@@ -240,7 +264,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
         */
        bpf_jit_build_prologue(NULL, &cgctx);
        addrs[fp->len] = cgctx.idx * 4;
-       bpf_jit_build_epilogue(NULL, &cgctx);
+       bpf_jit_build_epilogue(NULL, NULL, &cgctx);
 
        fixup_len = fp->aux->num_exentries * BPF_FIXUP_LEN * 4;
        extable_len = fp->aux->num_exentries * sizeof(struct 
exception_table_entry);
@@ -275,7 +299,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)
                        fp = org_fp;
                        goto out_addrs;
                }
-               bpf_jit_build_epilogue(code_base, &cgctx);
+               bpf_jit_build_epilogue(code_base, fcode_base, &cgctx);
 
                if (bpf_jit_enable > 1)
                        pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
diff --git a/arch/powerpc/net/bpf_jit_comp64.c 
b/arch/powerpc/net/bpf_jit_comp64.c
index c5e26d231cd5..d4873979ae9d 100644
--- a/arch/powerpc/net/bpf_jit_comp64.c
+++ b/arch/powerpc/net/bpf_jit_comp64.c
@@ -348,7 +348,7 @@ static void bpf_jit_emit_common_epilogue(u32 *image, struct 
codegen_context *ctx
        }
 }
 
-void bpf_jit_build_epilogue(u32 *image, struct codegen_context *ctx)
+void bpf_jit_build_epilogue(u32 *image, u32 *fimage, struct codegen_context 
*ctx)
 {
        bpf_jit_emit_common_epilogue(image, ctx);
 
@@ -357,7 +357,7 @@ void bpf_jit_build_epilogue(u32 *image, struct 
codegen_context *ctx)
 
        EMIT(PPC_RAW_BLR());
 
-       bpf_jit_build_fentry_stubs(image, ctx);
+       bpf_jit_build_fentry_stubs(image, fimage, ctx);
 }
 
 /*
-- 
2.52.0


Reply via email to