Commit a97b0e77 authored by Jim Mattson's avatar Jim Mattson Committed by Paolo Bonzini

kvm: call kvm_arch_destroy_vm if vm creation fails

In kvm_create_vm(), if we've successfully called kvm_arch_init_vm(), but
then fail later in the function, we need to call kvm_arch_destroy_vm()
so that it can do any necessary cleanup (like freeing memory).

Fixes: 44a95dae ("KVM: x86: Detect and Initialize AVIC support")
Signed-off-by: default avatarJohn Sperbeck <jsperbeck@google.com>
Signed-off-by: default avatarJim Mattson <jmattson@google.com>
Reviewed-by: default avatarJunaid Shahid <junaids@google.com>
[Remove dependency on "kvm: Don't clear reference count on
 kvm_create_vm() error path" which was not committed. - Paolo]
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 9121923c
...@@ -641,7 +641,6 @@ static struct kvm *kvm_create_vm(unsigned long type) ...@@ -641,7 +641,6 @@ static struct kvm *kvm_create_vm(unsigned long type)
mutex_init(&kvm->lock); mutex_init(&kvm->lock);
mutex_init(&kvm->irq_lock); mutex_init(&kvm->irq_lock);
mutex_init(&kvm->slots_lock); mutex_init(&kvm->slots_lock);
refcount_set(&kvm->users_count, 1);
INIT_LIST_HEAD(&kvm->devices); INIT_LIST_HEAD(&kvm->devices);
BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
...@@ -650,7 +649,7 @@ static struct kvm *kvm_create_vm(unsigned long type) ...@@ -650,7 +649,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
struct kvm_memslots *slots = kvm_alloc_memslots(); struct kvm_memslots *slots = kvm_alloc_memslots();
if (!slots) if (!slots)
goto out_err_no_disable; goto out_err_no_arch_destroy_vm;
/* Generations must be different for each address space. */ /* Generations must be different for each address space. */
slots->generation = i; slots->generation = i;
rcu_assign_pointer(kvm->memslots[i], slots); rcu_assign_pointer(kvm->memslots[i], slots);
...@@ -660,12 +659,13 @@ static struct kvm *kvm_create_vm(unsigned long type) ...@@ -660,12 +659,13 @@ static struct kvm *kvm_create_vm(unsigned long type)
rcu_assign_pointer(kvm->buses[i], rcu_assign_pointer(kvm->buses[i],
kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT)); kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT));
if (!kvm->buses[i]) if (!kvm->buses[i])
goto out_err_no_disable; goto out_err_no_arch_destroy_vm;
} }
refcount_set(&kvm->users_count, 1);
r = kvm_arch_init_vm(kvm, type); r = kvm_arch_init_vm(kvm, type);
if (r) if (r)
goto out_err_no_disable; goto out_err_no_arch_destroy_vm;
r = hardware_enable_all(); r = hardware_enable_all();
if (r) if (r)
...@@ -699,7 +699,9 @@ static struct kvm *kvm_create_vm(unsigned long type) ...@@ -699,7 +699,9 @@ static struct kvm *kvm_create_vm(unsigned long type)
out_err_no_srcu: out_err_no_srcu:
hardware_disable_all(); hardware_disable_all();
out_err_no_disable: out_err_no_disable:
refcount_set(&kvm->users_count, 0); kvm_arch_destroy_vm(kvm);
WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
out_err_no_arch_destroy_vm:
for (i = 0; i < KVM_NR_BUSES; i++) for (i = 0; i < KVM_NR_BUSES; i++)
kfree(kvm_get_bus(kvm, i)); kfree(kvm_get_bus(kvm, i));
for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment