>From b023d3ada9c856cbfe38839068861e5f2e19489f Mon Sep 17 00:00:00 2001 From: Sean Christopherson Date: Tue, 15 Oct 2024 16:33:58 -0700 Subject: [PATCH 2/5] KVM: pfncache: Wait for in-progress invalidations to complete during refresh 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 Signed-off-by: Sean Christopherson --- 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