public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Nikunj A Dadhania <nikunj@amd.com>
To: <linux-kernel@vger.kernel.org>, <bp@alien8.de>, <x86@kernel.org>
Cc: <tglx@linutronix.de>, <mingo@redhat.com>,
	<dave.hansen@linux.intel.com>, <thomas.lendacky@amd.com>,
	<aik@amd.com>, <stable@vger.kernel.org>
Subject: Re: [PATCH] x86/sev: Use TSC_FACTOR for Secure TSC frequency calculation
Date: Tue, 24 Jun 2025 04:13:39 +0000	[thread overview]
Message-ID: <858qlhhj4c.fsf@amd.com> (raw)
In-Reply-To: <20250609080841.1394941-1-nikunj@amd.com>

Nikunj A Dadhania <nikunj@amd.com> writes:

> When using Secure TSC, the GUEST_TSC_FREQ MSR reports a frequency based on
> the nominal P0 frequency, which deviates slightly (typically ~0.2%) from
> the actual mean TSC frequency due to clocking parameters. Over extended VM
> uptime, this discrepancy accumulates, causing clock skew between the
> hypervisor and SEV-SNP VM, leading to early timer interrupts as perceived
> by the guest.
>
> The guest kernel relies on the reported nominal frequency for TSC-based
> timekeeping, while the actual frequency set during SNP_LAUNCH_START may
> differ. This mismatch results in inaccurate time calculations, causing the
> guest to perceive hrtimers as firing earlier than expected.
>
> Utilize the TSC_FACTOR from the SEV firmware's secrets page (see "Secrets
> Page Format" in the SNP Firmware ABI Specification) to calculate the mean
> TSC frequency, ensuring accurate timekeeping and mitigating clock skew in
> SEV-SNP VMs.
>
> Use early_ioremap_encrypted() to map the secrets page as
> ioremap_encrypted() uses kmalloc() which is not available during early TSC
> initialization and causes a panic.
>
> Fixes: 73bbf3b0fbba ("x86/tsc: Init the TSC for Secure TSC guests")
> Reviewed-by: Tom Lendacky <thomas.lendacky@amd.com>
> Cc: stable@vger.kernel.org
> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>

A gentle reminder for review.

Thanks
Nikunj

> ---
>  arch/x86/include/asm/sev.h |  5 ++++-
>  arch/x86/coco/sev/core.c   | 24 ++++++++++++++++++++++++
>  2 files changed, 28 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
> index 58e028d42e41..655d7e37bbcc 100644
> --- a/arch/x86/include/asm/sev.h
> +++ b/arch/x86/include/asm/sev.h
> @@ -282,8 +282,11 @@ struct snp_secrets_page {
>  	u8 svsm_guest_vmpl;
>  	u8 rsvd3[3];
>  
> +	/* The percentage decrease from nominal to mean TSC frequency. */
> +	u32 tsc_factor;
> +
>  	/* Remainder of page */
> -	u8 rsvd4[3744];
> +	u8 rsvd4[3740];
>  } __packed;
>  
>  struct snp_msg_desc {
> diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
> index b6db4e0b936b..ffd44712cec0 100644
> --- a/arch/x86/coco/sev/core.c
> +++ b/arch/x86/coco/sev/core.c
> @@ -2167,15 +2167,39 @@ static unsigned long securetsc_get_tsc_khz(void)
>  
>  void __init snp_secure_tsc_init(void)
>  {
> +	struct snp_secrets_page *secrets;
>  	unsigned long long tsc_freq_mhz;
> +	void *mem;
>  
>  	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
>  		return;
>  
> +	mem = early_memremap_encrypted(sev_secrets_pa, PAGE_SIZE);
> +	if (!mem) {
> +		pr_err("Unable to get TSC_FACTOR: failed to map the SNP secrets page.\n");
> +		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECURE_TSC);
> +	}
> +
> +	secrets = (__force struct snp_secrets_page *)mem;
> +
>  	setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
>  	rdmsrq(MSR_AMD64_GUEST_TSC_FREQ, tsc_freq_mhz);
>  	snp_tsc_freq_khz = (unsigned long)(tsc_freq_mhz * 1000);
>  
> +	/*
> +	 * Obtain the mean TSC frequency by decreasing the nominal TSC frequency with
> +	 * TSC_FACTOR as documented in the SNP Firmware ABI specification:
> +	 *
> +	 * GUEST_TSC_FREQ * (1 - (TSC_FACTOR * 0.00001))
> +	 *
> +	 * which is equivalent to:
> +	 *
> +	 * GUEST_TSC_FREQ -= (GUEST_TSC_FREQ * TSC_FACTOR) / 100000;
> +	 */
> +	snp_tsc_freq_khz -= (snp_tsc_freq_khz * secrets->tsc_factor) / 100000;
> +
>  	x86_platform.calibrate_cpu = securetsc_get_tsc_khz;
>  	x86_platform.calibrate_tsc = securetsc_get_tsc_khz;
> +
> +	early_memunmap(mem, PAGE_SIZE);
>  }
>
> base-commit: 337964c8abfbef645cbbe25245e25c11d9d1fc4c
> -- 
> 2.43.0

  reply	other threads:[~2025-06-24  4:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-09  8:08 [PATCH] x86/sev: Use TSC_FACTOR for Secure TSC frequency calculation Nikunj A Dadhania
2025-06-24  4:13 ` Nikunj A Dadhania [this message]
2025-06-24 19:04   ` Dionna Amalie Glaze
2025-06-25  4:55     ` Nikunj A. Dadhania
2025-06-25 13:31       ` Tom Lendacky
2025-06-25 14:03         ` Nikunj A. Dadhania

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=858qlhhj4c.fsf@amd.com \
    --to=nikunj@amd.com \
    --cc=aik@amd.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox