All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] include: drop pointless __compiler_offsetof indirection
@ 2022-02-02 10:21 Rasmus Villemoes
  2022-02-02 11:03 ` Miguel Ojeda
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Rasmus Villemoes @ 2022-02-02 10:21 UTC (permalink / raw)
  To: Nick Desaulniers, Miguel Ojeda, Andrew Morton
  Cc: Nathan Chancellor, Kees Cook, Rasmus Villemoes, linux-kernel, llvm

(1) compiler_types.h is unconditionally included via an -include
flag (see scripts/Makefile.lib), and it defines __compiler_offsetof
unconditionally. So testing for definedness of __compiler_offsetof is
mostly pointless.

(2) Every relevant compiler provides __builtin_offsetof (even sparse
has had that for 14 years), and if for whatever reason one would end
up picking up the poor man's fallback definition (C file compiler with
completely custom CFLAGS?), newer clang versions won't treat the
result as an Integer Constant Expression, so if used in place where
such is required (static initializer or static_assert), one would
get errors like

t.c:11:16: error: static_assert expression is not an integral constant expression
t.c:11:16: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
t.c:4:33: note: expanded from macro 'offsetof'
#define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)

So just define offsetof unconditionally and directly in terms of
__builtin_offsetof.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 include/linux/compiler_types.h | 2 --
 include/linux/stddef.h         | 6 +-----
 2 files changed, 1 insertion(+), 7 deletions(-)

diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 3c1795fdb568..83ee7f7ada5d 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -137,8 +137,6 @@ struct ftrace_likely_data {
  */
 #define __naked			__attribute__((__naked__)) notrace
 
-#define __compiler_offsetof(a, b)	__builtin_offsetof(a, b)
-
 /*
  * Prefer gnu_inline, so that extern inline functions do not emit an
  * externally visible function. This makes extern inline behave as per gnu89
diff --git a/include/linux/stddef.h b/include/linux/stddef.h
index ca507bd5f808..929d67710cc5 100644
--- a/include/linux/stddef.h
+++ b/include/linux/stddef.h
@@ -13,11 +13,7 @@ enum {
 };
 
 #undef offsetof
-#ifdef __compiler_offsetof
-#define offsetof(TYPE, MEMBER)	__compiler_offsetof(TYPE, MEMBER)
-#else
-#define offsetof(TYPE, MEMBER)	((size_t)&((TYPE *)0)->MEMBER)
-#endif
+#define offsetof(TYPE, MEMBER)	__builtin_offsetof(TYPE, MEMBER)
 
 /**
  * sizeof_field() - Report the size of a struct field in bytes
-- 
2.31.1


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

* Re: [PATCH] include: drop pointless __compiler_offsetof indirection
  2022-02-02 10:21 [PATCH] include: drop pointless __compiler_offsetof indirection Rasmus Villemoes
@ 2022-02-02 11:03 ` Miguel Ojeda
  2022-02-02 16:01 ` Nathan Chancellor
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Miguel Ojeda @ 2022-02-02 11:03 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Nick Desaulniers, Miguel Ojeda, Andrew Morton, Nathan Chancellor,
	Kees Cook, linux-kernel, llvm

On Wed, Feb 2, 2022 at 11:21 AM Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:
>
> (1) compiler_types.h is unconditionally included via an -include
> flag (see scripts/Makefile.lib), and it defines __compiler_offsetof
> unconditionally. So testing for definedness of __compiler_offsetof is
> mostly pointless.

Sounds good to me.

By the way, there are a bunch of `#include <linux/compiler_types.h>`
around that could be removed.

