From: Lai Jiangshan <jiangshan.ljs@xxxxxxxxxxxx> When NPT enabled L1 is PAE paging, vcpu->arch.mmu->get_pdptrs() which is nested_svm_get_tdp_pdptr() reads the guest NPT's PDPTE from memroy unconditionally for each call. The guest PAE root page is not write-protected. The mmu->get_pdptrs() in FNAME(walk_addr_generic) might get different values every time or it is different from the return value of mmu->get_pdptrs() in mmu_alloc_shadow_roots(). And it will cause FNAME(fetch) installs the spte in a wrong sp or links a sp to a wrong parent since FNAME(gpte_changed) can't check these kind of changes. Cache the PDPTEs and the problem is resolved. The guest is responsible to info the host if its PAE root page is updated which will cause nested vmexit and the host updates the cache when next nested run. The commit e4e517b4be01 ("KVM: MMU: Do not unconditionally read PDPTE from guest memory") fixs the same problem for non-nested case. Fixes: e4e517b4be01 ("KVM: MMU: Do not unconditionally read PDPTE from guest memory") Signed-off-by: Lai Jiangshan <jiangshan.ljs@xxxxxxxxxxxx> --- arch/x86/kvm/svm/nested.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index d736ec6514ca..a34983d2dc07 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -72,18 +72,22 @@ static void svm_inject_page_fault_nested(struct kvm_vcpu *vcpu, struct x86_excep } } -static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) +static void nested_svm_cache_tdp_pdptrs(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); u64 cr3 = svm->nested.ctl.nested_cr3; - u64 pdpte; + u64 *pdptrs = vcpu->arch.mmu->pdptrs; int ret; - ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), &pdpte, - offset_in_page(cr3) + index * 8, 8); + ret = kvm_vcpu_read_guest_page(vcpu, gpa_to_gfn(cr3), pdptrs, + offset_in_page(cr3), 8 * 4); if (ret) - return 0; - return pdpte; + memset(pdptrs, 0, 8 * 4); +} + +static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) +{ + return vcpu->arch.mmu->pdptrs[index]; } static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) @@ -109,6 +113,8 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4, svm->vmcb01.ptr->save.efer, svm->nested.ctl.nested_cr3); + if (vcpu->arch.mmu->root_level == PT32E_ROOT_LEVEL) + nested_svm_cache_tdp_pdptrs(vcpu); vcpu->arch.mmu->get_guest_pgd = 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; -- 2.19.1.6.gb485710b