@@ -3901,6 +3901,20 @@ static int btf_dedup_strings(struct btf_dedup *d)
39013901 return err ;
39023902}
39033903
3904+ /*
3905+ * Calculate type signature hash of TYPEDEF, ignoring referenced type IDs,
3906+ * as referenced type IDs equivalence is established separately during type
3907+ * graph equivalence check algorithm.
3908+ */
3909+ static long btf_hash_typedef (struct btf_type * t )
3910+ {
3911+ long h ;
3912+
3913+ h = hash_combine (0 , t -> name_off );
3914+ h = hash_combine (h , t -> info );
3915+ return h ;
3916+ }
3917+
39043918static long btf_hash_common (struct btf_type * t )
39053919{
39063920 long h ;
@@ -3918,6 +3932,13 @@ static bool btf_equal_common(struct btf_type *t1, struct btf_type *t2)
39183932 t1 -> size == t2 -> size ;
39193933}
39203934
3935+ /* Check structural compatibility of two TYPEDEF. */
3936+ static bool btf_equal_typedef (struct btf_type * t1 , struct btf_type * t2 )
3937+ {
3938+ return t1 -> name_off == t2 -> name_off &&
3939+ t1 -> info == t2 -> info ;
3940+ }
3941+
39213942/* Calculate type signature hash of INT or TAG. */
39223943static long btf_hash_int_decl_tag (struct btf_type * t )
39233944{
@@ -4844,13 +4865,30 @@ static void btf_dedup_merge_hypot_map(struct btf_dedup *d)
48444865 }
48454866}
48464867
4868+ static inline long btf_hash_by_kind (struct btf_type * t , __u16 kind )
4869+ {
4870+ if (kind == BTF_KIND_TYPEDEF )
4871+ return btf_hash_typedef (t );
4872+ else
4873+ return btf_hash_struct (t );
4874+ }
4875+
4876+ static inline bool btf_equal_by_kind (struct btf_type * t1 , struct btf_type * t2 , __u16 kind )
4877+ {
4878+ if (kind == BTF_KIND_TYPEDEF )
4879+ return btf_equal_typedef (t1 , t2 );
4880+ else
4881+ return btf_shallow_equal_struct (t1 , t2 );
4882+ }
4883+
48474884/*
4848- * Deduplicate struct/union types.
4885+ * Deduplicate struct/union and typedef types.
48494886 *
48504887 * For each struct/union type its type signature hash is calculated, taking
48514888 * into account type's name, size, number, order and names of fields, but
48524889 * ignoring type ID's referenced from fields, because they might not be deduped
4853- * completely until after reference types deduplication phase. This type hash
4890+ * completely until after reference types deduplication phase. For each typedef
4891+ * type, the hash is computed based on the type’s name and size. This type hash
48544892 * is used to iterate over all potential canonical types, sharing same hash.
48554893 * For each canonical candidate we check whether type graphs that they form
48564894 * (through referenced types in fields and so on) are equivalent using algorithm
@@ -4882,26 +4920,28 @@ static int btf_dedup_struct_type(struct btf_dedup *d, __u32 type_id)
48824920 t = btf_type_by_id (d -> btf , type_id );
48834921 kind = btf_kind (t );
48844922
4885- if (kind != BTF_KIND_STRUCT && kind != BTF_KIND_UNION )
4923+ if (kind != BTF_KIND_STRUCT &&
4924+ kind != BTF_KIND_UNION &&
4925+ kind != BTF_KIND_TYPEDEF )
48864926 return 0 ;
48874927
4888- h = btf_hash_struct ( t );
4928+ h = btf_hash_by_kind ( t , kind );
48894929 for_each_dedup_cand (d , hash_entry , h ) {
48904930 __u32 cand_id = hash_entry -> value ;
48914931 int eq ;
48924932
48934933 /*
48944934 * Even though btf_dedup_is_equiv() checks for
4895- * btf_shallow_equal_struct () internally when checking two
4896- * structs (unions) for equivalence, we need to guard here
4935+ * btf_equal_by_kind () internally when checking two
4936+ * structs (unions) or typedefs for equivalence, we need to guard here
48974937 * from picking matching FWD type as a dedup candidate.
48984938 * This can happen due to hash collision. In such case just
48994939 * relying on btf_dedup_is_equiv() would lead to potentially
49004940 * creating a loop (FWD -> STRUCT and STRUCT -> FWD), because
49014941 * FWD and compatible STRUCT/UNION are considered equivalent.
49024942 */
49034943 cand_type = btf_type_by_id (d -> btf , cand_id );
4904- if (!btf_shallow_equal_struct (t , cand_type ))
4944+ if (!btf_equal_by_kind (t , cand_type , kind ))
49054945 continue ;
49064946
49074947 btf_dedup_clear_hypot_map (d );
@@ -4939,18 +4979,18 @@ static int btf_dedup_struct_types(struct btf_dedup *d)
49394979/*
49404980 * Deduplicate reference type.
49414981 *
4942- * Once all primitive and struct/union types got deduplicated, we can easily
4982+ * Once all primitive, struct/union and typedef types got deduplicated, we can easily
49434983 * deduplicate all other (reference) BTF types. This is done in two steps:
49444984 *
49454985 * 1. Resolve all referenced type IDs into their canonical type IDs. This
4946- * resolution can be done either immediately for primitive or struct/union types
4947- * (because they were deduped in previous two phases) or recursively for
4986+ * resolution can be done either immediately for primitive, struct/union, and typedef
4987+ * types (because they were deduped in previous two phases) or recursively for
49484988 * reference types. Recursion will always terminate at either primitive or
4949- * struct/union type , at which point we can "unwind" chain of reference types
4950- * one by one. There is no danger of encountering cycles because in C type
4951- * system the only way to form type cycle is through struct/ union, so any chain
4952- * of reference types, even those taking part in a type cycle, will inevitably
4953- * reach struct/union at some point.
4989+ * struct/union and typedef types , at which point we can "unwind" chain of reference
4990+ * types one by one. There is no danger of encountering cycles in C, as the only way to
4991+ * form a type cycle is through struct or union types. Go can form such cycles through
4992+ * typedef. Thus, any chain of reference types, even those taking part in a type cycle,
4993+ * will inevitably reach a struct/union or typedef type at some point.
49544994 *
49554995 * 2. Once all referenced type IDs are resolved into canonical ones, BTF type
49564996 * becomes "stable", in the sense that no further deduplication will cause
@@ -4982,7 +5022,6 @@ static int btf_dedup_ref_type(struct btf_dedup *d, __u32 type_id)
49825022 case BTF_KIND_VOLATILE :
49835023 case BTF_KIND_RESTRICT :
49845024 case BTF_KIND_PTR :
4985- case BTF_KIND_TYPEDEF :
49865025 case BTF_KIND_FUNC :
49875026 case BTF_KIND_TYPE_TAG :
49885027 ref_type_id = btf_dedup_ref_type (d , t -> type );
0 commit comments