vhost: lock receive queue, not the socket
[linux-2.6.git] / drivers / vhost / vhost.c
index da21ae9..2ab2912 100644 (file)
 #include <linux/vhost.h>
 #include <linux/virtio_net.h>
 #include <linux/mm.h>
+#include <linux/mmu_context.h>
 #include <linux/miscdevice.h>
 #include <linux/mutex.h>
-#include <linux/workqueue.h>
 #include <linux/rcupdate.h>
 #include <linux/poll.h>
 #include <linux/file.h>
 #include <linux/highmem.h>
 #include <linux/slab.h>
+#include <linux/kthread.h>
+#include <linux/cgroup.h>
 
 #include <linux/net.h>
 #include <linux/if_packet.h>
 #include <linux/if_arp.h>
 
-#include <net/sock.h>
-
 #include "vhost.h"
 
 enum {
@@ -37,14 +37,12 @@ enum {
        VHOST_MEMORY_F_LOG = 0x1,
 };
 
-static struct workqueue_struct *vhost_workqueue;
-
 static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
                            poll_table *pt)
 {
        struct vhost_poll *poll;
-       poll = container_of(pt, struct vhost_poll, table);
 
+       poll = container_of(pt, struct vhost_poll, table);
        poll->wqh = wqh;
        add_wait_queue(wqh, &poll->wait);
 }
@@ -52,23 +50,34 @@ static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
 static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
                             void *key)
 {
-       struct vhost_poll *poll;
-       poll = container_of(wait, struct vhost_poll, wait);
+       struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
+
        if (!((unsigned long)key & poll->mask))
                return 0;
 
-       queue_work(vhost_workqueue, &poll->work);
+       vhost_poll_queue(poll);
        return 0;
 }
 
+static void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
+{
+       INIT_LIST_HEAD(&work->node);
+       work->fn = fn;
+       init_waitqueue_head(&work->done);
+       work->flushing = 0;
+       work->queue_seq = work->done_seq = 0;
+}
+
 /* Init poll structure */
