Commit d780592b99d7d8a5ff905f6bacca519d4a342c76

Authored by Jan Kiszka
Committed by Avi Kivity
1 parent d462b81923

KVM: Clean up error handling during VCPU creation

So far kvm_arch_vcpu_setup is responsible for freeing the vcpu struct if
it fails. Move this confusing resonsibility back into the hands of
kvm_vm_ioctl_create_vcpu. Only kvm_arch_vcpu_setup of x86 is affected,
all other archs cannot fail.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Avi Kivity <avi@redhat.com>

Showing 2 changed files with 6 additions and 10 deletions Side-by-side Diff

... ... @@ -6126,12 +6126,7 @@
6126 6126 if (r == 0)
6127 6127 r = kvm_mmu_setup(vcpu);
6128 6128 vcpu_put(vcpu);
6129   - if (r < 0)
6130   - goto free_vcpu;
6131 6129  
6132   - return 0;
6133   -free_vcpu:
6134   - kvm_x86_ops->vcpu_free(vcpu);
6135 6130 return r;
6136 6131 }
6137 6132  
... ... @@ -1615,18 +1615,18 @@
1615 1615  
1616 1616 r = kvm_arch_vcpu_setup(vcpu);
1617 1617 if (r)
1618   - return r;
  1618 + goto vcpu_destroy;
1619 1619  
1620 1620 mutex_lock(&kvm->lock);
1621 1621 if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
1622 1622 r = -EINVAL;
1623   - goto vcpu_destroy;
  1623 + goto unlock_vcpu_destroy;
1624 1624 }
1625 1625  
1626 1626 kvm_for_each_vcpu(r, v, kvm)
1627 1627 if (v->vcpu_id == id) {
1628 1628 r = -EEXIST;
1629   - goto vcpu_destroy;
  1629 + goto unlock_vcpu_destroy;
1630 1630 }
1631 1631  
1632 1632 BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
... ... @@ -1636,7 +1636,7 @@
1636 1636 r = create_vcpu_fd(vcpu);
1637 1637 if (r < 0) {
1638 1638 kvm_put_kvm(kvm);
1639   - goto vcpu_destroy;
  1639 + goto unlock_vcpu_destroy;
1640 1640 }
1641 1641  
1642 1642 kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
1643 1643  
... ... @@ -1650,8 +1650,9 @@
1650 1650 mutex_unlock(&kvm->lock);
1651 1651 return r;
1652 1652  
1653   -vcpu_destroy:
  1653 +unlock_vcpu_destroy:
1654 1654 mutex_unlock(&kvm->lock);
  1655 +vcpu_destroy:
1655 1656 kvm_arch_vcpu_destroy(vcpu);
1656 1657 return r;
1657 1658 }