Re: [PATCH v1 RESEND 4/9] x86/kvm/mmu: introduce guest_mmu

From: Sean Christopherson
Date: Wed Sep 19 2018 - 11:08:47 EST


On Tue, 2018-09-18 at 18:09 +0200, Vitaly Kuznetsov wrote:
> When EPT is used for nested guest we need to re-init MMU as shadow
> EPT MMU (nested_ept_init_mmu_context() does that). When we return back
> from L2 to L1 kvm_mmu_reset_context() in nested_vmx_load_cr3() resets
> MMU back to normal TDP mode. Add a special 'guest_mmu' so we can use
> separate root caches; the improved hit rate is not very important for
> single vCPU performance, but it avoids contention on the mmu_lock for
> many vCPUs.
>
> On the nested CPUID benchmark, with 16 vCPUs, an L2->L1->L2 vmexit
> goes from 42k to 26k cycles.
>
> Signed-off-by: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
> Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
> ---
> Âarch/x86/include/asm/kvm_host.h |ÂÂ3 +++
> Âarch/x86/kvm/mmu.cÂÂÂÂÂÂÂÂÂÂÂÂÂÂ| 15 +++++++++++----
> Âarch/x86/kvm/vmx.cÂÂÂÂÂÂÂÂÂÂÂÂÂÂ| 27 +++++++++++++++++++--------
> Â3 files changed, 33 insertions(+), 12 deletions(-)

...

> @@ -10926,12 +10935,12 @@ static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
> Â */
> Âstatic void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu)
> Â{
> -ÂÂÂÂÂÂÂstruct vcpu_vmx *vmx = to_vmx(vcpu);
> + struct vcpu_vmx *vmx = to_vmx(vcpu);

Might be worth dropping the local @vmx and calling to_vmx() inline
since it's now being used only for the call to vmx_switch_vmcs().

>
> -ÂÂÂÂÂÂÂvmx_switch_vmcs(vcpu, &vmx->vmcs01);
> -ÂÂÂÂÂÂÂfree_nested(vmx);
> -ÂÂÂÂÂÂÂvcpu_put(vcpu);
> + vcpu_load(vcpu);
> + vmx_switch_vmcs(vcpu, &vmx->vmcs01);
> + free_nested(vcpu);
> + vcpu_put(vcpu);
> Â}
>
> Âstatic void vmx_free_vcpu(struct kvm_vcpu *vcpu)
> @@ -11281,6 +11290,7 @@ static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
> Â if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu)))
> Â return 1;
> Â
> + vcpu->arch.mmu = &vcpu->arch.guest_mmu;
> Â kvm_init_shadow_ept_mmu(vcpu,
> Â to_vmx(vcpu)->nested.msrs.ept_caps &
> Â VMX_EPT_EXECUTE_ONLY_BIT,
> @@ -11296,6 +11306,7 @@ static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)
> Â
> Âstatic void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu)
> Â{
> + vcpu->arch.mmu = &vcpu->arch.root_mmu;
> Â vcpu->arch.walk_mmu = &vcpu->arch.root_mmu;
> Â}
> Â
> @@ -13363,7 +13374,7 @@ static void vmx_leave_nested(struct kvm_vcpu *vcpu)
> Â to_vmx(vcpu)->nested.nested_run_pending = 0;
> Â nested_vmx_vmexit(vcpu, -1, 0, 0);
> Â }
> - free_nested(to_vmx(vcpu));
> + free_nested(vcpu);
> Â}
> Â
> Â/*