From nobody Wed Aug 26 13:55:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=infradead.org ARC-Seal: i=1; a=rsa-sha256; t=1783113762; cv=none; d=zohomail.com; s=zohoarc; b=Bcdh6mMjYHUIvue6v9rBgKjnSzFJMOdntyuc3Sr7pwhd9CHnSDQxSBzJAd1TiOjVfdNcG7gGhuERMWDER3YeOO2dcNSRUpaBRq22xE5ZSN63kg6Nx6W/TafLobUSyAKUUIjT+i224qO2EHuSKjwOpzIrC1/IzRHiDZoooPdLNGg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783113762; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=EpzXffKcCC8OCVBj38i2t8eVI2Er97bsxo1QNBYCK8E=; b=h+8dtf5mW+xbbFBWPOCwhPSALV0ss+hvZ0gcCksSQlqdRkC/NGD8i/y8DXVVQBco3Npbx/7mx/jyxcyDmjU8ojOxKf1Xc7Zlj//yVxdh+JLQfMFixclP5L2FvhVKFHTfuNZ95rskUTj1h/JW7ly1PbXgmt2Q1pEp0FRxp5iCCzI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1783113762716198.25361059553506; Fri, 3 Jul 2026 14:22:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1353748.1609438 (Exim 4.92) (envelope-from ) id 1wflKm-000522-Fe; Fri, 03 Jul 2026 21:22:04 +0000 Received: by outflank-mailman (output) from mailman id 1353748.1609438; Fri, 03 Jul 2026 21:22:04 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflKm-00051r-CB; Fri, 03 Jul 2026 21:22:04 +0000 Received: by outflank-mailman (input) for mailman id 1353748; Fri, 03 Jul 2026 21:22:03 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflKk-00051O-DP for xen-devel@lists.xenproject.org; Fri, 03 Jul 2026 21:22:03 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wflKj-001t36-Bn; Fri, 03 Jul 2026 23:22:01 +0200 Received: from [10.42.69.3] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4827ca-bab6-0a2a0a5309dd-0a2a4503bf3c-14 for ; Fri, 03 Jul 2026 23:22:01 +0200 Received: from [90.155.50.34] (helo=casper.infradead.org) by tlsNG-33051d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4827f8-ec1a-0a2a45030019-5a9b322284a8-3 for ; Fri, 03 Jul 2026 23:22:01 +0200 Received: from [2001:8b0:10b:1::425] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.99.1 #2 (Red Hat Linux)) id 1wflKX-0000000AsYQ-3NCh; Fri, 03 Jul 2026 21:21:50 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.99.2 #2 (Red Hat Linux)) id 1wflKX-00000001RQ7-2xkg; Fri, 03 Jul 2026 22:21:49 +0100 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=casper.20170209 header.d=infradead.org header.i="@infradead.org" header.h="Sender:Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To:From" 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:To: From:Reply-To:Cc:Content-ID:Content-Description; bh=EpzXffKcCC8OCVBj38i2t8eVI2Er97bsxo1QNBYCK8E=; b=jIerjpGT+yTwXfADnzFS9Ehvw1 qj5/hvk/X0LarGRgEVv+dsiKFbGxRWGknJH57gqRXscS5FUYAhSqnvtRBTN2Z6vn9teqs3fY7T27K jW8kTa1Wn2iOLEOqSbhIdiHt8Zmudzhq+tZg56Q8n6poP0Su9bZXGRUplcGieKFs0iNapavWjR6fc hzkoY/l9qx1zH8w8EdFXPjFM5inA9cmW6nJSdRy1/qSf00oSXhQLvq8P2NHcd9wzCsCydXn58F6FC AElEi2x7GlGagDUvtuQfZMQtQYs0miIjMnEqFVh2YzqPw/nArOHgfhgvtrehoSUcccIphTfkzHx4t XG8AdZ9w==; From: David Woodhouse To: Paolo Bonzini , Jonathan Corbet , Shuah Khan , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , David Woodhouse , Paul Durrant , Jonathan Cameron , Sascha Bischoff , Marc Zyngier , Joey Gouly , Jack Allister , Dongli Zhang , joe.jin@oracle.com, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, linux-kselftest@vger.kernel.org Subject: [PATCH v6 34/36] KVM: x86: Cache host vclock_mode for masterclock eligibility checks Date: Fri, 3 Jul 2026 22:18:13 +0100 Message-ID: <20260703212145.343527-35-dwmw2@infradead.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260703212145.343527-1-dwmw2@infradead.org> References: <20260703212145.343527-1-dwmw2@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html X-purgate-ID: tlsNG-33051d/1783113721-B4F835D1-4427BB93/0/0 X-purgate-type: clean X-purgate-size: 4113 X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1783113764547158500 From: David Woodhouse The remaining users of pvclock_gtod_data only need to know whether the host clocksource is TSC-based. Cache the vclock_mode value in a simple kvm_host_vclock_mode variable, updated by the pvclock_gtod_notify callback, and use gtod_is_based_on_tsc() for eligibility checks. This is inherently racy (as it always was =E2=80=94 kvm_track_tsc_matching never held the gtod seqcount), relying on eventual consistency: the notifier fires on every timekeeping update and will correct any transient inconsistency within one tick. Signed-off-by: David Woodhouse Assisted-by: Kiro (claude-opus-4.6-1m) --- arch/x86/kvm/x86.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 8d520bfae089..c67655f8e078 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2647,6 +2647,8 @@ static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s= 64 kernel_ns) } =20 #ifdef CONFIG_X86_64 +static int kvm_host_vclock_mode; + static inline bool gtod_is_based_on_tsc(int mode) { return mode =3D=3D VDSO_CLOCKMODE_TSC || mode =3D=3D VDSO_CLOCKMODE_HVCLO= CK; @@ -2676,7 +2678,6 @@ static bool kvm_use_master_clock(struct kvm *kvm) static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, bool update_mclo= ck) { struct kvm_arch *ka =3D &vcpu->kvm->arch; - struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; bool prev_matched_tsc =3D ka->all_vcpus_matched_tsc; =20 /* @@ -2713,7 +2714,7 @@ static void kvm_track_tsc_matching(struct kvm_vcpu *v= cpu, bool update_mclock) * accounts for its offset. */ bool use_master_clock =3D kvm_use_master_clock(vcpu->kvm) && - gtod_is_based_on_tsc(gtod->clock.vclock_mode); + gtod_is_based_on_tsc(kvm_host_vclock_mode); =20 /* * Request a masterclock update if needed: toggling master clock, @@ -2726,11 +2727,11 @@ static void kvm_track_tsc_matching(struct kvm_vcpu = *vcpu, bool update_mclock) =20 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); + ka->use_master_clock, kvm_host_vclock_mode); } #else static inline void kvm_track_tsc_matching(struct kvm_vcpu *vcpu, - bool new_generation) {} + bool update_mclock) {} #endif =20 /* @@ -2850,7 +2851,7 @@ static inline bool kvm_check_tsc_unstable(void) * TSC is marked unstable when we're running on Hyper-V, * 'TSC page' clocksource is good. */ - if (pvclock_gtod_data.clock.vclock_mode =3D=3D VDSO_CLOCKMODE_HVCLOCK) + if (kvm_host_vclock_mode =3D=3D VDSO_CLOCKMODE_HVCLOCK) return false; #endif return check_tsc_unstable(); @@ -3274,7 +3275,6 @@ static void pvclock_update_vm_gtod_copy(struct kvm *k= vm) { #ifdef CONFIG_X86_64 struct kvm_arch *ka =3D &kvm->arch; - int vclock_mode; bool host_tsc_clocksource; =20 lockdep_assert_held(&kvm->arch.tsc_write_lock); @@ -3314,9 +3314,9 @@ static void pvclock_update_vm_gtod_copy(struct kvm *k= vm) ka->use_master_clock =3D false; } =20 - vclock_mode =3D pvclock_gtod_data.clock.vclock_mode; - trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, - ka->all_vcpus_matched_freq); + trace_kvm_update_master_clock(ka->use_master_clock, + kvm_host_vclock_mode, + ka->all_vcpus_matched_freq); #endif } =20 @@ -10399,12 +10399,15 @@ static int pvclock_gtod_notify(struct notifier_bl= ock *nb, unsigned long unused, update_pvclock_gtod(tk); =20 #ifdef CONFIG_X86_64 + kvm_host_vclock_mode =3D + tk->tkr_mono.clock->vdso_clock_mode; + /* * Disable master clock if host does not trust, or does not use, * TSC based clocksource. Delegate queue_work() to irq_work as * this is invoked with tk_core.seq write held. */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode) && + if (!gtod_is_based_on_tsc(kvm_host_vclock_mode) && atomic_read(&kvm_guest_has_master_clock) !=3D 0) irq_work_queue(&pvclock_irq_work); #endif --=20 2.54.0