From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF1072E764D for ; Wed, 26 Aug 2026 21:33:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779988; cv=none; b=doG2ku2ZFLj/7CmGV4KzDefmSmxWRH/oe24IrMgPvaDcGrpZ3C25PYj/5y2+lId5kSAgXqt77UPxplR46v2gAkRxryDLkZMNueH33VGgA2BkoAZJaccprJ9kWoh3XE08rRodA1KiAEMG9hY7MWmGG68xhoKgCuI5DzXNFpF17aM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779988; c=relaxed/simple; bh=S0hjrjeSHb2kJaIjU0orALqDXIbu+1GkVZ/75jX0FMA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fh8+/oLYTG3Q+LHHi3OvB5NPtIzWe8j7Mt6P/4MPntk4Yr7SD7SF0A2Icqhfb4q98OwBEcdweB6ixhRSWlGQNScGoMsMYTBksheAxriZwp6iUMs8Cs6duuBVZWHKpLksVJuzTTMemP1b5r/3VblW7ZxBSzs0zqE+0mEsp4wYKQY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=inYZDupj; arc=none smtp.client-ip=209.85.215.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="inYZDupj" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cc1c057f480so1416994a12.0 for ; Wed, 26 Aug 2026 14:33:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779986; x=1788384786; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=HX64g1e+YNnFRPwyjdC5E39dn1LmH0dsFZuuMBpV62s=; b=inYZDupjGqThKNJ09IuN+TrH+CfUgKhVPvWImqTNZG2AZe5ZOak2MkTZpVCx6xUKXq Lcgf9y56Z0xxn3rsVsE3NQ5c/pfA77Ar1PJCGco5+RV/TDfQb3BJITllckutBcZTbo7m vwlIDN66/Z8QCzx1BZYUiocLyL3jptulCqRDOF2cfuDi8D9h70UxB48Yu+exoeUeJYr2 o+Kj0lCz5egN+gAVR2xqa/CBQxfLM+VAEGzkHJLlZPTJi4mEvVmcQLtFsCYUUdWdEXCl WNaxgtBnN5mBephnx95kz8V+TO+s1ft8QhQnJrEQsY86deYinKUxRB8TgRkZIuH2Q1wr kOZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779986; x=1788384786; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=HX64g1e+YNnFRPwyjdC5E39dn1LmH0dsFZuuMBpV62s=; b=epYlNx6NNy7sd8xQqxshrKWYkegZlr0sfXT3EhWDrhFtGEja96ksCpjprpvruSOfI8 dc33wwOVLHWNk7aJ8i8CMiozHTSWRDu5+ewkHbViOzVterpOzuKlYWD4Pa91qRfCReDU 9anIRjY/+rABjbhIYaOBk15xCsk25lwN+1hwirycCHdSlf5s7fLZgRm2ZE48D6ZDzGoE VD77MYUCSbSdkorVgUGHR2Ob5XJhu9kkaVveaVDRsZ5Ucep/FSiD4P6xQ9oZu6uN4WlY Pk4JwpYNyOaIfe/mqoevhx24XItWh6lGdFj3NRj0MVBI8y01TVAMMPtKfaguc+jAbVxv kjgQ== X-Forwarded-Encrypted: i=1; AHgh+RqE2CcjbNWXuMyXeTUNUe0SRTTUXERPtm/lloTD0ldNuJ9vH2BglHWWYl7lMM5Ka/wTiBLOPb+cvEMveUQ=@vger.kernel.org X-Gm-Message-State: AFuF++mG5ylurkMRAa2ASn8iJR5a5X26jx8TMefgkb9KU72OtUkJa+AR ZtZdQ5H6pT+fyA5Ga4y+V4T+sc1Tmbv1QthxqXhA8zIS57yn/m8C6WwQphbKlhdt8UEoK+NmiSE u7uaJUQ== X-Received: from pgak6.prod.google.com ([2002:a05:6a02:6746:b0:cbe:9e80:c394]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:430b:b0:3c3:6928:8b25 with SMTP id adf61e73a8af0-3cf84e51a6fmr16266744637.17.1787779985732; Wed, 26 Aug 2026 14:33:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:41 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-2-seanjc@google.com> Subject: [PATCH v10 01/21] KVM: x86: Update "last guest TSC" snapshot prior to enabling IRQs/preemption From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When refreshing the last observed guest TSC during a guest time update, write the snapshot before enabling IRQs, i.e. before enabling preemption. If the task is migrated between updating the local tsc_timestamp, e.g. to account for catch-up mode, and setting last_guest_tsc, kvm_arch_vcpu_load() would set the vCPU's TSC offset using the old last_guest_tsc. In practice, the bug is largely benign as it's not even strictly necessary for KVM to refresh last_guest_tsc when updating guest time, as KVM's goal is purely to prevent the guest from observing time jump backwards, i.e. super duper strictly speaking, KVM only *needs* to update last_guest_tsc in the VM-Exit path. In fact, the update kvm_guest_time_update() in wasn't even added to play nice with kvm_arch_vcpu_load(), it was added by commit 28e4639adf0c ("KVM: x86: Fix kvmclock bug") to fix code that no longer exists. As of commit 28e4639adf0c, kvm_guest_time_update() also consumed last_guest_tsc, to try and prevent guest time from jumping backwards. That code was eventually removed by commit f25e656d31ad ("KVM: x86: fix tsc catchup issue with tsc scaling"), but the last_guest_tsc update hung around. Keep the update even though it's technically ok to drop the update, e.g. so that the tsc_catchup updates aren't lost, and so that the guest won't see a PV clock timestamp that appears to be in the future. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4b3681796c75..10f11e06d117 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1820,6 +1820,12 @@ int kvm_guest_time_update(struct kvm_vcpu *v) } } =20 + /* + * Refresh L1's last "observed" TSC to match the PV clock's timestamp, + * e.g. so that the guest can't see a TSC that's behind the reference. + */ + vcpu->last_guest_tsc =3D tsc_timestamp; + local_irq_restore(flags); =20 /* With all the info we got, fill in the values */ @@ -1841,7 +1847,6 @@ int kvm_guest_time_update(struct kvm_vcpu *v) hv_clock.tsc_to_system_mul =3D vcpu->pvclock_tsc_mul; hv_clock.tsc_timestamp =3D tsc_timestamp; hv_clock.system_time =3D kernel_ns + v->kvm->arch.kvmclock_offset; - vcpu->last_guest_tsc =3D tsc_timestamp; =20 /* If the host uses TSC clocksource, then it is stable */ hv_clock.flags =3D 0; --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5DC348987C for ; Wed, 26 Aug 2026 21:33:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779992; cv=none; b=W3WH4OVFpQ+cbHnU0h9+p2fD+Rr0vPo/W/YUmgdM/Ot5o6xzDWjSbaLyQGK5fRGTyw7yDZdT4uox7GRTTYD4jZ6dfpCOBgD9yX1VHdLsJb5mpywWjOsKkIhyCLInoqD9wvWctYLK/79ZG8DUopSEyf7Nq/xaIHNW+ndFbE2KlaU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779992; c=relaxed/simple; bh=oyP3Jx5TwpM7lfELbNHOfyWPzvuct03eZ43EbbHjfho=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Wdka1sxYX65L7vefE0KjNSYrQkExVw0SZvxwurtQXmkB4baXTg8LIIXIIDhsOIwiz1rpLIQ6nLRcg/LhcRgABxyLJvj3ufmTjz4t0ykXlwVrQyooWl9mOOe7NDNZRUIIn3OrS5j6HlAtP/AYDTrMqh49RZkuXN9FvZlPN9Gx524= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LhwBOzJQ; arc=none smtp.client-ip=209.85.215.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LhwBOzJQ" Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cc1c62f0d60so1710071a12.1 for ; Wed, 26 Aug 2026 14:33:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779987; x=1788384787; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=SS97OA+o28099dnvzUD8Vv8IMBV5k83CG9rDEZqRG/8=; b=LhwBOzJQGYv359CevPYPq1TEGh8vUbBvQ7eg72os7QFgGLY7RHUd/U1zh2NBDS3Zzt OEDXbQdEDpAgsIzJK/Mmk4z0s8nr+4YUsCLsBK1fbyX2ejU+g4oVkqowUOTdowi5EkRH P57h82UbFDYsit0rm+eghfCRgnVff8Op67+WYOyXyVEK/doa/ycu8x1hGQbyVYwsSnIY toxeQM9I68KlKbjsRReUiN8NQsExheP6GzZtuhhxJgCFYJg+l71xzURH56JHetXLsUF8 IGjrw4I68DuXt5eEzabx6f+Xr2u1/dG9TYXTu9XnYp2PK/cDf5D9VJKrdy0axJUeB9D1 Z8kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779987; x=1788384787; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=SS97OA+o28099dnvzUD8Vv8IMBV5k83CG9rDEZqRG/8=; b=GUmkJ3vnMqOlNjdmd1qWC3zFNG4G3/IDvfNMQMeYOaCiNcAaMnzoJsqGkUEt/A0HSM /Yov3yDCCbId6jBhFAMmx54gKJQtZiVSPPan5fVPT4qdfbkLT/NwzXeUxqvHzaZqul1i FVk+1Ro2fctR8u+wozNYs2tvHKelE3Eg2WXuvpl7FHAln+7doXyd0Df1oYggPNhpUwOb rY3bCkOGwwomJwNJHd0PvNxUwC3Z2o0Q53hUM5CGKHEBBEr2rUmWkNy+L7vndnSbdduQ aiqTTHfoo7SPH6xSkTk/J008y70n07bfQJgn8mdANcxZYFs54QsOtrSXNCTknPYCbYeM j9PA== X-Forwarded-Encrypted: i=1; AHgh+Row/MFXmwFGaLM30BBKPQYdwiOeRsWAWITrpgfHKiubtCGMSfpELR9Ty6D7/+os8IkvRRQl2NPYvykuclw=@vger.kernel.org X-Gm-Message-State: AFuF++l+og2xmAE1cM/o+e2vQwLXpUASd+Z6YLt+oiV3mkoiCeaxBlVm ndZijR8YMsozpucgwEVt97RvgXwNJzf11viNLGd+yhiBx9LPr33fg5gan9pj++vlFlKDucYrNrh afCq1Fg== X-Received: from pgal4.prod.google.com ([2002:a63:3e04:0:b0:cc1:be9e:35ef]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:a95:b0:3c1:fbf:1e2e with SMTP id adf61e73a8af0-3cf83b22768mr21020948637.10.1787779986926; Wed, 26 Aug 2026 14:33:06 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:42 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-3-seanjc@google.com> Subject: [PATCH v10 02/21] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse The kvm_guest_time_update() function scales the host TSC frequency to the guest's using kvm_scale_tsc() and the v->arch.l1_tsc_scaling_ratio scaling ratio previously calculated for that vCPU. Then calculates the scaling factors for the KVM clock itself based on that guest TSC frequency. However, it uses kHz as the unit when scaling, and then multiplies by 1000 only at the end. With a host TSC frequency of 3000MHz and a guest set to 2500MHz, the result of kvm_scale_tsc() will actually come out at 2,499,999kHz. So the KVM clock advertised to the guest is based on a frequency of 2,499,999,000 Hz. By using Hz as the unit from the beginning, the KVM clock would be based on a more accurate frequency of 2,499,999,999 Hz in this example. Use u64 for the hw_tsc_hz field since an unsigned int would overflow for TSC frequencies above 4GHz. Fixes: 78db6a503796 ("KVM: x86: rewrite handling of scaled TSC for kvmclock= ") Reviewed-by: Paul Durrant Signed-off-by: David Woodhouse Tested-by: Dongli Zhang Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/x86.c | 18 ++++++++++-------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 683bb8bf43a9..0255ca6b24c2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -864,7 +864,7 @@ struct kvm_vcpu_arch { gpa_t time; s8 pvclock_tsc_shift; u32 pvclock_tsc_mul; - unsigned int hw_tsc_khz; + u64 hw_tsc_hz; struct gfn_to_pfn_cache pv_time; /* set guest stopped flag in pvclock flags field */ bool pvclock_set_guest_stopped_request; diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index ddb022cb203a..ca26be88c204 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -16,6 +16,7 @@ #include #include #include +#include =20 #include #include diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 10f11e06d117..e18158f51103 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -61,6 +61,7 @@ #include #include #include +#include =20 #include #include @@ -1763,7 +1764,8 @@ static void kvm_setup_guest_pvclock(struct pvclock_vc= pu_time_info *ref_hv_clock, int kvm_guest_time_update(struct kvm_vcpu *v) { struct pvclock_vcpu_time_info hv_clock =3D {}; - unsigned long flags, tgt_tsc_khz; + unsigned long flags; + u64 tgt_tsc_hz; unsigned seq; struct kvm_vcpu_arch *vcpu =3D &v->arch; struct kvm_arch *ka =3D &v->kvm->arch; @@ -1789,8 +1791,8 @@ int kvm_guest_time_update(struct kvm_vcpu *v) =20 /* Keep irq disabled to prevent changes to the clock */ local_irq_save(flags); - tgt_tsc_khz =3D get_cpu_tsc_khz(); - if (unlikely(tgt_tsc_khz =3D=3D 0)) { + tgt_tsc_hz =3D (u64)get_cpu_tsc_khz() * HZ_PER_KHZ; + if (unlikely(tgt_tsc_hz =3D=3D 0)) { local_irq_restore(flags); kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); return 1; @@ -1831,16 +1833,16 @@ int kvm_guest_time_update(struct kvm_vcpu *v) /* With all the info we got, fill in the values */ =20 if (kvm_caps.has_tsc_control) { - tgt_tsc_khz =3D kvm_scale_tsc(tgt_tsc_khz, + tgt_tsc_hz =3D kvm_scale_tsc(tgt_tsc_hz, v->arch.l1_tsc_scaling_ratio); - tgt_tsc_khz =3D tgt_tsc_khz ? : 1; + tgt_tsc_hz =3D tgt_tsc_hz ? : 1; } =20 - if (unlikely(vcpu->hw_tsc_khz !=3D tgt_tsc_khz)) { - kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, + if (unlikely(vcpu->hw_tsc_hz !=3D tgt_tsc_hz)) { + kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_hz, &vcpu->pvclock_tsc_shift, &vcpu->pvclock_tsc_mul); - vcpu->hw_tsc_khz =3D tgt_tsc_khz; + vcpu->hw_tsc_hz =3D tgt_tsc_hz; } =20 hv_clock.tsc_shift =3D vcpu->pvclock_tsc_shift; --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F36B2348C77 for ; Wed, 26 Aug 2026 21:33:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779994; cv=none; b=Uj7kaV7kjSPu656LmfXeyTOdtYplZd3ltMkWu5CT9RTHqW4Ck9Pegm7VDLethjG5OaFpTKnPGiyLRHR3Qmj2lbpzzJJdEXj6MYSuICunqvR47OPy9LMCbjf3xSFBj2R2egxlQQk7nAzFszi85dQ2i4uM3DShvY3W592QEJK1olo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779994; c=relaxed/simple; bh=WU8n/WXdbmjjpvxRKRTA/33ZIHDgdl7j/qh5wCMV59M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AguJ5cxmWx1qV9rXH8gl3qrdy41SDGS6RHosFDceAzggEclcixCJniBZOCYzTFN79P0MZBsf1Jo4NGIQkbsoIpS/Xv0sbOrsgR8zr+1CYjYUGg8hz25pG5XjKjA188RbczPqKwcOVvBdetLpsaozZASJyOUQrfF5AZgrBuSR2Y8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Fk1f7VEp; arc=none smtp.client-ip=209.85.215.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Fk1f7VEp" Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cc1c5810451so1549442a12.1 for ; Wed, 26 Aug 2026 14:33:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779988; x=1788384788; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=Rkm9yzIH6549diSepTNkZ7In6/mcXkjfxBNnDP6J8CU=; b=Fk1f7VEpYDmcmByywCX9ASHrt/GT4dqej22YAz8ZexLcj8+OJRhw3+Zmx3NzpJKZ3K jqsG08kLIB3nqrYNdkTT5yCQxfvcA24nHov4s3nkaUyrscL3fjJ77zlnoA5QSycAbEK7 z0Gf+NaVL7yk5oqNFrpXol8kqQJYtOfeJdk7QJUdeK1WveJGu88kYJoJyKeeLBZsb4ll wiE8647Ilclfi52FoIZi0cRSOv7uulWCCd9SoIsG1Ge32u/DNMI0LTGWWh91diIj53+t o3N6mabLzf8yfskF2s3bJgQf7qTkNhgj7UDNsZ81v0Gpk+zTP4kbPjsKgD+kTMURSGYp EL7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779988; x=1788384788; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=Rkm9yzIH6549diSepTNkZ7In6/mcXkjfxBNnDP6J8CU=; b=br5M5dHuca27KM4k1PmjgE5EKYuqmDZjmx2uf/e0gcTuz+0E0DdHXU0a+VvgpxcflE 6/UEpLG8qRs49AMYYBuD20SAVqBlxwKvT0f5kddYQvRGAIAXQPVEOp1FaWboz3fYauhl Ym7Sw2L0/Wip60fvr25MonA90dMZEcgu1G4iUVqIay1Vb+uGFaQYn0Ul57nd4IRJDq+T JxfV41XKicAlXoPdtA2irIRFCyacA/ONkrJdXrLVpidaQlXohfB2lZ6wl5fK0HMspyL5 LqxomQWpXkzwrCvJwJZHAAtsUSxlOtzFKiFx3JGad1K49g8KGY+Aqn2WeLGR47UI0Bje VLXg== X-Forwarded-Encrypted: i=1; AHgh+Rqpz2VguAYKlW76fqcjeWxp85VGdiON4lxDr47BeisgqJEBsQEuWB/Pi/Nx8ua03Lgk/oY9HsrKJ8Lhg2A=@vger.kernel.org X-Gm-Message-State: AFuF++khBXujRNViyaWRhoSffDUAQlyXlvQBG/K1LMHIuV04eftJYir3 uykowUjm2lHEwNDicXOz/o0kAX8+i7RsVt2ELkY/hino+gECKoOC1k+xsFOeh2y5KJSLhLUpUXs U9BCXrw== X-Received: from pfbhk5.prod.google.com ([2002:a05:6a00:8785:b0:84a:251a:a85e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2999:b0:852:3726:dfde with SMTP id d2e1a72fcca58-85373fb96b7mr19697388b3a.9.1787779988022; Wed, 26 Aug 2026 14:33:08 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:43 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-4-seanjc@google.com> Subject: [PATCH v10 03/21] KVM: x86: Explicitly disable TSC scaling without CONSTANT_TSC From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse KVM does make an attempt to cope with non-constant TSC, and has notifiers to handle host TSC frequency changes. However, it *only* adjusts the KVM clock, and doesn't adjust TSC frequency scaling when the host changes. This is presumably because non-constant TSCs were fixed in hardware long before TSC scaling was implemented, so there should never be real CPUs which have TSC scaling but *not* CONSTANT_TSC. Such a combination could potentially happen in some odd L1 nesting environment, but it isn't worth trying to support it. Just make the dependency explicit. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/vmx/vmx.c | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index ea647938a2a6..e2249408cfac 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5620,7 +5620,8 @@ static __init int svm_hardware_setup(void) XFEATURE_MASK_BNDCSR); =20 if (tsc_scaling) { - if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { + if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR) || + !boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { tsc_scaling =3D false; } else { pr_info("TSC scaling supported\n"); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 504630f0eb40..ad482e44b87b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2841,6 +2841,16 @@ static int setup_vmcs_config(struct vmcs_config *vmc= s_conf, if (!cpu_has_sgx()) _cpu_based_2nd_exec_control &=3D ~SECONDARY_EXEC_ENCLS_EXITING; =20 + /* + * KVM doesn't re-derive the TSC scaling ratio when the host TSC + * frequency changes, so TSC scaling is only usable with a constant + * TSC. Clear the control here rather than in vmx_hardware_setup() so + * that the per-CPU configs recomputed by vmx_check_processor_compat() + * stay consistent with the golden vmcs_config. + */ + if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) + _cpu_based_2nd_exec_control &=3D ~SECONDARY_EXEC_TSC_SCALING; + if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_TERTIARY_CONTROLS) _cpu_based_3rd_exec_control =3D adjust_vmx_controls64(KVM_OPTIONAL_VMX_TERTIARY_VM_EXEC_CONTROL, --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F0AB48551A for ; Wed, 26 Aug 2026 21:33:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779994; cv=none; b=hyeLqzPd31KbmO46svOzzrqZ1sxntfteQ3qNnHNw51er5JLUABRvekWoWXeYUZMAAcXqy56rH91fimlyrJBtMO2MBRYyO6Cc/D+B1xVWGmvy38coFmIvhxsax1EOCk5UW8L4rfw+vHw/l6Aqc20rk7pYVsL1GLfJok9ZnCVtKmA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779994; c=relaxed/simple; bh=KAoJX+w+XmJqc4fTFJNWN+O9Ca9qWU4Lqdvsuqf+0RM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=U4AvoArjQ3cDcH7BXbkbnocmRa+KeyDDFK58GNjVErKSxwioIzKohVVt6l2hydAEuST8ktOjOn0pwYBfpg5unkuaL36fXsKn4BKqXckev11jGxo3BytTKs5PilTLQRVjuTiAvB2KQd+WIi4dEvkzF1T7ISK57H5AJihK6INPuSI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=NYtiMiUm; arc=none smtp.client-ip=209.85.210.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="NYtiMiUm" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-84a67b16217so2281991b3a.3 for ; Wed, 26 Aug 2026 14:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779989; x=1788384789; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=91h2s+bdxLI8Fb+8IZGu8PwUcENxYkecSoLgiBQY74U=; b=NYtiMiUmudWyTLp8skkR+9KlrIGHJ5oLHFEYEEKCZZmT2F3mdXry9io0sh1PqUVQyY UbwQzCYz655VGCbTHhWJh91h4k6fbsBYnL7gpShFpcGSoiIsBP3cx/8qBvP8Eh/0OnZL ee5dwwNsji5AI6E2ngHC+0JJi+8un6IZxG3H2bV33jSgWsSi5IntiRCbmZWGct2G8R77 YTVZW1pshlb+OjFp8wKfyAhqU5UovfOyZOoWfPDaMjGxVx/VgfUOzve5mpTCC2z/jcPu W7TWCsIvJ6zZ+/QZU/tMtErBLtqXgFIZokBgM5SA6JjQRl2SqHABxPQ9x5hD4GoU6b3l F3zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779989; x=1788384789; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=91h2s+bdxLI8Fb+8IZGu8PwUcENxYkecSoLgiBQY74U=; b=HUe3p8WQ+3iWHeJ42Jmpb5HlGws76rhmc3ioOtdZG9wAKsqGkcfdUpgmeaRhj2FJhi pL6OFY+7lmmNSxEbQs5xFUTlhN1IcB7iUjuZ+l2soL8JBmSMRJuMkGcR0qr4Q2E5mAds nuPR+ZGLRaSi+aJqK3JG5Qe1dqjlaxmJE1Pzgtuc1nWlyNp4SqMM8ubROnlpky0dA3DZ 1TdZTuBLTKG7eS8z3f84ezZ3QzERLDbozR5iF4MMVQrA4g0Ppy97CxA/53itb6ScNfFd K/T3sYpu2ZvLenOmQ36yEfozwQ3jlghpEyI946YqGKmEQBVKsL6jqpYg5/khwQL/u1ra GYZg== X-Forwarded-Encrypted: i=1; AHgh+RorCtKTdsoev0B8f9zBuQFVMRPQs6V+yi2k6tV25Hug4zADXpclijirpTa2XeXZtwmlpod9hkNXID+OuiE=@vger.kernel.org X-Gm-Message-State: AFuF++mtYWDzL5NJGHT6QWPTT4vYmdDJyF5q2gtVXPWi8DxPgpoqxYyE vSR+gsack2u1bCm1XUd76zfpycHHQVEaeuqPParI4+kW1iErLd4yb/RvXrm4fdi0GzVwOqhXQZd /DizuBg== X-Received: from pgby7-n2.prod.google.com ([2002:a05:6a02:6507:20b0:cbe:9dc7:9ee8]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4ac6:b0:845:e9e8:645c with SMTP id d2e1a72fcca58-85372ba1874mr19864472b3a.6.1787779989166; Wed, 26 Aug 2026 14:33:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:44 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-5-seanjc@google.com> Subject: [PATCH v10 04/21] KVM: x86: Activate master clock immediately on vCPU creation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Previously, the master clock was only activated when the first vCPU processed KVM_REQ_MASTERCLOCK_UPDATE during KVM_RUN. This meant that KVM_GET_CLOCK could not return the host_tsc field until after the first KVM_RUN, making it impossible for userspace to follow the documented TSC migration procedure without a dummy vCPU run. Fix this by calling kvm_update_masterclock() directly from kvm_arch_vcpu_postcreate(), after kvm_synchronize_tsc() has already set all_vcpus_matched_freq. This ensures the master clock is active immediately, and KVM_GET_CLOCK returns a valid {host_tsc, realtime} pair as soon as a vCPU exists. Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e18158f51103..4ab0a41eee7b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9447,6 +9447,8 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) return; vcpu_load(vcpu); kvm_synchronize_tsc(vcpu, NULL); + if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu)) + kvm_update_masterclock(vcpu->kvm); vcpu_put(vcpu); =20 /* poll control enabled by default */ --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pl1-f197.google.com (mail-pl1-f197.google.com [209.85.214.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0EFE43B14B3 for ; Wed, 26 Aug 2026 21:33:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779994; cv=none; b=pXjsQH3i8WpfeRhgLiQwWMnAoX9XSLFqQVsXj4QiJigQybiW0WMAVNblIBtWePCDojMd8F1uTxWJ2wcM9czp657A6Vc1TthIfWvJAaYDiudLy4T1Ym7SVCPImMOfTmp4H81RzZ6mpjQXBo1ZatW9xdRBSRYQrrwmnrr/PlT1maM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779994; c=relaxed/simple; bh=s/He0eEBW5Ev317ZMBHjcSg4n8ESITAF3VdnBqEq3iI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RLXqpbLTxZ/UVa/pad0wlLtNc3Cti3MnItIR6ijmV08txhWeAytJEHErzphCf7JL/rbRan7OzfgtU34W3aoubrf3zykDYfqrc5FTP/LeTo4D+tOCz3tpCEg0C0o+LVKfX6VTN2MUkGWX0URV/lE8niVaDDCdlC7Oc+SsxrOdSIQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QzlfsQaZ; arc=none smtp.client-ip=209.85.214.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QzlfsQaZ" Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2cfc52ddc55so21102545ad.3 for ; Wed, 26 Aug 2026 14:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779990; x=1788384790; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=WzUUpK1X/Ek66AY89fN993ev5uItccqjWdSjYLSij3I=; b=QzlfsQaZnhz1FpmUlOTl6zz4HjG/VZi/Z5nLrMMO/XfVuWec0TMcMSu6LI9WlaTqsv 4BHprFXGgQCeRb57xRCL71cDPDEATMT2vUnT9I8CfURvzJsv1bhBlzMhLUG7QtpBQE+w yBjN3Z7KiBaB/9sA8pr8cf0Lly+YUgF/Y1d04xVIu5GlkSqfXv3zDQxl3vzH6EXaMCye tfE7wdf/s8D2DDDVGRIXKnrUJ8B1TWaywmPAnYlmJFi55bROVSmrCpGBTQZqnUHcMwQi hkT9H0mnkCzbPsDgUwf39GdVXt+o/evrAXz09PRlIGZO1Xv7vpT8RJuPcJ/2gs6SUlP+ 4GWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779990; x=1788384790; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=WzUUpK1X/Ek66AY89fN993ev5uItccqjWdSjYLSij3I=; b=M+xSVcnp74bTjCWM9XQu4yTU7cZUGb27aoCCfXQu2gwTFuvccGVEupaTOzssWKWRAc NbzfIVHs2Vc7DCkC2EovnmmrB9OSsK3CnQHVkCgLrNBFuE7AblunjAo22LIawLMfLCgf LjByfDwObDRsO+aeOQgaJZzpRnSfHctGLmJpo3/MAsDGenBDyrPBiBjKqlZbnZrV6DXj dObt1rh9A4DvBVeKY8Vg8BMQz2Dj+XkE9rCOZ5WKJyixj9uLmHjd7VdJHb/ELfNtd9nV bKA44dmjs/npphBFMt+k2zbJBuO18pZMs20I2oOuwrdDqC/OKmHVOV6yiT+1+s/fxna1 JxYA== X-Forwarded-Encrypted: i=1; AHgh+Rqz7I5BZUehQ2JG0hwcIF7YP/tZZ9iPlXMXHpqVhoDlO7vK35seMSMFKjqT2jFZVb7EXFzoXvyFKN9MdMw=@vger.kernel.org X-Gm-Message-State: AFuF++kMhYexH9KigoMpYBs+R2ASgkvsexwZ0+58QTI72Ul2qlfgAOue xLMgHjcJ5J639fNbYWz88h9w6nfo7lhVuDRLPTOCTSUfJ1rWkq0TYkYRkYJGEMJfcTo5z/AguD4 XHc7zyw== X-Received: from plblc14.prod.google.com ([2002:a17:902:fa8e:b0:2d5:62cd:db79]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:22c3:b0:2d0:cc92:f7a9 with SMTP id d9443c01a7336-2d707a9f435mr158585745ad.4.1787779990340; Wed, 26 Aug 2026 14:33:10 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:45 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-6-seanjc@google.com> Subject: [PATCH v10 05/21] KVM: x86: Compute kvmclock base without pvclock_gtod_data From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse get_kvmclock_base_ns() needs CLOCK_MONOTONIC_RAW + offs_boot. Compute this directly rather than reading offs_boot from the pvclock_gtod_data private copy. offs_boot only changes at suspend/resume so does not need to be atomically paired with the raw clock read. Signed-off-by: David Woodhouse Assisted-by: Kiro:claude-opus-4.6-1m Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4ab0a41eee7b..46c2c2ea64fe 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -930,7 +930,7 @@ static void update_pvclock_gtod(struct timekeeper *tk) static s64 get_kvmclock_base_ns(void) { /* Count up from boot time, but with the frequency of the raw clock. */ - return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot= )); + return ktime_to_ns(ktime_mono_to_any(ktime_get_raw(), TK_OFFS_BOOT)); } #else static s64 get_kvmclock_base_ns(void) --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12E8B488237 for ; Wed, 26 Aug 2026 21:33:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779994; cv=none; b=jfP3jRdTZDUEHKH1rArVgR3Cd2BJz+MAOVzVzVvVGu9vIRMFvDTrQGkCthgyrzTdTzMb/9UhXL5xXvptPlj3AM5O/GdOrMnoptQApUCPgoWGEuwa3q/WEvBjs+TYo6DNPK0uT2Hd4XXgWU0h6PiYl5FDza/9BOVeh9zaGxlIYBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779994; c=relaxed/simple; bh=vYwafw0yTTkax4N9kK7BxvmBePgKDiUUWhKS9VN1Mjk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jsNnIaCuTvNcEQg3J3ThDOiiRTCzm5B64aGTuGx37/rBvqo0809XNUBPp5A3MxrMP51FW8Mumxr5/6P981hv1vHfzSnYGit0ve8Of6fg9wppuFfG0INNx7xQ6iHLCdZ+ufo/GEFrVSunn+I9Pe4TJWQuFkX/R14+MAkGcF5u5cI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z6/eiln5; arc=none smtp.client-ip=209.85.215.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z6/eiln5" Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cc1c62f0d60so1710205a12.1 for ; Wed, 26 Aug 2026 14:33:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779992; x=1788384792; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=jQ9Xgia0qMFepkVGEti7/hov5lUDRQwCKPl494Tqn8c=; b=Z6/eiln5912hQA4BryXacoNrI4CFrgJC+Ck02N4orRVqe7a7MtpaaCcrKHEtUxAT0p MMNGc7nBNUk4EyzKjG/X88F6ZXVksTZoLiWeRCdGjGHreDEFfN0kwQ8UBZgjj6XbvULj re50nFdc7Cu3qaMH2YvHAAXow1gxeqsN2J/yrrB9e2o20UMKTFLOq4Z8IMrU03UHefpz UVsUbYu9U4PiVHBuv/iiIyzz5RMcskR8VvhNItTtvLMNyFV6sNmTRaSr8aCexiyPW4M0 tEfd5be9y/3tsid10Ka6c300uU/SchVpWyBZlDKqj3tfLSysSb9J6Ull+gtyAwvXWkv6 x3XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779992; x=1788384792; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=jQ9Xgia0qMFepkVGEti7/hov5lUDRQwCKPl494Tqn8c=; b=Rmsb3y2OhW8dp73pfRGDVYEEg9S+PlftNB5J+N0lEs/jhD4d6KPyBJbdQkrMHlM/O/ 7HTo8YadFKwcau3WW+vQF0g305iT9pcPPhUO8b/YMcE5WsTnkh13kjRlhbxSUi7JN2DP va9H2JHGLeVkJ2E43HWUwTK+bmc3afd//MgtnN13zVC+LlsrwmNbB7DDX/Fhi/z8A9oQ 4pBVSMQT7wzD0IcZXvnVAToLvMjM0Sk/k+vuJaNZSeQJsvboznZ6zQhYVx5EWWvKyTtW /vKEKxVNOEFGZygwEamf2/XXrVz8yjDjIhe7CIFU0RPp4doouAV2ABBPTfOgn8yKZooy yrqA== X-Forwarded-Encrypted: i=1; AHgh+RpjxP80FO5TZv8fdHUUUWGWMJb5q8WwAm4iBx1dcWFulEfs8m2kXKzE+k4/eyr5u6Q0NgH93ob8NZMeMmU=@vger.kernel.org X-Gm-Message-State: AFuF++lKB0qcphdSi1g8yDmNV9Pxb5K3tBpOFv7jkEbklMLLRQMI5qnL mWikSRudgNyJhoIhcQEsPvssDG5qTYq6iRroIe4rLeYVGiROvjiJdwvsMpfK7Bkp2P+J9F0Thu1 +2f1nAA== X-Received: from pgcn25.prod.google.com ([2002:a63:7219:0:b0:cbe:b8ad:f9bf]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:2989:b0:3d0:f57d:c87a with SMTP id adf61e73a8af0-3d0f57dcc1emr4505658637.0.1787779991384; Wed, 26 Aug 2026 14:33:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:46 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-7-seanjc@google.com> Subject: [PATCH v10 06/21] KVM: x86: Avoid NTP frequency skew for KVM clock on 32-bit host From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Now that 64-bit KVM doesn't consume pvclock_gtod_data when getting the kvmclock base, use the ktime_mono_to_any() version on 32-bit hosts so that they too are unaffected by NPT sync. Note, even if KVM still used pvclock_gtod_data for getting kvmclock base, it would still be very doable and reasonable to avoid skew on 32-bit hosts, it's just much simpler now that KVM uses ktime_mono_to_any(). Specifically, commit 53fafdbb8b21 ("KVM: x86: switch KVMCLOCK base to monotonic raw clock") did so only for 64-bit hosts, by capturing the boot offset from within the existing clocksource notifier update_pvclock_gtod(). That notifier was added in commit 16e8d74d2da9 ("KVM: x86: notifier for clocksource changes") but only on x86_64, because its original purpose was just to disable the "master clock" mode which is only supported on x86_64. Now that the notifier is used for more than disabling master clock mode, enabling it would be trivial for 32-bit builds as well, but as above, that's a moot point since get_kvmclock_base_ns() no longer relies on the notifier. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant [sean: rebase on top of ktime_mono_to_any() usage] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 46c2c2ea64fe..ed7986669bdb 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -926,19 +926,13 @@ static void update_pvclock_gtod(struct timekeeper *tk) =20 write_seqcount_end(&vdata->seq); } +#endif =20 static s64 get_kvmclock_base_ns(void) { /* Count up from boot time, but with the frequency of the raw clock. */ return ktime_to_ns(ktime_mono_to_any(ktime_get_raw(), TK_OFFS_BOOT)); } -#else -static s64 get_kvmclock_base_ns(void) -{ - /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ - return ktime_get_boottime_ns(); -} -#endif =20 static uint32_t div_frac(uint32_t dividend, uint32_t divisor) { --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D053048986C for ; Wed, 26 Aug 2026 21:33:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779995; cv=none; b=ol/om/vVhw0fd3mdk8l+zeT9mvxafe3l4v4nVFGT5PyD6K/y0H/XOXpJZRTBwnoiircxrKBrQq9pJujx7BK8g6MTxduYizi/8GChMhKLdHxje55zhsV3mXGUQUrZGAUG/2BQymDyw/tIYbJPgJBuiNkSFF4gaZPgO2akLBOi2Lw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779995; c=relaxed/simple; bh=BCTNpvjwr2susxonlk0tuu8f+ghkxuHDIS6503fQLHA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F276iD+FL1pCFcZF43TVMcZ0IRyIfd93qelF6AHjsR2dBPiGNLbQBtp6LIoTdSWQuQyQ/aPtBGaEyqWzCriMQpsiIIJkO3iC4K1fjJJqX+JF/HCDfkbRQTJd0ZLfo9qIlwww48n60bCbziXIgkZVQBjB0xm5JgQWBO7GrkbgEnk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bTCjIesV; arc=none smtp.client-ip=209.85.215.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bTCjIesV" Received: by mail-pg1-f200.google.com with SMTP id 41be03b00d2f7-cc1c62f0d60so1710229a12.1 for ; Wed, 26 Aug 2026 14:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779993; x=1788384793; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=jTWuF2bXHv9bsWC/HQQ9abPeXt+ugeBz2F3xzAoOZxE=; b=bTCjIesVIwwelgjmq+qn6JDaopdYAzBpTmfVOrxZOjXrF3yDYIv3uZPsnoeRKXR84L SBAtV98cRYaWvS5tgt1SjwdtNdQaWqH4kYCo4lRwnjRSRKBoYL7JhkQgtN6a3oQL+Mtj qfQPvGpKEtQ33eaHvqkGpQv2rIKnzptNxkFvGYAnHxQJZtCxASbQhnxZuQziqz7XhQ3G cwI4rAu4cUjxGEEIG+D9bSwgBr/5WPMrp+9PKTxSsBlzyC8v2109TZgUi+PLqQ6sP039 SDciTHMCvPd20CbvLhDlU2gBjfPfH5qn3OxVgy9xvluTEifl5u6lbPu6MfVwueWwTyh4 mhXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779993; x=1788384793; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=jTWuF2bXHv9bsWC/HQQ9abPeXt+ugeBz2F3xzAoOZxE=; b=lyvqRUdumek60lVqoZhOuwo8DrL2H0BSNjj6oXokHk4t0s+YwIuhQkDZtL9yt/mUWY of2ERd9ueQa6XfOCxx3oXW4z9JANZzfVlznASsHHEbfxYbblLtqgIJpgsxTl96aNbAXA RjPeBRlmqe+xRi9B8ccCjEaGzm6IcAaCx6svtixcPaMisZzxoAW+nBnfytfKhKe9ujz7 cn28zETaqiTxb+Gtk39SO1sNqF2Ekk0WPSJsyYc2X5iJ215hYNFSc9Gjb0RWzh/S2tKK d0Al/50Zn6vf6woq0lHyovjfJv3UWNTl64kul0u+WM2TqYw9mlSoruF85Y5XORJQTsw1 dHfA== X-Forwarded-Encrypted: i=1; AHgh+RrNrXDrV2GnFyArWFp6LD7k9YZz9U3mQZeO095szpjtOOWI+Ksr1HlThDjHFGId144azPDvztrdmTUxT0w=@vger.kernel.org X-Gm-Message-State: AFuF++l6TFPpqoWl90VEp7+RSv28ZGhG9KlEoxCRDzdT8Ct/f+6/fk54 n2p3QJomnFi11m8t8o4J5Cv+Gy5JTdQJJdbKhjG8SziQfi+F3xxw/y6Z8pggriCauwJuRkzd4M5 jdLtELg== X-Received: from pgzz12.prod.google.com ([2002:a05:6a02:682c:b0:c85:735b:ca4a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:116:b0:3cd:61d5:f342 with SMTP id adf61e73a8af0-3cf83b22bc2mr21127205637.9.1787779993030; Wed, 26 Aug 2026 14:33:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:47 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-8-seanjc@google.com> Subject: [PATCH v10 07/21] KVM: x86: Drop unnecessary CPU pinning when computing/getting kvmclock From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When computing the current kvmclock value, don't pin the task to the current CPU for the entire duration of the master clock path, as the CPU pinning was never about ensuring rdtsc() and cpu_tsc_khz would agree. As pointed out by David, ka->use_master_clock can only be true when the host clocksource is TSC based, which in turn requires a stable, constant and synchronised TSC across all CPUs. The CPU pinning was added in commit e2c2206a1899 ("KVM: x86: Fix potential preemption when get the current kvmclock timestamp") purely in response to a CONFIG_DEBUG_PREEMPT=3Dy bug due to accessing a per-CPU variable with preemption enabled. Despite what the comment would suggest, including rdtsc() in the {get,put}_cpu() section was opportunistic. In fact, Paolo even said exactly that when suggesting that KVM guarantee the rdtsc() would execute on the same CPU[*]: : Also, rdtsc() should really be on the same CPU as __this_cpu_read. We : know it's not really really necessary because the master clock is : active, but since we need a get_cpu/put_cpu pair, better be clean. Nothing has changed in the last ~9 years, i.e. the rdtsc() still *should* be on the same CPU, but super strictly speaking, all will be fine if the task is migrated between grabbing the frequency and doing rdtsc(). Dropping the CPU pinning will allow dropping the rdtsc() entirely without having to resort to a large "rewrite get_kvmclock()" patch. Opportunistically add a comment to explain why KVM needs to snapshot the frequency, because that _is_ a hard requirement to avoid reintroducing the bug fixed by commit e70b57a6ce4e ("KVM: X86: Fix softlockup when get the current kvmclock") Link: https://lore.kernel.org/all/ae8de642-8f14-a70a-1fab-57e2c4093cd5@redh= at.com [*] Suggested-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ed7986669bdb..75b689ec074d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1657,13 +1657,18 @@ static void __get_kvmclock(struct kvm *kvm, struct = kvm_clock_data *data) { struct kvm_arch *ka =3D &kvm->arch; struct pvclock_vcpu_time_info hv_clock; + u64 tsc_hz; =20 - /* both __this_cpu_read() and rdtsc() should be on the same cpu */ + /* + * Snapshot and validate the TSC frequency as kvmclock_cpu_down_prep() + * zeros the per-CPU value when a CPU is going offline. + */ get_cpu(); + tsc_hz =3D (u64)get_cpu_tsc_khz() * HZ_PER_KHZ; + put_cpu(); =20 data->flags =3D 0; - if (ka->use_master_clock && - (static_cpu_has(X86_FEATURE_CONSTANT_TSC) || __this_cpu_read(cpu_tsc_= khz))) { + if (ka->use_master_clock && tsc_hz) { #ifdef CONFIG_X86_64 struct timespec64 ts; =20 @@ -1677,15 +1682,13 @@ static void __get_kvmclock(struct kvm *kvm, struct = kvm_clock_data *data) data->flags |=3D KVM_CLOCK_TSC_STABLE; hv_clock.tsc_timestamp =3D ka->master_cycle_now; hv_clock.system_time =3D ka->master_kernel_ns + ka->kvmclock_offset; - kvm_get_time_scale(NSEC_PER_SEC, get_cpu_tsc_khz() * 1000LL, + kvm_get_time_scale(NSEC_PER_SEC, tsc_hz, &hv_clock.tsc_shift, &hv_clock.tsc_to_system_mul); data->clock =3D __pvclock_read_cycles(&hv_clock, data->host_tsc); } else { data->clock =3D get_kvmclock_base_ns() + ka->kvmclock_offset; } - - put_cpu(); } =20 static void get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data) --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BE8A3A785A for ; Wed, 26 Aug 2026 21:33:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779996; cv=none; b=T6rUmZIq5MHIQn2XGure8fX2oxwsON8DURF+hgpP0Au0aQ69oSBIGolOhS52p0e2Yle/L+sGwT53SuuLs3yScpF/FUoGVRxzXMiAvOtoEyqQIEsCyV/txEqyihbqN41mHEPulGpiOGnvgVIRqOXuJ7wWTfCmELPa7vH/fJGnQhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779996; c=relaxed/simple; bh=dsaCIkuhvZqCjTZVeCEPOV45bBjnIf2n5TfEJj18Yb0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Djp0Vp/6kEB5PbZAuHoElvkRljefB2gJOP+eH8HTLVQIlzk3ayTgy/9SWf0E8F8vBaxcMqelCk9BDZd2fIF+Yv2x0f6kZGx19zKh6ZUtgVnstSRTz9IhqVK5kwC5lOkNqQAZUYZgdyb+c9utwxwE/69KiXNnc47bb9XW1zGTllk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ixan4xUQ; arc=none smtp.client-ip=209.85.215.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ixan4xUQ" Received: by mail-pg1-f200.google.com with SMTP id 41be03b00d2f7-cc12e8e22b7so2978512a12.0 for ; Wed, 26 Aug 2026 14:33:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779994; x=1788384794; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=l1eygQedJKM5ekEKsn/E9bXYihX3nGywRp8RfIbS/YY=; b=Ixan4xUQea9JGSo9sfaaaA5G2Afb1fK8nv24uSr3maw5mEo6dbYrGj2QiLVFRcmtKj kr1oA0MR+nnSx7+KpfUacFC6GNd8u4aoNOTZSReu2AR3ExvkdeRru2szuYnzO01iQOJE 3NkOstxS+tJ3wSv2zoWqSzyVkpdSCQ9c/qCiKU2JsiLQKLSsjS7orfxB5yqIfIKUK20d It8qSa31O4TotRd+CA5SMr82oTyGWKEngZjf/z7Y26ID/Ej+mvs3Pp0G7YyMApJMgWEv GHOSNoN3cNWP7oqBbyOl1gA03IgrIqHU5JOfXNUYmHYLDNIJ/lumpUPZoVHf1V6SLKG0 oIqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779994; x=1788384794; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=l1eygQedJKM5ekEKsn/E9bXYihX3nGywRp8RfIbS/YY=; b=GGDYdq3COQNBYFlsHokdzZ8KZeaALEFqG8qi5BjiY5hyVoWeb5FpHa5NnFtU1h7YPQ xjugEUZUp9p8fqL3dMMpfRU3QlSIpI/HKOS/Wqmq2mVNywr3IXZJcw1UZ4Fr39WZUEZw VvurIjqMX8YS7o3kuBozt7aX3qIMAkzc7UATcHrqLTc3Qfq1un7eMR+/2kxY4RRVC9+l NC2k3Vz8uXt28GuPCM8czioaeJ4yzkeLKuCu1V77vSXXHl0NTt5mtp9qMAJdpUoM5sdx Waaf6FEB7lnHnULzC2xZUw2slF+Nwr0oioNwEHKZo+gCeM4ITZsHjI67NuAvtBGD9aUd UxRw== X-Forwarded-Encrypted: i=1; AHgh+RpNLfAx7Y9eEy6c3yo7yQg/xIIhLB7R9T2lfbD/8QrPzUUYIx5m0l13jPKffa95lhcWRYxTSyrJg2v6PH8=@vger.kernel.org X-Gm-Message-State: AFuF++kZ00KYK228M9Ti62mwzjTInevMo8cDFFhon+Bgy9yylTniLCVi WRikQ83YVJkfSqkDuoOScAmvT38xVLDEsoU9zaS/frLCoeDdhzzdccuauq/O9BC8/5eUoIJX4h1 L1BIXSg== X-Received: from pgbs63.prod.google.com ([2002:a63:5e42:0:b0:cbe:947d:d145]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:9c86:b0:3c3:bbe6:95c7 with SMTP id adf61e73a8af0-3cf84e4b0f8mr20727681637.17.1787779994090; Wed, 26 Aug 2026 14:33:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:48 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-9-seanjc@google.com> Subject: [PATCH v10 08/21] KVM: x86: Move "no master clock" fallback from __get_kvmclock() to get_kvmclock() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the fallback logic for getting the current kvmclock when not in master clock mode out of __get_kvmclock() and into its sole caller, get_kvmclock(). This will allow using early-return logic in the master clock code, without having to resort to a do-while() loop and/or gotos. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 75b689ec074d..4f98cc71a671 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1653,12 +1653,16 @@ static unsigned long get_cpu_tsc_khz(void) } =20 /* Called within read_seqcount_begin/retry for kvm->pvclock_sc. */ -static void __get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data) +static bool __get_kvmclock_master_clock(struct kvm *kvm, + struct kvm_clock_data *data) { struct kvm_arch *ka =3D &kvm->arch; struct pvclock_vcpu_time_info hv_clock; u64 tsc_hz; =20 + if (!ka->use_master_clock) + return false; + /* * Snapshot and validate the TSC frequency as kvmclock_cpu_down_prep() * zeros the per-CPU value when a CPU is going offline. @@ -1667,8 +1671,10 @@ static void __get_kvmclock(struct kvm *kvm, struct k= vm_clock_data *data) tsc_hz =3D (u64)get_cpu_tsc_khz() * HZ_PER_KHZ; put_cpu(); =20 - data->flags =3D 0; - if (ka->use_master_clock && tsc_hz) { + if (!tsc_hz) + return false; + + { #ifdef CONFIG_X86_64 struct timespec64 ts; =20 @@ -1686,9 +1692,9 @@ static void __get_kvmclock(struct kvm *kvm, struct kv= m_clock_data *data) &hv_clock.tsc_shift, &hv_clock.tsc_to_system_mul); data->clock =3D __pvclock_read_cycles(&hv_clock, data->host_tsc); - } else { - data->clock =3D get_kvmclock_base_ns() + ka->kvmclock_offset; } + + return true; } =20 static void get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data) @@ -1697,8 +1703,11 @@ static void get_kvmclock(struct kvm *kvm, struct kvm= _clock_data *data) unsigned seq; =20 do { + data->flags =3D 0; + seq =3D read_seqcount_begin(&ka->pvclock_sc); - __get_kvmclock(kvm, data); + if (!__get_kvmclock_master_clock(kvm, data)) + data->clock =3D get_kvmclock_base_ns() + ka->kvmclock_offset; } while (read_seqcount_retry(&ka->pvclock_sc, seq)); } =20 --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2248848C412 for ; Wed, 26 Aug 2026 21:33:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779999; cv=none; b=Nh6ne2h2MvlqBJJFKg6TLEyfRkgcf9UuM0oNC7H7TqWbQ5xazRcbzyP/OZgtwc69ECriOKMs/NLFtuniFZ9d5ZmbG7hdrEzktz3AZQO6SIa6KMF8YrOzyjXjZjSen3nUZVRQxnsEkUqZQbM5Z55bwiUOGIFV40qfyRn0Bytamqc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787779999; c=relaxed/simple; bh=1dubEWiH+wDpaRTr6xDH5FSSCgfhV3tA2a7yb409fro=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EasHG/SAbrIIu5AaQL+3hr/ASFohP4psyIPeVFA0MNgIuoiIPWm9QZmWAigfKofGbcQyl+14GrFYycscFHqlFAYQsfVENkinRpZF+W1OSugrLFAIID7EphkeFHfs0PBbhoul6D8MLNKk3p4WKtbvQ5xw4TtaE3YA6ltfI/vR2KE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rvp5AY2S; arc=none smtp.client-ip=209.85.214.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rvp5AY2S" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2d55d8cd938so4620945ad.1 for ; Wed, 26 Aug 2026 14:33:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779995; x=1788384795; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=iaeGWHwlG+AWk9QHVwY9V6qXxKpEzxckDrOJKqWFOA8=; b=rvp5AY2SqLRucYy7MFVva3mw9Zl9G3aLWfuJouawTo1NwwhJLPALb4wkkOnClPIYT0 FRl8ihJQmHzjKHcR7limoh7n6Ql7snRhviz6Afz5/zGeMBao6ZuSJ1EPaVsMWFBUMtGP iXaLICt8ucwFsEKfVuj25AUQCJ19e9MXnAvZKQ5h1d+Xe0NfiuKwcaFd7Qc0azlfBrQH HJgxtl+wuDwvPROgsFPZmgFeexyTCQjWzdaysgRqJmWso3VMyXz1h1mo4YmD/gEmCi97 MtlXp7Hip17f5cicXEV76DT5ZbzeEhwkj5Mu8r+GhSkg/5q0C74NdOHjhwFu5m0oa0uz YVoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779995; x=1788384795; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=iaeGWHwlG+AWk9QHVwY9V6qXxKpEzxckDrOJKqWFOA8=; b=gigkagZefi40UWP9+1DUq0TUQEuqdYbd3V2jIGhDG8CWYdJ3MisYoV4pBZOga2XBRT XpzrF+4C1g1ERdzOE8Kg3nMvLsiyZe7tTqwNh9+NGS5239WY8hEvX8yioOKeim2sE82U +Y8rnKLZs3VMWSy5vocynqswH0+0fStO1GHWSTyLBFDMzv5tcPimP22Ouzvdg8gE4xd+ fTUjQm3Y8+3ZR511jLuZ52aKngE2dSl1nly+XEI0UOpXCc6+DmmR6n5aT2EFznxera/u rTkwcg/e1/PzBmg6Bg6WFIskZY5GIKCcTmOkoZjK//32vHcdR4zpUv6XZ/+PsY3gbk3g 6juw== X-Forwarded-Encrypted: i=1; AHgh+RpKfVo6v0WXP7flnNYHeLfpdb4agIxKDDXCXXN0edjfwb4kCc1w9iJVSlIUj27m08UXAVhYxkZlik33zLQ=@vger.kernel.org X-Gm-Message-State: AFuF++mG7p1zwDL3nKChzK46ygPcsJZNAe3Li4WAYLtkT2QCXxmo1IPQ nKofeIeYR18R7aVGytJjYMvvBUBDqvi3s6q4nsDaUgPUPDMuj1cR0N8x+ksqgTK7GuOCDM/ZT2x TmXdTpg== X-Received: from plbmq13.prod.google.com ([2002:a17:902:fd4d:b0:2ca:cf09:91ca]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:120c:b0:2c6:90ec:f601 with SMTP id d9443c01a7336-2d707aaad36mr147369515ad.8.1787779995135; Wed, 26 Aug 2026 14:33:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:49 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-10-seanjc@google.com> Subject: [PATCH v10 09/21] KVM: x86: Wrap all of __get_kvmclock_master_clock() with CONFIG_X86_64=y From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Wrap the entire use_master_clock block in #ifdef CONFIG_X86_64, since use_master_clock is never true on 32-bit (host_tsc_clocksource is only set under CONFIG_X86_64), and declare hv_clock inside the block so it is not left as an unused variable on 32-bit. Signed-off-by: David Woodhouse [sean: keep only the CONFIG_X86_64 ifdef, update changelog accordingly] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4f98cc71a671..64df8da3aad4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1656,8 +1656,10 @@ static unsigned long get_cpu_tsc_khz(void) static bool __get_kvmclock_master_clock(struct kvm *kvm, struct kvm_clock_data *data) { +#ifdef CONFIG_X86_64 struct kvm_arch *ka =3D &kvm->arch; struct pvclock_vcpu_time_info hv_clock; + struct timespec64 ts; u64 tsc_hz; =20 if (!ka->use_master_clock) @@ -1674,27 +1676,24 @@ static bool __get_kvmclock_master_clock(struct kvm = *kvm, if (!tsc_hz) return false; =20 - { -#ifdef CONFIG_X86_64 - struct timespec64 ts; - - if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { - data->realtime =3D ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; - data->flags |=3D KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; - } else -#endif + if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { + data->realtime =3D ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; + data->flags |=3D KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; + } else { data->host_tsc =3D rdtsc(); - - data->flags |=3D KVM_CLOCK_TSC_STABLE; - hv_clock.tsc_timestamp =3D ka->master_cycle_now; - hv_clock.system_time =3D ka->master_kernel_ns + ka->kvmclock_offset; - kvm_get_time_scale(NSEC_PER_SEC, tsc_hz, - &hv_clock.tsc_shift, - &hv_clock.tsc_to_system_mul); - data->clock =3D __pvclock_read_cycles(&hv_clock, data->host_tsc); } =20 + data->flags |=3D KVM_CLOCK_TSC_STABLE; + hv_clock.tsc_timestamp =3D ka->master_cycle_now; + hv_clock.system_time =3D ka->master_kernel_ns + ka->kvmclock_offset; + kvm_get_time_scale(NSEC_PER_SEC, tsc_hz, + &hv_clock.tsc_shift, + &hv_clock.tsc_to_system_mul); + data->clock =3D __pvclock_read_cycles(&hv_clock, data->host_tsc); return true; +#else + return false; +#endif } =20 static void get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data) --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B46548C403 for ; Wed, 26 Aug 2026 21:33:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780004; cv=none; b=aiAV4G3D8C75VdTbbWWBvMrqGh+QGel1/y8uHLJoRyaf4slwNjnL6e9fXSjz7oYyZmRMUrRmDHk+jtdXRh9/GzLITL9Oo63W1mVe5bihsas1mtpMGDMlMCAPc/b9c3PhyC/GRRninKA+TO9yfEXwbS9HleLpWZmkIE/7L4wVfp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780004; c=relaxed/simple; bh=Dyer1ULn/a/i4L4ozp6ZOIcINMbH4yOkx+5FletSldI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UoO8mz9g0539hkeTvhL0QmGgg0nYjWDN86GIwgfvKulaVfl36IXGjg9HRFSvE1Tv2NOB9fZrBrvsz6xTCXDsAmO/mgHKEBqKq9AAmsJV81B3rvGJrTwiUAbrY+GowK5/NUjvXOOXcFkWEVa1Fz29avrZry91iV2WzXyNRe/qfQo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ERe9rvgW; arc=none smtp.client-ip=209.85.215.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ERe9rvgW" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cc132709c76so56529a12.3 for ; Wed, 26 Aug 2026 14:33:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779996; x=1788384796; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=goM61cDq5JnxRmzeon6yIX5IBAcd9O/5+qmf+efG8yU=; b=ERe9rvgWNMRNhq8CSzZF0bpo/QSW7+SbU1gXN5J8tzv32LQy5Pj7iJ9fIWGrIf3jc7 uMuC0BByzFxhcuH57U6YvqacPdjbluk7GpxIqgBz2IWqCJHHd9/CdWa60+oa7Q3VnKkA 6TZC9OAF6DBGz27LL31cz1RK9KXB+W3nNtKFUU+oO0XWyl4XxfoSfeTXQIh5svILkzxS Ri0nD4mfmcbwxp0ZE/UvMyLtZ0rOmJKCQLn9HrOS5Ire7BOKAZJbFdIOAkw/2yERTCQc hMC1nzzkVEJ7NdceuymLMK3MUSVIKQS9hR2/BcKopFbzHLagNtgINoXg6/ihZnP6VxZo m1wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779996; x=1788384796; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=goM61cDq5JnxRmzeon6yIX5IBAcd9O/5+qmf+efG8yU=; b=p2qAba893y4Cxp1SHsNJzI2Uf1vPZseiYGP4LoHHjoFUeu5QwcpVgKANHpmQG6hmun thEHSPfTR0Sah0LJV/O0OhEw/tjgH3VD1OyMmsFzfSB2gJhAHi/F17OSzkx9OdS1KCHI SusuoUtingQO3DblMeIoP3UFtxyq1Z/AsCf636BEj3Kps8tduW7V0dysyTGgUHD7/7+3 q0kjjKu++qVe2+pOXOEET9SvigGkAhqE8jWAcqnNi4Qjb8N8xG5qVpaR9t1XNco5ajS2 kTxkFtlpAdsKM3N5/DFMvz6TjxWKj0EvE0aBiesCTVhn/fgM6TAS3TZK1r0OhlDBWV+a GUhg== X-Forwarded-Encrypted: i=1; AHgh+RrJ1Sahwsa0a2nUe1fVbFCsB0sVxs2XW73g6NsYp5tuQniAXIwNy+CMBmQFpVc0rs7gtSVjKmJ0s8tUNTE=@vger.kernel.org X-Gm-Message-State: AFuF++kF7+vl6TG/ROaagcEki3szbSUuLC74J5W3rUqvOaBvDHf7ykbF K9LQGZ3BiR0LGeoWFea1mEcQ+EoTegO665/dyOffbbuwXd3QHocrAQjCBM6OIQ9OPry8rJuy3L6 m60zuxQ== X-Received: from pgjh17.prod.google.com ([2002:a63:df51:0:b0:c8b:2706:11a7]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:93a0:b0:3c3:791e:5e18 with SMTP id adf61e73a8af0-3cf83341769mr21745880637.7.1787779996284; Wed, 26 Aug 2026 14:33:16 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:50 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-11-seanjc@google.com> Subject: [PATCH v10 10/21] KVM: x86: Fall back to non-master-clock if clockread fails in get_kvmclock() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse When computing kvmclock and it's currently in master-clock mode, fall back to the non-master-clock path if the clock read fails, e.g. if the kernel's clocksource is transitioning away from TSC but ka->use_master_clock hasn't been updated yet. The rdtsc() fallback was added (well, kept) in commit c68dc1b577ea ("KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK") purely to avoid uninitialized variables and compilation problems on 32-bit kernels (already addressed). In hindsight, keeping the rdtsc() was a hack and a mistake. Link: https://lore.kernel.org/all/CAOQ_QsgVqS_PuJo8F10Gg5Xw+tKt+5gDx+kJf1j3= CiPO4MAOqg@mail.gmail.com Signed-off-by: David Woodhouse [sean: isolate from refactoring changes, write changelog] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 64df8da3aad4..d8bdc64b31ee 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1676,14 +1676,13 @@ static bool __get_kvmclock_master_clock(struct kvm = *kvm, if (!tsc_hz) return false; =20 - if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) { - data->realtime =3D ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; - data->flags |=3D KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC; - } else { - data->host_tsc =3D rdtsc(); - } + if (!kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) + return false; + + data->realtime =3D ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec; + data->flags |=3D KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC | + KVM_CLOCK_TSC_STABLE; =20 - data->flags |=3D KVM_CLOCK_TSC_STABLE; hv_clock.tsc_timestamp =3D ka->master_cycle_now; hv_clock.system_time =3D ka->master_kernel_ns + ka->kvmclock_offset; kvm_get_time_scale(NSEC_PER_SEC, tsc_hz, --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B03EC489868 for ; Wed, 26 Aug 2026 21:33:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780004; cv=none; b=obMH1TskYFoqjTtK7sdFb/70S1pylCKa1UCbA9YaisYFk2yahW3qdwHlL4xsTOduK7t27uy65Dt6zt/MfFzQBMSPB3FJVPtjcuyK0hLkD99/GOvo4Og/qZ7Tj0XAO/9OvKi1k9d8zHb+1EtpSypy6q90fhWk3WPm/v26Bmq/TOA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780004; c=relaxed/simple; bh=sXDQZzyLfMpNXHAU0iuO6oPpDwKknuBXN21wmqthJTQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tCsufLKkRqFzW/sWkGmTeXbzZnHCPdMeiV22iZXwEnw4tnz240a+3mzTNgOyvboHV9H3OavMr/4ghNsE0VmvnQvnuo5xG5G7VBmy2KZlJRkZzoX5DmEtIuV7w3eXAt8A/WRy7p7pllG/t73P6SZz/E2oWtyu+iawRHyMPv66nTs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=D5umEJZp; arc=none smtp.client-ip=209.85.215.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="D5umEJZp" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cc1b8088202so28692a12.3 for ; Wed, 26 Aug 2026 14:33:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779998; x=1788384798; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=FRbtodcnc3a4V5kRubqK5bV3gSLOL5hK3M7G07fabw4=; b=D5umEJZpMz2NkrqUejCRmjgDHcsWWNEV800abF3hqyR5iE7kBauZgb7eAUy+Dd4Vz6 AQ/0TWU0lxxB625NNssWMjKNUEXP8WjJ9gQ26h9h1nuNG4Wm2D7zq1EB3lcYe0zK7LgC 6hiFUv5IRyVhuYg5IkyGOQapoPVCRfswaSeGhuot8JKkg9bunMGslLWicrVqsokHxpbe pEUTd70S7iawjCuudk9rxqH2X41GRrDuGqto1EVIfjmXJ3wm7r9e4wUJ5iFqQ3qPegB1 X5MdNJ5k4QLc0obKxOWo/kGHySvx+OTg5EnsRVpmmElfb5Yn7PL+Qs4YVO1BTNO9Pip7 Uy0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779998; x=1788384798; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=FRbtodcnc3a4V5kRubqK5bV3gSLOL5hK3M7G07fabw4=; b=WKCoFKPAe6KRyDLP8oF0ABhL0C1Un5Ff9HMJXqx0W8dIvOXKk4dp2c5iS2yItluxL+ ju4VujgvomINuhk2ut8/mTm3MZw5OC9FqKSMBInKzX+uWPuxXH4ww2qzu8Ljj4bsVJjB FxsPFmUH0D8tzDVbFpE+qMSpRDyWqf2uZ8tFvJo14L3lDrYyHtn+2TujqdUKwkjaH3bG CGhXP5rbOrnYSTHce+wcTvTc0k/dDyQZWn6hoEWhHLnEI/3YtxLh9S+6vPOmL+uWtVcL 1cyO8+HPddPGq9tbsuC4uwgQYSQITp0gZ4TjeRZfoCPEgKGwsIWDOPoLdK3kq56hpzIq D2xQ== X-Forwarded-Encrypted: i=1; AHgh+Rr6qBoaTChhUvRGjVodxh/7N65I+0ZYQnAoOgN7zAvznPO0hUuCQVeNjYM/0hj7uNkJeW//mickrS12FG8=@vger.kernel.org X-Gm-Message-State: AFuF++kq5tNhuLb3pvQT1NlXfqMyt/40GL1pPkqLFJB6yYtOd9k+T0rK pTswRuU6XoD1NUiNDmS2tV5SOthLiT6D3TTYA8JPVTlhgL7izre3ETqVEp9dCG8ew4sO5/i6ZDL yGkntiw== X-Received: from pgvi20.prod.google.com ([2002:a65:61b4:0:b0:cab:ba03:2c9f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:4596:b0:3d0:cc29:ff63 with SMTP id adf61e73a8af0-3d0cc2a04a0mr7937373637.12.1787779997474; Wed, 26 Aug 2026 14:33:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:51 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-12-seanjc@google.com> Subject: [PATCH v10 11/21] KVM: x86: Fix KVM clock precision in get_kvmclock() with TSC scaling From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse When in master clock mode, the KVM clock is defined in terms of the guest TSC. But get_kvmclock() was computing it from the host TSC without applying TSC scaling, leading to a systemic drift from the values the guest computes from its own TSC. Store the VM's TSC scaling ratio in kvm_arch and precompute the guest-TSC-based mul/shift in pvclock_update_vm_gtod_copy(). Use these in get_kvmclock() to scale the host TSC delta to guest TSC before converting to nanoseconds. This avoids "definition C" of the KVM clock described in commit 633d7652f80f ("KVM: x86/xen: Do not corrupt KVM clock in kvm_xen_shared_info_init()"). Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/x86.c | 61 ++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 0255ca6b24c2..b958a6ff7f34 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1236,6 +1236,7 @@ struct kvm_arch { u64 last_tsc_write; u32 last_tsc_khz; u64 last_tsc_offset; + u64 last_tsc_scaling_ratio; u64 cur_tsc_nsec; u64 cur_tsc_write; u64 cur_tsc_offset; @@ -1251,6 +1252,9 @@ struct kvm_arch { u64 master_kernel_ns; u64 master_cycle_now; struct ratelimit_state kvmclock_update_rs; + u64 master_tsc_scaling_ratio; + s8 master_tsc_shift; + u32 master_tsc_mul; =20 #ifdef CONFIG_KVM_HYPERV struct kvm_hv hyperv; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d8bdc64b31ee..1da7b60fe274 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1249,6 +1249,7 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vc= pu, u64 offset, u64 tsc, kvm->arch.last_tsc_write =3D tsc; kvm->arch.last_tsc_khz =3D vcpu->arch.virtual_tsc_khz; kvm->arch.last_tsc_offset =3D offset; + kvm->arch.last_tsc_scaling_ratio =3D vcpu->arch.l1_tsc_scaling_ratio; =20 vcpu->arch.last_guest_tsc =3D tsc; =20 @@ -1561,6 +1562,8 @@ static bool kvm_get_walltime_and_clockread(struct tim= espec64 *ts, * */ =20 +static unsigned long get_cpu_tsc_khz(void); + static void pvclock_update_vm_gtod_copy(struct kvm *kvm) { #ifdef CONFIG_X86_64 @@ -1584,9 +1587,30 @@ static void pvclock_update_vm_gtod_copy(struct kvm *= kvm) && !ka->backwards_tsc_observed && !ka->boot_vcpu_runs_old_kvmclock; =20 - if (ka->use_master_clock) + if (ka->use_master_clock) { + u64 tsc_hz; + atomic_set(&kvm_guest_has_master_clock, 1); =20 + /* + * Copy the scaling ratio and precompute the mul/shift for + * converting guest TSC to nanoseconds. These are used by + * get_kvmclock() to compute kvmclock from the host TSC + * without needing a vCPU reference. + */ + ka->master_tsc_scaling_ratio =3D ka->last_tsc_scaling_ratio; + tsc_hz =3D (u64)get_cpu_tsc_khz() * HZ_PER_KHZ; + if (tsc_hz && kvm_caps.has_tsc_control) + tsc_hz =3D kvm_scale_tsc(tsc_hz, + ka->master_tsc_scaling_ratio); + if (tsc_hz) + kvm_get_time_scale(NSEC_PER_SEC, tsc_hz, + &ka->master_tsc_shift, + &ka->master_tsc_mul); + else + ka->use_master_clock =3D false; + } + vclock_mode =3D pvclock_gtod_data.clock.vclock_mode; trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, vcpus_matched); @@ -1660,22 +1684,10 @@ static bool __get_kvmclock_master_clock(struct kvm = *kvm, struct kvm_arch *ka =3D &kvm->arch; struct pvclock_vcpu_time_info hv_clock; struct timespec64 ts; - u64 tsc_hz; =20 if (!ka->use_master_clock) return false; =20 - /* - * Snapshot and validate the TSC frequency as kvmclock_cpu_down_prep() - * zeros the per-CPU value when a CPU is going offline. - */ - get_cpu(); - tsc_hz =3D (u64)get_cpu_tsc_khz() * HZ_PER_KHZ; - put_cpu(); - - if (!tsc_hz) - return false; - if (!kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) return false; =20 @@ -1685,10 +1697,25 @@ static bool __get_kvmclock_master_clock(struct kvm = *kvm, =20 hv_clock.tsc_timestamp =3D ka->master_cycle_now; hv_clock.system_time =3D ka->master_kernel_ns + ka->kvmclock_offset; - kvm_get_time_scale(NSEC_PER_SEC, tsc_hz, - &hv_clock.tsc_shift, - &hv_clock.tsc_to_system_mul); - data->clock =3D __pvclock_read_cycles(&hv_clock, data->host_tsc); + + /* + * Use the precomputed guest-TSC-based mul/shift so that the kvmclock + * value matches what the guest computes from its own TSC. + */ + hv_clock.tsc_shift =3D ka->master_tsc_shift; + hv_clock.tsc_to_system_mul =3D ka->master_tsc_mul; + + if (kvm_caps.has_tsc_control) { + u64 tsc_delta =3D data->host_tsc - ka->master_cycle_now; + + tsc_delta =3D kvm_scale_tsc(tsc_delta, ka->master_tsc_scaling_ratio); + data->clock =3D hv_clock.system_time + + pvclock_scale_delta(tsc_delta, + hv_clock.tsc_to_system_mul, + hv_clock.tsc_shift); + } else { + data->clock =3D __pvclock_read_cycles(&hv_clock, data->host_tsc); + } return true; #else return false; --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69E2E2E764D for ; Wed, 26 Aug 2026 21:33:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780003; cv=none; b=jlQFmTchzuQGkJuphm0R/31nFHBqLRJnXI00F9Miu4XLiK8BMBxQv93ICLi1a2QEg/y23DUpJFT2R9mkwJ1hdEBY2vgtp/n3+k28uwaZZ9ozgl6sZpARXfxkZ5n0Yk4phNmHc6S/JgziA7aFUYXLWO5gpoV8Izj820bmfaB27ac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780003; c=relaxed/simple; bh=9z6mjzl2SjhTUWaBxckfGZ0ZvIf2SOECpcAYHrhr2/o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aRBOjC5/q/sYGIwAG+g36/hZtfBfMppnlh2jXqBqBa+pPSrjD6f29qMQr2Sd+PYa01tFHhksrS/RSB7YhfAms+Mb/lqeN5B8YYgEFMSBxv1CC5P7OGvLjPvQPdDeE4FQzlehC08PDFlCnJZugubr+t5qZSuZiWZNdsVlNhRHSM4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=MKlYsRwq; arc=none smtp.client-ip=209.85.214.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MKlYsRwq" Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2ccb687f82eso19151795ad.3 for ; Wed, 26 Aug 2026 14:33:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787779999; x=1788384799; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:reply-to:from :to:cc:subject:date:message-id:reply-to:content-type; bh=szdnMJ3ntif5HGVUZlK4HU9owrFtRnFNddqnadYAxEQ=; b=MKlYsRwq/aOElDtEVcAID6wClnNd2uxQelNuAn5r9F1HBnd2JFahMkRWSXiKLDwdp/ 7vXbfH/h/I7oFdFGrG5VXlOEAhJHw4M+Fg2h4sV8TnGtoRZLnr6eQPcw6ZsCOpRQle1b 7mKl8kdiEDSU9tAy6pZ0+H3Em7TIcHqwOuE9STdh4ICUi1eXi+pJnW1I2+pSqFFyO4Wt u5Lf4wbtetIDLEEx/sHtS071m3xdkR7zQDHAC9NivcDvJ29zswKA8vqzddb1AZju7LmQ /2Q09gYgGGqnp06FZLILfEV8lWTOqCQvGJmUzuphiU4CDffIa6ITsVOXihQjbQQ1LlKb HAlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787779999; x=1788384799; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=szdnMJ3ntif5HGVUZlK4HU9owrFtRnFNddqnadYAxEQ=; b=hGkdKaUYKH5+atlp4gUzYyxxJtCOQPUVvWjihquQrn66+Zdo57C8JyUUI8jmzMYOxb rvJ+jdDDHh7ABfTaIT4kvuZ9yqQWTvqo/8wx2QqdvcglpVF3VAdIDnb52ZckqwcsRofn iCKFS4QLWpm7fL1JnaSPkwhOefEKEOzG3t1oHCxVvGOm/xKWi3BLAf2zJhHy+ymF8d5Y mqgRGC001C5nun/sEs4UjIM6fgz58MixBw+C/jXS0Iy0bIXwi4DdGkkiFU6bEPy3WlLt WwPXGAr8Qz/hIZYcX6DYTGIrKBIAbAbIZq2IQa6YEJOzv6ErWmuFoGjo+rArvyuFmSgB X0iQ== X-Forwarded-Encrypted: i=1; AHgh+RoWdUM/CiKhzee1T4Q0XkA/NomXhjcMacc6umzUvov0lytW57lBtXgs8yhnQLG2oHaH/wdAnmWBYL0SBjs=@vger.kernel.org X-Gm-Message-State: AFuF++kMFO0hJadzz4gfj2GptaERn/8AGs0GCzHcpdwSx7i8oMHyM7rT cJgJTpxTxPfKdz+Cf3u1sAjdx+x+5rmOpnxHQwPJsnwS8L5Yf3duKPn0tz4mTKH3HNjvEH4e7AS XCI6fyg== X-Received: from plgm5.prod.google.com ([2002:a17:902:f645:b0:2c8:903:9ae7]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:98b:b0:2c8:248a:5dbb with SMTP id d9443c01a7336-2d707a37141mr187514975ad.7.1787779998570; Wed, 26 Aug 2026 14:33:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:52 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-13-seanjc@google.com> Subject: [PATCH v10 12/21] KVM: x86: Use get_kvmclock() in kvm_get_wall_clock_epoch() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Now that get_kvmclock() correctly handles TSC scaling and captures both wallclock and kvmclock from the same TSC reading, kvm_get_wall_clock_epoch() can simply call it instead of duplicating the pvclock computation. This eliminates the last instance of the "definition C" kvmclock calculation =E2=80=94 as described in commit 633d7652f80f ("KVM: x86/xen: D= o not corrupt KVM clock in kvm_xen_shared_info_init()") =E2=80=94 which computed nanoseconds directly from the host TSC without accounting for guest TSC scaling. Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 59 +++++++--------------------------------------- 1 file changed, 9 insertions(+), 50 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1da7b60fe274..b9b7ae965d6d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1941,63 +1941,22 @@ int kvm_guest_time_update(struct kvm_vcpu *v) * wallclock and kvmclock times, and subtracting one from the other. * * Fall back to using their values at slightly different moments by - * calling ktime_get_real_ns() and get_kvmclock_ns() separately. + * calling ktime_get_real_ns() and get_kvmclock() separately. */ uint64_t kvm_get_wall_clock_epoch(struct kvm *kvm) { -#ifdef CONFIG_X86_64 - struct pvclock_vcpu_time_info hv_clock; - struct kvm_arch *ka =3D &kvm->arch; - unsigned long seq, local_tsc_khz; - struct timespec64 ts; - uint64_t host_tsc; + struct kvm_clock_data data; =20 - do { - seq =3D read_seqcount_begin(&ka->pvclock_sc); - - local_tsc_khz =3D 0; - if (!ka->use_master_clock) - break; - - /* - * The TSC read and the call to get_cpu_tsc_khz() must happen - * on the same CPU. - */ - get_cpu(); - - local_tsc_khz =3D get_cpu_tsc_khz(); - - if (local_tsc_khz && - !kvm_get_walltime_and_clockread(&ts, &host_tsc)) - local_tsc_khz =3D 0; /* Fall back to old method */ - - put_cpu(); - - /* - * These values must be snapshotted within the seqcount loop. - * After that, it's just mathematics which can happen on any - * CPU at any time. - */ - hv_clock.tsc_timestamp =3D ka->master_cycle_now; - hv_clock.system_time =3D ka->master_kernel_ns + ka->kvmclock_offset; - - } while (read_seqcount_retry(&ka->pvclock_sc, seq)); + get_kvmclock(kvm, &data); =20 /* - * If the conditions were right, and obtaining the wallclock+TSC was - * successful, calculate the KVM clock at the corresponding time and - * subtract one from the other to get the guest's epoch in nanoseconds - * since 1970-01-01. + * If get_kvmclock() captured both wallclock and kvmclock from the + * same TSC reading, use them for a precise epoch calculation. */ - if (local_tsc_khz) { - kvm_get_time_scale(NSEC_PER_SEC, local_tsc_khz * NSEC_PER_USEC, - &hv_clock.tsc_shift, - &hv_clock.tsc_to_system_mul); - return ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec - - __pvclock_read_cycles(&hv_clock, host_tsc); - } -#endif - return ktime_get_real_ns() - get_kvmclock_ns(kvm); + if (data.flags & KVM_CLOCK_REALTIME) + return data.realtime - data.clock; + + return ktime_get_real_ns() - data.clock; } =20 /* --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF8E548875D for ; Wed, 26 Aug 2026 21:33:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780007; cv=none; b=TgTR3TBYHI96BlI3KtnS7sSlxSgoOsLpQkW0uwkOe/NtgAbq3aZ+A5SYkUXibW45SrSJ+YFOMBcXHO4KilL4MdBfhsSBNuRq3GjNk6JWsKWIU2s8oiEiXfuzM/rxOcHb3/QxMCd92f8YfVYioRU6hVFJwnWZYPrKSdy/gyyMdUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780007; c=relaxed/simple; bh=HjzfaZn7iMtm1a+7Rn0/BRb39wt8D8ryTBsWCam3NPc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RxbHAm517Sf4sfK0ilCYQG/N61BmA4crS+42Jtu1XPTu9t5RljReU0hR8mZZaw9b/dOjCkpEtkoxZhvDmsyxvM8wMjFpPDime4juv7YjjzD7IvrUY9cGFh2cKDhuubNL7RLkrxGim1Vrk5Do9i41DdPbK/7B0KjIyaK4DO3WiPs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=epVsk11b; arc=none smtp.client-ip=209.85.214.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="epVsk11b" Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-2cf7dd9fd91so17067935ad.1 for ; Wed, 26 Aug 2026 14:33:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780000; x=1788384800; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=brsNxXs+z/LAPT3lGQq+uK9uE3dmP7i0Bl49SpdLG/o=; b=epVsk11bPDVoP8V12aCESMuGxIXRylWmW/lFgzmUuw33YzFXY2FnS67l7ywE9i4y5m PhvE8s0c9bdca5KnpQV6Oo0mXpzsGJseuqeEyihVCqjimDmolywKRpNrb9q8tLO9ZD6N ZPmoYIK97BoQVHMIMaz+oH1QxLnfoWcJrmb6AszIDlo/dhuVQ0VYWfWCdMzP2ClzCJJ3 WhUj3CGmobodtIcrSOofZuPP22k3JQRZYcGN2VWLcl88/XcEMlILlj0GseaNZVkKXkfy Ih8VVD53DG6OPHTRNR/PfL+2MU8T5HzBFsDJQ3czfROHyWIKYg2ZQzzCXcwWwdWh9K0W Pd/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780000; x=1788384800; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=brsNxXs+z/LAPT3lGQq+uK9uE3dmP7i0Bl49SpdLG/o=; b=bMshv9uTGtUyRgCPamrnGc4BNmskIavGb/U2q7PMCHhpb/0JtAuzVg71VW0XRUpmO6 mMmO09T7zsxX9eSw6qae6s8pLyNimYcCj/nLqeyPDxt62jNMvOKfnMJqbN1g/nNk6N47 WRg6809wfOw4N7Weuc/Rw23eiV59R3Y26cBnW8MdwJm2k7xVwE/006vfDq+eRSnldihY 60pMsCUa8nCGXkHgyFJhQgmZHGlF4e9og7kgNQCa2Y/YaBrKE1Xv/RiHNKlcono0AX0K 1PvQHdpBxHDtXbwa0T57/b+HoxfTg+ffVPxIQLKMJnL8gkB61MtEkoTb+dri2vQ9+WEm 7a8Q== X-Forwarded-Encrypted: i=1; AHgh+RpdIj/q+gPgowrFX1PHZU7T6k2DpMp3cStwIQEztfWn3CXsC5JhI2tMXci5M4T6QFkdMiU/Gpe2OH4Z4Y8=@vger.kernel.org X-Gm-Message-State: AFuF++mbxx18TzlpsntD625dOdYMO+WGSgmRI1+AGY0GVAgboizWdLtY eQTZy1XvMXfahRRYXk7JK/yatEWcUPipwClwGwRpARUFHb1izZMnqBWKzdSN02fGIVUr8XiBHSm CCrhMog== X-Received: from plbkh12.prod.google.com ([2002:a17:903:64c:b0:2d3:7c58:b100]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:950:b0:2cc:777f:d67c with SMTP id d9443c01a7336-2d707c07e9cmr181434615ad.13.1787779999707; Wed, 26 Aug 2026 14:33:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:53 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-14-seanjc@google.com> Subject: [PATCH v10 13/21] KVM: x86: Fix compute_guest_tsc() to handle negative time deltas From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse The compute_guest_tsc() function computes the guest TSC at a given kernel_ns timestamp. When the master clock reference point (master_kernel_ns) is earlier than vcpu->arch.this_tsc_nsec, the delta is negative. Since pvclock_scale_delta() takes a u64, the negative value wraps to a huge positive number, producing a wildly wrong result. Handle negative deltas explicitly by scaling the absolute value of the delta and applying it to this_tsc_write with the appropriate sign. This is believed to be unreachable in practice; no path has been identified which invokes compute_guest_tsc() with a timestamp from before the vCPU's TSC generation was established. Fix it for robustness, in the spirit of defence in depth. Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b9b7ae965d6d..09435f153b5b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1056,11 +1056,15 @@ static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u= 32 user_tsc_khz) =20 static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns) { - u64 tsc =3D pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, - vcpu->arch.virtual_tsc_mult, - vcpu->arch.virtual_tsc_shift); - tsc +=3D vcpu->arch.this_tsc_write; - return tsc; + s64 delta_ns =3D kernel_ns - vcpu->arch.this_tsc_nsec; + u64 tsc; + + /* Handle negative deltas gracefully (master clock ref may be earlier) */ + tsc =3D pvclock_scale_delta(abs(delta_ns), + vcpu->arch.virtual_tsc_mult, + vcpu->arch.virtual_tsc_shift); + + return vcpu->arch.this_tsc_write + (delta_ns >=3D 0 ? tsc : -tsc); } =20 #ifdef CONFIG_X86_64 --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9797548CD52 for ; Wed, 26 Aug 2026 21:33:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780006; cv=none; b=qmiMbWY93/PT88Q1xeiw7Y+y8k94UHoutLEKRPhAsInMQT0sQGInTJhfNerK9Nc3fpoAzDPbM0sr2jzi7D9jOHuVtuNsvQysWr2/BSOQwF/iID+HrKGBx+u3Ox1PNj+GtHfjC6Du7xSBmJpsGCQumWGF1cu43TcKNWCXIeBXzeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780006; c=relaxed/simple; bh=hanmY8654Xv7t6haK0KTLVbsCwMesm0siIK+5YDQXZM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GVNA+tnhJuFnbBNhFEBeSx4Jelea2Q7bayGV23d7HAeu+j7WHKpnZEVA4zFnw+cqu0ZFNvdRfWxa1tNMO52fWx8eJ5R2euXzVEhexdPpbTJbjwupNeqJaS/ICQKJT2ASheZriNhpSt9Z0nPyxLkrFhJE3YsN5gKKvoB/O5TgeE4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Qh95/L94; arc=none smtp.client-ip=209.85.215.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Qh95/L94" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cbee6bb8408so1673616a12.3 for ; Wed, 26 Aug 2026 14:33:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780001; x=1788384801; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=bkUML0mp/Ew2O+uE+2q6c50/k7kk99NsNilvUx2h/vE=; b=Qh95/L94rBpDjK6nX7nhHZ6bjSMpOUSdzEMpzw7S/7bKaDwIq/OHOgaJsRwVobUvYW 3IqUSVrsDatzJ6DDRTqJZSzPucC5ylMaoUPNVA7+jyKpw98hqVEy2OQRhRa8fao+RuUT ikJNhgKd05ty4pW6JtJINTo6E8XKqBDPTtX8x+Et5MmEy0TsFLXJfyuhH82K1yqwMQlb /3fwHGK4WrdZc+1DisbXD7irGTKhOlPERS2Jcbm5U38xbYqylBcOADXzjVtiOdwXpu1G +u3HfNnxh6YarX/U5wu/kKg3596zxq1SWMvze/wbOy4kRvvfgaisjmlR+lOXSE7VX9lK JUOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780001; x=1788384801; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=bkUML0mp/Ew2O+uE+2q6c50/k7kk99NsNilvUx2h/vE=; b=Ox13GFQekX8hsNpInCSYEEjT0vHYgp9nODoTlWt11yaBh/G9IZxeI7yQB2oYabi/hb 25L+6C+jBRKIgCk8kTe2pGHsmdFS26ZPZ+qrHMLLcQnHZg6oJV7yVSoCXoETpCVZasLB RCY/sCa37vvLdHH7YrncJTYksOr6sw7iMeMcTJsokfHA7gpWe7xZykhIu9xZ+yGbo8Hj J1iO0WPS+ExINhddgag4HGP6KD8CDGh203fDjrPEzUl9BkIKYNL7Z5HKhInA84d6Rss3 j8KP41jdeloZUibb2Vzk37Jl6SVnDLyqj8o1iluaswIdAvi73ATER+4/kM9C/YAulxhO SzDg== X-Forwarded-Encrypted: i=1; AHgh+RrsOLWoie2lfeDgyXCprDCsFHB1x1Fv1ygvrKqKhn3afrUNdr36RTy7+OHBZlz6Iay0ulYqjYCVJI2yp7g=@vger.kernel.org X-Gm-Message-State: AFuF++lw71HqZbVE75TF8IY3zu1Ur+10xqhq8n491qLZ99NEkZX0BY0r lTii14UwjNao2C45hN2aUQvtqQofmlRBjYIG1SW2vO+KAsBXr3aYT6bCWW0lkm/jBZICP0q7L8x 0Ls+luw== X-Received: from pgcm9.prod.google.com ([2002:a63:7109:0:b0:c96:8ff3:53b0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:7287:b0:3cc:9620:5816 with SMTP id adf61e73a8af0-3cf8282617dmr17682345637.8.1787780000771; Wed, 26 Aug 2026 14:33:20 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:54 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-15-seanjc@google.com> Subject: [PATCH v10 14/21] KVM: x86: Disable preemption, not IRQs, when getting TSC+freq pair From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Disable "just" preemption, not IRQs, when reading the TSC+frequency pair to update guest time, as disabling IRQs to protect against task migration is overkill (though it's *extremely* hard to see that it's overkill). Disabling IRQs was added by commit 18068523d3a0 ("KVM: paravirtualized clocksource: host part") before there was any coordination with timekeeping (presumably disabling IRQs prevented the kernel from completing a software- induced frequency change). After the coordination and locking was added, commit c09664bb4418 ("KVM: x86: fix deadlock in clock-in-progress request handling") moved the locking and coordination out of IRQ protection, and thus made disabling IRQs pointless, except for protecting get_cpu_tsc_khz(). And while cpu_tsc_khz is written only from IRQ context, and the *extremely* confusing double IPIs sent by __kvmclock_cpufreq_notifier() to update the per-CPU frequency make it seem like they would require readers to disable IRQs, it is safe to read and consume cpu_tsc_khz (via get_cpu_tsc_khz()) with IRQs enabled. The per-CPU variable is specifically written only in IRQ context to ensure hotplugging a CPU wouldn't write cpu_tsc_khz with a stale value (because apparently disabling IRQs would be too simple?!?). As for the double IPIs in the frequency notifier, both IPIs are red herrings. The actual sequence that ensures KVM updates guest time with the new frequency is that the first write is completed *before* the notifier sets KVM_REQ_CLOCK_UPDATE for all vCPUs that last ran on the target pCPU. The first write is done via IPI to adhere to the above rules, and the second IPI is sent purely to kick any vCPU that happens to be running on the target CPU out of the guest. I.e. the second IPI writes cpu_tsc_khz out of pure KVM laziness: it saves having to define another IPI callback. In fact prior to commit 8cfdc0008542 ("KVM: x86: Make cpu_tsc_khz updates use local CPU"), KVM did indeed use an empty callback to ack the IPI. As for why it was deemed cleaner to abuse tsc_khz_changed()... Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 09435f153b5b..31fa3c941b49 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1799,7 +1799,6 @@ static void kvm_setup_guest_pvclock(struct pvclock_vc= pu_time_info *ref_hv_clock, int kvm_guest_time_update(struct kvm_vcpu *v) { struct pvclock_vcpu_time_info hv_clock =3D {}; - unsigned long flags; u64 tgt_tsc_hz; unsigned seq; struct kvm_vcpu_arch *vcpu =3D &v->arch; @@ -1824,11 +1823,14 @@ int kvm_guest_time_update(struct kvm_vcpu *v) } } while (read_seqcount_retry(&ka->pvclock_sc, seq)); =20 - /* Keep irq disabled to prevent changes to the clock */ - local_irq_save(flags); + /* + * Ensure reading the TSC+frequency pair is done on the same CPU. When + * NOT using the master clock, the TSC frequency may vary between CPUs. + */ + preempt_disable(); tgt_tsc_hz =3D (u64)get_cpu_tsc_khz() * HZ_PER_KHZ; if (unlikely(tgt_tsc_hz =3D=3D 0)) { - local_irq_restore(flags); + preempt_enable(); kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); return 1; } @@ -1863,7 +1865,7 @@ int kvm_guest_time_update(struct kvm_vcpu *v) */ vcpu->last_guest_tsc =3D tsc_timestamp; =20 - local_irq_restore(flags); + preempt_enable(); =20 /* With all the info we got, fill in the values */ =20 --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF99348EBFC for ; Wed, 26 Aug 2026 21:33:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780008; cv=none; b=mL4+Kobq6+xbFfpa5zBCIB5l+ltkkl3V4zDIHBIV/ulyAT74UzLA0FFUYz5MhSxZucAajN2G88j1PTdqW6GGGHAGYM0HTFLnGkECOFD9SD8DYiUjOBsxymtL4fhmEuwA8pzLh22IMqueHdcm7X9a+0O0pBivanVLFV1w9HsyTqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780008; c=relaxed/simple; bh=JVai9/GonBe+qa9+c+N+2jVZBOVuhzhQjhyIBgD0mFk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=I5FHoxwvIGsylAe1J8M3xVo35Ib2Ih7UvHz9udZEbHZ4RttA1/c+eVXCQYkGRCPJIGbkjja8LoRvD2db0aKLy0rGx3WtNmZl7Lri48ZZjO6P+s/mZ6B5qq13IHWStyB9MDnH0UwadH+rD/+kUkQIgYt0PVOcPuYmvldNaVel6FI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=eHOmj/uY; arc=none smtp.client-ip=209.85.214.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="eHOmj/uY" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2d6fb956002so19257665ad.1 for ; Wed, 26 Aug 2026 14:33:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780002; x=1788384802; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=ezFSwGzdymoOq5J7PeW75j7ShifMhMEkSL+xAA8ccGM=; b=eHOmj/uYemQyLDnhzeAO0VxtF8iFKBrrj9CoWbFh6PAggzFSRFLhuS7yTHZd/TfguT yoULpa6G4KulsLHlo7PTA7HSVyCus8BDE+ciAZ5DFWXtWQRikLXSFhBBx3Yk5P5SMIiX Q0Hpj9CAQSAbf1hvvF87jG4I2IRsrL6COg4TAbVZedfVcM8sypDBE63FqgEZOh1Lu5MN NT4DbP2U06WUZpvtS+mkP19KxlPAmJoTOgEWnTKkp4gUvCO7ebhh32osbeh5O9KvidEY U/1AGpWEq+ZvROA5f5Tmy2RDVX7fvqndMHN4gkv+z7Lfw0r0McNCkEj+tvBp9T7zoBSF d+4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780002; x=1788384802; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ezFSwGzdymoOq5J7PeW75j7ShifMhMEkSL+xAA8ccGM=; b=KoJm5pSkSnlJe0UPccLdIz1gUJhegH8ZHTNEGOrVLKZy9mgOWkS3gkgvxfZw90eqKX E+gw8yYGpdqZOuIzlLBfZ+86KcD6uRH0eNC3c6VYJFzGTySMt4HKjFWNafGqMQDhefNC 7hZ+ZYhCjApuUu9BuubGBpWqck45nQ13nwta4XWbE1kLarhDR9H4Iqj1Mlb++U0voUCQ RvGChdWJEaC7gAEzig3/cTcG1k8v17RCebKj6Z8yiEIrw71bbkBVbkbK79BtpYsWeSTE WmkkxGJznQoJwCXS2HsihMvEs5Up+8k/0ZnG7M0mAXxC+y13S7ggY/8b38ciEyZSw1Fe haBQ== X-Forwarded-Encrypted: i=1; AHgh+RqN6ymjp+sDIm5nMPjBT18r+cYA7yQIwleWhVuntXK5NCk8gbg3I5MlnsKSByuqyFfZvwY20fcZGe195is=@vger.kernel.org X-Gm-Message-State: AFuF++me6WCZFFtXqySXRFYNlb0FqBHor/7tOiS2b/LKZ5TzWbLWC6de feRIrj6HF5Iv6AE/8Vf2ivj/6pTmTmhyHTr/2mmBs15jHLYfEPg1h109+99sB9VbRNlGtZMU/KF vx0zodg== X-Received: from plcx11.prod.google.com ([2002:a17:903:cb:b0:2d6:e968:e012]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2341:b0:2c7:ebfb:618f with SMTP id d9443c01a7336-2d707c1020emr164924215ad.14.1787780001912; Wed, 26 Aug 2026 14:33:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:55 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-16-seanjc@google.com> Subject: [PATCH v10 15/21] KVM: x86: Make master clock logic in guest PV clock updates 64-bit only From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Wrap kvm_guest_time_update()'s entire use_master_clock block in #ifdef CONFIG_X86_64 and provide a hardcoded-to-false variable for the 32-bit path, as use_master_clock is never true on 32-bit (host_tsc_clocksource is only set under CONFIG_X86_64) Keep the local "ka" variable outside of the 64-bit-only code as it's also used for Xen code, but tag it as potentially unused since it's not used on 32-bit kernels without Xen emulation. For all intents and purposes, no functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 31fa3c941b49..1614718a7d0b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1798,22 +1798,21 @@ static void kvm_setup_guest_pvclock(struct pvclock_= vcpu_time_info *ref_hv_clock, =20 int kvm_guest_time_update(struct kvm_vcpu *v) { + struct kvm_arch *ka __maybe_unused =3D &v->kvm->arch; struct pvclock_vcpu_time_info hv_clock =3D {}; u64 tgt_tsc_hz; - unsigned seq; struct kvm_vcpu_arch *vcpu =3D &v->arch; - struct kvm_arch *ka =3D &v->kvm->arch; s64 kernel_ns; u64 tsc_timestamp, host_tsc; + + /* + * If the host uses TSC clock, then passthrough TSC as stable + * to the guest. + */ +#ifdef CONFIG_X86_64 bool use_master_clock; + unsigned int seq; =20 - kernel_ns =3D 0; - host_tsc =3D 0; - - /* - * If the host uses TSC clock, then passthrough TSC as stable - * to the guest. - */ do { seq =3D read_seqcount_begin(&ka->pvclock_sc); use_master_clock =3D ka->use_master_clock; @@ -1822,7 +1821,9 @@ int kvm_guest_time_update(struct kvm_vcpu *v) kernel_ns =3D ka->master_kernel_ns; } } while (read_seqcount_retry(&ka->pvclock_sc, seq)); - +#else + const bool use_master_clock =3D false; +#endif /* * Ensure reading the TSC+frequency pair is done on the same CPU. When * NOT using the master clock, the TSC frequency may vary between CPUs. --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D9DD48CD68 for ; Wed, 26 Aug 2026 21:33:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780006; cv=none; b=KLVlUjErBp5sHHFYGpALZVPSvJe7D070vtiI8hEtGAItiC8uMsJbRRLMGcL8Qq6oybzLkkDGwz/4xbf5ylvBLt2wJqERTwiFTrjhVAxOQeqWSg7o1nex7VZGYQ28k1d6mSWPC5y/hZ4FUqN4uG2WgsOiJdqo5Xy3fNUX6jmvZa4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780006; c=relaxed/simple; bh=NmC/TIbNurvkOqlVrSByb0DS/AOuda9r/vWVO+SbpRg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UyQfzpMj/KMf6kvCA6C8PGNSc2pWuAvzxraZ4l32v2W4YboRZHugvp2wij6nL/e5LJ0pIQXdjU11DuucSUxRxIYdK6YQgik2P5tswdGtVKmiuxsOJBJPjEx4c274sNMbSynsuGNaEvVYCGhxlrIAEQ5RLtsiJguWj3xFqE6jCog= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gGoiNIQS; arc=none smtp.client-ip=209.85.214.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gGoiNIQS" Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2ce8a76df2dso544805ad.2 for ; Wed, 26 Aug 2026 14:33:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780003; x=1788384803; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=Et98QGpcgzG8rtiMrYGDfYUkbdGZQe7+45/h5KmEwG4=; b=gGoiNIQSjRKvABofi8EUGwc84Y2nudGIrpt/fRdUMr8Qd1ce+z1BzRM77CtKwj3xLC YRIWEPCqqCO/QYtOWIlmZ+F0JyxXdzxP83VMsikL/dt2SuDJVnbz5au3/L4Ju9QxfX/9 fxXQjcDMtD/p6/EiQB55n/ZtIElTNjxgcGV8dlLrj2kaEGDv+N7fInkWS82xukzuLI17 yIUz2Se6hoX4HWSXPnK59cp2NDvBX5Z/y8QVtCVyivzURqwCvawgwkdXeQ/IommE6Kbl Cbxdlpfv4Q3bl98v5qU02JSK2+DL4ax34uu7U6w+BKZFLxykx0codvl/ka1SwzHzggF7 R70w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780003; x=1788384803; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=Et98QGpcgzG8rtiMrYGDfYUkbdGZQe7+45/h5KmEwG4=; b=oVzuGfCOb/gMFBJQIL9CDK9qe9pdsVzgcvAUBACGToKz7LtsmT3K8lRdBzGtm5hv+g c+tgk1k8vrpqmbsKQLEJHYkyj6hDG6PwSaOATRTCgffkk+LYHv3t6DpqFeKdvbPaqMtn YLve3BR+PwasyuU4Jm5/BflTFj8IRbBWBGyaFX8sb0HMsg5SaL1buQM9/lNU/eIEZwRW 2ZA45g+0NEkedV7UxfZQiO2ZWw1Eg7u1p5aHxusXyFLYZm7QUzzpm0f5PHiqOZBIgGKk JOhVkCIqRdG5Ep4R4ubfv/2Z8WAgBBxMY2QOFazroD1UPNtu5bIrjy5xn5e/WErpUw7o hixA== X-Forwarded-Encrypted: i=1; AHgh+RoetHNKfCmH+nlpuUpV80lDC38aDypHIEbt7zzgQbkm6FJKT+Cp+HojnPk/vAnfLzUnBvaPOkDyWVKYE1s=@vger.kernel.org X-Gm-Message-State: AFuF++kQWlRXqiPjJh+fRuTCN65nHjkfKrgRxFZ2j3C9xNZBBlmO9dJ3 zZQec9iBpvwpENZMlvkWoSiCvkAs2tyYYx1rdz2YiLaeZ4bpOSOt603s9+vrzMPvewroijwKwNY lArt0tw== X-Received: from plblc14.prod.google.com ([2002:a17:902:fa8e:b0:2d5:62cd:db79]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f786:b0:2c9:bf82:dd11 with SMTP id d9443c01a7336-2d707a70d9fmr186756105ad.7.1787780003027; Wed, 26 Aug 2026 14:33:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:56 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-17-seanjc@google.com> Subject: [PATCH v10 16/21] KVM: x86: Upscale TSC to "now", not master clock when updating PV clocks From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse When doing TSC upscaling in software, e.g. when the guest TSC frequency is configured to run faster than the host without hardware support, always advance guest TSC to "now", not to the master clock's reference values, which represents some time in the past. In master clock mode, there are two points of time which need to be taken into account. First there is the master clock reference point, stored in kvm->arch.master_kernel_ns (and associated host TSC ->master_cycle_now). Secondly, there is the time *now*, at the point kvm_update_guest_time() is being called. With software TSC upscaling, the guest TSC is getting further and further ahead of the host TSC as time elapses. So at time "now", the guest TSC should be further ahead of the host, than it was at master_kernel_ns. Not taking that into account means KVM was advancing the guest TSC only by the amount appropriate for the snapshot taken in the past, *not* to the current time (which is what RDTSC is supposed to return). Signed-off-by: David Woodhouse Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1614718a7d0b..6d9ff7eafa5c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1798,12 +1798,11 @@ static void kvm_setup_guest_pvclock(struct pvclock_= vcpu_time_info *ref_hv_clock, =20 int kvm_guest_time_update(struct kvm_vcpu *v) { + u64 tgt_tsc_hz, tsc_timestamp, host_tsc, master_tsc, master_ns; struct kvm_arch *ka __maybe_unused =3D &v->kvm->arch; struct pvclock_vcpu_time_info hv_clock =3D {}; - u64 tgt_tsc_hz; struct kvm_vcpu_arch *vcpu =3D &v->arch; s64 kernel_ns; - u64 tsc_timestamp, host_tsc; =20 /* * If the host uses TSC clock, then passthrough TSC as stable @@ -1816,10 +1815,16 @@ int kvm_guest_time_update(struct kvm_vcpu *v) do { seq =3D read_seqcount_begin(&ka->pvclock_sc); use_master_clock =3D ka->use_master_clock; - if (use_master_clock) { - host_tsc =3D ka->master_cycle_now; - kernel_ns =3D ka->master_kernel_ns; + if (!use_master_clock) + continue; + + if (!kvm_get_time_and_clockread(&kernel_ns, &host_tsc)) { + use_master_clock =3D false; + continue; } + + master_tsc =3D ka->master_cycle_now; + master_ns =3D ka->master_kernel_ns; } while (read_seqcount_retry(&ka->pvclock_sc, seq)); #else const bool use_master_clock =3D false; @@ -1885,8 +1890,18 @@ int kvm_guest_time_update(struct kvm_vcpu *v) =20 hv_clock.tsc_shift =3D vcpu->pvclock_tsc_shift; hv_clock.tsc_to_system_mul =3D vcpu->pvclock_tsc_mul; - hv_clock.tsc_timestamp =3D tsc_timestamp; - hv_clock.system_time =3D kernel_ns + v->kvm->arch.kvmclock_offset; + /* + * If the master clock is NOT in use, the reference time placed in the + * hv_clock is "now". If master clock is in use, the reference time is + * the master clock's snapshot from some time in the past, not "now". + */ + if (use_master_clock) { + hv_clock.tsc_timestamp =3D kvm_read_l1_tsc(v, master_tsc); + hv_clock.system_time =3D master_ns + v->kvm->arch.kvmclock_offset; + } else { + hv_clock.tsc_timestamp =3D tsc_timestamp; + hv_clock.system_time =3D kernel_ns + v->kvm->arch.kvmclock_offset; + } =20 /* If the host uses TSC clocksource, then it is stable */ hv_clock.flags =3D 0; --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4512D490BF0 for ; Wed, 26 Aug 2026 21:33:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780009; cv=none; b=Pscc3iysagI4Nf1ZJVZ3xY8DJfQl3qeLsNPK/W1wRKn/S/dSoGHBzz5QA9SAAkgy3BjH2RnmTbe8Id8SbuLgqkHX4XKheiETtlkmhiqLCj4CmbV57cpB173d5YirJ5ZnaDv0MWSDBHvufvHuDjlcrg4QQYJs6SBVhdvx5Q2C0v4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780009; c=relaxed/simple; bh=UD4V+P+sJ4ggxTSMMhUjRA0kQeqR4BaRSMZh0omtQgI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fDaNIgR1w9z67CaC+xvkfToiVvffM3hP3pCPz+AExuuJISnU7QDbahofheZAKGihOwk+GUiyWjLbx45amuNUlueq3GHNqOFXBxpXRvlXaX/p/YwVtvYSPVlzyA+q2h4eaTgmCkvu0Ka2in+fsSMAFcFk+dNqbV5TuFt8lXB3iWg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ky8NV/l3; arc=none smtp.client-ip=209.85.215.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ky8NV/l3" Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cc18ced1a5aso2640599a12.1 for ; Wed, 26 Aug 2026 14:33:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780004; x=1788384804; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:reply-to:from :to:cc:subject:date:message-id:reply-to:content-type; bh=p0wLAh+oBJxC4Mzz9xo11F85UOi0qCTAzexRNcTkKnc=; b=Ky8NV/l3iW4/rBJcU5STadFeEPJkraBeUMYurIZsafGa/gDTsbgu5tRbmEUUWjweiV 7uJiz/KBGDFGTTbwTPAcOYB54j2Thl//10JI9HKmmEJSXL30v9YNk/G2MmW9JGsKxj0q pbTwJC9DWZLij/3Hk42NuzpnP9R5RH6BVuvuubNoTtcL2UXhMBNHeikDD1yi43uFKWnw 8gfLvg0Lbvgk4xvVdMwk8YZJ6PFpZlenkIthBv3VhAMobo0xZ2zYBx6Xv6v8q5R/FSIH 46+mbGNI84cdq3VJdIkS/0nWCXSOE3hWA85Q09U59BNE/nB5jGT6sdrN2q4r4ro8NxY+ F0tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780004; x=1788384804; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=p0wLAh+oBJxC4Mzz9xo11F85UOi0qCTAzexRNcTkKnc=; b=EQBoZBP6CAJZOrJSDsguuorUE+heu5AaxH+zeP34LOAkR4/iMsxSCIkYcd/qhryj5W E61mYaTllPf9hksSEEagqZE7snXMzUnLhZgfqbodfD4Ww8pNvgpsZs0KpJFaG3j4xkcm FkuS7aFljpR5r7ubaMHJsxndzQnNqqgVFNGPLGRaMPvDgG9A2oPTIGzooe3NIN6C3Hjm YexB1NkYklWvzqZY0qv1tJSdRFzufW+yG1OLijaZswN1VhPrPW+feBV3LNo7rzTxtn2x s2cE9GKyEVmdazs7ZSd1L1m4E7yM/1IeY8XcBVMri45/ltqFZcmt/wjzsCoMXi7NRf5L L+DA== X-Forwarded-Encrypted: i=1; AHgh+RraPi8XqTAzNwuk3H2xHQlNaOUZEzqvYZuiPc4eaDArc4mQ5p1uD6t9oUVRsYo1Sqh3lwhUSUOSGWHEqxM=@vger.kernel.org X-Gm-Message-State: AFuF++mKIx3RbiEBhMCMUDnkQzuaknQbgG27SiCcTdQeK6JJDgjQ5g1B xke/nCaGSqT7DNHnIOCeOTIYziFANMjoK4q4Z6njC2hwPLKgrt7h5QdcO6ZIC7EDbM0NOS76zdx Hc+gYfQ== X-Received: from pgcl15.prod.google.com ([2002:a63:700f:0:b0:c85:c772:c6a5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:4e01:b0:3c4:1493:6822 with SMTP id adf61e73a8af0-3cf84e51465mr19713083637.18.1787780004107; Wed, 26 Aug 2026 14:33:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:57 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-18-seanjc@google.com> Subject: [PATCH v10 17/21] KVM: x86: Simplify and comment kvm_get_time_scale() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse The kvm_get_time_scale() function was entirely opaque. Add comments explaining what it does: compute a fixed-point multiplier and shift for converting TSC ticks to nanoseconds via pvclock_scale_delta(). Rename the local variables from the cryptic tps64/tps32/scaled64 to base_hz_u64/base32/scaled_hz_u64 to make the code self-documenting. The "tps32" name stood for "Ticks Per Second" but was misleading since it held the shifted base frequency, not a tick count. No functional change. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 55 +++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6d9ff7eafa5c..f7d9dfe6046d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -940,32 +940,57 @@ static uint32_t div_frac(uint32_t dividend, uint32_t = divisor) return dividend; } =20 -static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz, +static void kvm_get_time_scale(u64 scaled_hz, u64 base_hz, s8 *pshift, u32 *pmultiplier) { - uint64_t scaled64; - int32_t shift =3D 0; - uint64_t tps64; - uint32_t tps32; + u64 scaled_hz_u64 =3D scaled_hz; + s32 shift =3D 0; + u64 base_hz_u64; + u32 base32; =20 - tps64 =3D base_hz; - scaled64 =3D scaled_hz; - while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) { - tps64 >>=3D 1; + /* + * This function calculates a fixed-point multiplier and shift such + * that: + * time_ns =3D (tsc_cycles << shift) * multiplier >> 32 + * + * Where tsc_cycles tick at base_hz, and time_ns should count at + * scaled_hz (typically NSEC_PER_SEC for a TSC=E2=86=92nanoseconds conver= sion). + * + * The multiplier is: (scaled_hz << 32) / base_hz, adjusted by shift + * to keep everything in range. + */ + + base_hz_u64 =3D base_hz; + + /* + * Start by shifting base_hz right until it fits in 32 bits, and + * is lower than double the target rate. This introduces a negative + * shift value which would result in pvclock_scale_delta() shifting + * the actual tick count right before performing the multiplication. + */ + while (base_hz_u64 > scaled_hz_u64 * 2 || base_hz_u64 >> 32) { + base_hz_u64 >>=3D 1; shift--; } =20 - tps32 =3D (uint32_t)tps64; - while (tps32 <=3D scaled64 || scaled64 & 0xffffffff00000000ULL) { - if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000) - scaled64 >>=3D 1; + /* Now the shifted base_hz fits in 32 bits. */ + base32 =3D (u32)base_hz_u64; + + /* + * Next, shift scaled_hz right until it fits in 32 bits, and ensure + * that the shifted base_hz is strictly larger (so that the result of the + * final division also fits in 32 bits). + */ + while (base32 <=3D scaled_hz_u64 || scaled_hz_u64 >> 32) { + if (scaled_hz_u64 >> 32 || base32 & BIT(31)) + scaled_hz_u64 >>=3D 1; else - tps32 <<=3D 1; + base32 <<=3D 1; shift++; } =20 *pshift =3D shift; - *pmultiplier =3D div_frac(scaled64, tps32); + *pmultiplier =3D div_frac(scaled_hz_u64, base32); } =20 #ifdef CONFIG_X86_64 --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E42B1494812 for ; Wed, 26 Aug 2026 21:33:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780012; cv=none; b=c0DuJVI5xB20Lqxs/JfWtHt4rX/XhAFT1qTcuPgcjFlaQRuc2gzK/QRbYJkjTIa1F1J4o+Sm/i74gepVMpHXpvTM6DK9wUH//Rz24ggELl7GEjVxa992FaE9mZM1ea1JJdXk+tDwKPaoMswflJ/EzdQDuBoP5kiW44Pn70yLqhw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780012; c=relaxed/simple; bh=5o1ucxtr3buA18SWRKhpY2bxC7WdEGghBR5g0Rxi3wM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PavVOD6xCnl+YAgdyNwDx4Q61VoeVUqRyVeeNIihjNgAh4dJsOhn8j8e6IIAQgA3wsU2ePYsJr+C7GPaGtqbjXS68TxlsBhHqaKXZBCsaoEY6Gg50s88RC3x3U3codCbH5jU5hxBCyvje29c0Ur3KMp4w1nxCIFTLHkYP8Yy9LU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ph21pb/4; arc=none smtp.client-ip=209.85.214.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ph21pb/4" Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2d63c16fc5bso30925385ad.1 for ; Wed, 26 Aug 2026 14:33:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780005; x=1788384805; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=UiH12BgSBfe0UjZRWynvrUBvNJGP2GUeAXjWBAJFBLE=; b=Ph21pb/4O1HCECZpP8VXLQV7yUva4/iJFKHjKqL9LjSGMteOsX0LTEFsZeZ2o4uCza DYR5SxD562TOQObS6aHyWN7XL90Uw44bpgCVytwnZ9l7R8nlYDJyAZSogomftgf+hHkP fGBC4fPkgNXBsvx+iN9rfTIPVnyAE0hQ6AKojXrGBV8Ud4wiOiIkz9W5jMa7tdMCLZSx n5Jb6QwpJ+swGhz64/qUmWPlqixIacrccBL+TO2lgiIPYkn/ECuEgzvWVoevPMDIvtLK y8MS5TtqQ43ij0q8ucyikJcQXfJ00PwMuKv85g7E1DUJxW4oS7cpfLEmIR1eHqF+LwUh ps8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780005; x=1788384805; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=UiH12BgSBfe0UjZRWynvrUBvNJGP2GUeAXjWBAJFBLE=; b=h86CQ64EpIayPfNtZUb2xbvk1B2zG5eVNk5USKnBHzTG5gOOOkdC6AS+txklT4/Ndv MXIowCskPegDR9u0TXzq4Hbw5pJkQ5pUUYmLzXtDiSe4jlM0jmVobKeMAC0gC8950L3q UfZ7QHAQrYGisn6fpQHuMy5DDeEYZxnjIviA4xs6iTM3kp5Myhx/kYVPR62A1w3AKUtq EPiaffMooNs7eqHLA0g/5aqAWE8Xz0YMTHFYc9Rqo++eG2ctoEKT6xNI4NRcnk3rGHBS fFiBh3QdrCMzyqAW9Ucsv2aQgwyx7lBU3IgyuZ15FjlCpu5VdRJrOpIIAL6xhZ0/j+hT 2k4g== X-Forwarded-Encrypted: i=1; AHgh+RrX1WwnkmPCDB6jpL/gdJc/uB7h2tqjcQrHjo3CpUhTkdiLP24JVkUP0U7VWeqnWb2pVrf6ymYtB9PyPhs=@vger.kernel.org X-Gm-Message-State: AFuF++mTrpV0qMIv7SY4vMdk7ZFIw/k+85e6DWRKw8zMjz+x7hoFLppI 2WBFLMKsxvP9rYguZIc4crCciuFG+F7tXs47FLJY9ez+ZTXlBnJ/QRH9JCci44M06uT2vJvEtJ7 wjjAFMg== X-Received: from plbmg8.prod.google.com ([2002:a17:903:3488:b0:2c7:f626:ec80]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3810:b0:2ca:d91d:d3a7 with SMTP id d9443c01a7336-2d707aea3aamr193928235ad.10.1787780005172; Wed, 26 Aug 2026 14:33:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:58 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-19-seanjc@google.com> Subject: [PATCH v10 18/21] KVM: x86: Remove implicit rdtsc() from kvm_compute_l1_tsc_offset() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Let the callers pass the host TSC value in as an explicit parameter. This leaves some fairly obviously stupid code, which is using this function to compare the guest TSC at some *other* time, with the newly-minted TSC value from rdtsc(). Unless it's being used to measure *elapsed* time, that isn't very sensible. In this case, "obviously stupid" is an improvement over being non-obviously so. No functional change intended. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant Signed-off-by: Sean Christopherson --- arch/x86/kvm/msrs.c | 3 ++- arch/x86/kvm/x86.c | 11 ++++++----- arch/x86/kvm/x86.h | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c index 66fa7140d65d..22ceb39d41f6 100644 --- a/arch/x86/kvm/msrs.c +++ b/arch/x86/kvm/msrs.c @@ -1631,7 +1631,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) if (msr_info->host_initiated) { kvm_synchronize_tsc(vcpu, &data); } else if (!vcpu->arch.guest_tsc_protected) { - u64 adj =3D kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_o= ffset; + u64 adj =3D kvm_compute_l1_tsc_offset(vcpu, rdtsc(), data) - + vcpu->arch.l1_tsc_offset; adjust_tsc_offset_guest(vcpu, adj); vcpu->arch.ia32_tsc_adjust_msr +=3D adj; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f7d9dfe6046d..51a8250e758b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1155,11 +1155,12 @@ u64 kvm_scale_tsc(u64 tsc, u64 ratio) return _tsc; } =20 -u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) +u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 host_tsc, + u64 target_tsc) { u64 tsc; =20 - tsc =3D kvm_scale_tsc(rdtsc(), vcpu->arch.l1_tsc_scaling_ratio); + tsc =3D kvm_scale_tsc(host_tsc, vcpu->arch.l1_tsc_scaling_ratio); =20 return target_tsc - tsc; } @@ -1321,7 +1322,7 @@ void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *= user_value) bool synchronizing =3D false; =20 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); - offset =3D kvm_compute_l1_tsc_offset(vcpu, data); + offset =3D kvm_compute_l1_tsc_offset(vcpu, rdtsc(), data); ns =3D get_kvmclock_base_ns(); elapsed =3D ns - kvm->arch.last_tsc_nsec; =20 @@ -1370,7 +1371,7 @@ void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 *= user_value) } else { u64 delta =3D nsec_to_cycles(vcpu, elapsed); data +=3D delta; - offset =3D kvm_compute_l1_tsc_offset(vcpu, data); + offset =3D kvm_compute_l1_tsc_offset(vcpu, rdtsc(), data); } matched =3D true; } @@ -2627,7 +2628,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cp= u) mark_tsc_unstable("KVM discovered backwards TSC"); =20 if (kvm_check_tsc_unstable()) { - u64 offset =3D kvm_compute_l1_tsc_offset(vcpu, + u64 offset =3D kvm_compute_l1_tsc_offset(vcpu, rdtsc(), vcpu->arch.last_guest_tsc); kvm_vcpu_write_tsc_offset(vcpu, offset); if (!vcpu->arch.guest_tsc_protected) diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 0f5919b092e4..1d9a66048b01 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -335,7 +335,8 @@ u64 kvm_scale_tsc(u64 tsc, u64 ratio); u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc); u64 kvm_calc_nested_tsc_offset(u64 l1_offset, u64 l2_offset, u64 l2_multip= lier); u64 kvm_calc_nested_tsc_multiplier(u64 l1_multiplier, u64 l2_multiplier); -u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc); +u64 kvm_compute_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 host_tsc, + u64 target_tsc); void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 l1_offset); =20 static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4C4EA48B38F for ; Wed, 26 Aug 2026 21:33:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780011; cv=none; b=cdZvx8aScGTRVWe/EBS4YT46015LJKymculLHmLzEq4V/98rnZANNjSzw/bn2CKEo2lKNOZvfVj/Ugb0CVrULvlm3rFwfoyhGJIYL0xZVEEkKhM3d1U+txWN4b2vUDevT8Dyt5gilYhIWi56Zgt4x7EJ5jPlKXh6zgbA3Y8f+l0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780011; c=relaxed/simple; bh=x9kqdAvWcySW/UFriv/XVSVV3wGHtlM1R1TTiEGvaKc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Md//l8V3Wnza6WTvtyFKbNVrxcUM+Yq4pEaD3/TrY3n0seAlaeOorA9l3W6GSqukCt3LbWV+vSbVj5816w8De78jvZFAX4LBllpjqKByYSvve7hBParUkmTUs4sqiJ/25MhXCQSJ0Lx3RetY4klYBtxs9MMb1FSiqzrFg8oJOwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=a9raJv3u; arc=none smtp.client-ip=209.85.215.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="a9raJv3u" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cc1bbd7d6e6so1629842a12.3 for ; Wed, 26 Aug 2026 14:33:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780006; x=1788384806; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=hxtXNEiVVHLTLVeOZPmDmbDA/5ZmZ/osRSTz6+H5aOI=; b=a9raJv3uywLEr3mXX36hLeUFvym8VC7a7qo212DBcpJKiXUnv6jx32p0c1hc1ErjXR OZzXJ8TDdLWKe5azUuDFfaqsjgIv+qykEAePUeKiwnDhubXYyVe3J6s/v5TRBzPUCJOZ ieqq7ynBEioOeSIZP9Mwty/0ZWpqP0KGSDHvaemZCHtHTroOd2SrmAfI9wBa6dx64xzN RelR/hSVsAggpgtYra1EL27ULVeQtHc7ExpmJl9+3xr5yPFcDzrW6w1zAzCXre+iSqn8 iZ5IoMkNLt8ad19YHizBNqaDpUE9K+B7JnGMMd025LT9l5P7QvCpSTy2vGhKNq3ZDnZV GwUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780006; x=1788384806; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=hxtXNEiVVHLTLVeOZPmDmbDA/5ZmZ/osRSTz6+H5aOI=; b=bJ8VaH8x2TUPFyankzIgL7TxeaMswXXWgZdxbeYE7VD8F7eYL5nSuUViF13EiF0Odv Ju10VzRPTRgvNLZpHGyKYfAQ7o1zGhHDvt3xY8U98y1AZtS3/+euXBoro6zjp26AUiuU 1xXTPtnD2I2OnPU7zbi2GzWX6hI0c3Z4MdI6355hBNibCnnaOlXY8eD8TgMQvmvJTPco eE44WXsPJSuj8U53t2ZcWuSChf1mtPEdRABYA8kZwo7q5xrQ9bRwv2zl5PaATNkmeIwL pqNfTerIP3l9vlNOXhJ5f71iq6eXiVC66DoQIlDGZTJQryFbGBMSGESyK8ukWc5r5H1/ ITBw== X-Forwarded-Encrypted: i=1; AHgh+Rp5qTvvKTM4+i3APpJVZit/VtefmXeHS1IR07Gjr7fCbYUMSEIxsKmqmAB20qVH1AuComO2A2s6PCTsgJE=@vger.kernel.org X-Gm-Message-State: AFuF++nzSiCfENbgyaoXrPxWl+HMM8X1fYXoZvLhXGnucWcnAIOmHIqI xTlGparFxz6//bVyGNR6mJ4EI8JSdqklIbB1I0tLzzODwZOxAiVTOUO/9DMHKK5orDBAUY9C99H nJ9mPlw== X-Received: from pfux28.prod.google.com ([2002:a05:6a00:bdc:b0:84a:3bc9:3bcd]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4f92:b0:848:2e3c:9955 with SMTP id d2e1a72fcca58-85371fa599amr17200089b3a.4.1787780006189; Wed, 26 Aug 2026 14:33:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:32:59 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-20-seanjc@google.com> Subject: [PATCH v10 19/21] KVM: x86: Use kernel timekeeping snapshots for getting kvmclock time since boot From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Replace the KVM-private vgettsc()+do_kvmclock_base() timekeeping reimplementation with calls to the recently crafted, generic ktime_get_snapshot_id() interface. This is the first step towards dropping KVM's homebrewed implementation entirely (do_monotonic() and do_realtime() will be converted in the near future). As with KVM's implementation, the snapshot provides both the system time and the raw_cycles (TSC), atomically paired using a sequence counter. The equivalents to vgettsc()'s TSC and HVCLOCK modes respectively are if the clocksource itself is TSC (cs_id =3D=3D CSID_X86_TSC) and if the underlying hardware clocksource is TSC (hw_csid =3D=3D CSID_X86_TSC). In the Hyper-V case, i.e. hw_csid =3D=3D CSID_X86_TSC, if the clocksource couldn't provide= a raw hardware counter value, treat the clock not being based on TSC, which which is equivalent to vgettsc() returning VDSO_CLOCKMODE_NONE. Unlike KVM's current implementation, don't include offs_boot in the atomically-acquired tuple as there's simply no need to do so: the time since boot only changes at boot (duh) and at suspend/resume boundaries. Unless processes aren't being frozen/thawed before/after suspend/resume, which would completely break suspend/resume, TK_OFFS_BOOT can't change while kvm_get_time_and_clockread() is running. And if KVM does somehow try to take a snapshot during suspend, timekeeping core will WARN and refuse to provide the snapshot. This is a step towards eliminating the pvclock_gtod_data private copy of timekeeping state and the associated notifier callback. Signed-off-by: David Woodhouse [sean: separate from other conversions, massage changelog accordingly] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 57 ++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 51a8250e758b..32390b20a2b4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -35,6 +35,7 @@ #include "smm.h" =20 #include +#include #include #include #include @@ -1437,29 +1438,6 @@ static inline u64 vgettsc(struct pvclock_clock *cloc= k, u64 *tsc_timestamp, return v * clock->mult; } =20 -/* - * As with get_kvmclock_base_ns(), this counts from boot time, at the - * frequency of CLOCK_MONOTONIC_RAW (hence adding gtos->offs_boot). - */ -static int do_kvmclock_base(s64 *t, u64 *tsc_timestamp) -{ - struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; - unsigned long seq; - int mode; - u64 ns; - - do { - seq =3D read_seqcount_begin(>od->seq); - ns =3D gtod->raw_clock.base_cycles; - ns +=3D vgettsc(>od->raw_clock, tsc_timestamp, &mode); - ns >>=3D gtod->raw_clock.shift; - ns +=3D ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); - } while (unlikely(read_seqcount_retry(>od->seq, seq))); - *t =3D ns; - - return mode; -} - /* * This calculates CLOCK_MONOTONIC at the time of the TSC snapshot, with * no boot time offset. @@ -1504,6 +1482,29 @@ static int do_realtime(struct timespec64 *ts, u64 *t= sc_timestamp) return mode; } =20 +static bool kvm_snapshot_has_tsc(struct system_time_snapshot *snap, + u64 *tsc_timestamp) +{ + /* + * ktime_get_snapshot_id() cannot fail for standard clock IDs + * (only for invalid/aux clocks or during suspend, with a WARN). + */ + if (!snap->valid) + return false; + + if (snap->cs_id =3D=3D CSID_X86_TSC) { + *tsc_timestamp =3D snap->cycles; + return true; + } + + if (snap->hw_csid =3D=3D CSID_X86_TSC && snap->hw_cycles) { + *tsc_timestamp =3D snap->hw_cycles; + return true; + } + + return false; +} + /* * Calculates the kvmclock_base_ns (CLOCK_MONOTONIC_RAW + boot time) and * reports the TSC value from which it do so. Returns true if host is @@ -1511,12 +1512,14 @@ static int do_realtime(struct timespec64 *ts, u64 *= tsc_timestamp) */ static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_MONOTONIC_RAW, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_kvmclock_base(kernel_ns, - tsc_timestamp)); + *kernel_ns =3D ktime_to_ns(ktime_mono_to_any(snap.systime, TK_OFFS_BOOT)); + return true; } =20 /* --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B3F8489876 for ; Wed, 26 Aug 2026 21:33:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780009; cv=none; b=IeJT9atbbCbJZAt5Kky1BhqHbudXgo+43nyqsqvbn8cCXYtOQ0PMJTMC9JwHI4QMikkSiseEcxhxxaQKgrwRhbQAVWKpwy9fTpzwTnZiqzw1+rMCuh7U77lS7NfF6LZhc6sdh8uPPTszv2G7wt2POaM9IivQWSDCQZL48S+m81E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780009; c=relaxed/simple; bh=J7awBibVXHg+ZynoE1GTv8WNYcoD5LqJUOY9xR1n0xE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pk40Rtvj14VHvrjHNsFFXzuj654tz5t0K0I+/DJFXXokdxPflpqTtwsjw1bUsqOvakwUwe+CbwFbRC1s490ZRu8HnWeD3s2omAv4/YVmXOkQMY67Q5BU51rNCxnYf2NnCkHcAlQgjAZeZjbzl+j/86e7T/S/QS/8XKJv+c0ziJY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aVKQepFr; arc=none smtp.client-ip=209.85.215.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aVKQepFr" Received: by mail-pg1-f200.google.com with SMTP id 41be03b00d2f7-cbb92868263so25418a12.2 for ; Wed, 26 Aug 2026 14:33:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780008; x=1788384808; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=Cl9yrkZ+dGZ9BiHgGOnk9QZOqiTp6OUnu1pYLMOniSs=; b=aVKQepFrPva+opNOkPQuvPBDbvjbdJLW6eh2rcI4ut4ImP31ggpHnIwoh2KHO+l52W ZlFhaOubrS0BgjylYaU54ARLgppnxb8XFujnMp1+esngwj+EG2JaGt8lg0c4MbwJSrRs eHumljUyvCELUlrU3LCyAkWxqF4hEY8xC3skAuveS+U7U6qSPSAlborBsPGIMNXsKiXT JLxEePsZ9pCzEj+n/gC2nnwxLRC8MTDA1xhY86CmVzNIvKez/IDbifcjx24EbtlfqTaA iqj9qGafluVb9tVUk02jByb+OA2iQI2hzXyxlXpUMNbko46X/XlvzNKnpeCcy2lRf60k Vmbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780008; x=1788384808; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=Cl9yrkZ+dGZ9BiHgGOnk9QZOqiTp6OUnu1pYLMOniSs=; b=Tu4t0YG/qX91U/jnGYeej1NJetVTPl+/EaCAuAi88Wl4ganZCpN85PS0lL+791nrxG 7BlzJ7xu8t0fWum2c+TXBIPqhQadv98eiINnoU4jm+4Ms+mO49/zAtQMeK3JW/P1SNbd KqbLLPGFPZyUGaXwM/hzHuEtYSpIelEkpF5TZhEzK+TSYQtfBwViGwjNzYsUd828p/1G kR6tOWCi7vJXblXVZQn7+CO9ngxovCzC8APtSzBUC1fxEGEbp3pw9imVr8i8pDw3LmRW Y4cPmtMqmovEAMoTqwqCOkZ3KrGIULxc0pH7eMbCcvd5SqCaXxDdTZHCGzFhz0XxZBe5 GqlA== X-Forwarded-Encrypted: i=1; AHgh+RoUzhKpF3dU8I0MkA9iER0OYen4girvDe4HZeYVImCskJByht18oI9hx/3W7hcVHX8v6u+Mlj5WpfQJF3M=@vger.kernel.org X-Gm-Message-State: AFuF++mO51B48dsz075XIsLZbXXbbDovy3B+TalEDektn3bUOcqUHQGC U4Kryh38V07rMTX1grS9P57p9HopgY+3XH7jjHWOoB0o+48ZSxkOB86Np7JaOuxBUsPAfTw7oKk K4m46iA== X-Received: from pgal6.prod.google.com ([2002:a63:3e06:0:b0:c9a:d174:5315]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:a03:b0:3c3:9c4e:de3a with SMTP id adf61e73a8af0-3cf82a348a3mr22345482637.6.1787780007516; Wed, 26 Aug 2026 14:33:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:33:00 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-21-seanjc@google.com> Subject: [PATCH v10 20/21] KVM: x86: Use kernel timekeeping snapshot for monotonic clock From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the KVM-private vgettsc()+/do_monotonic() timekeeping reimplementation with calls to the recently crafted, generic ktime_get_snapshot_id() interface. As noted previously, the snapshot provides both the system time and the raw_cycles (TSC), atomically paired using a sequence counter. Signed-off-by: David Woodhouse [sean: separate from other conversions] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 32390b20a2b4..0352bd147c40 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1438,29 +1438,6 @@ static inline u64 vgettsc(struct pvclock_clock *cloc= k, u64 *tsc_timestamp, return v * clock->mult; } =20 -/* - * This calculates CLOCK_MONOTONIC at the time of the TSC snapshot, with - * no boot time offset. - */ -static int do_monotonic(s64 *t, u64 *tsc_timestamp) -{ - struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; - unsigned long seq; - int mode; - u64 ns; - - do { - seq =3D read_seqcount_begin(>od->seq); - ns =3D gtod->clock.base_cycles; - ns +=3D vgettsc(>od->clock, tsc_timestamp, &mode); - ns >>=3D gtod->clock.shift; - ns +=3D ktime_to_ns(gtod->clock.offset); - } while (unlikely(read_seqcount_retry(>od->seq, seq))); - *t =3D ns; - - return mode; -} - static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) { struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; @@ -1528,12 +1505,14 @@ static bool kvm_get_time_and_clockread(s64 *kernel_= ns, u64 *tsc_timestamp) */ bool kvm_get_monotonic_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_MONOTONIC, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_monotonic(kernel_ns, - tsc_timestamp)); + *kernel_ns =3D ktime_to_ns(snap.systime); + return true; } =20 /* --=20 2.55.0.887.g758fc8c411-goog From nobody Mon Sep 28 04:08:39 2026 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73F88492516 for ; Wed, 26 Aug 2026 21:33:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780011; cv=none; b=OPq9on6ZxwTxCOPF52D1KRwOcAnZetDw6oBP7g3DcA21Qy/QoowqXfrVw7kwHhuFRspcFfuBJmrsHFhHIu9q/rs1nf/qhjt4TRtu5Ltvym3PmQJ6WOZSivLiLKen/ZF4qyqz1tjw6vb/ONlVNxL5H+p/uFOjxfWdy9fDwmIbpW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787780011; c=relaxed/simple; bh=MifOa2XnbtMroctCQ2vPlzSapAFPZbMD1f0LVHEO7X8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Se6Lof+P2KA9V8WnwfomevQH1/U4HWV4qDSwD1PA4z+I+gDTOo9gsb0AYxJblIeZD8RektGjGTmN8TMlnvnj/Zqf17f9Ni2K06InhlkO/wSfbzDJWQJMBNUR1Tfbpy165SJp6ETI5PCXJwLrZKxtz3qKiOiijhBu3SYspoOtwz0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PC2OcvxK; arc=none smtp.client-ip=209.85.215.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PC2OcvxK" Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cc1c1cc5206so1510028a12.3 for ; Wed, 26 Aug 2026 14:33:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1787780009; x=1788384809; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:from:to:cc:subject:date:message-id :reply-to:content-type; bh=sIs19yA05E5+D5bvhm1516uSg4N0iBHK8YfCkpxX2Cg=; b=PC2OcvxKts4dmKa4tevER8k3tej1DRYZUs52ZD5uXDkXdQga7mY9QeNZWuPG3f+iUB wY1lf1M2/F/1Z/ii4VYL3MhU7qKEf0ba4PIrZGxr5IEEnJS5s28H6zpSR3KhMCoF57Z5 G/vnAGRxMt2tmr3tk+mHmdiGY8N+DalR76Zng7j+ZPfpqo24qnve/XptvouipFiojr5b llyQcZZuSMsn/J7DiydAyWKH6pMEBo8DdRzyOazKD/XzJOjSfG0gcVrfyM3+Yhf/mHr9 6bv3w6NYKw1GLIGIdkQRkc0Y4iexWVlNNGuNXwIwfNy9L2Sk0+SQOVuAWQLfxreC3pZB DvvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787780009; x=1788384809; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:reply-to:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=sIs19yA05E5+D5bvhm1516uSg4N0iBHK8YfCkpxX2Cg=; b=ezXfMDOzmQ7KW/FvF+nqlh23V22k7j6QKg6xoxAzgT4r7Y6EWUwAlxAQ5ck5UW5K+j hJ8RRlzvf2/XNP5t81FWEwBoWj1ldCBxbVHc7+C2G7uKSudq3rIp6HXvWbVksKqKuAvX YhnJKWutKYZa89rVch1D0cpCyzjpTPIHSFjNf/QoX7+oMZ0VF5EpPgUwka9HD5KscrI8 J3iJAZ+RXYEd8d71wA6HI/svMvmZOUkPjVThP+7+RxY6RxfhskTkG2uKJcnssfgkMndv 0qCGrGcaq3GQjO+bzjcw0C78AYK8tSkLrNyXL7kpRteZ1q48cXemKd5d7IzxQT/yvIv2 bRdA== X-Forwarded-Encrypted: i=1; AHgh+Rpu8d/uIhRJJXTZRoimnOgcGZ4UtGVyU/nPdVRlQN5SsfC8+SdYJE6dhmu6gQSegghcZA66b8ZGfPv+PJ4=@vger.kernel.org X-Gm-Message-State: AFuF++nDYE6RthBc9l4NpuV+Vcv+n4yFTh3D7KRK75NYRVht0EGbR3OE ZVHAa6H+85+EmcroKS6NyAJ+McStVtsbu8ods7Q9PGJ6xO0mNUwBTpJ9djsvPETu/+qnaW1xMiS mYjSuYQ== X-Received: from pghu17.prod.google.com ([2002:a63:ef11:0:b0:c82:a5b0:d9f0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:e18b:b0:3be:b0b3:3962 with SMTP id adf61e73a8af0-3cf83f36b8amr21369888637.9.1787780008574; Wed, 26 Aug 2026 14:33:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 26 Aug 2026 14:33:01 -0700 In-Reply-To: <20260826213303.914988-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260826213303.914988-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.887.g758fc8c411-goog Message-ID: <20260826213303.914988-22-seanjc@google.com> Subject: [PATCH v10 21/21] KVM: x86: Use kernel timekeeping snapshot to get walltime+TSC From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the KVM-private vgettsc()+/do_do_realtime() timekeeping reimplementation with calls to the recently crafted, generic ktime_get_snapshot_id() interface. As noted previously, the snapshot provides both the system time and the raw_cycles (TSC), atomically paired using a sequence counter. With great pleasure, delete the now unused read_tsc() and vgettsc() Signed-off-by: David Woodhouse [sean: separate from other conversions, express joy at vgettsc()'s demise] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 85 ++++------------------------------------------ 1 file changed, 6 insertions(+), 79 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0352bd147c40..34eadc75fee4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1383,82 +1383,6 @@ void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 = *user_value) =20 #ifdef CONFIG_X86_64 =20 -static u64 read_tsc(void) -{ - u64 ret =3D (u64)rdtsc_ordered(); - u64 last =3D pvclock_gtod_data.clock.cycle_last; - - if (likely(ret >=3D last)) - return ret; - - /* - * GCC likes to generate cmov here, but this branch is extremely - * predictable (it's just a function of time and the likely is - * very likely) and there's a data dependence, so force GCC - * to generate a branch instead. I don't barrier() because - * we don't actually need a barrier, and if this function - * ever gets inlined it will generate worse code. - */ - asm volatile (""); - return last; -} - -static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, - int *mode) -{ - u64 tsc_pg_val; - long v; - - switch (clock->vclock_mode) { - case VDSO_CLOCKMODE_HVCLOCK: - if (hv_read_tsc_page_tsc(hv_get_tsc_page(), - tsc_timestamp, &tsc_pg_val)) { - /* TSC page valid */ - *mode =3D VDSO_CLOCKMODE_HVCLOCK; - v =3D (tsc_pg_val - clock->cycle_last) & - clock->mask; - } else { - /* TSC page invalid */ - *mode =3D VDSO_CLOCKMODE_NONE; - } - break; - case VDSO_CLOCKMODE_TSC: - *mode =3D VDSO_CLOCKMODE_TSC; - *tsc_timestamp =3D read_tsc(); - v =3D (*tsc_timestamp - clock->cycle_last) & - clock->mask; - break; - default: - *mode =3D VDSO_CLOCKMODE_NONE; - } - - if (*mode =3D=3D VDSO_CLOCKMODE_NONE) - *tsc_timestamp =3D v =3D 0; - - return v * clock->mult; -} - -static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) -{ - struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; - unsigned long seq; - int mode; - u64 ns; - - do { - seq =3D read_seqcount_begin(>od->seq); - ts->tv_sec =3D gtod->wall_time_sec; - ns =3D gtod->clock.base_cycles; - ns +=3D vgettsc(>od->clock, tsc_timestamp, &mode); - ns >>=3D gtod->clock.shift; - } while (unlikely(read_seqcount_retry(>od->seq, seq))); - - ts->tv_sec +=3D __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); - ts->tv_nsec =3D ns; - - return mode; -} - static bool kvm_snapshot_has_tsc(struct system_time_snapshot *snap, u64 *tsc_timestamp) { @@ -1525,11 +1449,14 @@ bool kvm_get_monotonic_and_clockread(s64 *kernel_ns= , u64 *tsc_timestamp) static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_REALTIME, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); + *ts =3D ktime_to_timespec64(snap.systime); + return true; } #endif =20 --=20 2.55.0.887.g758fc8c411-goog