Skip to contentSkip to conversation

linux-next: tree build failure

PATCH In review Jan Beulich 8 participants 25 messages Sep 29, 2009 – Nov 6, 2009 Linux-next tree

lore
Share & tools
lore
#0 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard 09/29/09 2:00 AM >>>

First, I think there is a real bug here, and the code should read like this (to match the comment):

   /* type has to be known at build time for optimization */
(quoted diff) +1 -1
-    BUILD_BUG_ON(__builtin_constant_p(type));
+    BUILD_BUG_ON(!__builtin_constant_p(type));

However, I get the same build error *both* ways, i.e. __builtin_constant_p(type) evaluates to both 0 and 1? Either that, or the new BUILD_BUG_ON() macro isn't working...

No, at this point of the compilation process it's neither zero nor one, it's simply considered non-constant by the compiler at that stage (this builtin is used for optimization, not during parsing, and the error gets generated when the body of the function gets parsed, not when code gets generated from it).

Jan

#1 lore raw
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard  09/29/09 2:00 AM >>>

First, I think there is a real bug here, and the code should read like this (to match the comment):
   /* type has to be known at build time for optimization */
-    BUILD_BUG_ON(__builtin_constant_p(type));
+    BUILD_BUG_ON(!__builtin_constant_p(type));

However, I get the same build error *both* ways, i.e. __builtin_constant_p(type) evaluates to both 0 and 1? Either that, or the new BUILD_BUG_ON() macro isn't working...

Quoted 7 lines

No, at this point of the compilation process it's neither zero nor one, it's simply considered non-constant by the compiler at that stage (this builtin is used for optimization, not during parsing, and the error gets generated when the body of the function gets parsed, not when code gets generated from it).

Jan

then maybe

if(__builtin_constant_p(type))

        BUILD_BUG_ON(1);

would work?

Roel

#2 lore raw
Quoted 19 lines
Quoted 1 line
Quoted 1 line

roel kluin <[email protected]> 29.09.09 11:51 >>>

Quoted 17 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard 09/29/09 2:00 AM >>>

First, I think there is a real bug here, and the code should read like this (to match the comment):

   /* type has to be known at build time for optimization */
(quoted diff) +1 -1
-    BUILD_BUG_ON(__builtin_constant_p(type));
+    BUILD_BUG_ON(!__builtin_constant_p(type));

However, I get the same build error *both* ways, i.e. __builtin_constant_p(type) evaluates to both 0 and 1? Either that, or the new BUILD_BUG_ON() macro isn't working...

No, at this point of the compilation process it's neither zero nor one, it's simply considered non-constant by the compiler at that stage (this builtin is used for optimization, not during parsing, and the error gets generated when the body of the function gets parsed, not when code gets generated from it).

Quoted 8 lines
Quoted 1 line

Jan

then maybe

if(__builtin_constant_p(type))

       BUILD_BUG_ON(1);

would work?

Definitely not - this would result in the compiler *always* generating an error.

Jan

#3 lore raw
In reply to Jan Beulich
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard 09/29/09 2:00 AM >>>

First, I think there is a real bug here, and the code should read like this (to match the comment):

   /* type has to be known at build time for optimization */
(quoted diff) +1 -1
-    BUILD_BUG_ON(__builtin_constant_p(type));
+    BUILD_BUG_ON(!__builtin_constant_p(type));

However, I get the same build error *both* ways, i.e. __builtin_constant_p(type) evaluates to both 0 and 1? Either that, or the new BUILD_BUG_ON() macro isn't working...

Quoted 5 lines

No, at this point of the compilation process it's neither zero nor one, it's simply considered non-constant by the compiler at that stage (this builtin is used for optimization, not during parsing, and the error gets generated when the body of the function gets parsed, not when code gets generated from it).

I think I see what you're saying. Do you have a fix to suggest?

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#4 lore raw
Quoted 13 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard <[email protected]> 30.09.09 01:39 >>>

Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard 09/29/09 2:00 AM >>>

First, I think there is a real bug here, and the code should read like this (to match the comment):

   /* type has to be known at build time for optimization */
(quoted diff) +1 -1
-    BUILD_BUG_ON(__builtin_constant_p(type));
+    BUILD_BUG_ON(!__builtin_constant_p(type));

However, I get the same build error *both* ways, i.e. __builtin_constant_p(type) evaluates to both 0 and 1? Either that, or the new BUILD_BUG_ON() macro isn't working...

Quoted 7 lines
Quoted 5 lines

No, at this point of the compilation process it's neither zero nor one, it's simply considered non-constant by the compiler at that stage (this builtin is used for optimization, not during parsing, and the error gets generated when the body of the function gets parsed, not when code gets generated from it).

I think I see what you're saying. Do you have a fix to suggest?

