Skip to contentSkip to conversation

irqsave/restore improvements

PATCH Superseded Andrew Cooper 5 participants 21 messages Oct 21, 2013 – Oct 29, 2013

lore
Share & tools
lore
#0 lore raw

cover irqsave/restore improvements

This series contains two independent but related fixes to do with irqsave/restore semantics.

The first patch prevents local_irq_restore() from corrupting system flags.

The second was constructed using the compile errors from the third patch, separated for clarity and reordered to prevent bisection problems.

The third patch uses a BUILD_BUG_ON() to ensure that the flags parameter to spin_lock_irqsave is wide enough to not trucate the result from local_irq_save().

I have not compile tested on arm, so request that one of the maintainers explicitly acks the final patch. Any compile failures will almost certainly be fixed with an int->unsigned long conversion on the affected variable(s).

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Ian Campbell <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Tim Deegan <[email protected]>
CC George Dunlap <[email protected]>

--
1.7.10.4

#1 lore raw

PATCH 1/3 x86/irq: local_irq_restore() should not blindly popf

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/include/asm-x86/system.h |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/include/asm-x86/system.h +4 -2
index 6ab7d56..cbf0f6a 100644
@@ -159,8 +159,10 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    if ( x & X86_EFLAGS_IF )                                     \
+        local_irq_enable();                                      \
+    else                                                         \
+        local_irq_disable();                                     \
 })
 
 static inline int local_irq_is_enabled(void)
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH 1/3] x86/irq: local_irq_restore() should not blindly popf

Quoted 15 lines · Andrew Cooper

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/include/asm-x86/system.h |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/include/asm-x86/system.h +0 -0
index 6ab7d56..cbf0f6a 100644

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 166

Quoted 10 lines
xen/include/asm-x86/system.h +4 -2
@@ -159,8 +159,10 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    if ( x & X86_EFLAGS_IF )                                     \
+        local_irq_enable();                                      \
+    else                                                         \
+        local_irq_disable();                                     \
 })

This adds a branch in a potentially hot path.

Is the local_irq_disable() needed? Interrupts should already be disabled on entry.

David

#3 lore raw

Re: [PATCH 1/3] x86/irq: local_irq_restore() should not blindly popf

Quoted 29 lines · David Vrabel
Quoted 15 lines · Andrew Cooper

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/include/asm-x86/system.h |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/include/asm-x86/system.h +0 -0
index 6ab7d56..cbf0f6a 100644

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 166

Quoted 10 lines
xen/include/asm-x86/system.h +4 -2
@@ -159,8 +159,10 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    if ( x & X86_EFLAGS_IF )                                     \
+        local_irq_enable();                                      \
+    else                                                         \
+        local_irq_disable();                                     \
 })

This adds a branch in a potentially hot path.

Quoted 2 lines

Is the local_irq_disable() needed? Interrupts should already be disabled on entry.

If that is always true, and so we get rid of the else, then local_irq_restore() should ASSERT it on entry.

-- Keir

Quoted 1 line

David

#4 lore raw

Re: [PATCH 1/3] x86/irq: local_irq_restore() should not blindly popf

Quoted 32 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 29 lines · David Vrabel
Quoted 15 lines · Andrew Cooper

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/include/asm-x86/system.h |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/include/asm-x86/system.h +0 -0
index 6ab7d56..cbf0f6a 100644

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 166

Quoted 10 lines
xen/include/asm-x86/system.h +4 -2
@@ -159,8 +159,10 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    if ( x & X86_EFLAGS_IF )                                     \
+        local_irq_enable();                                      \
+    else                                                         \
+        local_irq_disable();                                     \
 })

This adds a branch in a potentially hot path.

Quoted 5 lines
Quoted 2 lines

Is the local_irq_disable() needed? Interrupts should already be disabled on entry.

If that is always true, and so we get rid of the else, then local_irq_restore() should ASSERT it on entry.

Let's not go that route - the macro is supposed to do what it says - restore the prior state of the interrupt flag.

To deal with the two branches the code adds I'd rather recommend merging current and to-be-restored flags - the necessary "and" and "or" are quite likely faster than any mis-predicted branch (and perhaps as fast as a correctly predicted one).

Jan

#5 lore raw