> (2) Every relevant compiler provides __builtin_offsetof (even sparse

Indeed, even GCC 4.

Reviewed-by: Miguel Ojeda <ojeda@kernel.org>

Cheers,
Miguel

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

* Re: [PATCH] include: drop pointless __compiler_offsetof indirection
  2022-02-02 10:21 [PATCH] include: drop pointless __compiler_offsetof indirection Rasmus Villemoes
  2022-02-02 11:03 ` Miguel Ojeda
@ 2022-02-02 16:01 ` Nathan Chancellor
  2022-02-02 20:53 ` Kees Cook
  2022-02-02 21:04 ` Nick Desaulniers
  3 siblings, 0 replies; 5+ messages in thread
From: Nathan Chancellor @ 2022-02-02 16:01 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Nick Desaulniers, Miguel Ojeda, Andrew Morton, Kees Cook,
	linux-kernel, llvm

On Wed, Feb 02, 2022 at 11:21:47AM +0100, Rasmus Villemoes wrote:
> (1) compiler_types.h is unconditionally included via an -include
> flag (see scripts/Makefile.lib), and it defines __compiler_offsetof
> unconditionally. So testing for definedness of __compiler_offsetof is
> mostly pointless.
> 
> (2) Every relevant compiler provides __builtin_offsetof (even sparse
> has had that for 14 years), and if for whatever reason one would end
> up picking up the poor man's fallback definition (C file compiler with
> completely custom CFLAGS?), newer clang versions won't treat the
> result as an Integer Constant Expression, so if used in place where
> such is required (static initializer or static_assert), one would
> get errors like
> 
> t.c:11:16: error: static_assert expression is not an integral constant expression
> t.c:11:16: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
> t.c:4:33: note: expanded from macro 'offsetof'
> #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
> 
> So just define offsetof unconditionally and directly in terms of
> __builtin_offsetof.
> 
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>

Reviewed-by: Nathan Chancellor <nathan@kernel.org>

