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