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