Re: [PATCH 1/3] x86/irq: local_irq_restore() should not blindly popf

Quoted 33 lines
Quoted 33 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Keir Fraser
Quoted 29 lines · David Vrabel
Quoted 15 lines · Andrew Cooper

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/include/asm-x86/system.h |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/include/asm-x86/system.h +0 -0
index 6ab7d56..cbf0f6a 100644

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 166

Quoted 10 lines
xen/include/asm-x86/system.h +4 -2
@@ -159,8 +159,10 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    if ( x & X86_EFLAGS_IF )                                     \
+        local_irq_enable();                                      \
+    else                                                         \
+        local_irq_disable();                                     \
 })

This adds a branch in a potentially hot path.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Is the local_irq_disable() needed? Interrupts should already be disabled on entry.

If that is always true, and so we get rid of the else, then local_irq_restore() should ASSERT it on entry.

Let's not go that route - the macro is supposed to do what it says - restore the prior state of the interrupt flag.

And there is actually an example in the tree that might get broken if we went that suggested route:
xen/arch/x86/io_apic.c:timer_irq_works(). Albeit I'm not sure - it looks like the function gets called only in contexts where interrupts are already enabled, but considering its use of local_irq_enable() that may not have been the case earlier. In any event - I don't think we should take the risk of some rarely executed (perhaps error handling only) path doing something like that.

Jan

#6 lore raw

Re: [PATCH 1/3] x86/irq: local_irq_restore() should not blindly popf

In reply to Andrew Cooper

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 165

Quoted 13 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
xen/include/asm-x86/system.h +4 -2
@@ -159,8 +159,10 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    if ( x & X86_EFLAGS_IF )                                     \
+        local_irq_enable();                                      \
+    else                                                         \
+        local_irq_disable();                                     \

_If_ you're going to re-do this using the mask-and-merge approach I suggested in the other reply, may I ask that you also replace the open coded (1<<9) a few lines down with X86_EFLAGS_IF (at once making the comment there superfluous)?

Jan

#7 lore raw

PATCH v2 1/3 x86/irq: local_irq_restore() should not blindly popf

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

While playing in this area, fixup an opencoded use of X86_EFLAGS_IF.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>

---

This is rather more RFC. It boots and runs VMs, so I am fairly sure it is functionally correct, but I cant help feeling there might be a neater way to do the inline assembly. Suggestions welcome.

---
 xen/include/asm-x86/system.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
xen/include/asm-x86/system.h +8 -3
index 6ab7d56..ff52671 100644
@@ -3,6 +3,7 @@
 
 #include <xen/lib.h>
 #include <xen/bitops.h>
+#include <asm/processor.h>
 
 #define read_segment_register(name)                             \
 ({  u16 __sel;                                                  \
@@ -159,15 +160,19 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    asm volatile (                                               \
+    "pushf" __OS "\n\t"                                          \
+    "and" __OS " %0, (%%" __OP "sp)\n\t"                         \
+    "orw %1, (%%" __OP "sp)\n\t"                                 \
+    "popf" __OS "\n\t" : : "g" ( ~X86_EFLAGS_IF ),               \
+                           "g" ( x & X86_EFLAGS_IF ) );          \
 })
 
 static inline int local_irq_is_enabled(void)
 {
     unsigned long flags;
     local_save_flags(flags);
-    return !!(flags & (1<<9)); /* EFLAGS_IF */
+    return !!(flags & X86_EFLAGS_IF);
 }
 
 #define BROKEN_ACPI_Sx          0x0001
Signature · 1 line
1.7.10.4
#8 lore raw

Re: [Patch 1/3 v2] x86/irq: local_irq_restore() should not blindly popf

Quoted 31 lines · Andrew Cooper

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

While playing in this area, fixup an opencoded use of X86_EFLAGS_IF.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>

---

This is rather more RFC. It boots and runs VMs, so I am fairly sure it is functionally correct, but I cant help feeling there might be a neater way to do the inline assembly. Suggestions welcome.

---
 xen/include/asm-x86/system.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
xen/include/asm-x86/system.h +1 -0
index 6ab7d56..ff52671 100644
@@ -3,6 +3,7 @@
 
 #include <xen/lib.h>
 #include <xen/bitops.h>
