kvm_arch_flush_shadow_all() is called from the MMU notifier ->release() callback, which can run concurrently from __mmu_notifier_release() and mmu_notifier_unregister() when the last mm and KVM fd references are dropped at the same time. Calling kvm_uninit_stage2_mmu() there invokes kvm_mmu_free_memory_cache() without locking, which is not thread-safe and can double-free split_page_cache. Remove kvm_uninit_stage2_mmu() and call kvm_free_stage2_pgd() directly in kvm_arch_flush_shadow_all() and on the kvm_arch_init_vm() error path (where split_page_cache is still empty). Keep freeing both the stage-2 PGD and split_page_cache in kvm_arch_destroy_vm(), which runs once after mmu_notifier_unregister() completes and all VM references are gone (see commit a3ca3bfd01b7 ("KVM: arm64: Destroy stage-2 page-table in kvm_arch_destroy_vm()")). Fixes: e7bf7a490c68 ("KVM: arm64: Split huge pages when dirty logging is enabled") Cc: # 6.6.x: a3ca3bfd01b7: KVM: arm64: Destroy stage-2 page-table in kvm_arch_destroy_vm() Cc: # 6.6.x Suggested-by: Wei-Lin Chang Signed-off-by: Aashish Sharma --- Changes in v2: - Remove kvm_uninit_stage2_mmu() completely (including its prototype in kvm_mmu.h) and inline its remaining callers, as suggested by Wei-Lin Chang. - Drop the inaccurate claim in the commit message that VM ioctls can race with ->release() (pointed out by Wei-Lin Chang). - Link to v1: https://lore.kernel.org/all/20261124224959.685002-1-aashish@aashishsharma.net/ arch/arm64/include/asm/kvm_mmu.h | 1 - arch/arm64/kvm/arm.c | 5 +++-- arch/arm64/kvm/mmu.c | 6 ------ arch/arm64/kvm/nested.c | 2 +- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h index 32392abf2e21..69de28228c7f 100644 --- a/arch/arm64/include/asm/kvm_mmu.h +++ b/arch/arm64/include/asm/kvm_mmu.h @@ -189,7 +189,6 @@ void kvm_stage2_wp_range(struct kvm_s2_mmu *mmu, phys_addr_t addr, phys_addr_t e void stage2_unmap_vm(struct kvm *kvm); int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long type); -void kvm_uninit_stage2_mmu(struct kvm *kvm); void kvm_free_stage2_pgd(struct kvm_s2_mmu *mmu); int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, phys_addr_t pa, unsigned long size, bool writable); diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 157bf4b81202..9dfa1a81e5c2 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -303,7 +303,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) return 0; err_uninit_mmu: - kvm_uninit_stage2_mmu(kvm); + kvm_free_stage2_pgd(&kvm->arch.mmu); kvm_destroy_nested(kvm); err_free_cpumask: free_cpumask_var(kvm->arch.supported_cpus); @@ -354,7 +354,8 @@ void kvm_arch_destroy_vm(struct kvm *kvm) if (is_protected_kvm_enabled()) pkvm_destroy_hyp_vm(kvm); - kvm_uninit_stage2_mmu(kvm); + kvm_free_stage2_pgd(&kvm->arch.mmu); + kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache); kvm_destroy_mpidr_data(kvm); kfree(kvm->arch.sysreg_masks); diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 1db10d8d6db7..8610c9d091c1 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1117,12 +1117,6 @@ int kvm_init_stage2_mmu(struct kvm *kvm, struct kvm_s2_mmu *mmu, unsigned long t return err; } -void kvm_uninit_stage2_mmu(struct kvm *kvm) -{ - kvm_free_stage2_pgd(&kvm->arch.mmu); - kvm_mmu_free_memory_cache(&kvm->arch.mmu.split_page_cache); -} - static void stage2_unmap_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot) { diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c index 4d83281a7a28..2440ad9fed87 100644 --- a/arch/arm64/kvm/nested.c +++ b/arch/arm64/kvm/nested.c @@ -1472,7 +1472,7 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm) if (!WARN_ON(atomic_read(&mmu->refcnt))) kvm_free_stage2_pgd(mmu); } - kvm_uninit_stage2_mmu(kvm); + kvm_free_stage2_pgd(&kvm->arch.mmu); } /* base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f -- 2.56.1.911.g883d8bcb20-goog