Re: [PATCH 3/7] KVM: SVM: Save restore FRED_RSP0 for FRED supported guests
From: Shivansh Dhiman
Date: Thu Mar 05 2026 - 15:40:45 EST
Hi,
On 29-01-2026 12:06, Shivansh Dhiman wrote:
> From: Neeraj Upadhyay <Neeraj.Upadhyay@xxxxxxx>
>
> Hardware does not save/restore FRED_RSP0 for Non-SEV-ES guests.
> Save/restore it early in svm_vcpu_enter_exit() so that the
> correct physical CPU state is updated.
I'm planning to improve this path in v2 of this series, by moving the
restoring/saving of FRED RSP0 to svm_prepare_[switch_to_guest/host_switch]
respectively, thus saving some MSR accesses. Any comments are welcome.
>
> Synchronize the current value of MSR_IA32_FRED_RSP0 in hardware to the kernel's
> local cache. Note that the desired host's RSP0 will be set when the CPU exits to
> userspace for servicing vCPU tasks.
>
> Signed-off-by: Neeraj Upadhyay <Neeraj.Upadhyay@xxxxxxx>
> Co-developed-by: Shivansh Dhiman <shivansh.dhiman@xxxxxxx>
> Signed-off-by: Shivansh Dhiman <shivansh.dhiman@xxxxxxx>
> ---
> arch/x86/kvm/svm/svm.c | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index 05e44e804aba..ddd8941af6f0 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -4192,6 +4192,15 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, bool spec_ctrl_in
> {
> struct svm_cpu_data *sd = per_cpu_ptr(&svm_data, vcpu->cpu);
> struct vcpu_svm *svm = to_svm(vcpu);
> + bool update_fred_rsp0;
> +
> + /*
> + * Hardware does not save/restore FRED_RSP0 for Non-SEV-ES guests.
> + */
> + update_fred_rsp0 = !sev_es_guest(vcpu->kvm) && guest_cpu_cap_has(vcpu, X86_FEATURE_FRED);
> +
> + if (update_fred_rsp0)
> + wrmsrq(MSR_IA32_FRED_RSP0, svm->vmcb->save.fred_rsp0);
>
> guest_state_enter_irqoff();
>
@@ -1391,12 +1433,34 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu
*vcpu)
sd->bp_spec_reduce_set = true;
msr_set_bit(MSR_ZEN4_BP_CFG, MSR_ZEN4_BP_CFG_BP_SPEC_REDUCE_BIT);
}
+
+ /*
+ * Hardware does not save/restore FRED_RSP0 for Non-SEV-ES guests.
+ */
+ if (!sev_es_guest(vcpu->kvm) && guest_cpu_cap_has(vcpu, X86_FEATURE_FRED))
+ wrmsrq(MSR_IA32_FRED_RSP0, svm->vmcb->save.fred_rsp0);
+
svm->guest_state_loaded = true;
}
> @@ -4218,6 +4227,15 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, bool spec_ctrl_in
> raw_local_irq_disable();
>
> guest_state_exit_irqoff();
> +
> + if (update_fred_rsp0) {
> + rdmsrq(MSR_IA32_FRED_RSP0, svm->vmcb->save.fred_rsp0);
> + /*
> + * Sync hardware MSR value to per-CPU cache. This helps in restoring
> + * Host RSP0 when exiting to userspace in fred_update_rsp0().
> + */
> + fred_sync_rsp0(svm->vmcb->save.fred_rsp0);
> + }
> }
>
> static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)
static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
{
- to_svm(vcpu)->guest_state_loaded = false;
+ struct vcpu_svm *svm = to_svm(vcpu);
+
+ if (!svm->guest_state_loaded)
+ return;
+
+ /*
+ * Hardware does not save/restore FRED_RSP0 for Non-SEV-ES guests.
+ * Also, sync hardware MSR value to per-CPU cache. This helps in
+ * restoring Host RSP0 when exiting to userspace in fred_update_rsp0().
+ */
+ if (!sev_es_guest(vcpu->kvm) && guest_cpu_cap_has(vcpu, X86_FEATURE_FRED)) {
+ rdmsrq(MSR_IA32_FRED_RSP0, svm->vmcb->save.fred_rsp0);
+ fred_sync_rsp0(svm->vmcb->save.fred_rsp0);
+ }
+
+ svm->guest_state_loaded = false;
}
Thanks,
Shivansh