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