Skip to content

Commit 7faccdf

Browse files
Martin KaFai LauAlexei Starovoitov
authored andcommitted
bpf: Simplify reg0 marking for the rbtree kfuncs that return a bpf_rb_node pointer
The current rbtree kfunc, bpf_rbtree_{first, remove}, returns the bpf_rb_node pointer. The check_kfunc_call currently checks the kfunc btf_id instead of its return pointer type to decide if it needs to do mark_reg_graph_node(reg0) and ref_set_non_owning(reg0). The later patch will add bpf_rbtree_{root,left,right} that will also return a bpf_rb_node pointer. Instead of adding more kfunc btf_id checks to the "if" case, this patch changes the test to check the kfunc's return type. is_rbtree_node_type() function is added to test if a pointer type is a bpf_rb_node. The callers have already skipped the modifiers of the pointer type. A note on the ref_set_non_owning(), although bpf_rbtree_remove() also returns a bpf_rb_node pointer, the bpf_rbtree_remove() has the KF_ACQUIRE flag. Thus, its reg0 will not become non-owning. Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent b183c01 commit 7faccdf

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

kernel/bpf/verifier.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11987,6 +11987,11 @@ static bool is_kfunc_arg_res_spin_lock(const struct btf *btf, const struct btf_p
1198711987
return __is_kfunc_ptr_arg_type(btf, arg, KF_ARG_RES_SPIN_LOCK_ID);
1198811988
}
1198911989

11990+
static bool is_rbtree_node_type(const struct btf_type *t)
11991+
{
11992+
return t == btf_type_by_id(btf_vmlinux, kf_arg_btf_ids[KF_ARG_RB_NODE_ID]);
11993+
}
11994+
1199011995
static bool is_kfunc_arg_callback(struct bpf_verifier_env *env, const struct btf *btf,
1199111996
const struct btf_param *arg)
1199211997
{
@@ -13750,8 +13755,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1375013755
struct btf_field *field = meta.arg_list_head.field;
1375113756

1375213757
mark_reg_graph_node(regs, BPF_REG_0, &field->graph_root);
13753-
} else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_remove] ||
13754-
meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) {
13758+
} else if (is_rbtree_node_type(ptr_type)) {
1375513759
struct btf_field *field = meta.arg_rbtree_root.field;
1375613760

1375713761
mark_reg_graph_node(regs, BPF_REG_0, &field->graph_root);
@@ -13881,7 +13885,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
1388113885
if (is_kfunc_ret_null(&meta))
1388213886
regs[BPF_REG_0].id = id;
1388313887
regs[BPF_REG_0].ref_obj_id = id;
13884-
} else if (meta.func_id == special_kfunc_list[KF_bpf_rbtree_first]) {
13888+
} else if (is_rbtree_node_type(ptr_type)) {
1388513889
ref_set_non_owning(env, &regs[BPF_REG_0]);
1388613890
}
1388713891

0 commit comments

Comments
 (0)