Skip to content

Commit 2611e24

Browse files
Alexei Starovoitovsashalevin
authored andcommitted
bpf: fix 64-bit divide
[ Upstream commit 876a7ae ] ALU64_DIV instruction should be dividing 64-bit by 64-bit, whereas do_div() does 64-bit by 32-bit divide. x64 and arm64 JITs correctly implement 64 by 64 unsigned divide. llvm BPF backend emits code assuming that ALU64_DIV does 64 by 64. Fixes: 89aa075 ("net: sock: allow eBPF programs to be attached to sockets") Reported-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
1 parent 7f1a6ae commit 2611e24

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

‎kernel/bpf/core.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
357357
ALU64_MOD_X:
358358
if (unlikely(SRC == 0))
359359
return 0;
360-
tmp = DST;
361-
DST = do_div(tmp, SRC);
360+
div64_u64_rem(DST, SRC, &tmp);
361+
DST = tmp;
362362
CONT;
363363
ALU_MOD_X:
364364
if (unlikely(SRC == 0))
@@ -367,8 +367,8 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
367367
DST = do_div(tmp, (u32) SRC);
368368
CONT;
369369
ALU64_MOD_K:
370-
tmp = DST;
371-
DST = do_div(tmp, IMM);
370+
div64_u64_rem(DST, IMM, &tmp);
371+
DST = tmp;
372372
CONT;
373373
ALU_MOD_K:
374374
tmp = (u32) DST;
@@ -377,7 +377,7 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
377377
ALU64_DIV_X:
378378
if (unlikely(SRC == 0))
379379
return 0;
380-
do_div(DST, SRC);
380+
DST = div64_u64(DST, SRC);
381381
CONT;
382382
ALU_DIV_X:
383383
if (unlikely(SRC == 0))
@@ -387,7 +387,7 @@ static unsigned int __bpf_prog_run(void *ctx, const struct bpf_insn *insn)
387387
DST = (u32) tmp;
388388
CONT;
389389
ALU64_DIV_K:
390-
do_div(DST, IMM);
390+
DST = div64_u64(DST, IMM);
391391
CONT;
392392
ALU_DIV_K:
393393
tmp = (u32) DST;

0 commit comments

Comments
 (0)