Skip to content

Commit bab08d1

Browse files
ameryhungAlexei Starovoitov
authored andcommitted
bpf: Simplify mark_stack_slot_obj_read() and callers
Rename mark_stack_slot_obj_read() as mark_stack_slots_scratched() and directly call it from functions processing iter, dynptr and irq_flag. Commit 6762e3a ("bpf: simplify liveness to use (callsite, depth) keyed func_instances") has removed the dynamic liveness component in mark_stack_slot_obj_read(). The function effectively only marks stack slots as scratched and always succeed. Therefore, return void, drop the unused bpf_reg_state argument and rename it to mark_stack_slots_scratched() to reflect what it does now. In addition, to prepare for unifying dynptr handling, dynptr_get_spi() will be moved out of mark_dynptr_read(). As mark_dynptr_read() would join mark_iter_read() as a thin wrapper of mark_stack_slots_scratched(), just open code these helpers. Acked-by: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Amery Hung <ameryhung@gmail.com> Link: https://lore.kernel.org/r/20260529014936.2811085-2-ameryhung@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 868d43c commit bab08d1

1 file changed

Lines changed: 21 additions & 48 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3006,50 +3006,13 @@ static int sort_subprogs_topo(struct bpf_verifier_env *env)
30063006
return ret;
30073007
}
30083008

3009-
static int mark_stack_slot_obj_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
3010-
int spi, int nr_slots)
3009+
static void mark_stack_slots_scratched(struct bpf_verifier_env *env,
3010+
int spi, int nr_slots)
30113011
{
30123012
int i;
30133013

30143014
for (i = 0; i < nr_slots; i++)
30153015
mark_stack_slot_scratched(env, spi - i);
3016-
return 0;
3017-
}
3018-
3019-
static int mark_dynptr_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
3020-
{
3021-
int spi;
3022-
3023-
/* For CONST_PTR_TO_DYNPTR, it must have already been done by
3024-
* check_reg_arg in check_helper_call and mark_btf_func_reg_size in
3025-
* check_kfunc_call.
3026-
*/
3027-
if (reg->type == CONST_PTR_TO_DYNPTR)
3028-
return 0;
3029-
spi = dynptr_get_spi(env, reg);
3030-
if (spi < 0)
3031-
return spi;
3032-
/* Caller ensures dynptr is valid and initialized, which means spi is in
3033-
* bounds and spi is the first dynptr slot. Simply mark stack slot as
3034-
* read.
3035-
*/
3036-
return mark_stack_slot_obj_read(env, reg, spi, BPF_DYNPTR_NR_SLOTS);
3037-
}
3038-
3039-
static int mark_iter_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
3040-
int spi, int nr_slots)
3041-
{
3042-
return mark_stack_slot_obj_read(env, reg, spi, nr_slots);
3043-
}
3044-
3045-
static int mark_irq_flag_read(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
3046-
{
3047-
int spi;
3048-
3049-
spi = irq_flag_get_spi(env, reg);
3050-
if (spi < 0)
3051-
return spi;
3052-
return mark_stack_slot_obj_read(env, reg, spi, 1);
30533016
}
30543017

30553018
/* This function is supposed to be used by the following 32-bit optimization
@@ -7261,7 +7224,7 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
72617224
static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno, int insn_idx,
72627225
enum bpf_arg_type arg_type, int clone_ref_obj_id)
72637226
{
7264-
int err;
7227+
int spi, err = 0;
72657228

72667229
if (reg->type != PTR_TO_STACK && reg->type != CONST_PTR_TO_DYNPTR) {
72677230
verbose(env,
@@ -7323,7 +7286,17 @@ static int process_dynptr_func(struct bpf_verifier_env *env, struct bpf_reg_stat
73237286
return -EINVAL;
73247287
}
73257288

7326-
err = mark_dynptr_read(env, reg);
7289+
if (reg->type != CONST_PTR_TO_DYNPTR) {
7290+
spi = dynptr_get_spi(env, reg);
7291+
if (spi < 0)
7292+
return spi;
7293+
7294+
/*
7295+
* For CONST_PTR_TO_DYNPTR, reg is already scratched by check_reg_arg
7296+
* in check_helper_call and mark_btf_func_reg_size in check_kfunc_call.
7297+
*/
7298+
mark_stack_slots_scratched(env, spi, BPF_DYNPTR_NR_SLOTS);
7299+
}
73277300
}
73287301
return err;
73297302
}
@@ -7433,9 +7406,7 @@ static int process_iter_arg(struct bpf_verifier_env *env, struct bpf_reg_state *
74337406
if (spi < 0)
74347407
return spi;
74357408

7436-
err = mark_iter_read(env, reg, spi, nr_slots);
7437-
if (err)
7438-
return err;
7409+
mark_stack_slots_scratched(env, spi, nr_slots);
74397410

74407411
/* remember meta->iter info for process_iter_next_call() */
74417412
meta->iter.spi = spi;
@@ -11399,7 +11370,7 @@ static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env,
1139911370
static int process_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno,
1140011371
struct bpf_kfunc_call_arg_meta *meta)
1140111372
{
11402-
int err, kfunc_class = IRQ_NATIVE_KFUNC;
11373+
int err, spi, kfunc_class = IRQ_NATIVE_KFUNC;
1140311374
bool irq_save;
1140411375

1140511376
if (meta->func_id == special_kfunc_list[KF_bpf_local_irq_save] ||
@@ -11440,9 +11411,11 @@ static int process_irq_flag(struct bpf_verifier_env *env, struct bpf_reg_state *
1144011411
return err;
1144111412
}
1144211413

11443-
err = mark_irq_flag_read(env, reg);
11444-
if (err)
11445-
return err;
11414+
spi = irq_flag_get_spi(env, reg);
11415+
if (spi < 0)
11416+
return spi;
11417+
11418+
mark_stack_slots_scratched(env, spi, 1);
1144611419

1144711420
err = unmark_stack_slot_irq_flag(env, reg, kfunc_class);
1144811421
if (err)

0 commit comments

Comments
 (0)