| @@ -627,8 +627,9 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) · 626 unmodified lines |
| 627 | 627 | |
| 628 | 628 | static struct kvm *kvm_create_vm(unsigned long type) |
| 629 | 629 | { |
| 630 | | − int r, i; |
| 631 | 630 | struct kvm *kvm = kvm_arch_alloc_vm(); |
| 631 | + int r = -ENOMEM; |
| 632 | + int i; |
| 632 | 633 | |
| 633 | 634 | if (!kvm) |
| 634 | 635 | return ERR_PTR(-ENOMEM); |
| @@ -640,44 +641,45 @@ static struct kvm *kvm_create_vm(unsigned long type) · 5 unmodified lines |
| 640 | 641 | mutex_init(&kvm->lock); |
| 641 | 642 | mutex_init(&kvm->irq_lock); |
| 642 | 643 | mutex_init(&kvm->slots_lock); |
| 643 | | − refcount_set(&kvm->users_count, 1); |
| 644 | 644 | INIT_LIST_HEAD(&kvm->devices); |
| 645 | 645 | |
| 646 | | − r = kvm_arch_init_vm(kvm, type); |
| 647 | | − if (r) |
| 648 | | − goto out_err_no_disable; |
| 649 | | − |
| 650 | | − r = hardware_enable_all(); |
| 651 | | − if (r) |
| 652 | | − goto out_err_no_disable; |
| 653 | | − |
| 654 | | −#ifdef CONFIG_HAVE_KVM_IRQFD |
| 655 | | − INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); |
| 656 | | −#endif |
| 657 | | − |
| 658 | 646 | BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); |
| 659 | 647 | |
| 660 | | − r = -ENOMEM; |
| 661 | 648 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
| 662 | 649 | struct kvm_memslots *slots = kvm_alloc_memslots(); |
| 650 | + |
| 663 | 651 | if (!slots) |
| 664 | | − goto out_err_no_srcu; |
| 652 | + goto out_err_no_arch_destroy_vm; |
| 665 | 653 | /* Generations must be different for each address space. */ |
| 666 | 654 | slots->generation = i; |
| 667 | 655 | rcu_assign_pointer(kvm->memslots[i], slots); |
| 668 | 656 | } |
| 669 | 657 | |
| 670 | | − if (init_srcu_struct(&kvm->srcu)) |
| 671 | | − goto out_err_no_srcu; |
| 672 | | − if (init_srcu_struct(&kvm->irq_srcu)) |
| 673 | | − goto out_err_no_irq_srcu; |
| 674 | 658 | for (i = 0; i < KVM_NR_BUSES; i++) { |
| 675 | 659 | rcu_assign_pointer(kvm->buses[i], |
| 676 | 660 | kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT)); |
| 677 | 661 | if (!kvm->buses[i]) |
| 678 | | − goto out_err; |
| 662 | + goto out_err_no_arch_destroy_vm; |
| 679 | 663 | } |
| 680 | 664 | |
| 665 | + refcount_set(&kvm->users_count, 1); |
| 666 | + r = kvm_arch_init_vm(kvm, type); |
| 667 | + if (r) |
| 668 | + goto out_err_no_arch_destroy_vm; |
| 669 | + |
| 670 | + r = hardware_enable_all(); |
| 671 | + if (r) |
| 672 | + goto out_err_no_disable; |
| 673 | + |
| 674 | +#ifdef CONFIG_HAVE_KVM_IRQFD |
| 675 | + INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list); |
| 676 | +#endif |
| 677 | + |
| 678 | + if (init_srcu_struct(&kvm->srcu)) |
| 679 | + goto out_err_no_srcu; |
| 680 | + if (init_srcu_struct(&kvm->irq_srcu)) |
| 681 | + goto out_err_no_irq_srcu; |
| 682 | + |
| 681 | 683 | r = kvm_init_mmu_notifier(kvm); |
| 682 | 684 | if (r) |
| 683 | 685 | goto out_err; |
| @@ -697,7 +699,9 @@ out_err_no_irq_srcu: · 13 unmodified lines |
| 697 | 699 | out_err_no_srcu: |
| 698 | 700 | hardware_disable_all(); |
| 699 | 701 | out_err_no_disable: |
| 700 | | − refcount_set(&kvm->users_count, 0); |
| 702 | + kvm_arch_destroy_vm(kvm); |
| 703 | + WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count)); |
| 704 | +out_err_no_arch_destroy_vm: |
| 701 | 705 | for (i = 0; i < KVM_NR_BUSES; i++) |
| 702 | 706 | kfree(kvm_get_bus(kvm, i)); |
| 703 | 707 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) |