powerpc/bpf: Various fixes
Subsystems (3)
cover powerpc/bpf: Various fixes
Various fixes to the eBPF JIT for powerpc, thanks to some new tests added by Johan. This series fixes all failures in test_bpf on powerpc64. There are still some failures on powerpc32 to be looked into.
- Naveen
Naveen N. Rao (8):
powerpc/lib: Add helper to check if offset is within conditional
branch range
powerpc/bpf: Validate branch ranges
powerpc/bpf: Handle large branch ranges with BPF_EXIT powerpc/bpf: Fix BPF_MOD when imm == 1
powerpc/bpf: Fix BPF_SUB when imm == 0x80000000 powerpc/bpf: Limit 'ldbrx' to processors compliant with ISA v2.06 powerpc/security: Add a helper to query stf_barrier type powerpc/bpf: Emit stf barrier instruction sequences for BPF_NOSPEC
Ravi Bangoria (1):
powerpc/bpf: Remove unused SEEN_STACK
arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/include/asm/security_features.h | 5 + arch/powerpc/kernel/security.c | 5 + arch/powerpc/lib/code-patching.c | 7 +- arch/powerpc/net/bpf_jit.h | 39 ++++--- arch/powerpc/net/bpf_jit64.h | 8 +- arch/powerpc/net/bpf_jit_comp.c | 28 ++++- arch/powerpc/net/bpf_jit_comp32.c | 10 +- arch/powerpc/net/bpf_jit_comp64.c | 113 ++++++++++++++----- 10 files changed, 167 insertions(+), 50 deletions(-)
base-commit: 044c2d99d9f43c6d6fde8bed00672517dd9a5a57
Signature · 1 line
2.33.0
PATCH 1/9 powerpc/lib: Add helper to check if offset is within conditional branch range
Add a helper to check if a given offset is within the branch range for a powerpc conditional branch instruction, and update some sites to use the new helper.
--- arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 7 ++++++- arch/powerpc/net/bpf_jit.h | 7 +------ 3 files changed, 8 insertions(+), 7 deletions(-)
arch/powerpc/include/asm/code-patching.h +1 -0
@@ -23,6 +23,7 @@#define BRANCH_ABSOLUTE 0x2bool is_offset_in_branch_range (long offset); +bool is_offset_in_cond_branch_range (long offset);int create_branch (struct ppc_inst *instr,const u32 *addr,unsigned long target,int flags); int create_cond_branch(struct ppc_inst *instr, const u32 *addr,
arch/powerpc/lib/code-patching.c +6 -1
@@ -228,6 +228,11 @@ bool is_offset_in_branch_range(long offset)return (offset >=-0x2000000 && offset <=0x1fffffc && !(offset &0x3 )); } +bool is_offset_in_cond_branch_range (long offset) +{ +return offset >=-0x8000 && offset <=0x7FFF && !(offset &0x3 ); +} +/* * Helper to check if a given instruction is a conditional branch * Derived from the conditional checks in analyse_instr()
@@ -280,7 +285,7 @@ int create_cond_branch(struct ppc_inst *instr, const u32 *addr, offset = offset - (unsigned long )addr;/* Check we can represent the target in the instruction format */ -if (offset <-0x8000 || offset >0x7FFF || offset &0x3 ) +if (!is_offset_in_cond_branch_range (offset))return 1 ;/* Mask out the flags and target, so they don't step on each other. */
arch/powerpc/net/bpf_jit.h +1 -6
@@ -78,11 +78,6 @@#define PPC_FUNC_ADDR (d,i) do { PPC_LI32(d, i); } while(0)#endif -static inline bool is_nearbranch (int offset) -{ -return (offset <32768 ) && (offset >=-32768 ); -} -/* * The fly in the ointment of code size changing from pass to pass is * avoided by padding the short branch case with a NOP. If code size differs
@@ -91,7 +86,7 @@ static inline bool is_nearbranch(int offset)* state. */ #define PPC_BCC (cond, dest) do { \ - if (is_nearbranch((dest) - (ctx->idx * 4))) { \ + if (is_offset_in_cond_branch_range((long)(dest) - (ctx->idx * 4))) { \ PPC_BCC_SHORT(cond, dest); \ EMIT(PPC_RAW_NOP()); \ } else { \
Signature · 1 line
2.33.0
PATCH 2/9 powerpc/bpf: Validate branch ranges
In reply to Naveen N. RaoAdd checks to ensure that we never emit branch instructions with truncated branch offsets.
--- arch/powerpc/net/bpf_jit.h | 26 ++++++++++++++++++++------ arch/powerpc/net/bpf_jit_comp.c | 6 +++++- arch/powerpc/net/bpf_jit_comp32.c | 8 ++++++-- arch/powerpc/net/bpf_jit_comp64.c | 8 ++++++-- 4 files changed, 37 insertions(+), 11 deletions(-)
arch/powerpc/net/bpf_jit.h +20 -6
@@ -24,16 +24,30 @@#define EMIT (instr) PLANT_INSTR(image, ctx->idx, instr)/* Long jump; (unconditional 'branch') */ -#define PPC_JMP (dest) EMIT(PPC_INST_BRANCH | \ - (((dest) - (ctx->idx * 4)) & 0x03fffffc)) +#define PPC_JMP (dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_branch_range(offset)) { \ + pr_err_ratelimited("Branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \ + return -ERANGE; \ + } \ + EMIT(PPC_INST_BRANCH | (offset & 0x03fffffc)); \ + } while (0) +/* blr; (unconditional 'branch' with link) to absolute address */ #define PPC_BL_ABS (dest) EMIT(PPC_INST_BL | \ (((dest) - (unsigned long)(image + ctx->idx)) & 0x03fffffc))/* "cond" here covers BO:BI fields. */ -#define PPC_BCC_SHORT (cond, dest) EMIT(PPC_INST_BRANCH_COND | \ - (((cond) & 0x3ff) << 16) | \ - (((dest) - (ctx->idx * 4)) & \ - 0xfffc)) +#define PPC_BCC_SHORT (cond, dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_cond_branch_range(offset)) { \ + pr_err_ratelimited("Conditional branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \ + return -ERANGE; \ + } \ + EMIT(PPC_INST_BRANCH_COND | (((cond) & 0x3ff) << 16) | (offset & 0xfffc)); \ + } while (0) +/* Sign-extended 32-bit immediate load */ #define PPC_LI32 (d, i) do { \ if ((int)(uintptr_t)(i) >= -32768 && \
arch/powerpc/net/bpf_jit_comp.c +5 -1
@@ -210,7 +210,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)/* Now build the prologue, body code & epilogue for real. */ cgctx.idx =0 ;bpf_jit_build_prologue (code_base, &cgctx); -bpf_jit_build_body (fp, code_base, &cgctx, addrs, extra_pass); +if (bpf_jit_build_body (fp, code_base, &cgctx, addrs, extra_pass)) { +bpf_jit_binary_free (bpf_hdr); + fp = org_fp; + goto out_addrs; + }bpf_jit_build_epilogue (code_base, &cgctx);if (bpf_jit_enable >1 )
arch/powerpc/net/bpf_jit_comp32.c +6 -2
@@ -200,7 +200,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 fun } } -static void bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) +static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/* * By now, the eBPF program has already setup parameters in r3-r6
@@ -261,7 +261,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BCTR ()); +/* out: */ +return 0 ; }/* Assemble the body code between the prologue & epilogue */
@@ -1090,7 +1092,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context **/ case BPF_JMP |BPF_TAIL_CALL : ctx->seen |=SEEN_TAILCALL ; -bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); + ret =bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); +if (ret <0 ) +return ret;break ;default :
arch/powerpc/net/bpf_jit_comp64.c +6 -2
@@ -206,7 +206,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 funEMIT (PPC_RAW_BCTRL ()); } -static void bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) +static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/* * By now, the eBPF program has already setup parameters in r3, r4 and r5
@@ -267,7 +267,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BCTR ()); +/* out: */ +return 0 ; }/* Assemble the body code between the prologue & epilogue */
@@ -993,7 +995,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context **/ case BPF_JMP |BPF_TAIL_CALL : ctx->seen |=SEEN_TAILCALL ; -bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); + ret =bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); +if (ret <0 ) +return ret;break ;default :
Signature · 1 line
2.33.0
PATCH 3/9 powerpc/bpf: Remove unused SEEN_STACK
In reply to Naveen N. RaoFrom: Ravi Bangoria <[email protected]>
SEEN_STACK is unused on PowerPC. Remove it. Also, have SEEN_TAILCALL use 0x40000000.
--- arch/powerpc/net/bpf_jit.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/powerpc/net/bpf_jit.h +1 -2
@@ -125,8 +125,7 @@#define COND_LE (CR0_GT | COND_CMP_FALSE)#define SEEN_FUNC 0x20000000/* might call external helpers */ -#define SEEN_STACK 0x40000000/* uses BPF stack */ -#define SEEN_TAILCALL 0x80000000/* uses tail calls */ +#define SEEN_TAILCALL 0x40000000/* uses tail calls */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */
Signature · 1 line
2.33.0
PATCH 4/9 powerpc/bpf: Handle large branch ranges with BPF_EXIT
In reply to Naveen N. RaoIn some scenarios, it is possible that the program epilogue is outside the branch range for a BPF_EXIT instruction. Instead of rejecting such programs, emit an indirect branch. We track the size of the bpf program emitted after the initial run and do a second pass since BPF_EXIT can end up emitting different number of instructions depending on the program size.
--- arch/powerpc/net/bpf_jit.h | 3 +++ arch/powerpc/net/bpf_jit_comp.c | 22 +++++++++++++++++++++- arch/powerpc/net/bpf_jit_comp32.c | 2 +- arch/powerpc/net/bpf_jit_comp64.c | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-)
arch/powerpc/net/bpf_jit.h +3 -0
@@ -126,6 +126,7 @@#define SEEN_FUNC 0x20000000/* might call external helpers */ #define SEEN_TAILCALL 0x40000000/* uses tail calls */ +#define SEEN_BIG_PROG 0x80000000/* large prog, >32MB */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */
@@ -179,6 +180,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *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_realloc_regs (struct codegen_context *ctx); +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr);#endif
arch/powerpc/net/bpf_jit_comp.c +21 -1
@@ -72,6 +72,21 @@ static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image,return 0 ; } +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr) +{ +if (!(ctx->seen &SEEN_BIG_PROG ) &&is_offset_in_branch_range (exit_addr)) { +PPC_JMP (exit_addr); + }else { + ctx->seen |=SEEN_BIG_PROG ; +PPC_FUNC_ADDR (tmp_reg, (unsigned long )image + exit_addr); +EMIT (PPC_RAW_MTCTR (tmp_reg)); +EMIT (PPC_RAW_BCTR ()); + } + +return 0 ; +} +struct powerpc64_jit_data {struct bpf_binary_header *header;u32 *addrs;
@@ -155,12 +170,17 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) goto out_addrs; } +if (!is_offset_in_branch_range ((long )cgctx.idx *4 )) + cgctx.seen |=SEEN_BIG_PROG ; +/* * If we have seen a tail call, we need a second pass. * This is because bpf_jit_emit_common_epilogue() is called * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen. +* We also need a second pass if we ended up with too large +* a program so as to fix branches. */ -if (cgctx.seen &SEEN_TAILCALL ) { +if (cgctx.seen & (SEEN_TAILCALL |SEEN_BIG_PROG )) { cgctx.idx =0 ;if (bpf_jit_build_body (fp,0 , &cgctx, addrs, false)) { fp = org_fp;
arch/powerpc/net/bpf_jit_comp32.c +1 -1
@@ -852,7 +852,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, tmp_reg, exit_addr);/* else fall through to the epilogue */ break ;
arch/powerpc/net/bpf_jit_comp64.c +1 -1
@@ -761,7 +761,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, b2p[TMP_REG_1 ], exit_addr);/* else fall through to the epilogue */ break ;
Signature · 1 line
2.33.0
PATCH 5/9 powerpc/bpf: Fix BPF_MOD when imm == 1
In reply to Naveen N. RaoOnly ignore the operation if dividing by 1.
--- arch/powerpc/net/bpf_jit_comp64.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
arch/powerpc/net/bpf_jit_comp64.c +8 -2
@@ -391,8 +391,14 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case BPF_ALU64 |BPF_DIV |BPF_K :/* dst /= imm */ if (imm ==0 )return -EINVAL ; -else if (imm ==1 ) - goto bpf_alu32_trunc; +if (imm ==1 ) { +if (BPF_OP (code) ==BPF_DIV ) { + goto bpf_alu32_trunc; + }else { +EMIT (PPC_RAW_LI (dst_reg,0 )); +break ; + } + }PPC_LI32 (b2p[TMP_REG_1 ], imm);switch (BPF_CLASS (code)) {
Signature · 1 line
2.33.0
PATCH 6/9 powerpc/bpf: Fix BPF_SUB when imm == 0x80000000
In reply to Naveen N. RaoWe aren't handling subtraction involving an immediate value of 0x80000000 properly. Fix the same.
--- arch/powerpc/net/bpf_jit_comp64.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
arch/powerpc/net/bpf_jit_comp64.c +8 -8
@@ -333,15 +333,15 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case BPF_ALU |BPF_SUB |BPF_K :/* (u32) dst -= (u32) imm */ case BPF_ALU64 |BPF_ADD |BPF_K :/* dst += imm */ case BPF_ALU64 |BPF_SUB |BPF_K :/* dst -= imm */ -if (BPF_OP (code) ==BPF_SUB ) - imm = -imm; -if (imm) { -if (imm >=-32768 && imm <32768 ) -EMIT (PPC_RAW_ADDI (dst_reg, dst_reg,IMM_L (imm))); -else { -PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (imm >-32768 && imm <32768 ) { +EMIT (PPC_RAW_ADDI (dst_reg, dst_reg, +BPF_OP (code) ==BPF_SUB ?IMM_L (-imm) :IMM_L (imm))); + }else { +PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (BPF_OP (code) ==BPF_SUB ) +EMIT (PPC_RAW_SUB (dst_reg, dst_reg, b2p[TMP_REG_1 ])); +else EMIT (PPC_RAW_ADD (dst_reg, dst_reg, b2p[TMP_REG_1 ])); - } } goto bpf_alu32_trunc;case BPF_ALU |BPF_MUL |BPF_X :/* (u32) dst *= (u32) src */
Signature · 1 line
2.33.0
PATCH 7/9 powerpc/bpf: Limit 'ldbrx' to processors compliant with ISA v2.06
In reply to Naveen N. RaoJohan reported the below crash with test_bpf on ppc64 e5500:
test_bpf: #296 ALU_END_FROM_LE 64: 0x0123456789abcdef -> 0x67452301 jited:1
Oops: Exception in kernel mode, sig: 4 [#1] BE PAGE_SIZE=4K SMP NR_CPUS=24 QEMU e500 Modules linked in: test_bpf(+) CPU: 0 PID: 76 Comm: insmod Not tainted 5.14.0-03771-g98c2059e008a-dirty #1 NIP: 8000000000061c3c LR: 80000000006dea64 CTR: 8000000000061c18 REGS: c0000000032d3420 TRAP: 0700 Not tainted (5.14.0-03771-g98c2059e008a-dirty) MSR: 0000000080089000 <EE,ME> CR: 88002822 XER: 20000000 IRQMASK: 0 <...> NIP [8000000000061c3c] 0x8000000000061c3c LR [80000000006dea64] .__run_one+0x104/0x17c [test_bpf] Call Trace: .__run_one+0x60/0x17c [test_bpf] (unreliable) .test_bpf_init+0x6a8/0xdc8 [test_bpf] .do_one_initcall+0x6c/0x28c .do_init_module+0x68/0x28c .load_module+0x2460/0x2abc .__do_sys_init_module+0x120/0x18c .system_call_exception+0x110/0x1b8 system_call_common+0xf0/0x210 --- interrupt: c00 at 0x101d0acc <...> ---[ end trace 47b2bf19090bb3d0 ]---
Illegal instruction
The illegal instruction turned out to be 'ldbrx' emitted for BPF_FROM_[L|B]E, which was only introduced in ISA v2.06. Guard use of the same and implement an alternative approach for older processors.
--- arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/net/bpf_jit_comp64.c | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-)
arch/powerpc/include/asm/ppc-opcode.h +1 -0
@@ -498,6 +498,7 @@#define PPC_RAW_LDX (r, base, b) (0x7c00002a | ___PPC_RT(r) | ___PPC_RA(base) | ___PPC_RB(b))#define PPC_RAW_LHZ (r, base, i) (0xa0000000 | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))#define PPC_RAW_LHBRX (r, base, b) (0x7c00062c | ___PPC_RT(r) | ___PPC_RA(base) | ___PPC_RB(b)) +#define PPC_RAW_LWBRX (r, base, b) (0x7c00042c | ___PPC_RT(r) | ___PPC_RA(base) | ___PPC_RB(b))#define PPC_RAW_LDBRX (r, base, b) (0x7c000428 | ___PPC_RT(r) | ___PPC_RA(base) | ___PPC_RB(b))#define PPC_RAW_STWCX (s, a, b) (0x7c00012d | ___PPC_RS(s) | ___PPC_RA(a) | ___PPC_RB(b))#define PPC_RAW_CMPWI (a, i) (0x2c000000 | ___PPC_RA(a) | IMM_L(i))
arch/powerpc/net/bpf_jit_comp64.c +13 -9
@@ -601,17 +601,21 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *EMIT (PPC_RAW_MR (dst_reg, b2p[TMP_REG_1 ]));break ;case 64 : -/* -* Way easier and faster(?) to store the value -* into stack and then use ldbrx -* -* ctx->seen will be reliable in pass2, but -* the instructions generated will remain the -* same across all passes -*/ +/* Store the value to stack and then use byte-reverse loads */ PPC_BPF_STL (dst_reg,1 ,bpf_jit_stack_local (ctx));EMIT (PPC_RAW_ADDI (b2p[TMP_REG_1 ],1 ,bpf_jit_stack_local (ctx))); -EMIT (PPC_RAW_LDBRX (dst_reg,0 , b2p[TMP_REG_1 ])); +if (cpu_has_feature (CPU_FTR_ARCH_206 )) { +EMIT (PPC_RAW_LDBRX (dst_reg,0 , b2p[TMP_REG_1 ])); + }else { +EMIT (PPC_RAW_LWBRX (dst_reg,0 , b2p[TMP_REG_1 ])); +if (IS_ENABLED (CONFIG_CPU_LITTLE_ENDIAN )) +EMIT (PPC_RAW_SLDI (dst_reg, dst_reg,32 )); +EMIT (PPC_RAW_LI (b2p[TMP_REG_2 ],4 )); +EMIT (PPC_RAW_LWBRX (b2p[TMP_REG_2 ], b2p[TMP_REG_2 ], b2p[TMP_REG_1 ])); +if (IS_ENABLED (CONFIG_CPU_BIG_ENDIAN )) +EMIT (PPC_RAW_SLDI (b2p[TMP_REG_2 ], b2p[TMP_REG_2 ],32 )); +EMIT (PPC_RAW_OR (dst_reg, dst_reg, b2p[TMP_REG_2 ])); + }break ; }break ;
Signature · 1 line
2.33.0
PATCH 9/9 powerpc/bpf: Emit stf barrier instruction sequences for BPF_NOSPEC
In reply to Naveen N. RaoEmit similar instruction sequences to commit a048a07d7f4535 ("powerpc/64s: Add support for a store forwarding barrier at kernel entry/exit") when encountering BPF_NOSPEC.
Mitigations are enabled depending on what the firmware advertises. In particular, we do not gate these mitigations based on current settings, just like in x86. Due to this, we don't need to take any action if mitigations are enabled or disabled at runtime.
---
Thanks to Daniel Borkmann and Nick Piggin for their help in putting together this patch!
arch/powerpc/net/bpf_jit64.h | 8 ++--- arch/powerpc/net/bpf_jit_comp64.c | 55 ++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 8 deletions(-)
arch/powerpc/net/bpf_jit64.h +4 -4
@@ -16,18 +16,18 @@* with our redzone usage. * * [ prev sp ] <------------- -* [ nv gpr save area ] 6*8 | +* [ nv gpr save area ] 5*8 | * [ tail_call_cnt ] 8 | -* [ local_tmp_var ] 8 | +* [ local_tmp_var ] 16 | * fp (r31) --> [ ebpf stack space ] upto 512 | * [ frame header ] 32/112 | * sp (r1) ---> [ stack pointer ] -------------- */ /* for gpr non volatile registers BPG_REG_6 to 10 */ -#define BPF_PPC_STACK_SAVE (6*8) +#define BPF_PPC_STACK_SAVE (5*8)/* for bpf JIT code internal usage */ -#define BPF_PPC_STACK_LOCALS 16 +#define BPF_PPC_STACK_LOCALS 24/* stack frame excluding BPF stack, ensure this is quadword aligned */ #define BPF_PPC_STACKFRAME (STACK_FRAME_MIN_SIZE + \ BPF_PPC_STACK_LOCALS + BPF_PPC_STACK_SAVE)
arch/powerpc/net/bpf_jit_comp64.c +51 -4
94 lines not on this page · raw message
Signature · 1 line
2.33.0
PATCH 8/9 powerpc/security: Add a helper to query stf_barrier type
In reply to Naveen N. RaoAdd a helper to return the stf_barrier type for the current processor.
--- arch/powerpc/include/asm/security_features.h | 5 +++++ arch/powerpc/kernel/security.c | 5 +++++ 2 files changed, 10 insertions(+)
arch/powerpc/include/asm/security_features.h +5 -0
@@ -39,6 +39,11 @@ static inline bool security_ftr_enabled(u64 feature)return !!(powerpc_security_features & feature); } +#ifdef CONFIG_PPC_BOOK3S_64 +enum stf_barrier_type stf_barrier_type_get (void ); +#else +static inline enum stf_barrier_type stf_barrier_type_get (void ) {return STF_BARRIER_NONE ; } +#endif // Features indicating support for Spectre/Meltdown mitigations
arch/powerpc/kernel/security.c +5 -0
@@ -263,6 +263,11 @@ static int __init handle_no_stf_barrier(char *p)early_param ("no_stf_barrier" , handle_no_stf_barrier); +enum stf_barrier_type stf_barrier_type_get (void ) +{ +return stf_enabled_flush_types; +} +/* This is the generic flag used by other architectures */ static int __init handle_ssbd (char *p) {
Signature · 1 line
2.33.0
Re: [PATCH 1/9] powerpc/lib: Add helper to check if offset is within conditional branch range
In reply to Naveen N. RaoQuoted 6 lines · Naveen N. Rao
Add a helper to check if a given offset is within the branch range for a powerpc conditional branch instruction, and update some sites to use the new helper.
Signed-off-by Naveen N. Rao <[email protected]>
With one nitpick:
Quoted 23 lines
--- arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 7 ++++++- arch/powerpc/net/bpf_jit.h | 7 +------ 3 files changed, 8 insertions(+), 7 deletions(-)arch/powerpc/include/asm/code-patching.h +1
-0@@ -23,6 +23,7 @@#define BRANCH_ABSOLUTE 0x2bool is_offset_in_branch_range (long offset); +bool is_offset_in_cond_branch_range (long offset);int create_branch (struct ppc_inst *instr,const u32 *addr,unsigned long target,int flags); int create_cond_branch(struct ppc_inst *instr, const u32 *addr,arch/powerpc/lib/code-patching.c +0
-0
arch/powerpc/lib/code-patching.c bool is_offset_in_branch_range(long offset) · line 234
Quoted 7 lines
arch/powerpc/lib/code-patching.c +4
-0@@ -228,6 +228,11 @@ bool is_offset_in_branch_range(long offset)return (offset >=-0x2000000 && offset <=0x1fffffc && !(offset &0x3 )); } +bool is_offset_in_cond_branch_range (long offset) +{ +return offset >=-0x8000 && offset <=0x7FFF && !(offset &0x3 ); +}
Why not inline this one?
Quoted 41 lines
arch/powerpc/lib/code-patching.c +2
-1+/* * Helper to check if a given instruction is a conditional branch * Derived from the conditional checks in analyse_instr() @@ -280,7 +285,7 @@ int create_cond_branch(struct ppc_inst *instr, const u32 *addr, offset = offset - (unsigned long )addr;/* Check we can represent the target in the instruction format */ -if (offset <-0x8000 || offset >0x7FFF || offset &0x3 ) +if (!is_offset_in_cond_branch_range (offset))return 1 ;/* Mask out the flags and target, so they don't step on each other. */ arch/powerpc/net/bpf_jit.h +1
-7@@ -78,11 +78,6 @@#define PPC_FUNC_ADDR (d,i) do { PPC_LI32(d, i); } while(0)#endif -static inline bool is_nearbranch (int offset) -{ -return (offset <32768 ) && (offset >=-32768 ); -} -/* * The fly in the ointment of code size changing from pass to pass is * avoided by padding the short branch case with a NOP. If code size differs @@ -91,7 +86,7 @@ static inline bool is_nearbranch(int offset)* state. */ #define PPC_BCC (cond, dest) do { \ - if (is_nearbranch((dest) - (ctx->idx * 4))) { \ + if (is_offset_in_cond_branch_range((long)(dest) - (ctx->idx * 4))) { \ PPC_BCC_SHORT(cond, dest); \ EMIT(PPC_RAW_NOP()); \ } else { \ --2.33.0
Re: [PATCH 2/9] powerpc/bpf: Validate branch ranges
In reply to Naveen N. RaoQuoted 6 lines · Naveen N. Rao
Add checks to ensure that we never emit branch instructions with truncated branch offsets.
Suggested-by Michael Ellerman <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>
Quoted 136 lines
--- arch/powerpc/net/bpf_jit.h | 26 ++++++++++++++++++++------ arch/powerpc/net/bpf_jit_comp.c | 6 +++++- arch/powerpc/net/bpf_jit_comp32.c | 8 ++++++-- arch/powerpc/net/bpf_jit_comp64.c | 8 ++++++-- 4 files changed, 37 insertions(+), 11 deletions(-)arch/powerpc/net/bpf_jit.h +20
-6@@ -24,16 +24,30 @@#define EMIT (instr) PLANT_INSTR(image, ctx->idx, instr)/* Long jump; (unconditional 'branch') */ -#define PPC_JMP (dest) EMIT(PPC_INST_BRANCH | \ - (((dest) - (ctx->idx * 4)) & 0x03fffffc)) +#define PPC_JMP (dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_branch_range(offset)) { \ + pr_err_ratelimited("Branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \ + return -ERANGE; \ + } \ + EMIT(PPC_INST_BRANCH | (offset & 0x03fffffc)); \ + } while (0) +/* blr; (unconditional 'branch' with link) to absolute address */ #define PPC_BL_ABS (dest) EMIT(PPC_INST_BL | \ (((dest) - (unsigned long)(image + ctx->idx)) & 0x03fffffc))/* "cond" here covers BO:BI fields. */ -#define PPC_BCC_SHORT (cond, dest) EMIT(PPC_INST_BRANCH_COND | \ - (((cond) & 0x3ff) << 16) | \ - (((dest) - (ctx->idx * 4)) & \ - 0xfffc)) +#define PPC_BCC_SHORT (cond, dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_cond_branch_range(offset)) { \ + pr_err_ratelimited("Conditional branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \ + return -ERANGE; \ + } \ + EMIT(PPC_INST_BRANCH_COND | (((cond) & 0x3ff) << 16) | (offset & 0xfffc)); \ + } while (0) +/* Sign-extended 32-bit immediate load */ #define PPC_LI32 (d, i) do { \ if ((int)(uintptr_t)(i) >= -32768 && \arch/powerpc/net/bpf_jit_comp.c +5
-1@@ -210,7 +210,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)/* Now build the prologue, body code & epilogue for real. */ cgctx.idx =0 ;bpf_jit_build_prologue (code_base, &cgctx); -bpf_jit_build_body (fp, code_base, &cgctx, addrs, extra_pass); +if (bpf_jit_build_body (fp, code_base, &cgctx, addrs, extra_pass)) { +bpf_jit_binary_free (bpf_hdr); + fp = org_fp; + goto out_addrs; + }bpf_jit_build_epilogue (code_base, &cgctx);if (bpf_jit_enable >1 )arch/powerpc/net/bpf_jit_comp32.c +6
-2@@ -200,7 +200,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 fun } } -static void bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) +static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/* * By now, the eBPF program has already setup parameters in r3-r6 @@ -261,7 +261,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BCTR ()); +/* out: */ +return 0 ; }/* Assemble the body code between the prologue & epilogue */ @@ -1090,7 +1092,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context **/ case BPF_JMP |BPF_TAIL_CALL : ctx->seen |=SEEN_TAILCALL ; -bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); + ret =bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); +if (ret <0 ) +return ret;break ;default :arch/powerpc/net/bpf_jit_comp64.c +6
-3@@ -206,7 +206,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 funEMIT (PPC_RAW_BCTRL ()); } -static void bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) +static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/* * By now, the eBPF program has already setup parameters in r3, r4 and r5 @@ -267,7 +267,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BCTR ()); +/* out: */ +return 0 ; }/* Assemble the body code between the prologue & epilogue */ @@ -993,7 +995,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context **/ case BPF_JMP |BPF_TAIL_CALL : ctx->seen |=SEEN_TAILCALL ; -bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); + ret =bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); +if (ret <0 ) +return ret;break ;default : --2.33.0
Re: [PATCH 3/9] powerpc/bpf: Remove unused SEEN_STACK
In reply to Naveen N. RaoQuoted 8 lines · Naveen N. Rao
From: Ravi Bangoria <[email protected]>
SEEN_STACKis unused on PowerPC. Remove it. Also, haveSEEN_TAILCALLuse0x40000000.Signed-off-by Ravi Bangoria <[email protected]>Reviewed-by Christophe Leroy <[email protected]>
Quoted 21 lines
--- arch/powerpc/net/bpf_jit.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/powerpc/net/bpf_jit.h +1
-3@@ -125,8 +125,7 @@#define COND_LE (CR0_GT | COND_CMP_FALSE)#define SEEN_FUNC 0x20000000/* might call external helpers */ -#define SEEN_STACK 0x40000000/* uses BPF stack */ -#define SEEN_TAILCALL 0x80000000/* uses tail calls */ +#define SEEN_TAILCALL 0x40000000/* uses tail calls */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */ --2.33.0
Re: [PATCH 4/9] powerpc/bpf: Handle large branch ranges with BPF_EXIT
In reply to Naveen N. RaoQuoted 10 lines · Naveen N. Rao
In some scenarios, it is possible that the program epilogue is outside the branch range for a
BPF_EXITinstruction. Instead of rejecting such programs, emit an indirect branch. We track the size of the bpf program emitted after the initial run and do a second pass sinceBPF_EXITcan end up emitting different number of instructions depending on the program size.Suggested-by Jordan Niethe <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>
Quoted 102 lines
--- arch/powerpc/net/bpf_jit.h | 3 +++ arch/powerpc/net/bpf_jit_comp.c | 22 +++++++++++++++++++++- arch/powerpc/net/bpf_jit_comp32.c | 2 +- arch/powerpc/net/bpf_jit_comp64.c | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-)arch/powerpc/net/bpf_jit.h +3
-0@@ -126,6 +126,7 @@#define SEEN_FUNC 0x20000000/* might call external helpers */ #define SEEN_TAILCALL 0x40000000/* uses tail calls */ +#define SEEN_BIG_PROG 0x80000000/* large prog, >32MB */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */ @@ -179,6 +180,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *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_realloc_regs (struct codegen_context *ctx); +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr);#endif arch/powerpc/net/bpf_jit_comp.c +21
-1@@ -72,6 +72,21 @@ static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image,return 0 ; } +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr) +{ +if (!(ctx->seen &SEEN_BIG_PROG ) &&is_offset_in_branch_range (exit_addr)) { +PPC_JMP (exit_addr); + }else { + ctx->seen |=SEEN_BIG_PROG ; +PPC_FUNC_ADDR (tmp_reg, (unsigned long )image + exit_addr); +EMIT (PPC_RAW_MTCTR (tmp_reg)); +EMIT (PPC_RAW_BCTR ()); + } + +return 0 ; +} +struct powerpc64_jit_data {struct bpf_binary_header *header;u32 *addrs;@@ -155,12 +170,17 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) goto out_addrs; } +if (!is_offset_in_branch_range ((long )cgctx.idx *4 )) + cgctx.seen |=SEEN_BIG_PROG ; +/* * If we have seen a tail call, we need a second pass. * This is because bpf_jit_emit_common_epilogue() is called * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen. +* We also need a second pass if we ended up with too large +* a program so as to fix branches. */ -if (cgctx.seen &SEEN_TAILCALL ) { +if (cgctx.seen & (SEEN_TAILCALL |SEEN_BIG_PROG )) { cgctx.idx =0 ;if (bpf_jit_build_body (fp,0 , &cgctx, addrs, false)) { fp = org_fp;arch/powerpc/net/bpf_jit_comp32.c +1
-1@@ -852,7 +852,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, tmp_reg, exit_addr);/* else fall through to the epilogue */ break ;arch/powerpc/net/bpf_jit_comp64.c +1
-2@@ -761,7 +761,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, b2p[TMP_REG_1 ], exit_addr);/* else fall through to the epilogue */ break ; --2.33.0
Re: [PATCH 5/9] powerpc/bpf: Fix BPF_MOD when imm == 1
In reply to Naveen N. RaoQuoted 5 lines · Naveen N. Rao
Only ignore the operation if dividing by 1.
Fixes 156d0e290e969c ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF")Signed-off-by Naveen N. Rao <[email protected]>
Quoted 28 lines
--- arch/powerpc/net/bpf_jit_comp64.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)arch/powerpc/net/bpf_jit_comp64.c +8
-3@@ -391,8 +391,14 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case BPF_ALU64 |BPF_DIV |BPF_K :/* dst /= imm */ if (imm ==0 )return -EINVAL ; -else if (imm ==1 ) - goto bpf_alu32_trunc; +if (imm ==1 ) { +if (BPF_OP (code) ==BPF_DIV ) { + goto bpf_alu32_trunc; + }else { +EMIT (PPC_RAW_LI (dst_reg,0 )); +break ; + } + }PPC_LI32 (b2p[TMP_REG_1 ], imm);switch (BPF_CLASS (code)) { --2.33.0
Re: [PATCH 6/9] powerpc/bpf: Fix BPF_SUB when imm == 0x80000000
In reply to Naveen N. RaoQuoted 6 lines · Naveen N. Rao
We aren't handling subtraction involving an immediate value of
0x80000000properly. Fix the same.Fixes 156d0e290e969c ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF")Signed-off-by Naveen N. Rao <[email protected]>
Quoted 35 lines
--- arch/powerpc/net/bpf_jit_comp64.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)arch/powerpc/net/bpf_jit_comp64.c +8
-9@@ -333,15 +333,15 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case BPF_ALU |BPF_SUB |BPF_K :/* (u32) dst -= (u32) imm */ case BPF_ALU64 |BPF_ADD |BPF_K :/* dst += imm */ case BPF_ALU64 |BPF_SUB |BPF_K :/* dst -= imm */ -if (BPF_OP (code) ==BPF_SUB ) - imm = -imm; -if (imm) { -if (imm >=-32768 && imm <32768 ) -EMIT (PPC_RAW_ADDI (dst_reg, dst_reg,IMM_L (imm))); -else { -PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (imm >-32768 && imm <32768 ) { +EMIT (PPC_RAW_ADDI (dst_reg, dst_reg, +BPF_OP (code) ==BPF_SUB ?IMM_L (-imm) :IMM_L (imm))); + }else { +PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (BPF_OP (code) ==BPF_SUB ) +EMIT (PPC_RAW_SUB (dst_reg, dst_reg, b2p[TMP_REG_1 ])); +else EMIT (PPC_RAW_ADD (dst_reg, dst_reg, b2p[TMP_REG_1 ])); - } } goto bpf_alu32_trunc;case BPF_ALU |BPF_MUL |BPF_X :/* (u32) dst *= (u32) src */ --2.33.0
Re: [PATCH 2/9] powerpc/bpf: Validate branch ranges
In reply to Naveen N. RaoQuoted 6 lines · Naveen N. Rao
Add checks to ensure that we never emit branch instructions with truncated branch offsets.
Suggested-by Michael Ellerman <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>
Quoted 136 lines
--- arch/powerpc/net/bpf_jit.h | 26 ++++++++++++++++++++------ arch/powerpc/net/bpf_jit_comp.c | 6 +++++- arch/powerpc/net/bpf_jit_comp32.c | 8 ++++++-- arch/powerpc/net/bpf_jit_comp64.c | 8 ++++++-- 4 files changed, 37 insertions(+), 11 deletions(-)arch/powerpc/net/bpf_jit.h +20
-6@@ -24,16 +24,30 @@#define EMIT (instr) PLANT_INSTR(image, ctx->idx, instr)/* Long jump; (unconditional 'branch') */ -#define PPC_JMP (dest) EMIT(PPC_INST_BRANCH | \ - (((dest) - (ctx->idx * 4)) & 0x03fffffc)) +#define PPC_JMP (dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_branch_range(offset)) { \ + pr_err_ratelimited("Branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \ + return -ERANGE; \ + } \ + EMIT(PPC_INST_BRANCH | (offset & 0x03fffffc)); \ + } while (0) +/* blr; (unconditional 'branch' with link) to absolute address */ #define PPC_BL_ABS (dest) EMIT(PPC_INST_BL | \ (((dest) - (unsigned long)(image + ctx->idx)) & 0x03fffffc))/* "cond" here covers BO:BI fields. */ -#define PPC_BCC_SHORT (cond, dest) EMIT(PPC_INST_BRANCH_COND | \ - (((cond) & 0x3ff) << 16) | \ - (((dest) - (ctx->idx * 4)) & \ - 0xfffc)) +#define PPC_BCC_SHORT (cond, dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_cond_branch_range(offset)) { \ + pr_err_ratelimited("Conditional branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \ + return -ERANGE; \ + } \ + EMIT(PPC_INST_BRANCH_COND | (((cond) & 0x3ff) << 16) | (offset & 0xfffc)); \ + } while (0) +/* Sign-extended 32-bit immediate load */ #define PPC_LI32 (d, i) do { \ if ((int)(uintptr_t)(i) >= -32768 && \arch/powerpc/net/bpf_jit_comp.c +5
-1@@ -210,7 +210,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)/* Now build the prologue, body code & epilogue for real. */ cgctx.idx =0 ;bpf_jit_build_prologue (code_base, &cgctx); -bpf_jit_build_body (fp, code_base, &cgctx, addrs, extra_pass); +if (bpf_jit_build_body (fp, code_base, &cgctx, addrs, extra_pass)) { +bpf_jit_binary_free (bpf_hdr); + fp = org_fp; + goto out_addrs; + }bpf_jit_build_epilogue (code_base, &cgctx);if (bpf_jit_enable >1 )arch/powerpc/net/bpf_jit_comp32.c +6
-2@@ -200,7 +200,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 fun } } -static void bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) +static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/* * By now, the eBPF program has already setup parameters in r3-r6 @@ -261,7 +261,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BCTR ()); +/* out: */ +return 0 ; }/* Assemble the body code between the prologue & epilogue */ @@ -1090,7 +1092,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context **/ case BPF_JMP |BPF_TAIL_CALL : ctx->seen |=SEEN_TAILCALL ; -bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); + ret =bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); +if (ret <0 ) +return ret;break ;default :arch/powerpc/net/bpf_jit_comp64.c +6
-3@@ -206,7 +206,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 funEMIT (PPC_RAW_BCTRL ()); } -static void bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) +static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/* * By now, the eBPF program has already setup parameters in r3, r4 and r5 @@ -267,7 +267,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BCTR ()); +/* out: */ +return 0 ; }/* Assemble the body code between the prologue & epilogue */ @@ -993,7 +995,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context **/ case BPF_JMP |BPF_TAIL_CALL : ctx->seen |=SEEN_TAILCALL ; -bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); + ret =bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); +if (ret <0 ) +return ret;break ;default : --2.33.0
Re: [PATCH 3/9] powerpc/bpf: Remove unused SEEN_STACK
In reply to Naveen N. RaoQuoted 8 lines · Naveen N. Rao
From: Ravi Bangoria <[email protected]>
SEEN_STACKis unused on PowerPC. Remove it. Also, haveSEEN_TAILCALLuse0x40000000.Signed-off-by Ravi Bangoria <[email protected]>Reviewed-by Christophe Leroy <[email protected]>
Quoted 21 lines
--- arch/powerpc/net/bpf_jit.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/powerpc/net/bpf_jit.h +1
-3@@ -125,8 +125,7 @@#define COND_LE (CR0_GT | COND_CMP_FALSE)#define SEEN_FUNC 0x20000000/* might call external helpers */ -#define SEEN_STACK 0x40000000/* uses BPF stack */ -#define SEEN_TAILCALL 0x80000000/* uses tail calls */ +#define SEEN_TAILCALL 0x40000000/* uses tail calls */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */ --2.33.0
Re: [PATCH 4/9] powerpc/bpf: Handle large branch ranges with BPF_EXIT
In reply to Naveen N. RaoQuoted 10 lines · Naveen N. Rao
In some scenarios, it is possible that the program epilogue is outside the branch range for a
BPF_EXITinstruction. Instead of rejecting such programs, emit an indirect branch. We track the size of the bpf program emitted after the initial run and do a second pass sinceBPF_EXITcan end up emitting different number of instructions depending on the program size.Suggested-by Jordan Niethe <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>
Quoted 102 lines
--- arch/powerpc/net/bpf_jit.h | 3 +++ arch/powerpc/net/bpf_jit_comp.c | 22 +++++++++++++++++++++- arch/powerpc/net/bpf_jit_comp32.c | 2 +- arch/powerpc/net/bpf_jit_comp64.c | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-)arch/powerpc/net/bpf_jit.h +3
-0@@ -126,6 +126,7 @@#define SEEN_FUNC 0x20000000/* might call external helpers */ #define SEEN_TAILCALL 0x40000000/* uses tail calls */ +#define SEEN_BIG_PROG 0x80000000/* large prog, >32MB */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */ @@ -179,6 +180,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *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_realloc_regs (struct codegen_context *ctx); +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr);#endif arch/powerpc/net/bpf_jit_comp.c +21
-1@@ -72,6 +72,21 @@ static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image,return 0 ; } +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr) +{ +if (!(ctx->seen &SEEN_BIG_PROG ) &&is_offset_in_branch_range (exit_addr)) { +PPC_JMP (exit_addr); + }else { + ctx->seen |=SEEN_BIG_PROG ; +PPC_FUNC_ADDR (tmp_reg, (unsigned long )image + exit_addr); +EMIT (PPC_RAW_MTCTR (tmp_reg)); +EMIT (PPC_RAW_BCTR ()); + } + +return 0 ; +} +struct powerpc64_jit_data {struct bpf_binary_header *header;u32 *addrs;@@ -155,12 +170,17 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) goto out_addrs; } +if (!is_offset_in_branch_range ((long )cgctx.idx *4 )) + cgctx.seen |=SEEN_BIG_PROG ; +/* * If we have seen a tail call, we need a second pass. * This is because bpf_jit_emit_common_epilogue() is called * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen. +* We also need a second pass if we ended up with too large +* a program so as to fix branches. */ -if (cgctx.seen &SEEN_TAILCALL ) { +if (cgctx.seen & (SEEN_TAILCALL |SEEN_BIG_PROG )) { cgctx.idx =0 ;if (bpf_jit_build_body (fp,0 , &cgctx, addrs, false)) { fp = org_fp;arch/powerpc/net/bpf_jit_comp32.c +1
-1@@ -852,7 +852,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, tmp_reg, exit_addr);/* else fall through to the epilogue */ break ;arch/powerpc/net/bpf_jit_comp64.c +1
-2@@ -761,7 +761,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, b2p[TMP_REG_1 ], exit_addr);/* else fall through to the epilogue */ break ; --2.33.0
Re: [PATCH 5/9] powerpc/bpf: Fix BPF_MOD when imm == 1
In reply to Naveen N. RaoQuoted 5 lines · Naveen N. Rao
Only ignore the operation if dividing by 1.
Fixes 156d0e290e969c ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF")Signed-off-by Naveen N. Rao <[email protected]>
Quoted 28 lines
--- arch/powerpc/net/bpf_jit_comp64.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)arch/powerpc/net/bpf_jit_comp64.c +8
-3@@ -391,8 +391,14 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case BPF_ALU64 |BPF_DIV |BPF_K :/* dst /= imm */ if (imm ==0 )return -EINVAL ; -else if (imm ==1 ) - goto bpf_alu32_trunc; +if (imm ==1 ) { +if (BPF_OP (code) ==BPF_DIV ) { + goto bpf_alu32_trunc; + }else { +EMIT (PPC_RAW_LI (dst_reg,0 )); +break ; + } + }PPC_LI32 (b2p[TMP_REG_1 ], imm);switch (BPF_CLASS (code)) { --2.33.0
Re: [PATCH 6/9] powerpc/bpf: Fix BPF_SUB when imm == 0x80000000
In reply to Naveen N. RaoQuoted 6 lines · Naveen N. Rao
We aren't handling subtraction involving an immediate value of
0x80000000properly. Fix the same.Fixes 156d0e290e969c ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF")Signed-off-by Naveen N. Rao <[email protected]>
Quoted 35 lines
--- arch/powerpc/net/bpf_jit_comp64.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)arch/powerpc/net/bpf_jit_comp64.c +8
-9@@ -333,15 +333,15 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case BPF_ALU |BPF_SUB |BPF_K :/* (u32) dst -= (u32) imm */ case BPF_ALU64 |BPF_ADD |BPF_K :/* dst += imm */ case BPF_ALU64 |BPF_SUB |BPF_K :/* dst -= imm */ -if (BPF_OP (code) ==BPF_SUB ) - imm = -imm; -if (imm) { -if (imm >=-32768 && imm <32768 ) -EMIT (PPC_RAW_ADDI (dst_reg, dst_reg,IMM_L (imm))); -else { -PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (imm >-32768 && imm <32768 ) { +EMIT (PPC_RAW_ADDI (dst_reg, dst_reg, +BPF_OP (code) ==BPF_SUB ?IMM_L (-imm) :IMM_L (imm))); + }else { +PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (BPF_OP (code) ==BPF_SUB ) +EMIT (PPC_RAW_SUB (dst_reg, dst_reg, b2p[TMP_REG_1 ])); +else EMIT (PPC_RAW_ADD (dst_reg, dst_reg, b2p[TMP_REG_1 ])); - } } goto bpf_alu32_trunc;case BPF_ALU |BPF_MUL |BPF_X :/* (u32) dst *= (u32) src */ --2.33.0
Re: [PATCH 7/9] powerpc/bpf: Limit 'ldbrx' to processors compliant with ISA v2.06
In reply to Naveen N. RaoQuoted 29 lines · Naveen N. Rao
Johan reported the below crash with
test_bpfon ppc64 e5500:
test_bpf: #296ALU_END_FROM_LE64:0x0123456789abcdef->0x67452301jited:1Oops: Exception in kernel mode, sig: 4 [#1] BE PAGE_SIZE=4K SMP NR_CPUS=24 QEMU e500 Modules linked in: test_bpf(+) CPU: 0 PID: 76 Comm: insmod Not tainted 5.14.0-03771-g98c2059e008a-dirty #1 NIP: 8000000000061c3c LR: 80000000006dea64 CTR: 8000000000061c18 REGS: c0000000032d3420 TRAP: 0700 Not tainted (5.14.0-03771-g98c2059e008a-dirty) MSR: 0000000080089000 <EE,ME> CR: 88002822 XER: 20000000 IRQMASK: 0 <...> NIP [8000000000061c3c] 0x8000000000061c3c LR [80000000006dea64] .__run_one+0x104/0x17c [test_bpf] Call Trace: .__run_one+0x60/0x17c [test_bpf] (unreliable) .test_bpf_init+0x6a8/0xdc8 [test_bpf] .do_one_initcall+0x6c/0x28c .do_init_module+0x68/0x28c .load_module+0x2460/0x2abc .__do_sys_init_module+0x120/0x18c .system_call_exception+0x110/0x1b8 system_call_common+0xf0/0x210 --- interrupt: c00 at 0x101d0acc <...> ---[ end trace 47b2bf19090bb3d0 ]---Illegal instruction
Quoted 7 lines
The illegal instruction turned out to be 'ldbrx' emitted for BPF_FROM_[L|B]E, which was only introduced in ISA v2.06. Guard use of the same and implement an alternative approach for older processors.
Fixes 156d0e290e969c ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF")Reported-by Johan Almbladh <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>
Quoted 55 lines
--- arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/net/bpf_jit_comp64.c | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-)arch/powerpc/include/asm/ppc-opcode.h +1
-0@@ -498,6 +498,7 @@#define PPC_RAW_LDX (r, base, b) (0x7c00002a | ___PPC_RT(r) | ___PPC_RA(base) | ___PPC_RB(b))#define PPC_RAW_LHZ (r, base, i) (0xa0000000 | ___PPC_RT(r) | ___PPC_RA(base) | IMM_L(i))#define PPC_RAW_LHBRX (r, base, b) (0x7c00062c | ___PPC_RT(r) | ___PPC_RA(base) | ___PPC_RB(b)) +#define PPC_RAW_LWBRX (r, base, b) (0x7c00042c | ___PPC_RT(r) | ___PPC_RA(base) | ___PPC_RB(b))#define PPC_RAW_LDBRX (r, base, b) (0x7c000428 | ___PPC_RT(r) | ___PPC_RA(base) | ___PPC_RB(b))#define PPC_RAW_STWCX (s, a, b) (0x7c00012d | ___PPC_RS(s) | ___PPC_RA(a) | ___PPC_RB(b))#define PPC_RAW_CMPWI (a, i) (0x2c000000 | ___PPC_RA(a) | IMM_L(i))arch/powerpc/net/bpf_jit_comp64.c +13
-10@@ -601,17 +601,21 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *EMIT (PPC_RAW_MR (dst_reg, b2p[TMP_REG_1 ]));break ;case 64 : -/* -* Way easier and faster(?) to store the value -* into stack and then use ldbrx -* -* ctx->seen will be reliable in pass2, but -* the instructions generated will remain the -* same across all passes -*/ +/* Store the value to stack and then use byte-reverse loads */ PPC_BPF_STL (dst_reg,1 ,bpf_jit_stack_local (ctx));EMIT (PPC_RAW_ADDI (b2p[TMP_REG_1 ],1 ,bpf_jit_stack_local (ctx))); -EMIT (PPC_RAW_LDBRX (dst_reg,0 , b2p[TMP_REG_1 ])); +if (cpu_has_feature (CPU_FTR_ARCH_206 )) { +EMIT (PPC_RAW_LDBRX (dst_reg,0 , b2p[TMP_REG_1 ])); + }else { +EMIT (PPC_RAW_LWBRX (dst_reg,0 , b2p[TMP_REG_1 ])); +if (IS_ENABLED (CONFIG_CPU_LITTLE_ENDIAN )) +EMIT (PPC_RAW_SLDI (dst_reg, dst_reg,32 )); +EMIT (PPC_RAW_LI (b2p[TMP_REG_2 ],4 )); +EMIT (PPC_RAW_LWBRX (b2p[TMP_REG_2 ], b2p[TMP_REG_2 ], b2p[TMP_REG_1 ])); +if (IS_ENABLED (CONFIG_CPU_BIG_ENDIAN )) +EMIT (PPC_RAW_SLDI (b2p[TMP_REG_2 ], b2p[TMP_REG_2 ],32 )); +EMIT (PPC_RAW_OR (dst_reg, dst_reg, b2p[TMP_REG_2 ])); + }break ; }break ; --2.33.0
Quoted 4 lines · Naveen N. Rao
Various fixes to the eBPF JIT for powerpc, thanks to some new tests added by Johan. This series fixes all failures in
test_bpfon powerpc64. There are still some failures on powerpc32 to be looked into.
Great work! I have tested it on powerpc64 in QEMU, which is the same setup that previously triggered an illegal instruction, and all tests pass now. On powerpc32 there are still some issues left as you say.
Thanks!
Johan
Quoted 35 lines
- Naveen
Naveen N. Rao (8):
powerpc/lib: Add helper to check if offset is within conditional
branch range
powerpc/bpf: Validate branch ranges
powerpc/bpf: Handle large branch ranges withBPF_EXITpowerpc/bpf: FixBPF_MODwhen imm == 1
powerpc/bpf: FixBPF_SUBwhen imm ==0x80000000powerpc/bpf: Limit 'ldbrx' to processors compliant with ISA v2.06 powerpc/security: Add a helper to querystf_barriertype powerpc/bpf: Emit stf barrier instruction sequences forBPF_NOSPECRavi Bangoria (1):
powerpc/bpf: Remove unusedSEEN_STACKarch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/include/asm/ppc-opcode.h | 1 + arch/powerpc/include/asm/security_features.h | 5 + arch/powerpc/kernel/security.c | 5 + arch/powerpc/lib/code-patching.c | 7 +- arch/powerpc/net/bpf_jit.h | 39 ++++--- arch/powerpc/net/bpf_jit64.h | 8 +- arch/powerpc/net/bpf_jit_comp.c | 28 ++++- arch/powerpc/net/bpf_jit_comp32.c | 10 +- arch/powerpc/net/bpf_jit_comp64.c | 113 ++++++++++++++----- 10 files changed, 167 insertions(+), 50 deletions(-)base-commit:
044c2d99d9f43c6d6fde8bed00672517dd9a5a57--
2.33.0
Re: [PATCH 2/9] powerpc/bpf: Validate branch ranges
In reply to Naveen N. RaoLe 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 17 lines
Add checks to ensure that we never emit branch instructions with truncated branch offsets.
Suggested-by Michael Ellerman <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/net/bpf_jit.h | 26 ++++++++++++++++++++------ arch/powerpc/net/bpf_jit_comp.c | 6 +++++- arch/powerpc/net/bpf_jit_comp32.c | 8 ++++++-- arch/powerpc/net/bpf_jit_comp64.c | 8 ++++++-- 4 files changed, 37 insertions(+), 11 deletions(-)arch/powerpc/net/bpf_jit.h +0
-0
arch/powerpc/net/bpf_jit.h line 31
Quoted 10 lines
arch/powerpc/net/bpf_jit.h +5
-2@@ -24,16 +24,30 @@#define EMIT (instr) PLANT_INSTR(image, ctx->idx, instr)/* Long jump; (unconditional 'branch') */ -#define PPC_JMP (dest) EMIT(PPC_INST_BRANCH | \ - (((dest) - (ctx->idx * 4)) & 0x03fffffc)) +#define PPC_JMP (dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_branch_range(offset)) { \ + pr_err_ratelimited("Branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \
Does it really deserves a KERN_ERR ?
Isn't that something that can trigger with a userland request ?
Quoted 8 lines
arch/powerpc/net/bpf_jit.h +5
-0+return -ERANGE ; \ + } \ +EMIT (PPC_INST_BRANCH | (offset &0x03fffffc )); \ + }while (0 ) + /* blr; (unconditional 'branch' with link) to absolute address */#define PPC_BL_ABS (dest) EMIT(PPC_INST_BL | \ (((dest) - (unsigned long)(image + ctx->idx)) & 0x03fffffc))
arch/powerpc/net/bpf_jit.h line 45
Quoted 10 lines
arch/powerpc/net/bpf_jit.h +5
-4/* "cond" here covers BO:BI fields. */ -#define PPC_BCC_SHORT (cond, dest) EMIT(PPC_INST_BRANCH_COND | \ - (((cond) & 0x3ff) << 16) | \ - (((dest) - (ctx->idx * 4)) & \ - 0xfffc)) +#define PPC_BCC_SHORT (cond, dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_cond_branch_range(offset)) { \ + pr_err_ratelimited("Conditional branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \
Same
Quoted 94 lines
arch/powerpc/net/bpf_jit.h +5
-0+return -ERANGE ; \ + } \ +EMIT (PPC_INST_BRANCH_COND | (((cond) &0x3ff ) <<16 ) | (offset &0xfffc )); \ + }while (0 ) + /* Sign-extended 32-bit immediate load */#define PPC_LI32 (d, i) do { \ if ((int)(uintptr_t)(i) >= -32768 && \arch/powerpc/net/bpf_jit_comp.c +5
-1@@ -210,7 +210,11 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp)/* Now build the prologue, body code & epilogue for real. */ cgctx.idx =0 ;bpf_jit_build_prologue (code_base, &cgctx); -bpf_jit_build_body (fp, code_base, &cgctx, addrs, extra_pass); +if (bpf_jit_build_body (fp, code_base, &cgctx, addrs, extra_pass)) { +bpf_jit_binary_free (bpf_hdr); + fp = org_fp; + goto out_addrs; + }bpf_jit_build_epilogue (code_base, &cgctx);if (bpf_jit_enable >1 )arch/powerpc/net/bpf_jit_comp32.c +6
-2@@ -200,7 +200,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 fun } } -static void bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) +static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/* * By now, the eBPF program has already setup parameters in r3-r6 @@ -261,7 +261,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BCTR ()); +/* out: */ +return 0 ; }/* Assemble the body code between the prologue & epilogue */ @@ -1090,7 +1092,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context **/ case BPF_JMP |BPF_TAIL_CALL : ctx->seen |=SEEN_TAILCALL ; -bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); + ret =bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); +if (ret <0 ) +return ret;break ;default :arch/powerpc/net/bpf_jit_comp64.c +6
-2@@ -206,7 +206,7 @@ void bpf_jit_emit_func_call_rel(u32 *image, struct codegen_context *ctx, u64 funEMIT (PPC_RAW_BCTRL ()); } -static void bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) +static int bpf_jit_emit_tail_call (u32 *image,struct codegen_context *ctx,u32 out) {/* * By now, the eBPF program has already setup parameters in r3, r4 and r5 @@ -267,7 +267,9 @@ static void bpf_jit_emit_tail_call(u32 *image, struct codegen_context *ctx, u32bpf_jit_emit_common_epilogue (image, ctx);EMIT (PPC_RAW_BCTR ()); +/* out: */ +return 0 ; }/* Assemble the body code between the prologue & epilogue */ @@ -993,7 +995,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context **/ case BPF_JMP |BPF_TAIL_CALL : ctx->seen |=SEEN_TAILCALL ; -bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); + ret =bpf_jit_emit_tail_call (image, ctx, addrs[i +1 ]); +if (ret <0 ) +return ret;break ;default :
Re: [PATCH 1/9] powerpc/lib: Add helper to check if offset is within conditional branch range
In reply to Naveen N. RaoLe 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 28 lines
Add a helper to check if a given offset is within the branch range for a powerpc conditional branch instruction, and update some sites to use the new helper.
Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 7 ++++++- arch/powerpc/net/bpf_jit.h | 7 +------ 3 files changed, 8 insertions(+), 7 deletions(-)arch/powerpc/include/asm/code-patching.h +1
-0@@ -23,6 +23,7 @@#define BRANCH_ABSOLUTE 0x2bool is_offset_in_branch_range (long offset); +bool is_offset_in_cond_branch_range (long offset);int create_branch (struct ppc_inst *instr,const u32 *addr,unsigned long target,int flags); int create_cond_branch(struct ppc_inst *instr, const u32 *addr,arch/powerpc/lib/code-patching.c +0
-0
arch/powerpc/lib/code-patching.c bool is_offset_in_branch_range(long offset) · line 234
Quoted 7 lines
arch/powerpc/lib/code-patching.c +4
-0@@ -228,6 +228,11 @@ bool is_offset_in_branch_range(long offset)return (offset >=-0x2000000 && offset <=0x1fffffc && !(offset &0x3 )); } +bool is_offset_in_cond_branch_range (long offset) +{ +return offset >=-0x8000 && offset <=0x7FFF && !(offset &0x3 ); +}
Would be better without capital letters in numbers, in extenso 0x7fff instead of 0x7FFF
Quoted 39 lines
arch/powerpc/lib/code-patching.c +2
-1+/* * Helper to check if a given instruction is a conditional branch * Derived from the conditional checks in analyse_instr() @@ -280,7 +285,7 @@ int create_cond_branch(struct ppc_inst *instr, const u32 *addr, offset = offset - (unsigned long )addr;/* Check we can represent the target in the instruction format */ -if (offset <-0x8000 || offset >0x7FFF || offset &0x3 ) +if (!is_offset_in_cond_branch_range (offset))return 1 ;/* Mask out the flags and target, so they don't step on each other. */ arch/powerpc/net/bpf_jit.h +1
-6@@ -78,11 +78,6 @@#define PPC_FUNC_ADDR (d,i) do { PPC_LI32(d, i); } while(0)#endif -static inline bool is_nearbranch (int offset) -{ -return (offset <32768 ) && (offset >=-32768 ); -} -/* * The fly in the ointment of code size changing from pass to pass is * avoided by padding the short branch case with a NOP. If code size differs @@ -91,7 +86,7 @@ static inline bool is_nearbranch(int offset)* state. */ #define PPC_BCC (cond, dest) do { \ - if (is_nearbranch((dest) - (ctx->idx * 4))) { \ + if (is_offset_in_cond_branch_range((long)(dest) - (ctx->idx * 4))) { \ PPC_BCC_SHORT(cond, dest); \ EMIT(PPC_RAW_NOP()); \ } else { \
Re: [PATCH 3/9] powerpc/bpf: Remove unused SEEN_STACK
In reply to Naveen N. RaoLe 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 4 lines
From: Ravi Bangoria <[email protected]>
SEEN_STACKis unused on PowerPC. Remove it. Also, haveSEEN_TAILCALLuse0x40000000.
Why change SEEN_TAILCALL ? Would it be a problem to leave it as is ?
Quoted 22 lines
Signed-off-by Ravi Bangoria <[email protected]>Reviewed-by Christophe Leroy <[email protected]>--- arch/powerpc/net/bpf_jit.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/powerpc/net/bpf_jit.h +1
-2@@ -125,8 +125,7 @@#define COND_LE (CR0_GT | COND_CMP_FALSE)#define SEEN_FUNC 0x20000000/* might call external helpers */ -#define SEEN_STACK 0x40000000/* uses BPF stack */ -#define SEEN_TAILCALL 0x80000000/* uses tail calls */ +#define SEEN_TAILCALL 0x40000000/* uses tail calls */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */
Re: [PATCH 4/9] powerpc/bpf: Handle large branch ranges with BPF_EXIT
In reply to Naveen N. RaoLe 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 87 lines
In some scenarios, it is possible that the program epilogue is outside the branch range for a
BPF_EXITinstruction. Instead of rejecting such programs, emit an indirect branch. We track the size of the bpf program emitted after the initial run and do a second pass sinceBPF_EXITcan end up emitting different number of instructions depending on the program size.Suggested-by Jordan Niethe <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/net/bpf_jit.h | 3 +++ arch/powerpc/net/bpf_jit_comp.c | 22 +++++++++++++++++++++- arch/powerpc/net/bpf_jit_comp32.c | 2 +- arch/powerpc/net/bpf_jit_comp64.c | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-)arch/powerpc/net/bpf_jit.h +3
-0@@ -126,6 +126,7 @@#define SEEN_FUNC 0x20000000/* might call external helpers */ #define SEEN_TAILCALL 0x40000000/* uses tail calls */ +#define SEEN_BIG_PROG 0x80000000/* large prog, >32MB */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */ @@ -179,6 +180,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *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_realloc_regs (struct codegen_context *ctx); +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr);#endif arch/powerpc/net/bpf_jit_comp.c +21
-1@@ -72,6 +72,21 @@ static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image,return 0 ; } +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr) +{ +if (!(ctx->seen &SEEN_BIG_PROG ) &&is_offset_in_branch_range (exit_addr)) { +PPC_JMP (exit_addr); + }else { + ctx->seen |=SEEN_BIG_PROG ; +PPC_FUNC_ADDR (tmp_reg, (unsigned long )image + exit_addr); +EMIT (PPC_RAW_MTCTR (tmp_reg)); +EMIT (PPC_RAW_BCTR ()); + } + +return 0 ; +} +struct powerpc64_jit_data {struct bpf_binary_header *header;u32 *addrs;@@ -155,12 +170,17 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) goto out_addrs; } +if (!is_offset_in_branch_range ((long )cgctx.idx *4 )) + cgctx.seen |=SEEN_BIG_PROG ; +/* * If we have seen a tail call, we need a second pass. * This is because bpf_jit_emit_common_epilogue() is called * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen. +* We also need a second pass if we ended up with too large +* a program so as to fix branches. */ -if (cgctx.seen &SEEN_TAILCALL ) { +if (cgctx.seen & (SEEN_TAILCALL |SEEN_BIG_PROG )) { cgctx.idx =0 ;if (bpf_jit_build_body (fp,0 , &cgctx, addrs, false)) { fp = org_fp;arch/powerpc/net/bpf_jit_comp32.c +0
-0
arch/powerpc/net/bpf_jit_comp32.c int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * · line 855
Quoted 5 lines
arch/powerpc/net/bpf_jit_comp32.c +1
-1@@ -852,7 +852,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, tmp_reg, exit_addr);
On ppc32, if you use tmp_reg you must flag it. But I think you could use r0 instead.
Quoted 17 lines
arch/powerpc/net/bpf_jit_comp32.c +0
-0/* else fall through to the epilogue */ break ;arch/powerpc/net/bpf_jit_comp64.c +1
-1@@ -761,7 +761,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, b2p[TMP_REG_1 ], exit_addr);/* else fall through to the epilogue */ break ;
Re: [PATCH 6/9] powerpc/bpf: Fix BPF_SUB when imm == 0x80000000
In reply to Naveen N. RaoLe 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 26 lines
We aren't handling subtraction involving an immediate value of
0x80000000properly. Fix the same.Fixes 156d0e290e969c ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF")Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/net/bpf_jit_comp64.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)arch/powerpc/net/bpf_jit_comp64.c +2
-7@@ -333,15 +333,15 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case BPF_ALU |BPF_SUB |BPF_K :/* (u32) dst -= (u32) imm */ case BPF_ALU64 |BPF_ADD |BPF_K :/* dst += imm */ case BPF_ALU64 |BPF_SUB |BPF_K :/* dst -= imm */ -if (BPF_OP (code) ==BPF_SUB ) - imm = -imm; -if (imm) { -if (imm >=-32768 && imm <32768 ) -EMIT (PPC_RAW_ADDI (dst_reg, dst_reg,IMM_L (imm))); -else { -PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (imm >-32768 && imm <32768 ) { + EMIT(PPC_RAW_ADDI(dst_reg, dst_reg,
arch/powerpc/net/bpf_jit_comp64.c int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * · line 346
Quoted 10 lines
arch/powerpc/net/bpf_jit_comp64.c +6
-1+ BPF_OP(code) == BPF_SUB ? IMM_L(-imm) : IMM_L(imm))); + }else { +PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (BPF_OP (code) ==BPF_SUB ) +EMIT (PPC_RAW_SUB (dst_reg, dst_reg, b2p[TMP_REG_1 ])); +else EMIT (PPC_RAW_ADD (dst_reg, dst_reg, b2p[TMP_REG_1 ])); - } } goto bpf_alu32_trunc;
There is now so few code common to both BPF_ADD and BPF_SUB that you should make them different cases.
While at it, why not also use ADDIS if imm is 32 bits ? That would be an ADDIS/ADDI instead of LIS/ORI/ADD
Quoted 2 lines
arch/powerpc/net/bpf_jit_comp64.c +0
-0case BPF_ALU |BPF_MUL |BPF_X :/* (u32) dst *= (u32) src */
Re: [PATCH 1/9] powerpc/lib: Add helper to check if offset is within conditional branch range
In reply to Song LiuHi Song,
Thanks for the reviews.
Quoted 39 lines · Song Liu
Quoted 6 lines · Naveen N. Rao
Add a helper to check if a given offset is within the branch range for a powerpc conditional branch instruction, and update some sites to use the new helper.
Signed-off-by Naveen N. Rao <[email protected]>Acked-by Song Liu <[email protected]>With one nitpick:
Quoted 26 lines
--- arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 7 ++++++- arch/powerpc/net/bpf_jit.h | 7 +------ 3 files changed, 8 insertions(+), 7 deletions(-)arch/powerpc/include/asm/code-patching.h +1
-0@@ -23,6 +23,7 @@#define BRANCH_ABSOLUTE 0x2bool is_offset_in_branch_range (long offset); +bool is_offset_in_cond_branch_range (long offset);int create_branch (struct ppc_inst *instr,const u32 *addr,unsigned long target,int flags); int create_cond_branch(struct ppc_inst *instr, const u32 *addr,arch/powerpc/lib/code-patching.c +0
-0@@ -228,6 +228,11 @@ bool is_offset_in_branch_range(long offset)return (offset >=-0x2000000 && offset <=0x1fffffc && !(offset &0x3 )); }
Quoted 6 lines
arch/powerpc/lib/code-patching.c bool is_offset_in_branch_range(long offset) · line 234
Quoted 4 lines
arch/powerpc/lib/code-patching.c +4
-0+bool is_offset_in_cond_branch_range (long offset) +{ +return offset >=-0x8000 && offset <=0x7FFF && !(offset &0x3 ); +}Why not inline this one?
Good point. This was modeled after the existing is_offset_in_branch_range(), and I guess both of those helpers can be inlined. I'll do a separate patch for that.
- Naveen
Re: [PATCH 1/9] powerpc/lib: Add helper to check if offset is within conditional branch range
In reply to Christophe LeroyHi Christophe,
Thanks for the reviews.
Quoted 34 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 31 lines
Add a helper to check if a given offset is within the branch range for a powerpc conditional branch instruction, and update some sites to use the new helper.
Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/include/asm/code-patching.h | 1 + arch/powerpc/lib/code-patching.c | 7 ++++++- arch/powerpc/net/bpf_jit.h | 7 +------ 3 files changed, 8 insertions(+), 7 deletions(-)arch/powerpc/include/asm/code-patching.h +1
-0@@ -23,6 +23,7 @@#define BRANCH_ABSOLUTE 0x2bool is_offset_in_branch_range (long offset); +bool is_offset_in_cond_branch_range (long offset);int create_branch (struct ppc_inst *instr,const u32 *addr,unsigned long target,int flags); int create_cond_branch(struct ppc_inst *instr, const u32 *addr,arch/powerpc/lib/code-patching.c +0
-0@@ -228,6 +228,11 @@ bool is_offset_in_branch_range(long offset)return (offset >=-0x2000000 && offset <=0x1fffffc && !(offset &0x3 )); }
Quoted 6 lines
arch/powerpc/lib/code-patching.c bool is_offset_in_branch_range(long offset) · line 234
Quoted 4 lines
arch/powerpc/lib/code-patching.c +4
-0+bool is_offset_in_cond_branch_range (long offset) +{ +return offset >=-0x8000 && offset <=0x7FFF && !(offset &0x3 ); +}Would be better without capital letters in numbers, in extenso
0x7fffinstead of0x7FFF
Ack.
- Naveen
Re: [PATCH 2/9] powerpc/bpf: Validate branch ranges
In reply to Christophe LeroyQuoted 22 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 19 lines
Add checks to ensure that we never emit branch instructions with truncated branch offsets.
Suggested-by Michael Ellerman <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/net/bpf_jit.h | 26 ++++++++++++++++++++------ arch/powerpc/net/bpf_jit_comp.c | 6 +++++- arch/powerpc/net/bpf_jit_comp32.c | 8 ++++++-- arch/powerpc/net/bpf_jit_comp64.c | 8 ++++++-- 4 files changed, 37 insertions(+), 11 deletions(-)arch/powerpc/net/bpf_jit.h +0
-0@@ -24,16 +24,30 @@#define EMIT (instr) PLANT_INSTR(image, ctx->idx, instr)
Quoted 10 lines
arch/powerpc/net/bpf_jit.h line 31
Quoted 8 lines
arch/powerpc/net/bpf_jit.h +5
-2/* Long jump; (unconditional 'branch') */ -#define PPC_JMP (dest) EMIT(PPC_INST_BRANCH | \ - (((dest) - (ctx->idx * 4)) & 0x03fffffc)) +#define PPC_JMP (dest) \ + do { \ + long offset = (long)(dest) - (ctx->idx * 4); \ + if (!is_offset_in_branch_range(offset)) { \ + pr_err_ratelimited("Branch offset 0x%lx (@%u) out of range\n", offset, ctx->idx); \Does it really deserves a
KERN_ERR?
The intent is to ensure that we handle this when JIT'ing the BPF
instruction. One of the subsequent patches fixes the only scenario where we can hit this today. In practice, we should never hit this and if we do see this, then it is a bug with the JIT.
Quoted 1 line
Isn't that something that can trigger with a userland request ?
This can't be triggered by unprivileged BPF programs since those are limited to 4096 BPF instructions. You need root privileges to load large enough BPF programs that can trigger out of range branches.
- Naveen
Re: [PATCH 3/9] powerpc/bpf: Remove unused SEEN_STACK
In reply to Christophe LeroyQuoted 13 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 4 lines
From: Ravi Bangoria <[email protected]>
SEEN_STACKis unused on PowerPC. Remove it. Also, haveSEEN_TAILCALLuse0x40000000.Why change
SEEN_TAILCALL? Would it be a problem to leave it as is ?Quoted 3 lines
Signed-off-by Ravi Bangoria <[email protected]>Reviewed-by Christophe Leroy <[email protected]>
I prefer the bit usage to be contiguous. Changing SEEN_TAILCALL isn't a problem either.
- Naveen
Re: [PATCH 6/9] powerpc/bpf: Fix BPF_SUB when imm == 0x80000000
In reply to Christophe LeroyQuoted 42 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 26 lines
We aren't handling subtraction involving an immediate value of
0x80000000properly. Fix the same.Fixes 156d0e290e969c ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF")Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/net/bpf_jit_comp64.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)arch/powerpc/net/bpf_jit_comp64.c +2
-7@@ -333,15 +333,15 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case BPF_ALU |BPF_SUB |BPF_K :/* (u32) dst -= (u32) imm */ case BPF_ALU64 |BPF_ADD |BPF_K :/* dst += imm */ case BPF_ALU64 |BPF_SUB |BPF_K :/* dst -= imm */ -if (BPF_OP (code) ==BPF_SUB ) - imm = -imm; -if (imm) { -if (imm >=-32768 && imm <32768 ) -EMIT (PPC_RAW_ADDI (dst_reg, dst_reg,IMM_L (imm))); -else { -PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (imm >-32768 && imm <32768 ) { + EMIT(PPC_RAW_ADDI(dst_reg, dst_reg,arch/powerpc/net/bpf_jit_comp64.c int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * · line 346
Quoted 10 lines
arch/powerpc/net/bpf_jit_comp64.c +6
-1+ BPF_OP(code) == BPF_SUB ? IMM_L(-imm) : IMM_L(imm))); + }else { +PPC_LI32 (b2p[TMP_REG_1 ], imm); +if (BPF_OP (code) ==BPF_SUB ) +EMIT (PPC_RAW_SUB (dst_reg, dst_reg, b2p[TMP_REG_1 ])); +else EMIT (PPC_RAW_ADD (dst_reg, dst_reg, b2p[TMP_REG_1 ])); - } } goto bpf_alu32_trunc;There is now so few code common to both
BPF_ADDandBPF_SUBthat you should make them different cases.
Quoted 1 line
While at it, why not also use ADDIS if imm is 32 bits ? That would be an ADDIS/ADDI instead of LIS/ORI/ADD
Sure. I wanted to limit the change for this fix. We can do a separate patch to optimize code generation for BPF_ADD.
- Naveen
Hi Johan,
Quoted 10 lines · Johan Almbladh
Quoted 4 lines · Naveen N. Rao
Various fixes to the eBPF JIT for powerpc, thanks to some new tests added by Johan. This series fixes all failures in
test_bpfon powerpc64. There are still some failures on powerpc32 to be looked into.Great work! I have tested it on powerpc64 in QEMU, which is the same setup that previously triggered an illegal instruction, and all tests pass now. On powerpc32 there are still some issues left as you say.
Thanks for the review, and the test!
- Naveen
Re: [PATCH 4/9] powerpc/bpf: Handle large branch ranges with BPF_EXIT
In reply to Christophe LeroyQuoted 89 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 86 lines
In some scenarios, it is possible that the program epilogue is outside the branch range for a
BPF_EXITinstruction. Instead of rejecting such programs, emit an indirect branch. We track the size of the bpf program emitted after the initial run and do a second pass sinceBPF_EXITcan end up emitting different number of instructions depending on the program size.Suggested-by Jordan Niethe <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/net/bpf_jit.h | 3 +++ arch/powerpc/net/bpf_jit_comp.c | 22 +++++++++++++++++++++- arch/powerpc/net/bpf_jit_comp32.c | 2 +- arch/powerpc/net/bpf_jit_comp64.c | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-)arch/powerpc/net/bpf_jit.h +3
-0@@ -126,6 +126,7 @@#define SEEN_FUNC 0x20000000/* might call external helpers */ #define SEEN_TAILCALL 0x40000000/* uses tail calls */ +#define SEEN_BIG_PROG 0x80000000/* large prog, >32MB */ #define SEEN_VREG_MASK 0x1ff80000/* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff/* Non volatile registers r14-r31 */ @@ -179,6 +180,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *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_realloc_regs (struct codegen_context *ctx); +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr);#endif arch/powerpc/net/bpf_jit_comp.c +21
-1@@ -72,6 +72,21 @@ static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image,return 0 ; } +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, +int tmp_reg,unsigned long exit_addr) +{ +if (!(ctx->seen &SEEN_BIG_PROG ) &&is_offset_in_branch_range (exit_addr)) { +PPC_JMP (exit_addr); + }else { + ctx->seen |=SEEN_BIG_PROG ; +PPC_FUNC_ADDR (tmp_reg, (unsigned long )image + exit_addr); +EMIT (PPC_RAW_MTCTR (tmp_reg)); +EMIT (PPC_RAW_BCTR ()); + } + +return 0 ; +} +struct powerpc64_jit_data {struct bpf_binary_header *header;u32 *addrs;@@ -155,12 +170,17 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) goto out_addrs; } +if (!is_offset_in_branch_range ((long )cgctx.idx *4 )) + cgctx.seen |=SEEN_BIG_PROG ; +/* * If we have seen a tail call, we need a second pass. * This is because bpf_jit_emit_common_epilogue() is called * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen. +* We also need a second pass if we ended up with too large +* a program so as to fix branches. */ -if (cgctx.seen &SEEN_TAILCALL ) { +if (cgctx.seen & (SEEN_TAILCALL |SEEN_BIG_PROG )) { cgctx.idx =0 ;if (bpf_jit_build_body (fp,0 , &cgctx, addrs, false)) { fp = org_fp;arch/powerpc/net/bpf_jit_comp32.c +0
-0
Quoted 8 lines
arch/powerpc/net/bpf_jit_comp32.c int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * · line 855
Quoted 6 lines
arch/powerpc/net/bpf_jit_comp32.c +1
-1@@ -852,7 +852,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context ** we'll just fall through to the epilogue. */ if (i != flen -1 ) -PPC_JMP (exit_addr); +bpf_jit_emit_exit_insn (image, ctx, tmp_reg, exit_addr);On ppc32, if you use
tmp_regyou must flag it. But I think you could use r0 instead.
Indeed. Can we drop tracking of the temp registers and using them while remapping registers? Are you seeing significant benefits with re-use of those temp registers?
- Naveen
Re: [PATCH 6/9] powerpc/bpf: Fix BPF_SUB when imm == 0x80000000
In reply to Naveen N. RaoLe 04/10/2021 à 20:18, Naveen N. Rao a écrit :
Quoted 46 lines
Quoted 45 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 31 lines
We aren't handling subtraction involving an immediate value of
0x80000000properly. Fix the same.Fixes 156d0e290e969c ("powerpc/ebpf/jit: Implement JIT compiler for extended BPF")Signed-off-by Naveen N. Rao <[email protected]>---
arch/powerpc/net/bpf_jit_comp64.c| 16 ++++++++--------1 file changed, 8 insertions(+), 8 deletions(-)arch/powerpc/net/bpf_jit_comp64.c +0
-0@@ -333,15 +333,15 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *case
BPF_ALU|BPF_SUB|BPF_K: /* (u32) dst -= (u32) imm */case BPF_ALU64 | BPF_ADD | BPF_K: /* dst += imm */ case BPF_ALU64 | BPF_SUB | BPF_K: /* dst -= imm */- if (
BPF_OP(code)==BPF_SUB)
- imm = -imm;
- if (imm) {
- if (imm >= -32768 && imm < 32768)
-EMIT(PPC_RAW_ADDI(dst_reg, dst_reg, IMM_L(imm))); - else {
-PPC_LI32(b2p[TMP_REG_1], imm);
+ if (imm > -32768 && imm < 32768) {
+ EMIT(PPC_RAW_ADDI(dst_reg,dst_reg,
+BPF_OP(code)==BPF_SUB?IMM_L(-imm):IMM_L(imm)));
+ } else {
+PPC_LI32(b2p[TMP_REG_1], imm);Quoted 8 lines
+ if (
BPF_OP(code)==BPF_SUB)
+EMIT(PPC_RAW_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]));
+ else
EMIT(PPC_RAW_ADD(dst_reg, dst_reg, b2p[TMP_REG_1])); - }
}
gotobpf_alu32_trunc;There is now so few code common to both
BPF_ADDandBPF_SUBthat you should make them different cases.
Quoted 4 lines
Quoted 1 line
While at it, why not also use ADDIS if imm is 32 bits ? That would be an ADDIS/ADDI instead of LIS/ORI/ADD
Sure. I wanted to limit the change for this fix. We can do a separate patch to optimize code generation for
BPF_ADD.
Sure, this second part was just a thought, I agree it should be another patch.
My main comment here is to split stuff and make it a different case, I don't think it increases the change much, and IMO it is easier to read:
arch/powerpc/net/bpf_jit_comp64.c +11 -4
@@ -330,11 +330,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *EMIT (PPC_RAW_SUB (dst_reg, dst_reg, src_reg)); goto bpf_alu32_trunc;case BPF_ALU |BPF_ADD |BPF_K :/* (u32) dst += (u32) imm */ -case BPF_ALU |BPF_SUB |BPF_K :/* (u32) dst -= (u32) imm */ case BPF_ALU64 |BPF_ADD |BPF_K :/* dst += imm */ -case BPF_ALU64 |BPF_SUB |BPF_K :/* dst -= imm */ -if (BPF_OP (code) ==BPF_SUB ) - imm = -imm;if (imm) {if (imm >=-32768 && imm <32768 )EMIT (PPC_RAW_ADDI (dst_reg, dst_reg,IMM_L (imm)));
@@ -344,6 +340,17 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * } } goto bpf_alu32_trunc; +case BPF_ALU |BPF_SUB |BPF_K :/* (u32) dst -= (u32) imm */ +case BPF_ALU64 |BPF_SUB |BPF_K :/* dst -= imm */ +if (imm) { +if (-imm >=-32768 && -imm <32768 ) { +EMIT (PPC_RAW_ADDI (dst_reg, dst_reg,IMM_L (-imm))); + }else { +PPC_LI32 (b2p[TMP_REG_1 ], imm); +EMIT (PPC_RAW_SUB (dst_reg, dst_reg, b2p[TMP_REG_1 ])); + } + } + goto bpf_alu32_trunc;case BPF_ALU |BPF_MUL |BPF_X :/* (u32) dst *= (u32) src */ case BPF_ALU64 |BPF_MUL |BPF_X :/* dst *= src */ if (BPF_CLASS (code) ==BPF_ALU )
Re: [PATCH 4/9] powerpc/bpf: Handle large branch ranges with BPF_EXIT
In reply to Naveen N. RaoLe 04/10/2021 à 20:24, Naveen N. Rao a écrit :
Quoted 98 lines
Quoted 88 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 85 lines
In some scenarios, it is possible that the program epilogue is outside the branch range for a
BPF_EXITinstruction. Instead of rejecting such programs, emit an indirect branch. We track the size of the bpf program emitted after the initial run and do a second pass sinceBPF_EXITcan end up emitting different number of instructions depending on the program size.Suggested-by Jordan Niethe <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>---
arch/powerpc/net/bpf_jit.h | 3 +++ arch/powerpc/net/bpf_jit_comp.c | 22 +++++++++++++++++++++-arch/powerpc/net/bpf_jit_comp32.c| 2 +-arch/powerpc/net/bpf_jit_comp64.c| 2 +-4 files changed, 26 insertions(+), 3 deletions(-)arch/powerpc/net/bpf_jit.h +3
-0@@ -126,6 +126,7 @@ #define SEEN_FUNC 0x20000000 /* might call external helpers */ #define SEEN_TAILCALL 0x40000000 /* uses tail calls */ +#define SEEN_BIG_PROG 0x80000000/* large prog, >32MB */ #define SEEN_VREG_MASK 0x1ff80000 /* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff /* Non volatile registers r14-r31 */@@ -179,6 +180,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * 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_realloc_regs(struct codegen_context *ctx); +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, + int tmp_reg, unsigned long exit_addr); #endifarch/powerpc/net/bpf_jit_comp.c +15
-0@@ -72,6 +72,21 @@ static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image, return 0; } +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, + int tmp_reg, unsigned long exit_addr) +{ + if (!(ctx->seen & SEEN_BIG_PROG) &&is_offset_in_branch_range (exit_addr)) { + PPC_JMP(exit_addr); + } else { + ctx->seen |= SEEN_BIG_PROG; + PPC_FUNC_ADDR(tmp_reg, (unsigned long)image + exit_addr); + EMIT(PPC_RAW_MTCTR(tmp_reg)); + EMIT(PPC_RAW_BCTR()); + } + + return 0; +} + struct powerpc64_jit_data { struct bpf_binary_header *header;u32 *addrs;
arch/powerpc/net/bpf_jit_comp.c +6
-1@@ -155,12 +170,17 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) goto out_addrs; } + if (!is_offset_in_branch_range((long)cgctx.idx * 4)) + cgctx.seen |= SEEN_BIG_PROG; + /* * If we have seen a tail call, we need a second pass. * This is because bpf_jit_emit_common_epilogue() is called * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen. + * We also need a second pass if we ended up with too large + * a program so as to fix branches. */ - if (cgctx.seen & SEEN_TAILCALL) { + if (cgctx.seen & (SEEN_TAILCALL | SEEN_BIG_PROG)) { cgctx.idx = 0; if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) { fp = org_fp;arch/powerpc/net/bpf_jit_comp32.c +0
-0@@ -852,7 +852,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32Quoted 8 lines
arch/powerpc/net/bpf_jit_comp32.c int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * · line 855
Quoted 6 lines
arch/powerpc/net/bpf_jit_comp32.c +1
-1*image, struct codegen_context * * we'll just fall through to the epilogue. */ if (i != flen - 1) - PPC_JMP(exit_addr); + bpf_jit_emit_exit_insn(image, ctx, tmp_reg, exit_addr);On ppc32, if you use
tmp_regyou must flag it. But I think you could use r0 instead.
Quoted 3 lines
Indeed. Can we drop tracking of the temp registers and using them while remapping registers? Are you seeing significant benefits with re-use of those temp registers?
I'm not sure to follow you.
On ppc32, all volatile registers are used for function arguments, so temp registers are necessarily non-volatile so we track them as all non-volatile registers we use.
I think saving on stack only the non-volatile registers we use provides real benefit, otherwise you wouldn't have implemented it would you ?
Anyway here you should use _R0 instead of tmp_reg.
Christophe
Christophe Leroy
Reviewed-byRe: [PATCH 3/9] powerpc/bpf: Remove unused SEEN_STACK
In reply to Naveen N. RaoLe 04/10/2021 à 20:11, Naveen N. Rao a écrit :
Quoted 9 lines
Quoted 8 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 4 lines
From: Ravi Bangoria <[email protected]>
SEEN_STACKis unused on PowerPC. Remove it. Also, haveSEEN_TAILCALLuse0x40000000.
Quoted 8 lines
Quoted 5 lines
Why change
SEEN_TAILCALL? Would it be a problem to leave it as is ?Quoted 3 lines
Signed-off-by Ravi Bangoria <[email protected]>Reviewed-by Christophe Leroy <[email protected]>I prefer the bit usage to be contiguous. Changing
SEEN_TAILCALLisn't a problem either.
Well you are adding SEEN_BIG_PROG in following patch so it would still be contiguous at the end.
I don't really mind but I thought it would be less churn to just leave SEEN_TAILCALL as is and re-use 0x40000000 for SEEN_BIG_PROG.
Anyway
Re: [PATCH 3/9] powerpc/bpf: Remove unused SEEN_STACK
Quoted 18 lines · Christophe Leroy
Le 04/10/2021 à 20:11, Naveen N. Rao a écrit :
Quoted 15 lines
Quoted 13 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 4 lines
From: Ravi Bangoria <[email protected]>
SEEN_STACKis unused on PowerPC. Remove it. Also, haveSEEN_TAILCALLuse0x40000000.Why change
SEEN_TAILCALL? Would it be a problem to leave it as is ?Quoted 3 lines
Signed-off-by Ravi Bangoria <[email protected]>Reviewed-by Christophe Leroy <[email protected]>
Quoted 6 lines
Quoted 2 lines
I prefer the bit usage to be contiguous. Changing
SEEN_TAILCALLisn't a problem either.Well you are adding
SEEN_BIG_PROGin following patch so it would still be contiguous at the end.I don't really mind but I thought it would be less churn to just leave
SEEN_TAILCALLas is and re-use0x40000000forSEEN_BIG_PROG.
Ah ok. This patch was from a different series and it made more sense to change the bit number there. I have reused the patch here as-is since the change is fairly trivial.
- Naveen
Re: [PATCH 4/9] powerpc/bpf: Handle large branch ranges with BPF_EXIT
In reply to Christophe LeroyQuoted 105 lines · Christophe Leroy
Le 04/10/2021 à 20:24, Naveen N. Rao a écrit :
Quoted 98 lines
Quoted 88 lines · Christophe Leroy
Le 01/10/2021 à 23:14, Naveen N. Rao a écrit :
Quoted 85 lines
In some scenarios, it is possible that the program epilogue is outside the branch range for a
BPF_EXITinstruction. Instead of rejecting such programs, emit an indirect branch. We track the size of the bpf program emitted after the initial run and do a second pass sinceBPF_EXITcan end up emitting different number of instructions depending on the program size.Suggested-by Jordan Niethe <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>---
arch/powerpc/net/bpf_jit.h | 3 +++ arch/powerpc/net/bpf_jit_comp.c | 22 +++++++++++++++++++++-arch/powerpc/net/bpf_jit_comp32.c| 2 +-arch/powerpc/net/bpf_jit_comp64.c| 2 +-4 files changed, 26 insertions(+), 3 deletions(-)arch/powerpc/net/bpf_jit.h +3
-0@@ -126,6 +126,7 @@ #define SEEN_FUNC 0x20000000 /* might call external helpers */ #define SEEN_TAILCALL 0x40000000 /* uses tail calls */ +#define SEEN_BIG_PROG 0x80000000/* large prog, >32MB */ #define SEEN_VREG_MASK 0x1ff80000 /* Volatile registers r3-r12 */ #define SEEN_NVREG_MASK 0x0003ffff /* Non volatile registers r14-r31 */@@ -179,6 +180,8 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * 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_realloc_regs(struct codegen_context *ctx); +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, + int tmp_reg, unsigned long exit_addr); #endifarch/powerpc/net/bpf_jit_comp.c +15
-0@@ -72,6 +72,21 @@ static int bpf_jit_fixup_subprog_calls(struct bpf_prog *fp, u32 *image, return 0; } +int bpf_jit_emit_exit_insn (u32 *image,struct codegen_context *ctx, + int tmp_reg, unsigned long exit_addr) +{ + if (!(ctx->seen & SEEN_BIG_PROG) &&is_offset_in_branch_range (exit_addr)) { + PPC_JMP(exit_addr); + } else { + ctx->seen |= SEEN_BIG_PROG; + PPC_FUNC_ADDR(tmp_reg, (unsigned long)image + exit_addr); + EMIT(PPC_RAW_MTCTR(tmp_reg)); + EMIT(PPC_RAW_BCTR()); + } + + return 0; +} + struct powerpc64_jit_data { struct bpf_binary_header *header;u32 *addrs;
arch/powerpc/net/bpf_jit_comp.c +6
-1@@ -155,12 +170,17 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) goto out_addrs; } + if (!is_offset_in_branch_range((long)cgctx.idx * 4)) + cgctx.seen |= SEEN_BIG_PROG; + /* * If we have seen a tail call, we need a second pass. * This is because bpf_jit_emit_common_epilogue() is called * from bpf_jit_emit_tail_call() with a not yet stable ctx->seen. + * We also need a second pass if we ended up with too large + * a program so as to fix branches. */ - if (cgctx.seen & SEEN_TAILCALL) { + if (cgctx.seen & (SEEN_TAILCALL | SEEN_BIG_PROG)) { cgctx.idx = 0; if (bpf_jit_build_body(fp, 0, &cgctx, addrs, false)) { fp = org_fp;arch/powerpc/net/bpf_jit_comp32.c +0
-0@@ -852,7 +852,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32Quoted 8 lines
arch/powerpc/net/bpf_jit_comp32.c int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context * · line 855
Quoted 6 lines
arch/powerpc/net/bpf_jit_comp32.c +1
-1*image, struct codegen_context * * we'll just fall through to the epilogue. */ if (i != flen - 1) - PPC_JMP(exit_addr); + bpf_jit_emit_exit_insn(image, ctx, tmp_reg, exit_addr);On ppc32, if you use
tmp_regyou must flag it. But I think you could use r0 instead.Quoted 3 lines
Indeed. Can we drop tracking of the temp registers and using them while remapping registers? Are you seeing significant benefits with re-use of those temp registers?
Quoted 5 lines
I'm not sure to follow you.
On ppc32, all volatile registers are used for function arguments, so temp registers are necessarily non-volatile so we track them as all non-volatile registers we use.
I think saving on stack only the non-volatile registers we use provides real benefit, otherwise you wouldn't have implemented it would you ?
You're right. I was wary of having to track temporary register usage, which is a bit harder and prone to mistakes like the above. A related concern was that the register remapping is only used if there are no helper calls, which looks like a big limitation.
But, I do agree that it is worth the trouble for ppc32 given the register usage.
Quoted 2 lines
Anyway here you should use
_R0instead oftmp_reg.
Thanks,
Naveen