bpf: Add KF_SPINLOCK_SAFE flag for kfuncs under bpf_spin_lock

Introduce the KF_SPINLOCK_SAFE kfunc metadata flag in BTF so kfuncs may
be explicitly marked as safe to call while holding bpf_spin_lock.

Allow kfuncs defined in kernel modules to be marked with KF_SPINLOCK_SAFE.

Example: BTF_ID_FLAGS(func, $kfunc_name, KF_SPINLOCK_SAFE)

Signed-off-by: Kaitao Cheng <chengkaitao@kylinos.cn>
Acked-by: Leon Hwang <leon.hwang@linux.dev>
Link: https://lore.kernel.org/bpf/20260805153340.34776-2-kaitao.cheng@linux.dev
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
Kaitao Cheng
2026-08-05 23:33:38 +08:00
committed by Kumar Kartikeya Dwivedi
parent 11c1e83671
commit ed3b3093b6
2 changed files with 16 additions and 5 deletions

View File

@@ -79,6 +79,7 @@
#define KF_ARENA_ARG1 (1 << 14) /* kfunc takes an arena pointer as its first argument */
#define KF_ARENA_ARG2 (1 << 15) /* kfunc takes an arena pointer as its second argument */
#define KF_IMPLICIT_ARGS (1 << 16) /* kfunc has implicit arguments supplied by the verifier */
#define KF_SPINLOCK_SAFE (1 << 17) /* kfunc is allowed inside bpf_spin_lock-ed region */
/*
* Tag marking a kernel function as a kfunc. This is meant to minimize the

View File

@@ -11837,11 +11837,21 @@ static bool is_bpf_stream_kfunc(u32 btf_id)
btf_id == special_kfunc_list[KF_bpf_stream_print_stack];
}
static bool kfunc_spin_allowed(u32 btf_id)
static bool kfunc_spin_allowed(struct bpf_verifier_env *env, s32 func_id, s16 offset)
{
return is_bpf_graph_api_kfunc(btf_id) || is_bpf_iter_num_api_kfunc(btf_id) ||
is_bpf_res_spin_lock_kfunc(btf_id) || is_bpf_arena_kfunc(btf_id) ||
is_bpf_stream_kfunc(btf_id);
struct bpf_kfunc_meta kfunc;
int err;
if (is_bpf_graph_api_kfunc(func_id) || is_bpf_iter_num_api_kfunc(func_id) ||
is_bpf_res_spin_lock_kfunc(func_id) || is_bpf_arena_kfunc(func_id) ||
is_bpf_stream_kfunc(func_id))
return true;
err = fetch_kfunc_meta(env, func_id, offset, &kfunc);
if (err || !kfunc.flags)
return false;
return *kfunc.flags & KF_SPINLOCK_SAFE;
}
static bool is_sync_callback_calling_kfunc(u32 btf_id)
@@ -17420,7 +17430,7 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)
insn->imm != BPF_FUNC_spin_unlock &&
insn->imm != BPF_FUNC_kptr_xchg) ||
(insn->src_reg == BPF_PSEUDO_KFUNC_CALL &&
(insn->off != 0 || !kfunc_spin_allowed(insn->imm)))) {
!kfunc_spin_allowed(env, insn->imm, insn->off))) {
verbose(env,
"function calls are not allowed while holding a lock\n");
return -EINVAL;