Skip to contentSkip to conversation

x86/irq: Improve local_irq_restore() code generation and performance

PATCH In review Andrew Cooper 3 participants 5 messages Dec 6, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/irq: Improve local_irq_restore() code generation and performance

popf is a horribly expensive instruction, while sti is an optimised fastpath. Switching popf for a conditional branch and sti caused an 8% perf improvement in various linux measurements.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/include/asm-x86/system.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
xen/include/asm-x86/system.h +2 -7
index 65e63de69a67..4be235472ecd 100644
@@ -267,13 +267,8 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
 })
 #define local_irq_restore(x)                                     \
 ({                                                               \
-    BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "pushfq\n\t"                                  \
-                   "andq %0, (%%rsp)\n\t"                        \
-                   "orq  %1, (%%rsp)\n\t"                        \
-                   "popfq"                                       \
-                   : : "i?r" ( ~X86_EFLAGS_IF ),                 \
-                       "ri" ( (x) & X86_EFLAGS_IF ) );           \
+    if ( (x) & X86_EFLAGS_IF )                                   \
+        local_irq_enable();                                      \
 })
 
 static inline int local_irq_is_enabled(void)
Signature · 1 line
2.11.0
#1 lore raw
Quoted 21 lines · Andrew Cooper

popf is a horribly expensive instruction, while sti is an optimised fastpath. Switching popf for a conditional branch and sti caused an 8% perf improvement in various linux measurements.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/include/asm-x86/system.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
xen/include/asm-x86/system.h +0 -0
index 65e63de69a67..4be235472ecd 100644
@@ -267,13 +267,8 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
 })
 #define local_irq_restore(x)                                     \
 ({                                                               \

xen/include/asm-x86/system.h static inline unsigned long array_index_mask_nospec(unsigned long index, · line 272

Quoted 10 lines
xen/include/asm-x86/system.h +2 -7
-    BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "pushfq\n\t"                                  \
-                   "andq %0, (%%rsp)\n\t"                        \
-                   "orq  %1, (%%rsp)\n\t"                        \
-                   "popfq"                                       \
-                   : : "i?r" ( ~X86_EFLAGS_IF ),                 \
-                       "ri" ( (x) & X86_EFLAGS_IF ) );           \
+    if ( (x) & X86_EFLAGS_IF )                                   \
+        local_irq_enable();                                      \
 })

Bah.  There's still the one total abuse of local_irq_restore() to disable interrupts.

I'll do a pre-requisite patch.

~Andrew

#2 lore raw
Quoted 27 lines · Andrew Cooper
Quoted 26 lines · Andrew Cooper

popf is a horribly expensive instruction, while sti is an optimised fastpath. Switching popf for a conditional branch and sti caused an 8% perf improvement in various linux measurements.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/include/asm-x86/system.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
xen/include/asm-x86/system.h +0 -5
index 65e63de69a67..4be235472ecd 100644
@@ -267,13 +267,8 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
 })
 #define local_irq_restore(x)                                     \
 ({                                                               \
-    BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "pushfq\n\t"                                  \
-                   "andq %0, (%%rsp)\n\t"                        \
-                   "orq  %1, (%%rsp)\n\t"                        \
-                   "popfq"                                       \
Quoted 8 lines

xen/include/asm-x86/system.h static inline unsigned long array_index_mask_nospec(unsigned long index, · line 272

Quoted 5 lines
xen/include/asm-x86/system.h +2 -2
-                   : : "i?r" ( ~X86_EFLAGS_IF ),                 \
-                       "ri" ( (x) & X86_EFLAGS_IF ) );           \
+    if ( (x) & X86_EFLAGS_IF )                                   \
+        local_irq_enable();                                      \
 })

Bah.  There's still the one total abuse of local_irq_restore() to disable interrupts.

Question is whether that's really to be considered an abuse: To me "restore" doesn't mean only "maybe re-enable", but also "maybe re-disable". And a conditional STI-or-CLI is likely still be better than POPF.

Jan

#3 lore raw
Quoted 35 lines · Jan Beulich
Quoted 26 lines · Andrew Cooper
Quoted 25 lines · Andrew Cooper

