Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Add the get_reg_width function

Put calculation of the register value width into a dedicated function.
This function will also be used in a following commit.

Signed-off-by: Maxim Mikityanskiy <maxim@isovalent.com>
Link: https://lore.kernel.org/r/20240108205209.838365-8-maxtram95@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Maxim Mikityanskiy and committed by
Alexei Starovoitov
87e51ac6 8e0e074a

+7 -2
+7 -2
kernel/bpf/verifier.c
··· 4450 4450 return BPF_CLASS(insn->code) == BPF_ST && BPF_MODE(insn->code) == BPF_MEM; 4451 4451 } 4452 4452 4453 + static int get_reg_width(struct bpf_reg_state *reg) 4454 + { 4455 + return fls64(reg->umax_value); 4456 + } 4457 + 4453 4458 /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, 4454 4459 * stack boundary and alignment are checked in check_mem_access() 4455 4460 */ ··· 4507 4502 if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && env->bpf_capable) { 4508 4503 save_register_state(env, state, spi, reg, size); 4509 4504 /* Break the relation on a narrowing spill. */ 4510 - if (fls64(reg->umax_value) > BITS_PER_BYTE * size) 4505 + if (get_reg_width(reg) > BITS_PER_BYTE * size) 4511 4506 state->stack[spi].spilled_ptr.id = 0; 4512 4507 } else if (!reg && !(off % BPF_REG_SIZE) && is_bpf_st_mem(insn) && 4513 4508 insn->imm != 0 && env->bpf_capable) { ··· 13964 13959 return -EACCES; 13965 13960 } else if (src_reg->type == SCALAR_VALUE) { 13966 13961 if (insn->off == 0) { 13967 - bool is_src_reg_u32 = src_reg->umax_value <= U32_MAX; 13962 + bool is_src_reg_u32 = get_reg_width(src_reg) <= 32; 13968 13963 13969 13964 if (is_src_reg_u32) 13970 13965 assign_scalar_id_before_mov(env, src_reg);