xref: /linux/mm/migrate_device.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Device Memory Migration functionality.
4  *
5  * Originally written by Jérôme Glisse.
6  */
7 #include <linux/export.h>
8 #include <linux/memremap.h>
9 #include <linux/migrate.h>
10 #include <linux/mm.h>
11 #include <linux/mm_inline.h>
12 #include <linux/mmu_notifier.h>
13 #include <linux/oom.h>
14 #include <linux/pagewalk.h>
15 #include <linux/rmap.h>
16 #include <linux/swapops.h>
17 #include <asm/tlbflush.h>
18 #include "internal.h"
19 
20 static int migrate_vma_collect_skip(unsigned long start,
21 				    unsigned long end,
22 				    struct mm_walk *walk)
23 {
24 	struct migrate_vma *migrate = walk->private;
25 	unsigned long addr;
26 
27 	for (addr = start; addr < end; addr += PAGE_SIZE) {
28 		migrate->dst[migrate->npages] = 0;
29 		migrate->src[migrate->npages++] = 0;
30 	}
31 
32 	return 0;
33 }
34 
35 static int migrate_vma_collect_hole(unsigned long start,
36 				    unsigned long end,
37 				    __always_unused int depth,
38 				    struct mm_walk *walk)
39 {
40 	struct migrate_vma *migrate = walk->private;
41 	unsigned long addr;
42 
43 	/* Only allow populating anonymous memory. */
44 	if (!vma_is_anonymous(walk->vma))
45 		return migrate_vma_collect_skip(start, end, walk);
46 
47 	for (addr = start; addr < end; addr += PAGE_SIZE) {
48 		migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
49 		migrate->dst[migrate->npages] = 0;
50 		migrate->npages++;
51 		migrate->cpages++;
52 	}
53 
54 	return 0;
55 }
56 
57 static int migrate_vma_collect_pmd(pmd_t *pmdp,
58 				   unsigned long start,
59 				   unsigned long end,
60 				   struct mm_walk *walk)
61 {
62 	struct migrate_vma *migrate = walk->private;
63 	struct vm_area_struct *vma = walk->vma;
64 	struct mm_struct *mm = vma->vm_mm;
65 	unsigned long addr = start, unmapped = 0;
66 	spinlock_t *ptl;
67 	pte_t *ptep;
68 
69 again:
70 	if (pmd_none(*pmdp))
71 		return migrate_vma_collect_hole(start, end, -1, walk);
72 
73 	if (pmd_trans_huge(*pmdp)) {
74 		struct page *page;
75 
76 		ptl = pmd_lock(mm, pmdp);
77 		if (unlikely(!pmd_trans_huge(*pmdp))) {
78 			spin_unlock(ptl);
79 			goto again;
80 		}
81 
82 		page = pmd_page(*pmdp);
83 		if (is_huge_zero_page(page)) {
84 			spin_unlock(ptl);
85 			split_huge_pmd(vma, pmdp, addr);
86 		} else {
87 			int ret;
88 
89 			get_page(page);
90 			spin_unlock(ptl);
91 			if (unlikely(!trylock_page(page)))
92 				return migrate_vma_collect_skip(start, end,
93 								walk);
94 			ret = split_huge_page(page);
95 			unlock_page(page);
96 			put_page(page);
97 			if (ret)
98 				return migrate_vma_collect_skip(start, end,
99 								walk);
100 		}
101 	}
102 
103 	ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
104 	if (!ptep)
105 		goto again;
106 	arch_enter_lazy_mmu_mode();
107 
108 	for (; addr < end; addr += PAGE_SIZE, ptep++) {
109 		unsigned long mpfn = 0, pfn;
110 		struct folio *folio;
111 		struct page *page;
112 		swp_entry_t entry;
113 		pte_t pte;
114 
115 		pte = ptep_get(ptep);
116 
117 		if (pte_none(pte)) {
118 			if (vma_is_anonymous(vma)) {
119 				mpfn = MIGRATE_PFN_MIGRATE;
120 				migrate->cpages++;
121 			}
122 			goto next;
123 		}
124 
125 		if (!pte_present(pte)) {
126 			/*
127 			 * Only care about unaddressable device page special
128 			 * page table entry. Other special swap entries are not
129 			 * migratable, and we ignore regular swapped page.
130 			 */
131 			entry = pte_to_swp_entry(pte);
132 			if (!is_device_private_entry(entry))
133 				goto next;
134 
135 			page = pfn_swap_entry_to_page(entry);
136 			if (!(migrate->flags &
137 				MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
138 			    page->pgmap->owner != migrate->pgmap_owner)
139 				goto next;
140 
141 			mpfn = migrate_pfn(page_to_pfn(page)) |
142 					MIGRATE_PFN_MIGRATE;
143 			if (is_writable_device_private_entry(entry))
144 				mpfn |= MIGRATE_PFN_WRITE;
145 		} else {
146 			pfn = pte_pfn(pte);
147 			if (is_zero_pfn(pfn) &&
148 			    (migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
149 				mpfn = MIGRATE_PFN_MIGRATE;
150 				migrate->cpages++;
151 				goto next;
152 			}
153 			page = vm_normal_page(migrate->vma, addr, pte);
154 			if (page && !is_zone_device_page(page) &&
155 			    !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
156 				goto next;
157 			else if (page && is_device_coherent_page(page) &&
158 			    (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
159 			     page->pgmap->owner != migrate->pgmap_owner))
160 				goto next;
161 			mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
162 			mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
163 		}
164 
165 		/* FIXME support THP */
166 		if (!page || !page->mapping || PageTransCompound(page)) {
167 			mpfn = 0;
168 			goto next;
169 		}
170 
171 		/*
172 		 * By getting a reference on the folio we pin it and that blocks
173 		 * any kind of migration. Side effect is that it "freezes" the
174 		 * pte.
175 		 *
176 		 * We drop this reference after isolating the folio from the lru
177 		 * for non device folio (device folio are not on the lru and thus
178 		 * can't be dropped from it).
179 		 */
180 		folio = page_folio(page);
181 		folio_get(folio);
182 
183 		/*
184 		 * We rely on folio_trylock() to avoid deadlock between
185 		 * concurrent migrations where each is waiting on the others
186 		 * folio lock. If we can't immediately lock the folio we fail this
187 		 * migration as it is only best effort anyway.
188 		 *
189 		 * If we can lock the folio it's safe to set up a migration entry
190 		 * now. In the common case where the folio is mapped once in a
191 		 * single process setting up the migration entry now is an
192 		 * optimisation to avoid walking the rmap later with
193 		 * try_to_migrate().
194 		 */
195 		if (folio_trylock(folio)) {
196 			bool anon_exclusive;
197 			pte_t swp_pte;
198 
199 			flush_cache_page(vma, addr, pte_pfn(pte));
200 			anon_exclusive = folio_test_anon(folio) &&
201 					  PageAnonExclusive(page);
202 			if (anon_exclusive) {
203 				pte = ptep_clear_flush(vma, addr, ptep);
204 
205 				if (folio_try_share_anon_rmap_pte(folio, page)) {
206 					set_pte_at(mm, addr, ptep, pte);
207 					folio_unlock(folio);
208 					folio_put(folio);
209 					mpfn = 0;
210 					goto next;
211 				}
212 			} else {
213 				pte = ptep_get_and_clear(mm, addr, ptep);
214 			}
215 
216 			migrate->cpages++;
217 
218 			/* Set the dirty flag on the folio now the pte is gone. */
219 			if (pte_dirty(pte))
220 				folio_mark_dirty(folio);
221 
222 			/* Setup special migration page table entry */
223 			if (mpfn & MIGRATE_PFN_WRITE)
224 				entry = make_writable_migration_entry(
225 							page_to_pfn(page));
226 			else if (anon_exclusive)
227 				entry = make_readable_exclusive_migration_entry(
228 							page_to_pfn(page));
229 			else
230 				entry = make_readable_migration_entry(
231 							page_to_pfn(page));
232 			if (pte_present(pte)) {
233 				if (pte_young(pte))
234 					entry = make_migration_entry_young(entry);
235 				if (pte_dirty(pte))
236 					entry = make_migration_entry_dirty(entry);
237 			}
238 			swp_pte = swp_entry_to_pte(entry);
239 			if (pte_present(pte)) {
240 				if (pte_soft_dirty(pte))
241 					swp_pte = pte_swp_mksoft_dirty(swp_pte);
242 				if (pte_uffd_wp(pte))
243 					swp_pte = pte_swp_mkuffd_wp(swp_pte);
244 			} else {
245 				if (pte_swp_soft_dirty(pte))
246 					swp_pte = pte_swp_mksoft_dirty(swp_pte);
247 				if (pte_swp_uffd_wp(pte))
248 					swp_pte = pte_swp_mkuffd_wp(swp_pte);
249 			}
250 			set_pte_at(mm, addr, ptep, swp_pte);
251 
252 			/*
253 			 * This is like regular unmap: we remove the rmap and
254 			 * drop the folio refcount. The folio won't be freed, as
255 			 * we took a reference just above.
256 			 */
257 			folio_remove_rmap_pte(folio, page, vma);
258 			folio_put(folio);
259 
260 			if (pte_present(pte))
261 				unmapped++;
262 		} else {
263 			folio_put(folio);
264 			mpfn = 0;
265 		}
266 
267 next:
268 		migrate->dst[migrate->npages] = 0;
269 		migrate->src[migrate->npages++] = mpfn;
270 	}
271 
272 	/* Only flush the TLB if we actually modified any entries */
273 	if (unmapped)
274 		flush_tlb_range(walk->vma, start, end);
275 
276 	arch_leave_lazy_mmu_mode();
277 	pte_unmap_unlock(ptep - 1, ptl);
278 
279 	return 0;
280 }
281 
282 static const struct mm_walk_ops migrate_vma_walk_ops = {
283 	.pmd_entry		= migrate_vma_collect_pmd,
284 	.pte_hole		= migrate_vma_collect_hole,
285 	.walk_lock		= PGWALK_RDLOCK,
286 };
287 
288 /*
289  * migrate_vma_collect() - collect pages over a range of virtual addresses
290  * @migrate: migrate struct containing all migration information
291  *
292  * This will walk the CPU page table. For each virtual address backed by a
293  * valid page, it updates the src array and takes a reference on the page, in
294  * order to pin the page until we lock it and unmap it.
295  */
296 static void migrate_vma_collect(struct migrate_vma *migrate)
297 {
298 	struct mmu_notifier_range range;
299 
300 	/*
301 	 * Note that the pgmap_owner is passed to the mmu notifier callback so
302 	 * that the registered device driver can skip invalidating device
303 	 * private page mappings that won't be migrated.
304 	 */
305 	mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
306 		migrate->vma->vm_mm, migrate->start, migrate->end,
307 		migrate->pgmap_owner);
308 	mmu_notifier_invalidate_range_start(&range);
309 
310 	walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
311 			&migrate_vma_walk_ops, migrate);
312 
313 	mmu_notifier_invalidate_range_end(&range);
314 	migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
315 }
316 
317 /*
318  * migrate_vma_check_page() - check if page is pinned or not
319  * @page: struct page to check
320  *
321  * Pinned pages cannot be migrated. This is the same test as in
322  * folio_migrate_mapping(), except that here we allow migration of a
323  * ZONE_DEVICE page.
324  */
325 static bool migrate_vma_check_page(struct page *page, struct page *fault_page)
326 {
327 	/*
328 	 * One extra ref because caller holds an extra reference, either from
329 	 * isolate_lru_page() for a regular page, or migrate_vma_collect() for
330 	 * a device page.
331 	 */
332 	int extra = 1 + (page == fault_page);
333 
334 	/*
335 	 * FIXME support THP (transparent huge page), it is bit more complex to
336 	 * check them than regular pages, because they can be mapped with a pmd
337 	 * or with a pte (split pte mapping).
338 	 */
339 	if (PageCompound(page))
340 		return false;
341 
342 	/* Page from ZONE_DEVICE have one extra reference */
343 	if (is_zone_device_page(page))
344 		extra++;
345 
346 	/* For file back page */
347 	if (page_mapping(page))
348 		extra += 1 + page_has_private(page);
349 
350 	if ((page_count(page) - extra) > page_mapcount(page))
351 		return false;
352 
353 	return true;
354 }
355 
356 /*
357  * Unmaps pages for migration. Returns number of source pfns marked as
358  * migrating.
359  */
360 static unsigned long migrate_device_unmap(unsigned long *src_pfns,
361 					  unsigned long npages,
362 					  struct page *fault_page)
363 {
364 	unsigned long i, restore = 0;
365 	bool allow_drain = true;
366 	unsigned long unmapped = 0;
367 
368 	lru_add_drain();
369 
370 	for (i = 0; i < npages; i++) {
371 		struct page *page = migrate_pfn_to_page(src_pfns[i]);
372 		struct folio *folio;
373 
374 		if (!page) {
375 			if (src_pfns[i] & MIGRATE_PFN_MIGRATE)
376 				unmapped++;
377 			continue;
378 		}
379 
380 		/* ZONE_DEVICE pages are not on LRU */
381 		if (!is_zone_device_page(page)) {
382 			if (!PageLRU(page) && allow_drain) {
383 				/* Drain CPU's lru cache */
384 				lru_add_drain_all();
385 				allow_drain = false;
386 			}
387 
388 			if (!isolate_lru_page(page)) {
389 				src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
390 				restore++;
391 				continue;
392 			}
393 
394 			/* Drop the reference we took in collect */
395 			put_page(page);
396 		}
397 
398 		folio = page_folio(page);
399 		if (folio_mapped(folio))
400 			try_to_migrate(folio, 0);
401 
402 		if (page_mapped(page) ||
403 		    !migrate_vma_check_page(page, fault_page)) {
404 			if (!is_zone_device_page(page)) {
405 				get_page(page);
406 				putback_lru_page(page);
407 			}
408 
409 			src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
410 			restore++;
411 			continue;
412 		}
413 
414 		unmapped++;
415 	}
416 
417 	for (i = 0; i < npages && restore; i++) {
418 		struct page *page = migrate_pfn_to_page(src_pfns[i]);
419 		struct folio *folio;
420 
421 		if (!page || (src_pfns[i] & MIGRATE_PFN_MIGRATE))
422 			continue;
423 
424 		folio = page_folio(page);
425 		remove_migration_ptes(folio, folio, false);
426 
427 		src_pfns[i] = 0;
428 		folio_unlock(folio);
429 		folio_put(folio);
430 		restore--;
431 	}
432 
433 	return unmapped;
434 }
435 
436 /*
437  * migrate_vma_unmap() - replace page mapping with special migration pte entry
438  * @migrate: migrate struct containing all migration information
439  *
440  * Isolate pages from the LRU and replace mappings (CPU page table pte) with a
441  * special migration pte entry and check if it has been pinned. Pinned pages are
442  * restored because we cannot migrate them.
443  *
444  * This is the last step before we call the device driver callback to allocate
445  * destination memory and copy contents of original page over to new page.
446  */
447 static void migrate_vma_unmap(struct migrate_vma *migrate)
448 {
449 	migrate->cpages = migrate_device_unmap(migrate->src, migrate->npages,
450 					migrate->fault_page);
451 }
452 
453 /**
454  * migrate_vma_setup() - prepare to migrate a range of memory
455  * @args: contains the vma, start, and pfns arrays for the migration
456  *
457  * Returns: negative errno on failures, 0 when 0 or more pages were migrated
458  * without an error.
459  *
460  * Prepare to migrate a range of memory virtual address range by collecting all
461  * the pages backing each virtual address in the range, saving them inside the
462  * src array.  Then lock those pages and unmap them. Once the pages are locked
463  * and unmapped, check whether each page is pinned or not.  Pages that aren't
464  * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
465  * corresponding src array entry.  Then restores any pages that are pinned, by
466  * remapping and unlocking those pages.
467  *
468  * The caller should then allocate destination memory and copy source memory to
469  * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
470  * flag set).  Once these are allocated and copied, the caller must update each
471  * corresponding entry in the dst array with the pfn value of the destination
472  * page and with MIGRATE_PFN_VALID. Destination pages must be locked via
473  * lock_page().
474  *
475  * Note that the caller does not have to migrate all the pages that are marked
476  * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
477  * device memory to system memory.  If the caller cannot migrate a device page
478  * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
479  * consequences for the userspace process, so it must be avoided if at all
480  * possible.
481  *
482  * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
483  * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
484  * allowing the caller to allocate device memory for those unbacked virtual
485  * addresses.  For this the caller simply has to allocate device memory and
486  * properly set the destination entry like for regular migration.  Note that
487  * this can still fail, and thus inside the device driver you must check if the
488  * migration was successful for those entries after calling migrate_vma_pages(),
489  * just like for regular migration.
490  *
491  * After that, the callers must call migrate_vma_pages() to go over each entry
492  * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
493  * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
494  * then migrate_vma_pages() to migrate struct page information from the source
495  * struct page to the destination struct page.  If it fails to migrate the
496  * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
497  * src array.
498  *
499  * At this point all successfully migrated pages have an entry in the src
500  * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
501  * array entry with MIGRATE_PFN_VALID flag set.
502  *
503  * Once migrate_vma_pages() returns the caller may inspect which pages were
504  * successfully migrated, and which were not.  Successfully migrated pages will
505  * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
506  *
507  * It is safe to update device page table after migrate_vma_pages() because
508  * both destination and source page are still locked, and the mmap_lock is held
509  * in read mode (hence no one can unmap the range being migrated).
510  *
511  * Once the caller is done cleaning up things and updating its page table (if it
512  * chose to do so, this is not an obligation) it finally calls
513  * migrate_vma_finalize() to update the CPU page table to point to new pages
514  * for successfully migrated pages or otherwise restore the CPU page table to
515  * point to the original source pages.
516  */
517 int migrate_vma_setup(struct migrate_vma *args)
518 {
519 	long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
520 
521 	args->start &= PAGE_MASK;
522 	args->end &= PAGE_MASK;
523 	if (!args->vma || is_vm_hugetlb_page(args->vma) ||
524 	    (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
525 		return -EINVAL;
526 	if (nr_pages <= 0)
527 		return -EINVAL;
528 	if (args->start < args->vma->vm_start ||
529 	    args->start >= args->vma->vm_end)
530 		return -EINVAL;
531 	if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
532 		return -EINVAL;
533 	if (!args->src || !args->dst)
534 		return -EINVAL;
535 	if (args->fault_page && !is_device_private_page(args->fault_page))
536 		return -EINVAL;
537 
538 	memset(args->src, 0, sizeof(*args->src) * nr_pages);
539 	args->cpages = 0;
540 	args->npages = 0;
541 
542 	migrate_vma_collect(args);
543 
544 	if (args->cpages)
545 		migrate_vma_unmap(args);
546 
547 	/*
548 	 * At this point pages are locked and unmapped, and thus they have
549 	 * stable content and can safely be copied to destination memory that
550 	 * is allocated by the drivers.
551 	 */
552 	return 0;
553 
554 }
555 EXPORT_SYMBOL(migrate_vma_setup);
556 
557 /*
558  * This code closely matches the code in:
559  *   __handle_mm_fault()
560  *     handle_pte_fault()
561  *       do_anonymous_page()
562  * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
563  * private or coherent page.
564  */
565 static void migrate_vma_insert_page(struct migrate_vma *migrate,
566 				    unsigned long addr,
567 				    struct page *page,
568 				    unsigned long *src)
569 {
570 	struct folio *folio = page_folio(page);
571 	struct vm_area_struct *vma = migrate->vma;
572 	struct mm_struct *mm = vma->vm_mm;
573 	bool flush = false;
574 	spinlock_t *ptl;
575 	pte_t entry;
576 	pgd_t *pgdp;
577 	p4d_t *p4dp;
578 	pud_t *pudp;
579 	pmd_t *pmdp;
580 	pte_t *ptep;
581 	pte_t orig_pte;
582 
583 	/* Only allow populating anonymous memory */
584 	if (!vma_is_anonymous(vma))
585 		goto abort;
586 
587 	pgdp = pgd_offset(mm, addr);
588 	p4dp = p4d_alloc(mm, pgdp, addr);
589 	if (!p4dp)
590 		goto abort;
591 	pudp = pud_alloc(mm, p4dp, addr);
592 	if (!pudp)
593 		goto abort;
594 	pmdp = pmd_alloc(mm, pudp, addr);
595 	if (!pmdp)
596 		goto abort;
597 	if (pmd_trans_huge(*pmdp) || pmd_devmap(*pmdp))
598 		goto abort;
599 	if (pte_alloc(mm, pmdp))
600 		goto abort;
601 	if (unlikely(anon_vma_prepare(vma)))
602 		goto abort;
603 	if (mem_cgroup_charge(folio, vma->vm_mm, GFP_KERNEL))
604 		goto abort;
605 
606 	/*
607 	 * The memory barrier inside __folio_mark_uptodate makes sure that
608 	 * preceding stores to the folio contents become visible before
609 	 * the set_pte_at() write.
610 	 */
611 	__folio_mark_uptodate(folio);
612 
613 	if (folio_is_device_private(folio)) {
614 		swp_entry_t swp_entry;
615 
616 		if (vma->vm_flags & VM_WRITE)
617 			swp_entry = make_writable_device_private_entry(
618 						page_to_pfn(page));
619 		else
620 			swp_entry = make_readable_device_private_entry(
621 						page_to_pfn(page));
622 		entry = swp_entry_to_pte(swp_entry);
623 	} else {
624 		if (folio_is_zone_device(folio) &&
625 		    !folio_is_device_coherent(folio)) {
626 			pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
627 			goto abort;
628 		}
629 		entry = mk_pte(page, vma->vm_page_prot);
630 		if (vma->vm_flags & VM_WRITE)
631 			entry = pte_mkwrite(pte_mkdirty(entry), vma);
632 	}
633 
634 	ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
635 	if (!ptep)
636 		goto abort;
637 	orig_pte = ptep_get(ptep);
638 
639 	if (check_stable_address_space(mm))
640 		goto unlock_abort;
641 
642 	if (pte_present(orig_pte)) {
643 		unsigned long pfn = pte_pfn(orig_pte);
644 
645 		if (!is_zero_pfn(pfn))
646 			goto unlock_abort;
647 		flush = true;
648 	} else if (!pte_none(orig_pte))
649 		goto unlock_abort;
650 
651 	/*
652 	 * Check for userfaultfd but do not deliver the fault. Instead,
653 	 * just back off.
654 	 */
655 	if (userfaultfd_missing(vma))
656 		goto unlock_abort;
657 
658 	inc_mm_counter(mm, MM_ANONPAGES);
659 	folio_add_new_anon_rmap(folio, vma, addr);
660 	if (!folio_is_zone_device(folio))
661 		folio_add_lru_vma(folio, vma);
662 	folio_get(folio);
663 
664 	if (flush) {
665 		flush_cache_page(vma, addr, pte_pfn(orig_pte));
666 		ptep_clear_flush(vma, addr, ptep);
667 		set_pte_at_notify(mm, addr, ptep, entry);
668 		update_mmu_cache(vma, addr, ptep);
669 	} else {
670 		/* No need to invalidate - it was non-present before */
671 		set_pte_at(mm, addr, ptep, entry);
672 		update_mmu_cache(vma, addr, ptep);
673 	}
674 
675 	pte_unmap_unlock(ptep, ptl);
676 	*src = MIGRATE_PFN_MIGRATE;
677 	return;
678 
679 unlock_abort:
680 	pte_unmap_unlock(ptep, ptl);
681 abort:
682 	*src &= ~MIGRATE_PFN_MIGRATE;
683 }
684 
685 static void __migrate_device_pages(unsigned long *src_pfns,
686 				unsigned long *dst_pfns, unsigned long npages,
687 				struct migrate_vma *migrate)
688 {
689 	struct mmu_notifier_range range;
690 	unsigned long i;
691 	bool notified = false;
692 
693 	for (i = 0; i < npages; i++) {
694 		struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
695 		struct page *page = migrate_pfn_to_page(src_pfns[i]);
696 		struct address_space *mapping;
697 		int r;
698 
699 		if (!newpage) {
700 			src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
701 			continue;
702 		}
703 
704 		if (!page) {
705 			unsigned long addr;
706 
707 			if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE))
708 				continue;
709 
710 			/*
711 			 * The only time there is no vma is when called from
712 			 * migrate_device_coherent_page(). However this isn't
713 			 * called if the page could not be unmapped.
714 			 */
715 			VM_BUG_ON(!migrate);
716 			addr = migrate->start + i*PAGE_SIZE;
717 			if (!notified) {
718 				notified = true;
719 
720 				mmu_notifier_range_init_owner(&range,
721 					MMU_NOTIFY_MIGRATE, 0,
722 					migrate->vma->vm_mm, addr, migrate->end,
723 					migrate->pgmap_owner);
724 				mmu_notifier_invalidate_range_start(&range);
725 			}
726 			migrate_vma_insert_page(migrate, addr, newpage,
727 						&src_pfns[i]);
728 			continue;
729 		}
730 
731 		mapping = page_mapping(page);
732 
733 		if (is_device_private_page(newpage) ||
734 		    is_device_coherent_page(newpage)) {
735 			if (mapping) {
736 				struct folio *folio;
737 
738 				folio = page_folio(page);
739 
740 				/*
741 				 * For now only support anonymous memory migrating to
742 				 * device private or coherent memory.
743 				 *
744 				 * Try to get rid of swap cache if possible.
745 				 */
746 				if (!folio_test_anon(folio) ||
747 				    !folio_free_swap(folio)) {
748 					src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
749 					continue;
750 				}
751 			}
752 		} else if (is_zone_device_page(newpage)) {
753 			/*
754 			 * Other types of ZONE_DEVICE page are not supported.
755 			 */
756 			src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
757 			continue;
758 		}
759 
760 		if (migrate && migrate->fault_page == page)
761 			r = migrate_folio_extra(mapping, page_folio(newpage),
762 						page_folio(page),
763 						MIGRATE_SYNC_NO_COPY, 1);
764 		else
765 			r = migrate_folio(mapping, page_folio(newpage),
766 					page_folio(page), MIGRATE_SYNC_NO_COPY);
767 		if (r != MIGRATEPAGE_SUCCESS)
768 			src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
769 	}
770 
771 	if (notified)
772 		mmu_notifier_invalidate_range_end(&range);
773 }
774 
775 /**
776  * migrate_device_pages() - migrate meta-data from src page to dst page
777  * @src_pfns: src_pfns returned from migrate_device_range()
778  * @dst_pfns: array of pfns allocated by the driver to migrate memory to
779  * @npages: number of pages in the range
780  *
781  * Equivalent to migrate_vma_pages(). This is called to migrate struct page
782  * meta-data from source struct page to destination.
783  */
784 void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,
785 			unsigned long npages)
786 {
787 	__migrate_device_pages(src_pfns, dst_pfns, npages, NULL);
788 }
789 EXPORT_SYMBOL(migrate_device_pages);
790 
791 /**
792  * migrate_vma_pages() - migrate meta-data from src page to dst page
793  * @migrate: migrate struct containing all migration information
794  *
795  * This migrates struct page meta-data from source struct page to destination
796  * struct page. This effectively finishes the migration from source page to the
797  * destination page.
798  */
799 void migrate_vma_pages(struct migrate_vma *migrate)
800 {
801 	__migrate_device_pages(migrate->src, migrate->dst, migrate->npages, migrate);
802 }
803 EXPORT_SYMBOL(migrate_vma_pages);
804 
805 /*
806  * migrate_device_finalize() - complete page migration
807  * @src_pfns: src_pfns returned from migrate_device_range()
808  * @dst_pfns: array of pfns allocated by the driver to migrate memory to
809  * @npages: number of pages in the range
810  *
811  * Completes migration of the page by removing special migration entries.
812  * Drivers must ensure copying of page data is complete and visible to the CPU
813  * before calling this.
814  */
815 void migrate_device_finalize(unsigned long *src_pfns,
816 			unsigned long *dst_pfns, unsigned long npages)
817 {
818 	unsigned long i;
819 
820 	for (i = 0; i < npages; i++) {
821 		struct folio *dst, *src;
822 		struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
823 		struct page *page = migrate_pfn_to_page(src_pfns[i]);
824 
825 		if (!page) {
826 			if (newpage) {
827 				unlock_page(newpage);
828 				put_page(newpage);
829 			}
830 			continue;
831 		}
832 
833 		if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE) || !newpage) {
834 			if (newpage) {
835 				unlock_page(newpage);
836 				put_page(newpage);
837 			}
838 			newpage = page;
839 		}
840 
841 		src = page_folio(page);
842 		dst = page_folio(newpage);
843 		remove_migration_ptes(src, dst, false);
844 		folio_unlock(src);
845 
846 		if (is_zone_device_page(page))
847 			put_page(page);
848 		else
849 			putback_lru_page(page);
850 
851 		if (newpage != page) {
852 			unlock_page(newpage);
853 			if (is_zone_device_page(newpage))
854 				put_page(newpage);
855 			else
856 				putback_lru_page(newpage);
857 		}
858 	}
859 }
860 EXPORT_SYMBOL(migrate_device_finalize);
861 
862 /**
863  * migrate_vma_finalize() - restore CPU page table entry
864  * @migrate: migrate struct containing all migration information
865  *
866  * This replaces the special migration pte entry with either a mapping to the
867  * new page if migration was successful for that page, or to the original page
868  * otherwise.
869  *
870  * This also unlocks the pages and puts them back on the lru, or drops the extra
871  * refcount, for device pages.
872  */
873 void migrate_vma_finalize(struct migrate_vma *migrate)
874 {
875 	migrate_device_finalize(migrate->src, migrate->dst, migrate->npages);
876 }
877 EXPORT_SYMBOL(migrate_vma_finalize);
878 
879 /**
880  * migrate_device_range() - migrate device private pfns to normal memory.
881  * @src_pfns: array large enough to hold migrating source device private pfns.
882  * @start: starting pfn in the range to migrate.
883  * @npages: number of pages to migrate.
884  *
885  * migrate_vma_setup() is similar in concept to migrate_vma_setup() except that
886  * instead of looking up pages based on virtual address mappings a range of
887  * device pfns that should be migrated to system memory is used instead.
888  *
889  * This is useful when a driver needs to free device memory but doesn't know the
890  * virtual mappings of every page that may be in device memory. For example this
891  * is often the case when a driver is being unloaded or unbound from a device.
892  *
893  * Like migrate_vma_setup() this function will take a reference and lock any
894  * migrating pages that aren't free before unmapping them. Drivers may then
895  * allocate destination pages and start copying data from the device to CPU
896  * memory before calling migrate_device_pages().
897  */
898 int migrate_device_range(unsigned long *src_pfns, unsigned long start,
899 			unsigned long npages)
900 {
901 	unsigned long i, pfn;
902 
903 	for (pfn = start, i = 0; i < npages; pfn++, i++) {
904 		struct page *page = pfn_to_page(pfn);
905 
906 		if (!get_page_unless_zero(page)) {
907 			src_pfns[i] = 0;
908 			continue;
909 		}
910 
911 		if (!trylock_page(page)) {
912 			src_pfns[i] = 0;
913 			put_page(page);
914 			continue;
915 		}
916 
917 		src_pfns[i] = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
918 	}
919 
920 	migrate_device_unmap(src_pfns, npages, NULL);
921 
922 	return 0;
923 }
924 EXPORT_SYMBOL(migrate_device_range);
925 
926 /*
927  * Migrate a device coherent page back to normal memory. The caller should have
928  * a reference on page which will be copied to the new page if migration is
929  * successful or dropped on failure.
930  */
931 int migrate_device_coherent_page(struct page *page)
932 {
933 	unsigned long src_pfn, dst_pfn = 0;
934 	struct page *dpage;
935 
936 	WARN_ON_ONCE(PageCompound(page));
937 
938 	lock_page(page);
939 	src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
940 
941 	/*
942 	 * We don't have a VMA and don't need to walk the page tables to find
943 	 * the source page. So call migrate_vma_unmap() directly to unmap the
944 	 * page as migrate_vma_setup() will fail if args.vma == NULL.
945 	 */
946 	migrate_device_unmap(&src_pfn, 1, NULL);
947 	if (!(src_pfn & MIGRATE_PFN_MIGRATE))
948 		return -EBUSY;
949 
950 	dpage = alloc_page(GFP_USER | __GFP_NOWARN);
951 	if (dpage) {
952 		lock_page(dpage);
953 		dst_pfn = migrate_pfn(page_to_pfn(dpage));
954 	}
955 
956 	migrate_device_pages(&src_pfn, &dst_pfn, 1);
957 	if (src_pfn & MIGRATE_PFN_MIGRATE)
958 		copy_highpage(dpage, page);
959 	migrate_device_finalize(&src_pfn, &dst_pfn, 1);
960 
961 	if (src_pfn & MIGRATE_PFN_MIGRATE)
962 		return 0;
963 	return -EBUSY;
964 }
965