Skip to content

Commit 46c10ad

Browse files
committed
KVM: Assert that mmu_invalidate_in_progress *never* goes negative
Move the assertion on the in-progress invalidation count from the primary MMU's notifier path to KVM's common notification path, i.e. assert that the count doesn't go negative even when the invalidation is coming from KVM itself. Opportunistically convert the assertion to a KVM_BUG_ON(), i.e. kill only the affected VM, not the entire kernel. A corrupted count is fatal to the VM, e.g. the non-zero (negative) count will cause mmu_invalidate_retry() to block any and all attempts to install new mappings. But it's far from guaranteed that an end() without a start() is fatal or even problematic to anything other than the target VM, e.g. the underlying bug could simply be a duplicate call to end(). And it's much more likely that a missed invalidation, i.e. a potential use-after-free, would manifest as no notification whatsoever, not an end() without a start(). Link: https://lore.kernel.org/r/20230921203331.3746712-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7af66fb commit 46c10ad

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

virt/kvm/kvm_main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ void kvm_mmu_invalidate_end(struct kvm *kvm)
870870
* in conjunction with the smp_rmb in mmu_invalidate_retry().
871871
*/
872872
kvm->mmu_invalidate_in_progress--;
873+
KVM_BUG_ON(kvm->mmu_invalidate_in_progress < 0, kvm);
873874

874875
/*
875876
* Assert that at least one range must be added between start() and
@@ -906,8 +907,6 @@ static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
906907
*/
907908
if (wake)
908909
rcuwait_wake_up(&kvm->mn_memslots_update_rcuwait);
909-
910-
BUG_ON(kvm->mmu_invalidate_in_progress < 0);
911910
}
912911

913912
static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,

0 commit comments

Comments
 (0)