1 /* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
4 * Author: Michael S. Tsirkin <mst@redhat.com>
6 * Inspiration, some code, and most witty comments come from
7 * Documentation/lguest/lguest.c, by Rusty Russell
9 * This work is licensed under the terms of the GNU GPL, version 2.
11 * Generic code for virtio server in host kernel.
14 #include <linux/eventfd.h>
15 #include <linux/vhost.h>
16 #include <linux/virtio_net.h>
18 #include <linux/miscdevice.h>
19 #include <linux/mutex.h>
20 #include <linux/workqueue.h>
21 #include <linux/rcupdate.h>
22 #include <linux/poll.h>
23 #include <linux/file.h>
24 #include <linux/highmem.h>
26 #include <linux/net.h>
27 #include <linux/if_packet.h>
28 #include <linux/if_arp.h>
35 VHOST_MEMORY_MAX_NREGIONS
= 64,
36 VHOST_MEMORY_F_LOG
= 0x1,
39 static struct workqueue_struct
*vhost_workqueue
;
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
;
55 poll
= container_of(wait
, struct vhost_poll
, wait
);
56 if (!((unsigned long)key
& poll
->mask
))
59 queue_work(vhost_workqueue
, &poll
->work
);
63 /* Init poll structure */
64 void vhost_poll_init(struct vhost_poll
*poll
, work_func_t func
,
67 INIT_WORK(&poll
->work
, func
);
68 init_waitqueue_func_entry(&poll
->wait
, vhost_poll_wakeup
);
69 init_poll_funcptr(&poll
->table
, vhost_poll_func
);
73 /* Start polling a file. We add ourselves to file's wait queue. The caller must
74 * keep a reference to a file until after vhost_poll_stop is called. */
75 void vhost_poll_start(struct vhost_poll
*poll
, struct file
*file
)
78 mask
= file
->f_op
->poll(file
, &poll
->table
);
80 vhost_poll_wakeup(&poll
->wait
, 0, 0, (void *)mask
);
83 /* Stop polling a file. After this function returns, it becomes safe to drop the
84 * file reference. You must also flush afterwards. */
85 void vhost_poll_stop(struct vhost_poll
*poll
)
87 remove_wait_queue(poll
->wqh
, &poll
->wait
);
90 /* Flush any work that has been scheduled. When calling this, don't hold any
91 * locks that are also used by the callback. */
92 void vhost_poll_flush(struct vhost_poll
*poll
)
94 flush_work(&poll
->work
);
97 void vhost_poll_queue(struct vhost_poll
*poll
)
99 queue_work(vhost_workqueue
, &poll
->work
);
102 static void vhost_vq_reset(struct vhost_dev
*dev
,
103 struct vhost_virtqueue
*vq
)
109 vq
->last_avail_idx
= 0;
111 vq
->last_used_idx
= 0;
114 vq
->log_used
= false;
115 vq
->log_addr
= -1ull;
117 vq
->private_data
= NULL
;
119 vq
->error_ctx
= NULL
;
127 long vhost_dev_init(struct vhost_dev
*dev
,
128 struct vhost_virtqueue
*vqs
, int nvqs
)
133 mutex_init(&dev
->mutex
);
135 dev
->log_file
= NULL
;
139 for (i
= 0; i
< dev
->nvqs
; ++i
) {
140 dev
->vqs
[i
].dev
= dev
;
141 mutex_init(&dev
->vqs
[i
].mutex
);
142 vhost_vq_reset(dev
, dev
->vqs
+ i
);
143 if (dev
->vqs
[i
].handle_kick
)
144 vhost_poll_init(&dev
->vqs
[i
].poll
,
145 dev
->vqs
[i
].handle_kick
,
151 /* Caller should have device mutex */
152 long vhost_dev_check_owner(struct vhost_dev
*dev
)
154 /* Are you the owner? If not, I don't think you mean to do that */
155 return dev
->mm
== current
->mm
? 0 : -EPERM
;
158 /* Caller should have device mutex */
159 static long vhost_dev_set_owner(struct vhost_dev
*dev
)
161 /* Is there an owner already? */
164 /* No owner, become one */
165 dev
->mm
= get_task_mm(current
);
169 /* Caller should have device mutex */
170 long vhost_dev_reset_owner(struct vhost_dev
*dev
)
172 struct vhost_memory
*memory
;
174 /* Restore memory to default empty mapping. */
175 memory
= kmalloc(offsetof(struct vhost_memory
, regions
), GFP_KERNEL
);
179 vhost_dev_cleanup(dev
);
181 memory
->nregions
= 0;
182 dev
->memory
= memory
;
186 /* Caller should have device mutex */
187 void vhost_dev_cleanup(struct vhost_dev
*dev
)
190 for (i
= 0; i
< dev
->nvqs
; ++i
) {
191 if (dev
->vqs
[i
].kick
&& dev
->vqs
[i
].handle_kick
) {
192 vhost_poll_stop(&dev
->vqs
[i
].poll
);
193 vhost_poll_flush(&dev
->vqs
[i
].poll
);
195 if (dev
->vqs
[i
].error_ctx
)
196 eventfd_ctx_put(dev
->vqs
[i
].error_ctx
);
197 if (dev
->vqs
[i
].error
)
198 fput(dev
->vqs
[i
].error
);
199 if (dev
->vqs
[i
].kick
)
200 fput(dev
->vqs
[i
].kick
);
201 if (dev
->vqs
[i
].call_ctx
)
202 eventfd_ctx_put(dev
->vqs
[i
].call_ctx
);
203 if (dev
->vqs
[i
].call
)
204 fput(dev
->vqs
[i
].call
);
205 vhost_vq_reset(dev
, dev
->vqs
+ i
);
208 eventfd_ctx_put(dev
->log_ctx
);
212 dev
->log_file
= NULL
;
213 /* No one will access memory at this point */
221 static int log_access_ok(void __user
*log_base
, u64 addr
, unsigned long sz
)
223 u64 a
= addr
/ VHOST_PAGE_SIZE
/ 8;
224 /* Make sure 64 bit math will not overflow. */
225 if (a
> ULONG_MAX
- (unsigned long)log_base
||
226 a
+ (unsigned long)log_base
> ULONG_MAX
)
229 return access_ok(VERIFY_WRITE
, log_base
+ a
,
230 (sz
+ VHOST_PAGE_SIZE
* 8 - 1) / VHOST_PAGE_SIZE
/ 8);
233 /* Caller should have vq mutex and device mutex. */
234 static int vq_memory_access_ok(void __user
*log_base
, struct vhost_memory
*mem
,
238 for (i
= 0; i
< mem
->nregions
; ++i
) {
239 struct vhost_memory_region
*m
= mem
->regions
+ i
;
240 unsigned long a
= m
->userspace_addr
;
241 if (m
->memory_size
> ULONG_MAX
)
243 else if (!access_ok(VERIFY_WRITE
, (void __user
*)a
,
246 else if (log_all
&& !log_access_ok(log_base
,
254 /* Can we switch to this memory table? */
255 /* Caller should have device mutex but not vq mutex */
256 static int memory_access_ok(struct vhost_dev
*d
, struct vhost_memory
*mem
,
260 for (i
= 0; i
< d
->nvqs
; ++i
) {
262 mutex_lock(&d
->vqs
[i
].mutex
);
263 /* If ring is inactive, will check when it's enabled. */
264 if (d
->vqs
[i
].private_data
)
265 ok
= vq_memory_access_ok(d
->vqs
[i
].log_base
, mem
,
269 mutex_unlock(&d
->vqs
[i
].mutex
);
276 static int vq_access_ok(unsigned int num
,
277 struct vring_desc __user
*desc
,
278 struct vring_avail __user
*avail
,
279 struct vring_used __user
*used
)
281 return access_ok(VERIFY_READ
, desc
, num
* sizeof *desc
) &&
282 access_ok(VERIFY_READ
, avail
,
283 sizeof *avail
+ num
* sizeof *avail
->ring
) &&
284 access_ok(VERIFY_WRITE
, used
,
285 sizeof *used
+ num
* sizeof *used
->ring
);
288 /* Can we log writes? */
289 /* Caller should have device mutex but not vq mutex */
290 int vhost_log_access_ok(struct vhost_dev
*dev
)
292 return memory_access_ok(dev
, dev
->memory
, 1);
295 /* Verify access for write logging. */
296 /* Caller should have vq mutex and device mutex */
297 static int vq_log_access_ok(struct vhost_virtqueue
*vq
, void __user
*log_base
)
299 return vq_memory_access_ok(log_base
, vq
->dev
->memory
,
300 vhost_has_feature(vq
->dev
, VHOST_F_LOG_ALL
)) &&
301 (!vq
->log_used
|| log_access_ok(log_base
, vq
->log_addr
,
303 vq
->num
* sizeof *vq
->used
->ring
));
306 /* Can we start vq? */
307 /* Caller should have vq mutex and device mutex */
308 int vhost_vq_access_ok(struct vhost_virtqueue
*vq
)
310 return vq_access_ok(vq
->num
, vq
->desc
, vq
->avail
, vq
->used
) &&
311 vq_log_access_ok(vq
, vq
->log_base
);
314 static long vhost_set_memory(struct vhost_dev
*d
, struct vhost_memory __user
*m
)
316 struct vhost_memory mem
, *newmem
, *oldmem
;
317 unsigned long size
= offsetof(struct vhost_memory
, regions
);
319 r
= copy_from_user(&mem
, m
, size
);
324 if (mem
.nregions
> VHOST_MEMORY_MAX_NREGIONS
)
326 newmem
= kmalloc(size
+ mem
.nregions
* sizeof *m
->regions
, GFP_KERNEL
);
330 memcpy(newmem
, &mem
, size
);
331 r
= copy_from_user(newmem
->regions
, m
->regions
,
332 mem
.nregions
* sizeof *m
->regions
);
338 if (!memory_access_ok(d
, newmem
, vhost_has_feature(d
, VHOST_F_LOG_ALL
)))
341 rcu_assign_pointer(d
->memory
, newmem
);
347 static int init_used(struct vhost_virtqueue
*vq
,
348 struct vring_used __user
*used
)
350 int r
= put_user(vq
->used_flags
, &used
->flags
);
353 return get_user(vq
->last_used_idx
, &used
->idx
);
356 static long vhost_set_vring(struct vhost_dev
*d
, int ioctl
, void __user
*argp
)
358 struct file
*eventfp
, *filep
= NULL
,
359 *pollstart
= NULL
, *pollstop
= NULL
;
360 struct eventfd_ctx
*ctx
= NULL
;
361 u32 __user
*idxp
= argp
;
362 struct vhost_virtqueue
*vq
;
363 struct vhost_vring_state s
;
364 struct vhost_vring_file f
;
365 struct vhost_vring_addr a
;
369 r
= get_user(idx
, idxp
);
377 mutex_lock(&vq
->mutex
);
380 case VHOST_SET_VRING_NUM
:
381 /* Resizing ring with an active backend?
382 * You don't want to do that. */
383 if (vq
->private_data
) {
387 r
= copy_from_user(&s
, argp
, sizeof s
);
390 if (!s
.num
|| s
.num
> 0xffff || (s
.num
& (s
.num
- 1))) {
396 case VHOST_SET_VRING_BASE
:
397 /* Moving base with an active backend?
398 * You don't want to do that. */
399 if (vq
->private_data
) {
403 r
= copy_from_user(&s
, argp
, sizeof s
);
406 if (s
.num
> 0xffff) {
410 vq
->last_avail_idx
= s
.num
;
411 /* Forget the cached index value. */
412 vq
->avail_idx
= vq
->last_avail_idx
;
414 case VHOST_GET_VRING_BASE
:
416 s
.num
= vq
->last_avail_idx
;
417 r
= copy_to_user(argp
, &s
, sizeof s
);
419 case VHOST_SET_VRING_ADDR
:
420 r
= copy_from_user(&a
, argp
, sizeof a
);
423 if (a
.flags
& ~(0x1 << VHOST_VRING_F_LOG
)) {
427 /* For 32bit, verify that the top 32bits of the user
428 data are set to zero. */
429 if ((u64
)(unsigned long)a
.desc_user_addr
!= a
.desc_user_addr
||
430 (u64
)(unsigned long)a
.used_user_addr
!= a
.used_user_addr
||
431 (u64
)(unsigned long)a
.avail_user_addr
!= a
.avail_user_addr
) {
435 if ((a
.avail_user_addr
& (sizeof *vq
->avail
->ring
- 1)) ||
436 (a
.used_user_addr
& (sizeof *vq
->used
->ring
- 1)) ||
437 (a
.log_guest_addr
& (sizeof *vq
->used
->ring
- 1))) {
442 /* We only verify access here if backend is configured.
443 * If it is not, we don't as size might not have been setup.
444 * We will verify when backend is configured. */
445 if (vq
->private_data
) {
446 if (!vq_access_ok(vq
->num
,
447 (void __user
*)(unsigned long)a
.desc_user_addr
,
448 (void __user
*)(unsigned long)a
.avail_user_addr
,
449 (void __user
*)(unsigned long)a
.used_user_addr
)) {
454 /* Also validate log access for used ring if enabled. */
455 if ((a
.flags
& (0x1 << VHOST_VRING_F_LOG
)) &&
456 !log_access_ok(vq
->log_base
, a
.log_guest_addr
,
458 vq
->num
* sizeof *vq
->used
->ring
)) {
464 r
= init_used(vq
, (struct vring_used __user
*)(unsigned long)
468 vq
->log_used
= !!(a
.flags
& (0x1 << VHOST_VRING_F_LOG
));
469 vq
->desc
= (void __user
*)(unsigned long)a
.desc_user_addr
;
470 vq
->avail
= (void __user
*)(unsigned long)a
.avail_user_addr
;
471 vq
->log_addr
= a
.log_guest_addr
;
472 vq
->used
= (void __user
*)(unsigned long)a
.used_user_addr
;
474 case VHOST_SET_VRING_KICK
:
475 r
= copy_from_user(&f
, argp
, sizeof f
);
478 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
479 if (IS_ERR(eventfp
)) {
480 r
= PTR_ERR(eventfp
);
483 if (eventfp
!= vq
->kick
) {
484 pollstop
= filep
= vq
->kick
;
485 pollstart
= vq
->kick
= eventfp
;
489 case VHOST_SET_VRING_CALL
:
490 r
= copy_from_user(&f
, argp
, sizeof f
);
493 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
494 if (IS_ERR(eventfp
)) {
495 r
= PTR_ERR(eventfp
);
498 if (eventfp
!= vq
->call
) {
502 vq
->call_ctx
= eventfp
?
503 eventfd_ctx_fileget(eventfp
) : NULL
;
507 case VHOST_SET_VRING_ERR
:
508 r
= copy_from_user(&f
, argp
, sizeof f
);
511 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
512 if (IS_ERR(eventfp
)) {
513 r
= PTR_ERR(eventfp
);
516 if (eventfp
!= vq
->error
) {
520 vq
->error_ctx
= eventfp
?
521 eventfd_ctx_fileget(eventfp
) : NULL
;
529 if (pollstop
&& vq
->handle_kick
)
530 vhost_poll_stop(&vq
->poll
);
533 eventfd_ctx_put(ctx
);
537 if (pollstart
&& vq
->handle_kick
)
538 vhost_poll_start(&vq
->poll
, vq
->kick
);
540 mutex_unlock(&vq
->mutex
);
542 if (pollstop
&& vq
->handle_kick
)
543 vhost_poll_flush(&vq
->poll
);
547 /* Caller must have device mutex */
548 long vhost_dev_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, unsigned long arg
)
550 void __user
*argp
= (void __user
*)arg
;
551 struct file
*eventfp
, *filep
= NULL
;
552 struct eventfd_ctx
*ctx
= NULL
;
557 /* If you are not the owner, you can become one */
558 if (ioctl
== VHOST_SET_OWNER
) {
559 r
= vhost_dev_set_owner(d
);
563 /* You must be the owner to do anything else */
564 r
= vhost_dev_check_owner(d
);
569 case VHOST_SET_MEM_TABLE
:
570 r
= vhost_set_memory(d
, argp
);
572 case VHOST_SET_LOG_BASE
:
573 r
= copy_from_user(&p
, argp
, sizeof p
);
576 if ((u64
)(unsigned long)p
!= p
) {
580 for (i
= 0; i
< d
->nvqs
; ++i
) {
581 struct vhost_virtqueue
*vq
;
582 void __user
*base
= (void __user
*)(unsigned long)p
;
584 mutex_lock(&vq
->mutex
);
585 /* If ring is inactive, will check when it's enabled. */
586 if (vq
->private_data
&& !vq_log_access_ok(vq
, base
))
590 mutex_unlock(&vq
->mutex
);
593 case VHOST_SET_LOG_FD
:
594 r
= get_user(fd
, (int __user
*)argp
);
597 eventfp
= fd
== -1 ? NULL
: eventfd_fget(fd
);
598 if (IS_ERR(eventfp
)) {
599 r
= PTR_ERR(eventfp
);
602 if (eventfp
!= d
->log_file
) {
605 d
->log_ctx
= eventfp
?
606 eventfd_ctx_fileget(eventfp
) : NULL
;
609 for (i
= 0; i
< d
->nvqs
; ++i
) {
610 mutex_lock(&d
->vqs
[i
].mutex
);
611 d
->vqs
[i
].log_ctx
= d
->log_ctx
;
612 mutex_unlock(&d
->vqs
[i
].mutex
);
615 eventfd_ctx_put(ctx
);
620 r
= vhost_set_vring(d
, ioctl
, argp
);
627 static const struct vhost_memory_region
*find_region(struct vhost_memory
*mem
,
628 __u64 addr
, __u32 len
)
630 struct vhost_memory_region
*reg
;
632 /* linear search is not brilliant, but we really have on the order of 6
633 * regions in practice */
634 for (i
= 0; i
< mem
->nregions
; ++i
) {
635 reg
= mem
->regions
+ i
;
636 if (reg
->guest_phys_addr
<= addr
&&
637 reg
->guest_phys_addr
+ reg
->memory_size
- 1 >= addr
)
643 /* TODO: This is really inefficient. We need something like get_user()
644 * (instruction directly accesses the data, with an exception table entry
645 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
647 static int set_bit_to_user(int nr
, void __user
*addr
)
649 unsigned long log
= (unsigned long)addr
;
652 int bit
= nr
+ (log
% PAGE_SIZE
) * 8;
654 r
= get_user_pages_fast(log
, 1, 1, &page
);
658 base
= kmap_atomic(page
, KM_USER0
);
660 kunmap_atomic(base
, KM_USER0
);
661 set_page_dirty_lock(page
);
666 static int log_write(void __user
*log_base
,
667 u64 write_address
, u64 write_length
)
672 write_address
/= VHOST_PAGE_SIZE
;
674 u64 base
= (u64
)(unsigned long)log_base
;
675 u64 log
= base
+ write_address
/ 8;
676 int bit
= write_address
% 8;
677 if ((u64
)(unsigned long)log
!= log
)
679 r
= set_bit_to_user(bit
, (void __user
*)(unsigned long)log
);
682 if (write_length
<= VHOST_PAGE_SIZE
)
684 write_length
-= VHOST_PAGE_SIZE
;
685 write_address
+= VHOST_PAGE_SIZE
;
690 int vhost_log_write(struct vhost_virtqueue
*vq
, struct vhost_log
*log
,
691 unsigned int log_num
, u64 len
)
695 /* Make sure data written is seen before log. */
697 for (i
= 0; i
< log_num
; ++i
) {
698 u64 l
= min(log
[i
].len
, len
);
699 r
= log_write(vq
->log_base
, log
[i
].addr
, l
);
707 eventfd_signal(vq
->log_ctx
, 1);
708 /* Length written exceeds what we have stored. This is a bug. */
713 int translate_desc(struct vhost_dev
*dev
, u64 addr
, u32 len
,
714 struct iovec iov
[], int iov_size
)
716 const struct vhost_memory_region
*reg
;
717 struct vhost_memory
*mem
;
724 mem
= rcu_dereference(dev
->memory
);
725 while ((u64
)len
> s
) {
727 if (ret
>= iov_size
) {
731 reg
= find_region(mem
, addr
, len
);
737 size
= reg
->memory_size
- addr
+ reg
->guest_phys_addr
;
738 _iov
->iov_len
= min((u64
)len
, size
);
739 _iov
->iov_base
= (void *)(unsigned long)
740 (reg
->userspace_addr
+ addr
- reg
->guest_phys_addr
);
750 /* Each buffer in the virtqueues is actually a chain of descriptors. This
751 * function returns the next descriptor in the chain,
752 * or -1U if we're at the end. */
753 static unsigned next_desc(struct vring_desc
*desc
)
757 /* If this descriptor says it doesn't chain, we're done. */
758 if (!(desc
->flags
& VRING_DESC_F_NEXT
))
761 /* Check they're not leading us off end of descriptors. */
763 /* Make sure compiler knows to grab that: we don't want it changing! */
764 /* We will use the result as an index in an array, so most
765 * architectures only need a compiler barrier here. */
766 read_barrier_depends();
771 static unsigned get_indirect(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
772 struct iovec iov
[], unsigned int iov_size
,
773 unsigned int *out_num
, unsigned int *in_num
,
774 struct vhost_log
*log
, unsigned int *log_num
,
775 struct vring_desc
*indirect
)
777 struct vring_desc desc
;
778 unsigned int i
= 0, count
, found
= 0;
782 if (indirect
->len
% sizeof desc
) {
783 vq_err(vq
, "Invalid length in indirect descriptor: "
784 "len 0x%llx not multiple of 0x%zx\n",
785 (unsigned long long)indirect
->len
,
790 ret
= translate_desc(dev
, indirect
->addr
, indirect
->len
, vq
->indirect
,
791 ARRAY_SIZE(vq
->indirect
));
793 vq_err(vq
, "Translation failure %d in indirect.\n", ret
);
797 /* We will use the result as an address to read from, so most
798 * architectures only need a compiler barrier here. */
799 read_barrier_depends();
801 count
= indirect
->len
/ sizeof desc
;
802 /* Buffers are chained via a 16 bit next field, so
803 * we can have at most 2^16 of these. */
804 if (count
> USHORT_MAX
+ 1) {
805 vq_err(vq
, "Indirect buffer length too big: %d\n",
811 unsigned iov_count
= *in_num
+ *out_num
;
812 if (++found
> count
) {
813 vq_err(vq
, "Loop detected: last one at %u "
814 "indirect size %u\n",
818 if (memcpy_fromiovec((unsigned char *)&desc
, vq
->indirect
,
820 vq_err(vq
, "Failed indirect descriptor: idx %d, %zx\n",
821 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
824 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
825 vq_err(vq
, "Nested indirect descriptor: idx %d, %zx\n",
826 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
830 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
831 iov_size
- iov_count
);
833 vq_err(vq
, "Translation failure %d indirect idx %d\n",
837 /* If this is an input descriptor, increment that count. */
838 if (desc
.flags
& VRING_DESC_F_WRITE
) {
841 log
[*log_num
].addr
= desc
.addr
;
842 log
[*log_num
].len
= desc
.len
;
846 /* If it's an output descriptor, they're all supposed
847 * to come before any input descriptors. */
849 vq_err(vq
, "Indirect descriptor "
850 "has out after in: idx %d\n", i
);
855 } while ((i
= next_desc(&desc
)) != -1);
859 /* This looks in the virtqueue and for the first available buffer, and converts
860 * it to an iovec for convenient access. Since descriptors consist of some
861 * number of output then some number of input descriptors, it's actually two
862 * iovecs, but we pack them into one and note how many of each there were.
864 * This function returns the descriptor number found, or vq->num (which
865 * is never a valid descriptor number) if none was found. */
866 unsigned vhost_get_vq_desc(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
867 struct iovec iov
[], unsigned int iov_size
,
868 unsigned int *out_num
, unsigned int *in_num
,
869 struct vhost_log
*log
, unsigned int *log_num
)
871 struct vring_desc desc
;
872 unsigned int i
, head
, found
= 0;
876 /* Check it isn't doing very strange things with descriptor numbers. */
877 last_avail_idx
= vq
->last_avail_idx
;
878 if (get_user(vq
->avail_idx
, &vq
->avail
->idx
)) {
879 vq_err(vq
, "Failed to access avail idx at %p\n",
884 if ((u16
)(vq
->avail_idx
- last_avail_idx
) > vq
->num
) {
885 vq_err(vq
, "Guest moved used index from %u to %u",
886 last_avail_idx
, vq
->avail_idx
);
890 /* If there's nothing new since last we looked, return invalid. */
891 if (vq
->avail_idx
== last_avail_idx
)
894 /* Only get avail ring entries after they have been exposed by guest. */
897 /* Grab the next descriptor number they're advertising, and increment
898 * the index we've seen. */
899 if (get_user(head
, &vq
->avail
->ring
[last_avail_idx
% vq
->num
])) {
900 vq_err(vq
, "Failed to read head: idx %d address %p\n",
902 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]);
906 /* If their number is silly, that's an error. */
907 if (head
>= vq
->num
) {
908 vq_err(vq
, "Guest says index %u > %u is available",
913 /* When we start there are none of either input nor output. */
914 *out_num
= *in_num
= 0;
920 unsigned iov_count
= *in_num
+ *out_num
;
922 vq_err(vq
, "Desc index is %u > %u, head = %u",
926 if (++found
> vq
->num
) {
927 vq_err(vq
, "Loop detected: last one at %u "
928 "vq size %u head %u\n",
932 ret
= copy_from_user(&desc
, vq
->desc
+ i
, sizeof desc
);
934 vq_err(vq
, "Failed to get descriptor: idx %d addr %p\n",
938 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
939 ret
= get_indirect(dev
, vq
, iov
, iov_size
,
941 log
, log_num
, &desc
);
943 vq_err(vq
, "Failure detected "
944 "in indirect descriptor at idx %d\n", i
);
950 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
951 iov_size
- iov_count
);
953 vq_err(vq
, "Translation failure %d descriptor idx %d\n",
957 if (desc
.flags
& VRING_DESC_F_WRITE
) {
958 /* If this is an input descriptor,
959 * increment that count. */
962 log
[*log_num
].addr
= desc
.addr
;
963 log
[*log_num
].len
= desc
.len
;
967 /* If it's an output descriptor, they're all supposed
968 * to come before any input descriptors. */
970 vq_err(vq
, "Descriptor has out after in: "
976 } while ((i
= next_desc(&desc
)) != -1);
978 /* On success, increment avail index. */
979 vq
->last_avail_idx
++;
983 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
984 void vhost_discard_vq_desc(struct vhost_virtqueue
*vq
)
986 vq
->last_avail_idx
--;
989 /* After we've used one of their buffers, we tell them about it. We'll then
990 * want to notify the guest, using eventfd. */
991 int vhost_add_used(struct vhost_virtqueue
*vq
, unsigned int head
, int len
)
993 struct vring_used_elem
*used
;
995 /* The virtqueue contains a ring of used buffers. Get a pointer to the
996 * next entry in that used ring. */
997 used
= &vq
->used
->ring
[vq
->last_used_idx
% vq
->num
];
998 if (put_user(head
, &used
->id
)) {
999 vq_err(vq
, "Failed to write used id");
1002 if (put_user(len
, &used
->len
)) {
1003 vq_err(vq
, "Failed to write used len");
1006 /* Make sure buffer is written before we update index. */
1008 if (put_user(vq
->last_used_idx
+ 1, &vq
->used
->idx
)) {
1009 vq_err(vq
, "Failed to increment used idx");
1012 if (unlikely(vq
->log_used
)) {
1013 /* Make sure data is seen before log. */
1015 /* Log used ring entry write. */
1016 log_write(vq
->log_base
,
1017 vq
->log_addr
+ ((void *)used
- (void *)vq
->used
),
1019 /* Log used index update. */
1020 log_write(vq
->log_base
,
1021 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1022 sizeof vq
->used
->idx
);
1024 eventfd_signal(vq
->log_ctx
, 1);
1026 vq
->last_used_idx
++;
1030 /* This actually signals the guest, using eventfd. */
1031 void vhost_signal(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
1034 if (get_user(flags
, &vq
->avail
->flags
)) {
1035 vq_err(vq
, "Failed to get flags");
1039 /* If they don't want an interrupt, don't signal, unless empty. */
1040 if ((flags
& VRING_AVAIL_F_NO_INTERRUPT
) &&
1041 (vq
->avail_idx
!= vq
->last_avail_idx
||
1042 !vhost_has_feature(dev
, VIRTIO_F_NOTIFY_ON_EMPTY
)))
1045 /* Signal the Guest tell them we used something up. */
1047 eventfd_signal(vq
->call_ctx
, 1);
1050 /* And here's the combo meal deal. Supersize me! */
1051 void vhost_add_used_and_signal(struct vhost_dev
*dev
,
1052 struct vhost_virtqueue
*vq
,
1053 unsigned int head
, int len
)
1055 vhost_add_used(vq
, head
, len
);
1056 vhost_signal(dev
, vq
);
1059 /* OK, now we need to know about added descriptors. */
1060 bool vhost_enable_notify(struct vhost_virtqueue
*vq
)
1064 if (!(vq
->used_flags
& VRING_USED_F_NO_NOTIFY
))
1066 vq
->used_flags
&= ~VRING_USED_F_NO_NOTIFY
;
1067 r
= put_user(vq
->used_flags
, &vq
->used
->flags
);
1069 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1070 &vq
->used
->flags
, r
);
1073 /* They could have slipped one in as we were doing that: make
1074 * sure it's written, then check again. */
1076 r
= get_user(avail_idx
, &vq
->avail
->idx
);
1078 vq_err(vq
, "Failed to check avail idx at %p: %d\n",
1079 &vq
->avail
->idx
, r
);
1083 return avail_idx
!= vq
->last_avail_idx
;
1086 /* We don't need to be notified again. */
1087 void vhost_disable_notify(struct vhost_virtqueue
*vq
)
1090 if (vq
->used_flags
& VRING_USED_F_NO_NOTIFY
)
1092 vq
->used_flags
|= VRING_USED_F_NO_NOTIFY
;
1093 r
= put_user(vq
->used_flags
, &vq
->used
->flags
);
1095 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1096 &vq
->used
->flags
, r
);
1099 int vhost_init(void)
1101 vhost_workqueue
= create_singlethread_workqueue("vhost");
1102 if (!vhost_workqueue
)
1107 void vhost_cleanup(void)
1109 destroy_workqueue(vhost_workqueue
);