vmx/nmi: Do not use self_nmi() in VMEXIT handler
PATCH v3 1/1 vmx/nmi: Do not use self_nmi() in VMEXIT handler
The self_nmi() code cause's an NMI to be triggered by sending an APIC message to the local processor. However, NMIs are blocked by the VMEXIT, until the next iret or VMENTER.
Volume 3 Chapter 27 Section 1 of the Intel SDM states:
An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.
As a result, as soon as the VMENTER happens, an immediate VMEXIT happens as a result of the queued NMI. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.
--
Changes since v2
* Switch from 'int $2' to do_nmi()
* Reworked commit message to more clearly explain the problem
xen/arch/x86/hvm/vmx/vmx.c +8 -1
@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); +else if ( vector == TRAP_nmi && + ( (intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 ) ) ) +/* Must be called before interrupts are enabled to ensure +* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);
@@ -2442,7 +2450,6 @@ void vmx_vmexit_handler(struct cpu_user_ (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
The
self_nmi()code cause's an NMI to be triggered by sending an APIC message to the local processor. However, NMIs are blocked by the VMEXIT, until the next iret or VMENTER.Volume 3 Chapter 27 Section 1 of the Intel SDM states:
An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.
As a result, as soon as the VMENTER happens, an immediate VMEXIT happens as a result of the queued NMI. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.
Signed-off-by Malcolm Crossley <[email protected]>Signed-off-by Andrew Cooper <[email protected]>--
Changes since v2
* Switch from 'int $2' todo_nmi()
* Reworked commit message to more clearly explain the problemdiff -r
2489c2926698-rd7ea938044acxen/arch/x86/hvm/vmx/vmx.cxen/arch/x86/hvm/vmx/vmx.c +0
-0@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;
xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2279
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8
-0if ( vector == TRAP_machine_check )do_machine_check (regs); +else if ( vector == TRAP_nmi && + ( (intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 ) ) ) +/* Must be called before interrupts are enabled to ensure +* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();
But that's only half of it, at least as far as I recall the outcome of the discussion: You want an IRET (or VMRESUME) before possibly going into the scheduler (and hence not back to the current VM). And the same also on the PV exit path from an NMI.
Jan
Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c +0
-1break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);@@ -2442,7 +2450,6 @@ void vmx_vmexit_handler(struct cpu_user_ (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );
Quoted 43 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
The
self_nmi()code cause's an NMI to be triggered by sending an APIC message to the local processor. However, NMIs are blocked by the VMEXIT, until the next iret or VMENTER.Volume 3 Chapter 27 Section 1 of the Intel SDM states:
An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.
As a result, as soon as the VMENTER happens, an immediate VMEXIT happens as a result of the queued NMI. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.
Signed-off-by Malcolm Crossley <[email protected]>Signed-off-by Andrew Cooper <[email protected]>--
Changes since v2
* Switch from 'int $2' todo_nmi()
* Reworked commit message to more clearly explain the problemdiff -r
2489c2926698-rd7ea938044acxen/arch/x86/hvm/vmx/vmx.cxen/arch/x86/hvm/vmx/vmx.c +0
-0@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2279
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8
-0if ( vector == TRAP_machine_check )do_machine_check (regs); +else if ( vector == TRAP_nmi && + ( (intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 ) ) ) +/* Must be called before interrupts are enabled to ensure +* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();But that's only half of it, at least as far as I recall the outcome of the discussion: You want an IRET (or VMRESUME) before possibly going into the scheduler (and hence not back to the current VM). And the same also on the PV exit path from an NMI.
Quoted 1 line
Jan
When I read this codepath, there seemed to be no consideration for re-scheduling. I will double check, but I think the VMRESUME is unconditional if the VMEXIT reason was a real NMI.
~Andrew
Quoted 14 lines
Quoted 12 lines
xen/arch/x86/hvm/vmx/vmx.c +0
-1break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);@@ -2442,7 +2450,6 @@ void vmx_vmexit_handler(struct cpu_user_ (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );
Signature · 2 lines
Andrew Cooper - Dom0 Kernel Engineer, Citrix XenServer T: +44 (0)1223 225 900, http://www.citrix.com
Quoted 46 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 34 lines · Jan Beulich
Quoted 34 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
The
self_nmi()code cause's an NMI to be triggered by sending an APIC message to the local processor. However, NMIs are blocked by the VMEXIT, until the next iret or VMENTER.Volume 3 Chapter 27 Section 1 of the Intel SDM states:
An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.
As a result, as soon as the VMENTER happens, an immediate VMEXIT happens as a result of the queued NMI. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.
Signed-off-by Malcolm Crossley <[email protected]>Signed-off-by Andrew Cooper <[email protected]>--
Changes since v2
* Switch from 'int $2' todo_nmi()
* Reworked commit message to more clearly explain the problemdiff -r
2489c2926698-rd7ea938044acxen/arch/x86/hvm/vmx/vmx.cxen/arch/x86/hvm/vmx/vmx.c +4
-0@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); +else if ( vector == TRAP_nmi && + ( (intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 ) ) ) +/* Must be called before interrupts are enabled to ensure Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2279
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +4
-0+* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();But that's only half of it, at least as far as I recall the outcome of the discussion: You want an IRET (or VMRESUME) before possibly going into the scheduler (and hence not back to the current VM). And the same also on the PV exit path from an NMI.
Quoted 3 lines
When I read this codepath, there seemed to be no consideration for re-scheduling. I will double check, but I think the VMRESUME is unconditional if the VMEXIT reason was a real NMI.
Interesting - I see nothing NMI-related in vmx/entry.S, i.e. it is my understanding that just like in any other case you may end up calling do_softirq on the way out from handling the NMI.
Jan
Quoted 44 lines · Jan Beulich
Quoted 44 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 42 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
The
self_nmi()code cause's an NMI to be triggered by sending an APIC message to the local processor. However, NMIs are blocked by the VMEXIT, until the next iret or VMENTER.Volume 3 Chapter 27 Section 1 of the Intel SDM states:
An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.
As a result, as soon as the VMENTER happens, an immediate VMEXIT happens as a result of the queued NMI. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.
Signed-off-by Malcolm Crossley <[email protected]>Signed-off-by Andrew Cooper <[email protected]>--
Changes since v2
* Switch from 'int $2' todo_nmi()
* Reworked commit message to more clearly explain the problemdiff -r
2489c2926698-rd7ea938044acxen/arch/x86/hvm/vmx/vmx.cxen/arch/x86/hvm/vmx/vmx.c +0
-0@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2279
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8
-0if ( vector == TRAP_machine_check )do_machine_check (regs); +else if ( vector == TRAP_nmi && + ( (intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 ) ) ) +/* Must be called before interrupts are enabled to ensure +* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();But that's only half of it, at least as far as I recall the outcome of the discussion: You want an IRET (or VMRESUME) before possibly going into the scheduler (and hence not back to the current VM). And the same also on the PV exit path from an NMI.
Quoted 9 lines
Quoted 3 lines
When I read this codepath, there seemed to be no consideration for re-scheduling. I will double check, but I think the VMRESUME is unconditional if the VMEXIT reason was a real NMI.
Interesting - I see nothing NMI-related in
vmx/entry.S, i.e. it is my understanding that just like in any other case you may end up callingdo_softirqon the way out from handling the NMI.Jan
Ah yes - vmx_asm_do_vmentry does have the potential to process softirqs, which we will want to do with NMIs enabled. I missed that last time I looked.
So we need to deliberately execute an iretd. int $2 is unusable because it adds an extra dimension to the fake NMI corruption issue if you get the combination int $2->MCE->NMI before the int $2 has correctly saved its exception frame.
A quick solution would be to execute a noop function with run_in_exception_handler(). Alternatively, I can code enable_nmi() or so which does an inline iret to itself. Which of these would you prefer?
Signature · 2 lines
Andrew Cooper - Dom0 Kernel Engineer, Citrix XenServer T: +44 (0)1223 225 900, http://www.citrix.com
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?
I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
Jan
Quoted 10 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
Jan
That would probably be the easiest solution.
I was already going to do the same for the rentrant NMI and MCE handling case (and also the process pending upcalls checking), due to the complexities of fixing the race condition at the end of the handler.
Unfortunately, I don't think I have time to look at this issue immediately, but if it is ok to wait till the beginning of next week, I can roll it into the main dev work for the other NMI/MCE work.
Signature · 2 lines
Andrew Cooper - Dom0 Kernel Engineer, Citrix XenServer T: +44 (0)1223 225 900, http://www.citrix.com
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
Quoted 8 lines
That would probably be the easiest solution.
I was already going to do the same for the rentrant NMI and MCE handling case (and also the process pending upcalls checking), due to the complexities of fixing the race condition at the end of the handler.
Unfortunately, I don't think I have time to look at this issue immediately, but if it is ok to wait till the beginning of next week, I
That's fine of course.
Quoted 1 line
can roll it into the main dev work for the other NMI/MCE work.
Hmm, that would mean it's going to be more difficult to backport. I'd prefer having this in as simple a fashion as possible in the patch here, and then have your other patch(es) build on top of that.
Jan
Quoted 19 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
Quoted 8 lines
That would probably be the easiest solution.
I was already going to do the same for the rentrant NMI and MCE handling case (and also the process pending upcalls checking), due to the complexities of fixing the race condition at the end of the handler.
Unfortunately, I don't think I have time to look at this issue immediately, but if it is ok to wait till the beginning of next week, I
That's fine of course.
Quoted 8 lines
Quoted 1 line
can roll it into the main dev work for the other NMI/MCE work.
Hmm, that would mean it's going to be more difficult to backport. I'd prefer having this in as simple a fashion as possible in the patch here, and then have your other patch(es) build on top of that.
Jan
I will ensure that the fix for this is suitably separate in the patch series, and in the easiest backportable form.
Signature · 2 lines
Andrew Cooper - Dom0 Kernel Engineer, Citrix XenServer T: +44 (0)1223 225 900, http://www.citrix.com
Quoted 13 lines
Quoted 13 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
That would probably be the easiest solution.
Quoted 8 lines
Quoted 6 lines
I was already going to do the same for the rentrant NMI and MCE handling case (and also the process pending upcalls checking), due to the complexities of fixing the race condition at the end of the handler.
Unfortunately, I don't think I have time to look at this issue immediately, but if it is ok to wait till the beginning of next week, I
That's fine of course.
So that was 3 months ago, and we're likely going to need to do further unfixed releases if this won't move forward. Can you estimate whether you'll be able to get back to this?
Jan
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 16 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
That would probably be the easiest solution.
I was already going to do the same for the rentrant NMI and MCE handling case (and also the process pending upcalls checking), due to the complexities of fixing the race condition at the end of the handler.
Quoted 9 lines
Quoted 3 lines
Quoted 2 lines
Unfortunately, I don't think I have time to look at this issue immediately, but if it is ok to wait till the beginning of next week, I
That's fine of course.
So that was 3 months ago, and we're likely going to need to do further unfixed releases if this won't move forward. Can you estimate whether you'll be able to get back to this?
Jan
With the new enable_nmi() function, I can spin a workaround patch quite quickly, and will try to get that done this week. The remainder of the reentrant tangle is still happening very slowly.
~Andrew
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 17 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
That would probably be the easiest solution.
I was already going to do the same for the rentrant NMI and MCE handling case (and also the process pending upcalls checking), due to the complexities of fixing the race condition at the end of the handler.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Unfortunately, I don't think I have time to look at this issue immediately, but if it is ok to wait till the beginning of next week, I
That's fine of course.
So that was 3 months ago, and we're likely going to need to do further unfixed releases if this won't move forward. Can you estimate whether you'll be able to get back to this?
Let's make a step in the right direction before 4.3, at least:
commit d278beed1df2d226911dce92295411018c9bba2f
Author: Tim Deegan <[email protected]> Date: Thu Feb 28 12:42:15 2013 +0000
vmx: handle NMIs before re-enabling interrupts.
Also, switch to calling do_nmi() and explicitly re-enabling NMIs rather than raising a fake NMI and relying on the NMI processing to IRET, since that handling code is likely to change a fair amount in future.
xen/arch/x86/hvm/vmx/vmx.c +8 -1
@@ -2313,6 +2313,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); +if ( vector == TRAP_machine_check + && ((intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 )) ) + { +do_nmi (regs); +enable_nmis (); + }break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);
@@ -2486,7 +2493,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ +/* Already handled above. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );
Andrew Cooper
Acked-byQuoted 29 lines · Tim Deegan
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 16 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
That would probably be the easiest solution.
I was already going to do the same for the rentrant NMI and MCE handling case (and also the process pending upcalls checking), due to the complexities of fixing the race condition at the end of the handler.
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
Unfortunately, I don't think I have time to look at this issue immediately, but if it is ok to wait till the beginning of next week, I
That's fine of course.
So that was 3 months ago, and we're likely going to need to do further unfixed releases if this won't move forward. Can you estimate whether you'll be able to get back to this?
Let's make a step in the right direction before 4.3, at least:
commit
d278beed1df2d226911dce92295411018c9bba2fAuthor: Tim Deegan <[email protected]> Date: Thu Feb 28 12:42:15 2013 +0000
Quoted 8 lines
vmx: handle NMIs before re-enabling interrupts.
Also, switch to calling
do_nmi()and explicitly re-enabling NMIs rather than raising a fake NMI and relying on the NMI processing to IRET, since that handling code is likely to change a fair amount in future.Signed-off-by Tim Deegan <[email protected]>
As this is functionally equivalent to the patch I was just testing.
Quoted 28 lines
xen/arch/x86/hvm/vmx/vmx.c +8
-1@@ -2313,6 +2313,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); +if ( vector == TRAP_machine_check + && ((intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 )) ) + { +do_nmi (regs); +enable_nmis (); + }break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);@@ -2486,7 +2493,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ +/* Already handled above. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );
Jan Beulich
Acked-byQuoted 11 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
commit
d278beed1df2d226911dce92295411018c9bba2fAuthor: Tim Deegan <[email protected]> Date: Thu Feb 28 12:42:15 2013 +0000vmx: handle NMIs before re-enabling interrupts.
Also, switch to calling
do_nmi()and explicitly re-enabling NMIs rather than raising a fake NMI and relying on the NMI processing to IRET, since that handling code is likely to change a fair amount in future.
Isn't that a gross understatement, considering that you or Malcolm had found that the use of self_nmi() here was actively broken?
Quoted 1 line
Signed-off-by Tim Deegan <[email protected]>
(realizing that dealing with the PV side of the issue will be left to me in the end)
Quoted 15 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
commit
d278beed1df2d226911dce92295411018c9bba2fAuthor: Tim Deegan <[email protected]> Date: Thu Feb 28 12:42:15 2013 +0000vmx: handle NMIs before re-enabling interrupts.
Also, switch to calling
do_nmi()and explicitly re-enabling NMIs rather than raising a fake NMI and relying on the NMI processing to IRET, since that handling code is likely to change a fair amount in future.Isn't that a gross understatement, considering that you or Malcolm had found that the use of
self_nmi()here was actively broken?
Quoted 6 lines
Quoted 1 line
Signed-off-by Tim Deegan <[email protected]>Acked-by Jan Beulich <[email protected]>(realizing that dealing with the PV side of the issue will be left to me in the end)
For PV, would you be happy with something like this, or do you want to avoid the extra IRET in cases where we would be returning with IRET anyway?
xen/arch/x86/hvm/vmx/vmx.c +0 -3
@@ -2316,10 +2316,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs)if ( vector == TRAP_nmi && ((intr_info &INTR_INFO_INTR_TYPE_MASK ) == (X86_EVENTTYPE_NMI <<8 )) ) - {do_nmi (regs); -enable_nmis (); - }break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);
xen/arch/x86/traps.c +4 -1
@@ -3211,7 +3211,7 @@ void do_nmi(struct cpu_user_regs *regs) ++nmi_count (cpu);if (nmi_callback (regs, cpu) ) -return ; + goto out;if ( nmi_watchdog )nmi_watchdog_tick (regs);
@@ -3227,6 +3227,9 @@ void do_nmi(struct cpu_user_regs *regs)if ( !(reason &0xc0 ) && !nmi_watchdog )unknown_nmi_error (regs, reason); } + +out: +enable_nmis (); }void set_nmi_callback (nmi_callback_t callback)
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 2 lines · Jan Beulich
(realizing that dealing with the PV side of the issue will be left to me in the end)
For PV, would you be happy with something like this, or do you want to avoid the extra IRET in cases where we would be returning with IRET anyway?
No, and not so much because of the redundant IRET, but because ...
Quoted 12 lines
xen/arch/x86/traps.c +1
-1@@ -3211,7 +3211,7 @@ void do_nmi(struct cpu_user_regs *regs) ++nmi_count (cpu);if (nmi_callback (regs, cpu) ) -return ; + goto out;if ( nmi_watchdog )nmi_watchdog_tick (regs);
xen/arch/x86/traps.c void do_nmi(struct cpu_user_regs *regs) · line 3232
Quoted 6 lines
xen/arch/x86/traps.c +3
-0@@ -3227,6 +3227,9 @@ void do_nmi(struct cpu_user_regs *regs)if ( !(reason &0xc0 ) && !nmi_watchdog )unknown_nmi_error (regs, reason); } + +out: +enable_nmis ();
... this must not be done when on the NMI stack (i.e. when the NMI was raised while in hypervisor context). Checking for this here would be strait forward, but I was really considering to do all of this in the assembly exit path, and I was still undecided whether we shouldn't follow Linux in skipping softirq processing (and hence scheduling) on the way out from an NMI (I don't think we'd need to do the same for MCE).
Jan
Quoted 3 lines
xen/arch/x86/traps.c +0
-0}void set_nmi_callback (nmi_callback_t callback)
Quoted 36 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 2 lines · Jan Beulich
(realizing that dealing with the PV side of the issue will be left to me in the end)
For PV, would you be happy with something like this, or do you want to avoid the extra IRET in cases where we would be returning with IRET anyway?
No, and not so much because of the redundant IRET, but because ...
Quoted 12 lines
xen/arch/x86/traps.c +1
-1@@ -3211,7 +3211,7 @@ void do_nmi(struct cpu_user_regs *regs) ++nmi_count (cpu);if (nmi_callback (regs, cpu) ) -return ; + goto out;if ( nmi_watchdog )nmi_watchdog_tick (regs);xen/arch/x86/traps.c void do_nmi(struct cpu_user_regs *regs) · line 3232
Quoted 6 lines
xen/arch/x86/traps.c +3
-0@@ -3227,6 +3227,9 @@ void do_nmi(struct cpu_user_regs *regs)if ( !(reason &0xc0 ) && !nmi_watchdog )unknown_nmi_error (regs, reason); } + +out: +enable_nmis ();... this must not be done when on the NMI stack (i.e. when the NMI was raised while in hypervisor context). Checking for this here would be strait forward, but I was really considering to do all of this in the assembly exit path, and I was still undecided whether we shouldn't follow Linux in skipping softirq processing (and hence scheduling) on the way out from an NMI (I don't think we'd need to do the same for MCE).
Quoted 1 line
Jan
It is furthermore pointless. If we interrupted a guest with the NMI, we would have moved onto the main stack. We would only be on the NMI stack at this point if we interrupted Xen with the NMI, at which point we will be iret'ing back anyway.
~Andrew
Quoted 6 lines
Quoted 4 lines
xen/arch/x86/traps.c +0
-0}void set_nmi_callback (nmi_callback_t callback)
Quoted 5 lines · Jan Beulich
xen/arch/x86/traps.c void do_nmi(struct cpu_user_regs *regs) · line 3232
Quoted 2 lines
xen/arch/x86/traps.c +2
-0+out: +enable_nmis ();... this must not be done when on the NMI stack (i.e. when the NMI was raised while in hypervisor context).
Right; I had forgotten that we didn't switch stacks in that case.
Quoted 6 lines
Checking for this
here would be strait forward, but I was really considering to do all of this in the assembly exit path, and I was still undecided whether we shouldn't follow Linux in skipping softirq processing (and hence scheduling) on the way out from an NMI (I don't think we'd need to do the same for MCE).
That sounds sensible, if it simplifies the logic (i.e. if it avoids needing an extra IRET just to re-enable NMIs when we might as well IRET to the guest). Or is there any other reason to avoid it?
Tim.
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
Quoted 6 lines · Jan Beulich
Checking for this
here would be strait forward, but I was really considering to do all of this in the assembly exit path, and I was still undecided whether we shouldn't follow Linux in skipping softirq processing (and hence scheduling) on the way out from an NMI (I don't think we'd need to do the same for MCE).
Quoted 3 lines
That sounds sensible, if it simplifies the logic (i.e. if it avoids needing an extra IRET just to re-enable NMIs when we might as well IRET to the guest). Or is there any other reason to avoid it?
No, I don't think so. We only need to make sure we don't execute one as long as we're on the NMI stack.
Jan
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Jan Beulich
... this must not be done when on the NMI stack (i.e. when the NMI was raised while in hypervisor context). Checking for this here would be strait forward, but I was really considering to do all of this in the assembly exit path, and I was still undecided whether we shouldn't follow Linux in skipping softirq processing (and hence scheduling) on the way out from an NMI (I don't think we'd need to do the same for MCE).
Like this:
x86: skip processing events on the NMI exit path
Otherwise, we may end up in the scheduler, keeping NMIs masked for a possibly unbounded time (until whenever the next IRET gets executed).
Of course it's open for discussion whether to always use the strait exit path from handle_ist_exception.
xen/arch/x86/x86_64/compat/entry.S +1 -1
@@ -171,7 +171,7 @@ compat_bad_hypercall: jmp compat_test_all_events /* %rbx: struct vcpu, interrupts disabled */ -compat_restore_all_guest: +ENTRY(compat_restore_all_guest) ASSERT_INTERRUPTS_DISABLED RESTORE_ALL adj=8 compat=1 .Lft0: iretq
xen/arch/x86/x86_64/entry.S +13 -5
@@ -635,6 +635,9 @@ ENTRY(early_page_fault) jmp restore_all_xen .popsection +ENTRY(nmi) + pushq $0 + movl $TRAP_nmi,4(%rsp) handle_ist_exception: SAVE_ALL testb $3,UREGS_cs(%rsp)
@@ -649,12 +652,17 @@ handle_ist_exception: movzbl UREGS_entry_vector(%rsp),%eax leaq exception_table(%rip),%rdx callq *(%rdx,%rax,8) - jmp ret_from_intr + cmpb $TRAP_nmi,UREGS_entry_vector(%rsp) + jne ret_from_intr -ENTRY(nmi) - pushq $0 - movl $TRAP_nmi,4(%rsp) - jmp handle_ist_exception + /* We want to get strait to the IRET in the NMI exit path. */ + testb $3,UREGS_cs(%rsp) + GET_CURRENT(%rbx) + jz restore_all_xen + movq VCPU_domain(%rbx),%rax + testb $1,DOMAIN_is_32bit_pv(%rax) + jz restore_all_guest + jmp compat_restore_all_guest ENTRY(nmi_crash) pushq $0
Andrew Cooper
Acked-byQuoted 12 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Jan Beulich
... this must not be done when on the NMI stack (i.e. when the NMI was raised while in hypervisor context). Checking for this here would be strait forward, but I was really considering to do all of this in the assembly exit path, and I was still undecided whether we shouldn't follow Linux in skipping softirq processing (and hence scheduling) on the way out from an NMI (I don't think we'd need to do the same for MCE).
Like this:
x86: skip processing events on the NMI exit path
Quoted 7 lines
Otherwise, we may end up in the scheduler, keeping NMIs masked for a possibly unbounded time (until whenever the next IRET gets executed).
Of course it's open for discussion whether to always use the strait exit path from
handle_ist_exception.Signed-off-by Jan Beulich <[email protected]>
The logic looks good to me. As for the other IST exceptions, we will never return from a double fault, and will rarely return from an MCE, so I would say it doesn't really matter at the moment.
~Andrew
Quoted 50 lines
xen/arch/x86/x86_64/compat/entry.S +1
-1@@ -171,7 +171,7 @@ compat_bad_hypercall: jmp compat_test_all_events /* %rbx: struct vcpu, interrupts disabled */ -compat_restore_all_guest: +ENTRY(compat_restore_all_guest) ASSERT_INTERRUPTS_DISABLED RESTORE_ALL adj=8 compat=1 .Lft0: iretqxen/arch/x86/x86_64/entry.S +13
-5@@ -635,6 +635,9 @@ ENTRY(early_page_fault) jmp restore_all_xen .popsection +ENTRY(nmi) + pushq $0 + movl $TRAP_nmi,4(%rsp) handle_ist_exception: SAVE_ALL testb $3,UREGS_cs(%rsp)@@ -649,12 +652,17 @@ handle_ist_exception: movzbl UREGS_entry_vector(%rsp),%eax leaq exception_table(%rip),%rdx callq *(%rdx,%rax,8) - jmp ret_from_intr + cmpb $TRAP_nmi,UREGS_entry_vector(%rsp) + jne ret_from_intr -ENTRY(nmi) - pushq $0 - movl $TRAP_nmi,4(%rsp) - jmp handle_ist_exception + /* We want to get strait to the IRET in the NMI exit path. */ + testb $3,UREGS_cs(%rsp) + GET_CURRENT(%rbx) + jz restore_all_xen + movq VCPU_domain(%rbx),%rax + testb $1,DOMAIN_is_32bit_pv(%rax) + jz restore_all_guest + jmp compat_restore_all_guest ENTRY(nmi_crash) pushq $0
Tim Deegan
Reviewed-byQuoted 15 lines · Jan Beulich
xen/arch/x86/x86_64/entry.S +3
-0@@ -635,6 +635,9 @@ ENTRY(early_page_fault) jmp restore_all_xen .popsection +ENTRY(nmi) + pushq $0 + movl $TRAP_nmi,4(%rsp) handle_ist_exception: SAVE_ALL testb $3,UREGS_cs(%rsp)@@ -649,12 +652,17 @@ handle_ist_exception: movzbl UREGS_entry_vector(%rsp),%eax leaq exception_table(%rip),%rdx
xen/arch/x86/x86_64/entry.S handle_ist_exception: · line 658
Quoted 10 lines
xen/arch/x86/x86_64/entry.S +3
-5callq *(%rdx,%rax,8) - jmp ret_from_intr + cmpb $TRAP_nmi,UREGS_entry_vector(%rsp) + jne ret_from_intr -ENTRY(nmi) - pushq $0 - movl $TRAP_nmi,4(%rsp) - jmp handle_ist_exception + /* We want to get strait to the IRET in the NMI exit path. */
Language nit: 'straight', meaning linear or directly, is suitable here. 'strait' means narrow or constrained (as in strait-jacket).
xen/arch/x86/x86_64/entry.S handle_ist_exception: · line 661
Quoted 3 lines
xen/arch/x86/x86_64/entry.S +3
-0+ testb $3,UREGS_cs(%rsp) + GET_CURRENT(%rbx) + jz restore_all_xen
GET_CURRENT() contains an addq, which updates ZF. Swap it with the testb?
Otherwise, looks good to me.
Quoted 10 lines
xen/arch/x86/x86_64/entry.S +4
-0+ movq VCPU_domain(%rbx),%rax + testb $1,DOMAIN_is_32bit_pv(%rax) + jz restore_all_guest + jmp compat_restore_all_guest ENTRY(nmi_crash) pushq $0
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
xen/arch/x86/x86_64/entry.S handle_ist_exception: · line 661
Quoted 3 lines · Jan Beulich
xen/arch/x86/x86_64/entry.S +3
-0+ testb $3,UREGS_cs(%rsp) + GET_CURRENT(%rbx) + jz restore_all_xen
GET_CURRENT()contains an addq, which updates ZF. Swap it with the testb?
I'm glad you spotted that - thanks. I moved the GET_CURRENT() down though instead of up.
Jan
Quoted 9 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Jan Beulich
... this must not be done when on the NMI stack (i.e. when the NMI was raised while in hypervisor context). Checking for this here would be strait forward, but I was really considering to do all of this in the assembly exit path, and I was still undecided whether we shouldn't follow Linux in skipping softirq processing (and hence scheduling) on the way out from an NMI (I don't think we'd need to do the same for MCE).
Quoted 6 lines
Like this:
x86: skip processing events on the NMI exit path
Otherwise, we may end up in the scheduler, keeping NMIs masked for a possibly unbounded time (until whenever the next IRET gets executed).
Is this alternative that we might not process events for an unbounded time? No, I guess not -- either we would interrupt the notifying IPI and we will be IRETing into that IPI's handler, or the notifying IPI is delayed until the NMI handler's IRET.
What about if the NMI handler itself raises an event (eg softirq)? Perhaps there are no very essential ones of those?
Quoted 2 lines
Of course it's open for discussion whether to always use the strait exit path from
handle_ist_exception.
s/strait/straight (and below in the code comment that you add).
Quoted 56 lines
Signed-off-by Jan Beulich <[email protected]>xen/arch/x86/x86_64/compat/entry.S +1
-1@@ -171,7 +171,7 @@ compat_bad_hypercall: jmp compat_test_all_events /* %rbx: struct vcpu, interrupts disabled */ -compat_restore_all_guest: +ENTRY(compat_restore_all_guest) ASSERT_INTERRUPTS_DISABLED RESTORE_ALL adj=8 compat=1 .Lft0: iretqxen/arch/x86/x86_64/entry.S +13
-5@@ -635,6 +635,9 @@ ENTRY(early_page_fault) jmp restore_all_xen .popsection +ENTRY(nmi) + pushq $0 + movl $TRAP_nmi,4(%rsp) handle_ist_exception: SAVE_ALL testb $3,UREGS_cs(%rsp)@@ -649,12 +652,17 @@ handle_ist_exception: movzbl UREGS_entry_vector(%rsp),%eax leaq exception_table(%rip),%rdx callq *(%rdx,%rax,8) - jmp ret_from_intr + cmpb $TRAP_nmi,UREGS_entry_vector(%rsp) + jne ret_from_intr -ENTRY(nmi) - pushq $0 - movl $TRAP_nmi,4(%rsp) - jmp handle_ist_exception + /* We want to get strait to the IRET in the NMI exit path. */ + testb $3,UREGS_cs(%rsp) + GET_CURRENT(%rbx) + jz restore_all_xen + movq VCPU_domain(%rbx),%rax + testb $1,DOMAIN_is_32bit_pv(%rax) + jz restore_all_guest + jmp compat_restore_all_guest ENTRY(nmi_crash) pushq $0_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Keir Fraser
Quoted 9 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Jan Beulich
... this must not be done when on the NMI stack (i.e. when the NMI was raised while in hypervisor context). Checking for this here would be strait forward, but I was really considering to do all of this in the assembly exit path, and I was still undecided whether we shouldn't follow Linux in skipping softirq processing (and hence scheduling) on the way out from an NMI (I don't think we'd need to do the same for MCE).
Quoted 6 lines
Like this:
x86: skip processing events on the NMI exit path
Otherwise, we may end up in the scheduler, keeping NMIs masked for a possibly unbounded time (until whenever the next IRET gets executed).
Quoted 7 lines
Is this alternative that we might not process events for an unbounded time? No, I guess not -- either we would interrupt the notifying IPI and we will be IRETing into that IPI's handler, or the notifying IPI is delayed until the NMI handler's IRET.
What about if the NMI handler itself raises an event (eg softirq)? Perhaps there are no very essential ones of those?
We do raise PCI_SERR_SOFTIRQ, and the possibly injected NMI (to Dom0) might get slightly deferred too. The latter seems of little concern (Dom0 will get the event eventually). For the former, we might want to explicitly send a self-IPI with EVENT_CHECK_VECTOR following the raise_softirq()?
Jan
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Is this alternative that we might not process events for an unbounded time? No, I guess not -- either we would interrupt the notifying IPI and we will be IRETing into that IPI's handler, or the notifying IPI is delayed until the NMI handler's IRET.
Quoted 8 lines
Quoted 2 lines
What about if the NMI handler itself raises an event (eg softirq)? Perhaps there are no very essential ones of those?
We do raise
PCI_SERR_SOFTIRQ, and the possibly injected NMI (to Dom0) might get slightly deferred too. The latter seems of little concern (Dom0 will get the event eventually). For the former, we might want to explicitly send a self-IPI withEVENT_CHECK_VECTORfollowing theraise_softirq()?
Or perhaps self-IPI on the NMI exit path if softirq_pending is non-zero? Conservative but more generic.
-- Keir
v2 cover x86: defer processing events on the NMI exit path
1: defer processing events on the NMI exit path
2: don't rely on __softirq_pending to be the first field in irq_cpustat_t
PATCH v2 1/2 x86: defer processing events on the NMI exit path
Otherwise, we may end up in the scheduler, keeping NMIs masked for a possibly unbounded period of time (until whenever the next IRET gets executed). Enforce timely event processing by sending a self IPI.
Of course it's open for discussion whether to always use the straight exit path from handle_ist_exception.
---
v2: Send IPI to ourselves to enforce event processing. Dropped all ACKs.
xen/arch/x86/x86_64/compat/entry.S +1 -1
@@ -171,7 +171,7 @@ compat_bad_hypercall: jmp compat_test_all_events /* %rbx: struct vcpu, interrupts disabled */ -compat_restore_all_guest: +ENTRY(compat_restore_all_guest) ASSERT_INTERRUPTS_DISABLED RESTORE_ALL adj=8 compat=1 .Lft0: iretq
xen/arch/x86/x86_64/entry.S +22 -5
@@ -11,6 +11,7 @@ #include <asm/apicdef.h> #include <asm/page.h> #include <public/xen.h> +#include <irq_vectors.h> ALIGN /* %rbx: struct vcpu */
@@ -635,6 +636,9 @@ ENTRY(early_page_fault) jmp restore_all_xen .popsection +ENTRY(nmi) + pushq $0 + movl $TRAP_nmi,4(%rsp) handle_ist_exception: SAVE_ALL testb $3,UREGS_cs(%rsp)
@@ -649,12 +653,25 @@ handle_ist_exception: movzbl UREGS_entry_vector(%rsp),%eax leaq exception_table(%rip),%rdx callq *(%rdx,%rax,8) - jmp ret_from_intr + cmpb $TRAP_nmi,UREGS_entry_vector(%rsp) + jne ret_from_intr -ENTRY(nmi) - pushq $0 - movl $TRAP_nmi,4(%rsp) - jmp handle_ist_exception + /* We want to get straight to the IRET on the NMI exit path. */ + testb $3,UREGS_cs(%rsp) + jz restore_all_xen + GET_CURRENT(%rbx) + /* Send an IPI to ourselves to cover for the lack of event checking. */ + movl VCPU_processor(%rbx),%eax + shll $IRQSTAT_shift,%eax + leaq irq_stat(%rip),%rcx + cmpl $0,(%rcx,%rax,1) + je 1f + movl $EVENT_CHECK_VECTOR,%edi + call send_IPI_self +1: movq VCPU_domain(%rbx),%rax + cmpb $0,DOMAIN_is_32bit_pv(%rax) + je restore_all_guest + jmp compat_restore_all_guest ENTRY(nmi_crash) pushq $0
x86: defer processing events on the NMI exit path
Otherwise, we may end up in the scheduler, keeping NMIs masked for a possibly unbounded period of time (until whenever the next IRET gets executed). Enforce timely event processing by sending a self IPI.
Of course it's open for discussion whether to always use the straight exit path from handle_ist_exception.
---
v2: Send IPI to ourselves to enforce event processing. Dropped all ACKs.
xen/arch/x86/x86_64/compat/entry.S +1 -1
@@ -171,7 +171,7 @@ compat_bad_hypercall: jmp compat_test_all_events /* %rbx: struct vcpu, interrupts disabled */ -compat_restore_all_guest: +ENTRY(compat_restore_all_guest) ASSERT_INTERRUPTS_DISABLED RESTORE_ALL adj=8 compat=1 .Lft0: iretq
xen/arch/x86/x86_64/entry.S +22 -5
@@ -11,6 +11,7 @@ #include <asm/apicdef.h> #include <asm/page.h> #include <public/xen.h> +#include <irq_vectors.h> ALIGN /* %rbx: struct vcpu */
@@ -635,6 +636,9 @@ ENTRY(early_page_fault) jmp restore_all_xen .popsection +ENTRY(nmi) + pushq $0 + movl $TRAP_nmi,4(%rsp) handle_ist_exception: SAVE_ALL testb $3,UREGS_cs(%rsp)
@@ -649,12 +653,25 @@ handle_ist_exception: movzbl UREGS_entry_vector(%rsp),%eax leaq exception_table(%rip),%rdx callq *(%rdx,%rax,8) - jmp ret_from_intr + cmpb $TRAP_nmi,UREGS_entry_vector(%rsp) + jne ret_from_intr -ENTRY(nmi) - pushq $0 - movl $TRAP_nmi,4(%rsp) - jmp handle_ist_exception + /* We want to get straight to the IRET on the NMI exit path. */ + testb $3,UREGS_cs(%rsp) + jz restore_all_xen + GET_CURRENT(%rbx) + /* Send an IPI to ourselves to cover for the lack of event checking. */ + movl VCPU_processor(%rbx),%eax + shll $IRQSTAT_shift,%eax + leaq irq_stat(%rip),%rcx + cmpl $0,(%rcx,%rax,1) + je 1f + movl $EVENT_CHECK_VECTOR,%edi + call send_IPI_self +1: movq VCPU_domain(%rbx),%rax + cmpb $0,DOMAIN_is_32bit_pv(%rax) + je restore_all_guest + jmp compat_restore_all_guest ENTRY(nmi_crash) pushq $0
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v2 1/2] x86: defer processing events on the NMI exit path
Quoted 53 lines · Jan Beulich
Otherwise, we may end up in the scheduler, keeping NMIs masked for a possibly unbounded period of time (until whenever the next IRET gets executed). Enforce timely event processing by sending a self IPI.
Of course it's open for discussion whether to always use the straight exit path from
handle_ist_exception.Signed-off-by Jan Beulich <[email protected]>---
v2: Send IPI to ourselves to enforce event processing. Dropped all ACKs.xen/arch/x86/x86_64/compat/entry.S +1
-1@@ -171,7 +171,7 @@ compat_bad_hypercall: jmp compat_test_all_events /* %rbx: struct vcpu, interrupts disabled */ -compat_restore_all_guest: +ENTRY(compat_restore_all_guest) ASSERT_INTERRUPTS_DISABLED RESTORE_ALL adj=8 compat=1 .Lft0: iretqxen/arch/x86/x86_64/entry.S +6
-4@@ -11,6 +11,7 @@ #include <asm/apicdef.h> #include <asm/page.h> #include <public/xen.h> +#include <irq_vectors.h> ALIGN /* %rbx: struct vcpu */@@ -635,6 +636,9 @@ ENTRY(early_page_fault) jmp restore_all_xen .popsection +ENTRY(nmi) + pushq $0 + movl $TRAP_nmi,4(%rsp) handle_ist_exception: SAVE_ALL testb $3,UREGS_cs(%rsp)@@ -649,12 +653,25 @@ handle_ist_exception: movzbl UREGS_entry_vector(%rsp),%eax leaq exception_table(%rip),%rdx callq *(%rdx,%rax,8) - jmp ret_from_intr + cmpb $TRAP_nmi,UREGS_entry_vector(%rsp) + jne ret_from_intr -ENTRY(nmi) - pushq $0 - movl $TRAP_nmi,4(%rsp)
xen/arch/x86/x86_64/entry.S handle_ist_exception: · line 667
Quoted 10 lines
xen/arch/x86/x86_64/entry.S +9
-1- jmp handle_ist_exception + /* We want to get straight to the IRET on the NMI exit path. */ + testb $3,UREGS_cs(%rsp) + jz restore_all_xen + GET_CURRENT(%rbx) + /* Send an IPI to ourselves to cover for the lack of event checking. */ + movl VCPU_processor(%rbx),%eax + shll $IRQSTAT_shift,%eax + leaq irq_stat(%rip),%rcx + cmpl $0,(%rcx,%rax,1)
__softirq_pending is an unsigned long. Would it not be prudent to use cmpq to save obscure bugs if the implementation changes, or are we sufficiently sure that this wont happen?
~Andrew
Quoted 13 lines
xen/arch/x86/x86_64/entry.S +7
-0+ je 1f + movl $EVENT_CHECK_VECTOR,%edi + call send_IPI_self +1: movq VCPU_domain(%rbx),%rax + cmpb $0,DOMAIN_is_32bit_pv(%rax) + je restore_all_guest + jmp compat_restore_all_guest ENTRY(nmi_crash) pushq $0
Re: [PATCH v2 1/2] x86: defer processing events on the NMI exit path
Quoted 62 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 49 lines · Jan Beulich
Otherwise, we may end up in the scheduler, keeping NMIs masked for a possibly unbounded period of time (until whenever the next IRET gets executed). Enforce timely event processing by sending a self IPI.
Of course it's open for discussion whether to always use the straight exit path from
handle_ist_exception.Signed-off-by Jan Beulich <[email protected]>---
v2: Send IPI to ourselves to enforce event processing. Dropped all ACKs.xen/arch/x86/x86_64/compat/entry.S +1
-1@@ -171,7 +171,7 @@ compat_bad_hypercall: jmp compat_test_all_events /* %rbx: struct vcpu, interrupts disabled */ -compat_restore_all_guest: +ENTRY(compat_restore_all_guest) ASSERT_INTERRUPTS_DISABLED RESTORE_ALL adj=8 compat=1 .Lft0: iretqxen/arch/x86/x86_64/entry.S +6
-1@@ -11,6 +11,7 @@ #include <asm/apicdef.h> #include <asm/page.h> #include <public/xen.h> +#include <irq_vectors.h> ALIGN /* %rbx: struct vcpu */@@ -635,6 +636,9 @@ ENTRY(early_page_fault) jmp restore_all_xen .popsection +ENTRY(nmi) + pushq $0 + movl $TRAP_nmi,4(%rsp) handle_ist_exception: SAVE_ALL testb $3,UREGS_cs(%rsp)@@ -649,12 +653,25 @@ handle_ist_exception: movzbl UREGS_entry_vector(%rsp),%eax leaq exception_table(%rip),%rdx callq *(%rdx,%rax,8) - jmp ret_from_intr + cmpb $TRAP_nmi,UREGS_entry_vector(%rsp) + jne ret_from_intrxen/arch/x86/x86_64/entry.S handle_ist_exception: · line 663
Quoted 10 lines
xen/arch/x86/x86_64/entry.S +5
-4-ENTRY(nmi) - pushq $0 - movl $TRAP_nmi,4(%rsp) - jmp handle_ist_exception + /* We want to get straight to the IRET on the NMI exit path. */ + testb $3,UREGS_cs(%rsp) + jz restore_all_xen + GET_CURRENT(%rbx) + /* Send an IPI to ourselves to cover for the lack of eventchecking. */
Quoted 8 lines
xen/arch/x86/x86_64/entry.S handle_ist_exception: · line 667
Quoted 4 lines
xen/arch/x86/x86_64/entry.S +4
-0+ movl VCPU_processor(%rbx),%eax + shll $IRQSTAT_shift,%eax + leaq irq_stat(%rip),%rcx + cmpl $0,(%rcx,%rax,1)
__softirq_pendingis an unsigned long. Would it not be prudent to use cmpq to save obscure bugs if the implementation changes, or are we sufficiently sure that this wont happen?
All other existing instances of similar assembly code use testl or cmpl, and in fact I merely copied some other instance.
As we're not getting close to 32, I think we might rather want to adjust the __softirq_pending type. Keir?
Jan
Re: [PATCH v2 1/2] x86: defer processing events on the NMI exit path
Quoted 9 lines · Jan Beulich
Quoted 3 lines
__softirq_pendingis an unsigned long. Would it not be prudent to use cmpq to save obscure bugs if the implementation changes, or are we sufficiently sure that this wont happen?All other existing instances of similar assembly code use testl or cmpl, and in fact I merely copied some other instance.
As we're not getting close to 32, I think we might rather want to adjust the
__softirq_pendingtype. Keir?
Yup, I don't see any reason why it couldn't be a uint32_t.
-- Keir
Re: [PATCH v2 1/2] x86: defer processing events on the NMI exit path
Quoted 5 lines · Keir Fraser
Quoted 3 lines · Jan Beulich
Quoted 3 lines
__softirq_pendingis an unsigned long. Would it not be prudent to use cmpq to save obscure bugs if the implementation changes, or are we sufficiently sure that this wont happen?
Quoted 10 lines
Quoted 5 lines
All other existing instances of similar assembly code use testl or cmpl, and in fact I merely copied some other instance.
As we're not getting close to 32, I think we might rather want to adjust the
__softirq_pendingtype. Keir?Yup, I don't see any reason why it couldn't be a
uint32_t.-- Keir
Further to that, is there any reason that it cant be per-cpu, to save having information like this moving around pcpus in a hot cache line?
~Andrew
Re: [PATCH v2 1/2] x86: defer processing events on the NMI exit path
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 8 lines · Keir Fraser
Quoted 6 lines · Jan Beulich
Quoted 3 lines
__softirq_pendingis an unsigned long. Would it not be prudent to use cmpq to save obscure bugs if the implementation changes, or are we sufficiently sure that this wont happen?All other existing instances of similar assembly code use testl or cmpl, and in fact I merely copied some other instance.
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines
As we're not getting close to 32, I think we might rather want to adjust the
__softirq_pendingtype. Keir?Yup, I don't see any reason why it couldn't be a
uint32_t.Further to that, is there any reason that it cant be per-cpu, to save having information like this moving around pcpus in a hot cache line?
The structure definition has __cacheline_aligned, there shouldn't be much moving around (as long as our cache line size definition is still suitable for modern CPUs' L1 caches at least).
Jan
PATCH v2 2/2 x86: don't rely on __softirq_pending to be the first field in irq_cpustat_t
In reply to Jan BeulichThis is even more so as the field doesn't have a comment to that effect in the structure definition.
Once modifying the respective assembly code, also convert the IRQSTAT_shift users to do a 32-bit shift only (as we won't support 48M CPUs any time soon) and use "cmpl" instead of "testl" when checking the field (both reducing code size).
xen/arch/x86/hvm/svm/entry.S +4 -4
@@ -41,10 +41,10 @@ ENTRY(svm_asm_do_resume) CLGI mov VCPU_processor(%rbx),%eax - shl $IRQSTAT_shift,%rax - lea irq_stat(%rip),%rdx - testl $~0,(%rdx,%rax,1) - jnz .Lsvm_process_softirqs + shl $IRQSTAT_shift,%eax + lea irq_stat+IRQSTAT_softirq_pending(%rip),%rdx + cmpl $0,(%rdx,%rax,1) + jne .Lsvm_process_softirqs testb $0, VCPU_nsvm_hap_enabled(%rbx) UNLIKELY_START(nz, nsvm_hap)
xen/arch/x86/hvm/vmx/entry.S +2 -2
@@ -97,8 +97,8 @@ vmx_asm_do_vmentry: cli mov VCPU_processor(%rbx),%eax - shl $IRQSTAT_shift,%rax - lea irq_stat(%rip),%rdx + shl $IRQSTAT_shift,%eax + lea irq_stat+IRQSTAT_softirq_pending(%rip),%rdx cmpl $0,(%rdx,%rax,1) jnz .Lvmx_process_softirqs
xen/arch/x86/x86_64/asm-offsets.c +1 -0
@@ -162,6 +162,7 @@ void __dummy__(void)#endif DEFINE (IRQSTAT_shift,LOG_2 (sizeof (irq_cpustat_t))); +OFFSET (IRQSTAT_softirq_pending, irq_cpustat_t, __softirq_pending);BLANK (); OFFSET(CPUINFO86_ext_features, struct cpuinfo_x86, x86_capability[1]);
xen/arch/x86/x86_64/compat/entry.S +4 -4
@@ -96,10 +96,10 @@ ENTRY(compat_test_all_events) cli # tests must not race interrupts /*compat_test_softirqs:*/ movl VCPU_processor(%rbx),%eax - shlq $IRQSTAT_shift,%rax - leaq irq_stat(%rip),%rcx - testl $~0,(%rcx,%rax,1) - jnz compat_process_softirqs + shll $IRQSTAT_shift,%eax + leaq irq_stat+IRQSTAT_softirq_pending(%rip),%rcx + cmpl $0,(%rcx,%rax,1) + jne compat_process_softirqs testb $1,VCPU_mce_pending(%rbx) jnz compat_process_mce .Lcompat_test_guest_nmi:
xen/arch/x86/x86_64/entry.S +3 -3
@@ -195,8 +195,8 @@ test_all_events: cli # tests must not race interrupts /*test_softirqs:*/ movl VCPU_processor(%rbx),%eax - shl $IRQSTAT_shift,%rax - leaq irq_stat(%rip),%rcx + shll $IRQSTAT_shift,%eax + leaq irq_stat+IRQSTAT_softirq_pending(%rip),%rcx cmpl $0,(%rcx,%rax,1) jne process_softirqs testb $1,VCPU_mce_pending(%rbx)
@@ -663,7 +663,7 @@ handle_ist_exception: /* Send an IPI to ourselves to cover for the lack of event checking. */ movl VCPU_processor(%rbx),%eax shll $IRQSTAT_shift,%eax - leaq irq_stat(%rip),%rcx + leaq irq_stat+IRQSTAT_softirq_pending(%rip),%rcx cmpl $0,(%rcx,%rax,1) je 1f movl $EVENT_CHECK_VECTOR,%edi
x86: don't rely on __softirq_pending to be the first field in irq_cpustat_t
This is even more so as the field doesn't have a comment to that effect in the structure definition.
Once modifying the respective assembly code, also convert the IRQSTAT_shift users to do a 32-bit shift only (as we won't support 48M CPUs any time soon) and use "cmpl" instead of "testl" when checking the field (both reducing code size).
xen/arch/x86/hvm/svm/entry.S +4 -4
@@ -41,10 +41,10 @@ ENTRY(svm_asm_do_resume) CLGI mov VCPU_processor(%rbx),%eax - shl $IRQSTAT_shift,%rax - lea irq_stat(%rip),%rdx - testl $~0,(%rdx,%rax,1) - jnz .Lsvm_process_softirqs + shl $IRQSTAT_shift,%eax + lea irq_stat+IRQSTAT_softirq_pending(%rip),%rdx + cmpl $0,(%rdx,%rax,1) + jne .Lsvm_process_softirqs testb $0, VCPU_nsvm_hap_enabled(%rbx) UNLIKELY_START(nz, nsvm_hap)
xen/arch/x86/hvm/vmx/entry.S +2 -2
@@ -97,8 +97,8 @@ vmx_asm_do_vmentry: cli mov VCPU_processor(%rbx),%eax - shl $IRQSTAT_shift,%rax - lea irq_stat(%rip),%rdx + shl $IRQSTAT_shift,%eax + lea irq_stat+IRQSTAT_softirq_pending(%rip),%rdx cmpl $0,(%rdx,%rax,1) jnz .Lvmx_process_softirqs
xen/arch/x86/x86_64/asm-offsets.c +1 -0
@@ -162,6 +162,7 @@ void __dummy__(void)#endif DEFINE (IRQSTAT_shift,LOG_2 (sizeof (irq_cpustat_t))); +OFFSET (IRQSTAT_softirq_pending, irq_cpustat_t, __softirq_pending);BLANK (); OFFSET(CPUINFO86_ext_features, struct cpuinfo_x86, x86_capability[1]);
xen/arch/x86/x86_64/compat/entry.S +4 -4
@@ -96,10 +96,10 @@ ENTRY(compat_test_all_events) cli # tests must not race interrupts /*compat_test_softirqs:*/ movl VCPU_processor(%rbx),%eax - shlq $IRQSTAT_shift,%rax - leaq irq_stat(%rip),%rcx - testl $~0,(%rcx,%rax,1) - jnz compat_process_softirqs + shll $IRQSTAT_shift,%eax + leaq irq_stat+IRQSTAT_softirq_pending(%rip),%rcx + cmpl $0,(%rcx,%rax,1) + jne compat_process_softirqs testb $1,VCPU_mce_pending(%rbx) jnz compat_process_mce .Lcompat_test_guest_nmi:
xen/arch/x86/x86_64/entry.S +3 -3
@@ -195,8 +195,8 @@ test_all_events: cli # tests must not race interrupts /*test_softirqs:*/ movl VCPU_processor(%rbx),%eax - shl $IRQSTAT_shift,%rax - leaq irq_stat(%rip),%rcx + shll $IRQSTAT_shift,%eax + leaq irq_stat+IRQSTAT_softirq_pending(%rip),%rcx cmpl $0,(%rcx,%rax,1) jne process_softirqs testb $1,VCPU_mce_pending(%rbx)
@@ -663,7 +663,7 @@ handle_ist_exception: /* Send an IPI to ourselves to cover for the lack of event checking. */ movl VCPU_processor(%rbx),%eax shll $IRQSTAT_shift,%eax - leaq irq_stat(%rip),%rcx + leaq irq_stat+IRQSTAT_softirq_pending(%rip),%rcx cmpl $0,(%rcx,%rax,1) je 1f movl $EVENT_CHECK_VECTOR,%edi
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Re: [PATCH v2 0/2] x86: defer processing events on the NMI exit path
In reply to Jan BeulichQuoted 4 lines · Jan Beulich
1: defer processing events on the NMI exit path
2: don't rely on__softirq_pendingto be the first field inirq_cpustat_tSigned-off-by Jan Beulich <[email protected]>
Nice.
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
vmx: handle NMIs before re-enabling interrupts.
Also, switch to calling
do_nmi()and explicitly re-enabling NMIs rather than raising a fake NMI and relying on the NMI processing to IRET, since that handling code is likely to change a fair amount in future.Signed-off-by Tim Deegan <[email protected]>
Sorry, my Acked-by just sent requires one change to be done first:
xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2316
Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -2313,6 +2313,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); + if ( vector == TRAP_machine_check
This almost certainly needs to be TRAP_nmi.
Jan
Quoted 18 lines
xen/arch/x86/hvm/vmx/vmx.c +7
-1+ && ((intr_info & INTR_INFO_INTR_TYPE_MASK) == + (X86_EVENTTYPE_NMI << 8)) ) + { +do_nmi (regs); +enable_nmis (); + }break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);@@ -2486,7 +2493,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ +/* Already handled above. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );
Quoted 12 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · Tim Deegan
vmx: handle NMIs before re-enabling interrupts.
Also, switch to calling
do_nmi()and explicitly re-enabling NMIs rather than raising a fake NMI and relying on the NMI processing to IRET, since that handling code is likely to change a fair amount in future.Signed-off-by Tim Deegan <[email protected]>Sorry, my Acked-by just sent requires one change to be done first:
Quoted 9 lines
xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_regs *regs) · line 2316
Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -2313,6 +2313,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); + if ( vector == TRAP_machine_checkThis almost certainly needs to be
TRAP_nmi.
!
I was foolishly using the watchdog for testing, and of course losing NMIs doesn't cause the watchdog to trigger. :|
Here's v2, with that fixed and updating the description:
commit 7dd3b06ff031c9a8c727df16c5def2afb382101c
Author: Tim Deegan <[email protected]> Date: Thu Feb 28 12:42:15 2013 +0000
vmx: fix handling of NMI VMEXIT.
Call do_nmi() directly and explicitly re-enable NMIs rather than raising an NMI through the APIC. Since NMIs are disabled after the VMEXIT, the raised NMI would be blocked until the next IRET instruction (i.e. the next real interrupt, or after scheduling a PV guest) and in the meantime the guest will spin taking NMI VMEXITS.
Also, handle NMIs before re-enabling interrupts, since if we handle an interrupt (and therefore IRET) before calling do_nmi(), we may end up running the NMI handler with NMIs enabled.
xen/arch/x86/hvm/vmx/vmx.c +8 -1
@@ -2313,6 +2313,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); +if ( vector == TRAP_nmi + && ((intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 )) ) + { +do_nmi (regs); +enable_nmis (); + }break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);
@@ -2486,7 +2493,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ +/* Already handled above. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );
Quoted 21 lines · Tim Deegan
Quoted 20 lines · Jan Beulich
Quoted 20 lines
Quoted 19 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
A quick solution would be to execute a noop function with
run_in_exception_handler(). Alternatively, I can codeenable_nmi()or so which does an inline iret to itself. Which of these would you prefer?I would actually consider avoiding to run softirqs altogether in that case, just like native Linux doesn't do any event or scheduler processing in that case.
That would probably be the easiest solution.
I was already going to do the same for the rentrant NMI and MCE handling case (and also the process pending upcalls checking), due to the complexities of fixing the race condition at the end of the handler.
Unfortunately, I don't think I have time to look at this issue immediately, but if it is ok to wait till the beginning of next week, I
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
That's fine of course.
So that was 3 months ago, and we're likely going to need to do further unfixed releases if this won't move forward. Can you estimate whether you'll be able to get back to this?
Let's make a step in the right direction before 4.3, at least:
FYI, this also fixes oprofile being able to collect HVM guest data.
Quoted 47 lines
commit
d278beed1df2d226911dce92295411018c9bba2fAuthor: Tim Deegan <[email protected]> Date: Thu Feb 28 12:42:15 2013 +0000vmx: handle NMIs before re-enabling interrupts.
Also, switch to calling
do_nmi()and explicitly re-enabling NMIs rather than raising a fake NMI and relying on the NMI processing to IRET, since that handling code is likely to change a fair amount in future.Signed-off-by Tim Deegan <[email protected]>xen/arch/x86/hvm/vmx/vmx.c +8
-1@@ -2313,6 +2313,13 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); +if ( vector == TRAP_machine_check + && ((intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 )) ) + { +do_nmi (regs); +enable_nmis (); + }break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);@@ -2486,7 +2493,7 @@ void vmx_vmexit_handler(struct cpu_user_regs *regs) (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ +/* Already handled above. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 44 lines · Andrew Cooper
Quoted 43 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
The
self_nmi()code cause's an NMI to be triggered by sending an APIC message to the local processor. However, NMIs are blocked by the VMEXIT, until the next iret or VMENTER.Volume 3 Chapter 27 Section 1 of the Intel SDM states:
An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.
As a result, as soon as the VMENTER happens, an immediate VMEXIT happens as a result of the queued NMI. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.
Signed-off-by Malcolm Crossley <[email protected]>Signed-off-by Andrew Cooper <[email protected]>--
Changes since v2
* Switch from 'int $2' todo_nmi()
* Reworked commit message to more clearly explain the problemdiff -r
2489c2926698-rd7ea938044acxen/arch/x86/hvm/vmx/vmx.cxen/arch/x86/hvm/vmx/vmx.c +0
-0@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2279
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8
-0if ( vector == TRAP_machine_check )do_machine_check (regs); +else if ( vector == TRAP_nmi && + ( (intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 ) ) ) +/* Must be called before interrupts are enabled to ensure +* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();But that's only half of it, at least as far as I recall the outcome of the discussion: You want an IRET (or VMRESUME) before possibly going into the scheduler (and hence not back to the current VM). And the same also on the PV exit path from an NMI.
Quoted 4 lines
Quoted 1 line
Jan
When I read this codepath, there seemed to be no consideration for re-scheduling. I will double check, but I think the VMRESUME is unconditional if the VMEXIT reason was a real NMI.
Technically, we could probably get a timer interrupt which causes a scheduling event, between the NMI and the VMEXIT. However, that timer interrupt will have an IRET at the end of it, so it fixes itself.
Quoted 15 lines
~Andrew
Quoted 12 lines
Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c +0
-1break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);@@ -2442,7 +2450,6 @@ void vmx_vmexit_handler(struct cpu_user_ (X86_EVENTTYPE_NMI << 8) ) goto exit_and_crash;HVMTRACE_0D (NMI ); -self_nmi ();/* Real NMI, vector 2: normal processing. */ break ;case TRAP_machine_check:HVMTRACE_0D (MCE );
Quoted 46 lines
Quoted 1 line
Quoted 1 line · Mats Petersson
Quoted 44 lines · Andrew Cooper
Quoted 43 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
The
self_nmi()code cause's an NMI to be triggered by sending an APIC message to the local processor. However, NMIs are blocked by the VMEXIT, until the next iret or VMENTER.Volume 3 Chapter 27 Section 1 of the Intel SDM states:
An NMI causes subsequent NMIs to be blocked, but only after the VM exit completes.
As a result, as soon as the VMENTER happens, an immediate VMEXIT happens as a result of the queued NMI. We have seen hundreds of iterations of this VMEXIT/VMENTER loop before the HVM guest resumes normal operation.
Signed-off-by Malcolm Crossley <[email protected]>Signed-off-by Andrew Cooper <[email protected]>--
Changes since v2
* Switch from 'int $2' todo_nmi()
* Reworked commit message to more clearly explain the problemdiff -r
2489c2926698-rd7ea938044acxen/arch/x86/hvm/vmx/vmx.cxen/arch/x86/hvm/vmx/vmx.c +0
-0@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2279
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8
-0if ( vector == TRAP_machine_check )do_machine_check (regs); +else if ( vector == TRAP_nmi && + ( (intr_info &INTR_INFO_INTR_TYPE_MASK ) == + (X86_EVENTTYPE_NMI <<8 ) ) ) +/* Must be called before interrupts are enabled to ensure +* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();But that's only half of it, at least as far as I recall the outcome of the discussion: You want an IRET (or VMRESUME) before possibly going into the scheduler (and hence not back to the current VM). And the same also on the PV exit path from an NMI.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Jan
When I read this codepath, there seemed to be no consideration for re-scheduling. I will double check, but I think the VMRESUME is unconditional if the VMEXIT reason was a real NMI.
Technically, we could probably get a timer interrupt which causes a scheduling event, between the NMI and the VMEXIT. However, that timer interrupt will have an IRET at the end of it, so it fixes itself.
Not exactly - such a timer interrupt wouldn't cause any scheduling to happen (this and other softirqs get run only when exiting to a guest, or from an idle vCPU); when exiting to the hypervisor nothing like that happens (and it would be catastrophic if it did).
Furthermore, interrupts should _never_ get enabled while handling an NMI.
Jan
Quoted 8 lines · Andrew Cooper
diff -r
2489c2926698-rd7ea938044acxen/arch/x86/hvm/vmx/vmx.cxen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); + else if ( vector == TRAP_nmi &&
xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2282
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +7
-0+ ( (intr_info & INTR_INFO_INTR_TYPE_MASK) == + (X86_EVENTTYPE_NMI << 8) ) ) +/* Must be called before interrupts are enabled to ensure +* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);
I think it might also make sense to move the HVMTARCE invocations that are just above here down to after this block. There's quite a lot of code behind there and though I don't see any potential faults they might well get added later (including in places like printk() and tasklet_schedule()).
I had a look at the rest of the code that runs between the vmexit and here, and it looks OK to me.
George, would that make the tracing more confusing?
Tim.
Quoted 25 lines · Tim Deegan
Quoted 8 lines · Andrew Cooper
diff -r
2489c2926698-rd7ea938044acxen/arch/x86/hvm/vmx/vmx.cxen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -2269,6 +2269,14 @@ void vmx_vmexit_handler(struct cpu_user_ vector = intr_info &INTR_INFO_VECTOR_MASK ;if ( vector == TRAP_machine_check )do_machine_check (regs); + else if ( vector == TRAP_nmi &&xen/arch/x86/hvm/vmx/vmx.c void vmx_vmexit_handler(struct cpu_user_ · line 2282
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +7
-0+ ( (intr_info & INTR_INFO_INTR_TYPE_MASK) == + (X86_EVENTTYPE_NMI << 8) ) ) +/* Must be called before interrupts are enabled to ensure +* the NMI handler code is run before the first IRET. The +* IRET unblocks subsequent NMI's (Intel SDM Vol 3, 6.7.1) +*/ +do_nmi ();break ;case EXIT_REASON_MCE_DURING_VMENTRY :do_machine_check (regs);I think it might also make sense to move the HVMTARCE invocations that are just above here down to after this block. There's quite a lot of code behind there and though I don't see any potential faults they might well get added later (including in places like
printk()andtasklet_schedule()).
Quoted 4 lines
I had a look at the rest of the code that runs between the vmexit and here, and it looks OK to me.
George, would that make the tracing more confusing?
Well, it would mildly, because you'd get the trace in vmx_do_extint() before the trace for the VMEXIT that caused it. :-) If it's important for correctness, then xenalyze will just have to deal. But it would be a lot nicer not to have to deal.
-George