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