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
;
109 struct dmirror_device
{
111 unsigned int zone_device_type
;
112 struct device device
;
114 unsigned int devmem_capacity
;
115 unsigned int devmem_count
;
116 struct dmirror_chunk
**devmem_chunks
;
117 struct mutex devmem_lock
; /* protects the above */
119 unsigned long calloc
;
121 struct page
*free_pages
;
122 spinlock_t lock
; /* protects the above */
125 static struct dmirror_device dmirror_devices
[DMIRROR_NDEVICES
];
127 static int dmirror_bounce_init(struct dmirror_bounce
*bounce
,
134 bounce
->ptr
= vmalloc(size
);
140 static bool dmirror_is_private_zone(struct dmirror_device
*mdevice
)
142 return (mdevice
->zone_device_type
==
143 HMM_DMIRROR_MEMORY_DEVICE_PRIVATE
) ? true : false;
146 static enum migrate_vma_direction
147 dmirror_select_device(struct dmirror
*dmirror
)
149 return (dmirror
->mdevice
->zone_device_type
==
150 HMM_DMIRROR_MEMORY_DEVICE_PRIVATE
) ?
151 MIGRATE_VMA_SELECT_DEVICE_PRIVATE
:
152 MIGRATE_VMA_SELECT_DEVICE_COHERENT
;
155 static void dmirror_bounce_fini(struct dmirror_bounce
*bounce
)
160 static int dmirror_fops_open(struct inode
*inode
, struct file
*filp
)
162 struct cdev
*cdev
= inode
->i_cdev
;
163 struct dmirror
*dmirror
;
166 /* Mirror this process address space */
167 dmirror
= kzalloc(sizeof(*dmirror
), GFP_KERNEL
);
171 dmirror
->mdevice
= container_of(cdev
, struct dmirror_device
, cdevice
);
172 mutex_init(&dmirror
->mutex
);
173 xa_init(&dmirror
->pt
);
175 ret
= mmu_interval_notifier_insert(&dmirror
->notifier
, current
->mm
,
176 0, ULONG_MAX
& PAGE_MASK
, &dmirror_min_ops
);
182 filp
->private_data
= dmirror
;
186 static int dmirror_fops_release(struct inode
*inode
, struct file
*filp
)
188 struct dmirror
*dmirror
= filp
->private_data
;
190 mmu_interval_notifier_remove(&dmirror
->notifier
);
191 xa_destroy(&dmirror
->pt
);
196 static struct dmirror_chunk
*dmirror_page_to_chunk(struct page
*page
)
198 return container_of(page
->pgmap
, struct dmirror_chunk
, pagemap
);
201 static struct dmirror_device
*dmirror_page_to_device(struct page
*page
)
204 return dmirror_page_to_chunk(page
)->mdevice
;
207 static int dmirror_do_fault(struct dmirror
*dmirror
, struct hmm_range
*range
)
209 unsigned long *pfns
= range
->hmm_pfns
;
212 for (pfn
= (range
->start
>> PAGE_SHIFT
);
213 pfn
< (range
->end
>> PAGE_SHIFT
);
219 * Since we asked for hmm_range_fault() to populate pages,
220 * it shouldn't return an error entry on success.
222 WARN_ON(*pfns
& HMM_PFN_ERROR
);
223 WARN_ON(!(*pfns
& HMM_PFN_VALID
));
225 page
= hmm_pfn_to_page(*pfns
);
229 if (*pfns
& HMM_PFN_WRITE
)
230 entry
= xa_tag_pointer(entry
, DPT_XA_TAG_WRITE
);
231 else if (WARN_ON(range
->default_flags
& HMM_PFN_WRITE
))
233 entry
= xa_store(&dmirror
->pt
, pfn
, entry
, GFP_ATOMIC
);
234 if (xa_is_err(entry
))
235 return xa_err(entry
);
241 static void dmirror_do_update(struct dmirror
*dmirror
, unsigned long start
,
248 * The XArray doesn't hold references to pages since it relies on
249 * the mmu notifier to clear page pointers when they become stale.
250 * Therefore, it is OK to just clear the entry.
252 xa_for_each_range(&dmirror
->pt
, pfn
, entry
, start
>> PAGE_SHIFT
,
254 xa_erase(&dmirror
->pt
, pfn
);
257 static bool dmirror_interval_invalidate(struct mmu_interval_notifier
*mni
,
258 const struct mmu_notifier_range
*range
,
259 unsigned long cur_seq
)
261 struct dmirror
*dmirror
= container_of(mni
, struct dmirror
, notifier
);
264 * Ignore invalidation callbacks for device private pages since
265 * the invalidation is handled as part of the migration process.
267 if (range
->event
== MMU_NOTIFY_MIGRATE
&&
268 range
->owner
== dmirror
->mdevice
)
271 if (mmu_notifier_range_blockable(range
))
272 mutex_lock(&dmirror
->mutex
);
273 else if (!mutex_trylock(&dmirror
->mutex
))
276 mmu_interval_set_seq(mni
, cur_seq
);
277 dmirror_do_update(dmirror
, range
->start
, range
->end
);
279 mutex_unlock(&dmirror
->mutex
);
283 static const struct mmu_interval_notifier_ops dmirror_min_ops
= {
284 .invalidate
= dmirror_interval_invalidate
,
287 static int dmirror_range_fault(struct dmirror
*dmirror
,
288 struct hmm_range
*range
)
290 struct mm_struct
*mm
= dmirror
->notifier
.mm
;
291 unsigned long timeout
=
292 jiffies
+ msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT
);
296 if (time_after(jiffies
, timeout
)) {
301 range
->notifier_seq
= mmu_interval_read_begin(range
->notifier
);
303 ret
= hmm_range_fault(range
);
304 mmap_read_unlock(mm
);
311 mutex_lock(&dmirror
->mutex
);
312 if (mmu_interval_read_retry(range
->notifier
,
313 range
->notifier_seq
)) {
314 mutex_unlock(&dmirror
->mutex
);
320 ret
= dmirror_do_fault(dmirror
, range
);
322 mutex_unlock(&dmirror
->mutex
);
327 static int dmirror_fault(struct dmirror
*dmirror
, unsigned long start
,
328 unsigned long end
, bool write
)
330 struct mm_struct
*mm
= dmirror
->notifier
.mm
;
332 unsigned long pfns
[64];
333 struct hmm_range range
= {
334 .notifier
= &dmirror
->notifier
,
338 HMM_PFN_REQ_FAULT
| (write
? HMM_PFN_REQ_WRITE
: 0),
339 .dev_private_owner
= dmirror
->mdevice
,
343 /* Since the mm is for the mirrored process, get a reference first. */
344 if (!mmget_not_zero(mm
))
347 for (addr
= start
; addr
< end
; addr
= range
.end
) {
349 range
.end
= min(addr
+ (ARRAY_SIZE(pfns
) << PAGE_SHIFT
), end
);
351 ret
= dmirror_range_fault(dmirror
, &range
);
360 static int dmirror_do_read(struct dmirror
*dmirror
, unsigned long start
,
361 unsigned long end
, struct dmirror_bounce
*bounce
)
366 ptr
= bounce
->ptr
+ ((start
- bounce
->addr
) & PAGE_MASK
);
368 for (pfn
= start
>> PAGE_SHIFT
; pfn
< (end
>> PAGE_SHIFT
); pfn
++) {
372 entry
= xa_load(&dmirror
->pt
, pfn
);
373 page
= xa_untag_pointer(entry
);
377 memcpy_from_page(ptr
, page
, 0, PAGE_SIZE
);
386 static int dmirror_read(struct dmirror
*dmirror
, struct hmm_dmirror_cmd
*cmd
)
388 struct dmirror_bounce bounce
;
389 unsigned long start
, end
;
390 unsigned long size
= cmd
->npages
<< PAGE_SHIFT
;
398 ret
= dmirror_bounce_init(&bounce
, start
, size
);
403 mutex_lock(&dmirror
->mutex
);
404 ret
= dmirror_do_read(dmirror
, start
, end
, &bounce
);
405 mutex_unlock(&dmirror
->mutex
);
409 start
= cmd
->addr
+ (bounce
.cpages
<< PAGE_SHIFT
);
410 ret
= dmirror_fault(dmirror
, start
, end
, false);
417 if (copy_to_user(u64_to_user_ptr(cmd
->ptr
), bounce
.ptr
,
421 cmd
->cpages
= bounce
.cpages
;
422 dmirror_bounce_fini(&bounce
);
426 static int dmirror_do_write(struct dmirror
*dmirror
, unsigned long start
,
427 unsigned long end
, struct dmirror_bounce
*bounce
)
432 ptr
= bounce
->ptr
+ ((start
- bounce
->addr
) & PAGE_MASK
);
434 for (pfn
= start
>> PAGE_SHIFT
; pfn
< (end
>> PAGE_SHIFT
); pfn
++) {
438 entry
= xa_load(&dmirror
->pt
, pfn
);
439 page
= xa_untag_pointer(entry
);
440 if (!page
|| xa_pointer_tag(entry
) != DPT_XA_TAG_WRITE
)
443 memcpy_to_page(page
, 0, ptr
, PAGE_SIZE
);
452 static int dmirror_write(struct dmirror
*dmirror
, struct hmm_dmirror_cmd
*cmd
)
454 struct dmirror_bounce bounce
;
455 unsigned long start
, end
;
456 unsigned long size
= cmd
->npages
<< PAGE_SHIFT
;
464 ret
= dmirror_bounce_init(&bounce
, start
, size
);
467 if (copy_from_user(bounce
.ptr
, u64_to_user_ptr(cmd
->ptr
),
474 mutex_lock(&dmirror
->mutex
);
475 ret
= dmirror_do_write(dmirror
, start
, end
, &bounce
);
476 mutex_unlock(&dmirror
->mutex
);
480 start
= cmd
->addr
+ (bounce
.cpages
<< PAGE_SHIFT
);
481 ret
= dmirror_fault(dmirror
, start
, end
, true);
488 cmd
->cpages
= bounce
.cpages
;
489 dmirror_bounce_fini(&bounce
);
493 static int dmirror_allocate_chunk(struct dmirror_device
*mdevice
,
496 struct dmirror_chunk
*devmem
;
497 struct resource
*res
= NULL
;
499 unsigned long pfn_first
;
500 unsigned long pfn_last
;
504 devmem
= kzalloc(sizeof(*devmem
), GFP_KERNEL
);
508 switch (mdevice
->zone_device_type
) {
509 case HMM_DMIRROR_MEMORY_DEVICE_PRIVATE
:
510 res
= request_free_mem_region(&iomem_resource
, DEVMEM_CHUNK_SIZE
,
512 if (IS_ERR_OR_NULL(res
))
514 devmem
->pagemap
.range
.start
= res
->start
;
515 devmem
->pagemap
.range
.end
= res
->end
;
516 devmem
->pagemap
.type
= MEMORY_DEVICE_PRIVATE
;
518 case HMM_DMIRROR_MEMORY_DEVICE_COHERENT
:
519 devmem
->pagemap
.range
.start
= (MINOR(mdevice
->cdevice
.dev
) - 2) ?
522 devmem
->pagemap
.range
.end
= devmem
->pagemap
.range
.start
+
523 DEVMEM_CHUNK_SIZE
- 1;
524 devmem
->pagemap
.type
= MEMORY_DEVICE_COHERENT
;
531 devmem
->pagemap
.nr_range
= 1;
532 devmem
->pagemap
.ops
= &dmirror_devmem_ops
;
533 devmem
->pagemap
.owner
= mdevice
;
535 mutex_lock(&mdevice
->devmem_lock
);
537 if (mdevice
->devmem_count
== mdevice
->devmem_capacity
) {
538 struct dmirror_chunk
**new_chunks
;
539 unsigned int new_capacity
;
541 new_capacity
= mdevice
->devmem_capacity
+
542 DEVMEM_CHUNKS_RESERVE
;
543 new_chunks
= krealloc(mdevice
->devmem_chunks
,
544 sizeof(new_chunks
[0]) * new_capacity
,
548 mdevice
->devmem_capacity
= new_capacity
;
549 mdevice
->devmem_chunks
= new_chunks
;
551 ptr
= memremap_pages(&devmem
->pagemap
, numa_node_id());
552 if (IS_ERR_OR_NULL(ptr
)) {
560 devmem
->mdevice
= mdevice
;
561 pfn_first
= devmem
->pagemap
.range
.start
>> PAGE_SHIFT
;
562 pfn_last
= pfn_first
+ (range_len(&devmem
->pagemap
.range
) >> PAGE_SHIFT
);
563 mdevice
->devmem_chunks
[mdevice
->devmem_count
++] = devmem
;
565 mutex_unlock(&mdevice
->devmem_lock
);
567 pr_info("added new %u MB chunk (total %u chunks, %u MB) PFNs [0x%lx 0x%lx)\n",
568 DEVMEM_CHUNK_SIZE
/ (1024 * 1024),
569 mdevice
->devmem_count
,
570 mdevice
->devmem_count
* (DEVMEM_CHUNK_SIZE
/ (1024 * 1024)),
571 pfn_first
, pfn_last
);
573 spin_lock(&mdevice
->lock
);
574 for (pfn
= pfn_first
; pfn
< pfn_last
; pfn
++) {
575 struct page
*page
= pfn_to_page(pfn
);
577 page
->zone_device_data
= mdevice
->free_pages
;
578 mdevice
->free_pages
= page
;
581 *ppage
= mdevice
->free_pages
;
582 mdevice
->free_pages
= (*ppage
)->zone_device_data
;
585 spin_unlock(&mdevice
->lock
);
590 mutex_unlock(&mdevice
->devmem_lock
);
591 if (res
&& devmem
->pagemap
.type
== MEMORY_DEVICE_PRIVATE
)
592 release_mem_region(devmem
->pagemap
.range
.start
,
593 range_len(&devmem
->pagemap
.range
));
600 static struct page
*dmirror_devmem_alloc_page(struct dmirror_device
*mdevice
)
602 struct page
*dpage
= NULL
;
603 struct page
*rpage
= NULL
;
606 * For ZONE_DEVICE private type, this is a fake device so we allocate
607 * real system memory to store our device memory.
608 * For ZONE_DEVICE coherent type we use the actual dpage to store the
609 * data and ignore rpage.
611 if (dmirror_is_private_zone(mdevice
)) {
612 rpage
= alloc_page(GFP_HIGHUSER
);
616 spin_lock(&mdevice
->lock
);
618 if (mdevice
->free_pages
) {
619 dpage
= mdevice
->free_pages
;
620 mdevice
->free_pages
= dpage
->zone_device_data
;
622 spin_unlock(&mdevice
->lock
);
624 spin_unlock(&mdevice
->lock
);
625 if (dmirror_allocate_chunk(mdevice
, &dpage
))
629 zone_device_page_init(dpage
);
630 dpage
->zone_device_data
= rpage
;
639 static void dmirror_migrate_alloc_and_copy(struct migrate_vma
*args
,
640 struct dmirror
*dmirror
)
642 struct dmirror_device
*mdevice
= dmirror
->mdevice
;
643 const unsigned long *src
= args
->src
;
644 unsigned long *dst
= args
->dst
;
647 for (addr
= args
->start
; addr
< args
->end
; addr
+= PAGE_SIZE
,
653 if (!(*src
& MIGRATE_PFN_MIGRATE
))
657 * Note that spage might be NULL which is OK since it is an
658 * unallocated pte_none() or read-only zero page.
660 spage
= migrate_pfn_to_page(*src
);
661 if (WARN(spage
&& is_zone_device_page(spage
),
662 "page already in device spage pfn: 0x%lx\n",
666 dpage
= dmirror_devmem_alloc_page(mdevice
);
670 rpage
= BACKING_PAGE(dpage
);
672 copy_highpage(rpage
, spage
);
674 clear_highpage(rpage
);
677 * Normally, a device would use the page->zone_device_data to
678 * point to the mirror but here we use it to hold the page for
679 * the simulated device memory and that page holds the pointer
682 rpage
->zone_device_data
= dmirror
;
684 pr_debug("migrating from sys to dev pfn src: 0x%lx pfn dst: 0x%lx\n",
685 page_to_pfn(spage
), page_to_pfn(dpage
));
686 *dst
= migrate_pfn(page_to_pfn(dpage
));
687 if ((*src
& MIGRATE_PFN_WRITE
) ||
688 (!spage
&& args
->vma
->vm_flags
& VM_WRITE
))
689 *dst
|= MIGRATE_PFN_WRITE
;
693 static int dmirror_check_atomic(struct dmirror
*dmirror
, unsigned long start
,
698 for (pfn
= start
>> PAGE_SHIFT
; pfn
< (end
>> PAGE_SHIFT
); pfn
++) {
701 entry
= xa_load(&dmirror
->pt
, pfn
);
702 if (xa_pointer_tag(entry
) == DPT_XA_TAG_ATOMIC
)
709 static int dmirror_atomic_map(unsigned long start
, unsigned long end
,
710 struct page
**pages
, struct dmirror
*dmirror
)
712 unsigned long pfn
, mapped
= 0;
715 /* Map the migrated pages into the device's page tables. */
716 mutex_lock(&dmirror
->mutex
);
718 for (i
= 0, pfn
= start
>> PAGE_SHIFT
; pfn
< (end
>> PAGE_SHIFT
); pfn
++, i
++) {
725 entry
= xa_tag_pointer(entry
, DPT_XA_TAG_ATOMIC
);
726 entry
= xa_store(&dmirror
->pt
, pfn
, entry
, GFP_ATOMIC
);
727 if (xa_is_err(entry
)) {
728 mutex_unlock(&dmirror
->mutex
);
729 return xa_err(entry
);
735 mutex_unlock(&dmirror
->mutex
);
739 static int dmirror_migrate_finalize_and_map(struct migrate_vma
*args
,
740 struct dmirror
*dmirror
)
742 unsigned long start
= args
->start
;
743 unsigned long end
= args
->end
;
744 const unsigned long *src
= args
->src
;
745 const unsigned long *dst
= args
->dst
;
748 /* Map the migrated pages into the device's page tables. */
749 mutex_lock(&dmirror
->mutex
);
751 for (pfn
= start
>> PAGE_SHIFT
; pfn
< (end
>> PAGE_SHIFT
); pfn
++,
756 if (!(*src
& MIGRATE_PFN_MIGRATE
))
759 dpage
= migrate_pfn_to_page(*dst
);
763 entry
= BACKING_PAGE(dpage
);
764 if (*dst
& MIGRATE_PFN_WRITE
)
765 entry
= xa_tag_pointer(entry
, DPT_XA_TAG_WRITE
);
766 entry
= xa_store(&dmirror
->pt
, pfn
, entry
, GFP_ATOMIC
);
767 if (xa_is_err(entry
)) {
768 mutex_unlock(&dmirror
->mutex
);
769 return xa_err(entry
);
773 mutex_unlock(&dmirror
->mutex
);
777 static int dmirror_exclusive(struct dmirror
*dmirror
,
778 struct hmm_dmirror_cmd
*cmd
)
780 unsigned long start
, end
, addr
;
781 unsigned long size
= cmd
->npages
<< PAGE_SHIFT
;
782 struct mm_struct
*mm
= dmirror
->notifier
.mm
;
783 struct page
*pages
[64];
784 struct dmirror_bounce bounce
;
793 /* Since the mm is for the mirrored process, get a reference first. */
794 if (!mmget_not_zero(mm
))
798 for (addr
= start
; addr
< end
; addr
= next
) {
799 unsigned long mapped
= 0;
802 next
= min(end
, addr
+ (ARRAY_SIZE(pages
) << PAGE_SHIFT
));
804 ret
= make_device_exclusive_range(mm
, addr
, next
, pages
, NULL
);
806 * Do dmirror_atomic_map() iff all pages are marked for
807 * exclusive access to avoid accessing uninitialized
810 if (ret
== (next
- addr
) >> PAGE_SHIFT
)
811 mapped
= dmirror_atomic_map(addr
, next
, pages
, dmirror
);
812 for (i
= 0; i
< ret
; i
++) {
814 unlock_page(pages
[i
]);
819 if (addr
+ (mapped
<< PAGE_SHIFT
) < next
) {
820 mmap_read_unlock(mm
);
825 mmap_read_unlock(mm
);
828 /* Return the migrated data for verification. */
829 ret
= dmirror_bounce_init(&bounce
, start
, size
);
832 mutex_lock(&dmirror
->mutex
);
833 ret
= dmirror_do_read(dmirror
, start
, end
, &bounce
);
834 mutex_unlock(&dmirror
->mutex
);
836 if (copy_to_user(u64_to_user_ptr(cmd
->ptr
), bounce
.ptr
,
841 cmd
->cpages
= bounce
.cpages
;
842 dmirror_bounce_fini(&bounce
);
846 static vm_fault_t
dmirror_devmem_fault_alloc_and_copy(struct migrate_vma
*args
,
847 struct dmirror
*dmirror
)
849 const unsigned long *src
= args
->src
;
850 unsigned long *dst
= args
->dst
;
851 unsigned long start
= args
->start
;
852 unsigned long end
= args
->end
;
855 for (addr
= start
; addr
< end
; addr
+= PAGE_SIZE
,
857 struct page
*dpage
, *spage
;
859 spage
= migrate_pfn_to_page(*src
);
860 if (!spage
|| !(*src
& MIGRATE_PFN_MIGRATE
))
863 if (WARN_ON(!is_device_private_page(spage
) &&
864 !is_device_coherent_page(spage
)))
866 spage
= BACKING_PAGE(spage
);
867 dpage
= alloc_page_vma(GFP_HIGHUSER_MOVABLE
, args
->vma
, addr
);
870 pr_debug("migrating from dev to sys pfn src: 0x%lx pfn dst: 0x%lx\n",
871 page_to_pfn(spage
), page_to_pfn(dpage
));
874 xa_erase(&dmirror
->pt
, addr
>> PAGE_SHIFT
);
875 copy_highpage(dpage
, spage
);
876 *dst
= migrate_pfn(page_to_pfn(dpage
));
877 if (*src
& MIGRATE_PFN_WRITE
)
878 *dst
|= MIGRATE_PFN_WRITE
;
884 dmirror_successful_migrated_pages(struct migrate_vma
*migrate
)
886 unsigned long cpages
= 0;
889 for (i
= 0; i
< migrate
->npages
; i
++) {
890 if (migrate
->src
[i
] & MIGRATE_PFN_VALID
&&
891 migrate
->src
[i
] & MIGRATE_PFN_MIGRATE
)
897 static int dmirror_migrate_to_system(struct dmirror
*dmirror
,
898 struct hmm_dmirror_cmd
*cmd
)
900 unsigned long start
, end
, addr
;
901 unsigned long size
= cmd
->npages
<< PAGE_SHIFT
;
902 struct mm_struct
*mm
= dmirror
->notifier
.mm
;
903 struct vm_area_struct
*vma
;
904 unsigned long src_pfns
[64] = { 0 };
905 unsigned long dst_pfns
[64] = { 0 };
906 struct migrate_vma args
= { 0 };
915 /* Since the mm is for the mirrored process, get a reference first. */
916 if (!mmget_not_zero(mm
))
921 for (addr
= start
; addr
< end
; addr
= next
) {
922 vma
= vma_lookup(mm
, addr
);
923 if (!vma
|| !(vma
->vm_flags
& VM_READ
)) {
927 next
= min(end
, addr
+ (ARRAY_SIZE(src_pfns
) << PAGE_SHIFT
));
928 if (next
> vma
->vm_end
)
936 args
.pgmap_owner
= dmirror
->mdevice
;
937 args
.flags
= dmirror_select_device(dmirror
);
939 ret
= migrate_vma_setup(&args
);
943 pr_debug("Migrating from device mem to sys mem\n");
944 dmirror_devmem_fault_alloc_and_copy(&args
, dmirror
);
946 migrate_vma_pages(&args
);
947 cmd
->cpages
+= dmirror_successful_migrated_pages(&args
);
948 migrate_vma_finalize(&args
);
951 mmap_read_unlock(mm
);
957 static int dmirror_migrate_to_device(struct dmirror
*dmirror
,
958 struct hmm_dmirror_cmd
*cmd
)
960 unsigned long start
, end
, addr
;
961 unsigned long size
= cmd
->npages
<< PAGE_SHIFT
;
962 struct mm_struct
*mm
= dmirror
->notifier
.mm
;
963 struct vm_area_struct
*vma
;
964 unsigned long src_pfns
[64] = { 0 };
965 unsigned long dst_pfns
[64] = { 0 };
966 struct dmirror_bounce bounce
;
967 struct migrate_vma args
= { 0 };
976 /* Since the mm is for the mirrored process, get a reference first. */
977 if (!mmget_not_zero(mm
))
981 for (addr
= start
; addr
< end
; addr
= next
) {
982 vma
= vma_lookup(mm
, addr
);
983 if (!vma
|| !(vma
->vm_flags
& VM_READ
)) {
987 next
= min(end
, addr
+ (ARRAY_SIZE(src_pfns
) << PAGE_SHIFT
));
988 if (next
> vma
->vm_end
)
996 args
.pgmap_owner
= dmirror
->mdevice
;
997 args
.flags
= MIGRATE_VMA_SELECT_SYSTEM
;
998 ret
= migrate_vma_setup(&args
);
1002 pr_debug("Migrating from sys mem to device mem\n");
1003 dmirror_migrate_alloc_and_copy(&args
, dmirror
);
1004 migrate_vma_pages(&args
);
1005 dmirror_migrate_finalize_and_map(&args
, dmirror
);
1006 migrate_vma_finalize(&args
);
1008 mmap_read_unlock(mm
);
1012 * Return the migrated data for verification.
1013 * Only for pages in device zone
1015 ret
= dmirror_bounce_init(&bounce
, start
, size
);
1018 mutex_lock(&dmirror
->mutex
);
1019 ret
= dmirror_do_read(dmirror
, start
, end
, &bounce
);
1020 mutex_unlock(&dmirror
->mutex
);
1022 if (copy_to_user(u64_to_user_ptr(cmd
->ptr
), bounce
.ptr
,
1026 cmd
->cpages
= bounce
.cpages
;
1027 dmirror_bounce_fini(&bounce
);
1031 mmap_read_unlock(mm
);
1036 static void dmirror_mkentry(struct dmirror
*dmirror
, struct hmm_range
*range
,
1037 unsigned char *perm
, unsigned long entry
)
1041 if (entry
& HMM_PFN_ERROR
) {
1042 *perm
= HMM_DMIRROR_PROT_ERROR
;
1045 if (!(entry
& HMM_PFN_VALID
)) {
1046 *perm
= HMM_DMIRROR_PROT_NONE
;
1050 page
= hmm_pfn_to_page(entry
);
1051 if (is_device_private_page(page
)) {
1052 /* Is the page migrated to this device or some other? */
1053 if (dmirror
->mdevice
== dmirror_page_to_device(page
))
1054 *perm
= HMM_DMIRROR_PROT_DEV_PRIVATE_LOCAL
;
1056 *perm
= HMM_DMIRROR_PROT_DEV_PRIVATE_REMOTE
;
1057 } else if (is_device_coherent_page(page
)) {
1058 /* Is the page migrated to this device or some other? */
1059 if (dmirror
->mdevice
== dmirror_page_to_device(page
))
1060 *perm
= HMM_DMIRROR_PROT_DEV_COHERENT_LOCAL
;
1062 *perm
= HMM_DMIRROR_PROT_DEV_COHERENT_REMOTE
;
1063 } else if (is_zero_pfn(page_to_pfn(page
)))
1064 *perm
= HMM_DMIRROR_PROT_ZERO
;
1066 *perm
= HMM_DMIRROR_PROT_NONE
;
1067 if (entry
& HMM_PFN_WRITE
)
1068 *perm
|= HMM_DMIRROR_PROT_WRITE
;
1070 *perm
|= HMM_DMIRROR_PROT_READ
;
1071 if (hmm_pfn_to_map_order(entry
) + PAGE_SHIFT
== PMD_SHIFT
)
1072 *perm
|= HMM_DMIRROR_PROT_PMD
;
1073 else if (hmm_pfn_to_map_order(entry
) + PAGE_SHIFT
== PUD_SHIFT
)
1074 *perm
|= HMM_DMIRROR_PROT_PUD
;
1077 static bool dmirror_snapshot_invalidate(struct mmu_interval_notifier
*mni
,
1078 const struct mmu_notifier_range
*range
,
1079 unsigned long cur_seq
)
1081 struct dmirror_interval
*dmi
=
1082 container_of(mni
, struct dmirror_interval
, notifier
);
1083 struct dmirror
*dmirror
= dmi
->dmirror
;
1085 if (mmu_notifier_range_blockable(range
))
1086 mutex_lock(&dmirror
->mutex
);
1087 else if (!mutex_trylock(&dmirror
->mutex
))
1091 * Snapshots only need to set the sequence number since any
1092 * invalidation in the interval invalidates the whole snapshot.
1094 mmu_interval_set_seq(mni
, cur_seq
);
1096 mutex_unlock(&dmirror
->mutex
);
1100 static const struct mmu_interval_notifier_ops dmirror_mrn_ops
= {
1101 .invalidate
= dmirror_snapshot_invalidate
,
1104 static int dmirror_range_snapshot(struct dmirror
*dmirror
,
1105 struct hmm_range
*range
,
1106 unsigned char *perm
)
1108 struct mm_struct
*mm
= dmirror
->notifier
.mm
;
1109 struct dmirror_interval notifier
;
1110 unsigned long timeout
=
1111 jiffies
+ msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT
);
1116 notifier
.dmirror
= dmirror
;
1117 range
->notifier
= ¬ifier
.notifier
;
1119 ret
= mmu_interval_notifier_insert(range
->notifier
, mm
,
1120 range
->start
, range
->end
- range
->start
,
1126 if (time_after(jiffies
, timeout
)) {
1131 range
->notifier_seq
= mmu_interval_read_begin(range
->notifier
);
1134 ret
= hmm_range_fault(range
);
1135 mmap_read_unlock(mm
);
1142 mutex_lock(&dmirror
->mutex
);
1143 if (mmu_interval_read_retry(range
->notifier
,
1144 range
->notifier_seq
)) {
1145 mutex_unlock(&dmirror
->mutex
);
1151 n
= (range
->end
- range
->start
) >> PAGE_SHIFT
;
1152 for (i
= 0; i
< n
; i
++)
1153 dmirror_mkentry(dmirror
, range
, perm
+ i
, range
->hmm_pfns
[i
]);
1155 mutex_unlock(&dmirror
->mutex
);
1157 mmu_interval_notifier_remove(range
->notifier
);
1161 static int dmirror_snapshot(struct dmirror
*dmirror
,
1162 struct hmm_dmirror_cmd
*cmd
)
1164 struct mm_struct
*mm
= dmirror
->notifier
.mm
;
1165 unsigned long start
, end
;
1166 unsigned long size
= cmd
->npages
<< PAGE_SHIFT
;
1169 unsigned long pfns
[64];
1170 unsigned char perm
[64];
1172 struct hmm_range range
= {
1174 .dev_private_owner
= dmirror
->mdevice
,
1183 /* Since the mm is for the mirrored process, get a reference first. */
1184 if (!mmget_not_zero(mm
))
1188 * Register a temporary notifier to detect invalidations even if it
1189 * overlaps with other mmu_interval_notifiers.
1191 uptr
= u64_to_user_ptr(cmd
->ptr
);
1192 for (addr
= start
; addr
< end
; addr
= next
) {
1195 next
= min(addr
+ (ARRAY_SIZE(pfns
) << PAGE_SHIFT
), end
);
1199 ret
= dmirror_range_snapshot(dmirror
, &range
, perm
);
1203 n
= (range
.end
- range
.start
) >> PAGE_SHIFT
;
1204 if (copy_to_user(uptr
, perm
, n
)) {
1217 static void dmirror_device_evict_chunk(struct dmirror_chunk
*chunk
)
1219 unsigned long start_pfn
= chunk
->pagemap
.range
.start
>> PAGE_SHIFT
;
1220 unsigned long end_pfn
= chunk
->pagemap
.range
.end
>> PAGE_SHIFT
;
1221 unsigned long npages
= end_pfn
- start_pfn
+ 1;
1223 unsigned long *src_pfns
;
1224 unsigned long *dst_pfns
;
1226 src_pfns
= kvcalloc(npages
, sizeof(*src_pfns
), GFP_KERNEL
| __GFP_NOFAIL
);
1227 dst_pfns
= kvcalloc(npages
, sizeof(*dst_pfns
), GFP_KERNEL
| __GFP_NOFAIL
);
1229 migrate_device_range(src_pfns
, start_pfn
, npages
);
1230 for (i
= 0; i
< npages
; i
++) {
1231 struct page
*dpage
, *spage
;
1233 spage
= migrate_pfn_to_page(src_pfns
[i
]);
1234 if (!spage
|| !(src_pfns
[i
] & MIGRATE_PFN_MIGRATE
))
1237 if (WARN_ON(!is_device_private_page(spage
) &&
1238 !is_device_coherent_page(spage
)))
1240 spage
= BACKING_PAGE(spage
);
1241 dpage
= alloc_page(GFP_HIGHUSER_MOVABLE
| __GFP_NOFAIL
);
1243 copy_highpage(dpage
, spage
);
1244 dst_pfns
[i
] = migrate_pfn(page_to_pfn(dpage
));
1245 if (src_pfns
[i
] & MIGRATE_PFN_WRITE
)
1246 dst_pfns
[i
] |= MIGRATE_PFN_WRITE
;
1248 migrate_device_pages(src_pfns
, dst_pfns
, npages
);
1249 migrate_device_finalize(src_pfns
, dst_pfns
, npages
);
1254 /* Removes free pages from the free list so they can't be re-allocated */
1255 static void dmirror_remove_free_pages(struct dmirror_chunk
*devmem
)
1257 struct dmirror_device
*mdevice
= devmem
->mdevice
;
1260 for (page
= mdevice
->free_pages
; page
; page
= page
->zone_device_data
)
1261 if (dmirror_page_to_chunk(page
) == devmem
)
1262 mdevice
->free_pages
= page
->zone_device_data
;
1265 static void dmirror_device_remove_chunks(struct dmirror_device
*mdevice
)
1269 mutex_lock(&mdevice
->devmem_lock
);
1270 if (mdevice
->devmem_chunks
) {
1271 for (i
= 0; i
< mdevice
->devmem_count
; i
++) {
1272 struct dmirror_chunk
*devmem
=
1273 mdevice
->devmem_chunks
[i
];
1275 spin_lock(&mdevice
->lock
);
1276 devmem
->remove
= true;
1277 dmirror_remove_free_pages(devmem
);
1278 spin_unlock(&mdevice
->lock
);
1280 dmirror_device_evict_chunk(devmem
);
1281 memunmap_pages(&devmem
->pagemap
);
1282 if (devmem
->pagemap
.type
== MEMORY_DEVICE_PRIVATE
)
1283 release_mem_region(devmem
->pagemap
.range
.start
,
1284 range_len(&devmem
->pagemap
.range
));
1287 mdevice
->devmem_count
= 0;
1288 mdevice
->devmem_capacity
= 0;
1289 mdevice
->free_pages
= NULL
;
1290 kfree(mdevice
->devmem_chunks
);
1291 mdevice
->devmem_chunks
= NULL
;
1293 mutex_unlock(&mdevice
->devmem_lock
);
1296 static long dmirror_fops_unlocked_ioctl(struct file
*filp
,
1297 unsigned int command
,
1300 void __user
*uarg
= (void __user
*)arg
;
1301 struct hmm_dmirror_cmd cmd
;
1302 struct dmirror
*dmirror
;
1305 dmirror
= filp
->private_data
;
1309 if (copy_from_user(&cmd
, uarg
, sizeof(cmd
)))
1312 if (cmd
.addr
& ~PAGE_MASK
)
1314 if (cmd
.addr
>= (cmd
.addr
+ (cmd
.npages
<< PAGE_SHIFT
)))
1321 case HMM_DMIRROR_READ
:
1322 ret
= dmirror_read(dmirror
, &cmd
);
1325 case HMM_DMIRROR_WRITE
:
1326 ret
= dmirror_write(dmirror
, &cmd
);
1329 case HMM_DMIRROR_MIGRATE_TO_DEV
:
1330 ret
= dmirror_migrate_to_device(dmirror
, &cmd
);
1333 case HMM_DMIRROR_MIGRATE_TO_SYS
:
1334 ret
= dmirror_migrate_to_system(dmirror
, &cmd
);
1337 case HMM_DMIRROR_EXCLUSIVE
:
1338 ret
= dmirror_exclusive(dmirror
, &cmd
);
1341 case HMM_DMIRROR_CHECK_EXCLUSIVE
:
1342 ret
= dmirror_check_atomic(dmirror
, cmd
.addr
,
1343 cmd
.addr
+ (cmd
.npages
<< PAGE_SHIFT
));
1346 case HMM_DMIRROR_SNAPSHOT
:
1347 ret
= dmirror_snapshot(dmirror
, &cmd
);
1350 case HMM_DMIRROR_RELEASE
:
1351 dmirror_device_remove_chunks(dmirror
->mdevice
);
1361 if (copy_to_user(uarg
, &cmd
, sizeof(cmd
)))
1367 static int dmirror_fops_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1371 for (addr
= vma
->vm_start
; addr
< vma
->vm_end
; addr
+= PAGE_SIZE
) {
1375 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
1379 ret
= vm_insert_page(vma
, addr
, page
);
1390 static const struct file_operations dmirror_fops
= {
1391 .open
= dmirror_fops_open
,
1392 .release
= dmirror_fops_release
,
1393 .mmap
= dmirror_fops_mmap
,
1394 .unlocked_ioctl
= dmirror_fops_unlocked_ioctl
,
1395 .llseek
= default_llseek
,
1396 .owner
= THIS_MODULE
,
1399 static void dmirror_devmem_free(struct page
*page
)
1401 struct page
*rpage
= BACKING_PAGE(page
);
1402 struct dmirror_device
*mdevice
;
1407 mdevice
= dmirror_page_to_device(page
);
1408 spin_lock(&mdevice
->lock
);
1410 /* Return page to our allocator if not freeing the chunk */
1411 if (!dmirror_page_to_chunk(page
)->remove
) {
1413 page
->zone_device_data
= mdevice
->free_pages
;
1414 mdevice
->free_pages
= page
;
1416 spin_unlock(&mdevice
->lock
);
1419 static vm_fault_t
dmirror_devmem_fault(struct vm_fault
*vmf
)
1421 struct migrate_vma args
= { 0 };
1422 unsigned long src_pfns
= 0;
1423 unsigned long dst_pfns
= 0;
1425 struct dmirror
*dmirror
;
1429 * Normally, a device would use the page->zone_device_data to point to
1430 * the mirror but here we use it to hold the page for the simulated
1431 * device memory and that page holds the pointer to the mirror.
1433 rpage
= vmf
->page
->zone_device_data
;
1434 dmirror
= rpage
->zone_device_data
;
1436 /* FIXME demonstrate how we can adjust migrate range */
1437 args
.vma
= vmf
->vma
;
1438 args
.start
= vmf
->address
;
1439 args
.end
= args
.start
+ PAGE_SIZE
;
1440 args
.src
= &src_pfns
;
1441 args
.dst
= &dst_pfns
;
1442 args
.pgmap_owner
= dmirror
->mdevice
;
1443 args
.flags
= dmirror_select_device(dmirror
);
1444 args
.fault_page
= vmf
->page
;
1446 if (migrate_vma_setup(&args
))
1447 return VM_FAULT_SIGBUS
;
1449 ret
= dmirror_devmem_fault_alloc_and_copy(&args
, dmirror
);
1452 migrate_vma_pages(&args
);
1454 * No device finalize step is needed since
1455 * dmirror_devmem_fault_alloc_and_copy() will have already
1456 * invalidated the device page table.
1458 migrate_vma_finalize(&args
);
1462 static const struct dev_pagemap_ops dmirror_devmem_ops
= {
1463 .page_free
= dmirror_devmem_free
,
1464 .migrate_to_ram
= dmirror_devmem_fault
,
1467 static int dmirror_device_init(struct dmirror_device
*mdevice
, int id
)
1472 dev
= MKDEV(MAJOR(dmirror_dev
), id
);
1473 mutex_init(&mdevice
->devmem_lock
);
1474 spin_lock_init(&mdevice
->lock
);
1476 cdev_init(&mdevice
->cdevice
, &dmirror_fops
);
1477 mdevice
->cdevice
.owner
= THIS_MODULE
;
1478 device_initialize(&mdevice
->device
);
1479 mdevice
->device
.devt
= dev
;
1481 ret
= dev_set_name(&mdevice
->device
, "hmm_dmirror%u", id
);
1485 ret
= cdev_device_add(&mdevice
->cdevice
, &mdevice
->device
);
1489 /* Build a list of free ZONE_DEVICE struct pages */
1490 return dmirror_allocate_chunk(mdevice
, NULL
);
1493 static void dmirror_device_remove(struct dmirror_device
*mdevice
)
1495 dmirror_device_remove_chunks(mdevice
);
1496 cdev_device_del(&mdevice
->cdevice
, &mdevice
->device
);
1499 static int __init
hmm_dmirror_init(void)
1505 ret
= alloc_chrdev_region(&dmirror_dev
, 0, DMIRROR_NDEVICES
,
1510 memset(dmirror_devices
, 0, DMIRROR_NDEVICES
* sizeof(dmirror_devices
[0]));
1511 dmirror_devices
[ndevices
++].zone_device_type
=
1512 HMM_DMIRROR_MEMORY_DEVICE_PRIVATE
;
1513 dmirror_devices
[ndevices
++].zone_device_type
=
1514 HMM_DMIRROR_MEMORY_DEVICE_PRIVATE
;
1515 if (spm_addr_dev0
&& spm_addr_dev1
) {
1516 dmirror_devices
[ndevices
++].zone_device_type
=
1517 HMM_DMIRROR_MEMORY_DEVICE_COHERENT
;
1518 dmirror_devices
[ndevices
++].zone_device_type
=
1519 HMM_DMIRROR_MEMORY_DEVICE_COHERENT
;
1521 for (id
= 0; id
< ndevices
; id
++) {
1522 ret
= dmirror_device_init(dmirror_devices
+ id
, id
);
1527 pr_info("HMM test module loaded. This is only for testing HMM.\n");
1532 dmirror_device_remove(dmirror_devices
+ id
);
1533 unregister_chrdev_region(dmirror_dev
, DMIRROR_NDEVICES
);
1538 static void __exit
hmm_dmirror_exit(void)
1542 for (id
= 0; id
< DMIRROR_NDEVICES
; id
++)
1543 if (dmirror_devices
[id
].zone_device_type
)
1544 dmirror_device_remove(dmirror_devices
+ id
);
1545 unregister_chrdev_region(dmirror_dev
, DMIRROR_NDEVICES
);
1548 module_init(hmm_dmirror_init
);
1549 module_exit(hmm_dmirror_exit
);
1550 MODULE_DESCRIPTION("HMM (Heterogeneous Memory Management) test module");
1551 MODULE_LICENSE("GPL");