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

bpf: Refactor mark_{dynptr,iter}_read

There is possibility of sharing code between mark_dynptr_read and
mark_iter_read for updating liveness information of their stack slots.
Consolidate common logic into mark_stack_slot_obj_read function in
preparation for the next patch which needs the same logic for its own
stack slots.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20241204030400.208005-4-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Kumar Kartikeya Dwivedi and committed by
Alexei Starovoitov
b79f5f54 769b0f1c

+21 -22
+21 -22
kernel/bpf/verifier.c
··· 3191 3191 return 0; 3192 3192 } 3193 3193 3194 - static int mark_dynptr_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg) 3194 + static int mark_stack_slot_obj_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg, 3195 + int spi, int nr_slots) 3195 3196 { 3196 3197 struct bpf_func_state *state = func(env, reg); 3197 - int spi, ret; 3198 + int err, i; 3199 + 3200 + for (i = 0; i < nr_slots; i++) { 3201 + struct bpf_reg_state *st = &state->stack[spi - i].spilled_ptr; 3202 + 3203 + err = mark_reg_read(env, st, st->parent, REG_LIVE_READ64); 3204 + if (err) 3205 + return err; 3206 + 3207 + mark_stack_slot_scratched(env, spi - i); 3208 + } 3209 + return 0; 3210 + } 3211 + 3212 + static int mark_dynptr_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg) 3213 + { 3214 + int spi; 3198 3215 3199 3216 /* For CONST_PTR_TO_DYNPTR, it must have already been done by 3200 3217 * check_reg_arg in check_helper_call and mark_btf_func_reg_size in ··· 3226 3209 * bounds and spi is the first dynptr slot. Simply mark stack slot as 3227 3210 * read. 3228 3211 */ 3229 - ret = mark_reg_read(env, &state->stack[spi].spilled_ptr, 3230 - state->stack[spi].spilled_ptr.parent, REG_LIVE_READ64); 3231 - if (ret) 3232 - return ret; 3233 - return mark_reg_read(env, &state->stack[spi - 1].spilled_ptr, 3234 - state->stack[spi - 1].spilled_ptr.parent, REG_LIVE_READ64); 3212 + return mark_stack_slot_obj_read(env, reg, spi, BPF_DYNPTR_NR_SLOTS); 3235 3213 } 3236 3214 3237 3215 static int mark_iter_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg, 3238 3216 int spi, int nr_slots) 3239 3217 { 3240 - struct bpf_func_state *state = func(env, reg); 3241 - int err, i; 3242 - 3243 - for (i = 0; i < nr_slots; i++) { 3244 - struct bpf_reg_state *st = &state->stack[spi - i].spilled_ptr; 3245 - 3246 - err = mark_reg_read(env, st, st->parent, REG_LIVE_READ64); 3247 - if (err) 3248 - return err; 3249 - 3250 - mark_stack_slot_scratched(env, spi - i); 3251 - } 3252 - 3253 - return 0; 3218 + return mark_stack_slot_obj_read(env, reg, spi, nr_slots); 3254 3219 } 3255 3220 3256 3221 /* This function is supposed to be used by the following 32-bit optimization