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

Configure Feed

Select the types of activity you want to include in your feed.

bpf: Consistently use reg_state() for register access in the verifier

Replace the pattern of declaring a local regs array from cur_regs()
and then indexing into it with the more concise reg_state() helper.
This simplifies the code by eliminating intermediate variables and
makes register access more consistent throughout the verifier.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20260113134826.2214860-1-mykyta.yatsenko5@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Mykyta Yatsenko and committed by
Alexei Starovoitov
7af33399 9c1a3525

+19 -23
+19 -23
kernel/bpf/verifier.c
··· 5654 5654 static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, 5655 5655 int off, int size, enum bpf_access_type type) 5656 5656 { 5657 - struct bpf_reg_state *regs = cur_regs(env); 5658 - struct bpf_map *map = regs[regno].map_ptr; 5657 + struct bpf_reg_state *reg = reg_state(env, regno); 5658 + struct bpf_map *map = reg->map_ptr; 5659 5659 u32 cap = bpf_map_flags_to_cap(map); 5660 5660 5661 5661 if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { ··· 6168 6168 static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, 6169 6169 int size, bool zero_size_allowed) 6170 6170 { 6171 - struct bpf_reg_state *regs = cur_regs(env); 6172 - struct bpf_reg_state *reg = &regs[regno]; 6171 + struct bpf_reg_state *reg = reg_state(env, regno); 6173 6172 int err; 6174 6173 6175 6174 /* We may have added a variable offset to the packet pointer; but any ··· 6255 6256 u32 regno, int off, int size, 6256 6257 enum bpf_access_type t) 6257 6258 { 6258 - struct bpf_reg_state *regs = cur_regs(env); 6259 - struct bpf_reg_state *reg = &regs[regno]; 6259 + struct bpf_reg_state *reg = reg_state(env, regno); 6260 6260 struct bpf_insn_access_aux info = {}; 6261 6261 bool valid; 6262 6262 ··· 7451 7453 int regno, int off, int access_size, 7452 7454 enum bpf_access_type type) 7453 7455 { 7454 - struct bpf_reg_state *regs = cur_regs(env); 7455 - struct bpf_reg_state *reg = regs + regno; 7456 + struct bpf_reg_state *reg = reg_state(env, regno); 7456 7457 struct bpf_func_state *state = func(env, reg); 7457 7458 s64 min_off, max_off; 7458 7459 int err; ··· 8405 8408 { 8406 8409 bool is_lock = flags & PROCESS_SPIN_LOCK, is_res_lock = flags & PROCESS_RES_LOCK; 8407 8410 const char *lock_str = is_res_lock ? "bpf_res_spin" : "bpf_spin"; 8408 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 8411 + struct bpf_reg_state *reg = reg_state(env, regno); 8409 8412 struct bpf_verifier_state *cur = env->cur_state; 8410 8413 bool is_const = tnum_is_const(reg->var_off); 8411 8414 bool is_irq = flags & PROCESS_LOCK_IRQ; ··· 8521 8524 static int check_map_field_pointer(struct bpf_verifier_env *env, u32 regno, 8522 8525 enum btf_field_type field_type) 8523 8526 { 8524 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 8527 + struct bpf_reg_state *reg = reg_state(env, regno); 8525 8528 bool is_const = tnum_is_const(reg->var_off); 8526 8529 struct bpf_map *map = reg->map_ptr; 8527 8530 u64 val = reg->var_off.value; ··· 8568 8571 static int process_timer_func(struct bpf_verifier_env *env, int regno, 8569 8572 struct bpf_call_arg_meta *meta) 8570 8573 { 8571 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 8574 + struct bpf_reg_state *reg = reg_state(env, regno); 8572 8575 struct bpf_map *map = reg->map_ptr; 8573 8576 int err; 8574 8577 ··· 8592 8595 static int process_wq_func(struct bpf_verifier_env *env, int regno, 8593 8596 struct bpf_kfunc_call_arg_meta *meta) 8594 8597 { 8595 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 8598 + struct bpf_reg_state *reg = reg_state(env, regno); 8596 8599 struct bpf_map *map = reg->map_ptr; 8597 8600 int err; 8598 8601 ··· 8613 8616 static int process_task_work_func(struct bpf_verifier_env *env, int regno, 8614 8617 struct bpf_kfunc_call_arg_meta *meta) 8615 8618 { 8616 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 8619 + struct bpf_reg_state *reg = reg_state(env, regno); 8617 8620 struct bpf_map *map = reg->map_ptr; 8618 8621 int err; 8619 8622 ··· 8633 8636 static int process_kptr_func(struct bpf_verifier_env *env, int regno, 8634 8637 struct bpf_call_arg_meta *meta) 8635 8638 { 8636 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 8639 + struct bpf_reg_state *reg = reg_state(env, regno); 8637 8640 struct btf_field *kptr_field; 8638 8641 struct bpf_map *map_ptr; 8639 8642 struct btf_record *rec; ··· 8706 8709 static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx, 8707 8710 enum bpf_arg_type arg_type, int clone_ref_obj_id) 8708 8711 { 8709 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 8712 + struct bpf_reg_state *reg = reg_state(env, regno); 8710 8713 int err; 8711 8714 8712 8715 if (reg->type != PTR_TO_STACK && reg->type != CONST_PTR_TO_DYNPTR) { ··· 8826 8829 static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_idx, 8827 8830 struct bpf_kfunc_call_arg_meta *meta) 8828 8831 { 8829 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 8832 + struct bpf_reg_state *reg = reg_state(env, regno); 8830 8833 const struct btf_type *t; 8831 8834 int spi, err, i, nr_slots, btf_id; 8832 8835 ··· 9298 9301 const u32 *arg_btf_id, 9299 9302 struct bpf_call_arg_meta *meta) 9300 9303 { 9301 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 9304 + struct bpf_reg_state *reg = reg_state(env, regno); 9302 9305 enum bpf_reg_type expected, type = reg->type; 9303 9306 const struct bpf_reg_types *compatible; 9304 9307 int i, j; ··· 9711 9714 int insn_idx) 9712 9715 { 9713 9716 u32 regno = BPF_REG_1 + arg; 9714 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 9717 + struct bpf_reg_state *reg = reg_state(env, regno); 9715 9718 enum bpf_arg_type arg_type = fn->arg_type[arg]; 9716 9719 enum bpf_reg_type type = reg->type; 9717 9720 u32 *arg_btf_id = NULL; ··· 11244 11247 int func_id, int insn_idx) 11245 11248 { 11246 11249 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; 11247 - struct bpf_reg_state *regs = cur_regs(env), *reg; 11250 + struct bpf_reg_state *reg; 11248 11251 struct bpf_map *map = meta->map_ptr; 11249 11252 u64 val, max; 11250 11253 int err; ··· 11256 11259 return -EINVAL; 11257 11260 } 11258 11261 11259 - reg = &regs[BPF_REG_3]; 11262 + reg = reg_state(env, BPF_REG_3); 11260 11263 val = reg->var_off.value; 11261 11264 max = map->max_entries; 11262 11265 ··· 11402 11405 11403 11406 static bool loop_flag_is_zero(struct bpf_verifier_env *env) 11404 11407 { 11405 - struct bpf_reg_state *regs = cur_regs(env); 11406 - struct bpf_reg_state *reg = &regs[BPF_REG_4]; 11408 + struct bpf_reg_state *reg = reg_state(env, BPF_REG_4); 11407 11409 bool reg_is_null = register_is_null(reg); 11408 11410 11409 11411 if (reg_is_null) ··· 12664 12668 static int process_irq_flag(struct bpf_verifier_env *env, int regno, 12665 12669 struct bpf_kfunc_call_arg_meta *meta) 12666 12670 { 12667 - struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno]; 12671 + struct bpf_reg_state *reg = reg_state(env, regno); 12668 12672 int err, kfunc_class = IRQ_NATIVE_KFUNC; 12669 12673 bool irq_save; 12670 12674