blob: b5c49478d2032ecc9051f23b23f4dff832bd24d3 [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>
18#include <linux/miscdevice.h>
19#include <linux/mutex.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000020#include <linux/rcupdate.h>
21#include <linux/poll.h>
22#include <linux/file.h>
23#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Tejun Heoc23f34452010-06-02 20:40:00 +020025#include <linux/kthread.h>
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +030026#include <linux/cgroup.h>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000027
28#include <linux/net.h>
29#include <linux/if_packet.h>
30#include <linux/if_arp.h>
31
32#include <net/sock.h>
33
34#include "vhost.h"
35
36enum {
37 VHOST_MEMORY_MAX_NREGIONS = 64,
38 VHOST_MEMORY_F_LOG = 0x1,
39};
40
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000041static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh,
42 poll_table *pt)
43{
44 struct vhost_poll *poll;
45 poll = container_of(pt, struct vhost_poll, table);
46
47 poll->wqh = wqh;
48 add_wait_queue(wqh, &poll->wait);
49}
50
51static int vhost_poll_wakeup(wait_queue_t *wait, unsigned mode, int sync,
52 void *key)
53{
Tejun Heoc23f34452010-06-02 20:40:00 +020054 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait);
55
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000056 if (!((unsigned long)key & poll->mask))
57 return 0;
58
Tejun Heoc23f34452010-06-02 20:40:00 +020059 vhost_poll_queue(poll);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000060 return 0;
61}
62
63/* Init poll structure */
Tejun Heoc23f34452010-06-02 20:40:00 +020064void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn,
65 unsigned long mask, struct vhost_dev *dev)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000066{
Tejun Heoc23f34452010-06-02 20:40:00 +020067 struct vhost_work *work = &poll->work;
68
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000069 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
70 init_poll_funcptr(&poll->table, vhost_poll_func);
71 poll->mask = mask;
Tejun Heoc23f34452010-06-02 20:40:00 +020072 poll->dev = dev;
73
74 INIT_LIST_HEAD(&work->node);
75 work->fn = fn;
76 init_waitqueue_head(&work->done);
77 work->flushing = 0;
78 work->queue_seq = work->done_seq = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000079}
80
81/* Start polling a file. We add ourselves to file's wait queue. The caller must
82 * keep a reference to a file until after vhost_poll_stop is called. */
83void vhost_poll_start(struct vhost_poll *poll, struct file *file)
84{
85 unsigned long mask;
86 mask = file->f_op->poll(file, &poll->table);
87 if (mask)
88 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
89}
90
91/* Stop polling a file. After this function returns, it becomes safe to drop the
92 * file reference. You must also flush afterwards. */
93void vhost_poll_stop(struct vhost_poll *poll)
94{
95 remove_wait_queue(poll->wqh, &poll->wait);
96}
97
98/* Flush any work that has been scheduled. When calling this, don't hold any
99 * locks that are also used by the callback. */
100void vhost_poll_flush(struct vhost_poll *poll)
101{
Tejun Heoc23f34452010-06-02 20:40:00 +0200102 struct vhost_work *work = &poll->work;
103 unsigned seq;
104 int left;
105 int flushing;
106
107 spin_lock_irq(&poll->dev->work_lock);
108 seq = work->queue_seq;
109 work->flushing++;
110 spin_unlock_irq(&poll->dev->work_lock);
111 wait_event(work->done, ({
112 spin_lock_irq(&poll->dev->work_lock);
113 left = seq - work->done_seq <= 0;
114 spin_unlock_irq(&poll->dev->work_lock);
115 left;
116 }));
117 spin_lock_irq(&poll->dev->work_lock);
118 flushing = --work->flushing;
119 spin_unlock_irq(&poll->dev->work_lock);
120 BUG_ON(flushing < 0);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000121}
122
123void vhost_poll_queue(struct vhost_poll *poll)
124{
Tejun Heoc23f34452010-06-02 20:40:00 +0200125 struct vhost_dev *dev = poll->dev;
126 struct vhost_work *work = &poll->work;
127 unsigned long flags;
128
129 spin_lock_irqsave(&dev->work_lock, flags);
130 if (list_empty(&work->node)) {
131 list_add_tail(&work->node, &dev->work_list);
132 work->queue_seq++;
133 wake_up_process(dev->worker);
134 }
135 spin_unlock_irqrestore(&dev->work_lock, flags);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000136}
137
138static void vhost_vq_reset(struct vhost_dev *dev,
139 struct vhost_virtqueue *vq)
140{
141 vq->num = 1;
142 vq->desc = NULL;
143 vq->avail = NULL;
144 vq->used = NULL;
145 vq->last_avail_idx = 0;
146 vq->avail_idx = 0;
147 vq->last_used_idx = 0;
148 vq->used_flags = 0;
149 vq->used_flags = 0;
150 vq->log_used = false;
151 vq->log_addr = -1ull;
David Stevens8dd014a2010-07-27 18:52:21 +0300152 vq->vhost_hlen = 0;
153 vq->sock_hlen = 0;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000154 vq->private_data = NULL;
155 vq->log_base = NULL;
156 vq->error_ctx = NULL;
157 vq->error = NULL;
158 vq->kick = NULL;
159 vq->call_ctx = NULL;
160 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200161 vq->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000162}
163
Tejun Heoc23f34452010-06-02 20:40:00 +0200164static int vhost_worker(void *data)
165{
166 struct vhost_dev *dev = data;
167 struct vhost_work *work = NULL;
168 unsigned uninitialized_var(seq);
169
170 for (;;) {
171 /* mb paired w/ kthread_stop */
172 set_current_state(TASK_INTERRUPTIBLE);
173
174 spin_lock_irq(&dev->work_lock);
175 if (work) {
176 work->done_seq = seq;
177 if (work->flushing)
178 wake_up_all(&work->done);
179 }
180
181 if (kthread_should_stop()) {
182 spin_unlock_irq(&dev->work_lock);
183 __set_current_state(TASK_RUNNING);
184 return 0;
185 }
186 if (!list_empty(&dev->work_list)) {
187 work = list_first_entry(&dev->work_list,
188 struct vhost_work, node);
189 list_del_init(&work->node);
190 seq = work->queue_seq;
191 } else
192 work = NULL;
193 spin_unlock_irq(&dev->work_lock);
194
195 if (work) {
196 __set_current_state(TASK_RUNNING);
197 work->fn(work);
198 } else
199 schedule();
200
201 }
202}
203
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000204long vhost_dev_init(struct vhost_dev *dev,
205 struct vhost_virtqueue *vqs, int nvqs)
206{
207 int i;
Tejun Heoc23f34452010-06-02 20:40:00 +0200208
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000209 dev->vqs = vqs;
210 dev->nvqs = nvqs;
211 mutex_init(&dev->mutex);
212 dev->log_ctx = NULL;
213 dev->log_file = NULL;
214 dev->memory = NULL;
215 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200216 spin_lock_init(&dev->work_lock);
217 INIT_LIST_HEAD(&dev->work_list);
218 dev->worker = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000219
220 for (i = 0; i < dev->nvqs; ++i) {
221 dev->vqs[i].dev = dev;
222 mutex_init(&dev->vqs[i].mutex);
223 vhost_vq_reset(dev, dev->vqs + i);
224 if (dev->vqs[i].handle_kick)
225 vhost_poll_init(&dev->vqs[i].poll,
Tejun Heoc23f34452010-06-02 20:40:00 +0200226 dev->vqs[i].handle_kick, POLLIN, dev);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000227 }
Tejun Heoc23f34452010-06-02 20:40:00 +0200228
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000229 return 0;
230}
231
232/* Caller should have device mutex */
233long vhost_dev_check_owner(struct vhost_dev *dev)
234{
235 /* Are you the owner? If not, I don't think you mean to do that */
236 return dev->mm == current->mm ? 0 : -EPERM;
237}
238
239/* Caller should have device mutex */
240static long vhost_dev_set_owner(struct vhost_dev *dev)
241{
Tejun Heoc23f34452010-06-02 20:40:00 +0200242 struct task_struct *worker;
243 int err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000244 /* Is there an owner already? */
Tejun Heoc23f34452010-06-02 20:40:00 +0200245 if (dev->mm) {
246 err = -EBUSY;
247 goto err_mm;
248 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000249 /* No owner, become one */
250 dev->mm = get_task_mm(current);
Tejun Heoc23f34452010-06-02 20:40:00 +0200251 worker = kthread_create(vhost_worker, dev, "vhost-%d", current->pid);
252 if (IS_ERR(worker)) {
253 err = PTR_ERR(worker);
254 goto err_worker;
255 }
256
257 dev->worker = worker;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300258 err = cgroup_attach_task_current_cg(worker);
259 if (err)
260 goto err_cgroup;
Tejun Heoc23f34452010-06-02 20:40:00 +0200261 wake_up_process(worker); /* avoid contributing to loadavg */
262
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000263 return 0;
Michael S. Tsirkin9e3d1952010-07-27 22:56:50 +0300264err_cgroup:
265 kthread_stop(worker);
Tejun Heoc23f34452010-06-02 20:40:00 +0200266err_worker:
267 if (dev->mm)
268 mmput(dev->mm);
269 dev->mm = NULL;
270err_mm:
271 return err;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000272}
273
274/* Caller should have device mutex */
275long vhost_dev_reset_owner(struct vhost_dev *dev)
276{
277 struct vhost_memory *memory;
278
279 /* Restore memory to default empty mapping. */
280 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
281 if (!memory)
282 return -ENOMEM;
283
284 vhost_dev_cleanup(dev);
285
286 memory->nregions = 0;
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100287 RCU_INIT_POINTER(dev->memory, memory);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000288 return 0;
289}
290
291/* Caller should have device mutex */
292void vhost_dev_cleanup(struct vhost_dev *dev)
293{
294 int i;
295 for (i = 0; i < dev->nvqs; ++i) {
296 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
297 vhost_poll_stop(&dev->vqs[i].poll);
298 vhost_poll_flush(&dev->vqs[i].poll);
299 }
300 if (dev->vqs[i].error_ctx)
301 eventfd_ctx_put(dev->vqs[i].error_ctx);
302 if (dev->vqs[i].error)
303 fput(dev->vqs[i].error);
304 if (dev->vqs[i].kick)
305 fput(dev->vqs[i].kick);
306 if (dev->vqs[i].call_ctx)
307 eventfd_ctx_put(dev->vqs[i].call_ctx);
308 if (dev->vqs[i].call)
309 fput(dev->vqs[i].call);
310 vhost_vq_reset(dev, dev->vqs + i);
311 }
312 if (dev->log_ctx)
313 eventfd_ctx_put(dev->log_ctx);
314 dev->log_ctx = NULL;
315 if (dev->log_file)
316 fput(dev->log_file);
317 dev->log_file = NULL;
318 /* No one will access memory at this point */
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100319 kfree(rcu_dereference_protected(dev->memory,
320 lockdep_is_held(&dev->mutex)));
321 RCU_INIT_POINTER(dev->memory, NULL);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000322 if (dev->mm)
323 mmput(dev->mm);
324 dev->mm = NULL;
Tejun Heoc23f34452010-06-02 20:40:00 +0200325
326 WARN_ON(!list_empty(&dev->work_list));
327 kthread_stop(dev->worker);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000328}
329
330static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
331{
332 u64 a = addr / VHOST_PAGE_SIZE / 8;
333 /* Make sure 64 bit math will not overflow. */
334 if (a > ULONG_MAX - (unsigned long)log_base ||
335 a + (unsigned long)log_base > ULONG_MAX)
336 return -EFAULT;
337
338 return access_ok(VERIFY_WRITE, log_base + a,
339 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
340}
341
342/* Caller should have vq mutex and device mutex. */
343static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
344 int log_all)
345{
346 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400347
Michael S. Tsirkinf8322fb2010-05-27 12:28:03 +0300348 if (!mem)
349 return 0;
Jeff Dike179b2842010-04-07 09:59:10 -0400350
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000351 for (i = 0; i < mem->nregions; ++i) {
352 struct vhost_memory_region *m = mem->regions + i;
353 unsigned long a = m->userspace_addr;
354 if (m->memory_size > ULONG_MAX)
355 return 0;
356 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
357 m->memory_size))
358 return 0;
359 else if (log_all && !log_access_ok(log_base,
360 m->guest_phys_addr,
361 m->memory_size))
362 return 0;
363 }
364 return 1;
365}
366
367/* Can we switch to this memory table? */
368/* Caller should have device mutex but not vq mutex */
369static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
370 int log_all)
371{
372 int i;
373 for (i = 0; i < d->nvqs; ++i) {
374 int ok;
375 mutex_lock(&d->vqs[i].mutex);
376 /* If ring is inactive, will check when it's enabled. */
377 if (d->vqs[i].private_data)
378 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
379 log_all);
380 else
381 ok = 1;
382 mutex_unlock(&d->vqs[i].mutex);
383 if (!ok)
384 return 0;
385 }
386 return 1;
387}
388
389static int vq_access_ok(unsigned int num,
390 struct vring_desc __user *desc,
391 struct vring_avail __user *avail,
392 struct vring_used __user *used)
393{
394 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
395 access_ok(VERIFY_READ, avail,
396 sizeof *avail + num * sizeof *avail->ring) &&
397 access_ok(VERIFY_WRITE, used,
398 sizeof *used + num * sizeof *used->ring);
399}
400
401/* Can we log writes? */
402/* Caller should have device mutex but not vq mutex */
403int vhost_log_access_ok(struct vhost_dev *dev)
404{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100405 struct vhost_memory *mp;
406
407 mp = rcu_dereference_protected(dev->memory,
408 lockdep_is_held(&dev->mutex));
409 return memory_access_ok(dev, mp, 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000410}
411
412/* Verify access for write logging. */
413/* Caller should have vq mutex and device mutex */
414static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
415{
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100416 struct vhost_memory *mp;
417
418 mp = rcu_dereference_protected(vq->dev->memory,
419 lockdep_is_held(&vq->mutex));
420 return vq_memory_access_ok(log_base, mp,
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000421 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
422 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
423 sizeof *vq->used +
424 vq->num * sizeof *vq->used->ring));
425}
426
427/* Can we start vq? */
428/* Caller should have vq mutex and device mutex */
429int vhost_vq_access_ok(struct vhost_virtqueue *vq)
430{
431 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
432 vq_log_access_ok(vq, vq->log_base);
433}
434
435static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
436{
437 struct vhost_memory mem, *newmem, *oldmem;
438 unsigned long size = offsetof(struct vhost_memory, regions);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900439 if (copy_from_user(&mem, m, size))
440 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000441 if (mem.padding)
442 return -EOPNOTSUPP;
443 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
444 return -E2BIG;
445 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
446 if (!newmem)
447 return -ENOMEM;
448
449 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900450 if (copy_from_user(newmem->regions, m->regions,
451 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000452 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900453 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000454 }
455
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900456 if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL))) {
457 kfree(newmem);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000458 return -EFAULT;
Takuya Yoshikawaa02c3782010-05-27 19:03:56 +0900459 }
Arnd Bergmann28457ee2010-03-09 19:24:45 +0100460 oldmem = rcu_dereference_protected(d->memory,
461 lockdep_is_held(&d->mutex));
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000462 rcu_assign_pointer(d->memory, newmem);
463 synchronize_rcu();
464 kfree(oldmem);
465 return 0;
466}
467
468static int init_used(struct vhost_virtqueue *vq,
469 struct vring_used __user *used)
470{
471 int r = put_user(vq->used_flags, &used->flags);
472 if (r)
473 return r;
474 return get_user(vq->last_used_idx, &used->idx);
475}
476
477static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
478{
479 struct file *eventfp, *filep = NULL,
480 *pollstart = NULL, *pollstop = NULL;
481 struct eventfd_ctx *ctx = NULL;
482 u32 __user *idxp = argp;
483 struct vhost_virtqueue *vq;
484 struct vhost_vring_state s;
485 struct vhost_vring_file f;
486 struct vhost_vring_addr a;
487 u32 idx;
488 long r;
489
490 r = get_user(idx, idxp);
491 if (r < 0)
492 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530493 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000494 return -ENOBUFS;
495
496 vq = d->vqs + idx;
497
498 mutex_lock(&vq->mutex);
499
500 switch (ioctl) {
501 case VHOST_SET_VRING_NUM:
502 /* Resizing ring with an active backend?
503 * You don't want to do that. */
504 if (vq->private_data) {
505 r = -EBUSY;
506 break;
507 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900508 if (copy_from_user(&s, argp, sizeof s)) {
509 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000510 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900511 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000512 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
513 r = -EINVAL;
514 break;
515 }
516 vq->num = s.num;
517 break;
518 case VHOST_SET_VRING_BASE:
519 /* Moving base with an active backend?
520 * You don't want to do that. */
521 if (vq->private_data) {
522 r = -EBUSY;
523 break;
524 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900525 if (copy_from_user(&s, argp, sizeof s)) {
526 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000527 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900528 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000529 if (s.num > 0xffff) {
530 r = -EINVAL;
531 break;
532 }
533 vq->last_avail_idx = s.num;
534 /* Forget the cached index value. */
535 vq->avail_idx = vq->last_avail_idx;
536 break;
537 case VHOST_GET_VRING_BASE:
538 s.index = idx;
539 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900540 if (copy_to_user(argp, &s, sizeof s))
541 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000542 break;
543 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900544 if (copy_from_user(&a, argp, sizeof a)) {
545 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000546 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900547 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000548 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
549 r = -EOPNOTSUPP;
550 break;
551 }
552 /* For 32bit, verify that the top 32bits of the user
553 data are set to zero. */
554 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
555 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
556 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
557 r = -EFAULT;
558 break;
559 }
560 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
561 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
562 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
563 r = -EINVAL;
564 break;
565 }
566
567 /* We only verify access here if backend is configured.
568 * If it is not, we don't as size might not have been setup.
569 * We will verify when backend is configured. */
570 if (vq->private_data) {
571 if (!vq_access_ok(vq->num,
572 (void __user *)(unsigned long)a.desc_user_addr,
573 (void __user *)(unsigned long)a.avail_user_addr,
574 (void __user *)(unsigned long)a.used_user_addr)) {
575 r = -EINVAL;
576 break;
577 }
578
579 /* Also validate log access for used ring if enabled. */
580 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
581 !log_access_ok(vq->log_base, a.log_guest_addr,
582 sizeof *vq->used +
583 vq->num * sizeof *vq->used->ring)) {
584 r = -EINVAL;
585 break;
586 }
587 }
588
589 r = init_used(vq, (struct vring_used __user *)(unsigned long)
590 a.used_user_addr);
591 if (r)
592 break;
593 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
594 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
595 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
596 vq->log_addr = a.log_guest_addr;
597 vq->used = (void __user *)(unsigned long)a.used_user_addr;
598 break;
599 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900600 if (copy_from_user(&f, argp, sizeof f)) {
601 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000602 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900603 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000604 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200605 if (IS_ERR(eventfp)) {
606 r = PTR_ERR(eventfp);
607 break;
608 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000609 if (eventfp != vq->kick) {
610 pollstop = filep = vq->kick;
611 pollstart = vq->kick = eventfp;
612 } else
613 filep = eventfp;
614 break;
615 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900616 if (copy_from_user(&f, argp, sizeof f)) {
617 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000618 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900619 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000620 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200621 if (IS_ERR(eventfp)) {
622 r = PTR_ERR(eventfp);
623 break;
624 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000625 if (eventfp != vq->call) {
626 filep = vq->call;
627 ctx = vq->call_ctx;
628 vq->call = eventfp;
629 vq->call_ctx = eventfp ?
630 eventfd_ctx_fileget(eventfp) : NULL;
631 } else
632 filep = eventfp;
633 break;
634 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900635 if (copy_from_user(&f, argp, sizeof f)) {
636 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000637 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900638 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000639 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200640 if (IS_ERR(eventfp)) {
641 r = PTR_ERR(eventfp);
642 break;
643 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000644 if (eventfp != vq->error) {
645 filep = vq->error;
646 vq->error = eventfp;
647 ctx = vq->error_ctx;
648 vq->error_ctx = eventfp ?
649 eventfd_ctx_fileget(eventfp) : NULL;
650 } else
651 filep = eventfp;
652 break;
653 default:
654 r = -ENOIOCTLCMD;
655 }
656
657 if (pollstop && vq->handle_kick)
658 vhost_poll_stop(&vq->poll);
659
660 if (ctx)
661 eventfd_ctx_put(ctx);
662 if (filep)
663 fput(filep);
664
665 if (pollstart && vq->handle_kick)
666 vhost_poll_start(&vq->poll, vq->kick);
667
668 mutex_unlock(&vq->mutex);
669
670 if (pollstop && vq->handle_kick)
671 vhost_poll_flush(&vq->poll);
672 return r;
673}
674
675/* Caller must have device mutex */
676long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
677{
678 void __user *argp = (void __user *)arg;
679 struct file *eventfp, *filep = NULL;
680 struct eventfd_ctx *ctx = NULL;
681 u64 p;
682 long r;
683 int i, fd;
684
685 /* If you are not the owner, you can become one */
686 if (ioctl == VHOST_SET_OWNER) {
687 r = vhost_dev_set_owner(d);
688 goto done;
689 }
690
691 /* You must be the owner to do anything else */
692 r = vhost_dev_check_owner(d);
693 if (r)
694 goto done;
695
696 switch (ioctl) {
697 case VHOST_SET_MEM_TABLE:
698 r = vhost_set_memory(d, argp);
699 break;
700 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900701 if (copy_from_user(&p, argp, sizeof p)) {
702 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000703 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900704 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000705 if ((u64)(unsigned long)p != p) {
706 r = -EFAULT;
707 break;
708 }
709 for (i = 0; i < d->nvqs; ++i) {
710 struct vhost_virtqueue *vq;
711 void __user *base = (void __user *)(unsigned long)p;
712 vq = d->vqs + i;
713 mutex_lock(&vq->mutex);
714 /* If ring is inactive, will check when it's enabled. */
715 if (vq->private_data && !vq_log_access_ok(vq, base))
716 r = -EFAULT;
717 else
718 vq->log_base = base;
719 mutex_unlock(&vq->mutex);
720 }
721 break;
722 case VHOST_SET_LOG_FD:
723 r = get_user(fd, (int __user *)argp);
724 if (r < 0)
725 break;
726 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
727 if (IS_ERR(eventfp)) {
728 r = PTR_ERR(eventfp);
729 break;
730 }
731 if (eventfp != d->log_file) {
732 filep = d->log_file;
733 ctx = d->log_ctx;
734 d->log_ctx = eventfp ?
735 eventfd_ctx_fileget(eventfp) : NULL;
736 } else
737 filep = eventfp;
738 for (i = 0; i < d->nvqs; ++i) {
739 mutex_lock(&d->vqs[i].mutex);
740 d->vqs[i].log_ctx = d->log_ctx;
741 mutex_unlock(&d->vqs[i].mutex);
742 }
743 if (ctx)
744 eventfd_ctx_put(ctx);
745 if (filep)
746 fput(filep);
747 break;
748 default:
749 r = vhost_set_vring(d, ioctl, argp);
750 break;
751 }
752done:
753 return r;
754}
755
756static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
757 __u64 addr, __u32 len)
758{
759 struct vhost_memory_region *reg;
760 int i;
761 /* linear search is not brilliant, but we really have on the order of 6
762 * regions in practice */
763 for (i = 0; i < mem->nregions; ++i) {
764 reg = mem->regions + i;
765 if (reg->guest_phys_addr <= addr &&
766 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
767 return reg;
768 }
769 return NULL;
770}
771
772/* TODO: This is really inefficient. We need something like get_user()
773 * (instruction directly accesses the data, with an exception table entry
774 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
775 */
776static int set_bit_to_user(int nr, void __user *addr)
777{
778 unsigned long log = (unsigned long)addr;
779 struct page *page;
780 void *base;
781 int bit = nr + (log % PAGE_SIZE) * 8;
782 int r;
783 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200784 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000785 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200786 BUG_ON(r != 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000787 base = kmap_atomic(page, KM_USER0);
788 set_bit(bit, base);
789 kunmap_atomic(base, KM_USER0);
790 set_page_dirty_lock(page);
791 put_page(page);
792 return 0;
793}
794
795static int log_write(void __user *log_base,
796 u64 write_address, u64 write_length)
797{
798 int r;
799 if (!write_length)
800 return 0;
801 write_address /= VHOST_PAGE_SIZE;
802 for (;;) {
803 u64 base = (u64)(unsigned long)log_base;
804 u64 log = base + write_address / 8;
805 int bit = write_address % 8;
806 if ((u64)(unsigned long)log != log)
807 return -EFAULT;
808 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
809 if (r < 0)
810 return r;
811 if (write_length <= VHOST_PAGE_SIZE)
812 break;
813 write_length -= VHOST_PAGE_SIZE;
814 write_address += VHOST_PAGE_SIZE;
815 }
816 return r;
817}
818
819int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
820 unsigned int log_num, u64 len)
821{
822 int i, r;
823
824 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000825 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000826 for (i = 0; i < log_num; ++i) {
827 u64 l = min(log[i].len, len);
828 r = log_write(vq->log_base, log[i].addr, l);
829 if (r < 0)
830 return r;
831 len -= l;
832 if (!len)
833 return 0;
834 }
835 if (vq->log_ctx)
836 eventfd_signal(vq->log_ctx, 1);
837 /* Length written exceeds what we have stored. This is a bug. */
838 BUG();
839 return 0;
840}
841
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400842static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
843 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000844{
845 const struct vhost_memory_region *reg;
846 struct vhost_memory *mem;
847 struct iovec *_iov;
848 u64 s = 0;
849 int ret = 0;
850
851 rcu_read_lock();
852
853 mem = rcu_dereference(dev->memory);
854 while ((u64)len > s) {
855 u64 size;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300856 if (unlikely(ret >= iov_size)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000857 ret = -ENOBUFS;
858 break;
859 }
860 reg = find_region(mem, addr, len);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300861 if (unlikely(!reg)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000862 ret = -EFAULT;
863 break;
864 }
865 _iov = iov + ret;
866 size = reg->memory_size - addr + reg->guest_phys_addr;
867 _iov->iov_len = min((u64)len, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400868 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000869 (reg->userspace_addr + addr - reg->guest_phys_addr);
870 s += size;
871 addr += size;
872 ++ret;
873 }
874
875 rcu_read_unlock();
876 return ret;
877}
878
879/* Each buffer in the virtqueues is actually a chain of descriptors. This
880 * function returns the next descriptor in the chain,
881 * or -1U if we're at the end. */
882static unsigned next_desc(struct vring_desc *desc)
883{
884 unsigned int next;
885
886 /* If this descriptor says it doesn't chain, we're done. */
887 if (!(desc->flags & VRING_DESC_F_NEXT))
888 return -1U;
889
890 /* Check they're not leading us off end of descriptors. */
891 next = desc->next;
892 /* Make sure compiler knows to grab that: we don't want it changing! */
893 /* We will use the result as an index in an array, so most
894 * architectures only need a compiler barrier here. */
895 read_barrier_depends();
896
897 return next;
898}
899
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300900static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
901 struct iovec iov[], unsigned int iov_size,
902 unsigned int *out_num, unsigned int *in_num,
903 struct vhost_log *log, unsigned int *log_num,
904 struct vring_desc *indirect)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000905{
906 struct vring_desc desc;
907 unsigned int i = 0, count, found = 0;
908 int ret;
909
910 /* Sanity check */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300911 if (unlikely(indirect->len % sizeof desc)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000912 vq_err(vq, "Invalid length in indirect descriptor: "
913 "len 0x%llx not multiple of 0x%zx\n",
914 (unsigned long long)indirect->len,
915 sizeof desc);
916 return -EINVAL;
917 }
918
919 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
920 ARRAY_SIZE(vq->indirect));
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300921 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000922 vq_err(vq, "Translation failure %d in indirect.\n", ret);
923 return ret;
924 }
925
926 /* We will use the result as an address to read from, so most
927 * architectures only need a compiler barrier here. */
928 read_barrier_depends();
929
930 count = indirect->len / sizeof desc;
931 /* Buffers are chained via a 16 bit next field, so
932 * we can have at most 2^16 of these. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300933 if (unlikely(count > USHRT_MAX + 1)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000934 vq_err(vq, "Indirect buffer length too big: %d\n",
935 indirect->len);
936 return -E2BIG;
937 }
938
939 do {
940 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300941 if (unlikely(++found > count)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000942 vq_err(vq, "Loop detected: last one at %u "
943 "indirect size %u\n",
944 i, count);
945 return -EINVAL;
946 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300947 if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
948 sizeof desc))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000949 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
950 i, (size_t)indirect->addr + i * sizeof desc);
951 return -EINVAL;
952 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300953 if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000954 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
955 i, (size_t)indirect->addr + i * sizeof desc);
956 return -EINVAL;
957 }
958
959 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
960 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300961 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000962 vq_err(vq, "Translation failure %d indirect idx %d\n",
963 ret, i);
964 return ret;
965 }
966 /* If this is an input descriptor, increment that count. */
967 if (desc.flags & VRING_DESC_F_WRITE) {
968 *in_num += ret;
969 if (unlikely(log)) {
970 log[*log_num].addr = desc.addr;
971 log[*log_num].len = desc.len;
972 ++*log_num;
973 }
974 } else {
975 /* If it's an output descriptor, they're all supposed
976 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +0300977 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000978 vq_err(vq, "Indirect descriptor "
979 "has out after in: idx %d\n", i);
980 return -EINVAL;
981 }
982 *out_num += ret;
983 }
984 } while ((i = next_desc(&desc)) != -1);
985 return 0;
986}
987
988/* This looks in the virtqueue and for the first available buffer, and converts
989 * it to an iovec for convenient access. Since descriptors consist of some
990 * number of output then some number of input descriptors, it's actually two
991 * iovecs, but we pack them into one and note how many of each there were.
992 *
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +0300993 * This function returns the descriptor number found, or vq->num (which is
994 * never a valid descriptor number) if none was found. A negative code is
995 * returned on error. */
996int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
997 struct iovec iov[], unsigned int iov_size,
998 unsigned int *out_num, unsigned int *in_num,
999 struct vhost_log *log, unsigned int *log_num)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001000{
1001 struct vring_desc desc;
1002 unsigned int i, head, found = 0;
1003 u16 last_avail_idx;
1004 int ret;
1005
1006 /* Check it isn't doing very strange things with descriptor numbers. */
1007 last_avail_idx = vq->last_avail_idx;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001008 if (unlikely(get_user(vq->avail_idx, &vq->avail->idx))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001009 vq_err(vq, "Failed to access avail idx at %p\n",
1010 &vq->avail->idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001011 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001012 }
1013
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001014 if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001015 vq_err(vq, "Guest moved used index from %u to %u",
1016 last_avail_idx, vq->avail_idx);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001017 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001018 }
1019
1020 /* If there's nothing new since last we looked, return invalid. */
1021 if (vq->avail_idx == last_avail_idx)
1022 return vq->num;
1023
1024 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001025 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001026
1027 /* Grab the next descriptor number they're advertising, and increment
1028 * the index we've seen. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001029 if (unlikely(get_user(head,
1030 &vq->avail->ring[last_avail_idx % vq->num]))) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001031 vq_err(vq, "Failed to read head: idx %d address %p\n",
1032 last_avail_idx,
1033 &vq->avail->ring[last_avail_idx % vq->num]);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001034 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001035 }
1036
1037 /* If their number is silly, that's an error. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001038 if (unlikely(head >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001039 vq_err(vq, "Guest says index %u > %u is available",
1040 head, vq->num);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001041 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001042 }
1043
1044 /* When we start there are none of either input nor output. */
1045 *out_num = *in_num = 0;
1046 if (unlikely(log))
1047 *log_num = 0;
1048
1049 i = head;
1050 do {
1051 unsigned iov_count = *in_num + *out_num;
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001052 if (unlikely(i >= vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001053 vq_err(vq, "Desc index is %u > %u, head = %u",
1054 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001055 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001056 }
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001057 if (unlikely(++found > vq->num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001058 vq_err(vq, "Loop detected: last one at %u "
1059 "vq size %u head %u\n",
1060 i, vq->num, head);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001061 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001062 }
1063 ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001064 if (unlikely(ret)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001065 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
1066 i, vq->desc + i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001067 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001068 }
1069 if (desc.flags & VRING_DESC_F_INDIRECT) {
1070 ret = get_indirect(dev, vq, iov, iov_size,
1071 out_num, in_num,
1072 log, log_num, &desc);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001073 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001074 vq_err(vq, "Failure detected "
1075 "in indirect descriptor at idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001076 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001077 }
1078 continue;
1079 }
1080
1081 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
1082 iov_size - iov_count);
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001083 if (unlikely(ret < 0)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001084 vq_err(vq, "Translation failure %d descriptor idx %d\n",
1085 ret, i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001086 return ret;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001087 }
1088 if (desc.flags & VRING_DESC_F_WRITE) {
1089 /* If this is an input descriptor,
1090 * increment that count. */
1091 *in_num += ret;
1092 if (unlikely(log)) {
1093 log[*log_num].addr = desc.addr;
1094 log[*log_num].len = desc.len;
1095 ++*log_num;
1096 }
1097 } else {
1098 /* If it's an output descriptor, they're all supposed
1099 * to come before any input descriptors. */
Michael S. Tsirkin7b3384f2010-07-01 18:40:12 +03001100 if (unlikely(*in_num)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001101 vq_err(vq, "Descriptor has out after in: "
1102 "idx %d\n", i);
Michael S. Tsirkind5675bd2010-06-24 16:59:59 +03001103 return -EINVAL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001104 }
1105 *out_num += ret;
1106 }
1107 } while ((i = next_desc(&desc)) != -1);
1108
1109 /* On success, increment avail index. */
1110 vq->last_avail_idx++;
1111 return head;
1112}
1113
1114/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
David Stevens8dd014a2010-07-27 18:52:21 +03001115void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int n)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001116{
David Stevens8dd014a2010-07-27 18:52:21 +03001117 vq->last_avail_idx -= n;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001118}
1119
1120/* After we've used one of their buffers, we tell them about it. We'll then
1121 * want to notify the guest, using eventfd. */
1122int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1123{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001124 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001125
1126 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1127 * next entry in that used ring. */
1128 used = &vq->used->ring[vq->last_used_idx % vq->num];
1129 if (put_user(head, &used->id)) {
1130 vq_err(vq, "Failed to write used id");
1131 return -EFAULT;
1132 }
1133 if (put_user(len, &used->len)) {
1134 vq_err(vq, "Failed to write used len");
1135 return -EFAULT;
1136 }
1137 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001138 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001139 if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
1140 vq_err(vq, "Failed to increment used idx");
1141 return -EFAULT;
1142 }
1143 if (unlikely(vq->log_used)) {
1144 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001145 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001146 /* Log used ring entry write. */
1147 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001148 vq->log_addr +
1149 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001150 sizeof *used);
1151 /* Log used index update. */
1152 log_write(vq->log_base,
1153 vq->log_addr + offsetof(struct vring_used, idx),
1154 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001155 if (vq->log_ctx)
1156 eventfd_signal(vq->log_ctx, 1);
1157 }
1158 vq->last_used_idx++;
1159 return 0;
1160}
1161
David Stevens8dd014a2010-07-27 18:52:21 +03001162static int __vhost_add_used_n(struct vhost_virtqueue *vq,
1163 struct vring_used_elem *heads,
1164 unsigned count)
1165{
1166 struct vring_used_elem __user *used;
1167 int start;
1168
1169 start = vq->last_used_idx % vq->num;
1170 used = vq->used->ring + start;
1171 if (copy_to_user(used, heads, count * sizeof *used)) {
1172 vq_err(vq, "Failed to write used");
1173 return -EFAULT;
1174 }
1175 if (unlikely(vq->log_used)) {
1176 /* Make sure data is seen before log. */
1177 smp_wmb();
1178 /* Log used ring entry write. */
1179 log_write(vq->log_base,
1180 vq->log_addr +
1181 ((void __user *)used - (void __user *)vq->used),
1182 count * sizeof *used);
1183 }
1184 vq->last_used_idx += count;
1185 return 0;
1186}
1187
1188/* After we've used one of their buffers, we tell them about it. We'll then
1189 * want to notify the guest, using eventfd. */
1190int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads,
1191 unsigned count)
1192{
1193 int start, n, r;
1194
1195 start = vq->last_used_idx % vq->num;
1196 n = vq->num - start;
1197 if (n < count) {
1198 r = __vhost_add_used_n(vq, heads, n);
1199 if (r < 0)
1200 return r;
1201 heads += n;
1202 count -= n;
1203 }
1204 r = __vhost_add_used_n(vq, heads, count);
1205
1206 /* Make sure buffer is written before we update index. */
1207 smp_wmb();
1208 if (put_user(vq->last_used_idx, &vq->used->idx)) {
1209 vq_err(vq, "Failed to increment used idx");
1210 return -EFAULT;
1211 }
1212 if (unlikely(vq->log_used)) {
1213 /* Log used index update. */
1214 log_write(vq->log_base,
1215 vq->log_addr + offsetof(struct vring_used, idx),
1216 sizeof vq->used->idx);
1217 if (vq->log_ctx)
1218 eventfd_signal(vq->log_ctx, 1);
1219 }
1220 return r;
1221}
1222
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001223/* This actually signals the guest, using eventfd. */
1224void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1225{
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001226 __u16 flags;
1227 /* Flush out used index updates. This is paired
1228 * with the barrier that the Guest executes when enabling
1229 * interrupts. */
1230 smp_mb();
1231
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001232 if (get_user(flags, &vq->avail->flags)) {
1233 vq_err(vq, "Failed to get flags");
1234 return;
1235 }
1236
1237 /* If they don't want an interrupt, don't signal, unless empty. */
1238 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1239 (vq->avail_idx != vq->last_avail_idx ||
1240 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1241 return;
1242
1243 /* Signal the Guest tell them we used something up. */
1244 if (vq->call_ctx)
1245 eventfd_signal(vq->call_ctx, 1);
1246}
1247
1248/* And here's the combo meal deal. Supersize me! */
1249void vhost_add_used_and_signal(struct vhost_dev *dev,
1250 struct vhost_virtqueue *vq,
1251 unsigned int head, int len)
1252{
1253 vhost_add_used(vq, head, len);
1254 vhost_signal(dev, vq);
1255}
1256
David Stevens8dd014a2010-07-27 18:52:21 +03001257/* multi-buffer version of vhost_add_used_and_signal */
1258void vhost_add_used_and_signal_n(struct vhost_dev *dev,
1259 struct vhost_virtqueue *vq,
1260 struct vring_used_elem *heads, unsigned count)
1261{
1262 vhost_add_used_n(vq, heads, count);
1263 vhost_signal(dev, vq);
1264}
1265
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001266/* OK, now we need to know about added descriptors. */
1267bool vhost_enable_notify(struct vhost_virtqueue *vq)
1268{
1269 u16 avail_idx;
1270 int r;
1271 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1272 return false;
1273 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1274 r = put_user(vq->used_flags, &vq->used->flags);
1275 if (r) {
1276 vq_err(vq, "Failed to enable notification at %p: %d\n",
1277 &vq->used->flags, r);
1278 return false;
1279 }
1280 /* They could have slipped one in as we were doing that: make
1281 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001282 smp_mb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001283 r = get_user(avail_idx, &vq->avail->idx);
1284 if (r) {
1285 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1286 &vq->avail->idx, r);
1287 return false;
1288 }
1289
David Stevens8dd014a2010-07-27 18:52:21 +03001290 return avail_idx != vq->avail_idx;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001291}
1292
1293/* We don't need to be notified again. */
1294void vhost_disable_notify(struct vhost_virtqueue *vq)
1295{
1296 int r;
1297 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1298 return;
1299 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1300 r = put_user(vq->used_flags, &vq->used->flags);
1301 if (r)
1302 vq_err(vq, "Failed to enable notification at %p: %d\n",
1303 &vq->used->flags, r);
1304}