1 // SPDX-License-Identifier: GPL-2.0
3 * Memory Migration functionality - linux/mm/migrate.c
5 * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
7 * Page migration was first developed in the context of the memory hotplug
8 * project. The main authors of the migration code are:
10 * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
11 * Hirokazu Takahashi <taka@valinux.co.jp>
12 * Dave Hansen <haveblue@us.ibm.com>
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>
54 #include <asm/tlbflush.h>
56 #include <trace/events/migrate.h>
60 bool isolate_movable_page(struct page
*page
, isolate_mode_t mode
)
62 struct folio
*folio
= folio_get_nontail_page(page
);
63 const struct movable_operations
*mops
;
66 * Avoid burning cycles with pages that are yet under __free_pages(),
67 * or just got freed under us.
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.
77 if (unlikely(folio_test_slab(folio
)))
79 /* Pairs with smp_wmb() in slab freeing, e.g. SLUB's __free_slab() */
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.
86 if (unlikely(!__folio_test_movable(folio
)))
88 /* Pairs with smp_wmb() in slab allocation, e.g. SLUB's alloc_slab_page() */
90 if (unlikely(folio_test_slab(folio
)))
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.
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.
104 if (unlikely(!folio_trylock(folio
)))
107 if (!folio_test_movable(folio
) || folio_test_isolated(folio
))
108 goto out_no_isolated
;
110 mops
= folio_movable_ops(folio
);
111 VM_BUG_ON_FOLIO(!mops
, folio
);
113 if (!mops
->isolate_page(&folio
->page
, mode
))
114 goto out_no_isolated
;
116 /* Driver shouldn't use the isolated flag */
117 WARN_ON_ONCE(folio_test_isolated(folio
));
118 folio_set_isolated(folio
);
131 static void putback_movable_folio(struct folio
*folio
)
133 const struct movable_operations
*mops
= folio_movable_ops(folio
);
135 mops
->putback_page(&folio
->page
);
136 folio_clear_isolated(folio
);
140 * Put previously isolated pages back onto the appropriate lists
141 * from where they were once taken off for compaction/migration.
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().
147 void putback_movable_pages(struct list_head
*l
)
150 struct folio
*folio2
;
152 list_for_each_entry_safe(folio
, folio2
, l
, lru
) {
153 if (unlikely(folio_test_hugetlb(folio
))) {
154 folio_putback_active_hugetlb(folio
);
157 list_del(&folio
->lru
);
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.
163 if (unlikely(__folio_test_movable(folio
))) {
164 VM_BUG_ON_FOLIO(!folio_test_isolated(folio
), folio
);
166 if (folio_test_movable(folio
))
167 putback_movable_folio(folio
);
169 folio_clear_isolated(folio
);
173 node_stat_mod_folio(folio
, NR_ISOLATED_ANON
+
174 folio_is_file_lru(folio
), -folio_nr_pages(folio
));
175 folio_putback_lru(folio
);
181 * Restore a potential migration pte to a working pte entry
183 static bool remove_migration_pte(struct folio
*folio
,
184 struct vm_area_struct
*vma
, unsigned long addr
, void *old
)
186 DEFINE_FOLIO_VMA_WALK(pvmw
, old
, vma
, addr
, PVMW_SYNC
| PVMW_MIGRATION
);
188 while (page_vma_mapped_walk(&pvmw
)) {
189 rmap_t rmap_flags
= RMAP_NONE
;
194 unsigned long idx
= 0;
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
);
201 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
202 /* PMD-mapped THP migration entry */
204 VM_BUG_ON_FOLIO(folio_test_hugetlb(folio
) ||
205 !folio_test_pmd_mappable(folio
), folio
);
206 remove_migration_pmd(&pvmw
, new);
212 pte
= mk_pte(new, READ_ONCE(vma
->vm_page_prot
));
213 old_pte
= ptep_get(pvmw
.pte
);
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
);
223 pte
= pte_clear_soft_dirty(pte
);
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
);
230 if (folio_test_anon(folio
) && !is_readable_migration_entry(entry
))
231 rmap_flags
|= RMAP_EXCLUSIVE
;
233 if (unlikely(is_device_private_page(new))) {
235 entry
= make_writable_device_private_entry(
238 entry
= make_readable_device_private_entry(
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
);
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
);
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
,
258 hugetlb_add_file_rmap(folio
);
259 set_huge_pte_at(vma
->vm_mm
, pvmw
.address
, pvmw
.pte
, pte
,
264 if (folio_test_anon(folio
))
265 folio_add_anon_rmap_pte(folio
, new, vma
,
266 pvmw
.address
, rmap_flags
);
268 folio_add_file_rmap_pte(folio
, new, vma
);
269 set_pte_at(vma
->vm_mm
, pvmw
.address
, pvmw
.pte
, pte
);
271 if (vma
->vm_flags
& VM_LOCKED
)
274 trace_remove_migration_pte(pvmw
.address
, pte_val(pte
),
275 compound_order(new));
277 /* No need to invalidate - it was non-present before */
278 update_mmu_cache(vma
, pvmw
.address
, pvmw
.pte
);
285 * Get rid of all migration entries and replace them by
286 * references to the indicated page.
288 void remove_migration_ptes(struct folio
*src
, struct folio
*dst
, bool locked
)
290 struct rmap_walk_control rwc
= {
291 .rmap_one
= remove_migration_pte
,
296 rmap_walk_locked(dst
, &rwc
);
298 rmap_walk(dst
, &rwc
);
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.
306 void migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
,
307 unsigned long address
)
314 ptep
= pte_offset_map_lock(mm
, pmd
, address
, &ptl
);
318 pte
= ptep_get(ptep
);
321 if (!is_swap_pte(pte
))
324 entry
= pte_to_swp_entry(pte
);
325 if (!is_migration_entry(entry
))
328 migration_entry_wait_on_locked(entry
, ptl
);
334 #ifdef CONFIG_HUGETLB_PAGE
336 * The vma read lock must be held upon entry. Holding that lock prevents either
337 * the pte or the ptl from being freed.
339 * This function will release the vma lock before returning.
341 void migration_entry_wait_huge(struct vm_area_struct
*vma
, unsigned long addr
, pte_t
*ptep
)
343 spinlock_t
*ptl
= huge_pte_lockptr(hstate_vma(vma
), vma
->vm_mm
, ptep
);
346 hugetlb_vma_assert_locked(vma
);
348 pte
= huge_ptep_get(vma
->vm_mm
, addr
, ptep
);
350 if (unlikely(!is_hugetlb_entry_migration(pte
))) {
352 hugetlb_vma_unlock_read(vma
);
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().
360 hugetlb_vma_unlock_read(vma
);
361 migration_entry_wait_on_locked(pte_to_swp_entry(pte
), ptl
);
366 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
367 void pmd_migration_entry_wait(struct mm_struct
*mm
, pmd_t
*pmd
)
371 ptl
= pmd_lock(mm
, pmd
);
372 if (!is_pmd_migration_entry(*pmd
))
374 migration_entry_wait_on_locked(pmd_to_swp_entry(*pmd
), ptl
);
381 static int folio_expected_refs(struct address_space
*mapping
,
388 refs
+= folio_nr_pages(folio
);
389 if (folio_test_private(folio
))
396 * Replace the folio in the mapping.
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.
403 static int __folio_migrate_mapping(struct address_space
*mapping
,
404 struct folio
*newfolio
, struct folio
*folio
, int expected_count
)
406 XA_STATE(xas
, &mapping
->i_pages
, folio_index(folio
));
407 struct zone
*oldzone
, *newzone
;
409 long nr
= folio_nr_pages(folio
);
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
))
418 folio_undo_large_rmappable(folio
);
419 folio_ref_unfreeze(folio
, expected_count
);
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
);
428 return MIGRATEPAGE_SUCCESS
;
431 oldzone
= folio_zone(folio
);
432 newzone
= folio_zone(newfolio
);
435 if (!folio_ref_freeze(folio
, expected_count
)) {
436 xas_unlock_irq(&xas
);
440 /* Take off deferred split queue while frozen and memcg set */
441 folio_undo_large_rmappable(folio
);
444 * Now we know that no one else is looking at the folio:
445 * no turning back from here.
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
);
458 VM_BUG_ON_FOLIO(folio_test_swapcache(folio
), folio
);
462 /* Move dirty while folio refs frozen and newfolio not yet exposed */
463 dirty
= folio_test_dirty(folio
);
465 folio_clear_dirty(folio
);
466 folio_set_dirty(newfolio
);
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
);
476 * Drop cache reference from old folio by unfreezing
477 * to one less reference.
478 * We know this isn't the last reference.
480 folio_ref_unfreeze(folio
, expected_count
- nr
);
483 /* Leave irq disabled to prevent preemption while updating stats */
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.
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.
495 if (newzone
!= oldzone
) {
496 struct lruvec
*old_lruvec
, *new_lruvec
;
497 struct mem_cgroup
*memcg
;
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
);
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
);
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
);
515 if (folio_test_swapcache(folio
)) {
516 __mod_lruvec_state(old_lruvec
, NR_SWAPCACHE
, -nr
);
517 __mod_lruvec_state(new_lruvec
, NR_SWAPCACHE
, nr
);
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
);
529 return MIGRATEPAGE_SUCCESS
;
532 int folio_migrate_mapping(struct address_space
*mapping
,
533 struct folio
*newfolio
, struct folio
*folio
, int extra_count
)
535 int expected_count
= folio_expected_refs(mapping
, folio
) + extra_count
;
537 if (folio_ref_count(folio
) != expected_count
)
540 return __folio_migrate_mapping(mapping
, newfolio
, folio
, expected_count
);
542 EXPORT_SYMBOL(folio_migrate_mapping
);
545 * The expected number of remaining references is the same as that
546 * of folio_migrate_mapping().
548 int migrate_huge_page_move_mapping(struct address_space
*mapping
,
549 struct folio
*dst
, struct folio
*src
)
551 XA_STATE(xas
, &mapping
->i_pages
, folio_index(src
));
552 int rc
, expected_count
= folio_expected_refs(mapping
, src
);
554 if (folio_ref_count(src
) != expected_count
)
557 rc
= folio_mc_copy(dst
, src
);
562 if (!folio_ref_freeze(src
, expected_count
)) {
563 xas_unlock_irq(&xas
);
567 dst
->index
= src
->index
;
568 dst
->mapping
= src
->mapping
;
570 folio_ref_add(dst
, folio_nr_pages(dst
));
572 xas_store(&xas
, dst
);
574 folio_ref_unfreeze(src
, expected_count
- folio_nr_pages(src
));
576 xas_unlock_irq(&xas
);
578 return MIGRATEPAGE_SUCCESS
;
582 * Copy the flags and some other ancillary information
584 void folio_migrate_flags(struct folio
*newfolio
, struct folio
*folio
)
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
);
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.
609 if (folio_test_mappedtodisk(folio
))
610 folio_set_mappedtodisk(newfolio
);
612 /* Move dirty on pages not done by folio_migrate_mapping() */
613 if (folio_test_dirty(folio
))
614 folio_set_dirty(newfolio
);
616 if (folio_test_young(folio
))
617 folio_set_young(newfolio
);
618 if (folio_test_idle(folio
))
619 folio_set_idle(newfolio
);
622 * Copy NUMA information to the new page, to prevent over-eager
623 * future migrations of this same page.
625 cpupid
= folio_xchg_last_cpupid(folio
, -1);
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.
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
));
635 if (f_toptier
!= t_toptier
)
638 folio_xchg_last_cpupid(newfolio
, cpupid
);
640 folio_migrate_ksm(newfolio
, folio
);
642 * Please do not reorder this without considering how mm/ksm.c's
643 * ksm_get_folio() depends upon ksm_migrate_page() and PageSwapCache().
645 if (folio_test_swapcache(folio
))
646 folio_clear_swapcache(folio
);
647 folio_clear_private(folio
);
649 /* page->private contains hugetlb specific flags */
650 if (!folio_test_hugetlb(folio
))
651 folio
->private = NULL
;
654 * If any waiters have accumulated on the new page then
657 if (folio_test_writeback(newfolio
))
658 folio_end_writeback(newfolio
);
661 * PG_readahead shares the same bit with PG_reclaim. The above
662 * end_page_writeback() may clear PG_readahead mistakenly, so set the
665 if (folio_test_readahead(folio
))
666 folio_set_readahead(newfolio
);
668 folio_copy_owner(newfolio
, folio
);
670 mem_cgroup_migrate(folio
, newfolio
);
672 EXPORT_SYMBOL(folio_migrate_flags
);
674 /************************************************************
675 * Migration functions
676 ***********************************************************/
678 static int __migrate_folio(struct address_space
*mapping
, struct folio
*dst
,
679 struct folio
*src
, void *src_private
,
680 enum migrate_mode mode
)
682 int rc
, expected_count
= folio_expected_refs(mapping
, src
);
684 /* Check whether src does not have extra refs before we do more work */
685 if (folio_ref_count(src
) != expected_count
)
688 rc
= folio_mc_copy(dst
, src
);
692 rc
= __folio_migrate_mapping(mapping
, dst
, src
, expected_count
);
693 if (rc
!= MIGRATEPAGE_SUCCESS
)
697 folio_attach_private(dst
, folio_detach_private(src
));
699 folio_migrate_flags(dst
, src
);
700 return MIGRATEPAGE_SUCCESS
;
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.
710 * Common logic to directly migrate a single LRU folio suitable for
711 * folios that do not use PagePrivate/PagePrivate2.
713 * Folios are locked upon entry and exit.
715 int migrate_folio(struct address_space
*mapping
, struct folio
*dst
,
716 struct folio
*src
, enum migrate_mode mode
)
718 BUG_ON(folio_test_writeback(src
)); /* Writeback must be complete */
719 return __migrate_folio(mapping
, dst
, src
, NULL
, mode
);
721 EXPORT_SYMBOL(migrate_folio
);
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
)
728 struct buffer_head
*bh
= head
;
729 struct buffer_head
*failed_bh
;
732 if (!trylock_buffer(bh
)) {
733 if (mode
== MIGRATE_ASYNC
)
735 if (mode
== MIGRATE_SYNC_LIGHT
&& !buffer_uptodate(bh
))
740 bh
= bh
->b_this_page
;
741 } while (bh
!= head
);
746 /* We failed to lock the buffer and cannot stall. */
749 while (bh
!= failed_bh
) {
751 bh
= bh
->b_this_page
;
757 static int __buffer_migrate_folio(struct address_space
*mapping
,
758 struct folio
*dst
, struct folio
*src
, enum migrate_mode mode
,
761 struct buffer_head
*bh
, *head
;
765 head
= folio_buffers(src
);
767 return migrate_folio(mapping
, dst
, src
, mode
);
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
)
774 if (!buffer_migrate_lock_buffers(head
, mode
))
779 bool invalidated
= false;
783 spin_lock(&mapping
->i_private_lock
);
786 if (atomic_read(&bh
->b_count
)) {
790 bh
= bh
->b_this_page
;
791 } while (bh
!= head
);
797 spin_unlock(&mapping
->i_private_lock
);
798 invalidate_bh_lrus();
800 goto recheck_buffers
;
804 rc
= filemap_migrate_folio(mapping
, dst
, src
, mode
);
805 if (rc
!= MIGRATEPAGE_SUCCESS
)
810 folio_set_bh(bh
, dst
, bh_offset(bh
));
811 bh
= bh
->b_this_page
;
812 } while (bh
!= head
);
816 spin_unlock(&mapping
->i_private_lock
);
820 bh
= bh
->b_this_page
;
821 } while (bh
!= head
);
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.
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
839 * Return: 0 on success or a negative errno on failure.
841 int buffer_migrate_folio(struct address_space
*mapping
,
842 struct folio
*dst
, struct folio
*src
, enum migrate_mode mode
)
844 return __buffer_migrate_folio(mapping
, dst
, src
, mode
, false);
846 EXPORT_SYMBOL(buffer_migrate_folio
);
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.
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).
860 * Return: 0 on success or a negative errno on failure.
862 int buffer_migrate_folio_norefs(struct address_space
*mapping
,
863 struct folio
*dst
, struct folio
*src
, enum migrate_mode mode
)
865 return __buffer_migrate_folio(mapping
, dst
, src
, mode
, true);
867 EXPORT_SYMBOL_GPL(buffer_migrate_folio_norefs
);
868 #endif /* CONFIG_BUFFER_HEAD */
870 int filemap_migrate_folio(struct address_space
*mapping
,
871 struct folio
*dst
, struct folio
*src
, enum migrate_mode mode
)
873 return __migrate_folio(mapping
, dst
, src
, folio_get_private(src
), mode
);
875 EXPORT_SYMBOL_GPL(filemap_migrate_folio
);
878 * Writeback a folio to clean the dirty state
880 static int writeout(struct address_space
*mapping
, struct folio
*folio
)
882 struct writeback_control wbc
= {
883 .sync_mode
= WB_SYNC_NONE
,
886 .range_end
= LLONG_MAX
,
891 if (!mapping
->a_ops
->writepage
)
892 /* No write method for the address space */
895 if (!folio_clear_dirty_for_io(folio
))
896 /* Someone else already triggered a write */
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
907 remove_migration_ptes(folio
, folio
, false);
909 rc
= mapping
->a_ops
->writepage(&folio
->page
, &wbc
);
911 if (rc
!= AOP_WRITEPAGE_ACTIVATE
)
912 /* unlocked. Relock */
915 return (rc
< 0) ? -EIO
: -EAGAIN
;
919 * Default handling if a filesystem does not provide a migration function.
921 static int fallback_migrate_folio(struct address_space
*mapping
,
922 struct folio
*dst
, struct folio
*src
, enum migrate_mode mode
)
924 if (folio_test_dirty(src
)) {
925 /* Only writeback folios in full synchronous migration */
932 return writeout(mapping
, src
);
936 * Buffers may be managed in a filesystem specific way.
937 * We must have no buffers or drop them.
939 if (!filemap_release_folio(src
, GFP_KERNEL
))
940 return mode
== MIGRATE_SYNC
? -EAGAIN
: -EBUSY
;
942 return migrate_folio(mapping
, dst
, src
, mode
);
946 * Move a page to a newly allocated page
947 * The page is locked and all ptes have been successfully removed.
949 * The new page will have replaced the old page if this function
954 * MIGRATEPAGE_SUCCESS - success
956 static int move_to_new_folio(struct folio
*dst
, struct folio
*src
,
957 enum migrate_mode mode
)
960 bool is_lru
= !__folio_test_movable(src
);
962 VM_BUG_ON_FOLIO(!folio_test_locked(src
), src
);
963 VM_BUG_ON_FOLIO(!folio_test_locked(dst
), dst
);
965 if (likely(is_lru
)) {
966 struct address_space
*mapping
= folio_mapping(src
);
969 rc
= migrate_folio(mapping
, dst
, src
, mode
);
970 else if (mapping_inaccessible(mapping
))
972 else if (mapping
->a_ops
->migrate_folio
)
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.
980 rc
= mapping
->a_ops
->migrate_folio(mapping
, dst
, src
,
983 rc
= fallback_migrate_folio(mapping
, dst
, src
, mode
);
985 const struct movable_operations
*mops
;
988 * In case of non-lru page, it could be released after
989 * isolation step. In that case, we shouldn't try migration.
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
);
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
));
1005 * When successful, old pagecache src->mapping must be cleared before
1006 * src is freed; but stats require that PageAnon be left as PageAnon.
1008 if (rc
== MIGRATEPAGE_SUCCESS
) {
1009 if (__folio_test_movable(src
)) {
1010 VM_BUG_ON_FOLIO(!folio_test_isolated(src
), src
);
1013 * We clear PG_movable under page_lock so any compactor
1014 * cannot try to migrate this page.
1016 folio_clear_isolated(src
);
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.
1024 if (!folio_mapping_flags(src
))
1025 src
->mapping
= NULL
;
1027 if (likely(!folio_is_zone_device(dst
)))
1028 flush_dcache_folio(dst
);
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.
1040 PAGE_WAS_MAPPED
= BIT(0),
1041 PAGE_WAS_MLOCKED
= BIT(1),
1042 PAGE_OLD_STATES
= PAGE_WAS_MAPPED
| PAGE_WAS_MLOCKED
,
1045 static void __migrate_folio_record(struct folio
*dst
,
1047 struct anon_vma
*anon_vma
)
1049 dst
->private = (void *)anon_vma
+ old_page_state
;
1052 static void __migrate_folio_extract(struct folio
*dst
,
1053 int *old_page_state
,
1054 struct anon_vma
**anon_vmap
)
1056 unsigned long private = (unsigned long)dst
->private;
1058 *anon_vmap
= (struct anon_vma
*)(private & ~PAGE_OLD_STATES
);
1059 *old_page_state
= private & PAGE_OLD_STATES
;
1060 dst
->private = NULL
;
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
,
1068 struct list_head
*ret
)
1070 if (page_was_mapped
)
1071 remove_migration_ptes(src
, src
, false);
1072 /* Drop an anon_vma reference if we took one */
1074 put_anon_vma(anon_vma
);
1078 list_move_tail(&src
->lru
, ret
);
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)
1088 put_new_folio(dst
, private);
1093 /* Cleanup src folio upon migration success */
1094 static void migrate_folio_done(struct folio
*src
,
1095 enum migrate_reason reason
)
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
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
));
1106 if (reason
!= MR_MEMORY_FAILURE
)
1107 /* We release the page in page_handle_poison. */
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
)
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;
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
;
1135 dst
= get_new_folio(src
, private);
1140 dst
->private = NULL
;
1142 if (!folio_trylock(src
)) {
1143 if (mode
== MIGRATE_ASYNC
)
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
1159 if (current
->flags
& PF_MEMALLOC
)
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.
1167 if (mode
== MIGRATE_SYNC_LIGHT
&& !folio_test_uptodate(src
))
1173 if (folio_test_mlocked(src
))
1174 old_page_state
|= PAGE_WAS_MLOCKED
;
1176 if (folio_test_writeback(src
)) {
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
1190 folio_wait_writeback(src
);
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.
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).
1207 if (folio_test_anon(src
) && !folio_test_ksm(src
))
1208 anon_vma
= folio_get_anon_vma(src
);
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().
1218 if (unlikely(!folio_trylock(dst
)))
1222 if (unlikely(!is_lru
)) {
1223 __migrate_folio_record(dst
, old_page_state
, anon_vma
);
1224 return MIGRATEPAGE_UNMAP
;
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.
1239 if (!src
->mapping
) {
1240 if (folio_test_private(src
)) {
1241 try_to_free_buffers(src
);
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
;
1252 if (!folio_mapped(src
)) {
1253 __migrate_folio_record(dst
, old_page_state
, anon_vma
);
1254 return MIGRATEPAGE_UNMAP
;
1259 * A folio that has not been unmapped will be restored to
1260 * right list unless we want to retry.
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);
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
)
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
;
1284 __migrate_folio_extract(dst
, &old_page_state
, &anon_vma
);
1285 prev
= dst
->lru
.prev
;
1286 list_del(&dst
->lru
);
1288 rc
= move_to_new_folio(dst
, src
, mode
);
1292 if (unlikely(!is_lru
))
1293 goto out_unlock_both
;
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.
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.
1305 if (old_page_state
& PAGE_WAS_MLOCKED
)
1308 if (old_page_state
& PAGE_WAS_MAPPED
)
1309 remove_migration_ptes(src
, dst
, false);
1313 set_page_owner_migrate_reason(&dst
->page
, reason
);
1315 * If migration is successful, decrease refcount of dst,
1316 * which will not free the page because new page owner increased
1322 * A folio that has been migrated has all references removed
1323 * and will be freed.
1325 list_del(&src
->lru
);
1326 /* Drop an anon_vma reference if we took one */
1328 put_anon_vma(anon_vma
);
1330 migrate_folio_done(src
, reason
);
1335 * A folio that has not been migrated will be restored to
1336 * right list unless we want to retry.
1338 if (rc
== -EAGAIN
) {
1339 list_add(&dst
->lru
, prev
);
1340 __migrate_folio_record(dst
, old_page_state
, anon_vma
);
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);
1352 * Counterpart of unmap_and_move_page() for hugepage migration.
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.
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.
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
)
1376 int page_was_mapped
= 0;
1377 struct anon_vma
*anon_vma
= NULL
;
1378 struct address_space
*mapping
= NULL
;
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
;
1386 dst
= get_new_folio(src
, private);
1390 if (!folio_trylock(src
)) {
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.
1407 if (hugetlb_folio_subpool(src
) && !folio_mapping(src
)) {
1412 if (folio_test_anon(src
))
1413 anon_vma
= folio_get_anon_vma(src
);
1415 if (unlikely(!folio_trylock(dst
)))
1418 if (folio_mapped(src
)) {
1419 enum ttu_flags ttu
= 0;
1421 if (!folio_test_anon(src
)) {
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.
1428 mapping
= hugetlb_folio_mapping_lock_write(src
);
1429 if (unlikely(!mapping
))
1430 goto unlock_put_anon
;
1432 ttu
= TTU_RMAP_LOCKED
;
1435 try_to_migrate(src
, ttu
);
1436 page_was_mapped
= 1;
1438 if (ttu
& TTU_RMAP_LOCKED
)
1439 i_mmap_unlock_write(mapping
);
1442 if (!folio_mapped(src
))
1443 rc
= move_to_new_folio(dst
, src
, mode
);
1445 if (page_was_mapped
)
1446 remove_migration_ptes(src
,
1447 rc
== MIGRATEPAGE_SUCCESS
? dst
: src
, false);
1454 put_anon_vma(anon_vma
);
1456 if (rc
== MIGRATEPAGE_SUCCESS
) {
1457 move_hugetlb_state(src
, dst
, reason
);
1458 put_new_folio
= NULL
;
1464 if (rc
== MIGRATEPAGE_SUCCESS
)
1465 folio_putback_active_hugetlb(src
);
1466 else if (rc
!= -EAGAIN
)
1467 list_move_tail(&src
->lru
, ret
);
1470 * If migration was not successful and there's a freeing callback, use
1471 * it. Otherwise, put_page() will drop the reference grabbed during
1475 put_new_folio(dst
, private);
1477 folio_putback_active_hugetlb(dst
);
1482 static inline int try_split_folio(struct folio
*folio
, struct list_head
*split_folios
)
1487 rc
= split_folio_to_list(folio
, split_folios
);
1488 folio_unlock(folio
);
1490 list_move_tail(&folio
->lru
, split_folios
);
1495 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1496 #define NR_MAX_BATCHED_MIGRATION HPAGE_PMD_NR
1498 #define NR_MAX_BATCHED_MIGRATION 512
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)
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 */
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()
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
)
1531 int nr_retry_pages
= 0;
1533 struct folio
*folio
, *folio2
;
1536 for (pass
= 0; pass
< NR_MAX_MIGRATE_PAGES_RETRY
&& retry
; pass
++) {
1540 list_for_each_entry_safe(folio
, folio2
, from
, lru
) {
1541 if (!folio_test_hugetlb(folio
))
1544 nr_pages
= folio_nr_pages(folio
);
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.
1555 if (!hugepage_migration_supported(folio_hstate(folio
))) {
1557 stats
->nr_failed_pages
+= nr_pages
;
1558 list_move_tail(&folio
->lru
, ret_folios
);
1562 rc
= unmap_and_move_huge_page(get_new_folio
,
1563 put_new_folio
, private,
1564 folio
, pass
> 2, mode
,
1565 reason
, ret_folios
);
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
1576 * When memory is low, don't bother to try to migrate
1577 * other folios, just exit.
1579 stats
->nr_failed_pages
+= nr_pages
+ nr_retry_pages
;
1583 nr_retry_pages
+= nr_pages
;
1585 case MIGRATEPAGE_SUCCESS
:
1586 stats
->nr_succeeded
+= nr_pages
;
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.
1596 stats
->nr_failed_pages
+= nr_pages
;
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
1607 stats
->nr_failed_pages
+= nr_retry_pages
;
1613 * migrate_pages_batch() first unmaps folios in the from list as many as
1614 * possible, then move the unmapped folios.
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.
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
)
1630 int nr_retry_pages
= 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
);
1640 VM_WARN_ON_ONCE(mode
!= MIGRATE_ASYNC
&&
1641 !list_empty(from
) && !list_is_singular(from
));
1643 for (pass
= 0; pass
< nr_pass
&& retry
; pass
++) {
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
);
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.
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().
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.
1679 !list_empty(&folio
->_deferred_list
)) {
1680 if (try_split_folio(folio
, split_folios
) == 0) {
1682 stats
->nr_thp_failed
+= is_thp
;
1683 stats
->nr_thp_split
+= is_thp
;
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
1695 * Split folios are put in split_folios, and
1696 * we will migrate them after the rest of the
1697 * list is processed.
1699 if (!thp_migration_supported() && is_thp
) {
1701 stats
->nr_thp_failed
++;
1702 if (!try_split_folio(folio
, split_folios
)) {
1703 stats
->nr_thp_split
++;
1707 stats
->nr_failed_pages
+= nr_pages
;
1708 list_move_tail(&folio
->lru
, ret_folios
);
1712 rc
= migrate_folio_unmap(get_new_folio
, put_new_folio
,
1713 private, folio
, &dst
, mode
, reason
,
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
1727 * When memory is low, don't bother to try to migrate
1728 * other folios, move unmapped folios, then exit.
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
);
1737 stats
->nr_thp_split
+= is_thp
;
1740 } else if (reason
== MR_LONGTERM_PIN
&&
1743 * Try again to split large folio to
1744 * mitigate the failure of longterm pinning.
1747 thp_retry
+= is_thp
;
1748 nr_retry_pages
+= nr_pages
;
1749 /* Undo duplicated failure counting. */
1751 stats
->nr_thp_failed
-= is_thp
;
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
;
1760 if (list_empty(&unmap_folios
))
1766 thp_retry
+= is_thp
;
1767 nr_retry_pages
+= nr_pages
;
1769 case MIGRATEPAGE_SUCCESS
:
1770 stats
->nr_succeeded
+= nr_pages
;
1771 stats
->nr_thp_succeeded
+= is_thp
;
1773 case MIGRATEPAGE_UNMAP
:
1774 list_move_tail(&folio
->lru
, &unmap_folios
);
1775 list_add_tail(&dst
->lru
, &dst_folios
);
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.
1785 stats
->nr_thp_failed
+= is_thp
;
1786 stats
->nr_failed_pages
+= nr_pages
;
1792 stats
->nr_thp_failed
+= thp_retry
;
1793 stats
->nr_failed_pages
+= nr_retry_pages
;
1795 /* Flush TLBs for all unmapped folios */
1796 try_to_unmap_flush();
1799 for (pass
= 0; pass
< nr_pass
&& retry
; pass
++) {
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
);
1812 rc
= migrate_folio_move(put_new_folio
, private,
1814 reason
, ret_folios
);
1817 * Success: folio will be freed
1818 * -EAGAIN: stay on the unmap_folios list
1819 * Other errno: put on ret_folios list
1824 thp_retry
+= is_thp
;
1825 nr_retry_pages
+= nr_pages
;
1827 case MIGRATEPAGE_SUCCESS
:
1828 stats
->nr_succeeded
+= nr_pages
;
1829 stats
->nr_thp_succeeded
+= is_thp
;
1833 stats
->nr_thp_failed
+= is_thp
;
1834 stats
->nr_failed_pages
+= nr_pages
;
1838 dst2
= list_next_entry(dst
, lru
);
1842 stats
->nr_thp_failed
+= thp_retry
;
1843 stats
->nr_failed_pages
+= nr_retry_pages
;
1845 rc
= rc_saved
? : nr_failed
;
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
;
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);
1860 dst2
= list_next_entry(dst
, lru
);
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
)
1872 int rc
, nr_failed
= 0;
1874 struct migrate_pages_stats astats
;
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
;
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
);
1891 stats
->nr_thp_failed
+= astats
.nr_thp_split
;
1893 * Do not count rc, as pages will be retried below.
1894 * Count nr_split only, since it includes nr_thp_split.
1896 nr_failed
+= astats
.nr_split
;
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
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
);
1918 * migrate_pages - migrate the folios specified in a list, to the free folios
1919 * supplied as the target for the page migration
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.
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()
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.
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
)
1949 struct folio
*folio
, *folio2
;
1951 LIST_HEAD(ret_folios
);
1952 LIST_HEAD(split_folios
);
1953 struct migrate_pages_stats stats
;
1955 trace_mm_migrate_pages_start(mode
, reason
);
1957 memset(&stats
, 0, sizeof(stats
));
1959 rc_gather
= migrate_hugetlbs(from
, get_new_folio
, put_new_folio
, private,
1960 mode
, reason
, &stats
, &ret_folios
);
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
);
1973 nr_pages
+= folio_nr_pages(folio
);
1974 if (nr_pages
>= NR_MAX_BATCHED_MIGRATION
)
1977 if (nr_pages
>= NR_MAX_BATCHED_MIGRATION
)
1978 list_cut_before(&folios
, from
, &folio2
->lru
);
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
);
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
);
1993 list_splice_tail(&split_folios
, &ret_folios
);
1996 if (!list_empty(&split_folios
)) {
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.
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
);
2008 if (!list_empty(from
))
2012 * Put the permanent failure folio back to migration list, they
2013 * will be put back to the right list by the caller.
2015 list_splice(&ret_folios
, from
);
2018 * Return 0 in case all split folios of fail-to-migrate large folios
2019 * are migrated successfully.
2021 if (list_empty(from
))
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
,
2035 *ret_succeeded
= stats
.nr_succeeded
;
2040 struct folio
*alloc_migration_target(struct folio
*src
, unsigned long private)
2042 struct migration_target_control
*mtc
;
2044 unsigned int order
= 0;
2048 mtc
= (struct migration_target_control
*)private;
2049 gfp_mask
= mtc
->gfp_mask
;
2051 if (nid
== NUMA_NO_NODE
)
2052 nid
= folio_nid(src
);
2054 if (folio_test_hugetlb(src
)) {
2055 struct hstate
*h
= folio_hstate(src
);
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
));
2063 if (folio_test_large(src
)) {
2065 * clear __GFP_RECLAIM to make the migration callback
2066 * consistent with regular THP allocations.
2068 gfp_mask
&= ~__GFP_RECLAIM
;
2069 gfp_mask
|= GFP_TRANSHUGE
;
2070 order
= folio_order(src
);
2072 zidx
= zone_idx(folio_zone(src
));
2073 if (is_highmem_idx(zidx
) || zidx
== ZONE_MOVABLE
)
2074 gfp_mask
|= __GFP_HIGHMEM
;
2076 return __folio_alloc(gfp_mask
, order
, nid
, mtc
->nmask
);
2081 static int store_status(int __user
*status
, int start
, int value
, int nr
)
2084 if (put_user(value
, status
+ start
))
2092 static int do_move_pages_to_node(struct list_head
*pagelist
, int node
)
2095 struct migration_target_control mtc
= {
2097 .gfp_mask
= GFP_HIGHUSER_MOVABLE
| __GFP_THISNODE
,
2098 .reason
= MR_SYSCALL
,
2101 err
= migrate_pages(pagelist
, alloc_migration_target
, NULL
,
2102 (unsigned long)&mtc
, MIGRATE_SYNC
, MR_SYSCALL
, NULL
);
2104 putback_movable_pages(pagelist
);
2109 * Resolves the given address to a struct page, isolates it from the LRU and
2110 * puts it to the given pagelist.
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
2115 * 1 - when it has been queued
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
)
2120 struct vm_area_struct
*vma
;
2123 struct folio
*folio
;
2127 addr
= (unsigned long)untagged_addr_remote(mm
, p
);
2130 vma
= vma_lookup(mm
, addr
);
2131 if (!vma
|| !vma_migratable(vma
))
2134 /* FOLL_DUMP to ignore special (like zero) pages */
2135 page
= follow_page(vma
, addr
, FOLL_GET
| FOLL_DUMP
);
2137 err
= PTR_ERR(page
);
2145 folio
= page_folio(page
);
2146 if (folio_is_zone_device(folio
))
2150 if (folio_nid(folio
) == node
)
2154 if (folio_likely_mapped_shared(folio
) && !migrate_all
)
2158 if (folio_test_hugetlb(folio
)) {
2159 if (isolate_hugetlb(folio
, pagelist
))
2162 if (!folio_isolate_lru(folio
))
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
));
2173 * Either remove the duplicate refcount from folio_isolate_lru()
2174 * or drop the folio ref if it was not isolated.
2178 mmap_read_unlock(mm
);
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
)
2188 if (list_empty(pagelist
))
2191 err
= do_move_pages_to_node(pagelist
, node
);
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
2202 err
+= nr_pages
- i
;
2205 return store_status(status
, start
, node
, i
- start
);
2209 * Migrate an array of page address onto an array of nodes and fill
2210 * the corresponding array of status.
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
)
2218 compat_uptr_t __user
*compat_pages
= (void __user
*)pages
;
2219 int current_node
= NUMA_NO_NODE
;
2220 LIST_HEAD(pagelist
);
2224 lru_cache_disable();
2226 for (i
= start
= 0; i
< nr_pages
; i
++) {
2227 const void __user
*p
;
2231 if (in_compat_syscall()) {
2234 if (get_user(cp
, compat_pages
+ i
))
2239 if (get_user(p
, pages
+ i
))
2242 if (get_user(node
, nodes
+ i
))
2246 if (node
< 0 || node
>= MAX_NUMNODES
)
2248 if (!node_state(node
, N_MEMORY
))
2252 if (!node_isset(node
, task_nodes
))
2255 if (current_node
== NUMA_NO_NODE
) {
2256 current_node
= node
;
2258 } else if (node
!= current_node
) {
2259 err
= move_pages_and_store_status(current_node
,
2260 &pagelist
, status
, start
, i
, nr_pages
);
2264 current_node
= node
;
2268 * Errors in the page lookup or isolation are not fatal and we simply
2269 * report them via status
2271 err
= add_page_for_migration(mm
, p
, current_node
, &pagelist
,
2272 flags
& MPOL_MF_MOVE_ALL
);
2275 /* The page is successfully queued for migration */
2280 * The move_pages() man page does not have an -EEXIST choice, so
2281 * use -EFAULT instead.
2287 * If the page is already on the target node (!err), store the
2288 * node, otherwise, store the err.
2290 err
= store_status(status
, i
, err
? : current_node
, 1);
2294 err
= move_pages_and_store_status(current_node
, &pagelist
,
2295 status
, start
, i
, nr_pages
);
2297 /* We have accounted for page i */
2302 current_node
= NUMA_NO_NODE
;
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
);
2316 * Determine the nodes of an array of pages and store it in an array of status.
2318 static void do_pages_stat_array(struct mm_struct
*mm
, unsigned long nr_pages
,
2319 const void __user
**pages
, int *status
)
2325 for (i
= 0; i
< nr_pages
; i
++) {
2326 unsigned long addr
= (unsigned long)(*pages
);
2327 struct vm_area_struct
*vma
;
2331 vma
= vma_lookup(mm
, addr
);
2335 /* FOLL_DUMP to ignore special (like zero) pages */
2336 page
= follow_page(vma
, addr
, FOLL_GET
| FOLL_DUMP
);
2338 err
= PTR_ERR(page
);
2346 if (!is_zone_device_page(page
))
2347 err
= page_to_nid(page
);
2357 mmap_read_unlock(mm
);
2360 static int get_compat_pages_array(const void __user
*chunk_pages
[],
2361 const void __user
* __user
*pages
,
2362 unsigned long chunk_nr
)
2364 compat_uptr_t __user
*pages32
= (compat_uptr_t __user
*)pages
;
2368 for (i
= 0; i
< chunk_nr
; i
++) {
2369 if (get_user(p
, pages32
+ i
))
2371 chunk_pages
[i
] = compat_ptr(p
);
2378 * Determine the nodes of a user array of pages and store it in
2379 * a user array of status.
2381 static int do_pages_stat(struct mm_struct
*mm
, unsigned long nr_pages
,
2382 const void __user
* __user
*pages
,
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
];
2390 unsigned long chunk_nr
= min(nr_pages
, DO_PAGES_STAT_CHUNK_NR
);
2392 if (in_compat_syscall()) {
2393 if (get_compat_pages_array(chunk_pages
, pages
,
2397 if (copy_from_user(chunk_pages
, pages
,
2398 chunk_nr
* sizeof(*chunk_pages
)))
2402 do_pages_stat_array(mm
, chunk_nr
, chunk_pages
, chunk_status
);
2404 if (copy_to_user(status
, chunk_status
, chunk_nr
* sizeof(*status
)))
2409 nr_pages
-= chunk_nr
;
2411 return nr_pages
? -EFAULT
: 0;
2414 static struct mm_struct
*find_mm_struct(pid_t pid
, nodemask_t
*mem_nodes
)
2416 struct task_struct
*task
;
2417 struct mm_struct
*mm
;
2420 * There is no need to check if current process has the right to modify
2421 * the specified process when they are same.
2425 *mem_nodes
= cpuset_mems_allowed(current
);
2429 /* Find the mm_struct */
2431 task
= find_task_by_vpid(pid
);
2434 return ERR_PTR(-ESRCH
);
2436 get_task_struct(task
);
2439 * Check if this process has the right to modify the specified
2440 * process. Use the regular "ptrace_may_access()" checks.
2442 if (!ptrace_may_access(task
, PTRACE_MODE_READ_REALCREDS
)) {
2444 mm
= ERR_PTR(-EPERM
);
2449 mm
= ERR_PTR(security_task_movememory(task
));
2452 *mem_nodes
= cpuset_mems_allowed(task
);
2453 mm
= get_task_mm(task
);
2455 put_task_struct(task
);
2457 mm
= ERR_PTR(-EINVAL
);
2462 * Move a list of pages in the address space of the currently executing
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
)
2470 struct mm_struct
*mm
;
2472 nodemask_t task_nodes
;
2475 if (flags
& ~(MPOL_MF_MOVE
|MPOL_MF_MOVE_ALL
))
2478 if ((flags
& MPOL_MF_MOVE_ALL
) && !capable(CAP_SYS_NICE
))
2481 mm
= find_mm_struct(pid
, &task_nodes
);
2486 err
= do_pages_move(mm
, task_nodes
, nr_pages
, pages
,
2487 nodes
, status
, flags
);
2489 err
= do_pages_stat(mm
, nr_pages
, pages
, status
);
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
)
2500 return kernel_move_pages(pid
, nr_pages
, pages
, nodes
, status
, flags
);
2503 #ifdef CONFIG_NUMA_BALANCING
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.
2508 static bool migrate_balanced_pgdat(struct pglist_data
*pgdat
,
2509 unsigned long nr_migrate_pages
)
2513 for (z
= pgdat
->nr_zones
- 1; z
>= 0; z
--) {
2514 struct zone
*zone
= pgdat
->node_zones
+ z
;
2516 if (!managed_zone(zone
))
2519 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
2520 if (!zone_watermark_ok(zone
, 0,
2521 high_wmark_pages(zone
) +
2530 static struct folio
*alloc_misplaced_dst_folio(struct folio
*src
,
2533 int nid
= (int) data
;
2534 int order
= folio_order(src
);
2535 gfp_t gfp
= __GFP_THISNODE
;
2538 gfp
|= GFP_TRANSHUGE_LIGHT
;
2540 gfp
|= GFP_HIGHUSER_MOVABLE
| __GFP_NOMEMALLOC
| __GFP_NORETRY
|
2542 gfp
&= ~__GFP_RECLAIM
;
2544 return __folio_alloc_node(gfp
, order
, nid
);
2548 * Prepare for calling migrate_misplaced_folio() by isolating the folio if
2549 * permitted. Must be called with the PTL still held.
2551 int migrate_misplaced_folio_prepare(struct folio
*folio
,
2552 struct vm_area_struct
*vma
, int node
)
2554 int nr_pages
= folio_nr_pages(folio
);
2555 pg_data_t
*pgdat
= NODE_DATA(node
);
2557 if (folio_is_file_lru(folio
)) {
2559 * Do not migrate file folios that are mapped in multiple
2560 * processes with execute permissions as they are probably
2563 * See folio_likely_mapped_shared() on possible imprecision
2564 * when we cannot easily detect if a folio is shared.
2566 if ((vma
->vm_flags
& VM_EXEC
) &&
2567 folio_likely_mapped_shared(folio
))
2571 * Do not migrate dirty folios as not all filesystems can move
2572 * dirty folios in MIGRATE_ASYNC mode which is a waste of
2575 if (folio_test_dirty(folio
))
2579 /* Avoid migrating to a node that is nearly full */
2580 if (!migrate_balanced_pgdat(pgdat
, nr_pages
)) {
2583 if (!(sysctl_numa_balancing_mode
& NUMA_BALANCING_MEMORY_TIERING
))
2585 for (z
= pgdat
->nr_zones
- 1; z
>= 0; z
--) {
2586 if (managed_zone(pgdat
->node_zones
+ z
))
2591 * If there are no managed zones, it should not proceed
2597 wakeup_kswapd(pgdat
->node_zones
+ z
, 0,
2598 folio_order(folio
), ZONE_MOVABLE
);
2602 if (!folio_isolate_lru(folio
))
2605 node_stat_mod_folio(folio
, NR_ISOLATED_ANON
+ folio_is_file_lru(folio
),
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.
2617 int migrate_misplaced_folio(struct folio
*folio
, struct vm_area_struct
*vma
,
2620 pg_data_t
*pgdat
= NODE_DATA(node
);
2622 unsigned int nr_succeeded
;
2623 LIST_HEAD(migratepages
);
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
);
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
,
2637 BUG_ON(!list_empty(&migratepages
));
2638 return nr_remaining
? -EAGAIN
: 0;
2640 #endif /* CONFIG_NUMA_BALANCING */
2641 #endif /* CONFIG_NUMA */