From nobody Tue Aug 25 07:33:59 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=DQJ68FjYtxRDMpMsof2CLwz8py5LQzKaHzvW7NcN24+kw9piKImWGkFscaOFmq2y0IZjAFOcrwyvk+i5PyFGMXI0ZhgzQN38kDm4O9iCI3WFaQoy/Ks6y0eqNpndFFCtCVYoODFcDd3TSGObcUzzIIPNDH8BxeG+Yk4xlarAhhg= 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=9YLu6jUxizeyw6FPvXNgLWAHtpjzd7Ja/IQI9k+fk+s=; b=RclrD3tZZdfyfiWXXpjj6BQMtLB3NJ+4c2gqCgCEylduzNtvxtnOOS1mwebiloU8xEma9nerksxL9Wyhv8FX/l+7mdkxdIoXl/2dPHvXmONBFZZDpC4zdNEcxh0G/6C49Lr6eeGBRV02bKtCj5MDFVriMG70bQHTJ3pueAoSY+A= 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 1783113762261380.85631766456004; Fri, 3 Jul 2026 14:22:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1353758.1609507 (Exim 4.92) (envelope-from ) id 1wflKr-0006Wb-43; Fri, 03 Jul 2026 21:22:09 +0000 Received: by outflank-mailman (output) from mailman id 1353758.1609507; Fri, 03 Jul 2026 21:22:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflKq-0006Td-QL; Fri, 03 Jul 2026 21:22:08 +0000 Received: by outflank-mailman (input) for mailman id 1353758; Fri, 03 Jul 2026 21:22:06 +0000 Received: from mx.expurgate.net ([194.145.224.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wflKo-0005hr-LN for xen-devel@lists.xenproject.org; Fri, 03 Jul 2026 21:22:06 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wflKo-00BXCb-1w; Fri, 03 Jul 2026 23:22:06 +0200 Received: from [10.42.69.1] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a48279b-2eae-0a2a0a5409dd-0a2a45019e62-24 for ; Fri, 03 Jul 2026 23:22:01 +0200 Received: from [90.155.50.34] (helo=casper.infradead.org) by tlsNG-d62444.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4827f8-400f-0a2a45010019-5a9b3222a836-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-0000000AsYS-3lGH; 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-00000001RQI-3LPT; 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=9YLu6jUxizeyw6FPvXNgLWAHtpjzd7Ja/IQI9k+fk+s=; b=fcUVvHAIPPdYkE7/dYS8PsNO0M 0l7rKpCfaWyv7qnO627IUm/45JZzCNXpKFxdSx3aOe4X/asoyK1w+W3ZNk66BM8q3BbjYRzGNQLNV FIOdbKNq2+NaKcooJ/SV2aSPahIzidPDXtyB92+iE1701oAJYZIobMAgIC8rg0p2P6qLAa9eY/PVl /pGKXDoXfX1z+5rZmDtJJ9Wv34vMFTG3iFXgK7vu2PgYTbtbFTcJhelUV/ioie3vUIgVLGidMeVnR PeaxJ2Y8zB3aSgCvh1ZPeMDKjka+OYRslMm979N9HKfPZmmsO4HL0U8OnTYlA/VVYGrcVlh/JcdzD AnicB8aQ==; 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 36/36] KVM: x86: Activate master clock from kvm_arch_init_vm() Date: Fri, 3 Jul 2026 22:18:15 +0100 Message-ID: <20260703212145.343527-37-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-d62444/1783113721-D18C31E0-8D48DA2E/0/0 X-purgate-type: clean X-purgate-size: 4143 X-ZohoMail-DKIM: pass (identity @infradead.org) X-ZM-MESSAGEID: 1783113764544158501 From: David Woodhouse Initialize the TSC generation and master clock state at VM creation time, rather than relying on kvm_update_masterclock() from kvm_arch_vcpu_postcreate(). Set cur_tsc_generation=3D1, cur_tsc_khz, cur_tsc_scaling_ratio, cur_tsc_offset, cur_tsc_nsec and kvmclock_offset from an atomic ktime/TSC snapshot. This establishes a coherent epoch so that: - The first vCPU synchronizes to the existing generation (getting the pre-computed TSC offset) rather than computing its own. - pvclock_update_vm_gtod_copy() succeeds immediately (no zero tsc_hz from uninitialized cur_tsc_scaling_ratio). - KVM_GET_CLOCK returns valid {host_tsc, realtime} fields as soon as any vCPU exists. - KVM_[GS]ET_CLOCK_GUEST works without a prior KVM_RUN. - No O(N) iteration of all vCPUs on each vCPU creation. This replaces the kvm_update_masterclock() call that was added to kvm_arch_vcpu_postcreate() to work around the missing initialization. Signed-off-by: David Woodhouse Assisted-by: Kiro (claude-opus-4.6-1m) --- arch/x86/kvm/x86.c | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 58016af64932..d35f2ac3ef49 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13161,8 +13161,6 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) return; vcpu_load(vcpu); kvm_synchronize_tsc(vcpu, NULL); - if (!vcpu->kvm->arch.use_master_clock) - kvm_update_masterclock(vcpu->kvm, NULL); vcpu_put(vcpu); =20 /* poll control enabled by default */ @@ -13572,6 +13570,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long= type) { int ret; unsigned long flags; + u64 kvmclock_host_tsc; + s64 kvmclock_ns; =20 if (!kvm_is_vm_type_supported(type)) return -EINVAL; @@ -13603,18 +13603,47 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned lo= ng type) seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lo= ck); ratelimit_state_init(&kvm->arch.kvmclock_update_rs, HZ, 10); ratelimit_set_flags(&kvm->arch.kvmclock_update_rs, RATELIMIT_MSG_ON_RELEA= SE); - kvm->arch.kvmclock_offset =3D -get_kvmclock_base_ns(); - kvm->arch.all_vcpus_matched_freq =3D true; - - raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); - pvclock_update_vm_gtod_copy(kvm); - raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); =20 kvm->arch.default_tsc_khz =3D max_tsc_khz ? : tsc_khz; kvm->arch.apic_bus_cycle_ns =3D APIC_BUS_CYCLE_NS_DEFAULT; kvm->arch.guest_can_read_msr_platform_info =3D true; kvm->arch.enable_pmu =3D enable_pmu; =20 + kvm->arch.all_vcpus_matched_freq =3D true; + kvm->arch.all_vcpus_matched_tsc =3D true; + kvm->arch.cur_tsc_scaling_ratio =3D kvm_caps.default_tsc_scaling_ratio; + kvm->arch.cur_tsc_khz =3D kvm->arch.default_tsc_khz; + + raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); + + /* + * Establish the initial TSC generation for synchronization logic. + * The first vCPU will sync to this, starting its guest TSC at the + * number of cycles elapsed since VM creation rather than at zero. + * This ensures coherence with the kvmclock epoch (also set here) + * and means the master clock snapshot taken below is immediately + * valid =E2=80=94 no need to redo it at first vCPU creation. + */ +#ifdef CONFIG_X86_64 + if (!kvm_get_time_and_clockread(&kvmclock_ns, &kvmclock_host_tsc)) +#endif + { + /* + * If not simultaneous, we want the TSC to be zero slightly + * *after* the kvmclock is zero. + */ + kvmclock_ns =3D get_kvmclock_base_ns(); + kvmclock_host_tsc =3D rdtsc(); + } + kvm->arch.cur_tsc_write =3D 0; + kvm->arch.cur_tsc_offset =3D -(s64)kvmclock_host_tsc; + kvm->arch.cur_tsc_nsec =3D kvmclock_ns; + kvm->arch.cur_tsc_generation =3D 1; + kvm->arch.kvmclock_offset =3D -kvmclock_ns; + + pvclock_update_vm_gtod_copy(kvm); + raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); + #if IS_ENABLED(CONFIG_HYPERV) spin_lock_init(&kvm->arch.hv_root_tdp_lock); kvm->arch.hv_root_tdp =3D INVALID_PAGE; --=20 2.54.0