From: Marcelo Tosatti Subject: Re: [PATCH 3/3] Fix TSC MSR read in nested SVM Date: Wed, 3 Aug 2011 18:00:52 -0300 Message-ID: <20110803210052.GB31923@amt.cnet> References: <1312289591-nyh@il.ibm.com> <201108021255.p72CtNjD002153@rice.haifa.ibm.com> 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, "Roedel, Joerg" , Bandan Das , avi@redhat.com To: Zachary Amsden Return-path: Received: from mx1.redhat.com ([209.132.183.28]:65348 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755429Ab1HCVPj (ORCPT ); Wed, 3 Aug 2011 17:15:39 -0400 Content-Disposition: inline In-Reply-To: Sender: kvm-owner@vger.kernel.org List-ID: 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 t= o be > > read without exit), we need to return L2's notion of the TSC, not L= 1'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 cal= l 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 +03= 00 > > +++ .after/arch/x86/kvm/svm.c =A0 2011-08-02 15:51:02.000000000 +03= 00 > > @@ -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(s= vm); > > - > > - =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_offs= et + > > =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 restarte= d > later, what qemu is going to do is come along and read all of the MSR= s > as part of the saved state. One of those happens to be the TSC MSR. >=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/L= 2 > 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. Basically= , > all bets for stability are off. TSC synchronization is the least of your problems if you attempt to save/restore state a guest while any vcpu is in L2 mode. 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. > Apart from recording the L1 and L2 TSC through separate MSRs, there > isn't a good way to solve this. Walking through all the steps again, > I'm pretty sure this is why I thought it would be better for L0 > kvm_get_msr() to return the L1 values even if L2 was running. >=20 > In the end, it may not be worth the hassle to support this mode of > operation that to our current knowledge, is in fact, unused. I would > much prefer to see a bad virtualization of a known insecure and unuse= d > guest mode than to have a well used feature such as L1 guest suspend = / > resume continue to cause clock de-synchronization. >=20 > Zach