Skip to content

Commit d92d3a9

Browse files
iii-igregkh
authored andcommitted
s390/bpf: Fix optimizing out zero-extensions
commit db7bee6 upstream. Currently the JIT completely removes things like `reg32 += 0`, however, the BPF_ALU semantics requires the target register to be zero-extended in such cases. Fix by optimizing out only the arithmetic operation, but not the subsequent zero-extension. Reported-by: Johan Almbladh <johan.almbladh@anyfinetworks.com> Fixes: 0546231 ("s390/bpf: Add s390x eBPF JIT compiler backend") Reviewed-by: Heiko Carstens <hca@linux.ibm.com> Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4a93393 commit d92d3a9

1 file changed

Lines changed: 30 additions & 28 deletions

File tree

‎arch/s390/net/bpf_jit_comp.c‎

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -761,10 +761,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
761761
EMIT4(0xb9080000, dst_reg, src_reg);
762762
break;
763763
case BPF_ALU | BPF_ADD | BPF_K: /* dst = (u32) dst + (u32) imm */
764-
if (!imm)
765-
break;
766-
/* alfi %dst,imm */
767-
EMIT6_IMM(0xc20b0000, dst_reg, imm);
764+
if (imm != 0) {
765+
/* alfi %dst,imm */
766+
EMIT6_IMM(0xc20b0000, dst_reg, imm);
767+
}
768768
EMIT_ZERO(dst_reg);
769769
break;
770770
case BPF_ALU64 | BPF_ADD | BPF_K: /* dst = dst + imm */
@@ -786,10 +786,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
786786
EMIT4(0xb9090000, dst_reg, src_reg);
787787
break;
788788
case BPF_ALU | BPF_SUB | BPF_K: /* dst = (u32) dst - (u32) imm */
789-
if (!imm)
790-
break;
791-
/* alfi %dst,-imm */
792-
EMIT6_IMM(0xc20b0000, dst_reg, -imm);
789+
if (imm != 0) {
790+
/* alfi %dst,-imm */
791+
EMIT6_IMM(0xc20b0000, dst_reg, -imm);
792+
}
793793
EMIT_ZERO(dst_reg);
794794
break;
795795
case BPF_ALU64 | BPF_SUB | BPF_K: /* dst = dst - imm */
@@ -811,10 +811,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
811811
EMIT4(0xb90c0000, dst_reg, src_reg);
812812
break;
813813
case BPF_ALU | BPF_MUL | BPF_K: /* dst = (u32) dst * (u32) imm */
814-
if (imm == 1)
815-
break;
816-
/* msfi %r5,imm */
817-
EMIT6_IMM(0xc2010000, dst_reg, imm);
814+
if (imm != 1) {
815+
/* msfi %r5,imm */
816+
EMIT6_IMM(0xc2010000, dst_reg, imm);
817+
}
818818
EMIT_ZERO(dst_reg);
819819
break;
820820
case BPF_ALU64 | BPF_MUL | BPF_K: /* dst = dst * imm */
@@ -867,6 +867,8 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
867867
if (BPF_OP(insn->code) == BPF_MOD)
868868
/* lhgi %dst,0 */
869869
EMIT4_IMM(0xa7090000, dst_reg, 0);
870+
else
871+
EMIT_ZERO(dst_reg);
870872
break;
871873
}
872874
/* lhi %w0,0 */
@@ -999,10 +1001,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
9991001
EMIT4(0xb9820000, dst_reg, src_reg);
10001002
break;
10011003
case BPF_ALU | BPF_XOR | BPF_K: /* dst = (u32) dst ^ (u32) imm */
1002-
if (!imm)
1003-
break;
1004-
/* xilf %dst,imm */
1005-
EMIT6_IMM(0xc0070000, dst_reg, imm);
1004+
if (imm != 0) {
1005+
/* xilf %dst,imm */
1006+
EMIT6_IMM(0xc0070000, dst_reg, imm);
1007+
}
10061008
EMIT_ZERO(dst_reg);
10071009
break;
10081010
case BPF_ALU64 | BPF_XOR | BPF_K: /* dst = dst ^ imm */
@@ -1033,10 +1035,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
10331035
EMIT6_DISP_LH(0xeb000000, 0x000d, dst_reg, dst_reg, src_reg, 0);
10341036
break;
10351037
case BPF_ALU | BPF_LSH | BPF_K: /* dst = (u32) dst << (u32) imm */
1036-
if (imm == 0)
1037-
break;
1038-
/* sll %dst,imm(%r0) */
1039-
EMIT4_DISP(0x89000000, dst_reg, REG_0, imm);
1038+
if (imm != 0) {
1039+
/* sll %dst,imm(%r0) */
1040+
EMIT4_DISP(0x89000000, dst_reg, REG_0, imm);
1041+
}
10401042
EMIT_ZERO(dst_reg);
10411043
break;
10421044
case BPF_ALU64 | BPF_LSH | BPF_K: /* dst = dst << imm */
@@ -1058,10 +1060,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
10581060
EMIT6_DISP_LH(0xeb000000, 0x000c, dst_reg, dst_reg, src_reg, 0);
10591061
break;
10601062
case BPF_ALU | BPF_RSH | BPF_K: /* dst = (u32) dst >> (u32) imm */
1061-
if (imm == 0)
1062-
break;
1063-
/* srl %dst,imm(%r0) */
1064-
EMIT4_DISP(0x88000000, dst_reg, REG_0, imm);
1063+
if (imm != 0) {
1064+
/* srl %dst,imm(%r0) */
1065+
EMIT4_DISP(0x88000000, dst_reg, REG_0, imm);
1066+
}
10651067
EMIT_ZERO(dst_reg);
10661068
break;
10671069
case BPF_ALU64 | BPF_RSH | BPF_K: /* dst = dst >> imm */
@@ -1083,10 +1085,10 @@ static noinline int bpf_jit_insn(struct bpf_jit *jit, struct bpf_prog *fp,
10831085
EMIT6_DISP_LH(0xeb000000, 0x000a, dst_reg, dst_reg, src_reg, 0);
10841086
break;
10851087
case BPF_ALU | BPF_ARSH | BPF_K: /* ((s32) dst >> imm */
1086-
if (imm == 0)
1087-
break;
1088-
/* sra %dst,imm(%r0) */
1089-
EMIT4_DISP(0x8a000000, dst_reg, REG_0, imm);
1088+
if (imm != 0) {
1089+
/* sra %dst,imm(%r0) */
1090+
EMIT4_DISP(0x8a000000, dst_reg, REG_0, imm);
1091+
}
10901092
EMIT_ZERO(dst_reg);
10911093
break;
10921094
case BPF_ALU64 | BPF_ARSH | BPF_K: /* ((s64) dst) >>= imm */

0 commit comments

Comments
 (0)