From nobody Tue Sep 29 07:39:15 2026 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.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 AC6EC3B7B7F for ; Mon, 10 Aug 2026 22:55:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402506; cv=none; b=p5+Uin+iR1VJeMa2wczNrE15id6eXzWGQW1da7NoWTeOLhdyQqbQUywUTVbxFPGin6TmRdmQvvZqUrieDZLUgYQdQCmJ3RF4bhS/KataquhsTDVrIRuoPj8yuaYZjtKD9CZWHRZuomKvZ3UpcWQrkXVIBoKmlFQIoEQvE5t0diM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402506; c=relaxed/simple; bh=q5To44xBl/gE8wYNN3nfuNKweMRUq43GdPCEJJU3QqM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g72vkguXZZleMTq5Y2nlVxdTtpx3qj2Bv86izPUWRDzqsYaaN/fksynajmKqLTDeziIi+Qhapvkop7/8P5VVaNGz3nnTCiA2n5c0ClV0n7ZMkgPjNcdchKMXV3VFBKVpopMpCpzePuE0DGGo8L7529bCjQpnzY8w+olbhPXV3eI= 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=qE+/AgqQ; arc=none smtp.client-ip=209.85.210.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="qE+/AgqQ" Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-84885a4fcabso2664925b3a.3 for ; Mon, 10 Aug 2026 15:55:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402505; x=1787007305; 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=0u13O/4w/SnGeHc5D4Ig4kypIQphyw1Zk4osNUF32Cw=; b=qE+/AgqQkwFDz27cZHl+fQOIaJM5qgq63+B9GnUOuqcShfy6TSZImOMl1aQx2TVY+3 Z2Mh8JcLQfwg8GmTGnVGQpmGOrpWSXD/swNrB3NL+hW+w2CSU31aj/y808xdcdlYiwbX rtfSx9xf4/wHkk6w1yJGUuXCmiGxofMfpUSS1YAIwG2TrWyQFSItmTiQXjpNAMJS9I99 +9eEI7l2hkKN8ftkZSeL+cbkICbORokDJGcasVAKonEiTRCZl0TBJD3+fC8/NE3ia5eG nSeysKIdRZmJRQ9z3pBlUwB8typxhon4jvWsTsWRgSjji+7HFd4UE4krbeO+oYY9BHMU YEJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402505; x=1787007305; 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=0u13O/4w/SnGeHc5D4Ig4kypIQphyw1Zk4osNUF32Cw=; b=ER7WK9CTcv+WpM6iqjWBe7pNANPRm6rjPrAyC50hmFCKpJtRQJrWfV2jc6Qr8xcOfh AGCMVcKY2q3P5ZUs0chjDZLfjDW13gtGDTiLuwm6xC+7T024viJYrlFRZ6R11rubAiZg c5+atKn7DsjdtffmP7tnm6u1J/GhlVJAnCRyt+yB2RKvW9gyLG6olI59L+mjeSHu5z2r ZZQMYixJ9je7v34wPFcG+AIhXPs3TuB2bC7KQWr2zjIwvVf3KEaguuWdJMl9TPEC6ti8 Glp/9K+V45oP1bDZJVWzg2K2EI+bS62kz9qNkB85lCN4TJrzpwYyHQ3927UJqpQ2NqBE Zf3Q== X-Forwarded-Encrypted: i=1; AHgh+RpX9LUcXz6Rph99az6zG4kMhG1tXsXN5fSPSySSFywH1nedq/4tJMLvfF0KgGJrvkbzOwQRMaZszkO8JX0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw4QuB3CB+AtecsmfwTj+xZ3kiWWm5el3mjzo4i/W1ML70xuxI5 HMTydqszanHwR3LSiQdDTMlmk9J/nS6t0AUEb+id+T/KMkuh+RWcLw+XSwr11Ljwh9/TcKrF47T fcY4SXQ== X-Received: from pfbkq8.prod.google.com ([2002:a05:6a00:4b08:b0:84e:1951:8efd]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:8283:0:b0:848:86d6:298b with SMTP id d2e1a72fcca58-84f9b6c47f9mr4553523b3a.29.1786402504381; Mon, 10 Aug 2026 15:55:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:39 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-2-seanjc@google.com> Subject: [PATCH v9 01/21] KVM: x86: Update "last guest TSC" snapshot prior to enabling IRQs/preemption From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When refreshing the last observed guest TSC during a guest time update, write the snapshot before enabling IRQs, i.e. before enabling preemption. If the task is migrated between updating the local tsc_timestamp, e.g. to account for catch-up mode, and setting last_guest_tsc, kvm_arch_vcpu_load() would set the vCPU's TSC offset using the old last_guest_tsc. In practice, the bug is largely benign as it's not even strictly necessary for KVM to refresh last_guest_tsc when updating guest time, as KVM's goal is purely to prevent the guest from observing time jump backwards, i.e. super duper strictly speaking, KVM only *needs* to update last_guest_tsc in the VM-Exit path. In fact, the update kvm_guest_time_update() in wasn't even added to play nice with kvm_arch_vcpu_load(), it was added by commit 28e4639adf0c ("KVM: x86: Fix kvmclock bug") to fix code that no longer exists. As of commit 28e4639adf0c, kvm_guest_time_update() also consumed last_guest_tsc, to try and prevent guest time from jumping backwards. That code was eventually removed by commit f25e656d31ad ("KVM: x86: fix tsc catchup issue with tsc scaling"), but the last_guest_tsc update hung around. Keep the update even though it's technically ok to drop the update, e.g. so that the tsc_catchup updates aren't lost, and so that the guest won't see a PV clock timestamp that appears to be in the future. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 64D793D9535 for ; Mon, 10 Aug 2026 22:55:06 +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=1786402508; cv=none; b=hD+YvJjdWpd6H/MAZVnKvTZV2Rm9X96jkSubERMnAuo0xCu6LDos5IHtDq+9AC9aWpJv5RpnF/F8VOFaMVXwP6n6rNdbLffwF910np6ZNgdRJnbAkFXGf+Gu9UT8EkkMRoE1I2D0cdE+Z/qom5D8H4reIv5sBilLS4gETXFndHc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402508; c=relaxed/simple; bh=wLB7YDC5xsF+keI4em1HTCfj9TDcTp0KwwCz+Y/kJyA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cbCtFD5YIvHzypPqD18H5/7CGkkQFDnz8SBti7zKoiJwZJWL+Kvc4bhqLQwCgaoqxTTOrfjnbevf19y4HmZHtcdCpvU8peHsxHo5VdJ1JutjnOGcXGiTF1/GXNDiVa3UfQbibwBF/1EBDUWl9FGBr7toWGZXBitGd1SqgB3bphQ= 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=BU2j8ejd; 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="BU2j8ejd" Received: by mail-pf1-f200.google.com with SMTP id d2e1a72fcca58-8488ac68185so6524607b3a.2 for ; Mon, 10 Aug 2026 15:55:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402506; x=1787007306; 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=PzTkXY1O2YiLgBEN/U4cIdeKl7IFFDOnRDsOKpsivdg=; b=BU2j8ejd7MEl5tVX9B7vi3Uyj2CVmQRF8gd0vsaGJBny2MeF7ClGC9pSSCGwzqBD2H 7Q20TdsuPbs0QJu0xyhA3gVvks+32IVYxm6ErzNGJEmXlaHkN0YDXR6HWs8HhfrfEm/Z YGo5jQ27/axBhaZmG5WUDbiy1FSNkuY/rdFZ2SHiPi7MVgGoPRatu4bqDCx6AalduoOr JEDTTfgxfLvAvSlt0j7ZJjsPcd+YnqBA/mMlxzM8cxo3v8maMOalKrUZIIDwf9Zo3Ff9 /f2wDI59mC8Zt3dZk30rfk/hoEiZrSsuSmLzPqZOojdXWG9x+pMpVWqHfjS4F57tW3KC wEmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402506; x=1787007306; 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=PzTkXY1O2YiLgBEN/U4cIdeKl7IFFDOnRDsOKpsivdg=; b=okANRrva2MhHSgfQX7cGSQSWUkECWNSUgs6agJBakjY4arVbPLdYt4vZydAtaUk0Ig NED0lGVmtGF1PVALOoNwd/eswJUH39QHRQ7M8sprgYk9T+N4M5pYENVDP0nnPGisDMDz cT2N8T/etR1hnQVuQxx1yTX4Pk1N0OuwCFbMMH0OULHDgzbQ4UzgsQO/8P+0aRHD6len q7HMcm8W41YFy4ObsWsO9/uYBvJy2lnJz9/S3qvIsgLqiTT5CYDY6r+zhFB0oRtlUYGo +4m1CBSdMe8fHjsikMs6d+rHybFsRsnUlVZy4l6MXIn6cfa9Q1e3y0FmjtWYNSm9wDP5 4jrA== X-Forwarded-Encrypted: i=1; AHgh+RrMWmBqTy/H5GxpLe9lhPiUT9QWqLeJPk1Ngg0tuVFs4Oqtqnsust7xiMdUcQ6O1J96XQuOjQGVptg0f6I=@vger.kernel.org X-Gm-Message-State: AOJu0YzCOUIMkk3rPCwNuT9ysHiYQZ+xUdsQWAERxpLV5u8Si5ylzyi/ uV3O+OZK9n8wIsd5CL7q2qwy1o7F/ksnzPaLC8ib9scPq64rVbOBE687GBR0fOR4jxKYYEhh9As 03L9+xQ== X-Received: from pgww21.prod.google.com ([2002:a05:6a02:2c95:b0:cb2:54cf:afe5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2985:b0:842:48ae:1d6c with SMTP id d2e1a72fcca58-84f696a146emr27046272b3a.24.1786402505563; Mon, 10 Aug 2026 15:55:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:40 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-3-seanjc@google.com> Subject: [PATCH v9 02/21] KVM: x86: Improve accuracy of KVM clock when TSC scaling is in force From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse The kvm_guest_time_update() function scales the host TSC frequency to the guest's using kvm_scale_tsc() and the v->arch.l1_tsc_scaling_ratio scaling ratio previously calculated for that vCPU. Then calculates the scaling factors for the KVM clock itself based on that guest TSC frequency. However, it uses kHz as the unit when scaling, and then multiplies by 1000 only at the end. With a host TSC frequency of 3000MHz and a guest set to 2500MHz, the result of kvm_scale_tsc() will actually come out at 2,499,999kHz. So the KVM clock advertised to the guest is based on a frequency of 2,499,999,000 Hz. By using Hz as the unit from the beginning, the KVM clock would be based on a more accurate frequency of 2,499,999,999 Hz in this example. Use u64 for the hw_tsc_hz field since an unsigned int would overflow for TSC frequencies above 4GHz. 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 | 1 + arch/x86/kvm/x86.c | 18 ++++++++++-------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 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 ddb022cb203a..ca26be88c204 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -16,6 +16,7 @@ #include #include #include +#include =20 #include #include diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 CA31E40B11D for ; Mon, 10 Aug 2026 22:55:07 +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=1786402509; cv=none; b=Ge3ZG7yRSQYq+HUAhvxS1+8ZIAYV4jBtGaBugpknjpLvniFXx4Dz6wA6OQkIwI5FdyfIxLukbZeL/IOK8/d9D5Bm5mIQ6MEcC6VAdf+BqTEEf75DjxPmyxBtpe3lRIDrxJJw3Ym6pRSkUqJefHDji13/JQCnxqiWatUJgnyQ6+M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402509; c=relaxed/simple; bh=4Vnq3litlftGKPkKunWI7ZTCT4NvAgXB5UuXwXDvwtI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sZm/b6i4NhrYa3MgeZk26K9OeVZtf9aUyFmWo63oWFlQP9NSCHYjuCg513Bmcz9yftY41GFPWMZS3uro43nLLuSBOJPOIM87joZ/eN49MO39WwWNhZTpNok+LfqoIwq+BMwoI6gHQuEQ/OjDmOAt4qB5g213PrN3GxvHXCXr7/s= 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=ChTdeFl/; 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="ChTdeFl/" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2ccd1958e8fso35599275ad.2 for ; Mon, 10 Aug 2026 15:55:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402507; x=1787007307; 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=g3LUXdmTqXHcVQ/3nzNPOLrhFyzJ9u+6Fz+QQByvrF4=; b=ChTdeFl/STxksLCojnSC0zx4tAKa5GF1g/rziwwuY+TpuGe2Zws0GWO8BP/hhXNbuh bKQCTexutC//EkX5ho//rOss1Pf79cJD6ksAobQzGIQbyf+bbNbhIvHSCqxmNePGYOtJ M20fUuXfbZoDYaXbgb+bGq4jndK5FNmB1Bg+5Q9FDsie3AQft66uENSdu0wpiRIN7cqL g56ZUI07sDQx4BNgj7bvXIgoDIdge4IygmRWvZsdIX9f9ZbV7YwCHc5J2OuA7XSq12Yk i5MzDlWW67Gwtt7Ms/wh9FdHryFqEulxi5glCUkFMKvlWRAqZTdUYfNwPTbk9+U26P7X zxUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402507; x=1787007307; 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=g3LUXdmTqXHcVQ/3nzNPOLrhFyzJ9u+6Fz+QQByvrF4=; b=KnxyjRgPNdERYUV/mHNAC1HOtcHEKrSkIyfAX61EN/Yhh7bxIXHZrojj+fggkHuzym bqNydAhxQVN9Z3OwQGcULww1iZtHyOrz6/+Tuih4mI/coTXz2e/bCX1KyU+ZCU70SgfV NcYonZvk5Ri/jDHemGIoMqRsxR40fTVcNpXbhJ07jnshC1JcXf1NxLPfIhW/ggjzLFXK W2zFfmcEgGchm1ie8jgYt2nOW+Db1umY/NIxKWc8uw8tvz/CXSd8mXNWhjYFuu4TIMt6 Ybg1+xl4DiHCQtuZBiifLcSVLkLQ+zoO+likfcdtDRbbt3MJxf1QXlqIlDT+e424TEAf uzRw== X-Forwarded-Encrypted: i=1; AHgh+Rqix04Q0Dt+fVJT4o9x5KdDQwwfoBsQbk35wbWfEU6o83qosKdlKfzd/aDzo3e5sMgvJklLuPZRc1KC/xI=@vger.kernel.org X-Gm-Message-State: AOJu0YyRjhH6rheK3hGFzR+qyRCZKaXm8MHpR0nWmxdkJ42L/amZX50Y CJomzNMRuBCmdzwb0+iOvWQvn6yCaFx3uxEWH/cXbw3zDjeCSZ96crVQBLjKskzkRig2Bickkop B3Eszug== X-Received: from plhi14.prod.google.com ([2002:a17:903:2ece:b0:2cf:1f9d:da12]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e812:b0:2d2:da8e:9017 with SMTP id d9443c01a7336-2d2da8eb6e0mr162035195ad.8.1786402506649; Mon, 10 Aug 2026 15:55:06 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:41 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-4-seanjc@google.com> Subject: [PATCH v9 03/21] KVM: x86: Explicitly disable TSC scaling without CONSTANT_TSC From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse KVM does make an attempt to cope with non-constant TSC, and has notifiers to handle host TSC frequency changes. However, it *only* adjusts the KVM clock, and doesn't adjust TSC frequency scaling when the host changes. This is presumably because non-constant TSCs were fixed in hardware long before TSC scaling was implemented, so there should never be real CPUs which have TSC scaling but *not* CONSTANT_TSC. Such a combination could potentially happen in some odd L1 nesting environment, but it isn't worth trying to support it. Just make the dependency explicit. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/vmx/vmx.c | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 B5FD23F926D for ; Mon, 10 Aug 2026 22:55:08 +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=1786402511; cv=none; b=R9J4M9Tg8juo1HwlSS6+tsI+nPSw1j6Grr8rUoGe7zNsDi1ONG/DXLpzRYhrswlb2V41LIZW6Jf3EuH1kP9JxYNwAoldehTO+M7GlecdLYFsxS5K0B2cedAU3vJFyjl6rzHguovCPhjF+Vs2ZpeUj5M7FM5ADUGurYwCtEmSPHo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402511; c=relaxed/simple; bh=Enz6UW5jzOGeDIUWfw2NOrqdPdPLO7aFr63mUWyvVPU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=t96wtYxFgsNnNfiT/YcVddpEXmxsjGo0ifX0yrdNOwAPakSkF/iSIs/GIjSa7pMs/qHTIbXMwrfEhFicVX4OuT4ZlnULw2NwXUI/ZxyH7eAUojX9LWnz7wm6YrUptjkykF0wDPSgBQBf+6grNKizkyvxK5Fu0pGLdeaJgujwbV4= 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=m1lUxP8T; 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="m1lUxP8T" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-8488ac68185so6524670b3a.2 for ; Mon, 10 Aug 2026 15:55:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402508; x=1787007308; 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=XHzSwUqdKhsWaqPZjcPYfGCwbZbHLD3pwjjXIIUeXRs=; b=m1lUxP8T4ZJmp5xD9IVYcfnx/+Bdp5OyuYe7HT6d6sHS8YlfBOAJbiLIvONkiGTxef 71+TXixa8wkAM2M7u3LeBuR+ASfVsrFz7npgb63MYaZ0olYDDXmPMnm9fleBUSNZdnet QM1l3gYU31xfGM++UUR8pD4wLTLzLR7nqg0coZqAR/22VW69BPYfgpGVC0OoMRSnmTh5 l09eAuA1AXsw5aiwgscEWIQpelvkfw/dCYtwzL4Nvsr7Pc/wCjoGe2iCG+9+qLjOQDGR SdmzjNwmhAvROy571j5okcjWdLSEH4mp/VldaZ4wB7JWJZAjz20IlV2EGl7Lq4roPjs9 u0iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402508; x=1787007308; 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=XHzSwUqdKhsWaqPZjcPYfGCwbZbHLD3pwjjXIIUeXRs=; b=TGbAoCQ76+WFffZrQZxdttzrufJlL32LFO8KzU2KifVUsVZyzjzr4ScVajlwAgGPnY a+dUaa8vPildHNQKf0l0liJyGf59KzqxG/Y1Q0iA0Ed+Kx2MO5tsX17rNMiSD3sh01gM ITSR6nvnbA/kLkgTIn2DZkyuoO/CQGMzvlq1/Wr69b1kzpNOYUysDSKd7I93FIbrJSF2 39Vp+3RQ5dT8BsfOn11gXZO2pJiZlmvak/5TKBWQNR/WHpcmNAOAOHyRf5vsD04i+31r Br1stvYXeC2FD0RbEVEWx81p71zcmGKlRcHvsFZj51T/BddLN+ANfe1UzNjof3sWal9s D+0A== X-Forwarded-Encrypted: i=1; AHgh+Rrd1XCQa4qhVndKJH06k8rSjWIT8xdHMrhYQOylQXOYaQPqKLP1MSdyVMjqIGDmpyH8YwyE1Uk9w1enxJM=@vger.kernel.org X-Gm-Message-State: AOJu0YxiBpU/uzcatfoZd7Brec1Q5a0UFlO1/jo6osPnBZWvjPJgfgkP RCr2p61JA8yDcmYdKZyWr3kROWlqB3GbrRXpM3E9oPiFrFknUfjovYXVC2PApDQrrWVkVpVfGaz U5xcN0A== X-Received: from pggq19.prod.google.com ([2002:a63:d613:0:b0:c9a:2926:8ac3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:92a6:b0:848:2a55:7110 with SMTP id d2e1a72fcca58-84f6976eda2mr25023581b3a.36.1786402507740; Mon, 10 Aug 2026 15:55:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:42 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-5-seanjc@google.com> Subject: [PATCH v9 04/21] KVM: x86: Activate master clock immediately on vCPU creation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Previously, the master clock was only activated when the first vCPU processed KVM_REQ_MASTERCLOCK_UPDATE during KVM_RUN. This meant that KVM_GET_CLOCK could not return the host_tsc field until after the first KVM_RUN, making it impossible for userspace to follow the documented TSC migration procedure without a dummy vCPU run. Fix this by calling kvm_update_masterclock() directly from kvm_arch_vcpu_postcreate(), after kvm_synchronize_tsc() has already set all_vcpus_matched_freq. This ensures the master clock is active immediately, and KVM_GET_CLOCK returns a valid {host_tsc, realtime} pair as soon as a vCPU exists. Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ce6d155b2ee6..137a1dd2b85a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9447,6 +9447,8 @@ void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) return; vcpu_load(vcpu); kvm_synchronize_tsc(vcpu, NULL); + if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu)) + kvm_update_masterclock(vcpu->kvm); vcpu_put(vcpu); =20 /* poll control enabled by default */ --=20 2.55.0.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 6925C43B481 for ; Mon, 10 Aug 2026 22:55:10 +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=1786402512; cv=none; b=rrlha1iCILtyiddblpPiFMUZ5G1ocihD+RSi+UmtBsjjB5YUpOaUu8bOfnwMFm+ol/J9qHvNLwPg8gs+RDPi+KBOQxTf7xom4SD8DJOpcGCNCXZ55apI5oB+cQY3rfRAgy4YKm725jAZy5EmbcteANSLzLHK66Y4m1ie4n7B7T8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402512; c=relaxed/simple; bh=DBhEkQVI9NQRI1+LTosIl2HwXc4QsiNCRGdrxKptqFM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ig86czt2pRiQ3fnrIYvimZr2GRvN/G8A57wGud4fabw2xUk/mw+4EDHvREUoYdtLbOMCd0WgRBbpnku87yi7giHv8DZ7FPS2z3oXFrIuS/Ncg9cuAmJmQvxciMz9qOBJ+vxT7+JADzST88PK9/h7uSB5pMlNvMYZGQ8bXN9NkLc= 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=rbnG8I8z; 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="rbnG8I8z" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-c860544c077so5391740a12.3 for ; Mon, 10 Aug 2026 15:55:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402510; x=1787007310; 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=MCnLQXakLIHuM72bNNk8nqJeDhUwND5FLiRLpavLCaA=; b=rbnG8I8z2Mnhbe5Cla7pA4L3CKaELwImMjErJmSJxvzMmIxLgjvreR5b/oLKfSFKyI P8yjqn4ng5P2sGR+qy1DnuSEikYNywkYswXbRO0LlGrAyfRP7h1uDTPNJUnB6WYfkGo4 B64Rcs/0BmNFXgEop1fK0jR/CaCAb0vyZSs1xAf8sMj8BG16XYy8odGEomqASwM462Tl PcrFMZ+HmrPayu58wJgXWL20vIwPwqRQd31w/0cMFTmM0IgZktPo4cot0Mjod3c5IUSJ WOTUEmqH5JHHn0b3dN8nEi4O3i/naIfj1T/QsHfoZHouMVSadWfyv/KU/vnxj+CXCAfQ /ZQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402510; x=1787007310; 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=MCnLQXakLIHuM72bNNk8nqJeDhUwND5FLiRLpavLCaA=; b=pcKlTiL7aYHMgcq4AqZo+QQRf+sXKsT1EIbicRyBxTE4RPQMp6aUUqeokOsH0kVBdo 9zshzbYfT2tbzxVHxA0V9ghrWmVjwcVctlFi+ByFIDz/NgeXElHANhVGCQP9S3ioz/B4 vQJyPHSObOjkV9TRFRZ6vigUKw3wuOv+UPkpaa55lsBblVB/Ccw4MSsWNtKJqIqFu8I3 1srgwAKyy5aQtgpGiVWfYfAjEVOJm/wffbGk8TnxC+b67V8bVcgdAYLPyr7FlgpFcBbZ 4qCEHm4Q/T9o71p9HZtv7xAMsc325nSu8QWIhYV3LmRLlK9i7AZPygC3JyaMXPanxMH9 lYOQ== X-Forwarded-Encrypted: i=1; AHgh+Rq2YxjT8PyLrD/aaNYvM+q12q46GWczTe5Sd06X+jx9n1KVxHjKVh3Aofi11pHA622Jg0jHkmLYlJjDcw0=@vger.kernel.org X-Gm-Message-State: AOJu0YxyTBpNJZNLFdI4Lz70fZKBZgrOicqoRDlIOdLRi53Duvay8ySG BT6vV3Z7em0iTWwLignrC0wyjtKyxJhnWXUsVsVdClg3UuuIOnLJKSlSdK0jA4XmaBI6mPcUhed d9RF+9g== X-Received: from pgly34.prod.google.com ([2002:a63:1822:0:b0:c96:acc6:54d6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d38f:b0:3b4:8bc6:138 with SMTP id adf61e73a8af0-3cbc03b5526mr32318219637.23.1786402509474; Mon, 10 Aug 2026 15:55:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:43 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-6-seanjc@google.com> Subject: [PATCH v9 05/21] KVM: x86: Compute kvmclock base without pvclock_gtod_data From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse get_kvmclock_base_ns() needs CLOCK_MONOTONIC_RAW + offs_boot. Compute this directly rather than reading offs_boot from the pvclock_gtod_data private copy. offs_boot only changes at suspend/resume so does not need to be atomically paired with the raw clock read. Signed-off-by: David Woodhouse Assisted-by: Kiro:claude-opus-4.6-1m Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 137a1dd2b85a..67c762b3bf28 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -930,7 +930,7 @@ static void update_pvclock_gtod(struct timekeeper *tk) static s64 get_kvmclock_base_ns(void) { /* Count up from boot time, but with the frequency of the raw clock. */ - return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot= )); + return ktime_to_ns(ktime_mono_to_any(ktime_get_raw(), TK_OFFS_BOOT)); } #else static s64 get_kvmclock_base_ns(void) --=20 2.55.0.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 6BC5D43552F for ; Mon, 10 Aug 2026 22:55:11 +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=1786402512; cv=none; b=dv6lk7NWbOMOCK9VUkRarxiyTtf+dykgI0gWel5jPNCQdtjhP6oV4VlOUxwsyM4FFxETbjDOJHRdg382ID6GgeU2Pr3rhL8xy2rsmgwT/nj5JiGohlqrPMpyDaCzVsDcPoZAz0piOnbWnkcZjSIDCbqKD1tY+jGwkJHEDiQd+KM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402512; c=relaxed/simple; bh=tHgHzWXtTWzSurFZi7d7poZgSGg7TgtTAl+lGGS964I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uviXlokekF6sq5fd5Hdr6zoWw9URnqUPJ7VefXW2yhkgZFRlYuFAPxFYKUy8vFXW7XsVE+JpUHjSfSACUcHzCJ8dkUY5rH3U1nx5JqN6hpm4JTAafFd9ptZ52/cheKuN5VNVs3QSStWRVrzzebxP+VQ+4NxfVhLbYE6fPS/8tdo= 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=nB7Ad6DF; 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="nB7Ad6DF" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-848568a6f62so4849373b3a.0 for ; Mon, 10 Aug 2026 15:55:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402511; x=1787007311; 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=UeNZp5E2eUOzcTthah9NoXAW7FvUWg7wZ3z5+fNqOa4=; b=nB7Ad6DFmcHBM26xHgKAi8bV7rZqe747vNrrv1mh0ixoLCX7E9f0Ncgstv9/CFXsYe AU2DZIMOF4C58VskcTaYl6giWfTQUpkuwkw/3YqxsAH72d/1y+lcP7AYQrIHGeAgugds hu68lZkBrWC9I0w11ioDmi/bUnQA5WLYWrvd8XSh4qM4H2EFNs8dTh/xwxZ1rxwHoDf8 gnNyojiqcq2Ku5QbGjGqkr99DD7kptCioFjLU7KsybKE021uHs6NJh2Bora9hkkPUBnX 04032CvXdXhoMaDrWZh0G6vbiVhlx+rS++SwWkRY7WtKsgRETO7ff6kit1U79tac9xvw o2aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402511; x=1787007311; 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=UeNZp5E2eUOzcTthah9NoXAW7FvUWg7wZ3z5+fNqOa4=; b=Mz//KfhwhZSfNXd6bl7k59JmX5uNKKToVhZSCsaHgd4rRY4U5HMyrlk2pkXgzTCgon Zned3uNAw+mLQQj1j4rDBmVSA62+LFPTOfT1DsAWxtwTorv3H8qFEAQvC0YS0TPePPGA xExREPTh1uQjIIe7+R0v6WnVwSAsDE+R8f9n/ZcKlnUc+29p0b1NuGJ0mwlQFT9bnU7D PxXIdrKxggwj8VTzGcJAFYU8uSzB3k6tEYTLnKrdhlvyZPi5cXvJ1E8KxPukW/WZFj2q 9xAWNDhgzC+a7+M40avmZxY6zE7dicKgpKPBMQ2kSWkh3cm4txrjGycscWzrytctduUJ AILg== X-Forwarded-Encrypted: i=1; AHgh+RoH7eohH2dP6dm1zmxN5Lary5CM5AadddG6BnlW6zwpYeVAecGIhYI4GjqgN8aBc8eMgrrpP/P2OhrqZ7E=@vger.kernel.org X-Gm-Message-State: AOJu0Yyz29c+MX3xfpOMyYZskd7W8MvFlHXWnTqxdu4pz1+oe8AchsD1 yEeLw/jWdcmbXxR74FRkO8/rmBBrnOgjnjRRtZXNgRw5pEJJJaxgrpS5iW9oLMl2gVrOQb0n7ep 1UUTGkg== X-Received: from pfbeg27.prod.google.com ([2002:a05:6a00:801b:b0:842:83c0:8d73]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:9306:0:b0:847:98ff:4af5 with SMTP id d2e1a72fcca58-84f2e00ea3amr56097393b3a.26.1786402510570; Mon, 10 Aug 2026 15:55:10 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:44 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-7-seanjc@google.com> Subject: [PATCH v9 06/21] KVM: x86: Avoid NTP frequency skew for KVM clock on 32-bit host From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse 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 [sean: rebase on top of ktime_mono_to_any() usage] 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 67c762b3bf28..93b49be0d887 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_mono_to_any(ktime_get_raw(), TK_OFFS_BOOT)); } -#else -static s64 get_kvmclock_base_ns(void) -{ - /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ - return ktime_get_boottime_ns(); -} -#endif =20 static uint32_t div_frac(uint32_t dividend, uint32_t divisor) { @@ -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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 A6DC83B42CE for ; Mon, 10 Aug 2026 22:55:12 +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=1786402518; cv=none; b=jG6G8zV2ylIGTK/Nb4nDlfLw4qRXLdJh1yokjzgWdTcwsvQfqEQxp0xsO41PVyp+hDIwR99JUDPqt2A7BQakXQ9HerVqnXmSQ4KEzM6yQwSUN6/POlRZsTyEXciEp3Tmcep/w1XUkeugCPDFFqDZRwMVp9TEs7lvqbwW5GC74QY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402518; c=relaxed/simple; bh=9BxNKatIzOp+xBlZ24WPtnSnWe7eyb03hrAe5A7xVqs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qxzjRq/uLVCPUjU0EknZVhhsfjkgffMSuI61R8/DrZeI5eS6g7/0WqY5K4qJC1pyK4OEFIp8pnQTVy/lK2kAT9KIkqSRq2Zr17Nu07BR/tyh0gRu0f/ETT2Wks1PQWS+65HaePH9zcCuDL0feAhHHtQEXtvCNkMyVGmKhVns9rw= 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=kM+swXEA; 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="kM+swXEA" Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cab3e9cd922so1687074a12.0 for ; Mon, 10 Aug 2026 15:55:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402512; x=1787007312; 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=8sMALTNTQlRzIqiHYR1y7kSkpzP+a43a92KpCYzzzdY=; b=kM+swXEAqFhOdxmJlWYoBOW2bZXFZYjTu4S24ubFHHDfj/Nmvyf3EOPz0J2aYzeZUx SVRIs0fIQe23VoPnuP422yODwY2YbM5dAO6mAuHp5XuRigBvhMvncY5PIB7if8gsmy2V AlUg4F52JIpYJv/Za/Nw7Z82+lvaQ8pOSt1uzgysJyZ8of11nPc+RRI8a/742MvUnC7m 2u5fMzBJvRnw3Ah7lMqs9wfhp33WaAJ6/C4qtTMvljOBXmlNCDAzXvoMjhFSWMxVMQZB 1PdSaGsnf45OMzgQbwPubhQQZtrLgxI7ch5oEycHqXSosO9s5EGP4HQZGmJfTxRZ9Y55 qipQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402512; x=1787007312; 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=8sMALTNTQlRzIqiHYR1y7kSkpzP+a43a92KpCYzzzdY=; b=fG7mWJqvm/bJmQtCX0LTqcp6lrfa/GO/U9qQC0Iy0rFm78ZZ0IsE3/Kh1cJEeqqwM4 l0ASn8g3bGqQl5IITW1YMXh+sCnA4p9Zrx91S9HEltRdFjs5DZUoo7vkDKbuLupzHxKy 0WbzZ+B6bViDrRbwrTC9aL57bAfbLKedgFvlGH2Fk/yUwfMMMTedcJcPr4/GFEdt/5F7 +vhob91p8tzPaX1WL+6MF3G+qovImiGLIQDwSXJE74dvNNo92NOsxWohvkh1o8ICh1Yt t+B+Sx8xtZBbQ8KWmkYFcBRRwlyK1otkFDNTkGJ9w186gOWZUyBYTtGVRSGGRs5DppLS kIMw== X-Forwarded-Encrypted: i=1; AHgh+RpSOimi46ar8yxX3t3btUDe5B0LWQm+oXi3qOnOXThlwNndElQpVm7rp3JRQ1RuW3fupEPb5LTzRicX7HM=@vger.kernel.org X-Gm-Message-State: AOJu0Yya3BjIKKNjHmMczf5p/ZT6xmkxaS6QKG5sEgfaTkLlpO5fQA62 duASRjdwpBUN+TGbYXPkvqvxmtvNrFJtTf666IHt206xhTH89L4JUpMTyTp8OKUlIqAsY7nHinD gjvRCOQ== X-Received: from pgie15.prod.google.com ([2002:a63:ee0f:0:b0:cb1:bfdc:f782]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3181:b0:3cb:9594:91b8 with SMTP id adf61e73a8af0-3cbcea0aa13mr26685395637.35.1786402511717; Mon, 10 Aug 2026 15:55:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:45 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-8-seanjc@google.com> Subject: [PATCH v9 07/21] KVM: x86: Drop unnecessary CPU pinning when computing/getting kvmclock From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When computing the current kvmclock value, don't pin the task to the current CPU for the entire duration of the master clock path, as the CPU pinning was never about ensuring rdtsc() and cpu_tsc_khz would agree. As pointed out by David, ka->use_master_clock can only be true when the host clocksource is TSC based, which in turn requires a stable, constant and synchronised TSC across all CPUs. The CPU pinning was added in commit e2c2206a1899 ("KVM: x86: Fix potential preemption when get the current kvmclock timestamp") purely in response to a CONFIG_DEBUG_PREEMPT=3Dy bug due to accessing a per-CPU variable with preemption enabled. Despite what the comment would suggest, including rdtsc() in the {get,put}_cpu() section was opportunistic. In fact, Paolo even said exactly that when suggesting that KVM guarantee the rdtsc() would execute on the same CPU[*]: : Also, rdtsc() should really be on the same CPU as __this_cpu_read. We : know it's not really really necessary because the master clock is : active, but since we need a get_cpu/put_cpu pair, better be clean. Nothing has changed in the last ~9 years, i.e. the rdtsc() still *should* be on the same CPU, but super strictly speaking, all will be fine if the task is migrated between grabbing the frequency and doing rdtsc(). Dropping the CPU pinning will allow dropping the rdtsc() entirely without having to resort to a large "rewrite get_kvmclock()" patch. Opportunistically add a comment to explain why KVM needs to snapshot the frequency, because that _is_ a hard requirement to avoid reintroducing the bug fixed by commit e70b57a6ce4e ("KVM: X86: Fix softlockup when get the current kvmclock") Link: https://lore.kernel.org/all/ae8de642-8f14-a70a-1fab-57e2c4093cd5@redh= at.com [*] Suggested-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 93b49be0d887..56e095b14441 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 9894C3EF0A0 for ; Mon, 10 Aug 2026 22:55:13 +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=1786402518; cv=none; b=Ycydk4rkZ4kJEnf1YtKP302P3zPtUEFVNSTJhwRIMzKWOgIvUlCnDcsjgHX13p8HPs9++8uNJ31o/NYi7DGj9fk8sdfCzfy9hw8Th62BOl6NMn2VSJDUJMDtg8aqvjLUu+S1H5k/ewyOAUIp1ZDuKqkGmhz4zY5rAZr2BoxA+Vc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402518; c=relaxed/simple; bh=es4fCK6dWHmOsuq74FJz6dq2gHBwrNsXF0MHmvd49x0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pBKUvLfHvv1ZXmSt/JSRsFhrb5wOlpdePNKYFV6VFYJWqOYIJVDkxmIu6a+81XiP+m+bWbQSTTCzv8KIQOP6ovDU7UhJmVW1AfovEDtNnXDQy2c38UJHJRgWUF10gjm2B4dbe63JZdsQ8CKQqgr9lx6ZENhwAjO9IJ7aFxY3kB4= 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=bIsPqNPa; 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="bIsPqNPa" Received: by mail-pf1-f200.google.com with SMTP id d2e1a72fcca58-8488ac68185so6524819b3a.2 for ; Mon, 10 Aug 2026 15:55:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402513; x=1787007313; 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=6mJNwRsIdlgcDx1k8DwIZJ30uuN1iHndQUOpDPZR580=; b=bIsPqNPasVfvxZyKkp4PenPvUlebq2+0r1UxFcYdFRRvy9UY4tmluknNt60g6FuUNP /CJ8dIJjOatLfvK3zXDrCav9BJjCMgZKJGWPe06MyYZV3EftqjUh/cc+LrSMQv+Gfd+C Srn3re9OUlqcgo4BFymm6Ovr/miIJue29t4RbaZT+8eC9KKognEquT/isVavBJjyUq6d 6dGZrYT5uD17WWQvK/I/AyqRz2GDh+KffCu34q1EwNx0VRZDonLtL0y06F+ZgAZS/ljZ TACCuahtX1RWETo+uRNl7a6Ol+vjEycV/fHDLc7V1BbJTs7DmTkBNRvZ8MoET5WQwAez kqHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402513; x=1787007313; 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=6mJNwRsIdlgcDx1k8DwIZJ30uuN1iHndQUOpDPZR580=; b=dnKdOcf9e0ae2dtJIA/Lt8swyeH5q10ws7kbiXa8fK3Bv7tg/psyFTL6jBl/cgl98M mLAzmuf9Qm3YFoJOdlpMEdrzdGiDkiHuqOx6+PidmIlKeGYxxkg0MgrFqLroZt4/ARjG mP4KXc6Cq75+tEaIlTFTuGQGVhglYrkxYd7nQkETfWymbkGmzEEBCnn+xZ07E4vWgbr9 KfHKCYMg7mcjrH1XpKmAia6L0XDM1n+VhCudhVh3i4RSVHnFUsVqnll8EcqlkEkZTTov ZspvM2QCw34mtnviXtaC6ZERq4qvWTMQPfIP4qESmMzhKo8pypDVQRx2hicmIYjgwXw8 Jrfg== X-Forwarded-Encrypted: i=1; AHgh+Rpicwy8xcmmIqE0h32xH+bQw7Kkn/oEvHxsHy9qE7rB0CEXt2KtTE0B7KtNKqmR0P2X6cEg452oBfZaSlI=@vger.kernel.org X-Gm-Message-State: AOJu0YyiQLJ/ACoeV811lY02RWwyzok1VSHxLmxMH/MIHdqVKWbrGl/+ wjDCOp443z08B6s94s8ELzkvh5mcPe5F5dG2Kma1fdxgDsDJH6zPms7LBI2L7+mF1Jq3e72Weoe WBkNuZA== X-Received: from pfx36.prod.google.com ([2002:a05:6a00:a464:b0:84b:44f4:2d62]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3316:b0:845:36a0:9eb2 with SMTP id d2e1a72fcca58-84f69770781mr26628844b3a.34.1786402512910; Mon, 10 Aug 2026 15:55:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:46 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-9-seanjc@google.com> Subject: [PATCH v9 08/21] KVM: x86: Move "no master clock" fallback from __get_kvmclock() to get_kvmclock() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the fallback logic for getting the current kvmclock when not in master clock mode out of __get_kvmclock() and into its sole caller, get_kvmclock(). This will allow using early-return logic in the master clock code, without having to resort to a do-while() loop and/or gotos. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 56e095b14441..e0d623a122eb 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,9 +1690,9 @@ static void __get_kvmclock(struct kvm *kvm, struct kv= m_clock_data *data) &hv_clock.tsc_shift, &hv_clock.tsc_to_system_mul); data->clock =3D __pvclock_read_cycles(&hv_clock, data->host_tsc); - } else { - data->clock =3D get_kvmclock_base_ns() + ka->kvmclock_offset; } + + return true; } =20 static void get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data) @@ -1695,8 +1701,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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 2026 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.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 7B5F93ED3C3 for ; Mon, 10 Aug 2026 22:55:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402525; cv=none; b=j89+/pOuNG8rDf9xMRm0Y9iOcyh8Dy/K8eRCP6gPws3YW73BCV13qvnVDNy5UOV1WKIJhie+PVLAWGHgcL+sJQ1YbVqGg6A4oHxIBHZUNRdlcCBrjX78lghzfOQb597vak+g6gSwpWqHs57TPV7O87EC6LZVRNfY+l4wNdbzXyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402525; c=relaxed/simple; bh=qNv8wRrrsf5etbgikyiwqNy77PWXJvJYdvTzSRxJjfI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fkJDxxl546oJWfGVhHEq92c152se++wFuQNHxdXmB0wDM/aBg6p8dUAsmChmKjWP6iVi4TFw3Ao1ziEgeX4aA4DDZJw+eP8zpjYUnZM0QqbhCO2JrldAU728QQJRRN56C+P5MG2F34te0UrjGfL8yyswv9LLC/tmTl8xsdktK1Q= 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=vFTdQYRL; arc=none smtp.client-ip=209.85.210.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="vFTdQYRL" Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-8488ac68185so6524876b3a.2 for ; Mon, 10 Aug 2026 15:55:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402515; x=1787007315; 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=NF22hvAqvnv8oGw6Z0I6Swu4/ndBgLj6LoZAVm+Ye+k=; b=vFTdQYRLANcV5cHsLh/BSMEcbtqgeF864QqYobER6f8IHw/Zua5gfgLw8xpkYI6SRb oBSy8eb31mLsLJA2cqCOd20qqKWRIZvDCp8z+/lzJuobGwmV73n9xUlqt/cbBNcOkYtn uNAyrvAuRwG7ZOsso4NrKsfbJXC+eNnPcOXS0ZpFdmRCpJ6qxvv6tZ5kgbFa16R7us6z iStA10aOWc/EpKHxUHbhG5rkD4lUu6TmpPQH7GUw1l+8BNGeL131UGfyFv0kkZj0wt61 Vhu38O3Q0YE3wevAVKhjFCJ0/Nh5EweXQBt2ateAgNudaA+I9Sgs06+1WkXKBc5Ya5gF GYvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402515; x=1787007315; 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=NF22hvAqvnv8oGw6Z0I6Swu4/ndBgLj6LoZAVm+Ye+k=; b=Yi9T/ru9/jx7GbK5H01Vu76wAJDJLQHFpJqnHwfXnaWcM0bDD3y4IPmYwvSf9S1m3V /wKyaCwXcJOlNMDqB6AtH4h/vNkmyY0ovQVab7QqQGDYdLQh6igTdKTb1tTzf/tcYegZ 655HWrB9z8HJ4Xhb2Kx1rNnpwsWeysKB8XiZW2NOzYiW6e9sXZ2aoDsMBQ4GVpbnUEQ0 SUakUU//7FgUbm/aEWTuuLX/XMbO1qma08NuQCb7K/gIssoSiz7TFyLCDsPDS0QiCtu6 w1L9aMTysv+zz5QflfDrGLN5pw47OPT3mf1LZtjwwQOAM87eccw5xr47h15SY5w2LQwa YhNQ== X-Forwarded-Encrypted: i=1; AHgh+RrawcoOlaFgGbLFS2MXoNeRjyJpnBPM2Eynt5ku+PTovheV0NlHwwhyLKJLn/llORB6TVLqsx/j579vJkY=@vger.kernel.org X-Gm-Message-State: AOJu0YzfblWtpmefGNfrXcFySdLJLG1j70ghW/XXvGGHDzGFoIZHy/Tk 4YSx0fDwY8j17dYsPtFrsqwfpZz8cBBbzWQ2Tu9cyPKRgF4NJQv/TLbKSH80QiANy4dRMtK8Q3r jKcVi1w== X-Received: from pgja13.prod.google.com ([2002:a63:cd4d:0:b0:c9e:142c:425e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:6ca4:b0:848:7e85:f208 with SMTP id d2e1a72fcca58-84f6977b09emr29265155b3a.37.1786402514421; Mon, 10 Aug 2026 15:55:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:47 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-10-seanjc@google.com> Subject: [PATCH v9 09/21] KVM: x86: Wrap all of __get_kvmclock_master_clock() with CONFIG_X86_64=y From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Wrap the entire use_master_clock block in #ifdef CONFIG_X86_64, since use_master_clock is never true on 32-bit (host_tsc_clocksource is only set under CONFIG_X86_64), and declare hv_clock inside the block so it is not left as an unused variable on 32-bit. Signed-off-by: David Woodhouse [sean: keep only the CONFIG_X86_64 ifdef, update changelog accordingly] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e0d623a122eb..304e078cb899 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,27 +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); } =20 + data->flags |=3D KVM_CLOCK_TSC_STABLE; + hv_clock.tsc_timestamp =3D ka->master_cycle_now; + hv_clock.system_time =3D ka->master_kernel_ns + ka->kvmclock_offset; + kvm_get_time_scale(NSEC_PER_SEC, tsc_hz, + &hv_clock.tsc_shift, + &hv_clock.tsc_to_system_mul); + data->clock =3D __pvclock_read_cycles(&hv_clock, data->host_tsc); return true; +#else + return false; +#endif } =20 static void get_kvmclock(struct kvm *kvm, struct kvm_clock_data *data) --=20 2.55.0.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 63651442138 for ; Mon, 10 Aug 2026 22:55:16 +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=1786402518; cv=none; b=uunEnF986rHnyhalSiLK3VGn5KItUOOq+z5BJ9u0pMOCao8kxb08PqMUkEuKDPDhSiKb/GhhLAaK+e2d0KIxdwO/uqscotrsuWJuJwSCWyHxjuYEL1KYG2Pld5GKSpZmjpDUiOOVNIhUb2ZYNVgt1HDd/bM15YXQBCkq/r88um0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402518; c=relaxed/simple; bh=CVnhoSX+qbZJfZo5pFMAK6JiunEH6jHp8TBgm6WOvd4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BQqpHYtAYb0KD8KU8g1DHOXHfb1PXcGM8a7x+sQtvt9cx9f9OkrhDAwhxqB+rq8unKdbwWUVsxRrd9ZyLgzfJZpUpufp6jV0EWyCAP0YL+SWtJgqz0YVo++S75mRPcd+9ffFqZRlxJ9F+QycaCrpRQOUG2IGXTWnOhnUYkXv5L8= 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=MdkUwrYq; 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="MdkUwrYq" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-84e01a62d99so181068b3a.0 for ; Mon, 10 Aug 2026 15:55:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402516; x=1787007316; 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=yh0HMLDRPnv1g8EvjLjzH3sEAJckmpo3zknzdaSQGA0=; b=MdkUwrYqwbEjvaa2hHVkhcr5GcewoRf6eeL+KaP6g6GCI80+rWxQFkvmnVv5WH2i7e gcuLGK3mq2eQIbreTPAxg0m9AKXpSpEzmQqv95WAn+W8oPiplCWQYDgkTCpuU4DpZEnC AaYYfdl/3uDQxCm/Iw06JA77L9hS7lFezbUO9oeQIs1nUsSHT11aPJAt+O4GGoY1JI34 IaLovgm+FwYlE8giABRP7pXx5gwvKNeznoLIcJrleVwgXlNBlfglrCfzfeDXtu+WOwqH Rjbf36Oe+LesMUnKt11mKCu/CgLnc01or1CKZzIW2Oo376N+Xh4rjRtro2j4ZmU5YTAU zZzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402516; x=1787007316; 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=yh0HMLDRPnv1g8EvjLjzH3sEAJckmpo3zknzdaSQGA0=; b=bI5ejcNc7Aolk+xJrSge9U8vk6HbL+qw4WNxkF/qCNVD/eWRuXGmnrFR4Xjp7dOTpB mzpPTbnd68eVsUoi0tmicyqAa0ykvr77w77eZODyllQ/6OdBMjqNtD+ocNJDFdeyPBig LJrUIETzAW/bj9ADwizr8AU0HG3tyGbPdvrtkk9+ze47ZCYvDYAXs6e0SEIN8UyGq6gA I7PV77y37vGedWNF+jNvQu/lrnXDO0hFHRE5ugZkW22PG6rZLcevPK4t0cgSJqLzMu9U OOtI/fv9hRlEZWdEc/17F/ZOe/Vff9AyYUzvIIbSJEwshO8ZINE22av+E2x6Ub7lfZoN AVpQ== X-Forwarded-Encrypted: i=1; AHgh+Ro+u/ACPQUlR07wlCjbtdsRli8SNOyjb63T+q/BepxUxKKjRMrQOpVWjKPGt+seJmulfbljrkmVeFWv7Jg=@vger.kernel.org X-Gm-Message-State: AOJu0YwWyOp3n4TQnWEPjsPSumBzFE8HPzeHY12z8ZgSbDsB63DCFryZ R3k/YJD+8dSLFSFKylrTxcSQG5RzYwPrNLLzSPalfA5gTlS1mxV5+6u92Bo0+NXOSEIOOFoVBiy y5XdFWA== X-Received: from pfbc4.prod.google.com ([2002:a05:6a00:ad04:b0:848:478d:6efb]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:94e7:b0:848:3f91:da82 with SMTP id d2e1a72fcca58-84f2e12c1fcmr51167957b3a.32.1786402515490; Mon, 10 Aug 2026 15:55:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:48 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-11-seanjc@google.com> Subject: [PATCH v9 10/21] KVM: x86: Fall back to non-master-clock if clockread fails in get_kvmclock() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse When computing kvmclock and it's currently in master-clock mode, fall back to the non-master-clock path if the clock read fails, e.g. if the kernel's clocksource is transitioning away from TSC but ka->use_master_clock hasn't been updated yet. The rdtsc() fallback was added (well, kept) in commit c68dc1b577ea ("KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK") purely to avoid uninitialized variables and compilation problems on 32-bit kernels (already addressed). In hindsight, keeping the rdtsc() was a hack and a mistake. Link: https://lore.kernel.org/all/CAOQ_QsgVqS_PuJo8F10Gg5Xw+tKt+5gDx+kJf1j3= CiPO4MAOqg@mail.gmail.com Signed-off-by: David Woodhouse [sean: isolate from refactoring changes, write changelog] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 304e078cb899..33ba840d3267 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 79F2D3B47FC for ; Mon, 10 Aug 2026 22:55:17 +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=1786402526; cv=none; b=at0qCtBMQJWNd5maf4jt8bljgQ/XMWPCJx4oVh3rpD7xvJsxfhhLxUNNHqZEtX3upGprUiFfa6gMhGrblKKejSXsIHqgpLMwxRZZuLNwJ6xZbi33sUwrM1DGUO1JloykfsJWtli5b6PaeauFUxu3PbMOLZYY0U4OqGI5XKBj81g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402526; c=relaxed/simple; bh=B+zYaK0jv8vr3cfVGCR9hMeAybdTIs4VKA2n/3aUX4Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nVm7TVbnUSUm9doeKuos/778/nlt7mltAbuTHqfWauhg9BaxDVeg3xcAtvyB8UuG3T81jwYjXtfX232i/wAALkK0t/cA4N2G9OpeF0OyKaccB0tK6s2lCsisUsjjAMQNHLomilUOPbBg7nlmoBmMfQXFV+VONCBnFp6/HY/KOjU= 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=K2icogYW; 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="K2icogYW" Received: by mail-pl1-f200.google.com with SMTP id d9443c01a7336-2d122bbc621so71369225ad.0 for ; Mon, 10 Aug 2026 15:55:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402517; x=1787007317; 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=dxR15fCIX+zSacQBw3/oP3E10naqYejo6NYc0FRWoJM=; b=K2icogYW1E0eiFD/KV3TAZ/+1oYl/SOaDpeQ/mcq3yNHsvUFQSBPbHKb8L2SVwkdQp gQ/8+mhMgQeffmE0aoD2vspXRp5hOUXr3kQNTJ/xdh2EcOy68W2aEBinFHQj/vwfNcRf Yi6S77H2z/zE4kmNdfQ+J00KS8T427bpsDvS+nOLsJsQSQXeUNq/KSX2DArtYxXKqW8d nslmvw3RNu2ph/UddTMlr1hGk8yV8ynfn+11H/oNn0xIo13WEVgjAL9t8o4gPiEoxINu uyZhlQpyUSFCeOGQvHSYolnnpdwvoS8FcQE2PF3b910JRyEmFeoCUxByvJHe6YP+AB4u t1Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402517; x=1787007317; 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=dxR15fCIX+zSacQBw3/oP3E10naqYejo6NYc0FRWoJM=; b=h4R333stgnuPDc5bxo7/QE//fZXr7dkARw9DNTpC/zoJGjv20qyQxI/1YUXI6AitPB DN6PuQWHXPGrSC9KCvSOv4SX5Q4GnKs2aesXr5BerBP8srII8ahpQRdn6rn5qeEnrMWV AmWCIiAuikLdRiyhqSKuR60SHg2WLJICH34QnkwSioXBMzRyytGaG36KNKn2/R32VAUL hJhy3kpM2mAOztb7aK0IS56ST/xbPXnfq1N8E6ugBPW97b/Kz3Gwj3q0fvvv6feO7/Qc rNxnPuU3dovDRTGwfoY/5bpwWBIl6ivfVTRhSLJX/r2TtP/lMtwC5vZ9P2PTczsMP9PP mt+A== X-Forwarded-Encrypted: i=1; AHgh+RpgmGDmJuknBnpJWXVYe+i3dLX6/uAkllQSSsCqxCrhBwhGgpUimFs84BXShJF4OiouHM34dHBmQm6uu6I=@vger.kernel.org X-Gm-Message-State: AOJu0YxHwdfqCM0a8o35ex3/eysnV2V9WIVkvfhFKcxe67hz0hXJGA2o AF6Ot1nIxKgAhjQw6J7VBDbLuCJRWjKX7mrrfQJ9TfadgHqkU8iC0oM7RU3migPNG21Y4Bd0QL8 Qf1oGkQ== X-Received: from plgo14.prod.google.com ([2002:a17:902:d4ce:b0:2cc:79e2:e717]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e5c1:b0:2d0:cc92:f7ae with SMTP id d9443c01a7336-2d294b29499mr283668315ad.3.1786402516633; Mon, 10 Aug 2026 15:55:16 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:49 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-12-seanjc@google.com> Subject: [PATCH v9 11/21] KVM: x86: Fix KVM clock precision in get_kvmclock() with TSC scaling From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse When in master clock mode, the KVM clock is defined in terms of the guest TSC. But get_kvmclock() was computing it from the host TSC without applying TSC scaling, leading to a systemic drift from the values the guest computes from its own TSC. Store the VM's TSC scaling ratio in kvm_arch and precompute the guest-TSC-based mul/shift in pvclock_update_vm_gtod_copy(). Use these in get_kvmclock() to scale the host TSC delta to guest TSC before converting to nanoseconds. This avoids "definition C" of the KVM clock described in commit 633d7652f80f ("KVM: x86/xen: Do not corrupt KVM clock in kvm_xen_shared_info_init()"). Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/x86.c | 61 ++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 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 33ba840d3267..61c65241554d 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 C1E53442B3F for ; Mon, 10 Aug 2026 22:55:18 +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=1786402525; cv=none; b=IJyYt82nw1vXzhqqwzTMSSev4R7pTX9zg3YoCCDb3eRh2MSlmjIroslOcV1InLsfBca0xUsgGhrMBX8rYg4kRU8v2S111ZaWDbzddHUWJzuMGow/LAIQsAS8egUw5CkdernHYT2rkliYnK2Nt8yP7vswZdkAvA0GYM/XCSaMDyU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402525; c=relaxed/simple; bh=a4k1aVZHVVzjhch66sZIH3Bx+qyvEkl4jDBTXagNTDA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=c6qXynDNZ8Xd7ci/nX3g0Ot2Wxm0AwiTrxwPVqlQQHA6lTGHh5v5ckO81MuwPJLSsye0e2b4zkC48s/Bke2QvayCKRGYz89Co6eIk0whxEqn2sQ+bXTWnzHVVgI4GQaEHS3ZPjI81ZgMjzyAtYTFtZdgdDqEqYC8kdU1VxW9u1U= 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=czUvW8OY; 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="czUvW8OY" Received: by mail-pf1-f200.google.com with SMTP id d2e1a72fcca58-84c4cd31b51so431108b3a.0 for ; Mon, 10 Aug 2026 15:55:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402518; x=1787007318; 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=0X71CL0KX6+LAhAdItGgBlh9OhvJ66AL4rVV3P4B+SE=; b=czUvW8OYmBgsRgor4129IlqZxrKrmQ+fiMI2DGtPVkF4twkgQ3yyps8x1JoV0Rx10N BkhGWo1zLCTBJfUZkgzq6C0Ubdyaez5q9zlv3Zy/J8oCuHHoj2Sz094lrdy2oDJKD/dq /EgOK4UZ+SRchWHIcGMA+YYpxICFGLBfZb3PrasETWQskSVT8/yqp3BGEw/5Zh8eKZ+Y p+Xupz/vjernQBONvka4svhV+zT2IUTZYB5xrHomSVNNJEDNBQaamjF/7r3GBZYDp0Ko CTj4bBj+/alChc9v9GHRyQ7YS58xipsxny09C/kycWGt/WaYLaQzZ4oXn2I7UnUk5372 Bbkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402518; x=1787007318; 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=0X71CL0KX6+LAhAdItGgBlh9OhvJ66AL4rVV3P4B+SE=; b=Brqi3qhb9/MfFqWdOaR24GuW9e/vBXBEU+Rft91jjLnirSOne4MsBSotDsbJRnUt5H bMvdXfYtOJHcaAlhPWndBYtc62Q7LXp0nlA7Eq3jOt/TNIeMdnhCsVmOFJgJLTTO18xk 3v+zW9x5gfU7IaHL8JvA8/OONtLI/mV/fgg/rlBC1/0dE/2P8YmnnKHbdi4SQyL6lcy1 7YUrsdDLoNr4gxNix2m7cIVqIdyovnXMr7BxUbEUARmjlNafbyEdpSzFA1P07bqoN+OW KhbseEYb4rSYzsok00TpWXrXI6nl9TZe3IAraizcY4EGGQx3k6d8aFiabX2OW9nSe9PM TkkQ== X-Forwarded-Encrypted: i=1; AHgh+RoESmEXFg3UNEI58dZhTxxaoqaks+PHvLvaohR0zg4MoAl2FWA00e2EFZ4NKv/I8HzRZDCTKUaVlj4hU3E=@vger.kernel.org X-Gm-Message-State: AOJu0YzlpbmXeZN2Zxvn/nfyEDSEWq52tdKadumY1uRAXoPgZiTkrr74 shFWlwJVMrli3E+KV4G85iIG10rrP1OqQ8uz8e99j5EQtB1P2sAKBRY5qVh7qaE1mIxiR366rrV XNvXS9w== X-Received: from pgo11.prod.google.com ([2002:a63:e4b:0:b0:c9a:8872:2a15]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:9293:b0:842:446a:4cb5 with SMTP id d2e1a72fcca58-84fa14bb837mr2038335b3a.0.1786402517678; Mon, 10 Aug 2026 15:55:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:50 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-13-seanjc@google.com> Subject: [PATCH v9 12/21] KVM: x86: Use get_kvmclock() in kvm_get_wall_clock_epoch() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Now that get_kvmclock() correctly handles TSC scaling and captures both wallclock and kvmclock from the same TSC reading, kvm_get_wall_clock_epoch() can simply call it instead of duplicating the pvclock computation. This eliminates the last instance of the "definition C" kvmclock calculation =E2=80=94 as described in commit 633d7652f80f ("KVM: x86/xen: D= o not corrupt KVM clock in kvm_xen_shared_info_init()") =E2=80=94 which computed nanoseconds directly from the host TSC without accounting for guest TSC scaling. Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 59 +++++++--------------------------------------- 1 file changed, 9 insertions(+), 50 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 61c65241554d..50e92090ba65 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 299A04457AE for ; Mon, 10 Aug 2026 22:55:20 +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=1786402524; cv=none; b=Y8vkOvoL5Al89voRZPX8wt+n263Ou2I78BUs4mB0sS2B591srC5ZDEfG0mOTwGWCmEvxZKjU/6akSsfqAVY4knW0NsaVnVUfb1+fqv1z0suUkRTYbH/wS18ZcaWroHd7l0KaFBEpYbXKwNTdEARfhJO/6T9ng2jfEofIs3eW2bM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402524; c=relaxed/simple; bh=HEa+JxnqvNvLIMvTG8Br8LXOSsVcM74pllbSgSYeaIM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BFBSe3J2b+IsL0UfJvCq92MNTWuDyUNU1zg6nx1s3GvWAsQ1zqrjkYNTVvAd836DHpN/RYGiiBBcs50ZkUCzfMbGERVoIqv87indso1GL26cmAadfBrFsJbBTdNEdqWENDqtPj4KjGL057NfnL2GrGWlyVJRlnhtqCYrb6hVNxs= 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=tQtUMhw7; 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="tQtUMhw7" Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-38e5a616d07so2823600a91.2 for ; Mon, 10 Aug 2026 15:55:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402519; x=1787007319; 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=O2Apit3dTRO/Avi3ufPqsCY55zXXjY9yvVr1aPW3ZCc=; b=tQtUMhw7NGiElkujDBG0t1AWlNBS2ozUJ9QGKq/lvi1TD4ukxVyMWcjg5qfRw4ShfM WPu1Z9k0NF6zP8YjrU/DzOzavVT/H/CJvRQ+3uD+Y3p13zpcaUqT0WscWZzdte98lkqr Z6jNHIJ8mLi7Fig4z//MWd/Y/v0drPBlaROBqR7sdJmOkIyRrXmzvCPA0Bau6KUnHjrr Vj84X624S8vvyO4eN/8WDRiVnvHLfKj0tSlCOaWgcsM0q7Mv1IBAHyadq1kOiefSznQZ +KFhzD6Ip8xysay25sevzxw7pSoKv5drnU8SJDIKaKJjES/Bo6z3SbKXd8gOe92axkRD pmjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402519; x=1787007319; 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=O2Apit3dTRO/Avi3ufPqsCY55zXXjY9yvVr1aPW3ZCc=; b=sD6jPrS6Dm+/CPZ2Q/RZMWHC7wPBRtqpPhINc1bnsCxV7gmgPZS65ZmwcA9d/dS3Ya n03717ZE0iju/CHSdKcUJ+5yIDkfS4qCdMD7c+93g+nbHTbEBb6jc3k+al4UUArFkTp7 7pqXefnttnYjBm3QvVB90l4iFJEgHtLto5JwUHQW9DOo8Qhn/voXbuQpOsYQYACSEOgd aOS146Gx766D0/3R0OnrWCS6qpZN7W+MaLcxp8zx8mWqLlqKFuIZYgd+jvhrJJ1WOjCU 9qffIRNVV06WdIoIzw3w++anZiMDHEKmOK8cVVUxgMTFDxnogQjoBXEE4XrWyxGQ0tBn Zw7Q== X-Forwarded-Encrypted: i=1; AHgh+RoNlwUYM3Uhzj20a/lhQWCyEt6o82Hz2crSJhinaOxZ+vGCACG2nAPB2vs0WsPP4ztWYQJBczL54L0j/1Y=@vger.kernel.org X-Gm-Message-State: AOJu0Yzzu5rX3264omVzZQhoKqRPxN/JD0ejZUCuefBsfcmVJ81gsICP 5cCj+8eqm+HIR3ld1VtVKeHWFCvdgC7Y04wW3vOtnc7G0XGPgmLMMBY6zAydCsp2uGE//SQNdXD SLYwJNQ== X-Received: from pjbsb12.prod.google.com ([2002:a17:90b:50cc:b0:392:b872:81b9]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:390e:b0:38e:9ef9:eb97 with SMTP id 98e67ed59e1d1-392823ea541mr21595524a91.16.1786402519296; Mon, 10 Aug 2026 15:55:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:51 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-14-seanjc@google.com> Subject: [PATCH v9 13/21] KVM: x86: Fix compute_guest_tsc() to handle negative time deltas From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse The compute_guest_tsc() function computes the guest TSC at a given kernel_ns timestamp. When the master clock reference point (master_kernel_ns) is earlier than vcpu->arch.this_tsc_nsec, the delta is negative. Since pvclock_scale_delta() takes a u64, the negative value wraps to a huge positive number, producing a wildly wrong result. Handle negative deltas explicitly by scaling the absolute value of the delta and applying it to this_tsc_write with the appropriate sign. This is believed to be unreachable in practice; no path has been identified which invokes compute_guest_tsc() with a timestamp from before the vCPU's TSC generation was established. Fix it for robustness, in the spirit of defence in depth. Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 50e92090ba65..76c16a15f059 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 BE462407CC6 for ; Mon, 10 Aug 2026 22:55:21 +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=1786402527; cv=none; b=jFLCOgdFXw8D9D/1NKx+KFDEbnJngXArXAznlAUS7yFfZfvBva16lSN++SVBenbq5xQuwhTMKqLwpHmsrJjcfy4lE+wteHvlzT5oDsZc8LJ/FQ+HeU+jCIjqAbDk7E4S5ZSvDxd16KtD7N/xqz++4c53pmXGcZhJWhXIwrtDM+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402527; c=relaxed/simple; bh=yKoRdd0ffDUIImrM0joXcBmfrgPbp8a27fsII5ey2UE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qocZesKrn39BxgEdeUrzqtjWMh3GWmzLNMcS6vcco1og+fehGNpH2fcQvVDYY8frLnM1VXmJXKuaWqp8LMHxrNKWWrdxg3FHexhgwvZbIp0Cv2yvPbn4BkmgWTOUIcett+kg3yY9TaGL8qUFBzfkNP5EYLm9v2dqYIWiORqIqWU= 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=NSpvhZBH; 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="NSpvhZBH" Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2cec4226c70so47890575ad.1 for ; Mon, 10 Aug 2026 15:55:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402521; x=1787007321; 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=BfExY4RN+PjphJCfLm9GzDgD+ifrz78cA738SdvwwXQ=; b=NSpvhZBHxonLBC2okr6pdj7/hZ/UdcEnDgua27usE396E7ev2nOp0KHGc+2BdxNnKD O7PaNaP+uipnXlxCSI1vC6pXRd7STE+Bi0dmn/NeYddhleTPRUYbMDA5dmXGBSuYAHWB /gqM2PjF0RqdieN4eAc/b5bzkVEsKJTFYsilRaICbxTjgWhUyFcvpT+4VI6Iq4Lov6/z eZBU9oUjKb0pc6yBUQHypV7fLXeB9ZrPMMKENOWEGvn5udbydnWpcRfi9egaWsjlabUi 53FvrP14gGmqAueWDiwg5ZBkZxacIYXoxiDBif9IoMRh7Bbxsm6IYg0fT/E4gp26Ws4N 6QGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402521; x=1787007321; 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=BfExY4RN+PjphJCfLm9GzDgD+ifrz78cA738SdvwwXQ=; b=BceZ1oUbYZOVz9qCM6gM4kyNirLUkKJzj91eWjWy1RQEhbPUa0Qc/hs0C/l/0G9D6c s8ply/FPBl2ZrQLCaPpOi7MKDiamsJ17KmJCzE9E+LKUIdgc3TT9PYHdC+eAc/12i7/7 Ik3bHhdm+aweM/Kz2F/eHbqT3b2dBJVzeQ1HOnsyk6l21v+51YfoLTJBaaiJcWtRAQVm RPDzzWA9+cRql6xkbUi6VF87dkZwP23Iyn4uCO1n3ySa3ZMzBCwo6s8Pw6rPJ+HKqR2G Mzb+TzMY/fFLpWD4JJfDNAd+c7V7vlns6TfFIIj29GvgZ0CcOPcFQc8RWqv+W0MzKc+i y+4g== X-Forwarded-Encrypted: i=1; AHgh+Rr9IX6R0it+h9GdWxyfLZzk2wqopQhvLEal3SjlHyvuh5+jw5JtISCCQ5oNFxTWWsPd5IVbmVGtPHqNpKU=@vger.kernel.org X-Gm-Message-State: AOJu0YzQOp8/PogPTlZF+FK9PRYXmxvLGg3JgBkVZESxKT2N4RfBOyow npEoGXEwckKC0229aWSn+j5alFk/LPll/QxPGeDmyEXyCH7rAC1MOUx74qXnJsDpoo8A6ftPRGA J8Fz50g== X-Received: from plko2.prod.google.com ([2002:a17:902:6b02:b0:2cf:6c53:48ca]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1746:b0:2cf:afe8:b722 with SMTP id d9443c01a7336-2d0ca761e0amr525739405ad.11.1786402520391; Mon, 10 Aug 2026 15:55:20 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:52 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-15-seanjc@google.com> Subject: [PATCH v9 14/21] KVM: x86: Disable preemption, not IRQs, when getting TSC+freq pair From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Disable "just" preemption, not IRQs, when reading the TSC+frequency pair to update guest time, as disabling IRQs to protect against task migration is overkill (though it's *extremely* hard to see that it's overkill). Disabling IRQs was added by commit 18068523d3a0 ("KVM: paravirtualized clocksource: host part") before there was any coordination with timekeeping (presumably disabling IRQs prevented the kernel from completing a software- induced frequency change). After the coordination and locking was added, commit c09664bb4418 ("KVM: x86: fix deadlock in clock-in-progress request handling") moved the locking and coordination out of IRQ protection, and thus made disabling IRQs pointless, except for protecting get_cpu_tsc_khz(). And while cpu_tsc_khz is written only from IRQ context, and the *extremely* confusing double IPIs sent by __kvmclock_cpufreq_notifier() to update the per-CPU frequency make it seem like they would require readers to disable IRQs, it is safe to read and consume cpu_tsc_khz (via get_cpu_tsc_khz()) with IRQs enabled. The per-CPU variable is specifically written only in IRQ context to ensure hotplugging a CPU wouldn't write cpu_tsc_khz with a stale value (because apparently disabling IRQs would be too simple?!?). As for the double IPIs in the frequency notifier, both IPIs are red herrings. The actual sequence that ensures KVM updates guest time with the new frequency is that the first write is completed *before* the notifier sets KVM_REQ_CLOCK_UPDATE for all vCPUs that last ran on the target pCPU. The first write is done via IPI to adhere to the above rules, and the second IPI is sent purely to kick any vCPU that happens to be running on the target CPU out of the guest. I.e. the second IPI writes cpu_tsc_khz out of pure KVM laziness: it saves having to define another IPI callback. In fact prior to commit 8cfdc0008542 ("KVM: x86: Make cpu_tsc_khz updates use local CPU"), KVM did indeed use an empty callback to ack the IPI. As for why it was deemed cleaner to abuse tsc_khz_changed()... Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 76c16a15f059..a8dde1d898fb 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 C423B3F58C1 for ; Mon, 10 Aug 2026 22:55:22 +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=1786402527; cv=none; b=h92JO1gGvqWg29wi0v+Iy9WqIi48POllO5xokVmvlHeRwbiSgfktNNOiPxw2jtwfOOwoTMTLEvqpta/qXoMoPDixEMxZ79jg7OYgCFwFNAgkJpI6GEaT1eEVDIRwyoIkcsr59BB/2cKUUaS6X9B0p4Bgw7d6e8gyPdwUu9/eCB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402527; c=relaxed/simple; bh=AnYa9DsB3pC5ovwAQxf38sSPtNt8qCo49QzZbAmqaJw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gn2PNpq2yMeaRBVDeTmXIgWkzsQqCndrWa7Fmj5oTtVHUT+4y4+s+3nNn87Xh1faPx2ZRCC9OHer7iC+doO1CpLuz5l0S/t3v89Sh1yFXIXHzbw2GekBUb5V2nUPrr6Vzx6ZV802ZF3HoBaQIU0iAtxH8la66/nNN+Cw4sxxxps= 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=oYqa5uQo; 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="oYqa5uQo" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-848568a6f62so4849612b3a.0 for ; Mon, 10 Aug 2026 15:55:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402522; x=1787007322; 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=lVlimL3Aj8kSsMDGDfvignTmc+0OEOZcp7iMQk31IH8=; b=oYqa5uQoDsY/Zs77sINbsZ+SUmOQ3OvFoQQ6u363qFkdmJHGep3VM84qIwUpOB+qCk VuxylniXkthXJT+U5SXZKNh9swTea9m/+Njf3QNvWax9bGHkngwT6sWabhthhbqfexXa q+7+bcpqKW0qIsA6+hf4F9GHKjHTOO6tnt2lLKERbM9zx0wGo2ufyEjDclyTzu7FJ/bJ r2+MB1kFpzAXab5Wl5RDotlGZDpJf0aphRIdHjZdKAzZh8nbkjg9vD8miPsjzEt55lje PotrATiVin8iAWKjpq+/7MzfSwOHfcDZUwuJgnBn7sqfnf84WRDv8BeaAkxAxE8qJIRG rmHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402522; x=1787007322; 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=lVlimL3Aj8kSsMDGDfvignTmc+0OEOZcp7iMQk31IH8=; b=IkM4Dw5tF+G8tIbMaK6rbqYguVuHfctqXmD8Ivf5HDnZJcGfP9luF82GO8nbMqIXrF mVFGJMD612u4QKQ397hqcoA7ttWBel20KQir+kCP/pvsm+/KRxgDk/oGR47HeUXMM1nF 5sVzBR4YMunnRI1I5ZBx5QVPCnG9CIJbyn1yoKIbcFNbZT/TI2n1/SHaNAgH7nmOA3d9 TspWBamG8CyDfA9tMUeXqNeTVfBnmmTLf6iu0R4u5JgLhWWs3U+W+zCjV56mCPr+VePK AH1mHdtO0eqnJRM3lyhYaesR4TdDwV+Flb/jWK5ae1bxDuCXUoSX0HCLUxbcH31Wvbx7 qUpw== X-Forwarded-Encrypted: i=1; AHgh+Rpf74yueMmoS8UdnOuNQmCG4ysoiriXEmlZcqBIC4/gEOhJ3R5RkhP8xERzflBe0TF+Rw1SpSRr3qKgrCE=@vger.kernel.org X-Gm-Message-State: AOJu0YxAYduCjJIWCpJnNZ2QS640uqvUVq0YEsAq+TwcZHEUGV5JoHwe sSraVg71zFEXCjrOAVPV1CMAowq+2hiWHsnrI6eNM1mzxbBkOqIx7c53NmZ9EU+WBxvfPSRxLf8 qRWDWvQ== X-Received: from pfuu5.prod.google.com ([2002:a05:6a00:6f85:b0:848:4e44:7f19]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e19:b0:845:4928:8655 with SMTP id d2e1a72fcca58-84f2e0edaddmr45671680b3a.39.1786402521754; Mon, 10 Aug 2026 15:55:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:53 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-16-seanjc@google.com> Subject: [PATCH v9 15/21] KVM: x86: Make master clock logic in guest PV clock updates 64-bit only From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Wrap kvm_guest_time_update()'s entire use_master_clock block in #ifdef CONFIG_X86_64 and provide a hardcoded-to-false variable for the 32-bit path, as use_master_clock is never true on 32-bit (host_tsc_clocksource is only set under CONFIG_X86_64) Keep the local "ka" variable outside of the 64-bit-only code as it's also used for Xen code, but tag it as potentially unused since it's not used on 32-bit kernels without Xen emulation. For all intents and purposes, no functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a8dde1d898fb..d3630b2bfd3b 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 BE59C42883D for ; Mon, 10 Aug 2026 22:55:23 +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=1786402527; cv=none; b=U1y4dZ+dRYoSyYfhIMfs0aUyx/vfZA9+iY/+BxZ7JulQjbOjrpRxzv2sX72+BwpDuzQ7LYrsn03EzwYKUWmlweN9t1bwsP4hJWiWqlzcyxzliO03R02FSHbIwqB65zKLm3bb/660RVVPMjz4o6NX1exd5VX3r9L1udtTNtQhwcg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402527; c=relaxed/simple; bh=qE7twB9LHqX5wJ4D249ZvlxpYPETv3+Tb+pc76veePc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G1WDK+ZcBVOT8OWeDlGpGrU6MjGwYaClv4cq+QLiQzkb9ZrVox5X9gP100QbP5Ly6wQyszVmQLPK11m6GLqdrul5ehXGMX/fXvSo9OMjNxvXshtfb7h8H5kaCf891AlCBDU8N7nSATAM52Vp3gJi1+Idofp/zIFRs933YfbBZgw= 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=lynp2I6Y; 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="lynp2I6Y" Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2cacf17c7e0so34527865ad.0 for ; Mon, 10 Aug 2026 15:55:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402523; x=1787007323; 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=PvqJXiept+sy/D9olFJ27hxypc4xjYNey0XP8SG3v0U=; b=lynp2I6Ykd/Dekh/VtgdZuDWbpKcmmlMPGt7hHHVI2h/Myo0gsobElPMMftlXyuNc2 gb2iGmk9iFX7oSxK5IG6woftKAJ8Rgm9nOjSvmvS4RfAlWWDq2L6xb3oQaPXxo9J6tYW Mhy6YDDdx0pBzJkmY8UTF3qOV9l3GqVfS+tn5nJRjhwHiX1lGpYDmm4cpRRyofg08iFm 83FHunQqylfJNr0sQaSyp3/+M26zWbk71HCGPNKUaMAKfYIOKOsYCR8wCaJiIJP2SQeT G6byMIGXJdxj2cSlk1GDsbb4z12BfmVbm2OD92E883Wvr/XVM4SlppUqc1nKzuUWK9Jd VONQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402523; x=1787007323; 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=PvqJXiept+sy/D9olFJ27hxypc4xjYNey0XP8SG3v0U=; b=T8UkoiqwuIj7bO9u4QVrJG1kzt36mO38fBh8NEQFHMDSY/CNBKkzqFfe6FudGUOH2V 58mMG1dJsADkb+y2E6qwUv0q46z6hvy3g07bkyR1GF6nU5NXm9qrxVCO5RbDTevYhAMf XH7g9LhFWIlq2H+pCZ+KNiNUYcoqh10Sr/BoZJ+NgFZNdBJQmV7Unr7XhV1VQDuYD5F3 4WluIGNuHd/KYRQWw2M+huKUVBN9NqU4r88Vb11pg7RsJb0pFlqR1w5+9acokRWwsA/C +uLF26/mKl/cZzsPw+arr6IUzvkeYTKSSlIFkXCwjhfMBCQSygIOJzBBFxqZclHI0Niz H9qA== X-Forwarded-Encrypted: i=1; AHgh+RpmjZZiLz1lKxnERfB27lrQpVlUy/Tq9oS5gZ++pQ7iAbTy0w18GUMFMByQ7YAmTbBo9DTHYvtjEkldLEM=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9Rpw+02pYFFxRMUgwdusCrpRLxV/CW0UPeptDGUVmfzn85LUy iPwLTYXM3wf9xjJp39sQW8w/0mKLODrnkz9F4oVTyubVxb66GPCwuwuT7ZirnmlnhZl5+IIHqeH fGCM6sw== X-Received: from plbkf6.prod.google.com ([2002:a17:903:5c6:b0:2ca:cba4:f740]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2392:b0:2ca:b8fd:f31 with SMTP id d9443c01a7336-2d106da9160mr413442285ad.15.1786402522932; Mon, 10 Aug 2026 15:55:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:54 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-17-seanjc@google.com> Subject: [PATCH v9 16/21] KVM: x86: Upscale TSC to "now", not master clock when updating PV clocks From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse When doing TSC upscaling in software, e.g. when the guest TSC frequency is configured to run faster than the host without hardware support, always advance guest TSC to "now", not to the master clock's reference values, which represents some time in the past. In master clock mode, there are two points of time which need to be taken into account. First there is the master clock reference point, stored in kvm->arch.master_kernel_ns (and associated host TSC ->master_cycle_now). Secondly, there is the time *now*, at the point kvm_update_guest_time() is being called. With software TSC upscaling, the guest TSC is getting further and further ahead of the host TSC as time elapses. So at time "now", the guest TSC should be further ahead of the host, than it was at master_kernel_ns. Not taking that into account means KVM was advancing the guest TSC only by the amount appropriate for the snapshot taken in the past, *not* to the current time (which is what RDTSC is supposed to return). Signed-off-by: David Woodhouse Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d3630b2bfd3b..56c2fe11d866 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 D69EB434404 for ; Mon, 10 Aug 2026 22:55:24 +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=1786402527; cv=none; b=AqyMnwspxKyTRIPsBLTU//IkW7uuZ14mP0wM4Gn0155SBDw8kGg68by1K0jHVpDHMpYqMu9ilqxMa29V7Jh7iqEGDqMONI2VzqujPlVpCsgsq3d0I5c3baqPjGqFZQU+MXlq2WDd+KpNxi0rZTomL/uMO/riNzFKj8rD5gycgkA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402527; c=relaxed/simple; bh=8stmInZqKeF+zWzG+qsqlrTvQz23kM/mBIRU+hqbvBo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rp5zJPtKz/SF7lab+f9aqmIxK/zicirn+9tv7y3bFiQ4RHxfFII3pxdfxEbZhjDo54dG5xrT9nIeW2E1pr7MEr3nvanneQOnlmR6ysyy+h/bvzDvCMGDu6DuVN6VXSLP6/6493qNok4M+M0t7yh2CcG5k+Zj7Ee0YHGxMQUADcM= 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=rBBn9xMs; 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="rBBn9xMs" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-848d21bbb55so4134697b3a.0 for ; Mon, 10 Aug 2026 15:55:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402524; x=1787007324; 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=ae4gt5HyKD0FAFiH5hYPOm4wz/iCyBWNSwEKCKCHkvQ=; b=rBBn9xMsptu5bnsVILWh8PdFKFbWpMzrsQjBsAkT+Zjd4xp7I5fYanL5bQV38MzK9U Wx43m7RJ6VruwBIJkIs9dgFmaf2I7RoPZeOBDDub/5z+TWSbfp0pee6hPElHsfbaePRT Q3wL3ZjNrpFVm1Fe+pyj0BUp4tquxn1viIQ4y8Qjdnou6L05PCSFkvK3TgjyjG6CSrOj mKiTQeIGS/O39Qunu+7G9OGOZ+m8JLkvNDEALsJHAIhnRQNFhe7HgQTKCiUottgyHDlP 5SSojFfi+R04T4CprTIb1iQoQY/qyEQkW9HY2UyiTpc4Z2E07jw7crZbp5fJhRI9uDhr xpQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402524; x=1787007324; 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=ae4gt5HyKD0FAFiH5hYPOm4wz/iCyBWNSwEKCKCHkvQ=; b=qc73+8t432fJGAibT8OuOg9gRbHoe5EuS/HeoedBedfjGFSJriI1VIdEVMA7Xy+PpD mzMtWy5ekdFhxD4QTOsHJoqfRiTo7wKSVUQ/cVIDdtQtEDHzqXgDFqqe6nNzgZNUtfXa IuK2dZmBCRpKVX/JDz4sOlAc2DFeixL5r67wt1OYvcaWUY2SqhPWKeUyLqeoRa3TVZd1 Ce1CiMBECCbAYmzTHC9UzcQdNXU8vngnlxGpTveA7r3gMXL3xbmc+8ZrTPYOmCJfluE9 oXw7IuUuxAonnSKBH80OrN2jEmiq2fAsHjXTF6MlGllACKLR6nOi3IsNpeAojgNJzjG3 7ErQ== X-Forwarded-Encrypted: i=1; AHgh+Rqt2qzz30xtc3HosimKWPbSnBt4QolntUDtEfJmgjKwmhTz3DUUUM7jpWSuo/g+Pp+0u5S7L0x8KZ/2ywU=@vger.kernel.org X-Gm-Message-State: AOJu0YxR4F5evJmFyOrXC8xvScoeyJketqPcAO4rlFgPaO00qB7OFebc 4+CYRa1riUinOfuMjkqajSyde7pBwNxBUQI7lgeVZ4m0SiLfO3+yfG1dn50/bF3fFS0OxKca5A5 yHdyLlg== X-Received: from pgbeu25.prod.google.com ([2002:a05:6a02:4799:b0:cb3:9a4d:501]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4287:b0:846:bc60:5bf7 with SMTP id d2e1a72fcca58-84f9c8d86d9mr6120918b3a.6.1786402524047; Mon, 10 Aug 2026 15:55:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:55 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-18-seanjc@google.com> Subject: [PATCH v9 17/21] KVM: x86: Simplify and comment kvm_get_time_scale() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse The kvm_get_time_scale() function was entirely opaque. Add comments explaining what it does: compute a fixed-point multiplier and shift for converting TSC ticks to nanoseconds via pvclock_scale_delta(). Rename the local variables from the cryptic tps64/tps32/scaled64 to base_hz_u64/base32/scaled_hz_u64 to make the code self-documenting. The "tps32" name stood for "Ticks Per Second" but was misleading since it held the shifted base frequency, not a tick count. No functional change. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 55 +++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 56c2fe11d866..7661c49ccaed 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 5CE7F4418EC for ; Mon, 10 Aug 2026 22:55:26 +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=1786402528; cv=none; b=TtEt0YM45EU0FdZs0gKwKHoEWHgixRC3WGQZ2wap+/vzDv+HhF89fkiTmHcFQYfk5VSLxuTNlOReoaWPXfZT2pjVfwBoYbt8VUsDhllNlVb7fdNN65zIrwH+pZojssbQlAIhDGPvYedpyjjlmwBDTvFgiJ0FTESqzvwQI0DSL5s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402528; c=relaxed/simple; bh=iFcglWk9F6qKya6tgx5SS1bWqEKMAce1EoiI4+P8W3g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T7FXQxX5P9+S2zx3uj0pKvFVYU6Dh5hYxPTgP5V9VILQKAwt2klyROzvjwXV+NDBkJXLjaC7ogmNbf5EszxF23hOOm/MdxQ8/Rxom7dCFpHBzCtfqUg82dMjS0KIT4UgTznP3grrsvRe9Xqa25HCnI9pUXX5ZFWo1+aISFSQOSw= 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=aso5LUE/; 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="aso5LUE/" Received: by mail-pl1-f197.google.com with SMTP id d9443c01a7336-2cc88e22f92so60269495ad.1 for ; Mon, 10 Aug 2026 15:55:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402525; x=1787007325; 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=oH/Luzlq4au8ak/t1uoql865pxDvRNySjr6GD/iWdqY=; b=aso5LUE/l9Gy0eisYF0N7MjM9i0udyFalYW4cORUmBXSE/YTgZjPfCjqu5iebOWNDh 7qf+76C3n9AkX7wXWIGdy3ZNdh8uajH++sAkwW2jW3gR8YnTZYDtzPxjSyu+pnOWLz3h rBuwW6WrhB250YSenmSnxUuCYWWvRHQukGXsCsddXtMLiLgktEr6x0Q3PUh9HTIQSUI2 r0rwMljd8O7jDcS9DajT+VtN0nw0XfM7SxT7mBHF/hvehgRykzyykhhM3qqCvRGzajdw CANfeygOqZNsGCTImueDX9MsoBPaBMVE/LG1sJHPPPl9JPHoq4d5harjvrhcLY2fW/Rb dgWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402525; x=1787007325; 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=oH/Luzlq4au8ak/t1uoql865pxDvRNySjr6GD/iWdqY=; b=DqC598jFYMFl7SOxY4BVdKybjdsgVyAHuHLhXrLBAsTnZRIUa5SIJN/agTaWz8AWAm gpmcsxQrGGyzToUh362s2HepduAYJpz+S0u0+RaWpAm/Awy3llDscAWc2lPkbnKTV1nZ ODT2uajJS9kiwGoy4Clz7uuVz2Iiw50OnIn35E8WeVFUnweUS5NLJC6DuQzCAryRFVHA SfWJwa61o4/pOIvv8teEtLt5nAZFHMWaHZUT+d3SJwGsJGEIncPtLCJ8cAjknObsHbiD ORTsUoICuEB4nsLStlCDlVDbn6c/p8U98HufCN0br1cnoQLmDcjUfGZZ6ihAJmg6B/v+ Mj2g== X-Forwarded-Encrypted: i=1; AHgh+Rp08tig7gXnnvRsbAsZni54yWkLdH6mOTNcK0rmOcN8VgbGBRp9JvkNdi28C7z/kvnyZ4T4pCxHG9gzZH0=@vger.kernel.org X-Gm-Message-State: AOJu0YwCp5Gsp9ia+GtXUIz4pGVKlM3G7hT5fDHPIoxdcEkNTgVRhaAp N+uFPmEjdrC+M3NmxcMudDcmi1Cw24qKg9Vw3933ehOzp0C1dhiYWcgjdfsnsxzCl3GNke9aUWf rAoY0Dg== X-Received: from plbiz4.prod.google.com ([2002:a17:902:ef84:b0:2ca:b2fd:5a4f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d546:b0:2ca:6c8:abd8 with SMTP id d9443c01a7336-2d300337a4dmr58949855ad.12.1786402525067; Mon, 10 Aug 2026 15:55:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:56 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-19-seanjc@google.com> Subject: [PATCH v9 18/21] KVM: x86: Remove implicit rdtsc() from kvm_compute_l1_tsc_offset() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Let the callers pass the host TSC value in as an explicit parameter. This leaves some fairly obviously stupid code, which is using this function to compare the guest TSC at some *other* time, with the newly-minted TSC value from rdtsc(). Unless it's being used to measure *elapsed* time, that isn't very sensible. In this case, "obviously stupid" is an improvement over being non-obviously so. No functional change intended. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant Signed-off-by: Sean Christopherson --- arch/x86/kvm/msrs.c | 3 ++- arch/x86/kvm/x86.c | 11 ++++++----- arch/x86/kvm/x86.h | 3 ++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c index 66fa7140d65d..22ceb39d41f6 100644 --- a/arch/x86/kvm/msrs.c +++ b/arch/x86/kvm/msrs.c @@ -1631,7 +1631,8 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct = msr_data *msr_info) if (msr_info->host_initiated) { kvm_synchronize_tsc(vcpu, &data); } else if (!vcpu->arch.guest_tsc_protected) { - u64 adj =3D kvm_compute_l1_tsc_offset(vcpu, data) - vcpu->arch.l1_tsc_o= ffset; + u64 adj =3D kvm_compute_l1_tsc_offset(vcpu, rdtsc(), data) - + vcpu->arch.l1_tsc_offset; adjust_tsc_offset_guest(vcpu, adj); vcpu->arch.ia32_tsc_adjust_msr +=3D adj; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7661c49ccaed..aa39a423694c 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.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 0A7F13B7B71 for ; Mon, 10 Aug 2026 22:55:26 +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=1786402528; cv=none; b=jB3jL3zULk+CGIsi6panor5K/5jzj0Im6q4VZyVwaXX7iHI9BB/Lp/RzbTTtdErANriBlrCU4opqMV60HsXRuj9C+sx8zLhLMijeHpk/hYv6ExIPJhy6QuPBvorw1w+1i1QrdPOss5kxEbo6TWB+gciVnzp5yt76Z7BfV8dUGRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402528; c=relaxed/simple; bh=UvWnP8rThQ0I2iu6au3IwSGq/W2BJVM9E1d48NUSteQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ax2GR9gSciP9FGmExd6qtulsNi9Rkb+Wpwvqwuaa6erybeeDKsqDjYOuD31+0qYqThzRxrtwfYT4jcHanoNEJWe2u3pnGasiXkUEsqZnnfthOC3gJeISjdMLuiawhNwbwvoOwjdi9Gm5kplLKWZt19FpdrnJBzyIkFiwgEQqbFI= 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=IaRYzKW3; 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="IaRYzKW3" Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2cf6acd760cso31909395ad.3 for ; Mon, 10 Aug 2026 15:55:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402526; x=1787007326; 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=57aBPOihCrGWuqYYWudkiYYcx3GP5FARz7fTh3Mm/5c=; b=IaRYzKW3AZ+vr7omLIP6hsuC5mZVFBNRwls4uZMkBXMPH2I0E0BtlPpu2y+/XWDqpB pmRFkCXZvWc0dQi/yuo8/RuCF+hoh3+kNPKSr3B28PrBFYBf/WF5H3w25yELTcgUjiF1 /bInh1egzb6quKATsYrBxeTitO4omykEV5dhvrf4eQRP/y+LblmO2ljbnxkZer1/4PJz 0tCqsZ8sQktLeBEG4ICqt+szqfJ0YGlWWvzv7v5I1SndQdPhAu8ypcpFP+hUkA+8Vm6f 55PGN78Ea46kZARhkeqvzqvj+UlPWNgjgGVK1Aje1eGfbGqIMcrOREsq5JF/TeJicbva hYIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402526; x=1787007326; 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=57aBPOihCrGWuqYYWudkiYYcx3GP5FARz7fTh3Mm/5c=; b=OvmeksqFT/BVsHc0KGUsAaWhuYZfHXZYjsKkX3ZRjadVR0v4/I6k6dRBhTVUUkBtl3 UUAPMjTIdN0nSQ7p/8b/3+Oc9iYz2ys7RX1GCaGp9dap30C/KuisD1IpcP6Ea0hx03TC sCSMyaBNzwgQBhs+1EisDj96aAZ0GDETa/0ESboFkLibP84IrWZAv9xEwNWUIR7LmNN2 Lwwn8Oqs8lVJbZjVASeZsGdxm+N2Z2UzX12GTeg1v0pT0+DagKqpulK/I0M00HBrkcko qdLfotfoXP9LgIGxVANjQvtvEHtosjnBEK7nFyCAGpt1Dp0nx6DE/h4UNcG4d647Nl25 pIjA== X-Forwarded-Encrypted: i=1; AHgh+Rq/XtyDR1328mL0+JzuyE41c+O02z74A+AZaWylfbwhpeweEvMeDnNtOh22CjxKsgJzDVWykz2VDTauYJg=@vger.kernel.org X-Gm-Message-State: AOJu0YwjVm4pfUyMTe8Qb27fgvoZqNX/i9tH4LvmHEoXUIANaYbdnR3P EG+OZd5h+urB7iVYgj4gXIvWu80ZJarG1eJxjJcXcrp1F7BxOtmMsBJ3D2sq2Nw2hBtt6j4ZfGt 6uiIU/w== X-Received: from plsk18.prod.google.com ([2002:a17:902:ba92:b0:2ca:cdc4:8da]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2bcc:b0:2c9:a5e9:c26e with SMTP id d9443c01a7336-2d3001e6ebcmr57931765ad.13.1786402526184; Mon, 10 Aug 2026 15:55:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:57 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-20-seanjc@google.com> Subject: [PATCH v9 19/21] KVM: x86: Use kernel timekeeping snapshots for getting kvmclock time since boot From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Replace the KVM-private vgettsc()+do_kvmclock_base() timekeeping reimplementation with calls to the recently crafted, generic ktime_get_snapshot_id() interface. This is the first step towards dropping KVM's homebrewed implementation entirely (do_monotonic() and do_realtime() will be converted in the near future). As with KVM's implementation, the snapshot provides both the system time and the raw_cycles (TSC), atomically paired using a sequence counter. The equivalents to vgettsc()'s TSC and HVCLOCK modes respectively are if the clocksource itself is TSC (cs_id =3D=3D CSID_X86_TSC) and if the underlying hardware clocksource is TSC (hw_csid =3D=3D CSID_X86_TSC). In the Hyper-V case, i.e. hw_csid =3D=3D CSID_X86_TSC, if the clocksource couldn't provide= a raw hardware counter value, treat the clock not being based on TSC, which which is equivalent to vgettsc() returning VDSO_CLOCKMODE_NONE. Unlike KVM's current implementation, don't include offs_boot in the atomically-acquired tuple as there's simply no need to do so: the time since boot only changes at boot (duh) and at suspend/resume boundaries. Unless processes aren't being frozen/thawed before/after suspend/resume, which would completely break suspend/resume, TK_OFFS_BOOT can't change while kvm_get_time_and_clockread() is running. And if KVM does somehow try to take a snapshot during suspend, timekeeping core will WARN and refuse to provide the snapshot. This is a step towards eliminating the pvclock_gtod_data private copy of timekeeping state and the associated notifier callback. Signed-off-by: David Woodhouse [sean: separate from other conversions, massage changelog accordingly] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 57 ++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index aa39a423694c..85c456dd29d5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -35,6 +35,7 @@ #include "smm.h" =20 #include +#include #include #include #include @@ -1435,29 +1436,6 @@ static inline u64 vgettsc(struct pvclock_clock *cloc= k, u64 *tsc_timestamp, return v * clock->mult; } =20 -/* - * As with get_kvmclock_base_ns(), this counts from boot time, at the - * frequency of CLOCK_MONOTONIC_RAW (hence adding gtos->offs_boot). - */ -static int do_kvmclock_base(s64 *t, u64 *tsc_timestamp) -{ - struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; - unsigned long seq; - int mode; - u64 ns; - - do { - seq =3D read_seqcount_begin(>od->seq); - ns =3D gtod->raw_clock.base_cycles; - ns +=3D vgettsc(>od->raw_clock, tsc_timestamp, &mode); - ns >>=3D gtod->raw_clock.shift; - ns +=3D ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); - } while (unlikely(read_seqcount_retry(>od->seq, seq))); - *t =3D ns; - - return mode; -} - /* * This calculates CLOCK_MONOTONIC at the time of the TSC snapshot, with * no boot time offset. @@ -1502,6 +1480,29 @@ static int do_realtime(struct timespec64 *ts, u64 *t= sc_timestamp) return mode; } =20 +static bool kvm_snapshot_has_tsc(struct system_time_snapshot *snap, + u64 *tsc_timestamp) +{ + /* + * ktime_get_snapshot_id() cannot fail for standard clock IDs + * (only for invalid/aux clocks or during suspend, with a WARN). + */ + if (!snap->valid) + return false; + + if (snap->cs_id =3D=3D CSID_X86_TSC) { + *tsc_timestamp =3D snap->cycles; + return true; + } + + if (snap->hw_csid =3D=3D CSID_X86_TSC && snap->hw_cycles) { + *tsc_timestamp =3D snap->hw_cycles; + return true; + } + + return false; +} + /* * Calculates the kvmclock_base_ns (CLOCK_MONOTONIC_RAW + boot time) and * reports the TSC value from which it do so. Returns true if host is @@ -1509,12 +1510,14 @@ static int do_realtime(struct timespec64 *ts, u64 *= tsc_timestamp) */ static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_MONOTONIC_RAW, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_kvmclock_base(kernel_ns, - tsc_timestamp)); + *kernel_ns =3D ktime_to_ns(ktime_mono_to_any(snap.systime, TK_OFFS_BOOT)); + return true; } =20 /* --=20 2.55.0.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 1E07744683C for ; Mon, 10 Aug 2026 22:55:27 +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=1786402529; cv=none; b=iKHizZNx7TbdZcG0DNjfOcNNsMd2tnqixq++xnVaEUJG1ZRQeiwf/H/AeMcPW3RGN8O7BH+dq7kFVY6trSykPFyS0ipZmVjowo+ctWfuD4wAof7y2j44HG4Cr3IQiGbdfKe9uBmZPpL7SVl4qmQlZaY2TElb1AeCKGcZkSgQpiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402529; c=relaxed/simple; bh=hhQjHicQbVby2oFYCswEc1FMH9bMTv/ejeWgfY93B5M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MC0n61MObZdPd15DwR8HGw96XGmN2Y+mR0HGhoNEge/xSOa+MLkILbC1yPbAX4Fyjsth4v4payQ6jTk6Dol0rBH8KJN+QKWE+EN6iVd1G6GxT4btNJk5RLYtPBCpAMVkUSRHbtxTqioxDLfZN769qx0+gnpSYuTyGvDKyoqVR50= 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=cJaow7m2; 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="cJaow7m2" Received: by mail-pf1-f200.google.com with SMTP id d2e1a72fcca58-8488ac68185so6525227b3a.2 for ; Mon, 10 Aug 2026 15:55:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402527; x=1787007327; 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=qiJ2rCuFTNV6LsYeeN4PZpKxEhzfSbT5iPntzURy2DA=; b=cJaow7m2WBrGLGPBTnS8FQz+/2Iao6vG6MbuBcHt1eBWeZIEWSvVJd4ceUTMUEyEkT PcxaKS1KVSI+BkG+wwfv7iBYUvTMAbf11FAWsUAkiPe5ZrDmz8lTDOWlO51+jSmO1sC+ vP/FWzR1mCJO1oOta1ko9+BLq08BbkvBxQikbIPZscm133pJmx/bvkCimGVx4xzEPj2m K47269iKPuksmJLv7gQq15lFB6kyIcwCIsJsIPYbqlot6EKfJ9f5fYGLrRQgpHinUJvv ZIvDEVPLoc+xP0QfSfquVEM5GwcPbQowMmmwBRu3DHn8/s3LnqTondVeIrsQaKq9CreG Xlyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402527; x=1787007327; 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=qiJ2rCuFTNV6LsYeeN4PZpKxEhzfSbT5iPntzURy2DA=; b=INfr8Oo0WVE59+hN9lM+CZtQrfVmtcoK8OPQHb/2cJvSEwrOVuM0ARtCfxfbk8SVnq dfXmTkyj/driElgkSdIy0GGdWTtfODBBBkcvLwgVOz0xwWFyS67YBXgmyX1n8xEsfFPx F5kvb6cA0C8l4DNgpWpM5aCwQ89vH4ZLbmaqiQ+gAr+aJUYbbl2lxUSJ0gfSfNpLWrZt 909SXMokO29NSQVINZMRHMu9NpkwEdovTQfwaVpVUROZF8IZGNNAtBUFtYX84uZuxwlj kGJHfb+d+rT868GGywfEtz91KR13QMh1wlK3K8wbDAu/xsvc1OlWixW9NgUW6xkapr2B FC6Q== X-Forwarded-Encrypted: i=1; AHgh+Rpep0WNyvDU3pPPCa1qKqTDP738CKPvvBI+iVBNwZPihI1fsX4lAfvhx8+F/EUFTD79P9pn0KqgqinIEF0=@vger.kernel.org X-Gm-Message-State: AOJu0YwmnMOXxVSNpp8YEsqgFORh84QPJTI8EnvP9RsnO4tYnadccZXI sUb7gKwZZC0Gwrzxk05eB1rUZycbA/OGFd/USf9F6duBN2YCHCnAz10Jyir1wvLSnKPP0a8Ra5L /q0x2uQ== X-Received: from pfoi24.prod.google.com ([2002:aa7:87d8:0:b0:847:98fd:5a98]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3486:b0:847:9aa8:d3bb with SMTP id d2e1a72fcca58-84f694910cfmr26202403b3a.12.1786402527233; Mon, 10 Aug 2026 15:55:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:58 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-21-seanjc@google.com> Subject: [PATCH v9 20/21] KVM: x86: Use kernel timekeeping snapshot for monotonic clock From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the KVM-private vgettsc()+/do_monotonic() timekeeping reimplementation with calls to the recently crafted, generic ktime_get_snapshot_id() interface. As noted previously, the snapshot provides both the system time and the raw_cycles (TSC), atomically paired using a sequence counter. Signed-off-by: David Woodhouse [sean: separate from other conversions] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 85c456dd29d5..1513e4ae2353 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1436,29 +1436,6 @@ static inline u64 vgettsc(struct pvclock_clock *cloc= k, u64 *tsc_timestamp, return v * clock->mult; } =20 -/* - * This calculates CLOCK_MONOTONIC at the time of the TSC snapshot, with - * no boot time offset. - */ -static int do_monotonic(s64 *t, u64 *tsc_timestamp) -{ - struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; - unsigned long seq; - int mode; - u64 ns; - - do { - seq =3D read_seqcount_begin(>od->seq); - ns =3D gtod->clock.base_cycles; - ns +=3D vgettsc(>od->clock, tsc_timestamp, &mode); - ns >>=3D gtod->clock.shift; - ns +=3D ktime_to_ns(gtod->clock.offset); - } while (unlikely(read_seqcount_retry(>od->seq, seq))); - *t =3D ns; - - return mode; -} - static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) { struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; @@ -1526,12 +1503,14 @@ static bool kvm_get_time_and_clockread(s64 *kernel_= ns, u64 *tsc_timestamp) */ bool kvm_get_monotonic_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_MONOTONIC, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_monotonic(kernel_ns, - tsc_timestamp)); + *kernel_ns =3D ktime_to_ns(snap.systime); + return true; } =20 /* --=20 2.55.0.679.g6767b8d81c-goog From nobody Tue Sep 29 07:39:15 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 6CCEF448BB6 for ; Mon, 10 Aug 2026 22:55:29 +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=1786402531; cv=none; b=Vczw1gV/1EKlfbjtGgp9u3/u/0rLma+rNb7myic1UdnhnlcIS9DPplnPEdgjsGUnlKtpvc6lIeNti0F7KUiUZIs4b4WB21HW4fk1IPYLtNunZ3bziPb3kVBurVySVort1OV1t9IDxgqyfZlPGniO/Uo3WB2Qg64I0qb7kfCqW6A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786402531; c=relaxed/simple; bh=ezLJCLMbVJeO1TB2+TOYF2iKtaGrO11RtViA7rAPS+0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=modTTM2BQBD7MGVpnnkWREzJYIevFyrLQy6IfFTm3AW7rz53LYoCP38CCy1BefsYXHpb7Mhim9Ma6rYABTKwbTWCc7oNHHcDCI4o9FuMtjpkFnZCmOEXcLaMpzfpzmi9nOIrqIfHiyK+lKXhgUbjdnijbbYxhK/WF6ADf5ij/WI= 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=K4p8qdpy; 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="K4p8qdpy" Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-84865f326efso3032303b3a.0 for ; Mon, 10 Aug 2026 15:55:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786402529; x=1787007329; 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=x+HTMJZ+MtQQgAVQxt+nrwT2irv/Xu9LPWilczsy7do=; b=K4p8qdpyCBfam/LuUqtgT50b+4rD41+yLmwkoZ3wP0bkEee99rzhYy1kV6iybuXAGo tr7gVtF9woD9SPM4Lucg3dWq1d63puaGwezATI/0fhW5zUZn3qKBLvWuEG8ON0CFVI3L X5AApxHwd0nEfqqNAXfI4B77XGzEBwk4JDZiF8eGcmOsid5IvJ7t2bSuD6W8NFIgOeRU nv+bUFxuQTDBbcRzp2od9/SLpJGTWrwCS9BrVWgo2/Oji6c34aUrHNDUjvl0X7cFWoOX 9j9XKsGvOwLSKJZYhFjazkNYveX5aVtRaFcsYtwDGW+8E4tto52+snDYtlri/2avJsTz kFmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786402529; x=1787007329; 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=x+HTMJZ+MtQQgAVQxt+nrwT2irv/Xu9LPWilczsy7do=; b=pFyNhJfv5gfR5q0tTrgiaQk10ixj7SvdNy1n0uYn11RjdHiiss3UjmN4mcIewmPfcN QMgt+2fUjvCwbjs3FkvQvfBy8HrRa3hdmDb6SMI4nbdnAgargvGKsUdU24640H8irQra GueGqjaUC205rZWqDi+RX7jwl0UqW3Lp37PdzWuywGLqKwPLwPbtPDcgDOQc1D7PaosH 4hkqlJ0MCiu0MrosZvvh2s5c2S6eXUFJKEnIcxI6tMR6cyjRyXcMjDc6J7/gInvsCfML xSWATZMLyOBspkcqDuvjNVJ3uVtecWGevpSpLdwUNgnGlK9NDOb98ImvjarNUkfwSDF7 ksWA== X-Forwarded-Encrypted: i=1; AHgh+RrdjqFgmp0ulnU2vaRjWv7+y2WsJO9PgcgBffQ9Mp2GnVsfhxUhskCMWhdAGpZLsHBtFUwbLzH0zIFmixQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyYNlWNjUWI6SkkpPyNBFz7Fjl3zC/OsDHodN9fs35YQQ8j6+FL AdIu7ISA9TleJiMtZFxV9SjngZq4Ei69KWm+006b4wSXV9eCZzB0TyJ6vE0wazOOcHLavrH1+qz 8k48xFw== X-Received: from pgv36.prod.google.com ([2002:a63:1564:0:b0:cbe:93b3:1154]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1396:b0:845:eb88:3d74 with SMTP id d2e1a72fcca58-84f9c9d8504mr4998640b3a.29.1786402528485; Mon, 10 Aug 2026 15:55:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 10 Aug 2026 15:54:59 -0700 In-Reply-To: <20260810225500.869288-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: <20260810225500.869288-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260810225500.869288-22-seanjc@google.com> Subject: [PATCH v9 21/21] KVM: x86: Use kernel timekeeping snapshot to get walltime+TSC From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paul Durrant , David Woodhouse , Dongli Zhang Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the KVM-private vgettsc()+/do_do_realtime() timekeeping reimplementation with calls to the recently crafted, generic ktime_get_snapshot_id() interface. As noted previously, the snapshot provides both the system time and the raw_cycles (TSC), atomically paired using a sequence counter. With great pleasure, delete the now unused read_tsc() and vgettsc() Signed-off-by: David Woodhouse [sean: separate from other conversions, express joy at vgettsc()'s demise] Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 85 ++++------------------------------------------ 1 file changed, 6 insertions(+), 79 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1513e4ae2353..a638898e528f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1381,82 +1381,6 @@ void kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 = *user_value) =20 #ifdef CONFIG_X86_64 =20 -static u64 read_tsc(void) -{ - u64 ret =3D (u64)rdtsc_ordered(); - u64 last =3D pvclock_gtod_data.clock.cycle_last; - - if (likely(ret >=3D last)) - return ret; - - /* - * GCC likes to generate cmov here, but this branch is extremely - * predictable (it's just a function of time and the likely is - * very likely) and there's a data dependence, so force GCC - * to generate a branch instead. I don't barrier() because - * we don't actually need a barrier, and if this function - * ever gets inlined it will generate worse code. - */ - asm volatile (""); - return last; -} - -static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, - int *mode) -{ - u64 tsc_pg_val; - long v; - - switch (clock->vclock_mode) { - case VDSO_CLOCKMODE_HVCLOCK: - if (hv_read_tsc_page_tsc(hv_get_tsc_page(), - tsc_timestamp, &tsc_pg_val)) { - /* TSC page valid */ - *mode =3D VDSO_CLOCKMODE_HVCLOCK; - v =3D (tsc_pg_val - clock->cycle_last) & - clock->mask; - } else { - /* TSC page invalid */ - *mode =3D VDSO_CLOCKMODE_NONE; - } - break; - case VDSO_CLOCKMODE_TSC: - *mode =3D VDSO_CLOCKMODE_TSC; - *tsc_timestamp =3D read_tsc(); - v =3D (*tsc_timestamp - clock->cycle_last) & - clock->mask; - break; - default: - *mode =3D VDSO_CLOCKMODE_NONE; - } - - if (*mode =3D=3D VDSO_CLOCKMODE_NONE) - *tsc_timestamp =3D v =3D 0; - - return v * clock->mult; -} - -static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) -{ - struct pvclock_gtod_data *gtod =3D &pvclock_gtod_data; - unsigned long seq; - int mode; - u64 ns; - - do { - seq =3D read_seqcount_begin(>od->seq); - ts->tv_sec =3D gtod->wall_time_sec; - ns =3D gtod->clock.base_cycles; - ns +=3D vgettsc(>od->clock, tsc_timestamp, &mode); - ns >>=3D gtod->clock.shift; - } while (unlikely(read_seqcount_retry(>od->seq, seq))); - - ts->tv_sec +=3D __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); - ts->tv_nsec =3D ns; - - return mode; -} - static bool kvm_snapshot_has_tsc(struct system_time_snapshot *snap, u64 *tsc_timestamp) { @@ -1523,11 +1447,14 @@ bool kvm_get_monotonic_and_clockread(s64 *kernel_ns= , u64 *tsc_timestamp) static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, u64 *tsc_timestamp) { - /* checked again under seqlock below */ - if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) + struct system_time_snapshot snap =3D {}; + + ktime_get_snapshot_id(CLOCK_REALTIME, &snap); + if (!kvm_snapshot_has_tsc(&snap, tsc_timestamp)) return false; =20 - return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); + *ts =3D ktime_to_timespec64(snap.systime); + return true; } #endif =20 --=20 2.55.0.679.g6767b8d81c-goog