From mboxrd@z Thu Jan 1 00:00:00 1970 From: Gleb Natapov Subject: Re: [patch 02/16] x86: pvclock: make sure rdtsc doesnt speculate out of region Date: Fri, 2 Nov 2012 08:02:08 +0200 Message-ID: <20121102060208.GA29441@redhat.com> References: <20121031224656.417434866@redhat.com> <20121031224823.907464421@redhat.com> <20121101114807.GQ25650@redhat.com> <50927DFF.4000502@parallels.com> <20121101205611.GB14888@amt.cnet> <20121101221354.GA17149@redhat.com> <20121101222151.GA20682@amt.cnet> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Glauber Costa , kvm@vger.kernel.org, johnstul@us.ibm.com, jeremy@goop.org, zamsden@gmail.com, avi@redhat.com, pbonzini@redhat.com To: Marcelo Tosatti Return-path: Received: from mx1.redhat.com ([209.132.183.28]:25894 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751186Ab2KBGC1 (ORCPT ); Fri, 2 Nov 2012 02:02:27 -0400 Content-Disposition: inline In-Reply-To: <20121101222151.GA20682@amt.cnet> Sender: kvm-owner@vger.kernel.org List-ID: On Thu, Nov 01, 2012 at 08:21:51PM -0200, Marcelo Tosatti wrote: > On Fri, Nov 02, 2012 at 12:13:54AM +0200, Gleb Natapov wrote: > > On Thu, Nov 01, 2012 at 06:56:11PM -0200, Marcelo Tosatti wrote: > > > On Thu, Nov 01, 2012 at 05:49:51PM +0400, Glauber Costa wrote: > > > > On 11/01/2012 03:48 PM, Gleb Natapov wrote: > > > > > On Wed, Oct 31, 2012 at 08:46:58PM -0200, Marcelo Tosatti wrote: > > > > >> Originally from Jeremy Fitzhardinge. > > > > >> > > > > >> pvclock_get_time_values, which contains the memory barriers > > > > >> will be removed by next patch. > > > > >> > > > > >> Signed-off-by: Marcelo Tosatti > > > > >> > > > > >> Index: vsyscall/arch/x86/kernel/pvclock.c > > > > >> =================================================================== > > > > >> --- vsyscall.orig/arch/x86/kernel/pvclock.c > > > > >> +++ vsyscall/arch/x86/kernel/pvclock.c > > > > >> @@ -97,10 +97,10 @@ cycle_t pvclock_clocksource_read(struct > > > > >> > > > > >> do { > > > > >> version = pvclock_get_time_values(&shadow, src); > > > > >> - barrier(); > > > > >> + rdtsc_barrier(); > > > > >> offset = pvclock_get_nsec_offset(&shadow); > > > > >> ret = shadow.system_timestamp + offset; > > > > >> - barrier(); > > > > >> + rdtsc_barrier(); > > > > >> } while (version != src->version); > > > > >> > > > > >> if ((valid_flags & PVCLOCK_TSC_STABLE_BIT) && > > > > >> > > > > > On a guest without SSE2 rdtsc_barrier() will be nop while rmb() will > > > > > be "lock; addl $0,0(%%esp)". I doubt pvclock will work correctly either > > > > > way though. > > > > > > > > > > -- > > > > > Gleb. > > > > > > > > > Actually it shouldn't matter for KVM, since the page is only updated by > > > > the vcpu, and the guest is never running while it happens. If Jeremy is > > > > fine with this, so should I. > > > > > > 17.13 TIME-STAMP COUNTER > > > > > > "The RDTSC instruction is not serializing or ordered with other > > > instructions. It does not necessarily wait until all previous > > > instructions have been executed before reading the counter. Similarly, > > > subsequent instructions may begin execution before the RDTSC instruction > > > operation is performed." > > > > > > Both instructions are TSC barriers. > > > > > Which both instructions? > > static __always_inline void rdtsc_barrier(void) > { > alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC); > alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC); > } Both of them will be patched to nop if guest does not have SSE2 cpuid bit. -- Gleb.