From patchwork Tue Oct 20 07:22:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haozhong Zhang X-Patchwork-Id: 7442441 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id EC6E5BEEA4 for ; Tue, 20 Oct 2015 07:23:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 100F3205E3 for ; Tue, 20 Oct 2015 07:23:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 08C47205E9 for ; Tue, 20 Oct 2015 07:23:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752762AbbJTHXq (ORCPT ); Tue, 20 Oct 2015 03:23:46 -0400 Received: from mga09.intel.com ([134.134.136.24]:1780 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752284AbbJTHXp (ORCPT ); Tue, 20 Oct 2015 03:23:45 -0400 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP; 20 Oct 2015 00:23:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.17,706,1437462000"; d="scan'208";a="584362102" Received: from hzzhang-optiplex-9020.sh.intel.com (HELO localhost) ([10.239.12.28]) by FMSMGA003.fm.intel.com with ESMTP; 20 Oct 2015 00:23:42 -0700 From: Haozhong Zhang To: qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, "Michael S. Tsirkin" , afaerber@suse.de, Paolo Bonzini , Richard Henderson , Eduardo Habkost , Marcelo Tosatti , Haozhong Zhang Subject: [PATCH v2 3/3] target-i386: load the migrated vcpu's TSC rate Date: Tue, 20 Oct 2015 15:22:54 +0800 Message-Id: <1445325774-7195-4-git-send-email-haozhong.zhang@intel.com> X-Mailer: git-send-email 2.4.8 In-Reply-To: <1445325774-7195-1-git-send-email-haozhong.zhang@intel.com> References: <1445325774-7195-1-git-send-email-haozhong.zhang@intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Set vcpu's TSC rate to the migrated value (if any). If KVM supports TSC scaling, guest programs will observe TSC increasing in the migrated rate other than the host TSC rate. The loading is controlled by a new cpu option 'load-tsc-freq'. If it is present, then the loading will be enabled and the migrated vcpu's TSC rate will override the value specified by the cpu option 'tsc-freq'. Otherwise, the loading will be disabled. The setting of vcpu's TSC rate in this patch duplicates the code in kvm_arch_init_vcpu(), so we remove the latter one. Signed-off-by: Haozhong Zhang --- target-i386/cpu.c | 1 + target-i386/cpu.h | 1 + target-i386/kvm.c | 28 +++++++++++++++++++--------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/target-i386/cpu.c b/target-i386/cpu.c index b6bb457..763ba4b 100644 --- a/target-i386/cpu.c +++ b/target-i386/cpu.c @@ -3144,6 +3144,7 @@ static Property x86_cpu_properties[] = { DEFINE_PROP_BOOL("enforce", X86CPU, enforce_cpuid, false), DEFINE_PROP_BOOL("kvm", X86CPU, expose_kvm, true), DEFINE_PROP_BOOL("save-tsc-freq", X86CPU, env.save_tsc_khz, true), + DEFINE_PROP_BOOL("load-tsc-freq", X86CPU, env.load_tsc_khz, false), DEFINE_PROP_UINT32("level", X86CPU, env.cpuid_level, 0), DEFINE_PROP_UINT32("xlevel", X86CPU, env.cpuid_xlevel, 0), DEFINE_PROP_UINT32("xlevel2", X86CPU, env.cpuid_xlevel2, 0), diff --git a/target-i386/cpu.h b/target-i386/cpu.h index ba1a289..353f5fb 100644 --- a/target-i386/cpu.h +++ b/target-i386/cpu.h @@ -968,6 +968,7 @@ typedef struct CPUX86State { int64_t tsc_khz; int64_t tsc_khz_incoming; bool save_tsc_khz; + bool load_tsc_khz; void *kvm_xsave_buf; uint64_t mcg_cap; diff --git a/target-i386/kvm.c b/target-i386/kvm.c index 698524a..34616f5 100644 --- a/target-i386/kvm.c +++ b/target-i386/kvm.c @@ -743,15 +743,6 @@ int kvm_arch_init_vcpu(CPUState *cs) return r; } - r = kvm_check_extension(cs->kvm_state, KVM_CAP_TSC_CONTROL); - if (r && env->tsc_khz) { - r = kvm_vcpu_ioctl(cs, KVM_SET_TSC_KHZ, env->tsc_khz); - if (r < 0) { - fprintf(stderr, "KVM_SET_TSC_KHZ failed\n"); - return r; - } - } - if (kvm_has_xsave()) { env->kvm_xsave_buf = qemu_memalign(4096, sizeof(struct kvm_xsave)); } @@ -2223,6 +2214,25 @@ static int kvm_setup_tsc_khz(X86CPU *cpu, int level) return 0; /* + * If the cpu option 'load-tsc-freq' is present, the vcpu's TSC rate in the + * migrated state will be used and the overrides the user-specified vcpu's + * TSC rate (if any). + */ + if (runstate_check(RUN_STATE_INMIGRATE) && + env->load_tsc_khz && env->tsc_khz_incoming) { + env->tsc_khz = env->tsc_khz_incoming; + } + + r = kvm_check_extension(cs->kvm_state, KVM_CAP_TSC_CONTROL); + if (r && env->tsc_khz) { + r = kvm_vcpu_ioctl(cs, KVM_SET_TSC_KHZ, env->tsc_khz); + if (r < 0) { + fprintf(stderr, "KVM_SET_TSC_KHZ failed\n"); + return r; + } + } + + /* * Prepare the vcpu's TSC rate (ie. env->tsc_khz_incoming) to be migrated. * 1. If no user-specified value is provided, we will use the value from * KVM;