Skip to content

Commit d780592

Browse files
jan-kiszkaavikivity
authored andcommitted
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>
1 parent d462b81 commit d780592

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

‎arch/x86/kvm/x86.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6126,12 +6126,7 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
61266126
if (r == 0)
61276127
r = kvm_mmu_setup(vcpu);
61286128
vcpu_put(vcpu);
6129-
if (r < 0)
6130-
goto free_vcpu;
61316129

6132-
return 0;
6133-
free_vcpu:
6134-
kvm_x86_ops->vcpu_free(vcpu);
61356130
return r;
61366131
}
61376132

‎virt/kvm/kvm_main.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,18 +1615,18 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
16151615

16161616
r = kvm_arch_vcpu_setup(vcpu);
16171617
if (r)
1618-
return r;
1618+
goto vcpu_destroy;
16191619

16201620
mutex_lock(&kvm->lock);
16211621
if (atomic_read(&kvm->online_vcpus) == KVM_MAX_VCPUS) {
16221622
r = -EINVAL;
1623-
goto vcpu_destroy;
1623+
goto unlock_vcpu_destroy;
16241624
}
16251625

16261626
kvm_for_each_vcpu(r, v, kvm)
16271627
if (v->vcpu_id == id) {
16281628
r = -EEXIST;
1629-
goto vcpu_destroy;
1629+
goto unlock_vcpu_destroy;
16301630
}
16311631

16321632
BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
@@ -1636,7 +1636,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
16361636
r = create_vcpu_fd(vcpu);
16371637
if (r < 0) {
16381638
kvm_put_kvm(kvm);
1639-
goto vcpu_destroy;
1639+
goto unlock_vcpu_destroy;
16401640
}
16411641

16421642
kvm->vcpus[atomic_read(&kvm->online_vcpus)] = vcpu;
@@ -1650,8 +1650,9 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
16501650
mutex_unlock(&kvm->lock);
16511651
return r;
16521652

1653-
vcpu_destroy:
1653+
unlock_vcpu_destroy:
16541654
mutex_unlock(&kvm->lock);
1655+
vcpu_destroy:
16551656
kvm_arch_vcpu_destroy(vcpu);
16561657
return r;
16571658
}

0 commit comments

Comments
 (0)