Skip to content

Commit

Permalink
KVM: Clean up error handling during VCPU creation
Browse files Browse the repository at this point in the history
commit d780592 upstream.

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>
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
jan-kiszka authored and gregkh committed Apr 5, 2013
1 parent 8c70289 commit d104388
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
5 changes: 0 additions & 5 deletions arch/x86/kvm/x86.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6116,12 +6116,7 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
if (r == 0) if (r == 0)
r = kvm_mmu_setup(vcpu); r = kvm_mmu_setup(vcpu);
vcpu_put(vcpu); vcpu_put(vcpu);
if (r < 0)
goto free_vcpu;


return 0;
free_vcpu:
kvm_x86_ops->vcpu_free(vcpu);
return r; return r;
} }


Expand Down
11 changes: 6 additions & 5 deletions virt/kvm/kvm_main.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1616,18 +1616,18 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)


r = kvm_arch_vcpu_setup(vcpu); r = kvm_arch_vcpu_setup(vcpu);
if (r) if (r)
return r; goto vcpu_destroy;


mutex_lock(&kvm->lock); mutex_lock(&kvm->lock);
if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) { if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
r = -EINVAL; r = -EINVAL;
goto vcpu_destroy; goto unlock_vcpu_destroy;
} }


kvm_for_each_vcpu(r, v, kvm) kvm_for_each_vcpu(r, v, kvm)
if (v->vcpu_id == id) { if (v->vcpu_id == id) {
r = -EEXIST; r = -EEXIST;
goto vcpu_destroy; goto unlock_vcpu_destroy;
} }


BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]); BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
Expand All @@ -1637,7 +1637,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
r = create_vcpu_fd(vcpu); r = create_vcpu_fd(vcpu);
if (r < 0) { if (r < 0) {
kvm_put_kvm(kvm); kvm_put_kvm(kvm);
goto vcpu_destroy; goto unlock_vcpu_destroy;
} }


kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu; kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
Expand All @@ -1651,8 +1651,9 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
mutex_unlock(&kvm->lock); mutex_unlock(&kvm->lock);
return r; return r;


vcpu_destroy: unlock_vcpu_destroy:
mutex_unlock(&kvm->lock); mutex_unlock(&kvm->lock);
vcpu_destroy:
kvm_arch_vcpu_destroy(vcpu); kvm_arch_vcpu_destroy(vcpu);
return r; return r;
} }
Expand Down

0 comments on commit d104388

Please sign in to comment.