Skip to contentSkip to conversation

x86/fred: Send an EVENT_CHECK IPI on exit from NMI

PATCH In review Andrew Cooper 3 participants 9 messages Jun 24 – Jun 25

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/fred: Send an EVENT_CHECK IPI on exit from NMI

Returning from an NMI which hits guest context needs special casing in FRED mode just like it does in IDT mode.

Break nmi_exit_to_guest() out of handle_ist_exception(), and use it in entry_FRED_R3() also.

Expand the comment a little, and invert the conditional jump to compat_restore_all_guest() to avoid needing an #else clause for CONFIG_PV32.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>

Slightly RFC, not tested yet. (My AMD system takes an eternity to reboot)

For 4.22. Found during testing of FRED. The consqeuence is that we can end up scheduling while still in NMI context, after which things like the watchdog and other diagnostics don't work properly.

---
 xen/arch/x86/x86_64/entry-fred.S |  6 ++++
 xen/arch/x86/x86_64/entry.S      | 49 +++++++++++++++++++-------------
 2 files changed, 36 insertions(+), 19 deletions(-)
xen/arch/x86/x86_64/entry-fred.S +6 -0
index e9c84423dacd..1ad9694a043b 100644
@@ -20,6 +20,12 @@ FUNC(entry_FRED_R3, 4096)
         GET_STACK_END(14)
         movq    STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
 
+        /* NMIs need special handling on return to guest. */
+        movzbl  UREGS_ss + 6(%rsp), %eax
+        and     $0xf, %eax
+        cmp     $X86_ET_NMI, %al
+        je      nmi_exit_to_guest
+
         jmp     test_all_events
 #else
         BUG     /* Not Reached */
xen/arch/x86/x86_64/entry.S +30 -19

61 lines not on this page · raw message

base-commit: d42ace60290a4b4184ee2133b245b134fdf96fed

Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andrew Cooper

Returning from an NMI which hits guest context needs special casing in FRED mode just like it does in IDT mode.

Break nmi_exit_to_guest() out of handle_ist_exception(), and use it in entry_FRED_R3() also.

Expand the comment a little, and invert the conditional jump to compat_restore_all_guest() to avoid needing an #else clause for CONFIG_PV32.

provided of course ...

Quoted 1 line

Slightly RFC, not tested yet. (My AMD system takes an eternity to reboot)

... the results of this won't prove it wrong.

Quoted 3 lines

For 4.22. Found during testing of FRED. The consqeuence is that we can end up scheduling while still in NMI context, after which things like the watchdog and other diagnostics don't work properly.