The one Rusty suggested the other day may help here. I don't like it as a drop-in replacement for BUILD_BUG_ON() though (due to it deferring the error generated to the linking stage), I'd rather view this as an improvement to MAYBE_BUILD_BUG_ON() (which should then be used here).

Jan

#5 lore raw
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard <[email protected]> 30.09.09 01:39 >>>

Quoted 16 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard 09/29/09 2:00 AM >>>

First, I think there is a real bug here, and the code should read like this (to match the comment):

   /* type has to be known at build time for optimization */
(quoted diff) +1 -1
-    BUILD_BUG_ON(__builtin_constant_p(type));
+    BUILD_BUG_ON(!__builtin_constant_p(type));

However, I get the same build error *both* ways, i.e. __builtin_constant_p(type) evaluates to both 0 and 1? Either that, or the new BUILD_BUG_ON() macro isn't working...

No, at this point of the compilation process it's neither zero nor one, it's simply considered non-constant by the compiler at that stage (this builtin is used for optimization, not during parsing, and the error gets generated when the body of the function gets parsed, not when code gets generated from it).

Quoted 7 lines
Quoted 1 line

I think I see what you're saying. Do you have a fix to suggest?

The one Rusty suggested the other day may help here. I don't like it as a drop-in replacement for BUILD_BUG_ON() though (due to it deferring the error generated to the linking stage), I'd rather view this as an improvement to MAYBE_BUILD_BUG_ON() (which should then be used here).

Can you be more specific?

I have no idea what Rusty suggested where. I can't even guess what MAYBE_BUILD_BUG_ON() is supposed to do (sounds like a terrible name).

All I know is that this used to build...

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#6 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard <[email protected]> 02.10.09 17:48 >>>

Quoted 5 lines · Jan Beulich

The one Rusty suggested the other day may help here. I don't like it as a drop-in replacement for BUILD_BUG_ON() though (due to it deferring the error generated to the linking stage), I'd rather view this as an improvement to MAYBE_BUILD_BUG_ON() (which should then be used here).

Can you be more specific?

I have no idea what Rusty suggested where. I can't even guess what

I'm attaching Rusty's response I was referring to.

Quoted 1 line

MAYBE_BUILD_BUG_ON() is supposed to do (sounds like a terrible name).

Agreed - but presumably better than just deleting the bogus instances altogether...

Jan

Quoted 6 lines · Jan Beulich

gcc permitting variable length arrays makes the current construct used for BUILD_BUG_ON() useless, as that doesn't produce any diagnostic if the controlling expression isn't really constant. Instead, this patch makes it so that a bit field gets used here. Consequently, those uses where the condition isn't really constant now also need fixing.

Quoted 7 lines

Note that in the gfp.h, kmemcheck.h, and virtio_config.h cases MAYBE_BUILD_BUG_ON() really just serves documentation purposes - even if the expression is compile time constant (__builtin_constant_p() yields true), the array is still deemed of variable length by gcc, and hence the whole expression doesn't have the intended effect.

We used to use an undefined symbol here; diagnostics are worse but it catches more stuff.

Perhaps a hybrid is the way to go?

#ifndef __OPTIMIZE__
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) #else
/* If it's a constant, catch it at compile time, otherwise at link time. */ extern int __build_bug_on_failed;

#define BUILD_BUG_ON(condition) \
	do { 								\
		((void)sizeof(char[1 - 2*!!(condition)]));		\
		if (condition) __build_bug_on_failed = 1;		\
	} while(0)

#endif

Thanks,
Rusty.

#7 lore raw

Rusty's version of BUILD_BUG_ON() does indeed fix the build break, and also exposes the bug in kvmppc_account_exit_stat(). So to recap:

original: built but didn't work
Jan's: doesn't build
Rusty's: builds and works

Where do you want to go from here?

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
Quoted 63 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard <[email protected]> 02.10.09 17:48 >>>

Quoted 5 lines · Jan Beulich

The one Rusty suggested the other day may help here. I don't like it as a drop-in replacement for BUILD_BUG_ON() though (due to it deferring the error generated to the linking stage), I'd rather view this as an improvement to MAYBE_BUILD_BUG_ON() (which should then be used here).

Can you be more specific?

I have no idea what Rusty suggested where. I can't even guess what

I'm attaching Rusty's response I was referring to.

Quoted 1 line

MAYBE_BUILD_BUG_ON() is supposed to do (sounds like a terrible name).

Agreed - but presumably better than just deleting the bogus instances altogether...

Jan
email message attachment

Quoted 42 lines

-------- Forwarded Message --------
From: Rusty Russell <[email protected]>
To: Jan Beulich <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] fix BUILD_BUG_ON() and a couple of bogus uses of it
Date: Wed, 23 Sep 2009 10:27:00 +0930

Quoted 6 lines · Jan Beulich

