1 /****************************************************************************** 2 * gntdev.c 3 * 4 * Device for accessing (in user-space) pages that have been granted by other 5 * domains. 6 * 7 * Copyright (c) 2006-2007, D G Murray. 8 * (c) 2009 Gerd Hoffmann <kraxel@redhat.com> 9 * (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 21 #undef DEBUG 22 23 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt 24 25 #include <linux/dma-mapping.h> 26 #include <linux/module.h> 27 #include <linux/kernel.h> 28 #include <linux/init.h> 29 #include <linux/miscdevice.h> 30 #include <linux/fs.h> 31 #include <linux/uaccess.h> 32 #include <linux/sched.h> 33 #include <linux/sched/mm.h> 34 #include <linux/spinlock.h> 35 #include <linux/slab.h> 36 #include <linux/highmem.h> 37 #include <linux/refcount.h> 38 #include <linux/workqueue.h> 39 40 #include <xen/xen.h> 41 #include <xen/grant_table.h> 42 #include <xen/balloon.h> 43 #include <xen/gntdev.h> 44 #include <xen/events.h> 45 #include <xen/page.h> 46 #include <asm/xen/hypervisor.h> 47 #include <asm/xen/hypercall.h> 48 49 #include "gntdev-common.h" 50 #ifdef CONFIG_XEN_GNTDEV_DMABUF 51 #include "gntdev-dmabuf.h" 52 #endif 53 54 MODULE_LICENSE("GPL"); 55 MODULE_AUTHOR("Derek G. Murray <Derek.Murray@cl.cam.ac.uk>, " 56 "Gerd Hoffmann <kraxel@redhat.com>"); 57 MODULE_DESCRIPTION("User-space granted page access driver"); 58 59 #define GNTDEV_COPY_BATCH 16 60 61 struct gntdev_copy_batch { 62 struct gnttab_copy ops[GNTDEV_COPY_BATCH]; 63 struct page *pages[GNTDEV_COPY_BATCH]; 64 s16 __user *status[GNTDEV_COPY_BATCH]; 65 unsigned int nr_ops; 66 unsigned int nr_pages; 67 bool writeable; 68 struct gntdev_copy_batch *next; 69 }; 70 71 static unsigned int limit = 64*1024; 72 module_param(limit, uint, 0644); 73 MODULE_PARM_DESC(limit, 74 "Maximum number of grants that may be mapped by one mapping request"); 75 76 /* True in PV mode, false otherwise */ 77 static int use_ptemod; 78 79 static void unmap_grant_pages(struct gntdev_grant_map *map, 80 int offset, int pages); 81 82 static struct miscdevice gntdev_miscdev; 83 84 /* ------------------------------------------------------------------ */ 85 86 bool gntdev_test_page_count(unsigned int count) 87 { 88 return !count || count > limit; 89 } 90 91 static void gntdev_print_maps(struct gntdev_priv *priv, 92 char *text, int text_index) 93 { 94 #ifdef DEBUG 95 struct gntdev_grant_map *map; 96 97 pr_debug("%s: maps list (priv %p)\n", __func__, priv); 98 list_for_each_entry(map, &priv->maps, next) 99 pr_debug(" index %2d, count %2d %s\n", 100 map->index, map->count, 101 map->index == text_index && text ? text : ""); 102 #endif 103 } 104 105 static void gntdev_free_map(struct gntdev_grant_map *map) 106 { 107 if (map == NULL) 108 return; 109 110 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC 111 if (map->dma_vaddr) { 112 struct gnttab_dma_alloc_args args; 113 114 args.dev = map->dma_dev; 115 args.coherent = !!(map->dma_flags & GNTDEV_DMA_FLAG_COHERENT); 116 args.nr_pages = map->count; 117 args.pages = map->pages; 118 args.frames = map->frames; 119 args.vaddr = map->dma_vaddr; 120 args.dev_bus_addr = map->dma_bus_addr; 121 122 gnttab_dma_free_pages(&args); 123 } else 124 #endif 125 if (map->pages) 126 gnttab_free_pages(map->count, map->pages); 127 128 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC 129 kvfree(map->frames); 130 #endif 131 kvfree(map->pages); 132 kvfree(map->grants); 133 kvfree(map->map_ops); 134 kvfree(map->unmap_ops); 135 kvfree(map->kmap_ops); 136 kvfree(map->kunmap_ops); 137 kvfree(map->being_removed); 138 kfree(map); 139 } 140 141 struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count, 142 int dma_flags) 143 { 144 struct gntdev_grant_map *add; 145 int i; 146 147 add = kzalloc(sizeof(*add), GFP_KERNEL); 148 if (NULL == add) 149 return NULL; 150 151 add->grants = kvmalloc_array(count, sizeof(add->grants[0]), 152 GFP_KERNEL); 153 add->map_ops = kvmalloc_array(count, sizeof(add->map_ops[0]), 154 GFP_KERNEL); 155 add->unmap_ops = kvmalloc_array(count, sizeof(add->unmap_ops[0]), 156 GFP_KERNEL); 157 add->pages = kvcalloc(count, sizeof(add->pages[0]), GFP_KERNEL); 158 add->being_removed = 159 kvcalloc(count, sizeof(add->being_removed[0]), GFP_KERNEL); 160 if (NULL == add->grants || 161 NULL == add->map_ops || 162 NULL == add->unmap_ops || 163 NULL == add->pages || 164 NULL == add->being_removed) 165 goto err; 166 if (use_ptemod) { 167 add->kmap_ops = kvmalloc_array(count, sizeof(add->kmap_ops[0]), 168 GFP_KERNEL); 169 add->kunmap_ops = kvmalloc_array(count, sizeof(add->kunmap_ops[0]), 170 GFP_KERNEL); 171 if (NULL == add->kmap_ops || NULL == add->kunmap_ops) 172 goto err; 173 } 174 175 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC 176 add->dma_flags = dma_flags; 177 178 /* 179 * Check if this mapping is requested to be backed 180 * by a DMA buffer. 181 */ 182 if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT)) { 183 struct gnttab_dma_alloc_args args; 184 185 add->frames = kvcalloc(count, sizeof(add->frames[0]), 186 GFP_KERNEL); 187 if (!add->frames) 188 goto err; 189 190 /* Remember the device, so we can free DMA memory. */ 191 add->dma_dev = priv->dma_dev; 192 193 args.dev = priv->dma_dev; 194 args.coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT); 195 args.nr_pages = count; 196 args.pages = add->pages; 197 args.frames = add->frames; 198 199 if (gnttab_dma_alloc_pages(&args)) 200 goto err; 201 202 add->dma_vaddr = args.vaddr; 203 add->dma_bus_addr = args.dev_bus_addr; 204 } else 205 #endif 206 if (gnttab_alloc_pages(count, add->pages)) 207 goto err; 208 209 for (i = 0; i < count; i++) { 210 add->grants[i].domid = DOMID_INVALID; 211 add->grants[i].ref = INVALID_GRANT_REF; 212 add->map_ops[i].handle = INVALID_GRANT_HANDLE; 213 add->unmap_ops[i].handle = INVALID_GRANT_HANDLE; 214 if (use_ptemod) { 215 add->kmap_ops[i].handle = INVALID_GRANT_HANDLE; 216 add->kunmap_ops[i].handle = INVALID_GRANT_HANDLE; 217 } 218 } 219 220 add->index = 0; 221 add->count = count; 222 refcount_set(&add->users, 1); 223 224 return add; 225 226 err: 227 gntdev_free_map(add); 228 return NULL; 229 } 230 231 void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add) 232 { 233 struct gntdev_grant_map *map; 234 235 list_for_each_entry(map, &priv->maps, next) { 236 if (add->index + add->count < map->index) { 237 list_add_tail(&add->next, &map->next); 238 goto done; 239 } 240 add->index = map->index + map->count; 241 } 242 list_add_tail(&add->next, &priv->maps); 243 244 done: 245 gntdev_print_maps(priv, "[new]", add->index); 246 } 247 248 static struct gntdev_grant_map *gntdev_find_map_index(struct gntdev_priv *priv, 249 int index, int count) 250 { 251 struct gntdev_grant_map *map; 252 253 list_for_each_entry(map, &priv->maps, next) { 254 if (map->index != index) 255 continue; 256 if (count && map->count != count) 257 continue; 258 return map; 259 } 260 return NULL; 261 } 262 263 void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map) 264 { 265 if (!map) 266 return; 267 268 if (!refcount_dec_and_test(&map->users)) 269 return; 270 271 if (map->pages && !use_ptemod) { 272 /* 273 * Increment the reference count. This ensures that the 274 * subsequent call to unmap_grant_pages() will not wind up 275 * re-entering itself. It *can* wind up calling 276 * gntdev_put_map() recursively, but such calls will be with a 277 * reference count greater than 1, so they will return before 278 * this code is reached. The recursion depth is thus limited to 279 * 1. Do NOT use refcount_inc() here, as it will detect that 280 * the reference count is zero and WARN(). 281 */ 282 refcount_set(&map->users, 1); 283 284 /* 285 * Unmap the grants. This may or may not be asynchronous, so it 286 * is possible that the reference count is 1 on return, but it 287 * could also be greater than 1. 288 */ 289 unmap_grant_pages(map, 0, map->count); 290 291 /* Check if the memory now needs to be freed */ 292 if (!refcount_dec_and_test(&map->users)) 293 return; 294 295 /* 296 * All pages have been returned to the hypervisor, so free the 297 * map. 298 */ 299 } 300 301 if (use_ptemod && map->notifier_init) 302 mmu_interval_notifier_remove(&map->notifier); 303 304 if (map->notify.flags & UNMAP_NOTIFY_SEND_EVENT) { 305 notify_remote_via_evtchn(map->notify.event); 306 evtchn_put(map->notify.event); 307 } 308 gntdev_free_map(map); 309 } 310 311 /* ------------------------------------------------------------------ */ 312 313 static int find_grant_ptes(pte_t *pte, unsigned long addr, void *data) 314 { 315 struct gntdev_grant_map *map = data; 316 unsigned int pgnr = (addr - map->pages_vm_start) >> PAGE_SHIFT; 317 int flags = map->flags | GNTMAP_application_map | GNTMAP_contains_pte | 318 (1 << _GNTMAP_guest_avail0); 319 u64 pte_maddr; 320 321 BUG_ON(pgnr >= map->count); 322 pte_maddr = arbitrary_virt_to_machine(pte).maddr; 323 324 /* Note: this will perform a pte_mkspecial() through the hypercall. */ 325 gnttab_set_map_op(&map->map_ops[pgnr], pte_maddr, flags, 326 map->grants[pgnr].ref, 327 map->grants[pgnr].domid); 328 gnttab_set_unmap_op(&map->unmap_ops[pgnr], pte_maddr, flags, 329 INVALID_GRANT_HANDLE); 330 return 0; 331 } 332 333 int gntdev_map_grant_pages(struct gntdev_grant_map *map) 334 { 335 size_t alloced = 0; 336 int i, err = 0; 337 338 if (!use_ptemod) { 339 /* Note: it could already be mapped */ 340 if (map->map_ops[0].handle != INVALID_GRANT_HANDLE) 341 return 0; 342 for (i = 0; i < map->count; i++) { 343 unsigned long addr = (unsigned long) 344 pfn_to_kaddr(page_to_pfn(map->pages[i])); 345 gnttab_set_map_op(&map->map_ops[i], addr, map->flags, 346 map->grants[i].ref, 347 map->grants[i].domid); 348 gnttab_set_unmap_op(&map->unmap_ops[i], addr, 349 map->flags, INVALID_GRANT_HANDLE); 350 } 351 } else { 352 /* 353 * Setup the map_ops corresponding to the pte entries pointing 354 * to the kernel linear addresses of the struct pages. 355 * These ptes are completely different from the user ptes dealt 356 * with find_grant_ptes. 357 * Note that GNTMAP_device_map isn't needed here: The 358 * dev_bus_addr output field gets consumed only from ->map_ops, 359 * and by not requesting it when mapping we also avoid needing 360 * to mirror dev_bus_addr into ->unmap_ops (and holding an extra 361 * reference to the page in the hypervisor). 362 */ 363 unsigned int flags = (map->flags & ~GNTMAP_device_map) | 364 GNTMAP_host_map; 365 366 for (i = 0; i < map->count; i++) { 367 unsigned long address = (unsigned long) 368 pfn_to_kaddr(page_to_pfn(map->pages[i])); 369 BUG_ON(PageHighMem(map->pages[i])); 370 371 gnttab_set_map_op(&map->kmap_ops[i], address, flags, 372 map->grants[i].ref, 373 map->grants[i].domid); 374 gnttab_set_unmap_op(&map->kunmap_ops[i], address, 375 flags, INVALID_GRANT_HANDLE); 376 } 377 } 378 379 pr_debug("map %d+%d\n", map->index, map->count); 380 err = gnttab_map_refs(map->map_ops, map->kmap_ops, map->pages, 381 map->count); 382 383 for (i = 0; i < map->count; i++) { 384 if (map->map_ops[i].status == GNTST_okay) { 385 map->unmap_ops[i].handle = map->map_ops[i].handle; 386 alloced++; 387 } else if (!err) 388 err = -EINVAL; 389 390 if (map->flags & GNTMAP_device_map) 391 map->unmap_ops[i].dev_bus_addr = map->map_ops[i].dev_bus_addr; 392 393 if (use_ptemod) { 394 if (map->kmap_ops[i].status == GNTST_okay) { 395 alloced++; 396 map->kunmap_ops[i].handle = map->kmap_ops[i].handle; 397 } else if (!err) 398 err = -EINVAL; 399 } 400 } 401 atomic_add(alloced, &map->live_grants); 402 return err; 403 } 404 405 static void __unmap_grant_pages_done(int result, 406 struct gntab_unmap_queue_data *data) 407 { 408 unsigned int i; 409 struct gntdev_grant_map *map = data->data; 410 unsigned int offset = data->unmap_ops - map->unmap_ops; 411 int successful_unmaps = 0; 412 int live_grants; 413 414 for (i = 0; i < data->count; i++) { 415 if (map->unmap_ops[offset + i].status == GNTST_okay && 416 map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE) 417 successful_unmaps++; 418 419 WARN_ON(map->unmap_ops[offset + i].status != GNTST_okay && 420 map->unmap_ops[offset + i].handle != INVALID_GRANT_HANDLE); 421 pr_debug("unmap handle=%d st=%d\n", 422 map->unmap_ops[offset+i].handle, 423 map->unmap_ops[offset+i].status); 424 map->unmap_ops[offset+i].handle = INVALID_GRANT_HANDLE; 425 if (use_ptemod) { 426 if (map->kunmap_ops[offset + i].status == GNTST_okay && 427 map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE) 428 successful_unmaps++; 429 430 WARN_ON(map->kunmap_ops[offset + i].status != GNTST_okay && 431 map->kunmap_ops[offset + i].handle != INVALID_GRANT_HANDLE); 432 pr_debug("kunmap handle=%u st=%d\n", 433 map->kunmap_ops[offset+i].handle, 434 map->kunmap_ops[offset+i].status); 435 map->kunmap_ops[offset+i].handle = INVALID_GRANT_HANDLE; 436 } 437 } 438 439 /* 440 * Decrease the live-grant counter. This must happen after the loop to 441 * prevent premature reuse of the grants by gnttab_mmap(). 442 */ 443 live_grants = atomic_sub_return(successful_unmaps, &map->live_grants); 444 if (WARN_ON(live_grants < 0)) 445 pr_err("%s: live_grants became negative (%d) after unmapping %d pages!\n", 446 __func__, live_grants, successful_unmaps); 447 448 /* Release reference taken by __unmap_grant_pages */ 449 gntdev_put_map(NULL, map); 450 } 451 452 static void __unmap_grant_pages(struct gntdev_grant_map *map, int offset, 453 int pages) 454 { 455 if (map->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) { 456 int pgno = (map->notify.addr >> PAGE_SHIFT); 457 458 if (pgno >= offset && pgno < offset + pages) { 459 /* No need for kmap, pages are in lowmem */ 460 uint8_t *tmp = pfn_to_kaddr(page_to_pfn(map->pages[pgno])); 461 462 tmp[map->notify.addr & (PAGE_SIZE-1)] = 0; 463 map->notify.flags &= ~UNMAP_NOTIFY_CLEAR_BYTE; 464 } 465 } 466 467 map->unmap_data.unmap_ops = map->unmap_ops + offset; 468 map->unmap_data.kunmap_ops = use_ptemod ? map->kunmap_ops + offset : NULL; 469 map->unmap_data.pages = map->pages + offset; 470 map->unmap_data.count = pages; 471 map->unmap_data.done = __unmap_grant_pages_done; 472 map->unmap_data.data = map; 473 refcount_inc(&map->users); /* to keep map alive during async call below */ 474 475 gnttab_unmap_refs_async(&map->unmap_data); 476 } 477 478 static void unmap_grant_pages(struct gntdev_grant_map *map, int offset, 479 int pages) 480 { 481 int range; 482 483 if (atomic_read(&map->live_grants) == 0) 484 return; /* Nothing to do */ 485 486 pr_debug("unmap %d+%d [%d+%d]\n", map->index, map->count, offset, pages); 487 488 /* It is possible the requested range will have a "hole" where we 489 * already unmapped some of the grants. Only unmap valid ranges. 490 */ 491 while (pages) { 492 while (pages && map->being_removed[offset]) { 493 offset++; 494 pages--; 495 } 496 range = 0; 497 while (range < pages) { 498 if (map->being_removed[offset + range]) 499 break; 500 map->being_removed[offset + range] = true; 501 range++; 502 } 503 if (range) 504 __unmap_grant_pages(map, offset, range); 505 offset += range; 506 pages -= range; 507 } 508 } 509 510 /* ------------------------------------------------------------------ */ 511 512 static void gntdev_vma_open(struct vm_area_struct *vma) 513 { 514 struct gntdev_grant_map *map = vma->vm_private_data; 515 516 pr_debug("gntdev_vma_open %p\n", vma); 517 refcount_inc(&map->users); 518 } 519 520 static void gntdev_vma_close(struct vm_area_struct *vma) 521 { 522 struct gntdev_grant_map *map = vma->vm_private_data; 523 struct file *file = vma->vm_file; 524 struct gntdev_priv *priv = file->private_data; 525 526 pr_debug("gntdev_vma_close %p\n", vma); 527 528 vma->vm_private_data = NULL; 529 gntdev_put_map(priv, map); 530 } 531 532 static struct page *gntdev_vma_find_normal_page(struct vm_area_struct *vma, 533 unsigned long addr) 534 { 535 struct gntdev_grant_map *map = vma->vm_private_data; 536 537 return map->pages[(addr - map->pages_vm_start) >> PAGE_SHIFT]; 538 } 539 540 static const struct vm_operations_struct gntdev_vmops = { 541 .open = gntdev_vma_open, 542 .close = gntdev_vma_close, 543 .find_normal_page = gntdev_vma_find_normal_page, 544 }; 545 546 /* ------------------------------------------------------------------ */ 547 548 static bool gntdev_invalidate(struct mmu_interval_notifier *mn, 549 const struct mmu_notifier_range *range, 550 unsigned long cur_seq) 551 { 552 struct gntdev_grant_map *map = 553 container_of(mn, struct gntdev_grant_map, notifier); 554 unsigned long mstart, mend; 555 unsigned long map_start, map_end; 556 557 if (!mmu_notifier_range_blockable(range)) 558 return false; 559 560 map_start = map->pages_vm_start; 561 map_end = map->pages_vm_start + (map->count << PAGE_SHIFT); 562 563 /* 564 * If the VMA is split or otherwise changed the notifier is not 565 * updated, but we don't want to process VA's outside the modified 566 * VMA. FIXME: It would be much more understandable to just prevent 567 * modifying the VMA in the first place. 568 */ 569 if (map_start >= range->end || map_end <= range->start) 570 return true; 571 572 mstart = max(range->start, map_start); 573 mend = min(range->end, map_end); 574 pr_debug("map %d+%d (%lx %lx), range %lx %lx, mrange %lx %lx\n", 575 map->index, map->count, map_start, map_end, 576 range->start, range->end, mstart, mend); 577 unmap_grant_pages(map, (mstart - map_start) >> PAGE_SHIFT, 578 (mend - mstart) >> PAGE_SHIFT); 579 580 return true; 581 } 582 583 static const struct mmu_interval_notifier_ops gntdev_mmu_ops = { 584 .invalidate = gntdev_invalidate, 585 }; 586 587 /* ------------------------------------------------------------------ */ 588 589 static int gntdev_open(struct inode *inode, struct file *flip) 590 { 591 struct gntdev_priv *priv; 592 593 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 594 if (!priv) 595 return -ENOMEM; 596 597 INIT_LIST_HEAD(&priv->maps); 598 mutex_init(&priv->lock); 599 600 mutex_init(&priv->batch_lock); 601 602 #ifdef CONFIG_XEN_GNTDEV_DMABUF 603 priv->dmabuf_priv = gntdev_dmabuf_init(flip); 604 if (IS_ERR(priv->dmabuf_priv)) { 605 int ret = PTR_ERR(priv->dmabuf_priv); 606 607 kfree(priv); 608 return ret; 609 } 610 #endif 611 612 flip->private_data = priv; 613 #ifdef CONFIG_XEN_GRANT_DMA_ALLOC 614 priv->dma_dev = gntdev_miscdev.this_device; 615 dma_coerce_mask_and_coherent(priv->dma_dev, DMA_BIT_MASK(64)); 616 #endif 617 pr_debug("priv %p\n", priv); 618 619 return 0; 620 } 621 622 static int gntdev_release(struct inode *inode, struct file *flip) 623 { 624 struct gntdev_priv *priv = flip->private_data; 625 struct gntdev_grant_map *map; 626 struct gntdev_copy_batch *batch; 627 628 pr_debug("priv %p\n", priv); 629 630 mutex_lock(&priv->lock); 631 while (!list_empty(&priv->maps)) { 632 map = list_entry(priv->maps.next, 633 struct gntdev_grant_map, next); 634 list_del(&map->next); 635 gntdev_put_map(NULL /* already removed */, map); 636 } 637 mutex_unlock(&priv->lock); 638 639 mutex_lock(&priv->batch_lock); 640 while (priv->batch) { 641 batch = priv->batch; 642 priv->batch = batch->next; 643 kfree(batch); 644 } 645 mutex_unlock(&priv->batch_lock); 646 647 #ifdef CONFIG_XEN_GNTDEV_DMABUF 648 gntdev_dmabuf_fini(priv->dmabuf_priv); 649 #endif 650 651 kfree(priv); 652 return 0; 653 } 654 655 static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv, 656 struct ioctl_gntdev_map_grant_ref __user *u) 657 { 658 struct ioctl_gntdev_map_grant_ref op; 659 struct gntdev_grant_map *map; 660 int err; 661 662 if (copy_from_user(&op, u, sizeof(op)) != 0) 663 return -EFAULT; 664 pr_debug("priv %p, add %d\n", priv, op.count); 665 if (unlikely(gntdev_test_page_count(op.count))) 666 return -EINVAL; 667 668 err = -ENOMEM; 669 map = gntdev_alloc_map(priv, op.count, 0 /* This is not a dma-buf. */); 670 if (!map) 671 return err; 672 673 if (copy_from_user(map->grants, &u->refs, 674 sizeof(map->grants[0]) * op.count) != 0) { 675 gntdev_put_map(NULL, map); 676 return -EFAULT; 677 } 678 679 mutex_lock(&priv->lock); 680 gntdev_add_map(priv, map); 681 op.index = map->index << PAGE_SHIFT; 682 mutex_unlock(&priv->lock); 683 684 if (copy_to_user(u, &op, sizeof(op)) != 0) 685 return -EFAULT; 686 687 return 0; 688 } 689 690 static long gntdev_ioctl_unmap_grant_ref(struct gntdev_priv *priv, 691 struct ioctl_gntdev_unmap_grant_ref __user *u) 692 { 693 struct ioctl_gntdev_unmap_grant_ref op; 694 struct gntdev_grant_map *map; 695 int err = -ENOENT; 696 697 if (copy_from_user(&op, u, sizeof(op)) != 0) 698 return -EFAULT; 699 pr_debug("priv %p, del %d+%d\n", priv, (int)op.index, (int)op.count); 700 701 mutex_lock(&priv->lock); 702 map = gntdev_find_map_index(priv, op.index >> PAGE_SHIFT, op.count); 703 if (map) { 704 list_del(&map->next); 705 err = 0; 706 } 707 mutex_unlock(&priv->lock); 708 if (map) 709 gntdev_put_map(priv, map); 710 return err; 711 } 712 713 static long gntdev_ioctl_get_offset_for_vaddr(struct gntdev_priv *priv, 714 struct ioctl_gntdev_get_offset_for_vaddr __user *u) 715 { 716 struct ioctl_gntdev_get_offset_for_vaddr op; 717 struct vm_area_struct *vma; 718 struct gntdev_grant_map *map; 719 int rv = -EINVAL; 720 721 if (copy_from_user(&op, u, sizeof(op)) != 0) 722 return -EFAULT; 723 pr_debug("priv %p, offset for vaddr %lx\n", priv, (unsigned long)op.vaddr); 724 725 mmap_read_lock(current->mm); 726 vma = find_vma(current->mm, op.vaddr); 727 if (!vma || vma->vm_ops != &gntdev_vmops) 728 goto out_unlock; 729 730 map = vma->vm_private_data; 731 if (!map) 732 goto out_unlock; 733 734 op.offset = map->index << PAGE_SHIFT; 735 op.count = map->count; 736 rv = 0; 737 738 out_unlock: 739 mmap_read_unlock(current->mm); 740 741 if (rv == 0 && copy_to_user(u, &op, sizeof(op)) != 0) 742 return -EFAULT; 743 return rv; 744 } 745 746 static long gntdev_ioctl_notify(struct gntdev_priv *priv, void __user *u) 747 { 748 struct ioctl_gntdev_unmap_notify op; 749 struct gntdev_grant_map *map; 750 int rc; 751 int out_flags; 752 evtchn_port_t out_event; 753 754 if (copy_from_user(&op, u, sizeof(op))) 755 return -EFAULT; 756 757 if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) 758 return -EINVAL; 759 760 /* We need to grab a reference to the event channel we are going to use 761 * to send the notify before releasing the reference we may already have 762 * (if someone has called this ioctl twice). This is required so that 763 * it is possible to change the clear_byte part of the notification 764 * without disturbing the event channel part, which may now be the last 765 * reference to that event channel. 766 */ 767 if (op.action & UNMAP_NOTIFY_SEND_EVENT) { 768 if (evtchn_get(op.event_channel_port)) 769 return -EINVAL; 770 } 771 772 out_flags = op.action; 773 out_event = op.event_channel_port; 774 775 mutex_lock(&priv->lock); 776 777 list_for_each_entry(map, &priv->maps, next) { 778 uint64_t begin = map->index << PAGE_SHIFT; 779 uint64_t end = (map->index + map->count) << PAGE_SHIFT; 780 if (op.index >= begin && op.index < end) 781 goto found; 782 } 783 rc = -ENOENT; 784 goto unlock_out; 785 786 found: 787 if ((op.action & UNMAP_NOTIFY_CLEAR_BYTE) && 788 (map->flags & GNTMAP_readonly)) { 789 rc = -EINVAL; 790 goto unlock_out; 791 } 792 793 out_flags = map->notify.flags; 794 out_event = map->notify.event; 795 796 map->notify.flags = op.action; 797 map->notify.addr = op.index - (map->index << PAGE_SHIFT); 798 map->notify.event = op.event_channel_port; 799 800 rc = 0; 801 802 unlock_out: 803 mutex_unlock(&priv->lock); 804 805 /* Drop the reference to the event channel we did not save in the map */ 806 if (out_flags & UNMAP_NOTIFY_SEND_EVENT) 807 evtchn_put(out_event); 808 809 return rc; 810 } 811 812 static int gntdev_get_page(struct gntdev_copy_batch *batch, void __user *virt, 813 unsigned long *gfn) 814 { 815 unsigned long addr = (unsigned long)virt; 816 struct page *page; 817 unsigned long xen_pfn; 818 int ret; 819 820 ret = pin_user_pages_fast(addr, 1, batch->writeable ? FOLL_WRITE : 0, &page); 821 if (ret < 0) 822 return ret; 823 824 batch->pages[batch->nr_pages++] = page; 825 826 xen_pfn = page_to_xen_pfn(page) + XEN_PFN_DOWN(addr & ~PAGE_MASK); 827 *gfn = pfn_to_gfn(xen_pfn); 828 829 return 0; 830 } 831 832 static void gntdev_put_pages(struct gntdev_copy_batch *batch) 833 { 834 unpin_user_pages_dirty_lock(batch->pages, batch->nr_pages, batch->writeable); 835 batch->nr_pages = 0; 836 batch->writeable = false; 837 } 838 839 static int gntdev_copy(struct gntdev_copy_batch *batch) 840 { 841 unsigned int i; 842 843 gnttab_batch_copy(batch->ops, batch->nr_ops); 844 gntdev_put_pages(batch); 845 846 /* 847 * For each completed op, update the status if the op failed 848 * and all previous ops for the segment were successful. 849 */ 850 for (i = 0; i < batch->nr_ops; i++) { 851 s16 status = batch->ops[i].status; 852 s16 old_status; 853 854 if (status == GNTST_okay) 855 continue; 856 857 if (__get_user(old_status, batch->status[i])) 858 return -EFAULT; 859 860 if (old_status != GNTST_okay) 861 continue; 862 863 if (__put_user(status, batch->status[i])) 864 return -EFAULT; 865 } 866 867 batch->nr_ops = 0; 868 return 0; 869 } 870 871 static int gntdev_grant_copy_seg(struct gntdev_copy_batch *batch, 872 struct gntdev_grant_copy_segment *seg, 873 s16 __user *status) 874 { 875 uint16_t copied = 0; 876 877 /* 878 * Disallow local -> local copies since there is only space in 879 * batch->pages for one page per-op and this would be a very 880 * expensive memcpy(). 881 */ 882 if (!(seg->flags & (GNTCOPY_source_gref | GNTCOPY_dest_gref))) 883 return -EINVAL; 884 885 /* Can't cross page if source/dest is a grant ref. */ 886 if (seg->flags & GNTCOPY_source_gref) { 887 if (seg->source.foreign.offset + seg->len > XEN_PAGE_SIZE) 888 return -EINVAL; 889 } 890 if (seg->flags & GNTCOPY_dest_gref) { 891 if (seg->dest.foreign.offset + seg->len > XEN_PAGE_SIZE) 892 return -EINVAL; 893 } 894 895 if (put_user(GNTST_okay, status)) 896 return -EFAULT; 897 898 while (copied < seg->len) { 899 struct gnttab_copy *op; 900 void __user *virt; 901 size_t len, off; 902 unsigned long gfn; 903 int ret; 904 905 if (batch->nr_ops >= GNTDEV_COPY_BATCH) { 906 ret = gntdev_copy(batch); 907 if (ret < 0) 908 return ret; 909 } 910 911 len = seg->len - copied; 912 913 op = &batch->ops[batch->nr_ops]; 914 op->flags = 0; 915 916 if (seg->flags & GNTCOPY_source_gref) { 917 op->source.u.ref = seg->source.foreign.ref; 918 op->source.domid = seg->source.foreign.domid; 919 op->source.offset = seg->source.foreign.offset + copied; 920 op->flags |= GNTCOPY_source_gref; 921 } else { 922 virt = seg->source.virt + copied; 923 off = (unsigned long)virt & ~XEN_PAGE_MASK; 924 len = min(len, (size_t)XEN_PAGE_SIZE - off); 925 batch->writeable = false; 926 927 ret = gntdev_get_page(batch, virt, &gfn); 928 if (ret < 0) 929 return ret; 930 931 op->source.u.gmfn = gfn; 932 op->source.domid = DOMID_SELF; 933 op->source.offset = off; 934 } 935 936 if (seg->flags & GNTCOPY_dest_gref) { 937 op->dest.u.ref = seg->dest.foreign.ref; 938 op->dest.domid = seg->dest.foreign.domid; 939 op->dest.offset = seg->dest.foreign.offset + copied; 940 op->flags |= GNTCOPY_dest_gref; 941 } else { 942 virt = seg->dest.virt + copied; 943 off = (unsigned long)virt & ~XEN_PAGE_MASK; 944 len = min(len, (size_t)XEN_PAGE_SIZE - off); 945 batch->writeable = true; 946 947 ret = gntdev_get_page(batch, virt, &gfn); 948 if (ret < 0) 949 return ret; 950 951 op->dest.u.gmfn = gfn; 952 op->dest.domid = DOMID_SELF; 953 op->dest.offset = off; 954 } 955 956 op->len = len; 957 copied += len; 958 959 batch->status[batch->nr_ops] = status; 960 batch->nr_ops++; 961 } 962 963 return 0; 964 } 965 966 static long gntdev_ioctl_grant_copy(struct gntdev_priv *priv, void __user *u) 967 { 968 struct ioctl_gntdev_grant_copy copy; 969 struct gntdev_copy_batch *batch; 970 unsigned int i; 971 int ret = 0; 972 973 if (copy_from_user(©, u, sizeof(copy))) 974 return -EFAULT; 975 976 mutex_lock(&priv->batch_lock); 977 if (!priv->batch) { 978 batch = kmalloc(sizeof(*batch), GFP_KERNEL); 979 } else { 980 batch = priv->batch; 981 priv->batch = batch->next; 982 } 983 mutex_unlock(&priv->batch_lock); 984 if (!batch) 985 return -ENOMEM; 986 987 batch->nr_ops = 0; 988 batch->nr_pages = 0; 989 990 for (i = 0; i < copy.count; i++) { 991 struct gntdev_grant_copy_segment seg; 992 993 if (copy_from_user(&seg, ©.segments[i], sizeof(seg))) { 994 ret = -EFAULT; 995 gntdev_put_pages(batch); 996 goto out; 997 } 998 999 ret = gntdev_grant_copy_seg(batch, &seg, ©.segments[i].status); 1000 if (ret < 0) { 1001 gntdev_put_pages(batch); 1002 goto out; 1003 } 1004 1005 cond_resched(); 1006 } 1007 if (batch->nr_ops) 1008 ret = gntdev_copy(batch); 1009 1010 out: 1011 mutex_lock(&priv->batch_lock); 1012 batch->next = priv->batch; 1013 priv->batch = batch; 1014 mutex_unlock(&priv->batch_lock); 1015 1016 return ret; 1017 } 1018 1019 static long gntdev_ioctl(struct file *flip, 1020 unsigned int cmd, unsigned long arg) 1021 { 1022 struct gntdev_priv *priv = flip->private_data; 1023 void __user *ptr = (void __user *)arg; 1024 1025 switch (cmd) { 1026 case IOCTL_GNTDEV_MAP_GRANT_REF: 1027 return gntdev_ioctl_map_grant_ref(priv, ptr); 1028 1029 case IOCTL_GNTDEV_UNMAP_GRANT_REF: 1030 return gntdev_ioctl_unmap_grant_ref(priv, ptr); 1031 1032 case IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR: 1033 return gntdev_ioctl_get_offset_for_vaddr(priv, ptr); 1034 1035 case IOCTL_GNTDEV_SET_UNMAP_NOTIFY: 1036 return gntdev_ioctl_notify(priv, ptr); 1037 1038 case IOCTL_GNTDEV_GRANT_COPY: 1039 return gntdev_ioctl_grant_copy(priv, ptr); 1040 1041 #ifdef CONFIG_XEN_GNTDEV_DMABUF 1042 case IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS: 1043 return gntdev_ioctl_dmabuf_exp_from_refs(priv, use_ptemod, ptr); 1044 1045 case IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED: 1046 return gntdev_ioctl_dmabuf_exp_wait_released(priv, ptr); 1047 1048 case IOCTL_GNTDEV_DMABUF_IMP_TO_REFS: 1049 return gntdev_ioctl_dmabuf_imp_to_refs(priv, ptr); 1050 1051 case IOCTL_GNTDEV_DMABUF_IMP_RELEASE: 1052 return gntdev_ioctl_dmabuf_imp_release(priv, ptr); 1053 #endif 1054 1055 default: 1056 pr_debug("priv %p, unknown cmd %x\n", priv, cmd); 1057 return -ENOIOCTLCMD; 1058 } 1059 1060 return 0; 1061 } 1062 1063 static int gntdev_mmap(struct file *flip, struct vm_area_struct *vma) 1064 { 1065 struct gntdev_priv *priv = flip->private_data; 1066 int index = vma->vm_pgoff; 1067 int count = vma_pages(vma); 1068 struct gntdev_grant_map *map; 1069 int err = -EINVAL; 1070 1071 if ((vma->vm_flags & VM_WRITE) && !(vma->vm_flags & VM_SHARED)) 1072 return -EINVAL; 1073 1074 pr_debug("map %d+%d at %lx (pgoff %lx)\n", 1075 index, count, vma->vm_start, vma->vm_pgoff); 1076 1077 mutex_lock(&priv->lock); 1078 map = gntdev_find_map_index(priv, index, count); 1079 if (!map) 1080 goto unlock_out; 1081 if (!atomic_add_unless(&map->in_use, 1, 1)) 1082 goto unlock_out; 1083 1084 refcount_inc(&map->users); 1085 1086 vma->vm_ops = &gntdev_vmops; 1087 1088 vm_flags_set(vma, VM_DONTEXPAND | VM_DONTDUMP | VM_MIXEDMAP); 1089 1090 if (use_ptemod) 1091 vm_flags_set(vma, VM_DONTCOPY); 1092 1093 vma->vm_private_data = map; 1094 if (map->flags) { 1095 if ((vma->vm_flags & VM_WRITE) && 1096 (map->flags & GNTMAP_readonly)) 1097 goto out_unlock_put; 1098 } else { 1099 map->flags = GNTMAP_host_map; 1100 if (!(vma->vm_flags & VM_WRITE)) 1101 map->flags |= GNTMAP_readonly; 1102 } 1103 1104 map->pages_vm_start = vma->vm_start; 1105 1106 if (use_ptemod) { 1107 err = mmu_interval_notifier_insert_locked( 1108 &map->notifier, vma->vm_mm, vma->vm_start, 1109 vma->vm_end - vma->vm_start, &gntdev_mmu_ops); 1110 if (err) 1111 goto out_unlock_put; 1112 1113 map->notifier_init = true; 1114 } 1115 mutex_unlock(&priv->lock); 1116 1117 if (use_ptemod) { 1118 /* 1119 * gntdev takes the address of the PTE in find_grant_ptes() and 1120 * passes it to the hypervisor in gntdev_map_grant_pages(). The 1121 * purpose of the notifier is to prevent the hypervisor pointer 1122 * to the PTE from going stale. 1123 * 1124 * Since this vma's mappings can't be touched without the 1125 * mmap_lock, and we are holding it now, there is no need for 1126 * the notifier_range locking pattern. 1127 */ 1128 mmu_interval_read_begin(&map->notifier); 1129 1130 err = apply_to_page_range(vma->vm_mm, vma->vm_start, 1131 vma->vm_end - vma->vm_start, 1132 find_grant_ptes, map); 1133 if (err) { 1134 pr_warn("find_grant_ptes() failure.\n"); 1135 goto out_put_map; 1136 } 1137 } 1138 1139 err = gntdev_map_grant_pages(map); 1140 if (err) 1141 goto out_put_map; 1142 1143 if (!use_ptemod) { 1144 err = vm_map_pages_zero(vma, map->pages, map->count); 1145 if (err) 1146 goto out_put_map; 1147 } 1148 1149 return 0; 1150 1151 unlock_out: 1152 mutex_unlock(&priv->lock); 1153 return err; 1154 1155 out_unlock_put: 1156 mutex_unlock(&priv->lock); 1157 out_put_map: 1158 if (use_ptemod) 1159 unmap_grant_pages(map, 0, map->count); 1160 gntdev_put_map(priv, map); 1161 return err; 1162 } 1163 1164 static const struct file_operations gntdev_fops = { 1165 .owner = THIS_MODULE, 1166 .open = gntdev_open, 1167 .release = gntdev_release, 1168 .mmap = gntdev_mmap, 1169 .unlocked_ioctl = gntdev_ioctl 1170 }; 1171 1172 static struct miscdevice gntdev_miscdev = { 1173 .minor = MISC_DYNAMIC_MINOR, 1174 .name = "xen/gntdev", 1175 .fops = &gntdev_fops, 1176 }; 1177 1178 /* ------------------------------------------------------------------ */ 1179 1180 static int __init gntdev_init(void) 1181 { 1182 int err; 1183 1184 if (!xen_domain()) 1185 return -ENODEV; 1186 1187 use_ptemod = !xen_feature(XENFEAT_auto_translated_physmap); 1188 1189 err = misc_register(&gntdev_miscdev); 1190 if (err != 0) { 1191 pr_err("Could not register gntdev device\n"); 1192 return err; 1193 } 1194 return 0; 1195 } 1196 1197 static void __exit gntdev_exit(void) 1198 { 1199 misc_deregister(&gntdev_miscdev); 1200 } 1201 1202 module_init(gntdev_init); 1203 module_exit(gntdev_exit); 1204 1205 /* ------------------------------------------------------------------ */ 1206