public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
@ 2009-12-20 16:53 Andi Kleen
  2009-12-21 10:32 ` Rusty Russell
  2009-12-21 16:48 ` Arnd Bergmann
  0 siblings, 2 replies; 6+ messages in thread
From: Andi Kleen @ 2009-12-20 16:53 UTC (permalink / raw)
  To: torvalds, linux-kernel, jbeulich, rusty, rguenther; +Cc: arnd

Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot

The BUILD_BUG_ON in compat_ioctl_check_table() fails
with a recent gcc mainline snapshot (gcc version 4.5.0 20091219)
(GCC)), even though it works with older compilers.

 const int max = ARRAY_SIZE(ioctl_pointer) - 1;
 BUILD_BUG_ON(max >= (1 << 16));

I replaced the BUILD_BUG_ON with a old style extern reference and 
that works.

That shows that the actual expression is evaluated ok and something must be 
wrong with the BUILD_BUG_ON macro itself. Maybe Rusty's rework
will fix that (I haven't tried but it's probably worth investigating) 

I filed a bug on the compiler too 
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42439

According to Joseph Myers the problem is that even when declared
with const "max" is not a constant expression in the C standard sense,
so the code is indeed incorrect.

Anyways with this workaround a relatively standard defconfig like 
configuration and a 64bit allyes configuration builds again with gcc 4.5

Cc: jbeulich@novell.com
Cc: rusty@rustcorp.com.au
Cc: rguenther@suse.de

Signed-off-by: Andi Kleen <ak@linux.intel.com>

---
 fs/compat_ioctl.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Index: linux-2.6.33-rc1-ak/fs/compat_ioctl.c
