From nobody Fri Oct 2 05:29:28 2026 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (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 4A805394E80 for ; Tue, 4 Aug 2026 23:39:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886769; cv=none; b=YaSCN7SCz8b3A72DVvBSFU0NCRo9Vu1hY2Ha5a97N9iQLNNmjtFbC3IWrrC5g69dDSnDClQQRlVLKBOC8qVvyJ0dm7K38GuDsayksZIBpbt9ZcHtYoG9puCjRbrIRFDLleJ1hvpK3iKOfYyElnBxqxAlYGY/z2XatVDfOGPg+nA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886769; c=relaxed/simple; bh=Zb1yCTfWtc7AjTnpDdKcnQevCbLygXR+rO0daBu6lIw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aSf6IRAqu2nXpS0gDNS1QtfmriFgOxGtQxVFH14asshhG6w7nGxRTdSly8zsD4kYCeqX1UGSZ5vXi8NLIIy1kHDTgU8kqFu4J6FBHyrfOMTfdIzPYiO0fnBCn8KW8ugFF8j9iXCO/zOW74Drc7FVoGgINhsdHNVSR7/N8CIkb1s= 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=MWkH36vs; arc=none smtp.client-ip=209.85.216.72 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="MWkH36vs" Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-38ce7fabf76so535211a91.2 for ; Tue, 04 Aug 2026 16:39:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886768; x=1786491568; 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=EE4fwjfSfyqQPmpr98wwjwDPG0KhcYlo7/lIhHAV1RA=; b=MWkH36vsFBw19gtotn4yWpq1kg0kpb/MTTMyTt5tDbAL/WKVT5ceMZ4sTA7ouheH9w Ic3U9nnnp4mZo1YkDDXz6DEzJLBi4ACn5ITEuJIA363b+ImTx0TlEIHbGXS44r7Jvh5+ yv8tPZ37I1EpsWTIsFqn4dc+tTK6VF3amjiULONhW4wzIuLfpokMPeeZGlQCdWZPTGzM I9H3fw1x4/L6CzUibg8W5xrG+rGNF4AjoLxTqWGjmlOwkwfyLwyTS/L2tFCTF3JDbKwt Ul7rqZyKgMDSjq4XfZly5/I10D+2/K3W+ilXVjidr/IVf5yJ5c1KAfha+yMMlXnYOUxM MjyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886768; x=1786491568; 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=EE4fwjfSfyqQPmpr98wwjwDPG0KhcYlo7/lIhHAV1RA=; b=ZEL4JkRbFZld0M+4G5VOWxE3sWBSr9z/Sj+6NlquJG2NGnJLcSatbLjTQqQiU63CZ1 NzqBimVNQmNlDOJk++bfZGPRRoXcOq4LMULztki7vnHLmK0ZQXlKpx449vKtFQ4kIm5M ne1qT3BxHRoSYQVxqb7eqlpGcQ7qpCm5rY+kGFcUT9lwC7VkhUgWeWPYk47X85TAeLsF gJsfi/WZxE+0Kq8b2tPk0nsY4q9m8/4roMjTL+18PB4JhWpxei9xf8pSWd0a0EeAUvFT tm0hU0QYFNeld78yEBF4CL0C4XGC4PxQxT66Mo5iK8c2bnK1XTG6GhXyc02+PNjvj+sz WfCQ== X-Forwarded-Encrypted: i=1; AHgh+Rq0w4slysrZXYEKfH8d0kJFf4WetllaVkSYehSyJ1uG8lSMXxbYHizDbFWK0D3x7tYRiFI+yX4ff5aJHg8=@vger.kernel.org X-Gm-Message-State: AOJu0Yxw/787/zTa2iTwHLiLx3uI5ucNJV59nLXUBMdqBxtmrj9BDf2E K1Pq28XWaEqjgjxqGI/8Bw2Uzgelzce82t9axNsHXdTGN5//Wu5tel1nAoFCRepLQP/cbulzdKg eYGZtcg== X-Received: from pjub5.prod.google.com ([2002:a17:90a:cc05:b0:38e:b470:e6db]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:280d:b0:38e:1497:af5b with SMTP id 98e67ed59e1d1-3903c54fbebmr2514068a91.1.1785886767530; Tue, 04 Aug 2026 16:39:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:05 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-2-seanjc@google.com> Subject: [PATCH v8 01/17] 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 d94b59140c45..d3b47e38698c 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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 5F4723EFFB4 for ; Tue, 4 Aug 2026 23:39: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=1785886770; cv=none; b=X6MLF0iTR6s5SiFCuU5b5HAsVQO2mdUfBHouTN7HYqXWe8dfS0HQv4yXEH8lPDEViBbQe2BXaLVeE4Wvlsy4nvtVXc96qkFV/0JgVCmznel0WCegQGUnnNBzTqZysvtgDhYN6L1VEqn4Ir+/yL5ckF0T0LjUk1BQqe2wrHC3mUQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886770; c=relaxed/simple; bh=VcDFsyXNDS5AHqe/iR4hiX/uBj74+UXKYzqXs5zgolM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mswb6QgymXqgDjiDFGxtoi7J4ZvDipO1PceTj+XFycGurUyxE/CUZhvAVwMD2LyDIhe2fzovpzjLozDLreRYmvkVXi7A6ZmaQ8+38bGKtHeIeBi85UlYxjR/TV83GdbaENacvazZKSpZPx4PHEtFmqP2KjunHzaueMr5IbU9MlI= 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=I6hlMLUu; 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="I6hlMLUu" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cb48f41343aso1184240a12.1 for ; Tue, 04 Aug 2026 16:39:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886769; x=1786491569; 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=3GRGf0SAx8ZhbJ78r2siXWKgWBaDw4VeY+GDZV2SFCo=; b=I6hlMLUuVXe6DHnG+lI293LZZvFJohQ4wpp3q333dVSas0Sh+/UtCVNCgc/OAQHhpg PmyRfEgl5msRwo96d/qhbeoOLkD76uv0HHSnT2vKKeBhhvNm1ls9y7gJVYoJKMnxIA4L 1aUmoHAsxzjHzssm12jIg/WiK/kcYzx3V3WIL4QL3p7WgDdO0OSlB9VdfXZfonPZdRVB +eO83wKmUUv6qrkiqv4RrZDhNjiRdQnksNDEREhYpptiEzApMdUypGyLmhi76DLWF1q5 gUFlvBNjXJrtYws096vE+jalrIR3mTKTH5W6swJkhWG+c1WGVufxk6ssJ+yHO+J6Oo6O Mwrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886769; x=1786491569; 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=3GRGf0SAx8ZhbJ78r2siXWKgWBaDw4VeY+GDZV2SFCo=; b=EPsQuX6e6Ceqpi41XNQasO3+isXlSnNFuVL+8wlLuV8EGv0h7WhZ0sR5PlIflES8gu uKxjT8sWdIO7uVEtUHQk+fBcQJ8FCqyRnhOI43d0+myWcnCZO50W+dPEgeHn8jK9WFjT nUMh44AreiU4Y4oIwX/41OpKVJm8Qq8xBgy2m4VViIfpc2C5uCGGAMcFN2traWoJ5UUB 6r/KFVadngC75m9cSwf5Vdfclortj1RFSO0PAwHMHVl692nc8M3FAzyJ+8hxqaqzIch0 7WYYhO+HjxMe8RhG5z3hAgS8PjbkGV1rxMy0TQlzfP9ZgbQozq+CN5uV7ZOGEHqyPX4U bEIA== X-Forwarded-Encrypted: i=1; AHgh+RrGHVAyYvvaFizyrDHHkMgwl1NI38DR+hkmqxDXu4IZSpD2j3RSTWWgibjoc5OJIyFViTx9KoG1Mvd6hOs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/Dj+QPGCHH1nJZZLttRxTuau4ch1eLS7UN8RdZA83b7/XfeNL AiXaLmRfPwLrbEtS1ZkGQN19xflDYj3AuLdhyzwVS7uO76Fv5QIj0DgbUKAyXJeCzC3rf5LBlDG SyocSig== X-Received: from pjhk65.prod.google.com ([2002:a17:90a:4cc7:b0:381:6cb1:3289]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4e8d:b0:38e:5828:a446 with SMTP id 98e67ed59e1d1-3903c70ae1bmr1050461a91.19.1785886768582; Tue, 04 Aug 2026 16:39:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:06 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-3-seanjc@google.com> Subject: [PATCH v8 02/17] 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. Use div_u64() for the Xen CPUID leaf to play nice with 32-bit kernels. 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 | 3 ++- arch/x86/kvm/x86.c | 18 ++++++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 283847619ff8..6ddc988576d0 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 9e9cf6538a96..7049f5a5597b 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -16,6 +16,7 @@ #include #include #include +#include =20 #include #include @@ -2146,7 +2147,7 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *= ebx, *ecx =3D vcpu->arch.pvclock_tsc_mul; *edx =3D vcpu->arch.pvclock_tsc_shift; } else if (index =3D=3D 2) { - *eax =3D vcpu->arch.hw_tsc_khz; + *eax =3D div_u64(vcpu->arch.hw_tsc_hz, HZ_PER_KHZ); } } } else { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d3b47e38698c..ce6d155b2ee6 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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 A7A5F41378B for ; Tue, 4 Aug 2026 23:39:30 +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=1785886772; cv=none; b=oPrpQppBzfTMzG4unHej/a2IsbOTZo1w/5mUWoy3NbG1cRR/aKToISLQPcdvpyXOlol9UQEX0fpbX+I2sgy8oT1gt8GWsUdTdeoezG9Bvt7O+47woSa5EJbTJl8+PZ/Zqjr7d3yfdqlmJ3dNfINaZuPV6dCUabTb50ywVNPYthI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886772; c=relaxed/simple; bh=5Tr1KXR+RqOIj+v2jOYe+EmYVl4L3NmWNNOy1LaZrVE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WxjCSW2ybn4CYVhsB6IsiyL6Cr04LQeeMJdfQrs989T81Fp7Pb8dE6TnmWDpDeCjDiYCD1LUi6XDnySatUMlFWizzsBHidAjVeGpJLv/W8UxEGYmLBc3BCnFtpImIeBbLW+6C/g5hYSJjVKERgSZt6rpFeVukrZg/eGVvcw4LaE= 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=Ldxbraok; 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="Ldxbraok" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2cfa4e4684bso8934215ad.2 for ; Tue, 04 Aug 2026 16:39:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886770; x=1786491570; 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=yLPwB/cCTC3LB7Wr+XVRWt6l8jlTiyB5xo/WHJZPrQI=; b=Ldxbraok3KACRUkQeQG8fNeC710owmlFHF1bbA6GbaDVy2+mZjU6lt0jIczoN5jscZ D5pAfVTiKoCX6kzBc3ztUdoBzx6XFflwfYGpKDummRsNhyA8GgXMHkBcN7qCxvojSf3C EvNPnP1xb4xfdigVWB9o/Ygbe18v7a1+YGO8Di3Tc4WxOc1SJwh6metIT0eApaZjPR/g eng+wEDcVNUl3PtflMMNi61X6toqngOiqcDO4sucJAg6ZD4C4u4V0/27bbB0Hz0dXUYb SjoQUeNJTVMgv5388YqZU/gggmIOTq/Iq/fUlB2yIFq6EWzcnwmGingtHQUgs7VsqK3j 07zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886770; x=1786491570; 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=yLPwB/cCTC3LB7Wr+XVRWt6l8jlTiyB5xo/WHJZPrQI=; b=hwZg+Xx5IHPIEM9xElX/JJSm4bHJzrbJ5UkTn4uCKkxNWEX4a+H6Uody920d54zXk3 Rqnu0kOfnG/6Cv77mCEKSi+YckQc+Uf0Wdtvy4oQQJ5xBLumlcUTK9K5QQb7jzYCway3 fb9P7fCVghkqJG0CLAZx+WoYqT0dIWxIDKFiWUNUaHFriL0aO1NUhjPs/yqd3KJVTK6f d0T94Pt3SkfhzvbhmvB5VgII5GxPATVio2yHayLwuL9MmYajSOmBpt3YN/olPv9a3llL R3VXxUK/+f4lPqwX/BWsdn85wPJXICxPHrTy1XCDj4ybvHciSUm/I5YVz+l2nzH/n1dr 5Zyw== X-Forwarded-Encrypted: i=1; AHgh+RpGDhGLCmwpaTgHvBwfAZaUfIyvFWugoCYdYSkJwcrmUNxg+XbvIHzShg9WwyLKfmxRAY7z47WN6j+Zolc=@vger.kernel.org X-Gm-Message-State: AOJu0YxqEzL3FpECL1bkC7S5FtmlVwVradRxbZFX/k8wY2xE7RZQ1/ft tWVku3zeQYuYFJyxde32U4K0syv8z6c9UB03jCAYrHBnrYb5o2+79oMJd6mfV4M5Bjv5hiSSGQS mPwIG3Q== X-Received: from plblv14.prod.google.com ([2002:a17:903:2a8e:b0:2cf:2c6d:ddb8]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ecc8:b0:2bd:c925:3a16 with SMTP id d9443c01a7336-2d0ca710d3dmr21879415ad.2.1785886769655; Tue, 04 Aug 2026 16:39:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:07 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-4-seanjc@google.com> Subject: [PATCH v8 03/17] 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 9d607b98bd06..3057ca7051db 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5602,7 +5602,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 e3bfe6aca1a0..9abd2ed3aeae 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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 9C2B6432300 for ; Tue, 4 Aug 2026 23:39:31 +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=1785886772; cv=none; b=kinNLv7A+eQ+C6d+wynq63HdYtFdxjkvArqIoAFvfwpyk0A7fzUsySqs011E35qaIfUSlYpkvFKyK48KTW6LyOyIq7cws3+dyG5q6XT12DzfAtNT+WnnACoXCuGfS1+p3VG+Ph2MZsvxe3QEwKOSihGgjAoKZQTIt00TwQmRYuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886772; c=relaxed/simple; bh=89JyQv6HRefQDN/D0POA6sjh7E80QJy4cC+vhHi2sXg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aAT8etZgn8VeccShi3ysIYPFchdqogJOAI/FJV3vFX7YW9o5KAOX16sp54kLn0CQJabG2F97ZGIN5NInfLQhaIr3k+cjrcwiE+e+bAkOtzsx4tCVgU5SlVcn+mINkrWerMQyJXNMMBCsoRNfblyfD3GR49ZocGGQTtxO+Xon82Q= 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=DsWn6fvE; 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="DsWn6fvE" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2cc6dd43737so7203315ad.2 for ; Tue, 04 Aug 2026 16:39:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886771; x=1786491571; 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=P02jVjanYDtcmjUaf5UDPPw7sZUuSi7rTQrdCOouJNQ=; b=DsWn6fvEOLugLNFJ/QFY7ibwTuCh7B2O+6f8lewSSqRCfGfcpOyuHuwY6sNSb22RY6 0l+81eb16HQ6VzSTDox2sntKxRX91s37ZXB1l94D7Ux8yS5CIibxekNbTimG2h2d3YxJ I4MpkBeinkkHy2yI55hU1qYOj67aMC5NZE6zwrA3HPx4WU70D+qqmFgi87p5ZlCnuh1X XWhzLsV7qa6nLHY+8fjrt0dj8qKiLHmOTQ1yzK04BEAHrtlAZXJlOPZOufjtjUbj1+bC PLO9H1xh+HaeigFzF/ZBy7PyiqHCs+DmR21Ir7fPQ8/ZQntHGmrJQW/qOwchQVSMAcVQ RC+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886771; x=1786491571; 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=P02jVjanYDtcmjUaf5UDPPw7sZUuSi7rTQrdCOouJNQ=; b=hk592TxfhsykGMRh8tYqRz5Ue34mSzbSd7jzRZWUX5AiqhuZzokhtkjaUySH9ME5k0 va3aU919jtJTy097m0fyFV2Amxf1DNIL+AC4pzj2hcgkdjj7A19VAaELJ0XtoBtxgigq fLrCUwsRCA4742axjdIYkbu+JqnYmT9PyAVdi3IbWEdF9SE5AQ0KwD0mCQx8eJJazLwR vEFVMU94iRq8AY2/j1VlrC5xKuRwAkWh8Smwsc9hfYnpBA7iJfjDBVO0Le+pNflNBnHw UzM/2cfuSLSqdC8BEg+P1bpkBRX2CQW35DUU3PzCT5SR9gZK10icZlwEukHTwfceop1F I0jQ== X-Forwarded-Encrypted: i=1; AHgh+Rq+9StmQH/PCgNDGTUFxrk1A0wRTJvlYMcyYGfPrmcBYGXf460iIXSKk3DWIBMEP2FnjEQY36yV6vLGfT4=@vger.kernel.org X-Gm-Message-State: AOJu0YwJRT9IFGh0qzxydMe/ulPjufAKPLGh/u2G65D6TuDMJEiTo39R N+EePshvOAh9Vmb6Raq2qwdu6z7XJ0bJnoPhkG9EV80DMEUnzsDTOR1/+gus58Rs37yCf4/55Mv EaE6LIA== X-Received: from plry15.prod.google.com ([2002:a17:902:b48f:b0:2d0:8b2b:b8a8]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:184c:b0:2c6:a772:7e43 with SMTP id d9443c01a7336-2d0caa040e3mr28687395ad.18.1785886770845; Tue, 04 Aug 2026 16:39:30 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:08 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-5-seanjc@google.com> Subject: [PATCH v8 04/17] 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 ce6d155b2ee6..3ebcf2f27f7d 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 (!vcpu->kvm->arch.use_master_clock) + kvm_update_masterclock(vcpu->kvm); vcpu_put(vcpu); =20 /* poll control enabled by default */ --=20 2.55.0.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 C4B2343B3E8 for ; Tue, 4 Aug 2026 23:39:32 +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=1785886774; cv=none; b=ZwQ2httFCT0LFlPhnmDd87tkQF/sIymZQwEnAX5xxhPY4Nqw/RoDo3xC64v3CkL5wZHR/B2RPupbV/yZsRoQkQm4I2WF6jPeb2LItxtPgur0r/qNppKsZ4Qx9VMSN9+j1WFVI1Kzl/vYtlpeMZPCg45t+kYejcf7qWIJrkXDfT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886774; c=relaxed/simple; bh=WqiyIYDUJvoYrhXVhsOcnM9n3MISpQM9nYaqruRIwEo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kjW1cSeVJOE6+Iyt/8DQIs/R+NIVsF58q/VUmT9uAKUczUEfB/PQ3Xf0AHFaGK/bvPaz++gluep3Zksn2Ln7/aOk6UdEJGbMarGRm6FO5ysyrJSJAtP8kZeL1BWH2CgdWxX0MBxoHAFCI82zkzrgECOUib795uCNim9l6MlO550= 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=ZMDWs0B4; 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="ZMDWs0B4" Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2cc6dd43737so7203425ad.2 for ; Tue, 04 Aug 2026 16:39:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886772; x=1786491572; 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=x5rOF7VvxtdkKkuGdXTFcuTjBzanZoFZ5hsAKXxG2sk=; b=ZMDWs0B4p9YbqNPKhlrVRsNtllfcjB316CY7uxsAyQXycNamCaS/QCgH0v9ZqwqjuU PuT4nKuYXClwRxSTyKG4JOob8NqFbLcGBaCAjo5nMlsfaB8TSL7qyQnQSzpESIWuapdk r1muW0Gnl6p+rMHUGmI9OZFrn5sc+fVm9Jx2WTNaCta7Zlpu+js3Sm/P/wzQCvFtwyjh joAtqSL60TGW94machOexOie80Al82qUHrG2zQh8dp1IqTbIRoMoR+rntnti2po9/c3d GNHzZOtMEeJL+BWF3J3JLqaQXjkYbcnQegsjqYdwKMxdS5vGEfzBmiy3KXPFa5/KOiHQ Tx4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886772; x=1786491572; 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=x5rOF7VvxtdkKkuGdXTFcuTjBzanZoFZ5hsAKXxG2sk=; b=G1s7YHcvHCMkOB0kQcWSmks4k8NPgu2To4WZ3PHMX5Lx+P+1H0/cE1i0O8Ng7SWwaz lEfz3obn22JNSDrvw5RhZtPZEoevTiu42BpdwLgIBlOKCzOVxljiWkCYXabFZdW548Y+ zXupXEO1AraGDEYQXo51WOBxjxP81CGMCeTTP4FX0jvOfYtN6US6ERqcPPPM/H9WS8yW w38Kxn7icJFro821nHq+YFb6unb2JmvBfdEBDpgFvvyaZ/5ZRLg8YMLLMu5c3TPaK2g3 /o2PQm7RQ+2NvJ43zTBX1PM1AJWv4VUeMMxRRXUZ1PbB+YN4Q0U89kcMVsuXVcONK7Xq g7oA== X-Forwarded-Encrypted: i=1; AHgh+RrZDnMdhDBA0fsJd04t1pAdsfXr1K2lahpgKVq/0bKQaNjdGyv8OfjYGGdLinKzwkKzxK5jBXiMRAm09MY=@vger.kernel.org X-Gm-Message-State: AOJu0YwGIiFWBBTsUOfM/3OkBsJMYLgLXP7XBTgGMb3Z0QtQWe956oY0 cDhyBnwrsiR6xAIdFERfaaua3ty+/RqGo5H00VAM3+ozu0LEHpt9DnVPJbKpkuZ9sQ/fZy9Qoxs 89QMIDA== X-Received: from plck21.prod.google.com ([2002:a17:902:f295:b0:2cc:8e87:df3c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:388c:b0:2cf:4339:aaa with SMTP id d9443c01a7336-2d0ca7fbb6bmr26189435ad.12.1785886771887; Tue, 04 Aug 2026 16:39:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:09 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-6-seanjc@google.com> Subject: [PATCH v8 05/17] 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 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, enable it for the 32-bit build too so that get_kvmclock_base_ns() can be unaffected by NTP sync on 32-bit too. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3ebcf2f27f7d..f60f4826db1a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -874,7 +874,6 @@ static inline bool kvm_vcpu_exit_request(struct kvm_vcp= u *vcpu) kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending(); } =20 -#ifdef CONFIG_X86_64 struct pvclock_clock { int vclock_mode; u64 cycle_last; @@ -932,13 +931,6 @@ 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= )); } -#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) { @@ -6873,6 +6865,7 @@ static void pvclock_irq_work_fn(struct irq_work *w) } =20 static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_irq_work_fn); +#endif =20 /* * Notification about pvclock gtod data update. @@ -6880,26 +6873,26 @@ static DEFINE_IRQ_WORK(pvclock_irq_work, pvclock_ir= q_work_fn); static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long un= used, void *priv) { - struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; struct timekeeper *tk =3D priv; =20 update_pvclock_gtod(tk); =20 +#ifdef CONFIG_X86_64 /* * Disable master clock if host does not trust, or does not use, * TSC based clocksource. Delegate queue_work() to irq_work as * this is invoked with tk_core.seq write held. */ - if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && + if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode) && atomic_read(&kvm_guest_has_master_clock) !=3D 0) irq_work_queue(&pvclock_irq_work); +#endif return 0; } =20 static struct notifier_block pvclock_gtod_notifier =3D { .notifier_call =3D pvclock_gtod_notify, }; -#endif =20 void kvm_setup_xss_caps(void) { @@ -7118,9 +7111,9 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) =20 if (pi_inject_timer =3D=3D -1) pi_inject_timer =3D housekeeping_enabled(HK_TYPE_TIMER); -#ifdef CONFIG_X86_64 pvclock_gtod_register_notifier(&pvclock_gtod_notifier); =20 +#ifdef CONFIG_X86_64 if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); #endif @@ -7177,8 +7170,8 @@ void kvm_x86_vendor_exit(void) CPUFREQ_TRANSITION_NOTIFIER); cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE); } -#ifdef CONFIG_X86_64 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier); +#ifdef CONFIG_X86_64 irq_work_sync(&pvclock_irq_work); cancel_work_sync(&pvclock_gtod_work); #endif --=20 2.55.0.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 C8AF543CE68 for ; Tue, 4 Aug 2026 23:39:33 +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=1785886775; cv=none; b=WyMAp4ke7aIYHP5s99FWl2BxC/+5y0fonchiT024SGKafbhJfDJp2WOy7OkkG2u0Y1+fz0zZdYFPJ9JdeDh+vJClvZzO1pBrYOmRodBbU655eI3TItyO6HK8wz5fl4VdPfXAhR7hEHvTtuEpEW2TOyYiXAJSwpJ6YZrX4kjgHJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886775; c=relaxed/simple; bh=bxCylr4HiPO9zwqBqEHUaooi53GWIYbqRZaq07brKjc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=n2Ox8JmlzuXYtT8WhluR6nMbE/efuoKBWDjRSuffcQkFvNdGSMj66jqLP5ZS3t1i8huSdqSgZo+BcVvF6UNpS4Nw2F8ddxoGNjG1nDC5TkyRFTema0qjMYlUniy3VhCySPB4RATwfCLIcEGPOh2HiItHM0XA0WNTP7KWofPXhr0= 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=W5RXEdoB; 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="W5RXEdoB" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-ca6bd8a190cso440703a12.0 for ; Tue, 04 Aug 2026 16:39:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886773; x=1786491573; 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=5KEhdOkBgt8kZo6v75UUHaCXwdu0mISEpkcWN+SxXyQ=; b=W5RXEdoBiytjRahh7bmbnWZit5544wpYN6crUVZv9nhGzoqGPGbEzJeyJf6ZeE9jjC V+Q+pluD8JKoGyTrU6DRDjzwsaXsttlCmj13yCfSou1OUOvgA4u1MC6OZUR7Bwu2kg+0 RKC+JUfRdm620aFM61yXpOxdCfbNr60FFzj6GjZZT6XvRlZm7ZJwIwZ20KEJiRP/ctGu XRot9eRqGMyqCk2Z17dwX7LledL4EWO04OIq1G/3pHRSqUaiag8OyTMMeWXhqcvR/K55 El8jChWc9OjEufgK4jeMGpsZIV3GRQNio1RA3JoB92Hg9kVJqPl4XlB93bfwltGzvCN/ faCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886773; x=1786491573; 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=5KEhdOkBgt8kZo6v75UUHaCXwdu0mISEpkcWN+SxXyQ=; b=TEUBGlAmmRcSY+n3OWprSoTG6AYSAPdz6QL9Y9XarhPZLJ/i+YEfGjtwxOe02yGxNl VMzvnuKeZgMU07XOOM80MFN4j6mjgxakqX81LJWweqaWWKDMXMPTeNtSEHjvgZvBXhm7 qexjcQDIgVGwVxohiEsWesWnIySTQWG49JTlxhQaxO8xkrdsDTQ6th2tYZwIMqHYpbCT tKYqd+KFnMS5Psw+QMdgS88TEL5F06jFvHLK0lKNGlAoDXNlb12yNkKuY+aAZOEeJ9Iz ULYf+M4rZtnJJWF68Qv5G9f/YpfOmVfXDlTQuJKMcgsVAH50YpqFMtTRnUCe6e0bCosW mjEQ== X-Forwarded-Encrypted: i=1; AHgh+Rq6hzMI34iXdEzxXhJIAVsSbQdimfTknAj3VMJM7xkjqg6ePhtGmlJwRkAmHPkyMo904uVVJnmqSnbxfPg=@vger.kernel.org X-Gm-Message-State: AOJu0YxeJS0pfj0w19l6UsBwlZ9PQgnKNGvLwJMresvAJrwuUsbvEaio cp/LMGmHVYt/5Lnkr9YQLrcuBeKS+wBrlV/QJCgSByetNSDQVVG+lFaoo0CTymzMP8QWb9PZV8J rw/foqA== X-Received: from pgbl10.prod.google.com ([2002:a63:570a:0:b0:c8f:d0d0:d863]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6112:b0:3c3:b628:6d17 with SMTP id adf61e73a8af0-3cb85e7f1bdmr2532242637.25.1785886772956; Tue, 04 Aug 2026 16:39:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:10 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-7-seanjc@google.com> Subject: [PATCH v8 06/17] 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. 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 f60f4826db1a..0c0bf4c14bc5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1655,13 +1655,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 @@ -1675,15 +1680,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 E1DE03F210A for ; Tue, 4 Aug 2026 23:39:34 +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=1785886776; cv=none; b=fI0deRug8Zv66gfpSx+j8KSnNkxoLzV0Mc+5z6hqGDkAt0lelbI8voLhSedBgmzZAC76t51EJNQ+o9VSTEGAmZ1JcdAG3K2iDJb7eKV5c9rExnx2g+cjoDeUSZAEH25DZUIvExcG4pgMUlmepQ834jhAVmZso72EBmWBwIxFHig= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886776; c=relaxed/simple; bh=KehEpJaIvmjEDR8tAws9CYU8Ggr/kbnv/XQ+wxDFWrc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JJFzyqx3C0DwgXiCGEaujfgdDBgwe8pu78Vl8ceKih/J5VgH/H5UY85AFm4MBrgEzInsTUEzadUg/rxAUz5LKtnvO6UESV5Nh4hyckQkKkc6Ud2isj4+SwucN7BJR5Df9PYhSYjXEnGKFnNOKXoAwiveDhbxv/B3kYgC2eS5Gwg= 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=R2TFwAJJ; 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="R2TFwAJJ" Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2cfe48ca1efso5632795ad.0 for ; Tue, 04 Aug 2026 16:39:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886774; x=1786491574; 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=SIdV9g/PsGWm2rzYrQ2UqZD2cXobDRmK0jleLEJwTrk=; b=R2TFwAJJRf4/O9R7t9O8AhP09DPxrcf1sl0ma5AqlAPNOQ0Sah56v7e65V2Gn/YCvO j/W70Tm/9g7ifNKCwvzn+/0OeOfog+dMqcf4Jjp+ljbj23kdGkYuQb77eHwzwMXlmC+e jJbJR+CX/raZLzXb3fJdBzI0wfgn2UU6wpy+jl+bL7Mp8F5unitsfyQYcoR0GqpC4D4Q 1z7V/5qAkYrXfIJihE3oBnGQxIg/uK4x3URRPM9pryMWGPyWrRKeBwhi6xccUkGOSpQa rZwTpegIQ+fy/vTxDl4ZQbqJXvA21ncvxM7UTagZxtTsFRjtyLvcQL8AOOfMH3AizSlP jEgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886774; x=1786491574; 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=SIdV9g/PsGWm2rzYrQ2UqZD2cXobDRmK0jleLEJwTrk=; b=ou84flbnEYqorYuX+PetYMQtbb4BCJ2qGtnS9OE4qy6II58suQPR9aCaVDFBplEaOl 3/ywObmfhAmTxsSMkBje5Zj/soZtNbbMxwFW734hXROCfCKlduk99JOgRKYFy19Yq4qO todUZs70vD2fRI9Ezlqb0o3SkH94NCvtWId98GFdr1XizRUcIvyqvY2BTu0Dq1lWWrHD dcLnbVvIhawk4Frx6ap4ECse9JD6TpbW2bAk++R+69sd3RtsrLTAVj1087JAMrphdruz g6G3rDY88iE87qmUjQID2B9wPo7OCxYOzKgdphGJG3O82YLX9I7KZYnRfRpfUB31Zksf 1d0g== X-Forwarded-Encrypted: i=1; AHgh+Rqn9yzys26Xg8Qu8q9PPLTcgBpSqmw31jKKtdP71N5ZNwX6O/Oz6VDQiki1QRaEh3D440SQY45/QGX33zk=@vger.kernel.org X-Gm-Message-State: AOJu0Yxy9DRWlquaFpKIqAkI6PUaEBDEcVKiVccPEFSONvZKuem5Tfhd E6GeouvKgnfcp07gI5DYAhKDbaVc0hS23KnUYFBDkXPZZtJGcWWZmuor8qVoSXweHACSa7UdfHl qza49/w== X-Received: from pllq15.prod.google.com ([2002:a17:902:788f:b0:2ca:d209:8089]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:da87:b0:2cc:a977:4260 with SMTP id d9443c01a7336-2d0ca9fe0d7mr23375665ad.21.1785886774079; Tue, 04 Aug 2026 16:39:34 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:11 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-8-seanjc@google.com> Subject: [PATCH v8 07/17] 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 use 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 | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0c0bf4c14bc5..8666bd0337b8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1651,12 +1651,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. @@ -1665,8 +1669,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 @@ -1684,8 +1690,6 @@ 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; } } =20 @@ -1695,8 +1699,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 E8B93448B8C for ; Tue, 4 Aug 2026 23:39:35 +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=1785886777; cv=none; b=ehK1H/yEDHjBrv5zhda7MU7d5sBoJjNo0fr+W4G7PkCbHv9/BlvFTFF1fL3QL5mJt0o6L/Uy8Uef5Z7lCM3FxAdGvADoYr6GqQ3W5xsratDTOY0V9Nqa1/XmF6k17WSX1fIrwfm53nRQB15AyyKLBj2+dryU3mPIxgapB2eWktQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886777; c=relaxed/simple; bh=Kup2G3UCXJcE6WDasVFeEURcowEkQTfg5NMp3OJqWbc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hjKh8Uu4CJaEFTRuDGUpnF8boxw4HhjLavj/0S5fYPK426EAdQNtYcRBozrl7Go81mnfNx0IYmh7N2z/JKvjDyyoal41bEDGz1CPEojdmjOmz8ZWNSWettaXBkBXPMLjk8BlXX46kYGFQa+b7EmM87gSqmw75bJLk0cwtfHC1r0= 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=l7zHVuRn; 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="l7zHVuRn" Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2ccb6823efcso3515035ad.0 for ; Tue, 04 Aug 2026 16:39:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886775; x=1786491575; 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=Kc4cuBPkf5R+4x5JgDFzYooIArHsuaUyhf60HYNtLL4=; b=l7zHVuRnb4prtQmTkrHYR1tmqgmH3JQsa9JisQoqgkcK18Vtx3g2n0aTm6ATSWfAou B3efsqcFaJk69i46r03npgwoSo/bhKRQHh9YNANWTaVWI419HG/1se6f4hYFu2fAhKbc KVtPrTsViA4cKwCAgmzuVkwLQBRssmLWa7OU0T8GpcY2oh5geAAqyh8bU6IM2KDDAHEe UQtdtbArPFJQCwq4jSCNWX9QVLB/8BmWrl0NE5sxlLDQJ3QkMlrl9E9G6iKjZzKfj5Xf 8rb8gFfxjhrD0HBm5L8j9b5bHkYT5buWKt6s32e3w7ACkkjrFImdXAuG6NqwAAeMiPkc JBxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886775; x=1786491575; 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=Kc4cuBPkf5R+4x5JgDFzYooIArHsuaUyhf60HYNtLL4=; b=peiG1nIBgp27cNJYTc3cwhc5oI3OECcI+aJRTaolnPwVP1X6JO7Y7lscFJCQoRWCwB tTqE4nPCnFf0vFSOskBnt7gRH1rT9yfoqyS6Ik3qx0bbcZiLoglEApcXrhJUoQlqLOoT 64u/o0KN+QK3UJ3KxjaUTOXxxeDZy+4hhvmM7c7h4ctdLA9QlHYYo3Y9pHcpUIMlgRS9 w7OeHvinRm7OCcqxr1v2wsW4Fm/zPzGizJcGXsOMrrI60kfwwbeQQEkT3mU9lDxswdsu V7EBMq4J0wmvUimlqwqQHbLjGZUyN43BQncO91yEDABYiSWHgLTx3ni3rKdjWkyLlGsk PPgg== X-Forwarded-Encrypted: i=1; AHgh+RoYQijQuLXeubu8Z73mRjg7TVT2e5oZjqjSXsWEStfMY+xow48kJauvWT31n8HNRL5tqRZ3AB5KDYnNTCM=@vger.kernel.org X-Gm-Message-State: AOJu0YxdU0hbTewbNL+DhYo1j290jHpFYoQmlgZqaQaWQYZ7OgxB44q0 5lNWa+jyrnTk0cEpoik+cRiLJiqDLk45NCUMQVlQVLf2mTdorhAGP3DI3FUfAbaeWBqa+RWsDmx iREWEuw== X-Received: from plge8.prod.google.com ([2002:a17:902:cf48:b0:2cc:89b8:4244]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d48b:b0:2c9:cf62:6f61 with SMTP id d9443c01a7336-2d0ca78de48mr20399625ad.17.1785886775089; Tue, 04 Aug 2026 16:39:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:12 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-9-seanjc@google.com> Subject: [PATCH v8 08/17] 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 | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 8666bd0337b8..cd2a140e8e63 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1654,8 +1654,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) @@ -1672,25 +1674,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); } + + 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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 19E8044E66C for ; Tue, 4 Aug 2026 23:39:37 +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=1785886778; cv=none; b=bX/hpv3HDq65xCMZQCDFec2CKbFBlna/K02m4AaJHzSKX8g5fgYHoQJ0qvRPNYi8jrkadFTrIiJ/P4lQXRODLf+K/aEeXoHzxjtQH8UccylkFKtfli6A+SxdpMPfxax7z4F1mkoGeUdlFnCICkKY8G43LCuwMDOWKSNqISx2qlk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886778; c=relaxed/simple; bh=ve0XlDTIVdsxLZuAiRZTN8Znz8sHjY8CmLeSt9uNPNA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=W6Mx6b1LJOe/O7gvQbgLAPNijlR5WYHaZaoDDT+r4i/ovSoznxOvdnQ1x5Ab7F9Xkgcyf2sxDH5gFjc/+6LiHyJ+FTXB8kDI8R+BHl0j0iSv4GeYhcI5CZ6oT0UeIrJTm5nWvMO3t44YAtUXillXOmDlbOiiZfJ+bK3pzJ6wfO8= 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=GbtO9BIU; 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="GbtO9BIU" Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-c9c26587e67so384146a12.0 for ; Tue, 04 Aug 2026 16:39:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886776; x=1786491576; 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=pJI7d/gfSbhu3N7yByL0K0SVBnX3vMhWEbPVi3eeE6g=; b=GbtO9BIUPg3zry2zRc+CXvdZxW6gcRINv8Fo4b4E3K9v3X+qx0nxWpHYmV1jQqTOCy wRzUgNhP1Ntkt82jmFvQxfJEdepEZxbf+uxRHqrYB5sVka0jd95kJty5/tpLhCnTiw2a sv1Nra+CgwOcPHey/IF74cS0F/apPepaIJcDvE5M0bAAlkR7DLybwnFYVNJGSLugD6QY u8aNf5Xfl8G/JS0wrCci84Eij0cqUc7lPm8Z3FHjn3uGOWuAHYvjwoZIvmVhKs8N+ZSw hgYsgse4yPpoTGtyaB7GYFVP6nIAljDJhBIVXFVVGLOaqTArrkZ0LEdCF6UXTMfPj+co qLEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886776; x=1786491576; 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=pJI7d/gfSbhu3N7yByL0K0SVBnX3vMhWEbPVi3eeE6g=; b=ZTgLO0RhwvJnOMzLOiqrWWiNIdtGmCsxVliEaso1D6TrNP2q8TYmengQOOmyoO3qNd WviIXrUSWDYXkdtk6TJ/0sulhPm4fgTndFI1ZsmEKoFS+1AQgYsS52L7EytODPCsemko Vyl93JGRvZw++2OdXu6dWOeG0WSc8F/bGLjCZekQusEVhlvszbA5lPqDrQ8Odz8hb/KJ Oa6ex2srBSA8KMo8TjnmY3rFRrYhG9/rqqTRFLxIO7C/qUgfEbImjz3PgXbpLGe6+ClD uHZcyopvgRqfPEJnMqSSqhElqZcH+H1cT3dLaOFU5ZR6IpMKslQ9tHLQ0QywOmaede2t iTeQ== X-Forwarded-Encrypted: i=1; AHgh+RqF0OiVvSu4XAp4CnWOafbHofZv/kuluvt4wDyydSNxTQxBU9upv+RScEX2kXNNIHZCq/g4HxKdt7za7qI=@vger.kernel.org X-Gm-Message-State: AOJu0Yzue0yCpX7QOujLubjfQJbrjZzzlJSdQkEwiWcWWbSC209kmA0j 0CztJamFX39sUgmk9Qx5xtqPKKxCDOQCAZIPxiDuCetR4mnbyI2mihqSbrWRg07x5zvw8Y6Acwp AM+t0vQ== X-Received: from pgkn3.prod.google.com ([2002:a63:ee43:0:b0:c86:61a1:3390]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:728a:b0:3bf:651d:fee0 with SMTP id adf61e73a8af0-3cb85a5cf33mr2268115637.19.1785886776244; Tue, 04 Aug 2026 16:39:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:13 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-10-seanjc@google.com> Subject: [PATCH v8 09/17] 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 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 cd2a140e8e63..7768a8909cf8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1674,14 +1674,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 BC1984570F5 for ; Tue, 4 Aug 2026 23:39:38 +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=1785886780; cv=none; b=cAPJE4nS/XADqGMVKtW1FR2Hb8sCi1AxSzgBm+HHgD6bjgHxUMFDgb6Sqp9Z7QaVqmoAZGInok2zTM0cK3ROYKQ64kFC8gjQvH7Lu91E0ACiHhS6US5gLb4H+tbD1nEttftRmkFjM30qfkgxl2socAYIUvOEdBvzKg7CUROsGpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886780; c=relaxed/simple; bh=3f+LeGlTiW8NddqAC0vBItNms1RmHHPfUNRCc7qd5cU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dxAqj2bXw6ECGitAxFWQ8TIMGiDFJdosegPUTTL4owYpwYpSayN+zRGxrt/NrHTgIYqbiTPAD+qPtPQg+CIlf0l10u/STRHPHmytpGqP37vPWVtMB3O9T6pyhqEh7hI4nOKzBxyDigIZEp8qsp457hNeqof6LI6WfhsSvDIkQJ4= 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=Ngg6P0B3; 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="Ngg6P0B3" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2cca3673560so7160215ad.1 for ; Tue, 04 Aug 2026 16:39:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886778; x=1786491578; 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=u0AcTxHloFFR8M2y2fMS+T/BunS/UDptibKKxGh6Hbo=; b=Ngg6P0B3afcbg0Y+blid4OPBha+cTPwCgNjB5XKZiZqYWO3a1uJJk2wzh5Cx9eeG6D 9Wcfs9z3QZ+9uGb57HxK1S9P7vlh1UfvqWY+hs3F3EOr8MHl/sZFxSLRw5ehDwaySWKC y37JC34APFduWyr9LdoTlQLsSgrTkXfsQUSK+/udb7Bdhud8V8xvi3rVZxys+GS70H1K c+NjhLgqBL5oDpfj+ZPGtMeP50db1QIDmmA7qYlsqoP3h8ATZgS/+lIPymdTtO/QgHrS 7rv5UQQyTzpkYe1cVKgsmYxftZh7CprclPcmSqQD2daMQHt7Ydw2isgoDTBWlgkgNxwy G+vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886778; x=1786491578; 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=u0AcTxHloFFR8M2y2fMS+T/BunS/UDptibKKxGh6Hbo=; b=Uup7N0ffHN68xB7zBj4InyutDOI0eS4Hfe2wq67FXFMAW3pDGnjOPcqUkj8vHT3imI axC+hanWo3eVgGh1yDxEqYsmVsNUZBe4H0RTIdDCOp6EOKU2Q72reWm+lnOvbxrx/3Ks O/7u3v+ZAuA4yWwlaaoGCZeFlldla/Qf/UvusISJqpQbCh6OrOtQa9V5tH7EMisnFuLs DXkWwxoI0XGtGYQMa6yZNa/RRm6LY9Za1OKxGXQkLdQIK0U5buhf3jyDit+sfcrRZEzh z8eFXs6NlWCz2p32FvQy2Pko+olWoLk7mgrytBCKF0FNCJpcjIgFEpEDNMOfTz2gk/r4 WRIQ== X-Forwarded-Encrypted: i=1; AHgh+RoRXTpqvFV5+89cqlB187ZtTin7i+hfWiEeNdlGxeXPcrNiOP5n/9GeCRkNZqxwU8jEI5BXhrWikaGGZ0E=@vger.kernel.org X-Gm-Message-State: AOJu0YxyjR9xFs85iuFylKQjrWOWvyW+7gYBLLyKXomeVmzbWPWTjBj+ jGY/z93kUixuLvYQA1BEsj7hBjkDu5DmmTMEikuLV8TaxmKv52oytz7Yh4T0A2r3+kjodwll/ln ova9FEw== X-Received: from plat19.prod.google.com ([2002:a17:902:e1d3:b0:2cc:6ddb:debc]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1666:b0:2cf:c543:2c7e with SMTP id d9443c01a7336-2d0ca767b5fmr21739275ad.10.1785886777885; Tue, 04 Aug 2026 16:39:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:14 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-11-seanjc@google.com> Subject: [PATCH v8 10/17] 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 6ddc988576d0..2878a5181cd9 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 7768a8909cf8..f13100a7a63e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1247,6 +1247,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 @@ -1559,6 +1560,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 @@ -1582,9 +1585,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); @@ -1658,22 +1682,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 @@ -1683,10 +1695,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 2026 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.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 E8A3440B0FB for ; Tue, 4 Aug 2026 23:39:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.198 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886781; cv=none; b=Ifed6bH2mwDeuI0aCpwOC8Wgj/KTT8RKHPtJoI4e1EOyAx55Q6Mz+NW5bL/SM6CdiKnAsTK3SMV1+gtVjC21JxX0ciCZzhUC1XdwX1NtNXyBE3NCAMsvMN4P4WpIiMDZgraTITL0iXgEA1f9nyebWUIHQoopf95Llxz0Lfvs4g0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886781; c=relaxed/simple; bh=ofnWi+mEcP4gnrXw/JHS+6wXyfOySBt8ocjZq/4zwWs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Cqjd/5eyU160ItFCwwy9fxZ9UTQJFyxZnvFbPaHnO18TTTIijTGzw7zN4epQnLp4nUKaY2P0qf7A6V8GQQLad52zBX+FpZaVB4j0ZaCB7iwXEZoxUnn4s3h/9HBp6TT8KtyluRx2nVAc7izbGFK1wgOIpRXNnBwmDkXCsmsTVkc= 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=MemqMn3x; arc=none smtp.client-ip=209.85.210.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="MemqMn3x" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-84e13b57b2cso426117b3a.1 for ; Tue, 04 Aug 2026 16:39:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886779; x=1786491579; 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=WDzgu7ECyJ+WaNeJVkzUTS71kB1wuAWfhroN+BlFLXI=; b=MemqMn3xPCyNreAbomYY4xNWkz8a8b5TNeQ+rRC0Pq+v3iIb/w2Rc0fT/Pkoz1iCYc s7nUThFK+xThxPg+ncNKJ2ejPfSG+JWT5I+hu9/GQ/2Mwl4yuIiXOuNi6Ro8k1gCJKXC DudXqWILSc9eYatZTdbD6jRhZBsLKCuoNKtEW5RHME1hGLHk4r8sd/j2tCFuUJ32FsDb oxK0jBkX+beXIJgtCmg0bIZTZlmP3O6D2NHLGPv/CGDuEzFIfgEAAEdgPJL47K6J4ciI vbWQc+JKXaLwWOGn/7cILQUVrB3eYdzRLW+Hg25taFecZEs51SX+U/nvk3mhmyX4XnEk 18ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886779; x=1786491579; 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=WDzgu7ECyJ+WaNeJVkzUTS71kB1wuAWfhroN+BlFLXI=; b=VZkFhoTNPr7rsmkT5saU6aoWfX9iAkpMog6zLuU/CtFcF+AjsOgOpswupF6n4do86g 8B5bAEPlE8MeNDLgUgmobSmM8cuU/hvgKa+gDerjYyVcT42tMB8gzV5LtZVUU2xb7xG6 hYfyujVSVS6mHDUN9AuscVEcPbhMFqSmTHCNXj94HJXOlWhEe21hD5KO1OXUt7ibnru0 A7wBw45Gzyob2fP8sgvjMjSgiY/xcEJUfKiftAXh05f+evlGvGVSeXyzgwNHYTV38hjb baaPJBnRrtFWccwKKFyfKC7ufA6odXayZ7TaplILvmpaVCqPpGuL9yVpxPn0NXwzwFq4 TRKg== X-Forwarded-Encrypted: i=1; AHgh+Rp/ynpgFgAQKInJxCEu8MeHLvH/cPo+7r62kJApOBlzk/I7+lvX9OaIZ87iMnb0zZ0S1AKWTEQZqsIleZo=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+6QwUqZiXtfTWCMiH7U9x3EmxCejSvHCjnCFHVR0mJVvklshb ZLM8nZ0KJ0qaObQRtQ4zKOAxJrSn4XDNubb/DQ1KFjHSl6w0g7i5zKC58hpuDS3+ChRGWjo0zkR nDUEiOA== X-Received: from pgla35.prod.google.com ([2002:a63:b63:0:b0:c96:da8a:c09c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3026:b0:847:9ce5:d293 with SMTP id d2e1a72fcca58-84f2e03b823mr2404474b3a.24.1785886779031; Tue, 04 Aug 2026 16:39:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:15 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-12-seanjc@google.com> Subject: [PATCH v8 11/17] 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 f13100a7a63e..ec39ca82633b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1939,63 +1939,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 E99E745DF55 for ; Tue, 4 Aug 2026 23:39:40 +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=1785886782; cv=none; b=W1osWroN9JADGS5xCn7FSYnQ343xR7+JaJS4hbeRSwwkn3gPEoO8Roi5G3dD0/DYeqQyFDwlDRgr2fm2zoZBbqL/ziXIIQfRzezZL9Bcktqw5wW8XmY3/sgWt2SYPN21qxrPz+iKw3pNE/w+QqtVm2YFtXs3TVDb3wkCPJCKjKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886782; c=relaxed/simple; bh=4cNseqJHyCRSmBZnTa38ZFFXEI0fQN1xPMl3CqlFtU4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hP3tbNE/H2JoF3fx8905MO8RKI4uusgPbXb0tZBaMB+7T9zbSclTUdWis2Ua8gXDYZ/TOorc2THqJ/qeH2XminzEFZYOFkPSMh1txI4JJu65WCf0NgRdUSm/HQLpx7xDgveOqe/vjY5K+P3or+vF20mVcEUt9iYYdLG+MFEkE2A= 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=gg+Th5ao; 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="gg+Th5ao" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-84eccf9d899so465152b3a.2 for ; Tue, 04 Aug 2026 16:39:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886780; x=1786491580; 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=ylZqUeoLaLfQt/SbHDySdKd2LUI+wnxs3fPQEqBHbJQ=; b=gg+Th5aoEcwvkEwH2kUQf9XXHxPS7rqMPfS9MuJMu7q3LVrTj1NJrkMH7AYuxJepRC GJP1iWkQ3vLsow6bXWnRXXKhIFaCAtjire4IbfvIyq6GmFlrvQmht5NlBEUWxteBQo/l ynOMvhAHfio/xNROEs5SjAhyiSjMEh5FJimMd4pQsZnBxZB03plH3eP++e5vspD/C7M6 7OibgjUyoy9LkhREMooGNYBjzEAT59fdKV8Ch3toVrNhmJ6Wg3BTp8o0hPSKw1XrihIB Y2iOq/HHjlG0gS7yunMtXHcmtoth1N1B4vyKbVGrl7HqpEsu9LEB1lCsBeqy9Iu8sBJG ZPBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886780; x=1786491580; 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=ylZqUeoLaLfQt/SbHDySdKd2LUI+wnxs3fPQEqBHbJQ=; b=pqqydCsJ2POTd5LD31xCfEasHGktKf43tmiCzAwc7p498G9EN7Xpw2UVjp4YPrqVlO oXVcmJly3E7SAXLbDysqkwD+7Vo8CEfiKYutYwCRKa9Bwj+hGnUHr7uNgpxBaCujyB6R s7HDsRbC86RVnpo9piuXDaubboNymYVp3N2OsO8lKRzvpE5ZzqGtkPIsu2YM8w1fBhpN U2Lk/rDaYCeuVCMn8XL61TF4NrP9Jgdy7+KTxFGqsO02Py/1JAUzLN00N2KKqPoXtdVP +UPdzSGgqEYsDmQYoh34S2+BP596DKaX4y1q1RPUQSz8LQrEUAoEJreLOnY6eAbELnZF EU1Q== X-Forwarded-Encrypted: i=1; AHgh+Rr4uL3vcUM2Rb8gjZgCtsO+oDooxjKrOCtGzBTeqS6zamOxCJ1lI4FLn5qybWQJ+HC4a0hPikr86vSRtMI=@vger.kernel.org X-Gm-Message-State: AOJu0Yz/5tSYUr0TWFglx5ZFqH/DOQnJ5g03pvV1uJC0GsAxl6Gd+4gd NczEv6yYlaPForr8KQ0jIXv3JUII7jIcw0X+OoOgtBY9woE3URmhgznvUf7nYnRWjJ1s+F1hVAK vuOejKg== X-Received: from pfbhu27.prod.google.com ([2002:a05:6a00:699b:b0:84a:3b83:9673]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3c89:b0:848:7f56:1e44 with SMTP id d2e1a72fcca58-84f2dfcfcfbmr2273989b3a.7.1785886780109; Tue, 04 Aug 2026 16:39:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:16 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-13-seanjc@google.com> Subject: [PATCH v8 12/17] 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 ec39ca82633b..5667cd17672b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1054,11 +1054,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 1102A46C4B1 for ; Tue, 4 Aug 2026 23:39:41 +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=1785886783; cv=none; b=Hlg9K9OWAis0WHnoBkj8pfXEFBH75cqt6zcsUe3VARUyfgZgH3vkldVOVmMqur1Ot5kqkYYgWmpfYiERGLd12O4uyvyxoxpCmhDr0+bb/kKylZIa7mkSZv7NJDwedJvpjQ8vzsstTOiyqtAEx7YA5GNZ15qA8SWtOOKWtMznES4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886783; c=relaxed/simple; bh=OYDmse612vK5d4FLIG9srer7eR53sZ5xbP2KgtPnjDU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ES5YB3mkXswJUL5wdDBnMreF6SfRI3gE55tNCybSGLP7N4DPSgWYF8HCE+IvWy27rnOurLZLMGi8444oZ91qU/spFRL3gaGx7cu083tVmUn+kkxGmzrYrY9TjTh18g1RVXDXLy6P7F2ZjgJgi8OvEP3iHMmYbZ+lMuoi0QQCBlE= 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=WDw8Fwfz; 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="WDw8Fwfz" Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2cfbf3e4a63so9602305ad.0 for ; Tue, 04 Aug 2026 16:39:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886781; x=1786491581; 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=igGRF22UKQuGN63OKPz0N85SiBZ0lp1Jo/7y+mQANeQ=; b=WDw8Fwfzm4B8zTJ1QBhQW9mVf6K+tFSaD6GeAUsocMviZg0Zij1k3lqvaWmAuWK8+o s+NTNP/49xF7nJUXcITMHnEHgczhB6T77GQH5mZBxX2xp6aMzl5PRN8b3/gHd77NUdxk v/sRdeKcEt+PcYfSOlSDWifZwBfvaOlfSQAFKK662+wY8nIfDQBeYDlBR7GPEIGrckyl MVp94NXCXbsvMjJdk2SZa/MUBCneIRM4EWIlLidI6swR2jit4doAr/5Bck4JzuhnieaM ovkB16nzUnQgeQdEdz5/sYLxLTPgHja3Frui97mjpdb+a1KIRakit3Fu0XmbenkZHIHU YPeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886781; x=1786491581; 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=igGRF22UKQuGN63OKPz0N85SiBZ0lp1Jo/7y+mQANeQ=; b=TREFB/0MrtXsMRgY9CP/qT6mIURRaaG8ENlCE0gOzXm1knyHL26VdGqdUUHzEF+DBA jXSQBH6mzUxX1G9KzadKek+9V9V6p+0wNRrHDewYJy+hB7MHlwZTFh/vj8qwFP1rEQb+ H9Elvz0SOh7RYMX6Zo6CYGwWl/l78hMWvQEUup1htgzuq46x4aH7OtTT6uZFqak56JIy pUZid1E4AKabCAe0okXS0uyLaa7PmJwXh0dTsXloiR+pv5rzwrRwW/r3s0VBNOS+PSnz jI7oyrYZTRYWCkYdGl2+BVPTzFGdaejnz3H1iQwbouzmZeT7dt1etlnHVn/JkNhE7WNe Z77g== X-Forwarded-Encrypted: i=1; AHgh+RpMIV1Oj0/s+dRhT6ew/4Iq0ishDOJMOFKf63xkysRta+bsoFIJioFP3mfOjNYGN3FxqffvhmL5idoL/30=@vger.kernel.org X-Gm-Message-State: AOJu0YxNRsChwffLpJOWGU7ZItypwozL+m0tEwSe8G27UXuqGQc0UDsi Iw6TWT0K4Ui6TMcC2cSuCF0eyjqn5CNxolBxytmVm5cOl3zLggpohnruTsvMB/XPtgTQTuc0a95 zkb/XCA== X-Received: from plnx14.prod.google.com ([2002:a17:902:820e:b0:2c7:ec09:a994]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d58e:b0:2c7:1200:bd7e with SMTP id d9443c01a7336-2d0ca9838ebmr28241965ad.18.1785886781179; Tue, 04 Aug 2026 16:39:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:17 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-14-seanjc@google.com> Subject: [PATCH v8 13/17] 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 5667cd17672b..63702be799cc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1797,7 +1797,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; @@ -1822,11 +1821,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; } @@ -1861,7 +1863,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 2026 Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.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 13580471257 for ; Tue, 4 Aug 2026 23:39:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886784; cv=none; b=q9+nwRFUIANnC5zIKUo6qZAYdzQs0+8jacpfZKoimjoviMt+IA9q5Z6nAn1lZWFUflxU0HuC86MOpH1N4u7okjbtjTdgmZHWB6ED9E4jlqeV+ywwIfuwRsjBh9Af3zhA8PRdHh74xVhtHtcAfcrIJ3wePjq/nH9rZuempz5KAvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886784; c=relaxed/simple; bh=GbDDxRlbvftUHclixcrVm6MYfFPNZU3v3cMbJe0/dSA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u6QLYsuhviobHoEZyTCwf87sQ6QxYUpqnRnmKZG/iyZFjt0oiVeDW/6Smmod4bHs2IP3KquBp2oFbHCmKBCNAos9JxKf4liYvr8fcHNZ14HvhwnE21oeDmoHS/8VosgmOgB8ZcZmgtW7uvJfIKTEQLfn/8oBl1ZIf6xp6SDQJcY= 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=UG4kPBeo; arc=none smtp.client-ip=209.85.210.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="UG4kPBeo" Received: by mail-pf1-f200.google.com with SMTP id d2e1a72fcca58-84a251c2e3eso1948613b3a.1 for ; Tue, 04 Aug 2026 16:39:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886782; x=1786491582; 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=YqOvePDEGD7lsMrnIIEP459A6germ9Y0ToT6dwrTiHQ=; b=UG4kPBeoX/nzVapSgvaruEfaIW1qg4HcMXPjHLpr77Xc0aowSVJI0EX6v5OZZ4eJ3a CbabsN36j1w98lxtQJ6nVUwLTLQ/Hd+pjshrIFoIcEfIAnThF+tn+LA+S+nNGb7OouF2 /ulusf7C9nHp+u9xHVECFw1KXzlbbEYi/3DhEK7jb2OTVMExnp0miJt1cN13ybEmxPlJ h/bhbB64vU1HFim9MMGErXYV9OG/wUKuTNo1trbqmf4D2IiGZ9+RzGimLA9oXzae4N8V eVJYDIyeIxTZOAygCt2h6QsVXwBzEwiourfJtdeqhtEnamc0Smgv3GsLTEOhzyycHnC/ zjvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886782; x=1786491582; 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=YqOvePDEGD7lsMrnIIEP459A6germ9Y0ToT6dwrTiHQ=; b=It2Zb+jxrjRL5zb1kSvfbrHXn/LvaD1SSVYg93W7Y1bof64Iw6vgjuMC9K9pbGrcPY 0ZnmhDbQ54kry6poaqafaktDQe0h6KeY1DpmJKVXHvx6cn+xgnuHyuj8DTjZIn8ZKmjs TXZj8NNdCES5O/eVJCMqm/qo44sm0CWme8CkYFPqInfA7mtZuKQW1QF5dPsJE4U+Hkmk SLahkhpXntrhrEz9jTXKiTo9hQyheu1jBaJoQAoqeBnuMi3EjUynkKVin0gWlg/G8Ekr 64tct+FrElM2dfVVJ0W0ZvhQKuHpqxo1zWm9sWVNVRW1qf7UiS2JHSY/unx1D0ksLpaj 2RvQ== X-Forwarded-Encrypted: i=1; AHgh+RruW4bG3NrQfX7EDbagm9dNOMkq6HqjZU62JyEgvab8KWC3V7s8anwvDcJsafaUcZ+88T77YWOKFprzXzM=@vger.kernel.org X-Gm-Message-State: AOJu0YyO64WfEiv3iAJP+ezppKcAsP7EhCQoQtJyX/UGLFlur00iELLB ia39MpsvsTuCAbDbQs2hoRWa8H0gjCwy1gC/ZCA4SRdH4R8FSuWtzbG2dhy9gUqn8yH4LoX7goV ET3d7XQ== X-Received: from pfnx14.prod.google.com ([2002:aa7:84ce:0:b0:84a:894f:23f2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a116:b0:848:2a69:97be with SMTP id d2e1a72fcca58-84f2dbf31b3mr2422634b3a.10.1785886782216; Tue, 04 Aug 2026 16:39:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:18 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-15-seanjc@google.com> Subject: [PATCH v8 14/17] 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 providing 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 63702be799cc..070a12535788 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1796,22 +1796,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; @@ -1820,7 +1819,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 3C98D47604E for ; Tue, 4 Aug 2026 23:39:44 +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=1785886785; cv=none; b=BgxHDIGEvB+3wk7wTaalRvwzZspG/zSA4T3rIBlA+DA7xfTdIb60K2yrHZzoc6FPYip3BqEIyy4LmUVtLzTmccmt/SN+X+WFmGDizy8QW58uSBtlaGEFhbEWjEjcCYoxMVebkdWsu3mlvMwYdZX9A20lvXYr81aO75pCOeI7v00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886785; c=relaxed/simple; bh=2zDZMYJnAPP4eISEzQ3+eAg8OusTbWq0ZtfbBKV6o2E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u6G5s4O3HxBAL7rHZFpRRkaTbNfhDQe3CfhA0qeLgL+GjKg3ZUWbliQCWVpMdlnHlOK8I5VrGHhftIY2f0piGDkdIftFOrfMKmbemZIlbG/h7dxw+mB3DYoYr+AVI+9oisHmd0uNp2cFM+Mriuba8A3mogK6BxmH2INzfjCDp5U= 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=XtXitA4M; 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="XtXitA4M" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2cce14a21faso5572135ad.0 for ; Tue, 04 Aug 2026 16:39:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886783; x=1786491583; 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=Q8tVJEpYezHZD1AJM7SOc8wInFAVXdKdeRZ1XVRTCk0=; b=XtXitA4M+uovRh5BGfnrTYyZ3oDrU2NGg001XR2nuTKF4zDA0muYiHyii7883fF+uP 4db+eeNXADyIee0vcs8nW+3ots5XCct35dZx5gi9t/oYxSBomXu1ijdx1LiaTHvv6oPR 4XxkQeRH9fahU+DKIyTZNBjCoL8+zmdW4siRX6HCKkCQ2nsoSpHXx07Sq/9DqW7kjaqZ c9KeBBy3mfw4krSM4ULfmktid2IE8BqzOF+0eb1s8RGALYu6auTvmv8SxK/mkhIB+hwa blAVBUtaWpbzWro4CabmIXI8iOCYK9y6ZYoY5AmrDbR/BuBSCURXUw0Qq1cq3IsnuK3s Q4pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886783; x=1786491583; 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=Q8tVJEpYezHZD1AJM7SOc8wInFAVXdKdeRZ1XVRTCk0=; b=q+xKPno0jeJbmPiomHSj2buTgYZ0oviJ+qbo8PIvyxYjM4kRWHsgnpRCBAsYhi50B0 S8w4ON1Pb7GPm7x1Cd122YFEhWtKuG5PFQti6kNoBboZFmHQM1cKra2flT3kaIsKClNA a2HjzYui9t2eJtwo8Tv999Sv7CCYoQB1ZhNkOgGDvnY76KR2YW0q7bMbQxYGOnEgs1mX bTioiEGjroLm8dG5DpuSlwE9j7fdwSWTvR4aEt0+rXk9lm4oyXZuPEVpL09kByLoEpLA Uqcn5JCZVVgenZgTHyS3pwm34wZdc3XCDX3nQN1W0Ue8AD2ZqBOxi7bcjfhOaNXpdYge yWFQ== X-Forwarded-Encrypted: i=1; AHgh+RpPmyL7B+u3qrT/wrvBnl38zNOaypleGpGCMOIlUfUB+2O8PNd5HPEu3AKTqoelPsOfXlk81Jrm+KtA9QQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzgwnovxX139T4sXq8VgLC/QHLisH3CvJqwVlD3yEXwCbHRbbpc zWXn574iZI95rdpK9LoUiIhO3P8XXxWliWIgqH9D9r9QmejjB37Fyr4d8wyVGQRmUqykbST99Z/ kRQJCeA== X-Received: from ploy5.prod.google.com ([2002:a17:903:1b25:b0:2cf:cc85:6bcc]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2407:b0:2ca:c847:8e41 with SMTP id d9443c01a7336-2d0c9b0faf9mr14056335ad.11.1785886783345; Tue, 04 Aug 2026 16:39:43 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:19 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-16-seanjc@google.com> Subject: [PATCH v8 15/17] 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 070a12535788..cd438d06e4cc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1796,12 +1796,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 @@ -1814,10 +1813,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; @@ -1883,8 +1888,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 905E442BC45 for ; Tue, 4 Aug 2026 23:39:45 +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=1785886787; cv=none; b=sWjDUhsASG6nW7mQFEwXOEF7Idg8eZr4U502WR5Dc/y2fn8UIxmaGWDkOoyIJBQ0QZbdpFRd5NxaPCIuA/wHDleRyIanps/S8HWqOJsvhKUcrQWVmwDAAKcFxNSg69NQYEwjHR9F7XNFU3/7PTo/QzX1QrfTqDFWxgtlYtn+eY0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886787; c=relaxed/simple; bh=jk3Idq6KPZb6fR27iLsTnDj/FNc9cq600wFlmRCKtr0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NDxNjrYZi82T/rztG9x/GA9BLsq/wYUBRiSPc+MBixc595wpR8prryrB8pPBebMM9eZW0KxN04lx+eoSbyxCb/Ixm9mtFJWyLClo2bhPoNUc8czF8DmHZBzcGcfPeXCUKmmXszhhu3Vt5Vi97Xs4K+YDgl4mIYdNDvvJouHTAUo= 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=HYo8EkjR; 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="HYo8EkjR" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-c88cfe287e1so235333a12.1 for ; Tue, 04 Aug 2026 16:39:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886785; x=1786491585; 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=CKly7uqqnxARIiz4BbR0dueReH+dDjToiMJ3rjYyJts=; b=HYo8EkjRtwqv99cUe+tqK61dR6LGGnIupZaA5LOQ0649aU7Oqh7B6Lc8fa1Ms2qCN1 9Ty6ZvxmMiPBMpLKTZJcIGlJ4wJg7pJ+lDOP87EeMniwCga8JJSayDYyMzvSA5t0KKzw slixm8aUkrslrTs6q8RaWHciSYpiczzKV+r0gm3VFK9uAx4m8r2xfzwT7L0Y8JmAma2s xmj/M6a9hh+tH5sbl3TWjJjEqERxmrIh+x0KW078nqrAmolXG7dllGlCGkdImTG9pP01 9kdfGoNlCeXFmyYINsbnFjgB8TJfihiRGyQasHNjcUT9yP8CIejqFZ7f5JBO/933uY67 tr2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886785; x=1786491585; 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=CKly7uqqnxARIiz4BbR0dueReH+dDjToiMJ3rjYyJts=; b=InAfGd9+/4wzX7LcP7LwgYZSD4v1dy9RSm8oZhSKxEGlSmpkYC3hldWubT+TA8m4qA FHIa8uTTmi+1LLSaniMc7AVgck2WiIKEVf4Pq1NHNfR0c4XtqhGrAPYnVA6ldudIeUuD wk1Jnd9AtUlo5ify5a0u4s/tSW4fnlTUJi2kB57Xq1kEK6kHlznr3VLpObSx1rLjtmWx 6+pgpmHWoG6iUVbvQtQqaEu8H/JRXhWkk8WnSlgCDV2QCc02hnvATdF9FpZpn+5I73oP LP6vH0wj1s7SuntF7ISPqMZkMMvg26tv0vaLXnfQQFnl8Po+KZnFEGfIDVLk+D0NifWM kHUg== X-Forwarded-Encrypted: i=1; AHgh+Rr882caV8mHBZ/UzqSxtCXHDcLm2/V2RKrFr1HM3duJ/LGy1Vov4HAwqE5LAzyVz8jqBkByGI1eiiD2E+U=@vger.kernel.org X-Gm-Message-State: AOJu0YyjsSNNpXczvHborOqM6FmNzpLIIZM7R+sN2uGEzZ7/pZAAdMaT ZkXu7CG1agsTc2XX0bdrYaMuvJaklu5F2pKEmG1tcrQrevuiPKSNwibZ+HLJQQog0dhNlehN7Ac Mru/a/Q== X-Received: from pgk13.prod.google.com ([2002:a63:a0d:0:b0:c99:90dc:7b3f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:114c:b0:3c4:3767:adb2 with SMTP id adf61e73a8af0-3cb85df2783mr2539368637.1.1785886784447; Tue, 04 Aug 2026 16:39:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:20 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-17-seanjc@google.com> Subject: [PATCH v8 16/17] 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 cd438d06e4cc..02c389523f52 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -938,32 +938,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.571.g244d577d93-goog From nobody Fri Oct 2 05:29:28 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 B45F3480DD4 for ; Tue, 4 Aug 2026 23:39:46 +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=1785886788; cv=none; b=VbE1X/bxHpQXx4VYmi4yVWibVWdkks5O4JYW0tXAU3lv8TKjMyVCwSCQj+xBBYylcO8UHo13Ge2mPc3pjZYWbgAXh36/su/AHcWU1dsqE9pS3r51VJx+tB2W/rK54q7rQohyaeDWyp4jjOkyt8lpsKjd/UiSSACEH+SQKgSXQUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785886788; c=relaxed/simple; bh=8lvDeWtkfX+uoVNKASmySKo/OUYCoXFwueXvmd3AzkQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MRkK5AsSLQ8jvkbkL+cMvgZ28YU7emr2hg2EJiigr1FdA+pYkxVZ8rOIlnmXt5woGou0lsehpLq03AkUHLnjnHaE8tWH1JV29AZQgboDYuxa3mUS/KtmJ2EdPqjkZQ7mn+N9ZtJCoXmuHJGIiRaAdwaphQs11t6JysXdoW0Gv20= 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=iSUJBOiY; 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="iSUJBOiY" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-84e375d9736so462966b3a.2 for ; Tue, 04 Aug 2026 16:39:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785886786; x=1786491586; 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=MegQ9HN/KeyZhKX/8lwjVe1LaOD5fsnTzVy8ULN73io=; b=iSUJBOiY+HgMLbUGMNAotMbU5PiXqIaMaWvXY0MLVIr9o+tIRmg6rA0psRegaAno2r ffPGMyGzwkojgesHc4giEzFwT0V0CdlY4ty6biHI29cTtl+vtaNOy6Fm6ucCKt/3tPDF LiuiI3Zx2VMqjbHKPWlECEuhXu5JXMTPVLPEIs9S19BU3KX0/PICNQlsocBDVji4che5 +5NxrItGiI8B/3VsleiH0Y9kQLdiso3/VYKDR9iPTzR/yIOoOSGdkf79rNILTbj34HMW vJC/BU/3drXDBH+rtCoJ+QG+7rLeJPL7U3NY3KZzD97br35c5KCsTe/unOc+9oHUPb5b b7ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785886786; x=1786491586; 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=MegQ9HN/KeyZhKX/8lwjVe1LaOD5fsnTzVy8ULN73io=; b=YPaUT/8ay6Uno4PIEVtcDbYzPoKClHJvhFiTyWFVsqt7COXCzHC2AVlh/wOiwmpFzl RTon2qarQAYJjUXFypGqfwlBJ7uApdLmPUlT/Zo2d5DHqri8f/qJ9FZO/qYm1Rd+/reT 7US9j1o5MxRkc2F88xOj5ESuhoTQ8hfZNE3GC27vl3BnRVDNBa222Xv2JG62483vRNko Ks+oqGTLhczM1NGSHDu/HYyC2lxYwugZagwD1usWkCJx2Cx50tbHjDUoUreRCRBkA9sc vQF1+l6MNZ8aWtXoF8Bnw9lOlgOkkAs/xys/f6hzTay6yWiWYFgrkBJk96X71VQipg1y qYWw== X-Forwarded-Encrypted: i=1; AHgh+Rqc3gxFTMLM6VKTkAzkbLtMH+1HW8GaGkunULQH3d72EmRnYC/IiHYfRz/jPFnrVzV+iTj2JLTlZViWO/o=@vger.kernel.org X-Gm-Message-State: AOJu0YyuGtC3rAqRHzsJE30S5IhrBNjIigxScgk2yiEhJbTCBggkvm2G YdZkN+OC+2c/7t1WYWFnG8ZrCluXcl2IQfdqnn9UapFlG8HlbVaHnHyNvdaRCz54HXF/bmNNrJW U2/DP3g== X-Received: from pfbfv6.prod.google.com ([2002:a05:6a00:6186:b0:848:487e:d23c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:c95:b0:845:3033:6cb7 with SMTP id d2e1a72fcca58-84f2dfc8ec7mr1879050b3a.9.1785886785607; Tue, 04 Aug 2026 16:39:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 4 Aug 2026 16:39:21 -0700 In-Reply-To: <20260804233923.3504629-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: <20260804233923.3504629-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.571.g244d577d93-goog Message-ID: <20260804233923.3504629-18-seanjc@google.com> Subject: [PATCH v8 17/17] 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 02c389523f52..cbe4fd3457b4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1153,11 +1153,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; } @@ -1319,7 +1320,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 @@ -1368,7 +1369,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; } @@ -2625,7 +2626,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.571.g244d577d93-goog