1 // SPDX-License-Identifier: GPL-2.0
3 * Device Memory Migration functionality.
5 * Originally written by Jérôme Glisse.
7 #include <linux/export.h>
8 #include <linux/memremap.h>
9 #include <linux/migrate.h>
10 #include <linux/mm_inline.h>
11 #include <linux/mmu_notifier.h>
12 #include <linux/oom.h>
13 #include <linux/pagewalk.h>
14 #include <linux/rmap.h>
15 #include <linux/swapops.h>
16 #include <asm/tlbflush.h>
19 static int migrate_vma_collect_skip(unsigned long start,
23 struct migrate_vma *migrate = walk->private;
26 for (addr = start; addr < end; addr += PAGE_SIZE) {
27 migrate->dst[migrate->npages] = 0;
28 migrate->src[migrate->npages++] = 0;
34 static int migrate_vma_collect_hole(unsigned long start,
36 __always_unused int depth,
39 struct migrate_vma *migrate = walk->private;
42 /* Only allow populating anonymous memory. */
43 if (!vma_is_anonymous(walk->vma))
44 return migrate_vma_collect_skip(start, end, walk);
46 for (addr = start; addr < end; addr += PAGE_SIZE) {
47 migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
48 migrate->dst[migrate->npages] = 0;
56 static int migrate_vma_collect_pmd(pmd_t *pmdp,
61 struct migrate_vma *migrate = walk->private;
62 struct vm_area_struct *vma = walk->vma;
63 struct mm_struct *mm = vma->vm_mm;
64 unsigned long addr = start, unmapped = 0;
70 return migrate_vma_collect_hole(start, end, -1, walk);
72 if (pmd_trans_huge(*pmdp)) {
75 ptl = pmd_lock(mm, pmdp);
76 if (unlikely(!pmd_trans_huge(*pmdp))) {
81 page = pmd_page(*pmdp);
82 if (is_huge_zero_page(page)) {
84 split_huge_pmd(vma, pmdp, addr);
85 if (pmd_trans_unstable(pmdp))
86 return migrate_vma_collect_skip(start, end,
93 if (unlikely(!trylock_page(page)))
94 return migrate_vma_collect_skip(start, end,
96 ret = split_huge_page(page);
100 return migrate_vma_collect_skip(start, end,
103 return migrate_vma_collect_hole(start, end, -1,
108 if (unlikely(pmd_bad(*pmdp)))
109 return migrate_vma_collect_skip(start, end, walk);
111 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
112 arch_enter_lazy_mmu_mode();
114 for (; addr < end; addr += PAGE_SIZE, ptep++) {
115 unsigned long mpfn = 0, pfn;
123 if (vma_is_anonymous(vma)) {
124 mpfn = MIGRATE_PFN_MIGRATE;
130 if (!pte_present(pte)) {
132 * Only care about unaddressable device page special
133 * page table entry. Other special swap entries are not
134 * migratable, and we ignore regular swapped page.
136 entry = pte_to_swp_entry(pte);
137 if (!is_device_private_entry(entry))
140 page = pfn_swap_entry_to_page(entry);
141 if (!(migrate->flags &
142 MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
143 page->pgmap->owner != migrate->pgmap_owner)
146 mpfn = migrate_pfn(page_to_pfn(page)) |
148 if (is_writable_device_private_entry(entry))
149 mpfn |= MIGRATE_PFN_WRITE;
151 if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
154 if (is_zero_pfn(pfn)) {
155 mpfn = MIGRATE_PFN_MIGRATE;
159 page = vm_normal_page(migrate->vma, addr, pte);
160 mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
161 mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
164 /* FIXME support THP */
165 if (!page || !page->mapping || PageTransCompound(page)) {
171 * By getting a reference on the page we pin it and that blocks
172 * any kind of migration. Side effect is that it "freezes" the
175 * We drop this reference after isolating the page from the lru
176 * for non device page (device page are not on the lru and thus
177 * can't be dropped from it).
182 * Optimize for the common case where page is only mapped once
183 * in one process. If we can lock the page, then we can safely
184 * set up a special migration page table entry now.
186 if (trylock_page(page)) {
190 ptep_get_and_clear(mm, addr, ptep);
192 /* Setup special migration page table entry */
193 if (mpfn & MIGRATE_PFN_WRITE)
194 entry = make_writable_migration_entry(
197 entry = make_readable_migration_entry(
199 swp_pte = swp_entry_to_pte(entry);
200 if (pte_present(pte)) {
201 if (pte_soft_dirty(pte))
202 swp_pte = pte_swp_mksoft_dirty(swp_pte);
203 if (pte_uffd_wp(pte))
204 swp_pte = pte_swp_mkuffd_wp(swp_pte);
206 if (pte_swp_soft_dirty(pte))
207 swp_pte = pte_swp_mksoft_dirty(swp_pte);
208 if (pte_swp_uffd_wp(pte))
209 swp_pte = pte_swp_mkuffd_wp(swp_pte);
211 set_pte_at(mm, addr, ptep, swp_pte);
214 * This is like regular unmap: we remove the rmap and
215 * drop page refcount. Page won't be freed, as we took
216 * a reference just above.
218 page_remove_rmap(page, vma, false);
221 if (pte_present(pte))
229 migrate->dst[migrate->npages] = 0;
230 migrate->src[migrate->npages++] = mpfn;
232 arch_leave_lazy_mmu_mode();
233 pte_unmap_unlock(ptep - 1, ptl);
235 /* Only flush the TLB if we actually modified any entries */
237 flush_tlb_range(walk->vma, start, end);
242 static const struct mm_walk_ops migrate_vma_walk_ops = {
243 .pmd_entry = migrate_vma_collect_pmd,
244 .pte_hole = migrate_vma_collect_hole,
248 * migrate_vma_collect() - collect pages over a range of virtual addresses
249 * @migrate: migrate struct containing all migration information
251 * This will walk the CPU page table. For each virtual address backed by a
252 * valid page, it updates the src array and takes a reference on the page, in
253 * order to pin the page until we lock it and unmap it.
255 static void migrate_vma_collect(struct migrate_vma *migrate)
257 struct mmu_notifier_range range;
260 * Note that the pgmap_owner is passed to the mmu notifier callback so
261 * that the registered device driver can skip invalidating device
262 * private page mappings that won't be migrated.
264 mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
265 migrate->vma, migrate->vma->vm_mm, migrate->start, migrate->end,
266 migrate->pgmap_owner);
267 mmu_notifier_invalidate_range_start(&range);
269 walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
270 &migrate_vma_walk_ops, migrate);
272 mmu_notifier_invalidate_range_end(&range);
273 migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
277 * migrate_vma_check_page() - check if page is pinned or not
278 * @page: struct page to check
280 * Pinned pages cannot be migrated. This is the same test as in
281 * folio_migrate_mapping(), except that here we allow migration of a
284 static bool migrate_vma_check_page(struct page *page)
287 * One extra ref because caller holds an extra reference, either from
288 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
294 * FIXME support THP (transparent huge page), it is bit more complex to
295 * check them than regular pages, because they can be mapped with a pmd
296 * or with a pte (split pte mapping).
298 if (PageCompound(page))
301 /* Page from ZONE_DEVICE have one extra reference */
302 if (is_zone_device_page(page))
305 /* For file back page */
306 if (page_mapping(page))
307 extra += 1 + page_has_private(page);
309 if ((page_count(page) - extra) > page_mapcount(page))
316 * migrate_vma_unmap() - replace page mapping with special migration pte entry
317 * @migrate: migrate struct containing all migration information
319 * Isolate pages from the LRU and replace mappings (CPU page table pte) with a
320 * special migration pte entry and check if it has been pinned. Pinned pages are
321 * restored because we cannot migrate them.
323 * This is the last step before we call the device driver callback to allocate
324 * destination memory and copy contents of original page over to new page.
326 static void migrate_vma_unmap(struct migrate_vma *migrate)
328 const unsigned long npages = migrate->npages;
329 unsigned long i, restore = 0;
330 bool allow_drain = true;
334 for (i = 0; i < npages; i++) {
335 struct page *page = migrate_pfn_to_page(migrate->src[i]);
341 /* ZONE_DEVICE pages are not on LRU */
342 if (!is_zone_device_page(page)) {
343 if (!PageLRU(page) && allow_drain) {
344 /* Drain CPU's pagevec */
349 if (isolate_lru_page(page)) {
350 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
356 /* Drop the reference we took in collect */
360 folio = page_folio(page);
361 if (folio_mapped(folio))
362 try_to_migrate(folio, 0);
364 if (page_mapped(page) || !migrate_vma_check_page(page)) {
365 if (!is_zone_device_page(page)) {
367 putback_lru_page(page);
370 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
377 for (i = 0; i < npages && restore; i++) {
378 struct page *page = migrate_pfn_to_page(migrate->src[i]);
381 if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
384 folio = page_folio(page);
385 remove_migration_ptes(folio, folio, false);
395 * migrate_vma_setup() - prepare to migrate a range of memory
396 * @args: contains the vma, start, and pfns arrays for the migration
398 * Returns: negative errno on failures, 0 when 0 or more pages were migrated
401 * Prepare to migrate a range of memory virtual address range by collecting all
402 * the pages backing each virtual address in the range, saving them inside the
403 * src array. Then lock those pages and unmap them. Once the pages are locked
404 * and unmapped, check whether each page is pinned or not. Pages that aren't
405 * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
406 * corresponding src array entry. Then restores any pages that are pinned, by
407 * remapping and unlocking those pages.
409 * The caller should then allocate destination memory and copy source memory to
410 * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
411 * flag set). Once these are allocated and copied, the caller must update each
412 * corresponding entry in the dst array with the pfn value of the destination
413 * page and with MIGRATE_PFN_VALID. Destination pages must be locked via
416 * Note that the caller does not have to migrate all the pages that are marked
417 * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
418 * device memory to system memory. If the caller cannot migrate a device page
419 * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
420 * consequences for the userspace process, so it must be avoided if at all
423 * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
424 * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
425 * allowing the caller to allocate device memory for those unbacked virtual
426 * addresses. For this the caller simply has to allocate device memory and
427 * properly set the destination entry like for regular migration. Note that
428 * this can still fail, and thus inside the device driver you must check if the
429 * migration was successful for those entries after calling migrate_vma_pages(),
430 * just like for regular migration.
432 * After that, the callers must call migrate_vma_pages() to go over each entry
433 * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
434 * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
435 * then migrate_vma_pages() to migrate struct page information from the source
436 * struct page to the destination struct page. If it fails to migrate the
437 * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
440 * At this point all successfully migrated pages have an entry in the src
441 * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
442 * array entry with MIGRATE_PFN_VALID flag set.
444 * Once migrate_vma_pages() returns the caller may inspect which pages were
445 * successfully migrated, and which were not. Successfully migrated pages will
446 * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
448 * It is safe to update device page table after migrate_vma_pages() because
449 * both destination and source page are still locked, and the mmap_lock is held
450 * in read mode (hence no one can unmap the range being migrated).
452 * Once the caller is done cleaning up things and updating its page table (if it
453 * chose to do so, this is not an obligation) it finally calls
454 * migrate_vma_finalize() to update the CPU page table to point to new pages
455 * for successfully migrated pages or otherwise restore the CPU page table to
456 * point to the original source pages.
458 int migrate_vma_setup(struct migrate_vma *args)
460 long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
462 args->start &= PAGE_MASK;
463 args->end &= PAGE_MASK;
464 if (!args->vma || is_vm_hugetlb_page(args->vma) ||
465 (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
469 if (args->start < args->vma->vm_start ||
470 args->start >= args->vma->vm_end)
472 if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
474 if (!args->src || !args->dst)
477 memset(args->src, 0, sizeof(*args->src) * nr_pages);
481 migrate_vma_collect(args);
484 migrate_vma_unmap(args);
487 * At this point pages are locked and unmapped, and thus they have
488 * stable content and can safely be copied to destination memory that
489 * is allocated by the drivers.
494 EXPORT_SYMBOL(migrate_vma_setup);
497 * This code closely matches the code in:
498 * __handle_mm_fault()
500 * do_anonymous_page()
501 * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
504 static void migrate_vma_insert_page(struct migrate_vma *migrate,
509 struct vm_area_struct *vma = migrate->vma;
510 struct mm_struct *mm = vma->vm_mm;
520 /* Only allow populating anonymous memory */
521 if (!vma_is_anonymous(vma))
524 pgdp = pgd_offset(mm, addr);
525 p4dp = p4d_alloc(mm, pgdp, addr);
528 pudp = pud_alloc(mm, p4dp, addr);
531 pmdp = pmd_alloc(mm, pudp, addr);
535 if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
539 * Use pte_alloc() instead of pte_alloc_map(). We can't run
540 * pte_offset_map() on pmds where a huge pmd might be created
541 * from a different thread.
543 * pte_alloc_map() is safe to use under mmap_write_lock(mm) or when
544 * parallel threads are excluded by other means.
546 * Here we only have mmap_read_lock(mm).
548 if (pte_alloc(mm, pmdp))
551 /* See the comment in pte_alloc_one_map() */
552 if (unlikely(pmd_trans_unstable(pmdp)))
555 if (unlikely(anon_vma_prepare(vma)))
557 if (mem_cgroup_charge(page_folio(page), vma->vm_mm, GFP_KERNEL))
561 * The memory barrier inside __SetPageUptodate makes sure that
562 * preceding stores to the page contents become visible before
563 * the set_pte_at() write.
565 __SetPageUptodate(page);
567 if (is_device_private_page(page)) {
568 swp_entry_t swp_entry;
570 if (vma->vm_flags & VM_WRITE)
571 swp_entry = make_writable_device_private_entry(
574 swp_entry = make_readable_device_private_entry(
576 entry = swp_entry_to_pte(swp_entry);
579 * For now we only support migrating to un-addressable device
582 if (is_zone_device_page(page)) {
583 pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
586 entry = mk_pte(page, vma->vm_page_prot);
587 if (vma->vm_flags & VM_WRITE)
588 entry = pte_mkwrite(pte_mkdirty(entry));
591 ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
593 if (check_stable_address_space(mm))
596 if (pte_present(*ptep)) {
597 unsigned long pfn = pte_pfn(*ptep);
599 if (!is_zero_pfn(pfn))
602 } else if (!pte_none(*ptep))
606 * Check for userfaultfd but do not deliver the fault. Instead,
609 if (userfaultfd_missing(vma))
612 inc_mm_counter(mm, MM_ANONPAGES);
613 page_add_new_anon_rmap(page, vma, addr, false);
614 if (!is_zone_device_page(page))
615 lru_cache_add_inactive_or_unevictable(page, vma);
619 flush_cache_page(vma, addr, pte_pfn(*ptep));
620 ptep_clear_flush_notify(vma, addr, ptep);
621 set_pte_at_notify(mm, addr, ptep, entry);
622 update_mmu_cache(vma, addr, ptep);
624 /* No need to invalidate - it was non-present before */
625 set_pte_at(mm, addr, ptep, entry);
626 update_mmu_cache(vma, addr, ptep);
629 pte_unmap_unlock(ptep, ptl);
630 *src = MIGRATE_PFN_MIGRATE;
634 pte_unmap_unlock(ptep, ptl);
636 *src &= ~MIGRATE_PFN_MIGRATE;
640 * migrate_vma_pages() - migrate meta-data from src page to dst page
641 * @migrate: migrate struct containing all migration information
643 * This migrates struct page meta-data from source struct page to destination
644 * struct page. This effectively finishes the migration from source page to the
647 void migrate_vma_pages(struct migrate_vma *migrate)
649 const unsigned long npages = migrate->npages;
650 const unsigned long start = migrate->start;
651 struct mmu_notifier_range range;
652 unsigned long addr, i;
653 bool notified = false;
655 for (i = 0, addr = start; i < npages; addr += PAGE_SIZE, i++) {
656 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
657 struct page *page = migrate_pfn_to_page(migrate->src[i]);
658 struct address_space *mapping;
662 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
667 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE))
672 mmu_notifier_range_init_owner(&range,
673 MMU_NOTIFY_MIGRATE, 0, migrate->vma,
674 migrate->vma->vm_mm, addr, migrate->end,
675 migrate->pgmap_owner);
676 mmu_notifier_invalidate_range_start(&range);
678 migrate_vma_insert_page(migrate, addr, newpage,
683 mapping = page_mapping(page);
685 if (is_device_private_page(newpage)) {
687 * For now only support private anonymous when migrating
688 * to un-addressable device memory.
691 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
694 } else if (is_zone_device_page(newpage)) {
696 * Other types of ZONE_DEVICE page are not supported.
698 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
702 r = migrate_page(mapping, newpage, page, MIGRATE_SYNC_NO_COPY);
703 if (r != MIGRATEPAGE_SUCCESS)
704 migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
708 * No need to double call mmu_notifier->invalidate_range() callback as
709 * the above ptep_clear_flush_notify() inside migrate_vma_insert_page()
710 * did already call it.
713 mmu_notifier_invalidate_range_only_end(&range);
715 EXPORT_SYMBOL(migrate_vma_pages);
718 * migrate_vma_finalize() - restore CPU page table entry
719 * @migrate: migrate struct containing all migration information
721 * This replaces the special migration pte entry with either a mapping to the
722 * new page if migration was successful for that page, or to the original page
725 * This also unlocks the pages and puts them back on the lru, or drops the extra
726 * refcount, for device pages.
728 void migrate_vma_finalize(struct migrate_vma *migrate)
730 const unsigned long npages = migrate->npages;
733 for (i = 0; i < npages; i++) {
734 struct folio *dst, *src;
735 struct page *newpage = migrate_pfn_to_page(migrate->dst[i]);
736 struct page *page = migrate_pfn_to_page(migrate->src[i]);
740 unlock_page(newpage);
746 if (!(migrate->src[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
748 unlock_page(newpage);
754 src = page_folio(page);
755 dst = page_folio(newpage);
756 remove_migration_ptes(src, dst, false);
759 if (is_zone_device_page(page))
762 putback_lru_page(page);
764 if (newpage != page) {
765 unlock_page(newpage);
766 if (is_zone_device_page(newpage))
769 putback_lru_page(newpage);
773 EXPORT_SYMBOL(migrate_vma_finalize);