Commit b7cccd39 authored by Ben Gardon's avatar Ben Gardon Committed by Paolo Bonzini

KVM: x86/mmu: Fast invalidation for TDP MMU

Provide a real mechanism for fast invalidation by marking roots as
invalid so that their reference count will quickly fall to zero
and they will be torn down.

One negative side affect of this approach is that a vCPU thread will
likely drop the last reference to a root and be saddled with the work of
tearing down an entire paging structure. This issue will be resolved in
a later commit.
Signed-off-by: default avatarBen Gardon <bgardon@google.com>
Message-Id: <20210401233736.638171-13-bgardon@google.com>
[Move the loop to tdp_mmu.c, otherwise compilation fails on 32-bit. - Paolo]
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 24ae4cfa
...@@ -5421,6 +5421,15 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm) ...@@ -5421,6 +5421,15 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)
*/ */
kvm->arch.mmu_valid_gen = kvm->arch.mmu_valid_gen ? 0 : 1; kvm->arch.mmu_valid_gen = kvm->arch.mmu_valid_gen ? 0 : 1;
/* In order to ensure all threads see this change when
* handling the MMU reload signal, this must happen in the
* same critical section as kvm_reload_remote_mmus, and
* before kvm_zap_obsolete_pages as kvm_zap_obsolete_pages
* could drop the MMU lock and yield.
*/
if (is_tdp_mmu_enabled(kvm))
kvm_tdp_mmu_invalidate_all_roots(kvm);
/* /*
* Notify all vcpus to reload its shadow page table and flush TLB. * Notify all vcpus to reload its shadow page table and flush TLB.
* Then all vcpus will switch to new shadow page table with the new * Then all vcpus will switch to new shadow page table with the new
...@@ -5433,9 +5442,6 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm) ...@@ -5433,9 +5442,6 @@ static void kvm_mmu_zap_all_fast(struct kvm *kvm)
kvm_zap_obsolete_pages(kvm); kvm_zap_obsolete_pages(kvm);
if (is_tdp_mmu_enabled(kvm))
kvm_tdp_mmu_zap_all(kvm);
write_unlock(&kvm->mmu_lock); write_unlock(&kvm->mmu_lock);
} }
......
...@@ -797,6 +797,23 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm) ...@@ -797,6 +797,23 @@ void kvm_tdp_mmu_zap_all(struct kvm *kvm)
kvm_flush_remote_tlbs(kvm); kvm_flush_remote_tlbs(kvm);
} }
/*
* Mark each TDP MMU root as invalid so that other threads
* will drop their references and allow the root count to
* go to 0.
*
* This has essentially the same effect for the TDP MMU
* as updating mmu_valid_gen does for the shadow MMU.
*/
void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm)
{
struct kvm_mmu_page *root;
lockdep_assert_held_write(&kvm->mmu_lock);
list_for_each_entry(root, &kvm->arch.tdp_mmu_roots, link)
root->role.invalid = true;
}
/* /*
* Installs a last-level SPTE to handle a TDP page fault. * Installs a last-level SPTE to handle a TDP page fault.
* (NPT/EPT violation/misconfiguration) * (NPT/EPT violation/misconfiguration)
......
...@@ -10,6 +10,9 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu); ...@@ -10,6 +10,9 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu);
__must_check static inline bool kvm_tdp_mmu_get_root(struct kvm *kvm, __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm *kvm,
struct kvm_mmu_page *root) struct kvm_mmu_page *root)
{ {
if (root->role.invalid)
return false;
return refcount_inc_not_zero(&root->tdp_mmu_root_count); return refcount_inc_not_zero(&root->tdp_mmu_root_count);
} }
...@@ -43,7 +46,9 @@ static inline bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp) ...@@ -43,7 +46,9 @@ static inline bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
return __kvm_tdp_mmu_zap_gfn_range(kvm, kvm_mmu_page_as_id(sp), return __kvm_tdp_mmu_zap_gfn_range(kvm, kvm_mmu_page_as_id(sp),
sp->gfn, end, false, false, false); sp->gfn, end, false, false, false);
} }
void kvm_tdp_mmu_zap_all(struct kvm *kvm); void kvm_tdp_mmu_zap_all(struct kvm *kvm);
void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm);
int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,
int map_writable, int max_level, kvm_pfn_t pfn, int map_writable, int max_level, kvm_pfn_t pfn,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment