1 /* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
4 * Author: Michael S. Tsirkin <mst@redhat.com>
6 * Inspiration, some code, and most witty comments come from
7 * Documentation/lguest/lguest.c, by Rusty Russell
9 * This work is licensed under the terms of the GNU GPL, version 2.
11 * Generic code for virtio server in host kernel.
14 #include <linux/eventfd.h>
15 #include <linux/vhost.h>
16 #include <linux/virtio_net.h>
18 #include <linux/miscdevice.h>
19 #include <linux/mutex.h>
20 #include <linux/rcupdate.h>
21 #include <linux/poll.h>
22 #include <linux/file.h>
23 #include <linux/highmem.h>
24 #include <linux/slab.h>
25 #include <linux/kthread.h>
26 #include <linux/cgroup.h>
28 #include <linux/net.h>
29 #include <linux/if_packet.h>
30 #include <linux/if_arp.h>
37 VHOST_MEMORY_MAX_NREGIONS
= 64,
38 VHOST_MEMORY_F_LOG
= 0x1,
41 static void vhost_poll_func(struct file
*file
, wait_queue_head_t
*wqh
,
44 struct vhost_poll
*poll
;
45 poll
= container_of(pt
, struct vhost_poll
, table
);
48 add_wait_queue(wqh
, &poll
->wait
);
51 static int vhost_poll_wakeup(wait_queue_t
*wait
, unsigned mode
, int sync
,
54 struct vhost_poll
*poll
= container_of(wait
, struct vhost_poll
, wait
);
56 if (!((unsigned long)key
& poll
->mask
))
59 vhost_poll_queue(poll
);
63 static void vhost_work_init(struct vhost_work
*work
, vhost_work_fn_t fn
)
65 INIT_LIST_HEAD(&work
->node
);
67 init_waitqueue_head(&work
->done
);
69 work
->queue_seq
= work
->done_seq
= 0;
72 /* Init poll structure */
73 void vhost_poll_init(struct vhost_poll
*poll
, vhost_work_fn_t fn
,
74 unsigned long mask
, struct vhost_dev
*dev
)
76 init_waitqueue_func_entry(&poll
->wait
, vhost_poll_wakeup
);
77 init_poll_funcptr(&poll
->table
, vhost_poll_func
);
81 vhost_work_init(&poll
->work
, fn
);
84 /* Start polling a file. We add ourselves to file's wait queue. The caller must
85 * keep a reference to a file until after vhost_poll_stop is called. */
86 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 void vhost_work_flush(struct vhost_dev
*dev
, struct vhost_work
*work
)
107 spin_lock_irq(&dev
->work_lock
);
108 seq
= work
->queue_seq
;
110 spin_unlock_irq(&dev
->work_lock
);
111 wait_event(work
->done
, ({
112 spin_lock_irq(&dev
->work_lock
);
113 left
= seq
- work
->done_seq
<= 0;
114 spin_unlock_irq(&dev
->work_lock
);
117 spin_lock_irq(&dev
->work_lock
);
118 flushing
= --work
->flushing
;
119 spin_unlock_irq(&dev
->work_lock
);
120 BUG_ON(flushing
< 0);
123 /* Flush any work that has been scheduled. When calling this, don't hold any
124 * locks that are also used by the callback. */
125 void vhost_poll_flush(struct vhost_poll
*poll
)
127 vhost_work_flush(poll
->dev
, &poll
->work
);
130 static inline void vhost_work_queue(struct vhost_dev
*dev
,
131 struct vhost_work
*work
)
135 spin_lock_irqsave(&dev
->work_lock
, flags
);
136 if (list_empty(&work
->node
)) {
137 list_add_tail(&work
->node
, &dev
->work_list
);
139 wake_up_process(dev
->worker
);
141 spin_unlock_irqrestore(&dev
->work_lock
, flags
);
144 void vhost_poll_queue(struct vhost_poll
*poll
)
146 vhost_work_queue(poll
->dev
, &poll
->work
);
149 static void vhost_vq_reset(struct vhost_dev
*dev
,
150 struct vhost_virtqueue
*vq
)
156 vq
->last_avail_idx
= 0;
158 vq
->last_used_idx
= 0;
161 vq
->log_used
= false;
162 vq
->log_addr
= -1ull;
165 vq
->private_data
= NULL
;
167 vq
->error_ctx
= NULL
;
175 static int vhost_worker(void *data
)
177 struct vhost_dev
*dev
= data
;
178 struct vhost_work
*work
= NULL
;
179 unsigned uninitialized_var(seq
);
182 /* mb paired w/ kthread_stop */
183 set_current_state(TASK_INTERRUPTIBLE
);
185 spin_lock_irq(&dev
->work_lock
);
187 work
->done_seq
= seq
;
189 wake_up_all(&work
->done
);
192 if (kthread_should_stop()) {
193 spin_unlock_irq(&dev
->work_lock
);
194 __set_current_state(TASK_RUNNING
);
197 if (!list_empty(&dev
->work_list
)) {
198 work
= list_first_entry(&dev
->work_list
,
199 struct vhost_work
, node
);
200 list_del_init(&work
->node
);
201 seq
= work
->queue_seq
;
204 spin_unlock_irq(&dev
->work_lock
);
207 __set_current_state(TASK_RUNNING
);
215 long vhost_dev_init(struct vhost_dev
*dev
,
216 struct vhost_virtqueue
*vqs
, int nvqs
)
222 mutex_init(&dev
->mutex
);
224 dev
->log_file
= NULL
;
227 spin_lock_init(&dev
->work_lock
);
228 INIT_LIST_HEAD(&dev
->work_list
);
231 for (i
= 0; i
< dev
->nvqs
; ++i
) {
232 dev
->vqs
[i
].dev
= dev
;
233 mutex_init(&dev
->vqs
[i
].mutex
);
234 vhost_vq_reset(dev
, dev
->vqs
+ i
);
235 if (dev
->vqs
[i
].handle_kick
)
236 vhost_poll_init(&dev
->vqs
[i
].poll
,
237 dev
->vqs
[i
].handle_kick
, POLLIN
, dev
);
243 /* Caller should have device mutex */
244 long vhost_dev_check_owner(struct vhost_dev
*dev
)
246 /* Are you the owner? If not, I don't think you mean to do that */
247 return dev
->mm
== current
->mm
? 0 : -EPERM
;
250 struct vhost_attach_cgroups_struct
{
251 struct vhost_work work
;
252 struct task_struct
*owner
;
256 static void vhost_attach_cgroups_work(struct vhost_work
*work
)
258 struct vhost_attach_cgroups_struct
*s
;
259 s
= container_of(work
, struct vhost_attach_cgroups_struct
, work
);
260 s
->ret
= cgroup_attach_task_all(s
->owner
, current
);
263 static int vhost_attach_cgroups(struct vhost_dev
*dev
)
265 struct vhost_attach_cgroups_struct attach
;
266 attach
.owner
= current
;
267 vhost_work_init(&attach
.work
, vhost_attach_cgroups_work
);
268 vhost_work_queue(dev
, &attach
.work
);
269 vhost_work_flush(dev
, &attach
.work
);
273 /* Caller should have device mutex */
274 static long vhost_dev_set_owner(struct vhost_dev
*dev
)
276 struct task_struct
*worker
;
278 /* Is there an owner already? */
283 /* No owner, become one */
284 dev
->mm
= get_task_mm(current
);
285 worker
= kthread_create(vhost_worker
, dev
, "vhost-%d", current
->pid
);
286 if (IS_ERR(worker
)) {
287 err
= PTR_ERR(worker
);
291 dev
->worker
= worker
;
292 wake_up_process(worker
); /* avoid contributing to loadavg */
294 err
= vhost_attach_cgroups(dev
);
300 kthread_stop(worker
);
310 /* Caller should have device mutex */
311 long vhost_dev_reset_owner(struct vhost_dev
*dev
)
313 struct vhost_memory
*memory
;
315 /* Restore memory to default empty mapping. */
316 memory
= kmalloc(offsetof(struct vhost_memory
, regions
), GFP_KERNEL
);
320 vhost_dev_cleanup(dev
);
322 memory
->nregions
= 0;
323 dev
->memory
= memory
;
327 /* Caller should have device mutex */
328 void vhost_dev_cleanup(struct vhost_dev
*dev
)
331 for (i
= 0; i
< dev
->nvqs
; ++i
) {
332 if (dev
->vqs
[i
].kick
&& dev
->vqs
[i
].handle_kick
) {
333 vhost_poll_stop(&dev
->vqs
[i
].poll
);
334 vhost_poll_flush(&dev
->vqs
[i
].poll
);
336 if (dev
->vqs
[i
].error_ctx
)
337 eventfd_ctx_put(dev
->vqs
[i
].error_ctx
);
338 if (dev
->vqs
[i
].error
)
339 fput(dev
->vqs
[i
].error
);
340 if (dev
->vqs
[i
].kick
)
341 fput(dev
->vqs
[i
].kick
);
342 if (dev
->vqs
[i
].call_ctx
)
343 eventfd_ctx_put(dev
->vqs
[i
].call_ctx
);
344 if (dev
->vqs
[i
].call
)
345 fput(dev
->vqs
[i
].call
);
346 vhost_vq_reset(dev
, dev
->vqs
+ i
);
349 eventfd_ctx_put(dev
->log_ctx
);
353 dev
->log_file
= NULL
;
354 /* No one will access memory at this point */
361 WARN_ON(!list_empty(&dev
->work_list
));
363 kthread_stop(dev
->worker
);
368 static int log_access_ok(void __user
*log_base
, u64 addr
, unsigned long sz
)
370 u64 a
= addr
/ VHOST_PAGE_SIZE
/ 8;
371 /* Make sure 64 bit math will not overflow. */
372 if (a
> ULONG_MAX
- (unsigned long)log_base
||
373 a
+ (unsigned long)log_base
> ULONG_MAX
)
376 return access_ok(VERIFY_WRITE
, log_base
+ a
,
377 (sz
+ VHOST_PAGE_SIZE
* 8 - 1) / VHOST_PAGE_SIZE
/ 8);
380 /* Caller should have vq mutex and device mutex. */
381 static int vq_memory_access_ok(void __user
*log_base
, struct vhost_memory
*mem
,
389 for (i
= 0; i
< mem
->nregions
; ++i
) {
390 struct vhost_memory_region
*m
= mem
->regions
+ i
;
391 unsigned long a
= m
->userspace_addr
;
392 if (m
->memory_size
> ULONG_MAX
)
394 else if (!access_ok(VERIFY_WRITE
, (void __user
*)a
,
397 else if (log_all
&& !log_access_ok(log_base
,
405 /* Can we switch to this memory table? */
406 /* Caller should have device mutex but not vq mutex */
407 static int memory_access_ok(struct vhost_dev
*d
, struct vhost_memory
*mem
,
411 for (i
= 0; i
< d
->nvqs
; ++i
) {
413 mutex_lock(&d
->vqs
[i
].mutex
);
414 /* If ring is inactive, will check when it's enabled. */
415 if (d
->vqs
[i
].private_data
)
416 ok
= vq_memory_access_ok(d
->vqs
[i
].log_base
, mem
,
420 mutex_unlock(&d
->vqs
[i
].mutex
);
427 static int vq_access_ok(unsigned int num
,
428 struct vring_desc __user
*desc
,
429 struct vring_avail __user
*avail
,
430 struct vring_used __user
*used
)
432 return access_ok(VERIFY_READ
, desc
, num
* sizeof *desc
) &&
433 access_ok(VERIFY_READ
, avail
,
434 sizeof *avail
+ num
* sizeof *avail
->ring
) &&
435 access_ok(VERIFY_WRITE
, used
,
436 sizeof *used
+ num
* sizeof *used
->ring
);
439 /* Can we log writes? */
440 /* Caller should have device mutex but not vq mutex */
441 int vhost_log_access_ok(struct vhost_dev
*dev
)
443 return memory_access_ok(dev
, dev
->memory
, 1);
446 /* Verify access for write logging. */
447 /* Caller should have vq mutex and device mutex */
448 static int vq_log_access_ok(struct vhost_virtqueue
*vq
, void __user
*log_base
)
450 return vq_memory_access_ok(log_base
, vq
->dev
->memory
,
451 vhost_has_feature(vq
->dev
, VHOST_F_LOG_ALL
)) &&
452 (!vq
->log_used
|| log_access_ok(log_base
, vq
->log_addr
,
454 vq
->num
* sizeof *vq
->used
->ring
));
457 /* Can we start vq? */
458 /* Caller should have vq mutex and device mutex */
459 int vhost_vq_access_ok(struct vhost_virtqueue
*vq
)
461 return vq_access_ok(vq
->num
, vq
->desc
, vq
->avail
, vq
->used
) &&
462 vq_log_access_ok(vq
, vq
->log_base
);
465 static long vhost_set_memory(struct vhost_dev
*d
, struct vhost_memory __user
*m
)
467 struct vhost_memory mem
, *newmem
, *oldmem
;
468 unsigned long size
= offsetof(struct vhost_memory
, regions
);
469 if (copy_from_user(&mem
, m
, size
))
473 if (mem
.nregions
> VHOST_MEMORY_MAX_NREGIONS
)
475 newmem
= kmalloc(size
+ mem
.nregions
* sizeof *m
->regions
, GFP_KERNEL
);
479 memcpy(newmem
, &mem
, size
);
480 if (copy_from_user(newmem
->regions
, m
->regions
,
481 mem
.nregions
* sizeof *m
->regions
)) {
486 if (!memory_access_ok(d
, newmem
, vhost_has_feature(d
, VHOST_F_LOG_ALL
))) {
491 rcu_assign_pointer(d
->memory
, newmem
);
497 static int init_used(struct vhost_virtqueue
*vq
,
498 struct vring_used __user
*used
)
500 int r
= put_user(vq
->used_flags
, &used
->flags
);
503 return get_user(vq
->last_used_idx
, &used
->idx
);
506 static long vhost_set_vring(struct vhost_dev
*d
, int ioctl
, void __user
*argp
)
508 struct file
*eventfp
, *filep
= NULL
,
509 *pollstart
= NULL
, *pollstop
= NULL
;
510 struct eventfd_ctx
*ctx
= NULL
;
511 u32 __user
*idxp
= argp
;
512 struct vhost_virtqueue
*vq
;
513 struct vhost_vring_state s
;
514 struct vhost_vring_file f
;
515 struct vhost_vring_addr a
;
519 r
= get_user(idx
, idxp
);
527 mutex_lock(&vq
->mutex
);
530 case VHOST_SET_VRING_NUM
:
531 /* Resizing ring with an active backend?
532 * You don't want to do that. */
533 if (vq
->private_data
) {
537 if (copy_from_user(&s
, argp
, sizeof s
)) {
541 if (!s
.num
|| s
.num
> 0xffff || (s
.num
& (s
.num
- 1))) {
547 case VHOST_SET_VRING_BASE
:
548 /* Moving base with an active backend?
549 * You don't want to do that. */
550 if (vq
->private_data
) {
554 if (copy_from_user(&s
, argp
, sizeof s
)) {
558 if (s
.num
> 0xffff) {
562 vq
->last_avail_idx
= s
.num
;
563 /* Forget the cached index value. */
564 vq
->avail_idx
= vq
->last_avail_idx
;
566 case VHOST_GET_VRING_BASE
:
568 s
.num
= vq
->last_avail_idx
;
569 if (copy_to_user(argp
, &s
, sizeof s
))
572 case VHOST_SET_VRING_ADDR
:
573 if (copy_from_user(&a
, argp
, sizeof a
)) {
577 if (a
.flags
& ~(0x1 << VHOST_VRING_F_LOG
)) {
581 /* For 32bit, verify that the top 32bits of the user
582 data are set to zero. */
583 if ((u64
)(unsigned long)a
.desc_user_addr
!= a
.desc_user_addr
||
584 (u64
)(unsigned long)a
.used_user_addr
!= a
.used_user_addr
||
585 (u64
)(unsigned long)a
.avail_user_addr
!= a
.avail_user_addr
) {
589 if ((a
.avail_user_addr
& (sizeof *vq
->avail
->ring
- 1)) ||
590 (a
.used_user_addr
& (sizeof *vq
->used
->ring
- 1)) ||
591 (a
.log_guest_addr
& (sizeof *vq
->used
->ring
- 1))) {
596 /* We only verify access here if backend is configured.
597 * If it is not, we don't as size might not have been setup.
598 * We will verify when backend is configured. */
599 if (vq
->private_data
) {
600 if (!vq_access_ok(vq
->num
,
601 (void __user
*)(unsigned long)a
.desc_user_addr
,
602 (void __user
*)(unsigned long)a
.avail_user_addr
,
603 (void __user
*)(unsigned long)a
.used_user_addr
)) {
608 /* Also validate log access for used ring if enabled. */
609 if ((a
.flags
& (0x1 << VHOST_VRING_F_LOG
)) &&
610 !log_access_ok(vq
->log_base
, a
.log_guest_addr
,
612 vq
->num
* sizeof *vq
->used
->ring
)) {
618 r
= init_used(vq
, (struct vring_used __user
*)(unsigned long)
622 vq
->log_used
= !!(a
.flags
& (0x1 << VHOST_VRING_F_LOG
));
623 vq
->desc
= (void __user
*)(unsigned long)a
.desc_user_addr
;
624 vq
->avail
= (void __user
*)(unsigned long)a
.avail_user_addr
;
625 vq
->log_addr
= a
.log_guest_addr
;
626 vq
->used
= (void __user
*)(unsigned long)a
.used_user_addr
;
628 case VHOST_SET_VRING_KICK
:
629 if (copy_from_user(&f
, argp
, sizeof f
)) {
633 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
634 if (IS_ERR(eventfp
)) {
635 r
= PTR_ERR(eventfp
);
638 if (eventfp
!= vq
->kick
) {
639 pollstop
= filep
= vq
->kick
;
640 pollstart
= vq
->kick
= eventfp
;
644 case VHOST_SET_VRING_CALL
:
645 if (copy_from_user(&f
, argp
, sizeof f
)) {
649 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
650 if (IS_ERR(eventfp
)) {
651 r
= PTR_ERR(eventfp
);
654 if (eventfp
!= vq
->call
) {
658 vq
->call_ctx
= eventfp
?
659 eventfd_ctx_fileget(eventfp
) : NULL
;
663 case VHOST_SET_VRING_ERR
:
664 if (copy_from_user(&f
, argp
, sizeof f
)) {
668 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
669 if (IS_ERR(eventfp
)) {
670 r
= PTR_ERR(eventfp
);
673 if (eventfp
!= vq
->error
) {
677 vq
->error_ctx
= eventfp
?
678 eventfd_ctx_fileget(eventfp
) : NULL
;
686 if (pollstop
&& vq
->handle_kick
)
687 vhost_poll_stop(&vq
->poll
);
690 eventfd_ctx_put(ctx
);
694 if (pollstart
&& vq
->handle_kick
)
695 vhost_poll_start(&vq
->poll
, vq
->kick
);
697 mutex_unlock(&vq
->mutex
);
699 if (pollstop
&& vq
->handle_kick
)
700 vhost_poll_flush(&vq
->poll
);
704 /* Caller must have device mutex */
705 long vhost_dev_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, unsigned long arg
)
707 void __user
*argp
= (void __user
*)arg
;
708 struct file
*eventfp
, *filep
= NULL
;
709 struct eventfd_ctx
*ctx
= NULL
;
714 /* If you are not the owner, you can become one */
715 if (ioctl
== VHOST_SET_OWNER
) {
716 r
= vhost_dev_set_owner(d
);
720 /* You must be the owner to do anything else */
721 r
= vhost_dev_check_owner(d
);
726 case VHOST_SET_MEM_TABLE
:
727 r
= vhost_set_memory(d
, argp
);
729 case VHOST_SET_LOG_BASE
:
730 if (copy_from_user(&p
, argp
, sizeof p
)) {
734 if ((u64
)(unsigned long)p
!= p
) {
738 for (i
= 0; i
< d
->nvqs
; ++i
) {
739 struct vhost_virtqueue
*vq
;
740 void __user
*base
= (void __user
*)(unsigned long)p
;
742 mutex_lock(&vq
->mutex
);
743 /* If ring is inactive, will check when it's enabled. */
744 if (vq
->private_data
&& !vq_log_access_ok(vq
, base
))
748 mutex_unlock(&vq
->mutex
);
751 case VHOST_SET_LOG_FD
:
752 r
= get_user(fd
, (int __user
*)argp
);
755 eventfp
= fd
== -1 ? NULL
: eventfd_fget(fd
);
756 if (IS_ERR(eventfp
)) {
757 r
= PTR_ERR(eventfp
);
760 if (eventfp
!= d
->log_file
) {
763 d
->log_ctx
= eventfp
?
764 eventfd_ctx_fileget(eventfp
) : NULL
;
767 for (i
= 0; i
< d
->nvqs
; ++i
) {
768 mutex_lock(&d
->vqs
[i
].mutex
);
769 d
->vqs
[i
].log_ctx
= d
->log_ctx
;
770 mutex_unlock(&d
->vqs
[i
].mutex
);
773 eventfd_ctx_put(ctx
);
778 r
= vhost_set_vring(d
, ioctl
, argp
);
785 static const struct vhost_memory_region
*find_region(struct vhost_memory
*mem
,
786 __u64 addr
, __u32 len
)
788 struct vhost_memory_region
*reg
;
790 /* linear search is not brilliant, but we really have on the order of 6
791 * regions in practice */
792 for (i
= 0; i
< mem
->nregions
; ++i
) {
793 reg
= mem
->regions
+ i
;
794 if (reg
->guest_phys_addr
<= addr
&&
795 reg
->guest_phys_addr
+ reg
->memory_size
- 1 >= addr
)
801 /* TODO: This is really inefficient. We need something like get_user()
802 * (instruction directly accesses the data, with an exception table entry
803 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
805 static int set_bit_to_user(int nr
, void __user
*addr
)
807 unsigned long log
= (unsigned long)addr
;
810 int bit
= nr
+ (log
% PAGE_SIZE
) * 8;
812 r
= get_user_pages_fast(log
, 1, 1, &page
);
816 base
= kmap_atomic(page
, KM_USER0
);
818 kunmap_atomic(base
, KM_USER0
);
819 set_page_dirty_lock(page
);
824 static int log_write(void __user
*log_base
,
825 u64 write_address
, u64 write_length
)
830 write_address
/= VHOST_PAGE_SIZE
;
832 u64 base
= (u64
)(unsigned long)log_base
;
833 u64 log
= base
+ write_address
/ 8;
834 int bit
= write_address
% 8;
835 if ((u64
)(unsigned long)log
!= log
)
837 r
= set_bit_to_user(bit
, (void __user
*)(unsigned long)log
);
840 if (write_length
<= VHOST_PAGE_SIZE
)
842 write_length
-= VHOST_PAGE_SIZE
;
843 write_address
+= VHOST_PAGE_SIZE
;
848 int vhost_log_write(struct vhost_virtqueue
*vq
, struct vhost_log
*log
,
849 unsigned int log_num
, u64 len
)
853 /* Make sure data written is seen before log. */
855 for (i
= 0; i
< log_num
; ++i
) {
856 u64 l
= min(log
[i
].len
, len
);
857 r
= log_write(vq
->log_base
, log
[i
].addr
, l
);
863 eventfd_signal(vq
->log_ctx
, 1);
867 /* Length written exceeds what we have stored. This is a bug. */
872 static int translate_desc(struct vhost_dev
*dev
, u64 addr
, u32 len
,
873 struct iovec iov
[], int iov_size
)
875 const struct vhost_memory_region
*reg
;
876 struct vhost_memory
*mem
;
883 mem
= rcu_dereference(dev
->memory
);
884 while ((u64
)len
> s
) {
886 if (unlikely(ret
>= iov_size
)) {
890 reg
= find_region(mem
, addr
, len
);
891 if (unlikely(!reg
)) {
896 size
= reg
->memory_size
- addr
+ reg
->guest_phys_addr
;
897 _iov
->iov_len
= min((u64
)len
, size
);
898 _iov
->iov_base
= (void __user
*)(unsigned long)
899 (reg
->userspace_addr
+ addr
- reg
->guest_phys_addr
);
909 /* Each buffer in the virtqueues is actually a chain of descriptors. This
910 * function returns the next descriptor in the chain,
911 * or -1U if we're at the end. */
912 static unsigned next_desc(struct vring_desc
*desc
)
916 /* If this descriptor says it doesn't chain, we're done. */
917 if (!(desc
->flags
& VRING_DESC_F_NEXT
))
920 /* Check they're not leading us off end of descriptors. */
922 /* Make sure compiler knows to grab that: we don't want it changing! */
923 /* We will use the result as an index in an array, so most
924 * architectures only need a compiler barrier here. */
925 read_barrier_depends();
930 static int get_indirect(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
931 struct iovec iov
[], unsigned int iov_size
,
932 unsigned int *out_num
, unsigned int *in_num
,
933 struct vhost_log
*log
, unsigned int *log_num
,
934 struct vring_desc
*indirect
)
936 struct vring_desc desc
;
937 unsigned int i
= 0, count
, found
= 0;
941 if (unlikely(indirect
->len
% sizeof desc
)) {
942 vq_err(vq
, "Invalid length in indirect descriptor: "
943 "len 0x%llx not multiple of 0x%zx\n",
944 (unsigned long long)indirect
->len
,
949 ret
= translate_desc(dev
, indirect
->addr
, indirect
->len
, vq
->indirect
,
950 ARRAY_SIZE(vq
->indirect
));
951 if (unlikely(ret
< 0)) {
952 vq_err(vq
, "Translation failure %d in indirect.\n", ret
);
956 /* We will use the result as an address to read from, so most
957 * architectures only need a compiler barrier here. */
958 read_barrier_depends();
960 count
= indirect
->len
/ sizeof desc
;
961 /* Buffers are chained via a 16 bit next field, so
962 * we can have at most 2^16 of these. */
963 if (unlikely(count
> USHRT_MAX
+ 1)) {
964 vq_err(vq
, "Indirect buffer length too big: %d\n",
970 unsigned iov_count
= *in_num
+ *out_num
;
971 if (unlikely(++found
> count
)) {
972 vq_err(vq
, "Loop detected: last one at %u "
973 "indirect size %u\n",
977 if (unlikely(memcpy_fromiovec((unsigned char *)&desc
, vq
->indirect
,
979 vq_err(vq
, "Failed indirect descriptor: idx %d, %zx\n",
980 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
983 if (unlikely(desc
.flags
& VRING_DESC_F_INDIRECT
)) {
984 vq_err(vq
, "Nested indirect descriptor: idx %d, %zx\n",
985 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
989 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
990 iov_size
- iov_count
);
991 if (unlikely(ret
< 0)) {
992 vq_err(vq
, "Translation failure %d indirect idx %d\n",
996 /* If this is an input descriptor, increment that count. */
997 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1000 log
[*log_num
].addr
= desc
.addr
;
1001 log
[*log_num
].len
= desc
.len
;
1005 /* If it's an output descriptor, they're all supposed
1006 * to come before any input descriptors. */
1007 if (unlikely(*in_num
)) {
1008 vq_err(vq
, "Indirect descriptor "
1009 "has out after in: idx %d\n", i
);
1014 } while ((i
= next_desc(&desc
)) != -1);
1018 /* This looks in the virtqueue and for the first available buffer, and converts
1019 * it to an iovec for convenient access. Since descriptors consist of some
1020 * number of output then some number of input descriptors, it's actually two
1021 * iovecs, but we pack them into one and note how many of each there were.
1023 * This function returns the descriptor number found, or vq->num (which is
1024 * never a valid descriptor number) if none was found. A negative code is
1025 * returned on error. */
1026 int vhost_get_vq_desc(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
1027 struct iovec iov
[], unsigned int iov_size
,
1028 unsigned int *out_num
, unsigned int *in_num
,
1029 struct vhost_log
*log
, unsigned int *log_num
)
1031 struct vring_desc desc
;
1032 unsigned int i
, head
, found
= 0;
1036 /* Check it isn't doing very strange things with descriptor numbers. */
1037 last_avail_idx
= vq
->last_avail_idx
;
1038 if (unlikely(get_user(vq
->avail_idx
, &vq
->avail
->idx
))) {
1039 vq_err(vq
, "Failed to access avail idx at %p\n",
1044 if (unlikely((u16
)(vq
->avail_idx
- last_avail_idx
) > vq
->num
)) {
1045 vq_err(vq
, "Guest moved used index from %u to %u",
1046 last_avail_idx
, vq
->avail_idx
);
1050 /* If there's nothing new since last we looked, return invalid. */
1051 if (vq
->avail_idx
== last_avail_idx
)
1054 /* Only get avail ring entries after they have been exposed by guest. */
1057 /* Grab the next descriptor number they're advertising, and increment
1058 * the index we've seen. */
1059 if (unlikely(get_user(head
,
1060 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]))) {
1061 vq_err(vq
, "Failed to read head: idx %d address %p\n",
1063 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]);
1067 /* If their number is silly, that's an error. */
1068 if (unlikely(head
>= vq
->num
)) {
1069 vq_err(vq
, "Guest says index %u > %u is available",
1074 /* When we start there are none of either input nor output. */
1075 *out_num
= *in_num
= 0;
1081 unsigned iov_count
= *in_num
+ *out_num
;
1082 if (unlikely(i
>= vq
->num
)) {
1083 vq_err(vq
, "Desc index is %u > %u, head = %u",
1087 if (unlikely(++found
> vq
->num
)) {
1088 vq_err(vq
, "Loop detected: last one at %u "
1089 "vq size %u head %u\n",
1093 ret
= copy_from_user(&desc
, vq
->desc
+ i
, sizeof desc
);
1094 if (unlikely(ret
)) {
1095 vq_err(vq
, "Failed to get descriptor: idx %d addr %p\n",
1099 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1100 ret
= get_indirect(dev
, vq
, iov
, iov_size
,
1102 log
, log_num
, &desc
);
1103 if (unlikely(ret
< 0)) {
1104 vq_err(vq
, "Failure detected "
1105 "in indirect descriptor at idx %d\n", i
);
1111 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
1112 iov_size
- iov_count
);
1113 if (unlikely(ret
< 0)) {
1114 vq_err(vq
, "Translation failure %d descriptor idx %d\n",
1118 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1119 /* If this is an input descriptor,
1120 * increment that count. */
1122 if (unlikely(log
)) {
1123 log
[*log_num
].addr
= desc
.addr
;
1124 log
[*log_num
].len
= desc
.len
;
1128 /* If it's an output descriptor, they're all supposed
1129 * to come before any input descriptors. */
1130 if (unlikely(*in_num
)) {
1131 vq_err(vq
, "Descriptor has out after in: "
1137 } while ((i
= next_desc(&desc
)) != -1);
1139 /* On success, increment avail index. */
1140 vq
->last_avail_idx
++;
1144 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
1145 void vhost_discard_vq_desc(struct vhost_virtqueue
*vq
, int n
)
1147 vq
->last_avail_idx
-= n
;
1150 /* After we've used one of their buffers, we tell them about it. We'll then
1151 * want to notify the guest, using eventfd. */
1152 int vhost_add_used(struct vhost_virtqueue
*vq
, unsigned int head
, int len
)
1154 struct vring_used_elem __user
*used
;
1156 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1157 * next entry in that used ring. */
1158 used
= &vq
->used
->ring
[vq
->last_used_idx
% vq
->num
];
1159 if (put_user(head
, &used
->id
)) {
1160 vq_err(vq
, "Failed to write used id");
1163 if (put_user(len
, &used
->len
)) {
1164 vq_err(vq
, "Failed to write used len");
1167 /* Make sure buffer is written before we update index. */
1169 if (put_user(vq
->last_used_idx
+ 1, &vq
->used
->idx
)) {
1170 vq_err(vq
, "Failed to increment used idx");
1173 if (unlikely(vq
->log_used
)) {
1174 /* Make sure data is seen before log. */
1176 /* Log used ring entry write. */
1177 log_write(vq
->log_base
,
1179 ((void __user
*)used
- (void __user
*)vq
->used
),
1181 /* Log used index update. */
1182 log_write(vq
->log_base
,
1183 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1184 sizeof vq
->used
->idx
);
1186 eventfd_signal(vq
->log_ctx
, 1);
1188 vq
->last_used_idx
++;
1192 static int __vhost_add_used_n(struct vhost_virtqueue
*vq
,
1193 struct vring_used_elem
*heads
,
1196 struct vring_used_elem __user
*used
;
1199 start
= vq
->last_used_idx
% vq
->num
;
1200 used
= vq
->used
->ring
+ start
;
1201 if (copy_to_user(used
, heads
, count
* sizeof *used
)) {
1202 vq_err(vq
, "Failed to write used");
1205 if (unlikely(vq
->log_used
)) {
1206 /* Make sure data is seen before log. */
1208 /* Log used ring entry write. */
1209 log_write(vq
->log_base
,
1211 ((void __user
*)used
- (void __user
*)vq
->used
),
1212 count
* sizeof *used
);
1214 vq
->last_used_idx
+= count
;
1218 /* After we've used one of their buffers, we tell them about it. We'll then
1219 * want to notify the guest, using eventfd. */
1220 int vhost_add_used_n(struct vhost_virtqueue
*vq
, struct vring_used_elem
*heads
,
1225 start
= vq
->last_used_idx
% vq
->num
;
1226 n
= vq
->num
- start
;
1228 r
= __vhost_add_used_n(vq
, heads
, n
);
1234 r
= __vhost_add_used_n(vq
, heads
, count
);
1236 /* Make sure buffer is written before we update index. */
1238 if (put_user(vq
->last_used_idx
, &vq
->used
->idx
)) {
1239 vq_err(vq
, "Failed to increment used idx");
1242 if (unlikely(vq
->log_used
)) {
1243 /* Log used index update. */
1244 log_write(vq
->log_base
,
1245 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1246 sizeof vq
->used
->idx
);
1248 eventfd_signal(vq
->log_ctx
, 1);
1253 /* This actually signals the guest, using eventfd. */
1254 void vhost_signal(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
1257 /* Flush out used index updates. This is paired
1258 * with the barrier that the Guest executes when enabling
1262 if (get_user(flags
, &vq
->avail
->flags
)) {
1263 vq_err(vq
, "Failed to get flags");
1267 /* If they don't want an interrupt, don't signal, unless empty. */
1268 if ((flags
& VRING_AVAIL_F_NO_INTERRUPT
) &&
1269 (vq
->avail_idx
!= vq
->last_avail_idx
||
1270 !vhost_has_feature(dev
, VIRTIO_F_NOTIFY_ON_EMPTY
)))
1273 /* Signal the Guest tell them we used something up. */
1275 eventfd_signal(vq
->call_ctx
, 1);
1278 /* And here's the combo meal deal. Supersize me! */
1279 void vhost_add_used_and_signal(struct vhost_dev
*dev
,
1280 struct vhost_virtqueue
*vq
,
1281 unsigned int head
, int len
)
1283 vhost_add_used(vq
, head
, len
);
1284 vhost_signal(dev
, vq
);
1287 /* multi-buffer version of vhost_add_used_and_signal */
1288 void vhost_add_used_and_signal_n(struct vhost_dev
*dev
,
1289 struct vhost_virtqueue
*vq
,
1290 struct vring_used_elem
*heads
, unsigned count
)
1292 vhost_add_used_n(vq
, heads
, count
);
1293 vhost_signal(dev
, vq
);
1296 /* OK, now we need to know about added descriptors. */
1297 bool vhost_enable_notify(struct vhost_virtqueue
*vq
)
1301 if (!(vq
->used_flags
& VRING_USED_F_NO_NOTIFY
))
1303 vq
->used_flags
&= ~VRING_USED_F_NO_NOTIFY
;
1304 r
= put_user(vq
->used_flags
, &vq
->used
->flags
);
1306 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1307 &vq
->used
->flags
, r
);
1310 /* They could have slipped one in as we were doing that: make
1311 * sure it's written, then check again. */
1313 r
= get_user(avail_idx
, &vq
->avail
->idx
);
1315 vq_err(vq
, "Failed to check avail idx at %p: %d\n",
1316 &vq
->avail
->idx
, r
);
1320 return avail_idx
!= vq
->avail_idx
;
1323 /* We don't need to be notified again. */
1324 void vhost_disable_notify(struct vhost_virtqueue
*vq
)
1327 if (vq
->used_flags
& VRING_USED_F_NO_NOTIFY
)
1329 vq
->used_flags
|= VRING_USED_F_NO_NOTIFY
;
1330 r
= put_user(vq
->used_flags
, &vq
->used
->flags
);
1332 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1333 &vq
->used
->flags
, r
);