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/virtual/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/uio.h>
18 #include <linux/mmu_context.h>
19 #include <linux/miscdevice.h>
20 #include <linux/mutex.h>
21 #include <linux/poll.h>
22 #include <linux/file.h>
23 #include <linux/highmem.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/kthread.h>
27 #include <linux/cgroup.h>
28 #include <linux/module.h>
29 #include <linux/sort.h>
30 #include <linux/sched/mm.h>
31 #include <linux/sched/signal.h>
32 #include <linux/interval_tree_generic.h>
36 static ushort max_mem_regions = 64;
37 module_param(max_mem_regions, ushort, 0444);
38 MODULE_PARM_DESC(max_mem_regions,
39 "Maximum number of memory regions in memory map. (default: 64)");
40 static int max_iotlb_entries = 2048;
41 module_param(max_iotlb_entries, int, 0444);
42 MODULE_PARM_DESC(max_iotlb_entries,
43 "Maximum number of iotlb entries. (default: 2048)");
46 VHOST_MEMORY_F_LOG = 0x1,
49 #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
50 #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
52 INTERVAL_TREE_DEFINE(struct vhost_umem_node,
53 rb, __u64, __subtree_last,
54 START, LAST, static inline, vhost_umem_interval_tree);
56 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
57 static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
59 vq->user_be = !virtio_legacy_is_little_endian();
62 static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
67 static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
72 static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
74 struct vhost_vring_state s;
79 if (copy_from_user(&s, argp, sizeof(s)))
82 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
83 s.num != VHOST_VRING_BIG_ENDIAN)
86 if (s.num == VHOST_VRING_BIG_ENDIAN)
87 vhost_enable_cross_endian_big(vq);
89 vhost_enable_cross_endian_little(vq);
94 static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
97 struct vhost_vring_state s = {
102 if (copy_to_user(argp, &s, sizeof(s)))
108 static void vhost_init_is_le(struct vhost_virtqueue *vq)
110 /* Note for legacy virtio: user_be is initialized at reset time
111 * according to the host endianness. If userspace does not set an
112 * explicit endianness, the default behavior is native endian, as
113 * expected by legacy virtio.
115 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
118 static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
122 static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
127 static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
133 static void vhost_init_is_le(struct vhost_virtqueue *vq)
135 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
136 || virtio_legacy_is_little_endian();
138 #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
140 static void vhost_reset_is_le(struct vhost_virtqueue *vq)
142 vhost_init_is_le(vq);
145 struct vhost_flush_struct {
146 struct vhost_work work;
147 struct completion wait_event;
150 static void vhost_flush_work(struct vhost_work *work)
152 struct vhost_flush_struct *s;
154 s = container_of(work, struct vhost_flush_struct, work);
155 complete(&s->wait_event);
158 static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
161 struct vhost_poll *poll;
163 poll = container_of(pt, struct vhost_poll, table);
165 add_wait_queue(wqh, &poll->wait);
168 static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
171 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
173 if (!(key_to_poll(key) & poll->mask))
176 vhost_poll_queue(poll);
180 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
182 clear_bit(VHOST_WORK_QUEUED, &work->flags);
185 EXPORT_SYMBOL_GPL(vhost_work_init);
187 /* Init poll structure */
188 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
189 __poll_t mask, struct vhost_dev *dev)
191 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
192 init_poll_funcptr(&poll->table, vhost_poll_func);
197 vhost_work_init(&poll->work, fn);
199 EXPORT_SYMBOL_GPL(vhost_poll_init);
201 /* Start polling a file. We add ourselves to file's wait queue. The caller must
202 * keep a reference to a file until after vhost_poll_stop is called. */
203 int vhost_poll_start(struct vhost_poll *poll, struct file *file)
211 mask = vfs_poll(file, &poll->table);
213 vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
214 if (mask & EPOLLERR) {
215 vhost_poll_stop(poll);
221 EXPORT_SYMBOL_GPL(vhost_poll_start);
223 /* Stop polling a file. After this function returns, it becomes safe to drop the
224 * file reference. You must also flush afterwards. */
225 void vhost_poll_stop(struct vhost_poll *poll)
228 remove_wait_queue(poll->wqh, &poll->wait);
232 EXPORT_SYMBOL_GPL(vhost_poll_stop);
234 void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
236 struct vhost_flush_struct flush;
239 init_completion(&flush.wait_event);
240 vhost_work_init(&flush.work, vhost_flush_work);
242 vhost_work_queue(dev, &flush.work);
243 wait_for_completion(&flush.wait_event);
246 EXPORT_SYMBOL_GPL(vhost_work_flush);
248 /* Flush any work that has been scheduled. When calling this, don't hold any
249 * locks that are also used by the callback. */
250 void vhost_poll_flush(struct vhost_poll *poll)
252 vhost_work_flush(poll->dev, &poll->work);
254 EXPORT_SYMBOL_GPL(vhost_poll_flush);
256 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
261 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
262 /* We can only add the work to the list after we're
263 * sure it was not in the list.
264 * test_and_set_bit() implies a memory barrier.
266 llist_add(&work->node, &dev->work_list);
267 wake_up_process(dev->worker);
270 EXPORT_SYMBOL_GPL(vhost_work_queue);
272 /* A lockless hint for busy polling code to exit the loop */
273 bool vhost_has_work(struct vhost_dev *dev)
275 return !llist_empty(&dev->work_list);
277 EXPORT_SYMBOL_GPL(vhost_has_work);
279 void vhost_poll_queue(struct vhost_poll *poll)
281 vhost_work_queue(poll->dev, &poll->work);
283 EXPORT_SYMBOL_GPL(vhost_poll_queue);
285 static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
289 for (j = 0; j < VHOST_NUM_ADDRS; j++)
290 vq->meta_iotlb[j] = NULL;
293 static void vhost_vq_meta_reset(struct vhost_dev *d)
297 for (i = 0; i < d->nvqs; ++i)
298 __vhost_vq_meta_reset(d->vqs[i]);
301 static void vhost_vq_reset(struct vhost_dev *dev,
302 struct vhost_virtqueue *vq)
308 vq->last_avail_idx = 0;
310 vq->last_used_idx = 0;
311 vq->signalled_used = 0;
312 vq->signalled_used_valid = false;
314 vq->log_used = false;
315 vq->log_addr = -1ull;
316 vq->private_data = NULL;
317 vq->acked_features = 0;
319 vq->error_ctx = NULL;
323 vhost_reset_is_le(vq);
324 vhost_disable_cross_endian(vq);
325 vq->busyloop_timeout = 0;
328 __vhost_vq_meta_reset(vq);
331 static int vhost_worker(void *data)
333 struct vhost_dev *dev = data;
334 struct vhost_work *work, *work_next;
335 struct llist_node *node;
336 mm_segment_t oldfs = get_fs();
342 /* mb paired w/ kthread_stop */
343 set_current_state(TASK_INTERRUPTIBLE);
345 if (kthread_should_stop()) {
346 __set_current_state(TASK_RUNNING);
350 node = llist_del_all(&dev->work_list);
354 node = llist_reverse_order(node);
355 /* make sure flag is seen after deletion */
357 llist_for_each_entry_safe(work, work_next, node, node) {
358 clear_bit(VHOST_WORK_QUEUED, &work->flags);
359 __set_current_state(TASK_RUNNING);
370 static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
380 /* Helper to allocate iovec buffers for all vqs. */
381 static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
383 struct vhost_virtqueue *vq;
386 for (i = 0; i < dev->nvqs; ++i) {
388 vq->indirect = kmalloc_array(UIO_MAXIOV,
389 sizeof(*vq->indirect),
391 vq->log = kmalloc_array(UIO_MAXIOV, sizeof(*vq->log),
393 vq->heads = kmalloc_array(UIO_MAXIOV, sizeof(*vq->heads),
395 if (!vq->indirect || !vq->log || !vq->heads)
402 vhost_vq_free_iovecs(dev->vqs[i]);
406 static void vhost_dev_free_iovecs(struct vhost_dev *dev)
410 for (i = 0; i < dev->nvqs; ++i)
411 vhost_vq_free_iovecs(dev->vqs[i]);
414 void vhost_dev_init(struct vhost_dev *dev,
415 struct vhost_virtqueue **vqs, int nvqs)
417 struct vhost_virtqueue *vq;
422 mutex_init(&dev->mutex);
428 init_llist_head(&dev->work_list);
429 init_waitqueue_head(&dev->wait);
430 INIT_LIST_HEAD(&dev->read_list);
431 INIT_LIST_HEAD(&dev->pending_list);
432 spin_lock_init(&dev->iotlb_lock);
435 for (i = 0; i < dev->nvqs; ++i) {
441 mutex_init(&vq->mutex);
442 vhost_vq_reset(dev, vq);
444 vhost_poll_init(&vq->poll, vq->handle_kick,
448 EXPORT_SYMBOL_GPL(vhost_dev_init);
450 /* Caller should have device mutex */
451 long vhost_dev_check_owner(struct vhost_dev *dev)
453 /* Are you the owner? If not, I don't think you mean to do that */
454 return dev->mm == current->mm ? 0 : -EPERM;
456 EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
458 struct vhost_attach_cgroups_struct {
459 struct vhost_work work;
460 struct task_struct *owner;
464 static void vhost_attach_cgroups_work(struct vhost_work *work)
466 struct vhost_attach_cgroups_struct *s;
468 s = container_of(work, struct vhost_attach_cgroups_struct, work);
469 s->ret = cgroup_attach_task_all(s->owner, current);
472 static int vhost_attach_cgroups(struct vhost_dev *dev)
474 struct vhost_attach_cgroups_struct attach;
476 attach.owner = current;
477 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
478 vhost_work_queue(dev, &attach.work);
479 vhost_work_flush(dev, &attach.work);
483 /* Caller should have device mutex */
484 bool vhost_dev_has_owner(struct vhost_dev *dev)
488 EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
490 /* Caller should have device mutex */
491 long vhost_dev_set_owner(struct vhost_dev *dev)
493 struct task_struct *worker;
496 /* Is there an owner already? */
497 if (vhost_dev_has_owner(dev)) {
502 /* No owner, become one */
503 dev->mm = get_task_mm(current);
504 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
505 if (IS_ERR(worker)) {
506 err = PTR_ERR(worker);
510 dev->worker = worker;
511 wake_up_process(worker); /* avoid contributing to loadavg */
513 err = vhost_attach_cgroups(dev);
517 err = vhost_dev_alloc_iovecs(dev);
523 kthread_stop(worker);
532 EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
534 struct vhost_umem *vhost_dev_reset_owner_prepare(void)
536 return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
538 EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
540 /* Caller should have device mutex */
541 void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
545 vhost_dev_cleanup(dev);
547 /* Restore memory to default empty mapping. */
548 INIT_LIST_HEAD(&umem->umem_list);
550 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
551 * VQs aren't running.
553 for (i = 0; i < dev->nvqs; ++i)
554 dev->vqs[i]->umem = umem;
556 EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
558 void vhost_dev_stop(struct vhost_dev *dev)
562 for (i = 0; i < dev->nvqs; ++i) {
563 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
564 vhost_poll_stop(&dev->vqs[i]->poll);
565 vhost_poll_flush(&dev->vqs[i]->poll);
569 EXPORT_SYMBOL_GPL(vhost_dev_stop);
571 static void vhost_umem_free(struct vhost_umem *umem,
572 struct vhost_umem_node *node)
574 vhost_umem_interval_tree_remove(node, &umem->umem_tree);
575 list_del(&node->link);
580 static void vhost_umem_clean(struct vhost_umem *umem)
582 struct vhost_umem_node *node, *tmp;
587 list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
588 vhost_umem_free(umem, node);
593 static void vhost_clear_msg(struct vhost_dev *dev)
595 struct vhost_msg_node *node, *n;
597 spin_lock(&dev->iotlb_lock);
599 list_for_each_entry_safe(node, n, &dev->read_list, node) {
600 list_del(&node->node);
604 list_for_each_entry_safe(node, n, &dev->pending_list, node) {
605 list_del(&node->node);
609 spin_unlock(&dev->iotlb_lock);
612 void vhost_dev_cleanup(struct vhost_dev *dev)
616 for (i = 0; i < dev->nvqs; ++i) {
617 if (dev->vqs[i]->error_ctx)
618 eventfd_ctx_put(dev->vqs[i]->error_ctx);
619 if (dev->vqs[i]->kick)
620 fput(dev->vqs[i]->kick);
621 if (dev->vqs[i]->call_ctx)
622 eventfd_ctx_put(dev->vqs[i]->call_ctx);
623 vhost_vq_reset(dev, dev->vqs[i]);
625 vhost_dev_free_iovecs(dev);
627 eventfd_ctx_put(dev->log_ctx);
629 /* No one will access memory at this point */
630 vhost_umem_clean(dev->umem);
632 vhost_umem_clean(dev->iotlb);
634 vhost_clear_msg(dev);
635 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
636 WARN_ON(!llist_empty(&dev->work_list));
638 kthread_stop(dev->worker);
645 EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
647 static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
649 u64 a = addr / VHOST_PAGE_SIZE / 8;
651 /* Make sure 64 bit math will not overflow. */
652 if (a > ULONG_MAX - (unsigned long)log_base ||
653 a + (unsigned long)log_base > ULONG_MAX)
656 return access_ok(VERIFY_WRITE, log_base + a,
657 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
660 static bool vhost_overflow(u64 uaddr, u64 size)
662 /* Make sure 64 bit math will not overflow. */
663 return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
666 /* Caller should have vq mutex and device mutex. */
667 static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
670 struct vhost_umem_node *node;
675 list_for_each_entry(node, &umem->umem_list, link) {
676 unsigned long a = node->userspace_addr;
678 if (vhost_overflow(node->userspace_addr, node->size))
682 if (!access_ok(VERIFY_WRITE, (void __user *)a,
685 else if (log_all && !log_access_ok(log_base,
693 static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
694 u64 addr, unsigned int size,
697 const struct vhost_umem_node *node = vq->meta_iotlb[type];
702 return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
705 /* Can we switch to this memory table? */
706 /* Caller should have device mutex but not vq mutex */
707 static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
712 for (i = 0; i < d->nvqs; ++i) {
716 mutex_lock(&d->vqs[i]->mutex);
717 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
718 /* If ring is inactive, will check when it's enabled. */
719 if (d->vqs[i]->private_data)
720 ok = vq_memory_access_ok(d->vqs[i]->log_base,
724 mutex_unlock(&d->vqs[i]->mutex);
731 static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
732 struct iovec iov[], int iov_size, int access);
734 static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
735 const void *from, unsigned size)
740 return __copy_to_user(to, from, size);
742 /* This function should be called after iotlb
743 * prefetch, which means we're sure that all vq
744 * could be access through iotlb. So -EAGAIN should
745 * not happen in this case.
748 void __user *uaddr = vhost_vq_meta_fetch(vq,
749 (u64)(uintptr_t)to, size,
753 return __copy_to_user(uaddr, from, size);
755 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
756 ARRAY_SIZE(vq->iotlb_iov),
760 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
761 ret = copy_to_iter(from, size, &t);
769 static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
770 void __user *from, unsigned size)
775 return __copy_from_user(to, from, size);
777 /* This function should be called after iotlb
778 * prefetch, which means we're sure that vq
779 * could be access through iotlb. So -EAGAIN should
780 * not happen in this case.
782 void __user *uaddr = vhost_vq_meta_fetch(vq,
783 (u64)(uintptr_t)from, size,
788 return __copy_from_user(to, uaddr, size);
790 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
791 ARRAY_SIZE(vq->iotlb_iov),
794 vq_err(vq, "IOTLB translation failure: uaddr "
795 "%p size 0x%llx\n", from,
796 (unsigned long long) size);
799 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
800 ret = copy_from_iter(to, size, &f);
809 static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
810 void __user *addr, unsigned int size,
815 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
816 ARRAY_SIZE(vq->iotlb_iov),
819 vq_err(vq, "IOTLB translation failure: uaddr "
820 "%p size 0x%llx\n", addr,
821 (unsigned long long) size);
825 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
826 vq_err(vq, "Non atomic userspace memory access: uaddr "
827 "%p size 0x%llx\n", addr,
828 (unsigned long long) size);
832 return vq->iotlb_iov[0].iov_base;
835 /* This function should be called after iotlb
836 * prefetch, which means we're sure that vq
837 * could be access through iotlb. So -EAGAIN should
838 * not happen in this case.
840 static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
841 void *addr, unsigned int size,
844 void __user *uaddr = vhost_vq_meta_fetch(vq,
845 (u64)(uintptr_t)addr, size, type);
849 return __vhost_get_user_slow(vq, addr, size, type);
852 #define vhost_put_user(vq, x, ptr) \
856 ret = __put_user(x, ptr); \
858 __typeof__(ptr) to = \
859 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
860 sizeof(*ptr), VHOST_ADDR_USED); \
862 ret = __put_user(x, to); \
869 #define vhost_get_user(vq, x, ptr, type) \
873 ret = __get_user(x, ptr); \
875 __typeof__(ptr) from = \
876 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
880 ret = __get_user(x, from); \
887 #define vhost_get_avail(vq, x, ptr) \
888 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
890 #define vhost_get_used(vq, x, ptr) \
891 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
893 static void vhost_dev_lock_vqs(struct vhost_dev *d)
896 for (i = 0; i < d->nvqs; ++i)
897 mutex_lock_nested(&d->vqs[i]->mutex, i);
900 static void vhost_dev_unlock_vqs(struct vhost_dev *d)
903 for (i = 0; i < d->nvqs; ++i)
904 mutex_unlock(&d->vqs[i]->mutex);
907 static int vhost_new_umem_range(struct vhost_umem *umem,
908 u64 start, u64 size, u64 end,
909 u64 userspace_addr, int perm)
911 struct vhost_umem_node *tmp, *node = kmalloc(sizeof(*node), GFP_ATOMIC);
916 if (umem->numem == max_iotlb_entries) {
917 tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
918 vhost_umem_free(umem, tmp);
924 node->userspace_addr = userspace_addr;
926 INIT_LIST_HEAD(&node->link);
927 list_add_tail(&node->link, &umem->umem_list);
928 vhost_umem_interval_tree_insert(node, &umem->umem_tree);
934 static void vhost_del_umem_range(struct vhost_umem *umem,
937 struct vhost_umem_node *node;
939 while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
941 vhost_umem_free(umem, node);
944 static void vhost_iotlb_notify_vq(struct vhost_dev *d,
945 struct vhost_iotlb_msg *msg)
947 struct vhost_msg_node *node, *n;
949 spin_lock(&d->iotlb_lock);
951 list_for_each_entry_safe(node, n, &d->pending_list, node) {
952 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
953 if (msg->iova <= vq_msg->iova &&
954 msg->iova + msg->size - 1 > vq_msg->iova &&
955 vq_msg->type == VHOST_IOTLB_MISS) {
956 vhost_poll_queue(&node->vq->poll);
957 list_del(&node->node);
962 spin_unlock(&d->iotlb_lock);
965 static bool umem_access_ok(u64 uaddr, u64 size, int access)
967 unsigned long a = uaddr;
969 /* Make sure 64 bit math will not overflow. */
970 if (vhost_overflow(uaddr, size))
973 if ((access & VHOST_ACCESS_RO) &&
974 !access_ok(VERIFY_READ, (void __user *)a, size))
976 if ((access & VHOST_ACCESS_WO) &&
977 !access_ok(VERIFY_WRITE, (void __user *)a, size))
982 static int vhost_process_iotlb_msg(struct vhost_dev *dev,
983 struct vhost_iotlb_msg *msg)
987 mutex_lock(&dev->mutex);
988 vhost_dev_lock_vqs(dev);
990 case VHOST_IOTLB_UPDATE:
995 if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
999 vhost_vq_meta_reset(dev);
1000 if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
1001 msg->iova + msg->size - 1,
1002 msg->uaddr, msg->perm)) {
1006 vhost_iotlb_notify_vq(dev, msg);
1008 case VHOST_IOTLB_INVALIDATE:
1013 vhost_vq_meta_reset(dev);
1014 vhost_del_umem_range(dev->iotlb, msg->iova,
1015 msg->iova + msg->size - 1);
1022 vhost_dev_unlock_vqs(dev);
1023 mutex_unlock(&dev->mutex);
1027 ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
1028 struct iov_iter *from)
1030 struct vhost_msg_node node;
1031 unsigned size = sizeof(struct vhost_msg);
1035 if (iov_iter_count(from) < size)
1037 ret = copy_from_iter(&node.msg, size, from);
1041 switch (node.msg.type) {
1042 case VHOST_IOTLB_MSG:
1043 err = vhost_process_iotlb_msg(dev, &node.msg.iotlb);
1055 EXPORT_SYMBOL(vhost_chr_write_iter);
1057 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
1062 poll_wait(file, &dev->wait, wait);
1064 if (!list_empty(&dev->read_list))
1065 mask |= EPOLLIN | EPOLLRDNORM;
1069 EXPORT_SYMBOL(vhost_chr_poll);
1071 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1075 struct vhost_msg_node *node;
1077 unsigned size = sizeof(struct vhost_msg);
1079 if (iov_iter_count(to) < size)
1084 prepare_to_wait(&dev->wait, &wait,
1085 TASK_INTERRUPTIBLE);
1087 node = vhost_dequeue_msg(dev, &dev->read_list);
1094 if (signal_pending(current)) {
1107 finish_wait(&dev->wait, &wait);
1110 ret = copy_to_iter(&node->msg, size, to);
1112 if (ret != size || node->msg.type != VHOST_IOTLB_MISS) {
1117 vhost_enqueue_msg(dev, &dev->pending_list, node);
1122 EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1124 static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1126 struct vhost_dev *dev = vq->dev;
1127 struct vhost_msg_node *node;
1128 struct vhost_iotlb_msg *msg;
1130 node = vhost_new_msg(vq, VHOST_IOTLB_MISS);
1134 msg = &node->msg.iotlb;
1135 msg->type = VHOST_IOTLB_MISS;
1139 vhost_enqueue_msg(dev, &dev->read_list, node);
1144 static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
1145 struct vring_desc __user *desc,
1146 struct vring_avail __user *avail,
1147 struct vring_used __user *used)
1150 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1152 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
1153 access_ok(VERIFY_READ, avail,
1154 sizeof *avail + num * sizeof *avail->ring + s) &&
1155 access_ok(VERIFY_WRITE, used,
1156 sizeof *used + num * sizeof *used->ring + s);
1159 static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
1160 const struct vhost_umem_node *node,
1163 int access = (type == VHOST_ADDR_USED) ?
1164 VHOST_ACCESS_WO : VHOST_ACCESS_RO;
1166 if (likely(node->perm & access))
1167 vq->meta_iotlb[type] = node;
1170 static bool iotlb_access_ok(struct vhost_virtqueue *vq,
1171 int access, u64 addr, u64 len, int type)
1173 const struct vhost_umem_node *node;
1174 struct vhost_umem *umem = vq->iotlb;
1175 u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
1177 if (vhost_vq_meta_fetch(vq, addr, len, type))
1181 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1184 if (node == NULL || node->start > addr) {
1185 vhost_iotlb_miss(vq, addr, access);
1187 } else if (!(node->perm & access)) {
1188 /* Report the possible access violation by
1189 * request another translation from userspace.
1194 size = node->size - addr + node->start;
1196 if (orig_addr == addr && size >= len)
1197 vhost_vq_meta_update(vq, node, type);
1206 int vq_iotlb_prefetch(struct vhost_virtqueue *vq)
1208 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1209 unsigned int num = vq->num;
1214 return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
1215 num * sizeof(*vq->desc), VHOST_ADDR_DESC) &&
1216 iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
1218 num * sizeof(*vq->avail->ring) + s,
1219 VHOST_ADDR_AVAIL) &&
1220 iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
1222 num * sizeof(*vq->used->ring) + s,
1225 EXPORT_SYMBOL_GPL(vq_iotlb_prefetch);
1227 /* Can we log writes? */
1228 /* Caller should have device mutex but not vq mutex */
1229 bool vhost_log_access_ok(struct vhost_dev *dev)
1231 return memory_access_ok(dev, dev->umem, 1);
1233 EXPORT_SYMBOL_GPL(vhost_log_access_ok);
1235 /* Verify access for write logging. */
1236 /* Caller should have vq mutex and device mutex */
1237 static bool vq_log_access_ok(struct vhost_virtqueue *vq,
1238 void __user *log_base)
1240 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1242 return vq_memory_access_ok(log_base, vq->umem,
1243 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
1244 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
1246 vq->num * sizeof *vq->used->ring + s));
1249 /* Can we start vq? */
1250 /* Caller should have vq mutex and device mutex */
1251 bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
1253 if (!vq_log_access_ok(vq, vq->log_base))
1256 /* Access validation occurs at prefetch time with IOTLB */
1260 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
1262 EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
1264 static struct vhost_umem *vhost_umem_alloc(void)
1266 struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
1271 umem->umem_tree = RB_ROOT_CACHED;
1273 INIT_LIST_HEAD(&umem->umem_list);
1278 static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1280 struct vhost_memory mem, *newmem;
1281 struct vhost_memory_region *region;
1282 struct vhost_umem *newumem, *oldumem;
1283 unsigned long size = offsetof(struct vhost_memory, regions);
1286 if (copy_from_user(&mem, m, size))
1290 if (mem.nregions > max_mem_regions)
1292 newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
1297 memcpy(newmem, &mem, size);
1298 if (copy_from_user(newmem->regions, m->regions,
1299 mem.nregions * sizeof *m->regions)) {
1304 newumem = vhost_umem_alloc();
1310 for (region = newmem->regions;
1311 region < newmem->regions + mem.nregions;
1313 if (vhost_new_umem_range(newumem,
1314 region->guest_phys_addr,
1315 region->memory_size,
1316 region->guest_phys_addr +
1317 region->memory_size - 1,
1318 region->userspace_addr,
1323 if (!memory_access_ok(d, newumem, 0))
1329 /* All memory accesses are done under some VQ mutex. */
1330 for (i = 0; i < d->nvqs; ++i) {
1331 mutex_lock(&d->vqs[i]->mutex);
1332 d->vqs[i]->umem = newumem;
1333 mutex_unlock(&d->vqs[i]->mutex);
1337 vhost_umem_clean(oldumem);
1341 vhost_umem_clean(newumem);
1346 long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
1348 struct file *eventfp, *filep = NULL;
1349 bool pollstart = false, pollstop = false;
1350 struct eventfd_ctx *ctx = NULL;
1351 u32 __user *idxp = argp;
1352 struct vhost_virtqueue *vq;
1353 struct vhost_vring_state s;
1354 struct vhost_vring_file f;
1355 struct vhost_vring_addr a;
1359 r = get_user(idx, idxp);
1367 mutex_lock(&vq->mutex);
1370 case VHOST_SET_VRING_NUM:
1371 /* Resizing ring with an active backend?
1372 * You don't want to do that. */
1373 if (vq->private_data) {
1377 if (copy_from_user(&s, argp, sizeof s)) {
1381 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
1387 case VHOST_SET_VRING_BASE:
1388 /* Moving base with an active backend?
1389 * You don't want to do that. */
1390 if (vq->private_data) {
1394 if (copy_from_user(&s, argp, sizeof s)) {
1398 if (s.num > 0xffff) {
1402 vq->last_avail_idx = s.num;
1403 /* Forget the cached index value. */
1404 vq->avail_idx = vq->last_avail_idx;
1406 case VHOST_GET_VRING_BASE:
1408 s.num = vq->last_avail_idx;
1409 if (copy_to_user(argp, &s, sizeof s))
1412 case VHOST_SET_VRING_ADDR:
1413 if (copy_from_user(&a, argp, sizeof a)) {
1417 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
1421 /* For 32bit, verify that the top 32bits of the user
1422 data are set to zero. */
1423 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
1424 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
1425 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
1430 /* Make sure it's safe to cast pointers to vring types. */
1431 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
1432 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
1433 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
1434 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
1435 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
1440 /* We only verify access here if backend is configured.
1441 * If it is not, we don't as size might not have been setup.
1442 * We will verify when backend is configured. */
1443 if (vq->private_data) {
1444 if (!vq_access_ok(vq, vq->num,
1445 (void __user *)(unsigned long)a.desc_user_addr,
1446 (void __user *)(unsigned long)a.avail_user_addr,
1447 (void __user *)(unsigned long)a.used_user_addr)) {
1452 /* Also validate log access for used ring if enabled. */
1453 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
1454 !log_access_ok(vq->log_base, a.log_guest_addr,
1456 vq->num * sizeof *vq->used->ring)) {
1462 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
1463 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
1464 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
1465 vq->log_addr = a.log_guest_addr;
1466 vq->used = (void __user *)(unsigned long)a.used_user_addr;
1468 case VHOST_SET_VRING_KICK:
1469 if (copy_from_user(&f, argp, sizeof f)) {
1473 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
1474 if (IS_ERR(eventfp)) {
1475 r = PTR_ERR(eventfp);
1478 if (eventfp != vq->kick) {
1479 pollstop = (filep = vq->kick) != NULL;
1480 pollstart = (vq->kick = eventfp) != NULL;
1484 case VHOST_SET_VRING_CALL:
1485 if (copy_from_user(&f, argp, sizeof f)) {
1489 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1494 swap(ctx, vq->call_ctx);
1496 case VHOST_SET_VRING_ERR:
1497 if (copy_from_user(&f, argp, sizeof f)) {
1501 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1506 swap(ctx, vq->error_ctx);
1508 case VHOST_SET_VRING_ENDIAN:
1509 r = vhost_set_vring_endian(vq, argp);
1511 case VHOST_GET_VRING_ENDIAN:
1512 r = vhost_get_vring_endian(vq, idx, argp);
1514 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
1515 if (copy_from_user(&s, argp, sizeof(s))) {
1519 vq->busyloop_timeout = s.num;
1521 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
1523 s.num = vq->busyloop_timeout;
1524 if (copy_to_user(argp, &s, sizeof(s)))
1531 if (pollstop && vq->handle_kick)
1532 vhost_poll_stop(&vq->poll);
1534 if (!IS_ERR_OR_NULL(ctx))
1535 eventfd_ctx_put(ctx);
1539 if (pollstart && vq->handle_kick)
1540 r = vhost_poll_start(&vq->poll, vq->kick);
1542 mutex_unlock(&vq->mutex);
1544 if (pollstop && vq->handle_kick)
1545 vhost_poll_flush(&vq->poll);
1548 EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
1550 int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
1552 struct vhost_umem *niotlb, *oiotlb;
1555 niotlb = vhost_umem_alloc();
1562 for (i = 0; i < d->nvqs; ++i) {
1563 mutex_lock(&d->vqs[i]->mutex);
1564 d->vqs[i]->iotlb = niotlb;
1565 mutex_unlock(&d->vqs[i]->mutex);
1568 vhost_umem_clean(oiotlb);
1572 EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
1574 /* Caller must have device mutex */
1575 long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
1577 struct eventfd_ctx *ctx;
1582 /* If you are not the owner, you can become one */
1583 if (ioctl == VHOST_SET_OWNER) {
1584 r = vhost_dev_set_owner(d);
1588 /* You must be the owner to do anything else */
1589 r = vhost_dev_check_owner(d);
1594 case VHOST_SET_MEM_TABLE:
1595 r = vhost_set_memory(d, argp);
1597 case VHOST_SET_LOG_BASE:
1598 if (copy_from_user(&p, argp, sizeof p)) {
1602 if ((u64)(unsigned long)p != p) {
1606 for (i = 0; i < d->nvqs; ++i) {
1607 struct vhost_virtqueue *vq;
1608 void __user *base = (void __user *)(unsigned long)p;
1610 mutex_lock(&vq->mutex);
1611 /* If ring is inactive, will check when it's enabled. */
1612 if (vq->private_data && !vq_log_access_ok(vq, base))
1615 vq->log_base = base;
1616 mutex_unlock(&vq->mutex);
1619 case VHOST_SET_LOG_FD:
1620 r = get_user(fd, (int __user *)argp);
1623 ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
1628 swap(ctx, d->log_ctx);
1629 for (i = 0; i < d->nvqs; ++i) {
1630 mutex_lock(&d->vqs[i]->mutex);
1631 d->vqs[i]->log_ctx = d->log_ctx;
1632 mutex_unlock(&d->vqs[i]->mutex);
1635 eventfd_ctx_put(ctx);
1644 EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
1646 /* TODO: This is really inefficient. We need something like get_user()
1647 * (instruction directly accesses the data, with an exception table entry
1648 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1650 static int set_bit_to_user(int nr, void __user *addr)
1652 unsigned long log = (unsigned long)addr;
1655 int bit = nr + (log % PAGE_SIZE) * 8;
1658 r = get_user_pages_fast(log, 1, 1, &page);
1662 base = kmap_atomic(page);
1664 kunmap_atomic(base);
1665 set_page_dirty_lock(page);
1670 static int log_write(void __user *log_base,
1671 u64 write_address, u64 write_length)
1673 u64 write_page = write_address / VHOST_PAGE_SIZE;
1678 write_length += write_address % VHOST_PAGE_SIZE;
1680 u64 base = (u64)(unsigned long)log_base;
1681 u64 log = base + write_page / 8;
1682 int bit = write_page % 8;
1683 if ((u64)(unsigned long)log != log)
1685 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1688 if (write_length <= VHOST_PAGE_SIZE)
1690 write_length -= VHOST_PAGE_SIZE;
1696 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1697 unsigned int log_num, u64 len)
1701 /* Make sure data written is seen before log. */
1703 for (i = 0; i < log_num; ++i) {
1704 u64 l = min(log[i].len, len);
1705 r = log_write(vq->log_base, log[i].addr, l);
1711 eventfd_signal(vq->log_ctx, 1);
1715 /* Length written exceeds what we have stored. This is a bug. */
1719 EXPORT_SYMBOL_GPL(vhost_log_write);
1721 static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1724 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
1725 &vq->used->flags) < 0)
1727 if (unlikely(vq->log_used)) {
1728 /* Make sure the flag is seen before log. */
1730 /* Log used flag write. */
1731 used = &vq->used->flags;
1732 log_write(vq->log_base, vq->log_addr +
1733 (used - (void __user *)vq->used),
1734 sizeof vq->used->flags);
1736 eventfd_signal(vq->log_ctx, 1);
1741 static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1743 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
1744 vhost_avail_event(vq)))
1746 if (unlikely(vq->log_used)) {
1748 /* Make sure the event is seen before log. */
1750 /* Log avail event write */
1751 used = vhost_avail_event(vq);
1752 log_write(vq->log_base, vq->log_addr +
1753 (used - (void __user *)vq->used),
1754 sizeof *vhost_avail_event(vq));
1756 eventfd_signal(vq->log_ctx, 1);
1761 int vhost_vq_init_access(struct vhost_virtqueue *vq)
1763 __virtio16 last_used_idx;
1765 bool is_le = vq->is_le;
1767 if (!vq->private_data)
1770 vhost_init_is_le(vq);
1772 r = vhost_update_used_flags(vq);
1775 vq->signalled_used_valid = false;
1777 !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
1781 r = vhost_get_used(vq, last_used_idx, &vq->used->idx);
1783 vq_err(vq, "Can't access used idx at %p\n",
1787 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
1794 EXPORT_SYMBOL_GPL(vhost_vq_init_access);
1796 static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
1797 struct iovec iov[], int iov_size, int access)
1799 const struct vhost_umem_node *node;
1800 struct vhost_dev *dev = vq->dev;
1801 struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
1806 while ((u64)len > s) {
1808 if (unlikely(ret >= iov_size)) {
1813 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1814 addr, addr + len - 1);
1815 if (node == NULL || node->start > addr) {
1816 if (umem != dev->iotlb) {
1822 } else if (!(node->perm & access)) {
1828 size = node->size - addr + node->start;
1829 _iov->iov_len = min((u64)len - s, size);
1830 _iov->iov_base = (void __user *)(unsigned long)
1831 (node->userspace_addr + addr - node->start);
1838 vhost_iotlb_miss(vq, addr, access);
1842 /* Each buffer in the virtqueues is actually a chain of descriptors. This
1843 * function returns the next descriptor in the chain,
1844 * or -1U if we're at the end. */
1845 static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
1849 /* If this descriptor says it doesn't chain, we're done. */
1850 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
1853 /* Check they're not leading us off end of descriptors. */
1854 next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
1858 static int get_indirect(struct vhost_virtqueue *vq,
1859 struct iovec iov[], unsigned int iov_size,
1860 unsigned int *out_num, unsigned int *in_num,
1861 struct vhost_log *log, unsigned int *log_num,
1862 struct vring_desc *indirect)
1864 struct vring_desc desc;
1865 unsigned int i = 0, count, found = 0;
1866 u32 len = vhost32_to_cpu(vq, indirect->len);
1867 struct iov_iter from;
1871 if (unlikely(len % sizeof desc)) {
1872 vq_err(vq, "Invalid length in indirect descriptor: "
1873 "len 0x%llx not multiple of 0x%zx\n",
1874 (unsigned long long)len,
1879 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
1880 UIO_MAXIOV, VHOST_ACCESS_RO);
1881 if (unlikely(ret < 0)) {
1883 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1886 iov_iter_init(&from, READ, vq->indirect, ret, len);
1888 /* We will use the result as an address to read from, so most
1889 * architectures only need a compiler barrier here. */
1890 read_barrier_depends();
1892 count = len / sizeof desc;
1893 /* Buffers are chained via a 16 bit next field, so
1894 * we can have at most 2^16 of these. */
1895 if (unlikely(count > USHRT_MAX + 1)) {
1896 vq_err(vq, "Indirect buffer length too big: %d\n",
1902 unsigned iov_count = *in_num + *out_num;
1903 if (unlikely(++found > count)) {
1904 vq_err(vq, "Loop detected: last one at %u "
1905 "indirect size %u\n",
1909 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
1910 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1911 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
1914 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
1915 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1916 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
1920 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
1921 access = VHOST_ACCESS_WO;
1923 access = VHOST_ACCESS_RO;
1925 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1926 vhost32_to_cpu(vq, desc.len), iov + iov_count,
1927 iov_size - iov_count, access);
1928 if (unlikely(ret < 0)) {
1930 vq_err(vq, "Translation failure %d indirect idx %d\n",
1934 /* If this is an input descriptor, increment that count. */
1935 if (access == VHOST_ACCESS_WO) {
1937 if (unlikely(log)) {
1938 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1939 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
1943 /* If it's an output descriptor, they're all supposed
1944 * to come before any input descriptors. */
1945 if (unlikely(*in_num)) {
1946 vq_err(vq, "Indirect descriptor "
1947 "has out after in: idx %d\n", i);
1952 } while ((i = next_desc(vq, &desc)) != -1);
1956 /* This looks in the virtqueue and for the first available buffer, and converts
1957 * it to an iovec for convenient access. Since descriptors consist of some
1958 * number of output then some number of input descriptors, it's actually two
1959 * iovecs, but we pack them into one and note how many of each there were.
1961 * This function returns the descriptor number found, or vq->num (which is
1962 * never a valid descriptor number) if none was found. A negative code is
1963 * returned on error. */
1964 int vhost_get_vq_desc(struct vhost_virtqueue *vq,
1965 struct iovec iov[], unsigned int iov_size,
1966 unsigned int *out_num, unsigned int *in_num,
1967 struct vhost_log *log, unsigned int *log_num)
1969 struct vring_desc desc;
1970 unsigned int i, head, found = 0;
1972 __virtio16 avail_idx;
1973 __virtio16 ring_head;
1976 /* Check it isn't doing very strange things with descriptor numbers. */
1977 last_avail_idx = vq->last_avail_idx;
1979 if (vq->avail_idx == vq->last_avail_idx) {
1980 if (unlikely(vhost_get_avail(vq, avail_idx, &vq->avail->idx))) {
1981 vq_err(vq, "Failed to access avail idx at %p\n",
1985 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
1987 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
1988 vq_err(vq, "Guest moved used index from %u to %u",
1989 last_avail_idx, vq->avail_idx);
1993 /* If there's nothing new since last we looked, return
1996 if (vq->avail_idx == last_avail_idx)
1999 /* Only get avail ring entries after they have been
2005 /* Grab the next descriptor number they're advertising, and increment
2006 * the index we've seen. */
2007 if (unlikely(vhost_get_avail(vq, ring_head,
2008 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
2009 vq_err(vq, "Failed to read head: idx %d address %p\n",
2011 &vq->avail->ring[last_avail_idx % vq->num]);
2015 head = vhost16_to_cpu(vq, ring_head);
2017 /* If their number is silly, that's an error. */
2018 if (unlikely(head >= vq->num)) {
2019 vq_err(vq, "Guest says index %u > %u is available",
2024 /* When we start there are none of either input nor output. */
2025 *out_num = *in_num = 0;
2031 unsigned iov_count = *in_num + *out_num;
2032 if (unlikely(i >= vq->num)) {
2033 vq_err(vq, "Desc index is %u > %u, head = %u",
2037 if (unlikely(++found > vq->num)) {
2038 vq_err(vq, "Loop detected: last one at %u "
2039 "vq size %u head %u\n",
2043 ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
2045 if (unlikely(ret)) {
2046 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2050 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
2051 ret = get_indirect(vq, iov, iov_size,
2053 log, log_num, &desc);
2054 if (unlikely(ret < 0)) {
2056 vq_err(vq, "Failure detected "
2057 "in indirect descriptor at idx %d\n", i);
2063 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2064 access = VHOST_ACCESS_WO;
2066 access = VHOST_ACCESS_RO;
2067 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2068 vhost32_to_cpu(vq, desc.len), iov + iov_count,
2069 iov_size - iov_count, access);
2070 if (unlikely(ret < 0)) {
2072 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2076 if (access == VHOST_ACCESS_WO) {
2077 /* If this is an input descriptor,
2078 * increment that count. */
2080 if (unlikely(log)) {
2081 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2082 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
2086 /* If it's an output descriptor, they're all supposed
2087 * to come before any input descriptors. */
2088 if (unlikely(*in_num)) {
2089 vq_err(vq, "Descriptor has out after in: "
2095 } while ((i = next_desc(vq, &desc)) != -1);
2097 /* On success, increment avail index. */
2098 vq->last_avail_idx++;
2100 /* Assume notifications from guest are disabled at this point,
2101 * if they aren't we would need to update avail_event index. */
2102 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
2105 EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
2107 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
2108 void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
2110 vq->last_avail_idx -= n;
2112 EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
2114 /* After we've used one of their buffers, we tell them about it. We'll then
2115 * want to notify the guest, using eventfd. */
2116 int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2118 struct vring_used_elem heads = {
2119 cpu_to_vhost32(vq, head),
2120 cpu_to_vhost32(vq, len)
2123 return vhost_add_used_n(vq, &heads, 1);
2125 EXPORT_SYMBOL_GPL(vhost_add_used);
2127 static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2128 struct vring_used_elem *heads,
2131 struct vring_used_elem __user *used;
2135 start = vq->last_used_idx & (vq->num - 1);
2136 used = vq->used->ring + start;
2138 if (vhost_put_user(vq, heads[0].id, &used->id)) {
2139 vq_err(vq, "Failed to write used id");
2142 if (vhost_put_user(vq, heads[0].len, &used->len)) {
2143 vq_err(vq, "Failed to write used len");
2146 } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
2147 vq_err(vq, "Failed to write used");
2150 if (unlikely(vq->log_used)) {
2151 /* Make sure data is seen before log. */
2153 /* Log used ring entry write. */
2154 log_write(vq->log_base,
2156 ((void __user *)used - (void __user *)vq->used),
2157 count * sizeof *used);
2159 old = vq->last_used_idx;
2160 new = (vq->last_used_idx += count);
2161 /* If the driver never bothers to signal in a very long while,
2162 * used index might wrap around. If that happens, invalidate
2163 * signalled_used index we stored. TODO: make sure driver
2164 * signals at least once in 2^16 and remove this. */
2165 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2166 vq->signalled_used_valid = false;
2170 /* After we've used one of their buffers, we tell them about it. We'll then
2171 * want to notify the guest, using eventfd. */
2172 int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2177 start = vq->last_used_idx & (vq->num - 1);
2178 n = vq->num - start;
2180 r = __vhost_add_used_n(vq, heads, n);
2186 r = __vhost_add_used_n(vq, heads, count);
2188 /* Make sure buffer is written before we update index. */
2190 if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
2192 vq_err(vq, "Failed to increment used idx");
2195 if (unlikely(vq->log_used)) {
2196 /* Log used index update. */
2197 log_write(vq->log_base,
2198 vq->log_addr + offsetof(struct vring_used, idx),
2199 sizeof vq->used->idx);
2201 eventfd_signal(vq->log_ctx, 1);
2205 EXPORT_SYMBOL_GPL(vhost_add_used_n);
2207 static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2212 /* Flush out used index updates. This is paired
2213 * with the barrier that the Guest executes when enabling
2217 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
2218 unlikely(vq->avail_idx == vq->last_avail_idx))
2221 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
2223 if (vhost_get_avail(vq, flags, &vq->avail->flags)) {
2224 vq_err(vq, "Failed to get flags");
2227 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
2229 old = vq->signalled_used;
2230 v = vq->signalled_used_valid;
2231 new = vq->signalled_used = vq->last_used_idx;
2232 vq->signalled_used_valid = true;
2237 if (vhost_get_avail(vq, event, vhost_used_event(vq))) {
2238 vq_err(vq, "Failed to get used event idx");
2241 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
2244 /* This actually signals the guest, using eventfd. */
2245 void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2247 /* Signal the Guest tell them we used something up. */
2248 if (vq->call_ctx && vhost_notify(dev, vq))
2249 eventfd_signal(vq->call_ctx, 1);
2251 EXPORT_SYMBOL_GPL(vhost_signal);
2253 /* And here's the combo meal deal. Supersize me! */
2254 void vhost_add_used_and_signal(struct vhost_dev *dev,
2255 struct vhost_virtqueue *vq,
2256 unsigned int head, int len)
2258 vhost_add_used(vq, head, len);
2259 vhost_signal(dev, vq);
2261 EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
2263 /* multi-buffer version of vhost_add_used_and_signal */
2264 void vhost_add_used_and_signal_n(struct vhost_dev *dev,
2265 struct vhost_virtqueue *vq,
2266 struct vring_used_elem *heads, unsigned count)
2268 vhost_add_used_n(vq, heads, count);
2269 vhost_signal(dev, vq);
2271 EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
2273 /* return true if we're sure that avaiable ring is empty */
2274 bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2276 __virtio16 avail_idx;
2279 if (vq->avail_idx != vq->last_avail_idx)
2282 r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
2285 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2287 return vq->avail_idx == vq->last_avail_idx;
2289 EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
2291 /* OK, now we need to know about added descriptors. */
2292 bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2294 __virtio16 avail_idx;
2297 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
2299 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
2300 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
2301 r = vhost_update_used_flags(vq);
2303 vq_err(vq, "Failed to enable notification at %p: %d\n",
2304 &vq->used->flags, r);
2308 r = vhost_update_avail_event(vq, vq->avail_idx);
2310 vq_err(vq, "Failed to update avail event index at %p: %d\n",
2311 vhost_avail_event(vq), r);
2315 /* They could have slipped one in as we were doing that: make
2316 * sure it's written, then check again. */
2318 r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
2320 vq_err(vq, "Failed to check avail idx at %p: %d\n",
2321 &vq->avail->idx, r);
2325 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
2327 EXPORT_SYMBOL_GPL(vhost_enable_notify);
2329 /* We don't need to be notified again. */
2330 void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2334 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
2336 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
2337 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
2338 r = vhost_update_used_flags(vq);
2340 vq_err(vq, "Failed to enable notification at %p: %d\n",
2341 &vq->used->flags, r);
2344 EXPORT_SYMBOL_GPL(vhost_disable_notify);
2346 /* Create a new message. */
2347 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
2349 struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
2353 node->msg.type = type;
2356 EXPORT_SYMBOL_GPL(vhost_new_msg);
2358 void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
2359 struct vhost_msg_node *node)
2361 spin_lock(&dev->iotlb_lock);
2362 list_add_tail(&node->node, head);
2363 spin_unlock(&dev->iotlb_lock);
2365 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
2367 EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
2369 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
2370 struct list_head *head)
2372 struct vhost_msg_node *node = NULL;
2374 spin_lock(&dev->iotlb_lock);
2375 if (!list_empty(head)) {
2376 node = list_first_entry(head, struct vhost_msg_node,
2378 list_del(&node->node);
2380 spin_unlock(&dev->iotlb_lock);
2384 EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
2387 static int __init vhost_init(void)
2392 static void __exit vhost_exit(void)
2396 module_init(vhost_init);
2397 module_exit(vhost_exit);
2399 MODULE_VERSION("0.0.1");
2400 MODULE_LICENSE("GPL v2");
2401 MODULE_AUTHOR("Michael S. Tsirkin");
2402 MODULE_DESCRIPTION("Host kernel accelerator for virtio");