KVM: X86: misc fixes and cleanup
Subsystems (4)
cover KVM: X86: misc fixes and cleanup
From: Lai Jiangshan <[email protected]>
This pacheset has misc fixes and cleanups with loose dependence to each other. It includes a prevous patch that inverts and renames gpte_is_8_bytes.
Ohter patches focus on the usage of vcpu->arch.walk_mmu and vcpu->arch.mmu, root_level, lpage_level.
Patch 1,4,5,6,7 fixes something, but I don't think path4,5,6,7 are worth in stable tree.
Lai Jiangshan (12):
KVM: X86: Fix when shadow_root_level=5 && guest root_level<4
KVM: X86: Add parameter struct kvm_mmu *mmu into mmu->gva_to_gpa()
KVM: X86: Remove mmu->translate_gpa
KVM: X86: Use vcpu->arch.walk_mmu for kvm_mmu_invlpg()
KVM: X86: Change the type of a parameter of kvm_mmu_invalidate_gva()
and mmu->invlpg() to gpa_t
KVM: X86: Add huge_page_level to __reset_rsvds_bits_mask_ept()
KVM: X86: Add parameter huge_page_level to kvm_init_shadow_ept_mmu()
KVM: VMX: Use ept_caps_to_lpage_level() in hardware_setup()
KVM: X86: Rename gpte_is_8_bytes to has_4_byte_gpte and invert the
direction
KVM: X86: Remove mmu parameter from load_pdptrs()
KVM: X86: Check root_level only in fast_pgd_switch()
KVM: X86: Walk shadow page starting with shadow_root_level
Documentation/virt/kvm/mmu.rst | 8 +-- arch/x86/include/asm/kvm_host.h | 23 ++++---- arch/x86/kvm/mmu.h | 16 ++++- arch/x86/kvm/mmu/mmu.c | 100 +++++++++++++++----------------- arch/x86/kvm/mmu/mmu_audit.c | 5 +- arch/x86/kvm/mmu/mmutrace.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 55 ++++-------------- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- arch/x86/kvm/svm/nested.c | 4 +- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/capabilities.h | 9 +++ arch/x86/kvm/vmx/nested.c | 12 ++-- arch/x86/kvm/vmx/vmx.c | 12 +--- arch/x86/kvm/x86.c | 55 +++++++++++------- 14 files changed, 145 insertions(+), 160 deletions(-)
Signature · 1 line
2.19.1.6.gb485710b
PATCH 1/12 KVM: X86: Fix when shadow_root_level=5 && guest root_level<4
From: Lai Jiangshan <[email protected]>
If the is an L1 with nNPT in 32bit, the shadow walk starts with pae_root.
--- arch/x86/kvm/mmu/mmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -2
@@ -2171,10 +2171,10 @@ static void shadow_walk_init_using_root(struct kvm_shadow_walk_iterator *iterato iterator->shadow_addr = root; iterator->level = vcpu->arch.mmu->shadow_root_level; -if (iterator->level ==PT64_ROOT_4LEVEL && +if (iterator->level >=PT64_ROOT_4LEVEL && vcpu->arch.mmu->root_level <PT64_ROOT_4LEVEL && !vcpu->arch.mmu->direct_map) - --iterator->level; + iterator->level =PT32E_ROOT_LEVEL ;if (iterator->level ==PT32E_ROOT_LEVEL ) {/*
Signature · 1 line
2.19.1.6.gb485710b
Sean Christopherson
maintainerRe: [PATCH 1/12] KVM: X86: Fix when shadow_root_level=5 && guest root_level<4
Quoted 6 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
If the is an L1 with nNPT in 32bit, the shadow walk starts with
pae_root.Fixes a717a780fc4e ("KVM: x86/mmu: Support shadowing NPT when 5-level paging is enabled in host)
Have you actually run with 5-level nNPT? I don't have access to hardware, at least not that I know of :-)
I'm staring at kvm_mmu_sync_roots() and don't see how it can possibly work for 5-level nNPT with a 4-level NPT guest.
Sean Christopherson
maintainerRe: [PATCH 1/12] KVM: X86: Fix when shadow_root_level=5 && guest root_level<4
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
If the is an L1 with nNPT in 32bit, the shadow walk starts with
pae_root.
Quoted 7 lines
Quoted 1 line
Fixes a717a780fc4e ("KVM: x86/mmu: Support shadowing NPT when 5-level paging is enabled in host)Have you actually run with 5-level nNPT? I don't have access to hardware, at least not that I know of :-)
I'm staring at
kvm_mmu_sync_roots()and don't see how it can possibly work for 5-level nNPT with a 4-level NPT guest.
Oh, and fast_pgd_switch() will also break kvm_mmu_sync_prev_roots() / is_unsync_root() by putting a root into the prev_roots array that doesn't have a shadow page associated with the root.
Re: [PATCH 1/12] KVM: X86: Fix when shadow_root_level=5 && guest root_level<4
In reply to Sean ChristophersonQuoted 10 lines · Sean Christopherson
Quoted 6 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
If the is an L1 with nNPT in 32bit, the shadow walk starts with
pae_root.Fixes a717a780fc4e ("KVM: x86/mmu: Support shadowing NPT when 5-level paging is enabled in host)Have you actually run with 5-level nNPT? I don't have access to hardware, at least not that I know of :-)
The code is just obvious incorrect for shadow_root_level=5 && guest root_level<4.
Quoted 4 lines
I'm staring at
kvm_mmu_sync_roots()and don't see how it can possibly work for 5-level nNPT with a 4-level NPT guest.
It doesn't use pml5_root for 5-level nNPT with a 4-level NPT guest, so kvm_mmu_sync_roots() can work in a silence way with an "unexpected" root shadow page. It has problems for 5-level nNPT with a 4-level NPT guest.
See:
https://lore.kernel.org/lkml/[email protected]/
especially patch4.
Your this reply motivated me to complete the changelog of a patchset and send it, thanks!
Although the patchset is immature, it would be better than losing it.
PATCH 2/12 KVM: X86: Add parameter struct kvm_mmu *mmu into mmu->gva_to_gpa()
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
The mmu->gva_to_gpa() has no "struct kvm_mmu *mmu", so an extra FNAME(gva_to_gpa_nested) is needed.
Add the parameter can simplify the code. And it makes it explicit that the walk is upon vcpu->arch.walk_mmu for gva and vcpu->arch.mmu for L2 gpa in translate_nested_gpa() via the new parameter.
--- arch/x86/include/asm/kvm_host.h | 5 ++-- arch/x86/kvm/mmu/mmu.c | 24 +++++++------------ arch/x86/kvm/mmu/paging_tmpl.h | 41 ++++----------------------------- arch/x86/kvm/x86.c | 39 ++++++++++++++++++++----------- 4 files changed, 41 insertions(+), 68 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -2
@@ -426,8 +426,9 @@ struct kvm_mmu {int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);void (*inject_page_fault)(struct kvm_vcpu *vcpu,struct x86_exception *fault); -gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,gpa_t gva_or_gpa, -u32 access,struct x86_exception *exception); +gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +gpa_t gva_or_gpa,u32 access, +struct x86_exception *exception);gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu,gpa_t gpa,u32 access,struct x86_exception *exception); int (*sync_page)(struct kvm_vcpu *vcpu,
arch/x86/kvm/mmu/mmu.c +8 -16
@@ -3728,21 +3728,13 @@ void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu)kvm_mmu_free_roots (vcpu, vcpu->arch.mmu, roots_to_free); } -static gpa_t nonpaging_gva_to_gpa (struct kvm_vcpu *vcpu,gpa_t vaddr, -u32 access,struct x86_exception *exception) +static gpa_t nonpaging_gva_to_gpa (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +gpa_t vaddr,u32 access, +struct x86_exception *exception) {if (exception) exception->error_code =0 ; -return vaddr; -} - -static gpa_t nonpaging_gva_to_gpa_nested (struct kvm_vcpu *vcpu,gpa_t vaddr, -u32 access, -struct x86_exception *exception) -{ -if (exception) - exception->error_code =0 ; -return vcpu->arch.nested_mmu.translate_gpa (vcpu, vaddr, access, exception); +return mmu->translate_gpa (vcpu, vaddr, access, exception); }static bool mmio_info_in_cache (struct kvm_vcpu *vcpu,u64 addr,bool direct)
@@ -4982,13 +4974,13 @@ static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu)* the gva_to_gpa functions between mmu and nested_mmu are swapped. */ if (!is_paging (vcpu)) - g_context->gva_to_gpa = nonpaging_gva_to_gpa_nested; + g_context->gva_to_gpa = nonpaging_gva_to_gpa;else if (is_long_mode (vcpu)) - g_context->gva_to_gpa = paging64_gva_to_gpa_nested; + g_context->gva_to_gpa = paging64_gva_to_gpa;else if (is_pae (vcpu)) - g_context->gva_to_gpa = paging64_gva_to_gpa_nested; + g_context->gva_to_gpa = paging64_gva_to_gpa;else - g_context->gva_to_gpa = paging32_gva_to_gpa_nested; + g_context->gva_to_gpa = paging32_gva_to_gpa;reset_guest_paging_metadata (vcpu, g_context); }
arch/x86/kvm/mmu/paging_tmpl.h +5 -36
71 lines not on this page · raw message
arch/x86/kvm/x86.c +25 -14
@@ -6460,13 +6460,14 @@ void kvm_get_segment(struct kvm_vcpu *vcpu,gpa_t translate_nested_gpa (struct kvm_vcpu *vcpu,gpa_t gpa,u32 access,struct x86_exception *exception) { +struct kvm_mmu *mmu = vcpu->arch.mmu;gpa_t t_gpa;BUG_ON (!mmu_is_nested (vcpu));/* NPT walks are always user-walks */ access |=PFERR_USER_MASK ; - t_gpa = vcpu->arch.mmu->gva_to_gpa (vcpu, gpa, access, exception); + t_gpa = mmu->gva_to_gpa (vcpu, mmu, gpa, access, exception);return t_gpa; }
@@ -6474,25 +6475,31 @@ gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,gpa_t kvm_mmu_gva_to_gpa_read (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { +struct kvm_mmu *mmu = vcpu->arch.walk_mmu; +u32 access = (static_call (kvm_x86_get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ; -return vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva, access, exception); +return mmu->gva_to_gpa (vcpu, mmu, gva, access, exception); }EXPORT_SYMBOL_GPL (kvm_mmu_gva_to_gpa_read);gpa_t kvm_mmu_gva_to_gpa_fetch (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { +struct kvm_mmu *mmu = vcpu->arch.walk_mmu; +u32 access = (static_call (kvm_x86_get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ; access |=PFERR_FETCH_MASK ; -return vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva, access, exception); +return mmu->gva_to_gpa (vcpu, mmu, gva, access, exception); }gpa_t kvm_mmu_gva_to_gpa_write (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { +struct kvm_mmu *mmu = vcpu->arch.walk_mmu; +u32 access = (static_call (kvm_x86_get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ; access |=PFERR_WRITE_MASK ; -return vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva, access, exception); +return mmu->gva_to_gpa (vcpu, mmu, gva, access, exception); }EXPORT_SYMBOL_GPL (kvm_mmu_gva_to_gpa_write);
@@ -6500,19 +6507,21 @@ EXPORT_SYMBOL_GPL(kvm_mmu_gva_to_gpa_write);gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { -return vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva,0 , exception); +struct kvm_mmu *mmu = vcpu->arch.walk_mmu; + +return mmu->gva_to_gpa (vcpu, mmu, gva,0 , exception); }static int kvm_read_guest_virt_helper (gva_t addr,void *val,unsigned int bytes,struct kvm_vcpu *vcpu,u32 access,struct x86_exception *exception) { +struct kvm_mmu *mmu = vcpu->arch.walk_mmu;void *data = val;int r =X86EMUL_CONTINUE ;while (bytes) { -gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa (vcpu, addr, access, - exception); +gpa_t gpa = mmu->gva_to_gpa (vcpu, mmu, addr, access, exception);unsigned offset = addr & (PAGE_SIZE -1 );unsigned toread =min (bytes, (unsigned )PAGE_SIZE - offset);int ret;
@@ -6540,13 +6549,14 @@ static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, struct x86_exception *exception) {struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); +struct kvm_mmu *mmu = vcpu->arch.walk_mmu;u32 access = (static_call (kvm_x86_get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ;unsigned offset;int ret;/* Inline kvm_read_guest_virt_helper for speed. */ -gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa (vcpu, addr, access|PFERR_FETCH_MASK , - exception); +gpa_t gpa = mmu->gva_to_gpa (vcpu, mmu, addr, access|PFERR_FETCH_MASK , + exception);if (unlikely (gpa ==UNMAPPED_GVA ))return X86EMUL_PROPAGATE_FAULT ;
@@ -6605,13 +6615,12 @@ static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes struct kvm_vcpu *vcpu, u32 access, struct x86_exception *exception) { + struct kvm_mmu *mmu = vcpu->arch.walk_mmu;void *data = val;int r =X86EMUL_CONTINUE ;while (bytes) { -gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa (vcpu, addr, - access, - exception); +gpa_t gpa = mmu->gva_to_gpa (vcpu, mmu, addr, access, exception);unsigned offset = addr & (PAGE_SIZE -1 );unsigned towrite =min (bytes, (unsigned )PAGE_SIZE - offset);int ret;
@@ -6698,6 +6707,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, gpa_t *gpa, struct x86_exception *exception, bool write) { + struct kvm_mmu *mmu = vcpu->arch.walk_mmu;u32 access = ((static_call (kvm_x86_get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ) | (write ?PFERR_WRITE_MASK :0 );
@@ -6715,7 +6725,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,return 1 ; } - *gpa = vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva, access, exception); + *gpa = mmu->gva_to_gpa (vcpu, mmu, gva, access, exception);if (*gpa ==UNMAPPED_GVA )return -1 ;
@@ -12268,12 +12278,13 @@ EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value);void kvm_fixup_and_inject_pf_error (struct kvm_vcpu *vcpu,gva_t gva,u16 error_code) { +struct kvm_mmu *mmu = vcpu->arch.walk_mmu;struct x86_exception fault;u32 access = error_code & (PFERR_WRITE_MASK |PFERR_FETCH_MASK |PFERR_USER_MASK );if (!(error_code &PFERR_PRESENT_MASK ) || - vcpu->arch.walk_mmu->gva_to_gpa (vcpu, gva, access, &fault) !=UNMAPPED_GVA ) { + mmu->gva_to_gpa (vcpu, mmu, gva, access, &fault) !=UNMAPPED_GVA ) {/* * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page * tables probably do not match the TLB. Just proceed
Signature · 1 line
2.19.1.6.gb485710b
PATCH 3/12 KVM: X86: Remove mmu->translate_gpa
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
Reduce an indirect function call (retpoline) and some intialization code.
--- arch/x86/include/asm/kvm_host.h | 4 ---- arch/x86/kvm/mmu.h | 13 +++++++++++++ arch/x86/kvm/mmu/mmu.c | 11 +---------- arch/x86/kvm/mmu/paging_tmpl.h | 7 +++---- arch/x86/kvm/x86.c | 4 ++-- 5 files changed, 19 insertions(+), 20 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -4
@@ -429,8 +429,6 @@ struct kvm_mmu {gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_mmu *mmu,gpa_t gva_or_gpa,u32 access,struct x86_exception *exception); -gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu,gpa_t gpa,u32 access, -struct x86_exception *exception);int (*sync_page)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp);void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa);
@@ -1764,8 +1762,6 @@ int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn);void kvm_mmu_free_roots (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu,ulong roots_to_free);void kvm_mmu_free_guest_mode_roots (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu); -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,struct x86_exception *exception); gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
arch/x86/kvm/mmu.h +13 -0
@@ -351,4 +351,17 @@ static inline void kvm_update_page_stats(struct kvm *kvm, int level, int count) {atomic64_add (count, &kvm->stat.pages[level -1 ]); } + +gpa_t translate_nested_gpa (struct kvm_vcpu *vcpu,gpa_t gpa,u32 access, +struct x86_exception *exception); + +static inline gpa_t kvm_translate_gpa (struct kvm_vcpu *vcpu, +struct kvm_mmu *mmu, +gpa_t gpa,u32 access, +struct x86_exception *exception) +{ +if (mmu != &vcpu->arch.nested_mmu) +return gpa; +return translate_nested_gpa (vcpu, gpa, access, exception); +}#endif
arch/x86/kvm/mmu/mmu.c +1 -10
@@ -335,12 +335,6 @@ static bool check_mmio_spte(struct kvm_vcpu *vcpu, u64 spte)return likely (kvm_gen == spte_gen); } -static gpa_t translate_gpa (struct kvm_vcpu *vcpu,gpa_t gpa,u32 access, -struct x86_exception *exception) -{ -return gpa; -} -static int is_cpuid_PSE36 (void ) {return 1 ;
@@ -3734,7 +3728,7 @@ static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, {if (exception) exception->error_code =0 ; -return mmu->translate_gpa (vcpu, vaddr, access, exception); +return kvm_translate_gpa (vcpu, mmu, vaddr, access, exception); }static bool mmio_info_in_cache (struct kvm_vcpu *vcpu,u64 addr,bool direct)
@@ -5487,7 +5481,6 @@ static int __kvm_mmu_create(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu) mmu->root_hpa =INVALID_PAGE ; mmu->root_pgd =0 ; - mmu->translate_gpa = translate_gpa;for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++) mmu->prev_roots[i] =KVM_MMU_ROOT_INFO_INVALID ;
@@ -5549,8 +5542,6 @@ 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.nested_mmu.translate_gpa = translate_nested_gpa; - ret =__kvm_mmu_create (vcpu, &vcpu->arch.guest_mmu);if (ret)return ret;
arch/x86/kvm/mmu/paging_tmpl.h +3 -4
@@ -403,9 +403,8 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, walker->table_gfn[walker->level -1 ] = table_gfn; walker->pte_gpa[walker->level -1 ] = pte_gpa; - real_gpa = mmu->translate_gpa (vcpu,gfn_to_gpa (table_gfn), - nested_access, - &walker->fault); + real_gpa =kvm_translate_gpa (vcpu, mmu,gfn_to_gpa (table_gfn), + nested_access, &walker->fault);/* * FIXME: This can happen if emulation (for of an INS/OUTS
@@ -467,7 +466,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,if (PTTYPE ==32 && walker->level >PG_LEVEL_4K &&is_cpuid_PSE36 ()) gfn +=pse36_gfn_delta (pte); - real_gpa = mmu->translate_gpa (vcpu,gfn_to_gpa (gfn), access, &walker->fault); + real_gpa =kvm_translate_gpa (vcpu, mmu,gfn_to_gpa (gfn), access, &walker->fault);if (real_gpa ==UNMAPPED_GVA )return 0 ;
arch/x86/kvm/x86.c +2 -2
@@ -810,8 +810,8 @@ int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)* If the MMU is nested, CR3 holds an L2 GPA and needs to be translated * to an L1 GPA. */ - real_gpa = mmu->translate_gpa (vcpu,gfn_to_gpa (pdpt_gfn), -PFERR_USER_MASK |PFERR_WRITE_MASK ,NULL ); + real_gpa =kvm_translate_gpa (vcpu, mmu,gfn_to_gpa (pdpt_gfn), +PFERR_USER_MASK |PFERR_WRITE_MASK ,NULL );if (real_gpa ==UNMAPPED_GVA )return 0 ;
Signature · 1 line
2.19.1.6.gb485710b
PATCH 4/12 KVM: X86: Use vcpu->arch.walk_mmu for kvm_mmu_invlpg()
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
In design, the guest virtuall address is only make sense for vcpu->arch.walk_mmu which is often the same as vcpu->arch.mmu. But they are different semantic by design, so vcpu->arch.walk_mmu should be used instead like other call site of kvm_mmu_invalidate_gva().
In theory, if L2's invlpg is being emulated by L0 (in practice, it hardly happen) when nTDP is used, ->tlb_flush_gva() should be called to flush hwTLB, but using vcpu->arch.mmu causes it incorrectly skept.
--- arch/x86/kvm/mmu/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -5353,7 +5353,7 @@ void kvm_mmu_invalidate_gva(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,void kvm_mmu_invlpg (struct kvm_vcpu *vcpu,gva_t gva) { -kvm_mmu_invalidate_gva (vcpu, vcpu->arch.mmu, gva,INVALID_PAGE ); +kvm_mmu_invalidate_gva (vcpu, vcpu->arch.walk_mmu, gva,INVALID_PAGE ); ++vcpu->stat.invlpg; }EXPORT_SYMBOL_GPL (kvm_mmu_invlpg);
Signature · 1 line
2.19.1.6.gb485710b
PATCH 5/12 KVM: X86: Change the type of a parameter of kvm_mmu_invalidate_gva() and mmu->invlpg() to gpa_t
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
When kvm_mmu_invalidate_gva() is called for nested TDP, the @gva is L2 GPA, so the type of the parameter should be gpa_t like mmu->gva_to_gpa().
The parameter name is also changed to gva_or_l2pa for self documentation.
--- arch/x86/include/asm/kvm_host.h | 6 +++--- arch/x86/kvm/mmu/mmu.c | 14 +++++++------- arch/x86/kvm/mmu/paging_tmpl.h | 7 ++++--- 3 files changed, 14 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -3
@@ -427,11 +427,11 @@ struct kvm_mmu {void (*inject_page_fault)(struct kvm_vcpu *vcpu,struct x86_exception *fault);gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, -gpa_t gva_or_gpa,u32 access, +gpa_t gva_or_l2pa,u32 access,struct x86_exception *exception);int (*sync_page)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp); -void (*invlpg)(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa); +void (*invlpg)(struct kvm_vcpu *vcpu,gpa_t gva_or_l2pa,hpa_t root_hpa);hpa_t root_hpa;gpa_t root_pgd;union kvm_mmu_role mmu_role;
@@ -1785,7 +1785,7 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, void *insn, int insn_len);void kvm_mmu_invlpg (struct kvm_vcpu *vcpu,gva_t gva);void kvm_mmu_invalidate_gva (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, -gva_t gva,hpa_t root_hpa); +gpa_t gva_or_l2pa,hpa_t root_hpa);void kvm_mmu_invpcid_gva (struct kvm_vcpu *vcpu,gva_t gva,unsigned long pcid);void kvm_mmu_new_pgd (struct kvm_vcpu *vcpu,gpa_t new_pgd);
arch/x86/kvm/mmu/mmu.c +7 -7
@@ -5313,24 +5313,24 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,EXPORT_SYMBOL_GPL (kvm_mmu_page_fault);void kvm_mmu_invalidate_gva (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, -gva_t gva,hpa_t root_hpa) +gpa_t gva_or_l2pa,hpa_t root_hpa) {int i; -/* It's actually a GPA for vcpu->arch.guest_mmu. */ +/* It's actually a L2 GPA for vcpu->arch.guest_mmu. */ if (mmu != &vcpu->arch.guest_mmu) {/* INVLPG on a non-canonical address is a NOP according to the SDM. */ -if (is_noncanonical_address (gva, vcpu)) +if (is_noncanonical_address (gva_or_l2pa, vcpu))return ; -static_call (kvm_x86_tlb_flush_gva)(vcpu, gva); +static_call (kvm_x86_tlb_flush_gva)(vcpu, gva_or_l2pa); }if (!mmu->invlpg)return ;if (root_hpa ==INVALID_PAGE ) { - mmu->invlpg (vcpu, gva, mmu->root_hpa); + mmu->invlpg (vcpu, gva_or_l2pa, mmu->root_hpa);/* * INVLPG is required to invalidate any global mappings for the VA,
@@ -5345,9 +5345,9 @@ void kvm_mmu_invalidate_gva(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,*/ for (i =0 ; i <KVM_MMU_NUM_PREV_ROOTS ; i++)if (VALID_PAGE (mmu->prev_roots[i].hpa)) - mmu->invlpg (vcpu, gva, mmu->prev_roots[i].hpa); + mmu->invlpg (vcpu, gva_or_l2pa, mmu->prev_roots[i].hpa); }else { - mmu->invlpg (vcpu, gva, root_hpa); + mmu->invlpg (vcpu, gva_or_l2pa, root_hpa); } }
arch/x86/kvm/mmu/paging_tmpl.h +4 -3
@@ -928,7 +928,8 @@ static gpa_t FNAME(get_level1_sp_gpa)(struct kvm_mmu_page *sp)return gfn_to_gpa (sp->gfn) + offset *sizeof (pt_element_t); } -static void FNAME (invlpg )(struct kvm_vcpu *vcpu,gva_t gva,hpa_t root_hpa) +/* Note, @gva_or_l2pa is a GPA when invlpg() invalidates an L2 GPA. */ +static void FNAME (invlpg )(struct kvm_vcpu *vcpu,gpa_t gva_or_l2pa,hpa_t root_hpa) {struct kvm_shadow_walk_iterator iterator;struct kvm_mmu_page *sp;
@@ -936,7 +937,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa)int level;u64 *sptep; -vcpu_clear_mmio_info (vcpu, gva); +vcpu_clear_mmio_info (vcpu, gva_or_l2pa);/* * No need to check return value here, rmap_can_add() can
@@ -950,7 +951,7 @@ static void FNAME(invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa) }write_lock (&vcpu->kvm->mmu_lock); -for_each_shadow_entry_using_root (vcpu, root_hpa, gva, iterator) { +for_each_shadow_entry_using_root (vcpu, root_hpa, gva_or_l2pa, iterator) { level = iterator.level; sptep = iterator.sptep;
Signature · 1 line
2.19.1.6.gb485710b
PATCH 6/12 KVM: X86: Add huge_page_level to __reset_rsvds_bits_mask_ept()
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
Bit 7 on pte depends on the level of supported large page.
--- arch/x86/kvm/mmu/mmu.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu/mmu.c +19 -10
@@ -4339,22 +4339,28 @@ static void reset_rsvds_bits_mask(struct kvm_vcpu *vcpu,static void __reset_rsvds_bits_mask_ept (struct rsvd_bits_validate *rsvd_check, -u64 pa_bits_rsvd,bool execonly) +u64 pa_bits_rsvd,bool execonly,int huge_page_level) {u64 high_bits_rsvd = pa_bits_rsvd &rsvd_bits (0 ,51 ); +u64 large_1g_rsvd =0 , large_2m_rsvd =0 ;u64 bad_mt_xwr; +if (huge_page_level <PG_LEVEL_1G ) + large_1g_rsvd =rsvd_bits (7 ,7 ); +if (huge_page_level <PG_LEVEL_2M ) + large_2m_rsvd =rsvd_bits (7 ,7 ); + rsvd_check->rsvd_bits_mask[0 ][4 ] = high_bits_rsvd |rsvd_bits (3 ,7 ); rsvd_check->rsvd_bits_mask[0 ][3 ] = high_bits_rsvd |rsvd_bits (3 ,7 ); - rsvd_check->rsvd_bits_mask[0 ][2 ] = high_bits_rsvd |rsvd_bits (3 ,6 ); - rsvd_check->rsvd_bits_mask[0 ][1 ] = high_bits_rsvd |rsvd_bits (3 ,6 ); + rsvd_check->rsvd_bits_mask[0 ][2 ] = high_bits_rsvd |rsvd_bits (3 ,6 ) | large_1g_rsvd; + rsvd_check->rsvd_bits_mask[0 ][1 ] = high_bits_rsvd |rsvd_bits (3 ,6 ) | large_2m_rsvd; rsvd_check->rsvd_bits_mask[0 ][0 ] = high_bits_rsvd;/* large page */ rsvd_check->rsvd_bits_mask[1 ][4 ] = rsvd_check->rsvd_bits_mask[0 ][4 ]; rsvd_check->rsvd_bits_mask[1 ][3 ] = rsvd_check->rsvd_bits_mask[0 ][3 ]; - rsvd_check->rsvd_bits_mask[1 ][2 ] = high_bits_rsvd |rsvd_bits (12 ,29 ); - rsvd_check->rsvd_bits_mask[1 ][1 ] = high_bits_rsvd |rsvd_bits (12 ,20 ); + rsvd_check->rsvd_bits_mask[1 ][2 ] = high_bits_rsvd |rsvd_bits (12 ,29 ) | large_1g_rsvd; + rsvd_check->rsvd_bits_mask[1 ][1 ] = high_bits_rsvd |rsvd_bits (12 ,20 ) | large_2m_rsvd; rsvd_check->rsvd_bits_mask[1 ][0 ] = rsvd_check->rsvd_bits_mask[0 ][0 ]; bad_mt_xwr =0xFFull << (2 *8 );/* bits 3..5 must not be 2 */
@@ -4370,10 +4376,11 @@ __reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check, }static void reset_rsvds_bits_mask_ept (struct kvm_vcpu *vcpu, -struct kvm_mmu *context,bool execonly) +struct kvm_mmu *context,bool execonly,int huge_page_level) {__reset_rsvds_bits_mask_ept (&context->guest_rsvd_check, - vcpu->arch.reserved_gpa_bits, execonly); + vcpu->arch.reserved_gpa_bits, execonly, + huge_page_level); }static inline u64 reserved_hpa_bits (void )
@@ -4449,7 +4456,8 @@ reset_tdp_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, false, true); else__reset_rsvds_bits_mask_ept (shadow_zero_check , -reserved_hpa_bits (),false ); +reserved_hpa_bits (),false , +max_huge_page_level );if (!shadow_me_mask)return ;
@@ -4469,7 +4477,8 @@ reset_ept_shadow_zero_bits_mask(struct kvm_vcpu *vcpu, struct kvm_mmu *context, bool execonly) {__reset_rsvds_bits_mask_ept (&context->shadow_zero_check, -reserved_hpa_bits (), execonly); +reserved_hpa_bits (), execonly, + max_huge_page_level); }#define BYTE_MASK (access) \
@@ -4904,7 +4913,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,update_permission_bitmask (context, true);update_pkru_bitmask (context); -reset_rsvds_bits_mask_ept (vcpu, context, execonly); +reset_rsvds_bits_mask_ept (vcpu, context, execonly, max_huge_page_level);reset_ept_shadow_zero_bits_mask (vcpu, context, execonly); }EXPORT_SYMBOL_GPL (kvm_init_shadow_ept_mmu);
Signature · 1 line
2.19.1.6.gb485710b
PATCH 7/12 KVM: X86: Add parameter huge_page_level to kvm_init_shadow_ept_mmu()
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
The level of supported large page on nEPT affects the rsvds_bits_mask.
--- arch/x86/kvm/mmu.h | 3 ++- arch/x86/kvm/mmu/mmu.c | 5 +++-- arch/x86/kvm/vmx/capabilities.h | 9 +++++++++ arch/x86/kvm/vmx/nested.c | 8 +++++--- 4 files changed, 19 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu.h +2 -1
@@ -71,7 +71,8 @@ void kvm_init_mmu(struct kvm_vcpu *vcpu);void kvm_init_shadow_npt_mmu (struct kvm_vcpu *vcpu,unsigned long cr0,unsigned long cr4,u64 efer,gpa_t nested_cr3);void kvm_init_shadow_ept_mmu (struct kvm_vcpu *vcpu,bool execonly, -bool accessed_dirty,gpa_t new_eptp); +int huge_page_level,bool accessed_dirty, +gpa_t new_eptp);bool kvm_can_do_async_pf (struct kvm_vcpu *vcpu);int kvm_handle_page_fault (struct kvm_vcpu *vcpu,u64 error_code,u64 fault_address,char *insn,int insn_len);
arch/x86/kvm/mmu/mmu.c +3 -2
@@ -4886,7 +4886,8 @@ kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty, }void kvm_init_shadow_ept_mmu (struct kvm_vcpu *vcpu,bool execonly, -bool accessed_dirty,gpa_t new_eptp) +int huge_page_level,bool accessed_dirty, +gpa_t new_eptp) {struct kvm_mmu *context = &vcpu->arch.guest_mmu;u8 level =vmx_eptp_page_walk_level (new_eptp);
@@ -4913,7 +4914,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,update_permission_bitmask (context, true);update_pkru_bitmask (context); -reset_rsvds_bits_mask_ept (vcpu, context, execonly, max_huge_page_level); +reset_rsvds_bits_mask_ept (vcpu, context, execonly, huge_page_level);reset_ept_shadow_zero_bits_mask (vcpu, context, execonly); }EXPORT_SYMBOL_GPL (kvm_init_shadow_ept_mmu);
arch/x86/kvm/vmx/capabilities.h +9 -0
@@ -312,6 +312,15 @@ static inline bool cpu_has_vmx_ept_1g_page(void)return vmx_capability.ept &VMX_EPT_1GB_PAGE_BIT ; } +static inline int ept_caps_to_lpage_level (u32 ept_caps) +{ +if (ept_caps &VMX_EPT_1GB_PAGE_BIT ) +return PG_LEVEL_1G ; +if (ept_caps &VMX_EPT_2MB_PAGE_BIT ) +return PG_LEVEL_2M ; +return PG_LEVEL_4K ; +} +static inline bool cpu_has_vmx_ept_ad_bits (void ) {return vmx_capability.ept &VMX_EPT_AD_BIT ;
arch/x86/kvm/vmx/nested.c +5 -3
@@ -391,9 +391,11 @@ static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,static void nested_ept_new_eptp (struct kvm_vcpu *vcpu) { -kvm_init_shadow_ept_mmu (vcpu, -to_vmx (vcpu)->nested.msrs.ept_caps & -VMX_EPT_EXECUTE_ONLY_BIT , +struct vcpu_vmx *vmx =to_vmx (vcpu); +bool execonly = vmx->nested.msrs.ept_caps &VMX_EPT_EXECUTE_ONLY_BIT ; +int ept_lpage_level =ept_caps_to_lpage_level (vmx->nested.msrs.ept_caps); + +kvm_init_shadow_ept_mmu (vcpu, execonly, ept_lpage_level,nested_ept_ad_enabled (vcpu),nested_ept_get_eptp (vcpu)); }
Signature · 1 line
2.19.1.6.gb485710b
PATCH 8/12 KVM: VMX: Use ept_caps_to_lpage_level() in hardware_setup()
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
Using ept_caps_to_lpage_level is simpler.
--- arch/x86/kvm/vmx/vmx.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-)
arch/x86/kvm/vmx/vmx.c +2 -10
@@ -7693,7 +7693,7 @@ static __init int hardware_setup(void) {unsigned long host_bndcfgs;struct desc_ptr dt; -int r, ept_lpage_level; +int r;store_idt (&dt); host_idt_base = dt.address;
@@ -7790,16 +7790,8 @@ static __init int hardware_setup(void)kvm_mmu_set_ept_masks (enable_ept_ad_bits,cpu_has_vmx_ept_execute_only ()); -if (!enable_ept) - ept_lpage_level =0 ; -else if (cpu_has_vmx_ept_1g_page ()) - ept_lpage_level =PG_LEVEL_1G ; -else if (cpu_has_vmx_ept_2m_page ()) - ept_lpage_level =PG_LEVEL_2M ; -else - ept_lpage_level =PG_LEVEL_4K ;kvm_configure_mmu (enable_ept,0 ,vmx_get_max_tdp_level (), - ept_lpage_level); +ept_caps_to_lpage_level (vmx_capability.ept));/* * Only enable PML when hardware supports PML feature, and both EPT
Signature · 1 line
2.19.1.6.gb485710b
PATCH 9/12 KVM: X86: Rename gpte_is_8_bytes to has_4_byte_gpte and invert the direction
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
It's a bit more confusing to make gpte_is_8_bytes=1 if there are no guest PTEs at all. And has_4_byte_gpte is only changed to be only set when there are guest PTEs and the guest PTE size is 4 bytes. So when nonpaping, the value is not inverted, it is still false.
--- Documentation/virt/kvm/mmu.rst | 8 ++++---- arch/x86/include/asm/kvm_host.h | 8 ++++---- arch/x86/kvm/mmu/mmu.c | 12 ++++++------ arch/x86/kvm/mmu/mmutrace.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-)
Documentation/virt/kvm/mmu.rst +4 -4
@@ -161,7 +161,7 @@ Shadow pages contain the following information: If clear, this page corresponds to a guest page table denoted by the gfn field. role.quadrant: - When role.gpte_is_8_bytes=0, the guest uses 32-bit gptes while the host uses 64-bit + When role.has_4_byte_gpte=1, the guest uses 32-bit gptes while the host uses 64-bit sptes. That means a guest page table contains more ptes than the host, so multiple shadow pages are needed to shadow one guest page. For first-level shadow pages, role.quadrant can be 0 or 1 and denotes the
@@ -177,9 +177,9 @@ Shadow pages contain the following information: The page is invalid and should not be used. It is a root page that is currently pinned (by a cpu hardware register pointing to it); once it is unpinned it will be destroyed. - role.gpte_is_8_bytes: - Reflects the size of the guest PTE for which the page is valid, i.e. '1' - if 64-bit gptes are in use, '0' if 32-bit gptes are in use. + role.has_4_byte_gpte: + Reflects the size of the guest PTE for which the page is valid, i.e. '0' + if direct map or 64-bit gptes are in use, '1' if 32-bit gptes are in use. role.efer_nx: Contains the value of efer.nx for which the page is valid. role.cr0_wp:
arch/x86/include/asm/kvm_host.h +4 -4
@@ -296,14 +296,14 @@ struct kvm_kernel_irq_routing_entry;* * - invalid shadow pages are not accounted, so the bits are effectively 18 * -* - quadrant will only be used if gpte_is_8_bytes=0 (non-PAE paging); +* - quadrant will only be used if has_4_byte_gpte=1 (non-PAE paging); * execonly and ad_disabled are only used for nested EPT which has -* gpte_is_8_bytes=1. Therefore, 2 bits are always unused. +* has_4_byte_gpte=0. Therefore, 2 bits are always unused. * * - the 4 bits of level are effectively limited to the values 2/3/4/5, * as 4k SPs are not tracked (allowed to go unsync). In addition non-PAE * paging has exactly one upper level, making level completely redundant -* when gpte_is_8_bytes=0. +* when has_4_byte_gpte=1. * * - on top of this, smep_andnot_wp and smap_andnot_wp are only set if * cr0_wp=0, therefore these three bits only give rise to 5 possibilities.
@@ -315,7 +315,7 @@ union kvm_mmu_page_role {u32 word;struct {unsigned level:4 ; -unsigned gpte_is_8_bytes:1 ; +unsigned has_4_byte_gpte:1 ;unsigned quadrant:2 ;unsigned direct:1 ;unsigned access:3 ;
arch/x86/kvm/mmu/mmu.c +6 -6
@@ -2077,7 +2077,7 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, role.level = level; role.direct = direct; role.access = access; -if (!direct_mmu && !role.gpte_is_8_bytes) { +if (role.has_4_byte_gpte) { quadrant = gaddr >> (PAGE_SHIFT + (PT64_PT_BITS * level)); quadrant &= (1 << ((PT32_PT_BITS -PT64_PT_BITS ) * level)) -1 ; role.quadrant = quadrant;
@@ -4727,7 +4727,7 @@ kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu, role.base.ad_disabled = (shadow_accessed_mask ==0 ); role.base.level =kvm_mmu_get_tdp_level (vcpu); role.base.direct = true; - role.base.gpte_is_8_bytes = true; + role.base.has_4_byte_gpte = false;return role; }
@@ -4772,7 +4772,7 @@ kvm_calc_shadow_root_page_role_common(struct kvm_vcpu *vcpu, role.base.smep_andnot_wp = role.ext.cr4_smep && !____is_cr0_wp (regs); role.base.smap_andnot_wp = role.ext.cr4_smap && !____is_cr0_wp (regs); - role.base.gpte_is_8_bytes =____is_cr0_pg (regs) &&____is_cr4_pae (regs); + role.base.has_4_byte_gpte =____is_cr0_pg (regs) && !____is_cr4_pae (regs);return role; }
@@ -4871,7 +4871,7 @@ kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty, role.base.smm = vcpu->arch.root_mmu.mmu_role.base.smm; role.base.level = level; - role.base.gpte_is_8_bytes = true; + role.base.has_4_byte_gpte = false; role.base.direct = false; role.base.ad_disabled = !accessed_dirty; role.base.guest_mode = true;
@@ -5155,7 +5155,7 @@ static bool detect_write_misaligned(struct kvm_mmu_page *sp, gpa_t gpa, gpa, bytes, sp->role.word); offset =offset_in_page (gpa); - pte_size = sp->role.gpte_is_8_bytes ?8 :4 ; + pte_size = sp->role.has_4_byte_gpte ?4 :8 ;/* * Sometimes, the OS only writes the last one bytes to update status
@@ -5179,7 +5179,7 @@ static u64 *get_written_sptes(struct kvm_mmu_page *sp, gpa_t gpa, int *nspte) page_offset =offset_in_page (gpa); level = sp->role.level; *nspte =1 ; -if (!sp->role.gpte_is_8_bytes) { +if (sp->role.has_4_byte_gpte) { page_offset <<=1 ;/* 32->64 */ /* * A 32-bit pde maps 4MB while the shadow pdes map
arch/x86/kvm/mmu/mmutrace.h +1 -1
@@ -35,7 +35,7 @@" %snxe %sad root %u %s%c" , \ __entry->mmu_valid_gen, \ __entry->gfn, role.level, \ - role.gpte_is_8_bytes ?8 :4 , \ + role.has_4_byte_gpte ?4 :8 , \ role.quadrant, \ role.direct ?" direct" :"" , \ access_str[role.access], \
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
@@ -165,7 +165,7 @@ static union kvm_mmu_page_role page_role_for_level(struct kvm_vcpu *vcpu, role = vcpu->arch.mmu->mmu_role.base; role.level = level; role.direct = true; - role.gpte_is_8_bytes = true; + role.has_4_byte_gpte = false; role.access =ACC_ALL ; role.ad_disabled = !shadow_accessed_mask;
Signature · 1 line
2.19.1.6.gb485710b
PATCH 10/12 KVM: X86: Remove mmu parameter from load_pdptrs()
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
It uses vcpu->arch.walk_mmu always.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/nested.c | 4 ++-- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/nested.c | 4 ++-- arch/x86/kvm/x86.c | 12 ++++++------ 5 files changed, 12 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1591,7 +1591,7 @@ void kvm_mmu_zap_all(struct kvm *kvm);void kvm_mmu_invalidate_mmio_sptes (struct kvm *kvm,u64 gen);void kvm_mmu_change_mmu_pages (struct kvm *kvm,unsigned long kvm_nr_mmu_pages); -int load_pdptrs (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu,unsigned long cr3); +int load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3);int emulator_write_phys (struct kvm_vcpu *vcpu,gpa_t gpa,const void *val,int bytes);
arch/x86/kvm/svm/nested.c +2 -2
@@ -461,7 +461,7 @@ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,return -EINVAL ;if (reload_pdptrs && !nested_npt &&is_pae_paging (vcpu) && -CC (!load_pdptrs (vcpu, vcpu->arch.walk_mmu, cr3))) +CC (!load_pdptrs (vcpu, cr3)))return -EINVAL ;if (!nested_npt)
@@ -1518,7 +1518,7 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)* the guest CR3 might be restored prior to setting the nested * state which can lead to a load of wrong PDPTRs. */ -if (CC (!load_pdptrs (vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3))) +if (CC (!load_pdptrs (vcpu, vcpu->arch.cr3)))return false;if (!nested_svm_vmrun_msrpm (svm)) {
arch/x86/kvm/svm/svm.c +1 -1
@@ -1588,7 +1588,7 @@ static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)switch (reg) {case VCPU_EXREG_PDPTR :BUG_ON (!npt_enabled); -load_pdptrs (vcpu, vcpu->arch.walk_mmu,kvm_read_cr3 (vcpu)); +load_pdptrs (vcpu,kvm_read_cr3 (vcpu));break ;default :KVM_BUG_ON (1 , vcpu->kvm);
arch/x86/kvm/vmx/nested.c +2 -2
@@ -1097,7 +1097,7 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,* must not be dereferenced. */ if (reload_pdptrs && !nested_ept &&is_pae_paging (vcpu) && -CC (!load_pdptrs (vcpu, vcpu->arch.walk_mmu, cr3))) { +CC (!load_pdptrs (vcpu, cr3))) { *entry_failure_code =ENTRY_FAIL_PDPTE ;return -EINVAL ; }
@@ -3142,7 +3142,7 @@ static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu)* the guest CR3 might be restored prior to setting the nested * state which can lead to a load of wrong PDPTRs. */ -if (CC (!load_pdptrs (vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3))) +if (CC (!load_pdptrs (vcpu, vcpu->arch.cr3)))return false; }
arch/x86/kvm/x86.c +6 -6
@@ -798,8 +798,9 @@ static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)/* * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise. */ -int load_pdptrs (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu,unsigned long cr3) +int load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3) { +struct kvm_mmu *mmu = vcpu->arch.walk_mmu;gfn_t pdpt_gfn = cr3 >>PAGE_SHIFT ;gpa_t real_gpa;int i;
@@ -887,7 +888,7 @@ int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)#endif if (!(vcpu->arch.efer &EFER_LME ) && (cr0 &X86_CR0_PG ) &&is_pae (vcpu) && ((cr0 ^ old_cr0) & pdptr_bits) && - !load_pdptrs (vcpu, vcpu->arch.walk_mmu,kvm_read_cr3 (vcpu))) + !load_pdptrs (vcpu,kvm_read_cr3 (vcpu)))return 1 ;if (!(cr0 &X86_CR0_PG ) &&kvm_read_cr4_bits (vcpu,X86_CR4_PCIDE ))
@@ -1063,8 +1064,7 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ; }else if (is_paging (vcpu) && (cr4 &X86_CR4_PAE ) && ((cr4 ^ old_cr4) & pdptr_bits) - && !load_pdptrs (vcpu, vcpu->arch.walk_mmu, -kvm_read_cr3 (vcpu))) + && !load_pdptrs (vcpu,kvm_read_cr3 (vcpu)))return 1 ;if ((cr4 &X86_CR4_PCIDE ) && !(old_cr4 &X86_CR4_PCIDE )) {
@@ -1153,7 +1153,7 @@ int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)if (kvm_vcpu_is_illegal_gpa (vcpu, cr3))return 1 ; -if (is_pae_paging (vcpu) && !load_pdptrs (vcpu, vcpu->arch.walk_mmu, cr3)) +if (is_pae_paging (vcpu) && !load_pdptrs (vcpu, cr3))return 1 ;if (cr3 !=kvm_read_cr3 (vcpu))
@@ -10553,7 +10553,7 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,if (update_pdptrs) { idx =srcu_read_lock (&vcpu->kvm->srcu);if (is_pae_paging (vcpu)) { -load_pdptrs (vcpu, vcpu->arch.walk_mmu,kvm_read_cr3 (vcpu)); +load_pdptrs (vcpu,kvm_read_cr3 (vcpu)); *mmu_reset_needed =1 ; }srcu_read_unlock (&vcpu->kvm->srcu, idx);
Signature · 1 line
2.19.1.6.gb485710b
PATCH 11/12 KVM: X86: Check root_level only in fast_pgd_switch()
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
If root_level >= 4, shadow_root_level must be >= 4 too. Checking only root_level can reduce a check.
--- arch/x86/kvm/mmu/mmu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +1 -2
@@ -4136,8 +4136,7 @@ static bool fast_pgd_switch(struct kvm_vcpu *vcpu, gpa_t new_pgd,* having to deal with PDPTEs. We may add support for 32-bit hosts/VMs * later if necessary. */ -if (mmu->shadow_root_level >=PT64_ROOT_4LEVEL && - mmu->root_level >=PT64_ROOT_4LEVEL ) +if (mmu->root_level >=PT64_ROOT_4LEVEL )return cached_root_available (vcpu, new_pgd, new_role);return false;
Signature · 1 line
2.19.1.6.gb485710b
Paolo Bonzini
maintainerRe: [PATCH 11/12] KVM: X86: Check root_level only in fast_pgd_switch()
Quoted 16 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
If
root_level>= 4,shadow_root_levelmust be >= 4 too. Checking onlyroot_levelcan reduce a check.Signed-off-by Lai Jiangshan <[email protected]>--- arch/x86/kvm/mmu/mmu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/x86/kvm/mmu/mmu.c +0
-0
arch/x86/kvm/mmu/mmu.c static bool fast_pgd_switch(struct kvm_vcpu *vcpu, gpa_t new_pgd, · line 4142
Quoted 9 lines
arch/x86/kvm/mmu/mmu.c +1
-2@@ -4136,8 +4136,7 @@ static bool fast_pgd_switch(struct kvm_vcpu *vcpu, gpa_t new_pgd,* having to deal with PDPTEs. We may add support for 32-bit hosts/VMs * later if necessary. */ -if (mmu->shadow_root_level >=PT64_ROOT_4LEVEL && - mmu->root_level >=PT64_ROOT_4LEVEL ) +if (mmu->root_level >=PT64_ROOT_4LEVEL )return cached_root_available (vcpu, new_pgd, new_role);return false;
Hmm, I think this is more confusing. I *think* that adding support for PAE would be mostly an issue with the guest PDPTRs, and not with the shadow PDPTRs, but without thinking more about it I'm leaning towards not applying this patch.
Paolo
PATCH 12/12 KVM: X86: Walk shadow page starting with shadow_root_level
In reply to Lai JiangshanFrom: Lai Jiangshan <[email protected]>
Walking from the root page of the shadow page table should start with the level of the shadow page table: shadow_root_level.
Also change a small defect in audit_mappings(), it is believed that the current walking level is more valuable to print.
--- arch/x86/kvm/mmu/mmu_audit.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/mmu_audit.c +2 -3
@@ -63,7 +63,7 @@ static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)hpa_t root = vcpu->arch.mmu->root_hpa; sp =to_shadow_page (root); -__mmu_spte_walk (vcpu, sp, fn, vcpu->arch.mmu->root_level); +__mmu_spte_walk (vcpu, sp, fn, vcpu->arch.mmu->shadow_root_level);return ; }
@@ -119,8 +119,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, - hpa, *sptep); +"ent %llxn" , level, pfn, hpa, *sptep); }static void inspect_spte_has_rmap (struct kvm *kvm,u64 *sptep)
Signature · 1 line
2.19.1.6.gb485710b
Re: [PATCH 12/12] KVM: X86: Walk shadow page starting with shadow_root_level
Quoted 28 lines · Lai Jiangshan
From: Lai Jiangshan <[email protected]>
Walking from the root page of the shadow page table should start with the level of the shadow page table:
shadow_root_level.Also change a small defect in
audit_mappings(), it is believed that the current walking level is more valuable to print.Signed-off-by Lai Jiangshan <[email protected]>--- arch/x86/kvm/mmu/mmu_audit.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)arch/x86/kvm/mmu/mmu_audit.c +1
-1@@ -63,7 +63,7 @@ static void mmu_spte_walk(struct kvm_vcpu *vcpu, inspect_spte_fn fn)hpa_t root = vcpu->arch.mmu->root_hpa; sp =to_shadow_page (root); -__mmu_spte_walk (vcpu, sp, fn, vcpu->arch.mmu->root_level); +__mmu_spte_walk (vcpu, sp, fn, vcpu->arch.mmu->shadow_root_level);return ; }
arch/x86/kvm/mmu/mmu_audit.c static void audit_mappings(struct kvm_vcpu *vcpu, u64 *sptep, int level) · line 125
Quoted 9 lines
arch/x86/kvm/mmu/mmu_audit.c +1
-2@@ -119,8 +119,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, - hpa, *sptep); +"ent %llxn" , level, pfn, hpa, *sptep); }static void inspect_spte_has_rmap (struct kvm *kvm,u64 *sptep)
Queued all except patch 11, thanks.
Paolo