-void vhost_poll_init(struct vhost_poll *poll, work_func_t func,
-                    unsigned long mask)
+void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
+                    unsigned long mask, struct vhost_dev *dev)
 {
-       INIT_WORK(&poll->work, func);
        init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
        init_poll_funcptr(&poll->table, vhost_poll_func);
        poll->mask = mask;
+       poll->dev = dev;
+
+       vhost_work_init(&poll->work, fn);
 }
 
 /* Start polling a file. We add ourselves to file's wait queue. The caller must
@@ -76,6 +85,7 @@ void vhost_poll_init(struct vhost_poll *poll, work_func_t func,
 void vhost_poll_start(struct vhost_poll *poll, struct file *file)
 {
        unsigned long mask;
+
        mask = file->f_op->poll(file, &poll->table);
        if (mask)
                vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
@@ -88,16 +98,57 @@ void vhost_poll_stop(struct vhost_poll *poll)
        remove_wait_queue(poll->wqh, &poll->wait);
 }
 
+static bool vhost_work_seq_done(struct vhost_dev *dev, struct vhost_work *work,
+                               unsigned seq)
+{
+       int left;
+
+       spin_lock_irq(&dev->work_lock);
+       left = seq - work->done_seq;
+       spin_unlock_irq(&dev->work_lock);
+       return left <= 0;
+}
+
+static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
+{
+       unsigned seq;
+       int flushing;
+
+       spin_lock_irq(&dev->work_lock);
+       seq = work->queue_seq;
+       work->flushing++;
+       spin_unlock_irq(&dev->work_lock);
+       wait_event(work->done, vhost_work_seq_done(dev, work, seq));
+       spin_lock_irq(&dev->work_lock);
+       flushing = --work->flushing;
+       spin_unlock_irq(&dev->work_lock);
+       BUG_ON(flushing < 0);
+}
+
 /* Flush any work that has been scheduled. When calling this, don't hold any
  * locks that are also used by the callback. */
 void vhost_poll_flush(struct vhost_poll *poll)
 {
-       flush_work(&poll->work);
+       vhost_work_flush(poll->dev, &poll->work);
+}
+
+static inline void vhost_work_queue(struct vhost_dev *dev,
+                                   struct vhost_work *work)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&dev->work_lock, flags);
+       if (list_empty(&work->node)) {
+               list_add_tail(&work->node, &dev->work_list);
+               work->queue_seq++;
+               wake_up_process(dev->worker);
+       }
+       spin_unlock_irqrestore(&dev->work_lock, flags);
 }
 
 void vhost_poll_queue(struct vhost_poll *poll)
 {
-       queue_work(vhost_workqueue, &poll->work);
+       vhost_work_queue(poll->dev, &poll->work);
 }
 
 static void vhost_vq_reset(struct vhost_dev *dev,
@@ -111,10 +162,10 @@ static void vhost_vq_reset(struct vhost_dev *dev,
        vq->avail_idx = 0;
        vq->last_used_idx = 0;
        vq->used_flags = 0;
-       vq->used_flags = 0;
        vq->log_used = false;
        vq->log_addr = -1ull;
-       vq->hdr_size = 0;
+       vq->vhost_hlen = 0;
+       vq->sock_hlen = 0;
        vq->private_data = NULL;
        vq->log_base = NULL;
        vq->error_ctx = NULL;
@@ -125,10 +176,97 @@ static void vhost_vq_reset(struct vhost_dev *dev,
        vq->log_ctx = NULL;
 }
 
+static int vhost_worker(void *data)
+{
+       struct vhost_dev *dev = data;
+       struct vhost_work *work = NULL;
+       unsigned uninitialized_var(seq);
+
+       use_mm(dev->mm);
+
+       for (;;) {
+               /* mb paired w/ kthread_stop */
+               set_current_state(TASK_INTERRUPTIBLE);
+
+               spin_lock_irq(&dev->work_lock);
+               if (work) {
+                       work->done_seq = seq;
+                       if (work->flushing)
+                               wake_up_all(&work->done);
+               }
+
+               if (kthread_should_stop()) {
+                       spin_unlock_irq(&dev->work_lock);
+                       __set_current_state(TASK_RUNNING);
+                       break;
+               }
+               if (!list_empty(&dev->work_list)) {
+                       work = list_first_entry(&dev->work_list,
+                                               struct vhost_work, node);
+                       list_del_init(&work->node);
+                       seq = work->queue_seq;
+               } else
+                       work = NULL;
+               spin_unlock_irq(&dev->work_lock);
+
+               if (work) {
+                       __set_current_state(TASK_RUNNING);
+                       work->fn(work);
+               } else
+                       schedule();
+
+       }
+       unuse_mm(dev->mm);
+       return 0;
+}
+
+/* Helper to allocate iovec buffers for all vqs. */
+static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
+{
+       int i;
+
+       for (i = 0; i < dev->nvqs; ++i) {
+               dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
+                                              UIO_MAXIOV, GFP_KERNEL);
+               dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
+                                         GFP_KERNEL);
+               dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
+                                           UIO_MAXIOV, GFP_KERNEL);
+
+               if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
+                       !dev->vqs[i].heads)
+                       goto err_nomem;
+       }
+       return 0;
+
+err_nomem:
+       for (; i >= 0; --i) {
+               kfree(dev->vqs[i].indirect);
+               kfree(dev->vqs[i].log);
+               kfree(dev->vqs[i].heads);
+       }
+       return -ENOMEM;
+}
+
+static void vhost_dev_free_iovecs(struct vhost_dev *dev)
+{
+       int i;
+
+       for (i = 0; i < dev->nvqs; ++i) {
+               kfree(dev->vqs[i].indirect);
+               dev->vqs[i].indirect = NULL;
+               kfree(dev->vqs[i].log);
+               dev->vqs[i].log = NULL;
+               kfree(dev->vqs[i].heads);
+               dev->vqs[i].heads = NULL;
+       }
+}
+
 long vhost_dev_init(struct vhost_dev *dev,
                    struct vhost_virtqueue *vqs, int nvqs)
 {
        int i;
+
        dev->vqs = vqs;
        dev->nvqs = nvqs;
        mutex_init(&dev->mutex);
@@ -136,16 +274,22 @@ long vhost_dev_init(struct vhost_dev *dev,
        dev->log_file = NULL;
        dev->memory = NULL;
        dev->mm = NULL;
+       spin_lock_init(&dev->work_lock);
+       INIT_LIST_HEAD(&dev->work_list);
+       dev->worker = NULL;
 
        for (i = 0; i < dev->nvqs; ++i) {
+               dev->vqs[i].log = NULL;
+               dev->vqs[i].indirect = NULL;
+               dev->vqs[i].heads = NULL;
                dev->vqs[i].dev = dev;
                mutex_init(&dev->vqs[i].mutex);
                vhost_vq_reset(dev, dev->vqs + i);
                if (dev->vqs[i].handle_kick)
                        vhost_poll_init(&dev->vqs[i].poll,
-                                       dev->vqs[i].handle_kick,
-                                       POLLIN);
+                                       dev->vqs[i].handle_kick, POLLIN, dev);
        }
+
        return 0;
 }
 
