1 // SPDX-License-Identifier: GPL-2.0
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.
8 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/cdev.h>
14 #include <linux/device.h>
15 #include <linux/memremap.h>
16 #include <linux/mutex.h>
17 #include <linux/rwsem.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/highmem.h>
21 #include <linux/delay.h>
22 #include <linux/pagemap.h>
23 #include <linux/hmm.h>
24 #include <linux/vmalloc.h>
25 #include <linux/swap.h>
26 #include <linux/swapops.h>
27 #include <linux/sched/mm.h>
28 #include <linux/platform_device.h>
29 #include <linux/rmap.h>
30 #include <linux/mmu_notifier.h>
31 #include <linux/migrate.h>
33 #include "test_hmm_uapi.h"
35 #define DMIRROR_NDEVICES 4
36 #define DMIRROR_RANGE_FAULT_TIMEOUT 1000
37 #define DEVMEM_CHUNK_SIZE (256 * 1024 * 1024U)
38 #define DEVMEM_CHUNKS_RESERVE 16
41 * For device_private pages, dpage is just a dummy struct page
42 * representing a piece of device memory. dmirror_devmem_alloc_page
43 * allocates a real system memory page as backing storage to fake a
44 * real device. zone_device_data points to that backing page. But
45 * for device_coherent memory, the struct page represents real
46 * physical CPU-accessible memory that we can use directly.
48 #define BACKING_PAGE(page) (is_device_private_page((page)) ? \
49 (page)->zone_device_data : (page))
51 static unsigned long spm_addr_dev0;
52 module_param(spm_addr_dev0, long, 0644);
53 MODULE_PARM_DESC(spm_addr_dev0,
54 "Specify start address for SPM (special purpose memory) used for device 0. By setting this Coherent device type will be used. Make sure spm_addr_dev1 is set too. Minimum SPM size should be DEVMEM_CHUNK_SIZE.");
56 static unsigned long spm_addr_dev1;
57 module_param(spm_addr_dev1, long, 0644);
58 MODULE_PARM_DESC(spm_addr_dev1,
59 "Specify start address for SPM (special purpose memory) used for device 1. By setting this Coherent device type will be used. Make sure spm_addr_dev0 is set too. Minimum SPM size should be DEVMEM_CHUNK_SIZE.");
61 static const struct dev_pagemap_ops dmirror_devmem_ops;
62 static const struct mmu_interval_notifier_ops dmirror_min_ops;
63 static dev_t dmirror_dev;
65 struct dmirror_device;
67 struct dmirror_bounce {
74 #define DPT_XA_TAG_ATOMIC 1UL
75 #define DPT_XA_TAG_WRITE 3UL
78 * Data structure to track address ranges and register for mmu interval
81 struct dmirror_interval {
82 struct mmu_interval_notifier notifier;
83 struct dmirror *dmirror;
87 * Data attached to the open device file.
88 * Note that it might be shared after a fork().
91 struct dmirror_device *mdevice;
93 struct mmu_interval_notifier notifier;
98 * ZONE_DEVICE pages for migration and simulating device memory.
100 struct dmirror_chunk {
101 struct dev_pagemap pagemap;
102 struct dmirror_device *mdevice;
108 struct dmirror_device {
110 struct hmm_devmem *devmem;
111 unsigned int zone_device_type;
113 unsigned int devmem_capacity;
114 unsigned int devmem_count;
115 struct dmirror_chunk **devmem_chunks;
116 struct mutex devmem_lock; /* protects the above */
118 unsigned long calloc;
120 struct page *free_pages;
121 spinlock_t lock; /* protects the above */
124 static struct dmirror_device dmirror_devices[DMIRROR_NDEVICES];
126 static int dmirror_bounce_init(struct dmirror_bounce *bounce,
133 bounce->ptr = vmalloc(size);
139 static bool dmirror_is_private_zone(struct dmirror_device *mdevice)
141 return (mdevice->zone_device_type ==
142 HMM_DMIRROR_MEMORY_DEVICE_PRIVATE) ? true : false;
145 static enum migrate_vma_direction
146 dmirror_select_device(struct dmirror *dmirror)
148 return (dmirror->mdevice->zone_device_type ==
149 HMM_DMIRROR_MEMORY_DEVICE_PRIVATE) ?
150 MIGRATE_VMA_SELECT_DEVICE_PRIVATE :
151 MIGRATE_VMA_SELECT_DEVICE_COHERENT;
154 static void dmirror_bounce_fini(struct dmirror_bounce *bounce)
159 static int dmirror_fops_open(struct inode *inode, struct file *filp)
161 struct cdev *cdev = inode->i_cdev;
162 struct dmirror *dmirror;
165 /* Mirror this process address space */
166 dmirror = kzalloc(sizeof(*dmirror), GFP_KERNEL);
170 dmirror->mdevice = container_of(cdev, struct dmirror_device, cdevice);
171 mutex_init(&dmirror->mutex);
172 xa_init(&dmirror->pt);
174 ret = mmu_interval_notifier_insert(&dmirror->notifier, current->mm,
175 0, ULONG_MAX & PAGE_MASK, &dmirror_min_ops);
181 filp->private_data = dmirror;
185 static int dmirror_fops_release(struct inode *inode, struct file *filp)
187 struct dmirror *dmirror = filp->private_data;
189 mmu_interval_notifier_remove(&dmirror->notifier);
190 xa_destroy(&dmirror->pt);
195 static struct dmirror_device *dmirror_page_to_device(struct page *page)
198 return container_of(page->pgmap, struct dmirror_chunk,
202 static int dmirror_do_fault(struct dmirror *dmirror, struct hmm_range *range)
204 unsigned long *pfns = range->hmm_pfns;
207 for (pfn = (range->start >> PAGE_SHIFT);
208 pfn < (range->end >> PAGE_SHIFT);
214 * Since we asked for hmm_range_fault() to populate pages,
215 * it shouldn't return an error entry on success.
217 WARN_ON(*pfns & HMM_PFN_ERROR);
218 WARN_ON(!(*pfns & HMM_PFN_VALID));
220 page = hmm_pfn_to_page(*pfns);
224 if (*pfns & HMM_PFN_WRITE)
225 entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE);
226 else if (WARN_ON(range->default_flags & HMM_PFN_WRITE))
228 entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC);
229 if (xa_is_err(entry))
230 return xa_err(entry);
236 static void dmirror_do_update(struct dmirror *dmirror, unsigned long start,
243 * The XArray doesn't hold references to pages since it relies on
244 * the mmu notifier to clear page pointers when they become stale.
245 * Therefore, it is OK to just clear the entry.
247 xa_for_each_range(&dmirror->pt, pfn, entry, start >> PAGE_SHIFT,
249 xa_erase(&dmirror->pt, pfn);
252 static bool dmirror_interval_invalidate(struct mmu_interval_notifier *mni,
253 const struct mmu_notifier_range *range,
254 unsigned long cur_seq)
256 struct dmirror *dmirror = container_of(mni, struct dmirror, notifier);
259 * Ignore invalidation callbacks for device private pages since
260 * the invalidation is handled as part of the migration process.
262 if (range->event == MMU_NOTIFY_MIGRATE &&
263 range->owner == dmirror->mdevice)
266 if (mmu_notifier_range_blockable(range))
267 mutex_lock(&dmirror->mutex);
268 else if (!mutex_trylock(&dmirror->mutex))
271 mmu_interval_set_seq(mni, cur_seq);
272 dmirror_do_update(dmirror, range->start, range->end);
274 mutex_unlock(&dmirror->mutex);
278 static const struct mmu_interval_notifier_ops dmirror_min_ops = {
279 .invalidate = dmirror_interval_invalidate,
282 static int dmirror_range_fault(struct dmirror *dmirror,
283 struct hmm_range *range)
285 struct mm_struct *mm = dmirror->notifier.mm;
286 unsigned long timeout =
287 jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
291 if (time_after(jiffies, timeout)) {
296 range->notifier_seq = mmu_interval_read_begin(range->notifier);
298 ret = hmm_range_fault(range);
299 mmap_read_unlock(mm);
306 mutex_lock(&dmirror->mutex);
307 if (mmu_interval_read_retry(range->notifier,
308 range->notifier_seq)) {
309 mutex_unlock(&dmirror->mutex);
315 ret = dmirror_do_fault(dmirror, range);
317 mutex_unlock(&dmirror->mutex);
322 static int dmirror_fault(struct dmirror *dmirror, unsigned long start,
323 unsigned long end, bool write)
325 struct mm_struct *mm = dmirror->notifier.mm;
327 unsigned long pfns[64];
328 struct hmm_range range = {
329 .notifier = &dmirror->notifier,
333 HMM_PFN_REQ_FAULT | (write ? HMM_PFN_REQ_WRITE : 0),
334 .dev_private_owner = dmirror->mdevice,
338 /* Since the mm is for the mirrored process, get a reference first. */
339 if (!mmget_not_zero(mm))
342 for (addr = start; addr < end; addr = range.end) {
344 range.end = min(addr + (ARRAY_SIZE(pfns) << PAGE_SHIFT), end);
346 ret = dmirror_range_fault(dmirror, &range);
355 static int dmirror_do_read(struct dmirror *dmirror, unsigned long start,
356 unsigned long end, struct dmirror_bounce *bounce)
361 ptr = bounce->ptr + ((start - bounce->addr) & PAGE_MASK);
363 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
368 entry = xa_load(&dmirror->pt, pfn);
369 page = xa_untag_pointer(entry);
374 memcpy(ptr, tmp, PAGE_SIZE);
384 static int dmirror_read(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd)
386 struct dmirror_bounce bounce;
387 unsigned long start, end;
388 unsigned long size = cmd->npages << PAGE_SHIFT;
396 ret = dmirror_bounce_init(&bounce, start, size);
401 mutex_lock(&dmirror->mutex);
402 ret = dmirror_do_read(dmirror, start, end, &bounce);
403 mutex_unlock(&dmirror->mutex);
407 start = cmd->addr + (bounce.cpages << PAGE_SHIFT);
408 ret = dmirror_fault(dmirror, start, end, false);
415 if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr,
419 cmd->cpages = bounce.cpages;
420 dmirror_bounce_fini(&bounce);
424 static int dmirror_do_write(struct dmirror *dmirror, unsigned long start,
425 unsigned long end, struct dmirror_bounce *bounce)
430 ptr = bounce->ptr + ((start - bounce->addr) & PAGE_MASK);
432 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
437 entry = xa_load(&dmirror->pt, pfn);
438 page = xa_untag_pointer(entry);
439 if (!page || xa_pointer_tag(entry) != DPT_XA_TAG_WRITE)
443 memcpy(tmp, ptr, PAGE_SIZE);
453 static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd)
455 struct dmirror_bounce bounce;
456 unsigned long start, end;
457 unsigned long size = cmd->npages << PAGE_SHIFT;
465 ret = dmirror_bounce_init(&bounce, start, size);
468 if (copy_from_user(bounce.ptr, u64_to_user_ptr(cmd->ptr),
475 mutex_lock(&dmirror->mutex);
476 ret = dmirror_do_write(dmirror, start, end, &bounce);
477 mutex_unlock(&dmirror->mutex);
481 start = cmd->addr + (bounce.cpages << PAGE_SHIFT);
482 ret = dmirror_fault(dmirror, start, end, true);
489 cmd->cpages = bounce.cpages;
490 dmirror_bounce_fini(&bounce);
494 static int dmirror_allocate_chunk(struct dmirror_device *mdevice,
497 struct dmirror_chunk *devmem;
498 struct resource *res = NULL;
500 unsigned long pfn_first;
501 unsigned long pfn_last;
505 devmem = kzalloc(sizeof(*devmem), GFP_KERNEL);
509 switch (mdevice->zone_device_type) {
510 case HMM_DMIRROR_MEMORY_DEVICE_PRIVATE:
511 res = request_free_mem_region(&iomem_resource, DEVMEM_CHUNK_SIZE,
513 if (IS_ERR_OR_NULL(res))
515 devmem->pagemap.range.start = res->start;
516 devmem->pagemap.range.end = res->end;
517 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
519 case HMM_DMIRROR_MEMORY_DEVICE_COHERENT:
520 devmem->pagemap.range.start = (MINOR(mdevice->cdevice.dev) - 2) ?
523 devmem->pagemap.range.end = devmem->pagemap.range.start +
524 DEVMEM_CHUNK_SIZE - 1;
525 devmem->pagemap.type = MEMORY_DEVICE_COHERENT;
532 devmem->pagemap.nr_range = 1;
533 devmem->pagemap.ops = &dmirror_devmem_ops;
534 devmem->pagemap.owner = mdevice;
536 mutex_lock(&mdevice->devmem_lock);
538 if (mdevice->devmem_count == mdevice->devmem_capacity) {
539 struct dmirror_chunk **new_chunks;
540 unsigned int new_capacity;
542 new_capacity = mdevice->devmem_capacity +
543 DEVMEM_CHUNKS_RESERVE;
544 new_chunks = krealloc(mdevice->devmem_chunks,
545 sizeof(new_chunks[0]) * new_capacity,
549 mdevice->devmem_capacity = new_capacity;
550 mdevice->devmem_chunks = new_chunks;
552 ptr = memremap_pages(&devmem->pagemap, numa_node_id());
553 if (IS_ERR_OR_NULL(ptr)) {
561 devmem->mdevice = mdevice;
562 pfn_first = devmem->pagemap.range.start >> PAGE_SHIFT;
563 pfn_last = pfn_first + (range_len(&devmem->pagemap.range) >> PAGE_SHIFT);
564 mdevice->devmem_chunks[mdevice->devmem_count++] = devmem;
566 mutex_unlock(&mdevice->devmem_lock);
568 pr_info("added new %u MB chunk (total %u chunks, %u MB) PFNs [0x%lx 0x%lx)\n",
569 DEVMEM_CHUNK_SIZE / (1024 * 1024),
570 mdevice->devmem_count,
571 mdevice->devmem_count * (DEVMEM_CHUNK_SIZE / (1024 * 1024)),
572 pfn_first, pfn_last);
574 spin_lock(&mdevice->lock);
575 for (pfn = pfn_first; pfn < pfn_last; pfn++) {
576 struct page *page = pfn_to_page(pfn);
578 page->zone_device_data = mdevice->free_pages;
579 mdevice->free_pages = page;
582 *ppage = mdevice->free_pages;
583 mdevice->free_pages = (*ppage)->zone_device_data;
586 spin_unlock(&mdevice->lock);
591 mutex_unlock(&mdevice->devmem_lock);
592 if (res && devmem->pagemap.type == MEMORY_DEVICE_PRIVATE)
593 release_mem_region(devmem->pagemap.range.start,
594 range_len(&devmem->pagemap.range));
601 static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice)
603 struct page *dpage = NULL;
604 struct page *rpage = NULL;
607 * For ZONE_DEVICE private type, this is a fake device so we allocate
608 * real system memory to store our device memory.
609 * For ZONE_DEVICE coherent type we use the actual dpage to store the
610 * data and ignore rpage.
612 if (dmirror_is_private_zone(mdevice)) {
613 rpage = alloc_page(GFP_HIGHUSER);
617 spin_lock(&mdevice->lock);
619 if (mdevice->free_pages) {
620 dpage = mdevice->free_pages;
621 mdevice->free_pages = dpage->zone_device_data;
623 spin_unlock(&mdevice->lock);
625 spin_unlock(&mdevice->lock);
626 if (dmirror_allocate_chunk(mdevice, &dpage))
630 dpage->zone_device_data = rpage;
640 static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
641 struct dmirror *dmirror)
643 struct dmirror_device *mdevice = dmirror->mdevice;
644 const unsigned long *src = args->src;
645 unsigned long *dst = args->dst;
648 for (addr = args->start; addr < args->end; addr += PAGE_SIZE,
654 if (!(*src & MIGRATE_PFN_MIGRATE))
658 * Note that spage might be NULL which is OK since it is an
659 * unallocated pte_none() or read-only zero page.
661 spage = migrate_pfn_to_page(*src);
662 if (WARN(spage && is_zone_device_page(spage),
663 "page already in device spage pfn: 0x%lx\n",
667 dpage = dmirror_devmem_alloc_page(mdevice);
671 rpage = BACKING_PAGE(dpage);
673 copy_highpage(rpage, spage);
675 clear_highpage(rpage);
678 * Normally, a device would use the page->zone_device_data to
679 * point to the mirror but here we use it to hold the page for
680 * the simulated device memory and that page holds the pointer
683 rpage->zone_device_data = dmirror;
685 pr_debug("migrating from sys to dev pfn src: 0x%lx pfn dst: 0x%lx\n",
686 page_to_pfn(spage), page_to_pfn(dpage));
687 *dst = migrate_pfn(page_to_pfn(dpage));
688 if ((*src & MIGRATE_PFN_WRITE) ||
689 (!spage && args->vma->vm_flags & VM_WRITE))
690 *dst |= MIGRATE_PFN_WRITE;
694 static int dmirror_check_atomic(struct dmirror *dmirror, unsigned long start,
699 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
702 entry = xa_load(&dmirror->pt, pfn);
703 if (xa_pointer_tag(entry) == DPT_XA_TAG_ATOMIC)
710 static int dmirror_atomic_map(unsigned long start, unsigned long end,
711 struct page **pages, struct dmirror *dmirror)
713 unsigned long pfn, mapped = 0;
716 /* Map the migrated pages into the device's page tables. */
717 mutex_lock(&dmirror->mutex);
719 for (i = 0, pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++, i++) {
726 entry = xa_tag_pointer(entry, DPT_XA_TAG_ATOMIC);
727 entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC);
728 if (xa_is_err(entry)) {
729 mutex_unlock(&dmirror->mutex);
730 return xa_err(entry);
736 mutex_unlock(&dmirror->mutex);
740 static int dmirror_migrate_finalize_and_map(struct migrate_vma *args,
741 struct dmirror *dmirror)
743 unsigned long start = args->start;
744 unsigned long end = args->end;
745 const unsigned long *src = args->src;
746 const unsigned long *dst = args->dst;
749 /* Map the migrated pages into the device's page tables. */
750 mutex_lock(&dmirror->mutex);
752 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++,
757 if (!(*src & MIGRATE_PFN_MIGRATE))
760 dpage = migrate_pfn_to_page(*dst);
764 entry = BACKING_PAGE(dpage);
765 if (*dst & MIGRATE_PFN_WRITE)
766 entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE);
767 entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC);
768 if (xa_is_err(entry)) {
769 mutex_unlock(&dmirror->mutex);
770 return xa_err(entry);
774 mutex_unlock(&dmirror->mutex);
778 static int dmirror_exclusive(struct dmirror *dmirror,
779 struct hmm_dmirror_cmd *cmd)
781 unsigned long start, end, addr;
782 unsigned long size = cmd->npages << PAGE_SHIFT;
783 struct mm_struct *mm = dmirror->notifier.mm;
784 struct page *pages[64];
785 struct dmirror_bounce bounce;
794 /* Since the mm is for the mirrored process, get a reference first. */
795 if (!mmget_not_zero(mm))
799 for (addr = start; addr < end; addr = next) {
800 unsigned long mapped = 0;
803 if (end < addr + (ARRAY_SIZE(pages) << PAGE_SHIFT))
806 next = addr + (ARRAY_SIZE(pages) << PAGE_SHIFT);
808 ret = make_device_exclusive_range(mm, addr, next, pages, NULL);
810 * Do dmirror_atomic_map() iff all pages are marked for
811 * exclusive access to avoid accessing uninitialized
814 if (ret == (next - addr) >> PAGE_SHIFT)
815 mapped = dmirror_atomic_map(addr, next, pages, dmirror);
816 for (i = 0; i < ret; i++) {
818 unlock_page(pages[i]);
823 if (addr + (mapped << PAGE_SHIFT) < next) {
824 mmap_read_unlock(mm);
829 mmap_read_unlock(mm);
832 /* Return the migrated data for verification. */
833 ret = dmirror_bounce_init(&bounce, start, size);
836 mutex_lock(&dmirror->mutex);
837 ret = dmirror_do_read(dmirror, start, end, &bounce);
838 mutex_unlock(&dmirror->mutex);
840 if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr,
845 cmd->cpages = bounce.cpages;
846 dmirror_bounce_fini(&bounce);
850 static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args,
851 struct dmirror *dmirror)
853 const unsigned long *src = args->src;
854 unsigned long *dst = args->dst;
855 unsigned long start = args->start;
856 unsigned long end = args->end;
859 for (addr = start; addr < end; addr += PAGE_SIZE,
861 struct page *dpage, *spage;
863 spage = migrate_pfn_to_page(*src);
864 if (!spage || !(*src & MIGRATE_PFN_MIGRATE))
867 if (WARN_ON(!is_device_private_page(spage) &&
868 !is_device_coherent_page(spage)))
870 spage = BACKING_PAGE(spage);
871 dpage = alloc_page_vma(GFP_HIGHUSER_MOVABLE, args->vma, addr);
874 pr_debug("migrating from dev to sys pfn src: 0x%lx pfn dst: 0x%lx\n",
875 page_to_pfn(spage), page_to_pfn(dpage));
878 xa_erase(&dmirror->pt, addr >> PAGE_SHIFT);
879 copy_highpage(dpage, spage);
880 *dst = migrate_pfn(page_to_pfn(dpage));
881 if (*src & MIGRATE_PFN_WRITE)
882 *dst |= MIGRATE_PFN_WRITE;
888 dmirror_successful_migrated_pages(struct migrate_vma *migrate)
890 unsigned long cpages = 0;
893 for (i = 0; i < migrate->npages; i++) {
894 if (migrate->src[i] & MIGRATE_PFN_VALID &&
895 migrate->src[i] & MIGRATE_PFN_MIGRATE)
901 static int dmirror_migrate_to_system(struct dmirror *dmirror,
902 struct hmm_dmirror_cmd *cmd)
904 unsigned long start, end, addr;
905 unsigned long size = cmd->npages << PAGE_SHIFT;
906 struct mm_struct *mm = dmirror->notifier.mm;
907 struct vm_area_struct *vma;
908 unsigned long src_pfns[64] = { 0 };
909 unsigned long dst_pfns[64] = { 0 };
910 struct migrate_vma args;
919 /* Since the mm is for the mirrored process, get a reference first. */
920 if (!mmget_not_zero(mm))
925 for (addr = start; addr < end; addr = next) {
926 vma = vma_lookup(mm, addr);
927 if (!vma || !(vma->vm_flags & VM_READ)) {
931 next = min(end, addr + (ARRAY_SIZE(src_pfns) << PAGE_SHIFT));
932 if (next > vma->vm_end)
940 args.pgmap_owner = dmirror->mdevice;
941 args.flags = dmirror_select_device(dmirror);
943 ret = migrate_vma_setup(&args);
947 pr_debug("Migrating from device mem to sys mem\n");
948 dmirror_devmem_fault_alloc_and_copy(&args, dmirror);
950 migrate_vma_pages(&args);
951 cmd->cpages += dmirror_successful_migrated_pages(&args);
952 migrate_vma_finalize(&args);
955 mmap_read_unlock(mm);
961 static int dmirror_migrate_to_device(struct dmirror *dmirror,
962 struct hmm_dmirror_cmd *cmd)
964 unsigned long start, end, addr;
965 unsigned long size = cmd->npages << PAGE_SHIFT;
966 struct mm_struct *mm = dmirror->notifier.mm;
967 struct vm_area_struct *vma;
968 unsigned long src_pfns[64] = { 0 };
969 unsigned long dst_pfns[64] = { 0 };
970 struct dmirror_bounce bounce;
971 struct migrate_vma args;
980 /* Since the mm is for the mirrored process, get a reference first. */
981 if (!mmget_not_zero(mm))
985 for (addr = start; addr < end; addr = next) {
986 vma = vma_lookup(mm, addr);
987 if (!vma || !(vma->vm_flags & VM_READ)) {
991 next = min(end, addr + (ARRAY_SIZE(src_pfns) << PAGE_SHIFT));
992 if (next > vma->vm_end)
1000 args.pgmap_owner = dmirror->mdevice;
1001 args.flags = MIGRATE_VMA_SELECT_SYSTEM;
1002 ret = migrate_vma_setup(&args);
1006 pr_debug("Migrating from sys mem to device mem\n");
1007 dmirror_migrate_alloc_and_copy(&args, dmirror);
1008 migrate_vma_pages(&args);
1009 dmirror_migrate_finalize_and_map(&args, dmirror);
1010 migrate_vma_finalize(&args);
1012 mmap_read_unlock(mm);
1016 * Return the migrated data for verification.
1017 * Only for pages in device zone
1019 ret = dmirror_bounce_init(&bounce, start, size);
1022 mutex_lock(&dmirror->mutex);
1023 ret = dmirror_do_read(dmirror, start, end, &bounce);
1024 mutex_unlock(&dmirror->mutex);
1026 if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr,
1030 cmd->cpages = bounce.cpages;
1031 dmirror_bounce_fini(&bounce);
1035 mmap_read_unlock(mm);
1040 static void dmirror_mkentry(struct dmirror *dmirror, struct hmm_range *range,
1041 unsigned char *perm, unsigned long entry)
1045 if (entry & HMM_PFN_ERROR) {
1046 *perm = HMM_DMIRROR_PROT_ERROR;
1049 if (!(entry & HMM_PFN_VALID)) {
1050 *perm = HMM_DMIRROR_PROT_NONE;
1054 page = hmm_pfn_to_page(entry);
1055 if (is_device_private_page(page)) {
1056 /* Is the page migrated to this device or some other? */
1057 if (dmirror->mdevice == dmirror_page_to_device(page))
1058 *perm = HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL;
1060 *perm = HMM_DMIRROR_PROT_DEV_PRIVATE_REMOTE;
1061 } else if (is_device_coherent_page(page)) {
1062 /* Is the page migrated to this device or some other? */
1063 if (dmirror->mdevice == dmirror_page_to_device(page))
1064 *perm = HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL;
1066 *perm = HMM_DMIRROR_PROT_DEV_COHERENT_REMOTE;
1067 } else if (is_zero_pfn(page_to_pfn(page)))
1068 *perm = HMM_DMIRROR_PROT_ZERO;
1070 *perm = HMM_DMIRROR_PROT_NONE;
1071 if (entry & HMM_PFN_WRITE)
1072 *perm |= HMM_DMIRROR_PROT_WRITE;
1074 *perm |= HMM_DMIRROR_PROT_READ;
1075 if (hmm_pfn_to_map_order(entry) + PAGE_SHIFT == PMD_SHIFT)
1076 *perm |= HMM_DMIRROR_PROT_PMD;
1077 else if (hmm_pfn_to_map_order(entry) + PAGE_SHIFT == PUD_SHIFT)
1078 *perm |= HMM_DMIRROR_PROT_PUD;
1081 static bool dmirror_snapshot_invalidate(struct mmu_interval_notifier *mni,
1082 const struct mmu_notifier_range *range,
1083 unsigned long cur_seq)
1085 struct dmirror_interval *dmi =
1086 container_of(mni, struct dmirror_interval, notifier);
1087 struct dmirror *dmirror = dmi->dmirror;
1089 if (mmu_notifier_range_blockable(range))
1090 mutex_lock(&dmirror->mutex);
1091 else if (!mutex_trylock(&dmirror->mutex))
1095 * Snapshots only need to set the sequence number since any
1096 * invalidation in the interval invalidates the whole snapshot.
1098 mmu_interval_set_seq(mni, cur_seq);
1100 mutex_unlock(&dmirror->mutex);
1104 static const struct mmu_interval_notifier_ops dmirror_mrn_ops = {
1105 .invalidate = dmirror_snapshot_invalidate,
1108 static int dmirror_range_snapshot(struct dmirror *dmirror,
1109 struct hmm_range *range,
1110 unsigned char *perm)
1112 struct mm_struct *mm = dmirror->notifier.mm;
1113 struct dmirror_interval notifier;
1114 unsigned long timeout =
1115 jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
1120 notifier.dmirror = dmirror;
1121 range->notifier = ¬ifier.notifier;
1123 ret = mmu_interval_notifier_insert(range->notifier, mm,
1124 range->start, range->end - range->start,
1130 if (time_after(jiffies, timeout)) {
1135 range->notifier_seq = mmu_interval_read_begin(range->notifier);
1138 ret = hmm_range_fault(range);
1139 mmap_read_unlock(mm);
1146 mutex_lock(&dmirror->mutex);
1147 if (mmu_interval_read_retry(range->notifier,
1148 range->notifier_seq)) {
1149 mutex_unlock(&dmirror->mutex);
1155 n = (range->end - range->start) >> PAGE_SHIFT;
1156 for (i = 0; i < n; i++)
1157 dmirror_mkentry(dmirror, range, perm + i, range->hmm_pfns[i]);
1159 mutex_unlock(&dmirror->mutex);
1161 mmu_interval_notifier_remove(range->notifier);
1165 static int dmirror_snapshot(struct dmirror *dmirror,
1166 struct hmm_dmirror_cmd *cmd)
1168 struct mm_struct *mm = dmirror->notifier.mm;
1169 unsigned long start, end;
1170 unsigned long size = cmd->npages << PAGE_SHIFT;
1173 unsigned long pfns[64];
1174 unsigned char perm[64];
1176 struct hmm_range range = {
1178 .dev_private_owner = dmirror->mdevice,
1187 /* Since the mm is for the mirrored process, get a reference first. */
1188 if (!mmget_not_zero(mm))
1192 * Register a temporary notifier to detect invalidations even if it
1193 * overlaps with other mmu_interval_notifiers.
1195 uptr = u64_to_user_ptr(cmd->ptr);
1196 for (addr = start; addr < end; addr = next) {
1199 next = min(addr + (ARRAY_SIZE(pfns) << PAGE_SHIFT), end);
1203 ret = dmirror_range_snapshot(dmirror, &range, perm);
1207 n = (range.end - range.start) >> PAGE_SHIFT;
1208 if (copy_to_user(uptr, perm, n)) {
1221 static long dmirror_fops_unlocked_ioctl(struct file *filp,
1222 unsigned int command,
1225 void __user *uarg = (void __user *)arg;
1226 struct hmm_dmirror_cmd cmd;
1227 struct dmirror *dmirror;
1230 dmirror = filp->private_data;
1234 if (copy_from_user(&cmd, uarg, sizeof(cmd)))
1237 if (cmd.addr & ~PAGE_MASK)
1239 if (cmd.addr >= (cmd.addr + (cmd.npages << PAGE_SHIFT)))
1246 case HMM_DMIRROR_READ:
1247 ret = dmirror_read(dmirror, &cmd);
1250 case HMM_DMIRROR_WRITE:
1251 ret = dmirror_write(dmirror, &cmd);
1254 case HMM_DMIRROR_MIGRATE_TO_DEV:
1255 ret = dmirror_migrate_to_device(dmirror, &cmd);
1258 case HMM_DMIRROR_MIGRATE_TO_SYS:
1259 ret = dmirror_migrate_to_system(dmirror, &cmd);
1262 case HMM_DMIRROR_EXCLUSIVE:
1263 ret = dmirror_exclusive(dmirror, &cmd);
1266 case HMM_DMIRROR_CHECK_EXCLUSIVE:
1267 ret = dmirror_check_atomic(dmirror, cmd.addr,
1268 cmd.addr + (cmd.npages << PAGE_SHIFT));
1271 case HMM_DMIRROR_SNAPSHOT:
1272 ret = dmirror_snapshot(dmirror, &cmd);
1281 if (copy_to_user(uarg, &cmd, sizeof(cmd)))
1287 static int dmirror_fops_mmap(struct file *file, struct vm_area_struct *vma)
1291 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE) {
1295 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1299 ret = vm_insert_page(vma, addr, page);
1310 static const struct file_operations dmirror_fops = {
1311 .open = dmirror_fops_open,
1312 .release = dmirror_fops_release,
1313 .mmap = dmirror_fops_mmap,
1314 .unlocked_ioctl = dmirror_fops_unlocked_ioctl,
1315 .llseek = default_llseek,
1316 .owner = THIS_MODULE,
1319 static void dmirror_devmem_free(struct page *page)
1321 struct page *rpage = BACKING_PAGE(page);
1322 struct dmirror_device *mdevice;
1327 mdevice = dmirror_page_to_device(page);
1328 spin_lock(&mdevice->lock);
1330 page->zone_device_data = mdevice->free_pages;
1331 mdevice->free_pages = page;
1332 spin_unlock(&mdevice->lock);
1335 static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)
1337 struct migrate_vma args;
1338 unsigned long src_pfns = 0;
1339 unsigned long dst_pfns = 0;
1341 struct dmirror *dmirror;
1345 * Normally, a device would use the page->zone_device_data to point to
1346 * the mirror but here we use it to hold the page for the simulated
1347 * device memory and that page holds the pointer to the mirror.
1349 rpage = vmf->page->zone_device_data;
1350 dmirror = rpage->zone_device_data;
1352 /* FIXME demonstrate how we can adjust migrate range */
1353 args.vma = vmf->vma;
1354 args.start = vmf->address;
1355 args.end = args.start + PAGE_SIZE;
1356 args.src = &src_pfns;
1357 args.dst = &dst_pfns;
1358 args.pgmap_owner = dmirror->mdevice;
1359 args.flags = dmirror_select_device(dmirror);
1361 if (migrate_vma_setup(&args))
1362 return VM_FAULT_SIGBUS;
1364 ret = dmirror_devmem_fault_alloc_and_copy(&args, dmirror);
1367 migrate_vma_pages(&args);
1369 * No device finalize step is needed since
1370 * dmirror_devmem_fault_alloc_and_copy() will have already
1371 * invalidated the device page table.
1373 migrate_vma_finalize(&args);
1377 static const struct dev_pagemap_ops dmirror_devmem_ops = {
1378 .page_free = dmirror_devmem_free,
1379 .migrate_to_ram = dmirror_devmem_fault,
1382 static int dmirror_device_init(struct dmirror_device *mdevice, int id)
1387 dev = MKDEV(MAJOR(dmirror_dev), id);
1388 mutex_init(&mdevice->devmem_lock);
1389 spin_lock_init(&mdevice->lock);
1391 cdev_init(&mdevice->cdevice, &dmirror_fops);
1392 mdevice->cdevice.owner = THIS_MODULE;
1393 ret = cdev_add(&mdevice->cdevice, dev, 1);
1397 /* Build a list of free ZONE_DEVICE struct pages */
1398 return dmirror_allocate_chunk(mdevice, NULL);
1401 static void dmirror_device_remove(struct dmirror_device *mdevice)
1405 if (mdevice->devmem_chunks) {
1406 for (i = 0; i < mdevice->devmem_count; i++) {
1407 struct dmirror_chunk *devmem =
1408 mdevice->devmem_chunks[i];
1410 memunmap_pages(&devmem->pagemap);
1411 if (devmem->pagemap.type == MEMORY_DEVICE_PRIVATE)
1412 release_mem_region(devmem->pagemap.range.start,
1413 range_len(&devmem->pagemap.range));
1416 kfree(mdevice->devmem_chunks);
1419 cdev_del(&mdevice->cdevice);
1422 static int __init hmm_dmirror_init(void)
1428 ret = alloc_chrdev_region(&dmirror_dev, 0, DMIRROR_NDEVICES,
1433 memset(dmirror_devices, 0, DMIRROR_NDEVICES * sizeof(dmirror_devices[0]));
1434 dmirror_devices[ndevices++].zone_device_type =
1435 HMM_DMIRROR_MEMORY_DEVICE_PRIVATE;
1436 dmirror_devices[ndevices++].zone_device_type =
1437 HMM_DMIRROR_MEMORY_DEVICE_PRIVATE;
1438 if (spm_addr_dev0 && spm_addr_dev1) {
1439 dmirror_devices[ndevices++].zone_device_type =
1440 HMM_DMIRROR_MEMORY_DEVICE_COHERENT;
1441 dmirror_devices[ndevices++].zone_device_type =
1442 HMM_DMIRROR_MEMORY_DEVICE_COHERENT;
1444 for (id = 0; id < ndevices; id++) {
1445 ret = dmirror_device_init(dmirror_devices + id, id);
1450 pr_info("HMM test module loaded. This is only for testing HMM.\n");
1455 dmirror_device_remove(dmirror_devices + id);
1456 unregister_chrdev_region(dmirror_dev, DMIRROR_NDEVICES);
1461 static void __exit hmm_dmirror_exit(void)
1465 for (id = 0; id < DMIRROR_NDEVICES; id++)
1466 if (dmirror_devices[id].zone_device_type)
1467 dmirror_device_remove(dmirror_devices + id);
1468 unregister_chrdev_region(dmirror_dev, DMIRROR_NDEVICES);
1471 module_init(hmm_dmirror_init);
1472 module_exit(hmm_dmirror_exit);
1473 MODULE_LICENSE("GPL");