Skip to content

Commit 00b8586

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
bpf: Rewrite kfunc argument handling
As we continue to add more features, argument types, kfunc flags, and different extensions to kfuncs, the code to verify the correctness of the kfunc prototype wrt the passed in registers has become ad-hoc and ugly to read. To make life easier, and make a very clear split between different stages of argument processing, move all the code into verifier.c and refactor into easier to read helpers and functions. This also makes sharing code within the verifier easier with kfunc argument processing. This will be more and more useful in later patches as we are now moving to implement very core BPF helpers as kfuncs, to keep them experimental before baking into UAPI. Remove all kfunc related bits now from btf_check_func_arg_match, as users have been converted away to refactored kfunc argument handling. Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b7ff979 commit 00b8586

File tree

8 files changed

+573
-404
lines changed

8 files changed

+573
-404
lines changed

include/linux/bpf.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,22 +2109,11 @@ int btf_distill_func_proto(struct bpf_verifier_log *log,
21092109
const char *func_name,
21102110
struct btf_func_model *m);
21112111

2112-
struct bpf_kfunc_arg_meta {
2113-
u64 r0_size;
2114-
bool r0_rdonly;
2115-
int ref_obj_id;
2116-
u32 flags;
2117-
};
2118-
21192112
struct bpf_reg_state;
21202113
int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog,
21212114
struct bpf_reg_state *regs);
21222115
int btf_check_subprog_call(struct bpf_verifier_env *env, int subprog,
21232116
struct bpf_reg_state *regs);
2124-
int btf_check_kfunc_arg_match(struct bpf_verifier_env *env,
2125-
const struct btf *btf, u32 func_id,
2126-
struct bpf_reg_state *regs,
2127-
struct bpf_kfunc_arg_meta *meta);
21282117
int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog,
21292118
struct bpf_reg_state *reg);
21302119
int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,

include/linux/bpf_verifier.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,6 @@ int check_ptr_off_reg(struct bpf_verifier_env *env,
603603
int check_func_arg_reg_off(struct bpf_verifier_env *env,
604604
const struct bpf_reg_state *reg, int regno,
605605
enum bpf_arg_type arg_type);
606-
int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
607-
u32 regno);
608606
int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
609607
u32 regno, u32 mem_size);
610608
bool is_dynptr_reg_valid_init(struct bpf_verifier_env *env,

include/linux/btf.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ static inline bool btf_type_is_struct(const struct btf_type *t)
338338
return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
339339
}
340340

341+
static inline bool __btf_type_is_struct(const struct btf_type *t)
342+
{
343+
return BTF_INFO_KIND(t->info) == BTF_KIND_STRUCT;
344+
}
345+
346+
static inline bool btf_type_is_array(const struct btf_type *t)
347+
{
348+
return BTF_INFO_KIND(t->info) == BTF_KIND_ARRAY;
349+
}
350+
341351
static inline u16 btf_type_vlen(const struct btf_type *t)
342352
{
343353
return BTF_INFO_VLEN(t->info);
@@ -439,9 +449,10 @@ static inline void *btf_id_set8_contains(const struct btf_id_set8 *set, u32 id)
439449
return bsearch(&id, set->pairs, set->cnt, sizeof(set->pairs[0]), btf_id_cmp_func);
440450
}
441451

442-
#ifdef CONFIG_BPF_SYSCALL
443452
struct bpf_prog;
453+
struct bpf_verifier_log;
444454

455+
#ifdef CONFIG_BPF_SYSCALL
445456
const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id);
446457
const char *btf_name_by_offset(const struct btf *btf, u32 offset);
447458
struct btf *btf_parse_vmlinux(void);
@@ -455,6 +466,12 @@ s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
455466
int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
456467
struct module *owner);
457468
struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id);
469+
const struct btf_member *
470+
btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
471+
const struct btf_type *t, enum bpf_prog_type prog_type,
472+
int arg);
473+
bool btf_types_are_same(const struct btf *btf1, u32 id1,
474+
const struct btf *btf2, u32 id2);
458475
#else
459476
static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
460477
u32 type_id)
@@ -490,6 +507,18 @@ static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf
490507
{
491508
return NULL;
492509
}
510+
static inline const struct btf_member *
511+
btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
512+
const struct btf_type *t, enum bpf_prog_type prog_type,
513+
int arg)
514+
{
515+
return NULL;
516+
}
517+
static inline bool btf_types_are_same(const struct btf *btf1, u32 id1,
518+
const struct btf *btf2, u32 id2)
519+
{
520+
return false;
521+
}
493522
#endif
494523

495524
static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)

0 commit comments

Comments
 (0)