Skip to contentSkip to conversation

asm-generic: bug: add unlikely() to BUG_ON()

PATCH In review Igor Stoppa 2 participants 3 messages Sep 7, 2018 – Sep 8, 2018 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 asm-generic: bug: add unlikely() to BUG_ON()

Add a hint to the compiler.
If BUG_ON() is used instead of BUG(), it means that probably the preferred outcome is to not BUG().

The optimization is disabled, in case CONFIG_PROFILE_ANNOTATED_BRANCHES is turned on.

---
 include/asm-generic/bug.h | 4 ++++
 1 file changed, 4 insertions(+)
include/asm-generic/bug.h +4 -0
index 20561a60db9c..bf47584eab2a 100644
@@ -183,7 +183,11 @@ void __warn(const char *file, int line, void *caller, unsigned taint,
 #endif
 
 #ifndef HAVE_ARCH_BUG_ON
+#ifdef CONFIG_PROFILE_ANNOTATED_BRANCHES
 #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
+#else
+#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
+#endif
 #endif
 
 #ifndef HAVE_ARCH_WARN_ON
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 7 lines · Igor Stoppa

Add a hint to the compiler.
If BUG_ON() is used instead of BUG(), it means that probably the preferred outcome is to not BUG().

The optimization is disabled, in case CONFIG_PROFILE_ANNOTATED_BRANCHES is turned on.

This sounds like a good idea, as this is one of the more likely causes of false-positive -Wmaybe-uninitialized warnings with CONFIG_PROFILE_ANNOTATED_BRANCHES

Could you add a comment about -Wmaybe-uninitialized next to the definition? Otherwise that is easily lost.

Also, I see that the file has two separate definitions of BUG_ON(), and the other one does have an unlikely() in it already. Can you change both to do the same thing with unlikely() or not unlikely()?

Arnd

#2 lore raw
Quoted 6 lines · Arnd Bergmann

Could you add a comment about -Wmaybe-uninitialized next to the definition? Otherwise that is easily lost.

Also, I see that the file has two separate definitions of BUG_ON(), and the other one does have an unlikely() in it already. Can you change both to do the same thing with unlikely() or not unlikely()?

ack

--
igor