From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marcelo Tosatti Subject: Re: [PATCH 1/2] kvm: kvmclock: apply kvmclock offset to guest wall clock time Date: Mon, 23 Jul 2012 21:44:54 -0300 Message-ID: <20120724004454.GA3184@amt.cnet> References: <5009368802000048000C6E42@novprvoes0310.provo.novell.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: kvm@vger.kernel.org, Zamsden@gmail.com, glommer@redhat.com To: Bruce Rogers Return-path: Received: from mx1.redhat.com ([209.132.183.28]:4341 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754869Ab2GXArP (ORCPT ); Mon, 23 Jul 2012 20:47:15 -0400 Content-Disposition: inline In-Reply-To: <5009368802000048000C6E42@novprvoes0310.provo.novell.com> Sender: kvm-owner@vger.kernel.org List-ID: On Fri, Jul 20, 2012 at 10:44:24AM -0600, Bruce Rogers wrote: > When a guest migrates to a new host, the system time difference from the > previous host is used in the updates to the kvmclock system time visible > to the guest, resulting in a continuation of correct kvmclock based guest > timekeeping. > > The wall clock component of the kvmclock provided time is currently not > updated with this same time offset. Since the Linux guest caches the > wall clock based time, this discrepency is not noticed until the guest is > rebooted. After reboot the guest's time calculations are off. > > This patch adjusts the wall clock by the kvmclock_offset, resulting in > correct guest time after a reboot. > > Cc: Glauber Costa > Cc: Zachary Amsden > Signed-off-by: Bruce Rogers > --- > arch/x86/kvm/x86.c | 4 ++++ > 1 files changed, 4 insertions(+), 0 deletions(-) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index be6d549..14c290d 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -907,6 +907,10 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) > */ > getboottime(&boot); > > + if (kvm->arch.kvmclock_offset) { > + struct timespec ts = ns_to_timespec(kvm->arch.kvmclock_offset); > + boot = timespec_sub(boot, ts); > + } kvmclock_offset is signed (both directions). Must check the sign and use _sub and _add_safe accordingly.