Add support for GCC's __builtin_unreachable() and use it in BUG.
cover Add support for GCC's __builtin_unreachable() and use it in BUG.
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 1/10 Add support for GCC-4.5's __builtin_unreachable() to compiler.h
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.
--- 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
@@ -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
@@ -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; \
Signature · 1 line
1.6.2.5
PATCH 4/10 s390: Convert BUG() to use unreachable()
In reply to David DaneyUse 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
@@ -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
PATCH 6/10 parisc: Convert BUG() to use unreachable()
In reply to David DaneyUse the new unreachable() macro instead of for(;;);
--- arch/parisc/include/asm/bug.h | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
arch/parisc/include/asm/bug.h +2 -2
@@ -32,14 +32,14 @@ "\t.popsection" \ : : "i" (__FILE__), "i" (__LINE__), \ "i" (0), "i" (sizeof(struct bug_entry)) ); \ -for (;;) ; \ +unreachable (); \ } while(0) #else#define BUG () \ do { \ asm volatile(PARISC_BUG_BREAK_ASM : : ); \ - for(;;) ; \ + unreachable(); \ } while(0)#endif
Signature · 1 line
1.6.2.5
PATCH 8/10 alpha: Convert BUG() to use unreachable()
In reply to David DaneyUse the new unreachable() macro instead of for(;;);
--- arch/alpha/include/asm/bug.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/alpha/include/asm/bug.h +1 -1
@@ -13,7 +13,7 @@ "call_pal %0 # bugchk\n\t" \ ".long %1\n\t.8byte %2" \ : : "i"(PAL_bugchk), "i"(__LINE__), "i"(__FILE__)); \ - for ( ; ; ); } while (0) + unreachable(); } while (0)#define HAVE_ARCH_BUG #endif
Signature · 1 line
1.6.2.5
PATCH 5/10 mn10300: Convert BUG() to use unreachable()
In reply to David DaneyUse the new unreachable() macro instead of while(1).
--- arch/mn10300/include/asm/bug.h | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
arch/mn10300/include/asm/bug.h +2 -1
@@ -27,7 +27,8 @@ do { \ : \ : "i"(__FILE__), "i"(__LINE__) \ ); \ -} while (1) +unreachable (); \ +} while (0)#define HAVE_ARCH_BUG #endif /* CONFIG_BUG */
Signature · 1 line
1.6.2.5
PATCH 2/10 x86: Convert BUG() to use unreachable()
In reply to David DaneyUse 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
@@ -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
PATCH 3/10 MIPS: Convert BUG() to use unreachable()
In reply to David DaneyUse 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
@@ -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
PATCH 7/10 powerpc: Convert BUG() to use unreachable()
In reply to David DaneyUse the new unreachable() macro instead of for(;;);
--- arch/powerpc/include/asm/bug.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/powerpc/include/asm/bug.h +1 -1
@@ -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 { \
Signature · 1 line
1.6.2.5
PATCH 9/10 avr32: Convert BUG() to use unreachable()
In reply to David DaneyUse 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
@@ -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
PATCH 10/10 blackfin: Convert BUG() to use unreachable()
In reply to David DaneyUse the new unreachable() macro instead of for(;;);
--- arch/blackfin/include/asm/bug.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
arch/blackfin/include/asm/bug.h +1 -1
@@ -41,7 +41,7 @@#define BUG () \ do { \ _BUG_OR_WARN(0); \ - for (;;); \ + unreachable(); \ } while (0)#define WARN_ON (condition) \
Signature · 1 line
1.6.2.5
Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h
In reply to David Daneyinclude/linux/compiler.h void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect); · line 150
Quoted 3 lines · David Daney
include/linux/compiler.h +3
-0+#ifndef unreachable +# define unreachable () do { for (;;) ; } while (0) +#endif
#define unreachable() do { } while (1)
r~
Re: [PATCH 02/10] x86: Convert BUG() to use unreachable()
In reply to David DaneyQuoted 9 lines · 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).Signed-off-by David Daney <[email protected]>CC Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>
... although of course this clashes with Roland McGrath's patchset for the same thing which I applied earlier. I have to say I like unreachable() in lower case better though...
-hpa
Re: [PATCH 02/10] x86: Convert BUG() to use unreachable()
Quoted 3 lines
... although of course this clashes with Roland McGrath's patchset for the same thing which I applied earlier. I have to say I like
unreachable()in lower case better though...
I followed the example of existing function-like macros such as ACCESS_ONCE. I have no preference about the name.
Thanks,
Roland
Re: [PATCH 02/10] x86: Convert BUG() to use unreachable()
In reply to H. Peter AnvinQuoted 11 lines · H. Peter Anvin
Quoted 9 lines · 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).Signed-off-by David Daney <[email protected]>CC Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>
Quoted 5 lines
Acked-by H. Peter Anvin <[email protected]>... although of course this clashes with Roland McGrath's patchset for the same thing which I applied earlier. I have to say I like
unreachable()in lower case better though...
I like David's version a bit better, since it takes care of more architectures, and also because it avoids that butt-ugly special case for gcc-4.4.1-RH-relase-10 backporting this feature.
I realize that the RH backport thing is good for testing now, but at the same time, it really does look nasty. I wonder if we could add some config-time compiler feature testing - so that you'd not have a version test at all, but a CONFIG_BUILTIN_UNREACHABLE.
There are other cases where that kind of config-time testing could be useful, and we could avoid doing various gcc checks dynamically from within 'make' (along with checking for known-buggy versions etc).
And yeah, it looks better in lower case. That said, I don't care _that_ much, and people can fight it out.
Linus
H. Peter Anvin
reviewerRe: [PATCH 02/10] x86: Convert BUG() to use unreachable()
Quoted 6 lines · Linus Torvalds
I realize that the RH backport thing is good for testing now, but at the same time, it really does look nasty. I wonder if we could add some config-time compiler feature testing - so that you'd not have a version test at all, but a
CONFIG_BUILTIN_UNREACHABLE.
autoconf ;)
-hpa
Re: [PATCH 02/10] x86: Convert BUG() to use unreachable()
In reply to Linus TorvaldsQuoted 3 lines
There are other cases where that kind of config-time testing could be useful, and we could avoid doing various gcc checks dynamically from within 'make' (along with checking for known-buggy versions etc).
I quite agree that this would be an improvement for many sorts of compiler feature (and other tool detail) conditionalization.
Thanks,
Roland
H. Peter Anvin
reviewerRe: [PATCH 02/10] x86: Convert BUG() to use unreachable()
Quoted 6 lines · Roland McGrath
Quoted 3 lines
There are other cases where that kind of config-time testing could be useful, and we could avoid doing various gcc checks dynamically from within 'make' (along with checking for known-buggy versions etc).
I quite agree that this would be an improvement for many sorts of compiler feature (and other tool detail) conditionalization.
The nicest, of course, would be if gcc exported its internal support, similar to the way it nowadays export macros for the various internal data types.
-hpa
Re: [PATCH 02/10] x86: Convert BUG() to use unreachable()
In reply to Linus TorvaldsQuoted 28 lines · Linus Torvalds
Quoted 11 lines · H. Peter Anvin
Quoted 9 lines · 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).Signed-off-by David Daney <[email protected]>CC Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>Quoted 5 lines
Acked-by H. Peter Anvin <[email protected]>... although of course this clashes with Roland McGrath's patchset for the same thing which I applied earlier. I have to say I like
unreachable()in lower case better though...I like David's version a bit better, since it takes care of more architectures, and also because it avoids that butt-ugly special case for gcc-4.4.1-RH-relase-10 backporting this feature.
I realize that the RH backport thing is good for testing now, but at the same time, it really does look nasty. I wonder if we could add some config-time compiler feature testing - so that you'd not have a version test at all, but a
CONFIG_BUILTIN_UNREACHABLE.
Quoted 7 lines
There are other cases where that kind of config-time testing could be useful, and we could avoid doing various gcc checks dynamically from within 'make' (along with checking for known-buggy versions etc).
And yeah, it looks better in lower case. That said, I don't care _that_ much, and people can fight it out.
Another detail that would be nice to be fixed is to propagate the unreachable() call into the !CONFIG_BUG case as well in asm-generic/bug.h.
That would kill dozens of !CONFIG_BUG compiler warnings and would make a dont-allow-warnings policy a possibility for random builds.
Ingo
Re: [PATCH 09/10] avr32: Convert BUG() to use unreachable()
In reply to David DaneyQuoted 4 lines · David Daney
Use the new
unreachable()macro instead of for(;;);Signed-off-by David Daney <[email protected]>CC Haavard Skinnemoen <[email protected]>
Re: [PATCH 04/10] s390: Convert BUG() to use unreachable()
In reply to David DaneyQuoted 16 lines · On Thu, 10 Sep 2009 16:56:45 -0700 David Daney <[email protected]>…
Use the new
unreachable()macro instead of for(;;);Signed-off-by David Daney <[email protected]>CC Martin Schwidefsky <[email protected]>CC Heiko Carstens <[email protected]>--- arch/s390/include/asm/bug.h | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)arch/s390/include/asm/bug.h +0
-0
arch/s390/include/asm/bug.h line 55
Quoted 8 lines
arch/s390/include/asm/bug.h +1
-1@@ -49,7 +49,7 @@#define BUG () do { \ __EMIT_BUG(0); \ - for (;;); \ + unreachable(); \ } while (0)#define WARN_ON (x) ({ \
That seems simple enough ;-)
Signature · 4 lines
blue skies, Martin. "Reality continues to ruin my life." - Calvin.
Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h
In reply to David Daneyinclude/linux/compiler.h void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect); · line 150
Quoted 4 lines · On Friday 11 September 2009 01:56:42 David Daney wrote:
include/linux/compiler.h +4
-0+/* Unreachable code */ +#ifndef unreachable +# define unreachable () do { for (;;) ; } while (0) +#endif
# define unreachable() do { } while (1)? :)
Signature · 1 line
Greetings, Michael.
Re: [PATCH 02/10] x86: Convert BUG() to use unreachable()
In reply to Ingo MolnarQuoted 19 lines · Ingo Molnar
Quoted 17 lines · Linus Torvalds
Quoted 5 lines · H. Peter Anvin
Quoted 4 lines · 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).Quoted 8 lines
Quoted 5 lines
Signed-off-by David Daney <[email protected]>CC Thomas Gleixner <[email protected]>CC Ingo Molnar <[email protected]>CC "H. Peter Anvin" <[email protected]>Acked-by H. Peter Anvin <[email protected]>... although of course this clashes with Roland McGrath's patchset for the same thing which I applied earlier. I have to say I like
unreachable()in lower case better though...I like David's version a bit better, since it takes care of more architectures, and also because it avoids that butt-ugly special case for gcc-4.4.1-RH-relase-10 backporting this feature.
Quoted 8 lines
Quoted 5 lines
I realize that the RH backport thing is good for testing now, but at the same time, it really does look nasty. I wonder if we could add some config-time compiler feature testing - so that you'd not have a version test at all, but a
CONFIG_BUILTIN_UNREACHABLE.There are other cases where that kind of config-time testing could be useful, and we could avoid doing various gcc checks dynamically from within 'make' (along with checking for known-buggy versions etc).
And yeah, it looks better in lower case. That said, I don't care _that_ much, and people can fight it out.
Another detail that would be nice to be fixed is to propagate the
unreachable()call into the !CONFIG_BUG case as well inasm-generic/bug.h.
Perhaps, although with a pre-GCC-4.5 compiler, it would end up adding the endless loop code to BUG() sites for configurations where the user has explicitly stated their preference for the code to be as small as possible (by turning off CONFIG_BUG).
If that is acceptable, I will prepare another revision of the patch set.
Thanks,
David Daney
Quoted 3 lines
That would kill dozens of !CONFIG_BUG compiler warnings and would make a dont-allow-warnings policy a possibility for random builds.
Ingo
Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h
In reply to Michael BueschQuoted 9 lines · Michael Buesch
include/linux/compiler.h void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect); · line 150
Quoted 4 lines · On Friday 11 September 2009 01:56:42 David Daney wrote:
include/linux/compiler.h +4
-0+/* 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
Quoted 11 lines · David Daney
Quoted 10 lines · Michael Buesch
include/linux/compiler.h void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect); · line 150
Quoted 5 lines · On Friday 11 September 2009 01:56:42 David Daney wrote:
include/linux/compiler.h +4
-0+/* Unreachable code */ +#ifndef unreachable +# define unreachable () do { for (;;) ; } while (0) +#endif # define unreachable() do { } while (1)
Quoted 4 lines
Quoted 1 line
? :)
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?
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-alpha" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
H. Peter Anvin
reviewerRe: [PATCH 02/10] x86: Convert BUG() to use unreachable()
In reply to David DaneyQuoted 8 lines · David Daney
Perhaps, although with a pre-GCC-4.5 compiler, it would end up adding the endless loop code to
BUG()sites for configurations where the user has explicitly stated their preference for the code to be as small as possible (by turning offCONFIG_BUG).If that is acceptable, I will prepare another revision of the patch set.
The thing is that without the loop, the compiler will generate the fall-through case code, which may add more code than the loop removes.
-hpa
Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center I work for Intel. I don't speak on their behalf.
Re: [PATCH 01/10] Add support for GCC-4.5's __builtin_unreachable() to compiler.h
In reply to Geert UytterhoevenQuoted 10 lines · Geert Uytterhoeven
Quoted 9 lines · David Daney
Quoted 8 lines · Michael Buesch
include/linux/compiler.h void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect); · line 150
Quoted 4 lines · On Friday 11 September 2009 01:56:42 David Daney wrote:
include/linux/compiler.h +4
-0+/* Unreachable code */ +#ifndef unreachable +# define unreachable () do { for (;;) ; } while (0) +#endif # define unreachable() do { } while (1)? :)
Quoted 9 lines
Quoted 2 lines
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 a1' here, not a0'.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
Quoted 7 lines · David Daney
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:
Re: [PATCH 06/10] parisc: Convert BUG() to use unreachable()
In reply to David DaneyQuoted 6 lines · David Daney
Use the new
unreachable()macro instead of for(;;);Signed-off-by David Daney <[email protected]>CC Kyle McMartin <[email protected]>CC Helge Deller <[email protected]>
Since the dependent part of this patch seems to have missed the merge window, I guess we'll sit on this until next round?
--Kyle