bpf: simplify btf_get_prog_ctx_type() into btf_is_prog_ctx_type()
authorAndrii Nakryiko <andrii@kernel.org>
Mon, 12 Feb 2024 23:32:18 +0000 (15:32 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 14 Feb 2024 02:46:46 +0000 (18:46 -0800)
Return result of btf_get_prog_ctx_type() is never used and callers only
check NULL vs non-NULL case to determine if given type matches expected
PTR_TO_CTX type. So rename function to `btf_is_prog_ctx_type()` and
return a simple true/false. We'll use this simpler interface to handle
kprobe program type's special typedef case in the next patch.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240212233221.2575350-2-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/linux/btf.h
kernel/bpf/btf.c
kernel/bpf/verifier.c

index cb96f6263638816e62ffb3b41684608f6d82f642..f9e56fd12a9fd52d94347da85f7ae5b37f5ef330 100644 (file)
@@ -531,10 +531,9 @@ s32 btf_find_dtor_kfunc(struct btf *btf, u32 btf_id);
 int register_btf_id_dtor_kfuncs(const struct btf_id_dtor_kfunc *dtors, u32 add_cnt,
                                struct module *owner);
 struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf, u32 btf_id);
-const struct btf_type *
-btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
-                     const struct btf_type *t, enum bpf_prog_type prog_type,
-                     int arg);
+bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
+                          const struct btf_type *t, enum bpf_prog_type prog_type,
+                          int arg);
 int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type);
 bool btf_types_are_same(const struct btf *btf1, u32 id1,
                        const struct btf *btf2, u32 id2);
@@ -574,12 +573,12 @@ static inline struct btf_struct_meta *btf_find_struct_meta(const struct btf *btf
 {
        return NULL;
 }
-static inline const struct btf_member *
-btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
-                     const struct btf_type *t, enum bpf_prog_type prog_type,
-                     int arg)
+static inline bool
+btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
+                    const struct btf_type *t, enum bpf_prog_type prog_type,
+                    int arg)
 {
-       return NULL;
+       return false;
 }
 static inline int get_kern_ctx_btf_id(struct bpf_verifier_log *log,
                                      enum bpf_prog_type prog_type) {
index efd9bc274be0d6c2dfc2c85a1dc1384ba40ed2a8..405f95722905257f110173f595374d63bad327ae 100644 (file)
@@ -5694,10 +5694,9 @@ static int find_kern_ctx_type_id(enum bpf_prog_type prog_type)
        return ctx_type->type;
 }
 
-const struct btf_type *
-btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
-                     const struct btf_type *t, enum bpf_prog_type prog_type,
-                     int arg)
+bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
+                         const struct btf_type *t, enum bpf_prog_type prog_type,
+                         int arg)
 {
        const struct btf_type *ctx_type;
        const char *tname, *ctx_tname;
@@ -5711,26 +5710,26 @@ btf_get_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
                 * is not supported yet.
                 * BPF_PROG_TYPE_RAW_TRACEPOINT is fine.
                 */
-               return NULL;
+               return false;
        }
        tname = btf_name_by_offset(btf, t->name_off);
        if (!tname) {
                bpf_log(log, "arg#%d struct doesn't have a name\n", arg);
-               return NULL;
+               return false;
        }
 
        ctx_type = find_canonical_prog_ctx_type(prog_type);
        if (!ctx_type) {
                bpf_log(log, "btf_vmlinux is malformed\n");
                /* should not happen */
-               return NULL;
+               return false;
        }
 again:
        ctx_tname = btf_name_by_offset(btf_vmlinux, ctx_type->name_off);
        if (!ctx_tname) {
                /* should not happen */
                bpf_log(log, "Please fix kernel include/linux/bpf_types.h\n");
-               return NULL;
+               return false;
        }
        /* only compare that prog's ctx type name is the same as
         * kernel expects. No need to compare field by field.
@@ -5740,20 +5739,20 @@ again:
         * { // no fields of skb are ever used }
         */
        if (strcmp(ctx_tname, "__sk_buff") == 0 && strcmp(tname, "sk_buff") == 0)
-               return ctx_type;
+               return true;
        if (strcmp(ctx_tname, "xdp_md") == 0 && strcmp(tname, "xdp_buff") == 0)
-               return ctx_type;
+               return true;
        if (strcmp(ctx_tname, tname)) {
                /* bpf_user_pt_regs_t is a typedef, so resolve it to
                 * underlying struct and check name again
                 */
                if (!btf_type_is_modifier(ctx_type))
-                       return NULL;
+                       return false;
                while (btf_type_is_modifier(ctx_type))
                        ctx_type = btf_type_by_id(btf_vmlinux, ctx_type->type);
                goto again;
        }
-       return ctx_type;
+       return true;
 }
 
 /* forward declarations for arch-specific underlying types of
@@ -5905,7 +5904,7 @@ static int btf_translate_to_vmlinux(struct bpf_verifier_log *log,
                                     enum bpf_prog_type prog_type,
                                     int arg)
 {
-       if (!btf_get_prog_ctx_type(log, btf, t, prog_type, arg))
+       if (!btf_is_prog_ctx_type(log, btf, t, prog_type, arg))
                return -ENOENT;
        return find_kern_ctx_type_id(prog_type);
 }
@@ -7211,7 +7210,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
                if (!btf_type_is_ptr(t))
                        goto skip_pointer;
 
-               if ((tags & ARG_TAG_CTX) || btf_get_prog_ctx_type(log, btf, t, prog_type, i)) {
+               if ((tags & ARG_TAG_CTX) || btf_is_prog_ctx_type(log, btf, t, prog_type, i)) {
                        if (tags & ~ARG_TAG_CTX) {
                                bpf_log(log, "arg#%d has invalid combination of tags\n", i);
                                return -EINVAL;
index 72ca27f496168f38e82ecf57b130b02091cebd2b..aa192dc735a9f12b8c6b037f56ba1b25cc02a5a9 100644 (file)
@@ -11015,7 +11015,7 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
         * type to our caller. When a set of conditions hold in the BTF type of
         * arguments, we resolve it to a known kfunc_ptr_arg_type.
         */
-       if (btf_get_prog_ctx_type(&env->log, meta->btf, t, resolve_prog_type(env->prog), argno))
+       if (btf_is_prog_ctx_type(&env->log, meta->btf, t, resolve_prog_type(env->prog), argno))
                return KF_ARG_PTR_TO_CTX;
 
        if (is_kfunc_arg_alloc_obj(meta->btf, &args[argno]))