+#include <asm/processor.h>
 
 #define read_segment_register(name)                             \
 ({  u16 __sel;                                                  \

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 167

Quoted 10 lines
xen/include/asm-x86/system.h +5 -2
@@ -159,15 +160,19 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    asm volatile (                                               \
+    "pushf" __OS "\n\t"                                          \
+    "and" __OS " %0, (%%" __OP "sp)\n\t"                         \
+    "orw %1, (%%" __OP "sp)\n\t"                                 \
+    "popf" __OS "\n\t" : : "g" ( ~X86_EFLAGS_IF ),               \

Would this be better as a constant constraint ("i")?

Quoted 12 lines
xen/include/asm-x86/system.h +2 -1
+                           "g" ( x & X86_EFLAGS_IF ) );          \
 })
 
 static inline int local_irq_is_enabled(void)
 {
     unsigned long flags;
     local_save_flags(flags);
-    return !!(flags & (1<<9)); /* EFLAGS_IF */
+    return !!(flags & X86_EFLAGS_IF);
 }
 
 #define BROKEN_ACPI_Sx          0x0001
#9 lore raw

Re: [Patch 1/3 v2] x86/irq: local_irq_restore() should not blindly popf

Quoted 30 lines · Keir Fraser
Quoted 28 lines · Andrew Cooper

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

While playing in this area, fixup an opencoded use of X86_EFLAGS_IF.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>

---

This is rather more RFC. It boots and runs VMs, so I am fairly sure it is functionally correct, but I cant help feeling there might be a neater way to do the inline assembly. Suggestions welcome.

---
 xen/include/asm-x86/system.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
xen/include/asm-x86/system.h +1 -0
index 6ab7d56..ff52671 100644
@@ -3,6 +3,7 @@
 
 #include <xen/lib.h>
 #include <xen/bitops.h>
+#include <asm/processor.h>
 
Quoted 14 lines

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 167

Quoted 13 lines
xen/include/asm-x86/system.h +5 -2
 #define read_segment_register(name)                             \
 ({  u16 __sel;                                                  \
@@ -159,15 +160,19 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    asm volatile (                                               \
+    "pushf" __OS "\n\t"                                          \
+    "and" __OS " %0, (%%" __OP "sp)\n\t"                         \
+    "orw %1, (%%" __OP "sp)\n\t"                                 \
+    "popf" __OS "\n\t" : : "g" ( ~X86_EFLAGS_IF ),               \

Would this be better as a constant constraint ("i")?

I was wondering what the best practice for this would be.

In most cases, I would imagine that an immediate would be used. However, as this is a define and therefore forcibly inlined everywhere it is used, it is just possible that the compiler could find a ~X86_EFLAGS_IF already in context, and optimise down to an "and r64,r/m64".

~Andrew

Quoted 14 lines
Quoted 13 lines
xen/include/asm-x86/system.h +2 -1
+                           "g" ( x & X86_EFLAGS_IF ) );          \
 })
 
 static inline int local_irq_is_enabled(void)
 {
     unsigned long flags;
     local_save_flags(flags);
-    return !!(flags & (1<<9)); /* EFLAGS_IF */
+    return !!(flags & X86_EFLAGS_IF);
 }
 
 #define BROKEN_ACPI_Sx          0x0001
Acked-by
#10 lore raw

Re: [Patch 1/3 v2] x86/irq: local_irq_restore() should not blindly popf

Quoted 15 lines · Andrew Cooper
Quoted 6 lines
Quoted 6 lines

