linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libbpf bpf_helpers: Use __builtin_offsetof for offsetof if available
@ 2020-07-17  7:23 Ian Rogers
  2020-07-18  6:00 ` Yonghong Song
  2020-07-19  3:52 ` Andrii Nakryiko
  0 siblings, 2 replies; 3+ messages in thread
From: Ian Rogers @ 2020-07-17  7:23 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, Andrii Nakryiko, John Fastabend, KP Singh, netdev,
	bpf, linux-kernel
  Cc: Stanislav Fomichev, 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.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/lib/bpf/bpf_helpers.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index a510d8ed716f..ed2ac74fc515 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -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)				\
 	({							\
-- 
2.28.0.rc0.105.gf9edc3c819-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] libbpf bpf_helpers: Use __builtin_offsetof for offsetof if available
  2020-07-17  7:23 [PATCH] libbpf bpf_helpers: Use __builtin_offsetof for offsetof if available Ian Rogers
@ 2020-07-18  6:00 ` Yonghong Song
  2020-07-19  3:52 ` Andrii Nakryiko
  1 sibling, 0 replies; 3+ messages in thread
From: Yonghong Song @ 2020-07-18  6:00 UTC (permalink / raw)
  To: Ian Rogers, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Song Liu, Andrii Nakryiko, John Fastabend,
	KP Singh, netdev, bpf, linux-kernel
  Cc: Stanislav Fomichev



On 7/17/20 12:23 AM, Ian Rogers wrote:
> 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.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>

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

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   tools/lib/bpf/bpf_helpers.h | 4 ++++
>   1 file changed, 4 insertions(+)
> 
> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> index a510d8ed716f..ed2ac74fc515 100644
> --- a/tools/lib/bpf/bpf_helpers.h
> +++ b/tools/lib/bpf/bpf_helpers.h
> @@ -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.

> +#else
>   #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
>   #endif
> +#endif
>   #ifndef container_of
>   #define container_of(ptr, type, member)				\
>   	({							\
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libbpf bpf_helpers: Use __builtin_offsetof for offsetof if available
  2020-07-17  7:23 [PATCH] libbpf bpf_helpers: Use __builtin_offsetof for offsetof if available Ian Rogers
  2020-07-18  6:00 ` Yonghong Song
@ 2020-07-19  3:52 ` Andrii Nakryiko
  1 sibling, 0 replies; 3+ messages in thread
From: Andrii Nakryiko @ 2020-07-19  3:52 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, Andrii Nakryiko, John Fastabend, KP Singh,
	Networking, bpf, open list, Stanislav Fomichev

On Fri, Jul 17, 2020 at 12:24 AM Ian Rogers <irogers@google.com> wrote:
>
> 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.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/lib/bpf/bpf_helpers.h | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> index a510d8ed716f..ed2ac74fc515 100644
> --- a/tools/lib/bpf/bpf_helpers.h
> +++ b/tools/lib/bpf/bpf_helpers.h
> @@ -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?

>  #endif
> +#endif
>  #ifndef container_of
>  #define container_of(ptr, type, member)                                \
>         ({                                                      \
> --
> 2.28.0.rc0.105.gf9edc3c819-goog
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-07-19  3:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-17  7:23 [PATCH] libbpf bpf_helpers: Use __builtin_offsetof for offsetof if available Ian Rogers
2020-07-18  6:00 ` Yonghong Song
2020-07-19  3:52 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).