KVM: Fix mmu_notifier vs. pfncache race
Sean Christopherson
reviewercover KVM: Fix mmu_notifier vs. pfncache race
Fix a race between mmu_notifier invalidation and pfncache refresh, and then fix another race of sorts within pfncache that exists because of the hacky approach pfncache currently employs to try and handle races with mmu_notifiers.
Both issues were found by inspection and not proven on hardware.
Sean Christopherson (2):
KVM: Fix race between mmu_notifier invalidation and pfncache refresh
KVM: Do not speculatively mark pfn cache valid to "fix" race
virt/kvm/kvm_main.c | 9 ++++++ virt/kvm/pfncache.c | 79 +++++++++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 27 deletions(-)
base-commit: 150866cd0ec871c765181d145aa0912628289c8a
Signature · 1 line
2.36.0.rc0.470.gd361397f0d-goog
Sean Christopherson
reviewerPATCH 1/2 KVM: Fix race between mmu_notifier invalidation and pfncache refresh
Key off of mn_active_invalidate_count to detect that a pfncache refresh needs to wait for an in-progress mmu_notifier invalidation. Using the common "retry" approach is broken as the invalidation occurs _before_ mmu_notifier_count is elevated and before mmu_notifier_range_start is set/updated. The cache refresh handles the race where an invalidation occurs between dropping and re-acquiring gpc->lock, but it doesn't handle the scenario where the cache is refreshed after the cache was invalidated by the notifier, but before the notifier elevates mmu_notifier_count.
CPU0 CPU1
---- ----
gfn_to_pfn_cache_invalidate_start()
|
-> gpc->valid = false;
kvm_gfn_to_pfn_cache_refresh()
|
|-> gpc->valid = true; hva_to_pfn_retry()
|
-> acquire kvm->mmu_lock
kvm->mmu_notifier_count == 0
mmu_seq == kvm->mmu_notifier_seq
drop kvm->mmu_lock
return pfn 'X'
acquire kvm->mmu_lock
kvm_inc_notifier_count()
drop kvm->mmu_lock()
kernel frees pfn 'X'
kvm_gfn_to_pfn_cache_check()
|
|-> gpc->valid == true
caller accesses freed pfn 'X'While mn_active_invalidate_count is not guaranteed to be stable, it is guaranteed to be elevated prior to an invalidation acquiring gpc->lock, so either the refresh will see an active invalidation and wait, or the invalidation will run after the refresh completes.
Waiting for in-progress invalidations to complete also obviates the need to mark gpc->valid before calling hva_to_pfn_retry(), which is in itself flawed as a concurrent kvm_gfn_to_pfn_cache_check() would see a "valid" cache with garbage pfn/khva values. That issue will be remedied in a future commit.
Opportunistically change the do-while(1) to a for(;;) to make it easier to see that it loops until a break condition is encountered, the size of the loop body makes the while(1) rather difficult to see.
--- virt/kvm/kvm_main.c | 9 ++++++ virt/kvm/pfncache.c | 70 +++++++++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 21 deletions(-)
virt/kvm/kvm_main.c +9 -0
@@ -705,6 +705,15 @@ static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn, kvm->mn_active_invalidate_count++;spin_unlock (&kvm->mn_invalidate_lock); +/* +* Invalidate pfn caches _before_ invalidating the secondary MMUs, i.e. +* before acquiring mmu_lock, to avoid holding mmu_lock while acquiring +* each cache's lock. There are relatively few caches in existence at +* any given time, and the caches themselves can check for hva overlap, +* i.e. don't need to rely on memslot overlap checks for performance. +* Because this runs without holding mmu_lock, the pfn caches must use +* mn_active_invalidate_count (see above) instead of mmu_notifier_count. +*/ gfn_to_pfn_cache_invalidate_start (kvm, range->start, range->end, hva_range.may_block);
virt/kvm/pfncache.c +49 -21
110 lines not on this page · raw message
Signature · 1 line
2.36.0.rc0.470.gd361397f0d-goog
Sean Christopherson
reviewerPATCH 2/2 KVM: Do not speculatively mark pfn cache valid to "fix" race
In reply to Sean ChristophersonWhen refreshing a stale pfn cache, mark it valid only after the new pfn (and maybe kernel mapping) has been acquired. Speculatively marking the cache as valid with garbage pfn/khva values can lead to other tasks using the garbage values. Presumably, the cache was previously marked valid before dropping gpc->lock to address a race where an mmu_notifier event could invalidate the cache between retrieving the pfn via hva_to_pfn() and re-acquiring gpc->lock. That race has been plugged by waiting for mn_active_invalidate_count to reach zero before returning from hva_to_pfn_retry(), i.e. waiting for any notifiers to fully complete before returning.
Note, this could also be "fixed" by having kvm_gfn_to_pfn_cache_check() also verify gpc->hkva, but that's unnecessary now that the aforementioned race between refresh and mmu_notifier invalidation has been eliminated.
CPU0 CPU1
---- ----
gpc->valid == false
kvm_gfn_to_pfn_cache_refresh()
|
|-> gpc->valid = true;
hva_to_pfn_retry()
|
-> drop gpc->lock acquire gpc->lock for read
kvm_gfn_to_pfn_cache_check()
|
|-> gpc->valid == true
gpc->khva == NULL
caller dereferences NULL khva
Opportunistically add a lockdep to the check() helper to make it slightly more obvious that there is no memory ordering issue when setting "valid" versus "pfn" and/or "khva".
--- virt/kvm/pfncache.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
virt/kvm/pfncache.c +3 -6
@@ -81,6 +81,8 @@ bool kvm_gfn_to_pfn_cache_check(struct kvm *kvm, struct gfn_to_pfn_cache *gpc, {struct kvm_memslots *slots =kvm_memslots (kvm); +lockdep_assert_held_read (&gpc->lock); +if ((gpa & ~PAGE_MASK ) + len >PAGE_SIZE )return false;
@@ -226,11 +228,6 @@ int kvm_gfn_to_pfn_cache_refresh(struct kvm *kvm, struct gfn_to_pfn_cache *gpc,if (!old_valid || old_uhva != gpc->uhva) {void *new_khva =NULL ; -/* Placeholders for "hva is valid but not yet mapped" */ - gpc->pfn =KVM_PFN_ERR_FAULT ; - gpc->khva =NULL ; - gpc->valid = true; - new_pfn =hva_to_pfn_retry (kvm, gpc);if (is_error_noslot_pfn (new_pfn)) { ret = -EFAULT ;
@@ -259,7 +256,7 @@ int kvm_gfn_to_pfn_cache_refresh(struct kvm *kvm, struct gfn_to_pfn_cache *gpc, gpc->pfn =KVM_PFN_ERR_FAULT ; gpc->khva =NULL ; }else { -/* At this point, gpc->valid may already have been cleared */ + gpc->valid = true; gpc->pfn = new_pfn; gpc->khva = new_khva; }
Signature · 1 line
2.36.0.rc0.470.gd361397f0d-goog
Queued, thanks.
Paolo