}
 #define SECURITY_PREFIX "security_"
 
-static int check_attach_modify_return(struct bpf_prog *prog, unsigned long addr)
+static int check_attach_modify_return(unsigned long addr, const char *func_name)
 {
        if (within_error_injection_list(addr) ||
-           !strncmp(SECURITY_PREFIX, prog->aux->attach_func_name,
-                    sizeof(SECURITY_PREFIX) - 1))
+           !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1))
                return 0;
 
        return -EINVAL;
        return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id);
 }
 
-static int check_attach_btf_id(struct bpf_verifier_env *env)
+int bpf_check_attach_target(struct bpf_verifier_log *log,
+                           const struct bpf_prog *prog,
+                           const struct bpf_prog *tgt_prog,
+                           u32 btf_id,
+                           struct bpf_attach_target_info *tgt_info)
 {
-       struct bpf_prog *prog = env->prog;
        bool prog_extension = prog->type == BPF_PROG_TYPE_EXT;
-       struct bpf_prog *tgt_prog = prog->aux->linked_prog;
-       struct bpf_verifier_log *log = &env->log;
-       u32 btf_id = prog->aux->attach_btf_id;
        const char prefix[] = "btf_trace_";
-       struct btf_func_model fmodel;
        int ret = 0, subprog = -1, i;
-       struct bpf_trampoline *tr;
        const struct btf_type *t;
        bool conservative = true;
        const char *tname;
        struct btf *btf;
-       long addr;
-       u64 key;
-
-       if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING &&
-           prog->type != BPF_PROG_TYPE_LSM) {
-               verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n");
-               return -EINVAL;
-       }
-
-       if (prog->type == BPF_PROG_TYPE_STRUCT_OPS)
-               return check_struct_ops_btf_id(env);
-
-       if (prog->type != BPF_PROG_TYPE_TRACING &&
-           prog->type != BPF_PROG_TYPE_LSM &&
-           !prog_extension)
-               return 0;
+       long addr = 0;
 
        if (!btf_id) {
                bpf_log(log, "Tracing programs must provide btf_id\n");
                return -EINVAL;
        }
-       btf = bpf_prog_get_target_btf(prog);
+       btf = tgt_prog ? tgt_prog->aux->btf : btf_vmlinux;
        if (!btf) {
                bpf_log(log,
                        "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n");
                                        "Extension programs should be JITed\n");
                                return -EINVAL;
                        }
-                       env->ops = bpf_verifier_ops[tgt_prog->type];
-                       prog->expected_attach_type = tgt_prog->expected_attach_type;
                }
                if (!tgt_prog->jited) {
                        bpf_log(log, "Can attach to only JITed progs\n");
                        bpf_log(log, "Cannot extend fentry/fexit\n");
                        return -EINVAL;
                }
-               key = ((u64)aux->id) << 32 | btf_id;
        } else {
                if (prog_extension) {
                        bpf_log(log, "Cannot replace kernel functions\n");
                        return -EINVAL;
                }
-               key = btf_id;
        }
 
        switch (prog->expected_attach_type) {
                        /* should never happen in valid vmlinux build */
                        return -EINVAL;
 
-               /* remember two read only pointers that are valid for
-                * the life time of the kernel
-                */
-               prog->aux->attach_func_name = tname;
-               prog->aux->attach_func_proto = t;
-               prog->aux->attach_btf_trace = true;
-               return 0;
+               break;
        case BPF_TRACE_ITER:
                if (!btf_type_is_func(t)) {
                        bpf_log(log, "attach_btf_id %u is not a function\n",
                t = btf_type_by_id(btf, t->type);
                if (!btf_type_is_func_proto(t))
                        return -EINVAL;
-               prog->aux->attach_func_name = tname;
-               prog->aux->attach_func_proto = t;
-               if (!bpf_iter_prog_supported(prog))
-                       return -EINVAL;
-               ret = btf_distill_func_proto(log, btf, t, tname, &fmodel);
-               return ret;
+               ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel);
+               if (ret)
+                       return ret;
+               break;
        default:
                if (!prog_extension)
                        return -EINVAL;
        case BPF_LSM_MAC:
        case BPF_TRACE_FENTRY:
        case BPF_TRACE_FEXIT:
-               prog->aux->attach_func_name = tname;
-               if (prog->type == BPF_PROG_TYPE_LSM) {
-                       ret = bpf_lsm_verify_prog(log, prog);
-                       if (ret < 0)
-                               return ret;
-               }
-
                if (!btf_type_is_func(t)) {
                        bpf_log(log, "attach_btf_id %u is not a function\n",
                                btf_id);
                t = btf_type_by_id(btf, t->type);
                if (!btf_type_is_func_proto(t))
                        return -EINVAL;
-               tr = bpf_trampoline_lookup(key);
-               if (!tr)
-                       return -ENOMEM;
-               /* t is either vmlinux type or another program's type */
-               prog->aux->attach_func_proto = t;
-               mutex_lock(&tr->mutex);
-               if (tr->func.addr) {
-                       prog->aux->trampoline = tr;
-                       goto out;
-               }
-               if (tgt_prog && conservative) {
-                       prog->aux->attach_func_proto = NULL;
+
+               if (tgt_prog && conservative)
                        t = NULL;
-               }
-               ret = btf_distill_func_proto(log, btf, t,
-                                            tname, &tr->func.model);
+
+               ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel);
                if (ret < 0)
-                       goto out;
+                       return ret;
+
                if (tgt_prog) {
                        if (subprog == 0)
                                addr = (long) tgt_prog->bpf_func;
                                bpf_log(log,
                                        "The address of function %s cannot be found\n",
                                        tname);
-                               ret = -ENOENT;
-                               goto out;
+                               return -ENOENT;
                        }
                }
 
                        default:
                                break;
                        }
