bpf: Introduce might_sleep field in bpf_func_proto
authorYonghong Song <yhs@fb.com>
Thu, 24 Nov 2022 05:32:11 +0000 (21:32 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 24 Nov 2022 20:27:13 +0000 (12:27 -0800)
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>
include/linux/bpf.h
kernel/bpf/bpf_lsm.c
kernel/bpf/helpers.c
kernel/bpf/verifier.c
kernel/trace/bpf_trace.c

index c9eafa67f2a2a2c2e27e77e32a2ad138a9d0dbe2..43fd7eeeeabba33115c4766ec8ecf0e2b7790100 100644 (file)
@@ -682,6 +682,7 @@ struct bpf_func_proto {
        u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
        bool gpl_only;
        bool pkt_access;
+       bool might_sleep;
        enum bpf_return_type ret_type;
        union {
                struct {
index d6c9b3705f2423f4ded1b649210dd4fbf68d155a..ae0267f150b5f64724df898ccd037a9cbc98b05f 100644 (file)
@@ -151,6 +151,7 @@ BTF_ID_LIST_SINGLE(bpf_ima_inode_hash_btf_ids, struct, inode)
 static const struct bpf_func_proto bpf_ima_inode_hash_proto = {
        .func           = bpf_ima_inode_hash,
        .gpl_only       = false,
+       .might_sleep    = true,
        .ret_type       = RET_INTEGER,
        .arg1_type      = ARG_PTR_TO_BTF_ID,
        .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)
 static const struct bpf_func_proto bpf_ima_file_hash_proto = {
        .func           = bpf_ima_file_hash,
        .gpl_only       = false,
+       .might_sleep    = true,
        .ret_type       = RET_INTEGER,
        .arg1_type      = ARG_PTR_TO_BTF_ID,
        .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)
        case BPF_FUNC_bprm_opts_set:
                return &bpf_bprm_opts_set_proto;
        case BPF_FUNC_ima_inode_hash:
-               return prog->aux->sleepable ? &bpf_ima_inode_hash_proto : NULL;
+               return &bpf_ima_inode_hash_proto;
        case BPF_FUNC_ima_file_hash:
-               return prog->aux->sleepable ? &bpf_ima_file_hash_proto : NULL;
+               return &bpf_ima_file_hash_proto;
        case BPF_FUNC_get_attach_cookie:
                return bpf_prog_has_trampoline(prog) ? &bpf_get_attach_cookie_proto : NULL;
 #ifdef CONFIG_NET
index 2299bb0d89e5742cf57cf358c6c888f361c7c870..9296b654dbd75105b660b2302171545528c2d1af 100644 (file)
@@ -661,6 +661,7 @@ BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size,
 const struct bpf_func_proto bpf_copy_from_user_proto = {
        .func           = bpf_copy_from_user,
        .gpl_only       = false,
+       .might_sleep    = true,
        .ret_type       = RET_INTEGER,
        .arg1_type      = ARG_PTR_TO_UNINIT_MEM,
        .arg2_type      = ARG_CONST_SIZE_OR_ZERO,
@@ -691,6 +692,7 @@ BPF_CALL_5(bpf_copy_from_user_task, void *, dst, u32, size,
 const struct bpf_func_proto bpf_copy_from_user_task_proto = {
        .func           = bpf_copy_from_user_task,
        .gpl_only       = true,
+       .might_sleep    = true,
        .ret_type       = RET_INTEGER,
        .arg1_type      = ARG_PTR_TO_UNINIT_MEM,
        .arg2_type      = ARG_CONST_SIZE_OR_ZERO,
index 9528a066cfa5bd471b0db465c17feb19047c95f0..068cc885903c69a94ff64822fc2cbc5b0cde6e71 100644 (file)
@@ -7516,6 +7516,11 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
                return -EINVAL;
        }
 
+       if (!env->prog->aux->sleepable && fn->might_sleep) {
+               verbose(env, "helper call might sleep in a non-sleepable prog\n");
+               return -EINVAL;
+       }
+
        /* With LD_ABS/IND some JITs save/restore skb from r1. */
        changes_data = bpf_helper_changes_pkt_data(fn->func);
        if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
index 5b9008bc597b22a1cf7486e1b8f8b0a9e0c26b28..3bbd3f0c810c895a1c34d1f90af268e3c90b453a 100644 (file)
@@ -1485,9 +1485,9 @@ bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
        case BPF_FUNC_get_task_stack:
                return &bpf_get_task_stack_proto;
        case BPF_FUNC_copy_from_user:
-               return prog->aux->sleepable ? &bpf_copy_from_user_proto : NULL;
+               return &bpf_copy_from_user_proto;
        case BPF_FUNC_copy_from_user_task:
-               return prog->aux->sleepable ? &bpf_copy_from_user_task_proto : NULL;
+               return &bpf_copy_from_user_task_proto;
        case BPF_FUNC_snprintf_btf:
                return &bpf_snprintf_btf_proto;
        case BPF_FUNC_per_cpu_ptr: