KVM: MMU: update sp->gfns on pte update path
If an emulated pte write modifies the gpa of a present spte, sp->gfns is not updated, retaining a stale value which later leads to:
rmap_remove: ffff8807d245fff8 0->BUG
------------[ cut here ]------------ kernel BUG at arch/x86/kvm/mmu.c:695!
Fix by updating sp->gfns even if spte was present.
Resolves: https://bugzilla.kernel.org/show_bug.cgi?id=27052
KVM-Stable-Tag.
arch/x86/kvm/mmu.c +6 -0
@@ -2054,6 +2054,12 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, rmap_count =rmap_add (vcpu, sptep, gfn);if (rmap_count >RMAP_RECYCLE_THRESHOLD )rmap_recycle (vcpu, sptep, gfn); + }else { +struct kvm_mmu_page *sp =page_header (__pa (sptep)); +int index = sptep - sp->spt; + +if (!sp->role.direct && sp->gfns[index] != gfn) + sp->gfns[index] = gfn; }kvm_release_pfn_clean (pfn);if (speculative) {
Quoted 21 lines · Marcelo Tosatti
If an emulated pte write modifies the gpa of a present spte,
sp->gfnsis not updated, retaining a stale value which later leads to:
rmap_remove:ffff8807d245fff80->BUG------------[ cut here ]------------ kernel BUG at arch/x86/kvm/mmu.c:695!Fix by updating
sp->gfnseven if spte was present.Resolves: https://bugzilla.kernel.org/show_bug.cgi?id=27052
Reported-and-tested-by Nicolas Prochazka<[email protected]>KVM-Stable-Tag.
arch/x86/kvm/mmu.c +0
-0@@ -2054,6 +2054,12 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, rmap_count =rmap_add (vcpu, sptep, gfn);if (rmap_count>RMAP_RECYCLE_THRESHOLD )
arch/x86/kvm/mmu.c static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, · line 2065
Quoted 10 lines
arch/x86/kvm/mmu.c +6
-0rmap_recycle (vcpu, sptep, gfn); + }else { +struct kvm_mmu_page *sp =page_header (__pa (sptep)); +int index = sptep - sp->spt; + +if (!sp->role.direct&& sp->gfns[index] != gfn) + sp->gfns[index] = gfn; }kvm_release_pfn_clean (pfn);if (speculative) {
Should be done by a call to kvm_mmu_page_set_gfn(). But I don't understand how it could become inconsistent in the first place.
if (is_rmap_spte(*sptep)) {
/*
* If we overwrite a PTE page pointer with a 2MB PMD, unlink
* the parent of the now unreachable PTE.
*/
if (level > PT_PAGE_TABLE_LEVEL &&
!is_large_pte(*sptep)) {
struct kvm_mmu_page *child;
u64 pte = *sptep;
child = page_header(pte & PT64_BASE_ADDR_MASK);
mmu_page_remove_parent_pte(child, sptep);
__set_spte(sptep, shadow_trap_nonpresent_pte);
kvm_flush_remote_tlbs(vcpu->kvm);
} else if (pfn != spte_to_pfn(*sptep)) {
pgprintk("hfn old %llx new %llx\n",
spte_to_pfn(*sptep), pfn);
drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
kvm_flush_remote_tlbs(vcpu->kvm);
} else
was_rmapped = 1;
}If we set was_rmapped, that means rmap_add() was previously called for this spte/gfn/pfn pair, and all that changes is permissions, no?
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 59 lines · Avi Kivity
Quoted 21 lines · Marcelo Tosatti
If an emulated pte write modifies the gpa of a present spte,
sp->gfnsis not updated, retaining a stale value which later leads to:
rmap_remove:ffff8807d245fff80->BUG------------[ cut here ]------------ kernel BUG at arch/x86/kvm/mmu.c:695!Fix by updating
sp->gfnseven if spte was present.Resolves: https://bugzilla.kernel.org/show_bug.cgi?id=27052
Reported-and-tested-by Nicolas Prochazka<[email protected]>KVM-Stable-Tag.
arch/x86/kvm/mmu.c +0
-0@@ -2054,6 +2054,12 @@ static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, rmap_count =rmap_add (vcpu, sptep, gfn);if (rmap_count>RMAP_RECYCLE_THRESHOLD )arch/x86/kvm/mmu.c static void mmu_set_spte(struct kvm_vcpu *vcpu, u64 *sptep, · line 2065
Quoted 10 lines
arch/x86/kvm/mmu.c +6
-0rmap_recycle (vcpu, sptep, gfn); + }else { +struct kvm_mmu_page *sp =page_header (__pa (sptep)); +int index = sptep - sp->spt; + +if (!sp->role.direct&& sp->gfns[index] != gfn) + sp->gfns[index] = gfn; }kvm_release_pfn_clean (pfn);if (speculative) {Should be done by a call to
kvm_mmu_page_set_gfn(). But I don't understand how it could become inconsistent in the first place.if (is_rmap_spte(*sptep)) { /* * If we overwrite a PTE page pointer with a 2MB PMD, unlink * the parent of the now unreachable PTE. */ if (level > PT_PAGE_TABLE_LEVEL && !is_large_pte(*sptep)) { struct kvm_mmu_page *child; u64 pte = *sptep; child = page_header(pte & PT64_BASE_ADDR_MASK); mmu_page_remove_parent_pte(child, sptep); __set_spte(sptep, shadow_trap_nonpresent_pte); kvm_flush_remote_tlbs(vcpu->kvm); } else if (pfn != spte_to_pfn(*sptep)) { pgprintk("hfn old %llx new %llx\n", spte_to_pfn(*sptep), pfn); drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); kvm_flush_remote_tlbs(vcpu->kvm); } else was_rmapped = 1; }
Quoted 2 lines
If we set
was_rmapped, that meansrmap_add()was previously called for this spte/gfn/pfn pair, and all that changes is permissions, no?
What if pfn is the same but gfn differs?
Quoted 27 lines · Marcelo Tosatti
Quoted 27 lines
Should be done by a call to
kvm_mmu_page_set_gfn(). But I don't understand how it could become inconsistent in the first place.if (is_rmap_spte(*sptep)) { /* * If we overwrite a PTE page pointer with a 2MB PMD, unlink * the parent of the now unreachable PTE. */ if (level> PT_PAGE_TABLE_LEVEL&& !is_large_pte(*sptep)) { struct kvm_mmu_page *child; u64 pte = *sptep; child = page_header(pte& PT64_BASE_ADDR_MASK); mmu_page_remove_parent_pte(child, sptep); __set_spte(sptep, shadow_trap_nonpresent_pte); kvm_flush_remote_tlbs(vcpu->kvm); } else if (pfn != spte_to_pfn(*sptep)) { pgprintk("hfn old %llx new %llx\n", spte_to_pfn(*sptep), pfn); drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); kvm_flush_remote_tlbs(vcpu->kvm); } else was_rmapped = 1; }
Quoted 4 lines
Quoted 2 lines
If we set
was_rmapped, that meansrmap_add()was previously called for this spte/gfn/pfn pair, and all that changes is permissions, no?What if pfn is the same but gfn differs?
Could be. Any way to verify if this was the case?
Isn't it nicer to have it detected by the test above and do the drop_spte()/kvm_flush_remote_tlbs() thing instead?
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 31 lines · Avi Kivity
Quoted 30 lines · Marcelo Tosatti
Quoted 29 lines
Should be done by a call to
kvm_mmu_page_set_gfn(). But I don't understand how it could become inconsistent in the first place.if (is_rmap_spte(*sptep)) { /* * If we overwrite a PTE page pointer with a 2MB PMD, unlink * the parent of the now unreachable PTE. */ if (level> PT_PAGE_TABLE_LEVEL&& !is_large_pte(*sptep)) { struct kvm_mmu_page *child; u64 pte = *sptep; child = page_header(pte& PT64_BASE_ADDR_MASK); mmu_page_remove_parent_pte(child, sptep); __set_spte(sptep, shadow_trap_nonpresent_pte); kvm_flush_remote_tlbs(vcpu->kvm); } else if (pfn != spte_to_pfn(*sptep)) { pgprintk("hfn old %llx new %llx\n", spte_to_pfn(*sptep), pfn); drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte); kvm_flush_remote_tlbs(vcpu->kvm); } else was_rmapped = 1; }If we set
was_rmapped, that meansrmap_add()was previously called for this spte/gfn/pfn pair, and all that changes is permissions, no?
Quoted 6 lines
Quoted 1 line
What if pfn is the same but gfn differs?
Could be. Any way to verify if this was the case?
Isn't it nicer to have it detected by the test above and do the drop_spte()/kvm_flush_remote_tlbs() thing instead?
It could not be the case. If spte is updated to point to a new gfn, and rmap is not updated:
1. rmap[A] = spte
sp->gfns[i] = A
spte points to gfn A
2. rmap[A] = spte
sp->gfns[i] = A
spte points to gfn B
rmap_remove(spte) will succeed (as in not crash). In case gfn A's slot is removed, all shadow pages will be destroyed. So what can fail from this point on are operations on gfn B such as rmap_write_protect(B).
Yes, its nicer (and correct) to do it at drop_spte. Will resubmit.
However, still have no explanation for Nicolas BUG's... ideas?