[PATCH 2/5] KVM: pfncache: Wait for in-progress invalidations to complete during refresh

From: Sean Christopherson
Date: Tue Oct 15 2024 - 19:33:58 EST


When refreshing a gpc, wait for in-progress invalidations to complete
before reading the sequence counter and resolving the pfn. Resolving the
pfn when there is an in-progress invalidation is worse than pointless, as
the pfn is guaranteed to be discarded, and trying to resolve the pfn could
contended for resources, e.g. mmap_lock, and make the invalidation and
thus refresh take longer to complete.

Suggested-by: David Woodhouse <dwmw@xxxxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
virt/kvm/pfncache.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c
index 4afbc1262e3f..957f739227ab 100644
--- a/virt/kvm/pfncache.c
+++ b/virt/kvm/pfncache.c
@@ -194,6 +194,18 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc)
cond_resched();
}

+ /*
+ * Wait for in-progress invalidations to complete if the user
+ * already being invalidated. Unlike the page fault path, this
+ * task _must_ complete the refresh, i.e. there's no value in
+ * trying to race ahead in the hope that a different task makes
+ * the cache valid.
+ */
+ while (READ_ONCE(gpc->kvm->mn_active_invalidate_count)) {
+ if (!cond_resched())
+ cpu_relax();
+ }
+
mmu_seq = gpc->kvm->mmu_invalidate_seq;
smp_rmb();

--
2.47.0.rc1.288.g06298d1525-goog


--oDMvAmx7QP3BxXt9
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
filename="0003-KVM-pfncache-Implement-range-based-invalidation-chec.patch"