blob: c17c881e235ac12c9d6c042dafbd1fc32bce3149 [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
33#include <net/sock.h>
34
35#include "vhost.h"
36
37enum {
38 VHOST_MEMORY_MAX_NREGIONS = 64,
39 VHOST_MEMORY_F_LOG = 0x1,
40};
41
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000042static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
43 poll_table *pt)
44{
45 struct vhost_poll *poll;
46 poll = container_of(pt, struct vhost_poll, table);
47
48 poll->wqh = wqh;
49 add_wait_queue(wqh, &poll->wait);
50}
51
52static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
53 void *key)
54{
Tejun Heoc23f34452010-06-02 20:40:00 +020055 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
56
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000057 if (!((unsigned long)key & poll->mask))
58 return 0;
59
Tejun Heoc23f34452010-06-02 20:40:00 +020060 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000061 return 0;
62}
63
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030064static void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000065{
Tejun Heoc23f34452010-06-02 20:40:00 +020066 INIT_LIST_HEAD(&work->node);
67 work->fn = fn;
68 init_waitqueue_head(&work->done);
69 work->flushing = 0;
70 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000071}
72
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +030073/* Init poll structure */
74void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
75 unsigned long mask, struct vhost_dev *dev)
76{
77 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
78 init_poll_funcptr(&poll->table, vhost_poll_func);
79 poll->mask = mask;
80 poll->dev = dev;
81
82 vhost_work_init(&poll->work, fn);
83}
84
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000085/* Start polling a file. We add ourselves to file's wait queue. The caller must
86 * keep a reference to a file until after vhost_poll_stop is called. */
87void vhost_poll_start(struct vhost_poll *poll, struct file *file)
88{
89 unsigned long mask;
90 mask = file->f_op->poll(file, &poll->table);
91 if (mask)
92 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
93}
94
95/* Stop polling a file. After this function returns, it becomes safe to drop the
96 * file reference. You must also flush afterwards. */
97void vhost_poll_stop(struct vhost_poll *poll)
98{
99 remove_wait_queue(poll->wqh, &poll->wait);
100}
101
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300102static void vhost_work_flush(struct vhost_dev *dev, struct vhost_work *work)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000103{
Tejun Heoc23f34452010-06-02 20:40:00 +0200104 unsigned seq;
105 int left;
106 int flushing;
107
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300108 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200109 seq = work->queue_seq;
110 work->flushing++;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300111 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200112 wait_event(work->done, ({
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300113 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200114 left = seq - work->done_seq <= 0;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300115 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200116 left;
117 }));
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300118 spin_lock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200119 flushing = --work->flushing;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300120 spin_unlock_irq(&dev->work_lock);
Tejun Heoc23f34452010-06-02 20:40:00 +0200121 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000122}
123
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300124/* Flush any work that has been scheduled. When calling this, don't hold any
125 * locks that are also used by the callback. */
126void vhost_poll_flush(struct vhost_poll *poll)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000127{
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300128 vhost_work_flush(poll->dev, &poll->work);
129}
130
131static inline void vhost_work_queue(struct vhost_dev *dev,
132 struct vhost_work *work)
133{
Tejun Heoc23f34452010-06-02 20:40:00 +0200134 unsigned long flags;
135
136 spin_lock_irqsave(&dev->work_lock, flags);
137 if (list_empty(&work->node)) {
138 list_add_tail(&work->node, &dev->work_list);
139 work->queue_seq++;
140 wake_up_process(dev->worker);
141 }
142 spin_unlock_irqrestore(&dev->work_lock, flags);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000143}
144
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300145void vhost_poll_queue(struct vhost_poll *poll)
146{
147 vhost_work_queue(poll->dev, &poll->work);
148}
149
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000150static void vhost_vq_reset(struct vhost_dev *dev,
151 struct vhost_virtqueue *vq)
152{
153 vq->num = 1;
154 vq->desc = NULL;
155 vq->avail = NULL;
156 vq->used = NULL;
157 vq->last_avail_idx = 0;
158 vq->avail_idx = 0;
159 vq->last_used_idx = 0;
160 vq->used_flags = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000161 vq->log_used = false;
162 vq->log_addr = -1ull;
David Stevens8dd014a2010-07-27 18:52:21 +0300163 vq->vhost_hlen = 0;
164 vq->sock_hlen = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000165 vq->private_data = NULL;
166 vq->log_base = NULL;
167 vq->error_ctx = NULL;
168 vq->error = NULL;
169 vq->kick = NULL;
170 vq->call_ctx = NULL;
171 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200172 vq->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000173}
174
Tejun Heoc23f34452010-06-02 20:40:00 +0200175static int vhost_worker(void *data)
176{
177 struct vhost_dev *dev = data;
178 struct vhost_work *work = NULL;
179 unsigned uninitialized_var(seq);
180
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200181 use_mm(dev->mm);
182
Tejun Heoc23f34452010-06-02 20:40:00 +0200183 for (;;) {
184 /* mb paired w/ kthread_stop */
185 set_current_state(TASK_INTERRUPTIBLE);
186
187 spin_lock_irq(&dev->work_lock);
188 if (work) {
189 work->done_seq = seq;
190 if (work->flushing)
191 wake_up_all(&work->done);
192 }
193
194 if (kthread_should_stop()) {
195 spin_unlock_irq(&dev->work_lock);
196 __set_current_state(TASK_RUNNING);
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200197 break;
Tejun Heoc23f34452010-06-02 20:40:00 +0200198 }
199 if (!list_empty(&dev->work_list)) {
200 work = list_first_entry(&dev->work_list,
201 struct vhost_work, node);
202 list_del_init(&work->node);
203 seq = work->queue_seq;
204 } else
205 work = NULL;
206 spin_unlock_irq(&dev->work_lock);
207
208 if (work) {
209 __set_current_state(TASK_RUNNING);
210 work->fn(work);
211 } else
212 schedule();
213
214 }
Michael S. Tsirkin64e1c802010-10-06 15:34:45 +0200215 unuse_mm(dev->mm);
216 return 0;
Tejun Heoc23f34452010-06-02 20:40:00 +0200217}
218
Jason Wange0e9b402010-09-14 23:53:05 +0800219/* Helper to allocate iovec buffers for all vqs. */
220static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
221{
222 int i;
223 for (i = 0; i < dev->nvqs; ++i) {
224 dev->vqs[i].indirect = kmalloc(sizeof *dev->vqs[i].indirect *
225 UIO_MAXIOV, GFP_KERNEL);
226 dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
227 GFP_KERNEL);
228 dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
229 UIO_MAXIOV, GFP_KERNEL);
230
231 if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
232 !dev->vqs[i].heads)
233 goto err_nomem;
234 }
235 return 0;
236err_nomem:
237 for (; i >= 0; --i) {
238 kfree(dev->vqs[i].indirect);
239 kfree(dev->vqs[i].log);
240 kfree(dev->vqs[i].heads);
241 }
242 return -ENOMEM;
243}
244
245static void vhost_dev_free_iovecs(struct vhost_dev *dev)
246{
247 int i;
248 for (i = 0; i < dev->nvqs; ++i) {
249 kfree(dev->vqs[i].indirect);
250 dev->vqs[i].indirect = NULL;
251 kfree(dev->vqs[i].log);
252 dev->vqs[i].log = NULL;
253 kfree(dev->vqs[i].heads);
254 dev->vqs[i].heads = NULL;
255 }
256}
257
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000258long vhost_dev_init(struct vhost_dev *dev,
259 struct vhost_virtqueue *vqs, int nvqs)
260{
261 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200262
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000263 dev->vqs = vqs;
264 dev->nvqs = nvqs;
265 mutex_init(&dev->mutex);
266 dev->log_ctx = NULL;
267 dev->log_file = NULL;
268 dev->memory = NULL;
269 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200270 spin_lock_init(&dev->work_lock);
271 INIT_LIST_HEAD(&dev->work_list);
272 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000273
274 for (i = 0; i < dev->nvqs; ++i) {
Jason Wange0e9b402010-09-14 23:53:05 +0800275 dev->vqs[i].log = NULL;
276 dev->vqs[i].indirect = NULL;
277 dev->vqs[i].heads = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000278 dev->vqs[i].dev = dev;
279 mutex_init(&dev->vqs[i].mutex);
280 vhost_vq_reset(dev, dev->vqs + i);
281 if (dev->vqs[i].handle_kick)
282 vhost_poll_init(&dev->vqs[i].poll,
Tejun Heoc23f34452010-06-02 20:40:00 +0200283 dev->vqs[i].handle_kick, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000284 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200285
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000286 return 0;
287}
288
289/* Caller should have device mutex */
290long vhost_dev_check_owner(struct vhost_dev *dev)
291{
292 /* Are you the owner? If not, I don't think you mean to do that */
293 return dev->mm == current->mm ? 0 : -EPERM;
294}
295
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300296struct vhost_attach_cgroups_struct {
297 struct vhost_work work;
298 struct task_struct *owner;
299 int ret;
300};
301
302static void vhost_attach_cgroups_work(struct vhost_work *work)
303{
304 struct vhost_attach_cgroups_struct *s;
305 s = container_of(work, struct vhost_attach_cgroups_struct, work);
306 s->ret = cgroup_attach_task_all(s->owner, current);
307}
308
309static int vhost_attach_cgroups(struct vhost_dev *dev)
310{
311 struct vhost_attach_cgroups_struct attach;
312 attach.owner = current;
313 vhost_work_init(&attach.work, vhost_attach_cgroups_work);
314 vhost_work_queue(dev, &attach.work);
315 vhost_work_flush(dev, &attach.work);
316 return attach.ret;
317}
318
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000319/* Caller should have device mutex */
320static long vhost_dev_set_owner(struct vhost_dev *dev)
321{
Tejun Heoc23f34452010-06-02 20:40:00 +0200322 struct task_struct *worker;
323 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000324 /* Is there an owner already? */
Tejun Heoc23f34452010-06-02 20:40:00 +0200325 if (dev->mm) {
326 err = -EBUSY;
327 goto err_mm;
328 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000329 /* No owner, become one */
330 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200331 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
332 if (IS_ERR(worker)) {
333 err = PTR_ERR(worker);
334 goto err_worker;
335 }
336
337 dev->worker = worker;
Michael S. Tsirkin87d6a412010-09-02 14:05:30 +0300338 wake_up_process(worker); /* avoid contributing to loadavg */
339
340 err = vhost_attach_cgroups(dev);
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300341 if (err)
342 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200343
Jason Wange0e9b402010-09-14 23:53:05 +0800344 err = vhost_dev_alloc_iovecs(dev);
345 if (err)
346 goto err_cgroup;
347
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000348 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300349err_cgroup:
350 kthread_stop(worker);
Michael S. Tsirkin615cc222010-09-02 14:16:36 +0300351 dev->worker = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200352err_worker:
353 if (dev->mm)
354 mmput(dev->mm);
355 dev->mm = NULL;
356err_mm:
357 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000358}
359
360/* Caller should have device mutex */
361long vhost_dev_reset_owner(struct vhost_dev *dev)
362{
363 struct vhost_memory *memory;
364
365 /* Restore memory to default empty mapping. */
366 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
367 if (!memory)
368 return -ENOMEM;
369
370 vhost_dev_cleanup(dev);
371
372 memory->nregions = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100373 RCU_INIT_POINTER(dev->memory, memory);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000374 return 0;
375}
376
377/* Caller should have device mutex */
378void vhost_dev_cleanup(struct vhost_dev *dev)
379{
380 int i;
381 for (i = 0; i < dev->nvqs; ++i) {
382 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
383 vhost_poll_stop(&dev->vqs[i].poll);
384 vhost_poll_flush(&dev->vqs[i].poll);
385 }
386 if (dev->vqs[i].error_ctx)
387 eventfd_ctx_put(dev->vqs[i].error_ctx);
388 if (dev->vqs[i].error)
389 fput(dev->vqs[i].error);
390 if (dev->vqs[i].kick)
391 fput(dev->vqs[i].kick);
392 if (dev->vqs[i].call_ctx)
393 eventfd_ctx_put(dev->vqs[i].call_ctx);
394 if (dev->vqs[i].call)
395 fput(dev->vqs[i].call);
396 vhost_vq_reset(dev, dev->vqs + i);
397 }
Jason Wange0e9b402010-09-14 23:53:05 +0800398 vhost_dev_free_iovecs(dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000399 if (dev->log_ctx)
400 eventfd_ctx_put(dev->log_ctx);
401 dev->log_ctx = NULL;
402 if (dev->log_file)
403 fput(dev->log_file);
404 dev->log_file = NULL;
405 /* No one will access memory at this point */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100406 kfree(rcu_dereference_protected(dev->memory,
407 lockdep_is_held(&dev->mutex)));
408 RCU_INIT_POINTER(dev->memory, NULL);
Tejun Heoc23f34452010-06-02 20:40:00 +0200409 WARN_ON(!list_empty(&dev->work_list));
Eric Dumazet78b620c2010-08-31 02:05:57 +0000410 if (dev->worker) {
411 kthread_stop(dev->worker);
412 dev->worker = NULL;
413 }
Michael S. Tsirkin533a19b2010-10-06 15:34:38 +0200414 if (dev->mm)
415 mmput(dev->mm);
416 dev->mm = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000417}
418
419static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
420{
421 u64 a = addr / VHOST_PAGE_SIZE / 8;
422 /* Make sure 64 bit math will not overflow. */
423 if (a > ULONG_MAX - (unsigned long)log_base ||
424 a + (unsigned long)log_base > ULONG_MAX)
Dan Carpenter6d97e552010-10-11 19:24:19 +0200425 return 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000426
427 return access_ok(VERIFY_WRITE, log_base + a,
428 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
429}
430
431/* Caller should have vq mutex and device mutex. */
432static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
433 int log_all)
434{
435 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400436
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300437 if (!mem)
438 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400439
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000440 for (i = 0; i < mem->nregions; ++i) {
441 struct vhost_memory_region *m = mem->regions + i;
442 unsigned long a = m->userspace_addr;
443 if (m->memory_size > ULONG_MAX)
444 return 0;
445 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
446 m->memory_size))
447 return 0;
448 else if (log_all && !log_access_ok(log_base,
449 m->guest_phys_addr,
450 m->memory_size))
451 return 0;
452 }
453 return 1;
454}
455
456/* Can we switch to this memory table? */
457/* Caller should have device mutex but not vq mutex */
458static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
459 int log_all)
460{
461 int i;
462 for (i = 0; i < d->nvqs; ++i) {
463 int ok;
464 mutex_lock(&d->vqs[i].mutex);
465 /* If ring is inactive, will check when it's enabled. */
466 if (d->vqs[i].private_data)
467 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
468 log_all);
469 else
470 ok = 1;
471 mutex_unlock(&d->vqs[i].mutex);
472 if (!ok)
473 return 0;
474 }
475 return 1;
476}
477
478static int vq_access_ok(unsigned int num,
479 struct vring_desc __user *desc,
480 struct vring_avail __user *avail,
481 struct vring_used __user *used)
482{
483 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
484 access_ok(VERIFY_READ, avail,
485 sizeof *avail + num * sizeof *avail->ring) &&
486 access_ok(VERIFY_WRITE, used,
487 sizeof *used + num * sizeof *used->ring);
488}
489
490/* Can we log writes? */
491/* Caller should have device mutex but not vq mutex */
492int vhost_log_access_ok(struct vhost_dev *dev)
493{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100494 struct vhost_memory *mp;
495
496 mp = rcu_dereference_protected(dev->memory,
497 lockdep_is_held(&dev->mutex));
498 return memory_access_ok(dev, mp, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000499}
500
501/* Verify access for write logging. */
502/* Caller should have vq mutex and device mutex */
503static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
504{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100505 struct vhost_memory *mp;
506
507 mp = rcu_dereference_protected(vq->dev->memory,
508 lockdep_is_held(&vq->mutex));
509 return vq_memory_access_ok(log_base, mp,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000510 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
511 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
512 sizeof *vq->used +
513 vq->num * sizeof *vq->used->ring));
514}
515
516/* Can we start vq? */
517/* Caller should have vq mutex and device mutex */
518int vhost_vq_access_ok(struct vhost_virtqueue *vq)
519{
520 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
521 vq_log_access_ok(vq, vq->log_base);
522}
523
524static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
525{
526 struct vhost_memory mem, *newmem, *oldmem;
527 unsigned long size = offsetof(struct vhost_memory, regions);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900528 if (copy_from_user(&mem, m, size))
529 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000530 if (mem.padding)
531 return -EOPNOTSUPP;
532 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
533 return -E2BIG;
534 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
535 if (!newmem)
536 return -ENOMEM;
537
538 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900539 if (copy_from_user(newmem->regions, m->regions,
540 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000541 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900542 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000543 }
544
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900545 if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
546 kfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000547 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900548 }
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100549 oldmem = rcu_dereference_protected(d->memory,
550 lockdep_is_held(&d->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000551 rcu_assign_pointer(d->memory, newmem);
552 synchronize_rcu();
553 kfree(oldmem);
554 return 0;
555}
556
557static int init_used(struct vhost_virtqueue *vq,
558 struct vring_used __user *used)
559{
560 int r = put_user(vq->used_flags, &used->flags);
561 if (r)
562 return r;
563 return get_user(vq->last_used_idx, &used->idx);
564}
565
566static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
567{
568 struct file *eventfp, *filep = NULL,
569 *pollstart = NULL, *pollstop = NULL;
570 struct eventfd_ctx *ctx = NULL;
571 u32 __user *idxp = argp;
572 struct vhost_virtqueue *vq;
573 struct vhost_vring_state s;
574 struct vhost_vring_file f;
575 struct vhost_vring_addr a;
576 u32 idx;
577 long r;
578
579 r = get_user(idx, idxp);
580 if (r < 0)
581 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530582 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000583 return -ENOBUFS;
584
585 vq = d->vqs + idx;
586
587 mutex_lock(&vq->mutex);
588
589 switch (ioctl) {
590 case VHOST_SET_VRING_NUM:
591 /* Resizing ring with an active backend?
592 * You don't want to do that. */
593 if (vq->private_data) {
594 r = -EBUSY;
595 break;
596 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900597 if (copy_from_user(&s, argp, sizeof s)) {
598 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000599 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900600 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000601 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
602 r = -EINVAL;
603 break;
604 }
605 vq->num = s.num;
606 break;
607 case VHOST_SET_VRING_BASE:
608 /* Moving base with an active backend?
609 * You don't want to do that. */
610 if (vq->private_data) {
611 r = -EBUSY;
612 break;
613 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900614 if (copy_from_user(&s, argp, sizeof s)) {
615 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000616 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900617 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000618 if (s.num > 0xffff) {
619 r = -EINVAL;
620 break;
621 }
622 vq->last_avail_idx = s.num;
623 /* Forget the cached index value. */
624 vq->avail_idx = vq->last_avail_idx;
625 break;
626 case VHOST_GET_VRING_BASE:
627 s.index = idx;
628 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900629 if (copy_to_user(argp, &s, sizeof s))
630 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000631 break;
632 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900633 if (copy_from_user(&a, argp, sizeof a)) {
634 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000635 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900636 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000637 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
638 r = -EOPNOTSUPP;
639 break;
640 }
641 /* For 32bit, verify that the top 32bits of the user
642 data are set to zero. */
643 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
644 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
645 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
646 r = -EFAULT;
647 break;
648 }
649 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
650 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
651 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
652 r = -EINVAL;
653 break;
654 }
655
656 /* We only verify access here if backend is configured.
657 * If it is not, we don't as size might not have been setup.
658 * We will verify when backend is configured. */
659 if (vq->private_data) {
660 if (!vq_access_ok(vq->num,
661 (void __user *)(unsigned long)a.desc_user_addr,
662 (void __user *)(unsigned long)a.avail_user_addr,
663 (void __user *)(unsigned long)a.used_user_addr)) {
664 r = -EINVAL;
665 break;
666 }
667
668 /* Also validate log access for used ring if enabled. */
669 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
670 !log_access_ok(vq->log_base, a.log_guest_addr,
671 sizeof *vq->used +
672 vq->num * sizeof *vq->used->ring)) {
673 r = -EINVAL;
674 break;
675 }
676 }
677
678 r = init_used(vq, (struct vring_used __user *)(unsigned long)
679 a.used_user_addr);
680 if (r)
681 break;
682 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
683 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
684 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
685 vq->log_addr = a.log_guest_addr;
686 vq->used = (void __user *)(unsigned long)a.used_user_addr;
687 break;
688 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900689 if (copy_from_user(&f, argp, sizeof f)) {
690 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000691 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900692 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000693 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200694 if (IS_ERR(eventfp)) {
695 r = PTR_ERR(eventfp);
696 break;
697 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000698 if (eventfp != vq->kick) {
699 pollstop = filep = vq->kick;
700 pollstart = vq->kick = eventfp;
701 } else
702 filep = eventfp;
703 break;
704 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900705 if (copy_from_user(&f, argp, sizeof f)) {
706 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000707 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900708 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000709 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200710 if (IS_ERR(eventfp)) {
711 r = PTR_ERR(eventfp);
712 break;
713 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000714 if (eventfp != vq->call) {
715 filep = vq->call;
716 ctx = vq->call_ctx;
717 vq->call = eventfp;
718 vq->call_ctx = eventfp ?
719 eventfd_ctx_fileget(eventfp) : NULL;
720 } else
721 filep = eventfp;
722 break;
723 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900724 if (copy_from_user(&f, argp, sizeof f)) {
725 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000726 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900727 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000728 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200729 if (IS_ERR(eventfp)) {
730 r = PTR_ERR(eventfp);
731 break;
732 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000733 if (eventfp != vq->error) {
734 filep = vq->error;
735 vq->error = eventfp;
736 ctx = vq->error_ctx;
737 vq->error_ctx = eventfp ?
738 eventfd_ctx_fileget(eventfp) : NULL;
739 } else
740 filep = eventfp;
741 break;
742 default:
743 r = -ENOIOCTLCMD;
744 }
745
746 if (pollstop && vq->handle_kick)
747 vhost_poll_stop(&vq->poll);
748
749 if (ctx)
750 eventfd_ctx_put(ctx);
751 if (filep)
752 fput(filep);
753
754 if (pollstart && vq->handle_kick)
755 vhost_poll_start(&vq->poll, vq->kick);
756
757 mutex_unlock(&vq->mutex);
758
759 if (pollstop && vq->handle_kick)
760 vhost_poll_flush(&vq->poll);
761 return r;
762}
763
764/* Caller must have device mutex */
765long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
766{
767 void __user *argp = (void __user *)arg;
768 struct file *eventfp, *filep = NULL;
769 struct eventfd_ctx *ctx = NULL;
770 u64 p;
771 long r;
772 int i, fd;
773
774 /* If you are not the owner, you can become one */
775 if (ioctl == VHOST_SET_OWNER) {
776 r = vhost_dev_set_owner(d);
777 goto done;
778 }
779
780 /* You must be the owner to do anything else */
781 r = vhost_dev_check_owner(d);
782 if (r)
783 goto done;
784
785 switch (ioctl) {
786 case VHOST_SET_MEM_TABLE:
787 r = vhost_set_memory(d, argp);
788 break;
789 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900790 if (copy_from_user(&p, argp, sizeof p)) {
791 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000792 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900793 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000794 if ((u64)(unsigned long)p != p) {
795 r = -EFAULT;
796 break;
797 }
798 for (i = 0; i < d->nvqs; ++i) {
799 struct vhost_virtqueue *vq;
800 void __user *base = (void __user *)(unsigned long)p;
801 vq = d->vqs + i;
802 mutex_lock(&vq->mutex);
803 /* If ring is inactive, will check when it's enabled. */
804 if (vq->private_data && !vq_log_access_ok(vq, base))
805 r = -EFAULT;
806 else
807 vq->log_base = base;
808 mutex_unlock(&vq->mutex);
809 }
810 break;
811 case VHOST_SET_LOG_FD:
812 r = get_user(fd, (int __user *)argp);
813 if (r < 0)
814 break;
815 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
816 if (IS_ERR(eventfp)) {
817 r = PTR_ERR(eventfp);
818 break;
819 }
820 if (eventfp != d->log_file) {
821 filep = d->log_file;
822 ctx = d->log_ctx;
823 d->log_ctx = eventfp ?
824 eventfd_ctx_fileget(eventfp) : NULL;
825 } else
826 filep = eventfp;
827 for (i = 0; i < d->nvqs; ++i) {
828 mutex_lock(&d->vqs[i].mutex);
829 d->vqs[i].log_ctx = d->log_ctx;
830 mutex_unlock(&d->vqs[i].mutex);
831 }
832 if (ctx)
833 eventfd_ctx_put(ctx);
834 if (filep)
835 fput(filep);
836 break;
837 default:
838 r = vhost_set_vring(d, ioctl, argp);
839 break;
840 }
841done:
842 return r;
843}
844
845static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
846 __u64 addr, __u32 len)
847{
848 struct vhost_memory_region *reg;
849 int i;
850 /* linear search is not brilliant, but we really have on the order of 6
851 * regions in practice */
852 for (i = 0; i < mem->nregions; ++i) {
853 reg = mem->regions + i;
854 if (reg->guest_phys_addr <= addr &&
855 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
856 return reg;
857 }
858 return NULL;
859}
860
861/* TODO: This is really inefficient. We need something like get_user()
862 * (instruction directly accesses the data, with an exception table entry
863 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
864 */
865static int set_bit_to_user(int nr, void __user *addr)
866{
867 unsigned long log = (unsigned long)addr;
868 struct page *page;
869 void *base;
870 int bit = nr + (log % PAGE_SIZE) * 8;
871 int r;
872 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200873 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000874 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200875 BUG_ON(r != 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000876 base = kmap_atomic(page, KM_USER0);
877 set_bit(bit, base);
878 kunmap_atomic(base, KM_USER0);
879 set_page_dirty_lock(page);
880 put_page(page);
881 return 0;
882}
883
884static int log_write(void __user *log_base,
885 u64 write_address, u64 write_length)
886{
887 int r;
888 if (!write_length)
889 return 0;
890 write_address /= VHOST_PAGE_SIZE;
891 for (;;) {
892 u64 base = (u64)(unsigned long)log_base;
893 u64 log = base + write_address / 8;
894 int bit = write_address % 8;
895 if ((u64)(unsigned long)log != log)
896 return -EFAULT;
897 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
898 if (r < 0)
899 return r;
900 if (write_length <= VHOST_PAGE_SIZE)
901 break;
902 write_length -= VHOST_PAGE_SIZE;
903 write_address += VHOST_PAGE_SIZE;
904 }
905 return r;
906}
907
908int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
909 unsigned int log_num, u64 len)
910{
911 int i, r;
912
913 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000914 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000915 for (i = 0; i < log_num; ++i) {
916 u64 l = min(log[i].len, len);
917 r = log_write(vq->log_base, log[i].addr, l);
918 if (r < 0)
919 return r;
920 len -= l;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200921 if (!len) {
922 if (vq->log_ctx)
923 eventfd_signal(vq->log_ctx, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000924 return 0;
Michael S. Tsirkin5786aee2010-09-22 12:31:53 +0200925 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000926 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000927 /* Length written exceeds what we have stored. This is a bug. */
928 BUG();
929 return 0;
930}
931
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400932static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
933 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000934{
935 const struct vhost_memory_region *reg;
936 struct vhost_memory *mem;
937 struct iovec *_iov;
938 u64 s = 0;
939 int ret = 0;
940
941 rcu_read_lock();
942
943 mem = rcu_dereference(dev->memory);
944 while ((u64)len > s) {
945 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300946 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000947 ret = -ENOBUFS;
948 break;
949 }
950 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300951 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000952 ret = -EFAULT;
953 break;
954 }
955 _iov = iov + ret;
956 size = reg->memory_size - addr + reg->guest_phys_addr;
957 _iov->iov_len = min((u64)len, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400958 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000959 (reg->userspace_addr + addr - reg->guest_phys_addr);
960 s += size;
961 addr += size;
962 ++ret;
963 }
964
965 rcu_read_unlock();
966 return ret;
967}
968
969/* Each buffer in the virtqueues is actually a chain of descriptors. This
970 * function returns the next descriptor in the chain,
971 * or -1U if we're at the end. */
972static unsigned next_desc(struct vring_desc *desc)
973{
974 unsigned int next;
975
976 /* If this descriptor says it doesn't chain, we're done. */
977 if (!(desc->flags & VRING_DESC_F_NEXT))
978 return -1U;
979
980 /* Check they're not leading us off end of descriptors. */
981 next = desc->next;
982 /* Make sure compiler knows to grab that: we don't want it changing! */
983 /* We will use the result as an index in an array, so most
984 * architectures only need a compiler barrier here. */
985 read_barrier_depends();
986
987 return next;
988}
989
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300990static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
991 struct iovec iov[], unsigned int iov_size,
992 unsigned int *out_num, unsigned int *in_num,
993 struct vhost_log *log, unsigned int *log_num,
994 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000995{
996 struct vring_desc desc;
997 unsigned int i = 0, count, found = 0;
998 int ret;
999
1000 /* Sanity check */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001001 if (unlikely(indirect->len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001002 vq_err(vq, "Invalid length in indirect descriptor: "
1003 "len 0x%llx not multiple of 0x%zx\n",
1004 (unsigned long long)indirect->len,
1005 sizeof desc);
1006 return -EINVAL;
1007 }
1008
1009 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
Jason Wange0e9b402010-09-14 23:53:05 +08001010 UIO_MAXIOV);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001011 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001012 vq_err(vq, "Translation failure %d in indirect.\n", ret);
1013 return ret;
1014 }
1015
1016 /* We will use the result as an address to read from, so most
1017 * architectures only need a compiler barrier here. */
1018 read_barrier_depends();
1019
1020 count = indirect->len / sizeof desc;
1021 /* Buffers are chained via a 16 bit next field, so
1022 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001023 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001024 vq_err(vq, "Indirect buffer length too big: %d\n",
1025 indirect->len);
1026 return -E2BIG;
1027 }
1028
1029 do {
1030 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001031 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001032 vq_err(vq, "Loop detected: last one at %u "
1033 "indirect size %u\n",
1034 i, count);
1035 return -EINVAL;
1036 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001037 if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
1038 sizeof desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001039 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
1040 i, (size_t)indirect->addr + i * sizeof desc);
1041 return -EINVAL;
1042 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001043 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001044 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
1045 i, (size_t)indirect->addr + i * sizeof desc);
1046 return -EINVAL;
1047 }
1048
1049 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1050 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001051 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001052 vq_err(vq, "Translation failure %d indirect idx %d\n",
1053 ret, i);
1054 return ret;
1055 }
1056 /* If this is an input descriptor, increment that count. */
1057 if (desc.flags & VRING_DESC_F_WRITE) {
1058 *in_num += ret;
1059 if (unlikely(log)) {
1060 log[*log_num].addr = desc.addr;
1061 log[*log_num].len = desc.len;
1062 ++*log_num;
1063 }
1064 } else {
1065 /* If it's an output descriptor, they're all supposed
1066 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001067 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001068 vq_err(vq, "Indirect descriptor "
1069 "has out after in: idx %d\n", i);
1070 return -EINVAL;
1071 }
1072 *out_num += ret;
1073 }
1074 } while ((i = next_desc(&desc)) != -1);
1075 return 0;
1076}
1077
1078/* This looks in the virtqueue and for the first available buffer, and converts
1079 * it to an iovec for convenient access. Since descriptors consist of some
1080 * number of output then some number of input descriptors, it's actually two
1081 * iovecs, but we pack them into one and note how many of each there were.
1082 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001083 * This function returns the descriptor number found, or vq->num (which is
1084 * never a valid descriptor number) if none was found. A negative code is
1085 * returned on error. */
1086int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
1087 struct iovec iov[], unsigned int iov_size,
1088 unsigned int *out_num, unsigned int *in_num,
1089 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001090{
1091 struct vring_desc desc;
1092 unsigned int i, head, found = 0;
1093 u16 last_avail_idx;
1094 int ret;
1095
1096 /* Check it isn't doing very strange things with descriptor numbers. */
1097 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001098 if (unlikely(get_user(vq->avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001099 vq_err(vq, "Failed to access avail idx at %p\n",
1100 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001101 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001102 }
1103
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001104 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001105 vq_err(vq, "Guest moved used index from %u to %u",
1106 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001107 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001108 }
1109
1110 /* If there's nothing new since last we looked, return invalid. */
1111 if (vq->avail_idx == last_avail_idx)
1112 return vq->num;
1113
1114 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001115 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001116
1117 /* Grab the next descriptor number they're advertising, and increment
1118 * the index we've seen. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001119 if (unlikely(get_user(head,
1120 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001121 vq_err(vq, "Failed to read head: idx %d address %p\n",
1122 last_avail_idx,
1123 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001124 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001125 }
1126
1127 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001128 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001129 vq_err(vq, "Guest says index %u > %u is available",
1130 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001131 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001132 }
1133
1134 /* When we start there are none of either input nor output. */
1135 *out_num = *in_num = 0;
1136 if (unlikely(log))
1137 *log_num = 0;
1138
1139 i = head;
1140 do {
1141 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001142 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001143 vq_err(vq, "Desc index is %u > %u, head = %u",
1144 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001145 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001146 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001147 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001148 vq_err(vq, "Loop detected: last one at %u "
1149 "vq size %u head %u\n",
1150 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001151 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001152 }
1153 ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001154 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001155 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1156 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001157 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001158 }
1159 if (desc.flags & VRING_DESC_F_INDIRECT) {
1160 ret = get_indirect(dev, vq, iov, iov_size,
1161 out_num, in_num,
1162 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001163 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001164 vq_err(vq, "Failure detected "
1165 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001166 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001167 }
1168 continue;
1169 }
1170
1171 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1172 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001173 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001174 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1175 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001176 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001177 }
1178 if (desc.flags & VRING_DESC_F_WRITE) {
1179 /* If this is an input descriptor,
1180 * increment that count. */
1181 *in_num += ret;
1182 if (unlikely(log)) {
1183 log[*log_num].addr = desc.addr;
1184 log[*log_num].len = desc.len;
1185 ++*log_num;
1186 }
1187 } else {
1188 /* If it's an output descriptor, they're all supposed
1189 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001190 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001191 vq_err(vq, "Descriptor has out after in: "
1192 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001193 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001194 }
1195 *out_num += ret;
1196 }
1197 } while ((i = next_desc(&desc)) != -1);
1198
1199 /* On success, increment avail index. */
1200 vq->last_avail_idx++;
1201 return head;
1202}
1203
1204/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001205void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001206{
David Stevens8dd014a2010-07-27 18:52:21 +03001207 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001208}
1209
1210/* After we've used one of their buffers, we tell them about it. We'll then
1211 * want to notify the guest, using eventfd. */
1212int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1213{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001214 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001215
1216 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1217 * next entry in that used ring. */
1218 used = &vq->used->ring[vq->last_used_idx % vq->num];
1219 if (put_user(head, &used->id)) {
1220 vq_err(vq, "Failed to write used id");
1221 return -EFAULT;
1222 }
1223 if (put_user(len, &used->len)) {
1224 vq_err(vq, "Failed to write used len");
1225 return -EFAULT;
1226 }
1227 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001228 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001229 if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
1230 vq_err(vq, "Failed to increment used idx");
1231 return -EFAULT;
1232 }
1233 if (unlikely(vq->log_used)) {
1234 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001235 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001236 /* Log used ring entry write. */
1237 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001238 vq->log_addr +
1239 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001240 sizeof *used);
1241 /* Log used index update. */
1242 log_write(vq->log_base,
1243 vq->log_addr + offsetof(struct vring_used, idx),
1244 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001245 if (vq->log_ctx)
1246 eventfd_signal(vq->log_ctx, 1);
1247 }
1248 vq->last_used_idx++;
1249 return 0;
1250}
1251
David Stevens8dd014a2010-07-27 18:52:21 +03001252static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1253 struct vring_used_elem *heads,
1254 unsigned count)
1255{
1256 struct vring_used_elem __user *used;
1257 int start;
1258
1259 start = vq->last_used_idx % vq->num;
1260 used = vq->used->ring + start;
1261 if (copy_to_user(used, heads, count * sizeof *used)) {
1262 vq_err(vq, "Failed to write used");
1263 return -EFAULT;
1264 }
1265 if (unlikely(vq->log_used)) {
1266 /* Make sure data is seen before log. */
1267 smp_wmb();
1268 /* Log used ring entry write. */
1269 log_write(vq->log_base,
1270 vq->log_addr +
1271 ((void __user *)used - (void __user *)vq->used),
1272 count * sizeof *used);
1273 }
1274 vq->last_used_idx += count;
1275 return 0;
1276}
1277
1278/* After we've used one of their buffers, we tell them about it. We'll then
1279 * want to notify the guest, using eventfd. */
1280int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1281 unsigned count)
1282{
1283 int start, n, r;
1284
1285 start = vq->last_used_idx % vq->num;
1286 n = vq->num - start;
1287 if (n < count) {
1288 r = __vhost_add_used_n(vq, heads, n);
1289 if (r < 0)
1290 return r;
1291 heads += n;
1292 count -= n;
1293 }
1294 r = __vhost_add_used_n(vq, heads, count);
1295
1296 /* Make sure buffer is written before we update index. */
1297 smp_wmb();
1298 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1299 vq_err(vq, "Failed to increment used idx");
1300 return -EFAULT;
1301 }
1302 if (unlikely(vq->log_used)) {
1303 /* Log used index update. */
1304 log_write(vq->log_base,
1305 vq->log_addr + offsetof(struct vring_used, idx),
1306 sizeof vq->used->idx);
1307 if (vq->log_ctx)
1308 eventfd_signal(vq->log_ctx, 1);
1309 }
1310 return r;
1311}
1312
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001313/* This actually signals the guest, using eventfd. */
1314void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1315{
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001316 __u16 flags;
1317 /* Flush out used index updates. This is paired
1318 * with the barrier that the Guest executes when enabling
1319 * interrupts. */
1320 smp_mb();
1321
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001322 if (get_user(flags, &vq->avail->flags)) {
1323 vq_err(vq, "Failed to get flags");
1324 return;
1325 }
1326
1327 /* If they don't want an interrupt, don't signal, unless empty. */
1328 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1329 (vq->avail_idx != vq->last_avail_idx ||
1330 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1331 return;
1332
1333 /* Signal the Guest tell them we used something up. */
1334 if (vq->call_ctx)
1335 eventfd_signal(vq->call_ctx, 1);
1336}
1337
1338/* And here's the combo meal deal. Supersize me! */
1339void vhost_add_used_and_signal(struct vhost_dev *dev,
1340 struct vhost_virtqueue *vq,
1341 unsigned int head, int len)
1342{
1343 vhost_add_used(vq, head, len);
1344 vhost_signal(dev, vq);
1345}
1346
David Stevens8dd014a2010-07-27 18:52:21 +03001347/* multi-buffer version of vhost_add_used_and_signal */
1348void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1349 struct vhost_virtqueue *vq,
1350 struct vring_used_elem *heads, unsigned count)
1351{
1352 vhost_add_used_n(vq, heads, count);
1353 vhost_signal(dev, vq);
1354}
1355
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001356/* OK, now we need to know about added descriptors. */
1357bool vhost_enable_notify(struct vhost_virtqueue *vq)
1358{
1359 u16 avail_idx;
1360 int r;
1361 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1362 return false;
1363 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1364 r = put_user(vq->used_flags, &vq->used->flags);
1365 if (r) {
1366 vq_err(vq, "Failed to enable notification at %p: %d\n",
1367 &vq->used->flags, r);
1368 return false;
1369 }
1370 /* They could have slipped one in as we were doing that: make
1371 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001372 smp_mb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001373 r = get_user(avail_idx, &vq->avail->idx);
1374 if (r) {
1375 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1376 &vq->avail->idx, r);
1377 return false;
1378 }
1379
David Stevens8dd014a2010-07-27 18:52:21 +03001380 return avail_idx != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001381}
1382
1383/* We don't need to be notified again. */
1384void vhost_disable_notify(struct vhost_virtqueue *vq)
1385{
1386 int r;
1387 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1388 return;
1389 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1390 r = put_user(vq->used_flags, &vq->used->flags);
1391 if (r)
1392 vq_err(vq, "Failed to enable notification at %p: %d\n",
1393 &vq->used->flags, r);
1394}