Skip to content

Commit 163541e

Browse files
Jiong WangAlexei Starovoitov
Jiong Wang
authored and
Alexei Starovoitov
committed
arm: bpf: eliminate zero extension code-gen
Cc: Shubham Bansal <illusionist.neo@gmail.com> Signed-off-by: Jiong Wang <jiong.wang@netronome.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 9d120b4 commit 163541e

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

arch/arm/net/bpf_jit_32.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,8 @@ static inline void emit_a32_alu_r64(const bool is64, const s8 dst[],
736736

737737
/* ALU operation */
738738
emit_alu_r(rd[1], rs, true, false, op, ctx);
739-
emit_a32_mov_i(rd[0], 0, ctx);
739+
if (!ctx->prog->aux->verifier_zext)
740+
emit_a32_mov_i(rd[0], 0, ctx);
740741
}
741742

742743
arm_bpf_put_reg64(dst, rd, ctx);
@@ -758,8 +759,9 @@ static inline void emit_a32_mov_r64(const bool is64, const s8 dst[],
758759
struct jit_ctx *ctx) {
759760
if (!is64) {
760761
emit_a32_mov_r(dst_lo, src_lo, ctx);
761-
/* Zero out high 4 bytes */
762-
emit_a32_mov_i(dst_hi, 0, ctx);
762+
if (!ctx->prog->aux->verifier_zext)
763+
/* Zero out high 4 bytes */
764+
emit_a32_mov_i(dst_hi, 0, ctx);
763765
} else if (__LINUX_ARM_ARCH__ < 6 &&
764766
ctx->cpu_architecture < CPU_ARCH_ARMv5TE) {
765767
/* complete 8 byte move */
@@ -1060,17 +1062,20 @@ static inline void emit_ldx_r(const s8 dst[], const s8 src,
10601062
case BPF_B:
10611063
/* Load a Byte */
10621064
emit(ARM_LDRB_I(rd[1], rm, off), ctx);
1063-
emit_a32_mov_i(rd[0], 0, ctx);
1065+
if (!ctx->prog->aux->verifier_zext)
1066+
emit_a32_mov_i(rd[0], 0, ctx);
10641067
break;
10651068
case BPF_H:
10661069
/* Load a HalfWord */
10671070
emit(ARM_LDRH_I(rd[1], rm, off), ctx);
1068-
emit_a32_mov_i(rd[0], 0, ctx);
1071+
if (!ctx->prog->aux->verifier_zext)
1072+
emit_a32_mov_i(rd[0], 0, ctx);
10691073
break;
10701074
case BPF_W:
10711075
/* Load a Word */
10721076
emit(ARM_LDR_I(rd[1], rm, off), ctx);
1073-
emit_a32_mov_i(rd[0], 0, ctx);
1077+
if (!ctx->prog->aux->verifier_zext)
1078+
emit_a32_mov_i(rd[0], 0, ctx);
10741079
break;
10751080
case BPF_DW:
10761081
/* Load a Double Word */
@@ -1359,6 +1364,11 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
13591364
case BPF_ALU64 | BPF_MOV | BPF_X:
13601365
switch (BPF_SRC(code)) {
13611366
case BPF_X:
1367+
if (imm == 1) {
1368+
/* Special mov32 for zext */
1369+
emit_a32_mov_i(dst_hi, 0, ctx);
1370+
break;
1371+
}
13621372
emit_a32_mov_r64(is64, dst, src, ctx);
13631373
break;
13641374
case BPF_K:
@@ -1438,7 +1448,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
14381448
}
14391449
emit_udivmod(rd_lo, rd_lo, rt, ctx, BPF_OP(code));
14401450
arm_bpf_put_reg32(dst_lo, rd_lo, ctx);
1441-
emit_a32_mov_i(dst_hi, 0, ctx);
1451+
if (!ctx->prog->aux->verifier_zext)
1452+
emit_a32_mov_i(dst_hi, 0, ctx);
14421453
break;
14431454
case BPF_ALU64 | BPF_DIV | BPF_K:
14441455
case BPF_ALU64 | BPF_DIV | BPF_X:
@@ -1453,7 +1464,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
14531464
return -EINVAL;
14541465
if (imm)
14551466
emit_a32_alu_i(dst_lo, imm, ctx, BPF_OP(code));
1456-
emit_a32_mov_i(dst_hi, 0, ctx);
1467+
if (!ctx->prog->aux->verifier_zext)
1468+
emit_a32_mov_i(dst_hi, 0, ctx);
14571469
break;
14581470
/* dst = dst << imm */
14591471
case BPF_ALU64 | BPF_LSH | BPF_K:
@@ -1488,7 +1500,8 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
14881500
/* dst = ~dst */
14891501
case BPF_ALU | BPF_NEG:
14901502
emit_a32_alu_i(dst_lo, 0, ctx, BPF_OP(code));
1491-
emit_a32_mov_i(dst_hi, 0, ctx);
1503+
if (!ctx->prog->aux->verifier_zext)
1504+
emit_a32_mov_i(dst_hi, 0, ctx);
14921505
break;
14931506
/* dst = ~dst (64 bit) */
14941507
case BPF_ALU64 | BPF_NEG:
@@ -1544,11 +1557,13 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
15441557
#else /* ARMv6+ */
15451558
emit(ARM_UXTH(rd[1], rd[1]), ctx);
15461559
#endif
1547-
emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
1560+
if (!ctx->prog->aux->verifier_zext)
1561+
emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
15481562
break;
15491563
case 32:
15501564
/* zero-extend 32 bits into 64 bits */
1551-
emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
1565+
if (!ctx->prog->aux->verifier_zext)
1566+
emit(ARM_EOR_R(rd[0], rd[0], rd[0]), ctx);
15521567
break;
15531568
case 64:
15541569
/* nop */
@@ -1838,6 +1853,11 @@ void bpf_jit_compile(struct bpf_prog *prog)
18381853
/* Nothing to do here. We support Internal BPF. */
18391854
}
18401855

1856+
bool bpf_jit_needs_zext(void)
1857+
{
1858+
return true;
1859+
}
1860+
18411861
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
18421862
{
18431863
struct bpf_prog *tmp, *orig_prog = prog;

0 commit comments

Comments
 (0)