Commit 83cdb568 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: x86/mmu: Differentiate between nr zapped and list unstable

The return value of kvm_mmu_prepare_zap_page() has evolved to become
overloaded to convey two separate pieces of information.  1) was at
least one page zapped and 2) has the list of MMU pages become unstable.

In it's original incarnation (as kvm_mmu_zap_page()), there was no
return value at all.  Commit 07385413 ("KVM: MMU: awareness of new
kvm_mmu_zap_page behaviour") added a return value in preparation for
commit 4731d4c7 ("KVM: MMU: out of sync shadow core").  Although
the return value was of type 'int', it was actually used as a boolean
to indicate whether or not active_mmu_pages may have become unstable due
to zapping children.  Walking a list with list_for_each_entry_safe()
only protects against deleting/moving the current entry, i.e. zapping a
child page would break iteration due to modifying any number of entries.

Later, commit 60c8aec6 ("KVM: MMU: use page array in unsync walk")
modified mmu_zap_unsync_children() to return an approximation of the
number of children zapped.  This was not intentional, it was simply a
side effect of how the code was written.

The unintented side affect was then morphed into an actual feature by
commit 77662e00 ("KVM: MMU: fix kvm_mmu_zap_page() and its calling
path"), which modified kvm_mmu_change_mmu_pages() to use the number of
zapped pages when determining the number of MMU pages in use by the VM.

Finally, commit 54a4f023 ("KVM: MMU: make kvm_mmu_zap_page() return
the number of pages it actually freed") added the initial page to the
return value to make its behavior more consistent with what most users
would expect.  Incorporating the initial parent page in the return value
of kvm_mmu_zap_page() breaks the original usage of restarting a list
walk on a non-zero return value to handle a potentially unstable list,
i.e. walks will unnecessarily restart when any page is zapped.

Fix this by restoring the original behavior of kvm_mmu_zap_page(), i.e.
return a boolean to indicate that the list may be unstable and move the
number of zapped children to a dedicated parameter.  Since the majority
of callers to kvm_mmu_prepare_zap_page() don't care about either return
value, preserve the current definition of kvm_mmu_prepare_zap_page() by
making it a wrapper of a new helper, __kvm_mmu_prepare_zap_page().  This
avoids having to update every call site and also provides cleaner code
for functions that only care about the number of pages zapped.

Fixes: 54a4f023 ("KVM: MMU: make kvm_mmu_zap_page() return
                      the number of pages it actually freed")
Signed-off-by: default avatarSean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent ea145aac
...@@ -2200,7 +2200,7 @@ static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp) ...@@ -2200,7 +2200,7 @@ static void kvm_unlink_unsync_page(struct kvm *kvm, struct kvm_mmu_page *sp)
--kvm->stat.mmu_unsync; --kvm->stat.mmu_unsync;
} }
static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
struct list_head *invalid_list); struct list_head *invalid_list);
static void kvm_mmu_commit_zap_page(struct kvm *kvm, static void kvm_mmu_commit_zap_page(struct kvm *kvm,
struct list_head *invalid_list); struct list_head *invalid_list);
...@@ -2669,17 +2669,22 @@ static int mmu_zap_unsync_children(struct kvm *kvm, ...@@ -2669,17 +2669,22 @@ static int mmu_zap_unsync_children(struct kvm *kvm,
return zapped; return zapped;
} }
static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, static bool __kvm_mmu_prepare_zap_page(struct kvm *kvm,
struct list_head *invalid_list) struct kvm_mmu_page *sp,
struct list_head *invalid_list,
int *nr_zapped)
{ {
int ret; bool list_unstable;
trace_kvm_mmu_prepare_zap_page(sp); trace_kvm_mmu_prepare_zap_page(sp);
++kvm->stat.mmu_shadow_zapped; ++kvm->stat.mmu_shadow_zapped;
ret = mmu_zap_unsync_children(kvm, sp, invalid_list); *nr_zapped = mmu_zap_unsync_children(kvm, sp, invalid_list);
kvm_mmu_page_unlink_children(kvm, sp); kvm_mmu_page_unlink_children(kvm, sp);
kvm_mmu_unlink_parents(kvm, sp); kvm_mmu_unlink_parents(kvm, sp);
/* Zapping children means active_mmu_pages has become unstable. */
list_unstable = *nr_zapped;
if (!sp->role.invalid && !sp->role.direct) if (!sp->role.invalid && !sp->role.direct)
unaccount_shadowed(kvm, sp); unaccount_shadowed(kvm, sp);
...@@ -2687,7 +2692,7 @@ static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, ...@@ -2687,7 +2692,7 @@ static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
kvm_unlink_unsync_page(kvm, sp); kvm_unlink_unsync_page(kvm, sp);
if (!sp->root_count) { if (!sp->root_count) {
/* Count self */ /* Count self */
ret++; (*nr_zapped)++;
list_move(&sp->link, invalid_list); list_move(&sp->link, invalid_list);
kvm_mod_used_mmu_pages(kvm, -1); kvm_mod_used_mmu_pages(kvm, -1);
} else { } else {
...@@ -2698,7 +2703,16 @@ static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp, ...@@ -2698,7 +2703,16 @@ static int kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
} }
sp->role.invalid = 1; sp->role.invalid = 1;
return ret; return list_unstable;
}
static bool kvm_mmu_prepare_zap_page(struct kvm *kvm, struct kvm_mmu_page *sp,
struct list_head *invalid_list)
{
int nr_zapped;
__kvm_mmu_prepare_zap_page(kvm, sp, invalid_list, &nr_zapped);
return nr_zapped;
} }
static void kvm_mmu_commit_zap_page(struct kvm *kvm, static void kvm_mmu_commit_zap_page(struct kvm *kvm,
...@@ -5830,13 +5844,14 @@ void kvm_mmu_zap_all(struct kvm *kvm) ...@@ -5830,13 +5844,14 @@ void kvm_mmu_zap_all(struct kvm *kvm)
{ {
struct kvm_mmu_page *sp, *node; struct kvm_mmu_page *sp, *node;
LIST_HEAD(invalid_list); LIST_HEAD(invalid_list);
int ign;
spin_lock(&kvm->mmu_lock); spin_lock(&kvm->mmu_lock);
restart: restart:
list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) { list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) {
if (sp->role.invalid && sp->root_count) if (sp->role.invalid && sp->root_count)
continue; continue;
if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list) || if (__kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list, &ign) ||
cond_resched_lock(&kvm->mmu_lock)) cond_resched_lock(&kvm->mmu_lock))
goto restart; goto restart;
} }
...@@ -5849,13 +5864,14 @@ static void kvm_mmu_zap_mmio_sptes(struct kvm *kvm) ...@@ -5849,13 +5864,14 @@ static void kvm_mmu_zap_mmio_sptes(struct kvm *kvm)
{ {
struct kvm_mmu_page *sp, *node; struct kvm_mmu_page *sp, *node;
LIST_HEAD(invalid_list); LIST_HEAD(invalid_list);
int ign;
spin_lock(&kvm->mmu_lock); spin_lock(&kvm->mmu_lock);
restart: restart:
list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) { list_for_each_entry_safe(sp, node, &kvm->arch.active_mmu_pages, link) {
if (!sp->mmio_cached) if (!sp->mmio_cached)
continue; continue;
if (kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list) || if (__kvm_mmu_prepare_zap_page(kvm, sp, &invalid_list, &ign) ||
cond_resched_lock(&kvm->mmu_lock)) cond_resched_lock(&kvm->mmu_lock))
goto restart; goto restart;
} }
......
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