Skip to content

Commit 66e507d

Browse files
sean-jcgregkh
authored andcommitted
KVM: x86/mmu: Remove spurious TLB flushes in TDP MMU zap collapsible path
[ Upstream commit 4b85c92 ] Drop the "flush" param and return values to/from the TDP MMU's helper for zapping collapsible SPTEs. Because the helper runs with mmu_lock held for read, not write, it uses tdp_mmu_zap_spte_atomic(), and the atomic zap handles the necessary remote TLB flush. Similarly, because mmu_lock is dropped and re-acquired between zapping legacy MMUs and zapping TDP MMUs, kvm_mmu_zap_collapsible_sptes() must handle remote TLB flushes from the legacy MMU before calling into the TDP MMU. Fixes: e220971 ("KVM: x86/mmu: Skip rmap operations if rmaps not allocated") Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20211120045046.3940942-4-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e0609b2 commit 66e507d

3 files changed

Lines changed: 11 additions & 25 deletions

File tree

‎arch/x86/kvm/mmu/mmu.c‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5855,26 +5855,21 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm,
58555855
void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
58565856
const struct kvm_memory_slot *slot)
58575857
{
5858-
bool flush;
5859-
58605858
if (kvm_memslots_have_rmaps(kvm)) {
58615859
write_lock(&kvm->mmu_lock);
58625860
/*
58635861
* Zap only 4k SPTEs since the legacy MMU only supports dirty
58645862
* logging at a 4k granularity and never creates collapsible
58655863
* 2m SPTEs during dirty logging.
58665864
*/
5867-
flush = slot_handle_level_4k(kvm, slot, kvm_mmu_zap_collapsible_spte, true);
5868-
if (flush)
5865+
if (slot_handle_level_4k(kvm, slot, kvm_mmu_zap_collapsible_spte, true))
58695866
kvm_arch_flush_remote_tlbs_memslot(kvm, slot);
58705867
write_unlock(&kvm->mmu_lock);
58715868
}
58725869

58735870
if (is_tdp_mmu_enabled(kvm)) {
58745871
read_lock(&kvm->mmu_lock);
5875-
flush = kvm_tdp_mmu_zap_collapsible_sptes(kvm, slot, false);
5876-
if (flush)
5877-
kvm_arch_flush_remote_tlbs_memslot(kvm, slot);
5872+
kvm_tdp_mmu_zap_collapsible_sptes(kvm, slot);
58785873
read_unlock(&kvm->mmu_lock);
58795874
}
58805875
}

‎arch/x86/kvm/mmu/tdp_mmu.c‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,10 +1413,9 @@ void kvm_tdp_mmu_clear_dirty_pt_masked(struct kvm *kvm,
14131413
* Clear leaf entries which could be replaced by large mappings, for
14141414
* GFNs within the slot.
14151415
*/
1416-
static bool zap_collapsible_spte_range(struct kvm *kvm,
1416+
static void zap_collapsible_spte_range(struct kvm *kvm,
14171417
struct kvm_mmu_page *root,
1418-
const struct kvm_memory_slot *slot,
1419-
bool flush)
1418+
const struct kvm_memory_slot *slot)
14201419
{
14211420
gfn_t start = slot->base_gfn;
14221421
gfn_t end = start + slot->npages;
@@ -1427,10 +1426,8 @@ static bool zap_collapsible_spte_range(struct kvm *kvm,
14271426

14281427
tdp_root_for_each_pte(iter, root, start, end) {
14291428
retry:
1430-
if (tdp_mmu_iter_cond_resched(kvm, &iter, flush, true)) {
1431-
flush = false;
1429+
if (tdp_mmu_iter_cond_resched(kvm, &iter, false, true))
14321430
continue;
1433-
}
14341431

14351432
if (!is_shadow_present_pte(iter.old_spte) ||
14361433
!is_last_spte(iter.old_spte, iter.level))
@@ -1442,6 +1439,7 @@ static bool zap_collapsible_spte_range(struct kvm *kvm,
14421439
pfn, PG_LEVEL_NUM))
14431440
continue;
14441441

1442+
/* Note, a successful atomic zap also does a remote TLB flush. */
14451443
if (!tdp_mmu_zap_spte_atomic(kvm, &iter)) {
14461444
/*
14471445
* The iter must explicitly re-read the SPTE because
@@ -1450,30 +1448,24 @@ static bool zap_collapsible_spte_range(struct kvm *kvm,
14501448
iter.old_spte = READ_ONCE(*rcu_dereference(iter.sptep));
14511449
goto retry;
14521450
}
1453-
flush = true;
14541451
}
14551452

14561453
rcu_read_unlock();
1457-
1458-
return flush;
14591454
}
14601455

14611456
/*
14621457
* Clear non-leaf entries (and free associated page tables) which could
14631458
* be replaced by large mappings, for GFNs within the slot.
14641459
*/
1465-
bool kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,
1466-
const struct kvm_memory_slot *slot,
1467-
bool flush)
1460+
void kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,
1461+
const struct kvm_memory_slot *slot)
14681462
{
14691463
struct kvm_mmu_page *root;
14701464

14711465
lockdep_assert_held_read(&kvm->mmu_lock);
14721466

14731467
for_each_tdp_mmu_root_yield_safe(kvm, root, slot->as_id, true)
1474-
flush = zap_collapsible_spte_range(kvm, root, slot, flush);
1475-
1476-
return flush;
1468+
zap_collapsible_spte_range(kvm, root, slot);
14771469
}
14781470

14791471
/*

‎arch/x86/kvm/mmu/tdp_mmu.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ void kvm_tdp_mmu_clear_dirty_pt_masked(struct kvm *kvm,
6666
struct kvm_memory_slot *slot,
6767
gfn_t gfn, unsigned long mask,
6868
bool wrprot);
69-
bool kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,
70-
const struct kvm_memory_slot *slot,
71-
bool flush);
69+
void kvm_tdp_mmu_zap_collapsible_sptes(struct kvm *kvm,
70+
const struct kvm_memory_slot *slot);
7271

7372
bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm,
7473
struct kvm_memory_slot *slot, gfn_t gfn,

0 commit comments

Comments
 (0)