blob: e36620272715e061f5e7f5ff502a8d527b34769c [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>
20#include <linux/workqueue.h>
21#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>
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +000026
27#include <linux/net.h>
28#include <linux/if_packet.h>
29#include <linux/if_arp.h>
30
31#include <net/sock.h>
32
33#include "vhost.h"
34
35enum {
36 VHOST_MEMORY_MAX_NREGIONS = 64,
37 VHOST_MEMORY_F_LOG = 0x1,
38};
39
40static struct workqueue_struct *vhost_workqueue;
41
42static 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{
55 struct vhost_poll *poll;
56 poll = container_of(wait, struct vhost_poll, wait);
57 if (!((unsigned long)key & poll->mask))
58 return 0;
59
60 queue_work(vhost_workqueue, &poll->work);
61 return 0;
62}
63
64/* Init poll structure */
65void vhost_poll_init(struct vhost_poll *poll, work_func_t func,
66 unsigned long mask)
67{
68 INIT_WORK(&poll->work, func);
69 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup);
70 init_poll_funcptr(&poll->table, vhost_poll_func);
71 poll->mask = mask;
72}
73
74/* Start polling a file. We add ourselves to file's wait queue. The caller must
75 * keep a reference to a file until after vhost_poll_stop is called. */
76void vhost_poll_start(struct vhost_poll *poll, struct file *file)
77{
78 unsigned long mask;
79 mask = file->f_op->poll(file, &poll->table);
80 if (mask)
81 vhost_poll_wakeup(&poll->wait, 0, 0, (void *)mask);
82}
83
84/* Stop polling a file. After this function returns, it becomes safe to drop the
85 * file reference. You must also flush afterwards. */
86void vhost_poll_stop(struct vhost_poll *poll)
87{
88 remove_wait_queue(poll->wqh, &poll->wait);
89}
90
91/* Flush any work that has been scheduled. When calling this, don't hold any
92 * locks that are also used by the callback. */
93void vhost_poll_flush(struct vhost_poll *poll)
94{
95 flush_work(&poll->work);
96}
97
98void vhost_poll_queue(struct vhost_poll *poll)
99{
100 queue_work(vhost_workqueue, &poll->work);
101}
102
103static void vhost_vq_reset(struct vhost_dev *dev,
104 struct vhost_virtqueue *vq)
105{
106 vq->num = 1;
107 vq->desc = NULL;
108 vq->avail = NULL;
109 vq->used = NULL;
110 vq->last_avail_idx = 0;
111 vq->avail_idx = 0;
112 vq->last_used_idx = 0;
113 vq->used_flags = 0;
114 vq->used_flags = 0;
115 vq->log_used = false;
116 vq->log_addr = -1ull;
117 vq->hdr_size = 0;
118 vq->private_data = NULL;
119 vq->log_base = NULL;
120 vq->error_ctx = NULL;
121 vq->error = NULL;
122 vq->kick = NULL;
123 vq->call_ctx = NULL;
124 vq->call = NULL;
Michael S. Tsirkin73a99f02010-02-23 11:23:45 +0200125 vq->log_ctx = NULL;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000126}
127
128long vhost_dev_init(struct vhost_dev *dev,
129 struct vhost_virtqueue *vqs, int nvqs)
130{
131 int i;
132 dev->vqs = vqs;
133 dev->nvqs = nvqs;
134 mutex_init(&dev->mutex);
135 dev->log_ctx = NULL;
136 dev->log_file = NULL;
137 dev->memory = NULL;
138 dev->mm = NULL;
139
140 for (i = 0; i < dev->nvqs; ++i) {
141 dev->vqs[i].dev = dev;
142 mutex_init(&dev->vqs[i].mutex);
143 vhost_vq_reset(dev, dev->vqs + i);
144 if (dev->vqs[i].handle_kick)
145 vhost_poll_init(&dev->vqs[i].poll,
146 dev->vqs[i].handle_kick,
147 POLLIN);
148 }
149 return 0;
150}
151
152/* Caller should have device mutex */
153long vhost_dev_check_owner(struct vhost_dev *dev)
154{
155 /* Are you the owner? If not, I don't think you mean to do that */
156 return dev->mm == current->mm ? 0 : -EPERM;
157}
158
159/* Caller should have device mutex */
160static long vhost_dev_set_owner(struct vhost_dev *dev)
161{
162 /* Is there an owner already? */
163 if (dev->mm)
164 return -EBUSY;
165 /* No owner, become one */
166 dev->mm = get_task_mm(current);
167 return 0;
168}
169
170/* Caller should have device mutex */
171long vhost_dev_reset_owner(struct vhost_dev *dev)
172{
173 struct vhost_memory *memory;
174
175 /* Restore memory to default empty mapping. */
176 memory = kmalloc(offsetof(struct vhost_memory, regions), GFP_KERNEL);
177 if (!memory)
178 return -ENOMEM;
179
180 vhost_dev_cleanup(dev);
181
182 memory->nregions = 0;
183 dev->memory = memory;
184 return 0;
185}
186
187/* Caller should have device mutex */
188void vhost_dev_cleanup(struct vhost_dev *dev)
189{
190 int i;
191 for (i = 0; i < dev->nvqs; ++i) {
192 if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
193 vhost_poll_stop(&dev->vqs[i].poll);
194 vhost_poll_flush(&dev->vqs[i].poll);
195 }
196 if (dev->vqs[i].error_ctx)
197 eventfd_ctx_put(dev->vqs[i].error_ctx);
198 if (dev->vqs[i].error)
199 fput(dev->vqs[i].error);
200 if (dev->vqs[i].kick)
201 fput(dev->vqs[i].kick);
202 if (dev->vqs[i].call_ctx)
203 eventfd_ctx_put(dev->vqs[i].call_ctx);
204 if (dev->vqs[i].call)
205 fput(dev->vqs[i].call);
206 vhost_vq_reset(dev, dev->vqs + i);
207 }
208 if (dev->log_ctx)
209 eventfd_ctx_put(dev->log_ctx);
210 dev->log_ctx = NULL;
211 if (dev->log_file)
212 fput(dev->log_file);
213 dev->log_file = NULL;
214 /* No one will access memory at this point */
215 kfree(dev->memory);
216 dev->memory = NULL;
217 if (dev->mm)
218 mmput(dev->mm);
219 dev->mm = NULL;
220}
221
222static int log_access_ok(void __user *log_base, u64 addr, unsigned long sz)
223{
224 u64 a = addr / VHOST_PAGE_SIZE / 8;
225 /* Make sure 64 bit math will not overflow. */
226 if (a > ULONG_MAX - (unsigned long)log_base ||
227 a + (unsigned long)log_base > ULONG_MAX)
228 return -EFAULT;
229
230 return access_ok(VERIFY_WRITE, log_base + a,
231 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8);
232}
233
234/* Caller should have vq mutex and device mutex. */
235static int vq_memory_access_ok(void __user *log_base, struct vhost_memory *mem,
236 int log_all)
237{
238 int i;
Jeff Dike179b2842010-04-07 09:59:10 -0400239
240 if (!mem)
241 return 0;
242
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000243 for (i = 0; i < mem->nregions; ++i) {
244 struct vhost_memory_region *m = mem->regions + i;
245 unsigned long a = m->userspace_addr;
246 if (m->memory_size > ULONG_MAX)
247 return 0;
248 else if (!access_ok(VERIFY_WRITE, (void __user *)a,
249 m->memory_size))
250 return 0;
251 else if (log_all && !log_access_ok(log_base,
252 m->guest_phys_addr,
253 m->memory_size))
254 return 0;
255 }
256 return 1;
257}
258
259/* Can we switch to this memory table? */
260/* Caller should have device mutex but not vq mutex */
261static int memory_access_ok(struct vhost_dev *d, struct vhost_memory *mem,
262 int log_all)
263{
264 int i;
265 for (i = 0; i < d->nvqs; ++i) {
266 int ok;
267 mutex_lock(&d->vqs[i].mutex);
268 /* If ring is inactive, will check when it's enabled. */
269 if (d->vqs[i].private_data)
270 ok = vq_memory_access_ok(d->vqs[i].log_base, mem,
271 log_all);
272 else
273 ok = 1;
274 mutex_unlock(&d->vqs[i].mutex);
275 if (!ok)
276 return 0;
277 }
278 return 1;
279}
280
281static int vq_access_ok(unsigned int num,
282 struct vring_desc __user *desc,
283 struct vring_avail __user *avail,
284 struct vring_used __user *used)
285{
286 return access_ok(VERIFY_READ, desc, num * sizeof *desc) &&
287 access_ok(VERIFY_READ, avail,
288 sizeof *avail + num * sizeof *avail->ring) &&
289 access_ok(VERIFY_WRITE, used,
290 sizeof *used + num * sizeof *used->ring);
291}
292
293/* Can we log writes? */
294/* Caller should have device mutex but not vq mutex */
295int vhost_log_access_ok(struct vhost_dev *dev)
296{
297 return memory_access_ok(dev, dev->memory, 1);
298}
299
300/* Verify access for write logging. */
301/* Caller should have vq mutex and device mutex */
302static int vq_log_access_ok(struct vhost_virtqueue *vq, void __user *log_base)
303{
304 return vq_memory_access_ok(log_base, vq->dev->memory,
305 vhost_has_feature(vq->dev, VHOST_F_LOG_ALL)) &&
306 (!vq->log_used || log_access_ok(log_base, vq->log_addr,
307 sizeof *vq->used +
308 vq->num * sizeof *vq->used->ring));
309}
310
311/* Can we start vq? */
312/* Caller should have vq mutex and device mutex */
313int vhost_vq_access_ok(struct vhost_virtqueue *vq)
314{
315 return vq_access_ok(vq->num, vq->desc, vq->avail, vq->used) &&
316 vq_log_access_ok(vq, vq->log_base);
317}
318
319static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m)
320{
321 struct vhost_memory mem, *newmem, *oldmem;
322 unsigned long size = offsetof(struct vhost_memory, regions);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900323 if (copy_from_user(&mem, m, size))
324 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000325 if (mem.padding)
326 return -EOPNOTSUPP;
327 if (mem.nregions > VHOST_MEMORY_MAX_NREGIONS)
328 return -E2BIG;
329 newmem = kmalloc(size + mem.nregions * sizeof *m->regions, GFP_KERNEL);
330 if (!newmem)
331 return -ENOMEM;
332
333 memcpy(newmem, &mem, size);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900334 if (copy_from_user(newmem->regions, m->regions,
335 mem.nregions * sizeof *m->regions)) {
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000336 kfree(newmem);
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900337 return -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000338 }
339
340 if (!memory_access_ok(d, newmem, vhost_has_feature(d, VHOST_F_LOG_ALL)))
341 return -EFAULT;
342 oldmem = d->memory;
343 rcu_assign_pointer(d->memory, newmem);
344 synchronize_rcu();
345 kfree(oldmem);
346 return 0;
347}
348
349static int init_used(struct vhost_virtqueue *vq,
350 struct vring_used __user *used)
351{
352 int r = put_user(vq->used_flags, &used->flags);
353 if (r)
354 return r;
355 return get_user(vq->last_used_idx, &used->idx);
356}
357
358static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
359{
360 struct file *eventfp, *filep = NULL,
361 *pollstart = NULL, *pollstop = NULL;
362 struct eventfd_ctx *ctx = NULL;
363 u32 __user *idxp = argp;
364 struct vhost_virtqueue *vq;
365 struct vhost_vring_state s;
366 struct vhost_vring_file f;
367 struct vhost_vring_addr a;
368 u32 idx;
369 long r;
370
371 r = get_user(idx, idxp);
372 if (r < 0)
373 return r;
Krishna Kumar0f3d9a12010-05-25 11:10:36 +0530374 if (idx >= d->nvqs)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000375 return -ENOBUFS;
376
377 vq = d->vqs + idx;
378
379 mutex_lock(&vq->mutex);
380
381 switch (ioctl) {
382 case VHOST_SET_VRING_NUM:
383 /* Resizing ring with an active backend?
384 * You don't want to do that. */
385 if (vq->private_data) {
386 r = -EBUSY;
387 break;
388 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900389 if (copy_from_user(&s, argp, sizeof s)) {
390 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000391 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900392 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000393 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) {
394 r = -EINVAL;
395 break;
396 }
397 vq->num = s.num;
398 break;
399 case VHOST_SET_VRING_BASE:
400 /* Moving base with an active backend?
401 * You don't want to do that. */
402 if (vq->private_data) {
403 r = -EBUSY;
404 break;
405 }
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900406 if (copy_from_user(&s, argp, sizeof s)) {
407 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000408 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900409 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000410 if (s.num > 0xffff) {
411 r = -EINVAL;
412 break;
413 }
414 vq->last_avail_idx = s.num;
415 /* Forget the cached index value. */
416 vq->avail_idx = vq->last_avail_idx;
417 break;
418 case VHOST_GET_VRING_BASE:
419 s.index = idx;
420 s.num = vq->last_avail_idx;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900421 if (copy_to_user(argp, &s, sizeof s))
422 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000423 break;
424 case VHOST_SET_VRING_ADDR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900425 if (copy_from_user(&a, argp, sizeof a)) {
426 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000427 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900428 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000429 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) {
430 r = -EOPNOTSUPP;
431 break;
432 }
433 /* For 32bit, verify that the top 32bits of the user
434 data are set to zero. */
435 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr ||
436 (u64)(unsigned long)a.used_user_addr != a.used_user_addr ||
437 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) {
438 r = -EFAULT;
439 break;
440 }
441 if ((a.avail_user_addr & (sizeof *vq->avail->ring - 1)) ||
442 (a.used_user_addr & (sizeof *vq->used->ring - 1)) ||
443 (a.log_guest_addr & (sizeof *vq->used->ring - 1))) {
444 r = -EINVAL;
445 break;
446 }
447
448 /* We only verify access here if backend is configured.
449 * If it is not, we don't as size might not have been setup.
450 * We will verify when backend is configured. */
451 if (vq->private_data) {
452 if (!vq_access_ok(vq->num,
453 (void __user *)(unsigned long)a.desc_user_addr,
454 (void __user *)(unsigned long)a.avail_user_addr,
455 (void __user *)(unsigned long)a.used_user_addr)) {
456 r = -EINVAL;
457 break;
458 }
459
460 /* Also validate log access for used ring if enabled. */
461 if ((a.flags & (0x1 << VHOST_VRING_F_LOG)) &&
462 !log_access_ok(vq->log_base, a.log_guest_addr,
463 sizeof *vq->used +
464 vq->num * sizeof *vq->used->ring)) {
465 r = -EINVAL;
466 break;
467 }
468 }
469
470 r = init_used(vq, (struct vring_used __user *)(unsigned long)
471 a.used_user_addr);
472 if (r)
473 break;
474 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
475 vq->desc = (void __user *)(unsigned long)a.desc_user_addr;
476 vq->avail = (void __user *)(unsigned long)a.avail_user_addr;
477 vq->log_addr = a.log_guest_addr;
478 vq->used = (void __user *)(unsigned long)a.used_user_addr;
479 break;
480 case VHOST_SET_VRING_KICK:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900481 if (copy_from_user(&f, argp, sizeof f)) {
482 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000483 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900484 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000485 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200486 if (IS_ERR(eventfp)) {
487 r = PTR_ERR(eventfp);
488 break;
489 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000490 if (eventfp != vq->kick) {
491 pollstop = filep = vq->kick;
492 pollstart = vq->kick = eventfp;
493 } else
494 filep = eventfp;
495 break;
496 case VHOST_SET_VRING_CALL:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900497 if (copy_from_user(&f, argp, sizeof f)) {
498 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000499 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900500 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000501 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200502 if (IS_ERR(eventfp)) {
503 r = PTR_ERR(eventfp);
504 break;
505 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000506 if (eventfp != vq->call) {
507 filep = vq->call;
508 ctx = vq->call_ctx;
509 vq->call = eventfp;
510 vq->call_ctx = eventfp ?
511 eventfd_ctx_fileget(eventfp) : NULL;
512 } else
513 filep = eventfp;
514 break;
515 case VHOST_SET_VRING_ERR:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900516 if (copy_from_user(&f, argp, sizeof f)) {
517 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000518 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900519 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000520 eventfp = f.fd == -1 ? NULL : eventfd_fget(f.fd);
Michael S. Tsirkin535297a2010-03-17 16:06:11 +0200521 if (IS_ERR(eventfp)) {
522 r = PTR_ERR(eventfp);
523 break;
524 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000525 if (eventfp != vq->error) {
526 filep = vq->error;
527 vq->error = eventfp;
528 ctx = vq->error_ctx;
529 vq->error_ctx = eventfp ?
530 eventfd_ctx_fileget(eventfp) : NULL;
531 } else
532 filep = eventfp;
533 break;
534 default:
535 r = -ENOIOCTLCMD;
536 }
537
538 if (pollstop && vq->handle_kick)
539 vhost_poll_stop(&vq->poll);
540
541 if (ctx)
542 eventfd_ctx_put(ctx);
543 if (filep)
544 fput(filep);
545
546 if (pollstart && vq->handle_kick)
547 vhost_poll_start(&vq->poll, vq->kick);
548
549 mutex_unlock(&vq->mutex);
550
551 if (pollstop && vq->handle_kick)
552 vhost_poll_flush(&vq->poll);
553 return r;
554}
555
556/* Caller must have device mutex */
557long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, unsigned long arg)
558{
559 void __user *argp = (void __user *)arg;
560 struct file *eventfp, *filep = NULL;
561 struct eventfd_ctx *ctx = NULL;
562 u64 p;
563 long r;
564 int i, fd;
565
566 /* If you are not the owner, you can become one */
567 if (ioctl == VHOST_SET_OWNER) {
568 r = vhost_dev_set_owner(d);
569 goto done;
570 }
571
572 /* You must be the owner to do anything else */
573 r = vhost_dev_check_owner(d);
574 if (r)
575 goto done;
576
577 switch (ioctl) {
578 case VHOST_SET_MEM_TABLE:
579 r = vhost_set_memory(d, argp);
580 break;
581 case VHOST_SET_LOG_BASE:
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900582 if (copy_from_user(&p, argp, sizeof p)) {
583 r = -EFAULT;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000584 break;
Takuya Yoshikawa7ad9c9d2010-05-27 18:58:03 +0900585 }
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000586 if ((u64)(unsigned long)p != p) {
587 r = -EFAULT;
588 break;
589 }
590 for (i = 0; i < d->nvqs; ++i) {
591 struct vhost_virtqueue *vq;
592 void __user *base = (void __user *)(unsigned long)p;
593 vq = d->vqs + i;
594 mutex_lock(&vq->mutex);
595 /* If ring is inactive, will check when it's enabled. */
596 if (vq->private_data && !vq_log_access_ok(vq, base))
597 r = -EFAULT;
598 else
599 vq->log_base = base;
600 mutex_unlock(&vq->mutex);
601 }
602 break;
603 case VHOST_SET_LOG_FD:
604 r = get_user(fd, (int __user *)argp);
605 if (r < 0)
606 break;
607 eventfp = fd == -1 ? NULL : eventfd_fget(fd);
608 if (IS_ERR(eventfp)) {
609 r = PTR_ERR(eventfp);
610 break;
611 }
612 if (eventfp != d->log_file) {
613 filep = d->log_file;
614 ctx = d->log_ctx;
615 d->log_ctx = eventfp ?
616 eventfd_ctx_fileget(eventfp) : NULL;
617 } else
618 filep = eventfp;
619 for (i = 0; i < d->nvqs; ++i) {
620 mutex_lock(&d->vqs[i].mutex);
621 d->vqs[i].log_ctx = d->log_ctx;
622 mutex_unlock(&d->vqs[i].mutex);
623 }
624 if (ctx)
625 eventfd_ctx_put(ctx);
626 if (filep)
627 fput(filep);
628 break;
629 default:
630 r = vhost_set_vring(d, ioctl, argp);
631 break;
632 }
633done:
634 return r;
635}
636
637static const struct vhost_memory_region *find_region(struct vhost_memory *mem,
638 __u64 addr, __u32 len)
639{
640 struct vhost_memory_region *reg;
641 int i;
642 /* linear search is not brilliant, but we really have on the order of 6
643 * regions in practice */
644 for (i = 0; i < mem->nregions; ++i) {
645 reg = mem->regions + i;
646 if (reg->guest_phys_addr <= addr &&
647 reg->guest_phys_addr + reg->memory_size - 1 >= addr)
648 return reg;
649 }
650 return NULL;
651}
652
653/* TODO: This is really inefficient. We need something like get_user()
654 * (instruction directly accesses the data, with an exception table entry
655 * returning -EFAULT). See Documentation/x86/exception-tables.txt.
656 */
657static int set_bit_to_user(int nr, void __user *addr)
658{
659 unsigned long log = (unsigned long)addr;
660 struct page *page;
661 void *base;
662 int bit = nr + (log % PAGE_SIZE) * 8;
663 int r;
664 r = get_user_pages_fast(log, 1, 1, &page);
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200665 if (r < 0)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000666 return r;
Michael S. Tsirkind6db3f52010-02-23 11:25:23 +0200667 BUG_ON(r != 1);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000668 base = kmap_atomic(page, KM_USER0);
669 set_bit(bit, base);
670 kunmap_atomic(base, KM_USER0);
671 set_page_dirty_lock(page);
672 put_page(page);
673 return 0;
674}
675
676static int log_write(void __user *log_base,
677 u64 write_address, u64 write_length)
678{
679 int r;
680 if (!write_length)
681 return 0;
682 write_address /= VHOST_PAGE_SIZE;
683 for (;;) {
684 u64 base = (u64)(unsigned long)log_base;
685 u64 log = base + write_address / 8;
686 int bit = write_address % 8;
687 if ((u64)(unsigned long)log != log)
688 return -EFAULT;
689 r = set_bit_to_user(bit, (void __user *)(unsigned long)log);
690 if (r < 0)
691 return r;
692 if (write_length <= VHOST_PAGE_SIZE)
693 break;
694 write_length -= VHOST_PAGE_SIZE;
695 write_address += VHOST_PAGE_SIZE;
696 }
697 return r;
698}
699
700int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
701 unsigned int log_num, u64 len)
702{
703 int i, r;
704
705 /* Make sure data written is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000706 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000707 for (i = 0; i < log_num; ++i) {
708 u64 l = min(log[i].len, len);
709 r = log_write(vq->log_base, log[i].addr, l);
710 if (r < 0)
711 return r;
712 len -= l;
713 if (!len)
714 return 0;
715 }
716 if (vq->log_ctx)
717 eventfd_signal(vq->log_ctx, 1);
718 /* Length written exceeds what we have stored. This is a bug. */
719 BUG();
720 return 0;
721}
722
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400723static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
724 struct iovec iov[], int iov_size)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000725{
726 const struct vhost_memory_region *reg;
727 struct vhost_memory *mem;
728 struct iovec *_iov;
729 u64 s = 0;
730 int ret = 0;
731
732 rcu_read_lock();
733
734 mem = rcu_dereference(dev->memory);
735 while ((u64)len > s) {
736 u64 size;
737 if (ret >= iov_size) {
738 ret = -ENOBUFS;
739 break;
740 }
741 reg = find_region(mem, addr, len);
742 if (!reg) {
743 ret = -EFAULT;
744 break;
745 }
746 _iov = iov + ret;
747 size = reg->memory_size - addr + reg->guest_phys_addr;
748 _iov->iov_len = min((u64)len, size);
Christoph Hellwiga8d37822010-04-13 14:11:25 -0400749 _iov->iov_base = (void __user *)(unsigned long)
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000750 (reg->userspace_addr + addr - reg->guest_phys_addr);
751 s += size;
752 addr += size;
753 ++ret;
754 }
755
756 rcu_read_unlock();
757 return ret;
758}
759
760/* Each buffer in the virtqueues is actually a chain of descriptors. This
761 * function returns the next descriptor in the chain,
762 * or -1U if we're at the end. */
763static unsigned next_desc(struct vring_desc *desc)
764{
765 unsigned int next;
766
767 /* If this descriptor says it doesn't chain, we're done. */
768 if (!(desc->flags & VRING_DESC_F_NEXT))
769 return -1U;
770
771 /* Check they're not leading us off end of descriptors. */
772 next = desc->next;
773 /* Make sure compiler knows to grab that: we don't want it changing! */
774 /* We will use the result as an index in an array, so most
775 * architectures only need a compiler barrier here. */
776 read_barrier_depends();
777
778 return next;
779}
780
781static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
782 struct iovec iov[], unsigned int iov_size,
783 unsigned int *out_num, unsigned int *in_num,
784 struct vhost_log *log, unsigned int *log_num,
785 struct vring_desc *indirect)
786{
787 struct vring_desc desc;
788 unsigned int i = 0, count, found = 0;
789 int ret;
790
791 /* Sanity check */
792 if (indirect->len % sizeof desc) {
793 vq_err(vq, "Invalid length in indirect descriptor: "
794 "len 0x%llx not multiple of 0x%zx\n",
795 (unsigned long long)indirect->len,
796 sizeof desc);
797 return -EINVAL;
798 }
799
800 ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
801 ARRAY_SIZE(vq->indirect));
802 if (ret < 0) {
803 vq_err(vq, "Translation failure %d in indirect.\n", ret);
804 return ret;
805 }
806
807 /* We will use the result as an address to read from, so most
808 * architectures only need a compiler barrier here. */
809 read_barrier_depends();
810
811 count = indirect->len / sizeof desc;
812 /* Buffers are chained via a 16 bit next field, so
813 * we can have at most 2^16 of these. */
814 if (count > USHORT_MAX + 1) {
815 vq_err(vq, "Indirect buffer length too big: %d\n",
816 indirect->len);
817 return -E2BIG;
818 }
819
820 do {
821 unsigned iov_count = *in_num + *out_num;
822 if (++found > count) {
823 vq_err(vq, "Loop detected: last one at %u "
824 "indirect size %u\n",
825 i, count);
826 return -EINVAL;
827 }
828 if (memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
829 sizeof desc)) {
830 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
831 i, (size_t)indirect->addr + i * sizeof desc);
832 return -EINVAL;
833 }
834 if (desc.flags & VRING_DESC_F_INDIRECT) {
835 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
836 i, (size_t)indirect->addr + i * sizeof desc);
837 return -EINVAL;
838 }
839
840 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
841 iov_size - iov_count);
842 if (ret < 0) {
843 vq_err(vq, "Translation failure %d indirect idx %d\n",
844 ret, i);
845 return ret;
846 }
847 /* If this is an input descriptor, increment that count. */
848 if (desc.flags & VRING_DESC_F_WRITE) {
849 *in_num += ret;
850 if (unlikely(log)) {
851 log[*log_num].addr = desc.addr;
852 log[*log_num].len = desc.len;
853 ++*log_num;
854 }
855 } else {
856 /* If it's an output descriptor, they're all supposed
857 * to come before any input descriptors. */
858 if (*in_num) {
859 vq_err(vq, "Indirect descriptor "
860 "has out after in: idx %d\n", i);
861 return -EINVAL;
862 }
863 *out_num += ret;
864 }
865 } while ((i = next_desc(&desc)) != -1);
866 return 0;
867}
868
869/* This looks in the virtqueue and for the first available buffer, and converts
870 * it to an iovec for convenient access. Since descriptors consist of some
871 * number of output then some number of input descriptors, it's actually two
872 * iovecs, but we pack them into one and note how many of each there were.
873 *
874 * This function returns the descriptor number found, or vq->num (which
875 * is never a valid descriptor number) if none was found. */
876unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
877 struct iovec iov[], unsigned int iov_size,
878 unsigned int *out_num, unsigned int *in_num,
879 struct vhost_log *log, unsigned int *log_num)
880{
881 struct vring_desc desc;
882 unsigned int i, head, found = 0;
883 u16 last_avail_idx;
884 int ret;
885
886 /* Check it isn't doing very strange things with descriptor numbers. */
887 last_avail_idx = vq->last_avail_idx;
888 if (get_user(vq->avail_idx, &vq->avail->idx)) {
889 vq_err(vq, "Failed to access avail idx at %p\n",
890 &vq->avail->idx);
891 return vq->num;
892 }
893
894 if ((u16)(vq->avail_idx - last_avail_idx) > vq->num) {
895 vq_err(vq, "Guest moved used index from %u to %u",
896 last_avail_idx, vq->avail_idx);
897 return vq->num;
898 }
899
900 /* If there's nothing new since last we looked, return invalid. */
901 if (vq->avail_idx == last_avail_idx)
902 return vq->num;
903
904 /* Only get avail ring entries after they have been exposed by guest. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +0000905 smp_rmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +0000906
907 /* Grab the next descriptor number they're advertising, and increment
908 * the index we've seen. */
909 if (get_user(head, &vq->avail->ring[last_avail_idx % vq->num])) {
910 vq_err(vq, "Failed to read head: idx %d address %p\n",
911 last_avail_idx,
912 &vq->avail->ring[last_avail_idx % vq->num]);
913 return vq->num;
914 }
915
916 /* If their number is silly, that's an error. */
917 if (head >= vq->num) {
918 vq_err(vq, "Guest says index %u > %u is available",
919 head, vq->num);
920 return vq->num;
921 }
922
923 /* When we start there are none of either input nor output. */
924 *out_num = *in_num = 0;
925 if (unlikely(log))
926 *log_num = 0;
927
928 i = head;
929 do {
930 unsigned iov_count = *in_num + *out_num;
931 if (i >= vq->num) {
932 vq_err(vq, "Desc index is %u > %u, head = %u",
933 i, vq->num, head);
934 return vq->num;
935 }
936 if (++found > vq->num) {
937 vq_err(vq, "Loop detected: last one at %u "
938 "vq size %u head %u\n",
939 i, vq->num, head);
940 return vq->num;
941 }
942 ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
943 if (ret) {
944 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
945 i, vq->desc + i);
946 return vq->num;
947 }
948 if (desc.flags & VRING_DESC_F_INDIRECT) {
949 ret = get_indirect(dev, vq, iov, iov_size,
950 out_num, in_num,
951 log, log_num, &desc);
952 if (ret < 0) {
953 vq_err(vq, "Failure detected "
954 "in indirect descriptor at idx %d\n", i);
955 return vq->num;
956 }
957 continue;
958 }
959
960 ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
961 iov_size - iov_count);
962 if (ret < 0) {
963 vq_err(vq, "Translation failure %d descriptor idx %d\n",
964 ret, i);
965 return vq->num;
966 }
967 if (desc.flags & VRING_DESC_F_WRITE) {
968 /* If this is an input descriptor,
969 * increment that count. */
970 *in_num += ret;
971 if (unlikely(log)) {
972 log[*log_num].addr = desc.addr;
973 log[*log_num].len = desc.len;
974 ++*log_num;
975 }
976 } else {
977 /* If it's an output descriptor, they're all supposed
978 * to come before any input descriptors. */
979 if (*in_num) {
980 vq_err(vq, "Descriptor has out after in: "
981 "idx %d\n", i);
982 return vq->num;
983 }
984 *out_num += ret;
985 }
986 } while ((i = next_desc(&desc)) != -1);
987
988 /* On success, increment avail index. */
989 vq->last_avail_idx++;
990 return head;
991}
992
993/* Reverse the effect of vhost_get_vq_desc. Useful for error handling. */
994void vhost_discard_vq_desc(struct vhost_virtqueue *vq)
995{
996 vq->last_avail_idx--;
997}
998
999/* After we've used one of their buffers, we tell them about it. We'll then
1000 * want to notify the guest, using eventfd. */
1001int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len)
1002{
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001003 struct vring_used_elem __user *used;
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001004
1005 /* The virtqueue contains a ring of used buffers. Get a pointer to the
1006 * next entry in that used ring. */
1007 used = &vq->used->ring[vq->last_used_idx % vq->num];
1008 if (put_user(head, &used->id)) {
1009 vq_err(vq, "Failed to write used id");
1010 return -EFAULT;
1011 }
1012 if (put_user(len, &used->len)) {
1013 vq_err(vq, "Failed to write used len");
1014 return -EFAULT;
1015 }
1016 /* Make sure buffer is written before we update index. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001017 smp_wmb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001018 if (put_user(vq->last_used_idx + 1, &vq->used->idx)) {
1019 vq_err(vq, "Failed to increment used idx");
1020 return -EFAULT;
1021 }
1022 if (unlikely(vq->log_used)) {
1023 /* Make sure data is seen before log. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001024 smp_wmb();
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001025 /* Log used ring entry write. */
1026 log_write(vq->log_base,
Christoph Hellwiga8d37822010-04-13 14:11:25 -04001027 vq->log_addr +
1028 ((void __user *)used - (void __user *)vq->used),
Michael S. Tsirkin86e94242010-02-17 19:11:33 +02001029 sizeof *used);
1030 /* Log used index update. */
1031 log_write(vq->log_base,
1032 vq->log_addr + offsetof(struct vring_used, idx),
1033 sizeof vq->used->idx);
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001034 if (vq->log_ctx)
1035 eventfd_signal(vq->log_ctx, 1);
1036 }
1037 vq->last_used_idx++;
1038 return 0;
1039}
1040
1041/* This actually signals the guest, using eventfd. */
1042void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq)
1043{
Michael S. Tsirkin0d499352010-05-11 19:44:17 +03001044 __u16 flags;
1045 /* Flush out used index updates. This is paired
1046 * with the barrier that the Guest executes when enabling
1047 * interrupts. */
1048 smp_mb();
1049
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001050 if (get_user(flags, &vq->avail->flags)) {
1051 vq_err(vq, "Failed to get flags");
1052 return;
1053 }
1054
1055 /* If they don't want an interrupt, don't signal, unless empty. */
1056 if ((flags & VRING_AVAIL_F_NO_INTERRUPT) &&
1057 (vq->avail_idx != vq->last_avail_idx ||
1058 !vhost_has_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY)))
1059 return;
1060
1061 /* Signal the Guest tell them we used something up. */
1062 if (vq->call_ctx)
1063 eventfd_signal(vq->call_ctx, 1);
1064}
1065
1066/* And here's the combo meal deal. Supersize me! */
1067void vhost_add_used_and_signal(struct vhost_dev *dev,
1068 struct vhost_virtqueue *vq,
1069 unsigned int head, int len)
1070{
1071 vhost_add_used(vq, head, len);
1072 vhost_signal(dev, vq);
1073}
1074
1075/* OK, now we need to know about added descriptors. */
1076bool vhost_enable_notify(struct vhost_virtqueue *vq)
1077{
1078 u16 avail_idx;
1079 int r;
1080 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY))
1081 return false;
1082 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY;
1083 r = put_user(vq->used_flags, &vq->used->flags);
1084 if (r) {
1085 vq_err(vq, "Failed to enable notification at %p: %d\n",
1086 &vq->used->flags, r);
1087 return false;
1088 }
1089 /* They could have slipped one in as we were doing that: make
1090 * sure it's written, then check again. */
Michael S. Tsirkin56593382010-02-01 07:21:02 +00001091 smp_mb();
Michael S. Tsirkin3a4d5c92010-01-14 06:17:27 +00001092 r = get_user(avail_idx, &vq->avail->idx);
1093 if (r) {
1094 vq_err(vq, "Failed to check avail idx at %p: %d\n",
1095 &vq->avail->idx, r);
1096 return false;
1097 }
1098
1099 return avail_idx != vq->last_avail_idx;
1100}
1101
1102/* We don't need to be notified again. */
1103void vhost_disable_notify(struct vhost_virtqueue *vq)
1104{
1105 int r;
1106 if (vq->used_flags & VRING_USED_F_NO_NOTIFY)
1107 return;
1108 vq->used_flags |= VRING_USED_F_NO_NOTIFY;
1109 r = put_user(vq->used_flags, &vq->used->flags);
1110 if (r)
1111 vq_err(vq, "Failed to enable notification at %p: %d\n",
1112 &vq->used->flags, r);
1113}
1114
1115int vhost_init(void)
1116{
1117 vhost_workqueue = create_singlethread_workqueue("vhost");
1118 if (!vhost_workqueue)
1119 return -ENOMEM;
1120 return 0;
1121}
1122
1123void vhost_cleanup(void)
1124{
1125 destroy_workqueue(vhost_workqueue);
1126}