> ---
>  include/linux/compiler_types.h | 2 --
>  include/linux/stddef.h         | 6 +-----
>  2 files changed, 1 insertion(+), 7 deletions(-)
> 
> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> index 3c1795fdb568..83ee7f7ada5d 100644
> --- a/include/linux/compiler_types.h
> +++ b/include/linux/compiler_types.h
> @@ -137,8 +137,6 @@ struct ftrace_likely_data {
>   */
>  #define __naked			__attribute__((__naked__)) notrace
>  
> -#define __compiler_offsetof(a, b)	__builtin_offsetof(a, b)
> -
>  /*
>   * Prefer gnu_inline, so that extern inline functions do not emit an
>   * externally visible function. This makes extern inline behave as per gnu89
> diff --git a/include/linux/stddef.h b/include/linux/stddef.h
> index ca507bd5f808..929d67710cc5 100644
> --- a/include/linux/stddef.h
> +++ b/include/linux/stddef.h
> @@ -13,11 +13,7 @@ enum {
>  };
>  
>  #undef offsetof
> -#ifdef __compiler_offsetof
> -#define offsetof(TYPE, MEMBER)	__compiler_offsetof(TYPE, MEMBER)
> -#else
> -#define offsetof(TYPE, MEMBER)	((size_t)&((TYPE *)0)->MEMBER)
> -#endif
> +#define offsetof(TYPE, MEMBER)	__builtin_offsetof(TYPE, MEMBER)
>  
>  /**
>   * sizeof_field() - Report the size of a struct field in bytes
> -- 
> 2.31.1
> 

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

* Re: [PATCH] include: drop pointless __compiler_offsetof indirection
  2022-02-02 10:21 [PATCH] include: drop pointless __compiler_offsetof indirection Rasmus Villemoes
  2022-02-02 11:03 ` Miguel Ojeda
  2022-02-02 16:01 ` Nathan Chancellor
@ 2022-02-02 20:53 ` Kees Cook
  2022-02-02 21:04 ` Nick Desaulniers
  3 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2022-02-02 20:53 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Nick Desaulniers, Miguel Ojeda, Andrew Morton, Nathan Chancellor,
	linux-kernel, llvm

On Wed, Feb 02, 2022 at 11:21:47AM +0100, Rasmus Villemoes wrote:
> (1) compiler_types.h is unconditionally included via an -include
> flag (see scripts/Makefile.lib), and it defines __compiler_offsetof
> unconditionally. So testing for definedness of __compiler_offsetof is
> mostly pointless.
> 
> (2) Every relevant compiler provides __builtin_offsetof (even sparse
> has had that for 14 years), and if for whatever reason one would end
> up picking up the poor man's fallback definition (C file compiler with
> completely custom CFLAGS?), newer clang versions won't treat the
> result as an Integer Constant Expression, so if used in place where
> such is required (static initializer or static_assert), one would
> get errors like
> 
> t.c:11:16: error: static_assert expression is not an integral constant expression
> t.c:11:16: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
> t.c:4:33: note: expanded from macro 'offsetof'
> #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
> 
> So just define offsetof unconditionally and directly in terms of
> __builtin_offsetof.
> 
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>

Yes please. :)

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

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

* Re: [PATCH] include: drop pointless __compiler_offsetof indirection
  2022-02-02 10:21 [PATCH] include: drop pointless __compiler_offsetof indirection Rasmus Villemoes
                   ` (2 preceding siblings ...)
  2022-02-02 20:53 ` Kees Cook
@ 2022-02-02 21:04 ` Nick Desaulniers
  3 siblings, 0 replies; 5+ messages in thread
From: Nick Desaulniers @ 2022-02-02 21:04 UTC (permalink / raw)
  To: Rasmus Villemoes
  Cc: Miguel Ojeda, Andrew Morton, Nathan Chancellor, Kees Cook,
	linux-kernel, llvm

On Wed, Feb 2, 2022 at 2:21 AM Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:
>
> (1) compiler_types.h is unconditionally included via an -include
> flag (see scripts/Makefile.lib), and it defines __compiler_offsetof
> unconditionally. So testing for definedness of __compiler_offsetof is
> mostly pointless.
>
> (2) Every relevant compiler provides __builtin_offsetof (even sparse
> has had that for 14 years), and if for whatever reason one would end
> up picking up the poor man's fallback definition (C file compiler with
> completely custom CFLAGS?), newer clang versions won't treat the
> result as an Integer Constant Expression, so if used in place where
> such is required (static initializer or static_assert), one would
> get errors like
>
> t.c:11:16: error: static_assert expression is not an integral constant expression
> t.c:11:16: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
> t.c:4:33: note: expanded from macro 'offsetof'
> #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
>
> So just define offsetof unconditionally and directly in terms of
> __builtin_offsetof.
>
> Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
> ---
>  include/linux/compiler_types.h | 2 --
>  include/linux/stddef.h         | 6 +-----
>  2 files changed, 1 insertion(+), 7 deletions(-)
>
> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> index 3c1795fdb568..83ee7f7ada5d 100644
> --- a/include/linux/compiler_types.h
> +++ b/include/linux/compiler_types.h
> @@ -137,8 +137,6 @@ struct ftrace_likely_data {
>   */
>  #define __naked                        __attribute__((__naked__)) notrace
>
> -#define __compiler_offsetof(a, b)      __builtin_offsetof(a, b)
> -
>  /*
>   * Prefer gnu_inline, so that extern inline functions do not emit an
>   * externally visible function. This makes extern inline behave as per gnu89
> diff --git a/include/linux/stddef.h b/include/linux/stddef.h
> index ca507bd5f808..929d67710cc5 100644
> --- a/include/linux/stddef.h
> +++ b/include/linux/stddef.h
> @@ -13,11 +13,7 @@ enum {
>  };
>
>  #undef offsetof
> -#ifdef __compiler_offsetof
> -#define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
> -#else
> -#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)

RIP `((size_t)&((TYPE *)0)->MEMBER)`; you were beautiful in a way only
a mother could love.
Acked-by: Nick Desaulniers <ndesaulniers@google.com>

> -#endif
> +#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
>
>  /**
>   * sizeof_field() - Report the size of a struct field in bytes
> --
> 2.31.1
>


-- 
Thanks,
~Nick Desaulniers

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

end of thread, other threads:[~2022-02-02 21:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-02 10:21 [PATCH] include: drop pointless __compiler_offsetof indirection Rasmus Villemoes
2022-02-02 11:03 ` Miguel Ojeda
2022-02-02 16:01 ` Nathan Chancellor
2022-02-02 20:53 ` Kees Cook
2022-02-02 21:04 ` Nick Desaulniers

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.