===================================================================
--- linux-2.6.33-rc1-ak.orig/fs/compat_ioctl.c
+++ linux-2.6.33-rc1-ak/fs/compat_ioctl.c
@@ -1649,8 +1649,10 @@ static int compat_ioctl_check_table(unsi
 {
 	int i;
 	const int max = ARRAY_SIZE(ioctl_pointer) - 1;
+	extern void __ioctl_pointer_too_large(void);
 
-	BUILD_BUG_ON(max >= (1 << 16));
+	if (max >= (1 << 16))
+		__ioctl_pointer_too_large();
 
 	/* guess initial offset into table, assuming a
 	   normalized distribution */

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

* Re: [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
  2009-12-20 16:53 [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot Andi Kleen
@ 2009-12-21 10:32 ` Rusty Russell
  2009-12-21 10:51   ` Andi Kleen
  2009-12-21 16:48 ` Arnd Bergmann
  1 sibling, 1 reply; 6+ messages in thread
From: Rusty Russell @ 2009-12-21 10:32 UTC (permalink / raw)
  To: Andi Kleen; +Cc: torvalds, linux-kernel, jbeulich, rguenther, arnd

On Mon, 21 Dec 2009 03:23:15 am Andi Kleen wrote:
> Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
> 
> The BUILD_BUG_ON in compat_ioctl_check_table() fails
> with a recent gcc mainline snapshot (gcc version 4.5.0 20091219)
> (GCC)), even though it works with older compilers.

We have a proper fix for BUILD_BUG_ON which I sent Linus a pull req for,
but I think it was too late:

	git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus.git BUILD_BUG_ON

It's not really high priority, but it'd be nice to fix this properly.

Thanks,
Rusty.

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

* Re: [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
  2009-12-21 10:32 ` Rusty Russell
@ 2009-12-21 10:51   ` Andi Kleen
  2009-12-21 23:44     ` Rusty Russell
  0 siblings, 1 reply; 6+ messages in thread
From: Andi Kleen @ 2009-12-21 10:51 UTC (permalink / raw)
  To: Rusty Russell
  Cc: Andi Kleen, torvalds, linux-kernel, jbeulich, rguenther, arnd

On Mon, Dec 21, 2009 at 09:02:39PM +1030, Rusty Russell wrote:
> On Mon, 21 Dec 2009 03:23:15 am Andi Kleen wrote:
> > Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
> > 
> > The BUILD_BUG_ON in compat_ioctl_check_table() fails
> > with a recent gcc mainline snapshot (gcc version 4.5.0 20091219)
> > (GCC)), even though it works with older compilers.
> 
> We have a proper fix for BUILD_BUG_ON which I sent Linus a pull req for,
> but I think it was too late:
> 
> 	git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus.git BUILD_BUG_ON
> 
> It's not really high priority, but it'd be nice to fix this properly.

AFAIK the code I fixed was not correct in the first place because
"const int xxx" is not a constant expression in the standards sense.

So it's orthogonal to whatever changes you come up.

Building on gcc 4.5 is at least medium priority I would say.

-Andi

-- 
ak@linux.intel.com -- Speaking for myself only.

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

* Re: [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
  2009-12-20 16:53 [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot Andi Kleen
  2009-12-21 10:32 ` Rusty Russell
@ 2009-12-21 16:48 ` Arnd Bergmann
  1 sibling, 0 replies; 6+ messages in thread
From: Arnd Bergmann @ 2009-12-21 16:48 UTC (permalink / raw)
  To: Andi Kleen; +Cc: torvalds, linux-kernel, jbeulich, rusty, rguenther

On Sunday 20 December 2009, Andi Kleen wrote:
> ===================================================================
> --- linux-2.6.33-rc1-ak.orig/fs/compat_ioctl.c
> +++ linux-2.6.33-rc1-ak/fs/compat_ioctl.c
> @@ -1649,8 +1649,10 @@ static int compat_ioctl_check_table(unsi
>  {
>         int i;
>         const int max = ARRAY_SIZE(ioctl_pointer) - 1;
> +       extern void __ioctl_pointer_too_large(void);
>  
> -       BUILD_BUG_ON(max >= (1 << 16));
> +       if (max >= (1 << 16))
> +               __ioctl_pointer_too_large();
>  
>         /* guess initial offset into table, assuming a
>            normalized distribution */

Acked-by: Arnd Bergmann <arnd@arndb.de>

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

* Re: [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
  2009-12-21 10:51   ` Andi Kleen
@ 2009-12-21 23:44     ` Rusty Russell
  2009-12-22  0:13       ` Andi Kleen
  0 siblings, 1 reply; 6+ messages in thread
From: Rusty Russell @ 2009-12-21 23:44 UTC (permalink / raw)
  To: Andi Kleen; +Cc: torvalds, linux-kernel, jbeulich, rguenther, arnd

On Mon, 21 Dec 2009 09:21:39 pm Andi Kleen wrote:
> On Mon, Dec 21, 2009 at 09:02:39PM +1030, Rusty Russell wrote:
> > On Mon, 21 Dec 2009 03:23:15 am Andi Kleen wrote:
> > > Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
> > > 
> > > The BUILD_BUG_ON in compat_ioctl_check_table() fails
> > > with a recent gcc mainline snapshot (gcc version 4.5.0 20091219)
> > > (GCC)), even though it works with older compilers.
> > 
> > We have a proper fix for BUILD_BUG_ON which I sent Linus a pull req for,
> > but I think it was too late:
> > 
> > 	git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus.git BUILD_BUG_ON
> > 
> > It's not really high priority, but it'd be nice to fix this properly.
> 
> AFAIK the code I fixed was not correct in the first place because
> "const int xxx" is not a constant expression in the standards sense.
> 
> So it's orthogonal to whatever changes you come up.

No, it's solving the same problem, generically.  There are numerous places
where we want to use BUILD_BUG_ON() but we don't have a constant expression.
Look for MAYBE_BUILD_BUG_ON().

Subject: 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.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Acked-by: Hollis Blanchard <hollisb@us.ibm.com>
---
 include/linux/kernel.h |   33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -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__)
 

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

* Re: [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot
  2009-12-21 23:44     ` Rusty Russell
@ 2009-12-22  0:13       ` Andi Kleen
  0 siblings, 0 replies; 6+ messages in thread
From: Andi Kleen @ 2009-12-22  0:13 UTC (permalink / raw)
  To: Rusty Russell
  Cc: Andi Kleen, torvalds, linux-kernel, jbeulich, rguenther, arnd

> No, it's solving the same problem, generically.  There are numerous places
> where we want to use BUILD_BUG_ON() but we don't have a constant expression.
> Look for MAYBE_BUILD_BUG_ON().

Ok so you're saying that should be just a MAYBE_BUILD_BUG_ON()? 

> 
> Subject: 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.

Ok maybe that works, but we need a fix for 2.6.33 too.

-Andi


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

end of thread, other threads:[~2009-12-22  0:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-20 16:53 [PATCH] Fix BUILD_BUG_ON in fs/compat_ioctl.c to build with gcc 4.5 snapshot Andi Kleen
2009-12-21 10:32 ` Rusty Russell
2009-12-21 10:51   ` Andi Kleen
2009-12-21 23:44     ` Rusty Russell
2009-12-22  0:13       ` Andi Kleen
2009-12-21 16:48 ` Arnd Bergmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox