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