Skip to content

Commit a89ecbb

Browse files
yamahatabonzini
authored andcommitted
KVM: x86/tdp_mmu: Take root types for kvm_tdp_mmu_invalidate_all_roots()
Rename kvm_tdp_mmu_invalidate_all_roots() to kvm_tdp_mmu_invalidate_roots(), and make it enum kvm_tdp_mmu_root_types as an argument. kvm_tdp_mmu_invalidate_roots() is called with different root types. For kvm_mmu_zap_all_fast() it only operates on shared roots. But when tearing down a VM it needs to invalidate all roots. Have the callers only invalidate the required roots instead of all roots. Within kvm_tdp_mmu_invalidate_roots(), respect the root type passed by checking the root type in root iterator. Suggested-by: Chao Gao <chao.gao@intel.com> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Message-ID: <20240718211230.1492011-17-rick.p.edgecombe@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 94faba8 commit a89ecbb

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6454,8 +6454,13 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)
64546454
* write and in the same critical section as making the reload request,
64556455
* e.g. before kvm_zap_obsolete_pages() could drop mmu_lock and yield.
64566456
*/
6457-
if (tdp_mmu_enabled)
6458-
kvm_tdp_mmu_invalidate_all_roots(kvm);
6457+
if (tdp_mmu_enabled) {
6458+
/*
6459+
* External page tables don't support fast zapping, therefore
6460+
* their mirrors must be invalidated separately by the caller.
6461+
*/
6462+
kvm_tdp_mmu_invalidate_roots(kvm, KVM_DIRECT_ROOTS);
6463+
}
64596464

64606465
/*
64616466
* Notify all vcpus to reload its shadow page table and flush TLB.

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void kvm_mmu_uninit_tdp_mmu(struct kvm *kvm)
3737
* for zapping and thus puts the TDP MMU's reference to each root, i.e.
3838
* ultimately frees all roots.
3939
*/
40-
kvm_tdp_mmu_invalidate_all_roots(kvm);
40+
kvm_tdp_mmu_invalidate_roots(kvm, KVM_VALID_ROOTS);
4141
kvm_tdp_mmu_zap_invalidated_roots(kvm, false);
4242

4343
WARN_ON(atomic64_read(&kvm->arch.tdp_mmu_pages));
@@ -1070,10 +1070,18 @@ void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm, bool shared)
10701070
* Note, kvm_tdp_mmu_zap_invalidated_roots() is gifted the TDP MMU's reference.
10711071
* See kvm_tdp_mmu_alloc_root().
10721072
*/
1073-
void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)
1073+
void kvm_tdp_mmu_invalidate_roots(struct kvm *kvm,
1074+
enum kvm_tdp_mmu_root_types root_types)
10741075
{
10751076
struct kvm_mmu_page *root;
10761077

1078+
/*
1079+
* Invalidating invalid roots doesn't make sense, prevent developers from
1080+
* having to think about it.
1081+
*/
1082+
if (WARN_ON_ONCE(root_types & KVM_INVALID_ROOTS))
1083+
root_types &= ~KVM_INVALID_ROOTS;
1084+
10771085
/*
10781086
* mmu_lock must be held for write to ensure that a root doesn't become
10791087
* invalid while there are active readers (invalidating a root while
@@ -1095,6 +1103,9 @@ void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)
10951103
* or get/put references to roots.
10961104
*/
10971105
list_for_each_entry(root, &kvm->arch.tdp_mmu_roots, link) {
1106+
if (!tdp_mmu_root_match(root, root_types))
1107+
continue;
1108+
10981109
/*
10991110
* Note, invalid roots can outlive a memslot update! Invalid
11001111
* roots must be *zapped* before the memslot update completes,

arch/x86/kvm/mmu/tdp_mmu.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ static inline struct kvm_mmu_page *tdp_mmu_get_root(struct kvm_vcpu *vcpu,
6666
bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, gfn_t start, gfn_t end, bool flush);
6767
bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp);
6868
void kvm_tdp_mmu_zap_all(struct kvm *kvm);
69-
void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm);
69+
void kvm_tdp_mmu_invalidate_roots(struct kvm *kvm,
70+
enum kvm_tdp_mmu_root_types root_types);
7071
void kvm_tdp_mmu_zap_invalidated_roots(struct kvm *kvm, bool shared);
7172

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

0 commit comments

Comments
 (0)