Re: [RFC PATCH 26/35] KVM: SVM: Guest FPU state save/restore not needed for SEV-ES guest

From: Tom Lendacky
Date: Tue Sep 15 2020 - 20:10:47 EST


On 9/14/20 4:39 PM, Sean Christopherson wrote:
> On Mon, Sep 14, 2020 at 03:15:40PM -0500, Tom Lendacky wrote:
>> From: Tom Lendacky <thomas.lendacky@xxxxxxx>
>>
>> The guest FPU is automatically restored on VMRUN and saved on VMEXIT by
>> the hardware, so there is no reason to do this in KVM.
>
> I assume hardware has its own buffer? If so, a better approach would be to
> not allocate arch.guest_fpu in the first place, and then rework KVM to key
> off !guest_fpu.

Yup, let me look into that.

Thanks,
Tom

>
>> Signed-off-by: Tom Lendacky <thomas.lendacky@xxxxxxx>
>> ---
>> arch/x86/kvm/svm/svm.c | 8 ++++++--
>> arch/x86/kvm/x86.c | 18 ++++++++++++++----
>> 2 files changed, 20 insertions(+), 6 deletions(-)
>>
>> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
>> index b35c2de1130c..48699c41b62a 100644
>> --- a/arch/x86/kvm/svm/svm.c
>> +++ b/arch/x86/kvm/svm/svm.c
>> @@ -3682,7 +3682,9 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
>> svm_set_dr6(svm, DR6_FIXED_1 | DR6_RTM);
>>
>> clgi();
>> - kvm_load_guest_xsave_state(vcpu);
>> +
>> + if (!sev_es_guest(svm->vcpu.kvm))
>> + kvm_load_guest_xsave_state(vcpu);
>>
>> if (lapic_in_kernel(vcpu) &&
>> vcpu->arch.apic->lapic_timer.timer_advance_ns)
>> @@ -3728,7 +3730,9 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
>> if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
>> kvm_before_interrupt(&svm->vcpu);
>>
>> - kvm_load_host_xsave_state(vcpu);
>> + if (!sev_es_guest(svm->vcpu.kvm))
>> + kvm_load_host_xsave_state(vcpu);
>> +
>> stgi();
>>
>> /* Any pending NMI will happen here */
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index 76efe70cd635..a53e24c1c5d1 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -8896,9 +8896,14 @@ static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
>>
>> kvm_save_current_fpu(vcpu->arch.user_fpu);
>>
>> - /* PKRU is separately restored in kvm_x86_ops.run. */
>> - __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
>> - ~XFEATURE_MASK_PKRU);
>> + /*
>> + * An encrypted save area means that the guest state can't be
>> + * set by the hypervisor, so skip trying to set it.
>> + */
>> + if (!vcpu->arch.vmsa_encrypted)
>> + /* PKRU is separately restored in kvm_x86_ops.run. */
>> + __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
>> + ~XFEATURE_MASK_PKRU);
>>
>> fpregs_mark_activate();
>> fpregs_unlock();
>> @@ -8911,7 +8916,12 @@ static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
>> {
>> fpregs_lock();
>>
>> - kvm_save_current_fpu(vcpu->arch.guest_fpu);
>> + /*
>> + * An encrypted save area means that the guest state can't be
>> + * read/saved by the hypervisor, so skip trying to save it.
>> + */
>> + if (!vcpu->arch.vmsa_encrypted)
>> + kvm_save_current_fpu(vcpu->arch.guest_fpu);
>>
>> copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
>>
>> --
>> 2.28.0
>>