Skip to content

Commit 8e64c38

Browse files
alan-maguireanakryiko
authored andcommitted
libbpf: Add identical pointer detection to btf_dedup_is_equiv()
Recently as a side-effect of commit ac05394 ("compiler.h: introduce TYPEOF_UNQUAL() macro") issues were observed in deduplication between modules and kernel BTF such that a large number of kernel types were not deduplicated so were found in module BTF (task_struct, bpf_prog etc). The root cause appeared to be a failure to dedup struct types, specifically those with members that were pointers with __percpu annotations. The issue in dedup is at the point that we are deduplicating structures, we have not yet deduplicated reference types like pointers. If multiple copies of a pointer point at the same (deduplicated) integer as in this case, we do not see them as identical. Special handling already exists to deal with structures and arrays, so add pointer handling here too. Reported-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Alan Maguire <alan.maguire@oracle.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250429161042.2069678-1-alan.maguire@oracle.com
1 parent 224ee86 commit 8e64c38

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

tools/lib/bpf/btf.c

+16
Original file line numberDiff line numberDiff line change
@@ -4396,6 +4396,19 @@ static bool btf_dedup_identical_structs(struct btf_dedup *d, __u32 id1, __u32 id
43964396
return true;
43974397
}
43984398

4399+
static bool btf_dedup_identical_ptrs(struct btf_dedup *d, __u32 id1, __u32 id2)
4400+
{
4401+
struct btf_type *t1, *t2;
4402+
4403+
t1 = btf_type_by_id(d->btf, id1);
4404+
t2 = btf_type_by_id(d->btf, id2);
4405+
4406+
if (!btf_is_ptr(t1) || !btf_is_ptr(t2))
4407+
return false;
4408+
4409+
return t1->type == t2->type;
4410+
}
4411+
43994412
/*
44004413
* Check equivalence of BTF type graph formed by candidate struct/union (we'll
44014414
* call it "candidate graph" in this description for brevity) to a type graph
@@ -4528,6 +4541,9 @@ static int btf_dedup_is_equiv(struct btf_dedup *d, __u32 cand_id,
45284541
*/
45294542
if (btf_dedup_identical_structs(d, hypot_type_id, cand_id))
45304543
return 1;
4544+
/* A similar case is again observed for PTRs. */
4545+
if (btf_dedup_identical_ptrs(d, hypot_type_id, cand_id))
4546+
return 1;
45314547
return 0;
45324548
}
45334549

0 commit comments

Comments
 (0)