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(-)
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.
Signed-off-by: David Daney <[email protected]>
Acked-by: Ralf Baechle <[email protected]>
CC: Ralf Baechle <[email protected]>
---
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..59f2089 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 { } while (1)
+#endif
+
#ifndef RELOC_HIDE
# define RELOC_HIDE(ptr, off) \
({ unsigned long __ptr; \
--
1.6.2.5
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).
Signed-off-by: David Daney <[email protected]>
Acked-by: "H. Peter Anvin" <[email protected]>
CC: "H. Peter Anvin" <[email protected]>
CC: Thomas Gleixner <[email protected]>
CC: Ingo Molnar <[email protected]>
CC: [email protected]
---
arch/x86/include/asm/bug.h | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/x86/include/asm/bug.h b/arch/x86/include/asm/bug.h
index d9cf1cd..f654d1b 100644
--- a/arch/x86/include/asm/bug.h
+++ b/arch/x86/include/asm/bug.h
@@ -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
--
1.6.2.5
Use the new unreachable() macro instead of while(1);
Signed-off-by: David Daney <[email protected]>
Acked-by: Ralf Baechle <[email protected]>
CC: Ralf Baechle <[email protected]>
CC: [email protected]
---
arch/mips/include/asm/bug.h | 4 +---
1 files changed, 1 insertions(+), 3 deletions(-)
diff --git a/arch/mips/include/asm/bug.h b/arch/mips/include/asm/bug.h
index 6cf29c2..540c98a 100644
--- a/arch/mips/include/asm/bug.h
+++ b/arch/mips/include/asm/bug.h
@@ -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
--
1.6.2.5
Use the new unreachable() macro instead of for(;;);
Signed-off-by: David Daney <[email protected]>
Acked-by: Martin Schwidefsky <[email protected]>
CC: Martin Schwidefsky <[email protected]>
CC: Heiko Carstens <[email protected]>
CC: [email protected]
CC: [email protected]
---
arch/s390/include/asm/bug.h | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/arch/s390/include/asm/bug.h b/arch/s390/include/asm/bug.h
index 7efd0ab..efb74fd 100644
--- a/arch/s390/include/asm/bug.h
+++ b/arch/s390/include/asm/bug.h
@@ -49,7 +49,7 @@
#define BUG() do { \
__EMIT_BUG(0); \
- for (;;); \
+ unreachable(); \
} while (0)
#define WARN_ON(x) ({ \
--
1.6.2.5
Use the new unreachable() macro instead of for(;;);
Signed-off-by: David Daney <[email protected]>
Acked-by: Haavard Skinnemoen <[email protected]>
CC: Haavard Skinnemoen <[email protected]>
---
arch/avr32/include/asm/bug.h | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/arch/avr32/include/asm/bug.h b/arch/avr32/include/asm/bug.h
index 331d45b..2aa373c 100644
--- a/arch/avr32/include/asm/bug.h
+++ b/arch/avr32/include/asm/bug.h
@@ -52,7 +52,7 @@
#define BUG() \
do { \
_BUG_OR_WARN(0); \
- for (;;); \
+ unreachable(); \
} while (0)
#define WARN_ON(condition) \
--
1.6.2.5