Skip to content

Commit 5128164

Browse files
etsalAlexei Starovoitov
authored andcommitted
bpf: Allow bpf_for/bpf_repeat calls while holding a spinlock
Add the bpf_iter_num_* kfuncs called by bpf_for in special_kfunc_list, and allow the calls even while holding a spin lock. Signed-off-by: Emil Tsalapatis (Meta) <[email protected]> Reviewed-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2532608 commit 5128164

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

kernel/bpf/verifier.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11690,6 +11690,9 @@ enum special_kfunc_type {
1169011690
KF_bpf_get_kmem_cache,
1169111691
KF_bpf_local_irq_save,
1169211692
KF_bpf_local_irq_restore,
11693+
KF_bpf_iter_num_new,
11694+
KF_bpf_iter_num_next,
11695+
KF_bpf_iter_num_destroy,
1169311696
};
1169411697

1169511698
BTF_SET_START(special_kfunc_set)
@@ -11765,6 +11768,9 @@ BTF_ID_UNUSED
1176511768
BTF_ID(func, bpf_get_kmem_cache)
1176611769
BTF_ID(func, bpf_local_irq_save)
1176711770
BTF_ID(func, bpf_local_irq_restore)
11771+
BTF_ID(func, bpf_iter_num_new)
11772+
BTF_ID(func, bpf_iter_num_next)
11773+
BTF_ID(func, bpf_iter_num_destroy)
1176811774

1176911775
static bool is_kfunc_ret_null(struct bpf_kfunc_call_arg_meta *meta)
1177011776
{
@@ -12151,12 +12157,24 @@ static bool is_bpf_rbtree_api_kfunc(u32 btf_id)
1215112157
btf_id == special_kfunc_list[KF_bpf_rbtree_first];
1215212158
}
1215312159

12160+
static bool is_bpf_iter_num_api_kfunc(u32 btf_id)
12161+
{
12162+
return btf_id == special_kfunc_list[KF_bpf_iter_num_new] ||
12163+
btf_id == special_kfunc_list[KF_bpf_iter_num_next] ||
12164+
btf_id == special_kfunc_list[KF_bpf_iter_num_destroy];
12165+
}
12166+
1215412167
static bool is_bpf_graph_api_kfunc(u32 btf_id)
1215512168
{
1215612169
return is_bpf_list_api_kfunc(btf_id) || is_bpf_rbtree_api_kfunc(btf_id) ||
1215712170
btf_id == special_kfunc_list[KF_bpf_refcount_acquire_impl];
1215812171
}
1215912172

12173+
static bool kfunc_spin_allowed(u32 btf_id)
12174+
{
12175+
return is_bpf_graph_api_kfunc(btf_id) || is_bpf_iter_num_api_kfunc(btf_id);
12176+
}
12177+
1216012178
static bool is_sync_callback_calling_kfunc(u32 btf_id)
1216112179
{
1216212180
return btf_id == special_kfunc_list[KF_bpf_rbtree_add_impl];
@@ -19048,7 +19066,7 @@ static int do_check(struct bpf_verifier_env *env)
1904819066
if (env->cur_state->active_locks) {
1904919067
if ((insn->src_reg == BPF_REG_0 && insn->imm != BPF_FUNC_spin_unlock) ||
1905019068
(insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
19051-
(insn->off != 0 || !is_bpf_graph_api_kfunc(insn->imm)))) {
19069+
(insn->off != 0 || !kfunc_spin_allowed(insn->imm)))) {
1905219070
verbose(env, "function calls are not allowed while holding a lock\n");
1905319071
return -EINVAL;
1905419072
}

0 commit comments

Comments
 (0)