linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning}
@ 2021-08-02 20:23 Nick Desaulniers
  2021-08-02 20:29 ` Nathan Chancellor
  2021-08-03  6:42 ` Kees Cook
  0 siblings, 2 replies; 6+ messages in thread
From: Nick Desaulniers @ 2021-08-02 20:23 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Nick Desaulniers, Nathan Chancellor, Andrew Morton, Kees Cook,
	Marco Elver, Masahiro Yamada, Luc Van Oostenryck, Arvind Sankar,
	Will Deacon, Sami Tolvanen, Arnd Bergmann, Ard Biesheuvel,
	linux-kernel, clang-built-linux

I'm working on adding support for __attribute__((__error__(""))) and
__attribute__((__warning__(""))) to Clang. To make use of these in
__compiletime_error and __compiletime_warning (as used by BUILD_BUG and
friends) for newer clang and detect/fallback for older versions of
clang, move these to compiler_attributes.h and guard them with
__has_attribute preprocessor guards.

Link: https://reviews.llvm.org/D106030
Link: https://bugs.llvm.org/show_bug.cgi?id=16428
Link: https://github.com/ClangBuiltLinux/linux/issues/1173
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
---
Changes v1 -> v2:
* Use __warning__ rather than warning in __has_attribute check, as per
  Nathan.
* Don't sort existing __GCC4_has_attribute_* defines.

 include/linux/compiler-gcc.h        |  3 ---
 include/linux/compiler_attributes.h | 24 ++++++++++++++++++++++++
 include/linux/compiler_types.h      |  6 ------
 3 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index cb9217fc60af..21c36b69eb06 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -43,9 +43,6 @@
 
 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
 
-#define __compiletime_warning(message) __attribute__((__warning__(message)))
-#define __compiletime_error(message) __attribute__((__error__(message)))
-
 #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
 #define __latent_entropy __attribute__((latent_entropy))
 #endif
diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
index 67c5667f8042..fb08b843ab47 100644
--- a/include/linux/compiler_attributes.h
+++ b/include/linux/compiler_attributes.h
@@ -30,6 +30,7 @@
 # define __GCC4_has_attribute___assume_aligned__      1
 # define __GCC4_has_attribute___copy__                0
 # define __GCC4_has_attribute___designated_init__     0
+# define __GCC4_has_attribute___error__               1
 # define __GCC4_has_attribute___externally_visible__  1
 # define __GCC4_has_attribute___no_caller_saved_registers__ 0
 # define __GCC4_has_attribute___noclone__             1
@@ -39,6 +40,7 @@
 # define __GCC4_has_attribute___no_sanitize_undefined__ 1
 # define __GCC4_has_attribute___no_sanitize_coverage__ 0
 # define __GCC4_has_attribute___fallthrough__         0
+# define __GCC4_has_attribute___warning__             1
 #endif
 
 /*
@@ -138,6 +140,17 @@
 # define __designated_init
 #endif
 
+/*
+ * Optional: only supported since clang >= 13.0
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
+ */
+#if __has_attribute(__error__)
+# define __compiletime_error(msg)       __attribute__((__error__(msg)))
+#else
+# define __compiletime_error(msg)
+#endif
+
 /*
  * Optional: not supported by clang
  *
@@ -299,6 +312,17 @@
  */
 #define __must_check                    __attribute__((__warn_unused_result__))
 
