Re: [PATCH v5 bpf-next 15/23] bpf: unify 32-bit and 64-bit is_branch_taken logic

Alexei Starovoitov
2023-10-30T19:52:20Z

On Fri, Oct 27, 2023 at 11:13:38AM -0700, Andrii Nakryiko wrote:
> Combine 32-bit and 64-bit is_branch_taken logic for SCALAR_VALUE
> registers. It makes it easier to see parallels between two domains
> (32-bit and 64-bit), and makes subsequent refactoring more
> straightforward.
>
> No functional changes.
>
> Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
> ---
> kernel/bpf/verifier.c | 154 ++++++++++--------------------------------
> 1 file changed, 36 insertions(+), 118 deletions(-)
>
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index fedd6d0e76e5..b911d1111fad 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -14185,166 +14185,86 @@ static u64 reg_const_value(struct bpf_reg_state *reg, bool subreg32)
> /*
> * <reg1> <op> <reg2>, currently assuming reg2 is a constant
> */
> -static int is_branch32_taken(struct bpf_reg_state *reg1, struct bpf_reg_state *reg2, u8 opcode)
> +static int is_scalar_branch_taken(struct bpf_reg_state *reg1, struct bpf_reg_state *reg2,
> + u8 opcode, bool is_jmp32)
> {
> - struct tnum subreg = tnum_subreg(reg1->var_off);
> - u32 val = (u32)tnum_subreg(reg2->var_off).value;
> - s32 sval = (s32)val;
> + struct tnum t1 = is_jmp32 ? tnum_subreg(reg1->var_off) : reg1->var_off;
> + u64 umin1 = is_jmp32 ? (u64)reg1->u32_min_value : reg1->umin_value;
> + u64 umax1 = is_jmp32 ? (u64)reg1->u32_max_value : reg1->umax_value;
> + s64 smin1 = is_jmp32 ? (s64)reg1->s32_min_value : reg1->smin_value;
> + s64 smax1 = is_jmp32 ? (s64)reg1->s32_max_value : reg1->smax_value;
> + u64 val = is_jmp32 ? (u32)tnum_subreg(reg2->var_off).value : reg2->var_off.value;
> + s64 sval = is_jmp32 ? (s32)val : (s64)val;

Maybe use uval and sval to be consisten with umin/smin ?