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
;
44 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
)
88 mask
= file
->f_op
->poll(file
, &poll
->table
);
90 vhost_poll_wakeup(&poll
->wait
, 0, 0, (void *)mask
);
93 /* Stop polling a file. After this function returns, it becomes safe to drop the
94 * file reference. You must also flush afterwards. */
95 void vhost_poll_stop(struct vhost_poll
*poll
)
97 remove_wait_queue(poll
->wqh
, &poll
->wait
);
100 static bool vhost_work_seq_done(struct vhost_dev
*dev
, struct vhost_work
*work
,
104 spin_lock_irq(&dev
->work_lock
);
105 left
= seq
- work
->done_seq
;
106 spin_unlock_irq(&dev
->work_lock
);
110 static void vhost_work_flush(struct vhost_dev
*dev
, struct vhost_work
*work
)
115 spin_lock_irq(&dev
->work_lock
);
116 seq
= work
->queue_seq
;
118 spin_unlock_irq(&dev
->work_lock
);
119 wait_event(work
->done
, vhost_work_seq_done(dev
, work
, seq
));
120 spin_lock_irq(&dev
->work_lock
);
121 flushing
= --work
->flushing
;
122 spin_unlock_irq(&dev
->work_lock
);
123 BUG_ON(flushing
< 0);
126 /* Flush any work that has been scheduled. When calling this, don't hold any
127 * locks that are also used by the callback. */
128 void vhost_poll_flush(struct vhost_poll
*poll
)
130 vhost_work_flush(poll
->dev
, &poll
->work
);
133 static inline void vhost_work_queue(struct vhost_dev
*dev
,
134 struct vhost_work
*work
)
138 spin_lock_irqsave(&dev
->work_lock
, flags
);
139 if (list_empty(&work
->node
)) {
140 list_add_tail(&work
->node
, &dev
->work_list
);
142 wake_up_process(dev
->worker
);
144 spin_unlock_irqrestore(&dev
->work_lock
, flags
);
147 void vhost_poll_queue(struct vhost_poll
*poll
)
149 vhost_work_queue(poll
->dev
, &poll
->work
);
152 static void vhost_vq_reset(struct vhost_dev
*dev
,
153 struct vhost_virtqueue
*vq
)
159 vq
->last_avail_idx
= 0;
161 vq
->last_used_idx
= 0;
163 vq
->log_used
= false;
164 vq
->log_addr
= -1ull;
167 vq
->private_data
= NULL
;
169 vq
->error_ctx
= NULL
;
177 static int vhost_worker(void *data
)
179 struct vhost_dev
*dev
= data
;
180 struct vhost_work
*work
= NULL
;
181 unsigned uninitialized_var(seq
);
186 /* mb paired w/ kthread_stop */
187 set_current_state(TASK_INTERRUPTIBLE
);
189 spin_lock_irq(&dev
->work_lock
);
191 work
->done_seq
= seq
;
193 wake_up_all(&work
->done
);
196 if (kthread_should_stop()) {
197 spin_unlock_irq(&dev
->work_lock
);
198 __set_current_state(TASK_RUNNING
);
201 if (!list_empty(&dev
->work_list
)) {
202 work
= list_first_entry(&dev
->work_list
,
203 struct vhost_work
, node
);
204 list_del_init(&work
->node
);
205 seq
= work
->queue_seq
;
208 spin_unlock_irq(&dev
->work_lock
);
211 __set_current_state(TASK_RUNNING
);
221 /* Helper to allocate iovec buffers for all vqs. */
222 static long vhost_dev_alloc_iovecs(struct vhost_dev
*dev
)
225 for (i
= 0; i
< dev
->nvqs
; ++i
) {
226 dev
->vqs
[i
].indirect
= kmalloc(sizeof *dev
->vqs
[i
].indirect
*
227 UIO_MAXIOV
, GFP_KERNEL
);
228 dev
->vqs
[i
].log
= kmalloc(sizeof *dev
->vqs
[i
].log
* UIO_MAXIOV
,
230 dev
->vqs
[i
].heads
= kmalloc(sizeof *dev
->vqs
[i
].heads
*
231 UIO_MAXIOV
, GFP_KERNEL
);
233 if (!dev
->vqs
[i
].indirect
|| !dev
->vqs
[i
].log
||
239 for (; i
>= 0; --i
) {
240 kfree(dev
->vqs
[i
].indirect
);
241 kfree(dev
->vqs
[i
].log
);
242 kfree(dev
->vqs
[i
].heads
);
247 static void vhost_dev_free_iovecs(struct vhost_dev
*dev
)
250 for (i
= 0; i
< dev
->nvqs
; ++i
) {
251 kfree(dev
->vqs
[i
].indirect
);
252 dev
->vqs
[i
].indirect
= NULL
;
253 kfree(dev
->vqs
[i
].log
);
254 dev
->vqs
[i
].log
= NULL
;
255 kfree(dev
->vqs
[i
].heads
);
256 dev
->vqs
[i
].heads
= NULL
;
260 long vhost_dev_init(struct vhost_dev
*dev
,
261 struct vhost_virtqueue
*vqs
, int nvqs
)
267 mutex_init(&dev
->mutex
);
269 dev
->log_file
= NULL
;
272 spin_lock_init(&dev
->work_lock
);
273 INIT_LIST_HEAD(&dev
->work_list
);
276 for (i
= 0; i
< dev
->nvqs
; ++i
) {
277 dev
->vqs
[i
].log
= NULL
;
278 dev
->vqs
[i
].indirect
= NULL
;
279 dev
->vqs
[i
].heads
= NULL
;
280 dev
->vqs
[i
].dev
= dev
;
281 mutex_init(&dev
->vqs
[i
].mutex
);
282 vhost_vq_reset(dev
, dev
->vqs
+ i
);
283 if (dev
->vqs
[i
].handle_kick
)
284 vhost_poll_init(&dev
->vqs
[i
].poll
,
285 dev
->vqs
[i
].handle_kick
, POLLIN
, dev
);
291 /* Caller should have device mutex */
292 long vhost_dev_check_owner(struct vhost_dev
*dev
)
294 /* Are you the owner? If not, I don't think you mean to do that */
295 return dev
->mm
== current
->mm
? 0 : -EPERM
;
298 struct vhost_attach_cgroups_struct
{
299 struct vhost_work work
;
300 struct task_struct
*owner
;
304 static void vhost_attach_cgroups_work(struct vhost_work
*work
)
306 struct vhost_attach_cgroups_struct
*s
;
307 s
= container_of(work
, struct vhost_attach_cgroups_struct
, work
);
308 s
->ret
= cgroup_attach_task_all(s
->owner
, current
);
311 static int vhost_attach_cgroups(struct vhost_dev
*dev
)
313 struct vhost_attach_cgroups_struct attach
;
314 attach
.owner
= current
;
315 vhost_work_init(&attach
.work
, vhost_attach_cgroups_work
);
316 vhost_work_queue(dev
, &attach
.work
);
317 vhost_work_flush(dev
, &attach
.work
);
321 /* Caller should have device mutex */
322 static long vhost_dev_set_owner(struct vhost_dev
*dev
)
324 struct task_struct
*worker
;
326 /* Is there an owner already? */
331 /* No owner, become one */
332 dev
->mm
= get_task_mm(current
);
333 worker
= kthread_create(vhost_worker
, dev
, "vhost-%d", current
->pid
);
334 if (IS_ERR(worker
)) {
335 err
= PTR_ERR(worker
);
339 dev
->worker
= worker
;
340 wake_up_process(worker
); /* avoid contributing to loadavg */
342 err
= vhost_attach_cgroups(dev
);
346 err
= vhost_dev_alloc_iovecs(dev
);
352 kthread_stop(worker
);
362 /* Caller should have device mutex */
363 long vhost_dev_reset_owner(struct vhost_dev
*dev
)
365 struct vhost_memory
*memory
;
367 /* Restore memory to default empty mapping. */
368 memory
= kmalloc(offsetof(struct vhost_memory
, regions
), GFP_KERNEL
);
372 vhost_dev_cleanup(dev
);
374 memory
->nregions
= 0;
375 RCU_INIT_POINTER(dev
->memory
, memory
);
379 /* Caller should have device mutex */
380 void vhost_dev_cleanup(struct vhost_dev
*dev
)
383 for (i
= 0; i
< dev
->nvqs
; ++i
) {
384 if (dev
->vqs
[i
].kick
&& dev
->vqs
[i
].handle_kick
) {
385 vhost_poll_stop(&dev
->vqs
[i
].poll
);
386 vhost_poll_flush(&dev
->vqs
[i
].poll
);
388 if (dev
->vqs
[i
].error_ctx
)
389 eventfd_ctx_put(dev
->vqs
[i
].error_ctx
);
390 if (dev
->vqs
[i
].error
)
391 fput(dev
->vqs
[i
].error
);
392 if (dev
->vqs
[i
].kick
)
393 fput(dev
->vqs
[i
].kick
);
394 if (dev
->vqs
[i
].call_ctx
)
395 eventfd_ctx_put(dev
->vqs
[i
].call_ctx
);
396 if (dev
->vqs
[i
].call
)
397 fput(dev
->vqs
[i
].call
);
398 vhost_vq_reset(dev
, dev
->vqs
+ i
);
400 vhost_dev_free_iovecs(dev
);
402 eventfd_ctx_put(dev
->log_ctx
);
406 dev
->log_file
= NULL
;
407 /* No one will access memory at this point */
408 kfree(rcu_dereference_protected(dev
->memory
,
409 lockdep_is_held(&dev
->mutex
)));
410 RCU_INIT_POINTER(dev
->memory
, NULL
);
411 WARN_ON(!list_empty(&dev
->work_list
));
413 kthread_stop(dev
->worker
);
421 static int log_access_ok(void __user
*log_base
, u64 addr
, unsigned long sz
)
423 u64 a
= addr
/ VHOST_PAGE_SIZE
/ 8;
424 /* Make sure 64 bit math will not overflow. */
425 if (a
> ULONG_MAX
- (unsigned long)log_base
||
426 a
+ (unsigned long)log_base
> ULONG_MAX
)
429 return access_ok(VERIFY_WRITE
, log_base
+ a
,
430 (sz
+ VHOST_PAGE_SIZE
* 8 - 1) / VHOST_PAGE_SIZE
/ 8);
433 /* Caller should have vq mutex and device mutex. */
434 static int vq_memory_access_ok(void __user
*log_base
, struct vhost_memory
*mem
,
442 for (i
= 0; i
< mem
->nregions
; ++i
) {
443 struct vhost_memory_region
*m
= mem
->regions
+ i
;
444 unsigned long a
= m
->userspace_addr
;
445 if (m
->memory_size
> ULONG_MAX
)
447 else if (!access_ok(VERIFY_WRITE
, (void __user
*)a
,
450 else if (log_all
&& !log_access_ok(log_base
,
458 /* Can we switch to this memory table? */
459 /* Caller should have device mutex but not vq mutex */
460 static int memory_access_ok(struct vhost_dev
*d
, struct vhost_memory
*mem
,
464 for (i
= 0; i
< d
->nvqs
; ++i
) {
466 mutex_lock(&d
->vqs
[i
].mutex
);
467 /* If ring is inactive, will check when it's enabled. */
468 if (d
->vqs
[i
].private_data
)
469 ok
= vq_memory_access_ok(d
->vqs
[i
].log_base
, mem
,
473 mutex_unlock(&d
->vqs
[i
].mutex
);
480 static int vq_access_ok(unsigned int num
,
481 struct vring_desc __user
*desc
,
482 struct vring_avail __user
*avail
,
483 struct vring_used __user
*used
)
485 return access_ok(VERIFY_READ
, desc
, num
* sizeof *desc
) &&
486 access_ok(VERIFY_READ
, avail
,
487 sizeof *avail
+ num
* sizeof *avail
->ring
) &&
488 access_ok(VERIFY_WRITE
, used
,
489 sizeof *used
+ num
* sizeof *used
->ring
);
492 /* Can we log writes? */
493 /* Caller should have device mutex but not vq mutex */
494 int vhost_log_access_ok(struct vhost_dev
*dev
)
496 struct vhost_memory
*mp
;
498 mp
= rcu_dereference_protected(dev
->memory
,
499 lockdep_is_held(&dev
->mutex
));
500 return memory_access_ok(dev
, mp
, 1);
503 /* Verify access for write logging. */
504 /* Caller should have vq mutex and device mutex */
505 static int vq_log_access_ok(struct vhost_virtqueue
*vq
, void __user
*log_base
)
507 struct vhost_memory
*mp
;
509 mp
= rcu_dereference_protected(vq
->dev
->memory
,
510 lockdep_is_held(&vq
->mutex
));
511 return vq_memory_access_ok(log_base
, mp
,
512 vhost_has_feature(vq
->dev
, VHOST_F_LOG_ALL
)) &&
513 (!vq
->log_used
|| log_access_ok(log_base
, vq
->log_addr
,
515 vq
->num
* sizeof *vq
->used
->ring
));
518 /* Can we start vq? */
519 /* Caller should have vq mutex and device mutex */
520 int vhost_vq_access_ok(struct vhost_virtqueue
*vq
)
522 return vq_access_ok(vq
->num
, vq
->desc
, vq
->avail
, vq
->used
) &&
523 vq_log_access_ok(vq
, vq
->log_base
);
526 static long vhost_set_memory(struct vhost_dev
*d
, struct vhost_memory __user
*m
)
528 struct vhost_memory mem
, *newmem
, *oldmem
;
529 unsigned long size
= offsetof(struct vhost_memory
, regions
);
530 if (copy_from_user(&mem
, m
, size
))
534 if (mem
.nregions
> VHOST_MEMORY_MAX_NREGIONS
)
536 newmem
= kmalloc(size
+ mem
.nregions
* sizeof *m
->regions
, GFP_KERNEL
);
540 memcpy(newmem
, &mem
, size
);
541 if (copy_from_user(newmem
->regions
, m
->regions
,
542 mem
.nregions
* sizeof *m
->regions
)) {
547 if (!memory_access_ok(d
, newmem
, vhost_has_feature(d
, VHOST_F_LOG_ALL
))) {
551 oldmem
= rcu_dereference_protected(d
->memory
,
552 lockdep_is_held(&d
->mutex
));
553 rcu_assign_pointer(d
->memory
, newmem
);
559 static int init_used(struct vhost_virtqueue
*vq
,
560 struct vring_used __user
*used
)
562 int r
= put_user(vq
->used_flags
, &used
->flags
);
565 return get_user(vq
->last_used_idx
, &used
->idx
);
568 static long vhost_set_vring(struct vhost_dev
*d
, int ioctl
, void __user
*argp
)
570 struct file
*eventfp
, *filep
= NULL
,
571 *pollstart
= NULL
, *pollstop
= NULL
;
572 struct eventfd_ctx
*ctx
= NULL
;
573 u32 __user
*idxp
= argp
;
574 struct vhost_virtqueue
*vq
;
575 struct vhost_vring_state s
;
576 struct vhost_vring_file f
;
577 struct vhost_vring_addr a
;
581 r
= get_user(idx
, idxp
);
589 mutex_lock(&vq
->mutex
);
592 case VHOST_SET_VRING_NUM
:
593 /* Resizing ring with an active backend?
594 * You don't want to do that. */
595 if (vq
->private_data
) {
599 if (copy_from_user(&s
, argp
, sizeof s
)) {
603 if (!s
.num
|| s
.num
> 0xffff || (s
.num
& (s
.num
- 1))) {
609 case VHOST_SET_VRING_BASE
:
610 /* Moving base with an active backend?
611 * You don't want to do that. */
612 if (vq
->private_data
) {
616 if (copy_from_user(&s
, argp
, sizeof s
)) {
620 if (s
.num
> 0xffff) {
624 vq
->last_avail_idx
= s
.num
;
625 /* Forget the cached index value. */
626 vq
->avail_idx
= vq
->last_avail_idx
;
628 case VHOST_GET_VRING_BASE
:
630 s
.num
= vq
->last_avail_idx
;
631 if (copy_to_user(argp
, &s
, sizeof s
))
634 case VHOST_SET_VRING_ADDR
:
635 if (copy_from_user(&a
, argp
, sizeof a
)) {
639 if (a
.flags
& ~(0x1 << VHOST_VRING_F_LOG
)) {
643 /* For 32bit, verify that the top 32bits of the user
644 data are set to zero. */
645 if ((u64
)(unsigned long)a
.desc_user_addr
!= a
.desc_user_addr
||
646 (u64
)(unsigned long)a
.used_user_addr
!= a
.used_user_addr
||
647 (u64
)(unsigned long)a
.avail_user_addr
!= a
.avail_user_addr
) {
651 if ((a
.avail_user_addr
& (sizeof *vq
->avail
->ring
- 1)) ||
652 (a
.used_user_addr
& (sizeof *vq
->used
->ring
- 1)) ||
653 (a
.log_guest_addr
& (sizeof *vq
->used
->ring
- 1))) {
658 /* We only verify access here if backend is configured.
659 * If it is not, we don't as size might not have been setup.
660 * We will verify when backend is configured. */
661 if (vq
->private_data
) {
662 if (!vq_access_ok(vq
->num
,
663 (void __user
*)(unsigned long)a
.desc_user_addr
,
664 (void __user
*)(unsigned long)a
.avail_user_addr
,
665 (void __user
*)(unsigned long)a
.used_user_addr
)) {
670 /* Also validate log access for used ring if enabled. */
671 if ((a
.flags
& (0x1 << VHOST_VRING_F_LOG
)) &&
672 !log_access_ok(vq
->log_base
, a
.log_guest_addr
,
674 vq
->num
* sizeof *vq
->used
->ring
)) {
680 r
= init_used(vq
, (struct vring_used __user
*)(unsigned long)
684 vq
->log_used
= !!(a
.flags
& (0x1 << VHOST_VRING_F_LOG
));
685 vq
->desc
= (void __user
*)(unsigned long)a
.desc_user_addr
;
686 vq
->avail
= (void __user
*)(unsigned long)a
.avail_user_addr
;
687 vq
->log_addr
= a
.log_guest_addr
;
688 vq
->used
= (void __user
*)(unsigned long)a
.used_user_addr
;
690 case VHOST_SET_VRING_KICK
:
691 if (copy_from_user(&f
, argp
, sizeof f
)) {
695 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
696 if (IS_ERR(eventfp
)) {
697 r
= PTR_ERR(eventfp
);
700 if (eventfp
!= vq
->kick
) {
701 pollstop
= filep
= vq
->kick
;
702 pollstart
= vq
->kick
= eventfp
;
706 case VHOST_SET_VRING_CALL
:
707 if (copy_from_user(&f
, argp
, sizeof f
)) {
711 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
712 if (IS_ERR(eventfp
)) {
713 r
= PTR_ERR(eventfp
);
716 if (eventfp
!= vq
->call
) {
720 vq
->call_ctx
= eventfp
?
721 eventfd_ctx_fileget(eventfp
) : NULL
;
725 case VHOST_SET_VRING_ERR
:
726 if (copy_from_user(&f
, argp
, sizeof f
)) {
730 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
731 if (IS_ERR(eventfp
)) {
732 r
= PTR_ERR(eventfp
);
735 if (eventfp
!= vq
->error
) {
739 vq
->error_ctx
= eventfp
?
740 eventfd_ctx_fileget(eventfp
) : NULL
;
748 if (pollstop
&& vq
->handle_kick
)
749 vhost_poll_stop(&vq
->poll
);
752 eventfd_ctx_put(ctx
);
756 if (pollstart
&& vq
->handle_kick
)
757 vhost_poll_start(&vq
->poll
, vq
->kick
);
759 mutex_unlock(&vq
->mutex
);
761 if (pollstop
&& vq
->handle_kick
)
762 vhost_poll_flush(&vq
->poll
);
766 /* Caller must have device mutex */
767 long vhost_dev_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, unsigned long arg
)
769 void __user
*argp
= (void __user
*)arg
;
770 struct file
*eventfp
, *filep
= NULL
;
771 struct eventfd_ctx
*ctx
= NULL
;
776 /* If you are not the owner, you can become one */
777 if (ioctl
== VHOST_SET_OWNER
) {
778 r
= vhost_dev_set_owner(d
);
782 /* You must be the owner to do anything else */
783 r
= vhost_dev_check_owner(d
);
788 case VHOST_SET_MEM_TABLE
:
789 r
= vhost_set_memory(d
, argp
);
791 case VHOST_SET_LOG_BASE
:
792 if (copy_from_user(&p
, argp
, sizeof p
)) {
796 if ((u64
)(unsigned long)p
!= p
) {
800 for (i
= 0; i
< d
->nvqs
; ++i
) {
801 struct vhost_virtqueue
*vq
;
802 void __user
*base
= (void __user
*)(unsigned long)p
;
804 mutex_lock(&vq
->mutex
);
805 /* If ring is inactive, will check when it's enabled. */
806 if (vq
->private_data
&& !vq_log_access_ok(vq
, base
))
810 mutex_unlock(&vq
->mutex
);
813 case VHOST_SET_LOG_FD
:
814 r
= get_user(fd
, (int __user
*)argp
);
817 eventfp
= fd
== -1 ? NULL
: eventfd_fget(fd
);
818 if (IS_ERR(eventfp
)) {
819 r
= PTR_ERR(eventfp
);
822 if (eventfp
!= d
->log_file
) {
825 d
->log_ctx
= eventfp
?
826 eventfd_ctx_fileget(eventfp
) : NULL
;
829 for (i
= 0; i
< d
->nvqs
; ++i
) {
830 mutex_lock(&d
->vqs
[i
].mutex
);
831 d
->vqs
[i
].log_ctx
= d
->log_ctx
;
832 mutex_unlock(&d
->vqs
[i
].mutex
);
835 eventfd_ctx_put(ctx
);
840 r
= vhost_set_vring(d
, ioctl
, argp
);
847 static const struct vhost_memory_region
*find_region(struct vhost_memory
*mem
,
848 __u64 addr
, __u32 len
)
850 struct vhost_memory_region
*reg
;
852 /* linear search is not brilliant, but we really have on the order of 6
853 * regions in practice */
854 for (i
= 0; i
< mem
->nregions
; ++i
) {
855 reg
= mem
->regions
+ i
;
856 if (reg
->guest_phys_addr
<= addr
&&
857 reg
->guest_phys_addr
+ reg
->memory_size
- 1 >= addr
)
863 /* TODO: This is really inefficient. We need something like get_user()
864 * (instruction directly accesses the data, with an exception table entry
865 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
867 static int set_bit_to_user(int nr
, void __user
*addr
)
869 unsigned long log
= (unsigned long)addr
;
872 int bit
= nr
+ (log
% PAGE_SIZE
) * 8;
874 r
= get_user_pages_fast(log
, 1, 1, &page
);
878 base
= kmap_atomic(page
, KM_USER0
);
880 kunmap_atomic(base
, KM_USER0
);
881 set_page_dirty_lock(page
);
886 static int log_write(void __user
*log_base
,
887 u64 write_address
, u64 write_length
)
889 u64 write_page
= write_address
/ VHOST_PAGE_SIZE
;
893 write_length
+= write_address
% VHOST_PAGE_SIZE
;
895 u64 base
= (u64
)(unsigned long)log_base
;
896 u64 log
= base
+ write_page
/ 8;
897 int bit
= write_page
% 8;
898 if ((u64
)(unsigned long)log
!= log
)
900 r
= set_bit_to_user(bit
, (void __user
*)(unsigned long)log
);
903 if (write_length
<= VHOST_PAGE_SIZE
)
905 write_length
-= VHOST_PAGE_SIZE
;
911 int vhost_log_write(struct vhost_virtqueue
*vq
, struct vhost_log
*log
,
912 unsigned int log_num
, u64 len
)
916 /* Make sure data written is seen before log. */
918 for (i
= 0; i
< log_num
; ++i
) {
919 u64 l
= min(log
[i
].len
, len
);
920 r
= log_write(vq
->log_base
, log
[i
].addr
, l
);
926 eventfd_signal(vq
->log_ctx
, 1);
930 /* Length written exceeds what we have stored. This is a bug. */
935 static int translate_desc(struct vhost_dev
*dev
, u64 addr
, u32 len
,
936 struct iovec iov
[], int iov_size
)
938 const struct vhost_memory_region
*reg
;
939 struct vhost_memory
*mem
;
946 mem
= rcu_dereference(dev
->memory
);
947 while ((u64
)len
> s
) {
949 if (unlikely(ret
>= iov_size
)) {
953 reg
= find_region(mem
, addr
, len
);
954 if (unlikely(!reg
)) {
959 size
= reg
->memory_size
- addr
+ reg
->guest_phys_addr
;
960 _iov
->iov_len
= min((u64
)len
, size
);
961 _iov
->iov_base
= (void __user
*)(unsigned long)
962 (reg
->userspace_addr
+ addr
- reg
->guest_phys_addr
);
972 /* Each buffer in the virtqueues is actually a chain of descriptors. This
973 * function returns the next descriptor in the chain,
974 * or -1U if we're at the end. */
975 static unsigned next_desc(struct vring_desc
*desc
)
979 /* If this descriptor says it doesn't chain, we're done. */
980 if (!(desc
->flags
& VRING_DESC_F_NEXT
))
983 /* Check they're not leading us off end of descriptors. */
985 /* Make sure compiler knows to grab that: we don't want it changing! */
986 /* We will use the result as an index in an array, so most
987 * architectures only need a compiler barrier here. */
988 read_barrier_depends();
993 static int get_indirect(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
994 struct iovec iov
[], unsigned int iov_size
,
995 unsigned int *out_num
, unsigned int *in_num
,
996 struct vhost_log
*log
, unsigned int *log_num
,
997 struct vring_desc
*indirect
)
999 struct vring_desc desc
;
1000 unsigned int i
= 0, count
, found
= 0;
1004 if (unlikely(indirect
->len
% sizeof desc
)) {
1005 vq_err(vq
, "Invalid length in indirect descriptor: "
1006 "len 0x%llx not multiple of 0x%zx\n",
1007 (unsigned long long)indirect
->len
,
1012 ret
= translate_desc(dev
, indirect
->addr
, indirect
->len
, vq
->indirect
,
1014 if (unlikely(ret
< 0)) {
1015 vq_err(vq
, "Translation failure %d in indirect.\n", ret
);
1019 /* We will use the result as an address to read from, so most
1020 * architectures only need a compiler barrier here. */
1021 read_barrier_depends();
1023 count
= indirect
->len
/ sizeof desc
;
1024 /* Buffers are chained via a 16 bit next field, so
1025 * we can have at most 2^16 of these. */
1026 if (unlikely(count
> USHRT_MAX
+ 1)) {
1027 vq_err(vq
, "Indirect buffer length too big: %d\n",
1033 unsigned iov_count
= *in_num
+ *out_num
;
1034 if (unlikely(++found
> count
)) {
1035 vq_err(vq
, "Loop detected: last one at %u "
1036 "indirect size %u\n",
1040 if (unlikely(memcpy_fromiovec((unsigned char *)&desc
, vq
->indirect
,
1042 vq_err(vq
, "Failed indirect descriptor: idx %d, %zx\n",
1043 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
1046 if (unlikely(desc
.flags
& VRING_DESC_F_INDIRECT
)) {
1047 vq_err(vq
, "Nested indirect descriptor: idx %d, %zx\n",
1048 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
1052 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
1053 iov_size
- iov_count
);
1054 if (unlikely(ret
< 0)) {
1055 vq_err(vq
, "Translation failure %d indirect idx %d\n",
1059 /* If this is an input descriptor, increment that count. */
1060 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1062 if (unlikely(log
)) {
1063 log
[*log_num
].addr
= desc
.addr
;
1064 log
[*log_num
].len
= desc
.len
;
1068 /* If it's an output descriptor, they're all supposed
1069 * to come before any input descriptors. */
1070 if (unlikely(*in_num
)) {
1071 vq_err(vq
, "Indirect descriptor "
1072 "has out after in: idx %d\n", i
);
1077 } while ((i
= next_desc(&desc
)) != -1);
1081 /* This looks in the virtqueue and for the first available buffer, and converts
1082 * it to an iovec for convenient access. Since descriptors consist of some
1083 * number of output then some number of input descriptors, it's actually two
1084 * iovecs, but we pack them into one and note how many of each there were.
1086 * This function returns the descriptor number found, or vq->num (which is
1087 * never a valid descriptor number) if none was found. A negative code is
1088 * returned on error. */
1089 int vhost_get_vq_desc(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
1090 struct iovec iov
[], unsigned int iov_size
,
1091 unsigned int *out_num
, unsigned int *in_num
,
1092 struct vhost_log
*log
, unsigned int *log_num
)
1094 struct vring_desc desc
;
1095 unsigned int i
, head
, found
= 0;
1099 /* Check it isn't doing very strange things with descriptor numbers. */
1100 last_avail_idx
= vq
->last_avail_idx
;
1101 if (unlikely(__get_user(vq
->avail_idx
, &vq
->avail
->idx
))) {
1102 vq_err(vq
, "Failed to access avail idx at %p\n",
1107 if (unlikely((u16
)(vq
->avail_idx
- last_avail_idx
) > vq
->num
)) {
1108 vq_err(vq
, "Guest moved used index from %u to %u",
1109 last_avail_idx
, vq
->avail_idx
);
1113 /* If there's nothing new since last we looked, return invalid. */
1114 if (vq
->avail_idx
== last_avail_idx
)
1117 /* Only get avail ring entries after they have been exposed by guest. */
1120 /* Grab the next descriptor number they're advertising, and increment
1121 * the index we've seen. */
1122 if (unlikely(__get_user(head
,
1123 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]))) {
1124 vq_err(vq
, "Failed to read head: idx %d address %p\n",
1126 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]);
1130 /* If their number is silly, that's an error. */
1131 if (unlikely(head
>= vq
->num
)) {
1132 vq_err(vq
, "Guest says index %u > %u is available",
1137 /* When we start there are none of either input nor output. */
1138 *out_num
= *in_num
= 0;
1144 unsigned iov_count
= *in_num
+ *out_num
;
1145 if (unlikely(i
>= vq
->num
)) {
1146 vq_err(vq
, "Desc index is %u > %u, head = %u",
1150 if (unlikely(++found
> vq
->num
)) {
1151 vq_err(vq
, "Loop detected: last one at %u "
1152 "vq size %u head %u\n",
1156 ret
= copy_from_user(&desc
, vq
->desc
+ i
, sizeof desc
);
1157 if (unlikely(ret
)) {
1158 vq_err(vq
, "Failed to get descriptor: idx %d addr %p\n",
1162 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1163 ret
= get_indirect(dev
, vq
, iov
, iov_size
,
1165 log
, log_num
, &desc
);
1166 if (unlikely(ret
< 0)) {
1167 vq_err(vq
, "Failure detected "
1168 "in indirect descriptor at idx %d\n", i
);
1174 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
1175 iov_size
- iov_count
);
1176 if (unlikely(ret
< 0)) {
1177 vq_err(vq
, "Translation failure %d descriptor idx %d\n",
1181 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1182 /* If this is an input descriptor,
1183 * increment that count. */
1185 if (unlikely(log
)) {
1186 log
[*log_num
].addr
= desc
.addr
;
1187 log
[*log_num
].len
= desc
.len
;
1191 /* If it's an output descriptor, they're all supposed
1192 * to come before any input descriptors. */
1193 if (unlikely(*in_num
)) {
1194 vq_err(vq
, "Descriptor has out after in: "
1200 } while ((i
= next_desc(&desc
)) != -1);
1202 /* On success, increment avail index. */
1203 vq
->last_avail_idx
++;
1207 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
1208 void vhost_discard_vq_desc(struct vhost_virtqueue
*vq
, int n
)
1210 vq
->last_avail_idx
-= n
;
1213 /* After we've used one of their buffers, we tell them about it. We'll then
1214 * want to notify the guest, using eventfd. */
1215 int vhost_add_used(struct vhost_virtqueue
*vq
, unsigned int head
, int len
)
1217 struct vring_used_elem __user
*used
;
1219 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1220 * next entry in that used ring. */
1221 used
= &vq
->used
->ring
[vq
->last_used_idx
% vq
->num
];
1222 if (__put_user(head
, &used
->id
)) {
1223 vq_err(vq
, "Failed to write used id");
1226 if (__put_user(len
, &used
->len
)) {
1227 vq_err(vq
, "Failed to write used len");
1230 /* Make sure buffer is written before we update index. */
1232 if (__put_user(vq
->last_used_idx
+ 1, &vq
->used
->idx
)) {
1233 vq_err(vq
, "Failed to increment used idx");
1236 if (unlikely(vq
->log_used
)) {
1237 /* Make sure data is seen before log. */
1239 /* Log used ring entry write. */
1240 log_write(vq
->log_base
,
1242 ((void __user
*)used
- (void __user
*)vq
->used
),
1244 /* Log used index update. */
1245 log_write(vq
->log_base
,
1246 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1247 sizeof vq
->used
->idx
);
1249 eventfd_signal(vq
->log_ctx
, 1);
1251 vq
->last_used_idx
++;
1255 static int __vhost_add_used_n(struct vhost_virtqueue
*vq
,
1256 struct vring_used_elem
*heads
,
1259 struct vring_used_elem __user
*used
;
1262 start
= vq
->last_used_idx
% vq
->num
;
1263 used
= vq
->used
->ring
+ start
;
1264 if (__copy_to_user(used
, heads
, count
* sizeof *used
)) {
1265 vq_err(vq
, "Failed to write used");
1268 if (unlikely(vq
->log_used
)) {
1269 /* Make sure data is seen before log. */
1271 /* Log used ring entry write. */
1272 log_write(vq
->log_base
,
1274 ((void __user
*)used
- (void __user
*)vq
->used
),
1275 count
* sizeof *used
);
1277 vq
->last_used_idx
+= count
;
1281 /* After we've used one of their buffers, we tell them about it. We'll then
1282 * want to notify the guest, using eventfd. */
1283 int vhost_add_used_n(struct vhost_virtqueue
*vq
, struct vring_used_elem
*heads
,
1288 start
= vq
->last_used_idx
% vq
->num
;
1289 n
= vq
->num
- start
;
1291 r
= __vhost_add_used_n(vq
, heads
, n
);
1297 r
= __vhost_add_used_n(vq
, heads
, count
);
1299 /* Make sure buffer is written before we update index. */
1301 if (put_user(vq
->last_used_idx
, &vq
->used
->idx
)) {
1302 vq_err(vq
, "Failed to increment used idx");
1305 if (unlikely(vq
->log_used
)) {
1306 /* Log used index update. */
1307 log_write(vq
->log_base
,
1308 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1309 sizeof vq
->used
->idx
);
1311 eventfd_signal(vq
->log_ctx
, 1);
1316 /* This actually signals the guest, using eventfd. */
1317 void vhost_signal(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
1320 /* Flush out used index updates. This is paired
1321 * with the barrier that the Guest executes when enabling
1325 if (__get_user(flags
, &vq
->avail
->flags
)) {
1326 vq_err(vq
, "Failed to get flags");
1330 /* If they don't want an interrupt, don't signal, unless empty. */
1331 if ((flags
& VRING_AVAIL_F_NO_INTERRUPT
) &&
1332 (vq
->avail_idx
!= vq
->last_avail_idx
||
1333 !vhost_has_feature(dev
, VIRTIO_F_NOTIFY_ON_EMPTY
)))
1336 /* Signal the Guest tell them we used something up. */
1338 eventfd_signal(vq
->call_ctx
, 1);
1341 /* And here's the combo meal deal. Supersize me! */
1342 void vhost_add_used_and_signal(struct vhost_dev
*dev
,
1343 struct vhost_virtqueue
*vq
,
1344 unsigned int head
, int len
)
1346 vhost_add_used(vq
, head
, len
);
1347 vhost_signal(dev
, vq
);
1350 /* multi-buffer version of vhost_add_used_and_signal */
1351 void vhost_add_used_and_signal_n(struct vhost_dev
*dev
,
1352 struct vhost_virtqueue
*vq
,
1353 struct vring_used_elem
*heads
, unsigned count
)
1355 vhost_add_used_n(vq
, heads
, count
);
1356 vhost_signal(dev
, vq
);
1359 /* OK, now we need to know about added descriptors. */
1360 bool vhost_enable_notify(struct vhost_virtqueue
*vq
)
1364 if (!(vq
->used_flags
& VRING_USED_F_NO_NOTIFY
))
1366 vq
->used_flags
&= ~VRING_USED_F_NO_NOTIFY
;
1367 r
= put_user(vq
->used_flags
, &vq
->used
->flags
);
1369 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1370 &vq
->used
->flags
, r
);
1373 /* They could have slipped one in as we were doing that: make
1374 * sure it's written, then check again. */
1376 r
= __get_user(avail_idx
, &vq
->avail
->idx
);
1378 vq_err(vq
, "Failed to check avail idx at %p: %d\n",
1379 &vq
->avail
->idx
, r
);
1383 return avail_idx
!= vq
->avail_idx
;
1386 /* We don't need to be notified again. */
1387 void vhost_disable_notify(struct vhost_virtqueue
*vq
)
1390 if (vq
->used_flags
& VRING_USED_F_NO_NOTIFY
)
1392 vq
->used_flags
|= VRING_USED_F_NO_NOTIFY
;
1393 r
= put_user(vq
->used_flags
, &vq
->used
->flags
);
1395 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1396 &vq
->used
->flags
, r
);