Skip to content

Commit 35be969

Browse files
rpedgecobonzini
authored andcommitted
KVM: x86/mmu: Zap invalid roots with mmu_lock holding for write at uninit
Prepare for a future TDX patch which asserts that atomic zapping (i.e. zapping with mmu_lock taken for read) don't operate on mirror roots. When tearing down a VM, all roots have to be zapped (including mirror roots once they're in place) so do that with the mmu_lock taken for write. kvm_mmu_uninit_tdp_mmu() is invoked either before or after executing any atomic operations on SPTEs by vCPU threads. Therefore, it will not impact vCPU threads performance if kvm_tdp_mmu_zap_invalidated_roots() acquires mmu_lock for write to zap invalid roots. Co-developed-by: Yan Zhao <[email protected]> Signed-off-by: Yan Zhao <[email protected]> Signed-off-by: Rick Edgecombe <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 67b4303 commit 35be969

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6467,7 +6467,7 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)
64676467
* lead to use-after-free.
64686468
*/
64696469
if (tdp_mmu_enabled)
6470-
kvm_tdp_mmu_zap_invalidated_roots(kvm);
6470+
kvm_tdp_mmu_zap_invalidated_roots(kvm, true);
64716471
}
64726472

64736473
void kvm_mmu_init_vm(struct kvm *kvm)

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)
3838
* ultimately frees all roots.
3939
*/
4040
kvm_tdp_mmu_invalidate_all_roots(kvm);
41-
kvm_tdp_mmu_zap_invalidated_roots(kvm);
41+
kvm_tdp_mmu_zap_invalidated_roots(kvm, false);
4242

4343
WARN_ON(atomic64_read(&kvm->arch.tdp_mmu_pages));
4444
WARN_ON(!list_empty(&kvm->arch.tdp_mmu_roots));
@@ -883,11 +883,14 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm)
883883
* Zap all invalidated roots to ensure all SPTEs are dropped before the "fast
884884
* zap" completes.
885885
*/
886-
void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)
886+
void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm, bool shared)
887887
{
888888
struct kvm_mmu_page *root;
889889

890-
read_lock(&kvm->mmu_lock);
890+
if (shared)
891+
read_lock(&kvm->mmu_lock);
892+
else
893+
write_lock(&kvm->mmu_lock);
891894

892895
for_each_tdp_mmu_root_yield_safe(kvm, root) {
893896
if (!root->tdp_mmu_scheduled_root_to_zap)
@@ -905,7 +908,7 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)
905908
* that may be zapped, as such entries are associated with the
906909
* ASID on both VMX and SVM.
907910
*/
908-
tdp_mmu_zap_root(kvm, root, true);
911+
tdp_mmu_zap_root(kvm, root, shared);
909912

910913
/*
911914
* The referenced needs to be put *after* zapping the root, as
@@ -915,7 +918,10 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm)
915918
kvm_tdp_mmu_put_root(kvm, root);
916919
}
917920

918-
read_unlock(&kvm->mmu_lock);
921+
if (shared)
922+
read_unlock(&kvm->mmu_lock);
923+
else
924+
write_unlock(&kvm->mmu_lock);
919925
}
920926

921927
/*

arch/x86/kvm/mmu/tdp_mmu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, gfn_t start, gfn_t end, bool flush);
2323
bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp);
2424
void kvm_tdp_mmu_zap_all(struct kvm *kvm);
2525
void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm);
26-
void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm);
26+
void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm, bool shared);
2727

2828
int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault);
2929

0 commit comments

Comments
 (0)