Skip to content

Commit f47e5bb

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86/mmu: Zap only TDP MMU leafs in zap range and mmu_notifier unmap
Re-introduce zapping only leaf SPTEs in kvm_zap_gfn_range() and kvm_tdp_mmu_unmap_gfn_range(), this time without losing a pending TLB flush when processing multiple roots (including nested TDP shadow roots). Dropping the TLB flush resulted in random crashes when running Hyper-V Server 2019 in a guest with KSM enabled in the host (or any source of mmu_notifier invalidations, KSM is just the easiest to force). This effectively revert commits 873dd12 and fcb93eb, and thus restores commit cf3e264, plus this delta on top: bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, int as_id, gfn_t start, gfn_t end, struct kvm_mmu_page *root; for_each_tdp_mmu_root_yield_safe(kvm, root, as_id) - flush = tdp_mmu_zap_leafs(kvm, root, start, end, can_yield, false); + flush = tdp_mmu_zap_leafs(kvm, root, start, end, can_yield, flush); return flush; } Cc: Ben Gardon <bgardon@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com> Tested-by: Vitaly Kuznetsov <vkuznets@redhat.com> Message-Id: <20220325230348.2587437-1-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent a80ced6 commit f47e5bb

3 files changed

Lines changed: 19 additions & 48 deletions

File tree

arch/x86/kvm/mmu/mmu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5849,8 +5849,8 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end)
58495849

58505850
if (is_tdp_mmu_enabled(kvm)) {
58515851
for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
5852-
flush = kvm_tdp_mmu_zap_gfn_range(kvm, i, gfn_start,
5853-
gfn_end, flush);
5852+
flush = kvm_tdp_mmu_zap_leafs(kvm, i, gfn_start,
5853+
gfn_end, true, flush);
58545854
}
58555855

58565856
if (flush)

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -909,71 +909,48 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
909909
}
910910

911911
/*
912-
* Tears down the mappings for the range of gfns, [start, end), and frees the
913-
* non-root pages mapping GFNs strictly within that range. Returns true if
914-
* SPTEs have been cleared and a TLB flush is needed before releasing the
915-
* MMU lock.
912+
* Zap leafs SPTEs for the range of gfns, [start, end). Returns true if SPTEs
913+
* have been cleared and a TLB flush is needed before releasing the MMU lock.
916914
*
917915
* If can_yield is true, will release the MMU lock and reschedule if the
918916
* scheduler needs the CPU or there is contention on the MMU lock. If this
919917
* function cannot yield, it will not release the MMU lock or reschedule and
920918
* the caller must ensure it does not supply too large a GFN range, or the
921919
* operation can cause a soft lockup.
922920
*/
923-
static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
924-
gfn_t start, gfn_t end, bool can_yield, bool flush)
921+
static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root,
922+
gfn_t start, gfn_t end, bool can_yield, bool flush)
925923
{
926-
bool zap_all = (start == 0 && end >= tdp_mmu_max_gfn_host());
927924
struct tdp_iter iter;
928925

929-
/*
930-
* No need to try to step down in the iterator when zapping all SPTEs,
931-
* zapping the top-level non-leaf SPTEs will recurse on their children.
932-
*/
933-
int min_level = zap_all ? root->role.level : PG_LEVEL_4K;
934-
935926
end = min(end, tdp_mmu_max_gfn_host());
936927

937928
lockdep_assert_held_write(&kvm->mmu_lock);
938929

939930
rcu_read_lock();
940931

941-
for_each_tdp_pte_min_level(iter, root, min_level, start, end) {
932+
for_each_tdp_pte_min_level(iter, root, PG_LEVEL_4K, start, end) {
942933
if (can_yield &&
943934
tdp_mmu_iter_cond_resched(kvm, &iter, flush, false)) {
944935
flush = false;
945936
continue;
946937
}
947938

948-
if (!is_shadow_present_pte(iter.old_spte))
949-
continue;
950-
951-
/*
952-
* If this is a non-last-level SPTE that covers a larger range
953-
* than should be zapped, continue, and zap the mappings at a
954-
* lower level, except when zapping all SPTEs.
955-
*/
956-
if (!zap_all &&
957-
(iter.gfn < start ||
958-
iter.gfn + KVM_PAGES_PER_HPAGE(iter.level) > end) &&
939+
if (!is_shadow_present_pte(iter.old_spte) ||
959940
!is_last_spte(iter.old_spte, iter.level))
960941
continue;
961942

962943
tdp_mmu_set_spte(kvm, &iter, 0);
963944
flush = true;
964945
}
965946

966-
/*
967-
* Need to flush before releasing RCU. TODO: do it only if intermediate
968-
* page tables were zapped; there is no need to flush under RCU protection
969-
* if no 'struct kvm_mmu_page' is freed.
970-
*/
971-
if (flush)
972-
kvm_flush_remote_tlbs_with_address(kvm, start, end - start);
973-
974947
rcu_read_unlock();
975948

976-
return false;
949+
/*
950+
* Because this flow zaps _only_ leaf SPTEs, the caller doesn't need
951+
* to provide RCU protection as no 'struct kvm_mmu_page' will be freed.
952+
*/
953+
return flush;
977954
}
978955

979956
/*
@@ -982,13 +959,13 @@ static bool zap_gfn_range(struct kvm *kvm, struct kvm_mmu_page *root,
982959
* SPTEs have been cleared and a TLB flush is needed before releasing the
983960
* MMU lock.
984961
*/
985-
bool __kvm_tdp_mmu_zap_gfn_range(struct kvm *kvm, int as_id, gfn_t start,
986-
gfn_t end, bool can_yield, bool flush)
962+
bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, int as_id, gfn_t start, gfn_t end,
963+
bool can_yield, bool flush)
987964
{
988965
struct kvm_mmu_page *root;
989966

990967
for_each_tdp_mmu_root_yield_safe(kvm, root, as_id)
991-
flush = zap_gfn_range(kvm, root, start, end, can_yield, flush);
968+
flush = tdp_mmu_zap_leafs(kvm, root, start, end, can_yield, flush);
992969

993970
return flush;
994971
}
@@ -1236,8 +1213,8 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)
12361213
bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range,
12371214
bool flush)
12381215
{
1239-
return __kvm_tdp_mmu_zap_gfn_range(kvm, range->slot->as_id, range->start,
1240-
range->end, range->may_block, flush);
1216+
return kvm_tdp_mmu_zap_leafs(kvm, range->slot->as_id, range->start,
1217+
range->end, range->may_block, flush);
12411218
}
12421219

12431220
typedef bool (*tdp_handler_t)(struct kvm *kvm, struct tdp_iter *iter,

arch/x86/kvm/mmu/tdp_mmu.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@ __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm_mmu_page *root)
1515
void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root,
1616
bool shared);
1717

18-
bool __kvm_tdp_mmu_zap_gfn_range(struct kvm *kvm, int as_id, gfn_t start,
18+
bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, int as_id, gfn_t start,
1919
gfn_t end, bool can_yield, bool flush);
20-
static inline bool kvm_tdp_mmu_zap_gfn_range(struct kvm *kvm, int as_id,
21-
gfn_t start, gfn_t end, bool flush)
22-
{
23-
return __kvm_tdp_mmu_zap_gfn_range(kvm, as_id, start, end, true, flush);
24-
}
25-
2620
bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp);
2721
void kvm_tdp_mmu_zap_all(struct kvm *kvm);
2822
void kvm_tdp_mmu_invalidate_all_roots(struct kvm *kvm);

0 commit comments

Comments
 (0)