1 /* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
4 * Author: Michael S. Tsirkin <mst@redhat.com>
6 * Inspiration, some code, and most witty comments come from
7 * Documentation/lguest/lguest.c, by Rusty Russell
9 * This work is licensed under the terms of the GNU GPL, version 2.
11 * Generic code for virtio server in host kernel.
14 #include <linux/eventfd.h>
15 #include <linux/vhost.h>
16 #include <linux/virtio_net.h>
18 #include <linux/mmu_context.h>
19 #include <linux/miscdevice.h>
20 #include <linux/mutex.h>
21 #include <linux/rcupdate.h>
22 #include <linux/poll.h>
23 #include <linux/file.h>
24 #include <linux/highmem.h>
25 #include <linux/slab.h>
26 #include <linux/kthread.h>
27 #include <linux/cgroup.h>
29 #include <linux/net.h>
30 #include <linux/if_packet.h>
31 #include <linux/if_arp.h>
36 VHOST_MEMORY_MAX_NREGIONS
= 64,
37 VHOST_MEMORY_F_LOG
= 0x1,
40 static void vhost_poll_func(struct file
*file
, wait_queue_head_t
*wqh
,
43 struct vhost_poll
*poll
;
45 poll
= container_of(pt
, struct vhost_poll
, table
);
47 add_wait_queue(wqh
, &poll
->wait
);
50 static int vhost_poll_wakeup(wait_queue_t
*wait
, unsigned mode
, int sync
,
53 struct vhost_poll
*poll
= container_of(wait
, struct vhost_poll
, wait
);
55 if (!((unsigned long)key
& poll
->mask
))
58 vhost_poll_queue(poll
);
62 static void vhost_work_init(struct vhost_work
*work
, vhost_work_fn_t fn
)
64 INIT_LIST_HEAD(&work
->node
);
66 init_waitqueue_head(&work
->done
);
68 work
->queue_seq
= work
->done_seq
= 0;
71 /* Init poll structure */
72 void vhost_poll_init(struct vhost_poll
*poll
, vhost_work_fn_t fn
,
73 unsigned long mask
, struct vhost_dev
*dev
)
75 init_waitqueue_func_entry(&poll
->wait
, vhost_poll_wakeup
);
76 init_poll_funcptr(&poll
->table
, vhost_poll_func
);
80 vhost_work_init(&poll
->work
, fn
);
83 /* Start polling a file. We add ourselves to file's wait queue. The caller must
84 * keep a reference to a file until after vhost_poll_stop is called. */
85 void vhost_poll_start(struct vhost_poll
*poll
, struct file
*file
)
89 mask
= file
->f_op
->poll(file
, &poll
->table
);
91 vhost_poll_wakeup(&poll
->wait
, 0, 0, (void *)mask
);
94 /* Stop polling a file. After this function returns, it becomes safe to drop the
95 * file reference. You must also flush afterwards. */
96 void vhost_poll_stop(struct vhost_poll
*poll
)
98 remove_wait_queue(poll
->wqh
, &poll
->wait
);
101 static bool vhost_work_seq_done(struct vhost_dev
*dev
, struct vhost_work
*work
,
106 spin_lock_irq(&dev
->work_lock
);
107 left
= seq
- work
->done_seq
;
108 spin_unlock_irq(&dev
->work_lock
);
112 static void vhost_work_flush(struct vhost_dev
*dev
, struct vhost_work
*work
)
117 spin_lock_irq(&dev
->work_lock
);
118 seq
= work
->queue_seq
;
120 spin_unlock_irq(&dev
->work_lock
);
121 wait_event(work
->done
, vhost_work_seq_done(dev
, work
, seq
));
122 spin_lock_irq(&dev
->work_lock
);
123 flushing
= --work
->flushing
;
124 spin_unlock_irq(&dev
->work_lock
);
125 BUG_ON(flushing
< 0);
128 /* Flush any work that has been scheduled. When calling this, don't hold any
129 * locks that are also used by the callback. */
130 void vhost_poll_flush(struct vhost_poll
*poll
)
132 vhost_work_flush(poll
->dev
, &poll
->work
);
135 static inline void vhost_work_queue(struct vhost_dev
*dev
,
136 struct vhost_work
*work
)
140 spin_lock_irqsave(&dev
->work_lock
, flags
);
141 if (list_empty(&work
->node
)) {
142 list_add_tail(&work
->node
, &dev
->work_list
);
144 wake_up_process(dev
->worker
);
146 spin_unlock_irqrestore(&dev
->work_lock
, flags
);
149 void vhost_poll_queue(struct vhost_poll
*poll
)
151 vhost_work_queue(poll
->dev
, &poll
->work
);
154 static void vhost_vq_reset(struct vhost_dev
*dev
,
155 struct vhost_virtqueue
*vq
)
161 vq
->last_avail_idx
= 0;
163 vq
->last_used_idx
= 0;
165 vq
->log_used
= false;
166 vq
->log_addr
= -1ull;
169 vq
->private_data
= NULL
;
171 vq
->error_ctx
= NULL
;
179 static int vhost_worker(void *data
)
181 struct vhost_dev
*dev
= data
;
182 struct vhost_work
*work
= NULL
;
183 unsigned uninitialized_var(seq
);
188 /* mb paired w/ kthread_stop */
189 set_current_state(TASK_INTERRUPTIBLE
);
191 spin_lock_irq(&dev
->work_lock
);
193 work
->done_seq
= seq
;
195 wake_up_all(&work
->done
);
198 if (kthread_should_stop()) {
199 spin_unlock_irq(&dev
->work_lock
);
200 __set_current_state(TASK_RUNNING
);
203 if (!list_empty(&dev
->work_list
)) {
204 work
= list_first_entry(&dev
->work_list
,
205 struct vhost_work
, node
);
206 list_del_init(&work
->node
);
207 seq
= work
->queue_seq
;
210 spin_unlock_irq(&dev
->work_lock
);
213 __set_current_state(TASK_RUNNING
);
223 /* Helper to allocate iovec buffers for all vqs. */
224 static long vhost_dev_alloc_iovecs(struct vhost_dev
*dev
)
228 for (i
= 0; i
< dev
->nvqs
; ++i
) {
229 dev
->vqs
[i
].indirect
= kmalloc(sizeof *dev
->vqs
[i
].indirect
*
230 UIO_MAXIOV
, GFP_KERNEL
);
231 dev
->vqs
[i
].log
= kmalloc(sizeof *dev
->vqs
[i
].log
* UIO_MAXIOV
,
233 dev
->vqs
[i
].heads
= kmalloc(sizeof *dev
->vqs
[i
].heads
*
234 UIO_MAXIOV
, GFP_KERNEL
);
236 if (!dev
->vqs
[i
].indirect
|| !dev
->vqs
[i
].log
||
243 for (; i
>= 0; --i
) {
244 kfree(dev
->vqs
[i
].indirect
);
245 kfree(dev
->vqs
[i
].log
);
246 kfree(dev
->vqs
[i
].heads
);
251 static void vhost_dev_free_iovecs(struct vhost_dev
*dev
)
255 for (i
= 0; i
< dev
->nvqs
; ++i
) {
256 kfree(dev
->vqs
[i
].indirect
);
257 dev
->vqs
[i
].indirect
= NULL
;
258 kfree(dev
->vqs
[i
].log
);
259 dev
->vqs
[i
].log
= NULL
;
260 kfree(dev
->vqs
[i
].heads
);
261 dev
->vqs
[i
].heads
= NULL
;
265 long vhost_dev_init(struct vhost_dev
*dev
,
266 struct vhost_virtqueue
*vqs
, int nvqs
)
272 mutex_init(&dev
->mutex
);
274 dev
->log_file
= NULL
;
277 spin_lock_init(&dev
->work_lock
);
278 INIT_LIST_HEAD(&dev
->work_list
);
281 for (i
= 0; i
< dev
->nvqs
; ++i
) {
282 dev
->vqs
[i
].log
= NULL
;
283 dev
->vqs
[i
].indirect
= NULL
;
284 dev
->vqs
[i
].heads
= NULL
;
285 dev
->vqs
[i
].dev
= dev
;
286 mutex_init(&dev
->vqs
[i
].mutex
);
287 vhost_vq_reset(dev
, dev
->vqs
+ i
);
288 if (dev
->vqs
[i
].handle_kick
)
289 vhost_poll_init(&dev
->vqs
[i
].poll
,
290 dev
->vqs
[i
].handle_kick
, POLLIN
, dev
);
296 /* Caller should have device mutex */
297 long vhost_dev_check_owner(struct vhost_dev
*dev
)
299 /* Are you the owner? If not, I don't think you mean to do that */
300 return dev
->mm
== current
->mm
? 0 : -EPERM
;
303 struct vhost_attach_cgroups_struct
{
304 struct vhost_work work
;
305 struct task_struct
*owner
;
309 static void vhost_attach_cgroups_work(struct vhost_work
*work
)
311 struct vhost_attach_cgroups_struct
*s
;
313 s
= container_of(work
, struct vhost_attach_cgroups_struct
, work
);
314 s
->ret
= cgroup_attach_task_all(s
->owner
, current
);
317 static int vhost_attach_cgroups(struct vhost_dev
*dev
)
319 struct vhost_attach_cgroups_struct attach
;
321 attach
.owner
= current
;
322 vhost_work_init(&attach
.work
, vhost_attach_cgroups_work
);
323 vhost_work_queue(dev
, &attach
.work
);
324 vhost_work_flush(dev
, &attach
.work
);
328 /* Caller should have device mutex */
329 static long vhost_dev_set_owner(struct vhost_dev
*dev
)
331 struct task_struct
*worker
;
334 /* Is there an owner already? */
340 /* No owner, become one */
341 dev
->mm
= get_task_mm(current
);
342 worker
= kthread_create(vhost_worker
, dev
, "vhost-%d", current
->pid
);
343 if (IS_ERR(worker
)) {
344 err
= PTR_ERR(worker
);
348 dev
->worker
= worker
;
349 wake_up_process(worker
); /* avoid contributing to loadavg */
351 err
= vhost_attach_cgroups(dev
);
355 err
= vhost_dev_alloc_iovecs(dev
);
361 kthread_stop(worker
);
371 /* Caller should have device mutex */
372 long vhost_dev_reset_owner(struct vhost_dev
*dev
)
374 struct vhost_memory
*memory
;
376 /* Restore memory to default empty mapping. */
377 memory
= kmalloc(offsetof(struct vhost_memory
, regions
), GFP_KERNEL
);
381 vhost_dev_cleanup(dev
);
383 memory
->nregions
= 0;
384 RCU_INIT_POINTER(dev
->memory
, memory
);
388 /* Caller should have device mutex */
389 void vhost_dev_cleanup(struct vhost_dev
*dev
)
393 for (i
= 0; i
< dev
->nvqs
; ++i
) {
394 if (dev
->vqs
[i
].kick
&& dev
->vqs
[i
].handle_kick
) {
395 vhost_poll_stop(&dev
->vqs
[i
].poll
);
396 vhost_poll_flush(&dev
->vqs
[i
].poll
);
398 if (dev
->vqs
[i
].error_ctx
)
399 eventfd_ctx_put(dev
->vqs
[i
].error_ctx
);
400 if (dev
->vqs
[i
].error
)
401 fput(dev
->vqs
[i
].error
);
402 if (dev
->vqs
[i
].kick
)
403 fput(dev
->vqs
[i
].kick
);
404 if (dev
->vqs
[i
].call_ctx
)
405 eventfd_ctx_put(dev
->vqs
[i
].call_ctx
);
406 if (dev
->vqs
[i
].call
)
407 fput(dev
->vqs
[i
].call
);
408 vhost_vq_reset(dev
, dev
->vqs
+ i
);
410 vhost_dev_free_iovecs(dev
);
412 eventfd_ctx_put(dev
->log_ctx
);
416 dev
->log_file
= NULL
;
417 /* No one will access memory at this point */
418 kfree(rcu_dereference_protected(dev
->memory
,
419 lockdep_is_held(&dev
->mutex
)));
420 RCU_INIT_POINTER(dev
->memory
, NULL
);
421 WARN_ON(!list_empty(&dev
->work_list
));
423 kthread_stop(dev
->worker
);
431 static int log_access_ok(void __user
*log_base
, u64 addr
, unsigned long sz
)
433 u64 a
= addr
/ VHOST_PAGE_SIZE
/ 8;
435 /* Make sure 64 bit math will not overflow. */
436 if (a
> ULONG_MAX
- (unsigned long)log_base
||
437 a
+ (unsigned long)log_base
> ULONG_MAX
)
440 return access_ok(VERIFY_WRITE
, log_base
+ a
,
441 (sz
+ VHOST_PAGE_SIZE
* 8 - 1) / VHOST_PAGE_SIZE
/ 8);
444 /* Caller should have vq mutex and device mutex. */
445 static int vq_memory_access_ok(void __user
*log_base
, struct vhost_memory
*mem
,
453 for (i
= 0; i
< mem
->nregions
; ++i
) {
454 struct vhost_memory_region
*m
= mem
->regions
+ i
;
455 unsigned long a
= m
->userspace_addr
;
456 if (m
->memory_size
> ULONG_MAX
)
458 else if (!access_ok(VERIFY_WRITE
, (void __user
*)a
,
461 else if (log_all
&& !log_access_ok(log_base
,
469 /* Can we switch to this memory table? */
470 /* Caller should have device mutex but not vq mutex */
471 static int memory_access_ok(struct vhost_dev
*d
, struct vhost_memory
*mem
,
476 for (i
= 0; i
< d
->nvqs
; ++i
) {
478 mutex_lock(&d
->vqs
[i
].mutex
);
479 /* If ring is inactive, will check when it's enabled. */
480 if (d
->vqs
[i
].private_data
)
481 ok
= vq_memory_access_ok(d
->vqs
[i
].log_base
, mem
,
485 mutex_unlock(&d
->vqs
[i
].mutex
);
492 static int vq_access_ok(unsigned int num
,
493 struct vring_desc __user
*desc
,
494 struct vring_avail __user
*avail
,
495 struct vring_used __user
*used
)
497 return access_ok(VERIFY_READ
, desc
, num
* sizeof *desc
) &&
498 access_ok(VERIFY_READ
, avail
,
499 sizeof *avail
+ num
* sizeof *avail
->ring
) &&
500 access_ok(VERIFY_WRITE
, used
,
501 sizeof *used
+ num
* sizeof *used
->ring
);
504 /* Can we log writes? */
505 /* Caller should have device mutex but not vq mutex */
506 int vhost_log_access_ok(struct vhost_dev
*dev
)
508 struct vhost_memory
*mp
;
510 mp
= rcu_dereference_protected(dev
->memory
,
511 lockdep_is_held(&dev
->mutex
));
512 return memory_access_ok(dev
, mp
, 1);
515 /* Verify access for write logging. */
516 /* Caller should have vq mutex and device mutex */
517 static int vq_log_access_ok(struct vhost_virtqueue
*vq
, void __user
*log_base
)
519 struct vhost_memory
*mp
;
521 mp
= rcu_dereference_protected(vq
->dev
->memory
,
522 lockdep_is_held(&vq
->mutex
));
523 return vq_memory_access_ok(log_base
, mp
,
524 vhost_has_feature(vq
->dev
, VHOST_F_LOG_ALL
)) &&
525 (!vq
->log_used
|| log_access_ok(log_base
, vq
->log_addr
,
527 vq
->num
* sizeof *vq
->used
->ring
));
530 /* Can we start vq? */
531 /* Caller should have vq mutex and device mutex */
532 int vhost_vq_access_ok(struct vhost_virtqueue
*vq
)
534 return vq_access_ok(vq
->num
, vq
->desc
, vq
->avail
, vq
->used
) &&
535 vq_log_access_ok(vq
, vq
->log_base
);
538 static long vhost_set_memory(struct vhost_dev
*d
, struct vhost_memory __user
*m
)
540 struct vhost_memory mem
, *newmem
, *oldmem
;
541 unsigned long size
= offsetof(struct vhost_memory
, regions
);
543 if (copy_from_user(&mem
, m
, size
))
547 if (mem
.nregions
> VHOST_MEMORY_MAX_NREGIONS
)
549 newmem
= kmalloc(size
+ mem
.nregions
* sizeof *m
->regions
, GFP_KERNEL
);
553 memcpy(newmem
, &mem
, size
);
554 if (copy_from_user(newmem
->regions
, m
->regions
,
555 mem
.nregions
* sizeof *m
->regions
)) {
560 if (!memory_access_ok(d
, newmem
,
561 vhost_has_feature(d
, VHOST_F_LOG_ALL
))) {
565 oldmem
= rcu_dereference_protected(d
->memory
,
566 lockdep_is_held(&d
->mutex
));
567 rcu_assign_pointer(d
->memory
, newmem
);
573 static int init_used(struct vhost_virtqueue
*vq
,
574 struct vring_used __user
*used
)
576 int r
= put_user(vq
->used_flags
, &used
->flags
);
580 return get_user(vq
->last_used_idx
, &used
->idx
);
583 static long vhost_set_vring(struct vhost_dev
*d
, int ioctl
, void __user
*argp
)
585 struct file
*eventfp
, *filep
= NULL
,
586 *pollstart
= NULL
, *pollstop
= NULL
;
587 struct eventfd_ctx
*ctx
= NULL
;
588 u32 __user
*idxp
= argp
;
589 struct vhost_virtqueue
*vq
;
590 struct vhost_vring_state s
;
591 struct vhost_vring_file f
;
592 struct vhost_vring_addr a
;
596 r
= get_user(idx
, idxp
);
604 mutex_lock(&vq
->mutex
);
607 case VHOST_SET_VRING_NUM
:
608 /* Resizing ring with an active backend?
609 * You don't want to do that. */
610 if (vq
->private_data
) {
614 if (copy_from_user(&s
, argp
, sizeof s
)) {
618 if (!s
.num
|| s
.num
> 0xffff || (s
.num
& (s
.num
- 1))) {
624 case VHOST_SET_VRING_BASE
:
625 /* Moving base with an active backend?
626 * You don't want to do that. */
627 if (vq
->private_data
) {
631 if (copy_from_user(&s
, argp
, sizeof s
)) {
635 if (s
.num
> 0xffff) {
639 vq
->last_avail_idx
= s
.num
;
640 /* Forget the cached index value. */
641 vq
->avail_idx
= vq
->last_avail_idx
;
643 case VHOST_GET_VRING_BASE
:
645 s
.num
= vq
->last_avail_idx
;
646 if (copy_to_user(argp
, &s
, sizeof s
))
649 case VHOST_SET_VRING_ADDR
:
650 if (copy_from_user(&a
, argp
, sizeof a
)) {
654 if (a
.flags
& ~(0x1 << VHOST_VRING_F_LOG
)) {
658 /* For 32bit, verify that the top 32bits of the user
659 data are set to zero. */
660 if ((u64
)(unsigned long)a
.desc_user_addr
!= a
.desc_user_addr
||
661 (u64
)(unsigned long)a
.used_user_addr
!= a
.used_user_addr
||
662 (u64
)(unsigned long)a
.avail_user_addr
!= a
.avail_user_addr
) {
666 if ((a
.avail_user_addr
& (sizeof *vq
->avail
->ring
- 1)) ||
667 (a
.used_user_addr
& (sizeof *vq
->used
->ring
- 1)) ||
668 (a
.log_guest_addr
& (sizeof *vq
->used
->ring
- 1))) {
673 /* We only verify access here if backend is configured.
674 * If it is not, we don't as size might not have been setup.
675 * We will verify when backend is configured. */
676 if (vq
->private_data
) {
677 if (!vq_access_ok(vq
->num
,
678 (void __user
*)(unsigned long)a
.desc_user_addr
,
679 (void __user
*)(unsigned long)a
.avail_user_addr
,
680 (void __user
*)(unsigned long)a
.used_user_addr
)) {
685 /* Also validate log access for used ring if enabled. */
686 if ((a
.flags
& (0x1 << VHOST_VRING_F_LOG
)) &&
687 !log_access_ok(vq
->log_base
, a
.log_guest_addr
,
689 vq
->num
* sizeof *vq
->used
->ring
)) {
695 r
= init_used(vq
, (struct vring_used __user
*)(unsigned long)
699 vq
->log_used
= !!(a
.flags
& (0x1 << VHOST_VRING_F_LOG
));
700 vq
->desc
= (void __user
*)(unsigned long)a
.desc_user_addr
;
701 vq
->avail
= (void __user
*)(unsigned long)a
.avail_user_addr
;
702 vq
->log_addr
= a
.log_guest_addr
;
703 vq
->used
= (void __user
*)(unsigned long)a
.used_user_addr
;
705 case VHOST_SET_VRING_KICK
:
706 if (copy_from_user(&f
, argp
, sizeof f
)) {
710 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
711 if (IS_ERR(eventfp
)) {
712 r
= PTR_ERR(eventfp
);
715 if (eventfp
!= vq
->kick
) {
716 pollstop
= filep
= vq
->kick
;
717 pollstart
= vq
->kick
= eventfp
;
721 case VHOST_SET_VRING_CALL
:
722 if (copy_from_user(&f
, argp
, sizeof f
)) {
726 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
727 if (IS_ERR(eventfp
)) {
728 r
= PTR_ERR(eventfp
);
731 if (eventfp
!= vq
->call
) {
735 vq
->call_ctx
= eventfp
?
736 eventfd_ctx_fileget(eventfp
) : NULL
;
740 case VHOST_SET_VRING_ERR
:
741 if (copy_from_user(&f
, argp
, sizeof f
)) {
745 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
746 if (IS_ERR(eventfp
)) {
747 r
= PTR_ERR(eventfp
);
750 if (eventfp
!= vq
->error
) {
754 vq
->error_ctx
= eventfp
?
755 eventfd_ctx_fileget(eventfp
) : NULL
;
763 if (pollstop
&& vq
->handle_kick
)
764 vhost_poll_stop(&vq
->poll
);
767 eventfd_ctx_put(ctx
);
771 if (pollstart
&& vq
->handle_kick
)
772 vhost_poll_start(&vq
->poll
, vq
->kick
);
774 mutex_unlock(&vq
->mutex
);
776 if (pollstop
&& vq
->handle_kick
)
777 vhost_poll_flush(&vq
->poll
);
781 /* Caller must have device mutex */
782 long vhost_dev_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, unsigned long arg
)
784 void __user
*argp
= (void __user
*)arg
;
785 struct file
*eventfp
, *filep
= NULL
;
786 struct eventfd_ctx
*ctx
= NULL
;
791 /* If you are not the owner, you can become one */
792 if (ioctl
== VHOST_SET_OWNER
) {
793 r
= vhost_dev_set_owner(d
);
797 /* You must be the owner to do anything else */
798 r
= vhost_dev_check_owner(d
);
803 case VHOST_SET_MEM_TABLE
:
804 r
= vhost_set_memory(d
, argp
);
806 case VHOST_SET_LOG_BASE
:
807 if (copy_from_user(&p
, argp
, sizeof p
)) {
811 if ((u64
)(unsigned long)p
!= p
) {
815 for (i
= 0; i
< d
->nvqs
; ++i
) {
816 struct vhost_virtqueue
*vq
;
817 void __user
*base
= (void __user
*)(unsigned long)p
;
819 mutex_lock(&vq
->mutex
);
820 /* If ring is inactive, will check when it's enabled. */
821 if (vq
->private_data
&& !vq_log_access_ok(vq
, base
))
825 mutex_unlock(&vq
->mutex
);
828 case VHOST_SET_LOG_FD
:
829 r
= get_user(fd
, (int __user
*)argp
);
832 eventfp
= fd
== -1 ? NULL
: eventfd_fget(fd
);
833 if (IS_ERR(eventfp
)) {
834 r
= PTR_ERR(eventfp
);
837 if (eventfp
!= d
->log_file
) {
840 d
->log_ctx
= eventfp
?
841 eventfd_ctx_fileget(eventfp
) : NULL
;
844 for (i
= 0; i
< d
->nvqs
; ++i
) {
845 mutex_lock(&d
->vqs
[i
].mutex
);
846 d
->vqs
[i
].log_ctx
= d
->log_ctx
;
847 mutex_unlock(&d
->vqs
[i
].mutex
);
850 eventfd_ctx_put(ctx
);
855 r
= vhost_set_vring(d
, ioctl
, argp
);
862 static const struct vhost_memory_region
*find_region(struct vhost_memory
*mem
,
863 __u64 addr
, __u32 len
)
865 struct vhost_memory_region
*reg
;
868 /* linear search is not brilliant, but we really have on the order of 6
869 * regions in practice */
870 for (i
= 0; i
< mem
->nregions
; ++i
) {
871 reg
= mem
->regions
+ i
;
872 if (reg
->guest_phys_addr
<= addr
&&
873 reg
->guest_phys_addr
+ reg
->memory_size
- 1 >= addr
)
879 /* TODO: This is really inefficient. We need something like get_user()
880 * (instruction directly accesses the data, with an exception table entry
881 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
883 static int set_bit_to_user(int nr
, void __user
*addr
)
885 unsigned long log
= (unsigned long)addr
;
888 int bit
= nr
+ (log
% PAGE_SIZE
) * 8;
891 r
= get_user_pages_fast(log
, 1, 1, &page
);
895 base
= kmap_atomic(page
, KM_USER0
);
897 kunmap_atomic(base
, KM_USER0
);
898 set_page_dirty_lock(page
);
903 static int log_write(void __user
*log_base
,
904 u64 write_address
, u64 write_length
)
906 u64 write_page
= write_address
/ VHOST_PAGE_SIZE
;
911 write_length
+= write_address
% VHOST_PAGE_SIZE
;
913 u64 base
= (u64
)(unsigned long)log_base
;
914 u64 log
= base
+ write_page
/ 8;
915 int bit
= write_page
% 8;
916 if ((u64
)(unsigned long)log
!= log
)
918 r
= set_bit_to_user(bit
, (void __user
*)(unsigned long)log
);
921 if (write_length
<= VHOST_PAGE_SIZE
)
923 write_length
-= VHOST_PAGE_SIZE
;
929 int vhost_log_write(struct vhost_virtqueue
*vq
, struct vhost_log
*log
,
930 unsigned int log_num
, u64 len
)
934 /* Make sure data written is seen before log. */
936 for (i
= 0; i
< log_num
; ++i
) {
937 u64 l
= min(log
[i
].len
, len
);
938 r
= log_write(vq
->log_base
, log
[i
].addr
, l
);
944 eventfd_signal(vq
->log_ctx
, 1);
948 /* Length written exceeds what we have stored. This is a bug. */
953 static int translate_desc(struct vhost_dev
*dev
, u64 addr
, u32 len
,
954 struct iovec iov
[], int iov_size
)
956 const struct vhost_memory_region
*reg
;
957 struct vhost_memory
*mem
;
964 mem
= rcu_dereference(dev
->memory
);
965 while ((u64
)len
> s
) {
967 if (unlikely(ret
>= iov_size
)) {
971 reg
= find_region(mem
, addr
, len
);
972 if (unlikely(!reg
)) {
977 size
= reg
->memory_size
- addr
+ reg
->guest_phys_addr
;
978 _iov
->iov_len
= min((u64
)len
, size
);
979 _iov
->iov_base
= (void __user
*)(unsigned long)
980 (reg
->userspace_addr
+ addr
- reg
->guest_phys_addr
);
990 /* Each buffer in the virtqueues is actually a chain of descriptors. This
991 * function returns the next descriptor in the chain,
992 * or -1U if we're at the end. */
993 static unsigned next_desc(struct vring_desc
*desc
)
997 /* If this descriptor says it doesn't chain, we're done. */
998 if (!(desc
->flags
& VRING_DESC_F_NEXT
))
1001 /* Check they're not leading us off end of descriptors. */
1003 /* Make sure compiler knows to grab that: we don't want it changing! */
1004 /* We will use the result as an index in an array, so most
1005 * architectures only need a compiler barrier here. */
1006 read_barrier_depends();
1011 static int get_indirect(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
1012 struct iovec iov
[], unsigned int iov_size
,
1013 unsigned int *out_num
, unsigned int *in_num
,
1014 struct vhost_log
*log
, unsigned int *log_num
,
1015 struct vring_desc
*indirect
)
1017 struct vring_desc desc
;
1018 unsigned int i
= 0, count
, found
= 0;
1022 if (unlikely(indirect
->len
% sizeof desc
)) {
1023 vq_err(vq
, "Invalid length in indirect descriptor: "
1024 "len 0x%llx not multiple of 0x%zx\n",
1025 (unsigned long long)indirect
->len
,
1030 ret
= translate_desc(dev
, indirect
->addr
, indirect
->len
, vq
->indirect
,
1032 if (unlikely(ret
< 0)) {
1033 vq_err(vq
, "Translation failure %d in indirect.\n", ret
);
1037 /* We will use the result as an address to read from, so most
1038 * architectures only need a compiler barrier here. */
1039 read_barrier_depends();
1041 count
= indirect
->len
/ sizeof desc
;
1042 /* Buffers are chained via a 16 bit next field, so
1043 * we can have at most 2^16 of these. */
1044 if (unlikely(count
> USHRT_MAX
+ 1)) {
1045 vq_err(vq
, "Indirect buffer length too big: %d\n",
1051 unsigned iov_count
= *in_num
+ *out_num
;
1052 if (unlikely(++found
> count
)) {
1053 vq_err(vq
, "Loop detected: last one at %u "
1054 "indirect size %u\n",
1058 if (unlikely(memcpy_fromiovec((unsigned char *)&desc
,
1059 vq
->indirect
, sizeof desc
))) {
1060 vq_err(vq
, "Failed indirect descriptor: idx %d, %zx\n",
1061 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
1064 if (unlikely(desc
.flags
& VRING_DESC_F_INDIRECT
)) {
1065 vq_err(vq
, "Nested indirect descriptor: idx %d, %zx\n",
1066 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
1070 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
1071 iov_size
- iov_count
);
1072 if (unlikely(ret
< 0)) {
1073 vq_err(vq
, "Translation failure %d indirect idx %d\n",
1077 /* If this is an input descriptor, increment that count. */
1078 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1080 if (unlikely(log
)) {
1081 log
[*log_num
].addr
= desc
.addr
;
1082 log
[*log_num
].len
= desc
.len
;
1086 /* If it's an output descriptor, they're all supposed
1087 * to come before any input descriptors. */
1088 if (unlikely(*in_num
)) {
1089 vq_err(vq
, "Indirect descriptor "
1090 "has out after in: idx %d\n", i
);
1095 } while ((i
= next_desc(&desc
)) != -1);
1099 /* This looks in the virtqueue and for the first available buffer, and converts
1100 * it to an iovec for convenient access. Since descriptors consist of some
1101 * number of output then some number of input descriptors, it's actually two
1102 * iovecs, but we pack them into one and note how many of each there were.
1104 * This function returns the descriptor number found, or vq->num (which is
1105 * never a valid descriptor number) if none was found. A negative code is
1106 * returned on error. */
1107 int vhost_get_vq_desc(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
1108 struct iovec iov
[], unsigned int iov_size
,
1109 unsigned int *out_num
, unsigned int *in_num
,
1110 struct vhost_log
*log
, unsigned int *log_num
)
1112 struct vring_desc desc
;
1113 unsigned int i
, head
, found
= 0;
1117 /* Check it isn't doing very strange things with descriptor numbers. */
1118 last_avail_idx
= vq
->last_avail_idx
;
1119 if (unlikely(__get_user(vq
->avail_idx
, &vq
->avail
->idx
))) {
1120 vq_err(vq
, "Failed to access avail idx at %p\n",
1125 if (unlikely((u16
)(vq
->avail_idx
- last_avail_idx
) > vq
->num
)) {
1126 vq_err(vq
, "Guest moved used index from %u to %u",
1127 last_avail_idx
, vq
->avail_idx
);
1131 /* If there's nothing new since last we looked, return invalid. */
1132 if (vq
->avail_idx
== last_avail_idx
)
1135 /* Only get avail ring entries after they have been exposed by guest. */
1138 /* Grab the next descriptor number they're advertising, and increment
1139 * the index we've seen. */
1140 if (unlikely(__get_user(head
,
1141 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]))) {
1142 vq_err(vq
, "Failed to read head: idx %d address %p\n",
1144 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]);
1148 /* If their number is silly, that's an error. */
1149 if (unlikely(head
>= vq
->num
)) {
1150 vq_err(vq
, "Guest says index %u > %u is available",
1155 /* When we start there are none of either input nor output. */
1156 *out_num
= *in_num
= 0;
1162 unsigned iov_count
= *in_num
+ *out_num
;
1163 if (unlikely(i
>= vq
->num
)) {
1164 vq_err(vq
, "Desc index is %u > %u, head = %u",
1168 if (unlikely(++found
> vq
->num
)) {
1169 vq_err(vq
, "Loop detected: last one at %u "
1170 "vq size %u head %u\n",
1174 ret
= __copy_from_user(&desc
, vq
->desc
+ i
, sizeof desc
);
1175 if (unlikely(ret
)) {
1176 vq_err(vq
, "Failed to get descriptor: idx %d addr %p\n",
1180 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1181 ret
= get_indirect(dev
, vq
, iov
, iov_size
,
1183 log
, log_num
, &desc
);
1184 if (unlikely(ret
< 0)) {
1185 vq_err(vq
, "Failure detected "
1186 "in indirect descriptor at idx %d\n", i
);
1192 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
1193 iov_size
- iov_count
);
1194 if (unlikely(ret
< 0)) {
1195 vq_err(vq
, "Translation failure %d descriptor idx %d\n",
1199 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1200 /* If this is an input descriptor,
1201 * increment that count. */
1203 if (unlikely(log
)) {
1204 log
[*log_num
].addr
= desc
.addr
;
1205 log
[*log_num
].len
= desc
.len
;
1209 /* If it's an output descriptor, they're all supposed
1210 * to come before any input descriptors. */
1211 if (unlikely(*in_num
)) {
1212 vq_err(vq
, "Descriptor has out after in: "
1218 } while ((i
= next_desc(&desc
)) != -1);
1220 /* On success, increment avail index. */
1221 vq
->last_avail_idx
++;
1225 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
1226 void vhost_discard_vq_desc(struct vhost_virtqueue
*vq
, int n
)
1228 vq
->last_avail_idx
-= n
;
1231 /* After we've used one of their buffers, we tell them about it. We'll then
1232 * want to notify the guest, using eventfd. */
1233 int vhost_add_used(struct vhost_virtqueue
*vq
, unsigned int head
, int len
)
1235 struct vring_used_elem __user
*used
;
1237 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1238 * next entry in that used ring. */
1239 used
= &vq
->used
->ring
[vq
->last_used_idx
% vq
->num
];
1240 if (__put_user(head
, &used
->id
)) {
1241 vq_err(vq
, "Failed to write used id");
1244 if (__put_user(len
, &used
->len
)) {
1245 vq_err(vq
, "Failed to write used len");
1248 /* Make sure buffer is written before we update index. */
1250 if (__put_user(vq
->last_used_idx
+ 1, &vq
->used
->idx
)) {
1251 vq_err(vq
, "Failed to increment used idx");
1254 if (unlikely(vq
->log_used
)) {
1255 /* Make sure data is seen before log. */
1257 /* Log used ring entry write. */
1258 log_write(vq
->log_base
,
1260 ((void __user
*)used
- (void __user
*)vq
->used
),
1262 /* Log used index update. */
1263 log_write(vq
->log_base
,
1264 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1265 sizeof vq
->used
->idx
);
1267 eventfd_signal(vq
->log_ctx
, 1);
1269 vq
->last_used_idx
++;
1273 static int __vhost_add_used_n(struct vhost_virtqueue
*vq
,
1274 struct vring_used_elem
*heads
,
1277 struct vring_used_elem __user
*used
;
1280 start
= vq
->last_used_idx
% vq
->num
;
1281 used
= vq
->used
->ring
+ start
;
1282 if (__copy_to_user(used
, heads
, count
* sizeof *used
)) {
1283 vq_err(vq
, "Failed to write used");
1286 if (unlikely(vq
->log_used
)) {
1287 /* Make sure data is seen before log. */
1289 /* Log used ring entry write. */
1290 log_write(vq
->log_base
,
1292 ((void __user
*)used
- (void __user
*)vq
->used
),
1293 count
* sizeof *used
);
1295 vq
->last_used_idx
+= count
;
1299 /* After we've used one of their buffers, we tell them about it. We'll then
1300 * want to notify the guest, using eventfd. */
1301 int vhost_add_used_n(struct vhost_virtqueue
*vq
, struct vring_used_elem
*heads
,
1306 start
= vq
->last_used_idx
% vq
->num
;
1307 n
= vq
->num
- start
;
1309 r
= __vhost_add_used_n(vq
, heads
, n
);
1315 r
= __vhost_add_used_n(vq
, heads
, count
);
1317 /* Make sure buffer is written before we update index. */
1319 if (put_user(vq
->last_used_idx
, &vq
->used
->idx
)) {
1320 vq_err(vq
, "Failed to increment used idx");
1323 if (unlikely(vq
->log_used
)) {
1324 /* Log used index update. */
1325 log_write(vq
->log_base
,
1326 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1327 sizeof vq
->used
->idx
);
1329 eventfd_signal(vq
->log_ctx
, 1);
1334 /* This actually signals the guest, using eventfd. */
1335 void vhost_signal(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
1339 /* Flush out used index updates. This is paired
1340 * with the barrier that the Guest executes when enabling
1344 if (__get_user(flags
, &vq
->avail
->flags
)) {
1345 vq_err(vq
, "Failed to get flags");
1349 /* If they don't want an interrupt, don't signal, unless empty. */
1350 if ((flags
& VRING_AVAIL_F_NO_INTERRUPT
) &&
1351 (vq
->avail_idx
!= vq
->last_avail_idx
||
1352 !vhost_has_feature(dev
, VIRTIO_F_NOTIFY_ON_EMPTY
)))
1355 /* Signal the Guest tell them we used something up. */
1357 eventfd_signal(vq
->call_ctx
, 1);
1360 /* And here's the combo meal deal. Supersize me! */
1361 void vhost_add_used_and_signal(struct vhost_dev
*dev
,
1362 struct vhost_virtqueue
*vq
,
1363 unsigned int head
, int len
)
1365 vhost_add_used(vq
, head
, len
);
1366 vhost_signal(dev
, vq
);
1369 /* multi-buffer version of vhost_add_used_and_signal */
1370 void vhost_add_used_and_signal_n(struct vhost_dev
*dev
,
1371 struct vhost_virtqueue
*vq
,
1372 struct vring_used_elem
*heads
, unsigned count
)
1374 vhost_add_used_n(vq
, heads
, count
);
1375 vhost_signal(dev
, vq
);
1378 /* OK, now we need to know about added descriptors. */
1379 bool vhost_enable_notify(struct vhost_virtqueue
*vq
)
1384 if (!(vq
->used_flags
& VRING_USED_F_NO_NOTIFY
))
1386 vq
->used_flags
&= ~VRING_USED_F_NO_NOTIFY
;
1387 r
= put_user(vq
->used_flags
, &vq
->used
->flags
);
1389 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1390 &vq
->used
->flags
, r
);
1393 /* They could have slipped one in as we were doing that: make
1394 * sure it's written, then check again. */
1396 r
= __get_user(avail_idx
, &vq
->avail
->idx
);
1398 vq_err(vq
, "Failed to check avail idx at %p: %d\n",
1399 &vq
->avail
->idx
, r
);
1403 return avail_idx
!= vq
->avail_idx
;
1406 /* We don't need to be notified again. */
1407 void vhost_disable_notify(struct vhost_virtqueue
*vq
)
1411 if (vq
->used_flags
& VRING_USED_F_NO_NOTIFY
)
1413 vq
->used_flags
|= VRING_USED_F_NO_NOTIFY
;
1414 r
= put_user(vq
->used_flags
, &vq
->used
->flags
);
1416 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1417 &vq
->used
->flags
, r
);