Skip to contentSkip to conversation

x86: apic: convert BUG() to BUG_ON()

PATCH In review Daniel Walker 3 participants 7 messages Sep 12, 2009 – Sep 18, 2009 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: apic: convert BUG() to BUG_ON()

This was done using Coccinelle's BUG_ON semantic patch.

---
 arch/x86/kernel/apic/apic.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
arch/x86/kernel/apic/apic.c +1 -2
index a34601f..e3d467e 100644
@@ -1197,8 +1197,7 @@ void __cpuinit setup_local_APIC(void)
 	 * Double-check whether this APIC is really registered.
 	 * This is meaningless in clustered apic mode, so we skip it.
 	 */
-	if (!apic->apic_id_registered())
-		BUG();
+	BUG_ON(!apic->apic_id_registered());
 
 	/*
 	 * Intel recommends to set DFR, LDR and TPR before enabling
Signature · 1 line
1.5.6.3
#1 lore raw

[Daniel Walker - Sat, Sep 12, 2009 at 10:40:20AM -0700]

| This was done using Coccinelle's BUG_ON semantic patch.
| 
| Signed-off-by: Daniel Walker <[email protected]>
| ---
|  arch/x86/kernel/apic/apic.c |    3 +--
|  1 files changed, 1 insertions(+), 2 deletions(-)
| 
| diff --git a/arch/x86/kernel/apic/apic.c b/arch/x86/kernel/apic/apic.c
| index a34601f..e3d467e 100644
| --- a/arch/x86/kernel/apic/apic.c
| +++ b/arch/x86/kernel/apic/apic.c
| @@ -1197,8 +1197,7 @@ void __cpuinit setup_local_APIC(void)
|  	 * Double-check whether this APIC is really registered.
|  	 * This is meaningless in clustered apic mode, so we skip it.
|  	 */
| -	if (!apic->apic_id_registered())
| -		BUG();
| +	BUG_ON(!apic->apic_id_registered());
|  
|  	/*
|  	 * Intel recommends to set DFR, LDR and TPR before enabling
| -- 
| 1.5.6.3
| 

Hi Daniel,

I believe having a changelog like

Use short form of "if() BUG()" sequence

would be better perhaps? Since "Coccinelle's BUG_ON semantic patch" somehow doesn't describe why it's done.

Don't get me wrong please. It's trivial and seen from patch itself _why_ it's done though changelog doesn't say the same.

Perhaps I'm too nagging :) Feel free to ignore me.

-- Cyrill

#2 lore raw
Quoted 7 lines · Cyrill Gorcunov

Hi Daniel,

I believe having a changelog like

Use short form of "if() BUG()" sequence

Quoted 7 lines

would be better perhaps? Since "Coccinelle's BUG_ON semantic patch" somehow doesn't describe why it's done.

Don't get me wrong please. It's trivial and seen from patch itself _why_ it's done though changelog doesn't say the same.

Perhaps I'm too nagging :) Feel free to ignore me.

Not nagging, I wondered myself what the benefit was when I ran Coccinelle.

For one it condenses duplicate code (i.e. the if()). If the BUG_ON() macro gets updated with something new, all the users get the updates automatically. The other thing is your re-using potentially more advanced code that's inside the macro. In this case it's fairly trivial,

#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while(0)

So we're getting the benefit on the new "unlikely" in the apic code. unlikely/likely calls will usually allow the compiler to create smaller, and or, more optimized code.

So there are at least two benefits, and I don't see any downside to it.

Daniel

#3 lore raw

[Daniel Walker - Sat, Sep 12, 2009 at 11:20:41AM -0700]

| On Sat, 2009-09-12 at 22:05 +0400, Cyrill Gorcunov wrote:
| 
| > 
| > Hi Daniel,
| > 
| > I believe having a changelog like
| > 
| > 	Use short form of "if() BUG()" sequence
| > 
| > would be better perhaps? Since "Coccinelle's BUG_ON semantic patch"
| > somehow doesn't describe why it's done.
| > 
| > Don't get me wrong please. It's trivial and seen from patch
| > itself _why_ it's done though changelog doesn't say the same.
| > 
| > Perhaps I'm too nagging :) Feel free to ignore me.
| 
| Not nagging, I wondered myself what the benefit was when I ran
| Coccinelle.
| 
| For one it condenses duplicate code (i.e. the if()). If the BUG_ON()
| macro gets updated with something new, all the users get the updates
| automatically. The other thing is your re-using potentially more
| advanced code that's inside the macro. In this case it's fairly trivial,
| 
| #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while(0)
| 
| So we're getting the benefit on the new "unlikely" in the apic code.
| unlikely/likely calls will usually allow the compiler to create smaller,
| and or, more optimized code. 

I would not relay on "unlikely" much especially in apic code. Though on some platforms this apic_id_registered returns plain 1 which could (didn't check myself) bring in some optimization (to be fair -- I can't imagine what could be optimized
in this particular case, especially since we may have locked operations on mem write).

So I consider it as code shrinking on source level only :)

| 
| So there are at least two benefits, and I don't see any downside to it.
| 
| Daniel
| 

-- Cyrill

#4 lore raw
In reply to Daniel Walker
Quoted 10 lines · Daniel Walker

For one it condenses duplicate code (i.e. the if()). If the BUG_ON() macro gets updated with something new, all the users get the updates automatically. The other thing is your re-using potentially more advanced code that's inside the macro. In this case it's fairly trivial,

#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while(0)

So we're getting the benefit on the new "unlikely" in the apic code. unlikely/likely calls will usually allow the compiler to create smaller, and or, more optimized code.

For non-x86 platforms the use of the BUG_ON() macro may result in more
efficient code GCC may not be able to optimise to with if (...) BUG();. For example the macro may expand to inline assembly with a conditional trap instruction GCC would not emit for an if () clause. While GCC does have a __builtin_trap() intrinsic that could be optimised if alone in a conditional block, such usage may not be frequent enough for a dedicated optimisation to be provided and build-time efficiency of the compiler does matter too, so such an optimisation might be of too questionable a value to incur an additional performance hit for the compiler.

Just a general note on patches of this kind, or to put it short, yes I
agree it's a good idea.

Maciej

#5 lore raw

[Maciej W. Rozycki - Sat, Sep 12, 2009 at 11:51:40PM +0100]

| On Sat, 12 Sep 2009, Daniel Walker wrote:
| 
| > For one it condenses duplicate code (i.e. the if()). If the BUG_ON()
| > macro gets updated with something new, all the users get the updates
| > automatically. The other thing is your re-using potentially more
| > advanced code that's inside the macro. In this case it's fairly trivial,
| > 
| > #define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while(0)
| > 
| > So we're getting the benefit on the new "unlikely" in the apic code.
| > unlikely/likely calls will usually allow the compiler to create smaller,
| > and or, more optimized code. 
| 
|  For non-x86 platforms the use of the BUG_ON() macro may result in more 
| efficient code GCC may not be able to optimise to with if (...) BUG();.  
| For example the macro may expand to inline assembly with a conditional 
| trap instruction GCC would not emit for an if () clause.  While GCC does 
| have a __builtin_trap() intrinsic that could be optimised if alone in a 
| conditional block, such usage may not be frequent enough for a dedicated 
| optimisation to be provided and build-time efficiency of the compiler does 
| matter too, so such an optimisation might be of too questionable a value 
| to incur an additional performance hit for the compiler.
| 
|  Just a general note on patches of this kind, or to put it short, yes I 
| agree it's a good idea.
| 
|   Maciej
| 

Actually this is quite a good candidate for commit message :)

-- Cyrill

#6 lore raw

[tip:x86/urgent] x86: apic: Convert BUG() to BUG_ON()

In reply to Daniel Walker
Commit-ID:  c2777f98c205148f1a0d4f9ac03b9cb20b39b2da
Gitweb:     http://git.kernel.org/tip/c2777f98c205148f1a0d4f9ac03b9cb20b39b2da
Author:     Daniel Walker <[email protected]>
AuthorDate: Sat, 12 Sep 2009 10:40:20 -0700
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 18 Sep 2009 13:45:33 +0200

x86: apic: Convert BUG() to BUG_ON()

This was done using Coccinelle's BUG_ON semantic patch.

LKML-Reference: <[email protected]>

---
 arch/x86/kernel/apic/apic.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
arch/x86/kernel/apic/apic.c +1 -2
index 159740d..79e5b92 100644
@@ -1196,8 +1196,7 @@ void __cpuinit setup_local_APIC(void)
 	 * Double-check whether this APIC is really registered.
 	 * This is meaningless in clustered apic mode, so we skip it.
 	 */
-	if (!apic->apic_id_registered())
-		BUG();
+	BUG_ON(!apic->apic_id_registered());
 
 	/*
 	 * Intel recommends to set DFR, LDR and TPR before enabling