1 /* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
4 * Author: Michael S. Tsirkin <mst@redhat.com>
6 * Inspiration, some code, and most witty comments come from
7 * Documentation/lguest/lguest.c, by Rusty Russell
9 * This work is licensed under the terms of the GNU GPL, version 2.
11 * Generic code for virtio server in host kernel.
14 #include <linux/eventfd.h>
15 #include <linux/vhost.h>
16 #include <linux/virtio_net.h>
18 #include <linux/miscdevice.h>
19 #include <linux/mutex.h>
20 #include <linux/rcupdate.h>
21 #include <linux/poll.h>
22 #include <linux/file.h>
23 #include <linux/highmem.h>
24 #include <linux/slab.h>
25 #include <linux/kthread.h>
27 #include <linux/net.h>
28 #include <linux/if_packet.h>
29 #include <linux/if_arp.h>
36 VHOST_MEMORY_MAX_NREGIONS = 64,
37 VHOST_MEMORY_F_LOG = 0x1,
40 static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
43 struct vhost_poll *poll;
44 poll = container_of(pt, struct vhost_poll, table);
47 add_wait_queue(wqh, &poll->wait);
50 static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
53 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
55 if (!((unsigned long)key & poll->mask))
58 vhost_poll_queue(poll);
62 /* Init poll structure */
63 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
64 unsigned long mask, struct vhost_dev *dev)
66 struct vhost_work *work = &poll->work;
68 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
69 init_poll_funcptr(&poll->table, vhost_poll_func);
73 INIT_LIST_HEAD(&work->node);
75 init_waitqueue_head(&work->done);
77 work->queue_seq = work->done_seq = 0;
80 /* Start polling a file. We add ourselves to file's wait queue. The caller must
81 * keep a reference to a file until after vhost_poll_stop is called. */
82 void vhost_poll_start(struct vhost_poll *poll, struct file *file)
85 mask = file->f_op->poll(file, &poll->table);
87 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
90 /* Stop polling a file. After this function returns, it becomes safe to drop the
91 * file reference. You must also flush afterwards. */
92 void vhost_poll_stop(struct vhost_poll *poll)
94 remove_wait_queue(poll->wqh, &poll->wait);
97 /* Flush any work that has been scheduled. When calling this, don't hold any
98 * locks that are also used by the callback. */
99 void vhost_poll_flush(struct vhost_poll *poll)
101 struct vhost_work *work = &poll->work;
106 spin_lock_irq(&poll->dev->work_lock);
107 seq = work->queue_seq;
109 spin_unlock_irq(&poll->dev->work_lock);
110 wait_event(work->done, ({
111 spin_lock_irq(&poll->dev->work_lock);
112 left = seq - work->done_seq <= 0;
113 spin_unlock_irq(&poll->dev->work_lock);
116 spin_lock_irq(&poll->dev->work_lock);
117 flushing = --work->flushing;
118 spin_unlock_irq(&poll->dev->work_lock);
119 BUG_ON(flushing < 0);
122 void vhost_poll_queue(struct vhost_poll *poll)
124 struct vhost_dev *dev = poll->dev;
125 struct vhost_work *work = &poll->work;
128 spin_lock_irqsave(&dev->work_lock, flags);
129 if (list_empty(&work->node)) {
130 list_add_tail(&work->node, &dev->work_list);
132 wake_up_process(dev->worker);
134 spin_unlock_irqrestore(&dev->work_lock, flags);
137 static void vhost_vq_reset(struct vhost_dev *dev,
138 struct vhost_virtqueue *vq)
144 vq->last_avail_idx = 0;
146 vq->last_used_idx = 0;
149 vq->log_used = false;
150 vq->log_addr = -1ull;
152 vq->private_data = NULL;
154 vq->error_ctx = NULL;
162 static int vhost_worker(void *data)
164 struct vhost_dev *dev = data;
165 struct vhost_work *work = NULL;
166 unsigned uninitialized_var(seq);
169 /* mb paired w/ kthread_stop */
170 set_current_state(TASK_INTERRUPTIBLE);
172 spin_lock_irq(&dev->work_lock);
174 work->done_seq = seq;
176 wake_up_all(&work->done);
179 if (kthread_should_stop()) {
180 spin_unlock_irq(&dev->work_lock);
181 __set_current_state(TASK_RUNNING);
184 if (!list_empty(&dev->work_list)) {
185 work = list_first_entry(&dev->work_list,
186 struct vhost_work, node);
187 list_del_init(&work->node);
188 seq = work->queue_seq;
191 spin_unlock_irq(&dev->work_lock);
194 __set_current_state(TASK_RUNNING);
202 long vhost_dev_init(struct vhost_dev *dev,
203 struct vhost_virtqueue *vqs, int nvqs)
209 mutex_init(&dev->mutex);
211 dev->log_file = NULL;
214 spin_lock_init(&dev->work_lock);
215 INIT_LIST_HEAD(&dev->work_list);
218 for (i = 0; i < dev->nvqs; ++i) {
219 dev->vqs[i].dev = dev;
220 mutex_init(&dev->vqs[i].mutex);
221 vhost_vq_reset(dev, dev->vqs + i);
222 if (dev->vqs[i].handle_kick)
223 vhost_poll_init(&dev->vqs[i].poll,
224 dev->vqs[i].handle_kick, POLLIN, dev);
230 /* Caller should have device mutex */
231 long vhost_dev_check_owner(struct vhost_dev *dev)
233 /* Are you the owner? If not, I don't think you mean to do that */
234 return dev->mm == current->mm ? 0 : -EPERM;
237 /* Caller should have device mutex */
238 static long vhost_dev_set_owner(struct vhost_dev *dev)
240 struct task_struct *worker;
242 /* Is there an owner already? */
247 /* No owner, become one */
248 dev->mm = get_task_mm(current);
249 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
250 if (IS_ERR(worker)) {
251 err = PTR_ERR(worker);
255 dev->worker = worker;
256 wake_up_process(worker); /* avoid contributing to loadavg */
267 /* Caller should have device mutex */
268 long vhost_dev_reset_owner(struct vhost_dev *dev)
270 struct vhost_memory *memory;
272 /* Restore memory to default empty mapping. */
273 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
277 vhost_dev_cleanup(dev);
279 memory->nregions = 0;
280 dev->memory = memory;
284 /* Caller should have device mutex */
285 void vhost_dev_cleanup(struct vhost_dev *dev)
288 for (i = 0; i < dev->nvqs; ++i) {
289 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
290 vhost_poll_stop(&dev->vqs[i].poll);
291 vhost_poll_flush(&dev->vqs[i].poll);
293 if (dev->vqs[i].error_ctx)
294 eventfd_ctx_put(dev->vqs[i].error_ctx);
295 if (dev->vqs[i].error)
296 fput(dev->vqs[i].error);
297 if (dev->vqs[i].kick)
298 fput(dev->vqs[i].kick);
299 if (dev->vqs[i].call_ctx)
300 eventfd_ctx_put(dev->vqs[i].call_ctx);
301 if (dev->vqs[i].call)
302 fput(dev->vqs[i].call);
303 vhost_vq_reset(dev, dev->vqs + i);
306 eventfd_ctx_put(dev->log_ctx);
310 dev->log_file = NULL;
311 /* No one will access memory at this point */
318 WARN_ON(!list_empty(&dev->work_list));
319 kthread_stop(dev->worker);
322 static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
324 u64 a = addr / VHOST_PAGE_SIZE / 8;
325 /* Make sure 64 bit math will not overflow. */
326 if (a > ULONG_MAX - (unsigned long)log_base ||
327 a + (unsigned long)log_base > ULONG_MAX)
330 return access_ok(VERIFY_WRITE, log_base + a,
331 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
334 /* Caller should have vq mutex and device mutex. */
335 static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
343 for (i = 0; i < mem->nregions; ++i) {
344 struct vhost_memory_region *m = mem->regions + i;
345 unsigned long a = m->userspace_addr;
346 if (m->memory_size > ULONG_MAX)
348 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
351 else if (log_all && !log_access_ok(log_base,
359 /* Can we switch to this memory table? */
360 /* Caller should have device mutex but not vq mutex */
361 static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
365 for (i = 0; i < d->nvqs; ++i) {
367 mutex_lock(&d->vqs[i].mutex);
368 /* If ring is inactive, will check when it's enabled. */
369 if (d->vqs[i].private_data)
370 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
374 mutex_unlock(&d->vqs[i].mutex);
381 static int vq_access_ok(unsigned int num,
382 struct vring_desc __user *desc,
383 struct vring_avail __user *avail,
384 struct vring_used __user *used)
386 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
387 access_ok(VERIFY_READ, avail,
388 sizeof *avail + num * sizeof *avail->ring) &&
389 access_ok(VERIFY_WRITE, used,
390 sizeof *used + num * sizeof *used->ring);
393 /* Can we log writes? */
394 /* Caller should have device mutex but not vq mutex */
395 int vhost_log_access_ok(struct vhost_dev *dev)
397 return memory_access_ok(dev, dev->memory, 1);
400 /* Verify access for write logging. */
401 /* Caller should have vq mutex and device mutex */
402 static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
404 return vq_memory_access_ok(log_base, vq->dev->memory,
405 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
406 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
408 vq->num * sizeof *vq->used->ring));
411 /* Can we start vq? */
412 /* Caller should have vq mutex and device mutex */
413 int vhost_vq_access_ok(struct vhost_virtqueue *vq)
415 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
416 vq_log_access_ok(vq, vq->log_base);
419 static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
421 struct vhost_memory mem, *newmem, *oldmem;
422 unsigned long size = offsetof(struct vhost_memory, regions);
423 if (copy_from_user(&mem, m, size))
427 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
429 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
433 memcpy(newmem, &mem, size);
434 if (copy_from_user(newmem->regions, m->regions,
435 mem.nregions * sizeof *m->regions)) {
440 if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
445 rcu_assign_pointer(d->memory, newmem);
451 static int init_used(struct vhost_virtqueue *vq,
452 struct vring_used __user *used)
454 int r = put_user(vq->used_flags, &used->flags);
457 return get_user(vq->last_used_idx, &used->idx);
460 static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
462 struct file *eventfp, *filep = NULL,
463 *pollstart = NULL, *pollstop = NULL;
464 struct eventfd_ctx *ctx = NULL;
465 u32 __user *idxp = argp;
466 struct vhost_virtqueue *vq;
467 struct vhost_vring_state s;
468 struct vhost_vring_file f;
469 struct vhost_vring_addr a;
473 r = get_user(idx, idxp);
481 mutex_lock(&vq->mutex);
484 case VHOST_SET_VRING_NUM:
485 /* Resizing ring with an active backend?
486 * You don't want to do that. */
487 if (vq->private_data) {
491 if (copy_from_user(&s, argp, sizeof s)) {
495 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
501 case VHOST_SET_VRING_BASE:
502 /* Moving base with an active backend?
503 * You don't want to do that. */
504 if (vq->private_data) {
508 if (copy_from_user(&s, argp, sizeof s)) {
512 if (s.num > 0xffff) {
516 vq->last_avail_idx = s.num;
517 /* Forget the cached index value. */
518 vq->avail_idx = vq->last_avail_idx;
520 case VHOST_GET_VRING_BASE:
522 s.num = vq->last_avail_idx;
523 if (copy_to_user(argp, &s, sizeof s))
526 case VHOST_SET_VRING_ADDR:
527 if (copy_from_user(&a, argp, sizeof a)) {
531 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
535 /* For 32bit, verify that the top 32bits of the user
536 data are set to zero. */
537 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
538 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
539 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
543 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
544 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
545 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
550 /* We only verify access here if backend is configured.
551 * If it is not, we don't as size might not have been setup.
552 * We will verify when backend is configured. */
553 if (vq->private_data) {
554 if (!vq_access_ok(vq->num,
555 (void __user *)(unsigned long)a.desc_user_addr,
556 (void __user *)(unsigned long)a.avail_user_addr,
557 (void __user *)(unsigned long)a.used_user_addr)) {
562 /* Also validate log access for used ring if enabled. */
563 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
564 !log_access_ok(vq->log_base, a.log_guest_addr,
566 vq->num * sizeof *vq->used->ring)) {
572 r = init_used(vq, (struct vring_used __user *)(unsigned long)
576 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
577 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
578 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
579 vq->log_addr = a.log_guest_addr;
580 vq->used = (void __user *)(unsigned long)a.used_user_addr;
582 case VHOST_SET_VRING_KICK:
583 if (copy_from_user(&f, argp, sizeof f)) {
587 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
588 if (IS_ERR(eventfp)) {
589 r = PTR_ERR(eventfp);
592 if (eventfp != vq->kick) {
593 pollstop = filep = vq->kick;
594 pollstart = vq->kick = eventfp;
598 case VHOST_SET_VRING_CALL:
599 if (copy_from_user(&f, argp, sizeof f)) {
603 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
604 if (IS_ERR(eventfp)) {
605 r = PTR_ERR(eventfp);
608 if (eventfp != vq->call) {
612 vq->call_ctx = eventfp ?
613 eventfd_ctx_fileget(eventfp) : NULL;
617 case VHOST_SET_VRING_ERR:
618 if (copy_from_user(&f, argp, sizeof f)) {
622 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
623 if (IS_ERR(eventfp)) {
624 r = PTR_ERR(eventfp);
627 if (eventfp != vq->error) {
631 vq->error_ctx = eventfp ?
632 eventfd_ctx_fileget(eventfp) : NULL;
640 if (pollstop && vq->handle_kick)
641 vhost_poll_stop(&vq->poll);
644 eventfd_ctx_put(ctx);
648 if (pollstart && vq->handle_kick)
649 vhost_poll_start(&vq->poll, vq->kick);
651 mutex_unlock(&vq->mutex);
653 if (pollstop && vq->handle_kick)
654 vhost_poll_flush(&vq->poll);
658 /* Caller must have device mutex */
659 long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
661 void __user *argp = (void __user *)arg;
662 struct file *eventfp, *filep = NULL;
663 struct eventfd_ctx *ctx = NULL;
668 /* If you are not the owner, you can become one */
669 if (ioctl == VHOST_SET_OWNER) {
670 r = vhost_dev_set_owner(d);
674 /* You must be the owner to do anything else */
675 r = vhost_dev_check_owner(d);
680 case VHOST_SET_MEM_TABLE:
681 r = vhost_set_memory(d, argp);
683 case VHOST_SET_LOG_BASE:
684 if (copy_from_user(&p, argp, sizeof p)) {
688 if ((u64)(unsigned long)p != p) {
692 for (i = 0; i < d->nvqs; ++i) {
693 struct vhost_virtqueue *vq;
694 void __user *base = (void __user *)(unsigned long)p;
696 mutex_lock(&vq->mutex);
697 /* If ring is inactive, will check when it's enabled. */
698 if (vq->private_data && !vq_log_access_ok(vq, base))
702 mutex_unlock(&vq->mutex);
705 case VHOST_SET_LOG_FD:
706 r = get_user(fd, (int __user *)argp);
709 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
710 if (IS_ERR(eventfp)) {
711 r = PTR_ERR(eventfp);
714 if (eventfp != d->log_file) {
717 d->log_ctx = eventfp ?
718 eventfd_ctx_fileget(eventfp) : NULL;
721 for (i = 0; i < d->nvqs; ++i) {
722 mutex_lock(&d->vqs[i].mutex);
723 d->vqs[i].log_ctx = d->log_ctx;
724 mutex_unlock(&d->vqs[i].mutex);
727 eventfd_ctx_put(ctx);
732 r = vhost_set_vring(d, ioctl, argp);
739 static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
740 __u64 addr, __u32 len)
742 struct vhost_memory_region *reg;
744 /* linear search is not brilliant, but we really have on the order of 6
745 * regions in practice */
746 for (i = 0; i < mem->nregions; ++i) {
747 reg = mem->regions + i;
748 if (reg->guest_phys_addr <= addr &&
749 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
755 /* TODO: This is really inefficient. We need something like get_user()
756 * (instruction directly accesses the data, with an exception table entry
757 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
759 static int set_bit_to_user(int nr, void __user *addr)
761 unsigned long log = (unsigned long)addr;
764 int bit = nr + (log % PAGE_SIZE) * 8;
766 r = get_user_pages_fast(log, 1, 1, &page);
770 base = kmap_atomic(page, KM_USER0);
772 kunmap_atomic(base, KM_USER0);
773 set_page_dirty_lock(page);
778 static int log_write(void __user *log_base,
779 u64 write_address, u64 write_length)
784 write_address /= VHOST_PAGE_SIZE;
786 u64 base = (u64)(unsigned long)log_base;
787 u64 log = base + write_address / 8;
788 int bit = write_address % 8;
789 if ((u64)(unsigned long)log != log)
791 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
794 if (write_length <= VHOST_PAGE_SIZE)
796 write_length -= VHOST_PAGE_SIZE;
797 write_address += VHOST_PAGE_SIZE;
802 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
803 unsigned int log_num, u64 len)
807 /* Make sure data written is seen before log. */
809 for (i = 0; i < log_num; ++i) {
810 u64 l = min(log[i].len, len);
811 r = log_write(vq->log_base, log[i].addr, l);
819 eventfd_signal(vq->log_ctx, 1);
820 /* Length written exceeds what we have stored. This is a bug. */
825 static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
826 struct iovec iov[], int iov_size)
828 const struct vhost_memory_region *reg;
829 struct vhost_memory *mem;
836 mem = rcu_dereference(dev->memory);
837 while ((u64)len > s) {
839 if (unlikely(ret >= iov_size)) {
843 reg = find_region(mem, addr, len);
844 if (unlikely(!reg)) {
849 size = reg->memory_size - addr + reg->guest_phys_addr;
850 _iov->iov_len = min((u64)len, size);
851 _iov->iov_base = (void __user *)(unsigned long)
852 (reg->userspace_addr + addr - reg->guest_phys_addr);
862 /* Each buffer in the virtqueues is actually a chain of descriptors. This
863 * function returns the next descriptor in the chain,
864 * or -1U if we're at the end. */
865 static unsigned next_desc(struct vring_desc *desc)
869 /* If this descriptor says it doesn't chain, we're done. */
870 if (!(desc->flags & VRING_DESC_F_NEXT))
873 /* Check they're not leading us off end of descriptors. */
875 /* Make sure compiler knows to grab that: we don't want it changing! */
876 /* We will use the result as an index in an array, so most
877 * architectures only need a compiler barrier here. */
878 read_barrier_depends();
883 static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
884 struct iovec iov[], unsigned int iov_size,
885 unsigned int *out_num, unsigned int *in_num,
886 struct vhost_log *log, unsigned int *log_num,
887 struct vring_desc *indirect)
889 struct vring_desc desc;
890 unsigned int i = 0, count, found = 0;
894 if (unlikely(indirect->len % sizeof desc)) {
895 vq_err(vq, "Invalid length in indirect descriptor: "
896 "len 0x%llx not multiple of 0x%zx\n",
897 (unsigned long long)indirect->len,
902 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
903 ARRAY_SIZE(vq->indirect));
904 if (unlikely(ret < 0)) {
905 vq_err(vq, "Translation failure %d in indirect.\n", ret);
909 /* We will use the result as an address to read from, so most
910 * architectures only need a compiler barrier here. */
911 read_barrier_depends();
913 count = indirect->len / sizeof desc;
914 /* Buffers are chained via a 16 bit next field, so
915 * we can have at most 2^16 of these. */
916 if (unlikely(count > USHRT_MAX + 1)) {
917 vq_err(vq, "Indirect buffer length too big: %d\n",
923 unsigned iov_count = *in_num + *out_num;
924 if (unlikely(++found > count)) {
925 vq_err(vq, "Loop detected: last one at %u "
926 "indirect size %u\n",
930 if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
932 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
933 i, (size_t)indirect->addr + i * sizeof desc);
936 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
937 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
938 i, (size_t)indirect->addr + i * sizeof desc);
942 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
943 iov_size - iov_count);
944 if (unlikely(ret < 0)) {
945 vq_err(vq, "Translation failure %d indirect idx %d\n",
949 /* If this is an input descriptor, increment that count. */
950 if (desc.flags & VRING_DESC_F_WRITE) {
953 log[*log_num].addr = desc.addr;
954 log[*log_num].len = desc.len;
958 /* If it's an output descriptor, they're all supposed
959 * to come before any input descriptors. */
960 if (unlikely(*in_num)) {
961 vq_err(vq, "Indirect descriptor "
962 "has out after in: idx %d\n", i);
967 } while ((i = next_desc(&desc)) != -1);
971 /* This looks in the virtqueue and for the first available buffer, and converts
972 * it to an iovec for convenient access. Since descriptors consist of some
973 * number of output then some number of input descriptors, it's actually two
974 * iovecs, but we pack them into one and note how many of each there were.
976 * This function returns the descriptor number found, or vq->num (which is
977 * never a valid descriptor number) if none was found. A negative code is
978 * returned on error. */
979 int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
980 struct iovec iov[], unsigned int iov_size,
981 unsigned int *out_num, unsigned int *in_num,
982 struct vhost_log *log, unsigned int *log_num)
984 struct vring_desc desc;
985 unsigned int i, head, found = 0;
989 /* Check it isn't doing very strange things with descriptor numbers. */
990 last_avail_idx = vq->last_avail_idx;
991 if (unlikely(get_user(vq->avail_idx, &vq->avail->idx))) {
992 vq_err(vq, "Failed to access avail idx at %p\n",
997 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
998 vq_err(vq, "Guest moved used index from %u to %u",
999 last_avail_idx, vq->avail_idx);
1003 /* If there's nothing new since last we looked, return invalid. */
1004 if (vq->avail_idx == last_avail_idx)
1007 /* Only get avail ring entries after they have been exposed by guest. */
1010 /* Grab the next descriptor number they're advertising, and increment
1011 * the index we've seen. */
1012 if (unlikely(get_user(head,
1013 &vq->avail->ring[last_avail_idx % vq->num]))) {
1014 vq_err(vq, "Failed to read head: idx %d address %p\n",
1016 &vq->avail->ring[last_avail_idx % vq->num]);
1020 /* If their number is silly, that's an error. */
1021 if (unlikely(head >= vq->num)) {
1022 vq_err(vq, "Guest says index %u > %u is available",
1027 /* When we start there are none of either input nor output. */
1028 *out_num = *in_num = 0;
1034 unsigned iov_count = *in_num + *out_num;
1035 if (unlikely(i >= vq->num)) {
1036 vq_err(vq, "Desc index is %u > %u, head = %u",
1040 if (unlikely(++found > vq->num)) {
1041 vq_err(vq, "Loop detected: last one at %u "
1042 "vq size %u head %u\n",
1046 ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
1047 if (unlikely(ret)) {
1048 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1052 if (desc.flags & VRING_DESC_F_INDIRECT) {
1053 ret = get_indirect(dev, vq, iov, iov_size,
1055 log, log_num, &desc);
1056 if (unlikely(ret < 0)) {
1057 vq_err(vq, "Failure detected "
1058 "in indirect descriptor at idx %d\n", i);
1064 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1065 iov_size - iov_count);
1066 if (unlikely(ret < 0)) {
1067 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1071 if (desc.flags & VRING_DESC_F_WRITE) {
1072 /* If this is an input descriptor,
1073 * increment that count. */
1075 if (unlikely(log)) {
1076 log[*log_num].addr = desc.addr;
1077 log[*log_num].len = desc.len;
1081 /* If it's an output descriptor, they're all supposed
1082 * to come before any input descriptors. */
1083 if (unlikely(*in_num)) {
1084 vq_err(vq, "Descriptor has out after in: "
1090 } while ((i = next_desc(&desc)) != -1);
1092 /* On success, increment avail index. */
1093 vq->last_avail_idx++;
1097 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
1098 void vhost_discard_vq_desc(struct vhost_virtqueue *vq)
1100 vq->last_avail_idx--;
1103 /* After we've used one of their buffers, we tell them about it. We'll then
1104 * want to notify the guest, using eventfd. */
1105 int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1107 struct vring_used_elem __user *used;
1109 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1110 * next entry in that used ring. */
1111 used = &vq->used->ring[vq->last_used_idx % vq->num];
1112 if (put_user(head, &used->id)) {
1113 vq_err(vq, "Failed to write used id");
1116 if (put_user(len, &used->len)) {
1117 vq_err(vq, "Failed to write used len");
1120 /* Make sure buffer is written before we update index. */
1122 if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
1123 vq_err(vq, "Failed to increment used idx");
1126 if (unlikely(vq->log_used)) {
1127 /* Make sure data is seen before log. */
1129 /* Log used ring entry write. */
1130 log_write(vq->log_base,
1132 ((void __user *)used - (void __user *)vq->used),
1134 /* Log used index update. */
1135 log_write(vq->log_base,
1136 vq->log_addr + offsetof(struct vring_used, idx),
1137 sizeof vq->used->idx);
1139 eventfd_signal(vq->log_ctx, 1);
1141 vq->last_used_idx++;
1145 /* This actually signals the guest, using eventfd. */
1146 void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1149 /* Flush out used index updates. This is paired
1150 * with the barrier that the Guest executes when enabling
1154 if (get_user(flags, &vq->avail->flags)) {
1155 vq_err(vq, "Failed to get flags");
1159 /* If they don't want an interrupt, don't signal, unless empty. */
1160 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1161 (vq->avail_idx != vq->last_avail_idx ||
1162 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1165 /* Signal the Guest tell them we used something up. */
1167 eventfd_signal(vq->call_ctx, 1);
1170 /* And here's the combo meal deal. Supersize me! */
1171 void vhost_add_used_and_signal(struct vhost_dev *dev,
1172 struct vhost_virtqueue *vq,
1173 unsigned int head, int len)
1175 vhost_add_used(vq, head, len);
1176 vhost_signal(dev, vq);
1179 /* OK, now we need to know about added descriptors. */
1180 bool vhost_enable_notify(struct vhost_virtqueue *vq)
1184 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1186 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1187 r = put_user(vq->used_flags, &vq->used->flags);
1189 vq_err(vq, "Failed to enable notification at %p: %d\n",
1190 &vq->used->flags, r);
1193 /* They could have slipped one in as we were doing that: make
1194 * sure it's written, then check again. */
1196 r = get_user(avail_idx, &vq->avail->idx);
1198 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1199 &vq->avail->idx, r);
1203 return avail_idx != vq->last_avail_idx;
1206 /* We don't need to be notified again. */
1207 void vhost_disable_notify(struct vhost_virtqueue *vq)
1210 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1212 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1213 r = put_user(vq->used_flags, &vq->used->flags);
1215 vq_err(vq, "Failed to enable notification at %p: %d\n",
1216 &vq->used->flags, r);