Skip to content

Commit

Permalink
bpf, arm64: Emit A64_{ADD,SUB}_I when possible in emit_{lse,ll_sc}_at…
Browse files Browse the repository at this point in the history
…omic()

Currently in emit_{lse,ll_sc}_atomic(), if there is an offset, we add it
to the base address by emitting two instructions, for example:

  if (off) {
          emit_a64_mov_i(1, tmp, off, ctx);
          emit(A64_ADD(1, tmp, tmp, dst), ctx);
  ...

As pointed out by Xu, we can combine the above into a single A64_ADD_I
instruction if 'is_addsub_imm(off)' is true, or an A64_SUB_I, if
'is_addsub_imm(-off)' is true.

Suggested-by: Xu Kuohai <[email protected]>
Signed-off-by: Peilin Ye <[email protected]>
  • Loading branch information
peilin-ye authored and Kernel Patches Daemon committed Dec 30, 2024
1 parent 7dd4eef commit 5108796
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions arch/arm64/net/bpf_jit_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,14 @@ static int emit_lse_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
u8 reg = dst;

if (off) {
emit_a64_mov_i(1, tmp, off, ctx);
emit(A64_ADD(1, tmp, tmp, dst), ctx);
if (is_addsub_imm(off)) {
emit(A64_ADD_I(1, tmp, reg, off), ctx);
} else if (is_addsub_imm(-off)) {
emit(A64_SUB_I(1, tmp, reg, -off), ctx);
} else {
emit_a64_mov_i(1, tmp, off, ctx);
emit(A64_ADD(1, tmp, tmp, reg), ctx);
}
reg = tmp;
}
if (arena) {
Expand Down Expand Up @@ -721,7 +727,7 @@ static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
const s32 imm = insn->imm;
const s16 off = insn->off;
const bool isdw = BPF_SIZE(code) == BPF_DW;
u8 reg;
u8 reg = dst;
s32 jmp_offset;

if (BPF_MODE(code) == BPF_PROBE_ATOMIC) {
Expand All @@ -730,11 +736,15 @@ static int emit_ll_sc_atomic(const struct bpf_insn *insn, struct jit_ctx *ctx)
return -EINVAL;
}

if (!off) {
reg = dst;
} else {
emit_a64_mov_i(1, tmp, off, ctx);
emit(A64_ADD(1, tmp, tmp, dst), ctx);
if (off) {
if (is_addsub_imm(off)) {
emit(A64_ADD_I(1, tmp, reg, off), ctx);
} else if (is_addsub_imm(-off)) {
emit(A64_SUB_I(1, tmp, reg, -off), ctx);
} else {
emit_a64_mov_i(1, tmp, off, ctx);
emit(A64_ADD(1, tmp, tmp, reg), ctx);
}
reg = tmp;
}

Expand Down

0 comments on commit 5108796

Please sign in to comment.