Skip to contentSkip to conversation

libbpf bpf_helpers: Use __builtin_offsetof for offsetof if available

PATCH Superseded Ian Rogers 3 participants 3 messages Jul 17, 2020 – Jul 19, 2020

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 libbpf bpf_helpers: Use __builtin_offsetof for offsetof if available

The non-builtin route for offsetof has a dependency on size_t from stdlib.h/stdint.h that is undeclared and may break targets. The offsetof macro in bpf_helpers may disable the same macro in other headers that have a #ifdef offsetof guard. Rather than add additional dependencies improve the offsetof macro declared here to use the builtin if available.

---
 tools/lib/bpf/bpf_helpers.h | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/bpf_helpers.h +4 -0
index a510d8ed716f..ed2ac74fc515 100644
@@ -40,8 +40,12 @@
  * Helper macro to manipulate data structures
  */
 #ifndef offsetof
+#if __has_builtin(__builtin_offsetof)
+#define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)
+#else
 #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
 #endif
+#endif
 #ifndef container_of
 #define container_of(ptr, type, member)				\
 	({							\
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Ian Rogers

The non-builtin route for offsetof has a dependency on size_t from stdlib.h/stdint.h that is undeclared and may break targets. The offsetof macro in bpf_helpers may disable the same macro in other headers that have a #ifdef offsetof guard. Rather than add additional dependencies improve the offsetof macro declared here to use the builtin if available.

Thanks. I didn't pay attention to __builtin_offsetof() and clearly it is preferred. Ack the change with a nit below.

Quoted 9 lines
---
  tools/lib/bpf/bpf_helpers.h | 4 ++++
  1 file changed, 4 insertions(+)
tools/lib/bpf/bpf_helpers.h +0 -0
index a510d8ed716f..ed2ac74fc515 100644

tools/lib/bpf/bpf_helpers.h line 44

Quoted 5 lines
tools/lib/bpf/bpf_helpers.h +2 -0
@@ -40,8 +40,12 @@
   * Helper macro to manipulate data structures
   */
  #ifndef offsetof
+#if __has_builtin(__builtin_offsetof)
+#define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)

The __builtin_offsetof is actually available at least since llvm 3.7, so it is safe to use it always.

Quoted 8 lines
tools/lib/bpf/bpf_helpers.h +2 -0
+#else
  #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
  #endif
+#endif
  #ifndef container_of
  #define container_of(ptr, type, member)				\
  	({							\
#2 lore raw
In reply to Ian Rogers
Quoted 18 lines · Ian Rogers

The non-builtin route for offsetof has a dependency on size_t from stdlib.h/stdint.h that is undeclared and may break targets. The offsetof macro in bpf_helpers may disable the same macro in other headers that have a #ifdef offsetof guard. Rather than add additional dependencies improve the offsetof macro declared here to use the builtin if available.

---
 tools/lib/bpf/bpf_helpers.h | 4 ++++
 1 file changed, 4 insertions(+)
tools/lib/bpf/bpf_helpers.h +0 -0
index a510d8ed716f..ed2ac74fc515 100644

tools/lib/bpf/bpf_helpers.h line 46

Quoted 7 lines
tools/lib/bpf/bpf_helpers.h +3 -0
@@ -40,8 +40,12 @@
  * Helper macro to manipulate data structures
  */
 #ifndef offsetof
+#if __has_builtin(__builtin_offsetof)
+#define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)
+#else
 #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)

Let's either always use __builtin_offsetof (as Yonghong mentioned, it should always be available on relevant LLVM versions). Or instead of size_t, just cast to (unsigned long), I think it will have absolutely the same effect as size_t casting?

Quoted 8 lines
tools/lib/bpf/bpf_helpers.h +1 -1
 #endif
+#endif
 #ifndef container_of
 #define container_of(ptr, type, member)                                \
        ({                                                      \
--

2.28.0.rc0.105.gf9edc3c819-goog