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>
33 #include <linux/nospec.h>
37 static ushort max_mem_regions
= 64;
38 module_param(max_mem_regions
, ushort
, 0444);
39 MODULE_PARM_DESC(max_mem_regions
,
40 "Maximum number of memory regions in memory map. (default: 64)");
41 static int max_iotlb_entries
= 2048;
42 module_param(max_iotlb_entries
, int, 0444);
43 MODULE_PARM_DESC(max_iotlb_entries
,
44 "Maximum number of iotlb entries. (default: 2048)");
47 VHOST_MEMORY_F_LOG
= 0x1,
50 #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
51 #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
53 INTERVAL_TREE_DEFINE(struct vhost_umem_node
,
54 rb
, __u64
, __subtree_last
,
55 START
, LAST
, static inline, vhost_umem_interval_tree
);
57 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
58 static void vhost_disable_cross_endian(struct vhost_virtqueue
*vq
)
60 vq
->user_be
= !virtio_legacy_is_little_endian();
63 static void vhost_enable_cross_endian_big(struct vhost_virtqueue
*vq
)
68 static void vhost_enable_cross_endian_little(struct vhost_virtqueue
*vq
)
73 static long vhost_set_vring_endian(struct vhost_virtqueue
*vq
, int __user
*argp
)
75 struct vhost_vring_state s
;
80 if (copy_from_user(&s
, argp
, sizeof(s
)))
83 if (s
.num
!= VHOST_VRING_LITTLE_ENDIAN
&&
84 s
.num
!= VHOST_VRING_BIG_ENDIAN
)
87 if (s
.num
== VHOST_VRING_BIG_ENDIAN
)
88 vhost_enable_cross_endian_big(vq
);
90 vhost_enable_cross_endian_little(vq
);
95 static long vhost_get_vring_endian(struct vhost_virtqueue
*vq
, u32 idx
,
98 struct vhost_vring_state s
= {
103 if (copy_to_user(argp
, &s
, sizeof(s
)))
109 static void vhost_init_is_le(struct vhost_virtqueue
*vq
)
111 /* Note for legacy virtio: user_be is initialized at reset time
112 * according to the host endianness. If userspace does not set an
113 * explicit endianness, the default behavior is native endian, as
114 * expected by legacy virtio.
116 vq
->is_le
= vhost_has_feature(vq
, VIRTIO_F_VERSION_1
) || !vq
->user_be
;
119 static void vhost_disable_cross_endian(struct vhost_virtqueue
*vq
)
123 static long vhost_set_vring_endian(struct vhost_virtqueue
*vq
, int __user
*argp
)
128 static long vhost_get_vring_endian(struct vhost_virtqueue
*vq
, u32 idx
,
134 static void vhost_init_is_le(struct vhost_virtqueue
*vq
)
136 vq
->is_le
= vhost_has_feature(vq
, VIRTIO_F_VERSION_1
)
137 || virtio_legacy_is_little_endian();
139 #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
141 static void vhost_reset_is_le(struct vhost_virtqueue
*vq
)
143 vhost_init_is_le(vq
);
146 struct vhost_flush_struct
{
147 struct vhost_work work
;
148 struct completion wait_event
;
151 static void vhost_flush_work(struct vhost_work
*work
)
153 struct vhost_flush_struct
*s
;
155 s
= container_of(work
, struct vhost_flush_struct
, work
);
156 complete(&s
->wait_event
);
159 static void vhost_poll_func(struct file
*file
, wait_queue_head_t
*wqh
,
162 struct vhost_poll
*poll
;
164 poll
= container_of(pt
, struct vhost_poll
, table
);
166 add_wait_queue(wqh
, &poll
->wait
);
169 static int vhost_poll_wakeup(wait_queue_entry_t
*wait
, unsigned mode
, int sync
,
172 struct vhost_poll
*poll
= container_of(wait
, struct vhost_poll
, wait
);
174 if (!(key_to_poll(key
) & poll
->mask
))
177 vhost_poll_queue(poll
);
181 void vhost_work_init(struct vhost_work
*work
, vhost_work_fn_t fn
)
183 clear_bit(VHOST_WORK_QUEUED
, &work
->flags
);
186 EXPORT_SYMBOL_GPL(vhost_work_init
);
188 /* Init poll structure */
189 void vhost_poll_init(struct vhost_poll
*poll
, vhost_work_fn_t fn
,
190 __poll_t mask
, struct vhost_dev
*dev
)
192 init_waitqueue_func_entry(&poll
->wait
, vhost_poll_wakeup
);
193 init_poll_funcptr(&poll
->table
, vhost_poll_func
);
198 vhost_work_init(&poll
->work
, fn
);
200 EXPORT_SYMBOL_GPL(vhost_poll_init
);
202 /* Start polling a file. We add ourselves to file's wait queue. The caller must
203 * keep a reference to a file until after vhost_poll_stop is called. */
204 int vhost_poll_start(struct vhost_poll
*poll
, struct file
*file
)
212 mask
= vfs_poll(file
, &poll
->table
);
214 vhost_poll_wakeup(&poll
->wait
, 0, 0, poll_to_key(mask
));
215 if (mask
& EPOLLERR
) {
216 vhost_poll_stop(poll
);
222 EXPORT_SYMBOL_GPL(vhost_poll_start
);
224 /* Stop polling a file. After this function returns, it becomes safe to drop the
225 * file reference. You must also flush afterwards. */
226 void vhost_poll_stop(struct vhost_poll
*poll
)
229 remove_wait_queue(poll
->wqh
, &poll
->wait
);
233 EXPORT_SYMBOL_GPL(vhost_poll_stop
);
235 void vhost_work_flush(struct vhost_dev
*dev
, struct vhost_work
*work
)
237 struct vhost_flush_struct flush
;
240 init_completion(&flush
.wait_event
);
241 vhost_work_init(&flush
.work
, vhost_flush_work
);
243 vhost_work_queue(dev
, &flush
.work
);
244 wait_for_completion(&flush
.wait_event
);
247 EXPORT_SYMBOL_GPL(vhost_work_flush
);
249 /* Flush any work that has been scheduled. When calling this, don't hold any
250 * locks that are also used by the callback. */
251 void vhost_poll_flush(struct vhost_poll
*poll
)
253 vhost_work_flush(poll
->dev
, &poll
->work
);
255 EXPORT_SYMBOL_GPL(vhost_poll_flush
);
257 void vhost_work_queue(struct vhost_dev
*dev
, struct vhost_work
*work
)
262 if (!test_and_set_bit(VHOST_WORK_QUEUED
, &work
->flags
)) {
263 /* We can only add the work to the list after we're
264 * sure it was not in the list.
265 * test_and_set_bit() implies a memory barrier.
267 llist_add(&work
->node
, &dev
->work_list
);
268 wake_up_process(dev
->worker
);
271 EXPORT_SYMBOL_GPL(vhost_work_queue
);
273 /* A lockless hint for busy polling code to exit the loop */
274 bool vhost_has_work(struct vhost_dev
*dev
)
276 return !llist_empty(&dev
->work_list
);
278 EXPORT_SYMBOL_GPL(vhost_has_work
);
280 void vhost_poll_queue(struct vhost_poll
*poll
)
282 vhost_work_queue(poll
->dev
, &poll
->work
);
284 EXPORT_SYMBOL_GPL(vhost_poll_queue
);
286 static void __vhost_vq_meta_reset(struct vhost_virtqueue
*vq
)
290 for (j
= 0; j
< VHOST_NUM_ADDRS
; j
++)
291 vq
->meta_iotlb
[j
] = NULL
;
294 static void vhost_vq_meta_reset(struct vhost_dev
*d
)
298 for (i
= 0; i
< d
->nvqs
; ++i
)
299 __vhost_vq_meta_reset(d
->vqs
[i
]);
302 static void vhost_vq_reset(struct vhost_dev
*dev
,
303 struct vhost_virtqueue
*vq
)
309 vq
->last_avail_idx
= 0;
311 vq
->last_used_idx
= 0;
312 vq
->signalled_used
= 0;
313 vq
->signalled_used_valid
= false;
315 vq
->log_used
= false;
316 vq
->log_addr
= -1ull;
317 vq
->private_data
= NULL
;
318 vq
->acked_features
= 0;
319 vq
->acked_backend_features
= 0;
321 vq
->error_ctx
= NULL
;
325 vhost_reset_is_le(vq
);
326 vhost_disable_cross_endian(vq
);
327 vq
->busyloop_timeout
= 0;
330 __vhost_vq_meta_reset(vq
);
333 static int vhost_worker(void *data
)
335 struct vhost_dev
*dev
= data
;
336 struct vhost_work
*work
, *work_next
;
337 struct llist_node
*node
;
338 mm_segment_t oldfs
= get_fs();
344 /* mb paired w/ kthread_stop */
345 set_current_state(TASK_INTERRUPTIBLE
);
347 if (kthread_should_stop()) {
348 __set_current_state(TASK_RUNNING
);
352 node
= llist_del_all(&dev
->work_list
);
356 node
= llist_reverse_order(node
);
357 /* make sure flag is seen after deletion */
359 llist_for_each_entry_safe(work
, work_next
, node
, node
) {
360 clear_bit(VHOST_WORK_QUEUED
, &work
->flags
);
361 __set_current_state(TASK_RUNNING
);
372 static void vhost_vq_free_iovecs(struct vhost_virtqueue
*vq
)
382 /* Helper to allocate iovec buffers for all vqs. */
383 static long vhost_dev_alloc_iovecs(struct vhost_dev
*dev
)
385 struct vhost_virtqueue
*vq
;
388 for (i
= 0; i
< dev
->nvqs
; ++i
) {
390 vq
->indirect
= kmalloc_array(UIO_MAXIOV
,
391 sizeof(*vq
->indirect
),
393 vq
->log
= kmalloc_array(dev
->iov_limit
, sizeof(*vq
->log
),
395 vq
->heads
= kmalloc_array(dev
->iov_limit
, sizeof(*vq
->heads
),
397 if (!vq
->indirect
|| !vq
->log
|| !vq
->heads
)
404 vhost_vq_free_iovecs(dev
->vqs
[i
]);
408 static void vhost_dev_free_iovecs(struct vhost_dev
*dev
)
412 for (i
= 0; i
< dev
->nvqs
; ++i
)
413 vhost_vq_free_iovecs(dev
->vqs
[i
]);
416 void vhost_dev_init(struct vhost_dev
*dev
,
417 struct vhost_virtqueue
**vqs
, int nvqs
, int iov_limit
)
419 struct vhost_virtqueue
*vq
;
424 mutex_init(&dev
->mutex
);
430 dev
->iov_limit
= iov_limit
;
431 init_llist_head(&dev
->work_list
);
432 init_waitqueue_head(&dev
->wait
);
433 INIT_LIST_HEAD(&dev
->read_list
);
434 INIT_LIST_HEAD(&dev
->pending_list
);
435 spin_lock_init(&dev
->iotlb_lock
);
438 for (i
= 0; i
< dev
->nvqs
; ++i
) {
444 mutex_init(&vq
->mutex
);
445 vhost_vq_reset(dev
, vq
);
447 vhost_poll_init(&vq
->poll
, vq
->handle_kick
,
451 EXPORT_SYMBOL_GPL(vhost_dev_init
);
453 /* Caller should have device mutex */
454 long vhost_dev_check_owner(struct vhost_dev
*dev
)
456 /* Are you the owner? If not, I don't think you mean to do that */
457 return dev
->mm
== current
->mm
? 0 : -EPERM
;
459 EXPORT_SYMBOL_GPL(vhost_dev_check_owner
);
461 struct vhost_attach_cgroups_struct
{
462 struct vhost_work work
;
463 struct task_struct
*owner
;
467 static void vhost_attach_cgroups_work(struct vhost_work
*work
)
469 struct vhost_attach_cgroups_struct
*s
;
471 s
= container_of(work
, struct vhost_attach_cgroups_struct
, work
);
472 s
->ret
= cgroup_attach_task_all(s
->owner
, current
);
475 static int vhost_attach_cgroups(struct vhost_dev
*dev
)
477 struct vhost_attach_cgroups_struct attach
;
479 attach
.owner
= current
;
480 vhost_work_init(&attach
.work
, vhost_attach_cgroups_work
);
481 vhost_work_queue(dev
, &attach
.work
);
482 vhost_work_flush(dev
, &attach
.work
);
486 /* Caller should have device mutex */
487 bool vhost_dev_has_owner(struct vhost_dev
*dev
)
491 EXPORT_SYMBOL_GPL(vhost_dev_has_owner
);
493 /* Caller should have device mutex */
494 long vhost_dev_set_owner(struct vhost_dev
*dev
)
496 struct task_struct
*worker
;
499 /* Is there an owner already? */
500 if (vhost_dev_has_owner(dev
)) {
505 /* No owner, become one */
506 dev
->mm
= get_task_mm(current
);
507 worker
= kthread_create(vhost_worker
, dev
, "vhost-%d", current
->pid
);
508 if (IS_ERR(worker
)) {
509 err
= PTR_ERR(worker
);
513 dev
->worker
= worker
;
514 wake_up_process(worker
); /* avoid contributing to loadavg */
516 err
= vhost_attach_cgroups(dev
);
520 err
= vhost_dev_alloc_iovecs(dev
);
526 kthread_stop(worker
);
535 EXPORT_SYMBOL_GPL(vhost_dev_set_owner
);
537 struct vhost_umem
*vhost_dev_reset_owner_prepare(void)
539 return kvzalloc(sizeof(struct vhost_umem
), GFP_KERNEL
);
541 EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare
);
543 /* Caller should have device mutex */
544 void vhost_dev_reset_owner(struct vhost_dev
*dev
, struct vhost_umem
*umem
)
548 vhost_dev_cleanup(dev
);
550 /* Restore memory to default empty mapping. */
551 INIT_LIST_HEAD(&umem
->umem_list
);
553 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
554 * VQs aren't running.
556 for (i
= 0; i
< dev
->nvqs
; ++i
)
557 dev
->vqs
[i
]->umem
= umem
;
559 EXPORT_SYMBOL_GPL(vhost_dev_reset_owner
);
561 void vhost_dev_stop(struct vhost_dev
*dev
)
565 for (i
= 0; i
< dev
->nvqs
; ++i
) {
566 if (dev
->vqs
[i
]->kick
&& dev
->vqs
[i
]->handle_kick
) {
567 vhost_poll_stop(&dev
->vqs
[i
]->poll
);
568 vhost_poll_flush(&dev
->vqs
[i
]->poll
);
572 EXPORT_SYMBOL_GPL(vhost_dev_stop
);
574 static void vhost_umem_free(struct vhost_umem
*umem
,
575 struct vhost_umem_node
*node
)
577 vhost_umem_interval_tree_remove(node
, &umem
->umem_tree
);
578 list_del(&node
->link
);
583 static void vhost_umem_clean(struct vhost_umem
*umem
)
585 struct vhost_umem_node
*node
, *tmp
;
590 list_for_each_entry_safe(node
, tmp
, &umem
->umem_list
, link
)
591 vhost_umem_free(umem
, node
);
596 static void vhost_clear_msg(struct vhost_dev
*dev
)
598 struct vhost_msg_node
*node
, *n
;
600 spin_lock(&dev
->iotlb_lock
);
602 list_for_each_entry_safe(node
, n
, &dev
->read_list
, node
) {
603 list_del(&node
->node
);
607 list_for_each_entry_safe(node
, n
, &dev
->pending_list
, node
) {
608 list_del(&node
->node
);
612 spin_unlock(&dev
->iotlb_lock
);
615 void vhost_dev_cleanup(struct vhost_dev
*dev
)
619 for (i
= 0; i
< dev
->nvqs
; ++i
) {
620 if (dev
->vqs
[i
]->error_ctx
)
621 eventfd_ctx_put(dev
->vqs
[i
]->error_ctx
);
622 if (dev
->vqs
[i
]->kick
)
623 fput(dev
->vqs
[i
]->kick
);
624 if (dev
->vqs
[i
]->call_ctx
)
625 eventfd_ctx_put(dev
->vqs
[i
]->call_ctx
);
626 vhost_vq_reset(dev
, dev
->vqs
[i
]);
628 vhost_dev_free_iovecs(dev
);
630 eventfd_ctx_put(dev
->log_ctx
);
632 /* No one will access memory at this point */
633 vhost_umem_clean(dev
->umem
);
635 vhost_umem_clean(dev
->iotlb
);
637 vhost_clear_msg(dev
);
638 wake_up_interruptible_poll(&dev
->wait
, EPOLLIN
| EPOLLRDNORM
);
639 WARN_ON(!llist_empty(&dev
->work_list
));
641 kthread_stop(dev
->worker
);
648 EXPORT_SYMBOL_GPL(vhost_dev_cleanup
);
650 static bool log_access_ok(void __user
*log_base
, u64 addr
, unsigned long sz
)
652 u64 a
= addr
/ VHOST_PAGE_SIZE
/ 8;
654 /* Make sure 64 bit math will not overflow. */
655 if (a
> ULONG_MAX
- (unsigned long)log_base
||
656 a
+ (unsigned long)log_base
> ULONG_MAX
)
659 return access_ok(log_base
+ a
,
660 (sz
+ VHOST_PAGE_SIZE
* 8 - 1) / VHOST_PAGE_SIZE
/ 8);
663 static bool vhost_overflow(u64 uaddr
, u64 size
)
665 /* Make sure 64 bit math will not overflow. */
666 return uaddr
> ULONG_MAX
|| size
> ULONG_MAX
|| uaddr
> ULONG_MAX
- size
;
669 /* Caller should have vq mutex and device mutex. */
670 static bool vq_memory_access_ok(void __user
*log_base
, struct vhost_umem
*umem
,
673 struct vhost_umem_node
*node
;
678 list_for_each_entry(node
, &umem
->umem_list
, link
) {
679 unsigned long a
= node
->userspace_addr
;
681 if (vhost_overflow(node
->userspace_addr
, node
->size
))
685 if (!access_ok((void __user
*)a
,
688 else if (log_all
&& !log_access_ok(log_base
,
696 static inline void __user
*vhost_vq_meta_fetch(struct vhost_virtqueue
*vq
,
697 u64 addr
, unsigned int size
,
700 const struct vhost_umem_node
*node
= vq
->meta_iotlb
[type
];
705 return (void *)(uintptr_t)(node
->userspace_addr
+ addr
- node
->start
);
708 /* Can we switch to this memory table? */
709 /* Caller should have device mutex but not vq mutex */
710 static bool memory_access_ok(struct vhost_dev
*d
, struct vhost_umem
*umem
,
715 for (i
= 0; i
< d
->nvqs
; ++i
) {
719 mutex_lock(&d
->vqs
[i
]->mutex
);
720 log
= log_all
|| vhost_has_feature(d
->vqs
[i
], VHOST_F_LOG_ALL
);
721 /* If ring is inactive, will check when it's enabled. */
722 if (d
->vqs
[i
]->private_data
)
723 ok
= vq_memory_access_ok(d
->vqs
[i
]->log_base
,
727 mutex_unlock(&d
->vqs
[i
]->mutex
);
734 static int translate_desc(struct vhost_virtqueue
*vq
, u64 addr
, u32 len
,
735 struct iovec iov
[], int iov_size
, int access
);
737 static int vhost_copy_to_user(struct vhost_virtqueue
*vq
, void __user
*to
,
738 const void *from
, unsigned size
)
743 return __copy_to_user(to
, from
, size
);
745 /* This function should be called after iotlb
746 * prefetch, which means we're sure that all vq
747 * could be access through iotlb. So -EAGAIN should
748 * not happen in this case.
751 void __user
*uaddr
= vhost_vq_meta_fetch(vq
,
752 (u64
)(uintptr_t)to
, size
,
756 return __copy_to_user(uaddr
, from
, size
);
758 ret
= translate_desc(vq
, (u64
)(uintptr_t)to
, size
, vq
->iotlb_iov
,
759 ARRAY_SIZE(vq
->iotlb_iov
),
763 iov_iter_init(&t
, WRITE
, vq
->iotlb_iov
, ret
, size
);
764 ret
= copy_to_iter(from
, size
, &t
);
772 static int vhost_copy_from_user(struct vhost_virtqueue
*vq
, void *to
,
773 void __user
*from
, unsigned size
)
778 return __copy_from_user(to
, from
, size
);
780 /* This function should be called after iotlb
781 * prefetch, which means we're sure that vq
782 * could be access through iotlb. So -EAGAIN should
783 * not happen in this case.
785 void __user
*uaddr
= vhost_vq_meta_fetch(vq
,
786 (u64
)(uintptr_t)from
, size
,
791 return __copy_from_user(to
, uaddr
, size
);
793 ret
= translate_desc(vq
, (u64
)(uintptr_t)from
, size
, vq
->iotlb_iov
,
794 ARRAY_SIZE(vq
->iotlb_iov
),
797 vq_err(vq
, "IOTLB translation failure: uaddr "
798 "%p size 0x%llx\n", from
,
799 (unsigned long long) size
);
802 iov_iter_init(&f
, READ
, vq
->iotlb_iov
, ret
, size
);
803 ret
= copy_from_iter(to
, size
, &f
);
812 static void __user
*__vhost_get_user_slow(struct vhost_virtqueue
*vq
,
813 void __user
*addr
, unsigned int size
,
818 ret
= translate_desc(vq
, (u64
)(uintptr_t)addr
, size
, vq
->iotlb_iov
,
819 ARRAY_SIZE(vq
->iotlb_iov
),
822 vq_err(vq
, "IOTLB translation failure: uaddr "
823 "%p size 0x%llx\n", addr
,
824 (unsigned long long) size
);
828 if (ret
!= 1 || vq
->iotlb_iov
[0].iov_len
!= size
) {
829 vq_err(vq
, "Non atomic userspace memory access: uaddr "
830 "%p size 0x%llx\n", addr
,
831 (unsigned long long) size
);
835 return vq
->iotlb_iov
[0].iov_base
;
838 /* This function should be called after iotlb
839 * prefetch, which means we're sure that vq
840 * could be access through iotlb. So -EAGAIN should
841 * not happen in this case.
843 static inline void __user
*__vhost_get_user(struct vhost_virtqueue
*vq
,
844 void *addr
, unsigned int size
,
847 void __user
*uaddr
= vhost_vq_meta_fetch(vq
,
848 (u64
)(uintptr_t)addr
, size
, type
);
852 return __vhost_get_user_slow(vq
, addr
, size
, type
);
855 #define vhost_put_user(vq, x, ptr) \
859 ret = __put_user(x, ptr); \
861 __typeof__(ptr) to = \
862 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
863 sizeof(*ptr), VHOST_ADDR_USED); \
865 ret = __put_user(x, to); \
872 #define vhost_get_user(vq, x, ptr, type) \
876 ret = __get_user(x, ptr); \
878 __typeof__(ptr) from = \
879 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
883 ret = __get_user(x, from); \
890 #define vhost_get_avail(vq, x, ptr) \
891 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
893 #define vhost_get_used(vq, x, ptr) \
894 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
896 static void vhost_dev_lock_vqs(struct vhost_dev
*d
)
899 for (i
= 0; i
< d
->nvqs
; ++i
)
900 mutex_lock_nested(&d
->vqs
[i
]->mutex
, i
);
903 static void vhost_dev_unlock_vqs(struct vhost_dev
*d
)
906 for (i
= 0; i
< d
->nvqs
; ++i
)
907 mutex_unlock(&d
->vqs
[i
]->mutex
);
910 static int vhost_new_umem_range(struct vhost_umem
*umem
,
911 u64 start
, u64 size
, u64 end
,
912 u64 userspace_addr
, int perm
)
914 struct vhost_umem_node
*tmp
, *node
= kmalloc(sizeof(*node
), GFP_ATOMIC
);
919 if (umem
->numem
== max_iotlb_entries
) {
920 tmp
= list_first_entry(&umem
->umem_list
, typeof(*tmp
), link
);
921 vhost_umem_free(umem
, tmp
);
927 node
->userspace_addr
= userspace_addr
;
929 INIT_LIST_HEAD(&node
->link
);
930 list_add_tail(&node
->link
, &umem
->umem_list
);
931 vhost_umem_interval_tree_insert(node
, &umem
->umem_tree
);
937 static void vhost_del_umem_range(struct vhost_umem
*umem
,
940 struct vhost_umem_node
*node
;
942 while ((node
= vhost_umem_interval_tree_iter_first(&umem
->umem_tree
,
944 vhost_umem_free(umem
, node
);
947 static void vhost_iotlb_notify_vq(struct vhost_dev
*d
,
948 struct vhost_iotlb_msg
*msg
)
950 struct vhost_msg_node
*node
, *n
;
952 spin_lock(&d
->iotlb_lock
);
954 list_for_each_entry_safe(node
, n
, &d
->pending_list
, node
) {
955 struct vhost_iotlb_msg
*vq_msg
= &node
->msg
.iotlb
;
956 if (msg
->iova
<= vq_msg
->iova
&&
957 msg
->iova
+ msg
->size
- 1 >= vq_msg
->iova
&&
958 vq_msg
->type
== VHOST_IOTLB_MISS
) {
959 vhost_poll_queue(&node
->vq
->poll
);
960 list_del(&node
->node
);
965 spin_unlock(&d
->iotlb_lock
);
968 static bool umem_access_ok(u64 uaddr
, u64 size
, int access
)
970 unsigned long a
= uaddr
;
972 /* Make sure 64 bit math will not overflow. */
973 if (vhost_overflow(uaddr
, size
))
976 if ((access
& VHOST_ACCESS_RO
) &&
977 !access_ok((void __user
*)a
, size
))
979 if ((access
& VHOST_ACCESS_WO
) &&
980 !access_ok((void __user
*)a
, size
))
985 static int vhost_process_iotlb_msg(struct vhost_dev
*dev
,
986 struct vhost_iotlb_msg
*msg
)
990 mutex_lock(&dev
->mutex
);
991 vhost_dev_lock_vqs(dev
);
993 case VHOST_IOTLB_UPDATE
:
998 if (!umem_access_ok(msg
->uaddr
, msg
->size
, msg
->perm
)) {
1002 vhost_vq_meta_reset(dev
);
1003 if (vhost_new_umem_range(dev
->iotlb
, msg
->iova
, msg
->size
,
1004 msg
->iova
+ msg
->size
- 1,
1005 msg
->uaddr
, msg
->perm
)) {
1009 vhost_iotlb_notify_vq(dev
, msg
);
1011 case VHOST_IOTLB_INVALIDATE
:
1016 vhost_vq_meta_reset(dev
);
1017 vhost_del_umem_range(dev
->iotlb
, msg
->iova
,
1018 msg
->iova
+ msg
->size
- 1);
1025 vhost_dev_unlock_vqs(dev
);
1026 mutex_unlock(&dev
->mutex
);
1030 ssize_t
vhost_chr_write_iter(struct vhost_dev
*dev
,
1031 struct iov_iter
*from
)
1033 struct vhost_iotlb_msg msg
;
1037 ret
= copy_from_iter(&type
, sizeof(type
), from
);
1038 if (ret
!= sizeof(type
)) {
1044 case VHOST_IOTLB_MSG
:
1045 /* There maybe a hole after type for V1 message type,
1048 offset
= offsetof(struct vhost_msg
, iotlb
) - sizeof(int);
1050 case VHOST_IOTLB_MSG_V2
:
1051 offset
= sizeof(__u32
);
1058 iov_iter_advance(from
, offset
);
1059 ret
= copy_from_iter(&msg
, sizeof(msg
), from
);
1060 if (ret
!= sizeof(msg
)) {
1064 if (vhost_process_iotlb_msg(dev
, &msg
)) {
1069 ret
= (type
== VHOST_IOTLB_MSG
) ? sizeof(struct vhost_msg
) :
1070 sizeof(struct vhost_msg_v2
);
1074 EXPORT_SYMBOL(vhost_chr_write_iter
);
1076 __poll_t
vhost_chr_poll(struct file
*file
, struct vhost_dev
*dev
,
1081 poll_wait(file
, &dev
->wait
, wait
);
1083 if (!list_empty(&dev
->read_list
))
1084 mask
|= EPOLLIN
| EPOLLRDNORM
;
1088 EXPORT_SYMBOL(vhost_chr_poll
);
1090 ssize_t
vhost_chr_read_iter(struct vhost_dev
*dev
, struct iov_iter
*to
,
1094 struct vhost_msg_node
*node
;
1096 unsigned size
= sizeof(struct vhost_msg
);
1098 if (iov_iter_count(to
) < size
)
1103 prepare_to_wait(&dev
->wait
, &wait
,
1104 TASK_INTERRUPTIBLE
);
1106 node
= vhost_dequeue_msg(dev
, &dev
->read_list
);
1113 if (signal_pending(current
)) {
1126 finish_wait(&dev
->wait
, &wait
);
1129 struct vhost_iotlb_msg
*msg
;
1130 void *start
= &node
->msg
;
1132 switch (node
->msg
.type
) {
1133 case VHOST_IOTLB_MSG
:
1134 size
= sizeof(node
->msg
);
1135 msg
= &node
->msg
.iotlb
;
1137 case VHOST_IOTLB_MSG_V2
:
1138 size
= sizeof(node
->msg_v2
);
1139 msg
= &node
->msg_v2
.iotlb
;
1146 ret
= copy_to_iter(start
, size
, to
);
1147 if (ret
!= size
|| msg
->type
!= VHOST_IOTLB_MISS
) {
1151 vhost_enqueue_msg(dev
, &dev
->pending_list
, node
);
1156 EXPORT_SYMBOL_GPL(vhost_chr_read_iter
);
1158 static int vhost_iotlb_miss(struct vhost_virtqueue
*vq
, u64 iova
, int access
)
1160 struct vhost_dev
*dev
= vq
->dev
;
1161 struct vhost_msg_node
*node
;
1162 struct vhost_iotlb_msg
*msg
;
1163 bool v2
= vhost_backend_has_feature(vq
, VHOST_BACKEND_F_IOTLB_MSG_V2
);
1165 node
= vhost_new_msg(vq
, v2
? VHOST_IOTLB_MSG_V2
: VHOST_IOTLB_MSG
);
1170 node
->msg_v2
.type
= VHOST_IOTLB_MSG_V2
;
1171 msg
= &node
->msg_v2
.iotlb
;
1173 msg
= &node
->msg
.iotlb
;
1176 msg
->type
= VHOST_IOTLB_MISS
;
1180 vhost_enqueue_msg(dev
, &dev
->read_list
, node
);
1185 static bool vq_access_ok(struct vhost_virtqueue
*vq
, unsigned int num
,
1186 struct vring_desc __user
*desc
,
1187 struct vring_avail __user
*avail
,
1188 struct vring_used __user
*used
)
1191 size_t s __maybe_unused
= vhost_has_feature(vq
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
1193 return access_ok(desc
, num
* sizeof *desc
) &&
1195 sizeof *avail
+ num
* sizeof *avail
->ring
+ s
) &&
1197 sizeof *used
+ num
* sizeof *used
->ring
+ s
);
1200 static void vhost_vq_meta_update(struct vhost_virtqueue
*vq
,
1201 const struct vhost_umem_node
*node
,
1204 int access
= (type
== VHOST_ADDR_USED
) ?
1205 VHOST_ACCESS_WO
: VHOST_ACCESS_RO
;
1207 if (likely(node
->perm
& access
))
1208 vq
->meta_iotlb
[type
] = node
;
1211 static bool iotlb_access_ok(struct vhost_virtqueue
*vq
,
1212 int access
, u64 addr
, u64 len
, int type
)
1214 const struct vhost_umem_node
*node
;
1215 struct vhost_umem
*umem
= vq
->iotlb
;
1216 u64 s
= 0, size
, orig_addr
= addr
, last
= addr
+ len
- 1;
1218 if (vhost_vq_meta_fetch(vq
, addr
, len
, type
))
1222 node
= vhost_umem_interval_tree_iter_first(&umem
->umem_tree
,
1225 if (node
== NULL
|| node
->start
> addr
) {
1226 vhost_iotlb_miss(vq
, addr
, access
);
1228 } else if (!(node
->perm
& access
)) {
1229 /* Report the possible access violation by
1230 * request another translation from userspace.
1235 size
= node
->size
- addr
+ node
->start
;
1237 if (orig_addr
== addr
&& size
>= len
)
1238 vhost_vq_meta_update(vq
, node
, type
);
1247 int vq_iotlb_prefetch(struct vhost_virtqueue
*vq
)
1249 size_t s
= vhost_has_feature(vq
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
1250 unsigned int num
= vq
->num
;
1255 return iotlb_access_ok(vq
, VHOST_ACCESS_RO
, (u64
)(uintptr_t)vq
->desc
,
1256 num
* sizeof(*vq
->desc
), VHOST_ADDR_DESC
) &&
1257 iotlb_access_ok(vq
, VHOST_ACCESS_RO
, (u64
)(uintptr_t)vq
->avail
,
1259 num
* sizeof(*vq
->avail
->ring
) + s
,
1260 VHOST_ADDR_AVAIL
) &&
1261 iotlb_access_ok(vq
, VHOST_ACCESS_WO
, (u64
)(uintptr_t)vq
->used
,
1263 num
* sizeof(*vq
->used
->ring
) + s
,
1266 EXPORT_SYMBOL_GPL(vq_iotlb_prefetch
);
1268 /* Can we log writes? */
1269 /* Caller should have device mutex but not vq mutex */
1270 bool vhost_log_access_ok(struct vhost_dev
*dev
)
1272 return memory_access_ok(dev
, dev
->umem
, 1);
1274 EXPORT_SYMBOL_GPL(vhost_log_access_ok
);
1276 /* Verify access for write logging. */
1277 /* Caller should have vq mutex and device mutex */
1278 static bool vq_log_access_ok(struct vhost_virtqueue
*vq
,
1279 void __user
*log_base
)
1281 size_t s
= vhost_has_feature(vq
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
1283 return vq_memory_access_ok(log_base
, vq
->umem
,
1284 vhost_has_feature(vq
, VHOST_F_LOG_ALL
)) &&
1285 (!vq
->log_used
|| log_access_ok(log_base
, vq
->log_addr
,
1287 vq
->num
* sizeof *vq
->used
->ring
+ s
));
1290 /* Can we start vq? */
1291 /* Caller should have vq mutex and device mutex */
1292 bool vhost_vq_access_ok(struct vhost_virtqueue
*vq
)
1294 if (!vq_log_access_ok(vq
, vq
->log_base
))
1297 /* Access validation occurs at prefetch time with IOTLB */
1301 return vq_access_ok(vq
, vq
->num
, vq
->desc
, vq
->avail
, vq
->used
);
1303 EXPORT_SYMBOL_GPL(vhost_vq_access_ok
);
1305 static struct vhost_umem
*vhost_umem_alloc(void)
1307 struct vhost_umem
*umem
= kvzalloc(sizeof(*umem
), GFP_KERNEL
);
1312 umem
->umem_tree
= RB_ROOT_CACHED
;
1314 INIT_LIST_HEAD(&umem
->umem_list
);
1319 static long vhost_set_memory(struct vhost_dev
*d
, struct vhost_memory __user
*m
)
1321 struct vhost_memory mem
, *newmem
;
1322 struct vhost_memory_region
*region
;
1323 struct vhost_umem
*newumem
, *oldumem
;
1324 unsigned long size
= offsetof(struct vhost_memory
, regions
);
1327 if (copy_from_user(&mem
, m
, size
))
1331 if (mem
.nregions
> max_mem_regions
)
1333 newmem
= kvzalloc(struct_size(newmem
, regions
, mem
.nregions
),
1338 memcpy(newmem
, &mem
, size
);
1339 if (copy_from_user(newmem
->regions
, m
->regions
,
1340 mem
.nregions
* sizeof *m
->regions
)) {
1345 newumem
= vhost_umem_alloc();
1351 for (region
= newmem
->regions
;
1352 region
< newmem
->regions
+ mem
.nregions
;
1354 if (vhost_new_umem_range(newumem
,
1355 region
->guest_phys_addr
,
1356 region
->memory_size
,
1357 region
->guest_phys_addr
+
1358 region
->memory_size
- 1,
1359 region
->userspace_addr
,
1364 if (!memory_access_ok(d
, newumem
, 0))
1370 /* All memory accesses are done under some VQ mutex. */
1371 for (i
= 0; i
< d
->nvqs
; ++i
) {
1372 mutex_lock(&d
->vqs
[i
]->mutex
);
1373 d
->vqs
[i
]->umem
= newumem
;
1374 mutex_unlock(&d
->vqs
[i
]->mutex
);
1378 vhost_umem_clean(oldumem
);
1382 vhost_umem_clean(newumem
);
1387 long vhost_vring_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, void __user
*argp
)
1389 struct file
*eventfp
, *filep
= NULL
;
1390 bool pollstart
= false, pollstop
= false;
1391 struct eventfd_ctx
*ctx
= NULL
;
1392 u32 __user
*idxp
= argp
;
1393 struct vhost_virtqueue
*vq
;
1394 struct vhost_vring_state s
;
1395 struct vhost_vring_file f
;
1396 struct vhost_vring_addr a
;
1400 r
= get_user(idx
, idxp
);
1406 idx
= array_index_nospec(idx
, d
->nvqs
);
1409 mutex_lock(&vq
->mutex
);
1412 case VHOST_SET_VRING_NUM
:
1413 /* Resizing ring with an active backend?
1414 * You don't want to do that. */
1415 if (vq
->private_data
) {
1419 if (copy_from_user(&s
, argp
, sizeof s
)) {
1423 if (!s
.num
|| s
.num
> 0xffff || (s
.num
& (s
.num
- 1))) {
1429 case VHOST_SET_VRING_BASE
:
1430 /* Moving base with an active backend?
1431 * You don't want to do that. */
1432 if (vq
->private_data
) {
1436 if (copy_from_user(&s
, argp
, sizeof s
)) {
1440 if (s
.num
> 0xffff) {
1444 vq
->last_avail_idx
= s
.num
;
1445 /* Forget the cached index value. */
1446 vq
->avail_idx
= vq
->last_avail_idx
;
1448 case VHOST_GET_VRING_BASE
:
1450 s
.num
= vq
->last_avail_idx
;
1451 if (copy_to_user(argp
, &s
, sizeof s
))
1454 case VHOST_SET_VRING_ADDR
:
1455 if (copy_from_user(&a
, argp
, sizeof a
)) {
1459 if (a
.flags
& ~(0x1 << VHOST_VRING_F_LOG
)) {
1463 /* For 32bit, verify that the top 32bits of the user
1464 data are set to zero. */
1465 if ((u64
)(unsigned long)a
.desc_user_addr
!= a
.desc_user_addr
||
1466 (u64
)(unsigned long)a
.used_user_addr
!= a
.used_user_addr
||
1467 (u64
)(unsigned long)a
.avail_user_addr
!= a
.avail_user_addr
) {
1472 /* Make sure it's safe to cast pointers to vring types. */
1473 BUILD_BUG_ON(__alignof__
*vq
->avail
> VRING_AVAIL_ALIGN_SIZE
);
1474 BUILD_BUG_ON(__alignof__
*vq
->used
> VRING_USED_ALIGN_SIZE
);
1475 if ((a
.avail_user_addr
& (VRING_AVAIL_ALIGN_SIZE
- 1)) ||
1476 (a
.used_user_addr
& (VRING_USED_ALIGN_SIZE
- 1)) ||
1477 (a
.log_guest_addr
& (VRING_USED_ALIGN_SIZE
- 1))) {
1482 /* We only verify access here if backend is configured.
1483 * If it is not, we don't as size might not have been setup.
1484 * We will verify when backend is configured. */
1485 if (vq
->private_data
) {
1486 if (!vq_access_ok(vq
, vq
->num
,
1487 (void __user
*)(unsigned long)a
.desc_user_addr
,
1488 (void __user
*)(unsigned long)a
.avail_user_addr
,
1489 (void __user
*)(unsigned long)a
.used_user_addr
)) {
1494 /* Also validate log access for used ring if enabled. */
1495 if ((a
.flags
& (0x1 << VHOST_VRING_F_LOG
)) &&
1496 !log_access_ok(vq
->log_base
, a
.log_guest_addr
,
1498 vq
->num
* sizeof *vq
->used
->ring
)) {
1504 vq
->log_used
= !!(a
.flags
& (0x1 << VHOST_VRING_F_LOG
));
1505 vq
->desc
= (void __user
*)(unsigned long)a
.desc_user_addr
;
1506 vq
->avail
= (void __user
*)(unsigned long)a
.avail_user_addr
;
1507 vq
->log_addr
= a
.log_guest_addr
;
1508 vq
->used
= (void __user
*)(unsigned long)a
.used_user_addr
;
1510 case VHOST_SET_VRING_KICK
:
1511 if (copy_from_user(&f
, argp
, sizeof f
)) {
1515 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
1516 if (IS_ERR(eventfp
)) {
1517 r
= PTR_ERR(eventfp
);
1520 if (eventfp
!= vq
->kick
) {
1521 pollstop
= (filep
= vq
->kick
) != NULL
;
1522 pollstart
= (vq
->kick
= eventfp
) != NULL
;
1526 case VHOST_SET_VRING_CALL
:
1527 if (copy_from_user(&f
, argp
, sizeof f
)) {
1531 ctx
= f
.fd
== -1 ? NULL
: eventfd_ctx_fdget(f
.fd
);
1536 swap(ctx
, vq
->call_ctx
);
1538 case VHOST_SET_VRING_ERR
:
1539 if (copy_from_user(&f
, argp
, sizeof f
)) {
1543 ctx
= f
.fd
== -1 ? NULL
: eventfd_ctx_fdget(f
.fd
);
1548 swap(ctx
, vq
->error_ctx
);
1550 case VHOST_SET_VRING_ENDIAN
:
1551 r
= vhost_set_vring_endian(vq
, argp
);
1553 case VHOST_GET_VRING_ENDIAN
:
1554 r
= vhost_get_vring_endian(vq
, idx
, argp
);
1556 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT
:
1557 if (copy_from_user(&s
, argp
, sizeof(s
))) {
1561 vq
->busyloop_timeout
= s
.num
;
1563 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT
:
1565 s
.num
= vq
->busyloop_timeout
;
1566 if (copy_to_user(argp
, &s
, sizeof(s
)))
1573 if (pollstop
&& vq
->handle_kick
)
1574 vhost_poll_stop(&vq
->poll
);
1576 if (!IS_ERR_OR_NULL(ctx
))
1577 eventfd_ctx_put(ctx
);
1581 if (pollstart
&& vq
->handle_kick
)
1582 r
= vhost_poll_start(&vq
->poll
, vq
->kick
);
1584 mutex_unlock(&vq
->mutex
);
1586 if (pollstop
&& vq
->handle_kick
)
1587 vhost_poll_flush(&vq
->poll
);
1590 EXPORT_SYMBOL_GPL(vhost_vring_ioctl
);
1592 int vhost_init_device_iotlb(struct vhost_dev
*d
, bool enabled
)
1594 struct vhost_umem
*niotlb
, *oiotlb
;
1597 niotlb
= vhost_umem_alloc();
1604 for (i
= 0; i
< d
->nvqs
; ++i
) {
1605 struct vhost_virtqueue
*vq
= d
->vqs
[i
];
1607 mutex_lock(&vq
->mutex
);
1609 __vhost_vq_meta_reset(vq
);
1610 mutex_unlock(&vq
->mutex
);
1613 vhost_umem_clean(oiotlb
);
1617 EXPORT_SYMBOL_GPL(vhost_init_device_iotlb
);
1619 /* Caller must have device mutex */
1620 long vhost_dev_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, void __user
*argp
)
1622 struct eventfd_ctx
*ctx
;
1627 /* If you are not the owner, you can become one */
1628 if (ioctl
== VHOST_SET_OWNER
) {
1629 r
= vhost_dev_set_owner(d
);
1633 /* You must be the owner to do anything else */
1634 r
= vhost_dev_check_owner(d
);
1639 case VHOST_SET_MEM_TABLE
:
1640 r
= vhost_set_memory(d
, argp
);
1642 case VHOST_SET_LOG_BASE
:
1643 if (copy_from_user(&p
, argp
, sizeof p
)) {
1647 if ((u64
)(unsigned long)p
!= p
) {
1651 for (i
= 0; i
< d
->nvqs
; ++i
) {
1652 struct vhost_virtqueue
*vq
;
1653 void __user
*base
= (void __user
*)(unsigned long)p
;
1655 mutex_lock(&vq
->mutex
);
1656 /* If ring is inactive, will check when it's enabled. */
1657 if (vq
->private_data
&& !vq_log_access_ok(vq
, base
))
1660 vq
->log_base
= base
;
1661 mutex_unlock(&vq
->mutex
);
1664 case VHOST_SET_LOG_FD
:
1665 r
= get_user(fd
, (int __user
*)argp
);
1668 ctx
= fd
== -1 ? NULL
: eventfd_ctx_fdget(fd
);
1673 swap(ctx
, d
->log_ctx
);
1674 for (i
= 0; i
< d
->nvqs
; ++i
) {
1675 mutex_lock(&d
->vqs
[i
]->mutex
);
1676 d
->vqs
[i
]->log_ctx
= d
->log_ctx
;
1677 mutex_unlock(&d
->vqs
[i
]->mutex
);
1680 eventfd_ctx_put(ctx
);
1689 EXPORT_SYMBOL_GPL(vhost_dev_ioctl
);
1691 /* TODO: This is really inefficient. We need something like get_user()
1692 * (instruction directly accesses the data, with an exception table entry
1693 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1695 static int set_bit_to_user(int nr
, void __user
*addr
)
1697 unsigned long log
= (unsigned long)addr
;
1700 int bit
= nr
+ (log
% PAGE_SIZE
) * 8;
1703 r
= get_user_pages_fast(log
, 1, 1, &page
);
1707 base
= kmap_atomic(page
);
1709 kunmap_atomic(base
);
1710 set_page_dirty_lock(page
);
1715 static int log_write(void __user
*log_base
,
1716 u64 write_address
, u64 write_length
)
1718 u64 write_page
= write_address
/ VHOST_PAGE_SIZE
;
1723 write_length
+= write_address
% VHOST_PAGE_SIZE
;
1725 u64 base
= (u64
)(unsigned long)log_base
;
1726 u64 log
= base
+ write_page
/ 8;
1727 int bit
= write_page
% 8;
1728 if ((u64
)(unsigned long)log
!= log
)
1730 r
= set_bit_to_user(bit
, (void __user
*)(unsigned long)log
);
1733 if (write_length
<= VHOST_PAGE_SIZE
)
1735 write_length
-= VHOST_PAGE_SIZE
;
1741 static int log_write_hva(struct vhost_virtqueue
*vq
, u64 hva
, u64 len
)
1743 struct vhost_umem
*umem
= vq
->umem
;
1744 struct vhost_umem_node
*u
;
1745 u64 start
, end
, l
, min
;
1751 /* More than one GPAs can be mapped into a single HVA. So
1752 * iterate all possible umems here to be safe.
1754 list_for_each_entry(u
, &umem
->umem_list
, link
) {
1755 if (u
->userspace_addr
> hva
- 1 + len
||
1756 u
->userspace_addr
- 1 + u
->size
< hva
)
1758 start
= max(u
->userspace_addr
, hva
);
1759 end
= min(u
->userspace_addr
- 1 + u
->size
,
1761 l
= end
- start
+ 1;
1762 r
= log_write(vq
->log_base
,
1763 u
->start
+ start
- u
->userspace_addr
,
1781 static int log_used(struct vhost_virtqueue
*vq
, u64 used_offset
, u64 len
)
1783 struct iovec iov
[64];
1787 return log_write(vq
->log_base
, vq
->log_addr
+ used_offset
, len
);
1789 ret
= translate_desc(vq
, (uintptr_t)vq
->used
+ used_offset
,
1790 len
, iov
, 64, VHOST_ACCESS_WO
);
1794 for (i
= 0; i
< ret
; i
++) {
1795 ret
= log_write_hva(vq
, (uintptr_t)iov
[i
].iov_base
,
1804 int vhost_log_write(struct vhost_virtqueue
*vq
, struct vhost_log
*log
,
1805 unsigned int log_num
, u64 len
, struct iovec
*iov
, int count
)
1809 /* Make sure data written is seen before log. */
1813 for (i
= 0; i
< count
; i
++) {
1814 r
= log_write_hva(vq
, (uintptr_t)iov
[i
].iov_base
,
1822 for (i
= 0; i
< log_num
; ++i
) {
1823 u64 l
= min(log
[i
].len
, len
);
1824 r
= log_write(vq
->log_base
, log
[i
].addr
, l
);
1830 eventfd_signal(vq
->log_ctx
, 1);
1834 /* Length written exceeds what we have stored. This is a bug. */
1838 EXPORT_SYMBOL_GPL(vhost_log_write
);
1840 static int vhost_update_used_flags(struct vhost_virtqueue
*vq
)
1843 if (vhost_put_user(vq
, cpu_to_vhost16(vq
, vq
->used_flags
),
1844 &vq
->used
->flags
) < 0)
1846 if (unlikely(vq
->log_used
)) {
1847 /* Make sure the flag is seen before log. */
1849 /* Log used flag write. */
1850 used
= &vq
->used
->flags
;
1851 log_used(vq
, (used
- (void __user
*)vq
->used
),
1852 sizeof vq
->used
->flags
);
1854 eventfd_signal(vq
->log_ctx
, 1);
1859 static int vhost_update_avail_event(struct vhost_virtqueue
*vq
, u16 avail_event
)
1861 if (vhost_put_user(vq
, cpu_to_vhost16(vq
, vq
->avail_idx
),
1862 vhost_avail_event(vq
)))
1864 if (unlikely(vq
->log_used
)) {
1866 /* Make sure the event is seen before log. */
1868 /* Log avail event write */
1869 used
= vhost_avail_event(vq
);
1870 log_used(vq
, (used
- (void __user
*)vq
->used
),
1871 sizeof *vhost_avail_event(vq
));
1873 eventfd_signal(vq
->log_ctx
, 1);
1878 int vhost_vq_init_access(struct vhost_virtqueue
*vq
)
1880 __virtio16 last_used_idx
;
1882 bool is_le
= vq
->is_le
;
1884 if (!vq
->private_data
)
1887 vhost_init_is_le(vq
);
1889 r
= vhost_update_used_flags(vq
);
1892 vq
->signalled_used_valid
= false;
1894 !access_ok(&vq
->used
->idx
, sizeof vq
->used
->idx
)) {
1898 r
= vhost_get_used(vq
, last_used_idx
, &vq
->used
->idx
);
1900 vq_err(vq
, "Can't access used idx at %p\n",
1904 vq
->last_used_idx
= vhost16_to_cpu(vq
, last_used_idx
);
1911 EXPORT_SYMBOL_GPL(vhost_vq_init_access
);
1913 static int translate_desc(struct vhost_virtqueue
*vq
, u64 addr
, u32 len
,
1914 struct iovec iov
[], int iov_size
, int access
)
1916 const struct vhost_umem_node
*node
;
1917 struct vhost_dev
*dev
= vq
->dev
;
1918 struct vhost_umem
*umem
= dev
->iotlb
? dev
->iotlb
: dev
->umem
;
1923 while ((u64
)len
> s
) {
1925 if (unlikely(ret
>= iov_size
)) {
1930 node
= vhost_umem_interval_tree_iter_first(&umem
->umem_tree
,
1931 addr
, addr
+ len
- 1);
1932 if (node
== NULL
|| node
->start
> addr
) {
1933 if (umem
!= dev
->iotlb
) {
1939 } else if (!(node
->perm
& access
)) {
1945 size
= node
->size
- addr
+ node
->start
;
1946 _iov
->iov_len
= min((u64
)len
- s
, size
);
1947 _iov
->iov_base
= (void __user
*)(unsigned long)
1948 (node
->userspace_addr
+ addr
- node
->start
);
1955 vhost_iotlb_miss(vq
, addr
, access
);
1959 /* Each buffer in the virtqueues is actually a chain of descriptors. This
1960 * function returns the next descriptor in the chain,
1961 * or -1U if we're at the end. */
1962 static unsigned next_desc(struct vhost_virtqueue
*vq
, struct vring_desc
*desc
)
1966 /* If this descriptor says it doesn't chain, we're done. */
1967 if (!(desc
->flags
& cpu_to_vhost16(vq
, VRING_DESC_F_NEXT
)))
1970 /* Check they're not leading us off end of descriptors. */
1971 next
= vhost16_to_cpu(vq
, READ_ONCE(desc
->next
));
1975 static int get_indirect(struct vhost_virtqueue
*vq
,
1976 struct iovec iov
[], unsigned int iov_size
,
1977 unsigned int *out_num
, unsigned int *in_num
,
1978 struct vhost_log
*log
, unsigned int *log_num
,
1979 struct vring_desc
*indirect
)
1981 struct vring_desc desc
;
1982 unsigned int i
= 0, count
, found
= 0;
1983 u32 len
= vhost32_to_cpu(vq
, indirect
->len
);
1984 struct iov_iter from
;
1988 if (unlikely(len
% sizeof desc
)) {
1989 vq_err(vq
, "Invalid length in indirect descriptor: "
1990 "len 0x%llx not multiple of 0x%zx\n",
1991 (unsigned long long)len
,
1996 ret
= translate_desc(vq
, vhost64_to_cpu(vq
, indirect
->addr
), len
, vq
->indirect
,
1997 UIO_MAXIOV
, VHOST_ACCESS_RO
);
1998 if (unlikely(ret
< 0)) {
2000 vq_err(vq
, "Translation failure %d in indirect.\n", ret
);
2003 iov_iter_init(&from
, READ
, vq
->indirect
, ret
, len
);
2005 /* We will use the result as an address to read from, so most
2006 * architectures only need a compiler barrier here. */
2007 read_barrier_depends();
2009 count
= len
/ sizeof desc
;
2010 /* Buffers are chained via a 16 bit next field, so
2011 * we can have at most 2^16 of these. */
2012 if (unlikely(count
> USHRT_MAX
+ 1)) {
2013 vq_err(vq
, "Indirect buffer length too big: %d\n",
2019 unsigned iov_count
= *in_num
+ *out_num
;
2020 if (unlikely(++found
> count
)) {
2021 vq_err(vq
, "Loop detected: last one at %u "
2022 "indirect size %u\n",
2026 if (unlikely(!copy_from_iter_full(&desc
, sizeof(desc
), &from
))) {
2027 vq_err(vq
, "Failed indirect descriptor: idx %d, %zx\n",
2028 i
, (size_t)vhost64_to_cpu(vq
, indirect
->addr
) + i
* sizeof desc
);
2031 if (unlikely(desc
.flags
& cpu_to_vhost16(vq
, VRING_DESC_F_INDIRECT
))) {
2032 vq_err(vq
, "Nested indirect descriptor: idx %d, %zx\n",
2033 i
, (size_t)vhost64_to_cpu(vq
, indirect
->addr
) + i
* sizeof desc
);
2037 if (desc
.flags
& cpu_to_vhost16(vq
, VRING_DESC_F_WRITE
))
2038 access
= VHOST_ACCESS_WO
;
2040 access
= VHOST_ACCESS_RO
;
2042 ret
= translate_desc(vq
, vhost64_to_cpu(vq
, desc
.addr
),
2043 vhost32_to_cpu(vq
, desc
.len
), iov
+ iov_count
,
2044 iov_size
- iov_count
, access
);
2045 if (unlikely(ret
< 0)) {
2047 vq_err(vq
, "Translation failure %d indirect idx %d\n",
2051 /* If this is an input descriptor, increment that count. */
2052 if (access
== VHOST_ACCESS_WO
) {
2054 if (unlikely(log
)) {
2055 log
[*log_num
].addr
= vhost64_to_cpu(vq
, desc
.addr
);
2056 log
[*log_num
].len
= vhost32_to_cpu(vq
, desc
.len
);
2060 /* If it's an output descriptor, they're all supposed
2061 * to come before any input descriptors. */
2062 if (unlikely(*in_num
)) {
2063 vq_err(vq
, "Indirect descriptor "
2064 "has out after in: idx %d\n", i
);
2069 } while ((i
= next_desc(vq
, &desc
)) != -1);
2073 /* This looks in the virtqueue and for the first available buffer, and converts
2074 * it to an iovec for convenient access. Since descriptors consist of some
2075 * number of output then some number of input descriptors, it's actually two
2076 * iovecs, but we pack them into one and note how many of each there were.
2078 * This function returns the descriptor number found, or vq->num (which is
2079 * never a valid descriptor number) if none was found. A negative code is
2080 * returned on error. */
2081 int vhost_get_vq_desc(struct vhost_virtqueue
*vq
,
2082 struct iovec iov
[], unsigned int iov_size
,
2083 unsigned int *out_num
, unsigned int *in_num
,
2084 struct vhost_log
*log
, unsigned int *log_num
)
2086 struct vring_desc desc
;
2087 unsigned int i
, head
, found
= 0;
2089 __virtio16 avail_idx
;
2090 __virtio16 ring_head
;
2093 /* Check it isn't doing very strange things with descriptor numbers. */
2094 last_avail_idx
= vq
->last_avail_idx
;
2096 if (vq
->avail_idx
== vq
->last_avail_idx
) {
2097 if (unlikely(vhost_get_avail(vq
, avail_idx
, &vq
->avail
->idx
))) {
2098 vq_err(vq
, "Failed to access avail idx at %p\n",
2102 vq
->avail_idx
= vhost16_to_cpu(vq
, avail_idx
);
2104 if (unlikely((u16
)(vq
->avail_idx
- last_avail_idx
) > vq
->num
)) {
2105 vq_err(vq
, "Guest moved used index from %u to %u",
2106 last_avail_idx
, vq
->avail_idx
);
2110 /* If there's nothing new since last we looked, return
2113 if (vq
->avail_idx
== last_avail_idx
)
2116 /* Only get avail ring entries after they have been
2122 /* Grab the next descriptor number they're advertising, and increment
2123 * the index we've seen. */
2124 if (unlikely(vhost_get_avail(vq
, ring_head
,
2125 &vq
->avail
->ring
[last_avail_idx
& (vq
->num
- 1)]))) {
2126 vq_err(vq
, "Failed to read head: idx %d address %p\n",
2128 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]);
2132 head
= vhost16_to_cpu(vq
, ring_head
);
2134 /* If their number is silly, that's an error. */
2135 if (unlikely(head
>= vq
->num
)) {
2136 vq_err(vq
, "Guest says index %u > %u is available",
2141 /* When we start there are none of either input nor output. */
2142 *out_num
= *in_num
= 0;
2148 unsigned iov_count
= *in_num
+ *out_num
;
2149 if (unlikely(i
>= vq
->num
)) {
2150 vq_err(vq
, "Desc index is %u > %u, head = %u",
2154 if (unlikely(++found
> vq
->num
)) {
2155 vq_err(vq
, "Loop detected: last one at %u "
2156 "vq size %u head %u\n",
2160 ret
= vhost_copy_from_user(vq
, &desc
, vq
->desc
+ i
,
2162 if (unlikely(ret
)) {
2163 vq_err(vq
, "Failed to get descriptor: idx %d addr %p\n",
2167 if (desc
.flags
& cpu_to_vhost16(vq
, VRING_DESC_F_INDIRECT
)) {
2168 ret
= get_indirect(vq
, iov
, iov_size
,
2170 log
, log_num
, &desc
);
2171 if (unlikely(ret
< 0)) {
2173 vq_err(vq
, "Failure detected "
2174 "in indirect descriptor at idx %d\n", i
);
2180 if (desc
.flags
& cpu_to_vhost16(vq
, VRING_DESC_F_WRITE
))
2181 access
= VHOST_ACCESS_WO
;
2183 access
= VHOST_ACCESS_RO
;
2184 ret
= translate_desc(vq
, vhost64_to_cpu(vq
, desc
.addr
),
2185 vhost32_to_cpu(vq
, desc
.len
), iov
+ iov_count
,
2186 iov_size
- iov_count
, access
);
2187 if (unlikely(ret
< 0)) {
2189 vq_err(vq
, "Translation failure %d descriptor idx %d\n",
2193 if (access
== VHOST_ACCESS_WO
) {
2194 /* If this is an input descriptor,
2195 * increment that count. */
2197 if (unlikely(log
)) {
2198 log
[*log_num
].addr
= vhost64_to_cpu(vq
, desc
.addr
);
2199 log
[*log_num
].len
= vhost32_to_cpu(vq
, desc
.len
);
2203 /* If it's an output descriptor, they're all supposed
2204 * to come before any input descriptors. */
2205 if (unlikely(*in_num
)) {
2206 vq_err(vq
, "Descriptor has out after in: "
2212 } while ((i
= next_desc(vq
, &desc
)) != -1);
2214 /* On success, increment avail index. */
2215 vq
->last_avail_idx
++;
2217 /* Assume notifications from guest are disabled at this point,
2218 * if they aren't we would need to update avail_event index. */
2219 BUG_ON(!(vq
->used_flags
& VRING_USED_F_NO_NOTIFY
));
2222 EXPORT_SYMBOL_GPL(vhost_get_vq_desc
);
2224 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
2225 void vhost_discard_vq_desc(struct vhost_virtqueue
*vq
, int n
)
2227 vq
->last_avail_idx
-= n
;
2229 EXPORT_SYMBOL_GPL(vhost_discard_vq_desc
);
2231 /* After we've used one of their buffers, we tell them about it. We'll then
2232 * want to notify the guest, using eventfd. */
2233 int vhost_add_used(struct vhost_virtqueue
*vq
, unsigned int head
, int len
)
2235 struct vring_used_elem heads
= {
2236 cpu_to_vhost32(vq
, head
),
2237 cpu_to_vhost32(vq
, len
)
2240 return vhost_add_used_n(vq
, &heads
, 1);
2242 EXPORT_SYMBOL_GPL(vhost_add_used
);
2244 static int __vhost_add_used_n(struct vhost_virtqueue
*vq
,
2245 struct vring_used_elem
*heads
,
2248 struct vring_used_elem __user
*used
;
2252 start
= vq
->last_used_idx
& (vq
->num
- 1);
2253 used
= vq
->used
->ring
+ start
;
2255 if (vhost_put_user(vq
, heads
[0].id
, &used
->id
)) {
2256 vq_err(vq
, "Failed to write used id");
2259 if (vhost_put_user(vq
, heads
[0].len
, &used
->len
)) {
2260 vq_err(vq
, "Failed to write used len");
2263 } else if (vhost_copy_to_user(vq
, used
, heads
, count
* sizeof *used
)) {
2264 vq_err(vq
, "Failed to write used");
2267 if (unlikely(vq
->log_used
)) {
2268 /* Make sure data is seen before log. */
2270 /* Log used ring entry write. */
2271 log_used(vq
, ((void __user
*)used
- (void __user
*)vq
->used
),
2272 count
* sizeof *used
);
2274 old
= vq
->last_used_idx
;
2275 new = (vq
->last_used_idx
+= count
);
2276 /* If the driver never bothers to signal in a very long while,
2277 * used index might wrap around. If that happens, invalidate
2278 * signalled_used index we stored. TODO: make sure driver
2279 * signals at least once in 2^16 and remove this. */
2280 if (unlikely((u16
)(new - vq
->signalled_used
) < (u16
)(new - old
)))
2281 vq
->signalled_used_valid
= false;
2285 /* After we've used one of their buffers, we tell them about it. We'll then
2286 * want to notify the guest, using eventfd. */
2287 int vhost_add_used_n(struct vhost_virtqueue
*vq
, struct vring_used_elem
*heads
,
2292 start
= vq
->last_used_idx
& (vq
->num
- 1);
2293 n
= vq
->num
- start
;
2295 r
= __vhost_add_used_n(vq
, heads
, n
);
2301 r
= __vhost_add_used_n(vq
, heads
, count
);
2303 /* Make sure buffer is written before we update index. */
2305 if (vhost_put_user(vq
, cpu_to_vhost16(vq
, vq
->last_used_idx
),
2307 vq_err(vq
, "Failed to increment used idx");
2310 if (unlikely(vq
->log_used
)) {
2311 /* Make sure used idx is seen before log. */
2313 /* Log used index update. */
2314 log_used(vq
, offsetof(struct vring_used
, idx
),
2315 sizeof vq
->used
->idx
);
2317 eventfd_signal(vq
->log_ctx
, 1);
2321 EXPORT_SYMBOL_GPL(vhost_add_used_n
);
2323 static bool vhost_notify(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
2328 /* Flush out used index updates. This is paired
2329 * with the barrier that the Guest executes when enabling
2333 if (vhost_has_feature(vq
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
2334 unlikely(vq
->avail_idx
== vq
->last_avail_idx
))
2337 if (!vhost_has_feature(vq
, VIRTIO_RING_F_EVENT_IDX
)) {
2339 if (vhost_get_avail(vq
, flags
, &vq
->avail
->flags
)) {
2340 vq_err(vq
, "Failed to get flags");
2343 return !(flags
& cpu_to_vhost16(vq
, VRING_AVAIL_F_NO_INTERRUPT
));
2345 old
= vq
->signalled_used
;
2346 v
= vq
->signalled_used_valid
;
2347 new = vq
->signalled_used
= vq
->last_used_idx
;
2348 vq
->signalled_used_valid
= true;
2353 if (vhost_get_avail(vq
, event
, vhost_used_event(vq
))) {
2354 vq_err(vq
, "Failed to get used event idx");
2357 return vring_need_event(vhost16_to_cpu(vq
, event
), new, old
);
2360 /* This actually signals the guest, using eventfd. */
2361 void vhost_signal(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
2363 /* Signal the Guest tell them we used something up. */
2364 if (vq
->call_ctx
&& vhost_notify(dev
, vq
))
2365 eventfd_signal(vq
->call_ctx
, 1);
2367 EXPORT_SYMBOL_GPL(vhost_signal
);
2369 /* And here's the combo meal deal. Supersize me! */
2370 void vhost_add_used_and_signal(struct vhost_dev
*dev
,
2371 struct vhost_virtqueue
*vq
,
2372 unsigned int head
, int len
)
2374 vhost_add_used(vq
, head
, len
);
2375 vhost_signal(dev
, vq
);
2377 EXPORT_SYMBOL_GPL(vhost_add_used_and_signal
);
2379 /* multi-buffer version of vhost_add_used_and_signal */
2380 void vhost_add_used_and_signal_n(struct vhost_dev
*dev
,
2381 struct vhost_virtqueue
*vq
,
2382 struct vring_used_elem
*heads
, unsigned count
)
2384 vhost_add_used_n(vq
, heads
, count
);
2385 vhost_signal(dev
, vq
);
2387 EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n
);
2389 /* return true if we're sure that avaiable ring is empty */
2390 bool vhost_vq_avail_empty(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
2392 __virtio16 avail_idx
;
2395 if (vq
->avail_idx
!= vq
->last_avail_idx
)
2398 r
= vhost_get_avail(vq
, avail_idx
, &vq
->avail
->idx
);
2401 vq
->avail_idx
= vhost16_to_cpu(vq
, avail_idx
);
2403 return vq
->avail_idx
== vq
->last_avail_idx
;
2405 EXPORT_SYMBOL_GPL(vhost_vq_avail_empty
);
2407 /* OK, now we need to know about added descriptors. */
2408 bool vhost_enable_notify(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
2410 __virtio16 avail_idx
;
2413 if (!(vq
->used_flags
& VRING_USED_F_NO_NOTIFY
))
2415 vq
->used_flags
&= ~VRING_USED_F_NO_NOTIFY
;
2416 if (!vhost_has_feature(vq
, VIRTIO_RING_F_EVENT_IDX
)) {
2417 r
= vhost_update_used_flags(vq
);
2419 vq_err(vq
, "Failed to enable notification at %p: %d\n",
2420 &vq
->used
->flags
, r
);
2424 r
= vhost_update_avail_event(vq
, vq
->avail_idx
);
2426 vq_err(vq
, "Failed to update avail event index at %p: %d\n",
2427 vhost_avail_event(vq
), r
);
2431 /* They could have slipped one in as we were doing that: make
2432 * sure it's written, then check again. */
2434 r
= vhost_get_avail(vq
, avail_idx
, &vq
->avail
->idx
);
2436 vq_err(vq
, "Failed to check avail idx at %p: %d\n",
2437 &vq
->avail
->idx
, r
);
2441 return vhost16_to_cpu(vq
, avail_idx
) != vq
->avail_idx
;
2443 EXPORT_SYMBOL_GPL(vhost_enable_notify
);
2445 /* We don't need to be notified again. */
2446 void vhost_disable_notify(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
2450 if (vq
->used_flags
& VRING_USED_F_NO_NOTIFY
)
2452 vq
->used_flags
|= VRING_USED_F_NO_NOTIFY
;
2453 if (!vhost_has_feature(vq
, VIRTIO_RING_F_EVENT_IDX
)) {
2454 r
= vhost_update_used_flags(vq
);
2456 vq_err(vq
, "Failed to enable notification at %p: %d\n",
2457 &vq
->used
->flags
, r
);
2460 EXPORT_SYMBOL_GPL(vhost_disable_notify
);
2462 /* Create a new message. */
2463 struct vhost_msg_node
*vhost_new_msg(struct vhost_virtqueue
*vq
, int type
)
2465 struct vhost_msg_node
*node
= kmalloc(sizeof *node
, GFP_KERNEL
);
2469 /* Make sure all padding within the structure is initialized. */
2470 memset(&node
->msg
, 0, sizeof node
->msg
);
2472 node
->msg
.type
= type
;
2475 EXPORT_SYMBOL_GPL(vhost_new_msg
);
2477 void vhost_enqueue_msg(struct vhost_dev
*dev
, struct list_head
*head
,
2478 struct vhost_msg_node
*node
)
2480 spin_lock(&dev
->iotlb_lock
);
2481 list_add_tail(&node
->node
, head
);
2482 spin_unlock(&dev
->iotlb_lock
);
2484 wake_up_interruptible_poll(&dev
->wait
, EPOLLIN
| EPOLLRDNORM
);
2486 EXPORT_SYMBOL_GPL(vhost_enqueue_msg
);
2488 struct vhost_msg_node
*vhost_dequeue_msg(struct vhost_dev
*dev
,
2489 struct list_head
*head
)
2491 struct vhost_msg_node
*node
= NULL
;
2493 spin_lock(&dev
->iotlb_lock
);
2494 if (!list_empty(head
)) {
2495 node
= list_first_entry(head
, struct vhost_msg_node
,
2497 list_del(&node
->node
);
2499 spin_unlock(&dev
->iotlb_lock
);
2503 EXPORT_SYMBOL_GPL(vhost_dequeue_msg
);
2506 static int __init
vhost_init(void)
2511 static void __exit
vhost_exit(void)
2515 module_init(vhost_init
);
2516 module_exit(vhost_exit
);
2518 MODULE_VERSION("0.0.1");
2519 MODULE_LICENSE("GPL v2");
2520 MODULE_AUTHOR("Michael S. Tsirkin");
2521 MODULE_DESCRIPTION("Host kernel accelerator for virtio");