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