Skip to contentSkip to conversation

Add support for GCC's __builtin_unreachable() and use it in BUG (v2.1).

PATCH New David Daney 1 participant 6 messages Dec 5, 2009

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover Add support for GCC's __builtin_unreachable() and use it in BUG (v2.1).

Greetings Linus et al.,

From the announcement of the first version:

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 defaultconfig 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(;;).

For version 2:

I fixed a couple of checkpatch issues, and simplified the unreachable() macro for the pre-GCC-4.5 case (as suggested by Richard Henderson). Also several Acked-by: were added.

For this version 2.1:

I removed patches from the set for which there were no Acked-by, and rebased and tested against 2.6.32.

I will reply with the 5 patches.

David Daney (5):
Add support for GCC-4.5's __builtin_unreachable() to compiler.h (v2)
x86: Convert BUG() to use unreachable()
MIPS: Convert BUG() to use unreachable()
s390: Convert BUG() to use unreachable()
avr32: Convert BUG() to use unreachable()

 arch/avr32/include/asm/bug.h  |    2 +-
 arch/mips/include/asm/bug.h   |    4 +---
 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 +++++
 6 files changed, 24 insertions(+), 7 deletions(-)
#1 lore raw

PATCH 1/5 Add support for GCC-4.5's __builtin_unreachable() to compiler.h (v2)

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.

Change from v1: Simplify unreachable() for non-GCC 4.5 case.

---
 include/linux/compiler-gcc4.h |   14 ++++++++++++++
 include/linux/compiler.h      |    5 +++++
 2 files changed, 19 insertions(+), 0 deletions(-)
include/linux/compiler-gcc4.h +14 -0
index 450fa59..ab3af40 100644
@@ -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
include/linux/compiler.h +5 -0
index 04fb513..59f2089 100644
@@ -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 { } while (1)
+#endif
+
 #ifndef RELOC_HIDE
 # define RELOC_HIDE(ptr, off)					\
   ({ unsigned long __ptr;					\
Signature · 1 line
1.6.2.5
#2 lore raw

PATCH 2/5 x86: Convert BUG() to use unreachable()

In reply to David Daney

Use the new unreachable() macro instead of for(;;);. When allyesconfig is built with a GCC-4.5 snapshot on i686 the size of the text segment is reduced by 3987 bytes (from 6827019 to 6823032).

---
 arch/x86/include/asm/bug.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/bug.h +2 -2
index d9cf1cd..f654d1b 100644
@@ -22,14 +22,14 @@ do {								\
 		     ".popsection"				\
 		     : : "i" (__FILE__), "i" (__LINE__),	\
 		     "i" (sizeof(struct bug_entry)));		\
-	for (;;) ;						\
+	unreachable();						\
 } while (0)
 
 #else
 #define BUG()							\
 do {								\
 	asm volatile("ud2");					\
-	for (;;) ;						\
+	unreachable();						\
 } while (0)
 #endif
 
Signature · 1 line
1.6.2.5
#3 lore raw

PATCH 3/5 MIPS: Convert BUG() to use unreachable()

In reply to David Daney

Use the new unreachable() macro instead of while(1);

---
 arch/mips/include/asm/bug.h |    4 +---
 1 files changed, 1 insertions(+), 3 deletions(-)
arch/mips/include/asm/bug.h +1 -3
index 6cf29c2..540c98a 100644
@@ -11,9 +11,7 @@
 static inline void __noreturn BUG(void)
 {
 	__asm__ __volatile__("break %0" : : "i" (BRK_BUG));
-	/* Fool GCC into thinking the function doesn't return. */
-	while (1)
-		;
+	unreachable();
 }
 
 #define HAVE_ARCH_BUG
Signature · 1 line
1.6.2.5
#4 lore raw

PATCH 4/5 s390: Convert BUG() to use unreachable()

In reply to David Daney

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

---
 arch/s390/include/asm/bug.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/s390/include/asm/bug.h +1 -1
index 7efd0ab..efb74fd 100644
@@ -49,7 +49,7 @@
 
 #define BUG() do {					\
 	__EMIT_BUG(0);					\
-	for (;;);					\
+	unreachable();					\
 } while (0)
 
 #define WARN_ON(x) ({					\
Signature · 1 line
1.6.2.5
#5 lore raw

PATCH 5/5 avr32: Convert BUG() to use unreachable()

In reply to David Daney

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

---
 arch/avr32/include/asm/bug.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/avr32/include/asm/bug.h +1 -1
index 331d45b..2aa373c 100644
@@ -52,7 +52,7 @@
 #define BUG()								\
 	do {								\
 		_BUG_OR_WARN(0);					\
-		for (;;);						\
+		unreachable();						\
 	} while (0)
 
 #define WARN_ON(condition)							\
Signature · 1 line
1.6.2.5