From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=90.155.50.34 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793893808; b=KBl/Y18DkCTkzZVN41R6xfL7T0ug/vLK20XmEby9pHh79lPDlS/ugHQylEFU17YsO7yx Ld/6JHssfBwfr2SY5LWBeIuaeKVBRjCBOF5TIDB4q8irODNurwqCMN9Iw+qH8n265JexR ldeRnsxkXI/ceM3NnsoLmTr2SpSCbm6t9cZH5UEMrAgmfXVHuQkheqUnrCrs4DCEDUolp PKmV6PYOdtkR7RsPUEjoCWJ/gexzAdM4Kz7fHVYaWB71cR/Tiy9akCUz3fKBY3Ru6vWNb KFBeYj8LzStAVaeW2iALygcnu4Ho7qStHBhm0neEhDcQUdsjPuBX/chJd6hPKpTj+iw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793893808; h=DMARC-Filter:DKIM-Signature:Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Type:Content-Transfer-Encoding:Sender:X-SRS-Rewrite; bh=Fa5d4hF5b5g+cGGwa12UfyQs3OVoKCl+cYn2uuRhZjo=; b=1RRbk1URagfkk9l1vFtscMkzAB64SwEF5ZjVV84tO5K1T+fdMAXnJof681ogjhP1JGaE pmBtZrdSZpcVMBXEI0K4T21Me+H+/GlBc7BXNz3plAU4B4LoXZrt7XSPdHHJU+Ac7MXJA gEQpWw+uSgl5juiRWyVTkN/F/yF8pXvLUgfZtCQ9YToz2AplrtYiUyljVMxjnYkASxEqG yMtCSeRhSE5II8ciwAW+MG/7Rq45jVSeyVojjhoi9hy3t4FzUd5xQuJxZYqILJtobU5qh DSnsnsuHaqKutIRfeOm4BCNwpTWxVg/LSPqJB6uJkiPQIGkawAiQkAVF7RRqRM6EprQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=YwnXHIWz; arc=none smtp.remote-ip=90.155.50.34 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B505C6075F43; Thu, 5 Nov 2026 15:50:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B505C6075F43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=casper.20170209 header.b=YwnXHIWz DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description; bh=Fa5d4hF5b5g+cGGwa12UfyQs3OVoKCl+cYn2uuRhZjo=; b=YwnXHIWzvBYN9n4rVYggcXWgRc Z5T6VuZ6WDUaeWPsQh9E0B2YNYCK2WY0r4Np1IPStzrOa81QjNkLHVMewQVjM2g7x/zvWSN0m6hoM eP++2oV1qnVdrFZxAtlbOY2oK2mjdbxDEmCQwaCIC3UfqFeIHCukbZ9rQiM6dWsdpFRhVgt2T1Xhd QXjmq8l600NAeFYOf/G+EDpgCyR7jMu49EHj4INvehN4g4vABeQVBFk5NyPnG8s9h/L2GR570Sp1c YqYc+Sh6QKuEaE9Rbts9YSuwqVNXsImVewgSOK076uldTgrPQxg7g9WvZSKeqL8kXryMjpf/JhHkR NEWC7k4g==; Received: from [2001:8b0:10b:1::425] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.100.1 #2 (Red Hat Linux)) id 1xOzit-00000009aoj-3hfm; Thu, 05 Nov 2026 15:49:56 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.99.5 #2 (Red Hat Linux)) id 1xOzit-0000000Aaz2-2pvF; Thu, 05 Nov 2026 15:49:55 +0000 From: David Woodhouse To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, xen-devel@lists.xenproject.org, x86@kernel.org, Paolo Bonzini , Sean Christopherson , Paul Durrant , Vitaly Kuznetsov , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Jonathan Corbet , Shuah Khan , Juergen Gross , Boris Ostrovsky , Jonathan Cameron , Sascha Bischoff , Marc Zyngier , Joey Gouly , Jack Allister , Dongli Zhang , joe.jin@oracle.com, David Woodhouse Subject: [PATCH v11 05/23] KVM: x86: Factor out kvm_use_master_clock() Date: Thu, 5 Nov 2026 07:49:36 -0800 Message-ID: <20261105154955.2525419-6-dwmw2@infradead.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261105154955.2525419-1-dwmw2@infradead.org> References: <20261105154955.2525419-1-dwmw2@infradead.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html From: David Woodhouse Both kvm_track_tsc_matching() and pvclock_update_vm_gtod_copy() make a decision about whether the KVM clock should be in master clock mode. They used *different* criteria for the decision though. This isn't really a problem; it only has the potential to cause unnecessary invocations of KVM_REQ_MASTERCLOCK_UPDATE if the masterclock was disabled due to TSC going backwards, or the guest using the old MSR. But it isn't pretty. Factor the decision out to a single function. And document the historical reason why it's disabled for guests that use the old MSR_KVM_SYSTEM_TIME. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant --- arch/x86/kvm/x86.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 38fcb1d75661..4c3fce29e2af 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1112,11 +1112,30 @@ static inline bool gtod_is_based_on_tsc(int mode) { return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK; } -#endif + +static bool kvm_use_master_clock(struct kvm *kvm) +{ + struct kvm_arch *ka = &kvm->arch; + + /* + * The 'old kvmclock' check is a workaround (from 2015) for a + * SUSE 2.6.16 kernel that didn't boot if the system_time in + * its kvmclock was too far behind the current time. So the + * mode of just setting the reference point and allowing time + * to proceed linearly from there makes it fail to boot. + * Despite that being kind of the *point* of the way the clock + * is exposed to the guest. By coincidence, the offending + * kernels used the old MSR_KVM_SYSTEM_TIME, which was moved + * only because it resided in the wrong number range. So the + * workaround is activated for *all* guests using the old MSR. + */ + return ka->all_vcpus_matched_freq && + !ka->backwards_tsc_observed && + !ka->boot_vcpu_runs_old_kvmclock; +} static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool update_mclock) { -#ifdef CONFIG_X86_64 struct kvm_arch *ka = &vcpu->kvm->arch; struct pvclock_gtod_data *gtod = &pvclock_gtod_data; bool prev_matched_tsc = ka->all_vcpus_matched_tsc; @@ -1162,7 +1181,7 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool update_mclock) * are fine — each vCPU's pvclock has its own tsc_timestamp that * accounts for its offset. */ - bool use_master_clock = ka->all_vcpus_matched_freq && + bool use_master_clock = kvm_use_master_clock(vcpu->kvm) && gtod_is_based_on_tsc(gtod->clock.vclock_mode); /* @@ -1177,8 +1196,11 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool update_mclock) trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, atomic_read(&vcpu->kvm->online_vcpus), ka->use_master_clock, gtod->clock.vclock_mode); -#endif } +#else +static inline void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, + bool update_mclock) {} +#endif /* * Multiply tsc by a fixed point number represented by ratio. @@ -1640,10 +1662,9 @@ static void pvclock_update_vm_gtod_copy(struct kvm *kvm) #ifdef CONFIG_X86_64 struct kvm_arch *ka = &kvm->arch; int vclock_mode; - bool host_tsc_clocksource, vcpus_matched; + bool host_tsc_clocksource; lockdep_assert_held(&kvm->arch.tsc_write_lock); - vcpus_matched = ka->all_vcpus_matched_freq; /* * If the host uses TSC clock, then passthrough TSC as stable @@ -1653,9 +1674,8 @@ static void pvclock_update_vm_gtod_copy(struct kvm *kvm) &ka->master_kernel_ns, &ka->master_cycle_now); - ka->use_master_clock = host_tsc_clocksource && vcpus_matched - && !ka->backwards_tsc_observed - && !ka->boot_vcpu_runs_old_kvmclock; + ka->use_master_clock = host_tsc_clocksource && + kvm_use_master_clock(kvm); if (ka->use_master_clock) { u64 tsc_hz; @@ -1683,7 +1703,7 @@ static void pvclock_update_vm_gtod_copy(struct kvm *kvm) vclock_mode = pvclock_gtod_data.clock.vclock_mode; trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, - vcpus_matched); + ka->all_vcpus_matched_freq); #endif } -- 2.43.0