Skip to content

Commit 09f6ac2

Browse files
committed
Merge branch 'bpf-ppc-div-fix'
Naveen N. Rao says: ==================== The first patch updates DIV64 overflow tests to properly detect error conditions. The second patch fixes powerpc64 JIT to generate the proper unsigned division instruction for BPF_ALU64. ==================== Acked-by: Sandipan Das <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
2 parents 0e26574 + 758f204 commit 09f6ac2

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

arch/powerpc/include/asm/ppc-opcode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@
338338
#define PPC_INST_MADDLD 0x10000033
339339
#define PPC_INST_DIVWU 0x7c000396
340340
#define PPC_INST_DIVD 0x7c0003d2
341+
#define PPC_INST_DIVDU 0x7c000392
341342
#define PPC_INST_RLWINM 0x54000000
342343
#define PPC_INST_RLWINM_DOT 0x54000001
343344
#define PPC_INST_RLWIMI 0x50000000

arch/powerpc/net/bpf_jit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
___PPC_RA(a) | IMM_L(i))
117117
#define PPC_DIVWU(d, a, b) EMIT(PPC_INST_DIVWU | ___PPC_RT(d) | \
118118
___PPC_RA(a) | ___PPC_RB(b))
119-
#define PPC_DIVD(d, a, b) EMIT(PPC_INST_DIVD | ___PPC_RT(d) | \
119+
#define PPC_DIVDU(d, a, b) EMIT(PPC_INST_DIVDU | ___PPC_RT(d) | \
120120
___PPC_RA(a) | ___PPC_RB(b))
121121
#define PPC_AND(d, a, b) EMIT(PPC_INST_AND | ___PPC_RA(d) | \
122122
___PPC_RS(a) | ___PPC_RB(b))

arch/powerpc/net/bpf_jit_comp64.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,12 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
399399
case BPF_ALU64 | BPF_DIV | BPF_X: /* dst /= src */
400400
case BPF_ALU64 | BPF_MOD | BPF_X: /* dst %= src */
401401
if (BPF_OP(code) == BPF_MOD) {
402-
PPC_DIVD(b2p[TMP_REG_1], dst_reg, src_reg);
402+
PPC_DIVDU(b2p[TMP_REG_1], dst_reg, src_reg);
403403
PPC_MULD(b2p[TMP_REG_1], src_reg,
404404
b2p[TMP_REG_1]);
405405
PPC_SUB(dst_reg, dst_reg, b2p[TMP_REG_1]);
406406
} else
407-
PPC_DIVD(dst_reg, dst_reg, src_reg);
407+
PPC_DIVDU(dst_reg, dst_reg, src_reg);
408408
break;
409409
case BPF_ALU | BPF_MOD | BPF_K: /* (u32) dst %= (u32) imm */
410410
case BPF_ALU | BPF_DIV | BPF_K: /* (u32) dst /= (u32) imm */
@@ -432,15 +432,15 @@ static int bpf_jit_build_body(struct bpf_prog *fp, u32 *image,
432432
break;
433433
case BPF_ALU64:
434434
if (BPF_OP(code) == BPF_MOD) {
435-
PPC_DIVD(b2p[TMP_REG_2], dst_reg,
435+
PPC_DIVDU(b2p[TMP_REG_2], dst_reg,
436436
b2p[TMP_REG_1]);
437437
PPC_MULD(b2p[TMP_REG_1],
438438
b2p[TMP_REG_1],
439439
b2p[TMP_REG_2]);
440440
PPC_SUB(dst_reg, dst_reg,
441441
b2p[TMP_REG_1]);
442442
} else
443-
PPC_DIVD(dst_reg, dst_reg,
443+
PPC_DIVDU(dst_reg, dst_reg,
444444
b2p[TMP_REG_1]);
445445
break;
446446
}

tools/testing/selftests/bpf/verifier/div_overflow.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
"DIV64 overflow, check 1",
3030
.insns = {
3131
BPF_MOV64_IMM(BPF_REG_1, -1),
32-
BPF_LD_IMM64(BPF_REG_0, LLONG_MIN),
33-
BPF_ALU64_REG(BPF_DIV, BPF_REG_0, BPF_REG_1),
32+
BPF_LD_IMM64(BPF_REG_2, LLONG_MIN),
33+
BPF_ALU64_REG(BPF_DIV, BPF_REG_2, BPF_REG_1),
34+
BPF_MOV32_IMM(BPF_REG_0, 0),
35+
BPF_JMP_REG(BPF_JEQ, BPF_REG_0, BPF_REG_2, 1),
36+
BPF_MOV32_IMM(BPF_REG_0, 1),
3437
BPF_EXIT_INSN(),
3538
},
3639
.prog_type = BPF_PROG_TYPE_SCHED_CLS,
@@ -40,8 +43,11 @@
4043
{
4144
"DIV64 overflow, check 2",
4245
.insns = {
43-
BPF_LD_IMM64(BPF_REG_0, LLONG_MIN),
44-
BPF_ALU64_IMM(BPF_DIV, BPF_REG_0, -1),
46+
BPF_LD_IMM64(BPF_REG_1, LLONG_MIN),
47+
BPF_ALU64_IMM(BPF_DIV, BPF_REG_1, -1),
48+
BPF_MOV32_IMM(BPF_REG_0, 0),
49+
BPF_JMP_REG(BPF_JEQ, BPF_REG_0, BPF_REG_1, 1),
50+
BPF_MOV32_IMM(BPF_REG_0, 1),
4551
BPF_EXIT_INSN(),
4652
},
4753
.prog_type = BPF_PROG_TYPE_SCHED_CLS,

0 commit comments

Comments
 (0)