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/workqueue.h>
21 #include <linux/rcupdate.h>
22 #include <linux/poll.h>
23 #include <linux/file.h>
24 #include <linux/highmem.h>
25 #include <linux/slab.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 struct workqueue_struct *vhost_workqueue;
42 static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
45 struct vhost_poll *poll;
46 poll = container_of(pt, struct vhost_poll, table);
49 add_wait_queue(wqh, &poll->wait);
52 static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
55 struct vhost_poll *poll;
56 poll = container_of(wait, struct vhost_poll, wait);
57 if (!((unsigned long)key & poll->mask))
60 queue_work(vhost_workqueue, &poll->work);
64 /* Init poll structure */
65 void vhost_poll_init(struct vhost_poll *poll, work_func_t func,
68 INIT_WORK(&poll->work, func);
69 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
70 init_poll_funcptr(&poll->table, vhost_poll_func);
74 /* Start polling a file. We add ourselves to file's wait queue. The caller must
75 * keep a reference to a file until after vhost_poll_stop is called. */
76 void vhost_poll_start(struct vhost_poll *poll, struct file *file)
79 mask = file->f_op->poll(file, &poll->table);
81 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
84 /* Stop polling a file. After this function returns, it becomes safe to drop the
85 * file reference. You must also flush afterwards. */
86 void vhost_poll_stop(struct vhost_poll *poll)
88 remove_wait_queue(poll->wqh, &poll->wait);
91 /* Flush any work that has been scheduled. When calling this, don't hold any
92 * locks that are also used by the callback. */
93 void vhost_poll_flush(struct vhost_poll *poll)
95 flush_work(&poll->work);
98 void vhost_poll_queue(struct vhost_poll *poll)
100 queue_work(vhost_workqueue, &poll->work);
103 static void vhost_vq_reset(struct vhost_dev *dev,
104 struct vhost_virtqueue *vq)
110 vq->last_avail_idx = 0;
112 vq->last_used_idx = 0;
115 vq->log_used = false;
116 vq->log_addr = -1ull;
118 vq->private_data = NULL;
120 vq->error_ctx = NULL;
128 long vhost_dev_init(struct vhost_dev *dev,
129 struct vhost_virtqueue *vqs, int nvqs)
134 mutex_init(&dev->mutex);
136 dev->log_file = NULL;
140 for (i = 0; i < dev->nvqs; ++i) {
141 dev->vqs[i].dev = dev;
142 mutex_init(&dev->vqs[i].mutex);
143 vhost_vq_reset(dev, dev->vqs + i);
144 if (dev->vqs[i].handle_kick)
145 vhost_poll_init(&dev->vqs[i].poll,
146 dev->vqs[i].handle_kick,
152 /* Caller should have device mutex */
153 long vhost_dev_check_owner(struct vhost_dev *dev)
155 /* Are you the owner? If not, I don't think you mean to do that */
156 return dev->mm == current->mm ? 0 : -EPERM;
159 /* Caller should have device mutex */
160 static long vhost_dev_set_owner(struct vhost_dev *dev)
162 /* Is there an owner already? */
165 /* No owner, become one */
166 dev->mm = get_task_mm(current);
170 /* Caller should have device mutex */
171 long vhost_dev_reset_owner(struct vhost_dev *dev)
173 struct vhost_memory *memory;
175 /* Restore memory to default empty mapping. */
176 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
180 vhost_dev_cleanup(dev);
182 memory->nregions = 0;
183 dev->memory = memory;
187 /* Caller should have device mutex */
188 void vhost_dev_cleanup(struct vhost_dev *dev)
191 for (i = 0; i < dev->nvqs; ++i) {
192 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
193 vhost_poll_stop(&dev->vqs[i].poll);
194 vhost_poll_flush(&dev->vqs[i].poll);
196 if (dev->vqs[i].error_ctx)
197 eventfd_ctx_put(dev->vqs[i].error_ctx);
198 if (dev->vqs[i].error)
199 fput(dev->vqs[i].error);
200 if (dev->vqs[i].kick)
201 fput(dev->vqs[i].kick);
202 if (dev->vqs[i].call_ctx)
203 eventfd_ctx_put(dev->vqs[i].call_ctx);
204 if (dev->vqs[i].call)
205 fput(dev->vqs[i].call);
206 vhost_vq_reset(dev, dev->vqs + i);
209 eventfd_ctx_put(dev->log_ctx);
213 dev->log_file = NULL;
214 /* No one will access memory at this point */
222 static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
224 u64 a = addr / VHOST_PAGE_SIZE / 8;
225 /* Make sure 64 bit math will not overflow. */
226 if (a > ULONG_MAX - (unsigned long)log_base ||
227 a + (unsigned long)log_base > ULONG_MAX)
230 return access_ok(VERIFY_WRITE, log_base + a,
231 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
234 /* Caller should have vq mutex and device mutex. */
235 static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
243 for (i = 0; i < mem->nregions; ++i) {
244 struct vhost_memory_region *m = mem->regions + i;
245 unsigned long a = m->userspace_addr;
246 if (m->memory_size > ULONG_MAX)
248 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
251 else if (log_all && !log_access_ok(log_base,
259 /* Can we switch to this memory table? */
260 /* Caller should have device mutex but not vq mutex */
261 static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
265 for (i = 0; i < d->nvqs; ++i) {
267 mutex_lock(&d->vqs[i].mutex);
268 /* If ring is inactive, will check when it's enabled. */
269 if (d->vqs[i].private_data)
270 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
274 mutex_unlock(&d->vqs[i].mutex);
281 static int vq_access_ok(unsigned int num,
282 struct vring_desc __user *desc,
283 struct vring_avail __user *avail,
284 struct vring_used __user *used)
286 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
287 access_ok(VERIFY_READ, avail,
288 sizeof *avail + num * sizeof *avail->ring) &&
289 access_ok(VERIFY_WRITE, used,
290 sizeof *used + num * sizeof *used->ring);
293 /* Can we log writes? */
294 /* Caller should have device mutex but not vq mutex */
295 int vhost_log_access_ok(struct vhost_dev *dev)
297 return memory_access_ok(dev, dev->memory, 1);
300 /* Verify access for write logging. */
301 /* Caller should have vq mutex and device mutex */
302 static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
304 return vq_memory_access_ok(log_base, vq->dev->memory,
305 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
306 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
308 vq->num * sizeof *vq->used->ring));
311 /* Can we start vq? */
312 /* Caller should have vq mutex and device mutex */
313 int vhost_vq_access_ok(struct vhost_virtqueue *vq)
315 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
316 vq_log_access_ok(vq, vq->log_base);
319 static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
321 struct vhost_memory mem, *newmem, *oldmem;
322 unsigned long size = offsetof(struct vhost_memory, regions);
323 if (copy_from_user(&mem, m, size))
327 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
329 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
333 memcpy(newmem, &mem, size);
334 if (copy_from_user(newmem->regions, m->regions,
335 mem.nregions * sizeof *m->regions)) {
340 if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
345 rcu_assign_pointer(d->memory, newmem);
351 static int init_used(struct vhost_virtqueue *vq,
352 struct vring_used __user *used)
354 int r = put_user(vq->used_flags, &used->flags);
357 return get_user(vq->last_used_idx, &used->idx);
360 static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
362 struct file *eventfp, *filep = NULL,
363 *pollstart = NULL, *pollstop = NULL;
364 struct eventfd_ctx *ctx = NULL;
365 u32 __user *idxp = argp;
366 struct vhost_virtqueue *vq;
367 struct vhost_vring_state s;
368 struct vhost_vring_file f;
369 struct vhost_vring_addr a;
373 r = get_user(idx, idxp);
381 mutex_lock(&vq->mutex);
384 case VHOST_SET_VRING_NUM:
385 /* Resizing ring with an active backend?
386 * You don't want to do that. */
387 if (vq->private_data) {
391 if (copy_from_user(&s, argp, sizeof s)) {
395 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
401 case VHOST_SET_VRING_BASE:
402 /* Moving base with an active backend?
403 * You don't want to do that. */
404 if (vq->private_data) {
408 if (copy_from_user(&s, argp, sizeof s)) {
412 if (s.num > 0xffff) {
416 vq->last_avail_idx = s.num;
417 /* Forget the cached index value. */
418 vq->avail_idx = vq->last_avail_idx;
420 case VHOST_GET_VRING_BASE:
422 s.num = vq->last_avail_idx;
423 if (copy_to_user(argp, &s, sizeof s))
426 case VHOST_SET_VRING_ADDR:
427 if (copy_from_user(&a, argp, sizeof a)) {
431 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
435 /* For 32bit, verify that the top 32bits of the user
436 data are set to zero. */
437 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
438 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
439 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
443 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
444 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
445 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
450 /* We only verify access here if backend is configured.
451 * If it is not, we don't as size might not have been setup.
452 * We will verify when backend is configured. */
453 if (vq->private_data) {
454 if (!vq_access_ok(vq->num,
455 (void __user *)(unsigned long)a.desc_user_addr,
456 (void __user *)(unsigned long)a.avail_user_addr,
457 (void __user *)(unsigned long)a.used_user_addr)) {
462 /* Also validate log access for used ring if enabled. */
463 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
464 !log_access_ok(vq->log_base, a.log_guest_addr,
466 vq->num * sizeof *vq->used->ring)) {
472 r = init_used(vq, (struct vring_used __user *)(unsigned long)
476 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
477 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
478 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
479 vq->log_addr = a.log_guest_addr;
480 vq->used = (void __user *)(unsigned long)a.used_user_addr;
482 case VHOST_SET_VRING_KICK:
483 if (copy_from_user(&f, argp, sizeof f)) {
487 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
488 if (IS_ERR(eventfp)) {
489 r = PTR_ERR(eventfp);
492 if (eventfp != vq->kick) {
493 pollstop = filep = vq->kick;
494 pollstart = vq->kick = eventfp;
498 case VHOST_SET_VRING_CALL:
499 if (copy_from_user(&f, argp, sizeof f)) {
503 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
504 if (IS_ERR(eventfp)) {
505 r = PTR_ERR(eventfp);
508 if (eventfp != vq->call) {
512 vq->call_ctx = eventfp ?
513 eventfd_ctx_fileget(eventfp) : NULL;
517 case VHOST_SET_VRING_ERR:
518 if (copy_from_user(&f, argp, sizeof f)) {
522 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
523 if (IS_ERR(eventfp)) {
524 r = PTR_ERR(eventfp);
527 if (eventfp != vq->error) {
531 vq->error_ctx = eventfp ?
532 eventfd_ctx_fileget(eventfp) : NULL;
540 if (pollstop && vq->handle_kick)
541 vhost_poll_stop(&vq->poll);
544 eventfd_ctx_put(ctx);
548 if (pollstart && vq->handle_kick)
549 vhost_poll_start(&vq->poll, vq->kick);
551 mutex_unlock(&vq->mutex);
553 if (pollstop && vq->handle_kick)
554 vhost_poll_flush(&vq->poll);
558 /* Caller must have device mutex */
559 long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
561 void __user *argp = (void __user *)arg;
562 struct file *eventfp, *filep = NULL;
563 struct eventfd_ctx *ctx = NULL;
568 /* If you are not the owner, you can become one */
569 if (ioctl == VHOST_SET_OWNER) {
570 r = vhost_dev_set_owner(d);
574 /* You must be the owner to do anything else */
575 r = vhost_dev_check_owner(d);
580 case VHOST_SET_MEM_TABLE:
581 r = vhost_set_memory(d, argp);
583 case VHOST_SET_LOG_BASE:
584 if (copy_from_user(&p, argp, sizeof p)) {
588 if ((u64)(unsigned long)p != p) {
592 for (i = 0; i < d->nvqs; ++i) {
593 struct vhost_virtqueue *vq;
594 void __user *base = (void __user *)(unsigned long)p;
596 mutex_lock(&vq->mutex);
597 /* If ring is inactive, will check when it's enabled. */
598 if (vq->private_data && !vq_log_access_ok(vq, base))
602 mutex_unlock(&vq->mutex);
605 case VHOST_SET_LOG_FD:
606 r = get_user(fd, (int __user *)argp);
609 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
610 if (IS_ERR(eventfp)) {
611 r = PTR_ERR(eventfp);
614 if (eventfp != d->log_file) {
617 d->log_ctx = eventfp ?
618 eventfd_ctx_fileget(eventfp) : NULL;
621 for (i = 0; i < d->nvqs; ++i) {
622 mutex_lock(&d->vqs[i].mutex);
623 d->vqs[i].log_ctx = d->log_ctx;
624 mutex_unlock(&d->vqs[i].mutex);
627 eventfd_ctx_put(ctx);
632 r = vhost_set_vring(d, ioctl, argp);
639 static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
640 __u64 addr, __u32 len)
642 struct vhost_memory_region *reg;
644 /* linear search is not brilliant, but we really have on the order of 6
645 * regions in practice */
646 for (i = 0; i < mem->nregions; ++i) {
647 reg = mem->regions + i;
648 if (reg->guest_phys_addr <= addr &&
649 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
655 /* TODO: This is really inefficient. We need something like get_user()
656 * (instruction directly accesses the data, with an exception table entry
657 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
659 static int set_bit_to_user(int nr, void __user *addr)
661 unsigned long log = (unsigned long)addr;
664 int bit = nr + (log % PAGE_SIZE) * 8;
666 r = get_user_pages_fast(log, 1, 1, &page);
670 base = kmap_atomic(page, KM_USER0);
672 kunmap_atomic(base, KM_USER0);
673 set_page_dirty_lock(page);
678 static int log_write(void __user *log_base,
679 u64 write_address, u64 write_length)
684 write_address /= VHOST_PAGE_SIZE;
686 u64 base = (u64)(unsigned long)log_base;
687 u64 log = base + write_address / 8;
688 int bit = write_address % 8;
689 if ((u64)(unsigned long)log != log)
691 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
694 if (write_length <= VHOST_PAGE_SIZE)
696 write_length -= VHOST_PAGE_SIZE;
697 write_address += VHOST_PAGE_SIZE;
702 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
703 unsigned int log_num, u64 len)
707 /* Make sure data written is seen before log. */
709 for (i = 0; i < log_num; ++i) {
710 u64 l = min(log[i].len, len);
711 r = log_write(vq->log_base, log[i].addr, l);
719 eventfd_signal(vq->log_ctx, 1);
720 /* Length written exceeds what we have stored. This is a bug. */
725 static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
726 struct iovec iov[], int iov_size)
728 const struct vhost_memory_region *reg;
729 struct vhost_memory *mem;
736 mem = rcu_dereference(dev->memory);
737 while ((u64)len > s) {
739 if (unlikely(ret >= iov_size)) {
743 reg = find_region(mem, addr, len);
744 if (unlikely(!reg)) {
749 size = reg->memory_size - addr + reg->guest_phys_addr;
750 _iov->iov_len = min((u64)len, size);
751 _iov->iov_base = (void __user *)(unsigned long)
752 (reg->userspace_addr + addr - reg->guest_phys_addr);
762 /* Each buffer in the virtqueues is actually a chain of descriptors. This
763 * function returns the next descriptor in the chain,
764 * or -1U if we're at the end. */
765 static unsigned next_desc(struct vring_desc *desc)
769 /* If this descriptor says it doesn't chain, we're done. */
770 if (!(desc->flags & VRING_DESC_F_NEXT))
773 /* Check they're not leading us off end of descriptors. */
775 /* Make sure compiler knows to grab that: we don't want it changing! */
776 /* We will use the result as an index in an array, so most
777 * architectures only need a compiler barrier here. */
778 read_barrier_depends();
783 static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
784 struct iovec iov[], unsigned int iov_size,
785 unsigned int *out_num, unsigned int *in_num,
786 struct vhost_log *log, unsigned int *log_num,
787 struct vring_desc *indirect)
789 struct vring_desc desc;
790 unsigned int i = 0, count, found = 0;
794 if (unlikely(indirect->len % sizeof desc)) {
795 vq_err(vq, "Invalid length in indirect descriptor: "
796 "len 0x%llx not multiple of 0x%zx\n",
797 (unsigned long long)indirect->len,
802 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
803 ARRAY_SIZE(vq->indirect));
804 if (unlikely(ret < 0)) {
805 vq_err(vq, "Translation failure %d in indirect.\n", ret);
809 /* We will use the result as an address to read from, so most
810 * architectures only need a compiler barrier here. */
811 read_barrier_depends();
813 count = indirect->len / sizeof desc;
814 /* Buffers are chained via a 16 bit next field, so
815 * we can have at most 2^16 of these. */
816 if (unlikely(count > USHRT_MAX + 1)) {
817 vq_err(vq, "Indirect buffer length too big: %d\n",
823 unsigned iov_count = *in_num + *out_num;
824 if (unlikely(++found > count)) {
825 vq_err(vq, "Loop detected: last one at %u "
826 "indirect size %u\n",
830 if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
832 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
833 i, (size_t)indirect->addr + i * sizeof desc);
836 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
837 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
838 i, (size_t)indirect->addr + i * sizeof desc);
842 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
843 iov_size - iov_count);
844 if (unlikely(ret < 0)) {
845 vq_err(vq, "Translation failure %d indirect idx %d\n",
849 /* If this is an input descriptor, increment that count. */
850 if (desc.flags & VRING_DESC_F_WRITE) {
853 log[*log_num].addr = desc.addr;
854 log[*log_num].len = desc.len;
858 /* If it's an output descriptor, they're all supposed
859 * to come before any input descriptors. */
860 if (unlikely(*in_num)) {
861 vq_err(vq, "Indirect descriptor "
862 "has out after in: idx %d\n", i);
867 } while ((i = next_desc(&desc)) != -1);
871 /* This looks in the virtqueue and for the first available buffer, and converts
872 * it to an iovec for convenient access. Since descriptors consist of some
873 * number of output then some number of input descriptors, it's actually two
874 * iovecs, but we pack them into one and note how many of each there were.
876 * This function returns the descriptor number found, or vq->num (which is
877 * never a valid descriptor number) if none was found. A negative code is
878 * returned on error. */
879 int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
880 struct iovec iov[], unsigned int iov_size,
881 unsigned int *out_num, unsigned int *in_num,
882 struct vhost_log *log, unsigned int *log_num)
884 struct vring_desc desc;
885 unsigned int i, head, found = 0;
889 /* Check it isn't doing very strange things with descriptor numbers. */
890 last_avail_idx = vq->last_avail_idx;
891 if (unlikely(get_user(vq->avail_idx, &vq->avail->idx))) {
892 vq_err(vq, "Failed to access avail idx at %p\n",
897 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
898 vq_err(vq, "Guest moved used index from %u to %u",
899 last_avail_idx, vq->avail_idx);
903 /* If there's nothing new since last we looked, return invalid. */
904 if (vq->avail_idx == last_avail_idx)
907 /* Only get avail ring entries after they have been exposed by guest. */
910 /* Grab the next descriptor number they're advertising, and increment
911 * the index we've seen. */
912 if (unlikely(get_user(head,
913 &vq->avail->ring[last_avail_idx % vq->num]))) {
914 vq_err(vq, "Failed to read head: idx %d address %p\n",
916 &vq->avail->ring[last_avail_idx % vq->num]);
920 /* If their number is silly, that's an error. */
921 if (unlikely(head >= vq->num)) {
922 vq_err(vq, "Guest says index %u > %u is available",
927 /* When we start there are none of either input nor output. */
928 *out_num = *in_num = 0;
934 unsigned iov_count = *in_num + *out_num;
935 if (unlikely(i >= vq->num)) {
936 vq_err(vq, "Desc index is %u > %u, head = %u",
940 if (unlikely(++found > vq->num)) {
941 vq_err(vq, "Loop detected: last one at %u "
942 "vq size %u head %u\n",
946 ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
948 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
952 if (desc.flags & VRING_DESC_F_INDIRECT) {
953 ret = get_indirect(dev, vq, iov, iov_size,
955 log, log_num, &desc);
956 if (unlikely(ret < 0)) {
957 vq_err(vq, "Failure detected "
958 "in indirect descriptor at idx %d\n", i);
964 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
965 iov_size - iov_count);
966 if (unlikely(ret < 0)) {
967 vq_err(vq, "Translation failure %d descriptor idx %d\n",
971 if (desc.flags & VRING_DESC_F_WRITE) {
972 /* If this is an input descriptor,
973 * increment that count. */
976 log[*log_num].addr = desc.addr;
977 log[*log_num].len = desc.len;
981 /* If it's an output descriptor, they're all supposed
982 * to come before any input descriptors. */
983 if (unlikely(*in_num)) {
984 vq_err(vq, "Descriptor has out after in: "
990 } while ((i = next_desc(&desc)) != -1);
992 /* On success, increment avail index. */
993 vq->last_avail_idx++;
997 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
998 void vhost_discard_vq_desc(struct vhost_virtqueue *vq)
1000 vq->last_avail_idx--;
1003 /* After we've used one of their buffers, we tell them about it. We'll then
1004 * want to notify the guest, using eventfd. */
1005 int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1007 struct vring_used_elem __user *used;
1009 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1010 * next entry in that used ring. */
1011 used = &vq->used->ring[vq->last_used_idx % vq->num];
1012 if (put_user(head, &used->id)) {
1013 vq_err(vq, "Failed to write used id");
1016 if (put_user(len, &used->len)) {
1017 vq_err(vq, "Failed to write used len");
1020 /* Make sure buffer is written before we update index. */
1022 if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
1023 vq_err(vq, "Failed to increment used idx");
1026 if (unlikely(vq->log_used)) {
1027 /* Make sure data is seen before log. */
1029 /* Log used ring entry write. */
1030 log_write(vq->log_base,
1032 ((void __user *)used - (void __user *)vq->used),
1034 /* Log used index update. */
1035 log_write(vq->log_base,
1036 vq->log_addr + offsetof(struct vring_used, idx),
1037 sizeof vq->used->idx);
1039 eventfd_signal(vq->log_ctx, 1);
1041 vq->last_used_idx++;
1045 /* This actually signals the guest, using eventfd. */
1046 void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1049 /* Flush out used index updates. This is paired
1050 * with the barrier that the Guest executes when enabling
1054 if (get_user(flags, &vq->avail->flags)) {
1055 vq_err(vq, "Failed to get flags");
1059 /* If they don't want an interrupt, don't signal, unless empty. */
1060 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1061 (vq->avail_idx != vq->last_avail_idx ||
1062 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1065 /* Signal the Guest tell them we used something up. */
1067 eventfd_signal(vq->call_ctx, 1);
1070 /* And here's the combo meal deal. Supersize me! */
1071 void vhost_add_used_and_signal(struct vhost_dev *dev,
1072 struct vhost_virtqueue *vq,
1073 unsigned int head, int len)
1075 vhost_add_used(vq, head, len);
1076 vhost_signal(dev, vq);
1079 /* OK, now we need to know about added descriptors. */
1080 bool vhost_enable_notify(struct vhost_virtqueue *vq)
1084 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1086 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1087 r = put_user(vq->used_flags, &vq->used->flags);
1089 vq_err(vq, "Failed to enable notification at %p: %d\n",
1090 &vq->used->flags, r);
1093 /* They could have slipped one in as we were doing that: make
1094 * sure it's written, then check again. */
1096 r = get_user(avail_idx, &vq->avail->idx);
1098 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1099 &vq->avail->idx, r);
1103 return avail_idx != vq->last_avail_idx;
1106 /* We don't need to be notified again. */
1107 void vhost_disable_notify(struct vhost_virtqueue *vq)
1110 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1112 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1113 r = put_user(vq->used_flags, &vq->used->flags);
1115 vq_err(vq, "Failed to enable notification at %p: %d\n",
1116 &vq->used->flags, r);
1119 int vhost_init(void)
1121 vhost_workqueue = create_singlethread_workqueue("vhost");
1122 if (!vhost_workqueue)
1127 void vhost_cleanup(void)
1129 destroy_workqueue(vhost_workqueue);