gcc permitting variable length arrays makes the current construct used for BUILD_BUG_ON() useless, as that doesn't produce any diagnostic if the controlling expression isn't really constant. Instead, this patch makes it so that a bit field gets used here. Consequently, those uses where the condition isn't really constant now also need fixing.

Quoted 7 lines

Note that in the gfp.h, kmemcheck.h, and virtio_config.h cases MAYBE_BUILD_BUG_ON() really just serves documentation purposes - even if the expression is compile time constant (__builtin_constant_p() yields true), the array is still deemed of variable length by gcc, and hence the whole expression doesn't have the intended effect.

We used to use an undefined symbol here; diagnostics are worse but it catches more stuff.

Perhaps a hybrid is the way to go?

#ifndef __OPTIMIZE__
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) #else
/* If it's a constant, catch it at compile time, otherwise at link time. */ extern int __build_bug_on_failed;

#define BUILD_BUG_ON(condition) \
	do { 								\
		((void)sizeof(char[1 - 2*!!(condition)]));		\
		if (condition) __build_bug_on_failed = 1;		\
	} while(0)

#endif

Thanks,
Rusty.

#8 lore raw
Quoted 8 lines · Hollis Blanchard

Rusty's version of BUILD_BUG_ON() does indeed fix the build break, and also exposes the bug in kvmppc_account_exit_stat(). So to recap:

original: built but didn't work
Jan's: doesn't build
Rusty's: builds and works

Where do you want to go from here?

Jan, what are your thoughts? Your BUILD_BUG_ON patch has broken the build, and we still need to fix it.

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#9 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard <[email protected]> 15.10.09 00:57 >>>

Quoted 8 lines · Hollis Blanchard

Rusty's version of BUILD_BUG_ON() does indeed fix the build break, and also exposes the bug in kvmppc_account_exit_stat(). So to recap:
=20
original: built but didn't work
Jan's: doesn't build
Rusty's: builds and works
=20
Where do you want to go from here?

Quoted 2 lines

Jan, what are your thoughts? Your BUILD_BUG_ON patch has broken the build, and we still need to fix it.

My perspective is that it just uncovered already existing brokenness. And honestly, I won't be able to get to look into this within the next days. = (And
btw., when I run into issues with other people's code changes, quite frequently I'm told to propose a patch, so I'm also having some philosophical problem understanding why I can't simply expect the same when people run into issues with changes I made, especially in cases like this where it wasn't me introducing the broken code.) So, if this can wait for a couple of days, I can try to find time to look into this. Otherwise, = I'd
rely on someone running into the actual issue to implement a solution.

Jan

#10 lore raw
Quoted 14 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line

Hollis Blanchard <[email protected]> 15.10.09 00:57 >>>

Quoted 3 lines · Hollis Blanchard

Rusty's version of BUILD_BUG_ON() does indeed fix the build break, and also exposes the bug in kvmppc_account_exit_stat(). So to recap:

Quoted 8 lines
Quoted 5 lines

original: built but didn't work
Jan's: doesn't build
Rusty's: builds and works

Where do you want to go from here?

Jan, what are your thoughts? Your BUILD_BUG_ON patch has broken the build, and we still need to fix it.

Quoted 9 lines

My perspective is that it just uncovered already existing brokenness. And honestly, I won't be able to get to look into this within the next days. (And btw., when I run into issues with other people's code changes, quite frequently I'm told to propose a patch, so I'm also having some philosophical problem understanding why I can't simply expect the same when people run into issues with changes I made, especially in cases like this where it wasn't me introducing the broken code.) So, if this can wait for a couple of days, I can try to find time to look into this. Otherwise, I'd rely on someone running into the actual issue to implement a solution.

Sorry, I thought it was clear, but to be more explicit: I propose the following patch, which replaces the current BUILD_BUG_ON implementation with Rusty's version.

include/linux/kernel.h +13 -12
@@ -677,18 +677,19 @@ struct sysinfo {
 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
 };
 
-/* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
-
-/* Force a compilation error if condition is constant and true */
-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
-
-/* Force a compilation error if condition is true, but also produce a
-   result (of value 0 and type size_t), so the expression can be used
-   e.g. in a structure initializer (or where-ever else comma expressions
-   aren't permitted). */
-#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
-#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+/* If it's a constant, catch it at compile time, otherwise at link time. */
+extern int __build_bug_on_failed;
+#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
+#define BUILD_BUG_ON(condition) \
+		do {                                                            \
+				((void)sizeof(char[1 - 2*!!(condition)]));              \
+				if (condition) __build_bug_on_failed = 1;               \
+		} while(0)
+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
+#endif
 
 /* Trap pasters of __FUNCTION__ at compile-time */
 #define __FUNCTION__ (__func__)
Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#11 lore raw
Quoted 6 lines · Hollis Blanchard
Quoted 1 line · Jan Beulich

