[PATCH 6.18.y 06/17] LoongArch: KVM: Flush TLBs *before* dropping mmu_lock
From: Paolo Bonzini
Date: Fri Dec 04 2026 - 13:01:39 EST
From: Kyle Zeng <kylebot@xxxxxxxxxx>
[ Upstream commit 42c613825bf235a07d112e50255c14f479814c98 ]
When flushing/unmapping stage-2 mappings in response to a memslot updates
(including the "flush everything" when a VM is being destroyed), flush TLBs
*before* dropping mmu_lock, as KVM relies on the stage-2 mappings being
completely unreachable prior to dropping mmu_lock.
E.g. if a MMU notifier invalidation event runs after mmu_lock is dropped,
but before TLBs are flushed, then the MMU notifier task can observe the
missing PTE and return without issuing its own TLB flush, resulting in a
use-after-free (if the to-be-invalidated page was freed or moved).
Fixes: 752e2cd7b4fb ("LoongArch: KVM: Implement kvm mmu operations")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kyle Zeng <kylebot@xxxxxxxxxx>
[sean: rewrite changelog]
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261119172647.64295-3-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/loongarch/kvm/mmu.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/arch/loongarch/kvm/mmu.c b/arch/loongarch/kvm/mmu.c
index 4ca7c6ea8f08..d2af576802a4 100644
--- a/arch/loongarch/kvm/mmu.c
+++ b/arch/loongarch/kvm/mmu.c
@@ -300,18 +300,16 @@ static void kvm_flush_range(struct kvm *kvm, gfn_t start_gfn, gfn_t end_gfn, int
kvm_ptw_prepare(kvm, &ctx);
INIT_LIST_HEAD(&ctx.list);
- if (lock) {
+ if (lock)
spin_lock(&kvm->mmu_lock);
- ret = kvm_ptw_top(kvm->arch.pgd, start_gfn << PAGE_SHIFT,
- end_gfn << PAGE_SHIFT, &ctx);
- spin_unlock(&kvm->mmu_lock);
- } else
- ret = kvm_ptw_top(kvm->arch.pgd, start_gfn << PAGE_SHIFT,
- end_gfn << PAGE_SHIFT, &ctx);
+ ret = kvm_ptw_top(kvm->arch.pgd, start_gfn << PAGE_SHIFT,
+ end_gfn << PAGE_SHIFT, &ctx);
/* Flush vpid for each vCPU individually */
if (ret)
kvm_flush_remote_tlbs(kvm);
+ if (lock)
+ spin_unlock(&kvm->mmu_lock);
/*
* free pte table page after mmu_lock
--
2.56.1