Skip to contentSkip to conversation

x86/hvm: more sure APIC assist is aborted if guest EOIs APIC

PATCH In review Paul Durrant 2 participants 10 messages Jan 17, 2018 – Jan 18, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/hvm: more sure APIC assist is aborted if guest EOIs APIC

It appears there is a case where Windows enables the APIC assist enlightenment (see Microsoft Hypervisor Top Level Functional Spec. 5.0b, section 10.3.5) but does not use it. This scenario is perfectly valid according to the documentation, but causes the state machine in Xen to become confused leading to a domain_crash() such as the following:

(XEN) d4: VIRIDIAN GUEST_OS_ID: vendor: 1 os: 4 major: 6 minor: 1 sp: 0

      build: 1db0

(XEN) d4: VIRIDIAN HYPERCALL: enabled: 1 pfn: 3ffff (XEN) d4v0: VIRIDIAN VP_ASSIST_PAGE: enabled: 1 pfn: 3fffe (XEN) domain_crash called from viridian.c:452 (XEN) Domain 4 (vcpu#0) crashed on cpu#1:

The following sequence of events is an example of how this can happen:

- On return to guest vlapic_has_pending_irq() finds a bit set in the IRR.
- vlapic_ack_pending_irq() calls viridian_start_apic_assist() which latches the vector, sets the bit in the ISR and clears it from the IRR.
- The guest then processes the interrupt but EOIs it normally, therefore clearing the bit in the ISR.
- On next return to guest vlapic_has_pending_irq() calls viridian_complete_apic_assist(), which discovers the assist bit still set in the shared page and therefore leaves the latched vector in place, but also finds another bit set in the IRR.
- vlapic_ack_pending_irq() is then called but, because the ISR is was cleared by the EOI, another call is made to viridian_start_apic_assist() and this then calls domain_crash() because it finds the latched vector has not been cleared.

This patch adds a call to viridian_abort_apic_assist() into vlapic EOI handler to make sure any latched vector is cleared when the ISR is cleared.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/vlapic.c | 7 +++++++
 1 file changed, 7 insertions(+)
xen/arch/x86/hvm/vlapic.c +7 -0
index 50f53bdaef..3184970c6e 100644
@@ -422,6 +422,13 @@ void vlapic_EOI_set(struct vlapic *vlapic)
     if ( vector == -1 )
         return;
 
+    /*
+     * It is possible that APIC assist has been enabled by the guest but
+     * it has chosen not to use it, by EOIing normally. It is therefore
+     * necessary to abort any APIC assist that may have been started
+     * to avoid confusing the state machine.
+     */
+    viridian_abort_apic_assist(vlapic_vcpu(vlapic));
     vlapic_clear_vector(vector, &vlapic->regs->data[APIC_ISR]);
 
     if ( hvm_funcs.handle_eoi )
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 8 lines

-----Original Message-----
From: Paul Durrant [mailto:[email protected]]
Sent: 17 January 2018 12:54
To: [email protected]
Cc: Paul Durrant <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>
Subject: [PATCH] x86/hvm: more sure APIC assist is aborted if guest EOIs APIC

Sorry, I typo-ed the title. If there the no objections to the patch then hopefully this can be fixed up on commit.

Paul

Quoted 59 lines

It appears there is a case where Windows enables the APIC assist enlightenment (see Microsoft Hypervisor Top Level Functional Spec. 5.0b, section 10.3.5) but does not use it. This scenario is perfectly valid according to the documentation, but causes the state machine in Xen to become confused leading to a domain_crash() such as the following:

(XEN) d4: VIRIDIAN GUEST_OS_ID: vendor: 1 os: 4 major: 6 minor: 1 sp: 0

      build: 1db0

(XEN) d4: VIRIDIAN HYPERCALL: enabled: 1 pfn: 3ffff (XEN) d4v0: VIRIDIAN VP_ASSIST_PAGE: enabled: 1 pfn: 3fffe (XEN) domain_crash called from viridian.c:452 (XEN) Domain 4 (vcpu#0) crashed on cpu#1:

The following sequence of events is an example of how this can happen:

- On return to guest vlapic_has_pending_irq() finds a bit set in the IRR.
- vlapic_ack_pending_irq() calls viridian_start_apic_assist() which latches the vector, sets the bit in the ISR and clears it from the IRR.
- The guest then processes the interrupt but EOIs it normally, therefore clearing the bit in the ISR.
- On next return to guest vlapic_has_pending_irq() calls viridian_complete_apic_assist(), which discovers the assist bit still set in the shared page and therefore leaves the latched vector in place, but also finds another bit set in the IRR.
- vlapic_ack_pending_irq() is then called but, because the ISR is was cleared by the EOI, another call is made to viridian_start_apic_assist() and this then calls domain_crash() because it finds the latched vector has not been cleared.

This patch adds a call to viridian_abort_apic_assist() into vlapic EOI handler to make sure any latched vector is cleared when the ISR is cleared.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/vlapic.c | 7 +++++++
 1 file changed, 7 insertions(+)
xen/arch/x86/hvm/vlapic.c +7 -1
index 50f53bdaef..3184970c6e 100644
@@ -422,6 +422,13 @@ void vlapic_EOI_set(struct vlapic *vlapic)
     if ( vector == -1 )
         return;
 
+    /*
+     * It is possible that APIC assist has been enabled by the guest but
+     * it has chosen not to use it, by EOIing normally. It is therefore
+     * necessary to abort any APIC assist that may have been started
+     * to avoid confusing the state machine.
+     */
+    viridian_abort_apic_assist(vlapic_vcpu(vlapic));
     vlapic_clear_vector(vector, &vlapic->regs->data[APIC_ISR]);
 
     if ( hvm_funcs.handle_eoi )
--
Quoted 1 line

2.11.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Paul Durrant
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 17.01.18 at 13:53, <[email protected]> wrote:
xen/arch/x86/hvm/vlapic.c +0 -0
@@ -422,6 +422,13 @@ void vlapic_EOI_set(struct vlapic *vlapic)
     if ( vector == -1 )

xen/arch/x86/hvm/vlapic.c void vlapic_EOI_set(struct vlapic *vlapic) · line 432

Quoted 10 lines
xen/arch/x86/hvm/vlapic.c +7 -0
         return;
 
+    /*
+     * It is possible that APIC assist has been enabled by the guest but
+     * it has chosen not to use it, by EOIing normally. It is therefore
+     * necessary to abort any APIC assist that may have been started
+     * to avoid confusing the state machine.
+     */
+    viridian_abort_apic_assist(vlapic_vcpu(vlapic));
     vlapic_clear_vector(vector, &vlapic->regs->data[APIC_ISR]);

Fundamentally fine, but is clearing bit 0 in the shared location a valid thing to do in this case? Plus shouldn't you pass in the vector, so the abort would only happen for the very vector that is pending? Iirc with nested interrupts normal EOI might still need to be used by Windows (as only a single vector can be in flight in the assist), so clearing the assist for the wrong vector might actually cause harm.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 16 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 18 January 2018 08:33
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: Re: [PATCH] x86/hvm: more sure APIC assist is aborted if guest EOIs APIC

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.01.18 at 13:53, <[email protected]> wrote:
xen/arch/x86/hvm/vlapic.c +0 -0
@@ -422,6 +422,13 @@ void vlapic_EOI_set(struct vlapic *vlapic)
     if ( vector == -1 )
         return;
 
Quoted 12 lines

xen/arch/x86/hvm/vlapic.c void vlapic_EOI_set(struct vlapic *vlapic) · line 432

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +7 -0
+    /*
+     * It is possible that APIC assist has been enabled by the guest but
+     * it has chosen not to use it, by EOIing normally. It is therefore
+     * necessary to abort any APIC assist that may have been started
+     * to avoid confusing the state machine.
+     */
+    viridian_abort_apic_assist(vlapic_vcpu(vlapic));
     vlapic_clear_vector(vector, &vlapic->regs->data[APIC_ISR]);

Fundamentally fine, but is clearing bit 0 in the shared location a valid thing to do in this case? Plus shouldn't you pass in the vector, so the abort would only happen for the very vector that is pending?

I believe that it is ok.

Quoted 4 lines

Iirc with nested interrupts normal EOI might still need to be used by Windows (as only a single vector can be in flight in the assist), so clearing the assist for the wrong vector might actually cause harm.

If there are nested interrupts then the assist should be aborted anyway. Assist only helps if there is one interrupt pending so should not have been started if something higher priority already in the ISR so, if we see an EOI for a vector that is not the one latched in assist I think this is also a bug in the state machine. I can add verification of that, if you'd like.

Paul

Quoted 1 line

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 32 lines
Quoted 17 lines
Quoted 1 line · On 18.01.18 at 10:06, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 18 January 2018 08:33
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: Re: [PATCH] x86/hvm: more sure APIC assist is aborted if guest EOIs APIC

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.01.18 at 13:53, <[email protected]> wrote:
xen/arch/x86/hvm/vlapic.c +0 -0
@@ -422,6 +422,13 @@ void vlapic_EOI_set(struct vlapic *vlapic)
     if ( vector == -1 )
         return;
 
Quoted 12 lines

xen/arch/x86/hvm/vlapic.c void vlapic_EOI_set(struct vlapic *vlapic) · line 432

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +7 -0
+    /*
+     * It is possible that APIC assist has been enabled by the guest but
+     * it has chosen not to use it, by EOIing normally. It is therefore
+     * necessary to abort any APIC assist that may have been started
+     * to avoid confusing the state machine.
+     */
+    viridian_abort_apic_assist(vlapic_vcpu(vlapic));
     vlapic_clear_vector(vector, &vlapic->regs->data[APIC_ISR]);

Fundamentally fine, but is clearing bit 0 in the shared location a valid thing to do in this case? Plus shouldn't you pass in the vector, so the abort would only happen for the very vector that is pending?

I believe that it is ok.

Quoted 8 lines
Quoted 4 lines

Iirc with nested interrupts normal EOI might still need to be used by Windows (as only a single vector can be in flight in the assist), so clearing the assist for the wrong vector might actually cause harm.

If there are nested interrupts then the assist should be aborted anyway. Assist only helps if there is one interrupt pending so should not have been started if something higher priority already in the ISR

I was thinking about the opposite case - assist active with a higher priority interrupt coming in (and then being EOI-ed). But I guess that's already being taken care of by the existing call to viridian_abort_apic_assist().

Quoted 3 lines

so, if we see an EOI
for a vector that is not the one latched in assist I think this is also a bug in the state machine. I can add verification of that, if you'd like.

At least one-time warnings would seem helpful here, so we have some sort of indication that some assumption is being violated.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 47 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 18 January 2018 09:39
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: RE: [PATCH] x86/hvm: more sure APIC assist is aborted if guest EOIs APIC

Quoted 8 lines
Quoted 8 lines
Quoted 1 line · On 18.01.18 at 10:06, <[email protected]> wrote:

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 18 January 2018 08:33
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: Re: [PATCH] x86/hvm: more sure APIC assist is aborted if guest

EOIs

Quoted 29 lines
Quoted 9 lines

APIC

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 17.01.18 at 13:53, <[email protected]> wrote:
xen/arch/x86/hvm/vlapic.c +0 -0
@@ -422,6 +422,13 @@ void vlapic_EOI_set(struct vlapic *vlapic)
     if ( vector == -1 )
         return;
 
Quoted 12 lines

xen/arch/x86/hvm/vlapic.c void vlapic_EOI_set(struct vlapic *vlapic) · line 432

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +7 -0
+    /*
+     * It is possible that APIC assist has been enabled by the guest but
+     * it has chosen not to use it, by EOIing normally. It is therefore
+     * necessary to abort any APIC assist that may have been started
+     * to avoid confusing the state machine.
+     */
+    viridian_abort_apic_assist(vlapic_vcpu(vlapic));
     vlapic_clear_vector(vector, &vlapic->regs->data[APIC_ISR]);

Fundamentally fine, but is clearing bit 0 in the shared location a valid thing to do in this case? Plus shouldn't you pass in the vector, so the abort would only happen for the very vector that is pending?

I believe that it is ok.

Quoted 4 lines

Iirc with nested interrupts normal EOI might still need to be used by Windows (as only a single vector can be in flight in the assist), so clearing the assist for the wrong vector might actually cause harm.

Quoted 9 lines
Quoted 3 lines

If there are nested interrupts then the assist should be aborted anyway. Assist only helps if there is one interrupt pending so should not have been started if something higher priority already in the ISR

I was thinking about the opposite case - assist active with a higher priority interrupt coming in (and then being EOI-ed). But I guess that's already being taken care of by the existing call to viridian_abort_apic_assist().

Yes, that's right.

Quoted 7 lines
Quoted 3 lines

so, if we see an EOI
for a vector that is not the one latched in assist I think this is also a bug in the state machine. I can add verification of that, if you'd like.

At least one-time warnings would seem helpful here, so we have some sort of indication that some assumption is being violated.

Ok. I'll re-work it so that abort is passed a vector and warns on mismatch.

Paul

Quoted 1 line

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw
Quoted 9 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 18 January 2018 10:17
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: RE: [PATCH] x86/hvm: more sure APIC assist is aborted if guest EOIs APIC

Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 18.01.18 at 10:55, <[email protected]> wrote:

Ok. I'll re-work it so that abort is passed a vector and warns on mismatch.

You mean on the new path only, I suppose? The old path should not trigger warnings (aiui it would always do so).

I think, in the existing abort case, the latched vector should always be the lowest pending bit in the ISR.

Paul

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw
Quoted 20 lines

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Paul Durrant
Sent: 18 January 2018 10:37
To: 'Jan Beulich' <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: Re: [Xen-devel] [PATCH] x86/hvm: more sure APIC assist is aborted if guest EOIs APIC

Quoted 7 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 18 January 2018 10:17
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: RE: [PATCH] x86/hvm: more sure APIC assist is aborted if guest

EOIs

Quoted 2 lines

APIC

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 18.01.18 at 10:55, <[email protected]> wrote:

Ok. I'll re-work it so that abort is passed a vector and warns on mismatch.

You mean on the new path only, I suppose? The old path should not trigger warnings (aiui it would always do so).

I think, in the existing abort case, the latched vector should always be the lowest pending bit in the ISR.

Actually reading through the code and the spec. again, I'm not convinced by the logic even with the EOI fix. It looks to me like vlapic_has_pending_irq() could leave an existing assist in place and return a higher priority irr value. This guest would then service the higher priority interrupt, skip the EOI because the assist bit is set, but then the next call to vlapic_has_pending_irq() would erroneously clear the lower priority vector from the ISR because we originally latched the lower priority vector. As the spec. points out... only the first EOI can avoided, thus I'm not sure trying to track the vector in the viridian code is worth it. It looks like the correct thing to do as test whether an EOI was avoided and then clear the highest priority vector in the ISR (since, if we hadn't squashed it, that would have been the first EOI). We can test when an EOI is avoided because the bit in the shared page would have transitioned from 1 to 0.

Paul

Quoted 10 lines

Paul

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 18.01.18 at 12:21, <[email protected]> wrote:

Actually reading through the code and the spec. again, I'm not convinced by the logic even with the EOI fix. It looks to me like vlapic_has_pending_irq() could leave an existing assist in place and return a higher priority irr value. This guest would then service the higher priority interrupt, skip the EOI because the assist bit is set, but then the next call to vlapic_has_pending_irq() would erroneously clear the lower priority vector from the ISR because we originally latched the lower priority vector. As the spec. points out... only the first EOI can avoided, thus I'm not sure trying to track the vector in the viridian code is worth it. It looks like the correct thing to do as test whether an EOI was avoided and then clear the highest priority vector in the ISR (since, if we hadn't squashed it, that would have been the first EOI). We can test when an EOI is avoided because the bit in the shared page would have transitioned from 1 to 0.

Sounds plausible, looking forward to a patch.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel