KVM: x86/mmu: Don't step down in the TDP iterator when zapping all SPTEs

Set the min_level for the TDP iterator at the root level when zapping all
SPTEs to optimize the iterator's try_step_down(). Zapping a non-leaf
SPTE will recursively zap all its children, thus there is no need for the
iterator to attempt to step down. This avoids rereading the top-level
SPTEs after they are zapped by causing try_step_down() to short-circuit.

In most cases, optimizing try_step_down() will be in the noise as the cost
of zapping SPTEs completely dominates the overall time. The optimization
is however helpful if the zap occurs with relatively few SPTEs, e.g. if KVM
is zapping in response to multiple memslot updates when userspace is adding
and removing read-only memslots for option ROMs. In that case, the task
doing the zapping likely isn't a vCPU thread, but it still holds mmu_lock
for read and thus can be a noisy neighbor of sorts.

Reviewed-by: Ben Gardon <bgardon@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20210812181414.3376143-3-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by Sean Christopherson and committed by Paolo Bonzini 0103098f 524a1e4e

Changed files
+8 -1
arch
x86
kvm
mmu
+8 -1
arch/x86/kvm/mmu/tdp_mmu.c
··· 728 struct tdp_iter iter; 729 730 /* 731 * Bound the walk at host.MAXPHYADDR, guest accesses beyond that will 732 * hit a #PF(RSVD) and never get to an EPT Violation/Misconfig / #NPF, 733 * and so KVM will never install a SPTE for such addresses. ··· 744 745 rcu_read_lock(); 746 747 - tdp_root_for_each_pte(iter, root, start, end) { 748 retry: 749 if (can_yield && 750 tdp_mmu_iter_cond_resched(kvm, &iter, flush, shared)) {
··· 728 struct tdp_iter iter; 729 730 /* 731 + * No need to try to step down in the iterator when zapping all SPTEs, 732 + * zapping the top-level non-leaf SPTEs will recurse on their children. 733 + */ 734 + int min_level = zap_all ? root->role.level : PG_LEVEL_4K; 735 + 736 + /* 737 * Bound the walk at host.MAXPHYADDR, guest accesses beyond that will 738 * hit a #PF(RSVD) and never get to an EPT Violation/Misconfig / #NPF, 739 * and so KVM will never install a SPTE for such addresses. ··· 738 739 rcu_read_lock(); 740 741 + for_each_tdp_pte_min_level(iter, root->spt, root->role.level, 742 + min_level, start, end) { 743 retry: 744 if (can_yield && 745 tdp_mmu_iter_cond_resched(kvm, &iter, flush, shared)) {