From: Tom Lendacky <thomas.lendacky@amd.com>
To: Hou Wenlong <houwenlong.hwl@antgroup.com>, kvm@vger.kernel.org
Cc: Lai Jiangshan <jiangshan.ljs@antgroup.com>,
Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] KVM: SVM: Use cached value as restore value of TSC_AUX for SEV-ES guest
Date: Thu, 18 Sep 2025 13:47:06 -0500 [thread overview]
Message-ID: <9991df11-fe7c-41e1-9890-f0c38adc8137@amd.com> (raw)
In-Reply-To: <9da5eb48ccf403e1173484195d3d7d96978125b7.1758166596.git.houwenlong.hwl@antgroup.com>
On 9/17/25 22:38, Hou Wenlong wrote:
> The commit 916e3e5f26ab ("KVM: SVM: Do not use user return MSR support
> for virtualized TSC_AUX") assumes that TSC_AUX is not changed by Linux
> post-boot, so it always restores the initial host value on #VMEXIT.
> However, this is not true in KVM, as it can be modified by user return
> MSR support for normal guests. If an SEV-ES guest always restores the
> initial host value on #VMEXIT, this may result in the cached value in
> user return MSR being different from the hardware value if the previous
> vCPU was a non-SEV-ES guest that had called kvm_set_user_return_msr().
> Consequently, this may pose a problem when switching back to that vCPU,
> as kvm_set_user_return_msr() would not update the hardware value because
> the cached value matches the target value. Unlike the TDX case, the
> SEV-ES guest has the ability to set the restore value in the host save
> area, and the cached value in the user return MSR is always the current
> hardware value. Therefore, the cached value could be used directly
> without RDMSR in svm_prepare_switch_to_guest(), making this change
> minimal.
I'm not sure I follow. If Linux never changes the value of TSC_AUX once it
has set it, then how can it ever be different? Have you seen this issue?
Thanks,
Tom
>
> Fixes: 916e3e5f26ab ("KVM: SVM: Do not use user return MSR support for virtualized TSC_AUX")
> Suggested-by: Lai Jiangshan <jiangshan.ljs@antgroup.com>
> Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
> ---
> arch/x86/kvm/svm/svm.c | 33 ++++++++++++++-------------------
> 1 file changed, 14 insertions(+), 19 deletions(-)
>
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index 1650de78648a..1be9c65ee23b 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -577,18 +577,6 @@ static int svm_enable_virtualization_cpu(void)
>
> amd_pmu_enable_virt();
>
> - /*
> - * If TSC_AUX virtualization is supported, TSC_AUX becomes a swap type
> - * "B" field (see sev_es_prepare_switch_to_guest()) for SEV-ES guests.
> - * Since Linux does not change the value of TSC_AUX once set, prime the
> - * TSC_AUX field now to avoid a RDMSR on every vCPU run.
> - */
> - if (boot_cpu_has(X86_FEATURE_V_TSC_AUX)) {
> - u32 __maybe_unused msr_hi;
> -
> - rdmsr(MSR_TSC_AUX, sev_es_host_save_area(sd)->tsc_aux, msr_hi);
> - }
> -
> return 0;
> }
>
> @@ -1408,12 +1396,19 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
> /*
> * TSC_AUX is always virtualized for SEV-ES guests when the feature is
> * available. The user return MSR support is not required in this case
> - * because TSC_AUX is restored on #VMEXIT from the host save area
> - * (which has been initialized in svm_enable_virtualization_cpu()).
> + * because TSC_AUX is restored on #VMEXIT from the host save area.
> + * However, user return MSR could change the value of TSC_AUX in the
> + * kernel. Therefore, to maintain the logic of user return MSR, set the
> + * restore value to the cached value of user return MSR, which should
> + * always reflect the current hardware value.
> */
> - if (likely(tsc_aux_uret_slot >= 0) &&
> - (!boot_cpu_has(X86_FEATURE_V_TSC_AUX) || !sev_es_guest(vcpu->kvm)))
> - kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
> + if (likely(tsc_aux_uret_slot >= 0)) {
> + if (!boot_cpu_has(X86_FEATURE_V_TSC_AUX) || !sev_es_guest(vcpu->kvm))
> + kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
> + else
> + sev_es_host_save_area(sd)->tsc_aux =
> + (u32)kvm_get_user_return_msr_cache(tsc_aux_uret_slot);
> + }
>
> if (cpu_feature_enabled(X86_FEATURE_SRSO_BP_SPEC_REDUCE) &&
> !sd->bp_spec_reduce_set) {
> @@ -3004,8 +2999,8 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
> * TSC_AUX is always virtualized for SEV-ES guests when the
> * feature is available. The user return MSR support is not
> * required in this case because TSC_AUX is restored on #VMEXIT
> - * from the host save area (which has been initialized in
> - * svm_enable_virtualization_cpu()).
> + * from the host save area (which has been set in
> + * svm_prepare_switch_to_guest()).
> */
> if (boot_cpu_has(X86_FEATURE_V_TSC_AUX) && sev_es_guest(vcpu->kvm))
> break;
next prev parent reply other threads:[~2025-09-18 18:47 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-18 3:38 [PATCH 1/2] KVM: x86: Add helper to retrieve cached value of user return MSR Hou Wenlong
2025-09-18 3:38 ` [PATCH 2/2] KVM: SVM: Use cached value as restore value of TSC_AUX for SEV-ES guest Hou Wenlong
2025-09-18 18:47 ` Tom Lendacky [this message]
2025-09-19 13:15 ` Hou Wenlong
2025-09-19 16:23 ` Sean Christopherson
2025-09-19 16:30 ` Sean Christopherson
2025-09-19 16:45 ` Sean Christopherson
2025-09-20 6:52 ` Hou Wenlong
2025-09-19 16:28 ` [PATCH 1/2] KVM: x86: Add helper to retrieve cached value of user return MSR Sean Christopherson
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=9991df11-fe7c-41e1-9890-f0c38adc8137@amd.com \
--to=thomas.lendacky@amd.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=houwenlong.hwl@antgroup.com \
--cc=hpa@zytor.com \
--cc=jiangshan.ljs@antgroup.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--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
all inboxes | Powered by JetHome®