Skip to contentSkip to conversation

compiler_types: mark __compiletime_assert failure as __noreturn

PATCH In review Miguel Ojeda 7 participants 15 messages Oct 14, 2021 – Dec 2, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 compiler_types: mark __compiletime_assert failure as __noreturn

__compiletime_assert declares a fake extern function which appears (to the compiler) to be called when the test fails.

Therefore, compilers may emit possibly-uninitialized warnings in some cases, even if it will be an error anyway (for compilers supporting the error attribute, e.g. GCC and Clang >= 14) or a link failure (for those that do not, e.g. Clang < 14).

Annotating the fake function as __noreturn gives them the information they need to avoid the warning, e.g. see https://godbolt.org/z/x1v69jjYY.

---
 include/linux/compiler_types.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/compiler_types.h +7 -1
index b6ff83a714ca..ca1a66b8cd2f 100644
@@ -298,7 +298,13 @@ struct ftrace_likely_data {
 #ifdef __OPTIMIZE__
 # define __compiletime_assert(condition, msg, prefix, suffix)		\
 	do {								\
-		extern void prefix ## suffix(void) __compiletime_error(msg); \
+		/*							\
+		 * __noreturn is needed to give the compiler enough	\
+		 * information to avoid certain possibly-uninitialized	\
+		 * warnings (regardless of the build failing).		\
+		 */							\
+		__noreturn extern void prefix ## suffix(void)		\
+			__compiletime_error(msg);			\
 		if (!(condition))					\
 			prefix ## suffix();				\
 	} while (0)
Signature · 1 line
2.33.1
#1 lore raw
Quoted 28 lines · Miguel Ojeda

__compiletime_assert declares a fake extern function which appears (to the compiler) to be called when the test fails.

Therefore, compilers may emit possibly-uninitialized warnings in some cases, even if it will be an error anyway (for compilers supporting the error attribute, e.g. GCC and Clang >= 14) or a link failure (for those that do not, e.g. Clang < 14).

Annotating the fake function as __noreturn gives them the information they need to avoid the warning, e.g. see https://godbolt.org/z/x1v69jjYY.

---
 include/linux/compiler_types.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/compiler_types.h +0 -1
index b6ff83a714ca..ca1a66b8cd2f 100644
@@ -298,7 +298,13 @@ struct ftrace_likely_data {
 #ifdef __OPTIMIZE__
 # define __compiletime_assert(condition, msg, prefix, suffix)		\
 	do {								\
-		extern void prefix ## suffix(void) __compiletime_error(msg); \

include/linux/compiler_types.h struct ftrace_likely_data { · line 310

Quoted 10 lines
include/linux/compiler_types.h +7 -0
+		/*							\
+		 * __noreturn is needed to give the compiler enough	\
+		 * information to avoid certain possibly-uninitialized	\
+		 * warnings (regardless of the build failing).		\
+		 */							\
+		__noreturn extern void prefix ## suffix(void)		\
+			__compiletime_error(msg);			\
 		if (!(condition))					\
 			prefix ## suffix();				\
 	} while (0)

Should we not convert this to _Static_assert, now that all supported compilers are of recent enough vintage to support that?

Reviewed-by on v1 1/1
#10 lore raw
In reply to Miguel Ojeda
Quoted 8 lines · Miguel Ojeda

__compiletime_assert declares a fake extern function which appears (to the compiler) to be called when the test fails.

Therefore, compilers may emit possibly-uninitialized warnings in some cases, even if it will be an error anyway (for compilers supporting the error attribute, e.g. GCC and Clang >= 14) or a link failure (for those that do not, e.g. Clang < 14).

Quoted 7 lines

Annotating the fake function as __noreturn gives them the information they need to avoid the warning, e.g. see https://godbolt.org/z/x1v69jjYY.

Quoted 26 lines
---
 include/linux/compiler_types.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/compiler_types.h +7 -1
index b6ff83a714ca..ca1a66b8cd2f 100644
@@ -298,7 +298,13 @@ struct ftrace_likely_data {
 #ifdef __OPTIMIZE__
 # define __compiletime_assert(condition, msg, prefix, suffix)		\
 	do {								\
-		extern void prefix ## suffix(void) __compiletime_error(msg); \
+		/*							\
+		 * __noreturn is needed to give the compiler enough	\
+		 * information to avoid certain possibly-uninitialized	\
+		 * warnings (regardless of the build failing).		\
+		 */							\
+		__noreturn extern void prefix ## suffix(void)		\
+			__compiletime_error(msg);			\
 		if (!(condition))					\
 			prefix ## suffix();				\
 	} while (0)
Signature · 1 line
2.33.1
Reviewed-by on v1 1/1
#11 lore raw
In reply to Miguel Ojeda
Quoted 4 lines · Miguel Ojeda

__compiletime_assert declares a fake extern function which appears (to the compiler) to be called when the test fails.

Quoted 8 lines

Therefore, compilers may emit possibly-uninitialized warnings in some cases, even if it will be an error anyway (for compilers supporting the error attribute, e.g. GCC and Clang >= 14) or a link failure (for those that do not, e.g. Clang < 14).

Annotating the fake function as __noreturn gives them the information they need to avoid the warning, e.g. see https://godbolt.org/z/x1v69jjYY.

Thanks for the patch!

Quoted 31 lines
---
 include/linux/compiler_types.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/compiler_types.h +7 -2
index b6ff83a714ca..ca1a66b8cd2f 100644
@@ -298,7 +298,13 @@ struct ftrace_likely_data {
 #ifdef __OPTIMIZE__
 # define __compiletime_assert(condition, msg, prefix, suffix)          \
        do {                                                            \
-               extern void prefix ## suffix(void) __compiletime_error(msg); \
+               /*                                                      \
+                * __noreturn is needed to give the compiler enough     \
+                * information to avoid certain possibly-uninitialized  \
+                * warnings (regardless of the build failing).          \
+                */                                                     \
+               __noreturn extern void prefix ## suffix(void)           \
+                       __compiletime_error(msg);                       \
                if (!(condition))                                       \
                        prefix ## suffix();                             \
        } while (0)
--

2.33.1

Signature · 2 lines
Thanks,
~Nick Desaulniers
#2 lore raw
In reply to Peter Zijlstra
Quoted 35 lines · Peter Zijlstra
Quoted 33 lines · Miguel Ojeda

__compiletime_assert declares a fake extern function which appears (to the compiler) to be called when the test fails.

Therefore, compilers may emit possibly-uninitialized warnings in some cases, even if it will be an error anyway (for compilers supporting the error attribute, e.g. GCC and Clang >= 14) or a link failure (for those that do not, e.g. Clang < 14).

Annotating the fake function as __noreturn gives them the information they need to avoid the warning, e.g. see https://godbolt.org/z/x1v69jjYY.

---
 include/linux/compiler_types.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
include/linux/compiler_types.h +5 -1
index b6ff83a714ca..ca1a66b8cd2f 100644
@@ -298,7 +298,13 @@ struct ftrace_likely_data {
 #ifdef __OPTIMIZE__
 # define __compiletime_assert(condition, msg, prefix, suffix)                \
      do {                                                            \
-             extern void prefix ## suffix(void) __compiletime_error(msg); \
+             /*                                                      \
+              * __noreturn is needed to give the compiler enough     \
+              * information to avoid certain possibly-uninitialized  \
+              * warnings (regardless of the build failing).          \
+              */                                                     \
Quoted 8 lines

include/linux/compiler_types.h struct ftrace_likely_data { · line 310

Quoted 5 lines
include/linux/compiler_types.h +2 -0
+             __noreturn extern void prefix ## suffix(void)           \
+                     __compiletime_error(msg);                       \
              if (!(condition))                                       \
                      prefix ## suffix();                             \
      } while (0)

Should we not convert this to _Static_assert, now that all supported compilers are of recent enough vintage to support that?

It's a good question; I'm pretty sure we had a thread with Rasmus on the idea a while ago, and IIRC the answer is no.

Basically, we can't convert BUILD_BUG_ON to _Static_assert because _Static_assert requires integer constant expressions (ICE) while many expressions passed to BUILD_BUG_ON in the kernel require that optimizations such as inlining run (they are not ICEs); BUILD_BUG_ON is more flexible. So you can't replace the guts of BUILD_BUG_ON wholesale with _Static_assert (without doing anything else); it would be preferable for kernel developers to use _Static_assert (I think we have a macro, static_assert, too) in cases where they have ICEs rather than BUILD_BUG_ON (though it flips the condition of the expression; _Static_assert errors if the expression evaluates to false; BUILD_BUG_ON when true), but I think there's too much muscle memory around just using BUILD_BUG_ON that if you introduced something new, folks wouldn't know to use that instead.

Probably a better demonstration would be to try it and observe some of the spooky failures at build time that result. We may be able to separate the macro into two; BUILD_BUG_ON and BUILD_BUG_ON_OPT (or whatever color bikeshed), where the former uses _Static_assert under the hood, and the latter uses __attribute__((error)). Then we could go about converting cases that could not use _Static_assert to use the new macro, while the old macro is what folks still reach for first.

I'm not sure how worthwhile that yakshave would be, but at least the front end of the compiler would error sooner in the case of _Static_assert, FWIW (not much). But I don't think we can ever eliminate __attribute__((error)) from the kernel unless we're ok outright removing asserts that aren't ICEs. I would not recommend that. I would like to see more usage of static_assert, but I'm not sure how best to promote that, and if it's worth discussing the subtle distinction between BUILD_BUG_ON vs _Static_assert again and again and again every time.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#3 lore raw
Quoted 3 lines · Nick Desaulniers

It's a good question; I'm pretty sure we had a thread with Rasmus on the idea a while ago, and IIRC the answer is no.

Yeah, I remember that too.

Quoted 5 lines

Basically, we can't convert BUILD_BUG_ON to _Static_assert because _Static_assert requires integer constant expressions (ICE) while many expressions passed to BUILD_BUG_ON in the kernel require that optimizations such as inlining run (they are not ICEs); BUILD_BUG_ON is more flexible. So you can't replace the guts of BUILD_BUG_ON

Quoted 8 lines

wholesale with _Static_assert (without doing anything else); it would be preferable for kernel developers to use _Static_assert (I think we have a macro, static_assert, too) in cases where they have ICEs rather than BUILD_BUG_ON (though it flips the condition of the expression; _Static_assert errors if the expression evaluates to false; BUILD_BUG_ON when true), but I think there's too much muscle memory around just using BUILD_BUG_ON that if you introduced something new, folks wouldn't know to use that instead.

Indeed, BUILD_BUG_ON requires the optimizer to see through whatever you are trying to do. Way more powerful, but finicky too.

Another difference is that _Static_assert can be used in more places (file scope, inside structs...) for tests about e.g. sizes, i.e. BUILD_BUG_ON is not a complete replacement either.

Quoted 7 lines

Probably a better demonstration would be to try it and observe some of the spooky failures at build time that result. We may be able to separate the macro into two; BUILD_BUG_ON and BUILD_BUG_ON_OPT (or whatever color bikeshed), where the former uses _Static_assert under the hood, and the latter uses __attribute__((error)). Then we could go about converting cases that could not use _Static_assert to use the new macro, while the old macro is what folks still reach for first.

That would be a nice to do, but I am not sure about introducing one more macro about this... I think it would be simpler to submit patches for moves into static_assert even if we have to "flip" the meaning.

Quoted 9 lines

I'm not sure how worthwhile that yakshave would be, but at least the front end of the compiler would error sooner in the case of _Static_assert, FWIW (not much). But I don't think we can ever eliminate __attribute__((error)) from the kernel unless we're ok outright removing asserts that aren't ICEs. I would not recommend that. I would like to see more usage of static_assert, but I'm not sure how best to promote that, and if it's worth discussing the subtle distinction between BUILD_BUG_ON vs _Static_assert again and again and again every time.

Perhaps we should add a comment in BUILD_BUG* about checking out static_assert -- we have the comment in the latter, but those reading the former will not realize the may be able to use the latter...

Cheers,
Miguel

#4 lore raw
Quoted 4 lines · Miguel Ojeda

That would be a nice to do, but I am not sure about introducing one more macro about this... I think it would be simpler to submit patches for moves into static_assert even if we have to "flip" the meaning.

Actually, what would be ideal is a compiler-backed lint that checks whether it could be an static_assert, perhaps in clang-tidy?

It would also ensure things are kept clean.

Cheers,
Miguel

#5 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 4 lines · Miguel Ojeda

That would be a nice to do, but I am not sure about introducing one more macro about this... I think it would be simpler to submit patches for moves into static_assert even if we have to "flip" the meaning.

$ grep -r BUILD_BUG_ON | wc -l
3405
Quoted 2 lines

Actually, what would be ideal is a compiler-backed lint that checks whether it could be an static_assert, perhaps in clang-tidy?

Oh, that is a good idea. There is one already for recommending the use of static_assert instead of assert. That's actually very nice.

I was playing with trying to adapt clang-tidy's C++11 auto fixit to work on GNU C code to automate the replacement of:

__typeof(x) y = (x);

with:

__auto_type y = (x);

in macros. That's perhaps interesting, too. Given the volume of code in the kernel, I wouldn't waste time with one off patches; rather I'd work on automation (since clang-tidy can be taught "fixits" to fix the code in place, not just warn) so that we can better enable treewide changes AND keep new instances from sneaking back in easier.

Let's see if I get an intern in 2022, maybe I can have them focus on clang-tidy+kernel.

Quoted 5 lines

It would also ensure things are kept clean.

Cheers,
Miguel

Signature · 2 lines
Thanks,
~Nick Desaulniers
#6 lore raw
Quoted 3 lines · Nick Desaulniers
$ grep -r BUILD_BUG_ON | wc -l
3405

Definitely -- I am assuming a significant part of the macro invocations cannot be static asserts so that we would ended up with churn anyway...

Quoted 2 lines

Oh, that is a good idea. There is one already for recommending the use of static_assert instead of assert. That's actually very nice.

Happy to help!

Quoted 4 lines

in the kernel, I wouldn't waste time with one off patches; rather I'd work on automation (since clang-tidy can be taught "fixits" to fix the code in place, not just warn) so that we can better enable treewide changes AND keep new instances from sneaking back in easier.

For automatic fixing we would need to be a bit smart due to the negation w.r.t. "style", i.e. in most cases it should be easy to apply it to the expression, e.g. from !(x == 42) to x != 42, but in others it may require some "human touch".

There is also the possible mismatch of the usual style rules even if we apply e.g. clang-format after it (one more reason to avoid human formatting...).

But yeah, I think we should be able to cover the vast majority of them easily. We can always send them as RFC patches and let folks adapt the patch, then enable the warning/error by default after one release or two.

Quoted 2 lines

Let's see if I get an intern in 2022, maybe I can have them focus on clang-tidy+kernel.

It would be great to have someone adding more linuxkernel- checks!

Cheers,
Miguel

#7 lore raw
In reply to Nick Desaulniers
Quoted 10 lines · Nick Desaulniers
Quoted 7 lines · Miguel Ojeda
Quoted 4 lines · Miguel Ojeda

That would be a nice to do, but I am not sure about introducing one more macro about this... I think it would be simpler to submit patches for moves into static_assert even if we have to "flip" the meaning.

Quoted 8 lines
$ grep -r BUILD_BUG_ON | wc -l
3405
Quoted 2 lines

Actually, what would be ideal is a compiler-backed lint that checks whether it could be an static_assert, perhaps in clang-tidy?

Oh, that is a good idea. There is one already for recommending the use of static_assert instead of assert. That's actually very nice.

So I did

include/linux/compiler_types.h +2 -0
index b6ff83a714ca..e212220216e8 100644
@@ -295,12 +295,17 @@ struct ftrace_likely_data {
 # define __compiletime_object_size(obj) -1
 #endif
 
+#include <linux/const.h>
+
 #ifdef __OPTIMIZE__
 # define __compiletime_assert(condition, msg, prefix, suffix)          \
        do {                                                            \
                extern void prefix ## suffix(void)

__compiletime_error(msg); \
+ extern void prefix ## suffix ## ice(void)

__compiletime_warning("Ice ice baby"); \
                if (!(condition))                                       \
                        prefix ## suffix();                             \
+               if (__is_constexpr(condition))                          \
+                       prefix ## suffix ## ice();                      \
        } while (0)
 #else
 # define __compiletime_assert(condition, msg, prefix, suffix) do { }
while (0)

and that throws a gazillion warnings. Picking one at random shows that container_of() has a BUILD_BUG_ON. So we could do

include/linux/kernel.h +0 -0
index 2776423a587e..0a1969b11619 100644
@@ -492,9 +492,9 @@ static inline void ftrace_dump(enum ftrace_dump_mode

oops_dump_mode) { }
*/

 #define container_of(ptr, type, member) ({                             \
        void *__mptr = (void *)(ptr);                                   \
-       BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&   \
-                        !__same_type(*(ptr), void),                    \
-                        "pointer type mismatch in container_of()");    \
+       static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
+                     __same_type(*(ptr), void),                        \
+                     "pointer type mismatch in container_of()");       \
        ((type *)(__mptr - offsetof(type, member))); })

/**
@@ -507,9 +507,9 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
*/

 #define container_of_safe(ptr, type, member) ({
        \
        void *__mptr = (void *)(ptr);                                   \
-       BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&   \
-                        !__same_type(*(ptr), void),                    \
-                        "pointer type mismatch in container_of()");    \
+       static_assert(__same_type(*(ptr), ((type *)0)->member) ||       \
+                     __same_type(*(ptr), void),                        \
+                     "pointer type mismatch in container_of_safe()");  \
        IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) :                     \
                ((type *)(__mptr - offsetof(type, member))); })

