bpf: Merge "types_are_compat" logic into relo_core.c
authorDaniel Müller <deso@posteo.net>
Thu, 23 Jun 2022 18:29:34 +0000 (18:29 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 24 Jun 2022 21:15:37 +0000 (14:15 -0700)
commitfd75733da2f376c0c8c6513c3cb2ac227082ec5c
treee1a228b16bcea4efcff7a9bdb96b0dfcdf5c6d63
parent2f6d1e0f8ff3cf66b1eef8ed36f6c64d2e679284
bpf: Merge "types_are_compat" logic into relo_core.c

BPF type compatibility checks (bpf_core_types_are_compat()) are
currently duplicated between kernel and user space. That's a historical
artifact more than intentional doing and can lead to subtle bugs where
one implementation is adjusted but another is forgotten.

That happened with the enum64 work, for example, where the libbpf side
was changed (commit 23b2a3a8f63a ("libbpf: Add enum64 relocation
support")) to use the btf_kind_core_compat() helper function but the
kernel side was not (commit 6089fb325cf7 ("bpf: Add btf enum64
support")).

This patch addresses both the duplication issue, by merging both
implementations and moving them into relo_core.c, and fixes the alluded
to kind check (by giving preference to libbpf's already adjusted logic).

For discussion of the topic, please refer to:
https://lore.kernel.org/bpf/CAADnVQKbWR7oarBdewgOBZUPzryhRYvEbkhyPJQHHuxq=0K1gw@mail.gmail.com/T/#mcc99f4a33ad9a322afaf1b9276fb1f0b7add9665

Changelog:
v1 -> v2:
- limited libbpf recursion limit to 32
- changed name to __bpf_core_types_are_compat
- included warning previously present in libbpf version
- merged kernel and user space changes into a single patch

Signed-off-by: Daniel Müller <deso@posteo.net>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220623182934.2582827-1-deso@posteo.net
kernel/bpf/btf.c
tools/lib/bpf/libbpf.c
tools/lib/bpf/relo_core.c
tools/lib/bpf/relo_core.h