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