Skip to content

Commit 4fbb38a

Browse files
jrfastabAlexei Starovoitov
authored andcommitted
bpf, verifier: Remove redundant var_off.value ops in scalar known reg cases
In BPF_AND and BPF_OR alu cases we have this pattern when the src and dst tnum is a constant. 1 dst_reg->var_off = tnum_[op](dst_reg->var_off, src_reg.var_off) 2 scalar32_min_max_[op] 3 if (known) return 4 scalar_min_max_[op] 5 if (known) 6 __mark_reg_known(dst_reg, dst_reg->var_off.value [op] src_reg.var_off.value) The result is in 1 we calculate the var_off value and store it in the dst_reg. Then in 6 we duplicate this logic doing the op again on the value. The duplication comes from the the tnum_[op] handlers because they have already done the value calcuation. For example this is tnum_and(). struct tnum tnum_and(struct tnum a, struct tnum b) { u64 alpha, beta, v; alpha = a.value | a.mask; beta = b.value | b.mask; v = a.value & b.value; return TNUM(v, alpha & beta & ~v); } So lets remove the redundant op calculation. Its confusing for readers and unnecessary. Its also not harmful because those ops have the property, r1 & r1 = r1 and r1 | r1 = r1. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 84085f8 commit 4fbb38a

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

kernel/bpf/verifier.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5849,8 +5849,7 @@ static void scalar_min_max_and(struct bpf_reg_state *dst_reg,
58495849
u64 umax_val = src_reg->umax_value;
58505850

58515851
if (src_known && dst_known) {
5852-
__mark_reg_known(dst_reg, dst_reg->var_off.value &
5853-
src_reg->var_off.value);
5852+
__mark_reg_known(dst_reg, dst_reg->var_off.value);
58545853
return;
58555854
}
58565855

@@ -5920,8 +5919,7 @@ static void scalar_min_max_or(struct bpf_reg_state *dst_reg,
59205919
u64 umin_val = src_reg->umin_value;
59215920

59225921
if (src_known && dst_known) {
5923-
__mark_reg_known(dst_reg, dst_reg->var_off.value |
5924-
src_reg->var_off.value);
5922+
__mark_reg_known(dst_reg, dst_reg->var_off.value);
59255923
return;
59265924
}
59275925

0 commit comments

Comments
 (0)