KVM: Properly lock PIT creation
authorAvi Kivity <avi@redhat.com>
Mon, 5 Jan 2009 13:21:42 +0000 (15:21 +0200)
committerAvi Kivity <avi@redhat.com>
Tue, 24 Mar 2009 09:03:01 +0000 (11:03 +0200)
Otherwise, two threads can create a PIT in parallel and cause a memory leak.

Signed-off-by: Avi Kivity <avi@redhat.com>
arch/x86/kvm/i8254.c
arch/x86/kvm/x86.c

index 528daadeba4953251e3bc5916a839b2637858b6a..69d1bbff3fd3f5028245249f1637574bf8bdfaab 100644 (file)
@@ -548,9 +548,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm)
        if (!pit)
                return NULL;
 
-       mutex_lock(&kvm->lock);
        pit->irq_source_id = kvm_request_irq_source_id(kvm);
-       mutex_unlock(&kvm->lock);
        if (pit->irq_source_id < 0) {
                kfree(pit);
                return NULL;
index a1f14611f4b926cac053409764da40acff06d307..6fbc346033752132dccd2a5227c150078bc60bb4 100644 (file)
@@ -1837,10 +1837,16 @@ long kvm_arch_vm_ioctl(struct file *filp,
                        goto out;
                break;
        case KVM_CREATE_PIT:
+               mutex_lock(&kvm->lock);
+               r = -EEXIST;
+               if (kvm->arch.vpit)
+                       goto create_pit_unlock;
                r = -ENOMEM;
                kvm->arch.vpit = kvm_create_pit(kvm);
                if (kvm->arch.vpit)
                        r = 0;
+       create_pit_unlock:
+               mutex_unlock(&kvm->lock);
                break;
        case KVM_IRQ_LINE: {
                struct kvm_irq_level irq_event;