KVM: x86/mmu: Skip invalid TDP MMU roots when write-protecting SPTEs
authorSean Christopherson <seanjc@google.com>
Thu, 11 Jan 2024 02:00:45 +0000 (18:00 -0800)
committerSean Christopherson <seanjc@google.com>
Fri, 23 Feb 2024 00:28:45 +0000 (16:28 -0800)
commitd746182337c205660fd4d8eaa5fdc4f4e8320b9a
treea6c3f25fc3fc490492b90a6c17d12baa9771b506
parent99b85fda91b164b91a0d4e0aae376f32dc38d59c
KVM: x86/mmu: Skip invalid TDP MMU roots when write-protecting SPTEs

When write-protecting SPTEs, don't process invalid roots as invalid roots
are unreachable, i.e. can't be used to access guest memory and thus don't
need to be write-protected.

Note, this is *almost* a nop for kvm_tdp_mmu_clear_dirty_pt_masked(),
which is called under slots_lock, i.e. is mutually exclusive with
kvm_mmu_zap_all_fast().  But it's possible for something other than the
"fast zap" thread to grab a reference to an invalid root and thus keep a
root alive (but completely empty) after kvm_mmu_zap_all_fast() completes.

The kvm_tdp_mmu_write_protect_gfn() case is more interesting as KVM write-
protects SPTEs for reasons other than dirty logging, e.g. if a KVM creates
a SPTE for a nested VM while a fast zap is in-progress.

Add another TDP MMU iterator to visit only valid roots, and
opportunistically convert kvm_tdp_mmu_get_vcpu_root_hpa() to said iterator.

Link: https://lore.kernel.org/r/20240111020048.844847-6-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/mmu/tdp_mmu.c