libbpf: add bpf_core_cast() macro
authorAndrii Nakryiko <andrii@kernel.org>
Tue, 30 Jan 2024 21:20:22 +0000 (13:20 -0800)
committerMartin KaFai Lau <martin.lau@kernel.org>
Tue, 30 Jan 2024 23:55:50 +0000 (15:55 -0800)
commit20d59ee55172fdf6072abf871fa62b2070d6383f
tree943d0e21220a3f83781da27546c470cbd56f71cf
parent4d8ebe1304e99cf6e08e432c23041638d6d1de56
libbpf: add bpf_core_cast() macro

Add bpf_core_cast() macro that wraps bpf_rdonly_cast() kfunc. It's more
ergonomic than kfunc, as it automatically extracts btf_id with
bpf_core_type_id_kernel(), and works with type names. It also casts result
to (T *) pointer. See the definition of the macro, it's self-explanatory.

libbpf declares bpf_rdonly_cast() extern as __weak __ksym and should be
safe to not conflict with other possible declarations in user code.

But we do have a conflict with current BPF selftests that declare their
externs with first argument as `void *obj`, while libbpf opts into more
permissive `const void *obj`. This causes conflict, so we fix up BPF
selftests uses in the same patch.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240130212023.183765-2-andrii@kernel.org
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/lib/bpf/bpf_core_read.h
tools/testing/selftests/bpf/bpf_kfuncs.h
tools/testing/selftests/bpf/progs/sk_storage_omem_uncharge.c
tools/testing/selftests/bpf/progs/type_cast.c