From patchwork Mon Jun 12 13:23:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Zhoujian (jay)" X-Patchwork-Id: 774623 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wmYXm4hc0z9s3T for ; Mon, 12 Jun 2017 23:24:56 +1000 (AEST) Received: from localhost ([::1]:38054 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKPKv-000214-ER for incoming@patchwork.ozlabs.org; Mon, 12 Jun 2017 09:24:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33032) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKPKa-00020i-GC for qemu-devel@nongnu.org; Mon, 12 Jun 2017 09:24:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dKPKX-000883-Ar for qemu-devel@nongnu.org; Mon, 12 Jun 2017 09:24:32 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:3891) by eggs.gnu.org with esmtps (TLS1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.71) (envelope-from ) id 1dKPKW-00086B-M7 for qemu-devel@nongnu.org; Mon, 12 Jun 2017 09:24:29 -0400 Received: from 172.30.72.54 (EHLO dggeml406-hub.china.huawei.com) ([172.30.72.54]) by dggrg03-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id APG31116; Mon, 12 Jun 2017 21:24:15 +0800 (CST) Received: from localhost (10.177.19.14) by dggeml406-hub.china.huawei.com (10.3.17.50) with Microsoft SMTP Server id 14.3.301.0; Mon, 12 Jun 2017 21:24:05 +0800 From: Jay Zhou To: Date: Mon, 12 Jun 2017 21:23:16 +0800 Message-ID: <1497273796-44720-1-git-send-email-jianjay.zhou@huawei.com> X-Mailer: git-send-email 2.6.1.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.177.19.14] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A0B0201.593E9600.00A4, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 36df14e13bc5e04f3dcecf45980fce6b X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 45.249.212.189 Subject: [Qemu-devel] [PATCH RFC] KVM: X86: save and load PVCLOCK_TSC_UNSTABLE_BIT when migration X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: wangxinxin.wang@huawei.com, arei.gonglei@huawei.com, weidong.huang@huawei.com, qemu-devel@nongnu.org, Jay Zhou Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Guest using kvmclock will be hanged when migrating from unstable tsc host to stable tsc host occasionally. Sometimes, the tsc timestamp saved at the source side will be backward when the guest stopped, and this value is transferred to the destination side. The guest at the destination side thought kvmclock is stable, so the protection mechanism against time going backwards is not used. When the first time vcpu0 enters the guest at the destination side to update the wall clock, the result of pvclock_clocksource_read will be backward occasionally, which results in the wall clock drift. Signed-off-by: Jay Zhou --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/kvm.h | 2 ++ arch/x86/kvm/x86.c | 7 ++++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 695605e..6dd21ff 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -783,6 +783,7 @@ struct kvm_arch { spinlock_t pvclock_gtod_sync_lock; bool use_master_clock; + bool kvmclock_migration_unstable_tsc; u64 master_kernel_ns; u64 master_cycle_now; struct delayed_work kvmclock_update_work; diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index c2824d0..9faed3e 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -360,4 +360,6 @@ struct kvm_sync_regs { #define KVM_X86_QUIRK_LINT0_REENABLED (1 << 0) #define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1) +#define MIGRATION_PVCLOCK_TSC_UNSTABLE_BIT (1 << 0) + #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 87d3cb9..a0f7011 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1920,7 +1920,8 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) /* If the host uses TSC clocksource, then it is stable */ pvclock_flags = 0; - if (use_master_clock) + if (use_master_clock && + !(v->kvm->arch.kvmclock_migration_unstable_tsc)) pvclock_flags |= PVCLOCK_TSC_STABLE_BIT; vcpu->hv_clock.flags = pvclock_flags; @@ -4184,8 +4185,8 @@ long kvm_arch_vm_ioctl(struct file *filp, goto out; r = -EINVAL; - if (user_ns.flags) - goto out; + if (user_ns.flags & MIGRATION_PVCLOCK_TSC_UNSTABLE_BIT) + kvm->arch.kvmclock_migration_unstable_tsc = true; r = 0; now_ns = get_kvmclock_ns(kvm);