xref: /linux/mm/migrate.c (revision 297fef494d78d00fa563ead08396da6b4ba58172)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Memory Migration functionality - linux/mm/migrate.c
4  *
5  * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
6  *
7  * Page migration was first developed in the context of the memory hotplug
8  * project. The main authors of the migration code are:
9  *
10  * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
11  * Hirokazu Takahashi <taka@valinux.co.jp>
12  * Dave Hansen <haveblue@us.ibm.com>
13  * Christoph Lameter
14  */
15 
16 #include <linux/migrate.h>
17 #include <linux/export.h>
18 #include <linux/swap.h>
19 #include <linux/swapops.h>
20 #include <linux/pagemap.h>
21 #include <linux/buffer_head.h>
22 #include <linux/mm_inline.h>
23 #include <linux/nsproxy.h>
24 #include <linux/ksm.h>
25 #include <linux/rmap.h>
26 #include <linux/topology.h>
27 #include <linux/cpu.h>
28 #include <linux/cpuset.h>
29 #include <linux/writeback.h>
30 #include <linux/mempolicy.h>
31 #include <linux/vmalloc.h>
32 #include <linux/security.h>
33 #include <linux/backing-dev.h>
34 #include <linux/compaction.h>
35 #include <linux/syscalls.h>
36 #include <linux/compat.h>
37 #include <linux/hugetlb.h>
38 #include <linux/hugetlb_cgroup.h>
39 #include <linux/gfp.h>
40 #include <linux/pfn_t.h>
41 #include <linux/memremap.h>
42 #include <linux/userfaultfd_k.h>
43 #include <linux/balloon_compaction.h>
44 #include <linux/page_idle.h>
45 #include <linux/page_owner.h>
46 #include <linux/sched/mm.h>
47 #include <linux/ptrace.h>
48 #include <linux/oom.h>
49 #include <linux/memory.h>
50 #include <linux/random.h>
51 #include <linux/sched/sysctl.h>
52 #include <linux/memory-tiers.h>
53 
54 #include <asm/tlbflush.h>
55 
56 #include <trace/events/migrate.h>
57 
58 #include "internal.h"
59 
60 bool isolate_movable_page(struct page *page, isolate_mode_t mode)
61 {
62 	struct folio *folio = folio_get_nontail_page(page);
63 	const struct movable_operations *mops;
64 
65 	/*
66 	 * Avoid burning cycles with pages that are yet under __free_pages(),
67 	 * or just got freed under us.
68 	 *
69 	 * In case we 'win' a race for a movable page being freed under us and
70 	 * raise its refcount preventing __free_pages() from doing its job
71 	 * the put_page() at the end of this block will take care of
72 	 * release this page, thus avoiding a nasty leakage.
73 	 */
74 	if (!folio)
75 		goto out;
76 
77 	if (unlikely(folio_test_slab(folio)))
78 		goto out_putfolio;
79 	/* Pairs with smp_wmb() in slab freeing, e.g. SLUB's __free_slab() */
80 	smp_rmb();
81 	/*
82 	 * Check movable flag before taking the page lock because
83 	 * we use non-atomic bitops on newly allocated page flags so
84 	 * unconditionally grabbing the lock ruins page's owner side.
85 	 */
86 	if (unlikely(!__folio_test_movable(folio)))
87 		goto out_putfolio;
88 	/* Pairs with smp_wmb() in slab allocation, e.g. SLUB's alloc_slab_page() */
89 	smp_rmb();
90 	if (unlikely(folio_test_slab(folio)))
91 		goto out_putfolio;
92 
93 	/*
94 	 * As movable pages are not isolated from LRU lists, concurrent
95 	 * compaction threads can race against page migration functions
96 	 * as well as race against the releasing a page.
97 	 *
98 	 * In order to avoid having an already isolated movable page
99 	 * being (wrongly) re-isolated while it is under migration,
100 	 * or to avoid attempting to isolate pages being released,
101 	 * lets be sure we have the page lock
102 	 * before proceeding with the movable page isolation steps.
103 	 */
104 	if (unlikely(!folio_trylock(folio)))
105 		goto out_putfolio;
106 
107 	if (!folio_test_movable(folio) || folio_test_isolated(folio))
108 		goto out_no_isolated;
109 
110 	mops = folio_movable_ops(folio);
111 	VM_BUG_ON_FOLIO(!mops, folio);
112 
113 	if (!mops->isolate_page(&folio->page, mode))
114 		goto out_no_isolated;
115 
116 	/* Driver shouldn't use the isolated flag */
117 	WARN_ON_ONCE(folio_test_isolated(folio));
118 	folio_set_isolated(folio);
119 	folio_unlock(folio);
120 
121 	return true;
122 
123 out_no_isolated:
124 	folio_unlock(folio);
125 out_putfolio:
126 	folio_put(folio);
127 out:
128 	return false;
129 }
130 
131 static void putback_movable_folio(struct folio *folio)
132 {
133 	const struct movable_operations *mops = folio_movable_ops(folio);
134 
135 	mops->putback_page(&folio->page);
136 	folio_clear_isolated(folio);
137 }
138 
139 /*
140  * Put previously isolated pages back onto the appropriate lists
141  * from where they were once taken off for compaction/migration.
142  *
143  * This function shall be used whenever the isolated pageset has been
144  * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
145  * and isolate_hugetlb().
146  */
147 void putback_movable_pages(struct list_head *l)
148 {
149 	struct folio *folio;
150 	struct folio *folio2;
151 
152 	list_for_each_entry_safe(folio, folio2, l, lru) {
153 		if (unlikely(folio_test_hugetlb(folio))) {
154 			folio_putback_active_hugetlb(folio);
155 			continue;
156 		}
157 		list_del(&folio->lru);
158 		/*
159 		 * We isolated non-lru movable folio so here we can use
160 		 * __folio_test_movable because LRU folio's mapping cannot
161 		 * have PAGE_MAPPING_MOVABLE.
162 		 */
163 		if (unlikely(__folio_test_movable(folio))) {
164 			VM_BUG_ON_FOLIO(!folio_test_isolated(folio), folio);
165 			folio_lock(folio);
166 			if (folio_test_movable(folio))
167 				putback_movable_folio(folio);
168 			else
169 				folio_clear_isolated(folio);
170 			folio_unlock(folio);
171 			folio_put(folio);
172 		} else {
173 			node_stat_mod_folio(folio, NR_ISOLATED_ANON +
174 					folio_is_file_lru(folio), -folio_nr_pages(folio));
175 			folio_putback_lru(folio);
176 		}
177 	}
178 }
179 
180 /*
181  * Restore a potential migration pte to a working pte entry
182  */
183 static bool remove_migration_pte(struct folio *folio,
184 		struct vm_area_struct *vma, unsigned long addr, void *old)
185 {
186 	DEFINE_FOLIO_VMA_WALK(pvmw, old, vma, addr, PVMW_SYNC | PVMW_MIGRATION);
187 
188 	while (page_vma_mapped_walk(&pvmw)) {
189 		rmap_t rmap_flags = RMAP_NONE;
190 		pte_t old_pte;
191 		pte_t pte;
192 		swp_entry_t entry;
193 		struct page *new;
194 		unsigned long idx = 0;
195 
196 		/* pgoff is invalid for ksm pages, but they are never large */
197 		if (folio_test_large(folio) && !folio_test_hugetlb(folio))
198 			idx = linear_page_index(vma, pvmw.address) - pvmw.pgoff;
199 		new = folio_page(folio, idx);
200 
201 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
202 		/* PMD-mapped THP migration entry */
203 		if (!pvmw.pte) {
204 			VM_BUG_ON_FOLIO(folio_test_hugetlb(folio) ||
205 					!folio_test_pmd_mappable(folio), folio);
206 			remove_migration_pmd(&pvmw, new);
207 			continue;
208 		}
209 #endif
210 
211 		folio_get(folio);
212 		pte = mk_pte(new, READ_ONCE(vma->vm_page_prot));
213 		old_pte = ptep_get(pvmw.pte);
214 
215 		entry = pte_to_swp_entry(old_pte);
216 		if (!is_migration_entry_young(entry))
217 			pte = pte_mkold(pte);
218 		if (folio_test_dirty(folio) && is_migration_entry_dirty(entry))
219 			pte = pte_mkdirty(pte);
220 		if (pte_swp_soft_dirty(old_pte))
221 			pte = pte_mksoft_dirty(pte);
222 		else
223 			pte = pte_clear_soft_dirty(pte);
224 
225 		if (is_writable_migration_entry(entry))
226 			pte = pte_mkwrite(pte, vma);
227 		else if (pte_swp_uffd_wp(old_pte))
228 			pte = pte_mkuffd_wp(pte);
229 
230 		if (folio_test_anon(folio) && !is_readable_migration_entry(entry))
231 			rmap_flags |= RMAP_EXCLUSIVE;
232 
233 		if (unlikely(is_device_private_page(new))) {
234 			if (pte_write(pte))
235 				entry = make_writable_device_private_entry(
236 							page_to_pfn(new));
237 			else
238 				entry = make_readable_device_private_entry(
239 							page_to_pfn(new));
240 			pte = swp_entry_to_pte(entry);
241 			if (pte_swp_soft_dirty(old_pte))
242 				pte = pte_swp_mksoft_dirty(pte);
243 			if (pte_swp_uffd_wp(old_pte))
244 				pte = pte_swp_mkuffd_wp(pte);
245 		}
246 
247 #ifdef CONFIG_HUGETLB_PAGE
248 		if (folio_test_hugetlb(folio)) {
249 			struct hstate *h = hstate_vma(vma);
250 			unsigned int shift = huge_page_shift(h);
251 			unsigned long psize = huge_page_size(h);
252 
253 			pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
254 			if (folio_test_anon(folio))
255 				hugetlb_add_anon_rmap(folio, vma, pvmw.address,
256 						      rmap_flags);
257 			else
258 				hugetlb_add_file_rmap(folio);
259 			set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte,
260 					psize);
261 		} else
262 #endif
263 		{
264 			if (folio_test_anon(folio))
265 				folio_add_anon_rmap_pte(folio, new, vma,
266 							pvmw.address, rmap_flags);
267 			else
268 				folio_add_file_rmap_pte(folio, new, vma);
269 			set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
270 		}
271 		if (vma->vm_flags & VM_LOCKED)
272 			mlock_drain_local();
273 
274 		trace_remove_migration_pte(pvmw.address, pte_val(pte),
275 					   compound_order(new));
276 
277 		/* No need to invalidate - it was non-present before */
278 		update_mmu_cache(vma, pvmw.address, pvmw.pte);
279 	}
280 
281 	return true;
282 }
283 
284 /*
285  * Get rid of all migration entries and replace them by
286  * references to the indicated page.
287  */
288 void remove_migration_ptes(struct folio *src, struct folio *dst, bool locked)
289 {
290 	struct rmap_walk_control rwc = {
291 		.rmap_one = remove_migration_pte,
292 		.arg = src,
293 	};
294 
295 	if (locked)
296 		rmap_walk_locked(dst, &rwc);
297 	else
298 		rmap_walk(dst, &rwc);
299 }
300 
301 /*
302  * Something used the pte of a page under migration. We need to
303  * get to the page and wait until migration is finished.
304  * When we return from this function the fault will be retried.
305  */
306 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
307 			  unsigned long address)
308 {
309 	spinlock_t *ptl;
310 	pte_t *ptep;
311 	pte_t pte;
312 	swp_entry_t entry;
313 
314 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
315 	if (!ptep)
316 		return;
317 
318 	pte = ptep_get(ptep);
319 	pte_unmap(ptep);
320 
321 	if (!is_swap_pte(pte))
322 		goto out;
323 
324 	entry = pte_to_swp_entry(pte);
325 	if (!is_migration_entry(entry))
326 		goto out;
327 
328 	migration_entry_wait_on_locked(entry, ptl);
329 	return;
330 out:
331 	spin_unlock(ptl);
332 }
333 
334 #ifdef CONFIG_HUGETLB_PAGE
335 /*
336  * The vma read lock must be held upon entry. Holding that lock prevents either
337  * the pte or the ptl from being freed.
338  *
339  * This function will release the vma lock before returning.
340  */
341 void migration_entry_wait_huge(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
342 {
343 	spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), vma->vm_mm, ptep);
344 	pte_t pte;
345 
346 	hugetlb_vma_assert_locked(vma);
347 	spin_lock(ptl);
348 	pte = huge_ptep_get(vma->vm_mm, addr, ptep);
349 
350 	if (unlikely(!is_hugetlb_entry_migration(pte))) {
351 		spin_unlock(ptl);
352 		hugetlb_vma_unlock_read(vma);
353 	} else {
354 		/*
355 		 * If migration entry existed, safe to release vma lock
356 		 * here because the pgtable page won't be freed without the
357 		 * pgtable lock released.  See comment right above pgtable
358 		 * lock release in migration_entry_wait_on_locked().
359 		 */
360 		hugetlb_vma_unlock_read(vma);
361 		migration_entry_wait_on_locked(pte_to_swp_entry(pte), ptl);
362 	}
363 }
364 #endif
365 
366 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
367 void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
368 {
369 	spinlock_t *ptl;
370 
371 	ptl = pmd_lock(mm, pmd);
372 	if (!is_pmd_migration_entry(*pmd))
373 		goto unlock;
374 	migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd), ptl);
375 	return;
376 unlock:
377 	spin_unlock(ptl);
378 }
379 #endif
380 
381 static int folio_expected_refs(struct address_space *mapping,
382 		struct folio *folio)
383 {
384 	int refs = 1;
385 	if (!mapping)
386 		return refs;
387 
388 	refs += folio_nr_pages(folio);
389 	if (folio_test_private(folio))
390 		refs++;
391 
392 	return refs;
393 }
394 
395 /*
396  * Replace the folio in the mapping.
397  *
398  * The number of remaining references must be:
399  * 1 for anonymous folios without a mapping
400  * 2 for folios with a mapping
401  * 3 for folios with a mapping and PagePrivate/PagePrivate2 set.
402  */
403 static int __folio_migrate_mapping(struct address_space *mapping,
404 		struct folio *newfolio, struct folio *folio, int expected_count)
405 {
406 	XA_STATE(xas, &mapping->i_pages, folio_index(folio));
407 	struct zone *oldzone, *newzone;
408 	int dirty;
409 	long nr = folio_nr_pages(folio);
410 	long entries, i;
411 
412 	if (!mapping) {
413 		/* Take off deferred split queue while frozen and memcg set */
414 		if (folio_test_large(folio) &&
415 		    folio_test_large_rmappable(folio)) {
416 			if (!folio_ref_freeze(folio, expected_count))
417 				return -EAGAIN;
418 			folio_undo_large_rmappable(folio);
419 			folio_ref_unfreeze(folio, expected_count);
420 		}
421 
422 		/* No turning back from here */
423 		newfolio->index = folio->index;
424 		newfolio->mapping = folio->mapping;
425 		if (folio_test_swapbacked(folio))
426 			__folio_set_swapbacked(newfolio);
427 
428 		return MIGRATEPAGE_SUCCESS;
429 	}
430 
431 	oldzone = folio_zone(folio);
432 	newzone = folio_zone(newfolio);
433 
434 	xas_lock_irq(&xas);
435 	if (!folio_ref_freeze(folio, expected_count)) {
436 		xas_unlock_irq(&xas);
437 		return -EAGAIN;
438 	}
439 
440 	/* Take off deferred split queue while frozen and memcg set */
441 	folio_undo_large_rmappable(folio);
442 
443 	/*
444 	 * Now we know that no one else is looking at the folio:
445 	 * no turning back from here.
446 	 */
447 	newfolio->index = folio->index;
448 	newfolio->mapping = folio->mapping;
449 	folio_ref_add(newfolio, nr); /* add cache reference */
450 	if (folio_test_swapbacked(folio)) {
451 		__folio_set_swapbacked(newfolio);
452 		if (folio_test_swapcache(folio)) {
453 			folio_set_swapcache(newfolio);
454 			newfolio->private = folio_get_private(folio);
455 		}
456 		entries = nr;
457 	} else {
458 		VM_BUG_ON_FOLIO(folio_test_swapcache(folio), folio);
459 		entries = 1;
460 	}
461 
462 	/* Move dirty while folio refs frozen and newfolio not yet exposed */
463 	dirty = folio_test_dirty(folio);
464 	if (dirty) {
465 		folio_clear_dirty(folio);
466 		folio_set_dirty(newfolio);
467 	}
468 
469 	/* Swap cache still stores N entries instead of a high-order entry */
470 	for (i = 0; i < entries; i++) {
471 		xas_store(&xas, newfolio);
472 		xas_next(&xas);
473 	}
474 
475 	/*
476 	 * Drop cache reference from old folio by unfreezing
477 	 * to one less reference.
478 	 * We know this isn't the last reference.
479 	 */
480 	folio_ref_unfreeze(folio, expected_count - nr);
481 
482 	xas_unlock(&xas);
483 	/* Leave irq disabled to prevent preemption while updating stats */
484 
485 	/*
486 	 * If moved to a different zone then also account
487 	 * the folio for that zone. Other VM counters will be
488 	 * taken care of when we establish references to the
489 	 * new folio and drop references to the old folio.
490 	 *
491 	 * Note that anonymous folios are accounted for
492 	 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
493 	 * are mapped to swap space.
494 	 */
495 	if (newzone != oldzone) {
496 		struct lruvec *old_lruvec, *new_lruvec;
497 		struct mem_cgroup *memcg;
498 
499 		memcg = folio_memcg(folio);
500 		old_lruvec = mem_cgroup_lruvec(memcg, oldzone->zone_pgdat);
501 		new_lruvec = mem_cgroup_lruvec(memcg, newzone->zone_pgdat);
502 
503 		__mod_lruvec_state(old_lruvec, NR_FILE_PAGES, -nr);
504 		__mod_lruvec_state(new_lruvec, NR_FILE_PAGES, nr);
505 		if (folio_test_swapbacked(folio) && !folio_test_swapcache(folio)) {
506 			__mod_lruvec_state(old_lruvec, NR_SHMEM, -nr);
507 			__mod_lruvec_state(new_lruvec, NR_SHMEM, nr);
508 
509 			if (folio_test_pmd_mappable(folio)) {
510 				__mod_lruvec_state(old_lruvec, NR_SHMEM_THPS, -nr);
511 				__mod_lruvec_state(new_lruvec, NR_SHMEM_THPS, nr);
512 			}
513 		}
514 #ifdef CONFIG_SWAP
515 		if (folio_test_swapcache(folio)) {
516 			__mod_lruvec_state(old_lruvec, NR_SWAPCACHE, -nr);
517 			__mod_lruvec_state(new_lruvec, NR_SWAPCACHE, nr);
518 		}
519 #endif
520 		if (dirty && mapping_can_writeback(mapping)) {
521 			__mod_lruvec_state(old_lruvec, NR_FILE_DIRTY, -nr);
522 			__mod_zone_page_state(oldzone, NR_ZONE_WRITE_PENDING, -nr);
523 			__mod_lruvec_state(new_lruvec, NR_FILE_DIRTY, nr);
524 			__mod_zone_page_state(newzone, NR_ZONE_WRITE_PENDING, nr);
525 		}
526 	}
527 	local_irq_enable();
528 
529 	return MIGRATEPAGE_SUCCESS;
530 }
531 
532 int folio_migrate_mapping(struct address_space *mapping,
533 		struct folio *newfolio, struct folio *folio, int extra_count)
534 {
535 	int expected_count = folio_expected_refs(mapping, folio) + extra_count;
536 
537 	if (folio_ref_count(folio) != expected_count)
538 		return -EAGAIN;
539 
540 	return __folio_migrate_mapping(mapping, newfolio, folio, expected_count);
541 }
542 EXPORT_SYMBOL(folio_migrate_mapping);
543 
544 /*
545  * The expected number of remaining references is the same as that
546  * of folio_migrate_mapping().
547  */
548 int migrate_huge_page_move_mapping(struct address_space *mapping,
549 				   struct folio *dst, struct folio *src)
550 {
551 	XA_STATE(xas, &mapping->i_pages, folio_index(src));
552 	int rc, expected_count = folio_expected_refs(mapping, src);
553 
554 	if (folio_ref_count(src) != expected_count)
555 		return -EAGAIN;
556 
557 	rc = folio_mc_copy(dst, src);
558 	if (unlikely(rc))
559 		return rc;
560 
561 	xas_lock_irq(&xas);
562 	if (!folio_ref_freeze(src, expected_count)) {
563 		xas_unlock_irq(&xas);
564 		return -EAGAIN;
565 	}
566 
567 	dst->index = src->index;
568 	dst->mapping = src->mapping;
569 
570 	folio_ref_add(dst, folio_nr_pages(dst));
571 
572 	xas_store(&xas, dst);
573 
574 	folio_ref_unfreeze(src, expected_count - folio_nr_pages(src));
575 
576 	xas_unlock_irq(&xas);
577 
578 	return MIGRATEPAGE_SUCCESS;
579 }
580 
581 /*
582  * Copy the flags and some other ancillary information
583  */
584 void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
585 {
586 	int cpupid;
587 
588 	if (folio_test_error(folio))
589 		folio_set_error(newfolio);
590 	if (folio_test_referenced(folio))
591 		folio_set_referenced(newfolio);
592 	if (folio_test_uptodate(folio))
593 		folio_mark_uptodate(newfolio);
594 	if (folio_test_clear_active(folio)) {
595 		VM_BUG_ON_FOLIO(folio_test_unevictable(folio), folio);
596 		folio_set_active(newfolio);
597 	} else if (folio_test_clear_unevictable(folio))
598 		folio_set_unevictable(newfolio);
599 	if (folio_test_workingset(folio))
600 		folio_set_workingset(newfolio);
601 	if (folio_test_checked(folio))
602 		folio_set_checked(newfolio);
603 	/*
604 	 * PG_anon_exclusive (-> PG_mappedtodisk) is always migrated via
605 	 * migration entries. We can still have PG_anon_exclusive set on an
606 	 * effectively unmapped and unreferenced first sub-pages of an
607 	 * anonymous THP: we can simply copy it here via PG_mappedtodisk.
608 	 */
609 	if (folio_test_mappedtodisk(folio))
610 		folio_set_mappedtodisk(newfolio);
611 
612 	/* Move dirty on pages not done by folio_migrate_mapping() */
613 	if (folio_test_dirty(folio))
614 		folio_set_dirty(newfolio);
615 
616 	if (folio_test_young(folio))
617 		folio_set_young(newfolio);
618 	if (folio_test_idle(folio))
619 		folio_set_idle(newfolio);
620 
621 	/*
622 	 * Copy NUMA information to the new page, to prevent over-eager
623 	 * future migrations of this same page.
624 	 */
625 	cpupid = folio_xchg_last_cpupid(folio, -1);
626 	/*
627 	 * For memory tiering mode, when migrate between slow and fast
628 	 * memory node, reset cpupid, because that is used to record
629 	 * page access time in slow memory node.
630 	 */
631 	if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) {
632 		bool f_toptier = node_is_toptier(folio_nid(folio));
633 		bool t_toptier = node_is_toptier(folio_nid(newfolio));
634 
635 		if (f_toptier != t_toptier)
636 			cpupid = -1;
637 	}
638 	folio_xchg_last_cpupid(newfolio, cpupid);
639 
640 	folio_migrate_ksm(newfolio, folio);
641 	/*
642 	 * Please do not reorder this without considering how mm/ksm.c's
643 	 * ksm_get_folio() depends upon ksm_migrate_page() and PageSwapCache().
644 	 */
645 	if (folio_test_swapcache(folio))
646 		folio_clear_swapcache(folio);
647 	folio_clear_private(folio);
648 
649 	/* page->private contains hugetlb specific flags */
650 	if (!folio_test_hugetlb(folio))
651 		folio->private = NULL;
652 
653 	/*
654 	 * If any waiters have accumulated on the new page then
655 	 * wake them up.
656 	 */
657 	if (folio_test_writeback(newfolio))
658 		folio_end_writeback(newfolio);
659 
660 	/*
661 	 * PG_readahead shares the same bit with PG_reclaim.  The above
662 	 * end_page_writeback() may clear PG_readahead mistakenly, so set the
663 	 * bit after that.
664 	 */
665 	if (folio_test_readahead(folio))
666 		folio_set_readahead(newfolio);
667 
668 	folio_copy_owner(newfolio, folio);
669 
670 	mem_cgroup_migrate(folio, newfolio);
671 }
672 EXPORT_SYMBOL(folio_migrate_flags);
673 
674 /************************************************************
675  *                    Migration functions
676  ***********************************************************/
677 
678 static int __migrate_folio(struct address_space *mapping, struct folio *dst,
679 			   struct folio *src, void *src_private,
680 			   enum migrate_mode mode)
681 {
682 	int rc, expected_count = folio_expected_refs(mapping, src);
683 
684 	/* Check whether src does not have extra refs before we do more work */
685 	if (folio_ref_count(src) != expected_count)
686 		return -EAGAIN;
687 
688 	rc = folio_mc_copy(dst, src);
689 	if (unlikely(rc))
690 		return rc;
691 
692 	rc = __folio_migrate_mapping(mapping, dst, src, expected_count);
693 	if (rc != MIGRATEPAGE_SUCCESS)
694 		return rc;
695 
696 	if (src_private)
697 		folio_attach_private(dst, folio_detach_private(src));
698 
699 	folio_migrate_flags(dst, src);
700 	return MIGRATEPAGE_SUCCESS;
701 }
702 
703 /**
704  * migrate_folio() - Simple folio migration.
705  * @mapping: The address_space containing the folio.
706  * @dst: The folio to migrate the data to.
707  * @src: The folio containing the current data.
708  * @mode: How to migrate the page.
709  *
710  * Common logic to directly migrate a single LRU folio suitable for
711  * folios that do not use PagePrivate/PagePrivate2.
712  *
713  * Folios are locked upon entry and exit.
714  */
715 int migrate_folio(struct address_space *mapping, struct folio *dst,
716 		  struct folio *src, enum migrate_mode mode)
717 {
718 	BUG_ON(folio_test_writeback(src));	/* Writeback must be complete */
719 	return __migrate_folio(mapping, dst, src, NULL, mode);
720 }
721 EXPORT_SYMBOL(migrate_folio);
722 
723 #ifdef CONFIG_BUFFER_HEAD
724 /* Returns true if all buffers are successfully locked */
725 static bool buffer_migrate_lock_buffers(struct buffer_head *head,
726 							enum migrate_mode mode)
727 {
728 	struct buffer_head *bh = head;
729 	struct buffer_head *failed_bh;
730 
731 	do {
732 		if (!trylock_buffer(bh)) {
733 			if (mode == MIGRATE_ASYNC)
734 				goto unlock;
735 			if (mode == MIGRATE_SYNC_LIGHT && !buffer_uptodate(bh))
736 				goto unlock;
737 			lock_buffer(bh);
738 		}
739 
740 		bh = bh->b_this_page;
741 	} while (bh != head);
742 
743 	return true;
744 
745 unlock:
746 	/* We failed to lock the buffer and cannot stall. */
747 	failed_bh = bh;
748 	bh = head;
749 	while (bh != failed_bh) {
750 		unlock_buffer(bh);
751 		bh = bh->b_this_page;
752 	}
753 
754 	return false;
755 }
756 
757 static int __buffer_migrate_folio(struct address_space *mapping,
758 		struct folio *dst, struct folio *src, enum migrate_mode mode,
759 		bool check_refs)
760 {
761 	struct buffer_head *bh, *head;
762 	int rc;
763 	int expected_count;
764 
765 	head = folio_buffers(src);
766 	if (!head)
767 		return migrate_folio(mapping, dst, src, mode);
768 
769 	/* Check whether page does not have extra refs before we do more work */
770 	expected_count = folio_expected_refs(mapping, src);
771 	if (folio_ref_count(src) != expected_count)
772 		return -EAGAIN;
773 
774 	if (!buffer_migrate_lock_buffers(head, mode))
775 		return -EAGAIN;
776 
777 	if (check_refs) {
778 		bool busy;
779 		bool invalidated = false;
780 
781 recheck_buffers:
782 		busy = false;
783 		spin_lock(&mapping->i_private_lock);
784 		bh = head;
785 		do {
786 			if (atomic_read(&bh->b_count)) {
787 				busy = true;
788 				break;
789 			}
790 			bh = bh->b_this_page;
791 		} while (bh != head);
792 		if (busy) {
793 			if (invalidated) {
794 				rc = -EAGAIN;
795 				goto unlock_buffers;
796 			}
797 			spin_unlock(&mapping->i_private_lock);
798 			invalidate_bh_lrus();
799 			invalidated = true;
800 			goto recheck_buffers;
801 		}
802 	}
803 
804 	rc = filemap_migrate_folio(mapping, dst, src, mode);
805 	if (rc != MIGRATEPAGE_SUCCESS)
806 		goto unlock_buffers;
807 
808 	bh = head;
809 	do {
810 		folio_set_bh(bh, dst, bh_offset(bh));
811 		bh = bh->b_this_page;
812 	} while (bh != head);
813 
814 unlock_buffers:
815 	if (check_refs)
816 		spin_unlock(&mapping->i_private_lock);
817 	bh = head;
818 	do {
819 		unlock_buffer(bh);
820 		bh = bh->b_this_page;
821 	} while (bh != head);
822 
823 	return rc;
824 }
825 
826 /**
827  * buffer_migrate_folio() - Migration function for folios with buffers.
828  * @mapping: The address space containing @src.
829  * @dst: The folio to migrate to.
830  * @src: The folio to migrate from.
831  * @mode: How to migrate the folio.
832  *
833  * This function can only be used if the underlying filesystem guarantees
834  * that no other references to @src exist. For example attached buffer
835  * heads are accessed only under the folio lock.  If your filesystem cannot
836  * provide this guarantee, buffer_migrate_folio_norefs() may be more
837  * appropriate.
838  *
839  * Return: 0 on success or a negative errno on failure.
840  */
841 int buffer_migrate_folio(struct address_space *mapping,
842 		struct folio *dst, struct folio *src, enum migrate_mode mode)
843 {
844 	return __buffer_migrate_folio(mapping, dst, src, mode, false);
845 }
846 EXPORT_SYMBOL(buffer_migrate_folio);
847 
848 /**
849  * buffer_migrate_folio_norefs() - Migration function for folios with buffers.
850  * @mapping: The address space containing @src.
851  * @dst: The folio to migrate to.
852  * @src: The folio to migrate from.
853  * @mode: How to migrate the folio.
854  *
855  * Like buffer_migrate_folio() except that this variant is more careful
856  * and checks that there are also no buffer head references. This function
857  * is the right one for mappings where buffer heads are directly looked
858  * up and referenced (such as block device mappings).
859  *
860  * Return: 0 on success or a negative errno on failure.
861  */
862 int buffer_migrate_folio_norefs(struct address_space *mapping,
863 		struct folio *dst, struct folio *src, enum migrate_mode mode)
864 {
865 	return __buffer_migrate_folio(mapping, dst, src, mode, true);
866 }
867 EXPORT_SYMBOL_GPL(buffer_migrate_folio_norefs);
868 #endif /* CONFIG_BUFFER_HEAD */
869 
870 int filemap_migrate_folio(struct address_space *mapping,
871 		struct folio *dst, struct folio *src, enum migrate_mode mode)
872 {
873 	return __migrate_folio(mapping, dst, src, folio_get_private(src), mode);
874 }
875 EXPORT_SYMBOL_GPL(filemap_migrate_folio);
876 
877 /*
878  * Writeback a folio to clean the dirty state
879  */
880 static int writeout(struct address_space *mapping, struct folio *folio)
881 {
882 	struct writeback_control wbc = {
883 		.sync_mode = WB_SYNC_NONE,
884 		.nr_to_write = 1,
885 		.range_start = 0,
886 		.range_end = LLONG_MAX,
887 		.for_reclaim = 1
888 	};
889 	int rc;
890 
891 	if (!mapping->a_ops->writepage)
892 		/* No write method for the address space */
893 		return -EINVAL;
894 
895 	if (!folio_clear_dirty_for_io(folio))
896 		/* Someone else already triggered a write */
897 		return -EAGAIN;
898 
899 	/*
900 	 * A dirty folio may imply that the underlying filesystem has
901 	 * the folio on some queue. So the folio must be clean for
902 	 * migration. Writeout may mean we lose the lock and the
903 	 * folio state is no longer what we checked for earlier.
904 	 * At this point we know that the migration attempt cannot
905 	 * be successful.
906 	 */
907 	remove_migration_ptes(folio, folio, false);
908 
909 	rc = mapping->a_ops->writepage(&folio->page, &wbc);
910 
911 	if (rc != AOP_WRITEPAGE_ACTIVATE)
912 		/* unlocked. Relock */
913 		folio_lock(folio);
914 
915 	return (rc < 0) ? -EIO : -EAGAIN;
916 }
917 
918 /*
919  * Default handling if a filesystem does not provide a migration function.
920  */
921 static int fallback_migrate_folio(struct address_space *mapping,
922 		struct folio *dst, struct folio *src, enum migrate_mode mode)
923 {
924 	if (folio_test_dirty(src)) {
925 		/* Only writeback folios in full synchronous migration */
926 		switch (mode) {
927 		case MIGRATE_SYNC:
928 			break;
929 		default:
930 			return -EBUSY;
931 		}
932 		return writeout(mapping, src);
933 	}
934 
935 	/*
936 	 * Buffers may be managed in a filesystem specific way.
937 	 * We must have no buffers or drop them.
938 	 */
939 	if (!filemap_release_folio(src, GFP_KERNEL))
940 		return mode == MIGRATE_SYNC ? -EAGAIN : -EBUSY;
941 
942 	return migrate_folio(mapping, dst, src, mode);
943 }
944 
945 /*
946  * Move a page to a newly allocated page
947  * The page is locked and all ptes have been successfully removed.
948  *
949  * The new page will have replaced the old page if this function
950  * is successful.
951  *
952  * Return value:
953  *   < 0 - error code
954  *  MIGRATEPAGE_SUCCESS - success
955  */
956 static int move_to_new_folio(struct folio *dst, struct folio *src,
957 				enum migrate_mode mode)
958 {
959 	int rc = -EAGAIN;
960 	bool is_lru = !__folio_test_movable(src);
961 
962 	VM_BUG_ON_FOLIO(!folio_test_locked(src), src);
963 	VM_BUG_ON_FOLIO(!folio_test_locked(dst), dst);
964 
965 	if (likely(is_lru)) {
966 		struct address_space *mapping = folio_mapping(src);
967 
968 		if (!mapping)
969 			rc = migrate_folio(mapping, dst, src, mode);
970 		else if (mapping_inaccessible(mapping))
971 			rc = -EOPNOTSUPP;
972 		else if (mapping->a_ops->migrate_folio)
973 			/*
974 			 * Most folios have a mapping and most filesystems
975 			 * provide a migrate_folio callback. Anonymous folios
976 			 * are part of swap space which also has its own
977 			 * migrate_folio callback. This is the most common path
978 			 * for page migration.
979 			 */
980 			rc = mapping->a_ops->migrate_folio(mapping, dst, src,
981 								mode);
982 		else
983 			rc = fallback_migrate_folio(mapping, dst, src, mode);
984 	} else {
985 		const struct movable_operations *mops;
986 
987 		/*
988 		 * In case of non-lru page, it could be released after
989 		 * isolation step. In that case, we shouldn't try migration.
990 		 */
991 		VM_BUG_ON_FOLIO(!folio_test_isolated(src), src);
992 		if (!folio_test_movable(src)) {
993 			rc = MIGRATEPAGE_SUCCESS;
994 			folio_clear_isolated(src);
995 			goto out;
996 		}
997 
998 		mops = folio_movable_ops(src);
999 		rc = mops->migrate_page(&dst->page, &src->page, mode);
1000 		WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
1001 				!folio_test_isolated(src));
1002 	}
1003 
1004 	/*
1005 	 * When successful, old pagecache src->mapping must be cleared before
1006 	 * src is freed; but stats require that PageAnon be left as PageAnon.
1007 	 */
1008 	if (rc == MIGRATEPAGE_SUCCESS) {
1009 		if (__folio_test_movable(src)) {
1010 			VM_BUG_ON_FOLIO(!folio_test_isolated(src), src);
1011 
1012 			/*
1013 			 * We clear PG_movable under page_lock so any compactor
1014 			 * cannot try to migrate this page.
1015 			 */
1016 			folio_clear_isolated(src);
1017 		}
1018 
1019 		/*
1020 		 * Anonymous and movable src->mapping will be cleared by
1021 		 * free_pages_prepare so don't reset it here for keeping
1022 		 * the type to work PageAnon, for example.
1023 		 */
1024 		if (!folio_mapping_flags(src))
1025 			src->mapping = NULL;
1026 
1027 		if (likely(!folio_is_zone_device(dst)))
1028 			flush_dcache_folio(dst);
1029 	}
1030 out:
1031 	return rc;
1032 }
1033 
1034 /*
1035  * To record some information during migration, we use unused private
1036  * field of struct folio of the newly allocated destination folio.
1037  * This is safe because nobody is using it except us.
1038  */
1039 enum {
1040 	PAGE_WAS_MAPPED = BIT(0),
1041 	PAGE_WAS_MLOCKED = BIT(1),
1042 	PAGE_OLD_STATES = PAGE_WAS_MAPPED | PAGE_WAS_MLOCKED,
1043 };
1044 
1045 static void __migrate_folio_record(struct folio *dst,
1046 				   int old_page_state,
1047 				   struct anon_vma *anon_vma)
1048 {
1049 	dst->private = (void *)anon_vma + old_page_state;
1050 }
1051 
1052 static void __migrate_folio_extract(struct folio *dst,
1053 				   int *old_page_state,
1054 				   struct anon_vma **anon_vmap)
1055 {
1056 	unsigned long private = (unsigned long)dst->private;
1057 
1058 	*anon_vmap = (struct anon_vma *)(private & ~PAGE_OLD_STATES);
1059 	*old_page_state = private & PAGE_OLD_STATES;
1060 	dst->private = NULL;
1061 }
1062 
1063 /* Restore the source folio to the original state upon failure */
1064 static void migrate_folio_undo_src(struct folio *src,
1065 				   int page_was_mapped,
1066 				   struct anon_vma *anon_vma,
1067 				   bool locked,
1068 				   struct list_head *ret)
1069 {
1070 	if (page_was_mapped)
1071 		remove_migration_ptes(src, src, false);
1072 	/* Drop an anon_vma reference if we took one */
1073 	if (anon_vma)
1074 		put_anon_vma(anon_vma);
1075 	if (locked)
1076 		folio_unlock(src);
1077 	if (ret)
1078 		list_move_tail(&src->lru, ret);
1079 }
1080 
1081 /* Restore the destination folio to the original state upon failure */
1082 static void migrate_folio_undo_dst(struct folio *dst, bool locked,
1083 		free_folio_t put_new_folio, unsigned long private)
1084 {
1085 	if (locked)
1086 		folio_unlock(dst);
1087 	if (put_new_folio)
1088 		put_new_folio(dst, private);
1089 	else
1090 		folio_put(dst);
1091 }
1092 
1093 /* Cleanup src folio upon migration success */
1094 static void migrate_folio_done(struct folio *src,
1095 			       enum migrate_reason reason)
1096 {
1097 	/*
1098 	 * Compaction can migrate also non-LRU pages which are
1099 	 * not accounted to NR_ISOLATED_*. They can be recognized
1100 	 * as __folio_test_movable
1101 	 */
1102 	if (likely(!__folio_test_movable(src)))
1103 		mod_node_page_state(folio_pgdat(src), NR_ISOLATED_ANON +
1104 				    folio_is_file_lru(src), -folio_nr_pages(src));
1105 
1106 	if (reason != MR_MEMORY_FAILURE)
1107 		/* We release the page in page_handle_poison. */
1108 		folio_put(src);
1109 }
1110 
1111 /* Obtain the lock on page, remove all ptes. */
1112 static int migrate_folio_unmap(new_folio_t get_new_folio,
1113 		free_folio_t put_new_folio, unsigned long private,
1114 		struct folio *src, struct folio **dstp, enum migrate_mode mode,
1115 		enum migrate_reason reason, struct list_head *ret)
1116 {
1117 	struct folio *dst;
1118 	int rc = -EAGAIN;
1119 	int old_page_state = 0;
1120 	struct anon_vma *anon_vma = NULL;
1121 	bool is_lru = !__folio_test_movable(src);
1122 	bool locked = false;
1123 	bool dst_locked = false;
1124 
1125 	if (folio_ref_count(src) == 1) {
1126 		/* Folio was freed from under us. So we are done. */
1127 		folio_clear_active(src);
1128 		folio_clear_unevictable(src);
1129 		/* free_pages_prepare() will clear PG_isolated. */
1130 		list_del(&src->lru);
1131 		migrate_folio_done(src, reason);
1132 		return MIGRATEPAGE_SUCCESS;
1133 	}
1134 
1135 	dst = get_new_folio(src, private);
1136 	if (!dst)
1137 		return -ENOMEM;
1138 	*dstp = dst;
1139 
1140 	dst->private = NULL;
1141 
1142 	if (!folio_trylock(src)) {
1143 		if (mode == MIGRATE_ASYNC)
1144 			goto out;
1145 
1146 		/*
1147 		 * It's not safe for direct compaction to call lock_page.
1148 		 * For example, during page readahead pages are added locked
1149 		 * to the LRU. Later, when the IO completes the pages are
1150 		 * marked uptodate and unlocked. However, the queueing
1151 		 * could be merging multiple pages for one bio (e.g.
1152 		 * mpage_readahead). If an allocation happens for the
1153 		 * second or third page, the process can end up locking
1154 		 * the same page twice and deadlocking. Rather than
1155 		 * trying to be clever about what pages can be locked,
1156 		 * avoid the use of lock_page for direct compaction
1157 		 * altogether.
1158 		 */
1159 		if (current->flags & PF_MEMALLOC)
1160 			goto out;
1161 
1162 		/*
1163 		 * In "light" mode, we can wait for transient locks (eg
1164 		 * inserting a page into the page table), but it's not
1165 		 * worth waiting for I/O.
1166 		 */
1167 		if (mode == MIGRATE_SYNC_LIGHT && !folio_test_uptodate(src))
1168 			goto out;
1169 
1170 		folio_lock(src);
1171 	}
1172 	locked = true;
1173 	if (folio_test_mlocked(src))
1174 		old_page_state |= PAGE_WAS_MLOCKED;
1175 
1176 	if (folio_test_writeback(src)) {
1177 		/*
1178 		 * Only in the case of a full synchronous migration is it
1179 		 * necessary to wait for PageWriteback. In the async case,
1180 		 * the retry loop is too short and in the sync-light case,
1181 		 * the overhead of stalling is too much
1182 		 */
1183 		switch (mode) {
1184 		case MIGRATE_SYNC:
1185 			break;
1186 		default:
1187 			rc = -EBUSY;
1188 			goto out;
1189 		}
1190 		folio_wait_writeback(src);
1191 	}
1192 
1193 	/*
1194 	 * By try_to_migrate(), src->mapcount goes down to 0 here. In this case,
1195 	 * we cannot notice that anon_vma is freed while we migrate a page.
1196 	 * This get_anon_vma() delays freeing anon_vma pointer until the end
1197 	 * of migration. File cache pages are no problem because of page_lock()
1198 	 * File Caches may use write_page() or lock_page() in migration, then,
1199 	 * just care Anon page here.
1200 	 *
1201 	 * Only folio_get_anon_vma() understands the subtleties of
1202 	 * getting a hold on an anon_vma from outside one of its mms.
1203 	 * But if we cannot get anon_vma, then we won't need it anyway,
1204 	 * because that implies that the anon page is no longer mapped
1205 	 * (and cannot be remapped so long as we hold the page lock).
1206 	 */
1207 	if (folio_test_anon(src) && !folio_test_ksm(src))
1208 		anon_vma = folio_get_anon_vma(src);
1209 
1210 	/*
1211 	 * Block others from accessing the new page when we get around to
1212 	 * establishing additional references. We are usually the only one
1213 	 * holding a reference to dst at this point. We used to have a BUG
1214 	 * here if folio_trylock(dst) fails, but would like to allow for
1215 	 * cases where there might be a race with the previous use of dst.
1216 	 * This is much like races on refcount of oldpage: just don't BUG().
1217 	 */
1218 	if (unlikely(!folio_trylock(dst)))
1219 		goto out;
1220 	dst_locked = true;
1221 
1222 	if (unlikely(!is_lru)) {
1223 		__migrate_folio_record(dst, old_page_state, anon_vma);
1224 		return MIGRATEPAGE_UNMAP;
1225 	}
1226 
1227 	/*
1228 	 * Corner case handling:
1229 	 * 1. When a new swap-cache page is read into, it is added to the LRU
1230 	 * and treated as swapcache but it has no rmap yet.
1231 	 * Calling try_to_unmap() against a src->mapping==NULL page will
1232 	 * trigger a BUG.  So handle it here.
1233 	 * 2. An orphaned page (see truncate_cleanup_page) might have
1234 	 * fs-private metadata. The page can be picked up due to memory
1235 	 * offlining.  Everywhere else except page reclaim, the page is
1236 	 * invisible to the vm, so the page can not be migrated.  So try to
1237 	 * free the metadata, so the page can be freed.
1238 	 */
1239 	if (!src->mapping) {
1240 		if (folio_test_private(src)) {
1241 			try_to_free_buffers(src);
1242 			goto out;
1243 		}
1244 	} else if (folio_mapped(src)) {
1245 		/* Establish migration ptes */
1246 		VM_BUG_ON_FOLIO(folio_test_anon(src) &&
1247 			       !folio_test_ksm(src) && !anon_vma, src);
1248 		try_to_migrate(src, mode == MIGRATE_ASYNC ? TTU_BATCH_FLUSH : 0);
1249 		old_page_state |= PAGE_WAS_MAPPED;
1250 	}
1251 
1252 	if (!folio_mapped(src)) {
1253 		__migrate_folio_record(dst, old_page_state, anon_vma);
1254 		return MIGRATEPAGE_UNMAP;
1255 	}
1256 
1257 out:
1258 	/*
1259 	 * A folio that has not been unmapped will be restored to
1260 	 * right list unless we want to retry.
1261 	 */
1262 	if (rc == -EAGAIN)
1263 		ret = NULL;
1264 
1265 	migrate_folio_undo_src(src, old_page_state & PAGE_WAS_MAPPED,
1266 			       anon_vma, locked, ret);
1267 	migrate_folio_undo_dst(dst, dst_locked, put_new_folio, private);
1268 
1269 	return rc;
1270 }
1271 
1272 /* Migrate the folio to the newly allocated folio in dst. */
1273 static int migrate_folio_move(free_folio_t put_new_folio, unsigned long private,
1274 			      struct folio *src, struct folio *dst,
1275 			      enum migrate_mode mode, enum migrate_reason reason,
1276 			      struct list_head *ret)
1277 {
1278 	int rc;
1279 	int old_page_state = 0;
1280 	struct anon_vma *anon_vma = NULL;
1281 	bool is_lru = !__folio_test_movable(src);
1282 	struct list_head *prev;
1283 
1284 	__migrate_folio_extract(dst, &old_page_state, &anon_vma);
1285 	prev = dst->lru.prev;
1286 	list_del(&dst->lru);
1287 
1288 	rc = move_to_new_folio(dst, src, mode);
1289 	if (rc)
1290 		goto out;
1291 
1292 	if (unlikely(!is_lru))
1293 		goto out_unlock_both;
1294 
1295 	/*
1296 	 * When successful, push dst to LRU immediately: so that if it
1297 	 * turns out to be an mlocked page, remove_migration_ptes() will
1298 	 * automatically build up the correct dst->mlock_count for it.
1299 	 *
1300 	 * We would like to do something similar for the old page, when
1301 	 * unsuccessful, and other cases when a page has been temporarily
1302 	 * isolated from the unevictable LRU: but this case is the easiest.
1303 	 */
1304 	folio_add_lru(dst);
1305 	if (old_page_state & PAGE_WAS_MLOCKED)
1306 		lru_add_drain();
1307 
1308 	if (old_page_state & PAGE_WAS_MAPPED)
1309 		remove_migration_ptes(src, dst, false);
1310 
1311 out_unlock_both:
1312 	folio_unlock(dst);
1313 	set_page_owner_migrate_reason(&dst->page, reason);
1314 	/*
1315 	 * If migration is successful, decrease refcount of dst,
1316 	 * which will not free the page because new page owner increased
1317 	 * refcounter.
1318 	 */
1319 	folio_put(dst);
1320 
1321 	/*
1322 	 * A folio that has been migrated has all references removed
1323 	 * and will be freed.
1324 	 */
1325 	list_del(&src->lru);
1326 	/* Drop an anon_vma reference if we took one */
1327 	if (anon_vma)
1328 		put_anon_vma(anon_vma);
1329 	folio_unlock(src);
1330 	migrate_folio_done(src, reason);
1331 
1332 	return rc;
1333 out:
1334 	/*
1335 	 * A folio that has not been migrated will be restored to
1336 	 * right list unless we want to retry.
1337 	 */
1338 	if (rc == -EAGAIN) {
1339 		list_add(&dst->lru, prev);
1340 		__migrate_folio_record(dst, old_page_state, anon_vma);
1341 		return rc;
1342 	}
1343 
1344 	migrate_folio_undo_src(src, old_page_state & PAGE_WAS_MAPPED,
1345 			       anon_vma, true, ret);
1346 	migrate_folio_undo_dst(dst, true, put_new_folio, private);
1347 
1348 	return rc;
1349 }
1350 
1351 /*
1352  * Counterpart of unmap_and_move_page() for hugepage migration.
1353  *
1354  * This function doesn't wait the completion of hugepage I/O
1355  * because there is no race between I/O and migration for hugepage.
1356  * Note that currently hugepage I/O occurs only in direct I/O
1357  * where no lock is held and PG_writeback is irrelevant,
1358  * and writeback status of all subpages are counted in the reference
1359  * count of the head page (i.e. if all subpages of a 2MB hugepage are
1360  * under direct I/O, the reference of the head page is 512 and a bit more.)
1361  * This means that when we try to migrate hugepage whose subpages are
1362  * doing direct I/O, some references remain after try_to_unmap() and
1363  * hugepage migration fails without data corruption.
1364  *
1365  * There is also no race when direct I/O is issued on the page under migration,
1366  * because then pte is replaced with migration swap entry and direct I/O code
1367  * will wait in the page fault for migration to complete.
1368  */
1369 static int unmap_and_move_huge_page(new_folio_t get_new_folio,
1370 		free_folio_t put_new_folio, unsigned long private,
1371 		struct folio *src, int force, enum migrate_mode mode,
1372 		int reason, struct list_head *ret)
1373 {
1374 	struct folio *dst;
1375 	int rc = -EAGAIN;
1376 	int page_was_mapped = 0;
1377 	struct anon_vma *anon_vma = NULL;
1378 	struct address_space *mapping = NULL;
1379 
1380 	if (folio_ref_count(src) == 1) {
1381 		/* page was freed from under us. So we are done. */
1382 		folio_putback_active_hugetlb(src);
1383 		return MIGRATEPAGE_SUCCESS;
1384 	}
1385 
1386 	dst = get_new_folio(src, private);
1387 	if (!dst)
1388 		return -ENOMEM;
1389 
1390 	if (!folio_trylock(src)) {
1391 		if (!force)
1392 			goto out;
1393 		switch (mode) {
1394 		case MIGRATE_SYNC:
1395 			break;
1396 		default:
1397 			goto out;
1398 		}
1399 		folio_lock(src);
1400 	}
1401 
1402 	/*
1403 	 * Check for pages which are in the process of being freed.  Without
1404 	 * folio_mapping() set, hugetlbfs specific move page routine will not
1405 	 * be called and we could leak usage counts for subpools.
1406 	 */
1407 	if (hugetlb_folio_subpool(src) && !folio_mapping(src)) {
1408 		rc = -EBUSY;
1409 		goto out_unlock;
1410 	}
1411 
1412 	if (folio_test_anon(src))
1413 		anon_vma = folio_get_anon_vma(src);
1414 
1415 	if (unlikely(!folio_trylock(dst)))
1416 		goto put_anon;
1417 
1418 	if (folio_mapped(src)) {
1419 		enum ttu_flags ttu = 0;
1420 
1421 		if (!folio_test_anon(src)) {
1422 			/*
1423 			 * In shared mappings, try_to_unmap could potentially
1424 			 * call huge_pmd_unshare.  Because of this, take
1425 			 * semaphore in write mode here and set TTU_RMAP_LOCKED
1426 			 * to let lower levels know we have taken the lock.
1427 			 */
1428 			mapping = hugetlb_folio_mapping_lock_write(src);
1429 			if (unlikely(!mapping))
1430 				goto unlock_put_anon;
1431 
1432 			ttu = TTU_RMAP_LOCKED;
1433 		}
1434 
1435 		try_to_migrate(src, ttu);
1436 		page_was_mapped = 1;
1437 
1438 		if (ttu & TTU_RMAP_LOCKED)
1439 			i_mmap_unlock_write(mapping);
1440 	}
1441 
1442 	if (!folio_mapped(src))
1443 		rc = move_to_new_folio(dst, src, mode);
1444 
1445 	if (page_was_mapped)
1446 		remove_migration_ptes(src,
1447 			rc == MIGRATEPAGE_SUCCESS ? dst : src, false);
1448 
1449 unlock_put_anon:
1450 	folio_unlock(dst);
1451 
1452 put_anon:
1453 	if (anon_vma)
1454 		put_anon_vma(anon_vma);
1455 
1456 	if (rc == MIGRATEPAGE_SUCCESS) {
1457 		move_hugetlb_state(src, dst, reason);
1458 		put_new_folio = NULL;
1459 	}
1460 
1461 out_unlock:
1462 	folio_unlock(src);
1463 out:
1464 	if (rc == MIGRATEPAGE_SUCCESS)
1465 		folio_putback_active_hugetlb(src);
1466 	else if (rc != -EAGAIN)
1467 		list_move_tail(&src->lru, ret);
1468 
1469 	/*
1470 	 * If migration was not successful and there's a freeing callback, use
1471 	 * it.  Otherwise, put_page() will drop the reference grabbed during
1472 	 * isolation.
1473 	 */
1474 	if (put_new_folio)
1475 		put_new_folio(dst, private);
1476 	else
1477 		folio_putback_active_hugetlb(dst);
1478 
1479 	return rc;
1480 }
1481 
1482 static inline int try_split_folio(struct folio *folio, struct list_head *split_folios)
1483 {
1484 	int rc;
1485 
1486 	folio_lock(folio);
1487 	rc = split_folio_to_list(folio, split_folios);
1488 	folio_unlock(folio);
1489 	if (!rc)
1490 		list_move_tail(&folio->lru, split_folios);
1491 
1492 	return rc;
1493 }
1494 
1495 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1496 #define NR_MAX_BATCHED_MIGRATION	HPAGE_PMD_NR
1497 #else
1498 #define NR_MAX_BATCHED_MIGRATION	512
1499 #endif
1500 #define NR_MAX_MIGRATE_PAGES_RETRY	10
1501 #define NR_MAX_MIGRATE_ASYNC_RETRY	3
1502 #define NR_MAX_MIGRATE_SYNC_RETRY					\
1503 	(NR_MAX_MIGRATE_PAGES_RETRY - NR_MAX_MIGRATE_ASYNC_RETRY)
1504 
1505 struct migrate_pages_stats {
1506 	int nr_succeeded;	/* Normal and large folios migrated successfully, in
1507 				   units of base pages */
1508 	int nr_failed_pages;	/* Normal and large folios failed to be migrated, in
1509 				   units of base pages.  Untried folios aren't counted */
1510 	int nr_thp_succeeded;	/* THP migrated successfully */
1511 	int nr_thp_failed;	/* THP failed to be migrated */
1512 	int nr_thp_split;	/* THP split before migrating */
1513 	int nr_split;	/* Large folio (include THP) split before migrating */
1514 };
1515 
1516 /*
1517  * Returns the number of hugetlb folios that were not migrated, or an error code
1518  * after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are movable
1519  * any more because the list has become empty or no retryable hugetlb folios
1520  * exist any more. It is caller's responsibility to call putback_movable_pages()
1521  * only if ret != 0.
1522  */
1523 static int migrate_hugetlbs(struct list_head *from, new_folio_t get_new_folio,
1524 			    free_folio_t put_new_folio, unsigned long private,
1525 			    enum migrate_mode mode, int reason,
1526 			    struct migrate_pages_stats *stats,
1527 			    struct list_head *ret_folios)
1528 {
1529 	int retry = 1;
1530 	int nr_failed = 0;
1531 	int nr_retry_pages = 0;
1532 	int pass = 0;
1533 	struct folio *folio, *folio2;
1534 	int rc, nr_pages;
1535 
1536 	for (pass = 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) {
1537 		retry = 0;
1538 		nr_retry_pages = 0;
1539 
1540 		list_for_each_entry_safe(folio, folio2, from, lru) {
1541 			if (!folio_test_hugetlb(folio))
1542 				continue;
1543 
1544 			nr_pages = folio_nr_pages(folio);
1545 
1546 			cond_resched();
1547 
1548 			/*
1549 			 * Migratability of hugepages depends on architectures and
1550 			 * their size.  This check is necessary because some callers
1551 			 * of hugepage migration like soft offline and memory
1552 			 * hotremove don't walk through page tables or check whether
1553 			 * the hugepage is pmd-based or not before kicking migration.
1554 			 */
1555 			if (!hugepage_migration_supported(folio_hstate(folio))) {
1556 				nr_failed++;
1557 				stats->nr_failed_pages += nr_pages;
1558 				list_move_tail(&folio->lru, ret_folios);
1559 				continue;
1560 			}
1561 
1562 			rc = unmap_and_move_huge_page(get_new_folio,
1563 						      put_new_folio, private,
1564 						      folio, pass > 2, mode,
1565 						      reason, ret_folios);
1566 			/*
1567 			 * The rules are:
1568 			 *	Success: hugetlb folio will be put back
1569 			 *	-EAGAIN: stay on the from list
1570 			 *	-ENOMEM: stay on the from list
1571 			 *	Other errno: put on ret_folios list
1572 			 */
1573 			switch(rc) {
1574 			case -ENOMEM:
1575 				/*
1576 				 * When memory is low, don't bother to try to migrate
1577 				 * other folios, just exit.
1578 				 */
1579 				stats->nr_failed_pages += nr_pages + nr_retry_pages;
1580 				return -ENOMEM;
1581 			case -EAGAIN:
1582 				retry++;
1583 				nr_retry_pages += nr_pages;
1584 				break;
1585 			case MIGRATEPAGE_SUCCESS:
1586 				stats->nr_succeeded += nr_pages;
1587 				break;
1588 			default:
1589 				/*
1590 				 * Permanent failure (-EBUSY, etc.):
1591 				 * unlike -EAGAIN case, the failed folio is
1592 				 * removed from migration folio list and not
1593 				 * retried in the next outer loop.
1594 				 */
1595 				nr_failed++;
1596 				stats->nr_failed_pages += nr_pages;
1597 				break;
1598 			}
1599 		}
1600 	}
1601 	/*
1602 	 * nr_failed is number of hugetlb folios failed to be migrated.  After
1603 	 * NR_MAX_MIGRATE_PAGES_RETRY attempts, give up and count retried hugetlb
1604 	 * folios as failed.
1605 	 */
1606 	nr_failed += retry;
1607 	stats->nr_failed_pages += nr_retry_pages;
1608 
1609 	return nr_failed;
1610 }
1611 
1612 /*
1613  * migrate_pages_batch() first unmaps folios in the from list as many as
1614  * possible, then move the unmapped folios.
1615  *
1616  * We only batch migration if mode == MIGRATE_ASYNC to avoid to wait a
1617  * lock or bit when we have locked more than one folio.  Which may cause
1618  * deadlock (e.g., for loop device).  So, if mode != MIGRATE_ASYNC, the
1619  * length of the from list must be <= 1.
1620  */
1621 static int migrate_pages_batch(struct list_head *from,
1622 		new_folio_t get_new_folio, free_folio_t put_new_folio,
1623 		unsigned long private, enum migrate_mode mode, int reason,
1624 		struct list_head *ret_folios, struct list_head *split_folios,
1625 		struct migrate_pages_stats *stats, int nr_pass)
1626 {
1627 	int retry = 1;
1628 	int thp_retry = 1;
1629 	int nr_failed = 0;
1630 	int nr_retry_pages = 0;
1631 	int pass = 0;
1632 	bool is_thp = false;
1633 	bool is_large = false;
1634 	struct folio *folio, *folio2, *dst = NULL, *dst2;
1635 	int rc, rc_saved = 0, nr_pages;
1636 	LIST_HEAD(unmap_folios);
1637 	LIST_HEAD(dst_folios);
1638 	bool nosplit = (reason == MR_NUMA_MISPLACED);
1639 
1640 	VM_WARN_ON_ONCE(mode != MIGRATE_ASYNC &&
1641 			!list_empty(from) && !list_is_singular(from));
1642 
1643 	for (pass = 0; pass < nr_pass && retry; pass++) {
1644 		retry = 0;
1645 		thp_retry = 0;
1646 		nr_retry_pages = 0;
1647 
1648 		list_for_each_entry_safe(folio, folio2, from, lru) {
1649 			is_large = folio_test_large(folio);
1650 			is_thp = is_large && folio_test_pmd_mappable(folio);
1651 			nr_pages = folio_nr_pages(folio);
1652 
1653 			cond_resched();
1654 
1655 			/*
1656 			 * The rare folio on the deferred split list should
1657 			 * be split now. It should not count as a failure:
1658 			 * but increment nr_failed because, without doing so,
1659 			 * migrate_pages() may report success with (split but
1660 			 * unmigrated) pages still on its fromlist; whereas it
1661 			 * always reports success when its fromlist is empty.
1662 			 * stats->nr_thp_failed should be increased too,
1663 			 * otherwise stats inconsistency will happen when
1664 			 * migrate_pages_batch is called via migrate_pages()
1665 			 * with MIGRATE_SYNC and MIGRATE_ASYNC.
1666 			 *
1667 			 * Only check it without removing it from the list.
1668 			 * Since the folio can be on deferred_split_scan()
1669 			 * local list and removing it can cause the local list
1670 			 * corruption. Folio split process below can handle it
1671 			 * with the help of folio_ref_freeze().
1672 			 *
1673 			 * nr_pages > 2 is needed to avoid checking order-1
1674 			 * page cache folios. They exist, in contrast to
1675 			 * non-existent order-1 anonymous folios, and do not
1676 			 * use _deferred_list.
1677 			 */
1678 			if (nr_pages > 2 &&
1679 			   !list_empty(&folio->_deferred_list)) {
1680 				if (try_split_folio(folio, split_folios) == 0) {
1681 					nr_failed++;
1682 					stats->nr_thp_failed += is_thp;
1683 					stats->nr_thp_split += is_thp;
1684 					stats->nr_split++;
1685 					continue;
1686 				}
1687 			}
1688 
1689 			/*
1690 			 * Large folio migration might be unsupported or
1691 			 * the allocation might be failed so we should retry
1692 			 * on the same folio with the large folio split
1693 			 * to normal folios.
1694 			 *
1695 			 * Split folios are put in split_folios, and
1696 			 * we will migrate them after the rest of the
1697 			 * list is processed.
1698 			 */
1699 			if (!thp_migration_supported() && is_thp) {
1700 				nr_failed++;
1701 				stats->nr_thp_failed++;
1702 				if (!try_split_folio(folio, split_folios)) {
1703 					stats->nr_thp_split++;
1704 					stats->nr_split++;
1705 					continue;
1706 				}
1707 				stats->nr_failed_pages += nr_pages;
1708 				list_move_tail(&folio->lru, ret_folios);
1709 				continue;
1710 			}
1711 
1712 			rc = migrate_folio_unmap(get_new_folio, put_new_folio,
1713 					private, folio, &dst, mode, reason,
1714 					ret_folios);
1715 			/*
1716 			 * The rules are:
1717 			 *	Success: folio will be freed
1718 			 *	Unmap: folio will be put on unmap_folios list,
1719 			 *	       dst folio put on dst_folios list
1720 			 *	-EAGAIN: stay on the from list
1721 			 *	-ENOMEM: stay on the from list
1722 			 *	Other errno: put on ret_folios list
1723 			 */
1724 			switch(rc) {
1725 			case -ENOMEM:
1726 				/*
1727 				 * When memory is low, don't bother to try to migrate
1728 				 * other folios, move unmapped folios, then exit.
1729 				 */
1730 				nr_failed++;
1731 				stats->nr_thp_failed += is_thp;
1732 				/* Large folio NUMA faulting doesn't split to retry. */
1733 				if (is_large && !nosplit) {
1734 					int ret = try_split_folio(folio, split_folios);
1735 
1736 					if (!ret) {
1737 						stats->nr_thp_split += is_thp;
1738 						stats->nr_split++;
1739 						break;
1740 					} else if (reason == MR_LONGTERM_PIN &&
1741 						   ret == -EAGAIN) {
1742 						/*
1743 						 * Try again to split large folio to
1744 						 * mitigate the failure of longterm pinning.
1745 						 */
1746 						retry++;
1747 						thp_retry += is_thp;
1748 						nr_retry_pages += nr_pages;
1749 						/* Undo duplicated failure counting. */
1750 						nr_failed--;
1751 						stats->nr_thp_failed -= is_thp;
1752 						break;
1753 					}
1754 				}
1755 
1756 				stats->nr_failed_pages += nr_pages + nr_retry_pages;
1757 				/* nr_failed isn't updated for not used */
1758 				stats->nr_thp_failed += thp_retry;
1759 				rc_saved = rc;
1760 				if (list_empty(&unmap_folios))
1761 					goto out;
1762 				else
1763 					goto move;
1764 			case -EAGAIN:
1765 				retry++;
1766 				thp_retry += is_thp;
1767 				nr_retry_pages += nr_pages;
1768 				break;
1769 			case MIGRATEPAGE_SUCCESS:
1770 				stats->nr_succeeded += nr_pages;
1771 				stats->nr_thp_succeeded += is_thp;
1772 				break;
1773 			case MIGRATEPAGE_UNMAP:
1774 				list_move_tail(&folio->lru, &unmap_folios);
1775 				list_add_tail(&dst->lru, &dst_folios);
1776 				break;
1777 			default:
1778 				/*
1779 				 * Permanent failure (-EBUSY, etc.):
1780 				 * unlike -EAGAIN case, the failed folio is
1781 				 * removed from migration folio list and not
1782 				 * retried in the next outer loop.
1783 				 */
1784 				nr_failed++;
1785 				stats->nr_thp_failed += is_thp;
1786 				stats->nr_failed_pages += nr_pages;
1787 				break;
1788 			}
1789 		}
1790 	}
1791 	nr_failed += retry;
1792 	stats->nr_thp_failed += thp_retry;
1793 	stats->nr_failed_pages += nr_retry_pages;
1794 move:
1795 	/* Flush TLBs for all unmapped folios */
1796 	try_to_unmap_flush();
1797 
1798 	retry = 1;
1799 	for (pass = 0; pass < nr_pass && retry; pass++) {
1800 		retry = 0;
1801 		thp_retry = 0;
1802 		nr_retry_pages = 0;
1803 
1804 		dst = list_first_entry(&dst_folios, struct folio, lru);
1805 		dst2 = list_next_entry(dst, lru);
1806 		list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) {
1807 			is_thp = folio_test_large(folio) && folio_test_pmd_mappable(folio);
1808 			nr_pages = folio_nr_pages(folio);
1809 
1810 			cond_resched();
1811 
1812 			rc = migrate_folio_move(put_new_folio, private,
1813 						folio, dst, mode,
1814 						reason, ret_folios);
1815 			/*
1816 			 * The rules are:
1817 			 *	Success: folio will be freed
1818 			 *	-EAGAIN: stay on the unmap_folios list
1819 			 *	Other errno: put on ret_folios list
1820 			 */
1821 			switch(rc) {
1822 			case -EAGAIN:
1823 				retry++;
1824 				thp_retry += is_thp;
1825 				nr_retry_pages += nr_pages;
1826 				break;
1827 			case MIGRATEPAGE_SUCCESS:
1828 				stats->nr_succeeded += nr_pages;
1829 				stats->nr_thp_succeeded += is_thp;
1830 				break;
1831 			default:
1832 				nr_failed++;
1833 				stats->nr_thp_failed += is_thp;
1834 				stats->nr_failed_pages += nr_pages;
1835 				break;
1836 			}
1837 			dst = dst2;
1838 			dst2 = list_next_entry(dst, lru);
1839 		}
1840 	}
1841 	nr_failed += retry;
1842 	stats->nr_thp_failed += thp_retry;
1843 	stats->nr_failed_pages += nr_retry_pages;
1844 
1845 	rc = rc_saved ? : nr_failed;
1846 out:
1847 	/* Cleanup remaining folios */
1848 	dst = list_first_entry(&dst_folios, struct folio, lru);
1849 	dst2 = list_next_entry(dst, lru);
1850 	list_for_each_entry_safe(folio, folio2, &unmap_folios, lru) {
1851 		int old_page_state = 0;
1852 		struct anon_vma *anon_vma = NULL;
1853 
1854 		__migrate_folio_extract(dst, &old_page_state, &anon_vma);
1855 		migrate_folio_undo_src(folio, old_page_state & PAGE_WAS_MAPPED,
1856 				       anon_vma, true, ret_folios);
1857 		list_del(&dst->lru);
1858 		migrate_folio_undo_dst(dst, true, put_new_folio, private);
1859 		dst = dst2;
1860 		dst2 = list_next_entry(dst, lru);
1861 	}
1862 
1863 	return rc;
1864 }
1865 
1866 static int migrate_pages_sync(struct list_head *from, new_folio_t get_new_folio,
1867 		free_folio_t put_new_folio, unsigned long private,
1868 		enum migrate_mode mode, int reason,
1869 		struct list_head *ret_folios, struct list_head *split_folios,
1870 		struct migrate_pages_stats *stats)
1871 {
1872 	int rc, nr_failed = 0;
1873 	LIST_HEAD(folios);
1874 	struct migrate_pages_stats astats;
1875 
1876 	memset(&astats, 0, sizeof(astats));
1877 	/* Try to migrate in batch with MIGRATE_ASYNC mode firstly */
1878 	rc = migrate_pages_batch(from, get_new_folio, put_new_folio, private, MIGRATE_ASYNC,
1879 				 reason, &folios, split_folios, &astats,
1880 				 NR_MAX_MIGRATE_ASYNC_RETRY);
1881 	stats->nr_succeeded += astats.nr_succeeded;
1882 	stats->nr_thp_succeeded += astats.nr_thp_succeeded;
1883 	stats->nr_thp_split += astats.nr_thp_split;
1884 	stats->nr_split += astats.nr_split;
1885 	if (rc < 0) {
1886 		stats->nr_failed_pages += astats.nr_failed_pages;
1887 		stats->nr_thp_failed += astats.nr_thp_failed;
1888 		list_splice_tail(&folios, ret_folios);
1889 		return rc;
1890 	}
1891 	stats->nr_thp_failed += astats.nr_thp_split;
1892 	/*
1893 	 * Do not count rc, as pages will be retried below.
1894 	 * Count nr_split only, since it includes nr_thp_split.
1895 	 */
1896 	nr_failed += astats.nr_split;
1897 	/*
1898 	 * Fall back to migrate all failed folios one by one synchronously. All
1899 	 * failed folios except split THPs will be retried, so their failure
1900 	 * isn't counted
1901 	 */
1902 	list_splice_tail_init(&folios, from);
1903 	while (!list_empty(from)) {
1904 		list_move(from->next, &folios);
1905 		rc = migrate_pages_batch(&folios, get_new_folio, put_new_folio,
1906 					 private, mode, reason, ret_folios,
1907 					 split_folios, stats, NR_MAX_MIGRATE_SYNC_RETRY);
1908 		list_splice_tail_init(&folios, ret_folios);
1909 		if (rc < 0)
1910 			return rc;
1911 		nr_failed += rc;
1912 	}
1913 
1914 	return nr_failed;
1915 }
1916 
1917 /*
1918  * migrate_pages - migrate the folios specified in a list, to the free folios
1919  *		   supplied as the target for the page migration
1920  *
1921  * @from:		The list of folios to be migrated.
1922  * @get_new_folio:	The function used to allocate free folios to be used
1923  *			as the target of the folio migration.
1924  * @put_new_folio:	The function used to free target folios if migration
1925  *			fails, or NULL if no special handling is necessary.
1926  * @private:		Private data to be passed on to get_new_folio()
1927  * @mode:		The migration mode that specifies the constraints for
1928  *			folio migration, if any.
1929  * @reason:		The reason for folio migration.
1930  * @ret_succeeded:	Set to the number of folios migrated successfully if
1931  *			the caller passes a non-NULL pointer.
1932  *
1933  * The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios
1934  * are movable any more because the list has become empty or no retryable folios
1935  * exist any more. It is caller's responsibility to call putback_movable_pages()
1936  * only if ret != 0.
1937  *
1938  * Returns the number of {normal folio, large folio, hugetlb} that were not
1939  * migrated, or an error code. The number of large folio splits will be
1940  * considered as the number of non-migrated large folio, no matter how many
1941  * split folios of the large folio are migrated successfully.
1942  */
1943 int migrate_pages(struct list_head *from, new_folio_t get_new_folio,
1944 		free_folio_t put_new_folio, unsigned long private,
1945 		enum migrate_mode mode, int reason, unsigned int *ret_succeeded)
1946 {
1947 	int rc, rc_gather;
1948 	int nr_pages;
1949 	struct folio *folio, *folio2;
1950 	LIST_HEAD(folios);
1951 	LIST_HEAD(ret_folios);
1952 	LIST_HEAD(split_folios);
1953 	struct migrate_pages_stats stats;
1954 
1955 	trace_mm_migrate_pages_start(mode, reason);
1956 
1957 	memset(&stats, 0, sizeof(stats));
1958 
1959 	rc_gather = migrate_hugetlbs(from, get_new_folio, put_new_folio, private,
1960 				     mode, reason, &stats, &ret_folios);
1961 	if (rc_gather < 0)
1962 		goto out;
1963 
1964 again:
1965 	nr_pages = 0;
1966 	list_for_each_entry_safe(folio, folio2, from, lru) {
1967 		/* Retried hugetlb folios will be kept in list  */
1968 		if (folio_test_hugetlb(folio)) {
1969 			list_move_tail(&folio->lru, &ret_folios);
1970 			continue;
1971 		}
1972 
1973 		nr_pages += folio_nr_pages(folio);
1974 		if (nr_pages >= NR_MAX_BATCHED_MIGRATION)
1975 			break;
1976 	}
1977 	if (nr_pages >= NR_MAX_BATCHED_MIGRATION)
1978 		list_cut_before(&folios, from, &folio2->lru);
1979 	else
1980 		list_splice_init(from, &folios);
1981 	if (mode == MIGRATE_ASYNC)
1982 		rc = migrate_pages_batch(&folios, get_new_folio, put_new_folio,
1983 				private, mode, reason, &ret_folios,
1984 				&split_folios, &stats,
1985 				NR_MAX_MIGRATE_PAGES_RETRY);
1986 	else
1987 		rc = migrate_pages_sync(&folios, get_new_folio, put_new_folio,
1988 				private, mode, reason, &ret_folios,
1989 				&split_folios, &stats);
1990 	list_splice_tail_init(&folios, &ret_folios);
1991 	if (rc < 0) {
1992 		rc_gather = rc;
1993 		list_splice_tail(&split_folios, &ret_folios);
1994 		goto out;
1995 	}
1996 	if (!list_empty(&split_folios)) {
1997 		/*
1998 		 * Failure isn't counted since all split folios of a large folio
1999 		 * is counted as 1 failure already.  And, we only try to migrate
2000 		 * with minimal effort, force MIGRATE_ASYNC mode and retry once.
2001 		 */
2002 		migrate_pages_batch(&split_folios, get_new_folio,
2003 				put_new_folio, private, MIGRATE_ASYNC, reason,
2004 				&ret_folios, NULL, &stats, 1);
2005 		list_splice_tail_init(&split_folios, &ret_folios);
2006 	}
2007 	rc_gather += rc;
2008 	if (!list_empty(from))
2009 		goto again;
2010 out:
2011 	/*
2012 	 * Put the permanent failure folio back to migration list, they
2013 	 * will be put back to the right list by the caller.
2014 	 */
2015 	list_splice(&ret_folios, from);
2016 
2017 	/*
2018 	 * Return 0 in case all split folios of fail-to-migrate large folios
2019 	 * are migrated successfully.
2020 	 */
2021 	if (list_empty(from))
2022 		rc_gather = 0;
2023 
2024 	count_vm_events(PGMIGRATE_SUCCESS, stats.nr_succeeded);
2025 	count_vm_events(PGMIGRATE_FAIL, stats.nr_failed_pages);
2026 	count_vm_events(THP_MIGRATION_SUCCESS, stats.nr_thp_succeeded);
2027 	count_vm_events(THP_MIGRATION_FAIL, stats.nr_thp_failed);
2028 	count_vm_events(THP_MIGRATION_SPLIT, stats.nr_thp_split);
2029 	trace_mm_migrate_pages(stats.nr_succeeded, stats.nr_failed_pages,
2030 			       stats.nr_thp_succeeded, stats.nr_thp_failed,
2031 			       stats.nr_thp_split, stats.nr_split, mode,
2032 			       reason);
2033 
2034 	if (ret_succeeded)
2035 		*ret_succeeded = stats.nr_succeeded;
2036 
2037 	return rc_gather;
2038 }
2039 
2040 struct folio *alloc_migration_target(struct folio *src, unsigned long private)
2041 {
2042 	struct migration_target_control *mtc;
2043 	gfp_t gfp_mask;
2044 	unsigned int order = 0;
2045 	int nid;
2046 	int zidx;
2047 
2048 	mtc = (struct migration_target_control *)private;
2049 	gfp_mask = mtc->gfp_mask;
2050 	nid = mtc->nid;
2051 	if (nid == NUMA_NO_NODE)
2052 		nid = folio_nid(src);
2053 
2054 	if (folio_test_hugetlb(src)) {
2055 		struct hstate *h = folio_hstate(src);
2056 
2057 		gfp_mask = htlb_modify_alloc_mask(h, gfp_mask);
2058 		return alloc_hugetlb_folio_nodemask(h, nid,
2059 						mtc->nmask, gfp_mask,
2060 						htlb_allow_alloc_fallback(mtc->reason));
2061 	}
2062 
2063 	if (folio_test_large(src)) {
2064 		/*
2065 		 * clear __GFP_RECLAIM to make the migration callback
2066 		 * consistent with regular THP allocations.
2067 		 */
2068 		gfp_mask &= ~__GFP_RECLAIM;
2069 		gfp_mask |= GFP_TRANSHUGE;
2070 		order = folio_order(src);
2071 	}
2072 	zidx = zone_idx(folio_zone(src));
2073 	if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
2074 		gfp_mask |= __GFP_HIGHMEM;
2075 
2076 	return __folio_alloc(gfp_mask, order, nid, mtc->nmask);
2077 }
2078 
2079 #ifdef CONFIG_NUMA
2080 
2081 static int store_status(int __user *status, int start, int value, int nr)
2082 {
2083 	while (nr-- > 0) {
2084 		if (put_user(value, status + start))
2085 			return -EFAULT;
2086 		start++;
2087 	}
2088 
2089 	return 0;
2090 }
2091 
2092 static int do_move_pages_to_node(struct list_head *pagelist, int node)
2093 {
2094 	int err;
2095 	struct migration_target_control mtc = {
2096 		.nid = node,
2097 		.gfp_mask = GFP_HIGHUSER_MOVABLE | __GFP_THISNODE,
2098 		.reason = MR_SYSCALL,
2099 	};
2100 
2101 	err = migrate_pages(pagelist, alloc_migration_target, NULL,
2102 		(unsigned long)&mtc, MIGRATE_SYNC, MR_SYSCALL, NULL);
2103 	if (err)
2104 		putback_movable_pages(pagelist);
2105 	return err;
2106 }
2107 
2108 /*
2109  * Resolves the given address to a struct page, isolates it from the LRU and
2110  * puts it to the given pagelist.
2111  * Returns:
2112  *     errno - if the page cannot be found/isolated
2113  *     0 - when it doesn't have to be migrated because it is already on the
2114  *         target node
2115  *     1 - when it has been queued
2116  */
2117 static int add_page_for_migration(struct mm_struct *mm, const void __user *p,
2118 		int node, struct list_head *pagelist, bool migrate_all)
2119 {
2120 	struct vm_area_struct *vma;
2121 	unsigned long addr;
2122 	struct page *page;
2123 	struct folio *folio;
2124 	int err;
2125 
2126 	mmap_read_lock(mm);
2127 	addr = (unsigned long)untagged_addr_remote(mm, p);
2128 
2129 	err = -EFAULT;
2130 	vma = vma_lookup(mm, addr);
2131 	if (!vma || !vma_migratable(vma))
2132 		goto out;
2133 
2134 	/* FOLL_DUMP to ignore special (like zero) pages */
2135 	page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
2136 
2137 	err = PTR_ERR(page);
2138 	if (IS_ERR(page))
2139 		goto out;
2140 
2141 	err = -ENOENT;
2142 	if (!page)
2143 		goto out;
2144 
2145 	folio = page_folio(page);
2146 	if (folio_is_zone_device(folio))
2147 		goto out_putfolio;
2148 
2149 	err = 0;
2150 	if (folio_nid(folio) == node)
2151 		goto out_putfolio;
2152 
2153 	err = -EACCES;
2154 	if (folio_likely_mapped_shared(folio) && !migrate_all)
2155 		goto out_putfolio;
2156 
2157 	err = -EBUSY;
2158 	if (folio_test_hugetlb(folio)) {
2159 		if (isolate_hugetlb(folio, pagelist))
2160 			err = 1;
2161 	} else {
2162 		if (!folio_isolate_lru(folio))
2163 			goto out_putfolio;
2164 
2165 		err = 1;
2166 		list_add_tail(&folio->lru, pagelist);
2167 		node_stat_mod_folio(folio,
2168 			NR_ISOLATED_ANON + folio_is_file_lru(folio),
2169 			folio_nr_pages(folio));
2170 	}
2171 out_putfolio:
2172 	/*
2173 	 * Either remove the duplicate refcount from folio_isolate_lru()
2174 	 * or drop the folio ref if it was not isolated.
2175 	 */
2176 	folio_put(folio);
2177 out:
2178 	mmap_read_unlock(mm);
2179 	return err;
2180 }
2181 
2182 static int move_pages_and_store_status(int node,
2183 		struct list_head *pagelist, int __user *status,
2184 		int start, int i, unsigned long nr_pages)
2185 {
2186 	int err;
2187 
2188 	if (list_empty(pagelist))
2189 		return 0;
2190 
2191 	err = do_move_pages_to_node(pagelist, node);
2192 	if (err) {
2193 		/*
2194 		 * Positive err means the number of failed
2195 		 * pages to migrate.  Since we are going to
2196 		 * abort and return the number of non-migrated
2197 		 * pages, so need to include the rest of the
2198 		 * nr_pages that have not been attempted as
2199 		 * well.
2200 		 */
2201 		if (err > 0)
2202 			err += nr_pages - i;
2203 		return err;
2204 	}
2205 	return store_status(status, start, node, i - start);
2206 }
2207 
2208 /*
2209  * Migrate an array of page address onto an array of nodes and fill
2210  * the corresponding array of status.
2211  */
2212 static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
2213 			 unsigned long nr_pages,
2214 			 const void __user * __user *pages,
2215 			 const int __user *nodes,
2216 			 int __user *status, int flags)
2217 {
2218 	compat_uptr_t __user *compat_pages = (void __user *)pages;
2219 	int current_node = NUMA_NO_NODE;
2220 	LIST_HEAD(pagelist);
2221 	int start, i;
2222 	int err = 0, err1;
2223 
2224 	lru_cache_disable();
2225 
2226 	for (i = start = 0; i < nr_pages; i++) {
2227 		const void __user *p;
2228 		int node;
2229 
2230 		err = -EFAULT;
2231 		if (in_compat_syscall()) {
2232 			compat_uptr_t cp;
2233 
2234 			if (get_user(cp, compat_pages + i))
2235 				goto out_flush;
2236 
2237 			p = compat_ptr(cp);
2238 		} else {
2239 			if (get_user(p, pages + i))
2240 				goto out_flush;
2241 		}
2242 		if (get_user(node, nodes + i))
2243 			goto out_flush;
2244 
2245 		err = -ENODEV;
2246 		if (node < 0 || node >= MAX_NUMNODES)
2247 			goto out_flush;
2248 		if (!node_state(node, N_MEMORY))
2249 			goto out_flush;
2250 
2251 		err = -EACCES;
2252 		if (!node_isset(node, task_nodes))
2253 			goto out_flush;
2254 
2255 		if (current_node == NUMA_NO_NODE) {
2256 			current_node = node;
2257 			start = i;
2258 		} else if (node != current_node) {
2259 			err = move_pages_and_store_status(current_node,
2260 					&pagelist, status, start, i, nr_pages);
2261 			if (err)
2262 				goto out;
2263 			start = i;
2264 			current_node = node;
2265 		}
2266 
2267 		/*
2268 		 * Errors in the page lookup or isolation are not fatal and we simply
2269 		 * report them via status
2270 		 */
2271 		err = add_page_for_migration(mm, p, current_node, &pagelist,
2272 					     flags & MPOL_MF_MOVE_ALL);
2273 
2274 		if (err > 0) {
2275 			/* The page is successfully queued for migration */
2276 			continue;
2277 		}
2278 
2279 		/*
2280 		 * The move_pages() man page does not have an -EEXIST choice, so
2281 		 * use -EFAULT instead.
2282 		 */
2283 		if (err == -EEXIST)
2284 			err = -EFAULT;
2285 
2286 		/*
2287 		 * If the page is already on the target node (!err), store the
2288 		 * node, otherwise, store the err.
2289 		 */
2290 		err = store_status(status, i, err ? : current_node, 1);
2291 		if (err)
2292 			goto out_flush;
2293 
2294 		err = move_pages_and_store_status(current_node, &pagelist,
2295 				status, start, i, nr_pages);
2296 		if (err) {
2297 			/* We have accounted for page i */
2298 			if (err > 0)
2299 				err--;
2300 			goto out;
2301 		}
2302 		current_node = NUMA_NO_NODE;
2303 	}
2304 out_flush:
2305 	/* Make sure we do not overwrite the existing error */
2306 	err1 = move_pages_and_store_status(current_node, &pagelist,
2307 				status, start, i, nr_pages);
2308 	if (err >= 0)
2309 		err = err1;
2310 out:
2311 	lru_cache_enable();
2312 	return err;
2313 }
2314 
2315 /*
2316  * Determine the nodes of an array of pages and store it in an array of status.
2317  */
2318 static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
2319 				const void __user **pages, int *status)
2320 {
2321 	unsigned long i;
2322 
2323 	mmap_read_lock(mm);
2324 
2325 	for (i = 0; i < nr_pages; i++) {
2326 		unsigned long addr = (unsigned long)(*pages);
2327 		struct vm_area_struct *vma;
2328 		struct page *page;
2329 		int err = -EFAULT;
2330 
2331 		vma = vma_lookup(mm, addr);
2332 		if (!vma)
2333 			goto set_status;
2334 
2335 		/* FOLL_DUMP to ignore special (like zero) pages */
2336 		page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
2337 
2338 		err = PTR_ERR(page);
2339 		if (IS_ERR(page))
2340 			goto set_status;
2341 
2342 		err = -ENOENT;
2343 		if (!page)
2344 			goto set_status;
2345 
2346 		if (!is_zone_device_page(page))
2347 			err = page_to_nid(page);
2348 
2349 		put_page(page);
2350 set_status:
2351 		*status = err;
2352 
2353 		pages++;
2354 		status++;
2355 	}
2356 
2357 	mmap_read_unlock(mm);
2358 }
2359 
2360 static int get_compat_pages_array(const void __user *chunk_pages[],
2361 				  const void __user * __user *pages,
2362 				  unsigned long chunk_nr)
2363 {
2364 	compat_uptr_t __user *pages32 = (compat_uptr_t __user *)pages;
2365 	compat_uptr_t p;
2366 	int i;
2367 
2368 	for (i = 0; i < chunk_nr; i++) {
2369 		if (get_user(p, pages32 + i))
2370 			return -EFAULT;
2371 		chunk_pages[i] = compat_ptr(p);
2372 	}
2373 
2374 	return 0;
2375 }
2376 
2377 /*
2378  * Determine the nodes of a user array of pages and store it in
2379  * a user array of status.
2380  */
2381 static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
2382 			 const void __user * __user *pages,
2383 			 int __user *status)
2384 {
2385 #define DO_PAGES_STAT_CHUNK_NR 16UL
2386 	const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
2387 	int chunk_status[DO_PAGES_STAT_CHUNK_NR];
2388 
2389 	while (nr_pages) {
2390 		unsigned long chunk_nr = min(nr_pages, DO_PAGES_STAT_CHUNK_NR);
2391 
2392 		if (in_compat_syscall()) {
2393 			if (get_compat_pages_array(chunk_pages, pages,
2394 						   chunk_nr))
2395 				break;
2396 		} else {
2397 			if (copy_from_user(chunk_pages, pages,
2398 				      chunk_nr * sizeof(*chunk_pages)))
2399 				break;
2400 		}
2401 
2402 		do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
2403 
2404 		if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
2405 			break;
2406 
2407 		pages += chunk_nr;
2408 		status += chunk_nr;
2409 		nr_pages -= chunk_nr;
2410 	}
2411 	return nr_pages ? -EFAULT : 0;
2412 }
2413 
2414 static struct mm_struct *find_mm_struct(pid_t pid, nodemask_t *mem_nodes)
2415 {
2416 	struct task_struct *task;
2417 	struct mm_struct *mm;
2418 
2419 	/*
2420 	 * There is no need to check if current process has the right to modify
2421 	 * the specified process when they are same.
2422 	 */
2423 	if (!pid) {
2424 		mmget(current->mm);
2425 		*mem_nodes = cpuset_mems_allowed(current);
2426 		return current->mm;
2427 	}
2428 
2429 	/* Find the mm_struct */
2430 	rcu_read_lock();
2431 	task = find_task_by_vpid(pid);
2432 	if (!task) {
2433 		rcu_read_unlock();
2434 		return ERR_PTR(-ESRCH);
2435 	}
2436 	get_task_struct(task);
2437 
2438 	/*
2439 	 * Check if this process has the right to modify the specified
2440 	 * process. Use the regular "ptrace_may_access()" checks.
2441 	 */
2442 	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
2443 		rcu_read_unlock();
2444 		mm = ERR_PTR(-EPERM);
2445 		goto out;
2446 	}
2447 	rcu_read_unlock();
2448 
2449 	mm = ERR_PTR(security_task_movememory(task));
2450 	if (IS_ERR(mm))
2451 		goto out;
2452 	*mem_nodes = cpuset_mems_allowed(task);
2453 	mm = get_task_mm(task);
2454 out:
2455 	put_task_struct(task);
2456 	if (!mm)
2457 		mm = ERR_PTR(-EINVAL);
2458 	return mm;
2459 }
2460 
2461 /*
2462  * Move a list of pages in the address space of the currently executing
2463  * process.
2464  */
2465 static int kernel_move_pages(pid_t pid, unsigned long nr_pages,
2466 			     const void __user * __user *pages,
2467 			     const int __user *nodes,
2468 			     int __user *status, int flags)
2469 {
2470 	struct mm_struct *mm;
2471 	int err;
2472 	nodemask_t task_nodes;
2473 
2474 	/* Check flags */
2475 	if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
2476 		return -EINVAL;
2477 
2478 	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
2479 		return -EPERM;
2480 
2481 	mm = find_mm_struct(pid, &task_nodes);
2482 	if (IS_ERR(mm))
2483 		return PTR_ERR(mm);
2484 
2485 	if (nodes)
2486 		err = do_pages_move(mm, task_nodes, nr_pages, pages,
2487 				    nodes, status, flags);
2488 	else
2489 		err = do_pages_stat(mm, nr_pages, pages, status);
2490 
2491 	mmput(mm);
2492 	return err;
2493 }
2494 
2495 SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
2496 		const void __user * __user *, pages,
2497 		const int __user *, nodes,
2498 		int __user *, status, int, flags)
2499 {
2500 	return kernel_move_pages(pid, nr_pages, pages, nodes, status, flags);
2501 }
2502 
2503 #ifdef CONFIG_NUMA_BALANCING
2504 /*
2505  * Returns true if this is a safe migration target node for misplaced NUMA
2506  * pages. Currently it only checks the watermarks which is crude.
2507  */
2508 static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
2509 				   unsigned long nr_migrate_pages)
2510 {
2511 	int z;
2512 
2513 	for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2514 		struct zone *zone = pgdat->node_zones + z;
2515 
2516 		if (!managed_zone(zone))
2517 			continue;
2518 
2519 		/* Avoid waking kswapd by allocating pages_to_migrate pages. */
2520 		if (!zone_watermark_ok(zone, 0,
2521 				       high_wmark_pages(zone) +
2522 				       nr_migrate_pages,
2523 				       ZONE_MOVABLE, 0))
2524 			continue;
2525 		return true;
2526 	}
2527 	return false;
2528 }
2529 
2530 static struct folio *alloc_misplaced_dst_folio(struct folio *src,
2531 					   unsigned long data)
2532 {
2533 	int nid = (int) data;
2534 	int order = folio_order(src);
2535 	gfp_t gfp = __GFP_THISNODE;
2536 
2537 	if (order > 0)
2538 		gfp |= GFP_TRANSHUGE_LIGHT;
2539 	else {
2540 		gfp |= GFP_HIGHUSER_MOVABLE | __GFP_NOMEMALLOC | __GFP_NORETRY |
2541 			__GFP_NOWARN;
2542 		gfp &= ~__GFP_RECLAIM;
2543 	}
2544 	return __folio_alloc_node(gfp, order, nid);
2545 }
2546 
2547 /*
2548  * Prepare for calling migrate_misplaced_folio() by isolating the folio if
2549  * permitted. Must be called with the PTL still held.
2550  */
2551 int migrate_misplaced_folio_prepare(struct folio *folio,
2552 		struct vm_area_struct *vma, int node)
2553 {
2554 	int nr_pages = folio_nr_pages(folio);
2555 	pg_data_t *pgdat = NODE_DATA(node);
2556 
2557 	if (folio_is_file_lru(folio)) {
2558 		/*
2559 		 * Do not migrate file folios that are mapped in multiple
2560 		 * processes with execute permissions as they are probably
2561 		 * shared libraries.
2562 		 *
2563 		 * See folio_likely_mapped_shared() on possible imprecision
2564 		 * when we cannot easily detect if a folio is shared.
2565 		 */
2566 		if ((vma->vm_flags & VM_EXEC) &&
2567 		    folio_likely_mapped_shared(folio))
2568 			return -EACCES;
2569 
2570 		/*
2571 		 * Do not migrate dirty folios as not all filesystems can move
2572 		 * dirty folios in MIGRATE_ASYNC mode which is a waste of
2573 		 * cycles.
2574 		 */
2575 		if (folio_test_dirty(folio))
2576 			return -EAGAIN;
2577 	}
2578 
2579 	/* Avoid migrating to a node that is nearly full */
2580 	if (!migrate_balanced_pgdat(pgdat, nr_pages)) {
2581 		int z;
2582 
2583 		if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING))
2584 			return -EAGAIN;
2585 		for (z = pgdat->nr_zones - 1; z >= 0; z--) {
2586 			if (managed_zone(pgdat->node_zones + z))
2587 				break;
2588 		}
2589 
2590 		/*
2591 		 * If there are no managed zones, it should not proceed
2592 		 * further.
2593 		 */
2594 		if (z < 0)
2595 			return -EAGAIN;
2596 
2597 		wakeup_kswapd(pgdat->node_zones + z, 0,
2598 			      folio_order(folio), ZONE_MOVABLE);
2599 		return -EAGAIN;
2600 	}
2601 
2602 	if (!folio_isolate_lru(folio))
2603 		return -EAGAIN;
2604 
2605 	node_stat_mod_folio(folio, NR_ISOLATED_ANON + folio_is_file_lru(folio),
2606 			    nr_pages);
2607 	return 0;
2608 }
2609 
2610 /*
2611  * Attempt to migrate a misplaced folio to the specified destination
2612  * node. Caller is expected to have isolated the folio by calling
2613  * migrate_misplaced_folio_prepare(), which will result in an
2614  * elevated reference count on the folio. This function will un-isolate the
2615  * folio, dereferencing the folio before returning.
2616  */
2617 int migrate_misplaced_folio(struct folio *folio, struct vm_area_struct *vma,
2618 			    int node)
2619 {
2620 	pg_data_t *pgdat = NODE_DATA(node);
2621 	int nr_remaining;
2622 	unsigned int nr_succeeded;
2623 	LIST_HEAD(migratepages);
2624 
2625 	list_add(&folio->lru, &migratepages);
2626 	nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_folio,
2627 				     NULL, node, MIGRATE_ASYNC,
2628 				     MR_NUMA_MISPLACED, &nr_succeeded);
2629 	if (nr_remaining && !list_empty(&migratepages))
2630 		putback_movable_pages(&migratepages);
2631 	if (nr_succeeded) {
2632 		count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded);
2633 		if (!node_is_toptier(folio_nid(folio)) && node_is_toptier(node))
2634 			mod_node_page_state(pgdat, PGPROMOTE_SUCCESS,
2635 					    nr_succeeded);
2636 	}
2637 	BUG_ON(!list_empty(&migratepages));
2638 	return nr_remaining ? -EAGAIN : 0;
2639 }
2640 #endif /* CONFIG_NUMA_BALANCING */
2641 #endif /* CONFIG_NUMA */
2642