Lines Matching refs:d
2779 static void btf_dedup_free(struct btf_dedup *d);
2780 static int btf_dedup_prep(struct btf_dedup *d);
2781 static int btf_dedup_strings(struct btf_dedup *d);
2782 static int btf_dedup_prim_types(struct btf_dedup *d);
2783 static int btf_dedup_struct_types(struct btf_dedup *d);
2784 static int btf_dedup_ref_types(struct btf_dedup *d);
2785 static int btf_dedup_compact_types(struct btf_dedup *d);
2786 static int btf_dedup_remap_types(struct btf_dedup *d);
2928 struct btf_dedup *d = btf_dedup_new(btf, btf_ext, opts); in btf__dedup() local
2931 if (IS_ERR(d)) { in btf__dedup()
2932 pr_debug("btf_dedup_new failed: %ld", PTR_ERR(d)); in btf__dedup()
2941 err = btf_dedup_prep(d); in btf__dedup()
2946 err = btf_dedup_strings(d); in btf__dedup()
2951 err = btf_dedup_prim_types(d); in btf__dedup()
2956 err = btf_dedup_struct_types(d); in btf__dedup()
2961 err = btf_dedup_ref_types(d); in btf__dedup()
2966 err = btf_dedup_compact_types(d); in btf__dedup()
2971 err = btf_dedup_remap_types(d); in btf__dedup()
2978 btf_dedup_free(d); in btf__dedup()
3026 #define for_each_dedup_cand(d, node, hash) \ argument
3027 hashmap__for_each_key_entry(d->dedup_table, node, (void *)hash)
3029 static int btf_dedup_table_add(struct btf_dedup *d, long hash, __u32 type_id) in btf_dedup_table_add() argument
3031 return hashmap__append(d->dedup_table, in btf_dedup_table_add()
3035 static int btf_dedup_hypot_map_add(struct btf_dedup *d, in btf_dedup_hypot_map_add() argument
3038 if (d->hypot_cnt == d->hypot_cap) { in btf_dedup_hypot_map_add()
3041 d->hypot_cap += max((size_t)16, d->hypot_cap / 2); in btf_dedup_hypot_map_add()
3042 new_list = libbpf_reallocarray(d->hypot_list, d->hypot_cap, sizeof(__u32)); in btf_dedup_hypot_map_add()
3045 d->hypot_list = new_list; in btf_dedup_hypot_map_add()
3047 d->hypot_list[d->hypot_cnt++] = from_id; in btf_dedup_hypot_map_add()
3048 d->hypot_map[from_id] = to_id; in btf_dedup_hypot_map_add()
3052 static void btf_dedup_clear_hypot_map(struct btf_dedup *d) in btf_dedup_clear_hypot_map() argument
3056 for (i = 0; i < d->hypot_cnt; i++) in btf_dedup_clear_hypot_map()
3057 d->hypot_map[d->hypot_list[i]] = BTF_UNPROCESSED_ID; in btf_dedup_clear_hypot_map()
3058 d->hypot_cnt = 0; in btf_dedup_clear_hypot_map()
3059 d->hypot_adjust_canon = false; in btf_dedup_clear_hypot_map()
3062 static void btf_dedup_free(struct btf_dedup *d) in btf_dedup_free() argument
3064 hashmap__free(d->dedup_table); in btf_dedup_free()
3065 d->dedup_table = NULL; in btf_dedup_free()
3067 free(d->map); in btf_dedup_free()
3068 d->map = NULL; in btf_dedup_free()
3070 free(d->hypot_map); in btf_dedup_free()
3071 d->hypot_map = NULL; in btf_dedup_free()
3073 free(d->hypot_list); in btf_dedup_free()
3074 d->hypot_list = NULL; in btf_dedup_free()
3076 free(d); in btf_dedup_free()
3097 struct btf_dedup *d = calloc(1, sizeof(struct btf_dedup)); in btf_dedup_new() local
3101 if (!d) in btf_dedup_new()
3104 d->opts.dont_resolve_fwds = opts && opts->dont_resolve_fwds; in btf_dedup_new()
3109 d->btf = btf; in btf_dedup_new()
3110 d->btf_ext = btf_ext; in btf_dedup_new()
3112 d->dedup_table = hashmap__new(hash_fn, btf_dedup_equal_fn, NULL); in btf_dedup_new()
3113 if (IS_ERR(d->dedup_table)) { in btf_dedup_new()
3114 err = PTR_ERR(d->dedup_table); in btf_dedup_new()
3115 d->dedup_table = NULL; in btf_dedup_new()
3120 d->map = malloc(sizeof(__u32) * type_cnt); in btf_dedup_new()
3121 if (!d->map) { in btf_dedup_new()
3126 d->map[0] = 0; in btf_dedup_new()
3128 struct btf_type *t = btf_type_by_id(d->btf, i); in btf_dedup_new()
3132 d->map[i] = i; in btf_dedup_new()
3134 d->map[i] = BTF_UNPROCESSED_ID; in btf_dedup_new()
3137 d->hypot_map = malloc(sizeof(__u32) * type_cnt); in btf_dedup_new()
3138 if (!d->hypot_map) { in btf_dedup_new()
3143 d->hypot_map[i] = BTF_UNPROCESSED_ID; in btf_dedup_new()
3147 btf_dedup_free(d); in btf_dedup_new()
3151 return d; in btf_dedup_new()
3158 static int btf_for_each_str_off(struct btf_dedup *d, str_off_visit_fn fn, void *ctx) in btf_for_each_str_off() argument
3162 for (i = 0; i < d->btf->nr_types; i++) { in btf_for_each_str_off()
3163 struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i); in btf_for_each_str_off()
3170 if (!d->btf_ext) in btf_for_each_str_off()
3173 r = btf_ext_visit_str_offs(d->btf_ext, fn, ctx); in btf_for_each_str_off()
3182 struct btf_dedup *d = ctx; in strs_dedup_remap_str_off() local
3188 if (str_off == 0 || str_off < d->btf->start_str_off) in strs_dedup_remap_str_off()
3191 s = btf__str_by_offset(d->btf, str_off); in strs_dedup_remap_str_off()
3192 if (d->btf->base_btf) { in strs_dedup_remap_str_off()
3193 err = btf__find_str(d->btf->base_btf, s); in strs_dedup_remap_str_off()
3202 off = strset__add_str(d->strs_set, s); in strs_dedup_remap_str_off()
3206 *str_off_ptr = d->btf->start_str_off + off; in strs_dedup_remap_str_off()
3221 static int btf_dedup_strings(struct btf_dedup *d) in btf_dedup_strings() argument
3225 if (d->btf->strs_deduped) in btf_dedup_strings()
3228 d->strs_set = strset__new(BTF_MAX_STR_OFFSET, NULL, 0); in btf_dedup_strings()
3229 if (IS_ERR(d->strs_set)) { in btf_dedup_strings()
3230 err = PTR_ERR(d->strs_set); in btf_dedup_strings()
3234 if (!d->btf->base_btf) { in btf_dedup_strings()
3238 err = strset__add_str(d->strs_set, ""); in btf_dedup_strings()
3244 err = btf_for_each_str_off(d, strs_dedup_remap_str_off, d); in btf_dedup_strings()
3249 strset__free(d->btf->strs_set); in btf_dedup_strings()
3250 d->btf->hdr->str_len = strset__data_size(d->strs_set); in btf_dedup_strings()
3251 d->btf->strs_set = d->strs_set; in btf_dedup_strings()
3252 d->strs_set = NULL; in btf_dedup_strings()
3253 d->btf->strs_deduped = true; in btf_dedup_strings()
3257 strset__free(d->strs_set); in btf_dedup_strings()
3258 d->strs_set = NULL; in btf_dedup_strings()
3528 static int btf_dedup_prep(struct btf_dedup *d) in btf_dedup_prep() argument
3534 if (!d->btf->base_btf) in btf_dedup_prep()
3537 for (type_id = 1; type_id < d->btf->start_id; type_id++) { in btf_dedup_prep()
3538 t = btf_type_by_id(d->btf, type_id); in btf_dedup_prep()
3541 d->map[type_id] = type_id; in btf_dedup_prep()
3578 if (btf_dedup_table_add(d, h, type_id)) in btf_dedup_prep()
3591 static int btf_dedup_prim_type(struct btf_dedup *d, __u32 type_id) in btf_dedup_prim_type() argument
3593 struct btf_type *t = btf_type_by_id(d->btf, type_id); in btf_dedup_prim_type()
3618 for_each_dedup_cand(d, hash_entry, h) { in btf_dedup_prim_type()
3620 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
3630 for_each_dedup_cand(d, hash_entry, h) { in btf_dedup_prim_type()
3632 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
3637 if (d->opts.dont_resolve_fwds) in btf_dedup_prim_type()
3646 d->map[cand_id] = type_id; in btf_dedup_prim_type()
3654 for_each_dedup_cand(d, hash_entry, h) { in btf_dedup_prim_type()
3656 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_prim_type()
3668 d->map[type_id] = new_id; in btf_dedup_prim_type()
3669 if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) in btf_dedup_prim_type()
3675 static int btf_dedup_prim_types(struct btf_dedup *d) in btf_dedup_prim_types() argument
3679 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_prim_types()
3680 err = btf_dedup_prim_type(d, d->btf->start_id + i); in btf_dedup_prim_types()
3690 static inline bool is_type_mapped(struct btf_dedup *d, uint32_t type_id) in is_type_mapped() argument
3692 return d->map[type_id] <= BTF_MAX_NR_TYPES; in is_type_mapped()
3700 static inline __u32 resolve_type_id(struct btf_dedup *d, __u32 type_id) in resolve_type_id() argument
3702 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) in resolve_type_id()
3703 type_id = d->map[type_id]; in resolve_type_id()
3711 static uint32_t resolve_fwd_id(struct btf_dedup *d, uint32_t type_id) in resolve_fwd_id() argument
3715 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id))) in resolve_fwd_id()
3718 while (is_type_mapped(d, type_id) && d->map[type_id] != type_id) in resolve_fwd_id()
3719 type_id = d->map[type_id]; in resolve_fwd_id()
3721 if (!btf_is_fwd(btf__type_by_id(d->btf, type_id))) in resolve_fwd_id()
3734 static bool btf_dedup_identical_arrays(struct btf_dedup *d, __u32 id1, __u32 id2) in btf_dedup_identical_arrays() argument
3738 t1 = btf_type_by_id(d->btf, id1); in btf_dedup_identical_arrays()
3739 t2 = btf_type_by_id(d->btf, id2); in btf_dedup_identical_arrays()
3747 static bool btf_dedup_identical_structs(struct btf_dedup *d, __u32 id1, __u32 id2) in btf_dedup_identical_structs() argument
3753 t1 = btf_type_by_id(d->btf, id1); in btf_dedup_identical_structs()
3754 t2 = btf_type_by_id(d->btf, id2); in btf_dedup_identical_structs()
3766 !btf_dedup_identical_arrays(d, m1->type, m2->type) && in btf_dedup_identical_structs()
3767 !btf_dedup_identical_structs(d, m1->type, m2->type)) in btf_dedup_identical_structs()
3866 static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id, in btf_dedup_is_equiv() argument
3877 if (resolve_type_id(d, cand_id) == resolve_type_id(d, canon_id)) in btf_dedup_is_equiv()
3880 canon_id = resolve_fwd_id(d, canon_id); in btf_dedup_is_equiv()
3882 hypot_type_id = d->hypot_map[canon_id]; in btf_dedup_is_equiv()
3894 if (btf_dedup_identical_arrays(d, hypot_type_id, cand_id)) in btf_dedup_is_equiv()
3903 if (btf_dedup_identical_structs(d, hypot_type_id, cand_id)) in btf_dedup_is_equiv()
3908 if (btf_dedup_hypot_map_add(d, canon_id, cand_id)) in btf_dedup_is_equiv()
3911 cand_type = btf_type_by_id(d->btf, cand_id); in btf_dedup_is_equiv()
3912 canon_type = btf_type_by_id(d->btf, canon_id); in btf_dedup_is_equiv()
3920 if (!d->opts.dont_resolve_fwds in btf_dedup_is_equiv()
3933 if (fwd_kind == real_kind && canon_id < d->btf->start_id) in btf_dedup_is_equiv()
3934 d->hypot_adjust_canon = true; in btf_dedup_is_equiv()
3947 if (d->opts.dont_resolve_fwds) in btf_dedup_is_equiv()
3964 return btf_dedup_is_equiv(d, cand_type->type, canon_type->type); in btf_dedup_is_equiv()
3973 eq = btf_dedup_is_equiv(d, cand_arr->index_type, canon_arr->index_type); in btf_dedup_is_equiv()
3976 return btf_dedup_is_equiv(d, cand_arr->type, canon_arr->type); in btf_dedup_is_equiv()
3990 eq = btf_dedup_is_equiv(d, cand_m->type, canon_m->type); in btf_dedup_is_equiv()
4006 eq = btf_dedup_is_equiv(d, cand_type->type, canon_type->type); in btf_dedup_is_equiv()
4013 eq = btf_dedup_is_equiv(d, cand_p->type, canon_p->type); in btf_dedup_is_equiv()
4054 static void btf_dedup_merge_hypot_map(struct btf_dedup *d) in btf_dedup_merge_hypot_map() argument
4061 for (i = 0; i < d->hypot_cnt; i++) { in btf_dedup_merge_hypot_map()
4062 canon_type_id = d->hypot_list[i]; in btf_dedup_merge_hypot_map()
4063 targ_type_id = d->hypot_map[canon_type_id]; in btf_dedup_merge_hypot_map()
4064 t_id = resolve_type_id(d, targ_type_id); in btf_dedup_merge_hypot_map()
4065 c_id = resolve_type_id(d, canon_type_id); in btf_dedup_merge_hypot_map()
4066 t_kind = btf_kind(btf__type_by_id(d->btf, t_id)); in btf_dedup_merge_hypot_map()
4067 c_kind = btf_kind(btf__type_by_id(d->btf, c_id)); in btf_dedup_merge_hypot_map()
4089 d->map[c_id] = t_id; in btf_dedup_merge_hypot_map()
4096 if (d->hypot_adjust_canon) in btf_dedup_merge_hypot_map()
4100 d->map[t_id] = c_id; in btf_dedup_merge_hypot_map()
4104 is_type_mapped(d, c_id) && in btf_dedup_merge_hypot_map()
4105 !is_type_mapped(d, t_id)) { in btf_dedup_merge_hypot_map()
4112 d->map[t_id] = c_id; in btf_dedup_merge_hypot_map()
4139 static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id) in btf_dedup_struct_type() argument
4149 if (d->map[type_id] <= BTF_MAX_NR_TYPES) in btf_dedup_struct_type()
4152 t = btf_type_by_id(d->btf, type_id); in btf_dedup_struct_type()
4159 for_each_dedup_cand(d, hash_entry, h) { in btf_dedup_struct_type()
4173 cand_type = btf_type_by_id(d->btf, cand_id); in btf_dedup_struct_type()
4177 btf_dedup_clear_hypot_map(d); in btf_dedup_struct_type()
4178 eq = btf_dedup_is_equiv(d, type_id, cand_id); in btf_dedup_struct_type()
4183 btf_dedup_merge_hypot_map(d); in btf_dedup_struct_type()
4184 if (d->hypot_adjust_canon) /* not really equivalent */ in btf_dedup_struct_type()
4190 d->map[type_id] = new_id; in btf_dedup_struct_type()
4191 if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) in btf_dedup_struct_type()
4197 static int btf_dedup_struct_types(struct btf_dedup *d) in btf_dedup_struct_types() argument
4201 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_struct_types()
4202 err = btf_dedup_struct_type(d, d->btf->start_id + i); in btf_dedup_struct_types()
4233 static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id) in btf_dedup_ref_type() argument
4242 if (d->map[type_id] == BTF_IN_PROGRESS_ID) in btf_dedup_ref_type()
4244 if (d->map[type_id] <= BTF_MAX_NR_TYPES) in btf_dedup_ref_type()
4245 return resolve_type_id(d, type_id); in btf_dedup_ref_type()
4247 t = btf_type_by_id(d->btf, type_id); in btf_dedup_ref_type()
4248 d->map[type_id] = BTF_IN_PROGRESS_ID; in btf_dedup_ref_type()
4257 ref_type_id = btf_dedup_ref_type(d, t->type); in btf_dedup_ref_type()
4263 for_each_dedup_cand(d, hash_entry, h) { in btf_dedup_ref_type()
4265 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4276 ref_type_id = btf_dedup_ref_type(d, info->type); in btf_dedup_ref_type()
4281 ref_type_id = btf_dedup_ref_type(d, info->index_type); in btf_dedup_ref_type()
4287 for_each_dedup_cand(d, hash_entry, h) { in btf_dedup_ref_type()
4289 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4303 ref_type_id = btf_dedup_ref_type(d, t->type); in btf_dedup_ref_type()
4311 ref_type_id = btf_dedup_ref_type(d, param->type); in btf_dedup_ref_type()
4319 for_each_dedup_cand(d, hash_entry, h) { in btf_dedup_ref_type()
4321 cand = btf_type_by_id(d->btf, cand_id); in btf_dedup_ref_type()
4334 d->map[type_id] = new_id; in btf_dedup_ref_type()
4335 if (type_id == new_id && btf_dedup_table_add(d, h, type_id)) in btf_dedup_ref_type()
4341 static int btf_dedup_ref_types(struct btf_dedup *d) in btf_dedup_ref_types() argument
4345 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_ref_types()
4346 err = btf_dedup_ref_type(d, d->btf->start_id + i); in btf_dedup_ref_types()
4351 hashmap__free(d->dedup_table); in btf_dedup_ref_types()
4352 d->dedup_table = NULL; in btf_dedup_ref_types()
4367 static int btf_dedup_compact_types(struct btf_dedup *d) in btf_dedup_compact_types() argument
4370 __u32 next_type_id = d->btf->start_id; in btf_dedup_compact_types()
4376 d->hypot_map[0] = 0; in btf_dedup_compact_types()
4378 for (id = 1; id < d->btf->start_id; id++) in btf_dedup_compact_types()
4379 d->hypot_map[id] = id; in btf_dedup_compact_types()
4380 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) in btf_dedup_compact_types()
4381 d->hypot_map[id] = BTF_UNPROCESSED_ID; in btf_dedup_compact_types()
4383 p = d->btf->types_data; in btf_dedup_compact_types()
4385 for (i = 0, id = d->btf->start_id; i < d->btf->nr_types; i++, id++) { in btf_dedup_compact_types()
4386 if (d->map[id] != id) in btf_dedup_compact_types()
4389 t = btf__type_by_id(d->btf, id); in btf_dedup_compact_types()
4395 d->hypot_map[id] = next_type_id; in btf_dedup_compact_types()
4396 d->btf->type_offs[next_type_id - d->btf->start_id] = p - d->btf->types_data; in btf_dedup_compact_types()
4402 d->btf->nr_types = next_type_id - d->btf->start_id; in btf_dedup_compact_types()
4403 d->btf->type_offs_cap = d->btf->nr_types; in btf_dedup_compact_types()
4404 d->btf->hdr->type_len = p - d->btf->types_data; in btf_dedup_compact_types()
4405 new_offs = libbpf_reallocarray(d->btf->type_offs, d->btf->type_offs_cap, in btf_dedup_compact_types()
4407 if (d->btf->type_offs_cap && !new_offs) in btf_dedup_compact_types()
4409 d->btf->type_offs = new_offs; in btf_dedup_compact_types()
4410 d->btf->hdr->str_off = d->btf->hdr->type_len; in btf_dedup_compact_types()
4411 d->btf->raw_size = d->btf->hdr->hdr_len + d->btf->hdr->type_len + d->btf->hdr->str_len; in btf_dedup_compact_types()
4423 struct btf_dedup *d = ctx; in btf_dedup_remap_type_id() local
4426 resolved_type_id = resolve_type_id(d, *type_id); in btf_dedup_remap_type_id()
4427 new_type_id = d->hypot_map[resolved_type_id]; in btf_dedup_remap_type_id()
4445 static int btf_dedup_remap_types(struct btf_dedup *d) in btf_dedup_remap_types() argument
4449 for (i = 0; i < d->btf->nr_types; i++) { in btf_dedup_remap_types()
4450 struct btf_type *t = btf_type_by_id(d->btf, d->btf->start_id + i); in btf_dedup_remap_types()
4452 r = btf_type_visit_type_ids(t, btf_dedup_remap_type_id, d); in btf_dedup_remap_types()
4457 if (!d->btf_ext) in btf_dedup_remap_types()
4460 r = btf_ext_visit_type_ids(d->btf_ext, btf_dedup_remap_type_id, d); in btf_dedup_remap_types()