blob: 4c256d15c249971cdda20792ed7828cdde1c672e [file] [log] [blame]
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001/* 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>
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +020018#include <linux/mmu_context.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000019#include <linux/miscdevice.h>
20#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000021#include <linux/rcupdate.h>
22#include <linux/poll.h>
23#include <linux/file.h>
24#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Tejun Heoc23f34452010-06-02 20:40:00 +020026#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030027#include <linux/cgroup.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000028
29#include <linux/net.h>
30#include <linux/if_packet.h>
31#include <linux/if_arp.h>
32
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000033#include "vhost.h"
34
35enum {
36 VHOST_MEMORY_MAX_NREGIONS = 64,
37 VHOST_MEMORY_F_LOG = 0x1,
38};
39
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000040static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
41 poll_table *pt)
42{
43 struct vhost_poll *poll;
44 poll = container_of(pt, struct vhost_poll, table);
45
46 poll->wqh = wqh;
47 add_wait_queue(wqh, &poll->wait);
48}
49
50static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
51 void *key)
52{
Tejun Heoc23f34452010-06-02 20:40:00 +020053 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
54
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000055 if (!((unsigned long)key & poll->mask))
56 return 0;
57
Tejun Heoc23f34452010-06-02 20:40:00 +020058 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000059 return 0;
60}
61
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030062static void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000063{
Tejun Heoc23f34452010-06-02 20:40:00 +020064 INIT_LIST_HEAD(&work->node);
65 work->fn = fn;
66 init_waitqueue_head(&work->done);
67 work->flushing = 0;
68 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000069}
70
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030071/* Init poll structure */
72void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
73 unsigned long mask, struct vhost_dev *dev)
74{
75 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
76 init_poll_funcptr(&poll->table, vhost_poll_func);
77 poll->mask = mask;
78 poll->dev = dev;
79
80 vhost_work_init(&poll->work, fn);
81}
82
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000083/* 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. */
85void vhost_poll_start(struct vhost_poll *poll, struct file *file)
86{
87 unsigned long mask;
88 mask = file->f_op->poll(file, &poll->table);
89 if (mask)
90 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
91}
92
93/* Stop polling a file. After this function returns, it becomes safe to drop the
94 * file reference. You must also flush afterwards. */
95void vhost_poll_stop(struct vhost_poll *poll)
96{
97 remove_wait_queue(poll->wqh, &poll->wait);
98}
99
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300100static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000101{
Tejun Heoc23f34452010-06-02 20:40:00 +0200102 unsigned seq;
103 int left;
104 int flushing;
105
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300106 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200107 seq = work->queue_seq;
108 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300109 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200110 wait_event(work->done, ({
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300111 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200112 left = seq - work->done_seq <= 0;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300113 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200114 left;
115 }));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300116 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200117 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300118 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200119 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000120}
121
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300122/* Flush any work that has been scheduled. When calling this, don't hold any
123 * locks that are also used by the callback. */
124void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000125{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300126 vhost_work_flush(poll->dev, &poll->work);
127}
128
129static inline void vhost_work_queue(struct vhost_dev *dev,
130 struct vhost_work *work)
131{
Tejun Heoc23f34452010-06-02 20:40:00 +0200132 unsigned long flags;
133
134 spin_lock_irqsave(&dev->work_lock, flags);
135 if (list_empty(&work->node)) {
136 list_add_tail(&work->node, &dev->work_list);
137 work->queue_seq++;
138 wake_up_process(dev->worker);
139 }
140 spin_unlock_irqrestore(&dev->work_lock, flags);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000141}
142
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300143void vhost_poll_queue(struct vhost_poll *poll)
144{
145 vhost_work_queue(poll->dev, &poll->work);
146}
147
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000148static void vhost_vq_reset(struct vhost_dev *dev,
149 struct vhost_virtqueue *vq)
150{
151 vq->num = 1;
152 vq->desc = NULL;
153 vq->avail = NULL;
154 vq->used = NULL;
155 vq->last_avail_idx = 0;
156 vq->avail_idx = 0;
157 vq->last_used_idx = 0;
158 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000159 vq->log_used = false;
160 vq->log_addr = -1ull;
David Stevens8dd014a2010-07-27 18:52:21 +0300161 vq->vhost_hlen = 0;
162 vq->sock_hlen = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000163 vq->private_data = NULL;
164 vq->log_base = NULL;
165 vq->error_ctx = NULL;
166 vq->error = NULL;
167 vq->kick = NULL;
168 vq->call_ctx = NULL;
169 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200170 vq->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000171}
172
Tejun Heoc23f34452010-06-02 20:40:00 +0200173static int vhost_worker(void *data)
174{
175 struct vhost_dev *dev = data;
176 struct vhost_work *work = NULL;
177 unsigned uninitialized_var(seq);
178
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200179 use_mm(dev->mm);
180
Tejun Heoc23f34452010-06-02 20:40:00 +0200181 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);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200195 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200196 }
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 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200213 unuse_mm(dev->mm);
214 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200215}
216
Jason Wange0e9b402010-09-14 23:53:05 +0800217/* Helper to allocate iovec buffers for all vqs. */
218static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
219{
220 int i;
221 for (i = 0; i < dev->nvqs; ++i) {
222 dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
223 UIO_MAXIOV, GFP_KERNEL);
224 dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
225 GFP_KERNEL);
226 dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
227 UIO_MAXIOV, GFP_KERNEL);
228
229 if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
230 !dev->vqs[i].heads)
231 goto err_nomem;
232 }
233 return 0;
234err_nomem:
235 for (; i >= 0; --i) {
236 kfree(dev->vqs[i].indirect);
237 kfree(dev->vqs[i].log);
238 kfree(dev->vqs[i].heads);
239 }
240 return -ENOMEM;
241}
242
243static void vhost_dev_free_iovecs(struct vhost_dev *dev)
244{
245 int i;
246 for (i = 0; i < dev->nvqs; ++i) {
247 kfree(dev->vqs[i].indirect);
248 dev->vqs[i].indirect = NULL;
249 kfree(dev->vqs[i].log);
250 dev->vqs[i].log = NULL;
251 kfree(dev->vqs[i].heads);
252 dev->vqs[i].heads = NULL;
253 }
254}
255
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000256long vhost_dev_init(struct vhost_dev *dev,
257 struct vhost_virtqueue *vqs, int nvqs)
258{
259 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200260
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000261 dev->vqs = vqs;
262 dev->nvqs = nvqs;
263 mutex_init(&dev->mutex);
264 dev->log_ctx = NULL;
265 dev->log_file = NULL;
266 dev->memory = NULL;
267 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200268 spin_lock_init(&dev->work_lock);
269 INIT_LIST_HEAD(&dev->work_list);
270 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000271
272 for (i = 0; i < dev->nvqs; ++i) {
Jason Wange0e9b402010-09-14 23:53:05 +0800273 dev->vqs[i].log = NULL;
274 dev->vqs[i].indirect = NULL;
275 dev->vqs[i].heads = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000276 dev->vqs[i].dev = dev;
277 mutex_init(&dev->vqs[i].mutex);
278 vhost_vq_reset(dev, dev->vqs + i);
279 if (dev->vqs[i].handle_kick)
280 vhost_poll_init(&dev->vqs[i].poll,
Tejun Heoc23f34452010-06-02 20:40:00 +0200281 dev->vqs[i].handle_kick, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000282 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200283
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000284 return 0;
285}
286
287/* Caller should have device mutex */
288long vhost_dev_check_owner(struct vhost_dev *dev)
289{
290 /* Are you the owner? If not, I don't think you mean to do that */
291 return dev->mm == current->mm ? 0 : -EPERM;
292}
293
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300294struct vhost_attach_cgroups_struct {
295 struct vhost_work work;
296 struct task_struct *owner;
297 int ret;
298};
299
300static void vhost_attach_cgroups_work(struct vhost_work *work)
301{
302 struct vhost_attach_cgroups_struct *s;
303 s = container_of(work, struct vhost_attach_cgroups_struct, work);
304 s->ret = cgroup_attach_task_all(s->owner, current);
305}
306
307static int vhost_attach_cgroups(struct vhost_dev *dev)
308{
309 struct vhost_attach_cgroups_struct attach;
310 attach.owner = current;
311 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
312 vhost_work_queue(dev, &attach.work);
313 vhost_work_flush(dev, &attach.work);
314 return attach.ret;
315}
316
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000317/* Caller should have device mutex */
318static long vhost_dev_set_owner(struct vhost_dev *dev)
319{
Tejun Heoc23f34452010-06-02 20:40:00 +0200320 struct task_struct *worker;
321 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000322 /* Is there an owner already? */
Tejun Heoc23f34452010-06-02 20:40:00 +0200323 if (dev->mm) {
324 err = -EBUSY;
325 goto err_mm;
326 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000327 /* No owner, become one */
328 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200329 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
330 if (IS_ERR(worker)) {
331 err = PTR_ERR(worker);
332 goto err_worker;
333 }
334
335 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300336 wake_up_process(worker); /* avoid contributing to loadavg */
337
338 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300339 if (err)
340 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200341
Jason Wange0e9b402010-09-14 23:53:05 +0800342 err = vhost_dev_alloc_iovecs(dev);
343 if (err)
344 goto err_cgroup;
345
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000346 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300347err_cgroup:
348 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300349 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200350err_worker:
351 if (dev->mm)
352 mmput(dev->mm);
353 dev->mm = NULL;
354err_mm:
355 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000356}
357
358/* Caller should have device mutex */
359long vhost_dev_reset_owner(struct vhost_dev *dev)
360{
361 struct vhost_memory *memory;
362
363 /* Restore memory to default empty mapping. */
364 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
365 if (!memory)
366 return -ENOMEM;
367
368 vhost_dev_cleanup(dev);
369
370 memory->nregions = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100371 RCU_INIT_POINTER(dev->memory, memory);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000372 return 0;
373}
374
375/* Caller should have device mutex */
376void vhost_dev_cleanup(struct vhost_dev *dev)
377{
378 int i;
379 for (i = 0; i < dev->nvqs; ++i) {
380 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
381 vhost_poll_stop(&dev->vqs[i].poll);
382 vhost_poll_flush(&dev->vqs[i].poll);
383 }
384 if (dev->vqs[i].error_ctx)
385 eventfd_ctx_put(dev->vqs[i].error_ctx);
386 if (dev->vqs[i].error)
387 fput(dev->vqs[i].error);
388 if (dev->vqs[i].kick)
389 fput(dev->vqs[i].kick);
390 if (dev->vqs[i].call_ctx)
391 eventfd_ctx_put(dev->vqs[i].call_ctx);
392 if (dev->vqs[i].call)
393 fput(dev->vqs[i].call);
394 vhost_vq_reset(dev, dev->vqs + i);
395 }
Jason Wange0e9b402010-09-14 23:53:05 +0800396 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000397 if (dev->log_ctx)
398 eventfd_ctx_put(dev->log_ctx);
399 dev->log_ctx = NULL;
400 if (dev->log_file)
401 fput(dev->log_file);
402 dev->log_file = NULL;
403 /* No one will access memory at this point */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100404 kfree(rcu_dereference_protected(dev->memory,
405 lockdep_is_held(&dev->mutex)));
406 RCU_INIT_POINTER(dev->memory, NULL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200407 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000408 if (dev->worker) {
409 kthread_stop(dev->worker);
410 dev->worker = NULL;
411 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200412 if (dev->mm)
413 mmput(dev->mm);
414 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000415}
416
417static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
418{
419 u64 a = addr / VHOST_PAGE_SIZE / 8;
420 /* Make sure 64 bit math will not overflow. */
421 if (a > ULONG_MAX - (unsigned long)log_base ||
422 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200423 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000424
425 return access_ok(VERIFY_WRITE, log_base + a,
426 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
427}
428
429/* Caller should have vq mutex and device mutex. */
430static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
431 int log_all)
432{
433 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400434
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300435 if (!mem)
436 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400437
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000438 for (i = 0; i < mem->nregions; ++i) {
439 struct vhost_memory_region *m = mem->regions + i;
440 unsigned long a = m->userspace_addr;
441 if (m->memory_size > ULONG_MAX)
442 return 0;
443 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
444 m->memory_size))
445 return 0;
446 else if (log_all && !log_access_ok(log_base,
447 m->guest_phys_addr,
448 m->memory_size))
449 return 0;
450 }
451 return 1;
452}
453
454/* Can we switch to this memory table? */
455/* Caller should have device mutex but not vq mutex */
456static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
457 int log_all)
458{
459 int i;
460 for (i = 0; i < d->nvqs; ++i) {
461 int ok;
462 mutex_lock(&d->vqs[i].mutex);
463 /* If ring is inactive, will check when it's enabled. */
464 if (d->vqs[i].private_data)
465 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
466 log_all);
467 else
468 ok = 1;
469 mutex_unlock(&d->vqs[i].mutex);
470 if (!ok)
471 return 0;
472 }
473 return 1;
474}
475
476static int vq_access_ok(unsigned int num,
477 struct vring_desc __user *desc,
478 struct vring_avail __user *avail,
479 struct vring_used __user *used)
480{
481 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
482 access_ok(VERIFY_READ, avail,
483 sizeof *avail + num * sizeof *avail->ring) &&
484 access_ok(VERIFY_WRITE, used,
485 sizeof *used + num * sizeof *used->ring);
486}
487
488/* Can we log writes? */
489/* Caller should have device mutex but not vq mutex */
490int vhost_log_access_ok(struct vhost_dev *dev)
491{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100492 struct vhost_memory *mp;
493
494 mp = rcu_dereference_protected(dev->memory,
495 lockdep_is_held(&dev->mutex));
496 return memory_access_ok(dev, mp, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000497}
498
499/* Verify access for write logging. */
500/* Caller should have vq mutex and device mutex */
501static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
502{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100503 struct vhost_memory *mp;
504
505 mp = rcu_dereference_protected(vq->dev->memory,
506 lockdep_is_held(&vq->mutex));
507 return vq_memory_access_ok(log_base, mp,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000508 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
509 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
510 sizeof *vq->used +
511 vq->num * sizeof *vq->used->ring));
512}
513
514/* Can we start vq? */
515/* Caller should have vq mutex and device mutex */
516int vhost_vq_access_ok(struct vhost_virtqueue *vq)
517{
518 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
519 vq_log_access_ok(vq, vq->log_base);
520}
521
522static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
523{
524 struct vhost_memory mem, *newmem, *oldmem;
525 unsigned long size = offsetof(struct vhost_memory, regions);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900526 if (copy_from_user(&mem, m, size))
527 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000528 if (mem.padding)
529 return -EOPNOTSUPP;
530 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
531 return -E2BIG;
532 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
533 if (!newmem)
534 return -ENOMEM;
535
536 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900537 if (copy_from_user(newmem->regions, m->regions,
538 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000539 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900540 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000541 }
542
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900543 if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
544 kfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000545 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900546 }
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100547 oldmem = rcu_dereference_protected(d->memory,
548 lockdep_is_held(&d->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000549 rcu_assign_pointer(d->memory, newmem);
550 synchronize_rcu();
551 kfree(oldmem);
552 return 0;
553}
554
555static int init_used(struct vhost_virtqueue *vq,
556 struct vring_used __user *used)
557{
558 int r = put_user(vq->used_flags, &used->flags);
559 if (r)
560 return r;
561 return get_user(vq->last_used_idx, &used->idx);
562}
563
564static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
565{
566 struct file *eventfp, *filep = NULL,
567 *pollstart = NULL, *pollstop = NULL;
568 struct eventfd_ctx *ctx = NULL;
569 u32 __user *idxp = argp;
570 struct vhost_virtqueue *vq;
571 struct vhost_vring_state s;
572 struct vhost_vring_file f;
573 struct vhost_vring_addr a;
574 u32 idx;
575 long r;
576
577 r = get_user(idx, idxp);
578 if (r < 0)
579 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530580 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000581 return -ENOBUFS;
582
583 vq = d->vqs + idx;
584
585 mutex_lock(&vq->mutex);
586
587 switch (ioctl) {
588 case VHOST_SET_VRING_NUM:
589 /* Resizing ring with an active backend?
590 * You don't want to do that. */
591 if (vq->private_data) {
592 r = -EBUSY;
593 break;
594 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900595 if (copy_from_user(&s, argp, sizeof s)) {
596 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000597 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900598 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000599 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
600 r = -EINVAL;
601 break;
602 }
603 vq->num = s.num;
604 break;
605 case VHOST_SET_VRING_BASE:
606 /* Moving base with an active backend?
607 * You don't want to do that. */
608 if (vq->private_data) {
609 r = -EBUSY;
610 break;
611 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900612 if (copy_from_user(&s, argp, sizeof s)) {
613 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000614 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900615 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000616 if (s.num > 0xffff) {
617 r = -EINVAL;
618 break;
619 }
620 vq->last_avail_idx = s.num;
621 /* Forget the cached index value. */
622 vq->avail_idx = vq->last_avail_idx;
623 break;
624 case VHOST_GET_VRING_BASE:
625 s.index = idx;
626 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900627 if (copy_to_user(argp, &s, sizeof s))
628 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000629 break;
630 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900631 if (copy_from_user(&a, argp, sizeof a)) {
632 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000633 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900634 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000635 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
636 r = -EOPNOTSUPP;
637 break;
638 }
639 /* For 32bit, verify that the top 32bits of the user
640 data are set to zero. */
641 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
642 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
643 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
644 r = -EFAULT;
645 break;
646 }
647 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
648 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
649 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
650 r = -EINVAL;
651 break;
652 }
653
654 /* We only verify access here if backend is configured.
655 * If it is not, we don't as size might not have been setup.
656 * We will verify when backend is configured. */
657 if (vq->private_data) {
658 if (!vq_access_ok(vq->num,
659 (void __user *)(unsigned long)a.desc_user_addr,
660 (void __user *)(unsigned long)a.avail_user_addr,
661 (void __user *)(unsigned long)a.used_user_addr)) {
662 r = -EINVAL;
663 break;
664 }
665
666 /* Also validate log access for used ring if enabled. */
667 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
668 !log_access_ok(vq->log_base, a.log_guest_addr,
669 sizeof *vq->used +
670 vq->num * sizeof *vq->used->ring)) {
671 r = -EINVAL;
672 break;
673 }
674 }
675
676 r = init_used(vq, (struct vring_used __user *)(unsigned long)
677 a.used_user_addr);
678 if (r)
679 break;
680 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
681 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
682 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
683 vq->log_addr = a.log_guest_addr;
684 vq->used = (void __user *)(unsigned long)a.used_user_addr;
685 break;
686 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900687 if (copy_from_user(&f, argp, sizeof f)) {
688 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000689 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900690 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000691 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200692 if (IS_ERR(eventfp)) {
693 r = PTR_ERR(eventfp);
694 break;
695 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000696 if (eventfp != vq->kick) {
697 pollstop = filep = vq->kick;
698 pollstart = vq->kick = eventfp;
699 } else
700 filep = eventfp;
701 break;
702 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900703 if (copy_from_user(&f, argp, sizeof f)) {
704 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000705 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900706 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000707 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200708 if (IS_ERR(eventfp)) {
709 r = PTR_ERR(eventfp);
710 break;
711 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000712 if (eventfp != vq->call) {
713 filep = vq->call;
714 ctx = vq->call_ctx;
715 vq->call = eventfp;
716 vq->call_ctx = eventfp ?
717 eventfd_ctx_fileget(eventfp) : NULL;
718 } else
719 filep = eventfp;
720 break;
721 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900722 if (copy_from_user(&f, argp, sizeof f)) {
723 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000724 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900725 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000726 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200727 if (IS_ERR(eventfp)) {
728 r = PTR_ERR(eventfp);
729 break;
730 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000731 if (eventfp != vq->error) {
732 filep = vq->error;
733 vq->error = eventfp;
734 ctx = vq->error_ctx;
735 vq->error_ctx = eventfp ?
736 eventfd_ctx_fileget(eventfp) : NULL;
737 } else
738 filep = eventfp;
739 break;
740 default:
741 r = -ENOIOCTLCMD;
742 }
743
744 if (pollstop && vq->handle_kick)
745 vhost_poll_stop(&vq->poll);
746
747 if (ctx)
748 eventfd_ctx_put(ctx);
749 if (filep)
750 fput(filep);
751
752 if (pollstart && vq->handle_kick)
753 vhost_poll_start(&vq->poll, vq->kick);
754
755 mutex_unlock(&vq->mutex);
756
757 if (pollstop && vq->handle_kick)
758 vhost_poll_flush(&vq->poll);
759 return r;
760}
761
762/* Caller must have device mutex */
763long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
764{
765 void __user *argp = (void __user *)arg;
766 struct file *eventfp, *filep = NULL;
767 struct eventfd_ctx *ctx = NULL;
768 u64 p;
769 long r;
770 int i, fd;
771
772 /* If you are not the owner, you can become one */
773 if (ioctl == VHOST_SET_OWNER) {
774 r = vhost_dev_set_owner(d);
775 goto done;
776 }
777
778 /* You must be the owner to do anything else */
779 r = vhost_dev_check_owner(d);
780 if (r)
781 goto done;
782
783 switch (ioctl) {
784 case VHOST_SET_MEM_TABLE:
785 r = vhost_set_memory(d, argp);
786 break;
787 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900788 if (copy_from_user(&p, argp, sizeof p)) {
789 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000790 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900791 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000792 if ((u64)(unsigned long)p != p) {
793 r = -EFAULT;
794 break;
795 }
796 for (i = 0; i < d->nvqs; ++i) {
797 struct vhost_virtqueue *vq;
798 void __user *base = (void __user *)(unsigned long)p;
799 vq = d->vqs + i;
800 mutex_lock(&vq->mutex);
801 /* If ring is inactive, will check when it's enabled. */
802 if (vq->private_data && !vq_log_access_ok(vq, base))
803 r = -EFAULT;
804 else
805 vq->log_base = base;
806 mutex_unlock(&vq->mutex);
807 }
808 break;
809 case VHOST_SET_LOG_FD:
810 r = get_user(fd, (int __user *)argp);
811 if (r < 0)
812 break;
813 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
814 if (IS_ERR(eventfp)) {
815 r = PTR_ERR(eventfp);
816 break;
817 }
818 if (eventfp != d->log_file) {
819 filep = d->log_file;
820 ctx = d->log_ctx;
821 d->log_ctx = eventfp ?
822 eventfd_ctx_fileget(eventfp) : NULL;
823 } else
824 filep = eventfp;
825 for (i = 0; i < d->nvqs; ++i) {
826 mutex_lock(&d->vqs[i].mutex);
827 d->vqs[i].log_ctx = d->log_ctx;
828 mutex_unlock(&d->vqs[i].mutex);
829 }
830 if (ctx)
831 eventfd_ctx_put(ctx);
832 if (filep)
833 fput(filep);
834 break;
835 default:
836 r = vhost_set_vring(d, ioctl, argp);
837 break;
838 }
839done:
840 return r;
841}
842
843static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
844 __u64 addr, __u32 len)
845{
846 struct vhost_memory_region *reg;
847 int i;
848 /* linear search is not brilliant, but we really have on the order of 6
849 * regions in practice */
850 for (i = 0; i < mem->nregions; ++i) {
851 reg = mem->regions + i;
852 if (reg->guest_phys_addr <= addr &&
853 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
854 return reg;
855 }
856 return NULL;
857}
858
859/* TODO: This is really inefficient. We need something like get_user()
860 * (instruction directly accesses the data, with an exception table entry
861 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
862 */
863static int set_bit_to_user(int nr, void __user *addr)
864{
865 unsigned long log = (unsigned long)addr;
866 struct page *page;
867 void *base;
868 int bit = nr + (log % PAGE_SIZE) * 8;
869 int r;
870 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200871 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000872 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200873 BUG_ON(r != 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000874 base = kmap_atomic(page, KM_USER0);
875 set_bit(bit, base);
876 kunmap_atomic(base, KM_USER0);
877 set_page_dirty_lock(page);
878 put_page(page);
879 return 0;
880}
881
882static int log_write(void __user *log_base,
883 u64 write_address, u64 write_length)
884{
885 int r;
886 if (!write_length)
887 return 0;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +0200888 write_length += write_address % VHOST_PAGE_SIZE;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000889 write_address /= VHOST_PAGE_SIZE;
890 for (;;) {
891 u64 base = (u64)(unsigned long)log_base;
892 u64 log = base + write_address / 8;
893 int bit = write_address % 8;
894 if ((u64)(unsigned long)log != log)
895 return -EFAULT;
896 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
897 if (r < 0)
898 return r;
899 if (write_length <= VHOST_PAGE_SIZE)
900 break;
901 write_length -= VHOST_PAGE_SIZE;
Michael S. Tsirkin3bf9be42010-11-29 10:19:07 +0200902 write_address += 1;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000903 }
904 return r;
905}
906
907int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
908 unsigned int log_num, u64 len)
909{
910 int i, r;
911
912 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000913 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000914 for (i = 0; i < log_num; ++i) {
915 u64 l = min(log[i].len, len);
916 r = log_write(vq->log_base, log[i].addr, l);
917 if (r < 0)
918 return r;
919 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200920 if (!len) {
921 if (vq->log_ctx)
922 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000923 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200924 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000925 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000926 /* Length written exceeds what we have stored. This is a bug. */
927 BUG();
928 return 0;
929}
930
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400931static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
932 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000933{
934 const struct vhost_memory_region *reg;
935 struct vhost_memory *mem;
936 struct iovec *_iov;
937 u64 s = 0;
938 int ret = 0;
939
940 rcu_read_lock();
941
942 mem = rcu_dereference(dev->memory);
943 while ((u64)len > s) {
944 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300945 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000946 ret = -ENOBUFS;
947 break;
948 }
949 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300950 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000951 ret = -EFAULT;
952 break;
953 }
954 _iov = iov + ret;
955 size = reg->memory_size - addr + reg->guest_phys_addr;
956 _iov->iov_len = min((u64)len, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400957 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000958 (reg->userspace_addr + addr - reg->guest_phys_addr);
959 s += size;
960 addr += size;
961 ++ret;
962 }
963
964 rcu_read_unlock();
965 return ret;
966}
967
968/* Each buffer in the virtqueues is actually a chain of descriptors. This
969 * function returns the next descriptor in the chain,
970 * or -1U if we're at the end. */
971static unsigned next_desc(struct vring_desc *desc)
972{
973 unsigned int next;
974
975 /* If this descriptor says it doesn't chain, we're done. */
976 if (!(desc->flags & VRING_DESC_F_NEXT))
977 return -1U;
978
979 /* Check they're not leading us off end of descriptors. */
980 next = desc->next;
981 /* Make sure compiler knows to grab that: we don't want it changing! */
982 /* We will use the result as an index in an array, so most
983 * architectures only need a compiler barrier here. */
984 read_barrier_depends();
985
986 return next;
987}
988
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300989static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
990 struct iovec iov[], unsigned int iov_size,
991 unsigned int *out_num, unsigned int *in_num,
992 struct vhost_log *log, unsigned int *log_num,
993 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000994{
995 struct vring_desc desc;
996 unsigned int i = 0, count, found = 0;
997 int ret;
998
999 /* Sanity check */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001000 if (unlikely(indirect->len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001001 vq_err(vq, "Invalid length in indirect descriptor: "
1002 "len 0x%llx not multiple of 0x%zx\n",
1003 (unsigned long long)indirect->len,
1004 sizeof desc);
1005 return -EINVAL;
1006 }
1007
1008 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001009 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001010 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001011 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1012 return ret;
1013 }
1014
1015 /* We will use the result as an address to read from, so most
1016 * architectures only need a compiler barrier here. */
1017 read_barrier_depends();
1018
1019 count = indirect->len / sizeof desc;
1020 /* Buffers are chained via a 16 bit next field, so
1021 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001022 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001023 vq_err(vq, "Indirect buffer length too big: %d\n",
1024 indirect->len);
1025 return -E2BIG;
1026 }
1027
1028 do {
1029 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001030 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001031 vq_err(vq, "Loop detected: last one at %u "
1032 "indirect size %u\n",
1033 i, count);
1034 return -EINVAL;
1035 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001036 if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
1037 sizeof desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001038 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1039 i, (size_t)indirect->addr + i * sizeof desc);
1040 return -EINVAL;
1041 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001042 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001043 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1044 i, (size_t)indirect->addr + i * sizeof desc);
1045 return -EINVAL;
1046 }
1047
1048 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1049 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001050 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001051 vq_err(vq, "Translation failure %d indirect idx %d\n",
1052 ret, i);
1053 return ret;
1054 }
1055 /* If this is an input descriptor, increment that count. */
1056 if (desc.flags & VRING_DESC_F_WRITE) {
1057 *in_num += ret;
1058 if (unlikely(log)) {
1059 log[*log_num].addr = desc.addr;
1060 log[*log_num].len = desc.len;
1061 ++*log_num;
1062 }
1063 } else {
1064 /* If it's an output descriptor, they're all supposed
1065 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001066 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001067 vq_err(vq, "Indirect descriptor "
1068 "has out after in: idx %d\n", i);
1069 return -EINVAL;
1070 }
1071 *out_num += ret;
1072 }
1073 } while ((i = next_desc(&desc)) != -1);
1074 return 0;
1075}
1076
1077/* This looks in the virtqueue and for the first available buffer, and converts
1078 * it to an iovec for convenient access. Since descriptors consist of some
1079 * number of output then some number of input descriptors, it's actually two
1080 * iovecs, but we pack them into one and note how many of each there were.
1081 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001082 * This function returns the descriptor number found, or vq->num (which is
1083 * never a valid descriptor number) if none was found. A negative code is
1084 * returned on error. */
1085int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1086 struct iovec iov[], unsigned int iov_size,
1087 unsigned int *out_num, unsigned int *in_num,
1088 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001089{
1090 struct vring_desc desc;
1091 unsigned int i, head, found = 0;
1092 u16 last_avail_idx;
1093 int ret;
1094
1095 /* Check it isn't doing very strange things with descriptor numbers. */
1096 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001097 if (unlikely(__get_user(vq->avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001098 vq_err(vq, "Failed to access avail idx at %p\n",
1099 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001100 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001101 }
1102
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001103 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001104 vq_err(vq, "Guest moved used index from %u to %u",
1105 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001106 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001107 }
1108
1109 /* If there's nothing new since last we looked, return invalid. */
1110 if (vq->avail_idx == last_avail_idx)
1111 return vq->num;
1112
1113 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001114 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001115
1116 /* Grab the next descriptor number they're advertising, and increment
1117 * the index we've seen. */
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001118 if (unlikely(__get_user(head,
1119 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001120 vq_err(vq, "Failed to read head: idx %d address %p\n",
1121 last_avail_idx,
1122 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001123 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001124 }
1125
1126 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001127 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001128 vq_err(vq, "Guest says index %u > %u is available",
1129 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001130 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001131 }
1132
1133 /* When we start there are none of either input nor output. */
1134 *out_num = *in_num = 0;
1135 if (unlikely(log))
1136 *log_num = 0;
1137
1138 i = head;
1139 do {
1140 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001141 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001142 vq_err(vq, "Desc index is %u > %u, head = %u",
1143 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001144 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001145 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001146 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001147 vq_err(vq, "Loop detected: last one at %u "
1148 "vq size %u head %u\n",
1149 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001150 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001151 }
1152 ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001153 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001154 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1155 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001156 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001157 }
1158 if (desc.flags & VRING_DESC_F_INDIRECT) {
1159 ret = get_indirect(dev, vq, iov, iov_size,
1160 out_num, in_num,
1161 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001162 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001163 vq_err(vq, "Failure detected "
1164 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001165 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001166 }
1167 continue;
1168 }
1169
1170 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1171 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001172 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001173 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1174 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001175 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001176 }
1177 if (desc.flags & VRING_DESC_F_WRITE) {
1178 /* If this is an input descriptor,
1179 * increment that count. */
1180 *in_num += ret;
1181 if (unlikely(log)) {
1182 log[*log_num].addr = desc.addr;
1183 log[*log_num].len = desc.len;
1184 ++*log_num;
1185 }
1186 } else {
1187 /* If it's an output descriptor, they're all supposed
1188 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001189 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001190 vq_err(vq, "Descriptor has out after in: "
1191 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001192 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001193 }
1194 *out_num += ret;
1195 }
1196 } while ((i = next_desc(&desc)) != -1);
1197
1198 /* On success, increment avail index. */
1199 vq->last_avail_idx++;
1200 return head;
1201}
1202
1203/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001204void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001205{
David Stevens8dd014a2010-07-27 18:52:21 +03001206 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001207}
1208
1209/* After we've used one of their buffers, we tell them about it. We'll then
1210 * want to notify the guest, using eventfd. */
1211int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1212{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001213 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001214
1215 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1216 * next entry in that used ring. */
1217 used = &vq->used->ring[vq->last_used_idx % vq->num];
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001218 if (__put_user(head, &used->id)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001219 vq_err(vq, "Failed to write used id");
1220 return -EFAULT;
1221 }
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001222 if (__put_user(len, &used->len)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001223 vq_err(vq, "Failed to write used len");
1224 return -EFAULT;
1225 }
1226 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001227 smp_wmb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001228 if (__put_user(vq->last_used_idx + 1, &vq->used->idx)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001229 vq_err(vq, "Failed to increment used idx");
1230 return -EFAULT;
1231 }
1232 if (unlikely(vq->log_used)) {
1233 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001234 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001235 /* Log used ring entry write. */
1236 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001237 vq->log_addr +
1238 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001239 sizeof *used);
1240 /* Log used index update. */
1241 log_write(vq->log_base,
1242 vq->log_addr + offsetof(struct vring_used, idx),
1243 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001244 if (vq->log_ctx)
1245 eventfd_signal(vq->log_ctx, 1);
1246 }
1247 vq->last_used_idx++;
1248 return 0;
1249}
1250
David Stevens8dd014a2010-07-27 18:52:21 +03001251static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1252 struct vring_used_elem *heads,
1253 unsigned count)
1254{
1255 struct vring_used_elem __user *used;
1256 int start;
1257
1258 start = vq->last_used_idx % vq->num;
1259 used = vq->used->ring + start;
Michael S. Tsirkindfe5ac52010-09-21 14:18:01 +02001260 if (__copy_to_user(used, heads, count * sizeof *used)) {
David Stevens8dd014a2010-07-27 18:52:21 +03001261 vq_err(vq, "Failed to write used");
1262 return -EFAULT;
1263 }
1264 if (unlikely(vq->log_used)) {
1265 /* Make sure data is seen before log. */
1266 smp_wmb();
1267 /* Log used ring entry write. */
1268 log_write(vq->log_base,
1269 vq->log_addr +
1270 ((void __user *)used - (void __user *)vq->used),
1271 count * sizeof *used);
1272 }
1273 vq->last_used_idx += count;
1274 return 0;
1275}
1276
1277/* After we've used one of their buffers, we tell them about it. We'll then
1278 * want to notify the guest, using eventfd. */
1279int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1280 unsigned count)
1281{
1282 int start, n, r;
1283
1284 start = vq->last_used_idx % vq->num;
1285 n = vq->num - start;
1286 if (n < count) {
1287 r = __vhost_add_used_n(vq, heads, n);
1288 if (r < 0)
1289 return r;
1290 heads += n;
1291 count -= n;
1292 }
1293 r = __vhost_add_used_n(vq, heads, count);
1294
1295 /* Make sure buffer is written before we update index. */
1296 smp_wmb();
1297 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1298 vq_err(vq, "Failed to increment used idx");
1299 return -EFAULT;
1300 }
1301 if (unlikely(vq->log_used)) {
1302 /* Log used index update. */
1303 log_write(vq->log_base,
1304 vq->log_addr + offsetof(struct vring_used, idx),
1305 sizeof vq->used->idx);
1306 if (vq->log_ctx)
1307 eventfd_signal(vq->log_ctx, 1);
1308 }
1309 return r;
1310}
1311
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001312/* This actually signals the guest, using eventfd. */
1313void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1314{
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001315 __u16 flags;
1316 /* Flush out used index updates. This is paired
1317 * with the barrier that the Guest executes when enabling
1318 * interrupts. */
1319 smp_mb();
1320
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001321 if (__get_user(flags, &vq->avail->flags)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001322 vq_err(vq, "Failed to get flags");
1323 return;
1324 }
1325
1326 /* If they don't want an interrupt, don't signal, unless empty. */
1327 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1328 (vq->avail_idx != vq->last_avail_idx ||
1329 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1330 return;
1331
1332 /* Signal the Guest tell them we used something up. */
1333 if (vq->call_ctx)
1334 eventfd_signal(vq->call_ctx, 1);
1335}
1336
1337/* And here's the combo meal deal. Supersize me! */
1338void vhost_add_used_and_signal(struct vhost_dev *dev,
1339 struct vhost_virtqueue *vq,
1340 unsigned int head, int len)
1341{
1342 vhost_add_used(vq, head, len);
1343 vhost_signal(dev, vq);
1344}
1345
David Stevens8dd014a2010-07-27 18:52:21 +03001346/* multi-buffer version of vhost_add_used_and_signal */
1347void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1348 struct vhost_virtqueue *vq,
1349 struct vring_used_elem *heads, unsigned count)
1350{
1351 vhost_add_used_n(vq, heads, count);
1352 vhost_signal(dev, vq);
1353}
1354
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001355/* OK, now we need to know about added descriptors. */
1356bool vhost_enable_notify(struct vhost_virtqueue *vq)
1357{
1358 u16 avail_idx;
1359 int r;
1360 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1361 return false;
1362 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1363 r = put_user(vq->used_flags, &vq->used->flags);
1364 if (r) {
1365 vq_err(vq, "Failed to enable notification at %p: %d\n",
1366 &vq->used->flags, r);
1367 return false;
1368 }
1369 /* They could have slipped one in as we were doing that: make
1370 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001371 smp_mb();
Michael S. Tsirkin8b7347a2010-09-19 15:56:30 +02001372 r = __get_user(avail_idx, &vq->avail->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001373 if (r) {
1374 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1375 &vq->avail->idx, r);
1376 return false;
1377 }
1378
David Stevens8dd014a2010-07-27 18:52:21 +03001379 return avail_idx != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001380}
1381
1382/* We don't need to be notified again. */
1383void vhost_disable_notify(struct vhost_virtqueue *vq)
1384{
1385 int r;
1386 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1387 return;
1388 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1389 r = put_user(vq->used_flags, &vq->used->flags);
1390 if (r)
1391 vq_err(vq, "Failed to enable notification at %p: %d\n",
1392 &vq->used->flags, r);
1393}