Skip to content

Commit 01685c5

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Introduce might_sleep field in bpf_func_proto
Introduce bpf_func_proto->might_sleep to indicate a particular helper might sleep. This will make later check whether a helper might be sleepable or not easier. Acked-by: Martin KaFai Lau <martin.lau@kernel.org> Signed-off-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/r/20221124053211.2373553-1-yhs@fb.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 5a0f663 commit 01685c5

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ struct bpf_func_proto {
682682
u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
683683
bool gpl_only;
684684
bool pkt_access;
685+
bool might_sleep;
685686
enum bpf_return_type ret_type;
686687
union {
687688
struct {

kernel/bpf/bpf_lsm.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode)
151151
static const struct bpf_func_proto bpf_ima_inode_hash_proto = {
152152
.func = bpf_ima_inode_hash,
153153
.gpl_only = false,
154+
.might_sleep = true,
154155
.ret_type = RET_INTEGER,
155156
.arg1_type = ARG_PTR_TO_BTF_ID,
156157
.arg1_btf_id = &bpf_ima_inode_hash_btf_ids[0],
@@ -169,6 +170,7 @@ BTF_ID_LIST_SINGLE(bpf_ima_file_hash_btf_ids, struct, file)
169170
static const struct bpf_func_proto bpf_ima_file_hash_proto = {
170171
.func = bpf_ima_file_hash,
171172
.gpl_only = false,
173+
.might_sleep = true,
172174
.ret_type = RET_INTEGER,
173175
.arg1_type = ARG_PTR_TO_BTF_ID,
174176
.arg1_btf_id = &bpf_ima_file_hash_btf_ids[0],
@@ -221,9 +223,9 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
221223
case BPF_FUNC_bprm_opts_set:
222224
return &bpf_bprm_opts_set_proto;
223225
case BPF_FUNC_ima_inode_hash:
224-
return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL;
226+
return &bpf_ima_inode_hash_proto;
225227
case BPF_FUNC_ima_file_hash:
226-
return prog->aux->sleepable ? &bpf_ima_file_hash_proto : NULL;
228+
return &bpf_ima_file_hash_proto;
227229
case BPF_FUNC_get_attach_cookie:
228230
return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL;
229231
#ifdef CONFIG_NET

kernel/bpf/helpers.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size,
661661
const struct bpf_func_proto bpf_copy_from_user_proto = {
662662
.func = bpf_copy_from_user,
663663
.gpl_only = false,
664+
.might_sleep = true,
664665
.ret_type = RET_INTEGER,
665666
.arg1_type = ARG_PTR_TO_UNINIT_MEM,
666667
.arg2_type = ARG_CONST_SIZE_OR_ZERO,
@@ -691,6 +692,7 @@ BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size,
691692
const struct bpf_func_proto bpf_copy_from_user_task_proto = {
692693
.func = bpf_copy_from_user_task,
693694
.gpl_only = true,
695+
.might_sleep = true,
694696
.ret_type = RET_INTEGER,
695697
.arg1_type = ARG_PTR_TO_UNINIT_MEM,
696698
.arg2_type = ARG_CONST_SIZE_OR_ZERO,

kernel/bpf/verifier.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7516,6 +7516,11 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
75167516
return -EINVAL;
75177517
}
75187518

7519+
if (!env->prog->aux->sleepable && fn->might_sleep) {
7520+
verbose(env, "helper call might sleep in a non-sleepable prog\n");
7521+
return -EINVAL;
7522+
}
7523+
75197524
/* With LD_ABS/IND some JITs save/restore skb from r1. */
75207525
changes_data = bpf_helper_changes_pkt_data(fn->func);
75217526
if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,9 +1485,9 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
14851485
case BPF_FUNC_get_task_stack:
14861486
return &bpf_get_task_stack_proto;
14871487
case BPF_FUNC_copy_from_user:
1488-
return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
1488+
return &bpf_copy_from_user_proto;
14891489
case BPF_FUNC_copy_from_user_task:
1490-
return prog->aux->sleepable ? &bpf_copy_from_user_task_proto : NULL;
1490+
return &bpf_copy_from_user_task_proto;
14911491
case BPF_FUNC_snprintf_btf:
14921492
return &bpf_snprintf_btf_proto;
14931493
case BPF_FUNC_per_cpu_ptr:

0 commit comments

Comments
 (0)