Skip to contentSkip to conversation

xen/irq: Perf improvements

PATCH In review Andrew Cooper 2 participants 7 messages Feb 20, 2023 – Feb 22, 2023

lore
Share & tools
lore
#0 lore raw

cover xen/irq: Perf improvements

Andrew Cooper (2):
xen/ioapic: Don't use local_irq_restore() to disable irqs x86/irq: Improve local_irq_restore() code generation and performance

 xen/arch/x86/include/asm/system.h | 9 ++-------
 xen/arch/x86/io_apic.c            | 5 ++---
 2 files changed, 4 insertions(+), 10 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 xen/ioapic: Don't use local_irq_restore() to disable irqs

Despite its name, the irq{save,restore}() APIs are only intended to conditionally disable and re-enable interrupts.

IO-APIC's timer_irq_works() violates this intention. As it is init code, switch to simple irq enable/disable().

No functional change.

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

This is a logical equivelent to Linux's 058df195c2 "x86/ioapic: Cleanup the timer_works() irqflags mess", but we've diverged far enough for the patch to not be remotely relevant.

---
 xen/arch/x86/io_apic.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/io_apic.c +2 -3
index 9b8a972cf570..199098fa3e0f 100644
@@ -1502,15 +1502,14 @@ static void __init setup_ioapic_ids_from_mpc(void)
  */
 static int __init timer_irq_works(void)
 {
-    unsigned long t1, flags;
+    unsigned long t1;
 
     t1 = ACCESS_ONCE(pit0_ticks);
 
-    local_save_flags(flags);
     local_irq_enable();
     /* Let ten ticks pass... */
     mdelay((10 * 1000) / HZ);
-    local_irq_restore(flags);
+    local_irq_disable();
 
     /*
      * Expect a few ticks at least, to be sure some possible
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] xen/ioapic: Don't use local_irq_restore() to disable irqs

Quoted 2 lines · Andrew Cooper

Despite its name, the irq{save,restore}() APIs are only intended to conditionally disable and re-enable interrupts.

Are they? Maybe nowadays they indeed are, but I couldn't spot any wording to this effect in Linux'es Documentation/ (and I don't think we have anywhere such could be put). Yet I'd expect such a statement to be backed by something.

Quoted 2 lines

IO-APIC's timer_irq_works() violates this intention. As it is init code, switch to simple irq enable/disable().

If all callers of the function obviously did have interrupts off, I might agree. But the last of them sits _after_ local_irq_restore(), and all of this is called from underneath smp_prepare_cpus(), i.e. after IRQs were already enabled. I'm willing to believe that the local_irq_restore() there really comes too early, but then, ...

Quoted 1 line

No functional change.

... in order for this to be true, that'll need fixing at the same time (or in a prereq patch).

Jan

#3 lore raw

Re: [PATCH 1/2] xen/ioapic: Don't use local_irq_restore() to disable irqs

Quoted 4 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

Despite its name, the irq{save,restore}() APIs are only intended to conditionally disable and re-enable interrupts.

Are they?

Yes, absolutely.

And as said before, the potentially dubious naming does not give us permission or the right to interpret the behaviour differently.

Quoted 4 lines

Maybe nowadays they indeed are, but I couldn't spot any wording
to this effect in Linux'es Documentation/ (and I don't think we have anywhere such could be put). Yet I'd expect such a statement to be backed by something.

It is backed by the rude words the owners of this API had to say on discovering this particular use.

Conditionally enabling with a construct like this is bogus everywhere. It is only ever safe to enable irqs if you know exactly why they were disabled previously, and that can never be the case with a construct like this.

It only reason this one example doesn't explode is because its an init path not nested within an irq/irqsave lock or other critical region.

Quoted 6 lines
Quoted 3 lines

IO-APIC's timer_irq_works() violates this intention. As it is init code, switch to simple irq enable/disable().

If all callers of the function obviously did have interrupts off, I might agree. But the last of them sits _after_ local_irq_restore(), and all of this is called from underneath smp_prepare_cpus()

Which do you mean "the last of them" ?

There is a large amount of genuinely dead logic here.

~Andrew

#4 lore raw

Re: [PATCH 1/2] xen/ioapic: Don't use local_irq_restore() to disable irqs

Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Jan Beulich
Quoted 2 lines · Andrew Cooper

Despite its name, the irq{save,restore}() APIs are only intended to conditionally disable and re-enable interrupts.

Are they?

Yes, absolutely.

And as said before, the potentially dubious naming does not give us permission or the right to interpret the behaviour differently.

When I started to work with Linux, I seem to recall there were more uses of this nature, not considered dubious at the time. Views change, and if such views aren't expressed in the function names, then it is even more so important that this is spelled out in some other prominent way. So ...

Quoted 7 lines
Quoted 4 lines

Maybe nowadays they indeed are, but I couldn't spot any wording
to this effect in Linux'es Documentation/ (and I don't think we have anywhere such could be put). Yet I'd expect such a statement to be backed by something.

It is backed by the rude words the owners of this API had to say on discovering this particular use.

... have those rude words found their way into documentation, in a place I simply didn't spot?

Quoted 7 lines

Conditionally enabling with a construct like this is bogus everywhere. It is only ever safe to enable irqs if you know exactly why they were disabled previously, and that can never be the case with a construct like this.

It only reason this one example doesn't explode is because its an init path not nested within an irq/irqsave lock or other critical region.

Which is why, I assume, it was deemed fine to code that way back then.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

IO-APIC's timer_irq_works() violates this intention. As it is init code, switch to simple irq enable/disable().

If all callers of the function obviously did have interrupts off, I might agree. But the last of them sits _after_ local_irq_restore(), and all of this is called from underneath smp_prepare_cpus()

Which do you mean "the last of them" ?

Is "last" ambiguous here in any way? If so, this code fragment is the one:

    unlock_ExtINT_logic();

    local_irq_restore(flags);

    if (timer_irq_works()) {
        printk(" works.\n");
        return;
    }
    printk(" failed :(.\n");
    panic("IO-APIC + timer doesn't work!  Boot with apic_verbosity=debug and send a report\n");
}

Besides aspects one may deem benign and/or pre-existing, your change leads to the "works" path now running (returning) with IRQs off, when the caller expects them on.

Quoted 1 line

There is a large amount of genuinely dead logic here.

Possibly.

Jan

#5 lore raw

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

In reply to 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. While I don't expect the change to be that dramatic in Xen, there will be an improvement.

Furthermore, there is the following code generation improvement:

add/remove: 0/0 grow/shrink: 3/52 up/down: 52/-966 (-914)

owing to not needing to opencode the restriction to just IF in asm.

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

---
 xen/arch/x86/include/asm/system.h | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
xen/arch/x86/include/asm/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.30.2
#6 lore raw

Re: [PATCH 2/2] x86/irq: Improve local_irq_restore() code generation and performance

Quoted 6 lines · Andrew Cooper
xen/arch/x86/include/asm/system.h +0 -0
@@ -267,13 +267,8 @@ static inline unsigned long array_index_mask_nospec(unsigned long index,
 })
 #define local_irq_restore(x)                                     \
 ({                                                               \

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

Quoted 10 lines
xen/arch/x86/include/asm/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();                                      \
 })

Without it being written down anywhere that IRQs cannot be turned off this way, and without there being a reference to that documentation in the description, this is introducing a plain bug; I'm sorry to say it that way. With both of the above fulfilled I'd of course be happy to see the improvement take effect.

Jan