From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4B873D88E2; Sat, 9 May 2026 22:49:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778366944; cv=none; b=RYFuDFn8sKGU5KcxwOkPhLBE43XF3cutfxmz9kcqMcRC4u56H2eela9YG+6H4Q7ozz0MfLmYqBOiq06M6TOmxKw/Lt5Ff7T7aPVl381KlBwsm9PgX4tRJUNfw/S+U0/V+FIMs/hqI6UG63ZIYc0IJzu78ZJsSTtBcSqOa16uq7A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778366944; c=relaxed/simple; bh=ebDXMZMA9BPNgZcpkRdbEogAF4fO5xu46rHxJQ/VXKA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aKkA7zrtAH9KqpjmtPEuE2I5DODqn8YK7tWhGg322XG9r7ZpNRGZEbps8GAwW3FaDg+MHQalfqCTrN7bHCg+qGmuB2W+sCB3GYSPXkY/npbvc+g+IW3Ci8eCV4IyT1rfaWCqS/TEa1qtXg77GMQMgRrsi2ad3s5n0Rq3+1gtKVM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=p9tq+aOM; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="p9tq+aOM" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To:From:Reply-To: Cc:Content-Type:Content-ID:Content-Description; bh=7Ju9ZkuVTj15MTYPuc5v1baLIQZcnrhBioVVlGYLmig=; b=p9tq+aOMSeacmQ+4Sy4WtR+Bw1 rYvCfaSU8+lYsJ1CJ4Wy3pzj2uKXGy0pM0Ahea/jCqrw+5As5J8XgCbgN62RH7j2INOMeBQZuAIWB Hf1L4TNwru7qFAI4f8D1WGgtpujT/IjjqAWnDWz1VUrvNaFptMW1b+O++qTbN/ppWqeM6TyUvW8YV mflTFSI3RImhAJIQtf+O/jPIOeMiS7dA5nYi8sUibf5VboP3isIb2W1I3Dg+YT/BxfLoQkCXVpUAH R9/k0agLsuB17tEyMDxJhZ4DYg0sSBuzd2sIN4BUNWtGOm1xtsfk4W0cUu9vX8LGFQUJf9OwLePSX sjG3n0SA==; Received: from [2001:8b0:10b:1::425] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.99.1 #2 (Red Hat Linux)) id 1wLqTE-00000008wzO-1SLK; Sat, 09 May 2026 22:48:29 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.2 #2 (Red Hat Linux)) id 1wLqTD-0000000DhJB-1rWh; Sat, 09 May 2026 23:48:27 +0100 From: David Woodhouse To: Paolo Bonzini , Jonathan Corbet , Shuah Khan , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Vitaly Kuznetsov , Juergen Gross , Boris Ostrovsky , David Woodhouse , Paul Durrant , Jonathan Cameron , Sascha Bischoff , Marc Zyngier , Joey Gouly , Jack Allister , Dongli Zhang , joe.jin@oracle.com, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, linux-kselftest@vger.kernel.org Subject: [PATCH v4 30/30] x86/xen: Obtain TSC frequency from CPUID if present Date: Sat, 9 May 2026 23:46:56 +0100 Message-ID: <20260509224824.3264567-31-dwmw2@infradead.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260509224824.3264567-1-dwmw2@infradead.org> References: <20260509224824.3264567-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html From: David Woodhouse The Xen CPUID leaf 3, sub-leaf 0, ECX provides the guest TSC frequency in kHz directly. Use it when available instead of reverse-calculating the frequency from the pvclock tsc_to_system_mul and tsc_shift values, which loses precision. This mirrors the equivalent change for KVM guests using the generic 0x40000010 timing leaf. Signed-off-by: David Woodhouse --- arch/x86/kvm/x86.c | 3 +-- arch/x86/xen/time.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index c15303963686..ac982652e5e0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3469,7 +3469,6 @@ static void kvm_setup_guest_pvclock(struct pvclock_vcpu_time_info *ref_hv_clock, int kvm_guest_time_update(struct kvm_vcpu *v) { struct pvclock_vcpu_time_info hv_clock = {}; - unsigned long flags; u64 tgt_tsc_hz; unsigned seq; struct kvm_vcpu_arch *vcpu = &v->arch; @@ -10162,7 +10161,7 @@ static void kvm_hyperv_tsc_notifier(void) kvm_caps.max_guest_tsc_khz = tsc_khz; list_for_each_entry(kvm, &vm_list, vm_list) { - __kvm_start_pvclock_update(kvm); + __kvm_start_pvclock_update(kvm, NULL); pvclock_update_vm_gtod_copy(kvm); kvm_end_pvclock_update(kvm); } diff --git a/arch/x86/xen/time.c b/arch/x86/xen/time.c index 6f9f665bb7ae..862b8e9e8405 100644 --- a/arch/x86/xen/time.c +++ b/arch/x86/xen/time.c @@ -41,8 +41,20 @@ static unsigned long xen_tsc_khz(void) { struct pvclock_vcpu_time_info *info = &HYPERVISOR_shared_info->vcpu_info[0].time; + u32 eax, ebx, ecx, edx; + u32 base = xen_cpuid_base(); setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ); + + /* + * If Xen provides the guest TSC frequency directly in CPUID + * (leaf 3, sub-leaf 0, ECX), use that instead of reverse- + * calculating from the pvclock mul/shift. + */ + cpuid_count(base + 3, 0, &eax, &ebx, &ecx, &edx); + if (ecx) + return ecx; + return pvclock_tsc_khz(info); } -- 2.51.0