Skip to content

Commit d746182

Browse files
committed
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>
1 parent 99b85fd commit d746182

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,19 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm,
171171
* Holding mmu_lock for write obviates the need for RCU protection as the list
172172
* is guaranteed to be stable.
173173
*/
174-
#define for_each_tdp_mmu_root(_kvm, _root, _as_id) \
174+
#define __for_each_tdp_mmu_root(_kvm, _root, _as_id, _only_valid) \
175175
list_for_each_entry(_root, &_kvm->arch.tdp_mmu_roots, link) \
176176
if (kvm_lockdep_assert_mmu_lock_held(_kvm, false) && \
177-
_as_id >= 0 && kvm_mmu_page_as_id(_root) != _as_id) { \
177+
((_as_id >= 0 && kvm_mmu_page_as_id(_root) != _as_id) || \
178+
((_only_valid) && (_root)->role.invalid))) { \
178179
} else
179180

181+
#define for_each_tdp_mmu_root(_kvm, _root, _as_id) \
182+
__for_each_tdp_mmu_root(_kvm, _root, _as_id, false)
183+
184+
#define for_each_valid_tdp_mmu_root(_kvm, _root, _as_id) \
185+
__for_each_tdp_mmu_root(_kvm, _root, _as_id, true)
186+
180187
static struct kvm_mmu_page *tdp_mmu_alloc_sp(struct kvm_vcpu *vcpu)
181188
{
182189
struct kvm_mmu_page *sp;
@@ -224,11 +231,8 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)
224231

225232
lockdep_assert_held_write(&kvm->mmu_lock);
226233

227-
/*
228-
* Check for an existing root before allocating a new one. Note, the
229-
* role check prevents consuming an invalid root.
230-
*/
231-
for_each_tdp_mmu_root(kvm, root, kvm_mmu_role_as_id(role)) {
234+
/* Check for an existing root before allocating a new one. */
235+
for_each_valid_tdp_mmu_root(kvm, root, kvm_mmu_role_as_id(role)) {
232236
if (root->role.word == role.word &&
233237
kvm_tdp_mmu_get_root(root))
234238
goto out;
@@ -1639,7 +1643,7 @@ void kvm_tdp_mmu_clear_dirty_pt_masked(struct kvm *kvm,
16391643
{
16401644
struct kvm_mmu_page *root;
16411645

1642-
for_each_tdp_mmu_root(kvm, root, slot->as_id)
1646+
for_each_valid_tdp_mmu_root(kvm, root, slot->as_id)
16431647
clear_dirty_pt_masked(kvm, root, gfn, mask, wrprot);
16441648
}
16451649

@@ -1757,7 +1761,7 @@ bool kvm_tdp_mmu_write_protect_gfn(struct kvm *kvm,
17571761
bool spte_set = false;
17581762

17591763
lockdep_assert_held_write(&kvm->mmu_lock);
1760-
for_each_tdp_mmu_root(kvm, root, slot->as_id)
1764+
for_each_valid_tdp_mmu_root(kvm, root, slot->as_id)
17611765
spte_set |= write_protect_gfn(kvm, root, gfn, min_level);
17621766

17631767
return spte_set;

0 commit comments

Comments
 (0)