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 2
36 #define DMIRROR_RANGE_FAULT_TIMEOUT 1000
37 #define DEVMEM_CHUNK_SIZE (256 * 1024 * 1024U)
38 #define DEVMEM_CHUNKS_RESERVE 16
40 static const struct dev_pagemap_ops dmirror_devmem_ops;
41 static const struct mmu_interval_notifier_ops dmirror_min_ops;
42 static dev_t dmirror_dev;
44 struct dmirror_device;
46 struct dmirror_bounce {
53 #define DPT_XA_TAG_ATOMIC 1UL
54 #define DPT_XA_TAG_WRITE 3UL
57 * Data structure to track address ranges and register for mmu interval
60 struct dmirror_interval {
61 struct mmu_interval_notifier notifier;
62 struct dmirror *dmirror;
66 * Data attached to the open device file.
67 * Note that it might be shared after a fork().
70 struct dmirror_device *mdevice;
72 struct mmu_interval_notifier notifier;
77 * ZONE_DEVICE pages for migration and simulating device memory.
79 struct dmirror_chunk {
80 struct dev_pagemap pagemap;
81 struct dmirror_device *mdevice;
87 struct dmirror_device {
89 struct hmm_devmem *devmem;
91 unsigned int devmem_capacity;
92 unsigned int devmem_count;
93 struct dmirror_chunk **devmem_chunks;
94 struct mutex devmem_lock; /* protects the above */
98 struct page *free_pages;
99 spinlock_t lock; /* protects the above */
102 static struct dmirror_device dmirror_devices[DMIRROR_NDEVICES];
104 static int dmirror_bounce_init(struct dmirror_bounce *bounce,
111 bounce->ptr = vmalloc(size);
117 static void dmirror_bounce_fini(struct dmirror_bounce *bounce)
122 static int dmirror_fops_open(struct inode *inode, struct file *filp)
124 struct cdev *cdev = inode->i_cdev;
125 struct dmirror *dmirror;
128 /* Mirror this process address space */
129 dmirror = kzalloc(sizeof(*dmirror), GFP_KERNEL);
133 dmirror->mdevice = container_of(cdev, struct dmirror_device, cdevice);
134 mutex_init(&dmirror->mutex);
135 xa_init(&dmirror->pt);
137 ret = mmu_interval_notifier_insert(&dmirror->notifier, current->mm,
138 0, ULONG_MAX & PAGE_MASK, &dmirror_min_ops);
144 filp->private_data = dmirror;
148 static int dmirror_fops_release(struct inode *inode, struct file *filp)
150 struct dmirror *dmirror = filp->private_data;
152 mmu_interval_notifier_remove(&dmirror->notifier);
153 xa_destroy(&dmirror->pt);
158 static struct dmirror_device *dmirror_page_to_device(struct page *page)
161 return container_of(page->pgmap, struct dmirror_chunk,
165 static int dmirror_do_fault(struct dmirror *dmirror, struct hmm_range *range)
167 unsigned long *pfns = range->hmm_pfns;
170 for (pfn = (range->start >> PAGE_SHIFT);
171 pfn < (range->end >> PAGE_SHIFT);
177 * Since we asked for hmm_range_fault() to populate pages,
178 * it shouldn't return an error entry on success.
180 WARN_ON(*pfns & HMM_PFN_ERROR);
181 WARN_ON(!(*pfns & HMM_PFN_VALID));
183 page = hmm_pfn_to_page(*pfns);
187 if (*pfns & HMM_PFN_WRITE)
188 entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE);
189 else if (WARN_ON(range->default_flags & HMM_PFN_WRITE))
191 entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC);
192 if (xa_is_err(entry))
193 return xa_err(entry);
199 static void dmirror_do_update(struct dmirror *dmirror, unsigned long start,
206 * The XArray doesn't hold references to pages since it relies on
207 * the mmu notifier to clear page pointers when they become stale.
208 * Therefore, it is OK to just clear the entry.
210 xa_for_each_range(&dmirror->pt, pfn, entry, start >> PAGE_SHIFT,
212 xa_erase(&dmirror->pt, pfn);
215 static bool dmirror_interval_invalidate(struct mmu_interval_notifier *mni,
216 const struct mmu_notifier_range *range,
217 unsigned long cur_seq)
219 struct dmirror *dmirror = container_of(mni, struct dmirror, notifier);
222 * Ignore invalidation callbacks for device private pages since
223 * the invalidation is handled as part of the migration process.
225 if (range->event == MMU_NOTIFY_MIGRATE &&
226 range->owner == dmirror->mdevice)
229 if (mmu_notifier_range_blockable(range))
230 mutex_lock(&dmirror->mutex);
231 else if (!mutex_trylock(&dmirror->mutex))
234 mmu_interval_set_seq(mni, cur_seq);
235 dmirror_do_update(dmirror, range->start, range->end);
237 mutex_unlock(&dmirror->mutex);
241 static const struct mmu_interval_notifier_ops dmirror_min_ops = {
242 .invalidate = dmirror_interval_invalidate,
245 static int dmirror_range_fault(struct dmirror *dmirror,
246 struct hmm_range *range)
248 struct mm_struct *mm = dmirror->notifier.mm;
249 unsigned long timeout =
250 jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
254 if (time_after(jiffies, timeout)) {
259 range->notifier_seq = mmu_interval_read_begin(range->notifier);
261 ret = hmm_range_fault(range);
262 mmap_read_unlock(mm);
269 mutex_lock(&dmirror->mutex);
270 if (mmu_interval_read_retry(range->notifier,
271 range->notifier_seq)) {
272 mutex_unlock(&dmirror->mutex);
278 ret = dmirror_do_fault(dmirror, range);
280 mutex_unlock(&dmirror->mutex);
285 static int dmirror_fault(struct dmirror *dmirror, unsigned long start,
286 unsigned long end, bool write)
288 struct mm_struct *mm = dmirror->notifier.mm;
290 unsigned long pfns[64];
291 struct hmm_range range = {
292 .notifier = &dmirror->notifier,
296 HMM_PFN_REQ_FAULT | (write ? HMM_PFN_REQ_WRITE : 0),
297 .dev_private_owner = dmirror->mdevice,
301 /* Since the mm is for the mirrored process, get a reference first. */
302 if (!mmget_not_zero(mm))
305 for (addr = start; addr < end; addr = range.end) {
307 range.end = min(addr + (ARRAY_SIZE(pfns) << PAGE_SHIFT), end);
309 ret = dmirror_range_fault(dmirror, &range);
318 static int dmirror_do_read(struct dmirror *dmirror, unsigned long start,
319 unsigned long end, struct dmirror_bounce *bounce)
324 ptr = bounce->ptr + ((start - bounce->addr) & PAGE_MASK);
326 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
331 entry = xa_load(&dmirror->pt, pfn);
332 page = xa_untag_pointer(entry);
337 memcpy(ptr, tmp, PAGE_SIZE);
347 static int dmirror_read(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd)
349 struct dmirror_bounce bounce;
350 unsigned long start, end;
351 unsigned long size = cmd->npages << PAGE_SHIFT;
359 ret = dmirror_bounce_init(&bounce, start, size);
364 mutex_lock(&dmirror->mutex);
365 ret = dmirror_do_read(dmirror, start, end, &bounce);
366 mutex_unlock(&dmirror->mutex);
370 start = cmd->addr + (bounce.cpages << PAGE_SHIFT);
371 ret = dmirror_fault(dmirror, start, end, false);
378 if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr,
382 cmd->cpages = bounce.cpages;
383 dmirror_bounce_fini(&bounce);
387 static int dmirror_do_write(struct dmirror *dmirror, unsigned long start,
388 unsigned long end, struct dmirror_bounce *bounce)
393 ptr = bounce->ptr + ((start - bounce->addr) & PAGE_MASK);
395 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
400 entry = xa_load(&dmirror->pt, pfn);
401 page = xa_untag_pointer(entry);
402 if (!page || xa_pointer_tag(entry) != DPT_XA_TAG_WRITE)
406 memcpy(tmp, ptr, PAGE_SIZE);
416 static int dmirror_write(struct dmirror *dmirror, struct hmm_dmirror_cmd *cmd)
418 struct dmirror_bounce bounce;
419 unsigned long start, end;
420 unsigned long size = cmd->npages << PAGE_SHIFT;
428 ret = dmirror_bounce_init(&bounce, start, size);
431 if (copy_from_user(bounce.ptr, u64_to_user_ptr(cmd->ptr),
438 mutex_lock(&dmirror->mutex);
439 ret = dmirror_do_write(dmirror, start, end, &bounce);
440 mutex_unlock(&dmirror->mutex);
444 start = cmd->addr + (bounce.cpages << PAGE_SHIFT);
445 ret = dmirror_fault(dmirror, start, end, true);
452 cmd->cpages = bounce.cpages;
453 dmirror_bounce_fini(&bounce);
457 static bool dmirror_allocate_chunk(struct dmirror_device *mdevice,
460 struct dmirror_chunk *devmem;
461 struct resource *res;
463 unsigned long pfn_first;
464 unsigned long pfn_last;
467 devmem = kzalloc(sizeof(*devmem), GFP_KERNEL);
471 res = request_free_mem_region(&iomem_resource, DEVMEM_CHUNK_SIZE,
476 devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
477 devmem->pagemap.range.start = res->start;
478 devmem->pagemap.range.end = res->end;
479 devmem->pagemap.nr_range = 1;
480 devmem->pagemap.ops = &dmirror_devmem_ops;
481 devmem->pagemap.owner = mdevice;
483 mutex_lock(&mdevice->devmem_lock);
485 if (mdevice->devmem_count == mdevice->devmem_capacity) {
486 struct dmirror_chunk **new_chunks;
487 unsigned int new_capacity;
489 new_capacity = mdevice->devmem_capacity +
490 DEVMEM_CHUNKS_RESERVE;
491 new_chunks = krealloc(mdevice->devmem_chunks,
492 sizeof(new_chunks[0]) * new_capacity,
496 mdevice->devmem_capacity = new_capacity;
497 mdevice->devmem_chunks = new_chunks;
500 ptr = memremap_pages(&devmem->pagemap, numa_node_id());
504 devmem->mdevice = mdevice;
505 pfn_first = devmem->pagemap.range.start >> PAGE_SHIFT;
506 pfn_last = pfn_first + (range_len(&devmem->pagemap.range) >> PAGE_SHIFT);
507 mdevice->devmem_chunks[mdevice->devmem_count++] = devmem;
509 mutex_unlock(&mdevice->devmem_lock);
511 pr_info("added new %u MB chunk (total %u chunks, %u MB) PFNs [0x%lx 0x%lx)\n",
512 DEVMEM_CHUNK_SIZE / (1024 * 1024),
513 mdevice->devmem_count,
514 mdevice->devmem_count * (DEVMEM_CHUNK_SIZE / (1024 * 1024)),
515 pfn_first, pfn_last);
517 spin_lock(&mdevice->lock);
518 for (pfn = pfn_first; pfn < pfn_last; pfn++) {
519 struct page *page = pfn_to_page(pfn);
521 page->zone_device_data = mdevice->free_pages;
522 mdevice->free_pages = page;
525 *ppage = mdevice->free_pages;
526 mdevice->free_pages = (*ppage)->zone_device_data;
529 spin_unlock(&mdevice->lock);
534 mutex_unlock(&mdevice->devmem_lock);
535 release_mem_region(devmem->pagemap.range.start, range_len(&devmem->pagemap.range));
542 static struct page *dmirror_devmem_alloc_page(struct dmirror_device *mdevice)
544 struct page *dpage = NULL;
548 * This is a fake device so we alloc real system memory to store
551 rpage = alloc_page(GFP_HIGHUSER);
555 spin_lock(&mdevice->lock);
557 if (mdevice->free_pages) {
558 dpage = mdevice->free_pages;
559 mdevice->free_pages = dpage->zone_device_data;
561 spin_unlock(&mdevice->lock);
563 spin_unlock(&mdevice->lock);
564 if (!dmirror_allocate_chunk(mdevice, &dpage))
568 dpage->zone_device_data = rpage;
577 static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args,
578 struct dmirror *dmirror)
580 struct dmirror_device *mdevice = dmirror->mdevice;
581 const unsigned long *src = args->src;
582 unsigned long *dst = args->dst;
585 for (addr = args->start; addr < args->end; addr += PAGE_SIZE,
591 if (!(*src & MIGRATE_PFN_MIGRATE))
595 * Note that spage might be NULL which is OK since it is an
596 * unallocated pte_none() or read-only zero page.
598 spage = migrate_pfn_to_page(*src);
600 dpage = dmirror_devmem_alloc_page(mdevice);
604 rpage = dpage->zone_device_data;
606 copy_highpage(rpage, spage);
608 clear_highpage(rpage);
611 * Normally, a device would use the page->zone_device_data to
612 * point to the mirror but here we use it to hold the page for
613 * the simulated device memory and that page holds the pointer
616 rpage->zone_device_data = dmirror;
618 *dst = migrate_pfn(page_to_pfn(dpage));
619 if ((*src & MIGRATE_PFN_WRITE) ||
620 (!spage && args->vma->vm_flags & VM_WRITE))
621 *dst |= MIGRATE_PFN_WRITE;
625 static int dmirror_check_atomic(struct dmirror *dmirror, unsigned long start,
630 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
633 entry = xa_load(&dmirror->pt, pfn);
634 if (xa_pointer_tag(entry) == DPT_XA_TAG_ATOMIC)
641 static int dmirror_atomic_map(unsigned long start, unsigned long end,
642 struct page **pages, struct dmirror *dmirror)
644 unsigned long pfn, mapped = 0;
647 /* Map the migrated pages into the device's page tables. */
648 mutex_lock(&dmirror->mutex);
650 for (i = 0, pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++, i++) {
657 entry = xa_tag_pointer(entry, DPT_XA_TAG_ATOMIC);
658 entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC);
659 if (xa_is_err(entry)) {
660 mutex_unlock(&dmirror->mutex);
661 return xa_err(entry);
667 mutex_unlock(&dmirror->mutex);
671 static int dmirror_migrate_finalize_and_map(struct migrate_vma *args,
672 struct dmirror *dmirror)
674 unsigned long start = args->start;
675 unsigned long end = args->end;
676 const unsigned long *src = args->src;
677 const unsigned long *dst = args->dst;
680 /* Map the migrated pages into the device's page tables. */
681 mutex_lock(&dmirror->mutex);
683 for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++,
688 if (!(*src & MIGRATE_PFN_MIGRATE))
691 dpage = migrate_pfn_to_page(*dst);
696 * Store the page that holds the data so the page table
697 * doesn't have to deal with ZONE_DEVICE private pages.
699 entry = dpage->zone_device_data;
700 if (*dst & MIGRATE_PFN_WRITE)
701 entry = xa_tag_pointer(entry, DPT_XA_TAG_WRITE);
702 entry = xa_store(&dmirror->pt, pfn, entry, GFP_ATOMIC);
703 if (xa_is_err(entry)) {
704 mutex_unlock(&dmirror->mutex);
705 return xa_err(entry);
709 mutex_unlock(&dmirror->mutex);
713 static int dmirror_exclusive(struct dmirror *dmirror,
714 struct hmm_dmirror_cmd *cmd)
716 unsigned long start, end, addr;
717 unsigned long size = cmd->npages << PAGE_SHIFT;
718 struct mm_struct *mm = dmirror->notifier.mm;
719 struct page *pages[64];
720 struct dmirror_bounce bounce;
729 /* Since the mm is for the mirrored process, get a reference first. */
730 if (!mmget_not_zero(mm))
734 for (addr = start; addr < end; addr = next) {
735 unsigned long mapped;
738 if (end < addr + (ARRAY_SIZE(pages) << PAGE_SHIFT))
741 next = addr + (ARRAY_SIZE(pages) << PAGE_SHIFT);
743 ret = make_device_exclusive_range(mm, addr, next, pages, NULL);
744 mapped = dmirror_atomic_map(addr, next, pages, dmirror);
745 for (i = 0; i < ret; i++) {
747 unlock_page(pages[i]);
752 if (addr + (mapped << PAGE_SHIFT) < next) {
753 mmap_read_unlock(mm);
758 mmap_read_unlock(mm);
761 /* Return the migrated data for verification. */
762 ret = dmirror_bounce_init(&bounce, start, size);
765 mutex_lock(&dmirror->mutex);
766 ret = dmirror_do_read(dmirror, start, end, &bounce);
767 mutex_unlock(&dmirror->mutex);
769 if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr,
774 cmd->cpages = bounce.cpages;
775 dmirror_bounce_fini(&bounce);
779 static int dmirror_migrate(struct dmirror *dmirror,
780 struct hmm_dmirror_cmd *cmd)
782 unsigned long start, end, addr;
783 unsigned long size = cmd->npages << PAGE_SHIFT;
784 struct mm_struct *mm = dmirror->notifier.mm;
785 struct vm_area_struct *vma;
786 unsigned long src_pfns[64];
787 unsigned long dst_pfns[64];
788 struct dmirror_bounce bounce;
789 struct migrate_vma args;
798 /* Since the mm is for the mirrored process, get a reference first. */
799 if (!mmget_not_zero(mm))
803 for (addr = start; addr < end; addr = next) {
804 vma = vma_lookup(mm, addr);
805 if (!vma || !(vma->vm_flags & VM_READ)) {
809 next = min(end, addr + (ARRAY_SIZE(src_pfns) << PAGE_SHIFT));
810 if (next > vma->vm_end)
818 args.pgmap_owner = dmirror->mdevice;
819 args.flags = MIGRATE_VMA_SELECT_SYSTEM;
820 ret = migrate_vma_setup(&args);
824 dmirror_migrate_alloc_and_copy(&args, dmirror);
825 migrate_vma_pages(&args);
826 dmirror_migrate_finalize_and_map(&args, dmirror);
827 migrate_vma_finalize(&args);
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,
844 cmd->cpages = bounce.cpages;
845 dmirror_bounce_fini(&bounce);
849 mmap_read_unlock(mm);
854 static void dmirror_mkentry(struct dmirror *dmirror, struct hmm_range *range,
855 unsigned char *perm, unsigned long entry)
859 if (entry & HMM_PFN_ERROR) {
860 *perm = HMM_DMIRROR_PROT_ERROR;
863 if (!(entry & HMM_PFN_VALID)) {
864 *perm = HMM_DMIRROR_PROT_NONE;
868 page = hmm_pfn_to_page(entry);
869 if (is_device_private_page(page)) {
870 /* Is the page migrated to this device or some other? */
871 if (dmirror->mdevice == dmirror_page_to_device(page))
872 *perm = HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL;
874 *perm = HMM_DMIRROR_PROT_DEV_PRIVATE_REMOTE;
875 } else if (is_zero_pfn(page_to_pfn(page)))
876 *perm = HMM_DMIRROR_PROT_ZERO;
878 *perm = HMM_DMIRROR_PROT_NONE;
879 if (entry & HMM_PFN_WRITE)
880 *perm |= HMM_DMIRROR_PROT_WRITE;
882 *perm |= HMM_DMIRROR_PROT_READ;
883 if (hmm_pfn_to_map_order(entry) + PAGE_SHIFT == PMD_SHIFT)
884 *perm |= HMM_DMIRROR_PROT_PMD;
885 else if (hmm_pfn_to_map_order(entry) + PAGE_SHIFT == PUD_SHIFT)
886 *perm |= HMM_DMIRROR_PROT_PUD;
889 static bool dmirror_snapshot_invalidate(struct mmu_interval_notifier *mni,
890 const struct mmu_notifier_range *range,
891 unsigned long cur_seq)
893 struct dmirror_interval *dmi =
894 container_of(mni, struct dmirror_interval, notifier);
895 struct dmirror *dmirror = dmi->dmirror;
897 if (mmu_notifier_range_blockable(range))
898 mutex_lock(&dmirror->mutex);
899 else if (!mutex_trylock(&dmirror->mutex))
903 * Snapshots only need to set the sequence number since any
904 * invalidation in the interval invalidates the whole snapshot.
906 mmu_interval_set_seq(mni, cur_seq);
908 mutex_unlock(&dmirror->mutex);
912 static const struct mmu_interval_notifier_ops dmirror_mrn_ops = {
913 .invalidate = dmirror_snapshot_invalidate,
916 static int dmirror_range_snapshot(struct dmirror *dmirror,
917 struct hmm_range *range,
920 struct mm_struct *mm = dmirror->notifier.mm;
921 struct dmirror_interval notifier;
922 unsigned long timeout =
923 jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
928 notifier.dmirror = dmirror;
929 range->notifier = ¬ifier.notifier;
931 ret = mmu_interval_notifier_insert(range->notifier, mm,
932 range->start, range->end - range->start,
938 if (time_after(jiffies, timeout)) {
943 range->notifier_seq = mmu_interval_read_begin(range->notifier);
946 ret = hmm_range_fault(range);
947 mmap_read_unlock(mm);
954 mutex_lock(&dmirror->mutex);
955 if (mmu_interval_read_retry(range->notifier,
956 range->notifier_seq)) {
957 mutex_unlock(&dmirror->mutex);
963 n = (range->end - range->start) >> PAGE_SHIFT;
964 for (i = 0; i < n; i++)
965 dmirror_mkentry(dmirror, range, perm + i, range->hmm_pfns[i]);
967 mutex_unlock(&dmirror->mutex);
969 mmu_interval_notifier_remove(range->notifier);
973 static int dmirror_snapshot(struct dmirror *dmirror,
974 struct hmm_dmirror_cmd *cmd)
976 struct mm_struct *mm = dmirror->notifier.mm;
977 unsigned long start, end;
978 unsigned long size = cmd->npages << PAGE_SHIFT;
981 unsigned long pfns[64];
982 unsigned char perm[64];
984 struct hmm_range range = {
986 .dev_private_owner = dmirror->mdevice,
995 /* Since the mm is for the mirrored process, get a reference first. */
996 if (!mmget_not_zero(mm))
1000 * Register a temporary notifier to detect invalidations even if it
1001 * overlaps with other mmu_interval_notifiers.
1003 uptr = u64_to_user_ptr(cmd->ptr);
1004 for (addr = start; addr < end; addr = next) {
1007 next = min(addr + (ARRAY_SIZE(pfns) << PAGE_SHIFT), end);
1011 ret = dmirror_range_snapshot(dmirror, &range, perm);
1015 n = (range.end - range.start) >> PAGE_SHIFT;
1016 if (copy_to_user(uptr, perm, n)) {
1029 static long dmirror_fops_unlocked_ioctl(struct file *filp,
1030 unsigned int command,
1033 void __user *uarg = (void __user *)arg;
1034 struct hmm_dmirror_cmd cmd;
1035 struct dmirror *dmirror;
1038 dmirror = filp->private_data;
1042 if (copy_from_user(&cmd, uarg, sizeof(cmd)))
1045 if (cmd.addr & ~PAGE_MASK)
1047 if (cmd.addr >= (cmd.addr + (cmd.npages << PAGE_SHIFT)))
1054 case HMM_DMIRROR_READ:
1055 ret = dmirror_read(dmirror, &cmd);
1058 case HMM_DMIRROR_WRITE:
1059 ret = dmirror_write(dmirror, &cmd);
1062 case HMM_DMIRROR_MIGRATE:
1063 ret = dmirror_migrate(dmirror, &cmd);
1066 case HMM_DMIRROR_EXCLUSIVE:
1067 ret = dmirror_exclusive(dmirror, &cmd);
1070 case HMM_DMIRROR_CHECK_EXCLUSIVE:
1071 ret = dmirror_check_atomic(dmirror, cmd.addr,
1072 cmd.addr + (cmd.npages << PAGE_SHIFT));
1075 case HMM_DMIRROR_SNAPSHOT:
1076 ret = dmirror_snapshot(dmirror, &cmd);
1085 if (copy_to_user(uarg, &cmd, sizeof(cmd)))
1091 static int dmirror_fops_mmap(struct file *file, struct vm_area_struct *vma)
1095 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE) {
1099 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
1103 ret = vm_insert_page(vma, addr, page);
1114 static const struct file_operations dmirror_fops = {
1115 .open = dmirror_fops_open,
1116 .release = dmirror_fops_release,
1117 .mmap = dmirror_fops_mmap,
1118 .unlocked_ioctl = dmirror_fops_unlocked_ioctl,
1119 .llseek = default_llseek,
1120 .owner = THIS_MODULE,
1123 static void dmirror_devmem_free(struct page *page)
1125 struct page *rpage = page->zone_device_data;
1126 struct dmirror_device *mdevice;
1131 mdevice = dmirror_page_to_device(page);
1133 spin_lock(&mdevice->lock);
1135 page->zone_device_data = mdevice->free_pages;
1136 mdevice->free_pages = page;
1137 spin_unlock(&mdevice->lock);
1140 static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args,
1141 struct dmirror *dmirror)
1143 const unsigned long *src = args->src;
1144 unsigned long *dst = args->dst;
1145 unsigned long start = args->start;
1146 unsigned long end = args->end;
1149 for (addr = start; addr < end; addr += PAGE_SIZE,
1151 struct page *dpage, *spage;
1153 spage = migrate_pfn_to_page(*src);
1154 if (!spage || !(*src & MIGRATE_PFN_MIGRATE))
1156 spage = spage->zone_device_data;
1158 dpage = alloc_page_vma(GFP_HIGHUSER_MOVABLE, args->vma, addr);
1163 xa_erase(&dmirror->pt, addr >> PAGE_SHIFT);
1164 copy_highpage(dpage, spage);
1165 *dst = migrate_pfn(page_to_pfn(dpage));
1166 if (*src & MIGRATE_PFN_WRITE)
1167 *dst |= MIGRATE_PFN_WRITE;
1172 static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)
1174 struct migrate_vma args;
1175 unsigned long src_pfns;
1176 unsigned long dst_pfns;
1178 struct dmirror *dmirror;
1182 * Normally, a device would use the page->zone_device_data to point to
1183 * the mirror but here we use it to hold the page for the simulated
1184 * device memory and that page holds the pointer to the mirror.
1186 rpage = vmf->page->zone_device_data;
1187 dmirror = rpage->zone_device_data;
1189 /* FIXME demonstrate how we can adjust migrate range */
1190 args.vma = vmf->vma;
1191 args.start = vmf->address;
1192 args.end = args.start + PAGE_SIZE;
1193 args.src = &src_pfns;
1194 args.dst = &dst_pfns;
1195 args.pgmap_owner = dmirror->mdevice;
1196 args.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE;
1198 if (migrate_vma_setup(&args))
1199 return VM_FAULT_SIGBUS;
1201 ret = dmirror_devmem_fault_alloc_and_copy(&args, dmirror);
1204 migrate_vma_pages(&args);
1206 * No device finalize step is needed since
1207 * dmirror_devmem_fault_alloc_and_copy() will have already
1208 * invalidated the device page table.
1210 migrate_vma_finalize(&args);
1214 static const struct dev_pagemap_ops dmirror_devmem_ops = {
1215 .page_free = dmirror_devmem_free,
1216 .migrate_to_ram = dmirror_devmem_fault,
1219 static int dmirror_device_init(struct dmirror_device *mdevice, int id)
1224 dev = MKDEV(MAJOR(dmirror_dev), id);
1225 mutex_init(&mdevice->devmem_lock);
1226 spin_lock_init(&mdevice->lock);
1228 cdev_init(&mdevice->cdevice, &dmirror_fops);
1229 mdevice->cdevice.owner = THIS_MODULE;
1230 ret = cdev_add(&mdevice->cdevice, dev, 1);
1234 /* Build a list of free ZONE_DEVICE private struct pages */
1235 dmirror_allocate_chunk(mdevice, NULL);
1240 static void dmirror_device_remove(struct dmirror_device *mdevice)
1244 if (mdevice->devmem_chunks) {
1245 for (i = 0; i < mdevice->devmem_count; i++) {
1246 struct dmirror_chunk *devmem =
1247 mdevice->devmem_chunks[i];
1249 memunmap_pages(&devmem->pagemap);
1250 release_mem_region(devmem->pagemap.range.start,
1251 range_len(&devmem->pagemap.range));
1254 kfree(mdevice->devmem_chunks);
1257 cdev_del(&mdevice->cdevice);
1260 static int __init hmm_dmirror_init(void)
1265 ret = alloc_chrdev_region(&dmirror_dev, 0, DMIRROR_NDEVICES,
1270 for (id = 0; id < DMIRROR_NDEVICES; id++) {
1271 ret = dmirror_device_init(dmirror_devices + id, id);
1276 pr_info("HMM test module loaded. This is only for testing HMM.\n");
1281 dmirror_device_remove(dmirror_devices + id);
1282 unregister_chrdev_region(dmirror_dev, DMIRROR_NDEVICES);
1287 static void __exit hmm_dmirror_exit(void)
1291 for (id = 0; id < DMIRROR_NDEVICES; id++)
1292 dmirror_device_remove(dmirror_devices + id);
1293 unregister_chrdev_region(dmirror_dev, DMIRROR_NDEVICES);
1296 module_init(hmm_dmirror_init);
1297 module_exit(hmm_dmirror_exit);
1298 MODULE_LICENSE("GPL");