From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 C47063FE37A for ; Fri, 29 May 2026 14:45:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780065919; cv=none; b=S7MuZmg0FaKQloQT0P4Gc7AvUxA8RZVfII1sTQvz1Mn5uY5PNOEff58h4NPoHHFIQArIsuytcadd7IgBmFZaeqsAEuVidRNuNVHSoUSb+NY1po4W6pz2jXZrFjehhYkMe1IV2VIqo4TU6kdn7szLtxpQuu1UOFpjguPrs1Yr8BA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780065919; c=relaxed/simple; bh=CoPJIZSI46YKZQvif20N8ynjdQo2BPPlyBaBoo8Emxw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hpsQIX3yN1ZjIqrBQJSgAe/ffwiLkGwgYKzfxvdfEtmmWv2xn/n8IARzszg1p2MiSfW98pFh0WUeIy5PfgR0Y264TazED0PyghzMf+v1PStizD/YZJiTyInAYHtiS0D0sZUhqgjSI7bRJ73y+tMuQTC9+C1kvh222GbJXtcUXek= 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=XiIBNBOX; arc=none smtp.client-ip=209.85.216.73 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="XiIBNBOX" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-36b982ec27aso1382136a91.1 for ; Fri, 29 May 2026 07:45:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780065916; x=1780670716; darn=vger.kernel.org; h=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=uWYqy9oq3guwZeX1CT7rpLN1sV47Joj/CY0UrflMOfk=; b=XiIBNBOXOefMQ5nZn9iDj18xQBRQQXjW140UyNW1pHArVir09dE9ukJxg6UKMEVNpC lxlKRUAfNMHt5aStt7G+r304cHEkiMfeoki4q6TlZjkWhWFLqeXFDyKLwPoFnB92+Pgx bq79ZVy7auUlq8PnLfk+qy7Rg5Vs7DkRjUqDtWmBQje4xz8Z4/YdV2rIP509tUF7ajHO WAkT5hNbjazrkeYALqqUccSgxK0+SwMFQbKasK0EdnlGr1pTCYpD9yuoZhgqrFhCRlT5 UzIIhmcXDEbY7sBxULSquKeXJtlVNXwrOd4UzqShEDhaO0M4+I1/d0ONP7q42kWmOOTx YXEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780065916; x=1780670716; h=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=uWYqy9oq3guwZeX1CT7rpLN1sV47Joj/CY0UrflMOfk=; b=kbPcFOdXrbcPqphXjnI1g4e6OqANsJQtkdCYZYHYLUOktUIC00jWqYebJl5poyChS4 EswkVHEG80Lq/D9ewmJpoeTZDv02rIR+qbZkjbr9fzbGlOw+VlleadEqXj3dgzDQg3u7 WCn+AyLXsw4G1/5z2YE4tGEI/tosH61bPb0dqgIOpKfEJZY5AULsmvH1QXmH4Av1HK8N IH8ENAQOW26p2T+fYBAFd4rO2uW382LgSNf7hIqOmxjK8a3q47cf9kaz3bHcGmUEtFW0 I0F1R/Ku0pPCsYLZgWE33NU+IVzmdxLPGIkZ7VDtIOe4+mTGNYww+Oj5NV1dvvxcGdof MUgw== X-Forwarded-Encrypted: i=1; AFNElJ+00qMdQyLIJZljygwMPkx58lWyoAZh18YaGYJQZxCVMftZ6EuJsD5SchvJTcVNp2cbVpBj78Kwm3QDnzc=@vger.kernel.org X-Gm-Message-State: AOJu0YxdIZoRZo/KBTVFtJrPjMPn0IynzrA33DPO+LKMx5X2uYn+juwS /ZibcYvZj45ChX0ArCaW99kpmjeWOhgsMoiNArMLfXwP45GOl9iBL9vVVMVAwOnIRiGvkayUCbL dLySCiA== X-Received: from pgce4.prod.google.com ([2002:a05:6a02:1c4:b0:c82:7a7f:9bf6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:4d02:b0:39f:aa3:5e56 with SMTP id adf61e73a8af0-3b412058e7dmr3539717637.14.1780065915613; Fri, 29 May 2026 07:45:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 07:44:07 -0700 In-Reply-To: <20260529144435.704127-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: <20260529144435.704127-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529144435.704127-21-seanjc@google.com> Subject: [PATCH v4 20/47] x86/kvm: Get CPU base frequency from CPUID when it's available From: Sean Christopherson To: Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Kiryl Shutsemau , Sean Christopherson , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Long Li , Ajay Kaher , Alexey Makhalov , Jan Kiszka , Andy Lutomirski , Peter Zijlstra , Juergen Gross , Daniel Lezcano , John Stultz Cc: "H. Peter Anvin" , Rick Edgecombe , Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, linux-hyperv@vger.kernel.org, virtualization@lists.linux.dev, xen-devel@lists.xenproject.org, David Woodhouse , Tom Lendacky , Nikunj A Dadhania , David Woodhouse , Michael Kelley , Thomas Gleixner Content-Type: text/plain; charset="UTF-8" If CPUID.0x16 is present and valid, use the CPU frequency provided by CPUID instead of assuming that the virtual CPU runs at the same frequency as TSC and/or kvmclock. Back before constant TSCs were a thing, treating the TSC and CPU frequencies as one and the same was somewhat reasonable, but now it's nonsensical, especially if the hypervisor explicitly enumerates the CPU frequency. Signed-off-by: Sean Christopherson --- arch/x86/kernel/kvm.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index c1139182121d..c81a24d0efdf 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -50,6 +50,7 @@ #include static unsigned int kvm_tsc_khz_cpuid __initdata; +static unsigned int kvm_cpu_khz_cpuid __initdata; DEFINE_STATIC_KEY_FALSE_RO(kvm_async_pf_enabled); @@ -928,6 +929,11 @@ static unsigned int __init kvm_get_tsc_khz(void) return kvm_tsc_khz_cpuid; } +static unsigned int __init kvm_get_cpu_khz(void) +{ + return kvm_cpu_khz_cpuid; +} + unsigned int kvm_arch_para_features(void) { return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES); @@ -1049,6 +1055,14 @@ static void __init kvm_init_platform(void) #endif } + /* + * Prefer CPUID.0x16 over KVM's PV CPUID when possible, as the base CPU + * frequency isn't necessarily the same as the TSC frequency. + */ + kvm_cpu_khz_cpuid = __cpu_khz_from_cpuid(); + if (kvm_cpu_khz_cpuid) + x86_init.hyper.get_cpu_khz = kvm_get_cpu_khz; + /* * If the TSC counts at a constant frequency across P/T states, counts * in deep C-states, and the TSC hasn't been marked unstable, treat the -- 2.54.0.823.g6e5bcc1fc9-goog