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