From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marcelo Tosatti Subject: Re: [PATCH 1/2] x86: kvmclock: abolish PVCLOCK_COUNTS_FROM_ZERO Date: Tue, 22 Sep 2015 16:01:17 -0300 Message-ID: <20150922190117.GA23748@amt.cnet> References: <1442591670-5216-1-git-send-email-rkrcmar@redhat.com> <1442591670-5216-2-git-send-email-rkrcmar@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Paolo Bonzini , Luiz Capitulino To: Radim =?utf-8?B?S3LEjW3DocWZ?= Return-path: Content-Disposition: inline In-Reply-To: <1442591670-5216-2-git-send-email-rkrcmar@redhat.com> Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org On Fri, Sep 18, 2015 at 05:54:29PM +0200, Radim Kr=C4=8Dm=C3=A1=C5=99 w= rote: > Newer KVM won't be exposing PVCLOCK_COUNTS_FROM_ZERO anymore. > The purpose of that flags was to start counting system time from 0 wh= en > the KVM clock has been initialized. > We can achieve the same by selecting one read as the initial point. >=20 > A simple subtraction will work unless the KVM clock count overflows > earlier (has smaller width) than scheduler's cycle count. We should = be > safe till x86_128. >=20 > Because PVCLOCK_COUNTS_FROM_ZERO was enabled only on new hypervisors, > setting sched clock as stable based on PVCLOCK_TSC_STABLE_BIT might > regress on older ones. >=20 > I presume we don't need to change kvm_clock_read instead of introduci= ng > kvm_sched_clock_read. A problem could arise in case sched_clock is > expected to return the same value as get_cycles, but we should have > merged those clocks in that case. >=20 > Signed-off-by: Radim Kr=C4=8Dm=C3=A1=C5=99 > --- > arch/x86/kernel/kvmclock.c | 46 +++++++++++++++++++++++++++++++++++-= ---------- > 1 file changed, 35 insertions(+), 11 deletions(-) >=20 > diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c > index 2c7aafa70702..ef5b3d2cecce 100644 > --- a/arch/x86/kernel/kvmclock.c > +++ b/arch/x86/kernel/kvmclock.c > @@ -32,6 +32,7 @@ > static int kvmclock =3D 1; > static int msr_kvm_system_time =3D MSR_KVM_SYSTEM_TIME; > static int msr_kvm_wall_clock =3D MSR_KVM_WALL_CLOCK; > +static cycle_t kvm_sched_clock_offset; > =20 > static int parse_no_kvmclock(char *arg) > { > @@ -92,6 +93,29 @@ static cycle_t kvm_clock_get_cycles(struct clockso= urce *cs) > return kvm_clock_read(); > } > =20 > +static cycle_t kvm_sched_clock_read(void) > +{ > + return kvm_clock_read() - kvm_sched_clock_offset; > +} > + > +static inline void kvm_sched_clock_init(bool stable) > +{ > + if (!stable) { > + pv_time_ops.sched_clock =3D kvm_clock_read; > + return; > + } > + > + kvm_sched_clock_offset =3D kvm_clock_read(); > + pv_time_ops.sched_clock =3D kvm_sched_clock_read; > + set_sched_clock_stable(); > + > + printk("kvm-clock: using sched offset of %llu cycles\n", > + kvm_sched_clock_offset); > + > + BUILD_BUG_ON(sizeof(kvm_sched_clock_offset) > > + sizeof(((struct pvclock_vcpu_time_info *)NULL)->system_tim= e)); > +} > + > /* > * If we don't do that, there is the possibility that the guest > * will calibrate under heavy load - thus, getting a lower lpj - > @@ -248,7 +272,17 @@ void __init kvmclock_init(void) > memblock_free(mem, size); > return; > } > - pv_time_ops.sched_clock =3D kvm_clock_read; > + > + if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT)) > + pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT); > + > + cpu =3D get_cpu(); > + vcpu_time =3D &hv_clock[cpu].pvti; > + flags =3D pvclock_read_flags(vcpu_time); > + > + kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT); > + put_cpu(); > + > x86_platform.calibrate_tsc =3D kvm_get_tsc_khz; > x86_platform.get_wallclock =3D kvm_get_wallclock; > x86_platform.set_wallclock =3D kvm_set_wallclock; > @@ -265,16 +299,6 @@ void __init kvmclock_init(void) > kvm_get_preset_lpj(); > clocksource_register_hz(&kvm_clock, NSEC_PER_SEC); > pv_info.name =3D "KVM"; > - > - if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT)) > - pvclock_set_flags(~0); > - > - cpu =3D get_cpu(); > - vcpu_time =3D &hv_clock[cpu].pvti; > - flags =3D pvclock_read_flags(vcpu_time); > - if (flags & PVCLOCK_COUNTS_FROM_ZERO) > - set_sched_clock_stable(); > - put_cpu(); > } > =20 > int __init kvm_setup_vsyscall_timeinfo(void) > --=20 > 2.5.2 ACK