From patchwork Thu Feb 27 02:18:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13993615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 13BF3C021B8 for ; Thu, 27 Feb 2025 02:26:14 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.897516.1306287 (Exim 4.92) (envelope-from ) id 1tnTbC-0002IE-Gs; Thu, 27 Feb 2025 02:26:06 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 897516.1306287; Thu, 27 Feb 2025 02:26:06 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tnTbC-0002Gt-6b; Thu, 27 Feb 2025 02:26:06 +0000 Received: by outflank-mailman (input) for mailman id 897516; Thu, 27 Feb 2025 02:26:05 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1tnTVN-00063X-6C for xen-devel@lists.xenproject.org; Thu, 27 Feb 2025 02:20:05 +0000 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [2607:f8b0:4864:20::1049]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 59fa727e-f4b1-11ef-9898-31a8f345e629; Thu, 27 Feb 2025 03:20:03 +0100 (CET) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2fc45101191so1080596a91.1 for ; Wed, 26 Feb 2025 18:20:03 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 59fa727e-f4b1-11ef-9898-31a8f345e629 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740622802; x=1741227602; darn=lists.xenproject.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=evC/AJYAKH4bntjSNStZX5d0W0KQw/JlJZazMUftcQU=; b=uBuIornD6TSt8bcrWwRHfRoXTQplgjGgDYnnfh/DBHr0KMLKThKoE+UrGe9l4GC4eI gvYCQ8oezfU5TE/PTmk9d9th8oWTFDPQFeIDdldcJVCJovXK+hoa729mQwR5Q5M+ALW1 Jqw/yVzSRMkkJbPg/LJZPXwKJsdZoELc/YD38s16JTBzQuEAhDXEE1vaZ0e2Q3fTUDys EevG7oDVCB//+/RfLtRR914M7u4CkW9F6eEGeHUP/HkVyZxSGWgnZpyPx4HCNjZFwuXt dKM5MdD76LfE1EkuojbUx2YncnJcqYi1NufZxGBfiPFjxAQ9IxS1+XArMQfiYV0j8sd/ AEeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740622802; x=1741227602; 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=evC/AJYAKH4bntjSNStZX5d0W0KQw/JlJZazMUftcQU=; b=lbuuvmpfuQWNadWzgz2L5NFolNnwB6YefvAmyy2nU8qm8J3CTDCbE/mtfzNziCVyWl gBnVJRKqQybaQYWaYmxj+obPNqYICrkH7kh4tGIPZ3MDknz9JpFNhr4VwDfQXCLa5Rg4 iFn4yQLEYD4cpk6oPpLhXE6gwlAq6iIUblfPw3jDPL0eSrBYBozTYu3A8EO1Ek6+qEwg ftpXe6p4MvCwqK2c0z0B0viPP6BjvB+g7XusBugFSoEQ2HmG3M2Szi9nbxfjN05eakue ATI6ULSoIP9kDq8Oot2SikV09V1lf5PsoRrnAaRWC6okh1iKMeDvAZD8wEkk3Pt4Pkcb emhw== X-Forwarded-Encrypted: i=1; AJvYcCVSKLxdsJjbGn/dwAJDa8qrAO8O1HbgFX+EBa/DM9mTF+OKUbL131qwR5/VDwalgbQ2wr7cg+BCrvc=@lists.xenproject.org X-Gm-Message-State: AOJu0YwpbTOLLxe5OrnYYvyPkALP2tTv9xkiy8zJNbLrRsf9EwGi7lWV JfqQW4T4Og3e6GtOh8+Y/ke44/PphXNK5OtPbCkAoPFXD8IO6/o7p8jT41yrMZwyRd0c1+3+gTb D2A== X-Google-Smtp-Source: AGHT+IGv/zpuplWrcklhWH3aZ9H+bWJRwaJtozM+byhaXyymURPHFd4oTcq5U1mZsa54ViZesa4/C8fD/pk= X-Received: from pjvb12.prod.google.com ([2002:a17:90a:d88c:b0:2ea:aa56:49c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5101:b0:2fe:955d:cdb1 with SMTP id 98e67ed59e1d1-2fe955dd224mr3596029a91.23.1740622802051; Wed, 26 Feb 2025 18:20:02 -0800 (PST) Reply-To: Sean Christopherson Date: Wed, 26 Feb 2025 18:18:50 -0800 In-Reply-To: <20250227021855.3257188-1-seanjc@google.com> Mime-Version: 1.0 References: <20250227021855.3257188-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250227021855.3257188-35-seanjc@google.com> Subject: [PATCH v2 34/38] x86/kvmclock: Get CPU base frequency from CPUID when it's available From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "Kirill A. Shutemov" , Paolo Bonzini , Sean Christopherson , Juergen Gross , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Ajay Kaher , Jan Kiszka , Andy Lutomirski , Peter Zijlstra , Daniel Lezcano , John Stultz Cc: linux-kernel@vger.kernel.org, linux-coco@lists.linux.dev, kvm@vger.kernel.org, virtualization@lists.linux.dev, linux-hyperv@vger.kernel.org, xen-devel@lists.xenproject.org, Tom Lendacky , Nikunj A Dadhania 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/kvmclock.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/kvmclock.c b/arch/x86/kernel/kvmclock.c index b924b19e8f0f..c45b321533e5 100644 --- a/arch/x86/kernel/kvmclock.c +++ b/arch/x86/kernel/kvmclock.c @@ -188,6 +188,20 @@ void kvmclock_cpu_action(enum kvm_guest_cpu_action action) } } +static unsigned long kvm_get_cpu_khz(void) +{ + unsigned int cpu_khz; + + /* + * Prefer CPUID over kvmclock when possible, as the base CPU frequency + * isn't necessarily the same as the kvmlock "TSC" frequency. + */ + if (!cpuid_get_cpu_freq(&cpu_khz)) + return cpu_khz; + + return pvclock_tsc_khz(this_cpu_pvti()); +} + /* * If we don't do that, there is the possibility that the guest * will calibrate under heavy load - thus, getting a lower lpj - @@ -418,7 +432,7 @@ void __init kvmclock_init(void) kvm_sched_clock_init(stable); - tsc_register_calibration_routines(kvm_get_tsc_khz, kvm_get_tsc_khz, + tsc_register_calibration_routines(kvm_get_tsc_khz, kvm_get_cpu_khz, tsc_properties); x86_platform.get_wallclock = kvm_get_wallclock;