vhost: simplify work flushing
[linux-2.6-block.git] / drivers / vhost / vhost.c
CommitLineData
3a4d5c94
MT
1/* Copyright (C) 2009 Red Hat, Inc.
2 * Copyright (C) 2006 Rusty Russell IBM Corporation
3 *
4 * Author: Michael S. Tsirkin <mst@redhat.com>
5 *
6 * Inspiration, some code, and most witty comments come from
61516587 7 * Documentation/virtual/lguest/lguest.c, by Rusty Russell
3a4d5c94
MT
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 *
11 * Generic code for virtio server in host kernel.
12 */
13
14#include <linux/eventfd.h>
15#include <linux/vhost.h>
35596b27 16#include <linux/uio.h>
3a4d5c94 17#include <linux/mm.h>
64e1c807 18#include <linux/mmu_context.h>
3a4d5c94
MT
19#include <linux/miscdevice.h>
20#include <linux/mutex.h>
3a4d5c94
MT
21#include <linux/poll.h>
22#include <linux/file.h>
23#include <linux/highmem.h>
5a0e3ad6 24#include <linux/slab.h>
4de7255f 25#include <linux/vmalloc.h>
c23f3445 26#include <linux/kthread.h>
9e3d1957 27#include <linux/cgroup.h>
6ac1afbf 28#include <linux/module.h>
bcfeacab 29#include <linux/sort.h>
3a4d5c94 30
3a4d5c94
MT
31#include "vhost.h"
32
c9ce42f7
IM
33static ushort max_mem_regions = 64;
34module_param(max_mem_regions, ushort, 0444);
35MODULE_PARM_DESC(max_mem_regions,
36 "Maximum number of memory regions in memory map. (default: 64)");
37
3a4d5c94 38enum {
3a4d5c94
MT
39 VHOST_MEMORY_F_LOG = 0x1,
40};
41
3b1bbe89
MT
42#define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num])
43#define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num])
8ea8cf89 44
2751c988 45#ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY
c5072037 46static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
2751c988
GK
47{
48 vq->user_be = !virtio_legacy_is_little_endian();
49}
50
c5072037
GK
51static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq)
52{
53 vq->user_be = true;
54}
55
56static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq)
57{
58 vq->user_be = false;
59}
60
2751c988
GK
61static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
62{
63 struct vhost_vring_state s;
64
65 if (vq->private_data)
66 return -EBUSY;
67
68 if (copy_from_user(&s, argp, sizeof(s)))
69 return -EFAULT;
70
71 if (s.num != VHOST_VRING_LITTLE_ENDIAN &&
72 s.num != VHOST_VRING_BIG_ENDIAN)
73 return -EINVAL;
74
c5072037
GK
75 if (s.num == VHOST_VRING_BIG_ENDIAN)
76 vhost_enable_cross_endian_big(vq);
77 else
78 vhost_enable_cross_endian_little(vq);
2751c988
GK
79
80 return 0;
81}
82
83static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
84 int __user *argp)
85{
86 struct vhost_vring_state s = {
87 .index = idx,
88 .num = vq->user_be
89 };
90
91 if (copy_to_user(argp, &s, sizeof(s)))
92 return -EFAULT;
93
94 return 0;
95}
96
97static void vhost_init_is_le(struct vhost_virtqueue *vq)
98{
99 /* Note for legacy virtio: user_be is initialized at reset time
100 * according to the host endianness. If userspace does not set an
101 * explicit endianness, the default behavior is native endian, as
102 * expected by legacy virtio.
103 */
104 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be;
105}
106#else
c5072037 107static void vhost_disable_cross_endian(struct vhost_virtqueue *vq)
2751c988
GK
108{
109}
110
111static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp)
112{
113 return -ENOIOCTLCMD;
114}
115
116static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx,
117 int __user *argp)
118{
119 return -ENOIOCTLCMD;
120}
121
122static void vhost_init_is_le(struct vhost_virtqueue *vq)
123{
124 if (vhost_has_feature(vq, VIRTIO_F_VERSION_1))
125 vq->is_le = true;
126}
127#endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */
128
c5072037
GK
129static void vhost_reset_is_le(struct vhost_virtqueue *vq)
130{
131 vq->is_le = virtio_legacy_is_little_endian();
132}
133
7235acdb
JW
134struct vhost_flush_struct {
135 struct vhost_work work;
136 struct completion wait_event;
137};
138
139static void vhost_flush_work(struct vhost_work *work)
140{
141 struct vhost_flush_struct *s;
142
143 s = container_of(work, struct vhost_flush_struct, work);
144 complete(&s->wait_event);
145}
146
3a4d5c94
MT
147static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
148 poll_table *pt)
149{
150 struct vhost_poll *poll;
3a4d5c94 151
d47effe1 152 poll = container_of(pt, struct vhost_poll, table);
3a4d5c94
MT
153 poll->wqh = wqh;
154 add_wait_queue(wqh, &poll->wait);
155}
156
157static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
158 void *key)
159{
c23f3445
TH
160 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
161
3a4d5c94
MT
162 if (!((unsigned long)key & poll->mask))
163 return 0;
164
c23f3445 165 vhost_poll_queue(poll);
3a4d5c94
MT
166 return 0;
167}
168
163049ae 169void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
87d6a412
MT
170{
171 INIT_LIST_HEAD(&work->node);
172 work->fn = fn;
173 init_waitqueue_head(&work->done);
87d6a412 174}
6ac1afbf 175EXPORT_SYMBOL_GPL(vhost_work_init);
87d6a412 176
3a4d5c94 177/* Init poll structure */
c23f3445
TH
178void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
179 unsigned long mask, struct vhost_dev *dev)
3a4d5c94 180{
3a4d5c94
MT
181 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
182 init_poll_funcptr(&poll->table, vhost_poll_func);
183 poll->mask = mask;
c23f3445 184 poll->dev = dev;
2b8b328b 185 poll->wqh = NULL;
c23f3445 186
87d6a412 187 vhost_work_init(&poll->work, fn);
3a4d5c94 188}
6ac1afbf 189EXPORT_SYMBOL_GPL(vhost_poll_init);
3a4d5c94
MT
190
191/* Start polling a file. We add ourselves to file's wait queue. The caller must
192 * keep a reference to a file until after vhost_poll_stop is called. */
2b8b328b 193int vhost_poll_start(struct vhost_poll *poll, struct file *file)
3a4d5c94
MT
194{
195 unsigned long mask;
2b8b328b 196 int ret = 0;
d47effe1 197
70181d51
JW
198 if (poll->wqh)
199 return 0;
200
3a4d5c94
MT
201 mask = file->f_op->poll(file, &poll->table);
202 if (mask)
203 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
2b8b328b
JW
204 if (mask & POLLERR) {
205 if (poll->wqh)
206 remove_wait_queue(poll->wqh, &poll->wait);
207 ret = -EINVAL;
208 }
209
210 return ret;
3a4d5c94 211}
6ac1afbf 212EXPORT_SYMBOL_GPL(vhost_poll_start);
3a4d5c94
MT
213
214/* Stop polling a file. After this function returns, it becomes safe to drop the
215 * file reference. You must also flush afterwards. */
216void vhost_poll_stop(struct vhost_poll *poll)
217{
2b8b328b
JW
218 if (poll->wqh) {
219 remove_wait_queue(poll->wqh, &poll->wait);
220 poll->wqh = NULL;
221 }
3a4d5c94 222}
6ac1afbf 223EXPORT_SYMBOL_GPL(vhost_poll_stop);
3a4d5c94 224
7235acdb 225void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
0174b0c3 226{
7235acdb 227 struct vhost_flush_struct flush;
d47effe1 228
7235acdb
JW
229 if (dev->worker) {
230 init_completion(&flush.wait_event);
231 vhost_work_init(&flush.work, vhost_flush_work);
0174b0c3 232
7235acdb
JW
233 vhost_work_queue(dev, &flush.work);
234 wait_for_completion(&flush.wait_event);
235 }
3a4d5c94 236}
6ac1afbf 237EXPORT_SYMBOL_GPL(vhost_work_flush);
3a4d5c94 238
87d6a412
MT
239/* Flush any work that has been scheduled. When calling this, don't hold any
240 * locks that are also used by the callback. */
241void vhost_poll_flush(struct vhost_poll *poll)
242{
243 vhost_work_flush(poll->dev, &poll->work);
244}
6ac1afbf 245EXPORT_SYMBOL_GPL(vhost_poll_flush);
87d6a412 246
163049ae 247void vhost_work_queue(struct vhost_dev *dev, struct vhost_work *work)
3a4d5c94 248{
c23f3445
TH
249 unsigned long flags;
250
251 spin_lock_irqsave(&dev->work_lock, flags);
252 if (list_empty(&work->node)) {
253 list_add_tail(&work->node, &dev->work_list);
ac9fde24 254 spin_unlock_irqrestore(&dev->work_lock, flags);
c23f3445 255 wake_up_process(dev->worker);
ac9fde24
QC
256 } else {
257 spin_unlock_irqrestore(&dev->work_lock, flags);
c23f3445 258 }
3a4d5c94 259}
6ac1afbf 260EXPORT_SYMBOL_GPL(vhost_work_queue);
3a4d5c94 261
526d3e7f
JW
262/* A lockless hint for busy polling code to exit the loop */
263bool vhost_has_work(struct vhost_dev *dev)
264{
265 return !list_empty(&dev->work_list);
266}
267EXPORT_SYMBOL_GPL(vhost_has_work);
268
87d6a412
MT
269void vhost_poll_queue(struct vhost_poll *poll)
270{
271 vhost_work_queue(poll->dev, &poll->work);
272}
6ac1afbf 273EXPORT_SYMBOL_GPL(vhost_poll_queue);
87d6a412 274
3a4d5c94
MT
275static void vhost_vq_reset(struct vhost_dev *dev,
276 struct vhost_virtqueue *vq)
277{
278 vq->num = 1;
279 vq->desc = NULL;
280 vq->avail = NULL;
281 vq->used = NULL;
282 vq->last_avail_idx = 0;
283 vq->avail_idx = 0;
284 vq->last_used_idx = 0;
8ea8cf89
MT
285 vq->signalled_used = 0;
286 vq->signalled_used_valid = false;
3a4d5c94 287 vq->used_flags = 0;
3a4d5c94
MT
288 vq->log_used = false;
289 vq->log_addr = -1ull;
3a4d5c94 290 vq->private_data = NULL;
ea16c514 291 vq->acked_features = 0;
3a4d5c94
MT
292 vq->log_base = NULL;
293 vq->error_ctx = NULL;
294 vq->error = NULL;
295 vq->kick = NULL;
296 vq->call_ctx = NULL;
297 vq->call = NULL;
73a99f08 298 vq->log_ctx = NULL;
47283bef 299 vq->memory = NULL;
c5072037
GK
300 vhost_reset_is_le(vq);
301 vhost_disable_cross_endian(vq);
03088137 302 vq->busyloop_timeout = 0;
3a4d5c94
MT
303}
304
c23f3445
TH
305static int vhost_worker(void *data)
306{
307 struct vhost_dev *dev = data;
308 struct vhost_work *work = NULL;
d7ffde35 309 mm_segment_t oldfs = get_fs();
c23f3445 310
d7ffde35 311 set_fs(USER_DS);
64e1c807
MT
312 use_mm(dev->mm);
313
c23f3445
TH
314 for (;;) {
315 /* mb paired w/ kthread_stop */
316 set_current_state(TASK_INTERRUPTIBLE);
317
318 spin_lock_irq(&dev->work_lock);
c23f3445
TH
319
320 if (kthread_should_stop()) {
321 spin_unlock_irq(&dev->work_lock);
322 __set_current_state(TASK_RUNNING);
64e1c807 323 break;
c23f3445
TH
324 }
325 if (!list_empty(&dev->work_list)) {
326 work = list_first_entry(&dev->work_list,
327 struct vhost_work, node);
328 list_del_init(&work->node);
c23f3445
TH
329 } else
330 work = NULL;
331 spin_unlock_irq(&dev->work_lock);
332
333 if (work) {
334 __set_current_state(TASK_RUNNING);
335 work->fn(work);
d550dda1
NHE
336 if (need_resched())
337 schedule();
c23f3445
TH
338 } else
339 schedule();
340
341 }
64e1c807 342 unuse_mm(dev->mm);
d7ffde35 343 set_fs(oldfs);
64e1c807 344 return 0;
c23f3445
TH
345}
346
bab632d6
MT
347static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq)
348{
349 kfree(vq->indirect);
350 vq->indirect = NULL;
351 kfree(vq->log);
352 vq->log = NULL;
353 kfree(vq->heads);
354 vq->heads = NULL;
bab632d6
MT
355}
356
e0e9b406
JW
357/* Helper to allocate iovec buffers for all vqs. */
358static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
359{
6d5e6aa8 360 struct vhost_virtqueue *vq;
e0e9b406 361 int i;
d47effe1 362
e0e9b406 363 for (i = 0; i < dev->nvqs; ++i) {
6d5e6aa8
AH
364 vq = dev->vqs[i];
365 vq->indirect = kmalloc(sizeof *vq->indirect * UIO_MAXIOV,
366 GFP_KERNEL);
367 vq->log = kmalloc(sizeof *vq->log * UIO_MAXIOV, GFP_KERNEL);
368 vq->heads = kmalloc(sizeof *vq->heads * UIO_MAXIOV, GFP_KERNEL);
369 if (!vq->indirect || !vq->log || !vq->heads)
e0e9b406
JW
370 goto err_nomem;
371 }
372 return 0;
d47effe1 373
e0e9b406 374err_nomem:
bab632d6 375 for (; i >= 0; --i)
3ab2e420 376 vhost_vq_free_iovecs(dev->vqs[i]);
e0e9b406
JW
377 return -ENOMEM;
378}
379
380static void vhost_dev_free_iovecs(struct vhost_dev *dev)
381{
382 int i;
d47effe1 383
bab632d6 384 for (i = 0; i < dev->nvqs; ++i)
3ab2e420 385 vhost_vq_free_iovecs(dev->vqs[i]);
e0e9b406
JW
386}
387
59566b6e 388void vhost_dev_init(struct vhost_dev *dev,
3ab2e420 389 struct vhost_virtqueue **vqs, int nvqs)
3a4d5c94 390{
6d5e6aa8 391 struct vhost_virtqueue *vq;
3a4d5c94 392 int i;
c23f3445 393
3a4d5c94
MT
394 dev->vqs = vqs;
395 dev->nvqs = nvqs;
396 mutex_init(&dev->mutex);
397 dev->log_ctx = NULL;
398 dev->log_file = NULL;
399 dev->memory = NULL;
400 dev->mm = NULL;
c23f3445
TH
401 spin_lock_init(&dev->work_lock);
402 INIT_LIST_HEAD(&dev->work_list);
403 dev->worker = NULL;
3a4d5c94
MT
404
405 for (i = 0; i < dev->nvqs; ++i) {
6d5e6aa8
AH
406 vq = dev->vqs[i];
407 vq->log = NULL;
408 vq->indirect = NULL;
409 vq->heads = NULL;
410 vq->dev = dev;
411 mutex_init(&vq->mutex);
412 vhost_vq_reset(dev, vq);
413 if (vq->handle_kick)
414 vhost_poll_init(&vq->poll, vq->handle_kick,
415 POLLIN, dev);
3a4d5c94 416 }
3a4d5c94 417}
6ac1afbf 418EXPORT_SYMBOL_GPL(vhost_dev_init);
3a4d5c94
MT
419
420/* Caller should have device mutex */
421long vhost_dev_check_owner(struct vhost_dev *dev)
422{
423 /* Are you the owner? If not, I don't think you mean to do that */
424 return dev->mm == current->mm ? 0 : -EPERM;
425}
6ac1afbf 426EXPORT_SYMBOL_GPL(vhost_dev_check_owner);
3a4d5c94 427
87d6a412 428struct vhost_attach_cgroups_struct {
d47effe1
KK
429 struct vhost_work work;
430 struct task_struct *owner;
431 int ret;
87d6a412
MT
432};
433
434static void vhost_attach_cgroups_work(struct vhost_work *work)
435{
d47effe1
KK
436 struct vhost_attach_cgroups_struct *s;
437
438 s = container_of(work, struct vhost_attach_cgroups_struct, work);
439 s->ret = cgroup_attach_task_all(s->owner, current);
87d6a412
MT
440}
441
442static int vhost_attach_cgroups(struct vhost_dev *dev)
443{
d47effe1
KK
444 struct vhost_attach_cgroups_struct attach;
445
446 attach.owner = current;
447 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
448 vhost_work_queue(dev, &attach.work);
449 vhost_work_flush(dev, &attach.work);
450 return attach.ret;
87d6a412
MT
451}
452
05c05351
MT
453/* Caller should have device mutex */
454bool vhost_dev_has_owner(struct vhost_dev *dev)
455{
456 return dev->mm;
457}
6ac1afbf 458EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
05c05351 459
3a4d5c94 460/* Caller should have device mutex */
54db63c2 461long vhost_dev_set_owner(struct vhost_dev *dev)
3a4d5c94 462{
c23f3445
TH
463 struct task_struct *worker;
464 int err;
d47effe1 465
3a4d5c94 466 /* Is there an owner already? */
05c05351 467 if (vhost_dev_has_owner(dev)) {
c23f3445
TH
468 err = -EBUSY;
469 goto err_mm;
470 }
d47effe1 471
3a4d5c94
MT
472 /* No owner, become one */
473 dev->mm = get_task_mm(current);
c23f3445
TH
474 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
475 if (IS_ERR(worker)) {
476 err = PTR_ERR(worker);
477 goto err_worker;
478 }
479
480 dev->worker = worker;
87d6a412
MT
481 wake_up_process(worker); /* avoid contributing to loadavg */
482
483 err = vhost_attach_cgroups(dev);
9e3d1957
MT
484 if (err)
485 goto err_cgroup;
c23f3445 486
e0e9b406
JW
487 err = vhost_dev_alloc_iovecs(dev);
488 if (err)
489 goto err_cgroup;
490
3a4d5c94 491 return 0;
9e3d1957
MT
492err_cgroup:
493 kthread_stop(worker);
615cc221 494 dev->worker = NULL;
c23f3445
TH
495err_worker:
496 if (dev->mm)
497 mmput(dev->mm);
498 dev->mm = NULL;
499err_mm:
500 return err;
3a4d5c94 501}
6ac1afbf 502EXPORT_SYMBOL_GPL(vhost_dev_set_owner);
3a4d5c94 503
150b9e51 504struct vhost_memory *vhost_dev_reset_owner_prepare(void)
3a4d5c94 505{
150b9e51
MT
506 return kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
507}
6ac1afbf 508EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare);
3a4d5c94 509
150b9e51
MT
510/* Caller should have device mutex */
511void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_memory *memory)
512{
47283bef
MT
513 int i;
514
ea5d4046 515 vhost_dev_cleanup(dev, true);
3a4d5c94 516
150b9e51 517 /* Restore memory to default empty mapping. */
3a4d5c94 518 memory->nregions = 0;
47283bef
MT
519 dev->memory = memory;
520 /* We don't need VQ locks below since vhost_dev_cleanup makes sure
521 * VQs aren't running.
522 */
523 for (i = 0; i < dev->nvqs; ++i)
524 dev->vqs[i]->memory = memory;
3a4d5c94 525}
6ac1afbf 526EXPORT_SYMBOL_GPL(vhost_dev_reset_owner);
3a4d5c94 527
b211616d 528void vhost_dev_stop(struct vhost_dev *dev)
bab632d6
MT
529{
530 int i;
b211616d
MT
531
532 for (i = 0; i < dev->nvqs; ++i) {
3ab2e420
AH
533 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) {
534 vhost_poll_stop(&dev->vqs[i]->poll);
535 vhost_poll_flush(&dev->vqs[i]->poll);
b211616d 536 }
bab632d6 537 }
bab632d6 538}
6ac1afbf 539EXPORT_SYMBOL_GPL(vhost_dev_stop);
bab632d6 540
ea5d4046
MT
541/* Caller should have device mutex if and only if locked is set */
542void vhost_dev_cleanup(struct vhost_dev *dev, bool locked)
3a4d5c94
MT
543{
544 int i;
d47effe1 545
3a4d5c94 546 for (i = 0; i < dev->nvqs; ++i) {
3ab2e420
AH
547 if (dev->vqs[i]->error_ctx)
548 eventfd_ctx_put(dev->vqs[i]->error_ctx);
549 if (dev->vqs[i]->error)
550 fput(dev->vqs[i]->error);
551 if (dev->vqs[i]->kick)
552 fput(dev->vqs[i]->kick);
553 if (dev->vqs[i]->call_ctx)
554 eventfd_ctx_put(dev->vqs[i]->call_ctx);
555 if (dev->vqs[i]->call)
556 fput(dev->vqs[i]->call);
557 vhost_vq_reset(dev, dev->vqs[i]);
3a4d5c94 558 }
e0e9b406 559 vhost_dev_free_iovecs(dev);
3a4d5c94
MT
560 if (dev->log_ctx)
561 eventfd_ctx_put(dev->log_ctx);
562 dev->log_ctx = NULL;
563 if (dev->log_file)
564 fput(dev->log_file);
565 dev->log_file = NULL;
566 /* No one will access memory at this point */
4de7255f 567 kvfree(dev->memory);
47283bef 568 dev->memory = NULL;
c23f3445 569 WARN_ON(!list_empty(&dev->work_list));
78b620ce
ED
570 if (dev->worker) {
571 kthread_stop(dev->worker);
572 dev->worker = NULL;
573 }
533a19b4
MT
574 if (dev->mm)
575 mmput(dev->mm);
576 dev->mm = NULL;
3a4d5c94 577}
6ac1afbf 578EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
3a4d5c94
MT
579
580static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
581{
582 u64 a = addr / VHOST_PAGE_SIZE / 8;
d47effe1 583
3a4d5c94
MT
584 /* Make sure 64 bit math will not overflow. */
585 if (a > ULONG_MAX - (unsigned long)log_base ||
586 a + (unsigned long)log_base > ULONG_MAX)
6d97e55f 587 return 0;
3a4d5c94
MT
588
589 return access_ok(VERIFY_WRITE, log_base + a,
590 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
591}
592
593/* Caller should have vq mutex and device mutex. */
594static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
595 int log_all)
596{
597 int i;
179b284e 598
f8322fbe
MT
599 if (!mem)
600 return 0;
179b284e 601
3a4d5c94
MT
602 for (i = 0; i < mem->nregions; ++i) {
603 struct vhost_memory_region *m = mem->regions + i;
604 unsigned long a = m->userspace_addr;
605 if (m->memory_size > ULONG_MAX)
606 return 0;
607 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
608 m->memory_size))
609 return 0;
610 else if (log_all && !log_access_ok(log_base,
611 m->guest_phys_addr,
612 m->memory_size))
613 return 0;
614 }
615 return 1;
616}
617
618/* Can we switch to this memory table? */
619/* Caller should have device mutex but not vq mutex */
620static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
621 int log_all)
622{
623 int i;
d47effe1 624
3a4d5c94
MT
625 for (i = 0; i < d->nvqs; ++i) {
626 int ok;
ea16c514
MT
627 bool log;
628
3ab2e420 629 mutex_lock(&d->vqs[i]->mutex);
ea16c514 630 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL);
3a4d5c94 631 /* If ring is inactive, will check when it's enabled. */
3ab2e420 632 if (d->vqs[i]->private_data)
ea16c514 633 ok = vq_memory_access_ok(d->vqs[i]->log_base, mem, log);
3a4d5c94
MT
634 else
635 ok = 1;
3ab2e420 636 mutex_unlock(&d->vqs[i]->mutex);
3a4d5c94
MT
637 if (!ok)
638 return 0;
639 }
640 return 1;
641}
642
ea16c514 643static int vq_access_ok(struct vhost_virtqueue *vq, unsigned int num,
3a4d5c94
MT
644 struct vring_desc __user *desc,
645 struct vring_avail __user *avail,
646 struct vring_used __user *used)
647{
ea16c514 648 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
3a4d5c94
MT
649 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
650 access_ok(VERIFY_READ, avail,
8ea8cf89 651 sizeof *avail + num * sizeof *avail->ring + s) &&
3a4d5c94 652 access_ok(VERIFY_WRITE, used,
8ea8cf89 653 sizeof *used + num * sizeof *used->ring + s);
3a4d5c94
MT
654}
655
656/* Can we log writes? */
657/* Caller should have device mutex but not vq mutex */
658int vhost_log_access_ok(struct vhost_dev *dev)
659{
47283bef 660 return memory_access_ok(dev, dev->memory, 1);
3a4d5c94 661}
6ac1afbf 662EXPORT_SYMBOL_GPL(vhost_log_access_ok);
3a4d5c94
MT
663
664/* Verify access for write logging. */
665/* Caller should have vq mutex and device mutex */
ea16c514 666static int vq_log_access_ok(struct vhost_virtqueue *vq,
8ea8cf89 667 void __user *log_base)
3a4d5c94 668{
ea16c514 669 size_t s = vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
28457ee6 670
47283bef 671 return vq_memory_access_ok(log_base, vq->memory,
ea16c514 672 vhost_has_feature(vq, VHOST_F_LOG_ALL)) &&
3a4d5c94
MT
673 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
674 sizeof *vq->used +
8ea8cf89 675 vq->num * sizeof *vq->used->ring + s));
3a4d5c94
MT
676}
677
678/* Can we start vq? */
679/* Caller should have vq mutex and device mutex */
680int vhost_vq_access_ok(struct vhost_virtqueue *vq)
681{
ea16c514
MT
682 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used) &&
683 vq_log_access_ok(vq, vq->log_base);
3a4d5c94 684}
6ac1afbf 685EXPORT_SYMBOL_GPL(vhost_vq_access_ok);
3a4d5c94 686
bcfeacab
IM
687static int vhost_memory_reg_sort_cmp(const void *p1, const void *p2)
688{
689 const struct vhost_memory_region *r1 = p1, *r2 = p2;
690 if (r1->guest_phys_addr < r2->guest_phys_addr)
691 return 1;
692 if (r1->guest_phys_addr > r2->guest_phys_addr)
693 return -1;
694 return 0;
695}
696
4de7255f
IM
697static void *vhost_kvzalloc(unsigned long size)
698{
699 void *n = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
700
1e099473 701 if (!n)
4de7255f 702 n = vzalloc(size);
4de7255f
IM
703 return n;
704}
705
3a4d5c94
MT
706static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
707{
708 struct vhost_memory mem, *newmem, *oldmem;
709 unsigned long size = offsetof(struct vhost_memory, regions);
98f9ca0a 710 int i;
d47effe1 711
7ad9c9d2
TY
712 if (copy_from_user(&mem, m, size))
713 return -EFAULT;
3a4d5c94
MT
714 if (mem.padding)
715 return -EOPNOTSUPP;
c9ce42f7 716 if (mem.nregions > max_mem_regions)
3a4d5c94 717 return -E2BIG;
4de7255f 718 newmem = vhost_kvzalloc(size + mem.nregions * sizeof(*m->regions));
3a4d5c94
MT
719 if (!newmem)
720 return -ENOMEM;
721
722 memcpy(newmem, &mem, size);
7ad9c9d2
TY
723 if (copy_from_user(newmem->regions, m->regions,
724 mem.nregions * sizeof *m->regions)) {
bcfeacab 725 kvfree(newmem);
7ad9c9d2 726 return -EFAULT;
3a4d5c94 727 }
bcfeacab
IM
728 sort(newmem->regions, newmem->nregions, sizeof(*newmem->regions),
729 vhost_memory_reg_sort_cmp, NULL);
3a4d5c94 730
ea16c514 731 if (!memory_access_ok(d, newmem, 0)) {
4de7255f 732 kvfree(newmem);
3a4d5c94 733 return -EFAULT;
a02c3789 734 }
47283bef
MT
735 oldmem = d->memory;
736 d->memory = newmem;
98f9ca0a 737
47283bef 738 /* All memory accesses are done under some VQ mutex. */
98f9ca0a
MT
739 for (i = 0; i < d->nvqs; ++i) {
740 mutex_lock(&d->vqs[i]->mutex);
47283bef 741 d->vqs[i]->memory = newmem;
98f9ca0a
MT
742 mutex_unlock(&d->vqs[i]->mutex);
743 }
4de7255f 744 kvfree(oldmem);
3a4d5c94
MT
745 return 0;
746}
747
935cdee7 748long vhost_vring_ioctl(struct vhost_dev *d, int ioctl, void __user *argp)
3a4d5c94 749{
cecb46f1
AV
750 struct file *eventfp, *filep = NULL;
751 bool pollstart = false, pollstop = false;
3a4d5c94
MT
752 struct eventfd_ctx *ctx = NULL;
753 u32 __user *idxp = argp;
754 struct vhost_virtqueue *vq;
755 struct vhost_vring_state s;
756 struct vhost_vring_file f;
757 struct vhost_vring_addr a;
758 u32 idx;
759 long r;
760
761 r = get_user(idx, idxp);
762 if (r < 0)
763 return r;
0f3d9a17 764 if (idx >= d->nvqs)
3a4d5c94
MT
765 return -ENOBUFS;
766
3ab2e420 767 vq = d->vqs[idx];
3a4d5c94
MT
768
769 mutex_lock(&vq->mutex);
770
771 switch (ioctl) {
772 case VHOST_SET_VRING_NUM:
773 /* Resizing ring with an active backend?
774 * You don't want to do that. */
775 if (vq->private_data) {
776 r = -EBUSY;
777 break;
778 }
7ad9c9d2
TY
779 if (copy_from_user(&s, argp, sizeof s)) {
780 r = -EFAULT;
3a4d5c94 781 break;
7ad9c9d2 782 }
3a4d5c94
MT
783 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
784 r = -EINVAL;
785 break;
786 }
787 vq->num = s.num;
788 break;
789 case VHOST_SET_VRING_BASE:
790 /* Moving base with an active backend?
791 * You don't want to do that. */
792 if (vq->private_data) {
793 r = -EBUSY;
794 break;
795 }
7ad9c9d2
TY
796 if (copy_from_user(&s, argp, sizeof s)) {
797 r = -EFAULT;
3a4d5c94 798 break;
7ad9c9d2 799 }
3a4d5c94
MT
800 if (s.num > 0xffff) {
801 r = -EINVAL;
802 break;
803 }
804 vq->last_avail_idx = s.num;
805 /* Forget the cached index value. */
806 vq->avail_idx = vq->last_avail_idx;
807 break;
808 case VHOST_GET_VRING_BASE:
809 s.index = idx;
810 s.num = vq->last_avail_idx;
7ad9c9d2
TY
811 if (copy_to_user(argp, &s, sizeof s))
812 r = -EFAULT;
3a4d5c94
MT
813 break;
814 case VHOST_SET_VRING_ADDR:
7ad9c9d2
TY
815 if (copy_from_user(&a, argp, sizeof a)) {
816 r = -EFAULT;
3a4d5c94 817 break;
7ad9c9d2 818 }
3a4d5c94
MT
819 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
820 r = -EOPNOTSUPP;
821 break;
822 }
823 /* For 32bit, verify that the top 32bits of the user
824 data are set to zero. */
825 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
826 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
827 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
828 r = -EFAULT;
829 break;
830 }
5d9a07b0
MT
831
832 /* Make sure it's safe to cast pointers to vring types. */
833 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE);
834 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE);
835 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) ||
836 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) ||
d5424838 837 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) {
3a4d5c94
MT
838 r = -EINVAL;
839 break;
840 }
841
842 /* We only verify access here if backend is configured.
843 * If it is not, we don't as size might not have been setup.
844 * We will verify when backend is configured. */
845 if (vq->private_data) {
ea16c514 846 if (!vq_access_ok(vq, vq->num,
3a4d5c94
MT
847 (void __user *)(unsigned long)a.desc_user_addr,
848 (void __user *)(unsigned long)a.avail_user_addr,
849 (void __user *)(unsigned long)a.used_user_addr)) {
850 r = -EINVAL;
851 break;
852 }
853
854 /* Also validate log access for used ring if enabled. */
855 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
856 !log_access_ok(vq->log_base, a.log_guest_addr,
857 sizeof *vq->used +
858 vq->num * sizeof *vq->used->ring)) {
859 r = -EINVAL;
860 break;
861 }
862 }
863
3a4d5c94
MT
864 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
865 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
866 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
867 vq->log_addr = a.log_guest_addr;
868 vq->used = (void __user *)(unsigned long)a.used_user_addr;
869 break;
870 case VHOST_SET_VRING_KICK:
7ad9c9d2
TY
871 if (copy_from_user(&f, argp, sizeof f)) {
872 r = -EFAULT;
3a4d5c94 873 break;
7ad9c9d2 874 }
3a4d5c94 875 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
535297a6
MT
876 if (IS_ERR(eventfp)) {
877 r = PTR_ERR(eventfp);
878 break;
879 }
3a4d5c94 880 if (eventfp != vq->kick) {
cecb46f1
AV
881 pollstop = (filep = vq->kick) != NULL;
882 pollstart = (vq->kick = eventfp) != NULL;
3a4d5c94
MT
883 } else
884 filep = eventfp;
885 break;
886 case VHOST_SET_VRING_CALL:
7ad9c9d2
TY
887 if (copy_from_user(&f, argp, sizeof f)) {
888 r = -EFAULT;
3a4d5c94 889 break;
7ad9c9d2 890 }
3a4d5c94 891 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
535297a6
MT
892 if (IS_ERR(eventfp)) {
893 r = PTR_ERR(eventfp);
894 break;
895 }
3a4d5c94
MT
896 if (eventfp != vq->call) {
897 filep = vq->call;
898 ctx = vq->call_ctx;
899 vq->call = eventfp;
900 vq->call_ctx = eventfp ?
901 eventfd_ctx_fileget(eventfp) : NULL;
902 } else
903 filep = eventfp;
904 break;
905 case VHOST_SET_VRING_ERR:
7ad9c9d2
TY
906 if (copy_from_user(&f, argp, sizeof f)) {
907 r = -EFAULT;
3a4d5c94 908 break;
7ad9c9d2 909 }
3a4d5c94 910 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
535297a6
MT
911 if (IS_ERR(eventfp)) {
912 r = PTR_ERR(eventfp);
913 break;
914 }
3a4d5c94
MT
915 if (eventfp != vq->error) {
916 filep = vq->error;
917 vq->error = eventfp;
918 ctx = vq->error_ctx;
919 vq->error_ctx = eventfp ?
920 eventfd_ctx_fileget(eventfp) : NULL;
921 } else
922 filep = eventfp;
923 break;
2751c988
GK
924 case VHOST_SET_VRING_ENDIAN:
925 r = vhost_set_vring_endian(vq, argp);
926 break;
927 case VHOST_GET_VRING_ENDIAN:
928 r = vhost_get_vring_endian(vq, idx, argp);
929 break;
03088137
JW
930 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:
931 if (copy_from_user(&s, argp, sizeof(s))) {
932 r = -EFAULT;
933 break;
934 }
935 vq->busyloop_timeout = s.num;
936 break;
937 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:
938 s.index = idx;
939 s.num = vq->busyloop_timeout;
940 if (copy_to_user(argp, &s, sizeof(s)))
941 r = -EFAULT;
942 break;
3a4d5c94
MT
943 default:
944 r = -ENOIOCTLCMD;
945 }
946
947 if (pollstop && vq->handle_kick)
948 vhost_poll_stop(&vq->poll);
949
950 if (ctx)
951 eventfd_ctx_put(ctx);
952 if (filep)
953 fput(filep);
954
955 if (pollstart && vq->handle_kick)
2b8b328b 956 r = vhost_poll_start(&vq->poll, vq->kick);
3a4d5c94
MT
957
958 mutex_unlock(&vq->mutex);
959
960 if (pollstop && vq->handle_kick)
961 vhost_poll_flush(&vq->poll);
962 return r;
963}
6ac1afbf 964EXPORT_SYMBOL_GPL(vhost_vring_ioctl);
3a4d5c94
MT
965
966/* Caller must have device mutex */
935cdee7 967long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp)
3a4d5c94 968{
3a4d5c94
MT
969 struct file *eventfp, *filep = NULL;
970 struct eventfd_ctx *ctx = NULL;
971 u64 p;
972 long r;
973 int i, fd;
974
975 /* If you are not the owner, you can become one */
976 if (ioctl == VHOST_SET_OWNER) {
977 r = vhost_dev_set_owner(d);
978 goto done;
979 }
980
981 /* You must be the owner to do anything else */
982 r = vhost_dev_check_owner(d);
983 if (r)
984 goto done;
985
986 switch (ioctl) {
987 case VHOST_SET_MEM_TABLE:
988 r = vhost_set_memory(d, argp);
989 break;
990 case VHOST_SET_LOG_BASE:
7ad9c9d2
TY
991 if (copy_from_user(&p, argp, sizeof p)) {
992 r = -EFAULT;
3a4d5c94 993 break;
7ad9c9d2 994 }
3a4d5c94
MT
995 if ((u64)(unsigned long)p != p) {
996 r = -EFAULT;
997 break;
998 }
999 for (i = 0; i < d->nvqs; ++i) {
1000 struct vhost_virtqueue *vq;
1001 void __user *base = (void __user *)(unsigned long)p;
3ab2e420 1002 vq = d->vqs[i];
3a4d5c94
MT
1003 mutex_lock(&vq->mutex);
1004 /* If ring is inactive, will check when it's enabled. */
ea16c514 1005 if (vq->private_data && !vq_log_access_ok(vq, base))
3a4d5c94
MT
1006 r = -EFAULT;
1007 else
1008 vq->log_base = base;
1009 mutex_unlock(&vq->mutex);
1010 }
1011 break;
1012 case VHOST_SET_LOG_FD:
1013 r = get_user(fd, (int __user *)argp);
1014 if (r < 0)
1015 break;
1016 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
1017 if (IS_ERR(eventfp)) {
1018 r = PTR_ERR(eventfp);
1019 break;
1020 }
1021 if (eventfp != d->log_file) {
1022 filep = d->log_file;
7932c0bd 1023 d->log_file = eventfp;
3a4d5c94
MT
1024 ctx = d->log_ctx;
1025 d->log_ctx = eventfp ?
1026 eventfd_ctx_fileget(eventfp) : NULL;
1027 } else
1028 filep = eventfp;
1029 for (i = 0; i < d->nvqs; ++i) {
3ab2e420
AH
1030 mutex_lock(&d->vqs[i]->mutex);
1031 d->vqs[i]->log_ctx = d->log_ctx;
1032 mutex_unlock(&d->vqs[i]->mutex);
3a4d5c94
MT
1033 }
1034 if (ctx)
1035 eventfd_ctx_put(ctx);
1036 if (filep)
1037 fput(filep);
1038 break;
1039 default:
935cdee7 1040 r = -ENOIOCTLCMD;
3a4d5c94
MT
1041 break;
1042 }
1043done:
1044 return r;
1045}
6ac1afbf 1046EXPORT_SYMBOL_GPL(vhost_dev_ioctl);
3a4d5c94
MT
1047
1048static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
1049 __u64 addr, __u32 len)
1050{
bcfeacab
IM
1051 const struct vhost_memory_region *reg;
1052 int start = 0, end = mem->nregions;
1053
1054 while (start < end) {
1055 int slot = start + (end - start) / 2;
1056 reg = mem->regions + slot;
1057 if (addr >= reg->guest_phys_addr)
1058 end = slot;
1059 else
1060 start = slot + 1;
3a4d5c94 1061 }
bcfeacab
IM
1062
1063 reg = mem->regions + start;
1064 if (addr >= reg->guest_phys_addr &&
1065 reg->guest_phys_addr + reg->memory_size > addr)
1066 return reg;
3a4d5c94
MT
1067 return NULL;
1068}
1069
1070/* TODO: This is really inefficient. We need something like get_user()
1071 * (instruction directly accesses the data, with an exception table entry
1072 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
1073 */
1074static int set_bit_to_user(int nr, void __user *addr)
1075{
1076 unsigned long log = (unsigned long)addr;
1077 struct page *page;
1078 void *base;
1079 int bit = nr + (log % PAGE_SIZE) * 8;
1080 int r;
d47effe1 1081
3a4d5c94 1082 r = get_user_pages_fast(log, 1, 1, &page);
d6db3f5c 1083 if (r < 0)
3a4d5c94 1084 return r;
d6db3f5c 1085 BUG_ON(r != 1);
c6daa7ff 1086 base = kmap_atomic(page);
3a4d5c94 1087 set_bit(bit, base);
c6daa7ff 1088 kunmap_atomic(base);
3a4d5c94
MT
1089 set_page_dirty_lock(page);
1090 put_page(page);
1091 return 0;
1092}
1093
1094static int log_write(void __user *log_base,
1095 u64 write_address, u64 write_length)
1096{
28831ee6 1097 u64 write_page = write_address / VHOST_PAGE_SIZE;
3a4d5c94 1098 int r;
d47effe1 1099
3a4d5c94
MT
1100 if (!write_length)
1101 return 0;
3bf9be40 1102 write_length += write_address % VHOST_PAGE_SIZE;
3a4d5c94
MT
1103 for (;;) {
1104 u64 base = (u64)(unsigned long)log_base;
28831ee6
MT
1105 u64 log = base + write_page / 8;
1106 int bit = write_page % 8;
3a4d5c94
MT
1107 if ((u64)(unsigned long)log != log)
1108 return -EFAULT;
1109 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
1110 if (r < 0)
1111 return r;
1112 if (write_length <= VHOST_PAGE_SIZE)
1113 break;
1114 write_length -= VHOST_PAGE_SIZE;
28831ee6 1115 write_page += 1;
3a4d5c94
MT
1116 }
1117 return r;
1118}
1119
1120int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
1121 unsigned int log_num, u64 len)
1122{
1123 int i, r;
1124
1125 /* Make sure data written is seen before log. */
5659338c 1126 smp_wmb();
3a4d5c94
MT
1127 for (i = 0; i < log_num; ++i) {
1128 u64 l = min(log[i].len, len);
1129 r = log_write(vq->log_base, log[i].addr, l);
1130 if (r < 0)
1131 return r;
1132 len -= l;
5786aee8
MT
1133 if (!len) {
1134 if (vq->log_ctx)
1135 eventfd_signal(vq->log_ctx, 1);
3a4d5c94 1136 return 0;
5786aee8 1137 }
3a4d5c94 1138 }
3a4d5c94
MT
1139 /* Length written exceeds what we have stored. This is a bug. */
1140 BUG();
1141 return 0;
1142}
6ac1afbf 1143EXPORT_SYMBOL_GPL(vhost_log_write);
3a4d5c94 1144
2723feaa
JW
1145static int vhost_update_used_flags(struct vhost_virtqueue *vq)
1146{
1147 void __user *used;
3b1bbe89 1148 if (__put_user(cpu_to_vhost16(vq, vq->used_flags), &vq->used->flags) < 0)
2723feaa
JW
1149 return -EFAULT;
1150 if (unlikely(vq->log_used)) {
1151 /* Make sure the flag is seen before log. */
1152 smp_wmb();
1153 /* Log used flag write. */
1154 used = &vq->used->flags;
1155 log_write(vq->log_base, vq->log_addr +
1156 (used - (void __user *)vq->used),
1157 sizeof vq->used->flags);
1158 if (vq->log_ctx)
1159 eventfd_signal(vq->log_ctx, 1);
1160 }
1161 return 0;
1162}
1163
1164static int vhost_update_avail_event(struct vhost_virtqueue *vq, u16 avail_event)
1165{
3b1bbe89 1166 if (__put_user(cpu_to_vhost16(vq, vq->avail_idx), vhost_avail_event(vq)))
2723feaa
JW
1167 return -EFAULT;
1168 if (unlikely(vq->log_used)) {
1169 void __user *used;
1170 /* Make sure the event is seen before log. */
1171 smp_wmb();
1172 /* Log avail event write */
1173 used = vhost_avail_event(vq);
1174 log_write(vq->log_base, vq->log_addr +
1175 (used - (void __user *)vq->used),
1176 sizeof *vhost_avail_event(vq));
1177 if (vq->log_ctx)
1178 eventfd_signal(vq->log_ctx, 1);
1179 }
1180 return 0;
1181}
1182
80f7d030 1183int vhost_vq_init_access(struct vhost_virtqueue *vq)
2723feaa 1184{
3b1bbe89 1185 __virtio16 last_used_idx;
2723feaa 1186 int r;
e1f33be9
GK
1187 bool is_le = vq->is_le;
1188
2751c988 1189 if (!vq->private_data) {
c5072037 1190 vhost_reset_is_le(vq);
2723feaa 1191 return 0;
2751c988
GK
1192 }
1193
1194 vhost_init_is_le(vq);
2723feaa
JW
1195
1196 r = vhost_update_used_flags(vq);
1197 if (r)
e1f33be9 1198 goto err;
2723feaa 1199 vq->signalled_used_valid = false;
e1f33be9
GK
1200 if (!access_ok(VERIFY_READ, &vq->used->idx, sizeof vq->used->idx)) {
1201 r = -EFAULT;
1202 goto err;
1203 }
64f7f051
MT
1204 r = __get_user(last_used_idx, &vq->used->idx);
1205 if (r)
e1f33be9 1206 goto err;
3b1bbe89 1207 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx);
64f7f051 1208 return 0;
e1f33be9
GK
1209err:
1210 vq->is_le = is_le;
1211 return r;
2723feaa 1212}
80f7d030 1213EXPORT_SYMBOL_GPL(vhost_vq_init_access);
2723feaa 1214
47283bef 1215static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len,
a8d3782f 1216 struct iovec iov[], int iov_size)
3a4d5c94
MT
1217{
1218 const struct vhost_memory_region *reg;
1219 struct vhost_memory *mem;
1220 struct iovec *_iov;
1221 u64 s = 0;
1222 int ret = 0;
1223
47283bef 1224 mem = vq->memory;
3a4d5c94
MT
1225 while ((u64)len > s) {
1226 u64 size;
7b3384fc 1227 if (unlikely(ret >= iov_size)) {
3a4d5c94
MT
1228 ret = -ENOBUFS;
1229 break;
1230 }
1231 reg = find_region(mem, addr, len);
7b3384fc 1232 if (unlikely(!reg)) {
3a4d5c94
MT
1233 ret = -EFAULT;
1234 break;
1235 }
1236 _iov = iov + ret;
1237 size = reg->memory_size - addr + reg->guest_phys_addr;
bd97120f 1238 _iov->iov_len = min((u64)len - s, size);
a8d3782f 1239 _iov->iov_base = (void __user *)(unsigned long)
3a4d5c94
MT
1240 (reg->userspace_addr + addr - reg->guest_phys_addr);
1241 s += size;
1242 addr += size;
1243 ++ret;
1244 }
1245
3a4d5c94
MT
1246 return ret;
1247}
1248
1249/* Each buffer in the virtqueues is actually a chain of descriptors. This
1250 * function returns the next descriptor in the chain,
1251 * or -1U if we're at the end. */
3b1bbe89 1252static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc)
3a4d5c94
MT
1253{
1254 unsigned int next;
1255
1256 /* If this descriptor says it doesn't chain, we're done. */
3b1bbe89 1257 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT)))
3a4d5c94
MT
1258 return -1U;
1259
1260 /* Check they're not leading us off end of descriptors. */
3b1bbe89 1261 next = vhost16_to_cpu(vq, desc->next);
3a4d5c94
MT
1262 /* Make sure compiler knows to grab that: we don't want it changing! */
1263 /* We will use the result as an index in an array, so most
1264 * architectures only need a compiler barrier here. */
1265 read_barrier_depends();
1266
1267 return next;
1268}
1269
47283bef 1270static int get_indirect(struct vhost_virtqueue *vq,
7b3384fc
MT
1271 struct iovec iov[], unsigned int iov_size,
1272 unsigned int *out_num, unsigned int *in_num,
1273 struct vhost_log *log, unsigned int *log_num,
1274 struct vring_desc *indirect)
3a4d5c94
MT
1275{
1276 struct vring_desc desc;
1277 unsigned int i = 0, count, found = 0;
3b1bbe89 1278 u32 len = vhost32_to_cpu(vq, indirect->len);
aad9a1ce 1279 struct iov_iter from;
3a4d5c94
MT
1280 int ret;
1281
1282 /* Sanity check */
3b1bbe89 1283 if (unlikely(len % sizeof desc)) {
3a4d5c94
MT
1284 vq_err(vq, "Invalid length in indirect descriptor: "
1285 "len 0x%llx not multiple of 0x%zx\n",
3b1bbe89 1286 (unsigned long long)len,
3a4d5c94
MT
1287 sizeof desc);
1288 return -EINVAL;
1289 }
1290
3b1bbe89 1291 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect,
e0e9b406 1292 UIO_MAXIOV);
7b3384fc 1293 if (unlikely(ret < 0)) {
3a4d5c94
MT
1294 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1295 return ret;
1296 }
aad9a1ce 1297 iov_iter_init(&from, READ, vq->indirect, ret, len);
3a4d5c94
MT
1298
1299 /* We will use the result as an address to read from, so most
1300 * architectures only need a compiler barrier here. */
1301 read_barrier_depends();
1302
3b1bbe89 1303 count = len / sizeof desc;
3a4d5c94
MT
1304 /* Buffers are chained via a 16 bit next field, so
1305 * we can have at most 2^16 of these. */
7b3384fc 1306 if (unlikely(count > USHRT_MAX + 1)) {
3a4d5c94
MT
1307 vq_err(vq, "Indirect buffer length too big: %d\n",
1308 indirect->len);
1309 return -E2BIG;
1310 }
1311
1312 do {
1313 unsigned iov_count = *in_num + *out_num;
7b3384fc 1314 if (unlikely(++found > count)) {
3a4d5c94
MT
1315 vq_err(vq, "Loop detected: last one at %u "
1316 "indirect size %u\n",
1317 i, count);
1318 return -EINVAL;
1319 }
aad9a1ce
AV
1320 if (unlikely(copy_from_iter(&desc, sizeof(desc), &from) !=
1321 sizeof(desc))) {
3a4d5c94 1322 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
3b1bbe89 1323 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
3a4d5c94
MT
1324 return -EINVAL;
1325 }
3b1bbe89 1326 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) {
3a4d5c94 1327 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
3b1bbe89 1328 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc);
3a4d5c94
MT
1329 return -EINVAL;
1330 }
1331
3b1bbe89
MT
1332 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1333 vhost32_to_cpu(vq, desc.len), iov + iov_count,
3a4d5c94 1334 iov_size - iov_count);
7b3384fc 1335 if (unlikely(ret < 0)) {
3a4d5c94
MT
1336 vq_err(vq, "Translation failure %d indirect idx %d\n",
1337 ret, i);
1338 return ret;
1339 }
1340 /* If this is an input descriptor, increment that count. */
3b1bbe89 1341 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
3a4d5c94
MT
1342 *in_num += ret;
1343 if (unlikely(log)) {
3b1bbe89
MT
1344 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1345 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
3a4d5c94
MT
1346 ++*log_num;
1347 }
1348 } else {
1349 /* If it's an output descriptor, they're all supposed
1350 * to come before any input descriptors. */
7b3384fc 1351 if (unlikely(*in_num)) {
3a4d5c94
MT
1352 vq_err(vq, "Indirect descriptor "
1353 "has out after in: idx %d\n", i);
1354 return -EINVAL;
1355 }
1356 *out_num += ret;
1357 }
3b1bbe89 1358 } while ((i = next_desc(vq, &desc)) != -1);
3a4d5c94
MT
1359 return 0;
1360}
1361
1362/* This looks in the virtqueue and for the first available buffer, and converts
1363 * it to an iovec for convenient access. Since descriptors consist of some
1364 * number of output then some number of input descriptors, it's actually two
1365 * iovecs, but we pack them into one and note how many of each there were.
1366 *
d5675bd2
MT
1367 * This function returns the descriptor number found, or vq->num (which is
1368 * never a valid descriptor number) if none was found. A negative code is
1369 * returned on error. */
47283bef 1370int vhost_get_vq_desc(struct vhost_virtqueue *vq,
d5675bd2
MT
1371 struct iovec iov[], unsigned int iov_size,
1372 unsigned int *out_num, unsigned int *in_num,
1373 struct vhost_log *log, unsigned int *log_num)
3a4d5c94
MT
1374{
1375 struct vring_desc desc;
1376 unsigned int i, head, found = 0;
1377 u16 last_avail_idx;
3b1bbe89
MT
1378 __virtio16 avail_idx;
1379 __virtio16 ring_head;
3a4d5c94
MT
1380 int ret;
1381
1382 /* Check it isn't doing very strange things with descriptor numbers. */
1383 last_avail_idx = vq->last_avail_idx;
3b1bbe89 1384 if (unlikely(__get_user(avail_idx, &vq->avail->idx))) {
3a4d5c94
MT
1385 vq_err(vq, "Failed to access avail idx at %p\n",
1386 &vq->avail->idx);
d5675bd2 1387 return -EFAULT;
3a4d5c94 1388 }
3b1bbe89 1389 vq->avail_idx = vhost16_to_cpu(vq, avail_idx);
3a4d5c94 1390
7b3384fc 1391 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
3a4d5c94
MT
1392 vq_err(vq, "Guest moved used index from %u to %u",
1393 last_avail_idx, vq->avail_idx);
d5675bd2 1394 return -EFAULT;
3a4d5c94
MT
1395 }
1396
1397 /* If there's nothing new since last we looked, return invalid. */
1398 if (vq->avail_idx == last_avail_idx)
1399 return vq->num;
1400
1401 /* Only get avail ring entries after they have been exposed by guest. */
5659338c 1402 smp_rmb();
3a4d5c94
MT
1403
1404 /* Grab the next descriptor number they're advertising, and increment
1405 * the index we've seen. */
3b1bbe89 1406 if (unlikely(__get_user(ring_head,
5fba13b5 1407 &vq->avail->ring[last_avail_idx & (vq->num - 1)]))) {
3a4d5c94
MT
1408 vq_err(vq, "Failed to read head: idx %d address %p\n",
1409 last_avail_idx,
1410 &vq->avail->ring[last_avail_idx % vq->num]);
d5675bd2 1411 return -EFAULT;
3a4d5c94
MT
1412 }
1413
3b1bbe89
MT
1414 head = vhost16_to_cpu(vq, ring_head);
1415
3a4d5c94 1416 /* If their number is silly, that's an error. */
7b3384fc 1417 if (unlikely(head >= vq->num)) {
3a4d5c94
MT
1418 vq_err(vq, "Guest says index %u > %u is available",
1419 head, vq->num);
d5675bd2 1420 return -EINVAL;
3a4d5c94
MT
1421 }
1422
1423 /* When we start there are none of either input nor output. */
1424 *out_num = *in_num = 0;
1425 if (unlikely(log))
1426 *log_num = 0;
1427
1428 i = head;
1429 do {
1430 unsigned iov_count = *in_num + *out_num;
7b3384fc 1431 if (unlikely(i >= vq->num)) {
3a4d5c94
MT
1432 vq_err(vq, "Desc index is %u > %u, head = %u",
1433 i, vq->num, head);
d5675bd2 1434 return -EINVAL;
3a4d5c94 1435 }
7b3384fc 1436 if (unlikely(++found > vq->num)) {
3a4d5c94
MT
1437 vq_err(vq, "Loop detected: last one at %u "
1438 "vq size %u head %u\n",
1439 i, vq->num, head);
d5675bd2 1440 return -EINVAL;
3a4d5c94 1441 }
fcc042a2 1442 ret = __copy_from_user(&desc, vq->desc + i, sizeof desc);
7b3384fc 1443 if (unlikely(ret)) {
3a4d5c94
MT
1444 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1445 i, vq->desc + i);
d5675bd2 1446 return -EFAULT;
3a4d5c94 1447 }
3b1bbe89 1448 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) {
47283bef 1449 ret = get_indirect(vq, iov, iov_size,
3a4d5c94
MT
1450 out_num, in_num,
1451 log, log_num, &desc);
7b3384fc 1452 if (unlikely(ret < 0)) {
3a4d5c94
MT
1453 vq_err(vq, "Failure detected "
1454 "in indirect descriptor at idx %d\n", i);
d5675bd2 1455 return ret;
3a4d5c94
MT
1456 }
1457 continue;
1458 }
1459
3b1bbe89
MT
1460 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr),
1461 vhost32_to_cpu(vq, desc.len), iov + iov_count,
3a4d5c94 1462 iov_size - iov_count);
7b3384fc 1463 if (unlikely(ret < 0)) {
3a4d5c94
MT
1464 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1465 ret, i);
d5675bd2 1466 return ret;
3a4d5c94 1467 }
3b1bbe89 1468 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) {
3a4d5c94
MT
1469 /* If this is an input descriptor,
1470 * increment that count. */
1471 *in_num += ret;
1472 if (unlikely(log)) {
3b1bbe89
MT
1473 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr);
1474 log[*log_num].len = vhost32_to_cpu(vq, desc.len);
3a4d5c94
MT
1475 ++*log_num;
1476 }
1477 } else {
1478 /* If it's an output descriptor, they're all supposed
1479 * to come before any input descriptors. */
7b3384fc 1480 if (unlikely(*in_num)) {
3a4d5c94
MT
1481 vq_err(vq, "Descriptor has out after in: "
1482 "idx %d\n", i);
d5675bd2 1483 return -EINVAL;
3a4d5c94
MT
1484 }
1485 *out_num += ret;
1486 }
3b1bbe89 1487 } while ((i = next_desc(vq, &desc)) != -1);
3a4d5c94
MT
1488
1489 /* On success, increment avail index. */
1490 vq->last_avail_idx++;
8ea8cf89
MT
1491
1492 /* Assume notifications from guest are disabled at this point,
1493 * if they aren't we would need to update avail_event index. */
1494 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY));
3a4d5c94
MT
1495 return head;
1496}
6ac1afbf 1497EXPORT_SYMBOL_GPL(vhost_get_vq_desc);
3a4d5c94
MT
1498
1499/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
8dd014ad 1500void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
3a4d5c94 1501{
8dd014ad 1502 vq->last_avail_idx -= n;
3a4d5c94 1503}
6ac1afbf 1504EXPORT_SYMBOL_GPL(vhost_discard_vq_desc);
3a4d5c94
MT
1505
1506/* After we've used one of their buffers, we tell them about it. We'll then
1507 * want to notify the guest, using eventfd. */
1508int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1509{
3b1bbe89
MT
1510 struct vring_used_elem heads = {
1511 cpu_to_vhost32(vq, head),
1512 cpu_to_vhost32(vq, len)
1513 };
3a4d5c94 1514
c49e4e57 1515 return vhost_add_used_n(vq, &heads, 1);
3a4d5c94 1516}
6ac1afbf 1517EXPORT_SYMBOL_GPL(vhost_add_used);
3a4d5c94 1518
8dd014ad
DS
1519static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1520 struct vring_used_elem *heads,
1521 unsigned count)
1522{
1523 struct vring_used_elem __user *used;
8ea8cf89 1524 u16 old, new;
8dd014ad
DS
1525 int start;
1526
5fba13b5 1527 start = vq->last_used_idx & (vq->num - 1);
8dd014ad 1528 used = vq->used->ring + start;
c49e4e57
JW
1529 if (count == 1) {
1530 if (__put_user(heads[0].id, &used->id)) {
1531 vq_err(vq, "Failed to write used id");
1532 return -EFAULT;
1533 }
1534 if (__put_user(heads[0].len, &used->len)) {
1535 vq_err(vq, "Failed to write used len");
1536 return -EFAULT;
1537 }
1538 } else if (__copy_to_user(used, heads, count * sizeof *used)) {
8dd014ad
DS
1539 vq_err(vq, "Failed to write used");
1540 return -EFAULT;
1541 }
1542 if (unlikely(vq->log_used)) {
1543 /* Make sure data is seen before log. */
1544 smp_wmb();
1545 /* Log used ring entry write. */
1546 log_write(vq->log_base,
1547 vq->log_addr +
1548 ((void __user *)used - (void __user *)vq->used),
1549 count * sizeof *used);
1550 }
8ea8cf89
MT
1551 old = vq->last_used_idx;
1552 new = (vq->last_used_idx += count);
1553 /* If the driver never bothers to signal in a very long while,
1554 * used index might wrap around. If that happens, invalidate
1555 * signalled_used index we stored. TODO: make sure driver
1556 * signals at least once in 2^16 and remove this. */
1557 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old)))
1558 vq->signalled_used_valid = false;
8dd014ad
DS
1559 return 0;
1560}
1561
1562/* After we've used one of their buffers, we tell them about it. We'll then
1563 * want to notify the guest, using eventfd. */
1564int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1565 unsigned count)
1566{
1567 int start, n, r;
1568
5fba13b5 1569 start = vq->last_used_idx & (vq->num - 1);
8dd014ad
DS
1570 n = vq->num - start;
1571 if (n < count) {
1572 r = __vhost_add_used_n(vq, heads, n);
1573 if (r < 0)
1574 return r;
1575 heads += n;
1576 count -= n;
1577 }
1578 r = __vhost_add_used_n(vq, heads, count);
1579
1580 /* Make sure buffer is written before we update index. */
1581 smp_wmb();
3b1bbe89 1582 if (__put_user(cpu_to_vhost16(vq, vq->last_used_idx), &vq->used->idx)) {
8dd014ad
DS
1583 vq_err(vq, "Failed to increment used idx");
1584 return -EFAULT;
1585 }
1586 if (unlikely(vq->log_used)) {
1587 /* Log used index update. */
1588 log_write(vq->log_base,
1589 vq->log_addr + offsetof(struct vring_used, idx),
1590 sizeof vq->used->idx);
1591 if (vq->log_ctx)
1592 eventfd_signal(vq->log_ctx, 1);
1593 }
1594 return r;
1595}
6ac1afbf 1596EXPORT_SYMBOL_GPL(vhost_add_used_n);
8dd014ad 1597
8ea8cf89 1598static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
3a4d5c94 1599{
3b1bbe89
MT
1600 __u16 old, new;
1601 __virtio16 event;
8ea8cf89 1602 bool v;
0d499356
MT
1603 /* Flush out used index updates. This is paired
1604 * with the barrier that the Guest executes when enabling
1605 * interrupts. */
1606 smp_mb();
1607
ea16c514 1608 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) &&
8ea8cf89
MT
1609 unlikely(vq->avail_idx == vq->last_avail_idx))
1610 return true;
1611
ea16c514 1612 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
3b1bbe89 1613 __virtio16 flags;
8ea8cf89
MT
1614 if (__get_user(flags, &vq->avail->flags)) {
1615 vq_err(vq, "Failed to get flags");
1616 return true;
1617 }
3b1bbe89 1618 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT));
3a4d5c94 1619 }
8ea8cf89
MT
1620 old = vq->signalled_used;
1621 v = vq->signalled_used_valid;
1622 new = vq->signalled_used = vq->last_used_idx;
1623 vq->signalled_used_valid = true;
3a4d5c94 1624
8ea8cf89
MT
1625 if (unlikely(!v))
1626 return true;
3a4d5c94 1627
64f7f051 1628 if (__get_user(event, vhost_used_event(vq))) {
8ea8cf89
MT
1629 vq_err(vq, "Failed to get used event idx");
1630 return true;
1631 }
3b1bbe89 1632 return vring_need_event(vhost16_to_cpu(vq, event), new, old);
8ea8cf89
MT
1633}
1634
1635/* This actually signals the guest, using eventfd. */
1636void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1637{
3a4d5c94 1638 /* Signal the Guest tell them we used something up. */
8ea8cf89 1639 if (vq->call_ctx && vhost_notify(dev, vq))
3a4d5c94
MT
1640 eventfd_signal(vq->call_ctx, 1);
1641}
6ac1afbf 1642EXPORT_SYMBOL_GPL(vhost_signal);
3a4d5c94
MT
1643
1644/* And here's the combo meal deal. Supersize me! */
1645void vhost_add_used_and_signal(struct vhost_dev *dev,
1646 struct vhost_virtqueue *vq,
1647 unsigned int head, int len)
1648{
1649 vhost_add_used(vq, head, len);
1650 vhost_signal(dev, vq);
1651}
6ac1afbf 1652EXPORT_SYMBOL_GPL(vhost_add_used_and_signal);
3a4d5c94 1653
8dd014ad
DS
1654/* multi-buffer version of vhost_add_used_and_signal */
1655void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1656 struct vhost_virtqueue *vq,
1657 struct vring_used_elem *heads, unsigned count)
1658{
1659 vhost_add_used_n(vq, heads, count);
1660 vhost_signal(dev, vq);
1661}
6ac1afbf 1662EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n);
8dd014ad 1663
d4a60603
JW
1664/* return true if we're sure that avaiable ring is empty */
1665bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1666{
1667 __virtio16 avail_idx;
1668 int r;
1669
1670 r = __get_user(avail_idx, &vq->avail->idx);
1671 if (r)
1672 return false;
1673
1674 return vhost16_to_cpu(vq, avail_idx) == vq->avail_idx;
1675}
1676EXPORT_SYMBOL_GPL(vhost_vq_avail_empty);
1677
3a4d5c94 1678/* OK, now we need to know about added descriptors. */
8ea8cf89 1679bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
3a4d5c94 1680{
3b1bbe89 1681 __virtio16 avail_idx;
3a4d5c94 1682 int r;
d47effe1 1683
3a4d5c94
MT
1684 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1685 return false;
1686 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
ea16c514 1687 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
2723feaa 1688 r = vhost_update_used_flags(vq);
8ea8cf89
MT
1689 if (r) {
1690 vq_err(vq, "Failed to enable notification at %p: %d\n",
1691 &vq->used->flags, r);
1692 return false;
1693 }
1694 } else {
2723feaa 1695 r = vhost_update_avail_event(vq, vq->avail_idx);
8ea8cf89
MT
1696 if (r) {
1697 vq_err(vq, "Failed to update avail event index at %p: %d\n",
1698 vhost_avail_event(vq), r);
1699 return false;
1700 }
1701 }
3a4d5c94
MT
1702 /* They could have slipped one in as we were doing that: make
1703 * sure it's written, then check again. */
5659338c 1704 smp_mb();
8b7347aa 1705 r = __get_user(avail_idx, &vq->avail->idx);
3a4d5c94
MT
1706 if (r) {
1707 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1708 &vq->avail->idx, r);
1709 return false;
1710 }
1711
3b1bbe89 1712 return vhost16_to_cpu(vq, avail_idx) != vq->avail_idx;
3a4d5c94 1713}
6ac1afbf 1714EXPORT_SYMBOL_GPL(vhost_enable_notify);
3a4d5c94
MT
1715
1716/* We don't need to be notified again. */
8ea8cf89 1717void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq)
3a4d5c94
MT
1718{
1719 int r;
d47effe1 1720
3a4d5c94
MT
1721 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1722 return;
1723 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
ea16c514 1724 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) {
2723feaa 1725 r = vhost_update_used_flags(vq);
8ea8cf89
MT
1726 if (r)
1727 vq_err(vq, "Failed to enable notification at %p: %d\n",
1728 &vq->used->flags, r);
1729 }
3a4d5c94 1730}
6ac1afbf
AH
1731EXPORT_SYMBOL_GPL(vhost_disable_notify);
1732
1733static int __init vhost_init(void)
1734{
1735 return 0;
1736}
1737
1738static void __exit vhost_exit(void)
1739{
1740}
1741
1742module_init(vhost_init);
1743module_exit(vhost_exit);
1744
1745MODULE_VERSION("0.0.1");
1746MODULE_LICENSE("GPL v2");
1747MODULE_AUTHOR("Michael S. Tsirkin");
1748MODULE_DESCRIPTION("Host kernel accelerator for virtio");