From: Marcelo Tosatti Subject: KVM: x86: better fix for race between nmi injection and enabling nmi window (v2) Date: Fri, 1 Apr 2011 11:26:29 -0300 Message-ID: <20110401142629.GA27235@amt.cnet> References: <20110330163028.GA27365@amt.cnet> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Avi Kivity , Gleb Natapov To: kvm Return-path: Received: from mx1.redhat.com ([209.132.183.28]:54792 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752372Ab1DAO1U (ORCPT ); Fri, 1 Apr 2011 10:27:20 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p31ERJ5I026272 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 1 Apr 2011 10:27:20 -0400 Content-Disposition: inline In-Reply-To: <20110330163028.GA27365@amt.cnet> Sender: kvm-owner@vger.kernel.org List-ID: From: Gleb Natapov Fix race between nmi injection and enabling nmi window in a simpler way. Signed-off-by: Marcelo Tosatti v2: document benign race. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bfd7763..e6bdc14 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; + 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);