@@ -156,15 +300,72 @@ long vhost_dev_check_owner(struct vhost_dev *dev)
        return dev->mm == current->mm ? 0 : -EPERM;
 }
 
+struct vhost_attach_cgroups_struct {
+       struct vhost_work work;
+       struct task_struct *owner;
+       int ret;
+};
+
+static void vhost_attach_cgroups_work(struct vhost_work *work)
+{
+       struct vhost_attach_cgroups_struct *s;
+
+       s = container_of(work, struct vhost_attach_cgroups_struct, work);
+       s->ret = cgroup_attach_task_all(s->owner, current);
+}
+
+static int vhost_attach_cgroups(struct vhost_dev *dev)
+{
+       struct vhost_attach_cgroups_struct attach;
+
+       attach.owner = current;
+       vhost_work_init(&attach.work, vhost_attach_cgroups_work);
+       vhost_work_queue(dev, &attach.work);
+       vhost_work_flush(dev, &attach.work);
+       return attach.ret;
+}
+
 /* Caller should have device mutex */
 static long vhost_dev_set_owner(struct vhost_dev *dev)
 {
+       struct task_struct *worker;
+       int err;
+
        /* Is there an owner already? */
-       if (dev->mm)
-               return -EBUSY;
+       if (dev->mm) {
+               err = -EBUSY;
+               goto err_mm;
+       }
+
        /* No owner, become one */
        dev->mm = get_task_mm(current);
+       worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
+       if (IS_ERR(worker)) {
+               err = PTR_ERR(worker);
+               goto err_worker;
+       }
+
+       dev->worker = worker;
+       wake_up_process(worker);        /* avoid contributing to loadavg */
+
+       err = vhost_attach_cgroups(dev);
+       if (err)
+               goto err_cgroup;
+
+       err = vhost_dev_alloc_iovecs(dev);
+       if (err)
+               goto err_cgroup;
+
        return 0;
+err_cgroup:
+       kthread_stop(worker);
+       dev->worker = NULL;
+err_worker:
+       if (dev->mm)
+               mmput(dev->mm);
+       dev->mm = NULL;
+err_mm:
+       return err;
 }
 
 /* Caller should have device mutex */
@@ -180,7 +381,7 @@ long vhost_dev_reset_owner(struct vhost_dev *dev)
        vhost_dev_cleanup(dev);
 
        memory->nregions = 0;
-       dev->memory = memory;
+       RCU_INIT_POINTER(dev->memory, memory);
        return 0;
 }
 
