x86/kvm/nVMX: optimize MMU switch between L1 and L2
v2 cover x86/kvm/nVMX: optimize MMU switch between L1 and L2
Changes since v1 [Sean Christopherson]:
- drop now unneded local 'vmx' variable from vmx_free_vcpu_nested()
- Rename:
kvm_mmu_scache -> kvm_mmu_extended_role mmu_role.scache -> mmu_role.ext
mmu_role.base_role -> mmu_role.base
- Add BUILD_BUG_ONs checking MMU role unions sizes.
Original description:
Currently, when we switch from L1 to L2 (VMX) we do the following:
- Re-initialize L1 MMU as shadow EPT MMU (nested_ept_init_mmu_context())
- Re-initialize 'nested' MMU (nested_vmx_load_cr3() -> init_kvm_nested_mmu())
When we switch back we do:
- Re-initialize L1 MMU (nested_vmx_load_cr3() -> init_kvm_tdp_mmu())
This seems to be sub-optimal. Initializing MMU is expensive (thanks to update_permission_bitmask(), update_pkru_bitmask(),..) Try solving the issue by splitting L1-normal and L1-nested MMUs and checking if MMU reset is really needed. This spares us about 1000 cpu cycles on nested vmexit.
Brief look at SVM makes me think it can be optimized the exact same way. I'll do this in a separate series if nobody objects.
Paolo Bonzini (1):
x86/kvm/mmu: get rid of redundant kvm_mmu_setup()
Vitaly Kuznetsov (8):
x86/kvm/mmu: make vcpu->mmu a pointer to the current MMU x86/kvm/mmu.c: set get_pdptr hook in kvm_init_shadow_ept_mmu() x86/kvm/mmu.c: add kvm_mmu parameter to kvm_mmu_free_roots() x86/kvm/mmu: introduce guest_mmu
x86/kvm/mmu: make space for source data caching in struct kvm_mmu x86/kvm/nVMX: introduce source data cache for
kvm_init_shadow_ept_mmu()
x86/kvm/mmu: check if tdp/shadow MMU reconfiguration is needed x86/kvm/mmu: check if MMU reconfiguration is needed in
init_kvm_nested_mmu()
arch/x86/include/asm/kvm_host.h | 44 +++- arch/x86/kvm/mmu.c | 345 +++++++++++++++++++------------- arch/x86/kvm/mmu.h | 8 +- arch/x86/kvm/mmu_audit.c | 12 +- arch/x86/kvm/paging_tmpl.h | 15 +- arch/x86/kvm/svm.c | 14 +- arch/x86/kvm/vmx.c | 46 +++-- arch/x86/kvm/x86.c | 22 +- 8 files changed, 305 insertions(+), 201 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/9 x86/kvm/mmu: make vcpu->mmu a pointer to the current MMU
As a preparation to full MMU split between L1 and L2 make vcpu->arch.mmu a pointer to the currently used mmu. For now, this is always vcpu->arch.root_mmu. No functional change.
--- arch/x86/include/asm/kvm_host.h | 5 +- arch/x86/kvm/mmu.c | 165 ++++++++++++++++---------------- arch/x86/kvm/mmu.h | 8 +- arch/x86/kvm/mmu_audit.c | 12 +-- arch/x86/kvm/paging_tmpl.h | 15 +-- arch/x86/kvm/svm.c | 14 +-- arch/x86/kvm/vmx.c | 15 +-- arch/x86/kvm/x86.c | 20 ++-- 8 files changed, 130 insertions(+), 124 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -1
@@ -534,7 +534,10 @@ struct kvm_vcpu_arch {* the paging mode of the l1 guest. This context is always used to * handle faults. */ -struct kvm_mmu mmu; +struct kvm_mmu *mmu; + +/* Non-nested MMU for L1 */ +struct kvm_mmu root_mmu;/* * Paging state of an L2 guest (used for nested npt)
arch/x86/kvm/mmu.c +83 -82
504 lines not on this page · raw message
arch/x86/kvm/mmu.h +4 -4
@@ -80,7 +80,7 @@ static inline unsigned int kvm_mmu_available_pages(struct kvm *kvm)static inline int kvm_mmu_reload (struct kvm_vcpu *vcpu) { -if (likely (vcpu->arch.mmu.root_hpa !=INVALID_PAGE )) +if (likely (vcpu->arch.mmu->root_hpa !=INVALID_PAGE ))return 0 ;return kvm_mmu_load (vcpu);
@@ -102,9 +102,9 @@ static inline unsigned long kvm_get_active_pcid(struct kvm_vcpu *vcpu)static inline void kvm_mmu_load_cr3 (struct kvm_vcpu *vcpu) { -if (VALID_PAGE (vcpu->arch.mmu.root_hpa)) - vcpu->arch.mmu.set_cr3 (vcpu, vcpu->arch.mmu.root_hpa | -kvm_get_active_pcid (vcpu)); +if (VALID_PAGE (vcpu->arch.mmu->root_hpa)) + vcpu->arch.mmu->set_cr3 (vcpu, vcpu->arch.mmu->root_hpa | +kvm_get_active_pcid (vcpu)); }/*
arch/x86/kvm/mmu_audit.c +6 -6
@@ -59,19 +59,19 @@ static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)int i;struct kvm_mmu_page *sp; -if (!VALID_PAGE (vcpu->arch.mmu.root_hpa)) +if (!VALID_PAGE (vcpu->arch.mmu->root_hpa))return ; -if (vcpu->arch.mmu.root_level >=PT64_ROOT_4LEVEL ) { -hpa_t root = vcpu->arch.mmu.root_hpa; +if (vcpu->arch.mmu->root_level >=PT64_ROOT_4LEVEL ) { +hpa_t root = vcpu->arch.mmu->root_hpa; sp =page_header (root); -__mmu_spte_walk (vcpu, sp, fn, vcpu->arch.mmu.root_level); +__mmu_spte_walk (vcpu, sp, fn, vcpu->arch.mmu->root_level);return ; }for (i =0 ; i <4 ; ++i) { -hpa_t root = vcpu->arch.mmu.pae_root[i]; +hpa_t root = vcpu->arch.mmu->pae_root[i];if (root &&VALID_PAGE (root)) { root &=PT64_BASE_ADDR_MASK ;
@@ -122,7 +122,7 @@ static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level) hpa = pfn <<PAGE_SHIFT ;if ((*sptep &PT64_BASE_ADDR_MASK ) != hpa)audit_printk (vcpu->kvm,"levels %d pfn %llx hpa %llx " -"ent %llxn" , vcpu->arch.mmu.root_level, pfn, +"ent %llxn" , vcpu->arch.mmu->root_level, pfn, hpa, *sptep); }
arch/x86/kvm/paging_tmpl.h +8 -7
@@ -158,14 +158,15 @@ static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, u64 *spte, u64 gpte) { - if (is_rsvd_bits_set(&vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL)) + if (is_rsvd_bits_set(vcpu->arch.mmu, gpte, PT_PAGE_TABLE_LEVEL)) goto no_present;if (!FNAME (is_present_gpte)(gpte)) goto no_present;/* if accessed bit is not supported prefetch non accessed gpte */ -if (PT_HAVE_ACCESSED_DIRTY (&vcpu->arch.mmu) && !(gpte &PT_GUEST_ACCESSED_MASK )) +if (PT_HAVE_ACCESSED_DIRTY (vcpu->arch.mmu) && + !(gpte &PT_GUEST_ACCESSED_MASK )) goto no_present;return false;
@@ -480,7 +481,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,static int FNAME (walk_addr )(struct guest_walker *walker,struct kvm_vcpu *vcpu,gva_t addr,u32 access) { -return FNAME (walk_addr_generic)(walker, vcpu, &vcpu->arch.mmu, addr, +return FNAME (walk_addr_generic)(walker, vcpu, vcpu->arch.mmu, addr, access); }
@@ -509,7 +510,7 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, gfn =gpte_to_gfn (gpte); pte_access = sp->role.access &FNAME (gpte_access)(gpte); -FNAME (protect_clean_gpte)(&vcpu->arch.mmu, &pte_access, gpte); +FNAME (protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte); pfn =pte_prefetch_gfn_to_pfn (vcpu, gfn, no_dirty_log && (pte_access &ACC_WRITE_MASK ));if (is_error_pfn (pfn))
@@ -604,7 +605,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, direct_access = gw->pte_access; - top_level = vcpu->arch.mmu.root_level; + top_level = vcpu->arch.mmu->root_level;if (top_level ==PT32E_ROOT_LEVEL ) top_level =PT32_ROOT_LEVEL ;/*
@@ -616,7 +617,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,if (FNAME (gpte_changed)(vcpu, gw, top_level)) goto out_gpte_changed; -if (!VALID_PAGE (vcpu->arch.mmu.root_hpa)) +if (!VALID_PAGE (vcpu->arch.mmu->root_hpa)) goto out_gpte_changed; for (shadow_walk_init(&it, vcpu, addr);
@@ -1004,7 +1005,7 @@ static int FNAME(sync_page)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp) gfn =gpte_to_gfn (gpte); pte_access = sp->role.access; pte_access &=FNAME (gpte_access)(gpte); -FNAME (protect_clean_gpte)(&vcpu->arch.mmu, &pte_access, gpte); +FNAME (protect_clean_gpte)(vcpu->arch.mmu, &pte_access, gpte);if (sync_mmio_spte (vcpu, &sp->spt[i], gfn, pte_access, &nr_present))
arch/x86/kvm/svm.c +7 -7
@@ -2918,18 +2918,18 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) {WARN_ON (mmu_is_nested (vcpu));kvm_init_shadow_mmu (vcpu); - vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3; - vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3; - vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr; - vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit; - vcpu->arch.mmu.shadow_root_level =get_npt_level (vcpu); -reset_shadow_zero_bits_mask (vcpu, &vcpu->arch.mmu); + vcpu->arch.mmu->set_cr3 = nested_svm_set_tdp_cr3; + vcpu->arch.mmu->get_cr3 = nested_svm_get_tdp_cr3; + vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; + vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; + vcpu->arch.mmu->shadow_root_level =get_npt_level (vcpu); +reset_shadow_zero_bits_mask (vcpu, vcpu->arch.mmu); vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; }static void nested_svm_uninit_mmu_context (struct kvm_vcpu *vcpu) { - vcpu->arch.walk_mmu = &vcpu->arch.mmu; + vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; }static int nested_svm_check_permissions (struct vcpu_svm *svm)
arch/x86/kvm/vmx.c +8 -7
@@ -5129,9 +5129,10 @@ static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, bool invalidate_gpa) {if (enable_ept && (invalidate_gpa || !enable_vpid)) { -if (!VALID_PAGE (vcpu->arch.mmu.root_hpa)) +if (!VALID_PAGE (vcpu->arch.mmu->root_hpa))return ; -ept_sync_context (construct_eptp (vcpu, vcpu->arch.mmu.root_hpa)); +ept_sync_context (construct_eptp (vcpu, + vcpu->arch.mmu->root_hpa)); }else {vpid_sync_context (vpid); }
@@ -9180,7 +9181,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) }for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) -if (kvm_get_pcid (vcpu, vcpu->arch.mmu.prev_roots[i].cr3) +if (kvm_get_pcid (vcpu, vcpu->arch.mmu->prev_roots[i].cr3) == operand.pcid) roots_to_free |=KVM_MMU_ROOT_PREVIOUS (i);
@@ -11335,9 +11336,9 @@ static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu)VMX_EPT_EXECUTE_ONLY_BIT ,nested_ept_ad_enabled (vcpu), nested_ept_get_cr3(vcpu)); - vcpu->arch.mmu.set_cr3 = vmx_set_cr3; - vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; - vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; + vcpu->arch.mmu->set_cr3 = vmx_set_cr3; + vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3; + vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;return 0 ;
@@ -11345,7 +11346,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.walk_mmu = &vcpu->arch.mmu; + vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; } static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
arch/x86/kvm/x86.c +10 -10
@@ -503,7 +503,7 @@ static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fauif (mmu_is_nested (vcpu) && !fault->nested_page_fault) vcpu->arch.nested_mmu.inject_page_fault (vcpu, fault);else - vcpu->arch.mmu.inject_page_fault (vcpu, fault); + vcpu->arch.mmu->inject_page_fault (vcpu, fault);return fault->nested_page_fault; }
@@ -602,7 +602,7 @@ int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)for (i =0 ; i <ARRAY_SIZE (pdpte); ++i) {if ((pdpte[i] &PT_PRESENT_MASK ) && (pdpte[i] & - vcpu->arch.mmu.guest_rsvd_check.rsvd_bits_mask[0 ][2 ])) { + vcpu->arch.mmu->guest_rsvd_check.rsvd_bits_mask[0 ][2 ])) { ret =0 ; goto out; }
@@ -4803,7 +4803,7 @@ gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,/* NPT walks are always user-walks */ access |=PFERR_USER_MASK ; - t_gpa = vcpu->arch.mmu.gva_to_gpa (vcpu, gpa, access, exception); + t_gpa = vcpu->arch.mmu->gva_to_gpa (vcpu, gpa, access, exception);return t_gpa; }
@@ -5889,7 +5889,7 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,if (WARN_ON_ONCE (is_guest_mode (vcpu)))return false; -if (!vcpu->arch.mmu.direct_map) { +if (!vcpu->arch.mmu->direct_map) {/* * Write permission should be allowed since only * write access need to be emulated.
@@ -5922,7 +5922,7 @@ static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,kvm_release_pfn_clean (pfn);/* The instructions are well-emulated on direct mmu. */ -if (vcpu->arch.mmu.direct_map) { +if (vcpu->arch.mmu->direct_map) {unsigned int indirect_shadow_pages;spin_lock (&vcpu->kvm->mmu_lock);
@@ -5989,7 +5989,7 @@ static bool retry_instruction(struct x86_emulate_ctxt *ctxt, vcpu->arch.last_retry_eip = ctxt->eip; vcpu->arch.last_retry_addr = cr2; -if (!vcpu->arch.mmu.direct_map) +if (!vcpu->arch.mmu->direct_map) gpa =kvm_mmu_gva_to_gpa_write (vcpu, cr2,NULL );kvm_mmu_unprotect_page (vcpu->kvm,gpa_to_gfn (gpa));
@@ -9327,7 +9327,7 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) {int r; -if ((vcpu->arch.mmu.direct_map != work->arch.direct_map) || +if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) || work->wakeup_all)return ;
@@ -9335,11 +9335,11 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)if (unlikely (r))return ; -if (!vcpu->arch.mmu.direct_map && - work->arch.cr3 != vcpu->arch.mmu.get_cr3 (vcpu)) +if (!vcpu->arch.mmu->direct_map && + work->arch.cr3 != vcpu->arch.mmu->get_cr3 (vcpu))return ; - vcpu->arch.mmu.page_fault (vcpu, work->gva,0 , true); + vcpu->arch.mmu->page_fault (vcpu, work->gva,0 , true); }static inline u32 kvm_async_pf_hash_fn (gfn_t gfn)
Signature · 1 line
2.17.1
Re: [PATCH v2 1/9] x86/kvm/mmu: make vcpu->mmu a pointer to the current MMU
Quoted 7 lines · Vitaly Kuznetsov
As a preparation to full MMU split between L1 and L2 make
vcpu->arch.mmua pointer to the currently used mmu. For now, this is alwaysvcpu->arch.root_mmu. No functional change.Signed-off-by Vitaly Kuznetsov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>---
PATCH v2 2/9 x86/kvm/mmu.c: set get_pdptr hook in kvm_init_shadow_ept_mmu()
In reply to Vitaly Kuznetsovkvm_init_shadow_ept_mmu() doesn't set get_pdptr() hook and is this not a problem just because MMU context is already initialized and this hook points to kvm_pdptr_read(). As we're intended to use a dedicated MMU for shadow EPT MMU set this hook explicitly.
--- arch/x86/kvm/mmu.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/mmu.c +2 -0
@@ -4846,6 +4846,8 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->root_level =PT64_ROOT_4LEVEL ; context->direct_map = false; context->base_role.word = root_page_role.word & mmu_base_role_mask.word; + context->get_pdptr = kvm_pdptr_read; +update_permission_bitmask (vcpu, context, true);update_pkru_bitmask (vcpu, context, true);update_last_nonleaf_level (vcpu, context);
Signature · 1 line
2.17.1
Re: [PATCH v2 2/9] x86/kvm/mmu.c: set get_pdptr hook in kvm_init_shadow_ept_mmu()
Quoted 16 lines · Vitaly Kuznetsov
kvm_init_shadow_ept_mmu()doesn't setget_pdptr()hook and is this not a problem just because MMU context is already initialized and this hook points tokvm_pdptr_read(). As we're intended to use a dedicated MMU for shadow EPT MMU set this hook explicitly.Signed-off-by Vitaly Kuznetsov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/mmu.c | 2 ++ 1 file changed, 2 insertions(+)arch/x86/kvm/mmu.c +0
-0
arch/x86/kvm/mmu.c void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, · line 4849
Quoted 4 lines
arch/x86/kvm/mmu.c +1
-0@@ -4846,6 +4846,8 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->root_level =PT64_ROOT_4LEVEL ; context->direct_map = false; context->base_role.word = root_page_role.word & mmu_base_role_mask.word; + context->get_pdptr = kvm_pdptr_read;
Would it make sense to set this in nested_ept_init_mmu_context() along with set_cr3, get_cr3 and inject_page_fault? The other MMU flows set them as a package deal.
Either way...
Quoted 4 lines
arch/x86/kvm/mmu.c +1
-0+update_permission_bitmask (vcpu, context, true);update_pkru_bitmask (vcpu, context, true);update_last_nonleaf_level (vcpu, context);
Re: [PATCH v2 2/9] x86/kvm/mmu.c: set get_pdptr hook in kvm_init_shadow_ept_mmu()
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Vitaly Kuznetsov
kvm_init_shadow_ept_mmu()doesn't setget_pdptr()hook and is this not a problem just because MMU context is already initialized and this hook points tokvm_pdptr_read(). As we're intended to use a dedicated MMU for shadow EPT MMU set this hook explicitly.Signed-off-by Vitaly Kuznetsov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/mmu.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 13 lines
arch/x86/kvm/mmu.c void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, · line 4849
Quoted 9 lines
arch/x86/kvm/mmu.c +1
-0@@ -4846,6 +4846,8 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->root_level =PT64_ROOT_4LEVEL ; context->direct_map = false; context->base_role.word = root_page_role.word & mmu_base_role_mask.word; + context->get_pdptr = kvm_pdptr_read;Would it make sense to set this in
nested_ept_init_mmu_context()along withset_cr3,get_cr3andinject_page_fault? The other MMU flows set them as a package deal.
Well, kvm_init_shadow_ept_mmu() has only one call site and reading the code I was under an impression we set set_cr3/get_cr3/inject_page_fault in vmx.c just to avoid passing all these vmx-specific functions as pointers to kvm_init_shadow_ept_mmu(). With get_pdptr() I was thinking "oh, great, this is not vmx-specific so we can set it in mmu.c" - but I see your point and I'm ready to budge :-)
Quoted 4 lines
Either way...
Reviewed-by Sean Christopherson <[email protected]>
Thanks!
Quoted 5 lines
Quoted 5 lines
arch/x86/kvm/mmu.c +1
-0+update_permission_bitmask (vcpu, context, true);update_pkru_bitmask (vcpu, context, true);update_last_nonleaf_level (vcpu, context);
Signature · 1 line
Vitaly
PATCH v2 3/9 x86/kvm/mmu.c: add kvm_mmu parameter to kvm_mmu_free_roots()
In reply to Vitaly KuznetsovAdd an option to specify which MMU root we want to free. This will be used when nested and non-nested MMUs for L1 are split.
--- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/mmu.c | 9 +++++---- arch/x86/kvm/vmx.c | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
@@ -1327,7 +1327,8 @@ void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);int kvm_mmu_load (struct kvm_vcpu *vcpu);void kvm_mmu_unload (struct kvm_vcpu *vcpu);void kvm_mmu_sync_roots (struct kvm_vcpu *vcpu); -void kvm_mmu_free_roots (struct kvm_vcpu *vcpu,ulong roots_to_free); +void kvm_mmu_free_roots (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +ulong roots_to_free);gpa_t translate_nested_gpa (struct kvm_vcpu *vcpu,gpa_t gpa,u32 access,struct x86_exception *exception); gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
arch/x86/kvm/mmu.c +5 -4
@@ -3464,11 +3464,11 @@ static void mmu_free_root_page(struct kvm *kvm, hpa_t *root_hpa, }/* roots_to_free must be some combination of the KVM_MMU_ROOT_* flags */ -void kvm_mmu_free_roots (struct kvm_vcpu *vcpu,ulong roots_to_free) +void kvm_mmu_free_roots (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +ulong roots_to_free) {int i;LIST_HEAD (invalid_list); -struct kvm_mmu *mmu = vcpu->arch.mmu;bool free_active_root = roots_to_free &KVM_MMU_ROOT_CURRENT ;BUILD_BUG_ON (KVM_MMU_NUM_PREV_ROOTS >=BITS_PER_LONG );
@@ -4184,7 +4184,8 @@ static void __kvm_mmu_new_cr3(struct kvm_vcpu *vcpu, gpa_t new_cr3, bool skip_tlb_flush) {if (!fast_cr3_switch (vcpu, new_cr3, new_role, skip_tlb_flush)) -kvm_mmu_free_roots (vcpu,KVM_MMU_ROOT_CURRENT ); +kvm_mmu_free_roots (vcpu, vcpu->arch.mmu, +KVM_MMU_ROOT_CURRENT ); }void kvm_mmu_new_cr3 (struct kvm_vcpu *vcpu,gpa_t new_cr3,bool skip_tlb_flush)
@@ -4966,7 +4967,7 @@ EXPORT_SYMBOL_GPL(kvm_mmu_load);void kvm_mmu_unload (struct kvm_vcpu *vcpu) { -kvm_mmu_free_roots (vcpu,KVM_MMU_ROOTS_ALL ); +kvm_mmu_free_roots (vcpu, vcpu->arch.mmu,KVM_MMU_ROOTS_ALL );WARN_ON (VALID_PAGE (vcpu->arch.mmu->root_hpa)); }EXPORT_SYMBOL_GPL (kvm_mmu_unload);
arch/x86/kvm/vmx.c +1 -1
@@ -9185,7 +9185,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) == operand.pcid) roots_to_free |=KVM_MMU_ROOT_PREVIOUS (i); -kvm_mmu_free_roots (vcpu, roots_to_free); +kvm_mmu_free_roots (vcpu, vcpu->arch.mmu, roots_to_free);/* * If neither the current cr3 nor any of the prev_roots use the * given PCID, then nothing needs to be done here because a
Signature · 1 line
2.17.1
Re: [PATCH v2 3/9] x86/kvm/mmu.c: add kvm_mmu parameter to kvm_mmu_free_roots()
Quoted 6 lines · Vitaly Kuznetsov
Add an option to specify which MMU root we want to free. This will be used when nested and non-nested MMUs for L1 are split.
Signed-off-by Vitaly Kuznetsov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>---
PATCH v2 4/9 x86/kvm/mmu: introduce guest_mmu
In reply to Vitaly KuznetsovWhen 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.
---
Changes since v1:
- drop now unneded local vmx variable in vmx_free_vcpu_nested [Sean Christopherson]
--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/mmu.c | 15 +++++++++++---- arch/x86/kvm/vmx.c | 27 ++++++++++++++++++--------- 3 files changed, 32 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -539,6 +539,9 @@ struct kvm_vcpu_arch {/* Non-nested MMU for L1 */ struct kvm_mmu root_mmu; +/* L1 MMU when running nested */ +struct kvm_mmu guest_mmu; +/* * Paging state of an L2 guest (used for nested npt) *
arch/x86/kvm/mmu.c +11 -4
@@ -4967,8 +4967,10 @@ EXPORT_SYMBOL_GPL(kvm_mmu_load);void kvm_mmu_unload (struct kvm_vcpu *vcpu) { -kvm_mmu_free_roots (vcpu, vcpu->arch.mmu,KVM_MMU_ROOTS_ALL ); -WARN_ON (VALID_PAGE (vcpu->arch.mmu->root_hpa)); +kvm_mmu_free_roots (vcpu, &vcpu->arch.root_mmu,KVM_MMU_ROOTS_ALL ); +WARN_ON (VALID_PAGE (vcpu->arch.root_mmu.root_hpa)); +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu,KVM_MMU_ROOTS_ALL ); +WARN_ON (VALID_PAGE (vcpu->arch.guest_mmu.root_hpa)); }EXPORT_SYMBOL_GPL (kvm_mmu_unload);
@@ -5407,13 +5409,18 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu = &vcpu->arch.root_mmu; vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; + vcpu->arch.root_mmu.root_hpa =INVALID_PAGE ; vcpu->arch.root_mmu.translate_gpa = translate_gpa; - vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa; -for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) vcpu->arch.root_mmu.prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ; + vcpu->arch.guest_mmu.root_hpa =INVALID_PAGE ; + vcpu->arch.guest_mmu.translate_gpa = translate_gpa; +for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) + vcpu->arch.guest_mmu.prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ; + + vcpu->arch.nested_mmu.translate_gpa = translate_nested_gpa;return alloc_mmu_pages (vcpu); }
arch/x86/kvm/vmx.c +18 -9
@@ -8468,8 +8468,10 @@ static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)* Free whatever needs to be freed from vmx->nested when L1 goes down, or * just stops using VMX. */ -static void free_nested (struct vcpu_vmx *vmx) +static void free_nested (struct kvm_vcpu *vcpu) { +struct vcpu_vmx *vmx =to_vmx (vcpu); +if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)return ;
@@ -8502,6 +8504,8 @@ static void free_nested(struct vcpu_vmx *vmx) vmx->nested.pi_desc =NULL ; } +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu,KVM_MMU_ROOTS_ALL ); +free_loaded_vmcs (&vmx->nested.vmcs02); }
@@ -8510,7 +8514,7 @@ static int handle_vmoff(struct kvm_vcpu *vcpu) {if (!nested_vmx_check_permission (vcpu))return 1 ; -free_nested (to_vmx (vcpu)); +free_nested (vcpu);nested_vmx_succeed (vcpu);return kvm_skip_emulated_instruction (vcpu); }
@@ -8541,6 +8545,8 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)if (vmptr == vmx->nested.current_vmptr)nested_release_vmcs12 (vmx); +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu,KVM_MMU_ROOTS_ALL ); +kvm_vcpu_write_guest (vcpu, vmptr + offsetof(struct vmcs12 , launch_state), &zero,sizeof (zero));
@@ -8924,6 +8930,9 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu) }nested_release_vmcs12 (vmx); + +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu, +KVM_MMU_ROOTS_ALL );/* * Load VMCS12 from guest memory since it is not already * cached.
@@ -10976,12 +10985,10 @@ 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); - -vcpu_load (vcpu); -vmx_switch_vmcs (vcpu, &vmx->vmcs01); -free_nested (vmx); -vcpu_put (vcpu); +vcpu_load (vcpu); +vmx_switch_vmcs (vcpu, &to_vmx (vcpu)->vmcs01); +free_nested (vcpu); +vcpu_put (vcpu); }static void vmx_free_vcpu (struct kvm_vcpu *vcpu)
@@ -11331,6 +11338,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,
@@ -11346,6 +11354,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; }
@@ -13421,7 +13430,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); }/*
Signature · 1 line
2.17.1
Re: [PATCH v2 4/9] x86/kvm/mmu: introduce guest_mmu
Quoted 22 lines · Vitaly Kuznetsov
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 L1kvm_mmu_reset_context()innested_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 themmu_lockfor many vCPUs.On the nested CPUID benchmark, with 16 vCPUs, an
L2->L1->L2vmexit goes from 42k to 26k cycles.Signed-off-by Vitaly Kuznetsov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>---
Changes since v1:
- drop now unneded local vmx variable invmx_free_vcpu_nested[Sean Christopherson]--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/mmu.c | 15 +++++++++++---- arch/x86/kvm/vmx.c | 27 ++++++++++++++++++--------- 3 files changed, 32 insertions(+), 13 deletions(-)
...
Quoted 35 lines
arch/x86/kvm/vmx.c +6
-2@@ -8468,8 +8468,10 @@ static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)* Free whatever needs to be freed from vmx->nested when L1 goes down, or * just stops using VMX. */ -static void free_nested (struct vcpu_vmx *vmx) +static void free_nested (struct kvm_vcpu *vcpu) { +struct vcpu_vmx *vmx =to_vmx (vcpu); +if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)return ;@@ -8502,6 +8504,8 @@ static void free_nested(struct vcpu_vmx *vmx) vmx->nested.pi_desc =NULL ; } +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu,KVM_MMU_ROOTS_ALL ); +free_loaded_vmcs (&vmx->nested.vmcs02); }@@ -8510,7 +8514,7 @@ static int handle_vmoff(struct kvm_vcpu *vcpu) {if (!nested_vmx_check_permission (vcpu))return 1 ; -free_nested (to_vmx (vcpu)); +free_nested (vcpu);nested_vmx_succeed (vcpu);return kvm_skip_emulated_instruction (vcpu); }
arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 8548
Quoted 4 lines
arch/x86/kvm/vmx.c +1
-0@@ -8541,6 +8545,8 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)if (vmptr == vmx->nested.current_vmptr)nested_release_vmcs12 (vmx); +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu,KVM_MMU_ROOTS_ALL );
Shouldn't we only free guest_mmu if VMCLEAR is targeting current_vmptr? Assuming that's the case, we could put the call to kvm_mmu_free_roots() in nested_release_vmcs12() instead of calling it from handle_vmclear() and handle_vmptrld().
Quoted 59 lines
arch/x86/kvm/vmx.c +11
-7+kvm_vcpu_write_guest (vcpu, vmptr + offsetof(struct vmcs12 , launch_state), &zero,sizeof (zero));@@ -8924,6 +8930,9 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu) }nested_release_vmcs12 (vmx); + +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu, +KVM_MMU_ROOTS_ALL );/* * Load VMCS12 from guest memory since it is not already * cached. @@ -10976,12 +10985,10 @@ 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); - -vcpu_load (vcpu); -vmx_switch_vmcs (vcpu, &vmx->vmcs01); -free_nested (vmx); -vcpu_put (vcpu); +vcpu_load (vcpu); +vmx_switch_vmcs (vcpu, &to_vmx (vcpu)->vmcs01); +free_nested (vcpu); +vcpu_put (vcpu); }static void vmx_free_vcpu (struct kvm_vcpu *vcpu)@@ -11331,6 +11338,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,@@ -11346,6 +11354,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; }@@ -13421,7 +13430,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); }/* Signature · 1 line
2.17.1
Re: [PATCH v2 4/9] x86/kvm/mmu: introduce guest_mmu
Quoted 64 lines · Sean Christopherson
Quoted 22 lines · Vitaly Kuznetsov
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 L1kvm_mmu_reset_context()innested_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 themmu_lockfor many vCPUs.On the nested CPUID benchmark, with 16 vCPUs, an
L2->L1->L2vmexit goes from 42k to 26k cycles.Signed-off-by Vitaly Kuznetsov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>---
Changes since v1:
- drop now unneded local vmx variable invmx_free_vcpu_nested[Sean Christopherson]--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/mmu.c | 15 +++++++++++---- arch/x86/kvm/vmx.c | 27 ++++++++++++++++++--------- 3 files changed, 32 insertions(+), 13 deletions(-)...
Quoted 38 lines
arch/x86/kvm/vmx.c +6
-2@@ -8468,8 +8468,10 @@ static inline void nested_release_vmcs12(struct vcpu_vmx *vmx)* Free whatever needs to be freed from vmx->nested when L1 goes down, or * just stops using VMX. */ -static void free_nested (struct vcpu_vmx *vmx) +static void free_nested (struct kvm_vcpu *vcpu) { +struct vcpu_vmx *vmx =to_vmx (vcpu); +if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon)return ;@@ -8502,6 +8504,8 @@ static void free_nested(struct vcpu_vmx *vmx) vmx->nested.pi_desc =NULL ; } +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu,KVM_MMU_ROOTS_ALL ); +free_loaded_vmcs (&vmx->nested.vmcs02); }@@ -8510,7 +8514,7 @@ static int handle_vmoff(struct kvm_vcpu *vcpu) {if (!nested_vmx_check_permission (vcpu))return 1 ; -free_nested (to_vmx (vcpu)); +free_nested (vcpu);nested_vmx_succeed (vcpu);return kvm_skip_emulated_instruction (vcpu); }@@ -8541,6 +8545,8 @@ static int handle_vmclear(struct kvm_vcpu *vcpu)if (vmptr == vmx->nested.current_vmptr)nested_release_vmcs12 (vmx);
Quoted 4 lines
arch/x86/kvm/vmx.c static int handle_vmclear(struct kvm_vcpu *vcpu) · line 8548
Quoted 1 line
arch/x86/kvm/vmx.c +1
-0+kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu,KVM_MMU_ROOTS_ALL );Shouldn't we only free
guest_mmuif VMCLEAR is targetingcurrent_vmptr?
Right you are, this was definitely overlooked, no need for kvm_mmu_free_roots() when we VMCLEAR some-other-vmptr.
Quoted 3 lines
Assuming that's the case, we could put the call to
kvm_mmu_free_roots()innested_release_vmcs12()instead of calling it fromhandle_vmclear()andhandle_vmptrld().
Yep, will do in v3.
Quoted 60 lines
Quoted 60 lines
arch/x86/kvm/vmx.c +11
-7+kvm_vcpu_write_guest (vcpu, vmptr + offsetof(struct vmcs12 , launch_state), &zero,sizeof (zero));@@ -8924,6 +8930,9 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu) }nested_release_vmcs12 (vmx); + +kvm_mmu_free_roots (vcpu, &vcpu->arch.guest_mmu, +KVM_MMU_ROOTS_ALL );/* * Load VMCS12 from guest memory since it is not already * cached. @@ -10976,12 +10985,10 @@ 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); - -vcpu_load (vcpu); -vmx_switch_vmcs (vcpu, &vmx->vmcs01); -free_nested (vmx); -vcpu_put (vcpu); +vcpu_load (vcpu); +vmx_switch_vmcs (vcpu, &to_vmx (vcpu)->vmcs01); +free_nested (vcpu); +vcpu_put (vcpu); }static void vmx_free_vcpu (struct kvm_vcpu *vcpu)@@ -11331,6 +11338,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,@@ -11346,6 +11354,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; }@@ -13421,7 +13430,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); }/* Signature · 1 line
2.17.1
Signature · 1 line
Vitaly
PATCH v2 5/9 x86/kvm/mmu: get rid of redundant kvm_mmu_setup()
In reply to Vitaly KuznetsovFrom: Paolo Bonzini <[email protected]>
Just inline the contents into the sole caller, kvm_init_mmu is now public.
--- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/mmu.c | 12 ------------ arch/x86/kvm/x86.c | 2 +- 3 files changed, 1 insertion(+), 14 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
@@ -1176,7 +1176,6 @@ void kvm_mmu_module_exit(void);void kvm_mmu_destroy (struct kvm_vcpu *vcpu);int kvm_mmu_create (struct kvm_vcpu *vcpu); -void kvm_mmu_setup (struct kvm_vcpu *vcpu);void kvm_mmu_init_vm (struct kvm *kvm);void kvm_mmu_uninit_vm (struct kvm *kvm); void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
arch/x86/kvm/mmu.c +0 -12
@@ -5424,18 +5424,6 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu)return alloc_mmu_pages (vcpu); } -void kvm_mmu_setup (struct kvm_vcpu *vcpu) -{ -MMU_WARN_ON (VALID_PAGE (vcpu->arch.mmu->root_hpa)); - -/* -* kvm_mmu_setup() is called only on vCPU initialization. -* Therefore, no need to reset mmu roots as they are not yet -* initialized. -*/ -kvm_init_mmu (vcpu, false); -} -static void kvm_mmu_invalidate_zap_pages_in_memslot (struct kvm *kvm,struct kvm_memory_slot *slot,struct kvm_page_track_notifier_node *node)
arch/x86/kvm/x86.c +1 -1
@@ -8478,7 +8478,7 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)kvm_vcpu_mtrr_init (vcpu);vcpu_load (vcpu);kvm_vcpu_reset (vcpu, false); -kvm_mmu_setup (vcpu); +kvm_init_mmu (vcpu, false);vcpu_put (vcpu);return 0 ; }
Signature · 1 line
2.17.1
Re: [PATCH v2 5/9] x86/kvm/mmu: get rid of redundant kvm_mmu_setup()
Quoted 8 lines · Vitaly Kuznetsov
From: Paolo Bonzini <[email protected]>
Just inline the contents into the sole caller,
kvm_init_mmuis now public.Suggested-by Vitaly Kuznetsov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>---
PATCH v2 6/9 x86/kvm/mmu: make space for source data caching in struct kvm_mmu
In reply to Vitaly KuznetsovIn preparation to MMU reconfiguration avoidance we need a space to cache source data. As this partially intersects with kvm_mmu_page_role, create 64bit sized union kvm_mmu_role holding both base and extended data. No functional change.
---
Changes since v1:
- Rename:
kvm_mmu_scache -> kvm_mmu_extended_role
mmu_role.scache -> mmu_role.ext
mmu_role.base_role -> mmu_role.base
[Sean Christopherson]
- Add BUILD_BUG_ONs checking unions sizes.
[Sean Christopherson]
- Use explicit u32 for kvm_mmu_page_role/kvm_mmu_extended_role.word and u64 for kvm_mmu_role.as_u64
--- arch/x86/include/asm/kvm_host.h | 16 ++++++++++++++-- arch/x86/kvm/mmu.c | 29 ++++++++++++++++++++++------- arch/x86/kvm/vmx.c | 2 +- 3 files changed, 37 insertions(+), 10 deletions(-)
arch/x86/include/asm/kvm_host.h +14 -2
@@ -247,7 +247,7 @@ struct kvm_mmu_memory_cache {* @nxe, @cr0_wp, @smep_andnot_wp and @smap_andnot_wp. */ union kvm_mmu_page_role { - unsigned word; +u32 word;struct {unsigned level:4 ;unsigned cr4_pae:1 ;
@@ -273,6 +273,18 @@ union kvm_mmu_page_role { }; }; +union kvm_mmu_extended_role { +u32 word; +}; + +union kvm_mmu_role { +u64 as_u64; +struct { +union kvm_mmu_page_role base; +union kvm_mmu_extended_role ext; + }; +}; +struct kvm_rmap_head {unsigned long val; };
@@ -360,7 +372,7 @@ struct kvm_mmu {void (*update_pte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,u64 *spte,const void *pte);hpa_t root_hpa; -union kvm_mmu_page_role base_role; +union kvm_mmu_role mmu_role;u8 root_level;u8 shadow_root_level;u8 ept_ad;
arch/x86/kvm/mmu.c +22 -7
@@ -2359,7 +2359,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,int collisions =0 ;LIST_HEAD (invalid_list); - role = vcpu->arch.mmu->base_role; + role = vcpu->arch.mmu->mmu_role.base; role.level = level; role.direct = direct;if (role.direct)
@@ -4407,7 +4407,8 @@ static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,void reset_shadow_zero_bits_mask (struct kvm_vcpu *vcpu,struct kvm_mmu *context) { -bool uses_nx = context->nx || context->base_role.smep_andnot_wp; +bool uses_nx = context->nx || + context->mmu_role.base.smep_andnot_wp;struct rsvd_bits_validate *shadow_zero_check;int i;
@@ -4726,7 +4727,7 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) {struct kvm_mmu *context = vcpu->arch.mmu; - context->base_role.word = mmu_base_role_mask.word & + context->mmu_role.base.word = mmu_base_role_mask.word &kvm_calc_tdp_mmu_root_page_role (vcpu).word; context->page_fault = tdp_page_fault; context->sync_page = nonpaging_sync_page;
@@ -4807,7 +4808,7 @@ void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)else paging32_init_context (vcpu, context); - context->base_role.word = mmu_base_role_mask.word & + context->mmu_role.base.word = mmu_base_role_mask.word &kvm_calc_shadow_mmu_root_page_role (vcpu).word;reset_shadow_zero_bits_mask (vcpu, context); }
@@ -4816,7 +4817,7 @@ EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);static union kvm_mmu_page_role kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty) { -union kvm_mmu_page_role role = vcpu->arch.mmu->base_role; +union kvm_mmu_page_role role = vcpu->arch.mmu->mmu_role.base; role.level =PT64_ROOT_4LEVEL ; role.direct = false;
@@ -4846,7 +4847,8 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->update_pte = ept_update_pte; context->root_level =PT64_ROOT_4LEVEL ; context->direct_map = false; - context->base_role.word = root_page_role.word & mmu_base_role_mask.word; + context->mmu_role.base.word = + root_page_role.word & mmu_base_role_mask.word; context->get_pdptr = kvm_pdptr_read;update_permission_bitmask (vcpu, context, true);
@@ -5161,10 +5163,13 @@ static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, local_flush = true;while (npte--) { +unsigned int base_role = + vcpu->arch.mmu->mmu_role.base.word; + entry = *spte;mmu_page_zap_pte (vcpu->kvm, sp, spte);if (gentry && - !((sp->role.word ^ vcpu->arch.mmu->base_role.word) + !((sp->role.word ^ base_role) & mmu_base_role_mask.word) &&rmap_can_add (vcpu))mmu_pte_write_new_pte (vcpu, sp, spte, &gentry);if (need_remote_flush (entry, *spte))
@@ -5861,6 +5866,16 @@ int kvm_mmu_module_init(void) {int ret = -ENOMEM ; +/* +* MMU roles use union aliasing which is, generally speaking, an +* undefined behavior. However, we supposedly know how compilers behave +* and the current status quo is unlikely to change. Guardians below are +* supposed to let us know if the assumption becomes false. +*/ +BUILD_BUG_ON (sizeof (union kvm_mmu_page_role ) !=sizeof (u32)); +BUILD_BUG_ON (sizeof (union kvm_mmu_extended_role ) !=sizeof (u32)); +BUILD_BUG_ON (sizeof (union kvm_mmu_role ) !=sizeof (u64)); +kvm_mmu_reset_all_pte_masks (); pte_list_desc_cache = kmem_cache_create("pte_list_desc",
arch/x86/kvm/vmx.c +1 -1
@@ -9321,7 +9321,7 @@ static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,kvm_mmu_unload (vcpu); mmu->ept_ad = accessed_dirty; - mmu->base_role.ad_disabled = !accessed_dirty; + mmu->mmu_role.base.ad_disabled = !accessed_dirty; vmcs12->ept_pointer = address;/* * TODO: Check what's the correct approach in case
Signature · 1 line
2.17.1
Re: [PATCH v2 6/9] x86/kvm/mmu: make space for source data caching in struct kvm_mmu
Quoted 7 lines · Vitaly Kuznetsov
In preparation to MMU reconfiguration avoidance we need a space to cache source data. As this partially intersects with
kvm_mmu_page_role, create 64bit sized unionkvm_mmu_roleholding both base and extended data. No functional change.Signed-off-by Vitaly Kuznetsov <[email protected]>---
One nit below, other than that...
Quoted 61 lines
arch/x86/kvm/mmu.c +8
-6@@ -2359,7 +2359,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,int collisions =0 ;LIST_HEAD (invalid_list); - role = vcpu->arch.mmu->base_role; + role = vcpu->arch.mmu->mmu_role.base; role.level = level; role.direct = direct;if (role.direct)@@ -4407,7 +4407,8 @@ static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,void reset_shadow_zero_bits_mask (struct kvm_vcpu *vcpu,struct kvm_mmu *context) { -bool uses_nx = context->nx || context->base_role.smep_andnot_wp; +bool uses_nx = context->nx || + context->mmu_role.base.smep_andnot_wp;struct rsvd_bits_validate *shadow_zero_check;int i;@@ -4726,7 +4727,7 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) {struct kvm_mmu *context = vcpu->arch.mmu; - context->base_role.word = mmu_base_role_mask.word & + context->mmu_role.base.word = mmu_base_role_mask.word &kvm_calc_tdp_mmu_root_page_role (vcpu).word; context->page_fault = tdp_page_fault; context->sync_page = nonpaging_sync_page;@@ -4807,7 +4808,7 @@ void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)else paging32_init_context (vcpu, context); - context->base_role.word = mmu_base_role_mask.word & + context->mmu_role.base.word = mmu_base_role_mask.word &kvm_calc_shadow_mmu_root_page_role (vcpu).word;reset_shadow_zero_bits_mask (vcpu, context); }@@ -4816,7 +4817,7 @@ EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);static union kvm_mmu_page_role kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty) { -union kvm_mmu_page_role role = vcpu->arch.mmu->base_role; +union kvm_mmu_page_role role = vcpu->arch.mmu->mmu_role.base; role.level =PT64_ROOT_4LEVEL ; role.direct = false;@@ -4846,7 +4847,8 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->update_pte = ept_update_pte; context->root_level =PT64_ROOT_4LEVEL ; context->direct_map = false; - context->base_role.word = root_page_role.word & mmu_base_role_mask.word; + context->mmu_role.base.word = + root_page_role.word & mmu_base_role_mask.word; context->get_pdptr = kvm_pdptr_read;update_permission_bitmask (vcpu, context, true);
arch/x86/kvm/mmu.c static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, · line 5166
Quoted 4 lines
arch/x86/kvm/mmu.c +1
-0@@ -5161,10 +5163,13 @@ static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, local_flush = true;while (npte--) { + unsigned int base_role =
Nit: should this be a u32 to match mmu_role.base.word?
Quoted 43 lines
arch/x86/kvm/mmu.c +13
-1+ vcpu->arch.mmu->mmu_role.base.word; + entry = *spte;mmu_page_zap_pte (vcpu->kvm, sp, spte);if (gentry && - !((sp->role.word ^ vcpu->arch.mmu->base_role.word) + !((sp->role.word ^ base_role) & mmu_base_role_mask.word) &&rmap_can_add (vcpu))mmu_pte_write_new_pte (vcpu, sp, spte, &gentry);if (need_remote_flush (entry, *spte))@@ -5861,6 +5866,16 @@ int kvm_mmu_module_init(void) {int ret = -ENOMEM ; +/* +* MMU roles use union aliasing which is, generally speaking, an +* undefined behavior. However, we supposedly know how compilers behave +* and the current status quo is unlikely to change. Guardians below are +* supposed to let us know if the assumption becomes false. +*/ +BUILD_BUG_ON (sizeof (union kvm_mmu_page_role ) !=sizeof (u32)); +BUILD_BUG_ON (sizeof (union kvm_mmu_extended_role ) !=sizeof (u32)); +BUILD_BUG_ON (sizeof (union kvm_mmu_role ) !=sizeof (u64)); +kvm_mmu_reset_all_pte_masks (); pte_list_desc_cache = kmem_cache_create("pte_list_desc",arch/x86/kvm/vmx.c +1
-1@@ -9321,7 +9321,7 @@ static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,kvm_mmu_unload (vcpu); mmu->ept_ad = accessed_dirty; - mmu->base_role.ad_disabled = !accessed_dirty; + mmu->mmu_role.base.ad_disabled = !accessed_dirty; vmcs12->ept_pointer = address;/* * TODO: Check what's the correct approach in case Signature · 1 line
2.17.1
Re: [PATCH v2 6/9] x86/kvm/mmu: make space for source data caching in struct kvm_mmu
Quoted 72 lines · Sean Christopherson
Quoted 7 lines · Vitaly Kuznetsov
In preparation to MMU reconfiguration avoidance we need a space to cache source data. As this partially intersects with
kvm_mmu_page_role, create 64bit sized unionkvm_mmu_roleholding both base and extended data. No functional change.Signed-off-by Vitaly Kuznetsov <[email protected]>---
One nit below, other than that...
Reviewed-by Sean Christopherson <[email protected]>Quoted 59 lines
arch/x86/kvm/mmu.c +8
-6@@ -2359,7 +2359,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu,int collisions =0 ;LIST_HEAD (invalid_list); - role = vcpu->arch.mmu->base_role; + role = vcpu->arch.mmu->mmu_role.base; role.level = level; role.direct = direct;if (role.direct)@@ -4407,7 +4407,8 @@ static void reset_rsvds_bits_mask_ept(struct kvm_vcpu *vcpu,void reset_shadow_zero_bits_mask (struct kvm_vcpu *vcpu,struct kvm_mmu *context) { -bool uses_nx = context->nx || context->base_role.smep_andnot_wp; +bool uses_nx = context->nx || + context->mmu_role.base.smep_andnot_wp;struct rsvd_bits_validate *shadow_zero_check;int i;@@ -4726,7 +4727,7 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) {struct kvm_mmu *context = vcpu->arch.mmu; - context->base_role.word = mmu_base_role_mask.word & + context->mmu_role.base.word = mmu_base_role_mask.word &kvm_calc_tdp_mmu_root_page_role (vcpu).word; context->page_fault = tdp_page_fault; context->sync_page = nonpaging_sync_page;@@ -4807,7 +4808,7 @@ void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)else paging32_init_context (vcpu, context); - context->base_role.word = mmu_base_role_mask.word & + context->mmu_role.base.word = mmu_base_role_mask.word &kvm_calc_shadow_mmu_root_page_role (vcpu).word;reset_shadow_zero_bits_mask (vcpu, context); }@@ -4816,7 +4817,7 @@ EXPORT_SYMBOL_GPL(kvm_init_shadow_mmu);static union kvm_mmu_page_role kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty) { -union kvm_mmu_page_role role = vcpu->arch.mmu->base_role; +union kvm_mmu_page_role role = vcpu->arch.mmu->mmu_role.base; role.level =PT64_ROOT_4LEVEL ; role.direct = false;@@ -4846,7 +4847,8 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->update_pte = ept_update_pte; context->root_level =PT64_ROOT_4LEVEL ; context->direct_map = false; - context->base_role.word = root_page_role.word & mmu_base_role_mask.word; + context->mmu_role.base.word = + root_page_role.word & mmu_base_role_mask.word; context->get_pdptr = kvm_pdptr_read;
Quoted 9 lines
arch/x86/kvm/mmu.c static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, · line 5166
Quoted 6 lines
arch/x86/kvm/mmu.c +1
-0update_permission_bitmask (vcpu, context, true);@@ -5161,10 +5163,13 @@ static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, local_flush = true;while (npte--) { + unsigned int base_role =Nit: should this be a u32 to match mmu_role.base.word?
Of course, will do in v3. Thanks!
Quoted 43 lines
Quoted 43 lines
arch/x86/kvm/mmu.c +13
-1+ vcpu->arch.mmu->mmu_role.base.word; + entry = *spte;mmu_page_zap_pte (vcpu->kvm, sp, spte);if (gentry && - !((sp->role.word ^ vcpu->arch.mmu->base_role.word) + !((sp->role.word ^ base_role) & mmu_base_role_mask.word) &&rmap_can_add (vcpu))mmu_pte_write_new_pte (vcpu, sp, spte, &gentry);if (need_remote_flush (entry, *spte))@@ -5861,6 +5866,16 @@ int kvm_mmu_module_init(void) {int ret = -ENOMEM ; +/* +* MMU roles use union aliasing which is, generally speaking, an +* undefined behavior. However, we supposedly know how compilers behave +* and the current status quo is unlikely to change. Guardians below are +* supposed to let us know if the assumption becomes false. +*/ +BUILD_BUG_ON (sizeof (union kvm_mmu_page_role ) !=sizeof (u32)); +BUILD_BUG_ON (sizeof (union kvm_mmu_extended_role ) !=sizeof (u32)); +BUILD_BUG_ON (sizeof (union kvm_mmu_role ) !=sizeof (u64)); +kvm_mmu_reset_all_pte_masks (); pte_list_desc_cache = kmem_cache_create("pte_list_desc",arch/x86/kvm/vmx.c +1
-1@@ -9321,7 +9321,7 @@ static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu,kvm_mmu_unload (vcpu); mmu->ept_ad = accessed_dirty; - mmu->base_role.ad_disabled = !accessed_dirty; + mmu->mmu_role.base.ad_disabled = !accessed_dirty; vmcs12->ept_pointer = address;/* * TODO: Check what's the correct approach in case Signature · 1 line
2.17.1
Signature · 1 line
Vitaly
PATCH v2 7/9 x86/kvm/nVMX: introduce source data cache for kvm_init_shadow_ept_mmu()
In reply to Vitaly KuznetsovMMU re-initialization is expensive, in particular, update_permission_bitmask() and update_pkru_bitmask() are.
Cache the data used to setup shadow EPT MMU and avoid full re-init when it is unchanged.
--- arch/x86/include/asm/kvm_host.h | 14 +++++++++ arch/x86/kvm/mmu.c | 51 ++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +14 -0
@@ -274,7 +274,21 @@ union kvm_mmu_page_role { };union kvm_mmu_extended_role { +/* +* This structure complements kvm_mmu_page_role caching everything needed for +* MMU configuration. If nothing in both these structures changed, MMU +* re-configuration can be skipped. @valid bit is set on first usage so we don't +* treat all-zero structure as valid data. +*/ u32 word; +struct { +unsigned int valid:1 ; +unsigned int execonly:1 ; +unsigned int cr4_pse:1 ; +unsigned int cr4_pke:1 ; +unsigned int cr4_smap:1 ; +unsigned int cr4_smep:1 ; + }; };union kvm_mmu_role {
arch/x86/kvm/mmu.c +38 -13
78 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v2 7/9] x86/kvm/nVMX: introduce source data cache for kvm_init_shadow_ept_mmu()
Quoted 69 lines · Vitaly Kuznetsov
MMU re-initialization is expensive, in particular,
update_permission_bitmask()andupdate_pkru_bitmask()are.Cache the data used to setup shadow EPT MMU and avoid full re-init when it is unchanged.
Signed-off-by Vitaly Kuznetsov <[email protected]>--- arch/x86/include/asm/kvm_host.h | 14 +++++++++ arch/x86/kvm/mmu.c | 51 ++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 13 deletions(-)arch/x86/include/asm/kvm_host.h +14
-0@@ -274,7 +274,21 @@ union kvm_mmu_page_role { };union kvm_mmu_extended_role { +/* +* This structure complements kvm_mmu_page_role caching everything needed for +* MMU configuration. If nothing in both these structures changed, MMU +* re-configuration can be skipped. @valid bit is set on first usage so we don't +* treat all-zero structure as valid data. +*/ u32 word; +struct { +unsigned int valid:1 ; +unsigned int execonly:1 ; +unsigned int cr4_pse:1 ; +unsigned int cr4_pke:1 ; +unsigned int cr4_smap:1 ; +unsigned int cr4_smep:1 ; + }; };union kvm_mmu_role {arch/x86/kvm/mmu.c +18
-0@@ -4708,6 +4708,24 @@ static void paging32E_init_context(struct kvm_vcpu *vcpu,paging64_init_context_common (vcpu, context,PT32E_ROOT_LEVEL ); } +static union kvm_mmu_role +kvm_calc_mmu_role_common (struct kvm_vcpu *vcpu) +{ +union kvm_mmu_role role = {0 }; + + role.base.access =ACC_ALL ; + role.base.cr0_wp =is_write_protection (vcpu); + + role.ext.cr4_smep =kvm_read_cr4_bits (vcpu,X86_CR4_SMEP ) !=0 ; + role.ext.cr4_smap =kvm_read_cr4_bits (vcpu,X86_CR4_SMAP ) !=0 ; + role.ext.cr4_pse = !!is_pse (vcpu); + role.ext.cr4_pke =kvm_read_cr4_bits (vcpu,X86_CR4_PKE ) !=0 ; + + role.ext.valid =1 ; + +return role; +} +static union kvm_mmu_page_role kvm_calc_tdp_mmu_root_page_role (struct kvm_vcpu *vcpu) {@@ -4814,16 +4832,18 @@ void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu) }
arch/x86/kvm/mmu.c void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu) · line 4839
Quoted 10 lines
arch/x86/kvm/mmu.c +4
-3EXPORT_SYMBOL_GPL (kvm_init_shadow_mmu); -static union kvm_mmu_page_role -kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty) +static union kvm_mmu_role +kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty, +bool execonly) { -union kvm_mmu_page_role role = vcpu->arch.mmu->mmu_role.base; +union kvm_mmu_role role =kvm_calc_mmu_role_common (vcpu);
kvm_calc_mmu_role_common() doesn't preserve the current mmu_role.base and kvm_calc_mmu_role_common() doesn't capture all base fields. Won't @role will be incorrect for base fields that aren't set below, e.g. cr4_pae, smep_andnot_wp, smap_andnot_wp, etc...
Quoted 48 lines
arch/x86/kvm/mmu.c +16
-10- role.level =PT64_ROOT_4LEVEL ; - role.direct = false; - role.ad_disabled = !accessed_dirty; - role.guest_mode = true; - role.access =ACC_ALL ; + role.base.level =PT64_ROOT_4LEVEL ; + role.base.direct = false; + role.base.ad_disabled = !accessed_dirty; + role.base.guest_mode = true; + + role.ext.execonly = execonly;return role; }@@ -4832,10 +4852,16 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, bool accessed_dirty, gpa_t new_eptp) {struct kvm_mmu *context = vcpu->arch.mmu; -union kvm_mmu_page_role root_page_role = -kvm_calc_shadow_ept_root_page_role (vcpu, accessed_dirty); +union kvm_mmu_role new_role = +kvm_calc_shadow_ept_root_page_role (vcpu, accessed_dirty, + execonly); + +__kvm_mmu_new_cr3 (vcpu, new_eptp, new_role.base, false); + + new_role.base.word &= mmu_base_role_mask.word; +if (new_role.as_u64 == context->mmu_role.as_u64) +return ; -__kvm_mmu_new_cr3 (vcpu, new_eptp, root_page_role, false); context->shadow_root_level =PT64_ROOT_4LEVEL ; context->nx = true;@@ -4847,8 +4873,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->update_pte = ept_update_pte; context->root_level =PT64_ROOT_4LEVEL ; context->direct_map = false; - context->mmu_role.base.word = - root_page_role.word & mmu_base_role_mask.word; + context->mmu_role.as_u64 = new_role.as_u64; context->get_pdptr = kvm_pdptr_read;update_permission_bitmask (vcpu, context, true);Signature · 1 line
2.17.1
Re: [PATCH v2 7/9] x86/kvm/nVMX: introduce source data cache for kvm_init_shadow_ept_mmu()
Quoted 72 lines · Sean Christopherson
Quoted 71 lines · Vitaly Kuznetsov
MMU re-initialization is expensive, in particular,
update_permission_bitmask()andupdate_pkru_bitmask()are.Cache the data used to setup shadow EPT MMU and avoid full re-init when it is unchanged.
Signed-off-by Vitaly Kuznetsov <[email protected]>--- arch/x86/include/asm/kvm_host.h | 14 +++++++++ arch/x86/kvm/mmu.c | 51 ++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 13 deletions(-)arch/x86/include/asm/kvm_host.h +14
-0@@ -274,7 +274,21 @@ union kvm_mmu_page_role { };union kvm_mmu_extended_role { +/* +* This structure complements kvm_mmu_page_role caching everything needed for +* MMU configuration. If nothing in both these structures changed, MMU +* re-configuration can be skipped. @valid bit is set on first usage so we don't +* treat all-zero structure as valid data. +*/ u32 word; +struct { +unsigned int valid:1 ; +unsigned int execonly:1 ; +unsigned int cr4_pse:1 ; +unsigned int cr4_pke:1 ; +unsigned int cr4_smap:1 ; +unsigned int cr4_smep:1 ; + }; };union kvm_mmu_role {arch/x86/kvm/mmu.c +18
-0@@ -4708,6 +4708,24 @@ static void paging32E_init_context(struct kvm_vcpu *vcpu,paging64_init_context_common (vcpu, context,PT32E_ROOT_LEVEL ); } +static union kvm_mmu_role +kvm_calc_mmu_role_common (struct kvm_vcpu *vcpu) +{ +union kvm_mmu_role role = {0 }; + + role.base.access =ACC_ALL ; + role.base.cr0_wp =is_write_protection (vcpu); + + role.ext.cr4_smep =kvm_read_cr4_bits (vcpu,X86_CR4_SMEP ) !=0 ; + role.ext.cr4_smap =kvm_read_cr4_bits (vcpu,X86_CR4_SMAP ) !=0 ; + role.ext.cr4_pse = !!is_pse (vcpu); + role.ext.cr4_pke =kvm_read_cr4_bits (vcpu,X86_CR4_PKE ) !=0 ; + + role.ext.valid =1 ; + +return role; +} +static union kvm_mmu_page_role kvm_calc_tdp_mmu_root_page_role (struct kvm_vcpu *vcpu) {@@ -4814,16 +4832,18 @@ void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_GPL (kvm_init_shadow_mmu);
Quoted 13 lines
arch/x86/kvm/mmu.c void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu) · line 4839
Quoted 8 lines
arch/x86/kvm/mmu.c +4
-3-static union kvm_mmu_page_role -kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty) +static union kvm_mmu_role +kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty, +bool execonly) { -union kvm_mmu_page_role role = vcpu->arch.mmu->mmu_role.base; +union kvm_mmu_role role =kvm_calc_mmu_role_common (vcpu);
kvm_calc_mmu_role_common()doesn't preserve the current mmu_role.base andkvm_calc_mmu_role_common()doesn't capture all base fields. Won't @role will be incorrect for base fields that aren't set below, e.g.cr4_pae,smep_andnot_wp,smap_andnot_wp, etc...
Oh, I see what you mean. Actually, PATCH8 of this series adds some of this stuff but smep_andnot_wp and smap_andnot_wp are still not set. I think I'll enhance kvm_calc_mmu_role_common() and move some stuff from PATCH8 to this one.
(The fact that @role is currently not fully re-initialized here is very unobvious so I would definitely prefer to explicitly initialize everything over inheriting something from previously initialized role).
Thanks!
Quoted 49 lines
Quoted 49 lines
arch/x86/kvm/mmu.c +16
-10- role.level =PT64_ROOT_4LEVEL ; - role.direct = false; - role.ad_disabled = !accessed_dirty; - role.guest_mode = true; - role.access =ACC_ALL ; + role.base.level =PT64_ROOT_4LEVEL ; + role.base.direct = false; + role.base.ad_disabled = !accessed_dirty; + role.base.guest_mode = true; + + role.ext.execonly = execonly;return role; }@@ -4832,10 +4852,16 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, bool accessed_dirty, gpa_t new_eptp) {struct kvm_mmu *context = vcpu->arch.mmu; -union kvm_mmu_page_role root_page_role = -kvm_calc_shadow_ept_root_page_role (vcpu, accessed_dirty); +union kvm_mmu_role new_role = +kvm_calc_shadow_ept_root_page_role (vcpu, accessed_dirty, + execonly); + +__kvm_mmu_new_cr3 (vcpu, new_eptp, new_role.base, false); + + new_role.base.word &= mmu_base_role_mask.word; +if (new_role.as_u64 == context->mmu_role.as_u64) +return ; -__kvm_mmu_new_cr3 (vcpu, new_eptp, root_page_role, false); context->shadow_root_level =PT64_ROOT_4LEVEL ; context->nx = true;@@ -4847,8 +4873,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->update_pte = ept_update_pte; context->root_level =PT64_ROOT_4LEVEL ; context->direct_map = false; - context->mmu_role.base.word = - root_page_role.word & mmu_base_role_mask.word; + context->mmu_role.as_u64 = new_role.as_u64; context->get_pdptr = kvm_pdptr_read;update_permission_bitmask (vcpu, context, true);Signature · 1 line
2.17.1
Signature · 1 line
Vitaly
Re: [PATCH v2 7/9] x86/kvm/nVMX: introduce source data cache for kvm_init_shadow_ept_mmu()
Quoted 3 lines · Vitaly Kuznetsov
Quoted 1 line · Sean Christopherson
On Tue, Sep 25, 2018 at 07:58:42PM +0200, Vitaly Kuznetsov wrote:
...
Quoted 15 lines
Quoted 14 lines
arch/x86/kvm/mmu.c void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu) · line 4839
Quoted 9 lines
arch/x86/kvm/mmu.c +4
-3-static union kvm_mmu_page_role -kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty) +static union kvm_mmu_role +kvm_calc_shadow_ept_root_page_role (struct kvm_vcpu *vcpu,bool accessed_dirty, +bool execonly) { -union kvm_mmu_page_role role = vcpu->arch.mmu->mmu_role.base; +union kvm_mmu_role role =kvm_calc_mmu_role_common (vcpu);
kvm_calc_mmu_role_common()doesn't preserve the current mmu_role.base andkvm_calc_mmu_role_common()doesn't capture all base fields. Won't @role will be incorrect for base fields that aren't set below, e.g.cr4_pae,smep_andnot_wp,smap_andnot_wp, etc...
Quoted 7 lines
Oh, I see what you mean. Actually, PATCH8 of this series adds some of this stuff but
smep_andnot_wpandsmap_andnot_wpare still not set. I think I'll enhancekvm_calc_mmu_role_common()and move some stuff from PATCH8 to this one.
(The fact that @role is currently not fully re-initialized here is very unobvious so I would definitely prefer to explicitly initialize everything over inheriting something from previously initialized role).
On the other hand if we want to perform full re-initialization we'll have to distinguish between shadow and TDP here and this isn't what we want. I'm about to change my mind as it seems that inheriting base role here is not the worst idea after all...
Signature · 1 line
Vitaly
PATCH v2 8/9 x86/kvm/mmu: check if tdp/shadow MMU reconfiguration is needed
In reply to Vitaly KuznetsovMMU reconfiguration in init_kvm_tdp_mmu()/kvm_init_shadow_mmu() can be avoided if the source data used to configure it didn't change; enhance kvm_mmu_scache with the required fields and consolidate common code in kvm_calc_mmu_role_common().
--- arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/mmu.c | 86 +++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 36 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -284,10 +284,12 @@ union kvm_mmu_extended_role {struct {unsigned int valid:1 ;unsigned int execonly:1 ; +unsigned int cr0_pg:1 ;unsigned int cr4_pse:1 ;unsigned int cr4_pke:1 ;unsigned int cr4_smap:1 ;unsigned int cr4_smep:1 ; +unsigned int cr4_la57:1 ; }; };
arch/x86/kvm/mmu.c +50 -36
153 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v2 8/9] x86/kvm/mmu: check if tdp/shadow MMU reconfiguration is needed
Quoted 3 lines · Vitaly Kuznetsov
MMU reconfiguration in init_kvm_tdp_mmu()/kvm_init_shadow_mmu() can be avoided if the source data used to configure it didn't change; enhance
kvm_mmu_scachewith the required fields and consolidate common code in
Nit: kvm_mmu_scache no longer exists, probably say "source cache" instead?
Quoted 37 lines
kvm_calc_mmu_role_common().Signed-off-by Vitaly Kuznetsov <[email protected]>--- arch/x86/include/asm/kvm_host.h | 2 + arch/x86/kvm/mmu.c | 86 +++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 36 deletions(-)arch/x86/include/asm/kvm_host.h +2
-0@@ -284,10 +284,12 @@ union kvm_mmu_extended_role {struct {unsigned int valid:1 ;unsigned int execonly:1 ; +unsigned int cr0_pg:1 ;unsigned int cr4_pse:1 ;unsigned int cr4_pke:1 ;unsigned int cr4_smap:1 ;unsigned int cr4_smep:1 ; +unsigned int cr4_la57:1 ; }; };arch/x86/kvm/mmu.c +1
-1@@ -4709,34 +4709,40 @@ static void paging32E_init_context(struct kvm_vcpu *vcpu, }static union kvm_mmu_role -kvm_calc_mmu_role_common (struct kvm_vcpu *vcpu) +kvm_calc_mmu_role_common (struct kvm_vcpu *vcpu,bool mmu_init) {union kvm_mmu_role role = {0 };
arch/x86/kvm/mmu.c static void paging32E_init_context(struct kvm_vcpu *vcpu, · line 4724
Quoted 10 lines
arch/x86/kvm/mmu.c +6
-0role.base.access =ACC_ALL ; + role.base.nxe = !!is_nx (vcpu); + role.base.cr4_pae = !!is_pae (vcpu); role.base.cr0_wp =is_write_protection (vcpu); + role.base.smm =is_smm (vcpu); + role.base.guest_mode =is_guest_mode (vcpu); +if (!mmu_init) +return role;
Can you add a comment explaining why we don't fill in role.ext when !mmu_init? Or maybe just rename mmu_init to something like base_only?
arch/x86/kvm/mmu.c static void paging32E_init_context(struct kvm_vcpu *vcpu, · line 4725
Quoted 1 line
arch/x86/kvm/mmu.c +0
-0From what I can tell it's false when we only care about the base role,
which just happens to be only in the non-init flow.
Quoted 145 lines
arch/x86/kvm/mmu.c +9
-9+ + role.ext.cr0_pg = !!is_paging (vcpu); role.ext.cr4_smep =kvm_read_cr4_bits (vcpu,X86_CR4_SMEP ) !=0 ; role.ext.cr4_smap =kvm_read_cr4_bits (vcpu,X86_CR4_SMAP ) !=0 ; role.ext.cr4_pse = !!is_pse (vcpu); role.ext.cr4_pke =kvm_read_cr4_bits (vcpu,X86_CR4_PKE ) !=0 ; + role.ext.cr4_la57 =kvm_read_cr4_bits (vcpu,X86_CR4_LA57 ) !=0 ; role.ext.valid =1 ;return role; } -static union kvm_mmu_page_role -kvm_calc_tdp_mmu_root_page_role (struct kvm_vcpu *vcpu) +static union kvm_mmu_role +kvm_calc_tdp_mmu_root_page_role (struct kvm_vcpu *vcpu,bool mmu_init) { -union kvm_mmu_page_role role = {0 }; +union kvm_mmu_role role =kvm_calc_mmu_role_common (vcpu, mmu_init); - role.guest_mode =is_guest_mode (vcpu); - role.smm =is_smm (vcpu); - role.ad_disabled = (shadow_accessed_mask ==0 ); - role.level = kvm_x86_ops->get_tdp_level (vcpu); - role.direct = true; - role.access =ACC_ALL ; + role.base.ad_disabled = (shadow_accessed_mask ==0 ); + role.base.level = kvm_x86_ops->get_tdp_level (vcpu); + role.base.direct = true;return role;}
arch/x86/kvm/mmu.c +34
-26@@ -4744,9 +4750,14 @@ kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu)static void init_kvm_tdp_mmu (struct kvm_vcpu *vcpu) {struct kvm_mmu *context = vcpu->arch.mmu; +union kvm_mmu_role new_role = +kvm_calc_tdp_mmu_root_page_role (vcpu, true); - context->mmu_role.base.word = mmu_base_role_mask.word & -kvm_calc_tdp_mmu_root_page_role (vcpu).word; + new_role.base.word &= mmu_base_role_mask.word; +if (new_role.as_u64 == context->mmu_role.as_u64) +return ; + + context->mmu_role.as_u64 = new_role.as_u64; context->page_fault = tdp_page_fault; context->sync_page = nonpaging_sync_page; context->invlpg = nonpaging_invlpg;@@ -4786,29 +4797,23 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu)reset_tdp_shadow_zero_bits_mask (vcpu, context); } -static union kvm_mmu_page_role -kvm_calc_shadow_mmu_root_page_role (struct kvm_vcpu *vcpu) -{ -union kvm_mmu_page_role role = {0 }; -bool smep =kvm_read_cr4_bits (vcpu,X86_CR4_SMEP ); -bool smap =kvm_read_cr4_bits (vcpu,X86_CR4_SMAP ); - - role.nxe =is_nx (vcpu); - role.cr4_pae = !!is_pae (vcpu); - role.cr0_wp =is_write_protection (vcpu); - role.smep_andnot_wp = smep && !is_write_protection (vcpu); - role.smap_andnot_wp = smap && !is_write_protection (vcpu); - role.guest_mode =is_guest_mode (vcpu); - role.smm =is_smm (vcpu); - role.direct = !is_paging (vcpu); - role.access =ACC_ALL ; +static union kvm_mmu_role +kvm_calc_shadow_mmu_root_page_role (struct kvm_vcpu *vcpu,bool mmu_init) +{ +union kvm_mmu_role role =kvm_calc_mmu_role_common (vcpu, mmu_init); + + role.base.smep_andnot_wp = role.ext.cr4_smep && + !is_write_protection (vcpu); + role.base.smap_andnot_wp = role.ext.cr4_smap && + !is_write_protection (vcpu); + role.base.direct = !is_paging (vcpu);if (!is_long_mode (vcpu)) - role.level =PT32E_ROOT_LEVEL ; + role.base.level =PT32E_ROOT_LEVEL ;else if (is_la57_mode (vcpu)) - role.level =PT64_ROOT_5LEVEL ; + role.base.level =PT64_ROOT_5LEVEL ;else - role.level =PT64_ROOT_4LEVEL ; + role.base.level =PT64_ROOT_4LEVEL ;return role; }@@ -4816,6 +4821,12 @@ kvm_calc_shadow_mmu_root_page_role(struct kvm_vcpu *vcpu)void kvm_init_shadow_mmu (struct kvm_vcpu *vcpu) {struct kvm_mmu *context = vcpu->arch.mmu; +union kvm_mmu_role new_role = +kvm_calc_shadow_mmu_root_page_role (vcpu, true); + + new_role.base.word &= mmu_base_role_mask.word; +if (new_role.as_u64 == context->mmu_role.as_u64) +return ;if (!is_paging (vcpu))nonpaging_init_context (vcpu, context);@@ -4826,8 +4837,7 @@ void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu)else paging32_init_context (vcpu, context); - context->mmu_role.base.word = mmu_base_role_mask.word & -kvm_calc_shadow_mmu_root_page_role (vcpu).word; + context->mmu_role.as_u64 = new_role.as_u64;reset_shadow_zero_bits_mask (vcpu, context); }EXPORT_SYMBOL_GPL (kvm_init_shadow_mmu);@@ -4836,7 +4846,7 @@ static union kvm_mmu_role kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty, bool execonly) { - union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu); + union kvm_mmu_role role = kvm_calc_mmu_role_common(vcpu, true); role.base.level =PT64_ROOT_4LEVEL ; role.base.direct = false;@@ -4961,10 +4971,14 @@ EXPORT_SYMBOL_GPL(kvm_init_mmu);static union kvm_mmu_page_role kvm_mmu_calc_root_page_role (struct kvm_vcpu *vcpu) { +union kvm_mmu_role role; +if (tdp_enabled) -return kvm_calc_tdp_mmu_root_page_role (vcpu); + role =kvm_calc_tdp_mmu_root_page_role (vcpu, false);else -return kvm_calc_shadow_mmu_root_page_role (vcpu); + role =kvm_calc_shadow_mmu_root_page_role (vcpu, false); + +return role.base; }void kvm_mmu_reset_context (struct kvm_vcpu *vcpu)Signature · 1 line
2.17.1
PATCH v2 9/9 x86/kvm/mmu: check if MMU reconfiguration is needed in init_kvm_nested_mmu()
In reply to Vitaly KuznetsovWe don't use root page role for nested_mmu, however, optimizing out re-initialization in case nothing changed is still valuable as this is done for every nested vmentry.
--- arch/x86/kvm/mmu.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kvm/mmu.c +6 -0
@@ -4907,8 +4907,14 @@ static void init_kvm_softmmu(struct kvm_vcpu *vcpu)static void init_kvm_nested_mmu (struct kvm_vcpu *vcpu) { +union kvm_mmu_role new_role =kvm_calc_mmu_role_common (vcpu, true);struct kvm_mmu *g_context = &vcpu->arch.nested_mmu; + new_role.base.word &= mmu_base_role_mask.word; +if (new_role.as_u64 == g_context->mmu_role.as_u64) +return ; + + g_context->mmu_role.as_u64 = new_role.as_u64; g_context->get_cr3 = get_cr3; g_context->get_pdptr = kvm_pdptr_read; g_context->inject_page_fault = kvm_inject_page_fault;
Signature · 1 line
2.17.1
Re: [PATCH v2 9/9] x86/kvm/mmu: check if MMU reconfiguration is needed in init_kvm_nested_mmu()
Quoted 6 lines · Vitaly Kuznetsov
We don't use root page role for
nested_mmu, however, optimizing out re-initialization in case nothing changed is still valuable as this is done for every nested vmentry.Signed-off-by Vitaly Kuznetsov <[email protected]>---