xen: Rework WARN_ON() to return whether a warning was triggered
PATCH 1/1 xen: Rework WARN_ON() to return whether a warning was triggered
From: Julien Grall <[email protected]>
So far, our implementation of WARN_ON() cannot be used in the following situation:
if ( WARN_ON() )
...
This is because the WARN_ON() doesn't return whether a warning. Such construction can be handy to have if you have to print more information and now the stack track.
Rework the WARN_ON() implementation to return whether a warning was triggered. The idea was borrowed from Linux.
---
This will be used in the SMMUv3 driver (see [1]).
--- xen/include/xen/lib.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
xen/include/xen/lib.h +7 -1
@@ -23,7 +23,13 @@#include <asm/bug.h> #define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \ + \ + if ( unlikely(__ret_warn_on) ) \ + WARN(); \ + unlikely(__ret_warn_on); \ +})/* All clang versions supported by Xen have _Static_assert. */ #if defined(__clang__) || \
Signature · 1 line
2.17.1
Quoted 9 lines · Julien Grall
From: Julien Grall <[email protected]>
So far, our implementation of
WARN_ON()cannot be used in the following situation:if (
WARN_ON())...This is because the
WARN_ON()doesn't return whether a warning. Such
... warning has been triggered.
Quoted 2 lines
construction can be handy to have if you have to print more information and now the stack track.
Sorry, I'm not able to parse that sentence.
Quoted 5 lines
Rework the
WARN_ON()implementation to return whether a warning was triggered. The idea was borrowed from Linux.Signed-off-by Julien Grall <[email protected]>
Juergen
Hi Juergen,
Quoted 6 lines · Jürgen Groß
Quoted 5 lines · Julien Grall
From: Julien Grall <[email protected]>
So far, our implementation of
WARN_ON()cannot be used in the following situation:
Quoted 6 lines
Quoted 4 lines
if (
WARN_ON())
...This is because the
WARN_ON()doesn't return whether a warning. Such... warning has been triggered.
I will add it.
Quoted 5 lines
Quoted 3 lines
construction can be handy to have if you have to print more information and now the stack track.
Sorry, I'm not able to parse that sentence.
Urgh :/. How about the following commit message:
"So far, our implementation of WARN_ON() cannot be used in the following situation:
if ( WARN_ON() )
...
This is because WARN_ON() doesn't return whether a warning has been triggered. Such construciton can be handy if you want to print more information and also dump the stack trace.
Therefore, rework the WARN_ON() implementation to return whether a warning was triggered. The idea was borrowed from Linux".
Cheers,
Signature · 1 line
Julien Grall
Jürgen Groß
Reviewed-byQuoted 26 lines · Julien Grall
Hi Juergen,
Quoted 6 lines · Jürgen Groß
Quoted 5 lines · Julien Grall
From: Julien Grall <[email protected]>
So far, our implementation of
WARN_ON()cannot be used in the following situation:Quoted 6 lines
Quoted 4 lines
if (
WARN_ON())
...This is because the
WARN_ON()doesn't return whether a warning. Such... warning has been triggered.
I will add it.
Quoted 5 lines
Quoted 3 lines
construction can be handy to have if you have to print more information and now the stack track.
Sorry, I'm not able to parse that sentence.
Urgh :/. How about the following commit message:
Quoted 8 lines
"So far, our implementation of
WARN_ON()cannot be used in the following situation:if (
WARN_ON())
...This is because
WARN_ON()doesn't return whether a warning has been triggered. Such construciton can be handy if you want to print more information and also dump the stack trace.Therefore, rework the
WARN_ON()implementation to return whether a warning was triggered. The idea was borrowed from Linux".
Better :-)
With that you can add my:
Juergen
Bertrand Marquis
Reviewed-byHi Julien,
Quoted 25 lines · Julien Grall
Hi Juergen,
Quoted 11 lines · Jürgen Groß
Quoted 9 lines · Julien Grall
From: Julien Grall <[email protected]>
So far, our implementation of
WARN_ON()cannot be used in the following situation:if (
WARN_ON())...This is because the
WARN_ON()doesn't return whether a warning. Such... warning has been triggered.
I will add it.
Quoted 3 lines
Quoted 2 lines
construction can be handy to have if you have to print more information and now the stack track.
Sorry, I'm not able to parse that sentence.
Urgh :/. How about the following commit message:
Quoted 8 lines
"So far, our implementation of
WARN_ON()cannot be used in the following situation:if (
WARN_ON())
...This is because
WARN_ON()doesn't return whether a warning has been triggered. Such construciton can be handy if you want to print more information and also dump the stack trace.Therefore, rework the
WARN_ON()implementation to return whether a warning was triggered. The idea was borrowed from Linux".
With that.
And thanks a lot for this :-)
Cheers
Bertrand
Quoted 5 lines
Cheers,
Signature · 1 line
Julien Grall
xen/include/xen/lib.h line 27
Quoted 9 lines · Julien Grall
xen/include/xen/lib.h +2
-1@@ -23,7 +23,13 @@#include <asm/bug.h> #define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \
Please can you avoid leading underscores here?
xen/include/xen/lib.h line 32
Quoted 5 lines
xen/include/xen/lib.h +5
-0+ \ +if (unlikely (__ret_warn_on) ) \ +WARN (); \ +unlikely (__ret_warn_on); \ +})
Is this latter unlikely() having any effect? So far I thought it would need to be immediately inside a control construct or be an operand to && or ||.
Jan
Hi,
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
xen/include/xen/lib.h +0
-0@@ -23,7 +23,13 @@#include <asm/bug.h>
Quoted 6 lines
xen/include/xen/lib.h line 27
Quoted 4 lines
xen/include/xen/lib.h +2
-1#define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \Please can you avoid leading underscores here?
I can.
Quoted 10 lines
xen/include/xen/lib.h line 32
Quoted 6 lines
xen/include/xen/lib.h +5
-0+ \ +if (unlikely (__ret_warn_on) ) \ +WARN (); \ +unlikely (__ret_warn_on); \ +})Is this latter
unlikely()having any effect? So far I thought it would need to be immediately inside a control construct or be an operand to && or ||.
The unlikely() is directly taken from the Linux implementation.
My guess is the compiler is still able to use the information for the branch prediction in the case of:
if ( WARN_ON(...) )
Cheers,
Quoted 2 lines
Jan
Signature · 1 line
Julien Grall
Quoted 27 lines · Julien Grall
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
xen/include/xen/lib.h +0
-0@@ -23,7 +23,13 @@#include <asm/bug.h> Quoted 6 lines
xen/include/xen/lib.h line 27
Quoted 4 lines
xen/include/xen/lib.h +2
-1#define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \Please can you avoid leading underscores here?
I can.
Quoted 10 lines
xen/include/xen/lib.h line 32
Quoted 6 lines
xen/include/xen/lib.h +5
-0+ \ +if (unlikely (__ret_warn_on) ) \ +WARN (); \ +unlikely (__ret_warn_on); \ +})Is this latter
unlikely()having any effect? So far I thought it would need to be immediately inside a control construct or be an operand to && or ||.
Quoted 6 lines
The
unlikely()is directly taken from the Linux implementation.My guess is the compiler is still able to use the information for the branch prediction in the case of:
if (
WARN_ON(...))
Maybe. Or maybe not. I don't suppose the Linux commit introducing it clarifies this?
Jan
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Julien Grall
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
xen/include/xen/lib.h +0
-0@@ -23,7 +23,13 @@#include <asm/bug.h> Quoted 6 lines
xen/include/xen/lib.h line 27
Quoted 4 lines
xen/include/xen/lib.h +2
-1#define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \Please can you avoid leading underscores here?
I can.
Quoted 10 lines
xen/include/xen/lib.h line 32
Quoted 6 lines
xen/include/xen/lib.h +5
-0+ \ +if (unlikely (__ret_warn_on) ) \ +WARN (); \ +unlikely (__ret_warn_on); \ +})Is this latter
unlikely()having any effect? So far I thought it would need to be immediately inside a control construct or be an operand to && or ||.The
unlikely()is directly taken from the Linux implementation.
Quoted 7 lines
Quoted 4 lines
My guess is the compiler is still able to use the information for the branch prediction in the case of:
if (
WARN_ON(...))Maybe. Or maybe not. I don't suppose the Linux commit introducing it clarifies this?
I did a bit of digging but it looks like the unlikely has been there forever. I'd just keep it as is.
Quoted 35 lines · Stefano Stabellini
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Julien Grall
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
xen/include/xen/lib.h +0
-0@@ -23,7 +23,13 @@#include <asm/bug.h> Quoted 6 lines
xen/include/xen/lib.h line 27
Quoted 4 lines
xen/include/xen/lib.h +2
-1#define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \Please can you avoid leading underscores here?
I can.
Quoted 10 lines
xen/include/xen/lib.h line 32
Quoted 6 lines
xen/include/xen/lib.h +5
-0+ \ +if (unlikely (__ret_warn_on) ) \ +WARN (); \ +unlikely (__ret_warn_on); \ +})Is this latter
unlikely()having any effect? So far I thought it would need to be immediately inside a control construct or be an operand to && or ||.The
unlikely()is directly taken from the Linux implementation.My guess is the compiler is still able to use the information for the branch prediction in the case of:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
if (
WARN_ON(...))Maybe. Or maybe not. I don't suppose the Linux commit introducing it clarifies this?
I did a bit of digging but it looks like the unlikely has been there forever. I'd just keep it as is.
Thanks! I was planning to answer earlier on with some data but got preempted with some higher priority work.
The Linux commit message is not very helpful. I will do some testing so I can convince Jan that compilers can be clever and make use of it.
Cheers,
Quoted 34 lines · Stefano Stabellini
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Julien Grall
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
xen/include/xen/lib.h +0
-0@@ -23,7 +23,13 @@#include <asm/bug.h> Quoted 6 lines
xen/include/xen/lib.h line 27
Quoted 4 lines
xen/include/xen/lib.h +2
-1#define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \Please can you avoid leading underscores here?
I can.
Quoted 10 lines
xen/include/xen/lib.h line 32
Quoted 6 lines
xen/include/xen/lib.h +5
-0+ \ +if (unlikely (__ret_warn_on) ) \ +WARN (); \ +unlikely (__ret_warn_on); \ +})Is this latter
unlikely()having any effect? So far I thought it would need to be immediately inside a control construct or be an operand to && or ||.The
unlikely()is directly taken from the Linux implementation.My guess is the compiler is still able to use the information for the branch prediction in the case of:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
if (
WARN_ON(...))Maybe. Or maybe not. I don't suppose the Linux commit introducing it clarifies this?
I did a bit of digging but it looks like the unlikely has been there forever. I'd just keep it as is.
I'm afraid I don't view this as a reason to inherit code unchanged. If it was introduced with a clear indication that compilers can recognize it despite the somewhat unusual placement, then fine. But likely() / unlikely() quite often get put in more or less blindly - see the not uncommon unlikely(a && b) style of uses, which don't typically have the intended effect and would instead need to be unlikely(a) && unlikely(b) [assuming each condition alone is indeed deemed unlikely], unless compilers have learned to guess/infer what's meant between when I last looked at this and now.
Jan
Quoted 43 lines · Jan Beulich
Quoted 34 lines · Stefano Stabellini
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Julien Grall
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
xen/include/xen/lib.h +0
-0@@ -23,7 +23,13 @@#include <asm/bug.h> Quoted 6 lines
xen/include/xen/lib.h line 27
Quoted 4 lines
xen/include/xen/lib.h +2
-1#define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \Please can you avoid leading underscores here?
I can.
Quoted 10 lines
xen/include/xen/lib.h line 32
Quoted 6 lines
xen/include/xen/lib.h +5
-0+ \ +if (unlikely (__ret_warn_on) ) \ +WARN (); \ +unlikely (__ret_warn_on); \ +})Is this latter
unlikely()having any effect? So far I thought it would need to be immediately inside a control construct or be an operand to && or ||.The
unlikely()is directly taken from the Linux implementation.My guess is the compiler is still able to use the information for the branch prediction in the case of:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
if (
WARN_ON(...))Maybe. Or maybe not. I don't suppose the Linux commit introducing it clarifies this?
I did a bit of digging but it looks like the unlikely has been there forever. I'd just keep it as is.
Quoted 9 lines
I'm afraid I don't view this as a reason to inherit code unchanged. If it was introduced with a clear indication that compilers can recognize it despite the somewhat unusual placement, then fine. But
likely()/unlikely()quite often get put in more or less blindly - see the not uncommonunlikely(a && b)style of uses, which don't typically have the intended effect and would instead need to be unlikely(a) && unlikely(b) [assuming each condition alone is indeed deemed unlikely], unless compilers have learned to guess/infer what's meant between when I last looked at this and now.
I have made a little experiment and found that the unlikely() at the end of a macro is having effect.
The disassembly of
#define unlikely(x) __builtin_expect(!!(x), 0)
#define foo() ({ \
int i = !time(NULL); \
unlikely(i); })#include "stdio.h"
#include "time.h"
int main() {
if (foo())
puts("a");
return 0;
}is the same as that of:
#define unlikely(x) __builtin_expect(!!(x), 0)
#include "stdio.h"
#include "time.h"
int main() {
int i = !time(NULL);
if (unlikely(i))
puts("a");
return 0;
}while that of:
#include "stdio.h"
#include "time.h"
int main() {
int i = !time(NULL);
if (i)
puts("a");
return 0;
}is different.
Juergen
Quoted 54 lines · Jürgen Groß
Quoted 43 lines · Jan Beulich
Quoted 34 lines · Stefano Stabellini
Quoted 33 lines · Jan Beulich
Quoted 32 lines · Julien Grall
Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
xen/include/xen/lib.h +0
-0@@ -23,7 +23,13 @@#include <asm/bug.h> Quoted 6 lines
xen/include/xen/lib.h line 27
Quoted 4 lines
xen/include/xen/lib.h +2
-1#define BUG_ON (p) do { if (unlikely(p)) BUG(); } while (0) -#define WARN_ON (p) do { if (unlikely(p)) WARN(); } while (0) +#define WARN_ON (p) ({ \ + bool __ret_warn_on = (p); \Please can you avoid leading underscores here?
I can.
Quoted 10 lines
xen/include/xen/lib.h line 32
Quoted 6 lines
xen/include/xen/lib.h +5
-0+ \ +if (unlikely (__ret_warn_on) ) \ +WARN (); \ +unlikely (__ret_warn_on); \ +})Is this latter
unlikely()having any effect? So far I thought it would need to be immediately inside a control construct or be an operand to && or ||.The
unlikely()is directly taken from the Linux implementation.My guess is the compiler is still able to use the information for the branch prediction in the case of:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
if (
WARN_ON(...))Maybe. Or maybe not. I don't suppose the Linux commit introducing it clarifies this?
I did a bit of digging but it looks like the unlikely has been there forever. I'd just keep it as is.
Quoted 9 lines
I'm afraid I don't view this as a reason to inherit code unchanged. If it was introduced with a clear indication that compilers can recognize it despite the somewhat unusual placement, then fine. But
likely()/unlikely()quite often get put in more or less blindly - see the not uncommonunlikely(a && b)style of uses, which don't typically have the intended effect and would instead need to be unlikely(a) && unlikely(b) [assuming each condition alone is indeed deemed unlikely], unless compilers have learned to guess/infer what's meant between when I last looked at this and now.
Quoted 2 lines
I have made a little experiment and found that the
unlikely()at the end of a macro is having effect.
Okay, thanks - then my concern vanishes.
Jan