From mboxrd@z Thu Jan 1 00:00:00 1970 From: Radim =?utf-8?B?S3LEjW3DocWZ?= Subject: Re: [PATCH] KVM: x86: Fix lost interrupt on irr_pending race Date: Tue, 18 Nov 2014 16:03:19 +0100 Message-ID: <20141118150318.GA22235@potion.brq.redhat.com> References: <1416174547-5309-1-git-send-email-namit@cs.technion.ac.il> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: pbonzini@redhat.com, kvm@vger.kernel.org, wanpeng.li@linux.intel.com, nadav.amit@gmail.com To: Nadav Amit Return-path: Received: from mx1.redhat.com ([209.132.183.28]:32902 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755948AbaKRPDd (ORCPT ); Tue, 18 Nov 2014 10:03:33 -0500 Content-Disposition: inline In-Reply-To: <1416174547-5309-1-git-send-email-namit@cs.technion.ac.il> Sender: kvm-owner@vger.kernel.org List-ID: 2014-11-16 23:49+0200, Nadav Amit: > apic_find_highest_irr assumes irr_pending is set if any vector in API= C_IRR is > set. If this assumption is broken and apicv is disabled, the injecti= on of > interrupts may be deferred until another interrupt is delivered to th= e guest. > Ultimately, if no other interrupt should be injected to that vCPU, th= e pending > interrupt may be lost. >=20 > commit 56cc2406d68c ("KVM: nVMX: fix "acknowledge interrupt on exit" = when APICv > is in use") changed the behavior of apic_clear_irr so irr_pending is = cleared > after setting APIC_IRR vector. After this commit, if apic_set_irr and > apic_clear_irr run simultaneously, a race may occur, resulting in API= C_IRR > vector set, and irr_pending cleared. In the following example, assume= a single > vector is set in IRR prior to calling apic_clear_irr: >=20 > apic_set_irr apic_clear_irr > ------------ -------------- > apic->irr_pending =3D true; > apic_clear_vector(...); > vec =3D apic_search_irr(apic); > // =3D> vec =3D=3D -1 > apic_set_vector(...); > apic->irr_pending =3D (vec !=3D -1); > // =3D> apic->irr_pending =3D=3D false >=20 > Nonetheless, it appears the race might even occur prior to this commi= t: >=20 > apic_set_irr apic_clear_irr > ------------ -------------- > apic->irr_pending =3D true; > apic->irr_pending =3D false; > apic_clear_vector(...); > if (apic_search_irr(apic) !=3D -1) > apic->irr_pending =3D true; > // =3D> apic->irr_pending =3D=3D false > apic_set_vector(...); >=20 > Fixing this issue by: > 1. Restoring the previous behavior of apic_clear_irr: clear irr_pendi= ng, call > apic_clear_vector, and then if APIC_IRR is non-zero, set irr_pendi= ng. > 2. On apic_set_irr: first call apic_set_vector, then set irr_pending. >=20 > Signed-off-by: Nadav Amit > --- > arch/x86/kvm/lapic.c | 18 ++++++++++++------ > 1 file changed, 12 insertions(+), 6 deletions(-) >=20 > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index 6e8ce5a..e0e5642 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -341,8 +341,12 @@ EXPORT_SYMBOL_GPL(kvm_apic_update_irr); > =20 > static inline void apic_set_irr(int vec, struct kvm_lapic *apic) > { > - apic->irr_pending =3D true; > apic_set_vector(vec, apic->regs + APIC_IRR); > + /* > + * irr_pending must be true if any interrupt is pending; set it aft= er > + * APIC_IRR to avoid race with apic_clear_irr > + */ > + apic->irr_pending =3D true; (A race that ends up with 'irr_pending =3D true' and zero IRR is harmless.) > } > =20 > static inline int apic_search_irr(struct kvm_lapic *apic) > @@ -374,13 +378,15 @@ static inline void apic_clear_irr(int vec, stru= ct kvm_lapic *apic) > =20 > vcpu =3D apic->vcpu; > =20 > - apic_clear_vector(vec, apic->regs + APIC_IRR); > - if (unlikely(kvm_apic_vid_enabled(vcpu->kvm))) > + if (unlikely(kvm_apic_vid_enabled(vcpu->kvm))) { > /* try to update RVI */ > + apic_clear_vector(vec, apic->regs + APIC_IRR); > kvm_make_request(KVM_REQ_EVENT, vcpu); > - else { > - vec =3D apic_search_irr(apic); > - apic->irr_pending =3D (vec !=3D -1); > + } else { > + apic->irr_pending =3D false; > + apic_clear_vector(vec, apic->regs + APIC_IRR); > + if (apic_search_irr(apic) !=3D -1) > + apic->irr_pending =3D true; > } Works because apic_clear_vector() is also a compiler barrier ... Reviewed-by: Radim Kr=C4=8Dm=C3=A1=C5=99 (I hope the performance gain of irr_pending is worth its complexity.)