My perspective is that it just uncovered already existing brokenness.

Sorry, I thought it was clear, but to be more explicit: I propose the following patch, which replaces the current BUILD_BUG_ON implementation with Rusty's version.

OK, I switched my brain back on. Yeah, I agree: we may still want BUILD_OR_RUNTIME_BUG_ON one day, but I like this.

It's just missing the giant comment that it needs :)

/**
* BUILD_BUG_ON - break compile if a condition is true.
* @cond: the condition which the compiler should know is false. *
* If you have some code which relies on certain constants being equal, or
* other compile-time-evaluated condition, you should use BUILD_BUG_ON to
* detect if someone changes it.
*
* The implementation uses gcc's reluctance to create a negative array, but
* gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
* to inline functions). So as a fallback we use the optimizer; if it can't
* prove the condition is false, it will cause a link error on the undefined
* "__build_bug_on_failed". This error is less neat, and can be harder to
* track down.
*/

Thanks!
Rusty.

#12 lore raw
Quoted 13 lines · Rusty Russell
Quoted 6 lines · Hollis Blanchard
Quoted 1 line · Jan Beulich

My perspective is that it just uncovered already existing brokenness.

Sorry, I thought it was clear, but to be more explicit: I propose the following patch, which replaces the current BUILD_BUG_ON implementation with Rusty's version.

OK, I switched my brain back on. Yeah, I agree: we may still want BUILD_OR_RUNTIME_BUG_ON one day, but I like this.

It's just missing the giant comment that it needs :)

Quoted 15 lines

/**
* BUILD_BUG_ON - break compile if a condition is true.
* @cond: the condition which the compiler should know is false. *
* If you have some code which relies on certain constants being equal, or
* other compile-time-evaluated condition, you should use BUILD_BUG_ON to
* detect if someone changes it.
*
* The implementation uses gcc's reluctance to create a negative array, but
* gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
* to inline functions). So as a fallback we use the optimizer; if it can't
* prove the condition is false, it will cause a link error on the undefined
* "__build_bug_on_failed". This error is less neat, and can be harder to
* track down.
*/

Do you want to put together a signed-off patch Rusty? It's your code, so I don't feel comfortable doing that.