+/*
+ * Optional: only supported since clang >= 13.0
+ *
+ *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
+ */
+#if __has_attribute(__warning__)
+# define __compiletime_warning(msg)     __attribute__((__warning__(msg)))
+#else
+# define __compiletime_warning(msg)
+#endif
+
 /*
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index e4ea86fc584d..b6ff83a714ca 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -294,12 +294,6 @@ struct ftrace_likely_data {
 #ifndef __compiletime_object_size
 # define __compiletime_object_size(obj) -1
 #endif
-#ifndef __compiletime_warning
-# define __compiletime_warning(message)
-#endif
-#ifndef __compiletime_error
-# define __compiletime_error(message)
-#endif
 
 #ifdef __OPTIMIZE__
 # define __compiletime_assert(condition, msg, prefix, suffix)		\
-- 
2.32.0.554.ge1b32706d8-goog


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

* Re: [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning}
  2021-08-02 20:23 [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning} Nick Desaulniers
@ 2021-08-02 20:29 ` Nathan Chancellor
  2021-08-04 17:43   ` Nick Desaulniers
  2021-08-03  6:42 ` Kees Cook
  1 sibling, 1 reply; 6+ messages in thread
From: Nathan Chancellor @ 2021-08-02 20:29 UTC (permalink / raw)
  To: Nick Desaulniers, Miguel Ojeda
  Cc: Andrew Morton, Kees Cook, Marco Elver, Masahiro Yamada,
	Luc Van Oostenryck, Arvind Sankar, Will Deacon, Sami Tolvanen,
	Arnd Bergmann, Ard Biesheuvel, linux-kernel, clang-built-linux

On 8/2/2021 1:23 PM, 'Nick Desaulniers' via Clang Built Linux wrote:
> I'm working on adding support for __attribute__((__error__(""))) and
> __attribute__((__warning__(""))) to Clang. To make use of these in
> __compiletime_error and __compiletime_warning (as used by BUILD_BUG and
> friends) for newer clang and detect/fallback for older versions of
> clang, move these to compiler_attributes.h and guard them with
> __has_attribute preprocessor guards.
> 
> Link: https://reviews.llvm.org/D106030
> Link: https://bugs.llvm.org/show_bug.cgi?id=16428
> Link: https://github.com/ClangBuiltLinux/linux/issues/1173
> Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>

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

One comment below, please carry my tag forward in further revisions 
unless they are significant.

> ---
> Changes v1 -> v2:
> * Use __warning__ rather than warning in __has_attribute check, as per
>    Nathan.
> * Don't sort existing __GCC4_has_attribute_* defines.
> 
>   include/linux/compiler-gcc.h        |  3 ---
>   include/linux/compiler_attributes.h | 24 ++++++++++++++++++++++++
>   include/linux/compiler_types.h      |  6 ------
>   3 files changed, 24 insertions(+), 9 deletions(-)
> 
> diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
> index cb9217fc60af..21c36b69eb06 100644
> --- a/include/linux/compiler-gcc.h
> +++ b/include/linux/compiler-gcc.h
> @@ -43,9 +43,6 @@
>   
>   #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
>   
> -#define __compiletime_warning(message) __attribute__((__warning__(message)))
> -#define __compiletime_error(message) __attribute__((__error__(message)))
> -
>   #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
>   #define __latent_entropy __attribute__((latent_entropy))
>   #endif
> diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
> index 67c5667f8042..fb08b843ab47 100644
> --- a/include/linux/compiler_attributes.h
> +++ b/include/linux/compiler_attributes.h
> @@ -30,6 +30,7 @@
>   # define __GCC4_has_attribute___assume_aligned__      1
>   # define __GCC4_has_attribute___copy__                0
>   # define __GCC4_has_attribute___designated_init__     0
> +# define __GCC4_has_attribute___error__               1
>   # define __GCC4_has_attribute___externally_visible__  1
>   # define __GCC4_has_attribute___no_caller_saved_registers__ 0
>   # define __GCC4_has_attribute___noclone__             1
> @@ -39,6 +40,7 @@
>   # define __GCC4_has_attribute___no_sanitize_undefined__ 1
>   # define __GCC4_has_attribute___no_sanitize_coverage__ 0
>   # define __GCC4_has_attribute___fallthrough__         0
> +# define __GCC4_has_attribute___warning__             1
>   #endif
>   
>   /*
> @@ -138,6 +140,17 @@
>   # define __designated_init
>   #endif
>   
> +/*
> + * Optional: only supported since clang >= 13.0

Are you planning on petitioning for D106030 to be applied to 
release/13.x when it is merged into main? If not, this should be updated 
to 14.0.0 since that is main's current version.

> + *
> + *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
> + */
> +#if __has_attribute(__error__)
> +# define __compiletime_error(msg)       __attribute__((__error__(msg)))
> +#else
> +# define __compiletime_error(msg)
> +#endif
> +
>   /*
>    * Optional: not supported by clang
>    *
> @@ -299,6 +312,17 @@
>    */
>   #define __must_check                    __attribute__((__warn_unused_result__))
>   
> +/*
> + * Optional: only supported since clang >= 13.0
> + *
> + *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
> + */
> +#if __has_attribute(__warning__)
> +# define __compiletime_warning(msg)     __attribute__((__warning__(msg)))
> +#else
> +# define __compiletime_warning(msg)
> +#endif
> +
>   /*
>    *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
>    *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
> diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> index e4ea86fc584d..b6ff83a714ca 100644
> --- a/include/linux/compiler_types.h
> +++ b/include/linux/compiler_types.h
> @@ -294,12 +294,6 @@ struct ftrace_likely_data {
>   #ifndef __compiletime_object_size
>   # define __compiletime_object_size(obj) -1
>   #endif
> -#ifndef __compiletime_warning
> -# define __compiletime_warning(message)
> -#endif
> -#ifndef __compiletime_error
> -# define __compiletime_error(message)
> -#endif
>   
>   #ifdef __OPTIMIZE__
>   # define __compiletime_assert(condition, msg, prefix, suffix)		\
> 

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

* Re: [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning}
  2021-08-02 20:23 [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning} Nick Desaulniers
  2021-08-02 20:29 ` Nathan Chancellor
@ 2021-08-03  6:42 ` Kees Cook
  1 sibling, 0 replies; 6+ messages in thread
From: Kees Cook @ 2021-08-03  6:42 UTC (permalink / raw)
  To: Nick Desaulniers
  Cc: Miguel Ojeda, Nathan Chancellor, Andrew Morton, Marco Elver,
	Masahiro Yamada, Luc Van Oostenryck, Arvind Sankar, Will Deacon,
	Sami Tolvanen, Arnd Bergmann, Ard Biesheuvel, linux-kernel,
	clang-built-linux

On Mon, Aug 02, 2021 at 01:23:20PM -0700, Nick Desaulniers wrote:
> I'm working on adding support for __attribute__((__error__(""))) and
> __attribute__((__warning__(""))) to Clang. To make use of these in
> __compiletime_error and __compiletime_warning (as used by BUILD_BUG and
> friends) for newer clang and detect/fallback for older versions of
> clang, move these to compiler_attributes.h and guard them with
> __has_attribute preprocessor guards.
> 
> Link: https://reviews.llvm.org/D106030
> Link: https://bugs.llvm.org/show_bug.cgi?id=16428
> Link: https://github.com/ClangBuiltLinux/linux/issues/1173
> Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>

I'm looking forward to having this working in Clang! :)

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

-- 
Kees Cook

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

* Re: [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning}
  2021-08-02 20:29 ` Nathan Chancellor
@ 2021-08-04 17:43   ` Nick Desaulniers
  2021-08-04 17:48     ` Miguel Ojeda
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Desaulniers @ 2021-08-04 17:43 UTC (permalink / raw)
  To: Nathan Chancellor, Miguel Ojeda
  Cc: Andrew Morton, Kees Cook, Marco Elver, Masahiro Yamada,
	Luc Van Oostenryck, Arvind Sankar, Will Deacon, Sami Tolvanen,
	Arnd Bergmann, Ard Biesheuvel, linux-kernel, clang-built-linux,
	Tom Stellard

On Mon, Aug 2, 2021 at 1:29 PM Nathan Chancellor <nathan@kernel.org> wrote:
>
> On 8/2/2021 1:23 PM, 'Nick Desaulniers' via Clang Built Linux wrote:
> > I'm working on adding support for __attribute__((__error__(""))) and
> > __attribute__((__warning__(""))) to Clang. To make use of these in
> > __compiletime_error and __compiletime_warning (as used by BUILD_BUG and
> > friends) for newer clang and detect/fallback for older versions of
> > clang, move these to compiler_attributes.h and guard them with
> > __has_attribute preprocessor guards.
> >
> > Link: https://reviews.llvm.org/D106030
> > Link: https://bugs.llvm.org/show_bug.cgi?id=16428
> > Link: https://github.com/ClangBuiltLinux/linux/issues/1173
> > Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
>
> Reviewed-by: Nathan Chancellor <nathan@kernel.org>
>
> One comment below, please carry my tag forward in further revisions
> unless they are significant.
>
> > ---
> > Changes v1 -> v2:
> > * Use __warning__ rather than warning in __has_attribute check, as per
> >    Nathan.
> > * Don't sort existing __GCC4_has_attribute_* defines.
> >
> >   include/linux/compiler-gcc.h        |  3 ---
> >   include/linux/compiler_attributes.h | 24 ++++++++++++++++++++++++
> >   include/linux/compiler_types.h      |  6 ------
> >   3 files changed, 24 insertions(+), 9 deletions(-)
> >
> > diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
> > index cb9217fc60af..21c36b69eb06 100644
> > --- a/include/linux/compiler-gcc.h
> > +++ b/include/linux/compiler-gcc.h
> > @@ -43,9 +43,6 @@
> >
> >   #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
> >
> > -#define __compiletime_warning(message) __attribute__((__warning__(message)))
> > -#define __compiletime_error(message) __attribute__((__error__(message)))
> > -
> >   #if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
> >   #define __latent_entropy __attribute__((latent_entropy))
> >   #endif
> > diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
> > index 67c5667f8042..fb08b843ab47 100644
> > --- a/include/linux/compiler_attributes.h
> > +++ b/include/linux/compiler_attributes.h
> > @@ -30,6 +30,7 @@
> >   # define __GCC4_has_attribute___assume_aligned__      1
> >   # define __GCC4_has_attribute___copy__                0
> >   # define __GCC4_has_attribute___designated_init__     0
> > +# define __GCC4_has_attribute___error__               1
> >   # define __GCC4_has_attribute___externally_visible__  1
> >   # define __GCC4_has_attribute___no_caller_saved_registers__ 0
> >   # define __GCC4_has_attribute___noclone__             1
> > @@ -39,6 +40,7 @@
> >   # define __GCC4_has_attribute___no_sanitize_undefined__ 1
> >   # define __GCC4_has_attribute___no_sanitize_coverage__ 0
> >   # define __GCC4_has_attribute___fallthrough__         0
> > +# define __GCC4_has_attribute___warning__             1
> >   #endif
> >
> >   /*
> > @@ -138,6 +140,17 @@
> >   # define __designated_init
> >   #endif
> >
> > +/*
> > + * Optional: only supported since clang >= 13.0
>
> Are you planning on petitioning for D106030 to be applied to
> release/13.x when it is merged into main? If not, this should be updated
> to 14.0.0 since that is main's current version.

Ah, I forgot the release/13.x branch was cut before I sent this.  I'd
rather this feature "ride the trains" so that it gets more soak time.

Miguel, would you like a v3 updating the comment above (and the
comment below) to s/13.0/14.0/g, or can you simply fold that change
into this one when applying it?

>
> > + *
> > + *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-error-function-attribute
> > + */
> > +#if __has_attribute(__error__)
> > +# define __compiletime_error(msg)       __attribute__((__error__(msg)))
> > +#else
> > +# define __compiletime_error(msg)
> > +#endif
> > +
> >   /*
> >    * Optional: not supported by clang
> >    *
> > @@ -299,6 +312,17 @@
> >    */
> >   #define __must_check                    __attribute__((__warn_unused_result__))
> >
> > +/*
> > + * Optional: only supported since clang >= 13.0
> > + *
> > + *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-warning-function-attribute
> > + */
> > +#if __has_attribute(__warning__)
> > +# define __compiletime_warning(msg)     __attribute__((__warning__(msg)))
> > +#else
> > +# define __compiletime_warning(msg)
> > +#endif
> > +
> >   /*
> >    *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-weak-function-attribute
> >    *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-weak-variable-attribute
> > diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
> > index e4ea86fc584d..b6ff83a714ca 100644
> > --- a/include/linux/compiler_types.h
> > +++ b/include/linux/compiler_types.h
> > @@ -294,12 +294,6 @@ struct ftrace_likely_data {
> >   #ifndef __compiletime_object_size
> >   # define __compiletime_object_size(obj) -1
> >   #endif
> > -#ifndef __compiletime_warning
> > -# define __compiletime_warning(message)
> > -#endif
> > -#ifndef __compiletime_error
> > -# define __compiletime_error(message)
> > -#endif
> >
> >   #ifdef __OPTIMIZE__
> >   # define __compiletime_assert(condition, msg, prefix, suffix)               \
> >



-- 
Thanks,
~Nick Desaulniers

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

* Re: [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning}
  2021-08-04 17:43   ` Nick Desaulniers
@ 2021-08-04 17:48     ` Miguel Ojeda
  2021-09-02 18:26       ` Nick Desaulniers
  0 siblings, 1 reply; 6+ messages in thread
From: Miguel Ojeda @ 2021-08-04 17:48 UTC (permalink / raw)
  To: Nick Desaulniers
  Cc: Nathan Chancellor, Miguel Ojeda, Andrew Morton, Kees Cook,
	Marco Elver, Masahiro Yamada, Luc Van Oostenryck, Arvind Sankar,
	Will Deacon, Sami Tolvanen, Arnd Bergmann, Ard Biesheuvel,
	linux-kernel, clang-built-linux, Tom Stellard

On Wed, Aug 4, 2021 at 7:44 PM Nick Desaulniers <ndesaulniers@google.com> wrote:
>
> Ah, I forgot the release/13.x branch was cut before I sent this.  I'd
> rather this feature "ride the trains" so that it gets more soak time.
>
> Miguel, would you like a v3 updating the comment above (and the
> comment below) to s/13.0/14.0/g, or can you simply fold that change
> into this one when applying it?

I can do the replacements on my side, no worries!

Cheers,
Miguel

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

* Re: [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning}
  2021-08-04 17:48     ` Miguel Ojeda
@ 2021-09-02 18:26       ` Nick Desaulniers
  0 siblings, 0 replies; 6+ messages in thread
From: Nick Desaulniers @ 2021-09-02 18:26 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Nathan Chancellor, Miguel Ojeda, Andrew Morton, Kees Cook,
	Marco Elver, Masahiro Yamada, Luc Van Oostenryck, Arvind Sankar,
	Will Deacon, Sami Tolvanen, Arnd Bergmann, Ard Biesheuvel,
	linux-kernel, clang-built-linux, Tom Stellard

On Wed, Aug 4, 2021 at 10:49 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> On Wed, Aug 4, 2021 at 7:44 PM Nick Desaulniers <ndesaulniers@google.com> wrote:
> >
> > Ah, I forgot the release/13.x branch was cut before I sent this.  I'd
> > rather this feature "ride the trains" so that it gets more soak time.
> >
> > Miguel, would you like a v3 updating the comment above (and the
> > comment below) to s/13.0/14.0/g, or can you simply fold that change
> > into this one when applying it?
>
> I can do the replacements on my side, no worries!

Bump, this has landed in llvm 14 (so needs a fixup when applying locally).
-- 
Thanks,
~Nick Desaulniers

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

end of thread, other threads:[~2021-09-02 18:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-02 20:23 [PATCH v2] compiler_attributes.h: move __compiletime_{error|warning} Nick Desaulniers
2021-08-02 20:29 ` Nathan Chancellor
2021-08-04 17:43   ` Nick Desaulniers
2021-08-04 17:48     ` Miguel Ojeda
2021-09-02 18:26       ` Nick Desaulniers
2021-08-03  6:42 ` Kees Cook

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).