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 B462541228E for ; Wed, 1 Jul 2026 19:32:56 +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=1782934379; cv=none; b=iG3IGuNHiRL298zAHG4LpibRnK9oS9aYWBGyWYmzeBCnyTXb/mgX0r+PoQhMUNlBllDC3WEcPo8/buxAxI9y3ryfxuwM7A5XObCYczES6Fw2BKfEAXxB+Yw2Nz2TRIHQYr3liugQw0eSLdJhS0nufItiScb6f0Le2/u21h8v8T4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782934379; c=relaxed/simple; bh=qoYpZpdKAvs3c+m3l1ZOE6Nj7iAKGdPQ69UNTM7lOtU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nFFawCeOpONPJQrI98j1uUSHPSnpxeDa2TanYwspeiPIWLfEf8HSryvQZjndiuw13A/Qf98sHUEygqhw3BS2VDWuam16xy0k5ZlFq8e3vEscqYkRQB8VVw/yULdl1L7PjWPGiG8dtBRg4cuOBqOXGWSRAYa8qbOnnlBucr6s8zQ= 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=G0DH/Aov; 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="G0DH/Aov" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c89704da8c7so1486181a12.0 for ; Wed, 01 Jul 2026 12:32:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782934376; x=1783539176; darn=lists.linux.dev; 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=3ZuhwNx9YXpitDLlgZzJCjWGiYgYUv+Ggq0FniuPOLw=; b=G0DH/AovTsT59cZeFzGxZsV/eoUGxt3aXunt+K6663riMQ2aUQUIaB3amP1BSsIJ4o gHXUuc23LA00ShfIyy/kfq1sK33OOLT/qcwvFLsNy18wjKRRdx3VjlrxS8aDZS04xjo/ m0zBbhFW6oqqckjPJnQ5Pg6Ro8VnwwFMAXdZTlz+H2isBwW1RmpzYH+IetttAqDZcu3A 2kE4hy1tafW+Xa0TF2dlj9lNrF211GGjv7H3jU0+ul10Xq0WN69txSZCVo7OAu9A+v+h XtLJwUqgkVJbC+irGvZ/dD3R45ifRCNjyaBUjTkOd+3mDOkmrzh0hcf0s/wUpe61YywA kEsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782934376; x=1783539176; 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=3ZuhwNx9YXpitDLlgZzJCjWGiYgYUv+Ggq0FniuPOLw=; b=sScG1YUFq/E5GdN1njB8+NJwE6AxdkWfwJ4hdmyaW1S4NTftFcMXiEovOxMkGju6w3 F0HZsP1ZX9o/Yns2kBVqtHOOEakgDHPY3Fw4XEI1waUsArrACxuXsl6jo6YtjNA0vcvy 2qp4dm5x/lJ4gtcUZ2b4Tj9gs+M3tUgv1eFxD8y/AleHJFYA8A5JeTjz7bU4fv2j15CX ESCQ0fMLEiUy9sQXJBY3yqlF4kwRltZ9yZz/JefT3wOmtDjShCi2bfU3O/9sP6AJCHPB FG+viilLaZ7gh1PnY83xhwqzrVMvo7Zw+Vc7CJ2UMSXlLnb4dvC37RyiGBmXCkywZf22 Mo3g== X-Forwarded-Encrypted: i=1; AFNElJ+cGDZ+HYbEWtSNgcpS61p5zMIVar6W23RezJS5EZpVq0Mvvmz/u4/IkTd5GkD/mrSzcXbpqOI+teAy@lists.linux.dev X-Gm-Message-State: AOJu0YwOn5T/fVQwovxgj8JTvTc1YPtiF3VgoDybjOfAKrL+OwSbNY2A YFREBG7MtCYxESoSGzY5u3w123lDnOXLZIGOabwProZX6ERsFsY+K7D/3yFnb2J4uY4ktQPfSZx k8buEkQ== X-Received: from pglx30.prod.google.com ([2002:a63:171e:0:b0:c9e:a22d:15c4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6e8f:b0:3bf:d0c8:2aa4 with SMTP id adf61e73a8af0-3bfed1c3990mr3189645637.8.1782934375853; Wed, 01 Jul 2026 12:32:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 1 Jul 2026 12:31:45 -0700 In-Reply-To: <20260701193212.749551-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260701193212.749551-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.rc0.799.gd6f94ed593-goog Message-ID: <20260701193212.749551-25-seanjc@google.com> Subject: [PATCH v5 24/51] x86/kvm: Get CPU base frequency from CPUID when it's available From: Sean Christopherson To: Jonathan Corbet , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Kiryl Shutsemau , Rick Edgecombe , 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: Shuah Khan , "H. Peter Anvin" , Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , linux-doc@vger.kernel.org, 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, Tom Lendacky , Nikunj A Dadhania , David Woodhouse , 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 1cef54e1e7d9..6c7011ff7bd1 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); @@ -1041,6 +1047,14 @@ static void __init kvm_init_platform(void) } } + /* + * 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 and in * deep C-states, treat the TSC reliable, as guaranteed by KVM. -- 2.55.0.rc0.799.gd6f94ed593-goog