#define read_segment_register(name) \
({ u16 __sel; \

xen/include/asm-x86/system.h +0 -0
@@ -159,15 +160,19 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
Quoted 8 lines

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 167

Quoted 7 lines
xen/include/asm-x86/system.h +5 -2
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    asm volatile (                                               \
+    "pushf" __OS "\n\t"                                          \
+    "and" __OS " %0, (%%" __OP "sp)\n\t"                         \
+    "orw %1, (%%" __OP "sp)\n\t"                                 \
+    "popf" __OS "\n\t" : : "g" ( ~X86_EFLAGS_IF ),               \

Would this be better as a constant constraint ("i")?

Quoted 6 lines

I was wondering what the best practice for this would be.

In most cases, I would imagine that an immediate would be used. However, as this is a define and therefore forcibly inlined everywhere it is used, it is just possible that the compiler could find a ~X86_EFLAGS_IF already in context, and optimise down to an "and r64,r/m64".

Oh, g includes i, I forgot that. Well your choice is best then.

Quoted 1 line

~Andrew

#11 lore raw

Re: [Patch 1/3 v2] x86/irq: local_irq_restore() should not blindly popf

Quoted 18 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 15 lines · Andrew Cooper
Quoted 14 lines

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 167

Quoted 13 lines

#define read_segment_register(name) \
({ u16 __sel; \

xen/include/asm-x86/system.h +5 -2
@@ -159,15 +160,19 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    asm volatile (                                               \
+    "pushf" __OS "\n\t"                                          \
+    "and" __OS " %0, (%%" __OP "sp)\n\t"                         \
+    "orw %1, (%%" __OP "sp)\n\t"                                 \
+    "popf" __OS "\n\t" : : "g" ( ~X86_EFLAGS_IF ),               \

Would this be better as a constant constraint ("i")?

Quoted 8 lines
Quoted 6 lines

I was wondering what the best practice for this would be.

In most cases, I would imagine that an immediate would be used. However, as this is a define and therefore forcibly inlined everywhere it is used, it is just possible that the compiler could find a ~X86_EFLAGS_IF already in context, and optimise down to an "and r64,r/m64".

Oh, g includes i, I forgot that. Well your choice is best then.

Sorry, but no. "g" also includes "m", and
- the other operand of both operations is a memory operand already, so this one can't also be a memory one,
- on a non-debug build (without frame pointers) an eventual %rsp-relative memory location would be broken due to the shifted stack offsets resulting from the PUSHF.
Hence both constraints can at best be "ri".

Further I have a hard time seeing how the "orw" used above can even have built successfully: If a register gets picked (which ought to be the common case), opcode suffix and register name ought to collide. And "orw" is a bad choice here anyway, in that this is a 2-byte write following an 8-byte one.

And finally - what's the point of using __OS in new assembly constructs? I was actually considering cleaning up all this hard to read cruft, since we no longer care about the 32-bit case.

Jan

#12 lore raw

Re: [Patch 1/3 v2] x86/irq: local_irq_restore() should not blindly popf

Quoted 23 lines · Jan Beulich
Quoted 23 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 20 lines · Andrew Cooper
Quoted 6 lines
Quoted 6 lines

#define read_segment_register(name) \
({ u16 __sel; \

xen/include/asm-x86/system.h +0 -0
@@ -159,15 +160,19 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
Quoted 8 lines

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 167

Quoted 7 lines
xen/include/asm-x86/system.h +5 -2
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    asm volatile (                                               \
+    "pushf" __OS "\n\t"                                          \
+    "and" __OS " %0, (%%" __OP "sp)\n\t"                         \
+    "orw %1, (%%" __OP "sp)\n\t"                                 \
+    "popf" __OS "\n\t" : : "g" ( ~X86_EFLAGS_IF ),               \

Would this be better as a constant constraint ("i")?

I was wondering what the best practice for this would be.

In most cases, I would imagine that an immediate would be used. However, as this is a define and therefore forcibly inlined everywhere it is used, it is just possible that the compiler could find a ~X86_EFLAGS_IF already in context, and optimise down to an "and r64,r/m64".

Quoted 8 lines
Quoted 1 line

Oh, g includes i, I forgot that. Well your choice is best then.

Sorry, but no. "g" also includes "m", and
- the other operand of both operations is a memory operand already, so this one can't also be a memory one,
- on a non-debug build (without frame pointers) an eventual %rsp-relative memory location would be broken due to the shifted stack offsets resulting from the PUSHF.
Hence both constraints can at best be "ri".

Ok - I can change this.

Quoted 6 lines

Further I have a hard time seeing how the "orw" used above can even have built successfully: If a register gets picked (which ought to be the common case), opcode suffix and register name ought to collide. And "orw" is a bad choice here anyway, in that this is a 2-byte write following an 8-byte one.

GCC correctly picks a 2-byte register given the orw. Looking at the disassembly, it usually chooses %r12w

Why is symmetry of writes important here? We are possibly setting bit 9 alone.

Quoted 4 lines

And finally - what's the point of using __OS in new assembly constructs? I was actually considering cleaning up all this hard to read cruft, since we no longer care about the 32-bit case.

I am happy to remove __OS/__OP if that is considered a good thing moving forward - I was merely using the prevailing style.

~Andrew

#13 lore raw

Re: [Patch 1/3 v2] x86/irq: local_irq_restore() should not blindly popf

Quoted 10 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 5 lines · Jan Beulich

Further I have a hard time seeing how the "orw" used above can even have built successfully: If a register gets picked (which ought to be the common case), opcode suffix and register name ought to collide. And "orw" is a bad choice here anyway, in that this is a 2-byte write following an 8-byte one.

GCC correctly picks a 2-byte register given the orw. Looking at the disassembly, it usually chooses %r12w

Try compiling this

void test(unsigned long x) {
	asm volatile("orw %0, (%0)" :: "ri" (x));
}

with a 32-bit gcc (or with -m32). In fact I'm surprised the assembler doesn't generate a warning (or even error) in the 64-bit case - this very much smells like a bug (and I looked at that code the other day and got the impression that the 64-bit one would be _less_ forgiving here).

In any event - if you want to stay with "orw", you ought to use "%w<number>" for the operand.

Quoted 2 lines

Why is symmetry of writes important here? We are possibly setting bit 9 alone.

Store-to-load-forwarding works only when the store size is larger than the load one. So I shouldn't have drawn your attention to the preceding "andq" but to the following "pushfq" (though I wouldn't be surprised if mixed size writes to overlapping memory locations would also suffer from penalties).

Jan

#14 lore raw

PATCH v3 1/3 x86/irq: local_irq_restore() should not blindly popf

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

While playing in this area, fixup an opencoded use of X86_EFLAGS_IF.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>

---

Change in v3:
* asm tweaks

---
 xen/include/asm-x86/system.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
xen/include/asm-x86/system.h +8 -3
index 6ab7d56..7d9f31b 100644
@@ -3,6 +3,7 @@
 
 #include <xen/lib.h>
 #include <xen/bitops.h>
+#include <asm/processor.h>
 
 #define read_segment_register(name)                             \
 ({  u16 __sel;                                                  \
@@ -159,15 +160,19 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    asm volatile (                                               \
+        "pushfq\n\t"                                             \
+        "andq %0, (%%rsp)\n\t"                                   \
+        "orq  %1, (%%rsp)\n\t"                                   \
+        "popfq\n\t" : : "ri" ( ~X86_EFLAGS_IF ),                 \
+                        "ri" ( x & X86_EFLAGS_IF ) );            \
 })
 
 static inline int local_irq_is_enabled(void)
 {
     unsigned long flags;
     local_save_flags(flags);
-    return !!(flags & (1<<9)); /* EFLAGS_IF */
+    return !!(flags & X86_EFLAGS_IF);
 }
 
 #define BROKEN_ACPI_Sx          0x0001
Signature · 1 line
1.7.10.4
Acked-by on v3 1/3
#15 lore raw

Re: [PATCH 1/3 v3] x86/irq: local_irq_restore() should not blindly popf

Quoted 8 lines · Andrew Cooper

local_irq_restore() should only be concerned with possibly changing the interrupt flag. A blind popf could corrupt other system flags.

While playing in this area, fixup an opencoded use of X86_EFLAGS_IF.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
Quoted 43 lines

---

Change in v3:
* asm tweaks

---
 xen/include/asm-x86/system.h |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
xen/include/asm-x86/system.h +8 -3
index 6ab7d56..7d9f31b 100644
@@ -3,6 +3,7 @@
 
 #include <xen/lib.h>
 #include <xen/bitops.h>
+#include <asm/processor.h>
 
 #define read_segment_register(name)                             \
 ({  u16 __sel;                                                  \
@@ -159,15 +160,19 @@ static always_inline unsigned long __cmpxchg(
 #define local_irq_restore(x)                                     \
 ({                                                               \
     BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "push" __OS " %0 ; popf" __OS                 \
-                   : : "g" (x) : "memory", "cc" );               \
+    asm volatile (                                               \
+        "pushfq\n\t"                                             \
+        "andq %0, (%%rsp)\n\t"                                   \
+        "orq  %1, (%%rsp)\n\t"                                   \
+        "popfq\n\t" : : "ri" ( ~X86_EFLAGS_IF ),                 \
+                        "ri" ( x & X86_EFLAGS_IF ) );            \
 })
 
 static inline int local_irq_is_enabled(void)
 {
     unsigned long flags;
     local_save_flags(flags);
-    return !!(flags & (1<<9)); /* EFLAGS_IF */
+    return !!(flags & X86_EFLAGS_IF);
 }
 
 #define BROKEN_ACPI_Sx          0x0001
#16 lore raw

Re: [Patch 1/3 v2] x86/irq: local_irq_restore() should not blindly popf

In reply to Jan Beulich
Quoted 18 lines
Quoted 11 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Andrew Cooper
Quoted 5 lines · Jan Beulich

Further I have a hard time seeing how the "orw" used above can even have built successfully: If a register gets picked (which ought to be the common case), opcode suffix and register name ought to collide. And "orw" is a bad choice here anyway, in that this is a 2-byte write following an 8-byte one.

GCC correctly picks a 2-byte register given the orw. Looking at the disassembly, it usually chooses %r12w

Try compiling this

void test(unsigned long x) {
	asm volatile("orw %0, (%0)" :: "ri" (x));
}
Quoted 5 lines

with a 32-bit gcc (or with -m32). In fact I'm surprised the assembler doesn't generate a warning (or even error) in the 64-bit case - this very much smells like a bug (and I looked at that code the other day and got the impression that the 64-bit one would be _less_ forgiving here).

See http://www.sourceware.org/ml/binutils/2013-10/msg00358.html.

Jan

#17 lore raw

PATCH 2/3 xen: Widen flags parameter for spinlock_irqsave() and friends

In reply to Andrew Cooper

These issues were detected using the subsequent patch which forces a compilation error if the result from local_irq_save() would be truncated.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC George Dunlap <[email protected]>
---
 xen/common/sched_credit2.c |    7 ++++---
 xen/common/trace.c         |    2 +-
 2 files changed, 5 insertions(+), 4 deletions(-)
xen/common/sched_credit2.c +4 -3
index 84e547b..4e68375 100644
@@ -1480,7 +1480,7 @@ static void *
 csched_alloc_domdata(const struct scheduler *ops, struct domain *dom)
 {
     struct csched_dom *sdom;
-    int flags;
+    unsigned long flags;
 
     sdom = xzalloc(struct csched_dom);
     if ( sdom == NULL )
@@ -1524,7 +1524,7 @@ csched_dom_init(const struct scheduler *ops, struct domain *dom)
 static void
 csched_free_domdata(const struct scheduler *ops, void *data)
 {
-    int flags;
+    unsigned long flags;
     struct csched_dom *sdom = data;
 
     spin_lock_irqsave(&CSCHED_PRIV(ops)->lock, flags);
@@ -1944,7 +1944,8 @@ static void deactivate_runqueue(struct csched_private *prv, int rqi)
 
 static void init_pcpu(const struct scheduler *ops, int cpu)
 {
-    int rqi, flags;
+    int rqi;
+    unsigned long flags;
     struct csched_private *prv = CSCHED_PRIV(ops);
     struct csched_runqueue_data *rqd;
     spinlock_t *old_lock;
xen/common/trace.c +1 -1
index 63ea0b7..41ddc33 100644
@@ -420,7 +420,7 @@ int tb_control(xen_sysctl_tbuf_op_t *tbc)
          * hypercall returns, no more records should be placed into the buffers. */
         for_each_online_cpu(i)
         {
-            int flags;
+            unsigned long flags;
             spin_lock_irqsave(&per_cpu(t_lock, i), flags);
             per_cpu(lost_records, i)=0;
             spin_unlock_irqrestore(&per_cpu(t_lock, i), flags);
Signature · 1 line
1.7.10.4
#18 lore raw

PATCH 3/3 common/spinlock: Ensure the flags parameter is wide enough

In reply to Andrew Cooper

Because of the construction of spin_lock_irq() (and varients), the flags parameter could be trucated. Use a BUILD_BUG_ON() to verify the width of the parameter.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Ian Campbell <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Tim Deegan <[email protected]>

---

The previous patch in the series fixes the compilation issues I found as a result of this patch, but I have not tested on arm. I therefore request an explicit ack from an arm maintainer before this is committed.

---
 xen/include/xen/spinlock.h |   18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
xen/include/xen/spinlock.h +15 -3
index 76581c5..12b0a89 100644
@@ -188,7 +188,11 @@ int _rw_is_write_locked(rwlock_t *lock);
 
 #define spin_lock(l)                  _spin_lock(l)
 #define spin_lock_irq(l)              _spin_lock_irq(l)
-#define spin_lock_irqsave(l, f)       ((f) = _spin_lock_irqsave(l))
+#define spin_lock_irqsave(l, f)                                 \
+    ({                                                          \
+        BUILD_BUG_ON(sizeof(f) != sizeof(unsigned long));       \
+        ((f) = _spin_lock_irqsave(l));                          \
+    })
 
 #define spin_unlock(l)                _spin_unlock(l)
 #define spin_unlock_irq(l)            _spin_unlock_irq(l)
@@ -220,7 +224,11 @@ int _rw_is_write_locked(rwlock_t *lock);
 
 #define read_lock(l)                  _read_lock(l)
 #define read_lock_irq(l)              _read_lock_irq(l)
-#define read_lock_irqsave(l, f)       ((f) = _read_lock_irqsave(l))
+#define read_lock_irqsave(l, f)                                 \
+    ({                                                          \
+        BUILD_BUG_ON(sizeof(f) != sizeof(unsigned long));       \
+        ((f) = _read_lock_irqsave(l));                          \
+    })
 
 #define read_unlock(l)                _read_unlock(l)
 #define read_unlock_irq(l)            _read_unlock_irq(l)
@@ -229,7 +237,11 @@ int _rw_is_write_locked(rwlock_t *lock);
 
 #define write_lock(l)                 _write_lock(l)
 #define write_lock_irq(l)             _write_lock_irq(l)
-#define write_lock_irqsave(l, f)      ((f) = _write_lock_irqsave(l))
+#define write_lock_irqsave(l, f)                                \
+    ({                                                          \
+        BUILD_BUG_ON(sizeof(f) != sizeof(unsigned long));       \
+        ((f) = _write_lock_irqsave(l));                         \
+    })
 #define write_trylock(l)              _write_trylock(l)
 
 #define write_unlock(l)               _write_unlock(l)
Signature · 1 line
1.7.10.4
Acked-by on v1 3/3
#19 lore raw

Re: [PATCH 3/3] common/spinlock: Ensure the flags parameter is wide enough

Quoted 3 lines · Andrew Cooper

The previous patch in the series fixes the compilation issues I found as a result of this patch, but I have not tested on arm. I therefore request an explicit ack from an arm maintainer before this is committed.

I build tested just this patch on current staging and it failed, because I hadn't spotted that the previous patch was generic and not x86 specific.

With patch 2/3 added both arm32 and arm64 hypervisors build for me, so:

BTW cross-compiling the hypervisor is more or less trivial, compilers can be found at
https://launchpad.net/linaro-toolchain-binaries/+download and the runes are:

make dist-xen XEN_TARGET_ARCH=arm32 CROSS_COMPILER=arm-linux-gnueabihf- make dist-xen XEN_TARGET_ARCH=arm64 CROSS_COMPILER=aarch64-linux-gnu-

Ian.

Acked-by on the v1 series
#20 lore raw
In reply to Andrew Cooper
Quoted 16 lines · Andrew Cooper

This series contains two independent but related fixes to do with irqsave/restore semantics.

The first patch prevents local_irq_restore() from corrupting system flags.

The second was constructed using the compile errors from the third patch, separated for clarity and reordered to prevent bisection problems.

The third patch uses a BUILD_BUG_ON() to ensure that the flags parameter to spin_lock_irqsave is wide enough to not trucate the result from local_irq_save().

I have not compile tested on arm, so request that one of the maintainers explicitly acks the final patch. Any compile failures will almost certainly be fixed with an int->unsigned long conversion on the affected variable(s).

Quoted 7 lines
CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Ian Campbell <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Tim Deegan <[email protected]>
CC George Dunlap <[email protected]>
Quoted 3 lines

--
1.7.10.4