From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marcelo Tosatti Subject: [PATCH] KVM: x86: fix KVM_SET_CLOCK relative to setting correct clock value Date: Tue, 2 May 2017 18:36:16 -0300 Message-ID: <20170502213616.GA24837@amt.cnet> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: Paolo Bonzini , Radim =?utf-8?B?S3LEjW3DocWZ?= To: kvm-devel Return-path: Received: from mx1.redhat.com ([209.132.183.28]:43092 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750710AbdEBVgq (ORCPT ); Tue, 2 May 2017 17:36:46 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1D05AC00C11E for ; Tue, 2 May 2017 21:36:46 +0000 (UTC) Content-Disposition: inline Sender: kvm-owner@vger.kernel.org List-ID: In the masterclock enabled case, kvmclock_offset must be adjusted so that user_ns.clock = master_kernel_ns + kvmclock_offset (that is, the value set from KVM_SET_CLOCK is the one visible at system_timestamp). This way the guest clock: 1. Starts counting when KVM_SET_CLOCK executes. 2. With the value provided by userspace. Signed-off-by: Marcelo Tosatti --- arch/x86/kvm/x86.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) Index: kvm/arch/x86/kvm/x86.c =================================================================== --- kvm.orig/arch/x86/kvm/x86.c 2017-04-27 17:37:48.131348255 -0300 +++ kvm/arch/x86/kvm/x86.c 2017-04-27 17:56:58.397530444 -0300 @@ -4172,8 +4172,9 @@ break; } case KVM_SET_CLOCK: { - struct kvm_clock_data user_ns; u64 now_ns; + struct kvm_clock_data user_ns; + struct kvm_arch *ka = &kvm->arch; r = -EFAULT; if (copy_from_user(&user_ns, argp, sizeof(user_ns))) @@ -4184,9 +4185,25 @@ goto out; r = 0; - now_ns = get_kvmclock_ns(kvm); - kvm->arch.kvmclock_offset += user_ns.clock - now_ns; + kvm_gen_update_masterclock(kvm); + if (ka->use_master_clock) { + /* + * In the masterclock enabled case, + * kvmclock_offset must be adjusted so that + * user_ns.clock = master_kernel_ns + kvmclock_offset + * (that is, the value set from KVM_SET_CLOCK is the + * one visible at system_timestamp). + */ + kvm->arch.kvmclock_offset = user_ns.clock - + ka->master_kernel_ns; + + kvm_for_each_vcpu(i, vcpu, kvm) + kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); + } else { + now_ns = get_kvmclock_ns(kvm); + kvm->arch.kvmclock_offset += user_ns.clock - now_ns; + } break; } case KVM_GET_CLOCK: {