1 /* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
4 * Author: Michael S. Tsirkin <mst@redhat.com>
6 * Inspiration, some code, and most witty comments come from
7 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
9 * This work is licensed under the terms of the GNU GPL, version 2.
11 * Generic code for virtio server in host kernel.
14 #include <linux/eventfd.h>
15 #include <linux/vhost.h>
16 #include <linux/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>
32 VHOST_MEMORY_MAX_NREGIONS
= 64,
33 VHOST_MEMORY_F_LOG
= 0x1,
36 static unsigned vhost_zcopy_mask __read_mostly
;
38 #define vhost_used_event(vq) ((u16 __user *)&vq->avail->ring[vq->num])
39 #define vhost_avail_event(vq) ((u16 __user *)&vq->used->ring[vq->num])
41 static void vhost_poll_func(struct file
*file
, wait_queue_head_t
*wqh
,
44 struct vhost_poll
*poll
;
46 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 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
);
82 vhost_work_init(&poll
->work
, fn
);
85 /* Start polling a file. We add ourselves to file's wait queue. The caller must
86 * keep a reference to a file until after vhost_poll_stop is called. */
87 int vhost_poll_start(struct vhost_poll
*poll
, struct file
*file
)
92 mask
= file
->f_op
->poll(file
, &poll
->table
);
94 vhost_poll_wakeup(&poll
->wait
, 0, 0, (void *)mask
);
97 remove_wait_queue(poll
->wqh
, &poll
->wait
);
104 /* Stop polling a file. After this function returns, it becomes safe to drop the
105 * file reference. You must also flush afterwards. */
106 void vhost_poll_stop(struct vhost_poll
*poll
)
109 remove_wait_queue(poll
->wqh
, &poll
->wait
);
114 static bool vhost_work_seq_done(struct vhost_dev
*dev
, struct vhost_work
*work
,
119 spin_lock_irq(&dev
->work_lock
);
120 left
= seq
- work
->done_seq
;
121 spin_unlock_irq(&dev
->work_lock
);
125 static void vhost_work_flush(struct vhost_dev
*dev
, struct vhost_work
*work
)
130 spin_lock_irq(&dev
->work_lock
);
131 seq
= work
->queue_seq
;
133 spin_unlock_irq(&dev
->work_lock
);
134 wait_event(work
->done
, vhost_work_seq_done(dev
, work
, seq
));
135 spin_lock_irq(&dev
->work_lock
);
136 flushing
= --work
->flushing
;
137 spin_unlock_irq(&dev
->work_lock
);
138 BUG_ON(flushing
< 0);
141 /* Flush any work that has been scheduled. When calling this, don't hold any
142 * locks that are also used by the callback. */
143 void vhost_poll_flush(struct vhost_poll
*poll
)
145 vhost_work_flush(poll
->dev
, &poll
->work
);
148 void vhost_work_queue(struct vhost_dev
*dev
, struct vhost_work
*work
)
152 spin_lock_irqsave(&dev
->work_lock
, flags
);
153 if (list_empty(&work
->node
)) {
154 list_add_tail(&work
->node
, &dev
->work_list
);
156 wake_up_process(dev
->worker
);
158 spin_unlock_irqrestore(&dev
->work_lock
, flags
);
161 void vhost_poll_queue(struct vhost_poll
*poll
)
163 vhost_work_queue(poll
->dev
, &poll
->work
);
166 static void vhost_vq_reset(struct vhost_dev
*dev
,
167 struct vhost_virtqueue
*vq
)
173 vq
->last_avail_idx
= 0;
175 vq
->last_used_idx
= 0;
176 vq
->signalled_used
= 0;
177 vq
->signalled_used_valid
= false;
179 vq
->log_used
= false;
180 vq
->log_addr
= -1ull;
183 vq
->private_data
= NULL
;
185 vq
->error_ctx
= NULL
;
196 static int vhost_worker(void *data
)
198 struct vhost_dev
*dev
= data
;
199 struct vhost_work
*work
= NULL
;
200 unsigned uninitialized_var(seq
);
201 mm_segment_t oldfs
= get_fs();
207 /* mb paired w/ kthread_stop */
208 set_current_state(TASK_INTERRUPTIBLE
);
210 spin_lock_irq(&dev
->work_lock
);
212 work
->done_seq
= seq
;
214 wake_up_all(&work
->done
);
217 if (kthread_should_stop()) {
218 spin_unlock_irq(&dev
->work_lock
);
219 __set_current_state(TASK_RUNNING
);
222 if (!list_empty(&dev
->work_list
)) {
223 work
= list_first_entry(&dev
->work_list
,
224 struct vhost_work
, node
);
225 list_del_init(&work
->node
);
226 seq
= work
->queue_seq
;
229 spin_unlock_irq(&dev
->work_lock
);
232 __set_current_state(TASK_RUNNING
);
245 static void vhost_vq_free_iovecs(struct vhost_virtqueue
*vq
)
253 kfree(vq
->ubuf_info
);
254 vq
->ubuf_info
= NULL
;
257 void vhost_enable_zcopy(int vq
)
259 vhost_zcopy_mask
|= 0x1 << vq
;
262 /* Helper to allocate iovec buffers for all vqs. */
263 static long vhost_dev_alloc_iovecs(struct vhost_dev
*dev
)
268 for (i
= 0; i
< dev
->nvqs
; ++i
) {
269 dev
->vqs
[i
].indirect
= kmalloc(sizeof *dev
->vqs
[i
].indirect
*
270 UIO_MAXIOV
, GFP_KERNEL
);
271 dev
->vqs
[i
].log
= kmalloc(sizeof *dev
->vqs
[i
].log
* UIO_MAXIOV
,
273 dev
->vqs
[i
].heads
= kmalloc(sizeof *dev
->vqs
[i
].heads
*
274 UIO_MAXIOV
, GFP_KERNEL
);
275 zcopy
= vhost_zcopy_mask
& (0x1 << i
);
277 dev
->vqs
[i
].ubuf_info
=
278 kmalloc(sizeof *dev
->vqs
[i
].ubuf_info
*
279 UIO_MAXIOV
, GFP_KERNEL
);
280 if (!dev
->vqs
[i
].indirect
|| !dev
->vqs
[i
].log
||
281 !dev
->vqs
[i
].heads
||
282 (zcopy
&& !dev
->vqs
[i
].ubuf_info
))
289 vhost_vq_free_iovecs(&dev
->vqs
[i
]);
293 static void vhost_dev_free_iovecs(struct vhost_dev
*dev
)
297 for (i
= 0; i
< dev
->nvqs
; ++i
)
298 vhost_vq_free_iovecs(&dev
->vqs
[i
]);
301 long vhost_dev_init(struct vhost_dev
*dev
,
302 struct vhost_virtqueue
*vqs
, int nvqs
)
308 mutex_init(&dev
->mutex
);
310 dev
->log_file
= NULL
;
313 spin_lock_init(&dev
->work_lock
);
314 INIT_LIST_HEAD(&dev
->work_list
);
317 for (i
= 0; i
< dev
->nvqs
; ++i
) {
318 dev
->vqs
[i
].log
= NULL
;
319 dev
->vqs
[i
].indirect
= NULL
;
320 dev
->vqs
[i
].heads
= NULL
;
321 dev
->vqs
[i
].ubuf_info
= NULL
;
322 dev
->vqs
[i
].dev
= dev
;
323 mutex_init(&dev
->vqs
[i
].mutex
);
324 vhost_vq_reset(dev
, dev
->vqs
+ i
);
325 if (dev
->vqs
[i
].handle_kick
)
326 vhost_poll_init(&dev
->vqs
[i
].poll
,
327 dev
->vqs
[i
].handle_kick
, POLLIN
, dev
);
333 /* Caller should have device mutex */
334 long vhost_dev_check_owner(struct vhost_dev
*dev
)
336 /* Are you the owner? If not, I don't think you mean to do that */
337 return dev
->mm
== current
->mm
? 0 : -EPERM
;
340 struct vhost_attach_cgroups_struct
{
341 struct vhost_work work
;
342 struct task_struct
*owner
;
346 static void vhost_attach_cgroups_work(struct vhost_work
*work
)
348 struct vhost_attach_cgroups_struct
*s
;
350 s
= container_of(work
, struct vhost_attach_cgroups_struct
, work
);
351 s
->ret
= cgroup_attach_task_all(s
->owner
, current
);
354 static int vhost_attach_cgroups(struct vhost_dev
*dev
)
356 struct vhost_attach_cgroups_struct attach
;
358 attach
.owner
= current
;
359 vhost_work_init(&attach
.work
, vhost_attach_cgroups_work
);
360 vhost_work_queue(dev
, &attach
.work
);
361 vhost_work_flush(dev
, &attach
.work
);
365 /* Caller should have device mutex */
366 static long vhost_dev_set_owner(struct vhost_dev
*dev
)
368 struct task_struct
*worker
;
371 /* Is there an owner already? */
377 /* No owner, become one */
378 dev
->mm
= get_task_mm(current
);
379 worker
= kthread_create(vhost_worker
, dev
, "vhost-%d", current
->pid
);
380 if (IS_ERR(worker
)) {
381 err
= PTR_ERR(worker
);
385 dev
->worker
= worker
;
386 wake_up_process(worker
); /* avoid contributing to loadavg */
388 err
= vhost_attach_cgroups(dev
);
392 err
= vhost_dev_alloc_iovecs(dev
);
398 kthread_stop(worker
);
408 /* Caller should have device mutex */
409 long vhost_dev_reset_owner(struct vhost_dev
*dev
)
411 struct vhost_memory
*memory
;
413 /* Restore memory to default empty mapping. */
414 memory
= kmalloc(offsetof(struct vhost_memory
, regions
), GFP_KERNEL
);
418 vhost_dev_cleanup(dev
, true);
420 memory
->nregions
= 0;
421 RCU_INIT_POINTER(dev
->memory
, memory
);
425 void vhost_dev_stop(struct vhost_dev
*dev
)
429 for (i
= 0; i
< dev
->nvqs
; ++i
) {
430 if (dev
->vqs
[i
].kick
&& dev
->vqs
[i
].handle_kick
) {
431 vhost_poll_stop(&dev
->vqs
[i
].poll
);
432 vhost_poll_flush(&dev
->vqs
[i
].poll
);
437 /* Caller should have device mutex if and only if locked is set */
438 void vhost_dev_cleanup(struct vhost_dev
*dev
, bool locked
)
442 for (i
= 0; i
< dev
->nvqs
; ++i
) {
443 if (dev
->vqs
[i
].error_ctx
)
444 eventfd_ctx_put(dev
->vqs
[i
].error_ctx
);
445 if (dev
->vqs
[i
].error
)
446 fput(dev
->vqs
[i
].error
);
447 if (dev
->vqs
[i
].kick
)
448 fput(dev
->vqs
[i
].kick
);
449 if (dev
->vqs
[i
].call_ctx
)
450 eventfd_ctx_put(dev
->vqs
[i
].call_ctx
);
451 if (dev
->vqs
[i
].call
)
452 fput(dev
->vqs
[i
].call
);
453 vhost_vq_reset(dev
, dev
->vqs
+ i
);
455 vhost_dev_free_iovecs(dev
);
457 eventfd_ctx_put(dev
->log_ctx
);
461 dev
->log_file
= NULL
;
462 /* No one will access memory at this point */
463 kfree(rcu_dereference_protected(dev
->memory
,
465 lockdep_is_held(&dev
->mutex
)));
466 RCU_INIT_POINTER(dev
->memory
, NULL
);
467 WARN_ON(!list_empty(&dev
->work_list
));
469 kthread_stop(dev
->worker
);
477 static int log_access_ok(void __user
*log_base
, u64 addr
, unsigned long sz
)
479 u64 a
= addr
/ VHOST_PAGE_SIZE
/ 8;
481 /* Make sure 64 bit math will not overflow. */
482 if (a
> ULONG_MAX
- (unsigned long)log_base
||
483 a
+ (unsigned long)log_base
> ULONG_MAX
)
486 return access_ok(VERIFY_WRITE
, log_base
+ a
,
487 (sz
+ VHOST_PAGE_SIZE
* 8 - 1) / VHOST_PAGE_SIZE
/ 8);
490 /* Caller should have vq mutex and device mutex. */
491 static int vq_memory_access_ok(void __user
*log_base
, struct vhost_memory
*mem
,
499 for (i
= 0; i
< mem
->nregions
; ++i
) {
500 struct vhost_memory_region
*m
= mem
->regions
+ i
;
501 unsigned long a
= m
->userspace_addr
;
502 if (m
->memory_size
> ULONG_MAX
)
504 else if (!access_ok(VERIFY_WRITE
, (void __user
*)a
,
507 else if (log_all
&& !log_access_ok(log_base
,
515 /* Can we switch to this memory table? */
516 /* Caller should have device mutex but not vq mutex */
517 static int memory_access_ok(struct vhost_dev
*d
, struct vhost_memory
*mem
,
522 for (i
= 0; i
< d
->nvqs
; ++i
) {
524 mutex_lock(&d
->vqs
[i
].mutex
);
525 /* If ring is inactive, will check when it's enabled. */
526 if (d
->vqs
[i
].private_data
)
527 ok
= vq_memory_access_ok(d
->vqs
[i
].log_base
, mem
,
531 mutex_unlock(&d
->vqs
[i
].mutex
);
538 static int vq_access_ok(struct vhost_dev
*d
, unsigned int num
,
539 struct vring_desc __user
*desc
,
540 struct vring_avail __user
*avail
,
541 struct vring_used __user
*used
)
543 size_t s
= vhost_has_feature(d
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
544 return access_ok(VERIFY_READ
, desc
, num
* sizeof *desc
) &&
545 access_ok(VERIFY_READ
, avail
,
546 sizeof *avail
+ num
* sizeof *avail
->ring
+ s
) &&
547 access_ok(VERIFY_WRITE
, used
,
548 sizeof *used
+ num
* sizeof *used
->ring
+ s
);
551 /* Can we log writes? */
552 /* Caller should have device mutex but not vq mutex */
553 int vhost_log_access_ok(struct vhost_dev
*dev
)
555 struct vhost_memory
*mp
;
557 mp
= rcu_dereference_protected(dev
->memory
,
558 lockdep_is_held(&dev
->mutex
));
559 return memory_access_ok(dev
, mp
, 1);
562 /* Verify access for write logging. */
563 /* Caller should have vq mutex and device mutex */
564 static int vq_log_access_ok(struct vhost_dev
*d
, struct vhost_virtqueue
*vq
,
565 void __user
*log_base
)
567 struct vhost_memory
*mp
;
568 size_t s
= vhost_has_feature(d
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
570 mp
= rcu_dereference_protected(vq
->dev
->memory
,
571 lockdep_is_held(&vq
->mutex
));
572 return vq_memory_access_ok(log_base
, mp
,
573 vhost_has_feature(vq
->dev
, VHOST_F_LOG_ALL
)) &&
574 (!vq
->log_used
|| log_access_ok(log_base
, vq
->log_addr
,
576 vq
->num
* sizeof *vq
->used
->ring
+ s
));
579 /* Can we start vq? */
580 /* Caller should have vq mutex and device mutex */
581 int vhost_vq_access_ok(struct vhost_virtqueue
*vq
)
583 return vq_access_ok(vq
->dev
, vq
->num
, vq
->desc
, vq
->avail
, vq
->used
) &&
584 vq_log_access_ok(vq
->dev
, vq
, vq
->log_base
);
587 static long vhost_set_memory(struct vhost_dev
*d
, struct vhost_memory __user
*m
)
589 struct vhost_memory mem
, *newmem
, *oldmem
;
590 unsigned long size
= offsetof(struct vhost_memory
, regions
);
592 if (copy_from_user(&mem
, m
, size
))
596 if (mem
.nregions
> VHOST_MEMORY_MAX_NREGIONS
)
598 newmem
= kmalloc(size
+ mem
.nregions
* sizeof *m
->regions
, GFP_KERNEL
);
602 memcpy(newmem
, &mem
, size
);
603 if (copy_from_user(newmem
->regions
, m
->regions
,
604 mem
.nregions
* sizeof *m
->regions
)) {
609 if (!memory_access_ok(d
, newmem
,
610 vhost_has_feature(d
, VHOST_F_LOG_ALL
))) {
614 oldmem
= rcu_dereference_protected(d
->memory
,
615 lockdep_is_held(&d
->mutex
));
616 rcu_assign_pointer(d
->memory
, newmem
);
622 long vhost_vring_ioctl(struct vhost_dev
*d
, int ioctl
, void __user
*argp
)
624 struct file
*eventfp
, *filep
= NULL
;
625 bool pollstart
= false, pollstop
= false;
626 struct eventfd_ctx
*ctx
= NULL
;
627 u32 __user
*idxp
= argp
;
628 struct vhost_virtqueue
*vq
;
629 struct vhost_vring_state s
;
630 struct vhost_vring_file f
;
631 struct vhost_vring_addr a
;
635 r
= get_user(idx
, idxp
);
643 mutex_lock(&vq
->mutex
);
646 case VHOST_SET_VRING_NUM
:
647 /* Resizing ring with an active backend?
648 * You don't want to do that. */
649 if (vq
->private_data
) {
653 if (copy_from_user(&s
, argp
, sizeof s
)) {
657 if (!s
.num
|| s
.num
> 0xffff || (s
.num
& (s
.num
- 1))) {
663 case VHOST_SET_VRING_BASE
:
664 /* Moving base with an active backend?
665 * You don't want to do that. */
666 if (vq
->private_data
) {
670 if (copy_from_user(&s
, argp
, sizeof s
)) {
674 if (s
.num
> 0xffff) {
678 vq
->last_avail_idx
= s
.num
;
679 /* Forget the cached index value. */
680 vq
->avail_idx
= vq
->last_avail_idx
;
682 case VHOST_GET_VRING_BASE
:
684 s
.num
= vq
->last_avail_idx
;
685 if (copy_to_user(argp
, &s
, sizeof s
))
688 case VHOST_SET_VRING_ADDR
:
689 if (copy_from_user(&a
, argp
, sizeof a
)) {
693 if (a
.flags
& ~(0x1 << VHOST_VRING_F_LOG
)) {
697 /* For 32bit, verify that the top 32bits of the user
698 data are set to zero. */
699 if ((u64
)(unsigned long)a
.desc_user_addr
!= a
.desc_user_addr
||
700 (u64
)(unsigned long)a
.used_user_addr
!= a
.used_user_addr
||
701 (u64
)(unsigned long)a
.avail_user_addr
!= a
.avail_user_addr
) {
705 if ((a
.avail_user_addr
& (sizeof *vq
->avail
->ring
- 1)) ||
706 (a
.used_user_addr
& (sizeof *vq
->used
->ring
- 1)) ||
707 (a
.log_guest_addr
& (sizeof *vq
->used
->ring
- 1))) {
712 /* We only verify access here if backend is configured.
713 * If it is not, we don't as size might not have been setup.
714 * We will verify when backend is configured. */
715 if (vq
->private_data
) {
716 if (!vq_access_ok(d
, vq
->num
,
717 (void __user
*)(unsigned long)a
.desc_user_addr
,
718 (void __user
*)(unsigned long)a
.avail_user_addr
,
719 (void __user
*)(unsigned long)a
.used_user_addr
)) {
724 /* Also validate log access for used ring if enabled. */
725 if ((a
.flags
& (0x1 << VHOST_VRING_F_LOG
)) &&
726 !log_access_ok(vq
->log_base
, a
.log_guest_addr
,
728 vq
->num
* sizeof *vq
->used
->ring
)) {
734 vq
->log_used
= !!(a
.flags
& (0x1 << VHOST_VRING_F_LOG
));
735 vq
->desc
= (void __user
*)(unsigned long)a
.desc_user_addr
;
736 vq
->avail
= (void __user
*)(unsigned long)a
.avail_user_addr
;
737 vq
->log_addr
= a
.log_guest_addr
;
738 vq
->used
= (void __user
*)(unsigned long)a
.used_user_addr
;
740 case VHOST_SET_VRING_KICK
:
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
->kick
) {
751 pollstop
= (filep
= vq
->kick
) != NULL
;
752 pollstart
= (vq
->kick
= eventfp
) != NULL
;
756 case VHOST_SET_VRING_CALL
:
757 if (copy_from_user(&f
, argp
, sizeof f
)) {
761 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
762 if (IS_ERR(eventfp
)) {
763 r
= PTR_ERR(eventfp
);
766 if (eventfp
!= vq
->call
) {
770 vq
->call_ctx
= eventfp
?
771 eventfd_ctx_fileget(eventfp
) : NULL
;
775 case VHOST_SET_VRING_ERR
:
776 if (copy_from_user(&f
, argp
, sizeof f
)) {
780 eventfp
= f
.fd
== -1 ? NULL
: eventfd_fget(f
.fd
);
781 if (IS_ERR(eventfp
)) {
782 r
= PTR_ERR(eventfp
);
785 if (eventfp
!= vq
->error
) {
789 vq
->error_ctx
= eventfp
?
790 eventfd_ctx_fileget(eventfp
) : NULL
;
798 if (pollstop
&& vq
->handle_kick
)
799 vhost_poll_stop(&vq
->poll
);
802 eventfd_ctx_put(ctx
);
806 if (pollstart
&& vq
->handle_kick
)
807 r
= vhost_poll_start(&vq
->poll
, vq
->kick
);
809 mutex_unlock(&vq
->mutex
);
811 if (pollstop
&& vq
->handle_kick
)
812 vhost_poll_flush(&vq
->poll
);
816 /* Caller must have device mutex */
817 long vhost_dev_ioctl(struct vhost_dev
*d
, unsigned int ioctl
, void __user
*argp
)
819 struct file
*eventfp
, *filep
= NULL
;
820 struct eventfd_ctx
*ctx
= NULL
;
825 /* If you are not the owner, you can become one */
826 if (ioctl
== VHOST_SET_OWNER
) {
827 r
= vhost_dev_set_owner(d
);
831 /* You must be the owner to do anything else */
832 r
= vhost_dev_check_owner(d
);
837 case VHOST_SET_MEM_TABLE
:
838 r
= vhost_set_memory(d
, argp
);
840 case VHOST_SET_LOG_BASE
:
841 if (copy_from_user(&p
, argp
, sizeof p
)) {
845 if ((u64
)(unsigned long)p
!= p
) {
849 for (i
= 0; i
< d
->nvqs
; ++i
) {
850 struct vhost_virtqueue
*vq
;
851 void __user
*base
= (void __user
*)(unsigned long)p
;
853 mutex_lock(&vq
->mutex
);
854 /* If ring is inactive, will check when it's enabled. */
855 if (vq
->private_data
&& !vq_log_access_ok(d
, vq
, base
))
859 mutex_unlock(&vq
->mutex
);
862 case VHOST_SET_LOG_FD
:
863 r
= get_user(fd
, (int __user
*)argp
);
866 eventfp
= fd
== -1 ? NULL
: eventfd_fget(fd
);
867 if (IS_ERR(eventfp
)) {
868 r
= PTR_ERR(eventfp
);
871 if (eventfp
!= d
->log_file
) {
874 d
->log_ctx
= eventfp
?
875 eventfd_ctx_fileget(eventfp
) : NULL
;
878 for (i
= 0; i
< d
->nvqs
; ++i
) {
879 mutex_lock(&d
->vqs
[i
].mutex
);
880 d
->vqs
[i
].log_ctx
= d
->log_ctx
;
881 mutex_unlock(&d
->vqs
[i
].mutex
);
884 eventfd_ctx_put(ctx
);
896 static const struct vhost_memory_region
*find_region(struct vhost_memory
*mem
,
897 __u64 addr
, __u32 len
)
899 struct vhost_memory_region
*reg
;
902 /* linear search is not brilliant, but we really have on the order of 6
903 * regions in practice */
904 for (i
= 0; i
< mem
->nregions
; ++i
) {
905 reg
= mem
->regions
+ i
;
906 if (reg
->guest_phys_addr
<= addr
&&
907 reg
->guest_phys_addr
+ reg
->memory_size
- 1 >= addr
)
913 /* TODO: This is really inefficient. We need something like get_user()
914 * (instruction directly accesses the data, with an exception table entry
915 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
917 static int set_bit_to_user(int nr
, void __user
*addr
)
919 unsigned long log
= (unsigned long)addr
;
922 int bit
= nr
+ (log
% PAGE_SIZE
) * 8;
925 r
= get_user_pages_fast(log
, 1, 1, &page
);
929 base
= kmap_atomic(page
);
932 set_page_dirty_lock(page
);
937 static int log_write(void __user
*log_base
,
938 u64 write_address
, u64 write_length
)
940 u64 write_page
= write_address
/ VHOST_PAGE_SIZE
;
945 write_length
+= write_address
% VHOST_PAGE_SIZE
;
947 u64 base
= (u64
)(unsigned long)log_base
;
948 u64 log
= base
+ write_page
/ 8;
949 int bit
= write_page
% 8;
950 if ((u64
)(unsigned long)log
!= log
)
952 r
= set_bit_to_user(bit
, (void __user
*)(unsigned long)log
);
955 if (write_length
<= VHOST_PAGE_SIZE
)
957 write_length
-= VHOST_PAGE_SIZE
;
963 int vhost_log_write(struct vhost_virtqueue
*vq
, struct vhost_log
*log
,
964 unsigned int log_num
, u64 len
)
968 /* Make sure data written is seen before log. */
970 for (i
= 0; i
< log_num
; ++i
) {
971 u64 l
= min(log
[i
].len
, len
);
972 r
= log_write(vq
->log_base
, log
[i
].addr
, l
);
978 eventfd_signal(vq
->log_ctx
, 1);
982 /* Length written exceeds what we have stored. This is a bug. */
987 static int vhost_update_used_flags(struct vhost_virtqueue
*vq
)
990 if (__put_user(vq
->used_flags
, &vq
->used
->flags
) < 0)
992 if (unlikely(vq
->log_used
)) {
993 /* Make sure the flag is seen before log. */
995 /* Log used flag write. */
996 used
= &vq
->used
->flags
;
997 log_write(vq
->log_base
, vq
->log_addr
+
998 (used
- (void __user
*)vq
->used
),
999 sizeof vq
->used
->flags
);
1001 eventfd_signal(vq
->log_ctx
, 1);
1006 static int vhost_update_avail_event(struct vhost_virtqueue
*vq
, u16 avail_event
)
1008 if (__put_user(vq
->avail_idx
, vhost_avail_event(vq
)))
1010 if (unlikely(vq
->log_used
)) {
1012 /* Make sure the event is seen before log. */
1014 /* Log avail event write */
1015 used
= vhost_avail_event(vq
);
1016 log_write(vq
->log_base
, vq
->log_addr
+
1017 (used
- (void __user
*)vq
->used
),
1018 sizeof *vhost_avail_event(vq
));
1020 eventfd_signal(vq
->log_ctx
, 1);
1025 int vhost_init_used(struct vhost_virtqueue
*vq
)
1028 if (!vq
->private_data
)
1031 r
= vhost_update_used_flags(vq
);
1034 vq
->signalled_used_valid
= false;
1035 return get_user(vq
->last_used_idx
, &vq
->used
->idx
);
1038 static int translate_desc(struct vhost_dev
*dev
, u64 addr
, u32 len
,
1039 struct iovec iov
[], int iov_size
)
1041 const struct vhost_memory_region
*reg
;
1042 struct vhost_memory
*mem
;
1049 mem
= rcu_dereference(dev
->memory
);
1050 while ((u64
)len
> s
) {
1052 if (unlikely(ret
>= iov_size
)) {
1056 reg
= find_region(mem
, addr
, len
);
1057 if (unlikely(!reg
)) {
1062 size
= reg
->memory_size
- addr
+ reg
->guest_phys_addr
;
1063 _iov
->iov_len
= min((u64
)len
- s
, size
);
1064 _iov
->iov_base
= (void __user
*)(unsigned long)
1065 (reg
->userspace_addr
+ addr
- reg
->guest_phys_addr
);
1075 /* Each buffer in the virtqueues is actually a chain of descriptors. This
1076 * function returns the next descriptor in the chain,
1077 * or -1U if we're at the end. */
1078 static unsigned next_desc(struct vring_desc
*desc
)
1082 /* If this descriptor says it doesn't chain, we're done. */
1083 if (!(desc
->flags
& VRING_DESC_F_NEXT
))
1086 /* Check they're not leading us off end of descriptors. */
1088 /* Make sure compiler knows to grab that: we don't want it changing! */
1089 /* We will use the result as an index in an array, so most
1090 * architectures only need a compiler barrier here. */
1091 read_barrier_depends();
1096 static int get_indirect(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
1097 struct iovec iov
[], unsigned int iov_size
,
1098 unsigned int *out_num
, unsigned int *in_num
,
1099 struct vhost_log
*log
, unsigned int *log_num
,
1100 struct vring_desc
*indirect
)
1102 struct vring_desc desc
;
1103 unsigned int i
= 0, count
, found
= 0;
1107 if (unlikely(indirect
->len
% sizeof desc
)) {
1108 vq_err(vq
, "Invalid length in indirect descriptor: "
1109 "len 0x%llx not multiple of 0x%zx\n",
1110 (unsigned long long)indirect
->len
,
1115 ret
= translate_desc(dev
, indirect
->addr
, indirect
->len
, vq
->indirect
,
1117 if (unlikely(ret
< 0)) {
1118 vq_err(vq
, "Translation failure %d in indirect.\n", ret
);
1122 /* We will use the result as an address to read from, so most
1123 * architectures only need a compiler barrier here. */
1124 read_barrier_depends();
1126 count
= indirect
->len
/ sizeof desc
;
1127 /* Buffers are chained via a 16 bit next field, so
1128 * we can have at most 2^16 of these. */
1129 if (unlikely(count
> USHRT_MAX
+ 1)) {
1130 vq_err(vq
, "Indirect buffer length too big: %d\n",
1136 unsigned iov_count
= *in_num
+ *out_num
;
1137 if (unlikely(++found
> count
)) {
1138 vq_err(vq
, "Loop detected: last one at %u "
1139 "indirect size %u\n",
1143 if (unlikely(memcpy_fromiovec((unsigned char *)&desc
,
1144 vq
->indirect
, sizeof desc
))) {
1145 vq_err(vq
, "Failed indirect descriptor: idx %d, %zx\n",
1146 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
1149 if (unlikely(desc
.flags
& VRING_DESC_F_INDIRECT
)) {
1150 vq_err(vq
, "Nested indirect descriptor: idx %d, %zx\n",
1151 i
, (size_t)indirect
->addr
+ i
* sizeof desc
);
1155 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
1156 iov_size
- iov_count
);
1157 if (unlikely(ret
< 0)) {
1158 vq_err(vq
, "Translation failure %d indirect idx %d\n",
1162 /* If this is an input descriptor, increment that count. */
1163 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1165 if (unlikely(log
)) {
1166 log
[*log_num
].addr
= desc
.addr
;
1167 log
[*log_num
].len
= desc
.len
;
1171 /* If it's an output descriptor, they're all supposed
1172 * to come before any input descriptors. */
1173 if (unlikely(*in_num
)) {
1174 vq_err(vq
, "Indirect descriptor "
1175 "has out after in: idx %d\n", i
);
1180 } while ((i
= next_desc(&desc
)) != -1);
1184 /* This looks in the virtqueue and for the first available buffer, and converts
1185 * it to an iovec for convenient access. Since descriptors consist of some
1186 * number of output then some number of input descriptors, it's actually two
1187 * iovecs, but we pack them into one and note how many of each there were.
1189 * This function returns the descriptor number found, or vq->num (which is
1190 * never a valid descriptor number) if none was found. A negative code is
1191 * returned on error. */
1192 int vhost_get_vq_desc(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
,
1193 struct iovec iov
[], unsigned int iov_size
,
1194 unsigned int *out_num
, unsigned int *in_num
,
1195 struct vhost_log
*log
, unsigned int *log_num
)
1197 struct vring_desc desc
;
1198 unsigned int i
, head
, found
= 0;
1202 /* Check it isn't doing very strange things with descriptor numbers. */
1203 last_avail_idx
= vq
->last_avail_idx
;
1204 if (unlikely(__get_user(vq
->avail_idx
, &vq
->avail
->idx
))) {
1205 vq_err(vq
, "Failed to access avail idx at %p\n",
1210 if (unlikely((u16
)(vq
->avail_idx
- last_avail_idx
) > vq
->num
)) {
1211 vq_err(vq
, "Guest moved used index from %u to %u",
1212 last_avail_idx
, vq
->avail_idx
);
1216 /* If there's nothing new since last we looked, return invalid. */
1217 if (vq
->avail_idx
== last_avail_idx
)
1220 /* Only get avail ring entries after they have been exposed by guest. */
1223 /* Grab the next descriptor number they're advertising, and increment
1224 * the index we've seen. */
1225 if (unlikely(__get_user(head
,
1226 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]))) {
1227 vq_err(vq
, "Failed to read head: idx %d address %p\n",
1229 &vq
->avail
->ring
[last_avail_idx
% vq
->num
]);
1233 /* If their number is silly, that's an error. */
1234 if (unlikely(head
>= vq
->num
)) {
1235 vq_err(vq
, "Guest says index %u > %u is available",
1240 /* When we start there are none of either input nor output. */
1241 *out_num
= *in_num
= 0;
1247 unsigned iov_count
= *in_num
+ *out_num
;
1248 if (unlikely(i
>= vq
->num
)) {
1249 vq_err(vq
, "Desc index is %u > %u, head = %u",
1253 if (unlikely(++found
> vq
->num
)) {
1254 vq_err(vq
, "Loop detected: last one at %u "
1255 "vq size %u head %u\n",
1259 ret
= __copy_from_user(&desc
, vq
->desc
+ i
, sizeof desc
);
1260 if (unlikely(ret
)) {
1261 vq_err(vq
, "Failed to get descriptor: idx %d addr %p\n",
1265 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1266 ret
= get_indirect(dev
, vq
, iov
, iov_size
,
1268 log
, log_num
, &desc
);
1269 if (unlikely(ret
< 0)) {
1270 vq_err(vq
, "Failure detected "
1271 "in indirect descriptor at idx %d\n", i
);
1277 ret
= translate_desc(dev
, desc
.addr
, desc
.len
, iov
+ iov_count
,
1278 iov_size
- iov_count
);
1279 if (unlikely(ret
< 0)) {
1280 vq_err(vq
, "Translation failure %d descriptor idx %d\n",
1284 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1285 /* If this is an input descriptor,
1286 * increment that count. */
1288 if (unlikely(log
)) {
1289 log
[*log_num
].addr
= desc
.addr
;
1290 log
[*log_num
].len
= desc
.len
;
1294 /* If it's an output descriptor, they're all supposed
1295 * to come before any input descriptors. */
1296 if (unlikely(*in_num
)) {
1297 vq_err(vq
, "Descriptor has out after in: "
1303 } while ((i
= next_desc(&desc
)) != -1);
1305 /* On success, increment avail index. */
1306 vq
->last_avail_idx
++;
1308 /* Assume notifications from guest are disabled at this point,
1309 * if they aren't we would need to update avail_event index. */
1310 BUG_ON(!(vq
->used_flags
& VRING_USED_F_NO_NOTIFY
));
1314 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
1315 void vhost_discard_vq_desc(struct vhost_virtqueue
*vq
, int n
)
1317 vq
->last_avail_idx
-= n
;
1320 /* After we've used one of their buffers, we tell them about it. We'll then
1321 * want to notify the guest, using eventfd. */
1322 int vhost_add_used(struct vhost_virtqueue
*vq
, unsigned int head
, int len
)
1324 struct vring_used_elem __user
*used
;
1326 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1327 * next entry in that used ring. */
1328 used
= &vq
->used
->ring
[vq
->last_used_idx
% vq
->num
];
1329 if (__put_user(head
, &used
->id
)) {
1330 vq_err(vq
, "Failed to write used id");
1333 if (__put_user(len
, &used
->len
)) {
1334 vq_err(vq
, "Failed to write used len");
1337 /* Make sure buffer is written before we update index. */
1339 if (__put_user(vq
->last_used_idx
+ 1, &vq
->used
->idx
)) {
1340 vq_err(vq
, "Failed to increment used idx");
1343 if (unlikely(vq
->log_used
)) {
1344 /* Make sure data is seen before log. */
1346 /* Log used ring entry write. */
1347 log_write(vq
->log_base
,
1349 ((void __user
*)used
- (void __user
*)vq
->used
),
1351 /* Log used index update. */
1352 log_write(vq
->log_base
,
1353 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1354 sizeof vq
->used
->idx
);
1356 eventfd_signal(vq
->log_ctx
, 1);
1358 vq
->last_used_idx
++;
1359 /* If the driver never bothers to signal in a very long while,
1360 * used index might wrap around. If that happens, invalidate
1361 * signalled_used index we stored. TODO: make sure driver
1362 * signals at least once in 2^16 and remove this. */
1363 if (unlikely(vq
->last_used_idx
== vq
->signalled_used
))
1364 vq
->signalled_used_valid
= false;
1368 static int __vhost_add_used_n(struct vhost_virtqueue
*vq
,
1369 struct vring_used_elem
*heads
,
1372 struct vring_used_elem __user
*used
;
1376 start
= vq
->last_used_idx
% vq
->num
;
1377 used
= vq
->used
->ring
+ start
;
1378 if (__copy_to_user(used
, heads
, count
* sizeof *used
)) {
1379 vq_err(vq
, "Failed to write used");
1382 if (unlikely(vq
->log_used
)) {
1383 /* Make sure data is seen before log. */
1385 /* Log used ring entry write. */
1386 log_write(vq
->log_base
,
1388 ((void __user
*)used
- (void __user
*)vq
->used
),
1389 count
* sizeof *used
);
1391 old
= vq
->last_used_idx
;
1392 new = (vq
->last_used_idx
+= count
);
1393 /* If the driver never bothers to signal in a very long while,
1394 * used index might wrap around. If that happens, invalidate
1395 * signalled_used index we stored. TODO: make sure driver
1396 * signals at least once in 2^16 and remove this. */
1397 if (unlikely((u16
)(new - vq
->signalled_used
) < (u16
)(new - old
)))
1398 vq
->signalled_used_valid
= false;
1402 /* After we've used one of their buffers, we tell them about it. We'll then
1403 * want to notify the guest, using eventfd. */
1404 int vhost_add_used_n(struct vhost_virtqueue
*vq
, struct vring_used_elem
*heads
,
1409 start
= vq
->last_used_idx
% vq
->num
;
1410 n
= vq
->num
- start
;
1412 r
= __vhost_add_used_n(vq
, heads
, n
);
1418 r
= __vhost_add_used_n(vq
, heads
, count
);
1420 /* Make sure buffer is written before we update index. */
1422 if (put_user(vq
->last_used_idx
, &vq
->used
->idx
)) {
1423 vq_err(vq
, "Failed to increment used idx");
1426 if (unlikely(vq
->log_used
)) {
1427 /* Log used index update. */
1428 log_write(vq
->log_base
,
1429 vq
->log_addr
+ offsetof(struct vring_used
, idx
),
1430 sizeof vq
->used
->idx
);
1432 eventfd_signal(vq
->log_ctx
, 1);
1437 static bool vhost_notify(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
1439 __u16 old
, new, event
;
1441 /* Flush out used index updates. This is paired
1442 * with the barrier that the Guest executes when enabling
1446 if (vhost_has_feature(dev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
1447 unlikely(vq
->avail_idx
== vq
->last_avail_idx
))
1450 if (!vhost_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
1452 if (__get_user(flags
, &vq
->avail
->flags
)) {
1453 vq_err(vq
, "Failed to get flags");
1456 return !(flags
& VRING_AVAIL_F_NO_INTERRUPT
);
1458 old
= vq
->signalled_used
;
1459 v
= vq
->signalled_used_valid
;
1460 new = vq
->signalled_used
= vq
->last_used_idx
;
1461 vq
->signalled_used_valid
= true;
1466 if (get_user(event
, vhost_used_event(vq
))) {
1467 vq_err(vq
, "Failed to get used event idx");
1470 return vring_need_event(event
, new, old
);
1473 /* This actually signals the guest, using eventfd. */
1474 void vhost_signal(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
1476 /* Signal the Guest tell them we used something up. */
1477 if (vq
->call_ctx
&& vhost_notify(dev
, vq
))
1478 eventfd_signal(vq
->call_ctx
, 1);
1481 /* And here's the combo meal deal. Supersize me! */
1482 void vhost_add_used_and_signal(struct vhost_dev
*dev
,
1483 struct vhost_virtqueue
*vq
,
1484 unsigned int head
, int len
)
1486 vhost_add_used(vq
, head
, len
);
1487 vhost_signal(dev
, vq
);
1490 /* multi-buffer version of vhost_add_used_and_signal */
1491 void vhost_add_used_and_signal_n(struct vhost_dev
*dev
,
1492 struct vhost_virtqueue
*vq
,
1493 struct vring_used_elem
*heads
, unsigned count
)
1495 vhost_add_used_n(vq
, heads
, count
);
1496 vhost_signal(dev
, vq
);
1499 /* OK, now we need to know about added descriptors. */
1500 bool vhost_enable_notify(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
1505 if (!(vq
->used_flags
& VRING_USED_F_NO_NOTIFY
))
1507 vq
->used_flags
&= ~VRING_USED_F_NO_NOTIFY
;
1508 if (!vhost_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
1509 r
= vhost_update_used_flags(vq
);
1511 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1512 &vq
->used
->flags
, r
);
1516 r
= vhost_update_avail_event(vq
, vq
->avail_idx
);
1518 vq_err(vq
, "Failed to update avail event index at %p: %d\n",
1519 vhost_avail_event(vq
), r
);
1523 /* They could have slipped one in as we were doing that: make
1524 * sure it's written, then check again. */
1526 r
= __get_user(avail_idx
, &vq
->avail
->idx
);
1528 vq_err(vq
, "Failed to check avail idx at %p: %d\n",
1529 &vq
->avail
->idx
, r
);
1533 return avail_idx
!= vq
->avail_idx
;
1536 /* We don't need to be notified again. */
1537 void vhost_disable_notify(struct vhost_dev
*dev
, struct vhost_virtqueue
*vq
)
1541 if (vq
->used_flags
& VRING_USED_F_NO_NOTIFY
)
1543 vq
->used_flags
|= VRING_USED_F_NO_NOTIFY
;
1544 if (!vhost_has_feature(dev
, VIRTIO_RING_F_EVENT_IDX
)) {
1545 r
= vhost_update_used_flags(vq
);
1547 vq_err(vq
, "Failed to enable notification at %p: %d\n",
1548 &vq
->used
->flags
, r
);
1552 static void vhost_zerocopy_done_signal(struct kref
*kref
)
1554 struct vhost_ubuf_ref
*ubufs
= container_of(kref
, struct vhost_ubuf_ref
,
1556 wake_up(&ubufs
->wait
);
1559 struct vhost_ubuf_ref
*vhost_ubuf_alloc(struct vhost_virtqueue
*vq
,
1562 struct vhost_ubuf_ref
*ubufs
;
1563 /* No zero copy backend? Nothing to count. */
1566 ubufs
= kmalloc(sizeof *ubufs
, GFP_KERNEL
);
1568 return ERR_PTR(-ENOMEM
);
1569 kref_init(&ubufs
->kref
);
1570 init_waitqueue_head(&ubufs
->wait
);
1575 void vhost_ubuf_put(struct vhost_ubuf_ref
*ubufs
)
1577 kref_put(&ubufs
->kref
, vhost_zerocopy_done_signal
);
1580 void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref
*ubufs
)
1582 kref_put(&ubufs
->kref
, vhost_zerocopy_done_signal
);
1583 wait_event(ubufs
->wait
, !atomic_read(&ubufs
->kref
.refcount
));