1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (C) 2009 Red Hat, Inc. 3 * Copyright (C) 2006 Rusty Russell IBM Corporation 4 * 5 * Author: Michael S. Tsirkin <mst@redhat.com> 6 * 7 * Inspiration, some code, and most witty comments come from 8 * Documentation/virtual/lguest/lguest.c, by Rusty Russell 9 * 10 * Generic code for virtio server in host kernel. 11 */ 12 13 #include <linux/eventfd.h> 14 #include <linux/vhost.h> 15 #include <linux/uio.h> 16 #include <linux/mm.h> 17 #include <linux/miscdevice.h> 18 #include <linux/mutex.h> 19 #include <linux/poll.h> 20 #include <linux/file.h> 21 #include <linux/highmem.h> 22 #include <linux/slab.h> 23 #include <linux/vmalloc.h> 24 #include <linux/kthread.h> 25 #include <linux/cgroup.h> 26 #include <linux/module.h> 27 #include <linux/sort.h> 28 #include <linux/sched/mm.h> 29 #include <linux/sched/signal.h> 30 #include <linux/sched/vhost_task.h> 31 #include <linux/interval_tree_generic.h> 32 #include <linux/nospec.h> 33 #include <linux/kcov.h> 34 35 #include "vhost.h" 36 37 static ushort max_mem_regions = 64; 38 module_param(max_mem_regions, ushort, 0444); 39 MODULE_PARM_DESC(max_mem_regions, 40 "Maximum number of memory regions in memory map. (default: 64)"); 41 static int max_iotlb_entries = 2048; 42 module_param(max_iotlb_entries, int, 0444); 43 MODULE_PARM_DESC(max_iotlb_entries, 44 "Maximum number of iotlb entries. (default: 2048)"); 45 static bool fork_from_owner_default = VHOST_FORK_OWNER_TASK; 46 47 #ifdef CONFIG_VHOST_ENABLE_FORK_OWNER_CONTROL 48 module_param(fork_from_owner_default, bool, 0444); 49 MODULE_PARM_DESC(fork_from_owner_default, 50 "Set task mode as the default(default: Y)"); 51 #endif 52 53 enum { 54 VHOST_MEMORY_F_LOG = 0x1, 55 }; 56 57 #define vhost_used_event(vq) ((__virtio16 __user *)&vq->avail->ring[vq->num]) 58 #define vhost_avail_event(vq) ((__virtio16 __user *)&vq->used->ring[vq->num]) 59 60 #ifdef CONFIG_VHOST_CROSS_ENDIAN_LEGACY 61 static void vhost_disable_cross_endian(struct vhost_virtqueue *vq) 62 { 63 vq->user_be = !virtio_legacy_is_little_endian(); 64 } 65 66 static void vhost_enable_cross_endian_big(struct vhost_virtqueue *vq) 67 { 68 vq->user_be = true; 69 } 70 71 static void vhost_enable_cross_endian_little(struct vhost_virtqueue *vq) 72 { 73 vq->user_be = false; 74 } 75 76 static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) 77 { 78 struct vhost_vring_state s; 79 80 if (vq->private_data) 81 return -EBUSY; 82 83 if (copy_from_user(&s, argp, sizeof(s))) 84 return -EFAULT; 85 86 if (s.num != VHOST_VRING_LITTLE_ENDIAN && 87 s.num != VHOST_VRING_BIG_ENDIAN) 88 return -EINVAL; 89 90 if (s.num == VHOST_VRING_BIG_ENDIAN) 91 vhost_enable_cross_endian_big(vq); 92 else 93 vhost_enable_cross_endian_little(vq); 94 95 return 0; 96 } 97 98 static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, 99 int __user *argp) 100 { 101 struct vhost_vring_state s = { 102 .index = idx, 103 .num = vq->user_be 104 }; 105 106 if (copy_to_user(argp, &s, sizeof(s))) 107 return -EFAULT; 108 109 return 0; 110 } 111 112 static void vhost_init_is_le(struct vhost_virtqueue *vq) 113 { 114 /* Note for legacy virtio: user_be is initialized at reset time 115 * according to the host endianness. If userspace does not set an 116 * explicit endianness, the default behavior is native endian, as 117 * expected by legacy virtio. 118 */ 119 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) || !vq->user_be; 120 } 121 #else 122 static void vhost_disable_cross_endian(struct vhost_virtqueue *vq) 123 { 124 } 125 126 static long vhost_set_vring_endian(struct vhost_virtqueue *vq, int __user *argp) 127 { 128 return -ENOIOCTLCMD; 129 } 130 131 static long vhost_get_vring_endian(struct vhost_virtqueue *vq, u32 idx, 132 int __user *argp) 133 { 134 return -ENOIOCTLCMD; 135 } 136 137 static void vhost_init_is_le(struct vhost_virtqueue *vq) 138 { 139 vq->is_le = vhost_has_feature(vq, VIRTIO_F_VERSION_1) 140 || virtio_legacy_is_little_endian(); 141 } 142 #endif /* CONFIG_VHOST_CROSS_ENDIAN_LEGACY */ 143 144 static void vhost_reset_is_le(struct vhost_virtqueue *vq) 145 { 146 vhost_init_is_le(vq); 147 } 148 149 struct vhost_flush_struct { 150 struct vhost_work work; 151 struct completion wait_event; 152 }; 153 154 static void vhost_flush_work(struct vhost_work *work) 155 { 156 struct vhost_flush_struct *s; 157 158 s = container_of(work, struct vhost_flush_struct, work); 159 complete(&s->wait_event); 160 } 161 162 static void vhost_poll_func(struct file *file, wait_queue_head_t *wqh, 163 poll_table *pt) 164 { 165 struct vhost_poll *poll; 166 167 poll = container_of(pt, struct vhost_poll, table); 168 poll->wqh = wqh; 169 add_wait_queue(wqh, &poll->wait); 170 } 171 172 static int vhost_poll_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, 173 void *key) 174 { 175 struct vhost_poll *poll = container_of(wait, struct vhost_poll, wait); 176 struct vhost_work *work = &poll->work; 177 178 if (!(key_to_poll(key) & poll->mask)) 179 return 0; 180 181 if (!poll->dev->use_worker) 182 work->fn(work); 183 else 184 vhost_poll_queue(poll); 185 186 return 0; 187 } 188 189 void vhost_work_init(struct vhost_work *work, vhost_work_fn_t fn) 190 { 191 clear_bit(VHOST_WORK_QUEUED, &work->flags); 192 work->fn = fn; 193 } 194 EXPORT_SYMBOL_GPL(vhost_work_init); 195 196 /* Init poll structure */ 197 void vhost_poll_init(struct vhost_poll *poll, vhost_work_fn_t fn, 198 __poll_t mask, struct vhost_dev *dev, 199 struct vhost_virtqueue *vq) 200 { 201 init_waitqueue_func_entry(&poll->wait, vhost_poll_wakeup); 202 init_poll_funcptr(&poll->table, vhost_poll_func); 203 poll->mask = mask; 204 poll->dev = dev; 205 poll->wqh = NULL; 206 poll->vq = vq; 207 208 vhost_work_init(&poll->work, fn); 209 } 210 EXPORT_SYMBOL_GPL(vhost_poll_init); 211 212 /* Start polling a file. We add ourselves to file's wait queue. The caller must 213 * keep a reference to a file until after vhost_poll_stop is called. */ 214 int vhost_poll_start(struct vhost_poll *poll, struct file *file) 215 { 216 __poll_t mask; 217 218 if (poll->wqh) 219 return 0; 220 221 mask = vfs_poll(file, &poll->table); 222 if (mask) 223 vhost_poll_wakeup(&poll->wait, 0, 0, poll_to_key(mask)); 224 if (mask & EPOLLERR) { 225 vhost_poll_stop(poll); 226 return -EINVAL; 227 } 228 229 return 0; 230 } 231 EXPORT_SYMBOL_GPL(vhost_poll_start); 232 233 /* Stop polling a file. After this function returns, it becomes safe to drop the 234 * file reference. You must also flush afterwards. */ 235 void vhost_poll_stop(struct vhost_poll *poll) 236 { 237 if (poll->wqh) { 238 remove_wait_queue(poll->wqh, &poll->wait); 239 poll->wqh = NULL; 240 } 241 } 242 EXPORT_SYMBOL_GPL(vhost_poll_stop); 243 244 static void vhost_worker_queue(struct vhost_worker *worker, 245 struct vhost_work *work) 246 { 247 if (!test_and_set_bit(VHOST_WORK_QUEUED, &work->flags)) { 248 /* We can only add the work to the list after we're 249 * sure it was not in the list. 250 * test_and_set_bit() implies a memory barrier. 251 */ 252 llist_add(&work->node, &worker->work_list); 253 worker->ops->wakeup(worker); 254 } 255 } 256 257 bool vhost_vq_work_queue(struct vhost_virtqueue *vq, struct vhost_work *work) 258 { 259 struct vhost_worker *worker; 260 bool queued = false; 261 262 rcu_read_lock(); 263 worker = rcu_dereference(vq->worker); 264 if (worker) { 265 queued = true; 266 vhost_worker_queue(worker, work); 267 } 268 rcu_read_unlock(); 269 270 return queued; 271 } 272 EXPORT_SYMBOL_GPL(vhost_vq_work_queue); 273 274 /** 275 * __vhost_worker_flush - flush a worker 276 * @worker: worker to flush 277 * 278 * The worker's flush_mutex must be held. 279 */ 280 static void __vhost_worker_flush(struct vhost_worker *worker) 281 { 282 struct vhost_flush_struct flush; 283 284 if (!worker->attachment_cnt || worker->killed) 285 return; 286 287 init_completion(&flush.wait_event); 288 vhost_work_init(&flush.work, vhost_flush_work); 289 290 vhost_worker_queue(worker, &flush.work); 291 /* 292 * Drop mutex in case our worker is killed and it needs to take the 293 * mutex to force cleanup. 294 */ 295 mutex_unlock(&worker->mutex); 296 wait_for_completion(&flush.wait_event); 297 mutex_lock(&worker->mutex); 298 } 299 300 static void vhost_worker_flush(struct vhost_worker *worker) 301 { 302 mutex_lock(&worker->mutex); 303 __vhost_worker_flush(worker); 304 mutex_unlock(&worker->mutex); 305 } 306 307 void vhost_dev_flush(struct vhost_dev *dev) 308 { 309 struct vhost_worker *worker; 310 unsigned long i; 311 312 xa_for_each(&dev->worker_xa, i, worker) 313 vhost_worker_flush(worker); 314 } 315 EXPORT_SYMBOL_GPL(vhost_dev_flush); 316 317 /* A lockless hint for busy polling code to exit the loop */ 318 bool vhost_vq_has_work(struct vhost_virtqueue *vq) 319 { 320 struct vhost_worker *worker; 321 bool has_work = false; 322 323 rcu_read_lock(); 324 worker = rcu_dereference(vq->worker); 325 if (worker && !llist_empty(&worker->work_list)) 326 has_work = true; 327 rcu_read_unlock(); 328 329 return has_work; 330 } 331 EXPORT_SYMBOL_GPL(vhost_vq_has_work); 332 333 void vhost_poll_queue(struct vhost_poll *poll) 334 { 335 vhost_vq_work_queue(poll->vq, &poll->work); 336 } 337 EXPORT_SYMBOL_GPL(vhost_poll_queue); 338 339 static void __vhost_vq_meta_reset(struct vhost_virtqueue *vq) 340 { 341 int j; 342 343 for (j = 0; j < VHOST_NUM_ADDRS; j++) 344 vq->meta_iotlb[j] = NULL; 345 } 346 347 static void vhost_vq_meta_reset(struct vhost_dev *d) 348 { 349 int i; 350 351 for (i = 0; i < d->nvqs; ++i) 352 __vhost_vq_meta_reset(d->vqs[i]); 353 } 354 355 static void vhost_vring_call_reset(struct vhost_vring_call *call_ctx) 356 { 357 call_ctx->ctx = NULL; 358 memset(&call_ctx->producer, 0x0, sizeof(struct irq_bypass_producer)); 359 } 360 361 bool vhost_vq_is_setup(struct vhost_virtqueue *vq) 362 { 363 return vq->avail && vq->desc && vq->used && vhost_vq_access_ok(vq); 364 } 365 EXPORT_SYMBOL_GPL(vhost_vq_is_setup); 366 367 static void vhost_vq_reset(struct vhost_dev *dev, 368 struct vhost_virtqueue *vq) 369 { 370 vq->num = 1; 371 vq->desc = NULL; 372 vq->avail = NULL; 373 vq->used = NULL; 374 vq->last_avail_idx = 0; 375 vq->next_avail_head = 0; 376 vq->avail_idx = 0; 377 vq->last_used_idx = 0; 378 vq->signalled_used = 0; 379 vq->signalled_used_valid = false; 380 vq->used_flags = 0; 381 vq->log_used = false; 382 vq->log_addr = -1ull; 383 vq->private_data = NULL; 384 virtio_features_zero(vq->acked_features_array); 385 vq->acked_backend_features = 0; 386 vq->log_base = NULL; 387 vq->error_ctx = NULL; 388 vq->kick = NULL; 389 vq->log_ctx = NULL; 390 vhost_disable_cross_endian(vq); 391 vhost_reset_is_le(vq); 392 vq->busyloop_timeout = 0; 393 vq->umem = NULL; 394 vq->iotlb = NULL; 395 rcu_assign_pointer(vq->worker, NULL); 396 vhost_vring_call_reset(&vq->call_ctx); 397 __vhost_vq_meta_reset(vq); 398 } 399 400 static int vhost_run_work_kthread_list(void *data) 401 { 402 struct vhost_worker *worker = data; 403 struct vhost_work *work, *work_next; 404 struct vhost_dev *dev = worker->dev; 405 struct llist_node *node; 406 407 kthread_use_mm(dev->mm); 408 409 for (;;) { 410 /* mb paired w/ kthread_stop */ 411 set_current_state(TASK_INTERRUPTIBLE); 412 413 if (kthread_should_stop()) { 414 __set_current_state(TASK_RUNNING); 415 break; 416 } 417 node = llist_del_all(&worker->work_list); 418 if (!node) 419 schedule(); 420 421 node = llist_reverse_order(node); 422 /* make sure flag is seen after deletion */ 423 smp_wmb(); 424 llist_for_each_entry_safe(work, work_next, node, node) { 425 clear_bit(VHOST_WORK_QUEUED, &work->flags); 426 __set_current_state(TASK_RUNNING); 427 kcov_remote_start_common(worker->kcov_handle); 428 work->fn(work); 429 kcov_remote_stop(); 430 cond_resched(); 431 } 432 } 433 kthread_unuse_mm(dev->mm); 434 435 return 0; 436 } 437 438 static bool vhost_run_work_list(void *data) 439 { 440 struct vhost_worker *worker = data; 441 struct vhost_work *work, *work_next; 442 struct llist_node *node; 443 444 node = llist_del_all(&worker->work_list); 445 if (node) { 446 __set_current_state(TASK_RUNNING); 447 448 node = llist_reverse_order(node); 449 /* make sure flag is seen after deletion */ 450 smp_wmb(); 451 llist_for_each_entry_safe(work, work_next, node, node) { 452 clear_bit(VHOST_WORK_QUEUED, &work->flags); 453 kcov_remote_start_common(worker->kcov_handle); 454 work->fn(work); 455 kcov_remote_stop(); 456 cond_resched(); 457 } 458 } 459 460 return !!node; 461 } 462 463 static void vhost_worker_killed(void *data) 464 { 465 struct vhost_worker *worker = data; 466 struct vhost_dev *dev = worker->dev; 467 struct vhost_virtqueue *vq; 468 int i, attach_cnt = 0; 469 470 mutex_lock(&worker->mutex); 471 worker->killed = true; 472 473 for (i = 0; i < dev->nvqs; i++) { 474 vq = dev->vqs[i]; 475 476 mutex_lock(&vq->mutex); 477 if (worker == 478 rcu_dereference_check(vq->worker, 479 lockdep_is_held(&vq->mutex))) { 480 rcu_assign_pointer(vq->worker, NULL); 481 attach_cnt++; 482 } 483 mutex_unlock(&vq->mutex); 484 } 485 486 worker->attachment_cnt -= attach_cnt; 487 if (attach_cnt) 488 synchronize_rcu(); 489 /* 490 * Finish vhost_worker_flush calls and any other works that snuck in 491 * before the synchronize_rcu. 492 */ 493 vhost_run_work_list(worker); 494 mutex_unlock(&worker->mutex); 495 } 496 497 static void vhost_vq_free_iovecs(struct vhost_virtqueue *vq) 498 { 499 kfree(vq->indirect); 500 vq->indirect = NULL; 501 kfree(vq->log); 502 vq->log = NULL; 503 kfree(vq->heads); 504 vq->heads = NULL; 505 kfree(vq->nheads); 506 vq->nheads = NULL; 507 } 508 509 /* Helper to allocate iovec buffers for all vqs. */ 510 static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) 511 { 512 struct vhost_virtqueue *vq; 513 int i; 514 515 for (i = 0; i < dev->nvqs; ++i) { 516 vq = dev->vqs[i]; 517 vq->indirect = kmalloc_objs(*vq->indirect, UIO_MAXIOV, 518 GFP_KERNEL); 519 vq->log = kmalloc_objs(*vq->log, dev->iov_limit, GFP_KERNEL); 520 vq->heads = kmalloc_objs(*vq->heads, dev->iov_limit, GFP_KERNEL); 521 vq->nheads = kmalloc_array(dev->iov_limit, sizeof(*vq->nheads), 522 GFP_KERNEL); 523 if (!vq->indirect || !vq->log || !vq->heads || !vq->nheads) 524 goto err_nomem; 525 } 526 return 0; 527 528 err_nomem: 529 for (; i >= 0; --i) 530 vhost_vq_free_iovecs(dev->vqs[i]); 531 return -ENOMEM; 532 } 533 534 static void vhost_dev_free_iovecs(struct vhost_dev *dev) 535 { 536 int i; 537 538 for (i = 0; i < dev->nvqs; ++i) 539 vhost_vq_free_iovecs(dev->vqs[i]); 540 } 541 542 bool vhost_exceeds_weight(struct vhost_virtqueue *vq, 543 int pkts, int total_len) 544 { 545 struct vhost_dev *dev = vq->dev; 546 547 if ((dev->byte_weight && total_len >= dev->byte_weight) || 548 pkts >= dev->weight) { 549 vhost_poll_queue(&vq->poll); 550 return true; 551 } 552 553 return false; 554 } 555 EXPORT_SYMBOL_GPL(vhost_exceeds_weight); 556 557 static size_t vhost_get_avail_size(struct vhost_virtqueue *vq, 558 unsigned int num) 559 { 560 size_t event __maybe_unused = 561 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; 562 563 return size_add(struct_size(vq->avail, ring, num), event); 564 } 565 566 static size_t vhost_get_used_size(struct vhost_virtqueue *vq, 567 unsigned int num) 568 { 569 size_t event __maybe_unused = 570 vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; 571 572 return size_add(struct_size(vq->used, ring, num), event); 573 } 574 575 static size_t vhost_get_desc_size(struct vhost_virtqueue *vq, 576 unsigned int num) 577 { 578 return sizeof(*vq->desc) * num; 579 } 580 581 void vhost_dev_init(struct vhost_dev *dev, 582 struct vhost_virtqueue **vqs, int nvqs, 583 int iov_limit, int weight, int byte_weight, 584 bool use_worker, 585 int (*msg_handler)(struct vhost_dev *dev, u32 asid, 586 struct vhost_iotlb_msg *msg)) 587 { 588 struct vhost_virtqueue *vq; 589 int i; 590 591 dev->vqs = vqs; 592 dev->nvqs = nvqs; 593 mutex_init(&dev->mutex); 594 dev->log_ctx = NULL; 595 dev->umem = NULL; 596 dev->iotlb = NULL; 597 dev->mm = NULL; 598 dev->iov_limit = iov_limit; 599 dev->weight = weight; 600 dev->byte_weight = byte_weight; 601 dev->use_worker = use_worker; 602 dev->msg_handler = msg_handler; 603 dev->fork_owner = fork_from_owner_default; 604 init_waitqueue_head(&dev->wait); 605 INIT_LIST_HEAD(&dev->read_list); 606 INIT_LIST_HEAD(&dev->pending_list); 607 spin_lock_init(&dev->iotlb_lock); 608 xa_init_flags(&dev->worker_xa, XA_FLAGS_ALLOC); 609 610 for (i = 0; i < dev->nvqs; ++i) { 611 vq = dev->vqs[i]; 612 vq->log = NULL; 613 vq->indirect = NULL; 614 vq->heads = NULL; 615 vq->nheads = NULL; 616 vq->dev = dev; 617 mutex_init(&vq->mutex); 618 vhost_vq_reset(dev, vq); 619 if (vq->handle_kick) 620 vhost_poll_init(&vq->poll, vq->handle_kick, 621 EPOLLIN, dev, vq); 622 } 623 } 624 EXPORT_SYMBOL_GPL(vhost_dev_init); 625 626 /* Caller should have device mutex */ 627 long vhost_dev_check_owner(struct vhost_dev *dev) 628 { 629 /* Are you the owner? If not, I don't think you mean to do that */ 630 return dev->mm == current->mm ? 0 : -EPERM; 631 } 632 EXPORT_SYMBOL_GPL(vhost_dev_check_owner); 633 634 struct vhost_attach_cgroups_struct { 635 struct vhost_work work; 636 struct task_struct *owner; 637 int ret; 638 }; 639 640 static void vhost_attach_cgroups_work(struct vhost_work *work) 641 { 642 struct vhost_attach_cgroups_struct *s; 643 644 s = container_of(work, struct vhost_attach_cgroups_struct, work); 645 s->ret = cgroup_attach_task_all(s->owner, current); 646 } 647 648 static int vhost_attach_task_to_cgroups(struct vhost_worker *worker) 649 { 650 struct vhost_attach_cgroups_struct attach; 651 int saved_cnt; 652 653 attach.owner = current; 654 655 vhost_work_init(&attach.work, vhost_attach_cgroups_work); 656 vhost_worker_queue(worker, &attach.work); 657 658 mutex_lock(&worker->mutex); 659 660 /* 661 * Bypass attachment_cnt check in __vhost_worker_flush: 662 * Temporarily change it to INT_MAX to bypass the check 663 */ 664 saved_cnt = worker->attachment_cnt; 665 worker->attachment_cnt = INT_MAX; 666 __vhost_worker_flush(worker); 667 worker->attachment_cnt = saved_cnt; 668 669 mutex_unlock(&worker->mutex); 670 671 return attach.ret; 672 } 673 674 /* Caller should have device mutex */ 675 bool vhost_dev_has_owner(struct vhost_dev *dev) 676 { 677 return dev->mm; 678 } 679 EXPORT_SYMBOL_GPL(vhost_dev_has_owner); 680 681 static void vhost_attach_mm(struct vhost_dev *dev) 682 { 683 /* No owner, become one */ 684 if (dev->use_worker) { 685 dev->mm = get_task_mm(current); 686 } else { 687 /* vDPA device does not use worker thread, so there's 688 * no need to hold the address space for mm. This helps 689 * to avoid deadlock in the case of mmap() which may 690 * hold the refcnt of the file and depends on release 691 * method to remove vma. 692 */ 693 dev->mm = current->mm; 694 mmgrab(dev->mm); 695 } 696 } 697 698 static void vhost_detach_mm(struct vhost_dev *dev) 699 { 700 if (!dev->mm) 701 return; 702 703 if (dev->use_worker) 704 mmput(dev->mm); 705 else 706 mmdrop(dev->mm); 707 708 dev->mm = NULL; 709 } 710 711 static void vhost_worker_destroy(struct vhost_dev *dev, 712 struct vhost_worker *worker) 713 { 714 if (!worker) 715 return; 716 717 WARN_ON(!llist_empty(&worker->work_list)); 718 xa_erase(&dev->worker_xa, worker->id); 719 worker->ops->stop(worker); 720 kfree(worker); 721 } 722 723 static void vhost_workers_free(struct vhost_dev *dev) 724 { 725 struct vhost_worker *worker; 726 unsigned long i; 727 728 if (!dev->use_worker) 729 return; 730 731 for (i = 0; i < dev->nvqs; i++) 732 rcu_assign_pointer(dev->vqs[i]->worker, NULL); 733 /* 734 * Free the default worker we created and cleanup workers userspace 735 * created but couldn't clean up (it forgot or crashed). 736 */ 737 xa_for_each(&dev->worker_xa, i, worker) 738 vhost_worker_destroy(dev, worker); 739 xa_destroy(&dev->worker_xa); 740 } 741 742 static void vhost_task_wakeup(struct vhost_worker *worker) 743 { 744 return vhost_task_wake(worker->vtsk); 745 } 746 747 static void vhost_kthread_wakeup(struct vhost_worker *worker) 748 { 749 wake_up_process(worker->kthread_task); 750 } 751 752 static void vhost_task_do_stop(struct vhost_worker *worker) 753 { 754 return vhost_task_stop(worker->vtsk); 755 } 756 757 static void vhost_kthread_do_stop(struct vhost_worker *worker) 758 { 759 kthread_stop(worker->kthread_task); 760 } 761 762 static int vhost_task_worker_create(struct vhost_worker *worker, 763 struct vhost_dev *dev, const char *name) 764 { 765 struct vhost_task *vtsk; 766 u32 id; 767 int ret; 768 769 vtsk = vhost_task_create(vhost_run_work_list, vhost_worker_killed, 770 worker, name); 771 if (IS_ERR(vtsk)) 772 return PTR_ERR(vtsk); 773 774 worker->vtsk = vtsk; 775 vhost_task_start(vtsk); 776 ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL); 777 if (ret < 0) { 778 vhost_task_do_stop(worker); 779 return ret; 780 } 781 worker->id = id; 782 return 0; 783 } 784 785 static int vhost_kthread_worker_create(struct vhost_worker *worker, 786 struct vhost_dev *dev, const char *name) 787 { 788 struct task_struct *task; 789 u32 id; 790 int ret; 791 792 task = kthread_create(vhost_run_work_kthread_list, worker, "%s", name); 793 if (IS_ERR(task)) 794 return PTR_ERR(task); 795 796 worker->kthread_task = task; 797 wake_up_process(task); 798 ret = xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL); 799 if (ret < 0) 800 goto stop_worker; 801 802 ret = vhost_attach_task_to_cgroups(worker); 803 if (ret) 804 goto free_id; 805 806 worker->id = id; 807 return 0; 808 809 free_id: 810 xa_erase(&dev->worker_xa, id); 811 stop_worker: 812 vhost_kthread_do_stop(worker); 813 return ret; 814 } 815 816 static const struct vhost_worker_ops kthread_ops = { 817 .create = vhost_kthread_worker_create, 818 .stop = vhost_kthread_do_stop, 819 .wakeup = vhost_kthread_wakeup, 820 }; 821 822 static const struct vhost_worker_ops vhost_task_ops = { 823 .create = vhost_task_worker_create, 824 .stop = vhost_task_do_stop, 825 .wakeup = vhost_task_wakeup, 826 }; 827 828 static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) 829 { 830 struct vhost_worker *worker; 831 char name[TASK_COMM_LEN]; 832 int ret; 833 const struct vhost_worker_ops *ops = dev->fork_owner ? &vhost_task_ops : 834 &kthread_ops; 835 836 worker = kzalloc_obj(*worker, GFP_KERNEL_ACCOUNT); 837 if (!worker) 838 return NULL; 839 840 worker->dev = dev; 841 worker->ops = ops; 842 snprintf(name, sizeof(name), "vhost-%d", current->pid); 843 844 mutex_init(&worker->mutex); 845 init_llist_head(&worker->work_list); 846 worker->kcov_handle = kcov_common_handle(); 847 ret = ops->create(worker, dev, name); 848 if (ret < 0) 849 goto free_worker; 850 851 return worker; 852 853 free_worker: 854 kfree(worker); 855 return NULL; 856 } 857 858 /* Caller must have device mutex */ 859 static void __vhost_vq_attach_worker(struct vhost_virtqueue *vq, 860 struct vhost_worker *worker) 861 { 862 struct vhost_worker *old_worker; 863 864 mutex_lock(&worker->mutex); 865 if (worker->killed) { 866 mutex_unlock(&worker->mutex); 867 return; 868 } 869 870 mutex_lock(&vq->mutex); 871 872 old_worker = rcu_dereference_check(vq->worker, 873 lockdep_is_held(&vq->mutex)); 874 rcu_assign_pointer(vq->worker, worker); 875 worker->attachment_cnt++; 876 877 if (!old_worker) { 878 mutex_unlock(&vq->mutex); 879 mutex_unlock(&worker->mutex); 880 return; 881 } 882 mutex_unlock(&vq->mutex); 883 mutex_unlock(&worker->mutex); 884 885 /* 886 * Take the worker mutex to make sure we see the work queued from 887 * device wide flushes which doesn't use RCU for execution. 888 */ 889 mutex_lock(&old_worker->mutex); 890 if (old_worker->killed) { 891 mutex_unlock(&old_worker->mutex); 892 return; 893 } 894 895 /* 896 * We don't want to call synchronize_rcu for every vq during setup 897 * because it will slow down VM startup. If we haven't done 898 * VHOST_SET_VRING_KICK and not done the driver specific 899 * SET_ENDPOINT/RUNNING then we can skip the sync since there will 900 * not be any works queued for scsi and net. 901 */ 902 mutex_lock(&vq->mutex); 903 if (!vhost_vq_get_backend(vq) && !vq->kick) { 904 mutex_unlock(&vq->mutex); 905 906 old_worker->attachment_cnt--; 907 mutex_unlock(&old_worker->mutex); 908 /* 909 * vsock can queue anytime after VHOST_VSOCK_SET_GUEST_CID. 910 * Warn if it adds support for multiple workers but forgets to 911 * handle the early queueing case. 912 */ 913 WARN_ON(!old_worker->attachment_cnt && 914 !llist_empty(&old_worker->work_list)); 915 return; 916 } 917 mutex_unlock(&vq->mutex); 918 919 /* Make sure new vq queue/flush/poll calls see the new worker */ 920 synchronize_rcu(); 921 /* Make sure whatever was queued gets run */ 922 __vhost_worker_flush(old_worker); 923 old_worker->attachment_cnt--; 924 mutex_unlock(&old_worker->mutex); 925 } 926 927 /* Caller must have device mutex */ 928 static int vhost_vq_attach_worker(struct vhost_virtqueue *vq, 929 struct vhost_vring_worker *info) 930 { 931 unsigned long index = info->worker_id; 932 struct vhost_dev *dev = vq->dev; 933 struct vhost_worker *worker; 934 935 if (!dev->use_worker) 936 return -EINVAL; 937 938 worker = xa_find(&dev->worker_xa, &index, UINT_MAX, XA_PRESENT); 939 if (!worker || worker->id != info->worker_id) 940 return -ENODEV; 941 942 __vhost_vq_attach_worker(vq, worker); 943 return 0; 944 } 945 946 /* Caller must have device mutex */ 947 static int vhost_new_worker(struct vhost_dev *dev, 948 struct vhost_worker_state *info) 949 { 950 struct vhost_worker *worker; 951 952 worker = vhost_worker_create(dev); 953 if (!worker) 954 return -ENOMEM; 955 956 info->worker_id = worker->id; 957 return 0; 958 } 959 960 /* Caller must have device mutex */ 961 static int vhost_free_worker(struct vhost_dev *dev, 962 struct vhost_worker_state *info) 963 { 964 unsigned long index = info->worker_id; 965 struct vhost_worker *worker; 966 967 worker = xa_find(&dev->worker_xa, &index, UINT_MAX, XA_PRESENT); 968 if (!worker || worker->id != info->worker_id) 969 return -ENODEV; 970 971 mutex_lock(&worker->mutex); 972 if (worker->attachment_cnt || worker->killed) { 973 mutex_unlock(&worker->mutex); 974 return -EBUSY; 975 } 976 /* 977 * A flush might have raced and snuck in before attachment_cnt was set 978 * to zero. Make sure flushes are flushed from the queue before 979 * freeing. 980 */ 981 __vhost_worker_flush(worker); 982 mutex_unlock(&worker->mutex); 983 984 vhost_worker_destroy(dev, worker); 985 return 0; 986 } 987 988 static int vhost_get_vq_from_user(struct vhost_dev *dev, void __user *argp, 989 struct vhost_virtqueue **vq, u32 *id) 990 { 991 u32 __user *idxp = argp; 992 u32 idx; 993 long r; 994 995 r = get_user(idx, idxp); 996 if (r < 0) 997 return r; 998 999 if (idx >= dev->nvqs) 1000 return -ENOBUFS; 1001 1002 idx = array_index_nospec(idx, dev->nvqs); 1003 1004 *vq = dev->vqs[idx]; 1005 *id = idx; 1006 return 0; 1007 } 1008 1009 /* Caller must have device mutex */ 1010 long vhost_worker_ioctl(struct vhost_dev *dev, unsigned int ioctl, 1011 void __user *argp) 1012 { 1013 struct vhost_vring_worker ring_worker; 1014 struct vhost_worker_state state; 1015 struct vhost_worker *worker; 1016 struct vhost_virtqueue *vq; 1017 long ret; 1018 u32 idx; 1019 1020 if (!dev->use_worker) 1021 return -EINVAL; 1022 1023 if (!vhost_dev_has_owner(dev)) 1024 return -EINVAL; 1025 1026 ret = vhost_dev_check_owner(dev); 1027 if (ret) 1028 return ret; 1029 1030 switch (ioctl) { 1031 /* dev worker ioctls */ 1032 case VHOST_NEW_WORKER: 1033 /* 1034 * vhost_tasks will account for worker threads under the parent's 1035 * NPROC value but kthreads do not. To avoid userspace overflowing 1036 * the system with worker threads fork_owner must be true. 1037 */ 1038 if (!dev->fork_owner) 1039 return -EFAULT; 1040 1041 ret = vhost_new_worker(dev, &state); 1042 if (!ret && copy_to_user(argp, &state, sizeof(state))) 1043 ret = -EFAULT; 1044 return ret; 1045 case VHOST_FREE_WORKER: 1046 if (copy_from_user(&state, argp, sizeof(state))) 1047 return -EFAULT; 1048 return vhost_free_worker(dev, &state); 1049 /* vring worker ioctls */ 1050 case VHOST_ATTACH_VRING_WORKER: 1051 case VHOST_GET_VRING_WORKER: 1052 break; 1053 default: 1054 return -ENOIOCTLCMD; 1055 } 1056 1057 ret = vhost_get_vq_from_user(dev, argp, &vq, &idx); 1058 if (ret) 1059 return ret; 1060 1061 switch (ioctl) { 1062 case VHOST_ATTACH_VRING_WORKER: 1063 if (copy_from_user(&ring_worker, argp, sizeof(ring_worker))) { 1064 ret = -EFAULT; 1065 break; 1066 } 1067 1068 ret = vhost_vq_attach_worker(vq, &ring_worker); 1069 break; 1070 case VHOST_GET_VRING_WORKER: 1071 worker = rcu_dereference_check(vq->worker, 1072 lockdep_is_held(&dev->mutex)); 1073 if (!worker) { 1074 ret = -EINVAL; 1075 break; 1076 } 1077 1078 ring_worker.index = idx; 1079 ring_worker.worker_id = worker->id; 1080 1081 if (copy_to_user(argp, &ring_worker, sizeof(ring_worker))) 1082 ret = -EFAULT; 1083 break; 1084 default: 1085 ret = -ENOIOCTLCMD; 1086 break; 1087 } 1088 1089 return ret; 1090 } 1091 EXPORT_SYMBOL_GPL(vhost_worker_ioctl); 1092 1093 /* Caller should have device mutex */ 1094 long vhost_dev_set_owner(struct vhost_dev *dev) 1095 { 1096 struct vhost_worker *worker; 1097 int err, i; 1098 1099 /* Is there an owner already? */ 1100 if (vhost_dev_has_owner(dev)) { 1101 err = -EBUSY; 1102 goto err_mm; 1103 } 1104 1105 vhost_attach_mm(dev); 1106 1107 err = vhost_dev_alloc_iovecs(dev); 1108 if (err) 1109 goto err_iovecs; 1110 1111 if (dev->use_worker) { 1112 /* 1113 * This should be done last, because vsock can queue work 1114 * before VHOST_SET_OWNER so it simplifies the failure path 1115 * below since we don't have to worry about vsock queueing 1116 * while we free the worker. 1117 */ 1118 worker = vhost_worker_create(dev); 1119 if (!worker) { 1120 err = -ENOMEM; 1121 goto err_worker; 1122 } 1123 1124 for (i = 0; i < dev->nvqs; i++) 1125 __vhost_vq_attach_worker(dev->vqs[i], worker); 1126 } 1127 1128 return 0; 1129 1130 err_worker: 1131 vhost_dev_free_iovecs(dev); 1132 err_iovecs: 1133 vhost_detach_mm(dev); 1134 err_mm: 1135 return err; 1136 } 1137 EXPORT_SYMBOL_GPL(vhost_dev_set_owner); 1138 1139 static struct vhost_iotlb *iotlb_alloc(void) 1140 { 1141 return vhost_iotlb_alloc(max_iotlb_entries, 1142 VHOST_IOTLB_FLAG_RETIRE); 1143 } 1144 1145 struct vhost_iotlb *vhost_dev_reset_owner_prepare(void) 1146 { 1147 return iotlb_alloc(); 1148 } 1149 EXPORT_SYMBOL_GPL(vhost_dev_reset_owner_prepare); 1150 1151 /* Caller should have device mutex */ 1152 void vhost_dev_reset_owner(struct vhost_dev *dev, struct vhost_iotlb *umem) 1153 { 1154 int i; 1155 1156 vhost_dev_cleanup(dev); 1157 1158 dev->fork_owner = fork_from_owner_default; 1159 dev->umem = umem; 1160 /* We don't need VQ locks below since vhost_dev_cleanup makes sure 1161 * VQs aren't running. 1162 */ 1163 for (i = 0; i < dev->nvqs; ++i) 1164 dev->vqs[i]->umem = umem; 1165 } 1166 EXPORT_SYMBOL_GPL(vhost_dev_reset_owner); 1167 1168 void vhost_dev_stop(struct vhost_dev *dev) 1169 { 1170 int i; 1171 1172 for (i = 0; i < dev->nvqs; ++i) { 1173 if (dev->vqs[i]->kick && dev->vqs[i]->handle_kick) 1174 vhost_poll_stop(&dev->vqs[i]->poll); 1175 } 1176 1177 vhost_dev_flush(dev); 1178 } 1179 EXPORT_SYMBOL_GPL(vhost_dev_stop); 1180 1181 void vhost_clear_msg(struct vhost_dev *dev) 1182 { 1183 struct vhost_msg_node *node, *n; 1184 1185 spin_lock(&dev->iotlb_lock); 1186 1187 list_for_each_entry_safe(node, n, &dev->read_list, node) { 1188 list_del(&node->node); 1189 kfree(node); 1190 } 1191 1192 list_for_each_entry_safe(node, n, &dev->pending_list, node) { 1193 list_del(&node->node); 1194 kfree(node); 1195 } 1196 1197 spin_unlock(&dev->iotlb_lock); 1198 } 1199 EXPORT_SYMBOL_GPL(vhost_clear_msg); 1200 1201 void vhost_dev_cleanup(struct vhost_dev *dev) 1202 { 1203 int i; 1204 1205 for (i = 0; i < dev->nvqs; ++i) { 1206 if (dev->vqs[i]->error_ctx) 1207 eventfd_ctx_put(dev->vqs[i]->error_ctx); 1208 if (dev->vqs[i]->kick) 1209 fput(dev->vqs[i]->kick); 1210 if (dev->vqs[i]->call_ctx.ctx) 1211 eventfd_ctx_put(dev->vqs[i]->call_ctx.ctx); 1212 vhost_vq_reset(dev, dev->vqs[i]); 1213 } 1214 vhost_dev_free_iovecs(dev); 1215 if (dev->log_ctx) 1216 eventfd_ctx_put(dev->log_ctx); 1217 dev->log_ctx = NULL; 1218 /* No one will access memory at this point */ 1219 vhost_iotlb_free(dev->umem); 1220 dev->umem = NULL; 1221 vhost_iotlb_free(dev->iotlb); 1222 dev->iotlb = NULL; 1223 vhost_clear_msg(dev); 1224 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM); 1225 vhost_workers_free(dev); 1226 vhost_detach_mm(dev); 1227 } 1228 EXPORT_SYMBOL_GPL(vhost_dev_cleanup); 1229 1230 static bool log_access_ok(void __user *log_base, u64 addr, unsigned long sz) 1231 { 1232 u64 a = addr / VHOST_PAGE_SIZE / 8; 1233 1234 /* Make sure 64 bit math will not overflow. */ 1235 if (a > ULONG_MAX - (unsigned long)log_base || 1236 a + (unsigned long)log_base > ULONG_MAX) 1237 return false; 1238 1239 return access_ok(log_base + a, 1240 (sz + VHOST_PAGE_SIZE * 8 - 1) / VHOST_PAGE_SIZE / 8); 1241 } 1242 1243 /* Make sure 64 bit math will not overflow. */ 1244 static bool vhost_overflow(u64 uaddr, u64 size) 1245 { 1246 if (uaddr > ULONG_MAX || size > ULONG_MAX) 1247 return true; 1248 1249 if (!size) 1250 return false; 1251 1252 return uaddr > ULONG_MAX - size + 1; 1253 } 1254 1255 /* Caller should have vq mutex and device mutex. */ 1256 static bool vq_memory_access_ok(void __user *log_base, struct vhost_iotlb *umem, 1257 int log_all) 1258 { 1259 struct vhost_iotlb_map *map; 1260 1261 if (!umem) 1262 return false; 1263 1264 list_for_each_entry(map, &umem->list, link) { 1265 unsigned long a = map->addr; 1266 1267 if (vhost_overflow(map->addr, map->size)) 1268 return false; 1269 1270 1271 if (!access_ok((void __user *)a, map->size)) 1272 return false; 1273 else if (log_all && !log_access_ok(log_base, 1274 map->start, 1275 map->size)) 1276 return false; 1277 } 1278 return true; 1279 } 1280 1281 static inline void __user *vhost_vq_meta_fetch(struct vhost_virtqueue *vq, 1282 u64 addr, unsigned int size, 1283 int type) 1284 { 1285 const struct vhost_iotlb_map *map = vq->meta_iotlb[type]; 1286 1287 if (!map) 1288 return NULL; 1289 1290 return (void __user *)(uintptr_t)(map->addr + addr - map->start); 1291 } 1292 1293 /* Can we switch to this memory table? */ 1294 /* Caller should have device mutex but not vq mutex */ 1295 static bool memory_access_ok(struct vhost_dev *d, struct vhost_iotlb *umem, 1296 int log_all) 1297 { 1298 int i; 1299 1300 for (i = 0; i < d->nvqs; ++i) { 1301 bool ok; 1302 bool log; 1303 1304 mutex_lock(&d->vqs[i]->mutex); 1305 log = log_all || vhost_has_feature(d->vqs[i], VHOST_F_LOG_ALL); 1306 /* If ring is inactive, will check when it's enabled. */ 1307 if (d->vqs[i]->private_data) 1308 ok = vq_memory_access_ok(d->vqs[i]->log_base, 1309 umem, log); 1310 else 1311 ok = true; 1312 mutex_unlock(&d->vqs[i]->mutex); 1313 if (!ok) 1314 return false; 1315 } 1316 return true; 1317 } 1318 1319 static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len, 1320 struct iovec iov[], int iov_size, int access); 1321 1322 static int vhost_copy_to_user(struct vhost_virtqueue *vq, void __user *to, 1323 const void *from, unsigned size) 1324 { 1325 int ret; 1326 1327 if (!vq->iotlb) 1328 return __copy_to_user(to, from, size); 1329 else { 1330 /* This function should be called after iotlb 1331 * prefetch, which means we're sure that all vq 1332 * could be access through iotlb. So -EAGAIN should 1333 * not happen in this case. 1334 */ 1335 struct iov_iter t; 1336 void __user *uaddr = vhost_vq_meta_fetch(vq, 1337 (u64)(uintptr_t)to, size, 1338 VHOST_ADDR_USED); 1339 1340 if (uaddr) 1341 return __copy_to_user(uaddr, from, size); 1342 1343 ret = translate_desc(vq, (u64)(uintptr_t)to, size, vq->iotlb_iov, 1344 ARRAY_SIZE(vq->iotlb_iov), 1345 VHOST_ACCESS_WO); 1346 if (ret < 0) 1347 goto out; 1348 iov_iter_init(&t, ITER_DEST, vq->iotlb_iov, ret, size); 1349 ret = copy_to_iter(from, size, &t); 1350 if (ret == size) 1351 ret = 0; 1352 } 1353 out: 1354 return ret; 1355 } 1356 1357 static int vhost_copy_from_user(struct vhost_virtqueue *vq, void *to, 1358 void __user *from, unsigned size) 1359 { 1360 int ret; 1361 1362 if (!vq->iotlb) 1363 return __copy_from_user(to, from, size); 1364 else { 1365 /* This function should be called after iotlb 1366 * prefetch, which means we're sure that vq 1367 * could be access through iotlb. So -EAGAIN should 1368 * not happen in this case. 1369 */ 1370 void __user *uaddr = vhost_vq_meta_fetch(vq, 1371 (u64)(uintptr_t)from, size, 1372 VHOST_ADDR_DESC); 1373 struct iov_iter f; 1374 1375 if (uaddr) 1376 return __copy_from_user(to, uaddr, size); 1377 1378 ret = translate_desc(vq, (u64)(uintptr_t)from, size, vq->iotlb_iov, 1379 ARRAY_SIZE(vq->iotlb_iov), 1380 VHOST_ACCESS_RO); 1381 if (ret < 0) { 1382 vq_err(vq, "IOTLB translation failure: uaddr " 1383 "%p size 0x%llx\n", from, 1384 (unsigned long long) size); 1385 goto out; 1386 } 1387 iov_iter_init(&f, ITER_SOURCE, vq->iotlb_iov, ret, size); 1388 ret = copy_from_iter(to, size, &f); 1389 if (ret == size) 1390 ret = 0; 1391 } 1392 1393 out: 1394 return ret; 1395 } 1396 1397 static void __user *__vhost_get_user_slow(struct vhost_virtqueue *vq, 1398 void __user *addr, unsigned int size, 1399 int type) 1400 { 1401 int ret; 1402 1403 ret = translate_desc(vq, (u64)(uintptr_t)addr, size, vq->iotlb_iov, 1404 ARRAY_SIZE(vq->iotlb_iov), 1405 VHOST_ACCESS_RO); 1406 if (ret < 0) { 1407 vq_err(vq, "IOTLB translation failure: uaddr " 1408 "%p size 0x%llx\n", addr, 1409 (unsigned long long) size); 1410 return NULL; 1411 } 1412 1413 if (ret != 1 || vq->iotlb_iov[0].iov_len != size) { 1414 vq_err(vq, "Non atomic userspace memory access: uaddr " 1415 "%p size 0x%llx\n", addr, 1416 (unsigned long long) size); 1417 return NULL; 1418 } 1419 1420 return vq->iotlb_iov[0].iov_base; 1421 } 1422 1423 /* This function should be called after iotlb 1424 * prefetch, which means we're sure that vq 1425 * could be access through iotlb. So -EAGAIN should 1426 * not happen in this case. 1427 */ 1428 static inline void __user *__vhost_get_user(struct vhost_virtqueue *vq, 1429 void __user *addr, unsigned int size, 1430 int type) 1431 { 1432 void __user *uaddr = vhost_vq_meta_fetch(vq, 1433 (u64)(uintptr_t)addr, size, type); 1434 if (uaddr) 1435 return uaddr; 1436 1437 return __vhost_get_user_slow(vq, addr, size, type); 1438 } 1439 1440 #define vhost_put_user(vq, x, ptr) \ 1441 ({ \ 1442 int ret; \ 1443 if (!vq->iotlb) { \ 1444 ret = put_user(x, ptr); \ 1445 } else { \ 1446 __typeof__(ptr) to = \ 1447 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ 1448 sizeof(*ptr), VHOST_ADDR_USED); \ 1449 if (to != NULL) \ 1450 ret = put_user(x, to); \ 1451 else \ 1452 ret = -EFAULT; \ 1453 } \ 1454 ret; \ 1455 }) 1456 1457 static inline int vhost_put_avail_event(struct vhost_virtqueue *vq) 1458 { 1459 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->avail_idx), 1460 vhost_avail_event(vq)); 1461 } 1462 1463 static inline int vhost_put_used(struct vhost_virtqueue *vq, 1464 struct vring_used_elem *head, int idx, 1465 int count) 1466 { 1467 return vhost_copy_to_user(vq, vq->used->ring + idx, head, 1468 count * sizeof(*head)); 1469 } 1470 1471 static inline int vhost_put_used_flags(struct vhost_virtqueue *vq) 1472 1473 { 1474 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->used_flags), 1475 &vq->used->flags); 1476 } 1477 1478 static inline int vhost_put_used_idx(struct vhost_virtqueue *vq) 1479 1480 { 1481 return vhost_put_user(vq, cpu_to_vhost16(vq, vq->last_used_idx), 1482 &vq->used->idx); 1483 } 1484 1485 #define vhost_get_user(vq, x, ptr, type) \ 1486 ({ \ 1487 int ret; \ 1488 if (!vq->iotlb) { \ 1489 ret = get_user(x, ptr); \ 1490 } else { \ 1491 __typeof__(ptr) from = \ 1492 (__typeof__(ptr)) __vhost_get_user(vq, ptr, \ 1493 sizeof(*ptr), \ 1494 type); \ 1495 if (from != NULL) \ 1496 ret = get_user(x, from); \ 1497 else \ 1498 ret = -EFAULT; \ 1499 } \ 1500 ret; \ 1501 }) 1502 1503 #define vhost_get_avail(vq, x, ptr) \ 1504 vhost_get_user(vq, x, ptr, VHOST_ADDR_AVAIL) 1505 1506 #define vhost_get_used(vq, x, ptr) \ 1507 vhost_get_user(vq, x, ptr, VHOST_ADDR_USED) 1508 1509 static void vhost_dev_lock_vqs(struct vhost_dev *d) 1510 { 1511 int i = 0; 1512 for (i = 0; i < d->nvqs; ++i) 1513 mutex_lock_nested(&d->vqs[i]->mutex, i); 1514 } 1515 1516 static void vhost_dev_unlock_vqs(struct vhost_dev *d) 1517 { 1518 int i = 0; 1519 for (i = 0; i < d->nvqs; ++i) 1520 mutex_unlock(&d->vqs[i]->mutex); 1521 } 1522 1523 static inline int vhost_get_avail_idx(struct vhost_virtqueue *vq) 1524 { 1525 __virtio16 idx; 1526 int r; 1527 1528 r = vhost_get_avail(vq, idx, &vq->avail->idx); 1529 if (unlikely(r < 0)) { 1530 vq_err(vq, "Failed to access available index at %p (%d)\n", 1531 &vq->avail->idx, r); 1532 return r; 1533 } 1534 1535 /* Check it isn't doing very strange thing with available indexes */ 1536 vq->avail_idx = vhost16_to_cpu(vq, idx); 1537 if (unlikely((u16)(vq->avail_idx - vq->last_avail_idx) > vq->num)) { 1538 vq_err(vq, "Invalid available index change from %u to %u", 1539 vq->last_avail_idx, vq->avail_idx); 1540 return -EINVAL; 1541 } 1542 1543 /* We're done if there is nothing new */ 1544 if (vq->avail_idx == vq->last_avail_idx) 1545 return 0; 1546 1547 /* 1548 * We updated vq->avail_idx so we need a memory barrier between 1549 * the index read above and the caller reading avail ring entries. 1550 */ 1551 smp_rmb(); 1552 return 1; 1553 } 1554 1555 static inline int vhost_get_avail_head(struct vhost_virtqueue *vq, 1556 __virtio16 *head, int idx) 1557 { 1558 return vhost_get_avail(vq, *head, 1559 &vq->avail->ring[idx & (vq->num - 1)]); 1560 } 1561 1562 static inline int vhost_get_avail_flags(struct vhost_virtqueue *vq, 1563 __virtio16 *flags) 1564 { 1565 return vhost_get_avail(vq, *flags, &vq->avail->flags); 1566 } 1567 1568 static inline int vhost_get_used_event(struct vhost_virtqueue *vq, 1569 __virtio16 *event) 1570 { 1571 return vhost_get_avail(vq, *event, vhost_used_event(vq)); 1572 } 1573 1574 static inline int vhost_get_used_idx(struct vhost_virtqueue *vq, 1575 __virtio16 *idx) 1576 { 1577 return vhost_get_used(vq, *idx, &vq->used->idx); 1578 } 1579 1580 static inline int vhost_get_desc(struct vhost_virtqueue *vq, 1581 struct vring_desc *desc, int idx) 1582 { 1583 return vhost_copy_from_user(vq, desc, vq->desc + idx, sizeof(*desc)); 1584 } 1585 1586 static void vhost_iotlb_notify_vq(struct vhost_dev *d, 1587 struct vhost_iotlb_msg *msg) 1588 { 1589 struct vhost_msg_node *node, *n; 1590 1591 spin_lock(&d->iotlb_lock); 1592 1593 list_for_each_entry_safe(node, n, &d->pending_list, node) { 1594 struct vhost_iotlb_msg *vq_msg = &node->msg.iotlb; 1595 if (msg->iova <= vq_msg->iova && 1596 msg->iova + msg->size - 1 >= vq_msg->iova && 1597 vq_msg->type == VHOST_IOTLB_MISS) { 1598 vhost_poll_queue(&node->vq->poll); 1599 list_del(&node->node); 1600 kfree(node); 1601 } 1602 } 1603 1604 spin_unlock(&d->iotlb_lock); 1605 } 1606 1607 static bool umem_access_ok(u64 uaddr, u64 size, int access) 1608 { 1609 unsigned long a = uaddr; 1610 1611 /* Make sure 64 bit math will not overflow. */ 1612 if (vhost_overflow(uaddr, size)) 1613 return false; 1614 1615 if ((access & VHOST_ACCESS_RO) && 1616 !access_ok((void __user *)a, size)) 1617 return false; 1618 if ((access & VHOST_ACCESS_WO) && 1619 !access_ok((void __user *)a, size)) 1620 return false; 1621 return true; 1622 } 1623 1624 static int vhost_process_iotlb_msg(struct vhost_dev *dev, u32 asid, 1625 struct vhost_iotlb_msg *msg) 1626 { 1627 int ret = 0; 1628 1629 if (asid != 0) 1630 return -EINVAL; 1631 1632 mutex_lock(&dev->mutex); 1633 vhost_dev_lock_vqs(dev); 1634 switch (msg->type) { 1635 case VHOST_IOTLB_UPDATE: 1636 if (!dev->iotlb) { 1637 ret = -EFAULT; 1638 break; 1639 } 1640 if (!umem_access_ok(msg->uaddr, msg->size, msg->perm)) { 1641 ret = -EFAULT; 1642 break; 1643 } 1644 vhost_vq_meta_reset(dev); 1645 if (vhost_iotlb_add_range(dev->iotlb, msg->iova, 1646 msg->iova + msg->size - 1, 1647 msg->uaddr, msg->perm)) { 1648 ret = -ENOMEM; 1649 break; 1650 } 1651 vhost_iotlb_notify_vq(dev, msg); 1652 break; 1653 case VHOST_IOTLB_INVALIDATE: 1654 if (!dev->iotlb) { 1655 ret = -EFAULT; 1656 break; 1657 } 1658 vhost_vq_meta_reset(dev); 1659 vhost_iotlb_del_range(dev->iotlb, msg->iova, 1660 msg->iova + msg->size - 1); 1661 break; 1662 default: 1663 ret = -EINVAL; 1664 break; 1665 } 1666 1667 vhost_dev_unlock_vqs(dev); 1668 mutex_unlock(&dev->mutex); 1669 1670 return ret; 1671 } 1672 ssize_t vhost_chr_write_iter(struct vhost_dev *dev, 1673 struct iov_iter *from) 1674 { 1675 struct vhost_iotlb_msg msg; 1676 size_t offset; 1677 int type, ret; 1678 u32 asid = 0; 1679 1680 ret = copy_from_iter(&type, sizeof(type), from); 1681 if (ret != sizeof(type)) { 1682 ret = -EINVAL; 1683 goto done; 1684 } 1685 1686 switch (type) { 1687 case VHOST_IOTLB_MSG: 1688 /* There maybe a hole after type for V1 message type, 1689 * so skip it here. 1690 */ 1691 offset = offsetof(struct vhost_msg, iotlb) - sizeof(int); 1692 break; 1693 case VHOST_IOTLB_MSG_V2: 1694 if (vhost_backend_has_feature(dev->vqs[0], 1695 VHOST_BACKEND_F_IOTLB_ASID)) { 1696 ret = copy_from_iter(&asid, sizeof(asid), from); 1697 if (ret != sizeof(asid)) { 1698 ret = -EINVAL; 1699 goto done; 1700 } 1701 offset = 0; 1702 } else 1703 offset = sizeof(__u32); 1704 break; 1705 default: 1706 ret = -EINVAL; 1707 goto done; 1708 } 1709 1710 iov_iter_advance(from, offset); 1711 ret = copy_from_iter(&msg, sizeof(msg), from); 1712 if (ret != sizeof(msg)) { 1713 ret = -EINVAL; 1714 goto done; 1715 } 1716 1717 if (msg.type == VHOST_IOTLB_UPDATE && msg.size == 0) { 1718 ret = -EINVAL; 1719 goto done; 1720 } 1721 1722 if (dev->msg_handler) 1723 ret = dev->msg_handler(dev, asid, &msg); 1724 else 1725 ret = vhost_process_iotlb_msg(dev, asid, &msg); 1726 if (ret) { 1727 ret = -EFAULT; 1728 goto done; 1729 } 1730 1731 ret = (type == VHOST_IOTLB_MSG) ? sizeof(struct vhost_msg) : 1732 sizeof(struct vhost_msg_v2); 1733 done: 1734 return ret; 1735 } 1736 EXPORT_SYMBOL(vhost_chr_write_iter); 1737 1738 __poll_t vhost_chr_poll(struct file *file, struct vhost_dev *dev, 1739 poll_table *wait) 1740 { 1741 __poll_t mask = 0; 1742 1743 poll_wait(file, &dev->wait, wait); 1744 1745 if (!list_empty(&dev->read_list)) 1746 mask |= EPOLLIN | EPOLLRDNORM; 1747 1748 return mask; 1749 } 1750 EXPORT_SYMBOL(vhost_chr_poll); 1751 1752 ssize_t vhost_chr_read_iter(struct vhost_dev *dev, struct iov_iter *to, 1753 int noblock) 1754 { 1755 DEFINE_WAIT(wait); 1756 struct vhost_msg_node *node; 1757 ssize_t ret = 0; 1758 unsigned size = sizeof(struct vhost_msg); 1759 1760 if (iov_iter_count(to) < size) 1761 return 0; 1762 1763 while (1) { 1764 if (!noblock) 1765 prepare_to_wait(&dev->wait, &wait, 1766 TASK_INTERRUPTIBLE); 1767 1768 node = vhost_dequeue_msg(dev, &dev->read_list); 1769 if (node) 1770 break; 1771 if (noblock) { 1772 ret = -EAGAIN; 1773 break; 1774 } 1775 if (signal_pending(current)) { 1776 ret = -ERESTARTSYS; 1777 break; 1778 } 1779 if (!dev->iotlb) { 1780 ret = -EBADFD; 1781 break; 1782 } 1783 1784 schedule(); 1785 } 1786 1787 if (!noblock) 1788 finish_wait(&dev->wait, &wait); 1789 1790 if (node) { 1791 struct vhost_iotlb_msg *msg; 1792 void *start = &node->msg; 1793 1794 switch (node->msg.type) { 1795 case VHOST_IOTLB_MSG: 1796 size = sizeof(node->msg); 1797 msg = &node->msg.iotlb; 1798 break; 1799 case VHOST_IOTLB_MSG_V2: 1800 size = sizeof(node->msg_v2); 1801 msg = &node->msg_v2.iotlb; 1802 break; 1803 default: 1804 BUG(); 1805 break; 1806 } 1807 1808 ret = copy_to_iter(start, size, to); 1809 if (ret != size || msg->type != VHOST_IOTLB_MISS) { 1810 kfree(node); 1811 return ret; 1812 } 1813 vhost_enqueue_msg(dev, &dev->pending_list, node); 1814 } 1815 1816 return ret; 1817 } 1818 EXPORT_SYMBOL_GPL(vhost_chr_read_iter); 1819 1820 static int vhost_iotlb_miss(struct vhost_virtqueue *vq, u64 iova, int access) 1821 { 1822 struct vhost_dev *dev = vq->dev; 1823 struct vhost_msg_node *node; 1824 struct vhost_iotlb_msg *msg; 1825 bool v2 = vhost_backend_has_feature(vq, VHOST_BACKEND_F_IOTLB_MSG_V2); 1826 1827 node = vhost_new_msg(vq, v2 ? VHOST_IOTLB_MSG_V2 : VHOST_IOTLB_MSG); 1828 if (!node) 1829 return -ENOMEM; 1830 1831 if (v2) { 1832 node->msg_v2.type = VHOST_IOTLB_MSG_V2; 1833 msg = &node->msg_v2.iotlb; 1834 } else { 1835 msg = &node->msg.iotlb; 1836 } 1837 1838 msg->type = VHOST_IOTLB_MISS; 1839 msg->iova = iova; 1840 msg->perm = access; 1841 1842 vhost_enqueue_msg(dev, &dev->read_list, node); 1843 1844 return 0; 1845 } 1846 1847 static bool vq_access_ok(struct vhost_virtqueue *vq, unsigned int num, 1848 vring_desc_t __user *desc, 1849 vring_avail_t __user *avail, 1850 vring_used_t __user *used) 1851 1852 { 1853 /* If an IOTLB device is present, the vring addresses are 1854 * GIOVAs. Access validation occurs at prefetch time. */ 1855 if (vq->iotlb) 1856 return true; 1857 1858 return access_ok(desc, vhost_get_desc_size(vq, num)) && 1859 access_ok(avail, vhost_get_avail_size(vq, num)) && 1860 access_ok(used, vhost_get_used_size(vq, num)); 1861 } 1862 1863 static void vhost_vq_meta_update(struct vhost_virtqueue *vq, 1864 const struct vhost_iotlb_map *map, 1865 int type) 1866 { 1867 int access = (type == VHOST_ADDR_USED) ? 1868 VHOST_ACCESS_WO : VHOST_ACCESS_RO; 1869 1870 if (likely(map->perm & access)) 1871 vq->meta_iotlb[type] = map; 1872 } 1873 1874 static bool iotlb_access_ok(struct vhost_virtqueue *vq, 1875 int access, u64 addr, u64 len, int type) 1876 { 1877 const struct vhost_iotlb_map *map; 1878 struct vhost_iotlb *umem = vq->iotlb; 1879 u64 s = 0, size, orig_addr = addr, last = addr + len - 1; 1880 1881 if (vhost_vq_meta_fetch(vq, addr, len, type)) 1882 return true; 1883 1884 while (len > s) { 1885 map = vhost_iotlb_itree_first(umem, addr, last); 1886 if (map == NULL || map->start > addr) { 1887 vhost_iotlb_miss(vq, addr, access); 1888 return false; 1889 } else if (!(map->perm & access)) { 1890 /* Report the possible access violation by 1891 * request another translation from userspace. 1892 */ 1893 return false; 1894 } 1895 1896 size = map->size - addr + map->start; 1897 1898 if (orig_addr == addr && size >= len) 1899 vhost_vq_meta_update(vq, map, type); 1900 1901 s += size; 1902 addr += size; 1903 } 1904 1905 return true; 1906 } 1907 1908 int vq_meta_prefetch(struct vhost_virtqueue *vq) 1909 { 1910 unsigned int num = vq->num; 1911 1912 if (!vq->iotlb) 1913 return 1; 1914 1915 return iotlb_access_ok(vq, VHOST_MAP_RO, (u64)(uintptr_t)vq->desc, 1916 vhost_get_desc_size(vq, num), VHOST_ADDR_DESC) && 1917 iotlb_access_ok(vq, VHOST_MAP_RO, (u64)(uintptr_t)vq->avail, 1918 vhost_get_avail_size(vq, num), 1919 VHOST_ADDR_AVAIL) && 1920 iotlb_access_ok(vq, VHOST_MAP_WO, (u64)(uintptr_t)vq->used, 1921 vhost_get_used_size(vq, num), VHOST_ADDR_USED); 1922 } 1923 EXPORT_SYMBOL_GPL(vq_meta_prefetch); 1924 1925 /* Can we log writes? */ 1926 /* Caller should have device mutex but not vq mutex */ 1927 bool vhost_log_access_ok(struct vhost_dev *dev) 1928 { 1929 return memory_access_ok(dev, dev->umem, 1); 1930 } 1931 EXPORT_SYMBOL_GPL(vhost_log_access_ok); 1932 1933 static bool vq_log_used_access_ok(struct vhost_virtqueue *vq, 1934 void __user *log_base, 1935 bool log_used, 1936 u64 log_addr) 1937 { 1938 /* If an IOTLB device is present, log_addr is a GIOVA that 1939 * will never be logged by log_used(). */ 1940 if (vq->iotlb) 1941 return true; 1942 1943 return !log_used || log_access_ok(log_base, log_addr, 1944 vhost_get_used_size(vq, vq->num)); 1945 } 1946 1947 /* Verify access for write logging. */ 1948 /* Caller should have vq mutex and device mutex */ 1949 static bool vq_log_access_ok(struct vhost_virtqueue *vq, 1950 void __user *log_base) 1951 { 1952 return vq_memory_access_ok(log_base, vq->umem, 1953 vhost_has_feature(vq, VHOST_F_LOG_ALL)) && 1954 vq_log_used_access_ok(vq, log_base, vq->log_used, vq->log_addr); 1955 } 1956 1957 /* Can we start vq? */ 1958 /* Caller should have vq mutex and device mutex */ 1959 bool vhost_vq_access_ok(struct vhost_virtqueue *vq) 1960 { 1961 if (!vq_log_access_ok(vq, vq->log_base)) 1962 return false; 1963 1964 return vq_access_ok(vq, vq->num, vq->desc, vq->avail, vq->used); 1965 } 1966 EXPORT_SYMBOL_GPL(vhost_vq_access_ok); 1967 1968 static long vhost_set_memory(struct vhost_dev *d, struct vhost_memory __user *m) 1969 { 1970 struct vhost_memory mem, *newmem; 1971 struct vhost_memory_region *region; 1972 struct vhost_iotlb *newumem, *oldumem; 1973 unsigned long size = offsetof(struct vhost_memory, regions); 1974 int i; 1975 1976 if (copy_from_user(&mem, m, size)) 1977 return -EFAULT; 1978 if (mem.padding) 1979 return -EOPNOTSUPP; 1980 if (mem.nregions > max_mem_regions) 1981 return -E2BIG; 1982 newmem = kvzalloc_flex(*newmem, regions, mem.nregions, GFP_KERNEL); 1983 if (!newmem) 1984 return -ENOMEM; 1985 1986 memcpy(newmem, &mem, size); 1987 if (copy_from_user(newmem->regions, m->regions, 1988 flex_array_size(newmem, regions, mem.nregions))) { 1989 kvfree(newmem); 1990 return -EFAULT; 1991 } 1992 1993 newumem = iotlb_alloc(); 1994 if (!newumem) { 1995 kvfree(newmem); 1996 return -ENOMEM; 1997 } 1998 1999 for (region = newmem->regions; 2000 region < newmem->regions + mem.nregions; 2001 region++) { 2002 if (vhost_iotlb_add_range(newumem, 2003 region->guest_phys_addr, 2004 region->guest_phys_addr + 2005 region->memory_size - 1, 2006 region->userspace_addr, 2007 VHOST_MAP_RW)) 2008 goto err; 2009 } 2010 2011 if (!memory_access_ok(d, newumem, 0)) 2012 goto err; 2013 2014 oldumem = d->umem; 2015 d->umem = newumem; 2016 2017 /* All memory accesses are done under some VQ mutex. */ 2018 for (i = 0; i < d->nvqs; ++i) { 2019 mutex_lock(&d->vqs[i]->mutex); 2020 d->vqs[i]->umem = newumem; 2021 mutex_unlock(&d->vqs[i]->mutex); 2022 } 2023 2024 kvfree(newmem); 2025 vhost_iotlb_free(oldumem); 2026 return 0; 2027 2028 err: 2029 vhost_iotlb_free(newumem); 2030 kvfree(newmem); 2031 return -EFAULT; 2032 } 2033 2034 static long vhost_vring_set_num(struct vhost_dev *d, 2035 struct vhost_virtqueue *vq, 2036 void __user *argp) 2037 { 2038 struct vhost_vring_state s; 2039 2040 /* Resizing ring with an active backend? 2041 * You don't want to do that. */ 2042 if (vq->private_data) 2043 return -EBUSY; 2044 2045 if (copy_from_user(&s, argp, sizeof s)) 2046 return -EFAULT; 2047 2048 if (!s.num || s.num > 0xffff || (s.num & (s.num - 1))) 2049 return -EINVAL; 2050 vq->num = s.num; 2051 2052 return 0; 2053 } 2054 2055 static long vhost_vring_set_addr(struct vhost_dev *d, 2056 struct vhost_virtqueue *vq, 2057 void __user *argp) 2058 { 2059 struct vhost_vring_addr a; 2060 2061 if (copy_from_user(&a, argp, sizeof a)) 2062 return -EFAULT; 2063 if (a.flags & ~(0x1 << VHOST_VRING_F_LOG)) 2064 return -EOPNOTSUPP; 2065 2066 /* For 32bit, verify that the top 32bits of the user 2067 data are set to zero. */ 2068 if ((u64)(unsigned long)a.desc_user_addr != a.desc_user_addr || 2069 (u64)(unsigned long)a.used_user_addr != a.used_user_addr || 2070 (u64)(unsigned long)a.avail_user_addr != a.avail_user_addr) 2071 return -EFAULT; 2072 2073 /* Make sure it's safe to cast pointers to vring types. */ 2074 BUILD_BUG_ON(__alignof__ *vq->avail > VRING_AVAIL_ALIGN_SIZE); 2075 BUILD_BUG_ON(__alignof__ *vq->used > VRING_USED_ALIGN_SIZE); 2076 if ((a.avail_user_addr & (VRING_AVAIL_ALIGN_SIZE - 1)) || 2077 (a.used_user_addr & (VRING_USED_ALIGN_SIZE - 1)) || 2078 (a.log_guest_addr & (VRING_USED_ALIGN_SIZE - 1))) 2079 return -EINVAL; 2080 2081 /* We only verify access here if backend is configured. 2082 * If it is not, we don't as size might not have been setup. 2083 * We will verify when backend is configured. */ 2084 if (vq->private_data) { 2085 if (!vq_access_ok(vq, vq->num, 2086 (void __user *)(unsigned long)a.desc_user_addr, 2087 (void __user *)(unsigned long)a.avail_user_addr, 2088 (void __user *)(unsigned long)a.used_user_addr)) 2089 return -EINVAL; 2090 2091 /* Also validate log access for used ring if enabled. */ 2092 if (!vq_log_used_access_ok(vq, vq->log_base, 2093 a.flags & (0x1 << VHOST_VRING_F_LOG), 2094 a.log_guest_addr)) 2095 return -EINVAL; 2096 } 2097 2098 vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG)); 2099 vq->desc = (void __user *)(unsigned long)a.desc_user_addr; 2100 vq->avail = (void __user *)(unsigned long)a.avail_user_addr; 2101 vq->log_addr = a.log_guest_addr; 2102 vq->used = (void __user *)(unsigned long)a.used_user_addr; 2103 2104 return 0; 2105 } 2106 2107 static long vhost_vring_set_num_addr(struct vhost_dev *d, 2108 struct vhost_virtqueue *vq, 2109 unsigned int ioctl, 2110 void __user *argp) 2111 { 2112 long r; 2113 2114 mutex_lock(&vq->mutex); 2115 2116 switch (ioctl) { 2117 case VHOST_SET_VRING_NUM: 2118 r = vhost_vring_set_num(d, vq, argp); 2119 break; 2120 case VHOST_SET_VRING_ADDR: 2121 r = vhost_vring_set_addr(d, vq, argp); 2122 break; 2123 default: 2124 BUG(); 2125 } 2126 2127 mutex_unlock(&vq->mutex); 2128 2129 return r; 2130 } 2131 long vhost_vring_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) 2132 { 2133 struct file *eventfp, *filep = NULL; 2134 bool pollstart = false, pollstop = false; 2135 struct eventfd_ctx *ctx = NULL; 2136 struct vhost_virtqueue *vq; 2137 struct vhost_vring_state s; 2138 struct vhost_vring_file f; 2139 u32 idx; 2140 long r; 2141 2142 r = vhost_get_vq_from_user(d, argp, &vq, &idx); 2143 if (r < 0) 2144 return r; 2145 2146 if (ioctl == VHOST_SET_VRING_NUM || 2147 ioctl == VHOST_SET_VRING_ADDR) { 2148 return vhost_vring_set_num_addr(d, vq, ioctl, argp); 2149 } 2150 2151 mutex_lock(&vq->mutex); 2152 2153 switch (ioctl) { 2154 case VHOST_SET_VRING_BASE: 2155 /* Moving base with an active backend? 2156 * You don't want to do that. */ 2157 if (vq->private_data) { 2158 r = -EBUSY; 2159 break; 2160 } 2161 if (copy_from_user(&s, argp, sizeof s)) { 2162 r = -EFAULT; 2163 break; 2164 } 2165 if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) { 2166 vq->next_avail_head = vq->last_avail_idx = 2167 s.num & 0xffff; 2168 vq->last_used_idx = (s.num >> 16) & 0xffff; 2169 } else { 2170 if (s.num > 0xffff) { 2171 r = -EINVAL; 2172 break; 2173 } 2174 vq->next_avail_head = vq->last_avail_idx = s.num; 2175 } 2176 /* Forget the cached index value. */ 2177 vq->avail_idx = vq->last_avail_idx; 2178 break; 2179 case VHOST_GET_VRING_BASE: 2180 s.index = idx; 2181 if (vhost_has_feature(vq, VIRTIO_F_RING_PACKED)) 2182 s.num = (u32)vq->last_avail_idx | ((u32)vq->last_used_idx << 16); 2183 else 2184 s.num = vq->last_avail_idx; 2185 if (copy_to_user(argp, &s, sizeof s)) 2186 r = -EFAULT; 2187 break; 2188 case VHOST_SET_VRING_KICK: 2189 if (copy_from_user(&f, argp, sizeof f)) { 2190 r = -EFAULT; 2191 break; 2192 } 2193 eventfp = f.fd == VHOST_FILE_UNBIND ? NULL : eventfd_fget(f.fd); 2194 if (IS_ERR(eventfp)) { 2195 r = PTR_ERR(eventfp); 2196 break; 2197 } 2198 if (eventfp != vq->kick) { 2199 pollstop = (filep = vq->kick) != NULL; 2200 pollstart = (vq->kick = eventfp) != NULL; 2201 } else 2202 filep = eventfp; 2203 break; 2204 case VHOST_SET_VRING_CALL: 2205 if (copy_from_user(&f, argp, sizeof f)) { 2206 r = -EFAULT; 2207 break; 2208 } 2209 ctx = f.fd == VHOST_FILE_UNBIND ? NULL : eventfd_ctx_fdget(f.fd); 2210 if (IS_ERR(ctx)) { 2211 r = PTR_ERR(ctx); 2212 break; 2213 } 2214 2215 swap(ctx, vq->call_ctx.ctx); 2216 break; 2217 case VHOST_SET_VRING_ERR: 2218 if (copy_from_user(&f, argp, sizeof f)) { 2219 r = -EFAULT; 2220 break; 2221 } 2222 ctx = f.fd == VHOST_FILE_UNBIND ? NULL : eventfd_ctx_fdget(f.fd); 2223 if (IS_ERR(ctx)) { 2224 r = PTR_ERR(ctx); 2225 break; 2226 } 2227 swap(ctx, vq->error_ctx); 2228 break; 2229 case VHOST_SET_VRING_ENDIAN: 2230 r = vhost_set_vring_endian(vq, argp); 2231 break; 2232 case VHOST_GET_VRING_ENDIAN: 2233 r = vhost_get_vring_endian(vq, idx, argp); 2234 break; 2235 case VHOST_SET_VRING_BUSYLOOP_TIMEOUT: 2236 if (copy_from_user(&s, argp, sizeof(s))) { 2237 r = -EFAULT; 2238 break; 2239 } 2240 vq->busyloop_timeout = s.num; 2241 break; 2242 case VHOST_GET_VRING_BUSYLOOP_TIMEOUT: 2243 s.index = idx; 2244 s.num = vq->busyloop_timeout; 2245 if (copy_to_user(argp, &s, sizeof(s))) 2246 r = -EFAULT; 2247 break; 2248 default: 2249 r = -ENOIOCTLCMD; 2250 } 2251 2252 if (pollstop && vq->handle_kick) 2253 vhost_poll_stop(&vq->poll); 2254 2255 if (!IS_ERR_OR_NULL(ctx)) 2256 eventfd_ctx_put(ctx); 2257 if (filep) 2258 fput(filep); 2259 2260 if (pollstart && vq->handle_kick) 2261 r = vhost_poll_start(&vq->poll, vq->kick); 2262 2263 mutex_unlock(&vq->mutex); 2264 2265 if (pollstop && vq->handle_kick) 2266 vhost_dev_flush(vq->poll.dev); 2267 return r; 2268 } 2269 EXPORT_SYMBOL_GPL(vhost_vring_ioctl); 2270 2271 int vhost_init_device_iotlb(struct vhost_dev *d) 2272 { 2273 struct vhost_iotlb *niotlb, *oiotlb; 2274 int i; 2275 2276 niotlb = iotlb_alloc(); 2277 if (!niotlb) 2278 return -ENOMEM; 2279 2280 oiotlb = d->iotlb; 2281 d->iotlb = niotlb; 2282 2283 for (i = 0; i < d->nvqs; ++i) { 2284 struct vhost_virtqueue *vq = d->vqs[i]; 2285 2286 mutex_lock(&vq->mutex); 2287 vq->iotlb = niotlb; 2288 __vhost_vq_meta_reset(vq); 2289 mutex_unlock(&vq->mutex); 2290 } 2291 2292 vhost_iotlb_free(oiotlb); 2293 2294 return 0; 2295 } 2296 EXPORT_SYMBOL_GPL(vhost_init_device_iotlb); 2297 2298 /* Caller must have device mutex */ 2299 long vhost_dev_ioctl(struct vhost_dev *d, unsigned int ioctl, void __user *argp) 2300 { 2301 struct eventfd_ctx *ctx; 2302 u64 p; 2303 long r; 2304 int i, fd; 2305 2306 /* If you are not the owner, you can become one */ 2307 if (ioctl == VHOST_SET_OWNER) { 2308 r = vhost_dev_set_owner(d); 2309 goto done; 2310 } 2311 2312 #ifdef CONFIG_VHOST_ENABLE_FORK_OWNER_CONTROL 2313 if (ioctl == VHOST_SET_FORK_FROM_OWNER) { 2314 /* Only allow modification before owner is set */ 2315 if (vhost_dev_has_owner(d)) { 2316 r = -EBUSY; 2317 goto done; 2318 } 2319 u8 fork_owner_val; 2320 2321 if (get_user(fork_owner_val, (u8 __user *)argp)) { 2322 r = -EFAULT; 2323 goto done; 2324 } 2325 if (fork_owner_val != VHOST_FORK_OWNER_TASK && 2326 fork_owner_val != VHOST_FORK_OWNER_KTHREAD) { 2327 r = -EINVAL; 2328 goto done; 2329 } 2330 d->fork_owner = !!fork_owner_val; 2331 r = 0; 2332 goto done; 2333 } 2334 if (ioctl == VHOST_GET_FORK_FROM_OWNER) { 2335 u8 fork_owner_val = d->fork_owner; 2336 2337 if (fork_owner_val != VHOST_FORK_OWNER_TASK && 2338 fork_owner_val != VHOST_FORK_OWNER_KTHREAD) { 2339 r = -EINVAL; 2340 goto done; 2341 } 2342 if (put_user(fork_owner_val, (u8 __user *)argp)) { 2343 r = -EFAULT; 2344 goto done; 2345 } 2346 r = 0; 2347 goto done; 2348 } 2349 #endif 2350 2351 /* You must be the owner to do anything else */ 2352 r = vhost_dev_check_owner(d); 2353 if (r) 2354 goto done; 2355 2356 switch (ioctl) { 2357 case VHOST_SET_MEM_TABLE: 2358 r = vhost_set_memory(d, argp); 2359 break; 2360 case VHOST_SET_LOG_BASE: 2361 if (copy_from_user(&p, argp, sizeof p)) { 2362 r = -EFAULT; 2363 break; 2364 } 2365 if ((u64)(unsigned long)p != p) { 2366 r = -EFAULT; 2367 break; 2368 } 2369 for (i = 0; i < d->nvqs; ++i) { 2370 struct vhost_virtqueue *vq; 2371 void __user *base = (void __user *)(unsigned long)p; 2372 vq = d->vqs[i]; 2373 mutex_lock(&vq->mutex); 2374 /* If ring is inactive, will check when it's enabled. */ 2375 if (vq->private_data && !vq_log_access_ok(vq, base)) 2376 r = -EFAULT; 2377 else 2378 vq->log_base = base; 2379 mutex_unlock(&vq->mutex); 2380 } 2381 break; 2382 case VHOST_SET_LOG_FD: 2383 r = get_user(fd, (int __user *)argp); 2384 if (r < 0) 2385 break; 2386 ctx = fd == VHOST_FILE_UNBIND ? NULL : eventfd_ctx_fdget(fd); 2387 if (IS_ERR(ctx)) { 2388 r = PTR_ERR(ctx); 2389 break; 2390 } 2391 swap(ctx, d->log_ctx); 2392 for (i = 0; i < d->nvqs; ++i) { 2393 mutex_lock(&d->vqs[i]->mutex); 2394 d->vqs[i]->log_ctx = d->log_ctx; 2395 mutex_unlock(&d->vqs[i]->mutex); 2396 } 2397 if (ctx) 2398 eventfd_ctx_put(ctx); 2399 break; 2400 default: 2401 r = -ENOIOCTLCMD; 2402 break; 2403 } 2404 done: 2405 return r; 2406 } 2407 EXPORT_SYMBOL_GPL(vhost_dev_ioctl); 2408 2409 /* TODO: This is really inefficient. We need something like get_user() 2410 * (instruction directly accesses the data, with an exception table entry 2411 * returning -EFAULT). See Documentation/arch/x86/exception-tables.rst. 2412 */ 2413 static int set_bit_to_user(int nr, void __user *addr) 2414 { 2415 unsigned long log = (unsigned long)addr; 2416 struct page *page; 2417 void *base; 2418 int bit = nr + (log % PAGE_SIZE) * 8; 2419 int r; 2420 2421 r = pin_user_pages_fast(log, 1, FOLL_WRITE, &page); 2422 if (r < 0) 2423 return r; 2424 BUG_ON(r != 1); 2425 base = kmap_atomic(page); 2426 set_bit(bit, base); 2427 kunmap_atomic(base); 2428 unpin_user_pages_dirty_lock(&page, 1, true); 2429 return 0; 2430 } 2431 2432 static int log_write(void __user *log_base, 2433 u64 write_address, u64 write_length) 2434 { 2435 u64 write_page = write_address / VHOST_PAGE_SIZE; 2436 int r; 2437 2438 if (!write_length) 2439 return 0; 2440 write_length += write_address % VHOST_PAGE_SIZE; 2441 for (;;) { 2442 u64 base = (u64)(unsigned long)log_base; 2443 u64 log = base + write_page / 8; 2444 int bit = write_page % 8; 2445 if ((u64)(unsigned long)log != log) 2446 return -EFAULT; 2447 r = set_bit_to_user(bit, (void __user *)(unsigned long)log); 2448 if (r < 0) 2449 return r; 2450 if (write_length <= VHOST_PAGE_SIZE) 2451 break; 2452 write_length -= VHOST_PAGE_SIZE; 2453 write_page += 1; 2454 } 2455 return r; 2456 } 2457 2458 static int log_write_hva(struct vhost_virtqueue *vq, u64 hva, u64 len) 2459 { 2460 struct vhost_iotlb *umem = vq->umem; 2461 struct vhost_iotlb_map *u; 2462 u64 start, end, l, min; 2463 int r; 2464 bool hit = false; 2465 2466 while (len) { 2467 min = len; 2468 /* More than one GPAs can be mapped into a single HVA. So 2469 * iterate all possible umems here to be safe. 2470 */ 2471 list_for_each_entry(u, &umem->list, link) { 2472 if (u->addr > hva - 1 + len || 2473 u->addr - 1 + u->size < hva) 2474 continue; 2475 start = max(u->addr, hva); 2476 end = min(u->addr - 1 + u->size, hva - 1 + len); 2477 l = end - start + 1; 2478 r = log_write(vq->log_base, 2479 u->start + start - u->addr, 2480 l); 2481 if (r < 0) 2482 return r; 2483 hit = true; 2484 min = min(l, min); 2485 } 2486 2487 if (!hit) 2488 return -EFAULT; 2489 2490 len -= min; 2491 hva += min; 2492 } 2493 2494 return 0; 2495 } 2496 2497 static int log_used(struct vhost_virtqueue *vq, u64 used_offset, u64 len) 2498 { 2499 struct iovec *iov = vq->log_iov; 2500 int i, ret; 2501 2502 if (!vq->iotlb) 2503 return log_write(vq->log_base, vq->log_addr + used_offset, len); 2504 2505 ret = translate_desc(vq, (uintptr_t)vq->used + used_offset, 2506 len, iov, 64, VHOST_ACCESS_WO); 2507 if (ret < 0) 2508 return ret; 2509 2510 for (i = 0; i < ret; i++) { 2511 ret = log_write_hva(vq, (uintptr_t)iov[i].iov_base, 2512 iov[i].iov_len); 2513 if (ret) 2514 return ret; 2515 } 2516 2517 return 0; 2518 } 2519 2520 /* 2521 * vhost_log_write() - Log in dirty page bitmap 2522 * @vq: vhost virtqueue. 2523 * @log: Array of dirty memory in GPA. 2524 * @log_num: Size of vhost_log arrary. 2525 * @len: The total length of memory buffer to log in the dirty bitmap. 2526 * Some drivers may only partially use pages shared via the last 2527 * vring descriptor (i.e. vhost-net RX buffer). 2528 * Use (len == U64_MAX) to indicate the driver would log all 2529 * pages of vring descriptors. 2530 * @iov: Array of dirty memory in HVA. 2531 * @count: Size of iovec array. 2532 */ 2533 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log, 2534 unsigned int log_num, u64 len, struct iovec *iov, int count) 2535 { 2536 int i, r; 2537 2538 /* Make sure data written is seen before log. */ 2539 smp_wmb(); 2540 2541 if (vq->iotlb) { 2542 for (i = 0; i < count; i++) { 2543 r = log_write_hva(vq, (uintptr_t)iov[i].iov_base, 2544 iov[i].iov_len); 2545 if (r < 0) 2546 return r; 2547 } 2548 return 0; 2549 } 2550 2551 for (i = 0; i < log_num; ++i) { 2552 u64 l = min(log[i].len, len); 2553 r = log_write(vq->log_base, log[i].addr, l); 2554 if (r < 0) 2555 return r; 2556 2557 if (len != U64_MAX) 2558 len -= l; 2559 } 2560 2561 if (vq->log_ctx) 2562 eventfd_signal(vq->log_ctx); 2563 2564 return 0; 2565 } 2566 EXPORT_SYMBOL_GPL(vhost_log_write); 2567 2568 static int vhost_update_used_flags(struct vhost_virtqueue *vq) 2569 { 2570 void __user *used; 2571 if (vhost_put_used_flags(vq)) 2572 return -EFAULT; 2573 if (unlikely(vq->log_used)) { 2574 /* Make sure the flag is seen before log. */ 2575 smp_wmb(); 2576 /* Log used flag write. */ 2577 used = &vq->used->flags; 2578 log_used(vq, (used - (void __user *)vq->used), 2579 sizeof vq->used->flags); 2580 if (vq->log_ctx) 2581 eventfd_signal(vq->log_ctx); 2582 } 2583 return 0; 2584 } 2585 2586 static int vhost_update_avail_event(struct vhost_virtqueue *vq) 2587 { 2588 if (vhost_put_avail_event(vq)) 2589 return -EFAULT; 2590 if (unlikely(vq->log_used)) { 2591 void __user *used; 2592 /* Make sure the event is seen before log. */ 2593 smp_wmb(); 2594 /* Log avail event write */ 2595 used = vhost_avail_event(vq); 2596 log_used(vq, (used - (void __user *)vq->used), 2597 sizeof *vhost_avail_event(vq)); 2598 if (vq->log_ctx) 2599 eventfd_signal(vq->log_ctx); 2600 } 2601 return 0; 2602 } 2603 2604 int vhost_vq_init_access(struct vhost_virtqueue *vq) 2605 { 2606 __virtio16 last_used_idx; 2607 int r; 2608 bool is_le = vq->is_le; 2609 2610 if (!vq->private_data) 2611 return 0; 2612 2613 vhost_init_is_le(vq); 2614 2615 r = vhost_update_used_flags(vq); 2616 if (r) 2617 goto err; 2618 vq->signalled_used_valid = false; 2619 if (!vq->iotlb && 2620 !access_ok(&vq->used->idx, sizeof vq->used->idx)) { 2621 r = -EFAULT; 2622 goto err; 2623 } 2624 r = vhost_get_used_idx(vq, &last_used_idx); 2625 if (r) { 2626 vq_err(vq, "Can't access used idx at %p\n", 2627 &vq->used->idx); 2628 goto err; 2629 } 2630 vq->last_used_idx = vhost16_to_cpu(vq, last_used_idx); 2631 return 0; 2632 2633 err: 2634 vq->is_le = is_le; 2635 return r; 2636 } 2637 EXPORT_SYMBOL_GPL(vhost_vq_init_access); 2638 2639 static int translate_desc(struct vhost_virtqueue *vq, u64 addr, u32 len, 2640 struct iovec iov[], int iov_size, int access) 2641 { 2642 const struct vhost_iotlb_map *map; 2643 struct vhost_dev *dev = vq->dev; 2644 struct vhost_iotlb *umem = dev->iotlb ? dev->iotlb : dev->umem; 2645 struct iovec *_iov; 2646 u64 s = 0, last = addr + len - 1; 2647 int ret = 0; 2648 2649 while ((u64)len > s) { 2650 u64 size; 2651 if (unlikely(ret >= iov_size)) { 2652 ret = -ENOBUFS; 2653 break; 2654 } 2655 2656 map = vhost_iotlb_itree_first(umem, addr, last); 2657 if (map == NULL || map->start > addr) { 2658 if (umem != dev->iotlb) { 2659 ret = -EFAULT; 2660 break; 2661 } 2662 ret = -EAGAIN; 2663 break; 2664 } else if (!(map->perm & access)) { 2665 ret = -EPERM; 2666 break; 2667 } 2668 2669 _iov = iov + ret; 2670 size = map->size - addr + map->start; 2671 _iov->iov_len = min((u64)len - s, size); 2672 _iov->iov_base = (void __user *)(unsigned long) 2673 (map->addr + addr - map->start); 2674 s += size; 2675 addr += size; 2676 ++ret; 2677 } 2678 2679 if (ret == -EAGAIN) 2680 vhost_iotlb_miss(vq, addr, access); 2681 return ret; 2682 } 2683 2684 /* Each buffer in the virtqueues is actually a chain of descriptors. This 2685 * function returns the next descriptor in the chain, 2686 * or -1U if we're at the end. */ 2687 static unsigned next_desc(struct vhost_virtqueue *vq, struct vring_desc *desc) 2688 { 2689 unsigned int next; 2690 2691 /* If this descriptor says it doesn't chain, we're done. */ 2692 if (!(desc->flags & cpu_to_vhost16(vq, VRING_DESC_F_NEXT))) 2693 return -1U; 2694 2695 /* Check they're not leading us off end of descriptors. */ 2696 next = vhost16_to_cpu(vq, READ_ONCE(desc->next)); 2697 return next; 2698 } 2699 2700 static int get_indirect(struct vhost_virtqueue *vq, 2701 struct iovec iov[], unsigned int iov_size, 2702 unsigned int *out_num, unsigned int *in_num, 2703 struct vhost_log *log, unsigned int *log_num, 2704 struct vring_desc *indirect) 2705 { 2706 struct vring_desc desc; 2707 unsigned int i = 0, count, found = 0; 2708 u32 len = vhost32_to_cpu(vq, indirect->len); 2709 struct iov_iter from; 2710 int ret, access; 2711 2712 /* Sanity check */ 2713 if (unlikely(len % sizeof desc)) { 2714 vq_err(vq, "Invalid length in indirect descriptor: " 2715 "len 0x%llx not multiple of 0x%zx\n", 2716 (unsigned long long)len, 2717 sizeof desc); 2718 return -EINVAL; 2719 } 2720 2721 ret = translate_desc(vq, vhost64_to_cpu(vq, indirect->addr), len, vq->indirect, 2722 UIO_MAXIOV, VHOST_ACCESS_RO); 2723 if (unlikely(ret < 0)) { 2724 if (ret != -EAGAIN) 2725 vq_err(vq, "Translation failure %d in indirect.\n", ret); 2726 return ret; 2727 } 2728 iov_iter_init(&from, ITER_SOURCE, vq->indirect, ret, len); 2729 count = len / sizeof desc; 2730 /* Buffers are chained via a 16 bit next field, so 2731 * we can have at most 2^16 of these. */ 2732 if (unlikely(count > USHRT_MAX + 1)) { 2733 vq_err(vq, "Indirect buffer length too big: %d\n", 2734 indirect->len); 2735 return -E2BIG; 2736 } 2737 2738 do { 2739 unsigned iov_count = *in_num + *out_num; 2740 if (unlikely(++found > count)) { 2741 vq_err(vq, "Loop detected: last one at %u " 2742 "indirect size %u\n", 2743 i, count); 2744 return -EINVAL; 2745 } 2746 if (unlikely(!copy_from_iter_full(&desc, sizeof(desc), &from))) { 2747 vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n", 2748 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc); 2749 return -EINVAL; 2750 } 2751 if (unlikely(desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT))) { 2752 vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n", 2753 i, (size_t)vhost64_to_cpu(vq, indirect->addr) + i * sizeof desc); 2754 return -EINVAL; 2755 } 2756 2757 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) 2758 access = VHOST_ACCESS_WO; 2759 else 2760 access = VHOST_ACCESS_RO; 2761 2762 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), 2763 vhost32_to_cpu(vq, desc.len), iov + iov_count, 2764 iov_size - iov_count, access); 2765 if (unlikely(ret < 0)) { 2766 if (ret != -EAGAIN) 2767 vq_err(vq, "Translation failure %d indirect idx %d\n", 2768 ret, i); 2769 return ret; 2770 } 2771 /* If this is an input descriptor, increment that count. */ 2772 if (access == VHOST_ACCESS_WO) { 2773 *in_num += ret; 2774 if (unlikely(log && ret)) { 2775 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); 2776 log[*log_num].len = vhost32_to_cpu(vq, desc.len); 2777 ++*log_num; 2778 } 2779 } else { 2780 /* If it's an output descriptor, they're all supposed 2781 * to come before any input descriptors. */ 2782 if (unlikely(*in_num)) { 2783 vq_err(vq, "Indirect descriptor " 2784 "has out after in: idx %d\n", i); 2785 return -EINVAL; 2786 } 2787 *out_num += ret; 2788 } 2789 } while ((i = next_desc(vq, &desc)) != -1); 2790 return 0; 2791 } 2792 2793 /** 2794 * vhost_get_vq_desc_n - Fetch the next available descriptor chain and build iovecs 2795 * @vq: target virtqueue 2796 * @iov: array that receives the scatter/gather segments 2797 * @iov_size: capacity of @iov in elements 2798 * @out_num: the number of output segments 2799 * @in_num: the number of input segments 2800 * @log: optional array to record addr/len for each writable segment; NULL if unused 2801 * @log_num: optional output; number of entries written to @log when provided 2802 * @ndesc: optional output; number of descriptors consumed from the available ring 2803 * (useful for rollback via vhost_discard_vq_desc) 2804 * 2805 * Extracts one available descriptor chain from @vq and translates guest addresses 2806 * into host iovecs. 2807 * 2808 * On success, advances @vq->last_avail_idx by 1 and @vq->next_avail_head by the 2809 * number of descriptors consumed (also stored via @ndesc when non-NULL). 2810 * 2811 * Return: 2812 * - head index in [0, @vq->num) on success; 2813 * - @vq->num if no descriptor is currently available; 2814 * - negative errno on failure 2815 */ 2816 int vhost_get_vq_desc_n(struct vhost_virtqueue *vq, 2817 struct iovec iov[], unsigned int iov_size, 2818 unsigned int *out_num, unsigned int *in_num, 2819 struct vhost_log *log, unsigned int *log_num, 2820 unsigned int *ndesc) 2821 { 2822 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER); 2823 struct vring_desc desc; 2824 unsigned int i, head, found = 0; 2825 u16 last_avail_idx = vq->last_avail_idx; 2826 __virtio16 ring_head; 2827 int ret, access, c = 0; 2828 2829 if (vq->avail_idx == vq->last_avail_idx) { 2830 ret = vhost_get_avail_idx(vq); 2831 if (unlikely(ret < 0)) 2832 return ret; 2833 2834 if (!ret) 2835 return vq->num; 2836 } 2837 2838 if (in_order) 2839 head = vq->next_avail_head & (vq->num - 1); 2840 else { 2841 /* Grab the next descriptor number they're 2842 * advertising, and increment the index we've seen. */ 2843 if (unlikely(vhost_get_avail_head(vq, &ring_head, 2844 last_avail_idx))) { 2845 vq_err(vq, "Failed to read head: idx %d address %p\n", 2846 last_avail_idx, 2847 &vq->avail->ring[last_avail_idx % vq->num]); 2848 return -EFAULT; 2849 } 2850 head = vhost16_to_cpu(vq, ring_head); 2851 } 2852 2853 /* If their number is silly, that's an error. */ 2854 if (unlikely(head >= vq->num)) { 2855 vq_err(vq, "Guest says index %u > %u is available", 2856 head, vq->num); 2857 return -EINVAL; 2858 } 2859 2860 /* When we start there are none of either input nor output. */ 2861 *out_num = *in_num = 0; 2862 if (unlikely(log)) 2863 *log_num = 0; 2864 2865 i = head; 2866 do { 2867 unsigned iov_count = *in_num + *out_num; 2868 if (unlikely(i >= vq->num)) { 2869 vq_err(vq, "Desc index is %u > %u, head = %u", 2870 i, vq->num, head); 2871 return -EINVAL; 2872 } 2873 if (unlikely(++found > vq->num)) { 2874 vq_err(vq, "Loop detected: last one at %u " 2875 "vq size %u head %u\n", 2876 i, vq->num, head); 2877 return -EINVAL; 2878 } 2879 ret = vhost_get_desc(vq, &desc, i); 2880 if (unlikely(ret)) { 2881 vq_err(vq, "Failed to get descriptor: idx %d addr %p\n", 2882 i, vq->desc + i); 2883 return -EFAULT; 2884 } 2885 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_INDIRECT)) { 2886 ret = get_indirect(vq, iov, iov_size, 2887 out_num, in_num, 2888 log, log_num, &desc); 2889 if (unlikely(ret < 0)) { 2890 if (ret != -EAGAIN) 2891 vq_err(vq, "Failure detected " 2892 "in indirect descriptor at idx %d\n", i); 2893 return ret; 2894 } 2895 ++c; 2896 continue; 2897 } 2898 2899 if (desc.flags & cpu_to_vhost16(vq, VRING_DESC_F_WRITE)) 2900 access = VHOST_ACCESS_WO; 2901 else 2902 access = VHOST_ACCESS_RO; 2903 ret = translate_desc(vq, vhost64_to_cpu(vq, desc.addr), 2904 vhost32_to_cpu(vq, desc.len), iov + iov_count, 2905 iov_size - iov_count, access); 2906 if (unlikely(ret < 0)) { 2907 if (ret != -EAGAIN) 2908 vq_err(vq, "Translation failure %d descriptor idx %d\n", 2909 ret, i); 2910 return ret; 2911 } 2912 if (access == VHOST_ACCESS_WO) { 2913 /* If this is an input descriptor, 2914 * increment that count. */ 2915 *in_num += ret; 2916 if (unlikely(log && ret)) { 2917 log[*log_num].addr = vhost64_to_cpu(vq, desc.addr); 2918 log[*log_num].len = vhost32_to_cpu(vq, desc.len); 2919 ++*log_num; 2920 } 2921 } else { 2922 /* If it's an output descriptor, they're all supposed 2923 * to come before any input descriptors. */ 2924 if (unlikely(*in_num)) { 2925 vq_err(vq, "Descriptor has out after in: " 2926 "idx %d\n", i); 2927 return -EINVAL; 2928 } 2929 *out_num += ret; 2930 } 2931 ++c; 2932 } while ((i = next_desc(vq, &desc)) != -1); 2933 2934 /* On success, increment avail index. */ 2935 vq->last_avail_idx++; 2936 vq->next_avail_head += c; 2937 2938 if (ndesc) 2939 *ndesc = c; 2940 2941 /* Assume notifications from guest are disabled at this point, 2942 * if they aren't we would need to update avail_event index. */ 2943 BUG_ON(!(vq->used_flags & VRING_USED_F_NO_NOTIFY)); 2944 return head; 2945 } 2946 EXPORT_SYMBOL_GPL(vhost_get_vq_desc_n); 2947 2948 /* This looks in the virtqueue and for the first available buffer, and converts 2949 * it to an iovec for convenient access. Since descriptors consist of some 2950 * number of output then some number of input descriptors, it's actually two 2951 * iovecs, but we pack them into one and note how many of each there were. 2952 * 2953 * This function returns the descriptor number found, or vq->num (which is 2954 * never a valid descriptor number) if none was found. A negative code is 2955 * returned on error. 2956 */ 2957 int vhost_get_vq_desc(struct vhost_virtqueue *vq, 2958 struct iovec iov[], unsigned int iov_size, 2959 unsigned int *out_num, unsigned int *in_num, 2960 struct vhost_log *log, unsigned int *log_num) 2961 { 2962 return vhost_get_vq_desc_n(vq, iov, iov_size, out_num, in_num, 2963 log, log_num, NULL); 2964 } 2965 EXPORT_SYMBOL_GPL(vhost_get_vq_desc); 2966 2967 /** 2968 * vhost_discard_vq_desc - Reverse the effect of vhost_get_vq_desc_n() 2969 * @vq: target virtqueue 2970 * @nbufs: number of buffers to roll back 2971 * @ndesc: number of descriptors to roll back 2972 * 2973 * Rewinds the internal consumer cursors after a failed attempt to use buffers 2974 * returned by vhost_get_vq_desc_n(). 2975 */ 2976 void vhost_discard_vq_desc(struct vhost_virtqueue *vq, int nbufs, 2977 unsigned int ndesc) 2978 { 2979 vq->next_avail_head -= ndesc; 2980 vq->last_avail_idx -= nbufs; 2981 } 2982 EXPORT_SYMBOL_GPL(vhost_discard_vq_desc); 2983 2984 /* After we've used one of their buffers, we tell them about it. We'll then 2985 * want to notify the guest, using eventfd. */ 2986 int vhost_add_used(struct vhost_virtqueue *vq, unsigned int head, int len) 2987 { 2988 struct vring_used_elem heads = { 2989 cpu_to_vhost32(vq, head), 2990 cpu_to_vhost32(vq, len) 2991 }; 2992 u16 nheads = 1; 2993 2994 return vhost_add_used_n(vq, &heads, &nheads, 1); 2995 } 2996 EXPORT_SYMBOL_GPL(vhost_add_used); 2997 2998 static int __vhost_add_used_n(struct vhost_virtqueue *vq, 2999 struct vring_used_elem *heads, 3000 unsigned count) 3001 { 3002 vring_used_elem_t __user *used; 3003 u16 old, new; 3004 int start; 3005 3006 start = vq->last_used_idx & (vq->num - 1); 3007 used = vq->used->ring + start; 3008 if (vhost_put_used(vq, heads, start, count)) { 3009 vq_err(vq, "Failed to write used"); 3010 return -EFAULT; 3011 } 3012 if (unlikely(vq->log_used)) { 3013 /* Make sure data is seen before log. */ 3014 smp_wmb(); 3015 /* Log used ring entry write. */ 3016 log_used(vq, ((void __user *)used - (void __user *)vq->used), 3017 count * sizeof *used); 3018 } 3019 old = vq->last_used_idx; 3020 new = (vq->last_used_idx += count); 3021 /* If the driver never bothers to signal in a very long while, 3022 * used index might wrap around. If that happens, invalidate 3023 * signalled_used index we stored. TODO: make sure driver 3024 * signals at least once in 2^16 and remove this. */ 3025 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old))) 3026 vq->signalled_used_valid = false; 3027 return 0; 3028 } 3029 3030 static int vhost_add_used_n_ooo(struct vhost_virtqueue *vq, 3031 struct vring_used_elem *heads, 3032 unsigned count) 3033 { 3034 int start, n, r; 3035 3036 start = vq->last_used_idx & (vq->num - 1); 3037 n = vq->num - start; 3038 if (n < count) { 3039 r = __vhost_add_used_n(vq, heads, n); 3040 if (r < 0) 3041 return r; 3042 heads += n; 3043 count -= n; 3044 } 3045 return __vhost_add_used_n(vq, heads, count); 3046 } 3047 3048 static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq, 3049 struct vring_used_elem *heads, 3050 const u16 *nheads, 3051 unsigned count) 3052 { 3053 vring_used_elem_t __user *used; 3054 u16 old, new = vq->last_used_idx; 3055 int start, i; 3056 3057 if (!nheads) 3058 return -EINVAL; 3059 3060 start = vq->last_used_idx & (vq->num - 1); 3061 used = vq->used->ring + start; 3062 3063 for (i = 0; i < count; i++) { 3064 if (vhost_put_used(vq, &heads[i], start, 1)) { 3065 vq_err(vq, "Failed to write used"); 3066 return -EFAULT; 3067 } 3068 start += nheads[i]; 3069 new += nheads[i]; 3070 if (start >= vq->num) 3071 start -= vq->num; 3072 } 3073 3074 if (unlikely(vq->log_used)) { 3075 /* Make sure data is seen before log. */ 3076 smp_wmb(); 3077 /* Log used ring entry write. */ 3078 log_used(vq, ((void __user *)used - (void __user *)vq->used), 3079 (vq->num - start) * sizeof *used); 3080 if (start + count > vq->num) 3081 log_used(vq, 0, 3082 (start + count - vq->num) * sizeof *used); 3083 } 3084 3085 old = vq->last_used_idx; 3086 vq->last_used_idx = new; 3087 /* If the driver never bothers to signal in a very long while, 3088 * used index might wrap around. If that happens, invalidate 3089 * signalled_used index we stored. TODO: make sure driver 3090 * signals at least once in 2^16 and remove this. */ 3091 if (unlikely((u16)(new - vq->signalled_used) < (u16)(new - old))) 3092 vq->signalled_used_valid = false; 3093 return 0; 3094 } 3095 3096 /* After we've used one of their buffers, we tell them about it. We'll then 3097 * want to notify the guest, using eventfd. */ 3098 int vhost_add_used_n(struct vhost_virtqueue *vq, struct vring_used_elem *heads, 3099 u16 *nheads, unsigned count) 3100 { 3101 bool in_order = vhost_has_feature(vq, VIRTIO_F_IN_ORDER); 3102 int r; 3103 3104 if (!in_order || !nheads) 3105 r = vhost_add_used_n_ooo(vq, heads, count); 3106 else 3107 r = vhost_add_used_n_in_order(vq, heads, nheads, count); 3108 3109 if (r < 0) 3110 return r; 3111 3112 /* Make sure buffer is written before we update index. */ 3113 smp_wmb(); 3114 if (vhost_put_used_idx(vq)) { 3115 vq_err(vq, "Failed to increment used idx"); 3116 return -EFAULT; 3117 } 3118 if (unlikely(vq->log_used)) { 3119 /* Make sure used idx is seen before log. */ 3120 smp_wmb(); 3121 /* Log used index update. */ 3122 log_used(vq, offsetof(struct vring_used, idx), 3123 sizeof vq->used->idx); 3124 if (vq->log_ctx) 3125 eventfd_signal(vq->log_ctx); 3126 } 3127 return r; 3128 } 3129 EXPORT_SYMBOL_GPL(vhost_add_used_n); 3130 3131 static bool vhost_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) 3132 { 3133 __u16 old, new; 3134 __virtio16 event; 3135 bool v; 3136 /* Flush out used index updates. This is paired 3137 * with the barrier that the Guest executes when enabling 3138 * interrupts. */ 3139 smp_mb(); 3140 3141 if (vhost_has_feature(vq, VIRTIO_F_NOTIFY_ON_EMPTY) && 3142 unlikely(vq->avail_idx == vq->last_avail_idx)) 3143 return true; 3144 3145 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { 3146 __virtio16 flags; 3147 if (vhost_get_avail_flags(vq, &flags)) { 3148 vq_err(vq, "Failed to get flags"); 3149 return true; 3150 } 3151 return !(flags & cpu_to_vhost16(vq, VRING_AVAIL_F_NO_INTERRUPT)); 3152 } 3153 old = vq->signalled_used; 3154 v = vq->signalled_used_valid; 3155 new = vq->signalled_used = vq->last_used_idx; 3156 vq->signalled_used_valid = true; 3157 3158 if (unlikely(!v)) 3159 return true; 3160 3161 if (vhost_get_used_event(vq, &event)) { 3162 vq_err(vq, "Failed to get used event idx"); 3163 return true; 3164 } 3165 return vring_need_event(vhost16_to_cpu(vq, event), new, old); 3166 } 3167 3168 /* This actually signals the guest, using eventfd. */ 3169 void vhost_signal(struct vhost_dev *dev, struct vhost_virtqueue *vq) 3170 { 3171 /* Signal the Guest tell them we used something up. */ 3172 if (vq->call_ctx.ctx && vhost_notify(dev, vq)) 3173 eventfd_signal(vq->call_ctx.ctx); 3174 } 3175 EXPORT_SYMBOL_GPL(vhost_signal); 3176 3177 /* And here's the combo meal deal. Supersize me! */ 3178 void vhost_add_used_and_signal(struct vhost_dev *dev, 3179 struct vhost_virtqueue *vq, 3180 unsigned int head, int len) 3181 { 3182 vhost_add_used(vq, head, len); 3183 vhost_signal(dev, vq); 3184 } 3185 EXPORT_SYMBOL_GPL(vhost_add_used_and_signal); 3186 3187 /* multi-buffer version of vhost_add_used_and_signal */ 3188 void vhost_add_used_and_signal_n(struct vhost_dev *dev, 3189 struct vhost_virtqueue *vq, 3190 struct vring_used_elem *heads, 3191 u16 *nheads, 3192 unsigned count) 3193 { 3194 vhost_add_used_n(vq, heads, nheads, count); 3195 vhost_signal(dev, vq); 3196 } 3197 EXPORT_SYMBOL_GPL(vhost_add_used_and_signal_n); 3198 3199 /* return true if we're sure that available ring is empty */ 3200 bool vhost_vq_avail_empty(struct vhost_dev *dev, struct vhost_virtqueue *vq) 3201 { 3202 int r; 3203 3204 if (vq->avail_idx != vq->last_avail_idx) 3205 return false; 3206 3207 r = vhost_get_avail_idx(vq); 3208 3209 /* Note: we treat error as non-empty here */ 3210 return r == 0; 3211 } 3212 EXPORT_SYMBOL_GPL(vhost_vq_avail_empty); 3213 3214 /* OK, now we need to know about added descriptors. */ 3215 bool vhost_enable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) 3216 { 3217 int r; 3218 3219 if (!(vq->used_flags & VRING_USED_F_NO_NOTIFY)) 3220 return false; 3221 vq->used_flags &= ~VRING_USED_F_NO_NOTIFY; 3222 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { 3223 r = vhost_update_used_flags(vq); 3224 if (r) { 3225 vq_err(vq, "Failed to enable notification at %p: %d\n", 3226 &vq->used->flags, r); 3227 return false; 3228 } 3229 } else { 3230 r = vhost_update_avail_event(vq); 3231 if (r) { 3232 vq_err(vq, "Failed to update avail event index at %p: %d\n", 3233 vhost_avail_event(vq), r); 3234 return false; 3235 } 3236 } 3237 /* They could have slipped one in as we were doing that: make 3238 * sure it's written, then check again. */ 3239 smp_mb(); 3240 3241 r = vhost_get_avail_idx(vq); 3242 /* Note: we treat error as empty here */ 3243 if (unlikely(r < 0)) 3244 return false; 3245 3246 return r; 3247 } 3248 EXPORT_SYMBOL_GPL(vhost_enable_notify); 3249 3250 /* We don't need to be notified again. */ 3251 void vhost_disable_notify(struct vhost_dev *dev, struct vhost_virtqueue *vq) 3252 { 3253 int r; 3254 3255 if (vq->used_flags & VRING_USED_F_NO_NOTIFY) 3256 return; 3257 vq->used_flags |= VRING_USED_F_NO_NOTIFY; 3258 if (!vhost_has_feature(vq, VIRTIO_RING_F_EVENT_IDX)) { 3259 r = vhost_update_used_flags(vq); 3260 if (r) 3261 vq_err(vq, "Failed to disable notification at %p: %d\n", 3262 &vq->used->flags, r); 3263 } 3264 } 3265 EXPORT_SYMBOL_GPL(vhost_disable_notify); 3266 3267 /* Create a new message. */ 3268 struct vhost_msg_node *vhost_new_msg(struct vhost_virtqueue *vq, int type) 3269 { 3270 /* Make sure all padding within the structure is initialized. */ 3271 struct vhost_msg_node *node = kzalloc_obj(*node, GFP_KERNEL); 3272 if (!node) 3273 return NULL; 3274 3275 node->vq = vq; 3276 node->msg.type = type; 3277 return node; 3278 } 3279 EXPORT_SYMBOL_GPL(vhost_new_msg); 3280 3281 void vhost_enqueue_msg(struct vhost_dev *dev, struct list_head *head, 3282 struct vhost_msg_node *node) 3283 { 3284 spin_lock(&dev->iotlb_lock); 3285 list_add_tail(&node->node, head); 3286 spin_unlock(&dev->iotlb_lock); 3287 3288 wake_up_interruptible_poll(&dev->wait, EPOLLIN | EPOLLRDNORM); 3289 } 3290 EXPORT_SYMBOL_GPL(vhost_enqueue_msg); 3291 3292 struct vhost_msg_node *vhost_dequeue_msg(struct vhost_dev *dev, 3293 struct list_head *head) 3294 { 3295 struct vhost_msg_node *node = NULL; 3296 3297 spin_lock(&dev->iotlb_lock); 3298 if (!list_empty(head)) { 3299 node = list_first_entry(head, struct vhost_msg_node, 3300 node); 3301 list_del(&node->node); 3302 } 3303 spin_unlock(&dev->iotlb_lock); 3304 3305 return node; 3306 } 3307 EXPORT_SYMBOL_GPL(vhost_dequeue_msg); 3308 3309 void vhost_set_backend_features(struct vhost_dev *dev, u64 features) 3310 { 3311 struct vhost_virtqueue *vq; 3312 int i; 3313 3314 mutex_lock(&dev->mutex); 3315 for (i = 0; i < dev->nvqs; ++i) { 3316 vq = dev->vqs[i]; 3317 mutex_lock(&vq->mutex); 3318 vq->acked_backend_features = features; 3319 mutex_unlock(&vq->mutex); 3320 } 3321 mutex_unlock(&dev->mutex); 3322 } 3323 EXPORT_SYMBOL_GPL(vhost_set_backend_features); 3324 3325 static int __init vhost_init(void) 3326 { 3327 return 0; 3328 } 3329 3330 static void __exit vhost_exit(void) 3331 { 3332 } 3333 3334 module_init(vhost_init); 3335 module_exit(vhost_exit); 3336 3337 MODULE_VERSION("0.0.1"); 3338 MODULE_LICENSE("GPL v2"); 3339 MODULE_AUTHOR("Michael S. Tsirkin"); 3340 MODULE_DESCRIPTION("Host kernel accelerator for virtio"); 3341