Skip to contentSkip to conversation

xen: make sure that likely and unlikely convert the expression to a boolean

PATCH In review Ian Campbell 2 participants 5 messages Apr 7, 2014 – Apr 10, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: make sure that likely and unlikely convert the expression to a boolean

According to http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
__builtin_expect has the prototype:
long __builtin_expect (long exp, long c)

If sizeof(exp) > sizeof(long) then this will effectively mask off the top bits of exp, meaning that the if in "if (unlikey(x))" will see the masked version, which might be false when true was expected, likely has the same issue.

With the x86_32 hypervisor no longer existing this is mostly likely to affect arm32 builds. A quick grep however shows that all the existing arm32 uses of both likely and unlikely already pass a boolean. I noticed this with an as yet unposted patch which did not have this property.

Also the defintion of likely might not have had the expected affect for cases where a true value > 1 might be passed.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Tim Deegan <[email protected]>
---
 xen/include/xen/compiler.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xen/compiler.h +2 -2
index 6e07990..4b3472d 100644
@@ -7,8 +7,8 @@
 
 #define barrier()     __asm__ __volatile__("": : :"memory")
 
-#define likely(x)     __builtin_expect((x),1)
-#define unlikely(x)   __builtin_expect((x),0)
+#define likely(x)     __builtin_expect(!!(x),1)
+#define unlikely(x)   __builtin_expect(!!(x),0)
 
 #define inline        __inline__
 #define always_inline __inline__ __attribute__ ((always_inline))
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 07.04.14 at 13:07, <[email protected]> wrote:

According to http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
__builtin_expect has the prototype:
long __builtin_expect (long exp, long c)

Quoted 8 lines

If sizeof(exp) > sizeof(long) then this will effectively mask off the top bits of exp, meaning that the if in "if (unlikey(x))" will see the masked version, which might be false when true was expected, likely has the same issue.

With the x86_32 hypervisor no longer existing this is mostly likely to affect arm32 builds. A quick grep however shows that all the existing arm32 uses of both likely and unlikely already pass a boolean. I noticed this with an as yet unposted patch which did not have this property.

Good catch, except that at least in the 4.2 tree we still care for the x86_32 hypervisor, and I already spotted a case having the same issue (in mtrr_var_range_msr_set()). I.e. for the purposes of backporting it might be better to make the statement above a little less tailored to arm32.

I wonder how we managed to miss the similar change in Linux - even 2.6.5 already has it that way.

Quoted 13 lines
xen/include/xen/compiler.h +2 -2
@@ -7,8 +7,8 @@
 
 #define barrier()     __asm__ __volatile__("": : :"memory")
 
-#define likely(x)     __builtin_expect((x),1)
-#define unlikely(x)   __builtin_expect((x),0)
+#define likely(x)     __builtin_expect(!!(x),1)
+#define unlikely(x)   __builtin_expect(!!(x),0)
 
 #define inline        __inline__
 #define always_inline __inline__ __attribute__ ((always_inline))
#2 lore raw
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 07.04.14 at 13:07, <[email protected]> wrote:

According to http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
__builtin_expect has the prototype:
long __builtin_expect (long exp, long c)

If sizeof(exp) > sizeof(long) then this will effectively mask off the top bits of exp, meaning that the if in "if (unlikey(x))" will see the masked version, which might be false when true was expected, likely has the same issue.

Quoted 6 lines
Quoted 4 lines

With the x86_32 hypervisor no longer existing this is mostly likely to affect arm32 builds. A quick grep however shows that all the existing arm32 uses of both likely and unlikely already pass a boolean. I noticed this with an as yet unposted patch which did not have this property.

Good catch,

I tore out some hair before I got there of course ;-)

Quoted 5 lines

except that at least in the 4.2 tree we still care for the
x86_32 hypervisor, and I already spotted a case having the same issue (in mtrr_var_range_msr_set()). I.e. for the purposes of backporting it might be better to make the statement above a little less tailored to arm32.

That's fine by me. Do you mean for me to extend it now or did you want to change it as part of the backport process? Either is fine by me.

How about:
This is mostly likely to affect x86_32 and arm32 builds. x86_32 is not present on 4.3 onwards and a quick grep of current staging shows that all the existing arm32... <etc as before>
?

Quoted 2 lines

I wonder how we managed to miss the similar change in Linux - even 2.6.5 already has it that way.

I gave up looking at v2.12-rc1 (first git commit), but yeah, don't know how we missed it.

Quoted 17 lines
Quoted 13 lines
xen/include/xen/compiler.h +2 -2
@@ -7,8 +7,8 @@
 
 #define barrier()     __asm__ __volatile__("": : :"memory")
 
-#define likely(x)     __builtin_expect((x),1)
-#define unlikely(x)   __builtin_expect((x),0)
+#define likely(x)     __builtin_expect(!!(x),1)
+#define unlikely(x)   __builtin_expect(!!(x),0)
 
 #define inline        __inline__
 #define always_inline __inline__ __attribute__ ((always_inline))
