1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This is a module to test the HMM (Heterogeneous Memory Management) 4 * mirror and zone device private memory migration APIs of the kernel. 5 * Userspace programs can register with the driver to mirror their own address 6 * space and can use the device to read/write any valid virtual address. 7 */ 8 #include <linux/init.h> 9 #include <linux/fs.h> 10 #include <linux/mm.h> 11 #include <linux/module.h> 12 #include <linux/kernel.h> 13 #include <linux/cdev.h> 14 #include <linux/device.h> 15 #include <linux/mutex.h> 16 #include <linux/rwsem.h> 17 #include <linux/sched.h> 18 #include <linux/slab.h> 19 #include <linux/highmem.h> 20 #include <linux/delay.h> 21 #include <linux/pagemap.h> 22 #include <linux/hmm.h> 23 #include <linux/vmalloc.h> 24 #include <linux/swap.h> 25 #include <linux/swapops.h> 26 #include <linux/sched/mm.h> 27 #include <linux/platform_device.h> 28 29 #include "test_hmm_uapi.h" 30 31 #define DMIRROR_NDEVICES 2 32 #define DMIRROR_RANGE_FAULT_TIMEOUT 1000 33 #define DEVMEM_CHUNK_SIZE (256 * 1024 * 1024U) 34 #define DEVMEM_CHUNKS_RESERVE 16 35 36 static const struct dev_pagemap_ops dmirror_devmem_ops; 37 static const struct mmu_interval_notifier_ops dmirror_min_ops; 38 static dev_t dmirror_dev; 39 static struct page *dmirror_zero_page; 40 41 struct dmirror_device; 42 43 struct dmirror_bounce { 44 void *ptr; 45 unsigned long size; 46 unsigned long addr; 47 unsigned long cpages; 48 }; 49 50 #define DPT_XA_TAG_WRITE 3UL 51 52 /* 53 * Data structure to track address ranges and register for mmu interval 54 * notifier updates. 55 */ 56 struct dmirror_interval { 57 struct mmu_interval_notifier notifier; 58 struct dmirror *dmirror; 59 }; 60 61 /* 62 * Data attached to the open device file. 63 * Note that it might be shared after a fork(). 64 */ 65 struct dmirror { 66 struct dmirror_device *mdevice; 67 struct xarray pt; 68 struct mmu_interval_notifier notifier; 69 struct mutex mutex; 70 }; 71 72 /* 73 * ZONE_DEVICE pages for migration and simulating device memory. 74 */ 75 struct dmirror_chunk { 76 struct dev_pagemap pagemap; 77 struct dmirror_device *mdevice; 78 }; 79 80 /* 81 * Per device data. 82 */ 83 struct dmirror_device { 84 struct cdev cdevice; 85 struct hmm_devmem *devmem; 86 87 unsigned int devmem_capacity; 88 unsigned int devmem_count; 89 struct dmirror_chunk **devmem_chunks; 90 struct mutex devmem_lock; /* protects the above */ 91 92 unsigned long calloc; 93 unsigned long cfree; 94 struct page *free_pages; 95 spinlock_t lock; /* protects the above */ 96 }; 97 98 static struct dmirror_device dmirror_devices[DMIRROR_NDEVICES]; 99 100 static int dmirror_bounce_init(struct dmirror_bounce *bounce, 101 unsigned long addr, 102 unsigned long size) 103 { 104 bounce->addr = addr; 105 bounce->size = size; 106 bounce->cpages = 0; 107 bounce->ptr = vmalloc(size); 108 if (!bounce->ptr) 109 return -ENOMEM; 110 return 0; 111 } 112 113 static void dmirror_bounce_fini(struct dmirror_bounce *bounce) 114 { 115 vfree(bounce->ptr); 116 } 117 118 static int dmirror_fops_open(struct inode *inode, struct file *filp) 119 { 120 struct cdev *cdev = inode->i_cdev; 121 struct dmirror *dmirror; 122 int ret; 123 124 /* Mirror this process address space */ 125 dmirror = kzalloc(sizeof(*dmirror), GFP_KERNEL); 126 if (dmirror == NULL) 127 return -ENOMEM; 128 129 dmirror->mdevice = container_of(cdev, struct dmirror_device, cdevice); 130 mutex_init(&dmirror->mutex); 131 xa_init(&dmirror->pt); 132 133 ret = mmu_interval_notifier_insert(&dmirror->notifier, current->mm, 134 0, ULONG_MAX & PAGE_MASK, &dmirror_min_ops); 135 if (ret) { 136 kfree(dmirror); 137 return ret; 138 } 139 140 filp->private_data = dmirror; 141 return 0; 142 } 143 144 static int dmirror_fops_release(struct inode *inode, struct file *filp) 145 { 146 struct dmirror *dmirror = filp->private_data; 147 148 mmu_interval_notifier_remove(&dmirror->notifier); 149 xa_destroy(&dmirror->pt); 150 kfree(dmirror); 151 return 0; 152 } 153 154 static struct dmirror_device *dmirror_page_to_device(struct page *page) 155 156 { 157 return container_of(page->pgmap, struct dmirror_chunk, 158 pagemap)->mdevice; 159 } 160 161 static int dmirror_do_fault(struct dmirror *dmirror, struct hmm_range *range) 162 { 163 unsigned long *pfns = range->hmm_pfns; 164 unsigned long pfn; 165 166 for (pfn = (range->start >> PAGE_SHIFT); 167 pfn < (range->end >> PAGE_SHIFT); 168 pfn++, pfns++) { 169 struct page *page; 170 void *entry; 171 172 /* 173 * Since we asked for hmm_range_fault() to populate pages, 174 * it shouldn't return an error entry on success. 175 */ 176 WARN_ON(*pfns & HMM_PFN_ERROR); 177 WARN_ON(!(*pfns & HMM_PFN_VALID)); 178 179 page = hmm_pfn_to_page(*pfns); 180 WARN_ON(!page); 181 182 entry = page; 183 if (*pfns & HMM_PFN_WRITE) 184 entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE); 185 else if (WARN_ON(range->default_flags & HMM_PFN_WRITE)) 186 return -EFAULT; 187 entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC); 188 if (xa_is_err(entry)) 189 return xa_err(entry); 190 } 191 192 return 0; 193 } 194 195 static void dmirror_do_update(struct dmirror *dmirror, unsigned long start, 196 unsigned long end) 197 { 198 unsigned long pfn; 199 void *entry; 200 201 /* 202 * The XArray doesn't hold references to pages since it relies on 203 * the mmu notifier to clear page pointers when they become stale. 204 * Therefore, it is OK to just clear the entry. 205 */ 206 xa_for_each_range(&dmirror->pt, pfn, entry, start >> PAGE_SHIFT, 207 end >> PAGE_SHIFT) 208 xa_erase(&dmirror->pt, pfn); 209 } 210 211 static bool dmirror_interval_invalidate(struct mmu_interval_notifier *mni, 212 const struct mmu_notifier_range *range, 213 unsigned long cur_seq) 214 { 215 struct dmirror *dmirror = container_of(mni, struct dmirror, notifier); 216 217 if (mmu_notifier_range_blockable(range)) 218 mutex_lock(&dmirror->mutex); 219 else if (!mutex_trylock(&dmirror->mutex)) 220 return false; 221 222 mmu_interval_set_seq(mni, cur_seq); 223 dmirror_do_update(dmirror, range->start, range->end); 224 225 mutex_unlock(&dmirror->mutex); 226 return true; 227 } 228 229 static const struct mmu_interval_notifier_ops dmirror_min_ops = { 230 .invalidate = dmirror_interval_invalidate, 231 }; 232 233 static int dmirror_range_fault(struct dmirror *dmirror, 234 struct hmm_range *range) 235 { 236 struct mm_struct *mm = dmirror->notifier.mm; 237 unsigned long timeout = 238 jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT); 239 int ret; 240 241 while (true) { 242 if (time_after(jiffies, timeout)) { 243 ret = -EBUSY; 244 goto out; 245 } 246 247 range->notifier_seq = mmu_interval_read_begin(range->notifier); 248 mmap_read_lock(mm); 249 ret = hmm_range_fault(range); 250 mmap_read_unlock(mm); 251 if (ret) { 252 if (ret == -EBUSY) 253 continue; 254 goto out; 255 } 256 257 mutex_lock(&dmirror->mutex); 258 if (mmu_interval_read_retry(range->notifier, 259 range->notifier_seq)) { 260 mutex_unlock(&dmirror->mutex); 261 continue; 262 } 263 break; 264 } 265 266 ret = dmirror_do_fault(dmirror, range); 267 268 mutex_unlock(&dmirror->mutex); 269 out: 270 return ret; 271 } 272 273 static int dmirror_fault(struct dmirror *dmirror, unsigned long start, 274 unsigned long end, bool write) 275 { 276 struct mm_struct *mm = dmirror->notifier.mm; 277 unsigned long addr; 278 unsigned long pfns[64]; 279 struct hmm_range range = { 280 .notifier = &dmirror->notifier, 281 .hmm_pfns = pfns, 282 .pfn_flags_mask = 0, 283 .default_flags = 284 HMM_PFN_REQ_FAULT | (write ? HMM_PFN_REQ_WRITE : 0), 285 .dev_private_owner = dmirror->mdevice, 286 }; 287 int ret = 0; 288 289 /* Since the mm is for the mirrored process, get a reference first. */ 290 if (!mmget_not_zero(mm)) 291 return 0; 292 293 for (addr = start; addr < end; addr = range.end) { 294 range.start = addr; 295 range.end = min(addr + (ARRAY_SIZE(pfns) << PAGE_SHIFT), end); 296 297 ret = dmirror_range_fault(dmirror, &range); 298 if (ret) 299 break; 300 } 301 302 mmput(mm); 303 return ret; 304 } 305 306 static int dmirror_do_read(struct dmirror *dmirror, unsigned long start, 307 unsigned long end, struct dmirror_bounce *bounce) 308 { 309 unsigned long pfn; 310 void *ptr; 311 312 ptr = bounce->ptr + ((start - bounce->addr) & PAGE_MASK); 313 314 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) { 315 void *entry; 316 struct page *page; 317 void *tmp; 318 319 entry = xa_load(&dmirror->pt, pfn); 320 page = xa_untag_pointer(entry); 321 if (!page) 322 return -ENOENT; 323 324 tmp = kmap(page); 325 memcpy(ptr, tmp, PAGE_SIZE); 326 kunmap(page); 327 328 ptr += PAGE_SIZE; 329 bounce->cpages++; 330 } 331 332 return 0; 333 } 334 335 static int dmirror_read(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd) 336 { 337 struct dmirror_bounce bounce; 338 unsigned long start, end; 339 unsigned long size = cmd->npages << PAGE_SHIFT; 340 int ret; 341 342 start = cmd->addr; 343 end = start + size; 344 if (end < start) 345 return -EINVAL; 346 347 ret = dmirror_bounce_init(&bounce, start, size); 348 if (ret) 349 return ret; 350 351 while (1) { 352 mutex_lock(&dmirror->mutex); 353 ret = dmirror_do_read(dmirror, start, end, &bounce); 354 mutex_unlock(&dmirror->mutex); 355 if (ret != -ENOENT) 356 break; 357 358 start = cmd->addr + (bounce.cpages << PAGE_SHIFT); 359 ret = dmirror_fault(dmirror, start, end, false); 360 if (ret) 361 break; 362 cmd->faults++; 363 } 364 365 if (ret == 0) { 366 if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr, 367 bounce.size)) 368 ret = -EFAULT; 369 } 370 cmd->cpages = bounce.cpages; 371 dmirror_bounce_fini(&bounce); 372 return ret; 373 } 374 375 static int dmirror_do_write(struct dmirror *dmirror, unsigned long start, 376 unsigned long end, struct dmirror_bounce *bounce) 377 { 378 unsigned long pfn; 379 void *ptr; 380 381 ptr = bounce->ptr + ((start - bounce->addr) & PAGE_MASK); 382 383 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) { 384 void *entry; 385 struct page *page; 386 void *tmp; 387 388 entry = xa_load(&dmirror->pt, pfn); 389 page = xa_untag_pointer(entry); 390 if (!page || xa_pointer_tag(entry) != DPT_XA_TAG_WRITE) 391 return -ENOENT; 392 393 tmp = kmap(page); 394 memcpy(tmp, ptr, PAGE_SIZE); 395 kunmap(page); 396 397 ptr += PAGE_SIZE; 398 bounce->cpages++; 399 } 400 401 return 0; 402 } 403 404 static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd) 405 { 406 struct dmirror_bounce bounce; 407 unsigned long start, end; 408 unsigned long size = cmd->npages << PAGE_SHIFT; 409 int ret; 410 411 start = cmd->addr; 412 end = start + size; 413 if (end < start) 414 return -EINVAL; 415 416 ret = dmirror_bounce_init(&bounce, start, size); 417 if (ret) 418 return ret; 419 if (copy_from_user(bounce.ptr, u64_to_user_ptr(cmd->ptr), 420 bounce.size)) { 421 ret = -EFAULT; 422 goto fini; 423 } 424 425 while (1) { 426 mutex_lock(&dmirror->mutex); 427 ret = dmirror_do_write(dmirror, start, end, &bounce); 428 mutex_unlock(&dmirror->mutex); 429 if (ret != -ENOENT) 430 break; 431 432 start = cmd->addr + (bounce.cpages << PAGE_SHIFT); 433 ret = dmirror_fault(dmirror, start, end, true); 434 if (ret) 435 break; 436 cmd->faults++; 437 } 438 439 fini: 440 cmd->cpages = bounce.cpages; 441 dmirror_bounce_fini(&bounce); 442 return ret; 443 } 444 445 static bool dmirror_allocate_chunk(struct dmirror_device *mdevice, 446 struct page **ppage) 447 { 448 struct dmirror_chunk *devmem; 449 struct resource *res; 450 unsigned long pfn; 451 unsigned long pfn_first; 452 unsigned long pfn_last; 453 void *ptr; 454 455 mutex_lock(&mdevice->devmem_lock); 456 457 if (mdevice->devmem_count == mdevice->devmem_capacity) { 458 struct dmirror_chunk **new_chunks; 459 unsigned int new_capacity; 460 461 new_capacity = mdevice->devmem_capacity + 462 DEVMEM_CHUNKS_RESERVE; 463 new_chunks = krealloc(mdevice->devmem_chunks, 464 sizeof(new_chunks[0]) * new_capacity, 465 GFP_KERNEL); 466 if (!new_chunks) 467 goto err; 468 mdevice->devmem_capacity = new_capacity; 469 mdevice->devmem_chunks = new_chunks; 470 } 471 472 res = request_free_mem_region(&iomem_resource, DEVMEM_CHUNK_SIZE, 473 "hmm_dmirror"); 474 if (IS_ERR(res)) 475 goto err; 476 477 devmem = kzalloc(sizeof(*devmem), GFP_KERNEL); 478 if (!devmem) 479 goto err_release; 480 481 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE; 482 devmem->pagemap.res = *res; 483 devmem->pagemap.ops = &dmirror_devmem_ops; 484 devmem->pagemap.owner = mdevice; 485 486 ptr = memremap_pages(&devmem->pagemap, numa_node_id()); 487 if (IS_ERR(ptr)) 488 goto err_free; 489 490 devmem->mdevice = mdevice; 491 pfn_first = devmem->pagemap.res.start >> PAGE_SHIFT; 492 pfn_last = pfn_first + 493 (resource_size(&devmem->pagemap.res) >> PAGE_SHIFT); 494 mdevice->devmem_chunks[mdevice->devmem_count++] = devmem; 495 496 mutex_unlock(&mdevice->devmem_lock); 497 498 pr_info("added new %u MB chunk (total %u chunks, %u MB) PFNs [0x%lx 0x%lx)\n", 499 DEVMEM_CHUNK_SIZE / (1024 * 1024), 500 mdevice->devmem_count, 501 mdevice->devmem_count * (DEVMEM_CHUNK_SIZE / (1024 * 1024)), 502 pfn_first, pfn_last); 503 504 spin_lock(&mdevice->lock); 505 for (pfn = pfn_first; pfn < pfn_last; pfn++) { 506 struct page *page = pfn_to_page(pfn); 507 508 page->zone_device_data = mdevice->free_pages; 509 mdevice->free_pages = page; 510 } 511 if (ppage) { 512 *ppage = mdevice->free_pages; 513 mdevice->free_pages = (*ppage)->zone_device_data; 514 mdevice->calloc++; 515 } 516 spin_unlock(&mdevice->lock); 517 518 return true; 519 520 err_free: 521 kfree(devmem); 522 err_release: 523 release_mem_region(res->start, resource_size(res)); 524 err: 525 mutex_unlock(&mdevice->devmem_lock); 526 return false; 527 } 528 529 static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice) 530 { 531 struct page *dpage = NULL; 532 struct page *rpage; 533 534 /* 535 * This is a fake device so we alloc real system memory to store 536 * our device memory. 537 */ 538 rpage = alloc_page(GFP_HIGHUSER); 539 if (!rpage) 540 return NULL; 541 542 spin_lock(&mdevice->lock); 543 544 if (mdevice->free_pages) { 545 dpage = mdevice->free_pages; 546 mdevice->free_pages = dpage->zone_device_data; 547 mdevice->calloc++; 548 spin_unlock(&mdevice->lock); 549 } else { 550 spin_unlock(&mdevice->lock); 551 if (!dmirror_allocate_chunk(mdevice, &dpage)) 552 goto error; 553 } 554 555 dpage->zone_device_data = rpage; 556 get_page(dpage); 557 lock_page(dpage); 558 return dpage; 559 560 error: 561 __free_page(rpage); 562 return NULL; 563 } 564 565 static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args, 566 struct dmirror *dmirror) 567 { 568 struct dmirror_device *mdevice = dmirror->mdevice; 569 const unsigned long *src = args->src; 570 unsigned long *dst = args->dst; 571 unsigned long addr; 572 573 for (addr = args->start; addr < args->end; addr += PAGE_SIZE, 574 src++, dst++) { 575 struct page *spage; 576 struct page *dpage; 577 struct page *rpage; 578 579 if (!(*src & MIGRATE_PFN_MIGRATE)) 580 continue; 581 582 /* 583 * Note that spage might be NULL which is OK since it is an 584 * unallocated pte_none() or read-only zero page. 585 */ 586 spage = migrate_pfn_to_page(*src); 587 588 /* 589 * Don't migrate device private pages from our own driver or 590 * others. For our own we would do a device private memory copy 591 * not a migration and for others, we would need to fault the 592 * other device's page into system memory first. 593 */ 594 if (spage && is_zone_device_page(spage)) 595 continue; 596 597 dpage = dmirror_devmem_alloc_page(mdevice); 598 if (!dpage) 599 continue; 600 601 rpage = dpage->zone_device_data; 602 if (spage) 603 copy_highpage(rpage, spage); 604 else 605 clear_highpage(rpage); 606 607 /* 608 * Normally, a device would use the page->zone_device_data to 609 * point to the mirror but here we use it to hold the page for 610 * the simulated device memory and that page holds the pointer 611 * to the mirror. 612 */ 613 rpage->zone_device_data = dmirror; 614 615 *dst = migrate_pfn(page_to_pfn(dpage)) | 616 MIGRATE_PFN_LOCKED; 617 if ((*src & MIGRATE_PFN_WRITE) || 618 (!spage && args->vma->vm_flags & VM_WRITE)) 619 *dst |= MIGRATE_PFN_WRITE; 620 } 621 } 622 623 static int dmirror_migrate_finalize_and_map(struct migrate_vma *args, 624 struct dmirror *dmirror) 625 { 626 unsigned long start = args->start; 627 unsigned long end = args->end; 628 const unsigned long *src = args->src; 629 const unsigned long *dst = args->dst; 630 unsigned long pfn; 631 632 /* Map the migrated pages into the device's page tables. */ 633 mutex_lock(&dmirror->mutex); 634 635 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++, 636 src++, dst++) { 637 struct page *dpage; 638 void *entry; 639 640 if (!(*src & MIGRATE_PFN_MIGRATE)) 641 continue; 642 643 dpage = migrate_pfn_to_page(*dst); 644 if (!dpage) 645 continue; 646 647 /* 648 * Store the page that holds the data so the page table 649 * doesn't have to deal with ZONE_DEVICE private pages. 650 */ 651 entry = dpage->zone_device_data; 652 if (*dst & MIGRATE_PFN_WRITE) 653 entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE); 654 entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC); 655 if (xa_is_err(entry)) { 656 mutex_unlock(&dmirror->mutex); 657 return xa_err(entry); 658 } 659 } 660 661 mutex_unlock(&dmirror->mutex); 662 return 0; 663 } 664 665 static int dmirror_migrate(struct dmirror *dmirror, 666 struct hmm_dmirror_cmd *cmd) 667 { 668 unsigned long start, end, addr; 669 unsigned long size = cmd->npages << PAGE_SHIFT; 670 struct mm_struct *mm = dmirror->notifier.mm; 671 struct vm_area_struct *vma; 672 unsigned long src_pfns[64]; 673 unsigned long dst_pfns[64]; 674 struct dmirror_bounce bounce; 675 struct migrate_vma args; 676 unsigned long next; 677 int ret; 678 679 start = cmd->addr; 680 end = start + size; 681 if (end < start) 682 return -EINVAL; 683 684 /* Since the mm is for the mirrored process, get a reference first. */ 685 if (!mmget_not_zero(mm)) 686 return -EINVAL; 687 688 mmap_read_lock(mm); 689 for (addr = start; addr < end; addr = next) { 690 vma = find_vma(mm, addr); 691 if (!vma || addr < vma->vm_start || 692 !(vma->vm_flags & VM_READ)) { 693 ret = -EINVAL; 694 goto out; 695 } 696 next = min(end, addr + (ARRAY_SIZE(src_pfns) << PAGE_SHIFT)); 697 if (next > vma->vm_end) 698 next = vma->vm_end; 699 700 args.vma = vma; 701 args.src = src_pfns; 702 args.dst = dst_pfns; 703 args.start = addr; 704 args.end = next; 705 args.src_owner = NULL; 706 ret = migrate_vma_setup(&args); 707 if (ret) 708 goto out; 709 710 dmirror_migrate_alloc_and_copy(&args, dmirror); 711 migrate_vma_pages(&args); 712 dmirror_migrate_finalize_and_map(&args, dmirror); 713 migrate_vma_finalize(&args); 714 } 715 mmap_read_unlock(mm); 716 mmput(mm); 717 718 /* Return the migrated data for verification. */ 719 ret = dmirror_bounce_init(&bounce, start, size); 720 if (ret) 721 return ret; 722 mutex_lock(&dmirror->mutex); 723 ret = dmirror_do_read(dmirror, start, end, &bounce); 724 mutex_unlock(&dmirror->mutex); 725 if (ret == 0) { 726 if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr, 727 bounce.size)) 728 ret = -EFAULT; 729 } 730 cmd->cpages = bounce.cpages; 731 dmirror_bounce_fini(&bounce); 732 return ret; 733 734 out: 735 mmap_read_unlock(mm); 736 mmput(mm); 737 return ret; 738 } 739 740 static void dmirror_mkentry(struct dmirror *dmirror, struct hmm_range *range, 741 unsigned char *perm, unsigned long entry) 742 { 743 struct page *page; 744 745 if (entry & HMM_PFN_ERROR) { 746 *perm = HMM_DMIRROR_PROT_ERROR; 747 return; 748 } 749 if (!(entry & HMM_PFN_VALID)) { 750 *perm = HMM_DMIRROR_PROT_NONE; 751 return; 752 } 753 754 page = hmm_pfn_to_page(entry); 755 if (is_device_private_page(page)) { 756 /* Is the page migrated to this device or some other? */ 757 if (dmirror->mdevice == dmirror_page_to_device(page)) 758 *perm = HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL; 759 else 760 *perm = HMM_DMIRROR_PROT_DEV_PRIVATE_REMOTE; 761 } else if (is_zero_pfn(page_to_pfn(page))) 762 *perm = HMM_DMIRROR_PROT_ZERO; 763 else 764 *perm = HMM_DMIRROR_PROT_NONE; 765 if (entry & HMM_PFN_WRITE) 766 *perm |= HMM_DMIRROR_PROT_WRITE; 767 else 768 *perm |= HMM_DMIRROR_PROT_READ; 769 } 770 771 static bool dmirror_snapshot_invalidate(struct mmu_interval_notifier *mni, 772 const struct mmu_notifier_range *range, 773 unsigned long cur_seq) 774 { 775 struct dmirror_interval *dmi = 776 container_of(mni, struct dmirror_interval, notifier); 777 struct dmirror *dmirror = dmi->dmirror; 778 779 if (mmu_notifier_range_blockable(range)) 780 mutex_lock(&dmirror->mutex); 781 else if (!mutex_trylock(&dmirror->mutex)) 782 return false; 783 784 /* 785 * Snapshots only need to set the sequence number since any 786 * invalidation in the interval invalidates the whole snapshot. 787 */ 788 mmu_interval_set_seq(mni, cur_seq); 789 790 mutex_unlock(&dmirror->mutex); 791 return true; 792 } 793 794 static const struct mmu_interval_notifier_ops dmirror_mrn_ops = { 795 .invalidate = dmirror_snapshot_invalidate, 796 }; 797 798 static int dmirror_range_snapshot(struct dmirror *dmirror, 799 struct hmm_range *range, 800 unsigned char *perm) 801 { 802 struct mm_struct *mm = dmirror->notifier.mm; 803 struct dmirror_interval notifier; 804 unsigned long timeout = 805 jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT); 806 unsigned long i; 807 unsigned long n; 808 int ret = 0; 809 810 notifier.dmirror = dmirror; 811 range->notifier = ¬ifier.notifier; 812 813 ret = mmu_interval_notifier_insert(range->notifier, mm, 814 range->start, range->end - range->start, 815 &dmirror_mrn_ops); 816 if (ret) 817 return ret; 818 819 while (true) { 820 if (time_after(jiffies, timeout)) { 821 ret = -EBUSY; 822 goto out; 823 } 824 825 range->notifier_seq = mmu_interval_read_begin(range->notifier); 826 827 mmap_read_lock(mm); 828 ret = hmm_range_fault(range); 829 mmap_read_unlock(mm); 830 if (ret) { 831 if (ret == -EBUSY) 832 continue; 833 goto out; 834 } 835 836 mutex_lock(&dmirror->mutex); 837 if (mmu_interval_read_retry(range->notifier, 838 range->notifier_seq)) { 839 mutex_unlock(&dmirror->mutex); 840 continue; 841 } 842 break; 843 } 844 845 n = (range->end - range->start) >> PAGE_SHIFT; 846 for (i = 0; i < n; i++) 847 dmirror_mkentry(dmirror, range, perm + i, range->hmm_pfns[i]); 848 849 mutex_unlock(&dmirror->mutex); 850 out: 851 mmu_interval_notifier_remove(range->notifier); 852 return ret; 853 } 854 855 static int dmirror_snapshot(struct dmirror *dmirror, 856 struct hmm_dmirror_cmd *cmd) 857 { 858 struct mm_struct *mm = dmirror->notifier.mm; 859 unsigned long start, end; 860 unsigned long size = cmd->npages << PAGE_SHIFT; 861 unsigned long addr; 862 unsigned long next; 863 unsigned long pfns[64]; 864 unsigned char perm[64]; 865 char __user *uptr; 866 struct hmm_range range = { 867 .hmm_pfns = pfns, 868 .dev_private_owner = dmirror->mdevice, 869 }; 870 int ret = 0; 871 872 start = cmd->addr; 873 end = start + size; 874 if (end < start) 875 return -EINVAL; 876 877 /* Since the mm is for the mirrored process, get a reference first. */ 878 if (!mmget_not_zero(mm)) 879 return -EINVAL; 880 881 /* 882 * Register a temporary notifier to detect invalidations even if it 883 * overlaps with other mmu_interval_notifiers. 884 */ 885 uptr = u64_to_user_ptr(cmd->ptr); 886 for (addr = start; addr < end; addr = next) { 887 unsigned long n; 888 889 next = min(addr + (ARRAY_SIZE(pfns) << PAGE_SHIFT), end); 890 range.start = addr; 891 range.end = next; 892 893 ret = dmirror_range_snapshot(dmirror, &range, perm); 894 if (ret) 895 break; 896 897 n = (range.end - range.start) >> PAGE_SHIFT; 898 if (copy_to_user(uptr, perm, n)) { 899 ret = -EFAULT; 900 break; 901 } 902 903 cmd->cpages += n; 904 uptr += n; 905 } 906 mmput(mm); 907 908 return ret; 909 } 910 911 static long dmirror_fops_unlocked_ioctl(struct file *filp, 912 unsigned int command, 913 unsigned long arg) 914 { 915 void __user *uarg = (void __user *)arg; 916 struct hmm_dmirror_cmd cmd; 917 struct dmirror *dmirror; 918 int ret; 919 920 dmirror = filp->private_data; 921 if (!dmirror) 922 return -EINVAL; 923 924 if (copy_from_user(&cmd, uarg, sizeof(cmd))) 925 return -EFAULT; 926 927 if (cmd.addr & ~PAGE_MASK) 928 return -EINVAL; 929 if (cmd.addr >= (cmd.addr + (cmd.npages << PAGE_SHIFT))) 930 return -EINVAL; 931 932 cmd.cpages = 0; 933 cmd.faults = 0; 934 935 switch (command) { 936 case HMM_DMIRROR_READ: 937 ret = dmirror_read(dmirror, &cmd); 938 break; 939 940 case HMM_DMIRROR_WRITE: 941 ret = dmirror_write(dmirror, &cmd); 942 break; 943 944 case HMM_DMIRROR_MIGRATE: 945 ret = dmirror_migrate(dmirror, &cmd); 946 break; 947 948 case HMM_DMIRROR_SNAPSHOT: 949 ret = dmirror_snapshot(dmirror, &cmd); 950 break; 951 952 default: 953 return -EINVAL; 954 } 955 if (ret) 956 return ret; 957 958 if (copy_to_user(uarg, &cmd, sizeof(cmd))) 959 return -EFAULT; 960 961 return 0; 962 } 963 964 static const struct file_operations dmirror_fops = { 965 .open = dmirror_fops_open, 966 .release = dmirror_fops_release, 967 .unlocked_ioctl = dmirror_fops_unlocked_ioctl, 968 .llseek = default_llseek, 969 .owner = THIS_MODULE, 970 }; 971 972 static void dmirror_devmem_free(struct page *page) 973 { 974 struct page *rpage = page->zone_device_data; 975 struct dmirror_device *mdevice; 976 977 if (rpage) 978 __free_page(rpage); 979 980 mdevice = dmirror_page_to_device(page); 981 982 spin_lock(&mdevice->lock); 983 mdevice->cfree++; 984 page->zone_device_data = mdevice->free_pages; 985 mdevice->free_pages = page; 986 spin_unlock(&mdevice->lock); 987 } 988 989 static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args, 990 struct dmirror_device *mdevice) 991 { 992 const unsigned long *src = args->src; 993 unsigned long *dst = args->dst; 994 unsigned long start = args->start; 995 unsigned long end = args->end; 996 unsigned long addr; 997 998 for (addr = start; addr < end; addr += PAGE_SIZE, 999 src++, dst++) { 1000 struct page *dpage, *spage; 1001 1002 spage = migrate_pfn_to_page(*src); 1003 if (!spage || !(*src & MIGRATE_PFN_MIGRATE)) 1004 continue; 1005 spage = spage->zone_device_data; 1006 1007 dpage = alloc_page_vma(GFP_HIGHUSER_MOVABLE, args->vma, addr); 1008 if (!dpage) 1009 continue; 1010 1011 lock_page(dpage); 1012 copy_highpage(dpage, spage); 1013 *dst = migrate_pfn(page_to_pfn(dpage)) | MIGRATE_PFN_LOCKED; 1014 if (*src & MIGRATE_PFN_WRITE) 1015 *dst |= MIGRATE_PFN_WRITE; 1016 } 1017 return 0; 1018 } 1019 1020 static void dmirror_devmem_fault_finalize_and_map(struct migrate_vma *args, 1021 struct dmirror *dmirror) 1022 { 1023 /* Invalidate the device's page table mapping. */ 1024 mutex_lock(&dmirror->mutex); 1025 dmirror_do_update(dmirror, args->start, args->end); 1026 mutex_unlock(&dmirror->mutex); 1027 } 1028 1029 static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf) 1030 { 1031 struct migrate_vma args; 1032 unsigned long src_pfns; 1033 unsigned long dst_pfns; 1034 struct page *rpage; 1035 struct dmirror *dmirror; 1036 vm_fault_t ret; 1037 1038 /* 1039 * Normally, a device would use the page->zone_device_data to point to 1040 * the mirror but here we use it to hold the page for the simulated 1041 * device memory and that page holds the pointer to the mirror. 1042 */ 1043 rpage = vmf->page->zone_device_data; 1044 dmirror = rpage->zone_device_data; 1045 1046 /* FIXME demonstrate how we can adjust migrate range */ 1047 args.vma = vmf->vma; 1048 args.start = vmf->address; 1049 args.end = args.start + PAGE_SIZE; 1050 args.src = &src_pfns; 1051 args.dst = &dst_pfns; 1052 args.src_owner = dmirror->mdevice; 1053 1054 if (migrate_vma_setup(&args)) 1055 return VM_FAULT_SIGBUS; 1056 1057 ret = dmirror_devmem_fault_alloc_and_copy(&args, dmirror->mdevice); 1058 if (ret) 1059 return ret; 1060 migrate_vma_pages(&args); 1061 dmirror_devmem_fault_finalize_and_map(&args, dmirror); 1062 migrate_vma_finalize(&args); 1063 return 0; 1064 } 1065 1066 static const struct dev_pagemap_ops dmirror_devmem_ops = { 1067 .page_free = dmirror_devmem_free, 1068 .migrate_to_ram = dmirror_devmem_fault, 1069 }; 1070 1071 static int dmirror_device_init(struct dmirror_device *mdevice, int id) 1072 { 1073 dev_t dev; 1074 int ret; 1075 1076 dev = MKDEV(MAJOR(dmirror_dev), id); 1077 mutex_init(&mdevice->devmem_lock); 1078 spin_lock_init(&mdevice->lock); 1079 1080 cdev_init(&mdevice->cdevice, &dmirror_fops); 1081 mdevice->cdevice.owner = THIS_MODULE; 1082 ret = cdev_add(&mdevice->cdevice, dev, 1); 1083 if (ret) 1084 return ret; 1085 1086 /* Build a list of free ZONE_DEVICE private struct pages */ 1087 dmirror_allocate_chunk(mdevice, NULL); 1088 1089 return 0; 1090 } 1091 1092 static void dmirror_device_remove(struct dmirror_device *mdevice) 1093 { 1094 unsigned int i; 1095 1096 if (mdevice->devmem_chunks) { 1097 for (i = 0; i < mdevice->devmem_count; i++) { 1098 struct dmirror_chunk *devmem = 1099 mdevice->devmem_chunks[i]; 1100 1101 memunmap_pages(&devmem->pagemap); 1102 release_mem_region(devmem->pagemap.res.start, 1103 resource_size(&devmem->pagemap.res)); 1104 kfree(devmem); 1105 } 1106 kfree(mdevice->devmem_chunks); 1107 } 1108 1109 cdev_del(&mdevice->cdevice); 1110 } 1111 1112 static int __init hmm_dmirror_init(void) 1113 { 1114 int ret; 1115 int id; 1116 1117 ret = alloc_chrdev_region(&dmirror_dev, 0, DMIRROR_NDEVICES, 1118 "HMM_DMIRROR"); 1119 if (ret) 1120 goto err_unreg; 1121 1122 for (id = 0; id < DMIRROR_NDEVICES; id++) { 1123 ret = dmirror_device_init(dmirror_devices + id, id); 1124 if (ret) 1125 goto err_chrdev; 1126 } 1127 1128 /* 1129 * Allocate a zero page to simulate a reserved page of device private 1130 * memory which is always zero. The zero_pfn page isn't used just to 1131 * make the code here simpler (i.e., we need a struct page for it). 1132 */ 1133 dmirror_zero_page = alloc_page(GFP_HIGHUSER | __GFP_ZERO); 1134 if (!dmirror_zero_page) { 1135 ret = -ENOMEM; 1136 goto err_chrdev; 1137 } 1138 1139 pr_info("HMM test module loaded. This is only for testing HMM.\n"); 1140 return 0; 1141 1142 err_chrdev: 1143 while (--id >= 0) 1144 dmirror_device_remove(dmirror_devices + id); 1145 unregister_chrdev_region(dmirror_dev, DMIRROR_NDEVICES); 1146 err_unreg: 1147 return ret; 1148 } 1149 1150 static void __exit hmm_dmirror_exit(void) 1151 { 1152 int id; 1153 1154 if (dmirror_zero_page) 1155 __free_page(dmirror_zero_page); 1156 for (id = 0; id < DMIRROR_NDEVICES; id++) 1157 dmirror_device_remove(dmirror_devices + id); 1158 unregister_chrdev_region(dmirror_dev, DMIRROR_NDEVICES); 1159 } 1160 1161 module_init(hmm_dmirror_init); 1162 module_exit(hmm_dmirror_exit); 1163 MODULE_LICENSE("GPL"); 1164