From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jan Kiszka Subject: Re: [PATCH v2] KVM: x86: Convert INIT and SIPI signals into synchronously handled requests Date: Tue, 05 Mar 2013 00:00:10 +0100 Message-ID: <5135277A.4070806@web.de> References: <51351517.3090600@web.de> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="----enig2VTRAJOVFPTEKRJVKTDPI" Cc: kvm , Paolo Bonzini To: Gleb Natapov , Marcelo Tosatti Return-path: Received: from mout.web.de ([212.227.17.11]:51622 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758783Ab3CDXAR (ORCPT ); Mon, 4 Mar 2013 18:00:17 -0500 In-Reply-To: <51351517.3090600@web.de> Sender: kvm-owner@vger.kernel.org List-ID: This is an OpenPGP/MIME signed message (RFC 4880 and 3156) ------enig2VTRAJOVFPTEKRJVKTDPI Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 2013-03-04 22:41, Jan Kiszka wrote: > From: Jan Kiszka >=20 > A VCPU sending INIT or SIPI to some other VCPU races for setting the > remote VCPU's mp_state. When we were unlucky, KVM_MP_STATE_INIT_RECEIVE= D > was overwritten by kvm_emulate_halt and, thus, got lost. >=20 > Fix this by raising requests on the sender side that will then be > handled synchronously over the target VCPU context. >=20 > Signed-off-by: Jan Kiszka > --- >=20 > Changes in v2: > - check transition to INIT_RECEIVED in vcpu_enter_guest > - removed return value of kvm_check_init_and_sipi - caller has to > check for relevant transition afterward > - add write barrier after setting sipi_vector >=20 > arch/x86/kvm/lapic.c | 11 ++++++----- > arch/x86/kvm/x86.c | 15 +++++++++++++++ > include/linux/kvm_host.h | 2 ++ > 3 files changed, 23 insertions(+), 5 deletions(-) >=20 > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index 02b51dd..7986c9f 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -731,8 +731,7 @@ static int __apic_accept_irq(struct kvm_lapic *apic= , int delivery_mode, > case APIC_DM_INIT: > if (!trig_mode || level) { > result =3D 1; > - vcpu->arch.mp_state =3D KVM_MP_STATE_INIT_RECEIVED; > - kvm_make_request(KVM_REQ_EVENT, vcpu); > + kvm_make_request(KVM_REQ_INIT, vcpu); > kvm_vcpu_kick(vcpu); > } else { > apic_debug("Ignoring de-assert INIT to vcpu %d\n", > @@ -743,11 +742,13 @@ static int __apic_accept_irq(struct kvm_lapic *ap= ic, int delivery_mode, > case APIC_DM_STARTUP: > apic_debug("SIPI to vcpu %d vector 0x%02x\n", > vcpu->vcpu_id, vector); > - if (vcpu->arch.mp_state =3D=3D KVM_MP_STATE_INIT_RECEIVED) { > + if (vcpu->arch.mp_state =3D=3D KVM_MP_STATE_INIT_RECEIVED || > + test_bit(KVM_REQ_INIT, &vcpu->requests)) { > result =3D 1; > vcpu->arch.sipi_vector =3D vector; > - vcpu->arch.mp_state =3D KVM_MP_STATE_SIPI_RECEIVED; > - kvm_make_request(KVM_REQ_EVENT, vcpu); > + /* make sure sipi_vector is visible for the receiver */ > + smp_wmb(); > + kvm_make_request(KVM_REQ_SIPI, vcpu); > kvm_vcpu_kick(vcpu); > } > break; > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index d0cf737..0be04b9 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -5641,6 +5641,15 @@ static void update_eoi_exitmap(struct kvm_vcpu *= vcpu) > kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap); > } > =20 > +static void kvm_check_init_and_sipi(struct kvm_vcpu *vcpu) > +{ > + if (kvm_check_request(KVM_REQ_INIT, vcpu)) > + vcpu->arch.mp_state =3D KVM_MP_STATE_INIT_RECEIVED; And here is a small race between clearing REQ_INIT and setting INIT_RECEIVED. It can make the LAPIC drop the SIPI incorrectly. Need to break up test and clear, doing the clear after mp_state update. Yeah... Jan > + if (kvm_check_request(KVM_REQ_SIPI, vcpu) && > + vcpu->arch.mp_state =3D=3D KVM_MP_STATE_INIT_RECEIVED) > + vcpu->arch.mp_state =3D KVM_MP_STATE_SIPI_RECEIVED; > +} > + > static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > { > int r; > @@ -5649,6 +5658,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcp= u) > bool req_immediate_exit =3D 0; > =20 > if (vcpu->requests) { > + kvm_check_init_and_sipi(vcpu); > + if (vcpu->arch.mp_state =3D=3D KVM_MP_STATE_INIT_RECEIVED) { > + r =3D 1; > + goto out; > + } > if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) > kvm_mmu_unload(vcpu); > if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu)) > @@ -6977,6 +6991,7 @@ void kvm_arch_flush_shadow_memslot(struct kvm *kv= m, > =20 > int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) > { > + kvm_check_init_and_sipi(vcpu); > return (vcpu->arch.mp_state =3D=3D KVM_MP_STATE_RUNNABLE && > !vcpu->arch.apf.halted) > || !list_empty_careful(&vcpu->async_pf.done) > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index 722cae7..1a191c9 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -124,6 +124,8 @@ static inline bool is_error_page(struct page *page)= > #define KVM_REQ_MCLOCK_INPROGRESS 20 > #define KVM_REQ_EPR_EXIT 21 > #define KVM_REQ_EOIBITMAP 22 > +#define KVM_REQ_INIT 23 > +#define KVM_REQ_SIPI 24 > =20 > #define KVM_USERSPACE_IRQ_SOURCE_ID 0 > #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1 >=20 ------enig2VTRAJOVFPTEKRJVKTDPI Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.16 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlE1J3sACgkQitSsb3rl5xSFtwCg1KIu9f9QFHB12totaPe4b0cf M8kAoMLokvmP+Bdq2HC6NxR/5q9uUFpB =tKEd -----END PGP SIGNATURE----- ------enig2VTRAJOVFPTEKRJVKTDPI--