-                       if (ret)
-                               bpf_log(log, "%s is not sleepable\n",
-                                       prog->aux->attach_func_name);
+                       if (ret) {
+                               bpf_log(log, "%s is not sleepable\n", tname);
+                               return ret;
+                       }
                } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) {
                        if (tgt_prog) {
                                bpf_log(log, "can't modify return codes of BPF programs\n");
-                               ret = -EINVAL;
-                               goto out;
+                               return -EINVAL;
+                       }
+                       ret = check_attach_modify_return(addr, tname);
+                       if (ret) {
+                               bpf_log(log, "%s() is not modifiable\n", tname);
+                               return ret;
                        }
-                       ret = check_attach_modify_return(prog, addr);
-                       if (ret)
-                               bpf_log(log, "%s() is not modifiable\n",
-                                       prog->aux->attach_func_name);
                }
-               if (ret)
-                       goto out;
-               tr->func.addr = (void *)addr;
-               prog->aux->trampoline = tr;
-out:
-               mutex_unlock(&tr->mutex);
-               if (ret)
-                       bpf_trampoline_put(tr);
+
+               break;
+       }
+       tgt_info->tgt_addr = addr;
+       tgt_info->tgt_name = tname;
+       tgt_info->tgt_type = t;
+       return 0;
+}
+
+static int check_attach_btf_id(struct bpf_verifier_env *env)
+{
+       struct bpf_prog *prog = env->prog;
+       struct bpf_prog *tgt_prog = prog->aux->linked_prog;
+       struct bpf_attach_target_info tgt_info = {};
+       u32 btf_id = prog->aux->attach_btf_id;
+       struct bpf_trampoline *tr;
+       int ret;
+       u64 key;
+
+       if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING &&
+           prog->type != BPF_PROG_TYPE_LSM) {
+               verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n");
+               return -EINVAL;
+       }
+
+       if (prog->type == BPF_PROG_TYPE_STRUCT_OPS)
+               return check_struct_ops_btf_id(env);
+
+       if (prog->type != BPF_PROG_TYPE_TRACING &&
+           prog->type != BPF_PROG_TYPE_LSM &&
+           prog->type != BPF_PROG_TYPE_EXT)
+               return 0;
+
+       ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info);
+       if (ret)
                return ret;
+
+       if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) {
+               env->ops = bpf_verifier_ops[tgt_prog->type];
+               prog->expected_attach_type = tgt_prog->expected_attach_type;
+       }
+
+       /* store info about the attachment target that will be used later */
+       prog->aux->attach_func_proto = tgt_info.tgt_type;
+       prog->aux->attach_func_name = tgt_info.tgt_name;
+
+       if (prog->expected_attach_type == BPF_TRACE_RAW_TP) {
+               prog->aux->attach_btf_trace = true;
+               return 0;
+       } else if (prog->expected_attach_type == BPF_TRACE_ITER) {
+               if (!bpf_iter_prog_supported(prog))
+                       return -EINVAL;
+               return 0;
+       }
+
+       if (prog->type == BPF_PROG_TYPE_LSM) {
+               ret = bpf_lsm_verify_prog(&env->log, prog);
+               if (ret < 0)
+                       return ret;
        }
+
+       key = bpf_trampoline_compute_key(tgt_prog, btf_id);
+       tr = bpf_trampoline_get(key, &tgt_info);
+       if (!tr)
+               return -ENOMEM;
+
+       prog->aux->trampoline = tr;
+       return 0;
 }
 
 int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,