]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/vhost/vhost.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.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
7 * Documentation/lguest/lguest.c, by Rusty Russell
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2.
10 *
11 * Generic code for virtio server in host kernel.
12 */
13
14#include <linux/eventfd.h>
15#include <linux/vhost.h>
16#include <linux/virtio_net.h>
17#include <linux/mm.h>
18#include <linux/miscdevice.h>
19#include <linux/mutex.h>
3a4d5c94
MT
20#include <linux/rcupdate.h>
21#include <linux/poll.h>
22#include <linux/file.h>
23#include <linux/highmem.h>
5a0e3ad6 24#include <linux/slab.h>
c23f3445 25#include <linux/kthread.h>
9e3d1957 26#include <linux/cgroup.h>
3a4d5c94
MT
27
28#include <linux/net.h>
29#include <linux/if_packet.h>
30#include <linux/if_arp.h>
31
32#include <net/sock.h>
33
34#include "vhost.h"
35
36enum {
37 VHOST_MEMORY_MAX_NREGIONS = 64,
38 VHOST_MEMORY_F_LOG = 0x1,
39};
40
3a4d5c94
MT
41static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
42 poll_table *pt)
43{
44 struct vhost_poll *poll;
45 poll = container_of(pt, struct vhost_poll, table);
46
47 poll->wqh = wqh;
48 add_wait_queue(wqh, &poll->wait);
49}
50
51static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
52 void *key)
53{
c23f3445
TH
54 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
55
3a4d5c94
MT
56 if (!((unsigned long)key & poll->mask))
57 return 0;
58
c23f3445 59 vhost_poll_queue(poll);
3a4d5c94
MT
60 return 0;
61}
62
87d6a412
MT
63static void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
64{
65 INIT_LIST_HEAD(&work->node);
66 work->fn = fn;
67 init_waitqueue_head(&work->done);
68 work->flushing = 0;
69 work->queue_seq = work->done_seq = 0;
70}
71
3a4d5c94 72/* Init poll structure */
c23f3445
TH
73void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
74 unsigned long mask, struct vhost_dev *dev)
3a4d5c94 75{
3a4d5c94
MT
76 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
77 init_poll_funcptr(&poll->table, vhost_poll_func);
78 poll->mask = mask;
c23f3445
TH
79 poll->dev = dev;
80
87d6a412 81 vhost_work_init(&poll->work, fn);
3a4d5c94
MT
82}
83
84/* Start polling a file. We add ourselves to file's wait queue. The caller must
85 * keep a reference to a file until after vhost_poll_stop is called. */
86void vhost_poll_start(struct vhost_poll *poll, struct file *file)
87{
88 unsigned long mask;
89 mask = file->f_op->poll(file, &poll->table);
90 if (mask)
91 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
92}
93
94/* Stop polling a file. After this function returns, it becomes safe to drop the
95 * file reference. You must also flush afterwards. */
96void vhost_poll_stop(struct vhost_poll *poll)
97{
98 remove_wait_queue(poll->wqh, &poll->wait);
99}
100
87d6a412 101static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
3a4d5c94 102{
c23f3445
TH
103 unsigned seq;
104 int left;
105 int flushing;
106
87d6a412 107 spin_lock_irq(&dev->work_lock);
c23f3445
TH
108 seq = work->queue_seq;
109 work->flushing++;
87d6a412 110 spin_unlock_irq(&dev->work_lock);
c23f3445 111 wait_event(work->done, ({
87d6a412 112 spin_lock_irq(&dev->work_lock);
c23f3445 113 left = seq - work->done_seq <= 0;
87d6a412 114 spin_unlock_irq(&dev->work_lock);
c23f3445
TH
115 left;
116 }));
87d6a412 117 spin_lock_irq(&dev->work_lock);
c23f3445 118 flushing = --work->flushing;
87d6a412 119 spin_unlock_irq(&dev->work_lock);
c23f3445 120 BUG_ON(flushing < 0);
3a4d5c94
MT
121}
122
87d6a412
MT
123/* Flush any work that has been scheduled. When calling this, don't hold any
124 * locks that are also used by the callback. */
125void vhost_poll_flush(struct vhost_poll *poll)
126{
127 vhost_work_flush(poll->dev, &poll->work);
128}
129
130static inline void vhost_work_queue(struct vhost_dev *dev,
131 struct vhost_work *work)
3a4d5c94 132{
c23f3445
TH
133 unsigned long flags;
134
135 spin_lock_irqsave(&dev->work_lock, flags);
136 if (list_empty(&work->node)) {
137 list_add_tail(&work->node, &dev->work_list);
138 work->queue_seq++;
139 wake_up_process(dev->worker);
140 }
141 spin_unlock_irqrestore(&dev->work_lock, flags);
3a4d5c94
MT
142}
143
87d6a412
MT
144void vhost_poll_queue(struct vhost_poll *poll)
145{
146 vhost_work_queue(poll->dev, &poll->work);
147}
148
3a4d5c94
MT
149static void vhost_vq_reset(struct vhost_dev *dev,
150 struct vhost_virtqueue *vq)
151{
152 vq->num = 1;
153 vq->desc = NULL;
154 vq->avail = NULL;
155 vq->used = NULL;
156 vq->last_avail_idx = 0;
157 vq->avail_idx = 0;
158 vq->last_used_idx = 0;
159 vq->used_flags = 0;
160 vq->used_flags = 0;
161 vq->log_used = false;
162 vq->log_addr = -1ull;
8dd014ad
DS
163 vq->vhost_hlen = 0;
164 vq->sock_hlen = 0;
3a4d5c94
MT
165 vq->private_data = NULL;
166 vq->log_base = NULL;
167 vq->error_ctx = NULL;
168 vq->error = NULL;
169 vq->kick = NULL;
170 vq->call_ctx = NULL;
171 vq->call = NULL;
73a99f08 172 vq->log_ctx = NULL;
3a4d5c94
MT
173}
174
c23f3445
TH
175static int vhost_worker(void *data)
176{
177 struct vhost_dev *dev = data;
178 struct vhost_work *work = NULL;
179 unsigned uninitialized_var(seq);
180
181 for (;;) {
182 /* mb paired w/ kthread_stop */
183 set_current_state(TASK_INTERRUPTIBLE);
184
185 spin_lock_irq(&dev->work_lock);
186 if (work) {
187 work->done_seq = seq;
188 if (work->flushing)
189 wake_up_all(&work->done);
190 }
191
192 if (kthread_should_stop()) {
193 spin_unlock_irq(&dev->work_lock);
194 __set_current_state(TASK_RUNNING);
195 return 0;
196 }
197 if (!list_empty(&dev->work_list)) {
198 work = list_first_entry(&dev->work_list,
199 struct vhost_work, node);
200 list_del_init(&work->node);
201 seq = work->queue_seq;
202 } else
203 work = NULL;
204 spin_unlock_irq(&dev->work_lock);
205
206 if (work) {
207 __set_current_state(TASK_RUNNING);
208 work->fn(work);
209 } else
210 schedule();
211
212 }
213}
214
e0e9b406
JW
215/* Helper to allocate iovec buffers for all vqs. */
216static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
217{
218 int i;
219 for (i = 0; i < dev->nvqs; ++i) {
220 dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
221 UIO_MAXIOV, GFP_KERNEL);
222 dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
223 GFP_KERNEL);
224 dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
225 UIO_MAXIOV, GFP_KERNEL);
226
227 if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
228 !dev->vqs[i].heads)
229 goto err_nomem;
230 }
231 return 0;
232err_nomem:
233 for (; i >= 0; --i) {
234 kfree(dev->vqs[i].indirect);
235 kfree(dev->vqs[i].log);
236 kfree(dev->vqs[i].heads);
237 }
238 return -ENOMEM;
239}
240
241static void vhost_dev_free_iovecs(struct vhost_dev *dev)
242{
243 int i;
244 for (i = 0; i < dev->nvqs; ++i) {
245 kfree(dev->vqs[i].indirect);
246 dev->vqs[i].indirect = NULL;
247 kfree(dev->vqs[i].log);
248 dev->vqs[i].log = NULL;
249 kfree(dev->vqs[i].heads);
250 dev->vqs[i].heads = NULL;
251 }
252}
253
3a4d5c94
MT
254long vhost_dev_init(struct vhost_dev *dev,
255 struct vhost_virtqueue *vqs, int nvqs)
256{
257 int i;
c23f3445 258
3a4d5c94
MT
259 dev->vqs = vqs;
260 dev->nvqs = nvqs;
261 mutex_init(&dev->mutex);
262 dev->log_ctx = NULL;
263 dev->log_file = NULL;
264 dev->memory = NULL;
265 dev->mm = NULL;
c23f3445
TH
266 spin_lock_init(&dev->work_lock);
267 INIT_LIST_HEAD(&dev->work_list);
268 dev->worker = NULL;
3a4d5c94
MT
269
270 for (i = 0; i < dev->nvqs; ++i) {
e0e9b406
JW
271 dev->vqs[i].log = NULL;
272 dev->vqs[i].indirect = NULL;
273 dev->vqs[i].heads = NULL;
3a4d5c94
MT
274 dev->vqs[i].dev = dev;
275 mutex_init(&dev->vqs[i].mutex);
276 vhost_vq_reset(dev, dev->vqs + i);
277 if (dev->vqs[i].handle_kick)
278 vhost_poll_init(&dev->vqs[i].poll,
c23f3445 279 dev->vqs[i].handle_kick, POLLIN, dev);
3a4d5c94 280 }
c23f3445 281
3a4d5c94
MT
282 return 0;
283}
284
285/* Caller should have device mutex */
286long vhost_dev_check_owner(struct vhost_dev *dev)
287{
288 /* Are you the owner? If not, I don't think you mean to do that */
289 return dev->mm == current->mm ? 0 : -EPERM;
290}
291
87d6a412
MT
292struct vhost_attach_cgroups_struct {
293 struct vhost_work work;
294 struct task_struct *owner;
295 int ret;
296};
297
298static void vhost_attach_cgroups_work(struct vhost_work *work)
299{
300 struct vhost_attach_cgroups_struct *s;
301 s = container_of(work, struct vhost_attach_cgroups_struct, work);
302 s->ret = cgroup_attach_task_all(s->owner, current);
303}
304
305static int vhost_attach_cgroups(struct vhost_dev *dev)
306{
307 struct vhost_attach_cgroups_struct attach;
308 attach.owner = current;
309 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
310 vhost_work_queue(dev, &attach.work);
311 vhost_work_flush(dev, &attach.work);
312 return attach.ret;
313}
314
3a4d5c94
MT
315/* Caller should have device mutex */
316static long vhost_dev_set_owner(struct vhost_dev *dev)
317{
c23f3445
TH
318 struct task_struct *worker;
319 int err;
3a4d5c94 320 /* Is there an owner already? */
c23f3445
TH
321 if (dev->mm) {
322 err = -EBUSY;
323 goto err_mm;
324 }
3a4d5c94
MT
325 /* No owner, become one */
326 dev->mm = get_task_mm(current);
c23f3445
TH
327 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
328 if (IS_ERR(worker)) {
329 err = PTR_ERR(worker);
330 goto err_worker;
331 }
332
333 dev->worker = worker;
87d6a412
MT
334 wake_up_process(worker); /* avoid contributing to loadavg */
335
336 err = vhost_attach_cgroups(dev);
9e3d1957
MT
337 if (err)
338 goto err_cgroup;
c23f3445 339
e0e9b406
JW
340 err = vhost_dev_alloc_iovecs(dev);
341 if (err)
342 goto err_cgroup;
343
3a4d5c94 344 return 0;
9e3d1957
MT
345err_cgroup:
346 kthread_stop(worker);
615cc221 347 dev->worker = NULL;
c23f3445
TH
348err_worker:
349 if (dev->mm)
350 mmput(dev->mm);
351 dev->mm = NULL;
352err_mm:
353 return err;
3a4d5c94
MT
354}
355
356/* Caller should have device mutex */
357long vhost_dev_reset_owner(struct vhost_dev *dev)
358{
359 struct vhost_memory *memory;
360
361 /* Restore memory to default empty mapping. */
362 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
363 if (!memory)
364 return -ENOMEM;
365
366 vhost_dev_cleanup(dev);
367
368 memory->nregions = 0;
369 dev->memory = memory;
370 return 0;
371}
372
373/* Caller should have device mutex */
374void vhost_dev_cleanup(struct vhost_dev *dev)
375{
376 int i;
377 for (i = 0; i < dev->nvqs; ++i) {
378 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
379 vhost_poll_stop(&dev->vqs[i].poll);
380 vhost_poll_flush(&dev->vqs[i].poll);
381 }
382 if (dev->vqs[i].error_ctx)
383 eventfd_ctx_put(dev->vqs[i].error_ctx);
384 if (dev->vqs[i].error)
385 fput(dev->vqs[i].error);
386 if (dev->vqs[i].kick)
387 fput(dev->vqs[i].kick);
388 if (dev->vqs[i].call_ctx)
389 eventfd_ctx_put(dev->vqs[i].call_ctx);
390 if (dev->vqs[i].call)
391 fput(dev->vqs[i].call);
392 vhost_vq_reset(dev, dev->vqs + i);
393 }
e0e9b406 394 vhost_dev_free_iovecs(dev);
3a4d5c94
MT
395 if (dev->log_ctx)
396 eventfd_ctx_put(dev->log_ctx);
397 dev->log_ctx = NULL;
398 if (dev->log_file)
399 fput(dev->log_file);
400 dev->log_file = NULL;
401 /* No one will access memory at this point */
402 kfree(dev->memory);
403 dev->memory = NULL;
404 if (dev->mm)
405 mmput(dev->mm);
406 dev->mm = NULL;
c23f3445
TH
407
408 WARN_ON(!list_empty(&dev->work_list));
78b620ce
ED
409 if (dev->worker) {
410 kthread_stop(dev->worker);
411 dev->worker = NULL;
412 }
3a4d5c94
MT
413}
414
415static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
416{
417 u64 a = addr / VHOST_PAGE_SIZE / 8;
418 /* Make sure 64 bit math will not overflow. */
419 if (a > ULONG_MAX - (unsigned long)log_base ||
420 a + (unsigned long)log_base > ULONG_MAX)
6d97e55f 421 return 0;
3a4d5c94
MT
422
423 return access_ok(VERIFY_WRITE, log_base + a,
424 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
425}
426
427/* Caller should have vq mutex and device mutex. */
428static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
429 int log_all)
430{
431 int i;
179b284e 432
f8322fbe
MT
433 if (!mem)
434 return 0;
179b284e 435
3a4d5c94
MT
436 for (i = 0; i < mem->nregions; ++i) {
437 struct vhost_memory_region *m = mem->regions + i;
438 unsigned long a = m->userspace_addr;
439 if (m->memory_size > ULONG_MAX)
440 return 0;
441 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
442 m->memory_size))
443 return 0;
444 else if (log_all && !log_access_ok(log_base,
445 m->guest_phys_addr,
446 m->memory_size))
447 return 0;
448 }
449 return 1;
450}
451
452/* Can we switch to this memory table? */
453/* Caller should have device mutex but not vq mutex */
454static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
455 int log_all)
456{
457 int i;
458 for (i = 0; i < d->nvqs; ++i) {
459 int ok;
460 mutex_lock(&d->vqs[i].mutex);
461 /* If ring is inactive, will check when it's enabled. */
462 if (d->vqs[i].private_data)
463 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
464 log_all);
465 else
466 ok = 1;
467 mutex_unlock(&d->vqs[i].mutex);
468 if (!ok)
469 return 0;
470 }
471 return 1;
472}
473
474static int vq_access_ok(unsigned int num,
475 struct vring_desc __user *desc,
476 struct vring_avail __user *avail,
477 struct vring_used __user *used)
478{
479 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
480 access_ok(VERIFY_READ, avail,
481 sizeof *avail + num * sizeof *avail->ring) &&
482 access_ok(VERIFY_WRITE, used,
483 sizeof *used + num * sizeof *used->ring);
484}
485
486/* Can we log writes? */
487/* Caller should have device mutex but not vq mutex */
488int vhost_log_access_ok(struct vhost_dev *dev)
489{
490 return memory_access_ok(dev, dev->memory, 1);
491}
492
493/* Verify access for write logging. */
494/* Caller should have vq mutex and device mutex */
495static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
496{
497 return vq_memory_access_ok(log_base, vq->dev->memory,
498 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
499 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
500 sizeof *vq->used +
501 vq->num * sizeof *vq->used->ring));
502}
503
504/* Can we start vq? */
505/* Caller should have vq mutex and device mutex */
506int vhost_vq_access_ok(struct vhost_virtqueue *vq)
507{
508 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
509 vq_log_access_ok(vq, vq->log_base);
510}
511
512static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
513{
514 struct vhost_memory mem, *newmem, *oldmem;
515 unsigned long size = offsetof(struct vhost_memory, regions);
7ad9c9d2
TY
516 if (copy_from_user(&mem, m, size))
517 return -EFAULT;
3a4d5c94
MT
518 if (mem.padding)
519 return -EOPNOTSUPP;
520 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
521 return -E2BIG;
522 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
523 if (!newmem)
524 return -ENOMEM;
525
526 memcpy(newmem, &mem, size);
7ad9c9d2
TY
527 if (copy_from_user(newmem->regions, m->regions,
528 mem.nregions * sizeof *m->regions)) {
3a4d5c94 529 kfree(newmem);
7ad9c9d2 530 return -EFAULT;
3a4d5c94
MT
531 }
532
a02c3789
TY
533 if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
534 kfree(newmem);
3a4d5c94 535 return -EFAULT;
a02c3789 536 }
3a4d5c94
MT
537 oldmem = d->memory;
538 rcu_assign_pointer(d->memory, newmem);
539 synchronize_rcu();
540 kfree(oldmem);
541 return 0;
542}
543
544static int init_used(struct vhost_virtqueue *vq,
545 struct vring_used __user *used)
546{
547 int r = put_user(vq->used_flags, &used->flags);
548 if (r)
549 return r;
550 return get_user(vq->last_used_idx, &used->idx);
551}
552
553static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
554{
555 struct file *eventfp, *filep = NULL,
556 *pollstart = NULL, *pollstop = NULL;
557 struct eventfd_ctx *ctx = NULL;
558 u32 __user *idxp = argp;
559 struct vhost_virtqueue *vq;
560 struct vhost_vring_state s;
561 struct vhost_vring_file f;
562 struct vhost_vring_addr a;
563 u32 idx;
564 long r;
565
566 r = get_user(idx, idxp);
567 if (r < 0)
568 return r;
0f3d9a17 569 if (idx >= d->nvqs)
3a4d5c94
MT
570 return -ENOBUFS;
571
572 vq = d->vqs + idx;
573
574 mutex_lock(&vq->mutex);
575
576 switch (ioctl) {
577 case VHOST_SET_VRING_NUM:
578 /* Resizing ring with an active backend?
579 * You don't want to do that. */
580 if (vq->private_data) {
581 r = -EBUSY;
582 break;
583 }
7ad9c9d2
TY
584 if (copy_from_user(&s, argp, sizeof s)) {
585 r = -EFAULT;
3a4d5c94 586 break;
7ad9c9d2 587 }
3a4d5c94
MT
588 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
589 r = -EINVAL;
590 break;
591 }
592 vq->num = s.num;
593 break;
594 case VHOST_SET_VRING_BASE:
595 /* Moving base with an active backend?
596 * You don't want to do that. */
597 if (vq->private_data) {
598 r = -EBUSY;
599 break;
600 }
7ad9c9d2
TY
601 if (copy_from_user(&s, argp, sizeof s)) {
602 r = -EFAULT;
3a4d5c94 603 break;
7ad9c9d2 604 }
3a4d5c94
MT
605 if (s.num > 0xffff) {
606 r = -EINVAL;
607 break;
608 }
609 vq->last_avail_idx = s.num;
610 /* Forget the cached index value. */
611 vq->avail_idx = vq->last_avail_idx;
612 break;
613 case VHOST_GET_VRING_BASE:
614 s.index = idx;
615 s.num = vq->last_avail_idx;
7ad9c9d2
TY
616 if (copy_to_user(argp, &s, sizeof s))
617 r = -EFAULT;
3a4d5c94
MT
618 break;
619 case VHOST_SET_VRING_ADDR:
7ad9c9d2
TY
620 if (copy_from_user(&a, argp, sizeof a)) {
621 r = -EFAULT;
3a4d5c94 622 break;
7ad9c9d2 623 }
3a4d5c94
MT
624 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
625 r = -EOPNOTSUPP;
626 break;
627 }
628 /* For 32bit, verify that the top 32bits of the user
629 data are set to zero. */
630 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
631 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
632 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
633 r = -EFAULT;
634 break;
635 }
636 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
637 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
638 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
639 r = -EINVAL;
640 break;
641 }
642
643 /* We only verify access here if backend is configured.
644 * If it is not, we don't as size might not have been setup.
645 * We will verify when backend is configured. */
646 if (vq->private_data) {
647 if (!vq_access_ok(vq->num,
648 (void __user *)(unsigned long)a.desc_user_addr,
649 (void __user *)(unsigned long)a.avail_user_addr,
650 (void __user *)(unsigned long)a.used_user_addr)) {
651 r = -EINVAL;
652 break;
653 }
654
655 /* Also validate log access for used ring if enabled. */
656 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
657 !log_access_ok(vq->log_base, a.log_guest_addr,
658 sizeof *vq->used +
659 vq->num * sizeof *vq->used->ring)) {
660 r = -EINVAL;
661 break;
662 }
663 }
664
665 r = init_used(vq, (struct vring_used __user *)(unsigned long)
666 a.used_user_addr);
667 if (r)
668 break;
669 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
670 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
671 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
672 vq->log_addr = a.log_guest_addr;
673 vq->used = (void __user *)(unsigned long)a.used_user_addr;
674 break;
675 case VHOST_SET_VRING_KICK:
7ad9c9d2
TY
676 if (copy_from_user(&f, argp, sizeof f)) {
677 r = -EFAULT;
3a4d5c94 678 break;
7ad9c9d2 679 }
3a4d5c94 680 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
535297a6
MT
681 if (IS_ERR(eventfp)) {
682 r = PTR_ERR(eventfp);
683 break;
684 }
3a4d5c94
MT
685 if (eventfp != vq->kick) {
686 pollstop = filep = vq->kick;
687 pollstart = vq->kick = eventfp;
688 } else
689 filep = eventfp;
690 break;
691 case VHOST_SET_VRING_CALL:
7ad9c9d2
TY
692 if (copy_from_user(&f, argp, sizeof f)) {
693 r = -EFAULT;
3a4d5c94 694 break;
7ad9c9d2 695 }
3a4d5c94 696 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
535297a6
MT
697 if (IS_ERR(eventfp)) {
698 r = PTR_ERR(eventfp);
699 break;
700 }
3a4d5c94
MT
701 if (eventfp != vq->call) {
702 filep = vq->call;
703 ctx = vq->call_ctx;
704 vq->call = eventfp;
705 vq->call_ctx = eventfp ?
706 eventfd_ctx_fileget(eventfp) : NULL;
707 } else
708 filep = eventfp;
709 break;
710 case VHOST_SET_VRING_ERR:
7ad9c9d2
TY
711 if (copy_from_user(&f, argp, sizeof f)) {
712 r = -EFAULT;
3a4d5c94 713 break;
7ad9c9d2 714 }
3a4d5c94 715 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
535297a6
MT
716 if (IS_ERR(eventfp)) {
717 r = PTR_ERR(eventfp);
718 break;
719 }
3a4d5c94
MT
720 if (eventfp != vq->error) {
721 filep = vq->error;
722 vq->error = eventfp;
723 ctx = vq->error_ctx;
724 vq->error_ctx = eventfp ?
725 eventfd_ctx_fileget(eventfp) : NULL;
726 } else
727 filep = eventfp;
728 break;
729 default:
730 r = -ENOIOCTLCMD;
731 }
732
733 if (pollstop && vq->handle_kick)
734 vhost_poll_stop(&vq->poll);
735
736 if (ctx)
737 eventfd_ctx_put(ctx);
738 if (filep)
739 fput(filep);
740
741 if (pollstart && vq->handle_kick)
742 vhost_poll_start(&vq->poll, vq->kick);
743
744 mutex_unlock(&vq->mutex);
745
746 if (pollstop && vq->handle_kick)
747 vhost_poll_flush(&vq->poll);
748 return r;
749}
750
751/* Caller must have device mutex */
752long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
753{
754 void __user *argp = (void __user *)arg;
755 struct file *eventfp, *filep = NULL;
756 struct eventfd_ctx *ctx = NULL;
757 u64 p;
758 long r;
759 int i, fd;
760
761 /* If you are not the owner, you can become one */
762 if (ioctl == VHOST_SET_OWNER) {
763 r = vhost_dev_set_owner(d);
764 goto done;
765 }
766
767 /* You must be the owner to do anything else */
768 r = vhost_dev_check_owner(d);
769 if (r)
770 goto done;
771
772 switch (ioctl) {
773 case VHOST_SET_MEM_TABLE:
774 r = vhost_set_memory(d, argp);
775 break;
776 case VHOST_SET_LOG_BASE:
7ad9c9d2
TY
777 if (copy_from_user(&p, argp, sizeof p)) {
778 r = -EFAULT;
3a4d5c94 779 break;
7ad9c9d2 780 }
3a4d5c94
MT
781 if ((u64)(unsigned long)p != p) {
782 r = -EFAULT;
783 break;
784 }
785 for (i = 0; i < d->nvqs; ++i) {
786 struct vhost_virtqueue *vq;
787 void __user *base = (void __user *)(unsigned long)p;
788 vq = d->vqs + i;
789 mutex_lock(&vq->mutex);
790 /* If ring is inactive, will check when it's enabled. */
791 if (vq->private_data && !vq_log_access_ok(vq, base))
792 r = -EFAULT;
793 else
794 vq->log_base = base;
795 mutex_unlock(&vq->mutex);
796 }
797 break;
798 case VHOST_SET_LOG_FD:
799 r = get_user(fd, (int __user *)argp);
800 if (r < 0)
801 break;
802 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
803 if (IS_ERR(eventfp)) {
804 r = PTR_ERR(eventfp);
805 break;
806 }
807 if (eventfp != d->log_file) {
808 filep = d->log_file;
809 ctx = d->log_ctx;
810 d->log_ctx = eventfp ?
811 eventfd_ctx_fileget(eventfp) : NULL;
812 } else
813 filep = eventfp;
814 for (i = 0; i < d->nvqs; ++i) {
815 mutex_lock(&d->vqs[i].mutex);
816 d->vqs[i].log_ctx = d->log_ctx;
817 mutex_unlock(&d->vqs[i].mutex);
818 }
819 if (ctx)
820 eventfd_ctx_put(ctx);
821 if (filep)
822 fput(filep);
823 break;
824 default:
825 r = vhost_set_vring(d, ioctl, argp);
826 break;
827 }
828done:
829 return r;
830}
831
832static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
833 __u64 addr, __u32 len)
834{
835 struct vhost_memory_region *reg;
836 int i;
837 /* linear search is not brilliant, but we really have on the order of 6
838 * regions in practice */
839 for (i = 0; i < mem->nregions; ++i) {
840 reg = mem->regions + i;
841 if (reg->guest_phys_addr <= addr &&
842 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
843 return reg;
844 }
845 return NULL;
846}
847
848/* TODO: This is really inefficient. We need something like get_user()
849 * (instruction directly accesses the data, with an exception table entry
850 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
851 */
852static int set_bit_to_user(int nr, void __user *addr)
853{
854 unsigned long log = (unsigned long)addr;
855 struct page *page;
856 void *base;
857 int bit = nr + (log % PAGE_SIZE) * 8;
858 int r;
859 r = get_user_pages_fast(log, 1, 1, &page);
d6db3f5c 860 if (r < 0)
3a4d5c94 861 return r;
d6db3f5c 862 BUG_ON(r != 1);
3a4d5c94
MT
863 base = kmap_atomic(page, KM_USER0);
864 set_bit(bit, base);
865 kunmap_atomic(base, KM_USER0);
866 set_page_dirty_lock(page);
867 put_page(page);
868 return 0;
869}
870
871static int log_write(void __user *log_base,
872 u64 write_address, u64 write_length)
873{
874 int r;
875 if (!write_length)
876 return 0;
877 write_address /= VHOST_PAGE_SIZE;
878 for (;;) {
879 u64 base = (u64)(unsigned long)log_base;
880 u64 log = base + write_address / 8;
881 int bit = write_address % 8;
882 if ((u64)(unsigned long)log != log)
883 return -EFAULT;
884 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
885 if (r < 0)
886 return r;
887 if (write_length <= VHOST_PAGE_SIZE)
888 break;
889 write_length -= VHOST_PAGE_SIZE;
890 write_address += VHOST_PAGE_SIZE;
891 }
892 return r;
893}
894
895int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
896 unsigned int log_num, u64 len)
897{
898 int i, r;
899
900 /* Make sure data written is seen before log. */
5659338c 901 smp_wmb();
3a4d5c94
MT
902 for (i = 0; i < log_num; ++i) {
903 u64 l = min(log[i].len, len);
904 r = log_write(vq->log_base, log[i].addr, l);
905 if (r < 0)
906 return r;
907 len -= l;
5786aee8
MT
908 if (!len) {
909 if (vq->log_ctx)
910 eventfd_signal(vq->log_ctx, 1);
3a4d5c94 911 return 0;
5786aee8 912 }
3a4d5c94 913 }
3a4d5c94
MT
914 /* Length written exceeds what we have stored. This is a bug. */
915 BUG();
916 return 0;
917}
918
a8d3782f
CH
919static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
920 struct iovec iov[], int iov_size)
3a4d5c94
MT
921{
922 const struct vhost_memory_region *reg;
923 struct vhost_memory *mem;
924 struct iovec *_iov;
925 u64 s = 0;
926 int ret = 0;
927
928 rcu_read_lock();
929
930 mem = rcu_dereference(dev->memory);
931 while ((u64)len > s) {
932 u64 size;
7b3384fc 933 if (unlikely(ret >= iov_size)) {
3a4d5c94
MT
934 ret = -ENOBUFS;
935 break;
936 }
937 reg = find_region(mem, addr, len);
7b3384fc 938 if (unlikely(!reg)) {
3a4d5c94
MT
939 ret = -EFAULT;
940 break;
941 }
942 _iov = iov + ret;
943 size = reg->memory_size - addr + reg->guest_phys_addr;
944 _iov->iov_len = min((u64)len, size);
a8d3782f 945 _iov->iov_base = (void __user *)(unsigned long)
3a4d5c94
MT
946 (reg->userspace_addr + addr - reg->guest_phys_addr);
947 s += size;
948 addr += size;
949 ++ret;
950 }
951
952 rcu_read_unlock();
953 return ret;
954}
955
956/* Each buffer in the virtqueues is actually a chain of descriptors. This
957 * function returns the next descriptor in the chain,
958 * or -1U if we're at the end. */
959static unsigned next_desc(struct vring_desc *desc)
960{
961 unsigned int next;
962
963 /* If this descriptor says it doesn't chain, we're done. */
964 if (!(desc->flags & VRING_DESC_F_NEXT))
965 return -1U;
966
967 /* Check they're not leading us off end of descriptors. */
968 next = desc->next;
969 /* Make sure compiler knows to grab that: we don't want it changing! */
970 /* We will use the result as an index in an array, so most
971 * architectures only need a compiler barrier here. */
972 read_barrier_depends();
973
974 return next;
975}
976
7b3384fc
MT
977static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
978 struct iovec iov[], unsigned int iov_size,
979 unsigned int *out_num, unsigned int *in_num,
980 struct vhost_log *log, unsigned int *log_num,
981 struct vring_desc *indirect)
3a4d5c94
MT
982{
983 struct vring_desc desc;
984 unsigned int i = 0, count, found = 0;
985 int ret;
986
987 /* Sanity check */
7b3384fc 988 if (unlikely(indirect->len % sizeof desc)) {
3a4d5c94
MT
989 vq_err(vq, "Invalid length in indirect descriptor: "
990 "len 0x%llx not multiple of 0x%zx\n",
991 (unsigned long long)indirect->len,
992 sizeof desc);
993 return -EINVAL;
994 }
995
996 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
e0e9b406 997 UIO_MAXIOV);
7b3384fc 998 if (unlikely(ret < 0)) {
3a4d5c94
MT
999 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1000 return ret;
1001 }
1002
1003 /* We will use the result as an address to read from, so most
1004 * architectures only need a compiler barrier here. */
1005 read_barrier_depends();
1006
1007 count = indirect->len / sizeof desc;
1008 /* Buffers are chained via a 16 bit next field, so
1009 * we can have at most 2^16 of these. */
7b3384fc 1010 if (unlikely(count > USHRT_MAX + 1)) {
3a4d5c94
MT
1011 vq_err(vq, "Indirect buffer length too big: %d\n",
1012 indirect->len);
1013 return -E2BIG;
1014 }
1015
1016 do {
1017 unsigned iov_count = *in_num + *out_num;
7b3384fc 1018 if (unlikely(++found > count)) {
3a4d5c94
MT
1019 vq_err(vq, "Loop detected: last one at %u "
1020 "indirect size %u\n",
1021 i, count);
1022 return -EINVAL;
1023 }
7b3384fc
MT
1024 if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
1025 sizeof desc))) {
3a4d5c94
MT
1026 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1027 i, (size_t)indirect->addr + i * sizeof desc);
1028 return -EINVAL;
1029 }
7b3384fc 1030 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
3a4d5c94
MT
1031 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1032 i, (size_t)indirect->addr + i * sizeof desc);
1033 return -EINVAL;
1034 }
1035
1036 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1037 iov_size - iov_count);
7b3384fc 1038 if (unlikely(ret < 0)) {
3a4d5c94
MT
1039 vq_err(vq, "Translation failure %d indirect idx %d\n",
1040 ret, i);
1041 return ret;
1042 }
1043 /* If this is an input descriptor, increment that count. */
1044 if (desc.flags & VRING_DESC_F_WRITE) {
1045 *in_num += ret;
1046 if (unlikely(log)) {
1047 log[*log_num].addr = desc.addr;
1048 log[*log_num].len = desc.len;
1049 ++*log_num;
1050 }
1051 } else {
1052 /* If it's an output descriptor, they're all supposed
1053 * to come before any input descriptors. */
7b3384fc 1054 if (unlikely(*in_num)) {
3a4d5c94
MT
1055 vq_err(vq, "Indirect descriptor "
1056 "has out after in: idx %d\n", i);
1057 return -EINVAL;
1058 }
1059 *out_num += ret;
1060 }
1061 } while ((i = next_desc(&desc)) != -1);
1062 return 0;
1063}
1064
1065/* This looks in the virtqueue and for the first available buffer, and converts
1066 * it to an iovec for convenient access. Since descriptors consist of some
1067 * number of output then some number of input descriptors, it's actually two
1068 * iovecs, but we pack them into one and note how many of each there were.
1069 *
d5675bd2
MT
1070 * This function returns the descriptor number found, or vq->num (which is
1071 * never a valid descriptor number) if none was found. A negative code is
1072 * returned on error. */
1073int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1074 struct iovec iov[], unsigned int iov_size,
1075 unsigned int *out_num, unsigned int *in_num,
1076 struct vhost_log *log, unsigned int *log_num)
3a4d5c94
MT
1077{
1078 struct vring_desc desc;
1079 unsigned int i, head, found = 0;
1080 u16 last_avail_idx;
1081 int ret;
1082
1083 /* Check it isn't doing very strange things with descriptor numbers. */
1084 last_avail_idx = vq->last_avail_idx;
7b3384fc 1085 if (unlikely(get_user(vq->avail_idx, &vq->avail->idx))) {
3a4d5c94
MT
1086 vq_err(vq, "Failed to access avail idx at %p\n",
1087 &vq->avail->idx);
d5675bd2 1088 return -EFAULT;
3a4d5c94
MT
1089 }
1090
7b3384fc 1091 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
3a4d5c94
MT
1092 vq_err(vq, "Guest moved used index from %u to %u",
1093 last_avail_idx, vq->avail_idx);
d5675bd2 1094 return -EFAULT;
3a4d5c94
MT
1095 }
1096
1097 /* If there's nothing new since last we looked, return invalid. */
1098 if (vq->avail_idx == last_avail_idx)
1099 return vq->num;
1100
1101 /* Only get avail ring entries after they have been exposed by guest. */
5659338c 1102 smp_rmb();
3a4d5c94
MT
1103
1104 /* Grab the next descriptor number they're advertising, and increment
1105 * the index we've seen. */
7b3384fc
MT
1106 if (unlikely(get_user(head,
1107 &vq->avail->ring[last_avail_idx % vq->num]))) {
3a4d5c94
MT
1108 vq_err(vq, "Failed to read head: idx %d address %p\n",
1109 last_avail_idx,
1110 &vq->avail->ring[last_avail_idx % vq->num]);
d5675bd2 1111 return -EFAULT;
3a4d5c94
MT
1112 }
1113
1114 /* If their number is silly, that's an error. */
7b3384fc 1115 if (unlikely(head >= vq->num)) {
3a4d5c94
MT
1116 vq_err(vq, "Guest says index %u > %u is available",
1117 head, vq->num);
d5675bd2 1118 return -EINVAL;
3a4d5c94
MT
1119 }
1120
1121 /* When we start there are none of either input nor output. */
1122 *out_num = *in_num = 0;
1123 if (unlikely(log))
1124 *log_num = 0;
1125
1126 i = head;
1127 do {
1128 unsigned iov_count = *in_num + *out_num;
7b3384fc 1129 if (unlikely(i >= vq->num)) {
3a4d5c94
MT
1130 vq_err(vq, "Desc index is %u > %u, head = %u",
1131 i, vq->num, head);
d5675bd2 1132 return -EINVAL;
3a4d5c94 1133 }
7b3384fc 1134 if (unlikely(++found > vq->num)) {
3a4d5c94
MT
1135 vq_err(vq, "Loop detected: last one at %u "
1136 "vq size %u head %u\n",
1137 i, vq->num, head);
d5675bd2 1138 return -EINVAL;
3a4d5c94
MT
1139 }
1140 ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
7b3384fc 1141 if (unlikely(ret)) {
3a4d5c94
MT
1142 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1143 i, vq->desc + i);
d5675bd2 1144 return -EFAULT;
3a4d5c94
MT
1145 }
1146 if (desc.flags & VRING_DESC_F_INDIRECT) {
1147 ret = get_indirect(dev, vq, iov, iov_size,
1148 out_num, in_num,
1149 log, log_num, &desc);
7b3384fc 1150 if (unlikely(ret < 0)) {
3a4d5c94
MT
1151 vq_err(vq, "Failure detected "
1152 "in indirect descriptor at idx %d\n", i);
d5675bd2 1153 return ret;
3a4d5c94
MT
1154 }
1155 continue;
1156 }
1157
1158 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1159 iov_size - iov_count);
7b3384fc 1160 if (unlikely(ret < 0)) {
3a4d5c94
MT
1161 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1162 ret, i);
d5675bd2 1163 return ret;
3a4d5c94
MT
1164 }
1165 if (desc.flags & VRING_DESC_F_WRITE) {
1166 /* If this is an input descriptor,
1167 * increment that count. */
1168 *in_num += ret;
1169 if (unlikely(log)) {
1170 log[*log_num].addr = desc.addr;
1171 log[*log_num].len = desc.len;
1172 ++*log_num;
1173 }
1174 } else {
1175 /* If it's an output descriptor, they're all supposed
1176 * to come before any input descriptors. */
7b3384fc 1177 if (unlikely(*in_num)) {
3a4d5c94
MT
1178 vq_err(vq, "Descriptor has out after in: "
1179 "idx %d\n", i);
d5675bd2 1180 return -EINVAL;
3a4d5c94
MT
1181 }
1182 *out_num += ret;
1183 }
1184 } while ((i = next_desc(&desc)) != -1);
1185
1186 /* On success, increment avail index. */
1187 vq->last_avail_idx++;
1188 return head;
1189}
1190
1191/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
8dd014ad 1192void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
3a4d5c94 1193{
8dd014ad 1194 vq->last_avail_idx -= n;
3a4d5c94
MT
1195}
1196
1197/* After we've used one of their buffers, we tell them about it. We'll then
1198 * want to notify the guest, using eventfd. */
1199int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1200{
a8d3782f 1201 struct vring_used_elem __user *used;
3a4d5c94
MT
1202
1203 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1204 * next entry in that used ring. */
1205 used = &vq->used->ring[vq->last_used_idx % vq->num];
1206 if (put_user(head, &used->id)) {
1207 vq_err(vq, "Failed to write used id");
1208 return -EFAULT;
1209 }
1210 if (put_user(len, &used->len)) {
1211 vq_err(vq, "Failed to write used len");
1212 return -EFAULT;
1213 }
1214 /* Make sure buffer is written before we update index. */
5659338c 1215 smp_wmb();
3a4d5c94
MT
1216 if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
1217 vq_err(vq, "Failed to increment used idx");
1218 return -EFAULT;
1219 }
1220 if (unlikely(vq->log_used)) {
1221 /* Make sure data is seen before log. */
5659338c 1222 smp_wmb();
86e9424d
MT
1223 /* Log used ring entry write. */
1224 log_write(vq->log_base,
a8d3782f
CH
1225 vq->log_addr +
1226 ((void __user *)used - (void __user *)vq->used),
86e9424d
MT
1227 sizeof *used);
1228 /* Log used index update. */
1229 log_write(vq->log_base,
1230 vq->log_addr + offsetof(struct vring_used, idx),
1231 sizeof vq->used->idx);
3a4d5c94
MT
1232 if (vq->log_ctx)
1233 eventfd_signal(vq->log_ctx, 1);
1234 }
1235 vq->last_used_idx++;
1236 return 0;
1237}
1238
8dd014ad
DS
1239static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1240 struct vring_used_elem *heads,
1241 unsigned count)
1242{
1243 struct vring_used_elem __user *used;
1244 int start;
1245
1246 start = vq->last_used_idx % vq->num;
1247 used = vq->used->ring + start;
1248 if (copy_to_user(used, heads, count * sizeof *used)) {
1249 vq_err(vq, "Failed to write used");
1250 return -EFAULT;
1251 }
1252 if (unlikely(vq->log_used)) {
1253 /* Make sure data is seen before log. */
1254 smp_wmb();
1255 /* Log used ring entry write. */
1256 log_write(vq->log_base,
1257 vq->log_addr +
1258 ((void __user *)used - (void __user *)vq->used),
1259 count * sizeof *used);
1260 }
1261 vq->last_used_idx += count;
1262 return 0;
1263}
1264
1265/* After we've used one of their buffers, we tell them about it. We'll then
1266 * want to notify the guest, using eventfd. */
1267int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1268 unsigned count)
1269{
1270 int start, n, r;
1271
1272 start = vq->last_used_idx % vq->num;
1273 n = vq->num - start;
1274 if (n < count) {
1275 r = __vhost_add_used_n(vq, heads, n);
1276 if (r < 0)
1277 return r;
1278 heads += n;
1279 count -= n;
1280 }
1281 r = __vhost_add_used_n(vq, heads, count);
1282
1283 /* Make sure buffer is written before we update index. */
1284 smp_wmb();
1285 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1286 vq_err(vq, "Failed to increment used idx");
1287 return -EFAULT;
1288 }
1289 if (unlikely(vq->log_used)) {
1290 /* Log used index update. */
1291 log_write(vq->log_base,
1292 vq->log_addr + offsetof(struct vring_used, idx),
1293 sizeof vq->used->idx);
1294 if (vq->log_ctx)
1295 eventfd_signal(vq->log_ctx, 1);
1296 }
1297 return r;
1298}
1299
3a4d5c94
MT
1300/* This actually signals the guest, using eventfd. */
1301void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1302{
0d499356
MT
1303 __u16 flags;
1304 /* Flush out used index updates. This is paired
1305 * with the barrier that the Guest executes when enabling
1306 * interrupts. */
1307 smp_mb();
1308
3a4d5c94
MT
1309 if (get_user(flags, &vq->avail->flags)) {
1310 vq_err(vq, "Failed to get flags");
1311 return;
1312 }
1313
1314 /* If they don't want an interrupt, don't signal, unless empty. */
1315 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1316 (vq->avail_idx != vq->last_avail_idx ||
1317 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1318 return;
1319
1320 /* Signal the Guest tell them we used something up. */
1321 if (vq->call_ctx)
1322 eventfd_signal(vq->call_ctx, 1);
1323}
1324
1325/* And here's the combo meal deal. Supersize me! */
1326void vhost_add_used_and_signal(struct vhost_dev *dev,
1327 struct vhost_virtqueue *vq,
1328 unsigned int head, int len)
1329{
1330 vhost_add_used(vq, head, len);
1331 vhost_signal(dev, vq);
1332}
1333
8dd014ad
DS
1334/* multi-buffer version of vhost_add_used_and_signal */
1335void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1336 struct vhost_virtqueue *vq,
1337 struct vring_used_elem *heads, unsigned count)
1338{
1339 vhost_add_used_n(vq, heads, count);
1340 vhost_signal(dev, vq);
1341}
1342
3a4d5c94
MT
1343/* OK, now we need to know about added descriptors. */
1344bool vhost_enable_notify(struct vhost_virtqueue *vq)
1345{
1346 u16 avail_idx;
1347 int r;
1348 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1349 return false;
1350 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1351 r = put_user(vq->used_flags, &vq->used->flags);
1352 if (r) {
1353 vq_err(vq, "Failed to enable notification at %p: %d\n",
1354 &vq->used->flags, r);
1355 return false;
1356 }
1357 /* They could have slipped one in as we were doing that: make
1358 * sure it's written, then check again. */
5659338c 1359 smp_mb();
3a4d5c94
MT
1360 r = get_user(avail_idx, &vq->avail->idx);
1361 if (r) {
1362 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1363 &vq->avail->idx, r);
1364 return false;
1365 }
1366
8dd014ad 1367 return avail_idx != vq->avail_idx;
3a4d5c94
MT
1368}
1369
1370/* We don't need to be notified again. */
1371void vhost_disable_notify(struct vhost_virtqueue *vq)
1372{
1373 int r;
1374 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1375 return;
1376 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1377 r = put_user(vq->used_flags, &vq->used->flags);
1378 if (r)
1379 vq_err(vq, "Failed to enable notification at %p: %d\n",
1380 &vq->used->flags, r);
1381}