popf is a horribly expensive instruction, while sti is an optimised fastpath. Switching popf for a conditional branch and sti caused an 8% perf improvement in various linux measurements.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/include/asm-x86/system.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
xen/include/asm-x86/system.h +0 -4
index 65e63de69a67..4be235472ecd 100644
@@ -267,13 +267,8 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
 })
 #define local_irq_restore(x)                                     \
 ({                                                               \
-    BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "pushfq\n\t"                                  \
-                   "andq %0, (%%rsp)\n\t"                        \
-                   "orq  %1, (%%rsp)\n\t"                        \
Quoted 8 lines

xen/include/asm-x86/system.h static inline unsigned long array_index_mask_nospec(unsigned long index, · line 272

Quoted 6 lines
xen/include/asm-x86/system.h +2 -3
-                   "popfq"                                       \
-                   : : "i?r" ( ~X86_EFLAGS_IF ),                 \
-                       "ri" ( (x) & X86_EFLAGS_IF ) );           \
+    if ( (x) & X86_EFLAGS_IF )                                   \
+        local_irq_enable();                                      \
 })

Bah.  There's still the one total abuse of local_irq_restore() to disable interrupts.

Quoted 1 line

Question is whether that's really to be considered an abuse:

These are Linux's APIs, not ours, and they've spoken on the matter. Furthermore, I agree with this being an abuse of the mechanism.

Quoted 3 lines

To me
"restore" doesn't mean only "maybe re-enable", but also "maybe re-disable".

nor does "save" mean "save and disable", but that's what it does.

The naming may not be completely ideal, but the expected usage is very much one way.

Quoted 2 lines

And a conditional STI-or-CLI is likely still be better
than POPF.

It likely is better than popf, but for one single abuse which can be written in a better way anyway, it's really not worth it.

~Andrew

#4 lore raw
Quoted 47 lines · Andrew Cooper
Quoted 35 lines · Jan Beulich
Quoted 26 lines · Andrew Cooper
Quoted 25 lines · Andrew Cooper

popf is a horribly expensive instruction, while sti is an optimised fastpath. Switching popf for a conditional branch and sti caused an 8% perf improvement in various linux measurements.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/include/asm-x86/system.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
xen/include/asm-x86/system.h +0 -4
index 65e63de69a67..4be235472ecd 100644
@@ -267,13 +267,8 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
 })
 #define local_irq_restore(x)                                     \
 ({                                                               \
-    BUILD_BUG_ON(sizeof(x) != sizeof(long));                     \
-    asm volatile ( "pushfq\n\t"                                  \
-                   "andq %0, (%%rsp)\n\t"                        \
-                   "orq  %1, (%%rsp)\n\t"                        \
Quoted 8 lines

xen/include/asm-x86/system.h static inline unsigned long array_index_mask_nospec(unsigned long index, · line 272

Quoted 6 lines
xen/include/asm-x86/system.h +2 -3
-                   "popfq"                                       \
-                   : : "i?r" ( ~X86_EFLAGS_IF ),                 \
-                       "ri" ( (x) & X86_EFLAGS_IF ) );           \
+    if ( (x) & X86_EFLAGS_IF )                                   \
+        local_irq_enable();                                      \
 })

Bah.  There's still the one total abuse of local_irq_restore() to disable interrupts.

Quoted 1 line

Question is whether that's really to be considered an abuse:

These are Linux's APIs, not ours, and they've spoken on the matter. Furthermore, I agree with this being an abuse of the mechanism.

Quoted 3 lines

To me
"restore" doesn't mean only "maybe re-enable", but also "maybe re-disable".

nor does "save" mean "save and disable", but that's what it does.

Quoted 8 lines

The naming may not be completely ideal, but the expected usage is very much one way.

Quoted 2 lines

And a conditional STI-or-CLI is likely still be better
than POPF.

It likely is better than popf, but for one single abuse which can be written in a better way anyway, it's really not worth it.

Fine with me as long as we can be very certain that's it's really only one such case.

Jan