Beginning with commit 44a95dae1d229a ("KVM: x86: Detect and Initialize AVIC support"), AMD's version of kvm_arch_init_vm() will allocate memory if the module parameter, avic, is enabled. (Note that this module parameter is disabled by default.) However, there are many possible failure exits from kvm_create_vm() *after* the call to kvm_arch_init_vm(), and the memory allocated by kvm_arch_init_vm() was leaked on these failure paths. The obvious solution is to call kvm_arch_destroy_vm() on these failure paths, since it will free the memory allocated by kvm_arch_init_vm(). However, kvm_arch_destroy_vm() may reference memslots and buses that were allocated later in kvm_create_vm(). So, before we can call kvm_arch_destroy_vm() on the failure paths out of kvm_create_vm(), we need to hoist the memslot and bus allocation up before the call to kvm_arch_init_vm(). The call to clear the reference count on (some) failure paths out of kvm_create_vm() just added to the potential confusion. By sinking the call to set the reference count below any possible failure exits, we can eliminate the call to clear the reference count on the failure paths. v1 -> v2: Call kvm_arch_destroy_vm before refcount_set v2 -> v3: Added two preparatory changes Jim Mattson (2): kvm: Don't clear reference count on kvm_create_vm() error path kvm: Allocate memslots and buses before calling kvm_arch_init_vm John Sperbeck (1): kvm: call kvm_arch_destroy_vm if vm creation fails virt/kvm/kvm_main.c | 52 ++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 22 deletions(-) -- 2.24.0.rc0.303.g954a862665-goog