xref: /linux/mm/migrate_device.c (revision d60ec36cab338dfe2ae40d73e9c8d6c4af70d2b8)
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/leafops.h>
17 #include <linux/pgalloc.h>
18 #include <asm/tlbflush.h>
19 #include "internal.h"
20 
migrate_vma_collect_skip(unsigned long start,unsigned long end,struct mm_walk * walk)21 static int migrate_vma_collect_skip(unsigned long start,
22 				    unsigned long end,
23 				    struct mm_walk *walk)
24 {
25 	struct migrate_vma *migrate = walk->private;
26 	unsigned long addr;
27 
28 	for (addr = start; addr < end; addr += PAGE_SIZE) {
29 		migrate->dst[migrate->npages] = 0;
30 		migrate->src[migrate->npages++] = 0;
31 	}
32 
33 	return 0;
34 }
35 
migrate_vma_collect_hole(unsigned long start,unsigned long end,__always_unused int depth,struct mm_walk * walk)36 static int migrate_vma_collect_hole(unsigned long start,
37 				    unsigned long end,
38 				    __always_unused int depth,
39 				    struct mm_walk *walk)
40 {
41 	struct migrate_vma *migrate = walk->private;
42 	unsigned long addr;
43 
44 	/* Only allow populating anonymous memory. */
45 	if (!vma_is_anonymous(walk->vma))
46 		return migrate_vma_collect_skip(start, end, walk);
47 
48 	if (thp_migration_supported() &&
49 		(migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) &&
50 		(IS_ALIGNED(start, HPAGE_PMD_SIZE) &&
51 		 IS_ALIGNED(end, HPAGE_PMD_SIZE))) {
52 		migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE |
53 						MIGRATE_PFN_COMPOUND;
54 		migrate->dst[migrate->npages] = 0;
55 		migrate->npages++;
56 		migrate->cpages++;
57 
58 		/*
59 		 * Collect the remaining entries as holes, in case we
60 		 * need to split later
61 		 */
62 		return migrate_vma_collect_skip(start + PAGE_SIZE, end, walk);
63 	}
64 
65 	for (addr = start; addr < end; addr += PAGE_SIZE) {
66 		migrate->src[migrate->npages] = MIGRATE_PFN_MIGRATE;
67 		migrate->dst[migrate->npages] = 0;
68 		migrate->npages++;
69 		migrate->cpages++;
70 	}
71 
72 	return 0;
73 }
74 
75 /**
76  * migrate_vma_split_folio() - Helper function to split a THP folio
77  * @folio: the folio to split
78  * @fault_page: struct page associated with the fault if any
79  *
80  * Returns 0 on success
81  */
migrate_vma_split_folio(struct folio * folio,struct page * fault_page)82 static int migrate_vma_split_folio(struct folio *folio,
83 				   struct page *fault_page)
84 {
85 	int ret;
86 	struct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL;
87 	struct folio *new_fault_folio = NULL;
88 
89 	if (folio != fault_folio) {
90 		folio_get(folio);
91 		folio_lock(folio);
92 	}
93 
94 	ret = split_folio(folio);
95 	if (ret) {
96 		if (folio != fault_folio) {
97 			folio_unlock(folio);
98 			folio_put(folio);
99 		}
100 		return ret;
101 	}
102 
103 	new_fault_folio = fault_page ? page_folio(fault_page) : NULL;
104 
105 	/*
106 	 * Ensure the lock is held on the correct
107 	 * folio after the split
108 	 */
109 	if (!new_fault_folio) {
110 		folio_unlock(folio);
111 		folio_put(folio);
112 	} else if (folio != new_fault_folio) {
113 		if (new_fault_folio != fault_folio) {
114 			folio_get(new_fault_folio);
115 			folio_lock(new_fault_folio);
116 		}
117 		folio_unlock(folio);
118 		folio_put(folio);
119 	}
120 
121 	return 0;
122 }
123 
124 /** migrate_vma_collect_huge_pmd - collect THP pages without splitting the
125  * folio for device private pages.
126  * @pmdp: pointer to pmd entry
127  * @start: start address of the range for migration
128  * @end: end address of the range for migration
129  * @walk: mm_walk callback structure
130  * @fault_folio: folio associated with the fault if any
131  *
132  * Collect the huge pmd entry at @pmdp for migration and set the
133  * MIGRATE_PFN_COMPOUND flag in the migrate src entry to indicate that
134  * migration will occur at HPAGE_PMD granularity
135  */
migrate_vma_collect_huge_pmd(pmd_t * pmdp,unsigned long start,unsigned long end,struct mm_walk * walk,struct folio * fault_folio)136 static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,
137 					unsigned long end, struct mm_walk *walk,
138 					struct folio *fault_folio)
139 {
140 	struct mm_struct *mm = walk->mm;
141 	struct folio *folio;
142 	struct migrate_vma *migrate = walk->private;
143 	spinlock_t *ptl;
144 	int ret;
145 	unsigned long write = 0;
146 
147 	ptl = pmd_lock(mm, pmdp);
148 	if (pmd_none(*pmdp)) {
149 		spin_unlock(ptl);
150 		return migrate_vma_collect_hole(start, end, -1, walk);
151 	}
152 
153 	if (pmd_trans_huge(*pmdp)) {
154 		if (!(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
155 			spin_unlock(ptl);
156 			return migrate_vma_collect_skip(start, end, walk);
157 		}
158 
159 		folio = pmd_folio(*pmdp);
160 		if (is_huge_zero_folio(folio)) {
161 			spin_unlock(ptl);
162 			return migrate_vma_collect_hole(start, end, -1, walk);
163 		}
164 		if (pmd_write(*pmdp))
165 			write = MIGRATE_PFN_WRITE;
166 	} else if (!pmd_present(*pmdp)) {
167 		const softleaf_t entry = softleaf_from_pmd(*pmdp);
168 
169 		folio = softleaf_to_folio(entry);
170 
171 		if (!softleaf_is_device_private(entry) ||
172 			!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
173 			(folio->pgmap->owner != migrate->pgmap_owner)) {
174 			spin_unlock(ptl);
175 			return migrate_vma_collect_skip(start, end, walk);
176 		}
177 
178 		if (softleaf_is_device_private_write(entry))
179 			write = MIGRATE_PFN_WRITE;
180 	} else {
181 		spin_unlock(ptl);
182 		return -EAGAIN;
183 	}
184 
185 	folio_get(folio);
186 	if (folio != fault_folio && unlikely(!folio_trylock(folio))) {
187 		spin_unlock(ptl);
188 		folio_put(folio);
189 		return migrate_vma_collect_skip(start, end, walk);
190 	}
191 
192 	if (thp_migration_supported() &&
193 		(migrate->flags & MIGRATE_VMA_SELECT_COMPOUND) &&
194 		(IS_ALIGNED(start, HPAGE_PMD_SIZE) &&
195 		 IS_ALIGNED(end, HPAGE_PMD_SIZE))) {
196 
197 		struct page_vma_mapped_walk pvmw = {
198 			.ptl = ptl,
199 			.address = start,
200 			.pmd = pmdp,
201 			.vma = walk->vma,
202 		};
203 
204 		unsigned long pfn = page_to_pfn(folio_page(folio, 0));
205 
206 		migrate->src[migrate->npages] = migrate_pfn(pfn) | write
207 						| MIGRATE_PFN_MIGRATE
208 						| MIGRATE_PFN_COMPOUND;
209 		migrate->dst[migrate->npages++] = 0;
210 		migrate->cpages++;
211 		ret = set_pmd_migration_entry(&pvmw, folio_page(folio, 0));
212 		if (ret) {
213 			migrate->npages--;
214 			migrate->cpages--;
215 			migrate->src[migrate->npages] = 0;
216 			migrate->dst[migrate->npages] = 0;
217 			goto fallback;
218 		}
219 		migrate_vma_collect_skip(start + PAGE_SIZE, end, walk);
220 		spin_unlock(ptl);
221 		return 0;
222 	}
223 
224 fallback:
225 	spin_unlock(ptl);
226 	if (!folio_test_large(folio))
227 		goto done;
228 	ret = split_folio(folio);
229 	if (fault_folio != folio)
230 		folio_unlock(folio);
231 	folio_put(folio);
232 	if (ret)
233 		return migrate_vma_collect_skip(start, end, walk);
234 	if (pmd_none(pmdp_get_lockless(pmdp)))
235 		return migrate_vma_collect_hole(start, end, -1, walk);
236 
237 done:
238 	return -ENOENT;
239 }
240 
migrate_vma_collect_pmd(pmd_t * pmdp,unsigned long start,unsigned long end,struct mm_walk * walk)241 static int migrate_vma_collect_pmd(pmd_t *pmdp,
242 				   unsigned long start,
243 				   unsigned long end,
244 				   struct mm_walk *walk)
245 {
246 	struct migrate_vma *migrate = walk->private;
247 	struct vm_area_struct *vma = walk->vma;
248 	struct mm_struct *mm = vma->vm_mm;
249 	unsigned long addr = start, unmapped = 0;
250 	spinlock_t *ptl;
251 	struct folio *fault_folio = migrate->fault_page ?
252 		page_folio(migrate->fault_page) : NULL;
253 	pte_t *ptep;
254 
255 again:
256 	if (pmd_trans_huge(*pmdp) || !pmd_present(*pmdp)) {
257 		int ret = migrate_vma_collect_huge_pmd(pmdp, start, end, walk, fault_folio);
258 
259 		if (ret == -EAGAIN)
260 			goto again;
261 		if (ret == 0)
262 			return 0;
263 	}
264 
265 	ptep = pte_offset_map_lock(mm, pmdp, start, &ptl);
266 	if (!ptep)
267 		goto again;
268 	lazy_mmu_mode_enable();
269 	ptep += (addr - start) / PAGE_SIZE;
270 
271 	for (; addr < end; addr += PAGE_SIZE, ptep++) {
272 		struct dev_pagemap *pgmap;
273 		unsigned long mpfn = 0, pfn;
274 		struct folio *folio;
275 		struct page *page;
276 		softleaf_t entry;
277 		pte_t pte;
278 
279 		pte = ptep_get(ptep);
280 
281 		if (pte_none(pte)) {
282 			if (vma_is_anonymous(vma)) {
283 				mpfn = MIGRATE_PFN_MIGRATE;
284 				migrate->cpages++;
285 			}
286 			goto next;
287 		}
288 
289 		if (!pte_present(pte)) {
290 			/*
291 			 * Only care about unaddressable device page special
292 			 * page table entry. Other special swap entries are not
293 			 * migratable, and we ignore regular swapped page.
294 			 */
295 			entry = softleaf_from_pte(pte);
296 			if (!softleaf_is_device_private(entry))
297 				goto next;
298 
299 			page = softleaf_to_page(entry);
300 			pgmap = page_pgmap(page);
301 			if (!(migrate->flags &
302 				MIGRATE_VMA_SELECT_DEVICE_PRIVATE) ||
303 			    pgmap->owner != migrate->pgmap_owner)
304 				goto next;
305 
306 			folio = page_folio(page);
307 			if (folio_test_large(folio)) {
308 				int ret;
309 
310 				lazy_mmu_mode_disable();
311 				pte_unmap_unlock(ptep, ptl);
312 				ret = migrate_vma_split_folio(folio,
313 							  migrate->fault_page);
314 
315 				if (ret) {
316 					if (unmapped)
317 						flush_tlb_range(walk->vma, start, end);
318 
319 					return migrate_vma_collect_skip(addr, end, walk);
320 				}
321 
322 				goto again;
323 			}
324 
325 			mpfn = migrate_pfn(page_to_pfn(page)) |
326 					MIGRATE_PFN_MIGRATE;
327 			if (softleaf_is_device_private_write(entry))
328 				mpfn |= MIGRATE_PFN_WRITE;
329 		} else {
330 			pfn = pte_pfn(pte);
331 			if (is_zero_pfn(pfn) &&
332 			    (migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
333 				mpfn = MIGRATE_PFN_MIGRATE;
334 				migrate->cpages++;
335 				goto next;
336 			}
337 			page = vm_normal_page(migrate->vma, addr, pte);
338 			if (page && !is_zone_device_page(page) &&
339 			    !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
340 				goto next;
341 			} else if (page && is_device_coherent_page(page)) {
342 				pgmap = page_pgmap(page);
343 
344 				if (!(migrate->flags &
345 					MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
346 					pgmap->owner != migrate->pgmap_owner)
347 					goto next;
348 			}
349 			folio = page ? page_folio(page) : NULL;
350 			if (folio && folio_test_large(folio)) {
351 				int ret;
352 
353 				lazy_mmu_mode_disable();
354 				pte_unmap_unlock(ptep, ptl);
355 				ret = migrate_vma_split_folio(folio,
356 							  migrate->fault_page);
357 
358 				if (ret) {
359 					if (unmapped)
360 						flush_tlb_range(walk->vma, start, end);
361 
362 					return migrate_vma_collect_skip(addr, end, walk);
363 				}
364 
365 				goto again;
366 			}
367 			mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
368 			mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
369 		}
370 
371 		if (!page || !page->mapping) {
372 			mpfn = 0;
373 			goto next;
374 		}
375 
376 		/*
377 		 * By getting a reference on the folio we pin it and that blocks
378 		 * any kind of migration. Side effect is that it "freezes" the
379 		 * pte.
380 		 *
381 		 * We drop this reference after isolating the folio from the lru
382 		 * for non device folio (device folio are not on the lru and thus
383 		 * can't be dropped from it).
384 		 */
385 		folio = page_folio(page);
386 		folio_get(folio);
387 
388 		/*
389 		 * We rely on folio_trylock() to avoid deadlock between
390 		 * concurrent migrations where each is waiting on the others
391 		 * folio lock. If we can't immediately lock the folio we fail this
392 		 * migration as it is only best effort anyway.
393 		 *
394 		 * If we can lock the folio it's safe to set up a migration entry
395 		 * now. In the common case where the folio is mapped once in a
396 		 * single process setting up the migration entry now is an
397 		 * optimisation to avoid walking the rmap later with
398 		 * try_to_migrate().
399 		 */
400 		if (fault_folio == folio || folio_trylock(folio)) {
401 			bool anon_exclusive;
402 			pte_t swp_pte;
403 
404 			flush_cache_page(vma, addr, pte_pfn(pte));
405 			anon_exclusive = folio_test_anon(folio) &&
406 					  PageAnonExclusive(page);
407 			if (anon_exclusive) {
408 				pte = ptep_clear_flush(vma, addr, ptep);
409 
410 				if (folio_try_share_anon_rmap_pte(folio, page)) {
411 					set_pte_at(mm, addr, ptep, pte);
412 					if (fault_folio != folio)
413 						folio_unlock(folio);
414 					folio_put(folio);
415 					mpfn = 0;
416 					goto next;
417 				}
418 			} else {
419 				pte = ptep_get_and_clear(mm, addr, ptep);
420 			}
421 
422 			migrate->cpages++;
423 
424 			/* Set the dirty flag on the folio now the pte is gone. */
425 			if (pte_dirty(pte))
426 				folio_mark_dirty(folio);
427 
428 			/* Setup special migration page table entry */
429 			if (mpfn & MIGRATE_PFN_WRITE)
430 				entry = make_writable_migration_entry(
431 							page_to_pfn(page));
432 			else if (anon_exclusive)
433 				entry = make_readable_exclusive_migration_entry(
434 							page_to_pfn(page));
435 			else
436 				entry = make_readable_migration_entry(
437 							page_to_pfn(page));
438 			if (pte_present(pte)) {
439 				if (pte_young(pte))
440 					entry = make_migration_entry_young(entry);
441 				if (pte_dirty(pte))
442 					entry = make_migration_entry_dirty(entry);
443 			}
444 			swp_pte = swp_entry_to_pte(entry);
445 			if (pte_present(pte)) {
446 				if (pte_soft_dirty(pte))
447 					swp_pte = pte_swp_mksoft_dirty(swp_pte);
448 				if (pte_uffd_wp(pte))
449 					swp_pte = pte_swp_mkuffd_wp(swp_pte);
450 			} else {
451 				if (pte_swp_soft_dirty(pte))
452 					swp_pte = pte_swp_mksoft_dirty(swp_pte);
453 				if (pte_swp_uffd_wp(pte))
454 					swp_pte = pte_swp_mkuffd_wp(swp_pte);
455 			}
456 			set_pte_at(mm, addr, ptep, swp_pte);
457 
458 			/*
459 			 * This is like regular unmap: we remove the rmap and
460 			 * drop the folio refcount. The folio won't be freed, as
461 			 * we took a reference just above.
462 			 */
463 			folio_remove_rmap_pte(folio, page, vma);
464 			folio_put(folio);
465 
466 			if (pte_present(pte))
467 				unmapped++;
468 		} else {
469 			folio_put(folio);
470 			mpfn = 0;
471 		}
472 
473 next:
474 		migrate->dst[migrate->npages] = 0;
475 		migrate->src[migrate->npages++] = mpfn;
476 	}
477 
478 	/* Only flush the TLB if we actually modified any entries */
479 	if (unmapped)
480 		flush_tlb_range(walk->vma, start, end);
481 
482 	lazy_mmu_mode_disable();
483 	pte_unmap_unlock(ptep - 1, ptl);
484 
485 	return 0;
486 }
487 
488 static const struct mm_walk_ops migrate_vma_walk_ops = {
489 	.pmd_entry		= migrate_vma_collect_pmd,
490 	.pte_hole		= migrate_vma_collect_hole,
491 	.walk_lock		= PGWALK_RDLOCK,
492 };
493 
494 /*
495  * migrate_vma_collect() - collect pages over a range of virtual addresses
496  * @migrate: migrate struct containing all migration information
497  *
498  * This will walk the CPU page table. For each virtual address backed by a
499  * valid page, it updates the src array and takes a reference on the page, in
500  * order to pin the page until we lock it and unmap it.
501  */
migrate_vma_collect(struct migrate_vma * migrate)502 static void migrate_vma_collect(struct migrate_vma *migrate)
503 {
504 	struct mmu_notifier_range range;
505 
506 	/*
507 	 * Note that the pgmap_owner is passed to the mmu notifier callback so
508 	 * that the registered device driver can skip invalidating device
509 	 * private page mappings that won't be migrated.
510 	 */
511 	mmu_notifier_range_init_owner(&range, MMU_NOTIFY_MIGRATE, 0,
512 		migrate->vma->vm_mm, migrate->start, migrate->end,
513 		migrate->pgmap_owner);
514 	mmu_notifier_invalidate_range_start(&range);
515 
516 	walk_page_range(migrate->vma->vm_mm, migrate->start, migrate->end,
517 			&migrate_vma_walk_ops, migrate);
518 
519 	mmu_notifier_invalidate_range_end(&range);
520 	migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
521 }
522 
523 /*
524  * migrate_vma_check_page() - check if page is pinned or not
525  * @page: struct page to check
526  *
527  * Pinned pages cannot be migrated. This is the same test as in
528  * folio_migrate_mapping(), except that here we allow migration of a
529  * ZONE_DEVICE page.
530  */
migrate_vma_check_page(struct page * page,struct page * fault_page)531 static bool migrate_vma_check_page(struct page *page, struct page *fault_page)
532 {
533 	struct folio *folio = page_folio(page);
534 
535 	/*
536 	 * One extra ref because caller holds an extra reference, either from
537 	 * folio_isolate_lru() for a regular folio, or migrate_vma_collect() for
538 	 * a device folio.
539 	 */
540 	int extra = 1 + (page == fault_page);
541 
542 	/* Page from ZONE_DEVICE have one extra reference */
543 	if (folio_is_zone_device(folio))
544 		extra++;
545 
546 	/* For file back page */
547 	if (folio_mapping(folio))
548 		extra += 1 + folio_has_private(folio);
549 
550 	if ((folio_ref_count(folio) - extra) > folio_mapcount(folio))
551 		return false;
552 
553 	return true;
554 }
555 
556 /*
557  * Unmaps pages for migration. Returns number of source pfns marked as
558  * migrating.
559  */
migrate_device_unmap(unsigned long * src_pfns,unsigned long npages,struct page * fault_page)560 static unsigned long migrate_device_unmap(unsigned long *src_pfns,
561 					  unsigned long npages,
562 					  struct page *fault_page)
563 {
564 	struct folio *fault_folio = fault_page ?
565 		page_folio(fault_page) : NULL;
566 	unsigned long i, restore = 0;
567 	bool allow_drain = true;
568 	unsigned long unmapped = 0;
569 
570 	lru_add_drain();
571 
572 	for (i = 0; i < npages; ) {
573 		struct page *page = migrate_pfn_to_page(src_pfns[i]);
574 		struct folio *folio;
575 		unsigned int nr = 1;
576 
577 		if (!page) {
578 			if (src_pfns[i] & MIGRATE_PFN_MIGRATE)
579 				unmapped++;
580 			goto next;
581 		}
582 
583 		folio =	page_folio(page);
584 		nr = folio_nr_pages(folio);
585 
586 		if (nr > 1)
587 			src_pfns[i] |= MIGRATE_PFN_COMPOUND;
588 
589 
590 		/* ZONE_DEVICE folios are not on LRU */
591 		if (!folio_is_zone_device(folio)) {
592 			if (!folio_test_lru(folio) && allow_drain) {
593 				/* Drain CPU's lru cache */
594 				lru_add_drain_all();
595 				allow_drain = false;
596 			}
597 
598 			if (!folio_isolate_lru(folio)) {
599 				src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
600 				restore++;
601 				goto next;
602 			}
603 
604 			/* Drop the reference we took in collect */
605 			folio_put(folio);
606 		}
607 
608 		if (folio_mapped(folio))
609 			try_to_migrate(folio, 0);
610 
611 		if (folio_mapped(folio) ||
612 		    !migrate_vma_check_page(page, fault_page)) {
613 			if (!folio_is_zone_device(folio)) {
614 				folio_get(folio);
615 				folio_putback_lru(folio);
616 			}
617 
618 			src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
619 			restore++;
620 			goto next;
621 		}
622 
623 		unmapped++;
624 next:
625 		i += nr;
626 	}
627 
628 	for (i = 0; i < npages && restore; i++) {
629 		struct page *page = migrate_pfn_to_page(src_pfns[i]);
630 		struct folio *folio;
631 
632 		if (!page || (src_pfns[i] & MIGRATE_PFN_MIGRATE))
633 			continue;
634 
635 		folio = page_folio(page);
636 		remove_migration_ptes(folio, folio, 0);
637 
638 		src_pfns[i] = 0;
639 		if (fault_folio != folio)
640 			folio_unlock(folio);
641 		folio_put(folio);
642 		restore--;
643 	}
644 
645 	return unmapped;
646 }
647 
648 /*
649  * migrate_vma_unmap() - replace page mapping with special migration pte entry
650  * @migrate: migrate struct containing all migration information
651  *
652  * Isolate pages from the LRU and replace mappings (CPU page table pte) with a
653  * special migration pte entry and check if it has been pinned. Pinned pages are
654  * restored because we cannot migrate them.
655  *
656  * This is the last step before we call the device driver callback to allocate
657  * destination memory and copy contents of original page over to new page.
658  */
migrate_vma_unmap(struct migrate_vma * migrate)659 static void migrate_vma_unmap(struct migrate_vma *migrate)
660 {
661 	migrate->cpages = migrate_device_unmap(migrate->src, migrate->npages,
662 					migrate->fault_page);
663 }
664 
665 /**
666  * migrate_vma_setup() - prepare to migrate a range of memory
667  * @args: contains the vma, start, and pfns arrays for the migration
668  *
669  * Returns: negative errno on failures, 0 when 0 or more pages were migrated
670  * without an error.
671  *
672  * Prepare to migrate a range of memory virtual address range by collecting all
673  * the pages backing each virtual address in the range, saving them inside the
674  * src array.  Then lock those pages and unmap them. Once the pages are locked
675  * and unmapped, check whether each page is pinned or not.  Pages that aren't
676  * pinned have the MIGRATE_PFN_MIGRATE flag set (by this function) in the
677  * corresponding src array entry.  Then restores any pages that are pinned, by
678  * remapping and unlocking those pages.
679  *
680  * The caller should then allocate destination memory and copy source memory to
681  * it for all those entries (ie with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE
682  * flag set).  Once these are allocated and copied, the caller must update each
683  * corresponding entry in the dst array with the pfn value of the destination
684  * page and with MIGRATE_PFN_VALID. Destination pages must be locked via
685  * lock_page().
686  *
687  * Note that the caller does not have to migrate all the pages that are marked
688  * with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration from
689  * device memory to system memory.  If the caller cannot migrate a device page
690  * back to system memory, then it must return VM_FAULT_SIGBUS, which has severe
691  * consequences for the userspace process, so it must be avoided if at all
692  * possible.
693  *
694  * For empty entries inside CPU page table (pte_none() or pmd_none() is true) we
695  * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
696  * allowing the caller to allocate device memory for those unbacked virtual
697  * addresses.  For this the caller simply has to allocate device memory and
698  * properly set the destination entry like for regular migration.  Note that
699  * this can still fail, and thus inside the device driver you must check if the
700  * migration was successful for those entries after calling migrate_vma_pages(),
701  * just like for regular migration.
702  *
703  * After that, the callers must call migrate_vma_pages() to go over each entry
704  * in the src array that has the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag
705  * set. If the corresponding entry in dst array has MIGRATE_PFN_VALID flag set,
706  * then migrate_vma_pages() to migrate struct page information from the source
707  * struct page to the destination struct page.  If it fails to migrate the
708  * struct page information, then it clears the MIGRATE_PFN_MIGRATE flag in the
709  * src array.
710  *
711  * At this point all successfully migrated pages have an entry in the src
712  * array with MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set and the dst
713  * array entry with MIGRATE_PFN_VALID flag set.
714  *
715  * Once migrate_vma_pages() returns the caller may inspect which pages were
716  * successfully migrated, and which were not.  Successfully migrated pages will
717  * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
718  *
719  * It is safe to update device page table after migrate_vma_pages() because
720  * both destination and source page are still locked, and the mmap_lock is held
721  * in read mode (hence no one can unmap the range being migrated).
722  *
723  * Once the caller is done cleaning up things and updating its page table (if it
724  * chose to do so, this is not an obligation) it finally calls
725  * migrate_vma_finalize() to update the CPU page table to point to new pages
726  * for successfully migrated pages or otherwise restore the CPU page table to
727  * point to the original source pages.
728  */
migrate_vma_setup(struct migrate_vma * args)729 int migrate_vma_setup(struct migrate_vma *args)
730 {
731 	long nr_pages = (args->end - args->start) >> PAGE_SHIFT;
732 
733 	args->start &= PAGE_MASK;
734 	args->end &= PAGE_MASK;
735 	if (!args->vma || is_vm_hugetlb_page(args->vma) ||
736 	    (args->vma->vm_flags & VM_SPECIAL) || vma_is_dax(args->vma))
737 		return -EINVAL;
738 	if (nr_pages <= 0)
739 		return -EINVAL;
740 	if (args->start < args->vma->vm_start ||
741 	    args->start >= args->vma->vm_end)
742 		return -EINVAL;
743 	if (args->end <= args->vma->vm_start || args->end > args->vma->vm_end)
744 		return -EINVAL;
745 	if (!args->src || !args->dst)
746 		return -EINVAL;
747 	if (args->fault_page && !is_device_private_page(args->fault_page))
748 		return -EINVAL;
749 	if (args->fault_page && !PageLocked(args->fault_page))
750 		return -EINVAL;
751 
752 	memset(args->src, 0, sizeof(*args->src) * nr_pages);
753 	args->cpages = 0;
754 	args->npages = 0;
755 
756 	migrate_vma_collect(args);
757 
758 	if (args->cpages)
759 		migrate_vma_unmap(args);
760 
761 	/*
762 	 * At this point pages are locked and unmapped, and thus they have
763 	 * stable content and can safely be copied to destination memory that
764 	 * is allocated by the drivers.
765 	 */
766 	return 0;
767 
768 }
769 EXPORT_SYMBOL(migrate_vma_setup);
770 
771 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
772 /**
773  * migrate_vma_insert_huge_pmd_page: Insert a huge folio into @migrate->vma->vm_mm
774  * at @addr. folio is already allocated as a part of the migration process with
775  * large page.
776  *
777  * @page needs to be initialized and setup after it's allocated. The code bits
778  * here follow closely the code in __do_huge_pmd_anonymous_page(). This API does
779  * not support THP zero pages.
780  *
781  * @migrate: migrate_vma arguments
782  * @addr: address where the folio will be inserted
783  * @page: page to be inserted at @addr
784  * @src: src pfn which is being migrated
785  * @pmdp: pointer to the pmd
786  */
migrate_vma_insert_huge_pmd_page(struct migrate_vma * migrate,unsigned long addr,struct page * page,unsigned long * src,pmd_t * pmdp)787 static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
788 					 unsigned long addr,
789 					 struct page *page,
790 					 unsigned long *src,
791 					 pmd_t *pmdp)
792 {
793 	struct vm_area_struct *vma = migrate->vma;
794 	gfp_t gfp = vma_thp_gfp_mask(vma);
795 	struct folio *folio = page_folio(page);
796 	int ret;
797 	vm_fault_t csa_ret;
798 	spinlock_t *ptl;
799 	pgtable_t pgtable;
800 	pmd_t entry;
801 	bool flush = false;
802 	unsigned long i;
803 
804 	VM_WARN_ON_FOLIO(!folio, folio);
805 	VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp));
806 
807 	if (!thp_vma_suitable_order(vma, addr, HPAGE_PMD_ORDER))
808 		return -EINVAL;
809 
810 	ret = anon_vma_prepare(vma);
811 	if (ret)
812 		return ret;
813 
814 	folio_set_order(folio, HPAGE_PMD_ORDER);
815 	folio_set_large_rmappable(folio);
816 
817 	if (mem_cgroup_charge(folio, migrate->vma->vm_mm, gfp)) {
818 		count_vm_event(THP_FAULT_FALLBACK);
819 		count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
820 		ret = -ENOMEM;
821 		goto abort;
822 	}
823 
824 	__folio_mark_uptodate(folio);
825 
826 	pgtable = pte_alloc_one(vma->vm_mm);
827 	if (unlikely(!pgtable))
828 		goto abort;
829 
830 	if (folio_is_device_private(folio)) {
831 		swp_entry_t swp_entry;
832 
833 		if (vma->vm_flags & VM_WRITE)
834 			swp_entry = make_writable_device_private_entry(
835 						page_to_pfn(page));
836 		else
837 			swp_entry = make_readable_device_private_entry(
838 						page_to_pfn(page));
839 		entry = swp_entry_to_pmd(swp_entry);
840 	} else {
841 		if (folio_is_zone_device(folio) &&
842 		    !folio_is_device_coherent(folio)) {
843 			goto free_abort;
844 		}
845 		entry = folio_mk_pmd(folio, vma->vm_page_prot);
846 		if (vma->vm_flags & VM_WRITE)
847 			entry = pmd_mkwrite(pmd_mkdirty(entry), vma);
848 	}
849 
850 	ptl = pmd_lock(vma->vm_mm, pmdp);
851 	csa_ret = check_stable_address_space(vma->vm_mm);
852 	if (csa_ret)
853 		goto unlock_abort;
854 
855 	/*
856 	 * Check for userfaultfd but do not deliver the fault. Instead,
857 	 * just back off.
858 	 */
859 	if (userfaultfd_missing(vma))
860 		goto unlock_abort;
861 
862 	if (!pmd_none(*pmdp)) {
863 		if (!is_huge_zero_pmd(*pmdp))
864 			goto unlock_abort;
865 		flush = true;
866 	} else if (!pmd_none(*pmdp))
867 		goto unlock_abort;
868 
869 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
870 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
871 	if (!folio_is_zone_device(folio))
872 		folio_add_lru_vma(folio, vma);
873 	folio_get(folio);
874 
875 	if (flush) {
876 		pte_free(vma->vm_mm, pgtable);
877 		flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
878 		pmdp_invalidate(vma, addr, pmdp);
879 	} else {
880 		pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);
881 		mm_inc_nr_ptes(vma->vm_mm);
882 	}
883 	set_pmd_at(vma->vm_mm, addr, pmdp, entry);
884 	update_mmu_cache_pmd(vma, addr, pmdp);
885 
886 	spin_unlock(ptl);
887 
888 	count_vm_event(THP_FAULT_ALLOC);
889 	count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_ALLOC);
890 	count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
891 
892 	return 0;
893 
894 unlock_abort:
895 	spin_unlock(ptl);
896 free_abort:
897 	pte_free(vma->vm_mm, pgtable);
898 abort:
899 	for (i = 0; i < HPAGE_PMD_NR; i++)
900 		src[i] &= ~MIGRATE_PFN_MIGRATE;
901 	return 0;
902 }
903 
migrate_vma_split_unmapped_folio(struct migrate_vma * migrate,unsigned long idx,unsigned long addr,struct folio * folio)904 static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
905 					    unsigned long idx, unsigned long addr,
906 					    struct folio *folio)
907 {
908 	unsigned long i;
909 	unsigned long pfn;
910 	unsigned long flags;
911 	int ret = 0;
912 
913 	/*
914 	 * take a reference, since split_huge_pmd_address() with freeze = true
915 	 * drops a reference at the end.
916 	 */
917 	folio_get(folio);
918 	split_huge_pmd_address(migrate->vma, addr, true);
919 	ret = folio_split_unmapped(folio, 0);
920 	if (ret)
921 		return ret;
922 	migrate->src[idx] &= ~MIGRATE_PFN_COMPOUND;
923 	flags = migrate->src[idx] & ((1UL << MIGRATE_PFN_SHIFT) - 1);
924 	pfn = migrate->src[idx] >> MIGRATE_PFN_SHIFT;
925 	for (i = 1; i < HPAGE_PMD_NR; i++)
926 		migrate->src[i+idx] = migrate_pfn(pfn + i) | flags;
927 	return ret;
928 }
929 #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
migrate_vma_insert_huge_pmd_page(struct migrate_vma * migrate,unsigned long addr,struct page * page,unsigned long * src,pmd_t * pmdp)930 static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
931 					 unsigned long addr,
932 					 struct page *page,
933 					 unsigned long *src,
934 					 pmd_t *pmdp)
935 {
936 	return 0;
937 }
938 
migrate_vma_split_unmapped_folio(struct migrate_vma * migrate,unsigned long idx,unsigned long addr,struct folio * folio)939 static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
940 					    unsigned long idx, unsigned long addr,
941 					    struct folio *folio)
942 {
943 	return 0;
944 }
945 #endif
946 
migrate_vma_nr_pages(unsigned long * src)947 static unsigned long migrate_vma_nr_pages(unsigned long *src)
948 {
949 	unsigned long nr = 1;
950 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
951 	if (*src & MIGRATE_PFN_COMPOUND)
952 		nr = HPAGE_PMD_NR;
953 #else
954 	if (*src & MIGRATE_PFN_COMPOUND)
955 		VM_WARN_ON_ONCE(true);
956 #endif
957 	return nr;
958 }
959 
960 /*
961  * This code closely matches the code in:
962  *   __handle_mm_fault()
963  *     handle_pte_fault()
964  *       do_anonymous_page()
965  * to map in an anonymous zero page but the struct page will be a ZONE_DEVICE
966  * private or coherent page.
967  */
migrate_vma_insert_page(struct migrate_vma * migrate,unsigned long addr,unsigned long * dst,unsigned long * src)968 static void migrate_vma_insert_page(struct migrate_vma *migrate,
969 				    unsigned long addr,
970 				    unsigned long *dst,
971 				    unsigned long *src)
972 {
973 	struct page *page = migrate_pfn_to_page(*dst);
974 	struct folio *folio = page_folio(page);
975 	struct vm_area_struct *vma = migrate->vma;
976 	struct mm_struct *mm = vma->vm_mm;
977 	bool flush = false;
978 	spinlock_t *ptl;
979 	pte_t entry;
980 	pgd_t *pgdp;
981 	p4d_t *p4dp;
982 	pud_t *pudp;
983 	pmd_t *pmdp;
984 	pte_t *ptep;
985 	pte_t orig_pte;
986 
987 	/* Only allow populating anonymous memory */
988 	if (!vma_is_anonymous(vma))
989 		goto abort;
990 
991 	pgdp = pgd_offset(mm, addr);
992 	p4dp = p4d_alloc(mm, pgdp, addr);
993 	if (!p4dp)
994 		goto abort;
995 	pudp = pud_alloc(mm, p4dp, addr);
996 	if (!pudp)
997 		goto abort;
998 	pmdp = pmd_alloc(mm, pudp, addr);
999 	if (!pmdp)
1000 		goto abort;
1001 
1002 	if (thp_migration_supported() && (*dst & MIGRATE_PFN_COMPOUND)) {
1003 		int ret = migrate_vma_insert_huge_pmd_page(migrate, addr, page,
1004 								src, pmdp);
1005 		if (ret)
1006 			goto abort;
1007 		return;
1008 	}
1009 
1010 	if (!pmd_none(*pmdp)) {
1011 		if (pmd_trans_huge(*pmdp)) {
1012 			if (!is_huge_zero_pmd(*pmdp))
1013 				goto abort;
1014 			split_huge_pmd(vma, pmdp, addr);
1015 		} else if (pmd_leaf(*pmdp))
1016 			goto abort;
1017 	}
1018 
1019 	if (pte_alloc(mm, pmdp))
1020 		goto abort;
1021 	if (unlikely(anon_vma_prepare(vma)))
1022 		goto abort;
1023 	if (mem_cgroup_charge(folio, vma->vm_mm, GFP_KERNEL))
1024 		goto abort;
1025 
1026 	/*
1027 	 * The memory barrier inside __folio_mark_uptodate makes sure that
1028 	 * preceding stores to the folio contents become visible before
1029 	 * the set_pte_at() write.
1030 	 */
1031 	__folio_mark_uptodate(folio);
1032 
1033 	if (folio_is_device_private(folio)) {
1034 		swp_entry_t swp_entry;
1035 
1036 		if (vma->vm_flags & VM_WRITE)
1037 			swp_entry = make_writable_device_private_entry(
1038 						page_to_pfn(page));
1039 		else
1040 			swp_entry = make_readable_device_private_entry(
1041 						page_to_pfn(page));
1042 		entry = swp_entry_to_pte(swp_entry);
1043 	} else {
1044 		if (folio_is_zone_device(folio) &&
1045 		    !folio_is_device_coherent(folio)) {
1046 			pr_warn_once("Unsupported ZONE_DEVICE page type.\n");
1047 			goto abort;
1048 		}
1049 		entry = mk_pte(page, vma->vm_page_prot);
1050 		if (vma->vm_flags & VM_WRITE)
1051 			entry = pte_mkwrite(pte_mkdirty(entry), vma);
1052 	}
1053 
1054 	ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
1055 	if (!ptep)
1056 		goto abort;
1057 	orig_pte = ptep_get(ptep);
1058 
1059 	if (check_stable_address_space(mm))
1060 		goto unlock_abort;
1061 
1062 	if (pte_present(orig_pte)) {
1063 		unsigned long pfn = pte_pfn(orig_pte);
1064 
1065 		if (!is_zero_pfn(pfn))
1066 			goto unlock_abort;
1067 		flush = true;
1068 	} else if (!pte_none(orig_pte))
1069 		goto unlock_abort;
1070 
1071 	/*
1072 	 * Check for userfaultfd but do not deliver the fault. Instead,
1073 	 * just back off.
1074 	 */
1075 	if (userfaultfd_missing(vma))
1076 		goto unlock_abort;
1077 
1078 	inc_mm_counter(mm, MM_ANONPAGES);
1079 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
1080 	if (!folio_is_zone_device(folio))
1081 		folio_add_lru_vma(folio, vma);
1082 	folio_get(folio);
1083 
1084 	if (flush) {
1085 		flush_cache_page(vma, addr, pte_pfn(orig_pte));
1086 		ptep_clear_flush(vma, addr, ptep);
1087 	}
1088 	set_pte_at(mm, addr, ptep, entry);
1089 	update_mmu_cache(vma, addr, ptep);
1090 
1091 	pte_unmap_unlock(ptep, ptl);
1092 	*src = MIGRATE_PFN_MIGRATE;
1093 	return;
1094 
1095 unlock_abort:
1096 	pte_unmap_unlock(ptep, ptl);
1097 abort:
1098 	*src &= ~MIGRATE_PFN_MIGRATE;
1099 }
1100 
__migrate_device_pages(unsigned long * src_pfns,unsigned long * dst_pfns,unsigned long npages,struct migrate_vma * migrate)1101 static void __migrate_device_pages(unsigned long *src_pfns,
1102 				unsigned long *dst_pfns, unsigned long npages,
1103 				struct migrate_vma *migrate)
1104 {
1105 	struct mmu_notifier_range range;
1106 	unsigned long i, j;
1107 	bool notified = false;
1108 	unsigned long addr;
1109 
1110 	for (i = 0; i < npages; ) {
1111 		struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
1112 		struct page *page = migrate_pfn_to_page(src_pfns[i]);
1113 		struct address_space *mapping;
1114 		struct folio *newfolio, *folio;
1115 		int r, extra_cnt = 0;
1116 		unsigned long nr = 1;
1117 
1118 		if (!newpage) {
1119 			src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
1120 			goto next;
1121 		}
1122 
1123 		if (!page) {
1124 			unsigned long addr;
1125 
1126 			if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE))
1127 				goto next;
1128 
1129 			/*
1130 			 * The only time there is no vma is when called from
1131 			 * migrate_device_coherent_folio(). However this isn't
1132 			 * called if the page could not be unmapped.
1133 			 */
1134 			VM_BUG_ON(!migrate);
1135 			addr = migrate->start + i*PAGE_SIZE;
1136 			if (!notified) {
1137 				notified = true;
1138 
1139 				mmu_notifier_range_init_owner(&range,
1140 					MMU_NOTIFY_MIGRATE, 0,
1141 					migrate->vma->vm_mm, addr, migrate->end,
1142 					migrate->pgmap_owner);
1143 				mmu_notifier_invalidate_range_start(&range);
1144 			}
1145 
1146 			if ((src_pfns[i] & MIGRATE_PFN_COMPOUND) &&
1147 				(!(dst_pfns[i] & MIGRATE_PFN_COMPOUND))) {
1148 				nr = migrate_vma_nr_pages(&src_pfns[i]);
1149 				src_pfns[i] &= ~MIGRATE_PFN_COMPOUND;
1150 			} else {
1151 				nr = 1;
1152 			}
1153 
1154 			for (j = 0; j < nr && i + j < npages; j++) {
1155 				src_pfns[i+j] |= MIGRATE_PFN_MIGRATE;
1156 				migrate_vma_insert_page(migrate,
1157 					addr + j * PAGE_SIZE,
1158 					&dst_pfns[i+j], &src_pfns[i+j]);
1159 			}
1160 			goto next;
1161 		}
1162 
1163 		newfolio = page_folio(newpage);
1164 		folio = page_folio(page);
1165 		mapping = folio_mapping(folio);
1166 
1167 		/*
1168 		 * If THP migration is enabled, check if both src and dst
1169 		 * can migrate large pages
1170 		 */
1171 		if (thp_migration_supported()) {
1172 			if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) &&
1173 				(src_pfns[i] & MIGRATE_PFN_COMPOUND) &&
1174 				!(dst_pfns[i] & MIGRATE_PFN_COMPOUND)) {
1175 
1176 				if (!migrate) {
1177 					src_pfns[i] &= ~(MIGRATE_PFN_MIGRATE |
1178 							 MIGRATE_PFN_COMPOUND);
1179 					goto next;
1180 				}
1181 				nr = 1 << folio_order(folio);
1182 				addr = migrate->start + i * PAGE_SIZE;
1183 				if (migrate_vma_split_unmapped_folio(migrate, i, addr, folio)) {
1184 					src_pfns[i] &= ~(MIGRATE_PFN_MIGRATE |
1185 							 MIGRATE_PFN_COMPOUND);
1186 					goto next;
1187 				}
1188 			} else if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) &&
1189 				(dst_pfns[i] & MIGRATE_PFN_COMPOUND) &&
1190 				!(src_pfns[i] & MIGRATE_PFN_COMPOUND)) {
1191 				src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
1192 			}
1193 		}
1194 
1195 
1196 		if (folio_is_device_private(newfolio) ||
1197 		    folio_is_device_coherent(newfolio)) {
1198 			if (mapping) {
1199 				/*
1200 				 * For now only support anonymous memory migrating to
1201 				 * device private or coherent memory.
1202 				 *
1203 				 * Try to get rid of swap cache if possible.
1204 				 */
1205 				if (!folio_test_anon(folio) ||
1206 				    !folio_free_swap(folio)) {
1207 					src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
1208 					goto next;
1209 				}
1210 			}
1211 		} else if (folio_is_zone_device(newfolio)) {
1212 			/*
1213 			 * Other types of ZONE_DEVICE page are not supported.
1214 			 */
1215 			src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
1216 			goto next;
1217 		}
1218 
1219 		BUG_ON(folio_test_writeback(folio));
1220 
1221 		if (migrate && migrate->fault_page == page)
1222 			extra_cnt = 1;
1223 		for (j = 0; j < nr && i + j < npages; j++) {
1224 			folio = page_folio(migrate_pfn_to_page(src_pfns[i+j]));
1225 			newfolio = page_folio(migrate_pfn_to_page(dst_pfns[i+j]));
1226 
1227 			r = folio_migrate_mapping(mapping, newfolio, folio, extra_cnt);
1228 			if (r)
1229 				src_pfns[i+j] &= ~MIGRATE_PFN_MIGRATE;
1230 			else
1231 				folio_migrate_flags(newfolio, folio);
1232 		}
1233 next:
1234 		i += nr;
1235 	}
1236 
1237 	if (notified)
1238 		mmu_notifier_invalidate_range_end(&range);
1239 }
1240 
1241 /**
1242  * migrate_device_pages() - migrate meta-data from src page to dst page
1243  * @src_pfns: src_pfns returned from migrate_device_range()
1244  * @dst_pfns: array of pfns allocated by the driver to migrate memory to
1245  * @npages: number of pages in the range
1246  *
1247  * Equivalent to migrate_vma_pages(). This is called to migrate struct page
1248  * meta-data from source struct page to destination.
1249  */
migrate_device_pages(unsigned long * src_pfns,unsigned long * dst_pfns,unsigned long npages)1250 void migrate_device_pages(unsigned long *src_pfns, unsigned long *dst_pfns,
1251 			unsigned long npages)
1252 {
1253 	__migrate_device_pages(src_pfns, dst_pfns, npages, NULL);
1254 }
1255 EXPORT_SYMBOL(migrate_device_pages);
1256 
1257 /**
1258  * migrate_vma_pages() - migrate meta-data from src page to dst page
1259  * @migrate: migrate struct containing all migration information
1260  *
1261  * This migrates struct page meta-data from source struct page to destination
1262  * struct page. This effectively finishes the migration from source page to the
1263  * destination page.
1264  */
migrate_vma_pages(struct migrate_vma * migrate)1265 void migrate_vma_pages(struct migrate_vma *migrate)
1266 {
1267 	__migrate_device_pages(migrate->src, migrate->dst, migrate->npages, migrate);
1268 }
1269 EXPORT_SYMBOL(migrate_vma_pages);
1270 
__migrate_device_finalize(unsigned long * src_pfns,unsigned long * dst_pfns,unsigned long npages,struct page * fault_page)1271 static void __migrate_device_finalize(unsigned long *src_pfns,
1272 				      unsigned long *dst_pfns,
1273 				      unsigned long npages,
1274 				      struct page *fault_page)
1275 {
1276 	struct folio *fault_folio = fault_page ?
1277 		page_folio(fault_page) : NULL;
1278 	unsigned long i;
1279 
1280 	for (i = 0; i < npages; i++) {
1281 		struct folio *dst = NULL, *src = NULL;
1282 		struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
1283 		struct page *page = migrate_pfn_to_page(src_pfns[i]);
1284 
1285 		if (newpage)
1286 			dst = page_folio(newpage);
1287 
1288 		if (!page) {
1289 			if (dst) {
1290 				WARN_ON_ONCE(fault_folio == dst);
1291 				folio_unlock(dst);
1292 				folio_put(dst);
1293 			}
1294 			continue;
1295 		}
1296 
1297 		src = page_folio(page);
1298 
1299 		if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE) || !dst) {
1300 			if (dst) {
1301 				WARN_ON_ONCE(fault_folio == dst);
1302 				folio_unlock(dst);
1303 				folio_put(dst);
1304 			}
1305 			dst = src;
1306 		}
1307 
1308 		if (!folio_is_zone_device(dst))
1309 			folio_add_lru(dst);
1310 		remove_migration_ptes(src, dst, 0);
1311 		if (fault_folio != src)
1312 			folio_unlock(src);
1313 		folio_put(src);
1314 
1315 		if (dst != src) {
1316 			WARN_ON_ONCE(fault_folio == dst);
1317 			folio_unlock(dst);
1318 			folio_put(dst);
1319 		}
1320 	}
1321 }
1322 
1323 /*
1324  * migrate_device_finalize() - complete page migration
1325  * @src_pfns: src_pfns returned from migrate_device_range()
1326  * @dst_pfns: array of pfns allocated by the driver to migrate memory to
1327  * @npages: number of pages in the range
1328  *
1329  * Completes migration of the page by removing special migration entries.
1330  * Drivers must ensure copying of page data is complete and visible to the CPU
1331  * before calling this.
1332  */
migrate_device_finalize(unsigned long * src_pfns,unsigned long * dst_pfns,unsigned long npages)1333 void migrate_device_finalize(unsigned long *src_pfns,
1334 			     unsigned long *dst_pfns, unsigned long npages)
1335 {
1336 	return __migrate_device_finalize(src_pfns, dst_pfns, npages, NULL);
1337 }
1338 EXPORT_SYMBOL(migrate_device_finalize);
1339 
1340 /**
1341  * migrate_vma_finalize() - restore CPU page table entry
1342  * @migrate: migrate struct containing all migration information
1343  *
1344  * This replaces the special migration pte entry with either a mapping to the
1345  * new page if migration was successful for that page, or to the original page
1346  * otherwise.
1347  *
1348  * This also unlocks the pages and puts them back on the lru, or drops the extra
1349  * refcount, for device pages.
1350  */
migrate_vma_finalize(struct migrate_vma * migrate)1351 void migrate_vma_finalize(struct migrate_vma *migrate)
1352 {
1353 	__migrate_device_finalize(migrate->src, migrate->dst, migrate->npages,
1354 				  migrate->fault_page);
1355 }
1356 EXPORT_SYMBOL(migrate_vma_finalize);
1357 
migrate_device_pfn_lock(unsigned long pfn)1358 static unsigned long migrate_device_pfn_lock(unsigned long pfn)
1359 {
1360 	struct folio *folio;
1361 
1362 	folio = folio_get_nontail_page(pfn_to_page(pfn));
1363 	if (!folio)
1364 		return 0;
1365 
1366 	if (!folio_trylock(folio)) {
1367 		folio_put(folio);
1368 		return 0;
1369 	}
1370 
1371 	return migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
1372 }
1373 
1374 /**
1375  * migrate_device_range() - migrate device private pfns to normal memory.
1376  * @src_pfns: array large enough to hold migrating source device private pfns.
1377  * @start: starting pfn in the range to migrate.
1378  * @npages: number of pages to migrate.
1379  *
1380  * migrate_vma_setup() is similar in concept to migrate_vma_setup() except that
1381  * instead of looking up pages based on virtual address mappings a range of
1382  * device pfns that should be migrated to system memory is used instead.
1383  *
1384  * This is useful when a driver needs to free device memory but doesn't know the
1385  * virtual mappings of every page that may be in device memory. For example this
1386  * is often the case when a driver is being unloaded or unbound from a device.
1387  *
1388  * Like migrate_vma_setup() this function will take a reference and lock any
1389  * migrating pages that aren't free before unmapping them. Drivers may then
1390  * allocate destination pages and start copying data from the device to CPU
1391  * memory before calling migrate_device_pages().
1392  */
migrate_device_range(unsigned long * src_pfns,unsigned long start,unsigned long npages)1393 int migrate_device_range(unsigned long *src_pfns, unsigned long start,
1394 			unsigned long npages)
1395 {
1396 	unsigned long i, j, pfn;
1397 
1398 	for (pfn = start, i = 0; i < npages; pfn++, i++) {
1399 		struct page *page = pfn_to_page(pfn);
1400 		struct folio *folio = page_folio(page);
1401 		unsigned int nr = 1;
1402 
1403 		src_pfns[i] = migrate_device_pfn_lock(pfn);
1404 		nr = folio_nr_pages(folio);
1405 		if (nr > 1) {
1406 			src_pfns[i] |= MIGRATE_PFN_COMPOUND;
1407 			for (j = 1; j < nr; j++)
1408 				src_pfns[i+j] = 0;
1409 			i += j - 1;
1410 			pfn += j - 1;
1411 		}
1412 	}
1413 
1414 	migrate_device_unmap(src_pfns, npages, NULL);
1415 
1416 	return 0;
1417 }
1418 EXPORT_SYMBOL(migrate_device_range);
1419 
1420 /**
1421  * migrate_device_pfns() - migrate device private pfns to normal memory.
1422  * @src_pfns: pre-populated array of source device private pfns to migrate.
1423  * @npages: number of pages to migrate.
1424  *
1425  * Similar to migrate_device_range() but supports non-contiguous pre-populated
1426  * array of device pages to migrate.
1427  */
migrate_device_pfns(unsigned long * src_pfns,unsigned long npages)1428 int migrate_device_pfns(unsigned long *src_pfns, unsigned long npages)
1429 {
1430 	unsigned long i, j;
1431 
1432 	for (i = 0; i < npages; i++) {
1433 		struct page *page = pfn_to_page(src_pfns[i]);
1434 		struct folio *folio = page_folio(page);
1435 		unsigned int nr = 1;
1436 
1437 		src_pfns[i] = migrate_device_pfn_lock(src_pfns[i]);
1438 		nr = folio_nr_pages(folio);
1439 		if (nr > 1) {
1440 			src_pfns[i] |= MIGRATE_PFN_COMPOUND;
1441 			for (j = 1; j < nr; j++)
1442 				src_pfns[i+j] = 0;
1443 			i += j - 1;
1444 		}
1445 	}
1446 
1447 	migrate_device_unmap(src_pfns, npages, NULL);
1448 
1449 	return 0;
1450 }
1451 EXPORT_SYMBOL(migrate_device_pfns);
1452 
1453 /*
1454  * Migrate a device coherent folio back to normal memory. The caller should have
1455  * a reference on folio which will be copied to the new folio if migration is
1456  * successful or dropped on failure.
1457  */
migrate_device_coherent_folio(struct folio * folio)1458 int migrate_device_coherent_folio(struct folio *folio)
1459 {
1460 	unsigned long src_pfn, dst_pfn = 0;
1461 	struct folio *dfolio;
1462 
1463 	WARN_ON_ONCE(folio_test_large(folio));
1464 
1465 	folio_lock(folio);
1466 	src_pfn = migrate_pfn(folio_pfn(folio)) | MIGRATE_PFN_MIGRATE;
1467 
1468 	/*
1469 	 * We don't have a VMA and don't need to walk the page tables to find
1470 	 * the source folio. So call migrate_vma_unmap() directly to unmap the
1471 	 * folio as migrate_vma_setup() will fail if args.vma == NULL.
1472 	 */
1473 	migrate_device_unmap(&src_pfn, 1, NULL);
1474 	if (!(src_pfn & MIGRATE_PFN_MIGRATE))
1475 		return -EBUSY;
1476 
1477 	dfolio = folio_alloc(GFP_USER | __GFP_NOWARN, 0);
1478 	if (dfolio) {
1479 		folio_lock(dfolio);
1480 		dst_pfn = migrate_pfn(folio_pfn(dfolio));
1481 	}
1482 
1483 	migrate_device_pages(&src_pfn, &dst_pfn, 1);
1484 	if (src_pfn & MIGRATE_PFN_MIGRATE)
1485 		folio_copy(dfolio, folio);
1486 	migrate_device_finalize(&src_pfn, &dst_pfn, 1);
1487 
1488 	if (src_pfn & MIGRATE_PFN_MIGRATE)
1489 		return 0;
1490 	return -EBUSY;
1491 }
1492