From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paolo Bonzini Subject: Re: [RFC PATCH v3 2/2] add support for Hyper-V partition reference time enlightenment Date: Wed, 11 Dec 2013 13:28:34 +0100 Message-ID: <52A85A72.8000702@redhat.com> References: <1386502419-26614-1-git-send-email-vrozenfe@redhat.com> <1386502419-26614-3-git-send-email-vrozenfe@redhat.com> <52A5D497.1070308@redhat.com> <1386674597.32091.55.camel@localhost> <52A746C6.2020608@redhat.com> <1386759539.7616.28.camel@localhost> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Cc: kvm@vger.kernel.org, mtosatti@redhat.com, pl@dlhnet.de To: Vadim Rozenfeld Return-path: Received: from mx1.redhat.com ([209.132.183.28]:58744 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751048Ab3LKM2l (ORCPT ); Wed, 11 Dec 2013 07:28:41 -0500 In-Reply-To: <1386759539.7616.28.camel@localhost> Sender: kvm-owner@vger.kernel.org List-ID: Il 11/12/2013 11:58, Vadim Rozenfeld ha scritto: >> > + curr_time = (((tsc_ref->tsc_scale >> 32) * native_read_tsc()) >> 32) + >> > + tsc_ref->tsc_offset; >> > + tsc_ref->tsc_offset = kvm->arch.hv_ref_time - curr_time; >> > >> > Why do you need kvm->arch.hv_ref_time at all? Can you just use >> > "get_kernel_ns() + kvm->arch.kvmclock_offset - kvm->arch.hv_ref_count"? >> > Then the same code can set tsc_ref->tsc_offset in both cases. >> > >> > In fact, it's not clear to me what hv_ref_time is for, and how it >> > is different from > OK, let me explain how it works. > Hyper-V allows guest to use invariant TSC provided by host as a time > stamp source (KeQueryPerformanceCounter). Guest is calling rdtsc and > normalizing it to 10MHz frequency, it is why we need "tsc_scale". > "tsc_offset" is needed for migration or pause/resume cycles. > When we pause a VM, we need to save the current vTSC value > ("hv_ref_time"), which is rdtsc * tsc_scale + tsc_offset. > Then, during resume, we need to recalculate the new tsc_scale > as well as the new tsc_offset value. > tsc_offset = old(saved) vTSC - new vTSC In practice "save" means KVM_GET_CLOCK, and "restore" means KVM_SET_CLOCK, right? > So maybe hv_ref_time is not a good name, but we use it > for keeping the old vTSC value, saved before stopping VM. Ok, this was roughly my understanding as well. My understanding is also that (((tsc_ref->tsc_scale >> 32) * native_read_tsc()) >> 32) + tsc_ref->tsc_offset returns exactly the same value as HV_X64_MSR_TIME_REF_COUNT. Thus we do not need kvm->arch.hv_ref_time. We can use the value of HV_X64_MSR_TIME_REF_COUNT, which is "(get_kernel_ns() + kvm->arch.kvmclock_offset - kvm->arch.hv_ref_count) / 100", to compute tsc_offset, like this: curr_time = (((tsc_ref->tsc_scale >> 32) * native_read_tsc()) >> 32); tsc_ref->tsc_offset = get_hv_x64_msr_time_ref_count() - curr_time; This code can be applied always: when the TSC page is initialized and when KVM_SET_CLOCK is called. You do not need to do anything for KVM_GET_CLOCK. Paolo