Re: [PATCH Part2 RFC v4 39/40] KVM: SVM: Use a VMSA physical address variable for populating VMCB

From: Tom Lendacky
Date: Wed Jul 21 2021 - 12:26:50 EST


On 7/20/21 7:20 PM, Sean Christopherson wrote:
> On Wed, Jul 07, 2021, Brijesh Singh wrote:
>> From: Tom Lendacky <thomas.lendacky@xxxxxxx>
>>
>> In preparation to support SEV-SNP AP Creation, use a variable that holds
>> the VMSA physical address rather than converting the virtual address.
>> This will allow SEV-SNP AP Creation to set the new physical address that
>> will be used should the vCPU reset path be taken.
>
> I'm pretty sure adding vmsa_pa is unnecessary. The next patch sets svm->vmsa_pa
> and vmcb->control.vmsa_pa as a pair. And for the existing code, my proposed
> patch to emulate INIT on shutdown would eliminate the one path that zeros the
> VMCB[1]. That series patch also drops the init_vmcb() in svm_create_vcpu()[2].
>
> Assuming there are no VMCB shenanigans I'm missing, sev_es_init_vmcb() can do
>
> if (!init_event)
> svm->vmcb->control.vmsa_pa = __pa(svm->vmsa);

That will require passing init_event through to init_vmcb and successive
functions and ensuring that there isn't a path that could cause it to not
be set after it should no longer be used. This is very simple at the
moment, but maybe can be re-worked once all of the other changes you
mention are integrated.

Thanks,
Tom

>
> And while I'm thinking of it, the next patch should ideally free svm->vmsa when
> the the guest configures a new VMSA for the vCPU.
>
> [1] https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flkml.kernel.org%2Fr%2F20210713163324.627647-45-seanjc%40google.com&amp;data=04%7C01%7Cthomas.lendacky%40amd.com%7Cef81e5604f5242262b6908d94bdd5b32%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637624236352681486%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=O3LKXhVLqNuT1PpCNzkjG8Vho7wfMEibFgGbZkoFlMk%3D&amp;reserved=0
> [2] https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flkml.kernel.org%2Fr%2F20210713163324.627647-10-seanjc%40google.com&amp;data=04%7C01%7Cthomas.lendacky%40amd.com%7Cef81e5604f5242262b6908d94bdd5b32%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637624236352681486%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=rn6zZZbGEnN4Hd60Mg3EsPU3fIaoBHdA3jTluiDRvpo%3D&amp;reserved=0
>
>> Signed-off-by: Tom Lendacky <thomas.lendacky@xxxxxxx>
>> Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx>
>> ---
>> arch/x86/kvm/svm/sev.c | 5 ++---
>> arch/x86/kvm/svm/svm.c | 9 ++++++++-
>> arch/x86/kvm/svm/svm.h | 1 +
>> 3 files changed, 11 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
>> index 4cb4c1d7e444..d8ad6dd58c87 100644
>> --- a/arch/x86/kvm/svm/sev.c
>> +++ b/arch/x86/kvm/svm/sev.c
>> @@ -3553,10 +3553,9 @@ void sev_es_init_vmcb(struct vcpu_svm *svm)
>>
>> /*
>> * An SEV-ES guest requires a VMSA area that is a separate from the
>> - * VMCB page. Do not include the encryption mask on the VMSA physical
>> - * address since hardware will access it using the guest key.
>> + * VMCB page.
>> */
>> - svm->vmcb->control.vmsa_pa = __pa(svm->vmsa);
>> + svm->vmcb->control.vmsa_pa = svm->vmsa_pa;
>>
>> /* Can't intercept CR register access, HV can't modify CR registers */
>> svm_clr_intercept(svm, INTERCEPT_CR0_READ);
>> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
>> index 32e35d396508..74bc635c9608 100644
>> --- a/arch/x86/kvm/svm/svm.c
>> +++ b/arch/x86/kvm/svm/svm.c
>> @@ -1379,9 +1379,16 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)
>> svm->vmcb01.ptr = page_address(vmcb01_page);
>> svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
>>
>> - if (vmsa_page)
>> + if (vmsa_page) {
>> svm->vmsa = page_address(vmsa_page);
>>
>> + /*
>> + * Do not include the encryption mask on the VMSA physical
>> + * address since hardware will access it using the guest key.
>> + */
>> + svm->vmsa_pa = __pa(svm->vmsa);
>> + }
>> +
>> svm->guest_state_loaded = false;
>>
>> svm_switch_vmcb(svm, &svm->vmcb01);
>> diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
>> index 9fcfc0a51737..285d9b97b4d2 100644
>> --- a/arch/x86/kvm/svm/svm.h
>> +++ b/arch/x86/kvm/svm/svm.h
>> @@ -177,6 +177,7 @@ struct vcpu_svm {
>>
>> /* SEV-ES support */
>> struct sev_es_save_area *vmsa;
>> + hpa_t vmsa_pa;
>> struct ghcb *ghcb;
>> struct kvm_host_map ghcb_map;
>> bool received_first_sipi;
>> --
>> 2.17.1
>>