[fixing the copy-pasto in container_of_safe while at it].

Basically, all BUILD_BUG_ONs like that that do type checking are very obviously candidates for using static_assert instead (they very naturally go at the end of all the locally declared variables, so one won't hit the declaration-after-statement problem that can otherwise prevent using static_assert).

Quoted 11 lines

I was playing with trying to adapt clang-tidy's C++11 auto fixit to work on GNU C code to automate the replacement of:

__typeof(x) y = (x);

with:

__auto_type y = (x);

in macros. That's perhaps interesting, too. Given the volume of code in the kernel, I wouldn't waste time with one off patches;

Well, for the kind of macros that are used _everywhere_ a few one-off patches might be in order. It's also interesting if one could measure any speedup from switching those core macros to static_assert.

Rasmus

#8 lore raw
In reply to Nick Desaulniers
Quoted 4 lines · Nick Desaulniers
Quoted 1 line · Peter Zijlstra

I'm not sure how worthwhile that yakshave would be,

A yakshave that would be worthwhile is to kill off the macro compiletime_assert() completely - three is a crowd. It sounds like it would be implemented in terms of _Static_assert, but it's actually __attribute__(error). We can fold the definition of compiletime_assert into BUILD_BUG_ON_MSG.

The users in rwonce.h should just be changed to static_assert, and then there are very few random users left, which can either be static_assert or BUILD_BUG_ON_MSG.

Why do we even have a no-op version if !__OPTIMIZE__? AFAIK there's no CONFIG_O0 option, and such a build wouldn't be interesting at all - it can't be expected to boot, and it would likely throw warnings left and right.

Rasmus

#9 lore raw
Quoted 6 lines · Rasmus Villemoes

A yakshave that would be worthwhile is to kill off the macro compiletime_assert() completely - three is a crowd. It sounds like it would be implemented in terms of _Static_assert, but it's actually __attribute__(error). We can fold the definition of compiletime_assert into BUILD_BUG_ON_MSG.

Agreed, two should be enough.

Quoted 4 lines

Why do we even have a no-op version if !__OPTIMIZE__? AFAIK there's no CONFIG_O0 option, and such a build wouldn't be interesting at all - it can't be expected to boot, and it would likely throw warnings left and right.

Yeah, I don't think it would compile as it is anyway.

Perhaps it is there for some kind of tooling? For a static analyzer or something like sparse (if it didn't have its own define)...

But yeah, every use of it should have a comment explaining why it is there, like crypto/jitterentropy.c does. There are a couple without it.

Cheers,
Miguel

#12 lore raw
In reply to Miguel Ojeda
Quoted 3 lines · Miguel Ojeda

__compiletime_assert declares a fake extern function which appears (to the compiler) to be called when the test fails.

Queuing this one through my tree.

Cheers,
Miguel

#13 lore raw
In reply to Miguel Ojeda
Quoted 8 lines · Miguel Ojeda

__compiletime_assert declares a fake extern function which appears (to the compiler) to be called when the test fails.

Therefore, compilers may emit possibly-uninitialized warnings in some cases, even if it will be an error anyway (for compilers supporting the error attribute, e.g. GCC and Clang >= 14) or a link failure (for those that do not, e.g. Clang < 14).

Quoted 7 lines

Annotating the fake function as __noreturn gives them the information they need to avoid the warning, e.g. see https://godbolt.org/z/x1v69jjYY.

This patch is kind of a headache for Smatch.

The patch basically turns BUILD_BUG() into BUG(). That's fine.

But it also turn BUILD_BUG_ON(!__builtin_constant_p(foo) into a BUG_ON(). A lot of places call BUILD_BUG_ON() to ensure that functions are inlined and the build breaks if not. For example, the FIELD_GET() macro will trigger a build bug if "mask" is not a compile time constant.

   295  static __always_inline u32 cal_read_field(struct cal_dev *cal, u32 offset, u32 mask)
   296  {
   297          return FIELD_GET(mask, cal_read(cal, offset));
   298  }

Unfortunately, Smatch doesn't respect the __always_inline annotation so now it thinks cal_read_field calls BUG() and records it as a noreturn function. And all the callers get marked as noreturn functions as well.

Could we make it so that BUILD_BUG() is a noreturn function and BUILD_BUG_ON() is left as it was?

regards,
dan carpenter

#14 lore raw

Never mind. I can just add some code to Smatch to ignore compile time asserts.

regards,
dan carpenter