Commit fb101293 authored by Ben Gardon's avatar Ben Gardon Committed by Paolo Bonzini

KVM: x86/mmu: handle cmpxchg failure in kvm_tdp_mmu_get_root

To reduce dependence on the MMU write lock, don't rely on the assumption
that the atomic operation in kvm_tdp_mmu_get_root will always succeed.
By not relying on that assumption, threads do not need to hold the MMU
lock in write mode in order to take a reference on a TDP MMU root.

In the root iterator, this change means that some roots might have to be
skipped if they are found to have a zero refcount. This will still never
happen as of this patch, but a future patch will need that flexibility to
make the root iterator safe under the MMU read lock.
Signed-off-by: default avatarBen Gardon <bgardon@google.com>
Message-Id: <20210401233736.638171-8-bgardon@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 11cccf5c
...@@ -88,10 +88,12 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm, ...@@ -88,10 +88,12 @@ static struct kvm_mmu_page *tdp_mmu_next_root(struct kvm *kvm,
next_root = list_first_entry(&kvm->arch.tdp_mmu_roots, next_root = list_first_entry(&kvm->arch.tdp_mmu_roots,
typeof(*next_root), link); typeof(*next_root), link);
while (!list_entry_is_head(next_root, &kvm->arch.tdp_mmu_roots, link) &&
!kvm_tdp_mmu_get_root(kvm, next_root))
next_root = list_next_entry(next_root, link);
if (list_entry_is_head(next_root, &kvm->arch.tdp_mmu_roots, link)) if (list_entry_is_head(next_root, &kvm->arch.tdp_mmu_roots, link))
next_root = NULL; next_root = NULL;
else
kvm_tdp_mmu_get_root(kvm, next_root);
if (prev_root) if (prev_root)
kvm_tdp_mmu_put_root(kvm, prev_root); kvm_tdp_mmu_put_root(kvm, prev_root);
...@@ -161,10 +163,9 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu) ...@@ -161,10 +163,9 @@ hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu)
/* Check for an existing root before allocating a new one. */ /* Check for an existing root before allocating a new one. */
for_each_tdp_mmu_root(kvm, root, kvm_mmu_role_as_id(role)) { for_each_tdp_mmu_root(kvm, root, kvm_mmu_role_as_id(role)) {
if (root->role.word == role.word) { if (root->role.word == role.word &&
kvm_tdp_mmu_get_root(kvm, root); kvm_tdp_mmu_get_root(kvm, root))
goto out; goto out;
}
} }
root = alloc_tdp_mmu_page(vcpu, 0, vcpu->arch.mmu->shadow_root_level); root = alloc_tdp_mmu_page(vcpu, 0, vcpu->arch.mmu->shadow_root_level);
......
...@@ -7,17 +7,10 @@ ...@@ -7,17 +7,10 @@
hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu); hpa_t kvm_tdp_mmu_get_vcpu_root_hpa(struct kvm_vcpu *vcpu);
static inline void kvm_tdp_mmu_get_root(struct kvm *kvm, __must_check static inline bool kvm_tdp_mmu_get_root(struct kvm *kvm,
struct kvm_mmu_page *root) struct kvm_mmu_page *root)
{ {
lockdep_assert_held_write(&kvm->mmu_lock); return refcount_inc_not_zero(&root->tdp_mmu_root_count);
/*
* This should never fail since roots are removed from the roots
* list under the MMU write lock when their reference count falls
* to zero.
*/
refcount_inc_not_zero(&root->tdp_mmu_root_count);
} }
void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root); void kvm_tdp_mmu_put_root(struct kvm *kvm, struct kvm_mmu_page *root);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment