Received: by 10.223.176.46 with SMTP id f43csp747357wra; Wed, 24 Jan 2018 05:25:59 -0800 (PST) X-Google-Smtp-Source: AH8x227Kbs93gGcfMyJu5JmH56yOe5qi+dpxHJi5xmCfsHLJQPBt53carWiT6TSMODpRiPDIE+Eu X-Received: by 2002:a17:902:8f86:: with SMTP id z6-v6mr8169324plo.352.1516800358937; Wed, 24 Jan 2018 05:25:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516800358; cv=none; d=google.com; s=arc-20160816; b=Q+zOtm+J7cOsz58AM03HOVXE/mELJvY68yF18LXSV4t9lESL62CxqEytIo/qWgVMCn gLcAdk0pC6HhcMAL387U/7TNQqWaRjCo5AlG1hb+PdLXOBpyecCBCsSe53V9fjaNNvi0 Ho6KYv3WSMxsfji2Crd+R9JVI7ro94uWfeQEsRIZckYzWrK5m9rD5HMhBUFt4AI+Be/7 AyBmvJJnBdlFJvaZIrfpTNLcpD1o+Sjwqc8SBDT89orhdteh0hw0WQSgLjw52Uoc0Hvn SP2jJJoEUP7omrT3MaIs5pX+prbGqIqMjIk5W4s+jOEo6ChcEwJAZmfxPIOSQQeWCFoX bdEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=yDJW7ELqdjSRebFQuKAhyHkaHAAlrAzQIqGNGiRKTXc=; b=lOkE3o31InQQQqHPy8v2BLGhkPkMdIZpXiL77MEkO6Uhl901s3FZH/6P3YbLRrZgAC 7qa2br+KB0q/ENfmWFCWgwm9A4jXbvx3pDhwClJ6G/YGReLLB3O3fSi5diiMu2wO8jj3 gfzgEdk7nJjkF1xbjXYaJdC4ngypSgbpCoQdc6oXfTsHeRfZ4DaJdRpMGwARB3SrLncq gV05n021DfKfvFKgY60lZACpy3pOOqRjGBUJMHUkEWLwDyEeMR9ZD2wC+7IdbmKL0wm9 H5uIZzDQbvMgIyIwr4oR6hvJqsz7id88i6hHBmoxW2FBxB0PNCzfJuzXCIj4+byHOqex RKqA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 4-v6si199250plc.812.2018.01.24.05.25.45; Wed, 24 Jan 2018 05:25:58 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933965AbeAXNYd (ORCPT + 99 others); Wed, 24 Jan 2018 08:24:33 -0500 Received: from mx1.redhat.com ([209.132.183.28]:48528 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933685AbeAXNY3 (ORCPT ); Wed, 24 Jan 2018 08:24:29 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 61F3381DF1; Wed, 24 Jan 2018 13:24:29 +0000 (UTC) Received: from vitty.brq.redhat.com (unknown [10.43.2.155]) by smtp.corp.redhat.com (Postfix) with ESMTP id B59146562D; Wed, 24 Jan 2018 13:24:23 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org, x86@kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , "Michael Kelley (EOSG)" , Andy Lutomirski , Mohammed Gamal , Cathy Avery , Roman Kagan , linux-kernel@vger.kernel.org, devel@linuxdriverproject.org Subject: [PATCH v4 7/7] x86/kvm: support Hyper-V reenlightenment Date: Wed, 24 Jan 2018 14:23:37 +0100 Message-Id: <20180124132337.30138-8-vkuznets@redhat.com> In-Reply-To: <20180124132337.30138-1-vkuznets@redhat.com> References: <20180124132337.30138-1-vkuznets@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 24 Jan 2018 13:24:29 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When we run nested KVM on Hyper-V guests we need to update masterclocks for all guests when L1 migrates to a host with different TSC frequency. Implement the procedure in the following way: - Pause all guests. - Tell our host (Hyper-V) to stop emulating TSC accesses. - Update our gtod copy, recompute clocks. - Unpause all guests. This is somewhat similar to cpufreq but we have two important differences: we can only disable TSC emulation globally (on all CPUs) and we don't know the new TSC frequency until we turn the emulation off so we can't 'prepare' ourselves to the event. Signed-off-by: Vitaly Kuznetsov Acked-by: Paolo Bonzini --- v3 -> v4: add static to kvm_hyperv_tsc_notifier() [kbuild robot] --- arch/x86/kvm/x86.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b1ce368a07af..879a99987401 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -68,6 +68,7 @@ #include #include #include +#include #define CREATE_TRACE_POINTS #include "trace.h" @@ -5932,6 +5933,43 @@ static void tsc_khz_changed(void *data) __this_cpu_write(cpu_tsc_khz, khz); } +static void kvm_hyperv_tsc_notifier(void) +{ +#ifdef CONFIG_X86_64 + struct kvm *kvm; + struct kvm_vcpu *vcpu; + int cpu; + + spin_lock(&kvm_lock); + list_for_each_entry(kvm, &vm_list, vm_list) + kvm_make_mclock_inprogress_request(kvm); + + hyperv_stop_tsc_emulation(); + + /* TSC frequency always matches when on Hyper-V */ + for_each_present_cpu(cpu) + per_cpu(cpu_tsc_khz, cpu) = tsc_khz; + kvm_max_guest_tsc_khz = tsc_khz; + + list_for_each_entry(kvm, &vm_list, vm_list) { + struct kvm_arch *ka = &kvm->arch; + + spin_lock(&ka->pvclock_gtod_sync_lock); + + pvclock_update_vm_gtod_copy(kvm); + + kvm_for_each_vcpu(cpu, vcpu, kvm) + kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); + + kvm_for_each_vcpu(cpu, vcpu, kvm) + kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); + + spin_unlock(&ka->pvclock_gtod_sync_lock); + } + spin_unlock(&kvm_lock); +#endif +} + static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data) { @@ -6217,6 +6255,9 @@ int kvm_arch_init(void *opaque) kvm_lapic_init(); #ifdef CONFIG_X86_64 pvclock_gtod_register_notifier(&pvclock_gtod_notifier); + + if (x86_hyper_type == X86_HYPER_MS_HYPERV) + set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); #endif return 0; @@ -6229,6 +6270,10 @@ int kvm_arch_init(void *opaque) void kvm_arch_exit(void) { +#ifdef CONFIG_X86_64 + if (x86_hyper_type == X86_HYPER_MS_HYPERV) + clear_hv_tscchange_cb(); +#endif kvm_lapic_exit(); perf_unregister_guest_info_callbacks(&kvm_guest_cbs); -- 2.14.3