Merge branches 'work.misc' and 'work.dcache' of git://git.kernel.org/pub/scm/linux...
[sfrench/cifs-2.6.git] / drivers / vhost / vhost.c
1 /* Copyright (C) 2009 Red Hat, Inc.
2  * Copyright (C) 2006 Rusty Russell IBM Corporation
3  *
4  * Author: Michael S. Tsirkin <mst@redhat.com>
5  *
6  * Inspiration, some code, and most witty comments come from
7  * Documentation/virtual/lguest/lguest.c, by Rusty Russell
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.
10  *
11  * Generic code for virtio server in host kernel.
12  */
13
14 #include <linux/eventfd.h>
15 #include <linux/vhost.h>
16 #include <linux/uio.h>
17 #include <linux/mm.h>
18 #include <linux/mmu_context.h>
19 #include <linux/miscdevice.h>
20 #include <linux/mutex.h>
21 #include <linux/poll.h>
22 #include <linux/file.h>
23 #include <linux/highmem.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/kthread.h>
27 #include <linux/cgroup.h>
28 #include <linux/module.h>
29 #include <linux/sort.h>
30 #include <linux/sched/mm.h>
31 #include <linux/sched/signal.h>
32 #include <linux/interval_tree_generic.h>
33
34 #include "vhost.h"
35
36 static ushort max_mem_regions = 64;
37 module_param(max_mem_regions, ushort, 0444);
38 MODULE_PARM_DESC(max_mem_regions,
39         "Maximum number of memory regions in memory map. (default: 64)");
40 static int max_iotlb_entries = 2048;
41 module_param(max_iotlb_entries, int, 0444);
42 MODULE_PARM_DESC(max_iotlb_entries,
43         "Maximum number of iotlb entries. (default: 2048)");
44
45 enum {
46         VHOST_MEMORY_F_LOG = 0x1,
47 };
48
49 #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
50 #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
51
52 INTERVAL_TREE_DEFINE(struct vhost_umem_node,
53                      rb, __u64, __subtree_last,
54                      START, LAST, static inline, vhost_umem_interval_tree);
55
56 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
57 static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
58 {
59         vq->user_be = !virtio_legacy_is_little_endian();
60 }
61
62 static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
63 {
64         vq->user_be = true;
65 }
66
67 static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
68 {
69         vq->user_be = false;
70 }
71
72 static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
73 {
74         struct vhost_vring_state s;
75
76         if (vq->private_data)
77                 return -EBUSY;
78
79         if (copy_from_user(&s, argp, sizeof(s)))
80                 return -EFAULT;
81
82         if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
83             s.num != VHOST_VRING_BIG_ENDIAN)
84                 return -EINVAL;
85
86         if (s.num == VHOST_VRING_BIG_ENDIAN)
87                 vhost_enable_cross_endian_big(vq);
88         else
89                 vhost_enable_cross_endian_little(vq);
90
91         return 0;
92 }
93
94 static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
95                                    int __user *argp)
96 {
97         struct vhost_vring_state s = {
98                 .index = idx,
99                 .num = vq->user_be
100         };
101
102         if (copy_to_user(argp, &s, sizeof(s)))
103                 return -EFAULT;
104
105         return 0;
106 }
107
108 static void vhost_init_is_le(struct vhost_virtqueue *vq)
109 {
110         /* Note for legacy virtio: user_be is initialized at reset time
111          * according to the host endianness. If userspace does not set an
112          * explicit endianness, the default behavior is native endian, as
113          * expected by legacy virtio.
114          */
115         vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
116 }
117 #else
118 static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
119 {
120 }
121
122 static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
123 {
124         return -ENOIOCTLCMD;
125 }
126
127 static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
128                                    int __user *argp)
129 {
130         return -ENOIOCTLCMD;
131 }
132
133 static void vhost_init_is_le(struct vhost_virtqueue *vq)
134 {
135         vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1)
136                 || virtio_legacy_is_little_endian();
137 }
138 #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
139
140 static void vhost_reset_is_le(struct vhost_virtqueue *vq)
141 {
142         vhost_init_is_le(vq);
143 }
144
145 struct vhost_flush_struct {
146         struct vhost_work work;
147         struct completion wait_event;
148 };
149
150 static void vhost_flush_work(struct vhost_work *work)
151 {
152         struct vhost_flush_struct *s;
153
154         s = container_of(work, struct vhost_flush_struct, work);
155         complete(&s->wait_event);
156 }
157
158 static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
159                             poll_table *pt)
160 {
161         struct vhost_poll *poll;
162
163         poll = container_of(pt, struct vhost_poll, table);
164         poll->wqh = wqh;
165         add_wait_queue(wqh, &poll->wait);
166 }
167
168 static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync,
169                              void *key)
170 {
171         struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
172
173         if (!(key_to_poll(key) & poll->mask))
174                 return 0;
175
176         vhost_poll_queue(poll);
177         return 0;
178 }
179
180 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
181 {
182         clear_bit(VHOST_WORK_QUEUED, &work->flags);
183         work->fn = fn;
184 }
185 EXPORT_SYMBOL_GPL(vhost_work_init);
186
187 /* Init poll structure */
188 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
189                      __poll_t mask, struct vhost_dev *dev)
190 {
191         init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
192         init_poll_funcptr(&poll->table, vhost_poll_func);
193         poll->mask = mask;
194         poll->dev = dev;
195         poll->wqh = NULL;
196
197         vhost_work_init(&poll->work, fn);
198 }
199 EXPORT_SYMBOL_GPL(vhost_poll_init);
200
201 /* Start polling a file. We add ourselves to file's wait queue. The caller must
202  * keep a reference to a file until after vhost_poll_stop is called. */
203 int vhost_poll_start(struct vhost_poll *poll, struct file *file)
204 {
205         __poll_t mask;
206         int ret = 0;
207
208         if (poll->wqh)
209                 return 0;
210
211         mask = vfs_poll(file, &poll->table);
212         if (mask)
213                 vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask));
214         if (mask & EPOLLERR) {
215                 vhost_poll_stop(poll);
216                 ret = -EINVAL;
217         }
218
219         return ret;
220 }
221 EXPORT_SYMBOL_GPL(vhost_poll_start);
222
223 /* Stop polling a file. After this function returns, it becomes safe to drop the
224  * file reference. You must also flush afterwards. */
225 void vhost_poll_stop(struct vhost_poll *poll)
226 {
227         if (poll->wqh) {
228                 remove_wait_queue(poll->wqh, &poll->wait);
229                 poll->wqh = NULL;
230         }
231 }
232 EXPORT_SYMBOL_GPL(vhost_poll_stop);
233
234 void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
235 {
236         struct vhost_flush_struct flush;
237
238         if (dev->worker) {
239                 init_completion(&flush.wait_event);
240                 vhost_work_init(&flush.work, vhost_flush_work);
241
242                 vhost_work_queue(dev, &flush.work);
243                 wait_for_completion(&flush.wait_event);
244         }
245 }
246 EXPORT_SYMBOL_GPL(vhost_work_flush);
247
248 /* Flush any work that has been scheduled. When calling this, don't hold any
249  * locks that are also used by the callback. */
250 void vhost_poll_flush(struct vhost_poll *poll)
251 {
252         vhost_work_flush(poll->dev, &poll->work);
253 }
254 EXPORT_SYMBOL_GPL(vhost_poll_flush);
255
256 void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
257 {
258         if (!dev->worker)
259                 return;
260
261         if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) {
262                 /* We can only add the work to the list after we're
263                  * sure it was not in the list.
264                  * test_and_set_bit() implies a memory barrier.
265                  */
266                 llist_add(&work->node, &dev->work_list);
267                 wake_up_process(dev->worker);
268         }
269 }
270 EXPORT_SYMBOL_GPL(vhost_work_queue);
271
272 /* A lockless hint for busy polling code to exit the loop */
273 bool vhost_has_work(struct vhost_dev *dev)
274 {
275         return !llist_empty(&dev->work_list);
276 }
277 EXPORT_SYMBOL_GPL(vhost_has_work);
278
279 void vhost_poll_queue(struct vhost_poll *poll)
280 {
281         vhost_work_queue(poll->dev, &poll->work);
282 }
283 EXPORT_SYMBOL_GPL(vhost_poll_queue);
284
285 static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq)
286 {
287         int j;
288
289         for (j = 0; j < VHOST_NUM_ADDRS; j++)
290                 vq->meta_iotlb[j] = NULL;
291 }
292
293 static void vhost_vq_meta_reset(struct vhost_dev *d)
294 {
295         int i;
296
297         for (i = 0; i < d->nvqs; ++i)
298                 __vhost_vq_meta_reset(d->vqs[i]);
299 }
300
301 static void vhost_vq_reset(struct vhost_dev *dev,
302                            struct vhost_virtqueue *vq)
303 {
304         vq->num = 1;
305         vq->desc = NULL;
306         vq->avail = NULL;
307         vq->used = NULL;
308         vq->last_avail_idx = 0;
309         vq->avail_idx = 0;
310         vq->last_used_idx = 0;
311         vq->signalled_used = 0;
312         vq->signalled_used_valid = false;
313         vq->used_flags = 0;
314         vq->log_used = false;
315         vq->log_addr = -1ull;
316         vq->private_data = NULL;
317         vq->acked_features = 0;
318         vq->log_base = NULL;
319         vq->error_ctx = NULL;
320         vq->kick = NULL;
321         vq->call_ctx = NULL;
322         vq->log_ctx = NULL;
323         vhost_reset_is_le(vq);
324         vhost_disable_cross_endian(vq);
325         vq->busyloop_timeout = 0;
326         vq->umem = NULL;
327         vq->iotlb = NULL;
328         __vhost_vq_meta_reset(vq);
329 }
330
331 static int vhost_worker(void *data)
332 {
333         struct vhost_dev *dev = data;
334         struct vhost_work *work, *work_next;
335         struct llist_node *node;
336         mm_segment_t oldfs = get_fs();
337
338         set_fs(USER_DS);
339         use_mm(dev->mm);
340
341         for (;;) {
342                 /* mb paired w/ kthread_stop */
343                 set_current_state(TASK_INTERRUPTIBLE);
344
345                 if (kthread_should_stop()) {
346                         __set_current_state(TASK_RUNNING);
347                         break;
348                 }
349
350                 node = llist_del_all(&dev->work_list);
351                 if (!node)
352                         schedule();
353
354                 node = llist_reverse_order(node);
355                 /* make sure flag is seen after deletion */
356                 smp_wmb();
357                 llist_for_each_entry_safe(work, work_next, node, node) {
358                         clear_bit(VHOST_WORK_QUEUED, &work->flags);
359                         __set_current_state(TASK_RUNNING);
360                         work->fn(work);
361                         if (need_resched())
362                                 schedule();
363                 }
364         }
365         unuse_mm(dev->mm);
366         set_fs(oldfs);
367         return 0;
368 }
369
370 static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
371 {
372         kfree(vq->indirect);
373         vq->indirect = NULL;
374         kfree(vq->log);
375         vq->log = NULL;
376         kfree(vq->heads);
377         vq->heads = NULL;
378 }
379
380 /* Helper to allocate iovec buffers for all vqs. */
381 static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
382 {
383         struct vhost_virtqueue *vq;
384         int i;
385
386         for (i = 0; i < dev->nvqs; ++i) {
387                 vq = dev->vqs[i];
388                 vq->indirect = kmalloc_array(UIO_MAXIOV,
389                                              sizeof(*vq->indirect),
390                                              GFP_KERNEL);
391                 vq->log = kmalloc_array(UIO_MAXIOV, sizeof(*vq->log),
392                                         GFP_KERNEL);
393                 vq->heads = kmalloc_array(UIO_MAXIOV, sizeof(*vq->heads),
394                                           GFP_KERNEL);
395                 if (!vq->indirect || !vq->log || !vq->heads)
396                         goto err_nomem;
397         }
398         return 0;
399
400 err_nomem:
401         for (; i >= 0; --i)
402                 vhost_vq_free_iovecs(dev->vqs[i]);
403         return -ENOMEM;
404 }
405
406 static void vhost_dev_free_iovecs(struct vhost_dev *dev)
407 {
408         int i;
409
410         for (i = 0; i < dev->nvqs; ++i)
411                 vhost_vq_free_iovecs(dev->vqs[i]);
412 }
413
414 void vhost_dev_init(struct vhost_dev *dev,
415                     struct vhost_virtqueue **vqs, int nvqs)
416 {
417         struct vhost_virtqueue *vq;
418         int i;
419
420         dev->vqs = vqs;
421         dev->nvqs = nvqs;
422         mutex_init(&dev->mutex);
423         dev->log_ctx = NULL;
424         dev->umem = NULL;
425         dev->iotlb = NULL;
426         dev->mm = NULL;
427         dev->worker = NULL;
428         init_llist_head(&dev->work_list);
429         init_waitqueue_head(&dev->wait);
430         INIT_LIST_HEAD(&dev->read_list);
431         INIT_LIST_HEAD(&dev->pending_list);
432         spin_lock_init(&dev->iotlb_lock);
433
434
435         for (i = 0; i < dev->nvqs; ++i) {
436                 vq = dev->vqs[i];
437                 vq->log = NULL;
438                 vq->indirect = NULL;
439                 vq->heads = NULL;
440                 vq->dev = dev;
441                 mutex_init(&vq->mutex);
442                 vhost_vq_reset(dev, vq);
443                 if (vq->handle_kick)
444                         vhost_poll_init(&vq->poll, vq->handle_kick,
445                                         EPOLLIN, dev);
446         }
447 }
448 EXPORT_SYMBOL_GPL(vhost_dev_init);
449
450 /* Caller should have device mutex */
451 long vhost_dev_check_owner(struct vhost_dev *dev)
452 {
453         /* Are you the owner? If not, I don't think you mean to do that */
454         return dev->mm == current->mm ? 0 : -EPERM;
455 }
456 EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
457
458 struct vhost_attach_cgroups_struct {
459         struct vhost_work work;
460         struct task_struct *owner;
461         int ret;
462 };
463
464 static void vhost_attach_cgroups_work(struct vhost_work *work)
465 {
466         struct vhost_attach_cgroups_struct *s;
467
468         s = container_of(work, struct vhost_attach_cgroups_struct, work);
469         s->ret = cgroup_attach_task_all(s->owner, current);
470 }
471
472 static int vhost_attach_cgroups(struct vhost_dev *dev)
473 {
474         struct vhost_attach_cgroups_struct attach;
475
476         attach.owner = current;
477         vhost_work_init(&attach.work, vhost_attach_cgroups_work);
478         vhost_work_queue(dev, &attach.work);
479         vhost_work_flush(dev, &attach.work);
480         return attach.ret;
481 }
482
483 /* Caller should have device mutex */
484 bool vhost_dev_has_owner(struct vhost_dev *dev)
485 {
486         return dev->mm;
487 }
488 EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
489
490 /* Caller should have device mutex */
491 long vhost_dev_set_owner(struct vhost_dev *dev)
492 {
493         struct task_struct *worker;
494         int err;
495
496         /* Is there an owner already? */
497         if (vhost_dev_has_owner(dev)) {
498                 err = -EBUSY;
499                 goto err_mm;
500         }
501
502         /* No owner, become one */
503         dev->mm = get_task_mm(current);
504         worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
505         if (IS_ERR(worker)) {
506                 err = PTR_ERR(worker);
507                 goto err_worker;
508         }
509
510         dev->worker = worker;
511         wake_up_process(worker);        /* avoid contributing to loadavg */
512
513         err = vhost_attach_cgroups(dev);
514         if (err)
515                 goto err_cgroup;
516
517         err = vhost_dev_alloc_iovecs(dev);
518         if (err)
519                 goto err_cgroup;
520
521         return 0;
522 err_cgroup:
523         kthread_stop(worker);
524         dev->worker = NULL;
525 err_worker:
526         if (dev->mm)
527                 mmput(dev->mm);
528         dev->mm = NULL;
529 err_mm:
530         return err;
531 }
532 EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
533
534 struct vhost_umem *vhost_dev_reset_owner_prepare(void)
535 {
536         return kvzalloc(sizeof(struct vhost_umem), GFP_KERNEL);
537 }
538 EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
539
540 /* Caller should have device mutex */
541 void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_umem *umem)
542 {
543         int i;
544
545         vhost_dev_cleanup(dev);
546
547         /* Restore memory to default empty mapping. */
548         INIT_LIST_HEAD(&umem->umem_list);
549         dev->umem = umem;
550         /* We don't need VQ locks below since vhost_dev_cleanup makes sure
551          * VQs aren't running.
552          */
553         for (i = 0; i < dev->nvqs; ++i)
554                 dev->vqs[i]->umem = umem;
555 }
556 EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
557
558 void vhost_dev_stop(struct vhost_dev *dev)
559 {
560         int i;
561
562         for (i = 0; i < dev->nvqs; ++i) {
563                 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
564                         vhost_poll_stop(&dev->vqs[i]->poll);
565                         vhost_poll_flush(&dev->vqs[i]->poll);
566                 }
567         }
568 }
569 EXPORT_SYMBOL_GPL(vhost_dev_stop);
570
571 static void vhost_umem_free(struct vhost_umem *umem,
572                             struct vhost_umem_node *node)
573 {
574         vhost_umem_interval_tree_remove(node, &umem->umem_tree);
575         list_del(&node->link);
576         kfree(node);
577         umem->numem--;
578 }
579
580 static void vhost_umem_clean(struct vhost_umem *umem)
581 {
582         struct vhost_umem_node *node, *tmp;
583
584         if (!umem)
585                 return;
586
587         list_for_each_entry_safe(node, tmp, &umem->umem_list, link)
588                 vhost_umem_free(umem, node);
589
590         kvfree(umem);
591 }
592
593 static void vhost_clear_msg(struct vhost_dev *dev)
594 {
595         struct vhost_msg_node *node, *n;
596
597         spin_lock(&dev->iotlb_lock);
598
599         list_for_each_entry_safe(node, n, &dev->read_list, node) {
600                 list_del(&node->node);
601                 kfree(node);
602         }
603
604         list_for_each_entry_safe(node, n, &dev->pending_list, node) {
605                 list_del(&node->node);
606                 kfree(node);
607         }
608
609         spin_unlock(&dev->iotlb_lock);
610 }
611
612 void vhost_dev_cleanup(struct vhost_dev *dev)
613 {
614         int i;
615
616         for (i = 0; i < dev->nvqs; ++i) {
617                 if (dev->vqs[i]->error_ctx)
618                         eventfd_ctx_put(dev->vqs[i]->error_ctx);
619                 if (dev->vqs[i]->kick)
620                         fput(dev->vqs[i]->kick);
621                 if (dev->vqs[i]->call_ctx)
622                         eventfd_ctx_put(dev->vqs[i]->call_ctx);
623                 vhost_vq_reset(dev, dev->vqs[i]);
624         }
625         vhost_dev_free_iovecs(dev);
626         if (dev->log_ctx)
627                 eventfd_ctx_put(dev->log_ctx);
628         dev->log_ctx = NULL;
629         /* No one will access memory at this point */
630         vhost_umem_clean(dev->umem);
631         dev->umem = NULL;
632         vhost_umem_clean(dev->iotlb);
633         dev->iotlb = NULL;
634         vhost_clear_msg(dev);
635         wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
636         WARN_ON(!llist_empty(&dev->work_list));
637         if (dev->worker) {
638                 kthread_stop(dev->worker);
639                 dev->worker = NULL;
640         }
641         if (dev->mm)
642                 mmput(dev->mm);
643         dev->mm = NULL;
644 }
645 EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
646
647 static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
648 {
649         u64 a = addr / VHOST_PAGE_SIZE / 8;
650
651         /* Make sure 64 bit math will not overflow. */
652         if (a > ULONG_MAX - (unsigned long)log_base ||
653             a + (unsigned long)log_base > ULONG_MAX)
654                 return false;
655
656         return access_ok(VERIFY_WRITE, log_base + a,
657                          (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
658 }
659
660 static bool vhost_overflow(u64 uaddr, u64 size)
661 {
662         /* Make sure 64 bit math will not overflow. */
663         return uaddr > ULONG_MAX || size > ULONG_MAX || uaddr > ULONG_MAX - size;
664 }
665
666 /* Caller should have vq mutex and device mutex. */
667 static bool vq_memory_access_ok(void __user *log_base, struct vhost_umem *umem,
668                                 int log_all)
669 {
670         struct vhost_umem_node *node;
671
672         if (!umem)
673                 return false;
674
675         list_for_each_entry(node, &umem->umem_list, link) {
676                 unsigned long a = node->userspace_addr;
677
678                 if (vhost_overflow(node->userspace_addr, node->size))
679                         return false;
680
681
682                 if (!access_ok(VERIFY_WRITE, (void __user *)a,
683                                     node->size))
684                         return false;
685                 else if (log_all && !log_access_ok(log_base,
686                                                    node->start,
687                                                    node->size))
688                         return false;
689         }
690         return true;
691 }
692
693 static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq,
694                                                u64 addr, unsigned int size,
695                                                int type)
696 {
697         const struct vhost_umem_node *node = vq->meta_iotlb[type];
698
699         if (!node)
700                 return NULL;
701
702         return (void *)(uintptr_t)(node->userspace_addr + addr - node->start);
703 }
704
705 /* Can we switch to this memory table? */
706 /* Caller should have device mutex but not vq mutex */
707 static bool memory_access_ok(struct vhost_dev *d, struct vhost_umem *umem,
708                              int log_all)
709 {
710         int i;
711
712         for (i = 0; i < d->nvqs; ++i) {
713                 bool ok;
714                 bool log;
715
716                 mutex_lock(&d->vqs[i]->mutex);
717                 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
718                 /* If ring is inactive, will check when it's enabled. */
719                 if (d->vqs[i]->private_data)
720                         ok = vq_memory_access_ok(d->vqs[i]->log_base,
721                                                  umem, log);
722                 else
723                         ok = true;
724                 mutex_unlock(&d->vqs[i]->mutex);
725                 if (!ok)
726                         return false;
727         }
728         return true;
729 }
730
731 static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
732                           struct iovec iov[], int iov_size, int access);
733
734 static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to,
735                               const void *from, unsigned size)
736 {
737         int ret;
738
739         if (!vq->iotlb)
740                 return __copy_to_user(to, from, size);
741         else {
742                 /* This function should be called after iotlb
743                  * prefetch, which means we're sure that all vq
744                  * could be access through iotlb. So -EAGAIN should
745                  * not happen in this case.
746                  */
747                 struct iov_iter t;
748                 void __user *uaddr = vhost_vq_meta_fetch(vq,
749                                      (u64)(uintptr_t)to, size,
750                                      VHOST_ADDR_USED);
751
752                 if (uaddr)
753                         return __copy_to_user(uaddr, from, size);
754
755                 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov,
756                                      ARRAY_SIZE(vq->iotlb_iov),
757                                      VHOST_ACCESS_WO);
758                 if (ret < 0)
759                         goto out;
760                 iov_iter_init(&t, WRITE, vq->iotlb_iov, ret, size);
761                 ret = copy_to_iter(from, size, &t);
762                 if (ret == size)
763                         ret = 0;
764         }
765 out:
766         return ret;
767 }
768
769 static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to,
770                                 void __user *from, unsigned size)
771 {
772         int ret;
773
774         if (!vq->iotlb)
775                 return __copy_from_user(to, from, size);
776         else {
777                 /* This function should be called after iotlb
778                  * prefetch, which means we're sure that vq
779                  * could be access through iotlb. So -EAGAIN should
780                  * not happen in this case.
781                  */
782                 void __user *uaddr = vhost_vq_meta_fetch(vq,
783                                      (u64)(uintptr_t)from, size,
784                                      VHOST_ADDR_DESC);
785                 struct iov_iter f;
786
787                 if (uaddr)
788                         return __copy_from_user(to, uaddr, size);
789
790                 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov,
791                                      ARRAY_SIZE(vq->iotlb_iov),
792                                      VHOST_ACCESS_RO);
793                 if (ret < 0) {
794                         vq_err(vq, "IOTLB translation failure: uaddr "
795                                "%p size 0x%llx\n", from,
796                                (unsigned long long) size);
797                         goto out;
798                 }
799                 iov_iter_init(&f, READ, vq->iotlb_iov, ret, size);
800                 ret = copy_from_iter(to, size, &f);
801                 if (ret == size)
802                         ret = 0;
803         }
804
805 out:
806         return ret;
807 }
808
809 static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq,
810                                           void __user *addr, unsigned int size,
811                                           int type)
812 {
813         int ret;
814
815         ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov,
816                              ARRAY_SIZE(vq->iotlb_iov),
817                              VHOST_ACCESS_RO);
818         if (ret < 0) {
819                 vq_err(vq, "IOTLB translation failure: uaddr "
820                         "%p size 0x%llx\n", addr,
821                         (unsigned long long) size);
822                 return NULL;
823         }
824
825         if (ret != 1 || vq->iotlb_iov[0].iov_len != size) {
826                 vq_err(vq, "Non atomic userspace memory access: uaddr "
827                         "%p size 0x%llx\n", addr,
828                         (unsigned long long) size);
829                 return NULL;
830         }
831
832         return vq->iotlb_iov[0].iov_base;
833 }
834
835 /* This function should be called after iotlb
836  * prefetch, which means we're sure that vq
837  * could be access through iotlb. So -EAGAIN should
838  * not happen in this case.
839  */
840 static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq,
841                                             void *addr, unsigned int size,
842                                             int type)
843 {
844         void __user *uaddr = vhost_vq_meta_fetch(vq,
845                              (u64)(uintptr_t)addr, size, type);
846         if (uaddr)
847                 return uaddr;
848
849         return __vhost_get_user_slow(vq, addr, size, type);
850 }
851
852 #define vhost_put_user(vq, x, ptr)              \
853 ({ \
854         int ret = -EFAULT; \
855         if (!vq->iotlb) { \
856                 ret = __put_user(x, ptr); \
857         } else { \
858                 __typeof__(ptr) to = \
859                         (__typeof__(ptr)) __vhost_get_user(vq, ptr,     \
860                                           sizeof(*ptr), VHOST_ADDR_USED); \
861                 if (to != NULL) \
862                         ret = __put_user(x, to); \
863                 else \
864                         ret = -EFAULT;  \
865         } \
866         ret; \
867 })
868
869 #define vhost_get_user(vq, x, ptr, type)                \
870 ({ \
871         int ret; \
872         if (!vq->iotlb) { \
873                 ret = __get_user(x, ptr); \
874         } else { \
875                 __typeof__(ptr) from = \
876                         (__typeof__(ptr)) __vhost_get_user(vq, ptr, \
877                                                            sizeof(*ptr), \
878                                                            type); \
879                 if (from != NULL) \
880                         ret = __get_user(x, from); \
881                 else \
882                         ret = -EFAULT; \
883         } \
884         ret; \
885 })
886
887 #define vhost_get_avail(vq, x, ptr) \
888         vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL)
889
890 #define vhost_get_used(vq, x, ptr) \
891         vhost_get_user(vq, x, ptr, VHOST_ADDR_USED)
892
893 static void vhost_dev_lock_vqs(struct vhost_dev *d)
894 {
895         int i = 0;
896         for (i = 0; i < d->nvqs; ++i)
897                 mutex_lock_nested(&d->vqs[i]->mutex, i);
898 }
899
900 static void vhost_dev_unlock_vqs(struct vhost_dev *d)
901 {
902         int i = 0;
903         for (i = 0; i < d->nvqs; ++i)
904                 mutex_unlock(&d->vqs[i]->mutex);
905 }
906
907 static int vhost_new_umem_range(struct vhost_umem *umem,
908                                 u64 start, u64 size, u64 end,
909                                 u64 userspace_addr, int perm)
910 {
911         struct vhost_umem_node *tmp, *node = kmalloc(sizeof(*node), GFP_ATOMIC);
912
913         if (!node)
914                 return -ENOMEM;
915
916         if (umem->numem == max_iotlb_entries) {
917                 tmp = list_first_entry(&umem->umem_list, typeof(*tmp), link);
918                 vhost_umem_free(umem, tmp);
919         }
920
921         node->start = start;
922         node->size = size;
923         node->last = end;
924         node->userspace_addr = userspace_addr;
925         node->perm = perm;
926         INIT_LIST_HEAD(&node->link);
927         list_add_tail(&node->link, &umem->umem_list);
928         vhost_umem_interval_tree_insert(node, &umem->umem_tree);
929         umem->numem++;
930
931         return 0;
932 }
933
934 static void vhost_del_umem_range(struct vhost_umem *umem,
935                                  u64 start, u64 end)
936 {
937         struct vhost_umem_node *node;
938
939         while ((node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
940                                                            start, end)))
941                 vhost_umem_free(umem, node);
942 }
943
944 static void vhost_iotlb_notify_vq(struct vhost_dev *d,
945                                   struct vhost_iotlb_msg *msg)
946 {
947         struct vhost_msg_node *node, *n;
948
949         spin_lock(&d->iotlb_lock);
950
951         list_for_each_entry_safe(node, n, &d->pending_list, node) {
952                 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb;
953                 if (msg->iova <= vq_msg->iova &&
954                     msg->iova + msg->size - 1 > vq_msg->iova &&
955                     vq_msg->type == VHOST_IOTLB_MISS) {
956                         vhost_poll_queue(&node->vq->poll);
957                         list_del(&node->node);
958                         kfree(node);
959                 }
960         }
961
962         spin_unlock(&d->iotlb_lock);
963 }
964
965 static bool umem_access_ok(u64 uaddr, u64 size, int access)
966 {
967         unsigned long a = uaddr;
968
969         /* Make sure 64 bit math will not overflow. */
970         if (vhost_overflow(uaddr, size))
971                 return false;
972
973         if ((access & VHOST_ACCESS_RO) &&
974             !access_ok(VERIFY_READ, (void __user *)a, size))
975                 return false;
976         if ((access & VHOST_ACCESS_WO) &&
977             !access_ok(VERIFY_WRITE, (void __user *)a, size))
978                 return false;
979         return true;
980 }
981
982 static int vhost_process_iotlb_msg(struct vhost_dev *dev,
983                                    struct vhost_iotlb_msg *msg)
984 {
985         int ret = 0;
986
987         mutex_lock(&dev->mutex);
988         vhost_dev_lock_vqs(dev);
989         switch (msg->type) {
990         case VHOST_IOTLB_UPDATE:
991                 if (!dev->iotlb) {
992                         ret = -EFAULT;
993                         break;
994                 }
995                 if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) {
996                         ret = -EFAULT;
997                         break;
998                 }
999                 vhost_vq_meta_reset(dev);
1000                 if (vhost_new_umem_range(dev->iotlb, msg->iova, msg->size,
1001                                          msg->iova + msg->size - 1,
1002                                          msg->uaddr, msg->perm)) {
1003                         ret = -ENOMEM;
1004                         break;
1005                 }
1006                 vhost_iotlb_notify_vq(dev, msg);
1007                 break;
1008         case VHOST_IOTLB_INVALIDATE:
1009                 if (!dev->iotlb) {
1010                         ret = -EFAULT;
1011                         break;
1012                 }
1013                 vhost_vq_meta_reset(dev);
1014                 vhost_del_umem_range(dev->iotlb, msg->iova,
1015                                      msg->iova + msg->size - 1);
1016                 break;
1017         default:
1018                 ret = -EINVAL;
1019                 break;
1020         }
1021
1022         vhost_dev_unlock_vqs(dev);
1023         mutex_unlock(&dev->mutex);
1024
1025         return ret;
1026 }
1027 ssize_t vhost_chr_write_iter(struct vhost_dev *dev,
1028                              struct iov_iter *from)
1029 {
1030         struct vhost_msg_node node;
1031         unsigned size = sizeof(struct vhost_msg);
1032         size_t ret;
1033         int err;
1034
1035         if (iov_iter_count(from) < size)
1036                 return 0;
1037         ret = copy_from_iter(&node.msg, size, from);
1038         if (ret != size)
1039                 goto done;
1040
1041         switch (node.msg.type) {
1042         case VHOST_IOTLB_MSG:
1043                 err = vhost_process_iotlb_msg(dev, &node.msg.iotlb);
1044                 if (err)
1045                         ret = err;
1046                 break;
1047         default:
1048                 ret = -EINVAL;
1049                 break;
1050         }
1051
1052 done:
1053         return ret;
1054 }
1055 EXPORT_SYMBOL(vhost_chr_write_iter);
1056
1057 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev,
1058                             poll_table *wait)
1059 {
1060         __poll_t mask = 0;
1061
1062         poll_wait(file, &dev->wait, wait);
1063
1064         if (!list_empty(&dev->read_list))
1065                 mask |= EPOLLIN | EPOLLRDNORM;
1066
1067         return mask;
1068 }
1069 EXPORT_SYMBOL(vhost_chr_poll);
1070
1071 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to,
1072                             int noblock)
1073 {
1074         DEFINE_WAIT(wait);
1075         struct vhost_msg_node *node;
1076         ssize_t ret = 0;
1077         unsigned size = sizeof(struct vhost_msg);
1078
1079         if (iov_iter_count(to) < size)
1080                 return 0;
1081
1082         while (1) {
1083                 if (!noblock)
1084                         prepare_to_wait(&dev->wait, &wait,
1085                                         TASK_INTERRUPTIBLE);
1086
1087                 node = vhost_dequeue_msg(dev, &dev->read_list);
1088                 if (node)
1089                         break;
1090                 if (noblock) {
1091                         ret = -EAGAIN;
1092                         break;
1093                 }
1094                 if (signal_pending(current)) {
1095                         ret = -ERESTARTSYS;
1096                         break;
1097                 }
1098                 if (!dev->iotlb) {
1099                         ret = -EBADFD;
1100                         break;
1101                 }
1102
1103                 schedule();
1104         }
1105
1106         if (!noblock)
1107                 finish_wait(&dev->wait, &wait);
1108
1109         if (node) {
1110                 ret = copy_to_iter(&node->msg, size, to);
1111
1112                 if (ret != size || node->msg.type != VHOST_IOTLB_MISS) {
1113                         kfree(node);
1114                         return ret;
1115                 }
1116
1117                 vhost_enqueue_msg(dev, &dev->pending_list, node);
1118         }
1119
1120         return ret;
1121 }
1122 EXPORT_SYMBOL_GPL(vhost_chr_read_iter);
1123
1124 static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access)
1125 {
1126         struct vhost_dev *dev = vq->dev;
1127         struct vhost_msg_node *node;
1128         struct vhost_iotlb_msg *msg;
1129
1130         node = vhost_new_msg(vq, VHOST_IOTLB_MISS);
1131         if (!node)
1132                 return -ENOMEM;
1133
1134         msg = &node->msg.iotlb;
1135         msg->type = VHOST_IOTLB_MISS;
1136         msg->iova = iova;
1137         msg->perm = access;
1138
1139         vhost_enqueue_msg(dev, &dev->read_list, node);
1140
1141         return 0;
1142 }
1143
1144 static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
1145                          struct vring_desc __user *desc,
1146                          struct vring_avail __user *avail,
1147                          struct vring_used __user *used)
1148
1149 {
1150         size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1151
1152         return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
1153                access_ok(VERIFY_READ, avail,
1154                          sizeof *avail + num * sizeof *avail->ring + s) &&
1155                access_ok(VERIFY_WRITE, used,
1156                         sizeof *used + num * sizeof *used->ring + s);
1157 }
1158
1159 static void vhost_vq_meta_update(struct vhost_virtqueue *vq,
1160                                  const struct vhost_umem_node *node,
1161                                  int type)
1162 {
1163         int access = (type == VHOST_ADDR_USED) ?
1164                      VHOST_ACCESS_WO : VHOST_ACCESS_RO;
1165
1166         if (likely(node->perm & access))
1167                 vq->meta_iotlb[type] = node;
1168 }
1169
1170 static bool iotlb_access_ok(struct vhost_virtqueue *vq,
1171                             int access, u64 addr, u64 len, int type)
1172 {
1173         const struct vhost_umem_node *node;
1174         struct vhost_umem *umem = vq->iotlb;
1175         u64 s = 0, size, orig_addr = addr, last = addr + len - 1;
1176
1177         if (vhost_vq_meta_fetch(vq, addr, len, type))
1178                 return true;
1179
1180         while (len > s) {
1181                 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1182                                                            addr,
1183                                                            last);
1184                 if (node == NULL || node->start > addr) {
1185                         vhost_iotlb_miss(vq, addr, access);
1186                         return false;
1187                 } else if (!(node->perm & access)) {
1188                         /* Report the possible access violation by
1189                          * request another translation from userspace.
1190                          */
1191                         return false;
1192                 }
1193
1194                 size = node->size - addr + node->start;
1195
1196                 if (orig_addr == addr && size >= len)
1197                         vhost_vq_meta_update(vq, node, type);
1198
1199                 s += size;
1200                 addr += size;
1201         }
1202
1203         return true;
1204 }
1205
1206 int vq_iotlb_prefetch(struct vhost_virtqueue *vq)
1207 {
1208         size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1209         unsigned int num = vq->num;
1210
1211         if (!vq->iotlb)
1212                 return 1;
1213
1214         return iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->desc,
1215                                num * sizeof(*vq->desc), VHOST_ADDR_DESC) &&
1216                iotlb_access_ok(vq, VHOST_ACCESS_RO, (u64)(uintptr_t)vq->avail,
1217                                sizeof *vq->avail +
1218                                num * sizeof(*vq->avail->ring) + s,
1219                                VHOST_ADDR_AVAIL) &&
1220                iotlb_access_ok(vq, VHOST_ACCESS_WO, (u64)(uintptr_t)vq->used,
1221                                sizeof *vq->used +
1222                                num * sizeof(*vq->used->ring) + s,
1223                                VHOST_ADDR_USED);
1224 }
1225 EXPORT_SYMBOL_GPL(vq_iotlb_prefetch);
1226
1227 /* Can we log writes? */
1228 /* Caller should have device mutex but not vq mutex */
1229 bool vhost_log_access_ok(struct vhost_dev *dev)
1230 {
1231         return memory_access_ok(dev, dev->umem, 1);
1232 }
1233 EXPORT_SYMBOL_GPL(vhost_log_access_ok);
1234
1235 /* Verify access for write logging. */
1236 /* Caller should have vq mutex and device mutex */
1237 static bool vq_log_access_ok(struct vhost_virtqueue *vq,
1238                              void __user *log_base)
1239 {
1240         size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1241
1242         return vq_memory_access_ok(log_base, vq->umem,
1243                                    vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
1244                 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
1245                                         sizeof *vq->used +
1246                                         vq->num * sizeof *vq->used->ring + s));
1247 }
1248
1249 /* Can we start vq? */
1250 /* Caller should have vq mutex and device mutex */
1251 bool vhost_vq_access_ok(struct vhost_virtqueue *vq)
1252 {
1253         if (!vq_log_access_ok(vq, vq->log_base))
1254                 return false;
1255
1256         /* Access validation occurs at prefetch time with IOTLB */
1257         if (vq->iotlb)
1258                 return true;
1259
1260         return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used);
1261 }
1262 EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
1263
1264 static struct vhost_umem *vhost_umem_alloc(void)
1265 {
1266         struct vhost_umem *umem = kvzalloc(sizeof(*umem), GFP_KERNEL);
1267
1268         if (!umem)
1269                 return NULL;
1270
1271         umem->umem_tree = RB_ROOT_CACHED;
1272         umem->numem = 0;
1273         INIT_LIST_HEAD(&umem->umem_list);
1274
1275         return umem;
1276 }
1277
1278 static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
1279 {
1280         struct vhost_memory mem, *newmem;
1281         struct vhost_memory_region *region;
1282         struct vhost_umem *newumem, *oldumem;
1283         unsigned long size = offsetof(struct vhost_memory, regions);
1284         int i;
1285
1286         if (copy_from_user(&mem, m, size))
1287                 return -EFAULT;
1288         if (mem.padding)
1289                 return -EOPNOTSUPP;
1290         if (mem.nregions > max_mem_regions)
1291                 return -E2BIG;
1292         newmem = kvzalloc(struct_size(newmem, regions, mem.nregions),
1293                         GFP_KERNEL);
1294         if (!newmem)
1295                 return -ENOMEM;
1296
1297         memcpy(newmem, &mem, size);
1298         if (copy_from_user(newmem->regions, m->regions,
1299                            mem.nregions * sizeof *m->regions)) {
1300                 kvfree(newmem);
1301                 return -EFAULT;
1302         }
1303
1304         newumem = vhost_umem_alloc();
1305         if (!newumem) {
1306                 kvfree(newmem);
1307                 return -ENOMEM;
1308         }
1309
1310         for (region = newmem->regions;
1311              region < newmem->regions + mem.nregions;
1312              region++) {
1313                 if (vhost_new_umem_range(newumem,
1314                                          region->guest_phys_addr,
1315                                          region->memory_size,
1316                                          region->guest_phys_addr +
1317                                          region->memory_size - 1,
1318                                          region->userspace_addr,
1319                                          VHOST_ACCESS_RW))
1320                         goto err;
1321         }
1322
1323         if (!memory_access_ok(d, newumem, 0))
1324                 goto err;
1325
1326         oldumem = d->umem;
1327         d->umem = newumem;
1328
1329         /* All memory accesses are done under some VQ mutex. */
1330         for (i = 0; i < d->nvqs; ++i) {
1331                 mutex_lock(&d->vqs[i]->mutex);
1332                 d->vqs[i]->umem = newumem;
1333                 mutex_unlock(&d->vqs[i]->mutex);
1334         }
1335
1336         kvfree(newmem);
1337         vhost_umem_clean(oldumem);
1338         return 0;
1339
1340 err:
1341         vhost_umem_clean(newumem);
1342         kvfree(newmem);
1343         return -EFAULT;
1344 }
1345
1346 long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
1347 {
1348         struct file *eventfp, *filep = NULL;
1349         bool pollstart = false, pollstop = false;
1350         struct eventfd_ctx *ctx = NULL;
1351         u32 __user *idxp = argp;
1352         struct vhost_virtqueue *vq;
1353         struct vhost_vring_state s;
1354         struct vhost_vring_file f;
1355         struct vhost_vring_addr a;
1356         u32 idx;
1357         long r;
1358
1359         r = get_user(idx, idxp);
1360         if (r < 0)
1361                 return r;
1362         if (idx >= d->nvqs)
1363                 return -ENOBUFS;
1364
1365         vq = d->vqs[idx];
1366
1367         mutex_lock(&vq->mutex);
1368
1369         switch (ioctl) {
1370         case VHOST_SET_VRING_NUM:
1371                 /* Resizing ring with an active backend?
1372                  * You don't want to do that. */
1373                 if (vq->private_data) {
1374                         r = -EBUSY;
1375                         break;
1376                 }
1377                 if (copy_from_user(&s, argp, sizeof s)) {
1378                         r = -EFAULT;
1379                         break;
1380                 }
1381                 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
1382                         r = -EINVAL;
1383                         break;
1384                 }
1385                 vq->num = s.num;
1386                 break;
1387         case VHOST_SET_VRING_BASE:
1388                 /* Moving base with an active backend?
1389                  * You don't want to do that. */
1390                 if (vq->private_data) {
1391                         r = -EBUSY;
1392                         break;
1393                 }
1394                 if (copy_from_user(&s, argp, sizeof s)) {
1395                         r = -EFAULT;
1396                         break;
1397                 }
1398                 if (s.num > 0xffff) {
1399                         r = -EINVAL;
1400                         break;
1401                 }
1402                 vq->last_avail_idx = s.num;
1403                 /* Forget the cached index value. */
1404                 vq->avail_idx = vq->last_avail_idx;
1405                 break;
1406         case VHOST_GET_VRING_BASE:
1407                 s.index = idx;
1408                 s.num = vq->last_avail_idx;
1409                 if (copy_to_user(argp, &s, sizeof s))
1410                         r = -EFAULT;
1411                 break;
1412         case VHOST_SET_VRING_ADDR:
1413                 if (copy_from_user(&a, argp, sizeof a)) {
1414                         r = -EFAULT;
1415                         break;
1416                 }
1417                 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
1418                         r = -EOPNOTSUPP;
1419                         break;
1420                 }
1421                 /* For 32bit, verify that the top 32bits of the user
1422                    data are set to zero. */
1423                 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
1424                     (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
1425                     (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
1426                         r = -EFAULT;
1427                         break;
1428                 }
1429
1430                 /* Make sure it's safe to cast pointers to vring types. */
1431                 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
1432                 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
1433                 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
1434                     (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
1435                     (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
1436                         r = -EINVAL;
1437                         break;
1438                 }
1439
1440                 /* We only verify access here if backend is configured.
1441                  * If it is not, we don't as size might not have been setup.
1442                  * We will verify when backend is configured. */
1443                 if (vq->private_data) {
1444                         if (!vq_access_ok(vq, vq->num,
1445                                 (void __user *)(unsigned long)a.desc_user_addr,
1446                                 (void __user *)(unsigned long)a.avail_user_addr,
1447                                 (void __user *)(unsigned long)a.used_user_addr)) {
1448                                 r = -EINVAL;
1449                                 break;
1450                         }
1451
1452                         /* Also validate log access for used ring if enabled. */
1453                         if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
1454                             !log_access_ok(vq->log_base, a.log_guest_addr,
1455                                            sizeof *vq->used +
1456                                            vq->num * sizeof *vq->used->ring)) {
1457                                 r = -EINVAL;
1458                                 break;
1459                         }
1460                 }
1461
1462                 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
1463                 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
1464                 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
1465                 vq->log_addr = a.log_guest_addr;
1466                 vq->used = (void __user *)(unsigned long)a.used_user_addr;
1467                 break;
1468         case VHOST_SET_VRING_KICK:
1469                 if (copy_from_user(&f, argp, sizeof f)) {
1470                         r = -EFAULT;
1471                         break;
1472                 }
1473                 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
1474                 if (IS_ERR(eventfp)) {
1475                         r = PTR_ERR(eventfp);
1476                         break;
1477                 }
1478                 if (eventfp != vq->kick) {
1479                         pollstop = (filep = vq->kick) != NULL;
1480                         pollstart = (vq->kick = eventfp) != NULL;
1481                 } else
1482                         filep = eventfp;
1483                 break;
1484         case VHOST_SET_VRING_CALL:
1485                 if (copy_from_user(&f, argp, sizeof f)) {
1486                         r = -EFAULT;
1487                         break;
1488                 }
1489                 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1490                 if (IS_ERR(ctx)) {
1491                         r = PTR_ERR(ctx);
1492                         break;
1493                 }
1494                 swap(ctx, vq->call_ctx);
1495                 break;
1496         case VHOST_SET_VRING_ERR:
1497                 if (copy_from_user(&f, argp, sizeof f)) {
1498                         r = -EFAULT;
1499                         break;
1500                 }
1501                 ctx = f.fd == -1 ? NULL : eventfd_ctx_fdget(f.fd);
1502                 if (IS_ERR(ctx)) {
1503                         r = PTR_ERR(ctx);
1504                         break;
1505                 }
1506                 swap(ctx, vq->error_ctx);
1507                 break;
1508         case VHOST_SET_VRING_ENDIAN:
1509                 r = vhost_set_vring_endian(vq, argp);
1510                 break;
1511         case VHOST_GET_VRING_ENDIAN:
1512                 r = vhost_get_vring_endian(vq, idx, argp);
1513                 break;
1514         case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
1515                 if (copy_from_user(&s, argp, sizeof(s))) {
1516                         r = -EFAULT;
1517                         break;
1518                 }
1519                 vq->busyloop_timeout = s.num;
1520                 break;
1521         case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
1522                 s.index = idx;
1523                 s.num = vq->busyloop_timeout;
1524                 if (copy_to_user(argp, &s, sizeof(s)))
1525                         r = -EFAULT;
1526                 break;
1527         default:
1528                 r = -ENOIOCTLCMD;
1529         }
1530
1531         if (pollstop && vq->handle_kick)
1532                 vhost_poll_stop(&vq->poll);
1533
1534         if (!IS_ERR_OR_NULL(ctx))
1535                 eventfd_ctx_put(ctx);
1536         if (filep)
1537                 fput(filep);
1538
1539         if (pollstart && vq->handle_kick)
1540                 r = vhost_poll_start(&vq->poll, vq->kick);
1541
1542         mutex_unlock(&vq->mutex);
1543
1544         if (pollstop && vq->handle_kick)
1545                 vhost_poll_flush(&vq->poll);
1546         return r;
1547 }
1548 EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
1549
1550 int vhost_init_device_iotlb(struct vhost_dev *d, bool enabled)
1551 {
1552         struct vhost_umem *niotlb, *oiotlb;
1553         int i;
1554
1555         niotlb = vhost_umem_alloc();
1556         if (!niotlb)
1557                 return -ENOMEM;
1558
1559         oiotlb = d->iotlb;
1560         d->iotlb = niotlb;
1561
1562         for (i = 0; i < d->nvqs; ++i) {
1563                 struct vhost_virtqueue *vq = d->vqs[i];
1564
1565                 mutex_lock(&vq->mutex);
1566                 vq->iotlb = niotlb;
1567                 __vhost_vq_meta_reset(vq);
1568                 mutex_unlock(&vq->mutex);
1569         }
1570
1571         vhost_umem_clean(oiotlb);
1572
1573         return 0;
1574 }
1575 EXPORT_SYMBOL_GPL(vhost_init_device_iotlb);
1576
1577 /* Caller must have device mutex */
1578 long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
1579 {
1580         struct eventfd_ctx *ctx;
1581         u64 p;
1582         long r;
1583         int i, fd;
1584
1585         /* If you are not the owner, you can become one */
1586         if (ioctl == VHOST_SET_OWNER) {
1587                 r = vhost_dev_set_owner(d);
1588                 goto done;
1589         }
1590
1591         /* You must be the owner to do anything else */
1592         r = vhost_dev_check_owner(d);
1593         if (r)
1594                 goto done;
1595
1596         switch (ioctl) {
1597         case VHOST_SET_MEM_TABLE:
1598                 r = vhost_set_memory(d, argp);
1599                 break;
1600         case VHOST_SET_LOG_BASE:
1601                 if (copy_from_user(&p, argp, sizeof p)) {
1602                         r = -EFAULT;
1603                         break;
1604                 }
1605                 if ((u64)(unsigned long)p != p) {
1606                         r = -EFAULT;
1607                         break;
1608                 }
1609                 for (i = 0; i < d->nvqs; ++i) {
1610                         struct vhost_virtqueue *vq;
1611                         void __user *base = (void __user *)(unsigned long)p;
1612                         vq = d->vqs[i];
1613                         mutex_lock(&vq->mutex);
1614                         /* If ring is inactive, will check when it's enabled. */
1615                         if (vq->private_data && !vq_log_access_ok(vq, base))
1616                                 r = -EFAULT;
1617                         else
1618                                 vq->log_base = base;
1619                         mutex_unlock(&vq->mutex);
1620                 }
1621                 break;
1622         case VHOST_SET_LOG_FD:
1623                 r = get_user(fd, (int __user *)argp);
1624                 if (r < 0)
1625                         break;
1626                 ctx = fd == -1 ? NULL : eventfd_ctx_fdget(fd);
1627                 if (IS_ERR(ctx)) {
1628                         r = PTR_ERR(ctx);
1629                         break;
1630                 }
1631                 swap(ctx, d->log_ctx);
1632                 for (i = 0; i < d->nvqs; ++i) {
1633                         mutex_lock(&d->vqs[i]->mutex);
1634                         d->vqs[i]->log_ctx = d->log_ctx;
1635                         mutex_unlock(&d->vqs[i]->mutex);
1636                 }
1637                 if (ctx)
1638                         eventfd_ctx_put(ctx);
1639                 break;
1640         default:
1641                 r = -ENOIOCTLCMD;
1642                 break;
1643         }
1644 done:
1645         return r;
1646 }
1647 EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
1648
1649 /* TODO: This is really inefficient.  We need something like get_user()
1650  * (instruction directly accesses the data, with an exception table entry
1651  * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1652  */
1653 static int set_bit_to_user(int nr, void __user *addr)
1654 {
1655         unsigned long log = (unsigned long)addr;
1656         struct page *page;
1657         void *base;
1658         int bit = nr + (log % PAGE_SIZE) * 8;
1659         int r;
1660
1661         r = get_user_pages_fast(log, 1, 1, &page);
1662         if (r < 0)
1663                 return r;
1664         BUG_ON(r != 1);
1665         base = kmap_atomic(page);
1666         set_bit(bit, base);
1667         kunmap_atomic(base);
1668         set_page_dirty_lock(page);
1669         put_page(page);
1670         return 0;
1671 }
1672
1673 static int log_write(void __user *log_base,
1674                      u64 write_address, u64 write_length)
1675 {
1676         u64 write_page = write_address / VHOST_PAGE_SIZE;
1677         int r;
1678
1679         if (!write_length)
1680                 return 0;
1681         write_length += write_address % VHOST_PAGE_SIZE;
1682         for (;;) {
1683                 u64 base = (u64)(unsigned long)log_base;
1684                 u64 log = base + write_page / 8;
1685                 int bit = write_page % 8;
1686                 if ((u64)(unsigned long)log != log)
1687                         return -EFAULT;
1688                 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1689                 if (r < 0)
1690                         return r;
1691                 if (write_length <= VHOST_PAGE_SIZE)
1692                         break;
1693                 write_length -= VHOST_PAGE_SIZE;
1694                 write_page += 1;
1695         }
1696         return r;
1697 }
1698
1699 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1700                     unsigned int log_num, u64 len)
1701 {
1702         int i, r;
1703
1704         /* Make sure data written is seen before log. */
1705         smp_wmb();
1706         for (i = 0; i < log_num; ++i) {
1707                 u64 l = min(log[i].len, len);
1708                 r = log_write(vq->log_base, log[i].addr, l);
1709                 if (r < 0)
1710                         return r;
1711                 len -= l;
1712                 if (!len) {
1713                         if (vq->log_ctx)
1714                                 eventfd_signal(vq->log_ctx, 1);
1715                         return 0;
1716                 }
1717         }
1718         /* Length written exceeds what we have stored. This is a bug. */
1719         BUG();
1720         return 0;
1721 }
1722 EXPORT_SYMBOL_GPL(vhost_log_write);
1723
1724 static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1725 {
1726         void __user *used;
1727         if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags),
1728                            &vq->used->flags) < 0)
1729                 return -EFAULT;
1730         if (unlikely(vq->log_used)) {
1731                 /* Make sure the flag is seen before log. */
1732                 smp_wmb();
1733                 /* Log used flag write. */
1734                 used = &vq->used->flags;
1735                 log_write(vq->log_base, vq->log_addr +
1736                           (used - (void __user *)vq->used),
1737                           sizeof vq->used->flags);
1738                 if (vq->log_ctx)
1739                         eventfd_signal(vq->log_ctx, 1);
1740         }
1741         return 0;
1742 }
1743
1744 static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1745 {
1746         if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx),
1747                            vhost_avail_event(vq)))
1748                 return -EFAULT;
1749         if (unlikely(vq->log_used)) {
1750                 void __user *used;
1751                 /* Make sure the event is seen before log. */
1752                 smp_wmb();
1753                 /* Log avail event write */
1754                 used = vhost_avail_event(vq);
1755                 log_write(vq->log_base, vq->log_addr +
1756                           (used - (void __user *)vq->used),
1757                           sizeof *vhost_avail_event(vq));
1758                 if (vq->log_ctx)
1759                         eventfd_signal(vq->log_ctx, 1);
1760         }
1761         return 0;
1762 }
1763
1764 int vhost_vq_init_access(struct vhost_virtqueue *vq)
1765 {
1766         __virtio16 last_used_idx;
1767         int r;
1768         bool is_le = vq->is_le;
1769
1770         if (!vq->private_data)
1771                 return 0;
1772
1773         vhost_init_is_le(vq);
1774
1775         r = vhost_update_used_flags(vq);
1776         if (r)
1777                 goto err;
1778         vq->signalled_used_valid = false;
1779         if (!vq->iotlb &&
1780             !access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
1781                 r = -EFAULT;
1782                 goto err;
1783         }
1784         r = vhost_get_used(vq, last_used_idx, &vq->used->idx);
1785         if (r) {
1786                 vq_err(vq, "Can't access used idx at %p\n",
1787                        &vq->used->idx);
1788                 goto err;
1789         }
1790         vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
1791         return 0;
1792
1793 err:
1794         vq->is_le = is_le;
1795         return r;
1796 }
1797 EXPORT_SYMBOL_GPL(vhost_vq_init_access);
1798
1799 static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
1800                           struct iovec iov[], int iov_size, int access)
1801 {
1802         const struct vhost_umem_node *node;
1803         struct vhost_dev *dev = vq->dev;
1804         struct vhost_umem *umem = dev->iotlb ? dev->iotlb : dev->umem;
1805         struct iovec *_iov;
1806         u64 s = 0;
1807         int ret = 0;
1808
1809         while ((u64)len > s) {
1810                 u64 size;
1811                 if (unlikely(ret >= iov_size)) {
1812                         ret = -ENOBUFS;
1813                         break;
1814                 }
1815
1816                 node = vhost_umem_interval_tree_iter_first(&umem->umem_tree,
1817                                                         addr, addr + len - 1);
1818                 if (node == NULL || node->start > addr) {
1819                         if (umem != dev->iotlb) {
1820                                 ret = -EFAULT;
1821                                 break;
1822                         }
1823                         ret = -EAGAIN;
1824                         break;
1825                 } else if (!(node->perm & access)) {
1826                         ret = -EPERM;
1827                         break;
1828                 }
1829
1830                 _iov = iov + ret;
1831                 size = node->size - addr + node->start;
1832                 _iov->iov_len = min((u64)len - s, size);
1833                 _iov->iov_base = (void __user *)(unsigned long)
1834                         (node->userspace_addr + addr - node->start);
1835                 s += size;
1836                 addr += size;
1837                 ++ret;
1838         }
1839
1840         if (ret == -EAGAIN)
1841                 vhost_iotlb_miss(vq, addr, access);
1842         return ret;
1843 }
1844
1845 /* Each buffer in the virtqueues is actually a chain of descriptors.  This
1846  * function returns the next descriptor in the chain,
1847  * or -1U if we're at the end. */
1848 static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
1849 {
1850         unsigned int next;
1851
1852         /* If this descriptor says it doesn't chain, we're done. */
1853         if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
1854                 return -1U;
1855
1856         /* Check they're not leading us off end of descriptors. */
1857         next = vhost16_to_cpu(vq, READ_ONCE(desc->next));
1858         return next;
1859 }
1860
1861 static int get_indirect(struct vhost_virtqueue *vq,
1862                         struct iovec iov[], unsigned int iov_size,
1863                         unsigned int *out_num, unsigned int *in_num,
1864                         struct vhost_log *log, unsigned int *log_num,
1865                         struct vring_desc *indirect)
1866 {
1867         struct vring_desc desc;
1868         unsigned int i = 0, count, found = 0;
1869         u32 len = vhost32_to_cpu(vq, indirect->len);
1870         struct iov_iter from;
1871         int ret, access;
1872
1873         /* Sanity check */
1874         if (unlikely(len % sizeof desc)) {
1875                 vq_err(vq, "Invalid length in indirect descriptor: "
1876                        "len 0x%llx not multiple of 0x%zx\n",
1877                        (unsigned long long)len,
1878                        sizeof desc);
1879                 return -EINVAL;
1880         }
1881
1882         ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
1883                              UIO_MAXIOV, VHOST_ACCESS_RO);
1884         if (unlikely(ret < 0)) {
1885                 if (ret != -EAGAIN)
1886                         vq_err(vq, "Translation failure %d in indirect.\n", ret);
1887                 return ret;
1888         }
1889         iov_iter_init(&from, READ, vq->indirect, ret, len);
1890
1891         /* We will use the result as an address to read from, so most
1892          * architectures only need a compiler barrier here. */
1893         read_barrier_depends();
1894
1895         count = len / sizeof desc;
1896         /* Buffers are chained via a 16 bit next field, so
1897          * we can have at most 2^16 of these. */
1898         if (unlikely(count > USHRT_MAX + 1)) {
1899                 vq_err(vq, "Indirect buffer length too big: %d\n",
1900                        indirect->len);
1901                 return -E2BIG;
1902         }
1903
1904         do {
1905                 unsigned iov_count = *in_num + *out_num;
1906                 if (unlikely(++found > count)) {
1907                         vq_err(vq, "Loop detected: last one at %u "
1908                                "indirect size %u\n",
1909                                i, count);
1910                         return -EINVAL;
1911                 }
1912                 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) {
1913                         vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1914                                i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
1915                         return -EINVAL;
1916                 }
1917                 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
1918                         vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1919                                i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
1920                         return -EINVAL;
1921                 }
1922
1923                 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
1924                         access = VHOST_ACCESS_WO;
1925                 else
1926                         access = VHOST_ACCESS_RO;
1927
1928                 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1929                                      vhost32_to_cpu(vq, desc.len), iov + iov_count,
1930                                      iov_size - iov_count, access);
1931                 if (unlikely(ret < 0)) {
1932                         if (ret != -EAGAIN)
1933                                 vq_err(vq, "Translation failure %d indirect idx %d\n",
1934                                         ret, i);
1935                         return ret;
1936                 }
1937                 /* If this is an input descriptor, increment that count. */
1938                 if (access == VHOST_ACCESS_WO) {
1939                         *in_num += ret;
1940                         if (unlikely(log)) {
1941                                 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1942                                 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
1943                                 ++*log_num;
1944                         }
1945                 } else {
1946                         /* If it's an output descriptor, they're all supposed
1947                          * to come before any input descriptors. */
1948                         if (unlikely(*in_num)) {
1949                                 vq_err(vq, "Indirect descriptor "
1950                                        "has out after in: idx %d\n", i);
1951                                 return -EINVAL;
1952                         }
1953                         *out_num += ret;
1954                 }
1955         } while ((i = next_desc(vq, &desc)) != -1);
1956         return 0;
1957 }
1958
1959 /* This looks in the virtqueue and for the first available buffer, and converts
1960  * it to an iovec for convenient access.  Since descriptors consist of some
1961  * number of output then some number of input descriptors, it's actually two
1962  * iovecs, but we pack them into one and note how many of each there were.
1963  *
1964  * This function returns the descriptor number found, or vq->num (which is
1965  * never a valid descriptor number) if none was found.  A negative code is
1966  * returned on error. */
1967 int vhost_get_vq_desc(struct vhost_virtqueue *vq,
1968                       struct iovec iov[], unsigned int iov_size,
1969                       unsigned int *out_num, unsigned int *in_num,
1970                       struct vhost_log *log, unsigned int *log_num)
1971 {
1972         struct vring_desc desc;
1973         unsigned int i, head, found = 0;
1974         u16 last_avail_idx;
1975         __virtio16 avail_idx;
1976         __virtio16 ring_head;
1977         int ret, access;
1978
1979         /* Check it isn't doing very strange things with descriptor numbers. */
1980         last_avail_idx = vq->last_avail_idx;
1981
1982         if (vq->avail_idx == vq->last_avail_idx) {
1983                 if (unlikely(vhost_get_avail(vq, avail_idx, &vq->avail->idx))) {
1984                         vq_err(vq, "Failed to access avail idx at %p\n",
1985                                 &vq->avail->idx);
1986                         return -EFAULT;
1987                 }
1988                 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
1989
1990                 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
1991                         vq_err(vq, "Guest moved used index from %u to %u",
1992                                 last_avail_idx, vq->avail_idx);
1993                         return -EFAULT;
1994                 }
1995
1996                 /* If there's nothing new since last we looked, return
1997                  * invalid.
1998                  */
1999                 if (vq->avail_idx == last_avail_idx)
2000                         return vq->num;
2001
2002                 /* Only get avail ring entries after they have been
2003                  * exposed by guest.
2004                  */
2005                 smp_rmb();
2006         }
2007
2008         /* Grab the next descriptor number they're advertising, and increment
2009          * the index we've seen. */
2010         if (unlikely(vhost_get_avail(vq, ring_head,
2011                      &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
2012                 vq_err(vq, "Failed to read head: idx %d address %p\n",
2013                        last_avail_idx,
2014                        &vq->avail->ring[last_avail_idx % vq->num]);
2015                 return -EFAULT;
2016         }
2017
2018         head = vhost16_to_cpu(vq, ring_head);
2019
2020         /* If their number is silly, that's an error. */
2021         if (unlikely(head >= vq->num)) {
2022                 vq_err(vq, "Guest says index %u > %u is available",
2023                        head, vq->num);
2024                 return -EINVAL;
2025         }
2026
2027         /* When we start there are none of either input nor output. */
2028         *out_num = *in_num = 0;
2029         if (unlikely(log))
2030                 *log_num = 0;
2031
2032         i = head;
2033         do {
2034                 unsigned iov_count = *in_num + *out_num;
2035                 if (unlikely(i >= vq->num)) {
2036                         vq_err(vq, "Desc index is %u > %u, head = %u",
2037                                i, vq->num, head);
2038                         return -EINVAL;
2039                 }
2040                 if (unlikely(++found > vq->num)) {
2041                         vq_err(vq, "Loop detected: last one at %u "
2042                                "vq size %u head %u\n",
2043                                i, vq->num, head);
2044                         return -EINVAL;
2045                 }
2046                 ret = vhost_copy_from_user(vq, &desc, vq->desc + i,
2047                                            sizeof desc);
2048                 if (unlikely(ret)) {
2049                         vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
2050                                i, vq->desc + i);
2051                         return -EFAULT;
2052                 }
2053                 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
2054                         ret = get_indirect(vq, iov, iov_size,
2055                                            out_num, in_num,
2056                                            log, log_num, &desc);
2057                         if (unlikely(ret < 0)) {
2058                                 if (ret != -EAGAIN)
2059                                         vq_err(vq, "Failure detected "
2060                                                 "in indirect descriptor at idx %d\n", i);
2061                                 return ret;
2062                         }
2063                         continue;
2064                 }
2065
2066                 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE))
2067                         access = VHOST_ACCESS_WO;
2068                 else
2069                         access = VHOST_ACCESS_RO;
2070                 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
2071                                      vhost32_to_cpu(vq, desc.len), iov + iov_count,
2072                                      iov_size - iov_count, access);
2073                 if (unlikely(ret < 0)) {
2074                         if (ret != -EAGAIN)
2075                                 vq_err(vq, "Translation failure %d descriptor idx %d\n",
2076                                         ret, i);
2077                         return ret;
2078                 }
2079                 if (access == VHOST_ACCESS_WO) {
2080                         /* If this is an input descriptor,
2081                          * increment that count. */
2082                         *in_num += ret;
2083                         if (unlikely(log)) {
2084                                 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
2085                                 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
2086                                 ++*log_num;
2087                         }
2088                 } else {
2089                         /* If it's an output descriptor, they're all supposed
2090                          * to come before any input descriptors. */
2091                         if (unlikely(*in_num)) {
2092                                 vq_err(vq, "Descriptor has out after in: "
2093                                        "idx %d\n", i);
2094                                 return -EINVAL;
2095                         }
2096                         *out_num += ret;
2097                 }
2098         } while ((i = next_desc(vq, &desc)) != -1);
2099
2100         /* On success, increment avail index. */
2101         vq->last_avail_idx++;
2102
2103         /* Assume notifications from guest are disabled at this point,
2104          * if they aren't we would need to update avail_event index. */
2105         BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
2106         return head;
2107 }
2108 EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
2109
2110 /* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
2111 void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
2112 {
2113         vq->last_avail_idx -= n;
2114 }
2115 EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
2116
2117 /* After we've used one of their buffers, we tell them about it.  We'll then
2118  * want to notify the guest, using eventfd. */
2119 int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
2120 {
2121         struct vring_used_elem heads = {
2122                 cpu_to_vhost32(vq, head),
2123                 cpu_to_vhost32(vq, len)
2124         };
2125
2126         return vhost_add_used_n(vq, &heads, 1);
2127 }
2128 EXPORT_SYMBOL_GPL(vhost_add_used);
2129
2130 static int __vhost_add_used_n(struct vhost_virtqueue *vq,
2131                             struct vring_used_elem *heads,
2132                             unsigned count)
2133 {
2134         struct vring_used_elem __user *used;
2135         u16 old, new;
2136         int start;
2137
2138         start = vq->last_used_idx & (vq->num - 1);
2139         used = vq->used->ring + start;
2140         if (count == 1) {
2141                 if (vhost_put_user(vq, heads[0].id, &used->id)) {
2142                         vq_err(vq, "Failed to write used id");
2143                         return -EFAULT;
2144                 }
2145                 if (vhost_put_user(vq, heads[0].len, &used->len)) {
2146                         vq_err(vq, "Failed to write used len");
2147                         return -EFAULT;
2148                 }
2149         } else if (vhost_copy_to_user(vq, used, heads, count * sizeof *used)) {
2150                 vq_err(vq, "Failed to write used");
2151                 return -EFAULT;
2152         }
2153         if (unlikely(vq->log_used)) {
2154                 /* Make sure data is seen before log. */
2155                 smp_wmb();
2156                 /* Log used ring entry write. */
2157                 log_write(vq->log_base,
2158                           vq->log_addr +
2159                            ((void __user *)used - (void __user *)vq->used),
2160                           count * sizeof *used);
2161         }
2162         old = vq->last_used_idx;
2163         new = (vq->last_used_idx += count);
2164         /* If the driver never bothers to signal in a very long while,
2165          * used index might wrap around. If that happens, invalidate
2166          * signalled_used index we stored. TODO: make sure driver
2167          * signals at least once in 2^16 and remove this. */
2168         if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
2169                 vq->signalled_used_valid = false;
2170         return 0;
2171 }
2172
2173 /* After we've used one of their buffers, we tell them about it.  We'll then
2174  * want to notify the guest, using eventfd. */
2175 int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
2176                      unsigned count)
2177 {
2178         int start, n, r;
2179
2180         start = vq->last_used_idx & (vq->num - 1);
2181         n = vq->num - start;
2182         if (n < count) {
2183                 r = __vhost_add_used_n(vq, heads, n);
2184                 if (r < 0)
2185                         return r;
2186                 heads += n;
2187                 count -= n;
2188         }
2189         r = __vhost_add_used_n(vq, heads, count);
2190
2191         /* Make sure buffer is written before we update index. */
2192         smp_wmb();
2193         if (vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx),
2194                            &vq->used->idx)) {
2195                 vq_err(vq, "Failed to increment used idx");
2196                 return -EFAULT;
2197         }
2198         if (unlikely(vq->log_used)) {
2199                 /* Log used index update. */
2200                 log_write(vq->log_base,
2201                           vq->log_addr + offsetof(struct vring_used, idx),
2202                           sizeof vq->used->idx);
2203                 if (vq->log_ctx)
2204                         eventfd_signal(vq->log_ctx, 1);
2205         }
2206         return r;
2207 }
2208 EXPORT_SYMBOL_GPL(vhost_add_used_n);
2209
2210 static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2211 {
2212         __u16 old, new;
2213         __virtio16 event;
2214         bool v;
2215         /* Flush out used index updates. This is paired
2216          * with the barrier that the Guest executes when enabling
2217          * interrupts. */
2218         smp_mb();
2219
2220         if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
2221             unlikely(vq->avail_idx == vq->last_avail_idx))
2222                 return true;
2223
2224         if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
2225                 __virtio16 flags;
2226                 if (vhost_get_avail(vq, flags, &vq->avail->flags)) {
2227                         vq_err(vq, "Failed to get flags");
2228                         return true;
2229                 }
2230                 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
2231         }
2232         old = vq->signalled_used;
2233         v = vq->signalled_used_valid;
2234         new = vq->signalled_used = vq->last_used_idx;
2235         vq->signalled_used_valid = true;
2236
2237         if (unlikely(!v))
2238                 return true;
2239
2240         if (vhost_get_avail(vq, event, vhost_used_event(vq))) {
2241                 vq_err(vq, "Failed to get used event idx");
2242                 return true;
2243         }
2244         return vring_need_event(vhost16_to_cpu(vq, event), new, old);
2245 }
2246
2247 /* This actually signals the guest, using eventfd. */
2248 void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2249 {
2250         /* Signal the Guest tell them we used something up. */
2251         if (vq->call_ctx && vhost_notify(dev, vq))
2252                 eventfd_signal(vq->call_ctx, 1);
2253 }
2254 EXPORT_SYMBOL_GPL(vhost_signal);
2255
2256 /* And here's the combo meal deal.  Supersize me! */
2257 void vhost_add_used_and_signal(struct vhost_dev *dev,
2258                                struct vhost_virtqueue *vq,
2259                                unsigned int head, int len)
2260 {
2261         vhost_add_used(vq, head, len);
2262         vhost_signal(dev, vq);
2263 }
2264 EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
2265
2266 /* multi-buffer version of vhost_add_used_and_signal */
2267 void vhost_add_used_and_signal_n(struct vhost_dev *dev,
2268                                  struct vhost_virtqueue *vq,
2269                                  struct vring_used_elem *heads, unsigned count)
2270 {
2271         vhost_add_used_n(vq, heads, count);
2272         vhost_signal(dev, vq);
2273 }
2274 EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
2275
2276 /* return true if we're sure that avaiable ring is empty */
2277 bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2278 {
2279         __virtio16 avail_idx;
2280         int r;
2281
2282         if (vq->avail_idx != vq->last_avail_idx)
2283                 return false;
2284
2285         r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
2286         if (unlikely(r))
2287                 return false;
2288         vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
2289
2290         return vq->avail_idx == vq->last_avail_idx;
2291 }
2292 EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
2293
2294 /* OK, now we need to know about added descriptors. */
2295 bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2296 {
2297         __virtio16 avail_idx;
2298         int r;
2299
2300         if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
2301                 return false;
2302         vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
2303         if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
2304                 r = vhost_update_used_flags(vq);
2305                 if (r) {
2306                         vq_err(vq, "Failed to enable notification at %p: %d\n",
2307                                &vq->used->flags, r);
2308                         return false;
2309                 }
2310         } else {
2311                 r = vhost_update_avail_event(vq, vq->avail_idx);
2312                 if (r) {
2313                         vq_err(vq, "Failed to update avail event index at %p: %d\n",
2314                                vhost_avail_event(vq), r);
2315                         return false;
2316                 }
2317         }
2318         /* They could have slipped one in as we were doing that: make
2319          * sure it's written, then check again. */
2320         smp_mb();
2321         r = vhost_get_avail(vq, avail_idx, &vq->avail->idx);
2322         if (r) {
2323                 vq_err(vq, "Failed to check avail idx at %p: %d\n",
2324                        &vq->avail->idx, r);
2325                 return false;
2326         }
2327
2328         return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
2329 }
2330 EXPORT_SYMBOL_GPL(vhost_enable_notify);
2331
2332 /* We don't need to be notified again. */
2333 void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
2334 {
2335         int r;
2336
2337         if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
2338                 return;
2339         vq->used_flags |= VRING_USED_F_NO_NOTIFY;
2340         if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
2341                 r = vhost_update_used_flags(vq);
2342                 if (r)
2343                         vq_err(vq, "Failed to enable notification at %p: %d\n",
2344                                &vq->used->flags, r);
2345         }
2346 }
2347 EXPORT_SYMBOL_GPL(vhost_disable_notify);
2348
2349 /* Create a new message. */
2350 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type)
2351 {
2352         struct vhost_msg_node *node = kmalloc(sizeof *node, GFP_KERNEL);
2353         if (!node)
2354                 return NULL;
2355
2356         /* Make sure all padding within the structure is initialized. */
2357         memset(&node->msg, 0, sizeof node->msg);
2358         node->vq = vq;
2359         node->msg.type = type;
2360         return node;
2361 }
2362 EXPORT_SYMBOL_GPL(vhost_new_msg);
2363
2364 void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head,
2365                        struct vhost_msg_node *node)
2366 {
2367         spin_lock(&dev->iotlb_lock);
2368         list_add_tail(&node->node, head);
2369         spin_unlock(&dev->iotlb_lock);
2370
2371         wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM);
2372 }
2373 EXPORT_SYMBOL_GPL(vhost_enqueue_msg);
2374
2375 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev,
2376                                          struct list_head *head)
2377 {
2378         struct vhost_msg_node *node = NULL;
2379
2380         spin_lock(&dev->iotlb_lock);
2381         if (!list_empty(head)) {
2382                 node = list_first_entry(head, struct vhost_msg_node,
2383                                         node);
2384                 list_del(&node->node);
2385         }
2386         spin_unlock(&dev->iotlb_lock);
2387
2388         return node;
2389 }
2390 EXPORT_SYMBOL_GPL(vhost_dequeue_msg);
2391
2392
2393 static int __init vhost_init(void)
2394 {
2395         return 0;
2396 }
2397
2398 static void __exit vhost_exit(void)
2399 {
2400 }
2401
2402 module_init(vhost_init);
2403 module_exit(vhost_exit);
2404
2405 MODULE_VERSION("0.0.1");
2406 MODULE_LICENSE("GPL v2");
2407 MODULE_AUTHOR("Michael S. Tsirkin");
2408 MODULE_DESCRIPTION("Host kernel accelerator for virtio");