From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx> Add slots_lock around kvm_flush_shadow_all(). kvm_gmem_release() via fput() and kvm_mmu_notifier_release() via mmput() can be called simultaneously on process exit because vhost, /dev/vhost_{net, vsock}, can delay the call to release mmu_notifier, kvm_mmu_notifier_release() by its kernel thread. Vhost uses get_task_mm() and mmput() for the kernel thread to access process memory. mmput() can defer after closing the file. kvm_flush_shadow_all() and kvm_gmem_release() can be called simultaneously. With TDX KVM, HKID releasing by kvm_flush_shadow_all() and private memory releasing by kvm_gmem_release() can race. Add slots_lock to kvm_mmu_notifier_release(). Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx> --- virt/kvm/kvm_main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 49380cd62367..4855d0b7a859 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -927,9 +927,16 @@ static void kvm_mmu_notifier_release(struct mmu_notifier *mn, struct kvm *kvm = mmu_notifier_to_kvm(mn); int idx; + /* + * Avoide race with kvm_gmem_release(). + * This function is called via mmu notifier, mmu_release(). + * kvm_gmem_release() is called via fput() on process exit. + */ + mutex_lock(&kvm->slots_lock); idx = srcu_read_lock(&kvm->srcu); kvm_flush_shadow_all(kvm); srcu_read_unlock(&kvm->srcu, idx); + mutex_unlock(&kvm->slots_lock); } static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { -- 2.25.1