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