nfsd4: don't sleep in lease-break callback
[safe/jmp/linux-2.6] / virt / kvm / eventfd.c
index bb4ebd8..7016319 100644 (file)
@@ -47,7 +47,6 @@ struct _irqfd {
        int                       gsi;
        struct list_head          list;
        poll_table                pt;
-       wait_queue_head_t        *wqh;
        wait_queue_t              wait;
        struct work_struct        inject;
        struct work_struct        shutdown;
@@ -61,10 +60,8 @@ irqfd_inject(struct work_struct *work)
        struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
        struct kvm *kvm = irqfd->kvm;
 
-       mutex_lock(&kvm->irq_lock);
        kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1);
        kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
-       mutex_unlock(&kvm->irq_lock);
 }
 
 /*
@@ -74,12 +71,13 @@ static void
 irqfd_shutdown(struct work_struct *work)
 {
        struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
+       u64 cnt;
 
        /*
         * Synchronize with the wait-queue and unhook ourselves to prevent
         * further events.
         */
-       remove_wait_queue(irqfd->wqh, &irqfd->wait);
+       eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
 
        /*
         * We know no new events will be scheduled at this point, so block
@@ -160,15 +158,13 @@ irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
                        poll_table *pt)
 {
        struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt);
-
-       irqfd->wqh = wqh;
        add_wait_queue(wqh, &irqfd->wait);
 }
 
 static int
 kvm_irqfd_assign(struct kvm *kvm, int fd, int gsi)
 {
-       struct _irqfd *irqfd;
+       struct _irqfd *irqfd, *tmp;
        struct file *file = NULL;
        struct eventfd_ctx *eventfd = NULL;
        int ret;
@@ -205,9 +201,20 @@ kvm_irqfd_assign(struct kvm *kvm, int fd, int gsi)
        init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
        init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
 
+       spin_lock_irq(&kvm->irqfds.lock);
+
+       ret = 0;
+       list_for_each_entry(tmp, &kvm->irqfds.items, list) {
+               if (irqfd->eventfd != tmp->eventfd)
+                       continue;
+               /* This fd is used for another irq already. */
+               ret = -EBUSY;
+               spin_unlock_irq(&kvm->irqfds.lock);
+               goto fail;
+       }
+
        events = file->f_op->poll(file, &irqfd->pt);
 
-       spin_lock_irq(&kvm->irqfds.lock);
        list_add_tail(&irqfd->list, &kvm->irqfds.items);
        spin_unlock_irq(&kvm->irqfds.lock);
 
@@ -453,7 +460,7 @@ static int
 kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 {
        int                       pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
-       struct kvm_io_bus        *bus = pio ? &kvm->pio_bus : &kvm->mmio_bus;
+       enum kvm_bus              bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
        struct _ioeventfd        *p;
        struct eventfd_ctx       *eventfd;
        int                       ret;
@@ -498,7 +505,7 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
        else
                p->wildcard = true;
 
-       down_write(&kvm->slots_lock);
+       mutex_lock(&kvm->slots_lock);
 
        /* Verify that there isnt a match already */
        if (ioeventfd_check_collision(kvm, p)) {
@@ -508,18 +515,18 @@ kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 
        kvm_iodevice_init(&p->dev, &ioeventfd_ops);
 
-       ret = __kvm_io_bus_register_dev(bus, &p->dev);
+       ret = kvm_io_bus_register_dev(kvm, bus_idx, &p->dev);
        if (ret < 0)
                goto unlock_fail;
 
        list_add_tail(&p->list, &kvm->ioeventfds);
 
-       up_write(&kvm->slots_lock);
+       mutex_unlock(&kvm->slots_lock);
 
        return 0;
 
 unlock_fail:
-       up_write(&kvm->slots_lock);
+       mutex_unlock(&kvm->slots_lock);
 
 fail:
        kfree(p);
@@ -532,7 +539,7 @@ static int
 kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
 {
        int                       pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
-       struct kvm_io_bus        *bus = pio ? &kvm->pio_bus : &kvm->mmio_bus;
+       enum kvm_bus              bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
        struct _ioeventfd        *p, *tmp;
        struct eventfd_ctx       *eventfd;
        int                       ret = -ENOENT;
@@ -541,7 +548,7 @@ kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
        if (IS_ERR(eventfd))
                return PTR_ERR(eventfd);
 
-       down_write(&kvm->slots_lock);
+       mutex_lock(&kvm->slots_lock);
 
        list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
                bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
@@ -555,13 +562,13 @@ kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
                if (!p->wildcard && p->datamatch != args->datamatch)
                        continue;
 
-               __kvm_io_bus_unregister_dev(bus, &p->dev);
+               kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
                ioeventfd_release(p);
                ret = 0;
                break;
        }
 
-       up_write(&kvm->slots_lock);
+       mutex_unlock(&kvm->slots_lock);
 
        eventfd_ctx_put(eventfd);