KVM: x86: better fix for race between nmi injection and enabling nmi window
Based on Gleb's idea, fix race between nmi injection and enabling nmi window in a simpler way.
arch/x86/kvm/x86.c +4 -1
@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)static int vcpu_enter_guest (struct kvm_vcpu *vcpu) {int r; +int nmi_pending;bool req_int_win = !irqchip_in_kernel (vcpu->kvm) && vcpu->run->request_interrupt_window;
@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (unlikely (r)) goto out; + nmi_pending =ACCESS_ONCE (vcpu->arch.nmi_pending); +if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {inject_pending_event (vcpu);/* enable NMI/IRQ window open exits if needed */ -if (vcpu->arch.nmi_pending) +if (nmi_pending) kvm_x86_ops->enable_nmi_window (vcpu);else if (kvm_cpu_has_interrupt (vcpu) || req_int_win) kvm_x86_ops->enable_irq_window (vcpu);
Quoted 6 lines · Marcelo Tosatti
Based on Gleb's idea, fix race between nmi injection and enabling nmi window in a simpler way.
Signed-off-by Marcelo Tosatti <[email protected]>
But we need to revert the patch that introduced use of request for NMI first. Otherwise NMI will not be delivered, no?
Quoted 17 lines
arch/x86/kvm/x86.c +2
-0@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)static int vcpu_enter_guest (struct kvm_vcpu *vcpu) {int r; +int nmi_pending;bool req_int_win = !irqchip_in_kernel (vcpu->kvm) && vcpu->run->request_interrupt_window;@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (unlikely (r)) goto out; + nmi_pending =ACCESS_ONCE (vcpu->arch.nmi_pending);
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5208
Quoted 10 lines
arch/x86/kvm/x86.c +2
-1+if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {inject_pending_event (vcpu);/* enable NMI/IRQ window open exits if needed */ -if (vcpu->arch.nmi_pending) +if (nmi_pending) kvm_x86_ops->enable_nmi_window (vcpu);else if (kvm_cpu_has_interrupt (vcpu) || req_int_win) kvm_x86_ops->enable_irq_window (vcpu);
--
Gleb.
Quoted 9 lines · Gleb Natapov
Quoted 6 lines · Marcelo Tosatti
Based on Gleb's idea, fix race between nmi injection and enabling nmi window in a simpler way.
Signed-off-by Marcelo Tosatti <[email protected]>But we need to revert the patch that introduced use of request for NMI first. Otherwise NMI will not be delivered, no?
Yes, pushed, can you verify please?
Quoted 23 lines · Marcelo Tosatti
Based on Gleb's idea, fix race between nmi injection and enabling nmi window in a simpler way.
Signed-off-by Marcelo Tosatti<[email protected]>arch/x86/kvm/x86.c +2
-0@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)static int vcpu_enter_guest (struct kvm_vcpu *vcpu) {int r; +int nmi_pending;bool req_int_win = !irqchip_in_kernel (vcpu->kvm)&& vcpu->run->request_interrupt_window;@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (unlikely (r)) goto out; + nmi_pending =ACCESS_ONCE (vcpu->arch.nmi_pending);
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5208
Quoted 10 lines
arch/x86/kvm/x86.c +2
-1+if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {inject_pending_event (vcpu);/* enable NMI/IRQ window open exits if needed */ -if (vcpu->arch.nmi_pending) +if (nmi_pending) kvm_x86_ops->enable_nmi_window (vcpu);else if (kvm_cpu_has_interrupt (vcpu) || req_int_win) kvm_x86_ops->enable_irq_window (vcpu);
What about the check in inject_pending_events()?
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 27 lines · Avi Kivity
Quoted 26 lines · Marcelo Tosatti
Based on Gleb's idea, fix race between nmi injection and enabling nmi window in a simpler way.
Signed-off-by Marcelo Tosatti<[email protected]>arch/x86/kvm/x86.c +3
-0@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)static int vcpu_enter_guest (struct kvm_vcpu *vcpu) {int r; +int nmi_pending;bool req_int_win = !irqchip_in_kernel (vcpu->kvm)&& vcpu->run->request_interrupt_window;@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (unlikely (r)) goto out; + nmi_pending =ACCESS_ONCE (vcpu->arch.nmi_pending); +if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {inject_pending_event (vcpu);
Quoted 10 lines
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5208
Quoted 7 lines
arch/x86/kvm/x86.c +1
-1/* enable NMI/IRQ window open exits if needed */ -if (vcpu->arch.nmi_pending) +if (nmi_pending) kvm_x86_ops->enable_nmi_window (vcpu);else if (kvm_cpu_has_interrupt (vcpu) || req_int_win) kvm_x86_ops->enable_irq_window (vcpu);What about the check in
inject_pending_events()?
Didn't we decide that this check is not a problem? Worst that can happen is NMI injection will be delayed till next exit.
--
Gleb.
Quoted 36 lines · Gleb Natapov
Quoted 35 lines · Avi Kivity
Quoted 33 lines · Marcelo Tosatti
Based on Gleb's idea, fix race between nmi injection and enabling nmi window in a simpler way.
Signed-off-by Marcelo Tosatti<[email protected]>arch/x86/kvm/x86.c +4
-1@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)static int vcpu_enter_guest (struct kvm_vcpu *vcpu) {int r; +int nmi_pending;bool req_int_win = !irqchip_in_kernel (vcpu->kvm)&& vcpu->run->request_interrupt_window;@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (unlikely (r)) goto out; + nmi_pending =ACCESS_ONCE (vcpu->arch.nmi_pending); +if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {inject_pending_event (vcpu);/* enable NMI/IRQ window open exits if needed */ -if (vcpu->arch.nmi_pending) +if (nmi_pending) kvm_x86_ops->enable_nmi_window (vcpu);else if (kvm_cpu_has_interrupt (vcpu) || req_int_win) kvm_x86_ops->enable_irq_window (vcpu);
Quoted 4 lines
Quoted 2 lines
What about the check in
inject_pending_events()?Didn't we decide that this check is not a problem? Worst that can happen is NMI injection will be delayed till next exit.
Could be very far in the future.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 37 lines · Avi Kivity
Quoted 36 lines · Gleb Natapov
Quoted 35 lines · Avi Kivity
Quoted 33 lines · Marcelo Tosatti
Based on Gleb's idea, fix race between nmi injection and enabling nmi window in a simpler way.
Signed-off-by Marcelo Tosatti<[email protected]>arch/x86/kvm/x86.c +4
-1@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)static int vcpu_enter_guest (struct kvm_vcpu *vcpu) {int r; +int nmi_pending;bool req_int_win = !irqchip_in_kernel (vcpu->kvm)&& vcpu->run->request_interrupt_window;@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (unlikely (r)) goto out; + nmi_pending =ACCESS_ONCE (vcpu->arch.nmi_pending); +if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {inject_pending_event (vcpu);/* enable NMI/IRQ window open exits if needed */ -if (vcpu->arch.nmi_pending) +if (nmi_pending) kvm_x86_ops->enable_nmi_window (vcpu);else if (kvm_cpu_has_interrupt (vcpu) || req_int_win) kvm_x86_ops->enable_irq_window (vcpu);
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
What about the check in
inject_pending_events()?Didn't we decide that this check is not a problem? Worst that can happen is NMI injection will be delayed till next exit.
Could be very far in the future.
Next host interrupt. But with tickles host and guest yeah.
--
Gleb.
Quoted 38 lines · Gleb Natapov
Quoted 37 lines · Avi Kivity
Quoted 36 lines · Gleb Natapov
Quoted 35 lines · Avi Kivity
On 03/30/2011 06:30 PM, Marcelo Tosatti wrote: >Based on Gleb's idea, fix race between nmi injection and enabling >nmi window in a simpler way. > >Signed-off-by: Marcelo Tosatti<[email protected]> > >diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >index a6a129f..9a7cc1be 100644 >--- a/arch/x86/kvm/x86.c >+++ b/arch/x86/kvm/x86.c >@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu) > static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > { > int r; >+ int nmi_pending; > bool req_int_win = !irqchip_in_kernel(vcpu->kvm)&& > vcpu->run->request_interrupt_window; > >@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > if (unlikely(r)) > goto out; > >+ nmi_pending = ACCESS_ONCE(vcpu->arch.nmi_pending); >+ > if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) { > inject_pending_event(vcpu); > > /* enable NMI/IRQ window open exits if needed */ >- if (vcpu->arch.nmi_pending) >+ if (nmi_pending) > kvm_x86_ops->enable_nmi_window(vcpu); > else if (kvm_cpu_has_interrupt(vcpu) || req_int_win) > kvm_x86_ops->enable_irq_window(vcpu); >
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
What about the check in
inject_pending_events()?Didn't we decide that this check is not a problem? Worst that can happen is NMI injection will be delayed till next exit.
Could be very far in the future.
Next host interrupt. But with tickles host and guest yeah.
esp. important with NMI, which may be used in a situation where your tick (and everything else) are dead.
Signature · 1 line
error compiling committee.c: too many arguments to function
On Thu, Mar 31, 2011 at 11:25:46AM +0200, Avi Kivity wrote:
<snip>
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
else if (kvm_cpu_has_interrupt(vcpu) || req_int_win) kvm_x86_ops->enable_irq_window(vcpu);What about the check in
inject_pending_events()?Didn't we decide that this check is not a problem? Worst that can happen is NMI injection will be delayed till next exit.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Could be very far in the future.
Next host interrupt. But with tickles host and guest yeah.
esp. important with NMI, which may be used in a situation where your tick (and everything else) are dead.
Well the host must be alive. So eventually NMI will be delivered to the guest, but not closely matching guest visible clocks.
What is the problem with that? The race should be rare enough to not interfere with NMI watchdog-like things?
Quoted 44 lines · Avi Kivity
Quoted 43 lines · Gleb Natapov
Quoted 42 lines · Avi Kivity
Quoted 40 lines · Gleb Natapov
Quoted 35 lines · Avi Kivity
On 03/30/2011 06:30 PM, Marcelo Tosatti wrote: >Based on Gleb's idea, fix race between nmi injection and enabling >nmi window in a simpler way. > >Signed-off-by: Marcelo Tosatti<[email protected]> > >diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >index a6a129f..9a7cc1be 100644 >--- a/arch/x86/kvm/x86.c >+++ b/arch/x86/kvm/x86.c >@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu) > static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > { > int r; >+ int nmi_pending; > bool req_int_win = !irqchip_in_kernel(vcpu->kvm)&& > vcpu->run->request_interrupt_window; > >@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > if (unlikely(r)) > goto out; > >+ nmi_pending = ACCESS_ONCE(vcpu->arch.nmi_pending); >+ > if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) { > inject_pending_event(vcpu); > > /* enable NMI/IRQ window open exits if needed */ >- if (vcpu->arch.nmi_pending) >+ if (nmi_pending) > kvm_x86_ops->enable_nmi_window(vcpu); > else if (kvm_cpu_has_interrupt(vcpu) || req_int_win) > kvm_x86_ops->enable_irq_window(vcpu); >Quoted 2 lines
What about the check in
inject_pending_events()?Didn't we decide that this check is not a problem? Worst that can happen is NMI injection will be delayed till next exit.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Could be very far in the future.
Next host interrupt. But with tickles host and guest yeah.
esp. important with NMI, which may be used in a situation where your tick (and everything else) are dead.
If host is alive eventually cpu should receive reschedule IPI.
--
Gleb.
Quoted 38 lines · Gleb Natapov
Quoted 27 lines · Avi Kivity
Quoted 26 lines · Marcelo Tosatti
Based on Gleb's idea, fix race between nmi injection and enabling nmi window in a simpler way.
Signed-off-by Marcelo Tosatti<[email protected]>arch/x86/kvm/x86.c +3
-0@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)static int vcpu_enter_guest (struct kvm_vcpu *vcpu) {int r; +int nmi_pending;bool req_int_win = !irqchip_in_kernel (vcpu->kvm)&& vcpu->run->request_interrupt_window;@@ -5195,11 +5196,13 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (unlikely (r)) goto out; + nmi_pending =ACCESS_ONCE (vcpu->arch.nmi_pending); +if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {inject_pending_event (vcpu);Quoted 10 lines
arch/x86/kvm/x86.c static int vcpu_enter_guest(struct kvm_vcpu *vcpu) · line 5208
Quoted 7 lines
arch/x86/kvm/x86.c +1
-1/* enable NMI/IRQ window open exits if needed */ -if (vcpu->arch.nmi_pending) +if (nmi_pending) kvm_x86_ops->enable_nmi_window (vcpu);else if (kvm_cpu_has_interrupt (vcpu) || req_int_win) kvm_x86_ops->enable_irq_window (vcpu);What about the check in
inject_pending_events()?
Quoted 2 lines
Didn't we decide that this check is not a problem? Worst that can happen is NMI injection will be delayed till next exit.
Yes.
KVM: x86: better fix for race between nmi injection and enabling nmi window (v2)
In reply to Marcelo TosattiFrom: Gleb Natapov <[email protected]>
Fix race between nmi injection and enabling nmi window in a simpler way.
v2: document benign race.
arch/x86/kvm/x86.c +10 -1
@@ -5152,6 +5152,7 @@ static void kvm_put_guest_xcr0(struct kvm_vcpu *vcpu)static int vcpu_enter_guest (struct kvm_vcpu *vcpu) {int r; +bool nmi_pending;bool req_int_win = !irqchip_in_kernel (vcpu->kvm) && vcpu->run->request_interrupt_window;
@@ -5197,11 +5198,19 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (unlikely (r)) goto out; +/* +* An NMI can be injected between local nmi_pending read and +* vcpu->arch.nmi_pending read inside inject_pending_event(). +* But in that case, KVM_REQ_EVENT will be set, which makes +* the race described above benign. +*/ + nmi_pending =ACCESS_ONCE (vcpu->arch.nmi_pending); +if (kvm_check_request (KVM_REQ_EVENT , vcpu) || req_int_win) {inject_pending_event (vcpu);/* enable NMI/IRQ window open exits if needed */ -if (vcpu->arch.nmi_pending) +if (nmi_pending) kvm_x86_ops->enable_nmi_window (vcpu);else if (kvm_cpu_has_interrupt (vcpu) || req_int_win) kvm_x86_ops->enable_irq_window (vcpu);