Once we have that, can we remove the mysterious MAYBE_BUILD_BUG_ON statements introduced in previous patches? (Does it BUG or doesn't it??)

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#13 lore raw

PATCH 1/1 BUILD_BUG_ON: make it handle more cases

BUILD_BUG_ON used to use the optimizer to do code elimination or fail at link time; it was changed to first the size of a negative array (a nicer compile time error), then (in 8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.

bitfields: needs a literal constant at parse time, and can't be put under

	"if (__builtin_constant_p(x))" for example.

negative array: can handle anything, but if the compiler can't tell it's
a constant, silently has no effect.
link time: breaks link if the compiler can't determine the value, but the
linker output is not usually as informative as a compiler error.

If we use the negative-array-size method *and* the link time trick, we get the ability to use BUILD_BUG_ON() under __builtin_constant_p() branches, and maximal ability for the compiler to detect errors at build time.

include/linux/kernel.h +27 -6
@@ -683,12 +683,6 @@ struct sysinfo {
 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
 };
 
-/* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
-
-/* Force a compilation error if condition is constant and true */
-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
-
 /* Force a compilation error if condition is true, but also produce a
    result (of value 0 and type size_t), so the expression can be used
    e.g. in a structure initializer (or where-ever else comma expressions
@@ -696,6 +690,33 @@ struct sysinfo {
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
 
+/**
+ * BUILD_BUG_ON - break compile if a condition is true.
+ * @cond: the condition which the compiler should know is false.
+ *
+ * If you have some code which relies on certain constants being equal, or
+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
+ * detect if someone changes it.
+ *
+ * The implementation uses gcc's reluctance to create a negative array, but
+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
+ * to inline functions).  So as a fallback we use the optimizer; if it can't
+ * prove the condition is false, it will cause a link error on the undefined
+ * "__build_bug_on_failed".  This error message can be harder to track down
+ * though, hence the two different methods.
+ */
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+extern int __build_bug_on_failed;
+#define BUILD_BUG_ON(condition)					\
+	do {							\
+		((void)sizeof(char[1 - 2*!!(condition)]));	\
+		if (condition) __build_bug_on_failed = 1;	\
+	} while(0)
+#endif
+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
+
 /* Trap pasters of __FUNCTION__ at compile-time */
 #define __FUNCTION__ (__func__)
 
#14 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

Quoted 48 lines · Rusty Russell

BUILD_BUG_ON used to use the optimizer to do code elimination or fail at link time; it was changed to first the size of a negative array (a nicer compile time error), then (in 8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.

bitfields: needs a literal constant at parse time, and can't be put under

	"if (__builtin_constant_p(x))" for example.

negative array: can handle anything, but if the compiler can't tell it's
a constant, silently has no effect.
link time: breaks link if the compiler can't determine the value, but the
linker output is not usually as informative as a compiler error.

If we use the negative-array-size method *and* the link time trick, we get the ability to use BUILD_BUG_ON() under __builtin_constant_p() branches, and maximal ability for the compiler to detect errors at build time.

include/linux/kernel.h +9 -6
@@ -683,12 +683,6 @@ struct sysinfo {
 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
 };
 
-/* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
-
-/* Force a compilation error if condition is constant and true */
-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
-
 /* Force a compilation error if condition is true, but also produce a
   result (of value 0 and type size_t), so the expression can be used
   e.g. in a structure initializer (or where-ever else comma expressions
@@ -696,6 +690,33 @@ struct sysinfo {
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
 
+/**
+ * BUILD_BUG_ON - break compile if a condition is true.
+ * @cond: the condition which the compiler should know is false.
+ *
+ * If you have some code which relies on certain constants being equal, or
+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
+ * detect if someone changes it.
+ *
+ * The implementation uses gcc's reluctance to create a negative array, but

include/linux/kernel.h struct sysinfo { · line 711

Quoted 10 lines
include/linux/kernel.h +10 -0
+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
+ * to inline functions).  So as a fallback we use the optimizer; if it can't
+ * prove the condition is false, it will cause a link error on the undefined
+ * "__build_bug_on_failed".  This error message can be harder to track down
+ * though, hence the two different methods.
+ */
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+extern int __build_bug_on_failed;

Hmm, what exactly is __build_bug_on_failed?

Quoted 16 lines
include/linux/kernel.h +8 -1
+#define BUILD_BUG_ON(condition)					\
+	do {							\
+		((void)sizeof(char[1 - 2*!!(condition)]));	\
+		if (condition) __build_bug_on_failed = 1;	\
+	} while(0)
+#endif
+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
+
 /* Trap pasters of __FUNCTION__ at compile-time */
 #define __FUNCTION__ (__func__)
 
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 1 line
Live like a child, think like the god.
#15 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

Quoted 58 lines · Américo Wang
Quoted 57 lines · Rusty Russell

BUILD_BUG_ON used to use the optimizer to do code elimination or fail at link time; it was changed to first the size of a negative array (a nicer compile time error), then (in 8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.

bitfields: needs a literal constant at parse time, and can't be put under

	"if (__builtin_constant_p(x))" for example.

negative array: can handle anything, but if the compiler can't tell it's
a constant, silently has no effect.
link time: breaks link if the compiler can't determine the value, but the
linker output is not usually as informative as a compiler error.

If we use the negative-array-size method *and* the link time trick, we get the ability to use BUILD_BUG_ON() under __builtin_constant_p() branches, and maximal ability for the compiler to detect errors at build time.

include/linux/kernel.h +9 -6
@@ -683,12 +683,6 @@ struct sysinfo {
 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
 };
 
-/* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
-
-/* Force a compilation error if condition is constant and true */
-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
-
 /* Force a compilation error if condition is true, but also produce a
   result (of value 0 and type size_t), so the expression can be used
   e.g. in a structure initializer (or where-ever else comma expressions
@@ -696,6 +690,33 @@ struct sysinfo {
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
 
+/**
+ * BUILD_BUG_ON - break compile if a condition is true.
+ * @cond: the condition which the compiler should know is false.
+ *
+ * If you have some code which relies on certain constants being equal, or
+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
+ * detect if someone changes it.
+ *
+ * The implementation uses gcc's reluctance to create a negative array,

but
+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
+ * to inline functions). So as a fallback we use the optimizer; if it can't
+ * prove the condition is false, it will cause a link error on the undefined
+ * "__build_bug_on_failed". This error message can be harder to track down

Quoted 8 lines

include/linux/kernel.h struct sysinfo { · line 711

Quoted 6 lines

+ * though, hence the two different methods.
+ */

include/linux/kernel.h +4 -0
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+extern int __build_bug_on_failed;

Hmm, what exactly is __build_bug_on_failed?

Well, we haven't added a definition for it in this patch. I'm sure grep will tell you it wasn't defined before hand either. So any reference to it is an error - which will be reported at link time.

Quoted 4 lines

include/linux/kernel.h struct sysinfo { · line 715

Quoted 4 lines
include/linux/kernel.h +4 -0
+#define BUILD_BUG_ON(condition)					\
+	do {							\
+		((void)sizeof(char[1 - 2*!!(condition)]));	\
+		if (condition) __build_bug_on_failed = 1;	\

If "condition" is known false at compile time, gcc -O will eliminate the code which refers to __build_bug_on_failed. If it's not proved to be false - it will break the build, which is exactly what we want BUILD_BUG_ON to do.

Quoted 7 lines
Quoted 7 lines
include/linux/kernel.h +4 -0
+	} while(0)
+#endif
+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
+

/* Trap pasters of __FUNCTION__ at compile-time */ #define __FUNCTION__ (__func__)

Reviewed-by
#16 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

Quoted 77 lines · Alan Jenkins
Quoted 58 lines · Américo Wang
Quoted 57 lines · Rusty Russell

BUILD_BUG_ON used to use the optimizer to do code elimination or fail at link time; it was changed to first the size of a negative array (a nicer compile time error), then (in 8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.

bitfields: needs a literal constant at parse time, and can't be put under "if (__builtin_constant_p(x))" for example.
negative array: can handle anything, but if the compiler can't tell it's a constant, silently has no effect.
link time: breaks link if the compiler can't determine the value, but the linker output is not usually as informative as a compiler error.

If we use the negative-array-size method *and* the link time trick, we get the ability to use BUILD_BUG_ON() under __builtin_constant_p() branches, and maximal ability for the compiler to detect errors at build time.

include/linux/kernel.h +0 -0
@@ -683,12 +683,6 @@ struct sysinfo {

     char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ };

include/linux/kernel.h +0 -6
-/* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
-
-/* Force a compilation error if condition is constant and true */
-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
-

/* Force a compilation error if condition is true, but also produce a result (of value 0 and type size_t), so the expression can be used e.g. in a structure initializer (or where-ever else comma expressions

include/linux/kernel.h +9 -0
@@ -696,6 +690,33 @@ struct sysinfo {
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
 
+/**
+ * BUILD_BUG_ON - break compile if a condition is true.
+ * @cond: the condition which the compiler should know is false.
+ *
+ * If you have some code which relies on certain constants being equal, or
+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
+ * detect if someone changes it.
+ *
+ * The implementation uses gcc's reluctance to create a negative array,

but
+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
+ * to inline functions).  So as a fallback we use the optimizer; if it can't
+ * prove the condition is false, it will cause a link error on the undefined
+ * "__build_bug_on_failed".  This error message can be harder to track down

Quoted 8 lines

include/linux/kernel.h struct sysinfo { · line 711

Quoted 6 lines

+ * though, hence the two different methods.
+ */

include/linux/kernel.h +4 -0
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+extern int __build_bug_on_failed;

Hmm, what exactly is __build_bug_on_failed?

Well, we haven't added a definition for it in this patch.  I'm sure grep will tell you it wasn't defined before hand either.  So any reference to it is an error - which will be reported at link time.

Quoted 4 lines
Quoted 4 lines

+#define BUILD_BUG_ON(condition)                                      \
+     do {                                                    \
+             ((void)sizeof(char[1 - 2*!!(condition)]));      \
+             if (condition) __build_bug_on_failed = 1;       \

Quoted 4 lines

If "condition" is known false at compile time, gcc -O will eliminate the code which refers to __build_bug_on_failed.  If it's not proved to be false - it will break the build, which is exactly what we want BUILD_BUG_ON to do.

Ah, clever trick! Got it.
Thanks!

Acked-by on v1 1/1
#17 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

In reply to Rusty Russell
Quoted 12 lines · Rusty Russell

BUILD_BUG_ON used to use the optimizer to do code elimination or fail at link time; it was changed to first the size of a negative array (a nicer compile time error), then (in 8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.

bitfields: needs a literal constant at parse time, and can't be put under

	"if (__builtin_constant_p(x))" for example.

negative array: can handle anything, but if the compiler can't tell it's
a constant, silently has no effect.
link time: breaks link if the compiler can't determine the value, but the
linker output is not usually as informative as a compiler error.

Quoted 6 lines

If we use the negative-array-size method *and* the link time trick, we get the ability to use BUILD_BUG_ON() under __builtin_constant_p() branches, and maximal ability for the compiler to detect errors at build time.

Thanks Rusty, this indeed fixes the problem.

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#18 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

In reply to Rusty Russell
Quoted 58 lines · Rusty Russell

BUILD_BUG_ON used to use the optimizer to do code elimination or fail at link time; it was changed to first the size of a negative array (a nicer compile time error), then (in 8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.

bitfields: needs a literal constant at parse time, and can't be put under

	"if (__builtin_constant_p(x))" for example.

negative array: can handle anything, but if the compiler can't tell it's
a constant, silently has no effect.
link time: breaks link if the compiler can't determine the value, but the
linker output is not usually as informative as a compiler error.

If we use the negative-array-size method *and* the link time trick, we get the ability to use BUILD_BUG_ON() under __builtin_constant_p() branches, and maximal ability for the compiler to detect errors at build time.

include/linux/kernel.h +19 -6
@@ -683,12 +683,6 @@ struct sysinfo {
 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
 };
 
-/* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
-
-/* Force a compilation error if condition is constant and true */
-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
-
 /* Force a compilation error if condition is true, but also produce a
    result (of value 0 and type size_t), so the expression can be used
    e.g. in a structure initializer (or where-ever else comma expressions
@@ -696,6 +690,33 @@ struct sysinfo {
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
 
+/**
+ * BUILD_BUG_ON - break compile if a condition is true.
+ * @cond: the condition which the compiler should know is false.
+ *
+ * If you have some code which relies on certain constants being equal, or
+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
+ * detect if someone changes it.
+ *
+ * The implementation uses gcc's reluctance to create a negative array, but
+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
+ * to inline functions).  So as a fallback we use the optimizer; if it can't
+ * prove the condition is false, it will cause a link error on the undefined
+ * "__build_bug_on_failed".  This error message can be harder to track down
+ * though, hence the two different methods.
+ */
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+extern int __build_bug_on_failed;

include/linux/kernel.h struct sysinfo { · line 721

Quoted 10 lines
include/linux/kernel.h +8 -0
+#define BUILD_BUG_ON(condition)					\
+	do {							\
+		((void)sizeof(char[1 - 2*!!(condition)]));	\
+		if (condition) __build_bug_on_failed = 1;	\
+	} while(0)
+#endif
+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
+
 /* Trap pasters of __FUNCTION__ at compile-time */
 #define __FUNCTION__ (__func__)

What's the state of this patch?

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#19 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

In reply to Rusty Russell

Hi Rusty,

include/linux/kernel.h struct sysinfo { · line 719

Quoted 13 lines · On Tue, 20 Oct 2009 14:15:33 +1030 Rusty Russell <[email protected]> wrote:
include/linux/kernel.h +12 -0
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+extern int __build_bug_on_failed;
+#define BUILD_BUG_ON(condition)					\
+	do {							\
+		((void)sizeof(char[1 - 2*!!(condition)]));	\
+		if (condition) __build_bug_on_failed = 1;	\
+	} while(0)
+#endif
+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
+

I decided to try this in linux-next, but an x86_64 allmodconfig build gave this (gcc 4.4.0):

ERROR: "__build_bug_on_failed" [drivers/net/virtio_net.ko] undefined!
ERROR: "__build_bug_on_failed" [drivers/block/virtio_blk.ko] undefined!

I assume that this is caused by the "MAYBE_BUILD_BUG_ON(fbit >= 32)" in virtio_has_feature() (in include/linux/virtio_config.h) which is called all over the place. Unfortunately, virtio_has_feature() gets uninlined in those two files ...

I have taken the patch back out again for today.

Signature · 3 lines
Cheers,
Stephen Rothwell                    [email protected]
http://www.canb.auug.org.au/~sfr/
#20 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

Quoted 20 lines · Stephen Rothwell

Hi Rusty,

include/linux/kernel.h struct sysinfo { · line 719

Quoted 13 lines · On Tue, 20 Oct 2009 14:15:33 +1030 Rusty Russell <[email protected]> wrote:
include/linux/kernel.h +12 -0
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+extern int __build_bug_on_failed;
+#define BUILD_BUG_ON(condition)					\
+	do {							\
+		((void)sizeof(char[1 - 2*!!(condition)]));	\
+		if (condition) __build_bug_on_failed = 1;	\
+	} while(0)
+#endif
+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
+

I decided to try this in linux-next, but an x86_64 allmodconfig build gave this (gcc 4.4.0):

Quoted 7 lines

ERROR: "__build_bug_on_failed" [drivers/net/virtio_net.ko] undefined!
ERROR: "__build_bug_on_failed" [drivers/block/virtio_blk.ko] undefined!

I assume that this is caused by the "MAYBE_BUILD_BUG_ON(fbit >= 32)" in virtio_has_feature() (in include/linux/virtio_config.h) which is called all over the place. Unfortunately, virtio_has_feature() gets uninlined in those two files ...

Huh? virtio_has_feature does:

	if (__builtin_constant_p(fbit))
		BUILD_BUG_ON(fbit >= 32);
	else
		BUG_ON(fbit >= 32);

So, if it's not a constant, gcc should throw away that first branch. If it is, it should optimize it away (and no, these are not real bugs AFAICT).

I did a 4.3.3 allmodconfig with this patch on 64-bit a few days back and all was fine. Will try 4.4.0 now.

Thanks,
Rusty.

#21 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

Quoted 2 lines · Rusty Russell

I did a 4.3.3 allmodconfig with this patch on 64-bit a few days back and all was fine. Will try 4.4.0 now.

4.4.1 seems OK (Ubuntu).

This is annoying. But I'll withdraw the patches; if there's another reason that 4.4.0 is bad, we can ban it and reintroduce this. I don't think that breaking this hack is enough to declare 4.4.0 verboten.

Thanks,
Rusty.

#22 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

In reply to Rusty Russell

Hi Rusty,

Quoted 7 lines · On Thu, 5 Nov 2009 16:58:36 +1030 Rusty Russell <[email protected]> wrote:

Huh? virtio_has_feature does:

	if (__builtin_constant_p(fbit))
		BUILD_BUG_ON(fbit >= 32);
	else
		BUG_ON(fbit >= 32);

In Linus' tree (and linux-next) it looks like this:

static inline bool virtio_has_feature(const struct virtio_device *vdev,
unsigned int fbit)

{
	/* Did you forget to fix assumptions on max features? */
	MAYBE_BUILD_BUG_ON(fbit >= 32);

	if (fbit < VIRTIO_TRANSPORT_F_START)
		virtio_check_driver_offered_feature(vdev, fbit);

	return test_bit(fbit, vdev->features);
}
Quoted 2 lines

So, if it's not a constant, gcc should throw away that first branch. If it is, it should optimize it away (and no, these are not real bugs AFAICT).

Your version above may well fix the problem. Alternatively marking it __always_inline way work as well.

Quoted 2 lines

I did a 4.3.3 allmodconfig with this patch on 64-bit a few days back and all was fine. Will try 4.4.0 now.

4.4 is more problematic.

Signature · 3 lines
Cheers,
Stephen Rothwell                    [email protected]
http://www.canb.auug.org.au/~sfr/
#23 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

Quoted 4 lines · Stephen Rothwell

Hi Rusty,

Quoted 1 line · On Thu, 5 Nov 2009 16:58:36 +1030 Rusty Russell…
Quoted 8 lines
Quoted 6 lines

Huh? virtio_has_feature does:

	if (__builtin_constant_p(fbit))
		BUILD_BUG_ON(fbit >= 32);
	else
		BUG_ON(fbit >= 32);

In Linus' tree (and linux-next) it looks like this:

Ah. My patch series fixes that as part of removing MAYBE_BUILD_BUG_ON.

I've put both in for linux-next.

Cheers,
Rusty.

#24 lore raw

Re: [PATCH] BUILD_BUG_ON: make it handle more cases

In reply to Rusty Russell
Quoted 4 lines · Rusty Russell

BUILD_BUG_ON used to use the optimizer to do code elimination or fail at link time; it was changed to first the size of a negative array (a nicer compile time error), then (in 8c87df457cb58fe75b9b893007917cf8095660a0) to a bitfield.

What's the status with this patch ? The lack of it breaks my KVM stuff in powerpc...

Cheers,
Ben.

Quoted 68 lines

bitfields: needs a literal constant at parse time, and can't be put under

	"if (__builtin_constant_p(x))" for example.

negative array: can handle anything, but if the compiler can't tell it's
a constant, silently has no effect.
link time: breaks link if the compiler can't determine the value, but the
linker output is not usually as informative as a compiler error.

If we use the negative-array-size method *and* the link time trick, we get the ability to use BUILD_BUG_ON() under __builtin_constant_p() branches, and maximal ability for the compiler to detect errors at build time.

include/linux/kernel.h +27 -6
@@ -683,12 +683,6 @@ struct sysinfo {
 	char _f[20-2*sizeof(long)-sizeof(int)];	/* Padding: libc5 uses this.. */
 };
 
-/* Force a compilation error if condition is true */
-#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
-
-/* Force a compilation error if condition is constant and true */
-#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
-
 /* Force a compilation error if condition is true, but also produce a
    result (of value 0 and type size_t), so the expression can be used
    e.g. in a structure initializer (or where-ever else comma expressions
@@ -696,6 +690,33 @@ struct sysinfo {
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
 
+/**
+ * BUILD_BUG_ON - break compile if a condition is true.
+ * @cond: the condition which the compiler should know is false.
+ *
+ * If you have some code which relies on certain constants being equal, or
+ * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
+ * detect if someone changes it.
+ *
+ * The implementation uses gcc's reluctance to create a negative array, but
+ * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
+ * to inline functions).  So as a fallback we use the optimizer; if it can't
+ * prove the condition is false, it will cause a link error on the undefined
+ * "__build_bug_on_failed".  This error message can be harder to track down
+ * though, hence the two different methods.
+ */
+#ifndef __OPTIMIZE__
+#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
+#else
+extern int __build_bug_on_failed;
+#define BUILD_BUG_ON(condition)					\
+	do {							\
+		((void)sizeof(char[1 - 2*!!(condition)]));	\
+		if (condition) __build_bug_on_failed = 1;	\
+	} while(0)
+#endif
+#define MAYBE_BUILD_BUG_ON(condition) BUILD_BUG_ON(condition)
+
 /* Trap pasters of __FUNCTION__ at compile-time */
 #define __FUNCTION__ (__func__)
 

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev