libbpf: warn on unexpected __arg_ctx type when rewriting BTF
authorAndrii Nakryiko <andrii@kernel.org>
Thu, 18 Jan 2024 03:31:43 +0000 (19:31 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 18 Jan 2024 04:20:06 +0000 (20:20 -0800)
commit76ec90a996e3c707eb6772510afa36faeba2ecff
treef8a2ed263a3ea89d0307e293ebc57b795c281a86
parent989410cde81959c4033dc287d79b42b6eb04f04f
libbpf: warn on unexpected __arg_ctx type when rewriting BTF

On kernel that don't support arg:ctx tag, before adjusting global
subprog BTF information to match kernel's expected canonical type names,
make sure that types used by user are meaningful, and if not, warn and
don't do BTF adjustments.

This is similar to checks that kernel performs, but narrower in scope,
as only a small subset of BPF program types can be accommodated by
libbpf using canonical type names.

Libbpf unconditionally allows `struct pt_regs *` for perf_event program
types, unlike kernel, which supports that conditionally on architecture.
This is done to keep things simple and not cause unnecessary false
positives. This seems like a minor and harmless deviation, which in
real-world programs will be caught by kernels with arg:ctx tag support
anyways. So KISS principle.

This logic is hard to test (especially on latest kernels), so manual
testing was performed instead. Libbpf emitted the following warning for
perf_event program with wrong context argument type:

  libbpf: prog 'arg_tag_ctx_perf': subprog 'subprog_ctx_tag' arg#0 is expected to be of `struct bpf_perf_event_data *` type

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240118033143.3384355-6-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/lib/bpf/libbpf.c