| @@ -627,8 +627,9 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, int fd) · 626 unmodified lines |
| 627 | | 627 | |
| 628 | static struct kvm *kvm_create_vm(unsigned long type) | 628 | static struct kvm *kvm_create_vm(unsigned long type) |
| 629 | { | 629 | { |
| 630 | int r, i; | | |
| 631 | struct kvm *kvm = kvm_arch_alloc_vm(); | 630 | struct kvm *kvm = kvm_arch_alloc_vm(); |
| | 631 | int r = -ENOMEM; |
| | 632 | int i; |
| 632 | | 633 | |
| 633 | if (!kvm) | 634 | if (!kvm) |
| 634 | return ERR_PTR(-ENOMEM); | 635 | return ERR_PTR(-ENOMEM); |
| @@ -640,44 +641,45 @@ static struct kvm *kvm_create_vm(unsigned long type) · 5 unmodified lines |
| 640 | mutex_init(&kvm->lock); | 641 | mutex_init(&kvm->lock); |
| 641 | mutex_init(&kvm->irq_lock); | 642 | mutex_init(&kvm->irq_lock); |
| 642 | mutex_init(&kvm->slots_lock); | 643 | mutex_init(&kvm->slots_lock); |
| 643 | refcount_set(&kvm->users_count, 1); | | |
| 644 | INIT_LIST_HEAD(&kvm->devices); | 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 | BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); | 646 | BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX); |
| 659 | | 647 | |
| 660 | r = -ENOMEM; | | |
| 661 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { | 648 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
| 662 | struct kvm_memslots *slots = kvm_alloc_memslots(); | 649 | struct kvm_memslots *slots = kvm_alloc_memslots(); |
| | 650 | |
| 663 | if (!slots) | 651 | if (!slots) |
| 664 | goto out_err_no_srcu; | 652 | goto out_err_no_arch_destroy_vm; |
| 665 | /* Generations must be different for each address space. */ | 653 | /* Generations must be different for each address space. */ |
| 666 | slots->generation = i; | 654 | slots->generation = i; |
| 667 | rcu_assign_pointer(kvm->memslots[i], slots); | 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 | for (i = 0; i < KVM_NR_BUSES; i++) { | 658 | for (i = 0; i < KVM_NR_BUSES; i++) { |
| 675 | rcu_assign_pointer(kvm->buses[i], | 659 | rcu_assign_pointer(kvm->buses[i], |
| 676 | kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT)); | 660 | kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT)); |
| 677 | if (!kvm->buses[i]) | 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 | r = kvm_init_mmu_notifier(kvm); | 683 | r = kvm_init_mmu_notifier(kvm); |
| 682 | if (r) | 684 | if (r) |
| 683 | goto out_err; | 685 | goto out_err; |
| @@ -697,7 +699,9 @@ out_err_no_irq_srcu: · 13 unmodified lines |
| 697 | out_err_no_srcu: | 699 | out_err_no_srcu: |
| 698 | hardware_disable_all(); | 700 | hardware_disable_all(); |
| 699 | out_err_no_disable: | 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 | for (i = 0; i < KVM_NR_BUSES; i++) | 705 | for (i = 0; i < KVM_NR_BUSES; i++) |
| 702 | kfree(kvm_get_bus(kvm, i)); | 706 | kfree(kvm_get_bus(kvm, i)); |
| 703 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) | 707 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) |