Skip to content

Commit f3c51fe

Browse files
alan-maguireanakryiko
authored andcommitted
libbpf: Btf dedup identical struct test needs check for nested structs/arrays
When examining module BTF, it is common to see core kernel structures such as sk_buff, net_device duplicated in the module. After adding debug messaging to BTF it turned out that much of the problem was down to the identical struct test failing during deduplication; sometimes the compiler adds identical structs. However it turns out sometimes that type ids of identical struct members can also differ, even when the containing structs are still identical. To take an example, for struct sk_buff, debug messaging revealed that the identical struct matching was failing for the anon struct "headers"; specifically for the first field: __u8 __pkt_type_offset[0]; /* 128 0 */ Looking at the code in BTF deduplication, we have code that guards against the possibility of identical struct definitions, down to type ids, and identical array definitions. However in this case we have a struct which is being defined twice but does not have identical type ids since each duplicate struct has separate type ids for the above array member. A similar problem (though not observed) could occur for struct-in-struct. The solution is to make the "identical struct" test check members not just for matching ids, but to also check if they in turn are identical structs or arrays. The results of doing this are quite dramatic (for some modules at least); I see the number of type ids drop from around 10000 to just over 1000 in one module for example. For testing use latest pahole or apply [1], otherwise dedups can fail for the reasons described there. Also fix return type of btf_dedup_identical_arrays() as suggested by Andrii to match boolean return type used elsewhere. Fixes: efdd3eb ("libbpf: Accommodate DWARF/compiler bug with duplicated structs") Signed-off-by: Alan Maguire <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] [1] https://lore.kernel.org/bpf/1666364523-9648-1-git-send-email-alan.maguire
1 parent 31af1aa commit f3c51fe

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

tools/lib/bpf/btf.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3887,14 +3887,14 @@ static inline __u16 btf_fwd_kind(struct btf_type *t)
38873887
}
38883888

38893889
/* Check if given two types are identical ARRAY definitions */
3890-
static int btf_dedup_identical_arrays(struct btf_dedup *d, __u32 id1, __u32 id2)
3890+
static bool btf_dedup_identical_arrays(struct btf_dedup *d, __u32 id1, __u32 id2)
38913891
{
38923892
struct btf_type *t1, *t2;
38933893

38943894
t1 = btf_type_by_id(d->btf, id1);
38953895
t2 = btf_type_by_id(d->btf, id2);
38963896
if (!btf_is_array(t1) || !btf_is_array(t2))
3897-
return 0;
3897+
return false;
38983898

38993899
return btf_equal_array(t1, t2);
39003900
}
@@ -3918,7 +3918,9 @@ static bool btf_dedup_identical_structs(struct btf_dedup *d, __u32 id1, __u32 id
39183918
m1 = btf_members(t1);
39193919
m2 = btf_members(t2);
39203920
for (i = 0, n = btf_vlen(t1); i < n; i++, m1++, m2++) {
3921-
if (m1->type != m2->type)
3921+
if (m1->type != m2->type &&
3922+
!btf_dedup_identical_arrays(d, m1->type, m2->type) &&
3923+
!btf_dedup_identical_structs(d, m1->type, m2->type))
39223924
return false;
39233925
}
39243926
return true;

0 commit comments

Comments
 (0)