Skip to content

Commit 4ba4f41

Browse files
sean-jcbonzini
authored andcommitted
KVM: Properly unwind VM creation if creating debugfs fails
Properly unwind VM creation if kvm_create_vm_debugfs() fails. A recent change to invoke kvm_create_vm_debug() in kvm_create_vm() was led astray by buggy try_get_module() handling adding by commit 5f6de5c ("KVM: Prevent module exit until all VMs are freed"). The debugfs error path effectively inherits the bad error path of try_module_get(), e.g. KVM leaves the to-be-free VM on vm_list even though KVM appears to do the right thing by calling module_put() and falling through. Opportunistically hoist kvm_create_vm_debugfs() above the call to kvm_arch_post_init_vm() so that the "post-init" arch hook is actually invoked after the VM is initialized (ignoring kvm_coalesced_mmio_init() for the moment). x86 is the only non-nop implementation of the post-init hook, and it doesn't allocate/initialize any objects that are reachable via debugfs code (spawns a kthread worker for the NX huge page mitigation). Leave the buggy try_get_module() alone for now, it will be fixed in a separate commit. Fixes: b74ed7a ("KVM: Actually create debugfs in kvm_create_vm()") Reported-by: [email protected] Cc: Oliver Upton <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Reviewed-by: Oliver Upton <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 568035b commit 4ba4f41

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

virt/kvm/kvm_main.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,9 +1211,13 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
12111211
if (r)
12121212
goto out_err_no_mmu_notifier;
12131213

1214+
r = kvm_create_vm_debugfs(kvm, fdname);
1215+
if (r)
1216+
goto out_err_no_debugfs;
1217+
12141218
r = kvm_arch_post_init_vm(kvm);
12151219
if (r)
1216-
goto out_err_mmu_notifier;
1220+
goto out_err;
12171221

12181222
mutex_lock(&kvm_lock);
12191223
list_add(&kvm->vm_list, &vm_list);
@@ -1229,18 +1233,14 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
12291233
*/
12301234
if (!try_module_get(kvm_chardev_ops.owner)) {
12311235
r = -ENODEV;
1232-
goto out_err_mmu_notifier;
1233-
}
1234-
1235-
r = kvm_create_vm_debugfs(kvm, fdname);
1236-
if (r)
12371236
goto out_err;
1237+
}
12381238

12391239
return kvm;
12401240

12411241
out_err:
1242-
module_put(kvm_chardev_ops.owner);
1243-
out_err_mmu_notifier:
1242+
kvm_destroy_vm_debugfs(kvm);
1243+
out_err_no_debugfs:
12441244
#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
12451245
if (kvm->mmu_notifier.ops)
12461246
mmu_notifier_unregister(&kvm->mmu_notifier, current->mm);

0 commit comments

Comments
 (0)