Skip to content

Commit 2dcfa86

Browse files
committed
KVM: x86: Check for invalid/obsolete root *after* making MMU pages available
commit-author Sean Christopherson <seanjc@google.com> commit 2abd528 Check for a "stale" page fault, i.e. for an invalid and/or obsolete root, after making MMU pages available for the shadow MMU. If reclaiming shadow pages zaps an in-use root, i.e. marks it invalid, then KVM will attempt to map memory into an invalid root. On its own, populating an invalid root is "fine", but because child shadow pages inherit their parent's role, any children created during the map/fetch will be created as invalid pages, thus violating KVM's invariant that invalid pages are never on the list of active MMU pages. Note, the underlying flaw has existed since KVM first started tracking invalid roots in 2008 (commit 2e53d63, "KVM: MMU: ignore zapped root pagetables"), but the true badness only came along in 2020 (Linux 5.9) with the invariant that invalid shadow pages can't be on the list of active pages. Note #2, inheriting role.invalid when creating child shadow pages is also far from ideal; that flaw will be addressed separately. Reported-by: Hyunwoo Kim <imv4bel@gmail.com> Fixes: f95eec9 ("KVM: x86/mmu: Don't put invalid SPs back on the list of active pages") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> (cherry picked from commit 2abd528) Signed-off-by: Brett Mastbergen <bmastbergen@ciq.com>
1 parent ce5eb3f commit 2dcfa86

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

arch/x86/kvm/mmu/mmu.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,16 +4788,17 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
47884788
if (r != RET_PF_CONTINUE)
47894789
return r;
47904790

4791-
r = RET_PF_RETRY;
47924791
write_lock(&vcpu->kvm->mmu_lock);
47934792

4794-
if (is_page_fault_stale(vcpu, fault))
4795-
goto out_unlock;
4796-
47974793
r = make_mmu_pages_available(vcpu);
47984794
if (r)
47994795
goto out_unlock;
48004796

4797+
if (is_page_fault_stale(vcpu, fault)) {
4798+
r = RET_PF_RETRY;
4799+
goto out_unlock;
4800+
}
4801+
48014802
r = direct_map(vcpu, fault);
48024803

48034804
out_unlock:

arch/x86/kvm/mmu/paging_tmpl.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -827,15 +827,17 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault
827827
}
828828
#endif
829829

830-
r = RET_PF_RETRY;
831830
write_lock(&vcpu->kvm->mmu_lock);
832831

833-
if (is_page_fault_stale(vcpu, fault))
834-
goto out_unlock;
835-
836832
r = make_mmu_pages_available(vcpu);
837833
if (r)
838834
goto out_unlock;
835+
836+
if (is_page_fault_stale(vcpu, fault)) {
837+
r = RET_PF_RETRY;
838+
goto out_unlock;
839+
}
840+
839841
r = FNAME(fetch)(vcpu, fault, &walker);
840842

841843
out_unlock:

0 commit comments

Comments
 (0)