From: Marcelo Tosatti Subject: Re: [PATCH 3/3] Fix TSC MSR read in nested SVM Date: Fri, 5 Aug 2011 12:32:24 -0300 Message-ID: <20110805153224.GB16953@amt.cnet> References: <1312289591-nyh@il.ibm.com> <201108021255.p72CtNjD002153@rice.haifa.ibm.com> <20110803210052.GB31923@amt.cnet> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: "Nadav Har'El" , kvm@vger.kernel.org, Bandan Das , avi@redhat.com To: Zachary Amsden , Joerg Roedel Return-path: Received: from mx1.redhat.com ([209.132.183.28]:4781 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752974Ab1HERLJ (ORCPT ); Fri, 5 Aug 2011 13:11:09 -0400 Content-Disposition: inline In-Reply-To: <20110803210052.GB31923@amt.cnet> Sender: kvm-owner@vger.kernel.org List-ID: On Wed, Aug 03, 2011 at 06:00:52PM -0300, Marcelo Tosatti wrote: > On Wed, Aug 03, 2011 at 01:34:58AM -0700, Zachary Amsden wrote: > > Caution: this requires more care. > >=20 > > Pretty sure this breaks userspace suspend at the cost of supporting= a > > not-so-reasonable hardware feature. > >=20 > > On Tue, Aug 2, 2011 at 5:55 AM, Nadav Har'El wrote= : > > > When the TSC MSR is read by an L2 guest (when L1 allowed this MSR= to be > > > read without exit), we need to return L2's notion of the TSC, not= L1's. > > > > > > The current code incorrectly returned L1 TSC, because svm_get_msr= () was also > > > used in x86.c where this was assumed, but now that these places c= all the new > > > svm_read_l1_tsc(), the MSR read can be fixed. > > > > > > Signed-off-by: Nadav Har'El > > > --- > > > =A0arch/x86/kvm/svm.c | =A0 =A04 +--- > > > =A01 file changed, 1 insertion(+), 3 deletions(-) > > > > > > --- .before/arch/x86/kvm/svm.c =A02011-08-02 15:51:02.000000000 += 0300 > > > +++ .after/arch/x86/kvm/svm.c =A0 2011-08-02 15:51:02.000000000 += 0300 > > > @@ -2907,9 +2907,7 @@ static int svm_get_msr(struct kvm_vcpu * > > > > > > =A0 =A0 =A0 =A0switch (ecx) { > > > =A0 =A0 =A0 =A0case MSR_IA32_TSC: { > > > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 struct vmcb *vmcb =3D get_host_vmcb= (svm); > > > - > > > - =A0 =A0 =A0 =A0 =A0 =A0 =A0 *data =3D vmcb->control.tsc_offset = + > > > + =A0 =A0 =A0 =A0 =A0 =A0 =A0 *data =3D svm->vmcb->control.tsc_of= fset + > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0svm_scale_tsc(vcpu= , native_read_tsc()); > > > > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0break; > > > > >=20 > >=20 > > This is correct. Now you properly return the correct MSR value for > > the TSC to the guest in all cases. > >=20 > > However, there is a BIG PROBLEM (yes, it is that bad). Sorry I did > > not think of it before. > >=20 > > When the guest gets suspended and frozen by userspace, to be restar= ted > > later, what qemu is going to do is come along and read all of the M= SRs > > as part of the saved state. One of those happens to be the TSC MSR= =2E > >=20 > > Since you can't guarantee suspend will take place when only L1 is > > running, you may have mixed L1/L2 TSC MSRs now being returned to > > userspace. Now, when you resume this guest, you will have mixed L1= /L2 > > TSC values written into the MSRs. > >=20 > > Those will almost certainly fail to be matched by the TSC offset > > matching code, and thus, with multiple VCPUs, you will end up with > > slightly unsynchronized TSCs, and with that, all the problems > > associated with unstable TSC come back to haunt you again. Basical= ly, > > all bets for stability are off. >=20 > TSC synchronization is the least of your problems if you attempt to > save/restore state a guest while any vcpu is in L2 mode. >=20 > So to keep consistency between the remaining MSRs, i agree with Nadav= 's > patch. Apparently SVM's original patches to return L1 TSC were aimed = at > fixing the problem VMX is facing now, which is fixed by introduction > read_l1_tsc helpers. Joerg, can you review and ack Nadav's SVM patch? TIA