From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 DD6A440EBD2 for ; Fri, 15 May 2026 19:21:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778872878; cv=none; b=qCqKrDwNp+TOTaydghv/0sn5MfAxg/LDnJfibMwIWcNK18tOny4XjMxldlr3hCWtoNh+Vwe7aa6ygslsYXf8YNLqV7E87dOkde2zYZey3nbQBUWy/DnXexWB6I0bIzwbMQziJ2zPQO6d1DETX0e+ygv3nnUI1/mFGD/NPXCJZuQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778872878; c=relaxed/simple; bh=GcjjOomxWCHwXqJ+DzUBCv/BxVAVHsSL2Hl0ee8rM2Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XXRHVqKeuYOQSJZxToZ+23g9drbcKM2gNhDPaHm8/JDbiUeHTHE2Cgq9+X9LFlN8axkrQZxkuXIw8Bt/ualwteo3XCe4IaXLRgS6x5bp6ZwRlzKW/QpZIxGKPOFJy+6jdLxVSsYrGOB1RCrwX0idpFV1667gK9UnTlToFhhAkSc= 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.216.74 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-pj1-f74.google.com with SMTP id 98e67ed59e1d1-36629e48023so84742a91.2 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=VSeNVz1LngZIEdHpR1jZ55+ya+3IVXwneYGdZY8V8nwBjkhp49AvjEcT5PjQRu6t5G Bk5Mp3XKTG9baLCb2zyxqxlwu8lCOvTO0hcg7veVfvmn9dPwqFhZwbBdRLHjGvXSyvyU LHammf/jPa6fOX5q+fb1rUSy9PFxt4dBjvxwatZrzE37F1nvpinO0NIeq5fAus7k5PfP kqIJ2l4PebpVL4UJSxFmj2jtGmt3gRwlpdPnqG8pkrO84xq+Pd+iUt+WCIyR7pQcaLBv aB+W0IQsRV9KSAz1DSlvEOkF6/hO2osXMmKrWdQEPVaDMp/5cO8iH1qeOKMrnbIjL9cu 6yOg== X-Forwarded-Encrypted: i=1; AFNElJ+OT0VoqCcQf7JwwuEE9cVFOK7w3A8oEHXFCV6Ngvdg6iAi58GSSKWmCkuCzmRkZ59lx5+kUBngwCqZSzo=@vger.kernel.org X-Gm-Message-State: AOJu0Yy0xSzKE5wf+nsRgbP8fAjHFO73thmirkrSXbNYMV0B2cofC6bY mywepT6AkasQsTG1pWevsc1DS8B0o7z/Ns56NWEtF73a38XBu/2wt9po+uRp3osUP1xtlChoC9b itBn/jw== 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: linux-kernel@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