diff mbox series

[v2,09/46] KVM: SVM: Drop a redundant init_vmcb() from svm_create_vcpu()

Message ID 20210713163324.627647-10-seanjc@google.com (mailing list archive)
State New, archived
Headers show
Series KVM: x86: vCPU RESET/INIT fixes and consolidation | expand

Commit Message

Sean Christopherson July 13, 2021, 4:32 p.m. UTC
Drop an extra init_vmcb() from svm_create_vcpu(), svm_vcpu_reset() is
guaranteed to call init_vmcb() and there are no consumers of the VMCB
data between ->vcpu_create() and ->vcpu_reset().  Keep the call to
svm_switch_vmcb() as sev_es_create_vcpu() touches the current VMCB, but
hoist it up a few lines to associate the switch with the allocation of
vmcb01.

Reviewed-by: Reiji Watanabe <reijiw@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/svm.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Comments

Paolo Bonzini July 26, 2021, 8:33 p.m. UTC | #1
On 13/07/21 18:32, Sean Christopherson wrote:
> Drop an extra init_vmcb() from svm_create_vcpu(), svm_vcpu_reset() is
> guaranteed to call init_vmcb() and there are no consumers of the VMCB
> data between ->vcpu_create() and ->vcpu_reset().  Keep the call to
> svm_switch_vmcb() as sev_es_create_vcpu() touches the current VMCB, but
> hoist it up a few lines to associate the switch with the allocation of
> vmcb01.
> 
> Reviewed-by: Reiji Watanabe <reijiw@google.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
>   arch/x86/kvm/svm/svm.c | 4 +---
>   1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> index 44248548be7d..cef9520fe77f 100644
> --- a/arch/x86/kvm/svm/svm.c
> +++ b/arch/x86/kvm/svm/svm.c
> @@ -1431,15 +1431,13 @@ 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);
> +	svm_switch_vmcb(svm, &svm->vmcb01);
>   
>   	if (vmsa_page)
>   		svm->vmsa = page_address(vmsa_page);
>   
>   	svm->guest_state_loaded = false;
>   
> -	svm_switch_vmcb(svm, &svm->vmcb01);
> -	init_vmcb(vcpu);
> -
>   	svm_init_osvw(vcpu);
>   	vcpu->arch.microcode_version = 0x01000065;

While this patch makes sense, I'd rather not include it to reduce the 
part of the code in which svm->vmcb is NULL.   See for example the issue 
reported by the static analyzer in which 
svm_hv_vmcb_dirty_nested_enlightenments (called by svm_vcpu_init_msrpm) 
dereferences svm->vmcb.

Paolo
Sean Christopherson July 26, 2021, 10:26 p.m. UTC | #2
On Mon, Jul 26, 2021, Paolo Bonzini wrote:
> On 13/07/21 18:32, Sean Christopherson wrote:
> > Drop an extra init_vmcb() from svm_create_vcpu(), svm_vcpu_reset() is
> > guaranteed to call init_vmcb() and there are no consumers of the VMCB
> > data between ->vcpu_create() and ->vcpu_reset().  Keep the call to
> > svm_switch_vmcb() as sev_es_create_vcpu() touches the current VMCB, but
> > hoist it up a few lines to associate the switch with the allocation of
> > vmcb01.
> > 
> > Reviewed-by: Reiji Watanabe <reijiw@google.com>
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > ---
> >   arch/x86/kvm/svm/svm.c | 4 +---
> >   1 file changed, 1 insertion(+), 3 deletions(-)
> > 
> > diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
> > index 44248548be7d..cef9520fe77f 100644
> > --- a/arch/x86/kvm/svm/svm.c
> > +++ b/arch/x86/kvm/svm/svm.c
> > @@ -1431,15 +1431,13 @@ 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);
> > +	svm_switch_vmcb(svm, &svm->vmcb01);
> >   	if (vmsa_page)
> >   		svm->vmsa = page_address(vmsa_page);
> >   	svm->guest_state_loaded = false;
> > -	svm_switch_vmcb(svm, &svm->vmcb01);
> > -	init_vmcb(vcpu);
> > -
> >   	svm_init_osvw(vcpu);
> >   	vcpu->arch.microcode_version = 0x01000065;
> 
> While this patch makes sense, I'd rather not include it to reduce the part
> of the code in which svm->vmcb is NULL.

Not sure I follow.  The svm_switch_vmcb() is kept, and even moved earlier.
diff mbox series

Patch

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 44248548be7d..cef9520fe77f 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -1431,15 +1431,13 @@  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);
+	svm_switch_vmcb(svm, &svm->vmcb01);
 
 	if (vmsa_page)
 		svm->vmsa = page_address(vmsa_page);
 
 	svm->guest_state_loaded = false;
 
-	svm_switch_vmcb(svm, &svm->vmcb01);
-	init_vmcb(vcpu);
-
 	svm_init_osvw(vcpu);
 	vcpu->arch.microcode_version = 0x01000065;