May therefore want a Fixes: tag (it'll also want backporting aiui).

xen/arch/x86/x86_64/entry-fred.S FUNC(entry_FRED_R3, 4096) · line 25

Quoted 9 lines
xen/arch/x86/x86_64/entry-fred.S +3 -0
@@ -20,6 +20,12 @@ FUNC(entry_FRED_R3, 4096)
         GET_STACK_END(14)
         movq    STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
 
+        /* NMIs need special handling on return to guest. */
+        movzbl  UREGS_ss + 6(%rsp), %eax
+        and     $0xf, %eax

As you may be aware, I'm not overly happy with such literal numbers. But well, alternatives look a little involved. So just a remark, not a request to consider any kind of adjustment.

Quoted 20 lines
xen/arch/x86/x86_64/entry.S +14 -0
@@ -146,6 +146,35 @@ process_trap:
         jmp  test_all_events
 END(switch_to_kernel)
 
+/*
+ * When returning to guest from an NMI, we must execute an IRET/ERETU to
+ * re-enable NMIs, and must not process softirqs which can e.g. schedule
+ * rather than returning to guest context.
+ *
+ * If a softirq is pending, send ourselves an EVENT_CHECK IPI to compensate.
+ * This will cause softirq processing to occur upon leaving NMI context.
+ *
+ * %rbx: struct vcpu, %r14 stack_end
+ */
+FUNC(nmi_exit_to_guest)
+        mov     STACK_CPUINFO_FIELD(processor_id)(%r14), %eax
+        shl     $IRQSTAT_shift, %eax
+        lea     irq_stat + IRQSTAT_softirq_pending(%rip), %rcx

xen/arch/x86/x86_64/entry.S process_trap: · line 172

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +10 -0
+        cmpl    $0, (%rcx, %rax, 1)
+        je      1f
+        mov     $EVENT_CHECK_VECTOR, %edi
+        call    send_IPI_self
+1:
+        /* For restore_all_guest. */
+        mov     STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
+#ifdef CONFIG_PV32
+        mov     VCPU_domain(%rbx), %rax
+        cmpb    $0, DOMAIN_is_32bit_pv(%rax)

Would you be open to a little bit of trickery here while you move the code? The low 12 bits of %rbx are clear, so instead of $0 we could use %bl here.

xen/arch/x86/x86_64/entry.S process_trap: · line 176

Quoted 4 lines
xen/arch/x86/x86_64/entry.S +4 -0
+        jne     compat_restore_all_guest
+#endif
+        jmp     restore_all_guest
+END(nmi_exit_to_guest)

Much like you flipped the Jcc/JMP here, ...

xen/arch/x86/x86_64/entry.S FUNC(handle_ist_exception) · line 1240

Quoted 4 lines
xen/arch/x86/x86_64/entry.S +0 -0
@@ -1209,25 +1238,7 @@ FUNC(handle_ist_exception)
 #ifdef CONFIG_PV
         testb $3,UREGS_cs(%rsp)
         jz    restore_all_xen

... how about also making this plus ...

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +0 -10
-        /* Send an IPI to ourselves to cover for the lack of event checking. */
-        mov   STACK_CPUINFO_FIELD(processor_id)(%r14), %eax
-        shll  $IRQSTAT_shift,%eax
-        leaq  irq_stat+IRQSTAT_softirq_pending(%rip),%rcx
-        cmpl  $0,(%rcx,%rax,1)
-        je    1f
-        movl  $EVENT_CHECK_VECTOR,%edi
-        call  send_IPI_self
-1:
-        /* For restore_all_guest. */

xen/arch/x86/x86_64/entry.S FUNC(handle_ist_exception) · line 1241

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +1 -9
-        mov   STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
-#ifdef CONFIG_PV32
-        movq  VCPU_domain(%rbx),%rax
-        cmpb  $0,DOMAIN_is_32bit_pv(%rax)
-        je    restore_all_guest
-        jmp   compat_restore_all_guest
-#else
-        jmp   restore_all_guest
-#endif
+        jmp   nmi_exit_to_guest

... this

jnz nmi_exit_to_guest
jmp restore_all_xen

then allowing to fold with ...

xen/arch/x86/x86_64/entry.S FUNC(handle_ist_exception) · line 1244

Quoted 3 lines
xen/arch/x86/x86_64/entry.S +0 -0
 #else
         ASSERT_CONTEXT_IS_XEN
         jmp   restore_all_xen

... this?

Jan

#2 lore raw
Quoted 14 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper

Returning from an NMI which hits guest context needs special casing in FRED mode just like it does in IDT mode.

Break nmi_exit_to_guest() out of handle_ist_exception(), and use it in entry_FRED_R3() also.

Expand the comment a little, and invert the conditional jump to compat_restore_all_guest() to avoid needing an #else clause for CONFIG_PV32.

provided of course ...

Quoted 7 lines
Quoted 1 line

Slightly RFC, not tested yet. (My AMD system takes an eternity to reboot)

... the results of this won't prove it wrong.

Quoted 3 lines

For 4.22. Found during testing of FRED. The consqeuence is that we can end up scheduling while still in NMI context, after which things like the watchdog and other diagnostics don't work properly.

May therefore want a Fixes: tag (it'll also want backporting aiui).

Ah yes, I'd meant to set one, but forgot.

Fixes 87cfcbe9f0b5 ("x86/pv: Guest exception handling in FRED mode")
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/x86_64/entry-fred.S +0 -0
@@ -20,6 +20,12 @@ FUNC(entry_FRED_R3, 4096)
         GET_STACK_END(14)
         movq    STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
 
Quoted 6 lines

xen/arch/x86/x86_64/entry-fred.S FUNC(entry_FRED_R3, 4096) · line 25

Quoted 3 lines
xen/arch/x86/x86_64/entry-fred.S +3 -0
+        /* NMIs need special handling on return to guest. */
+        movzbl  UREGS_ss + 6(%rsp), %eax
+        and     $0xf, %eax

As you may be aware, I'm not overly happy with such literal numbers. But well, alternatives look a little involved. So just a remark, not a request to consider any kind of adjustment.

The 0xf cannot usefully be anything else.  It's the width of the event type field in a FRED frame, but you need to visually see it's less than 0xff or the switch from %eax to %al looks wrong.

The +6 can't be generated by asm-offsets because the infrastructure doesn't work on bitfields.

Quoted 25 lines
Quoted 25 lines
xen/arch/x86/x86_64/entry.S +18 -0
@@ -146,6 +146,35 @@ process_trap:
         jmp  test_all_events
 END(switch_to_kernel)
 
+/*
+ * When returning to guest from an NMI, we must execute an IRET/ERETU to
+ * re-enable NMIs, and must not process softirqs which can e.g. schedule
+ * rather than returning to guest context.
+ *
+ * If a softirq is pending, send ourselves an EVENT_CHECK IPI to compensate.
+ * This will cause softirq processing to occur upon leaving NMI context.
+ *
+ * %rbx: struct vcpu, %r14 stack_end
+ */
+FUNC(nmi_exit_to_guest)
+        mov     STACK_CPUINFO_FIELD(processor_id)(%r14), %eax
+        shl     $IRQSTAT_shift, %eax
+        lea     irq_stat + IRQSTAT_softirq_pending(%rip), %rcx
+        cmpl    $0, (%rcx, %rax, 1)
+        je      1f
+        mov     $EVENT_CHECK_VECTOR, %edi
+        call    send_IPI_self
Quoted 8 lines

xen/arch/x86/x86_64/entry.S process_trap: · line 172

Quoted 6 lines
xen/arch/x86/x86_64/entry.S +6 -0
+1:
+        /* For restore_all_guest. */
+        mov     STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
+#ifdef CONFIG_PV32
+        mov     VCPU_domain(%rbx), %rax
+        cmpb    $0, DOMAIN_is_32bit_pv(%rax)

Would you be open to a little bit of trickery here while you move the code? The low 12 bits of %rbx are clear, so instead of $0 we could use %bl here.

struct vcpu being page aligned is a convenience not a requirement.  It's hard alignment requirements are 32b and even then only with CONFIG_SHADOW.

Quoted 38 lines

xen/arch/x86/x86_64/entry.S process_trap: · line 176

Quoted 5 lines
xen/arch/x86/x86_64/entry.S +4 -0
+        jne     compat_restore_all_guest
+#endif
+        jmp     restore_all_guest
+END(nmi_exit_to_guest)

Much like you flipped the Jcc/JMP here, ...

xen/arch/x86/x86_64/entry.S FUNC(handle_ist_exception) · line 1240

Quoted 4 lines
xen/arch/x86/x86_64/entry.S +0 -0
@@ -1209,25 +1238,7 @@ FUNC(handle_ist_exception)
 #ifdef CONFIG_PV
         testb $3,UREGS_cs(%rsp)
         jz    restore_all_xen

... how about also making this plus ...

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +0 -10
-        /* Send an IPI to ourselves to cover for the lack of event checking. */
-        mov   STACK_CPUINFO_FIELD(processor_id)(%r14), %eax
-        shll  $IRQSTAT_shift,%eax
-        leaq  irq_stat+IRQSTAT_softirq_pending(%rip),%rcx
-        cmpl  $0,(%rcx,%rax,1)
-        je    1f
-        movl  $EVENT_CHECK_VECTOR,%edi
-        call  send_IPI_self
-1:
-        /* For restore_all_guest. */

xen/arch/x86/x86_64/entry.S FUNC(handle_ist_exception) · line 1241

Quoted 10 lines
xen/arch/x86/x86_64/entry.S +1 -9
-        mov   STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
-#ifdef CONFIG_PV32
-        movq  VCPU_domain(%rbx),%rax
-        cmpb  $0,DOMAIN_is_32bit_pv(%rax)
-        je    restore_all_guest
-        jmp   compat_restore_all_guest
-#else
-        jmp   restore_all_guest
-#endif
+        jmp   nmi_exit_to_guest

... this

jnz nmi_exit_to_guest
jmp restore_all_xen

Quoted 6 lines

then allowing to fold with ...

xen/arch/x86/x86_64/entry.S FUNC(handle_ist_exception) · line 1244

Quoted 3 lines
xen/arch/x86/x86_64/entry.S +0 -0
 #else
         ASSERT_CONTEXT_IS_XEN
         jmp   restore_all_xen

... this?

This makes the diff rather less legible (and specifically, far less obviously a "break out"), and changes the configurations that the ASSERT lives in.

Perhaps as a followup, but not in this patch.

~Andrew

#3 lore raw
In reply to Jan Beulich
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper

Returning from an NMI which hits guest context needs special casing in FRED mode just like it does in IDT mode.

Break nmi_exit_to_guest() out of handle_ist_exception(), and use it in entry_FRED_R3() also.

Expand the comment a little, and invert the conditional jump to compat_restore_all_guest() to avoid needing an #else clause for CONFIG_PV32.

Quoted 6 lines
Quoted 1 line

provided of course ...

Quoted 1 line

Slightly RFC, not tested yet. (My AMD system takes an eternity to reboot)

... the results of this won't prove it wrong.

Testing is good.  We now get core register state on all CPUs, rather than missing the ones which were in guest context.

~Andrew

#4 lore raw
In reply to Andrew Cooper

xen/arch/x86/x86_64/entry-fred.S FUNC(entry_FRED_R3, 4096) · line 29

Quoted 13 lines · Andrew Cooper
xen/arch/x86/x86_64/entry-fred.S +6 -0
@@ -20,6 +20,12 @@ FUNC(entry_FRED_R3, 4096)
         GET_STACK_END(14)
         movq    STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
 
+        /* NMIs need special handling on return to guest. */
+        movzbl  UREGS_ss + 6(%rsp), %eax
+        and     $0xf, %eax
+        cmp     $X86_ET_NMI, %al
+        je      nmi_exit_to_guest
+
         jmp     test_all_events

Actually, how about shrinking this to just

test %al, %al
jnz nmi_exit_to_guest

by having entry_from_pv() return a boolean?

Jan

#5 lore raw
Quoted 16 lines · Jan Beulich

xen/arch/x86/x86_64/entry-fred.S FUNC(entry_FRED_R3, 4096) · line 29

Quoted 13 lines · Andrew Cooper
xen/arch/x86/x86_64/entry-fred.S +6 -0
@@ -20,6 +20,12 @@ FUNC(entry_FRED_R3, 4096)
         GET_STACK_END(14)
         movq    STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
 
+        /* NMIs need special handling on return to guest. */
+        movzbl  UREGS_ss + 6(%rsp), %eax
+        and     $0xf, %eax
+        cmp     $X86_ET_NMI, %al
+        je      nmi_exit_to_guest
+
         jmp     test_all_events

Actually, how about shrinking this to just

Quoted 4 lines

test %al, %al
jnz nmi_exit_to_guest

by having entry_from_pv() return a boolean?

I considered that, and dismissed it.

It involve changing large chunks of traps.c (simply to compile) and puts far more than 4 instructions of logic onto the common path.

~Andrew

#6 lore raw
Quoted 20 lines · Andrew Cooper
Quoted 19 lines · Jan Beulich

xen/arch/x86/x86_64/entry-fred.S FUNC(entry_FRED_R3, 4096) · line 29

Quoted 13 lines · Andrew Cooper
xen/arch/x86/x86_64/entry-fred.S +6 -0
@@ -20,6 +20,12 @@ FUNC(entry_FRED_R3, 4096)
         GET_STACK_END(14)
         movq    STACK_CPUINFO_FIELD(current_vcpu)(%r14), %rbx
 
+        /* NMIs need special handling on return to guest. */
+        movzbl  UREGS_ss + 6(%rsp), %eax
+        and     $0xf, %eax
+        cmp     $X86_ET_NMI, %al
+        je      nmi_exit_to_guest
+
         jmp     test_all_events

Actually, how about shrinking this to just

test %al, %al
jnz nmi_exit_to_guest

Quoted 6 lines
Quoted 1 line

by having entry_from_pv() return a boolean?

I considered that, and dismissed it.

It involve changing large chunks of traps.c (simply to compile) and puts far more than 4 instructions of logic onto the common path.

Does it? Code size overall may grow, but since every return there is that of a constant (false in all but one case), every individual path would go down from 4 insns (with a memory access, admittedly a cache-hot one) to just 3 (the two here and the MOV or XOR on the return paths).

Jan

#7 lore raw
In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

Returning from an NMI which hits guest context needs special casing in FRED mode just like it does in IDT mode.

Break nmi_exit_to_guest() out of handle_ist_exception(), and use it in entry_FRED_R3() also.

Expand the comment a little, and invert the conditional jump to compat_restore_all_guest() to avoid needing an #else clause for CONFIG_PV32.

Quoted 8 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>

Slightly RFC, not tested yet. (My AMD system takes an eternity to reboot)

I would like to have a test on hardware to verify that it doesn't break something else. With that:

Quoted 5 lines

For 4.22. Found during testing of FRED. The consqeuence is that we can end up scheduling while still in NMI context, after which things like the watchdog and other diagnostics don't work properly.
---

Thanks.

~ Oleksii

#8 lore raw
Quoted 21 lines · Oleksii Kurochko
Quoted 18 lines · Andrew Cooper

Returning from an NMI which hits guest context needs special casing in FRED
mode just like it does in IDT mode.

Break nmi_exit_to_guest() out of handle_ist_exception(), and use it in entry_FRED_R3() also.

Expand the comment a little, and invert the conditional jump to compat_restore_all_guest() to avoid needing an #else clause for CONFIG_PV32.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>
CC: Oleksii Kurochko <[email protected]>

Quoted 5 lines
Quoted 2 lines

Slightly RFC, not tested yet.  (My AMD system takes an eternity to reboot)

I would like to have a test on hardware to verify that it doesn't break something else.

Yes, just confirmed elsewhere on this thread.

Quoted 11 lines

 With that:

Quoted 7 lines

For 4.22.  Found during testing of FRED.  The consqeuence is that we can end
up scheduling while still in NMI context, after which things like the watchdog
and other diagnostics don't work properly.
---

Thankyou.

~Andrew