From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 5FDA240EBC0 for ; Fri, 15 May 2026 19:21:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778872877; cv=none; b=DuDJuSYnWsL67WUruZAfOY2G/bZvdGntHgFZ1OkUhU5C+G8r0+H4Kzftds7eLSSYSAZDd1NCo+1wnr7oalus1YBywqePlwiAqGMh7Lesoz4JkUDELdeG0J6ePD4wbT+g0b5a8yrtwqzz61+wg5SnA+11T2E4fmzcbneRNVcOlmM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778872877; c=relaxed/simple; bh=GcjjOomxWCHwXqJ+DzUBCv/BxVAVHsSL2Hl0ee8rM2Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gbGKaUQCL25+3IzFotPOcqBDUPMl7LpBpNi0cRFV4+GR6UcurNTc+0smty7WV80JU/idJgsnfiDqF721mMcUKz6N9Ore6ys7jVCebeDu2u8lHlJ26Ciy6SlIXTUNH2qVwNxV61pkYYZdZCrHNQUGf79+tqgoNV32iELemFce1ow= 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=G8WG7OPx; arc=none smtp.client-ip=209.85.215.201 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="G8WG7OPx" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c70ea91bfe1so164939a12.1 for ; Fri, 15 May 2026 12:21:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778872875; x=1779477675; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:from:to:cc:subject:date :message-id:reply-to; bh=Zm1yasCgCkYBCfOrXX2peIQ68HcxfozY1zg+bDbrsDM=; b=G8WG7OPxz2P88eVCqylK6cyHYDSNISdnoVps7PmNyNP5fpbEdNSBKDx1uBZCkBRYvl EX+ZljKxgUfzs3PdYf9iLt6GJBPq99jv8TR8z9qGxNDeSq9HSZRP45t9W+QD7yyoS7W5 R+eo8BOsdhAovRYTGZV1vlMLWj/YQMVRtIJakwV10CLPhX3Q0HO+6nLj9Uwx+IUqSCp0 3sJK8llcl+JXSO3FKEVUnc1pfICLEHXZSB5MRbchk4N+Ii6ThOK3BX/LguxGbKwM7o9V HxMX2xalB8mJINU4y2idZFaIFPXdkp7fHfzLJd8+oqw36yVPmaJkBm4kVcbjxF993JQk Ao2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778872875; x=1779477675; h=content-transfer-encoding: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; bh=Zm1yasCgCkYBCfOrXX2peIQ68HcxfozY1zg+bDbrsDM=; b=SnLpx6B3hVPd+VDn/bTsMKWrvBD8GG5Q5pQ2gvsiQqKo75nEBOL35r9v0Sc4dvBS57 CsoLpyXJRYv4NhyyiirFZ9+zccj94rw9vBbZqFK62Je9oywXdc3n6pUvbFd5HGqpC246 6uATT9f1IF5DJf/N5V/Hey/jBkAP5ZEWcBLBqsEFtEg7akXShqf//sxWMCvrHOvxsUuu E3UnlVSQTrEQDH4Fd+4UXcBaAxyLH6u4wvhvDIvS+Uf4W0RggTeyqcHH/E9ydji4r92P s4v2oeNVmhAkVCaDbt2zMk+9LMopTHJWANT/nGHEEimvOPaBaBMPFvUHtgpBD1RUlty3 qeKg== X-Forwarded-Encrypted: i=1; AFNElJ/D72CnKw71ahljMfcsw9MWYrIOTsPeSXy5G5A/Gk3peLvZZdWRlwJQGuq6iOfmrhFIxbo=@vger.kernel.org X-Gm-Message-State: AOJu0YwbgRIHYxUmsUYpcZzQ2/r2AjDO8p4rS1/P7GRtLOKHSmu8Eg4Y 25hjR/HxtbELT6PFbBNu1E3JwVUQa2zY1Pxnf6bi5m5J61T+B4izfZkAAyplgs66KefkXGCjFav t1ucaaw== X-Received: from pfvo27.prod.google.com ([2002:a05:6a00:1b5b:b0:837:e90f:8cd9]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:338b:b0:838:9e25:f128 with SMTP id d2e1a72fcca58-83f33cf0bf3mr5811988b3a.26.1778872874478; Fri, 15 May 2026 12:21:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 15 May 2026 12:19:36 -0700 In-Reply-To: <20260515191942.1892718-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260515191942.1892718-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260515191942.1892718-36-seanjc@google.com> Subject: [PATCH v3 35/41] x86/kvmclock: Obtain TSC frequency from CPUID if present From: Sean Christopherson To: Kiryl Shutsemau , Paolo Bonzini , Sean Christopherson , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , Ajay Kaher , Alexey Makhalov , Jan Kiszka , Dave Hansen , Andy Lutomirski , Peter Zijlstra , Juergen Gross , Daniel Lezcano , Thomas Gleixner , John Stultz Cc: Rick Edgecombe , Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , x86@kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, xen-devel@lists.xenproject.org, Michael Kelley , Tom Lendacky , Nikunj A Dadhania , Thomas Gleixner , David Woodhouse Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: David Woodhouse In https://lkml.org/lkml/2008/10/1/246 a proposal was made for generic CPUID conventions across hypervisors. It was mostly shot down in flames, but the leaf at 0x40000010 containing timing information didn't die. It's used by XNU and FreeBSD guests under all hypervisors=C2=B9=C2=B2 to de= termine the TSC frequency, and also exposed by the EC2 Nitro hypervisor (as well as, presumably, VMware). FreeBSD's Bhyve is probably just about to start exposing it too. Use it under KVM to obtain the TSC frequency more accurately, instead of reverse-calculating the frequency from the mul/shift values in the KVM clock. Before: [ 0.000020] tsc: Detected 2900.014 MHz processor After: [ 0.000020] tsc: Detected 2900.015 MHz processor $ cpuid -1 -l 0x40000010 CPU: hypervisor generic timing information (0x40000010): TSC frequency (Hz) =3D 2900015 bus frequency (Hz) =3D 1000000 =C2=B9 https://github.com/apple/darwin-xnu/blob/main/osfmk/i386/cpuid.c =C2=B2 https://github.com/freebsd/freebsd-src/commit/4a432614f68 Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_para.h | 1 + arch/x86/kernel/kvm.c | 10 ++++++++++ arch/x86/kernel/kvmclock.c | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_par= a.h index 17053d2bf270..3f7f558b5b24 100644 --- a/arch/x86/include/asm/kvm_para.h +++ b/arch/x86/include/asm/kvm_para.h @@ -129,6 +129,7 @@ enum kvm_guest_cpu_action { void kvmclock_init(void); void kvmclock_cpu_action(enum kvm_guest_cpu_action action); bool kvm_para_available(void); +unsigned int kvm_para_tsc_khz(void); unsigned int kvm_arch_para_features(void); unsigned int kvm_arch_para_hints(void); void kvm_async_pf_task_wait_schedule(u32 token); diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 65c787b1ea03..5cd92a0b156a 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -918,6 +918,16 @@ bool kvm_para_available(void) } EXPORT_SYMBOL_GPL(kvm_para_available); =20 +unsigned int kvm_para_tsc_khz(void) +{ + u32 base =3D kvm_cpuid_base(); + + if (cpuid_eax(base) >=3D (base | KVM_CPUID_TIMING_INFO)) + return cpuid_eax(base | KVM_CPUID_TIMING_INFO); + + return 0; +} + unsigned int kvm_arch_para_features(void) { return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES); diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index 47f7df1e81a0..5ceba4f3836c 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -200,7 +200,11 @@ void kvmclock_cpu_action(enum kvm_guest_cpu_action act= ion) */ static unsigned long kvm_get_tsc_khz(void) { - return pvclock_tsc_khz(this_cpu_pvti()); + /* + * If KVM advertises the frequency directly in CPUID, use that + * instead of reverse-calculating it from the KVM clock data. + */ + return kvm_para_tsc_khz() ? : pvclock_tsc_khz(this_cpu_pvti()); } =20 static void __init kvm_get_preset_lpj(void) --=20 2.54.0.563.g4f69b47b94-goog