#3 lore raw
Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 07.04.14 at 14:42, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 07.04.14 at 13:07, <[email protected]> wrote:

According to http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
__builtin_expect has the prototype:
long __builtin_expect (long exp, long c)

If sizeof(exp) > sizeof(long) then this will effectively mask off the top

bits

Quoted 1 line
Quoted 1 line

of exp, meaning that the if in "if (unlikey(x))" will see the masked

version,

Quoted 3 lines
Quoted 3 lines

which might be false when true was expected, likely has the same issue.

With the x86_32 hypervisor no longer existing this is mostly likely to

affect

Quoted 1 line
Quoted 1 line

arm32 builds. A quick grep however shows that all the existing arm32 uses

of

Quoted 1 line
Quoted 1 line

both likely and unlikely already pass a boolean. I noticed this with an as

yet

Quoted 3 lines
Quoted 1 line

unposted patch which did not have this property.

Good catch,

I tore out some hair before I got there of course ;-)

Quoted 5 lines

except that at least in the 4.2 tree we still care for the
x86_32 hypervisor, and I already spotted a case having the same issue (in mtrr_var_range_msr_set()). I.e. for the purposes of backporting it might be better to make the statement above a little less tailored to arm32.

Quoted 8 lines

That's fine by me. Do you mean for me to extend it now or did you want to change it as part of the backport process? Either is fine by me.

How about:
This is mostly likely to affect x86_32 and arm32 builds. x86_32 is not present on 4.3 onwards and a quick grep of current staging shows that all the existing arm32... <etc as before>
?

Yes, that reads fine for both -unstable and the backport.

Jan

author Reviewed-by Jan Beulich
#4 lore raw
Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 07.04.14 at 14:42, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 07.04.14 at 13:07, <[email protected]> wrote:

According to http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
__builtin_expect has the prototype:
long __builtin_expect (long exp, long c)

If sizeof(exp) > sizeof(long) then this will effectively mask off the top

bits

Quoted 1 line
Quoted 1 line

of exp, meaning that the if in "if (unlikey(x))" will see the masked

version,

Quoted 3 lines
Quoted 3 lines

which might be false when true was expected, likely has the same issue.

With the x86_32 hypervisor no longer existing this is mostly likely to

affect

Quoted 1 line
Quoted 1 line

arm32 builds. A quick grep however shows that all the existing arm32 uses

of

Quoted 1 line
Quoted 1 line

both likely and unlikely already pass a boolean. I noticed this with an as

yet

Quoted 3 lines
Quoted 1 line

unposted patch which did not have this property.

Good catch,

I tore out some hair before I got there of course ;-)

Quoted 5 lines

except that at least in the 4.2 tree we still care for the
x86_32 hypervisor, and I already spotted a case having the same issue (in mtrr_var_range_msr_set()). I.e. for the purposes of backporting it might be better to make the statement above a little less tailored to arm32.

That's fine by me. Do you mean for me to extend it now or did you want to change it as part of the backport process? Either is fine by me.

Quoted 7 lines
Quoted 5 lines

How about:
This is mostly likely to affect x86_32 and arm32 builds. x86_32 is not present on 4.3 onwards and a quick grep of current staging shows that all the existing arm32... <etc as before>
?

Yes, that reads fine for both -unstable and the backport.

Thanks. Here's what I ended up applying:

Quoted 1 line

From e5545fb6d0dc5e2c48b2e450d18246d9bc1ae35b Mon Sep 17 00:00:00 2001

From: Ian Campbell <[email protected]>
Date: Mon, 7 Apr 2014 12:07:04 +0100
Subject: [PATCH] xen: make sure that likely and unlikely convert the
expression to a boolean

According to http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
__builtin_expect has the prototype:
long __builtin_expect (long exp, long c)

If sizeof(exp) > sizeof(long) then this will effectively mask off the top bits of exp, meaning that the if in "if (unlikey(x))" will see the masked version, which might be false when true was expected, likely has the same issue.

This is mostly likely to affect x86_32 and arm32 builds. x86_32 is not present on 4.3 onwards and a quick grep of current staging shows that all the existing arm32 uses of both likely and unlikely already pass a boolean. I noticed this with an as yet unposted patch which did not have this property.

Also the defintion of likely might not have had the expected affect for cases where a true value > 1 might be passed.

---
 xen/include/xen/compiler.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xen/compiler.h +2 -2
index 6e07990..4b3472d 100644
@@ -7,8 +7,8 @@
 
 #define barrier()     __asm__ __volatile__("": : :"memory")
 
-#define likely(x)     __builtin_expect((x),1)
-#define unlikely(x)   __builtin_expect((x),0)
+#define likely(x)     __builtin_expect(!!(x),1)
+#define unlikely(x)   __builtin_expect(!!(x),0)
 
 #define inline        __inline__
 #define always_inline __inline__ __attribute__ ((always_inline))
Signature · 1 line
1.7.10.4