From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) (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 75F634E379C for ; Thu, 6 Aug 2026 23:36:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786059400; cv=none; b=eafnwALa3df7glapbz+yyXo41FZNTzuvb7MmI2GDxsFIgaqktRD+KSjZyA/Q4Koh3lvR3XhdwYeg5Bguf2YR/L2Qwau+huT5aQizTvUooKhD+g0v1yAiX27suov238qy0RY693+JPFljkpuwU9IzQpyIn3ZvIPUE3dxOH0uPPVw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786059400; c=relaxed/simple; bh=jnf4MoiYxbuRu8FiLKuFAOuHO/WPwaUORBBRFhQd2s8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UwfAAPexf5ljCn37KpC/vb6jh5BpuSt2yhN9/rUWgrG99fi/8zx/AuJcm7GH55eZwZVsZfvQwBZDUTlDTeN0jEb2+uP3rO5jycgxHO64H2YHDUdptblQAoTwozsEOaF1NeTdGKTn6Mpks79ZuS+hnQAB4fqaldM2Flh9kGfkNa8= 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=pXcm1quY; arc=none smtp.client-ip=209.85.210.197 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="pXcm1quY" Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-84857446424so4590875b3a.1 for ; Thu, 06 Aug 2026 16:36:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786059396; x=1786664196; darn=vger.kernel.org; h=content-transfer-encoding:content-type:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:reply-to:from :to:cc:subject:date:message-id:reply-to:content-type; bh=QkWxTw8zJYn5bKvqCmD8koA0mP/8bnNFfn6ySpGUKjQ=; b=pXcm1quYWdOBPCJzw97caDH7jUgqV83RDpUk/ydq29MpVstn5PWqPHt/jACDF5+Olj 7z4he9P4I15TIatLQ6tOV6saPrFJHN9oaH/UB7al6eIOiZzBhjWOso84FdRSvND5F2fK jLERufDx+oQ4FlURlazAVCEJvascwEgTfFijNdFJ7+48Y+9OF1gx9I7KlNdcby1B6R3L Zz3d2AmFZp0Ubo0u6Dco2lBHrmaY41/SzVau2QTCgZeNBaec5cpmzhUG5oOB5KphGYK6 nRxPILq1nMddtMPzUt1iKP0iE3CWw1ErF/5FqFRDOEDFPX6Oa8gZd3iyZcm5cqG2u5nr CU8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786059396; x=1786664196; h=content-transfer-encoding:content-type: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 :content-type; bh=QkWxTw8zJYn5bKvqCmD8koA0mP/8bnNFfn6ySpGUKjQ=; b=HGLfP2Umi3CLEjIpn55LSXcm/BZIZhiFG86ATqmcwrbjo5lYo7TxCwzD0KatP9595z 21yc8Nc2n0EPNsOpMZAAarci0XzY9UQ5srQJRGpoacFwndL4YQe3GI5tEwpo4siRDV53 fTYW8A0J/rAitj/fR5WMXMeE42qD/h5y7XdOuqQhlHXKqQu/NQgNaVOoz4lkBUh4vR8k p/XfNldnelmkRBDXSdWTST94l9AOR4fyhOWhKO3lC6XsRJ9jSgGgnHwxLsAdXV6ZLB+o Jciju3nBjCvF7kM0Y/qPe9Q3SOUEURMziJbea5hOWDo8a76zAzfyzeWcStPGMhkwinTO vdAA== X-Forwarded-Encrypted: i=1; AHgh+RpoJZ+uaYPhhCnf3aaGB4W7bh2o0WiDxj7EvSgfDHCvW+Tjqz5GiaivGpLI0dUlR8P7pLZpe1MoyHAgycA=@vger.kernel.org X-Gm-Message-State: AOJu0Yy99aSiuIHbLKk7djcOrpALlHkGI5oCG10B4MEsJdb8KhtWF6cH gf9e4X7DrTA96xPHPF/An8Cr2V7GSvdGJXsUvbYbIbuEJu3G6WgZTC4Cexm/Dy3BYb+ihCLlmYO L42RJCg== X-Received: from pfqy1.prod.google.com ([2002:aa7:9e01:0:b0:848:46e6:1aac]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3692:b0:84e:24f:2677 with SMTP id d2e1a72fcca58-84f2dfc0601mr21949181b3a.4.1786059396135; Thu, 06 Aug 2026 16:36:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 6 Aug 2026 16:35:27 -0700 In-Reply-To: <20260806233609.212337-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: <20260806233609.212337-1-seanjc@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260806233609.212337-11-seanjc@google.com> Subject: [PATCH v6 10/51] x86/tdx: Force TSC frequency with CPUID-based info provided by the TDX-Module From: Sean Christopherson To: Kiryl Shutsemau , Rick Edgecombe , Sean Christopherson , Paolo Bonzini , "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: Vitaly Kuznetsov , Broadcom internal kernel review list , Boris Ostrovsky , Stephen Boyd , Miroslav Lichvar , 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 , David Woodhouse , David Woodhouse , Thomas Gleixner Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable When running as a TDX guest, explicitly set the TSC frequency to a known value, using CPUID-based information, instead of potentially relying on a hypervisor-controlled PV routine. For TDX guests, CPUID.0x15 is always emulated by the TDX-Module, i.e. the information from CPUID is more trustworthy than the information provided by the hypervisor. To maintain backwards compatibility with TDX guest kernels that use native calibration, and because it's the least awful option, retain native_calibrate_tsc()'s stuffing of the local APIC bus period using the core crystal frequency. While it's entirely possible for the hypervisor to emulate the APIC timer at a different frequency than the core crystal frequency, the commonly accepted interpretation of Intel's SDM is that APIC timer runs at the core crystal frequency when that latter is enumerated via CPUID: The APIC timer frequency will be the processor=E2=80=99s bus clock or cor= e crystal clock frequency (when TSC/core crystal clock ratio is enumerated in CPUID leaf 0x15). If the hypervisor is malicious and deliberately runs the APIC timer at the wrong frequency, nothing would stop the hypervisor from modifying the frequency at any time, i.e. attempting to manually calibrate the frequency out of paranoia would be futile. Deliberately leave CPU frequency calibration as is, since the TDX-Module doesn't provide any guarantees with respect to CPUID.0x16. Expose and use cpuid_get_tsc_info() instead of providing a wrapper to get the TSC and core crystal frequency, as TDX is the only anticipated user outside of the TSC code, i.e. adding a helper to dedup the math won't actually dedup anything. Having TDX use "struct cpuid_tsc_info" also avoids the temptation of declaring a local "tsc_khz" variable and thus unintentionally creating a shadow of the global "tsc_khz". Cc: Kiryl Shutsemau (Meta) Signed-off-by: Sean Christopherson --- .../admin-guide/kernel-parameters.txt | 4 ++-- arch/x86/coco/tdx/tdx.c | 20 ++++++++++++++++--- arch/x86/include/asm/tdx.h | 2 ++ arch/x86/include/asm/tsc.h | 7 +++++++ arch/x86/kernel/tsc.c | 11 ++++------ 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index aa4eb568d9fb..5a3d40a4a4ec 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -7947,8 +7947,8 @@ Kernel parameters Format: =20 Note, tsc_early_khz is ignored if the TSC frequency is - provided by trusted firmware when running as an SNP - guest. + provided by trusted firmware when running as an SNP or + TDX guest. =20 tsx=3D [X86] Control Transactional Synchronization Extensions (TSX) feature in Intel processors that diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index f904a636d449..ec28a15e26ac 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -1121,9 +1122,6 @@ void __init tdx_early_init(void) =20 setup_force_cpu_cap(X86_FEATURE_TDX_GUEST); =20 - /* TSC is the only reliable clock in TDX guest */ - setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE); - cc_vendor =3D CC_VENDOR_INTEL; =20 /* Configure the TD */ @@ -1193,3 +1191,19 @@ void __init tdx_early_init(void) =20 tdx_announce(); } + +unsigned int __init tdx_tsc_init(void) +{ + struct cpuid_tsc_info info; + + if (WARN_ON_ONCE(cpuid_get_tsc_info(&info) || !info.crystal_khz)) + return 0; + + apic_set_timer_frequency_khz(info.crystal_khz, "TDX-Module via CPUID"); + + /* TSC is the only reliable clock in TDX guest */ + setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE); + setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ); + + return info.crystal_khz * info.numerator / info.denominator; +} diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index 89e97d5761d8..d23ff06db41a 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -68,6 +68,7 @@ struct ve_info { #ifdef CONFIG_INTEL_TDX_GUEST =20 void __init tdx_early_init(void); +unsigned int __init tdx_tsc_init(void); =20 void tdx_get_ve_info(struct ve_info *ve); =20 @@ -89,6 +90,7 @@ void __init tdx_dump_td_ctls(u64 td_ctls); #else =20 static inline void tdx_early_init(void) { }; +static inline unsigned int tdx_tsc_init(void) { return 0; } static inline void tdx_halt(void) { }; =20 static inline bool tdx_early_handle_ve(struct pt_regs *regs) { return fals= e; } diff --git a/arch/x86/include/asm/tsc.h b/arch/x86/include/asm/tsc.h index 4d2d2f21ff06..b6b86e24e1bf 100644 --- a/arch/x86/include/asm/tsc.h +++ b/arch/x86/include/asm/tsc.h @@ -82,6 +82,13 @@ static inline cycles_t get_cycles(void) } #define get_cycles get_cycles =20 +struct cpuid_tsc_info { + unsigned int denominator; + unsigned int numerator; + unsigned int crystal_khz; +}; +extern int cpuid_get_tsc_info(struct cpuid_tsc_info *info); + extern void tsc_early_init(void); extern void tsc_init(void); extern void mark_tsc_unstable(char *reason); diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c index d405ff4e45e0..f3febe406fa8 100644 --- a/arch/x86/kernel/tsc.c +++ b/arch/x86/kernel/tsc.c @@ -34,6 +34,7 @@ #include #include #include +#include =20 unsigned int __read_mostly cpu_khz; /* TSC clocks / usec, not used here */ EXPORT_SYMBOL(cpu_khz); @@ -645,13 +646,7 @@ static unsigned long quick_pit_calibrate(void) return delta; } =20 -struct cpuid_tsc_info { - unsigned int denominator; - unsigned int numerator; - unsigned int crystal_khz; -}; - -static int cpuid_get_tsc_info(struct cpuid_tsc_info *info) +int cpuid_get_tsc_info(struct cpuid_tsc_info *info) { unsigned int ecx_hz, edx; =20 @@ -1529,6 +1524,8 @@ void __init tsc_early_init(void) =20 if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) known_tsc_khz =3D snp_secure_tsc_init(); + else if (boot_cpu_has(X86_FEATURE_TDX_GUEST)) + known_tsc_khz =3D tdx_tsc_init(); =20 /* * Ignore the user-provided TSC frequency if the exact frequency was --=20 2.55.0.679.g6767b8d81c-goog