libbpf: do not use __builtin_offsetof for offsetof
PATCH 1/1 libbpf: do not use __builtin_offsetof for offsetof
Commit 5fbc220862fc ("tools/libpf: Add offsetof/container_of macro in bpf_helpers.h") added a macro offsetof() to get the offset of a structure member:
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
In certain use cases, size_t type may not be available so Commit da7a35062bcc ("libbpf bpf_helpers: Use __builtin_offsetof for offsetof") changed to use __builtin_offsetof which removed the dependency on type size_t, which I suggested.
But using __builtin_offsetof will prevent CO-RE relocation generation in case that, e.g., TYPE is annotated with "preserve_access_info" where a relocation is desirable in case the member offset is changed in a different kernel version. So this patch reverted back to the original macro but using "unsigned long" instead of "site_t".
--- tools/lib/bpf/bpf_helpers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf_helpers.h +1 -1
@@ -40,7 +40,7 @@* Helper macro to manipulate data structures */ #ifndef offsetof -#define offsetof (TYPE ,MEMBER ) __builtin_offsetof(TYPE, MEMBER) +#define offsetof (TYPE ,MEMBER ) ((unsigned long)&((TYPE *)0)->MEMBER)#endif #ifndef container_of#define container_of (ptr, type, member) \
Signature · 1 line
2.24.1
Quoted 17 lines · Yonghong Song
Commit
5fbc220862fc("tools/libpf: Add offsetof/container_of macro inbpf_helpers.h") added a macrooffsetof()to get the offset of a structure member:#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)In certain use cases,
size_ttype may not be available so Commitda7a35062bcc("libbpfbpf_helpers: Use__builtin_offsetoffor offsetof") changed to use__builtin_offsetofwhich removed the dependency on typesize_t, which I suggested.But using
__builtin_offsetofwill prevent CO-RE relocation generation in case that, e.g., TYPE is annotated with "preserve_access_info" where a relocation is desirable in case the member offset is changed in a different kernel version. So this patch reverted back to the original macro but using "unsigned long" instead of "site_t".
Quoted 4 lines
Cc Ian Rogers <[email protected]>Fixes da7a35062bcc ("libbpf bpf_helpers: Use __builtin_offsetof for offsetof")Signed-off-by Yonghong Song <[email protected]>---
LGTM.
Quoted 19 lines
tools/lib/bpf/bpf_helpers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/bpf/bpf_helpers.h +1
-2@@ -40,7 +40,7 @@* Helper macro to manipulate data structures */ #ifndef offsetof -#define offsetof (TYPE ,MEMBER ) __builtin_offsetof(TYPE, MEMBER) +#define offsetof (TYPE ,MEMBER ) ((unsigned long)&((TYPE *)0)->MEMBER)#endif #ifndef container_of#define container_of (ptr, type, member) \ --2.24.1
Ian Rogers
Acked-byQuoted 19 lines · Andrii Nakryiko
Quoted 17 lines · Yonghong Song
Commit
5fbc220862fc("tools/libpf: Add offsetof/container_of macro inbpf_helpers.h") added a macrooffsetof()to get the offset of a structure member:#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)In certain use cases,
size_ttype may not be available so Commitda7a35062bcc("libbpfbpf_helpers: Use__builtin_offsetoffor offsetof") changed to use__builtin_offsetofwhich removed the dependency on typesize_t, which I suggested.But using
__builtin_offsetofwill prevent CO-RE relocation generation in case that, e.g., TYPE is annotated with "preserve_access_info" where a relocation is desirable in case the member offset is changed in a different kernel version. So this patch reverted back to the original macro but using "unsigned long" instead of "site_t".
Quoted 8 lines
Quoted 4 lines
Cc Ian Rogers <[email protected]>Fixes da7a35062bcc ("libbpf bpf_helpers: Use __builtin_offsetof for offsetof")Signed-off-by Yonghong Song <[email protected]>---
LGTM.
Acked-by Andrii Nakryiko <[email protected]>
Thanks,
Ian
Quoted 19 lines
Quoted 19 lines
tools/lib/bpf/bpf_helpers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/lib/bpf/bpf_helpers.h +1
-2@@ -40,7 +40,7 @@* Helper macro to manipulate data structures */ #ifndef offsetof -#define offsetof (TYPE ,MEMBER ) __builtin_offsetof(TYPE, MEMBER) +#define offsetof (TYPE ,MEMBER ) ((unsigned long)&((TYPE *)0)->MEMBER)#endif #ifndef container_of#define container_of (ptr, type, member) \ --2.24.1
Quoted 16 lines · Yonghong Song
Commit
5fbc220862fc("tools/libpf: Add offsetof/container_of macro inbpf_helpers.h") added a macrooffsetof()to get the offset of a structure member:#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)In certain use cases,
size_ttype may not be available so Commitda7a35062bcc("libbpfbpf_helpers: Use__builtin_offsetoffor offsetof") changed to use__builtin_offsetofwhich removed the dependency on typesize_t, which I suggested.But using
__builtin_offsetofwill prevent CO-RE relocation generation in case that, e.g., TYPE is annotated with "preserve_access_info" where a relocation is desirable in case the member offset is changed in a different kernel version. So this patch reverted back to the original macro but using "unsigned long" instead of "site_t".
Quoted 3 lines
Cc Ian Rogers <[email protected]>Fixes da7a35062bcc ("libbpf bpf_helpers: Use __builtin_offsetof for offsetof")Signed-off-by Yonghong Song <[email protected]>
Applied, thanks!