From patchwork Wed May 3 13:43:43 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 9709839 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B704460351 for ; Wed, 3 May 2017 13:44:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A8DED2839C for ; Wed, 3 May 2017 13:44:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9DC48284D2; Wed, 3 May 2017 13:44:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 437E12839C for ; Wed, 3 May 2017 13:44:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752284AbdECNoM (ORCPT ); Wed, 3 May 2017 09:44:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38974 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752665AbdECNoD (ORCPT ); Wed, 3 May 2017 09:44:03 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C24ABC04B32C for ; Wed, 3 May 2017 13:44:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C24ABC04B32C Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mtosatti@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com C24ABC04B32C Received: from amt.cnet (ovpn-116-20.gru2.redhat.com [10.97.116.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 84DCC5C3FD; Wed, 3 May 2017 13:44:02 +0000 (UTC) Received: from amt.cnet (localhost [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 5D7FC10403A; Wed, 3 May 2017 10:43:47 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.7/8.14.7/Submit) id v43Dhh5F012610; Wed, 3 May 2017 10:43:43 -0300 Date: Wed, 3 May 2017 10:43:43 -0300 From: Marcelo Tosatti To: Paolo Bonzini Cc: kvm-devel , Radim =?utf-8?B?S3LEjW3DocWZ?= Subject: [PATCH v2] KVM: x86: fix KVM_SET_CLOCK relative to setting correct clock value Message-ID: <20170503134341.GB10468@amt.cnet> References: <20170502213616.GA24837@amt.cnet> <2499ef65-1dfe-8460-ec41-661b05cc5023@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <2499ef65-1dfe-8460-ec41-661b05cc5023@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 03 May 2017 13:44:02 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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). Signed-off-by: Marcelo Tosatti --- arch/x86/kvm/x86.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) v2: grab pvclock_gtod_sync_lock spinlock (Paolo) 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-05-03 10:35:19.298447766 -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,31 @@ goto out; r = 0; - now_ns = get_kvmclock_ns(kvm); - kvm->arch.kvmclock_offset += user_ns.clock - now_ns; + kvm_gen_update_masterclock(kvm); + spin_lock(&ka->pvclock_gtod_sync_lock); + if (ka->use_master_clock) { + int i; + struct kvm_vcpu *vcpu; + + /* + * 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; + spin_unlock(&ka->pvclock_gtod_sync_lock); + + kvm_for_each_vcpu(i, vcpu, kvm) + kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); + } else { + spin_unlock(&ka->pvclock_gtod_sync_lock); + now_ns = get_kvmclock_ns(kvm); + kvm->arch.kvmclock_offset += user_ns.clock - now_ns; + } break; } case KVM_GET_CLOCK: {