Since commit f47e5bbbc92f ("KVM: x86/mmu: Zap only TDP MMU leafs in zap range and mmu_notifier unmap"), kvm_tdp_mmu_unmap_gfn_range() zaps only leaf SPTEs, and so do all users of kvm_unmap_gfn_range() today, e.g. mmu_notifier invalidations, guest_memfd punches and memslot deletion without the fast zap. A 4KB page table whose whole 2MB range was unmapped stays allocated until it is replaced by a hugepage or its root is zapped, usually when the VM is destroyed. A VMM that returns guest memory with MADV_DONTNEED or fallocate(PUNCH_HOLE), e.g. for ballooning, keeps paying for page tables that map nothing: up to 12MB when shrinking a 8GB guest to 2GiB. If the range covers all of a 2MB non-leaf SPTE, zap that SPTE instead of descending into it, and let handle_removed_pt() zap the remaining leaves and free the page table via RCU. Skip invalid roots, which are being zapped anyway, and mirror roots, as KVM frees S-EPT page tables only when the TD is torn down. Hardware may walk a detached page table until the TLB is flushed, so flush before dropping RCU. This can't be left to the caller: callers unmap one memslot at a time and flush at the end, and the walk of a later memslot may yield and drop RCU with no flush pending. Unlike arm64, which frees stage-2 page tables once they become empty, this doesn't free tables emptied by 4KB unmaps, e.g. by a balloon with 4KB pages. A side effect of this patch is that invalidations that don't discard memory, e.g. NUMA hinting or mprotect(), now free covered page tables too, so the next fault in each 2MB range allocates a new one. Punching 32MB from a 4KB-mapped memfd now frees 64KB of page tables, and deleting a 64MB memslot with KVM_X86_QUIRK_SLOT_ZAP_ALL disabled frees 128KB, where both freed nothing before. Signed-off-by: Usama Arif --- arch/x86/kvm/mmu/tdp_mmu.c | 41 ++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 27e9c15bf0a7a0..8cf08a87d874fd 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -931,9 +931,11 @@ bool kvm_tdp_mmu_zap_possible_nx_huge_page(struct kvm *kvm, * operation can cause a soft lockup. */ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, - gfn_t start, gfn_t end, bool can_yield, bool flush) + gfn_t start, gfn_t end, bool can_yield, bool flush, + bool free_pts) { struct tdp_iter iter; + bool freed_pt = false; end = min(end, tdp_mmu_max_gfn_exclusive()); @@ -945,13 +947,21 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, if (can_yield && tdp_mmu_iter_cond_resched(kvm, &iter, flush, false)) { flush = false; + freed_pt = false; continue; } - if (!is_shadow_present_pte(iter.old_spte) || - !is_last_spte(iter.old_spte, iter.level)) + if (!is_shadow_present_pte(iter.old_spte)) continue; + if (!is_last_spte(iter.old_spte, iter.level)) { + if (!free_pts || root->role.invalid || + iter.level != PG_LEVEL_2M || iter.gfn < start || + iter.gfn + KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) > end) + continue; + freed_pt = true; + } + tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE); /* @@ -962,12 +972,17 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, flush = true; } - rcu_read_unlock(); - /* - * Because this flow zaps _only_ leaf SPTEs, the caller doesn't need - * to provide RCU protection as no 'struct kvm_mmu_page' will be freed. + * Zapped page tables are freed via RCU, and hardware may walk them until + * the TLB is flushed, so flush before dropping RCU. */ + if (freed_pt) { + kvm_flush_remote_tlbs(kvm); + flush = false; + } + + rcu_read_unlock(); + return flush; } @@ -982,7 +997,8 @@ bool kvm_tdp_mmu_zap_leafs(struct kvm *kvm, gfn_t start, gfn_t end, bool flush) lockdep_assert_held_write(&kvm->mmu_lock); for_each_valid_tdp_mmu_root_yield_safe(kvm, root, -1) - flush = tdp_mmu_zap_leafs(kvm, root, start, end, true, flush); + flush = tdp_mmu_zap_leafs(kvm, root, start, end, true, flush, + false); return flush; } @@ -1304,7 +1320,11 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) return ret; } -/* Used by mmu notifier via kvm_unmap_gfn_range() */ +/* + * Used by kvm_unmap_gfn_range(). Unlike kvm_zap_gfn_range(), also free the + * last-level page tables that the range fully covers, except in mirror roots, + * whose S-EPT page tables are only freed when the TD is torn down. + */ bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range, bool flush) { @@ -1315,7 +1335,8 @@ bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range, __for_each_tdp_mmu_root_yield_safe(kvm, root, range->slot->as_id, types) flush = tdp_mmu_zap_leafs(kvm, root, range->start, range->end, - range->may_block, flush); + range->may_block, flush, + !is_mirror_sp(root)); return flush; } -- 2.53.1-Meta