[PATCH 00/10] Add support for GCC's __builtin_unreachable() and use it in BUG.

STALE6153d

9 messages, 5 authors, 2009-09-24 · open the first message on its own page

[PATCH 00/10] Add support for GCC's __builtin_unreachable() and use it in BUG.

From: David Daney <hidden>
Date: 2009-09-11 00:10:56

Starting with version 4.5, GCC has a new built-in function called
__builtin_unreachable().  The function tells the compiler that control
flow will never reach that point.  Currently we trick the compiler by
putting in for(;;); but this has the disadvantage that extra code is
emitted for an endless loop.  For an i386 kernel using
__builtin_unreachable() results in an allyesconfig that is nearly 4000
bytes smaller.

This patch set adds support to compiler.h creating a
new macro usable in the kernel called unreachable().  If the compiler
lacks __builtin_unreachable(), it just expands to for(;;).

The x86 and MIPS patches I actually tested with a GCC-4.5 snapshot.
Lacking the ability to test the rest of the architectures, I just did
what seemed right without even trying to compile the kernel.

01/10 adds the compiler.h support, the rest of the patches retrofit
the various architecture BUG macros to use it instead of for(;;) or
while(1) loops.

I will reply with the 10 patches.

The architecture specific patches I will send to a smaller set of
people.

David Daney (10):
   Add support for GCC-4.5's __builtin_unreachable() to compiler.h
   x86: Convert BUG() to use unreachable()
   MIPS: Convert BUG() to use unreachable()
   s390: Convert BUG() to use unreachable()
   mn10300: Convert BUG() to use unreachable()
   parisc: Convert BUG() to use unreachable()
   powerpc: Convert BUG() to use unreachable()
   alpha: Convert BUG() to use unreachable()
   avr32: Convert BUG() to use unreachable()
   blackfin: Convert BUG() to use unreachable()

  arch/alpha/include/asm/bug.h    |    2 +-
  arch/avr32/include/asm/bug.h    |    2 +-
  arch/blackfin/include/asm/bug.h |    2 +-
  arch/mips/include/asm/bug.h     |    4 +---
  arch/mn10300/include/asm/bug.h  |    3 ++-
  arch/parisc/include/asm/bug.h   |    4 ++--
  arch/powerpc/include/asm/bug.h  |    2 +-
  arch/s390/include/asm/bug.h     |    2 +-
  arch/x86/include/asm/bug.h      |    4 ++--
  include/linux/compiler-gcc4.h   |   14 ++++++++++++++
  include/linux/compiler.h        |    5 +++++
  11 files changed, 31 insertions(+), 13 deletions(-)

[PATCH 07/10] powerpc: Convert BUG() to use unreachable()

From: David Daney <hidden>
Date: 2009-09-10 23:57:44

Use the new unreachable() macro instead of for(;;);

Signed-off-by: David Daney <redacted>
CC: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <redacted>
CC: linuxppc-dev@ozlabs.org
---
 arch/powerpc/include/asm/bug.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/arch/powerpc/include/asm/bug.h b/arch/powerpc/include/asm/bug.h
index 64e1fdc..2c15212 100644
--- a/arch/powerpc/include/asm/bug.h
+++ b/arch/powerpc/include/asm/bug.h
@@ -68,7 +68,7 @@
 		_EMIT_BUG_ENTRY					\
 		: : "i" (__FILE__), "i" (__LINE__),		\
 		    "i" (0), "i"  (sizeof(struct bug_entry)));	\
-	for(;;) ;						\
+	unreachable();						\
 } while (0)
 
 #define BUG_ON(x) do {						\
-- 
1.6.2.5

[PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h

From: David Daney <hidden>
Date: 2009-09-10 23:58:31

Starting with version 4.5, GCC has a new built-in function
__builtin_unreachable() that can be used in places like the kernel's
BUG() where inline assembly is used to transfer control flow.  This
eliminated the need for an endless loop in these places.

The patch adds a new macro 'unreachable()' that will expand to either
__builtin_unreachable() or an endless loop depending on the compiler
version.

Signed-off-by: David Daney <redacted>
CC: Thomas Gleixner <redacted>
CC: Ingo Molnar <mingo@redhat.com>
CC: "H. Peter Anvin" <hpa@zytor.com>
CC: x86@kernel.org
CC: ralf@linux-mips.org
CC: linux-mips@linux-mips.org
CC: Martin Schwidefsky <redacted>
CC: Heiko Carstens <redacted>
CC: linux390@de.ibm.com
CC: linux-s390@vger.kernel.org
CC: David Howells <dhowells@redhat.com>
CC: Koichi Yasutake <redacted>
CC: linux-am33-list@redhat.com
CC: Kyle McMartin <redacted>
CC: Helge Deller <deller@gmx.de>
CC: linux-parisc@vger.kernel.org
CC: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <redacted>
CC: linuxppc-dev@ozlabs.org
CC: Richard Henderson <redacted>
CC: Ivan Kokshaysky <redacted>
CC: linux-alpha@vger.kernel.org
CC: Haavard Skinnemoen <redacted>
CC: Mike Frysinger <redacted>
CC: uclinux-dist-devel@blackfin.uclinux.org
---
 include/linux/compiler-gcc4.h |   14 ++++++++++++++
 include/linux/compiler.h      |    5 +++++
 2 files changed, 19 insertions(+), 0 deletions(-)
diff --git a/include/linux/compiler-gcc4.h b/include/linux/compiler-gcc4.h
index 450fa59..ab3af40 100644
--- a/include/linux/compiler-gcc4.h
+++ b/include/linux/compiler-gcc4.h
@@ -36,4 +36,18 @@
    the kernel context */
 #define __cold			__attribute__((__cold__))
 
+
+#if __GNUC_MINOR__ >= 5
+/*
+ * Mark a position in code as unreachable.  This can be used to
+ * suppress control flow warnings after asm blocks that transfer
+ * control elsewhere.
+ *
+ * Early snapshots of gcc 4.5 don't support this and we can't detect
+ * this in the preprocessor, but we can live with this because they're
+ * unreleased.  Really, we need to have autoconf for the kernel.
+ */
+#define unreachable() __builtin_unreachable()
+#endif
+
 #endif
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 04fb513..7efd73f 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -144,6 +144,11 @@ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
 # define barrier() __memory_barrier()
 #endif
 
+/* Unreachable code */
+#ifndef unreachable
+# define unreachable() do { for (;;) ; } while (0)
+#endif
+
 #ifndef RELOC_HIDE
 # define RELOC_HIDE(ptr, off)					\
   ({ unsigned long __ptr;					\
-- 
1.6.2.5

Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h

From: Richard Henderson <hidden>
Date: 2009-09-11 00:26:15

On 09/10/2009 04:56 PM, David Daney wrote:
+#ifndef unreachable
+# define unreachable() do { for (;;) ; } while (0)
+#endif
#define unreachable() do { } while (1)


r~

Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h

From: Michael Buesch <hidden>
Date: 2009-09-11 14:37:51

On Friday 11 September 2009 01:56:42 David Daney wrote:
+/* Unreachable code */
+#ifndef unreachable
+# define unreachable() do { for (;;) ; } while (0)
+#endif
# define unreachable() do { } while (1)

? :)

-- 
Greetings, Michael.

Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h

From: David Daney <hidden>
Date: 2009-09-11 15:59:16

Michael Buesch wrote:
On Friday 11 September 2009 01:56:42 David Daney wrote:
quoted
+/* Unreachable code */
+#ifndef unreachable
+# define unreachable() do { for (;;) ; } while (0)
+#endif
# define unreachable() do { } while (1)

? :)
Clearly I was not thinking clearly when I wrote that part.  RTH noted 
the same thing.  I will fix it.

Thanks,
David Daney

Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h

From: Geert Uytterhoeven <geert@linux-m68k.org>
Date: 2009-09-12 07:22:43

On Fri, Sep 11, 2009 at 17:58, David Daney[off-list ref] wrote=
:
Michael Buesch wrote:
quoted
On Friday 11 September 2009 01:56:42 David Daney wrote:
quoted
+/* Unreachable code */
+#ifndef unreachable
+# define unreachable() do { for (;;) ; } while (0)
+#endif
# define unreachable() do { } while (1)

? :)
Clearly I was not thinking clearly when I wrote that part. =C2=A0RTH note=
d the
same thing. =C2=A0I will fix it.
However, people are so used to seeing the `do { } while (0)' idiom,
that they might miss
there's a `1' here, not a `0'.

So perhaps it's better to use plain `for (;;)' for infinite loops?

Gr{oetje,eeting}s,

						Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k=
.org

In personal conversations with technical people, I call myself a hacker. Bu=
t
when I'm talking to journalists I just say "programmer" or something like t=
hat.
							    -- Linus Torvalds

Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h

From: David Daney <hidden>
Date: 2009-09-14 15:40:34

Geert Uytterhoeven wrote:
On Fri, Sep 11, 2009 at 17:58, David Daney[off-list ref] wrote:
quoted
Michael Buesch wrote:
quoted
On Friday 11 September 2009 01:56:42 David Daney wrote:
quoted
+/* Unreachable code */
+#ifndef unreachable
+# define unreachable() do { for (;;) ; } while (0)
+#endif
# define unreachable() do { } while (1)

? :)
Clearly I was not thinking clearly when I wrote that part.  RTH noted the
same thing.  I will fix it.
However, people are so used to seeing the `do { } while (0)' idiom,
that they might miss
there's a `1' here, not a `0'.

So perhaps it's better to use plain `for (;;)' for infinite loops?
I don't think so.  The only valid token that can follow 'do { } while 
(1)' is ';', any statement may follow 'for (;;)', so there is a greater 
possibility to silently screw things up with the for(;;) form.

David Daney

Re: [PATCH 00/10] Add support for GCC's __builtin_unreachable() and use it in BUG.

From: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Date: 2009-09-24 03:44:44

On Thu, 2009-09-10 at 16:54 -0700, David Daney wrote:
Starting with version 4.5, GCC has a new built-in function called
__builtin_unreachable().  The function tells the compiler that control
flow will never reach that point.  Currently we trick the compiler by
putting in for(;;); but this has the disadvantage that extra code is
emitted for an endless loop.  For an i386 kernel using
__builtin_unreachable() results in an allyesconfig that is nearly 4000
bytes smaller.
For the powerpc part:

Acked-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help