]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: riscv: Rely on common MMU notifier locking
authorJinyu Tang <tjytimi@163.com>
Sun, 17 May 2026 15:34:23 +0000 (23:34 +0800)
committerAnup Patel <anup@brainfault.org>
Wed, 3 Jun 2026 12:49:53 +0000 (18:19 +0530)
The common KVM invalidation paths call kvm_unmap_gfn_range() with
mmu_lock already held for write.

For the standard MMU notifier path, the call chain is:

  kvm_mmu_notifier_invalidate_range_start()
    kvm_handle_hva_range()
      kvm_unmap_gfn_range()

kvm_mmu_notifier_invalidate_range_start() leaves range.lockless clear.
kvm_handle_hva_range() therefore takes KVM_MMU_LOCK(kvm) before invoking
the handler.

The guest_memfd path has the same locking contract:

  __kvm_gmem_invalidate_begin()
    kvm_mmu_unmap_gfn_range()
      kvm_unmap_gfn_range()

__kvm_gmem_invalidate_begin() explicitly takes KVM_MMU_LOCK(kvm) before
calling kvm_mmu_unmap_gfn_range().

So remove the local trylock and make the common locking contract explicit
with lockdep_assert_held_write() like x86.

Signed-off-by: Jinyu Tang <tjytimi@163.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
Link: https://lore.kernel.org/r/20260517153427.94889-2-tjytimi@163.com
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/kvm/mmu.c

index 8469ed9324211e507a83e274f88d8938c58b6c2c..da944cb684045c2653ac826e928dcffea7d864aa 100644 (file)
@@ -245,22 +245,17 @@ out:
 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
 {
        struct kvm_gstage gstage;
-       bool mmu_locked;
        bool flush;
 
        if (!kvm->arch.pgd)
                return false;
 
-       kvm_riscv_gstage_init(&gstage, kvm);
-       mmu_locked = spin_trylock(&kvm->mmu_lock);
+       lockdep_assert_held_write(&kvm->mmu_lock);
 
+       kvm_riscv_gstage_init(&gstage, kvm);
        flush = kvm_riscv_gstage_unmap_range(&gstage, range->start << PAGE_SHIFT,
                                             (range->end - range->start) << PAGE_SHIFT,
                                             range->may_block);
-
-       if (mmu_locked)
-               spin_unlock(&kvm->mmu_lock);
-
        if (flush)
                kvm_flush_remote_tlbs_range(kvm, range->start,
                                            range->end - range->start);