Skip to content

Commit 2d9206b

Browse files
hbathinimpe
authored andcommitted
powerpc/bpf/32: Add instructions for atomic_[cmp]xchg
This adds two atomic opcodes BPF_XCHG and BPF_CMPXCHG on ppc32, both of which include the BPF_FETCH flag. The kernel's atomic_cmpxchg operation fundamentally has 3 operands, but we only have two register fields. Therefore the operand we compare against (the kernel's API calls it 'old') is hard-coded to be BPF_REG_R0. Also, kernel's atomic_cmpxchg returns the previous value at dst_reg + off. JIT the same for BPF too with return value put in BPF_REG_0. BPF_REG_R0 = atomic_cmpxchg(dst_reg + off, BPF_REG_R0, src_reg); Signed-off-by: Hari Bathini <[email protected]> Tested-by: Naveen N. Rao <[email protected]> (ppc64le) Reviewed-by: Naveen N. Rao <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent aea7ef8 commit 2d9206b

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

arch/powerpc/net/bpf_jit_comp32.c

+22-3
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
297297
u32 ax_reg = bpf_to_ppc(BPF_REG_AX);
298298
u32 tmp_reg = bpf_to_ppc(TMP_REG);
299299
u32 size = BPF_SIZE(code);
300+
u32 save_reg, ret_reg;
300301
s16 off = insn[i].off;
301302
s32 imm = insn[i].imm;
302303
bool func_addr_fixed;
@@ -799,6 +800,9 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
799800
* BPF_STX ATOMIC (atomic ops)
800801
*/
801802
case BPF_STX | BPF_ATOMIC | BPF_W:
803+
save_reg = _R0;
804+
ret_reg = src_reg;
805+
802806
bpf_set_seen_register(ctx, tmp_reg);
803807
bpf_set_seen_register(ctx, ax_reg);
804808

@@ -829,22 +833,37 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, struct codegen_context *
829833
case BPF_XOR | BPF_FETCH:
830834
EMIT(PPC_RAW_XOR(_R0, _R0, src_reg));
831835
break;
836+
case BPF_CMPXCHG:
837+
/*
838+
* Return old value in BPF_REG_0 for BPF_CMPXCHG &
839+
* in src_reg for other cases.
840+
*/
841+
ret_reg = bpf_to_ppc(BPF_REG_0);
842+
843+
/* Compare with old value in BPF_REG_0 */
844+
EMIT(PPC_RAW_CMPW(bpf_to_ppc(BPF_REG_0), _R0));
845+
/* Don't set if different from old value */
846+
PPC_BCC_SHORT(COND_NE, (ctx->idx + 3) * 4);
847+
fallthrough;
848+
case BPF_XCHG:
849+
save_reg = src_reg;
850+
break;
832851
default:
833852
pr_err_ratelimited("eBPF filter atomic op code %02x (@%d) unsupported\n",
834853
code, i);
835854
return -EOPNOTSUPP;
836855
}
837856

838857
/* store new value */
839-
EMIT(PPC_RAW_STWCX(_R0, tmp_reg, dst_reg));
858+
EMIT(PPC_RAW_STWCX(save_reg, tmp_reg, dst_reg));
840859
/* we're done if this succeeded */
841860
PPC_BCC_SHORT(COND_NE, tmp_idx);
842861

843862
/* For the BPF_FETCH variant, get old data into src_reg */
844863
if (imm & BPF_FETCH) {
845-
EMIT(PPC_RAW_MR(src_reg, ax_reg));
864+
EMIT(PPC_RAW_MR(ret_reg, ax_reg));
846865
if (!fp->aux->verifier_zext)
847-
EMIT(PPC_RAW_LI(src_reg_h, 0));
866+
EMIT(PPC_RAW_LI(ret_reg - 1, 0)); /* higher 32-bit */
848867
}
849868
break;
850869

0 commit comments

Comments
 (0)