@@ -188,6 +389,7 @@ long vhost_dev_reset_owner(struct vhost_dev *dev)
 void vhost_dev_cleanup(struct vhost_dev *dev)
 {
        int i;
+
        for (i = 0; i < dev->nvqs; ++i) {
                if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
                        vhost_poll_stop(&dev->vqs[i].poll);
@@ -205,6 +407,7 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
                        fput(dev->vqs[i].call);
                vhost_vq_reset(dev, dev->vqs + i);
        }
+       vhost_dev_free_iovecs(dev);
        if (dev->log_ctx)
                eventfd_ctx_put(dev->log_ctx);
        dev->log_ctx = NULL;
@@ -212,8 +415,14 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
                fput(dev->log_file);
        dev->log_file = NULL;
        /* No one will access memory at this point */
-       kfree(dev->memory);
-       dev->memory = NULL;
+       kfree(rcu_dereference_protected(dev->memory,
+                                       lockdep_is_held(&dev->mutex)));
+       RCU_INIT_POINTER(dev->memory, NULL);
+       WARN_ON(!list_empty(&dev->work_list));
+       if (dev->worker) {
+               kthread_stop(dev->worker);
+               dev->worker = NULL;
+       }
        if (dev->mm)
                mmput(dev->mm);
        dev->mm = NULL;
@@ -222,10 +431,11 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
 {
        u64 a = addr / VHOST_PAGE_SIZE / 8;
+
        /* Make sure 64 bit math will not overflow. */
        if (a > ULONG_MAX - (unsigned long)log_base ||
            a + (unsigned long)log_base > ULONG_MAX)
-               return -EFAULT;
+               return 0;
 
        return access_ok(VERIFY_WRITE, log_base + a,
                         (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
@@ -237,8 +447,8 @@ static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
 {
        int i;
 
-        if (!mem)
-                return 0;
+       if (!mem)
+               return 0;
 
        for (i = 0; i < mem->nregions; ++i) {
                struct vhost_memory_region *m = mem->regions + i;
@@ -262,6 +472,7 @@ static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
                            int log_all)
 {
        int i;
+
        for (i = 0; i < d->nvqs; ++i) {
                int ok;
                mutex_lock(&d->vqs[i].mutex);
@@ -294,14 +505,22 @@ static int vq_access_ok(unsigned int num,
 /* Caller should have device mutex but not vq mutex */
 int vhost_log_access_ok(struct vhost_dev *dev)
 {
-       return memory_access_ok(dev, dev->memory, 1);
+       struct vhost_memory *mp;
+
+       mp = rcu_dereference_protected(dev->memory,
+                                      lockdep_is_held(&dev->mutex));
+       return memory_access_ok(dev, mp, 1);
 }
 
 /* Verify access for write logging. */
 /* Caller should have vq mutex and device mutex */
 static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
 {
-       return vq_memory_access_ok(log_base, vq->dev->memory,
+       struct vhost_memory *mp;
+
+       mp = rcu_dereference_protected(vq->dev->memory,
+                                      lockdep_is_held(&vq->mutex));
+       return vq_memory_access_ok(log_base, mp,
                            vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
                (!vq->log_used || log_access_ok(log_base, vq->log_addr,
                                        sizeof *vq->used +
@@ -320,6 +539,7 @@ static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
 {
        struct vhost_memory mem, *newmem, *oldmem;
        unsigned long size = offsetof(struct vhost_memory, regions);
+
        if (copy_from_user(&mem, m, size))
                return -EFAULT;
        if (mem.padding)
@@ -337,11 +557,13 @@ static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
                return -EFAULT;
        }
 
-       if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
+       if (!memory_access_ok(d, newmem,
+                             vhost_has_feature(d, VHOST_F_LOG_ALL))) {
                kfree(newmem);
                return -EFAULT;
        }
-       oldmem = d->memory;
+       oldmem = rcu_dereference_protected(d->memory,
+                                          lockdep_is_held(&d->mutex));
        rcu_assign_pointer(d->memory, newmem);
        synchronize_rcu();
        kfree(oldmem);
@@ -352,6 +574,7 @@ static int init_used(struct vhost_virtqueue *vq,
                     struct vring_used __user *used)
 {
        int r = put_user(vq->used_flags, &used->flags);
+
        if (r)
                return r;
        return get_user(vq->last_used_idx, &used->idx);
@@ -641,6 +864,7 @@ static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
 {
        struct vhost_memory_region *reg;
        int i;
+
        /* linear search is not brilliant, but we really have on the order of 6
         * regions in practice */
        for (i = 0; i < mem->nregions; ++i) {
@@ -663,6 +887,7 @@ static int set_bit_to_user(int nr, void __user *addr)
        void *base;
        int bit = nr + (log % PAGE_SIZE) * 8;
        int r;
+
        r = get_user_pages_fast(log, 1, 1, &page);
        if (r < 0)
                return r;
@@ -678,14 +903,16 @@ static int set_bit_to_user(int nr, void __user *addr)
 static int log_write(void __user *log_base,
                     u64 write_address, u64 write_length)
 {
+       u64 write_page = write_address / VHOST_PAGE_SIZE;
        int r;
+
        if (!write_length)
                return 0;
-       write_address /= VHOST_PAGE_SIZE;
+       write_length += write_address % VHOST_PAGE_SIZE;
        for (;;) {
                u64 base = (u64)(unsigned long)log_base;
-               u64 log = base + write_address / 8;
-               int bit = write_address % 8;
+               u64 log = base + write_page / 8;
+               int bit = write_page % 8;
                if ((u64)(unsigned long)log != log)
                        return -EFAULT;
                r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
@@ -694,7 +921,7 @@ static int log_write(void __user *log_base,
                if (write_length <= VHOST_PAGE_SIZE)
                        break;
                write_length -= VHOST_PAGE_SIZE;
-               write_address += VHOST_PAGE_SIZE;
+               write_page += 1;
        }
        return r;
 }
@@ -712,11 +939,12 @@ int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
                if (r < 0)
                        return r;
                len -= l;
-               if (!len)
+               if (!len) {
+                       if (vq->log_ctx)
+                               eventfd_signal(vq->log_ctx, 1);
                        return 0;
+               }
        }
-       if (vq->log_ctx)
-               eventfd_signal(vq->log_ctx, 1);
        /* Length written exceeds what we have stored. This is a bug. */
        BUG();
        return 0;
@@ -736,12 +964,12 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
        mem = rcu_dereference(dev->memory);
        while ((u64)len > s) {
                u64 size;
-               if (ret >= iov_size) {
+               if (unlikely(ret >= iov_size)) {
                        ret = -ENOBUFS;
                        break;
                }
                reg = find_region(mem, addr, len);
-               if (!reg) {
+               if (unlikely(!reg)) {
                        ret = -EFAULT;
                        break;
                }
@@ -780,18 +1008,18 @@ static unsigned next_desc(struct vring_desc *desc)
        return next;
 }
 
-static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
-                            struct iovec iov[], unsigned int iov_size,
-                            unsigned int *out_num, unsigned int *in_num,
-                            struct vhost_log *log, unsigned int *log_num,
-                            struct vring_desc *indirect)
+static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
+                       struct iovec iov[], unsigned int iov_size,
+                       unsigned int *out_num, unsigned int *in_num,
+                       struct vhost_log *log, unsigned int *log_num,
+                       struct vring_desc *indirect)
 {
        struct vring_desc desc;
        unsigned int i = 0, count, found = 0;
        int ret;
 
        /* Sanity check */
-       if (indirect->len % sizeof desc) {
+       if (unlikely(indirect->len % sizeof desc)) {
                vq_err(vq, "Invalid length in indirect descriptor: "
                       "len 0x%llx not multiple of 0x%zx\n",
                       (unsigned long long)indirect->len,
@@ -800,8 +1028,8 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
        }
 
        ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
-                            ARRAY_SIZE(vq->indirect));
-       if (ret < 0) {
+                            UIO_MAXIOV);
+       if (unlikely(ret < 0)) {
                vq_err(vq, "Translation failure %d in indirect.\n", ret);
                return ret;
        }
@@ -813,7 +1041,7 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
        count = indirect->len / sizeof desc;
        /* Buffers are chained via a 16 bit next field, so
         * we can have at most 2^16 of these. */
-       if (count > USHORT_MAX + 1) {
+       if (unlikely(count > USHRT_MAX + 1)) {
                vq_err(vq, "Indirect buffer length too big: %d\n",
                       indirect->len);
                return -E2BIG;
@@ -821,19 +1049,19 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
 
        do {
                unsigned iov_count = *in_num + *out_num;
-               if (++found > count) {
+               if (unlikely(++found > count)) {
                        vq_err(vq, "Loop detected: last one at %u "
                               "indirect size %u\n",
                               i, count);
                        return -EINVAL;
                }
-               if (memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
-                                    sizeof desc)) {
+               if (unlikely(memcpy_fromiovec((unsigned char *)&desc,
+                                             vq->indirect, sizeof desc))) {
                        vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
                               i, (size_t)indirect->addr + i * sizeof desc);
                        return -EINVAL;
                }
-               if (desc.flags & VRING_DESC_F_INDIRECT) {
+               if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
                        vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
                               i, (size_t)indirect->addr + i * sizeof desc);
                        return -EINVAL;
@@ -841,7 +1069,7 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
 
                ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
                                     iov_size - iov_count);
-               if (ret < 0) {
+               if (unlikely(ret < 0)) {
                        vq_err(vq, "Translation failure %d indirect idx %d\n",
                               ret, i);
                        return ret;
@@ -857,7 +1085,7 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
                } else {
                        /* If it's an output descriptor, they're all supposed
                         * to come before any input descriptors. */
-                       if (*in_num) {
+                       if (unlikely(*in_num)) {
                                vq_err(vq, "Indirect descriptor "
                                       "has out after in: idx %d\n", i);
                                return -EINVAL;
@@ -873,12 +1101,13 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
  * number of output then some number of input descriptors, it's actually two
  * iovecs, but we pack them into one and note how many of each there were.
  *
- * This function returns the descriptor number found, or vq->num (which
- * is never a valid descriptor number) if none was found. */
-unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
-                          struct iovec iov[], unsigned int iov_size,
-                          unsigned int *out_num, unsigned int *in_num,
-                          struct vhost_log *log, unsigned int *log_num)
+ * This function returns the descriptor number found, or vq->num (which is
+ * never a valid descriptor number) if none was found.  A negative code is
+ * returned on error. */
+int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
+                     struct iovec iov[], unsigned int iov_size,
+                     unsigned int *out_num, unsigned int *in_num,
+                     struct vhost_log *log, unsigned int *log_num)
 {
        struct vring_desc desc;
        unsigned int i, head, found = 0;
@@ -887,16 +1116,16 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
 
        /* Check it isn't doing very strange things with descriptor numbers. */
        last_avail_idx = vq->last_avail_idx;
-       if (get_user(vq->avail_idx, &vq->avail->idx)) {
+       if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
                vq_err(vq, "Failed to access avail idx at %p\n",
                       &vq->avail->idx);
-               return vq->num;
+               return -EFAULT;
        }
 
-       if ((u16)(vq->avail_idx - last_avail_idx) > vq->num) {
+       if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
                vq_err(vq, "Guest moved used index from %u to %u",
                       last_avail_idx, vq->avail_idx);
-               return vq->num;
+               return -EFAULT;
        }
 
        /* If there's nothing new since last we looked, return invalid. */
@@ -908,18 +1137,19 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
 
        /* Grab the next descriptor number they're advertising, and increment
         * the index we've seen. */
-       if (get_user(head, &vq->avail->ring[last_avail_idx % vq->num])) {
+       if (unlikely(__get_user(head,
+                               &vq->avail->ring[last_avail_idx % vq->num]))) {
                vq_err(vq, "Failed to read head: idx %d address %p\n",
                       last_avail_idx,
                       &vq->avail->ring[last_avail_idx % vq->num]);
-               return vq->num;
+               return -EFAULT;
        }
 
        /* If their number is silly, that's an error. */
-       if (head >= vq->num) {
+       if (unlikely(head >= vq->num)) {
                vq_err(vq, "Guest says index %u > %u is available",
                       head, vq->num);
-               return vq->num;
+               return -EINVAL;
        }
 
        /* When we start there are none of either input nor output. */
@@ -930,41 +1160,41 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
        i = head;
        do {
                unsigned iov_count = *in_num + *out_num;
-               if (i >= vq->num) {
+               if (unlikely(i >= vq->num)) {
                        vq_err(vq, "Desc index is %u > %u, head = %u",
                               i, vq->num, head);
-                       return vq->num;
+                       return -EINVAL;
                }
-               if (++found > vq->num) {
+               if (unlikely(++found > vq->num)) {
                        vq_err(vq, "Loop detected: last one at %u "
                               "vq size %u head %u\n",
                               i, vq->num, head);
-                       return vq->num;
+                       return -EINVAL;
                }
-               ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
-               if (ret) {
+               ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
+               if (unlikely(ret)) {
                        vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
                               i, vq->desc + i);
-                       return vq->num;
+                       return -EFAULT;
                }
                if (desc.flags & VRING_DESC_F_INDIRECT) {
                        ret = get_indirect(dev, vq, iov, iov_size,
                                           out_num, in_num,
                                           log, log_num, &desc);
-                       if (ret < 0) {
+                       if (unlikely(ret < 0)) {
                                vq_err(vq, "Failure detected "
                                       "in indirect descriptor at idx %d\n", i);
-                               return vq->num;
+                               return ret;
                        }
                        continue;
                }
 
                ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
                                     iov_size - iov_count);
-               if (ret < 0) {
+               if (unlikely(ret < 0)) {
                        vq_err(vq, "Translation failure %d descriptor idx %d\n",
                               ret, i);
-                       return vq->num;
+                       return ret;
                }
                if (desc.flags & VRING_DESC_F_WRITE) {
                        /* If this is an input descriptor,
@@ -978,10 +1208,10 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
                } else {
                        /* If it's an output descriptor, they're all supposed
                         * to come before any input descriptors. */
-                       if (*in_num) {
+                       if (unlikely(*in_num)) {
                                vq_err(vq, "Descriptor has out after in: "
                                       "idx %d\n", i);
-                               return vq->num;
+                               return -EINVAL;
                        }
                        *out_num += ret;
                }
@@ -993,9 +1223,9 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
 }
 
 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
-void vhost_discard_vq_desc(struct vhost_virtqueue *vq)
+void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
 {
-       vq->last_avail_idx--;
+       vq->last_avail_idx -= n;
 }
 
 /* After we've used one of their buffers, we tell them about it.  We'll then
@@ -1007,17 +1237,17 @@ int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
        /* The virtqueue contains a ring of used buffers.  Get a pointer to the
         * next entry in that used ring. */
        used = &vq->used->ring[vq->last_used_idx % vq->num];
-       if (put_user(head, &used->id)) {
+       if (__put_user(head, &used->id)) {
                vq_err(vq, "Failed to write used id");
                return -EFAULT;
        }
-       if (put_user(len, &used->len)) {
+       if (__put_user(len, &used->len)) {
                vq_err(vq, "Failed to write used len");
                return -EFAULT;
        }
        /* Make sure buffer is written before we update index. */
        smp_wmb();
-       if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
+       if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
                vq_err(vq, "Failed to increment used idx");
                return -EFAULT;
        }
@@ -1040,16 +1270,78 @@ int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
        return 0;
 }
 
+static int __vhost_add_used_n(struct vhost_virtqueue *vq,
+                           struct vring_used_elem *heads,
+                           unsigned count)
+{
+       struct vring_used_elem __user *used;
+       int start;
+
+       start = vq->last_used_idx % vq->num;
+       used = vq->used->ring + start;
+       if (__copy_to_user(used, heads, count * sizeof *used)) {
+               vq_err(vq, "Failed to write used");
+               return -EFAULT;
+       }
+       if (unlikely(vq->log_used)) {
+               /* Make sure data is seen before log. */
+               smp_wmb();
+               /* Log used ring entry write. */
+               log_write(vq->log_base,
+                         vq->log_addr +
+                          ((void __user *)used - (void __user *)vq->used),
+                         count * sizeof *used);
+       }
+       vq->last_used_idx += count;
+       return 0;
+}
+
+/* After we've used one of their buffers, we tell them about it.  We'll then
+ * want to notify the guest, using eventfd. */
+int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
+                    unsigned count)
+{
+       int start, n, r;
+
+       start = vq->last_used_idx % vq->num;
+       n = vq->num - start;
+       if (n < count) {
+               r = __vhost_add_used_n(vq, heads, n);
+               if (r < 0)
+                       return r;
+               heads += n;
+               count -= n;
+       }
+       r = __vhost_add_used_n(vq, heads, count);
+
+       /* Make sure buffer is written before we update index. */
+       smp_wmb();
+       if (put_user(vq->last_used_idx, &vq->used->idx)) {
+               vq_err(vq, "Failed to increment used idx");
+               return -EFAULT;
+       }
+       if (unlikely(vq->log_used)) {
+               /* Log used index update. */
+               log_write(vq->log_base,
+                         vq->log_addr + offsetof(struct vring_used, idx),
+                         sizeof vq->used->idx);
+               if (vq->log_ctx)
+                       eventfd_signal(vq->log_ctx, 1);
+       }
+       return r;
+}
+
 /* This actually signals the guest, using eventfd. */
 void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
 {
        __u16 flags;
+
        /* Flush out used index updates. This is paired
         * with the barrier that the Guest executes when enabling
         * interrupts. */
        smp_mb();
 
-       if (get_user(flags, &vq->avail->flags)) {
+       if (__get_user(flags, &vq->avail->flags)) {
                vq_err(vq, "Failed to get flags");
                return;
        }
@@ -1074,11 +1366,21 @@ void vhost_add_used_and_signal(struct vhost_dev *dev,
        vhost_signal(dev, vq);
 }
 
+/* multi-buffer version of vhost_add_used_and_signal */
+void vhost_add_used_and_signal_n(struct vhost_dev *dev,
+                                struct vhost_virtqueue *vq,
+                                struct vring_used_elem *heads, unsigned count)
+{
+       vhost_add_used_n(vq, heads, count);
+       vhost_signal(dev, vq);
+}
+
 /* OK, now we need to know about added descriptors. */
 bool vhost_enable_notify(struct vhost_virtqueue *vq)
 {
        u16 avail_idx;
        int r;
+
        if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
                return false;
        vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
@@ -1091,20 +1393,21 @@ bool vhost_enable_notify(struct vhost_virtqueue *vq)
        /* They could have slipped one in as we were doing that: make
         * sure it's written, then check again. */
        smp_mb();
-       r = get_user(avail_idx, &vq->avail->idx);
+       r = __get_user(avail_idx, &vq->avail->idx);
        if (r) {
                vq_err(vq, "Failed to check avail idx at %p: %d\n",
                       &vq->avail->idx, r);
                return false;
        }
 
-       return avail_idx != vq->last_avail_idx;
+       return avail_idx != vq->avail_idx;
 }
 
 /* We don't need to be notified again. */
 void vhost_disable_notify(struct vhost_virtqueue *vq)
 {
        int r;
+
        if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
                return;
        vq->used_flags |= VRING_USED_F_NO_NOTIFY;
@@ -1113,16 +1416,3 @@ void vhost_disable_notify(struct vhost_virtqueue *vq)
                vq_err(vq, "Failed to enable notification at %p: %d\n",
                       &vq->used->flags, r);
 }
-
-int vhost_init(void)
-{
-       vhost_workqueue = create_singlethread_workqueue("vhost");
-       if (!vhost_workqueue)
-               return -ENOMEM;
-       return 0;
-}
-
-void vhost_cleanup(void)
-{
-       destroy_workqueue(vhost_workqueue);
-}