libbpf: fix offsetof() and container_of() to work with CO-RE
authorAndrii Nakryiko <andrii@kernel.org>
Tue, 9 May 2023 06:55:02 +0000 (23:55 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 19 Jul 2023 14:21:04 +0000 (16:21 +0200)
commit34fe7aa8ef1d159ca6b6f01728788e8b702ac7c5
treea6daf6e070b769161b91b01e1e34fbab9cb13c66
parent8404f8de1e23690524b92dded514dee29160f7ad
libbpf: fix offsetof() and container_of() to work with CO-RE

[ Upstream commit bdeeed3498c7871c17465bb4f11d1bc67f9098af ]

It seems like __builtin_offset() doesn't preserve CO-RE field
relocations properly. So if offsetof() macro is defined through
__builtin_offset(), CO-RE-enabled BPF code using container_of() will be
subtly and silently broken.

To avoid this problem, redefine offsetof() and container_of() in the
form that works with CO-RE relocations more reliably.

Fixes: 5fbc220862fc ("tools/libpf: Add offsetof/container_of macro in bpf_helpers.h")
Reported-by: Lennart Poettering <lennart@poettering.net>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/r/20230509065502.2306180-1-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/lib/bpf/bpf_helpers.h